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 'defsubst
'doc-string-elt
3)
127 (put 'define-skeleton
'doc-string-elt
2)
128 (put 'define-derived-mode
'doc-string-elt
4)
129 (put 'easy-mmode-define-minor-mode
'doc-string-elt
2)
130 (put 'define-minor-mode
'doc-string-elt
2)
131 (put 'define-generic-mode
'doc-string-elt
7)
132 ;; define-global-mode has no explicit docstring.
133 (put 'easy-mmode-define-global-mode
'doc-string-elt
0)
136 (defun lisp-font-lock-syntactic-face-function (state)
138 (if (and (eq (nth 0 state
) 1)
139 ;; This might be a docstring.
142 (goto-char (nth 8 state
))
144 (while (progn (backward-sexp 1) (setq n
(1+ n
))))
147 (let ((sym (intern-soft
149 (point) (progn (forward-sexp 1) (point))))))
150 (eq n
(or (get sym
'doc-string-elt
) 3)))))))
152 font-lock-string-face
)
153 font-lock-comment-face
))
155 ;; The LISP-SYNTAX argument is used by code in inf-lisp.el and is
156 ;; (uselessly) passed from pp.el, chistory.el, gnus-kill.el and score-mode.el
157 (defun lisp-mode-variables (&optional lisp-syntax
)
159 (set-syntax-table lisp-mode-syntax-table
))
160 (setq local-abbrev-table lisp-mode-abbrev-table
)
161 (make-local-variable 'paragraph-ignore-fill-prefix
)
162 (setq paragraph-ignore-fill-prefix t
)
163 (make-local-variable 'fill-paragraph-function
)
164 (setq fill-paragraph-function
'lisp-fill-paragraph
)
165 ;; Adaptive fill mode gets in the way of auto-fill,
166 ;; and should make no difference for explicit fill
167 ;; because lisp-fill-paragraph should do the job.
168 ;; I believe that newcomment's auto-fill code properly deals with it -stef
169 ;;(set (make-local-variable 'adaptive-fill-mode) nil)
170 (make-local-variable 'normal-auto-fill-function
)
171 (setq normal-auto-fill-function
'lisp-mode-auto-fill
)
172 (make-local-variable 'indent-line-function
)
173 (setq indent-line-function
'lisp-indent-line
)
174 (make-local-variable 'indent-region-function
)
175 (setq indent-region-function
'lisp-indent-region
)
176 (make-local-variable 'parse-sexp-ignore-comments
)
177 (setq parse-sexp-ignore-comments t
)
178 (make-local-variable 'outline-regexp
)
179 (setq outline-regexp
";;;;* \\|(")
180 (make-local-variable 'outline-level
)
181 (setq outline-level
'lisp-outline-level
)
182 (make-local-variable 'comment-start
)
183 (setq comment-start
";")
184 (make-local-variable 'comment-start-skip
)
185 ;; Look within the line for a ; following an even number of backslashes
186 ;; after either a non-backslash or the line beginning.
187 (setq comment-start-skip
"\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\);+ *")
188 (make-local-variable 'comment-add
)
189 (setq comment-add
1) ;default to `;;' in comment-region
190 (make-local-variable 'comment-column
)
191 (setq comment-column
40)
192 (make-local-variable 'comment-indent-function
)
193 (setq comment-indent-function
'lisp-comment-indent
)
194 (make-local-variable 'imenu-generic-expression
)
195 (setq imenu-generic-expression lisp-imenu-generic-expression
)
196 (make-local-variable 'multibyte-syntax-as-symbol
)
197 (setq multibyte-syntax-as-symbol t
)
198 (setq font-lock-defaults
199 '((lisp-font-lock-keywords
200 lisp-font-lock-keywords-1 lisp-font-lock-keywords-2
)
201 nil nil
(("+-*/.<>=!?$%_&~^:" .
"w")) beginning-of-defun
202 (font-lock-mark-block-function . mark-defun
)
203 (font-lock-syntactic-face-function
204 . lisp-font-lock-syntactic-face-function
))))
206 (defun lisp-outline-level ()
207 "Lisp mode `outline-level' function."
210 (looking-at outline-regexp
)
211 (- (match-end 0) (match-beginning 0))))
214 (defvar lisp-mode-shared-map
215 (let ((map (make-sparse-keymap)))
216 (define-key map
"\t" 'lisp-indent-line
)
217 (define-key map
"\e\C-q" 'indent-sexp
)
218 (define-key map
"\177" 'backward-delete-char-untabify
)
219 ;; This gets in the way when viewing a Lisp file in view-mode. As
220 ;; long as [backspace] is mapped into DEL via the
221 ;; function-key-map, this should remain disabled!!
222 ;;;(define-key map [backspace] 'backward-delete-char-untabify)
224 "Keymap for commands shared by all sorts of Lisp modes.")
226 (defvar emacs-lisp-mode-map
()
227 "Keymap for Emacs Lisp mode.
228 All commands in `lisp-mode-shared-map' are inherited by this map.")
230 (if emacs-lisp-mode-map
232 (let ((map (make-sparse-keymap "Emacs-Lisp")))
233 (setq emacs-lisp-mode-map
(make-sparse-keymap))
234 (set-keymap-parent emacs-lisp-mode-map lisp-mode-shared-map
)
235 (define-key emacs-lisp-mode-map
"\e\t" 'lisp-complete-symbol
)
236 (define-key emacs-lisp-mode-map
"\e\C-x" 'eval-defun
)
237 (define-key emacs-lisp-mode-map
[menu-bar
] (make-sparse-keymap))
238 (define-key emacs-lisp-mode-map
[menu-bar emacs-lisp
]
239 (cons "Emacs-Lisp" map
))
240 (define-key map
[edebug-defun
]
241 '("Instrument Function for Debugging" . edebug-defun
))
242 (define-key map
[byte-recompile
]
243 '("Byte-recompile Directory..." . byte-recompile-directory
))
244 (define-key map
[emacs-byte-compile-and-load
]
245 '("Byte-compile And Load" . emacs-lisp-byte-compile-and-load
))
246 (define-key map
[byte-compile
]
247 '("Byte-compile This File" . emacs-lisp-byte-compile
))
248 (define-key map
[separator-eval
] '("--"))
249 (define-key map
[eval-buffer
] '("Evaluate Buffer" . eval-current-buffer
))
250 (define-key map
[eval-region
] '("Evaluate Region" . eval-region
))
251 (define-key map
[eval-sexp
] '("Evaluate Last S-expression" . eval-last-sexp
))
252 (define-key map
[separator-format
] '("--"))
253 (define-key map
[comment-region
] '("Comment Out Region" . comment-region
))
254 (define-key map
[indent-region
] '("Indent Region" . indent-region
))
255 (define-key map
[indent-line
] '("Indent Line" . lisp-indent-line
))
256 (put 'eval-region
'menu-enable
'mark-active
)
257 (put 'comment-region
'menu-enable
'mark-active
)
258 (put 'indent-region
'menu-enable
'mark-active
)))
260 (defun emacs-lisp-byte-compile ()
261 "Byte compile the file containing the current buffer."
264 (byte-compile-file buffer-file-name
)
265 (error "The buffer must be saved in a file first")))
267 (defun emacs-lisp-byte-compile-and-load ()
268 "Byte-compile the current file (if it has changed), then load compiled code."
271 (error "The buffer must be saved in a file first"))
273 ;; Recompile if file or buffer has changed since last compilation.
274 (if (and (buffer-modified-p)
275 (y-or-n-p (format "Save buffer %s first? " (buffer-name))))
277 (let ((compiled-file-name (byte-compile-dest-file buffer-file-name
)))
278 (if (file-newer-than-file-p compiled-file-name buffer-file-name
)
279 (load-file compiled-file-name
)
280 (byte-compile-file buffer-file-name t
))))
282 (defcustom emacs-lisp-mode-hook nil
283 "Hook run when entering Emacs Lisp mode."
284 :options
'(turn-on-eldoc-mode imenu-add-menubar-index checkdoc-minor-mode
)
288 (defcustom lisp-mode-hook nil
289 "Hook run when entering Lisp mode."
290 :options
'(imenu-add-menubar-index)
294 (defcustom lisp-interaction-mode-hook nil
295 "Hook run when entering Lisp Interaction mode."
296 :options
'(turn-on-eldoc-mode)
300 (define-derived-mode emacs-lisp-mode nil
"Emacs-Lisp"
301 "Major mode for editing Lisp code to run in Emacs.
303 Delete converts tabs to spaces as it moves back.
304 Blank lines separate paragraphs. Semicolons start comments.
305 \\{emacs-lisp-mode-map}
306 Entry to this mode calls the value of `emacs-lisp-mode-hook'
307 if that value is non-nil."
308 (lisp-mode-variables)
309 (setq imenu-case-fold-search nil
))
311 (defvar lisp-mode-map
312 (let ((map (make-sparse-keymap)))
313 (set-keymap-parent map lisp-mode-shared-map
)
314 (define-key map
"\e\C-x" 'lisp-eval-defun
)
315 (define-key map
"\C-c\C-z" 'run-lisp
)
317 "Keymap for ordinary Lisp mode.
318 All commands in `lisp-mode-shared-map' are inherited by this map.")
320 (define-derived-mode lisp-mode nil
"Lisp"
321 "Major mode for editing Lisp code for Lisps other than GNU Emacs Lisp.
323 Delete converts tabs to spaces as it moves back.
324 Blank lines separate paragraphs. Semicolons start comments.
326 Note that `run-lisp' may be used either to start an inferior Lisp job
327 or to switch back to an existing one.
329 Entry to this mode calls the value of `lisp-mode-hook'
330 if that value is non-nil."
331 (lisp-mode-variables)
332 (set (make-local-variable 'comment-start-skip
)
333 "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\)\\(;+\\|#|\\) *")
334 (set (make-local-variable 'font-lock-keywords-case-fold-search
) t
)
335 (setq imenu-case-fold-search t
))
337 ;; This will do unless inf-lisp.el is loaded.
338 (defun lisp-eval-defun (&optional and-go
)
339 "Send the current defun to the Lisp process made by \\[run-lisp]."
341 (error "Process lisp does not exist"))
343 (defvar lisp-interaction-mode-map
344 (let ((map (make-sparse-keymap)))
345 (set-keymap-parent map lisp-mode-shared-map
)
346 (define-key map
"\e\C-x" 'eval-defun
)
347 (define-key map
"\e\t" 'lisp-complete-symbol
)
348 (define-key map
"\n" 'eval-print-last-sexp
)
350 "Keymap for Lisp Interaction mode.
351 All commands in `lisp-mode-shared-map' are inherited by this map.")
353 (define-derived-mode lisp-interaction-mode emacs-lisp-mode
"Lisp Interaction"
354 "Major mode for typing and evaluating Lisp forms.
355 Like Lisp mode except that \\[eval-print-last-sexp] evals the Lisp expression
356 before point, and prints its value into the buffer, advancing point.
357 Note that printing is controled by `eval-expression-print-length'
358 and `eval-expression-print-level'.
361 Delete converts tabs to spaces as it moves back.
362 Paragraphs are separated only by blank lines.
363 Semicolons start comments.
364 \\{lisp-interaction-mode-map}
365 Entry to this mode calls the value of `lisp-interaction-mode-hook'
366 if that value is non-nil.")
368 (defun eval-print-last-sexp ()
369 "Evaluate sexp before point; print value into current buffer.
371 Note that printing the result is controlled by the variables
372 `eval-expression-print-length' and `eval-expression-print-level',
375 (let ((standard-output (current-buffer)))
381 (defun last-sexp-setup-props (beg end value alt1 alt2
)
382 "Set up text properties for the output of `eval-last-sexp-1'.
383 BEG and END are the start and end of the output in current-buffer.
384 VALUE is the Lisp value printed, ALT1 and ALT2 are strings for the
385 alternative printed representations that can be displayed."
386 (let ((map (make-sparse-keymap)))
387 (define-key map
"\C-m" 'last-sexp-toggle-display
)
388 (define-key map
[down-mouse-2
] 'mouse-set-point
)
389 (define-key map
[mouse-2
] 'last-sexp-toggle-display
)
392 `(printed-value (,value
,alt1
,alt2
)
395 help-echo
"RET, mouse-2: toggle abbreviated display"
396 rear-nonsticky
(mouse-face keymap help-echo
400 (defun last-sexp-toggle-display ()
401 "Toggle between abbreviated and unabbreviated printed representations."
403 (let ((value (get-text-property (point) 'printed-value
)))
405 (let ((beg (or (previous-single-property-change (point) 'printed-value
) (point)))
406 (end (or (next-single-char-property-change (point) 'printed-value
) (point)))
407 (standard-output (current-buffer))
409 (delete-region beg end
)
410 (insert (nth 1 value
))
411 (last-sexp-setup-props beg
(point)
415 (goto-char (min (point-max) point
))))))
418 (defun eval-last-sexp-1 (eval-last-sexp-arg-internal)
419 "Evaluate sexp before point; print value in minibuffer.
420 With argument, print output into current buffer."
421 (let ((standard-output (if eval-last-sexp-arg-internal
(current-buffer) t
)))
423 (eval (let ((stab (syntax-table))
429 (set-syntax-table emacs-lisp-mode-syntax-table
)
430 ;; If this sexp appears to be enclosed in `...'
431 ;; then ignore the surrounding quotes.
433 (or (eq (following-char) ?
\')
434 (eq (preceding-char) ?
\')))
436 ;; If we were after `?\e' (or similar case),
437 ;; use the whole thing, not just the `e'.
438 (when (eq (preceding-char) ?
\\)
440 (when (eq (preceding-char) ??
)
444 (when (eq (preceding-char) ?
=)
447 (skip-chars-backward "0-9#=")
448 (setq labeled-p
(looking-at "\\(#[0-9]+=\\)+")))
453 ;; vladimir@cs.ualberta.ca 30-Jul-1997: skip ` in
454 ;; `variable' so that the value is returned, not the
456 (if (and ignore-quotes
457 (eq (following-char) ?
`))
459 (narrow-to-region (point-min) opoint
)
460 (setq expr
(read (current-buffer)))
461 ;; If it's an (interactive ...) form, it's more
462 ;; useful to show how an interactive call would
465 (eq (car expr
) 'interactive
)
467 (list 'call-interactively
474 (set-syntax-table stab
))))))
475 (let ((unabbreviated (let ((print-length nil
) (print-level nil
))
476 (prin1-to-string value
)))
477 (print-length eval-expression-print-length
)
478 (print-level eval-expression-print-level
)
484 (when (and (bufferp standard-output
)
485 (or (not (null print-length
))
486 (not (null print-level
)))
487 (not (string= unabbreviated
488 (buffer-substring-no-properties beg end
))))
489 (last-sexp-setup-props beg end value
491 (buffer-substring-no-properties beg end
))
495 (defun eval-last-sexp (eval-last-sexp-arg-internal)
496 "Evaluate sexp before point; print value in minibuffer.
497 Interactively, with prefix argument, print output into current buffer."
499 (if (null eval-expression-debug-on-error
)
500 (eval-last-sexp-1 eval-last-sexp-arg-internal
)
501 (let ((old-value (make-symbol "t")) new-value value
)
502 (let ((debug-on-error old-value
))
503 (setq value
(eval-last-sexp-1 eval-last-sexp-arg-internal
))
504 (setq new-value debug-on-error
))
505 (unless (eq old-value new-value
)
506 (setq debug-on-error new-value
))
509 (defun eval-defun-1 (form)
510 "Change defvar into defconst within FORM.
511 Likewise for other constructs as necessary."
512 ;; The code in edebug-defun should be consistent with this, but not
513 ;; the same, since this gets a macroexpended form.
514 (cond ((and (eq (car form
) 'defvar
)
515 (cdr-safe (cdr-safe form
)))
516 ;; Force variable to be bound.
517 (cons 'defconst
(cdr form
)))
518 ;; `defcustom' is now macroexpanded to
519 ;; `custom-declare-variable' with a quoted value arg.
520 ((and (eq (car form
) 'custom-declare-variable
)
521 (default-boundp (eval (nth 1 form
))))
522 ;; Force variable to be bound.
523 (set-default (eval (nth 1 form
)) (eval (nth 1 (nth 2 form
))))
525 ((eq (car form
) 'progn
)
526 (cons 'progn
(mapcar 'eval-defun-1
(cdr form
))))
529 (defun eval-defun-2 ()
530 "Evaluate defun that point is in or before.
531 The value is displayed in the minibuffer.
532 If the current defun is actually a call to `defvar',
533 then reset the variable using the initial value expression
534 even if the variable already has some other value.
535 \(Normally `defvar' does not change the variable's value
536 if it already has a value.\)
538 With argument, insert value in current buffer after the defun.
539 Return the result of evaluation."
541 (let ((debug-on-error eval-expression-debug-on-error
)
542 (print-length eval-expression-print-length
)
543 (print-level eval-expression-print-level
))
545 ;; Arrange for eval-region to "read" the (possibly) altered form.
546 ;; eval-region handles recording which file defines a function or
547 ;; variable. Re-written using `apply' to avoid capturing
548 ;; variables like `end'.
551 (let ((standard-output t
)
553 ;; Read the form from the buffer, and record where it ends.
558 (setq form
(read (current-buffer)))
560 ;; Alter the form if necessary, changing defvar into defconst, etc.
561 (setq form
(eval-defun-1 (macroexpand form
)))
562 (list beg end standard-output
564 ;; Skipping to the end of the specified region
565 ;; will make eval-region return.
568 ;; The result of evaluation has been put onto VALUES. So return it.
571 (defun eval-defun (edebug-it)
572 "Evaluate the top-level form containing point, or after point.
574 If the current defun is actually a call to `defvar' or `defcustom',
575 evaluating it this way resets the variable using its initial value
576 expression even if the variable already has some other value.
577 \(Normally `defvar' and `defcustom' do not alter the value if there
580 With a prefix argument, instrument the code for Edebug.
582 If acting on a `defun' for FUNCTION, and the function was
583 instrumented, `Edebug: FUNCTION' is printed in the minibuffer. If not
584 instrumented, just FUNCTION is printed.
586 If not acting on a `defun', the result of evaluation is displayed in
587 the minibuffer. This display is controlled by the variables
588 `eval-expression-print-length' and `eval-expression-print-level',
593 (eval-defun (not edebug-all-defs
)))
595 (if (null eval-expression-debug-on-error
)
597 (let ((old-value (make-symbol "t")) new-value value
)
598 (let ((debug-on-error old-value
))
599 (setq value
(eval-defun-2))
600 (setq new-value debug-on-error
))
601 (unless (eq old-value new-value
)
602 (setq debug-on-error new-value
))
606 (defun lisp-comment-indent ()
607 (if (looking-at "\\s<\\s<\\s<")
609 (if (looking-at "\\s<\\s<")
610 (let ((tem (or (calculate-lisp-indent) (current-column))))
611 (if (listp tem
) (car tem
) tem
))
612 (skip-chars-backward " \t")
613 (max (if (bolp) 0 (1+ (current-column)))
616 ;; This function just forces a more costly detection of comments (using
617 ;; parse-partial-sexp from beginning-of-defun). I.e. It avoids the problem of
618 ;; taking a `;' inside a string started on another line for a comment starter.
619 ;; Note: `newcomment' gets it right in 99% of the cases if you're using
620 ;; font-lock, anyway, so we could get rid of it. -stef
621 (defun lisp-mode-auto-fill ()
622 (if (> (current-column) (current-fill-column))
624 (nth 4 (parse-partial-sexp (save-excursion
629 (let ((comment-start nil
) (comment-start-skip nil
))
632 (defvar lisp-indent-offset nil
633 "If non-nil, indent second line of expressions that many more columns.")
634 (defvar lisp-indent-function
'lisp-indent-function
)
636 (defun lisp-indent-line (&optional whole-exp
)
637 "Indent current line as Lisp code.
638 With argument, indent any additional lines of the same expression
639 rigidly along with this one."
641 (let ((indent (calculate-lisp-indent)) shift-amt end
642 (pos (- (point-max) (point)))
643 (beg (progn (beginning-of-line) (point))))
644 (skip-chars-forward " \t")
645 (if (or (null indent
) (looking-at "\\s<\\s<\\s<"))
646 ;; Don't alter indentation of a ;;; comment line
647 ;; or a line that starts in a string.
648 (goto-char (- (point-max) pos
))
649 (if (and (looking-at "\\s<") (not (looking-at "\\s<\\s<")))
650 ;; Single-semicolon comment lines should be indented
651 ;; as comment lines, not as code.
652 (progn (indent-for-comment) (forward-char -
1))
653 (if (listp indent
) (setq indent
(car indent
)))
654 (setq shift-amt
(- indent
(current-column)))
655 (if (zerop shift-amt
)
657 (delete-region beg
(point))
659 ;; If initial point was within line's indentation,
660 ;; position after the indentation. Else stay at same point in text.
661 (if (> (- (point-max) pos
) (point))
662 (goto-char (- (point-max) pos
)))
663 ;; If desired, shift remaining lines of expression the same amount.
664 (and whole-exp
(not (zerop shift-amt
))
673 (indent-code-rigidly beg end shift-amt
)))))
675 (defvar calculate-lisp-indent-last-sexp
)
677 (defun calculate-lisp-indent (&optional parse-start
)
678 "Return appropriate indentation for current line as Lisp code.
679 In usual case returns an integer: the column to indent to.
680 If the value is nil, that means don't change the indentation
681 because the line starts inside a string.
683 The value can also be a list of the form (COLUMN CONTAINING-SEXP-START).
684 This means that following lines at the same level of indentation
685 should not necessarily be indented the same as this line.
686 Then COLUMN is the column to indent to, and CONTAINING-SEXP-START
687 is the buffer position of the start of the containing expression."
690 (let ((indent-point (point))
692 ;; setting this to a number inhibits calling hook
695 calculate-lisp-indent-last-sexp containing-sexp
)
697 (goto-char parse-start
)
698 (beginning-of-defun))
699 ;; Find outermost containing sexp
700 (while (< (point) indent-point
)
701 (setq state
(parse-partial-sexp (point) indent-point
0)))
702 ;; Find innermost containing sexp
705 (> (setq paren-depth
(elt state
0)) 0))
707 (setq calculate-lisp-indent-last-sexp
(elt state
2))
708 (setq containing-sexp
(elt state
1))
709 ;; Position following last unclosed open.
710 (goto-char (1+ containing-sexp
))
711 ;; Is there a complete sexp since then?
712 (if (and calculate-lisp-indent-last-sexp
713 (> calculate-lisp-indent-last-sexp
(point)))
714 ;; Yes, but is there a containing sexp after that?
715 (let ((peek (parse-partial-sexp calculate-lisp-indent-last-sexp
717 (if (setq retry
(car (cdr peek
))) (setq state peek
)))))
720 ;; Innermost containing sexp found
721 (goto-char (1+ containing-sexp
))
722 (if (not calculate-lisp-indent-last-sexp
)
723 ;; indent-point immediately follows open paren.
725 (setq desired-indent
(current-column))
726 ;; Find the start of first element of containing sexp.
727 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp
0 t
)
728 (cond ((looking-at "\\s(")
729 ;; First element of containing sexp is a list.
730 ;; Indent under that list.
732 ((> (save-excursion (forward-line 1) (point))
733 calculate-lisp-indent-last-sexp
)
734 ;; This is the first line to start within the containing sexp.
735 ;; It's almost certainly a function call.
736 (if (= (point) calculate-lisp-indent-last-sexp
)
737 ;; Containing sexp has nothing before this line
738 ;; except the first element. Indent under that element.
740 ;; Skip the first element, find start of second (the first
741 ;; argument of the function call) and indent under.
742 (progn (forward-sexp 1)
743 (parse-partial-sexp (point)
744 calculate-lisp-indent-last-sexp
746 (backward-prefix-chars))
748 ;; Indent beneath first sexp on same line as
749 ;; `calculate-lisp-indent-last-sexp'. Again, it's
750 ;; almost certainly a function call.
751 (goto-char calculate-lisp-indent-last-sexp
)
753 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp
755 (backward-prefix-chars)))))
756 ;; Point is at the point to indent under unless we are inside a string.
757 ;; Call indentation hook except when overridden by lisp-indent-offset
758 ;; or if the desired indentation has already been computed.
759 (let ((normal-indent (current-column)))
761 ;; Inside a string, don't change indentation.
763 ((and (integerp lisp-indent-offset
) containing-sexp
)
764 ;; Indent by constant offset
765 (goto-char containing-sexp
)
766 (+ (current-column) lisp-indent-offset
))
768 ((and (boundp 'lisp-indent-function
)
771 (or (funcall lisp-indent-function indent-point state
)
776 (defun lisp-indent-function (indent-point state
)
777 (let ((normal-indent (current-column)))
778 (goto-char (1+ (elt state
1)))
779 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp
0 t
)
780 (if (and (elt state
2)
781 (not (looking-at "\\sw\\|\\s_")))
782 ;; car of form doesn't seem to be a a symbol
784 (if (not (> (save-excursion (forward-line 1) (point))
785 calculate-lisp-indent-last-sexp
))
786 (progn (goto-char calculate-lisp-indent-last-sexp
)
788 (parse-partial-sexp (point)
789 calculate-lisp-indent-last-sexp
0 t
)))
790 ;; Indent under the list or under the first sexp on the same
791 ;; line as calculate-lisp-indent-last-sexp. Note that first
792 ;; thing on that line has to be complete sexp since we are
793 ;; inside the innermost containing sexp.
794 (backward-prefix-chars)
796 (let ((function (buffer-substring (point)
797 (progn (forward-sexp 1) (point))))
799 (setq method
(or (get (intern-soft function
) 'lisp-indent-function
)
800 (get (intern-soft function
) 'lisp-indent-hook
)))
801 (cond ((or (eq method
'defun
)
803 (> (length function
) 3)
804 (string-match "\\`def" function
)))
805 (lisp-indent-defform state indent-point
))
807 (lisp-indent-specform method state
808 indent-point normal-indent
))
810 (funcall method state indent-point
)))))))
812 (defvar lisp-body-indent
2
813 "Number of columns to indent the second line of a `(def...)' form.")
815 (defun lisp-indent-specform (count state indent-point normal-indent
)
816 (let ((containing-form-start (elt state
1))
818 body-indent containing-form-column
)
819 ;; Move to the start of containing form, calculate indentation
820 ;; to use for non-distinguished forms (> count), and move past the
821 ;; function symbol. lisp-indent-function guarantees that there is at
822 ;; least one word or symbol character following open paren of containing
824 (goto-char containing-form-start
)
825 (setq containing-form-column
(current-column))
826 (setq body-indent
(+ lisp-body-indent containing-form-column
))
829 ;; Now find the start of the last form.
830 (parse-partial-sexp (point) indent-point
1 t
)
831 (while (and (< (point) indent-point
)
834 (setq count
(1- count
))
836 (parse-partial-sexp (point) indent-point
1 t
))
838 ;; Point is sitting on first character of last (or count) sexp.
840 ;; A distinguished form. If it is the first or second form use double
841 ;; lisp-body-indent, else normal indent. With lisp-body-indent bound
842 ;; to 2 (the default), this just happens to work the same with if as
843 ;; the older code, but it makes unwind-protect, condition-case,
844 ;; with-output-to-temp-buffer, et. al. much more tasteful. The older,
845 ;; less hacked, behavior can be obtained by replacing below with
846 ;; (list normal-indent containing-form-start).
847 (if (<= (- i count
) 1)
848 (list (+ containing-form-column
(* 2 lisp-body-indent
))
849 containing-form-start
)
850 (list normal-indent containing-form-start
))
851 ;; A non-distinguished form. Use body-indent if there are no
852 ;; distinguished forms and this is the first undistinguished form,
853 ;; or if this is the first undistinguished form and the preceding
854 ;; distinguished form has indentation at least as great as body-indent.
855 (if (or (and (= i
0) (= count
0))
856 (and (= count
0) (<= body-indent normal-indent
)))
860 (defun lisp-indent-defform (state indent-point
)
861 (goto-char (car (cdr state
)))
863 (if (> (point) (car (cdr (cdr state
))))
865 (goto-char (car (cdr state
)))
866 (+ lisp-body-indent
(current-column)))))
869 ;; (put 'progn 'lisp-indent-function 0), say, causes progn to be indented
870 ;; like defun if the first form is placed on the next line, otherwise
871 ;; it is indented like any other form (i.e. forms line up under first).
873 (put 'lambda
'lisp-indent-function
'defun
)
874 (put 'autoload
'lisp-indent-function
'defun
)
875 (put 'progn
'lisp-indent-function
0)
876 (put 'prog1
'lisp-indent-function
1)
877 (put 'prog2
'lisp-indent-function
2)
878 (put 'save-excursion
'lisp-indent-function
0)
879 (put 'save-window-excursion
'lisp-indent-function
0)
880 (put 'save-selected-window
'lisp-indent-function
0)
881 (put 'save-restriction
'lisp-indent-function
0)
882 (put 'save-match-data
'lisp-indent-function
0)
883 (put 'save-current-buffer
'lisp-indent-function
0)
884 (put 'with-current-buffer
'lisp-indent-function
1)
885 (put 'combine-after-change-calls
'lisp-indent-function
0)
886 (put 'with-output-to-string
'lisp-indent-function
0)
887 (put 'with-temp-file
'lisp-indent-function
1)
888 (put 'with-temp-buffer
'lisp-indent-function
0)
889 (put 'with-temp-message
'lisp-indent-function
1)
890 (put 'with-syntax-table
'lisp-indent-function
1)
891 (put 'let
'lisp-indent-function
1)
892 (put 'let
* 'lisp-indent-function
1)
893 (put 'while
'lisp-indent-function
1)
894 (put 'if
'lisp-indent-function
2)
895 (put 'catch
'lisp-indent-function
1)
896 (put 'condition-case
'lisp-indent-function
2)
897 (put 'unwind-protect
'lisp-indent-function
1)
898 (put 'with-output-to-temp-buffer
'lisp-indent-function
1)
899 (put 'eval-after-load
'lisp-indent-function
1)
900 (put 'dolist
'lisp-indent-function
1)
901 (put 'dotimes
'lisp-indent-function
1)
902 (put 'when
'lisp-indent-function
1)
903 (put 'unless
'lisp-indent-function
1)
905 (defun indent-sexp (&optional endpos
)
906 "Indent each line of the list starting just after point.
907 If optional arg ENDPOS is given, indent each line, stopping when
908 ENDPOS is encountered."
910 (let ((indent-stack (list nil
))
912 ;; If ENDPOS is non-nil, use nil as STARTING-POINT
913 ;; so that calculate-lisp-indent will find the beginning of
914 ;; the defun we are in.
915 ;; If ENDPOS is nil, it is safe not to scan before point
916 ;; since every line we indent is more deeply nested than point is.
917 (starting-point (if endpos nil
(point)))
919 last-depth bol outer-loop-done inner-loop-done state this-indent
)
921 ;; Get error now if we don't have a complete sexp after point.
922 (save-excursion (forward-sexp 1)))
924 (setq outer-loop-done nil
)
925 (while (if endpos
(< (point) endpos
)
926 (not outer-loop-done
))
927 (setq last-depth next-depth
929 ;; Parse this line so we can learn the state
930 ;; to indent the next line.
931 ;; This inner loop goes through only once
932 ;; unless a line ends inside a string.
933 (while (and (not inner-loop-done
)
934 (not (setq outer-loop-done
(eobp))))
935 (setq state
(parse-partial-sexp (point) (progn (end-of-line) (point))
937 (setq next-depth
(car state
))
938 ;; If the line contains a comment other than the sort
939 ;; that is indented like code,
940 ;; indent it now with indent-for-comment.
941 ;; Comments indented like code are right already.
942 ;; In any case clear the in-comment flag in the state
943 ;; because parse-partial-sexp never sees the newlines.
944 (if (car (nthcdr 4 state
))
945 (progn (indent-for-comment)
947 (setcar (nthcdr 4 state
) nil
)))
948 ;; If this line ends inside a string,
949 ;; go straight to next line, remaining within the inner loop,
950 ;; and turn off the \-flag.
951 (if (car (nthcdr 3 state
))
954 (setcar (nthcdr 5 state
) nil
))
955 (setq inner-loop-done t
)))
959 (setq indent-stack
(nconc indent-stack
960 (make-list (- next-depth
) nil
))
961 last-depth
(- last-depth next-depth
)
963 (or outer-loop-done endpos
964 (setq outer-loop-done
(<= next-depth
0)))
967 (while (> last-depth next-depth
)
968 (setq indent-stack
(cdr indent-stack
)
969 last-depth
(1- last-depth
)))
970 (while (< last-depth next-depth
)
971 (setq indent-stack
(cons nil indent-stack
)
972 last-depth
(1+ last-depth
)))
973 ;; Now go to the next line and indent it according
974 ;; to what we learned from parsing the previous one.
977 (skip-chars-forward " \t")
978 ;; But not if the line is blank, or just a comment
979 ;; (except for double-semi comments; indent them as usual).
980 (if (or (eobp) (looking-at "\\s<\\|\n"))
982 (if (and (car indent-stack
)
983 (>= (car indent-stack
) 0))
984 (setq this-indent
(car indent-stack
))
985 (let ((val (calculate-lisp-indent
986 (if (car indent-stack
) (- (car indent-stack
))
989 (setq this-indent val
)
992 (setq this-indent val
))
993 (setcar indent-stack
(- (car (cdr val
))))
994 (setq this-indent
(car val
))))))
995 (if (and this-indent
(/= (current-column) this-indent
))
996 (progn (delete-region bol
(point))
997 (indent-to this-indent
)))))
999 (setq outer-loop-done
(= (point) last-point
))
1000 (setq last-point
(point)))))))
1002 (defun lisp-indent-region (start end
)
1003 "Indent every line whose first char is between START and END inclusive."
1005 (let ((endmark (copy-marker end
)))
1007 (and (bolp) (not (eolp))
1009 (indent-sexp endmark
)
1010 (set-marker endmark nil
))))
1012 ;;;; Lisp paragraph filling commands.
1014 (defun lisp-fill-paragraph (&optional justify
)
1015 "Like \\[fill-paragraph], but handle Emacs Lisp comments.
1016 If any of the current line is a comment, fill the comment or the
1017 paragraph of it that point is in, preserving the comment's indentation
1018 and initial semicolons."
1021 ;; Non-nil if the current line contains a comment.
1024 ;; Non-nil if the current line contains code and a comment.
1025 has-code-and-comment
1027 ;; If has-comment, the appropriate fill-prefix for the comment.
1031 ;; Figure out what kind of comment we are looking at.
1036 ;; A line with nothing but a comment on it?
1037 ((looking-at "[ \t]*;[; \t]*")
1039 comment-fill-prefix
(buffer-substring (match-beginning 0)
1042 ;; A line with some code, followed by a comment? Remember that the
1043 ;; semi which starts the comment shouldn't be part of a string or
1045 ((condition-case nil
1047 (narrow-to-region (point-min)
1048 (save-excursion (end-of-line) (point)))
1049 (while (not (looking-at ";\\|$"))
1050 (skip-chars-forward "^;\n\"\\\\?")
1052 ((eq (char-after (point)) ?
\\) (forward-char 2))
1053 ((memq (char-after (point)) '(?
\" ??
)) (forward-sexp 1))))
1054 (looking-at ";+[\t ]*"))
1056 (setq has-comment t has-code-and-comment t
)
1057 (setq comment-fill-prefix
1058 (concat (make-string (/ (current-column) tab-width
) ?
\t)
1059 (make-string (%
(current-column) tab-width
) ?\
)
1060 (buffer-substring (match-beginning 0) (match-end 0)))))))
1062 (if (not has-comment
)
1063 ;; `paragraph-start' is set here (not in the buffer-local
1064 ;; variable so that `forward-paragraph' et al work as
1065 ;; expected) so that filling (doc) strings works sensibly.
1066 ;; Adding the opening paren to avoid the following sexp being
1067 ;; filled means that sexps generally aren't filled as normal
1068 ;; text, which is probably sensible. The `;' and `:' stop the
1069 ;; filled para at following comment lines and keywords
1070 ;; (typically in `defcustom').
1071 (let ((paragraph-start (concat paragraph-start
1072 "\\|\\s-*[\(;:\"]"))
1073 ;; Avoid filling the first line of docstring.
1075 (concat paragraph-separate
"\\|\\s-*\".*\\.$")))
1076 (fill-paragraph justify
))
1078 ;; Narrow to include only the comment, and then fill the region.
1083 ;; Find the first line we should include in the region to fill.
1085 (while (and (looking-at "[ \t]*;")
1086 (zerop (forward-line -
1))))
1088 ;; Find the beginning of the first line past the region to fill.
1090 (while (progn (forward-line 1)
1091 (looking-at "[ \t]*;")))
1094 ;; Lines with only semicolons on them can be paragraph boundaries.
1095 (let* ((paragraph-start (concat paragraph-start
"\\|[ \t;]*$"))
1096 (paragraph-separate (concat paragraph-start
"\\|[ \t;]*$"))
1097 (paragraph-ignore-fill-prefix nil
)
1098 (fill-prefix comment-fill-prefix
)
1099 (after-line (if has-code-and-comment
1101 (forward-line 1) (point))))
1104 (or (bolp) (newline 1))
1106 ;; If this comment starts on a line with code,
1107 ;; include that like in the filling.
1108 (beg (progn (backward-paragraph)
1109 (if (eq (point) after-line
)
1112 (fill-region-as-paragraph beg end
1116 (if (looking-at fill-prefix
)
1118 (re-search-forward comment-start-skip
)
1122 (defun indent-code-rigidly (start end arg
&optional nochange-regexp
)
1123 "Indent all lines of code, starting in the region, sideways by ARG columns.
1124 Does not affect lines starting inside comments or strings, assuming that
1125 the start of the region is not inside them.
1127 Called from a program, takes args START, END, COLUMNS and NOCHANGE-REGEXP.
1128 The last is a regexp which, if matched at the beginning of a line,
1129 means don't indent that line."
1130 (interactive "r\np")
1134 (setq end
(point-marker))
1137 (setq state
(parse-partial-sexp (point)
1139 (forward-line 1) (point))
1141 (while (< (point) end
)
1142 (or (car (nthcdr 3 state
))
1143 (and nochange-regexp
1144 (looking-at nochange-regexp
))
1145 ;; If line does not start in string, indent it
1146 (let ((indent (current-indentation)))
1147 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
1149 (indent-to (max 0 (+ indent arg
)) 0))))
1150 (setq state
(parse-partial-sexp (point)
1152 (forward-line 1) (point))
1155 (provide 'lisp-mode
)
1157 ;;; lisp-mode.el ends here