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