1 ;;; lisp-mode.el --- Lisp mode, and its idiosyncratic commands
3 ;; Copyright (C) 1985-1986, 1999-2013 Free Software Foundation, Inc.
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 base major mode for editing Lisp code (used also for Emacs Lisp).
27 ;; This mode is documented in the Emacs manual.
31 (defvar font-lock-comment-face
)
32 (defvar font-lock-doc-face
)
33 (defvar font-lock-keywords-case-fold-search
)
34 (defvar font-lock-string-face
)
36 (defvar lisp-mode-abbrev-table nil
)
37 (define-abbrev-table 'lisp-mode-abbrev-table
()
38 "Abbrev table for Lisp mode.")
40 (defvar emacs-lisp-mode-abbrev-table nil
)
41 (define-abbrev-table 'emacs-lisp-mode-abbrev-table
()
42 "Abbrev table for Emacs Lisp mode.
43 It has `lisp-mode-abbrev-table' as its parent."
44 :parents
(list lisp-mode-abbrev-table
))
46 (defvar emacs-lisp-mode-syntax-table
47 (let ((table (make-syntax-table))
50 (modify-syntax-entry i
"_ " table
)
54 (modify-syntax-entry i
"_ " table
)
58 (modify-syntax-entry i
"_ " table
)
62 (modify-syntax-entry i
"_ " table
)
64 (modify-syntax-entry ?\s
" " table
)
65 ;; Non-break space acts as whitespace.
66 (modify-syntax-entry ?
\x8a0 " " table
)
67 (modify-syntax-entry ?
\t " " table
)
68 (modify-syntax-entry ?
\f " " table
)
69 (modify-syntax-entry ?
\n "> " table
)
70 ;; This is probably obsolete since nowadays such features use overlays.
71 ;; ;; Give CR the same syntax as newline, for selective-display.
72 ;; (modify-syntax-entry ?\^m "> " table)
73 (modify-syntax-entry ?\
; "< " table)
74 (modify-syntax-entry ?
` "' " table
)
75 (modify-syntax-entry ?
' "' " table
)
76 (modify-syntax-entry ?
, "' " table
)
77 (modify-syntax-entry ?
@ "' " table
)
78 ;; Used to be singlequote; changed for flonums.
79 (modify-syntax-entry ?.
"_ " table
)
80 (modify-syntax-entry ?
# "' " table
)
81 (modify-syntax-entry ?
\" "\" " table
)
82 (modify-syntax-entry ?
\\ "\\ " table
)
83 (modify-syntax-entry ?\
( "() " table
)
84 (modify-syntax-entry ?\
) ")( " table
)
85 (modify-syntax-entry ?\
[ "(] " table
)
86 (modify-syntax-entry ?\
] ")[ " table
)
88 "Syntax table used in `emacs-lisp-mode'.")
90 (defvar lisp-mode-syntax-table
91 (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table
)))
92 (modify-syntax-entry ?\
[ "_ " table
)
93 (modify-syntax-entry ?\
] "_ " table
)
94 (modify-syntax-entry ?
# "' 14" table
)
95 (modify-syntax-entry ?|
"\" 23bn" table
)
97 "Syntax table used in `lisp-mode'.")
99 (defvar lisp-imenu-generic-expression
102 (purecopy (concat "^\\s-*("
105 '("defun" "defun*" "defsubst" "defmacro"
106 "defadvice" "define-skeleton"
107 "define-minor-mode" "define-global-minor-mode"
108 "define-globalized-minor-mode"
109 "define-derived-mode" "define-generic-mode"
110 "define-compiler-macro" "define-modify-macro"
111 "defsetf" "define-setf-expander"
112 "define-method-combination"
113 "defgeneric" "defmethod") t
))
114 "\\s-+\\(\\(\\sw\\|\\s_\\)+\\)"))
116 (list (purecopy "Variables")
117 (purecopy (concat "^\\s-*("
120 '("defconst" "defconstant" "defcustom"
121 "defparameter" "define-symbol-macro") t
))
122 "\\s-+\\(\\(\\sw\\|\\s_\\)+\\)"))
124 ;; For `defvar', we ignore (defvar FOO) constructs.
125 (list (purecopy "Variables")
126 (purecopy (concat "^\\s-*(defvar\\s-+\\(\\(\\sw\\|\\s_\\)+\\)"
127 "[[:space:]\n]+[^)]"))
129 (list (purecopy "Types")
130 (purecopy (concat "^\\s-*("
133 '("defgroup" "deftheme" "deftype" "defstruct"
134 "defclass" "define-condition" "define-widget"
135 "defface" "defpackage") t
))
136 "\\s-+'?\\(\\(\\sw\\|\\s_\\)+\\)"))
139 "Imenu generic expression for Lisp mode. See `imenu-generic-expression'.")
141 ;; This was originally in autoload.el and is still used there.
142 (put 'autoload
'doc-string-elt
3)
143 (put 'defmethod
'doc-string-elt
3)
144 (put 'defvar
'doc-string-elt
3)
145 (put 'defconst
'doc-string-elt
3)
146 (put 'defalias
'doc-string-elt
3)
147 (put 'defvaralias
'doc-string-elt
3)
148 (put 'define-category
'doc-string-elt
2)
150 (defvar lisp-doc-string-elt-property
'doc-string-elt
151 "The symbol property that holds the docstring position info.")
153 (defun lisp-font-lock-syntactic-face-function (state)
155 ;; This might be a (doc)string or a |...| symbol.
156 (let ((startpos (nth 8 state
)))
157 (if (eq (char-after startpos
) ?|
)
158 ;; This is not a string, but a |...| symbol.
160 (let* ((listbeg (nth 1 state
))
161 (firstsym (and listbeg
164 (and (looking-at "([ \t\n]*\\(\\(\\sw\\|\\s_\\)+\\)")
166 (docelt (and firstsym
167 (function-get (intern-soft firstsym
)
168 lisp-doc-string-elt-property
))))
170 ;; It's a string in a form that can have a docstring.
171 ;; Check whether it's in docstring position.
173 (when (functionp docelt
)
174 (goto-char (match-end 1))
175 (setq docelt
(funcall docelt
)))
179 (while (and (> docelt
0) (< (point) startpos
)
180 (progn (forward-sexp 1) t
))
181 (setq docelt
(1- docelt
)))
183 (and (zerop docelt
) (<= (point) startpos
)
184 (progn (forward-comment (point-max)) t
)
185 (= (point) (nth 8 state
)))))
187 font-lock-string-face
))))
188 font-lock-comment-face
))
190 (defun lisp-mode-variables (&optional lisp-syntax keywords-case-insensitive
)
191 "Common initialization routine for lisp modes.
192 The LISP-SYNTAX argument is used by code in inf-lisp.el and is
193 \(uselessly) passed from pp.el, chistory.el, gnus-kill.el and
194 score-mode.el. KEYWORDS-CASE-INSENSITIVE non-nil means that for
195 font-lock keywords will not be case sensitive."
197 (set-syntax-table lisp-mode-syntax-table
))
198 (setq-local paragraph-ignore-fill-prefix t
)
199 (setq-local fill-paragraph-function
'lisp-fill-paragraph
)
200 ;; Adaptive fill mode gets the fill wrong for a one-line paragraph made of
201 ;; a single docstring. Let's fix it here.
202 (setq-local adaptive-fill-function
203 (lambda () (if (looking-at "\\s-+\"[^\n\"]+\"\\s-*$") "")))
204 ;; Adaptive fill mode gets in the way of auto-fill,
205 ;; and should make no difference for explicit fill
206 ;; because lisp-fill-paragraph should do the job.
207 ;; I believe that newcomment's auto-fill code properly deals with it -stef
208 ;;(set (make-local-variable 'adaptive-fill-mode) nil)
209 (setq-local indent-line-function
'lisp-indent-line
)
210 (setq-local outline-regexp
";;;\\(;* [^ \t\n]\\|###autoload\\)\\|(")
211 (setq-local outline-level
'lisp-outline-level
)
212 (setq-local add-log-current-defun-function
#'lisp-current-defun-name
)
213 (setq-local comment-start
";")
214 ;; Look within the line for a ; following an even number of backslashes
215 ;; after either a non-backslash or the line beginning.
216 (setq-local comment-start-skip
"\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\);+ *")
217 ;; Font lock mode uses this only when it KNOWS a comment is starting.
218 (setq-local font-lock-comment-start-skip
";+ *")
219 (setq-local comment-add
1) ;default to `;;' in comment-region
220 (setq-local comment-column
40)
221 ;; Don't get confused by `;' in doc strings when paragraph-filling.
222 (setq-local comment-use-global-state t
)
223 (setq-local imenu-generic-expression lisp-imenu-generic-expression
)
224 (setq-local multibyte-syntax-as-symbol t
)
225 (setq-local syntax-begin-function
'beginning-of-defun
)
226 (setq font-lock-defaults
227 `((lisp-font-lock-keywords
228 lisp-font-lock-keywords-1 lisp-font-lock-keywords-2
)
229 nil
,keywords-case-insensitive
(("+-*/.<>=!?$%_&~^:@" .
"w")) nil
230 (font-lock-mark-block-function . mark-defun
)
231 (font-lock-syntactic-face-function
232 . lisp-font-lock-syntactic-face-function
))))
234 (defun lisp-outline-level ()
235 "Lisp mode `outline-level' function."
236 (let ((len (- (match-end 0) (match-beginning 0))))
237 (if (looking-at "(\\|;;;###autoload")
241 (defun lisp-current-defun-name ()
242 "Return the name of the defun at point, or nil."
244 (let ((location (point)))
245 ;; If we are now precisely at the beginning of a defun, make sure
246 ;; beginning-of-defun finds that one rather than the previous one.
247 (or (eobp) (forward-char 1))
249 ;; Make sure we are really inside the defun found, not after it.
250 (when (and (looking-at "\\s(")
251 (progn (end-of-defun)
252 (< location
(point)))
253 (progn (forward-sexp -
1)
254 (>= location
(point))))
255 (if (looking-at "\\s(")
257 ;; Skip the defining construct name, typically "defun" or
260 ;; The second element is usually a symbol being defined. If it
261 ;; is not, use the first symbol in it.
262 (skip-chars-forward " \t\n'(")
263 (buffer-substring-no-properties (point)
264 (progn (forward-sexp 1)
267 (defvar lisp-mode-shared-map
268 (let ((map (make-sparse-keymap)))
269 (define-key map
"\e\C-q" 'indent-sexp
)
270 (define-key map
"\177" 'backward-delete-char-untabify
)
271 ;; This gets in the way when viewing a Lisp file in view-mode. As
272 ;; long as [backspace] is mapped into DEL via the
273 ;; function-key-map, this should remain disabled!!
274 ;;;(define-key map [backspace] 'backward-delete-char-untabify)
276 "Keymap for commands shared by all sorts of Lisp modes.")
278 (defvar emacs-lisp-mode-map
279 (let ((map (make-sparse-keymap "Emacs-Lisp"))
280 (menu-map (make-sparse-keymap "Emacs-Lisp"))
281 (lint-map (make-sparse-keymap))
282 (prof-map (make-sparse-keymap))
283 (tracing-map (make-sparse-keymap)))
284 (set-keymap-parent map lisp-mode-shared-map
)
285 (define-key map
"\e\t" 'completion-at-point
)
286 (define-key map
"\e\C-x" 'eval-defun
)
287 (define-key map
"\e\C-q" 'indent-pp-sexp
)
288 (bindings--define-key map
[menu-bar emacs-lisp
]
289 (cons "Emacs-Lisp" menu-map
))
290 (bindings--define-key menu-map
[eldoc]
291 '(menu-item "Auto-Display Documentation Strings" eldoc-mode
292 :button (:toggle . (bound-and-true-p eldoc-mode))
293 :help "Display the documentation string for the item under cursor"))
294 (bindings--define-key menu-map [checkdoc]
295 '(menu-item "Check Documentation Strings" checkdoc
296 :help "Check documentation strings for style requirements"))
297 (bindings--define-key menu-map [re-builder]
298 '(menu-item "Construct Regexp" re-builder
299 :help "Construct a regexp interactively"))
300 (bindings--define-key menu-map [tracing] (cons "Tracing" tracing-map))
301 (bindings--define-key tracing-map [tr-a]
302 '(menu-item "Untrace All" untrace-all
303 :help "Untrace all currently traced functions"))
304 (bindings--define-key tracing-map [tr-uf]
305 '(menu-item "Untrace Function..." untrace-function
306 :help "Untrace function, and possibly activate all remaining advice"))
307 (bindings--define-key tracing-map [tr-sep] menu-bar-separator)
308 (bindings--define-key tracing-map [tr-q]
309 '(menu-item "Trace Function Quietly..." trace-function-background
310 :help "Trace the function with trace output going quietly to a buffer"))
311 (bindings--define-key tracing-map [tr-f]
312 '(menu-item "Trace Function..." trace-function
313 :help "Trace the function given as an argument"))
314 (bindings--define-key menu-map [profiling] (cons "Profiling" prof-map))
315 (bindings--define-key prof-map [prof-restall]
316 '(menu-item "Remove Instrumentation for All Functions" elp-restore-all
317 :help "Restore the original definitions of all functions being profiled"))
318 (bindings--define-key prof-map [prof-restfunc]
319 '(menu-item "Remove Instrumentation for Function..." elp-restore-function
320 :help "Restore an instrumented function to its original definition"))
322 (bindings--define-key prof-map [sep-rem] menu-bar-separator)
323 (bindings--define-key prof-map [prof-resall]
324 '(menu-item "Reset Counters for All Functions" elp-reset-all
325 :help "Reset the profiling information for all functions being profiled"))
326 (bindings--define-key prof-map [prof-resfunc]
327 '(menu-item "Reset Counters for Function..." elp-reset-function
328 :help "Reset the profiling information for a function"))
329 (bindings--define-key prof-map [prof-res]
330 '(menu-item "Show Profiling Results" elp-results
331 :help "Display current profiling results"))
332 (bindings--define-key prof-map [prof-pack]
333 '(menu-item "Instrument Package..." elp-instrument-package
334 :help "Instrument for profiling all function that start with a prefix"))
335 (bindings--define-key prof-map [prof-func]
336 '(menu-item "Instrument Function..." elp-instrument-function
337 :help "Instrument a function for profiling"))
338 (bindings--define-key menu-map [lint] (cons "Linting" lint-map))
339 (bindings--define-key lint-map [lint-di]
340 '(menu-item "Lint Directory..." elint-directory
341 :help "Lint a directory"))
342 (bindings--define-key lint-map [lint-f]
343 '(menu-item "Lint File..." elint-file
344 :help "Lint a file"))
345 (bindings--define-key lint-map [lint-b]
346 '(menu-item "Lint Buffer" elint-current-buffer
347 :help "Lint the current buffer"))
348 (bindings--define-key lint-map [lint-d]
349 '(menu-item "Lint Defun" elint-defun
350 :help "Lint the function at point"))
351 (bindings--define-key menu-map [edebug-defun]
352 '(menu-item "Instrument Function for Debugging" edebug-defun
353 :help "Evaluate the top level form point is in, stepping through with Edebug"
355 (bindings--define-key menu-map [separator-byte] menu-bar-separator)
356 (bindings--define-key menu-map [disas]
357 '(menu-item "Disassemble Byte Compiled Object..." disassemble
358 :help "Print disassembled code for OBJECT in a buffer"))
359 (bindings--define-key menu-map [byte-recompile]
360 '(menu-item "Byte-recompile Directory..." byte-recompile-directory
361 :help "Recompile every `.el' file in DIRECTORY that needs recompilation"))
362 (bindings--define-key menu-map [emacs-byte-compile-and-load]
363 '(menu-item "Byte-compile and Load" emacs-lisp-byte-compile-and-load
364 :help "Byte-compile the current file (if it has changed), then load compiled code"))
365 (bindings--define-key menu-map [byte-compile]
366 '(menu-item "Byte-compile This File" emacs-lisp-byte-compile
367 :help "Byte compile the file containing the current buffer"))
368 (bindings--define-key menu-map [separator-eval] menu-bar-separator)
369 (bindings--define-key menu-map [ielm]
370 '(menu-item "Interactive Expression Evaluation" ielm
371 :help "Interactively evaluate Emacs Lisp expressions"))
372 (bindings--define-key menu-map [eval-buffer]
373 '(menu-item "Evaluate Buffer" eval-buffer
374 :help "Execute the current buffer as Lisp code"))
375 (bindings--define-key menu-map [eval-region]
376 '(menu-item "Evaluate Region" eval-region
377 :help "Execute the region as Lisp code"
378 :enable mark-active))
379 (bindings--define-key menu-map [eval-sexp]
380 '(menu-item "Evaluate Last S-expression" eval-last-sexp
381 :help "Evaluate sexp before point; print value in minibuffer"))
382 (bindings--define-key menu-map [separator-format] menu-bar-separator)
383 (bindings--define-key menu-map [comment-region]
384 '(menu-item "Comment Out Region" comment-region
385 :help "Comment or uncomment each line in the region"
386 :enable mark-active))
387 (bindings--define-key menu-map [indent-region]
388 '(menu-item "Indent Region" indent-region
389 :help "Indent each nonblank line in the region"
390 :enable mark-active))
391 (bindings--define-key menu-map [indent-line]
392 '(menu-item "Indent Line" lisp-indent-line))
394 "Keymap for Emacs Lisp mode.
395 All commands in `lisp-mode-shared-map' are inherited by this map.")
397 (defun emacs-lisp-byte-compile ()
398 "Byte compile the file containing the current buffer."
401 (byte-compile-file buffer-file-name)
402 (error "The buffer must be saved in a file first")))
404 (defun emacs-lisp-byte-compile-and-load ()
405 "Byte-compile the current file (if it has changed), then load compiled code."
408 (error "The buffer must be saved in a file first"))
410 ;; Recompile if file or buffer has changed since last compilation.
411 (if (and (buffer-modified-p)
412 (y-or-n-p (format "Save buffer %s first? " (buffer-name))))
414 (byte-recompile-file buffer-file-name nil 0 t))
416 (defcustom emacs-lisp-mode-hook nil
417 "Hook run when entering Emacs Lisp mode."
418 :options '(turn-on-eldoc-mode imenu-add-menubar-index checkdoc-minor-mode)
422 (defcustom lisp-mode-hook nil
423 "Hook run when entering Lisp mode."
424 :options '(imenu-add-menubar-index)
428 (defcustom lisp-interaction-mode-hook nil
429 "Hook run when entering Lisp Interaction mode."
430 :options '(turn-on-eldoc-mode)
434 (define-derived-mode emacs-lisp-mode prog-mode "Emacs-Lisp"
435 "Major mode for editing Lisp code to run in Emacs.
437 Delete converts tabs to spaces as it moves back.
438 Blank lines separate paragraphs. Semicolons start comments.
440 \\{emacs-lisp-mode-map}
441 Entry to this mode calls the value of `emacs-lisp-mode-hook'
442 if that value is non-nil."
444 (lisp-mode-variables)
445 (setq imenu-case-fold-search nil)
446 (add-hook 'completion-at-point-functions
447 'lisp-completion-at-point nil 'local))
449 ;;; Emacs Lisp Byte-Code mode
452 (defconst emacs-list-byte-code-comment-re
453 (concat "\\(#\\)@\\([0-9]+\\) "
454 ;; Make sure it's a docstring and not a lazy-loaded byte-code.
455 "\\(?:[^(]\\|([^\"]\\)")))
457 (defun emacs-lisp-byte-code-comment (end &optional _point)
458 "Try to syntactically mark the #@NNN ....^_ docstrings in byte-code files."
459 (let ((ppss (syntax-ppss)))
460 (when (and (nth 4 ppss)
461 (eq (char-after (nth 8 ppss)) ?#))
462 (let* ((n (save-excursion
463 (goto-char (nth 8 ppss))
464 (when (looking-at emacs-list-byte-code-comment-re)
465 (string-to-number (match-string 2)))))
466 ;; `maxdiff' tries to make sure the loop below terminates.
469 (let* ((bchar (match-end 2))
470 (b (position-bytes bchar)))
472 (while (let ((diff (- (position-bytes (point)) b n)))
474 (when (> diff maxdiff) (setq diff maxdiff))
475 (forward-char (- diff))
476 (setq maxdiff (if (> diff 0) diff
477 (max (1- maxdiff) 1)))
480 (put-text-property (1- (point)) (point)
482 (string-to-syntax "> b"))
483 (goto-char end)))))))
485 (defun emacs-lisp-byte-code-syntax-propertize (start end)
486 (emacs-lisp-byte-code-comment end (point))
488 (syntax-propertize-rules
489 (emacs-list-byte-code-comment-re
490 (1 (prog1 "< b" (emacs-lisp-byte-code-comment end (point))))))
493 (add-to-list 'auto-mode-alist '("\\.elc\\'" . emacs-lisp-byte-code-mode))
494 (define-derived-mode emacs-lisp-byte-code-mode emacs-lisp-mode
496 "Major mode for *.elc files."
497 ;; TODO: Add way to disassemble byte-code under point.
498 (setq-local open-paren-in-column-0-is-defun-start nil)
499 (setq-local syntax-propertize-function
500 #'emacs-lisp-byte-code-syntax-propertize))
502 ;;; Generic Lisp mode.
504 (defvar lisp-mode-map
505 (let ((map (make-sparse-keymap))
506 (menu-map (make-sparse-keymap "Lisp")))
507 (set-keymap-parent map lisp-mode-shared-map)
508 (define-key map "\e\C-x" 'lisp-eval-defun)
509 (define-key map "\C-c\C-z" 'run-lisp)
510 (bindings--define-key map [menu-bar lisp] (cons "Lisp" menu-map))
511 (bindings--define-key menu-map [run-lisp]
512 '(menu-item "Run inferior Lisp" run-lisp
513 :help "Run an inferior Lisp process, input and output via buffer `*inferior-lisp*'"))
514 (bindings--define-key menu-map [ev-def]
515 '(menu-item "Eval defun" lisp-eval-defun
516 :help "Send the current defun to the Lisp process made by M-x run-lisp"))
517 (bindings--define-key menu-map [ind-sexp]
518 '(menu-item "Indent sexp" indent-sexp
519 :help "Indent each line of the list starting just after point"))
521 "Keymap for ordinary Lisp mode.
522 All commands in `lisp-mode-shared-map' are inherited by this map.")
524 (define-derived-mode lisp-mode prog-mode "Lisp"
525 "Major mode for editing Lisp code for Lisps other than GNU Emacs Lisp.
527 Delete converts tabs to spaces as it moves back.
528 Blank lines separate paragraphs. Semicolons start comments.
531 Note that `run-lisp' may be used either to start an inferior Lisp job
532 or to switch back to an existing one.
534 Entry to this mode calls the value of `lisp-mode-hook'
535 if that value is non-nil."
536 (lisp-mode-variables nil t)
537 (setq-local find-tag-default-function 'lisp-find-tag-default)
538 (setq-local comment-start-skip
539 "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\)\\(;+\\|#|\\) *")
540 (setq imenu-case-fold-search t))
542 (defun lisp-find-tag-default ()
543 (let ((default (find-tag-default)))
544 (when (stringp default)
545 (if (string-match ":+" default)
546 (substring default (match-end 0))
549 ;; Used in old LispM code.
550 (defalias 'common-lisp-mode 'lisp-mode)
552 ;; This will do unless inf-lisp.el is loaded.
553 (defun lisp-eval-defun (&optional and-go)
554 "Send the current defun to the Lisp process made by \\[run-lisp]."
556 (error "Process lisp does not exist"))
558 (defvar lisp-interaction-mode-map
559 (let ((map (make-sparse-keymap))
560 (menu-map (make-sparse-keymap "Lisp-Interaction")))
561 (set-keymap-parent map lisp-mode-shared-map)
562 (define-key map "\e\C-x" 'eval-defun)
563 (define-key map "\e\C-q" 'indent-pp-sexp)
564 (define-key map "\e\t" 'completion-at-point)
565 (define-key map "\n" 'eval-print-last-sexp)
566 (bindings--define-key map [menu-bar lisp-interaction]
567 (cons "Lisp-Interaction" menu-map))
568 (bindings--define-key menu-map [eval-defun]
569 '(menu-item "Evaluate Defun" eval-defun
570 :help "Evaluate the top-level form containing point, or after point"))
571 (bindings--define-key menu-map [eval-print-last-sexp]
572 '(menu-item "Evaluate and Print" eval-print-last-sexp
573 :help "Evaluate sexp before point; print value into current buffer"))
574 (bindings--define-key menu-map [edebug-defun-lisp-interaction]
575 '(menu-item "Instrument Function for Debugging" edebug-defun
576 :help "Evaluate the top level form point is in, stepping through with Edebug"
578 (bindings--define-key menu-map [indent-pp-sexp]
579 '(menu-item "Indent or Pretty-Print" indent-pp-sexp
580 :help "Indent each line of the list starting just after point, or prettyprint it"))
581 (bindings--define-key menu-map [complete-symbol]
582 '(menu-item "Complete Lisp Symbol" completion-at-point
583 :help "Perform completion on Lisp symbol preceding point"))
585 "Keymap for Lisp Interaction mode.
586 All commands in `lisp-mode-shared-map' are inherited by this map.")
588 (define-derived-mode lisp-interaction-mode emacs-lisp-mode "Lisp Interaction"
589 "Major mode for typing and evaluating Lisp forms.
590 Like Lisp mode except that \\[eval-print-last-sexp] evals the Lisp expression
591 before point, and prints its value into the buffer, advancing point.
592 Note that printing is controlled by `eval-expression-print-length'
593 and `eval-expression-print-level'.
596 Delete converts tabs to spaces as it moves back.
597 Paragraphs are separated only by blank lines.
598 Semicolons start comments.
600 \\{lisp-interaction-mode-map}
601 Entry to this mode calls the value of `lisp-interaction-mode-hook'
602 if that value is non-nil."
605 (defun eval-print-last-sexp ()
606 "Evaluate sexp before point; print value into current buffer.
608 If `eval-expression-debug-on-error' is non-nil, which is the default,
609 this command arranges for all errors to enter the debugger.
611 Note that printing the result is controlled by the variables
612 `eval-expression-print-length' and `eval-expression-print-level',
615 (let ((standard-output (current-buffer)))
621 (defun last-sexp-setup-props (beg end value alt1 alt2)
622 "Set up text properties for the output of `eval-last-sexp-1'.
623 BEG and END are the start and end of the output in current-buffer.
624 VALUE is the Lisp value printed, ALT1 and ALT2 are strings for the
625 alternative printed representations that can be displayed."
626 (let ((map (make-sparse-keymap)))
627 (define-key map "\C-m" 'last-sexp-toggle-display)
628 (define-key map [down-mouse-2] 'mouse-set-point)
629 (define-key map [mouse-2] 'last-sexp-toggle-display)
632 `(printed-value (,value ,alt1 ,alt2)
635 help-echo "RET, mouse-2: toggle abbreviated display"
636 rear-nonsticky (mouse-face keymap help-echo
640 (defun last-sexp-toggle-display (&optional arg)
641 "Toggle between abbreviated and unabbreviated printed representations."
645 (let ((value (get-text-property (point) 'printed-value)))
647 (let ((beg (or (previous-single-property-change (min (point-max) (1+ (point)))
650 (end (or (next-single-char-property-change (point) 'printed-value) (point)))
651 (standard-output (current-buffer))
653 (delete-region beg end)
654 (insert (nth 1 value))
656 (setq point (1- (point))))
657 (last-sexp-setup-props beg (point)
661 (goto-char (min (point-max) point)))))))
663 (defun prin1-char (char)
664 "Return a string representing CHAR as a character rather than as an integer.
665 If CHAR is not a character, return nil."
668 (let ((c (event-basic-type char))
669 (mods (event-modifiers char))
671 ;; Prevent ?A from turning into ?\S-a.
672 (if (and (memq 'shift mods)
673 (zerop (logand char ?\S-\^@))
674 (not (let ((case-fold-search nil))
675 (char-equal c (upcase c)))))
676 (setq c (upcase c) mods nil))
677 ;; What string are we considering using?
684 (cond ((eq modif 'super) "\\s-")
685 (t (string ?\\ (upcase (aref (symbol-name modif) 0)) ?-))))
688 ((memq c '(?\; ?\( ?\) ?\{ ?\} ?\[ ?\] ?\" ?\' ?\\)) (string ?\\ c))
693 ;; Verify the string reads a CHAR, not to some other character.
694 ;; If it doesn't, return nil instead.
696 (= (car (read-from-string string)) char)
700 (defun preceding-sexp ()
701 "Return sexp before the point."
702 (let ((opoint (point))
706 (with-syntax-table emacs-lisp-mode-syntax-table
707 ;; If this sexp appears to be enclosed in `...'
708 ;; then ignore the surrounding quotes.
710 (or (eq (following-char) ?\')
711 (eq (preceding-char) ?\')))
713 ;; If we were after `?\e' (or similar case),
714 ;; use the whole thing, not just the `e'.
715 (when (eq (preceding-char) ?\\)
717 (when (eq (preceding-char) ??)
720 ;; Skip over hash table read syntax.
721 (and (> (point) (1+ (point-min)))
722 (looking-back "#s" (- (point) 2))
726 (when (eq (preceding-char) ?=)
729 (skip-chars-backward "0-9#=")
730 (setq labeled-p (looking-at "\\(#[0-9]+=\\)+")))
735 ;; vladimir@cs.ualberta.ca 30-Jul-1997: skip ` in
736 ;; `variable' so that the value is returned, not the
738 (if (and ignore-quotes
739 (eq (following-char) ?`))
741 (narrow-to-region (point-min) opoint)
742 (setq expr (read (current-buffer)))
743 ;; If it's an (interactive ...) form, it's more
744 ;; useful to show how an interactive call would
747 (eq (car expr) 'interactive)
749 (list 'call-interactively
758 (defun eval-last-sexp-1 (eval-last-sexp-arg-internal)
759 "Evaluate sexp before point; print value in minibuffer.
760 With argument, print output into current buffer."
761 (let ((standard-output (if eval-last-sexp-arg-internal (current-buffer) t)))
762 ;; Setup the lexical environment if lexical-binding is enabled.
763 (eval-last-sexp-print-value
764 (eval (eval-sexp-add-defvars (preceding-sexp)) lexical-binding))))
767 (defun eval-last-sexp-print-value (value)
768 (let ((unabbreviated (let ((print-length nil) (print-level nil))
769 (prin1-to-string value)))
770 (print-length eval-expression-print-length)
771 (print-level eval-expression-print-level)
776 (let ((str (eval-expression-print-format value)))
777 (if str (princ str)))
779 (when (and (bufferp standard-output)
780 (or (not (null print-length))
781 (not (null print-level)))
782 (not (string= unabbreviated
783 (buffer-substring-no-properties beg end))))
784 (last-sexp-setup-props beg end value
786 (buffer-substring-no-properties beg end))
790 (defvar eval-last-sexp-fake-value (make-symbol "t"))
792 (defun eval-sexp-add-defvars (exp &optional pos)
793 "Prepend EXP with all the `defvar's that precede it in the buffer.
794 POS specifies the starting position where EXP was found and defaults to point."
795 (if (not lexical-binding)
798 (unless pos (setq pos (point)))
800 (goto-char (point-min))
801 (while (re-search-forward
802 "(def\\(?:var\\|const\\|custom\\)[ \t\n]+\\([^; '()\n\t]+\\)"
804 (let ((var (intern (match-string 1))))
805 (and (not (special-variable-p var))
807 (zerop (car (syntax-ppss (match-beginning 0)))))
809 `(progn ,@(mapcar (lambda (v) `(defvar ,v)) vars) ,exp)))))
811 (defun eval-last-sexp (eval-last-sexp-arg-internal)
812 "Evaluate sexp before point; print value in minibuffer.
813 Interactively, with prefix argument, print output into current buffer.
814 Truncates long output according to the value of the variables
815 `eval-expression-print-length' and `eval-expression-print-level'.
817 If `eval-expression-debug-on-error' is non-nil, which is the default,
818 this command arranges for all errors to enter the debugger."
820 (if (null eval-expression-debug-on-error)
821 (eval-last-sexp-1 eval-last-sexp-arg-internal)
823 (let ((debug-on-error eval-last-sexp-fake-value))
824 (cons (eval-last-sexp-1 eval-last-sexp-arg-internal)
826 (unless (eq (cdr value) eval-last-sexp-fake-value)
827 (setq debug-on-error (cdr value)))
830 (defun eval-defun-1 (form)
831 "Treat some expressions specially.
832 Reset the `defvar' and `defcustom' variables to the initial value.
833 \(For `defcustom', use the :set function if there is one.)
834 Reinitialize the face according to the `defface' specification."
835 ;; The code in edebug-defun should be consistent with this, but not
836 ;; the same, since this gets a macroexpanded form.
837 (cond ((not (listp form))
839 ((and (eq (car form) 'defvar)
840 (cdr-safe (cdr-safe form))
841 (boundp (cadr form)))
842 ;; Force variable to be re-set.
843 `(progn (defvar ,(nth 1 form) nil ,@(nthcdr 3 form))
844 (setq-default ,(nth 1 form) ,(nth 2 form))))
845 ;; `defcustom' is now macroexpanded to
846 ;; `custom-declare-variable' with a quoted value arg.
847 ((and (eq (car form) 'custom-declare-variable)
848 (default-boundp (eval (nth 1 form) lexical-binding)))
849 ;; Force variable to be bound, using :set function if specified.
850 (let ((setfunc (memq :set form)))
852 (setq setfunc (car-safe (cdr-safe setfunc)))
853 (or (functionp setfunc) (setq setfunc nil)))
854 (funcall (or setfunc 'set-default)
855 (eval (nth 1 form) lexical-binding)
856 ;; The second arg is an expression that evaluates to
857 ;; an expression. The second evaluation is the one
858 ;; normally performed not by normal execution but by
859 ;; custom-initialize-set (for example), which does not
860 ;; use lexical-binding.
861 (eval (eval (nth 2 form) lexical-binding))))
863 ;; `defface' is macroexpanded to `custom-declare-face'.
864 ((eq (car form) 'custom-declare-face)
866 (let ((face-symbol (eval (nth 1 form) lexical-binding)))
867 (setq face-new-frame-defaults
868 (assq-delete-all face-symbol face-new-frame-defaults))
869 (put face-symbol 'face-defface-spec nil)
870 (put face-symbol 'face-override-spec nil))
872 ((eq (car form) 'progn)
873 (cons 'progn (mapcar 'eval-defun-1 (cdr form))))
876 (defun eval-defun-2 ()
877 "Evaluate defun that point is in or before.
878 The value is displayed in the minibuffer.
879 If the current defun is actually a call to `defvar',
880 then reset the variable using the initial value expression
881 even if the variable already has some other value.
882 \(Normally `defvar' does not change the variable's value
883 if it already has a value.\)
885 With argument, insert value in current buffer after the defun.
886 Return the result of evaluation."
887 ;; FIXME: the print-length/level bindings should only be applied while
888 ;; printing, not while evaluating.
889 (let ((debug-on-error eval-expression-debug-on-error)
890 (print-length eval-expression-print-length)
891 (print-level eval-expression-print-level))
893 ;; Arrange for eval-region to "read" the (possibly) altered form.
894 ;; eval-region handles recording which file defines a function or
895 ;; variable. Re-written using `apply' to avoid capturing
896 ;; variables like `end'.
899 (let ((standard-output t)
901 ;; Read the form from the buffer, and record where it ends.
906 (setq form (read (current-buffer)))
908 ;; Alter the form if necessary.
909 (setq form (eval-sexp-add-defvars (eval-defun-1 (macroexpand form))))
910 (list beg end standard-output
912 ;; Skipping to the end of the specified region
913 ;; will make eval-region return.
916 ;; The result of evaluation has been put onto VALUES. So return it.
919 (defun eval-defun (edebug-it)
920 "Evaluate the top-level form containing point, or after point.
922 If the current defun is actually a call to `defvar' or `defcustom',
923 evaluating it this way resets the variable using its initial value
924 expression (using the defcustom's :set function if there is one), even
925 if the variable already has some other value. \(Normally `defvar' and
926 `defcustom' do not alter the value if there already is one.) In an
927 analogous way, evaluating a `defface' overrides any customizations of
928 the face, so that it becomes defined exactly as the `defface' expression
931 If `eval-expression-debug-on-error' is non-nil, which is the default,
932 this command arranges for all errors to enter the debugger.
934 With a prefix argument, instrument the code for Edebug.
936 If acting on a `defun' for FUNCTION, and the function was
937 instrumented, `Edebug: FUNCTION' is printed in the minibuffer. If not
938 instrumented, just FUNCTION is printed.
940 If not acting on a `defun', the result of evaluation is displayed in
941 the minibuffer. This display is controlled by the variables
942 `eval-expression-print-length' and `eval-expression-print-level',
947 (eval-defun (not edebug-all-defs)))
949 (if (null eval-expression-debug-on-error)
951 (let ((old-value (make-symbol "t")) new-value value)
952 (let ((debug-on-error old-value))
953 (setq value (eval-defun-2))
954 (setq new-value debug-on-error))
955 (unless (eq old-value new-value)
956 (setq debug-on-error new-value))
959 ;; May still be used by some external Lisp-mode variant.
960 (define-obsolete-function-alias 'lisp-comment-indent
961 'comment-indent-default "22.1")
962 (define-obsolete-function-alias 'lisp-mode-auto-fill 'do-auto-fill "23.1")
964 (defcustom lisp-indent-offset nil
965 "If non-nil, indent second line of expressions that many more columns."
967 :type '(choice (const nil) integer))
968 (put 'lisp-indent-offset 'safe-local-variable
969 (lambda (x) (or (null x) (integerp x))))
971 (defcustom lisp-indent-function 'lisp-indent-function
972 "A function to be called by `calculate-lisp-indent'.
973 It indents the arguments of a Lisp function call. This function
974 should accept two arguments: the indent-point, and the
975 `parse-partial-sexp' state at that position. One option for this
976 function is `common-lisp-indent-function'."
980 (defun lisp-indent-line (&optional whole-exp)
981 "Indent current line as Lisp code.
982 With argument, indent any additional lines of the same expression
983 rigidly along with this one."
985 (let ((indent (calculate-lisp-indent)) shift-amt end
986 (pos (- (point-max) (point)))
987 (beg (progn (beginning-of-line) (point))))
988 (skip-chars-forward " \t")
989 (if (or (null indent) (looking-at "\\s<\\s<\\s<"))
990 ;; Don't alter indentation of a ;;; comment line
991 ;; or a line that starts in a string.
992 ;; FIXME: inconsistency: comment-indent moves ;;; to column 0.
993 (goto-char (- (point-max) pos))
994 (if (and (looking-at "\\s<") (not (looking-at "\\s<\\s<")))
995 ;; Single-semicolon comment lines should be indented
996 ;; as comment lines, not as code.
997 (progn (indent-for-comment) (forward-char -1))
998 (if (listp indent) (setq indent (car indent)))
999 (setq shift-amt (- indent (current-column)))
1000 (if (zerop shift-amt)
1002 (delete-region beg (point))
1003 (indent-to indent)))
1004 ;; If initial point was within line's indentation,
1005 ;; position after the indentation. Else stay at same point in text.
1006 (if (> (- (point-max) pos) (point))
1007 (goto-char (- (point-max) pos))))))
1009 (defvar calculate-lisp-indent-last-sexp)
1011 (defun calculate-lisp-indent (&optional parse-start)
1012 "Return appropriate indentation for current line as Lisp code.
1013 In usual case returns an integer: the column to indent to.
1014 If the value is nil, that means don't change the indentation
1015 because the line starts inside a string.
1017 The value can also be a list of the form (COLUMN CONTAINING-SEXP-START).
1018 This means that following lines at the same level of indentation
1019 should not necessarily be indented the same as this line.
1020 Then COLUMN is the column to indent to, and CONTAINING-SEXP-START
1021 is the buffer position of the start of the containing expression."
1024 (let ((indent-point (point))
1026 ;; setting this to a number inhibits calling hook
1027 (desired-indent nil)
1029 calculate-lisp-indent-last-sexp containing-sexp)
1031 (goto-char parse-start)
1032 (beginning-of-defun))
1033 ;; Find outermost containing sexp
1034 (while (< (point) indent-point)
1035 (setq state (parse-partial-sexp (point) indent-point 0)))
1036 ;; Find innermost containing sexp
1039 (> (setq paren-depth (elt state 0)) 0))
1041 (setq calculate-lisp-indent-last-sexp (elt state 2))
1042 (setq containing-sexp (elt state 1))
1043 ;; Position following last unclosed open.
1044 (goto-char (1+ containing-sexp))
1045 ;; Is there a complete sexp since then?
1046 (if (and calculate-lisp-indent-last-sexp
1047 (> calculate-lisp-indent-last-sexp (point)))
1048 ;; Yes, but is there a containing sexp after that?
1049 (let ((peek (parse-partial-sexp calculate-lisp-indent-last-sexp
1051 (if (setq retry (car (cdr peek))) (setq state peek)))))
1054 ;; Innermost containing sexp found
1055 (goto-char (1+ containing-sexp))
1056 (if (not calculate-lisp-indent-last-sexp)
1057 ;; indent-point immediately follows open paren.
1059 (setq desired-indent (current-column))
1060 ;; Find the start of first element of containing sexp.
1061 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
1062 (cond ((looking-at "\\s(")
1063 ;; First element of containing sexp is a list.
1064 ;; Indent under that list.
1066 ((> (save-excursion (forward-line 1) (point))
1067 calculate-lisp-indent-last-sexp)
1068 ;; This is the first line to start within the containing sexp.
1069 ;; It's almost certainly a function call.
1070 (if (= (point) calculate-lisp-indent-last-sexp)
1071 ;; Containing sexp has nothing before this line
1072 ;; except the first element. Indent under that element.
1074 ;; Skip the first element, find start of second (the first
1075 ;; argument of the function call) and indent under.
1076 (progn (forward-sexp 1)
1077 (parse-partial-sexp (point)
1078 calculate-lisp-indent-last-sexp
1080 (backward-prefix-chars))
1082 ;; Indent beneath first sexp on same line as
1083 ;; `calculate-lisp-indent-last-sexp'. Again, it's
1084 ;; almost certainly a function call.
1085 (goto-char calculate-lisp-indent-last-sexp)
1087 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp
1089 (backward-prefix-chars)))))
1090 ;; Point is at the point to indent under unless we are inside a string.
1091 ;; Call indentation hook except when overridden by lisp-indent-offset
1092 ;; or if the desired indentation has already been computed.
1093 (let ((normal-indent (current-column)))
1094 (cond ((elt state 3)
1095 ;; Inside a string, don't change indentation.
1097 ((and (integerp lisp-indent-offset) containing-sexp)
1098 ;; Indent by constant offset
1099 (goto-char containing-sexp)
1100 (+ (current-column) lisp-indent-offset))
1101 ;; in this case calculate-lisp-indent-last-sexp is not nil
1102 (calculate-lisp-indent-last-sexp
1104 ;; try to align the parameters of a known function
1105 (and lisp-indent-function
1107 (funcall lisp-indent-function indent-point state))
1108 ;; If the function has no special alignment
1109 ;; or it does not apply to this argument,
1110 ;; try to align a constant-symbol under the last
1111 ;; preceding constant symbol, if there is such one of
1112 ;; the last 2 preceding symbols, in the previous
1113 ;; uncommented line.
1114 (and (save-excursion
1115 (goto-char indent-point)
1116 (skip-chars-forward " \t")
1118 ;; The last sexp may not be at the indentation
1119 ;; where it begins, so find that one, instead.
1121 (goto-char calculate-lisp-indent-last-sexp)
1122 ;; Handle prefix characters and whitespace
1123 ;; following an open paren. (Bug#1012)
1124 (backward-prefix-chars)
1125 (while (and (not (looking-back "^[ \t]*\\|([ \t]+"))
1126 (or (not containing-sexp)
1127 (< (1+ containing-sexp) (point))))
1129 (backward-prefix-chars))
1130 (setq calculate-lisp-indent-last-sexp (point)))
1131 (> calculate-lisp-indent-last-sexp
1133 (goto-char (1+ containing-sexp))
1134 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
1136 (let ((parse-sexp-ignore-comments t)
1138 (goto-char calculate-lisp-indent-last-sexp)
1139 (or (and (looking-at ":")
1140 (setq indent (current-column)))
1141 (and (< (line-beginning-position)
1142 (prog2 (backward-sexp) (point)))
1144 (setq indent (current-column))))
1146 ;; another symbols or constants not preceded by a constant
1147 ;; as defined above.
1149 ;; in this case calculate-lisp-indent-last-sexp is nil
1154 (defun lisp-indent-function (indent-point state)
1155 "This function is the normal value of the variable `lisp-indent-function'.
1156 The function `calculate-lisp-indent' calls this to determine
1157 if the arguments of a Lisp function call should be indented specially.
1159 INDENT-POINT is the position where the user typed TAB, or equivalent.
1160 Point is located at the point to indent under (for default indentation);
1161 STATE is the `parse-partial-sexp' state for that position.
1163 If the current line is in a call to a Lisp function that has a non-nil
1164 property `lisp-indent-function' (or the deprecated `lisp-indent-hook'),
1165 it specifies how to indent. The property value can be:
1167 * `defun', meaning indent `defun'-style
1168 \(this is also the case if there is no property and the function
1169 has a name that begins with \"def\", and three or more arguments);
1171 * an integer N, meaning indent the first N arguments specially
1172 (like ordinary function arguments), and then indent any further
1173 arguments like a body;
1175 * a function to call that returns the indentation (or nil).
1176 `lisp-indent-function' calls this function with the same two arguments
1177 that it itself received.
1179 This function returns either the indentation to use, or nil if the
1180 Lisp function does not specify a special indentation."
1181 (let ((normal-indent (current-column)))
1182 (goto-char (1+ (elt state 1)))
1183 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
1184 (if (and (elt state 2)
1185 (not (looking-at "\\sw\\|\\s_")))
1186 ;; car of form doesn't seem to be a symbol
1188 (if (not (> (save-excursion (forward-line 1) (point))
1189 calculate-lisp-indent-last-sexp))
1190 (progn (goto-char calculate-lisp-indent-last-sexp)
1192 (parse-partial-sexp (point)
1193 calculate-lisp-indent-last-sexp 0 t)))
1194 ;; Indent under the list or under the first sexp on the same
1195 ;; line as calculate-lisp-indent-last-sexp. Note that first
1196 ;; thing on that line has to be complete sexp since we are
1197 ;; inside the innermost containing sexp.
1198 (backward-prefix-chars)
1200 (let ((function (buffer-substring (point)
1201 (progn (forward-sexp 1) (point))))
1203 (setq method (or (function-get (intern-soft function)
1204 'lisp-indent-function)
1205 (get (intern-soft function) 'lisp-indent-hook)))
1206 (cond ((or (eq method 'defun)
1208 (> (length function) 3)
1209 (string-match "\\`def" function)))
1210 (lisp-indent-defform state indent-point))
1212 (lisp-indent-specform method state
1213 indent-point normal-indent))
1215 (funcall method indent-point state)))))))
1217 (defcustom lisp-body-indent 2
1218 "Number of columns to indent the second line of a `(def...)' form."
1221 (put 'lisp-body-indent 'safe-local-variable 'integerp)
1223 (defun lisp-indent-specform (count state indent-point normal-indent)
1224 (let ((containing-form-start (elt state 1))
1226 body-indent containing-form-column)
1227 ;; Move to the start of containing form, calculate indentation
1228 ;; to use for non-distinguished forms (> count), and move past the
1229 ;; function symbol. lisp-indent-function guarantees that there is at
1230 ;; least one word or symbol character following open paren of containing
1232 (goto-char containing-form-start)
1233 (setq containing-form-column (current-column))
1234 (setq body-indent (+ lisp-body-indent containing-form-column))
1237 ;; Now find the start of the last form.
1238 (parse-partial-sexp (point) indent-point 1 t)
1239 (while (and (< (point) indent-point)
1242 (setq count (1- count))
1244 (parse-partial-sexp (point) indent-point 1 t))
1246 ;; Point is sitting on first character of last (or count) sexp.
1248 ;; A distinguished form. If it is the first or second form use double
1249 ;; lisp-body-indent, else normal indent. With lisp-body-indent bound
1250 ;; to 2 (the default), this just happens to work the same with if as
1251 ;; the older code, but it makes unwind-protect, condition-case,
1252 ;; with-output-to-temp-buffer, et. al. much more tasteful. The older,
1253 ;; less hacked, behavior can be obtained by replacing below with
1254 ;; (list normal-indent containing-form-start).
1255 (if (<= (- i count) 1)
1256 (list (+ containing-form-column (* 2 lisp-body-indent))
1257 containing-form-start)
1258 (list normal-indent containing-form-start))
1259 ;; A non-distinguished form. Use body-indent if there are no
1260 ;; distinguished forms and this is the first undistinguished form,
1261 ;; or if this is the first undistinguished form and the preceding
1262 ;; distinguished form has indentation at least as great as body-indent.
1263 (if (or (and (= i 0) (= count 0))
1264 (and (= count 0) (<= body-indent normal-indent)))
1268 (defun lisp-indent-defform (state indent-point)
1269 (goto-char (car (cdr state)))
1271 (if (> (point) (car (cdr (cdr state))))
1273 (goto-char (car (cdr state)))
1274 (+ lisp-body-indent (current-column)))))
1277 ;; (put 'progn 'lisp-indent-function 0), say, causes progn to be indented
1278 ;; like defun if the first form is placed on the next line, otherwise
1279 ;; it is indented like any other form (i.e. forms line up under first).
1281 (put 'autoload 'lisp-indent-function 'defun)
1282 (put 'progn 'lisp-indent-function 0)
1283 (put 'prog1 'lisp-indent-function 1)
1284 (put 'prog2 'lisp-indent-function 2)
1285 (put 'save-excursion 'lisp-indent-function 0)
1286 (put 'save-restriction 'lisp-indent-function 0)
1287 (put 'save-current-buffer 'lisp-indent-function 0)
1288 (put 'let 'lisp-indent-function 1)
1289 (put 'let* 'lisp-indent-function 1)
1290 (put 'while 'lisp-indent-function 1)
1291 (put 'if 'lisp-indent-function 2)
1292 (put 'catch 'lisp-indent-function 1)
1293 (put 'condition-case 'lisp-indent-function 2)
1294 (put 'unwind-protect 'lisp-indent-function 1)
1295 (put 'with-output-to-temp-buffer 'lisp-indent-function 1)
1297 (defun indent-sexp (&optional endpos)
1298 "Indent each line of the list starting just after point.
1299 If optional arg ENDPOS is given, indent each line, stopping when
1300 ENDPOS is encountered."
1302 (let ((indent-stack (list nil))
1304 ;; If ENDPOS is non-nil, use nil as STARTING-POINT
1305 ;; so that calculate-lisp-indent will find the beginning of
1306 ;; the defun we are in.
1307 ;; If ENDPOS is nil, it is safe not to scan before point
1308 ;; since every line we indent is more deeply nested than point is.
1309 (starting-point (if endpos nil (point)))
1310 (last-point (point))
1311 last-depth bol outer-loop-done inner-loop-done state this-indent)
1313 ;; Get error now if we don't have a complete sexp after point.
1314 (save-excursion (forward-sexp 1)))
1316 (setq outer-loop-done nil)
1317 (while (if endpos (< (point) endpos)
1318 (not outer-loop-done))
1319 (setq last-depth next-depth
1320 inner-loop-done nil)
1321 ;; Parse this line so we can learn the state
1322 ;; to indent the next line.
1323 ;; This inner loop goes through only once
1324 ;; unless a line ends inside a string.
1325 (while (and (not inner-loop-done)
1326 (not (setq outer-loop-done (eobp))))
1327 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
1329 (setq next-depth (car state))
1330 ;; If the line contains a comment other than the sort
1331 ;; that is indented like code,
1332 ;; indent it now with indent-for-comment.
1333 ;; Comments indented like code are right already.
1334 ;; In any case clear the in-comment flag in the state
1335 ;; because parse-partial-sexp never sees the newlines.
1336 (if (car (nthcdr 4 state))
1337 (progn (indent-for-comment)
1339 (setcar (nthcdr 4 state) nil)))
1340 ;; If this line ends inside a string,
1341 ;; go straight to next line, remaining within the inner loop,
1342 ;; and turn off the \-flag.
1343 (if (car (nthcdr 3 state))
1346 (setcar (nthcdr 5 state) nil))
1347 (setq inner-loop-done t)))
1351 (setq indent-stack (nconc indent-stack
1352 (make-list (- next-depth) nil))
1353 last-depth (- last-depth next-depth)
1356 ;; Decide whether to exit.
1358 ;; If we have already reached the specified end,
1359 ;; give up and do not reindent this line.
1360 (if (<= endpos (point))
1361 (setq outer-loop-done t))
1362 ;; If no specified end, we are done if we have finished one sexp.
1363 (if (<= next-depth 0)
1364 (setq outer-loop-done t)))
1365 (unless outer-loop-done
1366 (while (> last-depth next-depth)
1367 (setq indent-stack (cdr indent-stack)
1368 last-depth (1- last-depth)))
1369 (while (< last-depth next-depth)
1370 (setq indent-stack (cons nil indent-stack)
1371 last-depth (1+ last-depth)))
1372 ;; Now indent the next line according
1373 ;; to what we learned from parsing the previous one.
1375 (skip-chars-forward " \t")
1376 ;; But not if the line is blank, or just a comment
1377 ;; (except for double-semi comments; indent them as usual).
1378 (if (or (eobp) (looking-at "\\s<\\|\n"))
1380 (if (and (car indent-stack)
1381 (>= (car indent-stack) 0))
1382 (setq this-indent (car indent-stack))
1383 (let ((val (calculate-lisp-indent
1384 (if (car indent-stack) (- (car indent-stack))
1387 (setq this-indent val)
1389 (setcar indent-stack
1390 (setq this-indent val))
1391 (setcar indent-stack (- (car (cdr val))))
1392 (setq this-indent (car val))))))
1393 (if (and this-indent (/= (current-column) this-indent))
1394 (progn (delete-region bol (point))
1395 (indent-to this-indent)))))
1397 (setq outer-loop-done (= (point) last-point))
1398 (setq last-point (point)))))))
1400 (defun indent-pp-sexp (&optional arg)
1401 "Indent each line of the list starting just after point, or prettyprint it.
1402 A prefix argument specifies pretty-printing."
1407 (narrow-to-region (point) (progn (forward-sexp 1) (point)))
1409 (goto-char (point-max))
1410 (if (eq (char-before) ?\n)
1411 (delete-char -1)))))
1414 ;;;; Lisp paragraph filling commands.
1416 (defcustom emacs-lisp-docstring-fill-column 65
1417 "Value of `fill-column' to use when filling a docstring.
1418 Any non-integer value means do not use a different value of
1419 `fill-column' when filling docstrings."
1420 :type '(choice (integer)
1421 (const :tag "Use the current `fill-column'" t))
1424 (defun lisp-fill-paragraph (&optional justify)
1425 "Like \\[fill-paragraph], but handle Emacs Lisp comments and docstrings.
1426 If any of the current line is a comment, fill the comment or the
1427 paragraph of it that point is in, preserving the comment's indentation
1428 and initial semicolons."
1430 (or (fill-comment-paragraph justify)
1431 ;; Since fill-comment-paragraph returned nil, that means we're not in
1432 ;; a comment: Point is on a program line; we are interested
1433 ;; particularly in docstring lines.
1435 ;; We bind `paragraph-start' and `paragraph-separate' temporarily. They
1436 ;; are buffer-local, but we avoid changing them so that they can be set
1437 ;; to make `forward-paragraph' and friends do something the user wants.
1439 ;; `paragraph-start': The `(' in the character alternative and the
1440 ;; left-singlequote plus `(' sequence after the \\| alternative prevent
1441 ;; sexps and backquoted sexps that follow a docstring from being filled
1442 ;; with the docstring. This setting has the consequence of inhibiting
1443 ;; filling many program lines that are not docstrings, which is sensible,
1444 ;; because the user probably asked to fill program lines by accident, or
1445 ;; expecting indentation (perhaps we should try to do indenting in that
1446 ;; case). The `;' and `:' stop the paragraph being filled at following
1447 ;; comment lines and at keywords (e.g., in `defcustom'). Left parens are
1448 ;; escaped to keep font-locking, filling, & paren matching in the source
1451 ;; `paragraph-separate': A clever regexp distinguishes the first line of
1452 ;; a docstring and identifies it as a paragraph separator, so that it
1453 ;; won't be filled. (Since the first line of documentation stands alone
1454 ;; in some contexts, filling should not alter the contents the author has
1455 ;; chosen.) Only the first line of a docstring begins with whitespace
1456 ;; and a quotation mark and ends with a period or (rarely) a comma.
1458 ;; The `fill-column' is temporarily bound to
1459 ;; `emacs-lisp-docstring-fill-column' if that value is an integer.
1460 (let ((paragraph-start (concat paragraph-start
1461 "\\|\\s-*\\([(;:\"]\\|`(\\|#'(\\)"))
1463 (concat paragraph-separate "\\|\\s-*\".*[,\\.]$"))
1464 (fill-column (if (and (integerp emacs-lisp-docstring-fill-column)
1465 (derived-mode-p 'emacs-lisp-mode))
1466 emacs-lisp-docstring-fill-column
1468 (fill-paragraph justify))
1469 ;; Never return nil.
1472 (defun indent-code-rigidly (start end arg &optional nochange-regexp)
1473 "Indent all lines of code, starting in the region, sideways by ARG columns.
1474 Does not affect lines starting inside comments or strings, assuming that
1475 the start of the region is not inside them.
1477 Called from a program, takes args START, END, COLUMNS and NOCHANGE-REGEXP.
1478 The last is a regexp which, if matched at the beginning of a line,
1479 means don't indent that line."
1480 (interactive "r\np")
1484 (setq end (point-marker))
1487 (setq state (parse-partial-sexp (point)
1489 (forward-line 1) (point))
1491 (while (< (point) end)
1492 (or (car (nthcdr 3 state))
1493 (and nochange-regexp
1494 (looking-at nochange-regexp))
1495 ;; If line does not start in string, indent it
1496 (let ((indent (current-indentation)))
1497 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
1499 (indent-to (max 0 (+ indent arg)) 0))))
1500 (setq state (parse-partial-sexp (point)
1502 (forward-line 1) (point))
1505 (provide 'lisp-mode)
1507 ;;; lisp-mode.el ends here