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