]> code.delx.au - gnu-emacs/blob - lisp/subr.el
(mark-c-function): Activate the mark.
[gnu-emacs] / lisp / subr.el
1 ;;; subr.el --- basic lisp subroutines for Emacs
2
3 ;;; Copyright (C) 1985, 1986, 1992 Free Software Foundation, Inc.
4
5 ;; This file is part of GNU Emacs.
6
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)
10 ;; any later version.
11
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.
16
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.
20
21 ;;; Code:
22
23 \f
24 ;;;; Lisp language features.
25
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)))
41
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)))
51 ;; 'args))))
52
53 \f
54 ;;;; Window tree functions.
55
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)))
63 (eq base-window
64 (next-window base-window (if nomini 'arg)))))
65
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.
72
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))
80 (while (progn
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))))))
85
86 \f
87 ;;;; Keymap support.
88
89 (defun undefined ()
90 (interactive)
91 (ding))
92
93 ;Prevent the \{...} documentation construct
94 ;from mentioning keys that run this command.
95 (put 'undefined 'suppress-keymap t)
96
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."
101 (let ((i 0))
102 (while (<= i 127)
103 (if (eql (lookup-key global-map (char-to-string i)) 'self-insert-command)
104 (define-key map (char-to-string i) 'undefined))
105 (setq i (1+ i))))
106 (or nodigits
107 (let (loop)
108 (define-key map "-" 'negative-argument)
109 ;; Make plain numbers do numeric args.
110 (setq loop ?0)
111 (while (<= loop ?9)
112 (define-key map (char-to-string loop) 'digit-argument)
113 (setq loop (1+ loop))))))
114
115 ;Moved to keymap.c
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)))
123
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))
131 (vec1 (vector nil))
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.
135 (while (consp scan)
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.
141 (aset vec1 0 char)
142 (aset prefix1 (length prefix) char)
143 (let (inner-def)
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)
149 (fboundp inner-def))
150 (setq inner-def (symbol-function inner-def)))
151 (if (eq defn olddef)
152 (define-key keymap prefix1 newdef)
153 (if (keymapp defn)
154 (substitute-key-definition olddef newdef keymap
155 inner-def
156 prefix1)))))
157 (if (arrayp (car scan))
158 (let* ((array (car scan))
159 (len (length array))
160 (i 0))
161 (while (< i len)
162 (let ((char i) (defn (aref array i)))
163 ;; The inside of this let duplicates exactly
164 ;; the inside of the previous let.
165 (aset vec1 0 char)
166 (aset prefix1 (length prefix) char)
167 (let (inner-def)
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)
173 (fboundp inner-def))
174 (setq inner-def (symbol-function inner-def)))
175 (if (eq defn olddef)
176 (define-key keymap prefix1 newdef)
177 (if (keymapp defn)
178 (substitute-key-definition olddef newdef keymap
179 inner-def
180 prefix1)))))
181 (setq i (1+ i))))))
182 (setq scan (cdr scan)))))
183
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)))
192 (progn
193 (let* ((i (length keyboard-translate-table))
194 (table (make-string (- 256 i) 0)))
195 (while (< i 256)
196 (aset table i i)
197 (setq i (1+ i)))
198 (setq keyboard-translate-table table))))
199 (aset keyboard-translate-table from to))
200
201 \f
202 ;;;; The global keymap tree.
203
204 ;;; global-map, esc-map, and ctl-x-map have their values set up in
205 ;;; keymap.c; we just give them docstrings here.
206
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
210 global map.")
211
212 (defvar esc-map nil
213 "Default keymap for ESC (meta) commands.
214 The normal global definition of the character ESC indirects to this keymap.")
215
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.")
219
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)
224
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)
229
230 \f
231 ;;;; Event manipulation functions.
232
233 (defun listify-key-sequence (key)
234 "Convert a key sequence to a list of events."
235 (if (vectorp key)
236 (append key nil)
237 (mapcar (function (lambda (c)
238 (if (> c 127)
239 (logxor c ?\M-\200)
240 c)))
241 (append key nil))))
242
243 (defsubst eventp (obj)
244 "True if the argument is an event object."
245 (or (integerp obj)
246 (and (symbolp obj)
247 (get obj 'event-symbol-elements))
248 (and (consp obj)
249 (symbolp (car obj))
250 (get (car obj) 'event-symbol-elements))))
251
252 (defun event-modifiers (event)
253 "Returns a list of symbols representing the modifier keys in event EVENT.
254 The elements of the list may include `meta', `control',
255 `shift', `hyper', `super', `alt'.
256 See also the function `event-modifier-bits'."
257 (let ((type event))
258 (if (listp type)
259 (setq type (car type)))
260 (if (symbolp type)
261 (cdr (get type 'event-symbol-elements))
262 (let ((list nil))
263 (or (zerop (logand type (lsh 1 23)))
264 (setq list (cons 'meta list)))
265 (or (and (zerop (logand type (lsh 1 22)))
266 (>= (logand type 127) 32))
267 (setq list (cons 'control list)))
268 (or (and (zerop (logand type (lsh 1 21)))
269 (= (logand type 255) (downcase (logand type 255))))
270 (setq list (cons 'shift list)))
271 (or (zerop (logand type (lsh 1 20)))
272 (setq list (cons 'hyper list)))
273 (or (zerop (logand type (lsh 1 19)))
274 (setq list (cons 'super list)))
275 (or (zerop (logand type (lsh 1 18)))
276 (setq list (cons 'alt list)))
277 list))))
278
279 (defun event-basic-type (event)
280 "Returns the basic type of the given event (all modifiers removed).
281 The value is an ASCII printing character (not upper case) or a symbol."
282 (if (symbolp event)
283 (car (get event 'event-symbol-elements))
284 (let ((base (logand event (1- (lsh 1 18)))))
285 (downcase (if (< base 32) (logior base 64) base)))))
286
287 (defsubst mouse-movement-p (object)
288 "Return non-nil if OBJECT is a mouse movement event."
289 (and (consp object)
290 (eq (car object) 'mouse-movement)))
291
292 (defsubst event-start (event)
293 "Return the starting position of EVENT.
294 If EVENT is a mouse press or a mouse click, this returns the location
295 of the event.
296 If EVENT is a drag, this returns the drag's starting position.
297 The return value is of the form
298 (WINDOW BUFFER-POSITION (COL . ROW) TIMESTAMP)
299 The `posn-' functions access elements of such lists."
300 (nth 1 event))
301
302 (defsubst event-end (event)
303 "Return the ending location of EVENT. EVENT should be a click or drag event.
304 If EVENT is a click event, this function is the same as `event-start'.
305 The return value is of the form
306 (WINDOW BUFFER-POSITION (COL . ROW) TIMESTAMP)
307 The `posn-' functions access elements of such lists."
308 (nth (1- (length event)) event))
309
310 (defsubst posn-window (position)
311 "Return the window in POSITION.
312 POSITION should be a list of the form
313 (WINDOW BUFFER-POSITION (COL . ROW) TIMESTAMP)
314 as returned by the `event-start' and `event-end' functions."
315 (nth 0 position))
316
317 (defsubst posn-point (position)
318 "Return the buffer location in POSITION.
319 POSITION should be a list of the form
320 (WINDOW BUFFER-POSITION (COL . ROW) TIMESTAMP)
321 as returned by the `event-start' and `event-end' functions."
322 (nth 1 position))
323
324 (defsubst posn-col-row (position)
325 "Return the row and column in POSITION.
326 POSITION should be a list of the form
327 (WINDOW BUFFER-POSITION (COL . ROW) TIMESTAMP)
328 as returned by the `event-start' and `event-end' functions."
329 (nth 2 position))
330
331 (defsubst posn-timestamp (position)
332 "Return the timestamp of POSITION.
333 POSITION should be a list of the form
334 (WINDOW BUFFER-POSITION (COL . ROW) TIMESTAMP)
335 nas returned by the `event-start' and `event-end' functions."
336 (nth 3 position))
337
338 \f
339 ;;;; Overlay dissection functions.
340
341 (defsubst overlay-start (overlay)
342 "Return the position at which OVERLAY starts."
343 (marker-position (car (car overlay))))
344
345 (defsubst overlay-end (overlay)
346 "Return the position at which OVERLAY ends."
347 (marker-position (cdr (car overlay))))
348
349 (defsubst overlay-buffer (overlay)
350 "Return the buffer OVERLAY belongs to."
351 (marker-buffer (overlay-start overlay)))
352
353 \f
354 ;;;; Obsolescent names for functions.
355
356 (defalias 'make-syntax-table 'copy-syntax-table)
357 (defalias 'dot 'point)
358 (defalias 'dot-marker 'point-marker)
359 (defalias 'dot-min 'point-min)
360 (defalias 'dot-max 'point-max)
361 (defalias 'window-dot 'window-point)
362 (defalias 'set-window-dot 'set-window-point)
363 (defalias 'read-input 'read-string)
364 (defalias 'send-string 'process-send-string)
365 (defalias 'send-region 'process-send-region)
366 (defalias 'show-buffer 'set-window-buffer)
367 (defalias 'buffer-flush-undo 'buffer-disable-undo)
368 (defalias 'eval-current-buffer 'eval-buffer)
369 (defalias 'compiled-function-p 'byte-code-function-p)
370
371 ;;; This name isn't mentioned in the manual, and we've been hoping to
372 ;;; phase it out, but there's still a lot of code out there, even for
373 ;;; Emacs 18.59, which uses mod. I'm going to let the byte compiler's
374 ;;; make-obsolete function to poke people a little more, and leave the
375 ;;; `mod' name around for a while longer.
376 (defalias 'mod '%)
377
378 ;; Some programs still use this as a function.
379 (defun baud-rate ()
380 "Obsolete function returning the value of the `baud-rate' variable."
381 baud-rate)
382
383 \f
384 ;;;; Alternate names for functions - these are not being phased out.
385
386 (defalias 'string= 'string-equal)
387 (defalias 'string< 'string-lessp)
388 (defalias 'move-marker 'set-marker)
389 (defalias 'eql 'eq)
390 (defalias 'not 'null)
391 (defalias 'rplaca 'setcar)
392 (defalias 'rplacd 'setcdr)
393 (defalias 'beep 'ding) ;preserve lingual purtity
394 (defalias 'indent-to-column 'indent-to)
395 (defalias 'backward-delete-char 'delete-backward-char)
396 (defalias 'search-forward-regexp (symbol-function 're-search-forward))
397 (defalias 'search-backward-regexp (symbol-function 're-search-backward))
398 (defalias 'int-to-string 'number-to-string)
399
400 ;;; Should this be an obsolete name? If you decide it should, you get
401 ;;; to go through all the sources and change them.
402 (defalias 'string-to-int 'string-to-number)
403 \f
404 ;;;; Hook manipulation functions.
405
406 (defun run-hooks (&rest hooklist)
407 "Takes hook names and runs each one in turn. Major mode functions use this.
408 Each argument should be a symbol, a hook variable.
409 These symbols are processed in the order specified.
410 If a hook symbol has a non-nil value, that value may be a function
411 or a list of functions to be called to run the hook.
412 If the value is a function, it is called with no arguments.
413 If it is a list, the elements are called, in order, with no arguments."
414 (while hooklist
415 (let ((sym (car hooklist)))
416 (and (boundp sym)
417 (symbol-value sym)
418 (let ((value (symbol-value sym)))
419 (if (and (listp value) (not (eq (car value) 'lambda)))
420 (mapcar 'funcall value)
421 (funcall value)))))
422 (setq hooklist (cdr hooklist))))
423
424 ;; Tell C code how to call this function.
425 (defconst run-hooks 'run-hooks
426 "Variable by which C primitives find the function `run-hooks'.
427 Don't change it.")
428
429 (defun add-hook (hook function &optional append)
430 "Add to the value of HOOK the function FUNCTION unless already present (it
431 becomes the first hook on the list unless optional APPEND is non-nil, in
432 which case it becomes the last). HOOK should be a symbol, and FUNCTION may be
433 any valid function. HOOK's value should be a list of functions, not a single
434 function. If HOOK is void, it is first set to nil."
435 (or (boundp hook) (set hook nil))
436 (or (if (consp function)
437 ;; Clever way to tell whether a given lambda-expression
438 ;; is equal to anything in the hook.
439 (let ((tail (assoc (cdr function) (symbol-value hook))))
440 (equal function tail))
441 (memq function (symbol-value hook)))
442 (set hook
443 (if append
444 (nconc (symbol-value hook) (list function))
445 (cons function (symbol-value hook))))))
446
447 \f
448 ;;;; Specifying things to do after certain files are loaded.
449
450 (defun eval-after-load (file form)
451 "Arrange that, if FILE is ever loaded, FORM will be run at that time.
452 This makes or adds to an entry on `after-load-alist'.
453 FILE should be the name of a library, with no directory name."
454 (or (assoc file after-load-alist)
455 (setq after-load-alist (cons (list file) after-load-alist)))
456 (nconc (assoc file after-load-alist) (list form))
457 form)
458
459 (defun eval-next-after-load (file)
460 "Read the following input sexp, and run it whenever FILE is loaded.
461 This makes or adds to an entry on `after-load-alist'.
462 FILE should be the name of a library, with no directory name."
463 (eval-after-load file (read)))
464
465 \f
466 ;;;; Input and display facilities.
467
468 (defun read-quoted-char (&optional prompt)
469 "Like `read-char', except that if the first character read is an octal
470 digit, we read up to two more octal digits and return the character
471 represented by the octal number consisting of those digits.
472 Optional argument PROMPT specifies a string to use to prompt the user."
473 (let ((count 0) (code 0) char)
474 (while (< count 3)
475 (let ((inhibit-quit (zerop count))
476 (help-form nil))
477 (and prompt (message "%s-" prompt))
478 (setq char (read-char))
479 (if inhibit-quit (setq quit-flag nil)))
480 (cond ((null char))
481 ((and (<= ?0 char) (<= char ?7))
482 (setq code (+ (* code 8) (- char ?0))
483 count (1+ count))
484 (and prompt (message (setq prompt
485 (format "%s %c" prompt char)))))
486 ((> count 0)
487 (setq unread-command-events (list char) count 259))
488 (t (setq code char count 259))))
489 (logand 255 code)))
490
491 (defun force-mode-line-update (&optional all)
492 "Force the mode-line of the current buffer to be redisplayed.
493 With optional non-nil ALL then force then force redisplay of all mode-lines."
494 (if all (save-excursion (set-buffer (other-buffer))))
495 (set-buffer-modified-p (buffer-modified-p)))
496
497 (defun momentary-string-display (string pos &optional exit-char message)
498 "Momentarily display STRING in the buffer at POS.
499 Display remains until next character is typed.
500 If the char is EXIT-CHAR (optional third arg, default is SPC) it is swallowed;
501 otherwise it is then available as input (as a command if nothing else).
502 Display MESSAGE (optional fourth arg) in the echo area.
503 If MESSAGE is nil, instructions to type EXIT-CHAR are displayed there."
504 (or exit-char (setq exit-char ?\ ))
505 (let ((buffer-read-only nil)
506 (modified (buffer-modified-p))
507 (name buffer-file-name)
508 insert-end)
509 (unwind-protect
510 (progn
511 (save-excursion
512 (goto-char pos)
513 ;; defeat file locking... don't try this at home, kids!
514 (setq buffer-file-name nil)
515 (insert-before-markers string)
516 (setq insert-end (point)))
517 (message (or message "Type %s to continue editing.")
518 (single-key-description exit-char))
519 (let ((char (read-event)))
520 (or (eq char exit-char)
521 (setq unread-command-events (list char)))))
522 (if insert-end
523 (save-excursion
524 (delete-region pos insert-end)))
525 (setq buffer-file-name name)
526 (set-buffer-modified-p modified))))
527
528 \f
529 ;;;; Miscellanea.
530
531 (defun ignore (&rest ignore)
532 "Do nothing.
533 Accept any number of arguments, but ignore them."
534 nil)
535
536 (defun error (&rest args)
537 "Signal an error, making error message by passing all args to `format'."
538 (while t
539 (signal 'error (list (apply 'format args)))))
540
541 (defun user-original-login-name ()
542 "Return user's login name from original login.
543 This tries to remain unaffected by `su', by looking in environment variables."
544 (or (getenv "LOGNAME") (getenv "USER") (user-login-name)))
545
546 (defun start-process-shell-command (name buffer &rest args)
547 "Start a program in a subprocess. Return the process object for it.
548 Args are NAME BUFFER COMMAND &rest COMMAND-ARGS.
549 NAME is name for process. It is modified if necessary to make it unique.
550 BUFFER is the buffer or (buffer-name) to associate with the process.
551 Process output goes at end of that buffer, unless you specify
552 an output stream or filter function to handle the output.
553 BUFFER may be also nil, meaning that this process is not associated
554 with any buffer
555 Third arg is command name, the name of a shell command.
556 Remaining arguments are the arguments for the command.
557 Wildcards and redirection are handle as usual in the shell."
558 (if (eq system-type 'vax-vms)
559 (apply 'start-process name buffer args)
560 (start-process name buffer shell-file-name "-c"
561 (concat "exec " (mapconcat 'identity args " ")))))
562
563 (defmacro save-match-data (&rest body)
564 "Execute the BODY forms, restoring the global value of the match data."
565 (let ((original (make-symbol "match-data")))
566 (list
567 'let (list (list original '(match-data)))
568 (list 'unwind-protect
569 (cons 'progn body)
570 (list 'store-match-data original)))))
571
572 ;; now in fns.c
573 ;(defun nth (n list)
574 ; "Returns the Nth element of LIST.
575 ;N counts from zero. If LIST is not that long, nil is returned."
576 ; (car (nthcdr n list)))
577 ;
578 ;(defun copy-alist (alist)
579 ; "Return a copy of ALIST.
580 ;This is a new alist which represents the same mapping
581 ;from objects to objects, but does not share the alist structure with ALIST.
582 ;The objects mapped (cars and cdrs of elements of the alist)
583 ;are shared, however."
584 ; (setq alist (copy-sequence alist))
585 ; (let ((tail alist))
586 ; (while tail
587 ; (if (consp (car tail))
588 ; (setcar tail (cons (car (car tail)) (cdr (car tail)))))
589 ; (setq tail (cdr tail))))
590 ; alist)
591
592 ;;; subr.el ends here
593