1 ;;; lisp-mode.el --- Lisp mode, and its idiosyncratic commands.
3 ;; Copyright (C) 1985 Free Software Foundation, Inc.
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)
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.
26 ;; The base major mode for editing Lisp code (used also for Emacs Lisp).
27 ;; This mode is documented in the Emacs manual
31 (defvar lisp-mode-syntax-table nil "")
32 (defvar emacs-lisp-mode-syntax-table nil "")
33 (defvar lisp-mode-abbrev-table nil "")
35 (if (not emacs-lisp-mode-syntax-table)
37 (setq emacs-lisp-mode-syntax-table (make-syntax-table))
39 (modify-syntax-entry i "_ " emacs-lisp-mode-syntax-table)
43 (modify-syntax-entry i "_ " emacs-lisp-mode-syntax-table)
47 (modify-syntax-entry i "_ " emacs-lisp-mode-syntax-table)
51 (modify-syntax-entry i "_ " emacs-lisp-mode-syntax-table)
53 (modify-syntax-entry ? " " emacs-lisp-mode-syntax-table)
54 (modify-syntax-entry ?\t " " emacs-lisp-mode-syntax-table)
55 (modify-syntax-entry ?\n "> " emacs-lisp-mode-syntax-table)
56 ;; Give CR the same syntax as newline, for selective-display.
57 (modify-syntax-entry ?\^m "> " 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 ;; Used to be singlequote; changed for flonums.
63 (modify-syntax-entry ?. "_ " emacs-lisp-mode-syntax-table)
64 (modify-syntax-entry ?# "' " emacs-lisp-mode-syntax-table)
65 (modify-syntax-entry ?\" "\" " emacs-lisp-mode-syntax-table)
66 (modify-syntax-entry ?\\ "\\ " emacs-lisp-mode-syntax-table)
67 (modify-syntax-entry ?\( "() " emacs-lisp-mode-syntax-table)
68 (modify-syntax-entry ?\) ")( " emacs-lisp-mode-syntax-table)
69 (modify-syntax-entry ?\[ "(] " emacs-lisp-mode-syntax-table)
70 (modify-syntax-entry ?\] ")[ " emacs-lisp-mode-syntax-table)))
72 (if (not lisp-mode-syntax-table)
73 (progn (setq lisp-mode-syntax-table
74 (copy-syntax-table emacs-lisp-mode-syntax-table))
75 (modify-syntax-entry ?\| "\" " lisp-mode-syntax-table)
76 (modify-syntax-entry ?\[ "_ " lisp-mode-syntax-table)
77 (modify-syntax-entry ?\] "_ " lisp-mode-syntax-table)))
79 (define-abbrev-table 'lisp-mode-abbrev-table ())
81 (defun lisp-mode-variables (lisp-syntax)
83 (set-syntax-table lisp-mode-syntax-table)))
84 (setq local-abbrev-table lisp-mode-abbrev-table)
85 (make-local-variable 'paragraph-start)
86 (setq paragraph-start (concat "^$\\|" page-delimiter))
87 (make-local-variable 'paragraph-separate)
88 (setq paragraph-separate paragraph-start)
89 (make-local-variable 'paragraph-ignore-fill-prefix)
90 (setq paragraph-ignore-fill-prefix t)
91 (make-local-variable 'fill-paragraph-function)
92 (setq fill-paragraph-function 'lisp-fill-paragraph)
93 (make-local-variable 'indent-line-function)
94 (setq indent-line-function 'lisp-indent-line)
95 (make-local-variable 'indent-region-function)
96 (setq indent-region-function 'lisp-indent-region)
97 (make-local-variable 'parse-sexp-ignore-comments)
98 (setq parse-sexp-ignore-comments t)
99 (make-local-variable 'outline-regexp)
100 (setq outline-regexp ";;; \\|(....")
101 (make-local-variable 'comment-start)
102 (setq comment-start ";")
103 (make-local-variable 'comment-start-skip)
104 (setq comment-start-skip ";+ *")
105 (make-local-variable 'comment-column)
106 (setq comment-column 40)
107 (make-local-variable 'comment-indent-function)
108 (setq comment-indent-function 'lisp-comment-indent))
110 (defvar shared-lisp-mode-map ()
111 "Keymap for commands shared by all sorts of Lisp modes.")
113 (if shared-lisp-mode-map
115 (setq shared-lisp-mode-map (make-sparse-keymap))
116 (define-key shared-lisp-mode-map "\e\C-q" 'indent-sexp)
117 (define-key shared-lisp-mode-map "\177" 'backward-delete-char-untabify)
118 (define-key shared-lisp-mode-map "\t" 'lisp-indent-line))
120 (defvar emacs-lisp-mode-map ()
121 "Keymap for Emacs Lisp mode.
122 All commands in shared-lisp-mode-map are inherited by this map.")
124 (if emacs-lisp-mode-map
126 (setq emacs-lisp-mode-map
127 (nconc (make-sparse-keymap) shared-lisp-mode-map))
128 (define-key emacs-lisp-mode-map "\e\t" 'lisp-complete-symbol)
129 (define-key emacs-lisp-mode-map "\e\C-x" 'eval-defun))
131 (defun emacs-lisp-mode ()
132 "Major mode for editing Lisp code to run in Emacs.
134 Delete converts tabs to spaces as it moves back.
135 Blank lines separate paragraphs. Semicolons start comments.
136 \\{emacs-lisp-mode-map}
137 Entry to this mode calls the value of `emacs-lisp-mode-hook'
138 if that value is non-nil."
140 (kill-all-local-variables)
141 (use-local-map emacs-lisp-mode-map)
142 (set-syntax-table emacs-lisp-mode-syntax-table)
143 (setq major-mode 'emacs-lisp-mode)
144 (setq mode-name "Emacs-Lisp")
145 (lisp-mode-variables nil)
146 (run-hooks 'emacs-lisp-mode-hook))
148 (defvar lisp-mode-map ()
149 "Keymap for ordinary Lisp mode.
150 All commands in `shared-lisp-mode-map' are inherited by this map.")
155 (nconc (make-sparse-keymap) shared-lisp-mode-map))
156 (define-key lisp-mode-map "\e\C-x" 'lisp-eval-defun)
157 (define-key lisp-mode-map "\C-c\C-z" 'run-lisp))
160 "Major mode for editing Lisp code for Lisps other than GNU Emacs Lisp.
162 Delete converts tabs to spaces as it moves back.
163 Blank lines separate paragraphs. Semicolons start comments.
165 Note that `run-lisp' may be used either to start an inferior Lisp job
166 or to switch back to an existing one.
168 Entry to this mode calls the value of `lisp-mode-hook'
169 if that value is non-nil."
171 (kill-all-local-variables)
172 (use-local-map lisp-mode-map)
173 (setq major-mode 'lisp-mode)
174 (setq mode-name "Lisp")
175 (lisp-mode-variables t)
176 (set-syntax-table lisp-mode-syntax-table)
177 (run-hooks 'lisp-mode-hook))
179 ;; This will do unless shell.el is loaded.
180 (defun lisp-eval-defun nil
181 "Send the current defun to the Lisp process made by \\[run-lisp]."
183 (error "Process lisp does not exist"))
185 (defvar lisp-interaction-mode-map ()
186 "Keymap for Lisp Interaction moe.
187 All commands in `shared-lisp-mode-map' are inherited by this map.")
189 (if lisp-interaction-mode-map
191 (setq lisp-interaction-mode-map
192 (nconc (make-sparse-keymap) shared-lisp-mode-map))
193 (define-key lisp-interaction-mode-map "\e\C-x" 'eval-defun)
194 (define-key lisp-interaction-mode-map "\e\t" 'lisp-complete-symbol)
195 (define-key lisp-interaction-mode-map "\n" 'eval-print-last-sexp))
197 (defun lisp-interaction-mode ()
198 "Major mode for typing and evaluating Lisp forms.
199 Like Lisp mode except that \\[eval-print-last-sexp] evals the Lisp expression
200 before point, and prints its value into the buffer, advancing point.
203 Delete converts tabs to spaces as it moves back.
204 Paragraphs are separated only by blank lines.
205 Semicolons start comments.
206 \\{lisp-interaction-mode-map}
207 Entry to this mode calls the value of `lisp-interaction-mode-hook'
208 if that value is non-nil."
210 (kill-all-local-variables)
211 (use-local-map lisp-interaction-mode-map)
212 (setq major-mode 'lisp-interaction-mode)
213 (setq mode-name "Lisp Interaction")
214 (set-syntax-table emacs-lisp-mode-syntax-table)
215 (lisp-mode-variables nil)
216 (run-hooks 'lisp-interaction-mode-hook))
218 (defun eval-print-last-sexp ()
219 "Evaluate sexp before point; print value into current buffer."
221 (let ((standard-output (current-buffer)))
226 (defun eval-last-sexp (eval-last-sexp-arg-internal)
227 "Evaluate sexp before point; print value in minibuffer.
228 With argument, print output into current buffer."
230 (let ((standard-output (if eval-last-sexp-arg-internal (current-buffer) t))
232 (prin1 (let ((stab (syntax-table)))
233 (eval (unwind-protect
235 (set-syntax-table emacs-lisp-mode-syntax-table)
238 (narrow-to-region (point-min) opoint)
239 (read (current-buffer))))
240 (set-syntax-table stab)))))))
242 (defun eval-defun (eval-defun-arg-internal)
243 "Evaluate defun that point is in or before.
244 Print value in minibuffer.
245 With argument, insert value in current buffer after the defun."
247 (let ((standard-output (if eval-defun-arg-internal (current-buffer) t)))
248 (prin1 (eval (save-excursion
251 (read (current-buffer)))))))
253 (defun lisp-comment-indent ()
254 (if (looking-at "\\s<\\s<\\s<")
256 (if (looking-at "\\s<\\s<")
257 (let ((tem (calculate-lisp-indent)))
258 (if (listp tem) (car tem) tem))
259 (skip-chars-backward " \t")
260 (max (if (bolp) 0 (1+ (current-column)))
263 (defconst lisp-indent-offset nil "")
264 (defconst lisp-indent-function 'lisp-indent-function "")
266 (defun lisp-indent-line (&optional whole-exp)
267 "Indent current line as Lisp code.
268 With argument, indent any additional lines of the same expression
269 rigidly along with this one."
271 (let ((indent (calculate-lisp-indent)) shift-amt beg end
272 (pos (- (point-max) (point))))
275 (skip-chars-forward " \t")
276 (if (looking-at "\\s<\\s<\\s<")
277 ;; Don't alter indentation of a ;;; comment line.
278 (goto-char (- (point-max) pos))
279 (if (and (looking-at "\\s<") (not (looking-at "\\s<\\s<")))
280 ;; Single-semicolon comment lines should be indented
281 ;; as comment lines, not as code.
282 (progn (indent-for-comment) (forward-char -1))
283 (if (listp indent) (setq indent (car indent)))
284 (setq shift-amt (- indent (current-column)))
285 (if (zerop shift-amt)
287 (delete-region beg (point))
289 ;; If initial point was within line's indentation,
290 ;; position after the indentation. Else stay at same point in text.
291 (if (> (- (point-max) pos) (point))
292 (goto-char (- (point-max) pos)))
293 ;; If desired, shift remaining lines of expression the same amount.
294 (and whole-exp (not (zerop shift-amt))
303 (indent-code-rigidly beg end shift-amt)))))
305 (defvar calculate-lisp-indent-last-sexp)
307 (defun calculate-lisp-indent (&optional parse-start)
308 "Return appropriate indentation for current line as Lisp code.
309 In usual case returns an integer: the column to indent to.
310 Can instead return a list, whose car is the column to indent to.
311 This means that following lines at the same level of indentation
312 should not necessarily be indented the same way.
313 The second element of the list is the buffer position
314 of the start of the containing expression."
317 (let ((indent-point (point))
319 ;; setting this to a number inhibits calling hook
322 calculate-lisp-indent-last-sexp containing-sexp)
324 (goto-char parse-start)
325 (beginning-of-defun))
326 ;; Find outermost containing sexp
327 (while (< (point) indent-point)
328 (setq state (parse-partial-sexp (point) indent-point 0)))
329 ;; Find innermost containing sexp
332 (> (setq paren-depth (elt state 0)) 0))
334 (setq calculate-lisp-indent-last-sexp (elt state 2))
335 (setq containing-sexp (elt state 1))
336 ;; Position following last unclosed open.
337 (goto-char (1+ containing-sexp))
338 ;; Is there a complete sexp since then?
339 (if (and calculate-lisp-indent-last-sexp
340 (> calculate-lisp-indent-last-sexp (point)))
341 ;; Yes, but is there a containing sexp after that?
342 (let ((peek (parse-partial-sexp calculate-lisp-indent-last-sexp
344 (if (setq retry (car (cdr peek))) (setq state peek)))))
347 ;; Innermost containing sexp found
348 (goto-char (1+ containing-sexp))
349 (if (not calculate-lisp-indent-last-sexp)
350 ;; indent-point immediately follows open paren.
352 (setq desired-indent (current-column))
353 ;; Find the start of first element of containing sexp.
354 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
355 (cond ((looking-at "\\s(")
356 ;; First element of containing sexp is a list.
357 ;; Indent under that list.
359 ((> (save-excursion (forward-line 1) (point))
360 calculate-lisp-indent-last-sexp)
361 ;; This is the first line to start within the containing sexp.
362 ;; It's almost certainly a function call.
363 (if (= (point) calculate-lisp-indent-last-sexp)
364 ;; Containing sexp has nothing before this line
365 ;; except the first element. Indent under that element.
367 ;; Skip the first element, find start of second (the first
368 ;; argument of the function call) and indent under.
369 (progn (forward-sexp 1)
370 (parse-partial-sexp (point)
371 calculate-lisp-indent-last-sexp
373 (backward-prefix-chars))
375 ;; Indent beneath first sexp on same line as
376 ;; calculate-lisp-indent-last-sexp. Again, it's
377 ;; almost certainly a function call.
378 (goto-char calculate-lisp-indent-last-sexp)
380 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp
382 (backward-prefix-chars)))))
383 ;; Point is at the point to indent under unless we are inside a string.
384 ;; Call indentation hook except when overridden by lisp-indent-offset
385 ;; or if the desired indentation has already been computed.
386 (let ((normal-indent (current-column)))
388 ;; Inside a string, don't change indentation.
389 (goto-char indent-point)
390 (skip-chars-forward " \t")
392 ((and (integerp lisp-indent-offset) containing-sexp)
393 ;; Indent by constant offset
394 (goto-char containing-sexp)
395 (+ (current-column) lisp-indent-offset))
397 ((and (boundp 'lisp-indent-function)
400 (or (funcall lisp-indent-function indent-point state)
405 (defun lisp-indent-function (indent-point state)
406 (let ((normal-indent (current-column)))
407 (goto-char (1+ (elt state 1)))
408 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
409 (if (and (elt state 2)
410 (not (looking-at "\\sw\\|\\s_")))
411 ;; car of form doesn't seem to be a a symbol
413 (if (not (> (save-excursion (forward-line 1) (point))
414 calculate-lisp-indent-last-sexp))
415 (progn (goto-char calculate-lisp-indent-last-sexp)
417 (parse-partial-sexp (point)
418 calculate-lisp-indent-last-sexp 0 t)))
419 ;; Indent under the list or under the first sexp on the same
420 ;; line as calculate-lisp-indent-last-sexp. Note that first
421 ;; thing on that line has to be complete sexp since we are
422 ;; inside the innermost containing sexp.
423 (backward-prefix-chars)
425 (let ((function (buffer-substring (point)
426 (progn (forward-sexp 1) (point))))
428 (setq method (or (get (intern-soft function) 'lisp-indent-function)
429 (get (intern-soft function) 'lisp-indent-hook)))
430 (cond ((or (eq method 'defun)
432 (> (length function) 3)
433 (string-match "\\`def" function)))
434 (lisp-indent-defform state indent-point))
436 (lisp-indent-specform method state
437 indent-point normal-indent))
439 (funcall method state indent-point)))))))
441 (defconst lisp-body-indent 2
442 "Number of columns to indent the second line of a `(def...)' form.")
444 (defun lisp-indent-specform (count state indent-point normal-indent)
445 (let ((containing-form-start (elt state 1))
447 body-indent containing-form-column)
448 ;; Move to the start of containing form, calculate indentation
449 ;; to use for non-distinguished forms (> count), and move past the
450 ;; function symbol. lisp-indent-function guarantees that there is at
451 ;; least one word or symbol character following open paren of containing
453 (goto-char containing-form-start)
454 (setq containing-form-column (current-column))
455 (setq body-indent (+ lisp-body-indent containing-form-column))
458 ;; Now find the start of the last form.
459 (parse-partial-sexp (point) indent-point 1 t)
460 (while (and (< (point) indent-point)
463 (setq count (1- count))
465 (parse-partial-sexp (point) indent-point 1 t))
467 ;; Point is sitting on first character of last (or count) sexp.
469 ;; A distinguished form. If it is the first or second form use double
470 ;; lisp-body-indent, else normal indent. With lisp-body-indent bound
471 ;; to 2 (the default), this just happens to work the same with if as
472 ;; the older code, but it makes unwind-protect, condition-case,
473 ;; with-output-to-temp-buffer, et. al. much more tasteful. The older,
474 ;; less hacked, behavior can be obtained by replacing below with
475 ;; (list normal-indent containing-form-start).
476 (if (<= (- i count) 1)
477 (list (+ containing-form-column (* 2 lisp-body-indent))
478 containing-form-start)
479 (list normal-indent containing-form-start))
480 ;; A non-distinguished form. Use body-indent if there are no
481 ;; distinguished forms and this is the first undistinguished form,
482 ;; or if this is the first undistinguished form and the preceding
483 ;; distinguished form has indentation at least as great as body-indent.
484 (if (or (and (= i 0) (= count 0))
485 (and (= count 0) (<= body-indent normal-indent)))
489 (defun lisp-indent-defform (state indent-point)
490 (goto-char (car (cdr state)))
492 (if (> (point) (car (cdr (cdr state))))
494 (goto-char (car (cdr state)))
495 (+ lisp-body-indent (current-column)))))
498 ;; (put 'progn 'lisp-indent-function 0), say, causes progn to be indented
499 ;; like defun if the first form is placed on the next line, otherwise
500 ;; it is indented like any other form (i.e. forms line up under first).
502 (put 'lambda 'lisp-indent-function 'defun)
503 (put 'autoload 'lisp-indent-function 'defun)
504 (put 'progn 'lisp-indent-function 0)
505 (put 'prog1 'lisp-indent-function 1)
506 (put 'prog2 'lisp-indent-function 2)
507 (put 'save-excursion 'lisp-indent-function 0)
508 (put 'save-window-excursion 'lisp-indent-function 0)
509 (put 'save-restriction 'lisp-indent-function 0)
510 (put 'save-match-data 'lisp-indent-function 0)
511 (put 'let 'lisp-indent-function 1)
512 (put 'let* 'lisp-indent-function 1)
513 (put 'while 'lisp-indent-function 1)
514 (put 'if 'lisp-indent-function 2)
515 (put 'catch 'lisp-indent-function 1)
516 (put 'condition-case 'lisp-indent-function 2)
517 (put 'unwind-protect 'lisp-indent-function 1)
518 (put 'with-output-to-temp-buffer 'lisp-indent-function 1)
520 (defun indent-sexp (&optional endpos)
521 "Indent each line of the list starting just after point.
522 If optional arg ENDPOS is given, indent each line, stopping when
523 ENDPOS is encountered."
525 (let ((indent-stack (list nil))
527 ;; If ENDPOS is non-nil, use nil as STARTING-POINT
528 ;; so that calculate-lisp-indent will find the beginning of
529 ;; the defun we are in.
530 ;; If ENDPOS is nil, it is safe not to scan before point
531 ;; since every line we indent is more deeply nested than point is.
532 (starting-point (if endpos nil (point)))
534 last-depth bol outer-loop-done inner-loop-done state this-indent)
536 ;; Get error now if we don't have a complete sexp after point.
537 (save-excursion (forward-sexp 1)))
539 (setq outer-loop-done nil)
540 (while (if endpos (< (point) endpos)
541 (not outer-loop-done))
542 (setq last-depth next-depth
544 ;; Parse this line so we can learn the state
545 ;; to indent the next line.
546 ;; This inner loop goes through only once
547 ;; unless a line ends inside a string.
548 (while (and (not inner-loop-done)
549 (not (setq outer-loop-done (eobp))))
550 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
552 (setq next-depth (car state))
553 ;; If the line contains a comment other than the sort
554 ;; that is indented like code,
555 ;; indent it now with indent-for-comment.
556 ;; Comments indented like code are right already.
557 ;; In any case clear the in-comment flag in the state
558 ;; because parse-partial-sexp never sees the newlines.
559 (if (car (nthcdr 4 state))
560 (progn (indent-for-comment)
562 (setcar (nthcdr 4 state) nil)))
563 ;; If this line ends inside a string,
564 ;; go straight to next line, remaining within the inner loop,
565 ;; and turn off the \-flag.
566 (if (car (nthcdr 3 state))
569 (setcar (nthcdr 5 state) nil))
570 (setq inner-loop-done t)))
574 (setq indent-stack (append indent-stack
575 (make-list (- next-depth) nil))
576 last-depth (- last-depth next-depth)
578 (or outer-loop-done endpos
579 (setq outer-loop-done (<= next-depth 0)))
582 (while (> last-depth next-depth)
583 (setq indent-stack (cdr indent-stack)
584 last-depth (1- last-depth)))
585 (while (< last-depth next-depth)
586 (setq indent-stack (cons nil indent-stack)
587 last-depth (1+ last-depth)))
588 ;; Now go to the next line and indent it according
589 ;; to what we learned from parsing the previous one.
592 (skip-chars-forward " \t")
593 ;; But not if the line is blank, or just a comment
594 ;; (except for double-semi comments; indent them as usual).
595 (if (or (eobp) (looking-at "\\s<\\|\n"))
597 (if (and (car indent-stack)
598 (>= (car indent-stack) 0))
599 (setq this-indent (car indent-stack))
600 (let ((val (calculate-lisp-indent
601 (if (car indent-stack) (- (car indent-stack))
605 (setq this-indent val))
606 (setcar indent-stack (- (car (cdr val))))
607 (setq this-indent (car val)))))
608 (if (/= (current-column) this-indent)
609 (progn (delete-region bol (point))
610 (indent-to this-indent)))))
612 (setq outer-loop-done (= (point) last-point))
613 (setq last-point (point)))))))
615 ;; Indent every line whose first char is between START and END inclusive.
616 (defun lisp-indent-region (start end)
618 (let ((endmark (copy-marker end)))
620 (and (bolp) (not (eolp))
622 (indent-sexp endmark)
623 (set-marker endmark nil))))
625 ;;;; Lisp paragraph filling commands.
627 (defun lisp-fill-paragraph (&optional justify)
628 "Like \\[fill-paragraph], but handle Emacs Lisp comments.
629 If any of the current line is a comment, fill the comment or the
630 paragraph of it that point is in, preserving the comment's indentation
631 and initial semicolons."
634 ;; Non-nil if the current line contains a comment.
637 ;; If has-comment, the appropriate fill-prefix for the comment.
641 ;; Figure out what kind of comment we are looking at.
646 ;; A line with nothing but a comment on it?
647 ((looking-at "[ \t]*;[; \t]*")
649 comment-fill-prefix (buffer-substring (match-beginning 0)
652 ;; A line with some code, followed by a comment? Remember that the
653 ;; semi which starts the comment shouldn't be part of a string or
656 (while (not (looking-at ";\\|$"))
657 (skip-chars-forward "^;\n\"\\\\?")
659 ((eq (char-after (point)) ?\\) (forward-char 2))
660 ((memq (char-after (point)) '(?\" ??)) (forward-sexp 1))))
661 (looking-at ";+[\t ]*"))
663 (setq comment-fill-prefix
664 (concat (make-string (current-column) ? )
665 (buffer-substring (match-beginning 0) (match-end 0)))))))
667 (if (not has-comment)
668 (fill-paragraph justify)
670 ;; Narrow to include only the comment, and then fill the region.
673 ;; Find the first line we should include in the region to fill.
675 (while (and (zerop (forward-line -1))
676 (looking-at "^[ \t]*;")))
677 ;; We may have gone to far. Go forward again.
678 (or (looking-at "^[ \t]*;")
681 ;; Find the beginning of the first line past the region to fill.
683 (while (progn (forward-line 1)
684 (looking-at "^[ \t]*;")))
687 ;; Lines with only semicolons on them can be paragraph boundaries.
688 (let ((paragraph-start (concat paragraph-start "\\|^[ \t;]*$"))
689 (paragraph-separate (concat paragraph-start "\\|^[ \t;]*$"))
690 (fill-prefix comment-fill-prefix))
691 (fill-paragraph justify))))
695 (defun indent-code-rigidly (start end arg &optional nochange-regexp)
696 "Indent all lines of code, starting in the region, sideways by ARG columns.
697 Does not affect lines starting inside comments or strings, assuming that
698 the start of the region is not inside them.
700 Called from a program, takes args START, END, COLUMNS and NOCHANGE-REGEXP.
701 The last is a regexp which, if matched at the beginning of a line,
702 means don't indent that line."
707 (setq end (point-marker))
710 (setq state (parse-partial-sexp (point)
712 (forward-line 1) (point))
714 (while (< (point) end)
715 (or (car (nthcdr 3 state))
717 (looking-at nochange-regexp))
718 ;; If line does not start in string, indent it
719 (let ((indent (current-indentation)))
720 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
722 (indent-to (max 0 (+ indent arg)) 0))))
723 (setq state (parse-partial-sexp (point)
725 (forward-line 1) (point))
730 ;;; lisp-mode.el ends here