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