(define-minor-mode): Docstring fix.
[bpt/emacs.git] / lisp / emacs-lisp / lisp-mode.el
CommitLineData
6594deb0
ER
1;;; lisp-mode.el --- Lisp mode, and its idiosyncratic commands.
2
535eadac 3;; Copyright (C) 1985, 1986, 1999, 2000 Free Software Foundation, Inc.
3a801d0c 4
e5167999 5;; Maintainer: FSF
fd7fa35a 6;; Keywords: lisp, languages
e5167999 7
a90256cc
BP
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software; you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
e5167999 12;; the Free Software Foundation; either version 2, or (at your option)
a90256cc
BP
13;; any later version.
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
b578f267
EN
21;; along with GNU Emacs; see the file COPYING. If not, write to the
22;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23;; Boston, MA 02111-1307, USA.
a90256cc 24
e41b2db1
ER
25;;; Commentary:
26
27;; The base major mode for editing Lisp code (used also for Emacs Lisp).
535eadac 28;; This mode is documented in the Emacs manual.
e41b2db1
ER
29
30;;; Code:
31
535eadac 32(defvar lisp-mode-abbrev-table nil)
a90256cc 33
535eadac
DL
34(defvar emacs-lisp-mode-syntax-table
35 (let ((table (make-syntax-table)))
a90256cc 36 (let ((i 0))
a90256cc 37 (while (< i ?0)
535eadac 38 (modify-syntax-entry i "_ " table)
a90256cc
BP
39 (setq i (1+ i)))
40 (setq i (1+ ?9))
41 (while (< i ?A)
535eadac 42 (modify-syntax-entry i "_ " table)
a90256cc
BP
43 (setq i (1+ i)))
44 (setq i (1+ ?Z))
45 (while (< i ?a)
535eadac 46 (modify-syntax-entry i "_ " table)
a90256cc
BP
47 (setq i (1+ i)))
48 (setq i (1+ ?z))
49 (while (< i 128)
535eadac 50 (modify-syntax-entry i "_ " table)
a90256cc 51 (setq i (1+ i)))
535eadac
DL
52 (modify-syntax-entry ? " " table)
53 (modify-syntax-entry ?\t " " table)
54 (modify-syntax-entry ?\f " " table)
55 (modify-syntax-entry ?\n "> " table)
910762b4 56 ;; Give CR the same syntax as newline, for selective-display.
535eadac
DL
57 (modify-syntax-entry ?\^m "> " table)
58 (modify-syntax-entry ?\; "< " table)
59 (modify-syntax-entry ?` "' " table)
60 (modify-syntax-entry ?' "' " table)
61 (modify-syntax-entry ?, "' " table)
a90256cc 62 ;; Used to be singlequote; changed for flonums.
535eadac
DL
63 (modify-syntax-entry ?. "_ " table)
64 (modify-syntax-entry ?# "' " table)
65 (modify-syntax-entry ?\" "\" " table)
66 (modify-syntax-entry ?\\ "\\ " table)
67 (modify-syntax-entry ?\( "() " table)
68 (modify-syntax-entry ?\) ")( " table)
69 (modify-syntax-entry ?\[ "(] " table)
1d3529b6 70 (modify-syntax-entry ?\] ")[ " table))
535eadac
DL
71 table))
72
73(defvar lisp-mode-syntax-table
74 (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table)))
535eadac
DL
75 (modify-syntax-entry ?\[ "_ " table)
76 (modify-syntax-entry ?\] "_ " table)
77 (modify-syntax-entry ?# "' 14bn" table)
e5f597f0 78 (modify-syntax-entry ?| "\" 23b" table)
535eadac 79 table))
ecca85de 80
a90256cc
BP
81(define-abbrev-table 'lisp-mode-abbrev-table ())
82
6c2cf866 83(defvar lisp-imenu-generic-expression
535eadac 84 (list
ca2ddd8e 85 (list nil
b0d99853 86 (purecopy "^\\s-*(def\\(un\\*?\\|subst\\|macro\\|advice\\|\
535eadac 87ine-skeleton\\|ine-minor-mode\\)\\s-+\\(\\sw\\(\\sw\\|\\s_\\)+\\)") 2)
ca2ddd8e 88 (list (purecopy "Variables")
535eadac
DL
89 (purecopy "^\\s-*(def\\(var\\|const\\|custom\\)\\s-+\
90\\(\\sw\\(\\sw\\|\\s_\\)+\\)") 2)
ca2ddd8e 91 (list (purecopy "Types")
535eadac 92 (purecopy "^\\s-*(def\\(group\\|type\\|struct\\|class\\|\
ca2ddd8e 93ine-condition\\|ine-widget\\|face\\)\\s-+'?\\(\\sw\\(\\sw\\|\\s_\\)+\\)")
6c2cf866
KH
94 2))
95
96 "Imenu generic expression for Lisp mode. See `imenu-generic-expression'.")
97
a90256cc
BP
98(defun lisp-mode-variables (lisp-syntax)
99 (cond (lisp-syntax
a90256cc
BP
100 (set-syntax-table lisp-mode-syntax-table)))
101 (setq local-abbrev-table lisp-mode-abbrev-table)
102 (make-local-variable 'paragraph-start)
763d1579 103 (setq paragraph-start (concat page-delimiter "\\|$" ))
a90256cc
BP
104 (make-local-variable 'paragraph-separate)
105 (setq paragraph-separate paragraph-start)
106 (make-local-variable 'paragraph-ignore-fill-prefix)
107 (setq paragraph-ignore-fill-prefix t)
35d132a8
RS
108 (make-local-variable 'fill-paragraph-function)
109 (setq fill-paragraph-function 'lisp-fill-paragraph)
3272c162
RS
110 ;; Adaptive fill mode gets in the way of auto-fill,
111 ;; and should make no difference for explicit fill
112 ;; because lisp-fill-paragraph should do the job.
113 (make-local-variable 'adaptive-fill-mode)
114 (setq adaptive-fill-mode nil)
7eb67d79
RS
115 (make-local-variable 'normal-auto-fill-function)
116 (setq normal-auto-fill-function 'lisp-mode-auto-fill)
a90256cc
BP
117 (make-local-variable 'indent-line-function)
118 (setq indent-line-function 'lisp-indent-line)
119 (make-local-variable 'indent-region-function)
120 (setq indent-region-function 'lisp-indent-region)
121 (make-local-variable 'parse-sexp-ignore-comments)
122 (setq parse-sexp-ignore-comments t)
5847f861 123 (make-local-variable 'outline-regexp)
a8050bff
GM
124 (setq outline-regexp ";;;;* \\|(")
125 (make-local-variable 'outline-level)
126 (setq outline-level 'lisp-outline-level)
a90256cc
BP
127 (make-local-variable 'comment-start)
128 (setq comment-start ";")
129 (make-local-variable 'comment-start-skip)
e56a043b
MB
130 ;; Look within the line for a ; following an even number of backslashes
131 ;; after either a non-backslash or the line beginning.
132 (setq comment-start-skip "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\);+ *")
3f0c3d8b
SM
133 (make-local-variable 'comment-add)
134 (setq comment-add 1) ;default to `;;' in comment-region
a90256cc
BP
135 (make-local-variable 'comment-column)
136 (setq comment-column 40)
e41b2db1 137 (make-local-variable 'comment-indent-function)
6c2cf866
KH
138 (setq comment-indent-function 'lisp-comment-indent)
139 (make-local-variable 'imenu-generic-expression)
1d3529b6
KH
140 (setq imenu-generic-expression lisp-imenu-generic-expression)
141 (make-local-variable 'multibyte-syntax-as-symbol)
1594a23a
SM
142 (setq multibyte-syntax-as-symbol t)
143 (setq font-lock-defaults
144 '((lisp-font-lock-keywords
145 lisp-font-lock-keywords-1 lisp-font-lock-keywords-2)
146 nil nil (("+-*/.<>=!?$%_&~^:" . "w")) beginning-of-defun
147 (font-lock-mark-block-function . mark-defun))))
a8050bff
GM
148
149(defun lisp-outline-level ()
150 "Lisp mode `outline-level' function."
151 (if (looking-at "(")
152 1000
153 (looking-at outline-regexp)
154 (- (match-end 0) (match-beginning 0))))
155
ca2ddd8e 156
1594a23a
SM
157(defvar lisp-mode-shared-map
158 (let ((map (make-sparse-keymap)))
159 (define-key map "\e\C-q" 'indent-sexp)
160 (define-key map "\177" 'backward-delete-char-untabify)
ca2ddd8e 161 (define-key map [backspace] 'backward-delete-char-untabify)
1594a23a 162 map)
a90256cc
BP
163 "Keymap for commands shared by all sorts of Lisp modes.")
164
a90256cc
BP
165(defvar emacs-lisp-mode-map ()
166 "Keymap for Emacs Lisp mode.
99ec65b3 167All commands in `lisp-mode-shared-map' are inherited by this map.")
a90256cc
BP
168
169(if emacs-lisp-mode-map
170 ()
b3a3cb63 171 (let ((map (make-sparse-keymap "Emacs-Lisp")))
b8bc6df2 172 (setq emacs-lisp-mode-map (make-sparse-keymap))
99ec65b3 173 (set-keymap-parent emacs-lisp-mode-map lisp-mode-shared-map)
b3a3cb63
KH
174 (define-key emacs-lisp-mode-map "\e\t" 'lisp-complete-symbol)
175 (define-key emacs-lisp-mode-map "\e\C-x" 'eval-defun)
176 (define-key emacs-lisp-mode-map [menu-bar] (make-sparse-keymap))
177 (define-key emacs-lisp-mode-map [menu-bar emacs-lisp]
178 (cons "Emacs-Lisp" map))
179 (define-key map [edebug-defun]
180 '("Instrument Function for Debugging" . edebug-defun))
181 (define-key map [byte-recompile]
182 '("Byte-recompile Directory..." . byte-recompile-directory))
00e40cc9 183 (define-key map [emacs-byte-compile-and-load]
eaec854f 184 '("Byte-compile And Load" . emacs-lisp-byte-compile-and-load))
b3a3cb63
KH
185 (define-key map [byte-compile]
186 '("Byte-compile This File" . emacs-lisp-byte-compile))
187 (define-key map [separator-eval] '("--"))
188 (define-key map [eval-buffer] '("Evaluate Buffer" . eval-current-buffer))
189 (define-key map [eval-region] '("Evaluate Region" . eval-region))
190 (define-key map [eval-sexp] '("Evaluate Last S-expression" . eval-last-sexp))
191 (define-key map [separator-format] '("--"))
192 (define-key map [comment-region] '("Comment Out Region" . comment-region))
193 (define-key map [indent-region] '("Indent Region" . indent-region))
4b619eca
SM
194 (define-key map [indent-line] '("Indent Line" . lisp-indent-line))
195 (put 'eval-region 'menu-enable 'mark-active)
196 (put 'comment-region 'menu-enable 'mark-active)
197 (put 'indent-region 'menu-enable 'mark-active)))
b3a3cb63
KH
198
199(defun emacs-lisp-byte-compile ()
200 "Byte compile the file containing the current buffer."
201 (interactive)
202 (if buffer-file-name
203 (byte-compile-file buffer-file-name)
767a1151
KH
204 (error "The buffer must be saved in a file first")))
205
eaec854f 206(defun emacs-lisp-byte-compile-and-load ()
767a1151
KH
207 "Byte-compile the current file (if it has changed), then load compiled code."
208 (interactive)
209 (or buffer-file-name
210 (error "The buffer must be saved in a file first"))
211 (require 'bytecomp)
212 ;; Recompile if file or buffer has changed since last compilation.
eaec854f 213 (if (and (buffer-modified-p)
535eadac 214 (y-or-n-p (format "Save buffer %s first? " (buffer-name))))
eaec854f
SM
215 (save-buffer))
216 (let ((compiled-file-name (byte-compile-dest-file buffer-file-name)))
217 (if (file-newer-than-file-p compiled-file-name buffer-file-name)
218 (load-file compiled-file-name)
219 (byte-compile-file buffer-file-name t))))
a90256cc 220
e596094d
DL
221(defcustom emacs-lisp-mode-hook nil
222 "Hook run when entering Emacs Lisp mode."
535eadac 223 :options '(turn-on-eldoc-mode imenu-add-menubar-index checkdoc-minor-mode)
e596094d
DL
224 :type 'hook
225 :group 'lisp)
226
227(defcustom lisp-mode-hook nil
228 "Hook run when entering Lisp mode."
229 :options '(imenu-add-menubar-index)
230 :type 'hook
231 :group 'lisp)
232
233(defcustom lisp-interaction-mode-hook nil
234 "Hook run when entering Lisp Interaction mode."
235 :options '(turn-on-eldoc-mode)
236 :type 'hook
237 :group 'lisp)
238
1594a23a 239(define-derived-mode emacs-lisp-mode nil "Emacs-Lisp"
a90256cc
BP
240 "Major mode for editing Lisp code to run in Emacs.
241Commands:
242Delete converts tabs to spaces as it moves back.
243Blank lines separate paragraphs. Semicolons start comments.
244\\{emacs-lisp-mode-map}
245Entry to this mode calls the value of `emacs-lisp-mode-hook'
246if that value is non-nil."
a90256cc 247 (lisp-mode-variables nil)
1594a23a 248 (setq imenu-case-fold-search nil))
a90256cc 249
535eadac
DL
250(defvar lisp-mode-map
251 (let ((map (make-sparse-keymap)))
99ec65b3 252 (set-keymap-parent map lisp-mode-shared-map)
535eadac
DL
253 (define-key map "\e\C-x" 'lisp-eval-defun)
254 (define-key map "\C-c\C-z" 'run-lisp)
255 map)
a90256cc 256 "Keymap for ordinary Lisp mode.
99ec65b3 257All commands in `lisp-mode-shared-map' are inherited by this map.")
a90256cc 258
1594a23a 259(define-derived-mode lisp-mode nil "Lisp"
a90256cc
BP
260 "Major mode for editing Lisp code for Lisps other than GNU Emacs Lisp.
261Commands:
262Delete converts tabs to spaces as it moves back.
263Blank lines separate paragraphs. Semicolons start comments.
264\\{lisp-mode-map}
265Note that `run-lisp' may be used either to start an inferior Lisp job
266or to switch back to an existing one.
267
268Entry to this mode calls the value of `lisp-mode-hook'
269if that value is non-nil."
a90256cc 270 (lisp-mode-variables t)
01b91009 271 (setq font-lock-keywords-case-fold-search t)
1594a23a 272 (setq imenu-case-fold-search t))
a90256cc 273
5e871da0
DL
274;; This will do unless inf-lisp.el is loaded.
275(defun lisp-eval-defun (&optional and-go)
a90256cc
BP
276 "Send the current defun to the Lisp process made by \\[run-lisp]."
277 (interactive)
278 (error "Process lisp does not exist"))
279
535eadac
DL
280(defvar lisp-interaction-mode-map
281 (let ((map (make-sparse-keymap)))
99ec65b3 282 (set-keymap-parent map lisp-mode-shared-map)
535eadac
DL
283 (define-key map "\e\C-x" 'eval-defun)
284 (define-key map "\e\t" 'lisp-complete-symbol)
285 (define-key map "\n" 'eval-print-last-sexp)
286 map)
bacd83a7 287 "Keymap for Lisp Interaction mode.
99ec65b3 288All commands in `lisp-mode-shared-map' are inherited by this map.")
a90256cc 289
1594a23a 290(define-derived-mode lisp-interaction-mode emacs-lisp-mode "Lisp Interaction"
a90256cc
BP
291 "Major mode for typing and evaluating Lisp forms.
292Like Lisp mode except that \\[eval-print-last-sexp] evals the Lisp expression
293before point, and prints its value into the buffer, advancing point.
294
295Commands:
296Delete converts tabs to spaces as it moves back.
297Paragraphs are separated only by blank lines.
298Semicolons start comments.
299\\{lisp-interaction-mode-map}
300Entry to this mode calls the value of `lisp-interaction-mode-hook'
1594a23a 301if that value is non-nil.")
a90256cc 302
121f0d57 303(defun eval-print-last-sexp ()
a90256cc 304 "Evaluate sexp before point; print value into current buffer."
121f0d57 305 (interactive)
f798d950
JB
306 (let ((standard-output (current-buffer)))
307 (terpri)
308 (eval-last-sexp t)
309 (terpri)))
ca2ddd8e 310
99c6d63b 311(defun eval-last-sexp-1 (eval-last-sexp-arg-internal)
a90256cc
BP
312 "Evaluate sexp before point; print value in minibuffer.
313With argument, print output into current buffer."
99c6d63b 314 (let ((standard-output (if eval-last-sexp-arg-internal (current-buffer) t)))
efb195f0
RS
315 (let ((value
316 (eval (let ((stab (syntax-table))
361721f2 317 (opoint (point))
05e94d32 318 ignore-quotes
361721f2
KH
319 expr)
320 (unwind-protect
19b2f8f1
RM
321 (save-excursion
322 (set-syntax-table emacs-lisp-mode-syntax-table)
05e94d32
RS
323 ;; If this sexp appears to be enclosed in `...'
324 ;; then ignore the surrounding quotes.
325 (setq ignore-quotes
326 (or (eq (following-char) ?\')
327 (eq (preceding-char) ?\')))
19b2f8f1 328 (forward-sexp -1)
19b014e4
RS
329 ;; If we were after `?\e' (or similar case),
330 ;; use the whole thing, not just the `e'.
331 (when (eq (preceding-char) ?\\)
332 (forward-char -1)
333 (when (eq (preceding-char) ??)
334 (forward-char -1)))
ca2ddd8e 335
07ca56eb
GM
336 ;; Skip over `#N='s.
337 (when (eq (preceding-char) ?=)
338 (let (labeled-p)
339 (save-excursion
340 (skip-chars-backward "0-9#=")
341 (setq labeled-p (looking-at "\\(#[0-9]+=\\)+")))
342 (when labeled-p
343 (forward-sexp -1))))
344
cfe158ab 345 (save-restriction
05e94d32
RS
346 ;; vladimir@cs.ualberta.ca 30-Jul-1997: skip ` in
347 ;; `variable' so that the value is returned, not the
ca2ddd8e 348 ;; name
05e94d32
RS
349 (if (and ignore-quotes
350 (eq (following-char) ?`))
351 (forward-char))
cfe158ab 352 (narrow-to-region (point-min) opoint)
361721f2
KH
353 (setq expr (read (current-buffer)))
354 ;; If it's an (interactive ...) form, it's more
355 ;; useful to show how an interactive call would
356 ;; use it.
357 (and (consp expr)
358 (eq (car expr) 'interactive)
359 (setq expr
360 (list 'call-interactively
361 (list 'quote
362 (list 'lambda
363 '(&rest args)
364 expr
365 'args)))))
366 expr))
efb195f0
RS
367 (set-syntax-table stab))))))
368 (let ((print-length eval-expression-print-length)
369 (print-level eval-expression-print-level))
2e0a943f 370 (prin1 value)))))
a90256cc 371
99c6d63b
GM
372(defun eval-last-sexp (eval-last-sexp-arg-internal)
373 "Evaluate sexp before point; print value in minibuffer.
374With argument, print output into current buffer."
375 (interactive "P")
376 (if (null eval-expression-debug-on-error)
377 (eval-last-sexp-1 eval-last-sexp-arg-internal)
378 (let ((old-value (make-symbol "t")) new-value value)
379 (let ((debug-on-error old-value))
380 (setq value (eval-last-sexp-1 eval-last-sexp-arg-internal))
381 (setq new-value debug-on-error))
382 (unless (eq old-value new-value)
383 (setq debug-on-error new-value))
384 value)))
ca2ddd8e 385
151a410a 386(defun eval-defun-1 (form)
99ec65b3
DL
387 "Change defvar into defconst within FORM.
388Likewise for other constructs as necessary."
389 ;; The code in edebug-defun should be consistent with this, but not
390 ;; the same, since this gets a macroexpended form.
151a410a
RS
391 (cond ((and (eq (car form) 'defvar)
392 (cdr-safe (cdr-safe form)))
393 ;; Force variable to be bound.
394 (cons 'defconst (cdr form)))
5e871da0
DL
395 ;; `defcustom' is now macroexpanded to
396 ;; `custom-declare-variable' with a quoted value arg.
535eadac
DL
397 ((and (eq (car form) 'custom-declare-variable)
398 (default-boundp (eval (nth 1 form))))
151a410a 399 ;; Force variable to be bound.
5e871da0 400 (set-default (eval (nth 1 form)) (eval (nth 1 (nth 2 form))))
151a410a
RS
401 form)
402 ((eq (car form) 'progn)
403 (cons 'progn (mapcar 'eval-defun-1 (cdr form))))
404 (t form)))
405
105d6be1 406(defun eval-defun-2 ()
121f0d57 407 "Evaluate defun that point is in or before.
ebc03d28
KH
408The value is displayed in the minibuffer.
409If the current defun is actually a call to `defvar',
410then reset the variable using the initial value expression
411even if the variable already has some other value.
412\(Normally `defvar' does not change the variable's value
413if it already has a value.\)
414
2298f9f7
KH
415With argument, insert value in current buffer after the defun.
416Return the result of evaluation."
a90256cc 417 (interactive "P")
efb195f0
RS
418 (let ((debug-on-error eval-expression-debug-on-error)
419 (print-length eval-expression-print-length)
420 (print-level eval-expression-print-level))
421 (save-excursion
422 ;; Arrange for eval-region to "read" the (possibly) altered form.
423 ;; eval-region handles recording which file defines a function or
424 ;; variable. Re-written using `apply' to avoid capturing
425 ;; variables like `end'.
426 (apply
ca2ddd8e 427 #'eval-region
105d6be1 428 (let ((standard-output t)
efb195f0
RS
429 beg end form)
430 ;; Read the form from the buffer, and record where it ends.
431 (save-excursion
432 (end-of-defun)
433 (beginning-of-defun)
434 (setq beg (point))
435 (setq form (read (current-buffer)))
436 (setq end (point)))
437 ;; Alter the form if necessary, changing defvar into defconst, etc.
438 (setq form (eval-defun-1 (macroexpand form)))
439 (list beg end standard-output
440 `(lambda (ignore)
441 ;; Skipping to the end of the specified region
442 ;; will make eval-region return.
443 (goto-char ,end)
444 ',form))))))
713c3fb1
DL
445 ;; The result of evaluation has been put onto VALUES. So return it.
446 (car values))
99c6d63b 447
105d6be1
GM
448(defun eval-defun (edebug-it)
449 "Evaluate the top-level form containing point, or after point.
99c6d63b 450
105d6be1
GM
451If the current defun is actually a call to `defvar', then reset the
452variable using its initial value expression even if the variable
453already has some other value. (Normally `defvar' does not change the
454variable's value if it already has a value.)
455
456With a prefix argument, instrument the code for Edebug.
457
458If acting on a `defun' for FUNCTION, and the function was
459instrumented, `Edebug: FUNCTION' is printed in the minibuffer. If not
460instrumented, just FUNCTION is printed.
461
462If not acting on a `defun', the result of evaluation is displayed in
463the minibuffer."
99c6d63b 464 (interactive "P")
105d6be1
GM
465 (cond (edebug-it
466 (require 'edebug)
467 (eval-defun (not edebug-all-defs)))
468 (t
469 (if (null eval-expression-debug-on-error)
470 (eval-defun-2)
471 (let ((old-value (make-symbol "t")) new-value value)
472 (let ((debug-on-error old-value))
473 (setq value (eval-defun-2))
474 (setq new-value debug-on-error))
475 (unless (eq old-value new-value)
476 (setq debug-on-error new-value))
477 value)))))
99c6d63b 478
ca2ddd8e 479
a90256cc
BP
480(defun lisp-comment-indent ()
481 (if (looking-at "\\s<\\s<\\s<")
482 (current-column)
483 (if (looking-at "\\s<\\s<")
531cbff1 484 (let ((tem (or (calculate-lisp-indent) (current-column))))
a90256cc
BP
485 (if (listp tem) (car tem) tem))
486 (skip-chars-backward " \t")
487 (max (if (bolp) 0 (1+ (current-column)))
488 comment-column))))
489
7eb67d79
RS
490(defun lisp-mode-auto-fill ()
491 (if (> (current-column) (current-fill-column))
492 (if (save-excursion
493 (nth 4 (parse-partial-sexp (save-excursion
494 (beginning-of-defun)
495 (point))
496 (point))))
497 (do-auto-fill)
498 (let ((comment-start nil) (comment-start-skip nil))
499 (do-auto-fill)))))
500
535eadac
DL
501(defvar lisp-indent-offset nil)
502(defvar lisp-indent-function 'lisp-indent-function)
a90256cc
BP
503
504(defun lisp-indent-line (&optional whole-exp)
505 "Indent current line as Lisp code.
506With argument, indent any additional lines of the same expression
507rigidly along with this one."
508 (interactive "P")
509 (let ((indent (calculate-lisp-indent)) shift-amt beg end
510 (pos (- (point-max) (point))))
511 (beginning-of-line)
512 (setq beg (point))
513 (skip-chars-forward " \t")
531cbff1
RS
514 (if (or (null indent) (looking-at "\\s<\\s<\\s<"))
515 ;; Don't alter indentation of a ;;; comment line
516 ;; or a line that starts in a string.
328561fc 517 (goto-char (- (point-max) pos))
a90256cc
BP
518 (if (and (looking-at "\\s<") (not (looking-at "\\s<\\s<")))
519 ;; Single-semicolon comment lines should be indented
520 ;; as comment lines, not as code.
521 (progn (indent-for-comment) (forward-char -1))
522 (if (listp indent) (setq indent (car indent)))
523 (setq shift-amt (- indent (current-column)))
524 (if (zerop shift-amt)
525 nil
526 (delete-region beg (point))
527 (indent-to indent)))
528 ;; If initial point was within line's indentation,
529 ;; position after the indentation. Else stay at same point in text.
530 (if (> (- (point-max) pos) (point))
531 (goto-char (- (point-max) pos)))
532 ;; If desired, shift remaining lines of expression the same amount.
533 (and whole-exp (not (zerop shift-amt))
534 (save-excursion
535 (goto-char beg)
536 (forward-sexp 1)
537 (setq end (point))
538 (goto-char beg)
539 (forward-line 1)
540 (setq beg (point))
541 (> end beg))
542 (indent-code-rigidly beg end shift-amt)))))
543
22486a7f 544(defvar calculate-lisp-indent-last-sexp)
c0df1d61 545
a90256cc
BP
546(defun calculate-lisp-indent (&optional parse-start)
547 "Return appropriate indentation for current line as Lisp code.
548In usual case returns an integer: the column to indent to.
531cbff1
RS
549If the value is nil, that means don't change the indentation
550because the line starts inside a string.
551
552The value can also be a list of the form (COLUMN CONTAINING-SEXP-START).
a90256cc 553This means that following lines at the same level of indentation
531cbff1
RS
554should not necessarily be indented the same as this line.
555Then COLUMN is the column to indent to, and CONTAINING-SEXP-START
556is the buffer position of the start of the containing expression."
a90256cc
BP
557 (save-excursion
558 (beginning-of-line)
559 (let ((indent-point (point))
560 state paren-depth
561 ;; setting this to a number inhibits calling hook
562 (desired-indent nil)
563 (retry t)
c0df1d61 564 calculate-lisp-indent-last-sexp containing-sexp)
a90256cc
BP
565 (if parse-start
566 (goto-char parse-start)
567 (beginning-of-defun))
568 ;; Find outermost containing sexp
569 (while (< (point) indent-point)
570 (setq state (parse-partial-sexp (point) indent-point 0)))
571 ;; Find innermost containing sexp
572 (while (and retry
573 state
574 (> (setq paren-depth (elt state 0)) 0))
575 (setq retry nil)
c0df1d61 576 (setq calculate-lisp-indent-last-sexp (elt state 2))
a90256cc
BP
577 (setq containing-sexp (elt state 1))
578 ;; Position following last unclosed open.
579 (goto-char (1+ containing-sexp))
580 ;; Is there a complete sexp since then?
c0df1d61
RS
581 (if (and calculate-lisp-indent-last-sexp
582 (> calculate-lisp-indent-last-sexp (point)))
a90256cc 583 ;; Yes, but is there a containing sexp after that?
c0df1d61
RS
584 (let ((peek (parse-partial-sexp calculate-lisp-indent-last-sexp
585 indent-point 0)))
a90256cc
BP
586 (if (setq retry (car (cdr peek))) (setq state peek)))))
587 (if retry
588 nil
589 ;; Innermost containing sexp found
590 (goto-char (1+ containing-sexp))
c0df1d61 591 (if (not calculate-lisp-indent-last-sexp)
a90256cc
BP
592 ;; indent-point immediately follows open paren.
593 ;; Don't call hook.
594 (setq desired-indent (current-column))
595 ;; Find the start of first element of containing sexp.
c0df1d61 596 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
a90256cc
BP
597 (cond ((looking-at "\\s(")
598 ;; First element of containing sexp is a list.
599 ;; Indent under that list.
600 )
601 ((> (save-excursion (forward-line 1) (point))
c0df1d61 602 calculate-lisp-indent-last-sexp)
a90256cc
BP
603 ;; This is the first line to start within the containing sexp.
604 ;; It's almost certainly a function call.
c0df1d61 605 (if (= (point) calculate-lisp-indent-last-sexp)
a90256cc
BP
606 ;; Containing sexp has nothing before this line
607 ;; except the first element. Indent under that element.
608 nil
609 ;; Skip the first element, find start of second (the first
610 ;; argument of the function call) and indent under.
611 (progn (forward-sexp 1)
c0df1d61
RS
612 (parse-partial-sexp (point)
613 calculate-lisp-indent-last-sexp
614 0 t)))
a90256cc
BP
615 (backward-prefix-chars))
616 (t
c0df1d61 617 ;; Indent beneath first sexp on same line as
535eadac 618 ;; `calculate-lisp-indent-last-sexp'. Again, it's
c0df1d61
RS
619 ;; almost certainly a function call.
620 (goto-char calculate-lisp-indent-last-sexp)
a90256cc 621 (beginning-of-line)
c0df1d61
RS
622 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp
623 0 t)
a90256cc
BP
624 (backward-prefix-chars)))))
625 ;; Point is at the point to indent under unless we are inside a string.
eb8c3be9 626 ;; Call indentation hook except when overridden by lisp-indent-offset
a90256cc
BP
627 ;; or if the desired indentation has already been computed.
628 (let ((normal-indent (current-column)))
629 (cond ((elt state 3)
630 ;; Inside a string, don't change indentation.
531cbff1 631 nil)
a90256cc
BP
632 ((and (integerp lisp-indent-offset) containing-sexp)
633 ;; Indent by constant offset
634 (goto-char containing-sexp)
635 (+ (current-column) lisp-indent-offset))
636 (desired-indent)
637 ((and (boundp 'lisp-indent-function)
638 lisp-indent-function
639 (not retry))
640 (or (funcall lisp-indent-function indent-point state)
641 normal-indent))
642 (t
f30ff39f 643 normal-indent))))))
a90256cc
BP
644
645(defun lisp-indent-function (indent-point state)
646 (let ((normal-indent (current-column)))
647 (goto-char (1+ (elt state 1)))
c0df1d61 648 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
a90256cc
BP
649 (if (and (elt state 2)
650 (not (looking-at "\\sw\\|\\s_")))
651 ;; car of form doesn't seem to be a a symbol
652 (progn
653 (if (not (> (save-excursion (forward-line 1) (point))
c0df1d61
RS
654 calculate-lisp-indent-last-sexp))
655 (progn (goto-char calculate-lisp-indent-last-sexp)
a90256cc 656 (beginning-of-line)
c0df1d61
RS
657 (parse-partial-sexp (point)
658 calculate-lisp-indent-last-sexp 0 t)))
659 ;; Indent under the list or under the first sexp on the same
660 ;; line as calculate-lisp-indent-last-sexp. Note that first
661 ;; thing on that line has to be complete sexp since we are
662 ;; inside the innermost containing sexp.
a90256cc
BP
663 (backward-prefix-chars)
664 (current-column))
665 (let ((function (buffer-substring (point)
666 (progn (forward-sexp 1) (point))))
667 method)
2dab7802
RS
668 (setq method (or (get (intern-soft function) 'lisp-indent-function)
669 (get (intern-soft function) 'lisp-indent-hook)))
a90256cc
BP
670 (cond ((or (eq method 'defun)
671 (and (null method)
672 (> (length function) 3)
673 (string-match "\\`def" function)))
674 (lisp-indent-defform state indent-point))
675 ((integerp method)
676 (lisp-indent-specform method state
677 indent-point normal-indent))
678 (method
679 (funcall method state indent-point)))))))
680
d7fa5aa2 681(defvar lisp-body-indent 2
ab69b2fb 682 "Number of columns to indent the second line of a `(def...)' form.")
a90256cc
BP
683
684(defun lisp-indent-specform (count state indent-point normal-indent)
685 (let ((containing-form-start (elt state 1))
686 (i count)
687 body-indent containing-form-column)
688 ;; Move to the start of containing form, calculate indentation
689 ;; to use for non-distinguished forms (> count), and move past the
690 ;; function symbol. lisp-indent-function guarantees that there is at
691 ;; least one word or symbol character following open paren of containing
692 ;; form.
693 (goto-char containing-form-start)
694 (setq containing-form-column (current-column))
695 (setq body-indent (+ lisp-body-indent containing-form-column))
696 (forward-char 1)
697 (forward-sexp 1)
698 ;; Now find the start of the last form.
699 (parse-partial-sexp (point) indent-point 1 t)
700 (while (and (< (point) indent-point)
701 (condition-case ()
702 (progn
703 (setq count (1- count))
704 (forward-sexp 1)
705 (parse-partial-sexp (point) indent-point 1 t))
706 (error nil))))
707 ;; Point is sitting on first character of last (or count) sexp.
708 (if (> count 0)
709 ;; A distinguished form. If it is the first or second form use double
710 ;; lisp-body-indent, else normal indent. With lisp-body-indent bound
711 ;; to 2 (the default), this just happens to work the same with if as
712 ;; the older code, but it makes unwind-protect, condition-case,
713 ;; with-output-to-temp-buffer, et. al. much more tasteful. The older,
714 ;; less hacked, behavior can be obtained by replacing below with
715 ;; (list normal-indent containing-form-start).
716 (if (<= (- i count) 1)
717 (list (+ containing-form-column (* 2 lisp-body-indent))
718 containing-form-start)
719 (list normal-indent containing-form-start))
720 ;; A non-distinguished form. Use body-indent if there are no
721 ;; distinguished forms and this is the first undistinguished form,
722 ;; or if this is the first undistinguished form and the preceding
723 ;; distinguished form has indentation at least as great as body-indent.
724 (if (or (and (= i 0) (= count 0))
725 (and (= count 0) (<= body-indent normal-indent)))
726 body-indent
727 normal-indent))))
728
729(defun lisp-indent-defform (state indent-point)
730 (goto-char (car (cdr state)))
731 (forward-line 1)
732 (if (> (point) (car (cdr (cdr state))))
733 (progn
734 (goto-char (car (cdr state)))
735 (+ lisp-body-indent (current-column)))))
736
ca2ddd8e 737
a90256cc
BP
738;; (put 'progn 'lisp-indent-function 0), say, causes progn to be indented
739;; like defun if the first form is placed on the next line, otherwise
740;; it is indented like any other form (i.e. forms line up under first).
741
742(put 'lambda 'lisp-indent-function 'defun)
743(put 'autoload 'lisp-indent-function 'defun)
744(put 'progn 'lisp-indent-function 0)
745(put 'prog1 'lisp-indent-function 1)
746(put 'prog2 'lisp-indent-function 2)
747(put 'save-excursion 'lisp-indent-function 0)
748(put 'save-window-excursion 'lisp-indent-function 0)
4b619eca 749(put 'save-selected-window 'lisp-indent-function 0)
a90256cc 750(put 'save-restriction 'lisp-indent-function 0)
cfe158ab 751(put 'save-match-data 'lisp-indent-function 0)
38f16fe1 752(put 'save-current-buffer 'lisp-indent-function 0)
54c014f0 753(put 'with-current-buffer 'lisp-indent-function 1)
488a0b05 754(put 'combine-after-change-calls 'lisp-indent-function 0)
38f16fe1 755(put 'with-output-to-string 'lisp-indent-function 0)
08adb099 756(put 'with-temp-file 'lisp-indent-function 1)
e0119683 757(put 'with-temp-buffer 'lisp-indent-function 0)
bacd83a7 758(put 'with-temp-message 'lisp-indent-function 1)
83c8f461 759(put 'with-syntax-table 'lisp-indent-function 1)
a90256cc
BP
760(put 'let 'lisp-indent-function 1)
761(put 'let* 'lisp-indent-function 1)
762(put 'while 'lisp-indent-function 1)
763(put 'if 'lisp-indent-function 2)
764(put 'catch 'lisp-indent-function 1)
765(put 'condition-case 'lisp-indent-function 2)
766(put 'unwind-protect 'lisp-indent-function 1)
767(put 'with-output-to-temp-buffer 'lisp-indent-function 1)
b6b4c8bd 768(put 'eval-after-load 'lisp-indent-function 1)
05c71036
DL
769(put 'dolist 'lisp-indent-function 1)
770(put 'dotimes 'lisp-indent-function 1)
771(put 'when 'lisp-indent-function 1)
772(put 'unless 'lisp-indent-function 1)
a90256cc
BP
773
774(defun indent-sexp (&optional endpos)
775 "Indent each line of the list starting just after point.
776If optional arg ENDPOS is given, indent each line, stopping when
777ENDPOS is encountered."
778 (interactive)
daa37602
JB
779 (let ((indent-stack (list nil))
780 (next-depth 0)
33f268ec
RS
781 ;; If ENDPOS is non-nil, use nil as STARTING-POINT
782 ;; so that calculate-lisp-indent will find the beginning of
783 ;; the defun we are in.
784 ;; If ENDPOS is nil, it is safe not to scan before point
785 ;; since every line we indent is more deeply nested than point is.
786 (starting-point (if endpos nil (point)))
daa37602
JB
787 (last-point (point))
788 last-depth bol outer-loop-done inner-loop-done state this-indent)
33f268ec
RS
789 (or endpos
790 ;; Get error now if we don't have a complete sexp after point.
791 (save-excursion (forward-sexp 1)))
a90256cc
BP
792 (save-excursion
793 (setq outer-loop-done nil)
794 (while (if endpos (< (point) endpos)
795 (not outer-loop-done))
796 (setq last-depth next-depth
797 inner-loop-done nil)
798 ;; Parse this line so we can learn the state
799 ;; to indent the next line.
800 ;; This inner loop goes through only once
801 ;; unless a line ends inside a string.
802 (while (and (not inner-loop-done)
803 (not (setq outer-loop-done (eobp))))
804 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
805 nil nil state))
806 (setq next-depth (car state))
807 ;; If the line contains a comment other than the sort
808 ;; that is indented like code,
809 ;; indent it now with indent-for-comment.
810 ;; Comments indented like code are right already.
811 ;; In any case clear the in-comment flag in the state
812 ;; because parse-partial-sexp never sees the newlines.
813 (if (car (nthcdr 4 state))
814 (progn (indent-for-comment)
815 (end-of-line)
816 (setcar (nthcdr 4 state) nil)))
817 ;; If this line ends inside a string,
818 ;; go straight to next line, remaining within the inner loop,
819 ;; and turn off the \-flag.
820 (if (car (nthcdr 3 state))
821 (progn
822 (forward-line 1)
823 (setcar (nthcdr 5 state) nil))
824 (setq inner-loop-done t)))
825 (and endpos
daa37602
JB
826 (<= next-depth 0)
827 (progn
99ec65b3
DL
828 (setq indent-stack (nconc indent-stack
829 (make-list (- next-depth) nil))
daa37602
JB
830 last-depth (- last-depth next-depth)
831 next-depth 0)))
33f268ec 832 (or outer-loop-done endpos
a90256cc
BP
833 (setq outer-loop-done (<= next-depth 0)))
834 (if outer-loop-done
1f5038b5 835 (forward-line 1)
a90256cc
BP
836 (while (> last-depth next-depth)
837 (setq indent-stack (cdr indent-stack)
838 last-depth (1- last-depth)))
839 (while (< last-depth next-depth)
840 (setq indent-stack (cons nil indent-stack)
841 last-depth (1+ last-depth)))
842 ;; Now go to the next line and indent it according
843 ;; to what we learned from parsing the previous one.
844 (forward-line 1)
845 (setq bol (point))
846 (skip-chars-forward " \t")
847 ;; But not if the line is blank, or just a comment
848 ;; (except for double-semi comments; indent them as usual).
849 (if (or (eobp) (looking-at "\\s<\\|\n"))
850 nil
851 (if (and (car indent-stack)
852 (>= (car indent-stack) 0))
853 (setq this-indent (car indent-stack))
854 (let ((val (calculate-lisp-indent
855 (if (car indent-stack) (- (car indent-stack))
daa37602 856 starting-point))))
531cbff1
RS
857 (if (null val)
858 (setq this-indent val)
859 (if (integerp val)
860 (setcar indent-stack
861 (setq this-indent val))
862 (setcar indent-stack (- (car (cdr val))))
863 (setq this-indent (car val))))))
864 (if (and this-indent (/= (current-column) this-indent))
a90256cc
BP
865 (progn (delete-region bol (point))
866 (indent-to this-indent)))))
867 (or outer-loop-done
868 (setq outer-loop-done (= (point) last-point))
869 (setq last-point (point)))))))
870
a90256cc 871(defun lisp-indent-region (start end)
535eadac 872 "Indent every line whose first char is between START and END inclusive."
a90256cc 873 (save-excursion
a90256cc 874 (let ((endmark (copy-marker end)))
33f268ec
RS
875 (goto-char start)
876 (and (bolp) (not (eolp))
877 (lisp-indent-line))
a90256cc
BP
878 (indent-sexp endmark)
879 (set-marker endmark nil))))
ca2ddd8e 880
6338c7ba
JB
881;;;; Lisp paragraph filling commands.
882
883(defun lisp-fill-paragraph (&optional justify)
884 "Like \\[fill-paragraph], but handle Emacs Lisp comments.
885If any of the current line is a comment, fill the comment or the
886paragraph of it that point is in, preserving the comment's indentation
887and initial semicolons."
888 (interactive "P")
889 (let (
890 ;; Non-nil if the current line contains a comment.
891 has-comment
892
4d2790ad
RS
893 ;; Non-nil if the current line contains code and a comment.
894 has-code-and-comment
895
6338c7ba
JB
896 ;; If has-comment, the appropriate fill-prefix for the comment.
897 comment-fill-prefix
898 )
899
900 ;; Figure out what kind of comment we are looking at.
901 (save-excursion
902 (beginning-of-line)
903 (cond
904
905 ;; A line with nothing but a comment on it?
906 ((looking-at "[ \t]*;[; \t]*")
907 (setq has-comment t
908 comment-fill-prefix (buffer-substring (match-beginning 0)
909 (match-end 0))))
910
911 ;; A line with some code, followed by a comment? Remember that the
912 ;; semi which starts the comment shouldn't be part of a string or
913 ;; character.
93353fea
KH
914 ((condition-case nil
915 (save-restriction
916 (narrow-to-region (point-min)
917 (save-excursion (end-of-line) (point)))
918 (while (not (looking-at ";\\|$"))
919 (skip-chars-forward "^;\n\"\\\\?")
920 (cond
921 ((eq (char-after (point)) ?\\) (forward-char 2))
922 ((memq (char-after (point)) '(?\" ??)) (forward-sexp 1))))
923 (looking-at ";+[\t ]*"))
924 (error nil))
4d2790ad 925 (setq has-comment t has-code-and-comment t)
6338c7ba 926 (setq comment-fill-prefix
93353fea
KH
927 (concat (make-string (/ (current-column) 8) ?\t)
928 (make-string (% (current-column) 8) ?\ )
6338c7ba
JB
929 (buffer-substring (match-beginning 0) (match-end 0)))))))
930
931 (if (not has-comment)
fe3188ec
DL
932 ;; `paragraph-start' is set here (not in the buffer-local
933 ;; variable so that `forward-paragraph' et al work as
934 ;; expected) so that filling (doc) strings works sensibly.
935 ;; Adding the opening paren to avoid the following sexp being
936 ;; filled means that sexps generally aren't filled as normal
937 ;; text, which is probably sensible. The `;' and `:' stop the
938 ;; filled para at following comment lines and keywords
939 ;; (typically in `defcustom').
940 (let ((paragraph-start (concat paragraph-start
941 "\\|\\s-*[\(;:\"]")))
942 (fill-paragraph justify))
6338c7ba
JB
943
944 ;; Narrow to include only the comment, and then fill the region.
4c06fbee
KH
945 (save-excursion
946 (save-restriction
947 (beginning-of-line)
948 (narrow-to-region
949 ;; Find the first line we should include in the region to fill.
950 (save-excursion
951 (while (and (zerop (forward-line -1))
6338c7ba 952 (looking-at "^[ \t]*;")))
4c06fbee
KH
953 ;; We may have gone too far. Go forward again.
954 (or (looking-at ".*;")
955 (forward-line 1))
956 (point))
957 ;; Find the beginning of the first line past the region to fill.
958 (save-excursion
959 (while (progn (forward-line 1)
960 (looking-at "^[ \t]*;")))
961 (point)))
962
963 ;; Lines with only semicolons on them can be paragraph boundaries.
964 (let* ((paragraph-start (concat paragraph-start "\\|[ \t;]*$"))
965 (paragraph-separate (concat paragraph-start "\\|[ \t;]*$"))
966 (paragraph-ignore-fill-prefix nil)
967 (fill-prefix comment-fill-prefix)
4d2790ad
RS
968 (after-line (if has-code-and-comment
969 (save-excursion
970 (forward-line 1) (point))))
4c06fbee
KH
971 (end (progn
972 (forward-paragraph)
973 (or (bolp) (newline 1))
974 (point)))
4d2790ad
RS
975 ;; If this comment starts on a line with code,
976 ;; include that like in the filling.
977 (beg (progn (backward-paragraph)
978 (if (eq (point) after-line)
979 (forward-line -1))
980 (point))))
4c06fbee
KH
981 (fill-region-as-paragraph beg end
982 justify nil
983 (save-excursion
984 (goto-char beg)
985 (if (looking-at fill-prefix)
986 nil
987 (re-search-forward comment-start-skip)
988 (point))))))))
2b4483bb 989 t))
ca2ddd8e 990
a90256cc
BP
991(defun indent-code-rigidly (start end arg &optional nochange-regexp)
992 "Indent all lines of code, starting in the region, sideways by ARG columns.
993Does not affect lines starting inside comments or strings, assuming that
994the start of the region is not inside them.
995
996Called from a program, takes args START, END, COLUMNS and NOCHANGE-REGEXP.
997The last is a regexp which, if matched at the beginning of a line,
998means don't indent that line."
999 (interactive "r\np")
1000 (let (state)
1001 (save-excursion
1002 (goto-char end)
1003 (setq end (point-marker))
1004 (goto-char start)
1005 (or (bolp)
1006 (setq state (parse-partial-sexp (point)
1007 (progn
1008 (forward-line 1) (point))
1009 nil nil state)))
1010 (while (< (point) end)
1011 (or (car (nthcdr 3 state))
1012 (and nochange-regexp
1013 (looking-at nochange-regexp))
1014 ;; If line does not start in string, indent it
1015 (let ((indent (current-indentation)))
1016 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
1017 (or (eolp)
1018 (indent-to (max 0 (+ indent arg)) 0))))
1019 (setq state (parse-partial-sexp (point)
1020 (progn
1021 (forward-line 1) (point))
1022 nil nil state))))))
49116ac0
JB
1023
1024(provide 'lisp-mode)
1025
6594deb0 1026;;; lisp-mode.el ends here