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