0
|
1 ;;; subr.el --- basic lisp subroutines for XEmacs
|
|
2
|
|
3 ;;; Copyright (C) 1985, 1986, 1992, 1994, 1995 Free Software Foundation, Inc.
|
|
4 ;;; Copyright (C) 1995 Tinker Systems and INS Engineering Corp.
|
|
5 ;;; Copyright (C) 1995 Sun Microsystems.
|
|
6
|
|
7 ;; This file is part of XEmacs.
|
|
8
|
|
9 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
10 ;; under the terms of the GNU General Public License as published by
|
|
11 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
12 ;; any later version.
|
|
13
|
|
14 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
15 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
17 ;; General Public License for more details.
|
|
18
|
|
19 ;; You should have received a copy of the GNU General Public License
|
|
20 ;; along with XEmacs; see the file COPYING. If not, write to the Free
|
4
|
21 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
22 ;; 02111-1307, USA.
|
|
23
|
|
24 ;;; Synched up with: FSF 19.34.
|
0
|
25
|
4
|
26 ;;; Commentary:
|
|
27
|
|
28 ;; There's not a whole lot in common now with the FSF version,
|
|
29 ;; be wary when applying differences. I've left in a number of lines
|
|
30 ;; of commentary just to give diff(1) something to synch itself with to
|
|
31 ;; provide useful context diffs. -sb
|
0
|
32
|
|
33 ;;; Code:
|
|
34
|
|
35
|
|
36 ;;;; Lisp language features.
|
|
37
|
|
38 (defmacro lambda (&rest cdr)
|
|
39 "Return a lambda expression.
|
|
40 A call of the form (lambda ARGS DOCSTRING INTERACTIVE BODY) is
|
|
41 self-quoting; the result of evaluating the lambda expression is the
|
|
42 expression itself. The lambda expression may then be treated as a
|
|
43 function, i.e., stored as the function value of a symbol, passed to
|
|
44 funcall or mapcar, etc.
|
|
45
|
|
46 ARGS should take the same form as an argument list for a `defun'.
|
|
47 DOCSTRING is an optional documentation string.
|
|
48 If present, it should describe how to call the function.
|
|
49 But documentation strings are usually not useful in nameless functions.
|
|
50 INTERACTIVE should be a call to the function `interactive', which see.
|
|
51 It may also be omitted.
|
|
52 BODY should be a list of lisp expressions."
|
|
53 ;; Note that this definition should not use backquotes; subr.el should not
|
|
54 ;; depend on backquote.el.
|
|
55 ;; #### - I don't see why. So long as backquote.el doesn't use anything
|
|
56 ;; from subr.el, there's no problem with using backquotes here. --Stig
|
4
|
57 ;;(list 'function (cons 'lambda cdr)))
|
|
58 `(function (lambda ,@cdr)))
|
|
59
|
|
60 (defmacro defun-when-void (&rest args)
|
|
61 "Define a function, just like `defun', unless it's already defined.
|
|
62 Used for compatibility among different emacs variants."
|
|
63 `(if (fboundp ',(car args))
|
|
64 nil
|
|
65 (defun ,@args)))
|
0
|
66
|
4
|
67 (defmacro define-function-when-void (&rest args)
|
|
68 "Define a function, just like `define-function', unless it's already defined.
|
|
69 Used for compatibility among different emacs variants."
|
|
70 `(if (fboundp ,(car args))
|
|
71 nil
|
|
72 (define-function ,@args)))
|
|
73
|
|
74 ;;;; Keymap support.
|
|
75 ;; XEmacs: removed to keymap.el
|
|
76
|
|
77 ;;;; The global keymap tree.
|
|
78
|
|
79 ;;; global-map, esc-map, and ctl-x-map have their values set up in
|
|
80 ;;; keymap.c; we just give them docstrings here.
|
|
81
|
|
82 ;;;; Event manipulation functions.
|
|
83
|
|
84 ;; The call to `read' is to ensure that the value is computed at load time
|
|
85 ;; and not compiled into the .elc file. The value is negative on most
|
|
86 ;; machines, but not on all!
|
|
87 ;; XEmacs: This stuff is done in C Code.
|
|
88
|
|
89 ;;;; Obsolescent names for functions.
|
|
90 ;; XEmacs: not used.
|
|
91
|
|
92 ;; XEmacs:
|
0
|
93 (define-function 'not 'null)
|
12
|
94 (define-function-when-void 'numberp 'integerp) ; different when floats
|
0
|
95
|
|
96 (defun local-variable-if-set-p (sym buffer)
|
|
97 "Return t if SYM would be local to BUFFER after it is set.
|
|
98 A nil value for BUFFER is *not* the same as (current-buffer), but
|
|
99 can be used to determine whether `make-variable-buffer-local' has been
|
|
100 called on SYM."
|
|
101 (local-variable-p sym buffer t))
|
|
102
|
|
103
|
|
104 ;;;; Hook manipulation functions.
|
|
105
|
4
|
106 ;; (defconst run-hooks 'run-hooks ...)
|
|
107
|
0
|
108 (defun make-local-hook (hook)
|
|
109 "Make the hook HOOK local to the current buffer.
|
|
110 When a hook is local, its local and global values
|
|
111 work in concert: running the hook actually runs all the hook
|
|
112 functions listed in *either* the local value *or* the global value
|
|
113 of the hook variable.
|
|
114
|
|
115 This function works by making `t' a member of the buffer-local value,
|
|
116 which acts as a flag to run the hook functions in the default value as
|
|
117 well. This works for all normal hooks, but does not work for most
|
|
118 non-normal hooks yet. We will be changing the callers of non-normal
|
|
119 hooks so that they can handle localness; this has to be done one by
|
|
120 one.
|
|
121
|
|
122 This function does nothing if HOOK is already local in the current
|
|
123 buffer.
|
|
124
|
|
125 Do not use `make-local-variable' to make a hook variable buffer-local."
|
4
|
126 (if (local-variable-p hook (current-buffer)) ; XEmacs
|
0
|
127 nil
|
|
128 (or (boundp hook) (set hook nil))
|
|
129 (make-local-variable hook)
|
|
130 (set hook (list t))))
|
|
131
|
|
132 (defun add-hook (hook function &optional append local)
|
|
133 "Add to the value of HOOK the function FUNCTION.
|
|
134 FUNCTION is not added if already present.
|
|
135 FUNCTION is added (if necessary) at the beginning of the hook list
|
|
136 unless the optional argument APPEND is non-nil, in which case
|
|
137 FUNCTION is added at the end.
|
|
138
|
|
139 The optional fourth argument, LOCAL, if non-nil, says to modify
|
|
140 the hook's buffer-local value rather than its default value.
|
|
141 This makes no difference if the hook is not buffer-local.
|
|
142 To make a hook variable buffer-local, always use
|
|
143 `make-local-hook', not `make-local-variable'.
|
|
144
|
|
145 HOOK should be a symbol, and FUNCTION may be any valid function. If
|
|
146 HOOK is void, it is first set to nil. If HOOK's value is a single
|
|
147 function, it is changed to a list of functions."
|
|
148 (or (boundp hook) (set hook nil))
|
|
149 (or (default-boundp hook) (set-default hook nil))
|
|
150 ;; If the hook value is a single function, turn it into a list.
|
|
151 (let ((old (symbol-value hook)))
|
|
152 (if (or (not (listp old)) (eq (car old) 'lambda))
|
|
153 (set hook (list old))))
|
|
154 (if (or local
|
|
155 ;; Detect the case where make-local-variable was used on a hook
|
|
156 ;; and do what we used to do.
|
4
|
157 (and (local-variable-if-set-p hook (current-buffer)) ; XEmacs
|
0
|
158 (not (memq t (symbol-value hook)))))
|
|
159 ;; Alter the local value only.
|
|
160 (or (if (consp function)
|
|
161 (member function (symbol-value hook))
|
|
162 (memq function (symbol-value hook)))
|
|
163 (set hook
|
|
164 (if append
|
|
165 (append (symbol-value hook) (list function))
|
|
166 (cons function (symbol-value hook)))))
|
|
167 ;; Alter the global value (which is also the only value,
|
|
168 ;; if the hook doesn't have a local value).
|
|
169 (or (if (consp function)
|
|
170 (member function (default-value hook))
|
|
171 (memq function (default-value hook)))
|
|
172 (set-default hook
|
|
173 (if append
|
|
174 (append (default-value hook) (list function))
|
|
175 (cons function (default-value hook)))))))
|
|
176
|
|
177 (defun remove-hook (hook function &optional local)
|
|
178 "Remove from the value of HOOK the function FUNCTION.
|
|
179 HOOK should be a symbol, and FUNCTION may be any valid function. If
|
|
180 FUNCTION isn't the value of HOOK, or, if FUNCTION doesn't appear in the
|
|
181 list of hooks to run in HOOK, then nothing is done. See `add-hook'.
|
|
182
|
|
183 The optional third argument, LOCAL, if non-nil, says to modify
|
|
184 the hook's buffer-local value rather than its default value.
|
|
185 This makes no difference if the hook is not buffer-local.
|
|
186 To make a hook variable buffer-local, always use
|
|
187 `make-local-hook', not `make-local-variable'."
|
|
188 (if (or (not (boundp hook)) ;unbound symbol, or
|
|
189 (not (default-boundp 'hook))
|
|
190 (null (symbol-value hook)) ;value is nil, or
|
|
191 (null function)) ;function is nil, then
|
|
192 nil ;Do nothing.
|
|
193 (if (or local
|
|
194 ;; Detect the case where make-local-variable was used on a hook
|
|
195 ;; and do what we used to do.
|
|
196 (and (local-variable-p hook (current-buffer))
|
|
197 (not (memq t (symbol-value hook)))))
|
|
198 (let ((hook-value (symbol-value hook)))
|
|
199 (if (consp hook-value)
|
|
200 (if (member function hook-value)
|
|
201 (setq hook-value (delete function (copy-sequence hook-value))))
|
|
202 (if (equal hook-value function)
|
|
203 (setq hook-value nil)))
|
|
204 (set hook hook-value))
|
|
205 (let ((hook-value (default-value hook)))
|
|
206 (if (consp hook-value)
|
|
207 (if (member function hook-value)
|
|
208 (setq hook-value (delete function (copy-sequence hook-value))))
|
|
209 (if (equal hook-value function)
|
|
210 (setq hook-value nil)))
|
|
211 (set-default hook hook-value)))))
|
|
212
|
|
213 (defun add-to-list (list-var element)
|
|
214 "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet.
|
4
|
215 The test for presence of ELEMENT is done with `equal'.
|
0
|
216 If you want to use `add-to-list' on a variable that is not defined
|
|
217 until a certain package is loaded, you should put the call to `add-to-list'
|
|
218 into a hook function that will be run only after loading the package.
|
|
219 `eval-after-load' provides one way to do this. In some cases
|
|
220 other hooks, such as major mode hooks, can do the job."
|
|
221 (or (member element (symbol-value list-var))
|
|
222 (set list-var (cons element (symbol-value list-var)))))
|
|
223
|
4
|
224 ;; XEmacs additions
|
0
|
225 ;; called by Fkill_buffer()
|
|
226 (defvar kill-buffer-hook nil
|
|
227 "Function or functions to be called when a buffer is killed.
|
|
228 The value of this variable may be buffer-local.
|
|
229 The buffer about to be killed is current when this hook is run.")
|
|
230
|
|
231 ;; in C in FSFmacs
|
|
232 (defvar kill-emacs-hook nil
|
|
233 "Function or functions to be called when `kill-emacs' is called,
|
|
234 just before emacs is actually killed.")
|
|
235
|
|
236 ;; not obsolete.
|
|
237 (define-function 'rplaca 'setcar)
|
|
238 (define-function 'rplacd 'setcdr)
|
|
239
|
4
|
240 ;; XEmacs
|
0
|
241 (defun mapvector (__function __seq)
|
|
242 "Apply FUNCTION to each element of SEQ, making a vector of the results.
|
|
243 The result is a vector of the same length as SEQ.
|
|
244 SEQ may be a list, a vector or a string."
|
|
245 (let* ((len (length __seq))
|
|
246 (vec (make-vector len 'nil))
|
|
247 (i 0))
|
|
248 (while (< i len)
|
|
249 (aset vec i (funcall __function (cond ((listp __seq)
|
|
250 (nth i __seq))
|
|
251 (t (aref __seq i)))))
|
|
252 (setq i (+ i 1)))
|
|
253 vec))
|
|
254
|
|
255 ;;;; String functions.
|
|
256
|
4
|
257 ;; XEmacs
|
0
|
258 (defun replace-in-string (str regexp newtext &optional literal)
|
|
259 "Replaces all matches in STR for REGEXP with NEWTEXT string.
|
|
260 Optional LITERAL non-nil means do a literal replacement.
|
|
261 Otherwise treat \\ in NEWTEXT string as special:
|
|
262 \\& means substitute original matched text,
|
|
263 \\N means substitute match for \(...\) number N,
|
|
264 \\\\ means insert one \\."
|
|
265 (if (not (stringp str))
|
|
266 (error "(replace-in-string): First argument must be a string: %s" str))
|
|
267 (if (stringp newtext)
|
|
268 nil
|
|
269 (error "(replace-in-string): 3rd arg must be a string: %s"
|
|
270 newtext))
|
|
271 (let ((rtn-str "")
|
|
272 (start 0)
|
|
273 (special)
|
|
274 match prev-start)
|
|
275 (while (setq match (string-match regexp str start))
|
|
276 (setq prev-start start
|
|
277 start (match-end 0)
|
|
278 rtn-str
|
|
279 (concat
|
|
280 rtn-str
|
|
281 (substring str prev-start match)
|
|
282 (cond (literal newtext)
|
|
283 (t (mapconcat
|
|
284 (function
|
|
285 (lambda (c)
|
|
286 (if special
|
|
287 (progn
|
|
288 (setq special nil)
|
|
289 (cond ((eq c ?\\) "\\")
|
|
290 ((eq c ?&)
|
|
291 (substring str
|
|
292 (match-beginning 0)
|
|
293 (match-end 0)))
|
|
294 ((and (>= c ?0) (<= c ?9))
|
|
295 (if (> c (+ ?0 (length
|
|
296 (match-data))))
|
|
297 ;; Invalid match num
|
|
298 (error "(replace-in-string) Invalid match num: %c" c)
|
|
299 (setq c (- c ?0))
|
|
300 (substring str
|
|
301 (match-beginning c)
|
|
302 (match-end c))))
|
|
303 (t (char-to-string c))))
|
|
304 (if (eq c ?\\) (progn (setq special t) nil)
|
|
305 (char-to-string c)))))
|
|
306 newtext ""))))))
|
|
307 (concat rtn-str (substring str start))))
|
|
308
|
|
309 (defun split-string (string pattern)
|
|
310 "Return a list of substrings of STRING which are separated by PATTERN."
|
|
311 (let (parts (start 0))
|
|
312 (while (string-match pattern string start)
|
|
313 (setq parts (cons (substring string start (match-beginning 0)) parts)
|
|
314 start (match-end 0)))
|
|
315 (nreverse (cons (substring string start) parts))
|
|
316 ))
|
|
317
|
|
318 (defmacro with-output-to-string (&rest forms)
|
|
319 "Collect output to `standard-output' while evaluating FORMS and return
|
|
320 it as a string."
|
|
321 ;; by "William G. Dubuque" <wgd@zurich.ai.mit.edu> w/ mods from Stig
|
|
322 (` (save-excursion
|
|
323 (set-buffer (get-buffer-create " *string-output*"))
|
|
324 (setq buffer-read-only nil)
|
|
325 (buffer-disable-undo (current-buffer))
|
|
326 (erase-buffer)
|
|
327 (let ((standard-output (current-buffer)))
|
|
328 (,@ forms))
|
|
329 (prog1
|
|
330 (buffer-string)
|
|
331 (erase-buffer)))))
|
|
332
|
26
|
333 (defmacro with-temp-buffer (&rest forms)
|
|
334 "Create a temporary buffer, and evaluate FORMS there like `progn'."
|
|
335 (let ((temp-buffer (make-symbol "temp-buffer")))
|
|
336 `(let ((,temp-buffer
|
|
337 (get-buffer-create (generate-new-buffer-name " *temp*"))))
|
|
338 (unwind-protect
|
|
339 (save-excursion
|
|
340 (set-buffer ,temp-buffer)
|
|
341 ,@forms)
|
|
342 (and (buffer-name ,temp-buffer)
|
|
343 (kill-buffer ,temp-buffer))))))
|
|
344
|
|
345 ;; Moved from 20.1:lisp/mule/mule-coding.el.
|
|
346 (defmacro with-string-as-buffer-contents (str &rest body)
|
|
347 "With the contents of the current buffer being STR, run BODY.
|
|
348 Returns the new contents of the buffer, as modified by BODY.
|
|
349 The original current buffer is restored afterwards."
|
|
350 `(let ((curbuf (current-buffer))
|
|
351 (tempbuf (get-buffer-create " *string-as-buffer-contents*")))
|
|
352 (unwind-protect
|
|
353 (progn
|
|
354 (set-buffer tempbuf)
|
|
355 (buffer-disable-undo (current-buffer))
|
|
356 (erase-buffer)
|
|
357 (insert ,str)
|
|
358 ,@body
|
|
359 (buffer-string))
|
|
360 (erase-buffer tempbuf)
|
|
361 (set-buffer curbuf))))
|
|
362
|
0
|
363 (defun insert-face (string face)
|
|
364 "Insert STRING and highlight with FACE. Returns the extent created."
|
|
365 (let ((p (point)) ext)
|
|
366 (insert string)
|
|
367 (setq ext (make-extent p (point)))
|
|
368 (set-extent-face ext face)
|
|
369 ext))
|
|
370
|
|
371 ;; not obsolete.
|
|
372 (define-function 'string= 'string-equal)
|
|
373 (define-function 'string< 'string-lessp)
|
|
374 (define-function 'int-to-string 'number-to-string)
|
|
375 (define-function 'string-to-int 'string-to-number)
|
|
376
|
|
377 ;; alist/plist functions
|
|
378 (defun plist-to-alist (plist)
|
|
379 "Convert property list PLIST into the equivalent association-list form.
|
|
380 The alist is returned. This converts from
|
|
381
|
|
382 \(a 1 b 2 c 3)
|
|
383
|
|
384 into
|
|
385
|
|
386 \((a . 1) (b . 2) (c . 3))
|
|
387
|
|
388 The original plist is not modified. See also `destructive-plist-to-alist'."
|
|
389 (let (alist)
|
|
390 (while plist
|
|
391 (setq alist (cons (cons (car plist) (cadr plist)) alist))
|
|
392 (setq plist (cddr plist)))
|
|
393 (nreverse alist)))
|
|
394
|
|
395 (defun destructive-plist-to-alist (plist)
|
|
396 "Convert property list PLIST into the equivalent association-list form.
|
|
397 The alist is returned. This converts from
|
|
398
|
|
399 \(a 1 b 2 c 3)
|
|
400
|
|
401 into
|
|
402
|
|
403 \((a . 1) (b . 2) (c . 3))
|
|
404
|
|
405 The original plist is destroyed in the process of constructing the alist.
|
|
406 See also `plist-to-alist'."
|
|
407 (let ((head plist)
|
|
408 next)
|
|
409 (while plist
|
|
410 ;; remember the next plist pair.
|
|
411 (setq next (cddr plist))
|
|
412 ;; make the cons holding the property value into the alist element.
|
|
413 (setcdr (cdr plist) (cadr plist))
|
|
414 (setcar (cdr plist) (car plist))
|
|
415 ;; reattach into alist form.
|
|
416 (setcar plist (cdr plist))
|
|
417 (setcdr plist next)
|
|
418 (setq plist next))
|
|
419 head))
|
|
420
|
|
421 (defun alist-to-plist (alist)
|
|
422 "Convert association list ALIST into the equivalent property-list form.
|
|
423 The plist is returned. This converts from
|
|
424
|
|
425 \((a . 1) (b . 2) (c . 3))
|
|
426
|
|
427 into
|
|
428
|
|
429 \(a 1 b 2 c 3)
|
|
430
|
|
431 The original alist is not modified. See also `destructive-alist-to-plist'."
|
|
432 (let (plist)
|
|
433 (while alist
|
|
434 (let ((el (car alist)))
|
|
435 (setq plist (cons (cdr el) (cons (car el) plist))))
|
|
436 (setq alist (cdr alist)))
|
|
437 (nreverse plist)))
|
|
438
|
|
439 ;; getf, remf in cl*.el.
|
|
440
|
|
441 (defmacro putf (plist prop val)
|
|
442 "Add property PROP to plist PLIST with value VAL.
|
|
443 Analogous to (setq PLIST (plist-put PLIST PROP VAL))."
|
|
444 `(setq ,plist (plist-put ,plist ,prop ,val)))
|
|
445
|
|
446 (defmacro laxputf (lax-plist prop val)
|
|
447 "Add property PROP to lax plist LAX-PLIST with value VAL.
|
|
448 Analogous to (setq LAX-PLIST (lax-plist-put LAX-PLIST PROP VAL))."
|
|
449 `(setq ,lax-plist (lax-plist-put ,lax-plist ,prop ,val)))
|
|
450
|
|
451 (defmacro laxremf (lax-plist prop)
|
|
452 "Remove property PROP from lax plist LAX-PLIST.
|
|
453 Analogous to (setq LAX-PLIST (lax-plist-remprop LAX-PLIST PROP))."
|
|
454 `(setq ,lax-plist (lax-plist-remprop ,lax-plist ,prop)))
|
|
455
|
|
456 ;;; Error functions
|
|
457
|
|
458 (defun error (&rest args)
|
|
459 "Signal an error, making error message by passing all args to `format'.
|
|
460 This error is not continuable: you cannot continue execution after the
|
|
461 error using the debugger `r' command. See also `cerror'."
|
|
462 (while t
|
|
463 (apply 'cerror args)))
|
|
464
|
|
465 (defun cerror (&rest args)
|
|
466 "Like `error' but signals a continuable error."
|
|
467 (signal 'error (list (apply 'format args))))
|
|
468
|
|
469 (defmacro check-argument-type (predicate argument)
|
|
470 "Check that ARGUMENT satisfies PREDICATE.
|
|
471 If not, signal a continuable `wrong-type-argument' error until the
|
30
|
472 returned value satisfies PREDICATE, and assign the returned value
|
0
|
473 to ARGUMENT."
|
|
474 `(if (not (,(eval predicate) ,argument))
|
|
475 (setq ,argument
|
|
476 (wrong-type-argument ,predicate ,argument))))
|
|
477
|
|
478 (defun signal-error (error-symbol data)
|
|
479 "Signal a non-continuable error. Args are ERROR-SYMBOL, and associated DATA.
|
|
480 An error symbol is a symbol defined using `define-error'.
|
|
481 DATA should be a list. Its elements are printed as part of the error message.
|
|
482 If the signal is handled, DATA is made available to the handler.
|
|
483 See also `signal', and the functions to handle errors: `condition-case'
|
|
484 and `call-with-condition-handler'."
|
|
485 (while t
|
|
486 (signal error-symbol data)))
|
|
487
|
|
488 (defun define-error (error-sym doc-string &optional inherits-from)
|
|
489 "Define a new error, denoted by ERROR-SYM.
|
|
490 DOC-STRING is an informative message explaining the error, and will be
|
|
491 printed out when an unhandled error occurs.
|
|
492 ERROR-SYM is a sub-error of INHERITS-FROM (which defaults to `error').
|
|
493
|
|
494 \[`define-error' internally works by putting on ERROR-SYM an `error-message'
|
|
495 property whose value is DOC-STRING, and an `error-conditions' property
|
|
496 that is a list of ERROR-SYM followed by each of its super-errors, up
|
|
497 to and including `error'. You will sometimes see code that sets this up
|
|
498 directly rather than calling `define-error', but you should *not* do this
|
|
499 yourself.]"
|
|
500 (check-argument-type 'symbolp error-sym)
|
|
501 (check-argument-type 'stringp doc-string)
|
|
502 (put error-sym 'error-message doc-string)
|
|
503 (or inherits-from (setq inherits-from 'error))
|
|
504 (let ((conds (get inherits-from 'error-conditions)))
|
|
505 (or conds (signal-error 'error (list "Not an error symbol" error-sym)))
|
|
506 (put error-sym 'error-conditions (cons error-sym conds))))
|
|
507
|
|
508 ;;;; Miscellanea.
|
|
509
|
|
510 (defun buffer-substring-no-properties (beg end)
|
|
511 "Return the text from BEG to END, without text properties, as a string."
|
|
512 (let ((string (buffer-substring beg end)))
|
|
513 (set-text-properties 0 (length string) nil string)
|
|
514 string))
|
|
515
|
|
516 (defun ignore (&rest ignore)
|
|
517 "Do nothing and return nil.
|
|
518 This function accepts any number of arguments, but ignores them."
|
|
519 (interactive)
|
|
520 nil)
|
|
521
|
|
522 (defmacro save-current-buffer (&rest forms)
|
|
523 "Restore the current buffer setting after executing FORMS.
|
|
524 Does not restore the values of point and mark.
|
|
525 See also: `save-excursion'."
|
|
526 ;; by Stig@hackvan.com
|
|
527 (` (let ((_cur_buf_ (current-buffer)))
|
|
528 (unwind-protect
|
|
529 (progn (,@ forms))
|
|
530 (set-buffer _cur_buf_)))))
|
|
531
|
|
532 (defmacro eval-in-buffer (buffer &rest forms)
|
|
533 "Evaluate FORMS in BUFFER.
|
|
534 See also: `save-current-buffer' and `save-excursion'."
|
|
535 ;; by Stig@hackvan.com
|
|
536 (` (save-current-buffer
|
|
537 (set-buffer (, buffer))
|
|
538 (,@ forms))))
|
|
539
|
|
540 ;;; The real defn is in abbrev.el but some early callers
|
|
541 ;;; (eg lisp-mode-abbrev-table) want this before abbrev.el is loaded...
|
|
542
|
|
543 (if (not (fboundp 'define-abbrev-table))
|
|
544 (progn
|
|
545 (setq abbrev-table-name-list '())
|
|
546 (fset 'define-abbrev-table (function (lambda (name defs)
|
|
547 ;; These are fixed-up when abbrev.el loads.
|
|
548 (setq abbrev-table-name-list
|
|
549 (cons (cons name defs)
|
|
550 abbrev-table-name-list)))))))
|
|
551
|
|
552 (defun functionp (obj)
|
|
553 "Returns t if OBJ is a function, nil otherwise."
|
|
554 (cond
|
|
555 ((symbolp obj) (fboundp obj))
|
|
556 ((subrp obj))
|
|
557 ((compiled-function-p obj))
|
|
558 ((consp obj)
|
|
559 (if (eq (car obj) 'lambda) (listp (car (cdr obj)))))
|
|
560 (t nil)))
|
|
561
|
|
562 ;; This was not present before. I think Jamie had some objections
|
|
563 ;; to this, so I'm leaving this undefined for now. --ben
|
|
564
|
|
565 ;;; The objection is this: there is more than one way to load the same file.
|
30
|
566 ;;; "foo", "foo.elc", "foo.el", and "/some/path/foo.elc" are all different
|
0
|
567 ;;; ways to load the exact same code. `eval-after-load' is too stupid to
|
|
568 ;;; deal with this sort of thing. If this sort of feature is desired, then
|
|
569 ;;; it should work off of a hook on `provide'. Features are unique and
|
|
570 ;;; the arguments to (load) are not. --Stig
|
|
571
|
|
572 ;;;; Specifying things to do after certain files are loaded.
|
|
573
|
26
|
574 (defun eval-after-load (file form)
|
|
575 "Arrange that, if FILE is ever loaded, FORM will be run at that time.
|
|
576 This makes or adds to an entry on `after-load-alist'.
|
|
577 If FILE is already loaded, evaluate FORM right now.
|
|
578 It does nothing if FORM is already on the list for FILE.
|
|
579 FILE should be the name of a library, with no directory name."
|
|
580 ;; Make sure there is an element for FILE.
|
|
581 (or (assoc file after-load-alist)
|
|
582 (setq after-load-alist (cons (list file) after-load-alist)))
|
|
583 ;; Add FORM to the element if it isn't there.
|
|
584 (let ((elt (assoc file after-load-alist)))
|
|
585 (or (member form (cdr elt))
|
|
586 (progn
|
|
587 (nconc elt (list form))
|
|
588 ;; If the file has been loaded already, run FORM right away.
|
|
589 (and (assoc file load-history)
|
|
590 (eval form)))))
|
|
591 form)
|
|
592
|
|
593 (defun eval-next-after-load (file)
|
|
594 "Read the following input sexp, and run it whenever FILE is loaded.
|
|
595 This makes or adds to an entry on `after-load-alist'.
|
|
596 FILE should be the name of a library, with no directory name."
|
|
597 (eval-after-load file (read)))
|
0
|
598
|
|
599 ; alternate names (not obsolete)
|
|
600 (if (not (fboundp 'mod)) (define-function 'mod '%))
|
|
601 (define-function 'move-marker 'set-marker)
|
2
|
602 (define-function 'beep 'ding) ; preserve lingual purity
|
0
|
603 (define-function 'indent-to-column 'indent-to)
|
|
604 (define-function 'backward-delete-char 'delete-backward-char)
|
|
605 (define-function 'search-forward-regexp (symbol-function 're-search-forward))
|
|
606 (define-function 'search-backward-regexp (symbol-function 're-search-backward))
|
|
607 (define-function 'remove-directory 'delete-directory)
|
|
608 (define-function 'set-match-data 'store-match-data)
|
|
609 (define-function 'send-string-to-terminal 'external-debugging-output)
|
|
610 (define-function 'buffer-string 'buffer-substring)
|
4
|
611
|
|
612 ;;; subr.el ends here
|