Fix previous fix of Bug#5849.
[emacs.git] / doc / lispref / package.texi
blob62fbc2a9a078ecd368acfd53870c57d62eb72021
1 @c -*-texinfo-*-
2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 2010-2011  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.
19 @menu
20 * Packaging Basics::        The basic concepts of Emacs Lisp packages.
21 * Simple Packages::         How to package a single .el file.
22 * Multi-file Packages::     How to package multiple files.
23 * Package Archives::        Maintaining package archives.
24 @end menu
26 @node Packaging Basics
27 @section Packaging Basics
28 @cindex package attributes
29 @cindex package name
30 @cindex package version
31 @cindex dependencies
32 @cindex package dependencies
34   A package is either a @dfn{simple package} or a @dfn{multi-file
35 package}.  A simple package is stored in a package archive as a single
36 Emacs Lisp file, while a multi-file package is stored as a tar file
37 (containing multiple Lisp files, and possibly non-Lisp files such as a
38 manual).
40   In ordinary usage, the difference between simple packages and
41 multi-file packages is relatively unimportant; the Package Menu
42 interface makes no distinction between them.  However, the procedure
43 for creating them differs, as explained in the following sections.
45   Each package (whether simple or multi-file) has certain
46 @dfn{attributes}:
48 @table @asis
49 @item Name
50 A short word (e.g. @samp{auctex}).  This is usually also the symbol
51 prefix used in the program (@pxref{Coding Conventions}).
53 @item Version
54 A version number, in a form that the function @code{version-to-list}
55 understands (e.g. @samp{11.86}).  Each release of a package should be
56 accompanied by an increase in the version number.
58 @item Brief description
59 This is shown when the package is listed in the Package Menu.  It
60 should occupy a single line, ideally in 36 characters or less.
62 @item Long description
63 This is shown in the buffer created by @kbd{C-h P}
64 (@code{describe-package}), following the package's brief description
65 and installation status.  It normally spans multiple lines, and should
66 fully describe the package and its capabilities.
68 @item Dependencies
69 A list of other packages (possibly including minimal acceptable
70 version numbers) on which this package depends.  The list may be
71 empty, meaning this package has no dependencies.  Otherwise,
72 installing this package also automatically installs its dependencies;
73 if any dependency cannot be found, the package cannot be installed.
74 @end table
76 @cindex content directory, package
77   Installing a package, either via the Package Menu, or via the
78 command @code{package-install-file}, creates a subdirectory of
79 @code{package-user-dir} named @file{@var{name}-@var{version}}, where
80 @var{name} is the package's name and @var{version} its version
81 (e.g. @file{~/.emacs.d/elpa/auctex-11.86/}).  We call this the
82 package's @dfn{content directory}.  It is where Emacs puts the
83 package's contents (the single Lisp file for a simple package, or the
84 files extracted from a multi-file package).
86 @cindex package autoloads
87   Emacs then searches every Lisp file in the content directory for
88 autoload magic comments (@pxref{Autoload}).  These autoload
89 definitions are saved to a file named @file{@var{name}-autoloads.el}
90 in the content directory.  They are typically used to autoload the
91 principal user commands defined in the package, but they can also
92 perform other tasks, such as adding an element to
93 @code{auto-mode-alist} (@pxref{Auto Major Mode}).  During this time,
94 Emacs will also byte-compile the Lisp files.
96   After installation, and (by default) each time Emacs is started, the
97 installed package is @dfn{activated}.  During activation, Emacs adds
98 the package's content directory to @code{load-path}, and evaluates the
99 autoload definitions in @file{@var{name}-autoloads.el}.
101   Note that a package typically does @emph{not} autoload every
102 function and variable defined within it---only the handful of commands
103 typically called to begin using the package.
105 @node Simple Packages
106 @section Simple Packages
107 @cindex single file package
108 @cindex simple package
110   A simple package consists of a single Emacs Lisp source file.  The
111 file must conform to the Emacs Lisp library header conventions
112 (@pxref{Library Headers}).  The package's attributes are taken from
113 the various headers, as illustrated by the following example:
115 @example
116 @group
117 ;;; superfrobnicator.el --- Frobnicate and bifurcate flanges
119 ;; Copyright (C) 2011 Free Software Foundation, Inc.
120 @end group
122 ;; Author: J. R. Hacker <jrh@@example.com>
123 ;; Version: 1.3
124 ;; Package-Requires: ((flange "1.0"))
125 ;; Keywords: frobnicate
127 @dots{}
129 ;;; Commentary:
131 ;; This package provides a minor mode to frobnicate and/or
132 ;; bifurcate any flanges you desire.  To activate it, just type
133 @dots{}
135 ;;;###autoload
136 (define-minor-mode superfrobnicator-mode
137 @dots{}
138 @end example
140   The name of the package is the same as the base name of the file, as
141 written on the first line.  Here, it is @samp{superfrobnicator}.
143   The brief description is also taken from the first line.  Here, it
144 is @samp{Frobnicate and bifurcate flanges}.
146   The version number comes from the @samp{Package-Version} header, if
147 it exists, or from the @samp{Version} header otherwise.  One or the
148 other @emph{must} be present.  Here, the version number is 1.3.
150   If the file has a @samp{;;; Commentary:} section, this section is
151 used as the long description.  (When displaying the description, Emacs
152 omits the @samp{;;; Commentary:} line, as well as the leading comment
153 characters in the commentary itself.)
155   If the file has a @samp{Package-Requires} header, that is used as
156 the package dependencies.  In the above example, the package depends
157 on the @samp{flange} package, version 1.0 or higher.  @xref{Library
158 Headers}, for a description of the @samp{Package-Requires} header.  If
159 the header is omitted, the package has no dependencies.
161   The file ought to also contain one or more autoload magic comments,
162 as explained in @ref{Packaging Basics}.  In the above example, a magic
163 comment autoloads @code{superfrobnicator-mode}.
165   @xref{Package Archives}, for a explanation of how to add a
166 single-file package to a package archive.
168 @node Multi-file Packages
169 @section Multi-file Packages
170 @cindex multi-file package
172   A multi-file package is less convenient to create than a single-file
173 package, but it offers more features: it can include multiple Emacs
174 Lisp files, an Info manual, and other file types (such as images).
176   Prior to installation, a multi-file package is stored in a package
177 archive as a tar file.  The tar file must be named
178 @file{@var{name}-@var{version}.tar}, where @var{name} is the package
179 name and @var{version} is the version number.  Its contents, once
180 extracted, must all appear in a directory named
181 @file{@var{name}-@var{version}}, the @dfn{content directory}
182 (@pxref{Packaging Basics}).  Files may also extract into
183 subdirectories of the content directory.
185   One of the files in the content directory must be named
186 @file{@var{name}-pkg.el}.  It must contain a single Lisp form,
187 consisting of a call to the function @code{define-package}, described
188 below.  This defines the package's version, brief description, and
189 requirements.
191   For example, if we distribute version 1.3 of the superfrobnicator as
192 a multi-file package, the tar file would be
193 @file{superfrobnicator-1.3.tar}.  Its contents would extract into the
194 directory @file{superfrobnicator-1.3}, and one of these would be the
195 file @file{superfrobnicator-pkg.el}.
197 @defun define-package name version &optional docstring requirements
198 This function defines a package.  @var{name} is the package name, a
199 string.  @var{version} is the version, as a string of a form that can
200 be understood by the function @code{version-to-list}.  @var{docstring}
201 is the brief description.
203 @var{requirements} is a list of required packages and their versions.
204 Each element in this list should have the form @code{(@var{dep-name}
205 @var{dep-version})}, where @var{dep-name} is a symbol whose name is
206 the dependency's package name, and @var{dep-version} is the
207 dependency's version (a string).
208 @end defun
210   If the content directory contains a file named @file{README}, this
211 file is used as the long description.
213   If the content directory contains a file named @file{dir}, this is
214 assumed to be an Info directory file made with @command{install-info}.
215 @xref{Invoking install-info, Invoking install-info, Invoking
216 install-info, texinfo, Texinfo}.  The relevant Info files should also
217 be present in the content directory.  In this case, Emacs will
218 automatically add the content directory to @code{Info-directory-list}
219 when the package is activated.
221   Do not include any @file{.elc} files in the package.  Those are
222 created when the package is installed.  Note that there is no way to
223 control the order in which files are byte-compiled.
225   Do not include any file named @file{@var{name}-autoloads.el}.  This
226 file is reserved for the package's autoload definitions
227 (@pxref{Packaging Basics}).  It is created automatically when the
228 package is installed, by searching all the Lisp files in the package
229 for autoload magic comments.
231   If the multi-file package contains auxiliary data files (such as
232 images), the package's Lisp code can refer to these files via the
233 variable @code{load-file-name} (@pxref{Loading}).  Here is an example:
235 @smallexample
236 (defconst superfrobnicator-base (file-name-directory load-file-name))
238 (defun superfrobnicator-fetch-image (file)
239   (expand-file-name file superfrobnicator-base))
240 @end smallexample
242 @node Package Archives
243 @section Creating and Maintaining Package Archives
244 @cindex package archive
246   Via the Package Menu, users may download packages from @dfn{package
247 archives}.  Such archives are specified by the variable
248 @code{package-archives}, whose default value contains a single entry:
249 the archive hosted by the GNU project at @url{elpa.gnu.org}.  This
250 section describes how to set up and maintain a package archive.
252 @cindex base location, package archive
253 @defopt package-archives
254 The value of this variable is an alist of package archives recognized
255 by the Emacs package manager.
257 Each alist element corresponds to one archive, and should have the
258 form @code{(@var{id} . @var{location})}, where @var{id} is the name of
259 the archive (a string) and @var{location} is its @dfn{base location}
260 (a string).
262 If the base location starts with @samp{http:}, it is treated as a HTTP
263 URL, and packages are downloaded from this archive via HTTP (as is the
264 case for the default GNU archive).
266 Otherwise, the base location should be a directory name.  In this
267 case, Emacs retrieves packages from this archive via ordinary file
268 access.  Such ``local'' archives are mainly useful for testing.
269 @end defopt
271   A package archive is simply a directory in which the package files,
272 and associated files, are stored.  If you want the archive to be
273 reachable via HTTP, this directory must be accessible to a web server.
274 How to accomplish this is beyond the scope of this manual.
276   A convenient way to set up and update a package archive is via the
277 @code{package-x} library.  This is included with Emacs, but not loaded
278 by default; type @kbd{M-x load-library @kbd{RET} package-x @kbd{RET}}
279 to load it, or add @code{(require 'package-x)} to your init file.
280 @xref{Lisp Libraries,, Lisp Libraries, emacs, The GNU Emacs Manual}.
281 Once loaded, you can make use of the following:
283 @defopt package-archive-upload-base
284 The value of this variable is the base location of a package archive,
285 as a directory name.  The commands in the @code{package-x} library
286 will use this base location.
288 The directory name should be absolute.  You may specify a remote name,
289 such as @file{/ssh:foo@@example.com:/var/www/packages/}, if the
290 package archive is on a different machine.  @xref{Remote Files,,
291 Remote Files, emacs, The GNU Emacs Manual}.
292 @end defopt
294 @deffn Command package-upload-file filename
295 This command prompts for @var{filename}, a file name, and uploads that
296 file to @code{package-archive-upload-base}.  The file must be either a
297 simple package (a @file{.el} file) or a multi-file package (a
298 @file{.tar} file); otherwise, an error is raised.  The package
299 attributes are automatically extracted, and the archive's contents
300 list is updated with this information.
302 If @code{package-archive-upload-base} does not specify a valid
303 directory, the function prompts interactively for one.  If the
304 directory does not exist, it is created.  The directory need not have
305 any initial contents (i.e., you can use this command to populate an
306 initially empty archive).
307 @end deffn
309 @deffn Command package-upload-buffer
310 This command is similar to @code{package-upload-file}, but instead of
311 prompting for a package file, it uploads the contents of the current
312 buffer.  The current buffer must be visiting a simple package (a
313 @file{.el} file) or a multi-file package (a @file{.tar} file);
314 otherwise, an error is raised.
315 @end deffn
317 @noindent
318 After you create an archive, remember that it is not accessible in the
319 Package Menu interface unless it is in @code{package-archives}.