1 ;;; lisp.el --- Lisp editing commands for Emacs
3 ;; Copyright (C) 1985, 86, 1994, 2000, 2004 Free Software Foundation, Inc.
6 ;; Keywords: lisp, languages
8 ;; This file is part of GNU Emacs.
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)
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.
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.
27 ;; Lisp editing commands to go with Lisp major mode. More-or-less
28 ;; applicable in other modes too.
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'.
38 Setting this variable automatically makes it local to the current buffer."
39 :type '(choice (const nil)
42 (make-variable-buffer-local 'defun-prompt-regexp)
44 (defcustom parens-require-spaces t
45 "Non-nil means `insert-parentheses' should insert whitespace as needed."
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'.")
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."
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))))
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."
70 (forward-sexp (- arg)))
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."
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)))
93 (forward-sexp (prefix-numeric-value arg))
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."
102 (or arg (setq arg 1))
103 (goto-char (or (scan-lists (point) arg 0) (buffer-end arg))))
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."
110 (or arg (setq arg 1))
111 (forward-list (- arg)))
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."
118 (or arg (setq arg 1))
119 (let ((inc (if (> arg 0) 1 -1)))
121 (goto-char (or (scan-lists (point) inc -1) (buffer-end arg)))
122 (setq arg (- arg inc)))))
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."
129 (up-list (- (or arg 1))))
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."
136 (or arg (setq arg 1))
137 (let ((inc (if (> arg 0) 1 -1)))
139 (goto-char (or (scan-lists (point) inc 1) (buffer-end arg)))
140 (setq arg (- arg inc)))))
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."
147 (let ((opoint (point)))
148 (forward-sexp (or arg 1))
149 (kill-region opoint (point))))
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."
156 (kill-sexp (- (or arg 1))))
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."
164 (let ((current-sexp (thing-at-point 'sexp)))
167 (backward-up-list arg)
169 (insert current-sexp))
170 (error "Not at a sexp"))))
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
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.")
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.
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.
194 If variable `beginning-of-defun-function' is non-nil, its value
195 is called as a function to find the defun's beginning."
197 (or (not (eq this-command 'beginning-of-defun))
198 (eq last-command 'beginning-of-defun)
199 (and transient-mark-mode mark-active)
201 (and (beginning-of-defun-raw arg)
202 (progn (beginning-of-line) t)))
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'
210 If variable `beginning-of-defun-function' is non-nil, its value
211 is called as a function to find the defun's beginning."
213 (if beginning-of-defun-function
214 (if (> (setq arg (or arg 1)) 0)
216 (funcall beginning-of-defun-function))
217 ;; Better not call end-of-defun-function directly, in case
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
224 "\\(?:" defun-prompt-regexp "\\)\\s(")
226 nil 'move (or arg 1))
227 (progn (goto-char (1- (match-end 0)))) t)))
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.")
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)))
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.
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'.
249 If variable `end-of-defun-function' is non-nil, its value
250 is called as a function to find the defun's end."
252 (or (not (eq this-command 'end-of-defun))
253 (eq last-command 'end-of-defun)
254 (and transient-mark-mode mark-active)
256 (if (or (null arg) (= arg 0)) (setq arg 1))
257 (if end-of-defun-function
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)))
265 (while (and (> arg 0) (< (point) (point-max)))
271 (beginning-of-defun-raw 1)))
273 (or (bobp) (forward-char -1))
274 (beginning-of-defun-raw -1))
277 (skip-chars-forward " \t")
278 (if (looking-at "\\s<\\|\n")
284 (beginning-of-defun-raw 1)
288 (if (beginning-of-defun-raw 2)
291 (skip-chars-forward " \t")
292 (if (looking-at "\\s<\\|\n")
294 (goto-char (point-min)))))
295 (setq arg (1+ arg))))))
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.
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."
305 (cond ((and allow-extend
306 (or (and (eq last-command this-command) (mark t))
307 (and transient-mark-mode mark-active)))
314 (let ((opoint (point))
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.
324 (while (looking-at "^\n")
326 (if (> (point) opoint)
328 ;; We got the right defun.
329 (push-mark beg nil t)
331 (exchange-point-and-mark))
332 ;; beginning-of-defun moved back one defun
333 ;; so we got the wrong one.
336 (push-mark (point) nil t)
337 (beginning-of-defun))
338 (re-search-backward "^\n" (- (point) 1) t)))))
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."
347 (let ((opoint (point))
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.
356 (while (looking-at "^\n")
358 (unless (> (point) opoint)
359 ;; beginning-of-defun moved back one defun
360 ;; so we got the wrong one.
367 (re-search-backward "^\n" (- (point) 1) t)
368 (narrow-to-region beg end))))
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'.")
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.
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."
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))))
400 (setq open (nth 1 pair) close (nth 2 pair))
401 (setq open (nth 0 pair) close (nth 1 pair))))))
403 (if (and transient-mark-mode mark-active)
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))
409 (cond ((> arg 0) (skip-chars-forward " \t"))
410 ((< arg 0) (forward-sexp arg) (setq arg (- arg))))
411 (and parens-require-spaces
413 (memq (char-syntax (preceding-char)) (list ?w ?_ (char-syntax close)))
417 (or (eq arg 0) (forward-sexp arg))
419 (and parens-require-spaces
421 (memq (char-syntax (following-char)) (list ?w ?_ (char-syntax open)))
423 (insert-char (event-basic-type last-command-event)
424 (prefix-numeric-value arg))))
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."
434 (insert-pair arg ?\( ?\)))
436 (defun delete-pair ()
437 "Delete a pair of characters enclosing the sexp that follows point."
439 (save-excursion (forward-sexp 1) (delete-char -1))
442 (defun raise-sexp (&optional arg)
443 "Raise ARG sexps higher up the tree."
445 (let ((s (if (and transient-mark-mode mark-active)
446 (buffer-substring (region-beginning) (region-end))
449 (save-excursion (forward-sexp arg) (point))))))
451 (delete-region (point) (save-excursion (forward-sexp 1) (point)))
452 (save-excursion (insert s))))
454 (defun move-past-close-and-reindent ()
455 "Move past next `)', delete indentation before it, then indent after it."
459 (while (save-excursion ; this is my contribution
460 (let ((before-paren (point)))
461 (back-to-indentation)
462 (and (= (point) before-paren)
464 ;; Move to end of previous line.
467 ;; Verify it doesn't end within a string or comment.
471 ;; Get state at start of line.
472 (setq state (list 0 nil nil
473 (null (calculate-lisp-indent))
476 ;; Parse state across the line to get state at end.
477 (setq state (parse-partial-sexp (point) end nil nil
479 ;; Check not in string or comment.
480 (and (not (elt state 3)) (not (elt state 4))))))))
481 (delete-indentation))
483 (newline-and-indent))
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."
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
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)))))))
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.
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
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)
540 (beg (with-syntax-table emacs-lisp-mode-syntax-table
543 (while (= (char-syntax (following-char)) ?\')
546 (pattern (buffer-substring-no-properties beg end))
551 (if (not (eq (char-before) ?\())
552 (lambda (sym) ;why not just nil ? -sm
553 (or (boundp sym) (fboundp 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) ?\())
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.
564 ;; Else, we assume that a function name is expected.
566 (completion (try-completion pattern obarray predicate)))
567 (cond ((eq completion t))
569 (message "Can't find completion for \"%s\"" pattern)
571 ((not (string= pattern completion))
572 (delete-region beg end)
575 (message "Making completion list...")
576 (let ((list (all-completions pattern obarray predicate)))
577 (setq list (sort list 'string<))
578 (or (eq predicate 'fboundp)
581 (setq new (cons (if (fboundp (intern (car list)))
582 (list (car list) " <f>")
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")))))))
591 ;;; arch-tag: aa7fa8a4-2e6f-4e9b-9cd9-fef06340e67e
592 ;;; lisp.el ends here