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