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