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