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