BSD 4_4 development
[unix-history] / usr / src / contrib / emacs-18.57 / lisp / subr.el
CommitLineData
788a2542
C
1;; Basic lisp subroutines for Emacs
2;; Copyright (C) 1985, 1986, 1990 Free Software Foundation, Inc.
3
4;; This file is part of GNU Emacs.
5
6;; GNU Emacs is free software; you can redistribute it and/or modify
7;; it under the terms of the GNU General Public License as published by
8;; the Free Software Foundation; either version 1, or (at your option)
9;; any later version.
10
11;; GNU Emacs is distributed in the hope that it will be useful,
12;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14;; GNU General Public License for more details.
15
16;; You should have received a copy of the GNU General Public License
17;; along with GNU Emacs; see the file COPYING. If not, write to
18;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
19
20
21(defun one-window-p (&optional arg)
22 "Returns non-nil if there is only one window.
23Optional arg NOMINI non-nil means don't count the minibuffer
24even if it is active."
25 (eq (selected-window)
26 (next-window (selected-window) (if arg 'arg))))
27
28(defun read-quoted-char (&optional prompt)
29 "Like `read-char', except that if the first character read is an octal
30digit, we read up to two more octal digits and return the character
31represented by the octal number consisting of those digits.
32Optional argument PROMPT specifies a string to use to prompt the user."
33 (let ((count 0) (code 0) char)
34 (while (< count 3)
35 (let ((inhibit-quit (zerop count))
36 (help-form nil))
37 (and prompt (message "%s-" prompt))
38 (setq char (read-char))
39 (if inhibit-quit (setq quit-flag nil)))
40 (cond ((null char))
41 ((and (<= ?0 char) (<= char ?7))
42 (setq code (+ (* code 8) (- char ?0))
43 count (1+ count))
44 (and prompt (message (setq prompt
45 (format "%s %c" prompt char)))))
46 ((> count 0)
47 (setq unread-command-char char count 259))
48 (t (setq code char count 259))))
49 (logand 255 code)))
50
51(defun error (&rest args)
52 "Signal an error, making error message by passing all args to `format'."
53 (while t
54 (signal 'error (list (apply 'format args)))))
55
56(defun undefined ()
57 (interactive)
58 (ding))
59
60;Prevent the \{...} documentation construct
61;from mentioning keys that run this command.
62(put 'undefined 'suppress-keymap t)
63
64(defun suppress-keymap (map &optional arg)
65 "Make MAP override all buffer-modifying commands to be undefined.
66Works by knowing which commands are normally buffer-modifying.
67Normally also makes digits set numeric arg,
68but optional second arg NODIGITS non-nil prevents this."
69 (let ((i ? ))
70 (while (< i 127)
71 (aset map i 'undefined)
72 (setq i (1+ i))))
73 (or arg
74 (let (loop)
75 (aset map ?- 'negative-argument)
76 ;; Make plain numbers do numeric args.
77 (setq loop ?0)
78 (while (<= loop ?9)
79 (aset map loop 'digit-argument)
80 (setq loop (1+ loop))))))
81
82;; now in fns.c
83;(defun nth (n list)
84; "Returns the Nth element of LIST.
85;N counts from zero. If LIST is not that long, nil is returned."
86; (car (nthcdr n list)))
87;
88;(defun copy-alist (alist)
89; "Return a copy of ALIST.
90;This is a new alist which represents the same mapping
91;from objects to objects, but does not share the alist structure with ALIST.
92;The objects mapped (cars and cdrs of elements of the alist)
93;are shared, however."
94; (setq alist (copy-sequence alist))
95; (let ((tail alist))
96; (while tail
97; (if (consp (car tail))
98; (setcar tail (cons (car (car tail)) (cdr (car tail)))))
99; (setq tail (cdr tail))))
100; alist)
101
102;Moved to keymap.c
103;(defun copy-keymap (keymap)
104; "Return a copy of KEYMAP"
105; (while (not (keymapp keymap))
106; (setq keymap (signal 'wrong-type-argument (list 'keymapp keymap))))
107; (if (vectorp keymap)
108; (copy-sequence keymap)
109; (copy-alist keymap)))
110
111(defun substitute-key-definition (olddef newdef keymap)
112 "Replace OLDDEF with NEWDEF for any keys in KEYMAP now defined as OLDDEF.
113In other words, OLDDEF is replaced with NEWDEF where ever it appears."
114 (if (arrayp keymap)
115 (let ((len (length keymap))
116 (i 0))
117 (while (< i len)
118 (if (eq (aref keymap i) olddef)
119 (aset keymap i newdef))
120 (setq i (1+ i))))
121 (while keymap
122 (if (eq (cdr-safe (car-safe keymap)) olddef)
123 (setcdr (car keymap) newdef))
124 (setq keymap (cdr keymap)))))
125
126;; Avoids useless byte-compilation.
127;; In the future, would be better to fix byte compiler
128;; not to really compile in cases like this,
129;; and use defun here.
130(fset 'ignore '(lambda (&rest ignore) nil))
131
132\f
133; old names
134(fset 'make-syntax-table 'copy-syntax-table)
135(fset 'dot 'point)
136(fset 'dot-marker 'point-marker)
137(fset 'dot-min 'point-min)
138(fset 'dot-max 'point-max)
139(fset 'window-dot 'window-point)
140(fset 'set-window-dot 'set-window-point)
141(fset 'read-input 'read-string)
142(fset 'send-string 'process-send-string)
143(fset 'send-region 'process-send-region)
144(fset 'show-buffer 'set-window-buffer)
145
146; alternate names
147(fset 'string= 'string-equal)
148(fset 'string< 'string-lessp)
149(fset 'mod '%)
150(fset 'move-marker 'set-marker)
151(fset 'eql 'eq)
152(fset 'not 'null)
153(fset 'numberp 'integerp)
154(fset 'rplaca 'setcar)
155(fset 'rplacd 'setcdr)
156(fset 'beep 'ding) ;preserve lingual purtity
157(fset 'indent-to-column 'indent-to)
158(fset 'backward-delete-char 'delete-backward-char)
159\f
160(defvar global-map nil
161 "Default global keymap mapping Emacs keyboard input into commands.
162The value is a keymap which is usually (but not necessarily) Emacs's
163global map.")
164
165(defvar ctl-x-map nil
166 "Default keymap for C-x commands.
167The normal global definition of the character C-x indirects to this keymap.")
168
169(defvar esc-map nil
170 "Default keymap for ESC (meta) commands.
171The normal global definition of the character ESC indirects to this keymap.")
172
173(defvar mouse-map nil
174 "Keymap for mouse commands from the X window system.")
175\f
176(defun run-hooks (&rest hooklist)
177 "Takes hook names and runs each one in turn. Major mode functions use this.
178Each argument should be a symbol, a hook variable.
179These symbols are processed in the order specified.
180If a hook symbol has a non-nil value, that value may be a function
181or a list of functions to be called to run the hook.
182If the value is a function, it is called with no arguments.
183If it is a list, the elements are called, in order, with no arguments."
184 (while hooklist
185 (let ((sym (car hooklist)))
186 (and (boundp sym)
187 (symbol-value sym)
188 (let ((value (symbol-value sym)))
189 (if (and (listp value) (not (eq (car value) 'lambda)))
190 (mapcar 'funcall value)
191 (funcall value)))))
192 (setq hooklist (cdr hooklist))))
193\f
194(defun momentary-string-display (string pos &optional exit-char message)
195 "Momentarily display STRING in the buffer at POS.
196Display remains until next character is typed.
197If the char is EXIT-CHAR (optional third arg, default is SPC) it is swallowed;
198otherwise it is then available as input (as a command if nothing else).
199Display MESSAGE (optional fourth arg) in the echo area.
200If MESSAGE is nil, instructions to type EXIT-CHAR are displayed there."
201 (or exit-char (setq exit-char ?\ ))
202 (let ((buffer-read-only nil)
203 (modified (buffer-modified-p))
204 (name buffer-file-name)
205 insert-end)
206 (unwind-protect
207 (progn
208 (save-excursion
209 (goto-char pos)
210 ;; defeat file locking... don't try this at home, kids!
211 (setq buffer-file-name nil)
212 (insert-before-markers string)
213 (setq insert-end (point)))
214 (message (or message "Type %s to continue editing.")
215 (single-key-description exit-char))
216 (let ((char (read-char)))
217 (or (eq char exit-char)
218 (setq unread-command-char char))))
219 (if insert-end
220 (save-excursion
221 (delete-region pos insert-end)))
222 (setq buffer-file-name name)
223 (set-buffer-modified-p modified))))
224\f
225(defun undo-start ()
226 "Move undo-pointer to front of undo records.
227The next call to undo-more will undo the most recently made change."
228 (if (eq buffer-undo-list t)
229 (error "No undo information in this buffer"))
230 (setq pending-undo-list buffer-undo-list))
231
232(defun undo-more (count)
233 "Undo back N undo-boundaries beyond what was already undone recently.
234Call undo-start to get ready to undo recent changes,
235then call undo-more one or more times to undo them."
236 (or pending-undo-list
237 (error "No further undo information"))
238 (setq pending-undo-list (primitive-undo count pending-undo-list)))