Apply typo patches from Paul Eggert.
[bpt/emacs.git] / lisp / emacs-lisp / lisp.el
1 ;;; lisp.el --- Lisp editing commands for Emacs
2
3 ;; Copyright (C) 1985, 1986 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6 ;; Keywords: lisp, languages
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Commentary:
25
26 ;; Lisp editing commands to go with Lisp major mode.
27
28 ;;; Code:
29
30 (defvar defun-prompt-regexp nil
31 "Non-nil => regexp to ignore, before the `(' that starts a defun.")
32
33 (defun forward-sexp (&optional arg)
34 "Move forward across one balanced expression (sexp).
35 With argument, do it that many times. Negative arg -N means
36 move backward across N balanced expressions."
37 (interactive "p")
38 (or arg (setq arg 1))
39 (goto-char (or (scan-sexps (point) arg) (buffer-end arg)))
40 (if (< arg 0) (backward-prefix-chars)))
41
42 (defun backward-sexp (&optional arg)
43 "Move backward across one balanced expression (sexp).
44 With argument, do it that many times. Negative arg -N means
45 move forward across N balanced expressions."
46 (interactive "p")
47 (or arg (setq arg 1))
48 (forward-sexp (- arg)))
49
50 (defun mark-sexp (arg)
51 "Set mark ARG sexps from point.
52 The place mark goes is the same place \\[forward-sexp] would
53 move to with the same argument."
54 (interactive "p")
55 (push-mark
56 (save-excursion
57 (forward-sexp arg)
58 (point))
59 nil t))
60
61 (defun forward-list (&optional arg)
62 "Move forward across one balanced group of parentheses.
63 With argument, do it that many times.
64 Negative arg -N means move backward across N groups of parentheses."
65 (interactive "p")
66 (or arg (setq arg 1))
67 (goto-char (or (scan-lists (point) arg 0) (buffer-end arg))))
68
69 (defun backward-list (&optional arg)
70 "Move backward across one balanced group of parentheses.
71 With argument, do it that many times.
72 Negative arg -N means move forward across N groups of parentheses."
73 (interactive "p")
74 (or arg (setq arg 1))
75 (forward-list (- arg)))
76
77 (defun down-list (arg)
78 "Move forward down one level of parentheses.
79 With argument, do this that many times.
80 A negative argument means move backward but still go down a level.
81 In Lisp programs, an argument is required."
82 (interactive "p")
83 (let ((inc (if (> arg 0) 1 -1)))
84 (while (/= arg 0)
85 (goto-char (or (scan-lists (point) inc -1) (buffer-end arg)))
86 (setq arg (- arg inc)))))
87
88 (defun backward-up-list (arg)
89 "Move backward out of one level of parentheses.
90 With argument, do this that many times.
91 A negative argument means move forward but still to a less deep spot.
92 In Lisp programs, an argument is required."
93 (interactive "p")
94 (up-list (- arg)))
95
96 (defun up-list (arg)
97 "Move forward out of one level of parentheses.
98 With argument, do this that many times.
99 A negative argument means move backward but still to a less deep spot.
100 In Lisp programs, an argument is required."
101 (interactive "p")
102 (let ((inc (if (> arg 0) 1 -1)))
103 (while (/= arg 0)
104 (goto-char (or (scan-lists (point) inc 1) (buffer-end arg)))
105 (setq arg (- arg inc)))))
106
107 (defun kill-sexp (arg)
108 "Kill the sexp (balanced expression) following the cursor.
109 With argument, kill that many sexps after the cursor.
110 Negative arg -N means kill N sexps before the cursor."
111 (interactive "p")
112 (let ((opoint (point)))
113 (forward-sexp arg)
114 (kill-region opoint (point))))
115
116 (defun backward-kill-sexp (arg)
117 "Kill the sexp (balanced expression) preceding the cursor.
118 With argument, kill that many sexps before the cursor.
119 Negative arg -N means kill N sexps after the cursor."
120 (interactive "p")
121 (kill-sexp (- arg)))
122 \f
123 (defun beginning-of-defun (&optional arg)
124 "Move backward to the beginning of a defun.
125 With argument, do it that many times. Negative arg -N
126 means move forward to Nth following beginning of defun.
127 Returns t unless search stops due to beginning or end of buffer.
128
129 Normally a defun starts when there is an char with open-parenthesis
130 syntax at the beginning of a line. If `defun-prompt-regexp' is
131 non-nil, then a string which matches that regexp may precede the
132 open-parenthesis."
133 (interactive "p")
134 (and arg (< arg 0) (forward-char 1))
135 (and (re-search-backward (if defun-prompt-regexp
136 (concat "^\\s(\\|"
137 "\\(" defun-prompt-regexp "\\)\\s(")
138 "^\\s(")
139 nil 'move (or arg 1))
140 (progn (beginning-of-line) t)))
141
142 (defun buffer-end (arg)
143 (if (> arg 0) (point-max) (point-min)))
144
145 (defun end-of-defun (&optional arg)
146 "Move forward to next end of defun. With argument, do it that many times.
147 Negative argument -N means move back to Nth preceding end of defun.
148
149 An end of a defun occurs right after the close-parenthesis that matches
150 the open-parenthesis that starts a defun; see `beginning-of-defun'."
151 (interactive "p")
152 (if (or (null arg) (= arg 0)) (setq arg 1))
153 (let ((first t))
154 (while (and (> arg 0) (< (point) (point-max)))
155 (let ((pos (point)) npos)
156 (while (progn
157 (if (and first
158 (progn
159 (forward-char 1)
160 (beginning-of-defun 1)))
161 nil
162 (or (bobp) (forward-char -1))
163 (beginning-of-defun -1))
164 (setq first nil)
165 (forward-list 1)
166 (skip-chars-forward " \t")
167 (if (looking-at "\\s<\\|\n")
168 (forward-line 1))
169 (<= (point) pos))))
170 (setq arg (1- arg)))
171 (while (< arg 0)
172 (let ((pos (point)))
173 (beginning-of-defun 1)
174 (forward-sexp 1)
175 (forward-line 1)
176 (if (>= (point) pos)
177 (if (beginning-of-defun 2)
178 (progn
179 (forward-list 1)
180 (skip-chars-forward " \t")
181 (if (looking-at "[;\n]")
182 (forward-line 1)))
183 (goto-char (point-min)))))
184 (setq arg (1+ arg)))))
185
186 (defun mark-defun ()
187 "Put mark at end of this defun, point at beginning.
188 The defun marked is the one that contains point or follows point."
189 (interactive)
190 (push-mark (point))
191 (end-of-defun)
192 (push-mark (point) nil t)
193 (beginning-of-defun)
194 (re-search-backward "^\n" (- (point) 1) t))
195
196 (defun insert-parentheses (arg)
197 "Put parentheses around next ARG sexps. Leave point after open-paren.
198 No argument is equivalent to zero: just insert () and leave point between."
199 (interactive "P")
200 (if arg (setq arg (prefix-numeric-value arg))
201 (setq arg 0))
202 (or (eq arg 0) (skip-chars-forward " \t"))
203 (and (memq (char-syntax (preceding-char)) '(?w ?_ ?\) ))
204 (insert " "))
205 (insert ?\()
206 (save-excursion
207 (or (eq arg 0) (forward-sexp arg))
208 (insert ?\))
209 (and (memq (char-syntax (following-char)) '(?w ?_ ?\( ))
210 (insert " "))))
211
212 (defun move-past-close-and-reindent ()
213 "Move past next `)', delete indentation before it, then indent after it."
214 (interactive)
215 (up-list 1)
216 (forward-char -1)
217 (while (save-excursion ; this is my contribution
218 (let ((before-paren (point)))
219 (back-to-indentation)
220 (= (point) before-paren)))
221 (delete-indentation))
222 (forward-char 1)
223 (newline-and-indent))
224 \f
225 (defun lisp-complete-symbol ()
226 "Perform completion on Lisp symbol preceding point. That symbol is
227 compared against the symbols that exist and any additional characters
228 determined by what is there are inserted.
229 If the symbol starts just after an open-parenthesis, only symbols
230 with function definitions are considered. Otherwise, all symbols with
231 function definitions, values or properties are considered."
232 (interactive)
233 (let* ((end (point))
234 (buffer-syntax (syntax-table))
235 (beg (unwind-protect
236 (save-excursion
237 (set-syntax-table emacs-lisp-mode-syntax-table)
238 (backward-sexp 1)
239 (while (= (char-syntax (following-char)) ?\')
240 (forward-char 1))
241 (point))
242 (set-syntax-table buffer-syntax)))
243 (pattern (buffer-substring beg end))
244 (predicate
245 (if (eq (char-after (1- beg)) ?\()
246 'fboundp
247 (function (lambda (sym)
248 (or (boundp sym) (fboundp sym)
249 (symbol-plist sym))))))
250 (completion (try-completion pattern obarray predicate)))
251 (cond ((eq completion t))
252 ((null completion)
253 (message "Can't find completion for \"%s\"" pattern)
254 (ding))
255 ((not (string= pattern completion))
256 (delete-region beg end)
257 (insert completion))
258 (t
259 (message "Making completion list...")
260 (let ((list (all-completions pattern obarray predicate)))
261 (or (eq predicate 'fboundp)
262 (let (new)
263 (while list
264 (setq new (cons (if (fboundp (intern (car list)))
265 (list (car list) " <f>")
266 (car list))
267 new))
268 (setq list (cdr list)))
269 (setq list (nreverse new))))
270 (with-output-to-temp-buffer " *Completions*"
271 (display-completion-list list)))
272 (message "Making completion list...%s" "done")))))
273
274 ;;; lisp.el ends here