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