Add 2011 to FSF/AIST copyright years.
[bpt/emacs.git] / lisp / emacs-lisp / lisp.el
CommitLineData
6594deb0
ER
1;;; lisp.el --- Lisp editing commands for Emacs
2
d59c3137 3;; Copyright (C) 1985, 1986, 1994, 2000, 2001, 2002, 2003, 2004,
5df4f04c 4;; 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
9750e079 5
e5167999 6;; Maintainer: FSF
e9571d2a 7;; Keywords: lisp, languages
e5167999 8
b73b9811 9;; This file is part of GNU Emacs.
10
d6cba7ae 11;; GNU Emacs is free software: you can redistribute it and/or modify
b73b9811 12;; it under the terms of the GNU General Public License as published by
d6cba7ae
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
b73b9811 15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
d6cba7ae 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
b73b9811 23
e41b2db1
ER
24;;; Commentary:
25
e50c4203
DL
26;; Lisp editing commands to go with Lisp major mode. More-or-less
27;; applicable in other modes too.
e41b2db1 28
e5167999 29;;; Code:
b73b9811 30
7fe78b07 31;; Note that this variable is used by non-lisp modes too.
30e19aee 32(defcustom defun-prompt-regexp nil
cb711556 33 "If non-nil, a regexp to ignore before a defun.
7fe78b07 34This is only necessary if the opening paren or brace is not in column 0.
a64dc1a4 35See function `beginning-of-defun'."
ba6b3a2a
RS
36 :type '(choice (const nil)
37 regexp)
30e19aee 38 :group 'lisp)
24ff5498 39(make-variable-buffer-local 'defun-prompt-regexp)
b73b9811 40
30e19aee 41(defcustom parens-require-spaces t
becc6788
CY
42 "If non-nil, add whitespace as needed when inserting parentheses.
43This affects `insert-parentheses' and `insert-pair'."
30e19aee
RS
44 :type 'boolean
45 :group 'lisp)
44a53673 46
11ae6c5d
SM
47(defvar forward-sexp-function nil
48 "If non-nil, `forward-sexp' delegates to this function.
49Should take the same arguments and behave similarly to `forward-sexp'.")
50
b73b9811 51(defun forward-sexp (&optional arg)
52 "Move forward across one balanced expression (sexp).
c6eeec65 53With ARG, do it that many times. Negative arg -N means
8fc2ac41
AM
54move backward across N balanced expressions.
55This command assumes point is not in a string or comment."
61eee794 56 (interactive "^p")
b73b9811 57 (or arg (setq arg 1))
11ae6c5d
SM
58 (if forward-sexp-function
59 (funcall forward-sexp-function arg)
60 (goto-char (or (scan-sexps (point) arg) (buffer-end arg)))
61 (if (< arg 0) (backward-prefix-chars))))
b73b9811 62
63(defun backward-sexp (&optional arg)
64 "Move backward across one balanced expression (sexp).
c6eeec65 65With ARG, do it that many times. Negative arg -N means
8fc2ac41
AM
66move forward across N balanced expressions.
67This command assumes point is not in a string or comment."
61eee794 68 (interactive "^p")
b73b9811 69 (or arg (setq arg 1))
70 (forward-sexp (- arg)))
71
afb62fdd 72(defun mark-sexp (&optional arg allow-extend)
b73b9811 73 "Set mark ARG sexps from point.
e3f99b91 74The place mark goes is the same place \\[forward-sexp] would
f07493e7 75move to with the same argument.
afb62fdd 76Interactively, if this command is repeated
a64dc1a4 77or (in Transient Mark mode) if the mark is active,
8fc2ac41
AM
78it marks the next ARG sexps after the ones already marked.
79This command assumes point is not in a string or comment."
afb62fdd
RS
80 (interactive "P\np")
81 (cond ((and allow-extend
82 (or (and (eq last-command this-command) (mark t))
83 (and transient-mark-mode mark-active)))
3610d3c9 84 (setq arg (if arg (prefix-numeric-value arg)
967e1a52 85 (if (< (mark) (point)) -1 1)))
cad113ae
KG
86 (set-mark
87 (save-excursion
967e1a52
JL
88 (goto-char (mark))
89 (forward-sexp arg)
90 (point))))
cad113ae
KG
91 (t
92 (push-mark
93 (save-excursion
3610d3c9 94 (forward-sexp (prefix-numeric-value arg))
cad113ae
KG
95 (point))
96 nil t))))
b73b9811 97
98(defun forward-list (&optional arg)
99 "Move forward across one balanced group of parentheses.
c6eeec65 100With ARG, do it that many times.
8fc2ac41
AM
101Negative arg -N means move backward across N groups of parentheses.
102This command assumes point is not in a string or comment."
61eee794 103 (interactive "^p")
b73b9811 104 (or arg (setq arg 1))
105 (goto-char (or (scan-lists (point) arg 0) (buffer-end arg))))
106
107(defun backward-list (&optional arg)
108 "Move backward across one balanced group of parentheses.
c6eeec65 109With ARG, do it that many times.
8fc2ac41
AM
110Negative arg -N means move forward across N groups of parentheses.
111This command assumes point is not in a string or comment."
61eee794 112 (interactive "^p")
b73b9811 113 (or arg (setq arg 1))
114 (forward-list (- arg)))
115
e50c4203 116(defun down-list (&optional arg)
b73b9811 117 "Move forward down one level of parentheses.
c6eeec65 118With ARG, do this that many times.
8fc2ac41
AM
119A negative argument means move backward but still go down a level.
120This command assumes point is not in a string or comment."
61eee794 121 (interactive "^p")
e50c4203 122 (or arg (setq arg 1))
b73b9811 123 (let ((inc (if (> arg 0) 1 -1)))
124 (while (/= arg 0)
125 (goto-char (or (scan-lists (point) inc -1) (buffer-end arg)))
126 (setq arg (- arg inc)))))
127
e50c4203 128(defun backward-up-list (&optional arg)
b73b9811 129 "Move backward out of one level of parentheses.
c6eeec65 130With ARG, do this that many times.
8fc2ac41
AM
131A negative argument means move forward but still to a less deep spot.
132This command assumes point is not in a string or comment."
61eee794 133 (interactive "^p")
e50c4203 134 (up-list (- (or arg 1))))
b73b9811 135
e50c4203 136(defun up-list (&optional arg)
b73b9811 137 "Move forward out of one level of parentheses.
c6eeec65 138With ARG, do this that many times.
8fc2ac41
AM
139A negative argument means move backward but still to a less deep spot.
140This command assumes point is not in a string or comment."
61eee794 141 (interactive "^p")
e50c4203 142 (or arg (setq arg 1))
b73b9811 143 (let ((inc (if (> arg 0) 1 -1)))
144 (while (/= arg 0)
145 (goto-char (or (scan-lists (point) inc 1) (buffer-end arg)))
146 (setq arg (- arg inc)))))
147
e50c4203 148(defun kill-sexp (&optional arg)
dd60a465
RS
149 "Kill the sexp (balanced expression) following point.
150With ARG, kill that many sexps after point.
8fc2ac41
AM
151Negative arg -N means kill N sexps before point.
152This command assumes point is not in a string or comment."
b73b9811 153 (interactive "p")
154 (let ((opoint (point)))
e50c4203 155 (forward-sexp (or arg 1))
b73b9811 156 (kill-region opoint (point))))
157
e50c4203 158(defun backward-kill-sexp (&optional arg)
dd60a465
RS
159 "Kill the sexp (balanced expression) preceding point.
160With ARG, kill that many sexps before point.
8fc2ac41
AM
161Negative arg -N means kill N sexps after point.
162This command assumes point is not in a string or comment."
b73b9811 163 (interactive "p")
e50c4203 164 (kill-sexp (- (or arg 1))))
de6d64b2
EZ
165
166;; After Zmacs:
167(defun kill-backward-up-list (&optional arg)
168 "Kill the form containing the current sexp, leaving the sexp itself.
169A prefix argument ARG causes the relevant number of surrounding
8fc2ac41
AM
170forms to be removed.
171This command assumes point is not in a string or comment."
de6d64b2
EZ
172 (interactive "*p")
173 (let ((current-sexp (thing-at-point 'sexp)))
174 (if current-sexp
175 (save-excursion
176 (backward-up-list arg)
177 (kill-sexp)
178 (insert current-sexp))
179 (error "Not at a sexp"))))
b73b9811 180\f
ab22ee53 181(defvar beginning-of-defun-function nil
c6eeec65
DL
182 "If non-nil, function for `beginning-of-defun-raw' to call.
183This is used to find the beginning of the defun instead of using the
ab22ee53
RS
184normal recipe (see `beginning-of-defun'). Major modes can define this
185if defining `defun-prompt-regexp' is not sufficient to handle the mode's
186needs.
c6eeec65 187
50bfa18a
SM
188The function takes the same argument as `beginning-of-defun' and should
189behave similarly, returning non-nil if it found the beginning of a defun.
190Ideally it should move to a point right before an open-paren which encloses
191the body of the defun.")
c6eeec65 192
b73b9811 193(defun beginning-of-defun (&optional arg)
194 "Move backward to the beginning of a defun.
6957495d
CY
195With ARG, do it that many times. Negative ARG means move forward
196to the ARGth following beginning of defun.
197
198If search is successful, return t; point ends up at the beginning
199of the line where the search succeeded. Otherwise, return nil.
200
201When `open-paren-in-column-0-is-defun-start' is non-nil, a defun
202is assumed to start where there is a char with open-parenthesis
203syntax at the beginning of a line. If `defun-prompt-regexp' is
204non-nil, then a string which matches that regexp may also precede
205the open-parenthesis. If `defun-prompt-regexp' and
206`open-paren-in-column-0-is-defun-start' are both nil, this
207function instead finds an open-paren at the outermost level.
208
209If the variable `beginning-of-defun-function' is non-nil, its
210value is called as a function, with argument ARG, to find the
211defun's beginning.
212
213Regardless of the values of `defun-prompt-regexp' and
214`beginning-of-defun-function', point always moves to the
215beginning of the line whenever the search is successful."
61eee794 216 (interactive "^p")
90c08845 217 (or (not (eq this-command 'beginning-of-defun))
967e1a52
JL
218 (eq last-command 'beginning-of-defun)
219 (and transient-mark-mode mark-active)
220 (push-mark))
afa995e1
KH
221 (and (beginning-of-defun-raw arg)
222 (progn (beginning-of-line) t)))
223
224(defun beginning-of-defun-raw (&optional arg)
225 "Move point to the character that starts a defun.
c6eeec65
DL
226This is identical to function `beginning-of-defun', except that point
227does not move to the beginning of the line when `defun-prompt-regexp'
228is non-nil.
229
ab22ee53
RS
230If variable `beginning-of-defun-function' is non-nil, its value
231is called as a function to find the defun's beginning."
61eee794 232 (interactive "^p") ; change this to "P", maybe, if we ever come to pass ARG
4df5698c
CY
233 ; to beginning-of-defun-function.
234 (unless arg (setq arg 1))
6cb54822
AM
235 (cond
236 (beginning-of-defun-function
50bfa18a
SM
237 (condition-case nil
238 (funcall beginning-of-defun-function arg)
239 ;; We used to define beginning-of-defun-function as taking no argument
240 ;; but that makes it impossible to implement correct forward motion:
241 ;; we used to use end-of-defun for that, but it's not supposed to do
242 ;; the same thing (it moves to the end of a defun not to the beginning
243 ;; of the next).
244 ;; In case the beginning-of-defun-function uses the old calling
245 ;; convention, fallback on the old implementation.
246 (wrong-number-of-arguments
247 (if (> arg 0)
248 (dotimes (i arg)
249 (funcall beginning-of-defun-function))
250 ;; Better not call end-of-defun-function directly, in case
251 ;; it's not defined.
252 (end-of-defun (- arg))))))
6cb54822
AM
253
254 ((or defun-prompt-regexp open-paren-in-column-0-is-defun-start)
255 (and (< arg 0) (not (eobp)) (forward-char 1))
c6eeec65 256 (and (re-search-backward (if defun-prompt-regexp
418d645c
GM
257 (concat (if open-paren-in-column-0-is-defun-start
258 "^\\s(\\|" "")
b5ed9def 259 "\\(?:" defun-prompt-regexp "\\)\\s(")
c6eeec65 260 "^\\s(")
6cb54822 261 nil 'move arg)
4114bc49
SM
262 (progn (goto-char (1- (match-end 0)))
263 t)))
6cb54822 264
4df5698c
CY
265 ;; If open-paren-in-column-0-is-defun-start and defun-prompt-regexp
266 ;; are both nil, column 0 has no significance - so scan forward
267 ;; from BOB to see how nested point is, then carry on from there.
268 ;;
269 ;; It is generally not a good idea to land up here, because the
270 ;; call to scan-lists below can be extremely slow. This is because
271 ;; back_comment in syntax.c may have to scan from bob to find the
272 ;; beginning of each comment. Fixing this is not trivial -- cyd.
273
274 ((eq arg 0))
6cb54822 275 (t
4df5698c
CY
276 (let ((floor (point-min))
277 (ceiling (point-max))
278 (arg-+ve (> arg 0)))
6cb54822
AM
279 (save-restriction
280 (widen)
4df5698c
CY
281 (let ((ppss (let (syntax-begin-function
282 font-lock-beginning-of-syntax-function)
283 (syntax-ppss)))
284 ;; position of least enclosing paren, or nil.
285 encl-pos)
286 ;; Back out of any comment/string, so that encl-pos will always
287 ;; become nil if we're at top-level.
288 (when (nth 8 ppss)
289 (goto-char (nth 8 ppss))
290 (setq ppss (syntax-ppss))) ; should be fast, due to cache.
291 (setq encl-pos (syntax-ppss-toplevel-pos ppss))
292 (if encl-pos (goto-char encl-pos))
293
294 (and encl-pos arg-+ve (setq arg (1- arg)))
295 (and (not encl-pos) (not arg-+ve) (not (looking-at "\\s("))
296 (setq arg (1+ arg)))
297
298 (condition-case nil ; to catch crazy parens.
299 (progn
300 (goto-char (scan-lists (point) (- arg) 0))
301 (if arg-+ve
302 (if (>= (point) floor)
303 t
304 (goto-char floor)
305 nil)
306 ;; forward to next (, or trigger the c-c
307 (goto-char (1- (scan-lists (point) 1 -1)))
308 (if (<= (point) ceiling)
309 t
310 (goto-char ceiling)
311 nil)))
312 (error
313 (goto-char (if arg-+ve floor ceiling))
314 nil))))))))
c6eeec65 315
7bbab3e0
SM
316(defvar end-of-defun-function
317 (lambda () (forward-sexp 1))
61e21607 318 "Function for `end-of-defun' to call.
6d3e4d22 319This is used to find the end of the defun at point.
61e21607 320It is called with no argument, right after calling `beginning-of-defun-raw'.
6d3e4d22
SM
321So the function can assume that point is at the beginning of the defun body.
322It should move point to the first position after the defun.")
b73b9811 323
324(defun buffer-end (arg)
a64dc1a4 325 "Return the \"far end\" position of the buffer, in direction ARG.
03deb635
RS
326If ARG is positive, that's the end of the buffer.
327Otherwise, that's the beginning of the buffer."
b73b9811 328 (if (> arg 0) (point-max) (point-min)))
329
330(defun end-of-defun (&optional arg)
a64dc1a4
LT
331 "Move forward to next end of defun.
332With argument, do it that many times.
b73b9811 333Negative argument -N means move back to Nth preceding end of defun.
334
c6eeec65
DL
335An end of a defun occurs right after the close-parenthesis that
336matches the open-parenthesis that starts a defun; see function
ab22ee53
RS
337`beginning-of-defun'.
338
339If variable `end-of-defun-function' is non-nil, its value
340is called as a function to find the defun's end."
61eee794 341 (interactive "^p")
90c08845 342 (or (not (eq this-command 'end-of-defun))
967e1a52
JL
343 (eq last-command 'end-of-defun)
344 (and transient-mark-mode mark-active)
345 (push-mark))
44b254cc 346 (if (or (null arg) (= arg 0)) (setq arg 1))
f9f34ece
SM
347 (let ((pos (point))
348 (beg (progn (end-of-line 1) (beginning-of-defun-raw 1) (point))))
349 (funcall end-of-defun-function)
f2a8252a
SM
350 ;; When comparing point against pos, we want to consider that if
351 ;; point was right after the end of the function, it's still
352 ;; considered as "in that function".
353 ;; E.g. `eval-defun' from right after the last close-paren.
354 (unless (bolp)
355 (skip-chars-forward " \t")
356 (if (looking-at "\\s<\\|\n")
357 (forward-line 1)))
f9f34ece
SM
358 (cond
359 ((> arg 0)
360 ;; Moving forward.
361 (if (> (point) pos)
362 ;; We already moved forward by one because we started from
363 ;; within a function.
364 (setq arg (1- arg))
365 ;; We started from after the end of the previous function.
366 (goto-char pos))
367 (unless (zerop arg)
368 (beginning-of-defun-raw (- arg))
369 (funcall end-of-defun-function)))
370 ((< arg 0)
371 ;; Moving backward.
372 (if (< (point) pos)
373 ;; We already moved backward because we started from between
374 ;; two functions.
375 (setq arg (1+ arg))
376 ;; We started from inside a function.
377 (goto-char beg))
378 (unless (zerop arg)
379 (beginning-of-defun-raw (- arg))
380 (funcall end-of-defun-function))))
381 (unless (bolp)
382 (skip-chars-forward " \t")
383 (if (looking-at "\\s<\\|\n")
384 (forward-line 1)))))
b73b9811 385
afb62fdd 386(defun mark-defun (&optional allow-extend)
b73b9811 387 "Put mark at end of this defun, point at beginning.
cad113ae 388The defun marked is the one that contains point or follows point.
afb62fdd
RS
389
390Interactively, if this command is repeated
a64dc1a4 391or (in Transient Mark mode) if the mark is active,
afb62fdd
RS
392it marks the next defun after the ones already marked."
393 (interactive "p")
394 (cond ((and allow-extend
395 (or (and (eq last-command this-command) (mark t))
396 (and transient-mark-mode mark-active)))
be0d25b6
KG
397 (set-mark
398 (save-excursion
399 (goto-char (mark))
400 (end-of-defun)
401 (point))))
402 (t
d3d6bc9b
RS
403 (let ((opoint (point))
404 beg end)
405 (push-mark opoint)
406 ;; Try first in this order for the sake of languages with nested
407 ;; functions where several can end at the same place as with
408 ;; the offside rule, e.g. Python.
409 (beginning-of-defun)
410 (setq beg (point))
411 (end-of-defun)
412 (setq end (point))
413 (while (looking-at "^\n")
414 (forward-line 1))
415 (if (> (point) opoint)
416 (progn
417 ;; We got the right defun.
418 (push-mark beg nil t)
419 (goto-char end)
420 (exchange-point-and-mark))
421 ;; beginning-of-defun moved back one defun
422 ;; so we got the wrong one.
423 (goto-char opoint)
424 (end-of-defun)
425 (push-mark (point) nil t)
426 (beginning-of-defun))
427 (re-search-backward "^\n" (- (point) 1) t)))))
b73b9811 428
3f7e952d
RS
429(defun narrow-to-defun (&optional arg)
430 "Make text outside current defun invisible.
c6eeec65
DL
431The defun visible is the one that contains point or follows point.
432Optional ARG is ignored."
3f7e952d
RS
433 (interactive)
434 (save-excursion
435 (widen)
d3d6bc9b
RS
436 (let ((opoint (point))
437 beg end)
438 ;; Try first in this order for the sake of languages with nested
439 ;; functions where several can end at the same place as with
440 ;; the offside rule, e.g. Python.
441 (beginning-of-defun)
442 (setq beg (point))
750e563f 443 (end-of-defun)
d3d6bc9b
RS
444 (setq end (point))
445 (while (looking-at "^\n")
446 (forward-line 1))
447 (unless (> (point) opoint)
448 ;; beginning-of-defun moved back one defun
449 ;; so we got the wrong one.
450 (goto-char opoint)
451 (end-of-defun)
452 (setq end (point))
453 (beginning-of-defun)
454 (setq beg (point)))
455 (goto-char end)
456 (re-search-backward "^\n" (- (point) 1) t)
457 (narrow-to-region beg end))))
3f7e952d 458
d97c8198
JL
459(defvar insert-pair-alist
460 '((?\( ?\)) (?\[ ?\]) (?\{ ?\}) (?\< ?\>) (?\" ?\") (?\' ?\') (?\` ?\'))
461 "Alist of paired characters inserted by `insert-pair'.
462Each element looks like (OPEN-CHAR CLOSE-CHAR) or (COMMAND-CHAR
463OPEN-CHAR CLOSE-CHAR). The characters OPEN-CHAR and CLOSE-CHAR
464of the pair whose key is equal to the last input character with
465or without modifiers, are inserted by `insert-pair'.")
466
467(defun insert-pair (&optional arg open close)
5891bf24
JL
468 "Enclose following ARG sexps in a pair of OPEN and CLOSE characters.
469Leave point after the first character.
e3ac26cb 470A negative ARG encloses the preceding ARG sexps instead.
5891bf24
JL
471No argument is equivalent to zero: just insert characters
472and leave point between.
d5bfe076 473If `parens-require-spaces' is non-nil, this command also inserts a space
5891bf24 474before and after, depending on the surrounding characters.
d97c8198
JL
475If region is active, insert enclosing characters at region boundaries.
476
477If arguments OPEN and CLOSE are nil, the character pair is found
478from the variable `insert-pair-alist' according to the last input
479character with or without modifiers. If no character pair is
480found in the variable `insert-pair-alist', then the last input
8fc2ac41
AM
481character is inserted ARG times.
482
483This command assumes point is not in a string or comment."
b73b9811 484 (interactive "P")
d97c8198 485 (if (not (and open close))
61a846fb 486 (let ((pair (or (assq last-command-event insert-pair-alist)
d97c8198
JL
487 (assq (event-basic-type last-command-event)
488 insert-pair-alist))))
489 (if pair
490 (if (nth 2 pair)
491 (setq open (nth 1 pair) close (nth 2 pair))
492 (setq open (nth 0 pair) close (nth 1 pair))))))
493 (if (and open close)
494 (if (and transient-mark-mode mark-active)
495 (progn
496 (save-excursion (goto-char (region-end)) (insert close))
497 (save-excursion (goto-char (region-beginning)) (insert open)))
498 (if arg (setq arg (prefix-numeric-value arg))
499 (setq arg 0))
500 (cond ((> arg 0) (skip-chars-forward " \t"))
501 ((< arg 0) (forward-sexp arg) (setq arg (- arg))))
502 (and parens-require-spaces
503 (not (bobp))
504 (memq (char-syntax (preceding-char)) (list ?w ?_ (char-syntax close)))
505 (insert " "))
506 (insert open)
507 (save-excursion
508 (or (eq arg 0) (forward-sexp arg))
509 (insert close)
510 (and parens-require-spaces
511 (not (eobp))
512 (memq (char-syntax (following-char)) (list ?w ?_ (char-syntax open)))
513 (insert " "))))
514 (insert-char (event-basic-type last-command-event)
515 (prefix-numeric-value arg))))
516
517(defun insert-parentheses (&optional arg)
a64dc1a4
LT
518 "Enclose following ARG sexps in parentheses.
519Leave point after open-paren.
5891bf24
JL
520A negative ARG encloses the preceding ARG sexps instead.
521No argument is equivalent to zero: just insert `()' and leave point between.
522If `parens-require-spaces' is non-nil, this command also inserts a space
523before and after, depending on the surrounding characters.
8fc2ac41
AM
524If region is active, insert enclosing characters at region boundaries.
525
526This command assumes point is not in a string or comment."
5891bf24
JL
527 (interactive "P")
528 (insert-pair arg ?\( ?\)))
b73b9811 529
d97c8198
JL
530(defun delete-pair ()
531 "Delete a pair of characters enclosing the sexp that follows point."
532 (interactive)
533 (save-excursion (forward-sexp 1) (delete-char -1))
534 (delete-char 1))
535
536(defun raise-sexp (&optional arg)
537 "Raise ARG sexps higher up the tree."
538 (interactive "p")
539 (let ((s (if (and transient-mark-mode mark-active)
540 (buffer-substring (region-beginning) (region-end))
541 (buffer-substring
542 (point)
543 (save-excursion (forward-sexp arg) (point))))))
544 (backward-up-list 1)
545 (delete-region (point) (save-excursion (forward-sexp 1) (point)))
546 (save-excursion (insert s))))
547
b73b9811 548(defun move-past-close-and-reindent ()
549 "Move past next `)', delete indentation before it, then indent after it."
550 (interactive)
551 (up-list 1)
552 (forward-char -1)
553 (while (save-excursion ; this is my contribution
554 (let ((before-paren (point)))
555 (back-to-indentation)
adce3b5f
RS
556 (and (= (point) before-paren)
557 (progn
558 ;; Move to end of previous line.
559 (beginning-of-line)
560 (forward-char -1)
561 ;; Verify it doesn't end within a string or comment.
562 (let ((end (point))
563 state)
564 (beginning-of-line)
565 ;; Get state at start of line.
c6eeec65 566 (setq state (list 0 nil nil
adce3b5f
RS
567 (null (calculate-lisp-indent))
568 nil nil nil nil
569 nil))
570 ;; Parse state across the line to get state at end.
571 (setq state (parse-partial-sexp (point) end nil nil
572 state))
573 ;; Check not in string or comment.
574 (and (not (elt state 3)) (not (elt state 4))))))))
b73b9811 575 (delete-indentation))
576 (forward-char 1)
577 (newline-and-indent))
c6eeec65
DL
578
579(defun check-parens () ; lame name?
580 "Check for unbalanced parentheses in the current buffer.
581More accurately, check the narrowed part of the buffer for unbalanced
582expressions (\"sexps\") in general. This is done according to the
583current syntax table and will find unbalanced brackets or quotes as
7bc10886 584appropriate. (See Info node `(emacs)Parentheses'.) If imbalance is
8ad8cfa5 585found, an error is signaled and point is left at the first unbalanced
7bc10886 586character."
c6eeec65
DL
587 (interactive)
588 (condition-case data
589 ;; Buffer can't have more than (point-max) sexps.
590 (scan-sexps (point-min) (point-max))
591 (scan-error (goto-char (nth 2 data))
592 ;; Could print (nth 1 data), which is either
593 ;; "Containing expression ends prematurely" or
594 ;; "Unbalanced parentheses", but those may not be so
595 ;; accurate/helpful, e.g. quotes may actually be
596 ;; mismatched.
61e21607 597 (error "Unmatched bracket or quote"))))
b73b9811 598\f
61e21607 599(defun field-complete (table &optional predicate)
73ebf88f
SM
600 (let ((minibuffer-completion-table table)
601 (minibuffer-completion-predicate predicate)
602 ;; This made sense for lisp-complete-symbol, but for
603 ;; field-complete, this is out of place. --Stef
604 ;; (completion-annotate-function
605 ;; (unless (eq predicate 'fboundp)
606 ;; (lambda (str)
607 ;; (if (fboundp (intern-soft str)) " <f>"))))
608 )
609 (call-interactively 'minibuffer-complete)))
61e21607 610
e50c4203 611(defun lisp-complete-symbol (&optional predicate)
2eb9adab
RS
612 "Perform completion on Lisp symbol preceding point.
613Compare that symbol against the known Lisp symbols.
1fa1cb1b
RS
614If no characters can be completed, display a list of possible completions.
615Repeating the command at that point scrolls the list.
2eb9adab 616
e50c4203
DL
617When called from a program, optional arg PREDICATE is a predicate
618determining which symbols are considered, e.g. `commandp'.
619If PREDICATE is nil, the context determines which symbols are
620considered. If the symbol starts just after an open-parenthesis, only
621symbols with function definitions are considered. Otherwise, all
622symbols with function definitions, values or properties are
623considered."
b73b9811 624 (interactive)
51ef56c4
SM
625 (let* ((data (lisp-completion-at-point predicate))
626 (plist (nthcdr 3 data)))
627 (let ((completion-annotate-function (plist-get plist :annotate-function)))
628 (completion-in-region (nth 0 data) (nth 1 data) (nth 2 data)
629 (plist-get plist :predicate)))))
0ca12598 630
51ef56c4
SM
631
632(defun lisp-completion-at-point (&optional predicate)
0ca12598 633 "Function used for `completion-at-point-functions' in `emacs-lisp-mode'."
51ef56c4 634 ;; FIXME: the `end' could be after point?
4c528197
SM
635 (with-syntax-table emacs-lisp-mode-syntax-table
636 (let* ((end (point))
637 (beg (save-excursion
a1bf889a
SM
638 (backward-sexp 1)
639 (while (= (char-syntax (following-char)) ?\')
640 (forward-char 1))
4c528197
SM
641 (point)))
642 (predicate
643 (or predicate
644 (save-excursion
645 (goto-char beg)
646 (if (not (eq (char-before) ?\())
647 (lambda (sym) ;why not just nil ? -sm
648 (or (boundp sym) (fboundp sym)
649 (symbol-plist sym)))
650 ;; Looks like a funcall position. Let's double check.
651 (if (condition-case nil
652 (progn (up-list -2) (forward-char 1)
653 (eq (char-after) ?\())
654 (error nil))
655 ;; If the first element of the parent list is an open
656 ;; paren we are probably not in a funcall position.
657 ;; Maybe a `let' varlist or something.
658 nil
659 ;; Else, we assume that a function name is expected.
660 'fboundp))))))
661 (list beg end obarray
662 :predicate predicate
663 :annotate-function
51ef56c4 664 (unless (eq predicate 'fboundp)
4c528197 665 (lambda (str) (if (fboundp (intern-soft str)) " <f>")))))))
6594deb0 666
398de718 667;; arch-tag: aa7fa8a4-2e6f-4e9b-9cd9-fef06340e67e
6594deb0 668;;; lisp.el ends here