Commit | Line | Data |
---|---|---|
55535639 | 1 | ;;; lisp-mode.el --- Lisp mode, and its idiosyncratic commands |
6594deb0 | 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 () |
343462ed EZ |
311 | "Evaluate sexp before point; print value into current buffer. |
312 | ||
313 | Note that printing the result is controlled by the variables | |
314 | `eval-expression-print-length' and `eval-expression-print-level', | |
315 | which see." | |
121f0d57 | 316 | (interactive) |
f798d950 JB |
317 | (let ((standard-output (current-buffer))) |
318 | (terpri) | |
319 | (eval-last-sexp t) | |
320 | (terpri))) | |
ca2ddd8e | 321 | |
5f096255 GM |
322 | |
323 | (defun last-sexp-print () | |
324 | (interactive) | |
325 | (let ((value (get-text-property (point) 'printed-value))) | |
326 | (when value | |
327 | (let ((beg (previous-single-property-change (point) 'printed-value)) | |
328 | (end (next-single-char-property-change (point) 'printed-value)) | |
8f4f953c GM |
329 | (standard-output (current-buffer)) |
330 | (print-length nil) | |
331 | (print-level nil)) | |
5f096255 GM |
332 | (delete-region beg end) |
333 | (prin1 value))))) | |
334 | ||
335 | ||
99c6d63b | 336 | (defun eval-last-sexp-1 (eval-last-sexp-arg-internal) |
a90256cc BP |
337 | "Evaluate sexp before point; print value in minibuffer. |
338 | With argument, print output into current buffer." | |
99c6d63b | 339 | (let ((standard-output (if eval-last-sexp-arg-internal (current-buffer) t))) |
efb195f0 RS |
340 | (let ((value |
341 | (eval (let ((stab (syntax-table)) | |
361721f2 | 342 | (opoint (point)) |
05e94d32 | 343 | ignore-quotes |
361721f2 KH |
344 | expr) |
345 | (unwind-protect | |
19b2f8f1 RM |
346 | (save-excursion |
347 | (set-syntax-table emacs-lisp-mode-syntax-table) | |
05e94d32 RS |
348 | ;; If this sexp appears to be enclosed in `...' |
349 | ;; then ignore the surrounding quotes. | |
350 | (setq ignore-quotes | |
351 | (or (eq (following-char) ?\') | |
352 | (eq (preceding-char) ?\'))) | |
19b2f8f1 | 353 | (forward-sexp -1) |
19b014e4 RS |
354 | ;; If we were after `?\e' (or similar case), |
355 | ;; use the whole thing, not just the `e'. | |
356 | (when (eq (preceding-char) ?\\) | |
357 | (forward-char -1) | |
358 | (when (eq (preceding-char) ??) | |
359 | (forward-char -1))) | |
ca2ddd8e | 360 | |
07ca56eb GM |
361 | ;; Skip over `#N='s. |
362 | (when (eq (preceding-char) ?=) | |
363 | (let (labeled-p) | |
364 | (save-excursion | |
365 | (skip-chars-backward "0-9#=") | |
366 | (setq labeled-p (looking-at "\\(#[0-9]+=\\)+"))) | |
367 | (when labeled-p | |
368 | (forward-sexp -1)))) | |
369 | ||
cfe158ab | 370 | (save-restriction |
05e94d32 RS |
371 | ;; vladimir@cs.ualberta.ca 30-Jul-1997: skip ` in |
372 | ;; `variable' so that the value is returned, not the | |
ca2ddd8e | 373 | ;; name |
05e94d32 RS |
374 | (if (and ignore-quotes |
375 | (eq (following-char) ?`)) | |
376 | (forward-char)) | |
cfe158ab | 377 | (narrow-to-region (point-min) opoint) |
361721f2 KH |
378 | (setq expr (read (current-buffer))) |
379 | ;; If it's an (interactive ...) form, it's more | |
380 | ;; useful to show how an interactive call would | |
381 | ;; use it. | |
382 | (and (consp expr) | |
383 | (eq (car expr) 'interactive) | |
384 | (setq expr | |
385 | (list 'call-interactively | |
386 | (list 'quote | |
387 | (list 'lambda | |
388 | '(&rest args) | |
389 | expr | |
390 | 'args))))) | |
391 | expr)) | |
efb195f0 | 392 | (set-syntax-table stab)))))) |
8f4f953c GM |
393 | (let ((unabbreviated (let ((print-length nil) (print-level nil)) |
394 | (prin1-to-string value))) | |
395 | (print-length eval-expression-print-length) | |
6849e6d5 | 396 | (print-level eval-expression-print-level) |
be4a68f9 GM |
397 | (beg (point)) |
398 | end) | |
6849e6d5 | 399 | (prin1 value) |
be4a68f9 | 400 | (setq end (point)) |
5f096255 GM |
401 | (when (and (bufferp standard-output) |
402 | (or (not (null print-length)) | |
be4a68f9 | 403 | (not (null print-level))) |
8f4f953c | 404 | (not (string= unabbreviated (buffer-substring beg end)))) |
be4a68f9 | 405 | (let ((map (make-sparse-keymap))) |
5f096255 GM |
406 | (define-key map "\C-m" 'last-sexp-print) |
407 | (define-key map [down-mouse-2] 'mouse-set-point) | |
408 | (define-key map [mouse-2] 'last-sexp-print) | |
409 | (add-text-properties | |
410 | beg end | |
411 | `(printed-value ,value | |
412 | mouse-face highlight | |
413 | keymap ,map | |
be4a68f9 GM |
414 | help-echo "RET, mouse-2: print unabbreviated" |
415 | read-nonsticky (mouse-face keymap help-echo | |
416 | printed-value) | |
417 | )))))))) | |
5f096255 | 418 | |
a90256cc | 419 | |
99c6d63b GM |
420 | (defun eval-last-sexp (eval-last-sexp-arg-internal) |
421 | "Evaluate sexp before point; print value in minibuffer. | |
0202508f | 422 | Interactively, with prefix argument, print output into current buffer." |
99c6d63b GM |
423 | (interactive "P") |
424 | (if (null eval-expression-debug-on-error) | |
425 | (eval-last-sexp-1 eval-last-sexp-arg-internal) | |
426 | (let ((old-value (make-symbol "t")) new-value value) | |
427 | (let ((debug-on-error old-value)) | |
428 | (setq value (eval-last-sexp-1 eval-last-sexp-arg-internal)) | |
429 | (setq new-value debug-on-error)) | |
430 | (unless (eq old-value new-value) | |
431 | (setq debug-on-error new-value)) | |
432 | value))) | |
ca2ddd8e | 433 | |
151a410a | 434 | (defun eval-defun-1 (form) |
99ec65b3 DL |
435 | "Change defvar into defconst within FORM. |
436 | Likewise for other constructs as necessary." | |
437 | ;; The code in edebug-defun should be consistent with this, but not | |
438 | ;; the same, since this gets a macroexpended form. | |
151a410a RS |
439 | (cond ((and (eq (car form) 'defvar) |
440 | (cdr-safe (cdr-safe form))) | |
441 | ;; Force variable to be bound. | |
442 | (cons 'defconst (cdr form))) | |
5e871da0 DL |
443 | ;; `defcustom' is now macroexpanded to |
444 | ;; `custom-declare-variable' with a quoted value arg. | |
535eadac DL |
445 | ((and (eq (car form) 'custom-declare-variable) |
446 | (default-boundp (eval (nth 1 form)))) | |
151a410a | 447 | ;; Force variable to be bound. |
5e871da0 | 448 | (set-default (eval (nth 1 form)) (eval (nth 1 (nth 2 form)))) |
151a410a RS |
449 | form) |
450 | ((eq (car form) 'progn) | |
451 | (cons 'progn (mapcar 'eval-defun-1 (cdr form)))) | |
452 | (t form))) | |
453 | ||
105d6be1 | 454 | (defun eval-defun-2 () |
121f0d57 | 455 | "Evaluate defun that point is in or before. |
ebc03d28 KH |
456 | The value is displayed in the minibuffer. |
457 | If the current defun is actually a call to `defvar', | |
458 | then reset the variable using the initial value expression | |
459 | even if the variable already has some other value. | |
460 | \(Normally `defvar' does not change the variable's value | |
461 | if it already has a value.\) | |
462 | ||
2298f9f7 KH |
463 | With argument, insert value in current buffer after the defun. |
464 | Return the result of evaluation." | |
a90256cc | 465 | (interactive "P") |
efb195f0 RS |
466 | (let ((debug-on-error eval-expression-debug-on-error) |
467 | (print-length eval-expression-print-length) | |
468 | (print-level eval-expression-print-level)) | |
469 | (save-excursion | |
470 | ;; Arrange for eval-region to "read" the (possibly) altered form. | |
471 | ;; eval-region handles recording which file defines a function or | |
472 | ;; variable. Re-written using `apply' to avoid capturing | |
473 | ;; variables like `end'. | |
474 | (apply | |
ca2ddd8e | 475 | #'eval-region |
105d6be1 | 476 | (let ((standard-output t) |
efb195f0 RS |
477 | beg end form) |
478 | ;; Read the form from the buffer, and record where it ends. | |
479 | (save-excursion | |
480 | (end-of-defun) | |
481 | (beginning-of-defun) | |
482 | (setq beg (point)) | |
483 | (setq form (read (current-buffer))) | |
484 | (setq end (point))) | |
485 | ;; Alter the form if necessary, changing defvar into defconst, etc. | |
486 | (setq form (eval-defun-1 (macroexpand form))) | |
487 | (list beg end standard-output | |
488 | `(lambda (ignore) | |
489 | ;; Skipping to the end of the specified region | |
490 | ;; will make eval-region return. | |
491 | (goto-char ,end) | |
492 | ',form)))))) | |
713c3fb1 DL |
493 | ;; The result of evaluation has been put onto VALUES. So return it. |
494 | (car values)) | |
99c6d63b | 495 | |
105d6be1 GM |
496 | (defun eval-defun (edebug-it) |
497 | "Evaluate the top-level form containing point, or after point. | |
99c6d63b | 498 | |
105d6be1 GM |
499 | If the current defun is actually a call to `defvar', then reset the |
500 | variable using its initial value expression even if the variable | |
501 | already has some other value. (Normally `defvar' does not change the | |
502 | variable's value if it already has a value.) | |
503 | ||
504 | With a prefix argument, instrument the code for Edebug. | |
505 | ||
506 | If acting on a `defun' for FUNCTION, and the function was | |
507 | instrumented, `Edebug: FUNCTION' is printed in the minibuffer. If not | |
508 | instrumented, just FUNCTION is printed. | |
509 | ||
510 | If not acting on a `defun', the result of evaluation is displayed in | |
343462ed EZ |
511 | the minibuffer. This display is controlled by the variables |
512 | `eval-expression-print-length' and `eval-expression-print-level', | |
513 | which see." | |
99c6d63b | 514 | (interactive "P") |
105d6be1 GM |
515 | (cond (edebug-it |
516 | (require 'edebug) | |
517 | (eval-defun (not edebug-all-defs))) | |
518 | (t | |
519 | (if (null eval-expression-debug-on-error) | |
520 | (eval-defun-2) | |
521 | (let ((old-value (make-symbol "t")) new-value value) | |
522 | (let ((debug-on-error old-value)) | |
523 | (setq value (eval-defun-2)) | |
524 | (setq new-value debug-on-error)) | |
525 | (unless (eq old-value new-value) | |
526 | (setq debug-on-error new-value)) | |
527 | value))))) | |
99c6d63b | 528 | |
ca2ddd8e | 529 | |
a90256cc BP |
530 | (defun lisp-comment-indent () |
531 | (if (looking-at "\\s<\\s<\\s<") | |
532 | (current-column) | |
533 | (if (looking-at "\\s<\\s<") | |
531cbff1 | 534 | (let ((tem (or (calculate-lisp-indent) (current-column)))) |
a90256cc BP |
535 | (if (listp tem) (car tem) tem)) |
536 | (skip-chars-backward " \t") | |
537 | (max (if (bolp) 0 (1+ (current-column))) | |
538 | comment-column)))) | |
539 | ||
7eb67d79 RS |
540 | (defun lisp-mode-auto-fill () |
541 | (if (> (current-column) (current-fill-column)) | |
542 | (if (save-excursion | |
543 | (nth 4 (parse-partial-sexp (save-excursion | |
544 | (beginning-of-defun) | |
545 | (point)) | |
546 | (point)))) | |
547 | (do-auto-fill) | |
548 | (let ((comment-start nil) (comment-start-skip nil)) | |
549 | (do-auto-fill))))) | |
550 | ||
cada28bb EZ |
551 | (defvar lisp-indent-offset nil |
552 | "If non-nil, indent second line of expressions that many more columns.") | |
535eadac | 553 | (defvar lisp-indent-function 'lisp-indent-function) |
a90256cc BP |
554 | |
555 | (defun lisp-indent-line (&optional whole-exp) | |
556 | "Indent current line as Lisp code. | |
557 | With argument, indent any additional lines of the same expression | |
558 | rigidly along with this one." | |
559 | (interactive "P") | |
560 | (let ((indent (calculate-lisp-indent)) shift-amt beg end | |
561 | (pos (- (point-max) (point)))) | |
562 | (beginning-of-line) | |
563 | (setq beg (point)) | |
564 | (skip-chars-forward " \t") | |
531cbff1 RS |
565 | (if (or (null indent) (looking-at "\\s<\\s<\\s<")) |
566 | ;; Don't alter indentation of a ;;; comment line | |
567 | ;; or a line that starts in a string. | |
328561fc | 568 | (goto-char (- (point-max) pos)) |
a90256cc BP |
569 | (if (and (looking-at "\\s<") (not (looking-at "\\s<\\s<"))) |
570 | ;; Single-semicolon comment lines should be indented | |
571 | ;; as comment lines, not as code. | |
572 | (progn (indent-for-comment) (forward-char -1)) | |
573 | (if (listp indent) (setq indent (car indent))) | |
574 | (setq shift-amt (- indent (current-column))) | |
575 | (if (zerop shift-amt) | |
576 | nil | |
577 | (delete-region beg (point)) | |
578 | (indent-to indent))) | |
579 | ;; If initial point was within line's indentation, | |
580 | ;; position after the indentation. Else stay at same point in text. | |
581 | (if (> (- (point-max) pos) (point)) | |
582 | (goto-char (- (point-max) pos))) | |
583 | ;; If desired, shift remaining lines of expression the same amount. | |
584 | (and whole-exp (not (zerop shift-amt)) | |
585 | (save-excursion | |
586 | (goto-char beg) | |
587 | (forward-sexp 1) | |
588 | (setq end (point)) | |
589 | (goto-char beg) | |
590 | (forward-line 1) | |
591 | (setq beg (point)) | |
592 | (> end beg)) | |
593 | (indent-code-rigidly beg end shift-amt))))) | |
594 | ||
22486a7f | 595 | (defvar calculate-lisp-indent-last-sexp) |
c0df1d61 | 596 | |
a90256cc BP |
597 | (defun calculate-lisp-indent (&optional parse-start) |
598 | "Return appropriate indentation for current line as Lisp code. | |
599 | In usual case returns an integer: the column to indent to. | |
531cbff1 RS |
600 | If the value is nil, that means don't change the indentation |
601 | because the line starts inside a string. | |
602 | ||
603 | The value can also be a list of the form (COLUMN CONTAINING-SEXP-START). | |
a90256cc | 604 | This means that following lines at the same level of indentation |
531cbff1 RS |
605 | should not necessarily be indented the same as this line. |
606 | Then COLUMN is the column to indent to, and CONTAINING-SEXP-START | |
607 | is the buffer position of the start of the containing expression." | |
a90256cc BP |
608 | (save-excursion |
609 | (beginning-of-line) | |
610 | (let ((indent-point (point)) | |
611 | state paren-depth | |
612 | ;; setting this to a number inhibits calling hook | |
613 | (desired-indent nil) | |
614 | (retry t) | |
c0df1d61 | 615 | calculate-lisp-indent-last-sexp containing-sexp) |
a90256cc BP |
616 | (if parse-start |
617 | (goto-char parse-start) | |
618 | (beginning-of-defun)) | |
619 | ;; Find outermost containing sexp | |
620 | (while (< (point) indent-point) | |
621 | (setq state (parse-partial-sexp (point) indent-point 0))) | |
622 | ;; Find innermost containing sexp | |
623 | (while (and retry | |
624 | state | |
625 | (> (setq paren-depth (elt state 0)) 0)) | |
626 | (setq retry nil) | |
c0df1d61 | 627 | (setq calculate-lisp-indent-last-sexp (elt state 2)) |
a90256cc BP |
628 | (setq containing-sexp (elt state 1)) |
629 | ;; Position following last unclosed open. | |
630 | (goto-char (1+ containing-sexp)) | |
631 | ;; Is there a complete sexp since then? | |
c0df1d61 RS |
632 | (if (and calculate-lisp-indent-last-sexp |
633 | (> calculate-lisp-indent-last-sexp (point))) | |
a90256cc | 634 | ;; Yes, but is there a containing sexp after that? |
c0df1d61 RS |
635 | (let ((peek (parse-partial-sexp calculate-lisp-indent-last-sexp |
636 | indent-point 0))) | |
a90256cc BP |
637 | (if (setq retry (car (cdr peek))) (setq state peek))))) |
638 | (if retry | |
639 | nil | |
640 | ;; Innermost containing sexp found | |
641 | (goto-char (1+ containing-sexp)) | |
c0df1d61 | 642 | (if (not calculate-lisp-indent-last-sexp) |
a90256cc BP |
643 | ;; indent-point immediately follows open paren. |
644 | ;; Don't call hook. | |
645 | (setq desired-indent (current-column)) | |
646 | ;; Find the start of first element of containing sexp. | |
c0df1d61 | 647 | (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t) |
a90256cc BP |
648 | (cond ((looking-at "\\s(") |
649 | ;; First element of containing sexp is a list. | |
650 | ;; Indent under that list. | |
651 | ) | |
652 | ((> (save-excursion (forward-line 1) (point)) | |
c0df1d61 | 653 | calculate-lisp-indent-last-sexp) |
a90256cc BP |
654 | ;; This is the first line to start within the containing sexp. |
655 | ;; It's almost certainly a function call. | |
c0df1d61 | 656 | (if (= (point) calculate-lisp-indent-last-sexp) |
a90256cc BP |
657 | ;; Containing sexp has nothing before this line |
658 | ;; except the first element. Indent under that element. | |
659 | nil | |
660 | ;; Skip the first element, find start of second (the first | |
661 | ;; argument of the function call) and indent under. | |
662 | (progn (forward-sexp 1) | |
c0df1d61 RS |
663 | (parse-partial-sexp (point) |
664 | calculate-lisp-indent-last-sexp | |
665 | 0 t))) | |
a90256cc BP |
666 | (backward-prefix-chars)) |
667 | (t | |
c0df1d61 | 668 | ;; Indent beneath first sexp on same line as |
535eadac | 669 | ;; `calculate-lisp-indent-last-sexp'. Again, it's |
c0df1d61 RS |
670 | ;; almost certainly a function call. |
671 | (goto-char calculate-lisp-indent-last-sexp) | |
a90256cc | 672 | (beginning-of-line) |
c0df1d61 RS |
673 | (parse-partial-sexp (point) calculate-lisp-indent-last-sexp |
674 | 0 t) | |
a90256cc BP |
675 | (backward-prefix-chars))))) |
676 | ;; Point is at the point to indent under unless we are inside a string. | |
eb8c3be9 | 677 | ;; Call indentation hook except when overridden by lisp-indent-offset |
a90256cc BP |
678 | ;; or if the desired indentation has already been computed. |
679 | (let ((normal-indent (current-column))) | |
680 | (cond ((elt state 3) | |
681 | ;; Inside a string, don't change indentation. | |
531cbff1 | 682 | nil) |
a90256cc BP |
683 | ((and (integerp lisp-indent-offset) containing-sexp) |
684 | ;; Indent by constant offset | |
685 | (goto-char containing-sexp) | |
686 | (+ (current-column) lisp-indent-offset)) | |
687 | (desired-indent) | |
688 | ((and (boundp 'lisp-indent-function) | |
689 | lisp-indent-function | |
690 | (not retry)) | |
691 | (or (funcall lisp-indent-function indent-point state) | |
692 | normal-indent)) | |
693 | (t | |
f30ff39f | 694 | normal-indent)))))) |
a90256cc BP |
695 | |
696 | (defun lisp-indent-function (indent-point state) | |
697 | (let ((normal-indent (current-column))) | |
698 | (goto-char (1+ (elt state 1))) | |
c0df1d61 | 699 | (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t) |
a90256cc BP |
700 | (if (and (elt state 2) |
701 | (not (looking-at "\\sw\\|\\s_"))) | |
702 | ;; car of form doesn't seem to be a a symbol | |
703 | (progn | |
704 | (if (not (> (save-excursion (forward-line 1) (point)) | |
c0df1d61 RS |
705 | calculate-lisp-indent-last-sexp)) |
706 | (progn (goto-char calculate-lisp-indent-last-sexp) | |
a90256cc | 707 | (beginning-of-line) |
c0df1d61 RS |
708 | (parse-partial-sexp (point) |
709 | calculate-lisp-indent-last-sexp 0 t))) | |
710 | ;; Indent under the list or under the first sexp on the same | |
711 | ;; line as calculate-lisp-indent-last-sexp. Note that first | |
712 | ;; thing on that line has to be complete sexp since we are | |
713 | ;; inside the innermost containing sexp. | |
a90256cc BP |
714 | (backward-prefix-chars) |
715 | (current-column)) | |
716 | (let ((function (buffer-substring (point) | |
717 | (progn (forward-sexp 1) (point)))) | |
718 | method) | |
2dab7802 RS |
719 | (setq method (or (get (intern-soft function) 'lisp-indent-function) |
720 | (get (intern-soft function) 'lisp-indent-hook))) | |
a90256cc BP |
721 | (cond ((or (eq method 'defun) |
722 | (and (null method) | |
723 | (> (length function) 3) | |
724 | (string-match "\\`def" function))) | |
725 | (lisp-indent-defform state indent-point)) | |
726 | ((integerp method) | |
727 | (lisp-indent-specform method state | |
728 | indent-point normal-indent)) | |
729 | (method | |
730 | (funcall method state indent-point))))))) | |
731 | ||
d7fa5aa2 | 732 | (defvar lisp-body-indent 2 |
ab69b2fb | 733 | "Number of columns to indent the second line of a `(def...)' form.") |
a90256cc BP |
734 | |
735 | (defun lisp-indent-specform (count state indent-point normal-indent) | |
736 | (let ((containing-form-start (elt state 1)) | |
737 | (i count) | |
738 | body-indent containing-form-column) | |
739 | ;; Move to the start of containing form, calculate indentation | |
740 | ;; to use for non-distinguished forms (> count), and move past the | |
741 | ;; function symbol. lisp-indent-function guarantees that there is at | |
742 | ;; least one word or symbol character following open paren of containing | |
743 | ;; form. | |
744 | (goto-char containing-form-start) | |
745 | (setq containing-form-column (current-column)) | |
746 | (setq body-indent (+ lisp-body-indent containing-form-column)) | |
747 | (forward-char 1) | |
748 | (forward-sexp 1) | |
749 | ;; Now find the start of the last form. | |
750 | (parse-partial-sexp (point) indent-point 1 t) | |
751 | (while (and (< (point) indent-point) | |
752 | (condition-case () | |
753 | (progn | |
754 | (setq count (1- count)) | |
755 | (forward-sexp 1) | |
756 | (parse-partial-sexp (point) indent-point 1 t)) | |
757 | (error nil)))) | |
758 | ;; Point is sitting on first character of last (or count) sexp. | |
759 | (if (> count 0) | |
760 | ;; A distinguished form. If it is the first or second form use double | |
761 | ;; lisp-body-indent, else normal indent. With lisp-body-indent bound | |
762 | ;; to 2 (the default), this just happens to work the same with if as | |
763 | ;; the older code, but it makes unwind-protect, condition-case, | |
764 | ;; with-output-to-temp-buffer, et. al. much more tasteful. The older, | |
765 | ;; less hacked, behavior can be obtained by replacing below with | |
766 | ;; (list normal-indent containing-form-start). | |
767 | (if (<= (- i count) 1) | |
768 | (list (+ containing-form-column (* 2 lisp-body-indent)) | |
769 | containing-form-start) | |
770 | (list normal-indent containing-form-start)) | |
771 | ;; A non-distinguished form. Use body-indent if there are no | |
772 | ;; distinguished forms and this is the first undistinguished form, | |
773 | ;; or if this is the first undistinguished form and the preceding | |
774 | ;; distinguished form has indentation at least as great as body-indent. | |
775 | (if (or (and (= i 0) (= count 0)) | |
776 | (and (= count 0) (<= body-indent normal-indent))) | |
777 | body-indent | |
778 | normal-indent)))) | |
779 | ||
780 | (defun lisp-indent-defform (state indent-point) | |
781 | (goto-char (car (cdr state))) | |
782 | (forward-line 1) | |
783 | (if (> (point) (car (cdr (cdr state)))) | |
784 | (progn | |
785 | (goto-char (car (cdr state))) | |
786 | (+ lisp-body-indent (current-column))))) | |
787 | ||
ca2ddd8e | 788 | |
a90256cc BP |
789 | ;; (put 'progn 'lisp-indent-function 0), say, causes progn to be indented |
790 | ;; like defun if the first form is placed on the next line, otherwise | |
791 | ;; it is indented like any other form (i.e. forms line up under first). | |
792 | ||
793 | (put 'lambda 'lisp-indent-function 'defun) | |
794 | (put 'autoload 'lisp-indent-function 'defun) | |
795 | (put 'progn 'lisp-indent-function 0) | |
796 | (put 'prog1 'lisp-indent-function 1) | |
797 | (put 'prog2 'lisp-indent-function 2) | |
798 | (put 'save-excursion 'lisp-indent-function 0) | |
799 | (put 'save-window-excursion 'lisp-indent-function 0) | |
4b619eca | 800 | (put 'save-selected-window 'lisp-indent-function 0) |
a90256cc | 801 | (put 'save-restriction 'lisp-indent-function 0) |
cfe158ab | 802 | (put 'save-match-data 'lisp-indent-function 0) |
38f16fe1 | 803 | (put 'save-current-buffer 'lisp-indent-function 0) |
54c014f0 | 804 | (put 'with-current-buffer 'lisp-indent-function 1) |
488a0b05 | 805 | (put 'combine-after-change-calls 'lisp-indent-function 0) |
38f16fe1 | 806 | (put 'with-output-to-string 'lisp-indent-function 0) |
08adb099 | 807 | (put 'with-temp-file 'lisp-indent-function 1) |
e0119683 | 808 | (put 'with-temp-buffer 'lisp-indent-function 0) |
bacd83a7 | 809 | (put 'with-temp-message 'lisp-indent-function 1) |
83c8f461 | 810 | (put 'with-syntax-table 'lisp-indent-function 1) |
a90256cc BP |
811 | (put 'let 'lisp-indent-function 1) |
812 | (put 'let* 'lisp-indent-function 1) | |
813 | (put 'while 'lisp-indent-function 1) | |
814 | (put 'if 'lisp-indent-function 2) | |
815 | (put 'catch 'lisp-indent-function 1) | |
816 | (put 'condition-case 'lisp-indent-function 2) | |
817 | (put 'unwind-protect 'lisp-indent-function 1) | |
818 | (put 'with-output-to-temp-buffer 'lisp-indent-function 1) | |
b6b4c8bd | 819 | (put 'eval-after-load 'lisp-indent-function 1) |
05c71036 DL |
820 | (put 'dolist 'lisp-indent-function 1) |
821 | (put 'dotimes 'lisp-indent-function 1) | |
822 | (put 'when 'lisp-indent-function 1) | |
823 | (put 'unless 'lisp-indent-function 1) | |
a90256cc BP |
824 | |
825 | (defun indent-sexp (&optional endpos) | |
826 | "Indent each line of the list starting just after point. | |
827 | If optional arg ENDPOS is given, indent each line, stopping when | |
828 | ENDPOS is encountered." | |
829 | (interactive) | |
daa37602 JB |
830 | (let ((indent-stack (list nil)) |
831 | (next-depth 0) | |
33f268ec RS |
832 | ;; If ENDPOS is non-nil, use nil as STARTING-POINT |
833 | ;; so that calculate-lisp-indent will find the beginning of | |
834 | ;; the defun we are in. | |
835 | ;; If ENDPOS is nil, it is safe not to scan before point | |
836 | ;; since every line we indent is more deeply nested than point is. | |
837 | (starting-point (if endpos nil (point))) | |
daa37602 JB |
838 | (last-point (point)) |
839 | last-depth bol outer-loop-done inner-loop-done state this-indent) | |
33f268ec RS |
840 | (or endpos |
841 | ;; Get error now if we don't have a complete sexp after point. | |
842 | (save-excursion (forward-sexp 1))) | |
a90256cc BP |
843 | (save-excursion |
844 | (setq outer-loop-done nil) | |
845 | (while (if endpos (< (point) endpos) | |
846 | (not outer-loop-done)) | |
847 | (setq last-depth next-depth | |
848 | inner-loop-done nil) | |
849 | ;; Parse this line so we can learn the state | |
850 | ;; to indent the next line. | |
851 | ;; This inner loop goes through only once | |
852 | ;; unless a line ends inside a string. | |
853 | (while (and (not inner-loop-done) | |
854 | (not (setq outer-loop-done (eobp)))) | |
855 | (setq state (parse-partial-sexp (point) (progn (end-of-line) (point)) | |
856 | nil nil state)) | |
857 | (setq next-depth (car state)) | |
858 | ;; If the line contains a comment other than the sort | |
859 | ;; that is indented like code, | |
860 | ;; indent it now with indent-for-comment. | |
861 | ;; Comments indented like code are right already. | |
862 | ;; In any case clear the in-comment flag in the state | |
863 | ;; because parse-partial-sexp never sees the newlines. | |
864 | (if (car (nthcdr 4 state)) | |
865 | (progn (indent-for-comment) | |
866 | (end-of-line) | |
867 | (setcar (nthcdr 4 state) nil))) | |
868 | ;; If this line ends inside a string, | |
869 | ;; go straight to next line, remaining within the inner loop, | |
870 | ;; and turn off the \-flag. | |
871 | (if (car (nthcdr 3 state)) | |
872 | (progn | |
873 | (forward-line 1) | |
874 | (setcar (nthcdr 5 state) nil)) | |
875 | (setq inner-loop-done t))) | |
876 | (and endpos | |
daa37602 JB |
877 | (<= next-depth 0) |
878 | (progn | |
99ec65b3 DL |
879 | (setq indent-stack (nconc indent-stack |
880 | (make-list (- next-depth) nil)) | |
daa37602 JB |
881 | last-depth (- last-depth next-depth) |
882 | next-depth 0))) | |
33f268ec | 883 | (or outer-loop-done endpos |
a90256cc BP |
884 | (setq outer-loop-done (<= next-depth 0))) |
885 | (if outer-loop-done | |
1f5038b5 | 886 | (forward-line 1) |
a90256cc BP |
887 | (while (> last-depth next-depth) |
888 | (setq indent-stack (cdr indent-stack) | |
889 | last-depth (1- last-depth))) | |
890 | (while (< last-depth next-depth) | |
891 | (setq indent-stack (cons nil indent-stack) | |
892 | last-depth (1+ last-depth))) | |
893 | ;; Now go to the next line and indent it according | |
894 | ;; to what we learned from parsing the previous one. | |
895 | (forward-line 1) | |
896 | (setq bol (point)) | |
897 | (skip-chars-forward " \t") | |
898 | ;; But not if the line is blank, or just a comment | |
899 | ;; (except for double-semi comments; indent them as usual). | |
900 | (if (or (eobp) (looking-at "\\s<\\|\n")) | |
901 | nil | |
902 | (if (and (car indent-stack) | |
903 | (>= (car indent-stack) 0)) | |
904 | (setq this-indent (car indent-stack)) | |
905 | (let ((val (calculate-lisp-indent | |
906 | (if (car indent-stack) (- (car indent-stack)) | |
daa37602 | 907 | starting-point)))) |
531cbff1 RS |
908 | (if (null val) |
909 | (setq this-indent val) | |
910 | (if (integerp val) | |
911 | (setcar indent-stack | |
912 | (setq this-indent val)) | |
913 | (setcar indent-stack (- (car (cdr val)))) | |
914 | (setq this-indent (car val)))))) | |
915 | (if (and this-indent (/= (current-column) this-indent)) | |
a90256cc BP |
916 | (progn (delete-region bol (point)) |
917 | (indent-to this-indent))))) | |
918 | (or outer-loop-done | |
919 | (setq outer-loop-done (= (point) last-point)) | |
920 | (setq last-point (point))))))) | |
921 | ||
a90256cc | 922 | (defun lisp-indent-region (start end) |
535eadac | 923 | "Indent every line whose first char is between START and END inclusive." |
a90256cc | 924 | (save-excursion |
a90256cc | 925 | (let ((endmark (copy-marker end))) |
33f268ec RS |
926 | (goto-char start) |
927 | (and (bolp) (not (eolp)) | |
928 | (lisp-indent-line)) | |
a90256cc BP |
929 | (indent-sexp endmark) |
930 | (set-marker endmark nil)))) | |
ca2ddd8e | 931 | |
6338c7ba JB |
932 | ;;;; Lisp paragraph filling commands. |
933 | ||
934 | (defun lisp-fill-paragraph (&optional justify) | |
935 | "Like \\[fill-paragraph], but handle Emacs Lisp comments. | |
936 | If any of the current line is a comment, fill the comment or the | |
937 | paragraph of it that point is in, preserving the comment's indentation | |
938 | and initial semicolons." | |
939 | (interactive "P") | |
940 | (let ( | |
941 | ;; Non-nil if the current line contains a comment. | |
942 | has-comment | |
943 | ||
4d2790ad RS |
944 | ;; Non-nil if the current line contains code and a comment. |
945 | has-code-and-comment | |
946 | ||
6338c7ba JB |
947 | ;; If has-comment, the appropriate fill-prefix for the comment. |
948 | comment-fill-prefix | |
949 | ) | |
950 | ||
951 | ;; Figure out what kind of comment we are looking at. | |
952 | (save-excursion | |
953 | (beginning-of-line) | |
954 | (cond | |
955 | ||
956 | ;; A line with nothing but a comment on it? | |
957 | ((looking-at "[ \t]*;[; \t]*") | |
958 | (setq has-comment t | |
959 | comment-fill-prefix (buffer-substring (match-beginning 0) | |
960 | (match-end 0)))) | |
961 | ||
962 | ;; A line with some code, followed by a comment? Remember that the | |
963 | ;; semi which starts the comment shouldn't be part of a string or | |
964 | ;; character. | |
93353fea KH |
965 | ((condition-case nil |
966 | (save-restriction | |
967 | (narrow-to-region (point-min) | |
968 | (save-excursion (end-of-line) (point))) | |
969 | (while (not (looking-at ";\\|$")) | |
970 | (skip-chars-forward "^;\n\"\\\\?") | |
971 | (cond | |
972 | ((eq (char-after (point)) ?\\) (forward-char 2)) | |
973 | ((memq (char-after (point)) '(?\" ??)) (forward-sexp 1)))) | |
974 | (looking-at ";+[\t ]*")) | |
975 | (error nil)) | |
4d2790ad | 976 | (setq has-comment t has-code-and-comment t) |
6338c7ba | 977 | (setq comment-fill-prefix |
93353fea KH |
978 | (concat (make-string (/ (current-column) 8) ?\t) |
979 | (make-string (% (current-column) 8) ?\ ) | |
6338c7ba JB |
980 | (buffer-substring (match-beginning 0) (match-end 0))))))) |
981 | ||
982 | (if (not has-comment) | |
fe3188ec DL |
983 | ;; `paragraph-start' is set here (not in the buffer-local |
984 | ;; variable so that `forward-paragraph' et al work as | |
985 | ;; expected) so that filling (doc) strings works sensibly. | |
986 | ;; Adding the opening paren to avoid the following sexp being | |
987 | ;; filled means that sexps generally aren't filled as normal | |
988 | ;; text, which is probably sensible. The `;' and `:' stop the | |
989 | ;; filled para at following comment lines and keywords | |
990 | ;; (typically in `defcustom'). | |
991 | (let ((paragraph-start (concat paragraph-start | |
992 | "\\|\\s-*[\(;:\"]"))) | |
993 | (fill-paragraph justify)) | |
6338c7ba JB |
994 | |
995 | ;; Narrow to include only the comment, and then fill the region. | |
4c06fbee KH |
996 | (save-excursion |
997 | (save-restriction | |
998 | (beginning-of-line) | |
999 | (narrow-to-region | |
1000 | ;; Find the first line we should include in the region to fill. | |
1001 | (save-excursion | |
1002 | (while (and (zerop (forward-line -1)) | |
6338c7ba | 1003 | (looking-at "^[ \t]*;"))) |
4c06fbee KH |
1004 | ;; We may have gone too far. Go forward again. |
1005 | (or (looking-at ".*;") | |
1006 | (forward-line 1)) | |
1007 | (point)) | |
1008 | ;; Find the beginning of the first line past the region to fill. | |
1009 | (save-excursion | |
1010 | (while (progn (forward-line 1) | |
1011 | (looking-at "^[ \t]*;"))) | |
1012 | (point))) | |
1013 | ||
1014 | ;; Lines with only semicolons on them can be paragraph boundaries. | |
1015 | (let* ((paragraph-start (concat paragraph-start "\\|[ \t;]*$")) | |
1016 | (paragraph-separate (concat paragraph-start "\\|[ \t;]*$")) | |
1017 | (paragraph-ignore-fill-prefix nil) | |
1018 | (fill-prefix comment-fill-prefix) | |
4d2790ad RS |
1019 | (after-line (if has-code-and-comment |
1020 | (save-excursion | |
1021 | (forward-line 1) (point)))) | |
4c06fbee KH |
1022 | (end (progn |
1023 | (forward-paragraph) | |
1024 | (or (bolp) (newline 1)) | |
1025 | (point))) | |
4d2790ad RS |
1026 | ;; If this comment starts on a line with code, |
1027 | ;; include that like in the filling. | |
1028 | (beg (progn (backward-paragraph) | |
1029 | (if (eq (point) after-line) | |
1030 | (forward-line -1)) | |
1031 | (point)))) | |
4c06fbee KH |
1032 | (fill-region-as-paragraph beg end |
1033 | justify nil | |
1034 | (save-excursion | |
1035 | (goto-char beg) | |
1036 | (if (looking-at fill-prefix) | |
1037 | nil | |
1038 | (re-search-forward comment-start-skip) | |
1039 | (point)))))))) | |
2b4483bb | 1040 | t)) |
ca2ddd8e | 1041 | |
a90256cc BP |
1042 | (defun indent-code-rigidly (start end arg &optional nochange-regexp) |
1043 | "Indent all lines of code, starting in the region, sideways by ARG columns. | |
1044 | Does not affect lines starting inside comments or strings, assuming that | |
1045 | the start of the region is not inside them. | |
1046 | ||
1047 | Called from a program, takes args START, END, COLUMNS and NOCHANGE-REGEXP. | |
1048 | The last is a regexp which, if matched at the beginning of a line, | |
1049 | means don't indent that line." | |
1050 | (interactive "r\np") | |
1051 | (let (state) | |
1052 | (save-excursion | |
1053 | (goto-char end) | |
1054 | (setq end (point-marker)) | |
1055 | (goto-char start) | |
1056 | (or (bolp) | |
1057 | (setq state (parse-partial-sexp (point) | |
1058 | (progn | |
1059 | (forward-line 1) (point)) | |
1060 | nil nil state))) | |
1061 | (while (< (point) end) | |
1062 | (or (car (nthcdr 3 state)) | |
1063 | (and nochange-regexp | |
1064 | (looking-at nochange-regexp)) | |
1065 | ;; If line does not start in string, indent it | |
1066 | (let ((indent (current-indentation))) | |
1067 | (delete-region (point) (progn (skip-chars-forward " \t") (point))) | |
1068 | (or (eolp) | |
1069 | (indent-to (max 0 (+ indent arg)) 0)))) | |
1070 | (setq state (parse-partial-sexp (point) | |
1071 | (progn | |
1072 | (forward-line 1) (point)) | |
1073 | nil nil state)))))) | |
49116ac0 JB |
1074 | |
1075 | (provide 'lisp-mode) | |
1076 | ||
6594deb0 | 1077 | ;;; lisp-mode.el ends here |