Set syntaxes all non-word multibyte
[bpt/emacs.git] / lisp / emacs-lisp / lisp-mode.el
CommitLineData
6594deb0
ER
1;;; lisp-mode.el --- Lisp mode, and its idiosyncratic commands.
2
9596811a 3;; Copyright (C) 1985, 1986 Free Software Foundation, Inc.
3a801d0c 4
e5167999 5;; Maintainer: FSF
fd7fa35a 6;; Keywords: lisp, languages
e5167999 7
a90256cc
BP
8;; This file is part of GNU Emacs.
9
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
e5167999 12;; the Free Software Foundation; either version 2, or (at your option)
a90256cc
BP
13;; any later version.
14
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.
19
20;; You should have received a copy of the GNU General Public License
b578f267
EN
21;; along with GNU Emacs; see the file COPYING. If not, write to the
22;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23;; Boston, MA 02111-1307, USA.
a90256cc 24
e41b2db1
ER
25;;; Commentary:
26
27;; The base major mode for editing Lisp code (used also for Emacs Lisp).
28;; This mode is documented in the Emacs manual
29
30;;; Code:
31
a90256cc
BP
32(defvar lisp-mode-syntax-table nil "")
33(defvar emacs-lisp-mode-syntax-table nil "")
34(defvar lisp-mode-abbrev-table nil "")
35
36(if (not emacs-lisp-mode-syntax-table)
37 (let ((i 0))
38 (setq emacs-lisp-mode-syntax-table (make-syntax-table))
39 (while (< i ?0)
40 (modify-syntax-entry i "_ " emacs-lisp-mode-syntax-table)
41 (setq i (1+ i)))
42 (setq i (1+ ?9))
43 (while (< i ?A)
44 (modify-syntax-entry i "_ " emacs-lisp-mode-syntax-table)
45 (setq i (1+ i)))
46 (setq i (1+ ?Z))
47 (while (< i ?a)
48 (modify-syntax-entry i "_ " emacs-lisp-mode-syntax-table)
49 (setq i (1+ i)))
50 (setq i (1+ ?z))
51 (while (< i 128)
52 (modify-syntax-entry i "_ " emacs-lisp-mode-syntax-table)
53 (setq i (1+ i)))
54 (modify-syntax-entry ? " " emacs-lisp-mode-syntax-table)
55 (modify-syntax-entry ?\t " " emacs-lisp-mode-syntax-table)
37922e6c 56 (modify-syntax-entry ?\f " " emacs-lisp-mode-syntax-table)
a90256cc 57 (modify-syntax-entry ?\n "> " emacs-lisp-mode-syntax-table)
910762b4
RS
58 ;; Give CR the same syntax as newline, for selective-display.
59 (modify-syntax-entry ?\^m "> " emacs-lisp-mode-syntax-table)
a90256cc
BP
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)
64 ;; Used to be singlequote; changed for flonums.
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)
71 (modify-syntax-entry ?\[ "(] " emacs-lisp-mode-syntax-table)
bbbc0af0
KH
72 (modify-syntax-entry ?\] ")[ " emacs-lisp-mode-syntax-table)
73 ;; All non-word multibyte characters should be `symbol'.
74 (map-char-table
75 (function (lambda (key val)
76 (and (>= key 256)
77 (/= (char-syntax key) ?w)
78 (modify-syntax-entry key "_ "
79 emacs-lisp-mode-syntax-table))))
80 (standard-syntax-table))))
a90256cc 81
ecca85de
JB
82(if (not lisp-mode-syntax-table)
83 (progn (setq lisp-mode-syntax-table
84 (copy-syntax-table emacs-lisp-mode-syntax-table))
85 (modify-syntax-entry ?\| "\" " lisp-mode-syntax-table)
86 (modify-syntax-entry ?\[ "_ " lisp-mode-syntax-table)
87 (modify-syntax-entry ?\] "_ " lisp-mode-syntax-table)))
88
a90256cc
BP
89(define-abbrev-table 'lisp-mode-abbrev-table ())
90
6c2cf866
KH
91(defvar lisp-imenu-generic-expression
92 '(
93 (nil
00a90f42 94 "^\\s-*(def\\(un\\|subst\\|macro\\|advice\\|ine-skeleton\\)\\s-+\\([-A-Za-z0-9+*|:/]+\\)" 2)
6c2cf866 95 ("Variables"
cafba899 96 "^\\s-*(def\\(var\\|const\\|custom\\)\\s-+\\([-A-Za-z0-9+*|:/]+\\)" 2)
6c2cf866 97 ("Types"
cafba899 98 "^\\s-*(def\\(group\\|type\\|struct\\|class\\|ine-condition\\)\\s-+\\([-A-Za-z0-9+*|:/]+\\)"
6c2cf866
KH
99 2))
100
101 "Imenu generic expression for Lisp mode. See `imenu-generic-expression'.")
102
a90256cc
BP
103(defun lisp-mode-variables (lisp-syntax)
104 (cond (lisp-syntax
a90256cc
BP
105 (set-syntax-table lisp-mode-syntax-table)))
106 (setq local-abbrev-table lisp-mode-abbrev-table)
107 (make-local-variable 'paragraph-start)
763d1579 108 (setq paragraph-start (concat page-delimiter "\\|$" ))
a90256cc
BP
109 (make-local-variable 'paragraph-separate)
110 (setq paragraph-separate paragraph-start)
111 (make-local-variable 'paragraph-ignore-fill-prefix)
112 (setq paragraph-ignore-fill-prefix t)
35d132a8
RS
113 (make-local-variable 'fill-paragraph-function)
114 (setq fill-paragraph-function 'lisp-fill-paragraph)
3272c162
RS
115 ;; Adaptive fill mode gets in the way of auto-fill,
116 ;; and should make no difference for explicit fill
117 ;; because lisp-fill-paragraph should do the job.
118 (make-local-variable 'adaptive-fill-mode)
119 (setq adaptive-fill-mode nil)
7eb67d79
RS
120 (make-local-variable 'normal-auto-fill-function)
121 (setq normal-auto-fill-function 'lisp-mode-auto-fill)
a90256cc
BP
122 (make-local-variable 'indent-line-function)
123 (setq indent-line-function 'lisp-indent-line)
124 (make-local-variable 'indent-region-function)
125 (setq indent-region-function 'lisp-indent-region)
126 (make-local-variable 'parse-sexp-ignore-comments)
127 (setq parse-sexp-ignore-comments t)
5847f861 128 (make-local-variable 'outline-regexp)
30ff174e 129 (setq outline-regexp ";;; \\|(....")
a90256cc
BP
130 (make-local-variable 'comment-start)
131 (setq comment-start ";")
132 (make-local-variable 'comment-start-skip)
e56a043b
MB
133 ;; Look within the line for a ; following an even number of backslashes
134 ;; after either a non-backslash or the line beginning.
135 (setq comment-start-skip "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\);+ *")
a90256cc
BP
136 (make-local-variable 'comment-column)
137 (setq comment-column 40)
e41b2db1 138 (make-local-variable 'comment-indent-function)
6c2cf866
KH
139 (setq comment-indent-function 'lisp-comment-indent)
140 (make-local-variable 'imenu-generic-expression)
141 (setq imenu-generic-expression lisp-imenu-generic-expression))
a90256cc
BP
142\f
143(defvar shared-lisp-mode-map ()
144 "Keymap for commands shared by all sorts of Lisp modes.")
145
146(if shared-lisp-mode-map
147 ()
148 (setq shared-lisp-mode-map (make-sparse-keymap))
149 (define-key shared-lisp-mode-map "\e\C-q" 'indent-sexp)
3bdffa9e 150 (define-key shared-lisp-mode-map "\177" 'backward-delete-char-untabify))
a90256cc
BP
151
152(defvar emacs-lisp-mode-map ()
153 "Keymap for Emacs Lisp mode.
3bdffa9e 154All commands in `shared-lisp-mode-map' are inherited by this map.")
a90256cc
BP
155
156(if emacs-lisp-mode-map
157 ()
b3a3cb63 158 (let ((map (make-sparse-keymap "Emacs-Lisp")))
b8bc6df2
RS
159 (setq emacs-lisp-mode-map (make-sparse-keymap))
160 (set-keymap-parent emacs-lisp-mode-map shared-lisp-mode-map)
b3a3cb63
KH
161 (define-key emacs-lisp-mode-map "\e\t" 'lisp-complete-symbol)
162 (define-key emacs-lisp-mode-map "\e\C-x" 'eval-defun)
163 (define-key emacs-lisp-mode-map [menu-bar] (make-sparse-keymap))
164 (define-key emacs-lisp-mode-map [menu-bar emacs-lisp]
165 (cons "Emacs-Lisp" map))
166 (define-key map [edebug-defun]
167 '("Instrument Function for Debugging" . edebug-defun))
168 (define-key map [byte-recompile]
169 '("Byte-recompile Directory..." . byte-recompile-directory))
00e40cc9 170 (define-key map [emacs-byte-compile-and-load]
eaec854f 171 '("Byte-compile And Load" . emacs-lisp-byte-compile-and-load))
b3a3cb63
KH
172 (define-key map [byte-compile]
173 '("Byte-compile This File" . emacs-lisp-byte-compile))
174 (define-key map [separator-eval] '("--"))
175 (define-key map [eval-buffer] '("Evaluate Buffer" . eval-current-buffer))
176 (define-key map [eval-region] '("Evaluate Region" . eval-region))
177 (define-key map [eval-sexp] '("Evaluate Last S-expression" . eval-last-sexp))
178 (define-key map [separator-format] '("--"))
179 (define-key map [comment-region] '("Comment Out Region" . comment-region))
180 (define-key map [indent-region] '("Indent Region" . indent-region))
4b619eca
SM
181 (define-key map [indent-line] '("Indent Line" . lisp-indent-line))
182 (put 'eval-region 'menu-enable 'mark-active)
183 (put 'comment-region 'menu-enable 'mark-active)
184 (put 'indent-region 'menu-enable 'mark-active)))
b3a3cb63
KH
185
186(defun emacs-lisp-byte-compile ()
187 "Byte compile the file containing the current buffer."
188 (interactive)
189 (if buffer-file-name
190 (byte-compile-file buffer-file-name)
767a1151
KH
191 (error "The buffer must be saved in a file first")))
192
eaec854f 193(defun emacs-lisp-byte-compile-and-load ()
767a1151
KH
194 "Byte-compile the current file (if it has changed), then load compiled code."
195 (interactive)
196 (or buffer-file-name
197 (error "The buffer must be saved in a file first"))
198 (require 'bytecomp)
199 ;; Recompile if file or buffer has changed since last compilation.
eaec854f
SM
200 (if (and (buffer-modified-p)
201 (y-or-n-p (format "save buffer %s first? " (buffer-name))))
202 (save-buffer))
203 (let ((compiled-file-name (byte-compile-dest-file buffer-file-name)))
204 (if (file-newer-than-file-p compiled-file-name buffer-file-name)
205 (load-file compiled-file-name)
206 (byte-compile-file buffer-file-name t))))
a90256cc
BP
207
208(defun emacs-lisp-mode ()
209 "Major mode for editing Lisp code to run in Emacs.
210Commands:
211Delete converts tabs to spaces as it moves back.
212Blank lines separate paragraphs. Semicolons start comments.
213\\{emacs-lisp-mode-map}
214Entry to this mode calls the value of `emacs-lisp-mode-hook'
215if that value is non-nil."
216 (interactive)
217 (kill-all-local-variables)
218 (use-local-map emacs-lisp-mode-map)
219 (set-syntax-table emacs-lisp-mode-syntax-table)
220 (setq major-mode 'emacs-lisp-mode)
221 (setq mode-name "Emacs-Lisp")
222 (lisp-mode-variables nil)
c0b08eb0 223 (setq imenu-case-fold-search nil)
a90256cc
BP
224 (run-hooks 'emacs-lisp-mode-hook))
225
226(defvar lisp-mode-map ()
227 "Keymap for ordinary Lisp mode.
228All commands in `shared-lisp-mode-map' are inherited by this map.")
229
230(if lisp-mode-map
231 ()
b8bc6df2
RS
232 (setq lisp-mode-map (make-sparse-keymap))
233 (set-keymap-parent lisp-mode-map shared-lisp-mode-map)
ef7485ce
RS
234 (define-key lisp-mode-map "\e\C-x" 'lisp-eval-defun)
235 (define-key lisp-mode-map "\C-c\C-z" 'run-lisp))
a90256cc
BP
236
237(defun lisp-mode ()
238 "Major mode for editing Lisp code for Lisps other than GNU Emacs Lisp.
239Commands:
240Delete converts tabs to spaces as it moves back.
241Blank lines separate paragraphs. Semicolons start comments.
242\\{lisp-mode-map}
243Note that `run-lisp' may be used either to start an inferior Lisp job
244or to switch back to an existing one.
245
246Entry to this mode calls the value of `lisp-mode-hook'
247if that value is non-nil."
248 (interactive)
249 (kill-all-local-variables)
250 (use-local-map lisp-mode-map)
251 (setq major-mode 'lisp-mode)
252 (setq mode-name "Lisp")
253 (lisp-mode-variables t)
c0b08eb0 254 (setq imenu-case-fold-search t)
a90256cc
BP
255 (set-syntax-table lisp-mode-syntax-table)
256 (run-hooks 'lisp-mode-hook))
257
258;; This will do unless shell.el is loaded.
ef7485ce 259(defun lisp-eval-defun nil
a90256cc
BP
260 "Send the current defun to the Lisp process made by \\[run-lisp]."
261 (interactive)
262 (error "Process lisp does not exist"))
263
264(defvar lisp-interaction-mode-map ()
265 "Keymap for Lisp Interaction moe.
266All commands in `shared-lisp-mode-map' are inherited by this map.")
267
268(if lisp-interaction-mode-map
269 ()
b8bc6df2
RS
270 (setq lisp-interaction-mode-map (make-sparse-keymap))
271 (set-keymap-parent lisp-interaction-mode-map shared-lisp-mode-map)
a90256cc 272 (define-key lisp-interaction-mode-map "\e\C-x" 'eval-defun)
ab67260b 273 (define-key lisp-interaction-mode-map "\e\t" 'lisp-complete-symbol)
a90256cc
BP
274 (define-key lisp-interaction-mode-map "\n" 'eval-print-last-sexp))
275
276(defun lisp-interaction-mode ()
277 "Major mode for typing and evaluating Lisp forms.
278Like Lisp mode except that \\[eval-print-last-sexp] evals the Lisp expression
279before point, and prints its value into the buffer, advancing point.
280
281Commands:
282Delete converts tabs to spaces as it moves back.
283Paragraphs are separated only by blank lines.
284Semicolons start comments.
285\\{lisp-interaction-mode-map}
286Entry to this mode calls the value of `lisp-interaction-mode-hook'
287if that value is non-nil."
288 (interactive)
289 (kill-all-local-variables)
290 (use-local-map lisp-interaction-mode-map)
291 (setq major-mode 'lisp-interaction-mode)
292 (setq mode-name "Lisp Interaction")
293 (set-syntax-table emacs-lisp-mode-syntax-table)
294 (lisp-mode-variables nil)
295 (run-hooks 'lisp-interaction-mode-hook))
296
121f0d57 297(defun eval-print-last-sexp ()
a90256cc 298 "Evaluate sexp before point; print value into current buffer."
121f0d57 299 (interactive)
f798d950
JB
300 (let ((standard-output (current-buffer)))
301 (terpri)
302 (eval-last-sexp t)
303 (terpri)))
a90256cc 304\f
efb8f835 305(defun eval-last-sexp (eval-last-sexp-arg-internal)
a90256cc
BP
306 "Evaluate sexp before point; print value in minibuffer.
307With argument, print output into current buffer."
308 (interactive "P")
361721f2
KH
309 (let ((standard-output (if eval-last-sexp-arg-internal (current-buffer) t)))
310 (prin1 (eval (let ((stab (syntax-table))
311 (opoint (point))
05e94d32 312 ignore-quotes
361721f2
KH
313 expr)
314 (unwind-protect
19b2f8f1
RM
315 (save-excursion
316 (set-syntax-table emacs-lisp-mode-syntax-table)
05e94d32
RS
317 ;; If this sexp appears to be enclosed in `...'
318 ;; then ignore the surrounding quotes.
319 (setq ignore-quotes
320 (or (eq (following-char) ?\')
321 (eq (preceding-char) ?\')))
19b2f8f1 322 (forward-sexp -1)
cfe158ab 323 (save-restriction
05e94d32
RS
324 ;; vladimir@cs.ualberta.ca 30-Jul-1997: skip ` in
325 ;; `variable' so that the value is returned, not the
326 ;; name
327 (if (and ignore-quotes
328 (eq (following-char) ?`))
329 (forward-char))
cfe158ab 330 (narrow-to-region (point-min) opoint)
361721f2
KH
331 (setq expr (read (current-buffer)))
332 ;; If it's an (interactive ...) form, it's more
333 ;; useful to show how an interactive call would
334 ;; use it.
335 (and (consp expr)
336 (eq (car expr) 'interactive)
337 (setq expr
338 (list 'call-interactively
339 (list 'quote
340 (list 'lambda
341 '(&rest args)
342 expr
343 'args)))))
344 expr))
19b2f8f1 345 (set-syntax-table stab)))))))
a90256cc 346
efb8f835 347(defun eval-defun (eval-defun-arg-internal)
121f0d57
RM
348 "Evaluate defun that point is in or before.
349Print value in minibuffer.
2298f9f7
KH
350With argument, insert value in current buffer after the defun.
351Return the result of evaluation."
a90256cc 352 (interactive "P")
e31897ed 353 (let ((standard-output (if eval-defun-arg-internal (current-buffer) t))
2d75b395 354 beg end form)
7dbce55e
KH
355 ;; Read the form from the buffer, and record where it ends.
356 (save-excursion
357 (end-of-defun)
358 (beginning-of-defun)
2d75b395 359 (setq beg (point))
7dbce55e
KH
360 (setq form (read (current-buffer)))
361 (setq end (point)))
362 ;; Alter the form if necessary.
2c66f5d1
RS
363 (cond ((and (eq (car form) 'defvar)
364 (cdr-safe (cdr-safe form)))
365 ;; Force variable to be bound.
366 (setq form (cons 'defconst (cdr form))))
367 ((and (eq (car form) 'defcustom)
368 (default-boundp (nth 1 form)))
369 ;; Force variable to be bound.
370 (set-default (nth 1 form) (eval (nth 2 form)))))
7dbce55e
KH
371 ;; Now arrange for eval-region to "read" the (possibly) altered form.
372 ;; eval-region handles recording which file defines a function or variable.
373 (save-excursion
63987c84
RS
374 (eval-region beg end standard-output
375 #'(lambda (ignore)
376 ;; Skipping to the end of the specified region
377 ;; will make eval-region return.
378 (goto-char end)
2298f9f7
KH
379 form))
380 ;; The result of evaluation has been put onto VALUES.
381 ;; So return it.
382 (car values))))
a90256cc
BP
383\f
384(defun lisp-comment-indent ()
385 (if (looking-at "\\s<\\s<\\s<")
386 (current-column)
387 (if (looking-at "\\s<\\s<")
531cbff1 388 (let ((tem (or (calculate-lisp-indent) (current-column))))
a90256cc
BP
389 (if (listp tem) (car tem) tem))
390 (skip-chars-backward " \t")
391 (max (if (bolp) 0 (1+ (current-column)))
392 comment-column))))
393
7eb67d79
RS
394(defun lisp-mode-auto-fill ()
395 (if (> (current-column) (current-fill-column))
396 (if (save-excursion
397 (nth 4 (parse-partial-sexp (save-excursion
398 (beginning-of-defun)
399 (point))
400 (point))))
401 (do-auto-fill)
402 (let ((comment-start nil) (comment-start-skip nil))
403 (do-auto-fill)))))
404
d7fa5aa2
RS
405(defvar lisp-indent-offset nil "")
406(defvar lisp-indent-function 'lisp-indent-function "")
a90256cc
BP
407
408(defun lisp-indent-line (&optional whole-exp)
409 "Indent current line as Lisp code.
410With argument, indent any additional lines of the same expression
411rigidly along with this one."
412 (interactive "P")
413 (let ((indent (calculate-lisp-indent)) shift-amt beg end
414 (pos (- (point-max) (point))))
415 (beginning-of-line)
416 (setq beg (point))
417 (skip-chars-forward " \t")
531cbff1
RS
418 (if (or (null indent) (looking-at "\\s<\\s<\\s<"))
419 ;; Don't alter indentation of a ;;; comment line
420 ;; or a line that starts in a string.
328561fc 421 (goto-char (- (point-max) pos))
a90256cc
BP
422 (if (and (looking-at "\\s<") (not (looking-at "\\s<\\s<")))
423 ;; Single-semicolon comment lines should be indented
424 ;; as comment lines, not as code.
425 (progn (indent-for-comment) (forward-char -1))
426 (if (listp indent) (setq indent (car indent)))
427 (setq shift-amt (- indent (current-column)))
428 (if (zerop shift-amt)
429 nil
430 (delete-region beg (point))
431 (indent-to indent)))
432 ;; If initial point was within line's indentation,
433 ;; position after the indentation. Else stay at same point in text.
434 (if (> (- (point-max) pos) (point))
435 (goto-char (- (point-max) pos)))
436 ;; If desired, shift remaining lines of expression the same amount.
437 (and whole-exp (not (zerop shift-amt))
438 (save-excursion
439 (goto-char beg)
440 (forward-sexp 1)
441 (setq end (point))
442 (goto-char beg)
443 (forward-line 1)
444 (setq beg (point))
445 (> end beg))
446 (indent-code-rigidly beg end shift-amt)))))
447
22486a7f 448(defvar calculate-lisp-indent-last-sexp)
c0df1d61 449
a90256cc
BP
450(defun calculate-lisp-indent (&optional parse-start)
451 "Return appropriate indentation for current line as Lisp code.
452In usual case returns an integer: the column to indent to.
531cbff1
RS
453If the value is nil, that means don't change the indentation
454because the line starts inside a string.
455
456The value can also be a list of the form (COLUMN CONTAINING-SEXP-START).
a90256cc 457This means that following lines at the same level of indentation
531cbff1
RS
458should not necessarily be indented the same as this line.
459Then COLUMN is the column to indent to, and CONTAINING-SEXP-START
460is the buffer position of the start of the containing expression."
a90256cc
BP
461 (save-excursion
462 (beginning-of-line)
463 (let ((indent-point (point))
464 state paren-depth
465 ;; setting this to a number inhibits calling hook
466 (desired-indent nil)
467 (retry t)
c0df1d61 468 calculate-lisp-indent-last-sexp containing-sexp)
a90256cc
BP
469 (if parse-start
470 (goto-char parse-start)
471 (beginning-of-defun))
472 ;; Find outermost containing sexp
473 (while (< (point) indent-point)
474 (setq state (parse-partial-sexp (point) indent-point 0)))
475 ;; Find innermost containing sexp
476 (while (and retry
477 state
478 (> (setq paren-depth (elt state 0)) 0))
479 (setq retry nil)
c0df1d61 480 (setq calculate-lisp-indent-last-sexp (elt state 2))
a90256cc
BP
481 (setq containing-sexp (elt state 1))
482 ;; Position following last unclosed open.
483 (goto-char (1+ containing-sexp))
484 ;; Is there a complete sexp since then?
c0df1d61
RS
485 (if (and calculate-lisp-indent-last-sexp
486 (> calculate-lisp-indent-last-sexp (point)))
a90256cc 487 ;; Yes, but is there a containing sexp after that?
c0df1d61
RS
488 (let ((peek (parse-partial-sexp calculate-lisp-indent-last-sexp
489 indent-point 0)))
a90256cc
BP
490 (if (setq retry (car (cdr peek))) (setq state peek)))))
491 (if retry
492 nil
493 ;; Innermost containing sexp found
494 (goto-char (1+ containing-sexp))
c0df1d61 495 (if (not calculate-lisp-indent-last-sexp)
a90256cc
BP
496 ;; indent-point immediately follows open paren.
497 ;; Don't call hook.
498 (setq desired-indent (current-column))
499 ;; Find the start of first element of containing sexp.
c0df1d61 500 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
a90256cc
BP
501 (cond ((looking-at "\\s(")
502 ;; First element of containing sexp is a list.
503 ;; Indent under that list.
504 )
505 ((> (save-excursion (forward-line 1) (point))
c0df1d61 506 calculate-lisp-indent-last-sexp)
a90256cc
BP
507 ;; This is the first line to start within the containing sexp.
508 ;; It's almost certainly a function call.
c0df1d61 509 (if (= (point) calculate-lisp-indent-last-sexp)
a90256cc
BP
510 ;; Containing sexp has nothing before this line
511 ;; except the first element. Indent under that element.
512 nil
513 ;; Skip the first element, find start of second (the first
514 ;; argument of the function call) and indent under.
515 (progn (forward-sexp 1)
c0df1d61
RS
516 (parse-partial-sexp (point)
517 calculate-lisp-indent-last-sexp
518 0 t)))
a90256cc
BP
519 (backward-prefix-chars))
520 (t
c0df1d61
RS
521 ;; Indent beneath first sexp on same line as
522 ;; calculate-lisp-indent-last-sexp. Again, it's
523 ;; almost certainly a function call.
524 (goto-char calculate-lisp-indent-last-sexp)
a90256cc 525 (beginning-of-line)
c0df1d61
RS
526 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp
527 0 t)
a90256cc
BP
528 (backward-prefix-chars)))))
529 ;; Point is at the point to indent under unless we are inside a string.
eb8c3be9 530 ;; Call indentation hook except when overridden by lisp-indent-offset
a90256cc
BP
531 ;; or if the desired indentation has already been computed.
532 (let ((normal-indent (current-column)))
533 (cond ((elt state 3)
534 ;; Inside a string, don't change indentation.
531cbff1 535 nil)
a90256cc
BP
536 ((and (integerp lisp-indent-offset) containing-sexp)
537 ;; Indent by constant offset
538 (goto-char containing-sexp)
539 (+ (current-column) lisp-indent-offset))
540 (desired-indent)
541 ((and (boundp 'lisp-indent-function)
542 lisp-indent-function
543 (not retry))
544 (or (funcall lisp-indent-function indent-point state)
545 normal-indent))
546 (t
f30ff39f 547 normal-indent))))))
a90256cc
BP
548
549(defun lisp-indent-function (indent-point state)
550 (let ((normal-indent (current-column)))
551 (goto-char (1+ (elt state 1)))
c0df1d61 552 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
a90256cc
BP
553 (if (and (elt state 2)
554 (not (looking-at "\\sw\\|\\s_")))
555 ;; car of form doesn't seem to be a a symbol
556 (progn
557 (if (not (> (save-excursion (forward-line 1) (point))
c0df1d61
RS
558 calculate-lisp-indent-last-sexp))
559 (progn (goto-char calculate-lisp-indent-last-sexp)
a90256cc 560 (beginning-of-line)
c0df1d61
RS
561 (parse-partial-sexp (point)
562 calculate-lisp-indent-last-sexp 0 t)))
563 ;; Indent under the list or under the first sexp on the same
564 ;; line as calculate-lisp-indent-last-sexp. Note that first
565 ;; thing on that line has to be complete sexp since we are
566 ;; inside the innermost containing sexp.
a90256cc
BP
567 (backward-prefix-chars)
568 (current-column))
569 (let ((function (buffer-substring (point)
570 (progn (forward-sexp 1) (point))))
571 method)
2dab7802
RS
572 (setq method (or (get (intern-soft function) 'lisp-indent-function)
573 (get (intern-soft function) 'lisp-indent-hook)))
a90256cc
BP
574 (cond ((or (eq method 'defun)
575 (and (null method)
576 (> (length function) 3)
577 (string-match "\\`def" function)))
578 (lisp-indent-defform state indent-point))
579 ((integerp method)
580 (lisp-indent-specform method state
581 indent-point normal-indent))
582 (method
583 (funcall method state indent-point)))))))
584
d7fa5aa2 585(defvar lisp-body-indent 2
ab69b2fb 586 "Number of columns to indent the second line of a `(def...)' form.")
a90256cc
BP
587
588(defun lisp-indent-specform (count state indent-point normal-indent)
589 (let ((containing-form-start (elt state 1))
590 (i count)
591 body-indent containing-form-column)
592 ;; Move to the start of containing form, calculate indentation
593 ;; to use for non-distinguished forms (> count), and move past the
594 ;; function symbol. lisp-indent-function guarantees that there is at
595 ;; least one word or symbol character following open paren of containing
596 ;; form.
597 (goto-char containing-form-start)
598 (setq containing-form-column (current-column))
599 (setq body-indent (+ lisp-body-indent containing-form-column))
600 (forward-char 1)
601 (forward-sexp 1)
602 ;; Now find the start of the last form.
603 (parse-partial-sexp (point) indent-point 1 t)
604 (while (and (< (point) indent-point)
605 (condition-case ()
606 (progn
607 (setq count (1- count))
608 (forward-sexp 1)
609 (parse-partial-sexp (point) indent-point 1 t))
610 (error nil))))
611 ;; Point is sitting on first character of last (or count) sexp.
612 (if (> count 0)
613 ;; A distinguished form. If it is the first or second form use double
614 ;; lisp-body-indent, else normal indent. With lisp-body-indent bound
615 ;; to 2 (the default), this just happens to work the same with if as
616 ;; the older code, but it makes unwind-protect, condition-case,
617 ;; with-output-to-temp-buffer, et. al. much more tasteful. The older,
618 ;; less hacked, behavior can be obtained by replacing below with
619 ;; (list normal-indent containing-form-start).
620 (if (<= (- i count) 1)
621 (list (+ containing-form-column (* 2 lisp-body-indent))
622 containing-form-start)
623 (list normal-indent containing-form-start))
624 ;; A non-distinguished form. Use body-indent if there are no
625 ;; distinguished forms and this is the first undistinguished form,
626 ;; or if this is the first undistinguished form and the preceding
627 ;; distinguished form has indentation at least as great as body-indent.
628 (if (or (and (= i 0) (= count 0))
629 (and (= count 0) (<= body-indent normal-indent)))
630 body-indent
631 normal-indent))))
632
633(defun lisp-indent-defform (state indent-point)
634 (goto-char (car (cdr state)))
635 (forward-line 1)
636 (if (> (point) (car (cdr (cdr state))))
637 (progn
638 (goto-char (car (cdr state)))
639 (+ lisp-body-indent (current-column)))))
640
641\f
642;; (put 'progn 'lisp-indent-function 0), say, causes progn to be indented
643;; like defun if the first form is placed on the next line, otherwise
644;; it is indented like any other form (i.e. forms line up under first).
645
646(put 'lambda 'lisp-indent-function 'defun)
647(put 'autoload 'lisp-indent-function 'defun)
648(put 'progn 'lisp-indent-function 0)
649(put 'prog1 'lisp-indent-function 1)
650(put 'prog2 'lisp-indent-function 2)
651(put 'save-excursion 'lisp-indent-function 0)
652(put 'save-window-excursion 'lisp-indent-function 0)
4b619eca 653(put 'save-selected-window 'lisp-indent-function 0)
a90256cc 654(put 'save-restriction 'lisp-indent-function 0)
cfe158ab 655(put 'save-match-data 'lisp-indent-function 0)
38f16fe1 656(put 'save-current-buffer 'lisp-indent-function 0)
54c014f0 657(put 'with-current-buffer 'lisp-indent-function 1)
488a0b05 658(put 'combine-after-change-calls 'lisp-indent-function 0)
38f16fe1 659(put 'with-output-to-string 'lisp-indent-function 0)
08adb099 660(put 'with-temp-file 'lisp-indent-function 1)
e0119683 661(put 'with-temp-buffer 'lisp-indent-function 0)
a90256cc
BP
662(put 'let 'lisp-indent-function 1)
663(put 'let* 'lisp-indent-function 1)
664(put 'while 'lisp-indent-function 1)
665(put 'if 'lisp-indent-function 2)
666(put 'catch 'lisp-indent-function 1)
667(put 'condition-case 'lisp-indent-function 2)
668(put 'unwind-protect 'lisp-indent-function 1)
669(put 'with-output-to-temp-buffer 'lisp-indent-function 1)
b6b4c8bd 670(put 'eval-after-load 'lisp-indent-function 1)
a90256cc
BP
671
672(defun indent-sexp (&optional endpos)
673 "Indent each line of the list starting just after point.
674If optional arg ENDPOS is given, indent each line, stopping when
675ENDPOS is encountered."
676 (interactive)
daa37602
JB
677 (let ((indent-stack (list nil))
678 (next-depth 0)
33f268ec
RS
679 ;; If ENDPOS is non-nil, use nil as STARTING-POINT
680 ;; so that calculate-lisp-indent will find the beginning of
681 ;; the defun we are in.
682 ;; If ENDPOS is nil, it is safe not to scan before point
683 ;; since every line we indent is more deeply nested than point is.
684 (starting-point (if endpos nil (point)))
daa37602
JB
685 (last-point (point))
686 last-depth bol outer-loop-done inner-loop-done state this-indent)
33f268ec
RS
687 (or endpos
688 ;; Get error now if we don't have a complete sexp after point.
689 (save-excursion (forward-sexp 1)))
a90256cc
BP
690 (save-excursion
691 (setq outer-loop-done nil)
692 (while (if endpos (< (point) endpos)
693 (not outer-loop-done))
694 (setq last-depth next-depth
695 inner-loop-done nil)
696 ;; Parse this line so we can learn the state
697 ;; to indent the next line.
698 ;; This inner loop goes through only once
699 ;; unless a line ends inside a string.
700 (while (and (not inner-loop-done)
701 (not (setq outer-loop-done (eobp))))
702 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
703 nil nil state))
704 (setq next-depth (car state))
705 ;; If the line contains a comment other than the sort
706 ;; that is indented like code,
707 ;; indent it now with indent-for-comment.
708 ;; Comments indented like code are right already.
709 ;; In any case clear the in-comment flag in the state
710 ;; because parse-partial-sexp never sees the newlines.
711 (if (car (nthcdr 4 state))
712 (progn (indent-for-comment)
713 (end-of-line)
714 (setcar (nthcdr 4 state) nil)))
715 ;; If this line ends inside a string,
716 ;; go straight to next line, remaining within the inner loop,
717 ;; and turn off the \-flag.
718 (if (car (nthcdr 3 state))
719 (progn
720 (forward-line 1)
721 (setcar (nthcdr 5 state) nil))
722 (setq inner-loop-done t)))
723 (and endpos
daa37602
JB
724 (<= next-depth 0)
725 (progn
726 (setq indent-stack (append indent-stack
727 (make-list (- next-depth) nil))
728 last-depth (- last-depth next-depth)
729 next-depth 0)))
33f268ec 730 (or outer-loop-done endpos
a90256cc
BP
731 (setq outer-loop-done (<= next-depth 0)))
732 (if outer-loop-done
1f5038b5 733 (forward-line 1)
a90256cc
BP
734 (while (> last-depth next-depth)
735 (setq indent-stack (cdr indent-stack)
736 last-depth (1- last-depth)))
737 (while (< last-depth next-depth)
738 (setq indent-stack (cons nil indent-stack)
739 last-depth (1+ last-depth)))
740 ;; Now go to the next line and indent it according
741 ;; to what we learned from parsing the previous one.
742 (forward-line 1)
743 (setq bol (point))
744 (skip-chars-forward " \t")
745 ;; But not if the line is blank, or just a comment
746 ;; (except for double-semi comments; indent them as usual).
747 (if (or (eobp) (looking-at "\\s<\\|\n"))
748 nil
749 (if (and (car indent-stack)
750 (>= (car indent-stack) 0))
751 (setq this-indent (car indent-stack))
752 (let ((val (calculate-lisp-indent
753 (if (car indent-stack) (- (car indent-stack))
daa37602 754 starting-point))))
531cbff1
RS
755 (if (null val)
756 (setq this-indent val)
757 (if (integerp val)
758 (setcar indent-stack
759 (setq this-indent val))
760 (setcar indent-stack (- (car (cdr val))))
761 (setq this-indent (car val))))))
762 (if (and this-indent (/= (current-column) this-indent))
a90256cc
BP
763 (progn (delete-region bol (point))
764 (indent-to this-indent)))))
765 (or outer-loop-done
766 (setq outer-loop-done (= (point) last-point))
767 (setq last-point (point)))))))
768
769;; Indent every line whose first char is between START and END inclusive.
770(defun lisp-indent-region (start end)
771 (save-excursion
a90256cc 772 (let ((endmark (copy-marker end)))
33f268ec
RS
773 (goto-char start)
774 (and (bolp) (not (eolp))
775 (lisp-indent-line))
a90256cc
BP
776 (indent-sexp endmark)
777 (set-marker endmark nil))))
778\f
6338c7ba
JB
779;;;; Lisp paragraph filling commands.
780
781(defun lisp-fill-paragraph (&optional justify)
782 "Like \\[fill-paragraph], but handle Emacs Lisp comments.
783If any of the current line is a comment, fill the comment or the
784paragraph of it that point is in, preserving the comment's indentation
785and initial semicolons."
786 (interactive "P")
787 (let (
788 ;; Non-nil if the current line contains a comment.
789 has-comment
790
4d2790ad
RS
791 ;; Non-nil if the current line contains code and a comment.
792 has-code-and-comment
793
6338c7ba
JB
794 ;; If has-comment, the appropriate fill-prefix for the comment.
795 comment-fill-prefix
796 )
797
798 ;; Figure out what kind of comment we are looking at.
799 (save-excursion
800 (beginning-of-line)
801 (cond
802
803 ;; A line with nothing but a comment on it?
804 ((looking-at "[ \t]*;[; \t]*")
805 (setq has-comment t
806 comment-fill-prefix (buffer-substring (match-beginning 0)
807 (match-end 0))))
808
809 ;; A line with some code, followed by a comment? Remember that the
810 ;; semi which starts the comment shouldn't be part of a string or
811 ;; character.
93353fea
KH
812 ((condition-case nil
813 (save-restriction
814 (narrow-to-region (point-min)
815 (save-excursion (end-of-line) (point)))
816 (while (not (looking-at ";\\|$"))
817 (skip-chars-forward "^;\n\"\\\\?")
818 (cond
819 ((eq (char-after (point)) ?\\) (forward-char 2))
820 ((memq (char-after (point)) '(?\" ??)) (forward-sexp 1))))
821 (looking-at ";+[\t ]*"))
822 (error nil))
4d2790ad 823 (setq has-comment t has-code-and-comment t)
6338c7ba 824 (setq comment-fill-prefix
93353fea
KH
825 (concat (make-string (/ (current-column) 8) ?\t)
826 (make-string (% (current-column) 8) ?\ )
6338c7ba
JB
827 (buffer-substring (match-beginning 0) (match-end 0)))))))
828
829 (if (not has-comment)
fe3188ec
DL
830 ;; `paragraph-start' is set here (not in the buffer-local
831 ;; variable so that `forward-paragraph' et al work as
832 ;; expected) so that filling (doc) strings works sensibly.
833 ;; Adding the opening paren to avoid the following sexp being
834 ;; filled means that sexps generally aren't filled as normal
835 ;; text, which is probably sensible. The `;' and `:' stop the
836 ;; filled para at following comment lines and keywords
837 ;; (typically in `defcustom').
838 (let ((paragraph-start (concat paragraph-start
839 "\\|\\s-*[\(;:\"]")))
840 (fill-paragraph justify))
6338c7ba
JB
841
842 ;; Narrow to include only the comment, and then fill the region.
4c06fbee
KH
843 (save-excursion
844 (save-restriction
845 (beginning-of-line)
846 (narrow-to-region
847 ;; Find the first line we should include in the region to fill.
848 (save-excursion
849 (while (and (zerop (forward-line -1))
6338c7ba 850 (looking-at "^[ \t]*;")))
4c06fbee
KH
851 ;; We may have gone too far. Go forward again.
852 (or (looking-at ".*;")
853 (forward-line 1))
854 (point))
855 ;; Find the beginning of the first line past the region to fill.
856 (save-excursion
857 (while (progn (forward-line 1)
858 (looking-at "^[ \t]*;")))
859 (point)))
860
861 ;; Lines with only semicolons on them can be paragraph boundaries.
862 (let* ((paragraph-start (concat paragraph-start "\\|[ \t;]*$"))
863 (paragraph-separate (concat paragraph-start "\\|[ \t;]*$"))
864 (paragraph-ignore-fill-prefix nil)
865 (fill-prefix comment-fill-prefix)
4d2790ad
RS
866 (after-line (if has-code-and-comment
867 (save-excursion
868 (forward-line 1) (point))))
4c06fbee
KH
869 (end (progn
870 (forward-paragraph)
871 (or (bolp) (newline 1))
872 (point)))
4d2790ad
RS
873 ;; If this comment starts on a line with code,
874 ;; include that like in the filling.
875 (beg (progn (backward-paragraph)
876 (if (eq (point) after-line)
877 (forward-line -1))
878 (point))))
4c06fbee
KH
879 (fill-region-as-paragraph beg end
880 justify nil
881 (save-excursion
882 (goto-char beg)
883 (if (looking-at fill-prefix)
884 nil
885 (re-search-forward comment-start-skip)
886 (point))))))))
2b4483bb 887 t))
6338c7ba 888\f
a90256cc
BP
889(defun indent-code-rigidly (start end arg &optional nochange-regexp)
890 "Indent all lines of code, starting in the region, sideways by ARG columns.
891Does not affect lines starting inside comments or strings, assuming that
892the start of the region is not inside them.
893
894Called from a program, takes args START, END, COLUMNS and NOCHANGE-REGEXP.
895The last is a regexp which, if matched at the beginning of a line,
896means don't indent that line."
897 (interactive "r\np")
898 (let (state)
899 (save-excursion
900 (goto-char end)
901 (setq end (point-marker))
902 (goto-char start)
903 (or (bolp)
904 (setq state (parse-partial-sexp (point)
905 (progn
906 (forward-line 1) (point))
907 nil nil state)))
908 (while (< (point) end)
909 (or (car (nthcdr 3 state))
910 (and nochange-regexp
911 (looking-at nochange-regexp))
912 ;; If line does not start in string, indent it
913 (let ((indent (current-indentation)))
914 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
915 (or (eolp)
916 (indent-to (max 0 (+ indent arg)) 0))))
917 (setq state (parse-partial-sexp (point)
918 (progn
919 (forward-line 1) (point))
920 nil nil state))))))
49116ac0
JB
921
922(provide 'lisp-mode)
923
6594deb0 924;;; lisp-mode.el ends here