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