1 ;;; lisp-mode.el --- Lisp mode, and its idiosyncratic commands
3 ;; Copyright (C) 1985, 1986, 1999, 2000, 2001, 2003, 2004
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"
94 "define-compiler-macro" "define-modify-macro"
95 "defsetf" "define-setf-expander"
96 "define-method-combination"
97 "defgeneric" "defmethod") t
))
98 "\\s-+\\(\\sw\\(\\sw\\|\\s_\\)+\\)"))
100 (list (purecopy "Variables")
101 (purecopy (concat "^\\s-*("
104 '("defvar" "defconst" "defconstant" "defcustom"
105 "defparameter" "define-symbol-macro") t
))
106 "\\s-+\\(\\sw\\(\\sw\\|\\s_\\)+\\)"))
108 (list (purecopy "Types")
109 (purecopy (concat "^\\s-*("
112 '("defgroup" "deftheme" "deftype" "defstruct"
113 "defclass" "define-condition" "define-widget"
114 "defface" "defpackage") t
))
115 "\\s-+'?\\(\\sw\\(\\sw\\|\\s_\\)+\\)"))
118 "Imenu generic expression for Lisp mode. See `imenu-generic-expression'.")
120 ;; This was originally in autoload.el and is still used there.
121 (put 'autoload
'doc-string-elt
3)
122 (put 'defun
'doc-string-elt
3)
123 (put 'defun
* 'doc-string-elt
3)
124 (put 'defvar
'doc-string-elt
3)
125 (put 'defcustom
'doc-string-elt
3)
126 (put 'deftheme
'doc-string-elt
2)
127 (put 'defconst
'doc-string-elt
3)
128 (put 'defmacro
'doc-string-elt
3)
129 (put 'defmacro
* 'doc-string-elt
3)
130 (put 'defsubst
'doc-string-elt
3)
131 (put 'define-skeleton
'doc-string-elt
2)
132 (put 'define-derived-mode
'doc-string-elt
4)
133 (put 'define-compilation-mode
'doc-string-elt
3)
134 (put 'easy-mmode-define-minor-mode
'doc-string-elt
2)
135 (put 'define-minor-mode
'doc-string-elt
2)
136 (put 'define-generic-mode
'doc-string-elt
7)
137 ;; define-global-mode has no explicit docstring.
138 (put 'easy-mmode-define-global-mode
'doc-string-elt
0)
139 (put 'define-ibuffer-filter
'doc-string-elt
2)
140 (put 'define-ibuffer-op
'doc-string-elt
3)
141 (put 'define-ibuffer-sorter
'doc-string-elt
2)
143 (defun lisp-font-lock-syntactic-face-function (state)
145 (if (and (eq (nth 0 state
) 1)
146 ;; This might be a docstring.
149 (goto-char (nth 8 state
))
151 (while (and (not (bobp))
152 (progn (backward-sexp 1) (setq n
(1+ n
)))))
155 (let ((sym (intern-soft
157 (point) (progn (forward-sexp 1) (point))))))
158 (eq n
(or (get sym
'doc-string-elt
) 3)))))))
160 font-lock-string-face
)
161 font-lock-comment-face
))
163 ;; The LISP-SYNTAX argument is used by code in inf-lisp.el and is
164 ;; (uselessly) passed from pp.el, chistory.el, gnus-kill.el and score-mode.el
165 (defun lisp-mode-variables (&optional lisp-syntax
)
167 (set-syntax-table lisp-mode-syntax-table
))
168 (setq local-abbrev-table lisp-mode-abbrev-table
)
169 (make-local-variable 'paragraph-ignore-fill-prefix
)
170 (setq paragraph-ignore-fill-prefix t
)
171 (make-local-variable 'fill-paragraph-function
)
172 (setq fill-paragraph-function
'lisp-fill-paragraph
)
173 ;; Adaptive fill mode gets in the way of auto-fill,
174 ;; and should make no difference for explicit fill
175 ;; because lisp-fill-paragraph should do the job.
176 ;; I believe that newcomment's auto-fill code properly deals with it -stef
177 ;;(set (make-local-variable 'adaptive-fill-mode) nil)
178 (make-local-variable 'indent-line-function
)
179 (setq indent-line-function
'lisp-indent-line
)
180 (make-local-variable 'indent-region-function
)
181 (setq indent-region-function
'lisp-indent-region
)
182 (make-local-variable 'parse-sexp-ignore-comments
)
183 (setq parse-sexp-ignore-comments t
)
184 (make-local-variable 'outline-regexp
)
185 (setq outline-regexp
";;;;* [^ \t\n]\\|(")
186 (make-local-variable 'outline-level
)
187 (setq outline-level
'lisp-outline-level
)
188 (make-local-variable 'comment-start
)
189 (setq comment-start
";")
190 (make-local-variable 'comment-start-skip
)
191 ;; Look within the line for a ; following an even number of backslashes
192 ;; after either a non-backslash or the line beginning.
193 (setq comment-start-skip
"\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\);+ *")
194 (make-local-variable 'comment-add
)
195 (setq comment-add
1) ;default to `;;' in comment-region
196 (make-local-variable 'comment-column
)
197 (setq comment-column
40)
198 ;; Don't get confused by `;' in doc strings when paragraph-filling.
199 (set (make-local-variable 'comment-use-global-state
) t
)
200 (make-local-variable 'imenu-generic-expression
)
201 (setq imenu-generic-expression lisp-imenu-generic-expression
)
202 (make-local-variable 'multibyte-syntax-as-symbol
)
203 (setq multibyte-syntax-as-symbol t
)
204 (set (make-local-variable 'syntax-begin-function
) 'beginning-of-defun
)
205 (setq font-lock-defaults
206 '((lisp-font-lock-keywords
207 lisp-font-lock-keywords-1 lisp-font-lock-keywords-2
)
208 nil nil
(("+-*/.<>=!?$%_&~^:" .
"w")) nil
209 (font-lock-mark-block-function . mark-defun
)
210 (font-lock-syntactic-face-function
211 . lisp-font-lock-syntactic-face-function
))))
213 (defun lisp-outline-level ()
214 "Lisp mode `outline-level' function."
215 (if (looking-at "(\\|;;;###autoload")
217 (looking-at outline-regexp
)
218 (- (match-end 0) (match-beginning 0))))
221 (defvar lisp-mode-shared-map
222 (let ((map (make-sparse-keymap)))
223 (define-key map
"\t" 'lisp-indent-line
)
224 (define-key map
"\e\C-q" 'indent-sexp
)
225 (define-key map
"\177" 'backward-delete-char-untabify
)
226 ;; This gets in the way when viewing a Lisp file in view-mode. As
227 ;; long as [backspace] is mapped into DEL via the
228 ;; function-key-map, this should remain disabled!!
229 ;;;(define-key map [backspace] 'backward-delete-char-untabify)
231 "Keymap for commands shared by all sorts of Lisp modes.")
233 (defvar emacs-lisp-mode-map
()
234 "Keymap for Emacs Lisp mode.
235 All commands in `lisp-mode-shared-map' are inherited by this map.")
237 (if emacs-lisp-mode-map
239 (let ((map (make-sparse-keymap "Emacs-Lisp")))
240 (setq emacs-lisp-mode-map
(make-sparse-keymap))
241 (set-keymap-parent emacs-lisp-mode-map lisp-mode-shared-map
)
242 (define-key emacs-lisp-mode-map
"\e\t" 'lisp-complete-symbol
)
243 (define-key emacs-lisp-mode-map
"\e\C-x" 'eval-defun
)
244 (define-key emacs-lisp-mode-map
"\e\C-q" 'indent-pp-sexp
)
245 (define-key emacs-lisp-mode-map
[menu-bar
] (make-sparse-keymap))
246 (define-key emacs-lisp-mode-map
[menu-bar emacs-lisp
]
247 (cons "Emacs-Lisp" map
))
248 (define-key map
[edebug-defun
]
249 '("Instrument Function for Debugging" . edebug-defun
))
250 (define-key map
[byte-recompile
]
251 '("Byte-recompile Directory..." . byte-recompile-directory
))
252 (define-key map
[emacs-byte-compile-and-load
]
253 '("Byte-compile And Load" . emacs-lisp-byte-compile-and-load
))
254 (define-key map
[byte-compile
]
255 '("Byte-compile This File" . emacs-lisp-byte-compile
))
256 (define-key map
[separator-eval
] '("--"))
257 (define-key map
[eval-buffer
] '("Evaluate Buffer" . eval-current-buffer
))
258 (define-key map
[eval-region
] '("Evaluate Region" . eval-region
))
259 (define-key map
[eval-sexp
] '("Evaluate Last S-expression" . eval-last-sexp
))
260 (define-key map
[separator-format
] '("--"))
261 (define-key map
[comment-region
] '("Comment Out Region" . comment-region
))
262 (define-key map
[indent-region
] '("Indent Region" . indent-region
))
263 (define-key map
[indent-line
] '("Indent Line" . lisp-indent-line
))
264 (put 'eval-region
'menu-enable
'mark-active
)
265 (put 'comment-region
'menu-enable
'mark-active
)
266 (put 'indent-region
'menu-enable
'mark-active
)))
268 (defun emacs-lisp-byte-compile ()
269 "Byte compile the file containing the current buffer."
272 (byte-compile-file buffer-file-name
)
273 (error "The buffer must be saved in a file first")))
275 (defun emacs-lisp-byte-compile-and-load ()
276 "Byte-compile the current file (if it has changed), then load compiled code."
279 (error "The buffer must be saved in a file first"))
281 ;; Recompile if file or buffer has changed since last compilation.
282 (if (and (buffer-modified-p)
283 (y-or-n-p (format "Save buffer %s first? " (buffer-name))))
285 (let ((compiled-file-name (byte-compile-dest-file buffer-file-name
)))
286 (if (file-newer-than-file-p compiled-file-name buffer-file-name
)
287 (load-file compiled-file-name
)
288 (byte-compile-file buffer-file-name t
))))
290 (defcustom emacs-lisp-mode-hook nil
291 "Hook run when entering Emacs Lisp mode."
292 :options
'(turn-on-eldoc-mode imenu-add-menubar-index checkdoc-minor-mode
)
296 (defcustom lisp-mode-hook nil
297 "Hook run when entering Lisp mode."
298 :options
'(imenu-add-menubar-index)
302 (defcustom lisp-interaction-mode-hook nil
303 "Hook run when entering Lisp Interaction mode."
304 :options
'(turn-on-eldoc-mode)
308 (defun emacs-lisp-mode ()
309 "Major mode for editing Lisp code to run in Emacs.
311 Delete converts tabs to spaces as it moves back.
312 Blank lines separate paragraphs. Semicolons start comments.
313 \\{emacs-lisp-mode-map}
314 Entry to this mode calls the value of `emacs-lisp-mode-hook'
315 if that value is non-nil."
317 (kill-all-local-variables)
318 (use-local-map emacs-lisp-mode-map
)
319 (set-syntax-table emacs-lisp-mode-syntax-table
)
320 (setq major-mode
'emacs-lisp-mode
)
321 (setq mode-name
"Emacs-Lisp")
322 (lisp-mode-variables)
323 (setq imenu-case-fold-search nil
)
324 (run-mode-hooks 'emacs-lisp-mode-hook
))
325 (put 'emacs-lisp-mode
'custom-mode-group
'lisp
)
327 (defvar lisp-mode-map
328 (let ((map (make-sparse-keymap)))
329 (set-keymap-parent map lisp-mode-shared-map
)
330 (define-key map
"\e\C-x" 'lisp-eval-defun
)
331 (define-key map
"\C-c\C-z" 'run-lisp
)
333 "Keymap for ordinary Lisp mode.
334 All commands in `lisp-mode-shared-map' are inherited by this map.")
337 "Major mode for editing Lisp code for Lisps other than GNU Emacs Lisp.
339 Delete converts tabs to spaces as it moves back.
340 Blank lines separate paragraphs. Semicolons start comments.
342 Note that `run-lisp' may be used either to start an inferior Lisp job
343 or to switch back to an existing one.
345 Entry to this mode calls the value of `lisp-mode-hook'
346 if that value is non-nil."
348 (kill-all-local-variables)
349 (use-local-map lisp-mode-map
)
350 (setq major-mode
'lisp-mode
)
351 (setq mode-name
"Lisp")
352 (lisp-mode-variables)
353 (make-local-variable 'comment-start-skip
)
354 (setq comment-start-skip
355 "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\)\\(;+\\|#|\\) *")
356 (make-local-variable 'font-lock-keywords-case-fold-search
)
357 (setq font-lock-keywords-case-fold-search t
)
358 (setq imenu-case-fold-search t
)
359 (set-syntax-table lisp-mode-syntax-table
)
360 (run-mode-hooks 'lisp-mode-hook
))
361 (put 'lisp-mode
'find-tag-default-function
'lisp-find-tag-default
)
363 (defun lisp-find-tag-default ()
364 (let ((default (find-tag-default)))
365 (when (stringp default
)
366 (if (string-match ":+" default
)
367 (substring default
(match-end 0))
370 ;; Used in old LispM code.
371 (defalias 'common-lisp-mode
'lisp-mode
)
373 ;; This will do unless inf-lisp.el is loaded.
374 (defun lisp-eval-defun (&optional and-go
)
375 "Send the current defun to the Lisp process made by \\[run-lisp]."
377 (error "Process lisp does not exist"))
379 (defvar lisp-interaction-mode-map
380 (let ((map (make-sparse-keymap)))
381 (set-keymap-parent map lisp-mode-shared-map
)
382 (define-key map
"\e\C-x" 'eval-defun
)
383 (define-key map
"\e\C-q" 'indent-pp-sexp
)
384 (define-key map
"\e\t" 'lisp-complete-symbol
)
385 (define-key map
"\n" 'eval-print-last-sexp
)
387 "Keymap for Lisp Interaction mode.
388 All commands in `lisp-mode-shared-map' are inherited by this map.")
390 (defvar lisp-interaction-mode-abbrev-table lisp-mode-abbrev-table
)
391 (define-derived-mode lisp-interaction-mode emacs-lisp-mode
"Lisp Interaction"
392 "Major mode for typing and evaluating Lisp forms.
393 Like Lisp mode except that \\[eval-print-last-sexp] evals the Lisp expression
394 before point, and prints its value into the buffer, advancing point.
395 Note that printing is controlled by `eval-expression-print-length'
396 and `eval-expression-print-level'.
399 Delete converts tabs to spaces as it moves back.
400 Paragraphs are separated only by blank lines.
401 Semicolons start comments.
402 \\{lisp-interaction-mode-map}
403 Entry to this mode calls the value of `lisp-interaction-mode-hook'
404 if that value is non-nil.")
406 (defun eval-print-last-sexp ()
407 "Evaluate sexp before point; print value into current buffer.
409 Note that printing the result is controlled by the variables
410 `eval-expression-print-length' and `eval-expression-print-level',
413 (let ((standard-output (current-buffer)))
419 (defun last-sexp-setup-props (beg end value alt1 alt2
)
420 "Set up text properties for the output of `eval-last-sexp-1'.
421 BEG and END are the start and end of the output in current-buffer.
422 VALUE is the Lisp value printed, ALT1 and ALT2 are strings for the
423 alternative printed representations that can be displayed."
424 (let ((map (make-sparse-keymap)))
425 (define-key map
"\C-m" 'last-sexp-toggle-display
)
426 (define-key map
[down-mouse-2
] 'mouse-set-point
)
427 (define-key map
[mouse-2
] 'last-sexp-toggle-display
)
430 `(printed-value (,value
,alt1
,alt2
)
433 help-echo
"RET, mouse-2: toggle abbreviated display"
434 rear-nonsticky
(mouse-face keymap help-echo
438 (defun last-sexp-toggle-display (&optional arg
)
439 "Toggle between abbreviated and unabbreviated printed representations."
443 (let ((value (get-text-property (point) 'printed-value
)))
445 (let ((beg (or (previous-single-property-change (min (point-max) (1+ (point)))
448 (end (or (next-single-char-property-change (point) 'printed-value
) (point)))
449 (standard-output (current-buffer))
451 (delete-region beg end
)
452 (insert (nth 1 value
))
453 (last-sexp-setup-props beg
(point)
457 (goto-char (min (point-max) point
)))))))
459 (defun prin1-char (char)
460 "Return a string representing CHAR as a character rather than as an integer.
461 If CHAR is not a character, return nil."
464 (let ((c (event-basic-type char
))
465 (mods (event-modifiers char
))
467 ;; Prevent ?A from turning into ?\S-a.
468 (if (and (memq 'shift mods
)
469 (zerop (logand char ?\S-\^
@))
470 (not (let ((case-fold-search nil
))
471 (char-equal c
(upcase c
)))))
472 (setq c
(upcase c
) mods nil
))
473 ;; What string are we considering using?
480 (cond ((eq modif
'super
) "\\s-")
481 (t (string ?
\\ (upcase (aref (symbol-name modif
) 0)) ?-
))))
484 ((memq c
'(?\
; ?\( ?\) ?\{ ?\} ?\[ ?\] ?\" ?\' ?\\)) (string ?\\ c))
489 ;; Verify the string reads a CHAR, not to some other character.
490 ;; If it doesn't, return nil instead.
492 (= (car (read-from-string string
)) char
)
496 (defun eval-last-sexp-1 (eval-last-sexp-arg-internal)
497 "Evaluate sexp before point; print value in minibuffer.
498 With argument, print output into current buffer."
499 (let ((standard-output (if eval-last-sexp-arg-internal
(current-buffer) t
)))
501 (eval (let ((stab (syntax-table))
506 (with-syntax-table emacs-lisp-mode-syntax-table
507 ;; If this sexp appears to be enclosed in `...'
508 ;; then ignore the surrounding quotes.
510 (or (eq (following-char) ?
\')
511 (eq (preceding-char) ?
\')))
513 ;; If we were after `?\e' (or similar case),
514 ;; use the whole thing, not just the `e'.
515 (when (eq (preceding-char) ?
\\)
517 (when (eq (preceding-char) ??
)
521 (when (eq (preceding-char) ?
=)
524 (skip-chars-backward "0-9#=")
525 (setq labeled-p
(looking-at "\\(#[0-9]+=\\)+")))
530 ;; vladimir@cs.ualberta.ca 30-Jul-1997: skip ` in
531 ;; `variable' so that the value is returned, not the
533 (if (and ignore-quotes
534 (eq (following-char) ?
`))
536 (narrow-to-region (point-min) opoint
)
537 (setq expr
(read (current-buffer)))
538 ;; If it's an (interactive ...) form, it's more
539 ;; useful to show how an interactive call would
542 (eq (car expr
) 'interactive
)
544 (list 'call-interactively
551 (eval-last-sexp-print-value value
))))
553 (defun eval-last-sexp-print-value (value)
554 (let ((unabbreviated (let ((print-length nil
) (print-level nil
))
555 (prin1-to-string value
)))
556 (print-length eval-expression-print-length
)
557 (print-level eval-expression-print-level
)
562 (let ((str (eval-expression-print-format value
)))
563 (if str
(princ str
)))
565 (when (and (bufferp standard-output
)
566 (or (not (null print-length
))
567 (not (null print-level
)))
568 (not (string= unabbreviated
569 (buffer-substring-no-properties beg end
))))
570 (last-sexp-setup-props beg end value
572 (buffer-substring-no-properties beg end
))
576 (defvar eval-last-sexp-fake-value
(make-symbol "t"))
578 (defun eval-last-sexp (eval-last-sexp-arg-internal)
579 "Evaluate sexp before point; print value in minibuffer.
580 Interactively, with prefix argument, print output into current buffer."
582 (if (null eval-expression-debug-on-error
)
583 (eval-last-sexp-1 eval-last-sexp-arg-internal
)
584 (let ((old-value eval-last-sexp-fake-value
) new-value value
)
585 (let ((debug-on-error old-value
))
586 (setq value
(eval-last-sexp-1 eval-last-sexp-arg-internal
))
587 (setq new-value debug-on-error
))
588 (unless (eq old-value new-value
)
589 (setq debug-on-error new-value
))
592 (defun eval-defun-1 (form)
593 "Treat some expressions specially.
594 Reset the `defvar' and `defcustom' variables to the initial value.
595 Reinitialize the face according to the `defface' specification."
596 ;; The code in edebug-defun should be consistent with this, but not
597 ;; the same, since this gets a macroexpended form.
598 (cond ((not (listp form
))
600 ((and (eq (car form
) 'defvar
)
601 (cdr-safe (cdr-safe form
))
602 (boundp (cadr form
)))
603 ;; Force variable to be re-set.
604 `(progn (defvar ,(nth 1 form
) nil
,@(nthcdr 3 form
))
605 (setq ,(nth 1 form
) ,(nth 2 form
))))
606 ;; `defcustom' is now macroexpanded to
607 ;; `custom-declare-variable' with a quoted value arg.
608 ((and (eq (car form
) 'custom-declare-variable
)
609 (default-boundp (eval (nth 1 form
))))
610 ;; Force variable to be bound.
611 (set-default (eval (nth 1 form
)) (eval (nth 1 (nth 2 form
))))
613 ;; `defface' is macroexpanded to `custom-declare-face'.
614 ((eq (car form
) 'custom-declare-face
)
616 (put (eval (nth 1 form
)) 'face-defface-spec nil
)
617 (setq face-new-frame-defaults
618 (assq-delete-all (eval (nth 1 form
)) face-new-frame-defaults
))
620 ((eq (car form
) 'progn
)
621 (cons 'progn
(mapcar 'eval-defun-1
(cdr form
))))
624 (defun eval-defun-2 ()
625 "Evaluate defun that point is in or before.
626 The value is displayed in the minibuffer.
627 If the current defun is actually a call to `defvar',
628 then reset the variable using the initial value expression
629 even if the variable already has some other value.
630 \(Normally `defvar' does not change the variable's value
631 if it already has a value.\)
633 With argument, insert value in current buffer after the defun.
634 Return the result of evaluation."
636 (let ((debug-on-error eval-expression-debug-on-error
)
637 (print-length eval-expression-print-length
)
638 (print-level eval-expression-print-level
))
640 ;; Arrange for eval-region to "read" the (possibly) altered form.
641 ;; eval-region handles recording which file defines a function or
642 ;; variable. Re-written using `apply' to avoid capturing
643 ;; variables like `end'.
646 (let ((standard-output t
)
648 ;; Read the form from the buffer, and record where it ends.
653 (setq form
(read (current-buffer)))
655 ;; Alter the form if necessary.
656 (setq form
(eval-defun-1 (macroexpand form
)))
657 (list beg end standard-output
659 ;; Skipping to the end of the specified region
660 ;; will make eval-region return.
663 ;; The result of evaluation has been put onto VALUES. So return it.
666 (defun eval-defun (edebug-it)
667 "Evaluate the top-level form containing point, or after point.
669 If the current defun is actually a call to `defvar' or `defcustom',
670 evaluating it this way resets the variable using its initial value
671 expression even if the variable already has some other value.
672 \(Normally `defvar' and `defcustom' do not alter the value if there
675 With a prefix argument, instrument the code for Edebug.
677 If acting on a `defun' for FUNCTION, and the function was
678 instrumented, `Edebug: FUNCTION' is printed in the minibuffer. If not
679 instrumented, just FUNCTION is printed.
681 If not acting on a `defun', the result of evaluation is displayed in
682 the minibuffer. This display is controlled by the variables
683 `eval-expression-print-length' and `eval-expression-print-level',
688 (eval-defun (not edebug-all-defs
)))
690 (if (null eval-expression-debug-on-error
)
692 (let ((old-value (make-symbol "t")) new-value value
)
693 (let ((debug-on-error old-value
))
694 (setq value
(eval-defun-2))
695 (setq new-value debug-on-error
))
696 (unless (eq old-value new-value
)
697 (setq debug-on-error new-value
))
701 (defun lisp-comment-indent ()
702 (if (looking-at "\\s<\\s<\\s<")
704 (if (looking-at "\\s<\\s<")
705 (let ((tem (or (calculate-lisp-indent) (current-column))))
706 (if (listp tem
) (car tem
) tem
))
707 (skip-chars-backward " \t")
708 (max (if (bolp) 0 (1+ (current-column)))
711 ;; This function just forces a more costly detection of comments (using
712 ;; parse-partial-sexp from beginning-of-defun). I.e. It avoids the problem of
713 ;; taking a `;' inside a string started on another line for a comment starter.
714 ;; Note: `newcomment' gets it right now since we set comment-use-global-state
715 ;; so we could get rid of it. -stef
716 (defun lisp-mode-auto-fill ()
717 (if (> (current-column) (current-fill-column))
719 (nth 4 (syntax-ppss (point))))
721 (unless (and (boundp 'comment-auto-fill-only-comments
)
722 comment-auto-fill-only-comments
)
723 (let ((comment-start nil
) (comment-start-skip nil
))
726 (defvar lisp-indent-offset nil
727 "If non-nil, indent second line of expressions that many more columns.")
728 (defvar lisp-indent-function
'lisp-indent-function
)
730 (defun lisp-indent-line (&optional whole-exp
)
731 "Indent current line as Lisp code.
732 With argument, indent any additional lines of the same expression
733 rigidly along with this one."
735 (let ((indent (calculate-lisp-indent)) shift-amt end
736 (pos (- (point-max) (point)))
737 (beg (progn (beginning-of-line) (point))))
738 (skip-chars-forward " \t")
739 (if (or (null indent
) (looking-at "\\s<\\s<\\s<"))
740 ;; Don't alter indentation of a ;;; comment line
741 ;; or a line that starts in a string.
742 (goto-char (- (point-max) pos
))
743 (if (and (looking-at "\\s<") (not (looking-at "\\s<\\s<")))
744 ;; Single-semicolon comment lines should be indented
745 ;; as comment lines, not as code.
746 (progn (indent-for-comment) (forward-char -
1))
747 (if (listp indent
) (setq indent
(car indent
)))
748 (setq shift-amt
(- indent
(current-column)))
749 (if (zerop shift-amt
)
751 (delete-region beg
(point))
753 ;; If initial point was within line's indentation,
754 ;; position after the indentation. Else stay at same point in text.
755 (if (> (- (point-max) pos
) (point))
756 (goto-char (- (point-max) pos
)))
757 ;; If desired, shift remaining lines of expression the same amount.
758 (and whole-exp
(not (zerop shift-amt
))
767 (indent-code-rigidly beg end shift-amt
)))))
769 (defvar calculate-lisp-indent-last-sexp
)
771 (defun calculate-lisp-indent (&optional parse-start
)
772 "Return appropriate indentation for current line as Lisp code.
773 In usual case returns an integer: the column to indent to.
774 If the value is nil, that means don't change the indentation
775 because the line starts inside a string.
777 The value can also be a list of the form (COLUMN CONTAINING-SEXP-START).
778 This means that following lines at the same level of indentation
779 should not necessarily be indented the same as this line.
780 Then COLUMN is the column to indent to, and CONTAINING-SEXP-START
781 is the buffer position of the start of the containing expression."
784 (let ((indent-point (point))
786 ;; setting this to a number inhibits calling hook
789 calculate-lisp-indent-last-sexp containing-sexp
)
791 (goto-char parse-start
)
792 (beginning-of-defun))
793 ;; Find outermost containing sexp
794 (while (< (point) indent-point
)
795 (setq state
(parse-partial-sexp (point) indent-point
0)))
796 ;; Find innermost containing sexp
799 (> (setq paren-depth
(elt state
0)) 0))
801 (setq calculate-lisp-indent-last-sexp
(elt state
2))
802 (setq containing-sexp
(elt state
1))
803 ;; Position following last unclosed open.
804 (goto-char (1+ containing-sexp
))
805 ;; Is there a complete sexp since then?
806 (if (and calculate-lisp-indent-last-sexp
807 (> calculate-lisp-indent-last-sexp
(point)))
808 ;; Yes, but is there a containing sexp after that?
809 (let ((peek (parse-partial-sexp calculate-lisp-indent-last-sexp
811 (if (setq retry
(car (cdr peek
))) (setq state peek
)))))
814 ;; Innermost containing sexp found
815 (goto-char (1+ containing-sexp
))
816 (if (not calculate-lisp-indent-last-sexp
)
817 ;; indent-point immediately follows open paren.
819 (setq desired-indent
(current-column))
820 ;; Find the start of first element of containing sexp.
821 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp
0 t
)
822 (cond ((looking-at "\\s(")
823 ;; First element of containing sexp is a list.
824 ;; Indent under that list.
826 ((> (save-excursion (forward-line 1) (point))
827 calculate-lisp-indent-last-sexp
)
828 ;; This is the first line to start within the containing sexp.
829 ;; It's almost certainly a function call.
830 (if (= (point) calculate-lisp-indent-last-sexp
)
831 ;; Containing sexp has nothing before this line
832 ;; except the first element. Indent under that element.
834 ;; Skip the first element, find start of second (the first
835 ;; argument of the function call) and indent under.
836 (progn (forward-sexp 1)
837 (parse-partial-sexp (point)
838 calculate-lisp-indent-last-sexp
840 (backward-prefix-chars))
842 ;; Indent beneath first sexp on same line as
843 ;; `calculate-lisp-indent-last-sexp'. Again, it's
844 ;; almost certainly a function call.
845 (goto-char calculate-lisp-indent-last-sexp
)
847 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp
849 (backward-prefix-chars)))))
850 ;; Point is at the point to indent under unless we are inside a string.
851 ;; Call indentation hook except when overridden by lisp-indent-offset
852 ;; or if the desired indentation has already been computed.
853 (let ((normal-indent (current-column)))
855 ;; Inside a string, don't change indentation.
857 ((and (integerp lisp-indent-offset
) containing-sexp
)
858 ;; Indent by constant offset
859 (goto-char containing-sexp
)
860 (+ (current-column) lisp-indent-offset
))
862 ((and (boundp 'lisp-indent-function
)
865 (or (funcall lisp-indent-function indent-point state
)
870 (defun lisp-indent-function (indent-point state
)
871 "This function is the normal value of the variable `lisp-indent-function'.
872 It is used when indenting a line within a function call, to see if the
873 called function says anything special about how to indent the line.
875 INDENT-POINT is the position where the user typed TAB, or equivalent.
876 Point is located at the point to indent under (for default indentation);
877 STATE is the `parse-partial-sexp' state for that position.
879 If the current line is in a call to a Lisp function
880 which has a non-nil property `lisp-indent-function',
881 that specifies how to do the indentation. The property value can be
882 * `defun', meaning indent `defun'-style;
883 * an integer N, meaning indent the first N arguments specially
884 like ordinary function arguments and then indent any further
885 aruments like a body;
886 * a function to call just as this function was called.
887 If that function returns nil, that means it doesn't specify
890 This function also returns nil meaning don't specify the indentation."
891 (let ((normal-indent (current-column)))
892 (goto-char (1+ (elt state
1)))
893 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp
0 t
)
894 (if (and (elt state
2)
895 (not (looking-at "\\sw\\|\\s_")))
896 ;; car of form doesn't seem to be a symbol
898 (if (not (> (save-excursion (forward-line 1) (point))
899 calculate-lisp-indent-last-sexp
))
900 (progn (goto-char calculate-lisp-indent-last-sexp
)
902 (parse-partial-sexp (point)
903 calculate-lisp-indent-last-sexp
0 t
)))
904 ;; Indent under the list or under the first sexp on the same
905 ;; line as calculate-lisp-indent-last-sexp. Note that first
906 ;; thing on that line has to be complete sexp since we are
907 ;; inside the innermost containing sexp.
908 (backward-prefix-chars)
910 (let ((function (buffer-substring (point)
911 (progn (forward-sexp 1) (point))))
913 (setq method
(or (get (intern-soft function
) 'lisp-indent-function
)
914 (get (intern-soft function
) 'lisp-indent-hook
)))
915 (cond ((or (eq method
'defun
)
917 (> (length function
) 3)
918 (string-match "\\`def" function
)))
919 (lisp-indent-defform state indent-point
))
921 (lisp-indent-specform method state
922 indent-point normal-indent
))
924 (funcall method state indent-point
)))))))
926 (defvar lisp-body-indent
2
927 "Number of columns to indent the second line of a `(def...)' form.")
929 (defun lisp-indent-specform (count state indent-point normal-indent
)
930 (let ((containing-form-start (elt state
1))
932 body-indent containing-form-column
)
933 ;; Move to the start of containing form, calculate indentation
934 ;; to use for non-distinguished forms (> count), and move past the
935 ;; function symbol. lisp-indent-function guarantees that there is at
936 ;; least one word or symbol character following open paren of containing
938 (goto-char containing-form-start
)
939 (setq containing-form-column
(current-column))
940 (setq body-indent
(+ lisp-body-indent containing-form-column
))
943 ;; Now find the start of the last form.
944 (parse-partial-sexp (point) indent-point
1 t
)
945 (while (and (< (point) indent-point
)
948 (setq count
(1- count
))
950 (parse-partial-sexp (point) indent-point
1 t
))
952 ;; Point is sitting on first character of last (or count) sexp.
954 ;; A distinguished form. If it is the first or second form use double
955 ;; lisp-body-indent, else normal indent. With lisp-body-indent bound
956 ;; to 2 (the default), this just happens to work the same with if as
957 ;; the older code, but it makes unwind-protect, condition-case,
958 ;; with-output-to-temp-buffer, et. al. much more tasteful. The older,
959 ;; less hacked, behavior can be obtained by replacing below with
960 ;; (list normal-indent containing-form-start).
961 (if (<= (- i count
) 1)
962 (list (+ containing-form-column
(* 2 lisp-body-indent
))
963 containing-form-start
)
964 (list normal-indent containing-form-start
))
965 ;; A non-distinguished form. Use body-indent if there are no
966 ;; distinguished forms and this is the first undistinguished form,
967 ;; or if this is the first undistinguished form and the preceding
968 ;; distinguished form has indentation at least as great as body-indent.
969 (if (or (and (= i
0) (= count
0))
970 (and (= count
0) (<= body-indent normal-indent
)))
974 (defun lisp-indent-defform (state indent-point
)
975 (goto-char (car (cdr state
)))
977 (if (> (point) (car (cdr (cdr state
))))
979 (goto-char (car (cdr state
)))
980 (+ lisp-body-indent
(current-column)))))
983 ;; (put 'progn 'lisp-indent-function 0), say, causes progn to be indented
984 ;; like defun if the first form is placed on the next line, otherwise
985 ;; it is indented like any other form (i.e. forms line up under first).
987 (put 'lambda
'lisp-indent-function
'defun
)
988 (put 'autoload
'lisp-indent-function
'defun
)
989 (put 'progn
'lisp-indent-function
0)
990 (put 'prog1
'lisp-indent-function
1)
991 (put 'prog2
'lisp-indent-function
2)
992 (put 'save-excursion
'lisp-indent-function
0)
993 (put 'save-window-excursion
'lisp-indent-function
0)
994 (put 'save-selected-window
'lisp-indent-function
0)
995 (put 'save-restriction
'lisp-indent-function
0)
996 (put 'save-match-data
'lisp-indent-function
0)
997 (put 'save-current-buffer
'lisp-indent-function
0)
998 (put 'with-current-buffer
'lisp-indent-function
1)
999 (put 'combine-after-change-calls
'lisp-indent-function
0)
1000 (put 'with-output-to-string
'lisp-indent-function
0)
1001 (put 'with-temp-file
'lisp-indent-function
1)
1002 (put 'with-temp-buffer
'lisp-indent-function
0)
1003 (put 'with-temp-message
'lisp-indent-function
1)
1004 (put 'with-syntax-table
'lisp-indent-function
1)
1005 (put 'let
'lisp-indent-function
1)
1006 (put 'let
* 'lisp-indent-function
1)
1007 (put 'while
'lisp-indent-function
1)
1008 (put 'if
'lisp-indent-function
2)
1009 (put 'read-if
'lisp-indent-function
2)
1010 (put 'catch
'lisp-indent-function
1)
1011 (put 'condition-case
'lisp-indent-function
2)
1012 (put 'unwind-protect
'lisp-indent-function
1)
1013 (put 'with-output-to-temp-buffer
'lisp-indent-function
1)
1014 (put 'eval-after-load
'lisp-indent-function
1)
1015 (put 'dolist
'lisp-indent-function
1)
1016 (put 'dotimes
'lisp-indent-function
1)
1017 (put 'when
'lisp-indent-function
1)
1018 (put 'unless
'lisp-indent-function
1)
1020 (defun indent-sexp (&optional endpos
)
1021 "Indent each line of the list starting just after point.
1022 If optional arg ENDPOS is given, indent each line, stopping when
1023 ENDPOS is encountered."
1025 (let ((indent-stack (list nil
))
1027 ;; If ENDPOS is non-nil, use nil as STARTING-POINT
1028 ;; so that calculate-lisp-indent will find the beginning of
1029 ;; the defun we are in.
1030 ;; If ENDPOS is nil, it is safe not to scan before point
1031 ;; since every line we indent is more deeply nested than point is.
1032 (starting-point (if endpos nil
(point)))
1033 (last-point (point))
1034 last-depth bol outer-loop-done inner-loop-done state this-indent
)
1036 ;; Get error now if we don't have a complete sexp after point.
1037 (save-excursion (forward-sexp 1)))
1039 (setq outer-loop-done nil
)
1040 (while (if endpos
(< (point) endpos
)
1041 (not outer-loop-done
))
1042 (setq last-depth next-depth
1043 inner-loop-done nil
)
1044 ;; Parse this line so we can learn the state
1045 ;; to indent the next line.
1046 ;; This inner loop goes through only once
1047 ;; unless a line ends inside a string.
1048 (while (and (not inner-loop-done
)
1049 (not (setq outer-loop-done
(eobp))))
1050 (setq state
(parse-partial-sexp (point) (progn (end-of-line) (point))
1052 (setq next-depth
(car state
))
1053 ;; If the line contains a comment other than the sort
1054 ;; that is indented like code,
1055 ;; indent it now with indent-for-comment.
1056 ;; Comments indented like code are right already.
1057 ;; In any case clear the in-comment flag in the state
1058 ;; because parse-partial-sexp never sees the newlines.
1059 (if (car (nthcdr 4 state
))
1060 (progn (indent-for-comment)
1062 (setcar (nthcdr 4 state
) nil
)))
1063 ;; If this line ends inside a string,
1064 ;; go straight to next line, remaining within the inner loop,
1065 ;; and turn off the \-flag.
1066 (if (car (nthcdr 3 state
))
1069 (setcar (nthcdr 5 state
) nil
))
1070 (setq inner-loop-done t
)))
1074 (setq indent-stack
(nconc indent-stack
1075 (make-list (- next-depth
) nil
))
1076 last-depth
(- last-depth next-depth
)
1078 (or outer-loop-done endpos
1079 (setq outer-loop-done
(<= next-depth
0)))
1082 (while (> last-depth next-depth
)
1083 (setq indent-stack
(cdr indent-stack
)
1084 last-depth
(1- last-depth
)))
1085 (while (< last-depth next-depth
)
1086 (setq indent-stack
(cons nil indent-stack
)
1087 last-depth
(1+ last-depth
)))
1088 ;; Now go to the next line and indent it according
1089 ;; to what we learned from parsing the previous one.
1092 (skip-chars-forward " \t")
1093 ;; But not if the line is blank, or just a comment
1094 ;; (except for double-semi comments; indent them as usual).
1095 (if (or (eobp) (looking-at "\\s<\\|\n"))
1097 (if (and (car indent-stack
)
1098 (>= (car indent-stack
) 0))
1099 (setq this-indent
(car indent-stack
))
1100 (let ((val (calculate-lisp-indent
1101 (if (car indent-stack
) (- (car indent-stack
))
1104 (setq this-indent val
)
1106 (setcar indent-stack
1107 (setq this-indent val
))
1108 (setcar indent-stack
(- (car (cdr val
))))
1109 (setq this-indent
(car val
))))))
1110 (if (and this-indent
(/= (current-column) this-indent
))
1111 (progn (delete-region bol
(point))
1112 (indent-to this-indent
)))))
1114 (setq outer-loop-done
(= (point) last-point
))
1115 (setq last-point
(point)))))))
1117 (defun lisp-indent-region (start end
)
1118 "Indent every line whose first char is between START and END inclusive."
1120 (let ((endmark (copy-marker end
)))
1122 (and (bolp) (not (eolp))
1124 (indent-sexp endmark
)
1125 (set-marker endmark nil
))))
1127 (defun indent-pp-sexp (&optional arg
)
1128 "Indent each line of the list or, with prefix ARG, pretty-printify the list."
1133 (narrow-to-region (point) (progn (forward-sexp 1) (point)))
1135 (goto-char (point-max))
1136 (if (eq (char-before) ?
\n)
1137 (delete-char -
1)))))
1140 ;;;; Lisp paragraph filling commands.
1142 (defcustom emacs-lisp-docstring-fill-column
65
1143 "Value of `fill-column' to use when filling a docstring.
1144 Any non-integer value means do not use a different value of
1145 `fill-column' when filling docstrings."
1146 :type
'(choice (integer)
1147 (const :tag
"Use the current `fill-column'" t
))
1150 (defun lisp-fill-paragraph (&optional justify
)
1151 "Like \\[fill-paragraph], but handle Emacs Lisp comments and docstrings.
1152 If any of the current line is a comment, fill the comment or the
1153 paragraph of it that point is in, preserving the comment's indentation
1154 and initial semicolons."
1156 (or (fill-comment-paragraph justify
)
1157 ;; Since fill-comment-paragraph returned nil, that means we're not in
1158 ;; a comment: Point is on a program line; we are interested
1159 ;; particularly in docstring lines.
1161 ;; We bind `paragraph-start' and `paragraph-separate' temporarily. They
1162 ;; are buffer-local, but we avoid changing them so that they can be set
1163 ;; to make `forward-paragraph' and friends do something the user wants.
1165 ;; `paragraph-start': The `(' in the character alternative and the
1166 ;; left-singlequote plus `(' sequence after the \\| alternative prevent
1167 ;; sexps and backquoted sexps that follow a docstring from being filled
1168 ;; with the docstring. This setting has the consequence of inhibiting
1169 ;; filling many program lines that are not docstrings, which is sensible,
1170 ;; because the user probably asked to fill program lines by accident, or
1171 ;; expecting indentation (perhaps we should try to do indenting in that
1172 ;; case). The `;' and `:' stop the paragraph being filled at following
1173 ;; comment lines and at keywords (e.g., in `defcustom'). Left parens are
1174 ;; escaped to keep font-locking, filling, & paren matching in the source
1177 ;; `paragraph-separate': A clever regexp distinguishes the first line of
1178 ;; a docstring and identifies it as a paragraph separator, so that it
1179 ;; won't be filled. (Since the first line of documentation stands alone
1180 ;; in some contexts, filling should not alter the contents the author has
1181 ;; chosen.) Only the first line of a docstring begins with whitespace
1182 ;; and a quotation mark and ends with a period or (rarely) a comma.
1184 ;; The `fill-column' is temporarily bound to
1185 ;; `emacs-lisp-docstring-fill-column' if that value is an integer.
1186 (let ((paragraph-start (concat paragraph-start
1187 "\\|\\s-*\\([(;:\"]\\|`(\\|#'(\\)"))
1189 (concat paragraph-separate
"\\|\\s-*\".*[,\\.]$"))
1190 (fill-column (if (integerp emacs-lisp-docstring-fill-column
)
1191 emacs-lisp-docstring-fill-column
1193 (fill-paragraph justify
))
1194 ;; Never return nil.
1197 (defun indent-code-rigidly (start end arg
&optional nochange-regexp
)
1198 "Indent all lines of code, starting in the region, sideways by ARG columns.
1199 Does not affect lines starting inside comments or strings, assuming that
1200 the start of the region is not inside them.
1202 Called from a program, takes args START, END, COLUMNS and NOCHANGE-REGEXP.
1203 The last is a regexp which, if matched at the beginning of a line,
1204 means don't indent that line."
1205 (interactive "r\np")
1209 (setq end
(point-marker))
1212 (setq state
(parse-partial-sexp (point)
1214 (forward-line 1) (point))
1216 (while (< (point) end
)
1217 (or (car (nthcdr 3 state
))
1218 (and nochange-regexp
1219 (looking-at nochange-regexp
))
1220 ;; If line does not start in string, indent it
1221 (let ((indent (current-indentation)))
1222 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
1224 (indent-to (max 0 (+ indent arg
)) 0))))
1225 (setq state
(parse-partial-sexp (point)
1227 (forward-line 1) (point))
1230 (provide 'lisp-mode
)
1232 ;; arch-tag: 414c7f93-c245-4b77-8ed5-ed05ef7ff1bf
1233 ;;; lisp-mode.el ends here