Tom Tromey <tromey at redhat.com>
[emacs.git] / lisp / emacs-lisp / lisp-mode.el
blob48f0b9b78607a93f7862b31e52c1cb53b39cb8da
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 Free Software Foundation, Inc.
6 ;; Maintainer: FSF
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, or (at your option)
14 ;; 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; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
26 ;;; Commentary:
28 ;; The base major mode for editing Lisp code (used also for Emacs Lisp).
29 ;; This mode is documented in the Emacs manual.
31 ;;; Code:
33 (defvar font-lock-comment-face)
34 (defvar font-lock-doc-face)
35 (defvar font-lock-keywords-case-fold-search)
36 (defvar font-lock-string-face)
38 (defvar lisp-mode-abbrev-table nil)
40 (define-abbrev-table 'lisp-mode-abbrev-table ())
42 (defvar emacs-lisp-mode-syntax-table
43 (let ((table (make-syntax-table)))
44 (let ((i 0))
45 (while (< i ?0)
46 (modify-syntax-entry i "_ " table)
47 (setq i (1+ i)))
48 (setq i (1+ ?9))
49 (while (< i ?A)
50 (modify-syntax-entry i "_ " table)
51 (setq i (1+ i)))
52 (setq i (1+ ?Z))
53 (while (< i ?a)
54 (modify-syntax-entry i "_ " table)
55 (setq i (1+ i)))
56 (setq i (1+ ?z))
57 (while (< i 128)
58 (modify-syntax-entry i "_ " table)
59 (setq i (1+ i)))
60 (modify-syntax-entry ?\s " " table)
61 ;; Non-break space acts as whitespace.
62 (modify-syntax-entry ?\x8a0 " " table)
63 (modify-syntax-entry ?\t " " table)
64 (modify-syntax-entry ?\f " " table)
65 (modify-syntax-entry ?\n "> " table)
66 ;; This is probably obsolete since nowadays such features use overlays.
67 ;; ;; Give CR the same syntax as newline, for selective-display.
68 ;; (modify-syntax-entry ?\^m "> " table)
69 (modify-syntax-entry ?\; "< " table)
70 (modify-syntax-entry ?` "' " table)
71 (modify-syntax-entry ?' "' " table)
72 (modify-syntax-entry ?, "' " table)
73 (modify-syntax-entry ?@ "' " table)
74 ;; Used to be singlequote; changed for flonums.
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)
81 (modify-syntax-entry ?\[ "(] " table)
82 (modify-syntax-entry ?\] ")[ " table))
83 table))
85 (defvar lisp-mode-syntax-table
86 (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table)))
87 (modify-syntax-entry ?\[ "_ " table)
88 (modify-syntax-entry ?\] "_ " table)
89 (modify-syntax-entry ?# "' 14b" table)
90 (modify-syntax-entry ?| "\" 23bn" table)
91 table))
93 (defvar lisp-imenu-generic-expression
94 (list
95 (list nil
96 (purecopy (concat "^\\s-*("
97 (eval-when-compile
98 (regexp-opt
99 '("defun" "defun*" "defsubst" "defmacro"
100 "defadvice" "define-skeleton"
101 "define-minor-mode" "define-global-minor-mode"
102 "define-globalized-minor-mode"
103 "define-derived-mode" "define-generic-mode"
104 "define-compiler-macro" "define-modify-macro"
105 "defsetf" "define-setf-expander"
106 "define-method-combination"
107 "defgeneric" "defmethod") t))
108 "\\s-+\\(\\(\\sw\\|\\s_\\)+\\)"))
110 (list (purecopy "Variables")
111 (purecopy (concat "^\\s-*("
112 (eval-when-compile
113 (regexp-opt
114 '("defvar" "defconst" "defconstant" "defcustom"
115 "defparameter" "define-symbol-macro") t))
116 "\\s-+\\(\\(\\sw\\|\\s_\\)+\\)"))
118 (list (purecopy "Types")
119 (purecopy (concat "^\\s-*("
120 (eval-when-compile
121 (regexp-opt
122 '("defgroup" "deftheme" "deftype" "defstruct"
123 "defclass" "define-condition" "define-widget"
124 "defface" "defpackage") t))
125 "\\s-+'?\\(\\(\\sw\\|\\s_\\)+\\)"))
128 "Imenu generic expression for Lisp mode. See `imenu-generic-expression'.")
130 ;; This was originally in autoload.el and is still used there.
131 (put 'autoload 'doc-string-elt 3)
132 (put 'defun 'doc-string-elt 3)
133 (put 'defun* 'doc-string-elt 3)
134 (put 'defvar 'doc-string-elt 3)
135 (put 'defcustom 'doc-string-elt 3)
136 (put 'deftheme 'doc-string-elt 2)
137 (put 'defconst 'doc-string-elt 3)
138 (put 'defmacro 'doc-string-elt 3)
139 (put 'defmacro* 'doc-string-elt 3)
140 (put 'defsubst 'doc-string-elt 3)
141 (put 'defstruct 'doc-string-elt 2)
142 (put 'define-skeleton 'doc-string-elt 2)
143 (put 'define-derived-mode 'doc-string-elt 4)
144 (put 'define-compilation-mode 'doc-string-elt 3)
145 (put 'easy-mmode-define-minor-mode 'doc-string-elt 2)
146 (put 'define-minor-mode 'doc-string-elt 2)
147 (put 'easy-mmode-define-global-mode 'doc-string-elt 2)
148 (put 'define-global-minor-mode 'doc-string-elt 2)
149 (put 'define-globalized-minor-mode 'doc-string-elt 2)
150 (put 'define-generic-mode 'doc-string-elt 7)
151 (put 'define-ibuffer-filter 'doc-string-elt 2)
152 (put 'define-ibuffer-op 'doc-string-elt 3)
153 (put 'define-ibuffer-sorter 'doc-string-elt 2)
154 (put 'lambda 'doc-string-elt 2)
155 (put 'defalias 'doc-string-elt 3)
156 (put 'defvaralias 'doc-string-elt 3)
157 (put 'define-category 'doc-string-elt 2)
159 (defvar lisp-doc-string-elt-property 'doc-string-elt
160 "The symbol property that holds the docstring position info.")
162 (defun lisp-font-lock-syntactic-face-function (state)
163 (if (nth 3 state)
164 ;; This might be a (doc)string or a |...| symbol.
165 (let ((startpos (nth 8 state)))
166 (if (eq (char-after startpos) ?|)
167 ;; This is not a string, but a |...| symbol.
169 (let* ((listbeg (nth 1 state))
170 (firstsym (and listbeg
171 (save-excursion
172 (goto-char listbeg)
173 (and (looking-at "([ \t\n]*\\(\\(\\sw\\|\\s_\\)+\\)")
174 (match-string 1)))))
175 (docelt (and firstsym (get (intern-soft firstsym)
176 lisp-doc-string-elt-property))))
177 (if (and docelt
178 ;; It's a string in a form that can have a docstring.
179 ;; Check whether it's in docstring position.
180 (save-excursion
181 (when (functionp docelt)
182 (goto-char (match-end 1))
183 (setq docelt (funcall docelt)))
184 (goto-char listbeg)
185 (forward-char 1)
186 (condition-case nil
187 (while (and (> docelt 0) (< (point) startpos)
188 (progn (forward-sexp 1) t))
189 (setq docelt (1- docelt)))
190 (error nil))
191 (and (zerop docelt) (<= (point) startpos)
192 (progn (forward-comment (point-max)) t)
193 (= (point) (nth 8 state)))))
194 font-lock-doc-face
195 font-lock-string-face))))
196 font-lock-comment-face))
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 (defun lisp-mode-variables (&optional lisp-syntax)
201 (when lisp-syntax
202 (set-syntax-table lisp-mode-syntax-table))
203 (setq local-abbrev-table lisp-mode-abbrev-table)
204 (make-local-variable 'paragraph-ignore-fill-prefix)
205 (setq paragraph-ignore-fill-prefix t)
206 (make-local-variable 'fill-paragraph-function)
207 (setq fill-paragraph-function 'lisp-fill-paragraph)
208 ;; Adaptive fill mode gets the fill wrong for a one-line paragraph made of
209 ;; a single docstring. Let's fix it here.
210 (set (make-local-variable 'adaptive-fill-function)
211 (lambda () (if (looking-at "\\s-+\"[^\n\"]+\"\\s-*$") "")))
212 ;; Adaptive fill mode gets in the way of auto-fill,
213 ;; and should make no difference for explicit fill
214 ;; because lisp-fill-paragraph should do the job.
215 ;; I believe that newcomment's auto-fill code properly deals with it -stef
216 ;;(set (make-local-variable 'adaptive-fill-mode) nil)
217 (make-local-variable 'indent-line-function)
218 (setq indent-line-function 'lisp-indent-line)
219 (make-local-variable 'parse-sexp-ignore-comments)
220 (setq parse-sexp-ignore-comments t)
221 (make-local-variable 'outline-regexp)
222 (setq outline-regexp ";;;\\(;* [^ \t\n]\\|###autoload\\)\\|(")
223 (make-local-variable 'outline-level)
224 (setq outline-level 'lisp-outline-level)
225 (make-local-variable 'comment-start)
226 (setq comment-start ";")
227 (make-local-variable 'comment-start-skip)
228 ;; Look within the line for a ; following an even number of backslashes
229 ;; after either a non-backslash or the line beginning.
230 (setq comment-start-skip "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\);+ *")
231 (make-local-variable 'font-lock-comment-start-skip)
232 ;; Font lock mode uses this only when it KNOWS a comment is starting.
233 (setq font-lock-comment-start-skip ";+ *")
234 (make-local-variable 'comment-add)
235 (setq comment-add 1) ;default to `;;' in comment-region
236 (make-local-variable 'comment-column)
237 (setq comment-column 40)
238 ;; Don't get confused by `;' in doc strings when paragraph-filling.
239 (set (make-local-variable 'comment-use-global-state) t)
240 (make-local-variable 'imenu-generic-expression)
241 (setq imenu-generic-expression lisp-imenu-generic-expression)
242 (make-local-variable 'multibyte-syntax-as-symbol)
243 (setq multibyte-syntax-as-symbol t)
244 (set (make-local-variable 'syntax-begin-function) 'beginning-of-defun)
245 (setq font-lock-defaults
246 '((lisp-font-lock-keywords
247 lisp-font-lock-keywords-1 lisp-font-lock-keywords-2)
248 nil nil (("+-*/.<>=!?$%_&~^:@" . "w")) nil
249 (font-lock-mark-block-function . mark-defun)
250 (font-lock-syntactic-face-function
251 . lisp-font-lock-syntactic-face-function))))
253 (defun lisp-outline-level ()
254 "Lisp mode `outline-level' function."
255 (let ((len (- (match-end 0) (match-beginning 0))))
256 (if (looking-at "(\\|;;;###autoload")
257 1000
258 len)))
260 (defvar lisp-mode-shared-map
261 (let ((map (make-sparse-keymap)))
262 (define-key map "\e\C-q" 'indent-sexp)
263 (define-key map "\177" 'backward-delete-char-untabify)
264 ;; This gets in the way when viewing a Lisp file in view-mode. As
265 ;; long as [backspace] is mapped into DEL via the
266 ;; function-key-map, this should remain disabled!!
267 ;;;(define-key map [backspace] 'backward-delete-char-untabify)
268 map)
269 "Keymap for commands shared by all sorts of Lisp modes.")
271 (defvar emacs-lisp-mode-map
272 (let ((map (make-sparse-keymap "Emacs-Lisp"))
273 (menu-map (make-sparse-keymap "Emacs-Lisp"))
274 (prof-map (make-sparse-keymap))
275 (tracing-map (make-sparse-keymap)))
276 (set-keymap-parent map lisp-mode-shared-map)
277 (define-key map "\e\t" 'lisp-complete-symbol)
278 (define-key map "\e\C-x" 'eval-defun)
279 (define-key map "\e\C-q" 'indent-pp-sexp)
280 (define-key map [menu-bar emacs-lisp] (cons "Emacs-Lisp" menu-map))
281 (define-key menu-map [eldoc]
282 '(menu-item "Auto-Display Documentation Strings" eldoc-mode
283 :button (:toggle . (bound-and-true-p eldoc-mode))
284 :help "Display the documentation string for the item under cursor"))
285 (define-key menu-map [checkdoc]
286 '(menu-item "Check Documentation Strings" checkdoc
287 :help "Check documentation strings for style requirements"))
288 (define-key menu-map [re-builder]
289 '(menu-item "Construct Regexp" re-builder
290 :help "Construct a regexp interactively"))
291 (define-key menu-map [tracing] (cons "Tracing" tracing-map))
292 (define-key tracing-map [tr-a]
293 '(menu-item "Untrace all" untrace-all
294 :help "Untraces all currently traced functions"))
295 (define-key tracing-map [tr-uf]
296 '(menu-item "Untrace function..." untrace-function
297 :help "Untraces FUNCTION and possibly activates all remaining advice"))
298 (define-key tracing-map [tr-sep] '("--"))
299 (define-key tracing-map [tr-q]
300 '(menu-item "Trace function quietly..." trace-function-background
301 :help "Trace the function with trace output going quietly to a buffer"))
302 (define-key tracing-map [tr-f]
303 '(menu-item "Trace function..." trace-function
304 :help "Trace the function given as a argument"))
305 (define-key menu-map [profiling] (cons "Profiling" prof-map))
306 (define-key prof-map [prof-restall]
307 '(menu-item "Remove Instrumentation for All Functions" elp-restore-all
308 :help "Restore the original definitions of all functions being profiled"))
309 (define-key prof-map [prof-restfunc]
310 '(menu-item "Remove Instrumentation for Function..." elp-restore-function
311 :help "Restore an instrumented function to its original definition"))
313 (define-key prof-map [sep-rem] '("--"))
314 (define-key prof-map [prof-resall]
315 '(menu-item "Reset Counters for All Functions" elp-reset-all
316 :help "Reset the profiling information for all functions being profiled"))
317 (define-key prof-map [prof-resfunc]
318 '(menu-item "Reset Counters for Function..." elp-reset-function
319 :help "Reset the profiling information for a function"))
320 (define-key prof-map [prof-res]
321 '(menu-item "Show Profiling Results" elp-results
322 :help "Display current profiling results"))
323 (define-key prof-map [prof-pack]
324 '(menu-item "Instrument Package..." elp-instrument-package
325 :help "Instrument for profiling all function that start with a prefix"))
326 (define-key prof-map [prof-func]
327 '(menu-item "Instrument Function..." elp-instrument-function
328 :help "Instrument a function for profiling"))
329 (define-key menu-map [edebug-defun]
330 '(menu-item "Instrument Function for Debugging" edebug-defun
331 :help "Evaluate the top level form point is in, stepping through with Edebug"
332 :keys "C-u C-M-x"))
333 (define-key menu-map [separator-byte] '("--"))
334 (define-key menu-map [disas]
335 '(menu-item "Disassemble byte compiled object..." disassemble
336 :help "Print disassembled code for OBJECT in a buffer"))
337 (define-key menu-map [byte-recompile]
338 '(menu-item "Byte-recompile Directory..." byte-recompile-directory
339 :help "Recompile every `.el' file in DIRECTORY that needs recompilation"))
340 (define-key menu-map [emacs-byte-compile-and-load]
341 '(menu-item "Byte-compile And Load" emacs-lisp-byte-compile-and-load
342 :help "Byte-compile the current file (if it has changed), then load compiled code"))
343 (define-key menu-map [byte-compile]
344 '(menu-item "Byte-compile This File" emacs-lisp-byte-compile
345 :help "Byte compile the file containing the current buffer"))
346 (define-key menu-map [separator-eval] '("--"))
347 (define-key menu-map [ielm]
348 '(menu-item "Interactive Expression Evaluation" ielm
349 :help "Interactively evaluate Emacs Lisp expressions"))
350 (define-key menu-map [eval-buffer]
351 '(menu-item "Evaluate Buffer" eval-buffer
352 :help "Execute the current buffer as Lisp code"))
353 (define-key menu-map [eval-region]
354 '(menu-item "Evaluate Region" eval-region
355 :help "Execute the region as Lisp code"
356 :enable mark-active))
357 (define-key menu-map [eval-sexp]
358 '(menu-item "Evaluate Last S-expression" eval-last-sexp
359 :help "Evaluate sexp before point; print value in minibuffer"))
360 (define-key menu-map [separator-format] '("--"))
361 (define-key menu-map [comment-region]
362 '(menu-item "Comment Out Region" comment-region
363 :help "Comment or uncomment each line in the region"
364 :enable mark-active))
365 (define-key menu-map [indent-region]
366 '(menu-item "Indent Region" indent-region
367 :help "Indent each nonblank line in the region"
368 :enable mark-active))
369 (define-key menu-map [indent-line] '("Indent Line" . lisp-indent-line))
370 map)
371 "Keymap for Emacs Lisp mode.
372 All commands in `lisp-mode-shared-map' are inherited by this map.")
374 (defun emacs-lisp-byte-compile ()
375 "Byte compile the file containing the current buffer."
376 (interactive)
377 (if buffer-file-name
378 (byte-compile-file buffer-file-name)
379 (error "The buffer must be saved in a file first")))
381 (defun emacs-lisp-byte-compile-and-load ()
382 "Byte-compile the current file (if it has changed), then load compiled code."
383 (interactive)
384 (or buffer-file-name
385 (error "The buffer must be saved in a file first"))
386 (require 'bytecomp)
387 ;; Recompile if file or buffer has changed since last compilation.
388 (if (and (buffer-modified-p)
389 (y-or-n-p (format "Save buffer %s first? " (buffer-name))))
390 (save-buffer))
391 (let ((compiled-file-name (byte-compile-dest-file buffer-file-name)))
392 (if (file-newer-than-file-p compiled-file-name buffer-file-name)
393 (load-file compiled-file-name)
394 (byte-compile-file buffer-file-name t))))
396 (defcustom emacs-lisp-mode-hook nil
397 "Hook run when entering Emacs Lisp mode."
398 :options '(turn-on-eldoc-mode imenu-add-menubar-index checkdoc-minor-mode)
399 :type 'hook
400 :group 'lisp)
402 (defcustom lisp-mode-hook nil
403 "Hook run when entering Lisp mode."
404 :options '(imenu-add-menubar-index)
405 :type 'hook
406 :group 'lisp)
408 (defcustom lisp-interaction-mode-hook nil
409 "Hook run when entering Lisp Interaction mode."
410 :options '(turn-on-eldoc-mode)
411 :type 'hook
412 :group 'lisp)
414 (defun emacs-lisp-mode ()
415 "Major mode for editing Lisp code to run in Emacs.
416 Commands:
417 Delete converts tabs to spaces as it moves back.
418 Blank lines separate paragraphs. Semicolons start comments.
419 \\{emacs-lisp-mode-map}
420 Entry to this mode calls the value of `emacs-lisp-mode-hook'
421 if that value is non-nil."
422 (interactive)
423 (kill-all-local-variables)
424 (use-local-map emacs-lisp-mode-map)
425 (set-syntax-table emacs-lisp-mode-syntax-table)
426 (setq major-mode 'emacs-lisp-mode)
427 (setq mode-name "Emacs-Lisp")
428 (lisp-mode-variables)
429 (setq imenu-case-fold-search nil)
430 (run-mode-hooks 'emacs-lisp-mode-hook))
431 (put 'emacs-lisp-mode 'custom-mode-group 'lisp)
433 (defvar lisp-mode-map
434 (let ((map (make-sparse-keymap)))
435 (set-keymap-parent map lisp-mode-shared-map)
436 (define-key map "\e\C-x" 'lisp-eval-defun)
437 (define-key map "\C-c\C-z" 'run-lisp)
438 map)
439 "Keymap for ordinary Lisp mode.
440 All commands in `lisp-mode-shared-map' are inherited by this map.")
442 (defun lisp-mode ()
443 "Major mode for editing Lisp code for Lisps other than GNU Emacs Lisp.
444 Commands:
445 Delete converts tabs to spaces as it moves back.
446 Blank lines separate paragraphs. Semicolons start comments.
447 \\{lisp-mode-map}
448 Note that `run-lisp' may be used either to start an inferior Lisp job
449 or to switch back to an existing one.
451 Entry to this mode calls the value of `lisp-mode-hook'
452 if that value is non-nil."
453 (interactive)
454 (kill-all-local-variables)
455 (use-local-map lisp-mode-map)
456 (setq major-mode 'lisp-mode)
457 (setq mode-name "Lisp")
458 (lisp-mode-variables)
459 (make-local-variable 'comment-start-skip)
460 (setq comment-start-skip
461 "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\)\\(;+\\|#|\\) *")
462 (make-local-variable 'font-lock-keywords-case-fold-search)
463 (setq font-lock-keywords-case-fold-search t)
464 (setq imenu-case-fold-search t)
465 (set-syntax-table lisp-mode-syntax-table)
466 (run-mode-hooks 'lisp-mode-hook))
467 (put 'lisp-mode 'find-tag-default-function 'lisp-find-tag-default)
469 (defun lisp-find-tag-default ()
470 (let ((default (find-tag-default)))
471 (when (stringp default)
472 (if (string-match ":+" default)
473 (substring default (match-end 0))
474 default))))
476 ;; Used in old LispM code.
477 (defalias 'common-lisp-mode 'lisp-mode)
479 ;; This will do unless inf-lisp.el is loaded.
480 (defun lisp-eval-defun (&optional and-go)
481 "Send the current defun to the Lisp process made by \\[run-lisp]."
482 (interactive)
483 (error "Process lisp does not exist"))
485 (defvar lisp-interaction-mode-map
486 (let ((map (make-sparse-keymap))
487 (menu-map (make-sparse-keymap "Lisp-Interaction")))
488 (set-keymap-parent map lisp-mode-shared-map)
489 (define-key map "\e\C-x" 'eval-defun)
490 (define-key map "\e\C-q" 'indent-pp-sexp)
491 (define-key map "\e\t" 'lisp-complete-symbol)
492 (define-key map "\n" 'eval-print-last-sexp)
493 (define-key map [menu-bar lisp-interaction] (cons "Lisp-Interaction" menu-map))
494 (define-key menu-map [eval-defun]
495 '(menu-item "Evaluate Defun" eval-defun
496 :help "Evaluate the top-level form containing point, or after point"))
497 (define-key menu-map [eval-print-last-sexp]
498 '(menu-item "Evaluate and print" eval-print-last-sexp
499 :help "Evaluate sexp before point; print value into current buffer"))
500 (define-key menu-map [edebug-defun-lisp-interaction]
501 '(menu-item "Instrument Function for Debugging" edebug-defun
502 :help "Evaluate the top level form point is in, stepping through with Edebug"
503 :keys "C-u C-M-x"))
504 (define-key menu-map [indent-pp-sexp]
505 '(menu-item "Indent or Pretty-Print" indent-pp-sexp
506 :help "Indent each line of the list starting just after point, or prettyprint it"))
507 (define-key menu-map [lisp-complete-symbol]
508 '(menu-item "Complete Lisp Symbol" lisp-complete-symbol
509 :help "Perform completion on Lisp symbol preceding point"))
510 map)
511 "Keymap for Lisp Interaction mode.
512 All commands in `lisp-mode-shared-map' are inherited by this map.")
514 (defvar lisp-interaction-mode-abbrev-table lisp-mode-abbrev-table)
515 (define-derived-mode lisp-interaction-mode emacs-lisp-mode "Lisp Interaction"
516 "Major mode for typing and evaluating Lisp forms.
517 Like Lisp mode except that \\[eval-print-last-sexp] evals the Lisp expression
518 before point, and prints its value into the buffer, advancing point.
519 Note that printing is controlled by `eval-expression-print-length'
520 and `eval-expression-print-level'.
522 Commands:
523 Delete converts tabs to spaces as it moves back.
524 Paragraphs are separated only by blank lines.
525 Semicolons start comments.
526 \\{lisp-interaction-mode-map}
527 Entry to this mode calls the value of `lisp-interaction-mode-hook'
528 if that value is non-nil.")
530 (defun eval-print-last-sexp ()
531 "Evaluate sexp before point; print value into current buffer.
533 If `eval-expression-debug-on-error' is non-nil, which is the default,
534 this command arranges for all errors to enter the debugger.
536 Note that printing the result is controlled by the variables
537 `eval-expression-print-length' and `eval-expression-print-level',
538 which see."
539 (interactive)
540 (let ((standard-output (current-buffer)))
541 (terpri)
542 (eval-last-sexp t)
543 (terpri)))
546 (defun last-sexp-setup-props (beg end value alt1 alt2)
547 "Set up text properties for the output of `eval-last-sexp-1'.
548 BEG and END are the start and end of the output in current-buffer.
549 VALUE is the Lisp value printed, ALT1 and ALT2 are strings for the
550 alternative printed representations that can be displayed."
551 (let ((map (make-sparse-keymap)))
552 (define-key map "\C-m" 'last-sexp-toggle-display)
553 (define-key map [down-mouse-2] 'mouse-set-point)
554 (define-key map [mouse-2] 'last-sexp-toggle-display)
555 (add-text-properties
556 beg end
557 `(printed-value (,value ,alt1 ,alt2)
558 mouse-face highlight
559 keymap ,map
560 help-echo "RET, mouse-2: toggle abbreviated display"
561 rear-nonsticky (mouse-face keymap help-echo
562 printed-value)))))
565 (defun last-sexp-toggle-display (&optional arg)
566 "Toggle between abbreviated and unabbreviated printed representations."
567 (interactive "P")
568 (save-restriction
569 (widen)
570 (let ((value (get-text-property (point) 'printed-value)))
571 (when value
572 (let ((beg (or (previous-single-property-change (min (point-max) (1+ (point)))
573 'printed-value)
574 (point)))
575 (end (or (next-single-char-property-change (point) 'printed-value) (point)))
576 (standard-output (current-buffer))
577 (point (point)))
578 (delete-region beg end)
579 (insert (nth 1 value))
580 (or (= beg point)
581 (setq point (1- (point))))
582 (last-sexp-setup-props beg (point)
583 (nth 0 value)
584 (nth 2 value)
585 (nth 1 value))
586 (goto-char (min (point-max) point)))))))
588 (defun prin1-char (char)
589 "Return a string representing CHAR as a character rather than as an integer.
590 If CHAR is not a character, return nil."
591 (and (integerp char)
592 (eventp char)
593 (let ((c (event-basic-type char))
594 (mods (event-modifiers char))
595 string)
596 ;; Prevent ?A from turning into ?\S-a.
597 (if (and (memq 'shift mods)
598 (zerop (logand char ?\S-\^@))
599 (not (let ((case-fold-search nil))
600 (char-equal c (upcase c)))))
601 (setq c (upcase c) mods nil))
602 ;; What string are we considering using?
603 (condition-case nil
604 (setq string
605 (concat
607 (mapconcat
608 (lambda (modif)
609 (cond ((eq modif 'super) "\\s-")
610 (t (string ?\\ (upcase (aref (symbol-name modif) 0)) ?-))))
611 mods "")
612 (cond
613 ((memq c '(?\; ?\( ?\) ?\{ ?\} ?\[ ?\] ?\" ?\' ?\\)) (string ?\\ c))
614 ((eq c 127) "\\C-?")
616 (string c)))))
617 (error nil))
618 ;; Verify the string reads a CHAR, not to some other character.
619 ;; If it doesn't, return nil instead.
620 (and string
621 (= (car (read-from-string string)) char)
622 string))))
625 (defun preceding-sexp ()
626 "Return sexp before the point."
627 (let ((opoint (point))
628 ignore-quotes
629 expr)
630 (save-excursion
631 (with-syntax-table emacs-lisp-mode-syntax-table
632 ;; If this sexp appears to be enclosed in `...'
633 ;; then ignore the surrounding quotes.
634 (setq ignore-quotes
635 (or (eq (following-char) ?\')
636 (eq (preceding-char) ?\')))
637 (forward-sexp -1)
638 ;; If we were after `?\e' (or similar case),
639 ;; use the whole thing, not just the `e'.
640 (when (eq (preceding-char) ?\\)
641 (forward-char -1)
642 (when (eq (preceding-char) ??)
643 (forward-char -1)))
645 ;; Skip over `#N='s.
646 (when (eq (preceding-char) ?=)
647 (let (labeled-p)
648 (save-excursion
649 (skip-chars-backward "0-9#=")
650 (setq labeled-p (looking-at "\\(#[0-9]+=\\)+")))
651 (when labeled-p
652 (forward-sexp -1))))
654 (save-restriction
655 ;; vladimir@cs.ualberta.ca 30-Jul-1997: skip ` in
656 ;; `variable' so that the value is returned, not the
657 ;; name
658 (if (and ignore-quotes
659 (eq (following-char) ?`))
660 (forward-char))
661 (narrow-to-region (point-min) opoint)
662 (setq expr (read (current-buffer)))
663 ;; If it's an (interactive ...) form, it's more
664 ;; useful to show how an interactive call would
665 ;; use it.
666 (and (consp expr)
667 (eq (car expr) 'interactive)
668 (setq expr
669 (list 'call-interactively
670 (list 'quote
671 (list 'lambda
672 '(&rest args)
673 expr
674 'args)))))
675 expr)))))
678 (defun eval-last-sexp-1 (eval-last-sexp-arg-internal)
679 "Evaluate sexp before point; print value in minibuffer.
680 With argument, print output into current buffer."
681 (let ((standard-output (if eval-last-sexp-arg-internal (current-buffer) t)))
682 (eval-last-sexp-print-value (eval (preceding-sexp)))))
685 (defun eval-last-sexp-print-value (value)
686 (let ((unabbreviated (let ((print-length nil) (print-level nil))
687 (prin1-to-string value)))
688 (print-length eval-expression-print-length)
689 (print-level eval-expression-print-level)
690 (beg (point))
691 end)
692 (prog1
693 (prin1 value)
694 (let ((str (eval-expression-print-format value)))
695 (if str (princ str)))
696 (setq end (point))
697 (when (and (bufferp standard-output)
698 (or (not (null print-length))
699 (not (null print-level)))
700 (not (string= unabbreviated
701 (buffer-substring-no-properties beg end))))
702 (last-sexp-setup-props beg end value
703 unabbreviated
704 (buffer-substring-no-properties beg end))
705 ))))
708 (defvar eval-last-sexp-fake-value (make-symbol "t"))
710 (defun eval-last-sexp (eval-last-sexp-arg-internal)
711 "Evaluate sexp before point; print value in minibuffer.
712 Interactively, with prefix argument, print output into current buffer.
714 If `eval-expression-debug-on-error' is non-nil, which is the default,
715 this command arranges for all errors to enter the debugger."
716 (interactive "P")
717 (if (null eval-expression-debug-on-error)
718 (eval-last-sexp-1 eval-last-sexp-arg-internal)
719 (let ((value
720 (let ((debug-on-error eval-last-sexp-fake-value))
721 (cons (eval-last-sexp-1 eval-last-sexp-arg-internal)
722 debug-on-error))))
723 (unless (eq (cdr value) eval-last-sexp-fake-value)
724 (setq debug-on-error (cdr value)))
725 (car value))))
727 (defun eval-defun-1 (form)
728 "Treat some expressions specially.
729 Reset the `defvar' and `defcustom' variables to the initial value.
730 Reinitialize the face according to the `defface' specification."
731 ;; The code in edebug-defun should be consistent with this, but not
732 ;; the same, since this gets a macroexpended form.
733 (cond ((not (listp form))
734 form)
735 ((and (eq (car form) 'defvar)
736 (cdr-safe (cdr-safe form))
737 (boundp (cadr form)))
738 ;; Force variable to be re-set.
739 `(progn (defvar ,(nth 1 form) nil ,@(nthcdr 3 form))
740 (setq-default ,(nth 1 form) ,(nth 2 form))))
741 ;; `defcustom' is now macroexpanded to
742 ;; `custom-declare-variable' with a quoted value arg.
743 ((and (eq (car form) 'custom-declare-variable)
744 (default-boundp (eval (nth 1 form))))
745 ;; Force variable to be bound.
746 (set-default (eval (nth 1 form)) (eval (nth 1 (nth 2 form))))
747 form)
748 ;; `defface' is macroexpanded to `custom-declare-face'.
749 ((eq (car form) 'custom-declare-face)
750 ;; Reset the face.
751 (setq face-new-frame-defaults
752 (assq-delete-all (eval (nth 1 form)) face-new-frame-defaults))
753 (put (eval (nth 1 form)) 'face-defface-spec nil)
754 ;; Setting `customized-face' to the new spec after calling
755 ;; the form, but preserving the old saved spec in `saved-face',
756 ;; imitates the situation when the new face spec is set
757 ;; temporarily for the current session in the customize
758 ;; buffer, thus allowing `face-user-default-spec' to use the
759 ;; new customized spec instead of the saved spec.
760 ;; Resetting `saved-face' temporarily to nil is needed to let
761 ;; `defface' change the spec, regardless of a saved spec.
762 (prog1 `(prog1 ,form
763 (put ,(nth 1 form) 'saved-face
764 ',(get (eval (nth 1 form)) 'saved-face))
765 (put ,(nth 1 form) 'customized-face
766 ,(nth 2 form)))
767 (put (eval (nth 1 form)) 'saved-face nil)))
768 ((eq (car form) 'progn)
769 (cons 'progn (mapcar 'eval-defun-1 (cdr form))))
770 (t form)))
772 (defun eval-defun-2 ()
773 "Evaluate defun that point is in or before.
774 The value is displayed in the minibuffer.
775 If the current defun is actually a call to `defvar',
776 then reset the variable using the initial value expression
777 even if the variable already has some other value.
778 \(Normally `defvar' does not change the variable's value
779 if it already has a value.\)
781 With argument, insert value in current buffer after the defun.
782 Return the result of evaluation."
783 (interactive "P")
784 ;; FIXME: the print-length/level bindings should only be applied while
785 ;; printing, not while evaluating.
786 (let ((debug-on-error eval-expression-debug-on-error)
787 (print-length eval-expression-print-length)
788 (print-level eval-expression-print-level))
789 (save-excursion
790 ;; Arrange for eval-region to "read" the (possibly) altered form.
791 ;; eval-region handles recording which file defines a function or
792 ;; variable. Re-written using `apply' to avoid capturing
793 ;; variables like `end'.
794 (apply
795 #'eval-region
796 (let ((standard-output t)
797 beg end form)
798 ;; Read the form from the buffer, and record where it ends.
799 (save-excursion
800 (end-of-defun)
801 (beginning-of-defun)
802 (setq beg (point))
803 (setq form (read (current-buffer)))
804 (setq end (point)))
805 ;; Alter the form if necessary.
806 (setq form (eval-defun-1 (macroexpand form)))
807 (list beg end standard-output
808 `(lambda (ignore)
809 ;; Skipping to the end of the specified region
810 ;; will make eval-region return.
811 (goto-char ,end)
812 ',form))))))
813 ;; The result of evaluation has been put onto VALUES. So return it.
814 (car values))
816 (defun eval-defun (edebug-it)
817 "Evaluate the top-level form containing point, or after point.
819 If the current defun is actually a call to `defvar' or `defcustom',
820 evaluating it this way resets the variable using its initial value
821 expression even if the variable already has some other value.
822 \(Normally `defvar' and `defcustom' do not alter the value if there
823 already is one.) In an analogous way, evaluating a `defface'
824 overrides any customizations of the face, so that it becomes
825 defined exactly as the `defface' expression says.
827 If `eval-expression-debug-on-error' is non-nil, which is the default,
828 this command arranges for all errors to enter the debugger.
830 With a prefix argument, instrument the code for Edebug.
832 If acting on a `defun' for FUNCTION, and the function was
833 instrumented, `Edebug: FUNCTION' is printed in the minibuffer. If not
834 instrumented, just FUNCTION is printed.
836 If not acting on a `defun', the result of evaluation is displayed in
837 the minibuffer. This display is controlled by the variables
838 `eval-expression-print-length' and `eval-expression-print-level',
839 which see."
840 (interactive "P")
841 (cond (edebug-it
842 (require 'edebug)
843 (eval-defun (not edebug-all-defs)))
845 (if (null eval-expression-debug-on-error)
846 (eval-defun-2)
847 (let ((old-value (make-symbol "t")) new-value value)
848 (let ((debug-on-error old-value))
849 (setq value (eval-defun-2))
850 (setq new-value debug-on-error))
851 (unless (eq old-value new-value)
852 (setq debug-on-error new-value))
853 value)))))
855 ;; May still be used by some external Lisp-mode variant.
856 (define-obsolete-function-alias 'lisp-comment-indent
857 'comment-indent-default "22.1")
858 (define-obsolete-function-alias 'lisp-mode-auto-fill 'do-auto-fill "23.1")
860 (defcustom lisp-indent-offset nil
861 "If non-nil, indent second line of expressions that many more columns."
862 :group 'lisp
863 :type '(choice nil integer))
864 (put 'lisp-body-indent 'safe-local-variable
865 (lambda (x) (or (null x) (integerp x))))
867 (defvar lisp-indent-function 'lisp-indent-function)
869 (defun lisp-indent-line (&optional whole-exp)
870 "Indent current line as Lisp code.
871 With argument, indent any additional lines of the same expression
872 rigidly along with this one."
873 (interactive "P")
874 (let ((indent (calculate-lisp-indent)) shift-amt end
875 (pos (- (point-max) (point)))
876 (beg (progn (beginning-of-line) (point))))
877 (skip-chars-forward " \t")
878 (if (or (null indent) (looking-at "\\s<\\s<\\s<"))
879 ;; Don't alter indentation of a ;;; comment line
880 ;; or a line that starts in a string.
881 (goto-char (- (point-max) pos))
882 (if (and (looking-at "\\s<") (not (looking-at "\\s<\\s<")))
883 ;; Single-semicolon comment lines should be indented
884 ;; as comment lines, not as code.
885 (progn (indent-for-comment) (forward-char -1))
886 (if (listp indent) (setq indent (car indent)))
887 (setq shift-amt (- indent (current-column)))
888 (if (zerop shift-amt)
890 (delete-region beg (point))
891 (indent-to indent)))
892 ;; If initial point was within line's indentation,
893 ;; position after the indentation. Else stay at same point in text.
894 (if (> (- (point-max) pos) (point))
895 (goto-char (- (point-max) pos)))
896 ;; If desired, shift remaining lines of expression the same amount.
897 (and whole-exp (not (zerop shift-amt))
898 (save-excursion
899 (goto-char beg)
900 (forward-sexp 1)
901 (setq end (point))
902 (goto-char beg)
903 (forward-line 1)
904 (setq beg (point))
905 (> end beg))
906 (indent-code-rigidly beg end shift-amt)))))
908 (defvar calculate-lisp-indent-last-sexp)
910 (defun calculate-lisp-indent (&optional parse-start)
911 "Return appropriate indentation for current line as Lisp code.
912 In usual case returns an integer: the column to indent to.
913 If the value is nil, that means don't change the indentation
914 because the line starts inside a string.
916 The value can also be a list of the form (COLUMN CONTAINING-SEXP-START).
917 This means that following lines at the same level of indentation
918 should not necessarily be indented the same as this line.
919 Then COLUMN is the column to indent to, and CONTAINING-SEXP-START
920 is the buffer position of the start of the containing expression."
921 (save-excursion
922 (beginning-of-line)
923 (let ((indent-point (point))
924 state paren-depth
925 ;; setting this to a number inhibits calling hook
926 (desired-indent nil)
927 (retry t)
928 calculate-lisp-indent-last-sexp containing-sexp)
929 (if parse-start
930 (goto-char parse-start)
931 (beginning-of-defun))
932 ;; Find outermost containing sexp
933 (while (< (point) indent-point)
934 (setq state (parse-partial-sexp (point) indent-point 0)))
935 ;; Find innermost containing sexp
936 (while (and retry
937 state
938 (> (setq paren-depth (elt state 0)) 0))
939 (setq retry nil)
940 (setq calculate-lisp-indent-last-sexp (elt state 2))
941 (setq containing-sexp (elt state 1))
942 ;; Position following last unclosed open.
943 (goto-char (1+ containing-sexp))
944 ;; Is there a complete sexp since then?
945 (if (and calculate-lisp-indent-last-sexp
946 (> calculate-lisp-indent-last-sexp (point)))
947 ;; Yes, but is there a containing sexp after that?
948 (let ((peek (parse-partial-sexp calculate-lisp-indent-last-sexp
949 indent-point 0)))
950 (if (setq retry (car (cdr peek))) (setq state peek)))))
951 (if retry
953 ;; Innermost containing sexp found
954 (goto-char (1+ containing-sexp))
955 (if (not calculate-lisp-indent-last-sexp)
956 ;; indent-point immediately follows open paren.
957 ;; Don't call hook.
958 (setq desired-indent (current-column))
959 ;; Find the start of first element of containing sexp.
960 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
961 (cond ((looking-at "\\s(")
962 ;; First element of containing sexp is a list.
963 ;; Indent under that list.
965 ((> (save-excursion (forward-line 1) (point))
966 calculate-lisp-indent-last-sexp)
967 ;; This is the first line to start within the containing sexp.
968 ;; It's almost certainly a function call.
969 (if (= (point) calculate-lisp-indent-last-sexp)
970 ;; Containing sexp has nothing before this line
971 ;; except the first element. Indent under that element.
973 ;; Skip the first element, find start of second (the first
974 ;; argument of the function call) and indent under.
975 (progn (forward-sexp 1)
976 (parse-partial-sexp (point)
977 calculate-lisp-indent-last-sexp
978 0 t)))
979 (backward-prefix-chars))
981 ;; Indent beneath first sexp on same line as
982 ;; `calculate-lisp-indent-last-sexp'. Again, it's
983 ;; almost certainly a function call.
984 (goto-char calculate-lisp-indent-last-sexp)
985 (beginning-of-line)
986 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp
987 0 t)
988 (backward-prefix-chars)))))
989 ;; Point is at the point to indent under unless we are inside a string.
990 ;; Call indentation hook except when overridden by lisp-indent-offset
991 ;; or if the desired indentation has already been computed.
992 (let ((normal-indent (current-column)))
993 (cond ((elt state 3)
994 ;; Inside a string, don't change indentation.
995 nil)
996 ((and (integerp lisp-indent-offset) containing-sexp)
997 ;; Indent by constant offset
998 (goto-char containing-sexp)
999 (+ (current-column) lisp-indent-offset))
1000 ;; in this case calculate-lisp-indent-last-sexp is not nil
1001 (calculate-lisp-indent-last-sexp
1003 ;; try to align the parameters of a known function
1004 (and lisp-indent-function
1005 (not retry)
1006 (funcall lisp-indent-function indent-point state))
1007 ;; If the function has no special alignment
1008 ;; or it does not apply to this argument,
1009 ;; try to align a constant-symbol under the last
1010 ;; preceding constant symbol, if there is such one of
1011 ;; the last 2 preceding symbols, in the previous
1012 ;; uncommented line.
1013 (and (save-excursion
1014 (goto-char indent-point)
1015 (skip-chars-forward " \t")
1016 (looking-at ":"))
1017 ;; The last sexp may not be at the indentation
1018 ;; where it begins, so find that one, instead.
1019 (save-excursion
1020 (goto-char calculate-lisp-indent-last-sexp)
1021 (while (and (not (looking-back "^[ \t]*"))
1022 (or (not containing-sexp)
1023 (< (1+ containing-sexp) (point))))
1024 (forward-sexp -1)
1025 (backward-prefix-chars))
1026 (setq calculate-lisp-indent-last-sexp (point)))
1027 (> calculate-lisp-indent-last-sexp
1028 (save-excursion
1029 (goto-char (1+ containing-sexp))
1030 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
1031 (point)))
1032 (let ((parse-sexp-ignore-comments t)
1033 indent)
1034 (goto-char calculate-lisp-indent-last-sexp)
1035 (or (and (looking-at ":")
1036 (setq indent (current-column)))
1037 (and (< (save-excursion (beginning-of-line) (point))
1038 (prog2 (backward-sexp) (point)))
1039 (looking-at ":")
1040 (setq indent (current-column))))
1041 indent))
1042 ;; another symbols or constants not preceded by a constant
1043 ;; as defined above.
1044 normal-indent))
1045 ;; in this case calculate-lisp-indent-last-sexp is nil
1046 (desired-indent)
1048 normal-indent))))))
1050 (defun lisp-indent-function (indent-point state)
1051 "This function is the normal value of the variable `lisp-indent-function'.
1052 It is used when indenting a line within a function call, to see if the
1053 called function says anything special about how to indent the line.
1055 INDENT-POINT is the position where the user typed TAB, or equivalent.
1056 Point is located at the point to indent under (for default indentation);
1057 STATE is the `parse-partial-sexp' state for that position.
1059 If the current line is in a call to a Lisp function
1060 which has a non-nil property `lisp-indent-function',
1061 that specifies how to do the indentation. The property value can be
1062 * `defun', meaning indent `defun'-style;
1063 * an integer N, meaning indent the first N arguments specially
1064 like ordinary function arguments and then indent any further
1065 arguments like a body;
1066 * a function to call just as this function was called.
1067 If that function returns nil, that means it doesn't specify
1068 the indentation.
1070 This function also returns nil meaning don't specify the indentation."
1071 (let ((normal-indent (current-column)))
1072 (goto-char (1+ (elt state 1)))
1073 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
1074 (if (and (elt state 2)
1075 (not (looking-at "\\sw\\|\\s_")))
1076 ;; car of form doesn't seem to be a symbol
1077 (progn
1078 (if (not (> (save-excursion (forward-line 1) (point))
1079 calculate-lisp-indent-last-sexp))
1080 (progn (goto-char calculate-lisp-indent-last-sexp)
1081 (beginning-of-line)
1082 (parse-partial-sexp (point)
1083 calculate-lisp-indent-last-sexp 0 t)))
1084 ;; Indent under the list or under the first sexp on the same
1085 ;; line as calculate-lisp-indent-last-sexp. Note that first
1086 ;; thing on that line has to be complete sexp since we are
1087 ;; inside the innermost containing sexp.
1088 (backward-prefix-chars)
1089 (current-column))
1090 (let ((function (buffer-substring (point)
1091 (progn (forward-sexp 1) (point))))
1092 method)
1093 (setq method (or (get (intern-soft function) 'lisp-indent-function)
1094 (get (intern-soft function) 'lisp-indent-hook)))
1095 (cond ((or (eq method 'defun)
1096 (and (null method)
1097 (> (length function) 3)
1098 (string-match "\\`def" function)))
1099 (lisp-indent-defform state indent-point))
1100 ((integerp method)
1101 (lisp-indent-specform method state
1102 indent-point normal-indent))
1103 (method
1104 (funcall method indent-point state)))))))
1106 (defcustom lisp-body-indent 2
1107 "Number of columns to indent the second line of a `(def...)' form."
1108 :group 'lisp
1109 :type 'integer)
1110 (put 'lisp-body-indent 'safe-local-variable 'integerp)
1112 (defun lisp-indent-specform (count state indent-point normal-indent)
1113 (let ((containing-form-start (elt state 1))
1114 (i count)
1115 body-indent containing-form-column)
1116 ;; Move to the start of containing form, calculate indentation
1117 ;; to use for non-distinguished forms (> count), and move past the
1118 ;; function symbol. lisp-indent-function guarantees that there is at
1119 ;; least one word or symbol character following open paren of containing
1120 ;; form.
1121 (goto-char containing-form-start)
1122 (setq containing-form-column (current-column))
1123 (setq body-indent (+ lisp-body-indent containing-form-column))
1124 (forward-char 1)
1125 (forward-sexp 1)
1126 ;; Now find the start of the last form.
1127 (parse-partial-sexp (point) indent-point 1 t)
1128 (while (and (< (point) indent-point)
1129 (condition-case ()
1130 (progn
1131 (setq count (1- count))
1132 (forward-sexp 1)
1133 (parse-partial-sexp (point) indent-point 1 t))
1134 (error nil))))
1135 ;; Point is sitting on first character of last (or count) sexp.
1136 (if (> count 0)
1137 ;; A distinguished form. If it is the first or second form use double
1138 ;; lisp-body-indent, else normal indent. With lisp-body-indent bound
1139 ;; to 2 (the default), this just happens to work the same with if as
1140 ;; the older code, but it makes unwind-protect, condition-case,
1141 ;; with-output-to-temp-buffer, et. al. much more tasteful. The older,
1142 ;; less hacked, behavior can be obtained by replacing below with
1143 ;; (list normal-indent containing-form-start).
1144 (if (<= (- i count) 1)
1145 (list (+ containing-form-column (* 2 lisp-body-indent))
1146 containing-form-start)
1147 (list normal-indent containing-form-start))
1148 ;; A non-distinguished form. Use body-indent if there are no
1149 ;; distinguished forms and this is the first undistinguished form,
1150 ;; or if this is the first undistinguished form and the preceding
1151 ;; distinguished form has indentation at least as great as body-indent.
1152 (if (or (and (= i 0) (= count 0))
1153 (and (= count 0) (<= body-indent normal-indent)))
1154 body-indent
1155 normal-indent))))
1157 (defun lisp-indent-defform (state indent-point)
1158 (goto-char (car (cdr state)))
1159 (forward-line 1)
1160 (if (> (point) (car (cdr (cdr state))))
1161 (progn
1162 (goto-char (car (cdr state)))
1163 (+ lisp-body-indent (current-column)))))
1166 ;; (put 'progn 'lisp-indent-function 0), say, causes progn to be indented
1167 ;; like defun if the first form is placed on the next line, otherwise
1168 ;; it is indented like any other form (i.e. forms line up under first).
1170 (put 'lambda 'lisp-indent-function 'defun)
1171 (put 'autoload 'lisp-indent-function 'defun)
1172 (put 'progn 'lisp-indent-function 0)
1173 (put 'prog1 'lisp-indent-function 1)
1174 (put 'prog2 'lisp-indent-function 2)
1175 (put 'save-excursion 'lisp-indent-function 0)
1176 (put 'save-window-excursion 'lisp-indent-function 0)
1177 (put 'save-selected-window 'lisp-indent-function 0)
1178 (put 'save-restriction 'lisp-indent-function 0)
1179 (put 'save-match-data 'lisp-indent-function 0)
1180 (put 'save-current-buffer 'lisp-indent-function 0)
1181 (put 'with-current-buffer 'lisp-indent-function 1)
1182 (put 'combine-after-change-calls 'lisp-indent-function 0)
1183 (put 'with-output-to-string 'lisp-indent-function 0)
1184 (put 'with-temp-file 'lisp-indent-function 1)
1185 (put 'with-temp-buffer 'lisp-indent-function 0)
1186 (put 'with-temp-message 'lisp-indent-function 1)
1187 (put 'with-syntax-table 'lisp-indent-function 1)
1188 (put 'let 'lisp-indent-function 1)
1189 (put 'let* 'lisp-indent-function 1)
1190 (put 'while 'lisp-indent-function 1)
1191 (put 'if 'lisp-indent-function 2)
1192 (put 'read-if 'lisp-indent-function 2)
1193 (put 'catch 'lisp-indent-function 1)
1194 (put 'condition-case 'lisp-indent-function 2)
1195 (put 'unwind-protect 'lisp-indent-function 1)
1196 (put 'with-output-to-temp-buffer 'lisp-indent-function 1)
1197 (put 'eval-after-load 'lisp-indent-function 1)
1198 (put 'dolist 'lisp-indent-function 1)
1199 (put 'dotimes 'lisp-indent-function 1)
1200 (put 'when 'lisp-indent-function 1)
1201 (put 'unless 'lisp-indent-function 1)
1203 (defun indent-sexp (&optional endpos)
1204 "Indent each line of the list starting just after point.
1205 If optional arg ENDPOS is given, indent each line, stopping when
1206 ENDPOS is encountered."
1207 (interactive)
1208 (let ((indent-stack (list nil))
1209 (next-depth 0)
1210 ;; If ENDPOS is non-nil, use nil as STARTING-POINT
1211 ;; so that calculate-lisp-indent will find the beginning of
1212 ;; the defun we are in.
1213 ;; If ENDPOS is nil, it is safe not to scan before point
1214 ;; since every line we indent is more deeply nested than point is.
1215 (starting-point (if endpos nil (point)))
1216 (last-point (point))
1217 last-depth bol outer-loop-done inner-loop-done state this-indent)
1218 (or endpos
1219 ;; Get error now if we don't have a complete sexp after point.
1220 (save-excursion (forward-sexp 1)))
1221 (save-excursion
1222 (setq outer-loop-done nil)
1223 (while (if endpos (< (point) endpos)
1224 (not outer-loop-done))
1225 (setq last-depth next-depth
1226 inner-loop-done nil)
1227 ;; Parse this line so we can learn the state
1228 ;; to indent the next line.
1229 ;; This inner loop goes through only once
1230 ;; unless a line ends inside a string.
1231 (while (and (not inner-loop-done)
1232 (not (setq outer-loop-done (eobp))))
1233 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
1234 nil nil state))
1235 (setq next-depth (car state))
1236 ;; If the line contains a comment other than the sort
1237 ;; that is indented like code,
1238 ;; indent it now with indent-for-comment.
1239 ;; Comments indented like code are right already.
1240 ;; In any case clear the in-comment flag in the state
1241 ;; because parse-partial-sexp never sees the newlines.
1242 (if (car (nthcdr 4 state))
1243 (progn (indent-for-comment)
1244 (end-of-line)
1245 (setcar (nthcdr 4 state) nil)))
1246 ;; If this line ends inside a string,
1247 ;; go straight to next line, remaining within the inner loop,
1248 ;; and turn off the \-flag.
1249 (if (car (nthcdr 3 state))
1250 (progn
1251 (forward-line 1)
1252 (setcar (nthcdr 5 state) nil))
1253 (setq inner-loop-done t)))
1254 (and endpos
1255 (<= next-depth 0)
1256 (progn
1257 (setq indent-stack (nconc indent-stack
1258 (make-list (- next-depth) nil))
1259 last-depth (- last-depth next-depth)
1260 next-depth 0)))
1261 (forward-line 1)
1262 ;; Decide whether to exit.
1263 (if endpos
1264 ;; If we have already reached the specified end,
1265 ;; give up and do not reindent this line.
1266 (if (<= endpos (point))
1267 (setq outer-loop-done t))
1268 ;; If no specified end, we are done if we have finished one sexp.
1269 (if (<= next-depth 0)
1270 (setq outer-loop-done t)))
1271 (unless outer-loop-done
1272 (while (> last-depth next-depth)
1273 (setq indent-stack (cdr indent-stack)
1274 last-depth (1- last-depth)))
1275 (while (< last-depth next-depth)
1276 (setq indent-stack (cons nil indent-stack)
1277 last-depth (1+ last-depth)))
1278 ;; Now indent the next line according
1279 ;; to what we learned from parsing the previous one.
1280 (setq bol (point))
1281 (skip-chars-forward " \t")
1282 ;; But not if the line is blank, or just a comment
1283 ;; (except for double-semi comments; indent them as usual).
1284 (if (or (eobp) (looking-at "\\s<\\|\n"))
1286 (if (and (car indent-stack)
1287 (>= (car indent-stack) 0))
1288 (setq this-indent (car indent-stack))
1289 (let ((val (calculate-lisp-indent
1290 (if (car indent-stack) (- (car indent-stack))
1291 starting-point))))
1292 (if (null val)
1293 (setq this-indent val)
1294 (if (integerp val)
1295 (setcar indent-stack
1296 (setq this-indent val))
1297 (setcar indent-stack (- (car (cdr val))))
1298 (setq this-indent (car val))))))
1299 (if (and this-indent (/= (current-column) this-indent))
1300 (progn (delete-region bol (point))
1301 (indent-to this-indent)))))
1302 (or outer-loop-done
1303 (setq outer-loop-done (= (point) last-point))
1304 (setq last-point (point)))))))
1306 (defun lisp-indent-region (start end)
1307 "Indent every line whose first char is between START and END inclusive."
1308 (save-excursion
1309 (let ((endmark (copy-marker end)))
1310 (goto-char start)
1311 (and (bolp) (not (eolp))
1312 (lisp-indent-line))
1313 (indent-sexp endmark)
1314 (set-marker endmark nil))))
1316 (defun indent-pp-sexp (&optional arg)
1317 "Indent each line of the list starting just after point, or prettyprint it.
1318 A prefix argument specifies pretty-printing."
1319 (interactive "P")
1320 (if arg
1321 (save-excursion
1322 (save-restriction
1323 (narrow-to-region (point) (progn (forward-sexp 1) (point)))
1324 (pp-buffer)
1325 (goto-char (point-max))
1326 (if (eq (char-before) ?\n)
1327 (delete-char -1)))))
1328 (indent-sexp))
1330 ;;;; Lisp paragraph filling commands.
1332 (defcustom emacs-lisp-docstring-fill-column 65
1333 "Value of `fill-column' to use when filling a docstring.
1334 Any non-integer value means do not use a different value of
1335 `fill-column' when filling docstrings."
1336 :type '(choice (integer)
1337 (const :tag "Use the current `fill-column'" t))
1338 :group 'lisp)
1340 (defun lisp-fill-paragraph (&optional justify)
1341 "Like \\[fill-paragraph], but handle Emacs Lisp comments and docstrings.
1342 If any of the current line is a comment, fill the comment or the
1343 paragraph of it that point is in, preserving the comment's indentation
1344 and initial semicolons."
1345 (interactive "P")
1346 (or (fill-comment-paragraph justify)
1347 ;; Since fill-comment-paragraph returned nil, that means we're not in
1348 ;; a comment: Point is on a program line; we are interested
1349 ;; particularly in docstring lines.
1351 ;; We bind `paragraph-start' and `paragraph-separate' temporarily. They
1352 ;; are buffer-local, but we avoid changing them so that they can be set
1353 ;; to make `forward-paragraph' and friends do something the user wants.
1355 ;; `paragraph-start': The `(' in the character alternative and the
1356 ;; left-singlequote plus `(' sequence after the \\| alternative prevent
1357 ;; sexps and backquoted sexps that follow a docstring from being filled
1358 ;; with the docstring. This setting has the consequence of inhibiting
1359 ;; filling many program lines that are not docstrings, which is sensible,
1360 ;; because the user probably asked to fill program lines by accident, or
1361 ;; expecting indentation (perhaps we should try to do indenting in that
1362 ;; case). The `;' and `:' stop the paragraph being filled at following
1363 ;; comment lines and at keywords (e.g., in `defcustom'). Left parens are
1364 ;; escaped to keep font-locking, filling, & paren matching in the source
1365 ;; file happy.
1367 ;; `paragraph-separate': A clever regexp distinguishes the first line of
1368 ;; a docstring and identifies it as a paragraph separator, so that it
1369 ;; won't be filled. (Since the first line of documentation stands alone
1370 ;; in some contexts, filling should not alter the contents the author has
1371 ;; chosen.) Only the first line of a docstring begins with whitespace
1372 ;; and a quotation mark and ends with a period or (rarely) a comma.
1374 ;; The `fill-column' is temporarily bound to
1375 ;; `emacs-lisp-docstring-fill-column' if that value is an integer.
1376 (let ((paragraph-start (concat paragraph-start
1377 "\\|\\s-*\\([(;:\"]\\|`(\\|#'(\\)"))
1378 (paragraph-separate
1379 (concat paragraph-separate "\\|\\s-*\".*[,\\.]$"))
1380 (fill-column (if (and (integerp emacs-lisp-docstring-fill-column)
1381 (derived-mode-p 'emacs-lisp-mode))
1382 emacs-lisp-docstring-fill-column
1383 fill-column)))
1384 (fill-paragraph justify))
1385 ;; Never return nil.
1388 (defun indent-code-rigidly (start end arg &optional nochange-regexp)
1389 "Indent all lines of code, starting in the region, sideways by ARG columns.
1390 Does not affect lines starting inside comments or strings, assuming that
1391 the start of the region is not inside them.
1393 Called from a program, takes args START, END, COLUMNS and NOCHANGE-REGEXP.
1394 The last is a regexp which, if matched at the beginning of a line,
1395 means don't indent that line."
1396 (interactive "r\np")
1397 (let (state)
1398 (save-excursion
1399 (goto-char end)
1400 (setq end (point-marker))
1401 (goto-char start)
1402 (or (bolp)
1403 (setq state (parse-partial-sexp (point)
1404 (progn
1405 (forward-line 1) (point))
1406 nil nil state)))
1407 (while (< (point) end)
1408 (or (car (nthcdr 3 state))
1409 (and nochange-regexp
1410 (looking-at nochange-regexp))
1411 ;; If line does not start in string, indent it
1412 (let ((indent (current-indentation)))
1413 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
1414 (or (eolp)
1415 (indent-to (max 0 (+ indent arg)) 0))))
1416 (setq state (parse-partial-sexp (point)
1417 (progn
1418 (forward-line 1) (point))
1419 nil nil state))))))
1421 (provide 'lisp-mode)
1423 ;; arch-tag: 414c7f93-c245-4b77-8ed5-ed05ef7ff1bf
1424 ;;; lisp-mode.el ends here