1 ;;; lisp-mode.el --- Lisp mode, and its idiosyncratic commands
3 ;; Copyright (C) 1985, 1986, 1999, 2000, 2001, 2003 Free Software Foundation, Inc.
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)
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.
27 ;; The base major mode for editing Lisp code (used also for Emacs Lisp).
28 ;; This mode is documented in the Emacs manual.
32 (defvar lisp-mode-abbrev-table nil)
34 (defvar emacs-lisp-mode-syntax-table
35 (let ((table (make-syntax-table)))
38 (modify-syntax-entry i "_ " table)
42 (modify-syntax-entry i "_ " table)
46 (modify-syntax-entry i "_ " table)
50 (modify-syntax-entry i "_ " table)
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 (modify-syntax-entry ?@ "' " table)
63 ;; Used to be singlequote; changed for flonums.
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 (modify-syntax-entry ?\] ")[ " table))
74 (defvar lisp-mode-syntax-table
75 (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table)))
76 (modify-syntax-entry ?\[ "_ " table)
77 (modify-syntax-entry ?\] "_ " table)
78 (modify-syntax-entry ?# "' 14bn" table)
79 (modify-syntax-entry ?| "\" 23b" table)
82 (define-abbrev-table 'lisp-mode-abbrev-table ())
84 (defvar lisp-imenu-generic-expression
87 (purecopy (concat "^\\s-*("
90 '("defun" "defun*" "defsubst" "defmacro"
91 "defadvice" "define-skeleton"
92 "define-minor-mode" "define-derived-mode"
93 "define-compiler-macro" "define-modify-macro"
94 "defsetf" "define-setf-expander"
95 "define-method-combination"
96 "defgeneric" "defmethod") t))
97 "\\s-+\\(\\sw\\(\\sw\\|\\s_\\)+\\)"))
99 (list (purecopy "Variables")
100 (purecopy (concat "^\\s-*("
103 '("defvar" "defconst" "defconstant" "defcustom"
104 "defparameter" "define-symbol-macro") t))
105 "\\s-+\\(\\sw\\(\\sw\\|\\s_\\)+\\)"))
107 (list (purecopy "Types")
108 (purecopy (concat "^\\s-*("
111 '("defgroup" "deftheme" "deftype" "defstruct"
112 "defclass" "define-condition" "define-widget"
113 "defface" "defpackage") t))
114 "\\s-+'?\\(\\sw\\(\\sw\\|\\s_\\)+\\)"))
117 "Imenu generic expression for Lisp mode. See `imenu-generic-expression'.")
119 ;; This was originally in autoload.el and is still used there.
120 (put 'autoload 'doc-string-elt 3)
121 (put 'defun 'doc-string-elt 3)
122 (put 'defun* 'doc-string-elt 3)
123 (put 'defvar 'doc-string-elt 3)
124 (put 'defcustom 'doc-string-elt 3)
125 (put 'deftheme 'doc-string-elt 2)
126 (put 'defconst 'doc-string-elt 3)
127 (put 'defmacro 'doc-string-elt 3)
128 (put 'defmacro* 'doc-string-elt 3)
129 (put 'defsubst 'doc-string-elt 3)
130 (put 'define-skeleton 'doc-string-elt 2)
131 (put 'define-derived-mode 'doc-string-elt 4)
132 (put 'easy-mmode-define-minor-mode 'doc-string-elt 2)
133 (put 'define-minor-mode 'doc-string-elt 2)
134 (put 'define-generic-mode 'doc-string-elt 7)
135 ;; define-global-mode has no explicit docstring.
136 (put 'easy-mmode-define-global-mode 'doc-string-elt 0)
137 (put 'define-ibuffer-filter 'doc-string-elt 2)
138 (put 'define-ibuffer-op 'doc-string-elt 3)
139 (put 'define-ibuffer-sorter 'doc-string-elt 2)
141 (defun lisp-font-lock-syntactic-face-function (state)
143 (if (and (eq (nth 0 state) 1)
144 ;; This might be a docstring.
147 (goto-char (nth 8 state))
149 (while (and (not (bobp))
150 (progn (backward-sexp 1) (setq n (1+ n)))))
153 (let ((sym (intern-soft
155 (point) (progn (forward-sexp 1) (point))))))
156 (eq n (or (get sym 'doc-string-elt) 3)))))))
158 font-lock-string-face)
159 font-lock-comment-face))
161 ;; The LISP-SYNTAX argument is used by code in inf-lisp.el and is
162 ;; (uselessly) passed from pp.el, chistory.el, gnus-kill.el and score-mode.el
163 (defun lisp-mode-variables (&optional lisp-syntax)
165 (set-syntax-table lisp-mode-syntax-table))
166 (setq local-abbrev-table lisp-mode-abbrev-table)
167 (make-local-variable 'paragraph-ignore-fill-prefix)
168 (setq paragraph-ignore-fill-prefix t)
169 (make-local-variable 'fill-paragraph-function)
170 (setq fill-paragraph-function 'lisp-fill-paragraph)
171 ;; Adaptive fill mode gets in the way of auto-fill,
172 ;; and should make no difference for explicit fill
173 ;; because lisp-fill-paragraph should do the job.
174 ;; I believe that newcomment's auto-fill code properly deals with it -stef
175 ;;(set (make-local-variable 'adaptive-fill-mode) nil)
176 (make-local-variable 'normal-auto-fill-function)
177 (setq normal-auto-fill-function 'lisp-mode-auto-fill)
178 (make-local-variable 'indent-line-function)
179 (setq indent-line-function 'lisp-indent-line)
180 (make-local-variable 'indent-region-function)
181 (setq indent-region-function 'lisp-indent-region)
182 (make-local-variable 'parse-sexp-ignore-comments)
183 (setq parse-sexp-ignore-comments t)
184 (make-local-variable 'outline-regexp)
185 (setq outline-regexp ";;;;* \\|(")
186 (make-local-variable 'outline-level)
187 (setq outline-level 'lisp-outline-level)
188 (make-local-variable 'comment-start)
189 (setq comment-start ";")
190 (make-local-variable 'comment-start-skip)
191 ;; Look within the line for a ; following an even number of backslashes
192 ;; after either a non-backslash or the line beginning.
193 (setq comment-start-skip "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\);+ *")
194 (make-local-variable 'comment-add)
195 (setq comment-add 1) ;default to `;;' in comment-region
196 (make-local-variable 'comment-column)
197 (setq comment-column 40)
198 (make-local-variable 'comment-indent-function)
199 (setq comment-indent-function 'lisp-comment-indent)
200 (make-local-variable 'imenu-generic-expression)
201 (setq imenu-generic-expression lisp-imenu-generic-expression)
202 (make-local-variable 'multibyte-syntax-as-symbol)
203 (setq multibyte-syntax-as-symbol t)
204 (set (make-local-variable 'syntax-begin-function) 'beginning-of-defun)
205 (setq font-lock-defaults
206 '((lisp-font-lock-keywords
207 lisp-font-lock-keywords-1 lisp-font-lock-keywords-2)
208 nil nil (("+-*/.<>=!?$%_&~^:" . "w")) beginning-of-defun
209 (font-lock-mark-block-function . mark-defun)
210 (font-lock-syntactic-face-function
211 . lisp-font-lock-syntactic-face-function))))
213 (defun lisp-outline-level ()
214 "Lisp mode `outline-level' function."
217 (looking-at outline-regexp)
218 (- (match-end 0) (match-beginning 0))))
221 (defvar lisp-mode-shared-map
222 (let ((map (make-sparse-keymap)))
223 (define-key map "\t" 'lisp-indent-line)
224 (define-key map "\e\C-q" 'indent-sexp)
225 (define-key map "\177" 'backward-delete-char-untabify)
226 ;; This gets in the way when viewing a Lisp file in view-mode. As
227 ;; long as [backspace] is mapped into DEL via the
228 ;; function-key-map, this should remain disabled!!
229 ;;;(define-key map [backspace] 'backward-delete-char-untabify)
231 "Keymap for commands shared by all sorts of Lisp modes.")
233 (defvar emacs-lisp-mode-map ()
234 "Keymap for Emacs Lisp mode.
235 All commands in `lisp-mode-shared-map' are inherited by this map.")
237 (if emacs-lisp-mode-map
239 (let ((map (make-sparse-keymap "Emacs-Lisp")))
240 (setq emacs-lisp-mode-map (make-sparse-keymap))
241 (set-keymap-parent emacs-lisp-mode-map lisp-mode-shared-map)
242 (define-key emacs-lisp-mode-map "\e\t" 'lisp-complete-symbol)
243 (define-key emacs-lisp-mode-map "\e\C-x" 'eval-defun)
244 (define-key emacs-lisp-mode-map [menu-bar] (make-sparse-keymap))
245 (define-key emacs-lisp-mode-map [menu-bar emacs-lisp]
246 (cons "Emacs-Lisp" map))
247 (define-key map [edebug-defun]
248 '("Instrument Function for Debugging" . edebug-defun))
249 (define-key map [byte-recompile]
250 '("Byte-recompile Directory..." . byte-recompile-directory))
251 (define-key map [emacs-byte-compile-and-load]
252 '("Byte-compile And Load" . emacs-lisp-byte-compile-and-load))
253 (define-key map [byte-compile]
254 '("Byte-compile This File" . emacs-lisp-byte-compile))
255 (define-key map [separator-eval] '("--"))
256 (define-key map [eval-buffer] '("Evaluate Buffer" . eval-current-buffer))
257 (define-key map [eval-region] '("Evaluate Region" . eval-region))
258 (define-key map [eval-sexp] '("Evaluate Last S-expression" . eval-last-sexp))
259 (define-key map [separator-format] '("--"))
260 (define-key map [comment-region] '("Comment Out Region" . comment-region))
261 (define-key map [indent-region] '("Indent Region" . indent-region))
262 (define-key map [indent-line] '("Indent Line" . lisp-indent-line))
263 (put 'eval-region 'menu-enable 'mark-active)
264 (put 'comment-region 'menu-enable 'mark-active)
265 (put 'indent-region 'menu-enable 'mark-active)))
267 (defun emacs-lisp-byte-compile ()
268 "Byte compile the file containing the current buffer."
271 (byte-compile-file buffer-file-name)
272 (error "The buffer must be saved in a file first")))
274 (defun emacs-lisp-byte-compile-and-load ()
275 "Byte-compile the current file (if it has changed), then load compiled code."
278 (error "The buffer must be saved in a file first"))
280 ;; Recompile if file or buffer has changed since last compilation.
281 (if (and (buffer-modified-p)
282 (y-or-n-p (format "Save buffer %s first? " (buffer-name))))
284 (let ((compiled-file-name (byte-compile-dest-file buffer-file-name)))
285 (if (file-newer-than-file-p compiled-file-name buffer-file-name)
286 (load-file compiled-file-name)
287 (byte-compile-file buffer-file-name t))))
289 (defcustom emacs-lisp-mode-hook nil
290 "Hook run when entering Emacs Lisp mode."
291 :options '(turn-on-eldoc-mode imenu-add-menubar-index checkdoc-minor-mode)
295 (defcustom lisp-mode-hook nil
296 "Hook run when entering Lisp mode."
297 :options '(imenu-add-menubar-index)
301 (defcustom lisp-interaction-mode-hook nil
302 "Hook run when entering Lisp Interaction mode."
303 :options '(turn-on-eldoc-mode)
307 (defun emacs-lisp-mode ()
308 "Major mode for editing Lisp code to run in Emacs.
310 Delete converts tabs to spaces as it moves back.
311 Blank lines separate paragraphs. Semicolons start comments.
312 \\{emacs-lisp-mode-map}
313 Entry to this mode calls the value of `emacs-lisp-mode-hook'
314 if that value is non-nil."
316 (kill-all-local-variables)
317 (use-local-map emacs-lisp-mode-map)
318 (set-syntax-table emacs-lisp-mode-syntax-table)
319 (setq major-mode 'emacs-lisp-mode)
320 (setq mode-name "Emacs-Lisp")
321 (lisp-mode-variables)
322 (setq imenu-case-fold-search nil)
323 (run-hooks 'emacs-lisp-mode-hook))
324 (put 'emacs-lisp-mode 'custom-mode-group 'lisp)
326 (defvar lisp-mode-map
327 (let ((map (make-sparse-keymap)))
328 (set-keymap-parent map lisp-mode-shared-map)
329 (define-key map "\e\C-x" 'lisp-eval-defun)
330 (define-key map "\C-c\C-z" 'run-lisp)
332 "Keymap for ordinary Lisp mode.
333 All commands in `lisp-mode-shared-map' are inherited by this map.")
336 "Major mode for editing Lisp code for Lisps other than GNU Emacs Lisp.
338 Delete converts tabs to spaces as it moves back.
339 Blank lines separate paragraphs. Semicolons start comments.
341 Note that `run-lisp' may be used either to start an inferior Lisp job
342 or to switch back to an existing one.
344 Entry to this mode calls the value of `lisp-mode-hook'
345 if that value is non-nil."
347 (kill-all-local-variables)
348 (use-local-map lisp-mode-map)
349 (setq major-mode 'lisp-mode)
350 (setq mode-name "Lisp")
351 (lisp-mode-variables)
352 (make-local-variable 'comment-start-skip)
353 (setq comment-start-skip
354 "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\)\\(;+\\|#|\\) *")
355 (make-local-variable 'font-lock-keywords-case-fold-search)
356 (setq font-lock-keywords-case-fold-search t)
357 (setq imenu-case-fold-search t)
358 (set-syntax-table lisp-mode-syntax-table)
359 (run-hooks 'lisp-mode-hook))
361 ;; This will do unless inf-lisp.el is loaded.
362 (defun lisp-eval-defun (&optional and-go)
363 "Send the current defun to the Lisp process made by \\[run-lisp]."
365 (error "Process lisp does not exist"))
367 (defvar lisp-interaction-mode-map
368 (let ((map (make-sparse-keymap)))
369 (set-keymap-parent map lisp-mode-shared-map)
370 (define-key map "\e\C-x" 'eval-defun)
371 (define-key map "\e\t" 'lisp-complete-symbol)
372 (define-key map "\n" 'eval-print-last-sexp)
374 "Keymap for Lisp Interaction mode.
375 All commands in `lisp-mode-shared-map' are inherited by this map.")
377 (defvar lisp-interaction-mode-abbrev-table lisp-mode-abbrev-table)
378 (define-derived-mode lisp-interaction-mode emacs-lisp-mode "Lisp Interaction"
379 "Major mode for typing and evaluating Lisp forms.
380 Like Lisp mode except that \\[eval-print-last-sexp] evals the Lisp expression
381 before point, and prints its value into the buffer, advancing point.
382 Note that printing is controlled by `eval-expression-print-length'
383 and `eval-expression-print-level'.
386 Delete converts tabs to spaces as it moves back.
387 Paragraphs are separated only by blank lines.
388 Semicolons start comments.
389 \\{lisp-interaction-mode-map}
390 Entry to this mode calls the value of `lisp-interaction-mode-hook'
391 if that value is non-nil.")
393 (defun eval-print-last-sexp ()
394 "Evaluate sexp before point; print value into current buffer.
396 Note that printing the result is controlled by the variables
397 `eval-expression-print-length' and `eval-expression-print-level',
400 (let ((standard-output (current-buffer)))
406 (defun last-sexp-setup-props (beg end value alt1 alt2)
407 "Set up text properties for the output of `eval-last-sexp-1'.
408 BEG and END are the start and end of the output in current-buffer.
409 VALUE is the Lisp value printed, ALT1 and ALT2 are strings for the
410 alternative printed representations that can be displayed."
411 (let ((map (make-sparse-keymap)))
412 (define-key map "\C-m" 'last-sexp-toggle-display)
413 (define-key map [down-mouse-2] 'mouse-set-point)
414 (define-key map [mouse-2] 'last-sexp-toggle-display)
417 `(printed-value (,value ,alt1 ,alt2)
420 help-echo "RET, mouse-2: toggle abbreviated display"
421 rear-nonsticky (mouse-face keymap help-echo
425 (defun last-sexp-toggle-display (&optional arg)
426 "Toggle between abbreviated and unabbreviated printed representations."
428 ;; Normally this command won't be called at end of line.
429 ;; But when the end of the line is also the end of the buffer,
430 ;; it does get called. For consistency, pretend it was not called.
432 (let ((prefix-arg arg))
433 (command-execute (lookup-key global-map (this-single-command-keys))))
434 (let ((value (get-text-property (point) 'printed-value)))
436 (let ((beg (or (previous-single-property-change (min (point-max) (1+ (point)))
439 (end (or (next-single-char-property-change (point) 'printed-value) (point)))
440 (standard-output (current-buffer))
442 (delete-region beg end)
443 (insert (nth 1 value))
444 (last-sexp-setup-props beg (point)
448 (goto-char (min (point-max) point)))))))
450 (defun prin1-char (char)
451 "Return a string representing CHAR as a character rather than as an integer.
452 If CHAR is not a character, return nil."
454 (char-valid-p (event-basic-type char))
459 (cond ((eq modif 'super) "\\s-")
460 (t (string ?\\ (upcase (aref (symbol-name modif) 0)) ?-))))
461 (event-modifiers char) "")
462 (string (event-basic-type char)))))
464 (defun eval-last-sexp-1 (eval-last-sexp-arg-internal)
465 "Evaluate sexp before point; print value in minibuffer.
466 With argument, print output into current buffer."
467 (let ((standard-output (if eval-last-sexp-arg-internal (current-buffer) t)))
469 (eval (let ((stab (syntax-table))
473 (with-syntax-table emacs-lisp-mode-syntax-table
474 ;; If this sexp appears to be enclosed in `...'
475 ;; then ignore the surrounding quotes.
477 (or (eq (following-char) ?\')
478 (eq (preceding-char) ?\')))
480 ;; If we were after `?\e' (or similar case),
481 ;; use the whole thing, not just the `e'.
482 (when (eq (preceding-char) ?\\)
484 (when (eq (preceding-char) ??)
488 (when (eq (preceding-char) ?=)
491 (skip-chars-backward "0-9#=")
492 (setq labeled-p (looking-at "\\(#[0-9]+=\\)+")))
497 ;; vladimir@cs.ualberta.ca 30-Jul-1997: skip ` in
498 ;; `variable' so that the value is returned, not the
500 (if (and ignore-quotes
501 (eq (following-char) ?`))
503 (narrow-to-region (point-min) opoint)
504 (setq expr (read (current-buffer)))
505 ;; If it's an (interactive ...) form, it's more
506 ;; useful to show how an interactive call would
509 (eq (car expr) 'interactive)
511 (list 'call-interactively
518 (let ((unabbreviated (let ((print-length nil) (print-level nil))
519 (prin1-to-string value)))
520 (print-length eval-expression-print-length)
521 (print-level eval-expression-print-level)
522 (char-string (prin1-char value))
527 (if (and (eq standard-output t) char-string)
528 (princ (concat " = " char-string)))
530 (when (and (bufferp standard-output)
531 (or (not (null print-length))
532 (not (null print-level)))
533 (not (string= unabbreviated
534 (buffer-substring-no-properties beg end))))
535 (last-sexp-setup-props beg end value
537 (buffer-substring-no-properties beg end))
541 (defun eval-last-sexp (eval-last-sexp-arg-internal)
542 "Evaluate sexp before point; print value in minibuffer.
543 Interactively, with prefix argument, print output into current buffer."
545 (if (null eval-expression-debug-on-error)
546 (eval-last-sexp-1 eval-last-sexp-arg-internal)
547 (let ((old-value (make-symbol "t")) new-value value)
548 (let ((debug-on-error old-value))
549 (setq value (eval-last-sexp-1 eval-last-sexp-arg-internal))
550 (setq new-value debug-on-error))
551 (unless (eq old-value new-value)
552 (setq debug-on-error new-value))
555 (defun eval-defun-1 (form)
556 "Change defvar into defconst within FORM.
557 Likewise for other constructs as necessary."
558 ;; The code in edebug-defun should be consistent with this, but not
559 ;; the same, since this gets a macroexpended form.
560 (cond ((not (listp form))
562 ((and (eq (car form) 'defvar)
563 (cdr-safe (cdr-safe form))
564 (boundp (cadr form)))
565 ;; Force variable to be re-set.
566 `(progn (defvar ,(nth 1 form) nil ,@(nthcdr 3 form))
567 (setq ,(nth 1 form) ,(nth 2 form))))
568 ;; `defcustom' is now macroexpanded to
569 ;; `custom-declare-variable' with a quoted value arg.
570 ((and (eq (car form) 'custom-declare-variable)
571 (default-boundp (eval (nth 1 form))))
572 ;; Force variable to be bound.
573 (set-default (eval (nth 1 form)) (eval (nth 1 (nth 2 form))))
575 ((eq (car form) 'progn)
576 (cons 'progn (mapcar 'eval-defun-1 (cdr form))))
579 (defun eval-defun-2 ()
580 "Evaluate defun that point is in or before.
581 The value is displayed in the minibuffer.
582 If the current defun is actually a call to `defvar',
583 then reset the variable using the initial value expression
584 even if the variable already has some other value.
585 \(Normally `defvar' does not change the variable's value
586 if it already has a value.\)
588 With argument, insert value in current buffer after the defun.
589 Return the result of evaluation."
591 (let ((debug-on-error eval-expression-debug-on-error)
592 (print-length eval-expression-print-length)
593 (print-level eval-expression-print-level))
595 ;; Arrange for eval-region to "read" the (possibly) altered form.
596 ;; eval-region handles recording which file defines a function or
597 ;; variable. Re-written using `apply' to avoid capturing
598 ;; variables like `end'.
601 (let ((standard-output t)
603 ;; Read the form from the buffer, and record where it ends.
608 (setq form (read (current-buffer)))
610 ;; Alter the form if necessary, changing defvar into defconst, etc.
611 (setq form (eval-defun-1 (macroexpand form)))
612 (list beg end standard-output
614 ;; Skipping to the end of the specified region
615 ;; will make eval-region return.
618 ;; The result of evaluation has been put onto VALUES. So return it.
621 (defun eval-defun (edebug-it)
622 "Evaluate the top-level form containing point, or after point.
624 If the current defun is actually a call to `defvar' or `defcustom',
625 evaluating it this way resets the variable using its initial value
626 expression even if the variable already has some other value.
627 \(Normally `defvar' and `defcustom' do not alter the value if there
630 With a prefix argument, instrument the code for Edebug.
632 If acting on a `defun' for FUNCTION, and the function was
633 instrumented, `Edebug: FUNCTION' is printed in the minibuffer. If not
634 instrumented, just FUNCTION is printed.
636 If not acting on a `defun', the result of evaluation is displayed in
637 the minibuffer. This display is controlled by the variables
638 `eval-expression-print-length' and `eval-expression-print-level',
643 (eval-defun (not edebug-all-defs)))
645 (if (null eval-expression-debug-on-error)
647 (let ((old-value (make-symbol "t")) new-value value)
648 (let ((debug-on-error old-value))
649 (setq value (eval-defun-2))
650 (setq new-value debug-on-error))
651 (unless (eq old-value new-value)
652 (setq debug-on-error new-value))
656 (defun lisp-comment-indent ()
657 (if (looking-at "\\s<\\s<\\s<")
659 (if (looking-at "\\s<\\s<")
660 (let ((tem (or (calculate-lisp-indent) (current-column))))
661 (if (listp tem) (car tem) tem))
662 (skip-chars-backward " \t")
663 (max (if (bolp) 0 (1+ (current-column)))
666 ;; This function just forces a more costly detection of comments (using
667 ;; parse-partial-sexp from beginning-of-defun). I.e. It avoids the problem of
668 ;; taking a `;' inside a string started on another line for a comment starter.
669 ;; Note: `newcomment' gets it right in 99% of the cases if you're using
670 ;; font-lock, anyway, so we could get rid of it. -stef
671 (defun lisp-mode-auto-fill ()
672 (if (> (current-column) (current-fill-column))
674 (nth 4 (syntax-ppss (point))))
676 (unless (and (boundp 'comment-auto-fill-only-comments)
677 comment-auto-fill-only-comments)
678 (let ((comment-start nil) (comment-start-skip nil))
681 (defvar lisp-indent-offset nil
682 "If non-nil, indent second line of expressions that many more columns.")
683 (defvar lisp-indent-function 'lisp-indent-function)
685 (defun lisp-indent-line (&optional whole-exp)
686 "Indent current line as Lisp code.
687 With argument, indent any additional lines of the same expression
688 rigidly along with this one."
690 (let ((indent (calculate-lisp-indent)) shift-amt end
691 (pos (- (point-max) (point)))
692 (beg (progn (beginning-of-line) (point))))
693 (skip-chars-forward " \t")
694 (if (or (null indent) (looking-at "\\s<\\s<\\s<"))
695 ;; Don't alter indentation of a ;;; comment line
696 ;; or a line that starts in a string.
697 (goto-char (- (point-max) pos))
698 (if (and (looking-at "\\s<") (not (looking-at "\\s<\\s<")))
699 ;; Single-semicolon comment lines should be indented
700 ;; as comment lines, not as code.
701 (progn (indent-for-comment) (forward-char -1))
702 (if (listp indent) (setq indent (car indent)))
703 (setq shift-amt (- indent (current-column)))
704 (if (zerop shift-amt)
706 (delete-region beg (point))
708 ;; If initial point was within line's indentation,
709 ;; position after the indentation. Else stay at same point in text.
710 (if (> (- (point-max) pos) (point))
711 (goto-char (- (point-max) pos)))
712 ;; If desired, shift remaining lines of expression the same amount.
713 (and whole-exp (not (zerop shift-amt))
722 (indent-code-rigidly beg end shift-amt)))))
724 (defvar calculate-lisp-indent-last-sexp)
726 (defun calculate-lisp-indent (&optional parse-start)
727 "Return appropriate indentation for current line as Lisp code.
728 In usual case returns an integer: the column to indent to.
729 If the value is nil, that means don't change the indentation
730 because the line starts inside a string.
732 The value can also be a list of the form (COLUMN CONTAINING-SEXP-START).
733 This means that following lines at the same level of indentation
734 should not necessarily be indented the same as this line.
735 Then COLUMN is the column to indent to, and CONTAINING-SEXP-START
736 is the buffer position of the start of the containing expression."
739 (let ((indent-point (point))
741 ;; setting this to a number inhibits calling hook
744 calculate-lisp-indent-last-sexp containing-sexp)
746 (goto-char parse-start)
747 (beginning-of-defun))
748 ;; Find outermost containing sexp
749 (while (< (point) indent-point)
750 (setq state (parse-partial-sexp (point) indent-point 0)))
751 ;; Find innermost containing sexp
754 (> (setq paren-depth (elt state 0)) 0))
756 (setq calculate-lisp-indent-last-sexp (elt state 2))
757 (setq containing-sexp (elt state 1))
758 ;; Position following last unclosed open.
759 (goto-char (1+ containing-sexp))
760 ;; Is there a complete sexp since then?
761 (if (and calculate-lisp-indent-last-sexp
762 (> calculate-lisp-indent-last-sexp (point)))
763 ;; Yes, but is there a containing sexp after that?
764 (let ((peek (parse-partial-sexp calculate-lisp-indent-last-sexp
766 (if (setq retry (car (cdr peek))) (setq state peek)))))
769 ;; Innermost containing sexp found
770 (goto-char (1+ containing-sexp))
771 (if (not calculate-lisp-indent-last-sexp)
772 ;; indent-point immediately follows open paren.
774 (setq desired-indent (current-column))
775 ;; Find the start of first element of containing sexp.
776 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
777 (cond ((looking-at "\\s(")
778 ;; First element of containing sexp is a list.
779 ;; Indent under that list.
781 ((> (save-excursion (forward-line 1) (point))
782 calculate-lisp-indent-last-sexp)
783 ;; This is the first line to start within the containing sexp.
784 ;; It's almost certainly a function call.
785 (if (= (point) calculate-lisp-indent-last-sexp)
786 ;; Containing sexp has nothing before this line
787 ;; except the first element. Indent under that element.
789 ;; Skip the first element, find start of second (the first
790 ;; argument of the function call) and indent under.
791 (progn (forward-sexp 1)
792 (parse-partial-sexp (point)
793 calculate-lisp-indent-last-sexp
795 (backward-prefix-chars))
797 ;; Indent beneath first sexp on same line as
798 ;; `calculate-lisp-indent-last-sexp'. Again, it's
799 ;; almost certainly a function call.
800 (goto-char calculate-lisp-indent-last-sexp)
802 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp
804 (backward-prefix-chars)))))
805 ;; Point is at the point to indent under unless we are inside a string.
806 ;; Call indentation hook except when overridden by lisp-indent-offset
807 ;; or if the desired indentation has already been computed.
808 (let ((normal-indent (current-column)))
810 ;; Inside a string, don't change indentation.
812 ((and (integerp lisp-indent-offset) containing-sexp)
813 ;; Indent by constant offset
814 (goto-char containing-sexp)
815 (+ (current-column) lisp-indent-offset))
817 ((and (boundp 'lisp-indent-function)
820 (or (funcall lisp-indent-function indent-point state)
825 (defun lisp-indent-function (indent-point state)
826 "This function is the normal value of the variable `lisp-indent-function'.
827 It is used when indenting a line within a function call, to see if the
828 called function says anything special about how to indent the line.
830 INDENT-POINT is the position where the user typed TAB, or equivalent.
831 Point is located at the point to indent under (for default indentation);
832 STATE is the `parse-partial-sexp' state for that position.
834 If the current line is in a call to a Lisp function
835 which has a non-nil property `lisp-indent-function',
836 that specifies how to do the indentation. The property value can be
837 * `defun', meaning indent `defun'-style;
838 * an integer N, meaning indent the first N arguments specially
839 like ordinary function arguments and then indent any further
840 aruments like a body;
841 * a function to call just as this function was called.
842 If that function returns nil, that means it doesn't specify
845 This function also returns nil meaning don't specify the indentation."
846 (let ((normal-indent (current-column)))
847 (goto-char (1+ (elt state 1)))
848 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
849 (if (and (elt state 2)
850 (not (looking-at "\\sw\\|\\s_")))
851 ;; car of form doesn't seem to be a symbol
853 (if (not (> (save-excursion (forward-line 1) (point))
854 calculate-lisp-indent-last-sexp))
855 (progn (goto-char calculate-lisp-indent-last-sexp)
857 (parse-partial-sexp (point)
858 calculate-lisp-indent-last-sexp 0 t)))
859 ;; Indent under the list or under the first sexp on the same
860 ;; line as calculate-lisp-indent-last-sexp. Note that first
861 ;; thing on that line has to be complete sexp since we are
862 ;; inside the innermost containing sexp.
863 (backward-prefix-chars)
865 (let ((function (buffer-substring (point)
866 (progn (forward-sexp 1) (point))))
868 (setq method (or (get (intern-soft function) 'lisp-indent-function)
869 (get (intern-soft function) 'lisp-indent-hook)))
870 (cond ((or (eq method 'defun)
872 (> (length function) 3)
873 (string-match "\\`def" function)))
874 (lisp-indent-defform state indent-point))
876 (lisp-indent-specform method state
877 indent-point normal-indent))
879 (funcall method state indent-point)))))))
881 (defvar lisp-body-indent 2
882 "Number of columns to indent the second line of a `(def...)' form.")
884 (defun lisp-indent-specform (count state indent-point normal-indent)
885 (let ((containing-form-start (elt state 1))
887 body-indent containing-form-column)
888 ;; Move to the start of containing form, calculate indentation
889 ;; to use for non-distinguished forms (> count), and move past the
890 ;; function symbol. lisp-indent-function guarantees that there is at
891 ;; least one word or symbol character following open paren of containing
893 (goto-char containing-form-start)
894 (setq containing-form-column (current-column))
895 (setq body-indent (+ lisp-body-indent containing-form-column))
898 ;; Now find the start of the last form.
899 (parse-partial-sexp (point) indent-point 1 t)
900 (while (and (< (point) indent-point)
903 (setq count (1- count))
905 (parse-partial-sexp (point) indent-point 1 t))
907 ;; Point is sitting on first character of last (or count) sexp.
909 ;; A distinguished form. If it is the first or second form use double
910 ;; lisp-body-indent, else normal indent. With lisp-body-indent bound
911 ;; to 2 (the default), this just happens to work the same with if as
912 ;; the older code, but it makes unwind-protect, condition-case,
913 ;; with-output-to-temp-buffer, et. al. much more tasteful. The older,
914 ;; less hacked, behavior can be obtained by replacing below with
915 ;; (list normal-indent containing-form-start).
916 (if (<= (- i count) 1)
917 (list (+ containing-form-column (* 2 lisp-body-indent))
918 containing-form-start)
919 (list normal-indent containing-form-start))
920 ;; A non-distinguished form. Use body-indent if there are no
921 ;; distinguished forms and this is the first undistinguished form,
922 ;; or if this is the first undistinguished form and the preceding
923 ;; distinguished form has indentation at least as great as body-indent.
924 (if (or (and (= i 0) (= count 0))
925 (and (= count 0) (<= body-indent normal-indent)))
929 (defun lisp-indent-defform (state indent-point)
930 (goto-char (car (cdr state)))
932 (if (> (point) (car (cdr (cdr state))))
934 (goto-char (car (cdr state)))
935 (+ lisp-body-indent (current-column)))))
938 ;; (put 'progn 'lisp-indent-function 0), say, causes progn to be indented
939 ;; like defun if the first form is placed on the next line, otherwise
940 ;; it is indented like any other form (i.e. forms line up under first).
942 (put 'lambda 'lisp-indent-function 'defun)
943 (put 'autoload 'lisp-indent-function 'defun)
944 (put 'progn 'lisp-indent-function 0)
945 (put 'prog1 'lisp-indent-function 1)
946 (put 'prog2 'lisp-indent-function 2)
947 (put 'save-excursion 'lisp-indent-function 0)
948 (put 'save-window-excursion 'lisp-indent-function 0)
949 (put 'save-selected-window 'lisp-indent-function 0)
950 (put 'save-restriction 'lisp-indent-function 0)
951 (put 'save-match-data 'lisp-indent-function 0)
952 (put 'save-current-buffer 'lisp-indent-function 0)
953 (put 'with-current-buffer 'lisp-indent-function 1)
954 (put 'combine-after-change-calls 'lisp-indent-function 0)
955 (put 'with-output-to-string 'lisp-indent-function 0)
956 (put 'with-temp-file 'lisp-indent-function 1)
957 (put 'with-temp-buffer 'lisp-indent-function 0)
958 (put 'with-temp-message 'lisp-indent-function 1)
959 (put 'with-syntax-table 'lisp-indent-function 1)
960 (put 'let 'lisp-indent-function 1)
961 (put 'let* 'lisp-indent-function 1)
962 (put 'while 'lisp-indent-function 1)
963 (put 'if 'lisp-indent-function 2)
964 (put 'read-if 'lisp-indent-function 2)
965 (put 'catch 'lisp-indent-function 1)
966 (put 'condition-case 'lisp-indent-function 2)
967 (put 'unwind-protect 'lisp-indent-function 1)
968 (put 'with-output-to-temp-buffer 'lisp-indent-function 1)
969 (put 'eval-after-load 'lisp-indent-function 1)
970 (put 'dolist 'lisp-indent-function 1)
971 (put 'dotimes 'lisp-indent-function 1)
972 (put 'when 'lisp-indent-function 1)
973 (put 'unless 'lisp-indent-function 1)
975 (defun indent-sexp (&optional endpos)
976 "Indent each line of the list starting just after point.
977 If optional arg ENDPOS is given, indent each line, stopping when
978 ENDPOS is encountered."
980 (let ((indent-stack (list nil))
982 ;; If ENDPOS is non-nil, use nil as STARTING-POINT
983 ;; so that calculate-lisp-indent will find the beginning of
984 ;; the defun we are in.
985 ;; If ENDPOS is nil, it is safe not to scan before point
986 ;; since every line we indent is more deeply nested than point is.
987 (starting-point (if endpos nil (point)))
989 last-depth bol outer-loop-done inner-loop-done state this-indent)
991 ;; Get error now if we don't have a complete sexp after point.
992 (save-excursion (forward-sexp 1)))
994 (setq outer-loop-done nil)
995 (while (if endpos (< (point) endpos)
996 (not outer-loop-done))
997 (setq last-depth next-depth
999 ;; Parse this line so we can learn the state
1000 ;; to indent the next line.
1001 ;; This inner loop goes through only once
1002 ;; unless a line ends inside a string.
1003 (while (and (not inner-loop-done)
1004 (not (setq outer-loop-done (eobp))))
1005 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
1007 (setq next-depth (car state))
1008 ;; If the line contains a comment other than the sort
1009 ;; that is indented like code,
1010 ;; indent it now with indent-for-comment.
1011 ;; Comments indented like code are right already.
1012 ;; In any case clear the in-comment flag in the state
1013 ;; because parse-partial-sexp never sees the newlines.
1014 (if (car (nthcdr 4 state))
1015 (progn (indent-for-comment)
1017 (setcar (nthcdr 4 state) nil)))
1018 ;; If this line ends inside a string,
1019 ;; go straight to next line, remaining within the inner loop,
1020 ;; and turn off the \-flag.
1021 (if (car (nthcdr 3 state))
1024 (setcar (nthcdr 5 state) nil))
1025 (setq inner-loop-done t)))
1029 (setq indent-stack (nconc indent-stack
1030 (make-list (- next-depth) nil))
1031 last-depth (- last-depth next-depth)
1033 (or outer-loop-done endpos
1034 (setq outer-loop-done (<= next-depth 0)))
1037 (while (> last-depth next-depth)
1038 (setq indent-stack (cdr indent-stack)
1039 last-depth (1- last-depth)))
1040 (while (< last-depth next-depth)
1041 (setq indent-stack (cons nil indent-stack)
1042 last-depth (1+ last-depth)))
1043 ;; Now go to the next line and indent it according
1044 ;; to what we learned from parsing the previous one.
1047 (skip-chars-forward " \t")
1048 ;; But not if the line is blank, or just a comment
1049 ;; (except for double-semi comments; indent them as usual).
1050 (if (or (eobp) (looking-at "\\s<\\|\n"))
1052 (if (and (car indent-stack)
1053 (>= (car indent-stack) 0))
1054 (setq this-indent (car indent-stack))
1055 (let ((val (calculate-lisp-indent
1056 (if (car indent-stack) (- (car indent-stack))
1059 (setq this-indent val)
1061 (setcar indent-stack
1062 (setq this-indent val))
1063 (setcar indent-stack (- (car (cdr val))))
1064 (setq this-indent (car val))))))
1065 (if (and this-indent (/= (current-column) this-indent))
1066 (progn (delete-region bol (point))
1067 (indent-to this-indent)))))
1069 (setq outer-loop-done (= (point) last-point))
1070 (setq last-point (point)))))))
1072 (defun lisp-indent-region (start end)
1073 "Indent every line whose first char is between START and END inclusive."
1075 (let ((endmark (copy-marker end)))
1077 (and (bolp) (not (eolp))
1079 (indent-sexp endmark)
1080 (set-marker endmark nil))))
1082 ;;;; Lisp paragraph filling commands.
1084 (defcustom emacs-lisp-docstring-fill-column 65
1085 "Value of `fill-column' to use when filling a docstring.
1086 Any non-integer value means do not use a different value of
1087 `fill-column' when filling docstrings."
1088 :type '(choice (integer)
1089 (const :tag "Use the current `fill-column'" t))
1092 (defun lisp-fill-paragraph (&optional justify)
1093 "Like \\[fill-paragraph], but handle Emacs Lisp comments and docstrings.
1094 If any of the current line is a comment, fill the comment or the
1095 paragraph of it that point is in, preserving the comment's indentation
1096 and initial semicolons."
1098 (or (fill-comment-paragraph justify)
1099 ;; Point is on a program line (a line no comment); we are interested
1100 ;; particularly in docstring lines.
1102 ;; We bind `paragraph-start' and `paragraph-separate' temporarily. They
1103 ;; are buffer-local, but we avoid changing them so that they can be set
1104 ;; to make `forward-paragraph' and friends do something the user wants.
1106 ;; `paragraph-start': The `(' in the character alternative and the
1107 ;; left-singlequote plus `(' sequence after the \\| alternative prevent
1108 ;; sexps and backquoted sexps that follow a docstring from being filled
1109 ;; with the docstring. This setting has the consequence of inhibiting
1110 ;; filling many program lines that are not docstrings, which is sensible,
1111 ;; because the user probably asked to fill program lines by accident, or
1112 ;; expecting indentation (perhaps we should try to do indenting in that
1113 ;; case). The `;' and `:' stop the paragraph being filled at following
1114 ;; comment lines and at keywords (e.g., in `defcustom'). Left parens are
1115 ;; escaped to keep font-locking, filling, & paren matching in the source
1118 ;; `paragraph-separate': A clever regexp distinguishes the first line of
1119 ;; a docstring and identifies it as a paragraph separator, so that it
1120 ;; won't be filled. (Since the first line of documentation stands alone
1121 ;; in some contexts, filling should not alter the contents the author has
1122 ;; chosen.) Only the first line of a docstring begins with whitespace
1123 ;; and a quotation mark and ends with a period or (rarely) a comma.
1125 ;; The `fill-column' is temporarily bound to
1126 ;; `emacs-lisp-docstring-fill-column' if that value is an integer.
1127 (let ((paragraph-start (concat paragraph-start
1128 "\\|\\s-*\\([\(;:\"]\\|`\(\\)"))
1130 (concat paragraph-separate "\\|\\s-*\".*[,\\.]$"))
1131 (fill-column (if (integerp emacs-lisp-docstring-fill-column)
1132 emacs-lisp-docstring-fill-column
1134 (fill-paragraph justify))
1135 ;; Never return nil.
1138 (defun indent-code-rigidly (start end arg &optional nochange-regexp)
1139 "Indent all lines of code, starting in the region, sideways by ARG columns.
1140 Does not affect lines starting inside comments or strings, assuming that
1141 the start of the region is not inside them.
1143 Called from a program, takes args START, END, COLUMNS and NOCHANGE-REGEXP.
1144 The last is a regexp which, if matched at the beginning of a line,
1145 means don't indent that line."
1146 (interactive "r\np")
1150 (setq end (point-marker))
1153 (setq state (parse-partial-sexp (point)
1155 (forward-line 1) (point))
1157 (while (< (point) end)
1158 (or (car (nthcdr 3 state))
1159 (and nochange-regexp
1160 (looking-at nochange-regexp))
1161 ;; If line does not start in string, indent it
1162 (let ((indent (current-indentation)))
1163 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
1165 (indent-to (max 0 (+ indent arg)) 0))))
1166 (setq state (parse-partial-sexp (point)
1168 (forward-line 1) (point))
1171 (provide 'lisp-mode)
1173 ;;; lisp-mode.el ends here