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