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