1 ;;; lisp-mode.el --- Lisp mode, and its idiosyncratic commands
3 ;; Copyright (C) 1985, 1986, 1999, 2000, 2001, 2003, 2004, 2005
4 ;; Free Software Foundation, Inc.
7 ;; Keywords: lisp, languages
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; The base major mode for editing Lisp code (used also for Emacs Lisp).
29 ;; This mode is documented in the Emacs manual.
33 (defvar lisp-mode-abbrev-table nil)
35 (defvar emacs-lisp-mode-syntax-table
36 (let ((table (make-syntax-table)))
39 (modify-syntax-entry i "_ " table)
43 (modify-syntax-entry i "_ " table)
47 (modify-syntax-entry i "_ " table)
51 (modify-syntax-entry i "_ " table)
53 (modify-syntax-entry ? " " table)
54 (modify-syntax-entry ?\t " " table)
55 (modify-syntax-entry ?\f " " table)
56 (modify-syntax-entry ?\n "> " table)
57 ;; Give CR the same syntax as newline, for selective-display.
58 (modify-syntax-entry ?\^m "> " table)
59 (modify-syntax-entry ?\; "< " table)
60 (modify-syntax-entry ?` "' " table)
61 (modify-syntax-entry ?' "' " table)
62 (modify-syntax-entry ?, "' " table)
63 (modify-syntax-entry ?@ "' " table)
64 ;; Used to be singlequote; changed for flonums.
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)
72 (modify-syntax-entry ?\] ")[ " table))
75 (defvar lisp-mode-syntax-table
76 (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table)))
77 (modify-syntax-entry ?\[ "_ " table)
78 (modify-syntax-entry ?\] "_ " table)
79 (modify-syntax-entry ?# "' 14bn" table)
80 (modify-syntax-entry ?| "\" 23b" table)
83 (define-abbrev-table 'lisp-mode-abbrev-table ())
85 (defvar lisp-imenu-generic-expression
88 (purecopy (concat "^\\s-*("
91 '("defun" "defun*" "defsubst" "defmacro"
92 "defadvice" "define-skeleton"
93 "define-minor-mode" "define-derived-mode"
95 "define-compiler-macro" "define-modify-macro"
96 "defsetf" "define-setf-expander"
97 "define-method-combination"
98 "defgeneric" "defmethod") t))
99 "\\s-+\\(\\sw\\(\\sw\\|\\s_\\)+\\)"))
101 (list (purecopy "Variables")
102 (purecopy (concat "^\\s-*("
105 '("defvar" "defconst" "defconstant" "defcustom"
106 "defparameter" "define-symbol-macro") t))
107 "\\s-+\\(\\sw\\(\\sw\\|\\s_\\)+\\)"))
109 (list (purecopy "Types")
110 (purecopy (concat "^\\s-*("
113 '("defgroup" "deftheme" "deftype" "defstruct"
114 "defclass" "define-condition" "define-widget"
115 "defface" "defpackage") t))
116 "\\s-+'?\\(\\sw\\(\\sw\\|\\s_\\)+\\)"))
119 "Imenu generic expression for Lisp mode. See `imenu-generic-expression'.")
121 ;; This was originally in autoload.el and is still used there.
122 (put 'autoload 'doc-string-elt 3)
123 (put 'defun 'doc-string-elt 3)
124 (put 'defun* 'doc-string-elt 3)
125 (put 'defvar 'doc-string-elt 3)
126 (put 'defcustom 'doc-string-elt 3)
127 (put 'deftheme 'doc-string-elt 2)
128 (put 'defconst 'doc-string-elt 3)
129 (put 'defmacro 'doc-string-elt 3)
130 (put 'defmacro* 'doc-string-elt 3)
131 (put 'defsubst 'doc-string-elt 3)
132 (put 'defstruct 'doc-string-elt 2)
133 (put 'define-skeleton 'doc-string-elt 2)
134 (put 'define-derived-mode 'doc-string-elt 4)
135 (put 'define-compilation-mode 'doc-string-elt 3)
136 (put 'easy-mmode-define-minor-mode 'doc-string-elt 2)
137 (put 'define-minor-mode 'doc-string-elt 2)
138 (put 'define-generic-mode 'doc-string-elt 7)
139 ;; define-global-mode has no explicit docstring.
140 (put 'easy-mmode-define-global-mode 'doc-string-elt 0)
141 (put 'define-ibuffer-filter 'doc-string-elt 2)
142 (put 'define-ibuffer-op 'doc-string-elt 3)
143 (put 'define-ibuffer-sorter 'doc-string-elt 2)
145 (defun lisp-font-lock-syntactic-face-function (state)
147 (if (and (eq (nth 0 state) 1)
148 ;; This might be a docstring.
151 (goto-char (nth 8 state))
153 (while (and (not (bobp))
154 (progn (backward-sexp 1) (setq n (1+ n)))))
157 (let ((sym (intern-soft
159 (point) (progn (forward-sexp 1) (point))))))
160 (eq n (or (get sym 'doc-string-elt) 3)))))))
162 font-lock-string-face)
163 font-lock-comment-face))
165 ;; The LISP-SYNTAX argument is used by code in inf-lisp.el and is
166 ;; (uselessly) passed from pp.el, chistory.el, gnus-kill.el and score-mode.el
167 (defun lisp-mode-variables (&optional lisp-syntax)
169 (set-syntax-table lisp-mode-syntax-table))
170 (setq local-abbrev-table lisp-mode-abbrev-table)
171 (make-local-variable 'paragraph-ignore-fill-prefix)
172 (setq paragraph-ignore-fill-prefix t)
173 (make-local-variable 'fill-paragraph-function)
174 (setq fill-paragraph-function 'lisp-fill-paragraph)
175 ;; Adaptive fill mode gets in the way of auto-fill,
176 ;; and should make no difference for explicit fill
177 ;; because lisp-fill-paragraph should do the job.
178 ;; I believe that newcomment's auto-fill code properly deals with it -stef
179 ;;(set (make-local-variable 'adaptive-fill-mode) nil)
180 (make-local-variable 'indent-line-function)
181 (setq indent-line-function 'lisp-indent-line)
182 (make-local-variable 'indent-region-function)
183 (setq indent-region-function 'lisp-indent-region)
184 (make-local-variable 'parse-sexp-ignore-comments)
185 (setq parse-sexp-ignore-comments t)
186 (make-local-variable 'outline-regexp)
187 (setq outline-regexp ";;;\\(;* [^ \t\n]\\|###autoload\\)\\|(")
188 (make-local-variable 'outline-level)
189 (setq outline-level 'lisp-outline-level)
190 (make-local-variable 'comment-start)
191 (setq comment-start ";")
192 (make-local-variable 'comment-start-skip)
193 ;; Look within the line for a ; following an even number of backslashes
194 ;; after either a non-backslash or the line beginning.
195 (setq comment-start-skip "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\);+ *")
196 (make-local-variable 'font-lock-comment-start-skip)
197 ;; Font lock mode uses this only when it KNOWS a comment is starting.
198 (setq font-lock-comment-start-skip ";+ *")
199 (make-local-variable 'comment-add)
200 (setq comment-add 1) ;default to `;;' in comment-region
201 (make-local-variable 'comment-column)
202 (setq comment-column 40)
203 ;; Don't get confused by `;' in doc strings when paragraph-filling.
204 (set (make-local-variable 'comment-use-global-state) t)
205 (make-local-variable 'comment-indent-function)
206 (setq comment-indent-function 'lisp-comment-indent)
207 (make-local-variable 'imenu-generic-expression)
208 (setq imenu-generic-expression lisp-imenu-generic-expression)
209 (make-local-variable 'multibyte-syntax-as-symbol)
210 (setq multibyte-syntax-as-symbol t)
211 (set (make-local-variable 'syntax-begin-function) 'beginning-of-defun)
212 (setq font-lock-defaults
213 '((lisp-font-lock-keywords
214 lisp-font-lock-keywords-1 lisp-font-lock-keywords-2)
215 nil nil (("+-*/.<>=!?$%_&~^:@" . "w")) nil
216 (font-lock-mark-block-function . mark-defun)
217 (font-lock-syntactic-face-function
218 . lisp-font-lock-syntactic-face-function))))
220 (defun lisp-outline-level ()
221 "Lisp mode `outline-level' function."
222 (let ((len (- (match-end 0) (match-beginning 0))))
223 (if (looking-at "(\\|;;;###autoload")
227 (defvar lisp-mode-shared-map
228 (let ((map (make-sparse-keymap)))
229 (define-key map "\t" 'lisp-indent-line)
230 (define-key map "\e\C-q" 'indent-sexp)
231 (define-key map "\177" 'backward-delete-char-untabify)
232 ;; This gets in the way when viewing a Lisp file in view-mode. As
233 ;; long as [backspace] is mapped into DEL via the
234 ;; function-key-map, this should remain disabled!!
235 ;;;(define-key map [backspace] 'backward-delete-char-untabify)
237 "Keymap for commands shared by all sorts of Lisp modes.")
239 (defvar emacs-lisp-mode-map ()
240 "Keymap for Emacs Lisp mode.
241 All commands in `lisp-mode-shared-map' are inherited by this map.")
243 (if emacs-lisp-mode-map
245 (let ((map (make-sparse-keymap "Emacs-Lisp")))
246 (setq emacs-lisp-mode-map (make-sparse-keymap))
247 (set-keymap-parent emacs-lisp-mode-map lisp-mode-shared-map)
248 (define-key emacs-lisp-mode-map "\e\t" 'lisp-complete-symbol)
249 (define-key emacs-lisp-mode-map "\e\C-x" 'eval-defun)
250 (define-key emacs-lisp-mode-map "\e\C-q" 'indent-pp-sexp)
251 (define-key emacs-lisp-mode-map [menu-bar] (make-sparse-keymap))
252 (define-key emacs-lisp-mode-map [menu-bar emacs-lisp]
253 (cons "Emacs-Lisp" map))
254 (define-key map [edebug-defun]
255 '("Instrument Function for Debugging" . edebug-defun))
256 (define-key map [byte-recompile]
257 '("Byte-recompile Directory..." . byte-recompile-directory))
258 (define-key map [emacs-byte-compile-and-load]
259 '("Byte-compile And Load" . emacs-lisp-byte-compile-and-load))
260 (define-key map [byte-compile]
261 '("Byte-compile This File" . emacs-lisp-byte-compile))
262 (define-key map [separator-eval] '("--"))
263 (define-key map [eval-buffer] '("Evaluate Buffer" . eval-current-buffer))
264 (define-key map [eval-region] '("Evaluate Region" . eval-region))
265 (define-key map [eval-sexp] '("Evaluate Last S-expression" . eval-last-sexp))
266 (define-key map [separator-format] '("--"))
267 (define-key map [comment-region] '("Comment Out Region" . comment-region))
268 (define-key map [indent-region] '("Indent Region" . indent-region))
269 (define-key map [indent-line] '("Indent Line" . lisp-indent-line))
270 (put 'eval-region 'menu-enable 'mark-active)
271 (put 'comment-region 'menu-enable 'mark-active)
272 (put 'indent-region 'menu-enable 'mark-active)))
274 (defun emacs-lisp-byte-compile ()
275 "Byte compile the file containing the current buffer."
278 (byte-compile-file buffer-file-name)
279 (error "The buffer must be saved in a file first")))
281 (defun emacs-lisp-byte-compile-and-load ()
282 "Byte-compile the current file (if it has changed), then load compiled code."
285 (error "The buffer must be saved in a file first"))
287 ;; Recompile if file or buffer has changed since last compilation.
288 (if (and (buffer-modified-p)
289 (y-or-n-p (format "Save buffer %s first? " (buffer-name))))
291 (let ((compiled-file-name (byte-compile-dest-file buffer-file-name)))
292 (if (file-newer-than-file-p compiled-file-name buffer-file-name)
293 (load-file compiled-file-name)
294 (byte-compile-file buffer-file-name t))))
296 (defcustom emacs-lisp-mode-hook nil
297 "Hook run when entering Emacs Lisp mode."
298 :options '(turn-on-eldoc-mode imenu-add-menubar-index checkdoc-minor-mode)
302 (defcustom lisp-mode-hook nil
303 "Hook run when entering Lisp mode."
304 :options '(imenu-add-menubar-index)
308 (defcustom lisp-interaction-mode-hook nil
309 "Hook run when entering Lisp Interaction mode."
310 :options '(turn-on-eldoc-mode)
314 (defun emacs-lisp-mode ()
315 "Major mode for editing Lisp code to run in Emacs.
317 Delete converts tabs to spaces as it moves back.
318 Blank lines separate paragraphs. Semicolons start comments.
319 \\{emacs-lisp-mode-map}
320 Entry to this mode calls the value of `emacs-lisp-mode-hook'
321 if that value is non-nil."
323 (kill-all-local-variables)
324 (use-local-map emacs-lisp-mode-map)
325 (set-syntax-table emacs-lisp-mode-syntax-table)
326 (setq major-mode 'emacs-lisp-mode)
327 (setq mode-name "Emacs-Lisp")
328 (lisp-mode-variables)
329 (setq imenu-case-fold-search nil)
330 (run-mode-hooks 'emacs-lisp-mode-hook))
331 (put 'emacs-lisp-mode 'custom-mode-group 'lisp)
333 (defvar lisp-mode-map
334 (let ((map (make-sparse-keymap)))
335 (set-keymap-parent map lisp-mode-shared-map)
336 (define-key map "\e\C-x" 'lisp-eval-defun)
337 (define-key map "\C-c\C-z" 'run-lisp)
339 "Keymap for ordinary Lisp mode.
340 All commands in `lisp-mode-shared-map' are inherited by this map.")
343 "Major mode for editing Lisp code for Lisps other than GNU Emacs Lisp.
345 Delete converts tabs to spaces as it moves back.
346 Blank lines separate paragraphs. Semicolons start comments.
348 Note that `run-lisp' may be used either to start an inferior Lisp job
349 or to switch back to an existing one.
351 Entry to this mode calls the value of `lisp-mode-hook'
352 if that value is non-nil."
354 (kill-all-local-variables)
355 (use-local-map lisp-mode-map)
356 (setq major-mode 'lisp-mode)
357 (setq mode-name "Lisp")
358 (lisp-mode-variables)
359 (make-local-variable 'comment-start-skip)
360 (setq comment-start-skip
361 "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\)\\(;+\\|#|\\) *")
362 (make-local-variable 'font-lock-keywords-case-fold-search)
363 (setq font-lock-keywords-case-fold-search t)
364 (setq imenu-case-fold-search t)
365 (set-syntax-table lisp-mode-syntax-table)
366 (run-mode-hooks 'lisp-mode-hook))
367 (put 'lisp-mode 'find-tag-default-function 'lisp-find-tag-default)
369 (defun lisp-find-tag-default ()
370 (let ((default (find-tag-default)))
371 (when (stringp default)
372 (if (string-match ":+" default)
373 (substring default (match-end 0))
376 ;; Used in old LispM code.
377 (defalias 'common-lisp-mode 'lisp-mode)
379 ;; This will do unless inf-lisp.el is loaded.
380 (defun lisp-eval-defun (&optional and-go)
381 "Send the current defun to the Lisp process made by \\[run-lisp]."
383 (error "Process lisp does not exist"))
385 (defvar lisp-interaction-mode-map
386 (let ((map (make-sparse-keymap)))
387 (set-keymap-parent map lisp-mode-shared-map)
388 (define-key map "\e\C-x" 'eval-defun)
389 (define-key map "\e\C-q" 'indent-pp-sexp)
390 (define-key map "\e\t" 'lisp-complete-symbol)
391 (define-key map "\n" 'eval-print-last-sexp)
393 "Keymap for Lisp Interaction mode.
394 All commands in `lisp-mode-shared-map' are inherited by this map.")
396 (defvar lisp-interaction-mode-abbrev-table lisp-mode-abbrev-table)
397 (define-derived-mode lisp-interaction-mode emacs-lisp-mode "Lisp Interaction"
398 "Major mode for typing and evaluating Lisp forms.
399 Like Lisp mode except that \\[eval-print-last-sexp] evals the Lisp expression
400 before point, and prints its value into the buffer, advancing point.
401 Note that printing is controlled by `eval-expression-print-length'
402 and `eval-expression-print-level'.
405 Delete converts tabs to spaces as it moves back.
406 Paragraphs are separated only by blank lines.
407 Semicolons start comments.
408 \\{lisp-interaction-mode-map}
409 Entry to this mode calls the value of `lisp-interaction-mode-hook'
410 if that value is non-nil.")
412 (defun eval-print-last-sexp ()
413 "Evaluate sexp before point; print value into current buffer.
415 Note that printing the result is controlled by the variables
416 `eval-expression-print-length' and `eval-expression-print-level',
419 (let ((standard-output (current-buffer)))
425 (defun last-sexp-setup-props (beg end value alt1 alt2)
426 "Set up text properties for the output of `eval-last-sexp-1'.
427 BEG and END are the start and end of the output in current-buffer.
428 VALUE is the Lisp value printed, ALT1 and ALT2 are strings for the
429 alternative printed representations that can be displayed."
430 (let ((map (make-sparse-keymap)))
431 (define-key map "\C-m" 'last-sexp-toggle-display)
432 (define-key map [down-mouse-2] 'mouse-set-point)
433 (define-key map [mouse-2] 'last-sexp-toggle-display)
436 `(printed-value (,value ,alt1 ,alt2)
439 help-echo "RET, mouse-2: toggle abbreviated display"
440 rear-nonsticky (mouse-face keymap help-echo
444 (defun last-sexp-toggle-display (&optional arg)
445 "Toggle between abbreviated and unabbreviated printed representations."
449 (let ((value (get-text-property (point) 'printed-value)))
451 (let ((beg (or (previous-single-property-change (min (point-max) (1+ (point)))
454 (end (or (next-single-char-property-change (point) 'printed-value) (point)))
455 (standard-output (current-buffer))
457 (delete-region beg end)
458 (insert (nth 1 value))
459 (last-sexp-setup-props beg (point)
463 (goto-char (min (point-max) point)))))))
465 (defun prin1-char (char)
466 "Return a string representing CHAR as a character rather than as an integer.
467 If CHAR is not a character, return nil."
470 (let ((c (event-basic-type char))
471 (mods (event-modifiers char))
473 ;; Prevent ?A from turning into ?\S-a.
474 (if (and (memq 'shift mods)
475 (zerop (logand char ?\S-\^@))
476 (not (let ((case-fold-search nil))
477 (char-equal c (upcase c)))))
478 (setq c (upcase c) mods nil))
479 ;; What string are we considering using?
486 (cond ((eq modif 'super) "\\s-")
487 (t (string ?\\ (upcase (aref (symbol-name modif) 0)) ?-))))
490 ((memq c '(?\; ?\( ?\) ?\{ ?\} ?\[ ?\] ?\" ?\' ?\\)) (string ?\\ c))
495 ;; Verify the string reads a CHAR, not to some other character.
496 ;; If it doesn't, return nil instead.
498 (= (car (read-from-string string)) char)
502 (defun eval-last-sexp-1 (eval-last-sexp-arg-internal)
503 "Evaluate sexp before point; print value in minibuffer.
504 With argument, print output into current buffer."
505 (let ((standard-output (if eval-last-sexp-arg-internal (current-buffer) t)))
507 (eval (let ((stab (syntax-table))
512 (with-syntax-table emacs-lisp-mode-syntax-table
513 ;; If this sexp appears to be enclosed in `...'
514 ;; then ignore the surrounding quotes.
516 (or (eq (following-char) ?\')
517 (eq (preceding-char) ?\')))
519 ;; If we were after `?\e' (or similar case),
520 ;; use the whole thing, not just the `e'.
521 (when (eq (preceding-char) ?\\)
523 (when (eq (preceding-char) ??)
527 (when (eq (preceding-char) ?=)
530 (skip-chars-backward "0-9#=")
531 (setq labeled-p (looking-at "\\(#[0-9]+=\\)+")))
536 ;; vladimir@cs.ualberta.ca 30-Jul-1997: skip ` in
537 ;; `variable' so that the value is returned, not the
539 (if (and ignore-quotes
540 (eq (following-char) ?`))
542 (narrow-to-region (point-min) opoint)
543 (setq expr (read (current-buffer)))
544 ;; If it's an (interactive ...) form, it's more
545 ;; useful to show how an interactive call would
548 (eq (car expr) 'interactive)
550 (list 'call-interactively
557 (eval-last-sexp-print-value value))))
559 (defun eval-last-sexp-print-value (value)
560 (let ((unabbreviated (let ((print-length nil) (print-level nil))
561 (prin1-to-string value)))
562 (print-length eval-expression-print-length)
563 (print-level eval-expression-print-level)
568 (let ((str (eval-expression-print-format value)))
569 (if str (princ str)))
571 (when (and (bufferp standard-output)
572 (or (not (null print-length))
573 (not (null print-level)))
574 (not (string= unabbreviated
575 (buffer-substring-no-properties beg end))))
576 (last-sexp-setup-props beg end value
578 (buffer-substring-no-properties beg end))
582 (defvar eval-last-sexp-fake-value (make-symbol "t"))
584 (defun eval-last-sexp (eval-last-sexp-arg-internal)
585 "Evaluate sexp before point; print value in minibuffer.
586 Interactively, with prefix argument, print output into current buffer."
588 (if (null eval-expression-debug-on-error)
589 (eval-last-sexp-1 eval-last-sexp-arg-internal)
590 (let ((old-value eval-last-sexp-fake-value) new-value value)
591 (let ((debug-on-error old-value))
592 (setq value (eval-last-sexp-1 eval-last-sexp-arg-internal))
593 (setq new-value debug-on-error))
594 (unless (eq old-value new-value)
595 (setq debug-on-error new-value))
598 (defun eval-defun-1 (form)
599 "Treat some expressions specially.
600 Reset the `defvar' and `defcustom' variables to the initial value.
601 Reinitialize the face according to the `defface' specification."
602 ;; The code in edebug-defun should be consistent with this, but not
603 ;; the same, since this gets a macroexpended form.
604 (cond ((not (listp form))
606 ((and (eq (car form) 'defvar)
607 (cdr-safe (cdr-safe form))
608 (boundp (cadr form)))
609 ;; Force variable to be re-set.
610 `(progn (defvar ,(nth 1 form) nil ,@(nthcdr 3 form))
611 (setq-default ,(nth 1 form) ,(nth 2 form))))
612 ;; `defcustom' is now macroexpanded to
613 ;; `custom-declare-variable' with a quoted value arg.
614 ((and (eq (car form) 'custom-declare-variable)
615 (default-boundp (eval (nth 1 form))))
616 ;; Force variable to be bound.
617 (set-default (eval (nth 1 form)) (eval (nth 1 (nth 2 form))))
619 ;; `defface' is macroexpanded to `custom-declare-face'.
620 ((eq (car form) 'custom-declare-face)
622 (setq face-new-frame-defaults
623 (assq-delete-all (eval (nth 1 form)) face-new-frame-defaults))
624 (put (eval (nth 1 form)) 'face-defface-spec nil)
625 ;; Setting `customized-face' to the new spec after calling
626 ;; the form, but preserving the old saved spec in `saved-face',
627 ;; imitates the situation when the new face spec is set
628 ;; temporarily for the current session in the customize
629 ;; buffer, thus allowing `face-user-default-spec' to use the
630 ;; new customized spec instead of the saved spec.
631 ;; Resetting `saved-face' temporarily to nil is needed to let
632 ;; `defface' change the spec, regardless of a saved spec.
634 (put ,(nth 1 form) 'saved-face
635 ',(get (eval (nth 1 form)) 'saved-face))
636 (put ,(nth 1 form) 'customized-face
638 (put (eval (nth 1 form)) 'saved-face nil)))
639 ((eq (car form) 'progn)
640 (cons 'progn (mapcar 'eval-defun-1 (cdr form))))
643 (defun eval-defun-2 ()
644 "Evaluate defun that point is in or before.
645 The value is displayed in the minibuffer.
646 If the current defun is actually a call to `defvar',
647 then reset the variable using the initial value expression
648 even if the variable already has some other value.
649 \(Normally `defvar' does not change the variable's value
650 if it already has a value.\)
652 With argument, insert value in current buffer after the defun.
653 Return the result of evaluation."
655 (let ((debug-on-error eval-expression-debug-on-error)
656 (print-length eval-expression-print-length)
657 (print-level eval-expression-print-level))
659 ;; Arrange for eval-region to "read" the (possibly) altered form.
660 ;; eval-region handles recording which file defines a function or
661 ;; variable. Re-written using `apply' to avoid capturing
662 ;; variables like `end'.
665 (let ((standard-output t)
667 ;; Read the form from the buffer, and record where it ends.
672 (setq form (read (current-buffer)))
674 ;; Alter the form if necessary.
675 (setq form (eval-defun-1 (macroexpand form)))
676 (list beg end standard-output
678 ;; Skipping to the end of the specified region
679 ;; will make eval-region return.
682 ;; The result of evaluation has been put onto VALUES. So return it.
685 (defun eval-defun (edebug-it)
686 "Evaluate the top-level form containing point, or after point.
688 If the current defun is actually a call to `defvar' or `defcustom',
689 evaluating it this way resets the variable using its initial value
690 expression even if the variable already has some other value.
691 \(Normally `defvar' and `defcustom' do not alter the value if there
694 With a prefix argument, instrument the code for Edebug.
696 If acting on a `defun' for FUNCTION, and the function was
697 instrumented, `Edebug: FUNCTION' is printed in the minibuffer. If not
698 instrumented, just FUNCTION is printed.
700 If not acting on a `defun', the result of evaluation is displayed in
701 the minibuffer. This display is controlled by the variables
702 `eval-expression-print-length' and `eval-expression-print-level',
707 (eval-defun (not edebug-all-defs)))
709 (if (null eval-expression-debug-on-error)
711 (let ((old-value (make-symbol "t")) new-value value)
712 (let ((debug-on-error old-value))
713 (setq value (eval-defun-2))
714 (setq new-value debug-on-error))
715 (unless (eq old-value new-value)
716 (setq debug-on-error new-value))
719 ;; Used for comment-indent-function in Lisp modes.
720 (defun lisp-comment-indent ()
721 (if (looking-at "\\s<\\s<\\s<")
723 (if (looking-at "\\s<\\s<")
724 (let ((tem (or (calculate-lisp-indent) (current-column))))
725 (if (listp tem) (car tem) tem))
726 (skip-chars-backward " \t")
727 (max (if (bolp) 0 (1+ (current-column)))
730 ;; This function just forces a more costly detection of comments (using
731 ;; parse-partial-sexp from beginning-of-defun). I.e. It avoids the problem of
732 ;; taking a `;' inside a string started on another line for a comment starter.
733 ;; Note: `newcomment' gets it right now since we set comment-use-global-state
734 ;; so we could get rid of it. -stef
735 (defun lisp-mode-auto-fill ()
736 (if (> (current-column) (current-fill-column))
738 (nth 4 (syntax-ppss (point))))
740 (unless (and (boundp 'comment-auto-fill-only-comments)
741 comment-auto-fill-only-comments)
742 (let ((comment-start nil) (comment-start-skip nil))
745 (defvar lisp-indent-offset nil
746 "If non-nil, indent second line of expressions that many more columns.")
747 (defvar lisp-indent-function 'lisp-indent-function)
749 (defun lisp-indent-line (&optional whole-exp)
750 "Indent current line as Lisp code.
751 With argument, indent any additional lines of the same expression
752 rigidly along with this one."
754 (let ((indent (calculate-lisp-indent)) shift-amt end
755 (pos (- (point-max) (point)))
756 (beg (progn (beginning-of-line) (point))))
757 (skip-chars-forward " \t")
758 (if (or (null indent) (looking-at "\\s<\\s<\\s<"))
759 ;; Don't alter indentation of a ;;; comment line
760 ;; or a line that starts in a string.
761 (goto-char (- (point-max) pos))
762 (if (and (looking-at "\\s<") (not (looking-at "\\s<\\s<")))
763 ;; Single-semicolon comment lines should be indented
764 ;; as comment lines, not as code.
765 (progn (indent-for-comment) (forward-char -1))
766 (if (listp indent) (setq indent (car indent)))
767 (setq shift-amt (- indent (current-column)))
768 (if (zerop shift-amt)
770 (delete-region beg (point))
772 ;; If initial point was within line's indentation,
773 ;; position after the indentation. Else stay at same point in text.
774 (if (> (- (point-max) pos) (point))
775 (goto-char (- (point-max) pos)))
776 ;; If desired, shift remaining lines of expression the same amount.
777 (and whole-exp (not (zerop shift-amt))
786 (indent-code-rigidly beg end shift-amt)))))
788 (defvar calculate-lisp-indent-last-sexp)
790 (defun calculate-lisp-indent (&optional parse-start)
791 "Return appropriate indentation for current line as Lisp code.
792 In usual case returns an integer: the column to indent to.
793 If the value is nil, that means don't change the indentation
794 because the line starts inside a string.
796 The value can also be a list of the form (COLUMN CONTAINING-SEXP-START).
797 This means that following lines at the same level of indentation
798 should not necessarily be indented the same as this line.
799 Then COLUMN is the column to indent to, and CONTAINING-SEXP-START
800 is the buffer position of the start of the containing expression."
803 (let ((indent-point (point))
805 ;; setting this to a number inhibits calling hook
808 calculate-lisp-indent-last-sexp containing-sexp)
810 (goto-char parse-start)
811 (beginning-of-defun))
812 ;; Find outermost containing sexp
813 (while (< (point) indent-point)
814 (setq state (parse-partial-sexp (point) indent-point 0)))
815 ;; Find innermost containing sexp
818 (> (setq paren-depth (elt state 0)) 0))
820 (setq calculate-lisp-indent-last-sexp (elt state 2))
821 (setq containing-sexp (elt state 1))
822 ;; Position following last unclosed open.
823 (goto-char (1+ containing-sexp))
824 ;; Is there a complete sexp since then?
825 (if (and calculate-lisp-indent-last-sexp
826 (> calculate-lisp-indent-last-sexp (point)))
827 ;; Yes, but is there a containing sexp after that?
828 (let ((peek (parse-partial-sexp calculate-lisp-indent-last-sexp
830 (if (setq retry (car (cdr peek))) (setq state peek)))))
833 ;; Innermost containing sexp found
834 (goto-char (1+ containing-sexp))
835 (if (not calculate-lisp-indent-last-sexp)
836 ;; indent-point immediately follows open paren.
838 (setq desired-indent (current-column))
839 ;; Find the start of first element of containing sexp.
840 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
841 (cond ((looking-at "\\s(")
842 ;; First element of containing sexp is a list.
843 ;; Indent under that list.
845 ((> (save-excursion (forward-line 1) (point))
846 calculate-lisp-indent-last-sexp)
847 ;; This is the first line to start within the containing sexp.
848 ;; It's almost certainly a function call.
849 (if (= (point) calculate-lisp-indent-last-sexp)
850 ;; Containing sexp has nothing before this line
851 ;; except the first element. Indent under that element.
853 ;; Skip the first element, find start of second (the first
854 ;; argument of the function call) and indent under.
855 (progn (forward-sexp 1)
856 (parse-partial-sexp (point)
857 calculate-lisp-indent-last-sexp
859 (backward-prefix-chars))
861 ;; Indent beneath first sexp on same line as
862 ;; `calculate-lisp-indent-last-sexp'. Again, it's
863 ;; almost certainly a function call.
864 (goto-char calculate-lisp-indent-last-sexp)
866 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp
868 (backward-prefix-chars)))))
869 ;; Point is at the point to indent under unless we are inside a string.
870 ;; Call indentation hook except when overridden by lisp-indent-offset
871 ;; or if the desired indentation has already been computed.
872 (let ((normal-indent (current-column)))
874 ;; Inside a string, don't change indentation.
876 ((and (integerp lisp-indent-offset) containing-sexp)
877 ;; Indent by constant offset
878 (goto-char containing-sexp)
879 (+ (current-column) lisp-indent-offset))
881 ((and (boundp 'lisp-indent-function)
884 (or (funcall lisp-indent-function indent-point state)
889 (defun lisp-indent-function (indent-point state)
890 "This function is the normal value of the variable `lisp-indent-function'.
891 It is used when indenting a line within a function call, to see if the
892 called function says anything special about how to indent the line.
894 INDENT-POINT is the position where the user typed TAB, or equivalent.
895 Point is located at the point to indent under (for default indentation);
896 STATE is the `parse-partial-sexp' state for that position.
898 If the current line is in a call to a Lisp function
899 which has a non-nil property `lisp-indent-function',
900 that specifies how to do the indentation. The property value can be
901 * `defun', meaning indent `defun'-style;
902 * an integer N, meaning indent the first N arguments specially
903 like ordinary function arguments and then indent any further
904 arguments like a body;
905 * a function to call just as this function was called.
906 If that function returns nil, that means it doesn't specify
909 This function also returns nil meaning don't specify the indentation."
910 (let ((normal-indent (current-column)))
911 (goto-char (1+ (elt state 1)))
912 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
913 (if (and (elt state 2)
914 (not (looking-at "\\sw\\|\\s_")))
915 ;; car of form doesn't seem to be a symbol
917 (if (not (> (save-excursion (forward-line 1) (point))
918 calculate-lisp-indent-last-sexp))
919 (progn (goto-char calculate-lisp-indent-last-sexp)
921 (parse-partial-sexp (point)
922 calculate-lisp-indent-last-sexp 0 t)))
923 ;; Indent under the list or under the first sexp on the same
924 ;; line as calculate-lisp-indent-last-sexp. Note that first
925 ;; thing on that line has to be complete sexp since we are
926 ;; inside the innermost containing sexp.
927 (backward-prefix-chars)
929 (let ((function (buffer-substring (point)
930 (progn (forward-sexp 1) (point))))
932 (setq method (or (get (intern-soft function) 'lisp-indent-function)
933 (get (intern-soft function) 'lisp-indent-hook)))
934 (cond ((or (eq method 'defun)
936 (> (length function) 3)
937 (string-match "\\`def" function)))
938 (lisp-indent-defform state indent-point))
940 (lisp-indent-specform method state
941 indent-point normal-indent))
943 (funcall method indent-point state)))))))
945 (defvar lisp-body-indent 2
946 "Number of columns to indent the second line of a `(def...)' form.")
948 (defun lisp-indent-specform (count state indent-point normal-indent)
949 (let ((containing-form-start (elt state 1))
951 body-indent containing-form-column)
952 ;; Move to the start of containing form, calculate indentation
953 ;; to use for non-distinguished forms (> count), and move past the
954 ;; function symbol. lisp-indent-function guarantees that there is at
955 ;; least one word or symbol character following open paren of containing
957 (goto-char containing-form-start)
958 (setq containing-form-column (current-column))
959 (setq body-indent (+ lisp-body-indent containing-form-column))
962 ;; Now find the start of the last form.
963 (parse-partial-sexp (point) indent-point 1 t)
964 (while (and (< (point) indent-point)
967 (setq count (1- count))
969 (parse-partial-sexp (point) indent-point 1 t))
971 ;; Point is sitting on first character of last (or count) sexp.
973 ;; A distinguished form. If it is the first or second form use double
974 ;; lisp-body-indent, else normal indent. With lisp-body-indent bound
975 ;; to 2 (the default), this just happens to work the same with if as
976 ;; the older code, but it makes unwind-protect, condition-case,
977 ;; with-output-to-temp-buffer, et. al. much more tasteful. The older,
978 ;; less hacked, behavior can be obtained by replacing below with
979 ;; (list normal-indent containing-form-start).
980 (if (<= (- i count) 1)
981 (list (+ containing-form-column (* 2 lisp-body-indent))
982 containing-form-start)
983 (list normal-indent containing-form-start))
984 ;; A non-distinguished form. Use body-indent if there are no
985 ;; distinguished forms and this is the first undistinguished form,
986 ;; or if this is the first undistinguished form and the preceding
987 ;; distinguished form has indentation at least as great as body-indent.
988 (if (or (and (= i 0) (= count 0))
989 (and (= count 0) (<= body-indent normal-indent)))
993 (defun lisp-indent-defform (state indent-point)
994 (goto-char (car (cdr state)))
996 (if (> (point) (car (cdr (cdr state))))
998 (goto-char (car (cdr state)))
999 (+ lisp-body-indent (current-column)))))
1002 ;; (put 'progn 'lisp-indent-function 0), say, causes progn to be indented
1003 ;; like defun if the first form is placed on the next line, otherwise
1004 ;; it is indented like any other form (i.e. forms line up under first).
1006 (put 'lambda 'lisp-indent-function 'defun)
1007 (put 'autoload 'lisp-indent-function 'defun)
1008 (put 'progn 'lisp-indent-function 0)
1009 (put 'prog1 'lisp-indent-function 1)
1010 (put 'prog2 'lisp-indent-function 2)
1011 (put 'save-excursion 'lisp-indent-function 0)
1012 (put 'save-window-excursion 'lisp-indent-function 0)
1013 (put 'save-selected-window 'lisp-indent-function 0)
1014 (put 'save-restriction 'lisp-indent-function 0)
1015 (put 'save-match-data 'lisp-indent-function 0)
1016 (put 'save-current-buffer 'lisp-indent-function 0)
1017 (put 'with-current-buffer 'lisp-indent-function 1)
1018 (put 'combine-after-change-calls 'lisp-indent-function 0)
1019 (put 'with-output-to-string 'lisp-indent-function 0)
1020 (put 'with-temp-file 'lisp-indent-function 1)
1021 (put 'with-temp-buffer 'lisp-indent-function 0)
1022 (put 'with-temp-message 'lisp-indent-function 1)
1023 (put 'with-syntax-table 'lisp-indent-function 1)
1024 (put 'let 'lisp-indent-function 1)
1025 (put 'let* 'lisp-indent-function 1)
1026 (put 'while 'lisp-indent-function 1)
1027 (put 'if 'lisp-indent-function 2)
1028 (put 'read-if 'lisp-indent-function 2)
1029 (put 'catch 'lisp-indent-function 1)
1030 (put 'condition-case 'lisp-indent-function 2)
1031 (put 'unwind-protect 'lisp-indent-function 1)
1032 (put 'with-output-to-temp-buffer 'lisp-indent-function 1)
1033 (put 'eval-after-load 'lisp-indent-function 1)
1034 (put 'dolist 'lisp-indent-function 1)
1035 (put 'dotimes 'lisp-indent-function 1)
1036 (put 'when 'lisp-indent-function 1)
1037 (put 'unless 'lisp-indent-function 1)
1039 (defun indent-sexp (&optional endpos)
1040 "Indent each line of the list starting just after point.
1041 If optional arg ENDPOS is given, indent each line, stopping when
1042 ENDPOS is encountered."
1044 (let ((indent-stack (list nil))
1046 ;; If ENDPOS is non-nil, use nil as STARTING-POINT
1047 ;; so that calculate-lisp-indent will find the beginning of
1048 ;; the defun we are in.
1049 ;; If ENDPOS is nil, it is safe not to scan before point
1050 ;; since every line we indent is more deeply nested than point is.
1051 (starting-point (if endpos nil (point)))
1052 (last-point (point))
1053 last-depth bol outer-loop-done inner-loop-done state this-indent)
1055 ;; Get error now if we don't have a complete sexp after point.
1056 (save-excursion (forward-sexp 1)))
1058 (setq outer-loop-done nil)
1059 (while (if endpos (< (point) endpos)
1060 (not outer-loop-done))
1061 (setq last-depth next-depth
1062 inner-loop-done nil)
1063 ;; Parse this line so we can learn the state
1064 ;; to indent the next line.
1065 ;; This inner loop goes through only once
1066 ;; unless a line ends inside a string.
1067 (while (and (not inner-loop-done)
1068 (not (setq outer-loop-done (eobp))))
1069 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
1071 (setq next-depth (car state))
1072 ;; If the line contains a comment other than the sort
1073 ;; that is indented like code,
1074 ;; indent it now with indent-for-comment.
1075 ;; Comments indented like code are right already.
1076 ;; In any case clear the in-comment flag in the state
1077 ;; because parse-partial-sexp never sees the newlines.
1078 (if (car (nthcdr 4 state))
1079 (progn (indent-for-comment)
1081 (setcar (nthcdr 4 state) nil)))
1082 ;; If this line ends inside a string,
1083 ;; go straight to next line, remaining within the inner loop,
1084 ;; and turn off the \-flag.
1085 (if (car (nthcdr 3 state))
1088 (setcar (nthcdr 5 state) nil))
1089 (setq inner-loop-done t)))
1093 (setq indent-stack (nconc indent-stack
1094 (make-list (- next-depth) nil))
1095 last-depth (- last-depth next-depth)
1097 (or outer-loop-done endpos
1098 (setq outer-loop-done (<= next-depth 0)))
1101 (while (> last-depth next-depth)
1102 (setq indent-stack (cdr indent-stack)
1103 last-depth (1- last-depth)))
1104 (while (< last-depth next-depth)
1105 (setq indent-stack (cons nil indent-stack)
1106 last-depth (1+ last-depth)))
1107 ;; Now go to the next line and indent it according
1108 ;; to what we learned from parsing the previous one.
1111 (skip-chars-forward " \t")
1112 ;; But not if the line is blank, or just a comment
1113 ;; (except for double-semi comments; indent them as usual).
1114 (if (or (eobp) (looking-at "\\s<\\|\n"))
1116 (if (and (car indent-stack)
1117 (>= (car indent-stack) 0))
1118 (setq this-indent (car indent-stack))
1119 (let ((val (calculate-lisp-indent
1120 (if (car indent-stack) (- (car indent-stack))
1123 (setq this-indent val)
1125 (setcar indent-stack
1126 (setq this-indent val))
1127 (setcar indent-stack (- (car (cdr val))))
1128 (setq this-indent (car val))))))
1129 (if (and this-indent (/= (current-column) this-indent))
1130 (progn (delete-region bol (point))
1131 (indent-to this-indent)))))
1133 (setq outer-loop-done (= (point) last-point))
1134 (setq last-point (point)))))))
1136 (defun lisp-indent-region (start end)
1137 "Indent every line whose first char is between START and END inclusive."
1139 (let ((endmark (copy-marker end)))
1141 (and (bolp) (not (eolp))
1143 (indent-sexp endmark)
1144 (set-marker endmark nil))))
1146 (defun indent-pp-sexp (&optional arg)
1147 "Indent each line of the list starting just after point, or prettyprint it.
1148 A prefix argument specifies pretty-printing."
1153 (narrow-to-region (point) (progn (forward-sexp 1) (point)))
1155 (goto-char (point-max))
1156 (if (eq (char-before) ?\n)
1157 (delete-char -1)))))
1160 ;;;; Lisp paragraph filling commands.
1162 (defcustom emacs-lisp-docstring-fill-column 65
1163 "Value of `fill-column' to use when filling a docstring.
1164 Any non-integer value means do not use a different value of
1165 `fill-column' when filling docstrings."
1166 :type '(choice (integer)
1167 (const :tag "Use the current `fill-column'" t))
1170 (defun lisp-fill-paragraph (&optional justify)
1171 "Like \\[fill-paragraph], but handle Emacs Lisp comments and docstrings.
1172 If any of the current line is a comment, fill the comment or the
1173 paragraph of it that point is in, preserving the comment's indentation
1174 and initial semicolons."
1176 (or (fill-comment-paragraph justify)
1177 ;; Since fill-comment-paragraph returned nil, that means we're not in
1178 ;; a comment: Point is on a program line; we are interested
1179 ;; particularly in docstring lines.
1181 ;; We bind `paragraph-start' and `paragraph-separate' temporarily. They
1182 ;; are buffer-local, but we avoid changing them so that they can be set
1183 ;; to make `forward-paragraph' and friends do something the user wants.
1185 ;; `paragraph-start': The `(' in the character alternative and the
1186 ;; left-singlequote plus `(' sequence after the \\| alternative prevent
1187 ;; sexps and backquoted sexps that follow a docstring from being filled
1188 ;; with the docstring. This setting has the consequence of inhibiting
1189 ;; filling many program lines that are not docstrings, which is sensible,
1190 ;; because the user probably asked to fill program lines by accident, or
1191 ;; expecting indentation (perhaps we should try to do indenting in that
1192 ;; case). The `;' and `:' stop the paragraph being filled at following
1193 ;; comment lines and at keywords (e.g., in `defcustom'). Left parens are
1194 ;; escaped to keep font-locking, filling, & paren matching in the source
1197 ;; `paragraph-separate': A clever regexp distinguishes the first line of
1198 ;; a docstring and identifies it as a paragraph separator, so that it
1199 ;; won't be filled. (Since the first line of documentation stands alone
1200 ;; in some contexts, filling should not alter the contents the author has
1201 ;; chosen.) Only the first line of a docstring begins with whitespace
1202 ;; and a quotation mark and ends with a period or (rarely) a comma.
1204 ;; The `fill-column' is temporarily bound to
1205 ;; `emacs-lisp-docstring-fill-column' if that value is an integer.
1206 (let ((paragraph-start (concat paragraph-start
1207 "\\|\\s-*\\([(;:\"]\\|`(\\|#'(\\)"))
1209 (concat paragraph-separate "\\|\\s-*\".*[,\\.]$"))
1210 (fill-column (if (integerp emacs-lisp-docstring-fill-column)
1211 emacs-lisp-docstring-fill-column
1213 (fill-paragraph justify))
1214 ;; Never return nil.
1217 (defun indent-code-rigidly (start end arg &optional nochange-regexp)
1218 "Indent all lines of code, starting in the region, sideways by ARG columns.
1219 Does not affect lines starting inside comments or strings, assuming that
1220 the start of the region is not inside them.
1222 Called from a program, takes args START, END, COLUMNS and NOCHANGE-REGEXP.
1223 The last is a regexp which, if matched at the beginning of a line,
1224 means don't indent that line."
1225 (interactive "r\np")
1229 (setq end (point-marker))
1232 (setq state (parse-partial-sexp (point)
1234 (forward-line 1) (point))
1236 (while (< (point) end)
1237 (or (car (nthcdr 3 state))
1238 (and nochange-regexp
1239 (looking-at nochange-regexp))
1240 ;; If line does not start in string, indent it
1241 (let ((indent (current-indentation)))
1242 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
1244 (indent-to (max 0 (+ indent arg)) 0))))
1245 (setq state (parse-partial-sexp (point)
1247 (forward-line 1) (point))
1250 (provide 'lisp-mode)
1252 ;; arch-tag: 414c7f93-c245-4b77-8ed5-ed05ef7ff1bf
1253 ;;; lisp-mode.el ends here