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