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