Commit | Line | Data |
---|---|---|
6594deb0 ER |
1 | ;;; lisp-mode.el --- Lisp mode, and its idiosyncratic commands. |
2 | ||
94f8f5d3 | 3 | ;; Copyright (C) 1985, 1986, 1999, 2000, 2001 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) | |
a90256cc | 62 | ;; Used to be singlequote; changed for flonums. |
535eadac DL |
63 | (modify-syntax-entry ?. "_ " table) |
64 | (modify-syntax-entry ?# "' " table) | |
65 | (modify-syntax-entry ?\" "\" " table) | |
66 | (modify-syntax-entry ?\\ "\\ " table) | |
67 | (modify-syntax-entry ?\( "() " table) | |
68 | (modify-syntax-entry ?\) ")( " table) | |
69 | (modify-syntax-entry ?\[ "(] " table) | |
1d3529b6 | 70 | (modify-syntax-entry ?\] ")[ " table)) |
535eadac DL |
71 | table)) |
72 | ||
73 | (defvar lisp-mode-syntax-table | |
74 | (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table))) | |
535eadac DL |
75 | (modify-syntax-entry ?\[ "_ " table) |
76 | (modify-syntax-entry ?\] "_ " table) | |
77 | (modify-syntax-entry ?# "' 14bn" table) | |
e5f597f0 | 78 | (modify-syntax-entry ?| "\" 23b" table) |
535eadac | 79 | table)) |
ecca85de | 80 | |
a90256cc BP |
81 | (define-abbrev-table 'lisp-mode-abbrev-table ()) |
82 | ||
6c2cf866 | 83 | (defvar lisp-imenu-generic-expression |
535eadac | 84 | (list |
ca2ddd8e | 85 | (list nil |
b0d99853 | 86 | (purecopy "^\\s-*(def\\(un\\*?\\|subst\\|macro\\|advice\\|\ |
535eadac | 87 | ine-skeleton\\|ine-minor-mode\\)\\s-+\\(\\sw\\(\\sw\\|\\s_\\)+\\)") 2) |
ca2ddd8e | 88 | (list (purecopy "Variables") |
535eadac DL |
89 | (purecopy "^\\s-*(def\\(var\\|const\\|custom\\)\\s-+\ |
90 | \\(\\sw\\(\\sw\\|\\s_\\)+\\)") 2) | |
ca2ddd8e | 91 | (list (purecopy "Types") |
535eadac | 92 | (purecopy "^\\s-*(def\\(group\\|type\\|struct\\|class\\|\ |
ca2ddd8e | 93 | ine-condition\\|ine-widget\\|face\\)\\s-+'?\\(\\sw\\(\\sw\\|\\s_\\)+\\)") |
6c2cf866 KH |
94 | 2)) |
95 | ||
96 | "Imenu generic expression for Lisp mode. See `imenu-generic-expression'.") | |
97 | ||
a90256cc BP |
98 | (defun lisp-mode-variables (lisp-syntax) |
99 | (cond (lisp-syntax | |
a90256cc BP |
100 | (set-syntax-table lisp-mode-syntax-table))) |
101 | (setq local-abbrev-table lisp-mode-abbrev-table) | |
102 | (make-local-variable 'paragraph-start) | |
763d1579 | 103 | (setq paragraph-start (concat page-delimiter "\\|$" )) |
a90256cc BP |
104 | (make-local-variable 'paragraph-separate) |
105 | (setq paragraph-separate paragraph-start) | |
106 | (make-local-variable 'paragraph-ignore-fill-prefix) | |
107 | (setq paragraph-ignore-fill-prefix t) | |
35d132a8 RS |
108 | (make-local-variable 'fill-paragraph-function) |
109 | (setq fill-paragraph-function 'lisp-fill-paragraph) | |
3272c162 RS |
110 | ;; Adaptive fill mode gets in the way of auto-fill, |
111 | ;; and should make no difference for explicit fill | |
112 | ;; because lisp-fill-paragraph should do the job. | |
113 | (make-local-variable 'adaptive-fill-mode) | |
114 | (setq adaptive-fill-mode nil) | |
7eb67d79 RS |
115 | (make-local-variable 'normal-auto-fill-function) |
116 | (setq normal-auto-fill-function 'lisp-mode-auto-fill) | |
a90256cc BP |
117 | (make-local-variable 'indent-line-function) |
118 | (setq indent-line-function 'lisp-indent-line) | |
119 | (make-local-variable 'indent-region-function) | |
120 | (setq indent-region-function 'lisp-indent-region) | |
121 | (make-local-variable 'parse-sexp-ignore-comments) | |
122 | (setq parse-sexp-ignore-comments t) | |
5847f861 | 123 | (make-local-variable 'outline-regexp) |
a8050bff GM |
124 | (setq outline-regexp ";;;;* \\|(") |
125 | (make-local-variable 'outline-level) | |
126 | (setq outline-level 'lisp-outline-level) | |
a90256cc BP |
127 | (make-local-variable 'comment-start) |
128 | (setq comment-start ";") | |
129 | (make-local-variable 'comment-start-skip) | |
e56a043b MB |
130 | ;; Look within the line for a ; following an even number of backslashes |
131 | ;; after either a non-backslash or the line beginning. | |
132 | (setq comment-start-skip "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\);+ *") | |
3f0c3d8b SM |
133 | (make-local-variable 'comment-add) |
134 | (setq comment-add 1) ;default to `;;' in comment-region | |
a90256cc BP |
135 | (make-local-variable 'comment-column) |
136 | (setq comment-column 40) | |
e41b2db1 | 137 | (make-local-variable 'comment-indent-function) |
6c2cf866 KH |
138 | (setq comment-indent-function 'lisp-comment-indent) |
139 | (make-local-variable 'imenu-generic-expression) | |
1d3529b6 KH |
140 | (setq imenu-generic-expression lisp-imenu-generic-expression) |
141 | (make-local-variable 'multibyte-syntax-as-symbol) | |
1594a23a SM |
142 | (setq multibyte-syntax-as-symbol t) |
143 | (setq font-lock-defaults | |
144 | '((lisp-font-lock-keywords | |
145 | lisp-font-lock-keywords-1 lisp-font-lock-keywords-2) | |
146 | nil nil (("+-*/.<>=!?$%_&~^:" . "w")) beginning-of-defun | |
147 | (font-lock-mark-block-function . mark-defun)))) | |
a8050bff GM |
148 | |
149 | (defun lisp-outline-level () | |
150 | "Lisp mode `outline-level' function." | |
151 | (if (looking-at "(") | |
152 | 1000 | |
153 | (looking-at outline-regexp) | |
154 | (- (match-end 0) (match-beginning 0)))) | |
155 | ||
ca2ddd8e | 156 | |
1594a23a SM |
157 | (defvar lisp-mode-shared-map |
158 | (let ((map (make-sparse-keymap))) | |
a25e82a8 | 159 | (define-key map "\t" 'lisp-indent-line) |
1594a23a SM |
160 | (define-key map "\e\C-q" 'indent-sexp) |
161 | (define-key map "\177" 'backward-delete-char-untabify) | |
c1acacc4 EZ |
162 | ;; This gets in the way when viewing a Lisp file in view-mode. As |
163 | ;; long as [backspace] is mapped into DEL via the | |
164 | ;; function-key-map, this should remain disabled!! | |
165 | ;;;(define-key map [backspace] 'backward-delete-char-untabify) | |
1594a23a | 166 | map) |
a90256cc BP |
167 | "Keymap for commands shared by all sorts of Lisp modes.") |
168 | ||
a90256cc BP |
169 | (defvar emacs-lisp-mode-map () |
170 | "Keymap for Emacs Lisp mode. | |
99ec65b3 | 171 | All commands in `lisp-mode-shared-map' are inherited by this map.") |
a90256cc BP |
172 | |
173 | (if emacs-lisp-mode-map | |
174 | () | |
b3a3cb63 | 175 | (let ((map (make-sparse-keymap "Emacs-Lisp"))) |
b8bc6df2 | 176 | (setq emacs-lisp-mode-map (make-sparse-keymap)) |
99ec65b3 | 177 | (set-keymap-parent emacs-lisp-mode-map lisp-mode-shared-map) |
b3a3cb63 KH |
178 | (define-key emacs-lisp-mode-map "\e\t" 'lisp-complete-symbol) |
179 | (define-key emacs-lisp-mode-map "\e\C-x" 'eval-defun) | |
180 | (define-key emacs-lisp-mode-map [menu-bar] (make-sparse-keymap)) | |
181 | (define-key emacs-lisp-mode-map [menu-bar emacs-lisp] | |
182 | (cons "Emacs-Lisp" map)) | |
183 | (define-key map [edebug-defun] | |
184 | '("Instrument Function for Debugging" . edebug-defun)) | |
185 | (define-key map [byte-recompile] | |
186 | '("Byte-recompile Directory..." . byte-recompile-directory)) | |
00e40cc9 | 187 | (define-key map [emacs-byte-compile-and-load] |
eaec854f | 188 | '("Byte-compile And Load" . emacs-lisp-byte-compile-and-load)) |
b3a3cb63 KH |
189 | (define-key map [byte-compile] |
190 | '("Byte-compile This File" . emacs-lisp-byte-compile)) | |
191 | (define-key map [separator-eval] '("--")) | |
192 | (define-key map [eval-buffer] '("Evaluate Buffer" . eval-current-buffer)) | |
193 | (define-key map [eval-region] '("Evaluate Region" . eval-region)) | |
194 | (define-key map [eval-sexp] '("Evaluate Last S-expression" . eval-last-sexp)) | |
195 | (define-key map [separator-format] '("--")) | |
196 | (define-key map [comment-region] '("Comment Out Region" . comment-region)) | |
197 | (define-key map [indent-region] '("Indent Region" . indent-region)) | |
4b619eca SM |
198 | (define-key map [indent-line] '("Indent Line" . lisp-indent-line)) |
199 | (put 'eval-region 'menu-enable 'mark-active) | |
200 | (put 'comment-region 'menu-enable 'mark-active) | |
201 | (put 'indent-region 'menu-enable 'mark-active))) | |
b3a3cb63 KH |
202 | |
203 | (defun emacs-lisp-byte-compile () | |
204 | "Byte compile the file containing the current buffer." | |
205 | (interactive) | |
206 | (if buffer-file-name | |
207 | (byte-compile-file buffer-file-name) | |
767a1151 KH |
208 | (error "The buffer must be saved in a file first"))) |
209 | ||
eaec854f | 210 | (defun emacs-lisp-byte-compile-and-load () |
767a1151 KH |
211 | "Byte-compile the current file (if it has changed), then load compiled code." |
212 | (interactive) | |
213 | (or buffer-file-name | |
214 | (error "The buffer must be saved in a file first")) | |
215 | (require 'bytecomp) | |
216 | ;; Recompile if file or buffer has changed since last compilation. | |
eaec854f | 217 | (if (and (buffer-modified-p) |
535eadac | 218 | (y-or-n-p (format "Save buffer %s first? " (buffer-name)))) |
eaec854f SM |
219 | (save-buffer)) |
220 | (let ((compiled-file-name (byte-compile-dest-file buffer-file-name))) | |
221 | (if (file-newer-than-file-p compiled-file-name buffer-file-name) | |
222 | (load-file compiled-file-name) | |
223 | (byte-compile-file buffer-file-name t)))) | |
a90256cc | 224 | |
e596094d DL |
225 | (defcustom emacs-lisp-mode-hook nil |
226 | "Hook run when entering Emacs Lisp mode." | |
535eadac | 227 | :options '(turn-on-eldoc-mode imenu-add-menubar-index checkdoc-minor-mode) |
e596094d DL |
228 | :type 'hook |
229 | :group 'lisp) | |
230 | ||
231 | (defcustom lisp-mode-hook nil | |
232 | "Hook run when entering Lisp mode." | |
233 | :options '(imenu-add-menubar-index) | |
234 | :type 'hook | |
235 | :group 'lisp) | |
236 | ||
237 | (defcustom lisp-interaction-mode-hook nil | |
238 | "Hook run when entering Lisp Interaction mode." | |
239 | :options '(turn-on-eldoc-mode) | |
240 | :type 'hook | |
241 | :group 'lisp) | |
242 | ||
1594a23a | 243 | (define-derived-mode emacs-lisp-mode nil "Emacs-Lisp" |
a90256cc BP |
244 | "Major mode for editing Lisp code to run in Emacs. |
245 | Commands: | |
246 | Delete converts tabs to spaces as it moves back. | |
247 | Blank lines separate paragraphs. Semicolons start comments. | |
248 | \\{emacs-lisp-mode-map} | |
249 | Entry to this mode calls the value of `emacs-lisp-mode-hook' | |
250 | if that value is non-nil." | |
a90256cc | 251 | (lisp-mode-variables nil) |
1594a23a | 252 | (setq imenu-case-fold-search nil)) |
a90256cc | 253 | |
535eadac DL |
254 | (defvar lisp-mode-map |
255 | (let ((map (make-sparse-keymap))) | |
99ec65b3 | 256 | (set-keymap-parent map lisp-mode-shared-map) |
535eadac DL |
257 | (define-key map "\e\C-x" 'lisp-eval-defun) |
258 | (define-key map "\C-c\C-z" 'run-lisp) | |
259 | map) | |
a90256cc | 260 | "Keymap for ordinary Lisp mode. |
99ec65b3 | 261 | All commands in `lisp-mode-shared-map' are inherited by this map.") |
a90256cc | 262 | |
1594a23a | 263 | (define-derived-mode lisp-mode nil "Lisp" |
a90256cc BP |
264 | "Major mode for editing Lisp code for Lisps other than GNU Emacs Lisp. |
265 | Commands: | |
266 | Delete converts tabs to spaces as it moves back. | |
267 | Blank lines separate paragraphs. Semicolons start comments. | |
268 | \\{lisp-mode-map} | |
269 | Note that `run-lisp' may be used either to start an inferior Lisp job | |
270 | or to switch back to an existing one. | |
271 | ||
272 | Entry to this mode calls the value of `lisp-mode-hook' | |
273 | if that value is non-nil." | |
a90256cc | 274 | (lisp-mode-variables t) |
5408c149 | 275 | (make-local-variable 'font-lock-keywords-case-fold-search) |
01b91009 | 276 | (setq font-lock-keywords-case-fold-search t) |
1594a23a | 277 | (setq imenu-case-fold-search t)) |
a90256cc | 278 | |
5e871da0 DL |
279 | ;; This will do unless inf-lisp.el is loaded. |
280 | (defun lisp-eval-defun (&optional and-go) | |
a90256cc BP |
281 | "Send the current defun to the Lisp process made by \\[run-lisp]." |
282 | (interactive) | |
283 | (error "Process lisp does not exist")) | |
284 | ||
535eadac DL |
285 | (defvar lisp-interaction-mode-map |
286 | (let ((map (make-sparse-keymap))) | |
99ec65b3 | 287 | (set-keymap-parent map lisp-mode-shared-map) |
535eadac DL |
288 | (define-key map "\e\C-x" 'eval-defun) |
289 | (define-key map "\e\t" 'lisp-complete-symbol) | |
290 | (define-key map "\n" 'eval-print-last-sexp) | |
291 | map) | |
bacd83a7 | 292 | "Keymap for Lisp Interaction mode. |
99ec65b3 | 293 | All commands in `lisp-mode-shared-map' are inherited by this map.") |
a90256cc | 294 | |
1594a23a | 295 | (define-derived-mode lisp-interaction-mode emacs-lisp-mode "Lisp Interaction" |
a90256cc BP |
296 | "Major mode for typing and evaluating Lisp forms. |
297 | Like Lisp mode except that \\[eval-print-last-sexp] evals the Lisp expression | |
298 | before point, and prints its value into the buffer, advancing point. | |
94f8f5d3 GM |
299 | Note that printing is controled by `eval-expression-print-length' |
300 | and `eval-expression-print-level'. | |
a90256cc BP |
301 | |
302 | Commands: | |
303 | Delete converts tabs to spaces as it moves back. | |
304 | Paragraphs are separated only by blank lines. | |
305 | Semicolons start comments. | |
306 | \\{lisp-interaction-mode-map} | |
307 | Entry to this mode calls the value of `lisp-interaction-mode-hook' | |
1594a23a | 308 | if that value is non-nil.") |
a90256cc | 309 | |
121f0d57 | 310 | (defun eval-print-last-sexp () |
a90256cc | 311 | "Evaluate sexp before point; print value into current buffer." |
121f0d57 | 312 | (interactive) |
f798d950 JB |
313 | (let ((standard-output (current-buffer))) |
314 | (terpri) | |
315 | (eval-last-sexp t) | |
316 | (terpri))) | |
ca2ddd8e | 317 | |
99c6d63b | 318 | (defun eval-last-sexp-1 (eval-last-sexp-arg-internal) |
a90256cc BP |
319 | "Evaluate sexp before point; print value in minibuffer. |
320 | With argument, print output into current buffer." | |
99c6d63b | 321 | (let ((standard-output (if eval-last-sexp-arg-internal (current-buffer) t))) |
efb195f0 RS |
322 | (let ((value |
323 | (eval (let ((stab (syntax-table)) | |
361721f2 | 324 | (opoint (point)) |
05e94d32 | 325 | ignore-quotes |
361721f2 KH |
326 | expr) |
327 | (unwind-protect | |
19b2f8f1 RM |
328 | (save-excursion |
329 | (set-syntax-table emacs-lisp-mode-syntax-table) | |
05e94d32 RS |
330 | ;; If this sexp appears to be enclosed in `...' |
331 | ;; then ignore the surrounding quotes. | |
332 | (setq ignore-quotes | |
333 | (or (eq (following-char) ?\') | |
334 | (eq (preceding-char) ?\'))) | |
19b2f8f1 | 335 | (forward-sexp -1) |
19b014e4 RS |
336 | ;; If we were after `?\e' (or similar case), |
337 | ;; use the whole thing, not just the `e'. | |
338 | (when (eq (preceding-char) ?\\) | |
339 | (forward-char -1) | |
340 | (when (eq (preceding-char) ??) | |
341 | (forward-char -1))) | |
ca2ddd8e | 342 | |
07ca56eb GM |
343 | ;; Skip over `#N='s. |
344 | (when (eq (preceding-char) ?=) | |
345 | (let (labeled-p) | |
346 | (save-excursion | |
347 | (skip-chars-backward "0-9#=") | |
348 | (setq labeled-p (looking-at "\\(#[0-9]+=\\)+"))) | |
349 | (when labeled-p | |
350 | (forward-sexp -1)))) | |
351 | ||
cfe158ab | 352 | (save-restriction |
05e94d32 RS |
353 | ;; vladimir@cs.ualberta.ca 30-Jul-1997: skip ` in |
354 | ;; `variable' so that the value is returned, not the | |
ca2ddd8e | 355 | ;; name |
05e94d32 RS |
356 | (if (and ignore-quotes |
357 | (eq (following-char) ?`)) | |
358 | (forward-char)) | |
cfe158ab | 359 | (narrow-to-region (point-min) opoint) |
361721f2 KH |
360 | (setq expr (read (current-buffer))) |
361 | ;; If it's an (interactive ...) form, it's more | |
362 | ;; useful to show how an interactive call would | |
363 | ;; use it. | |
364 | (and (consp expr) | |
365 | (eq (car expr) 'interactive) | |
366 | (setq expr | |
367 | (list 'call-interactively | |
368 | (list 'quote | |
369 | (list 'lambda | |
370 | '(&rest args) | |
371 | expr | |
372 | 'args))))) | |
373 | expr)) | |
efb195f0 RS |
374 | (set-syntax-table stab)))))) |
375 | (let ((print-length eval-expression-print-length) | |
376 | (print-level eval-expression-print-level)) | |
2e0a943f | 377 | (prin1 value))))) |
a90256cc | 378 | |
99c6d63b GM |
379 | (defun eval-last-sexp (eval-last-sexp-arg-internal) |
380 | "Evaluate sexp before point; print value in minibuffer. | |
0202508f | 381 | Interactively, with prefix argument, print output into current buffer." |
99c6d63b GM |
382 | (interactive "P") |
383 | (if (null eval-expression-debug-on-error) | |
384 | (eval-last-sexp-1 eval-last-sexp-arg-internal) | |
385 | (let ((old-value (make-symbol "t")) new-value value) | |
386 | (let ((debug-on-error old-value)) | |
387 | (setq value (eval-last-sexp-1 eval-last-sexp-arg-internal)) | |
388 | (setq new-value debug-on-error)) | |
389 | (unless (eq old-value new-value) | |
390 | (setq debug-on-error new-value)) | |
391 | value))) | |
ca2ddd8e | 392 | |
151a410a | 393 | (defun eval-defun-1 (form) |
99ec65b3 DL |
394 | "Change defvar into defconst within FORM. |
395 | Likewise for other constructs as necessary." | |
396 | ;; The code in edebug-defun should be consistent with this, but not | |
397 | ;; the same, since this gets a macroexpended form. | |
151a410a RS |
398 | (cond ((and (eq (car form) 'defvar) |
399 | (cdr-safe (cdr-safe form))) | |
400 | ;; Force variable to be bound. | |
401 | (cons 'defconst (cdr form))) | |
5e871da0 DL |
402 | ;; `defcustom' is now macroexpanded to |
403 | ;; `custom-declare-variable' with a quoted value arg. | |
535eadac DL |
404 | ((and (eq (car form) 'custom-declare-variable) |
405 | (default-boundp (eval (nth 1 form)))) | |
151a410a | 406 | ;; Force variable to be bound. |
5e871da0 | 407 | (set-default (eval (nth 1 form)) (eval (nth 1 (nth 2 form)))) |
151a410a RS |
408 | form) |
409 | ((eq (car form) 'progn) | |
410 | (cons 'progn (mapcar 'eval-defun-1 (cdr form)))) | |
411 | (t form))) | |
412 | ||
105d6be1 | 413 | (defun eval-defun-2 () |
121f0d57 | 414 | "Evaluate defun that point is in or before. |
ebc03d28 KH |
415 | The value is displayed in the minibuffer. |
416 | If the current defun is actually a call to `defvar', | |
417 | then reset the variable using the initial value expression | |
418 | even if the variable already has some other value. | |
419 | \(Normally `defvar' does not change the variable's value | |
420 | if it already has a value.\) | |
421 | ||
2298f9f7 KH |
422 | With argument, insert value in current buffer after the defun. |
423 | Return the result of evaluation." | |
a90256cc | 424 | (interactive "P") |
efb195f0 RS |
425 | (let ((debug-on-error eval-expression-debug-on-error) |
426 | (print-length eval-expression-print-length) | |
427 | (print-level eval-expression-print-level)) | |
428 | (save-excursion | |
429 | ;; Arrange for eval-region to "read" the (possibly) altered form. | |
430 | ;; eval-region handles recording which file defines a function or | |
431 | ;; variable. Re-written using `apply' to avoid capturing | |
432 | ;; variables like `end'. | |
433 | (apply | |
ca2ddd8e | 434 | #'eval-region |
105d6be1 | 435 | (let ((standard-output t) |
efb195f0 RS |
436 | beg end form) |
437 | ;; Read the form from the buffer, and record where it ends. | |
438 | (save-excursion | |
439 | (end-of-defun) | |
440 | (beginning-of-defun) | |
441 | (setq beg (point)) | |
442 | (setq form (read (current-buffer))) | |
443 | (setq end (point))) | |
444 | ;; Alter the form if necessary, changing defvar into defconst, etc. | |
445 | (setq form (eval-defun-1 (macroexpand form))) | |
446 | (list beg end standard-output | |
447 | `(lambda (ignore) | |
448 | ;; Skipping to the end of the specified region | |
449 | ;; will make eval-region return. | |
450 | (goto-char ,end) | |
451 | ',form)))))) | |
713c3fb1 DL |
452 | ;; The result of evaluation has been put onto VALUES. So return it. |
453 | (car values)) | |
99c6d63b | 454 | |
105d6be1 GM |
455 | (defun eval-defun (edebug-it) |
456 | "Evaluate the top-level form containing point, or after point. | |
99c6d63b | 457 | |
105d6be1 GM |
458 | If the current defun is actually a call to `defvar', then reset the |
459 | variable using its initial value expression even if the variable | |
460 | already has some other value. (Normally `defvar' does not change the | |
461 | variable's value if it already has a value.) | |
462 | ||
463 | With a prefix argument, instrument the code for Edebug. | |
464 | ||
465 | If acting on a `defun' for FUNCTION, and the function was | |
466 | instrumented, `Edebug: FUNCTION' is printed in the minibuffer. If not | |
467 | instrumented, just FUNCTION is printed. | |
468 | ||
469 | If not acting on a `defun', the result of evaluation is displayed in | |
470 | the minibuffer." | |
99c6d63b | 471 | (interactive "P") |
105d6be1 GM |
472 | (cond (edebug-it |
473 | (require 'edebug) | |
474 | (eval-defun (not edebug-all-defs))) | |
475 | (t | |
476 | (if (null eval-expression-debug-on-error) | |
477 | (eval-defun-2) | |
478 | (let ((old-value (make-symbol "t")) new-value value) | |
479 | (let ((debug-on-error old-value)) | |
480 | (setq value (eval-defun-2)) | |
481 | (setq new-value debug-on-error)) | |
482 | (unless (eq old-value new-value) | |
483 | (setq debug-on-error new-value)) | |
484 | value))))) | |
99c6d63b | 485 | |
ca2ddd8e | 486 | |
a90256cc BP |
487 | (defun lisp-comment-indent () |
488 | (if (looking-at "\\s<\\s<\\s<") | |
489 | (current-column) | |
490 | (if (looking-at "\\s<\\s<") | |
531cbff1 | 491 | (let ((tem (or (calculate-lisp-indent) (current-column)))) |
a90256cc BP |
492 | (if (listp tem) (car tem) tem)) |
493 | (skip-chars-backward " \t") | |
494 | (max (if (bolp) 0 (1+ (current-column))) | |
495 | comment-column)))) | |
496 | ||
7eb67d79 RS |
497 | (defun lisp-mode-auto-fill () |
498 | (if (> (current-column) (current-fill-column)) | |
499 | (if (save-excursion | |
500 | (nth 4 (parse-partial-sexp (save-excursion | |
501 | (beginning-of-defun) | |
502 | (point)) | |
503 | (point)))) | |
504 | (do-auto-fill) | |
505 | (let ((comment-start nil) (comment-start-skip nil)) | |
506 | (do-auto-fill))))) | |
507 | ||
cada28bb EZ |
508 | (defvar lisp-indent-offset nil |
509 | "If non-nil, indent second line of expressions that many more columns.") | |
535eadac | 510 | (defvar lisp-indent-function 'lisp-indent-function) |
a90256cc BP |
511 | |
512 | (defun lisp-indent-line (&optional whole-exp) | |
513 | "Indent current line as Lisp code. | |
514 | With argument, indent any additional lines of the same expression | |
515 | rigidly along with this one." | |
516 | (interactive "P") | |
517 | (let ((indent (calculate-lisp-indent)) shift-amt beg end | |
518 | (pos (- (point-max) (point)))) | |
519 | (beginning-of-line) | |
520 | (setq beg (point)) | |
521 | (skip-chars-forward " \t") | |
531cbff1 RS |
522 | (if (or (null indent) (looking-at "\\s<\\s<\\s<")) |
523 | ;; Don't alter indentation of a ;;; comment line | |
524 | ;; or a line that starts in a string. | |
328561fc | 525 | (goto-char (- (point-max) pos)) |
a90256cc BP |
526 | (if (and (looking-at "\\s<") (not (looking-at "\\s<\\s<"))) |
527 | ;; Single-semicolon comment lines should be indented | |
528 | ;; as comment lines, not as code. | |
529 | (progn (indent-for-comment) (forward-char -1)) | |
530 | (if (listp indent) (setq indent (car indent))) | |
531 | (setq shift-amt (- indent (current-column))) | |
532 | (if (zerop shift-amt) | |
533 | nil | |
534 | (delete-region beg (point)) | |
535 | (indent-to indent))) | |
536 | ;; If initial point was within line's indentation, | |
537 | ;; position after the indentation. Else stay at same point in text. | |
538 | (if (> (- (point-max) pos) (point)) | |
539 | (goto-char (- (point-max) pos))) | |
540 | ;; If desired, shift remaining lines of expression the same amount. | |
541 | (and whole-exp (not (zerop shift-amt)) | |
542 | (save-excursion | |
543 | (goto-char beg) | |
544 | (forward-sexp 1) | |
545 | (setq end (point)) | |
546 | (goto-char beg) | |
547 | (forward-line 1) | |
548 | (setq beg (point)) | |
549 | (> end beg)) | |
550 | (indent-code-rigidly beg end shift-amt))))) | |
551 | ||
22486a7f | 552 | (defvar calculate-lisp-indent-last-sexp) |
c0df1d61 | 553 | |
a90256cc BP |
554 | (defun calculate-lisp-indent (&optional parse-start) |
555 | "Return appropriate indentation for current line as Lisp code. | |
556 | In usual case returns an integer: the column to indent to. | |
531cbff1 RS |
557 | If the value is nil, that means don't change the indentation |
558 | because the line starts inside a string. | |
559 | ||
560 | The value can also be a list of the form (COLUMN CONTAINING-SEXP-START). | |
a90256cc | 561 | This means that following lines at the same level of indentation |
531cbff1 RS |
562 | should not necessarily be indented the same as this line. |
563 | Then COLUMN is the column to indent to, and CONTAINING-SEXP-START | |
564 | is the buffer position of the start of the containing expression." | |
a90256cc BP |
565 | (save-excursion |
566 | (beginning-of-line) | |
567 | (let ((indent-point (point)) | |
568 | state paren-depth | |
569 | ;; setting this to a number inhibits calling hook | |
570 | (desired-indent nil) | |
571 | (retry t) | |
c0df1d61 | 572 | calculate-lisp-indent-last-sexp containing-sexp) |
a90256cc BP |
573 | (if parse-start |
574 | (goto-char parse-start) | |
575 | (beginning-of-defun)) | |
576 | ;; Find outermost containing sexp | |
577 | (while (< (point) indent-point) | |
578 | (setq state (parse-partial-sexp (point) indent-point 0))) | |
579 | ;; Find innermost containing sexp | |
580 | (while (and retry | |
581 | state | |
582 | (> (setq paren-depth (elt state 0)) 0)) | |
583 | (setq retry nil) | |
c0df1d61 | 584 | (setq calculate-lisp-indent-last-sexp (elt state 2)) |
a90256cc BP |
585 | (setq containing-sexp (elt state 1)) |
586 | ;; Position following last unclosed open. | |
587 | (goto-char (1+ containing-sexp)) | |
588 | ;; Is there a complete sexp since then? | |
c0df1d61 RS |
589 | (if (and calculate-lisp-indent-last-sexp |
590 | (> calculate-lisp-indent-last-sexp (point))) | |
a90256cc | 591 | ;; Yes, but is there a containing sexp after that? |
c0df1d61 RS |
592 | (let ((peek (parse-partial-sexp calculate-lisp-indent-last-sexp |
593 | indent-point 0))) | |
a90256cc BP |
594 | (if (setq retry (car (cdr peek))) (setq state peek))))) |
595 | (if retry | |
596 | nil | |
597 | ;; Innermost containing sexp found | |
598 | (goto-char (1+ containing-sexp)) | |
c0df1d61 | 599 | (if (not calculate-lisp-indent-last-sexp) |
a90256cc BP |
600 | ;; indent-point immediately follows open paren. |
601 | ;; Don't call hook. | |
602 | (setq desired-indent (current-column)) | |
603 | ;; Find the start of first element of containing sexp. | |
c0df1d61 | 604 | (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t) |
a90256cc BP |
605 | (cond ((looking-at "\\s(") |
606 | ;; First element of containing sexp is a list. | |
607 | ;; Indent under that list. | |
608 | ) | |
609 | ((> (save-excursion (forward-line 1) (point)) | |
c0df1d61 | 610 | calculate-lisp-indent-last-sexp) |
a90256cc BP |
611 | ;; This is the first line to start within the containing sexp. |
612 | ;; It's almost certainly a function call. | |
c0df1d61 | 613 | (if (= (point) calculate-lisp-indent-last-sexp) |
a90256cc BP |
614 | ;; Containing sexp has nothing before this line |
615 | ;; except the first element. Indent under that element. | |
616 | nil | |
617 | ;; Skip the first element, find start of second (the first | |
618 | ;; argument of the function call) and indent under. | |
619 | (progn (forward-sexp 1) | |
c0df1d61 RS |
620 | (parse-partial-sexp (point) |
621 | calculate-lisp-indent-last-sexp | |
622 | 0 t))) | |
a90256cc BP |
623 | (backward-prefix-chars)) |
624 | (t | |
c0df1d61 | 625 | ;; Indent beneath first sexp on same line as |
535eadac | 626 | ;; `calculate-lisp-indent-last-sexp'. Again, it's |
c0df1d61 RS |
627 | ;; almost certainly a function call. |
628 | (goto-char calculate-lisp-indent-last-sexp) | |
a90256cc | 629 | (beginning-of-line) |
c0df1d61 RS |
630 | (parse-partial-sexp (point) calculate-lisp-indent-last-sexp |
631 | 0 t) | |
a90256cc BP |
632 | (backward-prefix-chars))))) |
633 | ;; Point is at the point to indent under unless we are inside a string. | |
eb8c3be9 | 634 | ;; Call indentation hook except when overridden by lisp-indent-offset |
a90256cc BP |
635 | ;; or if the desired indentation has already been computed. |
636 | (let ((normal-indent (current-column))) | |
637 | (cond ((elt state 3) | |
638 | ;; Inside a string, don't change indentation. | |
531cbff1 | 639 | nil) |
a90256cc BP |
640 | ((and (integerp lisp-indent-offset) containing-sexp) |
641 | ;; Indent by constant offset | |
642 | (goto-char containing-sexp) | |
643 | (+ (current-column) lisp-indent-offset)) | |
644 | (desired-indent) | |
645 | ((and (boundp 'lisp-indent-function) | |
646 | lisp-indent-function | |
647 | (not retry)) | |
648 | (or (funcall lisp-indent-function indent-point state) | |
649 | normal-indent)) | |
650 | (t | |
f30ff39f | 651 | normal-indent)))))) |
a90256cc BP |
652 | |
653 | (defun lisp-indent-function (indent-point state) | |
654 | (let ((normal-indent (current-column))) | |
655 | (goto-char (1+ (elt state 1))) | |
c0df1d61 | 656 | (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t) |
a90256cc BP |
657 | (if (and (elt state 2) |
658 | (not (looking-at "\\sw\\|\\s_"))) | |
659 | ;; car of form doesn't seem to be a a symbol | |
660 | (progn | |
661 | (if (not (> (save-excursion (forward-line 1) (point)) | |
c0df1d61 RS |
662 | calculate-lisp-indent-last-sexp)) |
663 | (progn (goto-char calculate-lisp-indent-last-sexp) | |
a90256cc | 664 | (beginning-of-line) |
c0df1d61 RS |
665 | (parse-partial-sexp (point) |
666 | calculate-lisp-indent-last-sexp 0 t))) | |
667 | ;; Indent under the list or under the first sexp on the same | |
668 | ;; line as calculate-lisp-indent-last-sexp. Note that first | |
669 | ;; thing on that line has to be complete sexp since we are | |
670 | ;; inside the innermost containing sexp. | |
a90256cc BP |
671 | (backward-prefix-chars) |
672 | (current-column)) | |
673 | (let ((function (buffer-substring (point) | |
674 | (progn (forward-sexp 1) (point)))) | |
675 | method) | |
2dab7802 RS |
676 | (setq method (or (get (intern-soft function) 'lisp-indent-function) |
677 | (get (intern-soft function) 'lisp-indent-hook))) | |
a90256cc BP |
678 | (cond ((or (eq method 'defun) |
679 | (and (null method) | |
680 | (> (length function) 3) | |
681 | (string-match "\\`def" function))) | |
682 | (lisp-indent-defform state indent-point)) | |
683 | ((integerp method) | |
684 | (lisp-indent-specform method state | |
685 | indent-point normal-indent)) | |
686 | (method | |
687 | (funcall method state indent-point))))))) | |
688 | ||
d7fa5aa2 | 689 | (defvar lisp-body-indent 2 |
ab69b2fb | 690 | "Number of columns to indent the second line of a `(def...)' form.") |
a90256cc BP |
691 | |
692 | (defun lisp-indent-specform (count state indent-point normal-indent) | |
693 | (let ((containing-form-start (elt state 1)) | |
694 | (i count) | |
695 | body-indent containing-form-column) | |
696 | ;; Move to the start of containing form, calculate indentation | |
697 | ;; to use for non-distinguished forms (> count), and move past the | |
698 | ;; function symbol. lisp-indent-function guarantees that there is at | |
699 | ;; least one word or symbol character following open paren of containing | |
700 | ;; form. | |
701 | (goto-char containing-form-start) | |
702 | (setq containing-form-column (current-column)) | |
703 | (setq body-indent (+ lisp-body-indent containing-form-column)) | |
704 | (forward-char 1) | |
705 | (forward-sexp 1) | |
706 | ;; Now find the start of the last form. | |
707 | (parse-partial-sexp (point) indent-point 1 t) | |
708 | (while (and (< (point) indent-point) | |
709 | (condition-case () | |
710 | (progn | |
711 | (setq count (1- count)) | |
712 | (forward-sexp 1) | |
713 | (parse-partial-sexp (point) indent-point 1 t)) | |
714 | (error nil)))) | |
715 | ;; Point is sitting on first character of last (or count) sexp. | |
716 | (if (> count 0) | |
717 | ;; A distinguished form. If it is the first or second form use double | |
718 | ;; lisp-body-indent, else normal indent. With lisp-body-indent bound | |
719 | ;; to 2 (the default), this just happens to work the same with if as | |
720 | ;; the older code, but it makes unwind-protect, condition-case, | |
721 | ;; with-output-to-temp-buffer, et. al. much more tasteful. The older, | |
722 | ;; less hacked, behavior can be obtained by replacing below with | |
723 | ;; (list normal-indent containing-form-start). | |
724 | (if (<= (- i count) 1) | |
725 | (list (+ containing-form-column (* 2 lisp-body-indent)) | |
726 | containing-form-start) | |
727 | (list normal-indent containing-form-start)) | |
728 | ;; A non-distinguished form. Use body-indent if there are no | |
729 | ;; distinguished forms and this is the first undistinguished form, | |
730 | ;; or if this is the first undistinguished form and the preceding | |
731 | ;; distinguished form has indentation at least as great as body-indent. | |
732 | (if (or (and (= i 0) (= count 0)) | |
733 | (and (= count 0) (<= body-indent normal-indent))) | |
734 | body-indent | |
735 | normal-indent)))) | |
736 | ||
737 | (defun lisp-indent-defform (state indent-point) | |
738 | (goto-char (car (cdr state))) | |
739 | (forward-line 1) | |
740 | (if (> (point) (car (cdr (cdr state)))) | |
741 | (progn | |
742 | (goto-char (car (cdr state))) | |
743 | (+ lisp-body-indent (current-column))))) | |
744 | ||
ca2ddd8e | 745 | |
a90256cc BP |
746 | ;; (put 'progn 'lisp-indent-function 0), say, causes progn to be indented |
747 | ;; like defun if the first form is placed on the next line, otherwise | |
748 | ;; it is indented like any other form (i.e. forms line up under first). | |
749 | ||
750 | (put 'lambda 'lisp-indent-function 'defun) | |
751 | (put 'autoload 'lisp-indent-function 'defun) | |
752 | (put 'progn 'lisp-indent-function 0) | |
753 | (put 'prog1 'lisp-indent-function 1) | |
754 | (put 'prog2 'lisp-indent-function 2) | |
755 | (put 'save-excursion 'lisp-indent-function 0) | |
756 | (put 'save-window-excursion 'lisp-indent-function 0) | |
4b619eca | 757 | (put 'save-selected-window 'lisp-indent-function 0) |
a90256cc | 758 | (put 'save-restriction 'lisp-indent-function 0) |
cfe158ab | 759 | (put 'save-match-data 'lisp-indent-function 0) |
38f16fe1 | 760 | (put 'save-current-buffer 'lisp-indent-function 0) |
54c014f0 | 761 | (put 'with-current-buffer 'lisp-indent-function 1) |
488a0b05 | 762 | (put 'combine-after-change-calls 'lisp-indent-function 0) |
38f16fe1 | 763 | (put 'with-output-to-string 'lisp-indent-function 0) |
08adb099 | 764 | (put 'with-temp-file 'lisp-indent-function 1) |
e0119683 | 765 | (put 'with-temp-buffer 'lisp-indent-function 0) |
bacd83a7 | 766 | (put 'with-temp-message 'lisp-indent-function 1) |
83c8f461 | 767 | (put 'with-syntax-table 'lisp-indent-function 1) |
a90256cc BP |
768 | (put 'let 'lisp-indent-function 1) |
769 | (put 'let* 'lisp-indent-function 1) | |
770 | (put 'while 'lisp-indent-function 1) | |
771 | (put 'if 'lisp-indent-function 2) | |
772 | (put 'catch 'lisp-indent-function 1) | |
773 | (put 'condition-case 'lisp-indent-function 2) | |
774 | (put 'unwind-protect 'lisp-indent-function 1) | |
775 | (put 'with-output-to-temp-buffer 'lisp-indent-function 1) | |
b6b4c8bd | 776 | (put 'eval-after-load 'lisp-indent-function 1) |
05c71036 DL |
777 | (put 'dolist 'lisp-indent-function 1) |
778 | (put 'dotimes 'lisp-indent-function 1) | |
779 | (put 'when 'lisp-indent-function 1) | |
780 | (put 'unless 'lisp-indent-function 1) | |
a90256cc BP |
781 | |
782 | (defun indent-sexp (&optional endpos) | |
783 | "Indent each line of the list starting just after point. | |
784 | If optional arg ENDPOS is given, indent each line, stopping when | |
785 | ENDPOS is encountered." | |
786 | (interactive) | |
daa37602 JB |
787 | (let ((indent-stack (list nil)) |
788 | (next-depth 0) | |
33f268ec RS |
789 | ;; If ENDPOS is non-nil, use nil as STARTING-POINT |
790 | ;; so that calculate-lisp-indent will find the beginning of | |
791 | ;; the defun we are in. | |
792 | ;; If ENDPOS is nil, it is safe not to scan before point | |
793 | ;; since every line we indent is more deeply nested than point is. | |
794 | (starting-point (if endpos nil (point))) | |
daa37602 JB |
795 | (last-point (point)) |
796 | last-depth bol outer-loop-done inner-loop-done state this-indent) | |
33f268ec RS |
797 | (or endpos |
798 | ;; Get error now if we don't have a complete sexp after point. | |
799 | (save-excursion (forward-sexp 1))) | |
a90256cc BP |
800 | (save-excursion |
801 | (setq outer-loop-done nil) | |
802 | (while (if endpos (< (point) endpos) | |
803 | (not outer-loop-done)) | |
804 | (setq last-depth next-depth | |
805 | inner-loop-done nil) | |
806 | ;; Parse this line so we can learn the state | |
807 | ;; to indent the next line. | |
808 | ;; This inner loop goes through only once | |
809 | ;; unless a line ends inside a string. | |
810 | (while (and (not inner-loop-done) | |
811 | (not (setq outer-loop-done (eobp)))) | |
812 | (setq state (parse-partial-sexp (point) (progn (end-of-line) (point)) | |
813 | nil nil state)) | |
814 | (setq next-depth (car state)) | |
815 | ;; If the line contains a comment other than the sort | |
816 | ;; that is indented like code, | |
817 | ;; indent it now with indent-for-comment. | |
818 | ;; Comments indented like code are right already. | |
819 | ;; In any case clear the in-comment flag in the state | |
820 | ;; because parse-partial-sexp never sees the newlines. | |
821 | (if (car (nthcdr 4 state)) | |
822 | (progn (indent-for-comment) | |
823 | (end-of-line) | |
824 | (setcar (nthcdr 4 state) nil))) | |
825 | ;; If this line ends inside a string, | |
826 | ;; go straight to next line, remaining within the inner loop, | |
827 | ;; and turn off the \-flag. | |
828 | (if (car (nthcdr 3 state)) | |
829 | (progn | |
830 | (forward-line 1) | |
831 | (setcar (nthcdr 5 state) nil)) | |
832 | (setq inner-loop-done t))) | |
833 | (and endpos | |
daa37602 JB |
834 | (<= next-depth 0) |
835 | (progn | |
99ec65b3 DL |
836 | (setq indent-stack (nconc indent-stack |
837 | (make-list (- next-depth) nil)) | |
daa37602 JB |
838 | last-depth (- last-depth next-depth) |
839 | next-depth 0))) | |
33f268ec | 840 | (or outer-loop-done endpos |
a90256cc BP |
841 | (setq outer-loop-done (<= next-depth 0))) |
842 | (if outer-loop-done | |
1f5038b5 | 843 | (forward-line 1) |
a90256cc BP |
844 | (while (> last-depth next-depth) |
845 | (setq indent-stack (cdr indent-stack) | |
846 | last-depth (1- last-depth))) | |
847 | (while (< last-depth next-depth) | |
848 | (setq indent-stack (cons nil indent-stack) | |
849 | last-depth (1+ last-depth))) | |
850 | ;; Now go to the next line and indent it according | |
851 | ;; to what we learned from parsing the previous one. | |
852 | (forward-line 1) | |
853 | (setq bol (point)) | |
854 | (skip-chars-forward " \t") | |
855 | ;; But not if the line is blank, or just a comment | |
856 | ;; (except for double-semi comments; indent them as usual). | |
857 | (if (or (eobp) (looking-at "\\s<\\|\n")) | |
858 | nil | |
859 | (if (and (car indent-stack) | |
860 | (>= (car indent-stack) 0)) | |
861 | (setq this-indent (car indent-stack)) | |
862 | (let ((val (calculate-lisp-indent | |
863 | (if (car indent-stack) (- (car indent-stack)) | |
daa37602 | 864 | starting-point)))) |
531cbff1 RS |
865 | (if (null val) |
866 | (setq this-indent val) | |
867 | (if (integerp val) | |
868 | (setcar indent-stack | |
869 | (setq this-indent val)) | |
870 | (setcar indent-stack (- (car (cdr val)))) | |
871 | (setq this-indent (car val)))))) | |
872 | (if (and this-indent (/= (current-column) this-indent)) | |
a90256cc BP |
873 | (progn (delete-region bol (point)) |
874 | (indent-to this-indent))))) | |
875 | (or outer-loop-done | |
876 | (setq outer-loop-done (= (point) last-point)) | |
877 | (setq last-point (point))))))) | |
878 | ||
a90256cc | 879 | (defun lisp-indent-region (start end) |
535eadac | 880 | "Indent every line whose first char is between START and END inclusive." |
a90256cc | 881 | (save-excursion |
a90256cc | 882 | (let ((endmark (copy-marker end))) |
33f268ec RS |
883 | (goto-char start) |
884 | (and (bolp) (not (eolp)) | |
885 | (lisp-indent-line)) | |
a90256cc BP |
886 | (indent-sexp endmark) |
887 | (set-marker endmark nil)))) | |
ca2ddd8e | 888 | |
6338c7ba JB |
889 | ;;;; Lisp paragraph filling commands. |
890 | ||
891 | (defun lisp-fill-paragraph (&optional justify) | |
892 | "Like \\[fill-paragraph], but handle Emacs Lisp comments. | |
893 | If any of the current line is a comment, fill the comment or the | |
894 | paragraph of it that point is in, preserving the comment's indentation | |
895 | and initial semicolons." | |
896 | (interactive "P") | |
897 | (let ( | |
898 | ;; Non-nil if the current line contains a comment. | |
899 | has-comment | |
900 | ||
4d2790ad RS |
901 | ;; Non-nil if the current line contains code and a comment. |
902 | has-code-and-comment | |
903 | ||
6338c7ba JB |
904 | ;; If has-comment, the appropriate fill-prefix for the comment. |
905 | comment-fill-prefix | |
906 | ) | |
907 | ||
908 | ;; Figure out what kind of comment we are looking at. | |
909 | (save-excursion | |
910 | (beginning-of-line) | |
911 | (cond | |
912 | ||
913 | ;; A line with nothing but a comment on it? | |
914 | ((looking-at "[ \t]*;[; \t]*") | |
915 | (setq has-comment t | |
916 | comment-fill-prefix (buffer-substring (match-beginning 0) | |
917 | (match-end 0)))) | |
918 | ||
919 | ;; A line with some code, followed by a comment? Remember that the | |
920 | ;; semi which starts the comment shouldn't be part of a string or | |
921 | ;; character. | |
93353fea KH |
922 | ((condition-case nil |
923 | (save-restriction | |
924 | (narrow-to-region (point-min) | |
925 | (save-excursion (end-of-line) (point))) | |
926 | (while (not (looking-at ";\\|$")) | |
927 | (skip-chars-forward "^;\n\"\\\\?") | |
928 | (cond | |
929 | ((eq (char-after (point)) ?\\) (forward-char 2)) | |
930 | ((memq (char-after (point)) '(?\" ??)) (forward-sexp 1)))) | |
931 | (looking-at ";+[\t ]*")) | |
932 | (error nil)) | |
4d2790ad | 933 | (setq has-comment t has-code-and-comment t) |
6338c7ba | 934 | (setq comment-fill-prefix |
93353fea KH |
935 | (concat (make-string (/ (current-column) 8) ?\t) |
936 | (make-string (% (current-column) 8) ?\ ) | |
6338c7ba JB |
937 | (buffer-substring (match-beginning 0) (match-end 0))))))) |
938 | ||
939 | (if (not has-comment) | |
fe3188ec DL |
940 | ;; `paragraph-start' is set here (not in the buffer-local |
941 | ;; variable so that `forward-paragraph' et al work as | |
942 | ;; expected) so that filling (doc) strings works sensibly. | |
943 | ;; Adding the opening paren to avoid the following sexp being | |
944 | ;; filled means that sexps generally aren't filled as normal | |
945 | ;; text, which is probably sensible. The `;' and `:' stop the | |
946 | ;; filled para at following comment lines and keywords | |
947 | ;; (typically in `defcustom'). | |
948 | (let ((paragraph-start (concat paragraph-start | |
949 | "\\|\\s-*[\(;:\"]"))) | |
950 | (fill-paragraph justify)) | |
6338c7ba JB |
951 | |
952 | ;; Narrow to include only the comment, and then fill the region. | |
4c06fbee KH |
953 | (save-excursion |
954 | (save-restriction | |
955 | (beginning-of-line) | |
956 | (narrow-to-region | |
957 | ;; Find the first line we should include in the region to fill. | |
958 | (save-excursion | |
959 | (while (and (zerop (forward-line -1)) | |
6338c7ba | 960 | (looking-at "^[ \t]*;"))) |
4c06fbee KH |
961 | ;; We may have gone too far. Go forward again. |
962 | (or (looking-at ".*;") | |
963 | (forward-line 1)) | |
964 | (point)) | |
965 | ;; Find the beginning of the first line past the region to fill. | |
966 | (save-excursion | |
967 | (while (progn (forward-line 1) | |
968 | (looking-at "^[ \t]*;"))) | |
969 | (point))) | |
970 | ||
971 | ;; Lines with only semicolons on them can be paragraph boundaries. | |
972 | (let* ((paragraph-start (concat paragraph-start "\\|[ \t;]*$")) | |
973 | (paragraph-separate (concat paragraph-start "\\|[ \t;]*$")) | |
974 | (paragraph-ignore-fill-prefix nil) | |
975 | (fill-prefix comment-fill-prefix) | |
4d2790ad RS |
976 | (after-line (if has-code-and-comment |
977 | (save-excursion | |
978 | (forward-line 1) (point)))) | |
4c06fbee KH |
979 | (end (progn |
980 | (forward-paragraph) | |
981 | (or (bolp) (newline 1)) | |
982 | (point))) | |
4d2790ad RS |
983 | ;; If this comment starts on a line with code, |
984 | ;; include that like in the filling. | |
985 | (beg (progn (backward-paragraph) | |
986 | (if (eq (point) after-line) | |
987 | (forward-line -1)) | |
988 | (point)))) | |
4c06fbee KH |
989 | (fill-region-as-paragraph beg end |
990 | justify nil | |
991 | (save-excursion | |
992 | (goto-char beg) | |
993 | (if (looking-at fill-prefix) | |
994 | nil | |
995 | (re-search-forward comment-start-skip) | |
996 | (point)))))))) | |
2b4483bb | 997 | t)) |
ca2ddd8e | 998 | |
a90256cc BP |
999 | (defun indent-code-rigidly (start end arg &optional nochange-regexp) |
1000 | "Indent all lines of code, starting in the region, sideways by ARG columns. | |
1001 | Does not affect lines starting inside comments or strings, assuming that | |
1002 | the start of the region is not inside them. | |
1003 | ||
1004 | Called from a program, takes args START, END, COLUMNS and NOCHANGE-REGEXP. | |
1005 | The last is a regexp which, if matched at the beginning of a line, | |
1006 | means don't indent that line." | |
1007 | (interactive "r\np") | |
1008 | (let (state) | |
1009 | (save-excursion | |
1010 | (goto-char end) | |
1011 | (setq end (point-marker)) | |
1012 | (goto-char start) | |
1013 | (or (bolp) | |
1014 | (setq state (parse-partial-sexp (point) | |
1015 | (progn | |
1016 | (forward-line 1) (point)) | |
1017 | nil nil state))) | |
1018 | (while (< (point) end) | |
1019 | (or (car (nthcdr 3 state)) | |
1020 | (and nochange-regexp | |
1021 | (looking-at nochange-regexp)) | |
1022 | ;; If line does not start in string, indent it | |
1023 | (let ((indent (current-indentation))) | |
1024 | (delete-region (point) (progn (skip-chars-forward " \t") (point))) | |
1025 | (or (eolp) | |
1026 | (indent-to (max 0 (+ indent arg)) 0)))) | |
1027 | (setq state (parse-partial-sexp (point) | |
1028 | (progn | |
1029 | (forward-line 1) (point)) | |
1030 | nil nil state)))))) | |
49116ac0 JB |
1031 | |
1032 | (provide 'lisp-mode) | |
1033 | ||
6594deb0 | 1034 | ;;; lisp-mode.el ends here |