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