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