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