Misc manual updates.
[emacs.git] / doc / lispref / package.texi
blobb17f13b6b89e403909400441100cf24b2c4388fd
1 @c -*-texinfo-*-
2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 2010-2012  Free Software Foundation, Inc.
4 @c See the file elisp.texi for copying conditions.
5 @setfilename ../../info/package
6 @node Packaging, Antinews, System Interface, Top
7 @chapter Preparing Lisp code for distribution
8 @cindex package
9 @cindex Lisp package
11   Emacs provides a standard way to distribute Emacs Lisp code to
12 users.  A @dfn{package} is a collection of one or more files,
13 formatted and bundled in such a way that users can easily download,
14 install, uninstall, and upgrade it.
16   The following sections describe how to create a package, and how to
17 put it in a @dfn{package archive} for others to download.
18 @xref{Packages,,, emacs, The GNU Emacs Manual}, for a description of
19 user-level features of the packaging system.
21 @menu
22 * Packaging Basics::        The basic concepts of Emacs Lisp packages.
23 * Simple Packages::         How to package a single .el file.
24 * Multi-file Packages::     How to package multiple files.
25 * Package Archives::        Maintaining package archives.
26 @end menu
28 @node Packaging Basics
29 @section Packaging Basics
30 @cindex package attributes
31 @cindex package name
32 @cindex package version
33 @cindex dependencies
34 @cindex package dependencies
36   A package is either a @dfn{simple package} or a @dfn{multi-file
37 package}.  A simple package is stored in a package archive as a single
38 Emacs Lisp file, while a multi-file package is stored as a tar file
39 (containing multiple Lisp files, and possibly non-Lisp files such as a
40 manual).
42   In ordinary usage, the difference between simple packages and
43 multi-file packages is relatively unimportant; the Package Menu
44 interface makes no distinction between them.  However, the procedure
45 for creating them differs, as explained in the following sections.
47   Each package (whether simple or multi-file) has certain
48 @dfn{attributes}:
50 @table @asis
51 @item Name
52 A short word (e.g. @samp{auctex}).  This is usually also the symbol
53 prefix used in the program (@pxref{Coding Conventions}).
55 @item Version
56 A version number, in a form that the function @code{version-to-list}
57 understands (e.g. @samp{11.86}).  Each release of a package should be
58 accompanied by an increase in the version number.
60 @item Brief description
61 This is shown when the package is listed in the Package Menu.  It
62 should occupy a single line, ideally in 36 characters or less.
64 @item Long description
65 This is shown in the buffer created by @kbd{C-h P}
66 (@code{describe-package}), following the package's brief description
67 and installation status.  It normally spans multiple lines, and should
68 fully describe the package's capabilities and how to begin using it
69 once it is installed.
71 @item Dependencies
72 A list of other packages (possibly including minimal acceptable
73 version numbers) on which this package depends.  The list may be
74 empty, meaning this package has no dependencies.  Otherwise,
75 installing this package also automatically installs its dependencies;
76 if any dependency cannot be found, the package cannot be installed.
77 @end table
79 @cindex content directory, package
80   Installing a package, either via the Package Menu, or via the
81 command @code{package-install-file}, creates a subdirectory of
82 @code{package-user-dir} named @file{@var{name}-@var{version}}, where
83 @var{name} is the package's name and @var{version} its version
84 (e.g. @file{~/.emacs.d/elpa/auctex-11.86/}).  We call this the
85 package's @dfn{content directory}.  It is where Emacs puts the
86 package's contents (the single Lisp file for a simple package, or the
87 files extracted from a multi-file package).
89 @cindex package autoloads
90   Emacs then searches every Lisp file in the content directory for
91 autoload magic comments (@pxref{Autoload}).  These autoload
92 definitions are saved to a file named @file{@var{name}-autoloads.el}
93 in the content directory.  They are typically used to autoload the
94 principal user commands defined in the package, but they can also
95 perform other tasks, such as adding an element to
96 @code{auto-mode-alist} (@pxref{Auto Major Mode}).  Note that a package
97 typically does @emph{not} autoload every function and variable defined
98 within it---only the handful of commands typically called to begin
99 using the package.  Emacs then byte-compiles every Lisp file in the
100 package.
102   After installation, the installed package is @dfn{loaded}: Emacs
103 adds the package's content directory to @code{load-path}, and
104 evaluates the autoload definitions in @file{@var{name}-autoloads.el}.
106   Whenever Emacs starts up, it automatically calls the function
107 @code{package-initialize} to load installed packages.  This is done
108 after loading the init file and abbrev file (if any) and before
109 running @code{after-init-hook} (@pxref{Startup Summary}).  Automatic
110 package loading is disabled if the user option
111 @code{package-enable-at-startup} is @code{nil}.
113 @deffn Command package-initialize &optional no-activate
114 This function initializes Emacs' internal record of which packages are
115 installed, and loads them.  The user option @code{package-load-list}
116 specifies which packages to load; by default, all installed packages
117 are loaded.  @xref{Package Installation,,, emacs, The GNU Emacs
118 Manual}.
120 The optional argument @var{no-activate}, if non-@code{nil}, causes
121 Emacs to update its record of installed packages without actually
122 loading them; it is for internal use only.
123 @end deffn
125 @node Simple Packages
126 @section Simple Packages
127 @cindex single file package
128 @cindex simple package
130   A simple package consists of a single Emacs Lisp source file.  The
131 file must conform to the Emacs Lisp library header conventions
132 (@pxref{Library Headers}).  The package's attributes are taken from
133 the various headers, as illustrated by the following example:
135 @example
136 @group
137 ;;; superfrobnicator.el --- Frobnicate and bifurcate flanges
139 ;; Copyright (C) 2011 Free Software Foundation, Inc.
140 @end group
142 ;; Author: J. R. Hacker <jrh@@example.com>
143 ;; Version: 1.3
144 ;; Package-Requires: ((flange "1.0"))
145 ;; Keywords: frobnicate
147 @dots{}
149 ;;; Commentary:
151 ;; This package provides a minor mode to frobnicate and/or
152 ;; bifurcate any flanges you desire.  To activate it, just type
153 @dots{}
155 ;;;###autoload
156 (define-minor-mode superfrobnicator-mode
157 @dots{}
158 @end example
160   The name of the package is the same as the base name of the file, as
161 written on the first line.  Here, it is @samp{superfrobnicator}.
163   The brief description is also taken from the first line.  Here, it
164 is @samp{Frobnicate and bifurcate flanges}.
166   The version number comes from the @samp{Package-Version} header, if
167 it exists, or from the @samp{Version} header otherwise.  One or the
168 other @emph{must} be present.  Here, the version number is 1.3.
170   If the file has a @samp{;;; Commentary:} section, this section is
171 used as the long description.  (When displaying the description, Emacs
172 omits the @samp{;;; Commentary:} line, as well as the leading comment
173 characters in the commentary itself.)
175   If the file has a @samp{Package-Requires} header, that is used as
176 the package dependencies.  In the above example, the package depends
177 on the @samp{flange} package, version 1.0 or higher.  @xref{Library
178 Headers}, for a description of the @samp{Package-Requires} header.  If
179 the header is omitted, the package has no dependencies.
181   The file ought to also contain one or more autoload magic comments,
182 as explained in @ref{Packaging Basics}.  In the above example, a magic
183 comment autoloads @code{superfrobnicator-mode}.
185   @xref{Package Archives}, for a explanation of how to add a
186 single-file package to a package archive.
188 @node Multi-file Packages
189 @section Multi-file Packages
190 @cindex multi-file package
192   A multi-file package is less convenient to create than a single-file
193 package, but it offers more features: it can include multiple Emacs
194 Lisp files, an Info manual, and other file types (such as images).
196   Prior to installation, a multi-file package is stored in a package
197 archive as a tar file.  The tar file must be named
198 @file{@var{name}-@var{version}.tar}, where @var{name} is the package
199 name and @var{version} is the version number.  Its contents, once
200 extracted, must all appear in a directory named
201 @file{@var{name}-@var{version}}, the @dfn{content directory}
202 (@pxref{Packaging Basics}).  Files may also extract into
203 subdirectories of the content directory.
205   One of the files in the content directory must be named
206 @file{@var{name}-pkg.el}.  It must contain a single Lisp form,
207 consisting of a call to the function @code{define-package}, described
208 below.  This defines the package's version, brief description, and
209 requirements.
211   For example, if we distribute version 1.3 of the superfrobnicator as
212 a multi-file package, the tar file would be
213 @file{superfrobnicator-1.3.tar}.  Its contents would extract into the
214 directory @file{superfrobnicator-1.3}, and one of these would be the
215 file @file{superfrobnicator-pkg.el}.
217 @defun define-package name version &optional docstring requirements
218 This function defines a package.  @var{name} is the package name, a
219 string.  @var{version} is the version, as a string of a form that can
220 be understood by the function @code{version-to-list}.  @var{docstring}
221 is the brief description.
223 @var{requirements} is a list of required packages and their versions.
224 Each element in this list should have the form @code{(@var{dep-name}
225 @var{dep-version})}, where @var{dep-name} is a symbol whose name is
226 the dependency's package name, and @var{dep-version} is the
227 dependency's version (a string).
228 @end defun
230   If the content directory contains a file named @file{README}, this
231 file is used as the long description.
233   If the content directory contains a file named @file{dir}, this is
234 assumed to be an Info directory file made with @command{install-info}.
235 @xref{Invoking install-info, Invoking install-info, Invoking
236 install-info, texinfo, Texinfo}.  The relevant Info files should also
237 be present in the content directory.  In this case, Emacs will
238 automatically add the content directory to @code{Info-directory-list}
239 when the package is activated.
241   Do not include any @file{.elc} files in the package.  Those are
242 created when the package is installed.  Note that there is no way to
243 control the order in which files are byte-compiled.
245   Do not include any file named @file{@var{name}-autoloads.el}.  This
246 file is reserved for the package's autoload definitions
247 (@pxref{Packaging Basics}).  It is created automatically when the
248 package is installed, by searching all the Lisp files in the package
249 for autoload magic comments.
251   If the multi-file package contains auxiliary data files (such as
252 images), the package's Lisp code can refer to these files via the
253 variable @code{load-file-name} (@pxref{Loading}).  Here is an example:
255 @smallexample
256 (defconst superfrobnicator-base (file-name-directory load-file-name))
258 (defun superfrobnicator-fetch-image (file)
259   (expand-file-name file superfrobnicator-base))
260 @end smallexample
262 @node Package Archives
263 @section Creating and Maintaining Package Archives
264 @cindex package archive
266   Via the Package Menu, users may download packages from @dfn{package
267 archives}.  Such archives are specified by the variable
268 @code{package-archives}, whose default value contains a single entry:
269 the archive hosted by the GNU project at @url{elpa.gnu.org}.  This
270 section describes how to set up and maintain a package archive.
272 @cindex base location, package archive
273 @defopt package-archives
274 The value of this variable is an alist of package archives recognized
275 by the Emacs package manager.
277 Each alist element corresponds to one archive, and should have the
278 form @code{(@var{id} . @var{location})}, where @var{id} is the name of
279 the archive (a string) and @var{location} is its @dfn{base location}
280 (a string).
282 If the base location starts with @samp{http:}, it is treated as a HTTP
283 URL, and packages are downloaded from this archive via HTTP (as is the
284 case for the default GNU archive).
286 Otherwise, the base location should be a directory name.  In this
287 case, Emacs retrieves packages from this archive via ordinary file
288 access.  Such ``local'' archives are mainly useful for testing.
289 @end defopt
291   A package archive is simply a directory in which the package files,
292 and associated files, are stored.  If you want the archive to be
293 reachable via HTTP, this directory must be accessible to a web server.
294 How to accomplish this is beyond the scope of this manual.
296   A convenient way to set up and update a package archive is via the
297 @code{package-x} library.  This is included with Emacs, but not loaded
298 by default; type @kbd{M-x load-library @kbd{RET} package-x @kbd{RET}}
299 to load it, or add @code{(require 'package-x)} to your init file.
300 @xref{Lisp Libraries,, Lisp Libraries, emacs, The GNU Emacs Manual}.
301 Once loaded, you can make use of the following:
303 @defopt package-archive-upload-base
304 The value of this variable is the base location of a package archive,
305 as a directory name.  The commands in the @code{package-x} library
306 will use this base location.
308 The directory name should be absolute.  You may specify a remote name,
309 such as @file{/ssh:foo@@example.com:/var/www/packages/}, if the
310 package archive is on a different machine.  @xref{Remote Files,,
311 Remote Files, emacs, The GNU Emacs Manual}.
312 @end defopt
314 @deffn Command package-upload-file filename
315 This command prompts for @var{filename}, a file name, and uploads that
316 file to @code{package-archive-upload-base}.  The file must be either a
317 simple package (a @file{.el} file) or a multi-file package (a
318 @file{.tar} file); otherwise, an error is raised.  The package
319 attributes are automatically extracted, and the archive's contents
320 list is updated with this information.
322 If @code{package-archive-upload-base} does not specify a valid
323 directory, the function prompts interactively for one.  If the
324 directory does not exist, it is created.  The directory need not have
325 any initial contents (i.e., you can use this command to populate an
326 initially empty archive).
327 @end deffn
329 @deffn Command package-upload-buffer
330 This command is similar to @code{package-upload-file}, but instead of
331 prompting for a package file, it uploads the contents of the current
332 buffer.  The current buffer must be visiting a simple package (a
333 @file{.el} file) or a multi-file package (a @file{.tar} file);
334 otherwise, an error is raised.
335 @end deffn
337 @noindent
338 After you create an archive, remember that it is not accessible in the
339 Package Menu interface unless it is in @code{package-archives}.