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