copy-file now truncates output after writing
[emacs.git] / lisp / emacs-lisp / lisp.el
blob7b7b48c66dec2a56f4727dca05e8de79a61b7746
1 ;;; lisp.el --- Lisp editing commands for Emacs -*- lexical-binding:t -*-
3 ;; Copyright (C) 1985-1986, 1994, 2000-2015 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 font-lock-beginning-of-syntax-function)
369 (syntax-ppss)))
370 ;; position of least enclosing paren, or nil.
371 encl-pos)
372 ;; Back out of any comment/string, so that encl-pos will always
373 ;; become nil if we're at top-level.
374 (when (nth 8 ppss)
375 (goto-char (nth 8 ppss))
376 (setq ppss (syntax-ppss))) ; should be fast, due to cache.
377 (setq encl-pos (syntax-ppss-toplevel-pos ppss))
378 (if encl-pos (goto-char encl-pos))
380 (and encl-pos arg-+ve (setq arg (1- arg)))
381 (and (not encl-pos) (not arg-+ve) (not (looking-at "\\s("))
382 (setq arg (1+ arg)))
384 (condition-case nil ; to catch crazy parens.
385 (progn
386 (goto-char (scan-lists (point) (- arg) 0))
387 (if arg-+ve
388 (if (>= (point) floor)
390 (goto-char floor)
391 nil)
392 ;; forward to next (, or trigger the c-c
393 (goto-char (1- (scan-lists (point) 1 -1)))
394 (if (<= (point) ceiling)
396 (goto-char ceiling)
397 nil)))
398 (error
399 (goto-char (if arg-+ve floor ceiling))
400 nil))))))))
402 (defvar end-of-defun-function
403 (lambda () (forward-sexp 1))
404 "Function for `end-of-defun' to call.
405 This is used to find the end of the defun at point.
406 It is called with no argument, right after calling `beginning-of-defun-raw'.
407 So the function can assume that point is at the beginning of the defun body.
408 It should move point to the first position after the defun.")
410 (defun buffer-end (arg)
411 "Return the \"far end\" position of the buffer, in direction ARG.
412 If ARG is positive, that's the end of the buffer.
413 Otherwise, that's the beginning of the buffer."
414 (if (> arg 0) (point-max) (point-min)))
416 (defun end-of-defun (&optional arg)
417 "Move forward to next end of defun.
418 With argument, do it that many times.
419 Negative argument -N means move back to Nth preceding end of defun.
421 An end of a defun occurs right after the close-parenthesis that
422 matches the open-parenthesis that starts a defun; see function
423 `beginning-of-defun'.
425 If variable `end-of-defun-function' is non-nil, its value
426 is called as a function to find the defun's end."
427 (interactive "^p")
428 (or (not (eq this-command 'end-of-defun))
429 (eq last-command 'end-of-defun)
430 (and transient-mark-mode mark-active)
431 (push-mark))
432 (if (or (null arg) (= arg 0)) (setq arg 1))
433 (let ((pos (point))
434 (beg (progn (end-of-line 1) (beginning-of-defun-raw 1) (point)))
435 (skip (lambda ()
436 ;; When comparing point against pos, we want to consider that if
437 ;; point was right after the end of the function, it's still
438 ;; considered as "in that function".
439 ;; E.g. `eval-defun' from right after the last close-paren.
440 (unless (bolp)
441 (skip-chars-forward " \t")
442 (if (looking-at "\\s<\\|\n")
443 (forward-line 1))))))
444 (funcall end-of-defun-function)
445 (funcall skip)
446 (cond
447 ((> arg 0)
448 ;; Moving forward.
449 (if (> (point) pos)
450 ;; We already moved forward by one because we started from
451 ;; within a function.
452 (setq arg (1- arg))
453 ;; We started from after the end of the previous function.
454 (goto-char pos))
455 (unless (zerop arg)
456 (beginning-of-defun-raw (- arg))
457 (funcall end-of-defun-function)))
458 ((< arg 0)
459 ;; Moving backward.
460 (if (< (point) pos)
461 ;; We already moved backward because we started from between
462 ;; two functions.
463 (setq arg (1+ arg))
464 ;; We started from inside a function.
465 (goto-char beg))
466 (unless (zerop arg)
467 (beginning-of-defun-raw (- arg))
468 (setq beg (point))
469 (funcall end-of-defun-function))))
470 (funcall skip)
471 (while (and (< arg 0) (>= (point) pos))
472 ;; We intended to move backward, but this ended up not doing so:
473 ;; Try harder!
474 (goto-char beg)
475 (beginning-of-defun-raw (- arg))
476 (if (>= (point) beg)
477 (setq arg 0)
478 (setq beg (point))
479 (funcall end-of-defun-function)
480 (funcall skip)))))
482 (defun mark-defun (&optional allow-extend)
483 "Put mark at end of this defun, point at beginning.
484 The defun marked is the one that contains point or follows point.
486 Interactively, if this command is repeated
487 or (in Transient Mark mode) if the mark is active,
488 it marks the next defun after the ones already marked."
489 (interactive "p")
490 (cond ((and allow-extend
491 (or (and (eq last-command this-command) (mark t))
492 (and transient-mark-mode mark-active)))
493 (set-mark
494 (save-excursion
495 (goto-char (mark))
496 (end-of-defun)
497 (point))))
499 (let ((opoint (point))
500 beg end)
501 (push-mark opoint)
502 ;; Try first in this order for the sake of languages with nested
503 ;; functions where several can end at the same place as with
504 ;; the offside rule, e.g. Python.
505 (beginning-of-defun)
506 (setq beg (point))
507 (end-of-defun)
508 (setq end (point))
509 (while (looking-at "^\n")
510 (forward-line 1))
511 (if (> (point) opoint)
512 (progn
513 ;; We got the right defun.
514 (push-mark beg nil t)
515 (goto-char end)
516 (exchange-point-and-mark))
517 ;; beginning-of-defun moved back one defun
518 ;; so we got the wrong one.
519 (goto-char opoint)
520 (end-of-defun)
521 (push-mark (point) nil t)
522 (beginning-of-defun))
523 (re-search-backward "^\n" (- (point) 1) t)))))
525 (defvar narrow-to-defun-include-comments nil
526 "If non-nil, `narrow-to-defun' will also show comments preceding the defun.")
528 (defun narrow-to-defun (&optional include-comments)
529 "Make text outside current defun invisible.
530 The current defun is the one that contains point or follows point.
531 Preceding comments are included if INCLUDE-COMMENTS is non-nil.
532 Interactively, the behavior depends on `narrow-to-defun-include-comments'."
533 (interactive (list narrow-to-defun-include-comments))
534 (save-excursion
535 (widen)
536 (let ((opoint (point))
537 beg end)
538 ;; Try first in this order for the sake of languages with nested
539 ;; functions where several can end at the same place as with
540 ;; the offside rule, e.g. Python.
542 ;; Finding the start of the function is a bit problematic since
543 ;; `beginning-of-defun' when we are on the first character of
544 ;; the function might go to the previous function.
546 ;; Therefore we first move one character forward and then call
547 ;; `beginning-of-defun'. However now we must check that we did
548 ;; not move into the next function.
549 (let ((here (point)))
550 (unless (eolp)
551 (forward-char))
552 (beginning-of-defun)
553 (when (< (point) here)
554 (goto-char here)
555 (beginning-of-defun)))
556 (setq beg (point))
557 (end-of-defun)
558 (setq end (point))
559 (while (looking-at "^\n")
560 (forward-line 1))
561 (unless (> (point) opoint)
562 ;; beginning-of-defun moved back one defun
563 ;; so we got the wrong one.
564 (goto-char opoint)
565 (end-of-defun)
566 (setq end (point))
567 (beginning-of-defun)
568 (setq beg (point)))
569 (when include-comments
570 (goto-char beg)
571 ;; Move back past all preceding comments (and whitespace).
572 (when (forward-comment -1)
573 (while (forward-comment -1))
574 ;; Move forwards past any page breaks within these comments.
575 (when (and page-delimiter (not (string= page-delimiter "")))
576 (while (re-search-forward page-delimiter beg t)))
577 ;; Lastly, move past any empty lines.
578 (skip-chars-forward "[:space:]\n")
579 (beginning-of-line)
580 (setq beg (point))))
581 (goto-char end)
582 (re-search-backward "^\n" (- (point) 1) t)
583 (narrow-to-region beg end))))
585 (defvar insert-pair-alist
586 '((?\( ?\)) (?\[ ?\]) (?\{ ?\}) (?\< ?\>) (?\" ?\") (?\' ?\') (?\` ?\'))
587 "Alist of paired characters inserted by `insert-pair'.
588 Each element looks like (OPEN-CHAR CLOSE-CHAR) or (COMMAND-CHAR
589 OPEN-CHAR CLOSE-CHAR). The characters OPEN-CHAR and CLOSE-CHAR
590 of the pair whose key is equal to the last input character with
591 or without modifiers, are inserted by `insert-pair'.")
593 (defun insert-pair (&optional arg open close)
594 "Enclose following ARG sexps in a pair of OPEN and CLOSE characters.
595 Leave point after the first character.
596 A negative ARG encloses the preceding ARG sexps instead.
597 No argument is equivalent to zero: just insert characters
598 and leave point between.
599 If `parens-require-spaces' is non-nil, this command also inserts a space
600 before and after, depending on the surrounding characters.
601 If region is active, insert enclosing characters at region boundaries.
603 If arguments OPEN and CLOSE are nil, the character pair is found
604 from the variable `insert-pair-alist' according to the last input
605 character with or without modifiers. If no character pair is
606 found in the variable `insert-pair-alist', then the last input
607 character is inserted ARG times.
609 This command assumes point is not in a string or comment."
610 (interactive "P")
611 (if (not (and open close))
612 (let ((pair (or (assq last-command-event insert-pair-alist)
613 (assq (event-basic-type last-command-event)
614 insert-pair-alist))))
615 (if pair
616 (if (nth 2 pair)
617 (setq open (nth 1 pair) close (nth 2 pair))
618 (setq open (nth 0 pair) close (nth 1 pair))))))
619 (if (and open close)
620 (if (and transient-mark-mode mark-active)
621 (progn
622 (save-excursion (goto-char (region-end)) (insert close))
623 (save-excursion (goto-char (region-beginning)) (insert open)))
624 (if arg (setq arg (prefix-numeric-value arg))
625 (setq arg 0))
626 (cond ((> arg 0) (skip-chars-forward " \t"))
627 ((< arg 0) (forward-sexp arg) (setq arg (- arg))))
628 (and parens-require-spaces
629 (not (bobp))
630 (memq (char-syntax (preceding-char)) (list ?w ?_ (char-syntax close)))
631 (insert " "))
632 (insert open)
633 (save-excursion
634 (or (eq arg 0) (forward-sexp arg))
635 (insert close)
636 (and parens-require-spaces
637 (not (eobp))
638 (memq (char-syntax (following-char)) (list ?w ?_ (char-syntax open)))
639 (insert " "))))
640 (insert-char (event-basic-type last-command-event)
641 (prefix-numeric-value arg))))
643 (defun insert-parentheses (&optional arg)
644 "Enclose following ARG sexps in parentheses.
645 Leave point after open-paren.
646 A negative ARG encloses the preceding ARG sexps instead.
647 No argument is equivalent to zero: just insert `()' and leave point between.
648 If `parens-require-spaces' is non-nil, this command also inserts a space
649 before and after, depending on the surrounding characters.
650 If region is active, insert enclosing characters at region boundaries.
652 This command assumes point is not in a string or comment."
653 (interactive "P")
654 (insert-pair arg ?\( ?\)))
656 (defun delete-pair ()
657 "Delete a pair of characters enclosing the sexp that follows point."
658 (interactive)
659 (save-excursion (forward-sexp 1) (delete-char -1))
660 (delete-char 1))
662 (defun raise-sexp (&optional arg)
663 "Raise ARG sexps higher up the tree."
664 (interactive "p")
665 (let ((s (if (and transient-mark-mode mark-active)
666 (buffer-substring (region-beginning) (region-end))
667 (buffer-substring
668 (point)
669 (save-excursion (forward-sexp arg) (point))))))
670 (backward-up-list 1)
671 (delete-region (point) (save-excursion (forward-sexp 1) (point)))
672 (save-excursion (insert s))))
674 (defun move-past-close-and-reindent ()
675 "Move past next `)', delete indentation before it, then indent after it."
676 (interactive)
677 (up-list 1)
678 (forward-char -1)
679 (while (save-excursion ; this is my contribution
680 (let ((before-paren (point)))
681 (back-to-indentation)
682 (and (= (point) before-paren)
683 (progn
684 ;; Move to end of previous line.
685 (beginning-of-line)
686 (forward-char -1)
687 ;; Verify it doesn't end within a string or comment.
688 (let ((end (point))
689 state)
690 (beginning-of-line)
691 ;; Get state at start of line.
692 (setq state (list 0 nil nil
693 (null (calculate-lisp-indent))
694 nil nil nil nil
695 nil))
696 ;; Parse state across the line to get state at end.
697 (setq state (parse-partial-sexp (point) end nil nil
698 state))
699 ;; Check not in string or comment.
700 (and (not (elt state 3)) (not (elt state 4))))))))
701 (delete-indentation))
702 (forward-char 1)
703 (newline-and-indent))
705 (defun check-parens () ; lame name?
706 "Check for unbalanced parentheses in the current buffer.
707 More accurately, check the narrowed part of the buffer for unbalanced
708 expressions (\"sexps\") in general. This is done according to the
709 current syntax table and will find unbalanced brackets or quotes as
710 appropriate. (See Info node `(emacs)Parentheses'.) If imbalance is
711 found, an error is signaled and point is left at the first unbalanced
712 character."
713 (interactive)
714 (condition-case data
715 ;; Buffer can't have more than (point-max) sexps.
716 (scan-sexps (point-min) (point-max))
717 (scan-error (push-mark)
718 (goto-char (nth 2 data))
719 ;; Could print (nth 1 data), which is either
720 ;; "Containing expression ends prematurely" or
721 ;; "Unbalanced parentheses", but those may not be so
722 ;; accurate/helpful, e.g. quotes may actually be
723 ;; mismatched.
724 (user-error "Unmatched bracket or quote"))))
726 (defun field-complete (table &optional predicate)
727 (declare (obsolete completion-in-region "24.4"))
728 (let ((minibuffer-completion-table table)
729 (minibuffer-completion-predicate predicate)
730 ;; This made sense for lisp-complete-symbol, but for
731 ;; field-complete, this is out of place. --Stef
732 ;; (completion-annotate-function
733 ;; (unless (eq predicate 'fboundp)
734 ;; (lambda (str)
735 ;; (if (fboundp (intern-soft str)) " <f>"))))
737 (call-interactively 'minibuffer-complete)))
739 (defun lisp-complete-symbol (&optional _predicate)
740 "Perform completion on Lisp symbol preceding point.
741 Compare that symbol against the known Lisp symbols.
742 If no characters can be completed, display a list of possible completions.
743 Repeating the command at that point scrolls the list.
745 The context determines which symbols are considered. If the
746 symbol starts just after an open-parenthesis, only symbols with
747 function definitions are considered. Otherwise, all symbols with
748 function definitions, values or properties are considered."
749 (declare (obsolete completion-at-point "24.4")
750 (advertised-calling-convention () "25.1"))
751 (interactive)
752 (let* ((data (elisp-completion-at-point))
753 (plist (nthcdr 3 data)))
754 (if (null data)
755 (minibuffer-message "Nothing to complete")
756 (let ((completion-extra-properties plist))
757 (completion-in-region (nth 0 data) (nth 1 data) (nth 2 data)
758 (plist-get plist :predicate))))))
760 ;;; lisp.el ends here