1 ;;; lisp.el --- Lisp editing commands for Emacs -*- lexical-binding:t -*-
3 ;; Copyright (C) 1985-1986, 1994, 2000-2017 Free Software Foundation,
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Keywords: lisp, languages
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
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 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 :type
'(choice (const nil
)
40 (make-variable-buffer-local 'defun-prompt-regexp
)
42 (defcustom parens-require-spaces t
43 "If non-nil, add whitespace as needed when inserting parentheses.
44 This affects `insert-parentheses' and `insert-pair'."
48 (defvar forward-sexp-function nil
50 ;; - for some uses, we may want a "sexp-only" version, which only
51 ;; jumps over a well-formed sexp, rather than some dwimish thing
52 ;; like jumping from an "else" back up to its "if".
53 ;; - for up-list, we could use the "sexp-only" behavior as well
54 ;; to treat the dwimish halfsexp as a form of "up-list" step.
55 "If non-nil, `forward-sexp' delegates to this function.
56 Should take the same arguments and behave similarly to `forward-sexp'.")
58 (defun forward-sexp (&optional arg
)
59 "Move forward across one balanced expression (sexp).
60 With ARG, do it that many times. Negative arg -N means move
61 backward across N balanced expressions. This command assumes
62 point is not in a string or comment. Calls
63 `forward-sexp-function' to do the work, if that is non-nil. If
64 unable to move over a sexp, signal `scan-error' with three
65 arguments: a message, the start of the obstacle (usually a
66 parenthesis or list marker of some kind), and end of the
70 (if forward-sexp-function
71 (funcall forward-sexp-function arg
)
72 (goto-char (or (scan-sexps (point) arg
) (buffer-end arg
)))
73 (if (< arg
0) (backward-prefix-chars))))
75 (defun backward-sexp (&optional arg
)
76 "Move backward across one balanced expression (sexp).
77 With ARG, do it that many times. Negative arg -N means
78 move forward across N balanced expressions.
79 This command assumes point is not in a string or comment.
80 Uses `forward-sexp' to do the work."
83 (forward-sexp (- arg
)))
85 (defun mark-sexp (&optional arg allow-extend
)
86 "Set mark ARG sexps from point.
87 The place mark goes is the same place \\[forward-sexp] would
88 move to with the same argument.
89 Interactively, if this command is repeated
90 or (in Transient Mark mode) if the mark is active,
91 it marks the next ARG sexps after the ones already marked.
92 This command assumes point is not in a string or comment."
94 (cond ((and allow-extend
95 (or (and (eq last-command this-command
) (mark t
))
96 (and transient-mark-mode mark-active
)))
97 (setq arg
(if arg
(prefix-numeric-value arg
)
98 (if (< (mark) (point)) -
1 1)))
107 (forward-sexp (prefix-numeric-value arg
))
111 (defun forward-list (&optional arg
)
112 "Move forward across one balanced group of parentheses.
113 This command will also work on other parentheses-like expressions
114 defined by the current language mode.
115 With ARG, do it that many times.
116 Negative arg -N means move backward across N groups of parentheses.
117 This command assumes point is not in a string or comment."
119 (or arg
(setq arg
1))
120 (goto-char (or (scan-lists (point) arg
0) (buffer-end arg
))))
122 (defun backward-list (&optional arg
)
123 "Move backward across one balanced group of parentheses.
124 This command will also work on other parentheses-like expressions
125 defined by the current language mode.
126 With ARG, do it that many times.
127 Negative arg -N means move forward across N groups of parentheses.
128 This command assumes point is not in a string or comment."
130 (or arg
(setq arg
1))
131 (forward-list (- arg
)))
133 (defun down-list (&optional arg
)
134 "Move forward down one level of parentheses.
135 This command will also work on other parentheses-like expressions
136 defined by the current language mode.
137 With ARG, do this that many times.
138 A negative argument means move backward but still go down a level.
139 This command assumes point is not in a string or comment."
141 (or arg
(setq arg
1))
142 (let ((inc (if (> arg
0) 1 -
1)))
144 (goto-char (or (scan-lists (point) inc -
1) (buffer-end arg
)))
145 (setq arg
(- arg inc
)))))
147 (defun backward-up-list (&optional arg escape-strings no-syntax-crossing
)
148 "Move backward out of one level of parentheses.
149 This command will also work on other parentheses-like expressions
150 defined by the current language mode. With ARG, do this that
151 many times. A negative argument means move forward but still to
152 a less deep spot. If ESCAPE-STRINGS is non-nil (as it is
153 interactively), move out of enclosing strings as well. If
154 NO-SYNTAX-CROSSING is non-nil (as it is interactively), prefer to
155 break out of any enclosing string instead of moving to the start
156 of a list broken across multiple strings. On error, location of
157 point is unspecified."
158 (interactive "^p\nd\nd")
159 (up-list (- (or arg
1)) escape-strings no-syntax-crossing
))
161 (defun up-list (&optional arg escape-strings no-syntax-crossing
)
162 "Move forward out of one level of parentheses.
163 This command will also work on other parentheses-like expressions
164 defined by the current language mode. With ARG, do this that
165 many times. A negative argument means move backward but still to
166 a less deep spot. If ESCAPE-STRINGS is non-nil (as it is
167 interactively), move out of enclosing strings as well. If
168 NO-SYNTAX-CROSSING is non-nil (as it is interactively), prefer to
169 break out of any enclosing string instead of moving to the start
170 of a list broken across multiple strings. On error, location of
171 point is unspecified."
172 (interactive "^p\nd\nd")
173 (or arg
(setq arg
1))
174 (let ((inc (if (> arg
0) 1 -
1))
179 ;; If we've been asked not to cross string boundaries
180 ;; and we're inside a string, narrow to that string so
181 ;; that scan-lists doesn't find a match in a different
183 (when no-syntax-crossing
184 (let* ((syntax (syntax-ppss))
185 (string-comment-start (nth 8 syntax
)))
186 (when string-comment-start
188 (goto-char string-comment-start
)
191 (if (nth 3 syntax
) ; in string
193 (progn (forward-sexp) (point))
194 (scan-error (point-max)))
197 (if (null forward-sexp-function
)
198 (goto-char (or (scan-lists (point) inc
1)
201 (while (progn (setq pos
(point))
204 (scan-error (goto-char (nth (if (> arg
0) 3 2) err
))))
207 (list "Unbalanced parentheses" (point) (point))))))
211 ;; If we bumped up against the end of a list, see whether
212 ;; we're inside a string: if so, just go to the beginning
213 ;; or end of that string.
215 (or syntax
(setf syntax
(syntax-ppss)))
217 (goto-char (nth 8 syntax
))
218 (progn (when (> inc
0)
221 ;; If we narrowed to a comment above and failed to escape
222 ;; it, the error might be our fault, not an indication
223 ;; that we're out of syntax. Try again from beginning or
224 ;; end of the comment.
225 (and no-syntax-crossing
226 (or syntax
(setf syntax
(syntax-ppss)))
228 (goto-char (nth 8 syntax
))
231 (setf arg
(+ arg inc
)))
232 (signal (car err
) (cdr err
))))))
233 (setq arg
(- arg inc
)))))
235 (defun kill-sexp (&optional arg
)
236 "Kill the sexp (balanced expression) following point.
237 With ARG, kill that many sexps after point.
238 Negative arg -N means kill N sexps before point.
239 This command assumes point is not in a string or comment."
241 (let ((opoint (point)))
242 (forward-sexp (or arg
1))
243 (kill-region opoint
(point))))
245 (defun backward-kill-sexp (&optional arg
)
246 "Kill the sexp (balanced expression) preceding point.
247 With ARG, kill that many sexps before point.
248 Negative arg -N means kill N sexps after point.
249 This command assumes point is not in a string or comment."
251 (kill-sexp (- (or arg
1))))
254 (defun kill-backward-up-list (&optional arg
)
255 "Kill the form containing the current sexp, leaving the sexp itself.
256 A prefix argument ARG causes the relevant number of surrounding
258 This command assumes point is not in a string or comment."
260 (let ((current-sexp (thing-at-point 'sexp
)))
263 (backward-up-list arg
)
265 (insert current-sexp
))
266 (user-error "Not at a sexp"))))
268 (defvar beginning-of-defun-function nil
269 "If non-nil, function for `beginning-of-defun-raw' to call.
270 This is used to find the beginning of the defun instead of using the
271 normal recipe (see `beginning-of-defun'). Major modes can define this
272 if defining `defun-prompt-regexp' is not sufficient to handle the mode's
275 The function takes the same argument as `beginning-of-defun' and should
276 behave similarly, returning non-nil if it found the beginning of a defun.
277 Ideally it should move to a point right before an open-paren which encloses
278 the body of the defun.")
280 (defun beginning-of-defun (&optional arg
)
281 "Move backward to the beginning of a defun.
282 With ARG, do it that many times. Negative ARG means move forward
283 to the ARGth following beginning of defun.
285 If search is successful, return t; point ends up at the beginning
286 of the line where the search succeeded. Otherwise, return nil.
288 When `open-paren-in-column-0-is-defun-start' is non-nil, a defun
289 is assumed to start where there is a char with open-parenthesis
290 syntax at the beginning of a line. If `defun-prompt-regexp' is
291 non-nil, then a string which matches that regexp may also precede
292 the open-parenthesis. If `defun-prompt-regexp' and
293 `open-paren-in-column-0-is-defun-start' are both nil, this
294 function instead finds an open-paren at the outermost level.
296 If the variable `beginning-of-defun-function' is non-nil, its
297 value is called as a function, with argument ARG, to find the
300 Regardless of the values of `defun-prompt-regexp' and
301 `beginning-of-defun-function', point always moves to the
302 beginning of the line whenever the search is successful."
304 (or (not (eq this-command
'beginning-of-defun
))
305 (eq last-command
'beginning-of-defun
)
306 (and transient-mark-mode mark-active
)
308 (and (beginning-of-defun-raw arg
)
309 (progn (beginning-of-line) t
)))
311 (defun beginning-of-defun-raw (&optional arg
)
312 "Move point to the character that starts a defun.
313 This is identical to function `beginning-of-defun', except that point
314 does not move to the beginning of the line when `defun-prompt-regexp'
317 If variable `beginning-of-defun-function' is non-nil, its value
318 is called as a function to find the defun's beginning."
319 (interactive "^p") ; change this to "P", maybe, if we ever come to pass ARG
320 ; to beginning-of-defun-function.
321 (unless arg
(setq arg
1))
323 (beginning-of-defun-function
325 (funcall beginning-of-defun-function arg
)
326 ;; We used to define beginning-of-defun-function as taking no argument
327 ;; but that makes it impossible to implement correct forward motion:
328 ;; we used to use end-of-defun for that, but it's not supposed to do
329 ;; the same thing (it moves to the end of a defun not to the beginning
331 ;; In case the beginning-of-defun-function uses the old calling
332 ;; convention, fallback on the old implementation.
333 (wrong-number-of-arguments
336 (funcall beginning-of-defun-function
))
338 (funcall end-of-defun-function
))))))
340 ((or defun-prompt-regexp open-paren-in-column-0-is-defun-start
)
341 (and (< arg
0) (not (eobp)) (forward-char 1))
346 (if defun-prompt-regexp
347 (concat (if open-paren-in-column-0-is-defun-start
349 "\\(?:" defun-prompt-regexp
"\\)\\s(")
352 (nth 8 (syntax-ppss))))
354 (progn (goto-char (1- (match-end 0)))
357 ;; If open-paren-in-column-0-is-defun-start and defun-prompt-regexp
358 ;; are both nil, column 0 has no significance - so scan forward
359 ;; from BOB to see how nested point is, then carry on from there.
361 ;; It is generally not a good idea to land up here, because the
362 ;; call to scan-lists below can be extremely slow. This is because
363 ;; back_comment in syntax.c may have to scan from bob to find the
364 ;; beginning of each comment. Fixing this is not trivial -- cyd.
368 (let ((floor (point-min))
369 (ceiling (point-max))
373 (let ((ppss (let (syntax-begin-function)
375 ;; position of least enclosing paren, or nil.
377 ;; Back out of any comment/string, so that encl-pos will always
378 ;; become nil if we're at top-level.
380 (goto-char (nth 8 ppss
))
381 (setq ppss
(syntax-ppss))) ; should be fast, due to cache.
382 (setq encl-pos
(syntax-ppss-toplevel-pos ppss
))
383 (if encl-pos
(goto-char encl-pos
))
385 (and encl-pos arg-
+ve
(setq arg
(1- arg
)))
386 (and (not encl-pos
) (not arg-
+ve
) (not (looking-at "\\s("))
389 (condition-case nil
; to catch crazy parens.
391 (goto-char (scan-lists (point) (- arg
) 0))
393 (if (>= (point) floor
)
397 ;; forward to next (, or trigger the c-c
398 (goto-char (1- (scan-lists (point) 1 -
1)))
399 (if (<= (point) ceiling
)
404 (goto-char (if arg-
+ve floor ceiling
))
407 (defun beginning-of-defun--in-emptyish-line-p ()
408 "Return non-nil if the point is in an \"emptyish\" line.
409 This means a line that consists entirely of comments and/or
411 ;; See https://lists.gnu.org/r/help-gnu-emacs/2016-08/msg00141.html
414 (< (line-end-position)
415 (let ((ppss (syntax-ppss)))
417 (goto-char (nth 8 ppss
)))
418 (forward-comment (point-max))
421 (defun beginning-of-defun-comments (&optional arg
)
422 "Move to the beginning of ARGth defun, including comments."
424 (unless arg
(setq arg
1))
425 (beginning-of-defun arg)
427 (while (let ((ppss (progn (setq first-line-p
(= (forward-line -
1) -
1))
428 (syntax-ppss (line-end-position)))))
429 (while (and (nth 4 ppss
) ; If eol is in a line-spanning comment,
430 (< (nth 8 ppss
) (line-beginning-position)))
431 (goto-char (nth 8 ppss
)) ; skip to comment start.
432 (setq ppss
(syntax-ppss (line-end-position))))
433 (and (not first-line-p
)
434 (progn (skip-syntax-backward
435 "-" (line-beginning-position))
436 (not (bolp))) ; Check for blank line.
437 (progn (parse-partial-sexp
438 (line-beginning-position) (line-end-position)
439 nil t
(syntax-ppss (line-beginning-position)))
440 (eolp))))) ; Check for non-comment text.
441 (forward-line (if first-line-p
0 1))))
443 (defvar end-of-defun-function
444 (lambda () (forward-sexp 1))
445 "Function for `end-of-defun' to call.
446 This is used to find the end of the defun at point.
447 It is called with no argument, right after calling `beginning-of-defun-raw'.
448 So the function can assume that point is at the beginning of the defun body.
449 It should move point to the first position after the defun.")
451 (defun buffer-end (arg)
452 "Return the \"far end\" position of the buffer, in direction ARG.
453 If ARG is positive, that's the end of the buffer.
454 Otherwise, that's the beginning of the buffer."
455 (if (> arg
0) (point-max) (point-min)))
457 (defun end-of-defun (&optional arg
)
458 "Move forward to next end of defun.
459 With argument, do it that many times.
460 Negative argument -N means move back to Nth preceding end of defun.
462 An end of a defun occurs right after the close-parenthesis that
463 matches the open-parenthesis that starts a defun; see function
464 `beginning-of-defun'.
466 If variable `end-of-defun-function' is non-nil, its value
467 is called as a function to find the defun's end."
469 (or (not (eq this-command
'end-of-defun
))
470 (eq last-command
'end-of-defun
)
471 (and transient-mark-mode mark-active
)
473 (if (or (null arg
) (= arg
0)) (setq arg
1))
475 (beg (progn (end-of-line 1) (beginning-of-defun-raw 1) (point)))
477 ;; When comparing point against pos, we want to consider that if
478 ;; point was right after the end of the function, it's still
479 ;; considered as "in that function".
480 ;; E.g. `eval-defun' from right after the last close-paren.
482 (skip-chars-forward " \t")
483 (if (looking-at "\\s<\\|\n")
484 (forward-line 1))))))
485 (funcall end-of-defun-function
)
491 ;; We already moved forward by one because we started from
492 ;; within a function.
494 ;; We started from after the end of the previous function.
497 (beginning-of-defun-raw (- arg
))
498 (funcall end-of-defun-function
)))
502 ;; We already moved backward because we started from between
505 ;; We started from inside a function.
508 (beginning-of-defun-raw (- arg
))
510 (funcall end-of-defun-function
))))
512 (while (and (< arg
0) (>= (point) pos
))
513 ;; We intended to move backward, but this ended up not doing so:
516 (beginning-of-defun-raw (- arg
))
520 (funcall end-of-defun-function
)
523 (defun mark-defun (&optional arg
)
524 "Put mark at end of this defun, point at beginning.
525 The defun marked is the one that contains point or follows point.
526 With positive ARG, mark this and that many next defuns; with negative
527 ARG, change the direction of marking.
529 If the mark is active, it marks the next or previous defun(s) after
530 the one(s) already marked."
532 (setq arg
(or arg
1))
533 ;; There is no `mark-defun-back' function - see
534 ;; https://lists.gnu.org/r/bug-gnu-emacs/2016-11/msg00079.html
536 (when (eq last-command
'mark-defun-back
)
539 (setq this-command
'mark-defun-back
))
540 (cond ((use-region-p)
545 ;; change the dotimes below to (end-of-defun arg) once bug #24427 is fixed
546 (dotimes (_ignore arg
)
549 (beginning-of-defun-comments (- arg
))))
551 (let ((opoint (point))
554 ;; Try first in this order for the sake of languages with nested
555 ;; functions where several can end at the same place as with the
556 ;; offside rule, e.g. Python.
557 (beginning-of-defun-comments)
561 (when (or (and (<= (point) opoint
)
563 (= beg
(point-min))) ; we were before the first defun!
564 ;; beginning-of-defun moved back one defun so we got the wrong
565 ;; one. If ARG < 0, however, we actually want to go back.
569 (beginning-of-defun-comments)
573 ;; change the dotimes below to (end-of-defun arg) once bug #24427 is fixed
574 (dotimes (_ignore arg
)
577 (push-mark end nil t
)
581 (unless (= arg -
1) ; beginning-of-defun behaves
582 ; strange with zero arg - see
583 ; https://lists.gnu.org/r/bug-gnu-emacs/2017-02/msg00196.html
584 (beginning-of-defun (1- (- arg
))))
585 (push-mark end nil t
))))))
586 (skip-chars-backward "[:space:]\n")
590 (defvar narrow-to-defun-include-comments nil
591 "If non-nil, `narrow-to-defun' will also show comments preceding the defun.")
593 (defun narrow-to-defun (&optional include-comments
)
594 "Make text outside current defun invisible.
595 The current defun is the one that contains point or follows point.
596 Preceding comments are included if INCLUDE-COMMENTS is non-nil.
597 Interactively, the behavior depends on `narrow-to-defun-include-comments'."
598 (interactive (list narrow-to-defun-include-comments
))
601 (let ((opoint (point))
603 ;; Try first in this order for the sake of languages with nested
604 ;; functions where several can end at the same place as with
605 ;; the offside rule, e.g. Python.
607 ;; Finding the start of the function is a bit problematic since
608 ;; `beginning-of-defun' when we are on the first character of
609 ;; the function might go to the previous function.
611 ;; Therefore we first move one character forward and then call
612 ;; `beginning-of-defun'. However now we must check that we did
613 ;; not move into the next function.
614 (let ((here (point)))
618 (when (< (point) here
)
620 (beginning-of-defun)))
624 (while (looking-at "^\n")
626 (unless (> (point) opoint
)
627 ;; beginning-of-defun moved back one defun
628 ;; so we got the wrong one.
634 (when include-comments
636 ;; Move back past all preceding comments (and whitespace).
637 (when (forward-comment -
1)
638 (while (forward-comment -
1))
639 ;; Move forwards past any page breaks within these comments.
640 (when (and page-delimiter
(not (string= page-delimiter
"")))
641 (while (re-search-forward page-delimiter beg t
)))
642 ;; Lastly, move past any empty lines.
643 (skip-chars-forward "[:space:]\n")
647 (re-search-backward "^\n" (- (point) 1) t
)
648 (narrow-to-region beg end
))))
650 (defvar insert-pair-alist
651 '((?\
( ?\
)) (?\
[ ?\
]) (?\
{ ?\
}) (?\
< ?\
>) (?
\" ?
\") (?
\' ?
\') (?\
` ?
\'))
652 "Alist of paired characters inserted by `insert-pair'.
653 Each element looks like (OPEN-CHAR CLOSE-CHAR) or (COMMAND-CHAR
654 OPEN-CHAR CLOSE-CHAR). The characters OPEN-CHAR and CLOSE-CHAR
655 of the pair whose key is equal to the last input character with
656 or without modifiers, are inserted by `insert-pair'.
658 If COMMAND-CHAR is specified, it is a character that triggers the
659 insertion of the open/close pair, and COMMAND-CHAR itself isn't
662 (defun insert-pair (&optional arg open close
)
663 "Enclose following ARG sexps in a pair of OPEN and CLOSE characters.
664 Leave point after the first character.
665 A negative ARG encloses the preceding ARG sexps instead.
666 No argument is equivalent to zero: just insert characters
667 and leave point between.
668 If `parens-require-spaces' is non-nil, this command also inserts a space
669 before and after, depending on the surrounding characters.
670 If region is active, insert enclosing characters at region boundaries.
672 If arguments OPEN and CLOSE are nil, the character pair is found
673 from the variable `insert-pair-alist' according to the last input
674 character with or without modifiers. If no character pair is
675 found in the variable `insert-pair-alist', then the last input
676 character is inserted ARG times.
678 This command assumes point is not in a string or comment."
680 (if (not (and open close
))
681 (let ((pair (or (assq last-command-event insert-pair-alist
)
682 (assq (event-basic-type last-command-event
)
683 insert-pair-alist
))))
686 (setq open
(nth 1 pair
) close
(nth 2 pair
))
687 (setq open
(nth 0 pair
) close
(nth 1 pair
))))))
689 (if (and transient-mark-mode mark-active
)
692 (goto-char (region-end))
694 (goto-char (region-beginning))
696 (if arg
(setq arg
(prefix-numeric-value arg
))
698 (cond ((> arg
0) (skip-chars-forward " \t"))
699 ((< arg
0) (forward-sexp arg
) (setq arg
(- arg
))))
700 (and parens-require-spaces
702 (memq (char-syntax (preceding-char)) (list ?w ?_
(char-syntax close
)))
706 (or (eq arg
0) (forward-sexp arg
))
708 (and parens-require-spaces
710 (memq (char-syntax (following-char)) (list ?w ?_
(char-syntax open
)))
712 (insert-char (event-basic-type last-command-event
)
713 (prefix-numeric-value arg
))))
715 (defun insert-parentheses (&optional arg
)
716 "Enclose following ARG sexps in parentheses.
717 Leave point after open-paren.
718 A negative ARG encloses the preceding ARG sexps instead.
719 No argument is equivalent to zero: just insert `()' and leave point between.
720 If `parens-require-spaces' is non-nil, this command also inserts a space
721 before and after, depending on the surrounding characters.
722 If region is active, insert enclosing characters at region boundaries.
724 This command assumes point is not in a string or comment."
726 (insert-pair arg ?\
( ?\
)))
728 (defun delete-pair ()
729 "Delete a pair of characters enclosing the sexp that follows point."
731 (save-excursion (forward-sexp 1) (delete-char -
1))
734 (defun raise-sexp (&optional arg
)
735 "Raise ARG sexps higher up the tree."
737 (let ((s (if (and transient-mark-mode mark-active
)
738 (buffer-substring (region-beginning) (region-end))
741 (save-excursion (forward-sexp arg
) (point))))))
743 (delete-region (point) (save-excursion (forward-sexp 1) (point)))
744 (save-excursion (insert s
))))
746 (defun move-past-close-and-reindent ()
747 "Move past next `)', delete indentation before it, then indent after it."
751 (while (save-excursion ; this is my contribution
752 (let ((before-paren (point)))
753 (back-to-indentation)
754 (and (= (point) before-paren
)
756 ;; Move to end of previous line.
759 ;; Verify it doesn't end within a string or comment.
763 ;; Get state at start of line.
764 (setq state
(list 0 nil nil
765 (null (calculate-lisp-indent))
768 ;; Parse state across the line to get state at end.
769 (setq state
(parse-partial-sexp (point) end nil nil
771 ;; Check not in string or comment.
772 (and (not (elt state
3)) (not (elt state
4))))))))
773 (delete-indentation))
775 (newline-and-indent))
777 (defun check-parens () ; lame name?
778 "Check for unbalanced parentheses in the current buffer.
779 More accurately, check the narrowed part of the buffer for unbalanced
780 expressions (\"sexps\") in general. This is done according to the
781 current syntax table and will find unbalanced brackets or quotes as
782 appropriate. (See Info node `(emacs)Parentheses'.) If imbalance is
783 found, an error is signaled and point is left at the first unbalanced
787 ;; Buffer can't have more than (point-max) sexps.
788 (scan-sexps (point-min) (point-max))
789 (scan-error (push-mark)
790 (goto-char (nth 2 data
))
791 ;; Could print (nth 1 data), which is either
792 ;; "Containing expression ends prematurely" or
793 ;; "Unbalanced parentheses", but those may not be so
794 ;; accurate/helpful, e.g. quotes may actually be
796 (user-error "Unmatched bracket or quote"))))
798 (defun field-complete (table &optional predicate
)
799 (declare (obsolete completion-in-region
"24.4"))
800 (let ((minibuffer-completion-table table
)
801 (minibuffer-completion-predicate predicate
)
802 ;; This made sense for lisp-complete-symbol, but for
803 ;; field-complete, this is out of place. --Stef
804 ;; (completion-annotate-function
805 ;; (unless (eq predicate 'fboundp)
807 ;; (if (fboundp (intern-soft str)) " <f>"))))
809 (call-interactively 'minibuffer-complete
)))
811 (defun lisp-complete-symbol (&optional _predicate
)
812 "Perform completion on Lisp symbol preceding point.
813 Compare that symbol against the known Lisp symbols.
814 If no characters can be completed, display a list of possible completions.
815 Repeating the command at that point scrolls the list.
817 The context determines which symbols are considered. If the
818 symbol starts just after an open-parenthesis, only symbols with
819 function definitions are considered. Otherwise, all symbols with
820 function definitions, values or properties are considered."
821 (declare (obsolete completion-at-point
"24.4")
822 (advertised-calling-convention () "25.1"))
824 (let* ((data (elisp-completion-at-point))
825 (plist (nthcdr 3 data
)))
827 (minibuffer-message "Nothing to complete")
828 (let ((completion-extra-properties plist
))
829 (completion-in-region (nth 0 data
) (nth 1 data
) (nth 2 data
)
830 (plist-get plist
:predicate
))))))
832 ;;; lisp.el ends here