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