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