elisp-common.eclass: avoid //path, is not /path on Cygwin
[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
4 # @ECLASS: elisp-common.eclass
5 # @MAINTAINER:
6 # Gentoo GNU Emacs project <gnu-emacs@gentoo.org>
7 # @AUTHOR:
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
14 # @DESCRIPTION:
15 #
16 # Usually you want to use this eclass for (optional) GNU Emacs support
17 # of your package.  This is NOT for XEmacs!
18 #
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.
22 #
23 # When relying on the emacs USE flag, you need to add
24 #
25 # @CODE
26 #       emacs? ( virtual/emacs )
27 # @CODE
28 #
29 # to your DEPEND/RDEPEND line and use the functions provided here to
30 # bring the files to the correct locations.
31 #
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:
38 #
39 # @CODE
40 #       elisp-need-emacs 24 || die "Emacs version too low"
41 # @CODE
42 #
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.
46 #
47 # @ROFF .SS
48 # src_compile() usage:
49 #
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
54 # are loadable.
55 #
56 # @CODE
57 #       elisp-compile *.el
58 # @CODE
59 #
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.
67 #
68 # @ROFF .SS
69 # src_install() usage:
70 #
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}.
77 #
78 # @CODE
79 #       elisp-install ${PN} *.el *.elc
80 # @CODE
81 #
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:
86 #
87 # @CODE
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)
91 # @CODE
92 #
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.
98 #
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.
108 #
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.
116 #
117 # Best practice is to define a SITEFILE variable in the global scope of
118 # your ebuild (e.g., right after S or RDEPEND):
119 #
120 # @CODE
121 #       SITEFILE="50${PN}-gentoo.el"
122 # @CODE
123 #
124 # Which is then installed by
125 #
126 # @CODE
127 #       elisp-site-file-install "${FILESDIR}/${SITEFILE}"
128 # @CODE
129 #
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.
135 #
136 # @ROFF .SS
137 # pkg_postinst() / pkg_postrm() usage:
138 #
139 # After that you need to recreate the start-up file of Emacs after
140 # emerging and unmerging by using
141 #
142 # @CODE
143 #       pkg_postinst() {
144 #               elisp-site-regen
145 #       }
146 #
147 #       pkg_postrm() {
148 #               elisp-site-regen
149 #       }
150 # @CODE
151 #
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.
158
159 # @ECLASS-VARIABLE: SITELISP
160 # @DESCRIPTION:
161 # Directory where packages install Emacs Lisp files.
162 SITELISP=/usr/share/emacs/site-lisp
163
164 # @ECLASS-VARIABLE: SITEETC
165 # @DESCRIPTION:
166 # Directory where packages install miscellaneous (not Lisp) files.
167 SITEETC=/usr/share/emacs/etc
168
169 # @ECLASS-VARIABLE: EMACS
170 # @DESCRIPTION:
171 # Path of Emacs executable.
172 EMACS=${EPREFIX}/usr/bin/emacs
173
174 # @ECLASS-VARIABLE: EMACSFLAGS
175 # @DESCRIPTION:
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"
179
180 # @ECLASS-VARIABLE: BYTECOMPFLAGS
181 # @DESCRIPTION:
182 # Emacs flags used for byte-compilation in elisp-compile().
183 BYTECOMPFLAGS="-L ."
184
185 # @FUNCTION: elisp-emacs-version
186 # @RETURN: exit status of Emacs
187 # @DESCRIPTION:
188 # Output version of currently active Emacs.
189
190 elisp-emacs-version() {
191         local version ret
192         # The following will work for at least versions 18-24.
193         echo "(princ emacs-version)" >"${T}"/emacs-version.el
194         version=$(
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.
199                 unset TERM DISPLAY
200                 ${EMACS} ${EMACSFLAGS} -l "${T}"/emacs-version.el </dev/null
201         )
202         ret=$?
203         rm -f "${T}"/emacs-version.el
204         if [[ ${ret} -ne 0 ]]; then
205                 eerror "elisp-emacs-version: Failed to run ${EMACS}"
206                 return ${ret}
207         fi
208         if [[ -z ${version} ]]; then
209                 eerror "elisp-emacs-version: Could not determine Emacs version"
210                 return 1
211         fi
212         echo "${version}"
213 }
214
215 # @FUNCTION: elisp-need-emacs
216 # @USAGE: <version>
217 # @RETURN: 0 if true, 1 if false, 2 if trouble
218 # @DESCRIPTION:
219 # Test if the eselected Emacs version is at least the major version
220 # of GNU Emacs specified as argument.
221
222 elisp-need-emacs() {
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."
228                 return 1
229         fi
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."
233                 return 1
234         fi
235         return 0
236 }
237
238 # @FUNCTION: elisp-compile
239 # @USAGE: <list of elisp files>
240 # @DESCRIPTION:
241 # Byte-compile Emacs Lisp files.
242 #
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.
246 #
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.
250
251 elisp-compile() {
252         ebegin "Compiling GNU Emacs Elisp files"
253         ${EMACS} ${EMACSFLAGS} ${BYTECOMPFLAGS} -f batch-byte-compile "$@"
254         eend $? "elisp-compile: batch-byte-compile failed" || die
255 }
256
257 # @FUNCTION: elisp-make-autoload-file
258 # @USAGE: [output file] [list of directories]
259 # @DESCRIPTION:
260 # Generate a file with autoload definitions for the lisp functions.
261
262 elisp-make-autoload-file() {
263         local f="${1:-${PN}-autoloads.el}" null="" page=$'\f'
264         shift
265         ebegin "Generating autoload file for GNU Emacs"
266
267         cat >"${f}" <<-EOF
268         ;;; ${f##*/} --- autoloads for ${PN}
269
270         ;;; Commentary:
271         ;; Automatically generated by elisp-common.eclass
272         ;; DO NOT EDIT THIS FILE
273
274         ;;; Code:
275         ${page}
276         ;; Local ${null}Variables:
277         ;; version-control: never
278         ;; no-byte-compile: t
279         ;; no-update-autoloads: t
280         ;; End:
281
282         ;;; ${f##*/} ends here
283         EOF
284
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 "${@-.}"
289
290         eend $? "elisp-make-autoload-file: batch-update-autoloads failed" || die
291 }
292
293 # @FUNCTION: elisp-install
294 # @USAGE: <subdirectory> <list of files>
295 # @DESCRIPTION:
296 # Install files in SITELISP directory.
297
298 elisp-install() {
299         local subdir="$1"
300         shift
301         ebegin "Installing Elisp files for GNU Emacs support"
302         ( # subshell to avoid pollution of calling environment
303                 insinto "${SITELISP}/${subdir}"
304                 doins "$@"
305         )
306         eend $? "elisp-install: doins failed" || die
307 }
308
309 # @FUNCTION: elisp-site-file-install
310 # @USAGE: <site-init file> [subdirectory]
311 # @DESCRIPTION:
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,
316 # respectively.
317
318 elisp-site-file-install() {
319         local sf="${1##*/}" my_pn="${2:-${PN}}" ret
320         local header=";;; ${PN} site-lisp configuration"
321
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"
325         sf="${T}/${sf}"
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"
333                 doins "${sf}"
334         )
335         ret=$?
336         rm -f "${sf}"
337         eend ${ret} "elisp-site-file-install: doins failed" || die
338 }
339
340 # @FUNCTION: elisp-site-regen
341 # @DESCRIPTION:
342 # Regenerate the site-gentoo.el file, based on packages' site
343 # initialisation files in the /usr/share/emacs/site-lisp/site-gentoo.d/
344 # directory.
345
346 elisp-site-regen() {
347         local sitelisp=${ROOT%/}${EPREFIX}${SITELISP}
348         local sf i ret=0 null="" page=$'\f'
349         local -a sflist
350
351         if [[ ${EBUILD_PHASE} = *rm && ! -e ${sitelisp}/site-gentoo.el ]]; then
352                 ewarn "Refusing to create site-gentoo.el in ${EBUILD_PHASE} phase."
353                 return 0
354         fi
355
356         [[ -d ${sitelisp} ]] \
357                 || die "elisp-site-regen: Directory ${sitelisp} does not exist"
358
359         [[ -d ${T} ]] \
360                 || die "elisp-site-regen: Temporary directory ${T} does not exist"
361
362         ebegin "Regenerating site-gentoo.el for GNU Emacs (${EBUILD_PHASE})"
363
364         for sf in "${sitelisp}"/site-gentoo.d/[0-9][0-9]*.el; do
365                 [[ -r ${sf} ]] && sflist+=("${sf}")
366         done
367
368         cat <<-EOF >"${T}"/site-gentoo.el || ret=$?
369         ;;; site-gentoo.el --- site initialisation for Gentoo-installed packages
370
371         ;;; Commentary:
372         ;; Automatically generated by elisp-common.eclass
373         ;; DO NOT EDIT THIS FILE
374
375         ;;; Code:
376         EOF
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=$?
380
381         ${page}
382         (provide 'site-gentoo)
383
384         ;; Local ${null}Variables:
385         ;; no-byte-compile: t
386         ;; buffer-read-only: t
387         ;; End:
388
389         ;;; site-gentoo.el ends here
390         EOF
391
392         if [[ ${ret} -ne 0 ]]; then
393                 eend ${ret} "elisp-site-regen: Writing site-gentoo.el failed."
394                 die
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
400                 eend
401                 einfo "... no changes."
402         else
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." ;;
410                 esac
411         fi
412
413         return 0
414 }