Sync to HEAD
[bpt/emacs.git] / lisp / emacs-lisp / lisp.el
CommitLineData
6594deb0
ER
1;;; lisp.el --- Lisp editing commands for Emacs
2
6b61353c 3;; Copyright (C) 1985, 86, 1994, 2000, 2004 Free Software Foundation, Inc.
9750e079 4
e5167999 5;; Maintainer: FSF
e9571d2a 6;; Keywords: lisp, languages
e5167999 7
b73b9811 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
e5167999 12;; the Free Software Foundation; either version 2, or (at your option)
b73b9811 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
b578f267
EN
21;; along with GNU Emacs; see the file COPYING. If not, write to the
22;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23;; Boston, MA 02111-1307, USA.
b73b9811 24
e41b2db1
ER
25;;; Commentary:
26
e50c4203
DL
27;; Lisp editing commands to go with Lisp major mode. More-or-less
28;; applicable in other modes too.
e41b2db1 29
e5167999 30;;; Code:
b73b9811 31
7fe78b07 32;; Note that this variable is used by non-lisp modes too.
30e19aee 33(defcustom defun-prompt-regexp nil
e50c4203 34 "*If non-nil, a regexp to ignore before the character that starts a defun.
7fe78b07 35This is only necessary if the opening paren or brace is not in column 0.
c7f18fba
GM
36See function `beginning-of-defun'.
37
38Setting this variable automatically makes it local to the current buffer."
ba6b3a2a
RS
39 :type '(choice (const nil)
40 regexp)
30e19aee 41 :group 'lisp)
24ff5498 42(make-variable-buffer-local 'defun-prompt-regexp)
b73b9811 43
30e19aee 44(defcustom parens-require-spaces t
e50c4203 45 "Non-nil means `insert-parentheses' should insert whitespace as needed."
30e19aee
RS
46 :type 'boolean
47 :group 'lisp)
44a53673 48
11ae6c5d
SM
49(defvar forward-sexp-function nil
50 "If non-nil, `forward-sexp' delegates to this function.
51Should take the same arguments and behave similarly to `forward-sexp'.")
52
b73b9811 53(defun forward-sexp (&optional arg)
54 "Move forward across one balanced expression (sexp).
c6eeec65 55With ARG, do it that many times. Negative arg -N means
e3f99b91 56move backward across N balanced expressions."
b73b9811 57 (interactive "p")
58 (or arg (setq arg 1))
11ae6c5d
SM
59 (if forward-sexp-function
60 (funcall forward-sexp-function arg)
61 (goto-char (or (scan-sexps (point) arg) (buffer-end arg)))
62 (if (< arg 0) (backward-prefix-chars))))
b73b9811 63
64(defun backward-sexp (&optional arg)
65 "Move backward across one balanced expression (sexp).
c6eeec65 66With ARG, do it that many times. Negative arg -N means
e3f99b91 67move forward across N balanced expressions."
b73b9811 68 (interactive "p")
69 (or arg (setq arg 1))
70 (forward-sexp (- arg)))
71
e50c4203 72(defun mark-sexp (&optional arg)
b73b9811 73 "Set mark ARG sexps from point.
e3f99b91 74The place mark goes is the same place \\[forward-sexp] would
f07493e7
SM
75move to with the same argument.
76If this command is repeated, it marks the next ARG sexps after the ones
77already marked."
b73b9811 78 (interactive "p")
cad113ae
KG
79 (cond ((and (eq last-command this-command) (mark t))
80 (set-mark
81 (save-excursion
82 (goto-char (mark))
83 (forward-sexp (or arg 1))
84 (point))))
85 (t
86 (push-mark
87 (save-excursion
88 (forward-sexp (or arg 1))
89 (point))
90 nil t))))
b73b9811 91
92(defun forward-list (&optional arg)
93 "Move forward across one balanced group of parentheses.
c6eeec65 94With ARG, do it that many times.
b73b9811 95Negative arg -N means move backward across N groups of parentheses."
96 (interactive "p")
97 (or arg (setq arg 1))
98 (goto-char (or (scan-lists (point) arg 0) (buffer-end arg))))
99
100(defun backward-list (&optional arg)
101 "Move backward across one balanced group of parentheses.
c6eeec65 102With ARG, do it that many times.
b73b9811 103Negative arg -N means move forward across N groups of parentheses."
104 (interactive "p")
105 (or arg (setq arg 1))
106 (forward-list (- arg)))
107
e50c4203 108(defun down-list (&optional arg)
b73b9811 109 "Move forward down one level of parentheses.
c6eeec65 110With ARG, do this that many times.
b41c9501 111A negative argument means move backward but still go down a level."
b73b9811 112 (interactive "p")
e50c4203 113 (or arg (setq arg 1))
b73b9811 114 (let ((inc (if (> arg 0) 1 -1)))
115 (while (/= arg 0)
116 (goto-char (or (scan-lists (point) inc -1) (buffer-end arg)))
117 (setq arg (- arg inc)))))
118
e50c4203 119(defun backward-up-list (&optional arg)
b73b9811 120 "Move backward out of one level of parentheses.
c6eeec65 121With ARG, do this that many times.
b41c9501 122A negative argument means move forward but still to a less deep spot."
b73b9811 123 (interactive "p")
e50c4203 124 (up-list (- (or arg 1))))
b73b9811 125
e50c4203 126(defun up-list (&optional arg)
b73b9811 127 "Move forward out of one level of parentheses.
c6eeec65 128With ARG, do this that many times.
b41c9501 129A negative argument means move backward but still to a less deep spot."
b73b9811 130 (interactive "p")
e50c4203 131 (or arg (setq arg 1))
b73b9811 132 (let ((inc (if (> arg 0) 1 -1)))
133 (while (/= arg 0)
134 (goto-char (or (scan-lists (point) inc 1) (buffer-end arg)))
135 (setq arg (- arg inc)))))
136
e50c4203 137(defun kill-sexp (&optional arg)
b73b9811 138 "Kill the sexp (balanced expression) following the cursor.
c6eeec65 139With ARG, kill that many sexps after the cursor.
b73b9811 140Negative arg -N means kill N sexps before the cursor."
141 (interactive "p")
142 (let ((opoint (point)))
e50c4203 143 (forward-sexp (or arg 1))
b73b9811 144 (kill-region opoint (point))))
145
e50c4203 146(defun backward-kill-sexp (&optional arg)
b73b9811 147 "Kill the sexp (balanced expression) preceding the cursor.
c6eeec65 148With ARG, kill that many sexps before the cursor.
b73b9811 149Negative arg -N means kill N sexps after the cursor."
150 (interactive "p")
e50c4203 151 (kill-sexp (- (or arg 1))))
b73b9811 152\f
ab22ee53 153(defvar beginning-of-defun-function nil
c6eeec65
DL
154 "If non-nil, function for `beginning-of-defun-raw' to call.
155This is used to find the beginning of the defun instead of using the
ab22ee53
RS
156normal recipe (see `beginning-of-defun'). Major modes can define this
157if defining `defun-prompt-regexp' is not sufficient to handle the mode's
158needs.
c6eeec65 159
6b61353c
KH
160The function (of no args) should go to the line on which the current
161defun starts, and return non-nil, or should return nil if it can't
162find the beginning.")
c6eeec65 163
b73b9811 164(defun beginning-of-defun (&optional arg)
165 "Move backward to the beginning of a defun.
c6eeec65 166With ARG, do it that many times. Negative arg -N
b73b9811 167means move forward to Nth following beginning of defun.
168Returns t unless search stops due to beginning or end of buffer.
169
1269ae51 170Normally a defun starts when there is a char with open-parenthesis
b73b9811 171syntax at the beginning of a line. If `defun-prompt-regexp' is
172non-nil, then a string which matches that regexp may precede the
c6eeec65
DL
173open-parenthesis, and point ends up at the beginning of the line.
174
ab22ee53
RS
175If variable `beginning-of-defun-function' is non-nil, its value
176is called as a function to find the defun's beginning."
afa995e1
KH
177 (interactive "p")
178 (and (beginning-of-defun-raw arg)
179 (progn (beginning-of-line) t)))
180
181(defun beginning-of-defun-raw (&optional arg)
182 "Move point to the character that starts a defun.
c6eeec65
DL
183This is identical to function `beginning-of-defun', except that point
184does not move to the beginning of the line when `defun-prompt-regexp'
185is non-nil.
186
ab22ee53
RS
187If variable `beginning-of-defun-function' is non-nil, its value
188is called as a function to find the defun's beginning."
b73b9811 189 (interactive "p")
ab22ee53 190 (if beginning-of-defun-function
6b61353c
KH
191 (if (> (setq arg (or arg 1)) 0)
192 (dotimes (i arg)
193 (funcall beginning-of-defun-function))
194 ;; Better not call end-of-defun-function directly, in case
195 ;; it's not defined.
196 (end-of-defun (- arg)))
c6eeec65
DL
197 (and arg (< arg 0) (not (eobp)) (forward-char 1))
198 (and (re-search-backward (if defun-prompt-regexp
418d645c
GM
199 (concat (if open-paren-in-column-0-is-defun-start
200 "^\\s(\\|" "")
b5ed9def 201 "\\(?:" defun-prompt-regexp "\\)\\s(")
c6eeec65
DL
202 "^\\s(")
203 nil 'move (or arg 1))
204 (progn (goto-char (1- (match-end 0)))) t)))
205
ab22ee53 206(defvar end-of-defun-function nil
c6eeec65
DL
207 "If non-nil, function for function `end-of-defun' to call.
208This is used to find the end of the defun instead of using the normal
ab22ee53
RS
209recipe (see `end-of-defun'). Major modes can define this if the
210normal method is not appropriate.")
b73b9811 211
212(defun buffer-end (arg)
213 (if (> arg 0) (point-max) (point-min)))
214
215(defun end-of-defun (&optional arg)
216 "Move forward to next end of defun. With argument, do it that many times.
217Negative argument -N means move back to Nth preceding end of defun.
218
c6eeec65
DL
219An end of a defun occurs right after the close-parenthesis that
220matches the open-parenthesis that starts a defun; see function
ab22ee53
RS
221`beginning-of-defun'.
222
223If variable `end-of-defun-function' is non-nil, its value
224is called as a function to find the defun's end."
b73b9811 225 (interactive "p")
6b61353c 226 (if (or (null arg) (= arg 0)) (setq arg 1))
ab22ee53 227 (if end-of-defun-function
6b61353c
KH
228 (if (> arg 0)
229 (dotimes (i arg)
230 (funcall end-of-defun-function))
231 ;; Better not call beginning-of-defun-function
232 ;; directly, in case it's not defined.
233 (beginning-of-defun (- arg)))
c6eeec65
DL
234 (let ((first t))
235 (while (and (> arg 0) (< (point) (point-max)))
6b61353c 236 (let ((pos (point)))
c6eeec65
DL
237 (while (progn
238 (if (and first
239 (progn
240 (end-of-line 1)
241 (beginning-of-defun-raw 1)))
242 nil
243 (or (bobp) (forward-char -1))
244 (beginning-of-defun-raw -1))
245 (setq first nil)
246 (forward-list 1)
247 (skip-chars-forward " \t")
248 (if (looking-at "\\s<\\|\n")
249 (forward-line 1))
250 (<= (point) pos))))
251 (setq arg (1- arg)))
252 (while (< arg 0)
253 (let ((pos (point)))
254 (beginning-of-defun-raw 1)
255 (forward-sexp 1)
256 (forward-line 1)
257 (if (>= (point) pos)
258 (if (beginning-of-defun-raw 2)
259 (progn
260 (forward-list 1)
261 (skip-chars-forward " \t")
262 (if (looking-at "\\s<\\|\n")
263 (forward-line 1)))
264 (goto-char (point-min)))))
265 (setq arg (1+ arg))))))
b73b9811 266
267(defun mark-defun ()
268 "Put mark at end of this defun, point at beginning.
cad113ae
KG
269The defun marked is the one that contains point or follows point.
270If this command is repeated, marks more defuns after the ones
271already marked."
b73b9811 272 (interactive)
be0d25b6
KG
273 (cond ((and (eq last-command this-command) (mark t))
274 (set-mark
275 (save-excursion
276 (goto-char (mark))
277 (end-of-defun)
278 (point))))
279 (t
6b61353c
KH
280 ;; Do it in this order for the sake of languages with nested
281 ;; functions where several can end at the same place as with
282 ;; the offside rule, e.g. Python.
be0d25b6 283 (push-mark (point))
be0d25b6 284 (beginning-of-defun)
6b61353c
KH
285 (push-mark (point) nil t)
286 (end-of-defun)
287 (exchange-point-and-mark)
be0d25b6 288 (re-search-backward "^\n" (- (point) 1) t))))
b73b9811 289
3f7e952d
RS
290(defun narrow-to-defun (&optional arg)
291 "Make text outside current defun invisible.
c6eeec65
DL
292The defun visible is the one that contains point or follows point.
293Optional ARG is ignored."
3f7e952d
RS
294 (interactive)
295 (save-excursion
296 (widen)
6b61353c
KH
297 ;; Do it in this order for the sake of languages with nested
298 ;; functions where several can end at the same place as with the
299 ;; offside rule, e.g. Python.
300 (beginning-of-defun)
301 (let ((beg (point)))
302 (end-of-defun)
303 (narrow-to-region beg (point)))))
3f7e952d 304
b73b9811 305(defun insert-parentheses (arg)
e3ac26cb
EN
306 "Enclose following ARG sexps in parentheses. Leave point after open-paren.
307A negative ARG encloses the preceding ARG sexps instead.
44a53673 308No argument is equivalent to zero: just insert `()' and leave point between.
d5bfe076
KH
309If `parens-require-spaces' is non-nil, this command also inserts a space
310before and after, depending on the surrounding characters."
b73b9811 311 (interactive "P")
a17f9e55
RS
312 (if arg (setq arg (prefix-numeric-value arg))
313 (setq arg 0))
e3ac26cb
EN
314 (cond ((> arg 0) (skip-chars-forward " \t"))
315 ((< arg 0) (forward-sexp arg) (setq arg (- arg))))
78e367e9 316 (and parens-require-spaces
6696af65 317 (not (bobp))
44a53673 318 (memq (char-syntax (preceding-char)) '(?w ?_ ?\) ))
b73b9811 319 (insert " "))
b73b9811 320 (insert ?\()
321 (save-excursion
a17f9e55 322 (or (eq arg 0) (forward-sexp arg))
b73b9811 323 (insert ?\))
78e367e9 324 (and parens-require-spaces
6696af65 325 (not (eobp))
44a53673 326 (memq (char-syntax (following-char)) '(?w ?_ ?\( ))
a17f9e55 327 (insert " "))))
b73b9811 328
329(defun move-past-close-and-reindent ()
330 "Move past next `)', delete indentation before it, then indent after it."
331 (interactive)
332 (up-list 1)
333 (forward-char -1)
334 (while (save-excursion ; this is my contribution
335 (let ((before-paren (point)))
336 (back-to-indentation)
adce3b5f
RS
337 (and (= (point) before-paren)
338 (progn
339 ;; Move to end of previous line.
340 (beginning-of-line)
341 (forward-char -1)
342 ;; Verify it doesn't end within a string or comment.
343 (let ((end (point))
344 state)
345 (beginning-of-line)
346 ;; Get state at start of line.
c6eeec65 347 (setq state (list 0 nil nil
adce3b5f
RS
348 (null (calculate-lisp-indent))
349 nil nil nil nil
350 nil))
351 ;; Parse state across the line to get state at end.
352 (setq state (parse-partial-sexp (point) end nil nil
353 state))
354 ;; Check not in string or comment.
355 (and (not (elt state 3)) (not (elt state 4))))))))
b73b9811 356 (delete-indentation))
357 (forward-char 1)
358 (newline-and-indent))
c6eeec65
DL
359
360(defun check-parens () ; lame name?
361 "Check for unbalanced parentheses in the current buffer.
362More accurately, check the narrowed part of the buffer for unbalanced
363expressions (\"sexps\") in general. This is done according to the
364current syntax table and will find unbalanced brackets or quotes as
365appropriate. (See Info node `(emacs)Lists and Sexps'.) If imbalance
366is found, an error is signalled and point is left at the first
367unbalanced character."
368 (interactive)
369 (condition-case data
370 ;; Buffer can't have more than (point-max) sexps.
371 (scan-sexps (point-min) (point-max))
372 (scan-error (goto-char (nth 2 data))
373 ;; Could print (nth 1 data), which is either
374 ;; "Containing expression ends prematurely" or
375 ;; "Unbalanced parentheses", but those may not be so
376 ;; accurate/helpful, e.g. quotes may actually be
377 ;; mismatched.
378 (error "Unmatched bracket or quote"))
379 (error (cond ((eq 'scan-error (car data))
380 (goto-char (nth 2 data))
381 (error "Unmatched bracket or quote"))
382 (t (signal (car data) (cdr data)))))))
b73b9811 383\f
e50c4203 384(defun lisp-complete-symbol (&optional predicate)
2eb9adab
RS
385 "Perform completion on Lisp symbol preceding point.
386Compare that symbol against the known Lisp symbols.
1fa1cb1b
RS
387If no characters can be completed, display a list of possible completions.
388Repeating the command at that point scrolls the list.
2eb9adab 389
e50c4203
DL
390When called from a program, optional arg PREDICATE is a predicate
391determining which symbols are considered, e.g. `commandp'.
392If PREDICATE is nil, the context determines which symbols are
393considered. If the symbol starts just after an open-parenthesis, only
394symbols with function definitions are considered. Otherwise, all
395symbols with function definitions, values or properties are
396considered."
b73b9811 397 (interactive)
1fa1cb1b
RS
398
399 (let ((window (get-buffer-window "*Completions*")))
400 (if (and (eq last-command this-command)
401 window (window-live-p window) (window-buffer window)
402 (buffer-name (window-buffer window)))
403 ;; If this command was repeated, and
404 ;; there's a fresh completion window with a live buffer,
405 ;; and this command is repeated, scroll that window.
406 (with-current-buffer (window-buffer window)
407 (if (pos-visible-in-window-p (point-max) window)
408 (set-window-start window (point-min))
409 (save-selected-window
410 (select-window window)
411 (scroll-up))))
412
413 ;; Do completion.
414 (let* ((end (point))
415 (beg (with-syntax-table emacs-lisp-mode-syntax-table
416 (save-excursion
417 (backward-sexp 1)
418 (while (= (char-syntax (following-char)) ?\')
419 (forward-char 1))
420 (point))))
421 (pattern (buffer-substring-no-properties beg end))
422 (predicate
423 (or predicate
424 (save-excursion
425 (goto-char beg)
426 (if (not (eq (char-before) ?\())
427 (lambda (sym) ;why not just nil ? -sm
428 (or (boundp sym) (fboundp sym)
429 (symbol-plist sym)))
430 ;; Looks like a funcall position. Let's double check.
431 (if (condition-case nil
432 (progn (up-list -2) (forward-char 1)
433 (eq (char-after) ?\())
434 (error nil))
435 ;; If the first element of the parent list is an open
436 ;; parenthesis we are probably not in a funcall position.
437 ;; Maybe a `let' varlist or something.
438 nil
439 ;; Else, we assume that a function name is expected.
440 'fboundp)))))
441 (completion (try-completion pattern obarray predicate)))
442 (cond ((eq completion t))
443 ((null completion)
444 (message "Can't find completion for \"%s\"" pattern)
445 (ding))
446 ((not (string= pattern completion))
447 (delete-region beg end)
448 (insert completion))
449 (t
450 (message "Making completion list...")
451 (let ((list (all-completions pattern obarray predicate)))
452 (setq list (sort list 'string<))
453 (or (eq predicate 'fboundp)
454 (let (new)
455 (while list
456 (setq new (cons (if (fboundp (intern (car list)))
457 (list (car list) " <f>")
458 (car list))
459 new))
460 (setq list (cdr list)))
461 (setq list (nreverse new))))
462 (with-output-to-temp-buffer "*Completions*"
463 (display-completion-list list)))
464 (message "Making completion list...%s" "done")))))))
6594deb0 465
6b61353c 466;;; arch-tag: aa7fa8a4-2e6f-4e9b-9cd9-fef06340e67e
6594deb0 467;;; lisp.el ends here