]> code.delx.au - gnu-emacs/blob - lisp/progmodes/elisp-mode.el
Unify the absolutely equal xref-backend-references implementations
[gnu-emacs] / lisp / progmodes / elisp-mode.el
1 ;;; elisp-mode.el --- Emacs Lisp mode -*- lexical-binding:t -*-
2
3 ;; Copyright (C) 1985-1986, 1999-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 ;; The major mode for editing Emacs Lisp code.
27 ;; This mode is documented in the Emacs manual.
28
29 ;;; Code:
30
31 (require 'cl-generic)
32 (require 'lisp-mode)
33 (eval-when-compile (require 'cl-lib))
34
35 (define-abbrev-table 'emacs-lisp-mode-abbrev-table ()
36 "Abbrev table for Emacs Lisp mode.
37 It has `lisp-mode-abbrev-table' as its parent."
38 :parents (list lisp-mode-abbrev-table))
39
40 (defvar emacs-lisp-mode-syntax-table
41 (let ((table (make-syntax-table lisp--mode-syntax-table)))
42 (modify-syntax-entry ?\[ "(] " table)
43 (modify-syntax-entry ?\] ")[ " table)
44 table)
45 "Syntax table used in `emacs-lisp-mode'.")
46
47 (defvar emacs-lisp-mode-map
48 (let ((map (make-sparse-keymap "Emacs-Lisp"))
49 (menu-map (make-sparse-keymap "Emacs-Lisp"))
50 (lint-map (make-sparse-keymap))
51 (prof-map (make-sparse-keymap))
52 (tracing-map (make-sparse-keymap)))
53 (set-keymap-parent map lisp-mode-shared-map)
54 (define-key map "\e\t" 'completion-at-point)
55 (define-key map "\e\C-x" 'eval-defun)
56 (define-key map "\e\C-q" 'indent-pp-sexp)
57 (bindings--define-key map [menu-bar emacs-lisp]
58 (cons "Emacs-Lisp" menu-map))
59 (bindings--define-key menu-map [eldoc]
60 '(menu-item "Auto-Display Documentation Strings" eldoc-mode
61 :button (:toggle . (bound-and-true-p eldoc-mode))
62 :help "Display the documentation string for the item under cursor"))
63 (bindings--define-key menu-map [checkdoc]
64 '(menu-item "Check Documentation Strings" checkdoc
65 :help "Check documentation strings for style requirements"))
66 (bindings--define-key menu-map [re-builder]
67 '(menu-item "Construct Regexp" re-builder
68 :help "Construct a regexp interactively"))
69 (bindings--define-key menu-map [tracing] (cons "Tracing" tracing-map))
70 (bindings--define-key tracing-map [tr-a]
71 '(menu-item "Untrace All" untrace-all
72 :help "Untrace all currently traced functions"))
73 (bindings--define-key tracing-map [tr-uf]
74 '(menu-item "Untrace Function..." untrace-function
75 :help "Untrace function, and possibly activate all remaining advice"))
76 (bindings--define-key tracing-map [tr-sep] menu-bar-separator)
77 (bindings--define-key tracing-map [tr-q]
78 '(menu-item "Trace Function Quietly..." trace-function-background
79 :help "Trace the function with trace output going quietly to a buffer"))
80 (bindings--define-key tracing-map [tr-f]
81 '(menu-item "Trace Function..." trace-function
82 :help "Trace the function given as an argument"))
83 (bindings--define-key menu-map [profiling] (cons "Profiling" prof-map))
84 (bindings--define-key prof-map [prof-restall]
85 '(menu-item "Remove Instrumentation for All Functions" elp-restore-all
86 :help "Restore the original definitions of all functions being profiled"))
87 (bindings--define-key prof-map [prof-restfunc]
88 '(menu-item "Remove Instrumentation for Function..." elp-restore-function
89 :help "Restore an instrumented function to its original definition"))
90
91 (bindings--define-key prof-map [sep-rem] menu-bar-separator)
92 (bindings--define-key prof-map [prof-resall]
93 '(menu-item "Reset Counters for All Functions" elp-reset-all
94 :help "Reset the profiling information for all functions being profiled"))
95 (bindings--define-key prof-map [prof-resfunc]
96 '(menu-item "Reset Counters for Function..." elp-reset-function
97 :help "Reset the profiling information for a function"))
98 (bindings--define-key prof-map [prof-res]
99 '(menu-item "Show Profiling Results" elp-results
100 :help "Display current profiling results"))
101 (bindings--define-key prof-map [prof-pack]
102 '(menu-item "Instrument Package..." elp-instrument-package
103 :help "Instrument for profiling all function that start with a prefix"))
104 (bindings--define-key prof-map [prof-func]
105 '(menu-item "Instrument Function..." elp-instrument-function
106 :help "Instrument a function for profiling"))
107 ;; Maybe this should be in a separate submenu from the ELP stuff?
108 (bindings--define-key prof-map [sep-natprof] menu-bar-separator)
109 (bindings--define-key prof-map [prof-natprof-stop]
110 '(menu-item "Stop Native Profiler" profiler-stop
111 :help "Stop recording profiling information"
112 :enable (and (featurep 'profiler)
113 (profiler-running-p))))
114 (bindings--define-key prof-map [prof-natprof-report]
115 '(menu-item "Show Profiler Report" profiler-report
116 :help "Show the current profiler report"
117 :enable (and (featurep 'profiler)
118 (profiler-running-p))))
119 (bindings--define-key prof-map [prof-natprof-start]
120 '(menu-item "Start Native Profiler..." profiler-start
121 :help "Start recording profiling information"))
122
123 (bindings--define-key menu-map [lint] (cons "Linting" lint-map))
124 (bindings--define-key lint-map [lint-di]
125 '(menu-item "Lint Directory..." elint-directory
126 :help "Lint a directory"))
127 (bindings--define-key lint-map [lint-f]
128 '(menu-item "Lint File..." elint-file
129 :help "Lint a file"))
130 (bindings--define-key lint-map [lint-b]
131 '(menu-item "Lint Buffer" elint-current-buffer
132 :help "Lint the current buffer"))
133 (bindings--define-key lint-map [lint-d]
134 '(menu-item "Lint Defun" elint-defun
135 :help "Lint the function at point"))
136 (bindings--define-key menu-map [edebug-defun]
137 '(menu-item "Instrument Function for Debugging" edebug-defun
138 :help "Evaluate the top level form point is in, stepping through with Edebug"
139 :keys "C-u C-M-x"))
140 (bindings--define-key menu-map [separator-byte] menu-bar-separator)
141 (bindings--define-key menu-map [disas]
142 '(menu-item "Disassemble Byte Compiled Object..." disassemble
143 :help "Print disassembled code for OBJECT in a buffer"))
144 (bindings--define-key menu-map [byte-recompile]
145 '(menu-item "Byte-recompile Directory..." byte-recompile-directory
146 :help "Recompile every `.el' file in DIRECTORY that needs recompilation"))
147 (bindings--define-key menu-map [emacs-byte-compile-and-load]
148 '(menu-item "Byte-compile and Load" emacs-lisp-byte-compile-and-load
149 :help "Byte-compile the current file (if it has changed), then load compiled code"))
150 (bindings--define-key menu-map [byte-compile]
151 '(menu-item "Byte-compile This File" emacs-lisp-byte-compile
152 :help "Byte compile the file containing the current buffer"))
153 (bindings--define-key menu-map [separator-eval] menu-bar-separator)
154 (bindings--define-key menu-map [ielm]
155 '(menu-item "Interactive Expression Evaluation" ielm
156 :help "Interactively evaluate Emacs Lisp expressions"))
157 (bindings--define-key menu-map [eval-buffer]
158 '(menu-item "Evaluate Buffer" eval-buffer
159 :help "Execute the current buffer as Lisp code"))
160 (bindings--define-key menu-map [eval-region]
161 '(menu-item "Evaluate Region" eval-region
162 :help "Execute the region as Lisp code"
163 :enable mark-active))
164 (bindings--define-key menu-map [eval-sexp]
165 '(menu-item "Evaluate Last S-expression" eval-last-sexp
166 :help "Evaluate sexp before point; print value in echo area"))
167 (bindings--define-key menu-map [separator-format] menu-bar-separator)
168 (bindings--define-key menu-map [comment-region]
169 '(menu-item "Comment Out Region" comment-region
170 :help "Comment or uncomment each line in the region"
171 :enable mark-active))
172 (bindings--define-key menu-map [indent-region]
173 '(menu-item "Indent Region" indent-region
174 :help "Indent each nonblank line in the region"
175 :enable mark-active))
176 (bindings--define-key menu-map [indent-line]
177 '(menu-item "Indent Line" lisp-indent-line))
178 map)
179 "Keymap for Emacs Lisp mode.
180 All commands in `lisp-mode-shared-map' are inherited by this map.")
181
182 (defun emacs-lisp-byte-compile ()
183 "Byte compile the file containing the current buffer."
184 (interactive)
185 (if buffer-file-name
186 (byte-compile-file buffer-file-name)
187 (error "The buffer must be saved in a file first")))
188
189 (defun emacs-lisp-byte-compile-and-load ()
190 "Byte-compile the current file (if it has changed), then load compiled code."
191 (interactive)
192 (or buffer-file-name
193 (error "The buffer must be saved in a file first"))
194 (require 'bytecomp)
195 ;; Recompile if file or buffer has changed since last compilation.
196 (if (and (buffer-modified-p)
197 (y-or-n-p (format "Save buffer %s first? " (buffer-name))))
198 (save-buffer))
199 (byte-recompile-file buffer-file-name nil 0 t))
200
201 (defun emacs-lisp-macroexpand ()
202 "Macroexpand the form after point.
203 Comments in the form will be lost."
204 (interactive)
205 (let* ((start (point))
206 (exp (read (current-buffer)))
207 ;; Compute it before, since it may signal errors.
208 (new (macroexpand-1 exp)))
209 (if (equal exp new)
210 (message "Not a macro call, nothing to expand")
211 (delete-region start (point))
212 (pp new (current-buffer))
213 (if (bolp) (delete-char -1))
214 (indent-region start (point)))))
215
216 (defcustom emacs-lisp-mode-hook nil
217 "Hook run when entering Emacs Lisp mode."
218 :options '(eldoc-mode imenu-add-menubar-index checkdoc-minor-mode)
219 :type 'hook
220 :group 'lisp)
221
222 ;;;###autoload
223 (define-derived-mode emacs-lisp-mode prog-mode "Emacs-Lisp"
224 "Major mode for editing Lisp code to run in Emacs.
225 Commands:
226 Delete converts tabs to spaces as it moves back.
227 Blank lines separate paragraphs. Semicolons start comments.
228
229 \\{emacs-lisp-mode-map}"
230 :group 'lisp
231 (defvar xref-backend-functions)
232 (defvar project-library-roots-function)
233 (lisp-mode-variables nil nil 'elisp)
234 (add-hook 'after-load-functions #'elisp--font-lock-flush-elisp-buffers)
235 (setq-local electric-pair-text-pairs
236 (append '((?\` . ?\') (?‘ . ?’)) electric-pair-text-pairs))
237 (setq-local electric-quote-string t)
238 (setq imenu-case-fold-search nil)
239 (add-function :before-until (local 'eldoc-documentation-function)
240 #'elisp-eldoc-documentation-function)
241 (add-hook 'xref-backend-functions #'elisp--xref-backend nil t)
242 (setq-local project-library-roots-function #'elisp-library-roots)
243 (add-hook 'completion-at-point-functions
244 #'elisp-completion-at-point nil 'local))
245
246 ;; Font-locking support.
247
248 (defun elisp--font-lock-flush-elisp-buffers (&optional file)
249 ;; FIXME: Aren't we only ever called from after-load-functions?
250 ;; Don't flush during load unless called from after-load-functions.
251 ;; In that case, FILE is non-nil. It's somehow strange that
252 ;; load-in-progress is t when an after-load-function is called since
253 ;; that should run *after* the load...
254 (when (or (not load-in-progress) file)
255 ;; FIXME: If the loaded file did not define any macros, there shouldn't
256 ;; be any need to font-lock-flush all the Elisp buffers.
257 (dolist (buf (buffer-list))
258 (with-current-buffer buf
259 (when (derived-mode-p 'emacs-lisp-mode)
260 ;; So as to take into account new macros that may have been defined
261 ;; by the just-loaded file.
262 (font-lock-flush))))))
263
264 ;;; Completion at point for Elisp
265
266 (defun elisp--local-variables-1 (vars sexp)
267 "Return the vars locally bound around the witness, or nil if not found."
268 (let (res)
269 (while
270 (unless
271 (setq res
272 (pcase sexp
273 (`(,(or `let `let*) ,bindings)
274 (let ((vars vars))
275 (when (eq 'let* (car sexp))
276 (dolist (binding (cdr (reverse bindings)))
277 (push (or (car-safe binding) binding) vars)))
278 (elisp--local-variables-1
279 vars (car (cdr-safe (car (last bindings)))))))
280 (`(,(or `let `let*) ,bindings . ,body)
281 (let ((vars vars))
282 (dolist (binding bindings)
283 (push (or (car-safe binding) binding) vars))
284 (elisp--local-variables-1 vars (car (last body)))))
285 (`(lambda ,_args)
286 ;; FIXME: Look for the witness inside `args'.
287 (setq sexp nil))
288 (`(lambda ,args . ,body)
289 (elisp--local-variables-1
290 (append (remq '&optional (remq '&rest args)) vars)
291 (car (last body))))
292 (`(condition-case ,_ ,e) (elisp--local-variables-1 vars e))
293 (`(condition-case ,v ,_ . ,catches)
294 (elisp--local-variables-1
295 (cons v vars) (cdr (car (last catches)))))
296 (`(quote . ,_)
297 ;; FIXME: Look for the witness inside sexp.
298 (setq sexp nil))
299 ;; FIXME: Handle `cond'.
300 (`(,_ . ,_)
301 (elisp--local-variables-1 vars (car (last sexp))))
302 (`elisp--witness--lisp (or vars '(nil)))
303 (_ nil)))
304 ;; We didn't find the witness in the last element so we try to
305 ;; backtrack to the last-but-one.
306 (setq sexp (ignore-errors (butlast sexp)))))
307 res))
308
309 (defun elisp--local-variables ()
310 "Return a list of locally let-bound variables at point."
311 (save-excursion
312 (skip-syntax-backward "w_")
313 (let* ((ppss (syntax-ppss))
314 (txt (buffer-substring-no-properties (or (car (nth 9 ppss)) (point))
315 (or (nth 8 ppss) (point))))
316 (closer ()))
317 (dolist (p (nth 9 ppss))
318 (push (cdr (syntax-after p)) closer))
319 (setq closer (apply #'string closer))
320 (let* ((sexp (condition-case nil
321 (car (read-from-string
322 (concat txt "elisp--witness--lisp" closer)))
323 ((invalid-read-syntax end-of-file) nil)))
324 (macroexpand-advice (lambda (expander form &rest args)
325 (condition-case nil
326 (apply expander form args)
327 (error form))))
328 (sexp
329 (unwind-protect
330 (progn
331 (advice-add 'macroexpand :around macroexpand-advice)
332 (macroexpand-all sexp))
333 (advice-remove 'macroexpand macroexpand-advice)))
334 (vars (elisp--local-variables-1 nil sexp)))
335 (delq nil
336 (mapcar (lambda (var)
337 (and (symbolp var)
338 (not (string-match (symbol-name var) "\\`[&_]"))
339 ;; Eliminate uninterned vars.
340 (intern-soft var)
341 var))
342 vars))))))
343
344 (defvar elisp--local-variables-completion-table
345 ;; Use `defvar' rather than `defconst' since defconst would purecopy this
346 ;; value, which would doubly fail: it would fail because purecopy can't
347 ;; handle the recursive bytecode object, and it would fail because it would
348 ;; move `lastpos' and `lastvars' to pure space where they'd be immutable!
349 (let ((lastpos nil) (lastvars nil))
350 (letrec ((hookfun (lambda ()
351 (setq lastpos nil)
352 (remove-hook 'post-command-hook hookfun))))
353 (completion-table-dynamic
354 (lambda (_string)
355 (save-excursion
356 (skip-syntax-backward "_w")
357 (let ((newpos (cons (point) (current-buffer))))
358 (unless (equal lastpos newpos)
359 (add-hook 'post-command-hook hookfun)
360 (setq lastpos newpos)
361 (setq lastvars
362 (mapcar #'symbol-name (elisp--local-variables))))))
363 lastvars)))))
364
365 (defun elisp--expect-function-p (pos)
366 "Return non-nil if the symbol at point is expected to be a function."
367 (or
368 (and (eq (char-before pos) ?')
369 (eq (char-before (1- pos)) ?#))
370 (save-excursion
371 (let ((parent (nth 1 (syntax-ppss pos))))
372 (when parent
373 (goto-char parent)
374 (and
375 (looking-at (concat "(\\(cl-\\)?"
376 (regexp-opt '("declare-function"
377 "function" "defadvice"
378 "callf" "callf2"
379 "defsetf"))
380 "[ \t\r\n]+"))
381 (eq (match-end 0) pos)))))))
382
383 (defun elisp--form-quoted-p (pos)
384 "Return non-nil if the form at POS is not evaluated.
385 It can be quoted, or be inside a quoted form."
386 ;; FIXME: Do some macro expansion maybe.
387 (save-excursion
388 (let ((state (syntax-ppss pos)))
389 (or (nth 8 state) ; Code inside strings usually isn't evaluated.
390 ;; FIXME: The 9th element is undocumented.
391 (let ((nesting (cons (point) (reverse (nth 9 state))))
392 res)
393 (while (and nesting (not res))
394 (goto-char (pop nesting))
395 (cond
396 ((or (eq (char-after) ?\[)
397 (progn
398 (skip-chars-backward " ")
399 (memq (char-before) '(?' ?` ?‘))))
400 (setq res t))
401 ((eq (char-before) ?,)
402 (setq nesting nil))))
403 res)))))
404
405 ;; FIXME: Support for Company brings in features which straddle eldoc.
406 ;; We should consolidate this, so that major modes can provide all that
407 ;; data all at once:
408 ;; - a function to extract "the reference at point" (may be more complex
409 ;; than a mere string, to distinguish various namespaces).
410 ;; - a function to jump to such a reference.
411 ;; - a function to show the signature/interface of such a reference.
412 ;; - a function to build a help-buffer about that reference.
413 ;; FIXME: Those functions should also be used by the normal completion code in
414 ;; the *Completions* buffer.
415
416 (defun elisp--company-doc-buffer (str)
417 (let ((symbol (intern-soft str)))
418 ;; FIXME: we really don't want to "display-buffer and then undo it".
419 (save-window-excursion
420 ;; Make sure we don't display it in another frame, otherwise
421 ;; save-window-excursion won't be able to undo it.
422 (let ((display-buffer-overriding-action
423 '(nil . ((inhibit-switch-frame . t)))))
424 (ignore-errors
425 (cond
426 ((fboundp symbol) (describe-function symbol))
427 ((boundp symbol) (describe-variable symbol))
428 ((featurep symbol) (describe-package symbol))
429 ((facep symbol) (describe-face symbol))
430 (t (signal 'user-error nil)))
431 (help-buffer))))))
432
433 (defun elisp--company-doc-string (str)
434 (let* ((symbol (intern-soft str))
435 (doc (if (fboundp symbol)
436 (documentation symbol t)
437 (documentation-property symbol 'variable-documentation t))))
438 (and (stringp doc)
439 (string-match ".*$" doc)
440 (match-string 0 doc))))
441
442 ;; can't (require 'find-func) in a preloaded file
443 (declare-function find-library-name "find-func" (library))
444 (declare-function find-function-library "find-func" (function &optional l-o v))
445
446 (defun elisp--company-location (str)
447 (let ((sym (intern-soft str)))
448 (cond
449 ((fboundp sym) (find-definition-noselect sym nil))
450 ((boundp sym) (find-definition-noselect sym 'defvar))
451 ((featurep sym)
452 (require 'find-func)
453 (cons (find-file-noselect (find-library-name
454 (symbol-name sym)))
455 0))
456 ((facep sym) (find-definition-noselect sym 'defface)))))
457
458 (defun elisp-completion-at-point ()
459 "Function used for `completion-at-point-functions' in `emacs-lisp-mode'."
460 (with-syntax-table emacs-lisp-mode-syntax-table
461 (let* ((pos (point))
462 (beg (condition-case nil
463 (save-excursion
464 (backward-sexp 1)
465 (skip-chars-forward "`',‘#")
466 (point))
467 (scan-error pos)))
468 (end
469 (unless (or (eq beg (point-max))
470 (member (char-syntax (char-after beg))
471 '(?\s ?\" ?\( ?\))))
472 (condition-case nil
473 (save-excursion
474 (goto-char beg)
475 (forward-sexp 1)
476 (skip-chars-backward "'’")
477 (when (>= (point) pos)
478 (point)))
479 (scan-error pos))))
480 ;; t if in function position.
481 (funpos (eq (char-before beg) ?\())
482 (quoted (elisp--form-quoted-p beg)))
483 (when (and end (or (not (nth 8 (syntax-ppss)))
484 (memq (char-before beg) '(?` ?‘))))
485 (let ((table-etc
486 (if (or (not funpos) quoted)
487 ;; FIXME: We could look at the first element of the list and
488 ;; use it to provide a more specific completion table in some
489 ;; cases. E.g. filter out keywords that are not understood by
490 ;; the macro/function being called.
491 (cond
492 ((elisp--expect-function-p beg)
493 (list nil obarray
494 :predicate #'fboundp
495 :company-doc-buffer #'elisp--company-doc-buffer
496 :company-docsig #'elisp--company-doc-string
497 :company-location #'elisp--company-location))
498 (quoted
499 (list nil obarray
500 ;; Don't include all symbols (bug#16646).
501 :predicate (lambda (sym)
502 (or (boundp sym)
503 (fboundp sym)
504 (featurep sym)
505 (symbol-plist sym)))
506 :annotation-function
507 (lambda (str) (if (fboundp (intern-soft str)) " <f>"))
508 :company-doc-buffer #'elisp--company-doc-buffer
509 :company-docsig #'elisp--company-doc-string
510 :company-location #'elisp--company-location))
511 (t
512 (list nil (completion-table-merge
513 elisp--local-variables-completion-table
514 (apply-partially #'completion-table-with-predicate
515 obarray
516 #'boundp
517 'strict))
518 :company-doc-buffer #'elisp--company-doc-buffer
519 :company-docsig #'elisp--company-doc-string
520 :company-location #'elisp--company-location)))
521 ;; Looks like a funcall position. Let's double check.
522 (save-excursion
523 (goto-char (1- beg))
524 (let ((parent
525 (condition-case nil
526 (progn (up-list -1) (forward-char 1)
527 (let ((c (char-after)))
528 (if (eq c ?\() ?\(
529 (if (memq (char-syntax c) '(?w ?_))
530 (read (current-buffer))))))
531 (error nil))))
532 (pcase parent
533 ;; FIXME: Rather than hardcode special cases here,
534 ;; we should use something like a symbol-property.
535 (`declare
536 (list t (mapcar (lambda (x) (symbol-name (car x)))
537 (delete-dups
538 ;; FIXME: We should include some
539 ;; docstring with each entry.
540 (append
541 macro-declarations-alist
542 defun-declarations-alist)))))
543 ((and (or `condition-case `condition-case-unless-debug)
544 (guard (save-excursion
545 (ignore-errors
546 (forward-sexp 2)
547 (< (point) beg)))))
548 (list t obarray
549 :predicate (lambda (sym) (get sym 'error-conditions))))
550 ((and (or ?\( `let `let*)
551 (guard (save-excursion
552 (goto-char (1- beg))
553 (when (eq parent ?\()
554 (up-list -1))
555 (forward-symbol -1)
556 (looking-at "\\_<let\\*?\\_>"))))
557 (list t obarray
558 :predicate #'boundp
559 :company-doc-buffer #'elisp--company-doc-buffer
560 :company-docsig #'elisp--company-doc-string
561 :company-location #'elisp--company-location))
562 (_ (list nil obarray
563 :predicate #'fboundp
564 :company-doc-buffer #'elisp--company-doc-buffer
565 :company-docsig #'elisp--company-doc-string
566 :company-location #'elisp--company-location
567 ))))))))
568 (nconc (list beg end)
569 (if (null (car table-etc))
570 (cdr table-etc)
571 (cons
572 (if (memq (char-syntax (or (char-after end) ?\s))
573 '(?\s ?>))
574 (cadr table-etc)
575 (apply-partially 'completion-table-with-terminator
576 " " (cadr table-etc)))
577 (cddr table-etc)))))))))
578
579 (define-obsolete-function-alias
580 'lisp-completion-at-point 'elisp-completion-at-point "25.1")
581
582 ;;; Xref backend
583
584 (declare-function xref-make-bogus-location "xref" (message))
585 (declare-function xref-make "xref" (summary location))
586 (declare-function xref-collect-references "xref" (symbol dir))
587
588 (defun elisp--xref-backend () 'elisp)
589
590 ;; WORKAROUND: This is nominally a constant, but the text properties
591 ;; are not preserved thru dump if use defconst. See bug#21237.
592 (defvar elisp--xref-format
593 (let ((str "(%s %s)"))
594 (put-text-property 1 3 'face 'font-lock-keyword-face str)
595 (put-text-property 4 6 'face 'font-lock-function-name-face str)
596 str))
597
598 ;; WORKAROUND: This is nominally a constant, but the text properties
599 ;; are not preserved thru dump if use defconst. See bug#21237.
600 (defvar elisp--xref-format-extra
601 (let ((str "(%s %s %s)"))
602 (put-text-property 1 3 'face 'font-lock-keyword-face str)
603 (put-text-property 4 6 'face 'font-lock-function-name-face str)
604 str))
605
606 (defvar find-feature-regexp);; in find-func.el
607
608 (defun elisp--xref-make-xref (type symbol file &optional summary)
609 "Return an xref for TYPE SYMBOL in FILE.
610 TYPE must be a type in `find-function-regexp-alist' (use nil for
611 'defun). If SUMMARY is non-nil, use it for the summary;
612 otherwise build the summary from TYPE and SYMBOL."
613 (xref-make (or summary
614 (format elisp--xref-format (or type 'defun) symbol))
615 (xref-make-elisp-location symbol type file)))
616
617 (defvar elisp-xref-find-def-functions nil
618 "List of functions to be run from `elisp--xref-find-definitions' to add additional xrefs.
619 Called with one arg; the symbol whose definition is desired.
620 Each function should return a list of xrefs, or nil; the first
621 non-nil result supercedes the xrefs produced by
622 `elisp--xref-find-definitions'.")
623
624 (cl-defmethod xref-backend-definitions ((_backend (eql elisp)) identifier)
625 (require 'find-func)
626 ;; FIXME: use information in source near point to filter results:
627 ;; (dvc-log-edit ...) - exclude 'feature
628 ;; (require 'dvc-log-edit) - only 'feature
629 ;; Semantic may provide additional information
630 ;;
631 (let ((sym (intern-soft identifier)))
632 (when sym
633 (elisp--xref-find-definitions sym))))
634
635 (defun elisp--xref-find-definitions (symbol)
636 ;; The file name is not known when `symbol' is defined via interactive eval.
637 (let (xrefs)
638
639 (let ((temp elisp-xref-find-def-functions))
640 (while (and (null xrefs)
641 temp)
642 (setq xrefs (append xrefs (funcall (pop temp) symbol)))))
643
644 (unless xrefs
645 ;; alphabetical by result type symbol
646
647 ;; FIXME: advised function; list of advice functions
648
649 ;; Coding system symbols do not appear in ‘load-history’,
650 ;; so we can’t get a location for them.
651
652 (when (and (symbolp symbol)
653 (symbol-function symbol)
654 (symbolp (symbol-function symbol)))
655 ;; aliased function
656 (let* ((alias-symbol symbol)
657 (alias-file (symbol-file alias-symbol))
658 (real-symbol (symbol-function symbol))
659 (real-file (find-lisp-object-file-name real-symbol 'defun)))
660
661 (when real-file
662 (push (elisp--xref-make-xref nil real-symbol real-file) xrefs))
663
664 (when alias-file
665 (push (elisp--xref-make-xref 'defalias alias-symbol alias-file) xrefs))))
666
667 (when (facep symbol)
668 (let ((file (find-lisp-object-file-name symbol 'defface)))
669 (when file
670 (push (elisp--xref-make-xref 'defface symbol file) xrefs))))
671
672 (when (fboundp symbol)
673 (let ((file (find-lisp-object-file-name symbol (symbol-function symbol)))
674 generic doc)
675 (when file
676 (cond
677 ((eq file 'C-source)
678 ;; First call to find-lisp-object-file-name for an object
679 ;; defined in C; the doc strings from the C source have
680 ;; not been loaded yet. Second call will return "src/*.c"
681 ;; in file; handled by 't' case below.
682 (push (elisp--xref-make-xref nil symbol (help-C-file-name (symbol-function symbol) 'subr)) xrefs))
683
684 ((and (setq doc (documentation symbol t))
685 ;; This doc string is defined in cl-macs.el cl-defstruct
686 (string-match "Constructor for objects of type `\\(.*\\)'" doc))
687 ;; `symbol' is a name for the default constructor created by
688 ;; cl-defstruct, so return the location of the cl-defstruct.
689 (let* ((type-name (match-string 1 doc))
690 (type-symbol (intern type-name))
691 (file (find-lisp-object-file-name type-symbol 'define-type))
692 (summary (format elisp--xref-format-extra
693 'cl-defstruct
694 (concat "(" type-name)
695 (concat "(:constructor " (symbol-name symbol) "))"))))
696 (push (elisp--xref-make-xref 'define-type type-symbol file summary) xrefs)
697 ))
698
699 ((setq generic (cl--generic symbol))
700 ;; FIXME: move this to elisp-xref-find-def-functions, in cl-generic.el
701
702 ;; A generic function. If there is a default method, it
703 ;; will appear in the method table, with no
704 ;; specializers.
705 ;;
706 ;; If the default method is declared by the cl-defgeneric
707 ;; declaration, it will have the same location as the
708 ;; cl-defgeneric, so we want to exclude it from the
709 ;; result. In this case, it will have a null doc
710 ;; string. User declarations of default methods may also
711 ;; have null doc strings, but we hope that is
712 ;; rare. Perhaps this heuristic will discourage that.
713 (dolist (method (cl--generic-method-table generic))
714 (let* ((info (cl--generic-method-info method));; qual-string combined-args doconly
715 (specializers (cl--generic-method-specializers method))
716 (non-default nil)
717 (met-name (cons symbol specializers))
718 (file (find-lisp-object-file-name met-name 'cl-defmethod)))
719 (dolist (item specializers)
720 ;; default method has all 't' in specializers
721 (setq non-default (or non-default (not (equal t item)))))
722
723 (when (and file
724 (or non-default
725 (nth 2 info))) ;; assuming only co-located default has null doc string
726 (if specializers
727 (let ((summary (format elisp--xref-format-extra 'cl-defmethod symbol (nth 1 info))))
728 (push (elisp--xref-make-xref 'cl-defmethod met-name file summary) xrefs))
729
730 (let ((summary (format elisp--xref-format-extra 'cl-defmethod symbol "()")))
731 (push (elisp--xref-make-xref 'cl-defmethod met-name file summary) xrefs))))
732 ))
733
734 (if (and (setq doc (documentation symbol t))
735 ;; This doc string is created somewhere in
736 ;; cl--generic-make-function for an implicit
737 ;; defgeneric.
738 (string-match "\n\n(fn ARG &rest ARGS)" doc))
739 ;; This symbol is an implicitly defined defgeneric, so
740 ;; don't return it.
741 nil
742 (push (elisp--xref-make-xref 'cl-defgeneric symbol file) xrefs))
743 )
744
745 (t
746 (push (elisp--xref-make-xref nil symbol file) xrefs))
747 ))))
748
749 (when (boundp symbol)
750 ;; A variable
751 (let ((file (find-lisp-object-file-name symbol 'defvar)))
752 (when file
753 (cond
754 ((eq file 'C-source)
755 ;; The doc strings from the C source have not been loaded
756 ;; yet; help-C-file-name does that. Second call will
757 ;; return "src/*.c" in file; handled below.
758 (push (elisp--xref-make-xref 'defvar symbol (help-C-file-name symbol 'var)) xrefs))
759
760 ((string= "src/" (substring file 0 4))
761 ;; The variable is defined in a C source file; don't check
762 ;; for define-minor-mode.
763 (push (elisp--xref-make-xref 'defvar symbol file) xrefs))
764
765 ((memq symbol minor-mode-list)
766 ;; The symbol is a minor mode. These should be defined by
767 ;; "define-minor-mode", which means the variable and the
768 ;; function are declared in the same place. So we return only
769 ;; the function, arbitrarily.
770 ;;
771 ;; There is an exception, when the variable is defined in C
772 ;; code, as for abbrev-mode.
773 ;;
774 ;; IMPROVEME: If the user is searching for the identifier at
775 ;; point, we can determine whether it is a variable or
776 ;; function by looking at the source code near point.
777 ;;
778 ;; IMPROVEME: The user may actually be asking "do any
779 ;; variables by this name exist"; we need a way to specify
780 ;; that.
781 nil)
782
783 (t
784 (push (elisp--xref-make-xref 'defvar symbol file) xrefs))
785
786 ))))
787
788 (when (featurep symbol)
789 (let ((file (ignore-errors
790 (find-library-name (symbol-name symbol)))))
791 (when file
792 (push (elisp--xref-make-xref 'feature symbol file) xrefs))))
793 );; 'unless xrefs'
794
795 xrefs))
796
797 (declare-function project-library-roots "project")
798
799 (cl-defmethod xref-backend-apropos ((_backend (eql elisp)) regexp)
800 (apply #'nconc
801 (let (lst)
802 (dolist (sym (apropos-internal regexp))
803 (push (elisp--xref-find-definitions sym) lst))
804 (nreverse lst))))
805
806 (defvar elisp--xref-identifier-completion-table
807 (apply-partially #'completion-table-with-predicate
808 obarray
809 (lambda (sym)
810 (or (boundp sym)
811 (fboundp sym)
812 (featurep sym)
813 (facep sym)))
814 'strict))
815
816 (cl-defmethod xref-backend-identifier-completion-table ((_backend (eql elisp)))
817 elisp--xref-identifier-completion-table)
818
819 (cl-defstruct (xref-elisp-location
820 (:constructor xref-make-elisp-location (symbol type file)))
821 "Location of an Emacs Lisp symbol definition."
822 symbol type file)
823
824 (cl-defmethod xref-location-marker ((l xref-elisp-location))
825 (pcase-let (((cl-struct xref-elisp-location symbol type file) l))
826 (let ((buffer-point (find-function-search-for-symbol symbol type file)))
827 (with-current-buffer (car buffer-point)
828 (goto-char (or (cdr buffer-point) (point-min)))
829 (point-marker)))))
830
831 (cl-defmethod xref-location-group ((l xref-elisp-location))
832 (xref-elisp-location-file l))
833
834 (defun elisp-library-roots ()
835 (defvar package-user-dir)
836 (cons package-user-dir load-path))
837
838 ;;; Elisp Interaction mode
839
840 (defvar lisp-interaction-mode-map
841 (let ((map (make-sparse-keymap))
842 (menu-map (make-sparse-keymap "Lisp-Interaction")))
843 (set-keymap-parent map lisp-mode-shared-map)
844 (define-key map "\e\C-x" 'eval-defun)
845 (define-key map "\e\C-q" 'indent-pp-sexp)
846 (define-key map "\e\t" 'completion-at-point)
847 (define-key map "\n" 'eval-print-last-sexp)
848 (bindings--define-key map [menu-bar lisp-interaction]
849 (cons "Lisp-Interaction" menu-map))
850 (bindings--define-key menu-map [eval-defun]
851 '(menu-item "Evaluate Defun" eval-defun
852 :help "Evaluate the top-level form containing point, or after point"))
853 (bindings--define-key menu-map [eval-print-last-sexp]
854 '(menu-item "Evaluate and Print" eval-print-last-sexp
855 :help "Evaluate sexp before point; print value into current buffer"))
856 (bindings--define-key menu-map [edebug-defun-lisp-interaction]
857 '(menu-item "Instrument Function for Debugging" edebug-defun
858 :help "Evaluate the top level form point is in, stepping through with Edebug"
859 :keys "C-u C-M-x"))
860 (bindings--define-key menu-map [indent-pp-sexp]
861 '(menu-item "Indent or Pretty-Print" indent-pp-sexp
862 :help "Indent each line of the list starting just after point, or prettyprint it"))
863 (bindings--define-key menu-map [complete-symbol]
864 '(menu-item "Complete Lisp Symbol" completion-at-point
865 :help "Perform completion on Lisp symbol preceding point"))
866 map)
867 "Keymap for Lisp Interaction mode.
868 All commands in `lisp-mode-shared-map' are inherited by this map.")
869
870 (define-derived-mode lisp-interaction-mode emacs-lisp-mode "Lisp Interaction"
871 "Major mode for typing and evaluating Lisp forms.
872 Like Lisp mode except that \\[eval-print-last-sexp] evals the Lisp expression
873 before point, and prints its value into the buffer, advancing point.
874 Note that printing is controlled by `eval-expression-print-length'
875 and `eval-expression-print-level'.
876
877 Commands:
878 Delete converts tabs to spaces as it moves back.
879 Paragraphs are separated only by blank lines.
880 Semicolons start comments.
881
882 \\{lisp-interaction-mode-map}"
883 :abbrev-table nil)
884
885 ;;; Emacs Lisp Byte-Code mode
886
887 (eval-and-compile
888 (defconst emacs-list-byte-code-comment-re
889 (concat "\\(#\\)@\\([0-9]+\\) "
890 ;; Make sure it's a docstring and not a lazy-loaded byte-code.
891 "\\(?:[^(]\\|([^\"]\\)")))
892
893 (defun elisp--byte-code-comment (end &optional _point)
894 "Try to syntactically mark the #@NNN ....^_ docstrings in byte-code files."
895 (let ((ppss (syntax-ppss)))
896 (when (and (nth 4 ppss)
897 (eq (char-after (nth 8 ppss)) ?#))
898 (let* ((n (save-excursion
899 (goto-char (nth 8 ppss))
900 (when (looking-at emacs-list-byte-code-comment-re)
901 (string-to-number (match-string 2)))))
902 ;; `maxdiff' tries to make sure the loop below terminates.
903 (maxdiff n))
904 (when n
905 (let* ((bchar (match-end 2))
906 (b (position-bytes bchar)))
907 (goto-char (+ b n))
908 (while (let ((diff (- (position-bytes (point)) b n)))
909 (unless (zerop diff)
910 (when (> diff maxdiff) (setq diff maxdiff))
911 (forward-char (- diff))
912 (setq maxdiff (if (> diff 0) diff
913 (max (1- maxdiff) 1)))
914 t))))
915 (if (<= (point) end)
916 (put-text-property (1- (point)) (point)
917 'syntax-table
918 (string-to-syntax "> b"))
919 (goto-char end)))))))
920
921 (defun elisp-byte-code-syntax-propertize (start end)
922 (goto-char start)
923 (elisp--byte-code-comment end (point))
924 (funcall
925 (syntax-propertize-rules
926 (emacs-list-byte-code-comment-re
927 (1 (prog1 "< b" (elisp--byte-code-comment end (point))))))
928 start end))
929
930 ;;;###autoload
931 (add-to-list 'auto-mode-alist '("\\.elc\\'" . elisp-byte-code-mode))
932 ;;;###autoload
933 (define-derived-mode elisp-byte-code-mode emacs-lisp-mode
934 "Elisp-Byte-Code"
935 "Major mode for *.elc files."
936 ;; TODO: Add way to disassemble byte-code under point.
937 (setq-local open-paren-in-column-0-is-defun-start nil)
938 (setq-local syntax-propertize-function
939 #'elisp-byte-code-syntax-propertize))
940
941
942 ;;; Globally accessible functionality
943
944 (defun eval-print-last-sexp (&optional eval-last-sexp-arg-internal)
945 "Evaluate sexp before point; print value into current buffer.
946
947 Normally, this function truncates long output according to the value
948 of the variables `eval-expression-print-length' and
949 `eval-expression-print-level'. With a prefix argument of zero,
950 however, there is no such truncation. Such a prefix argument
951 also causes integers to be printed in several additional formats
952 \(octal, hexadecimal, and character).
953
954 If `eval-expression-debug-on-error' is non-nil, which is the default,
955 this command arranges for all errors to enter the debugger."
956 (interactive "P")
957 (let ((standard-output (current-buffer)))
958 (terpri)
959 (eval-last-sexp (or eval-last-sexp-arg-internal t))
960 (terpri)))
961
962
963 (defun last-sexp-setup-props (beg end value alt1 alt2)
964 "Set up text properties for the output of `elisp--eval-last-sexp'.
965 BEG and END are the start and end of the output in current-buffer.
966 VALUE is the Lisp value printed, ALT1 and ALT2 are strings for the
967 alternative printed representations that can be displayed."
968 (let ((map (make-sparse-keymap)))
969 (define-key map "\C-m" 'elisp-last-sexp-toggle-display)
970 (define-key map [down-mouse-2] 'mouse-set-point)
971 (define-key map [mouse-2] 'elisp-last-sexp-toggle-display)
972 (add-text-properties
973 beg end
974 `(printed-value (,value ,alt1 ,alt2)
975 mouse-face highlight
976 keymap ,map
977 help-echo "RET, mouse-2: toggle abbreviated display"
978 rear-nonsticky (mouse-face keymap help-echo
979 printed-value)))))
980
981
982 (defun elisp-last-sexp-toggle-display (&optional _arg)
983 "Toggle between abbreviated and unabbreviated printed representations."
984 (interactive "P")
985 (save-restriction
986 (widen)
987 (let ((value (get-text-property (point) 'printed-value)))
988 (when value
989 (let ((beg (or (previous-single-property-change (min (point-max) (1+ (point)))
990 'printed-value)
991 (point)))
992 (end (or (next-single-char-property-change (point) 'printed-value) (point)))
993 (standard-output (current-buffer))
994 (point (point)))
995 (delete-region beg end)
996 (insert (nth 1 value))
997 (or (= beg point)
998 (setq point (1- (point))))
999 (last-sexp-setup-props beg (point)
1000 (nth 0 value)
1001 (nth 2 value)
1002 (nth 1 value))
1003 (goto-char (min (point-max) point)))))))
1004
1005 (defun prin1-char (char) ;FIXME: Move it, e.g. to simple.el.
1006 "Return a string representing CHAR as a character rather than as an integer.
1007 If CHAR is not a character, return nil."
1008 (and (integerp char)
1009 (eventp char)
1010 (let ((c (event-basic-type char))
1011 (mods (event-modifiers char))
1012 string)
1013 ;; Prevent ?A from turning into ?\S-a.
1014 (if (and (memq 'shift mods)
1015 (zerop (logand char ?\S-\^@))
1016 (not (let ((case-fold-search nil))
1017 (char-equal c (upcase c)))))
1018 (setq c (upcase c) mods nil))
1019 ;; What string are we considering using?
1020 (condition-case nil
1021 (setq string
1022 (concat
1023 "?"
1024 (mapconcat
1025 (lambda (modif)
1026 (cond ((eq modif 'super) "\\s-")
1027 (t (string ?\\ (upcase (aref (symbol-name modif) 0)) ?-))))
1028 mods "")
1029 (cond
1030 ((memq c '(?\; ?\( ?\) ?\{ ?\} ?\[ ?\] ?\" ?\' ?\\)) (string ?\\ c))
1031 ((eq c 127) "\\C-?")
1032 (t
1033 (string c)))))
1034 (error nil))
1035 ;; Verify the string reads a CHAR, not to some other character.
1036 ;; If it doesn't, return nil instead.
1037 (and string
1038 (= (car (read-from-string string)) char)
1039 string))))
1040
1041 (defun elisp--preceding-sexp ()
1042 "Return sexp before the point."
1043 (let ((opoint (point))
1044 (left-quote ?‘)
1045 expr)
1046 (save-excursion
1047 (with-syntax-table emacs-lisp-mode-syntax-table
1048 ;; If this sexp appears to be enclosed in `...' or ‘...’
1049 ;; then ignore the surrounding quotes.
1050 (cond ((eq (preceding-char) ?’)
1051 (progn (forward-char -1) (setq opoint (point))))
1052 ((or (eq (following-char) ?\')
1053 (eq (preceding-char) ?\'))
1054 (setq left-quote ?\`)))
1055 (forward-sexp -1)
1056 ;; If we were after `?\e' (or similar case),
1057 ;; use the whole thing, not just the `e'.
1058 (when (eq (preceding-char) ?\\)
1059 (forward-char -1)
1060 (when (eq (preceding-char) ??)
1061 (forward-char -1)))
1062
1063 ;; Skip over hash table read syntax.
1064 (and (> (point) (1+ (point-min)))
1065 (looking-back "#s" (- (point) 2))
1066 (forward-char -2))
1067
1068 ;; Skip over `#N='s.
1069 (when (eq (preceding-char) ?=)
1070 (let (labeled-p)
1071 (save-excursion
1072 (skip-chars-backward "0-9#=")
1073 (setq labeled-p (looking-at "\\(#[0-9]+=\\)+")))
1074 (when labeled-p
1075 (forward-sexp -1))))
1076
1077 (save-restriction
1078 (if (eq (following-char) left-quote)
1079 ;; vladimir@cs.ualberta.ca 30-Jul-1997: Skip ` in `variable' so
1080 ;; that the value is returned, not the name.
1081 (forward-char))
1082 (when (looking-at ",@?") (goto-char (match-end 0)))
1083 (narrow-to-region (point-min) opoint)
1084 (setq expr (read (current-buffer)))
1085 ;; If it's an (interactive ...) form, it's more useful to show how an
1086 ;; interactive call would use it.
1087 ;; FIXME: Is it really the right place for this?
1088 (when (eq (car-safe expr) 'interactive)
1089 (setq expr
1090 `(call-interactively
1091 (lambda (&rest args) ,expr args))))
1092 expr)))))
1093 (define-obsolete-function-alias 'preceding-sexp 'elisp--preceding-sexp "25.1")
1094
1095 (defun elisp--eval-last-sexp (eval-last-sexp-arg-internal)
1096 "Evaluate sexp before point; print value in the echo area.
1097 If EVAL-LAST-SEXP-ARG-INTERNAL is non-nil, print output into
1098 current buffer. If EVAL-LAST-SEXP-ARG-INTERNAL is `0', print
1099 output with no limit on the length and level of lists, and
1100 include additional formats for integers \(octal, hexadecimal, and
1101 character)."
1102 (let ((standard-output (if eval-last-sexp-arg-internal (current-buffer) t)))
1103 ;; Setup the lexical environment if lexical-binding is enabled.
1104 (elisp--eval-last-sexp-print-value
1105 (eval (eval-sexp-add-defvars (elisp--preceding-sexp)) lexical-binding)
1106 eval-last-sexp-arg-internal)))
1107
1108 (defun elisp--eval-last-sexp-print-value (value &optional eval-last-sexp-arg-internal)
1109 (let ((unabbreviated (let ((print-length nil) (print-level nil))
1110 (prin1-to-string value)))
1111 (print-length (and (not (zerop (prefix-numeric-value
1112 eval-last-sexp-arg-internal)))
1113 eval-expression-print-length))
1114 (print-level (and (not (zerop (prefix-numeric-value
1115 eval-last-sexp-arg-internal)))
1116 eval-expression-print-level))
1117 (beg (point))
1118 end)
1119 (prog1
1120 (prin1 value)
1121 (let ((str (eval-expression-print-format value)))
1122 (if str (princ str)))
1123 (setq end (point))
1124 (when (and (bufferp standard-output)
1125 (or (not (null print-length))
1126 (not (null print-level)))
1127 (not (string= unabbreviated
1128 (buffer-substring-no-properties beg end))))
1129 (last-sexp-setup-props beg end value
1130 unabbreviated
1131 (buffer-substring-no-properties beg end))
1132 ))))
1133
1134
1135 (defvar elisp--eval-last-sexp-fake-value (make-symbol "t"))
1136
1137 (defun eval-sexp-add-defvars (exp &optional pos)
1138 "Prepend EXP with all the `defvar's that precede it in the buffer.
1139 POS specifies the starting position where EXP was found and defaults to point."
1140 (setq exp (macroexpand-all exp)) ;Eager macro-expansion.
1141 (if (not lexical-binding)
1142 exp
1143 (save-excursion
1144 (unless pos (setq pos (point)))
1145 (let ((vars ()))
1146 (goto-char (point-min))
1147 (while (re-search-forward
1148 "(def\\(?:var\\|const\\|custom\\)[ \t\n]+\\([^; '()\n\t]+\\)"
1149 pos t)
1150 (let ((var (intern (match-string 1))))
1151 (and (not (special-variable-p var))
1152 (save-excursion
1153 (zerop (car (syntax-ppss (match-beginning 0)))))
1154 (push var vars))))
1155 `(progn ,@(mapcar (lambda (v) `(defvar ,v)) vars) ,exp)))))
1156
1157 (defun eval-last-sexp (eval-last-sexp-arg-internal)
1158 "Evaluate sexp before point; print value in the echo area.
1159 Interactively, with prefix argument, print output into current buffer.
1160
1161 Normally, this function truncates long output according to the value
1162 of the variables `eval-expression-print-length' and
1163 `eval-expression-print-level'. With a prefix argument of zero,
1164 however, there is no such truncation. Such a prefix argument
1165 also causes integers to be printed in several additional formats
1166 \(octal, hexadecimal, and character).
1167
1168 If `eval-expression-debug-on-error' is non-nil, which is the default,
1169 this command arranges for all errors to enter the debugger."
1170 (interactive "P")
1171 (if (null eval-expression-debug-on-error)
1172 (elisp--eval-last-sexp eval-last-sexp-arg-internal)
1173 (let ((value
1174 (let ((debug-on-error elisp--eval-last-sexp-fake-value))
1175 (cons (elisp--eval-last-sexp eval-last-sexp-arg-internal)
1176 debug-on-error))))
1177 (unless (eq (cdr value) elisp--eval-last-sexp-fake-value)
1178 (setq debug-on-error (cdr value)))
1179 (car value))))
1180
1181 (defun elisp--eval-defun-1 (form)
1182 "Treat some expressions specially.
1183 Reset the `defvar' and `defcustom' variables to the initial value.
1184 \(For `defcustom', use the :set function if there is one.)
1185 Reinitialize the face according to the `defface' specification."
1186 ;; The code in edebug-defun should be consistent with this, but not
1187 ;; the same, since this gets a macroexpanded form.
1188 (cond ((not (listp form))
1189 form)
1190 ((and (eq (car form) 'defvar)
1191 (cdr-safe (cdr-safe form))
1192 (boundp (cadr form)))
1193 ;; Force variable to be re-set.
1194 `(progn (defvar ,(nth 1 form) nil ,@(nthcdr 3 form))
1195 (setq-default ,(nth 1 form) ,(nth 2 form))))
1196 ;; `defcustom' is now macroexpanded to
1197 ;; `custom-declare-variable' with a quoted value arg.
1198 ((and (eq (car form) 'custom-declare-variable)
1199 (default-boundp (eval (nth 1 form) lexical-binding)))
1200 ;; Force variable to be bound, using :set function if specified.
1201 (let ((setfunc (memq :set form)))
1202 (when setfunc
1203 (setq setfunc (car-safe (cdr-safe setfunc)))
1204 (or (functionp setfunc) (setq setfunc nil)))
1205 (funcall (or setfunc 'set-default)
1206 (eval (nth 1 form) lexical-binding)
1207 ;; The second arg is an expression that evaluates to
1208 ;; an expression. The second evaluation is the one
1209 ;; normally performed not by normal execution but by
1210 ;; custom-initialize-set (for example), which does not
1211 ;; use lexical-binding.
1212 (eval (eval (nth 2 form) lexical-binding))))
1213 form)
1214 ;; `defface' is macroexpanded to `custom-declare-face'.
1215 ((eq (car form) 'custom-declare-face)
1216 ;; Reset the face.
1217 (let ((face-symbol (eval (nth 1 form) lexical-binding)))
1218 (setq face-new-frame-defaults
1219 (assq-delete-all face-symbol face-new-frame-defaults))
1220 (put face-symbol 'face-defface-spec nil)
1221 (put face-symbol 'face-override-spec nil))
1222 form)
1223 ((eq (car form) 'progn)
1224 (cons 'progn (mapcar #'elisp--eval-defun-1 (cdr form))))
1225 (t form)))
1226
1227 (defun elisp--eval-defun ()
1228 "Evaluate defun that point is in or before.
1229 The value is displayed in the echo area.
1230 If the current defun is actually a call to `defvar',
1231 then reset the variable using the initial value expression
1232 even if the variable already has some other value.
1233 \(Normally `defvar' does not change the variable's value
1234 if it already has a value.)
1235
1236 Return the result of evaluation."
1237 ;; FIXME: the print-length/level bindings should only be applied while
1238 ;; printing, not while evaluating.
1239 (let ((debug-on-error eval-expression-debug-on-error)
1240 (print-length eval-expression-print-length)
1241 (print-level eval-expression-print-level))
1242 (save-excursion
1243 ;; Arrange for eval-region to "read" the (possibly) altered form.
1244 ;; eval-region handles recording which file defines a function or
1245 ;; variable.
1246 (let ((standard-output t)
1247 beg end form)
1248 ;; Read the form from the buffer, and record where it ends.
1249 (save-excursion
1250 (end-of-defun)
1251 (beginning-of-defun)
1252 (setq beg (point))
1253 (setq form (read (current-buffer)))
1254 (setq end (point)))
1255 ;; Alter the form if necessary.
1256 (let ((form (eval-sexp-add-defvars
1257 (elisp--eval-defun-1 (macroexpand form)))))
1258 (eval-region beg end standard-output
1259 (lambda (_ignore)
1260 ;; Skipping to the end of the specified region
1261 ;; will make eval-region return.
1262 (goto-char end)
1263 form))))))
1264 (let ((str (eval-expression-print-format (car values))))
1265 (if str (princ str)))
1266 ;; The result of evaluation has been put onto VALUES. So return it.
1267 (car values))
1268
1269 (defun eval-defun (edebug-it)
1270 "Evaluate the top-level form containing point, or after point.
1271
1272 If the current defun is actually a call to `defvar' or `defcustom',
1273 evaluating it this way resets the variable using its initial value
1274 expression (using the defcustom's :set function if there is one), even
1275 if the variable already has some other value. \(Normally `defvar' and
1276 `defcustom' do not alter the value if there already is one.) In an
1277 analogous way, evaluating a `defface' overrides any customizations of
1278 the face, so that it becomes defined exactly as the `defface' expression
1279 says.
1280
1281 If `eval-expression-debug-on-error' is non-nil, which is the default,
1282 this command arranges for all errors to enter the debugger.
1283
1284 With a prefix argument, instrument the code for Edebug.
1285
1286 If acting on a `defun' for FUNCTION, and the function was
1287 instrumented, `Edebug: FUNCTION' is printed in the echo area. If not
1288 instrumented, just FUNCTION is printed.
1289
1290 If not acting on a `defun', the result of evaluation is displayed in
1291 the echo area. This display is controlled by the variables
1292 `eval-expression-print-length' and `eval-expression-print-level',
1293 which see."
1294 (interactive "P")
1295 (cond (edebug-it
1296 (require 'edebug)
1297 (eval-defun (not edebug-all-defs)))
1298 (t
1299 (if (null eval-expression-debug-on-error)
1300 (elisp--eval-defun)
1301 (let (new-value value)
1302 (let ((debug-on-error elisp--eval-last-sexp-fake-value))
1303 (setq value (elisp--eval-defun))
1304 (setq new-value debug-on-error))
1305 (unless (eq elisp--eval-last-sexp-fake-value new-value)
1306 (setq debug-on-error new-value))
1307 value)))))
1308
1309 ;;; ElDoc Support
1310
1311 (defvar elisp--eldoc-last-data (make-vector 3 nil)
1312 "Bookkeeping; elements are as follows:
1313 0 - contains the last symbol read from the buffer.
1314 1 - contains the string last displayed in the echo area for variables,
1315 or argument string for functions.
1316 2 - `function' if function args, `variable' if variable documentation.")
1317
1318 (defun elisp-eldoc-documentation-function ()
1319 "`eldoc-documentation-function' (which see) for Emacs Lisp."
1320 (let ((current-symbol (elisp--current-symbol))
1321 (current-fnsym (elisp--fnsym-in-current-sexp)))
1322 (cond ((null current-fnsym)
1323 nil)
1324 ((eq current-symbol (car current-fnsym))
1325 (or (apply #'elisp-get-fnsym-args-string current-fnsym)
1326 (elisp-get-var-docstring current-symbol)))
1327 (t
1328 (or (elisp-get-var-docstring current-symbol)
1329 (apply #'elisp-get-fnsym-args-string current-fnsym))))))
1330
1331 (defun elisp-get-fnsym-args-string (sym &optional index prefix)
1332 "Return a string containing the parameter list of the function SYM.
1333 If SYM is a subr and no arglist is obtainable from the docstring
1334 or elsewhere, return a 1-line docstring."
1335 (let ((argstring
1336 (cond
1337 ((not (and sym (symbolp sym) (fboundp sym))) nil)
1338 ((and (eq sym (aref elisp--eldoc-last-data 0))
1339 (eq 'function (aref elisp--eldoc-last-data 2)))
1340 (aref elisp--eldoc-last-data 1))
1341 (t
1342 (let* ((advertised (gethash (indirect-function sym)
1343 advertised-signature-table t))
1344 doc
1345 (args
1346 (cond
1347 ((listp advertised) advertised)
1348 ((setq doc (help-split-fundoc
1349 (condition-case nil (documentation sym t)
1350 (invalid-function nil))
1351 sym))
1352 (car doc))
1353 (t (help-function-arglist sym)))))
1354 ;; Stringify, and store before highlighting, downcasing, etc.
1355 (elisp--last-data-store sym (elisp-function-argstring args)
1356 'function))))))
1357 ;; Highlight, truncate.
1358 (if argstring
1359 (elisp--highlight-function-argument
1360 sym argstring index
1361 (or prefix
1362 (concat (propertize (symbol-name sym) 'face
1363 (if (functionp sym)
1364 'font-lock-function-name-face
1365 'font-lock-keyword-face))
1366 ": "))))))
1367
1368 (defun elisp--highlight-function-argument (sym args index prefix)
1369 "Highlight argument INDEX in ARGS list for function SYM.
1370 In the absence of INDEX, just call `eldoc-docstring-format-sym-doc'."
1371 ;; FIXME: This should probably work on the list representation of `args'
1372 ;; rather than its string representation.
1373 ;; FIXME: This function is much too long, we need to split it up!
1374 (let ((start nil)
1375 (end 0)
1376 (argument-face 'eldoc-highlight-function-argument)
1377 (args-lst (mapcar (lambda (x)
1378 (replace-regexp-in-string
1379 "\\`[(]\\|[)]\\'" "" x))
1380 (split-string args))))
1381 ;; Find the current argument in the argument string. We need to
1382 ;; handle `&rest' and informal `...' properly.
1383 ;;
1384 ;; FIXME: What to do with optional arguments, like in
1385 ;; (defun NAME ARGLIST [DOCSTRING] BODY...) case?
1386 ;; The problem is there is no robust way to determine if
1387 ;; the current argument is indeed a docstring.
1388
1389 ;; When `&key' is used finding position based on `index'
1390 ;; would be wrong, so find the arg at point and determine
1391 ;; position in ARGS based on this current arg.
1392 (when (string-match "&key" args)
1393 (let* (case-fold-search
1394 key-have-value
1395 (sym-name (symbol-name sym))
1396 (cur-w (current-word))
1397 (args-lst-ak (cdr (member "&key" args-lst)))
1398 (limit (save-excursion
1399 (when (re-search-backward sym-name nil t)
1400 (match-end 0))))
1401 (cur-a (if (and cur-w (string-match ":\\([^ ()]*\\)" cur-w))
1402 (substring cur-w 1)
1403 (save-excursion
1404 (let (split)
1405 (when (re-search-backward ":\\([^()\n]*\\)" limit t)
1406 (setq split (split-string (match-string 1) " " t))
1407 (prog1 (car split)
1408 (when (cdr split)
1409 (setq key-have-value t))))))))
1410 ;; If `cur-a' is not one of `args-lst-ak'
1411 ;; assume user is entering an unknown key
1412 ;; referenced in last position in signature.
1413 (other-key-arg (and (stringp cur-a)
1414 args-lst-ak
1415 (not (member (upcase cur-a) args-lst-ak))
1416 (upcase (car (last args-lst-ak))))))
1417 (unless (string= cur-w sym-name)
1418 ;; The last keyword have already a value
1419 ;; i.e :foo a b and cursor is at b.
1420 ;; If signature have also `&rest'
1421 ;; (assume it is after the `&key' section)
1422 ;; go to the arg after `&rest'.
1423 (if (and key-have-value
1424 (save-excursion
1425 (not (re-search-forward ":.*" (point-at-eol) t)))
1426 (string-match "&rest \\([^ ()]*\\)" args))
1427 (setq index nil ; Skip next block based on positional args.
1428 start (match-beginning 1)
1429 end (match-end 1))
1430 ;; If `cur-a' is nil probably cursor is on a positional arg
1431 ;; before `&key', in this case, exit this block and determine
1432 ;; position with `index'.
1433 (when (and cur-a ; A keyword arg (dot removed) or nil.
1434 (or (string-match
1435 (concat "\\_<" (upcase cur-a) "\\_>") args)
1436 (string-match
1437 (concat "\\_<" other-key-arg "\\_>") args)))
1438 (setq index nil ; Skip next block based on positional args.
1439 start (match-beginning 0)
1440 end (match-end 0)))))))
1441 ;; Handle now positional arguments.
1442 (while (and index (>= index 1))
1443 (if (string-match "[^ ()]+" args end)
1444 (progn
1445 (setq start (match-beginning 0)
1446 end (match-end 0))
1447 (let ((argument (match-string 0 args)))
1448 (cond ((string= argument "&rest")
1449 ;; All the rest arguments are the same.
1450 (setq index 1))
1451 ((string= argument "&optional")) ; Skip.
1452 ((string= argument "&allow-other-keys")) ; Skip.
1453 ;; Back to index 0 in ARG1 ARG2 ARG2 ARG3 etc...
1454 ;; like in `setq'.
1455 ((or (and (string-match-p "\\.\\.\\.\\'" argument)
1456 (string= argument (car (last args-lst))))
1457 (and (string-match-p "\\.\\.\\.\\'"
1458 (substring args 1 (1- (length args))))
1459 (= (length (remove "..." args-lst)) 2)
1460 (> index 1) (eq (logand index 1) 1)))
1461 (setq index 0))
1462 (t
1463 (setq index (1- index))))))
1464 (setq end (length args)
1465 start (1- end)
1466 argument-face 'font-lock-warning-face
1467 index 0)))
1468 (let ((doc args))
1469 (when start
1470 (setq doc (copy-sequence args))
1471 (add-text-properties start end (list 'face argument-face) doc))
1472 (setq doc (eldoc-docstring-format-sym-doc prefix doc))
1473 doc)))
1474
1475 ;; Return a string containing a brief (one-line) documentation string for
1476 ;; the variable.
1477 (defun elisp-get-var-docstring (sym)
1478 (cond ((not sym) nil)
1479 ((and (eq sym (aref elisp--eldoc-last-data 0))
1480 (eq 'variable (aref elisp--eldoc-last-data 2)))
1481 (aref elisp--eldoc-last-data 1))
1482 (t
1483 (let ((doc (documentation-property sym 'variable-documentation t)))
1484 (when doc
1485 (let ((doc (eldoc-docstring-format-sym-doc
1486 sym (elisp--docstring-first-line doc)
1487 'font-lock-variable-name-face)))
1488 (elisp--last-data-store sym doc 'variable)))))))
1489
1490 (defun elisp--last-data-store (symbol doc type)
1491 (aset elisp--eldoc-last-data 0 symbol)
1492 (aset elisp--eldoc-last-data 1 doc)
1493 (aset elisp--eldoc-last-data 2 type)
1494 doc)
1495
1496 ;; Note that any leading `*' in the docstring (which indicates the variable
1497 ;; is a user option) is removed.
1498 (defun elisp--docstring-first-line (doc)
1499 (and (stringp doc)
1500 (substitute-command-keys
1501 (save-match-data
1502 ;; Don't use "^" in the regexp below since it may match
1503 ;; anywhere in the doc-string.
1504 (let ((start (if (string-match "\\`\\*" doc) (match-end 0) 0)))
1505 (cond ((string-match "\n" doc)
1506 (substring doc start (match-beginning 0)))
1507 ((zerop start) doc)
1508 (t (substring doc start))))))))
1509 \f
1510 ;; Return a list of current function name and argument index.
1511 (defun elisp--fnsym-in-current-sexp ()
1512 (save-excursion
1513 (let ((argument-index (1- (elisp--beginning-of-sexp))))
1514 ;; If we are at the beginning of function name, this will be -1.
1515 (when (< argument-index 0)
1516 (setq argument-index 0))
1517 ;; Don't do anything if current word is inside a string.
1518 (if (= (or (char-after (1- (point))) 0) ?\")
1519 nil
1520 (list (elisp--current-symbol) argument-index)))))
1521
1522 ;; Move to the beginning of current sexp. Return the number of nested
1523 ;; sexp the point was over or after.
1524 (defun elisp--beginning-of-sexp ()
1525 (let ((parse-sexp-ignore-comments t)
1526 (num-skipped-sexps 0))
1527 (condition-case _
1528 (progn
1529 ;; First account for the case the point is directly over a
1530 ;; beginning of a nested sexp.
1531 (condition-case _
1532 (let ((p (point)))
1533 (forward-sexp -1)
1534 (forward-sexp 1)
1535 (when (< (point) p)
1536 (setq num-skipped-sexps 1)))
1537 (error))
1538 (while
1539 (let ((p (point)))
1540 (forward-sexp -1)
1541 (when (< (point) p)
1542 (setq num-skipped-sexps (1+ num-skipped-sexps))))))
1543 (error))
1544 num-skipped-sexps))
1545
1546 ;; returns nil unless current word is an interned symbol.
1547 (defun elisp--current-symbol ()
1548 (let ((c (char-after (point))))
1549 (and c
1550 (memq (char-syntax c) '(?w ?_))
1551 (intern-soft (current-word)))))
1552
1553 (defun elisp-function-argstring (arglist)
1554 "Return ARGLIST as a string enclosed by ().
1555 ARGLIST is either a string, or a list of strings or symbols."
1556 (let ((str (cond ((stringp arglist) arglist)
1557 ((not (listp arglist)) nil)
1558 (t (help--make-usage-docstring 'toto arglist)))))
1559 (if (and str (string-match "\\`([^ )]+ ?" str))
1560 (replace-match "(" t t str)
1561 str)))
1562
1563 (provide 'elisp-mode)
1564 ;;; elisp-mode.el ends here