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