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