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