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