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.
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)
30 (setq emacs-lisp-mode-syntax-table (make-syntax-table))
32 (modify-syntax-entry i "_ " emacs-lisp-mode-syntax-table)
36 (modify-syntax-entry i "_ " emacs-lisp-mode-syntax-table)
40 (modify-syntax-entry i "_ " emacs-lisp-mode-syntax-table)
44 (modify-syntax-entry i "_ " emacs-lisp-mode-syntax-table)
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)
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\t" 'lisp-complete-symbol)
117 (define-key emacs-lisp-mode-map "\e\C-x" 'eval-defun))
119 (defun emacs-lisp-mode ()
120 "Major mode for editing Lisp code to run in Emacs.
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."
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))
136 (defvar lisp-mode-map ()
137 "Keymap for ordinary Lisp mode.
138 All commands in `shared-lisp-mode-map' are inherited by this 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))
148 "Major mode for editing Lisp code for Lisps other than GNU Emacs Lisp.
150 Delete converts tabs to spaces as it moves back.
151 Blank lines separate paragraphs. Semicolons start comments.
153 Note that `run-lisp' may be used either to start an inferior Lisp job
154 or to switch back to an existing one.
156 Entry to this mode calls the value of `lisp-mode-hook'
157 if that value is non-nil."
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))
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]."
171 (error "Process lisp does not exist"))
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.")
177 (if lisp-interaction-mode-map
179 (setq lisp-interaction-mode-map
180 (nconc (make-sparse-keymap) shared-lisp-mode-map))
181 (define-key lisp-interaction-mode-map "\e\C-x" 'eval-defun)
182 (define-key lisp-interaction-mode-map "\e\t" 'lisp-complete-symbol)
183 (define-key lisp-interaction-mode-map "\n" 'eval-print-last-sexp))
185 (defun lisp-interaction-mode ()
186 "Major mode for typing and evaluating Lisp forms.
187 Like Lisp mode except that \\[eval-print-last-sexp] evals the Lisp expression
188 before point, and prints its value into the buffer, advancing point.
191 Delete converts tabs to spaces as it moves back.
192 Paragraphs are separated only by blank lines.
193 Semicolons start comments.
194 \\{lisp-interaction-mode-map}
195 Entry to this mode calls the value of `lisp-interaction-mode-hook'
196 if that value is non-nil."
198 (kill-all-local-variables)
199 (use-local-map lisp-interaction-mode-map)
200 (setq major-mode 'lisp-interaction-mode)
201 (setq mode-name "Lisp Interaction")
202 (set-syntax-table emacs-lisp-mode-syntax-table)
203 (lisp-mode-variables nil)
204 (run-hooks 'lisp-interaction-mode-hook))
206 (defun eval-print-last-sexp ()
207 "Evaluate sexp before point; print value into current buffer."
209 (let ((standard-output (current-buffer)))
214 (defun eval-last-sexp (arg)
215 "Evaluate sexp before point; print value in minibuffer.
216 With argument, print output into current buffer."
218 (let ((standard-output (if arg (current-buffer) t))
220 (prin1 (let ((stab (syntax-table)))
221 (eval (unwind-protect
223 (set-syntax-table emacs-lisp-mode-syntax-table)
226 (narrow-to-region (point-min) opoint)
227 (read (current-buffer))))
228 (set-syntax-table stab)))))))
230 (defun eval-defun (arg)
231 "Evaluate defun that point is in or before.
232 Print value in minibuffer.
233 With argument, insert value in current buffer after the defun."
235 (let ((standard-output (if arg (current-buffer) t)))
236 (prin1 (eval (save-excursion
239 (read (current-buffer)))))))
241 (defun lisp-comment-indent ()
242 (if (looking-at "\\s<\\s<\\s<")
244 (if (looking-at "\\s<\\s<")
245 (let ((tem (calculate-lisp-indent)))
246 (if (listp tem) (car tem) tem))
247 (skip-chars-backward " \t")
248 (max (if (bolp) 0 (1+ (current-column)))
251 (defconst lisp-indent-offset nil "")
252 (defconst lisp-indent-function 'lisp-indent-function "")
254 (defun lisp-indent-line (&optional whole-exp)
255 "Indent current line as Lisp code.
256 With argument, indent any additional lines of the same expression
257 rigidly along with this one."
259 (let ((indent (calculate-lisp-indent)) shift-amt beg end
260 (pos (- (point-max) (point))))
263 (skip-chars-forward " \t")
264 (if (looking-at "\\s<\\s<\\s<")
265 ;; Don't alter indentation of a ;;; comment line.
266 (goto-char (- (point-max) pos))
267 (if (and (looking-at "\\s<") (not (looking-at "\\s<\\s<")))
268 ;; Single-semicolon comment lines should be indented
269 ;; as comment lines, not as code.
270 (progn (indent-for-comment) (forward-char -1))
271 (if (listp indent) (setq indent (car indent)))
272 (setq shift-amt (- indent (current-column)))
273 (if (zerop shift-amt)
275 (delete-region beg (point))
277 ;; If initial point was within line's indentation,
278 ;; position after the indentation. Else stay at same point in text.
279 (if (> (- (point-max) pos) (point))
280 (goto-char (- (point-max) pos)))
281 ;; If desired, shift remaining lines of expression the same amount.
282 (and whole-exp (not (zerop shift-amt))
291 (indent-code-rigidly beg end shift-amt)))))
293 (defun calculate-lisp-indent (&optional parse-start)
294 "Return appropriate indentation for current line as Lisp code.
295 In usual case returns an integer: the column to indent to.
296 Can instead return a list, whose car is the column to indent to.
297 This means that following lines at the same level of indentation
298 should not necessarily be indented the same way.
299 The second element of the list is the buffer position
300 of the start of the containing expression."
303 (let ((indent-point (point))
305 ;; setting this to a number inhibits calling hook
308 last-sexp containing-sexp)
310 (goto-char parse-start)
311 (beginning-of-defun))
312 ;; Find outermost containing sexp
313 (while (< (point) indent-point)
314 (setq state (parse-partial-sexp (point) indent-point 0)))
315 ;; Find innermost containing sexp
318 (> (setq paren-depth (elt state 0)) 0))
320 (setq last-sexp (elt state 2))
321 (setq containing-sexp (elt state 1))
322 ;; Position following last unclosed open.
323 (goto-char (1+ containing-sexp))
324 ;; Is there a complete sexp since then?
325 (if (and last-sexp (> last-sexp (point)))
326 ;; Yes, but is there a containing sexp after that?
327 (let ((peek (parse-partial-sexp last-sexp indent-point 0)))
328 (if (setq retry (car (cdr peek))) (setq state peek)))))
331 ;; Innermost containing sexp found
332 (goto-char (1+ containing-sexp))
334 ;; indent-point immediately follows open paren.
336 (setq desired-indent (current-column))
337 ;; Find the start of first element of containing sexp.
338 (parse-partial-sexp (point) last-sexp 0 t)
339 (cond ((looking-at "\\s(")
340 ;; First element of containing sexp is a list.
341 ;; Indent under that list.
343 ((> (save-excursion (forward-line 1) (point))
345 ;; This is the first line to start within the containing sexp.
346 ;; It's almost certainly a function call.
347 (if (= (point) last-sexp)
348 ;; Containing sexp has nothing before this line
349 ;; except the first element. Indent under that element.
351 ;; Skip the first element, find start of second (the first
352 ;; argument of the function call) and indent under.
353 (progn (forward-sexp 1)
354 (parse-partial-sexp (point) last-sexp 0 t)))
355 (backward-prefix-chars))
357 ;; Indent beneath first sexp on same line as last-sexp.
358 ;; Again, it's almost certainly a function call.
359 (goto-char last-sexp)
361 (parse-partial-sexp (point) last-sexp 0 t)
362 (backward-prefix-chars)))))
363 ;; Point is at the point to indent under unless we are inside a string.
364 ;; Call indentation hook except when overriden by lisp-indent-offset
365 ;; or if the desired indentation has already been computed.
366 (let ((normal-indent (current-column)))
368 ;; Inside a string, don't change indentation.
369 (goto-char indent-point)
370 (skip-chars-forward " \t")
372 ((and (integerp lisp-indent-offset) containing-sexp)
373 ;; Indent by constant offset
374 (goto-char containing-sexp)
375 (+ (current-column) lisp-indent-offset))
377 ((and (boundp 'lisp-indent-function)
380 (or (funcall lisp-indent-function indent-point state)
385 (defun lisp-indent-function (indent-point state)
386 (let ((normal-indent (current-column)))
387 (goto-char (1+ (elt state 1)))
388 (parse-partial-sexp (point) last-sexp 0 t)
389 (if (and (elt state 2)
390 (not (looking-at "\\sw\\|\\s_")))
391 ;; car of form doesn't seem to be a a symbol
393 (if (not (> (save-excursion (forward-line 1) (point))
395 (progn (goto-char last-sexp)
397 (parse-partial-sexp (point) last-sexp 0 t)))
398 ;; Indent under the list or under the first sexp on the
399 ;; same line as last-sexp. Note that first thing on that
400 ;; line has to be complete sexp since we are inside the
401 ;; innermost containing sexp.
402 (backward-prefix-chars)
404 (let ((function (buffer-substring (point)
405 (progn (forward-sexp 1) (point))))
407 (setq method (get (intern-soft function) 'lisp-indent-function))
408 (cond ((or (eq method 'defun)
410 (> (length function) 3)
411 (string-match "\\`def" function)))
412 (lisp-indent-defform state indent-point))
414 (lisp-indent-specform method state
415 indent-point normal-indent))
417 (funcall method state indent-point)))))))
419 (defconst lisp-body-indent 2 "")
421 (defun lisp-indent-specform (count state indent-point normal-indent)
422 (let ((containing-form-start (elt state 1))
424 body-indent containing-form-column)
425 ;; Move to the start of containing form, calculate indentation
426 ;; to use for non-distinguished forms (> count), and move past the
427 ;; function symbol. lisp-indent-function guarantees that there is at
428 ;; least one word or symbol character following open paren of containing
430 (goto-char containing-form-start)
431 (setq containing-form-column (current-column))
432 (setq body-indent (+ lisp-body-indent containing-form-column))
435 ;; Now find the start of the last form.
436 (parse-partial-sexp (point) indent-point 1 t)
437 (while (and (< (point) indent-point)
440 (setq count (1- count))
442 (parse-partial-sexp (point) indent-point 1 t))
444 ;; Point is sitting on first character of last (or count) sexp.
446 ;; A distinguished form. If it is the first or second form use double
447 ;; lisp-body-indent, else normal indent. With lisp-body-indent bound
448 ;; to 2 (the default), this just happens to work the same with if as
449 ;; the older code, but it makes unwind-protect, condition-case,
450 ;; with-output-to-temp-buffer, et. al. much more tasteful. The older,
451 ;; less hacked, behavior can be obtained by replacing below with
452 ;; (list normal-indent containing-form-start).
453 (if (<= (- i count) 1)
454 (list (+ containing-form-column (* 2 lisp-body-indent))
455 containing-form-start)
456 (list normal-indent containing-form-start))
457 ;; A non-distinguished form. Use body-indent if there are no
458 ;; distinguished forms and this is the first undistinguished form,
459 ;; or if this is the first undistinguished form and the preceding
460 ;; distinguished form has indentation at least as great as body-indent.
461 (if (or (and (= i 0) (= count 0))
462 (and (= count 0) (<= body-indent normal-indent)))
466 (defun lisp-indent-defform (state indent-point)
467 (goto-char (car (cdr state)))
469 (if (> (point) (car (cdr (cdr state))))
471 (goto-char (car (cdr state)))
472 (+ lisp-body-indent (current-column)))))
475 ;; (put 'progn 'lisp-indent-function 0), say, causes progn to be indented
476 ;; like defun if the first form is placed on the next line, otherwise
477 ;; it is indented like any other form (i.e. forms line up under first).
479 (put 'lambda 'lisp-indent-function 'defun)
480 (put 'autoload 'lisp-indent-function 'defun)
481 (put 'progn 'lisp-indent-function 0)
482 (put 'prog1 'lisp-indent-function 1)
483 (put 'prog2 'lisp-indent-function 2)
484 (put 'save-excursion 'lisp-indent-function 0)
485 (put 'save-window-excursion 'lisp-indent-function 0)
486 (put 'save-restriction 'lisp-indent-function 0)
487 (put 'save-match-data 'lisp-indent-function 0)
488 (put 'let 'lisp-indent-function 1)
489 (put 'let* 'lisp-indent-function 1)
490 (put 'while 'lisp-indent-function 1)
491 (put 'if 'lisp-indent-function 2)
492 (put 'catch 'lisp-indent-function 1)
493 (put 'condition-case 'lisp-indent-function 2)
494 (put 'unwind-protect 'lisp-indent-function 1)
495 (put 'with-output-to-temp-buffer 'lisp-indent-function 1)
497 (defun indent-sexp (&optional endpos)
498 "Indent each line of the list starting just after point.
499 If optional arg ENDPOS is given, indent each line, stopping when
500 ENDPOS is encountered."
502 (let ((indent-stack (list nil))
504 (starting-point (point))
506 last-depth bol outer-loop-done inner-loop-done state this-indent)
507 ;; Get error now if we don't have a complete sexp after point.
508 (save-excursion (forward-sexp 1))
510 (setq outer-loop-done nil)
511 (while (if endpos (< (point) endpos)
512 (not outer-loop-done))
513 (setq last-depth next-depth
515 ;; Parse this line so we can learn the state
516 ;; to indent the next line.
517 ;; This inner loop goes through only once
518 ;; unless a line ends inside a string.
519 (while (and (not inner-loop-done)
520 (not (setq outer-loop-done (eobp))))
521 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
523 (setq next-depth (car state))
524 ;; If the line contains a comment other than the sort
525 ;; that is indented like code,
526 ;; indent it now with indent-for-comment.
527 ;; Comments indented like code are right already.
528 ;; In any case clear the in-comment flag in the state
529 ;; because parse-partial-sexp never sees the newlines.
530 (if (car (nthcdr 4 state))
531 (progn (indent-for-comment)
533 (setcar (nthcdr 4 state) nil)))
534 ;; If this line ends inside a string,
535 ;; go straight to next line, remaining within the inner loop,
536 ;; and turn off the \-flag.
537 (if (car (nthcdr 3 state))
540 (setcar (nthcdr 5 state) nil))
541 (setq inner-loop-done t)))
545 (setq indent-stack (append indent-stack
546 (make-list (- next-depth) nil))
547 last-depth (- last-depth next-depth)
550 (setq outer-loop-done (<= next-depth 0)))
553 (while (> last-depth next-depth)
554 (setq indent-stack (cdr indent-stack)
555 last-depth (1- last-depth)))
556 (while (< last-depth next-depth)
557 (setq indent-stack (cons nil indent-stack)
558 last-depth (1+ last-depth)))
559 ;; Now go to the next line and indent it according
560 ;; to what we learned from parsing the previous one.
563 (skip-chars-forward " \t")
564 ;; But not if the line is blank, or just a comment
565 ;; (except for double-semi comments; indent them as usual).
566 (if (or (eobp) (looking-at "\\s<\\|\n"))
568 (if (and (car indent-stack)
569 (>= (car indent-stack) 0))
570 (setq this-indent (car indent-stack))
571 (let ((val (calculate-lisp-indent
572 (if (car indent-stack) (- (car indent-stack))
576 (setq this-indent val))
577 (setcar indent-stack (- (car (cdr val))))
578 (setq this-indent (car val)))))
579 (if (/= (current-column) this-indent)
580 (progn (delete-region bol (point))
581 (indent-to this-indent)))))
583 (setq outer-loop-done (= (point) last-point))
584 (setq last-point (point)))))))
586 ;; Indent every line whose first char is between START and END inclusive.
587 (defun lisp-indent-region (start end)
590 (and (bolp) (not (eolp))
592 (let ((endmark (copy-marker end)))
593 (indent-sexp endmark)
594 (set-marker endmark nil))))
596 (defun indent-code-rigidly (start end arg &optional nochange-regexp)
597 "Indent all lines of code, starting in the region, sideways by ARG columns.
598 Does not affect lines starting inside comments or strings, assuming that
599 the start of the region is not inside them.
601 Called from a program, takes args START, END, COLUMNS and NOCHANGE-REGEXP.
602 The last is a regexp which, if matched at the beginning of a line,
603 means don't indent that line."
608 (setq end (point-marker))
611 (setq state (parse-partial-sexp (point)
613 (forward-line 1) (point))
615 (while (< (point) end)
616 (or (car (nthcdr 3 state))
618 (looking-at nochange-regexp))
619 ;; If line does not start in string, indent it
620 (let ((indent (current-indentation)))
621 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
623 (indent-to (max 0 (+ indent arg)) 0))))
624 (setq state (parse-partial-sexp (point)
626 (forward-line 1) (point))
631 ;;; lisp-mode.el ends here