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