Commit | Line | Data |
---|---|---|
55535639 | 1 | ;;; lisp-mode.el --- Lisp mode, and its idiosyncratic commands |
6594deb0 | 2 | |
c689a61d | 3 | ;; Copyright (C) 1985, 1986, 1999, 2000, 2001, 2003 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). | |
535eadac | 28 | ;; This mode is documented in the Emacs manual. |
e41b2db1 ER |
29 | |
30 | ;;; Code: | |
31 | ||
535eadac | 32 | (defvar lisp-mode-abbrev-table nil) |
a90256cc | 33 | |
535eadac DL |
34 | (defvar emacs-lisp-mode-syntax-table |
35 | (let ((table (make-syntax-table))) | |
a90256cc | 36 | (let ((i 0)) |
a90256cc | 37 | (while (< i ?0) |
535eadac | 38 | (modify-syntax-entry i "_ " table) |
a90256cc BP |
39 | (setq i (1+ i))) |
40 | (setq i (1+ ?9)) | |
41 | (while (< i ?A) | |
535eadac | 42 | (modify-syntax-entry i "_ " table) |
a90256cc BP |
43 | (setq i (1+ i))) |
44 | (setq i (1+ ?Z)) | |
45 | (while (< i ?a) | |
535eadac | 46 | (modify-syntax-entry i "_ " table) |
a90256cc BP |
47 | (setq i (1+ i))) |
48 | (setq i (1+ ?z)) | |
49 | (while (< i 128) | |
535eadac | 50 | (modify-syntax-entry i "_ " table) |
a90256cc | 51 | (setq i (1+ i))) |
535eadac DL |
52 | (modify-syntax-entry ? " " table) |
53 | (modify-syntax-entry ?\t " " table) | |
54 | (modify-syntax-entry ?\f " " table) | |
55 | (modify-syntax-entry ?\n "> " table) | |
910762b4 | 56 | ;; Give CR the same syntax as newline, for selective-display. |
535eadac DL |
57 | (modify-syntax-entry ?\^m "> " table) |
58 | (modify-syntax-entry ?\; "< " table) | |
59 | (modify-syntax-entry ?` "' " table) | |
60 | (modify-syntax-entry ?' "' " table) | |
61 | (modify-syntax-entry ?, "' " table) | |
592060ab | 62 | (modify-syntax-entry ?@ "' " table) |
a90256cc | 63 | ;; Used to be singlequote; changed for flonums. |
535eadac DL |
64 | (modify-syntax-entry ?. "_ " table) |
65 | (modify-syntax-entry ?# "' " table) | |
66 | (modify-syntax-entry ?\" "\" " table) | |
67 | (modify-syntax-entry ?\\ "\\ " table) | |
68 | (modify-syntax-entry ?\( "() " table) | |
69 | (modify-syntax-entry ?\) ")( " table) | |
70 | (modify-syntax-entry ?\[ "(] " table) | |
1d3529b6 | 71 | (modify-syntax-entry ?\] ")[ " table)) |
535eadac DL |
72 | table)) |
73 | ||
74 | (defvar lisp-mode-syntax-table | |
75 | (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table))) | |
535eadac DL |
76 | (modify-syntax-entry ?\[ "_ " table) |
77 | (modify-syntax-entry ?\] "_ " table) | |
78 | (modify-syntax-entry ?# "' 14bn" table) | |
e5f597f0 | 79 | (modify-syntax-entry ?| "\" 23b" table) |
535eadac | 80 | table)) |
ecca85de | 81 | |
a90256cc BP |
82 | (define-abbrev-table 'lisp-mode-abbrev-table ()) |
83 | ||
6c2cf866 | 84 | (defvar lisp-imenu-generic-expression |
535eadac | 85 | (list |
ca2ddd8e | 86 | (list nil |
d7f519ed GM |
87 | (purecopy (concat "^\\s-*(" |
88 | (eval-when-compile | |
89 | (regexp-opt | |
90 | '("defun" "defun*" "defsubst" "defmacro" | |
91 | "defadvice" "define-skeleton" | |
92 | "define-minor-mode" "define-derived-mode" | |
93 | "define-compiler-macro" "define-modify-macro" | |
94 | "defsetf" "define-setf-expander" | |
95 | "define-method-combination" | |
63b74e64 SM |
96 | "defgeneric" "defmethod") t)) |
97 | "\\s-+\\(\\sw\\(\\sw\\|\\s_\\)+\\)")) | |
d7f519ed | 98 | 2) |
ca2ddd8e | 99 | (list (purecopy "Variables") |
d7f519ed GM |
100 | (purecopy (concat "^\\s-*(" |
101 | (eval-when-compile | |
102 | (regexp-opt | |
103 | '("defvar" "defconst" "defconstant" "defcustom" | |
63b74e64 SM |
104 | "defparameter" "define-symbol-macro") t)) |
105 | "\\s-+\\(\\sw\\(\\sw\\|\\s_\\)+\\)")) | |
d7f519ed | 106 | 2) |
ca2ddd8e | 107 | (list (purecopy "Types") |
d7f519ed GM |
108 | (purecopy (concat "^\\s-*(" |
109 | (eval-when-compile | |
110 | (regexp-opt | |
e2cd29bd JPW |
111 | '("defgroup" "deftheme" "deftype" "defstruct" |
112 | "defclass" "define-condition" "define-widget" | |
113 | "defface" "defpackage") t)) | |
63b74e64 | 114 | "\\s-+'?\\(\\sw\\(\\sw\\|\\s_\\)+\\)")) |
6c2cf866 KH |
115 | 2)) |
116 | ||
117 | "Imenu generic expression for Lisp mode. See `imenu-generic-expression'.") | |
118 | ||
d7f519ed GM |
119 | ;; This was originally in autoload.el and is still used there. |
120 | (put 'autoload 'doc-string-elt 3) | |
121 | (put 'defun 'doc-string-elt 3) | |
122 | (put 'defun* 'doc-string-elt 3) | |
123 | (put 'defvar 'doc-string-elt 3) | |
124 | (put 'defcustom 'doc-string-elt 3) | |
e2cd29bd | 125 | (put 'deftheme 'doc-string-elt 2) |
d7f519ed GM |
126 | (put 'defconst 'doc-string-elt 3) |
127 | (put 'defmacro 'doc-string-elt 3) | |
52c9b141 | 128 | (put 'defmacro* 'doc-string-elt 3) |
d7f519ed GM |
129 | (put 'defsubst 'doc-string-elt 3) |
130 | (put 'define-skeleton 'doc-string-elt 2) | |
131 | (put 'define-derived-mode 'doc-string-elt 4) | |
132 | (put 'easy-mmode-define-minor-mode 'doc-string-elt 2) | |
133 | (put 'define-minor-mode 'doc-string-elt 2) | |
134 | (put 'define-generic-mode 'doc-string-elt 7) | |
135 | ;; define-global-mode has no explicit docstring. | |
136 | (put 'easy-mmode-define-global-mode 'doc-string-elt 0) | |
e2cd29bd | 137 | (put 'define-ibuffer-filter 'doc-string-elt 2) |
f767db5b | 138 | (put 'define-ibuffer-op 'doc-string-elt 3) |
e2cd29bd | 139 | (put 'define-ibuffer-sorter 'doc-string-elt 2) |
d7f519ed GM |
140 | |
141 | (defun lisp-font-lock-syntactic-face-function (state) | |
142 | (if (nth 3 state) | |
143 | (if (and (eq (nth 0 state) 1) | |
144 | ;; This might be a docstring. | |
145 | (save-excursion | |
146 | (let ((n 0)) | |
147 | (goto-char (nth 8 state)) | |
148 | (condition-case nil | |
c689a61d SM |
149 | (while (and (not (bobp)) |
150 | (progn (backward-sexp 1) (setq n (1+ n))))) | |
d7f519ed GM |
151 | (scan-error nil)) |
152 | (when (> n 0) | |
153 | (let ((sym (intern-soft | |
154 | (buffer-substring | |
155 | (point) (progn (forward-sexp 1) (point)))))) | |
156 | (eq n (or (get sym 'doc-string-elt) 3))))))) | |
157 | font-lock-doc-face | |
158 | font-lock-string-face) | |
159 | font-lock-comment-face)) | |
160 | ||
161 | ;; The LISP-SYNTAX argument is used by code in inf-lisp.el and is | |
162 | ;; (uselessly) passed from pp.el, chistory.el, gnus-kill.el and score-mode.el | |
163 | (defun lisp-mode-variables (&optional lisp-syntax) | |
63b74e64 SM |
164 | (when lisp-syntax |
165 | (set-syntax-table lisp-mode-syntax-table)) | |
a90256cc | 166 | (setq local-abbrev-table lisp-mode-abbrev-table) |
a90256cc BP |
167 | (make-local-variable 'paragraph-ignore-fill-prefix) |
168 | (setq paragraph-ignore-fill-prefix t) | |
35d132a8 RS |
169 | (make-local-variable 'fill-paragraph-function) |
170 | (setq fill-paragraph-function 'lisp-fill-paragraph) | |
3272c162 RS |
171 | ;; Adaptive fill mode gets in the way of auto-fill, |
172 | ;; and should make no difference for explicit fill | |
173 | ;; because lisp-fill-paragraph should do the job. | |
63b74e64 SM |
174 | ;; I believe that newcomment's auto-fill code properly deals with it -stef |
175 | ;;(set (make-local-variable 'adaptive-fill-mode) nil) | |
7eb67d79 RS |
176 | (make-local-variable 'normal-auto-fill-function) |
177 | (setq normal-auto-fill-function 'lisp-mode-auto-fill) | |
a90256cc BP |
178 | (make-local-variable 'indent-line-function) |
179 | (setq indent-line-function 'lisp-indent-line) | |
180 | (make-local-variable 'indent-region-function) | |
181 | (setq indent-region-function 'lisp-indent-region) | |
182 | (make-local-variable 'parse-sexp-ignore-comments) | |
183 | (setq parse-sexp-ignore-comments t) | |
5847f861 | 184 | (make-local-variable 'outline-regexp) |
a8050bff GM |
185 | (setq outline-regexp ";;;;* \\|(") |
186 | (make-local-variable 'outline-level) | |
187 | (setq outline-level 'lisp-outline-level) | |
a90256cc BP |
188 | (make-local-variable 'comment-start) |
189 | (setq comment-start ";") | |
190 | (make-local-variable 'comment-start-skip) | |
e56a043b MB |
191 | ;; Look within the line for a ; following an even number of backslashes |
192 | ;; after either a non-backslash or the line beginning. | |
193 | (setq comment-start-skip "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\);+ *") | |
3f0c3d8b SM |
194 | (make-local-variable 'comment-add) |
195 | (setq comment-add 1) ;default to `;;' in comment-region | |
a90256cc BP |
196 | (make-local-variable 'comment-column) |
197 | (setq comment-column 40) | |
e41b2db1 | 198 | (make-local-variable 'comment-indent-function) |
6c2cf866 KH |
199 | (setq comment-indent-function 'lisp-comment-indent) |
200 | (make-local-variable 'imenu-generic-expression) | |
1d3529b6 KH |
201 | (setq imenu-generic-expression lisp-imenu-generic-expression) |
202 | (make-local-variable 'multibyte-syntax-as-symbol) | |
1594a23a | 203 | (setq multibyte-syntax-as-symbol t) |
52cf5c37 | 204 | (set (make-local-variable 'syntax-begin-function) 'beginning-of-defun) |
1594a23a SM |
205 | (setq font-lock-defaults |
206 | '((lisp-font-lock-keywords | |
207 | lisp-font-lock-keywords-1 lisp-font-lock-keywords-2) | |
208 | nil nil (("+-*/.<>=!?$%_&~^:" . "w")) beginning-of-defun | |
d7f519ed GM |
209 | (font-lock-mark-block-function . mark-defun) |
210 | (font-lock-syntactic-face-function | |
211 | . lisp-font-lock-syntactic-face-function)))) | |
a8050bff GM |
212 | |
213 | (defun lisp-outline-level () | |
214 | "Lisp mode `outline-level' function." | |
215 | (if (looking-at "(") | |
216 | 1000 | |
217 | (looking-at outline-regexp) | |
218 | (- (match-end 0) (match-beginning 0)))) | |
219 | ||
ca2ddd8e | 220 | |
1594a23a SM |
221 | (defvar lisp-mode-shared-map |
222 | (let ((map (make-sparse-keymap))) | |
a25e82a8 | 223 | (define-key map "\t" 'lisp-indent-line) |
1594a23a SM |
224 | (define-key map "\e\C-q" 'indent-sexp) |
225 | (define-key map "\177" 'backward-delete-char-untabify) | |
c1acacc4 EZ |
226 | ;; This gets in the way when viewing a Lisp file in view-mode. As |
227 | ;; long as [backspace] is mapped into DEL via the | |
228 | ;; function-key-map, this should remain disabled!! | |
229 | ;;;(define-key map [backspace] 'backward-delete-char-untabify) | |
1594a23a | 230 | map) |
a90256cc BP |
231 | "Keymap for commands shared by all sorts of Lisp modes.") |
232 | ||
a90256cc BP |
233 | (defvar emacs-lisp-mode-map () |
234 | "Keymap for Emacs Lisp mode. | |
99ec65b3 | 235 | All commands in `lisp-mode-shared-map' are inherited by this map.") |
a90256cc BP |
236 | |
237 | (if emacs-lisp-mode-map | |
238 | () | |
b3a3cb63 | 239 | (let ((map (make-sparse-keymap "Emacs-Lisp"))) |
b8bc6df2 | 240 | (setq emacs-lisp-mode-map (make-sparse-keymap)) |
99ec65b3 | 241 | (set-keymap-parent emacs-lisp-mode-map lisp-mode-shared-map) |
b3a3cb63 KH |
242 | (define-key emacs-lisp-mode-map "\e\t" 'lisp-complete-symbol) |
243 | (define-key emacs-lisp-mode-map "\e\C-x" 'eval-defun) | |
244 | (define-key emacs-lisp-mode-map [menu-bar] (make-sparse-keymap)) | |
245 | (define-key emacs-lisp-mode-map [menu-bar emacs-lisp] | |
246 | (cons "Emacs-Lisp" map)) | |
247 | (define-key map [edebug-defun] | |
248 | '("Instrument Function for Debugging" . edebug-defun)) | |
249 | (define-key map [byte-recompile] | |
250 | '("Byte-recompile Directory..." . byte-recompile-directory)) | |
00e40cc9 | 251 | (define-key map [emacs-byte-compile-and-load] |
eaec854f | 252 | '("Byte-compile And Load" . emacs-lisp-byte-compile-and-load)) |
b3a3cb63 KH |
253 | (define-key map [byte-compile] |
254 | '("Byte-compile This File" . emacs-lisp-byte-compile)) | |
255 | (define-key map [separator-eval] '("--")) | |
256 | (define-key map [eval-buffer] '("Evaluate Buffer" . eval-current-buffer)) | |
257 | (define-key map [eval-region] '("Evaluate Region" . eval-region)) | |
258 | (define-key map [eval-sexp] '("Evaluate Last S-expression" . eval-last-sexp)) | |
259 | (define-key map [separator-format] '("--")) | |
260 | (define-key map [comment-region] '("Comment Out Region" . comment-region)) | |
261 | (define-key map [indent-region] '("Indent Region" . indent-region)) | |
4b619eca SM |
262 | (define-key map [indent-line] '("Indent Line" . lisp-indent-line)) |
263 | (put 'eval-region 'menu-enable 'mark-active) | |
264 | (put 'comment-region 'menu-enable 'mark-active) | |
265 | (put 'indent-region 'menu-enable 'mark-active))) | |
b3a3cb63 KH |
266 | |
267 | (defun emacs-lisp-byte-compile () | |
268 | "Byte compile the file containing the current buffer." | |
269 | (interactive) | |
270 | (if buffer-file-name | |
271 | (byte-compile-file buffer-file-name) | |
767a1151 KH |
272 | (error "The buffer must be saved in a file first"))) |
273 | ||
eaec854f | 274 | (defun emacs-lisp-byte-compile-and-load () |
767a1151 KH |
275 | "Byte-compile the current file (if it has changed), then load compiled code." |
276 | (interactive) | |
277 | (or buffer-file-name | |
278 | (error "The buffer must be saved in a file first")) | |
279 | (require 'bytecomp) | |
280 | ;; Recompile if file or buffer has changed since last compilation. | |
eaec854f | 281 | (if (and (buffer-modified-p) |
535eadac | 282 | (y-or-n-p (format "Save buffer %s first? " (buffer-name)))) |
eaec854f SM |
283 | (save-buffer)) |
284 | (let ((compiled-file-name (byte-compile-dest-file buffer-file-name))) | |
285 | (if (file-newer-than-file-p compiled-file-name buffer-file-name) | |
286 | (load-file compiled-file-name) | |
287 | (byte-compile-file buffer-file-name t)))) | |
a90256cc | 288 | |
e596094d DL |
289 | (defcustom emacs-lisp-mode-hook nil |
290 | "Hook run when entering Emacs Lisp mode." | |
535eadac | 291 | :options '(turn-on-eldoc-mode imenu-add-menubar-index checkdoc-minor-mode) |
e596094d DL |
292 | :type 'hook |
293 | :group 'lisp) | |
294 | ||
295 | (defcustom lisp-mode-hook nil | |
296 | "Hook run when entering Lisp mode." | |
297 | :options '(imenu-add-menubar-index) | |
298 | :type 'hook | |
299 | :group 'lisp) | |
300 | ||
301 | (defcustom lisp-interaction-mode-hook nil | |
302 | "Hook run when entering Lisp Interaction mode." | |
303 | :options '(turn-on-eldoc-mode) | |
304 | :type 'hook | |
305 | :group 'lisp) | |
306 | ||
dda7c010 | 307 | (defun emacs-lisp-mode () |
a90256cc BP |
308 | "Major mode for editing Lisp code to run in Emacs. |
309 | Commands: | |
310 | Delete converts tabs to spaces as it moves back. | |
311 | Blank lines separate paragraphs. Semicolons start comments. | |
312 | \\{emacs-lisp-mode-map} | |
313 | Entry to this mode calls the value of `emacs-lisp-mode-hook' | |
314 | if that value is non-nil." | |
dda7c010 RS |
315 | (interactive) |
316 | (kill-all-local-variables) | |
317 | (use-local-map emacs-lisp-mode-map) | |
318 | (set-syntax-table emacs-lisp-mode-syntax-table) | |
319 | (setq major-mode 'emacs-lisp-mode) | |
320 | (setq mode-name "Emacs-Lisp") | |
d7f519ed | 321 | (lisp-mode-variables) |
dda7c010 | 322 | (setq imenu-case-fold-search nil) |
fb16e932 | 323 | (run-mode-hooks 'emacs-lisp-mode-hook)) |
c689a61d | 324 | (put 'emacs-lisp-mode 'custom-mode-group 'lisp) |
a90256cc | 325 | |
535eadac DL |
326 | (defvar lisp-mode-map |
327 | (let ((map (make-sparse-keymap))) | |
99ec65b3 | 328 | (set-keymap-parent map lisp-mode-shared-map) |
535eadac DL |
329 | (define-key map "\e\C-x" 'lisp-eval-defun) |
330 | (define-key map "\C-c\C-z" 'run-lisp) | |
331 | map) | |
a90256cc | 332 | "Keymap for ordinary Lisp mode. |
99ec65b3 | 333 | All commands in `lisp-mode-shared-map' are inherited by this map.") |
a90256cc | 334 | |
dda7c010 | 335 | (defun lisp-mode () |
a90256cc BP |
336 | "Major mode for editing Lisp code for Lisps other than GNU Emacs Lisp. |
337 | Commands: | |
338 | Delete converts tabs to spaces as it moves back. | |
339 | Blank lines separate paragraphs. Semicolons start comments. | |
340 | \\{lisp-mode-map} | |
341 | Note that `run-lisp' may be used either to start an inferior Lisp job | |
342 | or to switch back to an existing one. | |
343 | ||
344 | Entry to this mode calls the value of `lisp-mode-hook' | |
345 | if that value is non-nil." | |
dda7c010 RS |
346 | (interactive) |
347 | (kill-all-local-variables) | |
348 | (use-local-map lisp-mode-map) | |
349 | (setq major-mode 'lisp-mode) | |
350 | (setq mode-name "Lisp") | |
d7f519ed | 351 | (lisp-mode-variables) |
dda7c010 RS |
352 | (make-local-variable 'comment-start-skip) |
353 | (setq comment-start-skip | |
d7f519ed | 354 | "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\)\\(;+\\|#|\\) *") |
dda7c010 RS |
355 | (make-local-variable 'font-lock-keywords-case-fold-search) |
356 | (setq font-lock-keywords-case-fold-search t) | |
357 | (setq imenu-case-fold-search t) | |
358 | (set-syntax-table lisp-mode-syntax-table) | |
fb16e932 | 359 | (run-mode-hooks 'lisp-mode-hook)) |
a90256cc | 360 | |
60b2e60d DL |
361 | ;; Used in old LispM code. |
362 | (defalias 'common-lisp-mode 'lisp-mode) | |
363 | ||
5e871da0 DL |
364 | ;; This will do unless inf-lisp.el is loaded. |
365 | (defun lisp-eval-defun (&optional and-go) | |
a90256cc BP |
366 | "Send the current defun to the Lisp process made by \\[run-lisp]." |
367 | (interactive) | |
368 | (error "Process lisp does not exist")) | |
369 | ||
535eadac DL |
370 | (defvar lisp-interaction-mode-map |
371 | (let ((map (make-sparse-keymap))) | |
99ec65b3 | 372 | (set-keymap-parent map lisp-mode-shared-map) |
535eadac DL |
373 | (define-key map "\e\C-x" 'eval-defun) |
374 | (define-key map "\e\t" 'lisp-complete-symbol) | |
375 | (define-key map "\n" 'eval-print-last-sexp) | |
376 | map) | |
bacd83a7 | 377 | "Keymap for Lisp Interaction mode. |
99ec65b3 | 378 | All commands in `lisp-mode-shared-map' are inherited by this map.") |
a90256cc | 379 | |
52cf5c37 | 380 | (defvar lisp-interaction-mode-abbrev-table lisp-mode-abbrev-table) |
1594a23a | 381 | (define-derived-mode lisp-interaction-mode emacs-lisp-mode "Lisp Interaction" |
a90256cc BP |
382 | "Major mode for typing and evaluating Lisp forms. |
383 | Like Lisp mode except that \\[eval-print-last-sexp] evals the Lisp expression | |
384 | before point, and prints its value into the buffer, advancing point. | |
c9be79b0 | 385 | Note that printing is controlled by `eval-expression-print-length' |
94f8f5d3 | 386 | and `eval-expression-print-level'. |
a90256cc BP |
387 | |
388 | Commands: | |
389 | Delete converts tabs to spaces as it moves back. | |
390 | Paragraphs are separated only by blank lines. | |
391 | Semicolons start comments. | |
392 | \\{lisp-interaction-mode-map} | |
393 | Entry to this mode calls the value of `lisp-interaction-mode-hook' | |
52cf5c37 | 394 | if that value is non-nil.") |
a90256cc | 395 | |
121f0d57 | 396 | (defun eval-print-last-sexp () |
343462ed EZ |
397 | "Evaluate sexp before point; print value into current buffer. |
398 | ||
399 | Note that printing the result is controlled by the variables | |
400 | `eval-expression-print-length' and `eval-expression-print-level', | |
401 | which see." | |
121f0d57 | 402 | (interactive) |
f798d950 JB |
403 | (let ((standard-output (current-buffer))) |
404 | (terpri) | |
405 | (eval-last-sexp t) | |
406 | (terpri))) | |
ca2ddd8e | 407 | |
5f096255 | 408 | |
cb79ea64 GM |
409 | (defun last-sexp-setup-props (beg end value alt1 alt2) |
410 | "Set up text properties for the output of `eval-last-sexp-1'. | |
411 | BEG and END are the start and end of the output in current-buffer. | |
a1506d29 | 412 | VALUE is the Lisp value printed, ALT1 and ALT2 are strings for the |
cb79ea64 GM |
413 | alternative printed representations that can be displayed." |
414 | (let ((map (make-sparse-keymap))) | |
415 | (define-key map "\C-m" 'last-sexp-toggle-display) | |
416 | (define-key map [down-mouse-2] 'mouse-set-point) | |
417 | (define-key map [mouse-2] 'last-sexp-toggle-display) | |
418 | (add-text-properties | |
a1506d29 | 419 | beg end |
cb79ea64 | 420 | `(printed-value (,value ,alt1 ,alt2) |
a1506d29 | 421 | mouse-face highlight |
cb79ea64 GM |
422 | keymap ,map |
423 | help-echo "RET, mouse-2: toggle abbreviated display" | |
424 | rear-nonsticky (mouse-face keymap help-echo | |
425 | printed-value))))) | |
426 | ||
427 | ||
98996d89 | 428 | (defun last-sexp-toggle-display (&optional arg) |
cb79ea64 | 429 | "Toggle between abbreviated and unabbreviated printed representations." |
98996d89 | 430 | (interactive "P") |
3b8d36f1 RS |
431 | (save-restriction |
432 | (widen) | |
98996d89 RS |
433 | (let ((value (get-text-property (point) 'printed-value))) |
434 | (when value | |
435 | (let ((beg (or (previous-single-property-change (min (point-max) (1+ (point))) | |
436 | 'printed-value) | |
437 | (point))) | |
438 | (end (or (next-single-char-property-change (point) 'printed-value) (point))) | |
439 | (standard-output (current-buffer)) | |
440 | (point (point))) | |
441 | (delete-region beg end) | |
442 | (insert (nth 1 value)) | |
443 | (last-sexp-setup-props beg (point) | |
444 | (nth 0 value) | |
445 | (nth 2 value) | |
446 | (nth 1 value)) | |
447 | (goto-char (min (point-max) point))))))) | |
5f096255 | 448 | |
c689a61d SM |
449 | (defun prin1-char (char) |
450 | "Return a string representing CHAR as a character rather than as an integer. | |
451 | If CHAR is not a character, return nil." | |
452 | (and (integerp char) | |
453 | (char-valid-p (event-basic-type char)) | |
454 | (concat | |
455 | "?" | |
456 | (mapconcat | |
457 | (lambda (modif) | |
458 | (cond ((eq modif 'super) "\\s-") | |
459 | (t (string ?\\ (upcase (aref (symbol-name modif) 0)) ?-)))) | |
460 | (event-modifiers char) "") | |
461 | (string (event-basic-type char))))) | |
462 | ||
99c6d63b | 463 | (defun eval-last-sexp-1 (eval-last-sexp-arg-internal) |
a90256cc BP |
464 | "Evaluate sexp before point; print value in minibuffer. |
465 | With argument, print output into current buffer." | |
99c6d63b | 466 | (let ((standard-output (if eval-last-sexp-arg-internal (current-buffer) t))) |
efb195f0 RS |
467 | (let ((value |
468 | (eval (let ((stab (syntax-table)) | |
361721f2 | 469 | (opoint (point)) |
05e94d32 | 470 | ignore-quotes |
361721f2 | 471 | expr) |
c4e2d791 RS |
472 | (save-excursion |
473 | (with-syntax-table emacs-lisp-mode-syntax-table | |
474 | ;; If this sexp appears to be enclosed in `...' | |
475 | ;; then ignore the surrounding quotes. | |
476 | (setq ignore-quotes | |
477 | (or (eq (following-char) ?\') | |
478 | (eq (preceding-char) ?\'))) | |
479 | (forward-sexp -1) | |
480 | ;; If we were after `?\e' (or similar case), | |
481 | ;; use the whole thing, not just the `e'. | |
482 | (when (eq (preceding-char) ?\\) | |
483 | (forward-char -1) | |
484 | (when (eq (preceding-char) ??) | |
485 | (forward-char -1))) | |
486 | ||
487 | ;; Skip over `#N='s. | |
488 | (when (eq (preceding-char) ?=) | |
489 | (let (labeled-p) | |
490 | (save-excursion | |
491 | (skip-chars-backward "0-9#=") | |
492 | (setq labeled-p (looking-at "\\(#[0-9]+=\\)+"))) | |
493 | (when labeled-p | |
494 | (forward-sexp -1)))) | |
495 | ||
496 | (save-restriction | |
497 | ;; vladimir@cs.ualberta.ca 30-Jul-1997: skip ` in | |
498 | ;; `variable' so that the value is returned, not the | |
499 | ;; name | |
500 | (if (and ignore-quotes | |
501 | (eq (following-char) ?`)) | |
502 | (forward-char)) | |
503 | (narrow-to-region (point-min) opoint) | |
504 | (setq expr (read (current-buffer))) | |
505 | ;; If it's an (interactive ...) form, it's more | |
506 | ;; useful to show how an interactive call would | |
507 | ;; use it. | |
508 | (and (consp expr) | |
509 | (eq (car expr) 'interactive) | |
510 | (setq expr | |
511 | (list 'call-interactively | |
512 | (list 'quote | |
513 | (list 'lambda | |
514 | '(&rest args) | |
515 | expr | |
516 | 'args))))) | |
517 | expr))))))) | |
606f5e75 RS |
518 | (eval-last-sexp-print-value value)))) |
519 | ||
520 | (defun eval-last-sexp-print-value (value) | |
521 | (let ((unabbreviated (let ((print-length nil) (print-level nil)) | |
522 | (prin1-to-string value))) | |
523 | (print-length eval-expression-print-length) | |
524 | (print-level eval-expression-print-level) | |
525 | (char-string (prin1-char value)) | |
526 | (beg (point)) | |
527 | end) | |
528 | (prog1 | |
529 | (prin1 value) | |
530 | (if (and (eq standard-output t) char-string) | |
531 | (princ (concat " = " char-string))) | |
532 | (setq end (point)) | |
533 | (when (and (bufferp standard-output) | |
534 | (or (not (null print-length)) | |
535 | (not (null print-level))) | |
536 | (not (string= unabbreviated | |
537 | (buffer-substring-no-properties beg end)))) | |
538 | (last-sexp-setup-props beg end value | |
539 | unabbreviated | |
540 | (buffer-substring-no-properties beg end)) | |
541 | )))) | |
5f096255 | 542 | |
a90256cc | 543 | |
99c6d63b GM |
544 | (defun eval-last-sexp (eval-last-sexp-arg-internal) |
545 | "Evaluate sexp before point; print value in minibuffer. | |
0202508f | 546 | Interactively, with prefix argument, print output into current buffer." |
99c6d63b GM |
547 | (interactive "P") |
548 | (if (null eval-expression-debug-on-error) | |
549 | (eval-last-sexp-1 eval-last-sexp-arg-internal) | |
550 | (let ((old-value (make-symbol "t")) new-value value) | |
551 | (let ((debug-on-error old-value)) | |
552 | (setq value (eval-last-sexp-1 eval-last-sexp-arg-internal)) | |
553 | (setq new-value debug-on-error)) | |
554 | (unless (eq old-value new-value) | |
555 | (setq debug-on-error new-value)) | |
556 | value))) | |
ca2ddd8e | 557 | |
151a410a | 558 | (defun eval-defun-1 (form) |
99ec65b3 DL |
559 | "Change defvar into defconst within FORM. |
560 | Likewise for other constructs as necessary." | |
561 | ;; The code in edebug-defun should be consistent with this, but not | |
562 | ;; the same, since this gets a macroexpended form. | |
726e8778 RS |
563 | (cond ((not (listp form)) |
564 | form) | |
565 | ((and (eq (car form) 'defvar) | |
c689a61d SM |
566 | (cdr-safe (cdr-safe form)) |
567 | (boundp (cadr form))) | |
568 | ;; Force variable to be re-set. | |
569 | `(progn (defvar ,(nth 1 form) nil ,@(nthcdr 3 form)) | |
570 | (setq ,(nth 1 form) ,(nth 2 form)))) | |
5e871da0 DL |
571 | ;; `defcustom' is now macroexpanded to |
572 | ;; `custom-declare-variable' with a quoted value arg. | |
535eadac DL |
573 | ((and (eq (car form) 'custom-declare-variable) |
574 | (default-boundp (eval (nth 1 form)))) | |
151a410a | 575 | ;; Force variable to be bound. |
5e871da0 | 576 | (set-default (eval (nth 1 form)) (eval (nth 1 (nth 2 form)))) |
151a410a RS |
577 | form) |
578 | ((eq (car form) 'progn) | |
579 | (cons 'progn (mapcar 'eval-defun-1 (cdr form)))) | |
580 | (t form))) | |
581 | ||
105d6be1 | 582 | (defun eval-defun-2 () |
121f0d57 | 583 | "Evaluate defun that point is in or before. |
ebc03d28 KH |
584 | The value is displayed in the minibuffer. |
585 | If the current defun is actually a call to `defvar', | |
586 | then reset the variable using the initial value expression | |
587 | even if the variable already has some other value. | |
588 | \(Normally `defvar' does not change the variable's value | |
589 | if it already has a value.\) | |
590 | ||
2298f9f7 KH |
591 | With argument, insert value in current buffer after the defun. |
592 | Return the result of evaluation." | |
a90256cc | 593 | (interactive "P") |
efb195f0 RS |
594 | (let ((debug-on-error eval-expression-debug-on-error) |
595 | (print-length eval-expression-print-length) | |
596 | (print-level eval-expression-print-level)) | |
597 | (save-excursion | |
598 | ;; Arrange for eval-region to "read" the (possibly) altered form. | |
599 | ;; eval-region handles recording which file defines a function or | |
600 | ;; variable. Re-written using `apply' to avoid capturing | |
601 | ;; variables like `end'. | |
602 | (apply | |
ca2ddd8e | 603 | #'eval-region |
105d6be1 | 604 | (let ((standard-output t) |
efb195f0 RS |
605 | beg end form) |
606 | ;; Read the form from the buffer, and record where it ends. | |
607 | (save-excursion | |
608 | (end-of-defun) | |
609 | (beginning-of-defun) | |
610 | (setq beg (point)) | |
611 | (setq form (read (current-buffer))) | |
612 | (setq end (point))) | |
613 | ;; Alter the form if necessary, changing defvar into defconst, etc. | |
614 | (setq form (eval-defun-1 (macroexpand form))) | |
615 | (list beg end standard-output | |
616 | `(lambda (ignore) | |
617 | ;; Skipping to the end of the specified region | |
618 | ;; will make eval-region return. | |
619 | (goto-char ,end) | |
620 | ',form)))))) | |
713c3fb1 DL |
621 | ;; The result of evaluation has been put onto VALUES. So return it. |
622 | (car values)) | |
99c6d63b | 623 | |
105d6be1 GM |
624 | (defun eval-defun (edebug-it) |
625 | "Evaluate the top-level form containing point, or after point. | |
99c6d63b | 626 | |
46f90d0f RS |
627 | If the current defun is actually a call to `defvar' or `defcustom', |
628 | evaluating it this way resets the variable using its initial value | |
629 | expression even if the variable already has some other value. | |
630 | \(Normally `defvar' and `defcustom' do not alter the value if there | |
631 | already is one.) | |
105d6be1 GM |
632 | |
633 | With a prefix argument, instrument the code for Edebug. | |
634 | ||
635 | If acting on a `defun' for FUNCTION, and the function was | |
636 | instrumented, `Edebug: FUNCTION' is printed in the minibuffer. If not | |
637 | instrumented, just FUNCTION is printed. | |
638 | ||
639 | If not acting on a `defun', the result of evaluation is displayed in | |
343462ed EZ |
640 | the minibuffer. This display is controlled by the variables |
641 | `eval-expression-print-length' and `eval-expression-print-level', | |
642 | which see." | |
99c6d63b | 643 | (interactive "P") |
105d6be1 GM |
644 | (cond (edebug-it |
645 | (require 'edebug) | |
646 | (eval-defun (not edebug-all-defs))) | |
647 | (t | |
648 | (if (null eval-expression-debug-on-error) | |
649 | (eval-defun-2) | |
650 | (let ((old-value (make-symbol "t")) new-value value) | |
651 | (let ((debug-on-error old-value)) | |
652 | (setq value (eval-defun-2)) | |
653 | (setq new-value debug-on-error)) | |
654 | (unless (eq old-value new-value) | |
655 | (setq debug-on-error new-value)) | |
656 | value))))) | |
98996d89 | 657 | \f |
ca2ddd8e | 658 | |
a90256cc BP |
659 | (defun lisp-comment-indent () |
660 | (if (looking-at "\\s<\\s<\\s<") | |
661 | (current-column) | |
662 | (if (looking-at "\\s<\\s<") | |
531cbff1 | 663 | (let ((tem (or (calculate-lisp-indent) (current-column)))) |
a90256cc BP |
664 | (if (listp tem) (car tem) tem)) |
665 | (skip-chars-backward " \t") | |
666 | (max (if (bolp) 0 (1+ (current-column))) | |
667 | comment-column)))) | |
668 | ||
63b74e64 SM |
669 | ;; This function just forces a more costly detection of comments (using |
670 | ;; parse-partial-sexp from beginning-of-defun). I.e. It avoids the problem of | |
671 | ;; taking a `;' inside a string started on another line for a comment starter. | |
672 | ;; Note: `newcomment' gets it right in 99% of the cases if you're using | |
673 | ;; font-lock, anyway, so we could get rid of it. -stef | |
7eb67d79 RS |
674 | (defun lisp-mode-auto-fill () |
675 | (if (> (current-column) (current-fill-column)) | |
676 | (if (save-excursion | |
52cf5c37 | 677 | (nth 4 (syntax-ppss (point)))) |
7eb67d79 | 678 | (do-auto-fill) |
52cf5c37 SM |
679 | (unless (and (boundp 'comment-auto-fill-only-comments) |
680 | comment-auto-fill-only-comments) | |
681 | (let ((comment-start nil) (comment-start-skip nil)) | |
682 | (do-auto-fill)))))) | |
7eb67d79 | 683 | |
cada28bb EZ |
684 | (defvar lisp-indent-offset nil |
685 | "If non-nil, indent second line of expressions that many more columns.") | |
535eadac | 686 | (defvar lisp-indent-function 'lisp-indent-function) |
a90256cc BP |
687 | |
688 | (defun lisp-indent-line (&optional whole-exp) | |
689 | "Indent current line as Lisp code. | |
690 | With argument, indent any additional lines of the same expression | |
691 | rigidly along with this one." | |
692 | (interactive "P") | |
d7f519ed GM |
693 | (let ((indent (calculate-lisp-indent)) shift-amt end |
694 | (pos (- (point-max) (point))) | |
695 | (beg (progn (beginning-of-line) (point)))) | |
a90256cc | 696 | (skip-chars-forward " \t") |
531cbff1 RS |
697 | (if (or (null indent) (looking-at "\\s<\\s<\\s<")) |
698 | ;; Don't alter indentation of a ;;; comment line | |
699 | ;; or a line that starts in a string. | |
328561fc | 700 | (goto-char (- (point-max) pos)) |
a90256cc BP |
701 | (if (and (looking-at "\\s<") (not (looking-at "\\s<\\s<"))) |
702 | ;; Single-semicolon comment lines should be indented | |
703 | ;; as comment lines, not as code. | |
704 | (progn (indent-for-comment) (forward-char -1)) | |
705 | (if (listp indent) (setq indent (car indent))) | |
706 | (setq shift-amt (- indent (current-column))) | |
707 | (if (zerop shift-amt) | |
708 | nil | |
709 | (delete-region beg (point)) | |
710 | (indent-to indent))) | |
711 | ;; If initial point was within line's indentation, | |
712 | ;; position after the indentation. Else stay at same point in text. | |
713 | (if (> (- (point-max) pos) (point)) | |
714 | (goto-char (- (point-max) pos))) | |
715 | ;; If desired, shift remaining lines of expression the same amount. | |
716 | (and whole-exp (not (zerop shift-amt)) | |
717 | (save-excursion | |
718 | (goto-char beg) | |
719 | (forward-sexp 1) | |
720 | (setq end (point)) | |
721 | (goto-char beg) | |
722 | (forward-line 1) | |
723 | (setq beg (point)) | |
724 | (> end beg)) | |
725 | (indent-code-rigidly beg end shift-amt))))) | |
726 | ||
22486a7f | 727 | (defvar calculate-lisp-indent-last-sexp) |
c0df1d61 | 728 | |
a90256cc BP |
729 | (defun calculate-lisp-indent (&optional parse-start) |
730 | "Return appropriate indentation for current line as Lisp code. | |
731 | In usual case returns an integer: the column to indent to. | |
531cbff1 RS |
732 | If the value is nil, that means don't change the indentation |
733 | because the line starts inside a string. | |
734 | ||
735 | The value can also be a list of the form (COLUMN CONTAINING-SEXP-START). | |
a90256cc | 736 | This means that following lines at the same level of indentation |
531cbff1 RS |
737 | should not necessarily be indented the same as this line. |
738 | Then COLUMN is the column to indent to, and CONTAINING-SEXP-START | |
739 | is the buffer position of the start of the containing expression." | |
a90256cc BP |
740 | (save-excursion |
741 | (beginning-of-line) | |
742 | (let ((indent-point (point)) | |
743 | state paren-depth | |
744 | ;; setting this to a number inhibits calling hook | |
745 | (desired-indent nil) | |
746 | (retry t) | |
c0df1d61 | 747 | calculate-lisp-indent-last-sexp containing-sexp) |
a90256cc BP |
748 | (if parse-start |
749 | (goto-char parse-start) | |
750 | (beginning-of-defun)) | |
751 | ;; Find outermost containing sexp | |
752 | (while (< (point) indent-point) | |
753 | (setq state (parse-partial-sexp (point) indent-point 0))) | |
754 | ;; Find innermost containing sexp | |
755 | (while (and retry | |
756 | state | |
757 | (> (setq paren-depth (elt state 0)) 0)) | |
758 | (setq retry nil) | |
c0df1d61 | 759 | (setq calculate-lisp-indent-last-sexp (elt state 2)) |
a90256cc BP |
760 | (setq containing-sexp (elt state 1)) |
761 | ;; Position following last unclosed open. | |
762 | (goto-char (1+ containing-sexp)) | |
763 | ;; Is there a complete sexp since then? | |
c0df1d61 RS |
764 | (if (and calculate-lisp-indent-last-sexp |
765 | (> calculate-lisp-indent-last-sexp (point))) | |
a90256cc | 766 | ;; Yes, but is there a containing sexp after that? |
c0df1d61 RS |
767 | (let ((peek (parse-partial-sexp calculate-lisp-indent-last-sexp |
768 | indent-point 0))) | |
a90256cc BP |
769 | (if (setq retry (car (cdr peek))) (setq state peek))))) |
770 | (if retry | |
771 | nil | |
772 | ;; Innermost containing sexp found | |
773 | (goto-char (1+ containing-sexp)) | |
c0df1d61 | 774 | (if (not calculate-lisp-indent-last-sexp) |
a90256cc BP |
775 | ;; indent-point immediately follows open paren. |
776 | ;; Don't call hook. | |
777 | (setq desired-indent (current-column)) | |
778 | ;; Find the start of first element of containing sexp. | |
c0df1d61 | 779 | (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t) |
a90256cc BP |
780 | (cond ((looking-at "\\s(") |
781 | ;; First element of containing sexp is a list. | |
782 | ;; Indent under that list. | |
783 | ) | |
784 | ((> (save-excursion (forward-line 1) (point)) | |
c0df1d61 | 785 | calculate-lisp-indent-last-sexp) |
a90256cc BP |
786 | ;; This is the first line to start within the containing sexp. |
787 | ;; It's almost certainly a function call. | |
c0df1d61 | 788 | (if (= (point) calculate-lisp-indent-last-sexp) |
a90256cc BP |
789 | ;; Containing sexp has nothing before this line |
790 | ;; except the first element. Indent under that element. | |
791 | nil | |
792 | ;; Skip the first element, find start of second (the first | |
793 | ;; argument of the function call) and indent under. | |
794 | (progn (forward-sexp 1) | |
c0df1d61 RS |
795 | (parse-partial-sexp (point) |
796 | calculate-lisp-indent-last-sexp | |
797 | 0 t))) | |
a90256cc BP |
798 | (backward-prefix-chars)) |
799 | (t | |
c0df1d61 | 800 | ;; Indent beneath first sexp on same line as |
535eadac | 801 | ;; `calculate-lisp-indent-last-sexp'. Again, it's |
c0df1d61 RS |
802 | ;; almost certainly a function call. |
803 | (goto-char calculate-lisp-indent-last-sexp) | |
a90256cc | 804 | (beginning-of-line) |
c0df1d61 RS |
805 | (parse-partial-sexp (point) calculate-lisp-indent-last-sexp |
806 | 0 t) | |
a90256cc BP |
807 | (backward-prefix-chars))))) |
808 | ;; Point is at the point to indent under unless we are inside a string. | |
eb8c3be9 | 809 | ;; Call indentation hook except when overridden by lisp-indent-offset |
a90256cc BP |
810 | ;; or if the desired indentation has already been computed. |
811 | (let ((normal-indent (current-column))) | |
812 | (cond ((elt state 3) | |
813 | ;; Inside a string, don't change indentation. | |
531cbff1 | 814 | nil) |
a90256cc BP |
815 | ((and (integerp lisp-indent-offset) containing-sexp) |
816 | ;; Indent by constant offset | |
817 | (goto-char containing-sexp) | |
818 | (+ (current-column) lisp-indent-offset)) | |
819 | (desired-indent) | |
820 | ((and (boundp 'lisp-indent-function) | |
821 | lisp-indent-function | |
822 | (not retry)) | |
823 | (or (funcall lisp-indent-function indent-point state) | |
824 | normal-indent)) | |
825 | (t | |
f30ff39f | 826 | normal-indent)))))) |
a90256cc BP |
827 | |
828 | (defun lisp-indent-function (indent-point state) | |
9fefa08b RS |
829 | "This function is the normal value of the variable `lisp-indent-function'. |
830 | It is used when indenting a line within a function call, to see if the | |
831 | called function says anything special about how to indent the line. | |
832 | ||
833 | INDENT-POINT is the position where the user typed TAB, or equivalent. | |
834 | Point is located at the point to indent under (for default indentation); | |
835 | STATE is the `parse-partial-sexp' state for that position. | |
836 | ||
837 | If the current line is in a call to a Lisp function | |
838 | which has a non-nil property `lisp-indent-function', | |
839 | that specifies how to do the indentation. The property value can be | |
840 | * `defun', meaning indent `defun'-style; | |
841 | * an integer N, meaning indent the first N arguments specially | |
842 | like ordinary function arguments and then indent any further | |
843 | aruments like a body; | |
844 | * a function to call just as this function was called. | |
845 | If that function returns nil, that means it doesn't specify | |
846 | the indentation. | |
847 | ||
848 | This function also returns nil meaning don't specify the indentation." | |
a90256cc BP |
849 | (let ((normal-indent (current-column))) |
850 | (goto-char (1+ (elt state 1))) | |
c0df1d61 | 851 | (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t) |
a90256cc BP |
852 | (if (and (elt state 2) |
853 | (not (looking-at "\\sw\\|\\s_"))) | |
4696802b | 854 | ;; car of form doesn't seem to be a symbol |
a90256cc BP |
855 | (progn |
856 | (if (not (> (save-excursion (forward-line 1) (point)) | |
c0df1d61 | 857 | calculate-lisp-indent-last-sexp)) |
c689a61d SM |
858 | (progn (goto-char calculate-lisp-indent-last-sexp) |
859 | (beginning-of-line) | |
860 | (parse-partial-sexp (point) | |
861 | calculate-lisp-indent-last-sexp 0 t))) | |
862 | ;; Indent under the list or under the first sexp on the same | |
863 | ;; line as calculate-lisp-indent-last-sexp. Note that first | |
864 | ;; thing on that line has to be complete sexp since we are | |
c0df1d61 | 865 | ;; inside the innermost containing sexp. |
a90256cc BP |
866 | (backward-prefix-chars) |
867 | (current-column)) | |
868 | (let ((function (buffer-substring (point) | |
869 | (progn (forward-sexp 1) (point)))) | |
870 | method) | |
2dab7802 RS |
871 | (setq method (or (get (intern-soft function) 'lisp-indent-function) |
872 | (get (intern-soft function) 'lisp-indent-hook))) | |
a90256cc BP |
873 | (cond ((or (eq method 'defun) |
874 | (and (null method) | |
875 | (> (length function) 3) | |
876 | (string-match "\\`def" function))) | |
877 | (lisp-indent-defform state indent-point)) | |
878 | ((integerp method) | |
879 | (lisp-indent-specform method state | |
880 | indent-point normal-indent)) | |
881 | (method | |
882 | (funcall method state indent-point))))))) | |
883 | ||
d7fa5aa2 | 884 | (defvar lisp-body-indent 2 |
ab69b2fb | 885 | "Number of columns to indent the second line of a `(def...)' form.") |
a90256cc BP |
886 | |
887 | (defun lisp-indent-specform (count state indent-point normal-indent) | |
888 | (let ((containing-form-start (elt state 1)) | |
889 | (i count) | |
890 | body-indent containing-form-column) | |
891 | ;; Move to the start of containing form, calculate indentation | |
892 | ;; to use for non-distinguished forms (> count), and move past the | |
893 | ;; function symbol. lisp-indent-function guarantees that there is at | |
894 | ;; least one word or symbol character following open paren of containing | |
895 | ;; form. | |
896 | (goto-char containing-form-start) | |
897 | (setq containing-form-column (current-column)) | |
898 | (setq body-indent (+ lisp-body-indent containing-form-column)) | |
899 | (forward-char 1) | |
900 | (forward-sexp 1) | |
901 | ;; Now find the start of the last form. | |
902 | (parse-partial-sexp (point) indent-point 1 t) | |
903 | (while (and (< (point) indent-point) | |
904 | (condition-case () | |
905 | (progn | |
906 | (setq count (1- count)) | |
907 | (forward-sexp 1) | |
908 | (parse-partial-sexp (point) indent-point 1 t)) | |
909 | (error nil)))) | |
910 | ;; Point is sitting on first character of last (or count) sexp. | |
911 | (if (> count 0) | |
912 | ;; A distinguished form. If it is the first or second form use double | |
913 | ;; lisp-body-indent, else normal indent. With lisp-body-indent bound | |
914 | ;; to 2 (the default), this just happens to work the same with if as | |
915 | ;; the older code, but it makes unwind-protect, condition-case, | |
916 | ;; with-output-to-temp-buffer, et. al. much more tasteful. The older, | |
917 | ;; less hacked, behavior can be obtained by replacing below with | |
918 | ;; (list normal-indent containing-form-start). | |
919 | (if (<= (- i count) 1) | |
920 | (list (+ containing-form-column (* 2 lisp-body-indent)) | |
921 | containing-form-start) | |
922 | (list normal-indent containing-form-start)) | |
923 | ;; A non-distinguished form. Use body-indent if there are no | |
924 | ;; distinguished forms and this is the first undistinguished form, | |
925 | ;; or if this is the first undistinguished form and the preceding | |
926 | ;; distinguished form has indentation at least as great as body-indent. | |
927 | (if (or (and (= i 0) (= count 0)) | |
928 | (and (= count 0) (<= body-indent normal-indent))) | |
929 | body-indent | |
930 | normal-indent)))) | |
931 | ||
932 | (defun lisp-indent-defform (state indent-point) | |
933 | (goto-char (car (cdr state))) | |
934 | (forward-line 1) | |
935 | (if (> (point) (car (cdr (cdr state)))) | |
936 | (progn | |
937 | (goto-char (car (cdr state))) | |
938 | (+ lisp-body-indent (current-column))))) | |
939 | ||
ca2ddd8e | 940 | |
a90256cc BP |
941 | ;; (put 'progn 'lisp-indent-function 0), say, causes progn to be indented |
942 | ;; like defun if the first form is placed on the next line, otherwise | |
943 | ;; it is indented like any other form (i.e. forms line up under first). | |
944 | ||
945 | (put 'lambda 'lisp-indent-function 'defun) | |
946 | (put 'autoload 'lisp-indent-function 'defun) | |
947 | (put 'progn 'lisp-indent-function 0) | |
948 | (put 'prog1 'lisp-indent-function 1) | |
949 | (put 'prog2 'lisp-indent-function 2) | |
950 | (put 'save-excursion 'lisp-indent-function 0) | |
951 | (put 'save-window-excursion 'lisp-indent-function 0) | |
4b619eca | 952 | (put 'save-selected-window 'lisp-indent-function 0) |
a90256cc | 953 | (put 'save-restriction 'lisp-indent-function 0) |
cfe158ab | 954 | (put 'save-match-data 'lisp-indent-function 0) |
38f16fe1 | 955 | (put 'save-current-buffer 'lisp-indent-function 0) |
54c014f0 | 956 | (put 'with-current-buffer 'lisp-indent-function 1) |
488a0b05 | 957 | (put 'combine-after-change-calls 'lisp-indent-function 0) |
38f16fe1 | 958 | (put 'with-output-to-string 'lisp-indent-function 0) |
08adb099 | 959 | (put 'with-temp-file 'lisp-indent-function 1) |
e0119683 | 960 | (put 'with-temp-buffer 'lisp-indent-function 0) |
bacd83a7 | 961 | (put 'with-temp-message 'lisp-indent-function 1) |
83c8f461 | 962 | (put 'with-syntax-table 'lisp-indent-function 1) |
a90256cc BP |
963 | (put 'let 'lisp-indent-function 1) |
964 | (put 'let* 'lisp-indent-function 1) | |
965 | (put 'while 'lisp-indent-function 1) | |
966 | (put 'if 'lisp-indent-function 2) | |
0a88ae7b | 967 | (put 'read-if 'lisp-indent-function 2) |
a90256cc BP |
968 | (put 'catch 'lisp-indent-function 1) |
969 | (put 'condition-case 'lisp-indent-function 2) | |
970 | (put 'unwind-protect 'lisp-indent-function 1) | |
971 | (put 'with-output-to-temp-buffer 'lisp-indent-function 1) | |
b6b4c8bd | 972 | (put 'eval-after-load 'lisp-indent-function 1) |
05c71036 DL |
973 | (put 'dolist 'lisp-indent-function 1) |
974 | (put 'dotimes 'lisp-indent-function 1) | |
975 | (put 'when 'lisp-indent-function 1) | |
976 | (put 'unless 'lisp-indent-function 1) | |
a90256cc BP |
977 | |
978 | (defun indent-sexp (&optional endpos) | |
979 | "Indent each line of the list starting just after point. | |
980 | If optional arg ENDPOS is given, indent each line, stopping when | |
981 | ENDPOS is encountered." | |
982 | (interactive) | |
daa37602 JB |
983 | (let ((indent-stack (list nil)) |
984 | (next-depth 0) | |
33f268ec RS |
985 | ;; If ENDPOS is non-nil, use nil as STARTING-POINT |
986 | ;; so that calculate-lisp-indent will find the beginning of | |
987 | ;; the defun we are in. | |
988 | ;; If ENDPOS is nil, it is safe not to scan before point | |
989 | ;; since every line we indent is more deeply nested than point is. | |
990 | (starting-point (if endpos nil (point))) | |
daa37602 JB |
991 | (last-point (point)) |
992 | last-depth bol outer-loop-done inner-loop-done state this-indent) | |
33f268ec RS |
993 | (or endpos |
994 | ;; Get error now if we don't have a complete sexp after point. | |
995 | (save-excursion (forward-sexp 1))) | |
a90256cc BP |
996 | (save-excursion |
997 | (setq outer-loop-done nil) | |
998 | (while (if endpos (< (point) endpos) | |
999 | (not outer-loop-done)) | |
1000 | (setq last-depth next-depth | |
1001 | inner-loop-done nil) | |
1002 | ;; Parse this line so we can learn the state | |
1003 | ;; to indent the next line. | |
1004 | ;; This inner loop goes through only once | |
1005 | ;; unless a line ends inside a string. | |
1006 | (while (and (not inner-loop-done) | |
1007 | (not (setq outer-loop-done (eobp)))) | |
1008 | (setq state (parse-partial-sexp (point) (progn (end-of-line) (point)) | |
1009 | nil nil state)) | |
1010 | (setq next-depth (car state)) | |
1011 | ;; If the line contains a comment other than the sort | |
1012 | ;; that is indented like code, | |
1013 | ;; indent it now with indent-for-comment. | |
1014 | ;; Comments indented like code are right already. | |
1015 | ;; In any case clear the in-comment flag in the state | |
1016 | ;; because parse-partial-sexp never sees the newlines. | |
1017 | (if (car (nthcdr 4 state)) | |
1018 | (progn (indent-for-comment) | |
1019 | (end-of-line) | |
1020 | (setcar (nthcdr 4 state) nil))) | |
1021 | ;; If this line ends inside a string, | |
1022 | ;; go straight to next line, remaining within the inner loop, | |
1023 | ;; and turn off the \-flag. | |
1024 | (if (car (nthcdr 3 state)) | |
1025 | (progn | |
1026 | (forward-line 1) | |
1027 | (setcar (nthcdr 5 state) nil)) | |
1028 | (setq inner-loop-done t))) | |
1029 | (and endpos | |
daa37602 JB |
1030 | (<= next-depth 0) |
1031 | (progn | |
99ec65b3 DL |
1032 | (setq indent-stack (nconc indent-stack |
1033 | (make-list (- next-depth) nil)) | |
daa37602 JB |
1034 | last-depth (- last-depth next-depth) |
1035 | next-depth 0))) | |
33f268ec | 1036 | (or outer-loop-done endpos |
a90256cc BP |
1037 | (setq outer-loop-done (<= next-depth 0))) |
1038 | (if outer-loop-done | |
1f5038b5 | 1039 | (forward-line 1) |
a90256cc BP |
1040 | (while (> last-depth next-depth) |
1041 | (setq indent-stack (cdr indent-stack) | |
1042 | last-depth (1- last-depth))) | |
1043 | (while (< last-depth next-depth) | |
1044 | (setq indent-stack (cons nil indent-stack) | |
1045 | last-depth (1+ last-depth))) | |
1046 | ;; Now go to the next line and indent it according | |
1047 | ;; to what we learned from parsing the previous one. | |
1048 | (forward-line 1) | |
1049 | (setq bol (point)) | |
1050 | (skip-chars-forward " \t") | |
1051 | ;; But not if the line is blank, or just a comment | |
1052 | ;; (except for double-semi comments; indent them as usual). | |
1053 | (if (or (eobp) (looking-at "\\s<\\|\n")) | |
1054 | nil | |
1055 | (if (and (car indent-stack) | |
1056 | (>= (car indent-stack) 0)) | |
1057 | (setq this-indent (car indent-stack)) | |
1058 | (let ((val (calculate-lisp-indent | |
1059 | (if (car indent-stack) (- (car indent-stack)) | |
daa37602 | 1060 | starting-point)))) |
531cbff1 RS |
1061 | (if (null val) |
1062 | (setq this-indent val) | |
1063 | (if (integerp val) | |
1064 | (setcar indent-stack | |
1065 | (setq this-indent val)) | |
1066 | (setcar indent-stack (- (car (cdr val)))) | |
1067 | (setq this-indent (car val)))))) | |
1068 | (if (and this-indent (/= (current-column) this-indent)) | |
a90256cc BP |
1069 | (progn (delete-region bol (point)) |
1070 | (indent-to this-indent))))) | |
1071 | (or outer-loop-done | |
1072 | (setq outer-loop-done (= (point) last-point)) | |
1073 | (setq last-point (point))))))) | |
1074 | ||
a90256cc | 1075 | (defun lisp-indent-region (start end) |
535eadac | 1076 | "Indent every line whose first char is between START and END inclusive." |
a90256cc | 1077 | (save-excursion |
a90256cc | 1078 | (let ((endmark (copy-marker end))) |
33f268ec RS |
1079 | (goto-char start) |
1080 | (and (bolp) (not (eolp)) | |
1081 | (lisp-indent-line)) | |
a90256cc BP |
1082 | (indent-sexp endmark) |
1083 | (set-marker endmark nil)))) | |
ca2ddd8e | 1084 | |
6338c7ba JB |
1085 | ;;;; Lisp paragraph filling commands. |
1086 | ||
3e8737bf MS |
1087 | (defcustom emacs-lisp-docstring-fill-column 65 |
1088 | "Value of `fill-column' to use when filling a docstring. | |
1089 | Any non-integer value means do not use a different value of | |
1090 | `fill-column' when filling docstrings." | |
1091 | :type '(choice (integer) | |
1092 | (const :tag "Use the current `fill-column'" t)) | |
1093 | :group 'lisp) | |
1094 | ||
6338c7ba | 1095 | (defun lisp-fill-paragraph (&optional justify) |
3e8737bf | 1096 | "Like \\[fill-paragraph], but handle Emacs Lisp comments and docstrings. |
6338c7ba JB |
1097 | If any of the current line is a comment, fill the comment or the |
1098 | paragraph of it that point is in, preserving the comment's indentation | |
1099 | and initial semicolons." | |
1100 | (interactive "P") | |
833815e8 | 1101 | (or (fill-comment-paragraph justify) |
3e8737bf MS |
1102 | ;; Point is on a program line (a line no comment); we are interested |
1103 | ;; particularly in docstring lines. | |
1104 | ;; | |
1105 | ;; We bind `paragraph-start' and `paragraph-separate' temporarily. They | |
1106 | ;; are buffer-local, but we avoid changing them so that they can be set | |
1107 | ;; to make `forward-paragraph' and friends do something the user wants. | |
1108 | ;; | |
1109 | ;; `paragraph-start': The `(' in the character alternative and the | |
1110 | ;; left-singlequote plus `(' sequence after the \\| alternative prevent | |
1111 | ;; sexps and backquoted sexps that follow a docstring from being filled | |
1112 | ;; with the docstring. This setting has the consequence of inhibiting | |
1113 | ;; filling many program lines that are not docstrings, which is sensible, | |
1114 | ;; because the user probably asked to fill program lines by accident, or | |
1115 | ;; expecting indentation (perhaps we should try to do indenting in that | |
1116 | ;; case). The `;' and `:' stop the paragraph being filled at following | |
1117 | ;; comment lines and at keywords (e.g., in `defcustom'). Left parens are | |
1118 | ;; escaped to keep font-locking, filling, & paren matching in the source | |
1119 | ;; file happy. | |
1120 | ;; | |
1121 | ;; `paragraph-separate': A clever regexp distinguishes the first line of | |
1122 | ;; a docstring and identifies it as a paragraph separator, so that it | |
1123 | ;; won't be filled. (Since the first line of documentation stands alone | |
1124 | ;; in some contexts, filling should not alter the contents the author has | |
1125 | ;; chosen.) Only the first line of a docstring begins with whitespace | |
1126 | ;; and a quotation mark and ends with a period or (rarely) a comma. | |
1127 | ;; | |
1128 | ;; The `fill-column' is temporarily bound to | |
1129 | ;; `emacs-lisp-docstring-fill-column' if that value is an integer. | |
833815e8 | 1130 | (let ((paragraph-start (concat paragraph-start |
3e8737bf | 1131 | "\\|\\s-*\\([\(;:\"]\\|`\(\\)")) |
833815e8 | 1132 | (paragraph-separate |
3e8737bf MS |
1133 | (concat paragraph-separate "\\|\\s-*\".*[,\\.]$")) |
1134 | (fill-column (if (integerp emacs-lisp-docstring-fill-column) | |
1135 | emacs-lisp-docstring-fill-column | |
1136 | fill-column))) | |
833815e8 SM |
1137 | (fill-paragraph justify)) |
1138 | ;; Never return nil. | |
1139 | t)) | |
ca2ddd8e | 1140 | |
a90256cc BP |
1141 | (defun indent-code-rigidly (start end arg &optional nochange-regexp) |
1142 | "Indent all lines of code, starting in the region, sideways by ARG columns. | |
1143 | Does not affect lines starting inside comments or strings, assuming that | |
1144 | the start of the region is not inside them. | |
1145 | ||
1146 | Called from a program, takes args START, END, COLUMNS and NOCHANGE-REGEXP. | |
1147 | The last is a regexp which, if matched at the beginning of a line, | |
1148 | means don't indent that line." | |
1149 | (interactive "r\np") | |
1150 | (let (state) | |
1151 | (save-excursion | |
1152 | (goto-char end) | |
1153 | (setq end (point-marker)) | |
1154 | (goto-char start) | |
1155 | (or (bolp) | |
1156 | (setq state (parse-partial-sexp (point) | |
1157 | (progn | |
1158 | (forward-line 1) (point)) | |
1159 | nil nil state))) | |
1160 | (while (< (point) end) | |
1161 | (or (car (nthcdr 3 state)) | |
1162 | (and nochange-regexp | |
1163 | (looking-at nochange-regexp)) | |
1164 | ;; If line does not start in string, indent it | |
1165 | (let ((indent (current-indentation))) | |
1166 | (delete-region (point) (progn (skip-chars-forward " \t") (point))) | |
1167 | (or (eolp) | |
1168 | (indent-to (max 0 (+ indent arg)) 0)))) | |
1169 | (setq state (parse-partial-sexp (point) | |
1170 | (progn | |
1171 | (forward-line 1) (point)) | |
1172 | nil nil state)))))) | |
49116ac0 JB |
1173 | |
1174 | (provide 'lisp-mode) | |
1175 | ||
ab5796a9 | 1176 | ;;; arch-tag: 414c7f93-c245-4b77-8ed5-ed05ef7ff1bf |
6594deb0 | 1177 | ;;; lisp-mode.el ends here |