Commit | Line | Data |
---|---|---|
bbcc4d97 | 1 | ;;; lisp.el --- Lisp editing commands for Emacs -*- lexical-binding:t -*- |
6594deb0 | 2 | |
ba318903 | 3 | ;; Copyright (C) 1985-1986, 1994, 2000-2014 Free Software Foundation, |
ab422c4d | 4 | ;; 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 | 48 | (defvar forward-sexp-function nil |
dd8791e9 SM |
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. | |
11ae6c5d SM |
55 | "If non-nil, `forward-sexp' delegates to this function. |
56 | Should take the same arguments and behave similarly to `forward-sexp'.") | |
57 | ||
b73b9811 | 58 | (defun forward-sexp (&optional arg) |
59 | "Move forward across one balanced expression (sexp). | |
c6eeec65 | 60 | With ARG, do it that many times. Negative arg -N means |
8fc2ac41 | 61 | move backward across N balanced expressions. |
ba947bc4 GM |
62 | This command assumes point is not in a string or comment. |
63 | Calls `forward-sexp-function' to do the work, if that is non-nil." | |
61eee794 | 64 | (interactive "^p") |
b73b9811 | 65 | (or arg (setq arg 1)) |
11ae6c5d SM |
66 | (if forward-sexp-function |
67 | (funcall forward-sexp-function arg) | |
68 | (goto-char (or (scan-sexps (point) arg) (buffer-end arg))) | |
69 | (if (< arg 0) (backward-prefix-chars)))) | |
b73b9811 | 70 | |
71 | (defun backward-sexp (&optional arg) | |
72 | "Move backward across one balanced expression (sexp). | |
c6eeec65 | 73 | With ARG, do it that many times. Negative arg -N means |
8fc2ac41 | 74 | move forward across N balanced expressions. |
ba947bc4 GM |
75 | This command assumes point is not in a string or comment. |
76 | Uses `forward-sexp' to do the work." | |
61eee794 | 77 | (interactive "^p") |
b73b9811 | 78 | (or arg (setq arg 1)) |
79 | (forward-sexp (- arg))) | |
80 | ||
afb62fdd | 81 | (defun mark-sexp (&optional arg allow-extend) |
b73b9811 | 82 | "Set mark ARG sexps from point. |
e3f99b91 | 83 | The place mark goes is the same place \\[forward-sexp] would |
f07493e7 | 84 | move to with the same argument. |
afb62fdd | 85 | Interactively, if this command is repeated |
a64dc1a4 | 86 | or (in Transient Mark mode) if the mark is active, |
8fc2ac41 AM |
87 | it marks the next ARG sexps after the ones already marked. |
88 | This command assumes point is not in a string or comment." | |
afb62fdd RS |
89 | (interactive "P\np") |
90 | (cond ((and allow-extend | |
91 | (or (and (eq last-command this-command) (mark t)) | |
92 | (and transient-mark-mode mark-active))) | |
3610d3c9 | 93 | (setq arg (if arg (prefix-numeric-value arg) |
967e1a52 | 94 | (if (< (mark) (point)) -1 1))) |
cad113ae KG |
95 | (set-mark |
96 | (save-excursion | |
967e1a52 JL |
97 | (goto-char (mark)) |
98 | (forward-sexp arg) | |
99 | (point)))) | |
cad113ae KG |
100 | (t |
101 | (push-mark | |
102 | (save-excursion | |
3610d3c9 | 103 | (forward-sexp (prefix-numeric-value arg)) |
cad113ae KG |
104 | (point)) |
105 | nil t)))) | |
b73b9811 | 106 | |
107 | (defun forward-list (&optional arg) | |
108 | "Move forward across one balanced group of parentheses. | |
c6eeec65 | 109 | With ARG, do it that many times. |
8fc2ac41 AM |
110 | Negative arg -N means move backward 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 | (goto-char (or (scan-lists (point) arg 0) (buffer-end arg)))) | |
115 | ||
116 | (defun backward-list (&optional arg) | |
117 | "Move backward across one balanced group of parentheses. | |
c6eeec65 | 118 | With ARG, do it that many times. |
8fc2ac41 AM |
119 | Negative arg -N means move forward across N groups of parentheses. |
120 | This command assumes point is not in a string or comment." | |
61eee794 | 121 | (interactive "^p") |
b73b9811 | 122 | (or arg (setq arg 1)) |
123 | (forward-list (- arg))) | |
124 | ||
e50c4203 | 125 | (defun down-list (&optional arg) |
b73b9811 | 126 | "Move forward down one level of parentheses. |
c6eeec65 | 127 | With ARG, do this that many times. |
8fc2ac41 AM |
128 | A negative argument means move backward but still go down a level. |
129 | This command assumes point is not in a string or comment." | |
61eee794 | 130 | (interactive "^p") |
e50c4203 | 131 | (or arg (setq arg 1)) |
b73b9811 | 132 | (let ((inc (if (> arg 0) 1 -1))) |
133 | (while (/= arg 0) | |
134 | (goto-char (or (scan-lists (point) inc -1) (buffer-end arg))) | |
135 | (setq arg (- arg inc))))) | |
136 | ||
e50c4203 | 137 | (defun backward-up-list (&optional arg) |
b73b9811 | 138 | "Move backward out of one level of parentheses. |
c6eeec65 | 139 | With ARG, do this that many times. |
8fc2ac41 AM |
140 | A negative argument means move forward 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 | (up-list (- (or arg 1)))) |
b73b9811 | 144 | |
e50c4203 | 145 | (defun up-list (&optional arg) |
b73b9811 | 146 | "Move forward out of one level of parentheses. |
c6eeec65 | 147 | With ARG, do this that many times. |
8fc2ac41 AM |
148 | A negative argument means move backward but still to a less deep spot. |
149 | This command assumes point is not in a string or comment." | |
61eee794 | 150 | (interactive "^p") |
e50c4203 | 151 | (or arg (setq arg 1)) |
984edd22 SM |
152 | (let ((inc (if (> arg 0) 1 -1)) |
153 | pos) | |
b73b9811 | 154 | (while (/= arg 0) |
984edd22 SM |
155 | (if (null forward-sexp-function) |
156 | (goto-char (or (scan-lists (point) inc 1) (buffer-end arg))) | |
e44e373d SM |
157 | (condition-case err |
158 | (while (progn (setq pos (point)) | |
159 | (forward-sexp inc) | |
160 | (/= (point) pos))) | |
161 | (scan-error (goto-char (nth (if (> arg 0) 3 2) err)))) | |
162 | (if (= (point) pos) | |
984edd22 SM |
163 | (signal 'scan-error |
164 | (list "Unbalanced parentheses" (point) (point))))) | |
b73b9811 | 165 | (setq arg (- arg inc))))) |
166 | ||
e50c4203 | 167 | (defun kill-sexp (&optional arg) |
dd60a465 RS |
168 | "Kill the sexp (balanced expression) following point. |
169 | With ARG, kill that many sexps after point. | |
8fc2ac41 AM |
170 | Negative arg -N means kill N sexps before point. |
171 | This command assumes point is not in a string or comment." | |
b73b9811 | 172 | (interactive "p") |
173 | (let ((opoint (point))) | |
e50c4203 | 174 | (forward-sexp (or arg 1)) |
b73b9811 | 175 | (kill-region opoint (point)))) |
176 | ||
e50c4203 | 177 | (defun backward-kill-sexp (&optional arg) |
dd60a465 RS |
178 | "Kill the sexp (balanced expression) preceding point. |
179 | With ARG, kill that many sexps before point. | |
8fc2ac41 AM |
180 | Negative arg -N means kill N sexps after point. |
181 | This command assumes point is not in a string or comment." | |
b73b9811 | 182 | (interactive "p") |
e50c4203 | 183 | (kill-sexp (- (or arg 1)))) |
de6d64b2 EZ |
184 | |
185 | ;; After Zmacs: | |
186 | (defun kill-backward-up-list (&optional arg) | |
187 | "Kill the form containing the current sexp, leaving the sexp itself. | |
188 | A prefix argument ARG causes the relevant number of surrounding | |
8fc2ac41 AM |
189 | forms to be removed. |
190 | This command assumes point is not in a string or comment." | |
de6d64b2 EZ |
191 | (interactive "*p") |
192 | (let ((current-sexp (thing-at-point 'sexp))) | |
193 | (if current-sexp | |
194 | (save-excursion | |
195 | (backward-up-list arg) | |
196 | (kill-sexp) | |
197 | (insert current-sexp)) | |
198 | (error "Not at a sexp")))) | |
b73b9811 | 199 | \f |
ab22ee53 | 200 | (defvar beginning-of-defun-function nil |
c6eeec65 DL |
201 | "If non-nil, function for `beginning-of-defun-raw' to call. |
202 | This is used to find the beginning of the defun instead of using the | |
ab22ee53 RS |
203 | normal recipe (see `beginning-of-defun'). Major modes can define this |
204 | if defining `defun-prompt-regexp' is not sufficient to handle the mode's | |
205 | needs. | |
c6eeec65 | 206 | |
50bfa18a SM |
207 | The function takes the same argument as `beginning-of-defun' and should |
208 | behave similarly, returning non-nil if it found the beginning of a defun. | |
209 | Ideally it should move to a point right before an open-paren which encloses | |
210 | the body of the defun.") | |
c6eeec65 | 211 | |
b73b9811 | 212 | (defun beginning-of-defun (&optional arg) |
213 | "Move backward to the beginning of a defun. | |
6957495d CY |
214 | With ARG, do it that many times. Negative ARG means move forward |
215 | to the ARGth following beginning of defun. | |
216 | ||
217 | If search is successful, return t; point ends up at the beginning | |
218 | of the line where the search succeeded. Otherwise, return nil. | |
219 | ||
220 | When `open-paren-in-column-0-is-defun-start' is non-nil, a defun | |
221 | is assumed to start where there is a char with open-parenthesis | |
222 | syntax at the beginning of a line. If `defun-prompt-regexp' is | |
223 | non-nil, then a string which matches that regexp may also precede | |
224 | the open-parenthesis. If `defun-prompt-regexp' and | |
225 | `open-paren-in-column-0-is-defun-start' are both nil, this | |
226 | function instead finds an open-paren at the outermost level. | |
227 | ||
228 | If the variable `beginning-of-defun-function' is non-nil, its | |
229 | value is called as a function, with argument ARG, to find the | |
230 | defun's beginning. | |
231 | ||
232 | Regardless of the values of `defun-prompt-regexp' and | |
233 | `beginning-of-defun-function', point always moves to the | |
234 | beginning of the line whenever the search is successful." | |
61eee794 | 235 | (interactive "^p") |
90c08845 | 236 | (or (not (eq this-command 'beginning-of-defun)) |
967e1a52 JL |
237 | (eq last-command 'beginning-of-defun) |
238 | (and transient-mark-mode mark-active) | |
239 | (push-mark)) | |
afa995e1 KH |
240 | (and (beginning-of-defun-raw arg) |
241 | (progn (beginning-of-line) t))) | |
242 | ||
243 | (defun beginning-of-defun-raw (&optional arg) | |
244 | "Move point to the character that starts a defun. | |
c6eeec65 DL |
245 | This is identical to function `beginning-of-defun', except that point |
246 | does not move to the beginning of the line when `defun-prompt-regexp' | |
247 | is non-nil. | |
248 | ||
ab22ee53 RS |
249 | If variable `beginning-of-defun-function' is non-nil, its value |
250 | is called as a function to find the defun's beginning." | |
61eee794 | 251 | (interactive "^p") ; change this to "P", maybe, if we ever come to pass ARG |
4df5698c CY |
252 | ; to beginning-of-defun-function. |
253 | (unless arg (setq arg 1)) | |
6cb54822 AM |
254 | (cond |
255 | (beginning-of-defun-function | |
50bfa18a SM |
256 | (condition-case nil |
257 | (funcall beginning-of-defun-function arg) | |
258 | ;; We used to define beginning-of-defun-function as taking no argument | |
259 | ;; but that makes it impossible to implement correct forward motion: | |
260 | ;; we used to use end-of-defun for that, but it's not supposed to do | |
261 | ;; the same thing (it moves to the end of a defun not to the beginning | |
262 | ;; of the next). | |
263 | ;; In case the beginning-of-defun-function uses the old calling | |
264 | ;; convention, fallback on the old implementation. | |
265 | (wrong-number-of-arguments | |
266 | (if (> arg 0) | |
bbcc4d97 | 267 | (dotimes (_ arg) |
50bfa18a | 268 | (funcall beginning-of-defun-function)) |
bbcc4d97 | 269 | (dotimes (_ (- arg)) |
8e911f6f | 270 | (funcall end-of-defun-function)))))) |
6cb54822 AM |
271 | |
272 | ((or defun-prompt-regexp open-paren-in-column-0-is-defun-start) | |
273 | (and (< arg 0) (not (eobp)) (forward-char 1)) | |
c6eeec65 | 274 | (and (re-search-backward (if defun-prompt-regexp |
418d645c GM |
275 | (concat (if open-paren-in-column-0-is-defun-start |
276 | "^\\s(\\|" "") | |
b5ed9def | 277 | "\\(?:" defun-prompt-regexp "\\)\\s(") |
c6eeec65 | 278 | "^\\s(") |
6cb54822 | 279 | nil 'move arg) |
4114bc49 SM |
280 | (progn (goto-char (1- (match-end 0))) |
281 | t))) | |
6cb54822 | 282 | |
4df5698c CY |
283 | ;; If open-paren-in-column-0-is-defun-start and defun-prompt-regexp |
284 | ;; are both nil, column 0 has no significance - so scan forward | |
285 | ;; from BOB to see how nested point is, then carry on from there. | |
286 | ;; | |
287 | ;; It is generally not a good idea to land up here, because the | |
288 | ;; call to scan-lists below can be extremely slow. This is because | |
289 | ;; back_comment in syntax.c may have to scan from bob to find the | |
290 | ;; beginning of each comment. Fixing this is not trivial -- cyd. | |
291 | ||
292 | ((eq arg 0)) | |
6cb54822 | 293 | (t |
4df5698c CY |
294 | (let ((floor (point-min)) |
295 | (ceiling (point-max)) | |
296 | (arg-+ve (> arg 0))) | |
6cb54822 AM |
297 | (save-restriction |
298 | (widen) | |
4df5698c CY |
299 | (let ((ppss (let (syntax-begin-function |
300 | font-lock-beginning-of-syntax-function) | |
301 | (syntax-ppss))) | |
302 | ;; position of least enclosing paren, or nil. | |
303 | encl-pos) | |
304 | ;; Back out of any comment/string, so that encl-pos will always | |
305 | ;; become nil if we're at top-level. | |
306 | (when (nth 8 ppss) | |
307 | (goto-char (nth 8 ppss)) | |
308 | (setq ppss (syntax-ppss))) ; should be fast, due to cache. | |
309 | (setq encl-pos (syntax-ppss-toplevel-pos ppss)) | |
310 | (if encl-pos (goto-char encl-pos)) | |
311 | ||
312 | (and encl-pos arg-+ve (setq arg (1- arg))) | |
313 | (and (not encl-pos) (not arg-+ve) (not (looking-at "\\s(")) | |
314 | (setq arg (1+ arg))) | |
315 | ||
316 | (condition-case nil ; to catch crazy parens. | |
317 | (progn | |
318 | (goto-char (scan-lists (point) (- arg) 0)) | |
319 | (if arg-+ve | |
320 | (if (>= (point) floor) | |
321 | t | |
322 | (goto-char floor) | |
323 | nil) | |
324 | ;; forward to next (, or trigger the c-c | |
325 | (goto-char (1- (scan-lists (point) 1 -1))) | |
326 | (if (<= (point) ceiling) | |
327 | t | |
328 | (goto-char ceiling) | |
329 | nil))) | |
330 | (error | |
331 | (goto-char (if arg-+ve floor ceiling)) | |
332 | nil)))))))) | |
c6eeec65 | 333 | |
7bbab3e0 SM |
334 | (defvar end-of-defun-function |
335 | (lambda () (forward-sexp 1)) | |
61e21607 | 336 | "Function for `end-of-defun' to call. |
6d3e4d22 | 337 | This is used to find the end of the defun at point. |
61e21607 | 338 | It is called with no argument, right after calling `beginning-of-defun-raw'. |
6d3e4d22 SM |
339 | So the function can assume that point is at the beginning of the defun body. |
340 | It should move point to the first position after the defun.") | |
b73b9811 | 341 | |
342 | (defun buffer-end (arg) | |
a64dc1a4 | 343 | "Return the \"far end\" position of the buffer, in direction ARG. |
03deb635 RS |
344 | If ARG is positive, that's the end of the buffer. |
345 | Otherwise, that's the beginning of the buffer." | |
b73b9811 | 346 | (if (> arg 0) (point-max) (point-min))) |
347 | ||
348 | (defun end-of-defun (&optional arg) | |
a64dc1a4 LT |
349 | "Move forward to next end of defun. |
350 | With argument, do it that many times. | |
b73b9811 | 351 | Negative argument -N means move back to Nth preceding end of defun. |
352 | ||
c6eeec65 DL |
353 | An end of a defun occurs right after the close-parenthesis that |
354 | matches the open-parenthesis that starts a defun; see function | |
ab22ee53 RS |
355 | `beginning-of-defun'. |
356 | ||
357 | If variable `end-of-defun-function' is non-nil, its value | |
358 | is called as a function to find the defun's end." | |
61eee794 | 359 | (interactive "^p") |
90c08845 | 360 | (or (not (eq this-command 'end-of-defun)) |
967e1a52 JL |
361 | (eq last-command 'end-of-defun) |
362 | (and transient-mark-mode mark-active) | |
363 | (push-mark)) | |
44b254cc | 364 | (if (or (null arg) (= arg 0)) (setq arg 1)) |
f9f34ece SM |
365 | (let ((pos (point)) |
366 | (beg (progn (end-of-line 1) (beginning-of-defun-raw 1) (point)))) | |
367 | (funcall end-of-defun-function) | |
f2a8252a SM |
368 | ;; When comparing point against pos, we want to consider that if |
369 | ;; point was right after the end of the function, it's still | |
370 | ;; considered as "in that function". | |
371 | ;; E.g. `eval-defun' from right after the last close-paren. | |
372 | (unless (bolp) | |
373 | (skip-chars-forward " \t") | |
374 | (if (looking-at "\\s<\\|\n") | |
375 | (forward-line 1))) | |
f9f34ece SM |
376 | (cond |
377 | ((> arg 0) | |
378 | ;; Moving forward. | |
379 | (if (> (point) pos) | |
380 | ;; We already moved forward by one because we started from | |
381 | ;; within a function. | |
382 | (setq arg (1- arg)) | |
383 | ;; We started from after the end of the previous function. | |
384 | (goto-char pos)) | |
385 | (unless (zerop arg) | |
386 | (beginning-of-defun-raw (- arg)) | |
387 | (funcall end-of-defun-function))) | |
388 | ((< arg 0) | |
389 | ;; Moving backward. | |
390 | (if (< (point) pos) | |
391 | ;; We already moved backward because we started from between | |
392 | ;; two functions. | |
393 | (setq arg (1+ arg)) | |
394 | ;; We started from inside a function. | |
395 | (goto-char beg)) | |
396 | (unless (zerop arg) | |
397 | (beginning-of-defun-raw (- arg)) | |
398 | (funcall end-of-defun-function)))) | |
399 | (unless (bolp) | |
400 | (skip-chars-forward " \t") | |
401 | (if (looking-at "\\s<\\|\n") | |
402 | (forward-line 1))))) | |
b73b9811 | 403 | |
afb62fdd | 404 | (defun mark-defun (&optional allow-extend) |
b73b9811 | 405 | "Put mark at end of this defun, point at beginning. |
cad113ae | 406 | The defun marked is the one that contains point or follows point. |
afb62fdd RS |
407 | |
408 | Interactively, if this command is repeated | |
a64dc1a4 | 409 | or (in Transient Mark mode) if the mark is active, |
afb62fdd RS |
410 | it marks the next defun after the ones already marked." |
411 | (interactive "p") | |
412 | (cond ((and allow-extend | |
413 | (or (and (eq last-command this-command) (mark t)) | |
414 | (and transient-mark-mode mark-active))) | |
be0d25b6 KG |
415 | (set-mark |
416 | (save-excursion | |
417 | (goto-char (mark)) | |
418 | (end-of-defun) | |
419 | (point)))) | |
420 | (t | |
d3d6bc9b RS |
421 | (let ((opoint (point)) |
422 | beg end) | |
423 | (push-mark opoint) | |
424 | ;; Try first in this order for the sake of languages with nested | |
425 | ;; functions where several can end at the same place as with | |
426 | ;; the offside rule, e.g. Python. | |
427 | (beginning-of-defun) | |
428 | (setq beg (point)) | |
429 | (end-of-defun) | |
430 | (setq end (point)) | |
431 | (while (looking-at "^\n") | |
432 | (forward-line 1)) | |
433 | (if (> (point) opoint) | |
434 | (progn | |
435 | ;; We got the right defun. | |
436 | (push-mark beg nil t) | |
437 | (goto-char end) | |
438 | (exchange-point-and-mark)) | |
439 | ;; beginning-of-defun moved back one defun | |
440 | ;; so we got the wrong one. | |
441 | (goto-char opoint) | |
442 | (end-of-defun) | |
443 | (push-mark (point) nil t) | |
444 | (beginning-of-defun)) | |
445 | (re-search-backward "^\n" (- (point) 1) t))))) | |
b73b9811 | 446 | |
bbcc4d97 | 447 | (defun narrow-to-defun (&optional _arg) |
3f7e952d | 448 | "Make text outside current defun invisible. |
c6eeec65 DL |
449 | The defun visible is the one that contains point or follows point. |
450 | Optional ARG is ignored." | |
3f7e952d RS |
451 | (interactive) |
452 | (save-excursion | |
453 | (widen) | |
d3d6bc9b RS |
454 | (let ((opoint (point)) |
455 | beg end) | |
456 | ;; Try first in this order for the sake of languages with nested | |
457 | ;; functions where several can end at the same place as with | |
458 | ;; the offside rule, e.g. Python. | |
050cc68b LB |
459 | |
460 | ;; Finding the start of the function is a bit problematic since | |
461 | ;; `beginning-of-defun' when we are on the first character of | |
462 | ;; the function might go to the previous function. | |
463 | ;; | |
464 | ;; Therefore we first move one character forward and then call | |
465 | ;; `beginning-of-defun'. However now we must check that we did | |
466 | ;; not move into the next function. | |
467 | (let ((here (point))) | |
468 | (unless (eolp) | |
469 | (forward-char)) | |
470 | (beginning-of-defun) | |
471 | (when (< (point) here) | |
472 | (goto-char here) | |
473 | (beginning-of-defun))) | |
d3d6bc9b | 474 | (setq beg (point)) |
750e563f | 475 | (end-of-defun) |
d3d6bc9b RS |
476 | (setq end (point)) |
477 | (while (looking-at "^\n") | |
478 | (forward-line 1)) | |
479 | (unless (> (point) opoint) | |
480 | ;; beginning-of-defun moved back one defun | |
481 | ;; so we got the wrong one. | |
482 | (goto-char opoint) | |
483 | (end-of-defun) | |
484 | (setq end (point)) | |
485 | (beginning-of-defun) | |
486 | (setq beg (point))) | |
487 | (goto-char end) | |
488 | (re-search-backward "^\n" (- (point) 1) t) | |
489 | (narrow-to-region beg end)))) | |
3f7e952d | 490 | |
d97c8198 JL |
491 | (defvar insert-pair-alist |
492 | '((?\( ?\)) (?\[ ?\]) (?\{ ?\}) (?\< ?\>) (?\" ?\") (?\' ?\') (?\` ?\')) | |
493 | "Alist of paired characters inserted by `insert-pair'. | |
494 | Each element looks like (OPEN-CHAR CLOSE-CHAR) or (COMMAND-CHAR | |
495 | OPEN-CHAR CLOSE-CHAR). The characters OPEN-CHAR and CLOSE-CHAR | |
496 | of the pair whose key is equal to the last input character with | |
497 | or without modifiers, are inserted by `insert-pair'.") | |
498 | ||
499 | (defun insert-pair (&optional arg open close) | |
5891bf24 JL |
500 | "Enclose following ARG sexps in a pair of OPEN and CLOSE characters. |
501 | Leave point after the first character. | |
e3ac26cb | 502 | A negative ARG encloses the preceding ARG sexps instead. |
5891bf24 JL |
503 | No argument is equivalent to zero: just insert characters |
504 | and leave point between. | |
d5bfe076 | 505 | If `parens-require-spaces' is non-nil, this command also inserts a space |
5891bf24 | 506 | before and after, depending on the surrounding characters. |
d97c8198 JL |
507 | If region is active, insert enclosing characters at region boundaries. |
508 | ||
509 | If arguments OPEN and CLOSE are nil, the character pair is found | |
510 | from the variable `insert-pair-alist' according to the last input | |
511 | character with or without modifiers. If no character pair is | |
512 | found in the variable `insert-pair-alist', then the last input | |
8fc2ac41 AM |
513 | character is inserted ARG times. |
514 | ||
515 | This command assumes point is not in a string or comment." | |
b73b9811 | 516 | (interactive "P") |
d97c8198 | 517 | (if (not (and open close)) |
61a846fb | 518 | (let ((pair (or (assq last-command-event insert-pair-alist) |
d97c8198 JL |
519 | (assq (event-basic-type last-command-event) |
520 | insert-pair-alist)))) | |
521 | (if pair | |
522 | (if (nth 2 pair) | |
523 | (setq open (nth 1 pair) close (nth 2 pair)) | |
524 | (setq open (nth 0 pair) close (nth 1 pair)))))) | |
525 | (if (and open close) | |
526 | (if (and transient-mark-mode mark-active) | |
527 | (progn | |
528 | (save-excursion (goto-char (region-end)) (insert close)) | |
529 | (save-excursion (goto-char (region-beginning)) (insert open))) | |
530 | (if arg (setq arg (prefix-numeric-value arg)) | |
531 | (setq arg 0)) | |
532 | (cond ((> arg 0) (skip-chars-forward " \t")) | |
533 | ((< arg 0) (forward-sexp arg) (setq arg (- arg)))) | |
534 | (and parens-require-spaces | |
535 | (not (bobp)) | |
536 | (memq (char-syntax (preceding-char)) (list ?w ?_ (char-syntax close))) | |
537 | (insert " ")) | |
538 | (insert open) | |
539 | (save-excursion | |
540 | (or (eq arg 0) (forward-sexp arg)) | |
541 | (insert close) | |
542 | (and parens-require-spaces | |
543 | (not (eobp)) | |
544 | (memq (char-syntax (following-char)) (list ?w ?_ (char-syntax open))) | |
545 | (insert " ")))) | |
546 | (insert-char (event-basic-type last-command-event) | |
547 | (prefix-numeric-value arg)))) | |
548 | ||
549 | (defun insert-parentheses (&optional arg) | |
a64dc1a4 LT |
550 | "Enclose following ARG sexps in parentheses. |
551 | Leave point after open-paren. | |
5891bf24 JL |
552 | A negative ARG encloses the preceding ARG sexps instead. |
553 | No argument is equivalent to zero: just insert `()' and leave point between. | |
554 | If `parens-require-spaces' is non-nil, this command also inserts a space | |
555 | before and after, depending on the surrounding characters. | |
8fc2ac41 AM |
556 | If region is active, insert enclosing characters at region boundaries. |
557 | ||
558 | This command assumes point is not in a string or comment." | |
5891bf24 JL |
559 | (interactive "P") |
560 | (insert-pair arg ?\( ?\))) | |
b73b9811 | 561 | |
d97c8198 JL |
562 | (defun delete-pair () |
563 | "Delete a pair of characters enclosing the sexp that follows point." | |
564 | (interactive) | |
565 | (save-excursion (forward-sexp 1) (delete-char -1)) | |
566 | (delete-char 1)) | |
567 | ||
568 | (defun raise-sexp (&optional arg) | |
569 | "Raise ARG sexps higher up the tree." | |
570 | (interactive "p") | |
571 | (let ((s (if (and transient-mark-mode mark-active) | |
572 | (buffer-substring (region-beginning) (region-end)) | |
573 | (buffer-substring | |
574 | (point) | |
575 | (save-excursion (forward-sexp arg) (point)))))) | |
576 | (backward-up-list 1) | |
577 | (delete-region (point) (save-excursion (forward-sexp 1) (point))) | |
578 | (save-excursion (insert s)))) | |
579 | ||
b73b9811 | 580 | (defun move-past-close-and-reindent () |
581 | "Move past next `)', delete indentation before it, then indent after it." | |
582 | (interactive) | |
583 | (up-list 1) | |
584 | (forward-char -1) | |
585 | (while (save-excursion ; this is my contribution | |
586 | (let ((before-paren (point))) | |
587 | (back-to-indentation) | |
adce3b5f RS |
588 | (and (= (point) before-paren) |
589 | (progn | |
590 | ;; Move to end of previous line. | |
591 | (beginning-of-line) | |
592 | (forward-char -1) | |
593 | ;; Verify it doesn't end within a string or comment. | |
594 | (let ((end (point)) | |
595 | state) | |
596 | (beginning-of-line) | |
597 | ;; Get state at start of line. | |
c6eeec65 | 598 | (setq state (list 0 nil nil |
adce3b5f RS |
599 | (null (calculate-lisp-indent)) |
600 | nil nil nil nil | |
601 | nil)) | |
602 | ;; Parse state across the line to get state at end. | |
603 | (setq state (parse-partial-sexp (point) end nil nil | |
604 | state)) | |
605 | ;; Check not in string or comment. | |
606 | (and (not (elt state 3)) (not (elt state 4)))))))) | |
b73b9811 | 607 | (delete-indentation)) |
608 | (forward-char 1) | |
609 | (newline-and-indent)) | |
c6eeec65 DL |
610 | |
611 | (defun check-parens () ; lame name? | |
612 | "Check for unbalanced parentheses in the current buffer. | |
613 | More accurately, check the narrowed part of the buffer for unbalanced | |
614 | expressions (\"sexps\") in general. This is done according to the | |
615 | current syntax table and will find unbalanced brackets or quotes as | |
7bc10886 | 616 | appropriate. (See Info node `(emacs)Parentheses'.) If imbalance is |
8ad8cfa5 | 617 | found, an error is signaled and point is left at the first unbalanced |
7bc10886 | 618 | character." |
c6eeec65 DL |
619 | (interactive) |
620 | (condition-case data | |
621 | ;; Buffer can't have more than (point-max) sexps. | |
622 | (scan-sexps (point-min) (point-max)) | |
623 | (scan-error (goto-char (nth 2 data)) | |
624 | ;; Could print (nth 1 data), which is either | |
625 | ;; "Containing expression ends prematurely" or | |
626 | ;; "Unbalanced parentheses", but those may not be so | |
627 | ;; accurate/helpful, e.g. quotes may actually be | |
628 | ;; mismatched. | |
dd8791e9 | 629 | (user-error "Unmatched bracket or quote")))) |
b73b9811 | 630 | \f |
61e21607 | 631 | (defun field-complete (table &optional predicate) |
dd8791e9 | 632 | (declare (obsolete completion-in-region "24.4")) |
73ebf88f SM |
633 | (let ((minibuffer-completion-table table) |
634 | (minibuffer-completion-predicate predicate) | |
635 | ;; This made sense for lisp-complete-symbol, but for | |
636 | ;; field-complete, this is out of place. --Stef | |
637 | ;; (completion-annotate-function | |
638 | ;; (unless (eq predicate 'fboundp) | |
639 | ;; (lambda (str) | |
640 | ;; (if (fboundp (intern-soft str)) " <f>")))) | |
641 | ) | |
642 | (call-interactively 'minibuffer-complete))) | |
61e21607 | 643 | |
e50c4203 | 644 | (defun lisp-complete-symbol (&optional predicate) |
2eb9adab RS |
645 | "Perform completion on Lisp symbol preceding point. |
646 | Compare that symbol against the known Lisp symbols. | |
1fa1cb1b RS |
647 | If no characters can be completed, display a list of possible completions. |
648 | Repeating the command at that point scrolls the list. | |
2eb9adab | 649 | |
e50c4203 DL |
650 | When called from a program, optional arg PREDICATE is a predicate |
651 | determining which symbols are considered, e.g. `commandp'. | |
652 | If PREDICATE is nil, the context determines which symbols are | |
653 | considered. If the symbol starts just after an open-parenthesis, only | |
654 | symbols with function definitions are considered. Otherwise, all | |
655 | symbols with function definitions, values or properties are | |
656 | considered." | |
dd8791e9 | 657 | (declare (obsolete completion-at-point "24.4")) |
b73b9811 | 658 | (interactive) |
51ef56c4 SM |
659 | (let* ((data (lisp-completion-at-point predicate)) |
660 | (plist (nthcdr 3 data))) | |
ccaa4765 SM |
661 | (if (null data) |
662 | (minibuffer-message "Nothing to complete") | |
2403c841 SM |
663 | (let ((completion-extra-properties plist)) |
664 | (completion-in-region (nth 0 data) (nth 1 data) (nth 2 data) | |
ccaa4765 | 665 | (plist-get plist :predicate)))))) |
0ca12598 | 666 | |
bbcc4d97 SM |
667 | (defun lisp--local-variables-1 (vars sexp) |
668 | "Return the vars locally bound around the witness, or nil if not found." | |
669 | (let (res) | |
670 | (while | |
671 | (unless | |
672 | (setq res | |
673 | (pcase sexp | |
674 | (`(,(or `let `let*) ,bindings) | |
675 | (let ((vars vars)) | |
676 | (when (eq 'let* (car sexp)) | |
677 | (dolist (binding (cdr (reverse bindings))) | |
678 | (push (or (car-safe binding) binding) vars))) | |
679 | (lisp--local-variables-1 | |
680 | vars (car (cdr-safe (car (last bindings))))))) | |
681 | (`(,(or `let `let*) ,bindings . ,body) | |
682 | (let ((vars vars)) | |
683 | (dolist (binding bindings) | |
684 | (push (or (car-safe binding) binding) vars)) | |
685 | (lisp--local-variables-1 vars (car (last body))))) | |
686 | (`(lambda ,_) (setq sexp nil)) | |
687 | (`(lambda ,args . ,body) | |
688 | (lisp--local-variables-1 | |
689 | (append args vars) (car (last body)))) | |
690 | (`(condition-case ,_ ,e) (lisp--local-variables-1 vars e)) | |
691 | (`(condition-case ,v ,_ . ,catches) | |
692 | (lisp--local-variables-1 | |
693 | (cons v vars) (cdr (car (last catches))))) | |
694 | (`(,_ . ,_) | |
695 | (lisp--local-variables-1 vars (car (last sexp)))) | |
696 | (`lisp--witness--lisp (or vars '(nil))) | |
697 | (_ nil))) | |
698 | (setq sexp (ignore-errors (butlast sexp))))) | |
699 | res)) | |
700 | ||
701 | (defun lisp--local-variables () | |
702 | "Return a list of locally let-bound variables at point." | |
703 | (save-excursion | |
704 | (skip-syntax-backward "w_") | |
705 | (let* ((ppss (syntax-ppss)) | |
706 | (txt (buffer-substring-no-properties (or (car (nth 9 ppss)) (point)) | |
707 | (or (nth 8 ppss) (point)))) | |
708 | (closer ())) | |
709 | (dolist (p (nth 9 ppss)) | |
710 | (push (cdr (syntax-after p)) closer)) | |
711 | (setq closer (apply #'string closer)) | |
712 | (let* ((sexp (car (read-from-string | |
713 | (concat txt "lisp--witness--lisp" closer)))) | |
714 | (macroexpand-advice (lambda (expander form &rest args) | |
715 | (condition-case nil | |
716 | (apply expander form args) | |
717 | (error form)))) | |
718 | (sexp | |
719 | (unwind-protect | |
720 | (progn | |
721 | (advice-add 'macroexpand :around macroexpand-advice) | |
722 | (macroexpand-all sexp)) | |
723 | (advice-remove 'macroexpand macroexpand-advice))) | |
724 | (vars (lisp--local-variables-1 nil sexp))) | |
725 | (delq nil | |
726 | (mapcar (lambda (var) | |
727 | (and (symbolp var) | |
728 | (not (string-match (symbol-name var) "\\`[&_]")) | |
729 | ;; Eliminate uninterned vars. | |
730 | (intern-soft var) | |
731 | var)) | |
732 | vars)))))) | |
733 | ||
734 | (defvar lisp--local-variables-completion-table | |
735 | ;; Use `defvar' rather than `defconst' since defconst would purecopy this | |
736 | ;; value, which would doubly fail: it would fail because purecopy can't | |
737 | ;; handle the recursive bytecode object, and it would fail because it would | |
738 | ;; move `lastpos' and `lastvars' to pure space where they'd be immutable! | |
739 | (let ((lastpos nil) (lastvars nil)) | |
740 | (letrec ((hookfun (lambda () | |
741 | (setq lastpos nil) | |
742 | (remove-hook 'post-command-hook hookfun)))) | |
743 | (completion-table-dynamic | |
744 | (lambda (_string) | |
745 | (save-excursion | |
746 | (skip-syntax-backward "_w") | |
747 | (let ((newpos (cons (point) (current-buffer)))) | |
748 | (unless (equal lastpos newpos) | |
749 | (add-hook 'post-command-hook hookfun) | |
750 | (setq lastpos newpos) | |
751 | (setq lastvars | |
752 | (mapcar #'symbol-name (lisp--local-variables)))))) | |
753 | lastvars))))) | |
754 | ||
2da4c3ab SM |
755 | ;; FIXME: Support for Company brings in features which straddle eldoc. |
756 | ;; We should consolidate this, so that major modes can provide all that | |
757 | ;; data all at once: | |
758 | ;; - a function to extract "the reference at point" (may be more complex | |
759 | ;; than a mere string, to distinguish various namespaces). | |
760 | ;; - a function to jump to such a reference. | |
761 | ;; - a function to show the signature/interface of such a reference. | |
762 | ;; - a function to build a help-buffer about that reference. | |
763 | ;; FIXME: Those functions should also be used by the normal completion code in | |
764 | ;; the *Completions* buffer. | |
765 | ||
766 | (defun lisp--company-doc-buffer (str) | |
767 | (let ((symbol (intern-soft str))) | |
768 | ;; FIXME: we really don't want to "display-buffer and then undo it". | |
769 | (save-window-excursion | |
770 | ;; Make sure we don't display it in another frame, otherwise | |
771 | ;; save-window-excursion won't be able to undo it. | |
772 | (let ((display-buffer-overriding-action | |
773 | '(nil . ((inhibit-switch-frame . t))))) | |
774 | (ignore-errors | |
775 | (cond | |
776 | ((fboundp symbol) (describe-function symbol)) | |
777 | ((boundp symbol) (describe-variable symbol)) | |
778 | ((featurep symbol) (describe-package symbol)) | |
779 | ((facep symbol) (describe-face symbol)) | |
780 | (t (signal 'user-error nil))) | |
781 | (help-buffer)))))) | |
782 | ||
783 | (defun lisp--company-doc-string (str) | |
784 | (let* ((symbol (intern-soft str)) | |
785 | (doc (if (fboundp symbol) | |
786 | (documentation symbol t) | |
787 | (documentation-property symbol 'variable-documentation t)))) | |
788 | (and (stringp doc) | |
789 | (string-match ".*$" doc) | |
790 | (match-string 0 doc)))) | |
791 | ||
792 | (declare-function find-library-name "find-func" (library)) | |
793 | ||
794 | (defun lisp--company-location (str) | |
795 | (let ((sym (intern-soft str))) | |
796 | (cond | |
797 | ((fboundp sym) (find-definition-noselect sym nil)) | |
798 | ((boundp sym) (find-definition-noselect sym 'defvar)) | |
799 | ((featurep sym) | |
800 | (require 'find-func) | |
801 | (cons (find-file-noselect (find-library-name | |
802 | (symbol-name sym))) | |
803 | 0)) | |
804 | ((facep sym) (find-definition-noselect sym 'defface))))) | |
805 | ||
bbcc4d97 | 806 | (defun lisp-completion-at-point (&optional _predicate) |
0ca12598 | 807 | "Function used for `completion-at-point-functions' in `emacs-lisp-mode'." |
4c528197 | 808 | (with-syntax-table emacs-lisp-mode-syntax-table |
4c14013d JB |
809 | (let* ((pos (point)) |
810 | (beg (condition-case nil | |
811 | (save-excursion | |
812 | (backward-sexp 1) | |
813 | (skip-syntax-forward "'") | |
814 | (point)) | |
815 | (scan-error pos))) | |
4c14013d JB |
816 | (end |
817 | (unless (or (eq beg (point-max)) | |
818 | (member (char-syntax (char-after beg)) '(?\" ?\( ?\)))) | |
819 | (condition-case nil | |
820 | (save-excursion | |
821 | (goto-char beg) | |
822 | (forward-sexp 1) | |
823 | (when (>= (point) pos) | |
824 | (point))) | |
dd8791e9 SM |
825 | (scan-error pos)))) |
826 | (funpos (eq (char-before beg) ?\()) ;t if in function position. | |
827 | (table-etc | |
828 | (if (not funpos) | |
829 | ;; FIXME: We could look at the first element of the list and | |
830 | ;; use it to provide a more specific completion table in some | |
831 | ;; cases. E.g. filter out keywords that are not understood by | |
832 | ;; the macro/function being called. | |
bbcc4d97 SM |
833 | (list nil (completion-table-in-turn |
834 | lisp--local-variables-completion-table | |
835 | obarray) ;Could be anything. | |
dd8791e9 | 836 | :annotation-function |
2da4c3ab SM |
837 | (lambda (str) (if (fboundp (intern-soft str)) " <f>")) |
838 | :company-doc-buffer #'lisp--company-doc-buffer | |
839 | :company-docsig #'lisp--company-doc-string | |
840 | :company-location #'lisp--company-location) | |
dd8791e9 SM |
841 | ;; Looks like a funcall position. Let's double check. |
842 | (save-excursion | |
843 | (goto-char (1- beg)) | |
844 | (let ((parent | |
845 | (condition-case nil | |
846 | (progn (up-list -1) (forward-char 1) | |
847 | (let ((c (char-after))) | |
848 | (if (eq c ?\() ?\( | |
849 | (if (memq (char-syntax c) '(?w ?_)) | |
850 | (read (current-buffer)))))) | |
851 | (error nil)))) | |
852 | (pcase parent | |
853 | ;; FIXME: Rather than hardcode special cases here, | |
854 | ;; we should use something like a symbol-property. | |
855 | (`declare | |
856 | (list t (mapcar (lambda (x) (symbol-name (car x))) | |
2da4c3ab SM |
857 | (delete-dups |
858 | ;; FIXME: We should include some | |
859 | ;; docstring with each entry. | |
860 | (append | |
861 | macro-declarations-alist | |
862 | defun-declarations-alist))))) | |
c9023370 SM |
863 | ((and (or `condition-case `condition-case-unless-debug) |
864 | (guard (save-excursion | |
865 | (ignore-errors | |
866 | (forward-sexp 2) | |
867 | (< (point) beg))))) | |
dd8791e9 SM |
868 | (list t obarray |
869 | :predicate (lambda (sym) (get sym 'error-conditions)))) | |
e333fb10 SM |
870 | ((and ?\( |
871 | (guard (save-excursion | |
872 | (goto-char (1- beg)) | |
873 | (up-list -1) | |
874 | (forward-symbol -1) | |
875 | (looking-at "\\_<let\\*?\\_>")))) | |
876 | (list t obarray | |
877 | :predicate #'boundp | |
878 | :company-doc-buffer #'lisp--company-doc-buffer | |
879 | :company-docsig #'lisp--company-doc-string | |
880 | :company-location #'lisp--company-location)) | |
2da4c3ab SM |
881 | (_ (list nil obarray |
882 | :predicate #'fboundp | |
883 | :company-doc-buffer #'lisp--company-doc-buffer | |
884 | :company-docsig #'lisp--company-doc-string | |
885 | :company-location #'lisp--company-location | |
886 | )))))))) | |
4c14013d | 887 | (when end |
dd8791e9 SM |
888 | (let ((tail (if (null (car table-etc)) |
889 | (cdr table-etc) | |
890 | (cons | |
df76dacb | 891 | (if (memq (char-syntax (or (char-after end) ?\s)) |
dd8791e9 SM |
892 | '(?\s ?>)) |
893 | (cadr table-etc) | |
894 | (apply-partially 'completion-table-with-terminator | |
895 | " " (cadr table-etc))) | |
896 | (cddr table-etc))))) | |
897 | `(,beg ,end ,@tail)))))) | |
6594deb0 ER |
898 | |
899 | ;;; lisp.el ends here |