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