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