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\C-x" 'eval-defun
))
118 (defun emacs-lisp-mode ()
119 "Major mode for editing Lisp code to run in Emacs.
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."
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.")
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
))
147 "Major mode for editing Lisp code for Lisps other than GNU Emacs Lisp.
149 Delete converts tabs to spaces as it moves back.
150 Blank lines separate paragraphs. Semicolons start comments.
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."
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]."
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.
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."
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."
207 (let ((standard-output (current-buffer)))
212 (defun eval-last-sexp (arg)
213 "Evaluate sexp before point; print value in minibuffer.
214 With argument, print output into current buffer."
216 (let ((standard-output (if arg
(current-buffer) t
)))
217 (prin1 (let ((stab (syntax-table)))
218 (eval (unwind-protect
220 (set-syntax-table emacs-lisp-mode-syntax-table
)
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."
230 (let ((standard-output (if arg
(current-buffer) t
)))
231 (prin1 (eval (save-excursion
234 (read (current-buffer)))))))
236 (defun lisp-comment-indent ()
237 (if (looking-at "\\s<\\s<\\s<")
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)))
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."
254 (let ((indent (calculate-lisp-indent)) shift-amt beg end
255 (pos (- (point-max) (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))
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
))
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."
298 (let ((indent-point (point))
300 ;; setting this to a number inhibits calling hook
303 last-sexp containing-sexp
)
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
313 (> (setq paren-depth
(elt state
0)) 0))
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
)))))
326 ;; Innermost containing sexp found
327 (goto-char (1+ containing-sexp
))
329 ;; indent-point immediately follows open paren.
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))
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
)
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)))
363 ;; Inside a string, don't change indentation.
364 (goto-char indent-point
)
365 (skip-chars-forward " \t")
367 ((and (integerp lisp-indent-offset
) containing-sexp
)
368 ;; Indent by constant offset
369 (goto-char containing-sexp
)
370 (+ (current-column) lisp-indent-offset
))
372 ((and (boundp 'lisp-indent-function
)
375 (or (funcall lisp-indent-function indent-point state
)
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
388 (if (not (> (save-excursion (forward-line 1) (point))
390 (progn (goto-char last-sexp
)
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)
399 (let ((function (buffer-substring (point)
400 (progn (forward-sexp 1) (point))))
402 (setq method
(get (intern-soft function
) 'lisp-indent-function
))
403 (cond ((or (eq method
'defun
)
405 (> (length function
) 3)
406 (string-match "\\`def" function
)))
407 (lisp-indent-defform state indent-point
))
409 (lisp-indent-specform method state
410 indent-point normal-indent
))
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))
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
425 (goto-char containing-form-start
)
426 (setq containing-form-column
(current-column))
427 (setq body-indent
(+ lisp-body-indent containing-form-column
))
430 ;; Now find the start of the last form.
431 (parse-partial-sexp (point) indent-point
1 t
)
432 (while (and (< (point) indent-point
)
435 (setq count
(1- count
))
437 (parse-partial-sexp (point) indent-point
1 t
))
439 ;; Point is sitting on first character of last (or count) sexp.
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
)))
461 (defun lisp-indent-defform (state indent-point
)
462 (goto-char (car (cdr state
)))
464 (if (> (point) (car (cdr (cdr state
))))
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."
496 (let ((indent-stack (list nil
))
498 (starting-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))
504 (setq outer-loop-done nil
)
505 (while (if endpos
(< (point) endpos
)
506 (not outer-loop-done
))
507 (setq last-depth next-depth
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))
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)
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
))
534 (setcar (nthcdr 5 state
) nil
))
535 (setq inner-loop-done t
)))
539 (setq indent-stack
(append indent-stack
540 (make-list (- next-depth
) nil
))
541 last-depth
(- last-depth next-depth
)
544 (setq outer-loop-done
(<= next-depth
0)))
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.
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
))
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
)))))
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
)
584 (and (bolp) (not (eolp))
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."
602 (setq end
(point-marker))
605 (setq state
(parse-partial-sexp (point)
607 (forward-line 1) (point))
609 (while (< (point) end
)
610 (or (car (nthcdr 3 state
))
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)))
617 (indent-to (max 0 (+ indent arg
)) 0))))
618 (setq state
(parse-partial-sexp (point)
620 (forward-line 1) (point))
625 ;;; lisp-mode.el ends here