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