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 'imenu-generic-expression)
206 (setq imenu-generic-expression lisp-imenu-generic-expression)
207 (make-local-variable 'multibyte-syntax-as-symbol)
208 (setq multibyte-syntax-as-symbol t)
209 (set (make-local-variable 'syntax-begin-function) 'beginning-of-defun)
210 (setq font-lock-defaults
211 '((lisp-font-lock-keywords
212 lisp-font-lock-keywords-1 lisp-font-lock-keywords-2)
213 nil nil (("+-*/.<>=!?$%_&~^:@" . "w")) nil
214 (font-lock-mark-block-function . mark-defun)
215 (font-lock-syntactic-face-function
216 . lisp-font-lock-syntactic-face-function))))
218 (defun lisp-outline-level ()
219 "Lisp mode `outline-level' function."
220 (let ((len (- (match-end 0) (match-beginning 0))))
221 (if (looking-at "(\\|;;;###autoload")
225 (defvar lisp-mode-shared-map
226 (let ((map (make-sparse-keymap)))
227 (define-key map "\t" 'lisp-indent-line)
228 (define-key map "\e\C-q" 'indent-sexp)
229 (define-key map "\177" 'backward-delete-char-untabify)
230 ;; This gets in the way when viewing a Lisp file in view-mode. As
231 ;; long as [backspace] is mapped into DEL via the
232 ;; function-key-map, this should remain disabled!!
233 ;;;(define-key map [backspace] 'backward-delete-char-untabify)
235 "Keymap for commands shared by all sorts of Lisp modes.")
237 (defvar emacs-lisp-mode-map ()
238 "Keymap for Emacs Lisp mode.
239 All commands in `lisp-mode-shared-map' are inherited by this map.")
241 (if emacs-lisp-mode-map
243 (let ((map (make-sparse-keymap "Emacs-Lisp")))
244 (setq emacs-lisp-mode-map (make-sparse-keymap))
245 (set-keymap-parent emacs-lisp-mode-map lisp-mode-shared-map)
246 (define-key emacs-lisp-mode-map "\e\t" 'lisp-complete-symbol)
247 (define-key emacs-lisp-mode-map "\e\C-x" 'eval-defun)
248 (define-key emacs-lisp-mode-map "\e\C-q" 'indent-pp-sexp)
249 (define-key emacs-lisp-mode-map [menu-bar] (make-sparse-keymap))
250 (define-key emacs-lisp-mode-map [menu-bar emacs-lisp]
251 (cons "Emacs-Lisp" map))
252 (define-key map [edebug-defun]
253 '("Instrument Function for Debugging" . edebug-defun))
254 (define-key map [byte-recompile]
255 '("Byte-recompile Directory..." . byte-recompile-directory))
256 (define-key map [emacs-byte-compile-and-load]
257 '("Byte-compile And Load" . emacs-lisp-byte-compile-and-load))
258 (define-key map [byte-compile]
259 '("Byte-compile This File" . emacs-lisp-byte-compile))
260 (define-key map [separator-eval] '("--"))
261 (define-key map [eval-buffer] '("Evaluate Buffer" . eval-current-buffer))
262 (define-key map [eval-region] '("Evaluate Region" . eval-region))
263 (define-key map [eval-sexp] '("Evaluate Last S-expression" . eval-last-sexp))
264 (define-key map [separator-format] '("--"))
265 (define-key map [comment-region] '("Comment Out Region" . comment-region))
266 (define-key map [indent-region] '("Indent Region" . indent-region))
267 (define-key map [indent-line] '("Indent Line" . lisp-indent-line))
268 (put 'eval-region 'menu-enable 'mark-active)
269 (put 'comment-region 'menu-enable 'mark-active)
270 (put 'indent-region 'menu-enable 'mark-active)))
272 (defun emacs-lisp-byte-compile ()
273 "Byte compile the file containing the current buffer."
276 (byte-compile-file buffer-file-name)
277 (error "The buffer must be saved in a file first")))
279 (defun emacs-lisp-byte-compile-and-load ()
280 "Byte-compile the current file (if it has changed), then load compiled code."
283 (error "The buffer must be saved in a file first"))
285 ;; Recompile if file or buffer has changed since last compilation.
286 (if (and (buffer-modified-p)
287 (y-or-n-p (format "Save buffer %s first? " (buffer-name))))
289 (let ((compiled-file-name (byte-compile-dest-file buffer-file-name)))
290 (if (file-newer-than-file-p compiled-file-name buffer-file-name)
291 (load-file compiled-file-name)
292 (byte-compile-file buffer-file-name t))))
294 (defcustom emacs-lisp-mode-hook nil
295 "Hook run when entering Emacs Lisp mode."
296 :options '(turn-on-eldoc-mode imenu-add-menubar-index checkdoc-minor-mode)
300 (defcustom lisp-mode-hook nil
301 "Hook run when entering Lisp mode."
302 :options '(imenu-add-menubar-index)
306 (defcustom lisp-interaction-mode-hook nil
307 "Hook run when entering Lisp Interaction mode."
308 :options '(turn-on-eldoc-mode)
312 (defun emacs-lisp-mode ()
313 "Major mode for editing Lisp code to run in Emacs.
315 Delete converts tabs to spaces as it moves back.
316 Blank lines separate paragraphs. Semicolons start comments.
317 \\{emacs-lisp-mode-map}
318 Entry to this mode calls the value of `emacs-lisp-mode-hook'
319 if that value is non-nil."
321 (kill-all-local-variables)
322 (use-local-map emacs-lisp-mode-map)
323 (set-syntax-table emacs-lisp-mode-syntax-table)
324 (setq major-mode 'emacs-lisp-mode)
325 (setq mode-name "Emacs-Lisp")
326 (lisp-mode-variables)
327 (setq imenu-case-fold-search nil)
328 (run-mode-hooks 'emacs-lisp-mode-hook))
329 (put 'emacs-lisp-mode 'custom-mode-group 'lisp)
331 (defvar lisp-mode-map
332 (let ((map (make-sparse-keymap)))
333 (set-keymap-parent map lisp-mode-shared-map)
334 (define-key map "\e\C-x" 'lisp-eval-defun)
335 (define-key map "\C-c\C-z" 'run-lisp)
337 "Keymap for ordinary Lisp mode.
338 All commands in `lisp-mode-shared-map' are inherited by this map.")
341 "Major mode for editing Lisp code for Lisps other than GNU Emacs Lisp.
343 Delete converts tabs to spaces as it moves back.
344 Blank lines separate paragraphs. Semicolons start comments.
346 Note that `run-lisp' may be used either to start an inferior Lisp job
347 or to switch back to an existing one.
349 Entry to this mode calls the value of `lisp-mode-hook'
350 if that value is non-nil."
352 (kill-all-local-variables)
353 (use-local-map lisp-mode-map)
354 (setq major-mode 'lisp-mode)
355 (setq mode-name "Lisp")
356 (lisp-mode-variables)
357 (make-local-variable 'comment-start-skip)
358 (setq comment-start-skip
359 "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\)\\(;+\\|#|\\) *")
360 (make-local-variable 'font-lock-keywords-case-fold-search)
361 (setq font-lock-keywords-case-fold-search t)
362 (setq imenu-case-fold-search t)
363 (set-syntax-table lisp-mode-syntax-table)
364 (run-mode-hooks 'lisp-mode-hook))
365 (put 'lisp-mode 'find-tag-default-function 'lisp-find-tag-default)
367 (defun lisp-find-tag-default ()
368 (let ((default (find-tag-default)))
369 (when (stringp default)
370 (if (string-match ":+" default)
371 (substring default (match-end 0))
374 ;; Used in old LispM code.
375 (defalias 'common-lisp-mode 'lisp-mode)
377 ;; This will do unless inf-lisp.el is loaded.
378 (defun lisp-eval-defun (&optional and-go)
379 "Send the current defun to the Lisp process made by \\[run-lisp]."
381 (error "Process lisp does not exist"))
383 (defvar lisp-interaction-mode-map
384 (let ((map (make-sparse-keymap)))
385 (set-keymap-parent map lisp-mode-shared-map)
386 (define-key map "\e\C-x" 'eval-defun)
387 (define-key map "\e\C-q" 'indent-pp-sexp)
388 (define-key map "\e\t" 'lisp-complete-symbol)
389 (define-key map "\n" 'eval-print-last-sexp)
391 "Keymap for Lisp Interaction mode.
392 All commands in `lisp-mode-shared-map' are inherited by this map.")
394 (defvar lisp-interaction-mode-abbrev-table lisp-mode-abbrev-table)
395 (define-derived-mode lisp-interaction-mode emacs-lisp-mode "Lisp Interaction"
396 "Major mode for typing and evaluating Lisp forms.
397 Like Lisp mode except that \\[eval-print-last-sexp] evals the Lisp expression
398 before point, and prints its value into the buffer, advancing point.
399 Note that printing is controlled by `eval-expression-print-length'
400 and `eval-expression-print-level'.
403 Delete converts tabs to spaces as it moves back.
404 Paragraphs are separated only by blank lines.
405 Semicolons start comments.
406 \\{lisp-interaction-mode-map}
407 Entry to this mode calls the value of `lisp-interaction-mode-hook'
408 if that value is non-nil.")
410 (defun eval-print-last-sexp ()
411 "Evaluate sexp before point; print value into current buffer.
413 Note that printing the result is controlled by the variables
414 `eval-expression-print-length' and `eval-expression-print-level',
417 (let ((standard-output (current-buffer)))
423 (defun last-sexp-setup-props (beg end value alt1 alt2)
424 "Set up text properties for the output of `eval-last-sexp-1'.
425 BEG and END are the start and end of the output in current-buffer.
426 VALUE is the Lisp value printed, ALT1 and ALT2 are strings for the
427 alternative printed representations that can be displayed."
428 (let ((map (make-sparse-keymap)))
429 (define-key map "\C-m" 'last-sexp-toggle-display)
430 (define-key map [down-mouse-2] 'mouse-set-point)
431 (define-key map [mouse-2] 'last-sexp-toggle-display)
434 `(printed-value (,value ,alt1 ,alt2)
437 help-echo "RET, mouse-2: toggle abbreviated display"
438 rear-nonsticky (mouse-face keymap help-echo
442 (defun last-sexp-toggle-display (&optional arg)
443 "Toggle between abbreviated and unabbreviated printed representations."
447 (let ((value (get-text-property (point) 'printed-value)))
449 (let ((beg (or (previous-single-property-change (min (point-max) (1+ (point)))
452 (end (or (next-single-char-property-change (point) 'printed-value) (point)))
453 (standard-output (current-buffer))
455 (delete-region beg end)
456 (insert (nth 1 value))
457 (last-sexp-setup-props beg (point)
461 (goto-char (min (point-max) point)))))))
463 (defun prin1-char (char)
464 "Return a string representing CHAR as a character rather than as an integer.
465 If CHAR is not a character, return nil."
468 (let ((c (event-basic-type char))
469 (mods (event-modifiers char))
471 ;; Prevent ?A from turning into ?\S-a.
472 (if (and (memq 'shift mods)
473 (zerop (logand char ?\S-\^@))
474 (not (let ((case-fold-search nil))
475 (char-equal c (upcase c)))))
476 (setq c (upcase c) mods nil))
477 ;; What string are we considering using?
484 (cond ((eq modif 'super) "\\s-")
485 (t (string ?\\ (upcase (aref (symbol-name modif) 0)) ?-))))
488 ((memq c '(?\; ?\( ?\) ?\{ ?\} ?\[ ?\] ?\" ?\' ?\\)) (string ?\\ c))
493 ;; Verify the string reads a CHAR, not to some other character.
494 ;; If it doesn't, return nil instead.
496 (= (car (read-from-string string)) char)
500 (defun eval-last-sexp-1 (eval-last-sexp-arg-internal)
501 "Evaluate sexp before point; print value in minibuffer.
502 With argument, print output into current buffer."
503 (let ((standard-output (if eval-last-sexp-arg-internal (current-buffer) t)))
505 (eval (let ((stab (syntax-table))
510 (with-syntax-table emacs-lisp-mode-syntax-table
511 ;; If this sexp appears to be enclosed in `...'
512 ;; then ignore the surrounding quotes.
514 (or (eq (following-char) ?\')
515 (eq (preceding-char) ?\')))
517 ;; If we were after `?\e' (or similar case),
518 ;; use the whole thing, not just the `e'.
519 (when (eq (preceding-char) ?\\)
521 (when (eq (preceding-char) ??)
525 (when (eq (preceding-char) ?=)
528 (skip-chars-backward "0-9#=")
529 (setq labeled-p (looking-at "\\(#[0-9]+=\\)+")))
534 ;; vladimir@cs.ualberta.ca 30-Jul-1997: skip ` in
535 ;; `variable' so that the value is returned, not the
537 (if (and ignore-quotes
538 (eq (following-char) ?`))
540 (narrow-to-region (point-min) opoint)
541 (setq expr (read (current-buffer)))
542 ;; If it's an (interactive ...) form, it's more
543 ;; useful to show how an interactive call would
546 (eq (car expr) 'interactive)
548 (list 'call-interactively
555 (eval-last-sexp-print-value value))))
557 (defun eval-last-sexp-print-value (value)
558 (let ((unabbreviated (let ((print-length nil) (print-level nil))
559 (prin1-to-string value)))
560 (print-length eval-expression-print-length)
561 (print-level eval-expression-print-level)
566 (let ((str (eval-expression-print-format value)))
567 (if str (princ str)))
569 (when (and (bufferp standard-output)
570 (or (not (null print-length))
571 (not (null print-level)))
572 (not (string= unabbreviated
573 (buffer-substring-no-properties beg end))))
574 (last-sexp-setup-props beg end value
576 (buffer-substring-no-properties beg end))
580 (defvar eval-last-sexp-fake-value (make-symbol "t"))
582 (defun eval-last-sexp (eval-last-sexp-arg-internal)
583 "Evaluate sexp before point; print value in minibuffer.
584 Interactively, with prefix argument, print output into current buffer."
586 (if (null eval-expression-debug-on-error)
587 (eval-last-sexp-1 eval-last-sexp-arg-internal)
588 (let ((old-value eval-last-sexp-fake-value) new-value value)
589 (let ((debug-on-error old-value))
590 (setq value (eval-last-sexp-1 eval-last-sexp-arg-internal))
591 (setq new-value debug-on-error))
592 (unless (eq old-value new-value)
593 (setq debug-on-error new-value))
596 (defun eval-defun-1 (form)
597 "Treat some expressions specially.
598 Reset the `defvar' and `defcustom' variables to the initial value.
599 Reinitialize the face according to the `defface' specification."
600 ;; The code in edebug-defun should be consistent with this, but not
601 ;; the same, since this gets a macroexpended form.
602 (cond ((not (listp form))
604 ((and (eq (car form) 'defvar)
605 (cdr-safe (cdr-safe form))
606 (boundp (cadr form)))
607 ;; Force variable to be re-set.
608 `(progn (defvar ,(nth 1 form) nil ,@(nthcdr 3 form))
609 (setq-default ,(nth 1 form) ,(nth 2 form))))
610 ;; `defcustom' is now macroexpanded to
611 ;; `custom-declare-variable' with a quoted value arg.
612 ((and (eq (car form) 'custom-declare-variable)
613 (default-boundp (eval (nth 1 form))))
614 ;; Force variable to be bound.
615 (set-default (eval (nth 1 form)) (eval (nth 1 (nth 2 form))))
617 ;; `defface' is macroexpanded to `custom-declare-face'.
618 ((eq (car form) 'custom-declare-face)
620 (put (eval (nth 1 form)) 'face-defface-spec nil)
621 (setq face-new-frame-defaults
622 (assq-delete-all (eval (nth 1 form)) face-new-frame-defaults))
624 ((eq (car form) 'progn)
625 (cons 'progn (mapcar 'eval-defun-1 (cdr form))))
628 (defun eval-defun-2 ()
629 "Evaluate defun that point is in or before.
630 The value is displayed in the minibuffer.
631 If the current defun is actually a call to `defvar',
632 then reset the variable using the initial value expression
633 even if the variable already has some other value.
634 \(Normally `defvar' does not change the variable's value
635 if it already has a value.\)
637 With argument, insert value in current buffer after the defun.
638 Return the result of evaluation."
640 (let ((debug-on-error eval-expression-debug-on-error)
641 (print-length eval-expression-print-length)
642 (print-level eval-expression-print-level))
644 ;; Arrange for eval-region to "read" the (possibly) altered form.
645 ;; eval-region handles recording which file defines a function or
646 ;; variable. Re-written using `apply' to avoid capturing
647 ;; variables like `end'.
650 (let ((standard-output t)
652 ;; Read the form from the buffer, and record where it ends.
657 (setq form (read (current-buffer)))
659 ;; Alter the form if necessary.
660 (setq form (eval-defun-1 (macroexpand form)))
661 (list beg end standard-output
663 ;; Skipping to the end of the specified region
664 ;; will make eval-region return.
667 ;; The result of evaluation has been put onto VALUES. So return it.
670 (defun eval-defun (edebug-it)
671 "Evaluate the top-level form containing point, or after point.
673 If the current defun is actually a call to `defvar' or `defcustom',
674 evaluating it this way resets the variable using its initial value
675 expression even if the variable already has some other value.
676 \(Normally `defvar' and `defcustom' do not alter the value if there
679 With a prefix argument, instrument the code for Edebug.
681 If acting on a `defun' for FUNCTION, and the function was
682 instrumented, `Edebug: FUNCTION' is printed in the minibuffer. If not
683 instrumented, just FUNCTION is printed.
685 If not acting on a `defun', the result of evaluation is displayed in
686 the minibuffer. This display is controlled by the variables
687 `eval-expression-print-length' and `eval-expression-print-level',
692 (eval-defun (not edebug-all-defs)))
694 (if (null eval-expression-debug-on-error)
696 (let ((old-value (make-symbol "t")) new-value value)
697 (let ((debug-on-error old-value))
698 (setq value (eval-defun-2))
699 (setq new-value debug-on-error))
700 (unless (eq old-value new-value)
701 (setq debug-on-error new-value))
705 (defun lisp-comment-indent ()
706 (if (looking-at "\\s<\\s<\\s<")
708 (if (looking-at "\\s<\\s<")
709 (let ((tem (or (calculate-lisp-indent) (current-column))))
710 (if (listp tem) (car tem) tem))
711 (skip-chars-backward " \t")
712 (max (if (bolp) 0 (1+ (current-column)))
715 ;; This function just forces a more costly detection of comments (using
716 ;; parse-partial-sexp from beginning-of-defun). I.e. It avoids the problem of
717 ;; taking a `;' inside a string started on another line for a comment starter.
718 ;; Note: `newcomment' gets it right now since we set comment-use-global-state
719 ;; so we could get rid of it. -stef
720 (defun lisp-mode-auto-fill ()
721 (if (> (current-column) (current-fill-column))
723 (nth 4 (syntax-ppss (point))))
725 (unless (and (boundp 'comment-auto-fill-only-comments)
726 comment-auto-fill-only-comments)
727 (let ((comment-start nil) (comment-start-skip nil))
730 (defvar lisp-indent-offset nil
731 "If non-nil, indent second line of expressions that many more columns.")
732 (defvar lisp-indent-function 'lisp-indent-function)
734 (defun lisp-indent-line (&optional whole-exp)
735 "Indent current line as Lisp code.
736 With argument, indent any additional lines of the same expression
737 rigidly along with this one."
739 (let ((indent (calculate-lisp-indent)) shift-amt end
740 (pos (- (point-max) (point)))
741 (beg (progn (beginning-of-line) (point))))
742 (skip-chars-forward " \t")
743 (if (or (null indent) (looking-at "\\s<\\s<\\s<"))
744 ;; Don't alter indentation of a ;;; comment line
745 ;; or a line that starts in a string.
746 (goto-char (- (point-max) pos))
747 (if (and (looking-at "\\s<") (not (looking-at "\\s<\\s<")))
748 ;; Single-semicolon comment lines should be indented
749 ;; as comment lines, not as code.
750 (progn (indent-for-comment) (forward-char -1))
751 (if (listp indent) (setq indent (car indent)))
752 (setq shift-amt (- indent (current-column)))
753 (if (zerop shift-amt)
755 (delete-region beg (point))
757 ;; If initial point was within line's indentation,
758 ;; position after the indentation. Else stay at same point in text.
759 (if (> (- (point-max) pos) (point))
760 (goto-char (- (point-max) pos)))
761 ;; If desired, shift remaining lines of expression the same amount.
762 (and whole-exp (not (zerop shift-amt))
771 (indent-code-rigidly beg end shift-amt)))))
773 (defvar calculate-lisp-indent-last-sexp)
775 (defun calculate-lisp-indent (&optional parse-start)
776 "Return appropriate indentation for current line as Lisp code.
777 In usual case returns an integer: the column to indent to.
778 If the value is nil, that means don't change the indentation
779 because the line starts inside a string.
781 The value can also be a list of the form (COLUMN CONTAINING-SEXP-START).
782 This means that following lines at the same level of indentation
783 should not necessarily be indented the same as this line.
784 Then COLUMN is the column to indent to, and CONTAINING-SEXP-START
785 is the buffer position of the start of the containing expression."
788 (let ((indent-point (point))
790 ;; setting this to a number inhibits calling hook
793 calculate-lisp-indent-last-sexp containing-sexp)
795 (goto-char parse-start)
796 (beginning-of-defun))
797 ;; Find outermost containing sexp
798 (while (< (point) indent-point)
799 (setq state (parse-partial-sexp (point) indent-point 0)))
800 ;; Find innermost containing sexp
803 (> (setq paren-depth (elt state 0)) 0))
805 (setq calculate-lisp-indent-last-sexp (elt state 2))
806 (setq containing-sexp (elt state 1))
807 ;; Position following last unclosed open.
808 (goto-char (1+ containing-sexp))
809 ;; Is there a complete sexp since then?
810 (if (and calculate-lisp-indent-last-sexp
811 (> calculate-lisp-indent-last-sexp (point)))
812 ;; Yes, but is there a containing sexp after that?
813 (let ((peek (parse-partial-sexp calculate-lisp-indent-last-sexp
815 (if (setq retry (car (cdr peek))) (setq state peek)))))
818 ;; Innermost containing sexp found
819 (goto-char (1+ containing-sexp))
820 (if (not calculate-lisp-indent-last-sexp)
821 ;; indent-point immediately follows open paren.
823 (setq desired-indent (current-column))
824 ;; Find the start of first element of containing sexp.
825 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
826 (cond ((looking-at "\\s(")
827 ;; First element of containing sexp is a list.
828 ;; Indent under that list.
830 ((> (save-excursion (forward-line 1) (point))
831 calculate-lisp-indent-last-sexp)
832 ;; This is the first line to start within the containing sexp.
833 ;; It's almost certainly a function call.
834 (if (= (point) calculate-lisp-indent-last-sexp)
835 ;; Containing sexp has nothing before this line
836 ;; except the first element. Indent under that element.
838 ;; Skip the first element, find start of second (the first
839 ;; argument of the function call) and indent under.
840 (progn (forward-sexp 1)
841 (parse-partial-sexp (point)
842 calculate-lisp-indent-last-sexp
844 (backward-prefix-chars))
846 ;; Indent beneath first sexp on same line as
847 ;; `calculate-lisp-indent-last-sexp'. Again, it's
848 ;; almost certainly a function call.
849 (goto-char calculate-lisp-indent-last-sexp)
851 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp
853 (backward-prefix-chars)))))
854 ;; Point is at the point to indent under unless we are inside a string.
855 ;; Call indentation hook except when overridden by lisp-indent-offset
856 ;; or if the desired indentation has already been computed.
857 (let ((normal-indent (current-column)))
859 ;; Inside a string, don't change indentation.
861 ((and (integerp lisp-indent-offset) containing-sexp)
862 ;; Indent by constant offset
863 (goto-char containing-sexp)
864 (+ (current-column) lisp-indent-offset))
866 ((and (boundp 'lisp-indent-function)
869 (or (funcall lisp-indent-function indent-point state)
874 (defun lisp-indent-function (indent-point state)
875 "This function is the normal value of the variable `lisp-indent-function'.
876 It is used when indenting a line within a function call, to see if the
877 called function says anything special about how to indent the line.
879 INDENT-POINT is the position where the user typed TAB, or equivalent.
880 Point is located at the point to indent under (for default indentation);
881 STATE is the `parse-partial-sexp' state for that position.
883 If the current line is in a call to a Lisp function
884 which has a non-nil property `lisp-indent-function',
885 that specifies how to do the indentation. The property value can be
886 * `defun', meaning indent `defun'-style;
887 * an integer N, meaning indent the first N arguments specially
888 like ordinary function arguments and then indent any further
889 arguments like a body;
890 * a function to call just as this function was called.
891 If that function returns nil, that means it doesn't specify
894 This function also returns nil meaning don't specify the indentation."
895 (let ((normal-indent (current-column)))
896 (goto-char (1+ (elt state 1)))
897 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
898 (if (and (elt state 2)
899 (not (looking-at "\\sw\\|\\s_")))
900 ;; car of form doesn't seem to be a symbol
902 (if (not (> (save-excursion (forward-line 1) (point))
903 calculate-lisp-indent-last-sexp))
904 (progn (goto-char calculate-lisp-indent-last-sexp)
906 (parse-partial-sexp (point)
907 calculate-lisp-indent-last-sexp 0 t)))
908 ;; Indent under the list or under the first sexp on the same
909 ;; line as calculate-lisp-indent-last-sexp. Note that first
910 ;; thing on that line has to be complete sexp since we are
911 ;; inside the innermost containing sexp.
912 (backward-prefix-chars)
914 (let ((function (buffer-substring (point)
915 (progn (forward-sexp 1) (point))))
917 (setq method (or (get (intern-soft function) 'lisp-indent-function)
918 (get (intern-soft function) 'lisp-indent-hook)))
919 (cond ((or (eq method 'defun)
921 (> (length function) 3)
922 (string-match "\\`def" function)))
923 (lisp-indent-defform state indent-point))
925 (lisp-indent-specform method state
926 indent-point normal-indent))
928 (funcall method indent-point state)))))))
930 (defvar lisp-body-indent 2
931 "Number of columns to indent the second line of a `(def...)' form.")
933 (defun lisp-indent-specform (count state indent-point normal-indent)
934 (let ((containing-form-start (elt state 1))
936 body-indent containing-form-column)
937 ;; Move to the start of containing form, calculate indentation
938 ;; to use for non-distinguished forms (> count), and move past the
939 ;; function symbol. lisp-indent-function guarantees that there is at
940 ;; least one word or symbol character following open paren of containing
942 (goto-char containing-form-start)
943 (setq containing-form-column (current-column))
944 (setq body-indent (+ lisp-body-indent containing-form-column))
947 ;; Now find the start of the last form.
948 (parse-partial-sexp (point) indent-point 1 t)
949 (while (and (< (point) indent-point)
952 (setq count (1- count))
954 (parse-partial-sexp (point) indent-point 1 t))
956 ;; Point is sitting on first character of last (or count) sexp.
958 ;; A distinguished form. If it is the first or second form use double
959 ;; lisp-body-indent, else normal indent. With lisp-body-indent bound
960 ;; to 2 (the default), this just happens to work the same with if as
961 ;; the older code, but it makes unwind-protect, condition-case,
962 ;; with-output-to-temp-buffer, et. al. much more tasteful. The older,
963 ;; less hacked, behavior can be obtained by replacing below with
964 ;; (list normal-indent containing-form-start).
965 (if (<= (- i count) 1)
966 (list (+ containing-form-column (* 2 lisp-body-indent))
967 containing-form-start)
968 (list normal-indent containing-form-start))
969 ;; A non-distinguished form. Use body-indent if there are no
970 ;; distinguished forms and this is the first undistinguished form,
971 ;; or if this is the first undistinguished form and the preceding
972 ;; distinguished form has indentation at least as great as body-indent.
973 (if (or (and (= i 0) (= count 0))
974 (and (= count 0) (<= body-indent normal-indent)))
978 (defun lisp-indent-defform (state indent-point)
979 (goto-char (car (cdr state)))
981 (if (> (point) (car (cdr (cdr state))))
983 (goto-char (car (cdr state)))
984 (+ lisp-body-indent (current-column)))))
987 ;; (put 'progn 'lisp-indent-function 0), say, causes progn to be indented
988 ;; like defun if the first form is placed on the next line, otherwise
989 ;; it is indented like any other form (i.e. forms line up under first).
991 (put 'lambda 'lisp-indent-function 'defun)
992 (put 'autoload 'lisp-indent-function 'defun)
993 (put 'progn 'lisp-indent-function 0)
994 (put 'prog1 'lisp-indent-function 1)
995 (put 'prog2 'lisp-indent-function 2)
996 (put 'save-excursion 'lisp-indent-function 0)
997 (put 'save-window-excursion 'lisp-indent-function 0)
998 (put 'save-selected-window 'lisp-indent-function 0)
999 (put 'save-restriction 'lisp-indent-function 0)
1000 (put 'save-match-data 'lisp-indent-function 0)
1001 (put 'save-current-buffer 'lisp-indent-function 0)
1002 (put 'with-current-buffer 'lisp-indent-function 1)
1003 (put 'combine-after-change-calls 'lisp-indent-function 0)
1004 (put 'with-output-to-string 'lisp-indent-function 0)
1005 (put 'with-temp-file 'lisp-indent-function 1)
1006 (put 'with-temp-buffer 'lisp-indent-function 0)
1007 (put 'with-temp-message 'lisp-indent-function 1)
1008 (put 'with-syntax-table 'lisp-indent-function 1)
1009 (put 'let 'lisp-indent-function 1)
1010 (put 'let* 'lisp-indent-function 1)
1011 (put 'while 'lisp-indent-function 1)
1012 (put 'if 'lisp-indent-function 2)
1013 (put 'read-if 'lisp-indent-function 2)
1014 (put 'catch 'lisp-indent-function 1)
1015 (put 'condition-case 'lisp-indent-function 2)
1016 (put 'unwind-protect 'lisp-indent-function 1)
1017 (put 'with-output-to-temp-buffer 'lisp-indent-function 1)
1018 (put 'eval-after-load 'lisp-indent-function 1)
1019 (put 'dolist 'lisp-indent-function 1)
1020 (put 'dotimes 'lisp-indent-function 1)
1021 (put 'when 'lisp-indent-function 1)
1022 (put 'unless 'lisp-indent-function 1)
1024 (defun indent-sexp (&optional endpos)
1025 "Indent each line of the list starting just after point.
1026 If optional arg ENDPOS is given, indent each line, stopping when
1027 ENDPOS is encountered."
1029 (let ((indent-stack (list nil))
1031 ;; If ENDPOS is non-nil, use nil as STARTING-POINT
1032 ;; so that calculate-lisp-indent will find the beginning of
1033 ;; the defun we are in.
1034 ;; If ENDPOS is nil, it is safe not to scan before point
1035 ;; since every line we indent is more deeply nested than point is.
1036 (starting-point (if endpos nil (point)))
1037 (last-point (point))
1038 last-depth bol outer-loop-done inner-loop-done state this-indent)
1040 ;; Get error now if we don't have a complete sexp after point.
1041 (save-excursion (forward-sexp 1)))
1043 (setq outer-loop-done nil)
1044 (while (if endpos (< (point) endpos)
1045 (not outer-loop-done))
1046 (setq last-depth next-depth
1047 inner-loop-done nil)
1048 ;; Parse this line so we can learn the state
1049 ;; to indent the next line.
1050 ;; This inner loop goes through only once
1051 ;; unless a line ends inside a string.
1052 (while (and (not inner-loop-done)
1053 (not (setq outer-loop-done (eobp))))
1054 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
1056 (setq next-depth (car state))
1057 ;; If the line contains a comment other than the sort
1058 ;; that is indented like code,
1059 ;; indent it now with indent-for-comment.
1060 ;; Comments indented like code are right already.
1061 ;; In any case clear the in-comment flag in the state
1062 ;; because parse-partial-sexp never sees the newlines.
1063 (if (car (nthcdr 4 state))
1064 (progn (indent-for-comment)
1066 (setcar (nthcdr 4 state) nil)))
1067 ;; If this line ends inside a string,
1068 ;; go straight to next line, remaining within the inner loop,
1069 ;; and turn off the \-flag.
1070 (if (car (nthcdr 3 state))
1073 (setcar (nthcdr 5 state) nil))
1074 (setq inner-loop-done t)))
1078 (setq indent-stack (nconc indent-stack
1079 (make-list (- next-depth) nil))
1080 last-depth (- last-depth next-depth)
1082 (or outer-loop-done endpos
1083 (setq outer-loop-done (<= next-depth 0)))
1086 (while (> last-depth next-depth)
1087 (setq indent-stack (cdr indent-stack)
1088 last-depth (1- last-depth)))
1089 (while (< last-depth next-depth)
1090 (setq indent-stack (cons nil indent-stack)
1091 last-depth (1+ last-depth)))
1092 ;; Now go to the next line and indent it according
1093 ;; to what we learned from parsing the previous one.
1096 (skip-chars-forward " \t")
1097 ;; But not if the line is blank, or just a comment
1098 ;; (except for double-semi comments; indent them as usual).
1099 (if (or (eobp) (looking-at "\\s<\\|\n"))
1101 (if (and (car indent-stack)
1102 (>= (car indent-stack) 0))
1103 (setq this-indent (car indent-stack))
1104 (let ((val (calculate-lisp-indent
1105 (if (car indent-stack) (- (car indent-stack))
1108 (setq this-indent val)
1110 (setcar indent-stack
1111 (setq this-indent val))
1112 (setcar indent-stack (- (car (cdr val))))
1113 (setq this-indent (car val))))))
1114 (if (and this-indent (/= (current-column) this-indent))
1115 (progn (delete-region bol (point))
1116 (indent-to this-indent)))))
1118 (setq outer-loop-done (= (point) last-point))
1119 (setq last-point (point)))))))
1121 (defun lisp-indent-region (start end)
1122 "Indent every line whose first char is between START and END inclusive."
1124 (let ((endmark (copy-marker end)))
1126 (and (bolp) (not (eolp))
1128 (indent-sexp endmark)
1129 (set-marker endmark nil))))
1131 (defun indent-pp-sexp (&optional arg)
1132 "Indent each line of the list starting just after point, or prettyprint it.
1133 A prefix argument specifies pretty-printing."
1138 (narrow-to-region (point) (progn (forward-sexp 1) (point)))
1140 (goto-char (point-max))
1141 (if (eq (char-before) ?\n)
1142 (delete-char -1)))))
1145 ;;;; Lisp paragraph filling commands.
1147 (defcustom emacs-lisp-docstring-fill-column 65
1148 "Value of `fill-column' to use when filling a docstring.
1149 Any non-integer value means do not use a different value of
1150 `fill-column' when filling docstrings."
1151 :type '(choice (integer)
1152 (const :tag "Use the current `fill-column'" t))
1155 (defun lisp-fill-paragraph (&optional justify)
1156 "Like \\[fill-paragraph], but handle Emacs Lisp comments and docstrings.
1157 If any of the current line is a comment, fill the comment or the
1158 paragraph of it that point is in, preserving the comment's indentation
1159 and initial semicolons."
1161 (or (fill-comment-paragraph justify)
1162 ;; Since fill-comment-paragraph returned nil, that means we're not in
1163 ;; a comment: Point is on a program line; we are interested
1164 ;; particularly in docstring lines.
1166 ;; We bind `paragraph-start' and `paragraph-separate' temporarily. They
1167 ;; are buffer-local, but we avoid changing them so that they can be set
1168 ;; to make `forward-paragraph' and friends do something the user wants.
1170 ;; `paragraph-start': The `(' in the character alternative and the
1171 ;; left-singlequote plus `(' sequence after the \\| alternative prevent
1172 ;; sexps and backquoted sexps that follow a docstring from being filled
1173 ;; with the docstring. This setting has the consequence of inhibiting
1174 ;; filling many program lines that are not docstrings, which is sensible,
1175 ;; because the user probably asked to fill program lines by accident, or
1176 ;; expecting indentation (perhaps we should try to do indenting in that
1177 ;; case). The `;' and `:' stop the paragraph being filled at following
1178 ;; comment lines and at keywords (e.g., in `defcustom'). Left parens are
1179 ;; escaped to keep font-locking, filling, & paren matching in the source
1182 ;; `paragraph-separate': A clever regexp distinguishes the first line of
1183 ;; a docstring and identifies it as a paragraph separator, so that it
1184 ;; won't be filled. (Since the first line of documentation stands alone
1185 ;; in some contexts, filling should not alter the contents the author has
1186 ;; chosen.) Only the first line of a docstring begins with whitespace
1187 ;; and a quotation mark and ends with a period or (rarely) a comma.
1189 ;; The `fill-column' is temporarily bound to
1190 ;; `emacs-lisp-docstring-fill-column' if that value is an integer.
1191 (let ((paragraph-start (concat paragraph-start
1192 "\\|\\s-*\\([(;:\"]\\|`(\\|#'(\\)"))
1194 (concat paragraph-separate "\\|\\s-*\".*[,\\.]$"))
1195 (fill-column (if (integerp emacs-lisp-docstring-fill-column)
1196 emacs-lisp-docstring-fill-column
1198 (fill-paragraph justify))
1199 ;; Never return nil.
1202 (defun indent-code-rigidly (start end arg &optional nochange-regexp)
1203 "Indent all lines of code, starting in the region, sideways by ARG columns.
1204 Does not affect lines starting inside comments or strings, assuming that
1205 the start of the region is not inside them.
1207 Called from a program, takes args START, END, COLUMNS and NOCHANGE-REGEXP.
1208 The last is a regexp which, if matched at the beginning of a line,
1209 means don't indent that line."
1210 (interactive "r\np")
1214 (setq end (point-marker))
1217 (setq state (parse-partial-sexp (point)
1219 (forward-line 1) (point))
1221 (while (< (point) end)
1222 (or (car (nthcdr 3 state))
1223 (and nochange-regexp
1224 (looking-at nochange-regexp))
1225 ;; If line does not start in string, indent it
1226 (let ((indent (current-indentation)))
1227 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
1229 (indent-to (max 0 (+ indent arg)) 0))))
1230 (setq state (parse-partial-sexp (point)
1232 (forward-line 1) (point))
1235 (provide 'lisp-mode)
1237 ;; arch-tag: 414c7f93-c245-4b77-8ed5-ed05ef7ff1bf
1238 ;;; lisp-mode.el ends here