Mercurial > hg > xemacs-beta
annotate lisp/lisp-mode.el @ 5613:a944c124b2d3
Be more careful about what we shadow, #'byte-compile-eval.
2011-12-13 Aidan Kehoe <kehoea@parhasard.net>
* bytecomp.el (byte-compile-eval):
When evaluating code at compile time, don't put those macros in
the macro environment that only make sense when creating
byte-compiled output.
author | Aidan Kehoe <kehoea@parhasard.net> |
---|---|
date | Tue, 13 Dec 2011 20:42:30 +0000 |
parents | 071b810ceb18 |
children |
rev | line source |
---|---|
428 | 1 ;;; lisp-mode.el --- Lisp mode, and its idiosyncratic commands. |
2 | |
3 ;; Copyright (C) 1985, 1996, 1997 Free Software Foundation, Inc. | |
442 | 4 ;; Copyright (C) 1995 Tinker Systems. |
4942
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
5 ;; Copyright (C) 2002, 2010 Ben Wing. |
428 | 6 |
7 ;; Maintainer: FSF | |
8 ;; Keywords: lisp, languages, dumped | |
9 | |
10 ;; This file is part of XEmacs. | |
11 | |
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5194
diff
changeset
|
12 ;; XEmacs is free software: you can redistribute it and/or modify it |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5194
diff
changeset
|
13 ;; under the terms of the GNU General Public License as published by the |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5194
diff
changeset
|
14 ;; Free Software Foundation, either version 3 of the License, or (at your |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5194
diff
changeset
|
15 ;; option) any later version. |
428 | 16 |
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5194
diff
changeset
|
17 ;; XEmacs is distributed in the hope that it will be useful, but WITHOUT |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5194
diff
changeset
|
18 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5194
diff
changeset
|
19 ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5194
diff
changeset
|
20 ;; for more details. |
428 | 21 |
22 ;; You should have received a copy of the GNU General Public License | |
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5194
diff
changeset
|
23 ;; along with XEmacs. If not, see <http://www.gnu.org/licenses/>. |
428 | 24 |
25 ;;; Synched up with: FSF 19.34 (but starting to diverge). | |
26 | |
27 ;;; Commentary: | |
28 | |
29 ;; This file is dumped with XEmacs. | |
30 | |
31 ;; The base major mode for editing Lisp code (used also for Emacs Lisp). | |
32 ;; This mode is documented in the Emacs manual | |
33 | |
34 ;;; Code: | |
35 | |
36 (defgroup lisp nil | |
37 "Lisp support, including Emacs Lisp." | |
38 :group 'languages | |
39 :group 'development) | |
40 | |
41 (defvar lisp-mode-syntax-table nil) | |
42 (defvar emacs-lisp-mode-syntax-table nil) | |
43 (defvar lisp-mode-abbrev-table nil) | |
44 | |
442 | 45 (defun construct-lisp-mode-menu (popup-p emacs-lisp-p) |
5567
3bc58dc9d688
Replace #'flet by #'labels where appropriate, core code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
46 (labels ((popup-wrap (form) |
3bc58dc9d688
Replace #'flet by #'labels where appropriate, core code.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5402
diff
changeset
|
47 (if popup-p `(menu-call-at-event ',form) form))) |
5576
071b810ceb18
Declare labels as line where appropriate; use #'labels, not #'flet, tests.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5567
diff
changeset
|
48 (declare (inline popup-wrap)) |
442 | 49 `(,@(if emacs-lisp-p |
50 `(["%_Byte-Compile This File" ,(popup-wrap | |
51 'emacs-lisp-byte-compile)] | |
52 ["B%_yte-Compile/Load This File" | |
53 ,(popup-wrap 'emacs-lisp-byte-compile-and-load)] | |
54 ["Byte-%_Recompile Directory..." | |
55 ,(popup-wrap 'byte-recompile-directory)] | |
56 "---")) | |
57 ["%_Evaluate Region or Defun" | |
58 ,(popup-wrap '(if (region-exists-p) | |
59 (call-interactively 'eval-region) | |
60 (call-interactively 'eval-defun)))] | |
61 ["Evaluate %_Whole Buffer" ,(popup-wrap 'eval-current-buffer)] | |
62 ["Evaluate Last %_S-expression" ,(popup-wrap 'eval-last-sexp)] | |
63 "---" | |
800 | 64 ["%_Indent Region or Balanced Expression" |
65 ,(popup-wrap '(if (region-exists-p) | |
66 (call-interactively 'indent-region) | |
67 (call-interactively 'indent-sexp)))] | |
68 ["I%_ndent Defun" | |
69 ,(popup-wrap '(progn | |
70 (beginning-of-defun) | |
71 (indent-sexp)))] | |
72 "---" | |
73 ["%_Comment Out Region" comment-region :active (region-exists-p)] | |
1333 | 74 ["Unc%_omment Region" uncomment-region :active (region-exists-p)] |
800 | 75 "---" |
442 | 76 ,@(if popup-p |
77 '(["%_Find Function" | |
78 (find-function (menu-call-at-event '(function-at-point))) | |
79 :suffix (let ((fun (menu-call-at-event '(function-at-point)))) | |
80 (if fun (symbol-name fun) "")) | |
81 :active (and (fboundp 'find-function) | |
82 (menu-call-at-event '(function-at-point)))] | |
83 ["%_Find Variable" | |
84 (find-variable (menu-call-at-event '(variable-at-point))) | |
85 :suffix (let ((fun (menu-call-at-event '(variable-at-point)))) | |
86 (if fun (symbol-name fun) "")) | |
87 :active (and (fboundp 'find-variable) | |
88 (menu-call-at-event '(variable-at-point)))] | |
89 ["%_Help on Function" | |
90 (describe-function (menu-call-at-event '(function-at-point))) | |
91 :suffix (let ((fun (menu-call-at-event '(function-at-point)))) | |
92 (if fun (symbol-name fun) "")) | |
93 :active (and (fboundp 'describe-function) | |
94 (menu-call-at-event '(function-at-point)))] | |
95 ["%_Help on Variable" | |
96 (describe-variable (menu-call-at-event '(variable-at-point))) | |
97 :suffix (let ((fun (menu-call-at-event '(variable-at-point)))) | |
98 (if fun (symbol-name fun) "")) | |
99 :active (and (fboundp 'describe-variable) | |
100 (menu-call-at-event '(variable-at-point)))]) | |
101 '(["Find %_Function..." find-function | |
102 :active (fboundp 'find-function)] | |
103 ["Find %_Variable..." find-variable | |
104 :active (fboundp 'find-variable)] | |
105 ["%_Help on Function..." describe-function | |
106 :active (fboundp 'describe-function)] | |
107 ["Hel%_p on Variable..." describe-variable | |
108 :active (fboundp 'describe-variable)])) | |
109 "---" | |
110 ["Instrument This Defun for %_Debugging" ,(popup-wrap 'edebug-defun)] | |
111 ["%_Trace Function..." trace-function-background] | |
112 ["%_Untrace All Functions" untrace-all | |
113 :active (fboundp 'untrace-all)] | |
114 "---" | |
800 | 115 ["Display %_Macro Expansion of Balanced Expression" |
116 ,(popup-wrap 'macroexpand-sexp)] | |
117 ["Display Recursive Macro E%_xpansion of Balanced Expression" | |
118 ,(popup-wrap 'macroexpand-all-sexp)] | |
442 | 119 "---" |
120 "Look for debug-on-error under Options->Troubleshooting" | |
121 ))) | |
428 | 122 |
442 | 123 (defvar lisp-interaction-mode-popup-menu |
124 (cons "Lisp-Interaction" (construct-lisp-mode-menu t nil))) | |
125 | |
126 (defvar emacs-lisp-mode-popup-menu | |
127 (cons "Emacs-Lisp" (construct-lisp-mode-menu t t))) | |
428 | 128 |
129 ;Don't have a menubar entry in Lisp Interaction mode. Otherwise, the | |
130 ;*scratch* buffer has a Lisp menubar item! Very confusing. | |
442 | 131 ;Jan Vroonhof really wants this, so it's back. --ben |
132 (defvar lisp-interaction-mode-menubar-menu | |
133 (cons "%_Lisp" (construct-lisp-mode-menu nil nil))) | |
428 | 134 |
442 | 135 (defvar emacs-lisp-mode-menubar-menu |
136 (cons "%_Lisp" (construct-lisp-mode-menu nil t))) | |
428 | 137 |
138 (if (not emacs-lisp-mode-syntax-table) | |
139 (let ((i 0)) | |
140 (setq emacs-lisp-mode-syntax-table (make-syntax-table)) | |
141 (while (< i ?0) | |
142 (modify-syntax-entry i "_ " emacs-lisp-mode-syntax-table) | |
143 (setq i (1+ i))) | |
144 (setq i (1+ ?9)) | |
145 (while (< i ?A) | |
146 (modify-syntax-entry i "_ " emacs-lisp-mode-syntax-table) | |
147 (setq i (1+ i))) | |
148 (setq i (1+ ?Z)) | |
149 (while (< i ?a) | |
150 (modify-syntax-entry i "_ " emacs-lisp-mode-syntax-table) | |
151 (setq i (1+ i))) | |
152 (setq i (1+ ?z)) | |
153 (while (< i 128) | |
154 (modify-syntax-entry i "_ " emacs-lisp-mode-syntax-table) | |
155 (setq i (1+ i))) | |
156 (modify-syntax-entry ? " " emacs-lisp-mode-syntax-table) | |
157 (modify-syntax-entry ?\t " " emacs-lisp-mode-syntax-table) | |
158 (modify-syntax-entry ?\f " " emacs-lisp-mode-syntax-table) | |
159 (modify-syntax-entry ?\n "> " emacs-lisp-mode-syntax-table) | |
160 ;; Give CR the same syntax as newline, for selective-display. | |
161 (modify-syntax-entry ?\^m "> " emacs-lisp-mode-syntax-table) | |
162 (modify-syntax-entry ?\; "< " emacs-lisp-mode-syntax-table) | |
163 (modify-syntax-entry ?` "' " emacs-lisp-mode-syntax-table) | |
164 (modify-syntax-entry ?' "' " emacs-lisp-mode-syntax-table) | |
165 (modify-syntax-entry ?, "' " emacs-lisp-mode-syntax-table) | |
166 ;; Used to be singlequote; changed for flonums. | |
167 (modify-syntax-entry ?. "_ " emacs-lisp-mode-syntax-table) | |
168 (modify-syntax-entry ?# "' " emacs-lisp-mode-syntax-table) | |
169 (modify-syntax-entry ?\" "\" " emacs-lisp-mode-syntax-table) | |
170 (modify-syntax-entry ?\\ "\\ " emacs-lisp-mode-syntax-table) | |
171 (modify-syntax-entry ?\( "() " emacs-lisp-mode-syntax-table) | |
172 (modify-syntax-entry ?\) ")( " emacs-lisp-mode-syntax-table) | |
173 (modify-syntax-entry ?\[ "(] " emacs-lisp-mode-syntax-table) | |
174 (modify-syntax-entry ?\] ")[ " emacs-lisp-mode-syntax-table))) | |
175 | |
176 (if (not lisp-mode-syntax-table) | |
177 (progn (setq lisp-mode-syntax-table | |
178 (copy-syntax-table emacs-lisp-mode-syntax-table)) | |
179 (modify-syntax-entry ?\[ "_ " lisp-mode-syntax-table) | |
180 ;; XEmacs changes | |
181 (modify-syntax-entry ?\] "_ " lisp-mode-syntax-table) | |
460 | 182 (modify-syntax-entry ?# "' 58" lisp-mode-syntax-table) |
183 (modify-syntax-entry ?| "\" 67" lisp-mode-syntax-table))) | |
428 | 184 |
185 (define-abbrev-table 'lisp-mode-abbrev-table ()) | |
186 | |
187 (defvar lisp-imenu-generic-expression | |
188 '( | |
189 (nil | |
190 "^\\s-*(def\\(un\\|subst\\|macro\\|advice\\)\\s-+\\([-A-Za-z0-9+*|:]+\\)" 2) | |
191 ("Variables" | |
192 "^\\s-*(def\\(var\\|const\\|custom\\)\\s-+\\([-A-Za-z0-9+*|:]+\\)" 2) | |
193 ("Types" | |
194 "^\\s-*(def\\(group\\|type\\|struct\\|class\\|ine-condition\\)\\s-+\\([-A-Za-z0-9+*|:]+\\)" | |
195 2)) | |
196 | |
197 "Imenu generic expression for Lisp mode. See `imenu-generic-expression'.") | |
198 | |
199 (defun lisp-mode-variables (lisp-syntax) | |
200 (cond (lisp-syntax | |
201 (set-syntax-table lisp-mode-syntax-table))) | |
202 (setq local-abbrev-table lisp-mode-abbrev-table) | |
203 (make-local-variable 'paragraph-start) | |
204 (setq paragraph-start (concat page-delimiter "\\|$" )) | |
205 (make-local-variable 'paragraph-separate) | |
206 (setq paragraph-separate paragraph-start) | |
207 (make-local-variable 'paragraph-ignore-fill-prefix) | |
208 (setq paragraph-ignore-fill-prefix t) | |
209 (make-local-variable 'fill-paragraph-function) | |
210 (setq fill-paragraph-function 'lisp-fill-paragraph) | |
211 ;; Adaptive fill mode gets in the way of auto-fill, | |
212 ;; and should make no difference for explicit fill | |
213 ;; because lisp-fill-paragraph should do the job. | |
214 (make-local-variable 'adaptive-fill-mode) | |
215 (setq adaptive-fill-mode nil) | |
216 (make-local-variable 'indent-line-function) | |
217 (setq indent-line-function 'lisp-indent-line) | |
218 (make-local-variable 'indent-region-function) | |
219 (setq indent-region-function 'lisp-indent-region) | |
220 (make-local-variable 'parse-sexp-ignore-comments) | |
221 (setq parse-sexp-ignore-comments t) | |
222 (make-local-variable 'outline-regexp) | |
223 (setq outline-regexp ";;; \\|(....") | |
224 (make-local-variable 'comment-start) | |
225 (setq comment-start ";") | |
226 ;; XEmacs change | |
227 (set (make-local-variable 'block-comment-start) ";;") | |
228 (make-local-variable 'comment-start-skip) | |
229 ;; Look within the line for a ; following an even number of backslashes | |
230 ;; after either a non-backslash or the line beginning. | |
231 (setq comment-start-skip "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\);+ *") | |
232 (make-local-variable 'comment-column) | |
233 (setq comment-column 40) | |
234 (make-local-variable 'comment-indent-function) | |
235 (setq comment-indent-function 'lisp-comment-indent) | |
236 ;; XEmacs change | |
237 (set (make-local-variable 'dabbrev-case-fold-search) nil) | |
238 (set (make-local-variable 'dabbrev-case-replace) nil) | |
239 (make-local-variable 'imenu-generic-expression) | |
240 (setq imenu-generic-expression lisp-imenu-generic-expression)) | |
241 | |
242 (defvar shared-lisp-mode-map () | |
243 "Keymap for commands shared by all sorts of Lisp modes.") | |
244 | |
245 (if shared-lisp-mode-map | |
246 () | |
247 (setq shared-lisp-mode-map (make-sparse-keymap)) | |
248 ;; XEmacs changes | |
249 (set-keymap-name shared-lisp-mode-map 'shared-lisp-mode-map) | |
250 (define-key shared-lisp-mode-map "\M-;" 'lisp-indent-for-comment) | |
251 (define-key shared-lisp-mode-map "\e\C-q" 'indent-sexp)) | |
252 | |
253 (defvar emacs-lisp-mode-map () | |
254 "Keymap for Emacs Lisp mode. | |
255 All commands in `shared-lisp-mode-map' are inherited by this map.") | |
256 | |
257 (if emacs-lisp-mode-map | |
258 () | |
259 ;; XEmacs: Ignore FSF nconc stuff | |
260 (setq emacs-lisp-mode-map (make-sparse-keymap)) | |
261 (set-keymap-name emacs-lisp-mode-map 'emacs-lisp-mode-map) | |
262 (set-keymap-parents emacs-lisp-mode-map (list shared-lisp-mode-map)) | |
263 (define-key emacs-lisp-mode-map "\e\t" 'lisp-complete-symbol) | |
264 (define-key emacs-lisp-mode-map "\e\C-x" 'eval-defun) | |
265 ;; XEmacs: Not sure what the FSF menu bindings are. I hope XEmacs | |
266 ;; doesn't need them. | |
267 ) | |
268 | |
1428 | 269 ;; XEmacs: add docstrings to the hooks |
270 (defvar emacs-lisp-mode-hook nil | |
271 "Hook to run when entering emacs-lisp-mode.") | |
272 | |
273 (defvar lisp-mode-hook nil | |
274 "Hook to run when entering lisp-mode.") | |
275 | |
276 (defvar lisp-interaction-mode-hook nil | |
277 "Hook to run when entering lisp-interaction-mode.") | |
278 | |
428 | 279 (defun emacs-lisp-byte-compile () |
280 "Byte compile the file containing the current buffer." | |
281 (interactive) | |
282 (if buffer-file-name | |
283 ;; XEmacs change. Force buffer save first | |
284 (progn | |
285 (save-buffer) | |
286 (byte-compile-file buffer-file-name)) | |
287 (error "The buffer must be saved in a file first."))) | |
288 | |
289 (defun emacs-lisp-byte-compile-and-load () | |
290 "Byte-compile the current file (if it has changed), then load compiled code." | |
291 (interactive) | |
292 (or buffer-file-name | |
293 (error "The buffer must be saved in a file first")) | |
294 (require 'bytecomp) | |
295 ;; Recompile if file or buffer has changed since last compilation. | |
296 (if (and (buffer-modified-p) | |
297 (y-or-n-p (format "save buffer %s first? " (buffer-name)))) | |
298 (save-buffer)) | |
299 (let ((compiled-file-name (byte-compile-dest-file buffer-file-name))) | |
300 (if (file-newer-than-file-p compiled-file-name buffer-file-name) | |
301 (load-file compiled-file-name) | |
302 (byte-compile-file buffer-file-name t)))) | |
303 | |
304 (defun emacs-lisp-mode () | |
305 "Major mode for editing Lisp code to run in Emacs. | |
306 Commands: | |
307 Delete converts tabs to spaces as it moves back. | |
308 Blank lines separate paragraphs. Semicolons start comments. | |
309 \\{emacs-lisp-mode-map} | |
310 Entry to this mode calls the value of `emacs-lisp-mode-hook' | |
311 if that value is non-nil." | |
312 (interactive) | |
313 (kill-all-local-variables) | |
314 (use-local-map emacs-lisp-mode-map) | |
315 (set-syntax-table emacs-lisp-mode-syntax-table) | |
316 ;; XEmacs changes | |
317 (setq major-mode 'emacs-lisp-mode | |
442 | 318 mode-popup-menu emacs-lisp-mode-popup-menu |
428 | 319 mode-name "Emacs-Lisp") |
442 | 320 (if (and (featurep 'menubar) |
321 current-menubar) | |
322 (progn | |
428 | 323 ;; make a local copy of the menubar, so our modes don't |
324 ;; change the global menubar | |
442 | 325 (set-buffer-menubar current-menubar) |
326 (add-submenu nil emacs-lisp-mode-menubar-menu))) | |
428 | 327 (lisp-mode-variables nil) |
328 (run-hooks 'emacs-lisp-mode-hook)) | |
329 | |
430 | 330 (put 'emacs-lisp-mode 'font-lock-lisp-like t) |
331 | |
428 | 332 (defvar lisp-mode-map () |
333 "Keymap for ordinary Lisp mode. | |
334 All commands in `shared-lisp-mode-map' are inherited by this map.") | |
335 | |
336 (if lisp-mode-map | |
337 () | |
338 ;; XEmacs changes | |
339 (setq lisp-mode-map (make-sparse-keymap)) | |
340 (set-keymap-name lisp-mode-map 'lisp-mode-map) | |
341 (set-keymap-parents lisp-mode-map (list shared-lisp-mode-map)) | |
342 ;; gag, no. use ilisp. -jwz | |
343 ;; (define-key lisp-mode-map "\C-c\C-z" 'run-lisp) | |
344 ) | |
345 | |
346 (defun lisp-mode () | |
613 | 347 "Major mode for editing Lisp code for Lisps other than Emacs Lisp. |
428 | 348 Commands: |
349 Delete converts tabs to spaces as it moves back. | |
350 Blank lines separate paragraphs. Semicolons start comments. | |
351 \\{lisp-mode-map} | |
352 Note that `run-lisp' may be used either to start an inferior Lisp job | |
353 or to switch back to an existing one. | |
354 | |
355 Entry to this mode calls the value of `lisp-mode-hook' | |
356 if that value is non-nil." | |
357 (interactive) | |
358 (kill-all-local-variables) | |
359 (use-local-map lisp-mode-map) | |
360 (setq major-mode 'lisp-mode) | |
361 (setq mode-name "Lisp") | |
362 (lisp-mode-variables t) | |
363 (set-syntax-table lisp-mode-syntax-table) | |
364 (run-hooks 'lisp-mode-hook)) | |
365 | |
366 ;; XEmacs change: emacs-lisp-mode-map is a more appropriate parent. | |
367 (defvar lisp-interaction-mode-map () | |
368 "Keymap for Lisp Interaction mode. | |
369 All commands in `shared-lisp-mode-map' are inherited by this map.") | |
370 | |
371 (if lisp-interaction-mode-map | |
372 () | |
373 ;; XEmacs set keymap our way | |
374 (setq lisp-interaction-mode-map (make-sparse-keymap)) | |
375 (set-keymap-name lisp-interaction-mode-map 'lisp-interaction-mode-map) | |
376 (set-keymap-parents lisp-interaction-mode-map (list emacs-lisp-mode-map)) | |
377 (define-key lisp-interaction-mode-map "\e\C-x" 'eval-defun) | |
378 (define-key lisp-interaction-mode-map "\e\t" 'lisp-complete-symbol) | |
379 (define-key lisp-interaction-mode-map "\n" 'eval-print-last-sexp)) | |
380 | |
381 (defun lisp-interaction-mode () | |
382 "Major mode for typing and evaluating Lisp forms. | |
383 Like Lisp mode except that \\[eval-print-last-sexp] evals the Lisp expression | |
384 before point, and prints its value into the buffer, advancing point. | |
385 | |
386 Commands: | |
387 Delete converts tabs to spaces as it moves back. | |
388 Paragraphs are separated only by blank lines. | |
389 Semicolons start comments. | |
390 \\{lisp-interaction-mode-map} | |
391 Entry to this mode calls the value of `lisp-interaction-mode-hook' | |
392 if that value is non-nil." | |
393 (interactive) | |
394 (kill-all-local-variables) | |
395 (use-local-map lisp-interaction-mode-map) | |
396 (setq major-mode 'lisp-interaction-mode) | |
397 (setq mode-name "Lisp Interaction") | |
442 | 398 (setq mode-popup-menu lisp-interaction-mode-popup-menu) |
399 (if (and (featurep 'menubar) | |
400 current-menubar) | |
401 (progn | |
402 ;; make a local copy of the menubar, so our modes don't | |
403 ;; change the global menubar | |
404 (set-buffer-menubar current-menubar) | |
405 (add-submenu nil lisp-interaction-mode-menubar-menu))) | |
428 | 406 (set-syntax-table emacs-lisp-mode-syntax-table) |
407 (lisp-mode-variables nil) | |
408 (run-hooks 'lisp-interaction-mode-hook)) | |
409 | |
410 (defun eval-print-last-sexp () | |
411 "Evaluate sexp before point; print value into current buffer." | |
412 (interactive) | |
413 (let ((standard-output (current-buffer))) | |
414 (terpri) | |
415 (eval-last-sexp t) | |
416 (terpri))) | |
417 | |
418 ;; XEmacs change | |
419 (defcustom eval-interactive-verbose t | |
420 "*Non-nil means that interactive evaluation can print messages. | |
421 The messages are printed when the expression is treated differently | |
2116 | 422 using `\\[eval-last-sexp]' and `\\[eval-defun]' than it would have |
423 been treated noninteractively. | |
428 | 424 |
425 The printed messages are \"defvar treated as defconst\" and \"defcustom | |
4677
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
426 evaluation forced\". See `eval-interactive' for more details." |
428 | 427 :type 'boolean |
428 :group 'lisp) | |
429 | |
430 (defun eval-interactive (expr) | |
4677
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
431 "Evaluate EXPR; pass back multiple values, transform defvars to defconsts. |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
432 |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
433 Always returns a list. The length of this list will be something other than |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
434 one if the form returned multiple values. It will be zero if the form |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
435 returned a single zero-length multiple value." |
428 | 436 (cond ((and (eq (car-safe expr) 'defvar) |
437 (> (length expr) 2)) | |
4677
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
438 (setq expr (multiple-value-list (eval (cons 'defconst (cdr expr))))) |
428 | 439 (when eval-interactive-verbose |
440 (message "defvar treated as defconst") | |
441 (sit-for 1) | |
442 (message "")) | |
4677
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
443 expr) |
428 | 444 ((and (eq (car-safe expr) 'defcustom) |
445 (> (length expr) 2) | |
446 (default-boundp (nth 1 expr))) | |
447 ;; Force variable to be bound | |
4677
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
448 (funcall |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
449 (or (plist-get expr :set) #'custom-set-default) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
450 (nth 1 expr) (eval (nth 2 expr))) |
428 | 451 ;; And evaluate the defcustom |
4677
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
452 (setq expr (multiple-value-list (eval expr))) |
428 | 453 (when eval-interactive-verbose |
454 (message "defcustom evaluation forced") | |
455 (sit-for 1) | |
456 (message "")) | |
4677
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
457 expr) |
428 | 458 (t |
4677
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
459 (multiple-value-list (eval expr))))) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
460 |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
461 (defun prin1-list-as-multiple-values (multiple-value-list &optional stream) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
462 "Call `prin1' on each element of MULTIPLE-VALUE-LIST, separated by \" ;\\n\" |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
463 |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
464 If MULTIPLE-VALUE-LIST is zero-length, print the text |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
465 \"#<zero length multiple value> ;\\n\". Always returns nil." |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
466 (loop for value in multiple-value-list |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
467 with seen-first = nil |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
468 do |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
469 (if seen-first |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
470 (princ " ;\n" stream) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
471 (setq seen-first t)) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
472 (prin1 value stream) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
473 finally (unless seen-first |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
474 (princ "#<zero length multiple value> ;" stream)))) |
428 | 475 |
476 ;; XEmacs change, based on Bob Weiner suggestion | |
477 (defun eval-last-sexp (eval-last-sexp-arg-internal) ;dynamic scoping wonderment | |
478 "Evaluate sexp before point; print value in minibuffer. | |
479 With argument, print output into current buffer." | |
480 (interactive "P") | |
481 (let ((standard-output (if eval-last-sexp-arg-internal (current-buffer) t)) | |
482 (opoint (point)) | |
483 ignore-quotes) | |
4677
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
484 (prin1-list-as-multiple-values |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
485 (eval-interactive |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
486 (letf (((syntax-table) emacs-lisp-mode-syntax-table)) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
487 (save-excursion |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
488 ;; If this sexp appears to be enclosed in `...' then |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
489 ;; ignore the surrounding quotes. |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
490 (setq ignore-quotes (or (eq (char-after) ?\') |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
491 (eq (char-before) ?\'))) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
492 (forward-sexp -1) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
493 ;; vladimir@cs.ualberta.ca 30-Jul-1997: skip ` in |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
494 ;; `variable' so that the value is returned, not the |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
495 ;; name. |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
496 (if (and ignore-quotes |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
497 (eq (char-after) ?\`)) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
498 (forward-char)) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
499 (save-restriction |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
500 (narrow-to-region (point-min) opoint) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
501 (let ((expr (read (current-buffer)))) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
502 (if (eq (car-safe expr) 'interactive) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
503 ;; If it's an (interactive ...) form, it's |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
504 ;; more useful to show how an interactive call |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
505 ;; would use it. |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
506 `(call-interactively |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
507 (lambda (&rest args) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
508 ,expr args)) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
509 expr))))))))) |
428 | 510 |
511 (defun eval-defun (eval-defun-arg-internal) | |
512 "Evaluate defun that point is in or before. | |
513 Print value in minibuffer. | |
514 With argument, insert value in current buffer after the defun." | |
515 (interactive "P") | |
516 (let ((standard-output (if eval-defun-arg-internal (current-buffer) t))) | |
4677
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
517 (prin1-list-as-multiple-values |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
518 (eval-interactive |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
519 (save-excursion |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
520 (end-of-defun) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
521 (beginning-of-defun) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
522 (read (current-buffer))))))) |
428 | 523 |
524 (defun lisp-comment-indent () | |
525 (if (looking-at "\\s<\\s<\\s<") | |
526 (current-column) | |
527 (if (looking-at "\\s<\\s<") | |
528 ;; #### FSF has: | |
529 ;; (let ((tem (or (calculate-lisp-indent) (current-column)))) ... | |
530 (let ((tem (calculate-lisp-indent))) | |
531 (if (listp tem) (car tem) tem)) | |
532 (skip-chars-backward " \t") | |
533 (max (if (bolp) 0 (1+ (current-column))) | |
534 comment-column)))) | |
535 | |
536 ;; XEmacs change | |
537 (defun lisp-indent-for-comment () | |
538 "Indent this line's comment appropriately, or insert an empty comment. | |
539 If adding a new comment on a blank line, use `block-comment-start' instead | |
540 of `comment-start' to open the comment." | |
541 ;; by Stig@hackvan.com | |
542 ;; #### - This functionality, the recognition of block-comment-{start,end}, | |
543 ;; will perhaps be standardized across modes and move to indent-for-comment. | |
544 (interactive) | |
545 (if (and block-comment-start | |
546 (save-excursion (beginning-of-line) (looking-at "^[ \t]*$"))) | |
547 (insert block-comment-start)) | |
548 (indent-for-comment)) | |
549 | |
550 (defvar lisp-indent-offset nil) | |
551 (defvar lisp-indent-function 'lisp-indent-function) | |
552 | |
553 (defun lisp-indent-line (&optional whole-exp) | |
554 "Indent current line as Lisp code. | |
555 With argument, indent any additional lines of the same expression | |
556 rigidly along with this one." | |
557 (interactive "P") | |
558 (let ((indent (calculate-lisp-indent)) shift-amt beg end | |
559 (pos (- (point-max) (point)))) | |
560 (beginning-of-line) | |
561 (setq beg (point)) | |
562 (skip-chars-forward " \t") | |
563 (if (looking-at "\\s<\\s<\\s<") | |
564 ;; Don't alter indentation of a ;;; comment line. | |
565 (goto-char (- (point-max) pos)) | |
566 (if (and (looking-at "\\s<") (not (looking-at "\\s<\\s<"))) | |
567 ;; Single-semicolon comment lines should be indented | |
568 ;; as comment lines, not as code. | |
446 | 569 (progn (indent-for-comment) (backward-char 1)) |
428 | 570 (if (listp indent) (setq indent (car indent))) |
571 (setq shift-amt (- indent (current-column))) | |
572 (if (zerop shift-amt) | |
573 nil | |
574 (delete-region beg (point)) | |
575 (indent-to indent))) | |
576 ;; If initial point was within line's indentation, | |
577 ;; position after the indentation. Else stay at same point in text. | |
578 (if (> (- (point-max) pos) (point)) | |
579 (goto-char (- (point-max) pos))) | |
580 ;; If desired, shift remaining lines of expression the same amount. | |
581 (and whole-exp (not (zerop shift-amt)) | |
582 (save-excursion | |
583 (goto-char beg) | |
584 (forward-sexp 1) | |
585 (setq end (point)) | |
586 (goto-char beg) | |
587 (forward-line 1) | |
588 (setq beg (point)) | |
589 (> end beg)) | |
590 (indent-code-rigidly beg end shift-amt))))) | |
591 | |
592 (defvar calculate-lisp-indent-last-sexp) | |
593 | |
594 (defun calculate-lisp-indent (&optional parse-start) | |
595 "Return appropriate indentation for current line as Lisp code. | |
596 In usual case returns an integer: the column to indent to. | |
597 Can instead return a list, whose car is the column to indent to. | |
598 This means that following lines at the same level of indentation | |
599 should not necessarily be indented the same way. | |
600 The second element of the list is the buffer position | |
601 of the start of the containing expression." | |
602 (save-excursion | |
603 (beginning-of-line) | |
604 (let ((indent-point (point)) | |
605 ;; XEmacs change (remove paren-depth) | |
606 state ;;paren-depth | |
607 ;; setting this to a number inhibits calling hook | |
608 (desired-indent nil) | |
609 (retry t) | |
610 calculate-lisp-indent-last-sexp containing-sexp) | |
611 (if parse-start | |
612 (goto-char parse-start) | |
613 (beginning-of-defun)) | |
614 ;; Find outermost containing sexp | |
615 (while (< (point) indent-point) | |
616 (setq state (parse-partial-sexp (point) indent-point 0))) | |
617 ;; Find innermost containing sexp | |
618 (while (and retry | |
619 state | |
620 ;; XEmacs change (remove paren-depth) | |
621 (> ;;(setq paren-depth (elt state 0)) | |
622 (elt state 0) | |
623 0)) | |
624 (setq retry nil) | |
625 (setq calculate-lisp-indent-last-sexp (elt state 2)) | |
626 (setq containing-sexp (elt state 1)) | |
627 ;; Position following last unclosed open. | |
628 (goto-char (1+ containing-sexp)) | |
629 ;; Is there a complete sexp since then? | |
630 (if (and calculate-lisp-indent-last-sexp | |
631 (> calculate-lisp-indent-last-sexp (point))) | |
632 ;; Yes, but is there a containing sexp after that? | |
633 (let ((peek (parse-partial-sexp calculate-lisp-indent-last-sexp | |
634 indent-point 0))) | |
635 (if (setq retry (car (cdr peek))) (setq state peek))))) | |
636 (if retry | |
637 nil | |
638 ;; Innermost containing sexp found | |
639 (goto-char (1+ containing-sexp)) | |
640 (if (not calculate-lisp-indent-last-sexp) | |
641 ;; indent-point immediately follows open paren. | |
642 ;; Don't call hook. | |
643 (setq desired-indent (current-column)) | |
644 ;; Find the start of first element of containing sexp. | |
645 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t) | |
646 (cond ((looking-at "\\s(") | |
647 ;; First element of containing sexp is a list. | |
648 ;; Indent under that list. | |
649 ) | |
650 ((> (save-excursion (forward-line 1) (point)) | |
651 calculate-lisp-indent-last-sexp) | |
652 ;; This is the first line to start within the containing sexp. | |
653 ;; It's almost certainly a function call. | |
654 (if (= (point) calculate-lisp-indent-last-sexp) | |
655 ;; Containing sexp has nothing before this line | |
656 ;; except the first element. Indent under that element. | |
657 nil | |
658 ;; Skip the first element, find start of second (the first | |
659 ;; argument of the function call) and indent under. | |
660 (progn (forward-sexp 1) | |
661 (parse-partial-sexp (point) | |
662 calculate-lisp-indent-last-sexp | |
663 0 t))) | |
664 (backward-prefix-chars)) | |
665 (t | |
666 ;; Indent beneath first sexp on same line as | |
667 ;; calculate-lisp-indent-last-sexp. Again, it's | |
668 ;; almost certainly a function call. | |
669 (goto-char calculate-lisp-indent-last-sexp) | |
670 (beginning-of-line) | |
671 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp | |
672 0 t) | |
673 (backward-prefix-chars))))) | |
674 ;; Point is at the point to indent under unless we are inside a string. | |
675 ;; Call indentation hook except when overridden by lisp-indent-offset | |
676 ;; or if the desired indentation has already been computed. | |
677 (let ((normal-indent (current-column))) | |
678 (cond ((elt state 3) | |
679 ;; Inside a string, don't change indentation. | |
680 (goto-char indent-point) | |
681 (skip-chars-forward " \t") | |
682 (current-column)) | |
683 (desired-indent) | |
684 ((and (boundp 'lisp-indent-function) | |
685 lisp-indent-function | |
686 (not retry)) | |
687 (or (funcall lisp-indent-function indent-point state) | |
688 normal-indent)) | |
689 ;; XEmacs change: | |
690 ;; lisp-indent-offset shouldn't override lisp-indent-function ! | |
691 ((and (integerp lisp-indent-offset) containing-sexp) | |
692 ;; Indent by constant offset | |
693 (goto-char containing-sexp) | |
694 (+ normal-indent lisp-indent-offset)) | |
695 (t | |
696 normal-indent)))))) | |
697 | |
872 | 698 (defvar lisp-function-and-type-regexp |
699 (concat "def\\(" | |
700 ;; def but not define-. | |
5194
77907bd57d25
Add missing CL style fontification.
Didier Verna <didier@xemacs.org>
parents:
4958
diff
changeset
|
701 "\\(un\\*?\\|advice\\|alias\\|macro\\*?\\|setf\\|subst\\*?\\|" |
872 | 702 "-edebug-spec\\|" |
703 ;; CLOS | |
704 "method\\|generic\\|" | |
705 ;; define-* | |
706 "ine-\\(?:" | |
707 ;; basic Lisp stuff | |
708 "compiler-macro\\|function\\|function-when-void\\|modify-macro\\|" | |
709 "setf-method\\|" | |
710 ;; obsolete/compatible support, XEmacs-specific | |
711 "compatible-function-alias\\|obsolete-function-alias\\|" | |
712 ;; XEmacs-specific, supporting stuff inside of XEmacs | |
713 "ccl-program\\|device-method\\*?\\|prefix-command\\|skeleton" | |
714 "\\)\\)\\|" | |
715 ;; Structure declarations. | |
716 "\\(class\\|struct\\|type\\)\\)\\>") | |
717 "Regular expression to match the function and type keywords used in Lisp. | |
718 This matches, for example, the string \"defun\", as well as defsetf, | |
719 defsubst*, define-prefix-command, etc. Match string 1 matches everything | |
720 but the three-letter \"def\" string at the beginning. Match string 2 | |
721 matches everything after that, when it's *NOT* a \"type\" declaration -- | |
722 which includes defclass, defstruct, and deftype. Match string 3 is similar | |
723 to match string 2 in that it matches everything after the \"def\", when | |
724 \(and only when) the keyword matched *IS* a type declaration. You can use | |
725 match strings 2 and 3 to easily determine whether a function or type was | |
726 matched. The regex is terminated with a \\\> so that there must be a | |
727 word-end; i.e. defunbbb won't match.") | |
728 | |
729 (defvar lisp-flet-regexp | |
730 "(\\(flet\\|macrolet\\|labels\\)\\(\\s-\\|\n\\)") | |
731 | |
428 | 732 (defun lisp-indent-function (indent-point state) |
733 ;; free reference to `calculate-lisp-indent-last-sexp' | |
734 ;; in #'calculate-lisp-indent | |
735 (let ((normal-indent (current-column))) | |
736 (goto-char (1+ (elt state 1))) | |
737 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t) | |
738 (if (and (elt state 2) | |
739 (not (looking-at "\\sw\\|\\s_"))) | |
629 | 740 ;; car of form doesn't seem to be a symbol |
428 | 741 (progn |
742 (if (not (> (save-excursion (forward-line 1) (point)) | |
743 calculate-lisp-indent-last-sexp)) | |
744 (progn (goto-char calculate-lisp-indent-last-sexp) | |
745 (beginning-of-line) | |
746 (parse-partial-sexp (point) | |
747 calculate-lisp-indent-last-sexp 0 t))) | |
748 ;; Indent under the list or under the first sexp on the same | |
749 ;; line as calculate-lisp-indent-last-sexp. Note that first | |
750 ;; thing on that line has to be complete sexp since we are | |
751 ;; inside the innermost containing sexp. | |
752 (backward-prefix-chars) | |
753 (current-column)) | |
872 | 754 |
755 ;; Now come a bunch of ad-hoc checks to see if we're in particular | |
756 ;; places (defining an flet function, in the argument list of a | |
757 ;; regular or flet function, in a quoted list, etc.) where the | |
758 ;; regular indenting doesn't work well. | |
759 | |
760 ;; #### All this stuff here should be generalized so that | |
761 ;; you can specify, for various functions, how you want | |
762 ;; particular arguments handled -- in some way that works | |
763 ;; recursively, so it can handle flet and such. | |
764 | |
765 (let* ((function (buffer-substring (point) | |
428 | 766 (progn (forward-sexp 1) (point)))) |
872 | 767 (quote (condition-case nil |
768 (save-excursion | |
769 (backward-up-list 1) | |
770 (memq (char-before (point)) '(?' ?`))))) | |
771 method) | |
772 (cond | |
773 ;; if we're indenting a quoted list, and the first word is not | |
774 ;; lambda, line up second line below first with no indentation, | |
775 ;; so that property lists indent correctly. | |
776 ((and quote (not (equal function "lambda"))) | |
777 (setq method 'lisp-indent-quoteform)) | |
778 ;; do the same if we're indenting the arg list of a def* form. | |
779 ((let ((p (point))) | |
780 (condition-case nil | |
442 | 781 (save-excursion |
872 | 782 (backward-up-list 2) |
783 ;; make sure we're indeed the second argument of the | |
784 ;; def* form. | |
785 (and (eq (point) (save-excursion | |
786 (goto-char p) | |
787 (backward-up-list 1) | |
788 (backward-sexp 2) | |
789 (1- (point)))) | |
790 ;; check to see that the function is a def* type | |
791 (eq (char-after) ?\() | |
792 (progn (forward-char 1) t) | |
793 (looking-at lisp-function-and-type-regexp) | |
794 ;; defstruct may have slot option specs, which | |
795 ;; should probably be reverse-indented like | |
796 ;; normal, because the slot name is the first | |
797 ;; in the list. #### Review this. | |
798 (not (equal (match-string 0) "defstruct")))) | |
799 (error nil))) | |
800 (setq method 'lisp-indent-quoteform)) | |
801 | |
802 ;; handle functions in flet forms | |
803 ((let ((p (point))) | |
804 (condition-case nil | |
805 (save-excursion | |
806 (backward-up-list 3) | |
807 ;; make sure we're indeed a function, i.e. inside the | |
808 ;; first form after the flet. | |
809 (and (eq (point) (save-excursion | |
810 (goto-char p) | |
811 (backward-up-list 2) | |
812 (backward-sexp 1) | |
813 (1- (point)))) | |
814 (looking-at lisp-flet-regexp))) | |
815 (error nil))) | |
816 (setq method 'defun)) | |
817 | |
818 ;; handle the arg lists in functions in flet forms | |
819 ((let ((p (point))) | |
820 (condition-case nil | |
821 (save-excursion | |
822 (backward-up-list 2) | |
823 ;; make sure we're indeed the arg list -- i.e. the first | |
824 ;; element after the function name. | |
825 (and (eq (point) (save-excursion | |
826 (goto-char p) | |
827 (backward-up-list 1) | |
828 (backward-sexp 1) | |
829 (1- (point)))) | |
830 (progn | |
831 (backward-up-list 2) | |
832 (looking-at lisp-flet-regexp)))) | |
833 (error nil))) | |
834 (setq method 'lisp-indent-quoteform)) | |
835 (t | |
836 (setq method | |
837 (or (get (intern-soft function) 'lisp-indent-function) | |
838 (get (intern-soft function) 'lisp-indent-hook))))) | |
428 | 839 (cond ((or (eq method 'defun) |
840 (and (null method) | |
841 (> (length function) 3) | |
842 (string-match "\\`def" function))) | |
843 (lisp-indent-defform state indent-point)) | |
844 ((integerp method) | |
845 (lisp-indent-specform method state | |
846 indent-point normal-indent)) | |
847 (method | |
4942
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
848 (funcall method state indent-point)) |
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
849 ((and (> (length function) 5) |
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
850 (string-match "\\`with-" function)) |
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
851 (lisp-indent-specform 0 state indent-point normal-indent |
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
852 t))))))) |
428 | 853 |
872 | 854 (defun lisp-indent-quoteform (state indent-point) |
855 (goto-char (car (cdr state))) | |
856 (forward-line 1) | |
857 (if (> (point) (car (cdr (cdr state)))) | |
858 (progn | |
859 (goto-char (car (cdr state))) | |
860 (+ 1 (current-column))))) | |
861 | |
428 | 862 (defvar lisp-body-indent 2 |
863 "Number of columns to indent the second line of a `(def...)' form.") | |
864 | |
4958
e4bbe5622a80
finish doc comment on `lisp-indent-specform'
Ben Wing <ben@xemacs.org>
parents:
4942
diff
changeset
|
865 ;; Calculate the appropriate indentation for a special form (e.g. defun) or |
e4bbe5622a80
finish doc comment on `lisp-indent-specform'
Ben Wing <ben@xemacs.org>
parents:
4942
diff
changeset
|
866 ;; a macro that behaves like a special form (e.g. with-temp-buffer). |
e4bbe5622a80
finish doc comment on `lisp-indent-specform'
Ben Wing <ben@xemacs.org>
parents:
4942
diff
changeset
|
867 ;; Return value is as for `calculate-lisp-indent' (q.v.). |
e4bbe5622a80
finish doc comment on `lisp-indent-specform'
Ben Wing <ben@xemacs.org>
parents:
4942
diff
changeset
|
868 ;; |
4942
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
869 ;; COUNT is the number of "distinguished" forms (e.g. arguments before the |
4958
e4bbe5622a80
finish doc comment on `lisp-indent-specform'
Ben Wing <ben@xemacs.org>
parents:
4942
diff
changeset
|
870 ;; "body", in a macro taking a body as its last argument). |
e4bbe5622a80
finish doc comment on `lisp-indent-specform'
Ben Wing <ben@xemacs.org>
parents:
4942
diff
changeset
|
871 ;; |
e4bbe5622a80
finish doc comment on `lisp-indent-specform'
Ben Wing <ben@xemacs.org>
parents:
4942
diff
changeset
|
872 ;; INDENT-POINT is usually the value of (point) at the beginning of the |
e4bbe5622a80
finish doc comment on `lisp-indent-specform'
Ben Wing <ben@xemacs.org>
parents:
4942
diff
changeset
|
873 ;; line containing the form to be indented. |
e4bbe5622a80
finish doc comment on `lisp-indent-specform'
Ben Wing <ben@xemacs.org>
parents:
4942
diff
changeset
|
874 ;; |
e4bbe5622a80
finish doc comment on `lisp-indent-specform'
Ben Wing <ben@xemacs.org>
parents:
4942
diff
changeset
|
875 ;; STATE is the result of calling (parse-partial-sexp (point) INDENT-POINT) |
e4bbe5622a80
finish doc comment on `lisp-indent-specform'
Ben Wing <ben@xemacs.org>
parents:
4942
diff
changeset
|
876 ;; when (point) is sitting on (i.e. just before) the outermost containing |
e4bbe5622a80
finish doc comment on `lisp-indent-specform'
Ben Wing <ben@xemacs.org>
parents:
4942
diff
changeset
|
877 ;; left paren for the form to be indented. |
e4bbe5622a80
finish doc comment on `lisp-indent-specform'
Ben Wing <ben@xemacs.org>
parents:
4942
diff
changeset
|
878 ;; |
e4bbe5622a80
finish doc comment on `lisp-indent-specform'
Ben Wing <ben@xemacs.org>
parents:
4942
diff
changeset
|
879 ;; NORMAL-INDENT is the amount of "full" indentation used for arguments |
e4bbe5622a80
finish doc comment on `lisp-indent-specform'
Ben Wing <ben@xemacs.org>
parents:
4942
diff
changeset
|
880 ;; that aren't given some special indentation (usually less), and normally |
e4bbe5622a80
finish doc comment on `lisp-indent-specform'
Ben Wing <ben@xemacs.org>
parents:
4942
diff
changeset
|
881 ;; it lines up with the first word after the function name. forms are |
e4bbe5622a80
finish doc comment on `lisp-indent-specform'
Ben Wing <ben@xemacs.org>
parents:
4942
diff
changeset
|
882 ;; indented as follows: |
4942
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
883 ;; |
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
884 ;; -- The first and second distinguished forms are given 2 times body indent |
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
885 ;; (determined by `lisp-body-indent') |
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
886 ;; -- Other distinguished forms are given normal indent |
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
887 ;; -- The first nondistinguished form (the body, usually) is given body indent |
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
888 ;; -- Normally, any further nondistinguished forms are given normal indent |
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
889 ;; -- However, if INDENT-REMAINING-AS-BODY was specified, any further |
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
890 ;; nondistinguished forms are given body indent. This is useful when the |
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
891 ;; number of distinguished forms isn't known or may vary. |
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
892 |
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
893 (defun lisp-indent-specform (count state indent-point normal-indent |
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
894 &optional indent-remaining-as-body) |
428 | 895 (let ((containing-form-start (elt state 1)) |
896 (i count) | |
897 body-indent containing-form-column) | |
898 ;; Move to the start of containing form, calculate indentation | |
899 ;; to use for non-distinguished forms (> count), and move past the | |
900 ;; function symbol. lisp-indent-function guarantees that there is at | |
901 ;; least one word or symbol character following open paren of containing | |
902 ;; form. | |
903 (goto-char containing-form-start) | |
904 (setq containing-form-column (current-column)) | |
905 (setq body-indent (+ lisp-body-indent containing-form-column)) | |
906 (forward-char 1) | |
907 (forward-sexp 1) | |
908 ;; Now find the start of the last form. | |
909 (parse-partial-sexp (point) indent-point 1 t) | |
910 (while (and (< (point) indent-point) | |
911 (condition-case () | |
912 (progn | |
913 (setq count (1- count)) | |
914 (forward-sexp 1) | |
915 (parse-partial-sexp (point) indent-point 1 t)) | |
916 (error nil)))) | |
917 ;; Point is sitting on first character of last (or count) sexp. | |
918 (if (> count 0) | |
919 ;; A distinguished form. If it is the first or second form use double | |
920 ;; lisp-body-indent, else normal indent. With lisp-body-indent bound | |
921 ;; to 2 (the default), this just happens to work the same with if as | |
922 ;; the older code, but it makes unwind-protect, condition-case, | |
923 ;; with-output-to-temp-buffer, et. al. much more tasteful. The older, | |
924 ;; less hacked, behavior can be obtained by replacing below with | |
925 ;; (list normal-indent containing-form-start). | |
926 (if (<= (- i count) 1) | |
927 (list (+ containing-form-column (* 2 lisp-body-indent)) | |
928 containing-form-start) | |
929 (list normal-indent containing-form-start)) | |
930 ;; A non-distinguished form. Use body-indent if there are no | |
931 ;; distinguished forms and this is the first undistinguished form, | |
932 ;; or if this is the first undistinguished form and the preceding | |
933 ;; distinguished form has indentation at least as great as body-indent. | |
934 (if (or (and (= i 0) (= count 0)) | |
4942
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
935 (and (or (= count 0) indent-remaining-as-body) |
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
936 (<= body-indent normal-indent))) |
428 | 937 body-indent |
938 normal-indent)))) | |
939 | |
940 (defun lisp-indent-defform (state indent-point) | |
941 (goto-char (car (cdr state))) | |
942 (forward-line 1) | |
943 (if (> (point) (car (cdr (cdr state)))) | |
944 (progn | |
945 (goto-char (car (cdr state))) | |
946 (+ lisp-body-indent (current-column))))) | |
947 | |
948 | |
949 ;; (put 'progn 'lisp-indent-function 0), say, causes progn to be indented | |
950 ;; like defun if the first form is placed on the next line, otherwise | |
951 ;; it is indented like any other form (i.e. forms line up under first). | |
952 | |
776 | 953 ;; NOTE: All def* functions are handled specially, no need to put them here. |
954 ;; NOTE: All cl-* constructs are handled in cl.el. | |
955 | |
428 | 956 (put 'lambda 'lisp-indent-function 'defun) |
957 (put 'autoload 'lisp-indent-function 'defun) | |
958 (put 'progn 'lisp-indent-function 0) | |
959 (put 'prog1 'lisp-indent-function 1) | |
960 (put 'prog2 'lisp-indent-function 2) | |
961 (put 'save-excursion 'lisp-indent-function 0) | |
962 (put 'save-window-excursion 'lisp-indent-function 0) | |
963 (put 'save-selected-window 'lisp-indent-function 0) | |
442 | 964 (put 'with-selected-window 'lisp-indent-function 1) |
428 | 965 (put 'save-selected-frame 'lisp-indent-function 0) |
966 (put 'with-selected-frame 'lisp-indent-function 1) | |
872 | 967 (put 'save-selected-device 'lisp-indent-function 0) |
968 (put 'with-selected-device 'lisp-indent-function 1) | |
428 | 969 (put 'save-restriction 'lisp-indent-function 0) |
970 (put 'save-match-data 'lisp-indent-function 0) | |
971 (put 'let 'lisp-indent-function 1) | |
972 (put 'let* 'lisp-indent-function 1) | |
973 (put 'let-specifier 'lisp-indent-function 1) | |
974 (put 'while 'lisp-indent-function 1) | |
975 (put 'if 'lisp-indent-function 2) | |
976 (put 'catch 'lisp-indent-function 1) | |
977 (put 'condition-case 'lisp-indent-function 2) | |
460 | 978 (put 'handler-case 'lisp-indent-function 1) |
979 (put 'handler-bind 'lisp-indent-function 1) | |
428 | 980 (put 'call-with-condition-handler 'lisp-indent-function 2) |
981 (put 'unwind-protect 'lisp-indent-function 1) | |
982 (put 'save-current-buffer 'lisp-indent-function 0) | |
983 (put 'with-current-buffer 'lisp-indent-function 1) | |
984 (put 'with-string-as-buffer-contents 'lisp-indent-function 1) | |
985 (put 'with-temp-file 'lisp-indent-function 1) | |
986 (put 'with-temp-buffer 'lisp-indent-function 0) | |
987 (put 'with-output-to-string 'lisp-indent-function 0) | |
988 (put 'with-output-to-temp-buffer 'lisp-indent-function 1) | |
460 | 989 (put 'with-slots 'lisp-indent-function 2) |
990 (put 'with-open-file 'lisp-indent-function 1) | |
991 (put 'with-open-stream 'lisp-indent-function 1) | |
428 | 992 (put 'eval-after-load 'lisp-indent-function 1) |
993 (put 'display-message 'lisp-indent-function 1) | |
994 (put 'display-warning 'lisp-indent-function 1) | |
995 (put 'lmessage 'lisp-indent-function 2) | |
996 (put 'lwarn 'lisp-indent-function 2) | |
997 (put 'global-set-key 'lisp-indent-function 1) | |
460 | 998 (put 'print-unreadable-object 'lisp-indent-function 1) |
428 | 999 |
1000 (defun indent-sexp (&optional endpos) | |
1001 "Indent each line of the list starting just after point. | |
1002 If optional arg ENDPOS is given, indent each line, stopping when | |
1003 ENDPOS is encountered." | |
1004 (interactive) | |
1005 (let ((indent-stack (list nil)) | |
1006 (next-depth 0) | |
1007 ;; If ENDPOS is non-nil, use nil as STARTING-POINT | |
1008 ;; so that calculate-lisp-indent will find the beginning of | |
1009 ;; the defun we are in. | |
1010 ;; If ENDPOS is nil, it is safe not to scan before point | |
1011 ;; since every line we indent is more deeply nested than point is. | |
1012 (starting-point (if endpos nil (point))) | |
1013 (last-point (point)) | |
1014 last-depth bol outer-loop-done inner-loop-done state this-indent) | |
1015 (or endpos | |
1016 ;; Get error now if we don't have a complete sexp after point. | |
1017 (save-excursion (forward-sexp 1))) | |
1018 (save-excursion | |
1019 (setq outer-loop-done nil) | |
1020 (while (if endpos (< (point) endpos) | |
1021 (not outer-loop-done)) | |
1022 (setq last-depth next-depth | |
1023 inner-loop-done nil) | |
1024 ;; Parse this line so we can learn the state | |
1025 ;; to indent the next line. | |
1026 ;; This inner loop goes through only once | |
1027 ;; unless a line ends inside a string. | |
1028 (while (and (not inner-loop-done) | |
1029 (not (setq outer-loop-done (eobp)))) | |
1030 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point)) | |
1031 nil nil state)) | |
1032 (setq next-depth (car state)) | |
1033 ;; If the line contains a comment other than the sort | |
1034 ;; that is indented like code, | |
1035 ;; indent it now with indent-for-comment. | |
1036 ;; Comments indented like code are right already. | |
1037 ;; In any case clear the in-comment flag in the state | |
1038 ;; because parse-partial-sexp never sees the newlines. | |
1039 (if (car (nthcdr 4 state)) | |
1040 (progn (indent-for-comment) | |
1041 (end-of-line) | |
1042 (setcar (nthcdr 4 state) nil))) | |
1043 ;; If this line ends inside a string, | |
1044 ;; go straight to next line, remaining within the inner loop, | |
1045 ;; and turn off the \-flag. | |
1046 (if (car (nthcdr 3 state)) | |
1047 (progn | |
1048 (forward-line 1) | |
1049 (setcar (nthcdr 5 state) nil)) | |
1050 (setq inner-loop-done t))) | |
1051 (and endpos | |
1052 (<= next-depth 0) | |
1053 (progn | |
1054 (setq indent-stack (append indent-stack | |
1055 (make-list (- next-depth) nil)) | |
1056 last-depth (- last-depth next-depth) | |
1057 next-depth 0))) | |
1058 (or outer-loop-done endpos | |
1059 (setq outer-loop-done (<= next-depth 0))) | |
1060 (if outer-loop-done | |
1061 (forward-line 1) | |
1062 (while (> last-depth next-depth) | |
1063 (setq indent-stack (cdr indent-stack) | |
1064 last-depth (1- last-depth))) | |
1065 (while (< last-depth next-depth) | |
1066 (setq indent-stack (cons nil indent-stack) | |
1067 last-depth (1+ last-depth))) | |
1068 ;; Now go to the next line and indent it according | |
1069 ;; to what we learned from parsing the previous one. | |
1070 (forward-line 1) | |
1071 (setq bol (point)) | |
1072 (skip-chars-forward " \t") | |
1073 ;; But not if the line is blank, or just a comment | |
1074 ;; (except for double-semi comments; indent them as usual). | |
1075 (if (or (eobp) (looking-at "\\s<\\|\n")) | |
1076 nil | |
1077 (if (and (car indent-stack) | |
1078 (>= (car indent-stack) 0)) | |
1079 (setq this-indent (car indent-stack)) | |
1080 (let ((val (calculate-lisp-indent | |
1081 (if (car indent-stack) (- (car indent-stack)) | |
1082 starting-point)))) | |
1083 (if (integerp val) | |
1084 (setcar indent-stack | |
1085 (setq this-indent val)) | |
1086 (setcar indent-stack (- (car (cdr val)))) | |
1087 (setq this-indent (car val))))) | |
1088 (if (/= (current-column) this-indent) | |
1089 (progn (delete-region bol (point)) | |
1090 (indent-to this-indent))))) | |
1091 (or outer-loop-done | |
1092 (setq outer-loop-done (= (point) last-point)) | |
1093 (setq last-point (point))))))) | |
1094 | |
1095 ;; Indent every line whose first char is between START and END inclusive. | |
1096 (defun lisp-indent-region (start end) | |
1097 (save-excursion | |
1098 (let ((endmark (copy-marker end))) | |
1099 (goto-char start) | |
1100 (and (bolp) (not (eolp)) | |
1101 (lisp-indent-line)) | |
1102 (indent-sexp endmark) | |
1103 (set-marker endmark nil)))) | |
1104 | |
1105 ;;;; Lisp paragraph filling commands. | |
1106 | |
1107 (defun lisp-fill-paragraph (&optional justify) | |
1108 "Like \\[fill-paragraph], but handle Emacs Lisp comments. | |
1109 If any of the current line is a comment, fill the comment or the | |
1110 paragraph of it that point is in, preserving the comment's indentation | |
1111 and initial semicolons." | |
1112 (interactive "P") | |
1113 (let ( | |
1114 ;; Non-nil if the current line contains a comment. | |
1115 has-comment | |
1116 | |
1117 ;; Non-nil if the current line contains code and a comment. | |
1118 has-code-and-comment | |
1119 | |
1120 ;; If has-comment, the appropriate fill-prefix for the comment. | |
1121 comment-fill-prefix | |
1122 ) | |
1123 | |
1124 ;; Figure out what kind of comment we are looking at. | |
1125 (save-excursion | |
1126 (beginning-of-line) | |
1127 (cond | |
1128 | |
1129 ;; A line with nothing but a comment on it? | |
1130 ((looking-at "[ \t]*;[; \t]*") | |
1131 (setq has-comment t | |
1132 comment-fill-prefix (buffer-substring (match-beginning 0) | |
1133 (match-end 0)))) | |
1134 | |
1135 ;; A line with some code, followed by a comment? Remember that the | |
1136 ;; semi which starts the comment shouldn't be part of a string or | |
1137 ;; character. | |
1138 ;; XEmacs Try this the FSF and see if it works. | |
1139 ; ((progn | |
1140 ; (while (not (looking-at ";\\|$")) | |
1141 ; (skip-chars-forward "^;\n\"\\\\?") | |
1142 ; (cond | |
1143 ; ((eq (char-after (point)) ?\\) (forward-char 2)) | |
1144 ; ((memq (char-after (point)) '(?\" ??)) (forward-sexp 1)))) | |
1145 ; (looking-at ";+[\t ]*")) | |
1146 ; (setq has-comment t) | |
1147 ((condition-case nil | |
1148 (save-restriction | |
1149 (narrow-to-region (point-min) | |
1150 (save-excursion (end-of-line) (point))) | |
1151 (while (not (looking-at ";\\|$")) | |
1152 (skip-chars-forward "^;\n\"\\\\?") | |
1153 (cond | |
1154 ((eq (char-after (point)) ?\\) (forward-char 2)) | |
1155 ((memq (char-after (point)) '(?\" ??)) (forward-sexp 1)))) | |
844 | 1156 ;; don't do comment filling in a string, or we will mess up |
1157 ;; doc strings and other places where semicolons are used. | |
1158 (and (not (eq 'string (buffer-syntactic-context))) | |
1159 (looking-at ";+[\t ]*"))) | |
428 | 1160 (error nil)) |
1161 (setq has-comment t has-code-and-comment t) | |
1162 (setq comment-fill-prefix | |
1163 (concat (make-string (/ (current-column) 8) ?\t) | |
1164 (make-string (% (current-column) 8) ?\ ) | |
1165 (buffer-substring (match-beginning 0) (match-end 0))))))) | |
1166 | |
1167 (if (not has-comment) | |
1168 (fill-paragraph justify) | |
1169 | |
1170 ;; Narrow to include only the comment, and then fill the region. | |
1171 (save-excursion | |
1172 (save-restriction | |
1173 (beginning-of-line) | |
1174 (narrow-to-region | |
1175 ;; Find the first line we should include in the region to fill. | |
1176 (save-excursion | |
1177 (while (and (zerop (forward-line -1)) | |
1178 (looking-at "^[ \t]*;"))) | |
1179 ;; We may have gone too far. Go forward again. | |
1180 (or (looking-at ".*;") | |
1181 (forward-line 1)) | |
1182 (point)) | |
1183 ;; Find the beginning of the first line past the region to fill. | |
1184 (save-excursion | |
1185 (while (progn (forward-line 1) | |
1186 (looking-at "^[ \t]*;"))) | |
1187 (point))) | |
1188 | |
1189 ;; Lines with only semicolons on them can be paragraph boundaries. | |
1190 (let* ((paragraph-start (concat paragraph-start "\\|[ \t;]*$")) | |
1191 (paragraph-separate (concat paragraph-start "\\|[ \t;]*$")) | |
1192 (paragraph-ignore-fill-prefix nil) | |
1193 (fill-prefix comment-fill-prefix) | |
1194 (after-line (if has-code-and-comment | |
1195 (save-excursion | |
1196 (forward-line 1) (point)))) | |
1197 (end (progn | |
1198 (forward-paragraph) | |
1199 (or (bolp) (newline 1)) | |
1200 (point))) | |
1201 ;; If this comment starts on a line with code, | |
1202 ;; include that like in the filling. | |
1203 (beg (progn (backward-paragraph) | |
1204 (if (eq (point) after-line) | |
1205 (forward-line -1)) | |
1206 (point)))) | |
1207 (fill-region-as-paragraph beg end | |
1208 justify nil | |
1209 (save-excursion | |
1210 (goto-char beg) | |
1211 (if (looking-at fill-prefix) | |
1212 nil | |
1213 (re-search-forward comment-start-skip) | |
1214 (point)))))))) | |
1215 t)) | |
1216 | |
1217 (defun indent-code-rigidly (start end arg &optional nochange-regexp) | |
1218 "Indent all lines of code, starting in the region, sideways by ARG columns. | |
1219 Does not affect lines starting inside comments or strings, assuming that | |
1220 the start of the region is not inside them. | |
1221 | |
1222 Called from a program, takes args START, END, COLUMNS and NOCHANGE-REGEXP. | |
1223 The last is a regexp which, if matched at the beginning of a line, | |
1224 means don't indent that line." | |
1225 (interactive "r\np") | |
1226 (let (state) | |
1227 (save-excursion | |
1228 (goto-char end) | |
1229 (setq end (point-marker)) | |
1230 (goto-char start) | |
1231 (or (bolp) | |
1232 (setq state (parse-partial-sexp (point) | |
1233 (progn | |
1234 (forward-line 1) (point)) | |
1235 nil nil state))) | |
1236 (while (< (point) end) | |
1237 (or (car (nthcdr 3 state)) | |
1238 (and nochange-regexp | |
1239 (looking-at nochange-regexp)) | |
1240 ;; If line does not start in string, indent it | |
1241 (let ((indent (current-indentation))) | |
1242 (delete-region (point) (progn (skip-chars-forward " \t") (point))) | |
1243 (or (eolp) | |
1244 (indent-to (max 0 (+ indent arg)) 0)))) | |
1245 (setq state (parse-partial-sexp (point) | |
1246 (progn | |
1247 (forward-line 1) (point)) | |
1248 nil nil state)))))) | |
1249 | |
1250 (provide 'lisp-mode) | |
1251 | |
1252 ;;; lisp-mode.el ends here |