Trailing whitepace deleted.
[emacs.git] / lisp / emacs-lisp / lisp-mode.el
blob171f059d09f8b494ff8c0ed716a17d3187e0be86
1 ;;; lisp-mode.el --- Lisp mode, and its idiosyncratic commands
3 ;; Copyright (C) 1985, 1986, 1999, 2000, 2001 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
6 ;; Keywords: lisp, languages
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
25 ;;; Commentary:
27 ;; The base major mode for editing Lisp code (used also for Emacs Lisp).
28 ;; This mode is documented in the Emacs manual.
30 ;;; Code:
32 (defvar lisp-mode-abbrev-table nil)
34 (defvar emacs-lisp-mode-syntax-table
35 (let ((table (make-syntax-table)))
36 (let ((i 0))
37 (while (< i ?0)
38 (modify-syntax-entry i "_ " table)
39 (setq i (1+ i)))
40 (setq i (1+ ?9))
41 (while (< i ?A)
42 (modify-syntax-entry i "_ " table)
43 (setq i (1+ i)))
44 (setq i (1+ ?Z))
45 (while (< i ?a)
46 (modify-syntax-entry i "_ " table)
47 (setq i (1+ i)))
48 (setq i (1+ ?z))
49 (while (< i 128)
50 (modify-syntax-entry i "_ " table)
51 (setq i (1+ i)))
52 (modify-syntax-entry ? " " table)
53 (modify-syntax-entry ?\t " " table)
54 (modify-syntax-entry ?\f " " table)
55 (modify-syntax-entry ?\n "> " table)
56 ;; Give CR the same syntax as newline, for selective-display.
57 (modify-syntax-entry ?\^m "> " table)
58 (modify-syntax-entry ?\; "< " table)
59 (modify-syntax-entry ?` "' " table)
60 (modify-syntax-entry ?' "' " table)
61 (modify-syntax-entry ?, "' " table)
62 ;; Used to be singlequote; changed for flonums.
63 (modify-syntax-entry ?. "_ " table)
64 (modify-syntax-entry ?# "' " table)
65 (modify-syntax-entry ?\" "\" " table)
66 (modify-syntax-entry ?\\ "\\ " table)
67 (modify-syntax-entry ?\( "() " table)
68 (modify-syntax-entry ?\) ")( " table)
69 (modify-syntax-entry ?\[ "(] " table)
70 (modify-syntax-entry ?\] ")[ " table))
71 table))
73 (defvar lisp-mode-syntax-table
74 (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table)))
75 (modify-syntax-entry ?\[ "_ " table)
76 (modify-syntax-entry ?\] "_ " table)
77 (modify-syntax-entry ?# "' 14bn" table)
78 (modify-syntax-entry ?| "\" 23b" table)
79 table))
81 (define-abbrev-table 'lisp-mode-abbrev-table ())
83 (defvar lisp-imenu-generic-expression
84 (list
85 (list nil
86 (purecopy (concat "^\\s-*("
87 (eval-when-compile
88 (regexp-opt
89 '("defun" "defun*" "defsubst" "defmacro"
90 "defadvice" "define-skeleton"
91 "define-minor-mode" "define-derived-mode"
92 "define-compiler-macro" "define-modify-macro"
93 "defsetf" "define-setf-expander"
94 "define-method-combination"
95 "defgeneric" "defmethod") t))
96 "\\s-+\\(\\sw\\(\\sw\\|\\s_\\)+\\)"))
98 (list (purecopy "Variables")
99 (purecopy (concat "^\\s-*("
100 (eval-when-compile
101 (regexp-opt
102 '("defvar" "defconst" "defconstant" "defcustom"
103 "defparameter" "define-symbol-macro") t))
104 "\\s-+\\(\\sw\\(\\sw\\|\\s_\\)+\\)"))
106 (list (purecopy "Types")
107 (purecopy (concat "^\\s-*("
108 (eval-when-compile
109 (regexp-opt
110 '("defgroup" "deftheme" "deftype" "defstruct"
111 "defclass" "define-condition" "define-widget"
112 "defface" "defpackage") t))
113 "\\s-+'?\\(\\sw\\(\\sw\\|\\s_\\)+\\)"))
116 "Imenu generic expression for Lisp mode. See `imenu-generic-expression'.")
118 ;; This was originally in autoload.el and is still used there.
119 (put 'autoload 'doc-string-elt 3)
120 (put 'defun 'doc-string-elt 3)
121 (put 'defun* 'doc-string-elt 3)
122 (put 'defvar 'doc-string-elt 3)
123 (put 'defcustom 'doc-string-elt 3)
124 (put 'deftheme 'doc-string-elt 2)
125 (put 'defconst 'doc-string-elt 3)
126 (put 'defmacro 'doc-string-elt 3)
127 (put 'defmacro* 'doc-string-elt 3)
128 (put 'defsubst 'doc-string-elt 3)
129 (put 'define-skeleton 'doc-string-elt 2)
130 (put 'define-derived-mode 'doc-string-elt 4)
131 (put 'easy-mmode-define-minor-mode 'doc-string-elt 2)
132 (put 'define-minor-mode 'doc-string-elt 2)
133 (put 'define-generic-mode 'doc-string-elt 7)
134 ;; define-global-mode has no explicit docstring.
135 (put 'easy-mmode-define-global-mode 'doc-string-elt 0)
136 (put 'define-ibuffer-filter 'doc-string-elt 2)
137 (put 'define-ibuffer-op 'doc-string-elt 3)
138 (put 'define-ibuffer-sorter 'doc-string-elt 2)
140 (defun lisp-font-lock-syntactic-face-function (state)
141 (if (nth 3 state)
142 (if (and (eq (nth 0 state) 1)
143 ;; This might be a docstring.
144 (save-excursion
145 (let ((n 0))
146 (goto-char (nth 8 state))
147 (condition-case nil
148 (while (progn (backward-sexp 1) (setq n (1+ n))))
149 (scan-error nil))
150 (when (> n 0)
151 (let ((sym (intern-soft
152 (buffer-substring
153 (point) (progn (forward-sexp 1) (point))))))
154 (eq n (or (get sym 'doc-string-elt) 3)))))))
155 font-lock-doc-face
156 font-lock-string-face)
157 font-lock-comment-face))
159 ;; The LISP-SYNTAX argument is used by code in inf-lisp.el and is
160 ;; (uselessly) passed from pp.el, chistory.el, gnus-kill.el and score-mode.el
161 (defun lisp-mode-variables (&optional lisp-syntax)
162 (when lisp-syntax
163 (set-syntax-table lisp-mode-syntax-table))
164 (setq local-abbrev-table lisp-mode-abbrev-table)
165 (make-local-variable 'paragraph-ignore-fill-prefix)
166 (setq paragraph-ignore-fill-prefix t)
167 (make-local-variable 'fill-paragraph-function)
168 (setq fill-paragraph-function 'lisp-fill-paragraph)
169 ;; Adaptive fill mode gets in the way of auto-fill,
170 ;; and should make no difference for explicit fill
171 ;; because lisp-fill-paragraph should do the job.
172 ;; I believe that newcomment's auto-fill code properly deals with it -stef
173 ;;(set (make-local-variable 'adaptive-fill-mode) nil)
174 (make-local-variable 'normal-auto-fill-function)
175 (setq normal-auto-fill-function 'lisp-mode-auto-fill)
176 (make-local-variable 'indent-line-function)
177 (setq indent-line-function 'lisp-indent-line)
178 (make-local-variable 'indent-region-function)
179 (setq indent-region-function 'lisp-indent-region)
180 (make-local-variable 'parse-sexp-ignore-comments)
181 (setq parse-sexp-ignore-comments t)
182 (make-local-variable 'outline-regexp)
183 (setq outline-regexp ";;;;* \\|(")
184 (make-local-variable 'outline-level)
185 (setq outline-level 'lisp-outline-level)
186 (make-local-variable 'comment-start)
187 (setq comment-start ";")
188 (make-local-variable 'comment-start-skip)
189 ;; Look within the line for a ; following an even number of backslashes
190 ;; after either a non-backslash or the line beginning.
191 (setq comment-start-skip "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\);+ *")
192 (make-local-variable 'comment-add)
193 (setq comment-add 1) ;default to `;;' in comment-region
194 (make-local-variable 'comment-column)
195 (setq comment-column 40)
196 (make-local-variable 'comment-indent-function)
197 (setq comment-indent-function 'lisp-comment-indent)
198 (make-local-variable 'imenu-generic-expression)
199 (setq imenu-generic-expression lisp-imenu-generic-expression)
200 (make-local-variable 'multibyte-syntax-as-symbol)
201 (setq multibyte-syntax-as-symbol t)
202 (set (make-local-variable 'syntax-begin-function) 'beginning-of-defun)
203 (setq font-lock-defaults
204 '((lisp-font-lock-keywords
205 lisp-font-lock-keywords-1 lisp-font-lock-keywords-2)
206 nil nil (("+-*/.<>=!?$%_&~^:" . "w")) beginning-of-defun
207 (font-lock-mark-block-function . mark-defun)
208 (font-lock-syntactic-face-function
209 . lisp-font-lock-syntactic-face-function))))
211 (defun lisp-outline-level ()
212 "Lisp mode `outline-level' function."
213 (if (looking-at "(")
214 1000
215 (looking-at outline-regexp)
216 (- (match-end 0) (match-beginning 0))))
219 (defvar lisp-mode-shared-map
220 (let ((map (make-sparse-keymap)))
221 (define-key map "\t" 'lisp-indent-line)
222 (define-key map "\e\C-q" 'indent-sexp)
223 (define-key map "\177" 'backward-delete-char-untabify)
224 ;; This gets in the way when viewing a Lisp file in view-mode. As
225 ;; long as [backspace] is mapped into DEL via the
226 ;; function-key-map, this should remain disabled!!
227 ;;;(define-key map [backspace] 'backward-delete-char-untabify)
228 map)
229 "Keymap for commands shared by all sorts of Lisp modes.")
231 (defvar emacs-lisp-mode-map ()
232 "Keymap for Emacs Lisp mode.
233 All commands in `lisp-mode-shared-map' are inherited by this map.")
235 (if emacs-lisp-mode-map
237 (let ((map (make-sparse-keymap "Emacs-Lisp")))
238 (setq emacs-lisp-mode-map (make-sparse-keymap))
239 (set-keymap-parent emacs-lisp-mode-map lisp-mode-shared-map)
240 (define-key emacs-lisp-mode-map "\e\t" 'lisp-complete-symbol)
241 (define-key emacs-lisp-mode-map "\e\C-x" 'eval-defun)
242 (define-key emacs-lisp-mode-map [menu-bar] (make-sparse-keymap))
243 (define-key emacs-lisp-mode-map [menu-bar emacs-lisp]
244 (cons "Emacs-Lisp" map))
245 (define-key map [edebug-defun]
246 '("Instrument Function for Debugging" . edebug-defun))
247 (define-key map [byte-recompile]
248 '("Byte-recompile Directory..." . byte-recompile-directory))
249 (define-key map [emacs-byte-compile-and-load]
250 '("Byte-compile And Load" . emacs-lisp-byte-compile-and-load))
251 (define-key map [byte-compile]
252 '("Byte-compile This File" . emacs-lisp-byte-compile))
253 (define-key map [separator-eval] '("--"))
254 (define-key map [eval-buffer] '("Evaluate Buffer" . eval-current-buffer))
255 (define-key map [eval-region] '("Evaluate Region" . eval-region))
256 (define-key map [eval-sexp] '("Evaluate Last S-expression" . eval-last-sexp))
257 (define-key map [separator-format] '("--"))
258 (define-key map [comment-region] '("Comment Out Region" . comment-region))
259 (define-key map [indent-region] '("Indent Region" . indent-region))
260 (define-key map [indent-line] '("Indent Line" . lisp-indent-line))
261 (put 'eval-region 'menu-enable 'mark-active)
262 (put 'comment-region 'menu-enable 'mark-active)
263 (put 'indent-region 'menu-enable 'mark-active)))
265 (defun emacs-lisp-byte-compile ()
266 "Byte compile the file containing the current buffer."
267 (interactive)
268 (if buffer-file-name
269 (byte-compile-file buffer-file-name)
270 (error "The buffer must be saved in a file first")))
272 (defun emacs-lisp-byte-compile-and-load ()
273 "Byte-compile the current file (if it has changed), then load compiled code."
274 (interactive)
275 (or buffer-file-name
276 (error "The buffer must be saved in a file first"))
277 (require 'bytecomp)
278 ;; Recompile if file or buffer has changed since last compilation.
279 (if (and (buffer-modified-p)
280 (y-or-n-p (format "Save buffer %s first? " (buffer-name))))
281 (save-buffer))
282 (let ((compiled-file-name (byte-compile-dest-file buffer-file-name)))
283 (if (file-newer-than-file-p compiled-file-name buffer-file-name)
284 (load-file compiled-file-name)
285 (byte-compile-file buffer-file-name t))))
287 (defcustom emacs-lisp-mode-hook nil
288 "Hook run when entering Emacs Lisp mode."
289 :options '(turn-on-eldoc-mode imenu-add-menubar-index checkdoc-minor-mode)
290 :type 'hook
291 :group 'lisp)
293 (defcustom lisp-mode-hook nil
294 "Hook run when entering Lisp mode."
295 :options '(imenu-add-menubar-index)
296 :type 'hook
297 :group 'lisp)
299 (defcustom lisp-interaction-mode-hook nil
300 "Hook run when entering Lisp Interaction mode."
301 :options '(turn-on-eldoc-mode)
302 :type 'hook
303 :group 'lisp)
305 (defun emacs-lisp-mode ()
306 "Major mode for editing Lisp code to run in Emacs.
307 Commands:
308 Delete converts tabs to spaces as it moves back.
309 Blank lines separate paragraphs. Semicolons start comments.
310 \\{emacs-lisp-mode-map}
311 Entry to this mode calls the value of `emacs-lisp-mode-hook'
312 if that value is non-nil."
313 (interactive)
314 (kill-all-local-variables)
315 (use-local-map emacs-lisp-mode-map)
316 (set-syntax-table emacs-lisp-mode-syntax-table)
317 (setq major-mode 'emacs-lisp-mode)
318 (setq mode-name "Emacs-Lisp")
319 (lisp-mode-variables)
320 (setq imenu-case-fold-search nil)
321 (run-hooks 'emacs-lisp-mode-hook))
323 (defvar lisp-mode-map
324 (let ((map (make-sparse-keymap)))
325 (set-keymap-parent map lisp-mode-shared-map)
326 (define-key map "\e\C-x" 'lisp-eval-defun)
327 (define-key map "\C-c\C-z" 'run-lisp)
328 map)
329 "Keymap for ordinary Lisp mode.
330 All commands in `lisp-mode-shared-map' are inherited by this map.")
332 (defun lisp-mode ()
333 "Major mode for editing Lisp code for Lisps other than GNU Emacs Lisp.
334 Commands:
335 Delete converts tabs to spaces as it moves back.
336 Blank lines separate paragraphs. Semicolons start comments.
337 \\{lisp-mode-map}
338 Note that `run-lisp' may be used either to start an inferior Lisp job
339 or to switch back to an existing one.
341 Entry to this mode calls the value of `lisp-mode-hook'
342 if that value is non-nil."
343 (interactive)
344 (kill-all-local-variables)
345 (use-local-map lisp-mode-map)
346 (setq major-mode 'lisp-mode)
347 (setq mode-name "Lisp")
348 (lisp-mode-variables)
349 (make-local-variable 'comment-start-skip)
350 (setq comment-start-skip
351 "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\)\\(;+\\|#|\\) *")
352 (make-local-variable 'font-lock-keywords-case-fold-search)
353 (setq font-lock-keywords-case-fold-search t)
354 (setq imenu-case-fold-search t)
355 (set-syntax-table lisp-mode-syntax-table)
356 (run-hooks 'lisp-mode-hook))
358 ;; This will do unless inf-lisp.el is loaded.
359 (defun lisp-eval-defun (&optional and-go)
360 "Send the current defun to the Lisp process made by \\[run-lisp]."
361 (interactive)
362 (error "Process lisp does not exist"))
364 (defvar lisp-interaction-mode-map
365 (let ((map (make-sparse-keymap)))
366 (set-keymap-parent map lisp-mode-shared-map)
367 (define-key map "\e\C-x" 'eval-defun)
368 (define-key map "\e\t" 'lisp-complete-symbol)
369 (define-key map "\n" 'eval-print-last-sexp)
370 map)
371 "Keymap for Lisp Interaction mode.
372 All commands in `lisp-mode-shared-map' are inherited by this map.")
374 (defvar lisp-interaction-mode-abbrev-table lisp-mode-abbrev-table)
375 (define-derived-mode lisp-interaction-mode emacs-lisp-mode "Lisp Interaction"
376 "Major mode for typing and evaluating Lisp forms.
377 Like Lisp mode except that \\[eval-print-last-sexp] evals the Lisp expression
378 before point, and prints its value into the buffer, advancing point.
379 Note that printing is controlled by `eval-expression-print-length'
380 and `eval-expression-print-level'.
382 Commands:
383 Delete converts tabs to spaces as it moves back.
384 Paragraphs are separated only by blank lines.
385 Semicolons start comments.
386 \\{lisp-interaction-mode-map}
387 Entry to this mode calls the value of `lisp-interaction-mode-hook'
388 if that value is non-nil.")
390 (defun eval-print-last-sexp ()
391 "Evaluate sexp before point; print value into current buffer.
393 Note that printing the result is controlled by the variables
394 `eval-expression-print-length' and `eval-expression-print-level',
395 which see."
396 (interactive)
397 (let ((standard-output (current-buffer)))
398 (terpri)
399 (eval-last-sexp t)
400 (terpri)))
403 (defun last-sexp-setup-props (beg end value alt1 alt2)
404 "Set up text properties for the output of `eval-last-sexp-1'.
405 BEG and END are the start and end of the output in current-buffer.
406 VALUE is the Lisp value printed, ALT1 and ALT2 are strings for the
407 alternative printed representations that can be displayed."
408 (let ((map (make-sparse-keymap)))
409 (define-key map "\C-m" 'last-sexp-toggle-display)
410 (define-key map [down-mouse-2] 'mouse-set-point)
411 (define-key map [mouse-2] 'last-sexp-toggle-display)
412 (add-text-properties
413 beg end
414 `(printed-value (,value ,alt1 ,alt2)
415 mouse-face highlight
416 keymap ,map
417 help-echo "RET, mouse-2: toggle abbreviated display"
418 rear-nonsticky (mouse-face keymap help-echo
419 printed-value)))))
422 (defun last-sexp-toggle-display ()
423 "Toggle between abbreviated and unabbreviated printed representations."
424 (interactive)
425 (let ((value (get-text-property (point) 'printed-value)))
426 (when value
427 (let ((beg (or (previous-single-property-change (point) 'printed-value) (point)))
428 (end (or (next-single-char-property-change (point) 'printed-value) (point)))
429 (standard-output (current-buffer))
430 (point (point)))
431 (delete-region beg end)
432 (insert (nth 1 value))
433 (last-sexp-setup-props beg (point)
434 (nth 0 value)
435 (nth 2 value)
436 (nth 1 value))
437 (goto-char (min (point-max) point))))))
440 (defun eval-last-sexp-1 (eval-last-sexp-arg-internal)
441 "Evaluate sexp before point; print value in minibuffer.
442 With argument, print output into current buffer."
443 (let ((standard-output (if eval-last-sexp-arg-internal (current-buffer) t)))
444 (let ((value
445 (eval (let ((stab (syntax-table))
446 (opoint (point))
447 ignore-quotes
448 expr)
449 (unwind-protect
450 (save-excursion
451 (set-syntax-table emacs-lisp-mode-syntax-table)
452 ;; If this sexp appears to be enclosed in `...'
453 ;; then ignore the surrounding quotes.
454 (setq ignore-quotes
455 (or (eq (following-char) ?\')
456 (eq (preceding-char) ?\')))
457 (forward-sexp -1)
458 ;; If we were after `?\e' (or similar case),
459 ;; use the whole thing, not just the `e'.
460 (when (eq (preceding-char) ?\\)
461 (forward-char -1)
462 (when (eq (preceding-char) ??)
463 (forward-char -1)))
465 ;; Skip over `#N='s.
466 (when (eq (preceding-char) ?=)
467 (let (labeled-p)
468 (save-excursion
469 (skip-chars-backward "0-9#=")
470 (setq labeled-p (looking-at "\\(#[0-9]+=\\)+")))
471 (when labeled-p
472 (forward-sexp -1))))
474 (save-restriction
475 ;; vladimir@cs.ualberta.ca 30-Jul-1997: skip ` in
476 ;; `variable' so that the value is returned, not the
477 ;; name
478 (if (and ignore-quotes
479 (eq (following-char) ?`))
480 (forward-char))
481 (narrow-to-region (point-min) opoint)
482 (setq expr (read (current-buffer)))
483 ;; If it's an (interactive ...) form, it's more
484 ;; useful to show how an interactive call would
485 ;; use it.
486 (and (consp expr)
487 (eq (car expr) 'interactive)
488 (setq expr
489 (list 'call-interactively
490 (list 'quote
491 (list 'lambda
492 '(&rest args)
493 expr
494 'args)))))
495 expr))
496 (set-syntax-table stab))))))
497 (let ((unabbreviated (let ((print-length nil) (print-level nil))
498 (prin1-to-string value)))
499 (print-length eval-expression-print-length)
500 (print-level eval-expression-print-level)
501 (beg (point))
502 end)
503 (prog1
504 (prin1 value)
505 (setq end (point))
506 (when (and (bufferp standard-output)
507 (or (not (null print-length))
508 (not (null print-level)))
509 (not (string= unabbreviated
510 (buffer-substring-no-properties beg end))))
511 (last-sexp-setup-props beg end value
512 unabbreviated
513 (buffer-substring-no-properties beg end))
514 ))))))
517 (defun eval-last-sexp (eval-last-sexp-arg-internal)
518 "Evaluate sexp before point; print value in minibuffer.
519 Interactively, with prefix argument, print output into current buffer."
520 (interactive "P")
521 (if (null eval-expression-debug-on-error)
522 (eval-last-sexp-1 eval-last-sexp-arg-internal)
523 (let ((old-value (make-symbol "t")) new-value value)
524 (let ((debug-on-error old-value))
525 (setq value (eval-last-sexp-1 eval-last-sexp-arg-internal))
526 (setq new-value debug-on-error))
527 (unless (eq old-value new-value)
528 (setq debug-on-error new-value))
529 value)))
531 (defun eval-defun-1 (form)
532 "Change defvar into defconst within FORM.
533 Likewise for other constructs as necessary."
534 ;; The code in edebug-defun should be consistent with this, but not
535 ;; the same, since this gets a macroexpended form.
536 (cond ((not (listp form))
537 form)
538 ((and (eq (car form) 'defvar)
539 (cdr-safe (cdr-safe form)))
540 ;; Force variable to be bound.
541 (cons 'defconst (cdr form)))
542 ;; `defcustom' is now macroexpanded to
543 ;; `custom-declare-variable' with a quoted value arg.
544 ((and (eq (car form) 'custom-declare-variable)
545 (default-boundp (eval (nth 1 form))))
546 ;; Force variable to be bound.
547 (set-default (eval (nth 1 form)) (eval (nth 1 (nth 2 form))))
548 form)
549 ((eq (car form) 'progn)
550 (cons 'progn (mapcar 'eval-defun-1 (cdr form))))
551 (t form)))
553 (defun eval-defun-2 ()
554 "Evaluate defun that point is in or before.
555 The value is displayed in the minibuffer.
556 If the current defun is actually a call to `defvar',
557 then reset the variable using the initial value expression
558 even if the variable already has some other value.
559 \(Normally `defvar' does not change the variable's value
560 if it already has a value.\)
562 With argument, insert value in current buffer after the defun.
563 Return the result of evaluation."
564 (interactive "P")
565 (let ((debug-on-error eval-expression-debug-on-error)
566 (print-length eval-expression-print-length)
567 (print-level eval-expression-print-level))
568 (save-excursion
569 ;; Arrange for eval-region to "read" the (possibly) altered form.
570 ;; eval-region handles recording which file defines a function or
571 ;; variable. Re-written using `apply' to avoid capturing
572 ;; variables like `end'.
573 (apply
574 #'eval-region
575 (let ((standard-output t)
576 beg end form)
577 ;; Read the form from the buffer, and record where it ends.
578 (save-excursion
579 (end-of-defun)
580 (beginning-of-defun)
581 (setq beg (point))
582 (setq form (read (current-buffer)))
583 (setq end (point)))
584 ;; Alter the form if necessary, changing defvar into defconst, etc.
585 (setq form (eval-defun-1 (macroexpand form)))
586 (list beg end standard-output
587 `(lambda (ignore)
588 ;; Skipping to the end of the specified region
589 ;; will make eval-region return.
590 (goto-char ,end)
591 ',form))))))
592 ;; The result of evaluation has been put onto VALUES. So return it.
593 (car values))
595 (defun eval-defun (edebug-it)
596 "Evaluate the top-level form containing point, or after point.
598 If the current defun is actually a call to `defvar' or `defcustom',
599 evaluating it this way resets the variable using its initial value
600 expression even if the variable already has some other value.
601 \(Normally `defvar' and `defcustom' do not alter the value if there
602 already is one.)
604 With a prefix argument, instrument the code for Edebug.
606 If acting on a `defun' for FUNCTION, and the function was
607 instrumented, `Edebug: FUNCTION' is printed in the minibuffer. If not
608 instrumented, just FUNCTION is printed.
610 If not acting on a `defun', the result of evaluation is displayed in
611 the minibuffer. This display is controlled by the variables
612 `eval-expression-print-length' and `eval-expression-print-level',
613 which see."
614 (interactive "P")
615 (cond (edebug-it
616 (require 'edebug)
617 (eval-defun (not edebug-all-defs)))
619 (if (null eval-expression-debug-on-error)
620 (eval-defun-2)
621 (let ((old-value (make-symbol "t")) new-value value)
622 (let ((debug-on-error old-value))
623 (setq value (eval-defun-2))
624 (setq new-value debug-on-error))
625 (unless (eq old-value new-value)
626 (setq debug-on-error new-value))
627 value)))))
630 (defun lisp-comment-indent ()
631 (if (looking-at "\\s<\\s<\\s<")
632 (current-column)
633 (if (looking-at "\\s<\\s<")
634 (let ((tem (or (calculate-lisp-indent) (current-column))))
635 (if (listp tem) (car tem) tem))
636 (skip-chars-backward " \t")
637 (max (if (bolp) 0 (1+ (current-column)))
638 comment-column))))
640 ;; This function just forces a more costly detection of comments (using
641 ;; parse-partial-sexp from beginning-of-defun). I.e. It avoids the problem of
642 ;; taking a `;' inside a string started on another line for a comment starter.
643 ;; Note: `newcomment' gets it right in 99% of the cases if you're using
644 ;; font-lock, anyway, so we could get rid of it. -stef
645 (defun lisp-mode-auto-fill ()
646 (if (> (current-column) (current-fill-column))
647 (if (save-excursion
648 (nth 4 (syntax-ppss (point))))
649 (do-auto-fill)
650 (unless (and (boundp 'comment-auto-fill-only-comments)
651 comment-auto-fill-only-comments)
652 (let ((comment-start nil) (comment-start-skip nil))
653 (do-auto-fill))))))
655 (defvar lisp-indent-offset nil
656 "If non-nil, indent second line of expressions that many more columns.")
657 (defvar lisp-indent-function 'lisp-indent-function)
659 (defun lisp-indent-line (&optional whole-exp)
660 "Indent current line as Lisp code.
661 With argument, indent any additional lines of the same expression
662 rigidly along with this one."
663 (interactive "P")
664 (let ((indent (calculate-lisp-indent)) shift-amt end
665 (pos (- (point-max) (point)))
666 (beg (progn (beginning-of-line) (point))))
667 (skip-chars-forward " \t")
668 (if (or (null indent) (looking-at "\\s<\\s<\\s<"))
669 ;; Don't alter indentation of a ;;; comment line
670 ;; or a line that starts in a string.
671 (goto-char (- (point-max) pos))
672 (if (and (looking-at "\\s<") (not (looking-at "\\s<\\s<")))
673 ;; Single-semicolon comment lines should be indented
674 ;; as comment lines, not as code.
675 (progn (indent-for-comment) (forward-char -1))
676 (if (listp indent) (setq indent (car indent)))
677 (setq shift-amt (- indent (current-column)))
678 (if (zerop shift-amt)
680 (delete-region beg (point))
681 (indent-to indent)))
682 ;; If initial point was within line's indentation,
683 ;; position after the indentation. Else stay at same point in text.
684 (if (> (- (point-max) pos) (point))
685 (goto-char (- (point-max) pos)))
686 ;; If desired, shift remaining lines of expression the same amount.
687 (and whole-exp (not (zerop shift-amt))
688 (save-excursion
689 (goto-char beg)
690 (forward-sexp 1)
691 (setq end (point))
692 (goto-char beg)
693 (forward-line 1)
694 (setq beg (point))
695 (> end beg))
696 (indent-code-rigidly beg end shift-amt)))))
698 (defvar calculate-lisp-indent-last-sexp)
700 (defun calculate-lisp-indent (&optional parse-start)
701 "Return appropriate indentation for current line as Lisp code.
702 In usual case returns an integer: the column to indent to.
703 If the value is nil, that means don't change the indentation
704 because the line starts inside a string.
706 The value can also be a list of the form (COLUMN CONTAINING-SEXP-START).
707 This means that following lines at the same level of indentation
708 should not necessarily be indented the same as this line.
709 Then COLUMN is the column to indent to, and CONTAINING-SEXP-START
710 is the buffer position of the start of the containing expression."
711 (save-excursion
712 (beginning-of-line)
713 (let ((indent-point (point))
714 state paren-depth
715 ;; setting this to a number inhibits calling hook
716 (desired-indent nil)
717 (retry t)
718 calculate-lisp-indent-last-sexp containing-sexp)
719 (if parse-start
720 (goto-char parse-start)
721 (beginning-of-defun))
722 ;; Find outermost containing sexp
723 (while (< (point) indent-point)
724 (setq state (parse-partial-sexp (point) indent-point 0)))
725 ;; Find innermost containing sexp
726 (while (and retry
727 state
728 (> (setq paren-depth (elt state 0)) 0))
729 (setq retry nil)
730 (setq calculate-lisp-indent-last-sexp (elt state 2))
731 (setq containing-sexp (elt state 1))
732 ;; Position following last unclosed open.
733 (goto-char (1+ containing-sexp))
734 ;; Is there a complete sexp since then?
735 (if (and calculate-lisp-indent-last-sexp
736 (> calculate-lisp-indent-last-sexp (point)))
737 ;; Yes, but is there a containing sexp after that?
738 (let ((peek (parse-partial-sexp calculate-lisp-indent-last-sexp
739 indent-point 0)))
740 (if (setq retry (car (cdr peek))) (setq state peek)))))
741 (if retry
743 ;; Innermost containing sexp found
744 (goto-char (1+ containing-sexp))
745 (if (not calculate-lisp-indent-last-sexp)
746 ;; indent-point immediately follows open paren.
747 ;; Don't call hook.
748 (setq desired-indent (current-column))
749 ;; Find the start of first element of containing sexp.
750 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
751 (cond ((looking-at "\\s(")
752 ;; First element of containing sexp is a list.
753 ;; Indent under that list.
755 ((> (save-excursion (forward-line 1) (point))
756 calculate-lisp-indent-last-sexp)
757 ;; This is the first line to start within the containing sexp.
758 ;; It's almost certainly a function call.
759 (if (= (point) calculate-lisp-indent-last-sexp)
760 ;; Containing sexp has nothing before this line
761 ;; except the first element. Indent under that element.
763 ;; Skip the first element, find start of second (the first
764 ;; argument of the function call) and indent under.
765 (progn (forward-sexp 1)
766 (parse-partial-sexp (point)
767 calculate-lisp-indent-last-sexp
768 0 t)))
769 (backward-prefix-chars))
771 ;; Indent beneath first sexp on same line as
772 ;; `calculate-lisp-indent-last-sexp'. Again, it's
773 ;; almost certainly a function call.
774 (goto-char calculate-lisp-indent-last-sexp)
775 (beginning-of-line)
776 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp
777 0 t)
778 (backward-prefix-chars)))))
779 ;; Point is at the point to indent under unless we are inside a string.
780 ;; Call indentation hook except when overridden by lisp-indent-offset
781 ;; or if the desired indentation has already been computed.
782 (let ((normal-indent (current-column)))
783 (cond ((elt state 3)
784 ;; Inside a string, don't change indentation.
785 nil)
786 ((and (integerp lisp-indent-offset) containing-sexp)
787 ;; Indent by constant offset
788 (goto-char containing-sexp)
789 (+ (current-column) lisp-indent-offset))
790 (desired-indent)
791 ((and (boundp 'lisp-indent-function)
792 lisp-indent-function
793 (not retry))
794 (or (funcall lisp-indent-function indent-point state)
795 normal-indent))
797 normal-indent))))))
799 (defun lisp-indent-function (indent-point state)
800 "This function is the normal value of the variable `lisp-indent-function'.
801 It is used when indenting a line within a function call, to see if the
802 called function says anything special about how to indent the line.
804 INDENT-POINT is the position where the user typed TAB, or equivalent.
805 Point is located at the point to indent under (for default indentation);
806 STATE is the `parse-partial-sexp' state for that position.
808 If the current line is in a call to a Lisp function
809 which has a non-nil property `lisp-indent-function',
810 that specifies how to do the indentation. The property value can be
811 * `defun', meaning indent `defun'-style;
812 * an integer N, meaning indent the first N arguments specially
813 like ordinary function arguments and then indent any further
814 aruments like a body;
815 * a function to call just as this function was called.
816 If that function returns nil, that means it doesn't specify
817 the indentation.
819 This function also returns nil meaning don't specify the indentation."
820 (let ((normal-indent (current-column)))
821 (goto-char (1+ (elt state 1)))
822 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
823 (if (and (elt state 2)
824 (not (looking-at "\\sw\\|\\s_")))
825 ;; car of form doesn't seem to be a symbol
826 (progn
827 (if (not (> (save-excursion (forward-line 1) (point))
828 calculate-lisp-indent-last-sexp))
829 (progn (goto-char calculate-lisp-indent-last-sexp)
830 (beginning-of-line)
831 (parse-partial-sexp (point)
832 calculate-lisp-indent-last-sexp 0 t)))
833 ;; Indent under the list or under the first sexp on the same
834 ;; line as calculate-lisp-indent-last-sexp. Note that first
835 ;; thing on that line has to be complete sexp since we are
836 ;; inside the innermost containing sexp.
837 (backward-prefix-chars)
838 (current-column))
839 (let ((function (buffer-substring (point)
840 (progn (forward-sexp 1) (point))))
841 method)
842 (setq method (or (get (intern-soft function) 'lisp-indent-function)
843 (get (intern-soft function) 'lisp-indent-hook)))
844 (cond ((or (eq method 'defun)
845 (and (null method)
846 (> (length function) 3)
847 (string-match "\\`def" function)))
848 (lisp-indent-defform state indent-point))
849 ((integerp method)
850 (lisp-indent-specform method state
851 indent-point normal-indent))
852 (method
853 (funcall method state indent-point)))))))
855 (defvar lisp-body-indent 2
856 "Number of columns to indent the second line of a `(def...)' form.")
858 (defun lisp-indent-specform (count state indent-point normal-indent)
859 (let ((containing-form-start (elt state 1))
860 (i count)
861 body-indent containing-form-column)
862 ;; Move to the start of containing form, calculate indentation
863 ;; to use for non-distinguished forms (> count), and move past the
864 ;; function symbol. lisp-indent-function guarantees that there is at
865 ;; least one word or symbol character following open paren of containing
866 ;; form.
867 (goto-char containing-form-start)
868 (setq containing-form-column (current-column))
869 (setq body-indent (+ lisp-body-indent containing-form-column))
870 (forward-char 1)
871 (forward-sexp 1)
872 ;; Now find the start of the last form.
873 (parse-partial-sexp (point) indent-point 1 t)
874 (while (and (< (point) indent-point)
875 (condition-case ()
876 (progn
877 (setq count (1- count))
878 (forward-sexp 1)
879 (parse-partial-sexp (point) indent-point 1 t))
880 (error nil))))
881 ;; Point is sitting on first character of last (or count) sexp.
882 (if (> count 0)
883 ;; A distinguished form. If it is the first or second form use double
884 ;; lisp-body-indent, else normal indent. With lisp-body-indent bound
885 ;; to 2 (the default), this just happens to work the same with if as
886 ;; the older code, but it makes unwind-protect, condition-case,
887 ;; with-output-to-temp-buffer, et. al. much more tasteful. The older,
888 ;; less hacked, behavior can be obtained by replacing below with
889 ;; (list normal-indent containing-form-start).
890 (if (<= (- i count) 1)
891 (list (+ containing-form-column (* 2 lisp-body-indent))
892 containing-form-start)
893 (list normal-indent containing-form-start))
894 ;; A non-distinguished form. Use body-indent if there are no
895 ;; distinguished forms and this is the first undistinguished form,
896 ;; or if this is the first undistinguished form and the preceding
897 ;; distinguished form has indentation at least as great as body-indent.
898 (if (or (and (= i 0) (= count 0))
899 (and (= count 0) (<= body-indent normal-indent)))
900 body-indent
901 normal-indent))))
903 (defun lisp-indent-defform (state indent-point)
904 (goto-char (car (cdr state)))
905 (forward-line 1)
906 (if (> (point) (car (cdr (cdr state))))
907 (progn
908 (goto-char (car (cdr state)))
909 (+ lisp-body-indent (current-column)))))
912 ;; (put 'progn 'lisp-indent-function 0), say, causes progn to be indented
913 ;; like defun if the first form is placed on the next line, otherwise
914 ;; it is indented like any other form (i.e. forms line up under first).
916 (put 'lambda 'lisp-indent-function 'defun)
917 (put 'autoload 'lisp-indent-function 'defun)
918 (put 'progn 'lisp-indent-function 0)
919 (put 'prog1 'lisp-indent-function 1)
920 (put 'prog2 'lisp-indent-function 2)
921 (put 'save-excursion 'lisp-indent-function 0)
922 (put 'save-window-excursion 'lisp-indent-function 0)
923 (put 'save-selected-window 'lisp-indent-function 0)
924 (put 'save-restriction 'lisp-indent-function 0)
925 (put 'save-match-data 'lisp-indent-function 0)
926 (put 'save-current-buffer 'lisp-indent-function 0)
927 (put 'with-current-buffer 'lisp-indent-function 1)
928 (put 'combine-after-change-calls 'lisp-indent-function 0)
929 (put 'with-output-to-string 'lisp-indent-function 0)
930 (put 'with-temp-file 'lisp-indent-function 1)
931 (put 'with-temp-buffer 'lisp-indent-function 0)
932 (put 'with-temp-message 'lisp-indent-function 1)
933 (put 'with-syntax-table 'lisp-indent-function 1)
934 (put 'let 'lisp-indent-function 1)
935 (put 'let* 'lisp-indent-function 1)
936 (put 'while 'lisp-indent-function 1)
937 (put 'if 'lisp-indent-function 2)
938 (put 'read-if 'lisp-indent-function 2)
939 (put 'catch 'lisp-indent-function 1)
940 (put 'condition-case 'lisp-indent-function 2)
941 (put 'unwind-protect 'lisp-indent-function 1)
942 (put 'with-output-to-temp-buffer 'lisp-indent-function 1)
943 (put 'eval-after-load 'lisp-indent-function 1)
944 (put 'dolist 'lisp-indent-function 1)
945 (put 'dotimes 'lisp-indent-function 1)
946 (put 'when 'lisp-indent-function 1)
947 (put 'unless 'lisp-indent-function 1)
949 (defun indent-sexp (&optional endpos)
950 "Indent each line of the list starting just after point.
951 If optional arg ENDPOS is given, indent each line, stopping when
952 ENDPOS is encountered."
953 (interactive)
954 (let ((indent-stack (list nil))
955 (next-depth 0)
956 ;; If ENDPOS is non-nil, use nil as STARTING-POINT
957 ;; so that calculate-lisp-indent will find the beginning of
958 ;; the defun we are in.
959 ;; If ENDPOS is nil, it is safe not to scan before point
960 ;; since every line we indent is more deeply nested than point is.
961 (starting-point (if endpos nil (point)))
962 (last-point (point))
963 last-depth bol outer-loop-done inner-loop-done state this-indent)
964 (or endpos
965 ;; Get error now if we don't have a complete sexp after point.
966 (save-excursion (forward-sexp 1)))
967 (save-excursion
968 (setq outer-loop-done nil)
969 (while (if endpos (< (point) endpos)
970 (not outer-loop-done))
971 (setq last-depth next-depth
972 inner-loop-done nil)
973 ;; Parse this line so we can learn the state
974 ;; to indent the next line.
975 ;; This inner loop goes through only once
976 ;; unless a line ends inside a string.
977 (while (and (not inner-loop-done)
978 (not (setq outer-loop-done (eobp))))
979 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
980 nil nil state))
981 (setq next-depth (car state))
982 ;; If the line contains a comment other than the sort
983 ;; that is indented like code,
984 ;; indent it now with indent-for-comment.
985 ;; Comments indented like code are right already.
986 ;; In any case clear the in-comment flag in the state
987 ;; because parse-partial-sexp never sees the newlines.
988 (if (car (nthcdr 4 state))
989 (progn (indent-for-comment)
990 (end-of-line)
991 (setcar (nthcdr 4 state) nil)))
992 ;; If this line ends inside a string,
993 ;; go straight to next line, remaining within the inner loop,
994 ;; and turn off the \-flag.
995 (if (car (nthcdr 3 state))
996 (progn
997 (forward-line 1)
998 (setcar (nthcdr 5 state) nil))
999 (setq inner-loop-done t)))
1000 (and endpos
1001 (<= next-depth 0)
1002 (progn
1003 (setq indent-stack (nconc indent-stack
1004 (make-list (- next-depth) nil))
1005 last-depth (- last-depth next-depth)
1006 next-depth 0)))
1007 (or outer-loop-done endpos
1008 (setq outer-loop-done (<= next-depth 0)))
1009 (if outer-loop-done
1010 (forward-line 1)
1011 (while (> last-depth next-depth)
1012 (setq indent-stack (cdr indent-stack)
1013 last-depth (1- last-depth)))
1014 (while (< last-depth next-depth)
1015 (setq indent-stack (cons nil indent-stack)
1016 last-depth (1+ last-depth)))
1017 ;; Now go to the next line and indent it according
1018 ;; to what we learned from parsing the previous one.
1019 (forward-line 1)
1020 (setq bol (point))
1021 (skip-chars-forward " \t")
1022 ;; But not if the line is blank, or just a comment
1023 ;; (except for double-semi comments; indent them as usual).
1024 (if (or (eobp) (looking-at "\\s<\\|\n"))
1026 (if (and (car indent-stack)
1027 (>= (car indent-stack) 0))
1028 (setq this-indent (car indent-stack))
1029 (let ((val (calculate-lisp-indent
1030 (if (car indent-stack) (- (car indent-stack))
1031 starting-point))))
1032 (if (null val)
1033 (setq this-indent val)
1034 (if (integerp val)
1035 (setcar indent-stack
1036 (setq this-indent val))
1037 (setcar indent-stack (- (car (cdr val))))
1038 (setq this-indent (car val))))))
1039 (if (and this-indent (/= (current-column) this-indent))
1040 (progn (delete-region bol (point))
1041 (indent-to this-indent)))))
1042 (or outer-loop-done
1043 (setq outer-loop-done (= (point) last-point))
1044 (setq last-point (point)))))))
1046 (defun lisp-indent-region (start end)
1047 "Indent every line whose first char is between START and END inclusive."
1048 (save-excursion
1049 (let ((endmark (copy-marker end)))
1050 (goto-char start)
1051 (and (bolp) (not (eolp))
1052 (lisp-indent-line))
1053 (indent-sexp endmark)
1054 (set-marker endmark nil))))
1056 ;;;; Lisp paragraph filling commands.
1058 (defun lisp-fill-paragraph (&optional justify)
1059 "Like \\[fill-paragraph], but handle Emacs Lisp comments.
1060 If any of the current line is a comment, fill the comment or the
1061 paragraph of it that point is in, preserving the comment's indentation
1062 and initial semicolons."
1063 (interactive "P")
1064 (or (fill-comment-paragraph justify)
1065 ;; `paragraph-start' is set here (not in the buffer-local
1066 ;; variable so that `forward-paragraph' et al work as
1067 ;; expected) so that filling (doc) strings works sensibly.
1068 ;; Adding the opening paren to avoid the following sexp being
1069 ;; filled means that sexps generally aren't filled as normal
1070 ;; text, which is probably sensible. The `;' and `:' stop the
1071 ;; filled para at following comment lines and keywords
1072 ;; (typically in `defcustom').
1073 (let ((paragraph-start (concat paragraph-start
1074 "\\|\\s-*[\(;:\"]"))
1075 ;; Avoid filling the first line of docstring.
1076 (paragraph-separate
1077 (concat paragraph-separate "\\|\\s-*\".*\\.$")))
1078 (fill-paragraph justify))
1079 ;; Never return nil.
1082 (defun indent-code-rigidly (start end arg &optional nochange-regexp)
1083 "Indent all lines of code, starting in the region, sideways by ARG columns.
1084 Does not affect lines starting inside comments or strings, assuming that
1085 the start of the region is not inside them.
1087 Called from a program, takes args START, END, COLUMNS and NOCHANGE-REGEXP.
1088 The last is a regexp which, if matched at the beginning of a line,
1089 means don't indent that line."
1090 (interactive "r\np")
1091 (let (state)
1092 (save-excursion
1093 (goto-char end)
1094 (setq end (point-marker))
1095 (goto-char start)
1096 (or (bolp)
1097 (setq state (parse-partial-sexp (point)
1098 (progn
1099 (forward-line 1) (point))
1100 nil nil state)))
1101 (while (< (point) end)
1102 (or (car (nthcdr 3 state))
1103 (and nochange-regexp
1104 (looking-at nochange-regexp))
1105 ;; If line does not start in string, indent it
1106 (let ((indent (current-indentation)))
1107 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
1108 (or (eolp)
1109 (indent-to (max 0 (+ indent arg)) 0))))
1110 (setq state (parse-partial-sexp (point)
1111 (progn
1112 (forward-line 1) (point))
1113 nil nil state))))))
1115 (provide 'lisp-mode)
1117 ;;; lisp-mode.el ends here