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
|
285
|
20 (provide 'lisp-mode)
|
213
|
21
|
|
22 (defvar lisp-mode-syntax-table nil "")
|
|
23 (defvar emacs-lisp-mode-syntax-table nil "")
|
|
24 (defvar lisp-mode-abbrev-table nil "")
|
|
25
|
|
26 (if (not emacs-lisp-mode-syntax-table)
|
|
27 (let ((i 0))
|
|
28 (setq emacs-lisp-mode-syntax-table (make-syntax-table))
|
|
29 (while (< i ?0)
|
|
30 (modify-syntax-entry i "_ " emacs-lisp-mode-syntax-table)
|
|
31 (setq i (1+ i)))
|
|
32 (setq i (1+ ?9))
|
|
33 (while (< i ?A)
|
|
34 (modify-syntax-entry i "_ " emacs-lisp-mode-syntax-table)
|
|
35 (setq i (1+ i)))
|
|
36 (setq i (1+ ?Z))
|
|
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 128)
|
|
42 (modify-syntax-entry i "_ " emacs-lisp-mode-syntax-table)
|
|
43 (setq i (1+ i)))
|
|
44 (modify-syntax-entry ? " " emacs-lisp-mode-syntax-table)
|
|
45 (modify-syntax-entry ?\t " " emacs-lisp-mode-syntax-table)
|
|
46 (modify-syntax-entry ?\n "> " emacs-lisp-mode-syntax-table)
|
|
47 (modify-syntax-entry ?\f "> " 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 (modify-syntax-entry ?, "' " emacs-lisp-mode-syntax-table)
|
|
52 ;; Used to be singlequote; changed for flonums.
|
|
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 (modify-syntax-entry ?\] ")[ " emacs-lisp-mode-syntax-table)))
|
|
61
|
285
|
62 (if (not lisp-mode-syntax-table)
|
|
63 (progn (setq lisp-mode-syntax-table
|
|
64 (copy-syntax-table emacs-lisp-mode-syntax-table))
|
|
65 (modify-syntax-entry ?\| "\" " lisp-mode-syntax-table)
|
|
66 (modify-syntax-entry ?\[ "_ " lisp-mode-syntax-table)
|
|
67 (modify-syntax-entry ?\] "_ " lisp-mode-syntax-table)))
|
|
68
|
213
|
69 (define-abbrev-table 'lisp-mode-abbrev-table ())
|
|
70
|
|
71 (defun lisp-mode-variables (lisp-syntax)
|
|
72 (cond (lisp-syntax
|
|
73 (set-syntax-table lisp-mode-syntax-table)))
|
|
74 (setq local-abbrev-table lisp-mode-abbrev-table)
|
|
75 (make-local-variable 'paragraph-start)
|
|
76 (setq paragraph-start (concat "^$\\|" page-delimiter))
|
|
77 (make-local-variable 'paragraph-separate)
|
|
78 (setq paragraph-separate paragraph-start)
|
|
79 (make-local-variable 'paragraph-ignore-fill-prefix)
|
|
80 (setq paragraph-ignore-fill-prefix t)
|
|
81 (make-local-variable 'indent-line-function)
|
|
82 (setq indent-line-function 'lisp-indent-line)
|
|
83 (make-local-variable 'indent-region-function)
|
|
84 (setq indent-region-function 'lisp-indent-region)
|
|
85 (make-local-variable 'parse-sexp-ignore-comments)
|
|
86 (setq parse-sexp-ignore-comments t)
|
|
87 (make-local-variable 'comment-start)
|
|
88 (setq comment-start ";")
|
|
89 (make-local-variable 'comment-start-skip)
|
|
90 (setq comment-start-skip ";+ *")
|
|
91 (make-local-variable 'comment-column)
|
|
92 (setq comment-column 40)
|
|
93 (make-local-variable 'comment-indent-hook)
|
|
94 (setq comment-indent-hook 'lisp-comment-indent))
|
|
95
|
|
96 (defvar shared-lisp-mode-map ()
|
|
97 "Keymap for commands shared by all sorts of Lisp modes.")
|
|
98
|
|
99 (if shared-lisp-mode-map
|
|
100 ()
|
|
101 (setq shared-lisp-mode-map (make-sparse-keymap))
|
|
102 (define-key shared-lisp-mode-map "\e\C-q" 'indent-sexp)
|
|
103 (define-key shared-lisp-mode-map "\177" 'backward-delete-char-untabify)
|
|
104 (define-key shared-lisp-mode-map "\t" 'lisp-indent-line))
|
|
105
|
|
106 (defvar emacs-lisp-mode-map ()
|
|
107 "Keymap for Emacs Lisp mode.
|
|
108 All commands in shared-lisp-mode-map are inherited by this map.")
|
|
109
|
|
110 (if emacs-lisp-mode-map
|
|
111 ()
|
|
112 (setq emacs-lisp-mode-map
|
|
113 (nconc (make-sparse-keymap) shared-lisp-mode-map))
|
|
114 (define-key emacs-lisp-mode-map "\e\C-x" 'eval-defun))
|
|
115
|
|
116 (defun emacs-lisp-mode ()
|
|
117 "Major mode for editing Lisp code to run in Emacs.
|
|
118 Commands:
|
|
119 Delete converts tabs to spaces as it moves back.
|
|
120 Blank lines separate paragraphs. Semicolons start comments.
|
|
121 \\{emacs-lisp-mode-map}
|
|
122 Entry to this mode calls the value of `emacs-lisp-mode-hook'
|
|
123 if that value is non-nil."
|
|
124 (interactive)
|
|
125 (kill-all-local-variables)
|
|
126 (use-local-map emacs-lisp-mode-map)
|
|
127 (set-syntax-table emacs-lisp-mode-syntax-table)
|
|
128 (setq major-mode 'emacs-lisp-mode)
|
|
129 (setq mode-name "Emacs-Lisp")
|
|
130 (lisp-mode-variables nil)
|
|
131 (run-hooks 'emacs-lisp-mode-hook))
|
|
132
|
|
133 (defvar lisp-mode-map ()
|
|
134 "Keymap for ordinary Lisp mode.
|
|
135 All commands in `shared-lisp-mode-map' are inherited by this map.")
|
|
136
|
|
137 (if lisp-mode-map
|
|
138 ()
|
|
139 (setq lisp-mode-map
|
|
140 (nconc (make-sparse-keymap) shared-lisp-mode-map))
|
|
141 (define-key lisp-mode-map "\e\C-x" 'lisp-send-defun)
|
|
142 (define-key lisp-mode-map "\C-c\C-l" 'run-lisp))
|
|
143
|
|
144 (defun lisp-mode ()
|
|
145 "Major mode for editing Lisp code for Lisps other than GNU Emacs Lisp.
|
|
146 Commands:
|
|
147 Delete converts tabs to spaces as it moves back.
|
|
148 Blank lines separate paragraphs. Semicolons start comments.
|
|
149 \\{lisp-mode-map}
|
|
150 Note that `run-lisp' may be used either to start an inferior Lisp job
|
|
151 or to switch back to an existing one.
|
|
152
|
|
153 Entry to this mode calls the value of `lisp-mode-hook'
|
|
154 if that value is non-nil."
|
|
155 (interactive)
|
|
156 (kill-all-local-variables)
|
|
157 (use-local-map lisp-mode-map)
|
|
158 (setq major-mode 'lisp-mode)
|
|
159 (setq mode-name "Lisp")
|
|
160 (lisp-mode-variables t)
|
|
161 (set-syntax-table lisp-mode-syntax-table)
|
|
162 (run-hooks 'lisp-mode-hook))
|
|
163
|
|
164 ;; This will do unless shell.el is loaded.
|
|
165 (defun lisp-send-defun nil
|
|
166 "Send the current defun to the Lisp process made by \\[run-lisp]."
|
|
167 (interactive)
|
|
168 (error "Process lisp does not exist"))
|
|
169
|
|
170 (defvar lisp-interaction-mode-map ()
|
|
171 "Keymap for Lisp Interaction moe.
|
|
172 All commands in `shared-lisp-mode-map' are inherited by this map.")
|
|
173
|
|
174 (if lisp-interaction-mode-map
|
|
175 ()
|
|
176 (setq lisp-interaction-mode-map
|
|
177 (nconc (make-sparse-keymap) shared-lisp-mode-map))
|
|
178 (define-key lisp-interaction-mode-map "\e\C-x" 'eval-defun)
|
|
179 (define-key lisp-interaction-mode-map "\n" 'eval-print-last-sexp))
|
|
180
|
|
181 (defun lisp-interaction-mode ()
|
|
182 "Major mode for typing and evaluating Lisp forms.
|
|
183 Like Lisp mode except that \\[eval-print-last-sexp] evals the Lisp expression
|
|
184 before point, and prints its value into the buffer, advancing point.
|
|
185
|
|
186 Commands:
|
|
187 Delete converts tabs to spaces as it moves back.
|
|
188 Paragraphs are separated only by blank lines.
|
|
189 Semicolons start comments.
|
|
190 \\{lisp-interaction-mode-map}
|
|
191 Entry to this mode calls the value of `lisp-interaction-mode-hook'
|
|
192 if that value is non-nil."
|
|
193 (interactive)
|
|
194 (kill-all-local-variables)
|
|
195 (use-local-map lisp-interaction-mode-map)
|
|
196 (setq major-mode 'lisp-interaction-mode)
|
|
197 (setq mode-name "Lisp Interaction")
|
|
198 (set-syntax-table emacs-lisp-mode-syntax-table)
|
|
199 (lisp-mode-variables nil)
|
|
200 (run-hooks 'lisp-interaction-mode-hook))
|
|
201
|
248
|
202 (defun eval-print-last-sexp ()
|
213
|
203 "Evaluate sexp before point; print value into current buffer."
|
248
|
204 (interactive)
|
255
|
205 (terpri (current-buffer))
|
254
|
206 (eval-last-sexp t)
|
255
|
207 (terpri (current-buffer)))
|
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))))))
|