Commit | Line | Data |
---|---|---|
d1ad37b2 | 1 | ;;; lisp-mode.el --- Lisp mode, and its idiosyncratic commands -*- coding: utf-8 -*- |
d6a3febd | 2 | |
ab422c4d | 3 | ;; Copyright (C) 1985-1986, 1999-2013 Free Software Foundation, Inc. |
d6a3febd MR |
4 | |
5 | ;; Maintainer: FSF | |
6 | ;; Keywords: lisp, languages | |
bd78fa1d | 7 | ;; Package: emacs |
d6a3febd MR |
8 | |
9 | ;; This file is part of GNU Emacs. | |
10 | ||
11 | ;; GNU Emacs is free software: you can redistribute it and/or modify | |
12 | ;; it under the terms of the GNU General Public License as published by | |
13 | ;; the Free Software Foundation, either version 3 of the License, or | |
14 | ;; (at your option) any later version. | |
15 | ||
16 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | ;; GNU General Public License for more details. | |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
22 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. | |
23 | ||
24 | ;;; Commentary: | |
25 | ||
26 | ;; The base major mode for editing Lisp code (used also for Emacs Lisp). | |
27 | ;; This mode is documented in the Emacs manual. | |
28 | ||
29 | ;;; Code: | |
30 | ||
31 | (defvar font-lock-comment-face) | |
32 | (defvar font-lock-doc-face) | |
33 | (defvar font-lock-keywords-case-fold-search) | |
34 | (defvar font-lock-string-face) | |
35 | ||
36 | (defvar lisp-mode-abbrev-table nil) | |
db174434 CY |
37 | (define-abbrev-table 'lisp-mode-abbrev-table () |
38 | "Abbrev table for Lisp mode.") | |
d6a3febd | 39 | |
db174434 CY |
40 | (defvar emacs-lisp-mode-abbrev-table nil) |
41 | (define-abbrev-table 'emacs-lisp-mode-abbrev-table () | |
42 | "Abbrev table for Emacs Lisp mode. | |
43 | It has `lisp-mode-abbrev-table' as its parent." | |
44 | :parents (list lisp-mode-abbrev-table)) | |
d6a3febd MR |
45 | |
46 | (defvar emacs-lisp-mode-syntax-table | |
da68c4c8 DD |
47 | (let ((table (make-syntax-table)) |
48 | (i 0)) | |
49 | (while (< i ?0) | |
50 | (modify-syntax-entry i "_ " table) | |
51 | (setq i (1+ i))) | |
52 | (setq i (1+ ?9)) | |
53 | (while (< i ?A) | |
54 | (modify-syntax-entry i "_ " table) | |
55 | (setq i (1+ i))) | |
56 | (setq i (1+ ?Z)) | |
57 | (while (< i ?a) | |
58 | (modify-syntax-entry i "_ " table) | |
59 | (setq i (1+ i))) | |
60 | (setq i (1+ ?z)) | |
61 | (while (< i 128) | |
62 | (modify-syntax-entry i "_ " table) | |
63 | (setq i (1+ i))) | |
64 | (modify-syntax-entry ?\s " " table) | |
65 | ;; Non-break space acts as whitespace. | |
66 | (modify-syntax-entry ?\x8a0 " " table) | |
67 | (modify-syntax-entry ?\t " " table) | |
68 | (modify-syntax-entry ?\f " " table) | |
69 | (modify-syntax-entry ?\n "> " table) | |
70 | ;; This is probably obsolete since nowadays such features use overlays. | |
71 | ;; ;; Give CR the same syntax as newline, for selective-display. | |
72 | ;; (modify-syntax-entry ?\^m "> " table) | |
73 | (modify-syntax-entry ?\; "< " table) | |
74 | (modify-syntax-entry ?` "' " table) | |
75 | (modify-syntax-entry ?' "' " table) | |
76 | (modify-syntax-entry ?, "' " table) | |
77 | (modify-syntax-entry ?@ "' " table) | |
78 | ;; Used to be singlequote; changed for flonums. | |
79 | (modify-syntax-entry ?. "_ " table) | |
80 | (modify-syntax-entry ?# "' " table) | |
81 | (modify-syntax-entry ?\" "\" " table) | |
82 | (modify-syntax-entry ?\\ "\\ " table) | |
83 | (modify-syntax-entry ?\( "() " table) | |
84 | (modify-syntax-entry ?\) ")( " table) | |
85 | (modify-syntax-entry ?\[ "(] " table) | |
86 | (modify-syntax-entry ?\] ")[ " table) | |
e4a09a11 GM |
87 | table) |
88 | "Syntax table used in `emacs-lisp-mode'.") | |
d6a3febd MR |
89 | |
90 | (defvar lisp-mode-syntax-table | |
91 | (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table))) | |
92 | (modify-syntax-entry ?\[ "_ " table) | |
93 | (modify-syntax-entry ?\] "_ " table) | |
c5683ceb | 94 | (modify-syntax-entry ?# "' 14" table) |
d6a3febd | 95 | (modify-syntax-entry ?| "\" 23bn" table) |
a8106aec GM |
96 | table) |
97 | "Syntax table used in `lisp-mode'.") | |
d6a3febd MR |
98 | |
99 | (defvar lisp-imenu-generic-expression | |
100 | (list | |
101 | (list nil | |
102 | (purecopy (concat "^\\s-*(" | |
103 | (eval-when-compile | |
104 | (regexp-opt | |
105 | '("defun" "defun*" "defsubst" "defmacro" | |
106 | "defadvice" "define-skeleton" | |
107 | "define-minor-mode" "define-global-minor-mode" | |
108 | "define-globalized-minor-mode" | |
109 | "define-derived-mode" "define-generic-mode" | |
110 | "define-compiler-macro" "define-modify-macro" | |
111 | "defsetf" "define-setf-expander" | |
112 | "define-method-combination" | |
185e3b5a JB |
113 | "defgeneric" "defmethod" |
114 | "cl-defun" "cl-defsubst" "cl-defmacro" | |
115 | "cl-define-compiler-macro") t)) | |
d6a3febd MR |
116 | "\\s-+\\(\\(\\sw\\|\\s_\\)+\\)")) |
117 | 2) | |
118 | (list (purecopy "Variables") | |
119 | (purecopy (concat "^\\s-*(" | |
120 | (eval-when-compile | |
121 | (regexp-opt | |
b7ccbdc2 | 122 | '("defconst" "defconstant" "defcustom" |
d6a3febd MR |
123 | "defparameter" "define-symbol-macro") t)) |
124 | "\\s-+\\(\\(\\sw\\|\\s_\\)+\\)")) | |
125 | 2) | |
b7ccbdc2 CY |
126 | ;; For `defvar', we ignore (defvar FOO) constructs. |
127 | (list (purecopy "Variables") | |
128 | (purecopy (concat "^\\s-*(defvar\\s-+\\(\\(\\sw\\|\\s_\\)+\\)" | |
129 | "[[:space:]\n]+[^)]")) | |
130 | 1) | |
d6a3febd MR |
131 | (list (purecopy "Types") |
132 | (purecopy (concat "^\\s-*(" | |
133 | (eval-when-compile | |
134 | (regexp-opt | |
135 | '("defgroup" "deftheme" "deftype" "defstruct" | |
136 | "defclass" "define-condition" "define-widget" | |
185e3b5a JB |
137 | "defface" "defpackage" "cl-deftype" |
138 | "cl-defstruct") t)) | |
d6a3febd MR |
139 | "\\s-+'?\\(\\(\\sw\\|\\s_\\)+\\)")) |
140 | 2)) | |
141 | ||
142 | "Imenu generic expression for Lisp mode. See `imenu-generic-expression'.") | |
143 | ||
144 | ;; This was originally in autoload.el and is still used there. | |
145 | (put 'autoload 'doc-string-elt 3) | |
98449af8 | 146 | (put 'defmethod 'doc-string-elt 3) |
d6a3febd | 147 | (put 'defvar 'doc-string-elt 3) |
d6a3febd | 148 | (put 'defconst 'doc-string-elt 3) |
d6a3febd MR |
149 | (put 'defalias 'doc-string-elt 3) |
150 | (put 'defvaralias 'doc-string-elt 3) | |
151 | (put 'define-category 'doc-string-elt 2) | |
152 | ||
153 | (defvar lisp-doc-string-elt-property 'doc-string-elt | |
154 | "The symbol property that holds the docstring position info.") | |
155 | ||
156 | (defun lisp-font-lock-syntactic-face-function (state) | |
157 | (if (nth 3 state) | |
158 | ;; This might be a (doc)string or a |...| symbol. | |
159 | (let ((startpos (nth 8 state))) | |
160 | (if (eq (char-after startpos) ?|) | |
161 | ;; This is not a string, but a |...| symbol. | |
162 | nil | |
163 | (let* ((listbeg (nth 1 state)) | |
164 | (firstsym (and listbeg | |
165 | (save-excursion | |
166 | (goto-char listbeg) | |
167 | (and (looking-at "([ \t\n]*\\(\\(\\sw\\|\\s_\\)+\\)") | |
168 | (match-string 1))))) | |
7abaf5cc SM |
169 | (docelt (and firstsym |
170 | (function-get (intern-soft firstsym) | |
d6a3febd MR |
171 | lisp-doc-string-elt-property)))) |
172 | (if (and docelt | |
173 | ;; It's a string in a form that can have a docstring. | |
174 | ;; Check whether it's in docstring position. | |
175 | (save-excursion | |
176 | (when (functionp docelt) | |
177 | (goto-char (match-end 1)) | |
178 | (setq docelt (funcall docelt))) | |
179 | (goto-char listbeg) | |
180 | (forward-char 1) | |
181 | (condition-case nil | |
182 | (while (and (> docelt 0) (< (point) startpos) | |
183 | (progn (forward-sexp 1) t)) | |
184 | (setq docelt (1- docelt))) | |
185 | (error nil)) | |
186 | (and (zerop docelt) (<= (point) startpos) | |
187 | (progn (forward-comment (point-max)) t) | |
188 | (= (point) (nth 8 state))))) | |
189 | font-lock-doc-face | |
190 | font-lock-string-face)))) | |
191 | font-lock-comment-face)) | |
192 | ||
913274cb | 193 | (defun lisp-mode-variables (&optional lisp-syntax keywords-case-insensitive) |
94686171 DN |
194 | "Common initialization routine for lisp modes. |
195 | The LISP-SYNTAX argument is used by code in inf-lisp.el and is | |
5249a62d MR |
196 | \(uselessly) passed from pp.el, chistory.el, gnus-kill.el and |
197 | score-mode.el. KEYWORDS-CASE-INSENSITIVE non-nil means that for | |
913274cb | 198 | font-lock keywords will not be case sensitive." |
d6a3febd MR |
199 | (when lisp-syntax |
200 | (set-syntax-table lisp-mode-syntax-table)) | |
92eadba5 CY |
201 | (setq-local paragraph-ignore-fill-prefix t) |
202 | (setq-local fill-paragraph-function 'lisp-fill-paragraph) | |
d6a3febd MR |
203 | ;; Adaptive fill mode gets the fill wrong for a one-line paragraph made of |
204 | ;; a single docstring. Let's fix it here. | |
92eadba5 CY |
205 | (setq-local adaptive-fill-function |
206 | (lambda () (if (looking-at "\\s-+\"[^\n\"]+\"\\s-*$") ""))) | |
d6a3febd MR |
207 | ;; Adaptive fill mode gets in the way of auto-fill, |
208 | ;; and should make no difference for explicit fill | |
209 | ;; because lisp-fill-paragraph should do the job. | |
210 | ;; I believe that newcomment's auto-fill code properly deals with it -stef | |
211 | ;;(set (make-local-variable 'adaptive-fill-mode) nil) | |
92eadba5 CY |
212 | (setq-local indent-line-function 'lisp-indent-line) |
213 | (setq-local outline-regexp ";;;\\(;* [^ \t\n]\\|###autoload\\)\\|(") | |
214 | (setq-local outline-level 'lisp-outline-level) | |
ba03d0d9 | 215 | (setq-local add-log-current-defun-function #'lisp-current-defun-name) |
92eadba5 | 216 | (setq-local comment-start ";") |
d6a3febd MR |
217 | ;; Look within the line for a ; following an even number of backslashes |
218 | ;; after either a non-backslash or the line beginning. | |
92eadba5 | 219 | (setq-local comment-start-skip "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\);+ *") |
d6a3febd | 220 | ;; Font lock mode uses this only when it KNOWS a comment is starting. |
92eadba5 CY |
221 | (setq-local font-lock-comment-start-skip ";+ *") |
222 | (setq-local comment-add 1) ;default to `;;' in comment-region | |
223 | (setq-local comment-column 40) | |
d6a3febd | 224 | ;; Don't get confused by `;' in doc strings when paragraph-filling. |
92eadba5 CY |
225 | (setq-local comment-use-global-state t) |
226 | (setq-local imenu-generic-expression lisp-imenu-generic-expression) | |
227 | (setq-local multibyte-syntax-as-symbol t) | |
228 | (setq-local syntax-begin-function 'beginning-of-defun) | |
d6a3febd | 229 | (setq font-lock-defaults |
8fc57765 TZ |
230 | `((lisp-font-lock-keywords |
231 | lisp-font-lock-keywords-1 | |
232 | lisp-font-lock-keywords-2) | |
913274cb | 233 | nil ,keywords-case-insensitive nil nil |
d6a3febd MR |
234 | (font-lock-mark-block-function . mark-defun) |
235 | (font-lock-syntactic-face-function | |
7de135d0 | 236 | . lisp-font-lock-syntactic-face-function))) |
14dd22d2 | 237 | (setq-local prettify-symbols-alist lisp--prettify-symbols-alist)) |
d6a3febd MR |
238 | |
239 | (defun lisp-outline-level () | |
240 | "Lisp mode `outline-level' function." | |
241 | (let ((len (- (match-end 0) (match-beginning 0)))) | |
242 | (if (looking-at "(\\|;;;###autoload") | |
243 | 1000 | |
244 | len))) | |
245 | ||
ba03d0d9 CY |
246 | (defun lisp-current-defun-name () |
247 | "Return the name of the defun at point, or nil." | |
9dffb5b6 CY |
248 | (save-excursion |
249 | (let ((location (point))) | |
250 | ;; If we are now precisely at the beginning of a defun, make sure | |
251 | ;; beginning-of-defun finds that one rather than the previous one. | |
252 | (or (eobp) (forward-char 1)) | |
253 | (beginning-of-defun) | |
254 | ;; Make sure we are really inside the defun found, not after it. | |
255 | (when (and (looking-at "\\s(") | |
256 | (progn (end-of-defun) | |
257 | (< location (point))) | |
258 | (progn (forward-sexp -1) | |
259 | (>= location (point)))) | |
260 | (if (looking-at "\\s(") | |
261 | (forward-char 1)) | |
262 | ;; Skip the defining construct name, typically "defun" or | |
263 | ;; "defvar". | |
264 | (forward-sexp 1) | |
265 | ;; The second element is usually a symbol being defined. If it | |
266 | ;; is not, use the first symbol in it. | |
267 | (skip-chars-forward " \t\n'(") | |
268 | (buffer-substring-no-properties (point) | |
269 | (progn (forward-sexp 1) | |
270 | (point))))))) | |
ba03d0d9 | 271 | |
d6a3febd MR |
272 | (defvar lisp-mode-shared-map |
273 | (let ((map (make-sparse-keymap))) | |
33e249a2 | 274 | (set-keymap-parent map prog-mode-map) |
d6a3febd MR |
275 | (define-key map "\e\C-q" 'indent-sexp) |
276 | (define-key map "\177" 'backward-delete-char-untabify) | |
277 | ;; This gets in the way when viewing a Lisp file in view-mode. As | |
278 | ;; long as [backspace] is mapped into DEL via the | |
279 | ;; function-key-map, this should remain disabled!! | |
280 | ;;;(define-key map [backspace] 'backward-delete-char-untabify) | |
281 | map) | |
282 | "Keymap for commands shared by all sorts of Lisp modes.") | |
283 | ||
284 | (defvar emacs-lisp-mode-map | |
285 | (let ((map (make-sparse-keymap "Emacs-Lisp")) | |
286 | (menu-map (make-sparse-keymap "Emacs-Lisp")) | |
a707eb05 | 287 | (lint-map (make-sparse-keymap)) |
d6a3febd MR |
288 | (prof-map (make-sparse-keymap)) |
289 | (tracing-map (make-sparse-keymap))) | |
290 | (set-keymap-parent map lisp-mode-shared-map) | |
51ef56c4 | 291 | (define-key map "\e\t" 'completion-at-point) |
d6a3febd MR |
292 | (define-key map "\e\C-x" 'eval-defun) |
293 | (define-key map "\e\C-q" 'indent-pp-sexp) | |
1ec4b7b2 SM |
294 | (bindings--define-key map [menu-bar emacs-lisp] |
295 | (cons "Emacs-Lisp" menu-map)) | |
296 | (bindings--define-key menu-map [eldoc] | |
297 | '(menu-item "Auto-Display Documentation Strings" eldoc-mode | |
d6a3febd | 298 | :button (:toggle . (bound-and-true-p eldoc-mode)) |
1ec4b7b2 SM |
299 | :help "Display the documentation string for the item under cursor")) |
300 | (bindings--define-key menu-map [checkdoc] | |
301 | '(menu-item "Check Documentation Strings" checkdoc | |
302 | :help "Check documentation strings for style requirements")) | |
303 | (bindings--define-key menu-map [re-builder] | |
304 | '(menu-item "Construct Regexp" re-builder | |
305 | :help "Construct a regexp interactively")) | |
306 | (bindings--define-key menu-map [tracing] (cons "Tracing" tracing-map)) | |
307 | (bindings--define-key tracing-map [tr-a] | |
308 | '(menu-item "Untrace All" untrace-all | |
309 | :help "Untrace all currently traced functions")) | |
310 | (bindings--define-key tracing-map [tr-uf] | |
311 | '(menu-item "Untrace Function..." untrace-function | |
312 | :help "Untrace function, and possibly activate all remaining advice")) | |
313 | (bindings--define-key tracing-map [tr-sep] menu-bar-separator) | |
314 | (bindings--define-key tracing-map [tr-q] | |
315 | '(menu-item "Trace Function Quietly..." trace-function-background | |
316 | :help "Trace the function with trace output going quietly to a buffer")) | |
317 | (bindings--define-key tracing-map [tr-f] | |
318 | '(menu-item "Trace Function..." trace-function | |
319 | :help "Trace the function given as an argument")) | |
320 | (bindings--define-key menu-map [profiling] (cons "Profiling" prof-map)) | |
321 | (bindings--define-key prof-map [prof-restall] | |
322 | '(menu-item "Remove Instrumentation for All Functions" elp-restore-all | |
323 | :help "Restore the original definitions of all functions being profiled")) | |
324 | (bindings--define-key prof-map [prof-restfunc] | |
325 | '(menu-item "Remove Instrumentation for Function..." elp-restore-function | |
326 | :help "Restore an instrumented function to its original definition")) | |
327 | ||
328 | (bindings--define-key prof-map [sep-rem] menu-bar-separator) | |
329 | (bindings--define-key prof-map [prof-resall] | |
330 | '(menu-item "Reset Counters for All Functions" elp-reset-all | |
331 | :help "Reset the profiling information for all functions being profiled")) | |
332 | (bindings--define-key prof-map [prof-resfunc] | |
333 | '(menu-item "Reset Counters for Function..." elp-reset-function | |
334 | :help "Reset the profiling information for a function")) | |
335 | (bindings--define-key prof-map [prof-res] | |
336 | '(menu-item "Show Profiling Results" elp-results | |
337 | :help "Display current profiling results")) | |
338 | (bindings--define-key prof-map [prof-pack] | |
339 | '(menu-item "Instrument Package..." elp-instrument-package | |
340 | :help "Instrument for profiling all function that start with a prefix")) | |
341 | (bindings--define-key prof-map [prof-func] | |
342 | '(menu-item "Instrument Function..." elp-instrument-function | |
343 | :help "Instrument a function for profiling")) | |
a576cc76 GM |
344 | ;; Maybe this should be in a separate submenu from the ELP stuff? |
345 | (bindings--define-key prof-map [sep-natprof] menu-bar-separator) | |
346 | (bindings--define-key prof-map [prof-natprof-stop] | |
347 | '(menu-item "Stop Native Profiler" profiler-stop | |
348 | :help "Stop recording profiling information" | |
349 | :enable (and (featurep 'profiler) | |
350 | (profiler-running-p)))) | |
351 | (bindings--define-key prof-map [prof-natprof-report] | |
352 | '(menu-item "Show Profiler Report" profiler-report | |
353 | :help "Show the current profiler report" | |
354 | :enable (and (featurep 'profiler) | |
355 | (profiler-running-p)))) | |
356 | (bindings--define-key prof-map [prof-natprof-start] | |
357 | '(menu-item "Start Native Profiler..." profiler-start | |
358 | :help "Start recording profiling information")) | |
359 | ||
1ec4b7b2 SM |
360 | (bindings--define-key menu-map [lint] (cons "Linting" lint-map)) |
361 | (bindings--define-key lint-map [lint-di] | |
362 | '(menu-item "Lint Directory..." elint-directory | |
363 | :help "Lint a directory")) | |
364 | (bindings--define-key lint-map [lint-f] | |
365 | '(menu-item "Lint File..." elint-file | |
366 | :help "Lint a file")) | |
367 | (bindings--define-key lint-map [lint-b] | |
368 | '(menu-item "Lint Buffer" elint-current-buffer | |
369 | :help "Lint the current buffer")) | |
370 | (bindings--define-key lint-map [lint-d] | |
371 | '(menu-item "Lint Defun" elint-defun | |
372 | :help "Lint the function at point")) | |
373 | (bindings--define-key menu-map [edebug-defun] | |
374 | '(menu-item "Instrument Function for Debugging" edebug-defun | |
375 | :help "Evaluate the top level form point is in, stepping through with Edebug" | |
376 | :keys "C-u C-M-x")) | |
377 | (bindings--define-key menu-map [separator-byte] menu-bar-separator) | |
378 | (bindings--define-key menu-map [disas] | |
379 | '(menu-item "Disassemble Byte Compiled Object..." disassemble | |
380 | :help "Print disassembled code for OBJECT in a buffer")) | |
381 | (bindings--define-key menu-map [byte-recompile] | |
382 | '(menu-item "Byte-recompile Directory..." byte-recompile-directory | |
383 | :help "Recompile every `.el' file in DIRECTORY that needs recompilation")) | |
384 | (bindings--define-key menu-map [emacs-byte-compile-and-load] | |
385 | '(menu-item "Byte-compile and Load" emacs-lisp-byte-compile-and-load | |
386 | :help "Byte-compile the current file (if it has changed), then load compiled code")) | |
387 | (bindings--define-key menu-map [byte-compile] | |
388 | '(menu-item "Byte-compile This File" emacs-lisp-byte-compile | |
389 | :help "Byte compile the file containing the current buffer")) | |
390 | (bindings--define-key menu-map [separator-eval] menu-bar-separator) | |
391 | (bindings--define-key menu-map [ielm] | |
392 | '(menu-item "Interactive Expression Evaluation" ielm | |
393 | :help "Interactively evaluate Emacs Lisp expressions")) | |
394 | (bindings--define-key menu-map [eval-buffer] | |
395 | '(menu-item "Evaluate Buffer" eval-buffer | |
396 | :help "Execute the current buffer as Lisp code")) | |
397 | (bindings--define-key menu-map [eval-region] | |
398 | '(menu-item "Evaluate Region" eval-region | |
399 | :help "Execute the region as Lisp code" | |
d6a3febd | 400 | :enable mark-active)) |
1ec4b7b2 SM |
401 | (bindings--define-key menu-map [eval-sexp] |
402 | '(menu-item "Evaluate Last S-expression" eval-last-sexp | |
32985194 | 403 | :help "Evaluate sexp before point; print value in echo area")) |
1ec4b7b2 SM |
404 | (bindings--define-key menu-map [separator-format] menu-bar-separator) |
405 | (bindings--define-key menu-map [comment-region] | |
406 | '(menu-item "Comment Out Region" comment-region | |
407 | :help "Comment or uncomment each line in the region" | |
d6a3febd | 408 | :enable mark-active)) |
1ec4b7b2 SM |
409 | (bindings--define-key menu-map [indent-region] |
410 | '(menu-item "Indent Region" indent-region | |
411 | :help "Indent each nonblank line in the region" | |
d6a3febd | 412 | :enable mark-active)) |
1ec4b7b2 SM |
413 | (bindings--define-key menu-map [indent-line] |
414 | '(menu-item "Indent Line" lisp-indent-line)) | |
d6a3febd MR |
415 | map) |
416 | "Keymap for Emacs Lisp mode. | |
417 | All commands in `lisp-mode-shared-map' are inherited by this map.") | |
418 | ||
419 | (defun emacs-lisp-byte-compile () | |
420 | "Byte compile the file containing the current buffer." | |
421 | (interactive) | |
422 | (if buffer-file-name | |
423 | (byte-compile-file buffer-file-name) | |
424 | (error "The buffer must be saved in a file first"))) | |
425 | ||
426 | (defun emacs-lisp-byte-compile-and-load () | |
427 | "Byte-compile the current file (if it has changed), then load compiled code." | |
428 | (interactive) | |
429 | (or buffer-file-name | |
430 | (error "The buffer must be saved in a file first")) | |
431 | (require 'bytecomp) | |
432 | ;; Recompile if file or buffer has changed since last compilation. | |
433 | (if (and (buffer-modified-p) | |
434 | (y-or-n-p (format "Save buffer %s first? " (buffer-name)))) | |
435 | (save-buffer)) | |
430e7297 | 436 | (byte-recompile-file buffer-file-name nil 0 t)) |
d6a3febd MR |
437 | |
438 | (defcustom emacs-lisp-mode-hook nil | |
439 | "Hook run when entering Emacs Lisp mode." | |
440 | :options '(turn-on-eldoc-mode imenu-add-menubar-index checkdoc-minor-mode) | |
441 | :type 'hook | |
442 | :group 'lisp) | |
443 | ||
444 | (defcustom lisp-mode-hook nil | |
445 | "Hook run when entering Lisp mode." | |
446 | :options '(imenu-add-menubar-index) | |
447 | :type 'hook | |
448 | :group 'lisp) | |
449 | ||
450 | (defcustom lisp-interaction-mode-hook nil | |
451 | "Hook run when entering Lisp Interaction mode." | |
452 | :options '(turn-on-eldoc-mode) | |
453 | :type 'hook | |
454 | :group 'lisp) | |
455 | ||
3ca0d0b4 TZ |
456 | (defconst lisp--prettify-symbols-alist |
457 | '(("lambda" . ?λ))) | |
458 | ||
10dcc561 | 459 | (define-derived-mode emacs-lisp-mode prog-mode "Emacs-Lisp" |
d6a3febd MR |
460 | "Major mode for editing Lisp code to run in Emacs. |
461 | Commands: | |
462 | Delete converts tabs to spaces as it moves back. | |
463 | Blank lines separate paragraphs. Semicolons start comments. | |
a3c40f60 | 464 | |
d6a3febd MR |
465 | \\{emacs-lisp-mode-map} |
466 | Entry to this mode calls the value of `emacs-lisp-mode-hook' | |
467 | if that value is non-nil." | |
51ef56c4 | 468 | :group 'lisp |
d6a3febd MR |
469 | (lisp-mode-variables) |
470 | (setq imenu-case-fold-search nil) | |
51ef56c4 SM |
471 | (add-hook 'completion-at-point-functions |
472 | 'lisp-completion-at-point nil 'local)) | |
d6a3febd | 473 | |
9b851e25 SM |
474 | ;;; Emacs Lisp Byte-Code mode |
475 | ||
476 | (eval-and-compile | |
477 | (defconst emacs-list-byte-code-comment-re | |
478 | (concat "\\(#\\)@\\([0-9]+\\) " | |
479 | ;; Make sure it's a docstring and not a lazy-loaded byte-code. | |
480 | "\\(?:[^(]\\|([^\"]\\)"))) | |
481 | ||
482 | (defun emacs-lisp-byte-code-comment (end &optional _point) | |
483 | "Try to syntactically mark the #@NNN ....^_ docstrings in byte-code files." | |
484 | (let ((ppss (syntax-ppss))) | |
485 | (when (and (nth 4 ppss) | |
486 | (eq (char-after (nth 8 ppss)) ?#)) | |
487 | (let* ((n (save-excursion | |
488 | (goto-char (nth 8 ppss)) | |
489 | (when (looking-at emacs-list-byte-code-comment-re) | |
490 | (string-to-number (match-string 2))))) | |
491 | ;; `maxdiff' tries to make sure the loop below terminates. | |
492 | (maxdiff n)) | |
493 | (when n | |
494 | (let* ((bchar (match-end 2)) | |
495 | (b (position-bytes bchar))) | |
496 | (goto-char (+ b n)) | |
497 | (while (let ((diff (- (position-bytes (point)) b n))) | |
498 | (unless (zerop diff) | |
499 | (when (> diff maxdiff) (setq diff maxdiff)) | |
500 | (forward-char (- diff)) | |
501 | (setq maxdiff (if (> diff 0) diff | |
502 | (max (1- maxdiff) 1))) | |
503 | t)))) | |
504 | (if (<= (point) end) | |
505 | (put-text-property (1- (point)) (point) | |
506 | 'syntax-table | |
507 | (string-to-syntax "> b")) | |
508 | (goto-char end))))))) | |
509 | ||
510 | (defun emacs-lisp-byte-code-syntax-propertize (start end) | |
511 | (emacs-lisp-byte-code-comment end (point)) | |
512 | (funcall | |
513 | (syntax-propertize-rules | |
514 | (emacs-list-byte-code-comment-re | |
515 | (1 (prog1 "< b" (emacs-lisp-byte-code-comment end (point)))))) | |
516 | start end)) | |
517 | ||
518 | (add-to-list 'auto-mode-alist '("\\.elc\\'" . emacs-lisp-byte-code-mode)) | |
519 | (define-derived-mode emacs-lisp-byte-code-mode emacs-lisp-mode | |
520 | "Elisp-Byte-Code" | |
521 | "Major mode for *.elc files." | |
522 | ;; TODO: Add way to disassemble byte-code under point. | |
523 | (setq-local open-paren-in-column-0-is-defun-start nil) | |
524 | (setq-local syntax-propertize-function | |
525 | #'emacs-lisp-byte-code-syntax-propertize)) | |
526 | ||
527 | ;;; Generic Lisp mode. | |
528 | ||
d6a3febd MR |
529 | (defvar lisp-mode-map |
530 | (let ((map (make-sparse-keymap)) | |
531 | (menu-map (make-sparse-keymap "Lisp"))) | |
532 | (set-keymap-parent map lisp-mode-shared-map) | |
533 | (define-key map "\e\C-x" 'lisp-eval-defun) | |
534 | (define-key map "\C-c\C-z" 'run-lisp) | |
1ec4b7b2 SM |
535 | (bindings--define-key map [menu-bar lisp] (cons "Lisp" menu-map)) |
536 | (bindings--define-key menu-map [run-lisp] | |
537 | '(menu-item "Run inferior Lisp" run-lisp | |
538 | :help "Run an inferior Lisp process, input and output via buffer `*inferior-lisp*'")) | |
539 | (bindings--define-key menu-map [ev-def] | |
540 | '(menu-item "Eval defun" lisp-eval-defun | |
541 | :help "Send the current defun to the Lisp process made by M-x run-lisp")) | |
542 | (bindings--define-key menu-map [ind-sexp] | |
543 | '(menu-item "Indent sexp" indent-sexp | |
544 | :help "Indent each line of the list starting just after point")) | |
d6a3febd MR |
545 | map) |
546 | "Keymap for ordinary Lisp mode. | |
547 | All commands in `lisp-mode-shared-map' are inherited by this map.") | |
548 | ||
10dcc561 | 549 | (define-derived-mode lisp-mode prog-mode "Lisp" |
d6a3febd MR |
550 | "Major mode for editing Lisp code for Lisps other than GNU Emacs Lisp. |
551 | Commands: | |
552 | Delete converts tabs to spaces as it moves back. | |
553 | Blank lines separate paragraphs. Semicolons start comments. | |
a3c40f60 | 554 | |
d6a3febd MR |
555 | \\{lisp-mode-map} |
556 | Note that `run-lisp' may be used either to start an inferior Lisp job | |
557 | or to switch back to an existing one. | |
558 | ||
559 | Entry to this mode calls the value of `lisp-mode-hook' | |
560 | if that value is non-nil." | |
913274cb | 561 | (lisp-mode-variables nil t) |
92eadba5 CY |
562 | (setq-local find-tag-default-function 'lisp-find-tag-default) |
563 | (setq-local comment-start-skip | |
564 | "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\)\\(;+\\|#|\\) *") | |
f3a47002 | 565 | (setq imenu-case-fold-search t)) |
d6a3febd MR |
566 | |
567 | (defun lisp-find-tag-default () | |
568 | (let ((default (find-tag-default))) | |
569 | (when (stringp default) | |
570 | (if (string-match ":+" default) | |
571 | (substring default (match-end 0)) | |
572 | default)))) | |
573 | ||
574 | ;; Used in old LispM code. | |
575 | (defalias 'common-lisp-mode 'lisp-mode) | |
576 | ||
577 | ;; This will do unless inf-lisp.el is loaded. | |
578 | (defun lisp-eval-defun (&optional and-go) | |
579 | "Send the current defun to the Lisp process made by \\[run-lisp]." | |
580 | (interactive) | |
581 | (error "Process lisp does not exist")) | |
582 | ||
583 | (defvar lisp-interaction-mode-map | |
584 | (let ((map (make-sparse-keymap)) | |
585 | (menu-map (make-sparse-keymap "Lisp-Interaction"))) | |
586 | (set-keymap-parent map lisp-mode-shared-map) | |
587 | (define-key map "\e\C-x" 'eval-defun) | |
588 | (define-key map "\e\C-q" 'indent-pp-sexp) | |
51ef56c4 | 589 | (define-key map "\e\t" 'completion-at-point) |
d6a3febd | 590 | (define-key map "\n" 'eval-print-last-sexp) |
1ec4b7b2 SM |
591 | (bindings--define-key map [menu-bar lisp-interaction] |
592 | (cons "Lisp-Interaction" menu-map)) | |
593 | (bindings--define-key menu-map [eval-defun] | |
594 | '(menu-item "Evaluate Defun" eval-defun | |
595 | :help "Evaluate the top-level form containing point, or after point")) | |
596 | (bindings--define-key menu-map [eval-print-last-sexp] | |
597 | '(menu-item "Evaluate and Print" eval-print-last-sexp | |
598 | :help "Evaluate sexp before point; print value into current buffer")) | |
599 | (bindings--define-key menu-map [edebug-defun-lisp-interaction] | |
600 | '(menu-item "Instrument Function for Debugging" edebug-defun | |
601 | :help "Evaluate the top level form point is in, stepping through with Edebug" | |
602 | :keys "C-u C-M-x")) | |
603 | (bindings--define-key menu-map [indent-pp-sexp] | |
604 | '(menu-item "Indent or Pretty-Print" indent-pp-sexp | |
605 | :help "Indent each line of the list starting just after point, or prettyprint it")) | |
606 | (bindings--define-key menu-map [complete-symbol] | |
607 | '(menu-item "Complete Lisp Symbol" completion-at-point | |
608 | :help "Perform completion on Lisp symbol preceding point")) | |
d6a3febd MR |
609 | map) |
610 | "Keymap for Lisp Interaction mode. | |
611 | All commands in `lisp-mode-shared-map' are inherited by this map.") | |
612 | ||
d6a3febd MR |
613 | (define-derived-mode lisp-interaction-mode emacs-lisp-mode "Lisp Interaction" |
614 | "Major mode for typing and evaluating Lisp forms. | |
615 | Like Lisp mode except that \\[eval-print-last-sexp] evals the Lisp expression | |
616 | before point, and prints its value into the buffer, advancing point. | |
617 | Note that printing is controlled by `eval-expression-print-length' | |
618 | and `eval-expression-print-level'. | |
619 | ||
620 | Commands: | |
621 | Delete converts tabs to spaces as it moves back. | |
622 | Paragraphs are separated only by blank lines. | |
623 | Semicolons start comments. | |
a3c40f60 | 624 | |
d6a3febd MR |
625 | \\{lisp-interaction-mode-map} |
626 | Entry to this mode calls the value of `lisp-interaction-mode-hook' | |
db174434 CY |
627 | if that value is non-nil." |
628 | :abbrev-table nil) | |
d6a3febd MR |
629 | |
630 | (defun eval-print-last-sexp () | |
631 | "Evaluate sexp before point; print value into current buffer. | |
632 | ||
633 | If `eval-expression-debug-on-error' is non-nil, which is the default, | |
634 | this command arranges for all errors to enter the debugger. | |
635 | ||
636 | Note that printing the result is controlled by the variables | |
637 | `eval-expression-print-length' and `eval-expression-print-level', | |
638 | which see." | |
639 | (interactive) | |
640 | (let ((standard-output (current-buffer))) | |
641 | (terpri) | |
642 | (eval-last-sexp t) | |
643 | (terpri))) | |
644 | ||
645 | ||
646 | (defun last-sexp-setup-props (beg end value alt1 alt2) | |
647 | "Set up text properties for the output of `eval-last-sexp-1'. | |
648 | BEG and END are the start and end of the output in current-buffer. | |
649 | VALUE is the Lisp value printed, ALT1 and ALT2 are strings for the | |
650 | alternative printed representations that can be displayed." | |
651 | (let ((map (make-sparse-keymap))) | |
652 | (define-key map "\C-m" 'last-sexp-toggle-display) | |
653 | (define-key map [down-mouse-2] 'mouse-set-point) | |
654 | (define-key map [mouse-2] 'last-sexp-toggle-display) | |
655 | (add-text-properties | |
656 | beg end | |
657 | `(printed-value (,value ,alt1 ,alt2) | |
658 | mouse-face highlight | |
659 | keymap ,map | |
660 | help-echo "RET, mouse-2: toggle abbreviated display" | |
661 | rear-nonsticky (mouse-face keymap help-echo | |
662 | printed-value))))) | |
663 | ||
664 | ||
665 | (defun last-sexp-toggle-display (&optional arg) | |
666 | "Toggle between abbreviated and unabbreviated printed representations." | |
667 | (interactive "P") | |
668 | (save-restriction | |
669 | (widen) | |
670 | (let ((value (get-text-property (point) 'printed-value))) | |
671 | (when value | |
672 | (let ((beg (or (previous-single-property-change (min (point-max) (1+ (point))) | |
673 | 'printed-value) | |
674 | (point))) | |
675 | (end (or (next-single-char-property-change (point) 'printed-value) (point))) | |
676 | (standard-output (current-buffer)) | |
677 | (point (point))) | |
678 | (delete-region beg end) | |
679 | (insert (nth 1 value)) | |
680 | (or (= beg point) | |
681 | (setq point (1- (point)))) | |
682 | (last-sexp-setup-props beg (point) | |
683 | (nth 0 value) | |
684 | (nth 2 value) | |
685 | (nth 1 value)) | |
686 | (goto-char (min (point-max) point))))))) | |
687 | ||
688 | (defun prin1-char (char) | |
689 | "Return a string representing CHAR as a character rather than as an integer. | |
690 | If CHAR is not a character, return nil." | |
691 | (and (integerp char) | |
692 | (eventp char) | |
693 | (let ((c (event-basic-type char)) | |
694 | (mods (event-modifiers char)) | |
695 | string) | |
696 | ;; Prevent ?A from turning into ?\S-a. | |
697 | (if (and (memq 'shift mods) | |
698 | (zerop (logand char ?\S-\^@)) | |
699 | (not (let ((case-fold-search nil)) | |
700 | (char-equal c (upcase c))))) | |
701 | (setq c (upcase c) mods nil)) | |
702 | ;; What string are we considering using? | |
703 | (condition-case nil | |
704 | (setq string | |
705 | (concat | |
706 | "?" | |
707 | (mapconcat | |
708 | (lambda (modif) | |
709 | (cond ((eq modif 'super) "\\s-") | |
710 | (t (string ?\\ (upcase (aref (symbol-name modif) 0)) ?-)))) | |
711 | mods "") | |
712 | (cond | |
713 | ((memq c '(?\; ?\( ?\) ?\{ ?\} ?\[ ?\] ?\" ?\' ?\\)) (string ?\\ c)) | |
714 | ((eq c 127) "\\C-?") | |
715 | (t | |
716 | (string c))))) | |
717 | (error nil)) | |
718 | ;; Verify the string reads a CHAR, not to some other character. | |
719 | ;; If it doesn't, return nil instead. | |
720 | (and string | |
721 | (= (car (read-from-string string)) char) | |
722 | string)))) | |
723 | ||
724 | ||
725 | (defun preceding-sexp () | |
726 | "Return sexp before the point." | |
727 | (let ((opoint (point)) | |
728 | ignore-quotes | |
729 | expr) | |
730 | (save-excursion | |
731 | (with-syntax-table emacs-lisp-mode-syntax-table | |
732 | ;; If this sexp appears to be enclosed in `...' | |
733 | ;; then ignore the surrounding quotes. | |
734 | (setq ignore-quotes | |
735 | (or (eq (following-char) ?\') | |
736 | (eq (preceding-char) ?\'))) | |
737 | (forward-sexp -1) | |
738 | ;; If we were after `?\e' (or similar case), | |
739 | ;; use the whole thing, not just the `e'. | |
740 | (when (eq (preceding-char) ?\\) | |
741 | (forward-char -1) | |
742 | (when (eq (preceding-char) ??) | |
743 | (forward-char -1))) | |
744 | ||
7346a407 CY |
745 | ;; Skip over hash table read syntax. |
746 | (and (> (point) (1+ (point-min))) | |
747 | (looking-back "#s" (- (point) 2)) | |
748 | (forward-char -2)) | |
749 | ||
d6a3febd MR |
750 | ;; Skip over `#N='s. |
751 | (when (eq (preceding-char) ?=) | |
752 | (let (labeled-p) | |
753 | (save-excursion | |
754 | (skip-chars-backward "0-9#=") | |
755 | (setq labeled-p (looking-at "\\(#[0-9]+=\\)+"))) | |
756 | (when labeled-p | |
757 | (forward-sexp -1)))) | |
758 | ||
759 | (save-restriction | |
760 | ;; vladimir@cs.ualberta.ca 30-Jul-1997: skip ` in | |
761 | ;; `variable' so that the value is returned, not the | |
762 | ;; name | |
763 | (if (and ignore-quotes | |
764 | (eq (following-char) ?`)) | |
765 | (forward-char)) | |
766 | (narrow-to-region (point-min) opoint) | |
767 | (setq expr (read (current-buffer))) | |
768 | ;; If it's an (interactive ...) form, it's more | |
769 | ;; useful to show how an interactive call would | |
770 | ;; use it. | |
771 | (and (consp expr) | |
772 | (eq (car expr) 'interactive) | |
773 | (setq expr | |
774 | (list 'call-interactively | |
775 | (list 'quote | |
776 | (list 'lambda | |
777 | '(&rest args) | |
778 | expr | |
779 | 'args))))) | |
780 | expr))))) | |
781 | ||
782 | ||
783 | (defun eval-last-sexp-1 (eval-last-sexp-arg-internal) | |
32985194 | 784 | "Evaluate sexp before point; print value in the echo area. |
d6a3febd | 785 | With argument, print output into current buffer." |
a0ee6f27 | 786 | (let ((standard-output (if eval-last-sexp-arg-internal (current-buffer) t))) |
b9598260 | 787 | ;; Setup the lexical environment if lexical-binding is enabled. |
06788a55 SM |
788 | (eval-last-sexp-print-value |
789 | (eval (eval-sexp-add-defvars (preceding-sexp)) lexical-binding)))) | |
d6a3febd MR |
790 | |
791 | ||
792 | (defun eval-last-sexp-print-value (value) | |
793 | (let ((unabbreviated (let ((print-length nil) (print-level nil)) | |
794 | (prin1-to-string value))) | |
795 | (print-length eval-expression-print-length) | |
796 | (print-level eval-expression-print-level) | |
797 | (beg (point)) | |
798 | end) | |
799 | (prog1 | |
800 | (prin1 value) | |
801 | (let ((str (eval-expression-print-format value))) | |
802 | (if str (princ str))) | |
803 | (setq end (point)) | |
804 | (when (and (bufferp standard-output) | |
805 | (or (not (null print-length)) | |
806 | (not (null print-level))) | |
807 | (not (string= unabbreviated | |
808 | (buffer-substring-no-properties beg end)))) | |
809 | (last-sexp-setup-props beg end value | |
810 | unabbreviated | |
811 | (buffer-substring-no-properties beg end)) | |
812 | )))) | |
813 | ||
814 | ||
815 | (defvar eval-last-sexp-fake-value (make-symbol "t")) | |
816 | ||
06788a55 SM |
817 | (defun eval-sexp-add-defvars (exp &optional pos) |
818 | "Prepend EXP with all the `defvar's that precede it in the buffer. | |
819 | POS specifies the starting position where EXP was found and defaults to point." | |
1f8fdd53 | 820 | (setq exp (macroexpand-all exp)) ;Eager macro-expansion. |
06788a55 SM |
821 | (if (not lexical-binding) |
822 | exp | |
823 | (save-excursion | |
824 | (unless pos (setq pos (point))) | |
825 | (let ((vars ())) | |
826 | (goto-char (point-min)) | |
827 | (while (re-search-forward | |
9b851e25 | 828 | "(def\\(?:var\\|const\\|custom\\)[ \t\n]+\\([^; '()\n\t]+\\)" |
06788a55 SM |
829 | pos t) |
830 | (let ((var (intern (match-string 1)))) | |
9b851e25 SM |
831 | (and (not (special-variable-p var)) |
832 | (save-excursion | |
833 | (zerop (car (syntax-ppss (match-beginning 0))))) | |
06788a55 SM |
834 | (push var vars)))) |
835 | `(progn ,@(mapcar (lambda (v) `(defvar ,v)) vars) ,exp))))) | |
7200d79c | 836 | |
d6a3febd | 837 | (defun eval-last-sexp (eval-last-sexp-arg-internal) |
32985194 | 838 | "Evaluate sexp before point; print value in the echo area. |
d6a3febd | 839 | Interactively, with prefix argument, print output into current buffer. |
16ac2e61 GM |
840 | Truncates long output according to the value of the variables |
841 | `eval-expression-print-length' and `eval-expression-print-level'. | |
d6a3febd MR |
842 | |
843 | If `eval-expression-debug-on-error' is non-nil, which is the default, | |
844 | this command arranges for all errors to enter the debugger." | |
845 | (interactive "P") | |
846 | (if (null eval-expression-debug-on-error) | |
847 | (eval-last-sexp-1 eval-last-sexp-arg-internal) | |
848 | (let ((value | |
849 | (let ((debug-on-error eval-last-sexp-fake-value)) | |
850 | (cons (eval-last-sexp-1 eval-last-sexp-arg-internal) | |
851 | debug-on-error)))) | |
852 | (unless (eq (cdr value) eval-last-sexp-fake-value) | |
853 | (setq debug-on-error (cdr value))) | |
854 | (car value)))) | |
855 | ||
856 | (defun eval-defun-1 (form) | |
857 | "Treat some expressions specially. | |
858 | Reset the `defvar' and `defcustom' variables to the initial value. | |
c6c08d3f | 859 | \(For `defcustom', use the :set function if there is one.) |
d6a3febd MR |
860 | Reinitialize the face according to the `defface' specification." |
861 | ;; The code in edebug-defun should be consistent with this, but not | |
53964682 | 862 | ;; the same, since this gets a macroexpanded form. |
d6a3febd MR |
863 | (cond ((not (listp form)) |
864 | form) | |
865 | ((and (eq (car form) 'defvar) | |
866 | (cdr-safe (cdr-safe form)) | |
867 | (boundp (cadr form))) | |
868 | ;; Force variable to be re-set. | |
869 | `(progn (defvar ,(nth 1 form) nil ,@(nthcdr 3 form)) | |
870 | (setq-default ,(nth 1 form) ,(nth 2 form)))) | |
871 | ;; `defcustom' is now macroexpanded to | |
872 | ;; `custom-declare-variable' with a quoted value arg. | |
873 | ((and (eq (car form) 'custom-declare-variable) | |
a0ee6f27 | 874 | (default-boundp (eval (nth 1 form) lexical-binding))) |
c6c08d3f GM |
875 | ;; Force variable to be bound, using :set function if specified. |
876 | (let ((setfunc (memq :set form))) | |
877 | (when setfunc | |
878 | (setq setfunc (car-safe (cdr-safe setfunc))) | |
879 | (or (functionp setfunc) (setq setfunc nil))) | |
880 | (funcall (or setfunc 'set-default) | |
881 | (eval (nth 1 form) lexical-binding) | |
882 | ;; The second arg is an expression that evaluates to | |
883 | ;; an expression. The second evaluation is the one | |
884 | ;; normally performed not by normal execution but by | |
885 | ;; custom-initialize-set (for example), which does not | |
886 | ;; use lexical-binding. | |
887 | (eval (eval (nth 2 form) lexical-binding)))) | |
d6a3febd MR |
888 | form) |
889 | ;; `defface' is macroexpanded to `custom-declare-face'. | |
890 | ((eq (car form) 'custom-declare-face) | |
891 | ;; Reset the face. | |
f4f73198 LMI |
892 | (let ((face-symbol (eval (nth 1 form) lexical-binding))) |
893 | (setq face-new-frame-defaults | |
894 | (assq-delete-all face-symbol face-new-frame-defaults)) | |
895 | (put face-symbol 'face-defface-spec nil) | |
1c4f115d CY |
896 | (put face-symbol 'face-override-spec nil)) |
897 | form) | |
d6a3febd MR |
898 | ((eq (car form) 'progn) |
899 | (cons 'progn (mapcar 'eval-defun-1 (cdr form)))) | |
900 | (t form))) | |
901 | ||
902 | (defun eval-defun-2 () | |
903 | "Evaluate defun that point is in or before. | |
32985194 | 904 | The value is displayed in the echo area. |
d6a3febd MR |
905 | If the current defun is actually a call to `defvar', |
906 | then reset the variable using the initial value expression | |
907 | even if the variable already has some other value. | |
908 | \(Normally `defvar' does not change the variable's value | |
909 | if it already has a value.\) | |
910 | ||
d6a3febd | 911 | Return the result of evaluation." |
d6a3febd MR |
912 | ;; FIXME: the print-length/level bindings should only be applied while |
913 | ;; printing, not while evaluating. | |
914 | (let ((debug-on-error eval-expression-debug-on-error) | |
915 | (print-length eval-expression-print-length) | |
916 | (print-level eval-expression-print-level)) | |
917 | (save-excursion | |
918 | ;; Arrange for eval-region to "read" the (possibly) altered form. | |
919 | ;; eval-region handles recording which file defines a function or | |
920 | ;; variable. Re-written using `apply' to avoid capturing | |
921 | ;; variables like `end'. | |
922 | (apply | |
923 | #'eval-region | |
924 | (let ((standard-output t) | |
925 | beg end form) | |
926 | ;; Read the form from the buffer, and record where it ends. | |
927 | (save-excursion | |
928 | (end-of-defun) | |
929 | (beginning-of-defun) | |
930 | (setq beg (point)) | |
e4d4f539 | 931 | (setq form (read (current-buffer))) |
d6a3febd MR |
932 | (setq end (point))) |
933 | ;; Alter the form if necessary. | |
e4d4f539 | 934 | (setq form (eval-sexp-add-defvars (eval-defun-1 (macroexpand form)))) |
d6a3febd MR |
935 | (list beg end standard-output |
936 | `(lambda (ignore) | |
937 | ;; Skipping to the end of the specified region | |
938 | ;; will make eval-region return. | |
939 | (goto-char ,end) | |
940 | ',form)))))) | |
941 | ;; The result of evaluation has been put onto VALUES. So return it. | |
942 | (car values)) | |
943 | ||
944 | (defun eval-defun (edebug-it) | |
945 | "Evaluate the top-level form containing point, or after point. | |
946 | ||
947 | If the current defun is actually a call to `defvar' or `defcustom', | |
948 | evaluating it this way resets the variable using its initial value | |
c6c08d3f GM |
949 | expression (using the defcustom's :set function if there is one), even |
950 | if the variable already has some other value. \(Normally `defvar' and | |
951 | `defcustom' do not alter the value if there already is one.) In an | |
952 | analogous way, evaluating a `defface' overrides any customizations of | |
953 | the face, so that it becomes defined exactly as the `defface' expression | |
954 | says. | |
d6a3febd MR |
955 | |
956 | If `eval-expression-debug-on-error' is non-nil, which is the default, | |
957 | this command arranges for all errors to enter the debugger. | |
958 | ||
959 | With a prefix argument, instrument the code for Edebug. | |
960 | ||
961 | If acting on a `defun' for FUNCTION, and the function was | |
32985194 | 962 | instrumented, `Edebug: FUNCTION' is printed in the echo area. If not |
d6a3febd MR |
963 | instrumented, just FUNCTION is printed. |
964 | ||
965 | If not acting on a `defun', the result of evaluation is displayed in | |
32985194 | 966 | the echo area. This display is controlled by the variables |
d6a3febd MR |
967 | `eval-expression-print-length' and `eval-expression-print-level', |
968 | which see." | |
969 | (interactive "P") | |
970 | (cond (edebug-it | |
971 | (require 'edebug) | |
972 | (eval-defun (not edebug-all-defs))) | |
973 | (t | |
974 | (if (null eval-expression-debug-on-error) | |
975 | (eval-defun-2) | |
976 | (let ((old-value (make-symbol "t")) new-value value) | |
977 | (let ((debug-on-error old-value)) | |
978 | (setq value (eval-defun-2)) | |
979 | (setq new-value debug-on-error)) | |
980 | (unless (eq old-value new-value) | |
981 | (setq debug-on-error new-value)) | |
982 | value))))) | |
983 | ||
984 | ;; May still be used by some external Lisp-mode variant. | |
985 | (define-obsolete-function-alias 'lisp-comment-indent | |
986 | 'comment-indent-default "22.1") | |
987 | (define-obsolete-function-alias 'lisp-mode-auto-fill 'do-auto-fill "23.1") | |
988 | ||
989 | (defcustom lisp-indent-offset nil | |
990 | "If non-nil, indent second line of expressions that many more columns." | |
991 | :group 'lisp | |
992 | :type '(choice (const nil) integer)) | |
3fde45af | 993 | (put 'lisp-indent-offset 'safe-local-variable |
d6a3febd MR |
994 | (lambda (x) (or (null x) (integerp x)))) |
995 | ||
3fde45af | 996 | (defcustom lisp-indent-function 'lisp-indent-function |
c4ea8f00 GM |
997 | "A function to be called by `calculate-lisp-indent'. |
998 | It indents the arguments of a Lisp function call. This function | |
999 | should accept two arguments: the indent-point, and the | |
1000 | `parse-partial-sexp' state at that position. One option for this | |
3fde45af GM |
1001 | function is `common-lisp-indent-function'." |
1002 | :type 'function | |
1003 | :group 'lisp) | |
d6a3febd MR |
1004 | |
1005 | (defun lisp-indent-line (&optional whole-exp) | |
1006 | "Indent current line as Lisp code. | |
1007 | With argument, indent any additional lines of the same expression | |
1008 | rigidly along with this one." | |
1009 | (interactive "P") | |
1010 | (let ((indent (calculate-lisp-indent)) shift-amt end | |
1011 | (pos (- (point-max) (point))) | |
1012 | (beg (progn (beginning-of-line) (point)))) | |
1013 | (skip-chars-forward " \t") | |
1014 | (if (or (null indent) (looking-at "\\s<\\s<\\s<")) | |
1015 | ;; Don't alter indentation of a ;;; comment line | |
1016 | ;; or a line that starts in a string. | |
9b851e25 | 1017 | ;; FIXME: inconsistency: comment-indent moves ;;; to column 0. |
d6a3febd MR |
1018 | (goto-char (- (point-max) pos)) |
1019 | (if (and (looking-at "\\s<") (not (looking-at "\\s<\\s<"))) | |
1020 | ;; Single-semicolon comment lines should be indented | |
1021 | ;; as comment lines, not as code. | |
1022 | (progn (indent-for-comment) (forward-char -1)) | |
1023 | (if (listp indent) (setq indent (car indent))) | |
1024 | (setq shift-amt (- indent (current-column))) | |
1025 | (if (zerop shift-amt) | |
1026 | nil | |
1027 | (delete-region beg (point)) | |
1028 | (indent-to indent))) | |
1029 | ;; If initial point was within line's indentation, | |
1030 | ;; position after the indentation. Else stay at same point in text. | |
1031 | (if (> (- (point-max) pos) (point)) | |
9b851e25 | 1032 | (goto-char (- (point-max) pos)))))) |
d6a3febd MR |
1033 | |
1034 | (defvar calculate-lisp-indent-last-sexp) | |
1035 | ||
1036 | (defun calculate-lisp-indent (&optional parse-start) | |
1037 | "Return appropriate indentation for current line as Lisp code. | |
1038 | In usual case returns an integer: the column to indent to. | |
1039 | If the value is nil, that means don't change the indentation | |
1040 | because the line starts inside a string. | |
1041 | ||
1042 | The value can also be a list of the form (COLUMN CONTAINING-SEXP-START). | |
1043 | This means that following lines at the same level of indentation | |
1044 | should not necessarily be indented the same as this line. | |
1045 | Then COLUMN is the column to indent to, and CONTAINING-SEXP-START | |
1046 | is the buffer position of the start of the containing expression." | |
1047 | (save-excursion | |
1048 | (beginning-of-line) | |
1049 | (let ((indent-point (point)) | |
1050 | state paren-depth | |
1051 | ;; setting this to a number inhibits calling hook | |
1052 | (desired-indent nil) | |
1053 | (retry t) | |
1054 | calculate-lisp-indent-last-sexp containing-sexp) | |
1055 | (if parse-start | |
1056 | (goto-char parse-start) | |
1057 | (beginning-of-defun)) | |
1058 | ;; Find outermost containing sexp | |
1059 | (while (< (point) indent-point) | |
1060 | (setq state (parse-partial-sexp (point) indent-point 0))) | |
1061 | ;; Find innermost containing sexp | |
1062 | (while (and retry | |
1063 | state | |
1064 | (> (setq paren-depth (elt state 0)) 0)) | |
1065 | (setq retry nil) | |
1066 | (setq calculate-lisp-indent-last-sexp (elt state 2)) | |
1067 | (setq containing-sexp (elt state 1)) | |
1068 | ;; Position following last unclosed open. | |
1069 | (goto-char (1+ containing-sexp)) | |
1070 | ;; Is there a complete sexp since then? | |
1071 | (if (and calculate-lisp-indent-last-sexp | |
1072 | (> calculate-lisp-indent-last-sexp (point))) | |
1073 | ;; Yes, but is there a containing sexp after that? | |
1074 | (let ((peek (parse-partial-sexp calculate-lisp-indent-last-sexp | |
1075 | indent-point 0))) | |
1076 | (if (setq retry (car (cdr peek))) (setq state peek))))) | |
1077 | (if retry | |
1078 | nil | |
1079 | ;; Innermost containing sexp found | |
1080 | (goto-char (1+ containing-sexp)) | |
1081 | (if (not calculate-lisp-indent-last-sexp) | |
1082 | ;; indent-point immediately follows open paren. | |
1083 | ;; Don't call hook. | |
1084 | (setq desired-indent (current-column)) | |
1085 | ;; Find the start of first element of containing sexp. | |
1086 | (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t) | |
1087 | (cond ((looking-at "\\s(") | |
1088 | ;; First element of containing sexp is a list. | |
1089 | ;; Indent under that list. | |
1090 | ) | |
1091 | ((> (save-excursion (forward-line 1) (point)) | |
1092 | calculate-lisp-indent-last-sexp) | |
1093 | ;; This is the first line to start within the containing sexp. | |
1094 | ;; It's almost certainly a function call. | |
1095 | (if (= (point) calculate-lisp-indent-last-sexp) | |
1096 | ;; Containing sexp has nothing before this line | |
1097 | ;; except the first element. Indent under that element. | |
1098 | nil | |
1099 | ;; Skip the first element, find start of second (the first | |
1100 | ;; argument of the function call) and indent under. | |
1101 | (progn (forward-sexp 1) | |
1102 | (parse-partial-sexp (point) | |
1103 | calculate-lisp-indent-last-sexp | |
1104 | 0 t))) | |
1105 | (backward-prefix-chars)) | |
1106 | (t | |
1107 | ;; Indent beneath first sexp on same line as | |
1108 | ;; `calculate-lisp-indent-last-sexp'. Again, it's | |
1109 | ;; almost certainly a function call. | |
1110 | (goto-char calculate-lisp-indent-last-sexp) | |
1111 | (beginning-of-line) | |
1112 | (parse-partial-sexp (point) calculate-lisp-indent-last-sexp | |
1113 | 0 t) | |
1114 | (backward-prefix-chars))))) | |
1115 | ;; Point is at the point to indent under unless we are inside a string. | |
1116 | ;; Call indentation hook except when overridden by lisp-indent-offset | |
1117 | ;; or if the desired indentation has already been computed. | |
1118 | (let ((normal-indent (current-column))) | |
1119 | (cond ((elt state 3) | |
1120 | ;; Inside a string, don't change indentation. | |
1121 | nil) | |
1122 | ((and (integerp lisp-indent-offset) containing-sexp) | |
1123 | ;; Indent by constant offset | |
1124 | (goto-char containing-sexp) | |
1125 | (+ (current-column) lisp-indent-offset)) | |
1126 | ;; in this case calculate-lisp-indent-last-sexp is not nil | |
1127 | (calculate-lisp-indent-last-sexp | |
1128 | (or | |
1129 | ;; try to align the parameters of a known function | |
1130 | (and lisp-indent-function | |
1131 | (not retry) | |
1132 | (funcall lisp-indent-function indent-point state)) | |
1133 | ;; If the function has no special alignment | |
1134 | ;; or it does not apply to this argument, | |
1135 | ;; try to align a constant-symbol under the last | |
1136 | ;; preceding constant symbol, if there is such one of | |
1137 | ;; the last 2 preceding symbols, in the previous | |
1138 | ;; uncommented line. | |
1139 | (and (save-excursion | |
1140 | (goto-char indent-point) | |
1141 | (skip-chars-forward " \t") | |
1142 | (looking-at ":")) | |
1143 | ;; The last sexp may not be at the indentation | |
1144 | ;; where it begins, so find that one, instead. | |
1145 | (save-excursion | |
1146 | (goto-char calculate-lisp-indent-last-sexp) | |
1147 | ;; Handle prefix characters and whitespace | |
1148 | ;; following an open paren. (Bug#1012) | |
1149 | (backward-prefix-chars) | |
1150 | (while (and (not (looking-back "^[ \t]*\\|([ \t]+")) | |
1151 | (or (not containing-sexp) | |
1152 | (< (1+ containing-sexp) (point)))) | |
1153 | (forward-sexp -1) | |
1154 | (backward-prefix-chars)) | |
1155 | (setq calculate-lisp-indent-last-sexp (point))) | |
1156 | (> calculate-lisp-indent-last-sexp | |
1157 | (save-excursion | |
1158 | (goto-char (1+ containing-sexp)) | |
1159 | (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t) | |
1160 | (point))) | |
1161 | (let ((parse-sexp-ignore-comments t) | |
1162 | indent) | |
1163 | (goto-char calculate-lisp-indent-last-sexp) | |
1164 | (or (and (looking-at ":") | |
1165 | (setq indent (current-column))) | |
5ed619e0 | 1166 | (and (< (line-beginning-position) |
d6a3febd MR |
1167 | (prog2 (backward-sexp) (point))) |
1168 | (looking-at ":") | |
1169 | (setq indent (current-column)))) | |
1170 | indent)) | |
1171 | ;; another symbols or constants not preceded by a constant | |
1172 | ;; as defined above. | |
1173 | normal-indent)) | |
1174 | ;; in this case calculate-lisp-indent-last-sexp is nil | |
1175 | (desired-indent) | |
1176 | (t | |
1177 | normal-indent)))))) | |
1178 | ||
1179 | (defun lisp-indent-function (indent-point state) | |
1180 | "This function is the normal value of the variable `lisp-indent-function'. | |
b7556719 GM |
1181 | The function `calculate-lisp-indent' calls this to determine |
1182 | if the arguments of a Lisp function call should be indented specially. | |
d6a3febd | 1183 | |
f8a42ad6 | 1184 | INDENT-POINT is the position at which the line being indented begins. |
d6a3febd MR |
1185 | Point is located at the point to indent under (for default indentation); |
1186 | STATE is the `parse-partial-sexp' state for that position. | |
1187 | ||
b7556719 GM |
1188 | If the current line is in a call to a Lisp function that has a non-nil |
1189 | property `lisp-indent-function' (or the deprecated `lisp-indent-hook'), | |
1190 | it specifies how to indent. The property value can be: | |
1191 | ||
1192 | * `defun', meaning indent `defun'-style | |
1193 | \(this is also the case if there is no property and the function | |
1194 | has a name that begins with \"def\", and three or more arguments); | |
1195 | ||
d6a3febd | 1196 | * an integer N, meaning indent the first N arguments specially |
b7556719 | 1197 | (like ordinary function arguments), and then indent any further |
d6a3febd | 1198 | arguments like a body; |
d6a3febd | 1199 | |
b7556719 GM |
1200 | * a function to call that returns the indentation (or nil). |
1201 | `lisp-indent-function' calls this function with the same two arguments | |
1202 | that it itself received. | |
1203 | ||
1204 | This function returns either the indentation to use, or nil if the | |
1205 | Lisp function does not specify a special indentation." | |
d6a3febd MR |
1206 | (let ((normal-indent (current-column))) |
1207 | (goto-char (1+ (elt state 1))) | |
1208 | (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t) | |
1209 | (if (and (elt state 2) | |
1210 | (not (looking-at "\\sw\\|\\s_"))) | |
1211 | ;; car of form doesn't seem to be a symbol | |
1212 | (progn | |
1213 | (if (not (> (save-excursion (forward-line 1) (point)) | |
1214 | calculate-lisp-indent-last-sexp)) | |
1215 | (progn (goto-char calculate-lisp-indent-last-sexp) | |
1216 | (beginning-of-line) | |
1217 | (parse-partial-sexp (point) | |
1218 | calculate-lisp-indent-last-sexp 0 t))) | |
1219 | ;; Indent under the list or under the first sexp on the same | |
1220 | ;; line as calculate-lisp-indent-last-sexp. Note that first | |
1221 | ;; thing on that line has to be complete sexp since we are | |
1222 | ;; inside the innermost containing sexp. | |
1223 | (backward-prefix-chars) | |
1224 | (current-column)) | |
1225 | (let ((function (buffer-substring (point) | |
1226 | (progn (forward-sexp 1) (point)))) | |
1227 | method) | |
7abaf5cc SM |
1228 | (setq method (or (function-get (intern-soft function) |
1229 | 'lisp-indent-function) | |
d6a3febd MR |
1230 | (get (intern-soft function) 'lisp-indent-hook))) |
1231 | (cond ((or (eq method 'defun) | |
1232 | (and (null method) | |
1233 | (> (length function) 3) | |
1234 | (string-match "\\`def" function))) | |
1235 | (lisp-indent-defform state indent-point)) | |
1236 | ((integerp method) | |
1237 | (lisp-indent-specform method state | |
1238 | indent-point normal-indent)) | |
1239 | (method | |
1240 | (funcall method indent-point state))))))) | |
1241 | ||
1242 | (defcustom lisp-body-indent 2 | |
1243 | "Number of columns to indent the second line of a `(def...)' form." | |
1244 | :group 'lisp | |
1245 | :type 'integer) | |
1246 | (put 'lisp-body-indent 'safe-local-variable 'integerp) | |
1247 | ||
1248 | (defun lisp-indent-specform (count state indent-point normal-indent) | |
1249 | (let ((containing-form-start (elt state 1)) | |
1250 | (i count) | |
1251 | body-indent containing-form-column) | |
1252 | ;; Move to the start of containing form, calculate indentation | |
1253 | ;; to use for non-distinguished forms (> count), and move past the | |
1254 | ;; function symbol. lisp-indent-function guarantees that there is at | |
1255 | ;; least one word or symbol character following open paren of containing | |
1256 | ;; form. | |
1257 | (goto-char containing-form-start) | |
1258 | (setq containing-form-column (current-column)) | |
1259 | (setq body-indent (+ lisp-body-indent containing-form-column)) | |
1260 | (forward-char 1) | |
1261 | (forward-sexp 1) | |
1262 | ;; Now find the start of the last form. | |
1263 | (parse-partial-sexp (point) indent-point 1 t) | |
1264 | (while (and (< (point) indent-point) | |
1265 | (condition-case () | |
1266 | (progn | |
1267 | (setq count (1- count)) | |
1268 | (forward-sexp 1) | |
1269 | (parse-partial-sexp (point) indent-point 1 t)) | |
1270 | (error nil)))) | |
1271 | ;; Point is sitting on first character of last (or count) sexp. | |
1272 | (if (> count 0) | |
1273 | ;; A distinguished form. If it is the first or second form use double | |
1274 | ;; lisp-body-indent, else normal indent. With lisp-body-indent bound | |
1275 | ;; to 2 (the default), this just happens to work the same with if as | |
1276 | ;; the older code, but it makes unwind-protect, condition-case, | |
1277 | ;; with-output-to-temp-buffer, et. al. much more tasteful. The older, | |
1278 | ;; less hacked, behavior can be obtained by replacing below with | |
1279 | ;; (list normal-indent containing-form-start). | |
1280 | (if (<= (- i count) 1) | |
1281 | (list (+ containing-form-column (* 2 lisp-body-indent)) | |
1282 | containing-form-start) | |
1283 | (list normal-indent containing-form-start)) | |
1284 | ;; A non-distinguished form. Use body-indent if there are no | |
1285 | ;; distinguished forms and this is the first undistinguished form, | |
1286 | ;; or if this is the first undistinguished form and the preceding | |
1287 | ;; distinguished form has indentation at least as great as body-indent. | |
1288 | (if (or (and (= i 0) (= count 0)) | |
1289 | (and (= count 0) (<= body-indent normal-indent))) | |
1290 | body-indent | |
1291 | normal-indent)))) | |
1292 | ||
1293 | (defun lisp-indent-defform (state indent-point) | |
1294 | (goto-char (car (cdr state))) | |
1295 | (forward-line 1) | |
1296 | (if (> (point) (car (cdr (cdr state)))) | |
1297 | (progn | |
1298 | (goto-char (car (cdr state))) | |
1299 | (+ lisp-body-indent (current-column))))) | |
1300 | ||
1301 | ||
1302 | ;; (put 'progn 'lisp-indent-function 0), say, causes progn to be indented | |
1303 | ;; like defun if the first form is placed on the next line, otherwise | |
1304 | ;; it is indented like any other form (i.e. forms line up under first). | |
1305 | ||
d6a3febd MR |
1306 | (put 'autoload 'lisp-indent-function 'defun) |
1307 | (put 'progn 'lisp-indent-function 0) | |
1308 | (put 'prog1 'lisp-indent-function 1) | |
1309 | (put 'prog2 'lisp-indent-function 2) | |
1310 | (put 'save-excursion 'lisp-indent-function 0) | |
d6a3febd | 1311 | (put 'save-restriction 'lisp-indent-function 0) |
d6a3febd | 1312 | (put 'save-current-buffer 'lisp-indent-function 0) |
d6a3febd MR |
1313 | (put 'let 'lisp-indent-function 1) |
1314 | (put 'let* 'lisp-indent-function 1) | |
1315 | (put 'while 'lisp-indent-function 1) | |
1316 | (put 'if 'lisp-indent-function 2) | |
d6a3febd MR |
1317 | (put 'catch 'lisp-indent-function 1) |
1318 | (put 'condition-case 'lisp-indent-function 2) | |
1319 | (put 'unwind-protect 'lisp-indent-function 1) | |
1320 | (put 'with-output-to-temp-buffer 'lisp-indent-function 1) | |
d6a3febd MR |
1321 | |
1322 | (defun indent-sexp (&optional endpos) | |
1323 | "Indent each line of the list starting just after point. | |
1324 | If optional arg ENDPOS is given, indent each line, stopping when | |
1325 | ENDPOS is encountered." | |
1326 | (interactive) | |
1327 | (let ((indent-stack (list nil)) | |
1328 | (next-depth 0) | |
1329 | ;; If ENDPOS is non-nil, use nil as STARTING-POINT | |
1330 | ;; so that calculate-lisp-indent will find the beginning of | |
1331 | ;; the defun we are in. | |
1332 | ;; If ENDPOS is nil, it is safe not to scan before point | |
1333 | ;; since every line we indent is more deeply nested than point is. | |
1334 | (starting-point (if endpos nil (point))) | |
1335 | (last-point (point)) | |
1336 | last-depth bol outer-loop-done inner-loop-done state this-indent) | |
1337 | (or endpos | |
1338 | ;; Get error now if we don't have a complete sexp after point. | |
1339 | (save-excursion (forward-sexp 1))) | |
1340 | (save-excursion | |
1341 | (setq outer-loop-done nil) | |
1342 | (while (if endpos (< (point) endpos) | |
1343 | (not outer-loop-done)) | |
1344 | (setq last-depth next-depth | |
1345 | inner-loop-done nil) | |
1346 | ;; Parse this line so we can learn the state | |
1347 | ;; to indent the next line. | |
1348 | ;; This inner loop goes through only once | |
1349 | ;; unless a line ends inside a string. | |
1350 | (while (and (not inner-loop-done) | |
1351 | (not (setq outer-loop-done (eobp)))) | |
1352 | (setq state (parse-partial-sexp (point) (progn (end-of-line) (point)) | |
1353 | nil nil state)) | |
1354 | (setq next-depth (car state)) | |
1355 | ;; If the line contains a comment other than the sort | |
1356 | ;; that is indented like code, | |
1357 | ;; indent it now with indent-for-comment. | |
1358 | ;; Comments indented like code are right already. | |
1359 | ;; In any case clear the in-comment flag in the state | |
1360 | ;; because parse-partial-sexp never sees the newlines. | |
1361 | (if (car (nthcdr 4 state)) | |
1362 | (progn (indent-for-comment) | |
1363 | (end-of-line) | |
1364 | (setcar (nthcdr 4 state) nil))) | |
1365 | ;; If this line ends inside a string, | |
1366 | ;; go straight to next line, remaining within the inner loop, | |
1367 | ;; and turn off the \-flag. | |
1368 | (if (car (nthcdr 3 state)) | |
1369 | (progn | |
1370 | (forward-line 1) | |
1371 | (setcar (nthcdr 5 state) nil)) | |
1372 | (setq inner-loop-done t))) | |
1373 | (and endpos | |
1374 | (<= next-depth 0) | |
1375 | (progn | |
1376 | (setq indent-stack (nconc indent-stack | |
1377 | (make-list (- next-depth) nil)) | |
1378 | last-depth (- last-depth next-depth) | |
1379 | next-depth 0))) | |
1380 | (forward-line 1) | |
1381 | ;; Decide whether to exit. | |
1382 | (if endpos | |
1383 | ;; If we have already reached the specified end, | |
1384 | ;; give up and do not reindent this line. | |
1385 | (if (<= endpos (point)) | |
1386 | (setq outer-loop-done t)) | |
1387 | ;; If no specified end, we are done if we have finished one sexp. | |
1388 | (if (<= next-depth 0) | |
1389 | (setq outer-loop-done t))) | |
1390 | (unless outer-loop-done | |
1391 | (while (> last-depth next-depth) | |
1392 | (setq indent-stack (cdr indent-stack) | |
1393 | last-depth (1- last-depth))) | |
1394 | (while (< last-depth next-depth) | |
1395 | (setq indent-stack (cons nil indent-stack) | |
1396 | last-depth (1+ last-depth))) | |
1397 | ;; Now indent the next line according | |
1398 | ;; to what we learned from parsing the previous one. | |
1399 | (setq bol (point)) | |
1400 | (skip-chars-forward " \t") | |
1401 | ;; But not if the line is blank, or just a comment | |
1402 | ;; (except for double-semi comments; indent them as usual). | |
1403 | (if (or (eobp) (looking-at "\\s<\\|\n")) | |
1404 | nil | |
1405 | (if (and (car indent-stack) | |
1406 | (>= (car indent-stack) 0)) | |
1407 | (setq this-indent (car indent-stack)) | |
1408 | (let ((val (calculate-lisp-indent | |
1409 | (if (car indent-stack) (- (car indent-stack)) | |
1410 | starting-point)))) | |
1411 | (if (null val) | |
1412 | (setq this-indent val) | |
1413 | (if (integerp val) | |
1414 | (setcar indent-stack | |
1415 | (setq this-indent val)) | |
1416 | (setcar indent-stack (- (car (cdr val)))) | |
1417 | (setq this-indent (car val)))))) | |
1418 | (if (and this-indent (/= (current-column) this-indent)) | |
1419 | (progn (delete-region bol (point)) | |
1420 | (indent-to this-indent))))) | |
1421 | (or outer-loop-done | |
1422 | (setq outer-loop-done (= (point) last-point)) | |
1423 | (setq last-point (point))))))) | |
1424 | ||
d6a3febd MR |
1425 | (defun indent-pp-sexp (&optional arg) |
1426 | "Indent each line of the list starting just after point, or prettyprint it. | |
1427 | A prefix argument specifies pretty-printing." | |
1428 | (interactive "P") | |
1429 | (if arg | |
1430 | (save-excursion | |
1431 | (save-restriction | |
1432 | (narrow-to-region (point) (progn (forward-sexp 1) (point))) | |
1433 | (pp-buffer) | |
1434 | (goto-char (point-max)) | |
1435 | (if (eq (char-before) ?\n) | |
1436 | (delete-char -1))))) | |
1437 | (indent-sexp)) | |
1438 | ||
1439 | ;;;; Lisp paragraph filling commands. | |
1440 | ||
1441 | (defcustom emacs-lisp-docstring-fill-column 65 | |
1442 | "Value of `fill-column' to use when filling a docstring. | |
1443 | Any non-integer value means do not use a different value of | |
1444 | `fill-column' when filling docstrings." | |
1445 | :type '(choice (integer) | |
1446 | (const :tag "Use the current `fill-column'" t)) | |
1447 | :group 'lisp) | |
bde73d27 DG |
1448 | (put 'emacs-lisp-docstring-fill-column 'safe-local-variable |
1449 | (lambda (x) (or (eq x t) (integerp x)))) | |
d6a3febd MR |
1450 | |
1451 | (defun lisp-fill-paragraph (&optional justify) | |
1452 | "Like \\[fill-paragraph], but handle Emacs Lisp comments and docstrings. | |
1453 | If any of the current line is a comment, fill the comment or the | |
1454 | paragraph of it that point is in, preserving the comment's indentation | |
1455 | and initial semicolons." | |
1456 | (interactive "P") | |
1457 | (or (fill-comment-paragraph justify) | |
1458 | ;; Since fill-comment-paragraph returned nil, that means we're not in | |
1459 | ;; a comment: Point is on a program line; we are interested | |
1460 | ;; particularly in docstring lines. | |
1461 | ;; | |
1462 | ;; We bind `paragraph-start' and `paragraph-separate' temporarily. They | |
1463 | ;; are buffer-local, but we avoid changing them so that they can be set | |
1464 | ;; to make `forward-paragraph' and friends do something the user wants. | |
1465 | ;; | |
1466 | ;; `paragraph-start': The `(' in the character alternative and the | |
1467 | ;; left-singlequote plus `(' sequence after the \\| alternative prevent | |
1468 | ;; sexps and backquoted sexps that follow a docstring from being filled | |
1469 | ;; with the docstring. This setting has the consequence of inhibiting | |
1470 | ;; filling many program lines that are not docstrings, which is sensible, | |
1471 | ;; because the user probably asked to fill program lines by accident, or | |
1472 | ;; expecting indentation (perhaps we should try to do indenting in that | |
1473 | ;; case). The `;' and `:' stop the paragraph being filled at following | |
1474 | ;; comment lines and at keywords (e.g., in `defcustom'). Left parens are | |
1475 | ;; escaped to keep font-locking, filling, & paren matching in the source | |
1476 | ;; file happy. | |
1477 | ;; | |
1478 | ;; `paragraph-separate': A clever regexp distinguishes the first line of | |
1479 | ;; a docstring and identifies it as a paragraph separator, so that it | |
1480 | ;; won't be filled. (Since the first line of documentation stands alone | |
1481 | ;; in some contexts, filling should not alter the contents the author has | |
1482 | ;; chosen.) Only the first line of a docstring begins with whitespace | |
1483 | ;; and a quotation mark and ends with a period or (rarely) a comma. | |
1484 | ;; | |
1485 | ;; The `fill-column' is temporarily bound to | |
1486 | ;; `emacs-lisp-docstring-fill-column' if that value is an integer. | |
1487 | (let ((paragraph-start (concat paragraph-start | |
1488 | "\\|\\s-*\\([(;:\"]\\|`(\\|#'(\\)")) | |
1489 | (paragraph-separate | |
1490 | (concat paragraph-separate "\\|\\s-*\".*[,\\.]$")) | |
1491 | (fill-column (if (and (integerp emacs-lisp-docstring-fill-column) | |
1492 | (derived-mode-p 'emacs-lisp-mode)) | |
1493 | emacs-lisp-docstring-fill-column | |
1494 | fill-column))) | |
1495 | (fill-paragraph justify)) | |
1496 | ;; Never return nil. | |
1497 | t)) | |
1498 | ||
1499 | (defun indent-code-rigidly (start end arg &optional nochange-regexp) | |
1500 | "Indent all lines of code, starting in the region, sideways by ARG columns. | |
1501 | Does not affect lines starting inside comments or strings, assuming that | |
1502 | the start of the region is not inside them. | |
1503 | ||
1504 | Called from a program, takes args START, END, COLUMNS and NOCHANGE-REGEXP. | |
1505 | The last is a regexp which, if matched at the beginning of a line, | |
1506 | means don't indent that line." | |
1507 | (interactive "r\np") | |
1508 | (let (state) | |
1509 | (save-excursion | |
1510 | (goto-char end) | |
1511 | (setq end (point-marker)) | |
1512 | (goto-char start) | |
1513 | (or (bolp) | |
1514 | (setq state (parse-partial-sexp (point) | |
1515 | (progn | |
1516 | (forward-line 1) (point)) | |
1517 | nil nil state))) | |
1518 | (while (< (point) end) | |
1519 | (or (car (nthcdr 3 state)) | |
1520 | (and nochange-regexp | |
1521 | (looking-at nochange-regexp)) | |
1522 | ;; If line does not start in string, indent it | |
1523 | (let ((indent (current-indentation))) | |
1524 | (delete-region (point) (progn (skip-chars-forward " \t") (point))) | |
1525 | (or (eolp) | |
1526 | (indent-to (max 0 (+ indent arg)) 0)))) | |
1527 | (setq state (parse-partial-sexp (point) | |
1528 | (progn | |
1529 | (forward-line 1) (point)) | |
1530 | nil nil state)))))) | |
1531 | ||
1532 | (provide 'lisp-mode) | |
1533 | ||
d6a3febd | 1534 | ;;; lisp-mode.el ends here |