*** empty log message ***
[emacs.git] / lisp / emacs-lisp / lisp-mode.el
blobb922f4e80721f7c4770b63ce4fc20334711cbe52
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-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-hooks 'lisp-mode-hook))
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"))
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.")
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'.
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.")
393 (defun eval-print-last-sexp ()
394 "Evaluate sexp before point; print value into current buffer.
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)))
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)))))
425 (defun last-sexp-toggle-display (&optional arg)
426 "Toggle between abbreviated and unabbreviated printed representations."
427 (interactive "P")
428 ;; Normally this command won't be called at end of line.
429 ;; But when the end of the line is also the end of the buffer,
430 ;; it does get called. For consistency, pretend it was not called.
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 (with-syntax-table emacs-lisp-mode-syntax-table
473 ;; If this sexp appears to be enclosed in `...'
474 ;; then ignore the surrounding quotes.
475 (setq ignore-quotes
476 (or (eq (following-char) ?\')
477 (eq (preceding-char) ?\')))
478 (forward-sexp -1)
479 ;; If we were after `?\e' (or similar case),
480 ;; use the whole thing, not just the `e'.
481 (when (eq (preceding-char) ?\\)
482 (forward-char -1)
483 (when (eq (preceding-char) ??)
484 (forward-char -1)))
486 ;; Skip over `#N='s.
487 (when (eq (preceding-char) ?=)
488 (let (labeled-p)
489 (save-excursion
490 (skip-chars-backward "0-9#=")
491 (setq labeled-p (looking-at "\\(#[0-9]+=\\)+")))
492 (when labeled-p
493 (forward-sexp -1))))
495 (save-restriction
496 ;; vladimir@cs.ualberta.ca 30-Jul-1997: skip ` in
497 ;; `variable' so that the value is returned, not the
498 ;; name
499 (if (and ignore-quotes
500 (eq (following-char) ?`))
501 (forward-char))
502 (narrow-to-region (point-min) opoint)
503 (setq expr (read (current-buffer)))
504 ;; If it's an (interactive ...) form, it's more
505 ;; useful to show how an interactive call would
506 ;; use it.
507 (and (consp expr)
508 (eq (car expr) 'interactive)
509 (setq expr
510 (list 'call-interactively
511 (list 'quote
512 (list 'lambda
513 '(&rest args)
514 expr
515 'args)))))
516 expr))))))
517 (let ((unabbreviated (let ((print-length nil) (print-level nil))
518 (prin1-to-string value)))
519 (print-length eval-expression-print-length)
520 (print-level eval-expression-print-level)
521 (char-string (prin1-char value))
522 (beg (point))
523 end)
524 (prog1
525 (prin1 value)
526 (if (and (eq standard-output t) char-string)
527 (princ (concat " = " char-string)))
528 (setq end (point))
529 (when (and (bufferp standard-output)
530 (or (not (null print-length))
531 (not (null print-level)))
532 (not (string= unabbreviated
533 (buffer-substring-no-properties beg end))))
534 (last-sexp-setup-props beg end value
535 unabbreviated
536 (buffer-substring-no-properties beg end))
537 ))))))
540 (defun eval-last-sexp (eval-last-sexp-arg-internal)
541 "Evaluate sexp before point; print value in minibuffer.
542 Interactively, with prefix argument, print output into current buffer."
543 (interactive "P")
544 (if (null eval-expression-debug-on-error)
545 (eval-last-sexp-1 eval-last-sexp-arg-internal)
546 (let ((old-value (make-symbol "t")) new-value value)
547 (let ((debug-on-error old-value))
548 (setq value (eval-last-sexp-1 eval-last-sexp-arg-internal))
549 (setq new-value debug-on-error))
550 (unless (eq old-value new-value)
551 (setq debug-on-error new-value))
552 value)))
554 (defun eval-defun-1 (form)
555 "Change defvar into defconst within FORM.
556 Likewise for other constructs as necessary."
557 ;; The code in edebug-defun should be consistent with this, but not
558 ;; the same, since this gets a macroexpended form.
559 (cond ((not (listp form))
560 form)
561 ((and (eq (car form) 'defvar)
562 (cdr-safe (cdr-safe form))
563 (boundp (cadr form)))
564 ;; Force variable to be re-set.
565 `(progn (defvar ,(nth 1 form) nil ,@(nthcdr 3 form))
566 (setq ,(nth 1 form) ,(nth 2 form))))
567 ;; `defcustom' is now macroexpanded to
568 ;; `custom-declare-variable' with a quoted value arg.
569 ((and (eq (car form) 'custom-declare-variable)
570 (default-boundp (eval (nth 1 form))))
571 ;; Force variable to be bound.
572 (set-default (eval (nth 1 form)) (eval (nth 1 (nth 2 form))))
573 form)
574 ((eq (car form) 'progn)
575 (cons 'progn (mapcar 'eval-defun-1 (cdr form))))
576 (t form)))
578 (defun eval-defun-2 ()
579 "Evaluate defun that point is in or before.
580 The value is displayed in the minibuffer.
581 If the current defun is actually a call to `defvar',
582 then reset the variable using the initial value expression
583 even if the variable already has some other value.
584 \(Normally `defvar' does not change the variable's value
585 if it already has a value.\)
587 With argument, insert value in current buffer after the defun.
588 Return the result of evaluation."
589 (interactive "P")
590 (let ((debug-on-error eval-expression-debug-on-error)
591 (print-length eval-expression-print-length)
592 (print-level eval-expression-print-level))
593 (save-excursion
594 ;; Arrange for eval-region to "read" the (possibly) altered form.
595 ;; eval-region handles recording which file defines a function or
596 ;; variable. Re-written using `apply' to avoid capturing
597 ;; variables like `end'.
598 (apply
599 #'eval-region
600 (let ((standard-output t)
601 beg end form)
602 ;; Read the form from the buffer, and record where it ends.
603 (save-excursion
604 (end-of-defun)
605 (beginning-of-defun)
606 (setq beg (point))
607 (setq form (read (current-buffer)))
608 (setq end (point)))
609 ;; Alter the form if necessary, changing defvar into defconst, etc.
610 (setq form (eval-defun-1 (macroexpand form)))
611 (list beg end standard-output
612 `(lambda (ignore)
613 ;; Skipping to the end of the specified region
614 ;; will make eval-region return.
615 (goto-char ,end)
616 ',form))))))
617 ;; The result of evaluation has been put onto VALUES. So return it.
618 (car values))
620 (defun eval-defun (edebug-it)
621 "Evaluate the top-level form containing point, or after point.
623 If the current defun is actually a call to `defvar' or `defcustom',
624 evaluating it this way resets the variable using its initial value
625 expression even if the variable already has some other value.
626 \(Normally `defvar' and `defcustom' do not alter the value if there
627 already is one.)
629 With a prefix argument, instrument the code for Edebug.
631 If acting on a `defun' for FUNCTION, and the function was
632 instrumented, `Edebug: FUNCTION' is printed in the minibuffer. If not
633 instrumented, just FUNCTION is printed.
635 If not acting on a `defun', the result of evaluation is displayed in
636 the minibuffer. This display is controlled by the variables
637 `eval-expression-print-length' and `eval-expression-print-level',
638 which see."
639 (interactive "P")
640 (cond (edebug-it
641 (require 'edebug)
642 (eval-defun (not edebug-all-defs)))
644 (if (null eval-expression-debug-on-error)
645 (eval-defun-2)
646 (let ((old-value (make-symbol "t")) new-value value)
647 (let ((debug-on-error old-value))
648 (setq value (eval-defun-2))
649 (setq new-value debug-on-error))
650 (unless (eq old-value new-value)
651 (setq debug-on-error new-value))
652 value)))))
655 (defun lisp-comment-indent ()
656 (if (looking-at "\\s<\\s<\\s<")
657 (current-column)
658 (if (looking-at "\\s<\\s<")
659 (let ((tem (or (calculate-lisp-indent) (current-column))))
660 (if (listp tem) (car tem) tem))
661 (skip-chars-backward " \t")
662 (max (if (bolp) 0 (1+ (current-column)))
663 comment-column))))
665 ;; This function just forces a more costly detection of comments (using
666 ;; parse-partial-sexp from beginning-of-defun). I.e. It avoids the problem of
667 ;; taking a `;' inside a string started on another line for a comment starter.
668 ;; Note: `newcomment' gets it right in 99% of the cases if you're using
669 ;; font-lock, anyway, so we could get rid of it. -stef
670 (defun lisp-mode-auto-fill ()
671 (if (> (current-column) (current-fill-column))
672 (if (save-excursion
673 (nth 4 (syntax-ppss (point))))
674 (do-auto-fill)
675 (unless (and (boundp 'comment-auto-fill-only-comments)
676 comment-auto-fill-only-comments)
677 (let ((comment-start nil) (comment-start-skip nil))
678 (do-auto-fill))))))
680 (defvar lisp-indent-offset nil
681 "If non-nil, indent second line of expressions that many more columns.")
682 (defvar lisp-indent-function 'lisp-indent-function)
684 (defun lisp-indent-line (&optional whole-exp)
685 "Indent current line as Lisp code.
686 With argument, indent any additional lines of the same expression
687 rigidly along with this one."
688 (interactive "P")
689 (let ((indent (calculate-lisp-indent)) shift-amt end
690 (pos (- (point-max) (point)))
691 (beg (progn (beginning-of-line) (point))))
692 (skip-chars-forward " \t")
693 (if (or (null indent) (looking-at "\\s<\\s<\\s<"))
694 ;; Don't alter indentation of a ;;; comment line
695 ;; or a line that starts in a string.
696 (goto-char (- (point-max) pos))
697 (if (and (looking-at "\\s<") (not (looking-at "\\s<\\s<")))
698 ;; Single-semicolon comment lines should be indented
699 ;; as comment lines, not as code.
700 (progn (indent-for-comment) (forward-char -1))
701 (if (listp indent) (setq indent (car indent)))
702 (setq shift-amt (- indent (current-column)))
703 (if (zerop shift-amt)
705 (delete-region beg (point))
706 (indent-to indent)))
707 ;; If initial point was within line's indentation,
708 ;; position after the indentation. Else stay at same point in text.
709 (if (> (- (point-max) pos) (point))
710 (goto-char (- (point-max) pos)))
711 ;; If desired, shift remaining lines of expression the same amount.
712 (and whole-exp (not (zerop shift-amt))
713 (save-excursion
714 (goto-char beg)
715 (forward-sexp 1)
716 (setq end (point))
717 (goto-char beg)
718 (forward-line 1)
719 (setq beg (point))
720 (> end beg))
721 (indent-code-rigidly beg end shift-amt)))))
723 (defvar calculate-lisp-indent-last-sexp)
725 (defun calculate-lisp-indent (&optional parse-start)
726 "Return appropriate indentation for current line as Lisp code.
727 In usual case returns an integer: the column to indent to.
728 If the value is nil, that means don't change the indentation
729 because the line starts inside a string.
731 The value can also be a list of the form (COLUMN CONTAINING-SEXP-START).
732 This means that following lines at the same level of indentation
733 should not necessarily be indented the same as this line.
734 Then COLUMN is the column to indent to, and CONTAINING-SEXP-START
735 is the buffer position of the start of the containing expression."
736 (save-excursion
737 (beginning-of-line)
738 (let ((indent-point (point))
739 state paren-depth
740 ;; setting this to a number inhibits calling hook
741 (desired-indent nil)
742 (retry t)
743 calculate-lisp-indent-last-sexp containing-sexp)
744 (if parse-start
745 (goto-char parse-start)
746 (beginning-of-defun))
747 ;; Find outermost containing sexp
748 (while (< (point) indent-point)
749 (setq state (parse-partial-sexp (point) indent-point 0)))
750 ;; Find innermost containing sexp
751 (while (and retry
752 state
753 (> (setq paren-depth (elt state 0)) 0))
754 (setq retry nil)
755 (setq calculate-lisp-indent-last-sexp (elt state 2))
756 (setq containing-sexp (elt state 1))
757 ;; Position following last unclosed open.
758 (goto-char (1+ containing-sexp))
759 ;; Is there a complete sexp since then?
760 (if (and calculate-lisp-indent-last-sexp
761 (> calculate-lisp-indent-last-sexp (point)))
762 ;; Yes, but is there a containing sexp after that?
763 (let ((peek (parse-partial-sexp calculate-lisp-indent-last-sexp
764 indent-point 0)))
765 (if (setq retry (car (cdr peek))) (setq state peek)))))
766 (if retry
768 ;; Innermost containing sexp found
769 (goto-char (1+ containing-sexp))
770 (if (not calculate-lisp-indent-last-sexp)
771 ;; indent-point immediately follows open paren.
772 ;; Don't call hook.
773 (setq desired-indent (current-column))
774 ;; Find the start of first element of containing sexp.
775 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
776 (cond ((looking-at "\\s(")
777 ;; First element of containing sexp is a list.
778 ;; Indent under that list.
780 ((> (save-excursion (forward-line 1) (point))
781 calculate-lisp-indent-last-sexp)
782 ;; This is the first line to start within the containing sexp.
783 ;; It's almost certainly a function call.
784 (if (= (point) calculate-lisp-indent-last-sexp)
785 ;; Containing sexp has nothing before this line
786 ;; except the first element. Indent under that element.
788 ;; Skip the first element, find start of second (the first
789 ;; argument of the function call) and indent under.
790 (progn (forward-sexp 1)
791 (parse-partial-sexp (point)
792 calculate-lisp-indent-last-sexp
793 0 t)))
794 (backward-prefix-chars))
796 ;; Indent beneath first sexp on same line as
797 ;; `calculate-lisp-indent-last-sexp'. Again, it's
798 ;; almost certainly a function call.
799 (goto-char calculate-lisp-indent-last-sexp)
800 (beginning-of-line)
801 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp
802 0 t)
803 (backward-prefix-chars)))))
804 ;; Point is at the point to indent under unless we are inside a string.
805 ;; Call indentation hook except when overridden by lisp-indent-offset
806 ;; or if the desired indentation has already been computed.
807 (let ((normal-indent (current-column)))
808 (cond ((elt state 3)
809 ;; Inside a string, don't change indentation.
810 nil)
811 ((and (integerp lisp-indent-offset) containing-sexp)
812 ;; Indent by constant offset
813 (goto-char containing-sexp)
814 (+ (current-column) lisp-indent-offset))
815 (desired-indent)
816 ((and (boundp 'lisp-indent-function)
817 lisp-indent-function
818 (not retry))
819 (or (funcall lisp-indent-function indent-point state)
820 normal-indent))
822 normal-indent))))))
824 (defun lisp-indent-function (indent-point state)
825 "This function is the normal value of the variable `lisp-indent-function'.
826 It is used when indenting a line within a function call, to see if the
827 called function says anything special about how to indent the line.
829 INDENT-POINT is the position where the user typed TAB, or equivalent.
830 Point is located at the point to indent under (for default indentation);
831 STATE is the `parse-partial-sexp' state for that position.
833 If the current line is in a call to a Lisp function
834 which has a non-nil property `lisp-indent-function',
835 that specifies how to do the indentation. The property value can be
836 * `defun', meaning indent `defun'-style;
837 * an integer N, meaning indent the first N arguments specially
838 like ordinary function arguments and then indent any further
839 aruments like a body;
840 * a function to call just as this function was called.
841 If that function returns nil, that means it doesn't specify
842 the indentation.
844 This function also returns nil meaning don't specify the indentation."
845 (let ((normal-indent (current-column)))
846 (goto-char (1+ (elt state 1)))
847 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
848 (if (and (elt state 2)
849 (not (looking-at "\\sw\\|\\s_")))
850 ;; car of form doesn't seem to be a symbol
851 (progn
852 (if (not (> (save-excursion (forward-line 1) (point))
853 calculate-lisp-indent-last-sexp))
854 (progn (goto-char calculate-lisp-indent-last-sexp)
855 (beginning-of-line)
856 (parse-partial-sexp (point)
857 calculate-lisp-indent-last-sexp 0 t)))
858 ;; Indent under the list or under the first sexp on the same
859 ;; line as calculate-lisp-indent-last-sexp. Note that first
860 ;; thing on that line has to be complete sexp since we are
861 ;; inside the innermost containing sexp.
862 (backward-prefix-chars)
863 (current-column))
864 (let ((function (buffer-substring (point)
865 (progn (forward-sexp 1) (point))))
866 method)
867 (setq method (or (get (intern-soft function) 'lisp-indent-function)
868 (get (intern-soft function) 'lisp-indent-hook)))
869 (cond ((or (eq method 'defun)
870 (and (null method)
871 (> (length function) 3)
872 (string-match "\\`def" function)))
873 (lisp-indent-defform state indent-point))
874 ((integerp method)
875 (lisp-indent-specform method state
876 indent-point normal-indent))
877 (method
878 (funcall method state indent-point)))))))
880 (defvar lisp-body-indent 2
881 "Number of columns to indent the second line of a `(def...)' form.")
883 (defun lisp-indent-specform (count state indent-point normal-indent)
884 (let ((containing-form-start (elt state 1))
885 (i count)
886 body-indent containing-form-column)
887 ;; Move to the start of containing form, calculate indentation
888 ;; to use for non-distinguished forms (> count), and move past the
889 ;; function symbol. lisp-indent-function guarantees that there is at
890 ;; least one word or symbol character following open paren of containing
891 ;; form.
892 (goto-char containing-form-start)
893 (setq containing-form-column (current-column))
894 (setq body-indent (+ lisp-body-indent containing-form-column))
895 (forward-char 1)
896 (forward-sexp 1)
897 ;; Now find the start of the last form.
898 (parse-partial-sexp (point) indent-point 1 t)
899 (while (and (< (point) indent-point)
900 (condition-case ()
901 (progn
902 (setq count (1- count))
903 (forward-sexp 1)
904 (parse-partial-sexp (point) indent-point 1 t))
905 (error nil))))
906 ;; Point is sitting on first character of last (or count) sexp.
907 (if (> count 0)
908 ;; A distinguished form. If it is the first or second form use double
909 ;; lisp-body-indent, else normal indent. With lisp-body-indent bound
910 ;; to 2 (the default), this just happens to work the same with if as
911 ;; the older code, but it makes unwind-protect, condition-case,
912 ;; with-output-to-temp-buffer, et. al. much more tasteful. The older,
913 ;; less hacked, behavior can be obtained by replacing below with
914 ;; (list normal-indent containing-form-start).
915 (if (<= (- i count) 1)
916 (list (+ containing-form-column (* 2 lisp-body-indent))
917 containing-form-start)
918 (list normal-indent containing-form-start))
919 ;; A non-distinguished form. Use body-indent if there are no
920 ;; distinguished forms and this is the first undistinguished form,
921 ;; or if this is the first undistinguished form and the preceding
922 ;; distinguished form has indentation at least as great as body-indent.
923 (if (or (and (= i 0) (= count 0))
924 (and (= count 0) (<= body-indent normal-indent)))
925 body-indent
926 normal-indent))))
928 (defun lisp-indent-defform (state indent-point)
929 (goto-char (car (cdr state)))
930 (forward-line 1)
931 (if (> (point) (car (cdr (cdr state))))
932 (progn
933 (goto-char (car (cdr state)))
934 (+ lisp-body-indent (current-column)))))
937 ;; (put 'progn 'lisp-indent-function 0), say, causes progn to be indented
938 ;; like defun if the first form is placed on the next line, otherwise
939 ;; it is indented like any other form (i.e. forms line up under first).
941 (put 'lambda 'lisp-indent-function 'defun)
942 (put 'autoload 'lisp-indent-function 'defun)
943 (put 'progn 'lisp-indent-function 0)
944 (put 'prog1 'lisp-indent-function 1)
945 (put 'prog2 'lisp-indent-function 2)
946 (put 'save-excursion 'lisp-indent-function 0)
947 (put 'save-window-excursion 'lisp-indent-function 0)
948 (put 'save-selected-window 'lisp-indent-function 0)
949 (put 'save-restriction 'lisp-indent-function 0)
950 (put 'save-match-data 'lisp-indent-function 0)
951 (put 'save-current-buffer 'lisp-indent-function 0)
952 (put 'with-current-buffer 'lisp-indent-function 1)
953 (put 'combine-after-change-calls 'lisp-indent-function 0)
954 (put 'with-output-to-string 'lisp-indent-function 0)
955 (put 'with-temp-file 'lisp-indent-function 1)
956 (put 'with-temp-buffer 'lisp-indent-function 0)
957 (put 'with-temp-message 'lisp-indent-function 1)
958 (put 'with-syntax-table 'lisp-indent-function 1)
959 (put 'let 'lisp-indent-function 1)
960 (put 'let* 'lisp-indent-function 1)
961 (put 'while 'lisp-indent-function 1)
962 (put 'if 'lisp-indent-function 2)
963 (put 'read-if 'lisp-indent-function 2)
964 (put 'catch 'lisp-indent-function 1)
965 (put 'condition-case 'lisp-indent-function 2)
966 (put 'unwind-protect 'lisp-indent-function 1)
967 (put 'with-output-to-temp-buffer 'lisp-indent-function 1)
968 (put 'eval-after-load 'lisp-indent-function 1)
969 (put 'dolist 'lisp-indent-function 1)
970 (put 'dotimes 'lisp-indent-function 1)
971 (put 'when 'lisp-indent-function 1)
972 (put 'unless 'lisp-indent-function 1)
974 (defun indent-sexp (&optional endpos)
975 "Indent each line of the list starting just after point.
976 If optional arg ENDPOS is given, indent each line, stopping when
977 ENDPOS is encountered."
978 (interactive)
979 (let ((indent-stack (list nil))
980 (next-depth 0)
981 ;; If ENDPOS is non-nil, use nil as STARTING-POINT
982 ;; so that calculate-lisp-indent will find the beginning of
983 ;; the defun we are in.
984 ;; If ENDPOS is nil, it is safe not to scan before point
985 ;; since every line we indent is more deeply nested than point is.
986 (starting-point (if endpos nil (point)))
987 (last-point (point))
988 last-depth bol outer-loop-done inner-loop-done state this-indent)
989 (or endpos
990 ;; Get error now if we don't have a complete sexp after point.
991 (save-excursion (forward-sexp 1)))
992 (save-excursion
993 (setq outer-loop-done nil)
994 (while (if endpos (< (point) endpos)
995 (not outer-loop-done))
996 (setq last-depth next-depth
997 inner-loop-done nil)
998 ;; Parse this line so we can learn the state
999 ;; to indent the next line.
1000 ;; This inner loop goes through only once
1001 ;; unless a line ends inside a string.
1002 (while (and (not inner-loop-done)
1003 (not (setq outer-loop-done (eobp))))
1004 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
1005 nil nil state))
1006 (setq next-depth (car state))
1007 ;; If the line contains a comment other than the sort
1008 ;; that is indented like code,
1009 ;; indent it now with indent-for-comment.
1010 ;; Comments indented like code are right already.
1011 ;; In any case clear the in-comment flag in the state
1012 ;; because parse-partial-sexp never sees the newlines.
1013 (if (car (nthcdr 4 state))
1014 (progn (indent-for-comment)
1015 (end-of-line)
1016 (setcar (nthcdr 4 state) nil)))
1017 ;; If this line ends inside a string,
1018 ;; go straight to next line, remaining within the inner loop,
1019 ;; and turn off the \-flag.
1020 (if (car (nthcdr 3 state))
1021 (progn
1022 (forward-line 1)
1023 (setcar (nthcdr 5 state) nil))
1024 (setq inner-loop-done t)))
1025 (and endpos
1026 (<= next-depth 0)
1027 (progn
1028 (setq indent-stack (nconc indent-stack
1029 (make-list (- next-depth) nil))
1030 last-depth (- last-depth next-depth)
1031 next-depth 0)))
1032 (or outer-loop-done endpos
1033 (setq outer-loop-done (<= next-depth 0)))
1034 (if outer-loop-done
1035 (forward-line 1)
1036 (while (> last-depth next-depth)
1037 (setq indent-stack (cdr indent-stack)
1038 last-depth (1- last-depth)))
1039 (while (< last-depth next-depth)
1040 (setq indent-stack (cons nil indent-stack)
1041 last-depth (1+ last-depth)))
1042 ;; Now go to the next line and indent it according
1043 ;; to what we learned from parsing the previous one.
1044 (forward-line 1)
1045 (setq bol (point))
1046 (skip-chars-forward " \t")
1047 ;; But not if the line is blank, or just a comment
1048 ;; (except for double-semi comments; indent them as usual).
1049 (if (or (eobp) (looking-at "\\s<\\|\n"))
1051 (if (and (car indent-stack)
1052 (>= (car indent-stack) 0))
1053 (setq this-indent (car indent-stack))
1054 (let ((val (calculate-lisp-indent
1055 (if (car indent-stack) (- (car indent-stack))
1056 starting-point))))
1057 (if (null val)
1058 (setq this-indent val)
1059 (if (integerp val)
1060 (setcar indent-stack
1061 (setq this-indent val))
1062 (setcar indent-stack (- (car (cdr val))))
1063 (setq this-indent (car val))))))
1064 (if (and this-indent (/= (current-column) this-indent))
1065 (progn (delete-region bol (point))
1066 (indent-to this-indent)))))
1067 (or outer-loop-done
1068 (setq outer-loop-done (= (point) last-point))
1069 (setq last-point (point)))))))
1071 (defun lisp-indent-region (start end)
1072 "Indent every line whose first char is between START and END inclusive."
1073 (save-excursion
1074 (let ((endmark (copy-marker end)))
1075 (goto-char start)
1076 (and (bolp) (not (eolp))
1077 (lisp-indent-line))
1078 (indent-sexp endmark)
1079 (set-marker endmark nil))))
1081 ;;;; Lisp paragraph filling commands.
1083 (defcustom emacs-lisp-docstring-fill-column 65
1084 "Value of `fill-column' to use when filling a docstring.
1085 Any non-integer value means do not use a different value of
1086 `fill-column' when filling docstrings."
1087 :type '(choice (integer)
1088 (const :tag "Use the current `fill-column'" t))
1089 :group 'lisp)
1091 (defun lisp-fill-paragraph (&optional justify)
1092 "Like \\[fill-paragraph], but handle Emacs Lisp comments and docstrings.
1093 If any of the current line is a comment, fill the comment or the
1094 paragraph of it that point is in, preserving the comment's indentation
1095 and initial semicolons."
1096 (interactive "P")
1097 (or (fill-comment-paragraph justify)
1098 ;; Point is on a program line (a line no comment); we are interested
1099 ;; particularly in docstring lines.
1101 ;; We bind `paragraph-start' and `paragraph-separate' temporarily. They
1102 ;; are buffer-local, but we avoid changing them so that they can be set
1103 ;; to make `forward-paragraph' and friends do something the user wants.
1105 ;; `paragraph-start': The `(' in the character alternative and the
1106 ;; left-singlequote plus `(' sequence after the \\| alternative prevent
1107 ;; sexps and backquoted sexps that follow a docstring from being filled
1108 ;; with the docstring. This setting has the consequence of inhibiting
1109 ;; filling many program lines that are not docstrings, which is sensible,
1110 ;; because the user probably asked to fill program lines by accident, or
1111 ;; expecting indentation (perhaps we should try to do indenting in that
1112 ;; case). The `;' and `:' stop the paragraph being filled at following
1113 ;; comment lines and at keywords (e.g., in `defcustom'). Left parens are
1114 ;; escaped to keep font-locking, filling, & paren matching in the source
1115 ;; file happy.
1117 ;; `paragraph-separate': A clever regexp distinguishes the first line of
1118 ;; a docstring and identifies it as a paragraph separator, so that it
1119 ;; won't be filled. (Since the first line of documentation stands alone
1120 ;; in some contexts, filling should not alter the contents the author has
1121 ;; chosen.) Only the first line of a docstring begins with whitespace
1122 ;; and a quotation mark and ends with a period or (rarely) a comma.
1124 ;; The `fill-column' is temporarily bound to
1125 ;; `emacs-lisp-docstring-fill-column' if that value is an integer.
1126 (let ((paragraph-start (concat paragraph-start
1127 "\\|\\s-*\\([\(;:\"]\\|`\(\\)"))
1128 (paragraph-separate
1129 (concat paragraph-separate "\\|\\s-*\".*[,\\.]$"))
1130 (fill-column (if (integerp emacs-lisp-docstring-fill-column)
1131 emacs-lisp-docstring-fill-column
1132 fill-column)))
1133 (fill-paragraph justify))
1134 ;; Never return nil.
1137 (defun indent-code-rigidly (start end arg &optional nochange-regexp)
1138 "Indent all lines of code, starting in the region, sideways by ARG columns.
1139 Does not affect lines starting inside comments or strings, assuming that
1140 the start of the region is not inside them.
1142 Called from a program, takes args START, END, COLUMNS and NOCHANGE-REGEXP.
1143 The last is a regexp which, if matched at the beginning of a line,
1144 means don't indent that line."
1145 (interactive "r\np")
1146 (let (state)
1147 (save-excursion
1148 (goto-char end)
1149 (setq end (point-marker))
1150 (goto-char start)
1151 (or (bolp)
1152 (setq state (parse-partial-sexp (point)
1153 (progn
1154 (forward-line 1) (point))
1155 nil nil state)))
1156 (while (< (point) end)
1157 (or (car (nthcdr 3 state))
1158 (and nochange-regexp
1159 (looking-at nochange-regexp))
1160 ;; If line does not start in string, indent it
1161 (let ((indent (current-indentation)))
1162 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
1163 (or (eolp)
1164 (indent-to (max 0 (+ indent arg)) 0))))
1165 (setq state (parse-partial-sexp (point)
1166 (progn
1167 (forward-line 1) (point))
1168 nil nil state))))))
1170 (provide 'lisp-mode)
1172 ;;; lisp-mode.el ends here