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