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