(debugger-step-through): Make sure that stepping into the debugger's
[bpt/emacs.git] / 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 point.
144 With ARG, kill that many sexps after point.
145 Negative arg -N means kill N sexps before point."
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 point.
153 With ARG, kill that many sexps before point.
154 Negative arg -N means kill N sexps after point."
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 "Return the \"far end\" position of the buffer, moving in direction ARG.
237 If ARG is positive, that's the end of the buffer.
238 Otherwise, that's the beginning of the buffer."
239 (if (> arg 0) (point-max) (point-min)))
240
241 (defun end-of-defun (&optional arg)
242 "Move forward to next end of defun. With argument, do it that many times.
243 Negative argument -N means move back to Nth preceding end of defun.
244
245 An end of a defun occurs right after the close-parenthesis that
246 matches the open-parenthesis that starts a defun; see function
247 `beginning-of-defun'.
248
249 If variable `end-of-defun-function' is non-nil, its value
250 is called as a function to find the defun's end."
251 (interactive "p")
252 (or (not (eq this-command 'end-of-defun))
253 (eq last-command 'end-of-defun)
254 (and transient-mark-mode mark-active)
255 (push-mark))
256 (if (or (null arg) (= arg 0)) (setq arg 1))
257 (if end-of-defun-function
258 (if (> arg 0)
259 (dotimes (i arg)
260 (funcall end-of-defun-function))
261 ;; Better not call beginning-of-defun-function
262 ;; directly, in case it's not defined.
263 (beginning-of-defun (- arg)))
264 (let ((first t))
265 (while (and (> arg 0) (< (point) (point-max)))
266 (let ((pos (point)))
267 (while (progn
268 (if (and first
269 (progn
270 (end-of-line 1)
271 (beginning-of-defun-raw 1)))
272 nil
273 (or (bobp) (forward-char -1))
274 (beginning-of-defun-raw -1))
275 (setq first nil)
276 (forward-list 1)
277 (skip-chars-forward " \t")
278 (if (looking-at "\\s<\\|\n")
279 (forward-line 1))
280 (<= (point) pos))))
281 (setq arg (1- arg)))
282 (while (< arg 0)
283 (let ((pos (point)))
284 (beginning-of-defun-raw 1)
285 (forward-sexp 1)
286 (forward-line 1)
287 (if (>= (point) pos)
288 (if (beginning-of-defun-raw 2)
289 (progn
290 (forward-list 1)
291 (skip-chars-forward " \t")
292 (if (looking-at "\\s<\\|\n")
293 (forward-line 1)))
294 (goto-char (point-min)))))
295 (setq arg (1+ arg))))))
296
297 (defun mark-defun (&optional allow-extend)
298 "Put mark at end of this defun, point at beginning.
299 The defun marked is the one that contains point or follows point.
300
301 Interactively, if this command is repeated
302 or (in Transient Mark mode) if the mark is active,
303 it marks the next defun after the ones already marked."
304 (interactive "p")
305 (cond ((and allow-extend
306 (or (and (eq last-command this-command) (mark t))
307 (and transient-mark-mode mark-active)))
308 (set-mark
309 (save-excursion
310 (goto-char (mark))
311 (end-of-defun)
312 (point))))
313 (t
314 (let ((opoint (point))
315 beg end)
316 (push-mark opoint)
317 ;; Try first in this order for the sake of languages with nested
318 ;; functions where several can end at the same place as with
319 ;; the offside rule, e.g. Python.
320 (beginning-of-defun)
321 (setq beg (point))
322 (end-of-defun)
323 (setq end (point))
324 (while (looking-at "^\n")
325 (forward-line 1))
326 (if (> (point) opoint)
327 (progn
328 ;; We got the right defun.
329 (push-mark beg nil t)
330 (goto-char end)
331 (exchange-point-and-mark))
332 ;; beginning-of-defun moved back one defun
333 ;; so we got the wrong one.
334 (goto-char opoint)
335 (end-of-defun)
336 (push-mark (point) nil t)
337 (beginning-of-defun))
338 (re-search-backward "^\n" (- (point) 1) t)))))
339
340 (defun narrow-to-defun (&optional arg)
341 "Make text outside current defun invisible.
342 The defun visible is the one that contains point or follows point.
343 Optional ARG is ignored."
344 (interactive)
345 (save-excursion
346 (widen)
347 (let ((opoint (point))
348 beg end)
349 ;; Try first in this order for the sake of languages with nested
350 ;; functions where several can end at the same place as with
351 ;; the offside rule, e.g. Python.
352 (beginning-of-defun)
353 (setq beg (point))
354 (end-of-defun)
355 (setq end (point))
356 (while (looking-at "^\n")
357 (forward-line 1))
358 (unless (> (point) opoint)
359 ;; beginning-of-defun moved back one defun
360 ;; so we got the wrong one.
361 (goto-char opoint)
362 (end-of-defun)
363 (setq end (point))
364 (beginning-of-defun)
365 (setq beg (point)))
366 (goto-char end)
367 (re-search-backward "^\n" (- (point) 1) t)
368 (narrow-to-region beg end))))
369
370 (defvar insert-pair-alist
371 '((?\( ?\)) (?\[ ?\]) (?\{ ?\}) (?\< ?\>) (?\" ?\") (?\' ?\') (?\` ?\'))
372 "Alist of paired characters inserted by `insert-pair'.
373 Each element looks like (OPEN-CHAR CLOSE-CHAR) or (COMMAND-CHAR
374 OPEN-CHAR CLOSE-CHAR). The characters OPEN-CHAR and CLOSE-CHAR
375 of the pair whose key is equal to the last input character with
376 or without modifiers, are inserted by `insert-pair'.")
377
378 (defun insert-pair (&optional arg open close)
379 "Enclose following ARG sexps in a pair of OPEN and CLOSE characters.
380 Leave point after the first character.
381 A negative ARG encloses the preceding ARG sexps instead.
382 No argument is equivalent to zero: just insert characters
383 and leave point between.
384 If `parens-require-spaces' is non-nil, this command also inserts a space
385 before and after, depending on the surrounding characters.
386 If region is active, insert enclosing characters at region boundaries.
387
388 If arguments OPEN and CLOSE are nil, the character pair is found
389 from the variable `insert-pair-alist' according to the last input
390 character with or without modifiers. If no character pair is
391 found in the variable `insert-pair-alist', then the last input
392 character is inserted ARG times."
393 (interactive "P")
394 (if (not (and open close))
395 (let ((pair (or (assq last-command-char insert-pair-alist)
396 (assq (event-basic-type last-command-event)
397 insert-pair-alist))))
398 (if pair
399 (if (nth 2 pair)
400 (setq open (nth 1 pair) close (nth 2 pair))
401 (setq open (nth 0 pair) close (nth 1 pair))))))
402 (if (and open close)
403 (if (and transient-mark-mode mark-active)
404 (progn
405 (save-excursion (goto-char (region-end)) (insert close))
406 (save-excursion (goto-char (region-beginning)) (insert open)))
407 (if arg (setq arg (prefix-numeric-value arg))
408 (setq arg 0))
409 (cond ((> arg 0) (skip-chars-forward " \t"))
410 ((< arg 0) (forward-sexp arg) (setq arg (- arg))))
411 (and parens-require-spaces
412 (not (bobp))
413 (memq (char-syntax (preceding-char)) (list ?w ?_ (char-syntax close)))
414 (insert " "))
415 (insert open)
416 (save-excursion
417 (or (eq arg 0) (forward-sexp arg))
418 (insert close)
419 (and parens-require-spaces
420 (not (eobp))
421 (memq (char-syntax (following-char)) (list ?w ?_ (char-syntax open)))
422 (insert " "))))
423 (insert-char (event-basic-type last-command-event)
424 (prefix-numeric-value arg))))
425
426 (defun insert-parentheses (&optional arg)
427 "Enclose following ARG sexps in parentheses. Leave point after open-paren.
428 A negative ARG encloses the preceding ARG sexps instead.
429 No argument is equivalent to zero: just insert `()' and leave point between.
430 If `parens-require-spaces' is non-nil, this command also inserts a space
431 before and after, depending on the surrounding characters.
432 If region is active, insert enclosing characters at region boundaries."
433 (interactive "P")
434 (insert-pair arg ?\( ?\)))
435
436 (defun delete-pair ()
437 "Delete a pair of characters enclosing the sexp that follows point."
438 (interactive)
439 (save-excursion (forward-sexp 1) (delete-char -1))
440 (delete-char 1))
441
442 (defun raise-sexp (&optional arg)
443 "Raise ARG sexps higher up the tree."
444 (interactive "p")
445 (let ((s (if (and transient-mark-mode mark-active)
446 (buffer-substring (region-beginning) (region-end))
447 (buffer-substring
448 (point)
449 (save-excursion (forward-sexp arg) (point))))))
450 (backward-up-list 1)
451 (delete-region (point) (save-excursion (forward-sexp 1) (point)))
452 (save-excursion (insert s))))
453
454 (defun move-past-close-and-reindent ()
455 "Move past next `)', delete indentation before it, then indent after it."
456 (interactive)
457 (up-list 1)
458 (forward-char -1)
459 (while (save-excursion ; this is my contribution
460 (let ((before-paren (point)))
461 (back-to-indentation)
462 (and (= (point) before-paren)
463 (progn
464 ;; Move to end of previous line.
465 (beginning-of-line)
466 (forward-char -1)
467 ;; Verify it doesn't end within a string or comment.
468 (let ((end (point))
469 state)
470 (beginning-of-line)
471 ;; Get state at start of line.
472 (setq state (list 0 nil nil
473 (null (calculate-lisp-indent))
474 nil nil nil nil
475 nil))
476 ;; Parse state across the line to get state at end.
477 (setq state (parse-partial-sexp (point) end nil nil
478 state))
479 ;; Check not in string or comment.
480 (and (not (elt state 3)) (not (elt state 4))))))))
481 (delete-indentation))
482 (forward-char 1)
483 (newline-and-indent))
484
485 (defun check-parens () ; lame name?
486 "Check for unbalanced parentheses in the current buffer.
487 More accurately, check the narrowed part of the buffer for unbalanced
488 expressions (\"sexps\") in general. This is done according to the
489 current syntax table and will find unbalanced brackets or quotes as
490 appropriate. (See Info node `(emacs)Lists and Sexps'.) If imbalance
491 is found, an error is signalled and point is left at the first
492 unbalanced character."
493 (interactive)
494 (condition-case data
495 ;; Buffer can't have more than (point-max) sexps.
496 (scan-sexps (point-min) (point-max))
497 (scan-error (goto-char (nth 2 data))
498 ;; Could print (nth 1 data), which is either
499 ;; "Containing expression ends prematurely" or
500 ;; "Unbalanced parentheses", but those may not be so
501 ;; accurate/helpful, e.g. quotes may actually be
502 ;; mismatched.
503 (error "Unmatched bracket or quote"))
504 (error (cond ((eq 'scan-error (car data))
505 (goto-char (nth 2 data))
506 (error "Unmatched bracket or quote"))
507 (t (signal (car data) (cdr data)))))))
508 \f
509 (defun lisp-complete-symbol (&optional predicate)
510 "Perform completion on Lisp symbol preceding point.
511 Compare that symbol against the known Lisp symbols.
512 If no characters can be completed, display a list of possible completions.
513 Repeating the command at that point scrolls the list.
514
515 When called from a program, optional arg PREDICATE is a predicate
516 determining which symbols are considered, e.g. `commandp'.
517 If PREDICATE is nil, the context determines which symbols are
518 considered. If the symbol starts just after an open-parenthesis, only
519 symbols with function definitions are considered. Otherwise, all
520 symbols with function definitions, values or properties are
521 considered."
522 (interactive)
523
524 (let ((window (get-buffer-window "*Completions*")))
525 (if (and (eq last-command this-command)
526 window (window-live-p window) (window-buffer window)
527 (buffer-name (window-buffer window)))
528 ;; If this command was repeated, and
529 ;; there's a fresh completion window with a live buffer,
530 ;; and this command is repeated, scroll that window.
531 (with-current-buffer (window-buffer window)
532 (if (pos-visible-in-window-p (point-max) window)
533 (set-window-start window (point-min))
534 (save-selected-window
535 (select-window window)
536 (scroll-up))))
537
538 ;; Do completion.
539 (let* ((end (point))
540 (beg (with-syntax-table emacs-lisp-mode-syntax-table
541 (save-excursion
542 (backward-sexp 1)
543 (while (= (char-syntax (following-char)) ?\')
544 (forward-char 1))
545 (point))))
546 (pattern (buffer-substring-no-properties beg end))
547 (predicate
548 (or predicate
549 (save-excursion
550 (goto-char beg)
551 (if (not (eq (char-before) ?\())
552 (lambda (sym) ;why not just nil ? -sm
553 (or (boundp sym) (fboundp sym)
554 (symbol-plist sym)))
555 ;; Looks like a funcall position. Let's double check.
556 (if (condition-case nil
557 (progn (up-list -2) (forward-char 1)
558 (eq (char-after) ?\())
559 (error nil))
560 ;; If the first element of the parent list is an open
561 ;; parenthesis we are probably not in a funcall position.
562 ;; Maybe a `let' varlist or something.
563 nil
564 ;; Else, we assume that a function name is expected.
565 'fboundp)))))
566 (completion (try-completion pattern obarray predicate)))
567 (cond ((eq completion t))
568 ((null completion)
569 (message "Can't find completion for \"%s\"" pattern)
570 (ding))
571 ((not (string= pattern completion))
572 (delete-region beg end)
573 (insert completion))
574 (t
575 (message "Making completion list...")
576 (let ((list (all-completions pattern obarray predicate)))
577 (setq list (sort list 'string<))
578 (or (eq predicate 'fboundp)
579 (let (new)
580 (while list
581 (setq new (cons (if (fboundp (intern (car list)))
582 (list (car list) " <f>")
583 (car list))
584 new))
585 (setq list (cdr list)))
586 (setq list (nreverse new))))
587 (with-output-to-temp-buffer "*Completions*"
588 (display-completion-list list)))
589 (message "Making completion list...%s" "done")))))))
590
591 ;;; arch-tag: aa7fa8a4-2e6f-4e9b-9cd9-fef06340e67e
592 ;;; lisp.el ends here