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