(PC-do-completion): Do not forget to use `pred' as the default-directory
[emacs.git] / lisp / emacs-lisp / lisp-mode.el
blob04d00a2bdb5791a04e55e71ae7cc78e2d829547c
1 ;;; lisp-mode.el --- Lisp mode, and its idiosyncratic commands
3 ;; Copyright (C) 1985, 1986, 1999, 2000, 2001, 2003 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
6 ;; Keywords: lisp, languages
8 ;; This file is part of GNU Emacs.
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.
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.
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.
25 ;;; Commentary:
27 ;; The base major mode for editing Lisp code (used also for Emacs Lisp).
28 ;; This mode is documented in the Emacs manual.
30 ;;; Code:
32 (defvar lisp-mode-abbrev-table nil)
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))
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))
82 (define-abbrev-table 'lisp-mode-abbrev-table ())
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_\\)+\\)"))
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_\\)+\\)"))
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_\\)+\\)"))
117 "Imenu generic expression for Lisp mode. See `imenu-generic-expression'.")
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)
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))
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))))
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))))
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.")
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.")
237 (if emacs-lisp-mode-map
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)))
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")))
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))))
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)
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)
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)
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)
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.")
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.
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))
361 ;; Used in old LispM code.
362 (defalias 'common-lisp-mode 'lisp-mode)
364 ;; This will do unless inf-lisp.el is loaded.
365 (defun lisp-eval-defun (&optional and-go)
366 "Send the current defun to the Lisp process made by \\[run-lisp]."
367 (interactive)
368 (error "Process lisp does not exist"))
370 (defvar lisp-interaction-mode-map
371 (let ((map (make-sparse-keymap)))
372 (set-keymap-parent map lisp-mode-shared-map)
373 (define-key map "\e\C-x" 'eval-defun)
374 (define-key map "\e\t" 'lisp-complete-symbol)
375 (define-key map "\n" 'eval-print-last-sexp)
376 map)
377 "Keymap for Lisp Interaction mode.
378 All commands in `lisp-mode-shared-map' are inherited by this map.")
380 (defvar lisp-interaction-mode-abbrev-table lisp-mode-abbrev-table)
381 (define-derived-mode lisp-interaction-mode emacs-lisp-mode "Lisp Interaction"
382 "Major mode for typing and evaluating Lisp forms.
383 Like Lisp mode except that \\[eval-print-last-sexp] evals the Lisp expression
384 before point, and prints its value into the buffer, advancing point.
385 Note that printing is controlled by `eval-expression-print-length'
386 and `eval-expression-print-level'.
388 Commands:
389 Delete converts tabs to spaces as it moves back.
390 Paragraphs are separated only by blank lines.
391 Semicolons start comments.
392 \\{lisp-interaction-mode-map}
393 Entry to this mode calls the value of `lisp-interaction-mode-hook'
394 if that value is non-nil.")
396 (defun eval-print-last-sexp ()
397 "Evaluate sexp before point; print value into current buffer.
399 Note that printing the result is controlled by the variables
400 `eval-expression-print-length' and `eval-expression-print-level',
401 which see."
402 (interactive)
403 (let ((standard-output (current-buffer)))
404 (terpri)
405 (eval-last-sexp t)
406 (terpri)))
409 (defun last-sexp-setup-props (beg end value alt1 alt2)
410 "Set up text properties for the output of `eval-last-sexp-1'.
411 BEG and END are the start and end of the output in current-buffer.
412 VALUE is the Lisp value printed, ALT1 and ALT2 are strings for the
413 alternative printed representations that can be displayed."
414 (let ((map (make-sparse-keymap)))
415 (define-key map "\C-m" 'last-sexp-toggle-display)
416 (define-key map [down-mouse-2] 'mouse-set-point)
417 (define-key map [mouse-2] 'last-sexp-toggle-display)
418 (add-text-properties
419 beg end
420 `(printed-value (,value ,alt1 ,alt2)
421 mouse-face highlight
422 keymap ,map
423 help-echo "RET, mouse-2: toggle abbreviated display"
424 rear-nonsticky (mouse-face keymap help-echo
425 printed-value)))))
428 (defun last-sexp-toggle-display (&optional arg)
429 "Toggle between abbreviated and unabbreviated printed representations."
430 (interactive "P")
431 (save-restriction
432 (widen)
433 (let ((value (get-text-property (point) 'printed-value)))
434 (when value
435 (let ((beg (or (previous-single-property-change (min (point-max) (1+ (point)))
436 'printed-value)
437 (point)))
438 (end (or (next-single-char-property-change (point) 'printed-value) (point)))
439 (standard-output (current-buffer))
440 (point (point)))
441 (delete-region beg end)
442 (insert (nth 1 value))
443 (last-sexp-setup-props beg (point)
444 (nth 0 value)
445 (nth 2 value)
446 (nth 1 value))
447 (goto-char (min (point-max) point)))))))
449 (defun prin1-char (char)
450 "Return a string representing CHAR as a character rather than as an integer.
451 If CHAR is not a character, return nil."
452 (and (integerp char)
453 (char-valid-p (event-basic-type char))
454 (concat
456 (mapconcat
457 (lambda (modif)
458 (cond ((eq modif 'super) "\\s-")
459 (t (string ?\\ (upcase (aref (symbol-name modif) 0)) ?-))))
460 (event-modifiers char) "")
461 (string (event-basic-type char)))))
463 (defun eval-last-sexp-1 (eval-last-sexp-arg-internal)
464 "Evaluate sexp before point; print value in minibuffer.
465 With argument, print output into current buffer."
466 (let ((standard-output (if eval-last-sexp-arg-internal (current-buffer) t)))
467 (let ((value
468 (eval (let ((stab (syntax-table))
469 (opoint (point))
470 ignore-quotes
471 expr)
472 (save-excursion
473 (with-syntax-table emacs-lisp-mode-syntax-table
474 ;; If this sexp appears to be enclosed in `...'
475 ;; then ignore the surrounding quotes.
476 (setq ignore-quotes
477 (or (eq (following-char) ?\')
478 (eq (preceding-char) ?\')))
479 (forward-sexp -1)
480 ;; If we were after `?\e' (or similar case),
481 ;; use the whole thing, not just the `e'.
482 (when (eq (preceding-char) ?\\)
483 (forward-char -1)
484 (when (eq (preceding-char) ??)
485 (forward-char -1)))
487 ;; Skip over `#N='s.
488 (when (eq (preceding-char) ?=)
489 (let (labeled-p)
490 (save-excursion
491 (skip-chars-backward "0-9#=")
492 (setq labeled-p (looking-at "\\(#[0-9]+=\\)+")))
493 (when labeled-p
494 (forward-sexp -1))))
496 (save-restriction
497 ;; vladimir@cs.ualberta.ca 30-Jul-1997: skip ` in
498 ;; `variable' so that the value is returned, not the
499 ;; name
500 (if (and ignore-quotes
501 (eq (following-char) ?`))
502 (forward-char))
503 (narrow-to-region (point-min) opoint)
504 (setq expr (read (current-buffer)))
505 ;; If it's an (interactive ...) form, it's more
506 ;; useful to show how an interactive call would
507 ;; use it.
508 (and (consp expr)
509 (eq (car expr) 'interactive)
510 (setq expr
511 (list 'call-interactively
512 (list 'quote
513 (list 'lambda
514 '(&rest args)
515 expr
516 'args)))))
517 expr)))))))
518 (eval-last-sexp-print-value value))))
520 (defun eval-last-sexp-print-value (value)
521 (let ((unabbreviated (let ((print-length nil) (print-level nil))
522 (prin1-to-string value)))
523 (print-length eval-expression-print-length)
524 (print-level eval-expression-print-level)
525 (char-string (prin1-char value))
526 (beg (point))
527 end)
528 (prog1
529 (prin1 value)
530 (if (and (eq standard-output t) char-string)
531 (princ (concat " = " char-string)))
532 (setq end (point))
533 (when (and (bufferp standard-output)
534 (or (not (null print-length))
535 (not (null print-level)))
536 (not (string= unabbreviated
537 (buffer-substring-no-properties beg end))))
538 (last-sexp-setup-props beg end value
539 unabbreviated
540 (buffer-substring-no-properties beg end))
541 ))))
544 (defun eval-last-sexp (eval-last-sexp-arg-internal)
545 "Evaluate sexp before point; print value in minibuffer.
546 Interactively, with prefix argument, print output into current buffer."
547 (interactive "P")
548 (if (null eval-expression-debug-on-error)
549 (eval-last-sexp-1 eval-last-sexp-arg-internal)
550 (let ((old-value (make-symbol "t")) new-value value)
551 (let ((debug-on-error old-value))
552 (setq value (eval-last-sexp-1 eval-last-sexp-arg-internal))
553 (setq new-value debug-on-error))
554 (unless (eq old-value new-value)
555 (setq debug-on-error new-value))
556 value)))
558 (defun eval-defun-1 (form)
559 "Change defvar into defconst within FORM.
560 Likewise for other constructs as necessary."
561 ;; The code in edebug-defun should be consistent with this, but not
562 ;; the same, since this gets a macroexpended form.
563 (cond ((not (listp form))
564 form)
565 ((and (eq (car form) 'defvar)
566 (cdr-safe (cdr-safe form))
567 (boundp (cadr form)))
568 ;; Force variable to be re-set.
569 `(progn (defvar ,(nth 1 form) nil ,@(nthcdr 3 form))
570 (setq ,(nth 1 form) ,(nth 2 form))))
571 ;; `defcustom' is now macroexpanded to
572 ;; `custom-declare-variable' with a quoted value arg.
573 ((and (eq (car form) 'custom-declare-variable)
574 (default-boundp (eval (nth 1 form))))
575 ;; Force variable to be bound.
576 (set-default (eval (nth 1 form)) (eval (nth 1 (nth 2 form))))
577 form)
578 ((eq (car form) 'progn)
579 (cons 'progn (mapcar 'eval-defun-1 (cdr form))))
580 (t form)))
582 (defun eval-defun-2 ()
583 "Evaluate defun that point is in or before.
584 The value is displayed in the minibuffer.
585 If the current defun is actually a call to `defvar',
586 then reset the variable using the initial value expression
587 even if the variable already has some other value.
588 \(Normally `defvar' does not change the variable's value
589 if it already has a value.\)
591 With argument, insert value in current buffer after the defun.
592 Return the result of evaluation."
593 (interactive "P")
594 (let ((debug-on-error eval-expression-debug-on-error)
595 (print-length eval-expression-print-length)
596 (print-level eval-expression-print-level))
597 (save-excursion
598 ;; Arrange for eval-region to "read" the (possibly) altered form.
599 ;; eval-region handles recording which file defines a function or
600 ;; variable. Re-written using `apply' to avoid capturing
601 ;; variables like `end'.
602 (apply
603 #'eval-region
604 (let ((standard-output t)
605 beg end form)
606 ;; Read the form from the buffer, and record where it ends.
607 (save-excursion
608 (end-of-defun)
609 (beginning-of-defun)
610 (setq beg (point))
611 (setq form (read (current-buffer)))
612 (setq end (point)))
613 ;; Alter the form if necessary, changing defvar into defconst, etc.
614 (setq form (eval-defun-1 (macroexpand form)))
615 (list beg end standard-output
616 `(lambda (ignore)
617 ;; Skipping to the end of the specified region
618 ;; will make eval-region return.
619 (goto-char ,end)
620 ',form))))))
621 ;; The result of evaluation has been put onto VALUES. So return it.
622 (car values))
624 (defun eval-defun (edebug-it)
625 "Evaluate the top-level form containing point, or after point.
627 If the current defun is actually a call to `defvar' or `defcustom',
628 evaluating it this way resets the variable using its initial value
629 expression even if the variable already has some other value.
630 \(Normally `defvar' and `defcustom' do not alter the value if there
631 already is one.)
633 With a prefix argument, instrument the code for Edebug.
635 If acting on a `defun' for FUNCTION, and the function was
636 instrumented, `Edebug: FUNCTION' is printed in the minibuffer. If not
637 instrumented, just FUNCTION is printed.
639 If not acting on a `defun', the result of evaluation is displayed in
640 the minibuffer. This display is controlled by the variables
641 `eval-expression-print-length' and `eval-expression-print-level',
642 which see."
643 (interactive "P")
644 (cond (edebug-it
645 (require 'edebug)
646 (eval-defun (not edebug-all-defs)))
648 (if (null eval-expression-debug-on-error)
649 (eval-defun-2)
650 (let ((old-value (make-symbol "t")) new-value value)
651 (let ((debug-on-error old-value))
652 (setq value (eval-defun-2))
653 (setq new-value debug-on-error))
654 (unless (eq old-value new-value)
655 (setq debug-on-error new-value))
656 value)))))
659 (defun lisp-comment-indent ()
660 (if (looking-at "\\s<\\s<\\s<")
661 (current-column)
662 (if (looking-at "\\s<\\s<")
663 (let ((tem (or (calculate-lisp-indent) (current-column))))
664 (if (listp tem) (car tem) tem))
665 (skip-chars-backward " \t")
666 (max (if (bolp) 0 (1+ (current-column)))
667 comment-column))))
669 ;; This function just forces a more costly detection of comments (using
670 ;; parse-partial-sexp from beginning-of-defun). I.e. It avoids the problem of
671 ;; taking a `;' inside a string started on another line for a comment starter.
672 ;; Note: `newcomment' gets it right in 99% of the cases if you're using
673 ;; font-lock, anyway, so we could get rid of it. -stef
674 (defun lisp-mode-auto-fill ()
675 (if (> (current-column) (current-fill-column))
676 (if (save-excursion
677 (nth 4 (syntax-ppss (point))))
678 (do-auto-fill)
679 (unless (and (boundp 'comment-auto-fill-only-comments)
680 comment-auto-fill-only-comments)
681 (let ((comment-start nil) (comment-start-skip nil))
682 (do-auto-fill))))))
684 (defvar lisp-indent-offset nil
685 "If non-nil, indent second line of expressions that many more columns.")
686 (defvar lisp-indent-function 'lisp-indent-function)
688 (defun lisp-indent-line (&optional whole-exp)
689 "Indent current line as Lisp code.
690 With argument, indent any additional lines of the same expression
691 rigidly along with this one."
692 (interactive "P")
693 (let ((indent (calculate-lisp-indent)) shift-amt end
694 (pos (- (point-max) (point)))
695 (beg (progn (beginning-of-line) (point))))
696 (skip-chars-forward " \t")
697 (if (or (null indent) (looking-at "\\s<\\s<\\s<"))
698 ;; Don't alter indentation of a ;;; comment line
699 ;; or a line that starts in a string.
700 (goto-char (- (point-max) pos))
701 (if (and (looking-at "\\s<") (not (looking-at "\\s<\\s<")))
702 ;; Single-semicolon comment lines should be indented
703 ;; as comment lines, not as code.
704 (progn (indent-for-comment) (forward-char -1))
705 (if (listp indent) (setq indent (car indent)))
706 (setq shift-amt (- indent (current-column)))
707 (if (zerop shift-amt)
709 (delete-region beg (point))
710 (indent-to indent)))
711 ;; If initial point was within line's indentation,
712 ;; position after the indentation. Else stay at same point in text.
713 (if (> (- (point-max) pos) (point))
714 (goto-char (- (point-max) pos)))
715 ;; If desired, shift remaining lines of expression the same amount.
716 (and whole-exp (not (zerop shift-amt))
717 (save-excursion
718 (goto-char beg)
719 (forward-sexp 1)
720 (setq end (point))
721 (goto-char beg)
722 (forward-line 1)
723 (setq beg (point))
724 (> end beg))
725 (indent-code-rigidly beg end shift-amt)))))
727 (defvar calculate-lisp-indent-last-sexp)
729 (defun calculate-lisp-indent (&optional parse-start)
730 "Return appropriate indentation for current line as Lisp code.
731 In usual case returns an integer: the column to indent to.
732 If the value is nil, that means don't change the indentation
733 because the line starts inside a string.
735 The value can also be a list of the form (COLUMN CONTAINING-SEXP-START).
736 This means that following lines at the same level of indentation
737 should not necessarily be indented the same as this line.
738 Then COLUMN is the column to indent to, and CONTAINING-SEXP-START
739 is the buffer position of the start of the containing expression."
740 (save-excursion
741 (beginning-of-line)
742 (let ((indent-point (point))
743 state paren-depth
744 ;; setting this to a number inhibits calling hook
745 (desired-indent nil)
746 (retry t)
747 calculate-lisp-indent-last-sexp containing-sexp)
748 (if parse-start
749 (goto-char parse-start)
750 (beginning-of-defun))
751 ;; Find outermost containing sexp
752 (while (< (point) indent-point)
753 (setq state (parse-partial-sexp (point) indent-point 0)))
754 ;; Find innermost containing sexp
755 (while (and retry
756 state
757 (> (setq paren-depth (elt state 0)) 0))
758 (setq retry nil)
759 (setq calculate-lisp-indent-last-sexp (elt state 2))
760 (setq containing-sexp (elt state 1))
761 ;; Position following last unclosed open.
762 (goto-char (1+ containing-sexp))
763 ;; Is there a complete sexp since then?
764 (if (and calculate-lisp-indent-last-sexp
765 (> calculate-lisp-indent-last-sexp (point)))
766 ;; Yes, but is there a containing sexp after that?
767 (let ((peek (parse-partial-sexp calculate-lisp-indent-last-sexp
768 indent-point 0)))
769 (if (setq retry (car (cdr peek))) (setq state peek)))))
770 (if retry
772 ;; Innermost containing sexp found
773 (goto-char (1+ containing-sexp))
774 (if (not calculate-lisp-indent-last-sexp)
775 ;; indent-point immediately follows open paren.
776 ;; Don't call hook.
777 (setq desired-indent (current-column))
778 ;; Find the start of first element of containing sexp.
779 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
780 (cond ((looking-at "\\s(")
781 ;; First element of containing sexp is a list.
782 ;; Indent under that list.
784 ((> (save-excursion (forward-line 1) (point))
785 calculate-lisp-indent-last-sexp)
786 ;; This is the first line to start within the containing sexp.
787 ;; It's almost certainly a function call.
788 (if (= (point) calculate-lisp-indent-last-sexp)
789 ;; Containing sexp has nothing before this line
790 ;; except the first element. Indent under that element.
792 ;; Skip the first element, find start of second (the first
793 ;; argument of the function call) and indent under.
794 (progn (forward-sexp 1)
795 (parse-partial-sexp (point)
796 calculate-lisp-indent-last-sexp
797 0 t)))
798 (backward-prefix-chars))
800 ;; Indent beneath first sexp on same line as
801 ;; `calculate-lisp-indent-last-sexp'. Again, it's
802 ;; almost certainly a function call.
803 (goto-char calculate-lisp-indent-last-sexp)
804 (beginning-of-line)
805 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp
806 0 t)
807 (backward-prefix-chars)))))
808 ;; Point is at the point to indent under unless we are inside a string.
809 ;; Call indentation hook except when overridden by lisp-indent-offset
810 ;; or if the desired indentation has already been computed.
811 (let ((normal-indent (current-column)))
812 (cond ((elt state 3)
813 ;; Inside a string, don't change indentation.
814 nil)
815 ((and (integerp lisp-indent-offset) containing-sexp)
816 ;; Indent by constant offset
817 (goto-char containing-sexp)
818 (+ (current-column) lisp-indent-offset))
819 (desired-indent)
820 ((and (boundp 'lisp-indent-function)
821 lisp-indent-function
822 (not retry))
823 (or (funcall lisp-indent-function indent-point state)
824 normal-indent))
826 normal-indent))))))
828 (defun lisp-indent-function (indent-point state)
829 "This function is the normal value of the variable `lisp-indent-function'.
830 It is used when indenting a line within a function call, to see if the
831 called function says anything special about how to indent the line.
833 INDENT-POINT is the position where the user typed TAB, or equivalent.
834 Point is located at the point to indent under (for default indentation);
835 STATE is the `parse-partial-sexp' state for that position.
837 If the current line is in a call to a Lisp function
838 which has a non-nil property `lisp-indent-function',
839 that specifies how to do the indentation. The property value can be
840 * `defun', meaning indent `defun'-style;
841 * an integer N, meaning indent the first N arguments specially
842 like ordinary function arguments and then indent any further
843 aruments like a body;
844 * a function to call just as this function was called.
845 If that function returns nil, that means it doesn't specify
846 the indentation.
848 This function also returns nil meaning don't specify the indentation."
849 (let ((normal-indent (current-column)))
850 (goto-char (1+ (elt state 1)))
851 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
852 (if (and (elt state 2)
853 (not (looking-at "\\sw\\|\\s_")))
854 ;; car of form doesn't seem to be a symbol
855 (progn
856 (if (not (> (save-excursion (forward-line 1) (point))
857 calculate-lisp-indent-last-sexp))
858 (progn (goto-char calculate-lisp-indent-last-sexp)
859 (beginning-of-line)
860 (parse-partial-sexp (point)
861 calculate-lisp-indent-last-sexp 0 t)))
862 ;; Indent under the list or under the first sexp on the same
863 ;; line as calculate-lisp-indent-last-sexp. Note that first
864 ;; thing on that line has to be complete sexp since we are
865 ;; inside the innermost containing sexp.
866 (backward-prefix-chars)
867 (current-column))
868 (let ((function (buffer-substring (point)
869 (progn (forward-sexp 1) (point))))
870 method)
871 (setq method (or (get (intern-soft function) 'lisp-indent-function)
872 (get (intern-soft function) 'lisp-indent-hook)))
873 (cond ((or (eq method 'defun)
874 (and (null method)
875 (> (length function) 3)
876 (string-match "\\`def" function)))
877 (lisp-indent-defform state indent-point))
878 ((integerp method)
879 (lisp-indent-specform method state
880 indent-point normal-indent))
881 (method
882 (funcall method state indent-point)))))))
884 (defvar lisp-body-indent 2
885 "Number of columns to indent the second line of a `(def...)' form.")
887 (defun lisp-indent-specform (count state indent-point normal-indent)
888 (let ((containing-form-start (elt state 1))
889 (i count)
890 body-indent containing-form-column)
891 ;; Move to the start of containing form, calculate indentation
892 ;; to use for non-distinguished forms (> count), and move past the
893 ;; function symbol. lisp-indent-function guarantees that there is at
894 ;; least one word or symbol character following open paren of containing
895 ;; form.
896 (goto-char containing-form-start)
897 (setq containing-form-column (current-column))
898 (setq body-indent (+ lisp-body-indent containing-form-column))
899 (forward-char 1)
900 (forward-sexp 1)
901 ;; Now find the start of the last form.
902 (parse-partial-sexp (point) indent-point 1 t)
903 (while (and (< (point) indent-point)
904 (condition-case ()
905 (progn
906 (setq count (1- count))
907 (forward-sexp 1)
908 (parse-partial-sexp (point) indent-point 1 t))
909 (error nil))))
910 ;; Point is sitting on first character of last (or count) sexp.
911 (if (> count 0)
912 ;; A distinguished form. If it is the first or second form use double
913 ;; lisp-body-indent, else normal indent. With lisp-body-indent bound
914 ;; to 2 (the default), this just happens to work the same with if as
915 ;; the older code, but it makes unwind-protect, condition-case,
916 ;; with-output-to-temp-buffer, et. al. much more tasteful. The older,
917 ;; less hacked, behavior can be obtained by replacing below with
918 ;; (list normal-indent containing-form-start).
919 (if (<= (- i count) 1)
920 (list (+ containing-form-column (* 2 lisp-body-indent))
921 containing-form-start)
922 (list normal-indent containing-form-start))
923 ;; A non-distinguished form. Use body-indent if there are no
924 ;; distinguished forms and this is the first undistinguished form,
925 ;; or if this is the first undistinguished form and the preceding
926 ;; distinguished form has indentation at least as great as body-indent.
927 (if (or (and (= i 0) (= count 0))
928 (and (= count 0) (<= body-indent normal-indent)))
929 body-indent
930 normal-indent))))
932 (defun lisp-indent-defform (state indent-point)
933 (goto-char (car (cdr state)))
934 (forward-line 1)
935 (if (> (point) (car (cdr (cdr state))))
936 (progn
937 (goto-char (car (cdr state)))
938 (+ lisp-body-indent (current-column)))))
941 ;; (put 'progn 'lisp-indent-function 0), say, causes progn to be indented
942 ;; like defun if the first form is placed on the next line, otherwise
943 ;; it is indented like any other form (i.e. forms line up under first).
945 (put 'lambda 'lisp-indent-function 'defun)
946 (put 'autoload 'lisp-indent-function 'defun)
947 (put 'progn 'lisp-indent-function 0)
948 (put 'prog1 'lisp-indent-function 1)
949 (put 'prog2 'lisp-indent-function 2)
950 (put 'save-excursion 'lisp-indent-function 0)
951 (put 'save-window-excursion 'lisp-indent-function 0)
952 (put 'save-selected-window 'lisp-indent-function 0)
953 (put 'save-restriction 'lisp-indent-function 0)
954 (put 'save-match-data 'lisp-indent-function 0)
955 (put 'save-current-buffer 'lisp-indent-function 0)
956 (put 'with-current-buffer 'lisp-indent-function 1)
957 (put 'combine-after-change-calls 'lisp-indent-function 0)
958 (put 'with-output-to-string 'lisp-indent-function 0)
959 (put 'with-temp-file 'lisp-indent-function 1)
960 (put 'with-temp-buffer 'lisp-indent-function 0)
961 (put 'with-temp-message 'lisp-indent-function 1)
962 (put 'with-syntax-table 'lisp-indent-function 1)
963 (put 'let 'lisp-indent-function 1)
964 (put 'let* 'lisp-indent-function 1)
965 (put 'while 'lisp-indent-function 1)
966 (put 'if 'lisp-indent-function 2)
967 (put 'read-if 'lisp-indent-function 2)
968 (put 'catch 'lisp-indent-function 1)
969 (put 'condition-case 'lisp-indent-function 2)
970 (put 'unwind-protect 'lisp-indent-function 1)
971 (put 'with-output-to-temp-buffer 'lisp-indent-function 1)
972 (put 'eval-after-load 'lisp-indent-function 1)
973 (put 'dolist 'lisp-indent-function 1)
974 (put 'dotimes 'lisp-indent-function 1)
975 (put 'when 'lisp-indent-function 1)
976 (put 'unless 'lisp-indent-function 1)
978 (defun indent-sexp (&optional endpos)
979 "Indent each line of the list starting just after point.
980 If optional arg ENDPOS is given, indent each line, stopping when
981 ENDPOS is encountered."
982 (interactive)
983 (let ((indent-stack (list nil))
984 (next-depth 0)
985 ;; If ENDPOS is non-nil, use nil as STARTING-POINT
986 ;; so that calculate-lisp-indent will find the beginning of
987 ;; the defun we are in.
988 ;; If ENDPOS is nil, it is safe not to scan before point
989 ;; since every line we indent is more deeply nested than point is.
990 (starting-point (if endpos nil (point)))
991 (last-point (point))
992 last-depth bol outer-loop-done inner-loop-done state this-indent)
993 (or endpos
994 ;; Get error now if we don't have a complete sexp after point.
995 (save-excursion (forward-sexp 1)))
996 (save-excursion
997 (setq outer-loop-done nil)
998 (while (if endpos (< (point) endpos)
999 (not outer-loop-done))
1000 (setq last-depth next-depth
1001 inner-loop-done nil)
1002 ;; Parse this line so we can learn the state
1003 ;; to indent the next line.
1004 ;; This inner loop goes through only once
1005 ;; unless a line ends inside a string.
1006 (while (and (not inner-loop-done)
1007 (not (setq outer-loop-done (eobp))))
1008 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
1009 nil nil state))
1010 (setq next-depth (car state))
1011 ;; If the line contains a comment other than the sort
1012 ;; that is indented like code,
1013 ;; indent it now with indent-for-comment.
1014 ;; Comments indented like code are right already.
1015 ;; In any case clear the in-comment flag in the state
1016 ;; because parse-partial-sexp never sees the newlines.
1017 (if (car (nthcdr 4 state))
1018 (progn (indent-for-comment)
1019 (end-of-line)
1020 (setcar (nthcdr 4 state) nil)))
1021 ;; If this line ends inside a string,
1022 ;; go straight to next line, remaining within the inner loop,
1023 ;; and turn off the \-flag.
1024 (if (car (nthcdr 3 state))
1025 (progn
1026 (forward-line 1)
1027 (setcar (nthcdr 5 state) nil))
1028 (setq inner-loop-done t)))
1029 (and endpos
1030 (<= next-depth 0)
1031 (progn
1032 (setq indent-stack (nconc indent-stack
1033 (make-list (- next-depth) nil))
1034 last-depth (- last-depth next-depth)
1035 next-depth 0)))
1036 (or outer-loop-done endpos
1037 (setq outer-loop-done (<= next-depth 0)))
1038 (if outer-loop-done
1039 (forward-line 1)
1040 (while (> last-depth next-depth)
1041 (setq indent-stack (cdr indent-stack)
1042 last-depth (1- last-depth)))
1043 (while (< last-depth next-depth)
1044 (setq indent-stack (cons nil indent-stack)
1045 last-depth (1+ last-depth)))
1046 ;; Now go to the next line and indent it according
1047 ;; to what we learned from parsing the previous one.
1048 (forward-line 1)
1049 (setq bol (point))
1050 (skip-chars-forward " \t")
1051 ;; But not if the line is blank, or just a comment
1052 ;; (except for double-semi comments; indent them as usual).
1053 (if (or (eobp) (looking-at "\\s<\\|\n"))
1055 (if (and (car indent-stack)
1056 (>= (car indent-stack) 0))
1057 (setq this-indent (car indent-stack))
1058 (let ((val (calculate-lisp-indent
1059 (if (car indent-stack) (- (car indent-stack))
1060 starting-point))))
1061 (if (null val)
1062 (setq this-indent val)
1063 (if (integerp val)
1064 (setcar indent-stack
1065 (setq this-indent val))
1066 (setcar indent-stack (- (car (cdr val))))
1067 (setq this-indent (car val))))))
1068 (if (and this-indent (/= (current-column) this-indent))
1069 (progn (delete-region bol (point))
1070 (indent-to this-indent)))))
1071 (or outer-loop-done
1072 (setq outer-loop-done (= (point) last-point))
1073 (setq last-point (point)))))))
1075 (defun lisp-indent-region (start end)
1076 "Indent every line whose first char is between START and END inclusive."
1077 (save-excursion
1078 (let ((endmark (copy-marker end)))
1079 (goto-char start)
1080 (and (bolp) (not (eolp))
1081 (lisp-indent-line))
1082 (indent-sexp endmark)
1083 (set-marker endmark nil))))
1085 ;;;; Lisp paragraph filling commands.
1087 (defcustom emacs-lisp-docstring-fill-column 65
1088 "Value of `fill-column' to use when filling a docstring.
1089 Any non-integer value means do not use a different value of
1090 `fill-column' when filling docstrings."
1091 :type '(choice (integer)
1092 (const :tag "Use the current `fill-column'" t))
1093 :group 'lisp)
1095 (defun lisp-fill-paragraph (&optional justify)
1096 "Like \\[fill-paragraph], but handle Emacs Lisp comments and docstrings.
1097 If any of the current line is a comment, fill the comment or the
1098 paragraph of it that point is in, preserving the comment's indentation
1099 and initial semicolons."
1100 (interactive "P")
1101 (or (fill-comment-paragraph justify)
1102 ;; Point is on a program line (a line no comment); we are interested
1103 ;; particularly in docstring lines.
1105 ;; We bind `paragraph-start' and `paragraph-separate' temporarily. They
1106 ;; are buffer-local, but we avoid changing them so that they can be set
1107 ;; to make `forward-paragraph' and friends do something the user wants.
1109 ;; `paragraph-start': The `(' in the character alternative and the
1110 ;; left-singlequote plus `(' sequence after the \\| alternative prevent
1111 ;; sexps and backquoted sexps that follow a docstring from being filled
1112 ;; with the docstring. This setting has the consequence of inhibiting
1113 ;; filling many program lines that are not docstrings, which is sensible,
1114 ;; because the user probably asked to fill program lines by accident, or
1115 ;; expecting indentation (perhaps we should try to do indenting in that
1116 ;; case). The `;' and `:' stop the paragraph being filled at following
1117 ;; comment lines and at keywords (e.g., in `defcustom'). Left parens are
1118 ;; escaped to keep font-locking, filling, & paren matching in the source
1119 ;; file happy.
1121 ;; `paragraph-separate': A clever regexp distinguishes the first line of
1122 ;; a docstring and identifies it as a paragraph separator, so that it
1123 ;; won't be filled. (Since the first line of documentation stands alone
1124 ;; in some contexts, filling should not alter the contents the author has
1125 ;; chosen.) Only the first line of a docstring begins with whitespace
1126 ;; and a quotation mark and ends with a period or (rarely) a comma.
1128 ;; The `fill-column' is temporarily bound to
1129 ;; `emacs-lisp-docstring-fill-column' if that value is an integer.
1130 (let ((paragraph-start (concat paragraph-start
1131 "\\|\\s-*\\([\(;:\"]\\|`\(\\)"))
1132 (paragraph-separate
1133 (concat paragraph-separate "\\|\\s-*\".*[,\\.]$"))
1134 (fill-column (if (integerp emacs-lisp-docstring-fill-column)
1135 emacs-lisp-docstring-fill-column
1136 fill-column)))
1137 (fill-paragraph justify))
1138 ;; Never return nil.
1141 (defun indent-code-rigidly (start end arg &optional nochange-regexp)
1142 "Indent all lines of code, starting in the region, sideways by ARG columns.
1143 Does not affect lines starting inside comments or strings, assuming that
1144 the start of the region is not inside them.
1146 Called from a program, takes args START, END, COLUMNS and NOCHANGE-REGEXP.
1147 The last is a regexp which, if matched at the beginning of a line,
1148 means don't indent that line."
1149 (interactive "r\np")
1150 (let (state)
1151 (save-excursion
1152 (goto-char end)
1153 (setq end (point-marker))
1154 (goto-char start)
1155 (or (bolp)
1156 (setq state (parse-partial-sexp (point)
1157 (progn
1158 (forward-line 1) (point))
1159 nil nil state)))
1160 (while (< (point) end)
1161 (or (car (nthcdr 3 state))
1162 (and nochange-regexp
1163 (looking-at nochange-regexp))
1164 ;; If line does not start in string, indent it
1165 (let ((indent (current-indentation)))
1166 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
1167 (or (eolp)
1168 (indent-to (max 0 (+ indent arg)) 0))))
1169 (setq state (parse-partial-sexp (point)
1170 (progn
1171 (forward-line 1) (point))
1172 nil nil state))))))
1174 (provide 'lisp-mode)
1176 ;;; arch-tag: 414c7f93-c245-4b77-8ed5-ed05ef7ff1bf
1177 ;;; lisp-mode.el ends here