]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/lisp-mode.el
Merged from miles@gnu.org--gnu-2005 (patch 142-148, 615-628)
[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 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-derived-mode"
100 "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\\(\\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\\(\\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\\(\\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 'define-generic-mode 'doc-string-elt 7)
145 (put 'define-ibuffer-filter 'doc-string-elt 2)
146 (put 'define-ibuffer-op 'doc-string-elt 3)
147 (put 'define-ibuffer-sorter 'doc-string-elt 2)
148 (put 'lambda 'doc-string-elt 2)
149 (put 'defalias 'doc-string-elt 3)
150 (put 'defvaralias 'doc-string-elt 3)
151 (put 'define-category 'doc-string-elt 2)
152
153 (defvar lisp-doc-string-elt-property 'doc-string-elt
154 "The symbol property that holds the docstring position info.")
155
156 (defun lisp-font-lock-syntactic-face-function (state)
157 (if (nth 3 state)
158 ;; This might be a (doc)string or a |...| symbol.
159 (let ((startpos (nth 8 state)))
160 (if (eq (char-after startpos) ?|)
161 ;; This is not a string, but a |...| symbol.
162 nil
163 (let* ((listbeg (nth 1 state))
164 (firstsym (and listbeg
165 (save-excursion
166 (goto-char listbeg)
167 (and (looking-at "([ \t\n]*\\(\\(\\sw\\|\\s_\\)+\\)")
168 (match-string 1)))))
169 (docelt (and firstsym (get (intern-soft firstsym)
170 lisp-doc-string-elt-property))))
171 (if (and docelt
172 ;; It's a string in a form that can have a docstring.
173 ;; Check whether it's in docstring position.
174 (save-excursion
175 (when (functionp docelt)
176 (goto-char (match-end 1))
177 (setq docelt (funcall docelt)))
178 (goto-char listbeg)
179 (forward-char 1)
180 (condition-case nil
181 (while (and (> docelt 0) (< (point) startpos)
182 (progn (forward-sexp 1) t))
183 (setq docelt (1- docelt)))
184 (error nil))
185 (and (zerop docelt) (<= (point) startpos)
186 (progn (forward-comment (point-max)) t)
187 (= (point) (nth 8 state)))))
188 font-lock-doc-face
189 font-lock-string-face))))
190 font-lock-comment-face))
191
192 ;; The LISP-SYNTAX argument is used by code in inf-lisp.el and is
193 ;; (uselessly) passed from pp.el, chistory.el, gnus-kill.el and score-mode.el
194 (defun lisp-mode-variables (&optional lisp-syntax)
195 (when lisp-syntax
196 (set-syntax-table lisp-mode-syntax-table))
197 (setq local-abbrev-table lisp-mode-abbrev-table)
198 (make-local-variable 'paragraph-ignore-fill-prefix)
199 (setq paragraph-ignore-fill-prefix t)
200 (make-local-variable 'fill-paragraph-function)
201 (setq fill-paragraph-function 'lisp-fill-paragraph)
202 ;; Adaptive fill mode gets the fill wrong for a one-line paragraph made of
203 ;; a single docstring. Let's fix it here.
204 (set (make-local-variable 'adaptive-fill-function)
205 (lambda () (if (looking-at "\\s-+\"[^\n\"]+\"\\s-*$") "")))
206 ;; Adaptive fill mode gets in the way of auto-fill,
207 ;; and should make no difference for explicit fill
208 ;; because lisp-fill-paragraph should do the job.
209 ;; I believe that newcomment's auto-fill code properly deals with it -stef
210 ;;(set (make-local-variable 'adaptive-fill-mode) nil)
211 (make-local-variable 'indent-line-function)
212 (setq indent-line-function 'lisp-indent-line)
213 (make-local-variable 'indent-region-function)
214 (setq indent-region-function 'lisp-indent-region)
215 (make-local-variable 'parse-sexp-ignore-comments)
216 (setq parse-sexp-ignore-comments t)
217 (make-local-variable 'outline-regexp)
218 (setq outline-regexp ";;;\\(;* [^ \t\n]\\|###autoload\\)\\|(")
219 (make-local-variable 'outline-level)
220 (setq outline-level 'lisp-outline-level)
221 (make-local-variable 'comment-start)
222 (setq comment-start ";")
223 (make-local-variable 'comment-start-skip)
224 ;; Look within the line for a ; following an even number of backslashes
225 ;; after either a non-backslash or the line beginning.
226 (setq comment-start-skip "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\);+ *")
227 (make-local-variable 'font-lock-comment-start-skip)
228 ;; Font lock mode uses this only when it KNOWS a comment is starting.
229 (setq font-lock-comment-start-skip ";+ *")
230 (make-local-variable 'comment-add)
231 (setq comment-add 1) ;default to `;;' in comment-region
232 (make-local-variable 'comment-column)
233 (setq comment-column 40)
234 ;; Don't get confused by `;' in doc strings when paragraph-filling.
235 (set (make-local-variable 'comment-use-global-state) t)
236 (make-local-variable 'comment-indent-function)
237 (setq comment-indent-function 'lisp-comment-indent)
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 \f
750 ;; Used for comment-indent-function in Lisp modes.
751 (defun lisp-comment-indent ()
752 (if (looking-at "\\s<\\s<\\s<")
753 (current-column)
754 (if (looking-at "\\s<\\s<")
755 (let ((tem (or (calculate-lisp-indent) (current-column))))
756 (if (listp tem) (car tem) tem))
757 (skip-chars-backward " \t")
758 (max (if (bolp) 0 (1+ (current-column)))
759 comment-column))))
760
761 ;; This function just forces a more costly detection of comments (using
762 ;; parse-partial-sexp from beginning-of-defun). I.e. It avoids the problem of
763 ;; taking a `;' inside a string started on another line for a comment starter.
764 ;; Note: `newcomment' gets it right now since we set comment-use-global-state
765 ;; so we could get rid of it. -stef
766 (defun lisp-mode-auto-fill ()
767 (if (> (current-column) (current-fill-column))
768 (if (save-excursion
769 (nth 4 (syntax-ppss (point))))
770 (do-auto-fill)
771 (unless (and (boundp 'comment-auto-fill-only-comments)
772 comment-auto-fill-only-comments)
773 (let ((comment-start nil) (comment-start-skip nil))
774 (do-auto-fill))))))
775
776 (defvar lisp-indent-offset nil
777 "If non-nil, indent second line of expressions that many more columns.")
778 (defvar lisp-indent-function 'lisp-indent-function)
779
780 (defun lisp-indent-line (&optional whole-exp)
781 "Indent current line as Lisp code.
782 With argument, indent any additional lines of the same expression
783 rigidly along with this one."
784 (interactive "P")
785 (let ((indent (calculate-lisp-indent)) shift-amt end
786 (pos (- (point-max) (point)))
787 (beg (progn (beginning-of-line) (point))))
788 (skip-chars-forward " \t")
789 (if (or (null indent) (looking-at "\\s<\\s<\\s<"))
790 ;; Don't alter indentation of a ;;; comment line
791 ;; or a line that starts in a string.
792 (goto-char (- (point-max) pos))
793 (if (and (looking-at "\\s<") (not (looking-at "\\s<\\s<")))
794 ;; Single-semicolon comment lines should be indented
795 ;; as comment lines, not as code.
796 (progn (indent-for-comment) (forward-char -1))
797 (if (listp indent) (setq indent (car indent)))
798 (setq shift-amt (- indent (current-column)))
799 (if (zerop shift-amt)
800 nil
801 (delete-region beg (point))
802 (indent-to indent)))
803 ;; If initial point was within line's indentation,
804 ;; position after the indentation. Else stay at same point in text.
805 (if (> (- (point-max) pos) (point))
806 (goto-char (- (point-max) pos)))
807 ;; If desired, shift remaining lines of expression the same amount.
808 (and whole-exp (not (zerop shift-amt))
809 (save-excursion
810 (goto-char beg)
811 (forward-sexp 1)
812 (setq end (point))
813 (goto-char beg)
814 (forward-line 1)
815 (setq beg (point))
816 (> end beg))
817 (indent-code-rigidly beg end shift-amt)))))
818
819 (defvar calculate-lisp-indent-last-sexp)
820
821 (defun calculate-lisp-indent (&optional parse-start)
822 "Return appropriate indentation for current line as Lisp code.
823 In usual case returns an integer: the column to indent to.
824 If the value is nil, that means don't change the indentation
825 because the line starts inside a string.
826
827 The value can also be a list of the form (COLUMN CONTAINING-SEXP-START).
828 This means that following lines at the same level of indentation
829 should not necessarily be indented the same as this line.
830 Then COLUMN is the column to indent to, and CONTAINING-SEXP-START
831 is the buffer position of the start of the containing expression."
832 (save-excursion
833 (beginning-of-line)
834 (let ((indent-point (point))
835 state paren-depth
836 ;; setting this to a number inhibits calling hook
837 (desired-indent nil)
838 (retry t)
839 calculate-lisp-indent-last-sexp containing-sexp)
840 (if parse-start
841 (goto-char parse-start)
842 (beginning-of-defun))
843 ;; Find outermost containing sexp
844 (while (< (point) indent-point)
845 (setq state (parse-partial-sexp (point) indent-point 0)))
846 ;; Find innermost containing sexp
847 (while (and retry
848 state
849 (> (setq paren-depth (elt state 0)) 0))
850 (setq retry nil)
851 (setq calculate-lisp-indent-last-sexp (elt state 2))
852 (setq containing-sexp (elt state 1))
853 ;; Position following last unclosed open.
854 (goto-char (1+ containing-sexp))
855 ;; Is there a complete sexp since then?
856 (if (and calculate-lisp-indent-last-sexp
857 (> calculate-lisp-indent-last-sexp (point)))
858 ;; Yes, but is there a containing sexp after that?
859 (let ((peek (parse-partial-sexp calculate-lisp-indent-last-sexp
860 indent-point 0)))
861 (if (setq retry (car (cdr peek))) (setq state peek)))))
862 (if retry
863 nil
864 ;; Innermost containing sexp found
865 (goto-char (1+ containing-sexp))
866 (if (not calculate-lisp-indent-last-sexp)
867 ;; indent-point immediately follows open paren.
868 ;; Don't call hook.
869 (setq desired-indent (current-column))
870 ;; Find the start of first element of containing sexp.
871 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
872 (cond ((looking-at "\\s(")
873 ;; First element of containing sexp is a list.
874 ;; Indent under that list.
875 )
876 ((> (save-excursion (forward-line 1) (point))
877 calculate-lisp-indent-last-sexp)
878 ;; This is the first line to start within the containing sexp.
879 ;; It's almost certainly a function call.
880 (if (= (point) calculate-lisp-indent-last-sexp)
881 ;; Containing sexp has nothing before this line
882 ;; except the first element. Indent under that element.
883 nil
884 ;; Skip the first element, find start of second (the first
885 ;; argument of the function call) and indent under.
886 (progn (forward-sexp 1)
887 (parse-partial-sexp (point)
888 calculate-lisp-indent-last-sexp
889 0 t)))
890 (backward-prefix-chars))
891 (t
892 ;; Indent beneath first sexp on same line as
893 ;; `calculate-lisp-indent-last-sexp'. Again, it's
894 ;; almost certainly a function call.
895 (goto-char calculate-lisp-indent-last-sexp)
896 (beginning-of-line)
897 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp
898 0 t)
899 (backward-prefix-chars)))))
900 ;; Point is at the point to indent under unless we are inside a string.
901 ;; Call indentation hook except when overridden by lisp-indent-offset
902 ;; or if the desired indentation has already been computed.
903 (let ((normal-indent (current-column)))
904 (cond ((elt state 3)
905 ;; Inside a string, don't change indentation.
906 nil)
907 ((and (integerp lisp-indent-offset) containing-sexp)
908 ;; Indent by constant offset
909 (goto-char containing-sexp)
910 (+ (current-column) lisp-indent-offset))
911 (desired-indent)
912 ((and (boundp 'lisp-indent-function)
913 lisp-indent-function
914 (not retry))
915 (or (funcall lisp-indent-function indent-point state)
916 normal-indent))
917 (t
918 normal-indent))))))
919
920 (defun lisp-indent-function (indent-point state)
921 "This function is the normal value of the variable `lisp-indent-function'.
922 It is used when indenting a line within a function call, to see if the
923 called function says anything special about how to indent the line.
924
925 INDENT-POINT is the position where the user typed TAB, or equivalent.
926 Point is located at the point to indent under (for default indentation);
927 STATE is the `parse-partial-sexp' state for that position.
928
929 If the current line is in a call to a Lisp function
930 which has a non-nil property `lisp-indent-function',
931 that specifies how to do the indentation. The property value can be
932 * `defun', meaning indent `defun'-style;
933 * an integer N, meaning indent the first N arguments specially
934 like ordinary function arguments and then indent any further
935 arguments like a body;
936 * a function to call just as this function was called.
937 If that function returns nil, that means it doesn't specify
938 the indentation.
939
940 This function also returns nil meaning don't specify the indentation."
941 (let ((normal-indent (current-column)))
942 (goto-char (1+ (elt state 1)))
943 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
944 (if (and (elt state 2)
945 (not (looking-at "\\sw\\|\\s_")))
946 ;; car of form doesn't seem to be a symbol
947 (progn
948 (if (not (> (save-excursion (forward-line 1) (point))
949 calculate-lisp-indent-last-sexp))
950 (progn (goto-char calculate-lisp-indent-last-sexp)
951 (beginning-of-line)
952 (parse-partial-sexp (point)
953 calculate-lisp-indent-last-sexp 0 t)))
954 ;; Indent under the list or under the first sexp on the same
955 ;; line as calculate-lisp-indent-last-sexp. Note that first
956 ;; thing on that line has to be complete sexp since we are
957 ;; inside the innermost containing sexp.
958 (backward-prefix-chars)
959 (current-column))
960 (let ((function (buffer-substring (point)
961 (progn (forward-sexp 1) (point))))
962 method)
963 (setq method (or (get (intern-soft function) 'lisp-indent-function)
964 (get (intern-soft function) 'lisp-indent-hook)))
965 (cond ((or (eq method 'defun)
966 (and (null method)
967 (> (length function) 3)
968 (string-match "\\`def" function)))
969 (lisp-indent-defform state indent-point))
970 ((integerp method)
971 (lisp-indent-specform method state
972 indent-point normal-indent))
973 (method
974 (funcall method indent-point state)))))))
975
976 (defvar lisp-body-indent 2
977 "Number of columns to indent the second line of a `(def...)' form.")
978
979 (defun lisp-indent-specform (count state indent-point normal-indent)
980 (let ((containing-form-start (elt state 1))
981 (i count)
982 body-indent containing-form-column)
983 ;; Move to the start of containing form, calculate indentation
984 ;; to use for non-distinguished forms (> count), and move past the
985 ;; function symbol. lisp-indent-function guarantees that there is at
986 ;; least one word or symbol character following open paren of containing
987 ;; form.
988 (goto-char containing-form-start)
989 (setq containing-form-column (current-column))
990 (setq body-indent (+ lisp-body-indent containing-form-column))
991 (forward-char 1)
992 (forward-sexp 1)
993 ;; Now find the start of the last form.
994 (parse-partial-sexp (point) indent-point 1 t)
995 (while (and (< (point) indent-point)
996 (condition-case ()
997 (progn
998 (setq count (1- count))
999 (forward-sexp 1)
1000 (parse-partial-sexp (point) indent-point 1 t))
1001 (error nil))))
1002 ;; Point is sitting on first character of last (or count) sexp.
1003 (if (> count 0)
1004 ;; A distinguished form. If it is the first or second form use double
1005 ;; lisp-body-indent, else normal indent. With lisp-body-indent bound
1006 ;; to 2 (the default), this just happens to work the same with if as
1007 ;; the older code, but it makes unwind-protect, condition-case,
1008 ;; with-output-to-temp-buffer, et. al. much more tasteful. The older,
1009 ;; less hacked, behavior can be obtained by replacing below with
1010 ;; (list normal-indent containing-form-start).
1011 (if (<= (- i count) 1)
1012 (list (+ containing-form-column (* 2 lisp-body-indent))
1013 containing-form-start)
1014 (list normal-indent containing-form-start))
1015 ;; A non-distinguished form. Use body-indent if there are no
1016 ;; distinguished forms and this is the first undistinguished form,
1017 ;; or if this is the first undistinguished form and the preceding
1018 ;; distinguished form has indentation at least as great as body-indent.
1019 (if (or (and (= i 0) (= count 0))
1020 (and (= count 0) (<= body-indent normal-indent)))
1021 body-indent
1022 normal-indent))))
1023
1024 (defun lisp-indent-defform (state indent-point)
1025 (goto-char (car (cdr state)))
1026 (forward-line 1)
1027 (if (> (point) (car (cdr (cdr state))))
1028 (progn
1029 (goto-char (car (cdr state)))
1030 (+ lisp-body-indent (current-column)))))
1031
1032
1033 ;; (put 'progn 'lisp-indent-function 0), say, causes progn to be indented
1034 ;; like defun if the first form is placed on the next line, otherwise
1035 ;; it is indented like any other form (i.e. forms line up under first).
1036
1037 (put 'lambda 'lisp-indent-function 'defun)
1038 (put 'autoload 'lisp-indent-function 'defun)
1039 (put 'progn 'lisp-indent-function 0)
1040 (put 'prog1 'lisp-indent-function 1)
1041 (put 'prog2 'lisp-indent-function 2)
1042 (put 'save-excursion 'lisp-indent-function 0)
1043 (put 'save-window-excursion 'lisp-indent-function 0)
1044 (put 'save-selected-window 'lisp-indent-function 0)
1045 (put 'save-restriction 'lisp-indent-function 0)
1046 (put 'save-match-data 'lisp-indent-function 0)
1047 (put 'save-current-buffer 'lisp-indent-function 0)
1048 (put 'with-current-buffer 'lisp-indent-function 1)
1049 (put 'combine-after-change-calls 'lisp-indent-function 0)
1050 (put 'with-output-to-string 'lisp-indent-function 0)
1051 (put 'with-temp-file 'lisp-indent-function 1)
1052 (put 'with-temp-buffer 'lisp-indent-function 0)
1053 (put 'with-temp-message 'lisp-indent-function 1)
1054 (put 'with-syntax-table 'lisp-indent-function 1)
1055 (put 'let 'lisp-indent-function 1)
1056 (put 'let* 'lisp-indent-function 1)
1057 (put 'while 'lisp-indent-function 1)
1058 (put 'if 'lisp-indent-function 2)
1059 (put 'read-if 'lisp-indent-function 2)
1060 (put 'catch 'lisp-indent-function 1)
1061 (put 'condition-case 'lisp-indent-function 2)
1062 (put 'unwind-protect 'lisp-indent-function 1)
1063 (put 'with-output-to-temp-buffer 'lisp-indent-function 1)
1064 (put 'eval-after-load 'lisp-indent-function 1)
1065 (put 'dolist 'lisp-indent-function 1)
1066 (put 'dotimes 'lisp-indent-function 1)
1067 (put 'when 'lisp-indent-function 1)
1068 (put 'unless 'lisp-indent-function 1)
1069
1070 (defun indent-sexp (&optional endpos)
1071 "Indent each line of the list starting just after point.
1072 If optional arg ENDPOS is given, indent each line, stopping when
1073 ENDPOS is encountered."
1074 (interactive)
1075 (let ((indent-stack (list nil))
1076 (next-depth 0)
1077 ;; If ENDPOS is non-nil, use nil as STARTING-POINT
1078 ;; so that calculate-lisp-indent will find the beginning of
1079 ;; the defun we are in.
1080 ;; If ENDPOS is nil, it is safe not to scan before point
1081 ;; since every line we indent is more deeply nested than point is.
1082 (starting-point (if endpos nil (point)))
1083 (last-point (point))
1084 last-depth bol outer-loop-done inner-loop-done state this-indent)
1085 (or endpos
1086 ;; Get error now if we don't have a complete sexp after point.
1087 (save-excursion (forward-sexp 1)))
1088 (save-excursion
1089 (setq outer-loop-done nil)
1090 (while (if endpos (< (point) endpos)
1091 (not outer-loop-done))
1092 (setq last-depth next-depth
1093 inner-loop-done nil)
1094 ;; Parse this line so we can learn the state
1095 ;; to indent the next line.
1096 ;; This inner loop goes through only once
1097 ;; unless a line ends inside a string.
1098 (while (and (not inner-loop-done)
1099 (not (setq outer-loop-done (eobp))))
1100 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
1101 nil nil state))
1102 (setq next-depth (car state))
1103 ;; If the line contains a comment other than the sort
1104 ;; that is indented like code,
1105 ;; indent it now with indent-for-comment.
1106 ;; Comments indented like code are right already.
1107 ;; In any case clear the in-comment flag in the state
1108 ;; because parse-partial-sexp never sees the newlines.
1109 (if (car (nthcdr 4 state))
1110 (progn (indent-for-comment)
1111 (end-of-line)
1112 (setcar (nthcdr 4 state) nil)))
1113 ;; If this line ends inside a string,
1114 ;; go straight to next line, remaining within the inner loop,
1115 ;; and turn off the \-flag.
1116 (if (car (nthcdr 3 state))
1117 (progn
1118 (forward-line 1)
1119 (setcar (nthcdr 5 state) nil))
1120 (setq inner-loop-done t)))
1121 (and endpos
1122 (<= next-depth 0)
1123 (progn
1124 (setq indent-stack (nconc indent-stack
1125 (make-list (- next-depth) nil))
1126 last-depth (- last-depth next-depth)
1127 next-depth 0)))
1128 (or outer-loop-done endpos
1129 (setq outer-loop-done (<= next-depth 0)))
1130 (if outer-loop-done
1131 (forward-line 1)
1132 (while (> last-depth next-depth)
1133 (setq indent-stack (cdr indent-stack)
1134 last-depth (1- last-depth)))
1135 (while (< last-depth next-depth)
1136 (setq indent-stack (cons nil indent-stack)
1137 last-depth (1+ last-depth)))
1138 ;; Now go to the next line and indent it according
1139 ;; to what we learned from parsing the previous one.
1140 (forward-line 1)
1141 (setq bol (point))
1142 (skip-chars-forward " \t")
1143 ;; But not if the line is blank, or just a comment
1144 ;; (except for double-semi comments; indent them as usual).
1145 (if (or (eobp) (looking-at "\\s<\\|\n"))
1146 nil
1147 (if (and (car indent-stack)
1148 (>= (car indent-stack) 0))
1149 (setq this-indent (car indent-stack))
1150 (let ((val (calculate-lisp-indent
1151 (if (car indent-stack) (- (car indent-stack))
1152 starting-point))))
1153 (if (null val)
1154 (setq this-indent val)
1155 (if (integerp val)
1156 (setcar indent-stack
1157 (setq this-indent val))
1158 (setcar indent-stack (- (car (cdr val))))
1159 (setq this-indent (car val))))))
1160 (if (and this-indent (/= (current-column) this-indent))
1161 (progn (delete-region bol (point))
1162 (indent-to this-indent)))))
1163 (or outer-loop-done
1164 (setq outer-loop-done (= (point) last-point))
1165 (setq last-point (point)))))))
1166
1167 (defun lisp-indent-region (start end)
1168 "Indent every line whose first char is between START and END inclusive."
1169 (save-excursion
1170 (let ((endmark (copy-marker end)))
1171 (goto-char start)
1172 (and (bolp) (not (eolp))
1173 (lisp-indent-line))
1174 (indent-sexp endmark)
1175 (set-marker endmark nil))))
1176
1177 (defun indent-pp-sexp (&optional arg)
1178 "Indent each line of the list starting just after point, or prettyprint it.
1179 A prefix argument specifies pretty-printing."
1180 (interactive "P")
1181 (if arg
1182 (save-excursion
1183 (save-restriction
1184 (narrow-to-region (point) (progn (forward-sexp 1) (point)))
1185 (pp-buffer)
1186 (goto-char (point-max))
1187 (if (eq (char-before) ?\n)
1188 (delete-char -1)))))
1189 (indent-sexp))
1190
1191 ;;;; Lisp paragraph filling commands.
1192
1193 (defcustom emacs-lisp-docstring-fill-column 65
1194 "Value of `fill-column' to use when filling a docstring.
1195 Any non-integer value means do not use a different value of
1196 `fill-column' when filling docstrings."
1197 :type '(choice (integer)
1198 (const :tag "Use the current `fill-column'" t))
1199 :group 'lisp)
1200
1201 (defun lisp-fill-paragraph (&optional justify)
1202 "Like \\[fill-paragraph], but handle Emacs Lisp comments and docstrings.
1203 If any of the current line is a comment, fill the comment or the
1204 paragraph of it that point is in, preserving the comment's indentation
1205 and initial semicolons."
1206 (interactive "P")
1207 (or (fill-comment-paragraph justify)
1208 ;; Since fill-comment-paragraph returned nil, that means we're not in
1209 ;; a comment: Point is on a program line; we are interested
1210 ;; particularly in docstring lines.
1211 ;;
1212 ;; We bind `paragraph-start' and `paragraph-separate' temporarily. They
1213 ;; are buffer-local, but we avoid changing them so that they can be set
1214 ;; to make `forward-paragraph' and friends do something the user wants.
1215 ;;
1216 ;; `paragraph-start': The `(' in the character alternative and the
1217 ;; left-singlequote plus `(' sequence after the \\| alternative prevent
1218 ;; sexps and backquoted sexps that follow a docstring from being filled
1219 ;; with the docstring. This setting has the consequence of inhibiting
1220 ;; filling many program lines that are not docstrings, which is sensible,
1221 ;; because the user probably asked to fill program lines by accident, or
1222 ;; expecting indentation (perhaps we should try to do indenting in that
1223 ;; case). The `;' and `:' stop the paragraph being filled at following
1224 ;; comment lines and at keywords (e.g., in `defcustom'). Left parens are
1225 ;; escaped to keep font-locking, filling, & paren matching in the source
1226 ;; file happy.
1227 ;;
1228 ;; `paragraph-separate': A clever regexp distinguishes the first line of
1229 ;; a docstring and identifies it as a paragraph separator, so that it
1230 ;; won't be filled. (Since the first line of documentation stands alone
1231 ;; in some contexts, filling should not alter the contents the author has
1232 ;; chosen.) Only the first line of a docstring begins with whitespace
1233 ;; and a quotation mark and ends with a period or (rarely) a comma.
1234 ;;
1235 ;; The `fill-column' is temporarily bound to
1236 ;; `emacs-lisp-docstring-fill-column' if that value is an integer.
1237 (let ((paragraph-start (concat paragraph-start
1238 "\\|\\s-*\\([(;:\"]\\|`(\\|#'(\\)"))
1239 (paragraph-separate
1240 (concat paragraph-separate "\\|\\s-*\".*[,\\.]$"))
1241 (fill-column (if (integerp emacs-lisp-docstring-fill-column)
1242 emacs-lisp-docstring-fill-column
1243 fill-column)))
1244 (fill-paragraph justify))
1245 ;; Never return nil.
1246 t))
1247
1248 (defun indent-code-rigidly (start end arg &optional nochange-regexp)
1249 "Indent all lines of code, starting in the region, sideways by ARG columns.
1250 Does not affect lines starting inside comments or strings, assuming that
1251 the start of the region is not inside them.
1252
1253 Called from a program, takes args START, END, COLUMNS and NOCHANGE-REGEXP.
1254 The last is a regexp which, if matched at the beginning of a line,
1255 means don't indent that line."
1256 (interactive "r\np")
1257 (let (state)
1258 (save-excursion
1259 (goto-char end)
1260 (setq end (point-marker))
1261 (goto-char start)
1262 (or (bolp)
1263 (setq state (parse-partial-sexp (point)
1264 (progn
1265 (forward-line 1) (point))
1266 nil nil state)))
1267 (while (< (point) end)
1268 (or (car (nthcdr 3 state))
1269 (and nochange-regexp
1270 (looking-at nochange-regexp))
1271 ;; If line does not start in string, indent it
1272 (let ((indent (current-indentation)))
1273 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
1274 (or (eolp)
1275 (indent-to (max 0 (+ indent arg)) 0))))
1276 (setq state (parse-partial-sexp (point)
1277 (progn
1278 (forward-line 1) (point))
1279 nil nil state))))))
1280
1281 (provide 'lisp-mode)
1282
1283 ;; arch-tag: 414c7f93-c245-4b77-8ed5-ed05ef7ff1bf
1284 ;;; lisp-mode.el ends here