Fixes: debbugs:19133
[emacs.git] / lisp / progmodes / elisp-mode.el
blobba70f903b4ba901cd13a1fd1e371020c9ff4a805
1 ;;; elisp-mode.el --- Emacs Lisp mode -*- lexical-binding:t -*-
3 ;; Copyright (C) 1985-1986, 1999-2014 Free Software Foundation, Inc.
5 ;; Maintainer: emacs-devel@gnu.org
6 ;; Keywords: lisp, languages
7 ;; Package: emacs
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/>.
24 ;;; Commentary:
26 ;; The major mode for editing Emacs Lisp code.
27 ;; This mode is documented in the Emacs manual.
29 ;;; Code:
31 (require 'lisp-mode)
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))
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'.")
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"
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.")
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")))
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))
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)))))
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)
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.
228 \\{emacs-lisp-mode-map}"
229 :group 'lisp
230 (lisp-mode-variables nil nil 'elisp)
231 (setq imenu-case-fold-search nil)
232 (setq-local eldoc-documentation-function
233 #'elisp-eldoc-documentation-function)
234 (add-hook 'completion-at-point-functions
235 #'elisp-completion-at-point nil 'local))
237 ;;; Completion at point for Elisp
239 (defun elisp--local-variables-1 (vars sexp)
240 "Return the vars locally bound around the witness, or nil if not found."
241 (let (res)
242 (while
243 (unless
244 (setq res
245 (pcase sexp
246 (`(,(or `let `let*) ,bindings)
247 (let ((vars vars))
248 (when (eq 'let* (car sexp))
249 (dolist (binding (cdr (reverse bindings)))
250 (push (or (car-safe binding) binding) vars)))
251 (elisp--local-variables-1
252 vars (car (cdr-safe (car (last bindings)))))))
253 (`(,(or `let `let*) ,bindings . ,body)
254 (let ((vars vars))
255 (dolist (binding bindings)
256 (push (or (car-safe binding) binding) vars))
257 (elisp--local-variables-1 vars (car (last body)))))
258 (`(lambda ,_args)
259 ;; FIXME: Look for the witness inside `args'.
260 (setq sexp nil))
261 (`(lambda ,args . ,body)
262 (elisp--local-variables-1
263 (append (remq '&optional (remq '&rest args)) vars)
264 (car (last body))))
265 (`(condition-case ,_ ,e) (elisp--local-variables-1 vars e))
266 (`(condition-case ,v ,_ . ,catches)
267 (elisp--local-variables-1
268 (cons v vars) (cdr (car (last catches)))))
269 (`(quote . ,_)
270 ;; FIXME: Look for the witness inside sexp.
271 (setq sexp nil))
272 ;; FIXME: Handle `cond'.
273 (`(,_ . ,_)
274 (elisp--local-variables-1 vars (car (last sexp))))
275 (`elisp--witness--lisp (or vars '(nil)))
276 (_ nil)))
277 ;; We didn't find the witness in the last element so we try to
278 ;; backtrack to the last-but-one.
279 (setq sexp (ignore-errors (butlast sexp)))))
280 res))
282 (defun elisp--local-variables ()
283 "Return a list of locally let-bound variables at point."
284 (save-excursion
285 (skip-syntax-backward "w_")
286 (let* ((ppss (syntax-ppss))
287 (txt (buffer-substring-no-properties (or (car (nth 9 ppss)) (point))
288 (or (nth 8 ppss) (point))))
289 (closer ()))
290 (dolist (p (nth 9 ppss))
291 (push (cdr (syntax-after p)) closer))
292 (setq closer (apply #'string closer))
293 (let* ((sexp (condition-case nil
294 (car (read-from-string
295 (concat txt "elisp--witness--lisp" closer)))
296 ((invalid-read-syntax end-of-file) nil)))
297 (macroexpand-advice (lambda (expander form &rest args)
298 (condition-case nil
299 (apply expander form args)
300 (error form))))
301 (sexp
302 (unwind-protect
303 (progn
304 (advice-add 'macroexpand :around macroexpand-advice)
305 (macroexpand-all sexp))
306 (advice-remove 'macroexpand macroexpand-advice)))
307 (vars (elisp--local-variables-1 nil sexp)))
308 (delq nil
309 (mapcar (lambda (var)
310 (and (symbolp var)
311 (not (string-match (symbol-name var) "\\`[&_]"))
312 ;; Eliminate uninterned vars.
313 (intern-soft var)
314 var))
315 vars))))))
317 (defvar elisp--local-variables-completion-table
318 ;; Use `defvar' rather than `defconst' since defconst would purecopy this
319 ;; value, which would doubly fail: it would fail because purecopy can't
320 ;; handle the recursive bytecode object, and it would fail because it would
321 ;; move `lastpos' and `lastvars' to pure space where they'd be immutable!
322 (let ((lastpos nil) (lastvars nil))
323 (letrec ((hookfun (lambda ()
324 (setq lastpos nil)
325 (remove-hook 'post-command-hook hookfun))))
326 (completion-table-dynamic
327 (lambda (_string)
328 (save-excursion
329 (skip-syntax-backward "_w")
330 (let ((newpos (cons (point) (current-buffer))))
331 (unless (equal lastpos newpos)
332 (add-hook 'post-command-hook hookfun)
333 (setq lastpos newpos)
334 (setq lastvars
335 (mapcar #'symbol-name (elisp--local-variables))))))
336 lastvars)))))
338 (defun elisp--expect-function-p (pos)
339 "Return non-nil if the symbol at point is expected to be a function."
341 (and (eq (char-before pos) ?')
342 (eq (char-before (1- pos)) ?#))
343 (save-excursion
344 (let ((parent (nth 1 (syntax-ppss pos))))
345 (when parent
346 (goto-char parent)
347 (and
348 (looking-at (concat "(\\(cl-\\)?"
349 (regexp-opt '("declare-function"
350 "function" "defadvice"
351 "callf" "callf2"
352 "defsetf"))
353 "[ \t\r\n]+"))
354 (eq (match-end 0) pos)))))))
356 (defun elisp--form-quoted-p (pos)
357 "Return non-nil if the form at POS is not evaluated.
358 It can be quoted, or be inside a quoted form."
359 ;; FIXME: Do some macro expansion maybe.
360 (save-excursion
361 (let ((state (syntax-ppss pos)))
362 (or (nth 8 state) ; Code inside strings usually isn't evaluated.
363 ;; FIXME: The 9th element is undocumented.
364 (let ((nesting (cons (point) (reverse (nth 9 state))))
365 res)
366 (while (and nesting (not res))
367 (goto-char (pop nesting))
368 (cond
369 ((or (eq (char-after) ?\[)
370 (progn
371 (skip-chars-backward " ")
372 (memq (char-before) '(?' ?`))))
373 (setq res t))
374 ((eq (char-before) ?,)
375 (setq nesting nil))))
376 res)))))
378 ;; FIXME: Support for Company brings in features which straddle eldoc.
379 ;; We should consolidate this, so that major modes can provide all that
380 ;; data all at once:
381 ;; - a function to extract "the reference at point" (may be more complex
382 ;; than a mere string, to distinguish various namespaces).
383 ;; - a function to jump to such a reference.
384 ;; - a function to show the signature/interface of such a reference.
385 ;; - a function to build a help-buffer about that reference.
386 ;; FIXME: Those functions should also be used by the normal completion code in
387 ;; the *Completions* buffer.
389 (defun elisp--company-doc-buffer (str)
390 (let ((symbol (intern-soft str)))
391 ;; FIXME: we really don't want to "display-buffer and then undo it".
392 (save-window-excursion
393 ;; Make sure we don't display it in another frame, otherwise
394 ;; save-window-excursion won't be able to undo it.
395 (let ((display-buffer-overriding-action
396 '(nil . ((inhibit-switch-frame . t)))))
397 (ignore-errors
398 (cond
399 ((fboundp symbol) (describe-function symbol))
400 ((boundp symbol) (describe-variable symbol))
401 ((featurep symbol) (describe-package symbol))
402 ((facep symbol) (describe-face symbol))
403 (t (signal 'user-error nil)))
404 (help-buffer))))))
406 (defun elisp--company-doc-string (str)
407 (let* ((symbol (intern-soft str))
408 (doc (if (fboundp symbol)
409 (documentation symbol t)
410 (documentation-property symbol 'variable-documentation t))))
411 (and (stringp doc)
412 (string-match ".*$" doc)
413 (match-string 0 doc))))
415 (declare-function find-library-name "find-func" (library))
417 (defun elisp--company-location (str)
418 (let ((sym (intern-soft str)))
419 (cond
420 ((fboundp sym) (find-definition-noselect sym nil))
421 ((boundp sym) (find-definition-noselect sym 'defvar))
422 ((featurep sym)
423 (require 'find-func)
424 (cons (find-file-noselect (find-library-name
425 (symbol-name sym)))
427 ((facep sym) (find-definition-noselect sym 'defface)))))
429 (defun elisp-completion-at-point ()
430 "Function used for `completion-at-point-functions' in `emacs-lisp-mode'."
431 (with-syntax-table emacs-lisp-mode-syntax-table
432 (let* ((pos (point))
433 (beg (condition-case nil
434 (save-excursion
435 (backward-sexp 1)
436 (skip-syntax-forward "'")
437 (point))
438 (scan-error pos)))
439 (end
440 (unless (or (eq beg (point-max))
441 (member (char-syntax (char-after beg))
442 '(?\s ?\" ?\( ?\))))
443 (condition-case nil
444 (save-excursion
445 (goto-char beg)
446 (forward-sexp 1)
447 (skip-chars-backward "'")
448 (when (>= (point) pos)
449 (point)))
450 (scan-error pos))))
451 ;; t if in function position.
452 (funpos (eq (char-before beg) ?\()))
453 (when (and end (or (not (nth 8 (syntax-ppss)))
454 (eq (char-before beg) ?`)))
455 (let ((table-etc
456 (if (not funpos)
457 ;; FIXME: We could look at the first element of the list and
458 ;; use it to provide a more specific completion table in some
459 ;; cases. E.g. filter out keywords that are not understood by
460 ;; the macro/function being called.
461 (cond
462 ((elisp--expect-function-p beg)
463 (list nil obarray
464 :predicate #'fboundp
465 :company-doc-buffer #'elisp--company-doc-buffer
466 :company-docsig #'elisp--company-doc-string
467 :company-location #'elisp--company-location))
468 ((elisp--form-quoted-p beg)
469 (list nil obarray
470 ;; Don't include all symbols
471 ;; (bug#16646).
472 :predicate (lambda (sym)
473 (or (boundp sym)
474 (fboundp sym)
475 (symbol-plist sym)))
476 :annotation-function
477 (lambda (str) (if (fboundp (intern-soft str)) " <f>"))
478 :company-doc-buffer #'elisp--company-doc-buffer
479 :company-docsig #'elisp--company-doc-string
480 :company-location #'elisp--company-location))
482 (list nil (completion-table-merge
483 elisp--local-variables-completion-table
484 (apply-partially #'completion-table-with-predicate
485 obarray
486 #'boundp
487 'strict))
488 :company-doc-buffer #'elisp--company-doc-buffer
489 :company-docsig #'elisp--company-doc-string
490 :company-location #'elisp--company-location)))
491 ;; Looks like a funcall position. Let's double check.
492 (save-excursion
493 (goto-char (1- beg))
494 (let ((parent
495 (condition-case nil
496 (progn (up-list -1) (forward-char 1)
497 (let ((c (char-after)))
498 (if (eq c ?\() ?\(
499 (if (memq (char-syntax c) '(?w ?_))
500 (read (current-buffer))))))
501 (error nil))))
502 (pcase parent
503 ;; FIXME: Rather than hardcode special cases here,
504 ;; we should use something like a symbol-property.
505 (`declare
506 (list t (mapcar (lambda (x) (symbol-name (car x)))
507 (delete-dups
508 ;; FIXME: We should include some
509 ;; docstring with each entry.
510 (append
511 macro-declarations-alist
512 defun-declarations-alist)))))
513 ((and (or `condition-case `condition-case-unless-debug)
514 (guard (save-excursion
515 (ignore-errors
516 (forward-sexp 2)
517 (< (point) beg)))))
518 (list t obarray
519 :predicate (lambda (sym) (get sym 'error-conditions))))
520 ((and ?\(
521 (guard (save-excursion
522 (goto-char (1- beg))
523 (up-list -1)
524 (forward-symbol -1)
525 (looking-at "\\_<let\\*?\\_>"))))
526 (list t obarray
527 :predicate #'boundp
528 :company-doc-buffer #'elisp--company-doc-buffer
529 :company-docsig #'elisp--company-doc-string
530 :company-location #'elisp--company-location))
531 (_ (list nil obarray
532 :predicate #'fboundp
533 :company-doc-buffer #'elisp--company-doc-buffer
534 :company-docsig #'elisp--company-doc-string
535 :company-location #'elisp--company-location
536 ))))))))
537 (nconc (list beg end)
538 (if (null (car table-etc))
539 (cdr table-etc)
540 (cons
541 (if (memq (char-syntax (or (char-after end) ?\s))
542 '(?\s ?>))
543 (cadr table-etc)
544 (apply-partially 'completion-table-with-terminator
545 " " (cadr table-etc)))
546 (cddr table-etc)))))))))
548 (define-obsolete-function-alias
549 'lisp-completion-at-point 'elisp-completion-at-point "25.1")
551 ;;; Elisp Interaction mode
553 (defvar lisp-interaction-mode-map
554 (let ((map (make-sparse-keymap))
555 (menu-map (make-sparse-keymap "Lisp-Interaction")))
556 (set-keymap-parent map lisp-mode-shared-map)
557 (define-key map "\e\C-x" 'eval-defun)
558 (define-key map "\e\C-q" 'indent-pp-sexp)
559 (define-key map "\e\t" 'completion-at-point)
560 (define-key map "\n" 'eval-print-last-sexp)
561 (bindings--define-key map [menu-bar lisp-interaction]
562 (cons "Lisp-Interaction" menu-map))
563 (bindings--define-key menu-map [eval-defun]
564 '(menu-item "Evaluate Defun" eval-defun
565 :help "Evaluate the top-level form containing point, or after point"))
566 (bindings--define-key menu-map [eval-print-last-sexp]
567 '(menu-item "Evaluate and Print" eval-print-last-sexp
568 :help "Evaluate sexp before point; print value into current buffer"))
569 (bindings--define-key menu-map [edebug-defun-lisp-interaction]
570 '(menu-item "Instrument Function for Debugging" edebug-defun
571 :help "Evaluate the top level form point is in, stepping through with Edebug"
572 :keys "C-u C-M-x"))
573 (bindings--define-key menu-map [indent-pp-sexp]
574 '(menu-item "Indent or Pretty-Print" indent-pp-sexp
575 :help "Indent each line of the list starting just after point, or prettyprint it"))
576 (bindings--define-key menu-map [complete-symbol]
577 '(menu-item "Complete Lisp Symbol" completion-at-point
578 :help "Perform completion on Lisp symbol preceding point"))
579 map)
580 "Keymap for Lisp Interaction mode.
581 All commands in `lisp-mode-shared-map' are inherited by this map.")
583 (define-derived-mode lisp-interaction-mode emacs-lisp-mode "Lisp Interaction"
584 "Major mode for typing and evaluating Lisp forms.
585 Like Lisp mode except that \\[eval-print-last-sexp] evals the Lisp expression
586 before point, and prints its value into the buffer, advancing point.
587 Note that printing is controlled by `eval-expression-print-length'
588 and `eval-expression-print-level'.
590 Commands:
591 Delete converts tabs to spaces as it moves back.
592 Paragraphs are separated only by blank lines.
593 Semicolons start comments.
595 \\{lisp-interaction-mode-map}"
596 :abbrev-table nil)
598 ;;; Emacs Lisp Byte-Code mode
600 (eval-and-compile
601 (defconst emacs-list-byte-code-comment-re
602 (concat "\\(#\\)@\\([0-9]+\\) "
603 ;; Make sure it's a docstring and not a lazy-loaded byte-code.
604 "\\(?:[^(]\\|([^\"]\\)")))
606 (defun elisp--byte-code-comment (end &optional _point)
607 "Try to syntactically mark the #@NNN ....^_ docstrings in byte-code files."
608 (let ((ppss (syntax-ppss)))
609 (when (and (nth 4 ppss)
610 (eq (char-after (nth 8 ppss)) ?#))
611 (let* ((n (save-excursion
612 (goto-char (nth 8 ppss))
613 (when (looking-at emacs-list-byte-code-comment-re)
614 (string-to-number (match-string 2)))))
615 ;; `maxdiff' tries to make sure the loop below terminates.
616 (maxdiff n))
617 (when n
618 (let* ((bchar (match-end 2))
619 (b (position-bytes bchar)))
620 (goto-char (+ b n))
621 (while (let ((diff (- (position-bytes (point)) b n)))
622 (unless (zerop diff)
623 (when (> diff maxdiff) (setq diff maxdiff))
624 (forward-char (- diff))
625 (setq maxdiff (if (> diff 0) diff
626 (max (1- maxdiff) 1)))
627 t))))
628 (if (<= (point) end)
629 (put-text-property (1- (point)) (point)
630 'syntax-table
631 (string-to-syntax "> b"))
632 (goto-char end)))))))
634 (defun elisp-byte-code-syntax-propertize (start end)
635 (elisp--byte-code-comment end (point))
636 (funcall
637 (syntax-propertize-rules
638 (emacs-list-byte-code-comment-re
639 (1 (prog1 "< b" (elisp--byte-code-comment end (point))))))
640 start end))
642 ;;;###autoload
643 (add-to-list 'auto-mode-alist '("\\.elc\\'" . elisp-byte-code-mode))
644 ;;;###autoload
645 (define-derived-mode elisp-byte-code-mode emacs-lisp-mode
646 "Elisp-Byte-Code"
647 "Major mode for *.elc files."
648 ;; TODO: Add way to disassemble byte-code under point.
649 (setq-local open-paren-in-column-0-is-defun-start nil)
650 (setq-local syntax-propertize-function
651 #'elisp-byte-code-syntax-propertize))
654 ;;; Globally accessible functionality
656 (defun eval-print-last-sexp (&optional eval-last-sexp-arg-internal)
657 "Evaluate sexp before point; print value into current buffer.
659 Normally, this function truncates long output according to the value
660 of the variables `eval-expression-print-length' and
661 `eval-expression-print-level'. With a prefix argument of zero,
662 however, there is no such truncation. Such a prefix argument
663 also causes integers to be printed in several additional formats
664 \(octal, hexadecimal, and character).
666 If `eval-expression-debug-on-error' is non-nil, which is the default,
667 this command arranges for all errors to enter the debugger."
668 (interactive "P")
669 (let ((standard-output (current-buffer)))
670 (terpri)
671 (eval-last-sexp (or eval-last-sexp-arg-internal t))
672 (terpri)))
675 (defun last-sexp-setup-props (beg end value alt1 alt2)
676 "Set up text properties for the output of `elisp--eval-last-sexp'.
677 BEG and END are the start and end of the output in current-buffer.
678 VALUE is the Lisp value printed, ALT1 and ALT2 are strings for the
679 alternative printed representations that can be displayed."
680 (let ((map (make-sparse-keymap)))
681 (define-key map "\C-m" 'elisp-last-sexp-toggle-display)
682 (define-key map [down-mouse-2] 'mouse-set-point)
683 (define-key map [mouse-2] 'elisp-last-sexp-toggle-display)
684 (add-text-properties
685 beg end
686 `(printed-value (,value ,alt1 ,alt2)
687 mouse-face highlight
688 keymap ,map
689 help-echo "RET, mouse-2: toggle abbreviated display"
690 rear-nonsticky (mouse-face keymap help-echo
691 printed-value)))))
694 (defun elisp-last-sexp-toggle-display (&optional _arg)
695 "Toggle between abbreviated and unabbreviated printed representations."
696 (interactive "P")
697 (save-restriction
698 (widen)
699 (let ((value (get-text-property (point) 'printed-value)))
700 (when value
701 (let ((beg (or (previous-single-property-change (min (point-max) (1+ (point)))
702 'printed-value)
703 (point)))
704 (end (or (next-single-char-property-change (point) 'printed-value) (point)))
705 (standard-output (current-buffer))
706 (point (point)))
707 (delete-region beg end)
708 (insert (nth 1 value))
709 (or (= beg point)
710 (setq point (1- (point))))
711 (last-sexp-setup-props beg (point)
712 (nth 0 value)
713 (nth 2 value)
714 (nth 1 value))
715 (goto-char (min (point-max) point)))))))
717 (defun prin1-char (char) ;FIXME: Move it, e.g. to simple.el.
718 "Return a string representing CHAR as a character rather than as an integer.
719 If CHAR is not a character, return nil."
720 (and (integerp char)
721 (eventp char)
722 (let ((c (event-basic-type char))
723 (mods (event-modifiers char))
724 string)
725 ;; Prevent ?A from turning into ?\S-a.
726 (if (and (memq 'shift mods)
727 (zerop (logand char ?\S-\^@))
728 (not (let ((case-fold-search nil))
729 (char-equal c (upcase c)))))
730 (setq c (upcase c) mods nil))
731 ;; What string are we considering using?
732 (condition-case nil
733 (setq string
734 (concat
736 (mapconcat
737 (lambda (modif)
738 (cond ((eq modif 'super) "\\s-")
739 (t (string ?\\ (upcase (aref (symbol-name modif) 0)) ?-))))
740 mods "")
741 (cond
742 ((memq c '(?\; ?\( ?\) ?\{ ?\} ?\[ ?\] ?\" ?\' ?\\)) (string ?\\ c))
743 ((eq c 127) "\\C-?")
745 (string c)))))
746 (error nil))
747 ;; Verify the string reads a CHAR, not to some other character.
748 ;; If it doesn't, return nil instead.
749 (and string
750 (= (car (read-from-string string)) char)
751 string))))
753 (defun elisp--preceding-sexp ()
754 "Return sexp before the point."
755 (let ((opoint (point))
756 ignore-quotes
757 expr)
758 (save-excursion
759 (with-syntax-table emacs-lisp-mode-syntax-table
760 ;; If this sexp appears to be enclosed in `...'
761 ;; then ignore the surrounding quotes.
762 (setq ignore-quotes
763 (or (eq (following-char) ?\')
764 (eq (preceding-char) ?\')))
765 (forward-sexp -1)
766 ;; If we were after `?\e' (or similar case),
767 ;; use the whole thing, not just the `e'.
768 (when (eq (preceding-char) ?\\)
769 (forward-char -1)
770 (when (eq (preceding-char) ??)
771 (forward-char -1)))
773 ;; Skip over hash table read syntax.
774 (and (> (point) (1+ (point-min)))
775 (looking-back "#s" (- (point) 2))
776 (forward-char -2))
778 ;; Skip over `#N='s.
779 (when (eq (preceding-char) ?=)
780 (let (labeled-p)
781 (save-excursion
782 (skip-chars-backward "0-9#=")
783 (setq labeled-p (looking-at "\\(#[0-9]+=\\)+")))
784 (when labeled-p
785 (forward-sexp -1))))
787 (save-restriction
788 (if (and ignore-quotes (eq (following-char) ?`))
789 ;; vladimir@cs.ualberta.ca 30-Jul-1997: Skip ` in `variable' so
790 ;; that the value is returned, not the name.
791 (forward-char))
792 (when (looking-at ",@?") (goto-char (match-end 0)))
793 (narrow-to-region (point-min) opoint)
794 (setq expr (read (current-buffer)))
795 ;; If it's an (interactive ...) form, it's more useful to show how an
796 ;; interactive call would use it.
797 ;; FIXME: Is it really the right place for this?
798 (when (eq (car-safe expr) 'interactive)
799 (setq expr
800 `(call-interactively
801 (lambda (&rest args) ,expr args))))
802 expr)))))
803 (define-obsolete-function-alias 'preceding-sexp 'elisp--preceding-sexp "25.1")
805 (defun elisp--eval-last-sexp (eval-last-sexp-arg-internal)
806 "Evaluate sexp before point; print value in the echo area.
807 With argument, print output into current buffer.
808 With a zero prefix arg, print output with no limit on the length
809 and level of lists, and include additional formats for integers
810 \(octal, hexadecimal, and character)."
811 (let ((standard-output (if eval-last-sexp-arg-internal (current-buffer) t)))
812 ;; Setup the lexical environment if lexical-binding is enabled.
813 (elisp--eval-last-sexp-print-value
814 (eval (eval-sexp-add-defvars (elisp--preceding-sexp)) lexical-binding)
815 eval-last-sexp-arg-internal)))
818 (defun elisp--eval-last-sexp-print-value (value &optional eval-last-sexp-arg-internal)
819 (let ((unabbreviated (let ((print-length nil) (print-level nil))
820 (prin1-to-string value)))
821 (print-length (and (not (zerop (prefix-numeric-value
822 eval-last-sexp-arg-internal)))
823 eval-expression-print-length))
824 (print-level (and (not (zerop (prefix-numeric-value
825 eval-last-sexp-arg-internal)))
826 eval-expression-print-level))
827 (beg (point))
828 end)
829 (prog1
830 (prin1 value)
831 (let ((str (eval-expression-print-format value)))
832 (if str (princ str)))
833 (setq end (point))
834 (when (and (bufferp standard-output)
835 (or (not (null print-length))
836 (not (null print-level)))
837 (not (string= unabbreviated
838 (buffer-substring-no-properties beg end))))
839 (last-sexp-setup-props beg end value
840 unabbreviated
841 (buffer-substring-no-properties beg end))
842 ))))
845 (defvar elisp--eval-last-sexp-fake-value (make-symbol "t"))
847 (defun eval-sexp-add-defvars (exp &optional pos)
848 "Prepend EXP with all the `defvar's that precede it in the buffer.
849 POS specifies the starting position where EXP was found and defaults to point."
850 (setq exp (macroexpand-all exp)) ;Eager macro-expansion.
851 (if (not lexical-binding)
853 (save-excursion
854 (unless pos (setq pos (point)))
855 (let ((vars ()))
856 (goto-char (point-min))
857 (while (re-search-forward
858 "(def\\(?:var\\|const\\|custom\\)[ \t\n]+\\([^; '()\n\t]+\\)"
859 pos t)
860 (let ((var (intern (match-string 1))))
861 (and (not (special-variable-p var))
862 (save-excursion
863 (zerop (car (syntax-ppss (match-beginning 0)))))
864 (push var vars))))
865 `(progn ,@(mapcar (lambda (v) `(defvar ,v)) vars) ,exp)))))
867 (defun eval-last-sexp (eval-last-sexp-arg-internal)
868 "Evaluate sexp before point; print value in the echo area.
869 Interactively, with prefix argument, print output into current buffer.
871 Normally, this function truncates long output according to the value
872 of the variables `eval-expression-print-length' and
873 `eval-expression-print-level'. With a prefix argument of zero,
874 however, there is no such truncation. Such a prefix argument
875 also causes integers to be printed in several additional formats
876 \(octal, hexadecimal, and character).
878 If `eval-expression-debug-on-error' is non-nil, which is the default,
879 this command arranges for all errors to enter the debugger."
880 (interactive "P")
881 (if (null eval-expression-debug-on-error)
882 (elisp--eval-last-sexp eval-last-sexp-arg-internal)
883 (let ((value
884 (let ((debug-on-error elisp--eval-last-sexp-fake-value))
885 (cons (elisp--eval-last-sexp eval-last-sexp-arg-internal)
886 debug-on-error))))
887 (unless (eq (cdr value) elisp--eval-last-sexp-fake-value)
888 (setq debug-on-error (cdr value)))
889 (car value))))
891 (defun elisp--eval-defun-1 (form)
892 "Treat some expressions specially.
893 Reset the `defvar' and `defcustom' variables to the initial value.
894 \(For `defcustom', use the :set function if there is one.)
895 Reinitialize the face according to the `defface' specification."
896 ;; The code in edebug-defun should be consistent with this, but not
897 ;; the same, since this gets a macroexpanded form.
898 (cond ((not (listp form))
899 form)
900 ((and (eq (car form) 'defvar)
901 (cdr-safe (cdr-safe form))
902 (boundp (cadr form)))
903 ;; Force variable to be re-set.
904 `(progn (defvar ,(nth 1 form) nil ,@(nthcdr 3 form))
905 (setq-default ,(nth 1 form) ,(nth 2 form))))
906 ;; `defcustom' is now macroexpanded to
907 ;; `custom-declare-variable' with a quoted value arg.
908 ((and (eq (car form) 'custom-declare-variable)
909 (default-boundp (eval (nth 1 form) lexical-binding)))
910 ;; Force variable to be bound, using :set function if specified.
911 (let ((setfunc (memq :set form)))
912 (when setfunc
913 (setq setfunc (car-safe (cdr-safe setfunc)))
914 (or (functionp setfunc) (setq setfunc nil)))
915 (funcall (or setfunc 'set-default)
916 (eval (nth 1 form) lexical-binding)
917 ;; The second arg is an expression that evaluates to
918 ;; an expression. The second evaluation is the one
919 ;; normally performed not by normal execution but by
920 ;; custom-initialize-set (for example), which does not
921 ;; use lexical-binding.
922 (eval (eval (nth 2 form) lexical-binding))))
923 form)
924 ;; `defface' is macroexpanded to `custom-declare-face'.
925 ((eq (car form) 'custom-declare-face)
926 ;; Reset the face.
927 (let ((face-symbol (eval (nth 1 form) lexical-binding)))
928 (setq face-new-frame-defaults
929 (assq-delete-all face-symbol face-new-frame-defaults))
930 (put face-symbol 'face-defface-spec nil)
931 (put face-symbol 'face-override-spec nil))
932 form)
933 ((eq (car form) 'progn)
934 (cons 'progn (mapcar #'elisp--eval-defun-1 (cdr form))))
935 (t form)))
937 (defun elisp--eval-defun ()
938 "Evaluate defun that point is in or before.
939 The value is displayed in the echo area.
940 If the current defun is actually a call to `defvar',
941 then reset the variable using the initial value expression
942 even if the variable already has some other value.
943 \(Normally `defvar' does not change the variable's value
944 if it already has a value.\)
946 Return the result of evaluation."
947 ;; FIXME: the print-length/level bindings should only be applied while
948 ;; printing, not while evaluating.
949 (let ((debug-on-error eval-expression-debug-on-error)
950 (print-length eval-expression-print-length)
951 (print-level eval-expression-print-level))
952 (save-excursion
953 ;; Arrange for eval-region to "read" the (possibly) altered form.
954 ;; eval-region handles recording which file defines a function or
955 ;; variable.
956 (let ((standard-output t)
957 beg end form)
958 ;; Read the form from the buffer, and record where it ends.
959 (save-excursion
960 (end-of-defun)
961 (beginning-of-defun)
962 (setq beg (point))
963 (setq form (read (current-buffer)))
964 (setq end (point)))
965 ;; Alter the form if necessary.
966 (let ((form (eval-sexp-add-defvars
967 (elisp--eval-defun-1 (macroexpand form)))))
968 (eval-region beg end standard-output
969 (lambda (_ignore)
970 ;; Skipping to the end of the specified region
971 ;; will make eval-region return.
972 (goto-char end)
973 form))))))
974 (let ((str (eval-expression-print-format (car values))))
975 (if str (princ str)))
976 ;; The result of evaluation has been put onto VALUES. So return it.
977 (car values))
979 (defun eval-defun (edebug-it)
980 "Evaluate the top-level form containing point, or after point.
982 If the current defun is actually a call to `defvar' or `defcustom',
983 evaluating it this way resets the variable using its initial value
984 expression (using the defcustom's :set function if there is one), even
985 if the variable already has some other value. \(Normally `defvar' and
986 `defcustom' do not alter the value if there already is one.) In an
987 analogous way, evaluating a `defface' overrides any customizations of
988 the face, so that it becomes defined exactly as the `defface' expression
989 says.
991 If `eval-expression-debug-on-error' is non-nil, which is the default,
992 this command arranges for all errors to enter the debugger.
994 With a prefix argument, instrument the code for Edebug.
996 If acting on a `defun' for FUNCTION, and the function was
997 instrumented, `Edebug: FUNCTION' is printed in the echo area. If not
998 instrumented, just FUNCTION is printed.
1000 If not acting on a `defun', the result of evaluation is displayed in
1001 the echo area. This display is controlled by the variables
1002 `eval-expression-print-length' and `eval-expression-print-level',
1003 which see."
1004 (interactive "P")
1005 (cond (edebug-it
1006 (require 'edebug)
1007 (eval-defun (not edebug-all-defs)))
1009 (if (null eval-expression-debug-on-error)
1010 (elisp--eval-defun)
1011 (let (new-value value)
1012 (let ((debug-on-error elisp--eval-last-sexp-fake-value))
1013 (setq value (elisp--eval-defun))
1014 (setq new-value debug-on-error))
1015 (unless (eq elisp--eval-last-sexp-fake-value new-value)
1016 (setq debug-on-error new-value))
1017 value)))))
1019 ;;; ElDoc Support
1021 (defvar elisp--eldoc-last-data (make-vector 3 nil)
1022 "Bookkeeping; elements are as follows:
1023 0 - contains the last symbol read from the buffer.
1024 1 - contains the string last displayed in the echo area for variables,
1025 or argument string for functions.
1026 2 - 'function if function args, 'variable if variable documentation.")
1028 (defun elisp-eldoc-documentation-function ()
1029 "`eldoc-documentation-function' (which see) for Emacs Lisp."
1030 (let ((current-symbol (elisp--current-symbol))
1031 (current-fnsym (elisp--fnsym-in-current-sexp)))
1032 (cond ((null current-fnsym)
1033 nil)
1034 ((eq current-symbol (car current-fnsym))
1035 (or (apply #'elisp--get-fnsym-args-string current-fnsym)
1036 (elisp--get-var-docstring current-symbol)))
1038 (or (elisp--get-var-docstring current-symbol)
1039 (apply #'elisp--get-fnsym-args-string current-fnsym))))))
1041 (defun elisp--get-fnsym-args-string (sym &optional index)
1042 "Return a string containing the parameter list of the function SYM.
1043 If SYM is a subr and no arglist is obtainable from the docstring
1044 or elsewhere, return a 1-line docstring."
1045 (let ((argstring
1046 (cond
1047 ((not (and sym (symbolp sym) (fboundp sym))) nil)
1048 ((and (eq sym (aref elisp--eldoc-last-data 0))
1049 (eq 'function (aref elisp--eldoc-last-data 2)))
1050 (aref elisp--eldoc-last-data 1))
1052 (let* ((advertised (gethash (indirect-function sym)
1053 advertised-signature-table t))
1055 (args
1056 (cond
1057 ((listp advertised) advertised)
1058 ((setq doc (help-split-fundoc (documentation sym t) sym))
1059 (car doc))
1060 (t (help-function-arglist sym)))))
1061 ;; Stringify, and store before highlighting, downcasing, etc.
1062 ;; FIXME should truncate before storing.
1063 (elisp--last-data-store sym (elisp--function-argstring args)
1064 'function))))))
1065 ;; Highlight, truncate.
1066 (if argstring
1067 (elisp--highlight-function-argument sym argstring index))))
1069 (defun elisp--highlight-function-argument (sym args index)
1070 "Highlight argument INDEX in ARGS list for function SYM.
1071 In the absence of INDEX, just call `elisp--docstring-format-sym-doc'."
1072 ;; FIXME: This should probably work on the list representation of `args'
1073 ;; rather than its string representation.
1074 ;; FIXME: This function is much too long, we need to split it up!
1075 (let ((start nil)
1076 (end 0)
1077 (argument-face 'eldoc-highlight-function-argument)
1078 (args-lst (mapcar (lambda (x)
1079 (replace-regexp-in-string
1080 "\\`[(]\\|[)]\\'" "" x))
1081 (split-string args))))
1082 ;; Find the current argument in the argument string. We need to
1083 ;; handle `&rest' and informal `...' properly.
1085 ;; FIXME: What to do with optional arguments, like in
1086 ;; (defun NAME ARGLIST [DOCSTRING] BODY...) case?
1087 ;; The problem is there is no robust way to determine if
1088 ;; the current argument is indeed a docstring.
1090 ;; When `&key' is used finding position based on `index'
1091 ;; would be wrong, so find the arg at point and determine
1092 ;; position in ARGS based on this current arg.
1093 (when (string-match "&key" args)
1094 (let* (case-fold-search
1095 key-have-value
1096 (sym-name (symbol-name sym))
1097 (cur-w (current-word))
1098 (args-lst-ak (cdr (member "&key" args-lst)))
1099 (limit (save-excursion
1100 (when (re-search-backward sym-name nil t)
1101 (match-end 0))))
1102 (cur-a (if (and cur-w (string-match ":\\([^ ()]*\\)" cur-w))
1103 (substring cur-w 1)
1104 (save-excursion
1105 (let (split)
1106 (when (re-search-backward ":\\([^()\n]*\\)" limit t)
1107 (setq split (split-string (match-string 1) " " t))
1108 (prog1 (car split)
1109 (when (cdr split)
1110 (setq key-have-value t))))))))
1111 ;; If `cur-a' is not one of `args-lst-ak'
1112 ;; assume user is entering an unknown key
1113 ;; referenced in last position in signature.
1114 (other-key-arg (and (stringp cur-a)
1115 args-lst-ak
1116 (not (member (upcase cur-a) args-lst-ak))
1117 (upcase (car (last args-lst-ak))))))
1118 (unless (string= cur-w sym-name)
1119 ;; The last keyword have already a value
1120 ;; i.e :foo a b and cursor is at b.
1121 ;; If signature have also `&rest'
1122 ;; (assume it is after the `&key' section)
1123 ;; go to the arg after `&rest'.
1124 (if (and key-have-value
1125 (save-excursion
1126 (not (re-search-forward ":.*" (point-at-eol) t)))
1127 (string-match "&rest \\([^ ()]*\\)" args))
1128 (setq index nil ; Skip next block based on positional args.
1129 start (match-beginning 1)
1130 end (match-end 1))
1131 ;; If `cur-a' is nil probably cursor is on a positional arg
1132 ;; before `&key', in this case, exit this block and determine
1133 ;; position with `index'.
1134 (when (and cur-a ; A keyword arg (dot removed) or nil.
1135 (or (string-match
1136 (concat "\\_<" (upcase cur-a) "\\_>") args)
1137 (string-match
1138 (concat "\\_<" other-key-arg "\\_>") args)))
1139 (setq index nil ; Skip next block based on positional args.
1140 start (match-beginning 0)
1141 end (match-end 0)))))))
1142 ;; Handle now positional arguments.
1143 (while (and index (>= index 1))
1144 (if (string-match "[^ ()]+" args end)
1145 (progn
1146 (setq start (match-beginning 0)
1147 end (match-end 0))
1148 (let ((argument (match-string 0 args)))
1149 (cond ((string= argument "&rest")
1150 ;; All the rest arguments are the same.
1151 (setq index 1))
1152 ((string= argument "&optional")) ; Skip.
1153 ((string= argument "&allow-other-keys")) ; Skip.
1154 ;; Back to index 0 in ARG1 ARG2 ARG2 ARG3 etc...
1155 ;; like in `setq'.
1156 ((or (and (string-match-p "\\.\\.\\.$" argument)
1157 (string= argument (car (last args-lst))))
1158 (and (string-match-p "\\.\\.\\.$"
1159 (substring args 1 (1- (length args))))
1160 (= (length (remove "..." args-lst)) 2)
1161 (> index 1) (eq (logand index 1) 1)))
1162 (setq index 0))
1164 (setq index (1- index))))))
1165 (setq end (length args)
1166 start (1- end)
1167 argument-face 'font-lock-warning-face
1168 index 0)))
1169 (let ((doc args))
1170 (when start
1171 (setq doc (copy-sequence args))
1172 (add-text-properties start end (list 'face argument-face) doc))
1173 (setq doc (elisp--docstring-format-sym-doc
1174 sym doc (if (functionp sym) 'font-lock-function-name-face
1175 'font-lock-keyword-face)))
1176 doc)))
1178 ;; Return a string containing a brief (one-line) documentation string for
1179 ;; the variable.
1180 (defun elisp--get-var-docstring (sym)
1181 (cond ((not sym) nil)
1182 ((and (eq sym (aref elisp--eldoc-last-data 0))
1183 (eq 'variable (aref elisp--eldoc-last-data 2)))
1184 (aref elisp--eldoc-last-data 1))
1186 (let ((doc (documentation-property sym 'variable-documentation t)))
1187 (when doc
1188 (let ((doc (elisp--docstring-format-sym-doc
1189 sym (elisp--docstring-first-line doc)
1190 'font-lock-variable-name-face)))
1191 (elisp--last-data-store sym doc 'variable)))))))
1193 (defun elisp--last-data-store (symbol doc type)
1194 (aset elisp--eldoc-last-data 0 symbol)
1195 (aset elisp--eldoc-last-data 1 doc)
1196 (aset elisp--eldoc-last-data 2 type)
1197 doc)
1199 ;; Note that any leading `*' in the docstring (which indicates the variable
1200 ;; is a user option) is removed.
1201 (defun elisp--docstring-first-line (doc)
1202 (and (stringp doc)
1203 (substitute-command-keys
1204 (save-match-data
1205 ;; Don't use "^" in the regexp below since it may match
1206 ;; anywhere in the doc-string.
1207 (let ((start (if (string-match "\\`\\*" doc) (match-end 0) 0)))
1208 (cond ((string-match "\n" doc)
1209 (substring doc start (match-beginning 0)))
1210 ((zerop start) doc)
1211 (t (substring doc start))))))))
1213 (defvar eldoc-echo-area-use-multiline-p)
1215 ;; If the entire line cannot fit in the echo area, the symbol name may be
1216 ;; truncated or eliminated entirely from the output to make room for the
1217 ;; description.
1218 (defun elisp--docstring-format-sym-doc (sym doc face)
1219 (save-match-data
1220 (let* ((name (symbol-name sym))
1221 (ea-multi eldoc-echo-area-use-multiline-p)
1222 ;; Subtract 1 from window width since emacs will not write
1223 ;; any chars to the last column, or in later versions, will
1224 ;; cause a wraparound and resize of the echo area.
1225 (ea-width (1- (window-width (minibuffer-window))))
1226 (strip (- (+ (length name) (length ": ") (length doc)) ea-width)))
1227 (cond ((or (<= strip 0)
1228 (eq ea-multi t)
1229 (and ea-multi (> (length doc) ea-width)))
1230 (format "%s: %s" (propertize name 'face face) doc))
1231 ((> (length doc) ea-width)
1232 (substring (format "%s" doc) 0 ea-width))
1233 ((>= strip (length name))
1234 (format "%s" doc))
1236 ;; Show the end of the partial symbol name, rather
1237 ;; than the beginning, since the former is more likely
1238 ;; to be unique given package namespace conventions.
1239 (setq name (substring name strip))
1240 (format "%s: %s" (propertize name 'face face) doc))))))
1243 ;; Return a list of current function name and argument index.
1244 (defun elisp--fnsym-in-current-sexp ()
1245 (save-excursion
1246 (let ((argument-index (1- (elisp--beginning-of-sexp))))
1247 ;; If we are at the beginning of function name, this will be -1.
1248 (when (< argument-index 0)
1249 (setq argument-index 0))
1250 ;; Don't do anything if current word is inside a string.
1251 (if (= (or (char-after (1- (point))) 0) ?\")
1253 (list (elisp--current-symbol) argument-index)))))
1255 ;; Move to the beginning of current sexp. Return the number of nested
1256 ;; sexp the point was over or after.
1257 (defun elisp--beginning-of-sexp ()
1258 (let ((parse-sexp-ignore-comments t)
1259 (num-skipped-sexps 0))
1260 (condition-case _
1261 (progn
1262 ;; First account for the case the point is directly over a
1263 ;; beginning of a nested sexp.
1264 (condition-case _
1265 (let ((p (point)))
1266 (forward-sexp -1)
1267 (forward-sexp 1)
1268 (when (< (point) p)
1269 (setq num-skipped-sexps 1)))
1270 (error))
1271 (while
1272 (let ((p (point)))
1273 (forward-sexp -1)
1274 (when (< (point) p)
1275 (setq num-skipped-sexps (1+ num-skipped-sexps))))))
1276 (error))
1277 num-skipped-sexps))
1279 ;; returns nil unless current word is an interned symbol.
1280 (defun elisp--current-symbol ()
1281 (let ((c (char-after (point))))
1282 (and c
1283 (memq (char-syntax c) '(?w ?_))
1284 (intern-soft (current-word)))))
1286 (defun elisp--function-argstring (arglist)
1287 "Return ARGLIST as a string enclosed by ().
1288 ARGLIST is either a string, or a list of strings or symbols."
1289 (let ((str (cond ((stringp arglist) arglist)
1290 ((not (listp arglist)) nil)
1291 (t (format "%S" (help-make-usage 'toto arglist))))))
1292 (if (and str (string-match "\\`([^ )]+ ?" str))
1293 (replace-match "(" t t str)
1294 str)))
1296 (provide 'elisp-mode)
1297 ;;; elisp-mode.el ends here