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