]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/lisp.el
(mark-sexp, mark-defun): New arg ALLOW-EXTEND
[gnu-emacs] / lisp / emacs-lisp / lisp.el
1 ;;; lisp.el --- Lisp editing commands for Emacs
2
3 ;; Copyright (C) 1985, 86, 1994, 2000, 2004 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 the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; Lisp editing commands to go with Lisp major mode. More-or-less
28 ;; applicable in other modes too.
29
30 ;;; Code:
31
32 ;; Note that this variable is used by non-lisp modes too.
33 (defcustom defun-prompt-regexp nil
34 "*If non-nil, a regexp to ignore before the character that starts a defun.
35 This is only necessary if the opening paren or brace is not in column 0.
36 See function `beginning-of-defun'.
37
38 Setting this variable automatically makes it local to the current buffer."
39 :type '(choice (const nil)
40 regexp)
41 :group 'lisp)
42 (make-variable-buffer-local 'defun-prompt-regexp)
43
44 (defcustom parens-require-spaces t
45 "Non-nil means `insert-parentheses' should insert whitespace as needed."
46 :type 'boolean
47 :group 'lisp)
48
49 (defvar forward-sexp-function nil
50 "If non-nil, `forward-sexp' delegates to this function.
51 Should take the same arguments and behave similarly to `forward-sexp'.")
52
53 (defun forward-sexp (&optional arg)
54 "Move forward across one balanced expression (sexp).
55 With ARG, do it that many times. Negative arg -N means
56 move backward across N balanced expressions."
57 (interactive "p")
58 (or arg (setq arg 1))
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))))
63
64 (defun backward-sexp (&optional arg)
65 "Move backward across one balanced expression (sexp).
66 With ARG, do it that many times. Negative arg -N means
67 move forward across N balanced expressions."
68 (interactive "p")
69 (or arg (setq arg 1))
70 (forward-sexp (- arg)))
71
72 (defun mark-sexp (&optional arg allow-extend)
73 "Set mark ARG sexps from point.
74 The place mark goes is the same place \\[forward-sexp] would
75 move to with the same argument.
76 Interactively, if this command is repeated
77 or (in Transient Mark mode) if the mark is active,
78 it marks the next ARG sexps after the ones already marked."
79 (interactive "P\np")
80 (cond ((and allow-extend
81 (or (and (eq last-command this-command) (mark t))
82 (and transient-mark-mode mark-active)))
83 (setq arg (if arg (prefix-numeric-value arg)
84 (if (< (mark) (point)) -1 1)))
85 (set-mark
86 (save-excursion
87 (goto-char (mark))
88 (forward-sexp arg)
89 (point))))
90 (t
91 (push-mark
92 (save-excursion
93 (forward-sexp (prefix-numeric-value arg))
94 (point))
95 nil t))))
96
97 (defun forward-list (&optional arg)
98 "Move forward across one balanced group of parentheses.
99 With ARG, do it that many times.
100 Negative arg -N means move backward across N groups of parentheses."
101 (interactive "p")
102 (or arg (setq arg 1))
103 (goto-char (or (scan-lists (point) arg 0) (buffer-end arg))))
104
105 (defun backward-list (&optional arg)
106 "Move backward across one balanced group of parentheses.
107 With ARG, do it that many times.
108 Negative arg -N means move forward across N groups of parentheses."
109 (interactive "p")
110 (or arg (setq arg 1))
111 (forward-list (- arg)))
112
113 (defun down-list (&optional arg)
114 "Move forward down one level of parentheses.
115 With ARG, do this that many times.
116 A negative argument means move backward but still go down a level."
117 (interactive "p")
118 (or arg (setq arg 1))
119 (let ((inc (if (> arg 0) 1 -1)))
120 (while (/= arg 0)
121 (goto-char (or (scan-lists (point) inc -1) (buffer-end arg)))
122 (setq arg (- arg inc)))))
123
124 (defun backward-up-list (&optional arg)
125 "Move backward out of one level of parentheses.
126 With ARG, do this that many times.
127 A negative argument means move forward but still to a less deep spot."
128 (interactive "p")
129 (up-list (- (or arg 1))))
130
131 (defun up-list (&optional arg)
132 "Move forward out of one level of parentheses.
133 With ARG, do this that many times.
134 A negative argument means move backward but still to a less deep spot."
135 (interactive "p")
136 (or arg (setq arg 1))
137 (let ((inc (if (> arg 0) 1 -1)))
138 (while (/= arg 0)
139 (goto-char (or (scan-lists (point) inc 1) (buffer-end arg)))
140 (setq arg (- arg inc)))))
141
142 (defun kill-sexp (&optional arg)
143 "Kill the sexp (balanced expression) following the cursor.
144 With ARG, kill that many sexps after the cursor.
145 Negative arg -N means kill N sexps before the cursor."
146 (interactive "p")
147 (let ((opoint (point)))
148 (forward-sexp (or arg 1))
149 (kill-region opoint (point))))
150
151 (defun backward-kill-sexp (&optional arg)
152 "Kill the sexp (balanced expression) preceding the cursor.
153 With ARG, kill that many sexps before the cursor.
154 Negative arg -N means kill N sexps after the cursor."
155 (interactive "p")
156 (kill-sexp (- (or arg 1))))
157
158 ;; After Zmacs:
159 (defun kill-backward-up-list (&optional arg)
160 "Kill the form containing the current sexp, leaving the sexp itself.
161 A prefix argument ARG causes the relevant number of surrounding
162 forms to be removed."
163 (interactive "*p")
164 (let ((current-sexp (thing-at-point 'sexp)))
165 (if current-sexp
166 (save-excursion
167 (backward-up-list arg)
168 (kill-sexp)
169 (insert current-sexp))
170 (error "Not at a sexp"))))
171 \f
172 (defvar beginning-of-defun-function nil
173 "If non-nil, function for `beginning-of-defun-raw' to call.
174 This is used to find the beginning of the defun instead of using the
175 normal recipe (see `beginning-of-defun'). Major modes can define this
176 if defining `defun-prompt-regexp' is not sufficient to handle the mode's
177 needs.
178
179 The function (of no args) should go to the line on which the current
180 defun starts, and return non-nil, or should return nil if it can't
181 find the beginning.")
182
183 (defun beginning-of-defun (&optional arg)
184 "Move backward to the beginning of a defun.
185 With ARG, do it that many times. Negative arg -N
186 means move forward to Nth following beginning of defun.
187 Returns t unless search stops due to beginning or end of buffer.
188
189 Normally a defun starts when there is a char with open-parenthesis
190 syntax at the beginning of a line. If `defun-prompt-regexp' is
191 non-nil, then a string which matches that regexp may precede the
192 open-parenthesis, and point ends up at the beginning of the line.
193
194 If variable `beginning-of-defun-function' is non-nil, its value
195 is called as a function to find the defun's beginning."
196 (interactive "p")
197 (or (not (eq this-command 'beginning-of-defun))
198 (eq last-command 'beginning-of-defun)
199 (and transient-mark-mode mark-active)
200 (push-mark))
201 (and (beginning-of-defun-raw arg)
202 (progn (beginning-of-line) t)))
203
204 (defun beginning-of-defun-raw (&optional arg)
205 "Move point to the character that starts a defun.
206 This is identical to function `beginning-of-defun', except that point
207 does not move to the beginning of the line when `defun-prompt-regexp'
208 is non-nil.
209
210 If variable `beginning-of-defun-function' is non-nil, its value
211 is called as a function to find the defun's beginning."
212 (interactive "p")
213 (if beginning-of-defun-function
214 (if (> (setq arg (or arg 1)) 0)
215 (dotimes (i arg)
216 (funcall beginning-of-defun-function))
217 ;; Better not call end-of-defun-function directly, in case
218 ;; it's not defined.
219 (end-of-defun (- arg)))
220 (and arg (< arg 0) (not (eobp)) (forward-char 1))
221 (and (re-search-backward (if defun-prompt-regexp
222 (concat (if open-paren-in-column-0-is-defun-start
223 "^\\s(\\|" "")
224 "\\(?:" defun-prompt-regexp "\\)\\s(")
225 "^\\s(")
226 nil 'move (or arg 1))
227 (progn (goto-char (1- (match-end 0)))) t)))
228
229 (defvar end-of-defun-function nil
230 "If non-nil, function for function `end-of-defun' to call.
231 This is used to find the end of the defun instead of using the normal
232 recipe (see `end-of-defun'). Major modes can define this if the
233 normal method is not appropriate.")
234
235 (defun buffer-end (arg)
236 (if (> arg 0) (point-max) (point-min)))
237
238 (defun end-of-defun (&optional arg)
239 "Move forward to next end of defun. With argument, do it that many times.
240 Negative argument -N means move back to Nth preceding end of defun.
241
242 An end of a defun occurs right after the close-parenthesis that
243 matches the open-parenthesis that starts a defun; see function
244 `beginning-of-defun'.
245
246 If variable `end-of-defun-function' is non-nil, its value
247 is called as a function to find the defun's end."
248 (interactive "p")
249 (or (not (eq this-command 'end-of-defun))
250 (eq last-command 'end-of-defun)
251 (and transient-mark-mode mark-active)
252 (push-mark))
253 (if (or (null arg) (= arg 0)) (setq arg 1))
254 (if end-of-defun-function
255 (if (> arg 0)
256 (dotimes (i arg)
257 (funcall end-of-defun-function))
258 ;; Better not call beginning-of-defun-function
259 ;; directly, in case it's not defined.
260 (beginning-of-defun (- arg)))
261 (let ((first t))
262 (while (and (> arg 0) (< (point) (point-max)))
263 (let ((pos (point)))
264 (while (progn
265 (if (and first
266 (progn
267 (end-of-line 1)
268 (beginning-of-defun-raw 1)))
269 nil
270 (or (bobp) (forward-char -1))
271 (beginning-of-defun-raw -1))
272 (setq first nil)
273 (forward-list 1)
274 (skip-chars-forward " \t")
275 (if (looking-at "\\s<\\|\n")
276 (forward-line 1))
277 (<= (point) pos))))
278 (setq arg (1- arg)))
279 (while (< arg 0)
280 (let ((pos (point)))
281 (beginning-of-defun-raw 1)
282 (forward-sexp 1)
283 (forward-line 1)
284 (if (>= (point) pos)
285 (if (beginning-of-defun-raw 2)
286 (progn
287 (forward-list 1)
288 (skip-chars-forward " \t")
289 (if (looking-at "\\s<\\|\n")
290 (forward-line 1)))
291 (goto-char (point-min)))))
292 (setq arg (1+ arg))))))
293
294 (defun mark-defun (&optional allow-extend)
295 "Put mark at end of this defun, point at beginning.
296 The defun marked is the one that contains point or follows point.
297
298 Interactively, if this command is repeated
299 or (in Transient Mark mode) if the mark is active,
300 it marks the next defun after the ones already marked."
301 (interactive "p")
302 (cond ((and allow-extend
303 (or (and (eq last-command this-command) (mark t))
304 (and transient-mark-mode mark-active)))
305 (set-mark
306 (save-excursion
307 (goto-char (mark))
308 (end-of-defun)
309 (point))))
310 (t
311 (let ((opoint (point))
312 beg end)
313 (push-mark opoint)
314 ;; Try first in this order for the sake of languages with nested
315 ;; functions where several can end at the same place as with
316 ;; the offside rule, e.g. Python.
317 (beginning-of-defun)
318 (setq beg (point))
319 (end-of-defun)
320 (setq end (point))
321 (while (looking-at "^\n")
322 (forward-line 1))
323 (if (> (point) opoint)
324 (progn
325 ;; We got the right defun.
326 (push-mark beg nil t)
327 (goto-char end)
328 (exchange-point-and-mark))
329 ;; beginning-of-defun moved back one defun
330 ;; so we got the wrong one.
331 (goto-char opoint)
332 (end-of-defun)
333 (push-mark (point) nil t)
334 (beginning-of-defun))
335 (re-search-backward "^\n" (- (point) 1) t)))))
336
337 (defun narrow-to-defun (&optional arg)
338 "Make text outside current defun invisible.
339 The defun visible is the one that contains point or follows point.
340 Optional ARG is ignored."
341 (interactive)
342 (save-excursion
343 (widen)
344 (let ((opoint (point))
345 beg end)
346 ;; Try first in this order for the sake of languages with nested
347 ;; functions where several can end at the same place as with
348 ;; the offside rule, e.g. Python.
349 (beginning-of-defun)
350 (setq beg (point))
351 (end-of-defun)
352 (setq end (point))
353 (while (looking-at "^\n")
354 (forward-line 1))
355 (unless (> (point) opoint)
356 ;; beginning-of-defun moved back one defun
357 ;; so we got the wrong one.
358 (goto-char opoint)
359 (end-of-defun)
360 (setq end (point))
361 (beginning-of-defun)
362 (setq beg (point)))
363 (goto-char end)
364 (re-search-backward "^\n" (- (point) 1) t)
365 (narrow-to-region beg end))))
366
367 (defvar insert-pair-alist
368 '((?\( ?\)) (?\[ ?\]) (?\{ ?\}) (?\< ?\>) (?\" ?\") (?\' ?\') (?\` ?\'))
369 "Alist of paired characters inserted by `insert-pair'.
370 Each element looks like (OPEN-CHAR CLOSE-CHAR) or (COMMAND-CHAR
371 OPEN-CHAR CLOSE-CHAR). The characters OPEN-CHAR and CLOSE-CHAR
372 of the pair whose key is equal to the last input character with
373 or without modifiers, are inserted by `insert-pair'.")
374
375 (defun insert-pair (&optional arg open close)
376 "Enclose following ARG sexps in a pair of OPEN and CLOSE characters.
377 Leave point after the first character.
378 A negative ARG encloses the preceding ARG sexps instead.
379 No argument is equivalent to zero: just insert characters
380 and leave point between.
381 If `parens-require-spaces' is non-nil, this command also inserts a space
382 before and after, depending on the surrounding characters.
383 If region is active, insert enclosing characters at region boundaries.
384
385 If arguments OPEN and CLOSE are nil, the character pair is found
386 from the variable `insert-pair-alist' according to the last input
387 character with or without modifiers. If no character pair is
388 found in the variable `insert-pair-alist', then the last input
389 character is inserted ARG times."
390 (interactive "P")
391 (if (not (and open close))
392 (let ((pair (or (assq last-command-char insert-pair-alist)
393 (assq (event-basic-type last-command-event)
394 insert-pair-alist))))
395 (if pair
396 (if (nth 2 pair)
397 (setq open (nth 1 pair) close (nth 2 pair))
398 (setq open (nth 0 pair) close (nth 1 pair))))))
399 (if (and open close)
400 (if (and transient-mark-mode mark-active)
401 (progn
402 (save-excursion (goto-char (region-end)) (insert close))
403 (save-excursion (goto-char (region-beginning)) (insert open)))
404 (if arg (setq arg (prefix-numeric-value arg))
405 (setq arg 0))
406 (cond ((> arg 0) (skip-chars-forward " \t"))
407 ((< arg 0) (forward-sexp arg) (setq arg (- arg))))
408 (and parens-require-spaces
409 (not (bobp))
410 (memq (char-syntax (preceding-char)) (list ?w ?_ (char-syntax close)))
411 (insert " "))
412 (insert open)
413 (save-excursion
414 (or (eq arg 0) (forward-sexp arg))
415 (insert close)
416 (and parens-require-spaces
417 (not (eobp))
418 (memq (char-syntax (following-char)) (list ?w ?_ (char-syntax open)))
419 (insert " "))))
420 (insert-char (event-basic-type last-command-event)
421 (prefix-numeric-value arg))))
422
423 (defun insert-parentheses (&optional arg)
424 "Enclose following ARG sexps in parentheses. Leave point after open-paren.
425 A negative ARG encloses the preceding ARG sexps instead.
426 No argument is equivalent to zero: just insert `()' and leave point between.
427 If `parens-require-spaces' is non-nil, this command also inserts a space
428 before and after, depending on the surrounding characters.
429 If region is active, insert enclosing characters at region boundaries."
430 (interactive "P")
431 (insert-pair arg ?\( ?\)))
432
433 (defun delete-pair ()
434 "Delete a pair of characters enclosing the sexp that follows point."
435 (interactive)
436 (save-excursion (forward-sexp 1) (delete-char -1))
437 (delete-char 1))
438
439 (defun raise-sexp (&optional arg)
440 "Raise ARG sexps higher up the tree."
441 (interactive "p")
442 (let ((s (if (and transient-mark-mode mark-active)
443 (buffer-substring (region-beginning) (region-end))
444 (buffer-substring
445 (point)
446 (save-excursion (forward-sexp arg) (point))))))
447 (backward-up-list 1)
448 (delete-region (point) (save-excursion (forward-sexp 1) (point)))
449 (save-excursion (insert s))))
450
451 (defun move-past-close-and-reindent ()
452 "Move past next `)', delete indentation before it, then indent after it."
453 (interactive)
454 (up-list 1)
455 (forward-char -1)
456 (while (save-excursion ; this is my contribution
457 (let ((before-paren (point)))
458 (back-to-indentation)
459 (and (= (point) before-paren)
460 (progn
461 ;; Move to end of previous line.
462 (beginning-of-line)
463 (forward-char -1)
464 ;; Verify it doesn't end within a string or comment.
465 (let ((end (point))
466 state)
467 (beginning-of-line)
468 ;; Get state at start of line.
469 (setq state (list 0 nil nil
470 (null (calculate-lisp-indent))
471 nil nil nil nil
472 nil))
473 ;; Parse state across the line to get state at end.
474 (setq state (parse-partial-sexp (point) end nil nil
475 state))
476 ;; Check not in string or comment.
477 (and (not (elt state 3)) (not (elt state 4))))))))
478 (delete-indentation))
479 (forward-char 1)
480 (newline-and-indent))
481
482 (defun check-parens () ; lame name?
483 "Check for unbalanced parentheses in the current buffer.
484 More accurately, check the narrowed part of the buffer for unbalanced
485 expressions (\"sexps\") in general. This is done according to the
486 current syntax table and will find unbalanced brackets or quotes as
487 appropriate. (See Info node `(emacs)Lists and Sexps'.) If imbalance
488 is found, an error is signalled and point is left at the first
489 unbalanced character."
490 (interactive)
491 (condition-case data
492 ;; Buffer can't have more than (point-max) sexps.
493 (scan-sexps (point-min) (point-max))
494 (scan-error (goto-char (nth 2 data))
495 ;; Could print (nth 1 data), which is either
496 ;; "Containing expression ends prematurely" or
497 ;; "Unbalanced parentheses", but those may not be so
498 ;; accurate/helpful, e.g. quotes may actually be
499 ;; mismatched.
500 (error "Unmatched bracket or quote"))
501 (error (cond ((eq 'scan-error (car data))
502 (goto-char (nth 2 data))
503 (error "Unmatched bracket or quote"))
504 (t (signal (car data) (cdr data)))))))
505 \f
506 (defun lisp-complete-symbol (&optional predicate)
507 "Perform completion on Lisp symbol preceding point.
508 Compare that symbol against the known Lisp symbols.
509 If no characters can be completed, display a list of possible completions.
510 Repeating the command at that point scrolls the list.
511
512 When called from a program, optional arg PREDICATE is a predicate
513 determining which symbols are considered, e.g. `commandp'.
514 If PREDICATE is nil, the context determines which symbols are
515 considered. If the symbol starts just after an open-parenthesis, only
516 symbols with function definitions are considered. Otherwise, all
517 symbols with function definitions, values or properties are
518 considered."
519 (interactive)
520
521 (let ((window (get-buffer-window "*Completions*")))
522 (if (and (eq last-command this-command)
523 window (window-live-p window) (window-buffer window)
524 (buffer-name (window-buffer window)))
525 ;; If this command was repeated, and
526 ;; there's a fresh completion window with a live buffer,
527 ;; and this command is repeated, scroll that window.
528 (with-current-buffer (window-buffer window)
529 (if (pos-visible-in-window-p (point-max) window)
530 (set-window-start window (point-min))
531 (save-selected-window
532 (select-window window)
533 (scroll-up))))
534
535 ;; Do completion.
536 (let* ((end (point))
537 (beg (with-syntax-table emacs-lisp-mode-syntax-table
538 (save-excursion
539 (backward-sexp 1)
540 (while (= (char-syntax (following-char)) ?\')
541 (forward-char 1))
542 (point))))
543 (pattern (buffer-substring-no-properties beg end))
544 (predicate
545 (or predicate
546 (save-excursion
547 (goto-char beg)
548 (if (not (eq (char-before) ?\())
549 (lambda (sym) ;why not just nil ? -sm
550 (or (boundp sym) (fboundp sym)
551 (symbol-plist sym)))
552 ;; Looks like a funcall position. Let's double check.
553 (if (condition-case nil
554 (progn (up-list -2) (forward-char 1)
555 (eq (char-after) ?\())
556 (error nil))
557 ;; If the first element of the parent list is an open
558 ;; parenthesis we are probably not in a funcall position.
559 ;; Maybe a `let' varlist or something.
560 nil
561 ;; Else, we assume that a function name is expected.
562 'fboundp)))))
563 (completion (try-completion pattern obarray predicate)))
564 (cond ((eq completion t))
565 ((null completion)
566 (message "Can't find completion for \"%s\"" pattern)
567 (ding))
568 ((not (string= pattern completion))
569 (delete-region beg end)
570 (insert completion))
571 (t
572 (message "Making completion list...")
573 (let ((list (all-completions pattern obarray predicate)))
574 (setq list (sort list 'string<))
575 (or (eq predicate 'fboundp)
576 (let (new)
577 (while list
578 (setq new (cons (if (fboundp (intern (car list)))
579 (list (car list) " <f>")
580 (car list))
581 new))
582 (setq list (cdr list)))
583 (setq list (nreverse new))))
584 (with-output-to-temp-buffer "*Completions*"
585 (display-completion-list list)))
586 (message "Making completion list...%s" "done")))))))
587
588 ;;; arch-tag: aa7fa8a4-2e6f-4e9b-9cd9-fef06340e67e
589 ;;; lisp.el ends here