]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/lisp-mode.el
(eval-defun-1): Remove unnecessary quotes.
[gnu-emacs] / lisp / emacs-lisp / lisp-mode.el
1 ;;; lisp-mode.el --- Lisp mode, and its idiosyncratic commands
2
3 ;; Copyright (C) 1985, 1986, 1999, 2000, 2001, 2003, 2004, 2005
4 ;; Free Software Foundation, Inc.
5
6 ;; Maintainer: FSF
7 ;; Keywords: lisp, languages
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;; The base major mode for editing Lisp code (used also for Emacs Lisp).
29 ;; This mode is documented in the Emacs manual.
30
31 ;;; Code:
32
33 (defvar lisp-mode-abbrev-table nil)
34
35 (defvar emacs-lisp-mode-syntax-table
36 (let ((table (make-syntax-table)))
37 (let ((i 0))
38 (while (< i ?0)
39 (modify-syntax-entry i "_ " table)
40 (setq i (1+ i)))
41 (setq i (1+ ?9))
42 (while (< i ?A)
43 (modify-syntax-entry i "_ " table)
44 (setq i (1+ i)))
45 (setq i (1+ ?Z))
46 (while (< i ?a)
47 (modify-syntax-entry i "_ " table)
48 (setq i (1+ i)))
49 (setq i (1+ ?z))
50 (while (< i 128)
51 (modify-syntax-entry i "_ " table)
52 (setq i (1+ i)))
53 (modify-syntax-entry ? " " table)
54 (modify-syntax-entry ?\t " " table)
55 (modify-syntax-entry ?\f " " table)
56 (modify-syntax-entry ?\n "> " table)
57 ;; Give CR the same syntax as newline, for selective-display.
58 (modify-syntax-entry ?\^m "> " table)
59 (modify-syntax-entry ?\; "< " table)
60 (modify-syntax-entry ?` "' " table)
61 (modify-syntax-entry ?' "' " table)
62 (modify-syntax-entry ?, "' " table)
63 (modify-syntax-entry ?@ "' " table)
64 ;; Used to be singlequote; changed for flonums.
65 (modify-syntax-entry ?. "_ " table)
66 (modify-syntax-entry ?# "' " table)
67 (modify-syntax-entry ?\" "\" " table)
68 (modify-syntax-entry ?\\ "\\ " table)
69 (modify-syntax-entry ?\( "() " table)
70 (modify-syntax-entry ?\) ")( " table)
71 (modify-syntax-entry ?\[ "(] " table)
72 (modify-syntax-entry ?\] ")[ " table))
73 table))
74
75 (defvar lisp-mode-syntax-table
76 (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table)))
77 (modify-syntax-entry ?\[ "_ " table)
78 (modify-syntax-entry ?\] "_ " table)
79 (modify-syntax-entry ?# "' 14bn" table)
80 (modify-syntax-entry ?| "\" 23b" table)
81 table))
82
83 (define-abbrev-table 'lisp-mode-abbrev-table ())
84
85 (defvar lisp-imenu-generic-expression
86 (list
87 (list nil
88 (purecopy (concat "^\\s-*("
89 (eval-when-compile
90 (regexp-opt
91 '("defun" "defun*" "defsubst" "defmacro"
92 "defadvice" "define-skeleton"
93 "define-minor-mode" "define-derived-mode"
94 "define-generic-mode"
95 "define-compiler-macro" "define-modify-macro"
96 "defsetf" "define-setf-expander"
97 "define-method-combination"
98 "defgeneric" "defmethod") t))
99 "\\s-+\\(\\sw\\(\\sw\\|\\s_\\)+\\)"))
100 2)
101 (list (purecopy "Variables")
102 (purecopy (concat "^\\s-*("
103 (eval-when-compile
104 (regexp-opt
105 '("defvar" "defconst" "defconstant" "defcustom"
106 "defparameter" "define-symbol-macro") t))
107 "\\s-+\\(\\sw\\(\\sw\\|\\s_\\)+\\)"))
108 2)
109 (list (purecopy "Types")
110 (purecopy (concat "^\\s-*("
111 (eval-when-compile
112 (regexp-opt
113 '("defgroup" "deftheme" "deftype" "defstruct"
114 "defclass" "define-condition" "define-widget"
115 "defface" "defpackage") t))
116 "\\s-+'?\\(\\sw\\(\\sw\\|\\s_\\)+\\)"))
117 2))
118
119 "Imenu generic expression for Lisp mode. See `imenu-generic-expression'.")
120
121 ;; This was originally in autoload.el and is still used there.
122 (put 'autoload 'doc-string-elt 3)
123 (put 'defun 'doc-string-elt 3)
124 (put 'defun* 'doc-string-elt 3)
125 (put 'defvar 'doc-string-elt 3)
126 (put 'defcustom 'doc-string-elt 3)
127 (put 'deftheme 'doc-string-elt 2)
128 (put 'defconst 'doc-string-elt 3)
129 (put 'defmacro 'doc-string-elt 3)
130 (put 'defmacro* 'doc-string-elt 3)
131 (put 'defsubst 'doc-string-elt 3)
132 (put 'defstruct 'doc-string-elt 2)
133 (put 'define-skeleton 'doc-string-elt 2)
134 (put 'define-derived-mode 'doc-string-elt 4)
135 (put 'define-compilation-mode 'doc-string-elt 3)
136 (put 'easy-mmode-define-minor-mode 'doc-string-elt 2)
137 (put 'define-minor-mode 'doc-string-elt 2)
138 (put 'define-generic-mode 'doc-string-elt 7)
139 ;; define-global-mode has no explicit docstring.
140 (put 'easy-mmode-define-global-mode 'doc-string-elt 0)
141 (put 'define-ibuffer-filter 'doc-string-elt 2)
142 (put 'define-ibuffer-op 'doc-string-elt 3)
143 (put 'define-ibuffer-sorter 'doc-string-elt 2)
144
145 (defun lisp-font-lock-syntactic-face-function (state)
146 (if (nth 3 state)
147 (if (and (eq (nth 0 state) 1)
148 ;; This might be a docstring.
149 (save-excursion
150 (let ((n 0))
151 (goto-char (nth 8 state))
152 (condition-case nil
153 (while (and (not (bobp))
154 (progn (backward-sexp 1) (setq n (1+ n)))))
155 (scan-error nil))
156 (when (> n 0)
157 (let ((sym (intern-soft
158 (buffer-substring
159 (point) (progn (forward-sexp 1) (point))))))
160 (eq n (or (get sym 'doc-string-elt) 3)))))))
161 font-lock-doc-face
162 font-lock-string-face)
163 font-lock-comment-face))
164
165 ;; The LISP-SYNTAX argument is used by code in inf-lisp.el and is
166 ;; (uselessly) passed from pp.el, chistory.el, gnus-kill.el and score-mode.el
167 (defun lisp-mode-variables (&optional lisp-syntax)
168 (when lisp-syntax
169 (set-syntax-table lisp-mode-syntax-table))
170 (setq local-abbrev-table lisp-mode-abbrev-table)
171 (make-local-variable 'paragraph-ignore-fill-prefix)
172 (setq paragraph-ignore-fill-prefix t)
173 (make-local-variable 'fill-paragraph-function)
174 (setq fill-paragraph-function 'lisp-fill-paragraph)
175 ;; Adaptive fill mode gets in the way of auto-fill,
176 ;; and should make no difference for explicit fill
177 ;; because lisp-fill-paragraph should do the job.
178 ;; I believe that newcomment's auto-fill code properly deals with it -stef
179 ;;(set (make-local-variable 'adaptive-fill-mode) nil)
180 (make-local-variable 'indent-line-function)
181 (setq indent-line-function 'lisp-indent-line)
182 (make-local-variable 'indent-region-function)
183 (setq indent-region-function 'lisp-indent-region)
184 (make-local-variable 'parse-sexp-ignore-comments)
185 (setq parse-sexp-ignore-comments t)
186 (make-local-variable 'outline-regexp)
187 (setq outline-regexp ";;;\\(;* [^ \t\n]\\|###autoload\\)\\|(")
188 (make-local-variable 'outline-level)
189 (setq outline-level 'lisp-outline-level)
190 (make-local-variable 'comment-start)
191 (setq comment-start ";")
192 (make-local-variable 'comment-start-skip)
193 ;; Look within the line for a ; following an even number of backslashes
194 ;; after either a non-backslash or the line beginning.
195 (setq comment-start-skip "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\);+ *")
196 (make-local-variable 'font-lock-comment-start-skip)
197 ;; Font lock mode uses this only when it KNOWS a comment is starting.
198 (setq font-lock-comment-start-skip ";+ *")
199 (make-local-variable 'comment-add)
200 (setq comment-add 1) ;default to `;;' in comment-region
201 (make-local-variable 'comment-column)
202 (setq comment-column 40)
203 ;; Don't get confused by `;' in doc strings when paragraph-filling.
204 (set (make-local-variable 'comment-use-global-state) t)
205 (make-local-variable 'comment-indent-function)
206 (setq comment-indent-function 'lisp-comment-indent)
207 (make-local-variable 'imenu-generic-expression)
208 (setq imenu-generic-expression lisp-imenu-generic-expression)
209 (make-local-variable 'multibyte-syntax-as-symbol)
210 (setq multibyte-syntax-as-symbol t)
211 (set (make-local-variable 'syntax-begin-function) 'beginning-of-defun)
212 (setq font-lock-defaults
213 '((lisp-font-lock-keywords
214 lisp-font-lock-keywords-1 lisp-font-lock-keywords-2)
215 nil nil (("+-*/.<>=!?$%_&~^:@" . "w")) nil
216 (font-lock-mark-block-function . mark-defun)
217 (font-lock-syntactic-face-function
218 . lisp-font-lock-syntactic-face-function))))
219
220 (defun lisp-outline-level ()
221 "Lisp mode `outline-level' function."
222 (let ((len (- (match-end 0) (match-beginning 0))))
223 (if (looking-at "(\\|;;;###autoload")
224 1000
225 len)))
226
227 (defvar lisp-mode-shared-map
228 (let ((map (make-sparse-keymap)))
229 (define-key map "\t" 'lisp-indent-line)
230 (define-key map "\e\C-q" 'indent-sexp)
231 (define-key map "\177" 'backward-delete-char-untabify)
232 ;; This gets in the way when viewing a Lisp file in view-mode. As
233 ;; long as [backspace] is mapped into DEL via the
234 ;; function-key-map, this should remain disabled!!
235 ;;;(define-key map [backspace] 'backward-delete-char-untabify)
236 map)
237 "Keymap for commands shared by all sorts of Lisp modes.")
238
239 (defvar emacs-lisp-mode-map ()
240 "Keymap for Emacs Lisp mode.
241 All commands in `lisp-mode-shared-map' are inherited by this map.")
242
243 (if emacs-lisp-mode-map
244 ()
245 (let ((map (make-sparse-keymap "Emacs-Lisp")))
246 (setq emacs-lisp-mode-map (make-sparse-keymap))
247 (set-keymap-parent emacs-lisp-mode-map lisp-mode-shared-map)
248 (define-key emacs-lisp-mode-map "\e\t" 'lisp-complete-symbol)
249 (define-key emacs-lisp-mode-map "\e\C-x" 'eval-defun)
250 (define-key emacs-lisp-mode-map "\e\C-q" 'indent-pp-sexp)
251 (define-key emacs-lisp-mode-map [menu-bar] (make-sparse-keymap))
252 (define-key emacs-lisp-mode-map [menu-bar emacs-lisp]
253 (cons "Emacs-Lisp" map))
254 (define-key map [edebug-defun]
255 '("Instrument Function for Debugging" . edebug-defun))
256 (define-key map [byte-recompile]
257 '("Byte-recompile Directory..." . byte-recompile-directory))
258 (define-key map [emacs-byte-compile-and-load]
259 '("Byte-compile And Load" . emacs-lisp-byte-compile-and-load))
260 (define-key map [byte-compile]
261 '("Byte-compile This File" . emacs-lisp-byte-compile))
262 (define-key map [separator-eval] '("--"))
263 (define-key map [eval-buffer] '("Evaluate Buffer" . eval-current-buffer))
264 (define-key map [eval-region] '("Evaluate Region" . eval-region))
265 (define-key map [eval-sexp] '("Evaluate Last S-expression" . eval-last-sexp))
266 (define-key map [separator-format] '("--"))
267 (define-key map [comment-region] '("Comment Out Region" . comment-region))
268 (define-key map [indent-region] '("Indent Region" . indent-region))
269 (define-key map [indent-line] '("Indent Line" . lisp-indent-line))
270 (put 'eval-region 'menu-enable 'mark-active)
271 (put 'comment-region 'menu-enable 'mark-active)
272 (put 'indent-region 'menu-enable 'mark-active)))
273
274 (defun emacs-lisp-byte-compile ()
275 "Byte compile the file containing the current buffer."
276 (interactive)
277 (if buffer-file-name
278 (byte-compile-file buffer-file-name)
279 (error "The buffer must be saved in a file first")))
280
281 (defun emacs-lisp-byte-compile-and-load ()
282 "Byte-compile the current file (if it has changed), then load compiled code."
283 (interactive)
284 (or buffer-file-name
285 (error "The buffer must be saved in a file first"))
286 (require 'bytecomp)
287 ;; Recompile if file or buffer has changed since last compilation.
288 (if (and (buffer-modified-p)
289 (y-or-n-p (format "Save buffer %s first? " (buffer-name))))
290 (save-buffer))
291 (let ((compiled-file-name (byte-compile-dest-file buffer-file-name)))
292 (if (file-newer-than-file-p compiled-file-name buffer-file-name)
293 (load-file compiled-file-name)
294 (byte-compile-file buffer-file-name t))))
295
296 (defcustom emacs-lisp-mode-hook nil
297 "Hook run when entering Emacs Lisp mode."
298 :options '(turn-on-eldoc-mode imenu-add-menubar-index checkdoc-minor-mode)
299 :type 'hook
300 :group 'lisp)
301
302 (defcustom lisp-mode-hook nil
303 "Hook run when entering Lisp mode."
304 :options '(imenu-add-menubar-index)
305 :type 'hook
306 :group 'lisp)
307
308 (defcustom lisp-interaction-mode-hook nil
309 "Hook run when entering Lisp Interaction mode."
310 :options '(turn-on-eldoc-mode)
311 :type 'hook
312 :group 'lisp)
313
314 (defun emacs-lisp-mode ()
315 "Major mode for editing Lisp code to run in Emacs.
316 Commands:
317 Delete converts tabs to spaces as it moves back.
318 Blank lines separate paragraphs. Semicolons start comments.
319 \\{emacs-lisp-mode-map}
320 Entry to this mode calls the value of `emacs-lisp-mode-hook'
321 if that value is non-nil."
322 (interactive)
323 (kill-all-local-variables)
324 (use-local-map emacs-lisp-mode-map)
325 (set-syntax-table emacs-lisp-mode-syntax-table)
326 (setq major-mode 'emacs-lisp-mode)
327 (setq mode-name "Emacs-Lisp")
328 (lisp-mode-variables)
329 (setq imenu-case-fold-search nil)
330 (run-mode-hooks 'emacs-lisp-mode-hook))
331 (put 'emacs-lisp-mode 'custom-mode-group 'lisp)
332
333 (defvar lisp-mode-map
334 (let ((map (make-sparse-keymap)))
335 (set-keymap-parent map lisp-mode-shared-map)
336 (define-key map "\e\C-x" 'lisp-eval-defun)
337 (define-key map "\C-c\C-z" 'run-lisp)
338 map)
339 "Keymap for ordinary Lisp mode.
340 All commands in `lisp-mode-shared-map' are inherited by this map.")
341
342 (defun lisp-mode ()
343 "Major mode for editing Lisp code for Lisps other than GNU Emacs Lisp.
344 Commands:
345 Delete converts tabs to spaces as it moves back.
346 Blank lines separate paragraphs. Semicolons start comments.
347 \\{lisp-mode-map}
348 Note that `run-lisp' may be used either to start an inferior Lisp job
349 or to switch back to an existing one.
350
351 Entry to this mode calls the value of `lisp-mode-hook'
352 if that value is non-nil."
353 (interactive)
354 (kill-all-local-variables)
355 (use-local-map lisp-mode-map)
356 (setq major-mode 'lisp-mode)
357 (setq mode-name "Lisp")
358 (lisp-mode-variables)
359 (make-local-variable 'comment-start-skip)
360 (setq comment-start-skip
361 "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\)\\(;+\\|#|\\) *")
362 (make-local-variable 'font-lock-keywords-case-fold-search)
363 (setq font-lock-keywords-case-fold-search t)
364 (setq imenu-case-fold-search t)
365 (set-syntax-table lisp-mode-syntax-table)
366 (run-mode-hooks 'lisp-mode-hook))
367 (put 'lisp-mode 'find-tag-default-function 'lisp-find-tag-default)
368
369 (defun lisp-find-tag-default ()
370 (let ((default (find-tag-default)))
371 (when (stringp default)
372 (if (string-match ":+" default)
373 (substring default (match-end 0))
374 default))))
375
376 ;; Used in old LispM code.
377 (defalias 'common-lisp-mode 'lisp-mode)
378
379 ;; This will do unless inf-lisp.el is loaded.
380 (defun lisp-eval-defun (&optional and-go)
381 "Send the current defun to the Lisp process made by \\[run-lisp]."
382 (interactive)
383 (error "Process lisp does not exist"))
384
385 (defvar lisp-interaction-mode-map
386 (let ((map (make-sparse-keymap)))
387 (set-keymap-parent map lisp-mode-shared-map)
388 (define-key map "\e\C-x" 'eval-defun)
389 (define-key map "\e\C-q" 'indent-pp-sexp)
390 (define-key map "\e\t" 'lisp-complete-symbol)
391 (define-key map "\n" 'eval-print-last-sexp)
392 map)
393 "Keymap for Lisp Interaction mode.
394 All commands in `lisp-mode-shared-map' are inherited by this map.")
395
396 (defvar lisp-interaction-mode-abbrev-table lisp-mode-abbrev-table)
397 (define-derived-mode lisp-interaction-mode emacs-lisp-mode "Lisp Interaction"
398 "Major mode for typing and evaluating Lisp forms.
399 Like Lisp mode except that \\[eval-print-last-sexp] evals the Lisp expression
400 before point, and prints its value into the buffer, advancing point.
401 Note that printing is controlled by `eval-expression-print-length'
402 and `eval-expression-print-level'.
403
404 Commands:
405 Delete converts tabs to spaces as it moves back.
406 Paragraphs are separated only by blank lines.
407 Semicolons start comments.
408 \\{lisp-interaction-mode-map}
409 Entry to this mode calls the value of `lisp-interaction-mode-hook'
410 if that value is non-nil.")
411
412 (defun eval-print-last-sexp ()
413 "Evaluate sexp before point; print value into current buffer.
414
415 Note that printing the result is controlled by the variables
416 `eval-expression-print-length' and `eval-expression-print-level',
417 which see."
418 (interactive)
419 (let ((standard-output (current-buffer)))
420 (terpri)
421 (eval-last-sexp t)
422 (terpri)))
423
424
425 (defun last-sexp-setup-props (beg end value alt1 alt2)
426 "Set up text properties for the output of `eval-last-sexp-1'.
427 BEG and END are the start and end of the output in current-buffer.
428 VALUE is the Lisp value printed, ALT1 and ALT2 are strings for the
429 alternative printed representations that can be displayed."
430 (let ((map (make-sparse-keymap)))
431 (define-key map "\C-m" 'last-sexp-toggle-display)
432 (define-key map [down-mouse-2] 'mouse-set-point)
433 (define-key map [mouse-2] 'last-sexp-toggle-display)
434 (add-text-properties
435 beg end
436 `(printed-value (,value ,alt1 ,alt2)
437 mouse-face highlight
438 keymap ,map
439 help-echo "RET, mouse-2: toggle abbreviated display"
440 rear-nonsticky (mouse-face keymap help-echo
441 printed-value)))))
442
443
444 (defun last-sexp-toggle-display (&optional arg)
445 "Toggle between abbreviated and unabbreviated printed representations."
446 (interactive "P")
447 (save-restriction
448 (widen)
449 (let ((value (get-text-property (point) 'printed-value)))
450 (when value
451 (let ((beg (or (previous-single-property-change (min (point-max) (1+ (point)))
452 'printed-value)
453 (point)))
454 (end (or (next-single-char-property-change (point) 'printed-value) (point)))
455 (standard-output (current-buffer))
456 (point (point)))
457 (delete-region beg end)
458 (insert (nth 1 value))
459 (last-sexp-setup-props beg (point)
460 (nth 0 value)
461 (nth 2 value)
462 (nth 1 value))
463 (goto-char (min (point-max) point)))))))
464
465 (defun prin1-char (char)
466 "Return a string representing CHAR as a character rather than as an integer.
467 If CHAR is not a character, return nil."
468 (and (integerp char)
469 (eventp char)
470 (let ((c (event-basic-type char))
471 (mods (event-modifiers char))
472 string)
473 ;; Prevent ?A from turning into ?\S-a.
474 (if (and (memq 'shift mods)
475 (zerop (logand char ?\S-\^@))
476 (not (let ((case-fold-search nil))
477 (char-equal c (upcase c)))))
478 (setq c (upcase c) mods nil))
479 ;; What string are we considering using?
480 (condition-case nil
481 (setq string
482 (concat
483 "?"
484 (mapconcat
485 (lambda (modif)
486 (cond ((eq modif 'super) "\\s-")
487 (t (string ?\\ (upcase (aref (symbol-name modif) 0)) ?-))))
488 mods "")
489 (cond
490 ((memq c '(?\; ?\( ?\) ?\{ ?\} ?\[ ?\] ?\" ?\' ?\\)) (string ?\\ c))
491 ((eq c 127) "\\C-?")
492 (t
493 (string c)))))
494 (error nil))
495 ;; Verify the string reads a CHAR, not to some other character.
496 ;; If it doesn't, return nil instead.
497 (and string
498 (= (car (read-from-string string)) char)
499 string))))
500
501
502 (defun eval-last-sexp-1 (eval-last-sexp-arg-internal)
503 "Evaluate sexp before point; print value in minibuffer.
504 With argument, print output into current buffer."
505 (let ((standard-output (if eval-last-sexp-arg-internal (current-buffer) t)))
506 (let ((value
507 (eval (let ((stab (syntax-table))
508 (opoint (point))
509 ignore-quotes
510 expr)
511 (save-excursion
512 (with-syntax-table emacs-lisp-mode-syntax-table
513 ;; If this sexp appears to be enclosed in `...'
514 ;; then ignore the surrounding quotes.
515 (setq ignore-quotes
516 (or (eq (following-char) ?\')
517 (eq (preceding-char) ?\')))
518 (forward-sexp -1)
519 ;; If we were after `?\e' (or similar case),
520 ;; use the whole thing, not just the `e'.
521 (when (eq (preceding-char) ?\\)
522 (forward-char -1)
523 (when (eq (preceding-char) ??)
524 (forward-char -1)))
525
526 ;; Skip over `#N='s.
527 (when (eq (preceding-char) ?=)
528 (let (labeled-p)
529 (save-excursion
530 (skip-chars-backward "0-9#=")
531 (setq labeled-p (looking-at "\\(#[0-9]+=\\)+")))
532 (when labeled-p
533 (forward-sexp -1))))
534
535 (save-restriction
536 ;; vladimir@cs.ualberta.ca 30-Jul-1997: skip ` in
537 ;; `variable' so that the value is returned, not the
538 ;; name
539 (if (and ignore-quotes
540 (eq (following-char) ?`))
541 (forward-char))
542 (narrow-to-region (point-min) opoint)
543 (setq expr (read (current-buffer)))
544 ;; If it's an (interactive ...) form, it's more
545 ;; useful to show how an interactive call would
546 ;; use it.
547 (and (consp expr)
548 (eq (car expr) 'interactive)
549 (setq expr
550 (list 'call-interactively
551 (list 'quote
552 (list 'lambda
553 '(&rest args)
554 expr
555 'args)))))
556 expr)))))))
557 (eval-last-sexp-print-value value))))
558
559 (defun eval-last-sexp-print-value (value)
560 (let ((unabbreviated (let ((print-length nil) (print-level nil))
561 (prin1-to-string value)))
562 (print-length eval-expression-print-length)
563 (print-level eval-expression-print-level)
564 (beg (point))
565 end)
566 (prog1
567 (prin1 value)
568 (let ((str (eval-expression-print-format value)))
569 (if str (princ str)))
570 (setq end (point))
571 (when (and (bufferp standard-output)
572 (or (not (null print-length))
573 (not (null print-level)))
574 (not (string= unabbreviated
575 (buffer-substring-no-properties beg end))))
576 (last-sexp-setup-props beg end value
577 unabbreviated
578 (buffer-substring-no-properties beg end))
579 ))))
580
581
582 (defvar eval-last-sexp-fake-value (make-symbol "t"))
583
584 (defun eval-last-sexp (eval-last-sexp-arg-internal)
585 "Evaluate sexp before point; print value in minibuffer.
586 Interactively, with prefix argument, print output into current buffer."
587 (interactive "P")
588 (if (null eval-expression-debug-on-error)
589 (eval-last-sexp-1 eval-last-sexp-arg-internal)
590 (let ((old-value eval-last-sexp-fake-value) new-value value)
591 (let ((debug-on-error old-value))
592 (setq value (eval-last-sexp-1 eval-last-sexp-arg-internal))
593 (setq new-value debug-on-error))
594 (unless (eq old-value new-value)
595 (setq debug-on-error new-value))
596 value)))
597
598 (defun eval-defun-1 (form)
599 "Treat some expressions specially.
600 Reset the `defvar' and `defcustom' variables to the initial value.
601 Reinitialize the face according to the `defface' specification."
602 ;; The code in edebug-defun should be consistent with this, but not
603 ;; the same, since this gets a macroexpended form.
604 (cond ((not (listp form))
605 form)
606 ((and (eq (car form) 'defvar)
607 (cdr-safe (cdr-safe form))
608 (boundp (cadr form)))
609 ;; Force variable to be re-set.
610 `(progn (defvar ,(nth 1 form) nil ,@(nthcdr 3 form))
611 (setq-default ,(nth 1 form) ,(nth 2 form))))
612 ;; `defcustom' is now macroexpanded to
613 ;; `custom-declare-variable' with a quoted value arg.
614 ((and (eq (car form) 'custom-declare-variable)
615 (default-boundp (eval (nth 1 form))))
616 ;; Force variable to be bound.
617 (set-default (eval (nth 1 form)) (eval (nth 1 (nth 2 form))))
618 form)
619 ;; `defface' is macroexpanded to `custom-declare-face'.
620 ((eq (car form) 'custom-declare-face)
621 ;; Reset the face.
622 (setq face-new-frame-defaults
623 (assq-delete-all (eval (nth 1 form)) face-new-frame-defaults))
624 (put (eval (nth 1 form)) 'face-defface-spec nil)
625 ;; Setting `customized-face' to the new spec after calling
626 ;; the form, but preserving the old saved spec in `saved-face',
627 ;; imitates the situation when the new face spec is set
628 ;; temporarily for the current session in the customize
629 ;; buffer, thus allowing `face-user-default-spec' to use the
630 ;; new customized spec instead of the saved spec.
631 ;; Resetting `saved-face' temporarily to nil is needed to let
632 ;; `defface' change the spec, regardless of a saved spec.
633 (prog1 `(prog1 ,form
634 (put ,(nth 1 form) 'saved-face
635 ',(get (eval (nth 1 form)) 'saved-face))
636 (put ,(nth 1 form) 'customized-face
637 ,(nth 2 form)))
638 (put (eval (nth 1 form)) 'saved-face nil)))
639 ((eq (car form) 'progn)
640 (cons 'progn (mapcar 'eval-defun-1 (cdr form))))
641 (t form)))
642
643 (defun eval-defun-2 ()
644 "Evaluate defun that point is in or before.
645 The value is displayed in the minibuffer.
646 If the current defun is actually a call to `defvar',
647 then reset the variable using the initial value expression
648 even if the variable already has some other value.
649 \(Normally `defvar' does not change the variable's value
650 if it already has a value.\)
651
652 With argument, insert value in current buffer after the defun.
653 Return the result of evaluation."
654 (interactive "P")
655 (let ((debug-on-error eval-expression-debug-on-error)
656 (print-length eval-expression-print-length)
657 (print-level eval-expression-print-level))
658 (save-excursion
659 ;; Arrange for eval-region to "read" the (possibly) altered form.
660 ;; eval-region handles recording which file defines a function or
661 ;; variable. Re-written using `apply' to avoid capturing
662 ;; variables like `end'.
663 (apply
664 #'eval-region
665 (let ((standard-output t)
666 beg end form)
667 ;; Read the form from the buffer, and record where it ends.
668 (save-excursion
669 (end-of-defun)
670 (beginning-of-defun)
671 (setq beg (point))
672 (setq form (read (current-buffer)))
673 (setq end (point)))
674 ;; Alter the form if necessary.
675 (setq form (eval-defun-1 (macroexpand form)))
676 (list beg end standard-output
677 `(lambda (ignore)
678 ;; Skipping to the end of the specified region
679 ;; will make eval-region return.
680 (goto-char ,end)
681 ',form))))))
682 ;; The result of evaluation has been put onto VALUES. So return it.
683 (car values))
684
685 (defun eval-defun (edebug-it)
686 "Evaluate the top-level form containing point, or after point.
687
688 If the current defun is actually a call to `defvar' or `defcustom',
689 evaluating it this way resets the variable using its initial value
690 expression even if the variable already has some other value.
691 \(Normally `defvar' and `defcustom' do not alter the value if there
692 already is one.)
693
694 With a prefix argument, instrument the code for Edebug.
695
696 If acting on a `defun' for FUNCTION, and the function was
697 instrumented, `Edebug: FUNCTION' is printed in the minibuffer. If not
698 instrumented, just FUNCTION is printed.
699
700 If not acting on a `defun', the result of evaluation is displayed in
701 the minibuffer. This display is controlled by the variables
702 `eval-expression-print-length' and `eval-expression-print-level',
703 which see."
704 (interactive "P")
705 (cond (edebug-it
706 (require 'edebug)
707 (eval-defun (not edebug-all-defs)))
708 (t
709 (if (null eval-expression-debug-on-error)
710 (eval-defun-2)
711 (let ((old-value (make-symbol "t")) new-value value)
712 (let ((debug-on-error old-value))
713 (setq value (eval-defun-2))
714 (setq new-value debug-on-error))
715 (unless (eq old-value new-value)
716 (setq debug-on-error new-value))
717 value)))))
718 \f
719 ;; Used for comment-indent-function in Lisp modes.
720 (defun lisp-comment-indent ()
721 (if (looking-at "\\s<\\s<\\s<")
722 (current-column)
723 (if (looking-at "\\s<\\s<")
724 (let ((tem (or (calculate-lisp-indent) (current-column))))
725 (if (listp tem) (car tem) tem))
726 (skip-chars-backward " \t")
727 (max (if (bolp) 0 (1+ (current-column)))
728 comment-column))))
729
730 ;; This function just forces a more costly detection of comments (using
731 ;; parse-partial-sexp from beginning-of-defun). I.e. It avoids the problem of
732 ;; taking a `;' inside a string started on another line for a comment starter.
733 ;; Note: `newcomment' gets it right now since we set comment-use-global-state
734 ;; so we could get rid of it. -stef
735 (defun lisp-mode-auto-fill ()
736 (if (> (current-column) (current-fill-column))
737 (if (save-excursion
738 (nth 4 (syntax-ppss (point))))
739 (do-auto-fill)
740 (unless (and (boundp 'comment-auto-fill-only-comments)
741 comment-auto-fill-only-comments)
742 (let ((comment-start nil) (comment-start-skip nil))
743 (do-auto-fill))))))
744
745 (defvar lisp-indent-offset nil
746 "If non-nil, indent second line of expressions that many more columns.")
747 (defvar lisp-indent-function 'lisp-indent-function)
748
749 (defun lisp-indent-line (&optional whole-exp)
750 "Indent current line as Lisp code.
751 With argument, indent any additional lines of the same expression
752 rigidly along with this one."
753 (interactive "P")
754 (let ((indent (calculate-lisp-indent)) shift-amt end
755 (pos (- (point-max) (point)))
756 (beg (progn (beginning-of-line) (point))))
757 (skip-chars-forward " \t")
758 (if (or (null indent) (looking-at "\\s<\\s<\\s<"))
759 ;; Don't alter indentation of a ;;; comment line
760 ;; or a line that starts in a string.
761 (goto-char (- (point-max) pos))
762 (if (and (looking-at "\\s<") (not (looking-at "\\s<\\s<")))
763 ;; Single-semicolon comment lines should be indented
764 ;; as comment lines, not as code.
765 (progn (indent-for-comment) (forward-char -1))
766 (if (listp indent) (setq indent (car indent)))
767 (setq shift-amt (- indent (current-column)))
768 (if (zerop shift-amt)
769 nil
770 (delete-region beg (point))
771 (indent-to indent)))
772 ;; If initial point was within line's indentation,
773 ;; position after the indentation. Else stay at same point in text.
774 (if (> (- (point-max) pos) (point))
775 (goto-char (- (point-max) pos)))
776 ;; If desired, shift remaining lines of expression the same amount.
777 (and whole-exp (not (zerop shift-amt))
778 (save-excursion
779 (goto-char beg)
780 (forward-sexp 1)
781 (setq end (point))
782 (goto-char beg)
783 (forward-line 1)
784 (setq beg (point))
785 (> end beg))
786 (indent-code-rigidly beg end shift-amt)))))
787
788 (defvar calculate-lisp-indent-last-sexp)
789
790 (defun calculate-lisp-indent (&optional parse-start)
791 "Return appropriate indentation for current line as Lisp code.
792 In usual case returns an integer: the column to indent to.
793 If the value is nil, that means don't change the indentation
794 because the line starts inside a string.
795
796 The value can also be a list of the form (COLUMN CONTAINING-SEXP-START).
797 This means that following lines at the same level of indentation
798 should not necessarily be indented the same as this line.
799 Then COLUMN is the column to indent to, and CONTAINING-SEXP-START
800 is the buffer position of the start of the containing expression."
801 (save-excursion
802 (beginning-of-line)
803 (let ((indent-point (point))
804 state paren-depth
805 ;; setting this to a number inhibits calling hook
806 (desired-indent nil)
807 (retry t)
808 calculate-lisp-indent-last-sexp containing-sexp)
809 (if parse-start
810 (goto-char parse-start)
811 (beginning-of-defun))
812 ;; Find outermost containing sexp
813 (while (< (point) indent-point)
814 (setq state (parse-partial-sexp (point) indent-point 0)))
815 ;; Find innermost containing sexp
816 (while (and retry
817 state
818 (> (setq paren-depth (elt state 0)) 0))
819 (setq retry nil)
820 (setq calculate-lisp-indent-last-sexp (elt state 2))
821 (setq containing-sexp (elt state 1))
822 ;; Position following last unclosed open.
823 (goto-char (1+ containing-sexp))
824 ;; Is there a complete sexp since then?
825 (if (and calculate-lisp-indent-last-sexp
826 (> calculate-lisp-indent-last-sexp (point)))
827 ;; Yes, but is there a containing sexp after that?
828 (let ((peek (parse-partial-sexp calculate-lisp-indent-last-sexp
829 indent-point 0)))
830 (if (setq retry (car (cdr peek))) (setq state peek)))))
831 (if retry
832 nil
833 ;; Innermost containing sexp found
834 (goto-char (1+ containing-sexp))
835 (if (not calculate-lisp-indent-last-sexp)
836 ;; indent-point immediately follows open paren.
837 ;; Don't call hook.
838 (setq desired-indent (current-column))
839 ;; Find the start of first element of containing sexp.
840 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
841 (cond ((looking-at "\\s(")
842 ;; First element of containing sexp is a list.
843 ;; Indent under that list.
844 )
845 ((> (save-excursion (forward-line 1) (point))
846 calculate-lisp-indent-last-sexp)
847 ;; This is the first line to start within the containing sexp.
848 ;; It's almost certainly a function call.
849 (if (= (point) calculate-lisp-indent-last-sexp)
850 ;; Containing sexp has nothing before this line
851 ;; except the first element. Indent under that element.
852 nil
853 ;; Skip the first element, find start of second (the first
854 ;; argument of the function call) and indent under.
855 (progn (forward-sexp 1)
856 (parse-partial-sexp (point)
857 calculate-lisp-indent-last-sexp
858 0 t)))
859 (backward-prefix-chars))
860 (t
861 ;; Indent beneath first sexp on same line as
862 ;; `calculate-lisp-indent-last-sexp'. Again, it's
863 ;; almost certainly a function call.
864 (goto-char calculate-lisp-indent-last-sexp)
865 (beginning-of-line)
866 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp
867 0 t)
868 (backward-prefix-chars)))))
869 ;; Point is at the point to indent under unless we are inside a string.
870 ;; Call indentation hook except when overridden by lisp-indent-offset
871 ;; or if the desired indentation has already been computed.
872 (let ((normal-indent (current-column)))
873 (cond ((elt state 3)
874 ;; Inside a string, don't change indentation.
875 nil)
876 ((and (integerp lisp-indent-offset) containing-sexp)
877 ;; Indent by constant offset
878 (goto-char containing-sexp)
879 (+ (current-column) lisp-indent-offset))
880 (desired-indent)
881 ((and (boundp 'lisp-indent-function)
882 lisp-indent-function
883 (not retry))
884 (or (funcall lisp-indent-function indent-point state)
885 normal-indent))
886 (t
887 normal-indent))))))
888
889 (defun lisp-indent-function (indent-point state)
890 "This function is the normal value of the variable `lisp-indent-function'.
891 It is used when indenting a line within a function call, to see if the
892 called function says anything special about how to indent the line.
893
894 INDENT-POINT is the position where the user typed TAB, or equivalent.
895 Point is located at the point to indent under (for default indentation);
896 STATE is the `parse-partial-sexp' state for that position.
897
898 If the current line is in a call to a Lisp function
899 which has a non-nil property `lisp-indent-function',
900 that specifies how to do the indentation. The property value can be
901 * `defun', meaning indent `defun'-style;
902 * an integer N, meaning indent the first N arguments specially
903 like ordinary function arguments and then indent any further
904 arguments like a body;
905 * a function to call just as this function was called.
906 If that function returns nil, that means it doesn't specify
907 the indentation.
908
909 This function also returns nil meaning don't specify the indentation."
910 (let ((normal-indent (current-column)))
911 (goto-char (1+ (elt state 1)))
912 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
913 (if (and (elt state 2)
914 (not (looking-at "\\sw\\|\\s_")))
915 ;; car of form doesn't seem to be a symbol
916 (progn
917 (if (not (> (save-excursion (forward-line 1) (point))
918 calculate-lisp-indent-last-sexp))
919 (progn (goto-char calculate-lisp-indent-last-sexp)
920 (beginning-of-line)
921 (parse-partial-sexp (point)
922 calculate-lisp-indent-last-sexp 0 t)))
923 ;; Indent under the list or under the first sexp on the same
924 ;; line as calculate-lisp-indent-last-sexp. Note that first
925 ;; thing on that line has to be complete sexp since we are
926 ;; inside the innermost containing sexp.
927 (backward-prefix-chars)
928 (current-column))
929 (let ((function (buffer-substring (point)
930 (progn (forward-sexp 1) (point))))
931 method)
932 (setq method (or (get (intern-soft function) 'lisp-indent-function)
933 (get (intern-soft function) 'lisp-indent-hook)))
934 (cond ((or (eq method 'defun)
935 (and (null method)
936 (> (length function) 3)
937 (string-match "\\`def" function)))
938 (lisp-indent-defform state indent-point))
939 ((integerp method)
940 (lisp-indent-specform method state
941 indent-point normal-indent))
942 (method
943 (funcall method indent-point state)))))))
944
945 (defvar lisp-body-indent 2
946 "Number of columns to indent the second line of a `(def...)' form.")
947
948 (defun lisp-indent-specform (count state indent-point normal-indent)
949 (let ((containing-form-start (elt state 1))
950 (i count)
951 body-indent containing-form-column)
952 ;; Move to the start of containing form, calculate indentation
953 ;; to use for non-distinguished forms (> count), and move past the
954 ;; function symbol. lisp-indent-function guarantees that there is at
955 ;; least one word or symbol character following open paren of containing
956 ;; form.
957 (goto-char containing-form-start)
958 (setq containing-form-column (current-column))
959 (setq body-indent (+ lisp-body-indent containing-form-column))
960 (forward-char 1)
961 (forward-sexp 1)
962 ;; Now find the start of the last form.
963 (parse-partial-sexp (point) indent-point 1 t)
964 (while (and (< (point) indent-point)
965 (condition-case ()
966 (progn
967 (setq count (1- count))
968 (forward-sexp 1)
969 (parse-partial-sexp (point) indent-point 1 t))
970 (error nil))))
971 ;; Point is sitting on first character of last (or count) sexp.
972 (if (> count 0)
973 ;; A distinguished form. If it is the first or second form use double
974 ;; lisp-body-indent, else normal indent. With lisp-body-indent bound
975 ;; to 2 (the default), this just happens to work the same with if as
976 ;; the older code, but it makes unwind-protect, condition-case,
977 ;; with-output-to-temp-buffer, et. al. much more tasteful. The older,
978 ;; less hacked, behavior can be obtained by replacing below with
979 ;; (list normal-indent containing-form-start).
980 (if (<= (- i count) 1)
981 (list (+ containing-form-column (* 2 lisp-body-indent))
982 containing-form-start)
983 (list normal-indent containing-form-start))
984 ;; A non-distinguished form. Use body-indent if there are no
985 ;; distinguished forms and this is the first undistinguished form,
986 ;; or if this is the first undistinguished form and the preceding
987 ;; distinguished form has indentation at least as great as body-indent.
988 (if (or (and (= i 0) (= count 0))
989 (and (= count 0) (<= body-indent normal-indent)))
990 body-indent
991 normal-indent))))
992
993 (defun lisp-indent-defform (state indent-point)
994 (goto-char (car (cdr state)))
995 (forward-line 1)
996 (if (> (point) (car (cdr (cdr state))))
997 (progn
998 (goto-char (car (cdr state)))
999 (+ lisp-body-indent (current-column)))))
1000
1001
1002 ;; (put 'progn 'lisp-indent-function 0), say, causes progn to be indented
1003 ;; like defun if the first form is placed on the next line, otherwise
1004 ;; it is indented like any other form (i.e. forms line up under first).
1005
1006 (put 'lambda 'lisp-indent-function 'defun)
1007 (put 'autoload 'lisp-indent-function 'defun)
1008 (put 'progn 'lisp-indent-function 0)
1009 (put 'prog1 'lisp-indent-function 1)
1010 (put 'prog2 'lisp-indent-function 2)
1011 (put 'save-excursion 'lisp-indent-function 0)
1012 (put 'save-window-excursion 'lisp-indent-function 0)
1013 (put 'save-selected-window 'lisp-indent-function 0)
1014 (put 'save-restriction 'lisp-indent-function 0)
1015 (put 'save-match-data 'lisp-indent-function 0)
1016 (put 'save-current-buffer 'lisp-indent-function 0)
1017 (put 'with-current-buffer 'lisp-indent-function 1)
1018 (put 'combine-after-change-calls 'lisp-indent-function 0)
1019 (put 'with-output-to-string 'lisp-indent-function 0)
1020 (put 'with-temp-file 'lisp-indent-function 1)
1021 (put 'with-temp-buffer 'lisp-indent-function 0)
1022 (put 'with-temp-message 'lisp-indent-function 1)
1023 (put 'with-syntax-table 'lisp-indent-function 1)
1024 (put 'let 'lisp-indent-function 1)
1025 (put 'let* 'lisp-indent-function 1)
1026 (put 'while 'lisp-indent-function 1)
1027 (put 'if 'lisp-indent-function 2)
1028 (put 'read-if 'lisp-indent-function 2)
1029 (put 'catch 'lisp-indent-function 1)
1030 (put 'condition-case 'lisp-indent-function 2)
1031 (put 'unwind-protect 'lisp-indent-function 1)
1032 (put 'with-output-to-temp-buffer 'lisp-indent-function 1)
1033 (put 'eval-after-load 'lisp-indent-function 1)
1034 (put 'dolist 'lisp-indent-function 1)
1035 (put 'dotimes 'lisp-indent-function 1)
1036 (put 'when 'lisp-indent-function 1)
1037 (put 'unless 'lisp-indent-function 1)
1038
1039 (defun indent-sexp (&optional endpos)
1040 "Indent each line of the list starting just after point.
1041 If optional arg ENDPOS is given, indent each line, stopping when
1042 ENDPOS is encountered."
1043 (interactive)
1044 (let ((indent-stack (list nil))
1045 (next-depth 0)
1046 ;; If ENDPOS is non-nil, use nil as STARTING-POINT
1047 ;; so that calculate-lisp-indent will find the beginning of
1048 ;; the defun we are in.
1049 ;; If ENDPOS is nil, it is safe not to scan before point
1050 ;; since every line we indent is more deeply nested than point is.
1051 (starting-point (if endpos nil (point)))
1052 (last-point (point))
1053 last-depth bol outer-loop-done inner-loop-done state this-indent)
1054 (or endpos
1055 ;; Get error now if we don't have a complete sexp after point.
1056 (save-excursion (forward-sexp 1)))
1057 (save-excursion
1058 (setq outer-loop-done nil)
1059 (while (if endpos (< (point) endpos)
1060 (not outer-loop-done))
1061 (setq last-depth next-depth
1062 inner-loop-done nil)
1063 ;; Parse this line so we can learn the state
1064 ;; to indent the next line.
1065 ;; This inner loop goes through only once
1066 ;; unless a line ends inside a string.
1067 (while (and (not inner-loop-done)
1068 (not (setq outer-loop-done (eobp))))
1069 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
1070 nil nil state))
1071 (setq next-depth (car state))
1072 ;; If the line contains a comment other than the sort
1073 ;; that is indented like code,
1074 ;; indent it now with indent-for-comment.
1075 ;; Comments indented like code are right already.
1076 ;; In any case clear the in-comment flag in the state
1077 ;; because parse-partial-sexp never sees the newlines.
1078 (if (car (nthcdr 4 state))
1079 (progn (indent-for-comment)
1080 (end-of-line)
1081 (setcar (nthcdr 4 state) nil)))
1082 ;; If this line ends inside a string,
1083 ;; go straight to next line, remaining within the inner loop,
1084 ;; and turn off the \-flag.
1085 (if (car (nthcdr 3 state))
1086 (progn
1087 (forward-line 1)
1088 (setcar (nthcdr 5 state) nil))
1089 (setq inner-loop-done t)))
1090 (and endpos
1091 (<= next-depth 0)
1092 (progn
1093 (setq indent-stack (nconc indent-stack
1094 (make-list (- next-depth) nil))
1095 last-depth (- last-depth next-depth)
1096 next-depth 0)))
1097 (or outer-loop-done endpos
1098 (setq outer-loop-done (<= next-depth 0)))
1099 (if outer-loop-done
1100 (forward-line 1)
1101 (while (> last-depth next-depth)
1102 (setq indent-stack (cdr indent-stack)
1103 last-depth (1- last-depth)))
1104 (while (< last-depth next-depth)
1105 (setq indent-stack (cons nil indent-stack)
1106 last-depth (1+ last-depth)))
1107 ;; Now go to the next line and indent it according
1108 ;; to what we learned from parsing the previous one.
1109 (forward-line 1)
1110 (setq bol (point))
1111 (skip-chars-forward " \t")
1112 ;; But not if the line is blank, or just a comment
1113 ;; (except for double-semi comments; indent them as usual).
1114 (if (or (eobp) (looking-at "\\s<\\|\n"))
1115 nil
1116 (if (and (car indent-stack)
1117 (>= (car indent-stack) 0))
1118 (setq this-indent (car indent-stack))
1119 (let ((val (calculate-lisp-indent
1120 (if (car indent-stack) (- (car indent-stack))
1121 starting-point))))
1122 (if (null val)
1123 (setq this-indent val)
1124 (if (integerp val)
1125 (setcar indent-stack
1126 (setq this-indent val))
1127 (setcar indent-stack (- (car (cdr val))))
1128 (setq this-indent (car val))))))
1129 (if (and this-indent (/= (current-column) this-indent))
1130 (progn (delete-region bol (point))
1131 (indent-to this-indent)))))
1132 (or outer-loop-done
1133 (setq outer-loop-done (= (point) last-point))
1134 (setq last-point (point)))))))
1135
1136 (defun lisp-indent-region (start end)
1137 "Indent every line whose first char is between START and END inclusive."
1138 (save-excursion
1139 (let ((endmark (copy-marker end)))
1140 (goto-char start)
1141 (and (bolp) (not (eolp))
1142 (lisp-indent-line))
1143 (indent-sexp endmark)
1144 (set-marker endmark nil))))
1145
1146 (defun indent-pp-sexp (&optional arg)
1147 "Indent each line of the list starting just after point, or prettyprint it.
1148 A prefix argument specifies pretty-printing."
1149 (interactive "P")
1150 (if arg
1151 (save-excursion
1152 (save-restriction
1153 (narrow-to-region (point) (progn (forward-sexp 1) (point)))
1154 (pp-buffer)
1155 (goto-char (point-max))
1156 (if (eq (char-before) ?\n)
1157 (delete-char -1)))))
1158 (indent-sexp))
1159
1160 ;;;; Lisp paragraph filling commands.
1161
1162 (defcustom emacs-lisp-docstring-fill-column 65
1163 "Value of `fill-column' to use when filling a docstring.
1164 Any non-integer value means do not use a different value of
1165 `fill-column' when filling docstrings."
1166 :type '(choice (integer)
1167 (const :tag "Use the current `fill-column'" t))
1168 :group 'lisp)
1169
1170 (defun lisp-fill-paragraph (&optional justify)
1171 "Like \\[fill-paragraph], but handle Emacs Lisp comments and docstrings.
1172 If any of the current line is a comment, fill the comment or the
1173 paragraph of it that point is in, preserving the comment's indentation
1174 and initial semicolons."
1175 (interactive "P")
1176 (or (fill-comment-paragraph justify)
1177 ;; Since fill-comment-paragraph returned nil, that means we're not in
1178 ;; a comment: Point is on a program line; we are interested
1179 ;; particularly in docstring lines.
1180 ;;
1181 ;; We bind `paragraph-start' and `paragraph-separate' temporarily. They
1182 ;; are buffer-local, but we avoid changing them so that they can be set
1183 ;; to make `forward-paragraph' and friends do something the user wants.
1184 ;;
1185 ;; `paragraph-start': The `(' in the character alternative and the
1186 ;; left-singlequote plus `(' sequence after the \\| alternative prevent
1187 ;; sexps and backquoted sexps that follow a docstring from being filled
1188 ;; with the docstring. This setting has the consequence of inhibiting
1189 ;; filling many program lines that are not docstrings, which is sensible,
1190 ;; because the user probably asked to fill program lines by accident, or
1191 ;; expecting indentation (perhaps we should try to do indenting in that
1192 ;; case). The `;' and `:' stop the paragraph being filled at following
1193 ;; comment lines and at keywords (e.g., in `defcustom'). Left parens are
1194 ;; escaped to keep font-locking, filling, & paren matching in the source
1195 ;; file happy.
1196 ;;
1197 ;; `paragraph-separate': A clever regexp distinguishes the first line of
1198 ;; a docstring and identifies it as a paragraph separator, so that it
1199 ;; won't be filled. (Since the first line of documentation stands alone
1200 ;; in some contexts, filling should not alter the contents the author has
1201 ;; chosen.) Only the first line of a docstring begins with whitespace
1202 ;; and a quotation mark and ends with a period or (rarely) a comma.
1203 ;;
1204 ;; The `fill-column' is temporarily bound to
1205 ;; `emacs-lisp-docstring-fill-column' if that value is an integer.
1206 (let ((paragraph-start (concat paragraph-start
1207 "\\|\\s-*\\([(;:\"]\\|`(\\|#'(\\)"))
1208 (paragraph-separate
1209 (concat paragraph-separate "\\|\\s-*\".*[,\\.]$"))
1210 (fill-column (if (integerp emacs-lisp-docstring-fill-column)
1211 emacs-lisp-docstring-fill-column
1212 fill-column)))
1213 (fill-paragraph justify))
1214 ;; Never return nil.
1215 t))
1216
1217 (defun indent-code-rigidly (start end arg &optional nochange-regexp)
1218 "Indent all lines of code, starting in the region, sideways by ARG columns.
1219 Does not affect lines starting inside comments or strings, assuming that
1220 the start of the region is not inside them.
1221
1222 Called from a program, takes args START, END, COLUMNS and NOCHANGE-REGEXP.
1223 The last is a regexp which, if matched at the beginning of a line,
1224 means don't indent that line."
1225 (interactive "r\np")
1226 (let (state)
1227 (save-excursion
1228 (goto-char end)
1229 (setq end (point-marker))
1230 (goto-char start)
1231 (or (bolp)
1232 (setq state (parse-partial-sexp (point)
1233 (progn
1234 (forward-line 1) (point))
1235 nil nil state)))
1236 (while (< (point) end)
1237 (or (car (nthcdr 3 state))
1238 (and nochange-regexp
1239 (looking-at nochange-regexp))
1240 ;; If line does not start in string, indent it
1241 (let ((indent (current-indentation)))
1242 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
1243 (or (eolp)
1244 (indent-to (max 0 (+ indent arg)) 0))))
1245 (setq state (parse-partial-sexp (point)
1246 (progn
1247 (forward-line 1) (point))
1248 nil nil state))))))
1249
1250 (provide 'lisp-mode)
1251
1252 ;; arch-tag: 414c7f93-c245-4b77-8ed5-ed05ef7ff1bf
1253 ;;; lisp-mode.el ends here