summaryrefslogtreecommitdiff
path: root/eclass/xemacs-elisp-common.eclass
blob: 5f8fec40d4c96d7801d0f5cae07e639d477806fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# Copyright 1999-2012 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
#
# Copyright 2007-2011 Hans de Graaff <graaff@gentoo.org>
#
# Based on elisp-common.eclass:
# Copyright 2007 Christian Faulhammer <opfer@gentoo.org>
# Copyright 2002-2004 Matthew Kennedy <mkennedy@gentoo.org>
# Copyright 2004-2005 Mamoru Komachi <usata@gentoo.org>
# Copyright 2003 Jeremy Maitin-Shepard <jbms@attbi.com>
# Copyright 2007 Ulrich Müller <ulm@gentoo.org>
#
# @ECLASS: xemacs-elisp-common.eclass
# @MAINTAINER:
# xemacs@gentoo.org
# @BLURB: XEmacs-related installation utilities
# @DESCRIPTION:
#
# Usually you want to use this eclass for (optional) XEmacs support of
# your package.  This is NOT for GNU Emacs!
#
# Many of the steps here are sometimes done by the build system of your
# package (especially compilation), so this is mainly for standalone elisp
# files you gathered from somewhere else.
#
# When relying on the xemacs USE flag, you need to add
#
#       xemacs? ( app-editors/xemacs )
#
# to your DEPEND/RDEPEND line and use the functions provided here to bring
# the files to the correct locations.
#
# @ROFF .SS
# src_compile() usage:
#
# An elisp file is compiled by the xemacs-elisp-compile() function
# defined here and simply takes the source files as arguments.
#
#   xemacs-elisp-compile *.el
#
# In the case of interdependent elisp files, you can use the
# xemacs-elisp-comp() function which makes sure all files are
# loadable.
#
#   xemacs-elisp-comp *.el
#
# Function xemacs-elisp-make-autoload-file() can be used to generate a
# file with autoload definitions for the lisp functions.  It takes a
# list of directories (default: working directory) as its argument.
# Use of this function requires that the elisp source files contain
# magic ";;;###autoload" comments. See the XEmacs Lisp Reference Manual
# (node "Autoload") for a detailed explanation.
#
# @ROFF .SS
# src_install() usage:
#
# The resulting compiled files (.elc) should be put in a subdirectory
# of /usr/lib/xemacs/site-lisp/ which is named after the first
# argument of xemacs-elisp-install().  The following parameters are
# the files to be put in that directory.  Usually the subdirectory
# should be ${PN}, but you can choose something else.
#
#   xemacs-elisp-install ${PN} *.el *.elc
#
# To let the XEmacs support be activated by XEmacs on startup, you need
# to provide a site file (shipped in ${FILESDIR}) which contains the
# startup code (have a look in the documentation of your software).
# Normally this would look like this:
#
#   	(add-to-list 'load-path "@SITELISP@")
#   	(add-to-list 'auto-mode-alist '("\\.csv\\'" . csv-mode))
#   	(autoload 'csv-mode "csv-mode" "Major mode for csv files." t)
#
# If your XEmacs support files are installed in a subdirectory of
# /usr/share/xemacs/site-packages/ (which is strongly recommended), you need
# to extend XEmacs' load-path as shown in the first non-comment line.
# The xemacs-elisp-site-file-install() function of this eclass will replace
# "@SITELISP@" by the actual path.
#
# The next line tells XEmacs to load the mode opening a file ending
# with ".csv" and load functions depending on the context and needed
# features.  Be careful though.  Commands as "load-library" or "require"
# bloat the editor as they are loaded on every startup.  When having
# many XEmacs support files, users may be annoyed by the start-up time.
# Also avoid keybindings as they might interfere with the user's
# settings.  Give a hint in pkg_postinst(), which should be enough.
#
# The naming scheme for this site-init file matches the shell pattern
# "[1-8][0-9]*-gentoo*.el", where the two digits at the beginning define
# the loading order (numbers below 10 or above 89 are reserved for
# internal use).  So if your initialisation depends on another XEmacs
# package, your site file's number must be higher!  If there are no such
# interdependencies then the number should be 50.  Otherwise, numbers
# divisible by 10 are preferred.
#
# Best practice is to define a SITEFILE variable in the global scope of
# your ebuild (e.g., right after S or RDEPEND):
#
#   	SITEFILE="50${PN}-gentoo.el"
#
# Which is then installed by
#
#   	xemacs-elisp-site-file-install "${FILESDIR}/${SITEFILE}" || die
#
# in src_install().  Any characters after the "-gentoo" part and before
# the extension will be stripped from the destination file's name.
# For example, a file "50${PN}-gentoo-${PV}.el" will be installed as
# "50${PN}-gentoo.el".  If your subdirectory is not named ${PN}, give
# the differing name as second argument.

