Merge remote-tracking branch 'github/pr/674'.
[gentoo.git] / eclass / elisp-common.eclass
1 # Copyright 1999-2015 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 # $Id$
4 #
5 # @ECLASS: elisp-common.eclass
6 # @MAINTAINER:
7 # Gentoo GNU Emacs project <gnu-emacs@gentoo.org>
8 # @AUTHOR:
9 # Matthew Kennedy <mkennedy@gentoo.org>
10 # Jeremy Maitin-Shepard <jbms@attbi.com>
11 # Mamoru Komachi <usata@gentoo.org>
12 # Christian Faulhammer <fauli@gentoo.org>
13 # Ulrich Müller <ulm@gentoo.org>
14 # @BLURB: Emacs-related installation utilities
15 # @DESCRIPTION:
16 #
17 # Usually you want to use this eclass for (optional) GNU Emacs support
18 # of your package.  This is NOT for XEmacs!
19 #
20 # Many of the steps here are sometimes done by the build system of your
21 # package (especially compilation), so this is mainly for standalone
22 # elisp files you gathered from somewhere else.
23 #
24 # When relying on the emacs USE flag, you need to add
25 #
26 # @CODE
27 #       emacs? ( virtual/emacs )
28 # @CODE
29 #
30 # to your DEPEND/RDEPEND line and use the functions provided here to
31 # bring the files to the correct locations.
32 #
33 # If your package requires a minimum Emacs version, e.g. Emacs 24, then
34 # the dependency should be on >=virtual/emacs-24 instead.  Because the
35 # user can select the Emacs executable with eselect, you should also
36 # make sure that the active Emacs version is sufficient.  This can be
37 # tested with function elisp-need-emacs(), which would typically be
38 # called from pkg_setup(), as in the following example:
39 #
40 # @CODE
41 #       elisp-need-emacs 24 || die "Emacs version too low"
42 # @CODE
43 #
44 # Please note that such tests should be limited to packages that are
45 # known to fail with lower Emacs versions; the standard case is to
46 # depend on virtual/emacs without version.
47 #
48 # @ROFF .SS
49 # src_compile() usage:
50 #
51 # An elisp file is compiled by the elisp-compile() function defined
52 # here and simply takes the source files as arguments.  The case of
53 # interdependent elisp files is also supported, since the current
54 # directory is added to the load-path which makes sure that all files
55 # are loadable.
56 #
57 # @CODE
58 #       elisp-compile *.el
59 # @CODE
60 #
61 # Function elisp-make-autoload-file() can be used to generate a file
62 # with autoload definitions for the lisp functions.  It takes the output
63 # file name (default: "${PN}-autoloads.el") and a list of directories
64 # (default: working directory) as its arguments.  Use of this function
65 # requires that the elisp source files contain magic ";;;###autoload"
66 # comments.  See the Emacs Lisp Reference Manual (node "Autoload") for
67 # a detailed explanation.
68 #
69 # @ROFF .SS
70 # src_install() usage:
71 #
72 # The resulting compiled files (.elc) should be put in a subdirectory of
73 # /usr/share/emacs/site-lisp/ which is named after the first argument
74 # of elisp-install().  The following parameters are the files to be put
75 # in that directory.  Usually the subdirectory should be ${PN}, you can
76 # choose something else, but remember to tell elisp-site-file-install()
77 # (see below) the change, as it defaults to ${PN}.
78 #
79 # @CODE
80 #       elisp-install ${PN} *.el *.elc
81 # @CODE
82 #
83 # To let the Emacs support be activated by Emacs on startup, you need
84 # to provide a site file (shipped in ${FILESDIR}) which contains the
85 # startup code (have a look in the documentation of your software).
86 # Normally this would look like this:
87 #
88 # @CODE
89 #       (add-to-list 'load-path "@SITELISP@")
90 #       (add-to-list 'auto-mode-alist '("\\.csv\\'" . csv-mode))
91 #       (autoload 'csv-mode "csv-mode" "Major mode for csv files." t)
92 # @CODE
93 #
94 # If your Emacs support files are installed in a subdirectory of
95 # /usr/share/emacs/site-lisp/ (which is strongly recommended), you need
96 # to extend Emacs' load-path as shown in the first non-comment line.
97 # The elisp-site-file-install() function of this eclass will replace
98 # "@SITELISP@" and "@SITEETC@" by the actual paths.
99 #
100 # The next line tells Emacs to load the mode opening a file ending
101 # with ".csv" and load functions depending on the context and needed
102 # features.  Be careful though.  Commands as "load-library" or "require"
103 # bloat the editor as they are loaded on every startup.  When having
104 # many Emacs support files, users may be annoyed by the start-up time.
105 # Also avoid keybindings as they might interfere with the user's
106 # settings.  Give a hint in pkg_postinst(), which should be enough.
107 # The guiding principle is that emerging your package should not by
108 # itself cause a change of standard Emacs behaviour.
109 #
110 # The naming scheme for this site-init file matches the shell pattern
111 # "[1-8][0-9]*-gentoo*.el", where the two digits at the beginning define
112 # the loading order (numbers below 10 or above 89 are reserved for
113 # internal use).  So if your initialisation depends on another Emacs
114 # package, your site file's number must be higher!  If there are no such
115 # interdependencies then the number should be 50.  Otherwise, numbers
116 # divisible by 10 are preferred.
117 #
118 # Best practice is to define a SITEFILE variable in the global scope of
119 # your ebuild (e.g., right after S or RDEPEND):
120 #
121 # @CODE
122 #       SITEFILE="50${PN}-gentoo.el"
123 # @CODE
124 #
125 # Which is then installed by
126 #
127 # @CODE
128 #       elisp-site-file-install "${FILESDIR}/${SITEFILE}"
129 # @CODE
130 #
131 # in src_install().  Any characters after the "-gentoo" part and before
132 # the extension will be stripped from the destination file's name.
133 # For example, a file "50${PN}-gentoo-${PV}.el" will be installed as
134 # "50${PN}-gentoo.el".  If your subdirectory is not named ${PN}, give
135 # the differing name as second argument.
136 #
137 # @ROFF .SS
138 # pkg_postinst() / pkg_postrm() usage:
139 #
140 # After that you need to recreate the start-up file of Emacs after
141 # emerging and unmerging by using
142 #
143 # @CODE
144 #       pkg_postinst() {
145 #               elisp-site-regen
146 #       }
147 #
148 #       pkg_postrm() {
149 #               elisp-site-regen
150 #       }
151 # @CODE
152 #
153 # When having optional Emacs support, you should prepend "use emacs &&"
154 # to above calls of elisp-site-regen().
155 # Don't use "has_version virtual/emacs"!  When unmerging the state of
156 # the emacs USE flag is taken from the package database and not from the
157 # environment, so it is no problem when you unset USE=emacs between
158 # merge and unmerge of a package.
159
160 # @ECLASS-VARIABLE: SITELISP
161 # @DESCRIPTION:
162 # Directory where packages install Emacs Lisp files.
163 SITELISP=/usr/share/emacs/site-lisp
164
165 # @ECLASS-VARIABLE: SITEETC
166 # @DESCRIPTION:
167 # Directory where packages install miscellaneous (not Lisp) files.
168 SITEETC=/usr/share/emacs/etc
169
170 # @ECLASS-VARIABLE: EMACS
171 # @DESCRIPTION:
172 # Path of Emacs executable.
173 EMACS=${EPREFIX}/usr/bin/emacs
174
175 # @ECLASS-VARIABLE: EMACSFLAGS
176 # @DESCRIPTION:
177 # Flags for executing Emacs in batch mode.
178 # These work for Emacs versions 18-24, so don't change them.
179 EMACSFLAGS="-batch -q --no-site-file"
180
181 # @ECLASS-VARIABLE: BYTECOMPFLAGS
182 # @DESCRIPTION:
183 # Emacs flags used for byte-compilation in elisp-compile().
184 BYTECOMPFLAGS="-L ."
185
186 # @FUNCTION: elisp-emacs-version
187 # @RETURN: exit status of Emacs
188 # @DESCRIPTION:
189 # Output version of currently active Emacs.
190
191 elisp-emacs-version() {
192         local version ret
193         # The following will work for at least versions 18-24.
194         echo "(princ emacs-version)" >"${T}"/emacs-version.el
195         version=$(
196                 # EMACS could be a microemacs variant that ignores the -batch
197                 # option and would therefore hang, waiting for user interaction.
198                 # Redirecting stdin and unsetting TERM and DISPLAY will cause
199                 # most of them to exit with an error.
200                 unset TERM DISPLAY
201                 ${EMACS} ${EMACSFLAGS} -l "${T}"/emacs-version.el </dev/null
202         )
203         ret=$?
204         rm -f "${T}"/emacs-version.el
205         if [[ ${ret} -ne 0 ]]; then
206                 eerror "elisp-emacs-version: Failed to run ${EMACS}"
207                 return ${ret}
208         fi
209         if [[ -z ${version} ]]; then
210                 eerror "elisp-emacs-version: Could not determine Emacs version"
211                 return 1
212         fi
213         echo "${version}"
214 }
215
216 # @FUNCTION: elisp-need-emacs
217 # @USAGE: <version>
218 # @RETURN: 0 if true, 1 if false, 2 if trouble
219 # @DESCRIPTION:
220 # Test if the eselected Emacs version is at least the major version
221 # of GNU Emacs specified as argument.
222
223 elisp-need-emacs() {
224         local need_emacs=$1 have_emacs
225         have_emacs=$(elisp-emacs-version) || return 2
226         einfo "Emacs version: ${have_emacs}"
227         if [[ ${have_emacs} =~ XEmacs|Lucid ]]; then
228                 eerror "This package needs GNU Emacs."
229                 return 1
230         fi
231         if ! [[ ${have_emacs%%.*} -ge ${need_emacs%%.*} ]]; then
232                 eerror "This package needs at least Emacs ${need_emacs%%.*}."
233                 eerror "Use \"eselect emacs\" to select the active version."
234                 return 1
235         fi
236         return 0
237 }
238
239 # @FUNCTION: elisp-compile
240 # @USAGE: <list of elisp files>
241 # @DESCRIPTION:
242 # Byte-compile Emacs Lisp files.
243 #
244 # This function uses GNU Emacs to byte-compile all ".el" specified by
245 # its arguments.  The resulting byte-code (".elc") files are placed in
246 # the same directory as their corresponding source file.
247 #
248 # The current directory is added to the load-path.  This will ensure
249 # that interdependent Emacs Lisp files are visible between themselves,
250 # in case they require or load one another.
251
252 elisp-compile() {
253         ebegin "Compiling GNU Emacs Elisp files"
254         ${EMACS} ${EMACSFLAGS} ${BYTECOMPFLAGS} -f batch-byte-compile "$@"
255         eend $? "elisp-compile: batch-byte-compile failed" || die
256 }
257
258 # @FUNCTION: elisp-make-autoload-file
259 # @USAGE: [output file] [list of directories]
260 # @DESCRIPTION:
261 # Generate a file with autoload definitions for the lisp functions.
262
263 elisp-make-autoload-file() {
264         local f="${1:-${PN}-autoloads.el}" null="" page=$'\f'
265         shift
266         ebegin "Generating autoload file for GNU Emacs"
267
268         cat >"${f}" <<-EOF
269         ;;; ${f##*/} --- autoloads for ${PN}
270
271         ;;; Commentary:
272         ;; Automatically generated by elisp-common.eclass
273         ;; DO NOT EDIT THIS FILE
274
275         ;;; Code:
276         ${page}
277         ;; Local ${null}Variables:
278         ;; version-control: never
279         ;; no-byte-compile: t
280         ;; no-update-autoloads: t
281         ;; End:
282
283         ;;; ${f##*/} ends here
284         EOF
285
286         ${EMACS} ${EMACSFLAGS} \
287                 --eval "(setq make-backup-files nil)" \
288                 --eval "(setq generated-autoload-file (expand-file-name \"${f}\"))" \
289                 -f batch-update-autoloads "${@-.}"
290
291         eend $? "elisp-make-autoload-file: batch-update-autoloads failed" || die
292 }
293
294 # @FUNCTION: elisp-install
295 # @USAGE: <subdirectory> <list of files>
296 # @DESCRIPTION:
297 # Install files in SITELISP directory.
298
299 elisp-install() {
300         local subdir="$1"
301         shift
302         ebegin "Installing Elisp files for GNU Emacs support"
303         ( # subshell to avoid pollution of calling environment
304                 insinto "${SITELISP}/${subdir}"
305                 doins "$@"
306         )
307         eend $? "elisp-install: doins failed" || die
308 }
309
310 # @FUNCTION: elisp-site-file-install
311 # @USAGE: <site-init file> [subdirectory]
312 # @DESCRIPTION:
313 # Install Emacs site-init file in SITELISP directory.  Automatically
314 # inserts a standard comment header with the name of the package (unless
315 # it is already present).  Tokens @SITELISP@ and @SITEETC@ are replaced
316 # by the path to the package's subdirectory in SITELISP and SITEETC,
317 # respectively.
318
319 elisp-site-file-install() {
320         local sf="${1##*/}" my_pn="${2:-${PN}}" ret
321         local header=";;; ${PN} site-lisp configuration"
322
323         [[ ${sf} == [0-9][0-9]*-gentoo*.el ]] \
324                 || ewarn "elisp-site-file-install: bad name of site-init file"
325         [[ ${sf%-gentoo*.el} != "${sf}" ]] && sf="${sf%-gentoo*.el}-gentoo.el"
326         sf="${T}/${sf}"
327         ebegin "Installing site initialisation file for GNU Emacs"
328         [[ $1 = "${sf}" ]] || cp "$1" "${sf}"
329         sed -i -e "1{:x;/^\$/{n;bx;};/^;.*${PN}/I!s:^:${header}\n\n:;1s:^:\n:;}" \
330                 -e "s:@SITELISP@:${EPREFIX}${SITELISP}/${my_pn}:g" \
331                 -e "s:@SITEETC@:${EPREFIX}${SITEETC}/${my_pn}:g;\$q" "${sf}"
332         ( # subshell to avoid pollution of calling environment
333                 insinto "${SITELISP}/site-gentoo.d"
334                 doins "${sf}"
335         )
336         ret=$?
337         rm -f "${sf}"
338         eend ${ret} "elisp-site-file-install: doins failed" || die
339 }
340
341 # @FUNCTION: elisp-site-regen
342 # @DESCRIPTION:
343 # Regenerate the site-gentoo.el file, based on packages' site
344 # initialisation files in the /usr/share/emacs/site-lisp/site-gentoo.d/
345 # directory.
346
347 elisp-site-regen() {
348         local sitelisp=${ROOT}${EPREFIX}${SITELISP}
349         local sf i ret=0 null="" page=$'\f'
350         local -a sflist
351
352         if [[ ${EBUILD_PHASE} = *rm && ! -e ${sitelisp}/site-gentoo.el ]]; then
353                 ewarn "Refusing to create site-gentoo.el in ${EBUILD_PHASE} phase."
354                 return 0
355         fi
356
357         [[ -d ${sitelisp} ]] \
358                 || die "elisp-site-regen: Directory ${sitelisp} does not exist"
359
360         [[ -d ${T} ]] \
361                 || die "elisp-site-regen: Temporary directory ${T} does not exist"
362
363         ebegin "Regenerating site-gentoo.el for GNU Emacs (${EBUILD_PHASE})"
364
365         for sf in "${sitelisp}"/site-gentoo.d/[0-9][0-9]*.el; do
366                 [[ -r ${sf} ]] && sflist+=("${sf}")
367         done
368
369         cat <<-EOF >"${T}"/site-gentoo.el || ret=$?
370         ;;; site-gentoo.el --- site initialisation for Gentoo-installed packages
371
372         ;;; Commentary:
373         ;; Automatically generated by elisp-common.eclass
374         ;; DO NOT EDIT THIS FILE
375
376         ;;; Code:
377         EOF
378         # Use sed instead of cat here, since files may miss a trailing newline.
379         sed '$q' "${sflist[@]}" </dev/null >>"${T}"/site-gentoo.el || ret=$?
380         cat <<-EOF >>"${T}"/site-gentoo.el || ret=$?
381
382         ${page}
383         (provide 'site-gentoo)
384
385         ;; Local ${null}Variables:
386         ;; no-byte-compile: t
387         ;; buffer-read-only: t
388         ;; End:
389
390         ;;; site-gentoo.el ends here
391         EOF
392
393         if [[ ${ret} -ne 0 ]]; then
394                 eend ${ret} "elisp-site-regen: Writing site-gentoo.el failed."
395                 die
396         elif cmp -s "${sitelisp}"/site-gentoo.el "${T}"/site-gentoo.el; then
397                 # This prevents outputting unnecessary text when there
398                 # was actually no change.
399                 # A case is a remerge where we have doubled output.
400                 rm -f "${T}"/site-gentoo.el
401                 eend
402                 einfo "... no changes."
403         else
404                 mv "${T}"/site-gentoo.el "${sitelisp}"/site-gentoo.el
405                 eend $? "elisp-site-regen: Replacing site-gentoo.el failed" || die
406                 case ${#sflist[@]} in
407                         0) [[ ${PN} = emacs-common-gentoo ]] \
408                                 || ewarn "... Huh? No site initialisation files found." ;;
409                         1) einfo "... ${#sflist[@]} site initialisation file included." ;;
410                         *) einfo "... ${#sflist[@]} site initialisation files included." ;;
411                 esac
412         fi
413
414         return 0
415 }