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 (define-key lisp-mode-map "\e\C-x" 'lisp-send-defun)
|
|
344 ;; gag, no. use ilisp. -jwz
|
|
345 ;; (define-key lisp-mode-map "\C-c\C-z" 'run-lisp)
|
|
346 )
|
|
347
|
|
348 (defun lisp-mode ()
|
613
|
349 "Major mode for editing Lisp code for Lisps other than Emacs Lisp.
|
428
|
350 Commands:
|
|
351 Delete converts tabs to spaces as it moves back.
|
|
352 Blank lines separate paragraphs. Semicolons start comments.
|
|
353 \\{lisp-mode-map}
|
|
354 Note that `run-lisp' may be used either to start an inferior Lisp job
|
|
355 or to switch back to an existing one.
|
|
356
|
|
357 Entry to this mode calls the value of `lisp-mode-hook'
|
|
358 if that value is non-nil."
|
|
359 (interactive)
|
|
360 (kill-all-local-variables)
|
|
361 (use-local-map lisp-mode-map)
|
|
362 (setq major-mode 'lisp-mode)
|
|
363 (setq mode-name "Lisp")
|
|
364 (lisp-mode-variables t)
|
|
365 (set-syntax-table lisp-mode-syntax-table)
|
|
366 (run-hooks 'lisp-mode-hook))
|
|
367
|
|
368 ;; This will do unless shell.el is loaded.
|
|
369 ;; XEmacs change
|
|
370 (defun lisp-send-defun ()
|
|
371 "Send the current defun to the Lisp process made by \\[run-lisp]."
|
|
372 (interactive)
|
|
373 (error "Process lisp does not exist"))
|
|
374
|
|
375 ;; XEmacs change: emacs-lisp-mode-map is a more appropriate parent.
|
|
376 (defvar lisp-interaction-mode-map ()
|
|
377 "Keymap for Lisp Interaction mode.
|
|
378 All commands in `shared-lisp-mode-map' are inherited by this map.")
|
|
379
|
|
380 (if lisp-interaction-mode-map
|
|
381 ()
|
|
382 ;; XEmacs set keymap our way
|
|
383 (setq lisp-interaction-mode-map (make-sparse-keymap))
|
|
384 (set-keymap-name lisp-interaction-mode-map 'lisp-interaction-mode-map)
|
|
385 (set-keymap-parents lisp-interaction-mode-map (list emacs-lisp-mode-map))
|
|
386 (define-key lisp-interaction-mode-map "\e\C-x" 'eval-defun)
|
|
387 (define-key lisp-interaction-mode-map "\e\t" 'lisp-complete-symbol)
|
|
388 (define-key lisp-interaction-mode-map "\n" 'eval-print-last-sexp))
|
|
389
|
|
390 (defun lisp-interaction-mode ()
|
|
391 "Major mode for typing and evaluating Lisp forms.
|
|
392 Like Lisp mode except that \\[eval-print-last-sexp] evals the Lisp expression
|
|
393 before point, and prints its value into the buffer, advancing point.
|
|
394
|
|
395 Commands:
|
|
396 Delete converts tabs to spaces as it moves back.
|
|
397 Paragraphs are separated only by blank lines.
|
|
398 Semicolons start comments.
|
|
399 \\{lisp-interaction-mode-map}
|
|
400 Entry to this mode calls the value of `lisp-interaction-mode-hook'
|
|
401 if that value is non-nil."
|
|
402 (interactive)
|
|
403 (kill-all-local-variables)
|
|
404 (use-local-map lisp-interaction-mode-map)
|
|
405 (setq major-mode 'lisp-interaction-mode)
|
|
406 (setq mode-name "Lisp Interaction")
|
442
|
407 (setq mode-popup-menu lisp-interaction-mode-popup-menu)
|
|
408 (if (and (featurep 'menubar)
|
|
409 current-menubar)
|
|
410 (progn
|
|
411 ;; make a local copy of the menubar, so our modes don't
|
|
412 ;; change the global menubar
|
|
413 (set-buffer-menubar current-menubar)
|
|
414 (add-submenu nil lisp-interaction-mode-menubar-menu)))
|
428
|
415 (set-syntax-table emacs-lisp-mode-syntax-table)
|
|
416 (lisp-mode-variables nil)
|
|
417 (run-hooks 'lisp-interaction-mode-hook))
|
|
418
|
|
419 (defun eval-print-last-sexp ()
|
|
420 "Evaluate sexp before point; print value into current buffer."
|
|
421 (interactive)
|
|
422 (let ((standard-output (current-buffer)))
|
|
423 (terpri)
|
|
424 (eval-last-sexp t)
|
|
425 (terpri)))
|
|
426
|
|
427 ;; XEmacs change
|
|
428 (defcustom eval-interactive-verbose t
|
|
429 "*Non-nil means that interactive evaluation can print messages.
|
|
430 The messages are printed when the expression is treated differently
|
2116
|
431 using `\\[eval-last-sexp]' and `\\[eval-defun]' than it would have
|
|
432 been treated noninteractively.
|
428
|
433
|
|
434 The printed messages are \"defvar treated as defconst\" and \"defcustom
|
|
435 evaluation forced\". See `eval-interactive' for more details."
|
|
436 :type 'boolean
|
|
437 :group 'lisp)
|
|
438
|
|
439 (defun eval-interactive (expr)
|
|
440 "Like `eval' except that it transforms defvars to defconsts.
|
|
441 The evaluation of defcustom forms is forced."
|
|
442 (cond ((and (eq (car-safe expr) 'defvar)
|
|
443 (> (length expr) 2))
|
|
444 (eval (cons 'defconst (cdr expr)))
|
|
445 (when eval-interactive-verbose
|
|
446 (message "defvar treated as defconst")
|
|
447 (sit-for 1)
|
|
448 (message ""))
|
|
449 (nth 1 expr))
|
|
450 ((and (eq (car-safe expr) 'defcustom)
|
|
451 (> (length expr) 2)
|
|
452 (default-boundp (nth 1 expr)))
|
|
453 ;; Force variable to be bound
|
|
454 ;; #### defcustom might specify a different :set method.
|
|
455 (set-default (nth 1 expr) (eval (nth 2 expr)))
|
|
456 ;; And evaluate the defcustom
|
|
457 (eval expr)
|
|
458 (when eval-interactive-verbose
|
|
459 (message "defcustom evaluation forced")
|
|
460 (sit-for 1)
|
|
461 (message ""))
|
|
462 (nth 1 expr))
|
|
463 (t
|
|
464 (eval expr))))
|
|
465
|
|
466 ;; XEmacs change, based on Bob Weiner suggestion
|
|
467 (defun eval-last-sexp (eval-last-sexp-arg-internal) ;dynamic scoping wonderment
|
|
468 "Evaluate sexp before point; print value in minibuffer.
|
|
469 With argument, print output into current buffer."
|
|
470 (interactive "P")
|
|
471 (let ((standard-output (if eval-last-sexp-arg-internal (current-buffer) t))
|
|
472 (opoint (point))
|
|
473 ignore-quotes)
|
|
474 (prin1 (eval-interactive
|
|
475 (letf (((syntax-table) emacs-lisp-mode-syntax-table))
|
|
476 (save-excursion
|
|
477 ;; If this sexp appears to be enclosed in `...' then
|
|
478 ;; ignore the surrounding quotes.
|
|
479 (setq ignore-quotes (or (eq (char-after) ?\')
|
|
480 (eq (char-before) ?\')))
|
|
481 (forward-sexp -1)
|
|
482 ;; vladimir@cs.ualberta.ca 30-Jul-1997: skip ` in
|
|
483 ;; `variable' so that the value is returned, not the
|
|
484 ;; name.
|
|
485 (if (and ignore-quotes
|
|
486 (eq (char-after) ?\`))
|
|
487 (forward-char))
|
|
488 (save-restriction
|
|
489 (narrow-to-region (point-min) opoint)
|
|
490 (let ((expr (read (current-buffer))))
|
|
491 (if (eq (car-safe expr) 'interactive)
|
|
492 ;; If it's an (interactive ...) form, it's
|
|
493 ;; more useful to show how an interactive call
|
|
494 ;; would use it.
|
|
495 `(call-interactively
|
|
496 (lambda (&rest args)
|
|
497 ,expr args))
|
|
498 expr)))))))))
|
|
499
|
|
500 (defun eval-defun (eval-defun-arg-internal)
|
|
501 "Evaluate defun that point is in or before.
|
|
502 Print value in minibuffer.
|
|
503 With argument, insert value in current buffer after the defun."
|
|
504 (interactive "P")
|
|
505 (let ((standard-output (if eval-defun-arg-internal (current-buffer) t)))
|
|
506 (prin1 (eval-interactive (save-excursion
|
|
507 (end-of-defun)
|
|
508 (beginning-of-defun)
|
|
509 (read (current-buffer)))))))
|
|
510
|
|
511
|
|
512 (defun lisp-comment-indent ()
|
|
513 (if (looking-at "\\s<\\s<\\s<")
|
|
514 (current-column)
|
|
515 (if (looking-at "\\s<\\s<")
|
|
516 ;; #### FSF has:
|
|
517 ;; (let ((tem (or (calculate-lisp-indent) (current-column)))) ...
|
|
518 (let ((tem (calculate-lisp-indent)))
|
|
519 (if (listp tem) (car tem) tem))
|
|
520 (skip-chars-backward " \t")
|
|
521 (max (if (bolp) 0 (1+ (current-column)))
|
|
522 comment-column))))
|
|
523
|
|
524 ;; XEmacs change
|
|
525 (defun lisp-indent-for-comment ()
|
|
526 "Indent this line's comment appropriately, or insert an empty comment.
|
|
527 If adding a new comment on a blank line, use `block-comment-start' instead
|
|
528 of `comment-start' to open the comment."
|
|
529 ;; by Stig@hackvan.com
|
|
530 ;; #### - This functionality, the recognition of block-comment-{start,end},
|
|
531 ;; will perhaps be standardized across modes and move to indent-for-comment.
|
|
532 (interactive)
|
|
533 (if (and block-comment-start
|
|
534 (save-excursion (beginning-of-line) (looking-at "^[ \t]*$")))
|
|
535 (insert block-comment-start))
|
|
536 (indent-for-comment))
|
|
537
|
|
538 (defvar lisp-indent-offset nil)
|
|
539 (defvar lisp-indent-function 'lisp-indent-function)
|
|
540
|
|
541 (defun lisp-indent-line (&optional whole-exp)
|
|
542 "Indent current line as Lisp code.
|
|
543 With argument, indent any additional lines of the same expression
|
|
544 rigidly along with this one."
|
|
545 (interactive "P")
|
|
546 (let ((indent (calculate-lisp-indent)) shift-amt beg end
|
|
547 (pos (- (point-max) (point))))
|
|
548 (beginning-of-line)
|
|
549 (setq beg (point))
|
|
550 (skip-chars-forward " \t")
|
|
551 (if (looking-at "\\s<\\s<\\s<")
|
|
552 ;; Don't alter indentation of a ;;; comment line.
|
|
553 (goto-char (- (point-max) pos))
|
|
554 (if (and (looking-at "\\s<") (not (looking-at "\\s<\\s<")))
|
|
555 ;; Single-semicolon comment lines should be indented
|
|
556 ;; as comment lines, not as code.
|
446
|
557 (progn (indent-for-comment) (backward-char 1))
|
428
|
558 (if (listp indent) (setq indent (car indent)))
|
|
559 (setq shift-amt (- indent (current-column)))
|
|
560 (if (zerop shift-amt)
|
|
561 nil
|
|
562 (delete-region beg (point))
|
|
563 (indent-to indent)))
|
|
564 ;; If initial point was within line's indentation,
|
|
565 ;; position after the indentation. Else stay at same point in text.
|
|
566 (if (> (- (point-max) pos) (point))
|
|
567 (goto-char (- (point-max) pos)))
|
|
568 ;; If desired, shift remaining lines of expression the same amount.
|
|
569 (and whole-exp (not (zerop shift-amt))
|
|
570 (save-excursion
|
|
571 (goto-char beg)
|
|
572 (forward-sexp 1)
|
|
573 (setq end (point))
|
|
574 (goto-char beg)
|
|
575 (forward-line 1)
|
|
576 (setq beg (point))
|
|
577 (> end beg))
|
|
578 (indent-code-rigidly beg end shift-amt)))))
|
|
579
|
|
580 (defvar calculate-lisp-indent-last-sexp)
|
|
581
|
|
582 (defun calculate-lisp-indent (&optional parse-start)
|
|
583 "Return appropriate indentation for current line as Lisp code.
|
|
584 In usual case returns an integer: the column to indent to.
|
|
585 Can instead return a list, whose car is the column to indent to.
|
|
586 This means that following lines at the same level of indentation
|
|
587 should not necessarily be indented the same way.
|
|
588 The second element of the list is the buffer position
|
|
589 of the start of the containing expression."
|
|
590 (save-excursion
|
|
591 (beginning-of-line)
|
|
592 (let ((indent-point (point))
|
|
593 ;; XEmacs change (remove paren-depth)
|
|
594 state ;;paren-depth
|
|
595 ;; setting this to a number inhibits calling hook
|
|
596 (desired-indent nil)
|
|
597 (retry t)
|
|
598 calculate-lisp-indent-last-sexp containing-sexp)
|
|
599 (if parse-start
|
|
600 (goto-char parse-start)
|
|
601 (beginning-of-defun))
|
|
602 ;; Find outermost containing sexp
|
|
603 (while (< (point) indent-point)
|
|
604 (setq state (parse-partial-sexp (point) indent-point 0)))
|
|
605 ;; Find innermost containing sexp
|
|
606 (while (and retry
|
|
607 state
|
|
608 ;; XEmacs change (remove paren-depth)
|
|
609 (> ;;(setq paren-depth (elt state 0))
|
|
610 (elt state 0)
|
|
611 0))
|
|
612 (setq retry nil)
|
|
613 (setq calculate-lisp-indent-last-sexp (elt state 2))
|
|
614 (setq containing-sexp (elt state 1))
|
|
615 ;; Position following last unclosed open.
|
|
616 (goto-char (1+ containing-sexp))
|
|
617 ;; Is there a complete sexp since then?
|
|
618 (if (and calculate-lisp-indent-last-sexp
|
|
619 (> calculate-lisp-indent-last-sexp (point)))
|
|
620 ;; Yes, but is there a containing sexp after that?
|
|
621 (let ((peek (parse-partial-sexp calculate-lisp-indent-last-sexp
|
|
622 indent-point 0)))
|
|
623 (if (setq retry (car (cdr peek))) (setq state peek)))))
|
|
624 (if retry
|
|
625 nil
|
|
626 ;; Innermost containing sexp found
|
|
627 (goto-char (1+ containing-sexp))
|
|
628 (if (not calculate-lisp-indent-last-sexp)
|
|
629 ;; indent-point immediately follows open paren.
|
|
630 ;; Don't call hook.
|
|
631 (setq desired-indent (current-column))
|
|
632 ;; Find the start of first element of containing sexp.
|
|
633 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
|
|
634 (cond ((looking-at "\\s(")
|
|
635 ;; First element of containing sexp is a list.
|
|
636 ;; Indent under that list.
|
|
637 )
|
|
638 ((> (save-excursion (forward-line 1) (point))
|
|
639 calculate-lisp-indent-last-sexp)
|
|
640 ;; This is the first line to start within the containing sexp.
|
|
641 ;; It's almost certainly a function call.
|
|
642 (if (= (point) calculate-lisp-indent-last-sexp)
|
|
643 ;; Containing sexp has nothing before this line
|
|
644 ;; except the first element. Indent under that element.
|
|
645 nil
|
|
646 ;; Skip the first element, find start of second (the first
|
|
647 ;; argument of the function call) and indent under.
|
|
648 (progn (forward-sexp 1)
|
|
649 (parse-partial-sexp (point)
|
|
650 calculate-lisp-indent-last-sexp
|
|
651 0 t)))
|
|
652 (backward-prefix-chars))
|
|
653 (t
|
|
654 ;; Indent beneath first sexp on same line as
|
|
655 ;; calculate-lisp-indent-last-sexp. Again, it's
|
|
656 ;; almost certainly a function call.
|
|
657 (goto-char calculate-lisp-indent-last-sexp)
|
|
658 (beginning-of-line)
|
|
659 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp
|
|
660 0 t)
|
|
661 (backward-prefix-chars)))))
|
|
662 ;; Point is at the point to indent under unless we are inside a string.
|
|
663 ;; Call indentation hook except when overridden by lisp-indent-offset
|
|
664 ;; or if the desired indentation has already been computed.
|
|
665 (let ((normal-indent (current-column)))
|
|
666 (cond ((elt state 3)
|
|
667 ;; Inside a string, don't change indentation.
|
|
668 (goto-char indent-point)
|
|
669 (skip-chars-forward " \t")
|
|
670 (current-column))
|
|
671 (desired-indent)
|
|
672 ((and (boundp 'lisp-indent-function)
|
|
673 lisp-indent-function
|
|
674 (not retry))
|
|
675 (or (funcall lisp-indent-function indent-point state)
|
|
676 normal-indent))
|
|
677 ;; XEmacs change:
|
|
678 ;; lisp-indent-offset shouldn't override lisp-indent-function !
|
|
679 ((and (integerp lisp-indent-offset) containing-sexp)
|
|
680 ;; Indent by constant offset
|
|
681 (goto-char containing-sexp)
|
|
682 (+ normal-indent lisp-indent-offset))
|
|
683 (t
|
|
684 normal-indent))))))
|
|
685
|
872
|
686 (defvar lisp-function-and-type-regexp
|
|
687 (concat "def\\("
|
|
688 ;; def but not define-.
|
|
689 "\\(un\\|advice\\|alias\\|macro\\*?\\|setf\\|subst\\*?\\|"
|
|
690 "-edebug-spec\\|"
|
|
691 ;; CLOS
|
|
692 "method\\|generic\\|"
|
|
693 ;; define-*
|
|
694 "ine-\\(?:"
|
|
695 ;; basic Lisp stuff
|
|
696 "compiler-macro\\|function\\|function-when-void\\|modify-macro\\|"
|
|
697 "setf-method\\|"
|
|
698 ;; obsolete/compatible support, XEmacs-specific
|
|
699 "compatible-function-alias\\|obsolete-function-alias\\|"
|
|
700 ;; XEmacs-specific, supporting stuff inside of XEmacs
|
|
701 "ccl-program\\|device-method\\*?\\|prefix-command\\|skeleton"
|
|
702 "\\)\\)\\|"
|
|
703 ;; Structure declarations.
|
|
704 "\\(class\\|struct\\|type\\)\\)\\>")
|
|
705 "Regular expression to match the function and type keywords used in Lisp.
|
|
706 This matches, for example, the string \"defun\", as well as defsetf,
|
|
707 defsubst*, define-prefix-command, etc. Match string 1 matches everything
|
|
708 but the three-letter \"def\" string at the beginning. Match string 2
|
|
709 matches everything after that, when it's *NOT* a \"type\" declaration --
|
|
710 which includes defclass, defstruct, and deftype. Match string 3 is similar
|
|
711 to match string 2 in that it matches everything after the \"def\", when
|
|
712 \(and only when) the keyword matched *IS* a type declaration. You can use
|
|
713 match strings 2 and 3 to easily determine whether a function or type was
|
|
714 matched. The regex is terminated with a \\\> so that there must be a
|
|
715 word-end; i.e. defunbbb won't match.")
|
|
716
|
|
717 (defvar lisp-flet-regexp
|
|
718 "(\\(flet\\|macrolet\\|labels\\)\\(\\s-\\|\n\\)")
|
|
719
|
428
|
720 (defun lisp-indent-function (indent-point state)
|
|
721 ;; free reference to `calculate-lisp-indent-last-sexp'
|
|
722 ;; in #'calculate-lisp-indent
|
|
723 (let ((normal-indent (current-column)))
|
|
724 (goto-char (1+ (elt state 1)))
|
|
725 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
|
|
726 (if (and (elt state 2)
|
|
727 (not (looking-at "\\sw\\|\\s_")))
|
629
|
728 ;; car of form doesn't seem to be a symbol
|
428
|
729 (progn
|
|
730 (if (not (> (save-excursion (forward-line 1) (point))
|
|
731 calculate-lisp-indent-last-sexp))
|
|
732 (progn (goto-char calculate-lisp-indent-last-sexp)
|
|
733 (beginning-of-line)
|
|
734 (parse-partial-sexp (point)
|
|
735 calculate-lisp-indent-last-sexp 0 t)))
|
|
736 ;; Indent under the list or under the first sexp on the same
|
|
737 ;; line as calculate-lisp-indent-last-sexp. Note that first
|
|
738 ;; thing on that line has to be complete sexp since we are
|
|
739 ;; inside the innermost containing sexp.
|
|
740 (backward-prefix-chars)
|
|
741 (current-column))
|
872
|
742
|
|
743 ;; Now come a bunch of ad-hoc checks to see if we're in particular
|
|
744 ;; places (defining an flet function, in the argument list of a
|
|
745 ;; regular or flet function, in a quoted list, etc.) where the
|
|
746 ;; regular indenting doesn't work well.
|
|
747
|
|
748 ;; #### All this stuff here should be generalized so that
|
|
749 ;; you can specify, for various functions, how you want
|
|
750 ;; particular arguments handled -- in some way that works
|
|
751 ;; recursively, so it can handle flet and such.
|
|
752
|
|
753 (let* ((function (buffer-substring (point)
|
428
|
754 (progn (forward-sexp 1) (point))))
|
872
|
755 (quote (condition-case nil
|
|
756 (save-excursion
|
|
757 (backward-up-list 1)
|
|
758 (memq (char-before (point)) '(?' ?`)))))
|
|
759 method)
|
|
760 (cond
|
|
761 ;; if we're indenting a quoted list, and the first word is not
|
|
762 ;; lambda, line up second line below first with no indentation,
|
|
763 ;; so that property lists indent correctly.
|
|
764 ((and quote (not (equal function "lambda")))
|
|
765 (setq method 'lisp-indent-quoteform))
|
|
766 ;; do the same if we're indenting the arg list of a def* form.
|
|
767 ((let ((p (point)))
|
|
768 (condition-case nil
|
442
|
769 (save-excursion
|
872
|
770 (backward-up-list 2)
|
|
771 ;; make sure we're indeed the second argument of the
|
|
772 ;; def* form.
|
|
773 (and (eq (point) (save-excursion
|
|
774 (goto-char p)
|
|
775 (backward-up-list 1)
|
|
776 (backward-sexp 2)
|
|
777 (1- (point))))
|
|
778 ;; check to see that the function is a def* type
|
|
779 (eq (char-after) ?\()
|
|
780 (progn (forward-char 1) t)
|
|
781 (looking-at lisp-function-and-type-regexp)
|
|
782 ;; defstruct may have slot option specs, which
|
|
783 ;; should probably be reverse-indented like
|
|
784 ;; normal, because the slot name is the first
|
|
785 ;; in the list. #### Review this.
|
|
786 (not (equal (match-string 0) "defstruct"))))
|
|
787 (error nil)))
|
|
788 (setq method 'lisp-indent-quoteform))
|
|
789
|
|
790 ;; handle functions in flet forms
|
|
791 ((let ((p (point)))
|
|
792 (condition-case nil
|
|
793 (save-excursion
|
|
794 (backward-up-list 3)
|
|
795 ;; make sure we're indeed a function, i.e. inside the
|
|
796 ;; first form after the flet.
|
|
797 (and (eq (point) (save-excursion
|
|
798 (goto-char p)
|
|
799 (backward-up-list 2)
|
|
800 (backward-sexp 1)
|
|
801 (1- (point))))
|
|
802 (looking-at lisp-flet-regexp)))
|
|
803 (error nil)))
|
|
804 (setq method 'defun))
|
|
805
|
|
806 ;; handle the arg lists in functions in flet forms
|
|
807 ((let ((p (point)))
|
|
808 (condition-case nil
|
|
809 (save-excursion
|
|
810 (backward-up-list 2)
|
|
811 ;; make sure we're indeed the arg list -- i.e. the first
|
|
812 ;; element after the function name.
|
|
813 (and (eq (point) (save-excursion
|
|
814 (goto-char p)
|
|
815 (backward-up-list 1)
|
|
816 (backward-sexp 1)
|
|
817 (1- (point))))
|
|
818 (progn
|
|
819 (backward-up-list 2)
|
|
820 (looking-at lisp-flet-regexp))))
|
|
821 (error nil)))
|
|
822 (setq method 'lisp-indent-quoteform))
|
|
823 (t
|
|
824 (setq method
|
|
825 (or (get (intern-soft function) 'lisp-indent-function)
|
|
826 (get (intern-soft function) 'lisp-indent-hook)))))
|
428
|
827 (cond ((or (eq method 'defun)
|
|
828 (and (null method)
|
|
829 (> (length function) 3)
|
|
830 (string-match "\\`def" function)))
|
|
831 (lisp-indent-defform state indent-point))
|
|
832 ((integerp method)
|
|
833 (lisp-indent-specform method state
|
|
834 indent-point normal-indent))
|
|
835 (method
|
|
836 (funcall method state indent-point)))))))
|
|
837
|
872
|
838 (defun lisp-indent-quoteform (state indent-point)
|
|
839 (goto-char (car (cdr state)))
|
|
840 (forward-line 1)
|
|
841 (if (> (point) (car (cdr (cdr state))))
|
|
842 (progn
|
|
843 (goto-char (car (cdr state)))
|
|
844 (+ 1 (current-column)))))
|
|
845
|
428
|
846 (defvar lisp-body-indent 2
|
|
847 "Number of columns to indent the second line of a `(def...)' form.")
|
|
848
|
|
849 (defun lisp-indent-specform (count state indent-point normal-indent)
|
|
850 (let ((containing-form-start (elt state 1))
|
|
851 (i count)
|
|
852 body-indent containing-form-column)
|
|
853 ;; Move to the start of containing form, calculate indentation
|
|
854 ;; to use for non-distinguished forms (> count), and move past the
|
|
855 ;; function symbol. lisp-indent-function guarantees that there is at
|
|
856 ;; least one word or symbol character following open paren of containing
|
|
857 ;; form.
|
|
858 (goto-char containing-form-start)
|
|
859 (setq containing-form-column (current-column))
|
|
860 (setq body-indent (+ lisp-body-indent containing-form-column))
|
|
861 (forward-char 1)
|
|
862 (forward-sexp 1)
|
|
863 ;; Now find the start of the last form.
|
|
864 (parse-partial-sexp (point) indent-point 1 t)
|
|
865 (while (and (< (point) indent-point)
|
|
866 (condition-case ()
|
|
867 (progn
|
|
868 (setq count (1- count))
|
|
869 (forward-sexp 1)
|
|
870 (parse-partial-sexp (point) indent-point 1 t))
|
|
871 (error nil))))
|
|
872 ;; Point is sitting on first character of last (or count) sexp.
|
|
873 (if (> count 0)
|
|
874 ;; A distinguished form. If it is the first or second form use double
|
|
875 ;; lisp-body-indent, else normal indent. With lisp-body-indent bound
|
|
876 ;; to 2 (the default), this just happens to work the same with if as
|
|
877 ;; the older code, but it makes unwind-protect, condition-case,
|
|
878 ;; with-output-to-temp-buffer, et. al. much more tasteful. The older,
|
|
879 ;; less hacked, behavior can be obtained by replacing below with
|
|
880 ;; (list normal-indent containing-form-start).
|
|
881 (if (<= (- i count) 1)
|
|
882 (list (+ containing-form-column (* 2 lisp-body-indent))
|
|
883 containing-form-start)
|
|
884 (list normal-indent containing-form-start))
|
|
885 ;; A non-distinguished form. Use body-indent if there are no
|
|
886 ;; distinguished forms and this is the first undistinguished form,
|
|
887 ;; or if this is the first undistinguished form and the preceding
|
|
888 ;; distinguished form has indentation at least as great as body-indent.
|
|
889 (if (or (and (= i 0) (= count 0))
|
|
890 (and (= count 0) (<= body-indent normal-indent)))
|
|
891 body-indent
|
|
892 normal-indent))))
|
|
893
|
|
894 (defun lisp-indent-defform (state indent-point)
|
|
895 (goto-char (car (cdr state)))
|
|
896 (forward-line 1)
|
|
897 (if (> (point) (car (cdr (cdr state))))
|
|
898 (progn
|
|
899 (goto-char (car (cdr state)))
|
|
900 (+ lisp-body-indent (current-column)))))
|
|
901
|
|
902
|
|
903 ;; (put 'progn 'lisp-indent-function 0), say, causes progn to be indented
|
|
904 ;; like defun if the first form is placed on the next line, otherwise
|
|
905 ;; it is indented like any other form (i.e. forms line up under first).
|
|
906
|
776
|
907 ;; NOTE: All def* functions are handled specially, no need to put them here.
|
|
908 ;; NOTE: All cl-* constructs are handled in cl.el.
|
|
909
|
428
|
910 (put 'lambda 'lisp-indent-function 'defun)
|
|
911 (put 'autoload 'lisp-indent-function 'defun)
|
|
912 (put 'progn 'lisp-indent-function 0)
|
|
913 (put 'prog1 'lisp-indent-function 1)
|
|
914 (put 'prog2 'lisp-indent-function 2)
|
|
915 (put 'save-excursion 'lisp-indent-function 0)
|
|
916 (put 'save-window-excursion 'lisp-indent-function 0)
|
|
917 (put 'save-selected-window 'lisp-indent-function 0)
|
442
|
918 (put 'with-selected-window 'lisp-indent-function 1)
|
428
|
919 (put 'save-selected-frame 'lisp-indent-function 0)
|
|
920 (put 'with-selected-frame 'lisp-indent-function 1)
|
872
|
921 (put 'save-selected-device 'lisp-indent-function 0)
|
|
922 (put 'with-selected-device 'lisp-indent-function 1)
|
428
|
923 (put 'save-restriction 'lisp-indent-function 0)
|
|
924 (put 'save-match-data 'lisp-indent-function 0)
|
|
925 (put 'let 'lisp-indent-function 1)
|
|
926 (put 'let* 'lisp-indent-function 1)
|
|
927 (put 'let-specifier 'lisp-indent-function 1)
|
|
928 (put 'while 'lisp-indent-function 1)
|
|
929 (put 'if 'lisp-indent-function 2)
|
|
930 (put 'catch 'lisp-indent-function 1)
|
|
931 (put 'condition-case 'lisp-indent-function 2)
|
460
|
932 (put 'handler-case 'lisp-indent-function 1)
|
|
933 (put 'handler-bind 'lisp-indent-function 1)
|
428
|
934 (put 'call-with-condition-handler 'lisp-indent-function 2)
|
|
935 (put 'unwind-protect 'lisp-indent-function 1)
|
|
936 (put 'save-current-buffer 'lisp-indent-function 0)
|
|
937 (put 'with-current-buffer 'lisp-indent-function 1)
|
|
938 (put 'with-string-as-buffer-contents 'lisp-indent-function 1)
|
|
939 (put 'with-temp-file 'lisp-indent-function 1)
|
|
940 (put 'with-temp-buffer 'lisp-indent-function 0)
|
|
941 (put 'with-output-to-string 'lisp-indent-function 0)
|
|
942 (put 'with-output-to-temp-buffer 'lisp-indent-function 1)
|
460
|
943 (put 'with-slots 'lisp-indent-function 2)
|
|
944 (put 'with-open-file 'lisp-indent-function 1)
|
|
945 (put 'with-open-stream 'lisp-indent-function 1)
|
428
|
946 (put 'eval-after-load 'lisp-indent-function 1)
|
|
947 (put 'display-message 'lisp-indent-function 1)
|
|
948 (put 'display-warning 'lisp-indent-function 1)
|
|
949 (put 'lmessage 'lisp-indent-function 2)
|
|
950 (put 'lwarn 'lisp-indent-function 2)
|
|
951 (put 'global-set-key 'lisp-indent-function 1)
|
460
|
952 (put 'print-unreadable-object 'lisp-indent-function 1)
|
428
|
953
|
|
954 (defun indent-sexp (&optional endpos)
|
|
955 "Indent each line of the list starting just after point.
|
|
956 If optional arg ENDPOS is given, indent each line, stopping when
|
|
957 ENDPOS is encountered."
|
|
958 (interactive)
|
|
959 (let ((indent-stack (list nil))
|
|
960 (next-depth 0)
|
|
961 ;; If ENDPOS is non-nil, use nil as STARTING-POINT
|
|
962 ;; so that calculate-lisp-indent will find the beginning of
|
|
963 ;; the defun we are in.
|
|
964 ;; If ENDPOS is nil, it is safe not to scan before point
|
|
965 ;; since every line we indent is more deeply nested than point is.
|
|
966 (starting-point (if endpos nil (point)))
|
|
967 (last-point (point))
|
|
968 last-depth bol outer-loop-done inner-loop-done state this-indent)
|
|
969 (or endpos
|
|
970 ;; Get error now if we don't have a complete sexp after point.
|
|
971 (save-excursion (forward-sexp 1)))
|
|
972 (save-excursion
|
|
973 (setq outer-loop-done nil)
|
|
974 (while (if endpos (< (point) endpos)
|
|
975 (not outer-loop-done))
|
|
976 (setq last-depth next-depth
|
|
977 inner-loop-done nil)
|
|
978 ;; Parse this line so we can learn the state
|
|
979 ;; to indent the next line.
|
|
980 ;; This inner loop goes through only once
|
|
981 ;; unless a line ends inside a string.
|
|
982 (while (and (not inner-loop-done)
|
|
983 (not (setq outer-loop-done (eobp))))
|
|
984 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
|
|
985 nil nil state))
|
|
986 (setq next-depth (car state))
|
|
987 ;; If the line contains a comment other than the sort
|
|
988 ;; that is indented like code,
|
|
989 ;; indent it now with indent-for-comment.
|
|
990 ;; Comments indented like code are right already.
|
|
991 ;; In any case clear the in-comment flag in the state
|
|
992 ;; because parse-partial-sexp never sees the newlines.
|
|
993 (if (car (nthcdr 4 state))
|
|
994 (progn (indent-for-comment)
|
|
995 (end-of-line)
|
|
996 (setcar (nthcdr 4 state) nil)))
|
|
997 ;; If this line ends inside a string,
|
|
998 ;; go straight to next line, remaining within the inner loop,
|
|
999 ;; and turn off the \-flag.
|
|
1000 (if (car (nthcdr 3 state))
|
|
1001 (progn
|
|
1002 (forward-line 1)
|
|
1003 (setcar (nthcdr 5 state) nil))
|
|
1004 (setq inner-loop-done t)))
|
|
1005 (and endpos
|
|
1006 (<= next-depth 0)
|
|
1007 (progn
|
|
1008 (setq indent-stack (append indent-stack
|
|
1009 (make-list (- next-depth) nil))
|
|
1010 last-depth (- last-depth next-depth)
|
|
1011 next-depth 0)))
|
|
1012 (or outer-loop-done endpos
|
|
1013 (setq outer-loop-done (<= next-depth 0)))
|
|
1014 (if outer-loop-done
|
|
1015 (forward-line 1)
|
|
1016 (while (> last-depth next-depth)
|
|
1017 (setq indent-stack (cdr indent-stack)
|
|
1018 last-depth (1- last-depth)))
|
|
1019 (while (< last-depth next-depth)
|
|
1020 (setq indent-stack (cons nil indent-stack)
|
|
1021 last-depth (1+ last-depth)))
|
|
1022 ;; Now go to the next line and indent it according
|
|
1023 ;; to what we learned from parsing the previous one.
|
|
1024 (forward-line 1)
|
|
1025 (setq bol (point))
|
|
1026 (skip-chars-forward " \t")
|
|
1027 ;; But not if the line is blank, or just a comment
|
|
1028 ;; (except for double-semi comments; indent them as usual).
|
|
1029 (if (or (eobp) (looking-at "\\s<\\|\n"))
|
|
1030 nil
|
|
1031 (if (and (car indent-stack)
|
|
1032 (>= (car indent-stack) 0))
|
|
1033 (setq this-indent (car indent-stack))
|
|
1034 (let ((val (calculate-lisp-indent
|
|
1035 (if (car indent-stack) (- (car indent-stack))
|
|
1036 starting-point))))
|
|
1037 (if (integerp val)
|
|
1038 (setcar indent-stack
|
|
1039 (setq this-indent val))
|
|
1040 (setcar indent-stack (- (car (cdr val))))
|
|
1041 (setq this-indent (car val)))))
|
|
1042 (if (/= (current-column) this-indent)
|
|
1043 (progn (delete-region bol (point))
|
|
1044 (indent-to this-indent)))))
|
|
1045 (or outer-loop-done
|
|
1046 (setq outer-loop-done (= (point) last-point))
|
|
1047 (setq last-point (point)))))))
|
|
1048
|
|
1049 ;; Indent every line whose first char is between START and END inclusive.
|
|
1050 (defun lisp-indent-region (start end)
|
|
1051 (save-excursion
|
|
1052 (let ((endmark (copy-marker end)))
|
|
1053 (goto-char start)
|
|
1054 (and (bolp) (not (eolp))
|
|
1055 (lisp-indent-line))
|
|
1056 (indent-sexp endmark)
|
|
1057 (set-marker endmark nil))))
|
|
1058
|
|
1059 ;;;; Lisp paragraph filling commands.
|
|
1060
|
|
1061 (defun lisp-fill-paragraph (&optional justify)
|
|
1062 "Like \\[fill-paragraph], but handle Emacs Lisp comments.
|
|
1063 If any of the current line is a comment, fill the comment or the
|
|
1064 paragraph of it that point is in, preserving the comment's indentation
|
|
1065 and initial semicolons."
|
|
1066 (interactive "P")
|
|
1067 (let (
|
|
1068 ;; Non-nil if the current line contains a comment.
|
|
1069 has-comment
|
|
1070
|
|
1071 ;; Non-nil if the current line contains code and a comment.
|
|
1072 has-code-and-comment
|
|
1073
|
|
1074 ;; If has-comment, the appropriate fill-prefix for the comment.
|
|
1075 comment-fill-prefix
|
|
1076 )
|
|
1077
|
|
1078 ;; Figure out what kind of comment we are looking at.
|
|
1079 (save-excursion
|
|
1080 (beginning-of-line)
|
|
1081 (cond
|
|
1082
|
|
1083 ;; A line with nothing but a comment on it?
|
|
1084 ((looking-at "[ \t]*;[; \t]*")
|
|
1085 (setq has-comment t
|
|
1086 comment-fill-prefix (buffer-substring (match-beginning 0)
|
|
1087 (match-end 0))))
|
|
1088
|
|
1089 ;; A line with some code, followed by a comment? Remember that the
|
|
1090 ;; semi which starts the comment shouldn't be part of a string or
|
|
1091 ;; character.
|
|
1092 ;; XEmacs Try this the FSF and see if it works.
|
|
1093 ; ((progn
|
|
1094 ; (while (not (looking-at ";\\|$"))
|
|
1095 ; (skip-chars-forward "^;\n\"\\\\?")
|
|
1096 ; (cond
|
|
1097 ; ((eq (char-after (point)) ?\\) (forward-char 2))
|
|
1098 ; ((memq (char-after (point)) '(?\" ??)) (forward-sexp 1))))
|
|
1099 ; (looking-at ";+[\t ]*"))
|
|
1100 ; (setq has-comment t)
|
|
1101 ((condition-case nil
|
|
1102 (save-restriction
|
|
1103 (narrow-to-region (point-min)
|
|
1104 (save-excursion (end-of-line) (point)))
|
|
1105 (while (not (looking-at ";\\|$"))
|
|
1106 (skip-chars-forward "^;\n\"\\\\?")
|
|
1107 (cond
|
|
1108 ((eq (char-after (point)) ?\\) (forward-char 2))
|
|
1109 ((memq (char-after (point)) '(?\" ??)) (forward-sexp 1))))
|
844
|
1110 ;; don't do comment filling in a string, or we will mess up
|
|
1111 ;; doc strings and other places where semicolons are used.
|
|
1112 (and (not (eq 'string (buffer-syntactic-context)))
|
|
1113 (looking-at ";+[\t ]*")))
|
428
|
1114 (error nil))
|
|
1115 (setq has-comment t has-code-and-comment t)
|
|
1116 (setq comment-fill-prefix
|
|
1117 (concat (make-string (/ (current-column) 8) ?\t)
|
|
1118 (make-string (% (current-column) 8) ?\ )
|
|
1119 (buffer-substring (match-beginning 0) (match-end 0)))))))
|
|
1120
|
|
1121 (if (not has-comment)
|
|
1122 (fill-paragraph justify)
|
|
1123
|
|
1124 ;; Narrow to include only the comment, and then fill the region.
|
|
1125 (save-excursion
|
|
1126 (save-restriction
|
|
1127 (beginning-of-line)
|
|
1128 (narrow-to-region
|
|
1129 ;; Find the first line we should include in the region to fill.
|
|
1130 (save-excursion
|
|
1131 (while (and (zerop (forward-line -1))
|
|
1132 (looking-at "^[ \t]*;")))
|
|
1133 ;; We may have gone too far. Go forward again.
|
|
1134 (or (looking-at ".*;")
|
|
1135 (forward-line 1))
|
|
1136 (point))
|
|
1137 ;; Find the beginning of the first line past the region to fill.
|
|
1138 (save-excursion
|
|
1139 (while (progn (forward-line 1)
|
|
1140 (looking-at "^[ \t]*;")))
|
|
1141 (point)))
|
|
1142
|
|
1143 ;; Lines with only semicolons on them can be paragraph boundaries.
|
|
1144 (let* ((paragraph-start (concat paragraph-start "\\|[ \t;]*$"))
|
|
1145 (paragraph-separate (concat paragraph-start "\\|[ \t;]*$"))
|
|
1146 (paragraph-ignore-fill-prefix nil)
|
|
1147 (fill-prefix comment-fill-prefix)
|
|
1148 (after-line (if has-code-and-comment
|
|
1149 (save-excursion
|
|
1150 (forward-line 1) (point))))
|
|
1151 (end (progn
|
|
1152 (forward-paragraph)
|
|
1153 (or (bolp) (newline 1))
|
|
1154 (point)))
|
|
1155 ;; If this comment starts on a line with code,
|
|
1156 ;; include that like in the filling.
|
|
1157 (beg (progn (backward-paragraph)
|
|
1158 (if (eq (point) after-line)
|
|
1159 (forward-line -1))
|
|
1160 (point))))
|
|
1161 (fill-region-as-paragraph beg end
|
|
1162 justify nil
|
|
1163 (save-excursion
|
|
1164 (goto-char beg)
|
|
1165 (if (looking-at fill-prefix)
|
|
1166 nil
|
|
1167 (re-search-forward comment-start-skip)
|
|
1168 (point))))))))
|
|
1169 t))
|
|
1170
|
|
1171 (defun indent-code-rigidly (start end arg &optional nochange-regexp)
|
|
1172 "Indent all lines of code, starting in the region, sideways by ARG columns.
|
|
1173 Does not affect lines starting inside comments or strings, assuming that
|
|
1174 the start of the region is not inside them.
|
|
1175
|
|
1176 Called from a program, takes args START, END, COLUMNS and NOCHANGE-REGEXP.
|
|
1177 The last is a regexp which, if matched at the beginning of a line,
|
|
1178 means don't indent that line."
|
|
1179 (interactive "r\np")
|
|
1180 (let (state)
|
|
1181 (save-excursion
|
|
1182 (goto-char end)
|
|
1183 (setq end (point-marker))
|
|
1184 (goto-char start)
|
|
1185 (or (bolp)
|
|
1186 (setq state (parse-partial-sexp (point)
|
|
1187 (progn
|
|
1188 (forward-line 1) (point))
|
|
1189 nil nil state)))
|
|
1190 (while (< (point) end)
|
|
1191 (or (car (nthcdr 3 state))
|
|
1192 (and nochange-regexp
|
|
1193 (looking-at nochange-regexp))
|
|
1194 ;; If line does not start in string, indent it
|
|
1195 (let ((indent (current-indentation)))
|
|
1196 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
|
|
1197 (or (eolp)
|
|
1198 (indent-to (max 0 (+ indent arg)) 0))))
|
|
1199 (setq state (parse-partial-sexp (point)
|
|
1200 (progn
|
|
1201 (forward-line 1) (point))
|
|
1202 nil nil state))))))
|
|
1203
|
|
1204 (provide 'lisp-mode)
|
|
1205
|
|
1206 ;;; lisp-mode.el ends here
|