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