1 ;;; lisp-mode.el --- Lisp mode, and its idiosyncratic commands
3 ;; Copyright (C) 1985, 1986, 1999, 2000, 2001 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 ;; Used to be singlequote; changed for flonums.
63 (modify-syntax-entry ?.
"_ " table
)
64 (modify-syntax-entry ?
# "' " table
)
65 (modify-syntax-entry ?
\" "\" " table
)
66 (modify-syntax-entry ?
\\ "\\ " table
)
67 (modify-syntax-entry ?\
( "() " table
)
68 (modify-syntax-entry ?\
) ")( " table
)
69 (modify-syntax-entry ?\
[ "(] " table
)
70 (modify-syntax-entry ?\
] ")[ " table
))
73 (defvar lisp-mode-syntax-table
74 (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table
)))
75 (modify-syntax-entry ?\
[ "_ " table
)
76 (modify-syntax-entry ?\
] "_ " table
)
77 (modify-syntax-entry ?
# "' 14bn" table
)
78 (modify-syntax-entry ?|
"\" 23b" table
)
81 (define-abbrev-table 'lisp-mode-abbrev-table
())
83 (defvar lisp-imenu-generic-expression
86 (purecopy (concat "^\\s-*("
89 '("defun" "defun*" "defsubst" "defmacro"
90 "defadvice" "define-skeleton"
91 "define-minor-mode" "define-derived-mode"
92 "define-compiler-macro" "define-modify-macro"
93 "defsetf" "define-setf-expander"
94 "define-method-combination"
95 "defgeneric" "defmethod") t
))
96 "\\s-+\\(\\sw\\(\\sw\\|\\s_\\)+\\)"))
98 (list (purecopy "Variables")
99 (purecopy (concat "^\\s-*("
102 '("defvar" "defconst" "defconstant" "defcustom"
103 "defparameter" "define-symbol-macro") t
))
104 "\\s-+\\(\\sw\\(\\sw\\|\\s_\\)+\\)"))
106 (list (purecopy "Types")
107 (purecopy (concat "^\\s-*("
110 '("defgroup" "deftype" "defstruct" "defclass"
111 "define-condition" "define-widget" "defface"
113 "\\s-+'?\\(\\sw\\(\\sw\\|\\s_\\)+\\)"))
116 "Imenu generic expression for Lisp mode. See `imenu-generic-expression'.")
118 ;; This was originally in autoload.el and is still used there.
119 (put 'autoload
'doc-string-elt
3)
120 (put 'defun
'doc-string-elt
3)
121 (put 'defun
* 'doc-string-elt
3)
122 (put 'defvar
'doc-string-elt
3)
123 (put 'defcustom
'doc-string-elt
3)
124 (put 'defconst
'doc-string-elt
3)
125 (put 'defmacro
'doc-string-elt
3)
126 (put 'defmacro
* 'doc-string-elt
3)
127 (put 'defsubst
'doc-string-elt
3)
128 (put 'define-skeleton
'doc-string-elt
2)
129 (put 'define-derived-mode
'doc-string-elt
4)
130 (put 'easy-mmode-define-minor-mode
'doc-string-elt
2)
131 (put 'define-minor-mode
'doc-string-elt
2)
132 (put 'define-generic-mode
'doc-string-elt
7)
133 ;; define-global-mode has no explicit docstring.
134 (put 'easy-mmode-define-global-mode
'doc-string-elt
0)
137 (defun lisp-font-lock-syntactic-face-function (state)
139 (if (and (eq (nth 0 state
) 1)
140 ;; This might be a docstring.
143 (goto-char (nth 8 state
))
145 (while (progn (backward-sexp 1) (setq n
(1+ n
))))
148 (let ((sym (intern-soft
150 (point) (progn (forward-sexp 1) (point))))))
151 (eq n
(or (get sym
'doc-string-elt
) 3)))))))
153 font-lock-string-face
)
154 font-lock-comment-face
))
156 ;; The LISP-SYNTAX argument is used by code in inf-lisp.el and is
157 ;; (uselessly) passed from pp.el, chistory.el, gnus-kill.el and score-mode.el
158 (defun lisp-mode-variables (&optional lisp-syntax
)
160 (set-syntax-table lisp-mode-syntax-table
))
161 (setq local-abbrev-table lisp-mode-abbrev-table
)
162 (make-local-variable 'paragraph-ignore-fill-prefix
)
163 (setq paragraph-ignore-fill-prefix t
)
164 (make-local-variable 'fill-paragraph-function
)
165 (setq fill-paragraph-function
'lisp-fill-paragraph
)
166 ;; Adaptive fill mode gets in the way of auto-fill,
167 ;; and should make no difference for explicit fill
168 ;; because lisp-fill-paragraph should do the job.
169 ;; I believe that newcomment's auto-fill code properly deals with it -stef
170 ;;(set (make-local-variable 'adaptive-fill-mode) nil)
171 (make-local-variable 'normal-auto-fill-function
)
172 (setq normal-auto-fill-function
'lisp-mode-auto-fill
)
173 (make-local-variable 'indent-line-function
)
174 (setq indent-line-function
'lisp-indent-line
)
175 (make-local-variable 'indent-region-function
)
176 (setq indent-region-function
'lisp-indent-region
)
177 (make-local-variable 'parse-sexp-ignore-comments
)
178 (setq parse-sexp-ignore-comments t
)
179 (make-local-variable 'outline-regexp
)
180 (setq outline-regexp
";;;;* \\|(")
181 (make-local-variable 'outline-level
)
182 (setq outline-level
'lisp-outline-level
)
183 (make-local-variable 'comment-start
)
184 (setq comment-start
";")
185 (make-local-variable 'comment-start-skip
)
186 ;; Look within the line for a ; following an even number of backslashes
187 ;; after either a non-backslash or the line beginning.
188 (setq comment-start-skip
"\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\);+ *")
189 (make-local-variable 'comment-add
)
190 (setq comment-add
1) ;default to `;;' in comment-region
191 (make-local-variable 'comment-column
)
192 (setq comment-column
40)
193 (make-local-variable 'comment-indent-function
)
194 (setq comment-indent-function
'lisp-comment-indent
)
195 (make-local-variable 'imenu-generic-expression
)
196 (setq imenu-generic-expression lisp-imenu-generic-expression
)
197 (make-local-variable 'multibyte-syntax-as-symbol
)
198 (setq multibyte-syntax-as-symbol t
)
199 (set (make-local-variable 'syntax-begin-function
) 'beginning-of-defun
)
200 (setq font-lock-defaults
201 '((lisp-font-lock-keywords
202 lisp-font-lock-keywords-1 lisp-font-lock-keywords-2
)
203 nil nil
(("+-*/.<>=!?$%_&~^:" .
"w")) beginning-of-defun
204 (font-lock-mark-block-function . mark-defun
)
205 (font-lock-syntactic-face-function
206 . lisp-font-lock-syntactic-face-function
))))
208 (defun lisp-outline-level ()
209 "Lisp mode `outline-level' function."
212 (looking-at outline-regexp
)
213 (- (match-end 0) (match-beginning 0))))
216 (defvar lisp-mode-shared-map
217 (let ((map (make-sparse-keymap)))
218 (define-key map
"\t" 'lisp-indent-line
)
219 (define-key map
"\e\C-q" 'indent-sexp
)
220 (define-key map
"\177" 'backward-delete-char-untabify
)
221 ;; This gets in the way when viewing a Lisp file in view-mode. As
222 ;; long as [backspace] is mapped into DEL via the
223 ;; function-key-map, this should remain disabled!!
224 ;;;(define-key map [backspace] 'backward-delete-char-untabify)
226 "Keymap for commands shared by all sorts of Lisp modes.")
228 (defvar emacs-lisp-mode-map
()
229 "Keymap for Emacs Lisp mode.
230 All commands in `lisp-mode-shared-map' are inherited by this map.")
232 (if emacs-lisp-mode-map
234 (let ((map (make-sparse-keymap "Emacs-Lisp")))
235 (setq emacs-lisp-mode-map
(make-sparse-keymap))
236 (set-keymap-parent emacs-lisp-mode-map lisp-mode-shared-map
)
237 (define-key emacs-lisp-mode-map
"\e\t" 'lisp-complete-symbol
)
238 (define-key emacs-lisp-mode-map
"\e\C-x" 'eval-defun
)
239 (define-key emacs-lisp-mode-map
[menu-bar
] (make-sparse-keymap))
240 (define-key emacs-lisp-mode-map
[menu-bar emacs-lisp
]
241 (cons "Emacs-Lisp" map
))
242 (define-key map
[edebug-defun
]
243 '("Instrument Function for Debugging" . edebug-defun
))
244 (define-key map
[byte-recompile
]
245 '("Byte-recompile Directory..." . byte-recompile-directory
))
246 (define-key map
[emacs-byte-compile-and-load
]
247 '("Byte-compile And Load" . emacs-lisp-byte-compile-and-load
))
248 (define-key map
[byte-compile
]
249 '("Byte-compile This File" . emacs-lisp-byte-compile
))
250 (define-key map
[separator-eval
] '("--"))
251 (define-key map
[eval-buffer
] '("Evaluate Buffer" . eval-current-buffer
))
252 (define-key map
[eval-region
] '("Evaluate Region" . eval-region
))
253 (define-key map
[eval-sexp
] '("Evaluate Last S-expression" . eval-last-sexp
))
254 (define-key map
[separator-format
] '("--"))
255 (define-key map
[comment-region
] '("Comment Out Region" . comment-region
))
256 (define-key map
[indent-region
] '("Indent Region" . indent-region
))
257 (define-key map
[indent-line
] '("Indent Line" . lisp-indent-line
))
258 (put 'eval-region
'menu-enable
'mark-active
)
259 (put 'comment-region
'menu-enable
'mark-active
)
260 (put 'indent-region
'menu-enable
'mark-active
)))
262 (defun emacs-lisp-byte-compile ()
263 "Byte compile the file containing the current buffer."
266 (byte-compile-file buffer-file-name
)
267 (error "The buffer must be saved in a file first")))
269 (defun emacs-lisp-byte-compile-and-load ()
270 "Byte-compile the current file (if it has changed), then load compiled code."
273 (error "The buffer must be saved in a file first"))
275 ;; Recompile if file or buffer has changed since last compilation.
276 (if (and (buffer-modified-p)
277 (y-or-n-p (format "Save buffer %s first? " (buffer-name))))
279 (let ((compiled-file-name (byte-compile-dest-file buffer-file-name
)))
280 (if (file-newer-than-file-p compiled-file-name buffer-file-name
)
281 (load-file compiled-file-name
)
282 (byte-compile-file buffer-file-name t
))))
284 (defcustom emacs-lisp-mode-hook nil
285 "Hook run when entering Emacs Lisp mode."
286 :options
'(turn-on-eldoc-mode imenu-add-menubar-index checkdoc-minor-mode
)
290 (defcustom lisp-mode-hook nil
291 "Hook run when entering Lisp mode."
292 :options
'(imenu-add-menubar-index)
296 (defcustom lisp-interaction-mode-hook nil
297 "Hook run when entering Lisp Interaction mode."
298 :options
'(turn-on-eldoc-mode)
302 (defun emacs-lisp-mode ()
303 "Major mode for editing Lisp code to run in Emacs.
305 Delete converts tabs to spaces as it moves back.
306 Blank lines separate paragraphs. Semicolons start comments.
307 \\{emacs-lisp-mode-map}
308 Entry to this mode calls the value of `emacs-lisp-mode-hook'
309 if that value is non-nil."
311 (kill-all-local-variables)
312 (use-local-map emacs-lisp-mode-map
)
313 (set-syntax-table emacs-lisp-mode-syntax-table
)
314 (setq major-mode
'emacs-lisp-mode
)
315 (setq mode-name
"Emacs-Lisp")
316 (lisp-mode-variables)
317 (setq imenu-case-fold-search nil
)
318 (run-hooks 'emacs-lisp-mode-hook
))
320 (defvar lisp-mode-map
321 (let ((map (make-sparse-keymap)))
322 (set-keymap-parent map lisp-mode-shared-map
)
323 (define-key map
"\e\C-x" 'lisp-eval-defun
)
324 (define-key map
"\C-c\C-z" 'run-lisp
)
326 "Keymap for ordinary Lisp mode.
327 All commands in `lisp-mode-shared-map' are inherited by this map.")
330 "Major mode for editing Lisp code for Lisps other than GNU Emacs Lisp.
332 Delete converts tabs to spaces as it moves back.
333 Blank lines separate paragraphs. Semicolons start comments.
335 Note that `run-lisp' may be used either to start an inferior Lisp job
336 or to switch back to an existing one.
338 Entry to this mode calls the value of `lisp-mode-hook'
339 if that value is non-nil."
341 (kill-all-local-variables)
342 (use-local-map lisp-mode-map
)
343 (setq major-mode
'lisp-mode
)
344 (setq mode-name
"Lisp")
345 (lisp-mode-variables)
346 (make-local-variable 'comment-start-skip
)
347 (setq comment-start-skip
348 "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\)\\(;+\\|#|\\) *")
349 (make-local-variable 'font-lock-keywords-case-fold-search
)
350 (setq font-lock-keywords-case-fold-search t
)
351 (setq imenu-case-fold-search t
)
352 (set-syntax-table lisp-mode-syntax-table
)
353 (run-hooks 'lisp-mode-hook
))
355 ;; This will do unless inf-lisp.el is loaded.
356 (defun lisp-eval-defun (&optional and-go
)
357 "Send the current defun to the Lisp process made by \\[run-lisp]."
359 (error "Process lisp does not exist"))
361 (defvar lisp-interaction-mode-map
362 (let ((map (make-sparse-keymap)))
363 (set-keymap-parent map lisp-mode-shared-map
)
364 (define-key map
"\e\C-x" 'eval-defun
)
365 (define-key map
"\e\t" 'lisp-complete-symbol
)
366 (define-key map
"\n" 'eval-print-last-sexp
)
368 "Keymap for Lisp Interaction mode.
369 All commands in `lisp-mode-shared-map' are inherited by this map.")
371 (defvar lisp-interaction-mode-abbrev-table lisp-mode-abbrev-table
)
372 (define-derived-mode lisp-interaction-mode emacs-lisp-mode
"Lisp Interaction"
373 "Major mode for typing and evaluating Lisp forms.
374 Like Lisp mode except that \\[eval-print-last-sexp] evals the Lisp expression
375 before point, and prints its value into the buffer, advancing point.
376 Note that printing is controlled by `eval-expression-print-length'
377 and `eval-expression-print-level'.
380 Delete converts tabs to spaces as it moves back.
381 Paragraphs are separated only by blank lines.
382 Semicolons start comments.
383 \\{lisp-interaction-mode-map}
384 Entry to this mode calls the value of `lisp-interaction-mode-hook'
385 if that value is non-nil.")
387 (defun eval-print-last-sexp ()
388 "Evaluate sexp before point; print value into current buffer.
390 Note that printing the result is controlled by the variables
391 `eval-expression-print-length' and `eval-expression-print-level',
394 (let ((standard-output (current-buffer)))
400 (defun last-sexp-setup-props (beg end value alt1 alt2
)
401 "Set up text properties for the output of `eval-last-sexp-1'.
402 BEG and END are the start and end of the output in current-buffer.
403 VALUE is the Lisp value printed, ALT1 and ALT2 are strings for the
404 alternative printed representations that can be displayed."
405 (let ((map (make-sparse-keymap)))
406 (define-key map
"\C-m" 'last-sexp-toggle-display
)
407 (define-key map
[down-mouse-2
] 'mouse-set-point
)
408 (define-key map
[mouse-2
] 'last-sexp-toggle-display
)
411 `(printed-value (,value
,alt1
,alt2
)
414 help-echo
"RET, mouse-2: toggle abbreviated display"
415 rear-nonsticky
(mouse-face keymap help-echo
419 (defun last-sexp-toggle-display ()
420 "Toggle between abbreviated and unabbreviated printed representations."
422 (let ((value (get-text-property (point) 'printed-value
)))
424 (let ((beg (or (previous-single-property-change (point) 'printed-value
) (point)))
425 (end (or (next-single-char-property-change (point) 'printed-value
) (point)))
426 (standard-output (current-buffer))
428 (delete-region beg end
)
429 (insert (nth 1 value
))
430 (last-sexp-setup-props beg
(point)
434 (goto-char (min (point-max) point
))))))
437 (defun eval-last-sexp-1 (eval-last-sexp-arg-internal)
438 "Evaluate sexp before point; print value in minibuffer.
439 With argument, print output into current buffer."
440 (let ((standard-output (if eval-last-sexp-arg-internal
(current-buffer) t
)))
442 (eval (let ((stab (syntax-table))
448 (set-syntax-table emacs-lisp-mode-syntax-table
)
449 ;; If this sexp appears to be enclosed in `...'
450 ;; then ignore the surrounding quotes.
452 (or (eq (following-char) ?
\')
453 (eq (preceding-char) ?
\')))
455 ;; If we were after `?\e' (or similar case),
456 ;; use the whole thing, not just the `e'.
457 (when (eq (preceding-char) ?
\\)
459 (when (eq (preceding-char) ??
)
463 (when (eq (preceding-char) ?
=)
466 (skip-chars-backward "0-9#=")
467 (setq labeled-p
(looking-at "\\(#[0-9]+=\\)+")))
472 ;; vladimir@cs.ualberta.ca 30-Jul-1997: skip ` in
473 ;; `variable' so that the value is returned, not the
475 (if (and ignore-quotes
476 (eq (following-char) ?
`))
478 (narrow-to-region (point-min) opoint
)
479 (setq expr
(read (current-buffer)))
480 ;; If it's an (interactive ...) form, it's more
481 ;; useful to show how an interactive call would
484 (eq (car expr
) 'interactive
)
486 (list 'call-interactively
493 (set-syntax-table stab
))))))
494 (let ((unabbreviated (let ((print-length nil
) (print-level nil
))
495 (prin1-to-string value
)))
496 (print-length eval-expression-print-length
)
497 (print-level eval-expression-print-level
)
503 (when (and (bufferp standard-output
)
504 (or (not (null print-length
))
505 (not (null print-level
)))
506 (not (string= unabbreviated
507 (buffer-substring-no-properties beg end
))))
508 (last-sexp-setup-props beg end value
510 (buffer-substring-no-properties beg end
))
514 (defun eval-last-sexp (eval-last-sexp-arg-internal)
515 "Evaluate sexp before point; print value in minibuffer.
516 Interactively, with prefix argument, print output into current buffer."
518 (if (null eval-expression-debug-on-error
)
519 (eval-last-sexp-1 eval-last-sexp-arg-internal
)
520 (let ((old-value (make-symbol "t")) new-value value
)
521 (let ((debug-on-error old-value
))
522 (setq value
(eval-last-sexp-1 eval-last-sexp-arg-internal
))
523 (setq new-value debug-on-error
))
524 (unless (eq old-value new-value
)
525 (setq debug-on-error new-value
))
528 (defun eval-defun-1 (form)
529 "Change defvar into defconst within FORM.
530 Likewise for other constructs as necessary."
531 ;; The code in edebug-defun should be consistent with this, but not
532 ;; the same, since this gets a macroexpended form.
533 (cond ((not (listp form
))
535 ((and (eq (car form
) 'defvar
)
536 (cdr-safe (cdr-safe form
)))
537 ;; Force variable to be bound.
538 (cons 'defconst
(cdr form
)))
539 ;; `defcustom' is now macroexpanded to
540 ;; `custom-declare-variable' with a quoted value arg.
541 ((and (eq (car form
) 'custom-declare-variable
)
542 (default-boundp (eval (nth 1 form
))))
543 ;; Force variable to be bound.
544 (set-default (eval (nth 1 form
)) (eval (nth 1 (nth 2 form
))))
546 ((eq (car form
) 'progn
)
547 (cons 'progn
(mapcar 'eval-defun-1
(cdr form
))))
550 (defun eval-defun-2 ()
551 "Evaluate defun that point is in or before.
552 The value is displayed in the minibuffer.
553 If the current defun is actually a call to `defvar',
554 then reset the variable using the initial value expression
555 even if the variable already has some other value.
556 \(Normally `defvar' does not change the variable's value
557 if it already has a value.\)
559 With argument, insert value in current buffer after the defun.
560 Return the result of evaluation."
562 (let ((debug-on-error eval-expression-debug-on-error
)
563 (print-length eval-expression-print-length
)
564 (print-level eval-expression-print-level
))
566 ;; Arrange for eval-region to "read" the (possibly) altered form.
567 ;; eval-region handles recording which file defines a function or
568 ;; variable. Re-written using `apply' to avoid capturing
569 ;; variables like `end'.
572 (let ((standard-output t
)
574 ;; Read the form from the buffer, and record where it ends.
579 (setq form
(read (current-buffer)))
581 ;; Alter the form if necessary, changing defvar into defconst, etc.
582 (setq form
(eval-defun-1 (macroexpand form
)))
583 (list beg end standard-output
585 ;; Skipping to the end of the specified region
586 ;; will make eval-region return.
589 ;; The result of evaluation has been put onto VALUES. So return it.
592 (defun eval-defun (edebug-it)
593 "Evaluate the top-level form containing point, or after point.
595 If the current defun is actually a call to `defvar' or `defcustom',
596 evaluating it this way resets the variable using its initial value
597 expression even if the variable already has some other value.
598 \(Normally `defvar' and `defcustom' do not alter the value if there
601 With a prefix argument, instrument the code for Edebug.
603 If acting on a `defun' for FUNCTION, and the function was
604 instrumented, `Edebug: FUNCTION' is printed in the minibuffer. If not
605 instrumented, just FUNCTION is printed.
607 If not acting on a `defun', the result of evaluation is displayed in
608 the minibuffer. This display is controlled by the variables
609 `eval-expression-print-length' and `eval-expression-print-level',
614 (eval-defun (not edebug-all-defs
)))
616 (if (null eval-expression-debug-on-error
)
618 (let ((old-value (make-symbol "t")) new-value value
)
619 (let ((debug-on-error old-value
))
620 (setq value
(eval-defun-2))
621 (setq new-value debug-on-error
))
622 (unless (eq old-value new-value
)
623 (setq debug-on-error new-value
))
627 (defun lisp-comment-indent ()
628 (if (looking-at "\\s<\\s<\\s<")
630 (if (looking-at "\\s<\\s<")
631 (let ((tem (or (calculate-lisp-indent) (current-column))))
632 (if (listp tem
) (car tem
) tem
))
633 (skip-chars-backward " \t")
634 (max (if (bolp) 0 (1+ (current-column)))
637 ;; This function just forces a more costly detection of comments (using
638 ;; parse-partial-sexp from beginning-of-defun). I.e. It avoids the problem of
639 ;; taking a `;' inside a string started on another line for a comment starter.
640 ;; Note: `newcomment' gets it right in 99% of the cases if you're using
641 ;; font-lock, anyway, so we could get rid of it. -stef
642 (defun lisp-mode-auto-fill ()
643 (if (> (current-column) (current-fill-column))
645 (nth 4 (syntax-ppss (point))))
647 (unless (and (boundp 'comment-auto-fill-only-comments
)
648 comment-auto-fill-only-comments
)
649 (let ((comment-start nil
) (comment-start-skip nil
))
652 (defvar lisp-indent-offset nil
653 "If non-nil, indent second line of expressions that many more columns.")
654 (defvar lisp-indent-function
'lisp-indent-function
)
656 (defun lisp-indent-line (&optional whole-exp
)
657 "Indent current line as Lisp code.
658 With argument, indent any additional lines of the same expression
659 rigidly along with this one."
661 (let ((indent (calculate-lisp-indent)) shift-amt end
662 (pos (- (point-max) (point)))
663 (beg (progn (beginning-of-line) (point))))
664 (skip-chars-forward " \t")
665 (if (or (null indent
) (looking-at "\\s<\\s<\\s<"))
666 ;; Don't alter indentation of a ;;; comment line
667 ;; or a line that starts in a string.
668 (goto-char (- (point-max) pos
))
669 (if (and (looking-at "\\s<") (not (looking-at "\\s<\\s<")))
670 ;; Single-semicolon comment lines should be indented
671 ;; as comment lines, not as code.
672 (progn (indent-for-comment) (forward-char -
1))
673 (if (listp indent
) (setq indent
(car indent
)))
674 (setq shift-amt
(- indent
(current-column)))
675 (if (zerop shift-amt
)
677 (delete-region beg
(point))
679 ;; If initial point was within line's indentation,
680 ;; position after the indentation. Else stay at same point in text.
681 (if (> (- (point-max) pos
) (point))
682 (goto-char (- (point-max) pos
)))
683 ;; If desired, shift remaining lines of expression the same amount.
684 (and whole-exp
(not (zerop shift-amt
))
693 (indent-code-rigidly beg end shift-amt
)))))
695 (defvar calculate-lisp-indent-last-sexp
)
697 (defun calculate-lisp-indent (&optional parse-start
)
698 "Return appropriate indentation for current line as Lisp code.
699 In usual case returns an integer: the column to indent to.
700 If the value is nil, that means don't change the indentation
701 because the line starts inside a string.
703 The value can also be a list of the form (COLUMN CONTAINING-SEXP-START).
704 This means that following lines at the same level of indentation
705 should not necessarily be indented the same as this line.
706 Then COLUMN is the column to indent to, and CONTAINING-SEXP-START
707 is the buffer position of the start of the containing expression."
710 (let ((indent-point (point))
712 ;; setting this to a number inhibits calling hook
715 calculate-lisp-indent-last-sexp containing-sexp
)
717 (goto-char parse-start
)
718 (beginning-of-defun))
719 ;; Find outermost containing sexp
720 (while (< (point) indent-point
)
721 (setq state
(parse-partial-sexp (point) indent-point
0)))
722 ;; Find innermost containing sexp
725 (> (setq paren-depth
(elt state
0)) 0))
727 (setq calculate-lisp-indent-last-sexp
(elt state
2))
728 (setq containing-sexp
(elt state
1))
729 ;; Position following last unclosed open.
730 (goto-char (1+ containing-sexp
))
731 ;; Is there a complete sexp since then?
732 (if (and calculate-lisp-indent-last-sexp
733 (> calculate-lisp-indent-last-sexp
(point)))
734 ;; Yes, but is there a containing sexp after that?
735 (let ((peek (parse-partial-sexp calculate-lisp-indent-last-sexp
737 (if (setq retry
(car (cdr peek
))) (setq state peek
)))))
740 ;; Innermost containing sexp found
741 (goto-char (1+ containing-sexp
))
742 (if (not calculate-lisp-indent-last-sexp
)
743 ;; indent-point immediately follows open paren.
745 (setq desired-indent
(current-column))
746 ;; Find the start of first element of containing sexp.
747 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp
0 t
)
748 (cond ((looking-at "\\s(")
749 ;; First element of containing sexp is a list.
750 ;; Indent under that list.
752 ((> (save-excursion (forward-line 1) (point))
753 calculate-lisp-indent-last-sexp
)
754 ;; This is the first line to start within the containing sexp.
755 ;; It's almost certainly a function call.
756 (if (= (point) calculate-lisp-indent-last-sexp
)
757 ;; Containing sexp has nothing before this line
758 ;; except the first element. Indent under that element.
760 ;; Skip the first element, find start of second (the first
761 ;; argument of the function call) and indent under.
762 (progn (forward-sexp 1)
763 (parse-partial-sexp (point)
764 calculate-lisp-indent-last-sexp
766 (backward-prefix-chars))
768 ;; Indent beneath first sexp on same line as
769 ;; `calculate-lisp-indent-last-sexp'. Again, it's
770 ;; almost certainly a function call.
771 (goto-char calculate-lisp-indent-last-sexp
)
773 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp
775 (backward-prefix-chars)))))
776 ;; Point is at the point to indent under unless we are inside a string.
777 ;; Call indentation hook except when overridden by lisp-indent-offset
778 ;; or if the desired indentation has already been computed.
779 (let ((normal-indent (current-column)))
781 ;; Inside a string, don't change indentation.
783 ((and (integerp lisp-indent-offset
) containing-sexp
)
784 ;; Indent by constant offset
785 (goto-char containing-sexp
)
786 (+ (current-column) lisp-indent-offset
))
788 ((and (boundp 'lisp-indent-function
)
791 (or (funcall lisp-indent-function indent-point state
)
796 (defun lisp-indent-function (indent-point state
)
797 "This function is the normal value of the variable `lisp-indent-function'.
798 It is used when indenting a line within a function call, to see if the
799 called function says anything special about how to indent the line.
801 INDENT-POINT is the position where the user typed TAB, or equivalent.
802 Point is located at the point to indent under (for default indentation);
803 STATE is the `parse-partial-sexp' state for that position.
805 If the current line is in a call to a Lisp function
806 which has a non-nil property `lisp-indent-function',
807 that specifies how to do the indentation. The property value can be
808 * `defun', meaning indent `defun'-style;
809 * an integer N, meaning indent the first N arguments specially
810 like ordinary function arguments and then indent any further
811 aruments like a body;
812 * a function to call just as this function was called.
813 If that function returns nil, that means it doesn't specify
816 This function also returns nil meaning don't specify the indentation."
817 (let ((normal-indent (current-column)))
818 (goto-char (1+ (elt state
1)))
819 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp
0 t
)
820 (if (and (elt state
2)
821 (not (looking-at "\\sw\\|\\s_")))
822 ;; car of form doesn't seem to be a a symbol
824 (if (not (> (save-excursion (forward-line 1) (point))
825 calculate-lisp-indent-last-sexp
))
826 (progn (goto-char calculate-lisp-indent-last-sexp
)
828 (parse-partial-sexp (point)
829 calculate-lisp-indent-last-sexp
0 t
)))
830 ;; Indent under the list or under the first sexp on the same
831 ;; line as calculate-lisp-indent-last-sexp. Note that first
832 ;; thing on that line has to be complete sexp since we are
833 ;; inside the innermost containing sexp.
834 (backward-prefix-chars)
836 (let ((function (buffer-substring (point)
837 (progn (forward-sexp 1) (point))))
839 (setq method
(or (get (intern-soft function
) 'lisp-indent-function
)
840 (get (intern-soft function
) 'lisp-indent-hook
)))
841 (cond ((or (eq method
'defun
)
843 (> (length function
) 3)
844 (string-match "\\`def" function
)))
845 (lisp-indent-defform state indent-point
))
847 (lisp-indent-specform method state
848 indent-point normal-indent
))
850 (funcall method state indent-point
)))))))
852 (defvar lisp-body-indent
2
853 "Number of columns to indent the second line of a `(def...)' form.")
855 (defun lisp-indent-specform (count state indent-point normal-indent
)
856 (let ((containing-form-start (elt state
1))
858 body-indent containing-form-column
)
859 ;; Move to the start of containing form, calculate indentation
860 ;; to use for non-distinguished forms (> count), and move past the
861 ;; function symbol. lisp-indent-function guarantees that there is at
862 ;; least one word or symbol character following open paren of containing
864 (goto-char containing-form-start
)
865 (setq containing-form-column
(current-column))
866 (setq body-indent
(+ lisp-body-indent containing-form-column
))
869 ;; Now find the start of the last form.
870 (parse-partial-sexp (point) indent-point
1 t
)
871 (while (and (< (point) indent-point
)
874 (setq count
(1- count
))
876 (parse-partial-sexp (point) indent-point
1 t
))
878 ;; Point is sitting on first character of last (or count) sexp.
880 ;; A distinguished form. If it is the first or second form use double
881 ;; lisp-body-indent, else normal indent. With lisp-body-indent bound
882 ;; to 2 (the default), this just happens to work the same with if as
883 ;; the older code, but it makes unwind-protect, condition-case,
884 ;; with-output-to-temp-buffer, et. al. much more tasteful. The older,
885 ;; less hacked, behavior can be obtained by replacing below with
886 ;; (list normal-indent containing-form-start).
887 (if (<= (- i count
) 1)
888 (list (+ containing-form-column
(* 2 lisp-body-indent
))
889 containing-form-start
)
890 (list normal-indent containing-form-start
))
891 ;; A non-distinguished form. Use body-indent if there are no
892 ;; distinguished forms and this is the first undistinguished form,
893 ;; or if this is the first undistinguished form and the preceding
894 ;; distinguished form has indentation at least as great as body-indent.
895 (if (or (and (= i
0) (= count
0))
896 (and (= count
0) (<= body-indent normal-indent
)))
900 (defun lisp-indent-defform (state indent-point
)
901 (goto-char (car (cdr state
)))
903 (if (> (point) (car (cdr (cdr state
))))
905 (goto-char (car (cdr state
)))
906 (+ lisp-body-indent
(current-column)))))
909 ;; (put 'progn 'lisp-indent-function 0), say, causes progn to be indented
910 ;; like defun if the first form is placed on the next line, otherwise
911 ;; it is indented like any other form (i.e. forms line up under first).
913 (put 'lambda
'lisp-indent-function
'defun
)
914 (put 'autoload
'lisp-indent-function
'defun
)
915 (put 'progn
'lisp-indent-function
0)
916 (put 'prog1
'lisp-indent-function
1)
917 (put 'prog2
'lisp-indent-function
2)
918 (put 'save-excursion
'lisp-indent-function
0)
919 (put 'save-window-excursion
'lisp-indent-function
0)
920 (put 'save-selected-window
'lisp-indent-function
0)
921 (put 'save-restriction
'lisp-indent-function
0)
922 (put 'save-match-data
'lisp-indent-function
0)
923 (put 'save-current-buffer
'lisp-indent-function
0)
924 (put 'with-current-buffer
'lisp-indent-function
1)
925 (put 'combine-after-change-calls
'lisp-indent-function
0)
926 (put 'with-output-to-string
'lisp-indent-function
0)
927 (put 'with-temp-file
'lisp-indent-function
1)
928 (put 'with-temp-buffer
'lisp-indent-function
0)
929 (put 'with-temp-message
'lisp-indent-function
1)
930 (put 'with-syntax-table
'lisp-indent-function
1)
931 (put 'let
'lisp-indent-function
1)
932 (put 'let
* 'lisp-indent-function
1)
933 (put 'while
'lisp-indent-function
1)
934 (put 'if
'lisp-indent-function
2)
935 (put 'read-if
'lisp-indent-function
2)
936 (put 'catch
'lisp-indent-function
1)
937 (put 'condition-case
'lisp-indent-function
2)
938 (put 'unwind-protect
'lisp-indent-function
1)
939 (put 'with-output-to-temp-buffer
'lisp-indent-function
1)
940 (put 'eval-after-load
'lisp-indent-function
1)
941 (put 'dolist
'lisp-indent-function
1)
942 (put 'dotimes
'lisp-indent-function
1)
943 (put 'when
'lisp-indent-function
1)
944 (put 'unless
'lisp-indent-function
1)
946 (defun indent-sexp (&optional endpos
)
947 "Indent each line of the list starting just after point.
948 If optional arg ENDPOS is given, indent each line, stopping when
949 ENDPOS is encountered."
951 (let ((indent-stack (list nil
))
953 ;; If ENDPOS is non-nil, use nil as STARTING-POINT
954 ;; so that calculate-lisp-indent will find the beginning of
955 ;; the defun we are in.
956 ;; If ENDPOS is nil, it is safe not to scan before point
957 ;; since every line we indent is more deeply nested than point is.
958 (starting-point (if endpos nil
(point)))
960 last-depth bol outer-loop-done inner-loop-done state this-indent
)
962 ;; Get error now if we don't have a complete sexp after point.
963 (save-excursion (forward-sexp 1)))
965 (setq outer-loop-done nil
)
966 (while (if endpos
(< (point) endpos
)
967 (not outer-loop-done
))
968 (setq last-depth next-depth
970 ;; Parse this line so we can learn the state
971 ;; to indent the next line.
972 ;; This inner loop goes through only once
973 ;; unless a line ends inside a string.
974 (while (and (not inner-loop-done
)
975 (not (setq outer-loop-done
(eobp))))
976 (setq state
(parse-partial-sexp (point) (progn (end-of-line) (point))
978 (setq next-depth
(car state
))
979 ;; If the line contains a comment other than the sort
980 ;; that is indented like code,
981 ;; indent it now with indent-for-comment.
982 ;; Comments indented like code are right already.
983 ;; In any case clear the in-comment flag in the state
984 ;; because parse-partial-sexp never sees the newlines.
985 (if (car (nthcdr 4 state
))
986 (progn (indent-for-comment)
988 (setcar (nthcdr 4 state
) nil
)))
989 ;; If this line ends inside a string,
990 ;; go straight to next line, remaining within the inner loop,
991 ;; and turn off the \-flag.
992 (if (car (nthcdr 3 state
))
995 (setcar (nthcdr 5 state
) nil
))
996 (setq inner-loop-done t
)))
1000 (setq indent-stack
(nconc indent-stack
1001 (make-list (- next-depth
) nil
))
1002 last-depth
(- last-depth next-depth
)
1004 (or outer-loop-done endpos
1005 (setq outer-loop-done
(<= next-depth
0)))
1008 (while (> last-depth next-depth
)
1009 (setq indent-stack
(cdr indent-stack
)
1010 last-depth
(1- last-depth
)))
1011 (while (< last-depth next-depth
)
1012 (setq indent-stack
(cons nil indent-stack
)
1013 last-depth
(1+ last-depth
)))
1014 ;; Now go to the next line and indent it according
1015 ;; to what we learned from parsing the previous one.
1018 (skip-chars-forward " \t")
1019 ;; But not if the line is blank, or just a comment
1020 ;; (except for double-semi comments; indent them as usual).
1021 (if (or (eobp) (looking-at "\\s<\\|\n"))
1023 (if (and (car indent-stack
)
1024 (>= (car indent-stack
) 0))
1025 (setq this-indent
(car indent-stack
))
1026 (let ((val (calculate-lisp-indent
1027 (if (car indent-stack
) (- (car indent-stack
))
1030 (setq this-indent val
)
1032 (setcar indent-stack
1033 (setq this-indent val
))
1034 (setcar indent-stack
(- (car (cdr val
))))
1035 (setq this-indent
(car val
))))))
1036 (if (and this-indent
(/= (current-column) this-indent
))
1037 (progn (delete-region bol
(point))
1038 (indent-to this-indent
)))))
1040 (setq outer-loop-done
(= (point) last-point
))
1041 (setq last-point
(point)))))))
1043 (defun lisp-indent-region (start end
)
1044 "Indent every line whose first char is between START and END inclusive."
1046 (let ((endmark (copy-marker end
)))
1048 (and (bolp) (not (eolp))
1050 (indent-sexp endmark
)
1051 (set-marker endmark nil
))))
1053 ;;;; Lisp paragraph filling commands.
1055 (defun lisp-fill-paragraph (&optional justify
)
1056 "Like \\[fill-paragraph], but handle Emacs Lisp comments.
1057 If any of the current line is a comment, fill the comment or the
1058 paragraph of it that point is in, preserving the comment's indentation
1059 and initial semicolons."
1062 ;; Non-nil if the current line contains a comment.
1065 ;; Non-nil if the current line contains code and a comment.
1066 has-code-and-comment
1068 ;; If has-comment, the appropriate fill-prefix for the comment.
1072 ;; Figure out what kind of comment we are looking at.
1077 ;; A line with nothing but a comment on it?
1078 ((looking-at "[ \t]*;[; \t]*")
1080 comment-fill-prefix
(buffer-substring (match-beginning 0)
1083 ;; A line with some code, followed by a comment? Remember that the
1084 ;; semi which starts the comment shouldn't be part of a string or
1086 ((let ((state (syntax-ppss (line-end-position))))
1088 (goto-char (nth 8 state
))
1089 (looking-at ";+[\t ]*")))
1090 (setq has-comment t has-code-and-comment t
)
1091 (setq comment-fill-prefix
1092 (concat (make-string (/ (current-column) tab-width
) ?
\t)
1093 (make-string (%
(current-column) tab-width
) ?\
)
1094 (buffer-substring (match-beginning 0) (match-end 0)))))))
1096 (if (not has-comment
)
1097 ;; `paragraph-start' is set here (not in the buffer-local
1098 ;; variable so that `forward-paragraph' et al work as
1099 ;; expected) so that filling (doc) strings works sensibly.
1100 ;; Adding the opening paren to avoid the following sexp being
1101 ;; filled means that sexps generally aren't filled as normal
1102 ;; text, which is probably sensible. The `;' and `:' stop the
1103 ;; filled para at following comment lines and keywords
1104 ;; (typically in `defcustom').
1105 (let ((paragraph-start (concat paragraph-start
1106 "\\|\\s-*[\(;:\"]"))
1107 ;; Avoid filling the first line of docstring.
1109 (concat paragraph-separate
"\\|\\s-*\".*\\.$")))
1110 (fill-paragraph justify
))
1112 ;; Narrow to include only the comment, and then fill the region.
1117 ;; Find the first line we should include in the region to fill.
1119 (while (and (zerop (forward-line -
1))
1120 (looking-at "[ \t]*;")))
1121 ;; We may have gone too far. Go forward again.
1122 (or (looking-at ".*;")
1125 ;; Find the beginning of the first line past the region to fill.
1127 (while (progn (forward-line 1)
1128 (looking-at "[ \t]*;")))
1131 ;; Lines with only semicolons on them can be paragraph boundaries.
1132 (let* ((paragraph-start (concat paragraph-start
"\\|[ \t;]*$"))
1133 (paragraph-separate (concat paragraph-start
"\\|[ \t;]*$"))
1134 (paragraph-ignore-fill-prefix nil
)
1135 (fill-prefix comment-fill-prefix
)
1136 (after-line (if has-code-and-comment
1138 (forward-line 1) (point))))
1141 (or (bolp) (newline 1))
1143 ;; If this comment starts on a line with code,
1144 ;; include that like in the filling.
1145 (beg (progn (backward-paragraph)
1146 (if (eq (point) after-line
)
1149 (fill-region-as-paragraph beg end
1153 (if (looking-at fill-prefix
)
1155 (re-search-forward comment-start-skip
)
1159 (defun indent-code-rigidly (start end arg
&optional nochange-regexp
)
1160 "Indent all lines of code, starting in the region, sideways by ARG columns.
1161 Does not affect lines starting inside comments or strings, assuming that
1162 the start of the region is not inside them.
1164 Called from a program, takes args START, END, COLUMNS and NOCHANGE-REGEXP.
1165 The last is a regexp which, if matched at the beginning of a line,
1166 means don't indent that line."
1167 (interactive "r\np")
1171 (setq end
(point-marker))
1174 (setq state
(parse-partial-sexp (point)
1176 (forward-line 1) (point))
1178 (while (< (point) end
)
1179 (or (car (nthcdr 3 state
))
1180 (and nochange-regexp
1181 (looking-at nochange-regexp
))
1182 ;; If line does not start in string, indent it
1183 (let ((indent (current-indentation)))
1184 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
1186 (indent-to (max 0 (+ indent arg
)) 0))))
1187 (setq state
(parse-partial-sexp (point)
1189 (forward-line 1) (point))
1192 (provide 'lisp-mode
)
1194 ;;; lisp-mode.el ends here