Mercurial > emacs
annotate lisp/emacs-lisp/lisp-mode.el @ 908:94eb4344341b
*** empty log message ***
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Mon, 03 Aug 1992 01:04:04 +0000 |
parents | 5b1c5b4286e7 |
children | c205d560cc22 |
rev | line source |
---|---|
659
505130d1ddf8
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
584
diff
changeset
|
1 ;;; lisp-mode.el --- Lisp mode, and its idiosyncratic commands. |
505130d1ddf8
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
584
diff
changeset
|
2 |
846
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
811
diff
changeset
|
3 ;; Copyright (C) 1985 Free Software Foundation, Inc. |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
811
diff
changeset
|
4 |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
727
diff
changeset
|
5 ;; Maintainer: FSF |
811
e694e0879463
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
807
diff
changeset
|
6 ;; Keywords: lisp, languages |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
727
diff
changeset
|
7 |
213 | 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 | |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
727
diff
changeset
|
12 ;; the Free Software Foundation; either version 2, or (at your option) |
213 | 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 | |
24 (defvar lisp-mode-syntax-table nil "") | |
25 (defvar emacs-lisp-mode-syntax-table nil "") | |
26 (defvar lisp-mode-abbrev-table nil "") | |
27 | |
28 (if (not emacs-lisp-mode-syntax-table) | |
29 (let ((i 0)) | |
30 (setq emacs-lisp-mode-syntax-table (make-syntax-table)) | |
31 (while (< i ?0) | |
32 (modify-syntax-entry i "_ " emacs-lisp-mode-syntax-table) | |
33 (setq i (1+ i))) | |
34 (setq i (1+ ?9)) | |
35 (while (< i ?A) | |
36 (modify-syntax-entry i "_ " emacs-lisp-mode-syntax-table) | |
37 (setq i (1+ i))) | |
38 (setq i (1+ ?Z)) | |
39 (while (< i ?a) | |
40 (modify-syntax-entry i "_ " emacs-lisp-mode-syntax-table) | |
41 (setq i (1+ i))) | |
42 (setq i (1+ ?z)) | |
43 (while (< i 128) | |
44 (modify-syntax-entry i "_ " emacs-lisp-mode-syntax-table) | |
45 (setq i (1+ i))) | |
46 (modify-syntax-entry ? " " emacs-lisp-mode-syntax-table) | |
47 (modify-syntax-entry ?\t " " emacs-lisp-mode-syntax-table) | |
48 (modify-syntax-entry ?\n "> " emacs-lisp-mode-syntax-table) | |
49 (modify-syntax-entry ?\f "> " emacs-lisp-mode-syntax-table) | |
50 (modify-syntax-entry ?\; "< " emacs-lisp-mode-syntax-table) | |
51 (modify-syntax-entry ?` "' " emacs-lisp-mode-syntax-table) | |
52 (modify-syntax-entry ?' "' " emacs-lisp-mode-syntax-table) | |
53 (modify-syntax-entry ?, "' " emacs-lisp-mode-syntax-table) | |
54 ;; Used to be singlequote; changed for flonums. | |
55 (modify-syntax-entry ?. "_ " emacs-lisp-mode-syntax-table) | |
56 (modify-syntax-entry ?# "' " emacs-lisp-mode-syntax-table) | |
57 (modify-syntax-entry ?\" "\" " emacs-lisp-mode-syntax-table) | |
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 (modify-syntax-entry ?\] ")[ " emacs-lisp-mode-syntax-table))) | |
63 | |
285 | 64 (if (not lisp-mode-syntax-table) |
65 (progn (setq lisp-mode-syntax-table | |
66 (copy-syntax-table emacs-lisp-mode-syntax-table)) | |
67 (modify-syntax-entry ?\| "\" " lisp-mode-syntax-table) | |
68 (modify-syntax-entry ?\[ "_ " lisp-mode-syntax-table) | |
69 (modify-syntax-entry ?\] "_ " lisp-mode-syntax-table))) | |
70 | |
213 | 71 (define-abbrev-table 'lisp-mode-abbrev-table ()) |
72 | |
73 (defun lisp-mode-variables (lisp-syntax) | |
74 (cond (lisp-syntax | |
75 (set-syntax-table lisp-mode-syntax-table))) | |
76 (setq local-abbrev-table lisp-mode-abbrev-table) | |
77 (make-local-variable 'paragraph-start) | |
78 (setq paragraph-start (concat "^$\\|" page-delimiter)) | |
79 (make-local-variable 'paragraph-separate) | |
80 (setq paragraph-separate paragraph-start) | |
81 (make-local-variable 'paragraph-ignore-fill-prefix) | |
82 (setq paragraph-ignore-fill-prefix t) | |
83 (make-local-variable 'indent-line-function) | |
84 (setq indent-line-function 'lisp-indent-line) | |
85 (make-local-variable 'indent-region-function) | |
86 (setq indent-region-function 'lisp-indent-region) | |
87 (make-local-variable 'parse-sexp-ignore-comments) | |
88 (setq parse-sexp-ignore-comments t) | |
89 (make-local-variable 'comment-start) | |
90 (setq comment-start ";") | |
91 (make-local-variable 'comment-start-skip) | |
92 (setq comment-start-skip ";+ *") | |
93 (make-local-variable 'comment-column) | |
94 (setq comment-column 40) | |
95 (make-local-variable 'comment-indent-hook) | |
96 (setq comment-indent-hook 'lisp-comment-indent)) | |
97 | |
98 (defvar shared-lisp-mode-map () | |
99 "Keymap for commands shared by all sorts of Lisp modes.") | |
100 | |
101 (if shared-lisp-mode-map | |
102 () | |
103 (setq shared-lisp-mode-map (make-sparse-keymap)) | |
104 (define-key shared-lisp-mode-map "\e\C-q" 'indent-sexp) | |
105 (define-key shared-lisp-mode-map "\177" 'backward-delete-char-untabify) | |
106 (define-key shared-lisp-mode-map "\t" 'lisp-indent-line)) | |
107 | |
108 (defvar emacs-lisp-mode-map () | |
109 "Keymap for Emacs Lisp mode. | |
110 All commands in shared-lisp-mode-map are inherited by this map.") | |
111 | |
112 (if emacs-lisp-mode-map | |
113 () | |
114 (setq emacs-lisp-mode-map | |
115 (nconc (make-sparse-keymap) shared-lisp-mode-map)) | |
878
5b1c5b4286e7
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
116 (define-key emacs-lisp-mode-map "\e\t" 'lisp-complete-symbol) |
213 | 117 (define-key emacs-lisp-mode-map "\e\C-x" 'eval-defun)) |
118 | |
119 (defun emacs-lisp-mode () | |
120 "Major mode for editing Lisp code to run in Emacs. | |
121 Commands: | |
122 Delete converts tabs to spaces as it moves back. | |
123 Blank lines separate paragraphs. Semicolons start comments. | |
124 \\{emacs-lisp-mode-map} | |
125 Entry to this mode calls the value of `emacs-lisp-mode-hook' | |
126 if that value is non-nil." | |
127 (interactive) | |
128 (kill-all-local-variables) | |
129 (use-local-map emacs-lisp-mode-map) | |
130 (set-syntax-table emacs-lisp-mode-syntax-table) | |
131 (setq major-mode 'emacs-lisp-mode) | |
132 (setq mode-name "Emacs-Lisp") | |
133 (lisp-mode-variables nil) | |
134 (run-hooks 'emacs-lisp-mode-hook)) | |
135 | |
136 (defvar lisp-mode-map () | |
137 "Keymap for ordinary Lisp mode. | |
138 All commands in `shared-lisp-mode-map' are inherited by this map.") | |
139 | |
140 (if lisp-mode-map | |
141 () | |
142 (setq lisp-mode-map | |
143 (nconc (make-sparse-keymap) shared-lisp-mode-map)) | |
144 (define-key lisp-mode-map "\e\C-x" 'lisp-send-defun) | |
145 (define-key lisp-mode-map "\C-c\C-l" 'run-lisp)) | |
146 | |
147 (defun lisp-mode () | |
148 "Major mode for editing Lisp code for Lisps other than GNU Emacs Lisp. | |
149 Commands: | |
150 Delete converts tabs to spaces as it moves back. | |
151 Blank lines separate paragraphs. Semicolons start comments. | |
152 \\{lisp-mode-map} | |
153 Note that `run-lisp' may be used either to start an inferior Lisp job | |
154 or to switch back to an existing one. | |
155 | |
156 Entry to this mode calls the value of `lisp-mode-hook' | |
157 if that value is non-nil." | |
158 (interactive) | |
159 (kill-all-local-variables) | |
160 (use-local-map lisp-mode-map) | |
161 (setq major-mode 'lisp-mode) | |
162 (setq mode-name "Lisp") | |
163 (lisp-mode-variables t) | |
164 (set-syntax-table lisp-mode-syntax-table) | |
165 (run-hooks 'lisp-mode-hook)) | |
166 | |
167 ;; This will do unless shell.el is loaded. | |
168 (defun lisp-send-defun nil | |
169 "Send the current defun to the Lisp process made by \\[run-lisp]." | |
170 (interactive) | |
171 (error "Process lisp does not exist")) | |
172 | |
173 (defvar lisp-interaction-mode-map () | |
174 "Keymap for Lisp Interaction moe. | |
175 All commands in `shared-lisp-mode-map' are inherited by this map.") | |
176 | |
177 (if lisp-interaction-mode-map | |
178 () | |
179 (setq lisp-interaction-mode-map | |
180 (nconc (make-sparse-keymap) shared-lisp-mode-map)) | |
181 (define-key lisp-interaction-mode-map "\e\C-x" 'eval-defun) | |
878
5b1c5b4286e7
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
182 (define-key lisp-interaction-mode-map "\e\t" 'lisp-complete-symbol) |
213 | 183 (define-key lisp-interaction-mode-map "\n" 'eval-print-last-sexp)) |
184 | |
185 (defun lisp-interaction-mode () | |
186 "Major mode for typing and evaluating Lisp forms. | |
187 Like Lisp mode except that \\[eval-print-last-sexp] evals the Lisp expression | |
188 before point, and prints its value into the buffer, advancing point. | |
189 | |
190 Commands: | |
191 Delete converts tabs to spaces as it moves back. | |
192 Paragraphs are separated only by blank lines. | |
193 Semicolons start comments. | |
194 \\{lisp-interaction-mode-map} | |
195 Entry to this mode calls the value of `lisp-interaction-mode-hook' | |
196 if that value is non-nil." | |
197 (interactive) | |
198 (kill-all-local-variables) | |
199 (use-local-map lisp-interaction-mode-map) | |
200 (setq major-mode 'lisp-interaction-mode) | |
201 (setq mode-name "Lisp Interaction") | |
202 (set-syntax-table emacs-lisp-mode-syntax-table) | |
203 (lisp-mode-variables nil) | |
204 (run-hooks 'lisp-interaction-mode-hook)) | |
205 | |
248 | 206 (defun eval-print-last-sexp () |
213 | 207 "Evaluate sexp before point; print value into current buffer." |
248 | 208 (interactive) |
422 | 209 (let ((standard-output (current-buffer))) |
210 (terpri) | |
211 (eval-last-sexp t) | |
212 (terpri))) | |
213 | 213 |
214 (defun eval-last-sexp (arg) | |
215 "Evaluate sexp before point; print value in minibuffer. | |
216 With argument, print output into current buffer." | |
217 (interactive "P") | |
249 | 218 (let ((standard-output (if arg (current-buffer) t))) |
219 (prin1 (let ((stab (syntax-table))) | |
220 (eval (unwind-protect | |
221 (save-excursion | |
222 (set-syntax-table emacs-lisp-mode-syntax-table) | |
223 (forward-sexp -1) | |
224 (read (current-buffer))) | |
225 (set-syntax-table stab))))))) | |
213 | 226 |
227 (defun eval-defun (arg) | |
248 | 228 "Evaluate defun that point is in or before. |
229 Print value in minibuffer. | |
230 With argument, insert value in current buffer after the defun." | |
213 | 231 (interactive "P") |
249 | 232 (let ((standard-output (if arg (current-buffer) t))) |
233 (prin1 (eval (save-excursion | |
234 (end-of-defun) | |
235 (beginning-of-defun) | |
236 (read (current-buffer))))))) | |
213 | 237 |
238 (defun lisp-comment-indent () | |
239 (if (looking-at "\\s<\\s<\\s<") | |
240 (current-column) | |
241 (if (looking-at "\\s<\\s<") | |
242 (let ((tem (calculate-lisp-indent))) | |
243 (if (listp tem) (car tem) tem)) | |
244 (skip-chars-backward " \t") | |
245 (max (if (bolp) 0 (1+ (current-column))) | |
246 comment-column)))) | |
247 | |
248 (defconst lisp-indent-offset nil "") | |
249 (defconst lisp-indent-function 'lisp-indent-function "") | |
250 | |
251 (defun lisp-indent-line (&optional whole-exp) | |
252 "Indent current line as Lisp code. | |
253 With argument, indent any additional lines of the same expression | |
254 rigidly along with this one." | |
255 (interactive "P") | |
256 (let ((indent (calculate-lisp-indent)) shift-amt beg end | |
257 (pos (- (point-max) (point)))) | |
258 (beginning-of-line) | |
259 (setq beg (point)) | |
260 (skip-chars-forward " \t") | |
261 (if (looking-at "\\s<\\s<\\s<") | |
262 ;; Don't alter indentation of a ;;; comment line. | |
677
7a9b4ea68565
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
659
diff
changeset
|
263 (goto-char (- (point-max) pos)) |
213 | 264 (if (and (looking-at "\\s<") (not (looking-at "\\s<\\s<"))) |
265 ;; Single-semicolon comment lines should be indented | |
266 ;; as comment lines, not as code. | |
267 (progn (indent-for-comment) (forward-char -1)) | |
268 (if (listp indent) (setq indent (car indent))) | |
269 (setq shift-amt (- indent (current-column))) | |
270 (if (zerop shift-amt) | |
271 nil | |
272 (delete-region beg (point)) | |
273 (indent-to indent))) | |
274 ;; If initial point was within line's indentation, | |
275 ;; position after the indentation. Else stay at same point in text. | |
276 (if (> (- (point-max) pos) (point)) | |
277 (goto-char (- (point-max) pos))) | |
278 ;; If desired, shift remaining lines of expression the same amount. | |
279 (and whole-exp (not (zerop shift-amt)) | |
280 (save-excursion | |
281 (goto-char beg) | |
282 (forward-sexp 1) | |
283 (setq end (point)) | |
284 (goto-char beg) | |
285 (forward-line 1) | |
286 (setq beg (point)) | |
287 (> end beg)) | |
288 (indent-code-rigidly beg end shift-amt))))) | |
289 | |
290 (defun calculate-lisp-indent (&optional parse-start) | |
291 "Return appropriate indentation for current line as Lisp code. | |
292 In usual case returns an integer: the column to indent to. | |
293 Can instead return a list, whose car is the column to indent to. | |
294 This means that following lines at the same level of indentation | |
295 should not necessarily be indented the same way. | |
296 The second element of the list is the buffer position | |
297 of the start of the containing expression." | |
298 (save-excursion | |
299 (beginning-of-line) | |
300 (let ((indent-point (point)) | |
301 state paren-depth | |
302 ;; setting this to a number inhibits calling hook | |
303 (desired-indent nil) | |
304 (retry t) | |
305 last-sexp containing-sexp) | |
306 (if parse-start | |
307 (goto-char parse-start) | |
308 (beginning-of-defun)) | |
309 ;; Find outermost containing sexp | |
310 (while (< (point) indent-point) | |
311 (setq state (parse-partial-sexp (point) indent-point 0))) | |
312 ;; Find innermost containing sexp | |
313 (while (and retry | |
314 state | |
315 (> (setq paren-depth (elt state 0)) 0)) | |
316 (setq retry nil) | |
317 (setq last-sexp (elt state 2)) | |
318 (setq containing-sexp (elt state 1)) | |
319 ;; Position following last unclosed open. | |
320 (goto-char (1+ containing-sexp)) | |
321 ;; Is there a complete sexp since then? | |
322 (if (and last-sexp (> last-sexp (point))) | |
323 ;; Yes, but is there a containing sexp after that? | |
324 (let ((peek (parse-partial-sexp last-sexp indent-point 0))) | |
325 (if (setq retry (car (cdr peek))) (setq state peek))))) | |
326 (if retry | |
327 nil | |
328 ;; Innermost containing sexp found | |
329 (goto-char (1+ containing-sexp)) | |
330 (if (not last-sexp) | |
331 ;; indent-point immediately follows open paren. | |
332 ;; Don't call hook. | |
333 (setq desired-indent (current-column)) | |
334 ;; Find the start of first element of containing sexp. | |
335 (parse-partial-sexp (point) last-sexp 0 t) | |
336 (cond ((looking-at "\\s(") | |
337 ;; First element of containing sexp is a list. | |
338 ;; Indent under that list. | |
339 ) | |
340 ((> (save-excursion (forward-line 1) (point)) | |
341 last-sexp) | |
342 ;; This is the first line to start within the containing sexp. | |
343 ;; It's almost certainly a function call. | |
344 (if (= (point) last-sexp) | |
345 ;; Containing sexp has nothing before this line | |
346 ;; except the first element. Indent under that element. | |
347 nil | |
348 ;; Skip the first element, find start of second (the first | |
349 ;; argument of the function call) and indent under. | |
350 (progn (forward-sexp 1) | |
351 (parse-partial-sexp (point) last-sexp 0 t))) | |
352 (backward-prefix-chars)) | |
353 (t | |
354 ;; Indent beneath first sexp on same line as last-sexp. | |
355 ;; Again, it's almost certainly a function call. | |
356 (goto-char last-sexp) | |
357 (beginning-of-line) | |
358 (parse-partial-sexp (point) last-sexp 0 t) | |
359 (backward-prefix-chars))))) | |
360 ;; Point is at the point to indent under unless we are inside a string. | |
361 ;; Call indentation hook except when overriden by lisp-indent-offset | |
362 ;; or if the desired indentation has already been computed. | |
363 (let ((normal-indent (current-column))) | |
364 (cond ((elt state 3) | |
365 ;; Inside a string, don't change indentation. | |
366 (goto-char indent-point) | |
367 (skip-chars-forward " \t") | |
368 (current-column)) | |
369 ((and (integerp lisp-indent-offset) containing-sexp) | |
370 ;; Indent by constant offset | |
371 (goto-char containing-sexp) | |
372 (+ (current-column) lisp-indent-offset)) | |
373 (desired-indent) | |
374 ((and (boundp 'lisp-indent-function) | |
375 lisp-indent-function | |
376 (not retry)) | |
377 (or (funcall lisp-indent-function indent-point state) | |
378 normal-indent)) | |
379 (t | |
214 | 380 normal-indent)))))) |
213 | 381 |
382 (defun lisp-indent-function (indent-point state) | |
383 (let ((normal-indent (current-column))) | |
384 (goto-char (1+ (elt state 1))) | |
385 (parse-partial-sexp (point) last-sexp 0 t) | |
386 (if (and (elt state 2) | |
387 (not (looking-at "\\sw\\|\\s_"))) | |
388 ;; car of form doesn't seem to be a a symbol | |
389 (progn | |
390 (if (not (> (save-excursion (forward-line 1) (point)) | |
391 last-sexp)) | |
392 (progn (goto-char last-sexp) | |
393 (beginning-of-line) | |
394 (parse-partial-sexp (point) last-sexp 0 t))) | |
395 ;; Indent under the list or under the first sexp on the | |
396 ;; same line as last-sexp. Note that first thing on that | |
397 ;; line has to be complete sexp since we are inside the | |
398 ;; innermost containing sexp. | |
399 (backward-prefix-chars) | |
400 (current-column)) | |
401 (let ((function (buffer-substring (point) | |
402 (progn (forward-sexp 1) (point)))) | |
403 method) | |
404 (setq method (get (intern-soft function) 'lisp-indent-function)) | |
405 (cond ((or (eq method 'defun) | |
406 (and (null method) | |
407 (> (length function) 3) | |
408 (string-match "\\`def" function))) | |
409 (lisp-indent-defform state indent-point)) | |
410 ((integerp method) | |
411 (lisp-indent-specform method state | |
412 indent-point normal-indent)) | |
413 (method | |
414 (funcall method state indent-point))))))) | |
415 | |
416 (defconst lisp-body-indent 2 "") | |
417 | |
418 (defun lisp-indent-specform (count state indent-point normal-indent) | |
419 (let ((containing-form-start (elt state 1)) | |
420 (i count) | |
421 body-indent containing-form-column) | |
422 ;; Move to the start of containing form, calculate indentation | |
423 ;; to use for non-distinguished forms (> count), and move past the | |
424 ;; function symbol. lisp-indent-function guarantees that there is at | |
425 ;; least one word or symbol character following open paren of containing | |
426 ;; form. | |
427 (goto-char containing-form-start) | |
428 (setq containing-form-column (current-column)) | |
429 (setq body-indent (+ lisp-body-indent containing-form-column)) | |
430 (forward-char 1) | |
431 (forward-sexp 1) | |
432 ;; Now find the start of the last form. | |
433 (parse-partial-sexp (point) indent-point 1 t) | |
434 (while (and (< (point) indent-point) | |
435 (condition-case () | |
436 (progn | |
437 (setq count (1- count)) | |
438 (forward-sexp 1) | |
439 (parse-partial-sexp (point) indent-point 1 t)) | |
440 (error nil)))) | |
441 ;; Point is sitting on first character of last (or count) sexp. | |
442 (if (> count 0) | |
443 ;; A distinguished form. If it is the first or second form use double | |
444 ;; lisp-body-indent, else normal indent. With lisp-body-indent bound | |
445 ;; to 2 (the default), this just happens to work the same with if as | |
446 ;; the older code, but it makes unwind-protect, condition-case, | |
447 ;; with-output-to-temp-buffer, et. al. much more tasteful. The older, | |
448 ;; less hacked, behavior can be obtained by replacing below with | |
449 ;; (list normal-indent containing-form-start). | |
450 (if (<= (- i count) 1) | |
451 (list (+ containing-form-column (* 2 lisp-body-indent)) | |
452 containing-form-start) | |
453 (list normal-indent containing-form-start)) | |
454 ;; A non-distinguished form. Use body-indent if there are no | |
455 ;; distinguished forms and this is the first undistinguished form, | |
456 ;; or if this is the first undistinguished form and the preceding | |
457 ;; distinguished form has indentation at least as great as body-indent. | |
458 (if (or (and (= i 0) (= count 0)) | |
459 (and (= count 0) (<= body-indent normal-indent))) | |
460 body-indent | |
461 normal-indent)))) | |
462 | |
463 (defun lisp-indent-defform (state indent-point) | |
464 (goto-char (car (cdr state))) | |
465 (forward-line 1) | |
466 (if (> (point) (car (cdr (cdr state)))) | |
467 (progn | |
468 (goto-char (car (cdr state))) | |
469 (+ lisp-body-indent (current-column))))) | |
470 | |
471 | |
472 ;; (put 'progn 'lisp-indent-function 0), say, causes progn to be indented | |
473 ;; like defun if the first form is placed on the next line, otherwise | |
474 ;; it is indented like any other form (i.e. forms line up under first). | |
475 | |
476 (put 'lambda 'lisp-indent-function 'defun) | |
477 (put 'autoload 'lisp-indent-function 'defun) | |
478 (put 'progn 'lisp-indent-function 0) | |
479 (put 'prog1 'lisp-indent-function 1) | |
480 (put 'prog2 'lisp-indent-function 2) | |
481 (put 'save-excursion 'lisp-indent-function 0) | |
482 (put 'save-window-excursion 'lisp-indent-function 0) | |
483 (put 'save-restriction 'lisp-indent-function 0) | |
484 (put 'let 'lisp-indent-function 1) | |
485 (put 'let* 'lisp-indent-function 1) | |
486 (put 'while 'lisp-indent-function 1) | |
487 (put 'if 'lisp-indent-function 2) | |
488 (put 'catch 'lisp-indent-function 1) | |
489 (put 'condition-case 'lisp-indent-function 2) | |
490 (put 'unwind-protect 'lisp-indent-function 1) | |
491 (put 'with-output-to-temp-buffer 'lisp-indent-function 1) | |
492 | |
493 (defun indent-sexp (&optional endpos) | |
494 "Indent each line of the list starting just after point. | |
495 If optional arg ENDPOS is given, indent each line, stopping when | |
496 ENDPOS is encountered." | |
497 (interactive) | |
727 | 498 (let ((indent-stack (list nil)) |
499 (next-depth 0) | |
500 (starting-point (point)) | |
501 (last-point (point)) | |
502 last-depth bol outer-loop-done inner-loop-done state this-indent) | |
213 | 503 ;; Get error now if we don't have a complete sexp after point. |
504 (save-excursion (forward-sexp 1)) | |
505 (save-excursion | |
506 (setq outer-loop-done nil) | |
507 (while (if endpos (< (point) endpos) | |
508 (not outer-loop-done)) | |
509 (setq last-depth next-depth | |
510 inner-loop-done nil) | |
511 ;; Parse this line so we can learn the state | |
512 ;; to indent the next line. | |
513 ;; This inner loop goes through only once | |
514 ;; unless a line ends inside a string. | |
515 (while (and (not inner-loop-done) | |
516 (not (setq outer-loop-done (eobp)))) | |
517 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point)) | |
518 nil nil state)) | |
519 (setq next-depth (car state)) | |
520 ;; If the line contains a comment other than the sort | |
521 ;; that is indented like code, | |
522 ;; indent it now with indent-for-comment. | |
523 ;; Comments indented like code are right already. | |
524 ;; In any case clear the in-comment flag in the state | |
525 ;; because parse-partial-sexp never sees the newlines. | |
526 (if (car (nthcdr 4 state)) | |
527 (progn (indent-for-comment) | |
528 (end-of-line) | |
529 (setcar (nthcdr 4 state) nil))) | |
530 ;; If this line ends inside a string, | |
531 ;; go straight to next line, remaining within the inner loop, | |
532 ;; and turn off the \-flag. | |
533 (if (car (nthcdr 3 state)) | |
534 (progn | |
535 (forward-line 1) | |
536 (setcar (nthcdr 5 state) nil)) | |
537 (setq inner-loop-done t))) | |
538 (and endpos | |
727 | 539 (<= next-depth 0) |
540 (progn | |
541 (setq indent-stack (append indent-stack | |
542 (make-list (- next-depth) nil)) | |
543 last-depth (- last-depth next-depth) | |
544 next-depth 0))) | |
213 | 545 (or outer-loop-done |
546 (setq outer-loop-done (<= next-depth 0))) | |
547 (if outer-loop-done | |
548 nil | |
549 (while (> last-depth next-depth) | |
550 (setq indent-stack (cdr indent-stack) | |
551 last-depth (1- last-depth))) | |
552 (while (< last-depth next-depth) | |
553 (setq indent-stack (cons nil indent-stack) | |
554 last-depth (1+ last-depth))) | |
555 ;; Now go to the next line and indent it according | |
556 ;; to what we learned from parsing the previous one. | |
557 (forward-line 1) | |
558 (setq bol (point)) | |
559 (skip-chars-forward " \t") | |
560 ;; But not if the line is blank, or just a comment | |
561 ;; (except for double-semi comments; indent them as usual). | |
562 (if (or (eobp) (looking-at "\\s<\\|\n")) | |
563 nil | |
564 (if (and (car indent-stack) | |
565 (>= (car indent-stack) 0)) | |
566 (setq this-indent (car indent-stack)) | |
567 (let ((val (calculate-lisp-indent | |
568 (if (car indent-stack) (- (car indent-stack)) | |
727 | 569 starting-point)))) |
213 | 570 (if (integerp val) |
571 (setcar indent-stack | |
572 (setq this-indent val)) | |
573 (setcar indent-stack (- (car (cdr val)))) | |
574 (setq this-indent (car val))))) | |
575 (if (/= (current-column) this-indent) | |
576 (progn (delete-region bol (point)) | |
577 (indent-to this-indent))))) | |
578 (or outer-loop-done | |
579 (setq outer-loop-done (= (point) last-point)) | |
580 (setq last-point (point))))))) | |
581 | |
582 ;; Indent every line whose first char is between START and END inclusive. | |
583 (defun lisp-indent-region (start end) | |
584 (save-excursion | |
585 (goto-char start) | |
586 (and (bolp) (not (eolp)) | |
587 (lisp-indent-line)) | |
588 (let ((endmark (copy-marker end))) | |
589 (indent-sexp endmark) | |
590 (set-marker endmark nil)))) | |
591 | |
592 (defun indent-code-rigidly (start end arg &optional nochange-regexp) | |
593 "Indent all lines of code, starting in the region, sideways by ARG columns. | |
594 Does not affect lines starting inside comments or strings, assuming that | |
595 the start of the region is not inside them. | |
596 | |
597 Called from a program, takes args START, END, COLUMNS and NOCHANGE-REGEXP. | |
598 The last is a regexp which, if matched at the beginning of a line, | |
599 means don't indent that line." | |
600 (interactive "r\np") | |
601 (let (state) | |
602 (save-excursion | |
603 (goto-char end) | |
604 (setq end (point-marker)) | |
605 (goto-char start) | |
606 (or (bolp) | |
607 (setq state (parse-partial-sexp (point) | |
608 (progn | |
609 (forward-line 1) (point)) | |
610 nil nil state))) | |
611 (while (< (point) end) | |
612 (or (car (nthcdr 3 state)) | |
613 (and nochange-regexp | |
614 (looking-at nochange-regexp)) | |
615 ;; If line does not start in string, indent it | |
616 (let ((indent (current-indentation))) | |
617 (delete-region (point) (progn (skip-chars-forward " \t") (point))) | |
618 (or (eolp) | |
619 (indent-to (max 0 (+ indent arg)) 0)))) | |
620 (setq state (parse-partial-sexp (point) | |
621 (progn | |
622 (forward-line 1) (point)) | |
623 nil nil state)))))) | |
584 | 624 |
625 (provide 'lisp-mode) | |
626 | |
659
505130d1ddf8
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
584
diff
changeset
|
627 ;;; lisp-mode.el ends here |