1 ;;; lisp-mode.el --- Lisp mode, and its idiosyncratic commands
3 ;; Copyright (C) 1985,86,1999,2000,01,03,2004 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 '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
";;;;* [^ \t\n]\\|(")
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 ;; Don't get confused by `;' in doc strings when paragraph-filling.
197 (set (make-local-variable 'comment-use-global-state
) t
)
198 (make-local-variable 'imenu-generic-expression
)
199 (setq imenu-generic-expression lisp-imenu-generic-expression
)
200 (make-local-variable 'multibyte-syntax-as-symbol
)
201 (setq multibyte-syntax-as-symbol t
)
202 (set (make-local-variable 'syntax-begin-function
) 'beginning-of-defun
)
203 (setq font-lock-defaults
204 '((lisp-font-lock-keywords
205 lisp-font-lock-keywords-1 lisp-font-lock-keywords-2
)
206 nil nil
(("+-*/.<>=!?$%_&~^:" .
"w")) nil
207 (font-lock-mark-block-function . mark-defun
)
208 (font-lock-syntactic-face-function
209 . lisp-font-lock-syntactic-face-function
))))
211 (defun lisp-outline-level ()
212 "Lisp mode `outline-level' function."
213 (if (looking-at "(\\|;;;###autoload")
215 (looking-at outline-regexp
)
216 (- (match-end 0) (match-beginning 0))))
219 (defvar lisp-mode-shared-map
220 (let ((map (make-sparse-keymap)))
221 (define-key map
"\t" 'lisp-indent-line
)
222 (define-key map
"\e\C-q" 'indent-sexp
)
223 (define-key map
"\177" 'backward-delete-char-untabify
)
224 ;; This gets in the way when viewing a Lisp file in view-mode. As
225 ;; long as [backspace] is mapped into DEL via the
226 ;; function-key-map, this should remain disabled!!
227 ;;;(define-key map [backspace] 'backward-delete-char-untabify)
229 "Keymap for commands shared by all sorts of Lisp modes.")
231 (defvar emacs-lisp-mode-map
()
232 "Keymap for Emacs Lisp mode.
233 All commands in `lisp-mode-shared-map' are inherited by this map.")
235 (if emacs-lisp-mode-map
237 (let ((map (make-sparse-keymap "Emacs-Lisp")))
238 (setq emacs-lisp-mode-map
(make-sparse-keymap))
239 (set-keymap-parent emacs-lisp-mode-map lisp-mode-shared-map
)
240 (define-key emacs-lisp-mode-map
"\e\t" 'lisp-complete-symbol
)
241 (define-key emacs-lisp-mode-map
"\e\C-x" 'eval-defun
)
242 (define-key emacs-lisp-mode-map
[menu-bar
] (make-sparse-keymap))
243 (define-key emacs-lisp-mode-map
[menu-bar emacs-lisp
]
244 (cons "Emacs-Lisp" map
))
245 (define-key map
[edebug-defun
]
246 '("Instrument Function for Debugging" . edebug-defun
))
247 (define-key map
[byte-recompile
]
248 '("Byte-recompile Directory..." . byte-recompile-directory
))
249 (define-key map
[emacs-byte-compile-and-load
]
250 '("Byte-compile And Load" . emacs-lisp-byte-compile-and-load
))
251 (define-key map
[byte-compile
]
252 '("Byte-compile This File" . emacs-lisp-byte-compile
))
253 (define-key map
[separator-eval
] '("--"))
254 (define-key map
[eval-buffer
] '("Evaluate Buffer" . eval-current-buffer
))
255 (define-key map
[eval-region
] '("Evaluate Region" . eval-region
))
256 (define-key map
[eval-sexp
] '("Evaluate Last S-expression" . eval-last-sexp
))
257 (define-key map
[separator-format
] '("--"))
258 (define-key map
[comment-region
] '("Comment Out Region" . comment-region
))
259 (define-key map
[indent-region
] '("Indent Region" . indent-region
))
260 (define-key map
[indent-line
] '("Indent Line" . lisp-indent-line
))
261 (put 'eval-region
'menu-enable
'mark-active
)
262 (put 'comment-region
'menu-enable
'mark-active
)
263 (put 'indent-region
'menu-enable
'mark-active
)))
265 (defun emacs-lisp-byte-compile ()
266 "Byte compile the file containing the current buffer."
269 (byte-compile-file buffer-file-name
)
270 (error "The buffer must be saved in a file first")))
272 (defun emacs-lisp-byte-compile-and-load ()
273 "Byte-compile the current file (if it has changed), then load compiled code."
276 (error "The buffer must be saved in a file first"))
278 ;; Recompile if file or buffer has changed since last compilation.
279 (if (and (buffer-modified-p)
280 (y-or-n-p (format "Save buffer %s first? " (buffer-name))))
282 (let ((compiled-file-name (byte-compile-dest-file buffer-file-name
)))
283 (if (file-newer-than-file-p compiled-file-name buffer-file-name
)
284 (load-file compiled-file-name
)
285 (byte-compile-file buffer-file-name t
))))
287 (defcustom emacs-lisp-mode-hook nil
288 "Hook run when entering Emacs Lisp mode."
289 :options
'(turn-on-eldoc-mode imenu-add-menubar-index checkdoc-minor-mode
)
293 (defcustom lisp-mode-hook nil
294 "Hook run when entering Lisp mode."
295 :options
'(imenu-add-menubar-index)
299 (defcustom lisp-interaction-mode-hook nil
300 "Hook run when entering Lisp Interaction mode."
301 :options
'(turn-on-eldoc-mode)
305 (defun emacs-lisp-mode ()
306 "Major mode for editing Lisp code to run in Emacs.
308 Delete converts tabs to spaces as it moves back.
309 Blank lines separate paragraphs. Semicolons start comments.
310 \\{emacs-lisp-mode-map}
311 Entry to this mode calls the value of `emacs-lisp-mode-hook'
312 if that value is non-nil."
314 (kill-all-local-variables)
315 (use-local-map emacs-lisp-mode-map
)
316 (set-syntax-table emacs-lisp-mode-syntax-table
)
317 (setq major-mode
'emacs-lisp-mode
)
318 (setq mode-name
"Emacs-Lisp")
319 (lisp-mode-variables)
320 (setq imenu-case-fold-search nil
)
321 (run-mode-hooks 'emacs-lisp-mode-hook
))
322 (put 'emacs-lisp-mode
'custom-mode-group
'lisp
)
324 (defvar lisp-mode-map
325 (let ((map (make-sparse-keymap)))
326 (set-keymap-parent map lisp-mode-shared-map
)
327 (define-key map
"\e\C-x" 'lisp-eval-defun
)
328 (define-key map
"\C-c\C-z" 'run-lisp
)
330 "Keymap for ordinary Lisp mode.
331 All commands in `lisp-mode-shared-map' are inherited by this map.")
334 "Major mode for editing Lisp code for Lisps other than GNU Emacs Lisp.
336 Delete converts tabs to spaces as it moves back.
337 Blank lines separate paragraphs. Semicolons start comments.
339 Note that `run-lisp' may be used either to start an inferior Lisp job
340 or to switch back to an existing one.
342 Entry to this mode calls the value of `lisp-mode-hook'
343 if that value is non-nil."
345 (kill-all-local-variables)
346 (use-local-map lisp-mode-map
)
347 (setq major-mode
'lisp-mode
)
348 (setq mode-name
"Lisp")
349 (lisp-mode-variables)
350 (make-local-variable 'comment-start-skip
)
351 (setq comment-start-skip
352 "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\)\\(;+\\|#|\\) *")
353 (make-local-variable 'font-lock-keywords-case-fold-search
)
354 (setq font-lock-keywords-case-fold-search t
)
355 (setq imenu-case-fold-search t
)
356 (set-syntax-table lisp-mode-syntax-table
)
357 (run-mode-hooks 'lisp-mode-hook
))
359 ;; Used in old LispM code.
360 (defalias 'common-lisp-mode
'lisp-mode
)
362 ;; This will do unless inf-lisp.el is loaded.
363 (defun lisp-eval-defun (&optional and-go
)
364 "Send the current defun to the Lisp process made by \\[run-lisp]."
366 (error "Process lisp does not exist"))
368 (defvar lisp-interaction-mode-map
369 (let ((map (make-sparse-keymap)))
370 (set-keymap-parent map lisp-mode-shared-map
)
371 (define-key map
"\e\C-x" 'eval-defun
)
372 (define-key map
"\e\t" 'lisp-complete-symbol
)
373 (define-key map
"\n" 'eval-print-last-sexp
)
375 "Keymap for Lisp Interaction mode.
376 All commands in `lisp-mode-shared-map' are inherited by this map.")
378 (defvar lisp-interaction-mode-abbrev-table lisp-mode-abbrev-table
)
379 (define-derived-mode lisp-interaction-mode emacs-lisp-mode
"Lisp Interaction"
380 "Major mode for typing and evaluating Lisp forms.
381 Like Lisp mode except that \\[eval-print-last-sexp] evals the Lisp expression
382 before point, and prints its value into the buffer, advancing point.
383 Note that printing is controlled by `eval-expression-print-length'
384 and `eval-expression-print-level'.
387 Delete converts tabs to spaces as it moves back.
388 Paragraphs are separated only by blank lines.
389 Semicolons start comments.
390 \\{lisp-interaction-mode-map}
391 Entry to this mode calls the value of `lisp-interaction-mode-hook'
392 if that value is non-nil.")
394 (defun eval-print-last-sexp ()
395 "Evaluate sexp before point; print value into current buffer.
397 Note that printing the result is controlled by the variables
398 `eval-expression-print-length' and `eval-expression-print-level',
401 (let ((standard-output (current-buffer)))
407 (defun last-sexp-setup-props (beg end value alt1 alt2
)
408 "Set up text properties for the output of `eval-last-sexp-1'.
409 BEG and END are the start and end of the output in current-buffer.
410 VALUE is the Lisp value printed, ALT1 and ALT2 are strings for the
411 alternative printed representations that can be displayed."
412 (let ((map (make-sparse-keymap)))
413 (define-key map
"\C-m" 'last-sexp-toggle-display
)
414 (define-key map
[down-mouse-2
] 'mouse-set-point
)
415 (define-key map
[mouse-2
] 'last-sexp-toggle-display
)
418 `(printed-value (,value
,alt1
,alt2
)
421 help-echo
"RET, mouse-2: toggle abbreviated display"
422 rear-nonsticky
(mouse-face keymap help-echo
426 (defun last-sexp-toggle-display (&optional arg
)
427 "Toggle between abbreviated and unabbreviated printed representations."
431 (let ((value (get-text-property (point) 'printed-value
)))
433 (let ((beg (or (previous-single-property-change (min (point-max) (1+ (point)))
436 (end (or (next-single-char-property-change (point) 'printed-value
) (point)))
437 (standard-output (current-buffer))
439 (delete-region beg end
)
440 (insert (nth 1 value
))
441 (last-sexp-setup-props beg
(point)
445 (goto-char (min (point-max) point
)))))))
447 (defun prin1-char (char)
448 "Return a string representing CHAR as a character rather than as an integer.
449 If CHAR is not a character, return nil."
451 (characterp (event-basic-type char
))
452 (let ((c (event-basic-type char
)))
457 (cond ((eq modif
'super
) "\\s-")
458 (t (string ?
\\ (upcase (aref (symbol-name modif
) 0)) ?-
))))
459 (event-modifiers char
) "")
461 ((memq c
'(?\
; ?\( ?\) ?\{ ?\} ?\[ ?\] ?\" ?\' ?\\)) (string ?\\ c))
465 (defun eval-last-sexp-1 (eval-last-sexp-arg-internal)
466 "Evaluate sexp before point; print value in minibuffer.
467 With argument, print output into current buffer."
468 (let ((standard-output (if eval-last-sexp-arg-internal
(current-buffer) t
)))
470 (eval (let ((stab (syntax-table))
475 (with-syntax-table emacs-lisp-mode-syntax-table
476 ;; If this sexp appears to be enclosed in `...'
477 ;; then ignore the surrounding quotes.
479 (or (eq (following-char) ?
\')
480 (eq (preceding-char) ?
\')))
482 ;; If we were after `?\e' (or similar case),
483 ;; use the whole thing, not just the `e'.
484 (when (eq (preceding-char) ?
\\)
486 (when (eq (preceding-char) ??
)
490 (when (eq (preceding-char) ?
=)
493 (skip-chars-backward "0-9#=")
494 (setq labeled-p
(looking-at "\\(#[0-9]+=\\)+")))
499 ;; vladimir@cs.ualberta.ca 30-Jul-1997: skip ` in
500 ;; `variable' so that the value is returned, not the
502 (if (and ignore-quotes
503 (eq (following-char) ?
`))
505 (narrow-to-region (point-min) opoint
)
506 (setq expr
(read (current-buffer)))
507 ;; If it's an (interactive ...) form, it's more
508 ;; useful to show how an interactive call would
511 (eq (car expr
) 'interactive
)
513 (list 'call-interactively
520 (eval-last-sexp-print-value value
))))
522 (defun eval-last-sexp-print-value (value)
523 (let ((unabbreviated (let ((print-length nil
) (print-level nil
))
524 (prin1-to-string value
)))
525 (print-length eval-expression-print-length
)
526 (print-level eval-expression-print-level
)
527 (char-string (prin1-char value
))
532 (if (and (eq standard-output t
) char-string
)
533 (princ (concat " = " char-string
)))
535 (when (and (bufferp standard-output
)
536 (or (not (null print-length
))
537 (not (null print-level
)))
538 (not (string= unabbreviated
539 (buffer-substring-no-properties beg end
))))
540 (last-sexp-setup-props beg end value
542 (buffer-substring-no-properties beg end
))
546 (defun eval-last-sexp (eval-last-sexp-arg-internal)
547 "Evaluate sexp before point; print value in minibuffer.
548 Interactively, with prefix argument, print output into current buffer."
550 (if (null eval-expression-debug-on-error
)
551 (eval-last-sexp-1 eval-last-sexp-arg-internal
)
552 (let ((old-value (make-symbol "t")) new-value value
)
553 (let ((debug-on-error old-value
))
554 (setq value
(eval-last-sexp-1 eval-last-sexp-arg-internal
))
555 (setq new-value debug-on-error
))
556 (unless (eq old-value new-value
)
557 (setq debug-on-error new-value
))
560 (defun eval-defun-1 (form)
561 "Change defvar into defconst within FORM.
562 Likewise for other constructs as necessary."
563 ;; The code in edebug-defun should be consistent with this, but not
564 ;; the same, since this gets a macroexpended form.
565 (cond ((not (listp form
))
567 ((and (eq (car form
) 'defvar
)
568 (cdr-safe (cdr-safe form
))
569 (boundp (cadr form
)))
570 ;; Force variable to be re-set.
571 `(progn (defvar ,(nth 1 form
) nil
,@(nthcdr 3 form
))
572 (setq ,(nth 1 form
) ,(nth 2 form
))))
573 ;; `defcustom' is now macroexpanded to
574 ;; `custom-declare-variable' with a quoted value arg.
575 ((and (eq (car form
) 'custom-declare-variable
)
576 (default-boundp (eval (nth 1 form
))))
577 ;; Force variable to be bound.
578 (set-default (eval (nth 1 form
)) (eval (nth 1 (nth 2 form
))))
580 ((eq (car form
) 'progn
)
581 (cons 'progn
(mapcar 'eval-defun-1
(cdr form
))))
584 (defun eval-defun-2 ()
585 "Evaluate defun that point is in or before.
586 The value is displayed in the minibuffer.
587 If the current defun is actually a call to `defvar',
588 then reset the variable using the initial value expression
589 even if the variable already has some other value.
590 \(Normally `defvar' does not change the variable's value
591 if it already has a value.\)
593 With argument, insert value in current buffer after the defun.
594 Return the result of evaluation."
596 (let ((debug-on-error eval-expression-debug-on-error
)
597 (print-length eval-expression-print-length
)
598 (print-level eval-expression-print-level
))
600 ;; Arrange for eval-region to "read" the (possibly) altered form.
601 ;; eval-region handles recording which file defines a function or
602 ;; variable. Re-written using `apply' to avoid capturing
603 ;; variables like `end'.
606 (let ((standard-output t
)
608 ;; Read the form from the buffer, and record where it ends.
613 (setq form
(read (current-buffer)))
615 ;; Alter the form if necessary, changing defvar into defconst, etc.
616 (setq form
(eval-defun-1 (macroexpand form
)))
617 (list beg end standard-output
619 ;; Skipping to the end of the specified region
620 ;; will make eval-region return.
623 ;; The result of evaluation has been put onto VALUES. So return it.
626 (defun eval-defun (edebug-it)
627 "Evaluate the top-level form containing point, or after point.
629 If the current defun is actually a call to `defvar' or `defcustom',
630 evaluating it this way resets the variable using its initial value
631 expression even if the variable already has some other value.
632 \(Normally `defvar' and `defcustom' do not alter the value if there
635 With a prefix argument, instrument the code for Edebug.
637 If acting on a `defun' for FUNCTION, and the function was
638 instrumented, `Edebug: FUNCTION' is printed in the minibuffer. If not
639 instrumented, just FUNCTION is printed.
641 If not acting on a `defun', the result of evaluation is displayed in
642 the minibuffer. This display is controlled by the variables
643 `eval-expression-print-length' and `eval-expression-print-level',
648 (eval-defun (not edebug-all-defs
)))
650 (if (null eval-expression-debug-on-error
)
652 (let ((old-value (make-symbol "t")) new-value value
)
653 (let ((debug-on-error old-value
))
654 (setq value
(eval-defun-2))
655 (setq new-value debug-on-error
))
656 (unless (eq old-value new-value
)
657 (setq debug-on-error new-value
))
661 (defun lisp-comment-indent ()
662 (if (looking-at "\\s<\\s<\\s<")
664 (if (looking-at "\\s<\\s<")
665 (let ((tem (or (calculate-lisp-indent) (current-column))))
666 (if (listp tem
) (car tem
) tem
))
667 (skip-chars-backward " \t")
668 (max (if (bolp) 0 (1+ (current-column)))
671 ;; This function just forces a more costly detection of comments (using
672 ;; parse-partial-sexp from beginning-of-defun). I.e. It avoids the problem of
673 ;; taking a `;' inside a string started on another line for a comment starter.
674 ;; Note: `newcomment' gets it right now since we set comment-use-global-state
675 ;; so we could get rid of it. -stef
676 (defun lisp-mode-auto-fill ()
677 (if (> (current-column) (current-fill-column))
679 (nth 4 (syntax-ppss (point))))
681 (unless (and (boundp 'comment-auto-fill-only-comments
)
682 comment-auto-fill-only-comments
)
683 (let ((comment-start nil
) (comment-start-skip nil
))
686 (defvar lisp-indent-offset nil
687 "If non-nil, indent second line of expressions that many more columns.")
688 (defvar lisp-indent-function
'lisp-indent-function
)
690 (defun lisp-indent-line (&optional whole-exp
)
691 "Indent current line as Lisp code.
692 With argument, indent any additional lines of the same expression
693 rigidly along with this one."
695 (let ((indent (calculate-lisp-indent)) shift-amt end
696 (pos (- (point-max) (point)))
697 (beg (progn (beginning-of-line) (point))))
698 (skip-chars-forward " \t")
699 (if (or (null indent
) (looking-at "\\s<\\s<\\s<"))
700 ;; Don't alter indentation of a ;;; comment line
701 ;; or a line that starts in a string.
702 (goto-char (- (point-max) pos
))
703 (if (and (looking-at "\\s<") (not (looking-at "\\s<\\s<")))
704 ;; Single-semicolon comment lines should be indented
705 ;; as comment lines, not as code.
706 (progn (indent-for-comment) (forward-char -
1))
707 (if (listp indent
) (setq indent
(car indent
)))
708 (setq shift-amt
(- indent
(current-column)))
709 (if (zerop shift-amt
)
711 (delete-region beg
(point))
713 ;; If initial point was within line's indentation,
714 ;; position after the indentation. Else stay at same point in text.
715 (if (> (- (point-max) pos
) (point))
716 (goto-char (- (point-max) pos
)))
717 ;; If desired, shift remaining lines of expression the same amount.
718 (and whole-exp
(not (zerop shift-amt
))
727 (indent-code-rigidly beg end shift-amt
)))))
729 (defvar calculate-lisp-indent-last-sexp
)
731 (defun calculate-lisp-indent (&optional parse-start
)
732 "Return appropriate indentation for current line as Lisp code.
733 In usual case returns an integer: the column to indent to.
734 If the value is nil, that means don't change the indentation
735 because the line starts inside a string.
737 The value can also be a list of the form (COLUMN CONTAINING-SEXP-START).
738 This means that following lines at the same level of indentation
739 should not necessarily be indented the same as this line.
740 Then COLUMN is the column to indent to, and CONTAINING-SEXP-START
741 is the buffer position of the start of the containing expression."
744 (let ((indent-point (point))
746 ;; setting this to a number inhibits calling hook
749 calculate-lisp-indent-last-sexp containing-sexp
)
751 (goto-char parse-start
)
752 (beginning-of-defun))
753 ;; Find outermost containing sexp
754 (while (< (point) indent-point
)
755 (setq state
(parse-partial-sexp (point) indent-point
0)))
756 ;; Find innermost containing sexp
759 (> (setq paren-depth
(elt state
0)) 0))
761 (setq calculate-lisp-indent-last-sexp
(elt state
2))
762 (setq containing-sexp
(elt state
1))
763 ;; Position following last unclosed open.
764 (goto-char (1+ containing-sexp
))
765 ;; Is there a complete sexp since then?
766 (if (and calculate-lisp-indent-last-sexp
767 (> calculate-lisp-indent-last-sexp
(point)))
768 ;; Yes, but is there a containing sexp after that?
769 (let ((peek (parse-partial-sexp calculate-lisp-indent-last-sexp
771 (if (setq retry
(car (cdr peek
))) (setq state peek
)))))
774 ;; Innermost containing sexp found
775 (goto-char (1+ containing-sexp
))
776 (if (not calculate-lisp-indent-last-sexp
)
777 ;; indent-point immediately follows open paren.
779 (setq desired-indent
(current-column))
780 ;; Find the start of first element of containing sexp.
781 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp
0 t
)
782 (cond ((looking-at "\\s(")
783 ;; First element of containing sexp is a list.
784 ;; Indent under that list.
786 ((> (save-excursion (forward-line 1) (point))
787 calculate-lisp-indent-last-sexp
)
788 ;; This is the first line to start within the containing sexp.
789 ;; It's almost certainly a function call.
790 (if (= (point) calculate-lisp-indent-last-sexp
)
791 ;; Containing sexp has nothing before this line
792 ;; except the first element. Indent under that element.
794 ;; Skip the first element, find start of second (the first
795 ;; argument of the function call) and indent under.
796 (progn (forward-sexp 1)
797 (parse-partial-sexp (point)
798 calculate-lisp-indent-last-sexp
800 (backward-prefix-chars))
802 ;; Indent beneath first sexp on same line as
803 ;; `calculate-lisp-indent-last-sexp'. Again, it's
804 ;; almost certainly a function call.
805 (goto-char calculate-lisp-indent-last-sexp
)
807 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp
809 (backward-prefix-chars)))))
810 ;; Point is at the point to indent under unless we are inside a string.
811 ;; Call indentation hook except when overridden by lisp-indent-offset
812 ;; or if the desired indentation has already been computed.
813 (let ((normal-indent (current-column)))
815 ;; Inside a string, don't change indentation.
817 ((and (integerp lisp-indent-offset
) containing-sexp
)
818 ;; Indent by constant offset
819 (goto-char containing-sexp
)
820 (+ (current-column) lisp-indent-offset
))
822 ((and (boundp 'lisp-indent-function
)
825 (or (funcall lisp-indent-function indent-point state
)
830 (defun lisp-indent-function (indent-point state
)
831 "This function is the normal value of the variable `lisp-indent-function'.
832 It is used when indenting a line within a function call, to see if the
833 called function says anything special about how to indent the line.
835 INDENT-POINT is the position where the user typed TAB, or equivalent.
836 Point is located at the point to indent under (for default indentation);
837 STATE is the `parse-partial-sexp' state for that position.
839 If the current line is in a call to a Lisp function
840 which has a non-nil property `lisp-indent-function',
841 that specifies how to do the indentation. The property value can be
842 * `defun', meaning indent `defun'-style;
843 * an integer N, meaning indent the first N arguments specially
844 like ordinary function arguments and then indent any further
845 aruments like a body;
846 * a function to call just as this function was called.
847 If that function returns nil, that means it doesn't specify
850 This function also returns nil meaning don't specify the indentation."
851 (let ((normal-indent (current-column)))
852 (goto-char (1+ (elt state
1)))
853 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp
0 t
)
854 (if (and (elt state
2)
855 (not (looking-at "\\sw\\|\\s_")))
856 ;; car of form doesn't seem to be a symbol
858 (if (not (> (save-excursion (forward-line 1) (point))
859 calculate-lisp-indent-last-sexp
))
860 (progn (goto-char calculate-lisp-indent-last-sexp
)
862 (parse-partial-sexp (point)
863 calculate-lisp-indent-last-sexp
0 t
)))
864 ;; Indent under the list or under the first sexp on the same
865 ;; line as calculate-lisp-indent-last-sexp. Note that first
866 ;; thing on that line has to be complete sexp since we are
867 ;; inside the innermost containing sexp.
868 (backward-prefix-chars)
870 (let ((function (buffer-substring (point)
871 (progn (forward-sexp 1) (point))))
873 (setq method
(or (get (intern-soft function
) 'lisp-indent-function
)
874 (get (intern-soft function
) 'lisp-indent-hook
)))
875 (cond ((or (eq method
'defun
)
877 (> (length function
) 3)
878 (string-match "\\`def" function
)))
879 (lisp-indent-defform state indent-point
))
881 (lisp-indent-specform method state
882 indent-point normal-indent
))
884 (funcall method state indent-point
)))))))
886 (defvar lisp-body-indent
2
887 "Number of columns to indent the second line of a `(def...)' form.")
889 (defun lisp-indent-specform (count state indent-point normal-indent
)
890 (let ((containing-form-start (elt state
1))
892 body-indent containing-form-column
)
893 ;; Move to the start of containing form, calculate indentation
894 ;; to use for non-distinguished forms (> count), and move past the
895 ;; function symbol. lisp-indent-function guarantees that there is at
896 ;; least one word or symbol character following open paren of containing
898 (goto-char containing-form-start
)
899 (setq containing-form-column
(current-column))
900 (setq body-indent
(+ lisp-body-indent containing-form-column
))
903 ;; Now find the start of the last form.
904 (parse-partial-sexp (point) indent-point
1 t
)
905 (while (and (< (point) indent-point
)
908 (setq count
(1- count
))
910 (parse-partial-sexp (point) indent-point
1 t
))
912 ;; Point is sitting on first character of last (or count) sexp.
914 ;; A distinguished form. If it is the first or second form use double
915 ;; lisp-body-indent, else normal indent. With lisp-body-indent bound
916 ;; to 2 (the default), this just happens to work the same with if as
917 ;; the older code, but it makes unwind-protect, condition-case,
918 ;; with-output-to-temp-buffer, et. al. much more tasteful. The older,
919 ;; less hacked, behavior can be obtained by replacing below with
920 ;; (list normal-indent containing-form-start).
921 (if (<= (- i count
) 1)
922 (list (+ containing-form-column
(* 2 lisp-body-indent
))
923 containing-form-start
)
924 (list normal-indent containing-form-start
))
925 ;; A non-distinguished form. Use body-indent if there are no
926 ;; distinguished forms and this is the first undistinguished form,
927 ;; or if this is the first undistinguished form and the preceding
928 ;; distinguished form has indentation at least as great as body-indent.
929 (if (or (and (= i
0) (= count
0))
930 (and (= count
0) (<= body-indent normal-indent
)))
934 (defun lisp-indent-defform (state indent-point
)
935 (goto-char (car (cdr state
)))
937 (if (> (point) (car (cdr (cdr state
))))
939 (goto-char (car (cdr state
)))
940 (+ lisp-body-indent
(current-column)))))
943 ;; (put 'progn 'lisp-indent-function 0), say, causes progn to be indented
944 ;; like defun if the first form is placed on the next line, otherwise
945 ;; it is indented like any other form (i.e. forms line up under first).
947 (put 'lambda
'lisp-indent-function
'defun
)
948 (put 'autoload
'lisp-indent-function
'defun
)
949 (put 'progn
'lisp-indent-function
0)
950 (put 'prog1
'lisp-indent-function
1)
951 (put 'prog2
'lisp-indent-function
2)
952 (put 'save-excursion
'lisp-indent-function
0)
953 (put 'save-window-excursion
'lisp-indent-function
0)
954 (put 'save-selected-window
'lisp-indent-function
0)
955 (put 'save-restriction
'lisp-indent-function
0)
956 (put 'save-match-data
'lisp-indent-function
0)
957 (put 'save-current-buffer
'lisp-indent-function
0)
958 (put 'with-current-buffer
'lisp-indent-function
1)
959 (put 'combine-after-change-calls
'lisp-indent-function
0)
960 (put 'with-output-to-string
'lisp-indent-function
0)
961 (put 'with-temp-file
'lisp-indent-function
1)
962 (put 'with-temp-buffer
'lisp-indent-function
0)
963 (put 'with-temp-message
'lisp-indent-function
1)
964 (put 'with-syntax-table
'lisp-indent-function
1)
965 (put 'let
'lisp-indent-function
1)
966 (put 'let
* 'lisp-indent-function
1)
967 (put 'while
'lisp-indent-function
1)
968 (put 'if
'lisp-indent-function
2)
969 (put 'read-if
'lisp-indent-function
2)
970 (put 'catch
'lisp-indent-function
1)
971 (put 'condition-case
'lisp-indent-function
2)
972 (put 'unwind-protect
'lisp-indent-function
1)
973 (put 'with-output-to-temp-buffer
'lisp-indent-function
1)
974 (put 'eval-after-load
'lisp-indent-function
1)
975 (put 'dolist
'lisp-indent-function
1)
976 (put 'dotimes
'lisp-indent-function
1)
977 (put 'when
'lisp-indent-function
1)
978 (put 'unless
'lisp-indent-function
1)
980 (defun indent-sexp (&optional endpos
)
981 "Indent each line of the list starting just after point.
982 If optional arg ENDPOS is given, indent each line, stopping when
983 ENDPOS is encountered."
985 (let ((indent-stack (list nil
))
987 ;; If ENDPOS is non-nil, use nil as STARTING-POINT
988 ;; so that calculate-lisp-indent will find the beginning of
989 ;; the defun we are in.
990 ;; If ENDPOS is nil, it is safe not to scan before point
991 ;; since every line we indent is more deeply nested than point is.
992 (starting-point (if endpos nil
(point)))
994 last-depth bol outer-loop-done inner-loop-done state this-indent
)
996 ;; Get error now if we don't have a complete sexp after point.
997 (save-excursion (forward-sexp 1)))
999 (setq outer-loop-done nil
)
1000 (while (if endpos
(< (point) endpos
)
1001 (not outer-loop-done
))
1002 (setq last-depth next-depth
1003 inner-loop-done nil
)
1004 ;; Parse this line so we can learn the state
1005 ;; to indent the next line.
1006 ;; This inner loop goes through only once
1007 ;; unless a line ends inside a string.
1008 (while (and (not inner-loop-done
)
1009 (not (setq outer-loop-done
(eobp))))
1010 (setq state
(parse-partial-sexp (point) (progn (end-of-line) (point))
1012 (setq next-depth
(car state
))
1013 ;; If the line contains a comment other than the sort
1014 ;; that is indented like code,
1015 ;; indent it now with indent-for-comment.
1016 ;; Comments indented like code are right already.
1017 ;; In any case clear the in-comment flag in the state
1018 ;; because parse-partial-sexp never sees the newlines.
1019 (if (car (nthcdr 4 state
))
1020 (progn (indent-for-comment)
1022 (setcar (nthcdr 4 state
) nil
)))
1023 ;; If this line ends inside a string,
1024 ;; go straight to next line, remaining within the inner loop,
1025 ;; and turn off the \-flag.
1026 (if (car (nthcdr 3 state
))
1029 (setcar (nthcdr 5 state
) nil
))
1030 (setq inner-loop-done t
)))
1034 (setq indent-stack
(nconc indent-stack
1035 (make-list (- next-depth
) nil
))
1036 last-depth
(- last-depth next-depth
)
1038 (or outer-loop-done endpos
1039 (setq outer-loop-done
(<= next-depth
0)))
1042 (while (> last-depth next-depth
)
1043 (setq indent-stack
(cdr indent-stack
)
1044 last-depth
(1- last-depth
)))
1045 (while (< last-depth next-depth
)
1046 (setq indent-stack
(cons nil indent-stack
)
1047 last-depth
(1+ last-depth
)))
1048 ;; Now go to the next line and indent it according
1049 ;; to what we learned from parsing the previous one.
1052 (skip-chars-forward " \t")
1053 ;; But not if the line is blank, or just a comment
1054 ;; (except for double-semi comments; indent them as usual).
1055 (if (or (eobp) (looking-at "\\s<\\|\n"))
1057 (if (and (car indent-stack
)
1058 (>= (car indent-stack
) 0))
1059 (setq this-indent
(car indent-stack
))
1060 (let ((val (calculate-lisp-indent
1061 (if (car indent-stack
) (- (car indent-stack
))
1064 (setq this-indent val
)
1066 (setcar indent-stack
1067 (setq this-indent val
))
1068 (setcar indent-stack
(- (car (cdr val
))))
1069 (setq this-indent
(car val
))))))
1070 (if (and this-indent
(/= (current-column) this-indent
))
1071 (progn (delete-region bol
(point))
1072 (indent-to this-indent
)))))
1074 (setq outer-loop-done
(= (point) last-point
))
1075 (setq last-point
(point)))))))
1077 (defun lisp-indent-region (start end
)
1078 "Indent every line whose first char is between START and END inclusive."
1080 (let ((endmark (copy-marker end
)))
1082 (and (bolp) (not (eolp))
1084 (indent-sexp endmark
)
1085 (set-marker endmark nil
))))
1087 ;;;; Lisp paragraph filling commands.
1089 (defcustom emacs-lisp-docstring-fill-column
65
1090 "Value of `fill-column' to use when filling a docstring.
1091 Any non-integer value means do not use a different value of
1092 `fill-column' when filling docstrings."
1093 :type
'(choice (integer)
1094 (const :tag
"Use the current `fill-column'" t
))
1097 (defun lisp-fill-paragraph (&optional justify
)
1098 "Like \\[fill-paragraph], but handle Emacs Lisp comments and docstrings.
1099 If any of the current line is a comment, fill the comment or the
1100 paragraph of it that point is in, preserving the comment's indentation
1101 and initial semicolons."
1103 (or (fill-comment-paragraph justify
)
1104 ;; Point is on a program line (a line no comment); we are interested
1105 ;; particularly in docstring lines.
1107 ;; We bind `paragraph-start' and `paragraph-separate' temporarily. They
1108 ;; are buffer-local, but we avoid changing them so that they can be set
1109 ;; to make `forward-paragraph' and friends do something the user wants.
1111 ;; `paragraph-start': The `(' in the character alternative and the
1112 ;; left-singlequote plus `(' sequence after the \\| alternative prevent
1113 ;; sexps and backquoted sexps that follow a docstring from being filled
1114 ;; with the docstring. This setting has the consequence of inhibiting
1115 ;; filling many program lines that are not docstrings, which is sensible,
1116 ;; because the user probably asked to fill program lines by accident, or
1117 ;; expecting indentation (perhaps we should try to do indenting in that
1118 ;; case). The `;' and `:' stop the paragraph being filled at following
1119 ;; comment lines and at keywords (e.g., in `defcustom'). Left parens are
1120 ;; escaped to keep font-locking, filling, & paren matching in the source
1123 ;; `paragraph-separate': A clever regexp distinguishes the first line of
1124 ;; a docstring and identifies it as a paragraph separator, so that it
1125 ;; won't be filled. (Since the first line of documentation stands alone
1126 ;; in some contexts, filling should not alter the contents the author has
1127 ;; chosen.) Only the first line of a docstring begins with whitespace
1128 ;; and a quotation mark and ends with a period or (rarely) a comma.
1130 ;; The `fill-column' is temporarily bound to
1131 ;; `emacs-lisp-docstring-fill-column' if that value is an integer.
1132 (let ((paragraph-start (concat paragraph-start
1133 "\\|\\s-*\\([\(;:\"]\\|`\(\\)"))
1135 (concat paragraph-separate
"\\|\\s-*\".*[,\\.]$"))
1136 (fill-column (if (integerp emacs-lisp-docstring-fill-column
)
1137 emacs-lisp-docstring-fill-column
1139 (fill-paragraph justify
))
1140 ;; Never return nil.
1143 (defun indent-code-rigidly (start end arg
&optional nochange-regexp
)
1144 "Indent all lines of code, starting in the region, sideways by ARG columns.
1145 Does not affect lines starting inside comments or strings, assuming that
1146 the start of the region is not inside them.
1148 Called from a program, takes args START, END, COLUMNS and NOCHANGE-REGEXP.
1149 The last is a regexp which, if matched at the beginning of a line,
1150 means don't indent that line."
1151 (interactive "r\np")
1155 (setq end
(point-marker))
1158 (setq state
(parse-partial-sexp (point)
1160 (forward-line 1) (point))
1162 (while (< (point) end
)
1163 (or (car (nthcdr 3 state
))
1164 (and nochange-regexp
1165 (looking-at nochange-regexp
))
1166 ;; If line does not start in string, indent it
1167 (let ((indent (current-indentation)))
1168 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
1170 (indent-to (max 0 (+ indent arg
)) 0))))
1171 (setq state
(parse-partial-sexp (point)
1173 (forward-line 1) (point))
1176 (provide 'lisp-mode
)
1178 ;;; arch-tag: 414c7f93-c245-4b77-8ed5-ed05ef7ff1bf
1179 ;;; lisp-mode.el ends here