1 ;;; subr.el --- basic lisp subroutines for Emacs
3 ;;; Copyright (C) 1985, 1986, 1992 Free Software Foundation, Inc.
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to
19 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24 ;;;; Lisp language features.
26 (defmacro lambda
(&rest cdr
)
27 "Return a lambda expression.
28 A call of the form (lambda ARGS DOCSTRING INTERACTIVE BODY) is
29 self-quoting; the result of evaluating the lambda expression is the
30 expression itself. The lambda expression may then be treated as a
31 function, i. e. stored as the function value of a symbol, passed to
32 funcall or mapcar, etcetera.
33 ARGS should take the same form as an argument list for a `defun'.
34 DOCSTRING should be a string, as described for `defun'. It may be omitted.
35 INTERACTIVE should be a call to the function `interactive', which see.
36 It may also be omitted.
37 BODY should be a list of lisp expressions."
38 ;; Note that this definition should not use backquotes; subr.el should not
39 ;; depend on backquote.el.
40 (list 'function
(cons 'lambda cdr
)))
42 ;;(defmacro defun-inline (name args &rest body)
43 ;; "Create an \"inline defun\" (actually a macro).
44 ;;Use just like `defun'."
45 ;; (nconc (list 'defmacro name '(&rest args))
46 ;; (if (stringp (car body))
47 ;; (prog1 (list (car body))
48 ;; (setq body (or (cdr body) body))))
49 ;; (list (list 'cons (list 'quote
50 ;; (cons 'lambda (cons args body)))
54 ;;;; Window tree functions.
56 (defun one-window-p (&optional nomini
)
57 "Returns non-nil if there is only one window.
58 Optional arg NOMINI non-nil means don't count the minibuffer
59 even if it is active."
60 (let ((base-window (selected-window)))
61 (if (and nomini
(eq base-window
(minibuffer-window)))
62 (setq base-window
(next-window base-window
)))
64 (next-window base-window
(if nomini
'arg
)))))
66 (defun walk-windows (proc &optional minibuf all-frames
)
67 "Cycle through all visible windows, calling PROC for each one.
68 PROC is called with a window as argument.
69 Optional second arg MINIBUF t means count the minibuffer window
70 even if not active. If MINIBUF is neither t nor nil it means
71 not to count the minibuffer even if it is active.
73 Optional third arg ALL-FRAMES, if t, means include all frames.
74 ALL-FRAMES nil or omitted means cycle within the selected frame,
75 but include the minibuffer window (if MINIBUF says so) that that
76 frame uses, even if it is on another frame.
77 If ALL-FRAMES is neither nil nor t, stick strictly to the selected frame."
78 (let* ((walk-windows-start (selected-window))
79 (walk-windows-current walk-windows-start
))
81 (setq walk-windows-current
82 (next-window walk-windows-current minibuf all-frames
))
83 (funcall proc walk-windows-current
)
84 (not (eq walk-windows-current walk-windows-start
))))))
93 ;Prevent the \{...} documentation construct
94 ;from mentioning keys that run this command.
95 (put 'undefined
'suppress-keymap t
)
97 (defun suppress-keymap (map &optional nodigits
)
98 "Make MAP override all normally self-inserting keys to be undefined.
99 Normally, as an exception, digits and minus-sign are set to make prefix args,
100 but optional second arg NODIGITS non-nil treats them like other chars."
103 (if (eql (lookup-key global-map
(char-to-string i
)) 'self-insert-command
)
104 (define-key map
(char-to-string i
) 'undefined
))
108 (define-key map
"-" 'negative-argument
)
109 ;; Make plain numbers do numeric args.
112 (define-key map
(char-to-string loop
) 'digit-argument
)
113 (setq loop
(1+ loop
))))))
116 ;(defun copy-keymap (keymap)
117 ; "Return a copy of KEYMAP"
118 ; (while (not (keymapp keymap))
119 ; (setq keymap (signal 'wrong-type-argument (list 'keymapp keymap))))
120 ; (if (vectorp keymap)
121 ; (copy-sequence keymap)
122 ; (copy-alist keymap)))
124 (defun substitute-key-definition (olddef newdef keymap
&optional oldmap prefix
)
125 "Replace OLDDEF with NEWDEF for any keys in KEYMAP now defined as OLDDEF.
126 In other words, OLDDEF is replaced with NEWDEF where ever it appears.
127 If optional fourth argument OLDMAP is specified, we redefine
128 in KEYMAP as NEWDEF those chars which are defined as OLDDEF in OLDMAP."
129 (or prefix
(setq prefix
""))
130 (let* ((scan (or oldmap keymap
))
132 (prefix1 (vconcat prefix vec1
)))
133 ;; Scan OLDMAP, finding each char or event-symbol that
134 ;; has any definition, and act on it with hack-key.
136 (if (consp (car scan
))
137 (let ((char (car (car scan
)))
138 (defn (cdr (car scan
))))
139 ;; The inside of this let duplicates exactly
140 ;; the inside of the following let that handles array elements.
142 (aset prefix1
(length prefix
) char
)
144 ;; Skip past menu-prompt.
145 (while (stringp (car-safe defn
))
146 (setq defn
(cdr defn
)))
147 (setq inner-def defn
)
148 (while (and (symbolp inner-def
)
150 (setq inner-def
(symbol-function inner-def
)))
152 (define-key keymap prefix1 newdef
)
154 (substitute-key-definition olddef newdef keymap
157 (if (arrayp (car scan
))
158 (let* ((array (car scan
))
162 (let ((char i
) (defn (aref array i
)))
163 ;; The inside of this let duplicates exactly
164 ;; the inside of the previous let.
166 (aset prefix1
(length prefix
) char
)
168 ;; Skip past menu-prompt.
169 (while (stringp (car-safe defn
))
170 (setq defn
(cdr defn
)))
171 (setq inner-def defn
)
172 (while (and (symbolp inner-def
)
174 (setq inner-def
(symbol-function inner-def
)))
176 (define-key keymap prefix1 newdef
)
178 (substitute-key-definition olddef newdef keymap
182 (setq scan
(cdr scan
)))))
184 (defun keyboard-translate (from to
)
185 "Translate character FROM to TO at a low level.
186 This function creates a `keyboard-translate-table' if necessary
187 and then modifies one entry in it."
188 (or (arrayp keyboard-translate-table
)
189 (setq keyboard-translate-table
""))
190 (if (or (> from
(length keyboard-translate-table
))
191 (> to
(length keyboard-translate-table
)))
193 (let* ((i (length keyboard-translate-table
))
194 (table (make-string (- 256 i
) 0)))
198 (setq keyboard-translate-table table
))))
199 (aset keyboard-translate-table from to
))
202 ;;;; The global keymap tree.
204 ;;; global-map, esc-map, and ctl-x-map have their values set up in
205 ;;; keymap.c; we just give them docstrings here.
207 (defvar global-map nil
208 "Default global keymap mapping Emacs keyboard input into commands.
209 The value is a keymap which is usually (but not necessarily) Emacs's
213 "Default keymap for ESC (meta) commands.
214 The normal global definition of the character ESC indirects to this keymap.")
216 (defvar ctl-x-map nil
217 "Default keymap for C-x commands.
218 The normal global definition of the character C-x indirects to this keymap.")
220 (defvar ctl-x-4-map
(make-sparse-keymap)
221 "Keymap for subcommands of C-x 4")
222 (defalias 'ctl-x-4-prefix ctl-x-4-map
)
223 (define-key ctl-x-map
"4" 'ctl-x-4-prefix
)
225 (defvar ctl-x-5-map
(make-sparse-keymap)
226 "Keymap for frame commands.")
227 (defalias 'ctl-x-5-prefix ctl-x-5-map
)
228 (define-key ctl-x-map
"5" 'ctl-x-5-prefix
)
231 ;;;; Event manipulation functions.
233 ;; This code exists specifically to make sure that the
234 ;; resulting number does not appear in the .elc file.
235 ;; The number is negative on most machines, but not on all!
236 (defconst listify-key-sequence-1
238 (setq listify-key-sequence-1
(logior (lsh 1 23) listify-key-sequence-1
))
240 (defun listify-key-sequence (key)
241 "Convert a key sequence to a list of events."
244 (mapcar (function (lambda (c)
246 (logxor c listify-key-sequence-1
)
250 (defsubst eventp
(obj)
251 "True if the argument is an event object."
254 (get obj
'event-symbol-elements
))
257 (get (car obj
) 'event-symbol-elements
))))
259 (defun event-modifiers (event)
260 "Returns a list of symbols representing the modifier keys in event EVENT.
261 The elements of the list may include `meta', `control',
262 `shift', `hyper', `super', `alt', `click', `drag', and `down'."
265 (setq type
(car type
)))
267 (cdr (get type
'event-symbol-elements
))
269 (or (zerop (logand type
(lsh 1 23)))
270 (setq list
(cons 'meta list
)))
271 (or (and (zerop (logand type
(lsh 1 22)))
272 (>= (logand type
127) 32))
273 (setq list
(cons 'control list
)))
274 (or (and (zerop (logand type
(lsh 1 21)))
275 (= (logand type
255) (downcase (logand type
255))))
276 (setq list
(cons 'shift list
)))
277 (or (zerop (logand type
(lsh 1 20)))
278 (setq list
(cons 'hyper list
)))
279 (or (zerop (logand type
(lsh 1 19)))
280 (setq list
(cons 'super list
)))
281 (or (zerop (logand type
(lsh 1 18)))
282 (setq list
(cons 'alt list
)))
285 (defun event-basic-type (event)
286 "Returns the basic type of the given event (all modifiers removed).
287 The value is an ASCII printing character (not upper case) or a symbol."
289 (car (get event
'event-symbol-elements
))
290 (let ((base (logand event
(1- (lsh 1 18)))))
291 (downcase (if (< base
32) (logior base
64) base
)))))
293 (defsubst mouse-movement-p
(object)
294 "Return non-nil if OBJECT is a mouse movement event."
296 (eq (car object
) 'mouse-movement
)))
298 (defsubst event-start
(event)
299 "Return the starting position of EVENT.
300 If EVENT is a mouse press or a mouse click, this returns the location
302 If EVENT is a drag, this returns the drag's starting position.
303 The return value is of the form
304 (WINDOW BUFFER-POSITION (COL . ROW) TIMESTAMP)
305 The `posn-' functions access elements of such lists."
308 (defsubst event-end
(event)
309 "Return the ending location of EVENT. EVENT should be a click or drag event.
310 If EVENT is a click event, this function is the same as `event-start'.
311 The return value is of the form
312 (WINDOW BUFFER-POSITION (COL . ROW) TIMESTAMP)
313 The `posn-' functions access elements of such lists."
314 (nth (1- (length event
)) event
))
316 (defsubst posn-window
(position)
317 "Return the window in POSITION.
318 POSITION should be a list of the form
319 (WINDOW BUFFER-POSITION (COL . ROW) TIMESTAMP)
320 as returned by the `event-start' and `event-end' functions."
323 (defsubst posn-point
(position)
324 "Return the buffer location in POSITION.
325 POSITION should be a list of the form
326 (WINDOW BUFFER-POSITION (COL . ROW) TIMESTAMP)
327 as returned by the `event-start' and `event-end' functions."
330 (defsubst posn-col-row
(position)
331 "Return the row and column in POSITION.
332 POSITION should be a list of the form
333 (WINDOW BUFFER-POSITION (COL . ROW) TIMESTAMP)
334 as returned by the `event-start' and `event-end' functions."
337 (defsubst posn-timestamp
(position)
338 "Return the timestamp of POSITION.
339 POSITION should be a list of the form
340 (WINDOW BUFFER-POSITION (COL . ROW) TIMESTAMP)
341 as returned by the `event-start' and `event-end' functions."
345 ;;;; Obsolescent names for functions.
347 (defalias 'make-syntax-table
'copy-syntax-table
)
348 (defalias 'dot
'point
)
349 (defalias 'dot-marker
'point-marker
)
350 (defalias 'dot-min
'point-min
)
351 (defalias 'dot-max
'point-max
)
352 (defalias 'window-dot
'window-point
)
353 (defalias 'set-window-dot
'set-window-point
)
354 (defalias 'read-input
'read-string
)
355 (defalias 'send-string
'process-send-string
)
356 (defalias 'send-region
'process-send-region
)
357 (defalias 'show-buffer
'set-window-buffer
)
358 (defalias 'buffer-flush-undo
'buffer-disable-undo
)
359 (defalias 'eval-current-buffer
'eval-buffer
)
360 (defalias 'compiled-function-p
'byte-code-function-p
)
362 ;;; This name isn't mentioned in the manual, and we've been hoping to
363 ;;; phase it out, but there's still a lot of code out there, even for
364 ;;; Emacs 18.59, which uses mod. I'm going to let the byte compiler's
365 ;;; make-obsolete function to poke people a little more, and leave the
366 ;;; `mod' name around for a while longer.
369 ;; Some programs still use this as a function.
371 "Obsolete function returning the value of the `baud-rate' variable.
372 Please convert your programs to use the variable `baud-rate' directly."
376 ;;;; Alternate names for functions - these are not being phased out.
378 (defalias 'string
= 'string-equal
)
379 (defalias 'string
< 'string-lessp
)
380 (defalias 'move-marker
'set-marker
)
382 (defalias 'not
'null
)
383 (defalias 'rplaca
'setcar
)
384 (defalias 'rplacd
'setcdr
)
385 (defalias 'beep
'ding
) ;preserve lingual purtity
386 (defalias 'indent-to-column
'indent-to
)
387 (defalias 'backward-delete-char
'delete-backward-char
)
388 (defalias 'search-forward-regexp
(symbol-function 're-search-forward
))
389 (defalias 'search-backward-regexp
(symbol-function 're-search-backward
))
390 (defalias 'int-to-string
'number-to-string
)
392 ;;; Should this be an obsolete name? If you decide it should, you get
393 ;;; to go through all the sources and change them.
394 (defalias 'string-to-int
'string-to-number
)
396 ;;;; Hook manipulation functions.
398 (defun run-hooks (&rest hooklist
)
399 "Takes hook names and runs each one in turn. Major mode functions use this.
400 Each argument should be a symbol, a hook variable.
401 These symbols are processed in the order specified.
402 If a hook symbol has a non-nil value, that value may be a function
403 or a list of functions to be called to run the hook.
404 If the value is a function, it is called with no arguments.
405 If it is a list, the elements are called, in order, with no arguments."
407 (let ((sym (car hooklist
)))
410 (let ((value (symbol-value sym
)))
411 (if (and (listp value
) (not (eq (car value
) 'lambda
)))
412 (mapcar 'funcall value
)
414 (setq hooklist
(cdr hooklist
))))
416 ;; Tell C code how to call this function.
417 (defconst run-hooks
'run-hooks
418 "Variable by which C primitives find the function `run-hooks'.
421 (defun add-hook (hook function
&optional append
)
422 "Add to the value of HOOK the function FUNCTION unless already present (it
423 becomes the first hook on the list unless optional APPEND is non-nil, in
424 which case it becomes the last). HOOK should be a symbol, and FUNCTION may be
425 any valid function. HOOK's value should be a list of functions, not a single
426 function. If HOOK is void, it is first set to nil."
427 (or (boundp hook
) (set hook nil
))
428 (or (if (consp function
)
429 ;; Clever way to tell whether a given lambda-expression
430 ;; is equal to anything in the hook.
431 (let ((tail (assoc (cdr function
) (symbol-value hook
))))
432 (equal function tail
))
433 (memq function
(symbol-value hook
)))
436 (nconc (symbol-value hook
) (list function
))
437 (cons function
(symbol-value hook
))))))
440 ;;;; Specifying things to do after certain files are loaded.
442 (defun eval-after-load (file form
)
443 "Arrange that, if FILE is ever loaded, FORM will be run at that time.
444 This makes or adds to an entry on `after-load-alist'.
445 FILE should be the name of a library, with no directory name."
446 (or (assoc file after-load-alist
)
447 (setq after-load-alist
(cons (list file
) after-load-alist
)))
448 (nconc (assoc file after-load-alist
) (list form
))
451 (defun eval-next-after-load (file)
452 "Read the following input sexp, and run it whenever FILE is loaded.
453 This makes or adds to an entry on `after-load-alist'.
454 FILE should be the name of a library, with no directory name."
455 (eval-after-load file
(read)))
458 ;;;; Input and display facilities.
460 (defun read-quoted-char (&optional prompt
)
461 "Like `read-char', except that if the first character read is an octal
462 digit, we read up to two more octal digits and return the character
463 represented by the octal number consisting of those digits.
464 Optional argument PROMPT specifies a string to use to prompt the user."
465 (let ((count 0) (code 0) char
)
467 (let ((inhibit-quit (zerop count
))
469 (and prompt
(message "%s-" prompt
))
470 (setq char
(read-char))
471 (if inhibit-quit
(setq quit-flag nil
)))
473 ((and (<= ?
0 char
) (<= char ?
7))
474 (setq code
(+ (* code
8) (- char ?
0))
476 (and prompt
(message (setq prompt
477 (format "%s %c" prompt char
)))))
479 (setq unread-command-events
(list char
) count
259))
480 (t (setq code char count
259))))
483 (defun force-mode-line-update (&optional all
)
484 "Force the mode-line of the current buffer to be redisplayed.
485 With optional non-nil ALL then force then force redisplay of all mode-lines."
486 (if all
(save-excursion (set-buffer (other-buffer))))
487 (set-buffer-modified-p (buffer-modified-p)))
489 (defun momentary-string-display (string pos
&optional exit-char message
)
490 "Momentarily display STRING in the buffer at POS.
491 Display remains until next character is typed.
492 If the char is EXIT-CHAR (optional third arg, default is SPC) it is swallowed;
493 otherwise it is then available as input (as a command if nothing else).
494 Display MESSAGE (optional fourth arg) in the echo area.
495 If MESSAGE is nil, instructions to type EXIT-CHAR are displayed there."
496 (or exit-char
(setq exit-char ?\
))
497 (let ((buffer-read-only nil
)
498 (modified (buffer-modified-p))
499 (name buffer-file-name
)
505 ;; defeat file locking... don't try this at home, kids!
506 (setq buffer-file-name nil
)
507 (insert-before-markers string
)
508 (setq insert-end
(point)))
509 (message (or message
"Type %s to continue editing.")
510 (single-key-description exit-char
))
511 (let ((char (read-event)))
512 (or (eq char exit-char
)
513 (setq unread-command-events
(list char
)))))
516 (delete-region pos insert-end
)))
517 (setq buffer-file-name name
)
518 (set-buffer-modified-p modified
))))
523 (defun ignore (&rest ignore
)
525 Accept any number of arguments, but ignore them."
528 (defun error (&rest args
)
529 "Signal an error, making error message by passing all args to `format'."
531 (signal 'error
(list (apply 'format args
)))))
533 (defun user-original-login-name ()
534 "Return user's login name from original login.
535 This tries to remain unaffected by `su', by looking in environment variables."
536 (or (getenv "LOGNAME") (getenv "USER") (user-login-name)))
538 (defun start-process-shell-command (name buffer
&rest args
)
539 "Start a program in a subprocess. Return the process object for it.
540 Args are NAME BUFFER COMMAND &rest COMMAND-ARGS.
541 NAME is name for process. It is modified if necessary to make it unique.
542 BUFFER is the buffer or (buffer-name) to associate with the process.
543 Process output goes at end of that buffer, unless you specify
544 an output stream or filter function to handle the output.
545 BUFFER may be also nil, meaning that this process is not associated
547 Third arg is command name, the name of a shell command.
548 Remaining arguments are the arguments for the command.
549 Wildcards and redirection are handle as usual in the shell."
550 (if (eq system-type
'vax-vms
)
551 (apply 'start-process name buffer args
)
552 (start-process name buffer shell-file-name
"-c"
553 (concat "exec " (mapconcat 'identity args
" ")))))
555 (defmacro save-match-data
(&rest body
)
556 "Execute the BODY forms, restoring the global value of the match data."
557 (let ((original (make-symbol "match-data")))
559 'let
(list (list original
'(match-data)))
560 (list 'unwind-protect
562 (list 'store-match-data original
)))))
566 ; "Returns the Nth element of LIST.
567 ;N counts from zero. If LIST is not that long, nil is returned."
568 ; (car (nthcdr n list)))
570 ;(defun copy-alist (alist)
571 ; "Return a copy of ALIST.
572 ;This is a new alist which represents the same mapping
573 ;from objects to objects, but does not share the alist structure with ALIST.
574 ;The objects mapped (cars and cdrs of elements of the alist)
575 ;are shared, however."
576 ; (setq alist (copy-sequence alist))
577 ; (let ((tail alist))
579 ; (if (consp (car tail))
580 ; (setcar tail (cons (car (car tail)) (cdr (car tail)))))
581 ; (setq tail (cdr tail))))
584 ;;; subr.el ends here