home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / lemacs-19.6 / lisp / prim / subr.el < prev    next >
Encoding:
Text File  |  1993-02-14  |  15.6 KB  |  406 lines

  1. ;; Basic lisp subroutines for Emacs
  2. ;; Copyright (C) 1985-1993 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 2, 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. ;; called by Fkill_buffer()
  22. (defvar kill-buffer-hook nil
  23.   "Function or functions to be called when a buffer is killed.
  24. The value of this variable may be buffer-local.
  25. The buffer about to be killed is current when this hook is run.")
  26.  
  27. (defun generate-new-buffer (name)
  28.   "Create and return a buffer with a name based on NAME.
  29. Choose the buffer's name using `generate-new-buffer-name'."
  30.   (get-buffer-create (generate-new-buffer-name name)))
  31.  
  32. (defun one-window-p (&optional nomini)
  33.   "Returns non-nil if there is only one window.
  34. Optional arg NOMINI non-nil means don't count the minibuffer
  35. even if it is active."
  36.   (eq (selected-window)
  37.       (next-window (selected-window) (if nomini 'nomini))))
  38.  
  39. (defun walk-windows (proc &optional minibuf all-screens)
  40.   "Cycle through all visible windows, calling PROC for each one.
  41. PROC is called with a window as argument.
  42. Optional second arg MINIBUF t means count the minibuffer window
  43. even if not active.  If MINIBUF is neither t nor nil it means
  44. not to count the minibuffer even if it is active.
  45. Optional third arg ALL-SCREENS t means include all windows in all screens;
  46. otherwise cycle within the selected screen."
  47.   (let* ((walk-windows-start (selected-window))
  48.      (walk-windows-current walk-windows-start))
  49.     (while (progn
  50.          (setq walk-windows-current
  51.            (next-window walk-windows-current minibuf all-screens))
  52.          (funcall proc walk-windows-current)
  53.          (not (eq walk-windows-current walk-windows-start))))))
  54.  
  55. (defun read-quoted-char (&optional prompt)
  56.   "Like `read-char', except that if the first character read is an octal
  57. digit, we read up to two more octal digits and return the character
  58. represented by the octal number consisting of those digits.
  59. Optional argument PROMPT specifies a string to use to prompt the user."
  60.   (let ((count 0) (code 0) char)
  61.     (while (< count 3)
  62.       (let ((inhibit-quit (zerop count))
  63.         (help-form nil))
  64.     (and prompt (message "%s-" prompt))
  65.     (setq char (read-char))
  66.     (if inhibit-quit (setq quit-flag nil)))
  67.       (cond ((null char))
  68.         ((and (<= ?0 char) (<= char ?7))
  69.          (setq code (+ (* code 8) (- char ?0))
  70.            count (1+ count))
  71.          (and prompt (message (setq prompt
  72.                     (format "%s %c" prompt char)))))
  73.         ((> count 0)
  74.          (setq unread-command-event
  75.             (character-to-event char (allocate-event))
  76.            count 259))
  77.         (t (setq code char count 259))))
  78.     (logand 255 code)))
  79.  
  80. (defun error (&rest args)
  81.   "Signal an error, making error message by passing all args to `format'."
  82.   (while t
  83.     (signal 'error (list (apply 'format args)))))
  84.  
  85. (defun undefined ()
  86.   (interactive)
  87.   (ding))
  88.  
  89. ;; Some programs still use this as a function.
  90. (defun baud-rate ()
  91.   "Obsolete function returning the value of the `baud-rate' variable."
  92.   baud-rate)
  93.  
  94. ;Prevent the \{...} documentation construct
  95. ;from mentioning keys that run this command.
  96. (put 'undefined 'suppress-keymap t)
  97.  
  98. (defun suppress-keymap (map &optional nodigits)
  99.   "Make MAP override all normally self-inserting keys to be undefined.
  100. Normally, as an exception, digits and minus-sign are set to make prefix args,
  101. but optional second arg NODIGITS non-nil treats them like other chars."
  102.   (map-keymap (function (lambda (key binding)
  103.               (if (eq binding 'self-insert-command)
  104.                   (define-key map (vector key) 'undefined))))
  105.           global-map)
  106.   (or nodigits
  107.       (let ((string (make-string 1 ?0)))
  108.     (define-key map "-" 'negative-argument)
  109.     ;; Make plain numbers do numeric args.
  110.     (while (<= (aref string 0) ?9)
  111.       (define-key map string 'digit-argument)
  112.       (aset string 0 (1+ (aref string 0)))))))
  113.  
  114. ;; now in fns.c
  115. ;(defun nth (n list)
  116. ;  "Returns the Nth element of LIST.
  117. ;N counts from zero.  If LIST is not that long, nil is returned."
  118. ;  (car (nthcdr n list)))
  119. ;
  120. ;(defun copy-alist (alist)
  121. ;  "Return a copy of ALIST.
  122. ;This is a new alist which represents the same mapping
  123. ;from objects to objects, but does not share the alist structure with ALIST.
  124. ;The objects mapped (cars and cdrs of elements of the alist)
  125. ;are shared, however."
  126. ;  (setq alist (copy-sequence alist))
  127. ;  (let ((tail alist))
  128. ;    (while tail
  129. ;      (if (consp (car tail))
  130. ;      (setcar tail (cons (car (car tail)) (cdr (car tail)))))
  131. ;      (setq tail (cdr tail))))
  132. ;  alist)
  133.  
  134. ;Moved to keymap.c
  135. ;(defun copy-keymap (keymap)
  136. ;  "Return a copy of KEYMAP"  
  137. ;  (while (not (keymapp keymap))
  138. ;    (setq keymap (signal 'wrong-type-argument (list 'keymapp keymap))))
  139. ;  (if (vectorp keymap)
  140. ;      (copy-sequence keymap)
  141. ;      (copy-alist keymap)))
  142.  
  143. ;;;>>> FSF19 takes arguments (olddef newdef keymap &optional oldmap prefix),
  144. ;;;>>> where "If optional fourth argument OLDMAP is specified, we redefine
  145. ;;;>>> in KEYMAP as NEWDEF those chars which are defined as OLDDEF in OLDMAP."
  146. (defun substitute-key-definition (olddef newdef keymap)
  147.   "Replace OLDDEF with NEWDEF for any keys in KEYMAP now defined as OLDDEF.
  148. In other words, OLDDEF is replaced with NEWDEF where ever it appears.
  149. Prefix keymaps reached from KEYMAP are not checked recursively;
  150. perhaps they ought to be."
  151.   (map-keymap (function (lambda (key binding)
  152.               (if (eq binding olddef)
  153.                   (define-key keymap key newdef))))
  154.           keymap))
  155.  
  156. (defmacro save-match-data (&rest body)
  157.   "Execute the BODY forms, restoring the global value of the match data."
  158.   (list 'let '((_match_data_ (match-data)))
  159.     (list 'unwind-protect
  160.           (cons 'progn body)
  161.           '(store-match-data _match_data_))))
  162.  
  163. (defun ignore (&rest ignore) nil)
  164.  
  165. ; old names
  166. (fset 'make-syntax-table 'copy-syntax-table)
  167. (fset 'dot 'point)
  168. (fset 'dot-marker 'point-marker)
  169. (fset 'dot-min 'point-min)
  170. (fset 'dot-max 'point-max)
  171. (fset 'window-dot 'window-point)
  172. (fset 'set-window-dot 'set-window-point)
  173. (fset 'read-input 'read-string)
  174. (fset 'send-string 'process-send-string)
  175. (fset 'send-region 'process-send-region)
  176. (fset 'show-buffer 'set-window-buffer)
  177. (fset 'buffer-flush-undo 'buffer-disable-undo)
  178.  
  179. ; alternate names
  180. (fset 'string= 'string-equal)
  181. (fset 'string< 'string-lessp)
  182. (fset 'mod '%)
  183. (fset 'move-marker 'set-marker)
  184. (fset 'eql 'eq)
  185. (fset 'not 'null)
  186. (fset 'rplaca 'setcar)
  187. (fset 'rplacd 'setcdr)
  188. (fset 'beep 'ding) ;preserve lingual purtity
  189. (fset 'indent-to-column 'indent-to)
  190. (fset 'backward-delete-char 'delete-backward-char)
  191. (fset 'search-forward-regexp (symbol-function 're-search-forward))
  192. (fset 'search-backward-regexp (symbol-function 're-search-backward))
  193.  
  194. (defun run-hooks (&rest hooklist)
  195.   "Takes hook names and runs each one in turn.  Major mode functions use this.
  196. Each argument should be a symbol, a hook variable.
  197. These symbols are processed in the order specified.
  198. If a hook symbol has a non-nil value, that value may be a function
  199. or a list of functions to be called to run the hook.
  200. If the value is a function, it is called with no arguments.
  201. If it is a list, the elements are called, in order, with no arguments."
  202.   (while hooklist
  203.     (let ((sym (car hooklist)))
  204.       (and (boundp sym)
  205.        (symbol-value sym)
  206.        (let ((value (symbol-value sym)))
  207.          (if (and (listp value) (not (eq (car value) 'lambda)))
  208.          (mapcar 'funcall value)
  209.            (funcall value)))))
  210.     (setq hooklist (cdr hooklist))))
  211.  
  212. ;; Tell C code how to call this function.
  213. (defconst run-hooks 'run-hooks
  214.   "Variable by which C primitives find the function `run-hooks'.
  215. Don't change it.")
  216.  
  217. (defun add-hook (hook-var function &optional at-end)
  218.   "Add a function to a hook.
  219. First argument HOOK-VAR (a symbol) is the name of a hook, second
  220.  argument FUNCTION is the function to add.
  221. Third (optional) argument AT-END means to add the function at the end
  222.  of the hook list instead of the beginning.  If the function is already
  223.  present, this has no effect.
  224. Returns nil if FUNCTION was already present in HOOK-VAR, else new
  225.  value of HOOK-VAR."
  226.   ;(interactive "SAdd to hook-var (symbol): \naAdd which function to %s? ")
  227.   (if (not (boundp hook-var)) (set hook-var nil))
  228.   (let ((old (symbol-value hook-var)))
  229.     (if (or (not (listp old)) (eq (car old) 'lambda))
  230.     (setq old (list old)))
  231.     (if (member function old)
  232.     nil
  233.       (set hook-var
  234.        (if at-end
  235.            (append old (list function)) ; don't nconc
  236.          (cons function old))))))
  237.  
  238. (defun remove-hook (hook-var function)
  239.   "Remove a function from a hook, if it is present.
  240. First argument HOOK-VAR (a symbol) is the name of a hook, second
  241.  argument FUNCTION is the function to remove (compared with `eq')."
  242.   (let (val)
  243.     (cond ((not (boundp hook-var))
  244.        nil)
  245.       ((eq function (setq val (symbol-value hook-var)))
  246.        (setq hook-var nil))
  247.       ((consp val)
  248.        ;; don't side-effect the list
  249.        (set hook-var (delq function (copy-sequence val)))))))
  250.  
  251.  
  252. (defun momentary-string-display (string pos &optional exit-char message) 
  253.   "Momentarily display STRING in the buffer at POS.
  254. Display remains until next character is typed.
  255. If the char is EXIT-CHAR (optional third arg, default is SPC) it is swallowed;
  256. otherwise it is then available as input (as a command if nothing else).
  257. Display MESSAGE (optional fourth arg) in the echo area.
  258. If MESSAGE is nil, instructions to type EXIT-CHAR are displayed there."
  259.   (or exit-char (setq exit-char ?\ ))
  260.   (let ((buffer-read-only nil)
  261.     (modified (buffer-modified-p))
  262.     (name buffer-file-name)
  263.     insert-end)
  264.     (unwind-protect
  265.     (progn
  266.       (save-excursion
  267.         (goto-char pos)
  268.         ;; defeat file locking... don't try this at home, kids!
  269.         (setq buffer-file-name nil)
  270.         (insert-before-markers string)
  271.         (setq insert-end (point)))
  272.       (message (or message "Type %s to continue editing.")
  273.            (single-key-description exit-char))
  274.       (let ((event (next-command-event (allocate-event))))
  275.         (or (eq (event-to-character event) exit-char)
  276.         (setq unread-command-event event))))
  277.       (if insert-end
  278.       (save-excursion
  279.         (delete-region pos insert-end)))
  280.       (setq buffer-file-name name)
  281.       (set-buffer-modified-p modified))))
  282.  
  283. (defun start-process-shell-command (name buffer &rest args)
  284.   "Start a program in a subprocess.  Return the process object for it.
  285. Args are NAME BUFFER COMMAND &rest COMMAND-ARGS.
  286. NAME is name for process.  It is modified if necessary to make it unique.
  287. BUFFER is the buffer or (buffer-name) to associate with the process.
  288.  Process output goes at end of that buffer, unless you specify
  289.  an output stream or filter function to handle the output.
  290.  BUFFER may be also nil, meaning that this process is not associated
  291.  with any buffer
  292. Third arg is command name, the name of a shell command.
  293. Remaining arguments are the arguments for the command.
  294. Wildcards and redirection are handle as usual in the shell."
  295.   (if (eq system-type 'vax-vms)
  296.       (apply 'start-process name buffer args)
  297.     (start-process name buffer shell-file-name "-c"
  298.            (concat "exec " (mapconcat 'identity args " ")))))
  299.  
  300. ;;>> What a piece of junk!  This is what hooks are for!!
  301. ;(defun eval-after-load (file form)
  302. ;  "Arrange that, if FILE is ever loaded, FORM will be run at that time.
  303. ;This makes or adds to an entry on `after-load-alist'.
  304. ;FILE should be the name of a library, with no directory name."
  305. ;  (or (assoc file after-load-alist)
  306. ;      (setq after-load-alist (cons (list file) after-load-alist)))
  307. ;  (nconc (assoc file after-load-alist) (list form))
  308. ;  form)
  309. ;
  310. ;(defun eval-next-after-load (file)
  311. ;  "Read the following input sexp, and run it whenever FILE is loaded.
  312. ;This makes or adds to an entry on `after-load-alist'.
  313. ;FILE should be the name of a library, with no directory name."
  314. ;  (eval-after-load file (read)))
  315.  
  316. (defun user-original-login-name ()
  317.   "Return user's login name from original login.
  318. This tries to remain unaffected by `su', by looking in environment variables."
  319.   (or (getenv "LOGNAME") (getenv "USER") (user-login-name)))
  320.  
  321. (defun redraw-mode-line (&optional all)
  322.   "Force the mode-line of the current buffer to be redisplayed.
  323. With optional non-nil ALL then force then force redisplay of all mode-lines."
  324.   (if all (save-excursion (set-buffer (other-buffer))))
  325.   (set-buffer-modified-p (buffer-modified-p)))
  326.  
  327. (fset 'force-mode-line-update 'redraw-mode-line)
  328.  
  329. ;;;; Keymap stuff
  330.  
  331. (defun local-key-binding (keys)
  332.   "Return the binding for command KEYS in current local keymap only.
  333. KEYS is a string, a vector of events, or a vector of key-description lists
  334. as described in the documentation for the `define-key' function.
  335. The binding is probably a symbol with a function definition; see
  336. the documentation for `lookup-key' for more information."
  337.   (let ((map (current-local-map)))
  338.     (if map
  339.         (lookup-key map keys)
  340.         nil)))
  341.  
  342. (defun global-key-binding (keys)
  343.   "Return the binding for command KEYS in current global keymap only.
  344. KEYS is a string or vector of events, a sequence of keystrokes.
  345. The binding is probably a symbol with a function definition; see
  346. the documentation for `lookup-key' for more information."
  347.   (lookup-key (current-global-map) keys))
  348.  
  349.  
  350. (defun global-set-key (keys function)
  351.   "Give KEY a global binding as COMMAND.
  352. COMMAND is a symbol naming an interactively-callable function.
  353. KEYS is a string, a vector of events, or a vector of key-description lists
  354. as described in the documentation for the `define-key' function.
  355. Note that if KEY has a local binding in the current buffer
  356. that local binding will continue to shadow any global binding."
  357.   (interactive "kSet key globally: \nCSet key %s to command: ")
  358.   (define-key (current-global-map) keys function))
  359.  
  360. (defun local-set-key (keys function)
  361.   "Give KEY a local binding as COMMAND.
  362. COMMAND is a symbol naming an interactively-callable function.
  363. KEYS is a string, a vector of events, or a vector of key-description lists
  364. as described in the documentation for the `define-key' function.
  365. The binding goes in the current buffer's local map,
  366. which is shared with other buffers in the same major mode."
  367.   (interactive "kSet key locally: \nCSet key %s locally to command: ")
  368.   (if (null (current-local-map))
  369.       (use-local-map (make-sparse-keymap)))
  370.   (define-key (current-local-map) keys function))
  371.  
  372. (defun global-unset-key (keys)
  373.   "Remove global binding of KEY.
  374. KEYS is a string, a vector of events, or a vector of key-description lists
  375. as described in the documentation for the `define-key' function."
  376.   (interactive "kUnset key globally: ")
  377.   (global-set-key keys nil))
  378.  
  379. (defun local-unset-key (keys)
  380.   "Remove local binding of KEY.
  381. KEYS is a string, a vector of events, or a vector of key-description lists
  382. as described in the documentation for the `define-key' function."
  383.   (interactive "kUnset key locally: ")
  384.   (if (current-local-map)
  385.       (define-key (current-local-map) keys nil)))
  386.  
  387. ;;>>> What a crock
  388. (defun define-prefix-command (name &optional mapvar)
  389.   "Define COMMAND as a prefix command.
  390. A new sparse keymap is stored as COMMAND's function definition.
  391. If second optional argument MAPVAR is not specified,
  392.  COMMAND's value (as well as its function definition) is set to the keymap.
  393. If a second optional argument MAPVAR is given and is not `t',
  394.   the map is stored as its value.
  395. Regardless of MAPVAR, COMMAND's function-value is always set to the keymap."
  396.   (let ((map (make-sparse-keymap)))
  397.     (set-keymap-name map name)
  398.     (fset name map)
  399.     (cond ((not mapvar)
  400.            (set name map))
  401.           ((eq mapvar 't)
  402.            )
  403.           (t
  404.            (set mapvar map)))
  405.     name))
  406.