1 ;;; lisp-mode.el --- Lisp mode, and its idiosyncratic commands
3 ;; Copyright (C) 1985,86,1999,2000,01,03,2004 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 'define-compilation-mode 'doc-string-elt 3)
133 (put 'easy-mmode-define-minor-mode 'doc-string-elt 2)
134 (put 'define-minor-mode 'doc-string-elt 2)
135 (put 'define-generic-mode 'doc-string-elt 7)
136 ;; define-global-mode has no explicit docstring.
137 (put 'easy-mmode-define-global-mode 'doc-string-elt 0)
138 (put 'define-ibuffer-filter 'doc-string-elt 2)
139 (put 'define-ibuffer-op 'doc-string-elt 3)
140 (put 'define-ibuffer-sorter 'doc-string-elt 2)
142 (defun lisp-font-lock-syntactic-face-function (state)
144 (if (and (eq (nth 0 state) 1)
145 ;; This might be a docstring.
148 (goto-char (nth 8 state))
150 (while (and (not (bobp))
151 (progn (backward-sexp 1) (setq n (1+ n)))))
154 (let ((sym (intern-soft
156 (point) (progn (forward-sexp 1) (point))))))
157 (eq n (or (get sym 'doc-string-elt) 3)))))))
159 font-lock-string-face)
160 font-lock-comment-face))
162 ;; The LISP-SYNTAX argument is used by code in inf-lisp.el and is
163 ;; (uselessly) passed from pp.el, chistory.el, gnus-kill.el and score-mode.el
164 (defun lisp-mode-variables (&optional lisp-syntax)
166 (set-syntax-table lisp-mode-syntax-table))
167 (setq local-abbrev-table lisp-mode-abbrev-table)
168 (make-local-variable 'paragraph-ignore-fill-prefix)
169 (setq paragraph-ignore-fill-prefix t)
170 (make-local-variable 'fill-paragraph-function)
171 (setq fill-paragraph-function 'lisp-fill-paragraph)
172 ;; Adaptive fill mode gets in the way of auto-fill,
173 ;; and should make no difference for explicit fill
174 ;; because lisp-fill-paragraph should do the job.
175 ;; I believe that newcomment's auto-fill code properly deals with it -stef
176 ;;(set (make-local-variable 'adaptive-fill-mode) nil)
177 (make-local-variable 'indent-line-function)
178 (setq indent-line-function 'lisp-indent-line)
179 (make-local-variable 'indent-region-function)
180 (setq indent-region-function 'lisp-indent-region)
181 (make-local-variable 'parse-sexp-ignore-comments)
182 (setq parse-sexp-ignore-comments t)
183 (make-local-variable 'outline-regexp)
184 (setq outline-regexp ";;;;* [^ \t\n]\\|(")
185 (make-local-variable 'outline-level)
186 (setq outline-level 'lisp-outline-level)
187 (make-local-variable 'comment-start)
188 (setq comment-start ";")
189 (make-local-variable 'comment-start-skip)
190 ;; Look within the line for a ; following an even number of backslashes
191 ;; after either a non-backslash or the line beginning.
192 (setq comment-start-skip "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\);+ *")
193 (make-local-variable 'comment-add)
194 (setq comment-add 1) ;default to `;;' in comment-region
195 (make-local-variable 'comment-column)
196 (setq comment-column 40)
197 ;; Don't get confused by `;' in doc strings when paragraph-filling.
198 (set (make-local-variable 'comment-use-global-state) t)
199 (make-local-variable 'imenu-generic-expression)
200 (setq imenu-generic-expression lisp-imenu-generic-expression)
201 (make-local-variable 'multibyte-syntax-as-symbol)
202 (setq multibyte-syntax-as-symbol t)
203 (set (make-local-variable 'syntax-begin-function) 'beginning-of-defun)
204 (setq font-lock-defaults
205 '((lisp-font-lock-keywords
206 lisp-font-lock-keywords-1 lisp-font-lock-keywords-2)
207 nil nil (("+-*/.<>=!?$%_&~^:" . "w")) nil
208 (font-lock-mark-block-function . mark-defun)
209 (font-lock-syntactic-face-function
210 . lisp-font-lock-syntactic-face-function))))
212 (defun lisp-outline-level ()
213 "Lisp mode `outline-level' function."
214 (if (looking-at "(\\|;;;###autoload")
216 (looking-at outline-regexp)
217 (- (match-end 0) (match-beginning 0))))
220 (defvar lisp-mode-shared-map
221 (let ((map (make-sparse-keymap)))
222 (define-key map "\t" 'lisp-indent-line)
223 (define-key map "\e\C-q" 'indent-sexp)
224 (define-key map "\177" 'backward-delete-char-untabify)
225 ;; This gets in the way when viewing a Lisp file in view-mode. As
226 ;; long as [backspace] is mapped into DEL via the
227 ;; function-key-map, this should remain disabled!!
228 ;;;(define-key map [backspace] 'backward-delete-char-untabify)
230 "Keymap for commands shared by all sorts of Lisp modes.")
232 (defvar emacs-lisp-mode-map ()
233 "Keymap for Emacs Lisp mode.
234 All commands in `lisp-mode-shared-map' are inherited by this map.")
236 (if emacs-lisp-mode-map
238 (let ((map (make-sparse-keymap "Emacs-Lisp")))
239 (setq emacs-lisp-mode-map (make-sparse-keymap))
240 (set-keymap-parent emacs-lisp-mode-map lisp-mode-shared-map)
241 (define-key emacs-lisp-mode-map "\e\t" 'lisp-complete-symbol)
242 (define-key emacs-lisp-mode-map "\e\C-x" 'eval-defun)
243 (define-key emacs-lisp-mode-map "\e\C-q" 'indent-pp-sexp)
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-mode-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-mode-hooks 'lisp-mode-hook))
360 (put 'lisp-mode 'find-tag-default-function 'lisp-find-tag-default)
362 (defun lisp-find-tag-default ()
363 (let ((default (find-tag-default)))
364 (when (stringp default)
365 (if (string-match ":+" default)
366 (substring default (match-end 0))
369 ;; Used in old LispM code.
370 (defalias 'common-lisp-mode 'lisp-mode)
372 ;; This will do unless inf-lisp.el is loaded.
373 (defun lisp-eval-defun (&optional and-go)
374 "Send the current defun to the Lisp process made by \\[run-lisp]."
376 (error "Process lisp does not exist"))
378 (defvar lisp-interaction-mode-map
379 (let ((map (make-sparse-keymap)))
380 (set-keymap-parent map lisp-mode-shared-map)
381 (define-key map "\e\C-x" 'eval-defun)
382 (define-key map "\e\C-q" 'indent-pp-sexp)
383 (define-key map "\e\t" 'lisp-complete-symbol)
384 (define-key map "\n" 'eval-print-last-sexp)
386 "Keymap for Lisp Interaction mode.
387 All commands in `lisp-mode-shared-map' are inherited by this map.")
389 (defvar lisp-interaction-mode-abbrev-table lisp-mode-abbrev-table)
390 (define-derived-mode lisp-interaction-mode emacs-lisp-mode "Lisp Interaction"
391 "Major mode for typing and evaluating Lisp forms.
392 Like Lisp mode except that \\[eval-print-last-sexp] evals the Lisp expression
393 before point, and prints its value into the buffer, advancing point.
394 Note that printing is controlled by `eval-expression-print-length'
395 and `eval-expression-print-level'.
398 Delete converts tabs to spaces as it moves back.
399 Paragraphs are separated only by blank lines.
400 Semicolons start comments.
401 \\{lisp-interaction-mode-map}
402 Entry to this mode calls the value of `lisp-interaction-mode-hook'
403 if that value is non-nil.")
405 (defun eval-print-last-sexp ()
406 "Evaluate sexp before point; print value into current buffer.
408 Note that printing the result is controlled by the variables
409 `eval-expression-print-length' and `eval-expression-print-level',
412 (let ((standard-output (current-buffer)))
418 (defun last-sexp-setup-props (beg end value alt1 alt2)
419 "Set up text properties for the output of `eval-last-sexp-1'.
420 BEG and END are the start and end of the output in current-buffer.
421 VALUE is the Lisp value printed, ALT1 and ALT2 are strings for the
422 alternative printed representations that can be displayed."
423 (let ((map (make-sparse-keymap)))
424 (define-key map "\C-m" 'last-sexp-toggle-display)
425 (define-key map [down-mouse-2] 'mouse-set-point)
426 (define-key map [mouse-2] 'last-sexp-toggle-display)
429 `(printed-value (,value ,alt1 ,alt2)
432 help-echo "RET, mouse-2: toggle abbreviated display"
433 rear-nonsticky (mouse-face keymap help-echo
437 (defun last-sexp-toggle-display (&optional arg)
438 "Toggle between abbreviated and unabbreviated printed representations."
442 (let ((value (get-text-property (point) 'printed-value)))
444 (let ((beg (or (previous-single-property-change (min (point-max) (1+ (point)))
447 (end (or (next-single-char-property-change (point) 'printed-value) (point)))
448 (standard-output (current-buffer))
450 (delete-region beg end)
451 (insert (nth 1 value))
452 (last-sexp-setup-props beg (point)
456 (goto-char (min (point-max) point)))))))
458 (defun prin1-char (char)
459 "Return a string representing CHAR as a character rather than as an integer.
460 If CHAR is not a character, return nil."
463 (let ((c (event-basic-type char))
464 (mods (event-modifiers char))
466 ;; Prevent ?A from turning into ?\S-a.
467 (if (and (memq 'shift mods)
468 (zerop (logand char ?\S-\^@))
469 (not (let ((case-fold-search nil))
470 (char-equal c (upcase c)))))
471 (setq c (upcase c) mods nil))
472 ;; What string are we considering using?
479 (cond ((eq modif 'super) "\\s-")
480 (t (string ?\\ (upcase (aref (symbol-name modif) 0)) ?-))))
483 ((memq c '(?\; ?\( ?\) ?\{ ?\} ?\[ ?\] ?\" ?\' ?\\)) (string ?\\ c))
488 ;; Verify the string reads a CHAR, not to some other character.
489 ;; If it doesn't, return nil instead.
491 (= (car (read-from-string string)) char)
495 (defun eval-last-sexp-1 (eval-last-sexp-arg-internal)
496 "Evaluate sexp before point; print value in minibuffer.
497 With argument, print output into current buffer."
498 (let ((standard-output (if eval-last-sexp-arg-internal (current-buffer) t)))
500 (eval (let ((stab (syntax-table))
505 (with-syntax-table emacs-lisp-mode-syntax-table
506 ;; If this sexp appears to be enclosed in `...'
507 ;; then ignore the surrounding quotes.
509 (or (eq (following-char) ?\')
510 (eq (preceding-char) ?\')))
512 ;; If we were after `?\e' (or similar case),
513 ;; use the whole thing, not just the `e'.
514 (when (eq (preceding-char) ?\\)
516 (when (eq (preceding-char) ??)
520 (when (eq (preceding-char) ?=)
523 (skip-chars-backward "0-9#=")
524 (setq labeled-p (looking-at "\\(#[0-9]+=\\)+")))
529 ;; vladimir@cs.ualberta.ca 30-Jul-1997: skip ` in
530 ;; `variable' so that the value is returned, not the
532 (if (and ignore-quotes
533 (eq (following-char) ?`))
535 (narrow-to-region (point-min) opoint)
536 (setq expr (read (current-buffer)))
537 ;; If it's an (interactive ...) form, it's more
538 ;; useful to show how an interactive call would
541 (eq (car expr) 'interactive)
543 (list 'call-interactively
550 (eval-last-sexp-print-value value))))
552 (defun eval-last-sexp-print-value (value)
553 (let ((unabbreviated (let ((print-length nil) (print-level nil))
554 (prin1-to-string value)))
555 (print-length eval-expression-print-length)
556 (print-level eval-expression-print-level)
561 (let ((str (eval-expression-print-format value)))
562 (if str (princ str)))
564 (when (and (bufferp standard-output)
565 (or (not (null print-length))
566 (not (null print-level)))
567 (not (string= unabbreviated
568 (buffer-substring-no-properties beg end))))
569 (last-sexp-setup-props beg end value
571 (buffer-substring-no-properties beg end))
575 (defvar eval-last-sexp-fake-value (make-symbol "t"))
577 (defun eval-last-sexp (eval-last-sexp-arg-internal)
578 "Evaluate sexp before point; print value in minibuffer.
579 Interactively, with prefix argument, print output into current buffer."
581 (if (null eval-expression-debug-on-error)
582 (eval-last-sexp-1 eval-last-sexp-arg-internal)
583 (let ((old-value eval-last-sexp-fake-value) new-value value)
584 (let ((debug-on-error old-value))
585 (setq value (eval-last-sexp-1 eval-last-sexp-arg-internal))
586 (setq new-value debug-on-error))
587 (unless (eq old-value new-value)
588 (setq debug-on-error new-value))
591 (defun eval-defun-1 (form)
592 "Treat some expressions specially.
593 Reset the `defvar' and `defcustom' variables to the initial value.
594 Reinitialize the face according to the `defface' specification."
595 ;; The code in edebug-defun should be consistent with this, but not
596 ;; the same, since this gets a macroexpended form.
597 (cond ((not (listp form))
599 ((and (eq (car form) 'defvar)
600 (cdr-safe (cdr-safe form))
601 (boundp (cadr form)))
602 ;; Force variable to be re-set.
603 `(progn (defvar ,(nth 1 form) nil ,@(nthcdr 3 form))
604 (setq ,(nth 1 form) ,(nth 2 form))))
605 ;; `defcustom' is now macroexpanded to
606 ;; `custom-declare-variable' with a quoted value arg.
607 ((and (eq (car form) 'custom-declare-variable)
608 (default-boundp (eval (nth 1 form))))
609 ;; Force variable to be bound.
610 (set-default (eval (nth 1 form)) (eval (nth 1 (nth 2 form))))
612 ;; `defface' is macroexpanded to `custom-declare-face'.
613 ((eq (car form) 'custom-declare-face)
615 (put (eval (nth 1 form)) 'face-defface-spec nil)
616 (setq face-new-frame-defaults
617 (assq-delete-all (eval (nth 1 form)) face-new-frame-defaults))
619 ((eq (car form) 'progn)
620 (cons 'progn (mapcar 'eval-defun-1 (cdr form))))
623 (defun eval-defun-2 ()
624 "Evaluate defun that point is in or before.
625 The value is displayed in the minibuffer.
626 If the current defun is actually a call to `defvar',
627 then reset the variable using the initial value expression
628 even if the variable already has some other value.
629 \(Normally `defvar' does not change the variable's value
630 if it already has a value.\)
632 With argument, insert value in current buffer after the defun.
633 Return the result of evaluation."
635 (let ((debug-on-error eval-expression-debug-on-error)
636 (print-length eval-expression-print-length)
637 (print-level eval-expression-print-level))
639 ;; Arrange for eval-region to "read" the (possibly) altered form.
640 ;; eval-region handles recording which file defines a function or
641 ;; variable. Re-written using `apply' to avoid capturing
642 ;; variables like `end'.
645 (let ((standard-output t)
647 ;; Read the form from the buffer, and record where it ends.
652 (setq form (read (current-buffer)))
654 ;; Alter the form if necessary.
655 (setq form (eval-defun-1 (macroexpand form)))
656 (list beg end standard-output
658 ;; Skipping to the end of the specified region
659 ;; will make eval-region return.
662 ;; The result of evaluation has been put onto VALUES. So return it.
665 (defun eval-defun (edebug-it)
666 "Evaluate the top-level form containing point, or after point.
668 If the current defun is actually a call to `defvar' or `defcustom',
669 evaluating it this way resets the variable using its initial value
670 expression even if the variable already has some other value.
671 \(Normally `defvar' and `defcustom' do not alter the value if there
674 With a prefix argument, instrument the code for Edebug.
676 If acting on a `defun' for FUNCTION, and the function was
677 instrumented, `Edebug: FUNCTION' is printed in the minibuffer. If not
678 instrumented, just FUNCTION is printed.
680 If not acting on a `defun', the result of evaluation is displayed in
681 the minibuffer. This display is controlled by the variables
682 `eval-expression-print-length' and `eval-expression-print-level',
687 (eval-defun (not edebug-all-defs)))
689 (if (null eval-expression-debug-on-error)
691 (let ((old-value (make-symbol "t")) new-value value)
692 (let ((debug-on-error old-value))
693 (setq value (eval-defun-2))
694 (setq new-value debug-on-error))
695 (unless (eq old-value new-value)
696 (setq debug-on-error new-value))
700 (defun lisp-comment-indent ()
701 (if (looking-at "\\s<\\s<\\s<")
703 (if (looking-at "\\s<\\s<")
704 (let ((tem (or (calculate-lisp-indent) (current-column))))
705 (if (listp tem) (car tem) tem))
706 (skip-chars-backward " \t")
707 (max (if (bolp) 0 (1+ (current-column)))
710 ;; This function just forces a more costly detection of comments (using
711 ;; parse-partial-sexp from beginning-of-defun). I.e. It avoids the problem of
712 ;; taking a `;' inside a string started on another line for a comment starter.
713 ;; Note: `newcomment' gets it right now since we set comment-use-global-state
714 ;; so we could get rid of it. -stef
715 (defun lisp-mode-auto-fill ()
716 (if (> (current-column) (current-fill-column))
718 (nth 4 (syntax-ppss (point))))
720 (unless (and (boundp 'comment-auto-fill-only-comments)
721 comment-auto-fill-only-comments)
722 (let ((comment-start nil) (comment-start-skip nil))
725 (defvar lisp-indent-offset nil
726 "If non-nil, indent second line of expressions that many more columns.")
727 (defvar lisp-indent-function 'lisp-indent-function)
729 (defun lisp-indent-line (&optional whole-exp)
730 "Indent current line as Lisp code.
731 With argument, indent any additional lines of the same expression
732 rigidly along with this one."
734 (let ((indent (calculate-lisp-indent)) shift-amt end
735 (pos (- (point-max) (point)))
736 (beg (progn (beginning-of-line) (point))))
737 (skip-chars-forward " \t")
738 (if (or (null indent) (looking-at "\\s<\\s<\\s<"))
739 ;; Don't alter indentation of a ;;; comment line
740 ;; or a line that starts in a string.
741 (goto-char (- (point-max) pos))
742 (if (and (looking-at "\\s<") (not (looking-at "\\s<\\s<")))
743 ;; Single-semicolon comment lines should be indented
744 ;; as comment lines, not as code.
745 (progn (indent-for-comment) (forward-char -1))
746 (if (listp indent) (setq indent (car indent)))
747 (setq shift-amt (- indent (current-column)))
748 (if (zerop shift-amt)
750 (delete-region beg (point))
752 ;; If initial point was within line's indentation,
753 ;; position after the indentation. Else stay at same point in text.
754 (if (> (- (point-max) pos) (point))
755 (goto-char (- (point-max) pos)))
756 ;; If desired, shift remaining lines of expression the same amount.
757 (and whole-exp (not (zerop shift-amt))
766 (indent-code-rigidly beg end shift-amt)))))
768 (defvar calculate-lisp-indent-last-sexp)
770 (defun calculate-lisp-indent (&optional parse-start)
771 "Return appropriate indentation for current line as Lisp code.
772 In usual case returns an integer: the column to indent to.
773 If the value is nil, that means don't change the indentation
774 because the line starts inside a string.
776 The value can also be a list of the form (COLUMN CONTAINING-SEXP-START).
777 This means that following lines at the same level of indentation
778 should not necessarily be indented the same as this line.
779 Then COLUMN is the column to indent to, and CONTAINING-SEXP-START
780 is the buffer position of the start of the containing expression."
783 (let ((indent-point (point))
785 ;; setting this to a number inhibits calling hook
788 calculate-lisp-indent-last-sexp containing-sexp)
790 (goto-char parse-start)
791 (beginning-of-defun))
792 ;; Find outermost containing sexp
793 (while (< (point) indent-point)
794 (setq state (parse-partial-sexp (point) indent-point 0)))
795 ;; Find innermost containing sexp
798 (> (setq paren-depth (elt state 0)) 0))
800 (setq calculate-lisp-indent-last-sexp (elt state 2))
801 (setq containing-sexp (elt state 1))
802 ;; Position following last unclosed open.
803 (goto-char (1+ containing-sexp))
804 ;; Is there a complete sexp since then?
805 (if (and calculate-lisp-indent-last-sexp
806 (> calculate-lisp-indent-last-sexp (point)))
807 ;; Yes, but is there a containing sexp after that?
808 (let ((peek (parse-partial-sexp calculate-lisp-indent-last-sexp
810 (if (setq retry (car (cdr peek))) (setq state peek)))))
813 ;; Innermost containing sexp found
814 (goto-char (1+ containing-sexp))
815 (if (not calculate-lisp-indent-last-sexp)
816 ;; indent-point immediately follows open paren.
818 (setq desired-indent (current-column))
819 ;; Find the start of first element of containing sexp.
820 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
821 (cond ((looking-at "\\s(")
822 ;; First element of containing sexp is a list.
823 ;; Indent under that list.
825 ((> (save-excursion (forward-line 1) (point))
826 calculate-lisp-indent-last-sexp)
827 ;; This is the first line to start within the containing sexp.
828 ;; It's almost certainly a function call.
829 (if (= (point) calculate-lisp-indent-last-sexp)
830 ;; Containing sexp has nothing before this line
831 ;; except the first element. Indent under that element.
833 ;; Skip the first element, find start of second (the first
834 ;; argument of the function call) and indent under.
835 (progn (forward-sexp 1)
836 (parse-partial-sexp (point)
837 calculate-lisp-indent-last-sexp
839 (backward-prefix-chars))
841 ;; Indent beneath first sexp on same line as
842 ;; `calculate-lisp-indent-last-sexp'. Again, it's
843 ;; almost certainly a function call.
844 (goto-char calculate-lisp-indent-last-sexp)
846 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp
848 (backward-prefix-chars)))))
849 ;; Point is at the point to indent under unless we are inside a string.
850 ;; Call indentation hook except when overridden by lisp-indent-offset
851 ;; or if the desired indentation has already been computed.
852 (let ((normal-indent (current-column)))
854 ;; Inside a string, don't change indentation.
856 ((and (integerp lisp-indent-offset) containing-sexp)
857 ;; Indent by constant offset
858 (goto-char containing-sexp)
859 (+ (current-column) lisp-indent-offset))
861 ((and (boundp 'lisp-indent-function)
864 (or (funcall lisp-indent-function indent-point state)
869 (defun lisp-indent-function (indent-point state)
870 "This function is the normal value of the variable `lisp-indent-function'.
871 It is used when indenting a line within a function call, to see if the
872 called function says anything special about how to indent the line.
874 INDENT-POINT is the position where the user typed TAB, or equivalent.
875 Point is located at the point to indent under (for default indentation);
876 STATE is the `parse-partial-sexp' state for that position.
878 If the current line is in a call to a Lisp function
879 which has a non-nil property `lisp-indent-function',
880 that specifies how to do the indentation. The property value can be
881 * `defun', meaning indent `defun'-style;
882 * an integer N, meaning indent the first N arguments specially
883 like ordinary function arguments and then indent any further
884 aruments like a body;
885 * a function to call just as this function was called.
886 If that function returns nil, that means it doesn't specify
889 This function also returns nil meaning don't specify the indentation."
890 (let ((normal-indent (current-column)))
891 (goto-char (1+ (elt state 1)))
892 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
893 (if (and (elt state 2)
894 (not (looking-at "\\sw\\|\\s_")))
895 ;; car of form doesn't seem to be a symbol
897 (if (not (> (save-excursion (forward-line 1) (point))
898 calculate-lisp-indent-last-sexp))
899 (progn (goto-char calculate-lisp-indent-last-sexp)
901 (parse-partial-sexp (point)
902 calculate-lisp-indent-last-sexp 0 t)))
903 ;; Indent under the list or under the first sexp on the same
904 ;; line as calculate-lisp-indent-last-sexp. Note that first
905 ;; thing on that line has to be complete sexp since we are
906 ;; inside the innermost containing sexp.
907 (backward-prefix-chars)
909 (let ((function (buffer-substring (point)
910 (progn (forward-sexp 1) (point))))
912 (setq method (or (get (intern-soft function) 'lisp-indent-function)
913 (get (intern-soft function) 'lisp-indent-hook)))
914 (cond ((or (eq method 'defun)
916 (> (length function) 3)
917 (string-match "\\`def" function)))
918 (lisp-indent-defform state indent-point))
920 (lisp-indent-specform method state
921 indent-point normal-indent))
923 (funcall method state indent-point)))))))
925 (defvar lisp-body-indent 2
926 "Number of columns to indent the second line of a `(def...)' form.")
928 (defun lisp-indent-specform (count state indent-point normal-indent)
929 (let ((containing-form-start (elt state 1))
931 body-indent containing-form-column)
932 ;; Move to the start of containing form, calculate indentation
933 ;; to use for non-distinguished forms (> count), and move past the
934 ;; function symbol. lisp-indent-function guarantees that there is at
935 ;; least one word or symbol character following open paren of containing
937 (goto-char containing-form-start)
938 (setq containing-form-column (current-column))
939 (setq body-indent (+ lisp-body-indent containing-form-column))
942 ;; Now find the start of the last form.
943 (parse-partial-sexp (point) indent-point 1 t)
944 (while (and (< (point) indent-point)
947 (setq count (1- count))
949 (parse-partial-sexp (point) indent-point 1 t))
951 ;; Point is sitting on first character of last (or count) sexp.
953 ;; A distinguished form. If it is the first or second form use double
954 ;; lisp-body-indent, else normal indent. With lisp-body-indent bound
955 ;; to 2 (the default), this just happens to work the same with if as
956 ;; the older code, but it makes unwind-protect, condition-case,
957 ;; with-output-to-temp-buffer, et. al. much more tasteful. The older,
958 ;; less hacked, behavior can be obtained by replacing below with
959 ;; (list normal-indent containing-form-start).
960 (if (<= (- i count) 1)
961 (list (+ containing-form-column (* 2 lisp-body-indent))
962 containing-form-start)
963 (list normal-indent containing-form-start))
964 ;; A non-distinguished form. Use body-indent if there are no
965 ;; distinguished forms and this is the first undistinguished form,
966 ;; or if this is the first undistinguished form and the preceding
967 ;; distinguished form has indentation at least as great as body-indent.
968 (if (or (and (= i 0) (= count 0))
969 (and (= count 0) (<= body-indent normal-indent)))
973 (defun lisp-indent-defform (state indent-point)
974 (goto-char (car (cdr state)))
976 (if (> (point) (car (cdr (cdr state))))
978 (goto-char (car (cdr state)))
979 (+ lisp-body-indent (current-column)))))
982 ;; (put 'progn 'lisp-indent-function 0), say, causes progn to be indented
983 ;; like defun if the first form is placed on the next line, otherwise
984 ;; it is indented like any other form (i.e. forms line up under first).
986 (put 'lambda 'lisp-indent-function 'defun)
987 (put 'autoload 'lisp-indent-function 'defun)
988 (put 'progn 'lisp-indent-function 0)
989 (put 'prog1 'lisp-indent-function 1)
990 (put 'prog2 'lisp-indent-function 2)
991 (put 'save-excursion 'lisp-indent-function 0)
992 (put 'save-window-excursion 'lisp-indent-function 0)
993 (put 'save-selected-window 'lisp-indent-function 0)
994 (put 'save-restriction 'lisp-indent-function 0)
995 (put 'save-match-data 'lisp-indent-function 0)
996 (put 'save-current-buffer 'lisp-indent-function 0)
997 (put 'with-current-buffer 'lisp-indent-function 1)
998 (put 'combine-after-change-calls 'lisp-indent-function 0)
999 (put 'with-output-to-string 'lisp-indent-function 0)
1000 (put 'with-temp-file 'lisp-indent-function 1)
1001 (put 'with-temp-buffer 'lisp-indent-function 0)
1002 (put 'with-temp-message 'lisp-indent-function 1)
1003 (put 'with-syntax-table 'lisp-indent-function 1)
1004 (put 'let 'lisp-indent-function 1)
1005 (put 'let* 'lisp-indent-function 1)
1006 (put 'while 'lisp-indent-function 1)
1007 (put 'if 'lisp-indent-function 2)
1008 (put 'read-if 'lisp-indent-function 2)
1009 (put 'catch 'lisp-indent-function 1)
1010 (put 'condition-case 'lisp-indent-function 2)
1011 (put 'unwind-protect 'lisp-indent-function 1)
1012 (put 'with-output-to-temp-buffer 'lisp-indent-function 1)
1013 (put 'eval-after-load 'lisp-indent-function 1)
1014 (put 'dolist 'lisp-indent-function 1)
1015 (put 'dotimes 'lisp-indent-function 1)
1016 (put 'when 'lisp-indent-function 1)
1017 (put 'unless 'lisp-indent-function 1)
1019 (defun indent-sexp (&optional endpos)
1020 "Indent each line of the list starting just after point.
1021 If optional arg ENDPOS is given, indent each line, stopping when
1022 ENDPOS is encountered."
1024 (let ((indent-stack (list nil))
1026 ;; If ENDPOS is non-nil, use nil as STARTING-POINT
1027 ;; so that calculate-lisp-indent will find the beginning of
1028 ;; the defun we are in.
1029 ;; If ENDPOS is nil, it is safe not to scan before point
1030 ;; since every line we indent is more deeply nested than point is.
1031 (starting-point (if endpos nil (point)))
1032 (last-point (point))
1033 last-depth bol outer-loop-done inner-loop-done state this-indent)
1035 ;; Get error now if we don't have a complete sexp after point.
1036 (save-excursion (forward-sexp 1)))
1038 (setq outer-loop-done nil)
1039 (while (if endpos (< (point) endpos)
1040 (not outer-loop-done))
1041 (setq last-depth next-depth
1042 inner-loop-done nil)
1043 ;; Parse this line so we can learn the state
1044 ;; to indent the next line.
1045 ;; This inner loop goes through only once
1046 ;; unless a line ends inside a string.
1047 (while (and (not inner-loop-done)
1048 (not (setq outer-loop-done (eobp))))
1049 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
1051 (setq next-depth (car state))
1052 ;; If the line contains a comment other than the sort
1053 ;; that is indented like code,
1054 ;; indent it now with indent-for-comment.
1055 ;; Comments indented like code are right already.
1056 ;; In any case clear the in-comment flag in the state
1057 ;; because parse-partial-sexp never sees the newlines.
1058 (if (car (nthcdr 4 state))
1059 (progn (indent-for-comment)
1061 (setcar (nthcdr 4 state) nil)))
1062 ;; If this line ends inside a string,
1063 ;; go straight to next line, remaining within the inner loop,
1064 ;; and turn off the \-flag.
1065 (if (car (nthcdr 3 state))
1068 (setcar (nthcdr 5 state) nil))
1069 (setq inner-loop-done t)))
1073 (setq indent-stack (nconc indent-stack
1074 (make-list (- next-depth) nil))
1075 last-depth (- last-depth next-depth)
1077 (or outer-loop-done endpos
1078 (setq outer-loop-done (<= next-depth 0)))
1081 (while (> last-depth next-depth)
1082 (setq indent-stack (cdr indent-stack)
1083 last-depth (1- last-depth)))
1084 (while (< last-depth next-depth)
1085 (setq indent-stack (cons nil indent-stack)
1086 last-depth (1+ last-depth)))
1087 ;; Now go to the next line and indent it according
1088 ;; to what we learned from parsing the previous one.
1091 (skip-chars-forward " \t")
1092 ;; But not if the line is blank, or just a comment
1093 ;; (except for double-semi comments; indent them as usual).
1094 (if (or (eobp) (looking-at "\\s<\\|\n"))
1096 (if (and (car indent-stack)
1097 (>= (car indent-stack) 0))
1098 (setq this-indent (car indent-stack))
1099 (let ((val (calculate-lisp-indent
1100 (if (car indent-stack) (- (car indent-stack))
1103 (setq this-indent val)
1105 (setcar indent-stack
1106 (setq this-indent val))
1107 (setcar indent-stack (- (car (cdr val))))
1108 (setq this-indent (car val))))))
1109 (if (and this-indent (/= (current-column) this-indent))
1110 (progn (delete-region bol (point))
1111 (indent-to this-indent)))))
1113 (setq outer-loop-done (= (point) last-point))
1114 (setq last-point (point)))))))
1116 (defun lisp-indent-region (start end)
1117 "Indent every line whose first char is between START and END inclusive."
1119 (let ((endmark (copy-marker end)))
1121 (and (bolp) (not (eolp))
1123 (indent-sexp endmark)
1124 (set-marker endmark nil))))
1126 (defun indent-pp-sexp (&optional arg)
1127 "Indent each line of the list or, with prefix ARG, pretty-printify the list."
1132 (narrow-to-region (point) (progn (forward-sexp 1) (point)))
1134 (goto-char (point-max))
1135 (if (eq (char-before) ?\n)
1136 (delete-char -1)))))
1139 ;;;; Lisp paragraph filling commands.
1141 (defcustom emacs-lisp-docstring-fill-column 65
1142 "Value of `fill-column' to use when filling a docstring.
1143 Any non-integer value means do not use a different value of
1144 `fill-column' when filling docstrings."
1145 :type '(choice (integer)
1146 (const :tag "Use the current `fill-column'" t))
1149 (defun lisp-fill-paragraph (&optional justify)
1150 "Like \\[fill-paragraph], but handle Emacs Lisp comments and docstrings.
1151 If any of the current line is a comment, fill the comment or the
1152 paragraph of it that point is in, preserving the comment's indentation
1153 and initial semicolons."
1155 (or (fill-comment-paragraph justify)
1156 ;; Point is on a program line (a line no comment); we are interested
1157 ;; particularly in docstring lines.
1159 ;; We bind `paragraph-start' and `paragraph-separate' temporarily. They
1160 ;; are buffer-local, but we avoid changing them so that they can be set
1161 ;; to make `forward-paragraph' and friends do something the user wants.
1163 ;; `paragraph-start': The `(' in the character alternative and the
1164 ;; left-singlequote plus `(' sequence after the \\| alternative prevent
1165 ;; sexps and backquoted sexps that follow a docstring from being filled
1166 ;; with the docstring. This setting has the consequence of inhibiting
1167 ;; filling many program lines that are not docstrings, which is sensible,
1168 ;; because the user probably asked to fill program lines by accident, or
1169 ;; expecting indentation (perhaps we should try to do indenting in that
1170 ;; case). The `;' and `:' stop the paragraph being filled at following
1171 ;; comment lines and at keywords (e.g., in `defcustom'). Left parens are
1172 ;; escaped to keep font-locking, filling, & paren matching in the source
1175 ;; `paragraph-separate': A clever regexp distinguishes the first line of
1176 ;; a docstring and identifies it as a paragraph separator, so that it
1177 ;; won't be filled. (Since the first line of documentation stands alone
1178 ;; in some contexts, filling should not alter the contents the author has
1179 ;; chosen.) Only the first line of a docstring begins with whitespace
1180 ;; and a quotation mark and ends with a period or (rarely) a comma.
1182 ;; The `fill-column' is temporarily bound to
1183 ;; `emacs-lisp-docstring-fill-column' if that value is an integer.
1184 (let ((paragraph-start (concat paragraph-start
1185 "\\|\\s-*\\([\(;:\"]\\|`\(\\|#'\(\\)"))
1187 (concat paragraph-separate "\\|\\s-*\".*[,\\.]$"))
1188 (fill-column (if (integerp emacs-lisp-docstring-fill-column)
1189 emacs-lisp-docstring-fill-column
1191 (fill-paragraph justify))
1192 ;; Never return nil.
1195 (defun indent-code-rigidly (start end arg &optional nochange-regexp)
1196 "Indent all lines of code, starting in the region, sideways by ARG columns.
1197 Does not affect lines starting inside comments or strings, assuming that
1198 the start of the region is not inside them.
1200 Called from a program, takes args START, END, COLUMNS and NOCHANGE-REGEXP.
1201 The last is a regexp which, if matched at the beginning of a line,
1202 means don't indent that line."
1203 (interactive "r\np")
1207 (setq end (point-marker))
1210 (setq state (parse-partial-sexp (point)
1212 (forward-line 1) (point))
1214 (while (< (point) end)
1215 (or (car (nthcdr 3 state))
1216 (and nochange-regexp
1217 (looking-at nochange-regexp))
1218 ;; If line does not start in string, indent it
1219 (let ((indent (current-indentation)))
1220 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
1222 (indent-to (max 0 (+ indent arg)) 0))))
1223 (setq state (parse-partial-sexp (point)
1225 (forward-line 1) (point))
1228 (provide 'lisp-mode)
1230 ;;; arch-tag: 414c7f93-c245-4b77-8ed5-ed05ef7ff1bf
1231 ;;; lisp-mode.el ends here