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