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