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