1 ;;; lisp-mode.el --- Lisp mode, and its idiosyncratic commands
3 ;; Copyright (C) 1985, 1986, 1999, 2000, 2001, 2003 Free Software Foundation, Inc.
6 ;; Keywords: lisp, languages
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; The base major mode for editing Lisp code (used also for Emacs Lisp).
28 ;; This mode is documented in the Emacs manual.
32 (defvar lisp-mode-abbrev-table nil
)
34 (defvar emacs-lisp-mode-syntax-table
35 (let ((table (make-syntax-table)))
38 (modify-syntax-entry i
"_ " table
)
42 (modify-syntax-entry i
"_ " table
)
46 (modify-syntax-entry i
"_ " table
)
50 (modify-syntax-entry i
"_ " table
)
52 (modify-syntax-entry ?
" " table
)
53 (modify-syntax-entry ?
\t " " table
)
54 (modify-syntax-entry ?
\f " " table
)
55 (modify-syntax-entry ?
\n "> " table
)
56 ;; Give CR the same syntax as newline, for selective-display.
57 (modify-syntax-entry ?\^m
"> " table
)
58 (modify-syntax-entry ?\
; "< " table)
59 (modify-syntax-entry ?
` "' " table
)
60 (modify-syntax-entry ?
' "' " table
)
61 (modify-syntax-entry ?
, "' " table
)
62 (modify-syntax-entry ?
@ "' " table
)
63 ;; Used to be singlequote; changed for flonums.
64 (modify-syntax-entry ?.
"_ " table
)
65 (modify-syntax-entry ?
# "' " table
)
66 (modify-syntax-entry ?
\" "\" " table
)
67 (modify-syntax-entry ?
\\ "\\ " table
)
68 (modify-syntax-entry ?\
( "() " table
)
69 (modify-syntax-entry ?\
) ")( " table
)
70 (modify-syntax-entry ?\
[ "(] " table
)
71 (modify-syntax-entry ?\
] ")[ " table
))
74 (defvar lisp-mode-syntax-table
75 (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table
)))
76 (modify-syntax-entry ?\
[ "_ " table
)
77 (modify-syntax-entry ?\
] "_ " table
)
78 (modify-syntax-entry ?
# "' 14bn" table
)
79 (modify-syntax-entry ?|
"\" 23b" table
)
82 (define-abbrev-table 'lisp-mode-abbrev-table
())
84 (defvar lisp-imenu-generic-expression
87 (purecopy (concat "^\\s-*("
90 '("defun" "defun*" "defsubst" "defmacro"
91 "defadvice" "define-skeleton"
92 "define-minor-mode" "define-derived-mode"
93 "define-compiler-macro" "define-modify-macro"
94 "defsetf" "define-setf-expander"
95 "define-method-combination"
96 "defgeneric" "defmethod") t
))
97 "\\s-+\\(\\sw\\(\\sw\\|\\s_\\)+\\)"))
99 (list (purecopy "Variables")
100 (purecopy (concat "^\\s-*("
103 '("defvar" "defconst" "defconstant" "defcustom"
104 "defparameter" "define-symbol-macro") t
))
105 "\\s-+\\(\\sw\\(\\sw\\|\\s_\\)+\\)"))
107 (list (purecopy "Types")
108 (purecopy (concat "^\\s-*("
111 '("defgroup" "deftheme" "deftype" "defstruct"
112 "defclass" "define-condition" "define-widget"
113 "defface" "defpackage") t
))
114 "\\s-+'?\\(\\sw\\(\\sw\\|\\s_\\)+\\)"))
117 "Imenu generic expression for Lisp mode. See `imenu-generic-expression'.")
119 ;; This was originally in autoload.el and is still used there.
120 (put 'autoload
'doc-string-elt
3)
121 (put 'defun
'doc-string-elt
3)
122 (put 'defun
* 'doc-string-elt
3)
123 (put 'defvar
'doc-string-elt
3)
124 (put 'defcustom
'doc-string-elt
3)
125 (put 'deftheme
'doc-string-elt
2)
126 (put 'defconst
'doc-string-elt
3)
127 (put 'defmacro
'doc-string-elt
3)
128 (put 'defmacro
* 'doc-string-elt
3)
129 (put 'defsubst
'doc-string-elt
3)
130 (put 'define-skeleton
'doc-string-elt
2)
131 (put 'define-derived-mode
'doc-string-elt
4)
132 (put 'easy-mmode-define-minor-mode
'doc-string-elt
2)
133 (put 'define-minor-mode
'doc-string-elt
2)
134 (put 'define-generic-mode
'doc-string-elt
7)
135 ;; define-global-mode has no explicit docstring.
136 (put 'easy-mmode-define-global-mode
'doc-string-elt
0)
137 (put 'define-ibuffer-filter
'doc-string-elt
2)
138 (put 'define-ibuffer-op
'doc-string-elt
3)
139 (put 'define-ibuffer-sorter
'doc-string-elt
2)
141 (defun lisp-font-lock-syntactic-face-function (state)
143 (if (and (eq (nth 0 state
) 1)
144 ;; This might be a docstring.
147 (goto-char (nth 8 state
))
149 (while (and (not (bobp))
150 (progn (backward-sexp 1) (setq n
(1+ n
)))))
153 (let ((sym (intern-soft
155 (point) (progn (forward-sexp 1) (point))))))
156 (eq n
(or (get sym
'doc-string-elt
) 3)))))))
158 font-lock-string-face
)
159 font-lock-comment-face
))
161 ;; The LISP-SYNTAX argument is used by code in inf-lisp.el and is
162 ;; (uselessly) passed from pp.el, chistory.el, gnus-kill.el and score-mode.el
163 (defun lisp-mode-variables (&optional lisp-syntax
)
165 (set-syntax-table lisp-mode-syntax-table
))
166 (setq local-abbrev-table lisp-mode-abbrev-table
)
167 (make-local-variable 'paragraph-ignore-fill-prefix
)
168 (setq paragraph-ignore-fill-prefix t
)
169 (make-local-variable 'fill-paragraph-function
)
170 (setq fill-paragraph-function
'lisp-fill-paragraph
)
171 ;; Adaptive fill mode gets in the way of auto-fill,
172 ;; and should make no difference for explicit fill
173 ;; because lisp-fill-paragraph should do the job.
174 ;; I believe that newcomment's auto-fill code properly deals with it -stef
175 ;;(set (make-local-variable 'adaptive-fill-mode) nil)
176 (make-local-variable 'normal-auto-fill-function
)
177 (setq normal-auto-fill-function
'lisp-mode-auto-fill
)
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
";;;;* \\|(")
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 (make-local-variable 'comment-indent-function
)
199 (setq comment-indent-function
'lisp-comment-indent
)
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")) beginning-of-defun
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."
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
[menu-bar
] (make-sparse-keymap))
245 (define-key emacs-lisp-mode-map
[menu-bar emacs-lisp
]
246 (cons "Emacs-Lisp" map
))
247 (define-key map
[edebug-defun
]
248 '("Instrument Function for Debugging" . edebug-defun
))
249 (define-key map
[byte-recompile
]
250 '("Byte-recompile Directory..." . byte-recompile-directory
))
251 (define-key map
[emacs-byte-compile-and-load
]
252 '("Byte-compile And Load" . emacs-lisp-byte-compile-and-load
))
253 (define-key map
[byte-compile
]
254 '("Byte-compile This File" . emacs-lisp-byte-compile
))
255 (define-key map
[separator-eval
] '("--"))
256 (define-key map
[eval-buffer
] '("Evaluate Buffer" . eval-current-buffer
))
257 (define-key map
[eval-region
] '("Evaluate Region" . eval-region
))
258 (define-key map
[eval-sexp
] '("Evaluate Last S-expression" . eval-last-sexp
))
259 (define-key map
[separator-format
] '("--"))
260 (define-key map
[comment-region
] '("Comment Out Region" . comment-region
))
261 (define-key map
[indent-region
] '("Indent Region" . indent-region
))
262 (define-key map
[indent-line
] '("Indent Line" . lisp-indent-line
))
263 (put 'eval-region
'menu-enable
'mark-active
)
264 (put 'comment-region
'menu-enable
'mark-active
)
265 (put 'indent-region
'menu-enable
'mark-active
)))
267 (defun emacs-lisp-byte-compile ()
268 "Byte compile the file containing the current buffer."
271 (byte-compile-file buffer-file-name
)
272 (error "The buffer must be saved in a file first")))
274 (defun emacs-lisp-byte-compile-and-load ()
275 "Byte-compile the current file (if it has changed), then load compiled code."
278 (error "The buffer must be saved in a file first"))
280 ;; Recompile if file or buffer has changed since last compilation.
281 (if (and (buffer-modified-p)
282 (y-or-n-p (format "Save buffer %s first? " (buffer-name))))
284 (let ((compiled-file-name (byte-compile-dest-file buffer-file-name
)))
285 (if (file-newer-than-file-p compiled-file-name buffer-file-name
)
286 (load-file compiled-file-name
)
287 (byte-compile-file buffer-file-name t
))))
289 (defcustom emacs-lisp-mode-hook nil
290 "Hook run when entering Emacs Lisp mode."
291 :options
'(turn-on-eldoc-mode imenu-add-menubar-index checkdoc-minor-mode
)
295 (defcustom lisp-mode-hook nil
296 "Hook run when entering Lisp mode."
297 :options
'(imenu-add-menubar-index)
301 (defcustom lisp-interaction-mode-hook nil
302 "Hook run when entering Lisp Interaction mode."
303 :options
'(turn-on-eldoc-mode)
307 (defun emacs-lisp-mode ()
308 "Major mode for editing Lisp code to run in Emacs.
310 Delete converts tabs to spaces as it moves back.
311 Blank lines separate paragraphs. Semicolons start comments.
312 \\{emacs-lisp-mode-map}
313 Entry to this mode calls the value of `emacs-lisp-mode-hook'
314 if that value is non-nil."
316 (kill-all-local-variables)
317 (use-local-map emacs-lisp-mode-map
)
318 (set-syntax-table emacs-lisp-mode-syntax-table
)
319 (setq major-mode
'emacs-lisp-mode
)
320 (setq mode-name
"Emacs-Lisp")
321 (lisp-mode-variables)
322 (setq imenu-case-fold-search nil
)
323 (run-hooks 'emacs-lisp-mode-hook
))
324 (put 'emacs-lisp-mode
'custom-mode-group
'lisp
)
326 (defvar lisp-mode-map
327 (let ((map (make-sparse-keymap)))
328 (set-keymap-parent map lisp-mode-shared-map
)
329 (define-key map
"\e\C-x" 'lisp-eval-defun
)
330 (define-key map
"\C-c\C-z" 'run-lisp
)
332 "Keymap for ordinary Lisp mode.
333 All commands in `lisp-mode-shared-map' are inherited by this map.")
336 "Major mode for editing Lisp code for Lisps other than GNU Emacs Lisp.
338 Delete converts tabs to spaces as it moves back.
339 Blank lines separate paragraphs. Semicolons start comments.
341 Note that `run-lisp' may be used either to start an inferior Lisp job
342 or to switch back to an existing one.
344 Entry to this mode calls the value of `lisp-mode-hook'
345 if that value is non-nil."
347 (kill-all-local-variables)
348 (use-local-map lisp-mode-map
)
349 (setq major-mode
'lisp-mode
)
350 (setq mode-name
"Lisp")
351 (lisp-mode-variables)
352 (make-local-variable 'comment-start-skip
)
353 (setq comment-start-skip
354 "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\)\\(;+\\|#|\\) *")
355 (make-local-variable 'font-lock-keywords-case-fold-search
)
356 (setq font-lock-keywords-case-fold-search t
)
357 (setq imenu-case-fold-search t
)
358 (set-syntax-table lisp-mode-syntax-table
)
359 (run-hooks 'lisp-mode-hook
))
361 ;; This will do unless inf-lisp.el is loaded.
362 (defun lisp-eval-defun (&optional and-go
)
363 "Send the current defun to the Lisp process made by \\[run-lisp]."
365 (error "Process lisp does not exist"))
367 (defvar lisp-interaction-mode-map
368 (let ((map (make-sparse-keymap)))
369 (set-keymap-parent map lisp-mode-shared-map
)
370 (define-key map
"\e\C-x" 'eval-defun
)
371 (define-key map
"\e\t" 'lisp-complete-symbol
)
372 (define-key map
"\n" 'eval-print-last-sexp
)
374 "Keymap for Lisp Interaction mode.
375 All commands in `lisp-mode-shared-map' are inherited by this map.")
377 (defvar lisp-interaction-mode-abbrev-table lisp-mode-abbrev-table
)
378 (define-derived-mode lisp-interaction-mode emacs-lisp-mode
"Lisp Interaction"
379 "Major mode for typing and evaluating Lisp forms.
380 Like Lisp mode except that \\[eval-print-last-sexp] evals the Lisp expression
381 before point, and prints its value into the buffer, advancing point.
382 Note that printing is controlled by `eval-expression-print-length'
383 and `eval-expression-print-level'.
386 Delete converts tabs to spaces as it moves back.
387 Paragraphs are separated only by blank lines.
388 Semicolons start comments.
389 \\{lisp-interaction-mode-map}
390 Entry to this mode calls the value of `lisp-interaction-mode-hook'
391 if that value is non-nil.")
393 (defun eval-print-last-sexp ()
394 "Evaluate sexp before point; print value into current buffer.
396 Note that printing the result is controlled by the variables
397 `eval-expression-print-length' and `eval-expression-print-level',
400 (let ((standard-output (current-buffer)))
406 (defun last-sexp-setup-props (beg end value alt1 alt2
)
407 "Set up text properties for the output of `eval-last-sexp-1'.
408 BEG and END are the start and end of the output in current-buffer.
409 VALUE is the Lisp value printed, ALT1 and ALT2 are strings for the
410 alternative printed representations that can be displayed."
411 (let ((map (make-sparse-keymap)))
412 (define-key map
"\C-m" 'last-sexp-toggle-display
)
413 (define-key map
[down-mouse-2
] 'mouse-set-point
)
414 (define-key map
[mouse-2
] 'last-sexp-toggle-display
)
417 `(printed-value (,value
,alt1
,alt2
)
420 help-echo
"RET, mouse-2: toggle abbreviated display"
421 rear-nonsticky
(mouse-face keymap help-echo
425 (defun last-sexp-toggle-display (&optional arg
)
426 "Toggle between abbreviated and unabbreviated printed representations."
428 ;; Normally this command won't be called at end of line.
429 ;; But when the end of the line is also the end of the buffer,
430 ;; it does get called. For consistency, pretend it was not called.
433 (let ((value (get-text-property (point) 'printed-value
)))
435 (let ((beg (or (previous-single-property-change (min (point-max) (1+ (point)))
438 (end (or (next-single-char-property-change (point) 'printed-value
) (point)))
439 (standard-output (current-buffer))
441 (delete-region beg end
)
442 (insert (nth 1 value
))
443 (last-sexp-setup-props beg
(point)
447 (goto-char (min (point-max) point
)))))))
449 (defun prin1-char (char)
450 "Return a string representing CHAR as a character rather than as an integer.
451 If CHAR is not a character, return nil."
453 (char-valid-p (event-basic-type char
))
458 (cond ((eq modif
'super
) "\\s-")
459 (t (string ?
\\ (upcase (aref (symbol-name modif
) 0)) ?-
))))
460 (event-modifiers char
) "")
461 (string (event-basic-type char
)))))
463 (defun eval-last-sexp-1 (eval-last-sexp-arg-internal)
464 "Evaluate sexp before point; print value in minibuffer.
465 With argument, print output into current buffer."
466 (let ((standard-output (if eval-last-sexp-arg-internal
(current-buffer) t
)))
468 (eval (let ((stab (syntax-table))
472 (with-syntax-table emacs-lisp-mode-syntax-table
473 ;; If this sexp appears to be enclosed in `...'
474 ;; then ignore the surrounding quotes.
476 (or (eq (following-char) ?
\')
477 (eq (preceding-char) ?
\')))
479 ;; If we were after `?\e' (or similar case),
480 ;; use the whole thing, not just the `e'.
481 (when (eq (preceding-char) ?
\\)
483 (when (eq (preceding-char) ??
)
487 (when (eq (preceding-char) ?
=)
490 (skip-chars-backward "0-9#=")
491 (setq labeled-p
(looking-at "\\(#[0-9]+=\\)+")))
496 ;; vladimir@cs.ualberta.ca 30-Jul-1997: skip ` in
497 ;; `variable' so that the value is returned, not the
499 (if (and ignore-quotes
500 (eq (following-char) ?
`))
502 (narrow-to-region (point-min) opoint
)
503 (setq expr
(read (current-buffer)))
504 ;; If it's an (interactive ...) form, it's more
505 ;; useful to show how an interactive call would
508 (eq (car expr
) 'interactive
)
510 (list 'call-interactively
517 (let ((unabbreviated (let ((print-length nil
) (print-level nil
))
518 (prin1-to-string value
)))
519 (print-length eval-expression-print-length
)
520 (print-level eval-expression-print-level
)
521 (char-string (prin1-char value
))
526 (if (and (eq standard-output t
) char-string
)
527 (princ (concat " = " char-string
)))
529 (when (and (bufferp standard-output
)
530 (or (not (null print-length
))
531 (not (null print-level
)))
532 (not (string= unabbreviated
533 (buffer-substring-no-properties beg end
))))
534 (last-sexp-setup-props beg end value
536 (buffer-substring-no-properties beg end
))
540 (defun eval-last-sexp (eval-last-sexp-arg-internal)
541 "Evaluate sexp before point; print value in minibuffer.
542 Interactively, with prefix argument, print output into current buffer."
544 (if (null eval-expression-debug-on-error
)
545 (eval-last-sexp-1 eval-last-sexp-arg-internal
)
546 (let ((old-value (make-symbol "t")) new-value value
)
547 (let ((debug-on-error old-value
))
548 (setq value
(eval-last-sexp-1 eval-last-sexp-arg-internal
))
549 (setq new-value debug-on-error
))
550 (unless (eq old-value new-value
)
551 (setq debug-on-error new-value
))
554 (defun eval-defun-1 (form)
555 "Change defvar into defconst within FORM.
556 Likewise for other constructs as necessary."
557 ;; The code in edebug-defun should be consistent with this, but not
558 ;; the same, since this gets a macroexpended form.
559 (cond ((not (listp form
))
561 ((and (eq (car form
) 'defvar
)
562 (cdr-safe (cdr-safe form
))
563 (boundp (cadr form
)))
564 ;; Force variable to be re-set.
565 `(progn (defvar ,(nth 1 form
) nil
,@(nthcdr 3 form
))
566 (setq ,(nth 1 form
) ,(nth 2 form
))))
567 ;; `defcustom' is now macroexpanded to
568 ;; `custom-declare-variable' with a quoted value arg.
569 ((and (eq (car form
) 'custom-declare-variable
)
570 (default-boundp (eval (nth 1 form
))))
571 ;; Force variable to be bound.
572 (set-default (eval (nth 1 form
)) (eval (nth 1 (nth 2 form
))))
574 ((eq (car form
) 'progn
)
575 (cons 'progn
(mapcar 'eval-defun-1
(cdr form
))))
578 (defun eval-defun-2 ()
579 "Evaluate defun that point is in or before.
580 The value is displayed in the minibuffer.
581 If the current defun is actually a call to `defvar',
582 then reset the variable using the initial value expression
583 even if the variable already has some other value.
584 \(Normally `defvar' does not change the variable's value
585 if it already has a value.\)
587 With argument, insert value in current buffer after the defun.
588 Return the result of evaluation."
590 (let ((debug-on-error eval-expression-debug-on-error
)
591 (print-length eval-expression-print-length
)
592 (print-level eval-expression-print-level
))
594 ;; Arrange for eval-region to "read" the (possibly) altered form.
595 ;; eval-region handles recording which file defines a function or
596 ;; variable. Re-written using `apply' to avoid capturing
597 ;; variables like `end'.
600 (let ((standard-output t
)
602 ;; Read the form from the buffer, and record where it ends.
607 (setq form
(read (current-buffer)))
609 ;; Alter the form if necessary, changing defvar into defconst, etc.
610 (setq form
(eval-defun-1 (macroexpand form
)))
611 (list beg end standard-output
613 ;; Skipping to the end of the specified region
614 ;; will make eval-region return.
617 ;; The result of evaluation has been put onto VALUES. So return it.
620 (defun eval-defun (edebug-it)
621 "Evaluate the top-level form containing point, or after point.
623 If the current defun is actually a call to `defvar' or `defcustom',
624 evaluating it this way resets the variable using its initial value
625 expression even if the variable already has some other value.
626 \(Normally `defvar' and `defcustom' do not alter the value if there
629 With a prefix argument, instrument the code for Edebug.
631 If acting on a `defun' for FUNCTION, and the function was
632 instrumented, `Edebug: FUNCTION' is printed in the minibuffer. If not
633 instrumented, just FUNCTION is printed.
635 If not acting on a `defun', the result of evaluation is displayed in
636 the minibuffer. This display is controlled by the variables
637 `eval-expression-print-length' and `eval-expression-print-level',
642 (eval-defun (not edebug-all-defs
)))
644 (if (null eval-expression-debug-on-error
)
646 (let ((old-value (make-symbol "t")) new-value value
)
647 (let ((debug-on-error old-value
))
648 (setq value
(eval-defun-2))
649 (setq new-value debug-on-error
))
650 (unless (eq old-value new-value
)
651 (setq debug-on-error new-value
))
655 (defun lisp-comment-indent ()
656 (if (looking-at "\\s<\\s<\\s<")
658 (if (looking-at "\\s<\\s<")
659 (let ((tem (or (calculate-lisp-indent) (current-column))))
660 (if (listp tem
) (car tem
) tem
))
661 (skip-chars-backward " \t")
662 (max (if (bolp) 0 (1+ (current-column)))
665 ;; This function just forces a more costly detection of comments (using
666 ;; parse-partial-sexp from beginning-of-defun). I.e. It avoids the problem of
667 ;; taking a `;' inside a string started on another line for a comment starter.
668 ;; Note: `newcomment' gets it right in 99% of the cases if you're using
669 ;; font-lock, anyway, so we could get rid of it. -stef
670 (defun lisp-mode-auto-fill ()
671 (if (> (current-column) (current-fill-column))
673 (nth 4 (syntax-ppss (point))))
675 (unless (and (boundp 'comment-auto-fill-only-comments
)
676 comment-auto-fill-only-comments
)
677 (let ((comment-start nil
) (comment-start-skip nil
))
680 (defvar lisp-indent-offset nil
681 "If non-nil, indent second line of expressions that many more columns.")
682 (defvar lisp-indent-function
'lisp-indent-function
)
684 (defun lisp-indent-line (&optional whole-exp
)
685 "Indent current line as Lisp code.
686 With argument, indent any additional lines of the same expression
687 rigidly along with this one."
689 (let ((indent (calculate-lisp-indent)) shift-amt end
690 (pos (- (point-max) (point)))
691 (beg (progn (beginning-of-line) (point))))
692 (skip-chars-forward " \t")
693 (if (or (null indent
) (looking-at "\\s<\\s<\\s<"))
694 ;; Don't alter indentation of a ;;; comment line
695 ;; or a line that starts in a string.
696 (goto-char (- (point-max) pos
))
697 (if (and (looking-at "\\s<") (not (looking-at "\\s<\\s<")))
698 ;; Single-semicolon comment lines should be indented
699 ;; as comment lines, not as code.
700 (progn (indent-for-comment) (forward-char -
1))
701 (if (listp indent
) (setq indent
(car indent
)))
702 (setq shift-amt
(- indent
(current-column)))
703 (if (zerop shift-amt
)
705 (delete-region beg
(point))
707 ;; If initial point was within line's indentation,
708 ;; position after the indentation. Else stay at same point in text.
709 (if (> (- (point-max) pos
) (point))
710 (goto-char (- (point-max) pos
)))
711 ;; If desired, shift remaining lines of expression the same amount.
712 (and whole-exp
(not (zerop shift-amt
))
721 (indent-code-rigidly beg end shift-amt
)))))
723 (defvar calculate-lisp-indent-last-sexp
)
725 (defun calculate-lisp-indent (&optional parse-start
)
726 "Return appropriate indentation for current line as Lisp code.
727 In usual case returns an integer: the column to indent to.
728 If the value is nil, that means don't change the indentation
729 because the line starts inside a string.
731 The value can also be a list of the form (COLUMN CONTAINING-SEXP-START).
732 This means that following lines at the same level of indentation
733 should not necessarily be indented the same as this line.
734 Then COLUMN is the column to indent to, and CONTAINING-SEXP-START
735 is the buffer position of the start of the containing expression."
738 (let ((indent-point (point))
740 ;; setting this to a number inhibits calling hook
743 calculate-lisp-indent-last-sexp containing-sexp
)
745 (goto-char parse-start
)
746 (beginning-of-defun))
747 ;; Find outermost containing sexp
748 (while (< (point) indent-point
)
749 (setq state
(parse-partial-sexp (point) indent-point
0)))
750 ;; Find innermost containing sexp
753 (> (setq paren-depth
(elt state
0)) 0))
755 (setq calculate-lisp-indent-last-sexp
(elt state
2))
756 (setq containing-sexp
(elt state
1))
757 ;; Position following last unclosed open.
758 (goto-char (1+ containing-sexp
))
759 ;; Is there a complete sexp since then?
760 (if (and calculate-lisp-indent-last-sexp
761 (> calculate-lisp-indent-last-sexp
(point)))
762 ;; Yes, but is there a containing sexp after that?
763 (let ((peek (parse-partial-sexp calculate-lisp-indent-last-sexp
765 (if (setq retry
(car (cdr peek
))) (setq state peek
)))))
768 ;; Innermost containing sexp found
769 (goto-char (1+ containing-sexp
))
770 (if (not calculate-lisp-indent-last-sexp
)
771 ;; indent-point immediately follows open paren.
773 (setq desired-indent
(current-column))
774 ;; Find the start of first element of containing sexp.
775 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp
0 t
)
776 (cond ((looking-at "\\s(")
777 ;; First element of containing sexp is a list.
778 ;; Indent under that list.
780 ((> (save-excursion (forward-line 1) (point))
781 calculate-lisp-indent-last-sexp
)
782 ;; This is the first line to start within the containing sexp.
783 ;; It's almost certainly a function call.
784 (if (= (point) calculate-lisp-indent-last-sexp
)
785 ;; Containing sexp has nothing before this line
786 ;; except the first element. Indent under that element.
788 ;; Skip the first element, find start of second (the first
789 ;; argument of the function call) and indent under.
790 (progn (forward-sexp 1)
791 (parse-partial-sexp (point)
792 calculate-lisp-indent-last-sexp
794 (backward-prefix-chars))
796 ;; Indent beneath first sexp on same line as
797 ;; `calculate-lisp-indent-last-sexp'. Again, it's
798 ;; almost certainly a function call.
799 (goto-char calculate-lisp-indent-last-sexp
)
801 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp
803 (backward-prefix-chars)))))
804 ;; Point is at the point to indent under unless we are inside a string.
805 ;; Call indentation hook except when overridden by lisp-indent-offset
806 ;; or if the desired indentation has already been computed.
807 (let ((normal-indent (current-column)))
809 ;; Inside a string, don't change indentation.
811 ((and (integerp lisp-indent-offset
) containing-sexp
)
812 ;; Indent by constant offset
813 (goto-char containing-sexp
)
814 (+ (current-column) lisp-indent-offset
))
816 ((and (boundp 'lisp-indent-function
)
819 (or (funcall lisp-indent-function indent-point state
)
824 (defun lisp-indent-function (indent-point state
)
825 "This function is the normal value of the variable `lisp-indent-function'.
826 It is used when indenting a line within a function call, to see if the
827 called function says anything special about how to indent the line.
829 INDENT-POINT is the position where the user typed TAB, or equivalent.
830 Point is located at the point to indent under (for default indentation);
831 STATE is the `parse-partial-sexp' state for that position.
833 If the current line is in a call to a Lisp function
834 which has a non-nil property `lisp-indent-function',
835 that specifies how to do the indentation. The property value can be
836 * `defun', meaning indent `defun'-style;
837 * an integer N, meaning indent the first N arguments specially
838 like ordinary function arguments and then indent any further
839 aruments like a body;
840 * a function to call just as this function was called.
841 If that function returns nil, that means it doesn't specify
844 This function also returns nil meaning don't specify the indentation."
845 (let ((normal-indent (current-column)))
846 (goto-char (1+ (elt state
1)))
847 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp
0 t
)
848 (if (and (elt state
2)
849 (not (looking-at "\\sw\\|\\s_")))
850 ;; car of form doesn't seem to be a symbol
852 (if (not (> (save-excursion (forward-line 1) (point))
853 calculate-lisp-indent-last-sexp
))
854 (progn (goto-char calculate-lisp-indent-last-sexp
)
856 (parse-partial-sexp (point)
857 calculate-lisp-indent-last-sexp
0 t
)))
858 ;; Indent under the list or under the first sexp on the same
859 ;; line as calculate-lisp-indent-last-sexp. Note that first
860 ;; thing on that line has to be complete sexp since we are
861 ;; inside the innermost containing sexp.
862 (backward-prefix-chars)
864 (let ((function (buffer-substring (point)
865 (progn (forward-sexp 1) (point))))
867 (setq method
(or (get (intern-soft function
) 'lisp-indent-function
)
868 (get (intern-soft function
) 'lisp-indent-hook
)))
869 (cond ((or (eq method
'defun
)
871 (> (length function
) 3)
872 (string-match "\\`def" function
)))
873 (lisp-indent-defform state indent-point
))
875 (lisp-indent-specform method state
876 indent-point normal-indent
))
878 (funcall method state indent-point
)))))))
880 (defvar lisp-body-indent
2
881 "Number of columns to indent the second line of a `(def...)' form.")
883 (defun lisp-indent-specform (count state indent-point normal-indent
)
884 (let ((containing-form-start (elt state
1))
886 body-indent containing-form-column
)
887 ;; Move to the start of containing form, calculate indentation
888 ;; to use for non-distinguished forms (> count), and move past the
889 ;; function symbol. lisp-indent-function guarantees that there is at
890 ;; least one word or symbol character following open paren of containing
892 (goto-char containing-form-start
)
893 (setq containing-form-column
(current-column))
894 (setq body-indent
(+ lisp-body-indent containing-form-column
))
897 ;; Now find the start of the last form.
898 (parse-partial-sexp (point) indent-point
1 t
)
899 (while (and (< (point) indent-point
)
902 (setq count
(1- count
))
904 (parse-partial-sexp (point) indent-point
1 t
))
906 ;; Point is sitting on first character of last (or count) sexp.
908 ;; A distinguished form. If it is the first or second form use double
909 ;; lisp-body-indent, else normal indent. With lisp-body-indent bound
910 ;; to 2 (the default), this just happens to work the same with if as
911 ;; the older code, but it makes unwind-protect, condition-case,
912 ;; with-output-to-temp-buffer, et. al. much more tasteful. The older,
913 ;; less hacked, behavior can be obtained by replacing below with
914 ;; (list normal-indent containing-form-start).
915 (if (<= (- i count
) 1)
916 (list (+ containing-form-column
(* 2 lisp-body-indent
))
917 containing-form-start
)
918 (list normal-indent containing-form-start
))
919 ;; A non-distinguished form. Use body-indent if there are no
920 ;; distinguished forms and this is the first undistinguished form,
921 ;; or if this is the first undistinguished form and the preceding
922 ;; distinguished form has indentation at least as great as body-indent.
923 (if (or (and (= i
0) (= count
0))
924 (and (= count
0) (<= body-indent normal-indent
)))
928 (defun lisp-indent-defform (state indent-point
)
929 (goto-char (car (cdr state
)))
931 (if (> (point) (car (cdr (cdr state
))))
933 (goto-char (car (cdr state
)))
934 (+ lisp-body-indent
(current-column)))))
937 ;; (put 'progn 'lisp-indent-function 0), say, causes progn to be indented
938 ;; like defun if the first form is placed on the next line, otherwise
939 ;; it is indented like any other form (i.e. forms line up under first).
941 (put 'lambda
'lisp-indent-function
'defun
)
942 (put 'autoload
'lisp-indent-function
'defun
)
943 (put 'progn
'lisp-indent-function
0)
944 (put 'prog1
'lisp-indent-function
1)
945 (put 'prog2
'lisp-indent-function
2)
946 (put 'save-excursion
'lisp-indent-function
0)
947 (put 'save-window-excursion
'lisp-indent-function
0)
948 (put 'save-selected-window
'lisp-indent-function
0)
949 (put 'save-restriction
'lisp-indent-function
0)
950 (put 'save-match-data
'lisp-indent-function
0)
951 (put 'save-current-buffer
'lisp-indent-function
0)
952 (put 'with-current-buffer
'lisp-indent-function
1)
953 (put 'combine-after-change-calls
'lisp-indent-function
0)
954 (put 'with-output-to-string
'lisp-indent-function
0)
955 (put 'with-temp-file
'lisp-indent-function
1)
956 (put 'with-temp-buffer
'lisp-indent-function
0)
957 (put 'with-temp-message
'lisp-indent-function
1)
958 (put 'with-syntax-table
'lisp-indent-function
1)
959 (put 'let
'lisp-indent-function
1)
960 (put 'let
* 'lisp-indent-function
1)
961 (put 'while
'lisp-indent-function
1)
962 (put 'if
'lisp-indent-function
2)
963 (put 'read-if
'lisp-indent-function
2)
964 (put 'catch
'lisp-indent-function
1)
965 (put 'condition-case
'lisp-indent-function
2)
966 (put 'unwind-protect
'lisp-indent-function
1)
967 (put 'with-output-to-temp-buffer
'lisp-indent-function
1)
968 (put 'eval-after-load
'lisp-indent-function
1)
969 (put 'dolist
'lisp-indent-function
1)
970 (put 'dotimes
'lisp-indent-function
1)
971 (put 'when
'lisp-indent-function
1)
972 (put 'unless
'lisp-indent-function
1)
974 (defun indent-sexp (&optional endpos
)
975 "Indent each line of the list starting just after point.
976 If optional arg ENDPOS is given, indent each line, stopping when
977 ENDPOS is encountered."
979 (let ((indent-stack (list nil
))
981 ;; If ENDPOS is non-nil, use nil as STARTING-POINT
982 ;; so that calculate-lisp-indent will find the beginning of
983 ;; the defun we are in.
984 ;; If ENDPOS is nil, it is safe not to scan before point
985 ;; since every line we indent is more deeply nested than point is.
986 (starting-point (if endpos nil
(point)))
988 last-depth bol outer-loop-done inner-loop-done state this-indent
)
990 ;; Get error now if we don't have a complete sexp after point.
991 (save-excursion (forward-sexp 1)))
993 (setq outer-loop-done nil
)
994 (while (if endpos
(< (point) endpos
)
995 (not outer-loop-done
))
996 (setq last-depth next-depth
998 ;; Parse this line so we can learn the state
999 ;; to indent the next line.
1000 ;; This inner loop goes through only once
1001 ;; unless a line ends inside a string.
1002 (while (and (not inner-loop-done
)
1003 (not (setq outer-loop-done
(eobp))))
1004 (setq state
(parse-partial-sexp (point) (progn (end-of-line) (point))
1006 (setq next-depth
(car state
))
1007 ;; If the line contains a comment other than the sort
1008 ;; that is indented like code,
1009 ;; indent it now with indent-for-comment.
1010 ;; Comments indented like code are right already.
1011 ;; In any case clear the in-comment flag in the state
1012 ;; because parse-partial-sexp never sees the newlines.
1013 (if (car (nthcdr 4 state
))
1014 (progn (indent-for-comment)
1016 (setcar (nthcdr 4 state
) nil
)))
1017 ;; If this line ends inside a string,
1018 ;; go straight to next line, remaining within the inner loop,
1019 ;; and turn off the \-flag.
1020 (if (car (nthcdr 3 state
))
1023 (setcar (nthcdr 5 state
) nil
))
1024 (setq inner-loop-done t
)))
1028 (setq indent-stack
(nconc indent-stack
1029 (make-list (- next-depth
) nil
))
1030 last-depth
(- last-depth next-depth
)
1032 (or outer-loop-done endpos
1033 (setq outer-loop-done
(<= next-depth
0)))
1036 (while (> last-depth next-depth
)
1037 (setq indent-stack
(cdr indent-stack
)
1038 last-depth
(1- last-depth
)))
1039 (while (< last-depth next-depth
)
1040 (setq indent-stack
(cons nil indent-stack
)
1041 last-depth
(1+ last-depth
)))
1042 ;; Now go to the next line and indent it according
1043 ;; to what we learned from parsing the previous one.
1046 (skip-chars-forward " \t")
1047 ;; But not if the line is blank, or just a comment
1048 ;; (except for double-semi comments; indent them as usual).
1049 (if (or (eobp) (looking-at "\\s<\\|\n"))
1051 (if (and (car indent-stack
)
1052 (>= (car indent-stack
) 0))
1053 (setq this-indent
(car indent-stack
))
1054 (let ((val (calculate-lisp-indent
1055 (if (car indent-stack
) (- (car indent-stack
))
1058 (setq this-indent val
)
1060 (setcar indent-stack
1061 (setq this-indent val
))
1062 (setcar indent-stack
(- (car (cdr val
))))
1063 (setq this-indent
(car val
))))))
1064 (if (and this-indent
(/= (current-column) this-indent
))
1065 (progn (delete-region bol
(point))
1066 (indent-to this-indent
)))))
1068 (setq outer-loop-done
(= (point) last-point
))
1069 (setq last-point
(point)))))))
1071 (defun lisp-indent-region (start end
)
1072 "Indent every line whose first char is between START and END inclusive."
1074 (let ((endmark (copy-marker end
)))
1076 (and (bolp) (not (eolp))
1078 (indent-sexp endmark
)
1079 (set-marker endmark nil
))))
1081 ;;;; Lisp paragraph filling commands.
1083 (defcustom emacs-lisp-docstring-fill-column
65
1084 "Value of `fill-column' to use when filling a docstring.
1085 Any non-integer value means do not use a different value of
1086 `fill-column' when filling docstrings."
1087 :type
'(choice (integer)
1088 (const :tag
"Use the current `fill-column'" t
))
1091 (defun lisp-fill-paragraph (&optional justify
)
1092 "Like \\[fill-paragraph], but handle Emacs Lisp comments and docstrings.
1093 If any of the current line is a comment, fill the comment or the
1094 paragraph of it that point is in, preserving the comment's indentation
1095 and initial semicolons."
1097 (or (fill-comment-paragraph justify
)
1098 ;; Point is on a program line (a line no comment); we are interested
1099 ;; particularly in docstring lines.
1101 ;; We bind `paragraph-start' and `paragraph-separate' temporarily. They
1102 ;; are buffer-local, but we avoid changing them so that they can be set
1103 ;; to make `forward-paragraph' and friends do something the user wants.
1105 ;; `paragraph-start': The `(' in the character alternative and the
1106 ;; left-singlequote plus `(' sequence after the \\| alternative prevent
1107 ;; sexps and backquoted sexps that follow a docstring from being filled
1108 ;; with the docstring. This setting has the consequence of inhibiting
1109 ;; filling many program lines that are not docstrings, which is sensible,
1110 ;; because the user probably asked to fill program lines by accident, or
1111 ;; expecting indentation (perhaps we should try to do indenting in that
1112 ;; case). The `;' and `:' stop the paragraph being filled at following
1113 ;; comment lines and at keywords (e.g., in `defcustom'). Left parens are
1114 ;; escaped to keep font-locking, filling, & paren matching in the source
1117 ;; `paragraph-separate': A clever regexp distinguishes the first line of
1118 ;; a docstring and identifies it as a paragraph separator, so that it
1119 ;; won't be filled. (Since the first line of documentation stands alone
1120 ;; in some contexts, filling should not alter the contents the author has
1121 ;; chosen.) Only the first line of a docstring begins with whitespace
1122 ;; and a quotation mark and ends with a period or (rarely) a comma.
1124 ;; The `fill-column' is temporarily bound to
1125 ;; `emacs-lisp-docstring-fill-column' if that value is an integer.
1126 (let ((paragraph-start (concat paragraph-start
1127 "\\|\\s-*\\([\(;:\"]\\|`\(\\)"))
1129 (concat paragraph-separate
"\\|\\s-*\".*[,\\.]$"))
1130 (fill-column (if (integerp emacs-lisp-docstring-fill-column
)
1131 emacs-lisp-docstring-fill-column
1133 (fill-paragraph justify
))
1134 ;; Never return nil.
1137 (defun indent-code-rigidly (start end arg
&optional nochange-regexp
)
1138 "Indent all lines of code, starting in the region, sideways by ARG columns.
1139 Does not affect lines starting inside comments or strings, assuming that
1140 the start of the region is not inside them.
1142 Called from a program, takes args START, END, COLUMNS and NOCHANGE-REGEXP.
1143 The last is a regexp which, if matched at the beginning of a line,
1144 means don't indent that line."
1145 (interactive "r\np")
1149 (setq end
(point-marker))
1152 (setq state
(parse-partial-sexp (point)
1154 (forward-line 1) (point))
1156 (while (< (point) end
)
1157 (or (car (nthcdr 3 state
))
1158 (and nochange-regexp
1159 (looking-at nochange-regexp
))
1160 ;; If line does not start in string, indent it
1161 (let ((indent (current-indentation)))
1162 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
1164 (indent-to (max 0 (+ indent arg
)) 0))))
1165 (setq state
(parse-partial-sexp (point)
1167 (forward-line 1) (point))
1170 (provide 'lisp-mode
)
1172 ;;; lisp-mode.el ends here