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