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