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