home *** CD-ROM | disk | FTP | other *** search
- ;; autoload.mut : Delayed file loading.
- ;; Autoloading files means files are not loaded until they are used. This
- ;; can result in smaller init files and faster startup times.
- ;;
- ;; How to use:
- ;; (autoload function-name file-name)
- ;; When function-name is called, file-name is loaded and a call is
- ;; again made to function-name. file-name MUST have redefined
- ;; function-name (or you will find your self in a infinite loop).
- ;; (autoload-with-keypress file-name)
- ;; Same as autoload but the key that caused the autoload is pressed
- ;; again.
- ;; file-name MUST rebind the key (the usual case) or contain a
- ;; function with the same name as the function the key was bound
- ;; to. If not, you will load forever.
- ;; You would use this function (instead of autoload) when you want
- ;; (more than one key) to autoload a file and don't want to autoload
- ;; each function. This can save some code because lots of keys can
- ;; be bound to a single autoload function.
- ;;
- ;; Notes:
- ;; The function that calls autoload MUST be late bound else autoload
- ;; will be called every time the function is called. bind-to-key is
- ;; always later bound. For function calls use:
- ;; (floc "function-to-be-autoloaded" args) or
- ;; (floc "function-to-be-autoloaded" ()) if no args.
- ;; Autoloaded functions can take arguments - the args after fcn-name and
- ;; file-name are passed to the autoloaded function (if they are
- ;; passed to autoload: use (push-args) or push them explictly).
- ;;
- ;; Examples:
- ;; (defun foo { (floc "bar" 1 "two" 3) })
- ;; (defun bar { (autoload "bar" "bar.mco" (push-args 0)) })
- ;; Whenever foo or bar is called, bar.mco will loaded and bar called.
- ;; If foo is called, bar is called with the arg list: 1 "two" 3.
- ;; The next time foo is called, bar will be called directly: the
- ;; autoloading function will be by-passed.
- ;; (defun bar { (autoload-with-keypress "bar.mut") })
- ;; (bind-to-key bar "1")
- ;; (bind-to-key bar "2")
- ;; When 1 or 2 is pressed, bar.mco is loaded and the key is pressed
- ;; again. bar.mut MUST rebind 1 AND 2 in a MAIN or contain a
- ;; function named bar (not very useful because that would mean that
- ;; both 1 and 2 always run bar).
- ;;
- ;; How autoload works:
- ;; File one.mut contains:
- ;; (defun two { (autoload "two" "two.mut") }
- ;; (defun MAIN { (two) })
- ;; File two.mut contains:
- ;; (defun two { (msg "This is 2") })
- ;; When you load one.mco, MAIN is run and the function two is called.
- ;; two calls autoload and autoload loads two.mco. Here is where the
- ;; magic happens: When two.mco is loaded, the new function two
- ;; overwrites the old two in the (function-name, function-address)
- ;; table (the code for both is still loaded). By using late binding
- ;; (the (floc)), "two" is searched for in the table and the address
- ;; of the new two is found and that function is run. The sequence
- ;; (from the start) is: call two, load two.mco, overwrite the old
- ;; two in the table, search the table for "two", find the new two and
- ;; run it. An important thing to remember is the compiler can
- ;; use early (static) or late binding. If you don't tell it to use
- ;; late binding, it uses early (if it can). Thats why you need to be
- ;; sure and use late binding: so all this magic works. If a
- ;; function is static bound to autoload, autoload will be called
- ;; every time the first function is called, which is not what you
- ;; want. ME key binding is always late bound.
- ;; The reason key bindings don't go away when autoloading is because a
- ;; file other than the one the keys are originally bound in is
- ;; loaded. If one.mut had also contained:
- ;; (bind-to-key "two" "1")
- ;; (and no MAIN) then: when 1 is pressed, two.mco is loaded. The
- ;; new function two overwrites the old two but the key is not
- ;; collected (since it didn't point to a function in two.mco) and now
- ;; points to the new two. Pressing 1 will run the new two. If you
- ;; load two.mco again, all keys that point to functions in two.mco
- ;; are garbage collected and you would lose the key.
- ;;
- ;; C Durland Public Domain
-
- (defun
- autoload (string fcn-name file-name) (optional-args) HIDDEN
- {
- (if (load file-name)
- (floc (fcn-name) (push-args 2))
- (msg "autoload: Could not load " file-name)
- )
- }
- autoload-with-keypress (string file-name) HIDDEN
- {
- (if (load file-name)
- (exe-key (key-pressed))
- (msg "autoload: Could not load " file-name)
- )
- }
- )
-