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