# @ECLASS-VARIABLE: XEMACS_SITELISP
# @DESCRIPTION:
# Directory where packages install indivivual XEmacs Lisp files.
XEMACS_SITELISP=/usr/share/xemacs/site-lisp

# @ECLASS-VARIABLE: XEMACS_SITEPACKAGE
# @DESCRIPTION:
# Directory where packages install XEmacs Lisp packages.
XEMACS_SITEPACKAGE=/usr/share/xemacs/site-packages

# @ECLASS-VARIABLE: XEMACS
# @DESCRIPTION:
# Path of XEmacs executable.
XEMACS=/usr/bin/xemacs

# @ECLASS-VARIABLE: XEMACS_BATCH_CLEAN
# @DESCRIPTION:
# Invocation of XEMACS in batch mode.
XEMACS_BATCH_CLEAN="${XEMACS} --batch --no-site-file --no-init-file"

# @FUNCTION: xemacs-elisp-compile
# @USAGE: <list of elisp files>
# @DESCRIPTION:
# Byte-compile elisp files with xemacs. This function will die when
# there is a problem compiling the lisp files.
xemacs-elisp-compile () {
	{
		${XEMACS_BATCH_CLEAN} -f batch-byte-compile "$@"
		xemacs-elisp-make-autoload-file "$@"
	} || die "Compile lisp files failed"
}

xemacs-elisp-make-autoload-file () {
	${XEMACS_BATCH_CLEAN} \
		-eval "(setq autoload-package-name \"${PN}\")" \
		-eval "(setq generated-autoload-file \"${S}/auto-autoloads.el\")" \
		-l autoload -f batch-update-autoloads "$@"
}

# @FUNCTION: xemacs-elisp-install
# @USAGE: <subdirectory> <list of files>
# @DESCRIPTION:
# Install elisp source and byte-compiled files. All files are installed
# in site-packages in their own directory, indicated by the first
# argument to the function. This function will die if there is a problem
# installing the list files.

xemacs-elisp-install () {
	local subdir="$1"
	shift
	(  # use sub-shell to avoid possible environment polution
		dodir "${XEMACS_SITEPACKAGE}"/lisp/"${subdir}"
		insinto "${XEMACS_SITEPACKAGE}"/lisp/"${subdir}"
		doins "$@"
	) || die "Installing lisp files failed"
}

# @FUNCTION: xemacs-elisp-comp
# @USAGE: <list of elisp files>
# @DESCRIPTION:
# Byte-compile interdependent XEmacs lisp files.
# Originally taken from GNU autotools, but some configuration options
# removed as they don't make sense with the current status of XEmacs
# in Gentoo.

xemacs-elisp-comp() {
	# Copyright 1995 Free Software Foundation, Inc.
	# François Pinard <pinard@iro.umontreal.ca>, 1995.
	# This script byte-compiles all `.el' files which are part of its
	# arguments, using XEmacs, and put the resulting `.elc' files into
	# the current directory, so disregarding the original directories used
	# in `.el' arguments.
	#
	# This script manages in such a way that all XEmacs LISP files to
	# be compiled are made visible between themselves, in the event
	# they require or load-library one another.

	test $# -gt 0 || return 1

	einfo "Compiling XEmacs Elisp files ..."

	tempdir=elc.$$
	mkdir ${tempdir}
	cp "$@" ${tempdir}
	pushd ${tempdir}

	echo "(add-to-list 'load-path \"../\")" > script
	${XEMACS_BATCH_CLEAN} -l script -f batch-byte-compile *.el
	local ret=$?
	mv *.elc ..

	popd
	rm -fr ${tempdir}
	return ${ret}
}

