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