Update FSF's address.
[bpt/emacs.git] / lisp / emacs-lisp / lisp-mode.el
1 ;;; lisp-mode.el --- Lisp mode, and its idiosyncratic commands.
2
3 ;; Copyright (C) 1985 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-syntax-table nil "")
33 (defvar emacs-lisp-mode-syntax-table nil "")
34 (defvar lisp-mode-abbrev-table nil "")
35
36 (if (not emacs-lisp-mode-syntax-table)
37 (let ((i 0))
38 (setq emacs-lisp-mode-syntax-table (make-syntax-table))
39 (while (< i ?0)
40 (modify-syntax-entry i "_ " emacs-lisp-mode-syntax-table)
41 (setq i (1+ i)))
42 (setq i (1+ ?9))
43 (while (< i ?A)
44 (modify-syntax-entry i "_ " emacs-lisp-mode-syntax-table)
45 (setq i (1+ i)))
46 (setq i (1+ ?Z))
47 (while (< i ?a)
48 (modify-syntax-entry i "_ " emacs-lisp-mode-syntax-table)
49 (setq i (1+ i)))
50 (setq i (1+ ?z))
51 (while (< i 128)
52 (modify-syntax-entry i "_ " emacs-lisp-mode-syntax-table)
53 (setq i (1+ i)))
54 (modify-syntax-entry ? " " emacs-lisp-mode-syntax-table)
55 (modify-syntax-entry ?\t " " emacs-lisp-mode-syntax-table)
56 (modify-syntax-entry ?\n "> " emacs-lisp-mode-syntax-table)
57 ;; Give CR the same syntax as newline, for selective-display.
58 (modify-syntax-entry ?\^m "> " emacs-lisp-mode-syntax-table)
59 (modify-syntax-entry ?\; "< " 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 ;; Used to be singlequote; changed for flonums.
64 (modify-syntax-entry ?. "_ " emacs-lisp-mode-syntax-table)
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
73 (if (not lisp-mode-syntax-table)
74 (progn (setq lisp-mode-syntax-table
75 (copy-syntax-table emacs-lisp-mode-syntax-table))
76 (modify-syntax-entry ?\| "\" " lisp-mode-syntax-table)
77 (modify-syntax-entry ?\[ "_ " lisp-mode-syntax-table)
78 (modify-syntax-entry ?\] "_ " lisp-mode-syntax-table)))
79
80 (define-abbrev-table 'lisp-mode-abbrev-table ())
81
82 (defvar lisp-imenu-generic-expression
83 '(
84 (nil
85 "^\\s-*(def\\(un\\|subst\\|macro\\|advice\\)\\s-+\\([-A-Za-z0-9+]+\\)" 2)
86 ("Variables"
87 "^\\s-*(def\\(var\\|const\\)\\s-+\\([-A-Za-z0-9+]+\\)" 2)
88 ("Types"
89 "^\\s-*(def\\(type\\|struct\\|class\\|ine-condition\\)\\s-+\\([-A-Za-z0-9+]+\\)"
90 2))
91
92 "Imenu generic expression for Lisp mode. See `imenu-generic-expression'.")
93
94 (defun lisp-mode-variables (lisp-syntax)
95 (cond (lisp-syntax
96 (set-syntax-table lisp-mode-syntax-table)))
97 (setq local-abbrev-table lisp-mode-abbrev-table)
98 (make-local-variable 'paragraph-start)
99 (setq paragraph-start (concat page-delimiter "\\|$" ))
100 (make-local-variable 'paragraph-separate)
101 (setq paragraph-separate paragraph-start)
102 (make-local-variable 'paragraph-ignore-fill-prefix)
103 (setq paragraph-ignore-fill-prefix t)
104 (make-local-variable 'fill-paragraph-function)
105 (setq fill-paragraph-function 'lisp-fill-paragraph)
106 (make-local-variable 'indent-line-function)
107 (setq indent-line-function 'lisp-indent-line)
108 (make-local-variable 'indent-region-function)
109 (setq indent-region-function 'lisp-indent-region)
110 (make-local-variable 'parse-sexp-ignore-comments)
111 (setq parse-sexp-ignore-comments t)
112 (make-local-variable 'outline-regexp)
113 (setq outline-regexp ";;; \\|(....")
114 (make-local-variable 'comment-start)
115 (setq comment-start ";")
116 (make-local-variable 'comment-start-skip)
117 (setq comment-start-skip ";+ *")
118 (make-local-variable 'comment-column)
119 (setq comment-column 40)
120 (make-local-variable 'comment-indent-function)
121 (setq comment-indent-function 'lisp-comment-indent)
122 (make-local-variable 'imenu-generic-expression)
123 (setq imenu-generic-expression lisp-imenu-generic-expression))
124 \f
125 (defvar shared-lisp-mode-map ()
126 "Keymap for commands shared by all sorts of Lisp modes.")
127
128 (if shared-lisp-mode-map
129 ()
130 (setq shared-lisp-mode-map (make-sparse-keymap))
131 (define-key shared-lisp-mode-map "\e\C-q" 'indent-sexp)
132 (define-key shared-lisp-mode-map "\177" 'backward-delete-char-untabify))
133
134 (defvar emacs-lisp-mode-map ()
135 "Keymap for Emacs Lisp mode.
136 All commands in `shared-lisp-mode-map' are inherited by this map.")
137
138 (if emacs-lisp-mode-map
139 ()
140 (let ((map (make-sparse-keymap "Emacs-Lisp")))
141 (setq emacs-lisp-mode-map
142 (nconc (make-sparse-keymap) shared-lisp-mode-map))
143 (define-key emacs-lisp-mode-map "\e\t" 'lisp-complete-symbol)
144 (define-key emacs-lisp-mode-map "\e\C-x" 'eval-defun)
145 (define-key emacs-lisp-mode-map [menu-bar] (make-sparse-keymap))
146 (define-key emacs-lisp-mode-map [menu-bar emacs-lisp]
147 (cons "Emacs-Lisp" map))
148 (define-key map [edebug-defun]
149 '("Instrument Function for Debugging" . edebug-defun))
150 (define-key map [byte-recompile]
151 '("Byte-recompile Directory..." . byte-recompile-directory))
152 (define-key map [byte-compile]
153 '("Byte-compile And Load" . emacs-lisp-compile-and-load))
154 (define-key map [byte-compile]
155 '("Byte-compile This File" . emacs-lisp-byte-compile))
156 (define-key map [separator-eval] '("--"))
157 (define-key map [eval-buffer] '("Evaluate Buffer" . eval-current-buffer))
158 (define-key map [eval-region] '("Evaluate Region" . eval-region))
159 (define-key map [eval-sexp] '("Evaluate Last S-expression" . eval-last-sexp))
160 (define-key map [separator-format] '("--"))
161 (define-key map [comment-region] '("Comment Out Region" . comment-region))
162 (define-key map [indent-region] '("Indent Region" . indent-region))
163 (define-key map [indent-line] '("Indent Line" . lisp-indent-line))
164 (put 'eval-region 'menu-enable 'mark-active)
165 (put 'comment-region 'menu-enable 'mark-active)
166 (put 'indent-region 'menu-enable 'mark-active)))
167
168 (defun emacs-lisp-byte-compile ()
169 "Byte compile the file containing the current buffer."
170 (interactive)
171 (if buffer-file-name
172 (byte-compile-file buffer-file-name)
173 (error "The buffer must be saved in a file first")))
174
175 (defun emacs-lisp-compile-and-load ()
176 "Byte-compile the current file (if it has changed), then load compiled code."
177 (interactive)
178 (or buffer-file-name
179 (error "The buffer must be saved in a file first"))
180 (require 'bytecomp)
181 ;; Recompile if file or buffer has changed since last compilation.
182 (or (buffer-modified-p)
183 (file-newer-than-file-p (byte-compile-dest-file buffer-file-name)
184 buffer-file-name)
185 (byte-compile-file buffer-file-name))
186 (load-file (byte-compile-dest-file buffer-file-name)))
187
188 (defun emacs-lisp-mode ()
189 "Major mode for editing Lisp code to run in Emacs.
190 Commands:
191 Delete converts tabs to spaces as it moves back.
192 Blank lines separate paragraphs. Semicolons start comments.
193 \\{emacs-lisp-mode-map}
194 Entry to this mode calls the value of `emacs-lisp-mode-hook'
195 if that value is non-nil."
196 (interactive)
197 (kill-all-local-variables)
198 (use-local-map emacs-lisp-mode-map)
199 (set-syntax-table emacs-lisp-mode-syntax-table)
200 (setq major-mode 'emacs-lisp-mode)
201 (setq mode-name "Emacs-Lisp")
202 (lisp-mode-variables nil)
203 (run-hooks 'emacs-lisp-mode-hook))
204
205 (defvar lisp-mode-map ()
206 "Keymap for ordinary Lisp mode.
207 All commands in `shared-lisp-mode-map' are inherited by this map.")
208
209 (if lisp-mode-map
210 ()
211 (setq lisp-mode-map
212 (nconc (make-sparse-keymap) shared-lisp-mode-map))
213 (define-key lisp-mode-map "\e\C-x" 'lisp-eval-defun)
214 (define-key lisp-mode-map "\C-c\C-z" 'run-lisp))
215
216 (defun lisp-mode ()
217 "Major mode for editing Lisp code for Lisps other than GNU Emacs Lisp.
218 Commands:
219 Delete converts tabs to spaces as it moves back.
220 Blank lines separate paragraphs. Semicolons start comments.
221 \\{lisp-mode-map}
222 Note that `run-lisp' may be used either to start an inferior Lisp job
223 or to switch back to an existing one.
224
225 Entry to this mode calls the value of `lisp-mode-hook'
226 if that value is non-nil."
227 (interactive)
228 (kill-all-local-variables)
229 (use-local-map lisp-mode-map)
230 (setq major-mode 'lisp-mode)
231 (setq mode-name "Lisp")
232 (lisp-mode-variables t)
233 (set-syntax-table lisp-mode-syntax-table)
234 (run-hooks 'lisp-mode-hook))
235
236 ;; This will do unless shell.el is loaded.
237 (defun lisp-eval-defun nil
238 "Send the current defun to the Lisp process made by \\[run-lisp]."
239 (interactive)
240 (error "Process lisp does not exist"))
241
242 (defvar lisp-interaction-mode-map ()
243 "Keymap for Lisp Interaction moe.
244 All commands in `shared-lisp-mode-map' are inherited by this map.")
245
246 (if lisp-interaction-mode-map
247 ()
248 (setq lisp-interaction-mode-map
249 (nconc (make-sparse-keymap) shared-lisp-mode-map))
250 (define-key lisp-interaction-mode-map "\e\C-x" 'eval-defun)
251 (define-key lisp-interaction-mode-map "\e\t" 'lisp-complete-symbol)
252 (define-key lisp-interaction-mode-map "\n" 'eval-print-last-sexp))
253
254 (defun lisp-interaction-mode ()
255 "Major mode for typing and evaluating Lisp forms.
256 Like Lisp mode except that \\[eval-print-last-sexp] evals the Lisp expression
257 before point, and prints its value into the buffer, advancing point.
258
259 Commands:
260 Delete converts tabs to spaces as it moves back.
261 Paragraphs are separated only by blank lines.
262 Semicolons start comments.
263 \\{lisp-interaction-mode-map}
264 Entry to this mode calls the value of `lisp-interaction-mode-hook'
265 if that value is non-nil."
266 (interactive)
267 (kill-all-local-variables)
268 (use-local-map lisp-interaction-mode-map)
269 (setq major-mode 'lisp-interaction-mode)
270 (setq mode-name "Lisp Interaction")
271 (set-syntax-table emacs-lisp-mode-syntax-table)
272 (lisp-mode-variables nil)
273 (run-hooks 'lisp-interaction-mode-hook))
274
275 (defun eval-print-last-sexp ()
276 "Evaluate sexp before point; print value into current buffer."
277 (interactive)
278 (let ((standard-output (current-buffer)))
279 (terpri)
280 (eval-last-sexp t)
281 (terpri)))
282 \f
283 (defun eval-last-sexp (eval-last-sexp-arg-internal)
284 "Evaluate sexp before point; print value in minibuffer.
285 With argument, print output into current buffer."
286 (interactive "P")
287 (let ((standard-output (if eval-last-sexp-arg-internal (current-buffer) t))
288 (opoint (point)))
289 (prin1 (let ((stab (syntax-table)))
290 (eval (unwind-protect
291 (save-excursion
292 (set-syntax-table emacs-lisp-mode-syntax-table)
293 (forward-sexp -1)
294 (save-restriction
295 (narrow-to-region (point-min) opoint)
296 (read (current-buffer))))
297 (set-syntax-table stab)))))))
298
299 (defun eval-defun (eval-defun-arg-internal)
300 "Evaluate defun that point is in or before.
301 Print value in minibuffer.
302 With argument, insert value in current buffer after the defun."
303 (interactive "P")
304 (let ((standard-output (if eval-defun-arg-internal (current-buffer) t))
305 (form (save-excursion
306 (end-of-defun)
307 (beginning-of-defun)
308 (read (current-buffer)))))
309 (if (and (eq (car form) 'defvar)
310 (cdr-safe (cdr-safe form)))
311 (setq form (cons 'defconst (cdr form))))
312 (prin1 (eval form))))
313 \f
314 (defun lisp-comment-indent ()
315 (if (looking-at "\\s<\\s<\\s<")
316 (current-column)
317 (if (looking-at "\\s<\\s<")
318 (let ((tem (calculate-lisp-indent)))
319 (if (listp tem) (car tem) tem))
320 (skip-chars-backward " \t")
321 (max (if (bolp) 0 (1+ (current-column)))
322 comment-column))))
323
324 (defconst lisp-indent-offset nil "")
325 (defconst lisp-indent-function 'lisp-indent-function "")
326
327 (defun lisp-indent-line (&optional whole-exp)
328 "Indent current line as Lisp code.
329 With argument, indent any additional lines of the same expression
330 rigidly along with this one."
331 (interactive "P")
332 (let ((indent (calculate-lisp-indent)) shift-amt beg end
333 (pos (- (point-max) (point))))
334 (beginning-of-line)
335 (setq beg (point))
336 (skip-chars-forward " \t")
337 (if (looking-at "\\s<\\s<\\s<")
338 ;; Don't alter indentation of a ;;; comment line.
339 (goto-char (- (point-max) pos))
340 (if (and (looking-at "\\s<") (not (looking-at "\\s<\\s<")))
341 ;; Single-semicolon comment lines should be indented
342 ;; as comment lines, not as code.
343 (progn (indent-for-comment) (forward-char -1))
344 (if (listp indent) (setq indent (car indent)))
345 (setq shift-amt (- indent (current-column)))
346 (if (zerop shift-amt)
347 nil
348 (delete-region beg (point))
349 (indent-to indent)))
350 ;; If initial point was within line's indentation,
351 ;; position after the indentation. Else stay at same point in text.
352 (if (> (- (point-max) pos) (point))
353 (goto-char (- (point-max) pos)))
354 ;; If desired, shift remaining lines of expression the same amount.
355 (and whole-exp (not (zerop shift-amt))
356 (save-excursion
357 (goto-char beg)
358 (forward-sexp 1)
359 (setq end (point))
360 (goto-char beg)
361 (forward-line 1)
362 (setq beg (point))
363 (> end beg))
364 (indent-code-rigidly beg end shift-amt)))))
365
366 (defvar calculate-lisp-indent-last-sexp)
367
368 (defun calculate-lisp-indent (&optional parse-start)
369 "Return appropriate indentation for current line as Lisp code.
370 In usual case returns an integer: the column to indent to.
371 Can instead return a list, whose car is the column to indent to.
372 This means that following lines at the same level of indentation
373 should not necessarily be indented the same way.
374 The second element of the list is the buffer position
375 of the start of the containing expression."
376 (save-excursion
377 (beginning-of-line)
378 (let ((indent-point (point))
379 state paren-depth
380 ;; setting this to a number inhibits calling hook
381 (desired-indent nil)
382 (retry t)
383 calculate-lisp-indent-last-sexp containing-sexp)
384 (if parse-start
385 (goto-char parse-start)
386 (beginning-of-defun))
387 ;; Find outermost containing sexp
388 (while (< (point) indent-point)
389 (setq state (parse-partial-sexp (point) indent-point 0)))
390 ;; Find innermost containing sexp
391 (while (and retry
392 state
393 (> (setq paren-depth (elt state 0)) 0))
394 (setq retry nil)
395 (setq calculate-lisp-indent-last-sexp (elt state 2))
396 (setq containing-sexp (elt state 1))
397 ;; Position following last unclosed open.
398 (goto-char (1+ containing-sexp))
399 ;; Is there a complete sexp since then?
400 (if (and calculate-lisp-indent-last-sexp
401 (> calculate-lisp-indent-last-sexp (point)))
402 ;; Yes, but is there a containing sexp after that?
403 (let ((peek (parse-partial-sexp calculate-lisp-indent-last-sexp
404 indent-point 0)))
405 (if (setq retry (car (cdr peek))) (setq state peek)))))
406 (if retry
407 nil
408 ;; Innermost containing sexp found
409 (goto-char (1+ containing-sexp))
410 (if (not calculate-lisp-indent-last-sexp)
411 ;; indent-point immediately follows open paren.
412 ;; Don't call hook.
413 (setq desired-indent (current-column))
414 ;; Find the start of first element of containing sexp.
415 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
416 (cond ((looking-at "\\s(")
417 ;; First element of containing sexp is a list.
418 ;; Indent under that list.
419 )
420 ((> (save-excursion (forward-line 1) (point))
421 calculate-lisp-indent-last-sexp)
422 ;; This is the first line to start within the containing sexp.
423 ;; It's almost certainly a function call.
424 (if (= (point) calculate-lisp-indent-last-sexp)
425 ;; Containing sexp has nothing before this line
426 ;; except the first element. Indent under that element.
427 nil
428 ;; Skip the first element, find start of second (the first
429 ;; argument of the function call) and indent under.
430 (progn (forward-sexp 1)
431 (parse-partial-sexp (point)
432 calculate-lisp-indent-last-sexp
433 0 t)))
434 (backward-prefix-chars))
435 (t
436 ;; Indent beneath first sexp on same line as
437 ;; calculate-lisp-indent-last-sexp. Again, it's
438 ;; almost certainly a function call.
439 (goto-char calculate-lisp-indent-last-sexp)
440 (beginning-of-line)
441 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp
442 0 t)
443 (backward-prefix-chars)))))
444 ;; Point is at the point to indent under unless we are inside a string.
445 ;; Call indentation hook except when overridden by lisp-indent-offset
446 ;; or if the desired indentation has already been computed.
447 (let ((normal-indent (current-column)))
448 (cond ((elt state 3)
449 ;; Inside a string, don't change indentation.
450 (goto-char indent-point)
451 (skip-chars-forward " \t")
452 (current-column))
453 ((and (integerp lisp-indent-offset) containing-sexp)
454 ;; Indent by constant offset
455 (goto-char containing-sexp)
456 (+ (current-column) lisp-indent-offset))
457 (desired-indent)
458 ((and (boundp 'lisp-indent-function)
459 lisp-indent-function
460 (not retry))
461 (or (funcall lisp-indent-function indent-point state)
462 normal-indent))
463 (t
464 normal-indent))))))
465
466 (defun lisp-indent-function (indent-point state)
467 (let ((normal-indent (current-column)))
468 (goto-char (1+ (elt state 1)))
469 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
470 (if (and (elt state 2)
471 (not (looking-at "\\sw\\|\\s_")))
472 ;; car of form doesn't seem to be a a symbol
473 (progn
474 (if (not (> (save-excursion (forward-line 1) (point))
475 calculate-lisp-indent-last-sexp))
476 (progn (goto-char calculate-lisp-indent-last-sexp)
477 (beginning-of-line)
478 (parse-partial-sexp (point)
479 calculate-lisp-indent-last-sexp 0 t)))
480 ;; Indent under the list or under the first sexp on the same
481 ;; line as calculate-lisp-indent-last-sexp. Note that first
482 ;; thing on that line has to be complete sexp since we are
483 ;; inside the innermost containing sexp.
484 (backward-prefix-chars)
485 (current-column))
486 (let ((function (buffer-substring (point)
487 (progn (forward-sexp 1) (point))))
488 method)
489 (setq method (or (get (intern-soft function) 'lisp-indent-function)
490 (get (intern-soft function) 'lisp-indent-hook)))
491 (cond ((or (eq method 'defun)
492 (and (null method)
493 (> (length function) 3)
494 (string-match "\\`def" function)))
495 (lisp-indent-defform state indent-point))
496 ((integerp method)
497 (lisp-indent-specform method state
498 indent-point normal-indent))
499 (method
500 (funcall method state indent-point)))))))
501
502 (defconst lisp-body-indent 2
503 "Number of columns to indent the second line of a `(def...)' form.")
504
505 (defun lisp-indent-specform (count state indent-point normal-indent)
506 (let ((containing-form-start (elt state 1))
507 (i count)
508 body-indent containing-form-column)
509 ;; Move to the start of containing form, calculate indentation
510 ;; to use for non-distinguished forms (> count), and move past the
511 ;; function symbol. lisp-indent-function guarantees that there is at
512 ;; least one word or symbol character following open paren of containing
513 ;; form.
514 (goto-char containing-form-start)
515 (setq containing-form-column (current-column))
516 (setq body-indent (+ lisp-body-indent containing-form-column))
517 (forward-char 1)
518 (forward-sexp 1)
519 ;; Now find the start of the last form.
520 (parse-partial-sexp (point) indent-point 1 t)
521 (while (and (< (point) indent-point)
522 (condition-case ()
523 (progn
524 (setq count (1- count))
525 (forward-sexp 1)
526 (parse-partial-sexp (point) indent-point 1 t))
527 (error nil))))
528 ;; Point is sitting on first character of last (or count) sexp.
529 (if (> count 0)
530 ;; A distinguished form. If it is the first or second form use double
531 ;; lisp-body-indent, else normal indent. With lisp-body-indent bound
532 ;; to 2 (the default), this just happens to work the same with if as
533 ;; the older code, but it makes unwind-protect, condition-case,
534 ;; with-output-to-temp-buffer, et. al. much more tasteful. The older,
535 ;; less hacked, behavior can be obtained by replacing below with
536 ;; (list normal-indent containing-form-start).
537 (if (<= (- i count) 1)
538 (list (+ containing-form-column (* 2 lisp-body-indent))
539 containing-form-start)
540 (list normal-indent containing-form-start))
541 ;; A non-distinguished form. Use body-indent if there are no
542 ;; distinguished forms and this is the first undistinguished form,
543 ;; or if this is the first undistinguished form and the preceding
544 ;; distinguished form has indentation at least as great as body-indent.
545 (if (or (and (= i 0) (= count 0))
546 (and (= count 0) (<= body-indent normal-indent)))
547 body-indent
548 normal-indent))))
549
550 (defun lisp-indent-defform (state indent-point)
551 (goto-char (car (cdr state)))
552 (forward-line 1)
553 (if (> (point) (car (cdr (cdr state))))
554 (progn
555 (goto-char (car (cdr state)))
556 (+ lisp-body-indent (current-column)))))
557
558 \f
559 ;; (put 'progn 'lisp-indent-function 0), say, causes progn to be indented
560 ;; like defun if the first form is placed on the next line, otherwise
561 ;; it is indented like any other form (i.e. forms line up under first).
562
563 (put 'lambda 'lisp-indent-function 'defun)
564 (put 'autoload 'lisp-indent-function 'defun)
565 (put 'progn 'lisp-indent-function 0)
566 (put 'prog1 'lisp-indent-function 1)
567 (put 'prog2 'lisp-indent-function 2)
568 (put 'save-excursion 'lisp-indent-function 0)
569 (put 'save-window-excursion 'lisp-indent-function 0)
570 (put 'save-selected-window 'lisp-indent-function 0)
571 (put 'save-restriction 'lisp-indent-function 0)
572 (put 'save-match-data 'lisp-indent-function 0)
573 (put 'let 'lisp-indent-function 1)
574 (put 'let* 'lisp-indent-function 1)
575 (put 'while 'lisp-indent-function 1)
576 (put 'if 'lisp-indent-function 2)
577 (put 'catch 'lisp-indent-function 1)
578 (put 'condition-case 'lisp-indent-function 2)
579 (put 'unwind-protect 'lisp-indent-function 1)
580 (put 'with-output-to-temp-buffer 'lisp-indent-function 1)
581
582 (defun indent-sexp (&optional endpos)
583 "Indent each line of the list starting just after point.
584 If optional arg ENDPOS is given, indent each line, stopping when
585 ENDPOS is encountered."
586 (interactive)
587 (let ((indent-stack (list nil))
588 (next-depth 0)
589 ;; If ENDPOS is non-nil, use nil as STARTING-POINT
590 ;; so that calculate-lisp-indent will find the beginning of
591 ;; the defun we are in.
592 ;; If ENDPOS is nil, it is safe not to scan before point
593 ;; since every line we indent is more deeply nested than point is.
594 (starting-point (if endpos nil (point)))
595 (last-point (point))
596 last-depth bol outer-loop-done inner-loop-done state this-indent)
597 (or endpos
598 ;; Get error now if we don't have a complete sexp after point.
599 (save-excursion (forward-sexp 1)))
600 (save-excursion
601 (setq outer-loop-done nil)
602 (while (if endpos (< (point) endpos)
603 (not outer-loop-done))
604 (setq last-depth next-depth
605 inner-loop-done nil)
606 ;; Parse this line so we can learn the state
607 ;; to indent the next line.
608 ;; This inner loop goes through only once
609 ;; unless a line ends inside a string.
610 (while (and (not inner-loop-done)
611 (not (setq outer-loop-done (eobp))))
612 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
613 nil nil state))
614 (setq next-depth (car state))
615 ;; If the line contains a comment other than the sort
616 ;; that is indented like code,
617 ;; indent it now with indent-for-comment.
618 ;; Comments indented like code are right already.
619 ;; In any case clear the in-comment flag in the state
620 ;; because parse-partial-sexp never sees the newlines.
621 (if (car (nthcdr 4 state))
622 (progn (indent-for-comment)
623 (end-of-line)
624 (setcar (nthcdr 4 state) nil)))
625 ;; If this line ends inside a string,
626 ;; go straight to next line, remaining within the inner loop,
627 ;; and turn off the \-flag.
628 (if (car (nthcdr 3 state))
629 (progn
630 (forward-line 1)
631 (setcar (nthcdr 5 state) nil))
632 (setq inner-loop-done t)))
633 (and endpos
634 (<= next-depth 0)
635 (progn
636 (setq indent-stack (append indent-stack
637 (make-list (- next-depth) nil))
638 last-depth (- last-depth next-depth)
639 next-depth 0)))
640 (or outer-loop-done endpos
641 (setq outer-loop-done (<= next-depth 0)))
642 (if outer-loop-done
643 (forward-line 1)
644 (while (> last-depth next-depth)
645 (setq indent-stack (cdr indent-stack)
646 last-depth (1- last-depth)))
647 (while (< last-depth next-depth)
648 (setq indent-stack (cons nil indent-stack)
649 last-depth (1+ last-depth)))
650 ;; Now go to the next line and indent it according
651 ;; to what we learned from parsing the previous one.
652 (forward-line 1)
653 (setq bol (point))
654 (skip-chars-forward " \t")
655 ;; But not if the line is blank, or just a comment
656 ;; (except for double-semi comments; indent them as usual).
657 (if (or (eobp) (looking-at "\\s<\\|\n"))
658 nil
659 (if (and (car indent-stack)
660 (>= (car indent-stack) 0))
661 (setq this-indent (car indent-stack))
662 (let ((val (calculate-lisp-indent
663 (if (car indent-stack) (- (car indent-stack))
664 starting-point))))
665 (if (integerp val)
666 (setcar indent-stack
667 (setq this-indent val))
668 (setcar indent-stack (- (car (cdr val))))
669 (setq this-indent (car val)))))
670 (if (/= (current-column) this-indent)
671 (progn (delete-region bol (point))
672 (indent-to this-indent)))))
673 (or outer-loop-done
674 (setq outer-loop-done (= (point) last-point))
675 (setq last-point (point)))))))
676
677 ;; Indent every line whose first char is between START and END inclusive.
678 (defun lisp-indent-region (start end)
679 (save-excursion
680 (let ((endmark (copy-marker end)))
681 (goto-char start)
682 (and (bolp) (not (eolp))
683 (lisp-indent-line))
684 (indent-sexp endmark)
685 (set-marker endmark nil))))
686 \f
687 ;;;; Lisp paragraph filling commands.
688
689 (defun lisp-fill-paragraph (&optional justify)
690 "Like \\[fill-paragraph], but handle Emacs Lisp comments.
691 If any of the current line is a comment, fill the comment or the
692 paragraph of it that point is in, preserving the comment's indentation
693 and initial semicolons."
694 (interactive "P")
695 (let (
696 ;; Non-nil if the current line contains a comment.
697 has-comment
698
699 ;; If has-comment, the appropriate fill-prefix for the comment.
700 comment-fill-prefix
701 )
702
703 ;; Figure out what kind of comment we are looking at.
704 (save-excursion
705 (beginning-of-line)
706 (cond
707
708 ;; A line with nothing but a comment on it?
709 ((looking-at "[ \t]*;[; \t]*")
710 (setq has-comment t
711 comment-fill-prefix (buffer-substring (match-beginning 0)
712 (match-end 0))))
713
714 ;; A line with some code, followed by a comment? Remember that the
715 ;; semi which starts the comment shouldn't be part of a string or
716 ;; character.
717 ((condition-case nil
718 (save-restriction
719 (narrow-to-region (point-min)
720 (save-excursion (end-of-line) (point)))
721 (while (not (looking-at ";\\|$"))
722 (skip-chars-forward "^;\n\"\\\\?")
723 (cond
724 ((eq (char-after (point)) ?\\) (forward-char 2))
725 ((memq (char-after (point)) '(?\" ??)) (forward-sexp 1))))
726 (looking-at ";+[\t ]*"))
727 (error nil))
728 (setq has-comment t)
729 (setq comment-fill-prefix
730 (concat (make-string (/ (current-column) 8) ?\t)
731 (make-string (% (current-column) 8) ?\ )
732 (buffer-substring (match-beginning 0) (match-end 0)))))))
733
734 (if (not has-comment)
735 (fill-paragraph justify)
736
737 ;; Narrow to include only the comment, and then fill the region.
738 (save-restriction
739 (beginning-of-line)
740 (narrow-to-region
741 ;; Find the first line we should include in the region to fill.
742 (save-excursion
743 (while (and (zerop (forward-line -1))
744 (looking-at "^[ \t]*;")))
745 ;; We may have gone to far. Go forward again.
746 (or (looking-at ".*;")
747 (forward-line 1))
748 (point))
749 ;; Find the beginning of the first line past the region to fill.
750 (save-excursion
751 (while (progn (forward-line 1)
752 (looking-at "^[ \t]*;")))
753 (point)))
754
755 ;; Lines with only semicolons on them can be paragraph boundaries.
756 (let ((paragraph-start (concat paragraph-start "\\|[ \t;]*$"))
757 (paragraph-separate (concat paragraph-start "\\|[ \t;]*$"))
758 (fill-prefix comment-fill-prefix))
759 (fill-region (point-min) (point-max) justify t))))
760 t))
761
762 \f
763 (defun indent-code-rigidly (start end arg &optional nochange-regexp)
764 "Indent all lines of code, starting in the region, sideways by ARG columns.
765 Does not affect lines starting inside comments or strings, assuming that
766 the start of the region is not inside them.
767
768 Called from a program, takes args START, END, COLUMNS and NOCHANGE-REGEXP.
769 The last is a regexp which, if matched at the beginning of a line,
770 means don't indent that line."
771 (interactive "r\np")
772 (let (state)
773 (save-excursion
774 (goto-char end)
775 (setq end (point-marker))
776 (goto-char start)
777 (or (bolp)
778 (setq state (parse-partial-sexp (point)
779 (progn
780 (forward-line 1) (point))
781 nil nil state)))
782 (while (< (point) end)
783 (or (car (nthcdr 3 state))
784 (and nochange-regexp
785 (looking-at nochange-regexp))
786 ;; If line does not start in string, indent it
787 (let ((indent (current-indentation)))
788 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
789 (or (eolp)
790 (indent-to (max 0 (+ indent arg)) 0))))
791 (setq state (parse-partial-sexp (point)
792 (progn
793 (forward-line 1) (point))
794 nil nil state))))))
795
796 (provide 'lisp-mode)
797
798 ;;; lisp-mode.el ends here