1 ;;; lisp.el --- Lisp editing commands for Emacs
3 ;; Copyright (C) 1985-1986, 1994, 2000-2012 Free Software Foundation, Inc.
6 ;; Keywords: lisp, languages
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
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.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; Lisp editing commands to go with Lisp major mode. More-or-less
27 ;; applicable in other modes too.
31 ;; Note that this variable is used by non-lisp modes too.
32 (defcustom defun-prompt-regexp nil
33 "If non-nil, a regexp to ignore before a defun.
34 This is only necessary if the opening paren or brace is not in column 0.
35 See function `beginning-of-defun'."
36 :type
'(choice (const nil
)
39 (make-variable-buffer-local 'defun-prompt-regexp
)
41 (defcustom parens-require-spaces t
42 "If non-nil, add whitespace as needed when inserting parentheses.
43 This affects `insert-parentheses' and `insert-pair'."
47 (defvar forward-sexp-function nil
48 "If non-nil, `forward-sexp' delegates to this function.
49 Should take the same arguments and behave similarly to `forward-sexp'.")
51 (defun forward-sexp (&optional arg
)
52 "Move forward across one balanced expression (sexp).
53 With ARG, do it that many times. Negative arg -N means
54 move backward across N balanced expressions.
55 This command assumes point is not in a string or comment."
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))))
63 (defun backward-sexp (&optional arg
)
64 "Move backward across one balanced expression (sexp).
65 With ARG, do it that many times. Negative arg -N means
66 move forward across N balanced expressions.
67 This command assumes point is not in a string or comment."
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.
79 This command assumes point is not in a string or comment."
81 (cond ((and allow-extend
82 (or (and (eq last-command this-command
) (mark t
))
83 (and transient-mark-mode mark-active
)))
84 (setq arg
(if arg
(prefix-numeric-value arg
)
85 (if (< (mark) (point)) -
1 1)))
94 (forward-sexp (prefix-numeric-value arg
))
98 (defun forward-list (&optional arg
)
99 "Move forward across one balanced group of parentheses.
100 With ARG, do it that many times.
101 Negative arg -N means move backward across N groups of parentheses.
102 This command assumes point is not in a string or comment."
104 (or arg
(setq arg
1))
105 (goto-char (or (scan-lists (point) arg
0) (buffer-end arg
))))
107 (defun backward-list (&optional arg
)
108 "Move backward across one balanced group of parentheses.
109 With ARG, do it that many times.
110 Negative arg -N means move forward across N groups of parentheses.
111 This command assumes point is not in a string or comment."
113 (or arg
(setq arg
1))
114 (forward-list (- arg
)))
116 (defun down-list (&optional arg
)
117 "Move forward down one level of parentheses.
118 With ARG, do this that many times.
119 A negative argument means move backward but still go down a level.
120 This command assumes point is not in a string or comment."
122 (or arg
(setq arg
1))
123 (let ((inc (if (> arg
0) 1 -
1)))
125 (goto-char (or (scan-lists (point) inc -
1) (buffer-end arg
)))
126 (setq arg
(- arg inc
)))))
128 (defun backward-up-list (&optional arg
)
129 "Move backward out of one level of parentheses.
130 With ARG, do this that many times.
131 A negative argument means move forward but still to a less deep spot.
132 This command assumes point is not in a string or comment."
134 (up-list (- (or arg
1))))
136 (defun up-list (&optional arg
)
137 "Move forward out of one level of parentheses.
138 With ARG, do this that many times.
139 A negative argument means move backward but still to a less deep spot.
140 This command assumes point is not in a string or comment."
142 (or arg
(setq arg
1))
143 (let ((inc (if (> arg
0) 1 -
1))
146 (if (null forward-sexp-function
)
147 (goto-char (or (scan-lists (point) inc
1) (buffer-end arg
)))
149 (while (progn (setq pos
(point))
152 (scan-error (goto-char (nth (if (> arg
0) 3 2) err
))))
155 (list "Unbalanced parentheses" (point) (point)))))
156 (setq arg
(- arg inc
)))))
158 (defun kill-sexp (&optional arg
)
159 "Kill the sexp (balanced expression) following point.
160 With ARG, kill that many sexps after point.
161 Negative arg -N means kill N sexps before point.
162 This command assumes point is not in a string or comment."
164 (let ((opoint (point)))
165 (forward-sexp (or arg
1))
166 (kill-region opoint
(point))))
168 (defun backward-kill-sexp (&optional arg
)
169 "Kill the sexp (balanced expression) preceding point.
170 With ARG, kill that many sexps before point.
171 Negative arg -N means kill N sexps after point.
172 This command assumes point is not in a string or comment."
174 (kill-sexp (- (or arg
1))))
177 (defun kill-backward-up-list (&optional arg
)
178 "Kill the form containing the current sexp, leaving the sexp itself.
179 A prefix argument ARG causes the relevant number of surrounding
181 This command assumes point is not in a string or comment."
183 (let ((current-sexp (thing-at-point 'sexp
)))
186 (backward-up-list arg
)
188 (insert current-sexp
))
189 (error "Not at a sexp"))))
191 (defvar beginning-of-defun-function nil
192 "If non-nil, function for `beginning-of-defun-raw' to call.
193 This is used to find the beginning of the defun instead of using the
194 normal recipe (see `beginning-of-defun'). Major modes can define this
195 if defining `defun-prompt-regexp' is not sufficient to handle the mode's
198 The function takes the same argument as `beginning-of-defun' and should
199 behave similarly, returning non-nil if it found the beginning of a defun.
200 Ideally it should move to a point right before an open-paren which encloses
201 the body of the defun.")
203 (defun beginning-of-defun (&optional arg
)
204 "Move backward to the beginning of a defun.
205 With ARG, do it that many times. Negative ARG means move forward
206 to the ARGth following beginning of defun.
208 If search is successful, return t; point ends up at the beginning
209 of the line where the search succeeded. Otherwise, return nil.
211 When `open-paren-in-column-0-is-defun-start' is non-nil, a defun
212 is assumed to start where there is a char with open-parenthesis
213 syntax at the beginning of a line. If `defun-prompt-regexp' is
214 non-nil, then a string which matches that regexp may also precede
215 the open-parenthesis. If `defun-prompt-regexp' and
216 `open-paren-in-column-0-is-defun-start' are both nil, this
217 function instead finds an open-paren at the outermost level.
219 If the variable `beginning-of-defun-function' is non-nil, its
220 value is called as a function, with argument ARG, to find the
223 Regardless of the values of `defun-prompt-regexp' and
224 `beginning-of-defun-function', point always moves to the
225 beginning of the line whenever the search is successful."
227 (or (not (eq this-command
'beginning-of-defun
))
228 (eq last-command
'beginning-of-defun
)
229 (and transient-mark-mode mark-active
)
231 (and (beginning-of-defun-raw arg
)
232 (progn (beginning-of-line) t
)))
234 (defun beginning-of-defun-raw (&optional arg
)
235 "Move point to the character that starts a defun.
236 This is identical to function `beginning-of-defun', except that point
237 does not move to the beginning of the line when `defun-prompt-regexp'
240 If variable `beginning-of-defun-function' is non-nil, its value
241 is called as a function to find the defun's beginning."
242 (interactive "^p") ; change this to "P", maybe, if we ever come to pass ARG
243 ; to beginning-of-defun-function.
244 (unless arg
(setq arg
1))
246 (beginning-of-defun-function
248 (funcall beginning-of-defun-function arg
)
249 ;; We used to define beginning-of-defun-function as taking no argument
250 ;; but that makes it impossible to implement correct forward motion:
251 ;; we used to use end-of-defun for that, but it's not supposed to do
252 ;; the same thing (it moves to the end of a defun not to the beginning
254 ;; In case the beginning-of-defun-function uses the old calling
255 ;; convention, fallback on the old implementation.
256 (wrong-number-of-arguments
259 (funcall beginning-of-defun-function
))
261 (funcall end-of-defun-function
))))))
263 ((or defun-prompt-regexp open-paren-in-column-0-is-defun-start
)
264 (and (< arg
0) (not (eobp)) (forward-char 1))
265 (and (re-search-backward (if defun-prompt-regexp
266 (concat (if open-paren-in-column-0-is-defun-start
268 "\\(?:" defun-prompt-regexp
"\\)\\s(")
271 (progn (goto-char (1- (match-end 0)))
274 ;; If open-paren-in-column-0-is-defun-start and defun-prompt-regexp
275 ;; are both nil, column 0 has no significance - so scan forward
276 ;; from BOB to see how nested point is, then carry on from there.
278 ;; It is generally not a good idea to land up here, because the
279 ;; call to scan-lists below can be extremely slow. This is because
280 ;; back_comment in syntax.c may have to scan from bob to find the
281 ;; beginning of each comment. Fixing this is not trivial -- cyd.
285 (let ((floor (point-min))
286 (ceiling (point-max))
290 (let ((ppss (let (syntax-begin-function
291 font-lock-beginning-of-syntax-function
)
293 ;; position of least enclosing paren, or nil.
295 ;; Back out of any comment/string, so that encl-pos will always
296 ;; become nil if we're at top-level.
298 (goto-char (nth 8 ppss
))
299 (setq ppss
(syntax-ppss))) ; should be fast, due to cache.
300 (setq encl-pos
(syntax-ppss-toplevel-pos ppss
))
301 (if encl-pos
(goto-char encl-pos
))
303 (and encl-pos arg-
+ve
(setq arg
(1- arg
)))
304 (and (not encl-pos
) (not arg-
+ve
) (not (looking-at "\\s("))
307 (condition-case nil
; to catch crazy parens.
309 (goto-char (scan-lists (point) (- arg
) 0))
311 (if (>= (point) floor
)
315 ;; forward to next (, or trigger the c-c
316 (goto-char (1- (scan-lists (point) 1 -
1)))
317 (if (<= (point) ceiling
)
322 (goto-char (if arg-
+ve floor ceiling
))
325 (defvar end-of-defun-function
326 (lambda () (forward-sexp 1))
327 "Function for `end-of-defun' to call.
328 This is used to find the end of the defun at point.
329 It is called with no argument, right after calling `beginning-of-defun-raw'.
330 So the function can assume that point is at the beginning of the defun body.
331 It should move point to the first position after the defun.")
333 (defun buffer-end (arg)
334 "Return the \"far end\" position of the buffer, in direction ARG.
335 If ARG is positive, that's the end of the buffer.
336 Otherwise, that's the beginning of the buffer."
337 (if (> arg
0) (point-max) (point-min)))
339 (defun end-of-defun (&optional arg
)
340 "Move forward to next end of defun.
341 With argument, do it that many times.
342 Negative argument -N means move back to Nth preceding end of defun.
344 An end of a defun occurs right after the close-parenthesis that
345 matches the open-parenthesis that starts a defun; see function
346 `beginning-of-defun'.
348 If variable `end-of-defun-function' is non-nil, its value
349 is called as a function to find the defun's end."
351 (or (not (eq this-command
'end-of-defun
))
352 (eq last-command
'end-of-defun
)
353 (and transient-mark-mode mark-active
)
355 (if (or (null arg
) (= arg
0)) (setq arg
1))
357 (beg (progn (end-of-line 1) (beginning-of-defun-raw 1) (point))))
358 (funcall end-of-defun-function
)
359 ;; When comparing point against pos, we want to consider that if
360 ;; point was right after the end of the function, it's still
361 ;; considered as "in that function".
362 ;; E.g. `eval-defun' from right after the last close-paren.
364 (skip-chars-forward " \t")
365 (if (looking-at "\\s<\\|\n")
371 ;; We already moved forward by one because we started from
372 ;; within a function.
374 ;; We started from after the end of the previous function.
377 (beginning-of-defun-raw (- arg
))
378 (funcall end-of-defun-function
)))
382 ;; We already moved backward because we started from between
385 ;; We started from inside a function.
388 (beginning-of-defun-raw (- arg
))
389 (funcall end-of-defun-function
))))
391 (skip-chars-forward " \t")
392 (if (looking-at "\\s<\\|\n")
395 (defun mark-defun (&optional allow-extend
)
396 "Put mark at end of this defun, point at beginning.
397 The defun marked is the one that contains point or follows point.
399 Interactively, if this command is repeated
400 or (in Transient Mark mode) if the mark is active,
401 it marks the next defun after the ones already marked."
403 (cond ((and allow-extend
404 (or (and (eq last-command this-command
) (mark t
))
405 (and transient-mark-mode mark-active
)))
412 (let ((opoint (point))
415 ;; Try first in this order for the sake of languages with nested
416 ;; functions where several can end at the same place as with
417 ;; the offside rule, e.g. Python.
422 (while (looking-at "^\n")
424 (if (> (point) opoint
)
426 ;; We got the right defun.
427 (push-mark beg nil t
)
429 (exchange-point-and-mark))
430 ;; beginning-of-defun moved back one defun
431 ;; so we got the wrong one.
434 (push-mark (point) nil t
)
435 (beginning-of-defun))
436 (re-search-backward "^\n" (- (point) 1) t
)))))
438 (defun narrow-to-defun (&optional arg
)
439 "Make text outside current defun invisible.
440 The defun visible is the one that contains point or follows point.
441 Optional ARG is ignored."
445 (let ((opoint (point))
447 ;; Try first in this order for the sake of languages with nested
448 ;; functions where several can end at the same place as with
449 ;; the offside rule, e.g. Python.
454 (while (looking-at "^\n")
456 (unless (> (point) opoint
)
457 ;; beginning-of-defun moved back one defun
458 ;; so we got the wrong one.
465 (re-search-backward "^\n" (- (point) 1) t
)
466 (narrow-to-region beg end
))))
468 (defvar insert-pair-alist
469 '((?\
( ?\
)) (?\
[ ?\
]) (?\
{ ?\
}) (?\
< ?\
>) (?
\" ?
\") (?
\' ?
\') (?\
` ?
\'))
470 "Alist of paired characters inserted by `insert-pair'.
471 Each element looks like (OPEN-CHAR CLOSE-CHAR) or (COMMAND-CHAR
472 OPEN-CHAR CLOSE-CHAR). The characters OPEN-CHAR and CLOSE-CHAR
473 of the pair whose key is equal to the last input character with
474 or without modifiers, are inserted by `insert-pair'.")
476 (defun insert-pair (&optional arg open close
)
477 "Enclose following ARG sexps in a pair of OPEN and CLOSE characters.
478 Leave point after the first character.
479 A negative ARG encloses the preceding ARG sexps instead.
480 No argument is equivalent to zero: just insert characters
481 and leave point between.
482 If `parens-require-spaces' is non-nil, this command also inserts a space
483 before and after, depending on the surrounding characters.
484 If region is active, insert enclosing characters at region boundaries.
486 If arguments OPEN and CLOSE are nil, the character pair is found
487 from the variable `insert-pair-alist' according to the last input
488 character with or without modifiers. If no character pair is
489 found in the variable `insert-pair-alist', then the last input
490 character is inserted ARG times.
492 This command assumes point is not in a string or comment."
494 (if (not (and open close
))
495 (let ((pair (or (assq last-command-event insert-pair-alist
)
496 (assq (event-basic-type last-command-event
)
497 insert-pair-alist
))))
500 (setq open
(nth 1 pair
) close
(nth 2 pair
))
501 (setq open
(nth 0 pair
) close
(nth 1 pair
))))))
503 (if (and transient-mark-mode mark-active
)
505 (save-excursion (goto-char (region-end)) (insert close
))
506 (save-excursion (goto-char (region-beginning)) (insert open
)))
507 (if arg
(setq arg
(prefix-numeric-value arg
))
509 (cond ((> arg
0) (skip-chars-forward " \t"))
510 ((< arg
0) (forward-sexp arg
) (setq arg
(- arg
))))
511 (and parens-require-spaces
513 (memq (char-syntax (preceding-char)) (list ?w ?_
(char-syntax close
)))
517 (or (eq arg
0) (forward-sexp arg
))
519 (and parens-require-spaces
521 (memq (char-syntax (following-char)) (list ?w ?_
(char-syntax open
)))
523 (insert-char (event-basic-type last-command-event
)
524 (prefix-numeric-value arg
))))
526 (defun insert-parentheses (&optional arg
)
527 "Enclose following ARG sexps in parentheses.
528 Leave point after open-paren.
529 A negative ARG encloses the preceding ARG sexps instead.
530 No argument is equivalent to zero: just insert `()' and leave point between.
531 If `parens-require-spaces' is non-nil, this command also inserts a space
532 before and after, depending on the surrounding characters.
533 If region is active, insert enclosing characters at region boundaries.
535 This command assumes point is not in a string or comment."
537 (insert-pair arg ?\
( ?\
)))
539 (defun delete-pair ()
540 "Delete a pair of characters enclosing the sexp that follows point."
542 (save-excursion (forward-sexp 1) (delete-char -
1))
545 (defun raise-sexp (&optional arg
)
546 "Raise ARG sexps higher up the tree."
548 (let ((s (if (and transient-mark-mode mark-active
)
549 (buffer-substring (region-beginning) (region-end))
552 (save-excursion (forward-sexp arg
) (point))))))
554 (delete-region (point) (save-excursion (forward-sexp 1) (point)))
555 (save-excursion (insert s
))))
557 (defun move-past-close-and-reindent ()
558 "Move past next `)', delete indentation before it, then indent after it."
562 (while (save-excursion ; this is my contribution
563 (let ((before-paren (point)))
564 (back-to-indentation)
565 (and (= (point) before-paren
)
567 ;; Move to end of previous line.
570 ;; Verify it doesn't end within a string or comment.
574 ;; Get state at start of line.
575 (setq state
(list 0 nil nil
576 (null (calculate-lisp-indent))
579 ;; Parse state across the line to get state at end.
580 (setq state
(parse-partial-sexp (point) end nil nil
582 ;; Check not in string or comment.
583 (and (not (elt state
3)) (not (elt state
4))))))))
584 (delete-indentation))
586 (newline-and-indent))
588 (defun check-parens () ; lame name?
589 "Check for unbalanced parentheses in the current buffer.
590 More accurately, check the narrowed part of the buffer for unbalanced
591 expressions (\"sexps\") in general. This is done according to the
592 current syntax table and will find unbalanced brackets or quotes as
593 appropriate. (See Info node `(emacs)Parentheses'.) If imbalance is
594 found, an error is signaled and point is left at the first unbalanced
598 ;; Buffer can't have more than (point-max) sexps.
599 (scan-sexps (point-min) (point-max))
600 (scan-error (goto-char (nth 2 data
))
601 ;; Could print (nth 1 data), which is either
602 ;; "Containing expression ends prematurely" or
603 ;; "Unbalanced parentheses", but those may not be so
604 ;; accurate/helpful, e.g. quotes may actually be
606 (error "Unmatched bracket or quote"))))
608 (defun field-complete (table &optional predicate
)
609 (let ((minibuffer-completion-table table
)
610 (minibuffer-completion-predicate predicate
)
611 ;; This made sense for lisp-complete-symbol, but for
612 ;; field-complete, this is out of place. --Stef
613 ;; (completion-annotate-function
614 ;; (unless (eq predicate 'fboundp)
616 ;; (if (fboundp (intern-soft str)) " <f>"))))
618 (call-interactively 'minibuffer-complete
)))
620 (defun lisp-complete-symbol (&optional predicate
)
621 "Perform completion on Lisp symbol preceding point.
622 Compare that symbol against the known Lisp symbols.
623 If no characters can be completed, display a list of possible completions.
624 Repeating the command at that point scrolls the list.
626 When called from a program, optional arg PREDICATE is a predicate
627 determining which symbols are considered, e.g. `commandp'.
628 If PREDICATE is nil, the context determines which symbols are
629 considered. If the symbol starts just after an open-parenthesis, only
630 symbols with function definitions are considered. Otherwise, all
631 symbols with function definitions, values or properties are
634 (let* ((data (lisp-completion-at-point predicate
))
635 (plist (nthcdr 3 data
)))
637 (minibuffer-message "Nothing to complete")
638 (let ((completion-extra-properties plist
))
639 (completion-in-region (nth 0 data
) (nth 1 data
) (nth 2 data
)
640 (plist-get plist
:predicate
))))))
643 (defun lisp-completion-at-point (&optional predicate
)
644 "Function used for `completion-at-point-functions' in `emacs-lisp-mode'."
645 ;; FIXME: the `end' could be after point?
646 (with-syntax-table emacs-lisp-mode-syntax-table
648 (beg (condition-case nil
651 (skip-syntax-forward "'")
658 (if (not (eq (char-before) ?\
())
659 (lambda (sym) ;why not just nil ? -sm
660 (or (boundp sym
) (fboundp sym
)
662 ;; Looks like a funcall position. Let's double check.
663 (if (condition-case nil
664 (progn (up-list -
2) (forward-char 1)
665 (eq (char-after) ?\
())
667 ;; If the first element of the parent list is an open
668 ;; paren we are probably not in a funcall position.
669 ;; Maybe a `let' varlist or something.
671 ;; Else, we assume that a function name is expected.
674 (unless (or (eq beg
(point-max))
675 (member (char-syntax (char-after beg
)) '(?
\" ?\
( ?\
))))
680 (when (>= (point) pos
)
684 (list beg end obarray
687 (unless (eq predicate
'fboundp
)
688 (lambda (str) (if (fboundp (intern-soft str
)) " <f>"))))))))
690 ;;; lisp.el ends here