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