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