1 ;;; elisp-mode.el --- Emacs Lisp mode -*- lexical-binding:t -*-
3 ;; Copyright (C) 1985-1986, 1999-2015 Free Software Foundation, Inc.
5 ;; Maintainer: emacs-devel@gnu.org
6 ;; Keywords: lisp, languages
9 ;; This file is part of GNU Emacs.
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.
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.
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/>.
26 ;; The major mode for editing Emacs Lisp code.
27 ;; This mode is documented in the Emacs manual.
32 (eval-when-compile (require 'cl-lib))
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))
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)
44 "Syntax table used in `emacs-lisp-mode'.")
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"))
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"))
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"
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))
178 "Keymap for Emacs Lisp mode.
179 All commands in `lisp-mode-shared-map' are inherited by this map.")
181 (defun emacs-lisp-byte-compile ()
182 "Byte compile the file containing the current buffer."
185 (byte-compile-file buffer-file-name)
186 (error "The buffer must be saved in a file first")))
188 (defun emacs-lisp-byte-compile-and-load ()
189 "Byte-compile the current file (if it has changed), then load compiled code."
192 (error "The buffer must be saved in a file first"))
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))))
198 (byte-recompile-file buffer-file-name nil 0 t))
200 (defun emacs-lisp-macroexpand ()
201 "Macroexpand the form after point.
202 Comments in the form will be lost."
204 (let* ((start (point))
205 (exp (read (current-buffer)))
206 ;; Compute it before, since it may signal errors.
207 (new (macroexpand-1 exp)))
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)))))
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)
222 (define-derived-mode emacs-lisp-mode prog-mode "Emacs-Lisp"
223 "Major mode for editing Lisp code to run in Emacs.
225 Delete converts tabs to spaces as it moves back.
226 Blank lines separate paragraphs. Semicolons start comments.
228 \\{emacs-lisp-mode-map}"
230 (defvar xref-find-function)
231 (defvar xref-identifier-completion-table-function)
232 (defvar project-search-path-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 (setq-local xref-find-function #'elisp-xref-find)
242 (setq-local xref-identifier-completion-table-function
243 #'elisp--xref-identifier-completion-table)
244 (setq-local project-search-path-function #'elisp-search-path)
245 (add-hook 'completion-at-point-functions
246 #'elisp-completion-at-point nil 'local))
248 ;; Font-locking support.
250 (defun elisp--font-lock-flush-elisp-buffers (&optional file)
251 ;; FIXME: Aren't we only ever called from after-load-functions?
252 ;; Don't flush during load unless called from after-load-functions.
253 ;; In that case, FILE is non-nil. It's somehow strange that
254 ;; load-in-progress is t when an after-load-function is called since
255 ;; that should run *after* the load...
256 (when (or (not load-in-progress) file)
257 ;; FIXME: If the loaded file did not define any macros, there shouldn't
258 ;; be any need to font-lock-flush all the Elisp buffers.
259 (dolist (buf (buffer-list))
260 (with-current-buffer buf
261 (when (derived-mode-p 'emacs-lisp-mode)
262 ;; So as to take into account new macros that may have been defined
263 ;; by the just-loaded file.
264 (font-lock-flush))))))
266 ;;; Completion at point for Elisp
268 (defun elisp--local-variables-1 (vars sexp)
269 "Return the vars locally bound around the witness, or nil if not found."
275 (`(,(or `let `let*) ,bindings)
277 (when (eq 'let* (car sexp))
278 (dolist (binding (cdr (reverse bindings)))
279 (push (or (car-safe binding) binding) vars)))
280 (elisp--local-variables-1
281 vars (car (cdr-safe (car (last bindings)))))))
282 (`(,(or `let `let*) ,bindings . ,body)
284 (dolist (binding bindings)
285 (push (or (car-safe binding) binding) vars))
286 (elisp--local-variables-1 vars (car (last body)))))
288 ;; FIXME: Look for the witness inside `args'.
290 (`(lambda ,args . ,body)
291 (elisp--local-variables-1
292 (append (remq '&optional (remq '&rest args)) vars)
294 (`(condition-case ,_ ,e) (elisp--local-variables-1 vars e))
295 (`(condition-case ,v ,_ . ,catches)
296 (elisp--local-variables-1
297 (cons v vars) (cdr (car (last catches)))))
299 ;; FIXME: Look for the witness inside sexp.
301 ;; FIXME: Handle `cond'.
303 (elisp--local-variables-1 vars (car (last sexp))))
304 (`elisp--witness--lisp (or vars '(nil)))
306 ;; We didn't find the witness in the last element so we try to
307 ;; backtrack to the last-but-one.
308 (setq sexp (ignore-errors (butlast sexp)))))
311 (defun elisp--local-variables ()
312 "Return a list of locally let-bound variables at point."
314 (skip-syntax-backward "w_")
315 (let* ((ppss (syntax-ppss))
316 (txt (buffer-substring-no-properties (or (car (nth 9 ppss)) (point))
317 (or (nth 8 ppss) (point))))
319 (dolist (p (nth 9 ppss))
320 (push (cdr (syntax-after p)) closer))
321 (setq closer (apply #'string closer))
322 (let* ((sexp (condition-case nil
323 (car (read-from-string
324 (concat txt "elisp--witness--lisp" closer)))
325 ((invalid-read-syntax end-of-file) nil)))
326 (macroexpand-advice (lambda (expander form &rest args)
328 (apply expander form args)
333 (advice-add 'macroexpand :around macroexpand-advice)
334 (macroexpand-all sexp))
335 (advice-remove 'macroexpand macroexpand-advice)))
336 (vars (elisp--local-variables-1 nil sexp)))
338 (mapcar (lambda (var)
340 (not (string-match (symbol-name var) "\\`[&_]"))
341 ;; Eliminate uninterned vars.
346 (defvar elisp--local-variables-completion-table
347 ;; Use `defvar' rather than `defconst' since defconst would purecopy this
348 ;; value, which would doubly fail: it would fail because purecopy can't
349 ;; handle the recursive bytecode object, and it would fail because it would
350 ;; move `lastpos' and `lastvars' to pure space where they'd be immutable!
351 (let ((lastpos nil) (lastvars nil))
352 (letrec ((hookfun (lambda ()
354 (remove-hook 'post-command-hook hookfun))))
355 (completion-table-dynamic
358 (skip-syntax-backward "_w")
359 (let ((newpos (cons (point) (current-buffer))))
360 (unless (equal lastpos newpos)
361 (add-hook 'post-command-hook hookfun)
362 (setq lastpos newpos)
364 (mapcar #'symbol-name (elisp--local-variables))))))
367 (defun elisp--expect-function-p (pos)
368 "Return non-nil if the symbol at point is expected to be a function."
370 (and (eq (char-before pos) ?')
371 (eq (char-before (1- pos)) ?#))
373 (let ((parent (nth 1 (syntax-ppss pos))))
377 (looking-at (concat "(\\(cl-\\)?"
378 (regexp-opt '("declare-function"
379 "function" "defadvice"
383 (eq (match-end 0) pos)))))))
385 (defun elisp--form-quoted-p (pos)
386 "Return non-nil if the form at POS is not evaluated.
387 It can be quoted, or be inside a quoted form."
388 ;; FIXME: Do some macro expansion maybe.
390 (let ((state (syntax-ppss pos)))
391 (or (nth 8 state) ; Code inside strings usually isn't evaluated.
392 ;; FIXME: The 9th element is undocumented.
393 (let ((nesting (cons (point) (reverse (nth 9 state))))
395 (while (and nesting (not res))
396 (goto-char (pop nesting))
398 ((or (eq (char-after) ?\[)
400 (skip-chars-backward " ")
401 (memq (char-before) '(?' ?` ?‘))))
403 ((eq (char-before) ?,)
404 (setq nesting nil))))
407 ;; FIXME: Support for Company brings in features which straddle eldoc.
408 ;; We should consolidate this, so that major modes can provide all that
410 ;; - a function to extract "the reference at point" (may be more complex
411 ;; than a mere string, to distinguish various namespaces).
412 ;; - a function to jump to such a reference.
413 ;; - a function to show the signature/interface of such a reference.
414 ;; - a function to build a help-buffer about that reference.
415 ;; FIXME: Those functions should also be used by the normal completion code in
416 ;; the *Completions* buffer.
418 (defun elisp--company-doc-buffer (str)
419 (let ((symbol (intern-soft str)))
420 ;; FIXME: we really don't want to "display-buffer and then undo it".
421 (save-window-excursion
422 ;; Make sure we don't display it in another frame, otherwise
423 ;; save-window-excursion won't be able to undo it.
424 (let ((display-buffer-overriding-action
425 '(nil . ((inhibit-switch-frame . t)))))
428 ((fboundp symbol) (describe-function symbol))
429 ((boundp symbol) (describe-variable symbol))
430 ((featurep symbol) (describe-package symbol))
431 ((facep symbol) (describe-face symbol))
432 (t (signal 'user-error nil)))
435 (defun elisp--company-doc-string (str)
436 (let* ((symbol (intern-soft str))
437 (doc (if (fboundp symbol)
438 (documentation symbol t)
439 (documentation-property symbol 'variable-documentation t))))
441 (string-match ".*$" doc)
442 (match-string 0 doc))))
444 (declare-function find-library-name "find-func" (library))
445 (declare-function find-function-library "find-func" (function &optional l-o v))
447 (defun elisp--company-location (str)
448 (let ((sym (intern-soft str)))
450 ((fboundp sym) (find-definition-noselect sym nil))
451 ((boundp sym) (find-definition-noselect sym 'defvar))
454 (cons (find-file-noselect (find-library-name
457 ((facep sym) (find-definition-noselect sym 'defface)))))
459 (defun elisp-completion-at-point ()
460 "Function used for `completion-at-point-functions' in `emacs-lisp-mode'."
461 (with-syntax-table emacs-lisp-mode-syntax-table
463 (beg (condition-case nil
466 (skip-chars-forward "`',‘#")
470 (unless (or (eq beg (point-max))
471 (member (char-syntax (char-after beg))
477 (skip-chars-backward "'’")
478 (when (>= (point) pos)
481 ;; t if in function position.
482 (funpos (eq (char-before beg) ?\())
483 (quoted (elisp--form-quoted-p beg)))
484 (when (and end (or (not (nth 8 (syntax-ppss)))
485 (memq (char-before beg) '(?` ?‘))))
487 (if (or (not funpos) quoted)
488 ;; FIXME: We could look at the first element of the list and
489 ;; use it to provide a more specific completion table in some
490 ;; cases. E.g. filter out keywords that are not understood by
491 ;; the macro/function being called.
493 ((elisp--expect-function-p beg)
496 :company-doc-buffer #'elisp--company-doc-buffer
497 :company-docsig #'elisp--company-doc-string
498 :company-location #'elisp--company-location))
501 ;; Don't include all symbols (bug#16646).
502 :predicate (lambda (sym)
508 (lambda (str) (if (fboundp (intern-soft str)) " <f>"))
509 :company-doc-buffer #'elisp--company-doc-buffer
510 :company-docsig #'elisp--company-doc-string
511 :company-location #'elisp--company-location))
513 (list nil (completion-table-merge
514 elisp--local-variables-completion-table
515 (apply-partially #'completion-table-with-predicate
519 :company-doc-buffer #'elisp--company-doc-buffer
520 :company-docsig #'elisp--company-doc-string
521 :company-location #'elisp--company-location)))
522 ;; Looks like a funcall position. Let's double check.
527 (progn (up-list -1) (forward-char 1)
528 (let ((c (char-after)))
530 (if (memq (char-syntax c) '(?w ?_))
531 (read (current-buffer))))))
534 ;; FIXME: Rather than hardcode special cases here,
535 ;; we should use something like a symbol-property.
537 (list t (mapcar (lambda (x) (symbol-name (car x)))
539 ;; FIXME: We should include some
540 ;; docstring with each entry.
542 macro-declarations-alist
543 defun-declarations-alist)))))
544 ((and (or `condition-case `condition-case-unless-debug)
545 (guard (save-excursion
550 :predicate (lambda (sym) (get sym 'error-conditions))))
551 ((and (or ?\( `let `let*)
552 (guard (save-excursion
554 (when (eq parent ?\()
557 (looking-at "\\_<let\\*?\\_>"))))
560 :company-doc-buffer #'elisp--company-doc-buffer
561 :company-docsig #'elisp--company-doc-string
562 :company-location #'elisp--company-location))
565 :company-doc-buffer #'elisp--company-doc-buffer
566 :company-docsig #'elisp--company-doc-string
567 :company-location #'elisp--company-location
569 (nconc (list beg end)
570 (if (null (car table-etc))
573 (if (memq (char-syntax (or (char-after end) ?\s))
576 (apply-partially 'completion-table-with-terminator
577 " " (cadr table-etc)))
578 (cddr table-etc)))))))))
580 (define-obsolete-function-alias
581 'lisp-completion-at-point 'elisp-completion-at-point "25.1")
585 (declare-function xref-make-bogus-location "xref" (message))
586 (declare-function xref-make "xref" (summary location))
587 (declare-function xref-collect-references "xref" (symbol dir))
589 (defun elisp-xref-find (action id)
593 (let ((sym (intern-soft id)))
595 (elisp--xref-find-definitions sym))))
597 (elisp--xref-find-references id))
599 (elisp--xref-find-apropos id))))
601 (defun elisp--xref-identifier-location (type sym)
604 (`defun (when (fboundp sym)
606 (find-function-library sym)))
607 (setq sym (car fun-lib))
609 (`defvar (and (boundp sym)
610 (let ((el-file (symbol-file sym 'defvar)))
613 ;; Don't show minor modes twice.
614 ;; TODO: If TYPE ever becomes dependent on the
615 ;; context, move this check outside.
616 (not (and (fboundp sym)
617 (memq sym minor-mode-list)))
619 (help-C-file-name sym 'var)))))
620 (`feature (and (featurep sym)
621 ;; Skip when a function with the same name
622 ;; is defined, because it's probably in the
626 (find-library-name (symbol-name sym)))))
627 (`defface (when (facep sym)
628 (symbol-file sym 'defface))))))
630 (when (string-match-p "\\.elc\\'" file)
631 (setq file (substring file 0 -1)))
632 (xref-make-elisp-location sym type file))))
634 (defvar elisp--xref-format
635 (let ((str "(%s %s)"))
636 (put-text-property 1 3 'face 'font-lock-keyword-face str)
637 (put-text-property 4 6 'face 'font-lock-function-name-face str)
640 (defun elisp--xref-find-definitions (symbol)
643 (dolist (type '(feature defface defvar defun))
646 (elisp--xref-identifier-location type symbol)
648 (xref-make-bogus-location (error-message-string err))))))
651 (xref-make (format elisp--xref-format type symbol)
656 (declare-function project-search-path "project")
657 (declare-function project-current "project")
659 (defun elisp--xref-find-references (symbol)
662 (xref-collect-references symbol dir))
663 (project-search-path (project-current))))
665 (defun elisp--xref-find-apropos (regexp)
668 (dolist (sym (apropos-internal regexp))
669 (push (elisp--xref-find-definitions sym) lst))
672 (defvar elisp--xref-identifier-completion-table
673 (apply-partially #'completion-table-with-predicate
682 (defun elisp--xref-identifier-completion-table ()
683 elisp--xref-identifier-completion-table)
685 (cl-defstruct (xref-elisp-location
686 (:constructor xref-make-elisp-location (symbol type file)))
687 "Location of an Emacs Lisp symbol definition."
690 (cl-defmethod xref-location-marker ((l xref-elisp-location))
691 (pcase-let (((cl-struct xref-elisp-location symbol type file) l))
694 (`defun (find-function-search-for-symbol symbol nil file))
695 ((or `defvar `defface)
696 (find-function-search-for-symbol symbol type file))
698 (cons (find-file-noselect file) 1)))))
699 (with-current-buffer (car buffer-point)
700 (goto-char (or (cdr buffer-point) (point-min)))
703 (cl-defmethod xref-location-group ((l xref-elisp-location))
704 (xref-elisp-location-file l))
706 (defun elisp-search-path ()
707 (defvar package-user-dir)
708 (cons package-user-dir load-path))
710 ;;; Elisp Interaction mode
712 (defvar lisp-interaction-mode-map
713 (let ((map (make-sparse-keymap))
714 (menu-map (make-sparse-keymap "Lisp-Interaction")))
715 (set-keymap-parent map lisp-mode-shared-map)
716 (define-key map "\e\C-x" 'eval-defun)
717 (define-key map "\e\C-q" 'indent-pp-sexp)
718 (define-key map "\e\t" 'completion-at-point)
719 (define-key map "\n" 'eval-print-last-sexp)
720 (bindings--define-key map [menu-bar lisp-interaction]
721 (cons "Lisp-Interaction" menu-map))
722 (bindings--define-key menu-map [eval-defun]
723 '(menu-item "Evaluate Defun" eval-defun
724 :help "Evaluate the top-level form containing point, or after point"))
725 (bindings--define-key menu-map [eval-print-last-sexp]
726 '(menu-item "Evaluate and Print" eval-print-last-sexp
727 :help "Evaluate sexp before point; print value into current buffer"))
728 (bindings--define-key menu-map [edebug-defun-lisp-interaction]
729 '(menu-item "Instrument Function for Debugging" edebug-defun
730 :help "Evaluate the top level form point is in, stepping through with Edebug"
732 (bindings--define-key menu-map [indent-pp-sexp]
733 '(menu-item "Indent or Pretty-Print" indent-pp-sexp
734 :help "Indent each line of the list starting just after point, or prettyprint it"))
735 (bindings--define-key menu-map [complete-symbol]
736 '(menu-item "Complete Lisp Symbol" completion-at-point
737 :help "Perform completion on Lisp symbol preceding point"))
739 "Keymap for Lisp Interaction mode.
740 All commands in `lisp-mode-shared-map' are inherited by this map.")
742 (define-derived-mode lisp-interaction-mode emacs-lisp-mode "Lisp Interaction"
743 "Major mode for typing and evaluating Lisp forms.
744 Like Lisp mode except that \\[eval-print-last-sexp] evals the Lisp expression
745 before point, and prints its value into the buffer, advancing point.
746 Note that printing is controlled by `eval-expression-print-length'
747 and `eval-expression-print-level'.
750 Delete converts tabs to spaces as it moves back.
751 Paragraphs are separated only by blank lines.
752 Semicolons start comments.
754 \\{lisp-interaction-mode-map}"
757 ;;; Emacs Lisp Byte-Code mode
760 (defconst emacs-list-byte-code-comment-re
761 (concat "\\(#\\)@\\([0-9]+\\) "
762 ;; Make sure it's a docstring and not a lazy-loaded byte-code.
763 "\\(?:[^(]\\|([^\"]\\)")))
765 (defun elisp--byte-code-comment (end &optional _point)
766 "Try to syntactically mark the #@NNN ....^_ docstrings in byte-code files."
767 (let ((ppss (syntax-ppss)))
768 (when (and (nth 4 ppss)
769 (eq (char-after (nth 8 ppss)) ?#))
770 (let* ((n (save-excursion
771 (goto-char (nth 8 ppss))
772 (when (looking-at emacs-list-byte-code-comment-re)
773 (string-to-number (match-string 2)))))
774 ;; `maxdiff' tries to make sure the loop below terminates.
777 (let* ((bchar (match-end 2))
778 (b (position-bytes bchar)))
780 (while (let ((diff (- (position-bytes (point)) b n)))
782 (when (> diff maxdiff) (setq diff maxdiff))
783 (forward-char (- diff))
784 (setq maxdiff (if (> diff 0) diff
785 (max (1- maxdiff) 1)))
788 (put-text-property (1- (point)) (point)
790 (string-to-syntax "> b"))
791 (goto-char end)))))))
793 (defun elisp-byte-code-syntax-propertize (start end)
794 (elisp--byte-code-comment end (point))
796 (syntax-propertize-rules
797 (emacs-list-byte-code-comment-re
798 (1 (prog1 "< b" (elisp--byte-code-comment end (point))))))
802 (add-to-list 'auto-mode-alist '("\\.elc\\'" . elisp-byte-code-mode))
804 (define-derived-mode elisp-byte-code-mode emacs-lisp-mode
806 "Major mode for *.elc files."
807 ;; TODO: Add way to disassemble byte-code under point.
808 (setq-local open-paren-in-column-0-is-defun-start nil)
809 (setq-local syntax-propertize-function
810 #'elisp-byte-code-syntax-propertize))
813 ;;; Globally accessible functionality
815 (defun eval-print-last-sexp (&optional eval-last-sexp-arg-internal)
816 "Evaluate sexp before point; print value into current buffer.
818 Normally, this function truncates long output according to the value
819 of the variables `eval-expression-print-length' and
820 `eval-expression-print-level'. With a prefix argument of zero,
821 however, there is no such truncation. Such a prefix argument
822 also causes integers to be printed in several additional formats
823 \(octal, hexadecimal, and character).
825 If `eval-expression-debug-on-error' is non-nil, which is the default,
826 this command arranges for all errors to enter the debugger."
828 (let ((standard-output (current-buffer)))
830 (eval-last-sexp (or eval-last-sexp-arg-internal t))
834 (defun last-sexp-setup-props (beg end value alt1 alt2)
835 "Set up text properties for the output of `elisp--eval-last-sexp'.
836 BEG and END are the start and end of the output in current-buffer.
837 VALUE is the Lisp value printed, ALT1 and ALT2 are strings for the
838 alternative printed representations that can be displayed."
839 (let ((map (make-sparse-keymap)))
840 (define-key map "\C-m" 'elisp-last-sexp-toggle-display)
841 (define-key map [down-mouse-2] 'mouse-set-point)
842 (define-key map [mouse-2] 'elisp-last-sexp-toggle-display)
845 `(printed-value (,value ,alt1 ,alt2)
848 help-echo "RET, mouse-2: toggle abbreviated display"
849 rear-nonsticky (mouse-face keymap help-echo
853 (defun elisp-last-sexp-toggle-display (&optional _arg)
854 "Toggle between abbreviated and unabbreviated printed representations."
858 (let ((value (get-text-property (point) 'printed-value)))
860 (let ((beg (or (previous-single-property-change (min (point-max) (1+ (point)))
863 (end (or (next-single-char-property-change (point) 'printed-value) (point)))
864 (standard-output (current-buffer))
866 (delete-region beg end)
867 (insert (nth 1 value))
869 (setq point (1- (point))))
870 (last-sexp-setup-props beg (point)
874 (goto-char (min (point-max) point)))))))
876 (defun prin1-char (char) ;FIXME: Move it, e.g. to simple.el.
877 "Return a string representing CHAR as a character rather than as an integer.
878 If CHAR is not a character, return nil."
881 (let ((c (event-basic-type char))
882 (mods (event-modifiers char))
884 ;; Prevent ?A from turning into ?\S-a.
885 (if (and (memq 'shift mods)
886 (zerop (logand char ?\S-\^@))
887 (not (let ((case-fold-search nil))
888 (char-equal c (upcase c)))))
889 (setq c (upcase c) mods nil))
890 ;; What string are we considering using?
897 (cond ((eq modif 'super) "\\s-")
898 (t (string ?\\ (upcase (aref (symbol-name modif) 0)) ?-))))
901 ((memq c '(?\; ?\( ?\) ?\{ ?\} ?\[ ?\] ?\" ?\' ?\\)) (string ?\\ c))
906 ;; Verify the string reads a CHAR, not to some other character.
907 ;; If it doesn't, return nil instead.
909 (= (car (read-from-string string)) char)
912 (defun elisp--preceding-sexp ()
913 "Return sexp before the point."
914 (let ((opoint (point))
918 (with-syntax-table emacs-lisp-mode-syntax-table
919 ;; If this sexp appears to be enclosed in `...' or ‘...’
920 ;; then ignore the surrounding quotes.
921 (cond ((eq (preceding-char) ?’)
922 (progn (forward-char -1) (setq opoint (point))))
923 ((or (eq (following-char) ?\')
924 (eq (preceding-char) ?\'))
925 (setq left-quote ?\`)))
927 ;; If we were after `?\e' (or similar case),
928 ;; use the whole thing, not just the `e'.
929 (when (eq (preceding-char) ?\\)
931 (when (eq (preceding-char) ??)
934 ;; Skip over hash table read syntax.
935 (and (> (point) (1+ (point-min)))
936 (looking-back "#s" (- (point) 2))
940 (when (eq (preceding-char) ?=)
943 (skip-chars-backward "0-9#=")
944 (setq labeled-p (looking-at "\\(#[0-9]+=\\)+")))
949 (if (eq (following-char) left-quote)
950 ;; vladimir@cs.ualberta.ca 30-Jul-1997: Skip ` in `variable' so
951 ;; that the value is returned, not the name.
953 (when (looking-at ",@?") (goto-char (match-end 0)))
954 (narrow-to-region (point-min) opoint)
955 (setq expr (read (current-buffer)))
956 ;; If it's an (interactive ...) form, it's more useful to show how an
957 ;; interactive call would use it.
958 ;; FIXME: Is it really the right place for this?
959 (when (eq (car-safe expr) 'interactive)
962 (lambda (&rest args) ,expr args))))
964 (define-obsolete-function-alias 'preceding-sexp 'elisp--preceding-sexp "25.1")
966 (defun elisp--eval-last-sexp (eval-last-sexp-arg-internal)
967 "Evaluate sexp before point; print value in the echo area.
968 If EVAL-LAST-SEXP-ARG-INTERNAL is non-nil, print output into
969 current buffer. If EVAL-LAST-SEXP-ARG-INTERNAL is `0', print
970 output with no limit on the length and level of lists, and
971 include additional formats for integers \(octal, hexadecimal, and
973 (let ((standard-output (if eval-last-sexp-arg-internal (current-buffer) t)))
974 ;; Setup the lexical environment if lexical-binding is enabled.
975 (elisp--eval-last-sexp-print-value
976 (eval (eval-sexp-add-defvars (elisp--preceding-sexp)) lexical-binding)
977 eval-last-sexp-arg-internal)))
980 (defun elisp--eval-last-sexp-print-value (value &optional eval-last-sexp-arg-internal)
981 (let ((unabbreviated (let ((print-length nil) (print-level nil))
982 (prin1-to-string value)))
983 (print-length (and (not (zerop (prefix-numeric-value
984 eval-last-sexp-arg-internal)))
985 eval-expression-print-length))
986 (print-level (and (not (zerop (prefix-numeric-value
987 eval-last-sexp-arg-internal)))
988 eval-expression-print-level))
993 (let ((str (eval-expression-print-format value)))
994 (if str (princ str)))
996 (when (and (bufferp standard-output)
997 (or (not (null print-length))
998 (not (null print-level)))
999 (not (string= unabbreviated
1000 (buffer-substring-no-properties beg end))))
1001 (last-sexp-setup-props beg end value
1003 (buffer-substring-no-properties beg end))
1007 (defvar elisp--eval-last-sexp-fake-value (make-symbol "t"))
1009 (defun eval-sexp-add-defvars (exp &optional pos)
1010 "Prepend EXP with all the `defvar's that precede it in the buffer.
1011 POS specifies the starting position where EXP was found and defaults to point."
1012 (setq exp (macroexpand-all exp)) ;Eager macro-expansion.
1013 (if (not lexical-binding)
1016 (unless pos (setq pos (point)))
1018 (goto-char (point-min))
1019 (while (re-search-forward
1020 "(def\\(?:var\\|const\\|custom\\)[ \t\n]+\\([^; '()\n\t]+\\)"
1022 (let ((var (intern (match-string 1))))
1023 (and (not (special-variable-p var))
1025 (zerop (car (syntax-ppss (match-beginning 0)))))
1027 `(progn ,@(mapcar (lambda (v) `(defvar ,v)) vars) ,exp)))))
1029 (defun eval-last-sexp (eval-last-sexp-arg-internal)
1030 "Evaluate sexp before point; print value in the echo area.
1031 Interactively, with prefix argument, print output into current buffer.
1033 Normally, this function truncates long output according to the value
1034 of the variables `eval-expression-print-length' and
1035 `eval-expression-print-level'. With a prefix argument of zero,
1036 however, there is no such truncation. Such a prefix argument
1037 also causes integers to be printed in several additional formats
1038 \(octal, hexadecimal, and character).
1040 If `eval-expression-debug-on-error' is non-nil, which is the default,
1041 this command arranges for all errors to enter the debugger."
1043 (if (null eval-expression-debug-on-error)
1044 (elisp--eval-last-sexp eval-last-sexp-arg-internal)
1046 (let ((debug-on-error elisp--eval-last-sexp-fake-value))
1047 (cons (elisp--eval-last-sexp eval-last-sexp-arg-internal)
1049 (unless (eq (cdr value) elisp--eval-last-sexp-fake-value)
1050 (setq debug-on-error (cdr value)))
1053 (defun elisp--eval-defun-1 (form)
1054 "Treat some expressions specially.
1055 Reset the `defvar' and `defcustom' variables to the initial value.
1056 \(For `defcustom', use the :set function if there is one.)
1057 Reinitialize the face according to the `defface' specification."
1058 ;; The code in edebug-defun should be consistent with this, but not
1059 ;; the same, since this gets a macroexpanded form.
1060 (cond ((not (listp form))
1062 ((and (eq (car form) 'defvar)
1063 (cdr-safe (cdr-safe form))
1064 (boundp (cadr form)))
1065 ;; Force variable to be re-set.
1066 `(progn (defvar ,(nth 1 form) nil ,@(nthcdr 3 form))
1067 (setq-default ,(nth 1 form) ,(nth 2 form))))
1068 ;; `defcustom' is now macroexpanded to
1069 ;; `custom-declare-variable' with a quoted value arg.
1070 ((and (eq (car form) 'custom-declare-variable)
1071 (default-boundp (eval (nth 1 form) lexical-binding)))
1072 ;; Force variable to be bound, using :set function if specified.
1073 (let ((setfunc (memq :set form)))
1075 (setq setfunc (car-safe (cdr-safe setfunc)))
1076 (or (functionp setfunc) (setq setfunc nil)))
1077 (funcall (or setfunc 'set-default)
1078 (eval (nth 1 form) lexical-binding)
1079 ;; The second arg is an expression that evaluates to
1080 ;; an expression. The second evaluation is the one
1081 ;; normally performed not by normal execution but by
1082 ;; custom-initialize-set (for example), which does not
1083 ;; use lexical-binding.
1084 (eval (eval (nth 2 form) lexical-binding))))
1086 ;; `defface' is macroexpanded to `custom-declare-face'.
1087 ((eq (car form) 'custom-declare-face)
1089 (let ((face-symbol (eval (nth 1 form) lexical-binding)))
1090 (setq face-new-frame-defaults
1091 (assq-delete-all face-symbol face-new-frame-defaults))
1092 (put face-symbol 'face-defface-spec nil)
1093 (put face-symbol 'face-override-spec nil))
1095 ((eq (car form) 'progn)
1096 (cons 'progn (mapcar #'elisp--eval-defun-1 (cdr form))))
1099 (defun elisp--eval-defun ()
1100 "Evaluate defun that point is in or before.
1101 The value is displayed in the echo area.
1102 If the current defun is actually a call to `defvar',
1103 then reset the variable using the initial value expression
1104 even if the variable already has some other value.
1105 \(Normally `defvar' does not change the variable's value
1106 if it already has a value.\)
1108 Return the result of evaluation."
1109 ;; FIXME: the print-length/level bindings should only be applied while
1110 ;; printing, not while evaluating.
1111 (let ((debug-on-error eval-expression-debug-on-error)
1112 (print-length eval-expression-print-length)
1113 (print-level eval-expression-print-level))
1115 ;; Arrange for eval-region to "read" the (possibly) altered form.
1116 ;; eval-region handles recording which file defines a function or
1118 (let ((standard-output t)
1120 ;; Read the form from the buffer, and record where it ends.
1123 (beginning-of-defun)
1125 (setq form (read (current-buffer)))
1127 ;; Alter the form if necessary.
1128 (let ((form (eval-sexp-add-defvars
1129 (elisp--eval-defun-1 (macroexpand form)))))
1130 (eval-region beg end standard-output
1132 ;; Skipping to the end of the specified region
1133 ;; will make eval-region return.
1136 (let ((str (eval-expression-print-format (car values))))
1137 (if str (princ str)))
1138 ;; The result of evaluation has been put onto VALUES. So return it.
1141 (defun eval-defun (edebug-it)
1142 "Evaluate the top-level form containing point, or after point.
1144 If the current defun is actually a call to `defvar' or `defcustom',
1145 evaluating it this way resets the variable using its initial value
1146 expression (using the defcustom's :set function if there is one), even
1147 if the variable already has some other value. \(Normally `defvar' and
1148 `defcustom' do not alter the value if there already is one.) In an
1149 analogous way, evaluating a `defface' overrides any customizations of
1150 the face, so that it becomes defined exactly as the `defface' expression
1153 If `eval-expression-debug-on-error' is non-nil, which is the default,
1154 this command arranges for all errors to enter the debugger.
1156 With a prefix argument, instrument the code for Edebug.
1158 If acting on a `defun' for FUNCTION, and the function was
1159 instrumented, `Edebug: FUNCTION' is printed in the echo area. If not
1160 instrumented, just FUNCTION is printed.
1162 If not acting on a `defun', the result of evaluation is displayed in
1163 the echo area. This display is controlled by the variables
1164 `eval-expression-print-length' and `eval-expression-print-level',
1169 (eval-defun (not edebug-all-defs)))
1171 (if (null eval-expression-debug-on-error)
1173 (let (new-value value)
1174 (let ((debug-on-error elisp--eval-last-sexp-fake-value))
1175 (setq value (elisp--eval-defun))
1176 (setq new-value debug-on-error))
1177 (unless (eq elisp--eval-last-sexp-fake-value new-value)
1178 (setq debug-on-error new-value))
1183 (defvar elisp--eldoc-last-data (make-vector 3 nil)
1184 "Bookkeeping; elements are as follows:
1185 0 - contains the last symbol read from the buffer.
1186 1 - contains the string last displayed in the echo area for variables,
1187 or argument string for functions.
1188 2 - 'function if function args, 'variable if variable documentation.")
1190 (defun elisp-eldoc-documentation-function ()
1191 "`eldoc-documentation-function' (which see) for Emacs Lisp."
1192 (let ((current-symbol (elisp--current-symbol))
1193 (current-fnsym (elisp--fnsym-in-current-sexp)))
1194 (cond ((null current-fnsym)
1196 ((eq current-symbol (car current-fnsym))
1197 (or (apply #'elisp-get-fnsym-args-string current-fnsym)
1198 (elisp-get-var-docstring current-symbol)))
1200 (or (elisp-get-var-docstring current-symbol)
1201 (apply #'elisp-get-fnsym-args-string current-fnsym))))))
1203 (defun elisp-get-fnsym-args-string (sym &optional index prefix)
1204 "Return a string containing the parameter list of the function SYM.
1205 If SYM is a subr and no arglist is obtainable from the docstring
1206 or elsewhere, return a 1-line docstring."
1209 ((not (and sym (symbolp sym) (fboundp sym))) nil)
1210 ((and (eq sym (aref elisp--eldoc-last-data 0))
1211 (eq 'function (aref elisp--eldoc-last-data 2)))
1212 (aref elisp--eldoc-last-data 1))
1214 (let* ((advertised (gethash (indirect-function sym)
1215 advertised-signature-table t))
1219 ((listp advertised) advertised)
1220 ((setq doc (help-split-fundoc
1221 (condition-case nil (documentation sym t)
1222 (invalid-function nil))
1225 (t (help-function-arglist sym)))))
1226 ;; Stringify, and store before highlighting, downcasing, etc.
1227 (elisp--last-data-store sym (elisp-function-argstring args)
1229 ;; Highlight, truncate.
1231 (elisp--highlight-function-argument
1234 (concat (propertize (symbol-name sym) 'face
1236 'font-lock-function-name-face
1237 'font-lock-keyword-face))
1240 (defun elisp--highlight-function-argument (sym args index prefix)
1241 "Highlight argument INDEX in ARGS list for function SYM.
1242 In the absence of INDEX, just call `eldoc-docstring-format-sym-doc'."
1243 ;; FIXME: This should probably work on the list representation of `args'
1244 ;; rather than its string representation.
1245 ;; FIXME: This function is much too long, we need to split it up!
1248 (argument-face 'eldoc-highlight-function-argument)
1249 (args-lst (mapcar (lambda (x)
1250 (replace-regexp-in-string
1251 "\\`[(]\\|[)]\\'" "" x))
1252 (split-string args))))
1253 ;; Find the current argument in the argument string. We need to
1254 ;; handle `&rest' and informal `...' properly.
1256 ;; FIXME: What to do with optional arguments, like in
1257 ;; (defun NAME ARGLIST [DOCSTRING] BODY...) case?
1258 ;; The problem is there is no robust way to determine if
1259 ;; the current argument is indeed a docstring.
1261 ;; When `&key' is used finding position based on `index'
1262 ;; would be wrong, so find the arg at point and determine
1263 ;; position in ARGS based on this current arg.
1264 (when (string-match "&key" args)
1265 (let* (case-fold-search
1267 (sym-name (symbol-name sym))
1268 (cur-w (current-word))
1269 (args-lst-ak (cdr (member "&key" args-lst)))
1270 (limit (save-excursion
1271 (when (re-search-backward sym-name nil t)
1273 (cur-a (if (and cur-w (string-match ":\\([^ ()]*\\)" cur-w))
1277 (when (re-search-backward ":\\([^()\n]*\\)" limit t)
1278 (setq split (split-string (match-string 1) " " t))
1281 (setq key-have-value t))))))))
1282 ;; If `cur-a' is not one of `args-lst-ak'
1283 ;; assume user is entering an unknown key
1284 ;; referenced in last position in signature.
1285 (other-key-arg (and (stringp cur-a)
1287 (not (member (upcase cur-a) args-lst-ak))
1288 (upcase (car (last args-lst-ak))))))
1289 (unless (string= cur-w sym-name)
1290 ;; The last keyword have already a value
1291 ;; i.e :foo a b and cursor is at b.
1292 ;; If signature have also `&rest'
1293 ;; (assume it is after the `&key' section)
1294 ;; go to the arg after `&rest'.
1295 (if (and key-have-value
1297 (not (re-search-forward ":.*" (point-at-eol) t)))
1298 (string-match "&rest \\([^ ()]*\\)" args))
1299 (setq index nil ; Skip next block based on positional args.
1300 start (match-beginning 1)
1302 ;; If `cur-a' is nil probably cursor is on a positional arg
1303 ;; before `&key', in this case, exit this block and determine
1304 ;; position with `index'.
1305 (when (and cur-a ; A keyword arg (dot removed) or nil.
1307 (concat "\\_<" (upcase cur-a) "\\_>") args)
1309 (concat "\\_<" other-key-arg "\\_>") args)))
1310 (setq index nil ; Skip next block based on positional args.
1311 start (match-beginning 0)
1312 end (match-end 0)))))))
1313 ;; Handle now positional arguments.
1314 (while (and index (>= index 1))
1315 (if (string-match "[^ ()]+" args end)
1317 (setq start (match-beginning 0)
1319 (let ((argument (match-string 0 args)))
1320 (cond ((string= argument "&rest")
1321 ;; All the rest arguments are the same.
1323 ((string= argument "&optional")) ; Skip.
1324 ((string= argument "&allow-other-keys")) ; Skip.
1325 ;; Back to index 0 in ARG1 ARG2 ARG2 ARG3 etc...
1327 ((or (and (string-match-p "\\.\\.\\.\\'" argument)
1328 (string= argument (car (last args-lst))))
1329 (and (string-match-p "\\.\\.\\.\\'"
1330 (substring args 1 (1- (length args))))
1331 (= (length (remove "..." args-lst)) 2)
1332 (> index 1) (eq (logand index 1) 1)))
1335 (setq index (1- index))))))
1336 (setq end (length args)
1338 argument-face 'font-lock-warning-face
1342 (setq doc (copy-sequence args))
1343 (add-text-properties start end (list 'face argument-face) doc))
1344 (setq doc (eldoc-docstring-format-sym-doc prefix doc))
1347 ;; Return a string containing a brief (one-line) documentation string for
1349 (defun elisp-get-var-docstring (sym)
1350 (cond ((not sym) nil)
1351 ((and (eq sym (aref elisp--eldoc-last-data 0))
1352 (eq 'variable (aref elisp--eldoc-last-data 2)))
1353 (aref elisp--eldoc-last-data 1))
1355 (let ((doc (documentation-property sym 'variable-documentation t)))
1357 (let ((doc (eldoc-docstring-format-sym-doc
1358 sym (elisp--docstring-first-line doc)
1359 'font-lock-variable-name-face)))
1360 (elisp--last-data-store sym doc 'variable)))))))
1362 (defun elisp--last-data-store (symbol doc type)
1363 (aset elisp--eldoc-last-data 0 symbol)
1364 (aset elisp--eldoc-last-data 1 doc)
1365 (aset elisp--eldoc-last-data 2 type)
1368 ;; Note that any leading `*' in the docstring (which indicates the variable
1369 ;; is a user option) is removed.
1370 (defun elisp--docstring-first-line (doc)
1372 (substitute-command-keys
1374 ;; Don't use "^" in the regexp below since it may match
1375 ;; anywhere in the doc-string.
1376 (let ((start (if (string-match "\\`\\*" doc) (match-end 0) 0)))
1377 (cond ((string-match "\n" doc)
1378 (substring doc start (match-beginning 0)))
1380 (t (substring doc start))))))))
1382 ;; Return a list of current function name and argument index.
1383 (defun elisp--fnsym-in-current-sexp ()
1385 (let ((argument-index (1- (elisp--beginning-of-sexp))))
1386 ;; If we are at the beginning of function name, this will be -1.
1387 (when (< argument-index 0)
1388 (setq argument-index 0))
1389 ;; Don't do anything if current word is inside a string.
1390 (if (= (or (char-after (1- (point))) 0) ?\")
1392 (list (elisp--current-symbol) argument-index)))))
1394 ;; Move to the beginning of current sexp. Return the number of nested
1395 ;; sexp the point was over or after.
1396 (defun elisp--beginning-of-sexp ()
1397 (let ((parse-sexp-ignore-comments t)
1398 (num-skipped-sexps 0))
1401 ;; First account for the case the point is directly over a
1402 ;; beginning of a nested sexp.
1408 (setq num-skipped-sexps 1)))
1414 (setq num-skipped-sexps (1+ num-skipped-sexps))))))
1418 ;; returns nil unless current word is an interned symbol.
1419 (defun elisp--current-symbol ()
1420 (let ((c (char-after (point))))
1422 (memq (char-syntax c) '(?w ?_))
1423 (intern-soft (current-word)))))
1425 (defun elisp-function-argstring (arglist)
1426 "Return ARGLIST as a string enclosed by ().
1427 ARGLIST is either a string, or a list of strings or symbols."
1428 (let ((str (cond ((stringp arglist) arglist)
1429 ((not (listp arglist)) nil)
1430 (t (help--make-usage-docstring 'toto arglist)))))
1431 (if (and str (string-match "\\`([^ )]+ ?" str))
1432 (replace-match "(" t t str)
1435 (provide 'elisp-mode)
1436 ;;; elisp-mode.el ends here