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