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