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