1 ;;; lisp-mode.el --- Lisp mode, and its idiosyncratic commands.
3 ;; Copyright (C) 1985, 1986, 1999 Free Software Foundation, Inc.
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)
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.
27 ;; The base major mode for editing Lisp code (used also for Emacs Lisp).
28 ;; This mode is documented in the Emacs manual
32 (defvar lisp-mode-syntax-table nil
"")
33 (defvar emacs-lisp-mode-syntax-table nil
"")
34 (defvar lisp-mode-abbrev-table nil
"")
36 (if (not emacs-lisp-mode-syntax-table
)
38 (setq emacs-lisp-mode-syntax-table
(make-syntax-table))
40 (modify-syntax-entry i
"_ " emacs-lisp-mode-syntax-table
)
44 (modify-syntax-entry i
"_ " emacs-lisp-mode-syntax-table
)
48 (modify-syntax-entry i
"_ " emacs-lisp-mode-syntax-table
)
52 (modify-syntax-entry i
"_ " emacs-lisp-mode-syntax-table
)
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'.
75 (function (lambda (key val
)
77 (/= (char-syntax key
) ?w
)
78 (modify-syntax-entry key
"_ "
79 emacs-lisp-mode-syntax-table
))))
80 (standard-syntax-table))))
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
)))
89 (define-abbrev-table 'lisp-mode-abbrev-table
())
91 (defvar lisp-imenu-generic-expression
94 "^\\s-*(def\\(un\\|subst\\|macro\\|advice\\|ine-skeleton\\)\
95 \\s-+\\([-A-Za-z0-9+*|:/]+\\)" 2)
97 "^\\s-*(def\\(var\\|const\\|custom\\)\\s-+\\([-A-Za-z0-9+*|:/]+\\)" 2)
99 "^\\s-*(def\\(group\\|type\\|struct\\|class\\|ine-condition\
100 \\|ine-widget\\)\\s-+'?\\([-A-Za-z0-9+*|:/]+\\)"
103 "Imenu generic expression for Lisp mode. See `imenu-generic-expression'.")
105 (defun lisp-mode-variables (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
))
147 (defun lisp-outline-level ()
148 "Lisp mode `outline-level' function."
151 (looking-at outline-regexp
)
152 (- (match-end 0) (match-beginning 0))))
155 (defvar shared-lisp-mode-map
()
156 "Keymap for commands shared by all sorts of Lisp modes.")
158 (if shared-lisp-mode-map
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
))
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.")
168 (if emacs-lisp-mode-map
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
)))
198 (defun emacs-lisp-byte-compile ()
199 "Byte compile the file containing the current buffer."
202 (byte-compile-file buffer-file-name
)
203 (error "The buffer must be saved in a file first")))
205 (defun emacs-lisp-byte-compile-and-load ()
206 "Byte-compile the current file (if it has changed), then load compiled code."
209 (error "The buffer must be saved in a file first"))
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))))
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
))))
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
)
226 (defcustom lisp-mode-hook nil
227 "Hook run when entering Lisp mode."
228 :options
'(imenu-add-menubar-index)
232 (defcustom lisp-interaction-mode-hook nil
233 "Hook run when entering Lisp Interaction mode."
234 :options
'(turn-on-eldoc-mode)
238 (defun emacs-lisp-mode ()
239 "Major mode for editing Lisp code to run in Emacs.
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."
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
))
256 (defvar lisp-mode-map
()
257 "Keymap for ordinary Lisp mode.
258 All commands in `shared-lisp-mode-map' are inherited by this map.")
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
))
268 "Major mode for editing Lisp code for Lisps other than GNU Emacs Lisp.
270 Delete converts tabs to spaces as it moves back.
271 Blank lines separate paragraphs. Semicolons start comments.
273 Note that `run-lisp' may be used either to start an inferior Lisp job
274 or to switch back to an existing one.
276 Entry to this mode calls the value of `lisp-mode-hook'
277 if that value is non-nil."
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
))
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]."
292 (error "Process lisp does not exist"))
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.")
298 (if lisp-interaction-mode-map
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
))
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.
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."
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
))
327 (defun eval-print-last-sexp ()
328 "Evaluate sexp before point; print value into current buffer."
330 (let ((standard-output (current-buffer)))
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."
339 (let ((standard-output (if eval-last-sexp-arg-internal
(current-buffer) t
))
340 (debug-on-error eval-expression-debug-on-error
))
342 (eval (let ((stab (syntax-table))
348 (set-syntax-table emacs-lisp-mode-syntax-table
)
349 ;; If this sexp appears to be enclosed in `...'
350 ;; then ignore the surrounding quotes.
352 (or (eq (following-char) ?
\')
353 (eq (preceding-char) ?
\')))
355 ;; If we were after `?\e' (or similar case),
356 ;; use the whole thing, not just the `e'.
357 (when (eq (preceding-char) ?
\\)
359 (when (eq (preceding-char) ??
)
362 ;; vladimir@cs.ualberta.ca 30-Jul-1997: skip ` in
363 ;; `variable' so that the value is returned, not the
365 (if (and ignore-quotes
366 (eq (following-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
374 (eq (car expr
) 'interactive
)
376 (list 'call-interactively
383 (set-syntax-table stab
))))))
384 (let ((print-length eval-expression-print-length
)
385 (print-level eval-expression-print-level
))
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
)))
400 ((eq (car form
) 'progn
)
401 (cons 'progn
(mapcar 'eval-defun-1
(cdr form
))))
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.\)
413 With argument, insert value in current buffer after the defun.
414 Return the result of evaluation."
416 (let ((debug-on-error eval-expression-debug-on-error
)
417 (print-length eval-expression-print-length
)
418 (print-level eval-expression-print-level
))
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'.
426 (let ((standard-output (if eval-defun-arg-internal
(current-buffer) t
))
428 ;; Read the form from the buffer, and record where it ends.
433 (setq form
(read (current-buffer)))
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
439 ;; Skipping to the end of the specified region
440 ;; will make eval-region return.
443 ;; The result of evaluation has been put onto VALUES. So return it.
446 (defun lisp-comment-indent ()
447 (if (looking-at "\\s<\\s<\\s<")
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)))
456 (defun lisp-mode-auto-fill ()
457 (if (> (current-column) (current-fill-column))
459 (nth 4 (parse-partial-sexp (save-excursion
464 (let ((comment-start nil
) (comment-start-skip nil
))
467 (defvar lisp-indent-offset nil
"")
468 (defvar lisp-indent-function
'lisp-indent-function
"")
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."
475 (let ((indent (calculate-lisp-indent)) shift-amt beg end
476 (pos (- (point-max) (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
)
492 (delete-region beg
(point))
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
))
508 (indent-code-rigidly beg end shift-amt
)))))
510 (defvar calculate-lisp-indent-last-sexp
)
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.
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."
525 (let ((indent-point (point))
527 ;; setting this to a number inhibits calling hook
530 calculate-lisp-indent-last-sexp containing-sexp
)
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
540 (> (setq paren-depth
(elt state
0)) 0))
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
552 (if (setq retry
(car (cdr peek
))) (setq state peek
)))))
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.
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.
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.
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
581 (backward-prefix-chars))
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
)
588 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp
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)))
596 ;; Inside a string, don't change indentation.
598 ((and (integerp lisp-indent-offset
) containing-sexp
)
599 ;; Indent by constant offset
600 (goto-char containing-sexp
)
601 (+ (current-column) lisp-indent-offset
))
603 ((and (boundp 'lisp-indent-function
)
606 (or (funcall lisp-indent-function indent-point state
)
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
619 (if (not (> (save-excursion (forward-line 1) (point))
620 calculate-lisp-indent-last-sexp
))
621 (progn (goto-char calculate-lisp-indent-last-sexp
)
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)
631 (let ((function (buffer-substring (point)
632 (progn (forward-sexp 1) (point))))
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
)
638 (> (length function
) 3)
639 (string-match "\\`def" function
)))
640 (lisp-indent-defform state indent-point
))
642 (lisp-indent-specform method state
643 indent-point normal-indent
))
645 (funcall method state indent-point
)))))))
647 (defvar lisp-body-indent
2
648 "Number of columns to indent the second line of a `(def...)' form.")
650 (defun lisp-indent-specform (count state indent-point normal-indent
)
651 (let ((containing-form-start (elt state
1))
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
659 (goto-char containing-form-start
)
660 (setq containing-form-column
(current-column))
661 (setq body-indent
(+ lisp-body-indent containing-form-column
))
664 ;; Now find the start of the last form.
665 (parse-partial-sexp (point) indent-point
1 t
)
666 (while (and (< (point) indent-point
)
669 (setq count
(1- count
))
671 (parse-partial-sexp (point) indent-point
1 t
))
673 ;; Point is sitting on first character of last (or count) sexp.
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
)))
695 (defun lisp-indent-defform (state indent-point
)
696 (goto-char (car (cdr state
)))
698 (if (> (point) (car (cdr (cdr state
))))
700 (goto-char (car (cdr state
)))
701 (+ lisp-body-indent
(current-column)))))
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).
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)
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."
740 (let ((indent-stack (list nil
))
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)))
749 last-depth bol outer-loop-done inner-loop-done state this-indent
)
751 ;; Get error now if we don't have a complete sexp after point.
752 (save-excursion (forward-sexp 1)))
754 (setq outer-loop-done nil
)
755 (while (if endpos
(< (point) endpos
)
756 (not outer-loop-done
))
757 (setq last-depth next-depth
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))
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)
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
))
784 (setcar (nthcdr 5 state
) nil
))
785 (setq inner-loop-done t
)))
789 (setq indent-stack
(append indent-stack
790 (make-list (- next-depth
) nil
))
791 last-depth
(- last-depth next-depth
)
793 (or outer-loop-done endpos
794 (setq outer-loop-done
(<= next-depth
0)))
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.
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"))
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
))
819 (setq this-indent val
)
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
)))))
829 (setq outer-loop-done
(= (point) last-point
))
830 (setq last-point
(point)))))))
832 ;; Indent every line whose first char is between START and END inclusive.
833 (defun lisp-indent-region (start end
)
835 (let ((endmark (copy-marker end
)))
837 (and (bolp) (not (eolp))
839 (indent-sexp endmark
)
840 (set-marker endmark nil
))))
842 ;;;; Lisp paragraph filling commands.
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."
851 ;; Non-nil if the current line contains a comment.
854 ;; Non-nil if the current line contains code and a comment.
857 ;; If has-comment, the appropriate fill-prefix for the comment.
861 ;; Figure out what kind of comment we are looking at.
866 ;; A line with nothing but a comment on it?
867 ((looking-at "[ \t]*;[; \t]*")
869 comment-fill-prefix
(buffer-substring (match-beginning 0)
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
877 (narrow-to-region (point-min)
878 (save-excursion (end-of-line) (point)))
879 (while (not (looking-at ";\\|$"))
880 (skip-chars-forward "^;\n\"\\\\?")
882 ((eq (char-after (point)) ?
\\) (forward-char 2))
883 ((memq (char-after (point)) '(?
\" ??
)) (forward-sexp 1))))
884 (looking-at ";+[\t ]*"))
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)))))))
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
))
905 ;; Narrow to include only the comment, and then fill the region.
910 ;; Find the first line we should include in the region to fill.
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 ".*;")
918 ;; Find the beginning of the first line past the region to fill.
920 (while (progn (forward-line 1)
921 (looking-at "^[ \t]*;")))
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
931 (forward-line 1) (point))))
934 (or (bolp) (newline 1))
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
)
942 (fill-region-as-paragraph beg end
946 (if (looking-at fill-prefix
)
948 (re-search-forward comment-start-skip
)
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.
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."
964 (setq end
(point-marker))
967 (setq state
(parse-partial-sexp (point)
969 (forward-line 1) (point))
971 (while (< (point) end
)
972 (or (car (nthcdr 3 state
))
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)))
979 (indent-to (max 0 (+ indent arg
)) 0))))
980 (setq state
(parse-partial-sexp (point)
982 (forward-line 1) (point))
987 ;;; lisp-mode.el ends here