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)
135 (put 'define-ibuffer-filter
'doc-string-elt
3)
136 (put 'define-ibuffer-op
'doc-string-elt
3)
137 (put 'define-ibuffer-sorter
'doc-string-elt
3)
139 (defun lisp-font-lock-syntactic-face-function (state)
141 (if (and (eq (nth 0 state
) 1)
142 ;; This might be a docstring.
145 (goto-char (nth 8 state
))
147 (while (progn (backward-sexp 1) (setq n
(1+ n
))))
150 (let ((sym (intern-soft
152 (point) (progn (forward-sexp 1) (point))))))
153 (eq n
(or (get sym
'doc-string-elt
) 3)))))))
155 font-lock-string-face
)
156 font-lock-comment-face
))
158 ;; The LISP-SYNTAX argument is used by code in inf-lisp.el and is
159 ;; (uselessly) passed from pp.el, chistory.el, gnus-kill.el and score-mode.el
160 (defun lisp-mode-variables (&optional lisp-syntax
)
162 (set-syntax-table lisp-mode-syntax-table
))
163 (setq local-abbrev-table lisp-mode-abbrev-table
)
164 (make-local-variable 'paragraph-ignore-fill-prefix
)
165 (setq paragraph-ignore-fill-prefix t
)
166 (make-local-variable 'fill-paragraph-function
)
167 (setq fill-paragraph-function
'lisp-fill-paragraph
)
168 ;; Adaptive fill mode gets in the way of auto-fill,
169 ;; and should make no difference for explicit fill
170 ;; because lisp-fill-paragraph should do the job.
171 ;; I believe that newcomment's auto-fill code properly deals with it -stef
172 ;;(set (make-local-variable 'adaptive-fill-mode) nil)
173 (make-local-variable 'normal-auto-fill-function
)
174 (setq normal-auto-fill-function
'lisp-mode-auto-fill
)
175 (make-local-variable 'indent-line-function
)
176 (setq indent-line-function
'lisp-indent-line
)
177 (make-local-variable 'indent-region-function
)
178 (setq indent-region-function
'lisp-indent-region
)
179 (make-local-variable 'parse-sexp-ignore-comments
)
180 (setq parse-sexp-ignore-comments t
)
181 (make-local-variable 'outline-regexp
)
182 (setq outline-regexp
";;;;* \\|(")
183 (make-local-variable 'outline-level
)
184 (setq outline-level
'lisp-outline-level
)
185 (make-local-variable 'comment-start
)
186 (setq comment-start
";")
187 (make-local-variable 'comment-start-skip
)
188 ;; Look within the line for a ; following an even number of backslashes
189 ;; after either a non-backslash or the line beginning.
190 (setq comment-start-skip
"\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\);+ *")
191 (make-local-variable 'comment-add
)
192 (setq comment-add
1) ;default to `;;' in comment-region
193 (make-local-variable 'comment-column
)
194 (setq comment-column
40)
195 (make-local-variable 'comment-indent-function
)
196 (setq comment-indent-function
'lisp-comment-indent
)
197 (make-local-variable 'imenu-generic-expression
)
198 (setq imenu-generic-expression lisp-imenu-generic-expression
)
199 (make-local-variable 'multibyte-syntax-as-symbol
)
200 (setq multibyte-syntax-as-symbol t
)
201 (set (make-local-variable 'syntax-begin-function
) 'beginning-of-defun
)
202 (setq font-lock-defaults
203 '((lisp-font-lock-keywords
204 lisp-font-lock-keywords-1 lisp-font-lock-keywords-2
)
205 nil nil
(("+-*/.<>=!?$%_&~^:" .
"w")) beginning-of-defun
206 (font-lock-mark-block-function . mark-defun
)
207 (font-lock-syntactic-face-function
208 . lisp-font-lock-syntactic-face-function
))))
210 (defun lisp-outline-level ()
211 "Lisp mode `outline-level' function."
214 (looking-at outline-regexp
)
215 (- (match-end 0) (match-beginning 0))))
218 (defvar lisp-mode-shared-map
219 (let ((map (make-sparse-keymap)))
220 (define-key map
"\t" 'lisp-indent-line
)
221 (define-key map
"\e\C-q" 'indent-sexp
)
222 (define-key map
"\177" 'backward-delete-char-untabify
)
223 ;; This gets in the way when viewing a Lisp file in view-mode. As
224 ;; long as [backspace] is mapped into DEL via the
225 ;; function-key-map, this should remain disabled!!
226 ;;;(define-key map [backspace] 'backward-delete-char-untabify)
228 "Keymap for commands shared by all sorts of Lisp modes.")
230 (defvar emacs-lisp-mode-map
()
231 "Keymap for Emacs Lisp mode.
232 All commands in `lisp-mode-shared-map' are inherited by this map.")
234 (if emacs-lisp-mode-map
236 (let ((map (make-sparse-keymap "Emacs-Lisp")))
237 (setq emacs-lisp-mode-map
(make-sparse-keymap))
238 (set-keymap-parent emacs-lisp-mode-map lisp-mode-shared-map
)
239 (define-key emacs-lisp-mode-map
"\e\t" 'lisp-complete-symbol
)
240 (define-key emacs-lisp-mode-map
"\e\C-x" 'eval-defun
)
241 (define-key emacs-lisp-mode-map
[menu-bar
] (make-sparse-keymap))
242 (define-key emacs-lisp-mode-map
[menu-bar emacs-lisp
]
243 (cons "Emacs-Lisp" map
))
244 (define-key map
[edebug-defun
]
245 '("Instrument Function for Debugging" . edebug-defun
))
246 (define-key map
[byte-recompile
]
247 '("Byte-recompile Directory..." . byte-recompile-directory
))
248 (define-key map
[emacs-byte-compile-and-load
]
249 '("Byte-compile And Load" . emacs-lisp-byte-compile-and-load
))
250 (define-key map
[byte-compile
]
251 '("Byte-compile This File" . emacs-lisp-byte-compile
))
252 (define-key map
[separator-eval
] '("--"))
253 (define-key map
[eval-buffer
] '("Evaluate Buffer" . eval-current-buffer
))
254 (define-key map
[eval-region
] '("Evaluate Region" . eval-region
))
255 (define-key map
[eval-sexp
] '("Evaluate Last S-expression" . eval-last-sexp
))
256 (define-key map
[separator-format
] '("--"))
257 (define-key map
[comment-region
] '("Comment Out Region" . comment-region
))
258 (define-key map
[indent-region
] '("Indent Region" . indent-region
))
259 (define-key map
[indent-line
] '("Indent Line" . lisp-indent-line
))
260 (put 'eval-region
'menu-enable
'mark-active
)
261 (put 'comment-region
'menu-enable
'mark-active
)
262 (put 'indent-region
'menu-enable
'mark-active
)))
264 (defun emacs-lisp-byte-compile ()
265 "Byte compile the file containing the current buffer."
268 (byte-compile-file buffer-file-name
)
269 (error "The buffer must be saved in a file first")))
271 (defun emacs-lisp-byte-compile-and-load ()
272 "Byte-compile the current file (if it has changed), then load compiled code."
275 (error "The buffer must be saved in a file first"))
277 ;; Recompile if file or buffer has changed since last compilation.
278 (if (and (buffer-modified-p)
279 (y-or-n-p (format "Save buffer %s first? " (buffer-name))))
281 (let ((compiled-file-name (byte-compile-dest-file buffer-file-name
)))
282 (if (file-newer-than-file-p compiled-file-name buffer-file-name
)
283 (load-file compiled-file-name
)
284 (byte-compile-file buffer-file-name t
))))
286 (defcustom emacs-lisp-mode-hook nil
287 "Hook run when entering Emacs Lisp mode."
288 :options
'(turn-on-eldoc-mode imenu-add-menubar-index checkdoc-minor-mode
)
292 (defcustom lisp-mode-hook nil
293 "Hook run when entering Lisp mode."
294 :options
'(imenu-add-menubar-index)
298 (defcustom lisp-interaction-mode-hook nil
299 "Hook run when entering Lisp Interaction mode."
300 :options
'(turn-on-eldoc-mode)
304 (defun emacs-lisp-mode ()
305 "Major mode for editing Lisp code to run in Emacs.
307 Delete converts tabs to spaces as it moves back.
308 Blank lines separate paragraphs. Semicolons start comments.
309 \\{emacs-lisp-mode-map}
310 Entry to this mode calls the value of `emacs-lisp-mode-hook'
311 if that value is non-nil."
313 (kill-all-local-variables)
314 (use-local-map emacs-lisp-mode-map
)
315 (set-syntax-table emacs-lisp-mode-syntax-table
)
316 (setq major-mode
'emacs-lisp-mode
)
317 (setq mode-name
"Emacs-Lisp")
318 (lisp-mode-variables)
319 (setq imenu-case-fold-search nil
)
320 (run-hooks 'emacs-lisp-mode-hook
))
322 (defvar lisp-mode-map
323 (let ((map (make-sparse-keymap)))
324 (set-keymap-parent map lisp-mode-shared-map
)
325 (define-key map
"\e\C-x" 'lisp-eval-defun
)
326 (define-key map
"\C-c\C-z" 'run-lisp
)
328 "Keymap for ordinary Lisp mode.
329 All commands in `lisp-mode-shared-map' are inherited by this map.")
332 "Major mode for editing Lisp code for Lisps other than GNU Emacs Lisp.
334 Delete converts tabs to spaces as it moves back.
335 Blank lines separate paragraphs. Semicolons start comments.
337 Note that `run-lisp' may be used either to start an inferior Lisp job
338 or to switch back to an existing one.
340 Entry to this mode calls the value of `lisp-mode-hook'
341 if that value is non-nil."
343 (kill-all-local-variables)
344 (use-local-map lisp-mode-map
)
345 (setq major-mode
'lisp-mode
)
346 (setq mode-name
"Lisp")
347 (lisp-mode-variables)
348 (make-local-variable 'comment-start-skip
)
349 (setq comment-start-skip
350 "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\)\\(;+\\|#|\\) *")
351 (make-local-variable 'font-lock-keywords-case-fold-search
)
352 (setq font-lock-keywords-case-fold-search t
)
353 (setq imenu-case-fold-search t
)
354 (set-syntax-table lisp-mode-syntax-table
)
355 (run-hooks 'lisp-mode-hook
))
357 ;; This will do unless inf-lisp.el is loaded.
358 (defun lisp-eval-defun (&optional and-go
)
359 "Send the current defun to the Lisp process made by \\[run-lisp]."
361 (error "Process lisp does not exist"))
363 (defvar lisp-interaction-mode-map
364 (let ((map (make-sparse-keymap)))
365 (set-keymap-parent map lisp-mode-shared-map
)
366 (define-key map
"\e\C-x" 'eval-defun
)
367 (define-key map
"\e\t" 'lisp-complete-symbol
)
368 (define-key map
"\n" 'eval-print-last-sexp
)
370 "Keymap for Lisp Interaction mode.
371 All commands in `lisp-mode-shared-map' are inherited by this map.")
373 (defvar lisp-interaction-mode-abbrev-table lisp-mode-abbrev-table
)
374 (define-derived-mode lisp-interaction-mode emacs-lisp-mode
"Lisp Interaction"
375 "Major mode for typing and evaluating Lisp forms.
376 Like Lisp mode except that \\[eval-print-last-sexp] evals the Lisp expression
377 before point, and prints its value into the buffer, advancing point.
378 Note that printing is controlled by `eval-expression-print-length'
379 and `eval-expression-print-level'.
382 Delete converts tabs to spaces as it moves back.
383 Paragraphs are separated only by blank lines.
384 Semicolons start comments.
385 \\{lisp-interaction-mode-map}
386 Entry to this mode calls the value of `lisp-interaction-mode-hook'
387 if that value is non-nil.")
389 (defun eval-print-last-sexp ()
390 "Evaluate sexp before point; print value into current buffer.
392 Note that printing the result is controlled by the variables
393 `eval-expression-print-length' and `eval-expression-print-level',
396 (let ((standard-output (current-buffer)))
402 (defun last-sexp-setup-props (beg end value alt1 alt2
)
403 "Set up text properties for the output of `eval-last-sexp-1'.
404 BEG and END are the start and end of the output in current-buffer.
405 VALUE is the Lisp value printed, ALT1 and ALT2 are strings for the
406 alternative printed representations that can be displayed."
407 (let ((map (make-sparse-keymap)))
408 (define-key map
"\C-m" 'last-sexp-toggle-display
)
409 (define-key map
[down-mouse-2
] 'mouse-set-point
)
410 (define-key map
[mouse-2
] 'last-sexp-toggle-display
)
413 `(printed-value (,value
,alt1
,alt2
)
416 help-echo
"RET, mouse-2: toggle abbreviated display"
417 rear-nonsticky
(mouse-face keymap help-echo
421 (defun last-sexp-toggle-display ()
422 "Toggle between abbreviated and unabbreviated printed representations."
424 (let ((value (get-text-property (point) 'printed-value
)))
426 (let ((beg (or (previous-single-property-change (point) 'printed-value
) (point)))
427 (end (or (next-single-char-property-change (point) 'printed-value
) (point)))
428 (standard-output (current-buffer))
430 (delete-region beg end
)
431 (insert (nth 1 value
))
432 (last-sexp-setup-props beg
(point)
436 (goto-char (min (point-max) point
))))))
439 (defun eval-last-sexp-1 (eval-last-sexp-arg-internal)
440 "Evaluate sexp before point; print value in minibuffer.
441 With argument, print output into current buffer."
442 (let ((standard-output (if eval-last-sexp-arg-internal
(current-buffer) t
)))
444 (eval (let ((stab (syntax-table))
450 (set-syntax-table emacs-lisp-mode-syntax-table
)
451 ;; If this sexp appears to be enclosed in `...'
452 ;; then ignore the surrounding quotes.
454 (or (eq (following-char) ?
\')
455 (eq (preceding-char) ?
\')))
457 ;; If we were after `?\e' (or similar case),
458 ;; use the whole thing, not just the `e'.
459 (when (eq (preceding-char) ?
\\)
461 (when (eq (preceding-char) ??
)
465 (when (eq (preceding-char) ?
=)
468 (skip-chars-backward "0-9#=")
469 (setq labeled-p
(looking-at "\\(#[0-9]+=\\)+")))
474 ;; vladimir@cs.ualberta.ca 30-Jul-1997: skip ` in
475 ;; `variable' so that the value is returned, not the
477 (if (and ignore-quotes
478 (eq (following-char) ?
`))
480 (narrow-to-region (point-min) opoint
)
481 (setq expr
(read (current-buffer)))
482 ;; If it's an (interactive ...) form, it's more
483 ;; useful to show how an interactive call would
486 (eq (car expr
) 'interactive
)
488 (list 'call-interactively
495 (set-syntax-table stab
))))))
496 (let ((unabbreviated (let ((print-length nil
) (print-level nil
))
497 (prin1-to-string value
)))
498 (print-length eval-expression-print-length
)
499 (print-level eval-expression-print-level
)
505 (when (and (bufferp standard-output
)
506 (or (not (null print-length
))
507 (not (null print-level
)))
508 (not (string= unabbreviated
509 (buffer-substring-no-properties beg end
))))
510 (last-sexp-setup-props beg end value
512 (buffer-substring-no-properties beg end
))
516 (defun eval-last-sexp (eval-last-sexp-arg-internal)
517 "Evaluate sexp before point; print value in minibuffer.
518 Interactively, with prefix argument, print output into current buffer."
520 (if (null eval-expression-debug-on-error
)
521 (eval-last-sexp-1 eval-last-sexp-arg-internal
)
522 (let ((old-value (make-symbol "t")) new-value value
)
523 (let ((debug-on-error old-value
))
524 (setq value
(eval-last-sexp-1 eval-last-sexp-arg-internal
))
525 (setq new-value debug-on-error
))
526 (unless (eq old-value new-value
)
527 (setq debug-on-error new-value
))
530 (defun eval-defun-1 (form)
531 "Change defvar into defconst within FORM.
532 Likewise for other constructs as necessary."
533 ;; The code in edebug-defun should be consistent with this, but not
534 ;; the same, since this gets a macroexpended form.
535 (cond ((not (listp form
))
537 ((and (eq (car form
) 'defvar
)
538 (cdr-safe (cdr-safe form
)))
539 ;; Force variable to be bound.
540 (cons 'defconst
(cdr form
)))
541 ;; `defcustom' is now macroexpanded to
542 ;; `custom-declare-variable' with a quoted value arg.
543 ((and (eq (car form
) 'custom-declare-variable
)
544 (default-boundp (eval (nth 1 form
))))
545 ;; Force variable to be bound.
546 (set-default (eval (nth 1 form
)) (eval (nth 1 (nth 2 form
))))
548 ((eq (car form
) 'progn
)
549 (cons 'progn
(mapcar 'eval-defun-1
(cdr form
))))
552 (defun eval-defun-2 ()
553 "Evaluate defun that point is in or before.
554 The value is displayed in the minibuffer.
555 If the current defun is actually a call to `defvar',
556 then reset the variable using the initial value expression
557 even if the variable already has some other value.
558 \(Normally `defvar' does not change the variable's value
559 if it already has a value.\)
561 With argument, insert value in current buffer after the defun.
562 Return the result of evaluation."
564 (let ((debug-on-error eval-expression-debug-on-error
)
565 (print-length eval-expression-print-length
)
566 (print-level eval-expression-print-level
))
568 ;; Arrange for eval-region to "read" the (possibly) altered form.
569 ;; eval-region handles recording which file defines a function or
570 ;; variable. Re-written using `apply' to avoid capturing
571 ;; variables like `end'.
574 (let ((standard-output t
)
576 ;; Read the form from the buffer, and record where it ends.
581 (setq form
(read (current-buffer)))
583 ;; Alter the form if necessary, changing defvar into defconst, etc.
584 (setq form
(eval-defun-1 (macroexpand form
)))
585 (list beg end standard-output
587 ;; Skipping to the end of the specified region
588 ;; will make eval-region return.
591 ;; The result of evaluation has been put onto VALUES. So return it.
594 (defun eval-defun (edebug-it)
595 "Evaluate the top-level form containing point, or after point.
597 If the current defun is actually a call to `defvar' or `defcustom',
598 evaluating it this way resets the variable using its initial value
599 expression even if the variable already has some other value.
600 \(Normally `defvar' and `defcustom' do not alter the value if there
603 With a prefix argument, instrument the code for Edebug.
605 If acting on a `defun' for FUNCTION, and the function was
606 instrumented, `Edebug: FUNCTION' is printed in the minibuffer. If not
607 instrumented, just FUNCTION is printed.
609 If not acting on a `defun', the result of evaluation is displayed in
610 the minibuffer. This display is controlled by the variables
611 `eval-expression-print-length' and `eval-expression-print-level',
616 (eval-defun (not edebug-all-defs
)))
618 (if (null eval-expression-debug-on-error
)
620 (let ((old-value (make-symbol "t")) new-value value
)
621 (let ((debug-on-error old-value
))
622 (setq value
(eval-defun-2))
623 (setq new-value debug-on-error
))
624 (unless (eq old-value new-value
)
625 (setq debug-on-error new-value
))
629 (defun lisp-comment-indent ()
630 (if (looking-at "\\s<\\s<\\s<")
632 (if (looking-at "\\s<\\s<")
633 (let ((tem (or (calculate-lisp-indent) (current-column))))
634 (if (listp tem
) (car tem
) tem
))
635 (skip-chars-backward " \t")
636 (max (if (bolp) 0 (1+ (current-column)))
639 ;; This function just forces a more costly detection of comments (using
640 ;; parse-partial-sexp from beginning-of-defun). I.e. It avoids the problem of
641 ;; taking a `;' inside a string started on another line for a comment starter.
642 ;; Note: `newcomment' gets it right in 99% of the cases if you're using
643 ;; font-lock, anyway, so we could get rid of it. -stef
644 (defun lisp-mode-auto-fill ()
645 (if (> (current-column) (current-fill-column))
647 (nth 4 (syntax-ppss (point))))
649 (unless (and (boundp 'comment-auto-fill-only-comments
)
650 comment-auto-fill-only-comments
)
651 (let ((comment-start nil
) (comment-start-skip nil
))
654 (defvar lisp-indent-offset nil
655 "If non-nil, indent second line of expressions that many more columns.")
656 (defvar lisp-indent-function
'lisp-indent-function
)
658 (defun lisp-indent-line (&optional whole-exp
)
659 "Indent current line as Lisp code.
660 With argument, indent any additional lines of the same expression
661 rigidly along with this one."
663 (let ((indent (calculate-lisp-indent)) shift-amt end
664 (pos (- (point-max) (point)))
665 (beg (progn (beginning-of-line) (point))))
666 (skip-chars-forward " \t")
667 (if (or (null indent
) (looking-at "\\s<\\s<\\s<"))
668 ;; Don't alter indentation of a ;;; comment line
669 ;; or a line that starts in a string.
670 (goto-char (- (point-max) pos
))
671 (if (and (looking-at "\\s<") (not (looking-at "\\s<\\s<")))
672 ;; Single-semicolon comment lines should be indented
673 ;; as comment lines, not as code.
674 (progn (indent-for-comment) (forward-char -
1))
675 (if (listp indent
) (setq indent
(car indent
)))
676 (setq shift-amt
(- indent
(current-column)))
677 (if (zerop shift-amt
)
679 (delete-region beg
(point))
681 ;; If initial point was within line's indentation,
682 ;; position after the indentation. Else stay at same point in text.
683 (if (> (- (point-max) pos
) (point))
684 (goto-char (- (point-max) pos
)))
685 ;; If desired, shift remaining lines of expression the same amount.
686 (and whole-exp
(not (zerop shift-amt
))
695 (indent-code-rigidly beg end shift-amt
)))))
697 (defvar calculate-lisp-indent-last-sexp
)
699 (defun calculate-lisp-indent (&optional parse-start
)
700 "Return appropriate indentation for current line as Lisp code.
701 In usual case returns an integer: the column to indent to.
702 If the value is nil, that means don't change the indentation
703 because the line starts inside a string.
705 The value can also be a list of the form (COLUMN CONTAINING-SEXP-START).
706 This means that following lines at the same level of indentation
707 should not necessarily be indented the same as this line.
708 Then COLUMN is the column to indent to, and CONTAINING-SEXP-START
709 is the buffer position of the start of the containing expression."
712 (let ((indent-point (point))
714 ;; setting this to a number inhibits calling hook
717 calculate-lisp-indent-last-sexp containing-sexp
)
719 (goto-char parse-start
)
720 (beginning-of-defun))
721 ;; Find outermost containing sexp
722 (while (< (point) indent-point
)
723 (setq state
(parse-partial-sexp (point) indent-point
0)))
724 ;; Find innermost containing sexp
727 (> (setq paren-depth
(elt state
0)) 0))
729 (setq calculate-lisp-indent-last-sexp
(elt state
2))
730 (setq containing-sexp
(elt state
1))
731 ;; Position following last unclosed open.
732 (goto-char (1+ containing-sexp
))
733 ;; Is there a complete sexp since then?
734 (if (and calculate-lisp-indent-last-sexp
735 (> calculate-lisp-indent-last-sexp
(point)))
736 ;; Yes, but is there a containing sexp after that?
737 (let ((peek (parse-partial-sexp calculate-lisp-indent-last-sexp
739 (if (setq retry
(car (cdr peek
))) (setq state peek
)))))
742 ;; Innermost containing sexp found
743 (goto-char (1+ containing-sexp
))
744 (if (not calculate-lisp-indent-last-sexp
)
745 ;; indent-point immediately follows open paren.
747 (setq desired-indent
(current-column))
748 ;; Find the start of first element of containing sexp.
749 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp
0 t
)
750 (cond ((looking-at "\\s(")
751 ;; First element of containing sexp is a list.
752 ;; Indent under that list.
754 ((> (save-excursion (forward-line 1) (point))
755 calculate-lisp-indent-last-sexp
)
756 ;; This is the first line to start within the containing sexp.
757 ;; It's almost certainly a function call.
758 (if (= (point) calculate-lisp-indent-last-sexp
)
759 ;; Containing sexp has nothing before this line
760 ;; except the first element. Indent under that element.
762 ;; Skip the first element, find start of second (the first
763 ;; argument of the function call) and indent under.
764 (progn (forward-sexp 1)
765 (parse-partial-sexp (point)
766 calculate-lisp-indent-last-sexp
768 (backward-prefix-chars))
770 ;; Indent beneath first sexp on same line as
771 ;; `calculate-lisp-indent-last-sexp'. Again, it's
772 ;; almost certainly a function call.
773 (goto-char calculate-lisp-indent-last-sexp
)
775 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp
777 (backward-prefix-chars)))))
778 ;; Point is at the point to indent under unless we are inside a string.
779 ;; Call indentation hook except when overridden by lisp-indent-offset
780 ;; or if the desired indentation has already been computed.
781 (let ((normal-indent (current-column)))
783 ;; Inside a string, don't change indentation.
785 ((and (integerp lisp-indent-offset
) containing-sexp
)
786 ;; Indent by constant offset
787 (goto-char containing-sexp
)
788 (+ (current-column) lisp-indent-offset
))
790 ((and (boundp 'lisp-indent-function
)
793 (or (funcall lisp-indent-function indent-point state
)
798 (defun lisp-indent-function (indent-point state
)
799 "This function is the normal value of the variable `lisp-indent-function'.
800 It is used when indenting a line within a function call, to see if the
801 called function says anything special about how to indent the line.
803 INDENT-POINT is the position where the user typed TAB, or equivalent.
804 Point is located at the point to indent under (for default indentation);
805 STATE is the `parse-partial-sexp' state for that position.
807 If the current line is in a call to a Lisp function
808 which has a non-nil property `lisp-indent-function',
809 that specifies how to do the indentation. The property value can be
810 * `defun', meaning indent `defun'-style;
811 * an integer N, meaning indent the first N arguments specially
812 like ordinary function arguments and then indent any further
813 aruments like a body;
814 * a function to call just as this function was called.
815 If that function returns nil, that means it doesn't specify
818 This function also returns nil meaning don't specify the indentation."
819 (let ((normal-indent (current-column)))
820 (goto-char (1+ (elt state
1)))
821 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp
0 t
)
822 (if (and (elt state
2)
823 (not (looking-at "\\sw\\|\\s_")))
824 ;; car of form doesn't seem to be a a symbol
826 (if (not (> (save-excursion (forward-line 1) (point))
827 calculate-lisp-indent-last-sexp
))
828 (progn (goto-char calculate-lisp-indent-last-sexp
)
830 (parse-partial-sexp (point)
831 calculate-lisp-indent-last-sexp
0 t
)))
832 ;; Indent under the list or under the first sexp on the same
833 ;; line as calculate-lisp-indent-last-sexp. Note that first
834 ;; thing on that line has to be complete sexp since we are
835 ;; inside the innermost containing sexp.
836 (backward-prefix-chars)
838 (let ((function (buffer-substring (point)
839 (progn (forward-sexp 1) (point))))
841 (setq method
(or (get (intern-soft function
) 'lisp-indent-function
)
842 (get (intern-soft function
) 'lisp-indent-hook
)))
843 (cond ((or (eq method
'defun
)
845 (> (length function
) 3)
846 (string-match "\\`def" function
)))
847 (lisp-indent-defform state indent-point
))
849 (lisp-indent-specform method state
850 indent-point normal-indent
))
852 (funcall method state indent-point
)))))))
854 (defvar lisp-body-indent
2
855 "Number of columns to indent the second line of a `(def...)' form.")
857 (defun lisp-indent-specform (count state indent-point normal-indent
)
858 (let ((containing-form-start (elt state
1))
860 body-indent containing-form-column
)
861 ;; Move to the start of containing form, calculate indentation
862 ;; to use for non-distinguished forms (> count), and move past the
863 ;; function symbol. lisp-indent-function guarantees that there is at
864 ;; least one word or symbol character following open paren of containing
866 (goto-char containing-form-start
)
867 (setq containing-form-column
(current-column))
868 (setq body-indent
(+ lisp-body-indent containing-form-column
))
871 ;; Now find the start of the last form.
872 (parse-partial-sexp (point) indent-point
1 t
)
873 (while (and (< (point) indent-point
)
876 (setq count
(1- count
))
878 (parse-partial-sexp (point) indent-point
1 t
))
880 ;; Point is sitting on first character of last (or count) sexp.
882 ;; A distinguished form. If it is the first or second form use double
883 ;; lisp-body-indent, else normal indent. With lisp-body-indent bound
884 ;; to 2 (the default), this just happens to work the same with if as
885 ;; the older code, but it makes unwind-protect, condition-case,
886 ;; with-output-to-temp-buffer, et. al. much more tasteful. The older,
887 ;; less hacked, behavior can be obtained by replacing below with
888 ;; (list normal-indent containing-form-start).
889 (if (<= (- i count
) 1)
890 (list (+ containing-form-column
(* 2 lisp-body-indent
))
891 containing-form-start
)
892 (list normal-indent containing-form-start
))
893 ;; A non-distinguished form. Use body-indent if there are no
894 ;; distinguished forms and this is the first undistinguished form,
895 ;; or if this is the first undistinguished form and the preceding
896 ;; distinguished form has indentation at least as great as body-indent.
897 (if (or (and (= i
0) (= count
0))
898 (and (= count
0) (<= body-indent normal-indent
)))
902 (defun lisp-indent-defform (state indent-point
)
903 (goto-char (car (cdr state
)))
905 (if (> (point) (car (cdr (cdr state
))))
907 (goto-char (car (cdr state
)))
908 (+ lisp-body-indent
(current-column)))))
911 ;; (put 'progn 'lisp-indent-function 0), say, causes progn to be indented
912 ;; like defun if the first form is placed on the next line, otherwise
913 ;; it is indented like any other form (i.e. forms line up under first).
915 (put 'lambda
'lisp-indent-function
'defun
)
916 (put 'autoload
'lisp-indent-function
'defun
)
917 (put 'progn
'lisp-indent-function
0)
918 (put 'prog1
'lisp-indent-function
1)
919 (put 'prog2
'lisp-indent-function
2)
920 (put 'save-excursion
'lisp-indent-function
0)
921 (put 'save-window-excursion
'lisp-indent-function
0)
922 (put 'save-selected-window
'lisp-indent-function
0)
923 (put 'save-restriction
'lisp-indent-function
0)
924 (put 'save-match-data
'lisp-indent-function
0)
925 (put 'save-current-buffer
'lisp-indent-function
0)
926 (put 'with-current-buffer
'lisp-indent-function
1)
927 (put 'combine-after-change-calls
'lisp-indent-function
0)
928 (put 'with-output-to-string
'lisp-indent-function
0)
929 (put 'with-temp-file
'lisp-indent-function
1)
930 (put 'with-temp-buffer
'lisp-indent-function
0)
931 (put 'with-temp-message
'lisp-indent-function
1)
932 (put 'with-syntax-table
'lisp-indent-function
1)
933 (put 'let
'lisp-indent-function
1)
934 (put 'let
* 'lisp-indent-function
1)
935 (put 'while
'lisp-indent-function
1)
936 (put 'if
'lisp-indent-function
2)
937 (put 'read-if
'lisp-indent-function
2)
938 (put 'catch
'lisp-indent-function
1)
939 (put 'condition-case
'lisp-indent-function
2)
940 (put 'unwind-protect
'lisp-indent-function
1)
941 (put 'with-output-to-temp-buffer
'lisp-indent-function
1)
942 (put 'eval-after-load
'lisp-indent-function
1)
943 (put 'dolist
'lisp-indent-function
1)
944 (put 'dotimes
'lisp-indent-function
1)
945 (put 'when
'lisp-indent-function
1)
946 (put 'unless
'lisp-indent-function
1)
948 (defun indent-sexp (&optional endpos
)
949 "Indent each line of the list starting just after point.
950 If optional arg ENDPOS is given, indent each line, stopping when
951 ENDPOS is encountered."
953 (let ((indent-stack (list nil
))
955 ;; If ENDPOS is non-nil, use nil as STARTING-POINT
956 ;; so that calculate-lisp-indent will find the beginning of
957 ;; the defun we are in.
958 ;; If ENDPOS is nil, it is safe not to scan before point
959 ;; since every line we indent is more deeply nested than point is.
960 (starting-point (if endpos nil
(point)))
962 last-depth bol outer-loop-done inner-loop-done state this-indent
)
964 ;; Get error now if we don't have a complete sexp after point.
965 (save-excursion (forward-sexp 1)))
967 (setq outer-loop-done nil
)
968 (while (if endpos
(< (point) endpos
)
969 (not outer-loop-done
))
970 (setq last-depth next-depth
972 ;; Parse this line so we can learn the state
973 ;; to indent the next line.
974 ;; This inner loop goes through only once
975 ;; unless a line ends inside a string.
976 (while (and (not inner-loop-done
)
977 (not (setq outer-loop-done
(eobp))))
978 (setq state
(parse-partial-sexp (point) (progn (end-of-line) (point))
980 (setq next-depth
(car state
))
981 ;; If the line contains a comment other than the sort
982 ;; that is indented like code,
983 ;; indent it now with indent-for-comment.
984 ;; Comments indented like code are right already.
985 ;; In any case clear the in-comment flag in the state
986 ;; because parse-partial-sexp never sees the newlines.
987 (if (car (nthcdr 4 state
))
988 (progn (indent-for-comment)
990 (setcar (nthcdr 4 state
) nil
)))
991 ;; If this line ends inside a string,
992 ;; go straight to next line, remaining within the inner loop,
993 ;; and turn off the \-flag.
994 (if (car (nthcdr 3 state
))
997 (setcar (nthcdr 5 state
) nil
))
998 (setq inner-loop-done t
)))
1002 (setq indent-stack
(nconc indent-stack
1003 (make-list (- next-depth
) nil
))
1004 last-depth
(- last-depth next-depth
)
1006 (or outer-loop-done endpos
1007 (setq outer-loop-done
(<= next-depth
0)))
1010 (while (> last-depth next-depth
)
1011 (setq indent-stack
(cdr indent-stack
)
1012 last-depth
(1- last-depth
)))
1013 (while (< last-depth next-depth
)
1014 (setq indent-stack
(cons nil indent-stack
)
1015 last-depth
(1+ last-depth
)))
1016 ;; Now go to the next line and indent it according
1017 ;; to what we learned from parsing the previous one.
1020 (skip-chars-forward " \t")
1021 ;; But not if the line is blank, or just a comment
1022 ;; (except for double-semi comments; indent them as usual).
1023 (if (or (eobp) (looking-at "\\s<\\|\n"))
1025 (if (and (car indent-stack
)
1026 (>= (car indent-stack
) 0))
1027 (setq this-indent
(car indent-stack
))
1028 (let ((val (calculate-lisp-indent
1029 (if (car indent-stack
) (- (car indent-stack
))
1032 (setq this-indent val
)
1034 (setcar indent-stack
1035 (setq this-indent val
))
1036 (setcar indent-stack
(- (car (cdr val
))))
1037 (setq this-indent
(car val
))))))
1038 (if (and this-indent
(/= (current-column) this-indent
))
1039 (progn (delete-region bol
(point))
1040 (indent-to this-indent
)))))
1042 (setq outer-loop-done
(= (point) last-point
))
1043 (setq last-point
(point)))))))
1045 (defun lisp-indent-region (start end
)
1046 "Indent every line whose first char is between START and END inclusive."
1048 (let ((endmark (copy-marker end
)))
1050 (and (bolp) (not (eolp))
1052 (indent-sexp endmark
)
1053 (set-marker endmark nil
))))
1055 ;;;; Lisp paragraph filling commands.
1057 (defun lisp-fill-paragraph (&optional justify
)
1058 "Like \\[fill-paragraph], but handle Emacs Lisp comments.
1059 If any of the current line is a comment, fill the comment or the
1060 paragraph of it that point is in, preserving the comment's indentation
1061 and initial semicolons."
1064 ;; Non-nil if the current line contains a comment.
1067 ;; Non-nil if the current line contains code and a comment.
1068 has-code-and-comment
1070 ;; If has-comment, the appropriate fill-prefix for the comment.
1074 ;; Figure out what kind of comment we are looking at.
1079 ;; A line with nothing but a comment on it?
1080 ((looking-at "[ \t]*;[; \t]*")
1082 comment-fill-prefix
(buffer-substring (match-beginning 0)
1085 ;; A line with some code, followed by a comment? Remember that the
1086 ;; semi which starts the comment shouldn't be part of a string or
1088 ((let ((state (syntax-ppss (line-end-position))))
1090 (goto-char (nth 8 state
))
1091 (looking-at ";+[\t ]*")))
1092 (setq has-comment t has-code-and-comment t
)
1093 (setq comment-fill-prefix
1094 (concat (make-string (/ (current-column) tab-width
) ?
\t)
1095 (make-string (%
(current-column) tab-width
) ?\
)
1096 (buffer-substring (match-beginning 0) (match-end 0)))))))
1098 (if (not has-comment
)
1099 ;; `paragraph-start' is set here (not in the buffer-local
1100 ;; variable so that `forward-paragraph' et al work as
1101 ;; expected) so that filling (doc) strings works sensibly.
1102 ;; Adding the opening paren to avoid the following sexp being
1103 ;; filled means that sexps generally aren't filled as normal
1104 ;; text, which is probably sensible. The `;' and `:' stop the
1105 ;; filled para at following comment lines and keywords
1106 ;; (typically in `defcustom').
1107 (let ((paragraph-start (concat paragraph-start
1108 "\\|\\s-*[\(;:\"]"))
1109 ;; Avoid filling the first line of docstring.
1111 (concat paragraph-separate
"\\|\\s-*\".*\\.$")))
1112 (fill-paragraph justify
))
1114 ;; Narrow to include only the comment, and then fill the region.
1119 ;; Find the first line we should include in the region to fill.
1121 (while (and (zerop (forward-line -
1))
1122 (looking-at "[ \t]*;")))
1123 ;; We may have gone too far. Go forward again.
1124 (or (looking-at ".*;")
1127 ;; Find the beginning of the first line past the region to fill.
1129 (while (progn (forward-line 1)
1130 (looking-at "[ \t]*;")))
1133 ;; Lines with only semicolons on them can be paragraph boundaries.
1134 (let* ((paragraph-start (concat paragraph-start
"\\|[ \t;]*$"))
1135 (paragraph-separate (concat paragraph-start
"\\|[ \t;]*$"))
1136 (paragraph-ignore-fill-prefix nil
)
1137 (fill-prefix comment-fill-prefix
)
1138 (after-line (if has-code-and-comment
1140 (forward-line 1) (point))))
1143 (or (bolp) (newline 1))
1145 ;; If this comment starts on a line with code,
1146 ;; include that like in the filling.
1147 (beg (progn (backward-paragraph)
1148 (if (eq (point) after-line
)
1151 (fill-region-as-paragraph beg end
1155 (if (looking-at fill-prefix
)
1157 (re-search-forward comment-start-skip
)
1161 (defun indent-code-rigidly (start end arg
&optional nochange-regexp
)
1162 "Indent all lines of code, starting in the region, sideways by ARG columns.
1163 Does not affect lines starting inside comments or strings, assuming that
1164 the start of the region is not inside them.
1166 Called from a program, takes args START, END, COLUMNS and NOCHANGE-REGEXP.
1167 The last is a regexp which, if matched at the beginning of a line,
1168 means don't indent that line."
1169 (interactive "r\np")
1173 (setq end
(point-marker))
1176 (setq state
(parse-partial-sexp (point)
1178 (forward-line 1) (point))
1180 (while (< (point) end
)
1181 (or (car (nthcdr 3 state
))
1182 (and nochange-regexp
1183 (looking-at nochange-regexp
))
1184 ;; If line does not start in string, indent it
1185 (let ((indent (current-indentation)))
1186 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
1188 (indent-to (max 0 (+ indent arg
)) 0))))
1189 (setq state
(parse-partial-sexp (point)
1191 (forward-line 1) (point))
1194 (provide 'lisp-mode
)
1196 ;;; lisp-mode.el ends here