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