Commit | Line | Data |
---|---|---|
6594deb0 ER |
1 | ;;; lisp.el --- Lisp editing commands for Emacs |
2 | ||
73b0cd50 | 3 | ;; Copyright (C) 1985-1986, 1994, 2000-2011 Free Software Foundation, Inc. |
9750e079 | 4 | |
e5167999 | 5 | ;; Maintainer: FSF |
e9571d2a | 6 | ;; Keywords: lisp, languages |
bd78fa1d | 7 | ;; Package: emacs |
e5167999 | 8 | |
b73b9811 | 9 | ;; This file is part of GNU Emacs. |
10 | ||
d6cba7ae | 11 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
b73b9811 | 12 | ;; it under the terms of the GNU General Public License as published by |
d6cba7ae GM |
13 | ;; the Free Software Foundation, either version 3 of the License, or |
14 | ;; (at your option) any later version. | |
b73b9811 | 15 | |
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. | |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
d6cba7ae | 22 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
b73b9811 | 23 | |
e41b2db1 ER |
24 | ;;; Commentary: |
25 | ||
e50c4203 DL |
26 | ;; Lisp editing commands to go with Lisp major mode. More-or-less |
27 | ;; applicable in other modes too. | |
e41b2db1 | 28 | |
e5167999 | 29 | ;;; Code: |
b73b9811 | 30 | |
7fe78b07 | 31 | ;; Note that this variable is used by non-lisp modes too. |
30e19aee | 32 | (defcustom defun-prompt-regexp nil |
cb711556 | 33 | "If non-nil, a regexp to ignore before a defun. |
7fe78b07 | 34 | This is only necessary if the opening paren or brace is not in column 0. |
a64dc1a4 | 35 | See function `beginning-of-defun'." |
ba6b3a2a RS |
36 | :type '(choice (const nil) |
37 | regexp) | |
30e19aee | 38 | :group 'lisp) |
24ff5498 | 39 | (make-variable-buffer-local 'defun-prompt-regexp) |
b73b9811 | 40 | |
30e19aee | 41 | (defcustom parens-require-spaces t |
becc6788 CY |
42 | "If non-nil, add whitespace as needed when inserting parentheses. |
43 | This affects `insert-parentheses' and `insert-pair'." | |
30e19aee RS |
44 | :type 'boolean |
45 | :group 'lisp) | |
44a53673 | 46 | |
11ae6c5d SM |
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'.") | |
50 | ||
b73b9811 | 51 | (defun forward-sexp (&optional arg) |
52 | "Move forward across one balanced expression (sexp). | |
c6eeec65 | 53 | With ARG, do it that many times. Negative arg -N means |
8fc2ac41 AM |
54 | move backward across N balanced expressions. |
55 | This command assumes point is not in a string or comment." | |
61eee794 | 56 | (interactive "^p") |
b73b9811 | 57 | (or arg (setq arg 1)) |
11ae6c5d SM |
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)))) | |
b73b9811 | 62 | |
63 | (defun backward-sexp (&optional arg) | |
64 | "Move backward across one balanced expression (sexp). | |
c6eeec65 | 65 | With ARG, do it that many times. Negative arg -N means |
8fc2ac41 AM |
66 | move forward across N balanced expressions. |
67 | This command assumes point is not in a string or comment." | |
61eee794 | 68 | (interactive "^p") |
b73b9811 | 69 | (or arg (setq arg 1)) |
70 | (forward-sexp (- arg))) | |
71 | ||
afb62fdd | 72 | (defun mark-sexp (&optional arg allow-extend) |
b73b9811 | 73 | "Set mark ARG sexps from point. |
e3f99b91 | 74 | The place mark goes is the same place \\[forward-sexp] would |
f07493e7 | 75 | move to with the same argument. |
afb62fdd | 76 | Interactively, if this command is repeated |
a64dc1a4 | 77 | or (in Transient Mark mode) if the mark is active, |
8fc2ac41 AM |
78 | it marks the next ARG sexps after the ones already marked. |
79 | This command assumes point is not in a string or comment." | |
afb62fdd RS |
80 | (interactive "P\np") |
81 | (cond ((and allow-extend | |
82 | (or (and (eq last-command this-command) (mark t)) | |
83 | (and transient-mark-mode mark-active))) | |
3610d3c9 | 84 | (setq arg (if arg (prefix-numeric-value arg) |
967e1a52 | 85 | (if (< (mark) (point)) -1 1))) |
cad113ae KG |
86 | (set-mark |
87 | (save-excursion | |
967e1a52 JL |
88 | (goto-char (mark)) |
89 | (forward-sexp arg) | |
90 | (point)))) | |
cad113ae KG |
91 | (t |
92 | (push-mark | |
93 | (save-excursion | |
3610d3c9 | 94 | (forward-sexp (prefix-numeric-value arg)) |
cad113ae KG |
95 | (point)) |
96 | nil t)))) | |
b73b9811 | 97 | |
98 | (defun forward-list (&optional arg) | |
99 | "Move forward across one balanced group of parentheses. | |
c6eeec65 | 100 | With ARG, do it that many times. |
8fc2ac41 AM |
101 | Negative arg -N means move backward across N groups of parentheses. |
102 | This command assumes point is not in a string or comment." | |
61eee794 | 103 | (interactive "^p") |
b73b9811 | 104 | (or arg (setq arg 1)) |
105 | (goto-char (or (scan-lists (point) arg 0) (buffer-end arg)))) | |
106 | ||
107 | (defun backward-list (&optional arg) | |
108 | "Move backward across one balanced group of parentheses. | |
c6eeec65 | 109 | With ARG, do it that many times. |
8fc2ac41 AM |
110 | Negative arg -N means move forward across N groups of parentheses. |
111 | This command assumes point is not in a string or comment." | |
61eee794 | 112 | (interactive "^p") |
b73b9811 | 113 | (or arg (setq arg 1)) |
114 | (forward-list (- arg))) | |
115 | ||
e50c4203 | 116 | (defun down-list (&optional arg) |
b73b9811 | 117 | "Move forward down one level of parentheses. |
c6eeec65 | 118 | With ARG, do this that many times. |
8fc2ac41 AM |
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." | |
61eee794 | 121 | (interactive "^p") |
e50c4203 | 122 | (or arg (setq arg 1)) |
b73b9811 | 123 | (let ((inc (if (> arg 0) 1 -1))) |
124 | (while (/= arg 0) | |
125 | (goto-char (or (scan-lists (point) inc -1) (buffer-end arg))) | |
126 | (setq arg (- arg inc))))) | |
127 | ||
e50c4203 | 128 | (defun backward-up-list (&optional arg) |
b73b9811 | 129 | "Move backward out of one level of parentheses. |
c6eeec65 | 130 | With ARG, do this that many times. |
8fc2ac41 AM |
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." | |
61eee794 | 133 | (interactive "^p") |
e50c4203 | 134 | (up-list (- (or arg 1)))) |
b73b9811 | 135 | |
e50c4203 | 136 | (defun up-list (&optional arg) |
b73b9811 | 137 | "Move forward out of one level of parentheses. |
c6eeec65 | 138 | With ARG, do this that many times. |
8fc2ac41 AM |
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." | |
61eee794 | 141 | (interactive "^p") |
e50c4203 | 142 | (or arg (setq arg 1)) |
984edd22 SM |
143 | (let ((inc (if (> arg 0) 1 -1)) |
144 | pos) | |
b73b9811 | 145 | (while (/= arg 0) |
984edd22 SM |
146 | (if (null forward-sexp-function) |
147 | (goto-char (or (scan-lists (point) inc 1) (buffer-end arg))) | |
e44e373d SM |
148 | (condition-case err |
149 | (while (progn (setq pos (point)) | |
150 | (forward-sexp inc) | |
151 | (/= (point) pos))) | |
152 | (scan-error (goto-char (nth (if (> arg 0) 3 2) err)))) | |
153 | (if (= (point) pos) | |
984edd22 SM |
154 | (signal 'scan-error |
155 | (list "Unbalanced parentheses" (point) (point))))) | |
b73b9811 | 156 | (setq arg (- arg inc))))) |
157 | ||
e50c4203 | 158 | (defun kill-sexp (&optional arg) |
dd60a465 RS |
159 | "Kill the sexp (balanced expression) following point. |
160 | With ARG, kill that many sexps after point. | |
8fc2ac41 AM |
161 | Negative arg -N means kill N sexps before point. |
162 | This command assumes point is not in a string or comment." | |
b73b9811 | 163 | (interactive "p") |
164 | (let ((opoint (point))) | |
e50c4203 | 165 | (forward-sexp (or arg 1)) |
b73b9811 | 166 | (kill-region opoint (point)))) |
167 | ||
e50c4203 | 168 | (defun backward-kill-sexp (&optional arg) |
dd60a465 RS |
169 | "Kill the sexp (balanced expression) preceding point. |
170 | With ARG, kill that many sexps before point. | |
8fc2ac41 AM |
171 | Negative arg -N means kill N sexps after point. |
172 | This command assumes point is not in a string or comment." | |
b73b9811 | 173 | (interactive "p") |
e50c4203 | 174 | (kill-sexp (- (or arg 1)))) |
de6d64b2 EZ |
175 | |
176 | ;; After Zmacs: | |
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 | |
8fc2ac41 AM |
180 | forms to be removed. |
181 | This command assumes point is not in a string or comment." | |
de6d64b2 EZ |
182 | (interactive "*p") |
183 | (let ((current-sexp (thing-at-point 'sexp))) | |
184 | (if current-sexp | |
185 | (save-excursion | |
186 | (backward-up-list arg) | |
187 | (kill-sexp) | |
188 | (insert current-sexp)) | |
189 | (error "Not at a sexp")))) | |
b73b9811 | 190 | \f |
ab22ee53 | 191 | (defvar beginning-of-defun-function nil |
c6eeec65 DL |
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 | |
ab22ee53 RS |
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 | |
196 | needs. | |
c6eeec65 | 197 | |
50bfa18a SM |
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.") | |
c6eeec65 | 202 | |
b73b9811 | 203 | (defun beginning-of-defun (&optional arg) |
204 | "Move backward to the beginning of a defun. | |
6957495d CY |
205 | With ARG, do it that many times. Negative ARG means move forward |
206 | to the ARGth following beginning of defun. | |
207 | ||
208 | If search is successful, return t; point ends up at the beginning | |
209 | of the line where the search succeeded. Otherwise, return nil. | |
210 | ||
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. | |
218 | ||
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 | |
221 | defun's beginning. | |
222 | ||
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." | |
61eee794 | 226 | (interactive "^p") |
90c08845 | 227 | (or (not (eq this-command 'beginning-of-defun)) |
967e1a52 JL |
228 | (eq last-command 'beginning-of-defun) |
229 | (and transient-mark-mode mark-active) | |
230 | (push-mark)) | |
afa995e1 KH |
231 | (and (beginning-of-defun-raw arg) |
232 | (progn (beginning-of-line) t))) | |
233 | ||
234 | (defun beginning-of-defun-raw (&optional arg) | |
235 | "Move point to the character that starts a defun. | |
c6eeec65 DL |
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' | |
238 | is non-nil. | |
239 | ||
ab22ee53 RS |
240 | If variable `beginning-of-defun-function' is non-nil, its value |
241 | is called as a function to find the defun's beginning." | |
61eee794 | 242 | (interactive "^p") ; change this to "P", maybe, if we ever come to pass ARG |
4df5698c CY |
243 | ; to beginning-of-defun-function. |
244 | (unless arg (setq arg 1)) | |
6cb54822 AM |
245 | (cond |
246 | (beginning-of-defun-function | |
50bfa18a SM |
247 | (condition-case nil |
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 | |
253 | ;; of the next). | |
254 | ;; In case the beginning-of-defun-function uses the old calling | |
255 | ;; convention, fallback on the old implementation. | |
256 | (wrong-number-of-arguments | |
257 | (if (> arg 0) | |
258 | (dotimes (i arg) | |
259 | (funcall beginning-of-defun-function)) | |
260 | ;; Better not call end-of-defun-function directly, in case | |
261 | ;; it's not defined. | |
262 | (end-of-defun (- arg)))))) | |
6cb54822 AM |
263 | |
264 | ((or defun-prompt-regexp open-paren-in-column-0-is-defun-start) | |
265 | (and (< arg 0) (not (eobp)) (forward-char 1)) | |
c6eeec65 | 266 | (and (re-search-backward (if defun-prompt-regexp |
418d645c GM |
267 | (concat (if open-paren-in-column-0-is-defun-start |
268 | "^\\s(\\|" "") | |
b5ed9def | 269 | "\\(?:" defun-prompt-regexp "\\)\\s(") |
c6eeec65 | 270 | "^\\s(") |
6cb54822 | 271 | nil 'move arg) |
4114bc49 SM |
272 | (progn (goto-char (1- (match-end 0))) |
273 | t))) | |
6cb54822 | 274 | |
4df5698c CY |
275 | ;; If open-paren-in-column-0-is-defun-start and defun-prompt-regexp |
276 | ;; are both nil, column 0 has no significance - so scan forward | |
277 | ;; from BOB to see how nested point is, then carry on from there. | |
278 | ;; | |
279 | ;; It is generally not a good idea to land up here, because the | |
280 | ;; call to scan-lists below can be extremely slow. This is because | |
281 | ;; back_comment in syntax.c may have to scan from bob to find the | |
282 | ;; beginning of each comment. Fixing this is not trivial -- cyd. | |
283 | ||
284 | ((eq arg 0)) | |
6cb54822 | 285 | (t |
4df5698c CY |
286 | (let ((floor (point-min)) |
287 | (ceiling (point-max)) | |
288 | (arg-+ve (> arg 0))) | |
6cb54822 AM |
289 | (save-restriction |
290 | (widen) | |
4df5698c CY |
291 | (let ((ppss (let (syntax-begin-function |
292 | font-lock-beginning-of-syntax-function) | |
293 | (syntax-ppss))) | |
294 | ;; position of least enclosing paren, or nil. | |
295 | encl-pos) | |
296 | ;; Back out of any comment/string, so that encl-pos will always | |
297 | ;; become nil if we're at top-level. | |
298 | (when (nth 8 ppss) | |
299 | (goto-char (nth 8 ppss)) | |
300 | (setq ppss (syntax-ppss))) ; should be fast, due to cache. | |
301 | (setq encl-pos (syntax-ppss-toplevel-pos ppss)) | |
302 | (if encl-pos (goto-char encl-pos)) | |
303 | ||
304 | (and encl-pos arg-+ve (setq arg (1- arg))) | |
305 | (and (not encl-pos) (not arg-+ve) (not (looking-at "\\s(")) | |
306 | (setq arg (1+ arg))) | |
307 | ||
308 | (condition-case nil ; to catch crazy parens. | |
309 | (progn | |
310 | (goto-char (scan-lists (point) (- arg) 0)) | |
311 | (if arg-+ve | |
312 | (if (>= (point) floor) | |
313 | t | |
314 | (goto-char floor) | |
315 | nil) | |
316 | ;; forward to next (, or trigger the c-c | |
317 | (goto-char (1- (scan-lists (point) 1 -1))) | |
318 | (if (<= (point) ceiling) | |
319 | t | |
320 | (goto-char ceiling) | |
321 | nil))) | |
322 | (error | |
323 | (goto-char (if arg-+ve floor ceiling)) | |
324 | nil)))))))) | |
c6eeec65 | 325 | |
7bbab3e0 SM |
326 | (defvar end-of-defun-function |
327 | (lambda () (forward-sexp 1)) | |
61e21607 | 328 | "Function for `end-of-defun' to call. |
6d3e4d22 | 329 | This is used to find the end of the defun at point. |
61e21607 | 330 | It is called with no argument, right after calling `beginning-of-defun-raw'. |
6d3e4d22 SM |
331 | So the function can assume that point is at the beginning of the defun body. |
332 | It should move point to the first position after the defun.") | |
b73b9811 | 333 | |
334 | (defun buffer-end (arg) | |
a64dc1a4 | 335 | "Return the \"far end\" position of the buffer, in direction ARG. |
03deb635 RS |
336 | If ARG is positive, that's the end of the buffer. |
337 | Otherwise, that's the beginning of the buffer." | |
b73b9811 | 338 | (if (> arg 0) (point-max) (point-min))) |
339 | ||
340 | (defun end-of-defun (&optional arg) | |
a64dc1a4 LT |
341 | "Move forward to next end of defun. |
342 | With argument, do it that many times. | |
b73b9811 | 343 | Negative argument -N means move back to Nth preceding end of defun. |
344 | ||
c6eeec65 DL |
345 | An end of a defun occurs right after the close-parenthesis that |
346 | matches the open-parenthesis that starts a defun; see function | |
ab22ee53 RS |
347 | `beginning-of-defun'. |
348 | ||
349 | If variable `end-of-defun-function' is non-nil, its value | |
350 | is called as a function to find the defun's end." | |
61eee794 | 351 | (interactive "^p") |
90c08845 | 352 | (or (not (eq this-command 'end-of-defun)) |
967e1a52 JL |
353 | (eq last-command 'end-of-defun) |
354 | (and transient-mark-mode mark-active) | |
355 | (push-mark)) | |
44b254cc | 356 | (if (or (null arg) (= arg 0)) (setq arg 1)) |
f9f34ece SM |
357 | (let ((pos (point)) |
358 | (beg (progn (end-of-line 1) (beginning-of-defun-raw 1) (point)))) | |
359 | (funcall end-of-defun-function) | |
f2a8252a SM |
360 | ;; When comparing point against pos, we want to consider that if |
361 | ;; point was right after the end of the function, it's still | |
362 | ;; considered as "in that function". | |
363 | ;; E.g. `eval-defun' from right after the last close-paren. | |
364 | (unless (bolp) | |
365 | (skip-chars-forward " \t") | |
366 | (if (looking-at "\\s<\\|\n") | |
367 | (forward-line 1))) | |
f9f34ece SM |
368 | (cond |
369 | ((> arg 0) | |
370 | ;; Moving forward. | |
371 | (if (> (point) pos) | |
372 | ;; We already moved forward by one because we started from | |
373 | ;; within a function. | |
374 | (setq arg (1- arg)) | |
375 | ;; We started from after the end of the previous function. | |
376 | (goto-char pos)) | |
377 | (unless (zerop arg) | |
378 | (beginning-of-defun-raw (- arg)) | |
379 | (funcall end-of-defun-function))) | |
380 | ((< arg 0) | |
381 | ;; Moving backward. | |
382 | (if (< (point) pos) | |
383 | ;; We already moved backward because we started from between | |
384 | ;; two functions. | |
385 | (setq arg (1+ arg)) | |
386 | ;; We started from inside a function. | |
387 | (goto-char beg)) | |
388 | (unless (zerop arg) | |
389 | (beginning-of-defun-raw (- arg)) | |
390 | (funcall end-of-defun-function)))) | |
391 | (unless (bolp) | |
392 | (skip-chars-forward " \t") | |
393 | (if (looking-at "\\s<\\|\n") | |
394 | (forward-line 1))))) | |
b73b9811 | 395 | |
afb62fdd | 396 | (defun mark-defun (&optional allow-extend) |
b73b9811 | 397 | "Put mark at end of this defun, point at beginning. |
cad113ae | 398 | The defun marked is the one that contains point or follows point. |
afb62fdd RS |
399 | |
400 | Interactively, if this command is repeated | |
a64dc1a4 | 401 | or (in Transient Mark mode) if the mark is active, |
afb62fdd RS |
402 | it marks the next defun after the ones already marked." |
403 | (interactive "p") | |
404 | (cond ((and allow-extend | |
405 | (or (and (eq last-command this-command) (mark t)) | |
406 | (and transient-mark-mode mark-active))) | |
be0d25b6 KG |
407 | (set-mark |
408 | (save-excursion | |
409 | (goto-char (mark)) | |
410 | (end-of-defun) | |
411 | (point)))) | |
412 | (t | |
d3d6bc9b RS |
413 | (let ((opoint (point)) |
414 | beg end) | |
415 | (push-mark opoint) | |
416 | ;; Try first in this order for the sake of languages with nested | |
417 | ;; functions where several can end at the same place as with | |
418 | ;; the offside rule, e.g. Python. | |
419 | (beginning-of-defun) | |
420 | (setq beg (point)) | |
421 | (end-of-defun) | |
422 | (setq end (point)) | |
423 | (while (looking-at "^\n") | |
424 | (forward-line 1)) | |
425 | (if (> (point) opoint) | |
426 | (progn | |
427 | ;; We got the right defun. | |
428 | (push-mark beg nil t) | |
429 | (goto-char end) | |
430 | (exchange-point-and-mark)) | |
431 | ;; beginning-of-defun moved back one defun | |
432 | ;; so we got the wrong one. | |
433 | (goto-char opoint) | |
434 | (end-of-defun) | |
435 | (push-mark (point) nil t) | |
436 | (beginning-of-defun)) | |
437 | (re-search-backward "^\n" (- (point) 1) t))))) | |
b73b9811 | 438 | |
3f7e952d RS |
439 | (defun narrow-to-defun (&optional arg) |
440 | "Make text outside current defun invisible. | |
c6eeec65 DL |
441 | The defun visible is the one that contains point or follows point. |
442 | Optional ARG is ignored." | |
3f7e952d RS |
443 | (interactive) |
444 | (save-excursion | |
445 | (widen) | |
d3d6bc9b RS |
446 | (let ((opoint (point)) |
447 | beg end) | |
448 | ;; Try first in this order for the sake of languages with nested | |
449 | ;; functions where several can end at the same place as with | |
450 | ;; the offside rule, e.g. Python. | |
451 | (beginning-of-defun) | |
452 | (setq beg (point)) | |
750e563f | 453 | (end-of-defun) |
d3d6bc9b RS |
454 | (setq end (point)) |
455 | (while (looking-at "^\n") | |
456 | (forward-line 1)) | |
457 | (unless (> (point) opoint) | |
458 | ;; beginning-of-defun moved back one defun | |
459 | ;; so we got the wrong one. | |
460 | (goto-char opoint) | |
461 | (end-of-defun) | |
462 | (setq end (point)) | |
463 | (beginning-of-defun) | |
464 | (setq beg (point))) | |
465 | (goto-char end) | |
466 | (re-search-backward "^\n" (- (point) 1) t) | |
467 | (narrow-to-region beg end)))) | |
3f7e952d | 468 | |
d97c8198 JL |
469 | (defvar insert-pair-alist |
470 | '((?\( ?\)) (?\[ ?\]) (?\{ ?\}) (?\< ?\>) (?\" ?\") (?\' ?\') (?\` ?\')) | |
471 | "Alist of paired characters inserted by `insert-pair'. | |
472 | Each element looks like (OPEN-CHAR CLOSE-CHAR) or (COMMAND-CHAR | |
473 | OPEN-CHAR CLOSE-CHAR). The characters OPEN-CHAR and CLOSE-CHAR | |
474 | of the pair whose key is equal to the last input character with | |
475 | or without modifiers, are inserted by `insert-pair'.") | |
476 | ||
477 | (defun insert-pair (&optional arg open close) | |
5891bf24 JL |
478 | "Enclose following ARG sexps in a pair of OPEN and CLOSE characters. |
479 | Leave point after the first character. | |
e3ac26cb | 480 | A negative ARG encloses the preceding ARG sexps instead. |
5891bf24 JL |
481 | No argument is equivalent to zero: just insert characters |
482 | and leave point between. | |
d5bfe076 | 483 | If `parens-require-spaces' is non-nil, this command also inserts a space |
5891bf24 | 484 | before and after, depending on the surrounding characters. |
d97c8198 JL |
485 | If region is active, insert enclosing characters at region boundaries. |
486 | ||
487 | If arguments OPEN and CLOSE are nil, the character pair is found | |
488 | from the variable `insert-pair-alist' according to the last input | |
489 | character with or without modifiers. If no character pair is | |
490 | found in the variable `insert-pair-alist', then the last input | |
8fc2ac41 AM |
491 | character is inserted ARG times. |
492 | ||
493 | This command assumes point is not in a string or comment." | |
b73b9811 | 494 | (interactive "P") |
d97c8198 | 495 | (if (not (and open close)) |
61a846fb | 496 | (let ((pair (or (assq last-command-event insert-pair-alist) |
d97c8198 JL |
497 | (assq (event-basic-type last-command-event) |
498 | insert-pair-alist)))) | |
499 | (if pair | |
500 | (if (nth 2 pair) | |
501 | (setq open (nth 1 pair) close (nth 2 pair)) | |
502 | (setq open (nth 0 pair) close (nth 1 pair)))))) | |
503 | (if (and open close) | |
504 | (if (and transient-mark-mode mark-active) | |
505 | (progn | |
506 | (save-excursion (goto-char (region-end)) (insert close)) | |
507 | (save-excursion (goto-char (region-beginning)) (insert open))) | |
508 | (if arg (setq arg (prefix-numeric-value arg)) | |
509 | (setq arg 0)) | |
510 | (cond ((> arg 0) (skip-chars-forward " \t")) | |
511 | ((< arg 0) (forward-sexp arg) (setq arg (- arg)))) | |
512 | (and parens-require-spaces | |
513 | (not (bobp)) | |
514 | (memq (char-syntax (preceding-char)) (list ?w ?_ (char-syntax close))) | |
515 | (insert " ")) | |
516 | (insert open) | |
517 | (save-excursion | |
518 | (or (eq arg 0) (forward-sexp arg)) | |
519 | (insert close) | |
520 | (and parens-require-spaces | |
521 | (not (eobp)) | |
522 | (memq (char-syntax (following-char)) (list ?w ?_ (char-syntax open))) | |
523 | (insert " ")))) | |
524 | (insert-char (event-basic-type last-command-event) | |
525 | (prefix-numeric-value arg)))) | |
526 | ||
527 | (defun insert-parentheses (&optional arg) | |
a64dc1a4 LT |
528 | "Enclose following ARG sexps in parentheses. |
529 | Leave point after open-paren. | |
5891bf24 JL |
530 | A negative ARG encloses the preceding ARG sexps instead. |
531 | No argument is equivalent to zero: just insert `()' and leave point between. | |
532 | If `parens-require-spaces' is non-nil, this command also inserts a space | |
533 | before and after, depending on the surrounding characters. | |
8fc2ac41 AM |
534 | If region is active, insert enclosing characters at region boundaries. |
535 | ||
536 | This command assumes point is not in a string or comment." | |
5891bf24 JL |
537 | (interactive "P") |
538 | (insert-pair arg ?\( ?\))) | |
b73b9811 | 539 | |
d97c8198 JL |
540 | (defun delete-pair () |
541 | "Delete a pair of characters enclosing the sexp that follows point." | |
542 | (interactive) | |
543 | (save-excursion (forward-sexp 1) (delete-char -1)) | |
544 | (delete-char 1)) | |
545 | ||
546 | (defun raise-sexp (&optional arg) | |
547 | "Raise ARG sexps higher up the tree." | |
548 | (interactive "p") | |
549 | (let ((s (if (and transient-mark-mode mark-active) | |
550 | (buffer-substring (region-beginning) (region-end)) | |
551 | (buffer-substring | |
552 | (point) | |
553 | (save-excursion (forward-sexp arg) (point)))))) | |
554 | (backward-up-list 1) | |
555 | (delete-region (point) (save-excursion (forward-sexp 1) (point))) | |
556 | (save-excursion (insert s)))) | |
557 | ||
b73b9811 | 558 | (defun move-past-close-and-reindent () |
559 | "Move past next `)', delete indentation before it, then indent after it." | |
560 | (interactive) | |
561 | (up-list 1) | |
562 | (forward-char -1) | |
563 | (while (save-excursion ; this is my contribution | |
564 | (let ((before-paren (point))) | |
565 | (back-to-indentation) | |
adce3b5f RS |
566 | (and (= (point) before-paren) |
567 | (progn | |
568 | ;; Move to end of previous line. | |
569 | (beginning-of-line) | |
570 | (forward-char -1) | |
571 | ;; Verify it doesn't end within a string or comment. | |
572 | (let ((end (point)) | |
573 | state) | |
574 | (beginning-of-line) | |
575 | ;; Get state at start of line. | |
c6eeec65 | 576 | (setq state (list 0 nil nil |
adce3b5f RS |
577 | (null (calculate-lisp-indent)) |
578 | nil nil nil nil | |
579 | nil)) | |
580 | ;; Parse state across the line to get state at end. | |
581 | (setq state (parse-partial-sexp (point) end nil nil | |
582 | state)) | |
583 | ;; Check not in string or comment. | |
584 | (and (not (elt state 3)) (not (elt state 4)))))))) | |
b73b9811 | 585 | (delete-indentation)) |
586 | (forward-char 1) | |
587 | (newline-and-indent)) | |
c6eeec65 DL |
588 | |
589 | (defun check-parens () ; lame name? | |
590 | "Check for unbalanced parentheses in the current buffer. | |
591 | More accurately, check the narrowed part of the buffer for unbalanced | |
592 | expressions (\"sexps\") in general. This is done according to the | |
593 | current syntax table and will find unbalanced brackets or quotes as | |
7bc10886 | 594 | appropriate. (See Info node `(emacs)Parentheses'.) If imbalance is |
8ad8cfa5 | 595 | found, an error is signaled and point is left at the first unbalanced |
7bc10886 | 596 | character." |
c6eeec65 DL |
597 | (interactive) |
598 | (condition-case data | |
599 | ;; Buffer can't have more than (point-max) sexps. | |
600 | (scan-sexps (point-min) (point-max)) | |
601 | (scan-error (goto-char (nth 2 data)) | |
602 | ;; Could print (nth 1 data), which is either | |
603 | ;; "Containing expression ends prematurely" or | |
604 | ;; "Unbalanced parentheses", but those may not be so | |
605 | ;; accurate/helpful, e.g. quotes may actually be | |
606 | ;; mismatched. | |
61e21607 | 607 | (error "Unmatched bracket or quote")))) |
b73b9811 | 608 | \f |
61e21607 | 609 | (defun field-complete (table &optional predicate) |
73ebf88f SM |
610 | (let ((minibuffer-completion-table table) |
611 | (minibuffer-completion-predicate predicate) | |
612 | ;; This made sense for lisp-complete-symbol, but for | |
613 | ;; field-complete, this is out of place. --Stef | |
614 | ;; (completion-annotate-function | |
615 | ;; (unless (eq predicate 'fboundp) | |
616 | ;; (lambda (str) | |
617 | ;; (if (fboundp (intern-soft str)) " <f>")))) | |
618 | ) | |
619 | (call-interactively 'minibuffer-complete))) | |
61e21607 | 620 | |
e50c4203 | 621 | (defun lisp-complete-symbol (&optional predicate) |
2eb9adab RS |
622 | "Perform completion on Lisp symbol preceding point. |
623 | Compare that symbol against the known Lisp symbols. | |
1fa1cb1b RS |
624 | If no characters can be completed, display a list of possible completions. |
625 | Repeating the command at that point scrolls the list. | |
2eb9adab | 626 | |
e50c4203 DL |
627 | When called from a program, optional arg PREDICATE is a predicate |
628 | determining which symbols are considered, e.g. `commandp'. | |
629 | If PREDICATE is nil, the context determines which symbols are | |
630 | considered. If the symbol starts just after an open-parenthesis, only | |
631 | symbols with function definitions are considered. Otherwise, all | |
632 | symbols with function definitions, values or properties are | |
633 | considered." | |
b73b9811 | 634 | (interactive) |
51ef56c4 SM |
635 | (let* ((data (lisp-completion-at-point predicate)) |
636 | (plist (nthcdr 3 data))) | |
ccaa4765 SM |
637 | (if (null data) |
638 | (minibuffer-message "Nothing to complete") | |
2403c841 SM |
639 | (let ((completion-extra-properties plist)) |
640 | (completion-in-region (nth 0 data) (nth 1 data) (nth 2 data) | |
ccaa4765 | 641 | (plist-get plist :predicate)))))) |
0ca12598 | 642 | |
51ef56c4 SM |
643 | |
644 | (defun lisp-completion-at-point (&optional predicate) | |
0ca12598 | 645 | "Function used for `completion-at-point-functions' in `emacs-lisp-mode'." |
51ef56c4 | 646 | ;; FIXME: the `end' could be after point? |
4c528197 | 647 | (with-syntax-table emacs-lisp-mode-syntax-table |
4c14013d JB |
648 | (let* ((pos (point)) |
649 | (beg (condition-case nil | |
650 | (save-excursion | |
651 | (backward-sexp 1) | |
652 | (skip-syntax-forward "'") | |
653 | (point)) | |
654 | (scan-error pos))) | |
655 | (predicate | |
656 | (or predicate | |
657 | (save-excursion | |
658 | (goto-char beg) | |
659 | (if (not (eq (char-before) ?\()) | |
660 | (lambda (sym) ;why not just nil ? -sm | |
661 | (or (boundp sym) (fboundp sym) | |
662 | (symbol-plist sym))) | |
663 | ;; Looks like a funcall position. Let's double check. | |
664 | (if (condition-case nil | |
665 | (progn (up-list -2) (forward-char 1) | |
666 | (eq (char-after) ?\()) | |
667 | (error nil)) | |
668 | ;; If the first element of the parent list is an open | |
669 | ;; paren we are probably not in a funcall position. | |
670 | ;; Maybe a `let' varlist or something. | |
671 | nil | |
672 | ;; Else, we assume that a function name is expected. | |
673 | 'fboundp))))) | |
674 | (end | |
675 | (unless (or (eq beg (point-max)) | |
676 | (member (char-syntax (char-after beg)) '(?\" ?\( ?\)))) | |
677 | (condition-case nil | |
678 | (save-excursion | |
679 | (goto-char beg) | |
680 | (forward-sexp 1) | |
681 | (when (>= (point) pos) | |
682 | (point))) | |
683 | (scan-error pos))))) | |
684 | (when end | |
685 | (list beg end obarray | |
686 | :predicate predicate | |
d2fc7e3d | 687 | :annotation-function |
4c14013d JB |
688 | (unless (eq predicate 'fboundp) |
689 | (lambda (str) (if (fboundp (intern-soft str)) " <f>")))))))) | |
6594deb0 ER |
690 | |
691 | ;;; lisp.el ends here |