some tips for how to write a emacs lisp package -- elisp's loading mechanisms

1. elisp's loading mechanisms

In order to learn how to write an elisp package, you must be familiar with some ways of how to load your package to emacs. That means developers need to master following functions. Some of them puzzled me at  the past.
Functions: load-file, load, require, autoload
Following hyperlinks webpage removes my confusion, and give a good explanation of elisp's loading  mechanisms.
http://ergoemacs.org/emacs/elisp_library_system.html?1352689199

Function NamePurposeTech DetailComment
load-fileLoad a file.Load one specific file.Use this if you have one very SPECIFIC file at one particular file path.
loadLoad a file.Load a file by searching thru var “load-path”. Also, tries to load a compiled version (.elc) if exists.Use this if the path for the file is not known in advance, and you are using a file name without full paths, such as “undo” or “undo.el”, and you want to load the compiled version if it exists (undo.elc).
requireLoad a package if it has not already been loaded.Checks the var “features”, if symbol is not there, then call load to load it.Best used in elisp source code, similar to other lang's “require” or “import”.
autoloadLoad a file only when a function is called.Associate a function name with a file path. When the function is called, load the file, and execute the function.If you are writing a major mode, it's good to have your package installation go by autoload (if possible). It saves startup time.

We can see that require is better than load, autoload is better than require.

2. auto generated install files from your package.
http://www.gnu.org/software/emacs/manual/html_node/elisp/Autoload.html

autoload function just export function symbol but not load the function into emacs, so it saves emacs startup times. Some big packages are arranged by autoload functions. Emacs have some functions to auto generate installation files. Customers just need to load those installation files to install those packages.  You can follow those steps in your development.

1. you write some functions whether interactive or not, then add a magic comment before the function which you want to export, default magic comment is ';;;###autoload', which is defined by variable named generate-autoload-cookie.

2. use command M-x update-file-autoloads or update-directory-autoloads to generate autoload declarations into a target file which you can choose. The default target file name is defined by a local variable named generate-autoload-file. The autoload declarations come from functions with magic comment (default ';;;###autoload').

following is the relative variables and functions in autoload file generation.

- Variables: generate-autoload-cookie generate-autoload-file.
- Functions: update-file-autoloads update-director-autoloads

Comments

Popular posts from this blog

Bluedroid stack in android

How to setup a NAT server?

Network programming in elisp