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