* gud.el (gud-break): With a prefix argument, set a temporary
[bpt/emacs.git] / lisp / subr.el
CommitLineData
c88ab9ce 1;;; subr.el --- basic lisp subroutines for Emacs
630cc463 2
492878e4 3;;; Copyright (C) 1985, 1986, 1992 Free Software Foundation, Inc.
be9b65ac
DL
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
492878e4 9;; the Free Software Foundation; either version 2, or (at your option)
be9b65ac
DL
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
630cc463 21;;; Code:
be9b65ac 22
492878e4 23(defun one-window-p (&optional nomini)
be9b65ac
DL
24 "Returns non-nil if there is only one window.
25Optional arg NOMINI non-nil means don't count the minibuffer
26even if it is active."
492878e4
JB
27 (let ((base-window (selected-window)))
28 (if (and nomini (eq base-window (minibuffer-window)))
29 (setq base-window (next-window base-window)))
30 (eq base-window
31 (next-window base-window (if nomini 'arg)))))
be9b65ac 32
0cc89026 33(defun walk-windows (proc &optional minibuf all-frames)
be9b65ac
DL
34 "Cycle through all visible windows, calling PROC for each one.
35PROC is called with a window as argument.
36Optional second arg MINIBUF t means count the minibuffer window
37even if not active. If MINIBUF is neither t nor nil it means
38not to count the minibuffer even if it is active.
0cc89026
JB
39Optional third arg ALL-FRAMES t means include all windows in all frames;
40otherwise cycle within the selected frame."
be9b65ac
DL
41 (let* ((walk-windows-start (selected-window))
42 (walk-windows-current walk-windows-start))
43 (while (progn
44 (setq walk-windows-current
0cc89026 45 (next-window walk-windows-current minibuf all-frames))
be9b65ac
DL
46 (funcall proc walk-windows-current)
47 (not (eq walk-windows-current walk-windows-start))))))
48
49(defun read-quoted-char (&optional prompt)
50 "Like `read-char', except that if the first character read is an octal
51digit, we read up to two more octal digits and return the character
52represented by the octal number consisting of those digits.
53Optional argument PROMPT specifies a string to use to prompt the user."
54 (let ((count 0) (code 0) char)
55 (while (< count 3)
56 (let ((inhibit-quit (zerop count))
57 (help-form nil))
58 (and prompt (message "%s-" prompt))
59 (setq char (read-char))
60 (if inhibit-quit (setq quit-flag nil)))
61 (cond ((null char))
62 ((and (<= ?0 char) (<= char ?7))
63 (setq code (+ (* code 8) (- char ?0))
64 count (1+ count))
65 (and prompt (message (setq prompt
66 (format "%s %c" prompt char)))))
67 ((> count 0)
dbc4e1c1 68 (setq unread-command-events (list char) count 259))
be9b65ac
DL
69 (t (setq code char count 259))))
70 (logand 255 code)))
71
72(defun error (&rest args)
73 "Signal an error, making error message by passing all args to `format'."
74 (while t
75 (signal 'error (list (apply 'format args)))))
76
77(defun undefined ()
78 (interactive)
79 (ding))
80
8b824916
RS
81;; Some programs still use this as a function.
82(defun baud-rate ()
83 "Obsolete function returning the value of the `baud-rate' variable."
84 baud-rate)
85
be9b65ac
DL
86;Prevent the \{...} documentation construct
87;from mentioning keys that run this command.
88(put 'undefined 'suppress-keymap t)
89
90(defun suppress-keymap (map &optional nodigits)
91 "Make MAP override all normally self-inserting keys to be undefined.
92Normally, as an exception, digits and minus-sign are set to make prefix args,
93but optional second arg NODIGITS non-nil treats them like other chars."
94 (let ((i 0))
95 (while (<= i 127)
96 (if (eql (lookup-key global-map (char-to-string i)) 'self-insert-command)
97 (define-key map (char-to-string i) 'undefined))
98 (setq i (1+ i))))
99 (or nodigits
100 (let (loop)
101 (define-key map "-" 'negative-argument)
102 ;; Make plain numbers do numeric args.
103 (setq loop ?0)
104 (while (<= loop ?9)
105 (define-key map (char-to-string loop) 'digit-argument)
106 (setq loop (1+ loop))))))
107
108;; now in fns.c
109;(defun nth (n list)
110; "Returns the Nth element of LIST.
111;N counts from zero. If LIST is not that long, nil is returned."
112; (car (nthcdr n list)))
113;
114;(defun copy-alist (alist)
115; "Return a copy of ALIST.
116;This is a new alist which represents the same mapping
117;from objects to objects, but does not share the alist structure with ALIST.
118;The objects mapped (cars and cdrs of elements of the alist)
119;are shared, however."
120; (setq alist (copy-sequence alist))
121; (let ((tail alist))
122; (while tail
123; (if (consp (car tail))
124; (setcar tail (cons (car (car tail)) (cdr (car tail)))))
125; (setq tail (cdr tail))))
126; alist)
127
128;Moved to keymap.c
129;(defun copy-keymap (keymap)
130; "Return a copy of KEYMAP"
131; (while (not (keymapp keymap))
132; (setq keymap (signal 'wrong-type-argument (list 'keymapp keymap))))
133; (if (vectorp keymap)
134; (copy-sequence keymap)
135; (copy-alist keymap)))
136
7f2c2edd 137(defun substitute-key-definition (olddef newdef keymap &optional oldmap prefix)
be9b65ac
DL
138 "Replace OLDDEF with NEWDEF for any keys in KEYMAP now defined as OLDDEF.
139In other words, OLDDEF is replaced with NEWDEF where ever it appears.
7f2c2edd
RS
140If optional fourth argument OLDMAP is specified, we redefine
141in KEYMAP as NEWDEF those chars which are defined as OLDDEF in OLDMAP."
142 (or prefix (setq prefix ""))
143 (let* ((scan (or oldmap keymap))
144 (vec1 (vector nil))
145 (prefix1 (vconcat prefix vec1)))
146 ;; Scan OLDMAP, finding each char or event-symbol that
147 ;; has any definition, and act on it with hack-key.
148 (while (consp scan)
149 (if (consp (car scan))
150 (let ((char (car (car scan)))
151 (defn (cdr (car scan))))
152 ;; The inside of this let duplicates exactly
153 ;; the inside of the following let that handles array elements.
154 (aset vec1 0 char)
155 (aset prefix1 (length prefix) char)
156 (let (inner-def)
157 ;; Skip past menu-prompt.
158 (while (stringp (car-safe defn))
159 (setq defn (cdr defn)))
160 (setq inner-def defn)
161 (while (and (symbolp inner-def)
162 (fboundp inner-def))
163 (setq inner-def (symbol-function inner-def)))
164 (if (eq defn olddef)
165 (define-key keymap prefix1 newdef)
166 (if (keymapp defn)
167 (substitute-key-definition olddef newdef keymap
168 inner-def
169 prefix1)))))
170 (if (arrayp (car scan))
171 (let* ((array (car scan))
172 (len (length array))
173 (i 0))
174 (while (< i len)
175 (let ((char i) (defn (aref array i)))
176 ;; The inside of this let duplicates exactly
177 ;; the inside of the previous let.
178 (aset vec1 0 char)
179 (aset prefix1 (length prefix) char)
180 (let (inner-def)
181 ;; Skip past menu-prompt.
182 (while (stringp (car-safe defn))
183 (setq defn (cdr defn)))
184 (setq inner-def defn)
185 (while (and (symbolp inner-def)
186 (fboundp inner-def))
187 (setq inner-def (symbol-function inner-def)))
188 (if (eq defn olddef)
189 (define-key keymap prefix1 newdef)
190 (if (keymapp defn)
191 (substitute-key-definition olddef newdef keymap
192 inner-def
193 prefix1)))))
194 (setq i (1+ i))))))
195 (setq scan (cdr scan)))))
be9b65ac 196
715984d3
RS
197(defmacro save-match-data (&rest body)
198 "Execute the BODY forms, restoring the global value of the match data."
199 (let ((original (make-symbol "match-data")))
200 (list
201 'let (list (list original '(match-data)))
202 (list 'unwind-protect
203 (cons 'progn body)
204 (list 'store-match-data original)))))
205
a42a4305
RM
206(defun ignore (&rest ignore)
207 "Do nothing.
dbc3787c 208Accept any number of arguments, but ignore them."
a42a4305 209 nil)
be9b65ac
DL
210\f
211; old names
212(fset 'make-syntax-table 'copy-syntax-table)
213(fset 'dot 'point)
214(fset 'dot-marker 'point-marker)
215(fset 'dot-min 'point-min)
216(fset 'dot-max 'point-max)
217(fset 'window-dot 'window-point)
218(fset 'set-window-dot 'set-window-point)
219(fset 'read-input 'read-string)
220(fset 'send-string 'process-send-string)
221(fset 'send-region 'process-send-region)
222(fset 'show-buffer 'set-window-buffer)
223(fset 'buffer-flush-undo 'buffer-disable-undo)
94b304d7 224(fset 'eval-current-buffer 'eval-buffer)
dbc4e1c1 225(fset 'compiled-function-p 'byte-code-function-p)
be9b65ac 226
d2ec8956
JB
227;;; This name isn't mentioned in the manual, and we've been hoping to
228;;; phase it out, but there's still a lot of code out there, even for
229;;; Emacs 18.59, which uses mod. I'm going to let the byte compiler's
230;;; make-obsolete function to poke people a little more, and leave the
231;;; `mod' name around for a while longer.
232(fset 'mod '%)
233
be9b65ac
DL
234; alternate names
235(fset 'string= 'string-equal)
236(fset 'string< 'string-lessp)
237(fset 'move-marker 'set-marker)
238(fset 'eql 'eq)
239(fset 'not 'null)
be9b65ac
DL
240(fset 'rplaca 'setcar)
241(fset 'rplacd 'setcdr)
242(fset 'beep 'ding) ;preserve lingual purtity
243(fset 'indent-to-column 'indent-to)
244(fset 'backward-delete-char 'delete-backward-char)
6f236da8
JB
245(fset 'search-forward-regexp (symbol-function 're-search-forward))
246(fset 'search-backward-regexp (symbol-function 're-search-backward))
be9b65ac 247\f
d95b111b
JB
248;;; global-map, esc-map, and ctl-x-map have their values set up
249;;; in keymap.c.
be9b65ac
DL
250(defvar global-map nil
251 "Default global keymap mapping Emacs keyboard input into commands.
252The value is a keymap which is usually (but not necessarily) Emacs's
253global map.")
254
d95b111b
JB
255(defvar esc-map nil
256 "Default keymap for ESC (meta) commands.
257The normal global definition of the character ESC indirects to this keymap.")
258
be9b65ac
DL
259(defvar ctl-x-map nil
260 "Default keymap for C-x commands.
261The normal global definition of the character C-x indirects to this keymap.")
262
d95b111b
JB
263(defvar ctl-x-4-map (make-sparse-keymap)
264 "Keymap for subcommands of C-x 4")
265(fset 'ctl-x-4-prefix ctl-x-4-map)
266(define-key ctl-x-map "4" 'ctl-x-4-prefix)
267
492878e4 268(defvar ctl-x-5-map (make-sparse-keymap)
0cc89026 269 "Keymap for frame commands.")
492878e4
JB
270(fset 'ctl-x-5-prefix ctl-x-5-map)
271(define-key ctl-x-map "5" 'ctl-x-5-prefix)
be9b65ac 272
be9b65ac
DL
273\f
274(defun run-hooks (&rest hooklist)
275 "Takes hook names and runs each one in turn. Major mode functions use this.
276Each argument should be a symbol, a hook variable.
277These symbols are processed in the order specified.
278If a hook symbol has a non-nil value, that value may be a function
279or a list of functions to be called to run the hook.
280If the value is a function, it is called with no arguments.
281If it is a list, the elements are called, in order, with no arguments."
282 (while hooklist
283 (let ((sym (car hooklist)))
284 (and (boundp sym)
285 (symbol-value sym)
286 (let ((value (symbol-value sym)))
287 (if (and (listp value) (not (eq (car value) 'lambda)))
288 (mapcar 'funcall value)
289 (funcall value)))))
290 (setq hooklist (cdr hooklist))))
291
292;; Tell C code how to call this function.
293(defconst run-hooks 'run-hooks
294 "Variable by which C primitives find the function `run-hooks'.
295Don't change it.")
296
297(defun add-hook (hook function)
298 "Add to the value of HOOK the function FUNCTION unless already present.
299HOOK should be a symbol, and FUNCTION may be any valid function.
300HOOK's value should be a list of functions, not a single function.
301If HOOK is void, it is first set to nil."
302 (or (boundp hook) (set hook nil))
303 (or (if (consp function)
304 ;; Clever way to tell whether a given lambda-expression
305 ;; is equal to anything in the hook.
306 (let ((tail (assoc (cdr function) (symbol-value hook))))
307 (equal function tail))
308 (memq function (symbol-value hook)))
f35fe3c6 309 (set hook (cons function (symbol-value hook)))))
be9b65ac
DL
310\f
311(defun momentary-string-display (string pos &optional exit-char message)
312 "Momentarily display STRING in the buffer at POS.
313Display remains until next character is typed.
314If the char is EXIT-CHAR (optional third arg, default is SPC) it is swallowed;
315otherwise it is then available as input (as a command if nothing else).
316Display MESSAGE (optional fourth arg) in the echo area.
317If MESSAGE is nil, instructions to type EXIT-CHAR are displayed there."
318 (or exit-char (setq exit-char ?\ ))
319 (let ((buffer-read-only nil)
320 (modified (buffer-modified-p))
321 (name buffer-file-name)
322 insert-end)
323 (unwind-protect
324 (progn
325 (save-excursion
326 (goto-char pos)
327 ;; defeat file locking... don't try this at home, kids!
328 (setq buffer-file-name nil)
329 (insert-before-markers string)
330 (setq insert-end (point)))
331 (message (or message "Type %s to continue editing.")
332 (single-key-description exit-char))
333 (let ((char (read-char)))
334 (or (eq char exit-char)
dbc4e1c1 335 (setq unread-command-events (list char)))))
be9b65ac
DL
336 (if insert-end
337 (save-excursion
338 (delete-region pos insert-end)))
339 (setq buffer-file-name name)
340 (set-buffer-modified-p modified))))
341
342(defun start-process-shell-command (name buffer &rest args)
343 "Start a program in a subprocess. Return the process object for it.
344Args are NAME BUFFER COMMAND &rest COMMAND-ARGS.
345NAME is name for process. It is modified if necessary to make it unique.
346BUFFER is the buffer or (buffer-name) to associate with the process.
347 Process output goes at end of that buffer, unless you specify
348 an output stream or filter function to handle the output.
349 BUFFER may be also nil, meaning that this process is not associated
350 with any buffer
351Third arg is command name, the name of a shell command.
352Remaining arguments are the arguments for the command.
353Wildcards and redirection are handle as usual in the shell."
354 (if (eq system-type 'vax-vms)
355 (apply 'start-process name buffer args)
356 (start-process name buffer shell-file-name "-c"
357 (concat "exec " (mapconcat 'identity args " ")))))
358\f
359(defun eval-after-load (file form)
360 "Arrange that, if FILE is ever loaded, FORM will be run at that time.
361This makes or adds to an entry on `after-load-alist'.
362FILE should be the name of a library, with no directory name."
363 (or (assoc file after-load-alist)
364 (setq after-load-alist (cons (list file) after-load-alist)))
365 (nconc (assoc file after-load-alist) (list form))
366 form)
367
368(defun eval-next-after-load (file)
369 "Read the following input sexp, and run it whenever FILE is loaded.
370This makes or adds to an entry on `after-load-alist'.
371FILE should be the name of a library, with no directory name."
372 (eval-after-load file (read)))
408a4c8f 373
e6dfdce5
RS
374;;(defmacro defun-inline (name args &rest body)
375;; "Create an \"inline defun\" (actually a macro).
376;;Use just like `defun'."
377;; (nconc (list 'defmacro name '(&rest args))
378;; (if (stringp (car body))
379;; (prog1 (list (car body))
380;; (setq body (or (cdr body) body))))
381;; (list (list 'cons (list 'quote
382;; (cons 'lambda (cons args body)))
383;; 'args))))
be9b65ac
DL
384\f
385(defun user-original-login-name ()
386 "Return user's login name from original login.
387This tries to remain unaffected by `su', by looking in environment variables."
388 (or (getenv "LOGNAME") (getenv "USER") (user-login-name)))
389\f
390(defun force-mode-line-update (&optional all)
391 "Force the mode-line of the current buffer to be redisplayed.
392With optional non-nil ALL then force then force redisplay of all mode-lines."
393 (if all (save-excursion (set-buffer (other-buffer))))
394 (set-buffer-modified-p (buffer-modified-p)))
395
396(defun keyboard-translate (from to)
397 "Translate character FROM to TO at a low level.
398This function creates a `keyboard-translate-table' if necessary
399and then modifies one entry in it."
b7cceaf1
JB
400 (or (arrayp keyboard-translate-table)
401 (setq keyboard-translate-table ""))
402 (if (or (> from (length keyboard-translate-table))
403 (> to (length keyboard-translate-table)))
404 (progn
405 (let* ((i (length keyboard-translate-table))
406 (table (make-string (- 256 i) 0)))
407 (while (< i 256)
408 (aset table i i)
409 (setq i (1+ i)))
410 (setq keyboard-translate-table table))))
be9b65ac 411 (aset keyboard-translate-table from to))
ffd56f97
JB
412
413\f
414(defmacro lambda (&rest cdr)
397587ee
JB
415 "Return a lambda expression.
416A call of the form (lambda ARGS DOCSTRING INTERACTIVE BODY) is
417self-quoting; the result of evaluating the lambda expression is the
418expression itself. The lambda expression may then be treated as a
419function, i. e. stored as the function value of a symbol, passed to
420funcall or mapcar, etcetera.
421ARGS should take the same form as an argument list for a `defun'.
422DOCSTRING should be a string, as described for `defun'. It may be omitted.
423INTERACTIVE should be a call to the function `interactive', which see.
424It may also be omitted.
425BODY should be a list of lisp expressions."
d77c36e8
JB
426 ;; Note that this definition should not use backquotes; subr.el should not
427 ;; depend on backquote.el.
428 (list 'function (cons 'lambda cdr)))
630cc463
ER
429
430;;; subr.el ends here