# @FUNCTION: xemacs-elisp-site-file-install
# @USAGE: <site-init file> [subdirectory]
# @DESCRIPTION:
# Install XEmacs site-init file in XEMACS_SITELISP directory.
# Automatically inserts a standard comment header with the name of the
# package (unless it is already present).  Token @SITELISP@ is replaced
# by the path to the package's subdirectory in XEMACS_SITELISP.

xemacs-elisp-site-file-install() {
	local sf="${1##*/}" my_pn="${2:-${PN}}" ret
	local header=";;; ${PN} site-lisp configuration"

	[[ ${sf} == [0-9][0-9]*-gentoo*.el ]] \
		|| ewarn "xemacs-elisp-site-file-install: bad name of site-init file"
	sf="${T}/${sf/%-gentoo*.el/-gentoo.el}"
	ebegin "Installing site initialisation file for XEmacs"
	[[ $1 = "${sf}" ]] || cp "$1" "${sf}"
	sed -i -e "1{:x;/^\$/{n;bx;};/^;.*${PN}/I!s:^:${header}\n\n:;1s:^:\n:;}" \
		-e "s:@SITELISP@:${EPREFIX}${XEMACS_SITELISP}/${my_pn}:g" "${sf}"
	( # subshell to avoid pollution of calling environment
		insinto "${XEMACS_SITELISP}/site-gentoo.d"
		doins "${sf}"
	)
	ret=$?
	rm -f "${sf}"
	eend ${ret} "xemacs-elisp-site-file-install: doins failed"
}

# @FUNCTION: xemacs-elisp-site-regen
# @DESCRIPTION:
# Regenerate the site-gentoo.el file, based on packages' site
# initialisation files in the /usr/share/xemacs/site-lisp/site-gentoo.d/
# directory.

xemacs-elisp-site-regen() {
	local sitelisp=${ROOT}${EPREFIX}${XEMACS_SITELISP}
	local sf i line null="" page=$'\f'
	local -a sflist

	if [ ! -d "${sitelisp}" ]; then
		eerror "xemacs-elisp-site-regen: Directory ${sitelisp} does not exist"
		return 1
	fi

	if [ ! -d "${T}" ]; then
		eerror "xemacs-elisp-site-regen: Temporary directory ${T} does not exist"
		return 1
	fi

	einfon "Regenerating site-gentoo.el for XEmacs (${EBUILD_PHASE}) ..."

	for sf in "${sitelisp}"/site-gentoo.d/[0-9][0-9]*.el
	do
		[ -r "${sf}" ] || continue
		# sort files by their basename. straight insertion sort.
		for ((i=${#sflist[@]}; i>0; i--)); do
			[[ ${sf##*/} < ${sflist[i-1]##*/} ]] || break
			sflist[i]=${sflist[i-1]}
		done
		sflist[i]=${sf}
	done

	cat <<-EOF >"${T}"/site-gentoo.el
	;;; site-gentoo.el --- site initialisation for Gentoo-installed packages

	;;; Commentary:
	;; Automatically generated by xemacs-elisp-common.eclass
	;; DO NOT EDIT THIS FILE

	;;; Code:
	EOF
	# Use sed instead of cat here, since files may miss a trailing newline.
	sed '$q' "${sflist[@]}" </dev/null >>"${T}"/site-gentoo.el
	cat <<-EOF >>"${T}"/site-gentoo.el

	${page}
	(provide 'site-gentoo)

	;; Local ${null}Variables:
	;; no-byte-compile: t
	;; buffer-read-only: t
	;; End:

	;;; site-gentoo.el ends here
	EOF

	if cmp -s "${sitelisp}"/site-gentoo.el "${T}"/site-gentoo.el; then
		# This prevents outputting unnecessary text when there
		# was actually no change.
		# A case is a remerge where we have doubled output.
		rm -f "${T}"/site-gentoo.el
		echo " no changes."
	else
		mv "${T}"/site-gentoo.el "${sitelisp}"/site-gentoo.el
		echo
		case ${#sflist[@]} in
			0) ewarn "... Huh? No site initialisation files found." ;;
			1) einfo "... ${#sflist[@]} site initialisation file included." ;;
			*) einfo "... ${#sflist[@]} site initialisation files included." ;;
		esac
	fi

	return 0
}