Merge branch 'master' into comment-cache
[emacs.git] / lisp / emacs-lisp / lisp.el
blob0172e3af261c5d62a84daa850d9fbc5f8cead11e
1 ;;; lisp.el --- Lisp editing commands for Emacs -*- lexical-binding:t -*-
3 ;; Copyright (C) 1985-1986, 1994, 2000-2017 Free Software Foundation,
4 ;; Inc.
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Keywords: lisp, languages
8 ;; Package: emacs
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 <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; Lisp editing commands to go with Lisp major mode. More-or-less
28 ;; applicable in other modes too.
30 ;;; Code:
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)
38 regexp)
39 :group 'lisp)
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'."
45 :type 'boolean
46 :group 'lisp)
48 (defvar forward-sexp-function nil
49 ;; FIXME:
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
67 obstacle."
68 (interactive "^p")
69 (or arg (setq arg 1))
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."
81 (interactive "^p")
82 (or arg (setq arg 1))
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."
93 (interactive "P\np")
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)))
99 (set-mark
100 (save-excursion
101 (goto-char (mark))
102 (forward-sexp arg)
103 (point))))
105 (push-mark
106 (save-excursion
107 (forward-sexp (prefix-numeric-value arg))
108 (point))
109 nil t))))
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."
118 (interactive "^p")
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."
129 (interactive "^p")
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."
140 (interactive "^p")
141 (or arg (setq arg 1))
142 (let ((inc (if (> arg 0) 1 -1)))
143 (while (/= arg 0)
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))
175 (pos nil))
176 (while (/= arg 0)
177 (condition-case err
178 (save-restriction
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
182 ;; string.
183 (when no-syntax-crossing
184 (let* ((syntax (syntax-ppss))
185 (string-comment-start (nth 8 syntax)))
186 (when string-comment-start
187 (save-excursion
188 (goto-char string-comment-start)
189 (narrow-to-region
190 (point)
191 (if (nth 3 syntax) ; in string
192 (condition-case nil
193 (progn (forward-sexp) (point))
194 (scan-error (point-max)))
195 (forward-comment 1)
196 (point)))))))
197 (if (null forward-sexp-function)
198 (goto-char (or (scan-lists (point) inc 1)
199 (buffer-end arg)))
200 (condition-case err
201 (while (progn (setq pos (point))
202 (forward-sexp inc)
203 (/= (point) pos)))
204 (scan-error (goto-char (nth (if (> arg 0) 3 2) err))))
205 (if (= (point) pos)
206 (signal 'scan-error
207 (list "Unbalanced parentheses" (point) (point))))))
208 (scan-error
209 (let ((syntax nil))
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.
214 (and escape-strings
215 (or syntax (setf syntax (syntax-ppss)))
216 (nth 3 syntax)
217 (goto-char (nth 8 syntax))
218 (progn (when (> inc 0)
219 (forward-sexp))
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)))
227 (nth 4 syntax)
228 (goto-char (nth 8 syntax))
229 (or (< inc 0)
230 (forward-comment 1))
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."
240 (interactive "p")
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."
250 (interactive "p")
251 (kill-sexp (- (or arg 1))))
253 ;; After Zmacs:
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
257 forms to be removed.
258 This command assumes point is not in a string or comment."
259 (interactive "*p")
260 (let ((current-sexp (thing-at-point 'sexp)))
261 (if current-sexp
262 (save-excursion
263 (backward-up-list arg)
264 (kill-sexp)
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
273 needs.
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
298 defun's beginning.
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."
303 (interactive "^p")
304 (or (not (eq this-command 'beginning-of-defun))
305 (eq last-command 'beginning-of-defun)
306 (and transient-mark-mode mark-active)
307 (push-mark))
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'
315 is non-nil.
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))
322 (cond
323 (beginning-of-defun-function
324 (condition-case nil
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
330 ;; of the next).
331 ;; In case the beginning-of-defun-function uses the old calling
332 ;; convention, fallback on the old implementation.
333 (wrong-number-of-arguments
334 (if (> arg 0)
335 (dotimes (_ arg)
336 (funcall beginning-of-defun-function))
337 (dotimes (_ (- arg))
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))
342 (and (re-search-backward (if defun-prompt-regexp
343 (concat (if open-paren-in-column-0-is-defun-start
344 "^\\s(\\|" "")
345 "\\(?:" defun-prompt-regexp "\\)\\s(")
346 "^\\s(")
347 nil 'move arg)
348 (progn (goto-char (1- (match-end 0)))
349 t)))
351 ;; If open-paren-in-column-0-is-defun-start and defun-prompt-regexp
352 ;; are both nil, column 0 has no significance - so scan forward
353 ;; from BOB to see how nested point is, then carry on from there.
355 ;; It is generally not a good idea to land up here, because the
356 ;; call to scan-lists below can be extremely slow. This is because
357 ;; back_comment in syntax.c may have to scan from bob to find the
358 ;; beginning of each comment. Fixing this is not trivial -- cyd.
360 ((eq arg 0))
362 (let ((floor (point-min))
363 (ceiling (point-max))
364 (arg-+ve (> arg 0)))
365 (save-restriction
366 (widen)
367 (let ((ppss (let (syntax-begin-function)
368 (syntax-ppss)))
369 ;; position of least enclosing paren, or nil.
370 encl-pos)
371 ;; Back out of any comment/string, so that encl-pos will always
372 ;; become nil if we're at top-level.
373 (when (nth 8 ppss)
374 (goto-char (nth 8 ppss))
375 (setq ppss (syntax-ppss))) ; should be fast, due to cache.
376 (setq encl-pos (syntax-ppss-toplevel-pos ppss))
377 (if encl-pos (goto-char encl-pos))
379 (and encl-pos arg-+ve (setq arg (1- arg)))
380 (and (not encl-pos) (not arg-+ve) (not (looking-at "\\s("))
381 (setq arg (1+ arg)))
383 (condition-case nil ; to catch crazy parens.
384 (progn
385 (goto-char (scan-lists (point) (- arg) 0))
386 (if arg-+ve
387 (if (>= (point) floor)
389 (goto-char floor)
390 nil)
391 ;; forward to next (, or trigger the c-c
392 (goto-char (1- (scan-lists (point) 1 -1)))
393 (if (<= (point) ceiling)
395 (goto-char ceiling)
396 nil)))
397 (error
398 (goto-char (if arg-+ve floor ceiling))
399 nil))))))))
401 (defvar end-of-defun-function
402 (lambda () (forward-sexp 1))
403 "Function for `end-of-defun' to call.
404 This is used to find the end of the defun at point.
405 It is called with no argument, right after calling `beginning-of-defun-raw'.
406 So the function can assume that point is at the beginning of the defun body.
407 It should move point to the first position after the defun.")
409 (defun buffer-end (arg)
410 "Return the \"far end\" position of the buffer, in direction ARG.
411 If ARG is positive, that's the end of the buffer.
412 Otherwise, that's the beginning of the buffer."
413 (if (> arg 0) (point-max) (point-min)))
415 (defun end-of-defun (&optional arg)
416 "Move forward to next end of defun.
417 With argument, do it that many times.
418 Negative argument -N means move back to Nth preceding end of defun.
420 An end of a defun occurs right after the close-parenthesis that
421 matches the open-parenthesis that starts a defun; see function
422 `beginning-of-defun'.
424 If variable `end-of-defun-function' is non-nil, its value
425 is called as a function to find the defun's end."
426 (interactive "^p")
427 (or (not (eq this-command 'end-of-defun))
428 (eq last-command 'end-of-defun)
429 (and transient-mark-mode mark-active)
430 (push-mark))
431 (if (or (null arg) (= arg 0)) (setq arg 1))
432 (let ((pos (point))
433 (beg (progn (end-of-line 1) (beginning-of-defun-raw 1) (point)))
434 (skip (lambda ()
435 ;; When comparing point against pos, we want to consider that if
436 ;; point was right after the end of the function, it's still
437 ;; considered as "in that function".
438 ;; E.g. `eval-defun' from right after the last close-paren.
439 (unless (bolp)
440 (skip-chars-forward " \t")
441 (if (looking-at "\\s<\\|\n")
442 (forward-line 1))))))
443 (funcall end-of-defun-function)
444 (funcall skip)
445 (cond
446 ((> arg 0)
447 ;; Moving forward.
448 (if (> (point) pos)
449 ;; We already moved forward by one because we started from
450 ;; within a function.
451 (setq arg (1- arg))
452 ;; We started from after the end of the previous function.
453 (goto-char pos))
454 (unless (zerop arg)
455 (beginning-of-defun-raw (- arg))
456 (funcall end-of-defun-function)))
457 ((< arg 0)
458 ;; Moving backward.
459 (if (< (point) pos)
460 ;; We already moved backward because we started from between
461 ;; two functions.
462 (setq arg (1+ arg))
463 ;; We started from inside a function.
464 (goto-char beg))
465 (unless (zerop arg)
466 (beginning-of-defun-raw (- arg))
467 (setq beg (point))
468 (funcall end-of-defun-function))))
469 (funcall skip)
470 (while (and (< arg 0) (>= (point) pos))
471 ;; We intended to move backward, but this ended up not doing so:
472 ;; Try harder!
473 (goto-char beg)
474 (beginning-of-defun-raw (- arg))
475 (if (>= (point) beg)
476 (setq arg 0)
477 (setq beg (point))
478 (funcall end-of-defun-function)
479 (funcall skip)))))
481 (defun mark-defun (&optional allow-extend)
482 "Put mark at end of this defun, point at beginning.
483 The defun marked is the one that contains point or follows point.
485 Interactively, if this command is repeated
486 or (in Transient Mark mode) if the mark is active,
487 it marks the next defun after the ones already marked."
488 (interactive "p")
489 (cond ((and allow-extend
490 (or (and (eq last-command this-command) (mark t))
491 (and transient-mark-mode mark-active)))
492 (set-mark
493 (save-excursion
494 (goto-char (mark))
495 (end-of-defun)
496 (point))))
498 (let ((opoint (point))
499 beg end)
500 (push-mark opoint)
501 ;; Try first in this order for the sake of languages with nested
502 ;; functions where several can end at the same place as with
503 ;; the offside rule, e.g. Python.
504 (beginning-of-defun)
505 (setq beg (point))
506 (end-of-defun)
507 (setq end (point))
508 (while (looking-at "^\n")
509 (forward-line 1))
510 (if (> (point) opoint)
511 (progn
512 ;; We got the right defun.
513 (push-mark beg nil t)
514 (goto-char end)
515 (exchange-point-and-mark))
516 ;; beginning-of-defun moved back one defun
517 ;; so we got the wrong one.
518 (goto-char opoint)
519 (end-of-defun)
520 (push-mark (point) nil t)
521 (beginning-of-defun))
522 (re-search-backward "^\n" (- (point) 1) t)))))
524 (defvar narrow-to-defun-include-comments nil
525 "If non-nil, `narrow-to-defun' will also show comments preceding the defun.")
527 (defun narrow-to-defun (&optional include-comments)
528 "Make text outside current defun invisible.
529 The current defun is the one that contains point or follows point.
530 Preceding comments are included if INCLUDE-COMMENTS is non-nil.
531 Interactively, the behavior depends on `narrow-to-defun-include-comments'."
532 (interactive (list narrow-to-defun-include-comments))
533 (save-excursion
534 (widen)
535 (let ((opoint (point))
536 beg end)
537 ;; Try first in this order for the sake of languages with nested
538 ;; functions where several can end at the same place as with
539 ;; the offside rule, e.g. Python.
541 ;; Finding the start of the function is a bit problematic since
542 ;; `beginning-of-defun' when we are on the first character of
543 ;; the function might go to the previous function.
545 ;; Therefore we first move one character forward and then call
546 ;; `beginning-of-defun'. However now we must check that we did
547 ;; not move into the next function.
548 (let ((here (point)))
549 (unless (eolp)
550 (forward-char))
551 (beginning-of-defun)
552 (when (< (point) here)
553 (goto-char here)
554 (beginning-of-defun)))
555 (setq beg (point))
556 (end-of-defun)
557 (setq end (point))
558 (while (looking-at "^\n")
559 (forward-line 1))
560 (unless (> (point) opoint)
561 ;; beginning-of-defun moved back one defun
562 ;; so we got the wrong one.
563 (goto-char opoint)
564 (end-of-defun)
565 (setq end (point))
566 (beginning-of-defun)
567 (setq beg (point)))
568 (when include-comments
569 (goto-char beg)
570 ;; Move back past all preceding comments (and whitespace).
571 (when (forward-comment -1)
572 (while (forward-comment -1))
573 ;; Move forwards past any page breaks within these comments.
574 (when (and page-delimiter (not (string= page-delimiter "")))
575 (while (re-search-forward page-delimiter beg t)))
576 ;; Lastly, move past any empty lines.
577 (skip-chars-forward "[:space:]\n")
578 (beginning-of-line)
579 (setq beg (point))))
580 (goto-char end)
581 (re-search-backward "^\n" (- (point) 1) t)
582 (narrow-to-region beg end))))
584 (defvar insert-pair-alist
585 '((?\( ?\)) (?\[ ?\]) (?\{ ?\}) (?\< ?\>) (?\" ?\") (?\' ?\') (?\` ?\'))
586 "Alist of paired characters inserted by `insert-pair'.
587 Each element looks like (OPEN-CHAR CLOSE-CHAR) or (COMMAND-CHAR
588 OPEN-CHAR CLOSE-CHAR). The characters OPEN-CHAR and CLOSE-CHAR
589 of the pair whose key is equal to the last input character with
590 or without modifiers, are inserted by `insert-pair'.
592 If COMMAND-CHAR is specified, it is a character that triggers the
593 insertion of the open/close pair, and COMMAND-CHAR itself isn't
594 inserted.")
596 (defun insert-pair (&optional arg open close)
597 "Enclose following ARG sexps in a pair of OPEN and CLOSE characters.
598 Leave point after the first character.
599 A negative ARG encloses the preceding ARG sexps instead.
600 No argument is equivalent to zero: just insert characters
601 and leave point between.
602 If `parens-require-spaces' is non-nil, this command also inserts a space
603 before and after, depending on the surrounding characters.
604 If region is active, insert enclosing characters at region boundaries.
606 If arguments OPEN and CLOSE are nil, the character pair is found
607 from the variable `insert-pair-alist' according to the last input
608 character with or without modifiers. If no character pair is
609 found in the variable `insert-pair-alist', then the last input
610 character is inserted ARG times.
612 This command assumes point is not in a string or comment."
613 (interactive "P")
614 (if (not (and open close))
615 (let ((pair (or (assq last-command-event insert-pair-alist)
616 (assq (event-basic-type last-command-event)
617 insert-pair-alist))))
618 (if pair
619 (if (nth 2 pair)
620 (setq open (nth 1 pair) close (nth 2 pair))
621 (setq open (nth 0 pair) close (nth 1 pair))))))
622 (if (and open close)
623 (if (and transient-mark-mode mark-active)
624 (progn
625 (save-excursion
626 (goto-char (region-end))
627 (insert close))
628 (goto-char (region-beginning))
629 (insert open))
630 (if arg (setq arg (prefix-numeric-value arg))
631 (setq arg 0))
632 (cond ((> arg 0) (skip-chars-forward " \t"))
633 ((< arg 0) (forward-sexp arg) (setq arg (- arg))))
634 (and parens-require-spaces
635 (not (bobp))
636 (memq (char-syntax (preceding-char)) (list ?w ?_ (char-syntax close)))
637 (insert " "))
638 (insert open)
639 (save-excursion
640 (or (eq arg 0) (forward-sexp arg))
641 (insert close)
642 (and parens-require-spaces
643 (not (eobp))
644 (memq (char-syntax (following-char)) (list ?w ?_ (char-syntax open)))
645 (insert " "))))
646 (insert-char (event-basic-type last-command-event)
647 (prefix-numeric-value arg))))
649 (defun insert-parentheses (&optional arg)
650 "Enclose following ARG sexps in parentheses.
651 Leave point after open-paren.
652 A negative ARG encloses the preceding ARG sexps instead.
653 No argument is equivalent to zero: just insert `()' and leave point between.
654 If `parens-require-spaces' is non-nil, this command also inserts a space
655 before and after, depending on the surrounding characters.
656 If region is active, insert enclosing characters at region boundaries.
658 This command assumes point is not in a string or comment."
659 (interactive "P")
660 (insert-pair arg ?\( ?\)))
662 (defun delete-pair ()
663 "Delete a pair of characters enclosing the sexp that follows point."
664 (interactive)
665 (save-excursion (forward-sexp 1) (delete-char -1))
666 (delete-char 1))
668 (defun raise-sexp (&optional arg)
669 "Raise ARG sexps higher up the tree."
670 (interactive "p")
671 (let ((s (if (and transient-mark-mode mark-active)
672 (buffer-substring (region-beginning) (region-end))
673 (buffer-substring
674 (point)
675 (save-excursion (forward-sexp arg) (point))))))
676 (backward-up-list 1)
677 (delete-region (point) (save-excursion (forward-sexp 1) (point)))
678 (save-excursion (insert s))))
680 (defun move-past-close-and-reindent ()
681 "Move past next `)', delete indentation before it, then indent after it."
682 (interactive)
683 (up-list 1)
684 (forward-char -1)
685 (while (save-excursion ; this is my contribution
686 (let ((before-paren (point)))
687 (back-to-indentation)
688 (and (= (point) before-paren)
689 (progn
690 ;; Move to end of previous line.
691 (beginning-of-line)
692 (forward-char -1)
693 ;; Verify it doesn't end within a string or comment.
694 (let ((end (point))
695 state)
696 (beginning-of-line)
697 ;; Get state at start of line.
698 (setq state (list 0 nil nil
699 (null (calculate-lisp-indent))
700 nil nil nil nil
701 nil))
702 ;; Parse state across the line to get state at end.
703 (setq state (parse-partial-sexp (point) end nil nil
704 state))
705 ;; Check not in string or comment.
706 (and (not (elt state 3)) (not (elt state 4))))))))
707 (delete-indentation))
708 (forward-char 1)
709 (newline-and-indent))
711 (defun check-parens () ; lame name?
712 "Check for unbalanced parentheses in the current buffer.
713 More accurately, check the narrowed part of the buffer for unbalanced
714 expressions (\"sexps\") in general. This is done according to the
715 current syntax table and will find unbalanced brackets or quotes as
716 appropriate. (See Info node `(emacs)Parentheses'.) If imbalance is
717 found, an error is signaled and point is left at the first unbalanced
718 character."
719 (interactive)
720 (condition-case data
721 ;; Buffer can't have more than (point-max) sexps.
722 (scan-sexps (point-min) (point-max))
723 (scan-error (push-mark)
724 (goto-char (nth 2 data))
725 ;; Could print (nth 1 data), which is either
726 ;; "Containing expression ends prematurely" or
727 ;; "Unbalanced parentheses", but those may not be so
728 ;; accurate/helpful, e.g. quotes may actually be
729 ;; mismatched.
730 (user-error "Unmatched bracket or quote"))))
732 (defun field-complete (table &optional predicate)
733 (declare (obsolete completion-in-region "24.4"))
734 (let ((minibuffer-completion-table table)
735 (minibuffer-completion-predicate predicate)
736 ;; This made sense for lisp-complete-symbol, but for
737 ;; field-complete, this is out of place. --Stef
738 ;; (completion-annotate-function
739 ;; (unless (eq predicate 'fboundp)
740 ;; (lambda (str)
741 ;; (if (fboundp (intern-soft str)) " <f>"))))
743 (call-interactively 'minibuffer-complete)))
745 (defun lisp-complete-symbol (&optional _predicate)
746 "Perform completion on Lisp symbol preceding point.
747 Compare that symbol against the known Lisp symbols.
748 If no characters can be completed, display a list of possible completions.
749 Repeating the command at that point scrolls the list.
751 The context determines which symbols are considered. If the
752 symbol starts just after an open-parenthesis, only symbols with
753 function definitions are considered. Otherwise, all symbols with
754 function definitions, values or properties are considered."
755 (declare (obsolete completion-at-point "24.4")
756 (advertised-calling-convention () "25.1"))
757 (interactive)
758 (let* ((data (elisp-completion-at-point))
759 (plist (nthcdr 3 data)))
760 (if (null data)
761 (minibuffer-message "Nothing to complete")
762 (let ((completion-extra-properties plist))
763 (completion-in-region (nth 0 data) (nth 1 data) (nth 2 data)
764 (plist-get plist :predicate))))))
766 ;;; lisp.el ends here