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