(x_set_glyph_string_background_width): Set the glyph
[emacs.git] / lisp / emacs-lisp / lisp-mode.el
blob7cae7a92a7b1a751886e43f891642cf7429ac483
1 ;;; lisp-mode.el --- Lisp mode, and its idiosyncratic commands
3 ;; Copyright (C) 1985, 1986, 1999, 2000, 2001 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
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)
13 ;; any later version.
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.
25 ;;; Commentary:
27 ;; The base major mode for editing Lisp code (used also for Emacs Lisp).
28 ;; This mode is documented in the Emacs manual.
30 ;;; Code:
32 (defvar lisp-mode-abbrev-table nil)
34 (defvar emacs-lisp-mode-syntax-table
35 (let ((table (make-syntax-table)))
36 (let ((i 0))
37 (while (< i ?0)
38 (modify-syntax-entry i "_ " table)
39 (setq i (1+ i)))
40 (setq i (1+ ?9))
41 (while (< i ?A)
42 (modify-syntax-entry i "_ " table)
43 (setq i (1+ i)))
44 (setq i (1+ ?Z))
45 (while (< i ?a)
46 (modify-syntax-entry i "_ " table)
47 (setq i (1+ i)))
48 (setq i (1+ ?z))
49 (while (< i 128)
50 (modify-syntax-entry i "_ " table)
51 (setq i (1+ i)))
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))
71 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)
79 table))
81 (define-abbrev-table 'lisp-mode-abbrev-table ())
83 (defvar lisp-imenu-generic-expression
84 (list
85 (list nil
86 (purecopy "^\\s-*(def\\(un\\*?\\|subst\\|macro\\|advice\\|\
87 ine-skeleton\\|ine-minor-mode\\)\\s-+\\(\\sw\\(\\sw\\|\\s_\\)+\\)") 2)
88 (list (purecopy "Variables")
89 (purecopy "^\\s-*(def\\(var\\|const\\|custom\\)\\s-+\
90 \\(\\sw\\(\\sw\\|\\s_\\)+\\)") 2)
91 (list (purecopy "Types")
92 (purecopy "^\\s-*(def\\(group\\|type\\|struct\\|class\\|\
93 ine-condition\\|ine-widget\\|face\\)\\s-+'?\\(\\sw\\(\\sw\\|\\s_\\)+\\)")
94 2))
96 "Imenu generic expression for Lisp mode. See `imenu-generic-expression'.")
98 (defun lisp-mode-variables (lisp-syntax)
99 (cond (lisp-syntax
100 (set-syntax-table lisp-mode-syntax-table)))
101 (setq local-abbrev-table lisp-mode-abbrev-table)
102 (make-local-variable 'paragraph-start)
103 (setq paragraph-start (concat page-delimiter "\\|$" ))
104 (make-local-variable 'paragraph-separate)
105 (setq paragraph-separate paragraph-start)
106 (make-local-variable 'paragraph-ignore-fill-prefix)
107 (setq paragraph-ignore-fill-prefix t)
108 (make-local-variable 'fill-paragraph-function)
109 (setq fill-paragraph-function 'lisp-fill-paragraph)
110 ;; Adaptive fill mode gets in the way of auto-fill,
111 ;; and should make no difference for explicit fill
112 ;; because lisp-fill-paragraph should do the job.
113 (make-local-variable 'adaptive-fill-mode)
114 (setq adaptive-fill-mode nil)
115 (make-local-variable 'normal-auto-fill-function)
116 (setq normal-auto-fill-function 'lisp-mode-auto-fill)
117 (make-local-variable 'indent-line-function)
118 (setq indent-line-function 'lisp-indent-line)
119 (make-local-variable 'indent-region-function)
120 (setq indent-region-function 'lisp-indent-region)
121 (make-local-variable 'parse-sexp-ignore-comments)
122 (setq parse-sexp-ignore-comments t)
123 (make-local-variable 'outline-regexp)
124 (setq outline-regexp ";;;;* \\|(")
125 (make-local-variable 'outline-level)
126 (setq outline-level 'lisp-outline-level)
127 (make-local-variable 'comment-start)
128 (setq comment-start ";")
129 (make-local-variable 'comment-start-skip)
130 ;; Look within the line for a ; following an even number of backslashes
131 ;; after either a non-backslash or the line beginning.
132 (setq comment-start-skip "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\);+ *")
133 (make-local-variable 'comment-add)
134 (setq comment-add 1) ;default to `;;' in comment-region
135 (make-local-variable 'comment-column)
136 (setq comment-column 40)
137 (make-local-variable 'comment-indent-function)
138 (setq comment-indent-function 'lisp-comment-indent)
139 (make-local-variable 'imenu-generic-expression)
140 (setq imenu-generic-expression lisp-imenu-generic-expression)
141 (make-local-variable 'multibyte-syntax-as-symbol)
142 (setq multibyte-syntax-as-symbol t)
143 (setq font-lock-defaults
144 '((lisp-font-lock-keywords
145 lisp-font-lock-keywords-1 lisp-font-lock-keywords-2)
146 nil nil (("+-*/.<>=!?$%_&~^:" . "w")) beginning-of-defun
147 (font-lock-mark-block-function . mark-defun))))
149 (defun lisp-outline-level ()
150 "Lisp mode `outline-level' function."
151 (if (looking-at "(")
152 1000
153 (looking-at outline-regexp)
154 (- (match-end 0) (match-beginning 0))))
157 (defvar lisp-mode-shared-map
158 (let ((map (make-sparse-keymap)))
159 (define-key map "\t" 'lisp-indent-line)
160 (define-key map "\e\C-q" 'indent-sexp)
161 (define-key map "\177" 'backward-delete-char-untabify)
162 ;; This gets in the way when viewing a Lisp file in view-mode. As
163 ;; long as [backspace] is mapped into DEL via the
164 ;; function-key-map, this should remain disabled!!
165 ;;;(define-key map [backspace] 'backward-delete-char-untabify)
166 map)
167 "Keymap for commands shared by all sorts of Lisp modes.")
169 (defvar emacs-lisp-mode-map ()
170 "Keymap for Emacs Lisp mode.
171 All commands in `lisp-mode-shared-map' are inherited by this map.")
173 (if emacs-lisp-mode-map
175 (let ((map (make-sparse-keymap "Emacs-Lisp")))
176 (setq emacs-lisp-mode-map (make-sparse-keymap))
177 (set-keymap-parent emacs-lisp-mode-map lisp-mode-shared-map)
178 (define-key emacs-lisp-mode-map "\e\t" 'lisp-complete-symbol)
179 (define-key emacs-lisp-mode-map "\e\C-x" 'eval-defun)
180 (define-key emacs-lisp-mode-map [menu-bar] (make-sparse-keymap))
181 (define-key emacs-lisp-mode-map [menu-bar emacs-lisp]
182 (cons "Emacs-Lisp" map))
183 (define-key map [edebug-defun]
184 '("Instrument Function for Debugging" . edebug-defun))
185 (define-key map [byte-recompile]
186 '("Byte-recompile Directory..." . byte-recompile-directory))
187 (define-key map [emacs-byte-compile-and-load]
188 '("Byte-compile And Load" . emacs-lisp-byte-compile-and-load))
189 (define-key map [byte-compile]
190 '("Byte-compile This File" . emacs-lisp-byte-compile))
191 (define-key map [separator-eval] '("--"))
192 (define-key map [eval-buffer] '("Evaluate Buffer" . eval-current-buffer))
193 (define-key map [eval-region] '("Evaluate Region" . eval-region))
194 (define-key map [eval-sexp] '("Evaluate Last S-expression" . eval-last-sexp))
195 (define-key map [separator-format] '("--"))
196 (define-key map [comment-region] '("Comment Out Region" . comment-region))
197 (define-key map [indent-region] '("Indent Region" . indent-region))
198 (define-key map [indent-line] '("Indent Line" . lisp-indent-line))
199 (put 'eval-region 'menu-enable 'mark-active)
200 (put 'comment-region 'menu-enable 'mark-active)
201 (put 'indent-region 'menu-enable 'mark-active)))
203 (defun emacs-lisp-byte-compile ()
204 "Byte compile the file containing the current buffer."
205 (interactive)
206 (if buffer-file-name
207 (byte-compile-file buffer-file-name)
208 (error "The buffer must be saved in a file first")))
210 (defun emacs-lisp-byte-compile-and-load ()
211 "Byte-compile the current file (if it has changed), then load compiled code."
212 (interactive)
213 (or buffer-file-name
214 (error "The buffer must be saved in a file first"))
215 (require 'bytecomp)
216 ;; Recompile if file or buffer has changed since last compilation.
217 (if (and (buffer-modified-p)
218 (y-or-n-p (format "Save buffer %s first? " (buffer-name))))
219 (save-buffer))
220 (let ((compiled-file-name (byte-compile-dest-file buffer-file-name)))
221 (if (file-newer-than-file-p compiled-file-name buffer-file-name)
222 (load-file compiled-file-name)
223 (byte-compile-file buffer-file-name t))))
225 (defcustom emacs-lisp-mode-hook nil
226 "Hook run when entering Emacs Lisp mode."
227 :options '(turn-on-eldoc-mode imenu-add-menubar-index checkdoc-minor-mode)
228 :type 'hook
229 :group 'lisp)
231 (defcustom lisp-mode-hook nil
232 "Hook run when entering Lisp mode."
233 :options '(imenu-add-menubar-index)
234 :type 'hook
235 :group 'lisp)
237 (defcustom lisp-interaction-mode-hook nil
238 "Hook run when entering Lisp Interaction mode."
239 :options '(turn-on-eldoc-mode)
240 :type 'hook
241 :group 'lisp)
243 (define-derived-mode emacs-lisp-mode nil "Emacs-Lisp"
244 "Major mode for editing Lisp code to run in Emacs.
245 Commands:
246 Delete converts tabs to spaces as it moves back.
247 Blank lines separate paragraphs. Semicolons start comments.
248 \\{emacs-lisp-mode-map}
249 Entry to this mode calls the value of `emacs-lisp-mode-hook'
250 if that value is non-nil."
251 (lisp-mode-variables nil)
252 (setq imenu-case-fold-search nil))
254 (defvar lisp-mode-map
255 (let ((map (make-sparse-keymap)))
256 (set-keymap-parent map lisp-mode-shared-map)
257 (define-key map "\e\C-x" 'lisp-eval-defun)
258 (define-key map "\C-c\C-z" 'run-lisp)
259 map)
260 "Keymap for ordinary Lisp mode.
261 All commands in `lisp-mode-shared-map' are inherited by this map.")
263 (define-derived-mode lisp-mode nil "Lisp"
264 "Major mode for editing Lisp code for Lisps other than GNU Emacs Lisp.
265 Commands:
266 Delete converts tabs to spaces as it moves back.
267 Blank lines separate paragraphs. Semicolons start comments.
268 \\{lisp-mode-map}
269 Note that `run-lisp' may be used either to start an inferior Lisp job
270 or to switch back to an existing one.
272 Entry to this mode calls the value of `lisp-mode-hook'
273 if that value is non-nil."
274 (lisp-mode-variables t)
275 (make-local-variable 'font-lock-keywords-case-fold-search)
276 (setq font-lock-keywords-case-fold-search t)
277 (setq imenu-case-fold-search t))
279 ;; This will do unless inf-lisp.el is loaded.
280 (defun lisp-eval-defun (&optional and-go)
281 "Send the current defun to the Lisp process made by \\[run-lisp]."
282 (interactive)
283 (error "Process lisp does not exist"))
285 (defvar lisp-interaction-mode-map
286 (let ((map (make-sparse-keymap)))
287 (set-keymap-parent map lisp-mode-shared-map)
288 (define-key map "\e\C-x" 'eval-defun)
289 (define-key map "\e\t" 'lisp-complete-symbol)
290 (define-key map "\n" 'eval-print-last-sexp)
291 map)
292 "Keymap for Lisp Interaction mode.
293 All commands in `lisp-mode-shared-map' are inherited by this map.")
295 (define-derived-mode lisp-interaction-mode emacs-lisp-mode "Lisp Interaction"
296 "Major mode for typing and evaluating Lisp forms.
297 Like Lisp mode except that \\[eval-print-last-sexp] evals the Lisp expression
298 before point, and prints its value into the buffer, advancing point.
299 Note that printing is controled by `eval-expression-print-length'
300 and `eval-expression-print-level'.
302 Commands:
303 Delete converts tabs to spaces as it moves back.
304 Paragraphs are separated only by blank lines.
305 Semicolons start comments.
306 \\{lisp-interaction-mode-map}
307 Entry to this mode calls the value of `lisp-interaction-mode-hook'
308 if that value is non-nil.")
310 (defun eval-print-last-sexp ()
311 "Evaluate sexp before point; print value into current buffer.
313 Note that printing the result is controlled by the variables
314 `eval-expression-print-length' and `eval-expression-print-level',
315 which see."
316 (interactive)
317 (let ((standard-output (current-buffer)))
318 (terpri)
319 (eval-last-sexp t)
320 (terpri)))
323 (defun last-sexp-setup-props (beg end value alt1 alt2)
324 "Set up text properties for the output of `eval-last-sexp-1'.
325 BEG and END are the start and end of the output in current-buffer.
326 VALUE is the Lisp value printed, ALT1 and ALT2 are strings for the
327 alternative printed representations that can be displayed."
328 (let ((map (make-sparse-keymap)))
329 (define-key map "\C-m" 'last-sexp-toggle-display)
330 (define-key map [down-mouse-2] 'mouse-set-point)
331 (define-key map [mouse-2] 'last-sexp-toggle-display)
332 (add-text-properties
333 beg end
334 `(printed-value (,value ,alt1 ,alt2)
335 mouse-face highlight
336 keymap ,map
337 help-echo "RET, mouse-2: toggle abbreviated display"
338 rear-nonsticky (mouse-face keymap help-echo
339 printed-value)))))
342 (defun last-sexp-toggle-display ()
343 "Toggle between abbreviated and unabbreviated printed representations."
344 (interactive)
345 (let ((value (get-text-property (point) 'printed-value)))
346 (when value
347 (let ((beg (previous-single-property-change (point) 'printed-value))
348 (end (next-single-char-property-change (point) 'printed-value))
349 (standard-output (current-buffer))
350 (point (point)))
351 (delete-region beg end)
352 (insert (nth 1 value))
353 (last-sexp-setup-props beg (point)
354 (nth 0 value)
355 (nth 2 value)
356 (nth 1 value))
357 (goto-char (min (point-max) point))))))
360 (defun eval-last-sexp-1 (eval-last-sexp-arg-internal)
361 "Evaluate sexp before point; print value in minibuffer.
362 With argument, print output into current buffer."
363 (let ((standard-output (if eval-last-sexp-arg-internal (current-buffer) t)))
364 (let ((value
365 (eval (let ((stab (syntax-table))
366 (opoint (point))
367 ignore-quotes
368 expr)
369 (unwind-protect
370 (save-excursion
371 (set-syntax-table emacs-lisp-mode-syntax-table)
372 ;; If this sexp appears to be enclosed in `...'
373 ;; then ignore the surrounding quotes.
374 (setq ignore-quotes
375 (or (eq (following-char) ?\')
376 (eq (preceding-char) ?\')))
377 (forward-sexp -1)
378 ;; If we were after `?\e' (or similar case),
379 ;; use the whole thing, not just the `e'.
380 (when (eq (preceding-char) ?\\)
381 (forward-char -1)
382 (when (eq (preceding-char) ??)
383 (forward-char -1)))
385 ;; Skip over `#N='s.
386 (when (eq (preceding-char) ?=)
387 (let (labeled-p)
388 (save-excursion
389 (skip-chars-backward "0-9#=")
390 (setq labeled-p (looking-at "\\(#[0-9]+=\\)+")))
391 (when labeled-p
392 (forward-sexp -1))))
394 (save-restriction
395 ;; vladimir@cs.ualberta.ca 30-Jul-1997: skip ` in
396 ;; `variable' so that the value is returned, not the
397 ;; name
398 (if (and ignore-quotes
399 (eq (following-char) ?`))
400 (forward-char))
401 (narrow-to-region (point-min) opoint)
402 (setq expr (read (current-buffer)))
403 ;; If it's an (interactive ...) form, it's more
404 ;; useful to show how an interactive call would
405 ;; use it.
406 (and (consp expr)
407 (eq (car expr) 'interactive)
408 (setq expr
409 (list 'call-interactively
410 (list 'quote
411 (list 'lambda
412 '(&rest args)
413 expr
414 'args)))))
415 expr))
416 (set-syntax-table stab))))))
417 (let ((unabbreviated (let ((print-length nil) (print-level nil))
418 (prin1-to-string value)))
419 (print-length eval-expression-print-length)
420 (print-level eval-expression-print-level)
421 (beg (point))
422 end)
423 (prin1 value)
424 (setq end (point))
425 (when (and (bufferp standard-output)
426 (or (not (null print-length))
427 (not (null print-level)))
428 (not (string= unabbreviated
429 (buffer-substring-no-properties beg end))))
430 (last-sexp-setup-props beg end value
431 unabbreviated
432 (buffer-substring-no-properties beg end))
433 )))))
436 (defun eval-last-sexp (eval-last-sexp-arg-internal)
437 "Evaluate sexp before point; print value in minibuffer.
438 Interactively, with prefix argument, print output into current buffer."
439 (interactive "P")
440 (if (null eval-expression-debug-on-error)
441 (eval-last-sexp-1 eval-last-sexp-arg-internal)
442 (let ((old-value (make-symbol "t")) new-value value)
443 (let ((debug-on-error old-value))
444 (setq value (eval-last-sexp-1 eval-last-sexp-arg-internal))
445 (setq new-value debug-on-error))
446 (unless (eq old-value new-value)
447 (setq debug-on-error new-value))
448 value)))
450 (defun eval-defun-1 (form)
451 "Change defvar into defconst within FORM.
452 Likewise for other constructs as necessary."
453 ;; The code in edebug-defun should be consistent with this, but not
454 ;; the same, since this gets a macroexpended form.
455 (cond ((and (eq (car form) 'defvar)
456 (cdr-safe (cdr-safe form)))
457 ;; Force variable to be bound.
458 (cons 'defconst (cdr form)))
459 ;; `defcustom' is now macroexpanded to
460 ;; `custom-declare-variable' with a quoted value arg.
461 ((and (eq (car form) 'custom-declare-variable)
462 (default-boundp (eval (nth 1 form))))
463 ;; Force variable to be bound.
464 (set-default (eval (nth 1 form)) (eval (nth 1 (nth 2 form))))
465 form)
466 ((eq (car form) 'progn)
467 (cons 'progn (mapcar 'eval-defun-1 (cdr form))))
468 (t form)))
470 (defun eval-defun-2 ()
471 "Evaluate defun that point is in or before.
472 The value is displayed in the minibuffer.
473 If the current defun is actually a call to `defvar',
474 then reset the variable using the initial value expression
475 even if the variable already has some other value.
476 \(Normally `defvar' does not change the variable's value
477 if it already has a value.\)
479 With argument, insert value in current buffer after the defun.
480 Return the result of evaluation."
481 (interactive "P")
482 (let ((debug-on-error eval-expression-debug-on-error)
483 (print-length eval-expression-print-length)
484 (print-level eval-expression-print-level))
485 (save-excursion
486 ;; Arrange for eval-region to "read" the (possibly) altered form.
487 ;; eval-region handles recording which file defines a function or
488 ;; variable. Re-written using `apply' to avoid capturing
489 ;; variables like `end'.
490 (apply
491 #'eval-region
492 (let ((standard-output t)
493 beg end form)
494 ;; Read the form from the buffer, and record where it ends.
495 (save-excursion
496 (end-of-defun)
497 (beginning-of-defun)
498 (setq beg (point))
499 (setq form (read (current-buffer)))
500 (setq end (point)))
501 ;; Alter the form if necessary, changing defvar into defconst, etc.
502 (setq form (eval-defun-1 (macroexpand form)))
503 (list beg end standard-output
504 `(lambda (ignore)
505 ;; Skipping to the end of the specified region
506 ;; will make eval-region return.
507 (goto-char ,end)
508 ',form))))))
509 ;; The result of evaluation has been put onto VALUES. So return it.
510 (car values))
512 (defun eval-defun (edebug-it)
513 "Evaluate the top-level form containing point, or after point.
515 If the current defun is actually a call to `defvar', then reset the
516 variable using its initial value expression even if the variable
517 already has some other value. (Normally `defvar' does not change the
518 variable's value if it already has a value.)
520 With a prefix argument, instrument the code for Edebug.
522 If acting on a `defun' for FUNCTION, and the function was
523 instrumented, `Edebug: FUNCTION' is printed in the minibuffer. If not
524 instrumented, just FUNCTION is printed.
526 If not acting on a `defun', the result of evaluation is displayed in
527 the minibuffer. This display is controlled by the variables
528 `eval-expression-print-length' and `eval-expression-print-level',
529 which see."
530 (interactive "P")
531 (cond (edebug-it
532 (require 'edebug)
533 (eval-defun (not edebug-all-defs)))
535 (if (null eval-expression-debug-on-error)
536 (eval-defun-2)
537 (let ((old-value (make-symbol "t")) new-value value)
538 (let ((debug-on-error old-value))
539 (setq value (eval-defun-2))
540 (setq new-value debug-on-error))
541 (unless (eq old-value new-value)
542 (setq debug-on-error new-value))
543 value)))))
546 (defun lisp-comment-indent ()
547 (if (looking-at "\\s<\\s<\\s<")
548 (current-column)
549 (if (looking-at "\\s<\\s<")
550 (let ((tem (or (calculate-lisp-indent) (current-column))))
551 (if (listp tem) (car tem) tem))
552 (skip-chars-backward " \t")
553 (max (if (bolp) 0 (1+ (current-column)))
554 comment-column))))
556 (defun lisp-mode-auto-fill ()
557 (if (> (current-column) (current-fill-column))
558 (if (save-excursion
559 (nth 4 (parse-partial-sexp (save-excursion
560 (beginning-of-defun)
561 (point))
562 (point))))
563 (do-auto-fill)
564 (let ((comment-start nil) (comment-start-skip nil))
565 (do-auto-fill)))))
567 (defvar lisp-indent-offset nil
568 "If non-nil, indent second line of expressions that many more columns.")
569 (defvar lisp-indent-function 'lisp-indent-function)
571 (defun lisp-indent-line (&optional whole-exp)
572 "Indent current line as Lisp code.
573 With argument, indent any additional lines of the same expression
574 rigidly along with this one."
575 (interactive "P")
576 (let ((indent (calculate-lisp-indent)) shift-amt beg end
577 (pos (- (point-max) (point))))
578 (beginning-of-line)
579 (setq beg (point))
580 (skip-chars-forward " \t")
581 (if (or (null indent) (looking-at "\\s<\\s<\\s<"))
582 ;; Don't alter indentation of a ;;; comment line
583 ;; or a line that starts in a string.
584 (goto-char (- (point-max) pos))
585 (if (and (looking-at "\\s<") (not (looking-at "\\s<\\s<")))
586 ;; Single-semicolon comment lines should be indented
587 ;; as comment lines, not as code.
588 (progn (indent-for-comment) (forward-char -1))
589 (if (listp indent) (setq indent (car indent)))
590 (setq shift-amt (- indent (current-column)))
591 (if (zerop shift-amt)
593 (delete-region beg (point))
594 (indent-to indent)))
595 ;; If initial point was within line's indentation,
596 ;; position after the indentation. Else stay at same point in text.
597 (if (> (- (point-max) pos) (point))
598 (goto-char (- (point-max) pos)))
599 ;; If desired, shift remaining lines of expression the same amount.
600 (and whole-exp (not (zerop shift-amt))
601 (save-excursion
602 (goto-char beg)
603 (forward-sexp 1)
604 (setq end (point))
605 (goto-char beg)
606 (forward-line 1)
607 (setq beg (point))
608 (> end beg))
609 (indent-code-rigidly beg end shift-amt)))))
611 (defvar calculate-lisp-indent-last-sexp)
613 (defun calculate-lisp-indent (&optional parse-start)
614 "Return appropriate indentation for current line as Lisp code.
615 In usual case returns an integer: the column to indent to.
616 If the value is nil, that means don't change the indentation
617 because the line starts inside a string.
619 The value can also be a list of the form (COLUMN CONTAINING-SEXP-START).
620 This means that following lines at the same level of indentation
621 should not necessarily be indented the same as this line.
622 Then COLUMN is the column to indent to, and CONTAINING-SEXP-START
623 is the buffer position of the start of the containing expression."
624 (save-excursion
625 (beginning-of-line)
626 (let ((indent-point (point))
627 state paren-depth
628 ;; setting this to a number inhibits calling hook
629 (desired-indent nil)
630 (retry t)
631 calculate-lisp-indent-last-sexp containing-sexp)
632 (if parse-start
633 (goto-char parse-start)
634 (beginning-of-defun))
635 ;; Find outermost containing sexp
636 (while (< (point) indent-point)
637 (setq state (parse-partial-sexp (point) indent-point 0)))
638 ;; Find innermost containing sexp
639 (while (and retry
640 state
641 (> (setq paren-depth (elt state 0)) 0))
642 (setq retry nil)
643 (setq calculate-lisp-indent-last-sexp (elt state 2))
644 (setq containing-sexp (elt state 1))
645 ;; Position following last unclosed open.
646 (goto-char (1+ containing-sexp))
647 ;; Is there a complete sexp since then?
648 (if (and calculate-lisp-indent-last-sexp
649 (> calculate-lisp-indent-last-sexp (point)))
650 ;; Yes, but is there a containing sexp after that?
651 (let ((peek (parse-partial-sexp calculate-lisp-indent-last-sexp
652 indent-point 0)))
653 (if (setq retry (car (cdr peek))) (setq state peek)))))
654 (if retry
656 ;; Innermost containing sexp found
657 (goto-char (1+ containing-sexp))
658 (if (not calculate-lisp-indent-last-sexp)
659 ;; indent-point immediately follows open paren.
660 ;; Don't call hook.
661 (setq desired-indent (current-column))
662 ;; Find the start of first element of containing sexp.
663 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
664 (cond ((looking-at "\\s(")
665 ;; First element of containing sexp is a list.
666 ;; Indent under that list.
668 ((> (save-excursion (forward-line 1) (point))
669 calculate-lisp-indent-last-sexp)
670 ;; This is the first line to start within the containing sexp.
671 ;; It's almost certainly a function call.
672 (if (= (point) calculate-lisp-indent-last-sexp)
673 ;; Containing sexp has nothing before this line
674 ;; except the first element. Indent under that element.
676 ;; Skip the first element, find start of second (the first
677 ;; argument of the function call) and indent under.
678 (progn (forward-sexp 1)
679 (parse-partial-sexp (point)
680 calculate-lisp-indent-last-sexp
681 0 t)))
682 (backward-prefix-chars))
684 ;; Indent beneath first sexp on same line as
685 ;; `calculate-lisp-indent-last-sexp'. Again, it's
686 ;; almost certainly a function call.
687 (goto-char calculate-lisp-indent-last-sexp)
688 (beginning-of-line)
689 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp
690 0 t)
691 (backward-prefix-chars)))))
692 ;; Point is at the point to indent under unless we are inside a string.
693 ;; Call indentation hook except when overridden by lisp-indent-offset
694 ;; or if the desired indentation has already been computed.
695 (let ((normal-indent (current-column)))
696 (cond ((elt state 3)
697 ;; Inside a string, don't change indentation.
698 nil)
699 ((and (integerp lisp-indent-offset) containing-sexp)
700 ;; Indent by constant offset
701 (goto-char containing-sexp)
702 (+ (current-column) lisp-indent-offset))
703 (desired-indent)
704 ((and (boundp 'lisp-indent-function)
705 lisp-indent-function
706 (not retry))
707 (or (funcall lisp-indent-function indent-point state)
708 normal-indent))
710 normal-indent))))))
712 (defun lisp-indent-function (indent-point state)
713 (let ((normal-indent (current-column)))
714 (goto-char (1+ (elt state 1)))
715 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
716 (if (and (elt state 2)
717 (not (looking-at "\\sw\\|\\s_")))
718 ;; car of form doesn't seem to be a a symbol
719 (progn
720 (if (not (> (save-excursion (forward-line 1) (point))
721 calculate-lisp-indent-last-sexp))
722 (progn (goto-char calculate-lisp-indent-last-sexp)
723 (beginning-of-line)
724 (parse-partial-sexp (point)
725 calculate-lisp-indent-last-sexp 0 t)))
726 ;; Indent under the list or under the first sexp on the same
727 ;; line as calculate-lisp-indent-last-sexp. Note that first
728 ;; thing on that line has to be complete sexp since we are
729 ;; inside the innermost containing sexp.
730 (backward-prefix-chars)
731 (current-column))
732 (let ((function (buffer-substring (point)
733 (progn (forward-sexp 1) (point))))
734 method)
735 (setq method (or (get (intern-soft function) 'lisp-indent-function)
736 (get (intern-soft function) 'lisp-indent-hook)))
737 (cond ((or (eq method 'defun)
738 (and (null method)
739 (> (length function) 3)
740 (string-match "\\`def" function)))
741 (lisp-indent-defform state indent-point))
742 ((integerp method)
743 (lisp-indent-specform method state
744 indent-point normal-indent))
745 (method
746 (funcall method state indent-point)))))))
748 (defvar lisp-body-indent 2
749 "Number of columns to indent the second line of a `(def...)' form.")
751 (defun lisp-indent-specform (count state indent-point normal-indent)
752 (let ((containing-form-start (elt state 1))
753 (i count)
754 body-indent containing-form-column)
755 ;; Move to the start of containing form, calculate indentation
756 ;; to use for non-distinguished forms (> count), and move past the
757 ;; function symbol. lisp-indent-function guarantees that there is at
758 ;; least one word or symbol character following open paren of containing
759 ;; form.
760 (goto-char containing-form-start)
761 (setq containing-form-column (current-column))
762 (setq body-indent (+ lisp-body-indent containing-form-column))
763 (forward-char 1)
764 (forward-sexp 1)
765 ;; Now find the start of the last form.
766 (parse-partial-sexp (point) indent-point 1 t)
767 (while (and (< (point) indent-point)
768 (condition-case ()
769 (progn
770 (setq count (1- count))
771 (forward-sexp 1)
772 (parse-partial-sexp (point) indent-point 1 t))
773 (error nil))))
774 ;; Point is sitting on first character of last (or count) sexp.
775 (if (> count 0)
776 ;; A distinguished form. If it is the first or second form use double
777 ;; lisp-body-indent, else normal indent. With lisp-body-indent bound
778 ;; to 2 (the default), this just happens to work the same with if as
779 ;; the older code, but it makes unwind-protect, condition-case,
780 ;; with-output-to-temp-buffer, et. al. much more tasteful. The older,
781 ;; less hacked, behavior can be obtained by replacing below with
782 ;; (list normal-indent containing-form-start).
783 (if (<= (- i count) 1)
784 (list (+ containing-form-column (* 2 lisp-body-indent))
785 containing-form-start)
786 (list normal-indent containing-form-start))
787 ;; A non-distinguished form. Use body-indent if there are no
788 ;; distinguished forms and this is the first undistinguished form,
789 ;; or if this is the first undistinguished form and the preceding
790 ;; distinguished form has indentation at least as great as body-indent.
791 (if (or (and (= i 0) (= count 0))
792 (and (= count 0) (<= body-indent normal-indent)))
793 body-indent
794 normal-indent))))
796 (defun lisp-indent-defform (state indent-point)
797 (goto-char (car (cdr state)))
798 (forward-line 1)
799 (if (> (point) (car (cdr (cdr state))))
800 (progn
801 (goto-char (car (cdr state)))
802 (+ lisp-body-indent (current-column)))))
805 ;; (put 'progn 'lisp-indent-function 0), say, causes progn to be indented
806 ;; like defun if the first form is placed on the next line, otherwise
807 ;; it is indented like any other form (i.e. forms line up under first).
809 (put 'lambda 'lisp-indent-function 'defun)
810 (put 'autoload 'lisp-indent-function 'defun)
811 (put 'progn 'lisp-indent-function 0)
812 (put 'prog1 'lisp-indent-function 1)
813 (put 'prog2 'lisp-indent-function 2)
814 (put 'save-excursion 'lisp-indent-function 0)
815 (put 'save-window-excursion 'lisp-indent-function 0)
816 (put 'save-selected-window 'lisp-indent-function 0)
817 (put 'save-restriction 'lisp-indent-function 0)
818 (put 'save-match-data 'lisp-indent-function 0)
819 (put 'save-current-buffer 'lisp-indent-function 0)
820 (put 'with-current-buffer 'lisp-indent-function 1)
821 (put 'combine-after-change-calls 'lisp-indent-function 0)
822 (put 'with-output-to-string 'lisp-indent-function 0)
823 (put 'with-temp-file 'lisp-indent-function 1)
824 (put 'with-temp-buffer 'lisp-indent-function 0)
825 (put 'with-temp-message 'lisp-indent-function 1)
826 (put 'with-syntax-table 'lisp-indent-function 1)
827 (put 'let 'lisp-indent-function 1)
828 (put 'let* 'lisp-indent-function 1)
829 (put 'while 'lisp-indent-function 1)
830 (put 'if 'lisp-indent-function 2)
831 (put 'catch 'lisp-indent-function 1)
832 (put 'condition-case 'lisp-indent-function 2)
833 (put 'unwind-protect 'lisp-indent-function 1)
834 (put 'with-output-to-temp-buffer 'lisp-indent-function 1)
835 (put 'eval-after-load 'lisp-indent-function 1)
836 (put 'dolist 'lisp-indent-function 1)
837 (put 'dotimes 'lisp-indent-function 1)
838 (put 'when 'lisp-indent-function 1)
839 (put 'unless 'lisp-indent-function 1)
841 (defun indent-sexp (&optional endpos)
842 "Indent each line of the list starting just after point.
843 If optional arg ENDPOS is given, indent each line, stopping when
844 ENDPOS is encountered."
845 (interactive)
846 (let ((indent-stack (list nil))
847 (next-depth 0)
848 ;; If ENDPOS is non-nil, use nil as STARTING-POINT
849 ;; so that calculate-lisp-indent will find the beginning of
850 ;; the defun we are in.
851 ;; If ENDPOS is nil, it is safe not to scan before point
852 ;; since every line we indent is more deeply nested than point is.
853 (starting-point (if endpos nil (point)))
854 (last-point (point))
855 last-depth bol outer-loop-done inner-loop-done state this-indent)
856 (or endpos
857 ;; Get error now if we don't have a complete sexp after point.
858 (save-excursion (forward-sexp 1)))
859 (save-excursion
860 (setq outer-loop-done nil)
861 (while (if endpos (< (point) endpos)
862 (not outer-loop-done))
863 (setq last-depth next-depth
864 inner-loop-done nil)
865 ;; Parse this line so we can learn the state
866 ;; to indent the next line.
867 ;; This inner loop goes through only once
868 ;; unless a line ends inside a string.
869 (while (and (not inner-loop-done)
870 (not (setq outer-loop-done (eobp))))
871 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
872 nil nil state))
873 (setq next-depth (car state))
874 ;; If the line contains a comment other than the sort
875 ;; that is indented like code,
876 ;; indent it now with indent-for-comment.
877 ;; Comments indented like code are right already.
878 ;; In any case clear the in-comment flag in the state
879 ;; because parse-partial-sexp never sees the newlines.
880 (if (car (nthcdr 4 state))
881 (progn (indent-for-comment)
882 (end-of-line)
883 (setcar (nthcdr 4 state) nil)))
884 ;; If this line ends inside a string,
885 ;; go straight to next line, remaining within the inner loop,
886 ;; and turn off the \-flag.
887 (if (car (nthcdr 3 state))
888 (progn
889 (forward-line 1)
890 (setcar (nthcdr 5 state) nil))
891 (setq inner-loop-done t)))
892 (and endpos
893 (<= next-depth 0)
894 (progn
895 (setq indent-stack (nconc indent-stack
896 (make-list (- next-depth) nil))
897 last-depth (- last-depth next-depth)
898 next-depth 0)))
899 (or outer-loop-done endpos
900 (setq outer-loop-done (<= next-depth 0)))
901 (if outer-loop-done
902 (forward-line 1)
903 (while (> last-depth next-depth)
904 (setq indent-stack (cdr indent-stack)
905 last-depth (1- last-depth)))
906 (while (< last-depth next-depth)
907 (setq indent-stack (cons nil indent-stack)
908 last-depth (1+ last-depth)))
909 ;; Now go to the next line and indent it according
910 ;; to what we learned from parsing the previous one.
911 (forward-line 1)
912 (setq bol (point))
913 (skip-chars-forward " \t")
914 ;; But not if the line is blank, or just a comment
915 ;; (except for double-semi comments; indent them as usual).
916 (if (or (eobp) (looking-at "\\s<\\|\n"))
918 (if (and (car indent-stack)
919 (>= (car indent-stack) 0))
920 (setq this-indent (car indent-stack))
921 (let ((val (calculate-lisp-indent
922 (if (car indent-stack) (- (car indent-stack))
923 starting-point))))
924 (if (null val)
925 (setq this-indent val)
926 (if (integerp val)
927 (setcar indent-stack
928 (setq this-indent val))
929 (setcar indent-stack (- (car (cdr val))))
930 (setq this-indent (car val))))))
931 (if (and this-indent (/= (current-column) this-indent))
932 (progn (delete-region bol (point))
933 (indent-to this-indent)))))
934 (or outer-loop-done
935 (setq outer-loop-done (= (point) last-point))
936 (setq last-point (point)))))))
938 (defun lisp-indent-region (start end)
939 "Indent every line whose first char is between START and END inclusive."
940 (save-excursion
941 (let ((endmark (copy-marker end)))
942 (goto-char start)
943 (and (bolp) (not (eolp))
944 (lisp-indent-line))
945 (indent-sexp endmark)
946 (set-marker endmark nil))))
948 ;;;; Lisp paragraph filling commands.
950 (defun lisp-fill-paragraph (&optional justify)
951 "Like \\[fill-paragraph], but handle Emacs Lisp comments.
952 If any of the current line is a comment, fill the comment or the
953 paragraph of it that point is in, preserving the comment's indentation
954 and initial semicolons."
955 (interactive "P")
956 (let (
957 ;; Non-nil if the current line contains a comment.
958 has-comment
960 ;; Non-nil if the current line contains code and a comment.
961 has-code-and-comment
963 ;; If has-comment, the appropriate fill-prefix for the comment.
964 comment-fill-prefix
967 ;; Figure out what kind of comment we are looking at.
968 (save-excursion
969 (beginning-of-line)
970 (cond
972 ;; A line with nothing but a comment on it?
973 ((looking-at "[ \t]*;[; \t]*")
974 (setq has-comment t
975 comment-fill-prefix (buffer-substring (match-beginning 0)
976 (match-end 0))))
978 ;; A line with some code, followed by a comment? Remember that the
979 ;; semi which starts the comment shouldn't be part of a string or
980 ;; character.
981 ((condition-case nil
982 (save-restriction
983 (narrow-to-region (point-min)
984 (save-excursion (end-of-line) (point)))
985 (while (not (looking-at ";\\|$"))
986 (skip-chars-forward "^;\n\"\\\\?")
987 (cond
988 ((eq (char-after (point)) ?\\) (forward-char 2))
989 ((memq (char-after (point)) '(?\" ??)) (forward-sexp 1))))
990 (looking-at ";+[\t ]*"))
991 (error nil))
992 (setq has-comment t has-code-and-comment t)
993 (setq comment-fill-prefix
994 (concat (make-string (/ (current-column) 8) ?\t)
995 (make-string (% (current-column) 8) ?\ )
996 (buffer-substring (match-beginning 0) (match-end 0)))))))
998 (if (not has-comment)
999 ;; `paragraph-start' is set here (not in the buffer-local
1000 ;; variable so that `forward-paragraph' et al work as
1001 ;; expected) so that filling (doc) strings works sensibly.
1002 ;; Adding the opening paren to avoid the following sexp being
1003 ;; filled means that sexps generally aren't filled as normal
1004 ;; text, which is probably sensible. The `;' and `:' stop the
1005 ;; filled para at following comment lines and keywords
1006 ;; (typically in `defcustom').
1007 (let ((paragraph-start (concat paragraph-start
1008 "\\|\\s-*[\(;:\"]")))
1009 (fill-paragraph justify))
1011 ;; Narrow to include only the comment, and then fill the region.
1012 (save-excursion
1013 (save-restriction
1014 (beginning-of-line)
1015 (narrow-to-region
1016 ;; Find the first line we should include in the region to fill.
1017 (save-excursion
1018 (while (and (zerop (forward-line -1))
1019 (looking-at "^[ \t]*;")))
1020 ;; We may have gone too far. Go forward again.
1021 (or (looking-at ".*;")
1022 (forward-line 1))
1023 (point))
1024 ;; Find the beginning of the first line past the region to fill.
1025 (save-excursion
1026 (while (progn (forward-line 1)
1027 (looking-at "^[ \t]*;")))
1028 (point)))
1030 ;; Lines with only semicolons on them can be paragraph boundaries.
1031 (let* ((paragraph-start (concat paragraph-start "\\|[ \t;]*$"))
1032 (paragraph-separate (concat paragraph-start "\\|[ \t;]*$"))
1033 (paragraph-ignore-fill-prefix nil)
1034 (fill-prefix comment-fill-prefix)
1035 (after-line (if has-code-and-comment
1036 (save-excursion
1037 (forward-line 1) (point))))
1038 (end (progn
1039 (forward-paragraph)
1040 (or (bolp) (newline 1))
1041 (point)))
1042 ;; If this comment starts on a line with code,
1043 ;; include that like in the filling.
1044 (beg (progn (backward-paragraph)
1045 (if (eq (point) after-line)
1046 (forward-line -1))
1047 (point))))
1048 (fill-region-as-paragraph beg end
1049 justify nil
1050 (save-excursion
1051 (goto-char beg)
1052 (if (looking-at fill-prefix)
1054 (re-search-forward comment-start-skip)
1055 (point))))))))
1058 (defun indent-code-rigidly (start end arg &optional nochange-regexp)
1059 "Indent all lines of code, starting in the region, sideways by ARG columns.
1060 Does not affect lines starting inside comments or strings, assuming that
1061 the start of the region is not inside them.
1063 Called from a program, takes args START, END, COLUMNS and NOCHANGE-REGEXP.
1064 The last is a regexp which, if matched at the beginning of a line,
1065 means don't indent that line."
1066 (interactive "r\np")
1067 (let (state)
1068 (save-excursion
1069 (goto-char end)
1070 (setq end (point-marker))
1071 (goto-char start)
1072 (or (bolp)
1073 (setq state (parse-partial-sexp (point)
1074 (progn
1075 (forward-line 1) (point))
1076 nil nil state)))
1077 (while (< (point) end)
1078 (or (car (nthcdr 3 state))
1079 (and nochange-regexp
1080 (looking-at nochange-regexp))
1081 ;; If line does not start in string, indent it
1082 (let ((indent (current-indentation)))
1083 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
1084 (or (eolp)
1085 (indent-to (max 0 (+ indent arg)) 0))))
1086 (setq state (parse-partial-sexp (point)
1087 (progn
1088 (forward-line 1) (point))
1089 nil nil state))))))
1091 (provide 'lisp-mode)
1093 ;;; lisp-mode.el ends here