(eval-print-last-sexp, eval-last-sexp, eval-defun): Doc fixes.
[emacs.git] / lisp / emacs-lisp / lisp-mode.el
blobd5588f3811fb54fe54747e1004d89f5585cc7452
1 ;;; lisp-mode.el --- Lisp mode, and its idiosyncratic commands
3 ;; Copyright (C) 1985, 1986, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006 Free Software Foundation, Inc.
6 ;; Maintainer: FSF
7 ;; Keywords: lisp, languages
9 ;; This file is part of GNU Emacs.
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.
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.
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.
26 ;;; Commentary:
28 ;; The base major mode for editing Lisp code (used also for Emacs Lisp).
29 ;; This mode is documented in the Emacs manual.
31 ;;; Code:
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)
38 (defvar lisp-mode-abbrev-table nil)
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))
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))
89 (define-abbrev-table 'lisp-mode-abbrev-table ())
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_\\)+\\)"))
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_\\)+\\)"))
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_\\)+\\)"))
125 "Imenu generic expression for Lisp mode. See `imenu-generic-expression'.")
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)
155 (defvar lisp-doc-string-elt-property 'doc-string-elt
156 "The symbol property that holds the docstring position info.")
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.
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))
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))))
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)))
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.")
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.")
274 (if emacs-lisp-mode-map
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)))
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")))
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))))
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)
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)
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)
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)
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.")
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.
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)
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))))
407 ;; Used in old LispM code.
408 (defalias 'common-lisp-mode 'lisp-mode)
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"))
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.")
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'.
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.")
443 (defun eval-print-last-sexp ()
444 "Evaluate sexp before point; print value into current buffer.
446 If `eval-expression-debug-on-error' is non-nil, which is the default,
447 this command arranges for all errors to enter the debugger.
449 Note that printing the result is controlled by the variables
450 `eval-expression-print-length' and `eval-expression-print-level',
451 which see."
452 (interactive)
453 (let ((standard-output (current-buffer)))
454 (terpri)
455 (eval-last-sexp t)
456 (terpri)))
459 (defun last-sexp-setup-props (beg end value alt1 alt2)
460 "Set up text properties for the output of `eval-last-sexp-1'.
461 BEG and END are the start and end of the output in current-buffer.
462 VALUE is the Lisp value printed, ALT1 and ALT2 are strings for the
463 alternative printed representations that can be displayed."
464 (let ((map (make-sparse-keymap)))
465 (define-key map "\C-m" 'last-sexp-toggle-display)
466 (define-key map [down-mouse-2] 'mouse-set-point)
467 (define-key map [mouse-2] 'last-sexp-toggle-display)
468 (add-text-properties
469 beg end
470 `(printed-value (,value ,alt1 ,alt2)
471 mouse-face highlight
472 keymap ,map
473 help-echo "RET, mouse-2: toggle abbreviated display"
474 rear-nonsticky (mouse-face keymap help-echo
475 printed-value)))))
478 (defun last-sexp-toggle-display (&optional arg)
479 "Toggle between abbreviated and unabbreviated printed representations."
480 (interactive "P")
481 (save-restriction
482 (widen)
483 (let ((value (get-text-property (point) 'printed-value)))
484 (when value
485 (let ((beg (or (previous-single-property-change (min (point-max) (1+ (point)))
486 'printed-value)
487 (point)))
488 (end (or (next-single-char-property-change (point) 'printed-value) (point)))
489 (standard-output (current-buffer))
490 (point (point)))
491 (delete-region beg end)
492 (insert (nth 1 value))
493 (last-sexp-setup-props beg (point)
494 (nth 0 value)
495 (nth 2 value)
496 (nth 1 value))
497 (goto-char (min (point-max) point)))))))
499 (defun prin1-char (char)
500 "Return a string representing CHAR as a character rather than as an integer.
501 If CHAR is not a character, return nil."
502 (and (integerp char)
503 (eventp char)
504 (let ((c (event-basic-type char))
505 (mods (event-modifiers char))
506 string)
507 ;; Prevent ?A from turning into ?\S-a.
508 (if (and (memq 'shift mods)
509 (zerop (logand char ?\S-\^@))
510 (not (let ((case-fold-search nil))
511 (char-equal c (upcase c)))))
512 (setq c (upcase c) mods nil))
513 ;; What string are we considering using?
514 (condition-case nil
515 (setq string
516 (concat
518 (mapconcat
519 (lambda (modif)
520 (cond ((eq modif 'super) "\\s-")
521 (t (string ?\\ (upcase (aref (symbol-name modif) 0)) ?-))))
522 mods "")
523 (cond
524 ((memq c '(?\; ?\( ?\) ?\{ ?\} ?\[ ?\] ?\" ?\' ?\\)) (string ?\\ c))
525 ((eq c 127) "\\C-?")
527 (string c)))))
528 (error nil))
529 ;; Verify the string reads a CHAR, not to some other character.
530 ;; If it doesn't, return nil instead.
531 (and string
532 (= (car (read-from-string string)) char)
533 string))))
536 (defun eval-last-sexp-1 (eval-last-sexp-arg-internal)
537 "Evaluate sexp before point; print value in minibuffer.
538 With argument, print output into current buffer."
539 (let ((standard-output (if eval-last-sexp-arg-internal (current-buffer) t)))
540 (let ((value
541 (eval (let ((stab (syntax-table))
542 (opoint (point))
543 ignore-quotes
544 expr)
545 (save-excursion
546 (with-syntax-table emacs-lisp-mode-syntax-table
547 ;; If this sexp appears to be enclosed in `...'
548 ;; then ignore the surrounding quotes.
549 (setq ignore-quotes
550 (or (eq (following-char) ?\')
551 (eq (preceding-char) ?\')))
552 (forward-sexp -1)
553 ;; If we were after `?\e' (or similar case),
554 ;; use the whole thing, not just the `e'.
555 (when (eq (preceding-char) ?\\)
556 (forward-char -1)
557 (when (eq (preceding-char) ??)
558 (forward-char -1)))
560 ;; Skip over `#N='s.
561 (when (eq (preceding-char) ?=)
562 (let (labeled-p)
563 (save-excursion
564 (skip-chars-backward "0-9#=")
565 (setq labeled-p (looking-at "\\(#[0-9]+=\\)+")))
566 (when labeled-p
567 (forward-sexp -1))))
569 (save-restriction
570 ;; vladimir@cs.ualberta.ca 30-Jul-1997: skip ` in
571 ;; `variable' so that the value is returned, not the
572 ;; name
573 (if (and ignore-quotes
574 (eq (following-char) ?`))
575 (forward-char))
576 (narrow-to-region (point-min) opoint)
577 (setq expr (read (current-buffer)))
578 ;; If it's an (interactive ...) form, it's more
579 ;; useful to show how an interactive call would
580 ;; use it.
581 (and (consp expr)
582 (eq (car expr) 'interactive)
583 (setq expr
584 (list 'call-interactively
585 (list 'quote
586 (list 'lambda
587 '(&rest args)
588 expr
589 'args)))))
590 expr)))))))
591 (eval-last-sexp-print-value value))))
593 (defun eval-last-sexp-print-value (value)
594 (let ((unabbreviated (let ((print-length nil) (print-level nil))
595 (prin1-to-string value)))
596 (print-length eval-expression-print-length)
597 (print-level eval-expression-print-level)
598 (beg (point))
599 end)
600 (prog1
601 (prin1 value)
602 (let ((str (eval-expression-print-format value)))
603 (if str (princ str)))
604 (setq end (point))
605 (when (and (bufferp standard-output)
606 (or (not (null print-length))
607 (not (null print-level)))
608 (not (string= unabbreviated
609 (buffer-substring-no-properties beg end))))
610 (last-sexp-setup-props beg end value
611 unabbreviated
612 (buffer-substring-no-properties beg end))
613 ))))
616 (defvar eval-last-sexp-fake-value (make-symbol "t"))
618 (defun eval-last-sexp (eval-last-sexp-arg-internal)
619 "Evaluate sexp before point; print value in minibuffer.
620 Interactively, with prefix argument, print output into current buffer.
622 If `eval-expression-debug-on-error' is non-nil, which is the default,
623 this command arranges for all errors to enter the debugger."
624 (interactive "P")
625 (if (null eval-expression-debug-on-error)
626 (eval-last-sexp-1 eval-last-sexp-arg-internal)
627 (let ((old-value eval-last-sexp-fake-value) new-value value)
628 (let ((debug-on-error old-value))
629 (setq value (eval-last-sexp-1 eval-last-sexp-arg-internal))
630 (setq new-value debug-on-error))
631 (unless (eq old-value new-value)
632 (setq debug-on-error new-value))
633 value)))
635 (defun eval-defun-1 (form)
636 "Treat some expressions specially.
637 Reset the `defvar' and `defcustom' variables to the initial value.
638 Reinitialize the face according to the `defface' specification."
639 ;; The code in edebug-defun should be consistent with this, but not
640 ;; the same, since this gets a macroexpended form.
641 (cond ((not (listp form))
642 form)
643 ((and (eq (car form) 'defvar)
644 (cdr-safe (cdr-safe form))
645 (boundp (cadr form)))
646 ;; Force variable to be re-set.
647 `(progn (defvar ,(nth 1 form) nil ,@(nthcdr 3 form))
648 (setq-default ,(nth 1 form) ,(nth 2 form))))
649 ;; `defcustom' is now macroexpanded to
650 ;; `custom-declare-variable' with a quoted value arg.
651 ((and (eq (car form) 'custom-declare-variable)
652 (default-boundp (eval (nth 1 form))))
653 ;; Force variable to be bound.
654 (set-default (eval (nth 1 form)) (eval (nth 1 (nth 2 form))))
655 form)
656 ;; `defface' is macroexpanded to `custom-declare-face'.
657 ((eq (car form) 'custom-declare-face)
658 ;; Reset the face.
659 (setq face-new-frame-defaults
660 (assq-delete-all (eval (nth 1 form)) face-new-frame-defaults))
661 (put (eval (nth 1 form)) 'face-defface-spec nil)
662 ;; Setting `customized-face' to the new spec after calling
663 ;; the form, but preserving the old saved spec in `saved-face',
664 ;; imitates the situation when the new face spec is set
665 ;; temporarily for the current session in the customize
666 ;; buffer, thus allowing `face-user-default-spec' to use the
667 ;; new customized spec instead of the saved spec.
668 ;; Resetting `saved-face' temporarily to nil is needed to let
669 ;; `defface' change the spec, regardless of a saved spec.
670 (prog1 `(prog1 ,form
671 (put ,(nth 1 form) 'saved-face
672 ',(get (eval (nth 1 form)) 'saved-face))
673 (put ,(nth 1 form) 'customized-face
674 ,(nth 2 form)))
675 (put (eval (nth 1 form)) 'saved-face nil)))
676 ((eq (car form) 'progn)
677 (cons 'progn (mapcar 'eval-defun-1 (cdr form))))
678 (t form)))
680 (defun eval-defun-2 ()
681 "Evaluate defun that point is in or before.
682 The value is displayed in the minibuffer.
683 If the current defun is actually a call to `defvar',
684 then reset the variable using the initial value expression
685 even if the variable already has some other value.
686 \(Normally `defvar' does not change the variable's value
687 if it already has a value.\)
689 With argument, insert value in current buffer after the defun.
690 Return the result of evaluation."
691 (interactive "P")
692 (let ((debug-on-error eval-expression-debug-on-error)
693 (print-length eval-expression-print-length)
694 (print-level eval-expression-print-level))
695 (save-excursion
696 ;; Arrange for eval-region to "read" the (possibly) altered form.
697 ;; eval-region handles recording which file defines a function or
698 ;; variable. Re-written using `apply' to avoid capturing
699 ;; variables like `end'.
700 (apply
701 #'eval-region
702 (let ((standard-output t)
703 beg end form)
704 ;; Read the form from the buffer, and record where it ends.
705 (save-excursion
706 (end-of-defun)
707 (beginning-of-defun)
708 (setq beg (point))
709 (setq form (read (current-buffer)))
710 (setq end (point)))
711 ;; Alter the form if necessary.
712 (setq form (eval-defun-1 (macroexpand form)))
713 (list beg end standard-output
714 `(lambda (ignore)
715 ;; Skipping to the end of the specified region
716 ;; will make eval-region return.
717 (goto-char ,end)
718 ',form))))))
719 ;; The result of evaluation has been put onto VALUES. So return it.
720 (car values))
722 (defun eval-defun (edebug-it)
723 "Evaluate the top-level form containing point, or after point.
725 If the current defun is actually a call to `defvar' or `defcustom',
726 evaluating it this way resets the variable using its initial value
727 expression even if the variable already has some other value.
728 \(Normally `defvar' and `defcustom' do not alter the value if there
729 already is one.)
731 If `eval-expression-debug-on-error' is non-nil, which is the default,
732 this command arranges for all errors to enter the debugger.
734 With a prefix argument, instrument the code for Edebug.
736 If acting on a `defun' for FUNCTION, and the function was
737 instrumented, `Edebug: FUNCTION' is printed in the minibuffer. If not
738 instrumented, just FUNCTION is printed.
740 If not acting on a `defun', the result of evaluation is displayed in
741 the minibuffer. This display is controlled by the variables
742 `eval-expression-print-length' and `eval-expression-print-level',
743 which see."
744 (interactive "P")
745 (cond (edebug-it
746 (require 'edebug)
747 (eval-defun (not edebug-all-defs)))
749 (if (null eval-expression-debug-on-error)
750 (eval-defun-2)
751 (let ((old-value (make-symbol "t")) new-value value)
752 (let ((debug-on-error old-value))
753 (setq value (eval-defun-2))
754 (setq new-value debug-on-error))
755 (unless (eq old-value new-value)
756 (setq debug-on-error new-value))
757 value)))))
759 ;; May still be used by some external Lisp-mode variant.
760 (define-obsolete-function-alias 'lisp-comment-indent 'comment-indent-default)
762 ;; This function just forces a more costly detection of comments (using
763 ;; parse-partial-sexp from beginning-of-defun). I.e. It avoids the problem of
764 ;; taking a `;' inside a string started on another line for a comment starter.
765 ;; Note: `newcomment' gets it right now since we set comment-use-global-state
766 ;; so we could get rid of it. -stef
767 (defun lisp-mode-auto-fill ()
768 (if (> (current-column) (current-fill-column))
769 (if (save-excursion
770 (nth 4 (syntax-ppss (point))))
771 (do-auto-fill)
772 (unless (and (boundp 'comment-auto-fill-only-comments)
773 comment-auto-fill-only-comments)
774 (let ((comment-start nil) (comment-start-skip nil))
775 (do-auto-fill))))))
777 (defvar lisp-indent-offset nil
778 "If non-nil, indent second line of expressions that many more columns.")
779 (defvar lisp-indent-function 'lisp-indent-function)
781 (defun lisp-indent-line (&optional whole-exp)
782 "Indent current line as Lisp code.
783 With argument, indent any additional lines of the same expression
784 rigidly along with this one."
785 (interactive "P")
786 (let ((indent (calculate-lisp-indent)) shift-amt end
787 (pos (- (point-max) (point)))
788 (beg (progn (beginning-of-line) (point))))
789 (skip-chars-forward " \t")
790 (if (or (null indent) (looking-at "\\s<\\s<\\s<"))
791 ;; Don't alter indentation of a ;;; comment line
792 ;; or a line that starts in a string.
793 (goto-char (- (point-max) pos))
794 (if (and (looking-at "\\s<") (not (looking-at "\\s<\\s<")))
795 ;; Single-semicolon comment lines should be indented
796 ;; as comment lines, not as code.
797 (progn (indent-for-comment) (forward-char -1))
798 (if (listp indent) (setq indent (car indent)))
799 (setq shift-amt (- indent (current-column)))
800 (if (zerop shift-amt)
802 (delete-region beg (point))
803 (indent-to indent)))
804 ;; If initial point was within line's indentation,
805 ;; position after the indentation. Else stay at same point in text.
806 (if (> (- (point-max) pos) (point))
807 (goto-char (- (point-max) pos)))
808 ;; If desired, shift remaining lines of expression the same amount.
809 (and whole-exp (not (zerop shift-amt))
810 (save-excursion
811 (goto-char beg)
812 (forward-sexp 1)
813 (setq end (point))
814 (goto-char beg)
815 (forward-line 1)
816 (setq beg (point))
817 (> end beg))
818 (indent-code-rigidly beg end shift-amt)))))
820 (defvar calculate-lisp-indent-last-sexp)
822 (defun calculate-lisp-indent (&optional parse-start)
823 "Return appropriate indentation for current line as Lisp code.
824 In usual case returns an integer: the column to indent to.
825 If the value is nil, that means don't change the indentation
826 because the line starts inside a string.
828 The value can also be a list of the form (COLUMN CONTAINING-SEXP-START).
829 This means that following lines at the same level of indentation
830 should not necessarily be indented the same as this line.
831 Then COLUMN is the column to indent to, and CONTAINING-SEXP-START
832 is the buffer position of the start of the containing expression."
833 (save-excursion
834 (beginning-of-line)
835 (let ((indent-point (point))
836 state paren-depth
837 ;; setting this to a number inhibits calling hook
838 (desired-indent nil)
839 (retry t)
840 calculate-lisp-indent-last-sexp containing-sexp)
841 (if parse-start
842 (goto-char parse-start)
843 (beginning-of-defun))
844 ;; Find outermost containing sexp
845 (while (< (point) indent-point)
846 (setq state (parse-partial-sexp (point) indent-point 0)))
847 ;; Find innermost containing sexp
848 (while (and retry
849 state
850 (> (setq paren-depth (elt state 0)) 0))
851 (setq retry nil)
852 (setq calculate-lisp-indent-last-sexp (elt state 2))
853 (setq containing-sexp (elt state 1))
854 ;; Position following last unclosed open.
855 (goto-char (1+ containing-sexp))
856 ;; Is there a complete sexp since then?
857 (if (and calculate-lisp-indent-last-sexp
858 (> calculate-lisp-indent-last-sexp (point)))
859 ;; Yes, but is there a containing sexp after that?
860 (let ((peek (parse-partial-sexp calculate-lisp-indent-last-sexp
861 indent-point 0)))
862 (if (setq retry (car (cdr peek))) (setq state peek)))))
863 (if retry
865 ;; Innermost containing sexp found
866 (goto-char (1+ containing-sexp))
867 (if (not calculate-lisp-indent-last-sexp)
868 ;; indent-point immediately follows open paren.
869 ;; Don't call hook.
870 (setq desired-indent (current-column))
871 ;; Find the start of first element of containing sexp.
872 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
873 (cond ((looking-at "\\s(")
874 ;; First element of containing sexp is a list.
875 ;; Indent under that list.
877 ((> (save-excursion (forward-line 1) (point))
878 calculate-lisp-indent-last-sexp)
879 ;; This is the first line to start within the containing sexp.
880 ;; It's almost certainly a function call.
881 (if (= (point) calculate-lisp-indent-last-sexp)
882 ;; Containing sexp has nothing before this line
883 ;; except the first element. Indent under that element.
885 ;; Skip the first element, find start of second (the first
886 ;; argument of the function call) and indent under.
887 (progn (forward-sexp 1)
888 (parse-partial-sexp (point)
889 calculate-lisp-indent-last-sexp
890 0 t)))
891 (backward-prefix-chars))
893 ;; Indent beneath first sexp on same line as
894 ;; `calculate-lisp-indent-last-sexp'. Again, it's
895 ;; almost certainly a function call.
896 (goto-char calculate-lisp-indent-last-sexp)
897 (beginning-of-line)
898 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp
899 0 t)
900 (backward-prefix-chars)))))
901 ;; Point is at the point to indent under unless we are inside a string.
902 ;; Call indentation hook except when overridden by lisp-indent-offset
903 ;; or if the desired indentation has already been computed.
904 (let ((normal-indent (current-column)))
905 (cond ((elt state 3)
906 ;; Inside a string, don't change indentation.
907 nil)
908 ((and (integerp lisp-indent-offset) containing-sexp)
909 ;; Indent by constant offset
910 (goto-char containing-sexp)
911 (+ (current-column) lisp-indent-offset))
912 (desired-indent)
913 ((and (boundp 'lisp-indent-function)
914 lisp-indent-function
915 (not retry))
916 (or (funcall lisp-indent-function indent-point state)
917 normal-indent))
919 normal-indent))))))
921 (defun lisp-indent-function (indent-point state)
922 "This function is the normal value of the variable `lisp-indent-function'.
923 It is used when indenting a line within a function call, to see if the
924 called function says anything special about how to indent the line.
926 INDENT-POINT is the position where the user typed TAB, or equivalent.
927 Point is located at the point to indent under (for default indentation);
928 STATE is the `parse-partial-sexp' state for that position.
930 If the current line is in a call to a Lisp function
931 which has a non-nil property `lisp-indent-function',
932 that specifies how to do the indentation. The property value can be
933 * `defun', meaning indent `defun'-style;
934 * an integer N, meaning indent the first N arguments specially
935 like ordinary function arguments and then indent any further
936 arguments like a body;
937 * a function to call just as this function was called.
938 If that function returns nil, that means it doesn't specify
939 the indentation.
941 This function also returns nil meaning don't specify the indentation."
942 (let ((normal-indent (current-column)))
943 (goto-char (1+ (elt state 1)))
944 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
945 (if (and (elt state 2)
946 (not (looking-at "\\sw\\|\\s_")))
947 ;; car of form doesn't seem to be a symbol
948 (progn
949 (if (not (> (save-excursion (forward-line 1) (point))
950 calculate-lisp-indent-last-sexp))
951 (progn (goto-char calculate-lisp-indent-last-sexp)
952 (beginning-of-line)
953 (parse-partial-sexp (point)
954 calculate-lisp-indent-last-sexp 0 t)))
955 ;; Indent under the list or under the first sexp on the same
956 ;; line as calculate-lisp-indent-last-sexp. Note that first
957 ;; thing on that line has to be complete sexp since we are
958 ;; inside the innermost containing sexp.
959 (backward-prefix-chars)
960 (current-column))
961 (let ((function (buffer-substring (point)
962 (progn (forward-sexp 1) (point))))
963 method)
964 (setq method (or (get (intern-soft function) 'lisp-indent-function)
965 (get (intern-soft function) 'lisp-indent-hook)))
966 (cond ((or (eq method 'defun)
967 (and (null method)
968 (> (length function) 3)
969 (string-match "\\`def" function)))
970 (lisp-indent-defform state indent-point))
971 ((integerp method)
972 (lisp-indent-specform method state
973 indent-point normal-indent))
974 (method
975 (funcall method indent-point state)))))))
977 (defvar lisp-body-indent 2
978 "Number of columns to indent the second line of a `(def...)' form.")
980 (defun lisp-indent-specform (count state indent-point normal-indent)
981 (let ((containing-form-start (elt state 1))
982 (i count)
983 body-indent containing-form-column)
984 ;; Move to the start of containing form, calculate indentation
985 ;; to use for non-distinguished forms (> count), and move past the
986 ;; function symbol. lisp-indent-function guarantees that there is at
987 ;; least one word or symbol character following open paren of containing
988 ;; form.
989 (goto-char containing-form-start)
990 (setq containing-form-column (current-column))
991 (setq body-indent (+ lisp-body-indent containing-form-column))
992 (forward-char 1)
993 (forward-sexp 1)
994 ;; Now find the start of the last form.
995 (parse-partial-sexp (point) indent-point 1 t)
996 (while (and (< (point) indent-point)
997 (condition-case ()
998 (progn
999 (setq count (1- count))
1000 (forward-sexp 1)
1001 (parse-partial-sexp (point) indent-point 1 t))
1002 (error nil))))
1003 ;; Point is sitting on first character of last (or count) sexp.
1004 (if (> count 0)
1005 ;; A distinguished form. If it is the first or second form use double
1006 ;; lisp-body-indent, else normal indent. With lisp-body-indent bound
1007 ;; to 2 (the default), this just happens to work the same with if as
1008 ;; the older code, but it makes unwind-protect, condition-case,
1009 ;; with-output-to-temp-buffer, et. al. much more tasteful. The older,
1010 ;; less hacked, behavior can be obtained by replacing below with
1011 ;; (list normal-indent containing-form-start).
1012 (if (<= (- i count) 1)
1013 (list (+ containing-form-column (* 2 lisp-body-indent))
1014 containing-form-start)
1015 (list normal-indent containing-form-start))
1016 ;; A non-distinguished form. Use body-indent if there are no
1017 ;; distinguished forms and this is the first undistinguished form,
1018 ;; or if this is the first undistinguished form and the preceding
1019 ;; distinguished form has indentation at least as great as body-indent.
1020 (if (or (and (= i 0) (= count 0))
1021 (and (= count 0) (<= body-indent normal-indent)))
1022 body-indent
1023 normal-indent))))
1025 (defun lisp-indent-defform (state indent-point)
1026 (goto-char (car (cdr state)))
1027 (forward-line 1)
1028 (if (> (point) (car (cdr (cdr state))))
1029 (progn
1030 (goto-char (car (cdr state)))
1031 (+ lisp-body-indent (current-column)))))
1034 ;; (put 'progn 'lisp-indent-function 0), say, causes progn to be indented
1035 ;; like defun if the first form is placed on the next line, otherwise
1036 ;; it is indented like any other form (i.e. forms line up under first).
1038 (put 'lambda 'lisp-indent-function 'defun)
1039 (put 'autoload 'lisp-indent-function 'defun)
1040 (put 'progn 'lisp-indent-function 0)
1041 (put 'prog1 'lisp-indent-function 1)
1042 (put 'prog2 'lisp-indent-function 2)
1043 (put 'save-excursion 'lisp-indent-function 0)
1044 (put 'save-window-excursion 'lisp-indent-function 0)
1045 (put 'save-selected-window 'lisp-indent-function 0)
1046 (put 'save-restriction 'lisp-indent-function 0)
1047 (put 'save-match-data 'lisp-indent-function 0)
1048 (put 'save-current-buffer 'lisp-indent-function 0)
1049 (put 'with-current-buffer 'lisp-indent-function 1)
1050 (put 'combine-after-change-calls 'lisp-indent-function 0)
1051 (put 'with-output-to-string 'lisp-indent-function 0)
1052 (put 'with-temp-file 'lisp-indent-function 1)
1053 (put 'with-temp-buffer 'lisp-indent-function 0)
1054 (put 'with-temp-message 'lisp-indent-function 1)
1055 (put 'with-syntax-table 'lisp-indent-function 1)
1056 (put 'let 'lisp-indent-function 1)
1057 (put 'let* 'lisp-indent-function 1)
1058 (put 'while 'lisp-indent-function 1)
1059 (put 'if 'lisp-indent-function 2)
1060 (put 'read-if 'lisp-indent-function 2)
1061 (put 'catch 'lisp-indent-function 1)
1062 (put 'condition-case 'lisp-indent-function 2)
1063 (put 'unwind-protect 'lisp-indent-function 1)
1064 (put 'with-output-to-temp-buffer 'lisp-indent-function 1)
1065 (put 'eval-after-load 'lisp-indent-function 1)
1066 (put 'dolist 'lisp-indent-function 1)
1067 (put 'dotimes 'lisp-indent-function 1)
1068 (put 'when 'lisp-indent-function 1)
1069 (put 'unless 'lisp-indent-function 1)
1071 (defun indent-sexp (&optional endpos)
1072 "Indent each line of the list starting just after point.
1073 If optional arg ENDPOS is given, indent each line, stopping when
1074 ENDPOS is encountered."
1075 (interactive)
1076 (let ((indent-stack (list nil))
1077 (next-depth 0)
1078 ;; If ENDPOS is non-nil, use nil as STARTING-POINT
1079 ;; so that calculate-lisp-indent will find the beginning of
1080 ;; the defun we are in.
1081 ;; If ENDPOS is nil, it is safe not to scan before point
1082 ;; since every line we indent is more deeply nested than point is.
1083 (starting-point (if endpos nil (point)))
1084 (last-point (point))
1085 last-depth bol outer-loop-done inner-loop-done state this-indent)
1086 (or endpos
1087 ;; Get error now if we don't have a complete sexp after point.
1088 (save-excursion (forward-sexp 1)))
1089 (save-excursion
1090 (setq outer-loop-done nil)
1091 (while (if endpos (< (point) endpos)
1092 (not outer-loop-done))
1093 (setq last-depth next-depth
1094 inner-loop-done nil)
1095 ;; Parse this line so we can learn the state
1096 ;; to indent the next line.
1097 ;; This inner loop goes through only once
1098 ;; unless a line ends inside a string.
1099 (while (and (not inner-loop-done)
1100 (not (setq outer-loop-done (eobp))))
1101 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
1102 nil nil state))
1103 (setq next-depth (car state))
1104 ;; If the line contains a comment other than the sort
1105 ;; that is indented like code,
1106 ;; indent it now with indent-for-comment.
1107 ;; Comments indented like code are right already.
1108 ;; In any case clear the in-comment flag in the state
1109 ;; because parse-partial-sexp never sees the newlines.
1110 (if (car (nthcdr 4 state))
1111 (progn (indent-for-comment)
1112 (end-of-line)
1113 (setcar (nthcdr 4 state) nil)))
1114 ;; If this line ends inside a string,
1115 ;; go straight to next line, remaining within the inner loop,
1116 ;; and turn off the \-flag.
1117 (if (car (nthcdr 3 state))
1118 (progn
1119 (forward-line 1)
1120 (setcar (nthcdr 5 state) nil))
1121 (setq inner-loop-done t)))
1122 (and endpos
1123 (<= next-depth 0)
1124 (progn
1125 (setq indent-stack (nconc indent-stack
1126 (make-list (- next-depth) nil))
1127 last-depth (- last-depth next-depth)
1128 next-depth 0)))
1129 (or outer-loop-done endpos
1130 (setq outer-loop-done (<= next-depth 0)))
1131 (if outer-loop-done
1132 (forward-line 1)
1133 (while (> last-depth next-depth)
1134 (setq indent-stack (cdr indent-stack)
1135 last-depth (1- last-depth)))
1136 (while (< last-depth next-depth)
1137 (setq indent-stack (cons nil indent-stack)
1138 last-depth (1+ last-depth)))
1139 ;; Now go to the next line and indent it according
1140 ;; to what we learned from parsing the previous one.
1141 (forward-line 1)
1142 (setq bol (point))
1143 (skip-chars-forward " \t")
1144 ;; But not if the line is blank, or just a comment
1145 ;; (except for double-semi comments; indent them as usual).
1146 (if (or (eobp) (looking-at "\\s<\\|\n"))
1148 (if (and (car indent-stack)
1149 (>= (car indent-stack) 0))
1150 (setq this-indent (car indent-stack))
1151 (let ((val (calculate-lisp-indent
1152 (if (car indent-stack) (- (car indent-stack))
1153 starting-point))))
1154 (if (null val)
1155 (setq this-indent val)
1156 (if (integerp val)
1157 (setcar indent-stack
1158 (setq this-indent val))
1159 (setcar indent-stack (- (car (cdr val))))
1160 (setq this-indent (car val))))))
1161 (if (and this-indent (/= (current-column) this-indent))
1162 (progn (delete-region bol (point))
1163 (indent-to this-indent)))))
1164 (or outer-loop-done
1165 (setq outer-loop-done (= (point) last-point))
1166 (setq last-point (point)))))))
1168 (defun lisp-indent-region (start end)
1169 "Indent every line whose first char is between START and END inclusive."
1170 (save-excursion
1171 (let ((endmark (copy-marker end)))
1172 (goto-char start)
1173 (and (bolp) (not (eolp))
1174 (lisp-indent-line))
1175 (indent-sexp endmark)
1176 (set-marker endmark nil))))
1178 (defun indent-pp-sexp (&optional arg)
1179 "Indent each line of the list starting just after point, or prettyprint it.
1180 A prefix argument specifies pretty-printing."
1181 (interactive "P")
1182 (if arg
1183 (save-excursion
1184 (save-restriction
1185 (narrow-to-region (point) (progn (forward-sexp 1) (point)))
1186 (pp-buffer)
1187 (goto-char (point-max))
1188 (if (eq (char-before) ?\n)
1189 (delete-char -1)))))
1190 (indent-sexp))
1192 ;;;; Lisp paragraph filling commands.
1194 (defcustom emacs-lisp-docstring-fill-column 65
1195 "Value of `fill-column' to use when filling a docstring.
1196 Any non-integer value means do not use a different value of
1197 `fill-column' when filling docstrings."
1198 :type '(choice (integer)
1199 (const :tag "Use the current `fill-column'" t))
1200 :group 'lisp)
1202 (defun lisp-fill-paragraph (&optional justify)
1203 "Like \\[fill-paragraph], but handle Emacs Lisp comments and docstrings.
1204 If any of the current line is a comment, fill the comment or the
1205 paragraph of it that point is in, preserving the comment's indentation
1206 and initial semicolons."
1207 (interactive "P")
1208 (or (fill-comment-paragraph justify)
1209 ;; Since fill-comment-paragraph returned nil, that means we're not in
1210 ;; a comment: Point is on a program line; we are interested
1211 ;; particularly in docstring lines.
1213 ;; We bind `paragraph-start' and `paragraph-separate' temporarily. They
1214 ;; are buffer-local, but we avoid changing them so that they can be set
1215 ;; to make `forward-paragraph' and friends do something the user wants.
1217 ;; `paragraph-start': The `(' in the character alternative and the
1218 ;; left-singlequote plus `(' sequence after the \\| alternative prevent
1219 ;; sexps and backquoted sexps that follow a docstring from being filled
1220 ;; with the docstring. This setting has the consequence of inhibiting
1221 ;; filling many program lines that are not docstrings, which is sensible,
1222 ;; because the user probably asked to fill program lines by accident, or
1223 ;; expecting indentation (perhaps we should try to do indenting in that
1224 ;; case). The `;' and `:' stop the paragraph being filled at following
1225 ;; comment lines and at keywords (e.g., in `defcustom'). Left parens are
1226 ;; escaped to keep font-locking, filling, & paren matching in the source
1227 ;; file happy.
1229 ;; `paragraph-separate': A clever regexp distinguishes the first line of
1230 ;; a docstring and identifies it as a paragraph separator, so that it
1231 ;; won't be filled. (Since the first line of documentation stands alone
1232 ;; in some contexts, filling should not alter the contents the author has
1233 ;; chosen.) Only the first line of a docstring begins with whitespace
1234 ;; and a quotation mark and ends with a period or (rarely) a comma.
1236 ;; The `fill-column' is temporarily bound to
1237 ;; `emacs-lisp-docstring-fill-column' if that value is an integer.
1238 (let ((paragraph-start (concat paragraph-start
1239 "\\|\\s-*\\([(;:\"]\\|`(\\|#'(\\)"))
1240 (paragraph-separate
1241 (concat paragraph-separate "\\|\\s-*\".*[,\\.]$"))
1242 (fill-column (if (integerp emacs-lisp-docstring-fill-column)
1243 emacs-lisp-docstring-fill-column
1244 fill-column)))
1245 (fill-paragraph justify))
1246 ;; Never return nil.
1249 (defun indent-code-rigidly (start end arg &optional nochange-regexp)
1250 "Indent all lines of code, starting in the region, sideways by ARG columns.
1251 Does not affect lines starting inside comments or strings, assuming that
1252 the start of the region is not inside them.
1254 Called from a program, takes args START, END, COLUMNS and NOCHANGE-REGEXP.
1255 The last is a regexp which, if matched at the beginning of a line,
1256 means don't indent that line."
1257 (interactive "r\np")
1258 (let (state)
1259 (save-excursion
1260 (goto-char end)
1261 (setq end (point-marker))
1262 (goto-char start)
1263 (or (bolp)
1264 (setq state (parse-partial-sexp (point)
1265 (progn
1266 (forward-line 1) (point))
1267 nil nil state)))
1268 (while (< (point) end)
1269 (or (car (nthcdr 3 state))
1270 (and nochange-regexp
1271 (looking-at nochange-regexp))
1272 ;; If line does not start in string, indent it
1273 (let ((indent (current-indentation)))
1274 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
1275 (or (eolp)
1276 (indent-to (max 0 (+ indent arg)) 0))))
1277 (setq state (parse-partial-sexp (point)
1278 (progn
1279 (forward-line 1) (point))
1280 nil nil state))))))
1282 (provide 'lisp-mode)
1284 ;; arch-tag: 414c7f93-c245-4b77-8ed5-ed05ef7ff1bf
1285 ;;; lisp-mode.el ends here