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