1 ;;; lisp-mode.el --- Lisp mode, and its idiosyncratic commands
3 ;; Copyright (C) 1985, 1986, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
7 ;; 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
)
38 (define-abbrev-table 'lisp-mode-abbrev-table
())
40 (defvar emacs-lisp-mode-syntax-table
41 (let ((table (make-syntax-table)))
44 (modify-syntax-entry i
"_ " table
)
48 (modify-syntax-entry i
"_ " table
)
52 (modify-syntax-entry i
"_ " table
)
56 (modify-syntax-entry i
"_ " table
)
58 (modify-syntax-entry ?\s
" " table
)
59 ;; Non-break space acts as whitespace.
60 (modify-syntax-entry ?
\x8a0 " " table
)
61 (modify-syntax-entry ?
\t " " table
)
62 (modify-syntax-entry ?
\f " " table
)
63 (modify-syntax-entry ?
\n "> " table
)
64 ;; This is probably obsolete since nowadays such features use overlays.
65 ;; ;; Give CR the same syntax as newline, for selective-display.
66 ;; (modify-syntax-entry ?\^m "> " table)
67 (modify-syntax-entry ?\
; "< " table)
68 (modify-syntax-entry ?
` "' " table
)
69 (modify-syntax-entry ?
' "' " table
)
70 (modify-syntax-entry ?
, "' " table
)
71 (modify-syntax-entry ?
@ "' " table
)
72 ;; Used to be singlequote; changed for flonums.
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 (modify-syntax-entry ?\
) ")( " table
)
79 (modify-syntax-entry ?\
[ "(] " table
)
80 (modify-syntax-entry ?\
] ")[ " table
))
83 (defvar lisp-mode-syntax-table
84 (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table
)))
85 (modify-syntax-entry ?\
[ "_ " table
)
86 (modify-syntax-entry ?\
] "_ " table
)
87 (modify-syntax-entry ?
# "' 14b" table
)
88 (modify-syntax-entry ?|
"\" 23bn" table
)
91 (defvar lisp-imenu-generic-expression
94 (purecopy (concat "^\\s-*("
97 '("defun" "defun*" "defsubst" "defmacro"
98 "defadvice" "define-skeleton"
99 "define-minor-mode" "define-global-minor-mode"
100 "define-globalized-minor-mode"
101 "define-derived-mode" "define-generic-mode"
102 "define-compiler-macro" "define-modify-macro"
103 "defsetf" "define-setf-expander"
104 "define-method-combination"
105 "defgeneric" "defmethod") t
))
106 "\\s-+\\(\\(\\sw\\|\\s_\\)+\\)"))
108 (list (purecopy "Variables")
109 (purecopy (concat "^\\s-*("
112 '("defvar" "defconst" "defconstant" "defcustom"
113 "defparameter" "define-symbol-macro") t
))
114 "\\s-+\\(\\(\\sw\\|\\s_\\)+\\)"))
116 (list (purecopy "Types")
117 (purecopy (concat "^\\s-*("
120 '("defgroup" "deftheme" "deftype" "defstruct"
121 "defclass" "define-condition" "define-widget"
122 "defface" "defpackage") t
))
123 "\\s-+'?\\(\\(\\sw\\|\\s_\\)+\\)"))
126 "Imenu generic expression for Lisp mode. See `imenu-generic-expression'.")
128 ;; This was originally in autoload.el and is still used there.
129 (put 'autoload
'doc-string-elt
3)
130 (put 'defun
'doc-string-elt
3)
131 (put 'defun
* 'doc-string-elt
3)
132 (put 'defvar
'doc-string-elt
3)
133 (put 'defcustom
'doc-string-elt
3)
134 (put 'deftheme
'doc-string-elt
2)
135 (put 'defconst
'doc-string-elt
3)
136 (put 'defmacro
'doc-string-elt
3)
137 (put 'defmacro
* 'doc-string-elt
3)
138 (put 'defsubst
'doc-string-elt
3)
139 (put 'defstruct
'doc-string-elt
2)
140 (put 'define-skeleton
'doc-string-elt
2)
141 (put 'define-derived-mode
'doc-string-elt
4)
142 (put 'define-compilation-mode
'doc-string-elt
3)
143 (put 'easy-mmode-define-minor-mode
'doc-string-elt
2)
144 (put 'define-minor-mode
'doc-string-elt
2)
145 (put 'easy-mmode-define-global-mode
'doc-string-elt
2)
146 (put 'define-global-minor-mode
'doc-string-elt
2)
147 (put 'define-globalized-minor-mode
'doc-string-elt
2)
148 (put 'define-generic-mode
'doc-string-elt
7)
149 (put 'define-ibuffer-filter
'doc-string-elt
2)
150 (put 'define-ibuffer-op
'doc-string-elt
3)
151 (put 'define-ibuffer-sorter
'doc-string-elt
2)
152 (put 'lambda
'doc-string-elt
2)
153 (put 'defalias
'doc-string-elt
3)
154 (put 'defvaralias
'doc-string-elt
3)
155 (put 'define-category
'doc-string-elt
2)
157 (defvar lisp-doc-string-elt-property
'doc-string-elt
158 "The symbol property that holds the docstring position info.")
160 (defun lisp-font-lock-syntactic-face-function (state)
162 ;; This might be a (doc)string or a |...| symbol.
163 (let ((startpos (nth 8 state
)))
164 (if (eq (char-after startpos
) ?|
)
165 ;; This is not a string, but a |...| symbol.
167 (let* ((listbeg (nth 1 state
))
168 (firstsym (and listbeg
171 (and (looking-at "([ \t\n]*\\(\\(\\sw\\|\\s_\\)+\\)")
173 (docelt (and firstsym
(get (intern-soft firstsym
)
174 lisp-doc-string-elt-property
))))
176 ;; It's a string in a form that can have a docstring.
177 ;; Check whether it's in docstring position.
179 (when (functionp docelt
)
180 (goto-char (match-end 1))
181 (setq docelt
(funcall docelt
)))
185 (while (and (> docelt
0) (< (point) startpos
)
186 (progn (forward-sexp 1) t
))
187 (setq docelt
(1- docelt
)))
189 (and (zerop docelt
) (<= (point) startpos
)
190 (progn (forward-comment (point-max)) t
)
191 (= (point) (nth 8 state
)))))
193 font-lock-string-face
))))
194 font-lock-comment-face
))
196 (defun lisp-mode-variables (&optional lisp-syntax keywords-case-insensitive
)
197 "Common initialization routine for lisp modes.
198 The LISP-SYNTAX argument is used by code in inf-lisp.el and is
199 (uselessly) passed from pp.el, chistory.el, gnus-kill.el and score-mode.el
200 KEYWORDS-CASE-INSENSITIVE means that for font-lock keywords will not
203 (set-syntax-table lisp-mode-syntax-table
))
204 (setq local-abbrev-table lisp-mode-abbrev-table
)
205 (make-local-variable 'paragraph-ignore-fill-prefix
)
206 (setq paragraph-ignore-fill-prefix t
)
207 (make-local-variable 'fill-paragraph-function
)
208 (setq fill-paragraph-function
'lisp-fill-paragraph
)
209 ;; Adaptive fill mode gets the fill wrong for a one-line paragraph made of
210 ;; a single docstring. Let's fix it here.
211 (set (make-local-variable 'adaptive-fill-function
)
212 (lambda () (if (looking-at "\\s-+\"[^\n\"]+\"\\s-*$") "")))
213 ;; Adaptive fill mode gets in the way of auto-fill,
214 ;; and should make no difference for explicit fill
215 ;; because lisp-fill-paragraph should do the job.
216 ;; I believe that newcomment's auto-fill code properly deals with it -stef
217 ;;(set (make-local-variable 'adaptive-fill-mode) nil)
218 (make-local-variable 'indent-line-function
)
219 (setq indent-line-function
'lisp-indent-line
)
220 (make-local-variable 'parse-sexp-ignore-comments
)
221 (setq parse-sexp-ignore-comments t
)
222 (make-local-variable 'outline-regexp
)
223 (setq outline-regexp
";;;\\(;* [^ \t\n]\\|###autoload\\)\\|(")
224 (make-local-variable 'outline-level
)
225 (setq outline-level
'lisp-outline-level
)
226 (make-local-variable 'comment-start
)
227 (setq comment-start
";")
228 (make-local-variable 'comment-start-skip
)
229 ;; Look within the line for a ; following an even number of backslashes
230 ;; after either a non-backslash or the line beginning.
231 (setq comment-start-skip
"\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\);+ *")
232 (make-local-variable 'font-lock-comment-start-skip
)
233 ;; Font lock mode uses this only when it KNOWS a comment is starting.
234 (setq font-lock-comment-start-skip
";+ *")
235 (make-local-variable 'comment-add
)
236 (setq comment-add
1) ;default to `;;' in comment-region
237 (make-local-variable 'comment-column
)
238 (setq comment-column
40)
239 ;; Don't get confused by `;' in doc strings when paragraph-filling.
240 (set (make-local-variable 'comment-use-global-state
) t
)
241 (make-local-variable 'imenu-generic-expression
)
242 (setq imenu-generic-expression lisp-imenu-generic-expression
)
243 (make-local-variable 'multibyte-syntax-as-symbol
)
244 (setq multibyte-syntax-as-symbol t
)
245 (set (make-local-variable 'syntax-begin-function
) 'beginning-of-defun
)
246 (setq font-lock-defaults
247 `((lisp-font-lock-keywords
248 lisp-font-lock-keywords-1 lisp-font-lock-keywords-2
)
249 nil
,keywords-case-insensitive
(("+-*/.<>=!?$%_&~^:@" .
"w")) nil
250 (font-lock-mark-block-function . mark-defun
)
251 (font-lock-syntactic-face-function
252 . lisp-font-lock-syntactic-face-function
))))
254 (defun lisp-outline-level ()
255 "Lisp mode `outline-level' function."
256 (let ((len (- (match-end 0) (match-beginning 0))))
257 (if (looking-at "(\\|;;;###autoload")
261 (defvar lisp-mode-shared-map
262 (let ((map (make-sparse-keymap)))
263 (define-key map
"\e\C-q" 'indent-sexp
)
264 (define-key map
"\177" 'backward-delete-char-untabify
)
265 ;; This gets in the way when viewing a Lisp file in view-mode. As
266 ;; long as [backspace] is mapped into DEL via the
267 ;; function-key-map, this should remain disabled!!
268 ;;;(define-key map [backspace] 'backward-delete-char-untabify)
270 "Keymap for commands shared by all sorts of Lisp modes.")
272 (defvar emacs-lisp-mode-map
273 (let ((map (make-sparse-keymap "Emacs-Lisp"))
274 (menu-map (make-sparse-keymap "Emacs-Lisp"))
275 (prof-map (make-sparse-keymap))
276 (tracing-map (make-sparse-keymap)))
277 (set-keymap-parent map lisp-mode-shared-map
)
278 (define-key map
"\e\t" 'lisp-complete-symbol
)
279 (define-key map
"\e\C-x" 'eval-defun
)
280 (define-key map
"\e\C-q" 'indent-pp-sexp
)
281 (define-key map
[menu-bar emacs-lisp
] (cons "Emacs-Lisp" menu-map
))
282 (define-key menu-map
[eldoc]
283 '(menu-item "Auto-Display Documentation Strings" eldoc-mode
284 :button (:toggle . (bound-and-true-p eldoc-mode))
285 :help "Display the documentation string for the item under cursor"))
286 (define-key menu-map [checkdoc]
287 '(menu-item "Check Documentation Strings" checkdoc
288 :help "Check documentation strings for style requirements"))
289 (define-key menu-map [re-builder]
290 '(menu-item "Construct Regexp" re-builder
291 :help "Construct a regexp interactively"))
292 (define-key menu-map [tracing] (cons "Tracing" tracing-map))
293 (define-key tracing-map [tr-a]
294 '(menu-item "Untrace all" untrace-all
295 :help "Untrace all currently traced functions"))
296 (define-key tracing-map [tr-uf]
297 '(menu-item "Untrace function..." untrace-function
298 :help "Untrace function, and possibly activate all remaining advice"))
299 (define-key tracing-map [tr-sep] '("--"))
300 (define-key tracing-map [tr-q]
301 '(menu-item "Trace function quietly..." trace-function-background
302 :help "Trace the function with trace output going quietly to a buffer"))
303 (define-key tracing-map [tr-f]
304 '(menu-item "Trace function..." trace-function
305 :help "Trace the function given as an argument"))
306 (define-key menu-map [profiling] (cons "Profiling" prof-map))
307 (define-key prof-map [prof-restall]
308 '(menu-item "Remove Instrumentation for All Functions" elp-restore-all
309 :help "Restore the original definitions of all functions being profiled"))
310 (define-key prof-map [prof-restfunc]
311 '(menu-item "Remove Instrumentation for Function..." elp-restore-function
312 :help "Restore an instrumented function to its original definition"))
314 (define-key prof-map [sep-rem] '("--"))
315 (define-key prof-map [prof-resall]
316 '(menu-item "Reset Counters for All Functions" elp-reset-all
317 :help "Reset the profiling information for all functions being profiled"))
318 (define-key prof-map [prof-resfunc]
319 '(menu-item "Reset Counters for Function..." elp-reset-function
320 :help "Reset the profiling information for a function"))
321 (define-key prof-map [prof-res]
322 '(menu-item "Show Profiling Results" elp-results
323 :help "Display current profiling results"))
324 (define-key prof-map [prof-pack]
325 '(menu-item "Instrument Package..." elp-instrument-package
326 :help "Instrument for profiling all function that start with a prefix"))
327 (define-key prof-map [prof-func]
328 '(menu-item "Instrument Function..." elp-instrument-function
329 :help "Instrument a function for profiling"))
330 (define-key menu-map [edebug-defun]
331 '(menu-item "Instrument Function for Debugging" edebug-defun
332 :help "Evaluate the top level form point is in, stepping through with Edebug"
334 (define-key menu-map [separator-byte] '("--"))
335 (define-key menu-map [disas]
336 '(menu-item "Disassemble byte compiled object..." disassemble
337 :help "Print disassembled code for OBJECT in a buffer"))
338 (define-key menu-map [byte-recompile]
339 '(menu-item "Byte-recompile Directory..." byte-recompile-directory
340 :help "Recompile every `.el' file in DIRECTORY that needs recompilation"))
341 (define-key menu-map [emacs-byte-compile-and-load]
342 '(menu-item "Byte-compile And Load" emacs-lisp-byte-compile-and-load
343 :help "Byte-compile the current file (if it has changed), then load compiled code"))
344 (define-key menu-map [byte-compile]
345 '(menu-item "Byte-compile This File" emacs-lisp-byte-compile
346 :help "Byte compile the file containing the current buffer"))
347 (define-key menu-map [separator-eval] '("--"))
348 (define-key menu-map [ielm]
349 '(menu-item "Interactive Expression Evaluation" ielm
350 :help "Interactively evaluate Emacs Lisp expressions"))
351 (define-key menu-map [eval-buffer]
352 '(menu-item "Evaluate Buffer" eval-buffer
353 :help "Execute the current buffer as Lisp code"))
354 (define-key menu-map [eval-region]
355 '(menu-item "Evaluate Region" eval-region
356 :help "Execute the region as Lisp code"
357 :enable mark-active))
358 (define-key menu-map [eval-sexp]
359 '(menu-item "Evaluate Last S-expression" eval-last-sexp
360 :help "Evaluate sexp before point; print value in minibuffer"))
361 (define-key menu-map [separator-format] '("--"))
362 (define-key menu-map [comment-region]
363 '(menu-item "Comment Out Region" comment-region
364 :help "Comment or uncomment each line in the region"
365 :enable mark-active))
366 (define-key menu-map [indent-region]
367 '(menu-item "Indent Region" indent-region
368 :help "Indent each nonblank line in the region"
369 :enable mark-active))
370 (define-key menu-map [indent-line] '("Indent Line" . lisp-indent-line))
372 "Keymap for Emacs Lisp mode.
373 All commands in `lisp-mode-shared-map' are inherited by this map.")
375 (defun emacs-lisp-byte-compile ()
376 "Byte compile the file containing the current buffer."
379 (byte-compile-file buffer-file-name)
380 (error "The buffer must be saved in a file first")))
382 (defun emacs-lisp-byte-compile-and-load ()
383 "Byte-compile the current file (if it has changed), then load compiled code."
386 (error "The buffer must be saved in a file first"))
388 ;; Recompile if file or buffer has changed since last compilation.
389 (if (and (buffer-modified-p)
390 (y-or-n-p (format "Save buffer %s first? " (buffer-name))))
392 (let ((compiled-file-name (byte-compile-dest-file buffer-file-name)))
393 (if (file-newer-than-file-p compiled-file-name buffer-file-name)
394 (load-file compiled-file-name)
395 (byte-compile-file buffer-file-name t))))
397 (defcustom emacs-lisp-mode-hook nil
398 "Hook run when entering Emacs Lisp mode."
399 :options '(turn-on-eldoc-mode imenu-add-menubar-index checkdoc-minor-mode)
403 (defcustom lisp-mode-hook nil
404 "Hook run when entering Lisp mode."
405 :options '(imenu-add-menubar-index)
409 (defcustom lisp-interaction-mode-hook nil
410 "Hook run when entering Lisp Interaction mode."
411 :options '(turn-on-eldoc-mode)
415 (defun emacs-lisp-mode ()
416 "Major mode for editing Lisp code to run in Emacs.
418 Delete converts tabs to spaces as it moves back.
419 Blank lines separate paragraphs. Semicolons start comments.
421 \\{emacs-lisp-mode-map}
422 Entry to this mode calls the value of `emacs-lisp-mode-hook'
423 if that value is non-nil."
425 (kill-all-local-variables)
426 (use-local-map emacs-lisp-mode-map)
427 (set-syntax-table emacs-lisp-mode-syntax-table)
428 (setq major-mode 'emacs-lisp-mode)
429 (setq mode-name "Emacs-Lisp")
430 (lisp-mode-variables)
431 (setq imenu-case-fold-search nil)
432 (run-mode-hooks 'emacs-lisp-mode-hook))
433 (put 'emacs-lisp-mode 'custom-mode-group 'lisp)
435 (defvar lisp-mode-map
436 (let ((map (make-sparse-keymap))
437 (menu-map (make-sparse-keymap "Lisp")))
438 (set-keymap-parent map lisp-mode-shared-map)
439 (define-key map "\e\C-x" 'lisp-eval-defun)
440 (define-key map "\C-c\C-z" 'run-lisp)
441 (define-key map [menu-bar lisp] (cons "Lisp" menu-map))
442 (define-key menu-map [run-lisp]
443 '(menu-item "Run inferior Lisp" run-lisp
444 :help "Run an inferior Lisp process, input and output via buffer `*inferior-lisp*'"))
445 (define-key menu-map [ev-def]
446 '(menu-item "Eval defun" lisp-eval-defun
447 :help "Send the current defun to the Lisp process made by M-x run-lisp"))
448 (define-key menu-map [ind-sexp]
449 '(menu-item "Indent sexp" indent-sexp
450 :help "Indent each line of the list starting just after point"))
452 "Keymap for ordinary Lisp mode.
453 All commands in `lisp-mode-shared-map' are inherited by this map.")
456 "Major mode for editing Lisp code for Lisps other than GNU Emacs Lisp.
458 Delete converts tabs to spaces as it moves back.
459 Blank lines separate paragraphs. Semicolons start comments.
462 Note that `run-lisp' may be used either to start an inferior Lisp job
463 or to switch back to an existing one.
465 Entry to this mode calls the value of `lisp-mode-hook'
466 if that value is non-nil."
468 (kill-all-local-variables)
469 (use-local-map lisp-mode-map)
470 (setq major-mode 'lisp-mode)
471 (setq mode-name "Lisp")
472 (lisp-mode-variables nil t)
473 (make-local-variable 'comment-start-skip)
474 (setq comment-start-skip
475 "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\)\\(;+\\|#|\\) *")
476 (setq imenu-case-fold-search t)
477 (set-syntax-table lisp-mode-syntax-table)
478 (run-mode-hooks 'lisp-mode-hook))
479 (put 'lisp-mode 'find-tag-default-function 'lisp-find-tag-default)
481 (defun lisp-find-tag-default ()
482 (let ((default (find-tag-default)))
483 (when (stringp default)
484 (if (string-match ":+" default)
485 (substring default (match-end 0))
488 ;; Used in old LispM code.
489 (defalias 'common-lisp-mode 'lisp-mode)
491 ;; This will do unless inf-lisp.el is loaded.
492 (defun lisp-eval-defun (&optional and-go)
493 "Send the current defun to the Lisp process made by \\[run-lisp]."
495 (error "Process lisp does not exist"))
497 (defvar lisp-interaction-mode-map
498 (let ((map (make-sparse-keymap))
499 (menu-map (make-sparse-keymap "Lisp-Interaction")))
500 (set-keymap-parent map lisp-mode-shared-map)
501 (define-key map "\e\C-x" 'eval-defun)
502 (define-key map "\e\C-q" 'indent-pp-sexp)
503 (define-key map "\e\t" 'lisp-complete-symbol)
504 (define-key map "\n" 'eval-print-last-sexp)
505 (define-key map [menu-bar lisp-interaction] (cons "Lisp-Interaction" menu-map))
506 (define-key menu-map [eval-defun]
507 '(menu-item "Evaluate Defun" eval-defun
508 :help "Evaluate the top-level form containing point, or after point"))
509 (define-key menu-map [eval-print-last-sexp]
510 '(menu-item "Evaluate and print" eval-print-last-sexp
511 :help "Evaluate sexp before point; print value into current buffer"))
512 (define-key menu-map [edebug-defun-lisp-interaction]
513 '(menu-item "Instrument Function for Debugging" edebug-defun
514 :help "Evaluate the top level form point is in, stepping through with Edebug"
516 (define-key menu-map [indent-pp-sexp]
517 '(menu-item "Indent or Pretty-Print" indent-pp-sexp
518 :help "Indent each line of the list starting just after point, or prettyprint it"))
519 (define-key menu-map [lisp-complete-symbol]
520 '(menu-item "Complete Lisp Symbol" lisp-complete-symbol
521 :help "Perform completion on Lisp symbol preceding point"))
523 "Keymap for Lisp Interaction mode.
524 All commands in `lisp-mode-shared-map' are inherited by this map.")
526 (defvar lisp-interaction-mode-abbrev-table lisp-mode-abbrev-table)
527 (define-derived-mode lisp-interaction-mode emacs-lisp-mode "Lisp Interaction"
528 "Major mode for typing and evaluating Lisp forms.
529 Like Lisp mode except that \\[eval-print-last-sexp] evals the Lisp expression
530 before point, and prints its value into the buffer, advancing point.
531 Note that printing is controlled by `eval-expression-print-length'
532 and `eval-expression-print-level'.
535 Delete converts tabs to spaces as it moves back.
536 Paragraphs are separated only by blank lines.
537 Semicolons start comments.
539 \\{lisp-interaction-mode-map}
540 Entry to this mode calls the value of `lisp-interaction-mode-hook'
541 if that value is non-nil.")
543 (defun eval-print-last-sexp ()
544 "Evaluate sexp before point; print value into current buffer.
546 If `eval-expression-debug-on-error' is non-nil, which is the default,
547 this command arranges for all errors to enter the debugger.
549 Note that printing the result is controlled by the variables
550 `eval-expression-print-length' and `eval-expression-print-level',
553 (let ((standard-output (current-buffer)))
559 (defun last-sexp-setup-props (beg end value alt1 alt2)
560 "Set up text properties for the output of `eval-last-sexp-1'.
561 BEG and END are the start and end of the output in current-buffer.
562 VALUE is the Lisp value printed, ALT1 and ALT2 are strings for the
563 alternative printed representations that can be displayed."
564 (let ((map (make-sparse-keymap)))
565 (define-key map "\C-m" 'last-sexp-toggle-display)
566 (define-key map [down-mouse-2] 'mouse-set-point)
567 (define-key map [mouse-2] 'last-sexp-toggle-display)
570 `(printed-value (,value ,alt1 ,alt2)
573 help-echo "RET, mouse-2: toggle abbreviated display"
574 rear-nonsticky (mouse-face keymap help-echo
578 (defun last-sexp-toggle-display (&optional arg)
579 "Toggle between abbreviated and unabbreviated printed representations."
583 (let ((value (get-text-property (point) 'printed-value)))
585 (let ((beg (or (previous-single-property-change (min (point-max) (1+ (point)))
588 (end (or (next-single-char-property-change (point) 'printed-value) (point)))
589 (standard-output (current-buffer))
591 (delete-region beg end)
592 (insert (nth 1 value))
594 (setq point (1- (point))))
595 (last-sexp-setup-props beg (point)
599 (goto-char (min (point-max) point)))))))
601 (defun prin1-char (char)
602 "Return a string representing CHAR as a character rather than as an integer.
603 If CHAR is not a character, return nil."
606 (let ((c (event-basic-type char))
607 (mods (event-modifiers char))
609 ;; Prevent ?A from turning into ?\S-a.
610 (if (and (memq 'shift mods)
611 (zerop (logand char ?\S-\^@))
612 (not (let ((case-fold-search nil))
613 (char-equal c (upcase c)))))
614 (setq c (upcase c) mods nil))
615 ;; What string are we considering using?
622 (cond ((eq modif 'super) "\\s-")
623 (t (string ?\\ (upcase (aref (symbol-name modif) 0)) ?-))))
626 ((memq c '(?\; ?\( ?\) ?\{ ?\} ?\[ ?\] ?\" ?\' ?\\)) (string ?\\ c))
631 ;; Verify the string reads a CHAR, not to some other character.
632 ;; If it doesn't, return nil instead.
634 (= (car (read-from-string string)) char)
638 (defun preceding-sexp ()
639 "Return sexp before the point."
640 (let ((opoint (point))
644 (with-syntax-table emacs-lisp-mode-syntax-table
645 ;; If this sexp appears to be enclosed in `...'
646 ;; then ignore the surrounding quotes.
648 (or (eq (following-char) ?\')
649 (eq (preceding-char) ?\')))
651 ;; If we were after `?\e' (or similar case),
652 ;; use the whole thing, not just the `e'.
653 (when (eq (preceding-char) ?\\)
655 (when (eq (preceding-char) ??)
659 (when (eq (preceding-char) ?=)
662 (skip-chars-backward "0-9#=")
663 (setq labeled-p (looking-at "\\(#[0-9]+=\\)+")))
668 ;; vladimir@cs.ualberta.ca 30-Jul-1997: skip ` in
669 ;; `variable' so that the value is returned, not the
671 (if (and ignore-quotes
672 (eq (following-char) ?`))
674 (narrow-to-region (point-min) opoint)
675 (setq expr (read (current-buffer)))
676 ;; If it's an (interactive ...) form, it's more
677 ;; useful to show how an interactive call would
680 (eq (car expr) 'interactive)
682 (list 'call-interactively
691 (defun eval-last-sexp-1 (eval-last-sexp-arg-internal)
692 "Evaluate sexp before point; print value in minibuffer.
693 With argument, print output into current buffer."
694 (let ((standard-output (if eval-last-sexp-arg-internal (current-buffer) t)))
695 (eval-last-sexp-print-value (eval (preceding-sexp)))))
698 (defun eval-last-sexp-print-value (value)
699 (let ((unabbreviated (let ((print-length nil) (print-level nil))
700 (prin1-to-string value)))
701 (print-length eval-expression-print-length)
702 (print-level eval-expression-print-level)
707 (let ((str (eval-expression-print-format value)))
708 (if str (princ str)))
710 (when (and (bufferp standard-output)
711 (or (not (null print-length))
712 (not (null print-level)))
713 (not (string= unabbreviated
714 (buffer-substring-no-properties beg end))))
715 (last-sexp-setup-props beg end value
717 (buffer-substring-no-properties beg end))
721 (defvar eval-last-sexp-fake-value (make-symbol "t"))
723 (defun eval-last-sexp (eval-last-sexp-arg-internal)
724 "Evaluate sexp before point; print value in minibuffer.
725 Interactively, with prefix argument, print output into current buffer.
727 If `eval-expression-debug-on-error' is non-nil, which is the default,
728 this command arranges for all errors to enter the debugger."
730 (if (null eval-expression-debug-on-error)
731 (eval-last-sexp-1 eval-last-sexp-arg-internal)
733 (let ((debug-on-error eval-last-sexp-fake-value))
734 (cons (eval-last-sexp-1 eval-last-sexp-arg-internal)
736 (unless (eq (cdr value) eval-last-sexp-fake-value)
737 (setq debug-on-error (cdr value)))
740 (defun eval-defun-1 (form)
741 "Treat some expressions specially.
742 Reset the `defvar' and `defcustom' variables to the initial value.
743 Reinitialize the face according to the `defface' specification."
744 ;; The code in edebug-defun should be consistent with this, but not
745 ;; the same, since this gets a macroexpended form.
746 (cond ((not (listp form))
748 ((and (eq (car form) 'defvar)
749 (cdr-safe (cdr-safe form))
750 (boundp (cadr form)))
751 ;; Force variable to be re-set.
752 `(progn (defvar ,(nth 1 form) nil ,@(nthcdr 3 form))
753 (setq-default ,(nth 1 form) ,(nth 2 form))))
754 ;; `defcustom' is now macroexpanded to
755 ;; `custom-declare-variable' with a quoted value arg.
756 ((and (eq (car form) 'custom-declare-variable)
757 (default-boundp (eval (nth 1 form))))
758 ;; Force variable to be bound.
759 (set-default (eval (nth 1 form)) (eval (nth 1 (nth 2 form))))
761 ;; `defface' is macroexpanded to `custom-declare-face'.
762 ((eq (car form) 'custom-declare-face)
764 (setq face-new-frame-defaults
765 (assq-delete-all (eval (nth 1 form)) face-new-frame-defaults))
766 (put (eval (nth 1 form)) 'face-defface-spec nil)
767 ;; Setting `customized-face' to the new spec after calling
768 ;; the form, but preserving the old saved spec in `saved-face',
769 ;; imitates the situation when the new face spec is set
770 ;; temporarily for the current session in the customize
771 ;; buffer, thus allowing `face-user-default-spec' to use the
772 ;; new customized spec instead of the saved spec.
773 ;; Resetting `saved-face' temporarily to nil is needed to let
774 ;; `defface' change the spec, regardless of a saved spec.
776 (put ,(nth 1 form) 'saved-face
777 ',(get (eval (nth 1 form)) 'saved-face))
778 (put ,(nth 1 form) 'customized-face
780 (put (eval (nth 1 form)) 'saved-face nil)))
781 ((eq (car form) 'progn)
782 (cons 'progn (mapcar 'eval-defun-1 (cdr form))))
785 (defun eval-defun-2 ()
786 "Evaluate defun that point is in or before.
787 The value is displayed in the minibuffer.
788 If the current defun is actually a call to `defvar',
789 then reset the variable using the initial value expression
790 even if the variable already has some other value.
791 \(Normally `defvar' does not change the variable's value
792 if it already has a value.\)
794 With argument, insert value in current buffer after the defun.
795 Return the result of evaluation."
797 ;; FIXME: the print-length/level bindings should only be applied while
798 ;; printing, not while evaluating.
799 (let ((debug-on-error eval-expression-debug-on-error)
800 (print-length eval-expression-print-length)
801 (print-level eval-expression-print-level))
803 ;; Arrange for eval-region to "read" the (possibly) altered form.
804 ;; eval-region handles recording which file defines a function or
805 ;; variable. Re-written using `apply' to avoid capturing
806 ;; variables like `end'.
809 (let ((standard-output t)
811 ;; Read the form from the buffer, and record where it ends.
816 (setq form (read (current-buffer)))
818 ;; Alter the form if necessary.
819 (setq form (eval-defun-1 (macroexpand form)))
820 (list beg end standard-output
822 ;; Skipping to the end of the specified region
823 ;; will make eval-region return.
826 ;; The result of evaluation has been put onto VALUES. So return it.
829 (defun eval-defun (edebug-it)
830 "Evaluate the top-level form containing point, or after point.
832 If the current defun is actually a call to `defvar' or `defcustom',
833 evaluating it this way resets the variable using its initial value
834 expression even if the variable already has some other value.
835 \(Normally `defvar' and `defcustom' do not alter the value if there
836 already is one.) In an analogous way, evaluating a `defface'
837 overrides any customizations of the face, so that it becomes
838 defined exactly as the `defface' expression says.
840 If `eval-expression-debug-on-error' is non-nil, which is the default,
841 this command arranges for all errors to enter the debugger.
843 With a prefix argument, instrument the code for Edebug.
845 If acting on a `defun' for FUNCTION, and the function was
846 instrumented, `Edebug: FUNCTION' is printed in the minibuffer. If not
847 instrumented, just FUNCTION is printed.
849 If not acting on a `defun', the result of evaluation is displayed in
850 the minibuffer. This display is controlled by the variables
851 `eval-expression-print-length' and `eval-expression-print-level',
856 (eval-defun (not edebug-all-defs)))
858 (if (null eval-expression-debug-on-error)
860 (let ((old-value (make-symbol "t")) new-value value)
861 (let ((debug-on-error old-value))
862 (setq value (eval-defun-2))
863 (setq new-value debug-on-error))
864 (unless (eq old-value new-value)
865 (setq debug-on-error new-value))
868 ;; May still be used by some external Lisp-mode variant.
869 (define-obsolete-function-alias 'lisp-comment-indent
870 'comment-indent-default "22.1")
871 (define-obsolete-function-alias 'lisp-mode-auto-fill 'do-auto-fill "23.1")
873 (defcustom lisp-indent-offset nil
874 "If non-nil, indent second line of expressions that many more columns."
876 :type '(choice (const nil) integer))
877 (put 'lisp-body-indent 'safe-local-variable
878 (lambda (x) (or (null x) (integerp x))))
880 (defvar lisp-indent-function 'lisp-indent-function)
882 (defun lisp-indent-line (&optional whole-exp)
883 "Indent current line as Lisp code.
884 With argument, indent any additional lines of the same expression
885 rigidly along with this one."
887 (let ((indent (calculate-lisp-indent)) shift-amt end
888 (pos (- (point-max) (point)))
889 (beg (progn (beginning-of-line) (point))))
890 (skip-chars-forward " \t")
891 (if (or (null indent) (looking-at "\\s<\\s<\\s<"))
892 ;; Don't alter indentation of a ;;; comment line
893 ;; or a line that starts in a string.
894 (goto-char (- (point-max) pos))
895 (if (and (looking-at "\\s<") (not (looking-at "\\s<\\s<")))
896 ;; Single-semicolon comment lines should be indented
897 ;; as comment lines, not as code.
898 (progn (indent-for-comment) (forward-char -1))
899 (if (listp indent) (setq indent (car indent)))
900 (setq shift-amt (- indent (current-column)))
901 (if (zerop shift-amt)
903 (delete-region beg (point))
905 ;; If initial point was within line's indentation,
906 ;; position after the indentation. Else stay at same point in text.
907 (if (> (- (point-max) pos) (point))
908 (goto-char (- (point-max) pos)))
909 ;; If desired, shift remaining lines of expression the same amount.
910 (and whole-exp (not (zerop shift-amt))
919 (indent-code-rigidly beg end shift-amt)))))
921 (defvar calculate-lisp-indent-last-sexp)
923 (defun calculate-lisp-indent (&optional parse-start)
924 "Return appropriate indentation for current line as Lisp code.
925 In usual case returns an integer: the column to indent to.
926 If the value is nil, that means don't change the indentation
927 because the line starts inside a string.
929 The value can also be a list of the form (COLUMN CONTAINING-SEXP-START).
930 This means that following lines at the same level of indentation
931 should not necessarily be indented the same as this line.
932 Then COLUMN is the column to indent to, and CONTAINING-SEXP-START
933 is the buffer position of the start of the containing expression."
936 (let ((indent-point (point))
938 ;; setting this to a number inhibits calling hook
941 calculate-lisp-indent-last-sexp containing-sexp)
943 (goto-char parse-start)
944 (beginning-of-defun))
945 ;; Find outermost containing sexp
946 (while (< (point) indent-point)
947 (setq state (parse-partial-sexp (point) indent-point 0)))
948 ;; Find innermost containing sexp
951 (> (setq paren-depth (elt state 0)) 0))
953 (setq calculate-lisp-indent-last-sexp (elt state 2))
954 (setq containing-sexp (elt state 1))
955 ;; Position following last unclosed open.
956 (goto-char (1+ containing-sexp))
957 ;; Is there a complete sexp since then?
958 (if (and calculate-lisp-indent-last-sexp
959 (> calculate-lisp-indent-last-sexp (point)))
960 ;; Yes, but is there a containing sexp after that?
961 (let ((peek (parse-partial-sexp calculate-lisp-indent-last-sexp
963 (if (setq retry (car (cdr peek))) (setq state peek)))))
966 ;; Innermost containing sexp found
967 (goto-char (1+ containing-sexp))
968 (if (not calculate-lisp-indent-last-sexp)
969 ;; indent-point immediately follows open paren.
971 (setq desired-indent (current-column))
972 ;; Find the start of first element of containing sexp.
973 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
974 (cond ((looking-at "\\s(")
975 ;; First element of containing sexp is a list.
976 ;; Indent under that list.
978 ((> (save-excursion (forward-line 1) (point))
979 calculate-lisp-indent-last-sexp)
980 ;; This is the first line to start within the containing sexp.
981 ;; It's almost certainly a function call.
982 (if (= (point) calculate-lisp-indent-last-sexp)
983 ;; Containing sexp has nothing before this line
984 ;; except the first element. Indent under that element.
986 ;; Skip the first element, find start of second (the first
987 ;; argument of the function call) and indent under.
988 (progn (forward-sexp 1)
989 (parse-partial-sexp (point)
990 calculate-lisp-indent-last-sexp
992 (backward-prefix-chars))
994 ;; Indent beneath first sexp on same line as
995 ;; `calculate-lisp-indent-last-sexp'. Again, it's
996 ;; almost certainly a function call.
997 (goto-char calculate-lisp-indent-last-sexp)
999 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp
1001 (backward-prefix-chars)))))
1002 ;; Point is at the point to indent under unless we are inside a string.
1003 ;; Call indentation hook except when overridden by lisp-indent-offset
1004 ;; or if the desired indentation has already been computed.
1005 (let ((normal-indent (current-column)))
1006 (cond ((elt state 3)
1007 ;; Inside a string, don't change indentation.
1009 ((and (integerp lisp-indent-offset) containing-sexp)
1010 ;; Indent by constant offset
1011 (goto-char containing-sexp)
1012 (+ (current-column) lisp-indent-offset))
1013 ;; in this case calculate-lisp-indent-last-sexp is not nil
1014 (calculate-lisp-indent-last-sexp
1016 ;; try to align the parameters of a known function
1017 (and lisp-indent-function
1019 (funcall lisp-indent-function indent-point state))
1020 ;; If the function has no special alignment
1021 ;; or it does not apply to this argument,
1022 ;; try to align a constant-symbol under the last
1023 ;; preceding constant symbol, if there is such one of
1024 ;; the last 2 preceding symbols, in the previous
1025 ;; uncommented line.
1026 (and (save-excursion
1027 (goto-char indent-point)
1028 (skip-chars-forward " \t")
1030 ;; The last sexp may not be at the indentation
1031 ;; where it begins, so find that one, instead.
1033 (goto-char calculate-lisp-indent-last-sexp)
1034 ;; Handle prefix characters and whitespace
1035 ;; following an open paren. (Bug#1012)
1036 (backward-prefix-chars)
1037 (while (and (not (looking-back "^[ \t]*\\|([ \t]+"))
1038 (or (not containing-sexp)
1039 (< (1+ containing-sexp) (point))))
1041 (backward-prefix-chars))
1042 (setq calculate-lisp-indent-last-sexp (point)))
1043 (> calculate-lisp-indent-last-sexp
1045 (goto-char (1+ containing-sexp))
1046 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
1048 (let ((parse-sexp-ignore-comments t)
1050 (goto-char calculate-lisp-indent-last-sexp)
1051 (or (and (looking-at ":")
1052 (setq indent (current-column)))
1053 (and (< (save-excursion (beginning-of-line) (point))
1054 (prog2 (backward-sexp) (point)))
1056 (setq indent (current-column))))
1058 ;; another symbols or constants not preceded by a constant
1059 ;; as defined above.
1061 ;; in this case calculate-lisp-indent-last-sexp is nil
1066 (defun lisp-indent-function (indent-point state)
1067 "This function is the normal value of the variable `lisp-indent-function'.
1068 It is used when indenting a line within a function call, to see if the
1069 called function says anything special about how to indent the line.
1071 INDENT-POINT is the position where the user typed TAB, or equivalent.
1072 Point is located at the point to indent under (for default indentation);
1073 STATE is the `parse-partial-sexp' state for that position.
1075 If the current line is in a call to a Lisp function
1076 which has a non-nil property `lisp-indent-function',
1077 that specifies how to do the indentation. The property value can be
1078 * `defun', meaning indent `defun'-style;
1079 * an integer N, meaning indent the first N arguments specially
1080 like ordinary function arguments and then indent any further
1081 arguments like a body;
1082 * a function to call just as this function was called.
1083 If that function returns nil, that means it doesn't specify
1086 This function also returns nil meaning don't specify the indentation."
1087 (let ((normal-indent (current-column)))
1088 (goto-char (1+ (elt state 1)))
1089 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
1090 (if (and (elt state 2)
1091 (not (looking-at "\\sw\\|\\s_")))
1092 ;; car of form doesn't seem to be a symbol
1094 (if (not (> (save-excursion (forward-line 1) (point))
1095 calculate-lisp-indent-last-sexp))
1096 (progn (goto-char calculate-lisp-indent-last-sexp)
1098 (parse-partial-sexp (point)
1099 calculate-lisp-indent-last-sexp 0 t)))
1100 ;; Indent under the list or under the first sexp on the same
1101 ;; line as calculate-lisp-indent-last-sexp. Note that first
1102 ;; thing on that line has to be complete sexp since we are
1103 ;; inside the innermost containing sexp.
1104 (backward-prefix-chars)
1106 (let ((function (buffer-substring (point)
1107 (progn (forward-sexp 1) (point))))
1109 (setq method (or (get (intern-soft function) 'lisp-indent-function)
1110 (get (intern-soft function) 'lisp-indent-hook)))
1111 (cond ((or (eq method 'defun)
1113 (> (length function) 3)
1114 (string-match "\\`def" function)))
1115 (lisp-indent-defform state indent-point))
1117 (lisp-indent-specform method state
1118 indent-point normal-indent))
1120 (funcall method indent-point state)))))))
1122 (defcustom lisp-body-indent 2
1123 "Number of columns to indent the second line of a `(def...)' form."
1126 (put 'lisp-body-indent 'safe-local-variable 'integerp)
1128 (defun lisp-indent-specform (count state indent-point normal-indent)
1129 (let ((containing-form-start (elt state 1))
1131 body-indent containing-form-column)
1132 ;; Move to the start of containing form, calculate indentation
1133 ;; to use for non-distinguished forms (> count), and move past the
1134 ;; function symbol. lisp-indent-function guarantees that there is at
1135 ;; least one word or symbol character following open paren of containing
1137 (goto-char containing-form-start)
1138 (setq containing-form-column (current-column))
1139 (setq body-indent (+ lisp-body-indent containing-form-column))
1142 ;; Now find the start of the last form.
1143 (parse-partial-sexp (point) indent-point 1 t)
1144 (while (and (< (point) indent-point)
1147 (setq count (1- count))
1149 (parse-partial-sexp (point) indent-point 1 t))
1151 ;; Point is sitting on first character of last (or count) sexp.
1153 ;; A distinguished form. If it is the first or second form use double
1154 ;; lisp-body-indent, else normal indent. With lisp-body-indent bound
1155 ;; to 2 (the default), this just happens to work the same with if as
1156 ;; the older code, but it makes unwind-protect, condition-case,
1157 ;; with-output-to-temp-buffer, et. al. much more tasteful. The older,
1158 ;; less hacked, behavior can be obtained by replacing below with
1159 ;; (list normal-indent containing-form-start).
1160 (if (<= (- i count) 1)
1161 (list (+ containing-form-column (* 2 lisp-body-indent))
1162 containing-form-start)
1163 (list normal-indent containing-form-start))
1164 ;; A non-distinguished form. Use body-indent if there are no
1165 ;; distinguished forms and this is the first undistinguished form,
1166 ;; or if this is the first undistinguished form and the preceding
1167 ;; distinguished form has indentation at least as great as body-indent.
1168 (if (or (and (= i 0) (= count 0))
1169 (and (= count 0) (<= body-indent normal-indent)))
1173 (defun lisp-indent-defform (state indent-point)
1174 (goto-char (car (cdr state)))
1176 (if (> (point) (car (cdr (cdr state))))
1178 (goto-char (car (cdr state)))
1179 (+ lisp-body-indent (current-column)))))
1182 ;; (put 'progn 'lisp-indent-function 0), say, causes progn to be indented
1183 ;; like defun if the first form is placed on the next line, otherwise
1184 ;; it is indented like any other form (i.e. forms line up under first).
1186 (put 'lambda 'lisp-indent-function 'defun)
1187 (put 'autoload 'lisp-indent-function 'defun)
1188 (put 'progn 'lisp-indent-function 0)
1189 (put 'prog1 'lisp-indent-function 1)
1190 (put 'prog2 'lisp-indent-function 2)
1191 (put 'save-excursion 'lisp-indent-function 0)
1192 (put 'save-window-excursion 'lisp-indent-function 0)
1193 (put 'save-selected-window 'lisp-indent-function 0)
1194 (put 'save-restriction 'lisp-indent-function 0)
1195 (put 'save-match-data 'lisp-indent-function 0)
1196 (put 'save-current-buffer 'lisp-indent-function 0)
1197 (put 'with-current-buffer 'lisp-indent-function 1)
1198 (put 'combine-after-change-calls 'lisp-indent-function 0)
1199 (put 'with-output-to-string 'lisp-indent-function 0)
1200 (put 'with-temp-file 'lisp-indent-function 1)
1201 (put 'with-temp-buffer 'lisp-indent-function 0)
1202 (put 'with-temp-message 'lisp-indent-function 1)
1203 (put 'with-syntax-table 'lisp-indent-function 1)
1204 (put 'let 'lisp-indent-function 1)
1205 (put 'let* 'lisp-indent-function 1)
1206 (put 'while 'lisp-indent-function 1)
1207 (put 'if 'lisp-indent-function 2)
1208 (put 'read-if 'lisp-indent-function 2)
1209 (put 'catch 'lisp-indent-function 1)
1210 (put 'condition-case 'lisp-indent-function 2)
1211 (put 'unwind-protect 'lisp-indent-function 1)
1212 (put 'with-output-to-temp-buffer 'lisp-indent-function 1)
1213 (put 'eval-after-load 'lisp-indent-function 1)
1214 (put 'dolist 'lisp-indent-function 1)
1215 (put 'dotimes 'lisp-indent-function 1)
1216 (put 'when 'lisp-indent-function 1)
1217 (put 'unless 'lisp-indent-function 1)
1219 (defun indent-sexp (&optional endpos)
1220 "Indent each line of the list starting just after point.
1221 If optional arg ENDPOS is given, indent each line, stopping when
1222 ENDPOS is encountered."
1224 (let ((indent-stack (list nil))
1226 ;; If ENDPOS is non-nil, use nil as STARTING-POINT
1227 ;; so that calculate-lisp-indent will find the beginning of
1228 ;; the defun we are in.
1229 ;; If ENDPOS is nil, it is safe not to scan before point
1230 ;; since every line we indent is more deeply nested than point is.
1231 (starting-point (if endpos nil (point)))
1232 (last-point (point))
1233 last-depth bol outer-loop-done inner-loop-done state this-indent)
1235 ;; Get error now if we don't have a complete sexp after point.
1236 (save-excursion (forward-sexp 1)))
1238 (setq outer-loop-done nil)
1239 (while (if endpos (< (point) endpos)
1240 (not outer-loop-done))
1241 (setq last-depth next-depth
1242 inner-loop-done nil)
1243 ;; Parse this line so we can learn the state
1244 ;; to indent the next line.
1245 ;; This inner loop goes through only once
1246 ;; unless a line ends inside a string.
1247 (while (and (not inner-loop-done)
1248 (not (setq outer-loop-done (eobp))))
1249 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
1251 (setq next-depth (car state))
1252 ;; If the line contains a comment other than the sort
1253 ;; that is indented like code,
1254 ;; indent it now with indent-for-comment.
1255 ;; Comments indented like code are right already.
1256 ;; In any case clear the in-comment flag in the state
1257 ;; because parse-partial-sexp never sees the newlines.
1258 (if (car (nthcdr 4 state))
1259 (progn (indent-for-comment)
1261 (setcar (nthcdr 4 state) nil)))
1262 ;; If this line ends inside a string,
1263 ;; go straight to next line, remaining within the inner loop,
1264 ;; and turn off the \-flag.
1265 (if (car (nthcdr 3 state))
1268 (setcar (nthcdr 5 state) nil))
1269 (setq inner-loop-done t)))
1273 (setq indent-stack (nconc indent-stack
1274 (make-list (- next-depth) nil))
1275 last-depth (- last-depth next-depth)
1278 ;; Decide whether to exit.
1280 ;; If we have already reached the specified end,
1281 ;; give up and do not reindent this line.
1282 (if (<= endpos (point))
1283 (setq outer-loop-done t))
1284 ;; If no specified end, we are done if we have finished one sexp.
1285 (if (<= next-depth 0)
1286 (setq outer-loop-done t)))
1287 (unless outer-loop-done
1288 (while (> last-depth next-depth)
1289 (setq indent-stack (cdr indent-stack)
1290 last-depth (1- last-depth)))
1291 (while (< last-depth next-depth)
1292 (setq indent-stack (cons nil indent-stack)
1293 last-depth (1+ last-depth)))
1294 ;; Now indent the next line according
1295 ;; to what we learned from parsing the previous one.
1297 (skip-chars-forward " \t")
1298 ;; But not if the line is blank, or just a comment
1299 ;; (except for double-semi comments; indent them as usual).
1300 (if (or (eobp) (looking-at "\\s<\\|\n"))
1302 (if (and (car indent-stack)
1303 (>= (car indent-stack) 0))
1304 (setq this-indent (car indent-stack))
1305 (let ((val (calculate-lisp-indent
1306 (if (car indent-stack) (- (car indent-stack))
1309 (setq this-indent val)
1311 (setcar indent-stack
1312 (setq this-indent val))
1313 (setcar indent-stack (- (car (cdr val))))
1314 (setq this-indent (car val))))))
1315 (if (and this-indent (/= (current-column) this-indent))
1316 (progn (delete-region bol (point))
1317 (indent-to this-indent)))))
1319 (setq outer-loop-done (= (point) last-point))
1320 (setq last-point (point)))))))
1322 (defun lisp-indent-region (start end)
1323 "Indent every line whose first char is between START and END inclusive."
1325 (let ((endmark (copy-marker end)))
1327 (and (bolp) (not (eolp))
1329 (indent-sexp endmark)
1330 (set-marker endmark nil))))
1332 (defun indent-pp-sexp (&optional arg)
1333 "Indent each line of the list starting just after point, or prettyprint it.
1334 A prefix argument specifies pretty-printing."
1339 (narrow-to-region (point) (progn (forward-sexp 1) (point)))
1341 (goto-char (point-max))
1342 (if (eq (char-before) ?\n)
1343 (delete-char -1)))))
1346 ;;;; Lisp paragraph filling commands.
1348 (defcustom emacs-lisp-docstring-fill-column 65
1349 "Value of `fill-column' to use when filling a docstring.
1350 Any non-integer value means do not use a different value of
1351 `fill-column' when filling docstrings."
1352 :type '(choice (integer)
1353 (const :tag "Use the current `fill-column'" t))
1356 (defun lisp-fill-paragraph (&optional justify)
1357 "Like \\[fill-paragraph], but handle Emacs Lisp comments and docstrings.
1358 If any of the current line is a comment, fill the comment or the
1359 paragraph of it that point is in, preserving the comment's indentation
1360 and initial semicolons."
1362 (or (fill-comment-paragraph justify)
1363 ;; Since fill-comment-paragraph returned nil, that means we're not in
1364 ;; a comment: Point is on a program line; we are interested
1365 ;; particularly in docstring lines.
1367 ;; We bind `paragraph-start' and `paragraph-separate' temporarily. They
1368 ;; are buffer-local, but we avoid changing them so that they can be set
1369 ;; to make `forward-paragraph' and friends do something the user wants.
1371 ;; `paragraph-start': The `(' in the character alternative and the
1372 ;; left-singlequote plus `(' sequence after the \\| alternative prevent
1373 ;; sexps and backquoted sexps that follow a docstring from being filled
1374 ;; with the docstring. This setting has the consequence of inhibiting
1375 ;; filling many program lines that are not docstrings, which is sensible,
1376 ;; because the user probably asked to fill program lines by accident, or
1377 ;; expecting indentation (perhaps we should try to do indenting in that
1378 ;; case). The `;' and `:' stop the paragraph being filled at following
1379 ;; comment lines and at keywords (e.g., in `defcustom'). Left parens are
1380 ;; escaped to keep font-locking, filling, & paren matching in the source
1383 ;; `paragraph-separate': A clever regexp distinguishes the first line of
1384 ;; a docstring and identifies it as a paragraph separator, so that it
1385 ;; won't be filled. (Since the first line of documentation stands alone
1386 ;; in some contexts, filling should not alter the contents the author has
1387 ;; chosen.) Only the first line of a docstring begins with whitespace
1388 ;; and a quotation mark and ends with a period or (rarely) a comma.
1390 ;; The `fill-column' is temporarily bound to
1391 ;; `emacs-lisp-docstring-fill-column' if that value is an integer.
1392 (let ((paragraph-start (concat paragraph-start
1393 "\\|\\s-*\\([(;:\"]\\|`(\\|#'(\\)"))
1395 (concat paragraph-separate "\\|\\s-*\".*[,\\.]$"))
1396 (fill-column (if (and (integerp emacs-lisp-docstring-fill-column)
1397 (derived-mode-p 'emacs-lisp-mode))
1398 emacs-lisp-docstring-fill-column
1400 (fill-paragraph justify))
1401 ;; Never return nil.
1404 (defun indent-code-rigidly (start end arg &optional nochange-regexp)
1405 "Indent all lines of code, starting in the region, sideways by ARG columns.
1406 Does not affect lines starting inside comments or strings, assuming that
1407 the start of the region is not inside them.
1409 Called from a program, takes args START, END, COLUMNS and NOCHANGE-REGEXP.
1410 The last is a regexp which, if matched at the beginning of a line,
1411 means don't indent that line."
1412 (interactive "r\np")
1416 (setq end (point-marker))
1419 (setq state (parse-partial-sexp (point)
1421 (forward-line 1) (point))
1423 (while (< (point) end)
1424 (or (car (nthcdr 3 state))
1425 (and nochange-regexp
1426 (looking-at nochange-regexp))
1427 ;; If line does not start in string, indent it
1428 (let ((indent (current-indentation)))
1429 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
1431 (indent-to (max 0 (+ indent arg)) 0))))
1432 (setq state (parse-partial-sexp (point)
1434 (forward-line 1) (point))
1437 (provide 'lisp-mode)
1439 ;; arch-tag: 414c7f93-c245-4b77-8ed5-ed05ef7ff1bf
1440 ;;; lisp-mode.el ends here