Commit | Line | Data |
---|---|---|
6594deb0 ER |
1 | ;;; lisp-mode.el --- Lisp mode, and its idiosyncratic commands. |
2 | ||
3a801d0c ER |
3 | ;; Copyright (C) 1985 Free Software Foundation, Inc. |
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 | |
21 | ;; along with GNU Emacs; see the file COPYING. If not, write to | |
22 | ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
23 | ||
e41b2db1 ER |
24 | ;;; Commentary: |
25 | ||
26 | ;; The base major mode for editing Lisp code (used also for Emacs Lisp). | |
27 | ;; This mode is documented in the Emacs manual | |
28 | ||
29 | ;;; Code: | |
30 | ||
a90256cc BP |
31 | (defvar lisp-mode-syntax-table nil "") |
32 | (defvar emacs-lisp-mode-syntax-table nil "") | |
33 | (defvar lisp-mode-abbrev-table nil "") | |
34 | ||
35 | (if (not emacs-lisp-mode-syntax-table) | |
36 | (let ((i 0)) | |
37 | (setq emacs-lisp-mode-syntax-table (make-syntax-table)) | |
38 | (while (< i ?0) | |
39 | (modify-syntax-entry i "_ " emacs-lisp-mode-syntax-table) | |
40 | (setq i (1+ i))) | |
41 | (setq i (1+ ?9)) | |
42 | (while (< i ?A) | |
43 | (modify-syntax-entry i "_ " emacs-lisp-mode-syntax-table) | |
44 | (setq i (1+ i))) | |
45 | (setq i (1+ ?Z)) | |
46 | (while (< i ?a) | |
47 | (modify-syntax-entry i "_ " emacs-lisp-mode-syntax-table) | |
48 | (setq i (1+ i))) | |
49 | (setq i (1+ ?z)) | |
50 | (while (< i 128) | |
51 | (modify-syntax-entry i "_ " emacs-lisp-mode-syntax-table) | |
52 | (setq i (1+ i))) | |
53 | (modify-syntax-entry ? " " emacs-lisp-mode-syntax-table) | |
54 | (modify-syntax-entry ?\t " " emacs-lisp-mode-syntax-table) | |
55 | (modify-syntax-entry ?\n "> " emacs-lisp-mode-syntax-table) | |
910762b4 RS |
56 | ;; Give CR the same syntax as newline, for selective-display. |
57 | (modify-syntax-entry ?\^m "> " emacs-lisp-mode-syntax-table) | |
a90256cc BP |
58 | (modify-syntax-entry ?\; "< " emacs-lisp-mode-syntax-table) |
59 | (modify-syntax-entry ?` "' " emacs-lisp-mode-syntax-table) | |
60 | (modify-syntax-entry ?' "' " emacs-lisp-mode-syntax-table) | |
61 | (modify-syntax-entry ?, "' " emacs-lisp-mode-syntax-table) | |
62 | ;; Used to be singlequote; changed for flonums. | |
63 | (modify-syntax-entry ?. "_ " emacs-lisp-mode-syntax-table) | |
64 | (modify-syntax-entry ?# "' " emacs-lisp-mode-syntax-table) | |
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 | ||
ecca85de JB |
72 | (if (not lisp-mode-syntax-table) |
73 | (progn (setq lisp-mode-syntax-table | |
74 | (copy-syntax-table emacs-lisp-mode-syntax-table)) | |
75 | (modify-syntax-entry ?\| "\" " lisp-mode-syntax-table) | |
76 | (modify-syntax-entry ?\[ "_ " lisp-mode-syntax-table) | |
77 | (modify-syntax-entry ?\] "_ " lisp-mode-syntax-table))) | |
78 | ||
a90256cc BP |
79 | (define-abbrev-table 'lisp-mode-abbrev-table ()) |
80 | ||
81 | (defun lisp-mode-variables (lisp-syntax) | |
82 | (cond (lisp-syntax | |
a90256cc BP |
83 | (set-syntax-table lisp-mode-syntax-table))) |
84 | (setq local-abbrev-table lisp-mode-abbrev-table) | |
85 | (make-local-variable 'paragraph-start) | |
86 | (setq paragraph-start (concat "^$\\|" page-delimiter)) | |
87 | (make-local-variable 'paragraph-separate) | |
88 | (setq paragraph-separate paragraph-start) | |
89 | (make-local-variable 'paragraph-ignore-fill-prefix) | |
90 | (setq paragraph-ignore-fill-prefix t) | |
35d132a8 RS |
91 | (make-local-variable 'fill-paragraph-function) |
92 | (setq fill-paragraph-function 'lisp-fill-paragraph) | |
a90256cc BP |
93 | (make-local-variable 'indent-line-function) |
94 | (setq indent-line-function 'lisp-indent-line) | |
95 | (make-local-variable 'indent-region-function) | |
96 | (setq indent-region-function 'lisp-indent-region) | |
97 | (make-local-variable 'parse-sexp-ignore-comments) | |
98 | (setq parse-sexp-ignore-comments t) | |
5847f861 | 99 | (make-local-variable 'outline-regexp) |
30ff174e | 100 | (setq outline-regexp ";;; \\|(....") |
a90256cc BP |
101 | (make-local-variable 'comment-start) |
102 | (setq comment-start ";") | |
103 | (make-local-variable 'comment-start-skip) | |
c36e9c06 | 104 | (setq comment-start-skip ";+ *") |
a90256cc BP |
105 | (make-local-variable 'comment-column) |
106 | (setq comment-column 40) | |
e41b2db1 ER |
107 | (make-local-variable 'comment-indent-function) |
108 | (setq comment-indent-function 'lisp-comment-indent)) | |
a90256cc BP |
109 | \f |
110 | (defvar shared-lisp-mode-map () | |
111 | "Keymap for commands shared by all sorts of Lisp modes.") | |
112 | ||
113 | (if shared-lisp-mode-map | |
114 | () | |
115 | (setq shared-lisp-mode-map (make-sparse-keymap)) | |
116 | (define-key shared-lisp-mode-map "\e\C-q" 'indent-sexp) | |
117 | (define-key shared-lisp-mode-map "\177" 'backward-delete-char-untabify) | |
118 | (define-key shared-lisp-mode-map "\t" 'lisp-indent-line)) | |
119 | ||
120 | (defvar emacs-lisp-mode-map () | |
121 | "Keymap for Emacs Lisp mode. | |
122 | All commands in shared-lisp-mode-map are inherited by this map.") | |
123 | ||
124 | (if emacs-lisp-mode-map | |
125 | () | |
126 | (setq emacs-lisp-mode-map | |
127 | (nconc (make-sparse-keymap) shared-lisp-mode-map)) | |
ab67260b | 128 | (define-key emacs-lisp-mode-map "\e\t" 'lisp-complete-symbol) |
a90256cc BP |
129 | (define-key emacs-lisp-mode-map "\e\C-x" 'eval-defun)) |
130 | ||
131 | (defun emacs-lisp-mode () | |
132 | "Major mode for editing Lisp code to run in Emacs. | |
133 | Commands: | |
134 | Delete converts tabs to spaces as it moves back. | |
135 | Blank lines separate paragraphs. Semicolons start comments. | |
136 | \\{emacs-lisp-mode-map} | |
137 | Entry to this mode calls the value of `emacs-lisp-mode-hook' | |
138 | if that value is non-nil." | |
139 | (interactive) | |
140 | (kill-all-local-variables) | |
141 | (use-local-map emacs-lisp-mode-map) | |
142 | (set-syntax-table emacs-lisp-mode-syntax-table) | |
143 | (setq major-mode 'emacs-lisp-mode) | |
144 | (setq mode-name "Emacs-Lisp") | |
145 | (lisp-mode-variables nil) | |
146 | (run-hooks 'emacs-lisp-mode-hook)) | |
147 | ||
148 | (defvar lisp-mode-map () | |
149 | "Keymap for ordinary Lisp mode. | |
150 | All commands in `shared-lisp-mode-map' are inherited by this map.") | |
151 | ||
152 | (if lisp-mode-map | |
153 | () | |
154 | (setq lisp-mode-map | |
155 | (nconc (make-sparse-keymap) shared-lisp-mode-map)) | |
ef7485ce RS |
156 | (define-key lisp-mode-map "\e\C-x" 'lisp-eval-defun) |
157 | (define-key lisp-mode-map "\C-c\C-z" 'run-lisp)) | |
a90256cc BP |
158 | |
159 | (defun lisp-mode () | |
160 | "Major mode for editing Lisp code for Lisps other than GNU Emacs Lisp. | |
161 | Commands: | |
162 | Delete converts tabs to spaces as it moves back. | |
163 | Blank lines separate paragraphs. Semicolons start comments. | |
164 | \\{lisp-mode-map} | |
165 | Note that `run-lisp' may be used either to start an inferior Lisp job | |
166 | or to switch back to an existing one. | |
167 | ||
168 | Entry to this mode calls the value of `lisp-mode-hook' | |
169 | if that value is non-nil." | |
170 | (interactive) | |
171 | (kill-all-local-variables) | |
172 | (use-local-map lisp-mode-map) | |
173 | (setq major-mode 'lisp-mode) | |
174 | (setq mode-name "Lisp") | |
175 | (lisp-mode-variables t) | |
176 | (set-syntax-table lisp-mode-syntax-table) | |
177 | (run-hooks 'lisp-mode-hook)) | |
178 | ||
179 | ;; This will do unless shell.el is loaded. | |
ef7485ce | 180 | (defun lisp-eval-defun nil |
a90256cc BP |
181 | "Send the current defun to the Lisp process made by \\[run-lisp]." |
182 | (interactive) | |
183 | (error "Process lisp does not exist")) | |
184 | ||
185 | (defvar lisp-interaction-mode-map () | |
186 | "Keymap for Lisp Interaction moe. | |
187 | All commands in `shared-lisp-mode-map' are inherited by this map.") | |
188 | ||
189 | (if lisp-interaction-mode-map | |
190 | () | |
191 | (setq lisp-interaction-mode-map | |
192 | (nconc (make-sparse-keymap) shared-lisp-mode-map)) | |
193 | (define-key lisp-interaction-mode-map "\e\C-x" 'eval-defun) | |
ab67260b | 194 | (define-key lisp-interaction-mode-map "\e\t" 'lisp-complete-symbol) |
a90256cc BP |
195 | (define-key lisp-interaction-mode-map "\n" 'eval-print-last-sexp)) |
196 | ||
197 | (defun lisp-interaction-mode () | |
198 | "Major mode for typing and evaluating Lisp forms. | |
199 | Like Lisp mode except that \\[eval-print-last-sexp] evals the Lisp expression | |
200 | before point, and prints its value into the buffer, advancing point. | |
201 | ||
202 | Commands: | |
203 | Delete converts tabs to spaces as it moves back. | |
204 | Paragraphs are separated only by blank lines. | |
205 | Semicolons start comments. | |
206 | \\{lisp-interaction-mode-map} | |
207 | Entry to this mode calls the value of `lisp-interaction-mode-hook' | |
208 | if that value is non-nil." | |
209 | (interactive) | |
210 | (kill-all-local-variables) | |
211 | (use-local-map lisp-interaction-mode-map) | |
212 | (setq major-mode 'lisp-interaction-mode) | |
213 | (setq mode-name "Lisp Interaction") | |
214 | (set-syntax-table emacs-lisp-mode-syntax-table) | |
215 | (lisp-mode-variables nil) | |
216 | (run-hooks 'lisp-interaction-mode-hook)) | |
217 | ||
121f0d57 | 218 | (defun eval-print-last-sexp () |
a90256cc | 219 | "Evaluate sexp before point; print value into current buffer." |
121f0d57 | 220 | (interactive) |
f798d950 JB |
221 | (let ((standard-output (current-buffer))) |
222 | (terpri) | |
223 | (eval-last-sexp t) | |
224 | (terpri))) | |
a90256cc | 225 | \f |
efb8f835 | 226 | (defun eval-last-sexp (eval-last-sexp-arg-internal) |
a90256cc BP |
227 | "Evaluate sexp before point; print value in minibuffer. |
228 | With argument, print output into current buffer." | |
229 | (interactive "P") | |
efb8f835 | 230 | (let ((standard-output (if eval-last-sexp-arg-internal (current-buffer) t)) |
cfe158ab | 231 | (opoint (point))) |
19b2f8f1 RM |
232 | (prin1 (let ((stab (syntax-table))) |
233 | (eval (unwind-protect | |
234 | (save-excursion | |
235 | (set-syntax-table emacs-lisp-mode-syntax-table) | |
236 | (forward-sexp -1) | |
cfe158ab RS |
237 | (save-restriction |
238 | (narrow-to-region (point-min) opoint) | |
239 | (read (current-buffer)))) | |
19b2f8f1 | 240 | (set-syntax-table stab))))))) |
a90256cc | 241 | |
efb8f835 | 242 | (defun eval-defun (eval-defun-arg-internal) |
121f0d57 RM |
243 | "Evaluate defun that point is in or before. |
244 | Print value in minibuffer. | |
245 | With argument, insert value in current buffer after the defun." | |
a90256cc | 246 | (interactive "P") |
efb8f835 | 247 | (let ((standard-output (if eval-defun-arg-internal (current-buffer) t))) |
19b2f8f1 RM |
248 | (prin1 (eval (save-excursion |
249 | (end-of-defun) | |
250 | (beginning-of-defun) | |
251 | (read (current-buffer))))))) | |
a90256cc BP |
252 | \f |
253 | (defun lisp-comment-indent () | |
254 | (if (looking-at "\\s<\\s<\\s<") | |
255 | (current-column) | |
256 | (if (looking-at "\\s<\\s<") | |
257 | (let ((tem (calculate-lisp-indent))) | |
258 | (if (listp tem) (car tem) tem)) | |
259 | (skip-chars-backward " \t") | |
260 | (max (if (bolp) 0 (1+ (current-column))) | |
261 | comment-column)))) | |
262 | ||
263 | (defconst lisp-indent-offset nil "") | |
264 | (defconst lisp-indent-function 'lisp-indent-function "") | |
265 | ||
266 | (defun lisp-indent-line (&optional whole-exp) | |
267 | "Indent current line as Lisp code. | |
268 | With argument, indent any additional lines of the same expression | |
269 | rigidly along with this one." | |
270 | (interactive "P") | |
271 | (let ((indent (calculate-lisp-indent)) shift-amt beg end | |
272 | (pos (- (point-max) (point)))) | |
273 | (beginning-of-line) | |
274 | (setq beg (point)) | |
275 | (skip-chars-forward " \t") | |
276 | (if (looking-at "\\s<\\s<\\s<") | |
277 | ;; Don't alter indentation of a ;;; comment line. | |
328561fc | 278 | (goto-char (- (point-max) pos)) |
a90256cc BP |
279 | (if (and (looking-at "\\s<") (not (looking-at "\\s<\\s<"))) |
280 | ;; Single-semicolon comment lines should be indented | |
281 | ;; as comment lines, not as code. | |
282 | (progn (indent-for-comment) (forward-char -1)) | |
283 | (if (listp indent) (setq indent (car indent))) | |
284 | (setq shift-amt (- indent (current-column))) | |
285 | (if (zerop shift-amt) | |
286 | nil | |
287 | (delete-region beg (point)) | |
288 | (indent-to indent))) | |
289 | ;; If initial point was within line's indentation, | |
290 | ;; position after the indentation. Else stay at same point in text. | |
291 | (if (> (- (point-max) pos) (point)) | |
292 | (goto-char (- (point-max) pos))) | |
293 | ;; If desired, shift remaining lines of expression the same amount. | |
294 | (and whole-exp (not (zerop shift-amt)) | |
295 | (save-excursion | |
296 | (goto-char beg) | |
297 | (forward-sexp 1) | |
298 | (setq end (point)) | |
299 | (goto-char beg) | |
300 | (forward-line 1) | |
301 | (setq beg (point)) | |
302 | (> end beg)) | |
303 | (indent-code-rigidly beg end shift-amt))))) | |
304 | ||
22486a7f | 305 | (defvar calculate-lisp-indent-last-sexp) |
c0df1d61 | 306 | |
a90256cc BP |
307 | (defun calculate-lisp-indent (&optional parse-start) |
308 | "Return appropriate indentation for current line as Lisp code. | |
309 | In usual case returns an integer: the column to indent to. | |
310 | Can instead return a list, whose car is the column to indent to. | |
311 | This means that following lines at the same level of indentation | |
312 | should not necessarily be indented the same way. | |
313 | The second element of the list is the buffer position | |
314 | of the start of the containing expression." | |
315 | (save-excursion | |
316 | (beginning-of-line) | |
317 | (let ((indent-point (point)) | |
318 | state paren-depth | |
319 | ;; setting this to a number inhibits calling hook | |
320 | (desired-indent nil) | |
321 | (retry t) | |
c0df1d61 | 322 | calculate-lisp-indent-last-sexp containing-sexp) |
a90256cc BP |
323 | (if parse-start |
324 | (goto-char parse-start) | |
325 | (beginning-of-defun)) | |
326 | ;; Find outermost containing sexp | |
327 | (while (< (point) indent-point) | |
328 | (setq state (parse-partial-sexp (point) indent-point 0))) | |
329 | ;; Find innermost containing sexp | |
330 | (while (and retry | |
331 | state | |
332 | (> (setq paren-depth (elt state 0)) 0)) | |
333 | (setq retry nil) | |
c0df1d61 | 334 | (setq calculate-lisp-indent-last-sexp (elt state 2)) |
a90256cc BP |
335 | (setq containing-sexp (elt state 1)) |
336 | ;; Position following last unclosed open. | |
337 | (goto-char (1+ containing-sexp)) | |
338 | ;; Is there a complete sexp since then? | |
c0df1d61 RS |
339 | (if (and calculate-lisp-indent-last-sexp |
340 | (> calculate-lisp-indent-last-sexp (point))) | |
a90256cc | 341 | ;; Yes, but is there a containing sexp after that? |
c0df1d61 RS |
342 | (let ((peek (parse-partial-sexp calculate-lisp-indent-last-sexp |
343 | indent-point 0))) | |
a90256cc BP |
344 | (if (setq retry (car (cdr peek))) (setq state peek))))) |
345 | (if retry | |
346 | nil | |
347 | ;; Innermost containing sexp found | |
348 | (goto-char (1+ containing-sexp)) | |
c0df1d61 | 349 | (if (not calculate-lisp-indent-last-sexp) |
a90256cc BP |
350 | ;; indent-point immediately follows open paren. |
351 | ;; Don't call hook. | |
352 | (setq desired-indent (current-column)) | |
353 | ;; Find the start of first element of containing sexp. | |
c0df1d61 | 354 | (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t) |
a90256cc BP |
355 | (cond ((looking-at "\\s(") |
356 | ;; First element of containing sexp is a list. | |
357 | ;; Indent under that list. | |
358 | ) | |
359 | ((> (save-excursion (forward-line 1) (point)) | |
c0df1d61 | 360 | calculate-lisp-indent-last-sexp) |
a90256cc BP |
361 | ;; This is the first line to start within the containing sexp. |
362 | ;; It's almost certainly a function call. | |
c0df1d61 | 363 | (if (= (point) calculate-lisp-indent-last-sexp) |
a90256cc BP |
364 | ;; Containing sexp has nothing before this line |
365 | ;; except the first element. Indent under that element. | |
366 | nil | |
367 | ;; Skip the first element, find start of second (the first | |
368 | ;; argument of the function call) and indent under. | |
369 | (progn (forward-sexp 1) | |
c0df1d61 RS |
370 | (parse-partial-sexp (point) |
371 | calculate-lisp-indent-last-sexp | |
372 | 0 t))) | |
a90256cc BP |
373 | (backward-prefix-chars)) |
374 | (t | |
c0df1d61 RS |
375 | ;; Indent beneath first sexp on same line as |
376 | ;; calculate-lisp-indent-last-sexp. Again, it's | |
377 | ;; almost certainly a function call. | |
378 | (goto-char calculate-lisp-indent-last-sexp) | |
a90256cc | 379 | (beginning-of-line) |
c0df1d61 RS |
380 | (parse-partial-sexp (point) calculate-lisp-indent-last-sexp |
381 | 0 t) | |
a90256cc BP |
382 | (backward-prefix-chars))))) |
383 | ;; Point is at the point to indent under unless we are inside a string. | |
eb8c3be9 | 384 | ;; Call indentation hook except when overridden by lisp-indent-offset |
a90256cc BP |
385 | ;; or if the desired indentation has already been computed. |
386 | (let ((normal-indent (current-column))) | |
387 | (cond ((elt state 3) | |
388 | ;; Inside a string, don't change indentation. | |
389 | (goto-char indent-point) | |
390 | (skip-chars-forward " \t") | |
391 | (current-column)) | |
392 | ((and (integerp lisp-indent-offset) containing-sexp) | |
393 | ;; Indent by constant offset | |
394 | (goto-char containing-sexp) | |
395 | (+ (current-column) lisp-indent-offset)) | |
396 | (desired-indent) | |
397 | ((and (boundp 'lisp-indent-function) | |
398 | lisp-indent-function | |
399 | (not retry)) | |
400 | (or (funcall lisp-indent-function indent-point state) | |
401 | normal-indent)) | |
402 | (t | |
f30ff39f | 403 | normal-indent)))))) |
a90256cc BP |
404 | |
405 | (defun lisp-indent-function (indent-point state) | |
406 | (let ((normal-indent (current-column))) | |
407 | (goto-char (1+ (elt state 1))) | |
c0df1d61 | 408 | (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t) |
a90256cc BP |
409 | (if (and (elt state 2) |
410 | (not (looking-at "\\sw\\|\\s_"))) | |
411 | ;; car of form doesn't seem to be a a symbol | |
412 | (progn | |
413 | (if (not (> (save-excursion (forward-line 1) (point)) | |
c0df1d61 RS |
414 | calculate-lisp-indent-last-sexp)) |
415 | (progn (goto-char calculate-lisp-indent-last-sexp) | |
a90256cc | 416 | (beginning-of-line) |
c0df1d61 RS |
417 | (parse-partial-sexp (point) |
418 | calculate-lisp-indent-last-sexp 0 t))) | |
419 | ;; Indent under the list or under the first sexp on the same | |
420 | ;; line as calculate-lisp-indent-last-sexp. Note that first | |
421 | ;; thing on that line has to be complete sexp since we are | |
422 | ;; inside the innermost containing sexp. | |
a90256cc BP |
423 | (backward-prefix-chars) |
424 | (current-column)) | |
425 | (let ((function (buffer-substring (point) | |
426 | (progn (forward-sexp 1) (point)))) | |
427 | method) | |
2dab7802 RS |
428 | (setq method (or (get (intern-soft function) 'lisp-indent-function) |
429 | (get (intern-soft function) 'lisp-indent-hook))) | |
a90256cc BP |
430 | (cond ((or (eq method 'defun) |
431 | (and (null method) | |
432 | (> (length function) 3) | |
433 | (string-match "\\`def" function))) | |
434 | (lisp-indent-defform state indent-point)) | |
435 | ((integerp method) | |
436 | (lisp-indent-specform method state | |
437 | indent-point normal-indent)) | |
438 | (method | |
439 | (funcall method state indent-point))))))) | |
440 | ||
ab69b2fb RS |
441 | (defconst lisp-body-indent 2 |
442 | "Number of columns to indent the second line of a `(def...)' form.") | |
a90256cc BP |
443 | |
444 | (defun lisp-indent-specform (count state indent-point normal-indent) | |
445 | (let ((containing-form-start (elt state 1)) | |
446 | (i count) | |
447 | body-indent containing-form-column) | |
448 | ;; Move to the start of containing form, calculate indentation | |
449 | ;; to use for non-distinguished forms (> count), and move past the | |
450 | ;; function symbol. lisp-indent-function guarantees that there is at | |
451 | ;; least one word or symbol character following open paren of containing | |
452 | ;; form. | |
453 | (goto-char containing-form-start) | |
454 | (setq containing-form-column (current-column)) | |
455 | (setq body-indent (+ lisp-body-indent containing-form-column)) | |
456 | (forward-char 1) | |
457 | (forward-sexp 1) | |
458 | ;; Now find the start of the last form. | |
459 | (parse-partial-sexp (point) indent-point 1 t) | |
460 | (while (and (< (point) indent-point) | |
461 | (condition-case () | |
462 | (progn | |
463 | (setq count (1- count)) | |
464 | (forward-sexp 1) | |
465 | (parse-partial-sexp (point) indent-point 1 t)) | |
466 | (error nil)))) | |
467 | ;; Point is sitting on first character of last (or count) sexp. | |
468 | (if (> count 0) | |
469 | ;; A distinguished form. If it is the first or second form use double | |
470 | ;; lisp-body-indent, else normal indent. With lisp-body-indent bound | |
471 | ;; to 2 (the default), this just happens to work the same with if as | |
472 | ;; the older code, but it makes unwind-protect, condition-case, | |
473 | ;; with-output-to-temp-buffer, et. al. much more tasteful. The older, | |
474 | ;; less hacked, behavior can be obtained by replacing below with | |
475 | ;; (list normal-indent containing-form-start). | |
476 | (if (<= (- i count) 1) | |
477 | (list (+ containing-form-column (* 2 lisp-body-indent)) | |
478 | containing-form-start) | |
479 | (list normal-indent containing-form-start)) | |
480 | ;; A non-distinguished form. Use body-indent if there are no | |
481 | ;; distinguished forms and this is the first undistinguished form, | |
482 | ;; or if this is the first undistinguished form and the preceding | |
483 | ;; distinguished form has indentation at least as great as body-indent. | |
484 | (if (or (and (= i 0) (= count 0)) | |
485 | (and (= count 0) (<= body-indent normal-indent))) | |
486 | body-indent | |
487 | normal-indent)))) | |
488 | ||
489 | (defun lisp-indent-defform (state indent-point) | |
490 | (goto-char (car (cdr state))) | |
491 | (forward-line 1) | |
492 | (if (> (point) (car (cdr (cdr state)))) | |
493 | (progn | |
494 | (goto-char (car (cdr state))) | |
495 | (+ lisp-body-indent (current-column))))) | |
496 | ||
497 | \f | |
498 | ;; (put 'progn 'lisp-indent-function 0), say, causes progn to be indented | |
499 | ;; like defun if the first form is placed on the next line, otherwise | |
500 | ;; it is indented like any other form (i.e. forms line up under first). | |
501 | ||
502 | (put 'lambda 'lisp-indent-function 'defun) | |
503 | (put 'autoload 'lisp-indent-function 'defun) | |
504 | (put 'progn 'lisp-indent-function 0) | |
505 | (put 'prog1 'lisp-indent-function 1) | |
506 | (put 'prog2 'lisp-indent-function 2) | |
507 | (put 'save-excursion 'lisp-indent-function 0) | |
508 | (put 'save-window-excursion 'lisp-indent-function 0) | |
509 | (put 'save-restriction 'lisp-indent-function 0) | |
cfe158ab | 510 | (put 'save-match-data 'lisp-indent-function 0) |
a90256cc BP |
511 | (put 'let 'lisp-indent-function 1) |
512 | (put 'let* 'lisp-indent-function 1) | |
513 | (put 'while 'lisp-indent-function 1) | |
514 | (put 'if 'lisp-indent-function 2) | |
515 | (put 'catch 'lisp-indent-function 1) | |
516 | (put 'condition-case 'lisp-indent-function 2) | |
517 | (put 'unwind-protect 'lisp-indent-function 1) | |
518 | (put 'with-output-to-temp-buffer 'lisp-indent-function 1) | |
519 | ||
520 | (defun indent-sexp (&optional endpos) | |
521 | "Indent each line of the list starting just after point. | |
522 | If optional arg ENDPOS is given, indent each line, stopping when | |
523 | ENDPOS is encountered." | |
524 | (interactive) | |
daa37602 JB |
525 | (let ((indent-stack (list nil)) |
526 | (next-depth 0) | |
33f268ec RS |
527 | ;; If ENDPOS is non-nil, use nil as STARTING-POINT |
528 | ;; so that calculate-lisp-indent will find the beginning of | |
529 | ;; the defun we are in. | |
530 | ;; If ENDPOS is nil, it is safe not to scan before point | |
531 | ;; since every line we indent is more deeply nested than point is. | |
532 | (starting-point (if endpos nil (point))) | |
daa37602 JB |
533 | (last-point (point)) |
534 | last-depth bol outer-loop-done inner-loop-done state this-indent) | |
33f268ec RS |
535 | (or endpos |
536 | ;; Get error now if we don't have a complete sexp after point. | |
537 | (save-excursion (forward-sexp 1))) | |
a90256cc BP |
538 | (save-excursion |
539 | (setq outer-loop-done nil) | |
540 | (while (if endpos (< (point) endpos) | |
541 | (not outer-loop-done)) | |
542 | (setq last-depth next-depth | |
543 | inner-loop-done nil) | |
544 | ;; Parse this line so we can learn the state | |
545 | ;; to indent the next line. | |
546 | ;; This inner loop goes through only once | |
547 | ;; unless a line ends inside a string. | |
548 | (while (and (not inner-loop-done) | |
549 | (not (setq outer-loop-done (eobp)))) | |
550 | (setq state (parse-partial-sexp (point) (progn (end-of-line) (point)) | |
551 | nil nil state)) | |
552 | (setq next-depth (car state)) | |
553 | ;; If the line contains a comment other than the sort | |
554 | ;; that is indented like code, | |
555 | ;; indent it now with indent-for-comment. | |
556 | ;; Comments indented like code are right already. | |
557 | ;; In any case clear the in-comment flag in the state | |
558 | ;; because parse-partial-sexp never sees the newlines. | |
559 | (if (car (nthcdr 4 state)) | |
560 | (progn (indent-for-comment) | |
561 | (end-of-line) | |
562 | (setcar (nthcdr 4 state) nil))) | |
563 | ;; If this line ends inside a string, | |
564 | ;; go straight to next line, remaining within the inner loop, | |
565 | ;; and turn off the \-flag. | |
566 | (if (car (nthcdr 3 state)) | |
567 | (progn | |
568 | (forward-line 1) | |
569 | (setcar (nthcdr 5 state) nil)) | |
570 | (setq inner-loop-done t))) | |
571 | (and endpos | |
daa37602 JB |
572 | (<= next-depth 0) |
573 | (progn | |
574 | (setq indent-stack (append indent-stack | |
575 | (make-list (- next-depth) nil)) | |
576 | last-depth (- last-depth next-depth) | |
577 | next-depth 0))) | |
33f268ec | 578 | (or outer-loop-done endpos |
a90256cc BP |
579 | (setq outer-loop-done (<= next-depth 0))) |
580 | (if outer-loop-done | |
1f5038b5 | 581 | (forward-line 1) |
a90256cc BP |
582 | (while (> last-depth next-depth) |
583 | (setq indent-stack (cdr indent-stack) | |
584 | last-depth (1- last-depth))) | |
585 | (while (< last-depth next-depth) | |
586 | (setq indent-stack (cons nil indent-stack) | |
587 | last-depth (1+ last-depth))) | |
588 | ;; Now go to the next line and indent it according | |
589 | ;; to what we learned from parsing the previous one. | |
590 | (forward-line 1) | |
591 | (setq bol (point)) | |
592 | (skip-chars-forward " \t") | |
593 | ;; But not if the line is blank, or just a comment | |
594 | ;; (except for double-semi comments; indent them as usual). | |
595 | (if (or (eobp) (looking-at "\\s<\\|\n")) | |
596 | nil | |
597 | (if (and (car indent-stack) | |
598 | (>= (car indent-stack) 0)) | |
599 | (setq this-indent (car indent-stack)) | |
600 | (let ((val (calculate-lisp-indent | |
601 | (if (car indent-stack) (- (car indent-stack)) | |
daa37602 | 602 | starting-point)))) |
a90256cc BP |
603 | (if (integerp val) |
604 | (setcar indent-stack | |
605 | (setq this-indent val)) | |
606 | (setcar indent-stack (- (car (cdr val)))) | |
607 | (setq this-indent (car val))))) | |
608 | (if (/= (current-column) this-indent) | |
609 | (progn (delete-region bol (point)) | |
610 | (indent-to this-indent))))) | |
611 | (or outer-loop-done | |
612 | (setq outer-loop-done (= (point) last-point)) | |
613 | (setq last-point (point))))))) | |
614 | ||
615 | ;; Indent every line whose first char is between START and END inclusive. | |
616 | (defun lisp-indent-region (start end) | |
617 | (save-excursion | |
a90256cc | 618 | (let ((endmark (copy-marker end))) |
33f268ec RS |
619 | (goto-char start) |
620 | (and (bolp) (not (eolp)) | |
621 | (lisp-indent-line)) | |
a90256cc BP |
622 | (indent-sexp endmark) |
623 | (set-marker endmark nil)))) | |
624 | \f | |
6338c7ba JB |
625 | ;;;; Lisp paragraph filling commands. |
626 | ||
627 | (defun lisp-fill-paragraph (&optional justify) | |
628 | "Like \\[fill-paragraph], but handle Emacs Lisp comments. | |
629 | If any of the current line is a comment, fill the comment or the | |
630 | paragraph of it that point is in, preserving the comment's indentation | |
631 | and initial semicolons." | |
632 | (interactive "P") | |
633 | (let ( | |
634 | ;; Non-nil if the current line contains a comment. | |
635 | has-comment | |
636 | ||
637 | ;; If has-comment, the appropriate fill-prefix for the comment. | |
638 | comment-fill-prefix | |
639 | ) | |
640 | ||
641 | ;; Figure out what kind of comment we are looking at. | |
642 | (save-excursion | |
643 | (beginning-of-line) | |
644 | (cond | |
645 | ||
646 | ;; A line with nothing but a comment on it? | |
647 | ((looking-at "[ \t]*;[; \t]*") | |
648 | (setq has-comment t | |
649 | comment-fill-prefix (buffer-substring (match-beginning 0) | |
650 | (match-end 0)))) | |
651 | ||
652 | ;; A line with some code, followed by a comment? Remember that the | |
653 | ;; semi which starts the comment shouldn't be part of a string or | |
654 | ;; character. | |
655 | ((progn | |
656 | (while (not (looking-at ";\\|$")) | |
657 | (skip-chars-forward "^;\n\"\\\\?") | |
658 | (cond | |
659 | ((eq (char-after (point)) ?\\) (forward-char 2)) | |
660 | ((memq (char-after (point)) '(?\" ??)) (forward-sexp 1)))) | |
661 | (looking-at ";+[\t ]*")) | |
662 | (setq has-comment t) | |
663 | (setq comment-fill-prefix | |
664 | (concat (make-string (current-column) ? ) | |
665 | (buffer-substring (match-beginning 0) (match-end 0))))))) | |
666 | ||
667 | (if (not has-comment) | |
668 | (fill-paragraph justify) | |
669 | ||
670 | ;; Narrow to include only the comment, and then fill the region. | |
671 | (save-restriction | |
672 | (narrow-to-region | |
673 | ;; Find the first line we should include in the region to fill. | |
674 | (save-excursion | |
675 | (while (and (zerop (forward-line -1)) | |
676 | (looking-at "^[ \t]*;"))) | |
677 | ;; We may have gone to far. Go forward again. | |
678 | (or (looking-at "^[ \t]*;") | |
679 | (forward-line 1)) | |
680 | (point)) | |
681 | ;; Find the beginning of the first line past the region to fill. | |
682 | (save-excursion | |
683 | (while (progn (forward-line 1) | |
684 | (looking-at "^[ \t]*;"))) | |
685 | (point))) | |
686 | ||
687 | ;; Lines with only semicolons on them can be paragraph boundaries. | |
688 | (let ((paragraph-start (concat paragraph-start "\\|^[ \t;]*$")) | |
689 | (paragraph-separate (concat paragraph-start "\\|^[ \t;]*$")) | |
690 | (fill-prefix comment-fill-prefix)) | |
2b4483bb RS |
691 | (fill-paragraph justify)))) |
692 | t)) | |
6338c7ba JB |
693 | |
694 | \f | |
a90256cc BP |
695 | (defun indent-code-rigidly (start end arg &optional nochange-regexp) |
696 | "Indent all lines of code, starting in the region, sideways by ARG columns. | |
697 | Does not affect lines starting inside comments or strings, assuming that | |
698 | the start of the region is not inside them. | |
699 | ||
700 | Called from a program, takes args START, END, COLUMNS and NOCHANGE-REGEXP. | |
701 | The last is a regexp which, if matched at the beginning of a line, | |
702 | means don't indent that line." | |
703 | (interactive "r\np") | |
704 | (let (state) | |
705 | (save-excursion | |
706 | (goto-char end) | |
707 | (setq end (point-marker)) | |
708 | (goto-char start) | |
709 | (or (bolp) | |
710 | (setq state (parse-partial-sexp (point) | |
711 | (progn | |
712 | (forward-line 1) (point)) | |
713 | nil nil state))) | |
714 | (while (< (point) end) | |
715 | (or (car (nthcdr 3 state)) | |
716 | (and nochange-regexp | |
717 | (looking-at nochange-regexp)) | |
718 | ;; If line does not start in string, indent it | |
719 | (let ((indent (current-indentation))) | |
720 | (delete-region (point) (progn (skip-chars-forward " \t") (point))) | |
721 | (or (eolp) | |
722 | (indent-to (max 0 (+ indent arg)) 0)))) | |
723 | (setq state (parse-partial-sexp (point) | |
724 | (progn | |
725 | (forward-line 1) (point)) | |
726 | nil nil state)))))) | |
49116ac0 JB |
727 | |
728 | (provide 'lisp-mode) | |
729 | ||
6594deb0 | 730 | ;;; lisp-mode.el ends here |