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 "---"
|
|
65 ,@(if popup-p
|
|
66 '(["%_Find Function"
|
|
67 (find-function (menu-call-at-event '(function-at-point)))
|
|
68 :suffix (let ((fun (menu-call-at-event '(function-at-point))))
|
|
69 (if fun (symbol-name fun) ""))
|
|
70 :active (and (fboundp 'find-function)
|
|
71 (menu-call-at-event '(function-at-point)))]
|
|
72 ["%_Find Variable"
|
|
73 (find-variable (menu-call-at-event '(variable-at-point)))
|
|
74 :suffix (let ((fun (menu-call-at-event '(variable-at-point))))
|
|
75 (if fun (symbol-name fun) ""))
|
|
76 :active (and (fboundp 'find-variable)
|
|
77 (menu-call-at-event '(variable-at-point)))]
|
|
78 ["%_Help on Function"
|
|
79 (describe-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 'describe-function)
|
|
83 (menu-call-at-event '(function-at-point)))]
|
|
84 ["%_Help on Variable"
|
|
85 (describe-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 'describe-variable)
|
|
89 (menu-call-at-event '(variable-at-point)))])
|
|
90 '(["Find %_Function..." find-function
|
|
91 :active (fboundp 'find-function)]
|
|
92 ["Find %_Variable..." find-variable
|
|
93 :active (fboundp 'find-variable)]
|
|
94 ["%_Help on Function..." describe-function
|
|
95 :active (fboundp 'describe-function)]
|
|
96 ["Hel%_p on Variable..." describe-variable
|
|
97 :active (fboundp 'describe-variable)]))
|
|
98 "---"
|
|
99 ["Instrument This Defun for %_Debugging" ,(popup-wrap 'edebug-defun)]
|
|
100 ["%_Trace Function..." trace-function-background]
|
|
101 ["%_Untrace All Functions" untrace-all
|
|
102 :active (fboundp 'untrace-all)]
|
|
103 "---"
|
|
104 ["%_Comment Out Region" comment-region :active (region-exists-p)]
|
502
|
105 ["Unc%_omment Region" (comment-region (region-beginning)
|
|
106 (region-end) '(4))
|
|
107 :active (region-exists-p)]
|
442
|
108 "---"
|
|
109 ["%_Indent Region or Balanced Expression"
|
|
110 ,(popup-wrap '(if (region-exists-p)
|
|
111 (call-interactively 'indent-region)
|
|
112 (call-interactively 'indent-sexp)))]
|
|
113 ["I%_ndent Defun"
|
|
114 ,(popup-wrap '(progn
|
|
115 (beginning-of-defun)
|
|
116 (indent-sexp)))]
|
|
117 "---"
|
|
118 "Look for debug-on-error under Options->Troubleshooting"
|
|
119 )))
|
428
|
120
|
442
|
121 (defvar lisp-interaction-mode-popup-menu
|
|
122 (cons "Lisp-Interaction" (construct-lisp-mode-menu t nil)))
|
|
123
|
|
124 (defvar emacs-lisp-mode-popup-menu
|
|
125 (cons "Emacs-Lisp" (construct-lisp-mode-menu t t)))
|
428
|
126
|
|
127 ;Don't have a menubar entry in Lisp Interaction mode. Otherwise, the
|
|
128 ;*scratch* buffer has a Lisp menubar item! Very confusing.
|
442
|
129 ;Jan Vroonhof really wants this, so it's back. --ben
|
|
130 (defvar lisp-interaction-mode-menubar-menu
|
|
131 (cons "%_Lisp" (construct-lisp-mode-menu nil nil)))
|
428
|
132
|
442
|
133 (defvar emacs-lisp-mode-menubar-menu
|
|
134 (cons "%_Lisp" (construct-lisp-mode-menu nil t)))
|
428
|
135
|
|
136 (if (not emacs-lisp-mode-syntax-table)
|
|
137 (let ((i 0))
|
|
138 (setq emacs-lisp-mode-syntax-table (make-syntax-table))
|
|
139 (while (< i ?0)
|
|
140 (modify-syntax-entry i "_ " emacs-lisp-mode-syntax-table)
|
|
141 (setq i (1+ i)))
|
|
142 (setq i (1+ ?9))
|
|
143 (while (< i ?A)
|
|
144 (modify-syntax-entry i "_ " emacs-lisp-mode-syntax-table)
|
|
145 (setq i (1+ i)))
|
|
146 (setq i (1+ ?Z))
|
|
147 (while (< i ?a)
|
|
148 (modify-syntax-entry i "_ " emacs-lisp-mode-syntax-table)
|
|
149 (setq i (1+ i)))
|
|
150 (setq i (1+ ?z))
|
|
151 (while (< i 128)
|
|
152 (modify-syntax-entry i "_ " emacs-lisp-mode-syntax-table)
|
|
153 (setq i (1+ i)))
|
|
154 (modify-syntax-entry ? " " emacs-lisp-mode-syntax-table)
|
|
155 (modify-syntax-entry ?\t " " emacs-lisp-mode-syntax-table)
|
|
156 (modify-syntax-entry ?\f " " emacs-lisp-mode-syntax-table)
|
|
157 (modify-syntax-entry ?\n "> " emacs-lisp-mode-syntax-table)
|
|
158 ;; Give CR the same syntax as newline, for selective-display.
|
|
159 (modify-syntax-entry ?\^m "> " emacs-lisp-mode-syntax-table)
|
|
160 (modify-syntax-entry ?\; "< " emacs-lisp-mode-syntax-table)
|
|
161 (modify-syntax-entry ?` "' " emacs-lisp-mode-syntax-table)
|
|
162 (modify-syntax-entry ?' "' " emacs-lisp-mode-syntax-table)
|
|
163 (modify-syntax-entry ?, "' " emacs-lisp-mode-syntax-table)
|
|
164 ;; Used to be singlequote; changed for flonums.
|
|
165 (modify-syntax-entry ?. "_ " emacs-lisp-mode-syntax-table)
|
|
166 (modify-syntax-entry ?# "' " emacs-lisp-mode-syntax-table)
|
|
167 (modify-syntax-entry ?\" "\" " emacs-lisp-mode-syntax-table)
|
|
168 (modify-syntax-entry ?\\ "\\ " emacs-lisp-mode-syntax-table)
|
|
169 (modify-syntax-entry ?\( "() " emacs-lisp-mode-syntax-table)
|
|
170 (modify-syntax-entry ?\) ")( " emacs-lisp-mode-syntax-table)
|
|
171 (modify-syntax-entry ?\[ "(] " emacs-lisp-mode-syntax-table)
|
|
172 (modify-syntax-entry ?\] ")[ " emacs-lisp-mode-syntax-table)))
|
|
173
|
|
174 (if (not lisp-mode-syntax-table)
|
|
175 (progn (setq lisp-mode-syntax-table
|
|
176 (copy-syntax-table emacs-lisp-mode-syntax-table))
|
|
177 (modify-syntax-entry ?\[ "_ " lisp-mode-syntax-table)
|
|
178 ;; XEmacs changes
|
|
179 (modify-syntax-entry ?\] "_ " lisp-mode-syntax-table)
|
460
|
180 (modify-syntax-entry ?# "' 58" lisp-mode-syntax-table)
|
|
181 (modify-syntax-entry ?| "\" 67" lisp-mode-syntax-table)))
|
428
|
182
|
|
183 (define-abbrev-table 'lisp-mode-abbrev-table ())
|
|
184
|
|
185 (defvar lisp-imenu-generic-expression
|
|
186 '(
|
|
187 (nil
|
|
188 "^\\s-*(def\\(un\\|subst\\|macro\\|advice\\)\\s-+\\([-A-Za-z0-9+*|:]+\\)" 2)
|
|
189 ("Variables"
|
|
190 "^\\s-*(def\\(var\\|const\\|custom\\)\\s-+\\([-A-Za-z0-9+*|:]+\\)" 2)
|
|
191 ("Types"
|
|
192 "^\\s-*(def\\(group\\|type\\|struct\\|class\\|ine-condition\\)\\s-+\\([-A-Za-z0-9+*|:]+\\)"
|
|
193 2))
|
|
194
|
|
195 "Imenu generic expression for Lisp mode. See `imenu-generic-expression'.")
|
|
196
|
|
197 (defun lisp-mode-variables (lisp-syntax)
|
|
198 (cond (lisp-syntax
|
|
199 (set-syntax-table lisp-mode-syntax-table)))
|
|
200 (setq local-abbrev-table lisp-mode-abbrev-table)
|
|
201 (make-local-variable 'paragraph-start)
|
|
202 (setq paragraph-start (concat page-delimiter "\\|$" ))
|
|
203 (make-local-variable 'paragraph-separate)
|
|
204 (setq paragraph-separate paragraph-start)
|
|
205 (make-local-variable 'paragraph-ignore-fill-prefix)
|
|
206 (setq paragraph-ignore-fill-prefix t)
|
|
207 (make-local-variable 'fill-paragraph-function)
|
|
208 (setq fill-paragraph-function 'lisp-fill-paragraph)
|
|
209 ;; Adaptive fill mode gets in the way of auto-fill,
|
|
210 ;; and should make no difference for explicit fill
|
|
211 ;; because lisp-fill-paragraph should do the job.
|
|
212 (make-local-variable 'adaptive-fill-mode)
|
|
213 (setq adaptive-fill-mode nil)
|
|
214 (make-local-variable 'indent-line-function)
|
|
215 (setq indent-line-function 'lisp-indent-line)
|
|
216 (make-local-variable 'indent-region-function)
|
|
217 (setq indent-region-function 'lisp-indent-region)
|
|
218 (make-local-variable 'parse-sexp-ignore-comments)
|
|
219 (setq parse-sexp-ignore-comments t)
|
|
220 (make-local-variable 'outline-regexp)
|
|
221 (setq outline-regexp ";;; \\|(....")
|
|
222 (make-local-variable 'comment-start)
|
|
223 (setq comment-start ";")
|
|
224 ;; XEmacs change
|
|
225 (set (make-local-variable 'block-comment-start) ";;")
|
|
226 (make-local-variable 'comment-start-skip)
|
|
227 ;; Look within the line for a ; following an even number of backslashes
|
|
228 ;; after either a non-backslash or the line beginning.
|
|
229 (setq comment-start-skip "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\);+ *")
|
|
230 (make-local-variable 'comment-column)
|
|
231 (setq comment-column 40)
|
|
232 (make-local-variable 'comment-indent-function)
|
|
233 (setq comment-indent-function 'lisp-comment-indent)
|
|
234 ;; XEmacs change
|
|
235 (set (make-local-variable 'dabbrev-case-fold-search) nil)
|
|
236 (set (make-local-variable 'dabbrev-case-replace) nil)
|
|
237 (make-local-variable 'imenu-generic-expression)
|
|
238 (setq imenu-generic-expression lisp-imenu-generic-expression))
|
|
239
|
|
240 (defvar shared-lisp-mode-map ()
|
|
241 "Keymap for commands shared by all sorts of Lisp modes.")
|
|
242
|
|
243 (if shared-lisp-mode-map
|
|
244 ()
|
|
245 (setq shared-lisp-mode-map (make-sparse-keymap))
|
|
246 ;; XEmacs changes
|
|
247 (set-keymap-name shared-lisp-mode-map 'shared-lisp-mode-map)
|
|
248 (define-key shared-lisp-mode-map "\M-;" 'lisp-indent-for-comment)
|
|
249 (define-key shared-lisp-mode-map "\e\C-q" 'indent-sexp))
|
|
250
|
|
251 (defvar emacs-lisp-mode-map ()
|
|
252 "Keymap for Emacs Lisp mode.
|
|
253 All commands in `shared-lisp-mode-map' are inherited by this map.")
|
|
254
|
|
255 (if emacs-lisp-mode-map
|
|
256 ()
|
|
257 ;; XEmacs: Ignore FSF nconc stuff
|
|
258 (setq emacs-lisp-mode-map (make-sparse-keymap))
|
|
259 (set-keymap-name emacs-lisp-mode-map 'emacs-lisp-mode-map)
|
|
260 (set-keymap-parents emacs-lisp-mode-map (list shared-lisp-mode-map))
|
|
261 (define-key emacs-lisp-mode-map "\e\t" 'lisp-complete-symbol)
|
|
262 (define-key emacs-lisp-mode-map "\e\C-x" 'eval-defun)
|
|
263 ;; XEmacs: Not sure what the FSF menu bindings are. I hope XEmacs
|
|
264 ;; doesn't need them.
|
|
265 )
|
|
266
|
|
267 (defun emacs-lisp-byte-compile ()
|
|
268 "Byte compile the file containing the current buffer."
|
|
269 (interactive)
|
|
270 (if buffer-file-name
|
|
271 ;; XEmacs change. Force buffer save first
|
|
272 (progn
|
|
273 (save-buffer)
|
|
274 (byte-compile-file buffer-file-name))
|
|
275 (error "The buffer must be saved in a file first.")))
|
|
276
|
|
277 (defun emacs-lisp-byte-compile-and-load ()
|
|
278 "Byte-compile the current file (if it has changed), then load compiled code."
|
|
279 (interactive)
|
|
280 (or buffer-file-name
|
|
281 (error "The buffer must be saved in a file first"))
|
|
282 (require 'bytecomp)
|
|
283 ;; Recompile if file or buffer has changed since last compilation.
|
|
284 (if (and (buffer-modified-p)
|
|
285 (y-or-n-p (format "save buffer %s first? " (buffer-name))))
|
|
286 (save-buffer))
|
|
287 (let ((compiled-file-name (byte-compile-dest-file buffer-file-name)))
|
|
288 (if (file-newer-than-file-p compiled-file-name buffer-file-name)
|
|
289 (load-file compiled-file-name)
|
|
290 (byte-compile-file buffer-file-name t))))
|
|
291
|
|
292 (defun emacs-lisp-mode ()
|
|
293 "Major mode for editing Lisp code to run in Emacs.
|
|
294 Commands:
|
|
295 Delete converts tabs to spaces as it moves back.
|
|
296 Blank lines separate paragraphs. Semicolons start comments.
|
|
297 \\{emacs-lisp-mode-map}
|
|
298 Entry to this mode calls the value of `emacs-lisp-mode-hook'
|
|
299 if that value is non-nil."
|
|
300 (interactive)
|
|
301 (kill-all-local-variables)
|
|
302 (use-local-map emacs-lisp-mode-map)
|
|
303 (set-syntax-table emacs-lisp-mode-syntax-table)
|
|
304 ;; XEmacs changes
|
|
305 (setq major-mode 'emacs-lisp-mode
|
442
|
306 mode-popup-menu emacs-lisp-mode-popup-menu
|
428
|
307 mode-name "Emacs-Lisp")
|
442
|
308 (if (and (featurep 'menubar)
|
|
309 current-menubar)
|
|
310 (progn
|
428
|
311 ;; make a local copy of the menubar, so our modes don't
|
|
312 ;; change the global menubar
|
442
|
313 (set-buffer-menubar current-menubar)
|
|
314 (add-submenu nil emacs-lisp-mode-menubar-menu)))
|
428
|
315 (lisp-mode-variables nil)
|
|
316 (run-hooks 'emacs-lisp-mode-hook))
|
|
317
|
430
|
318 (put 'emacs-lisp-mode 'font-lock-lisp-like t)
|
|
319
|
428
|
320 (defvar lisp-mode-map ()
|
|
321 "Keymap for ordinary Lisp mode.
|
|
322 All commands in `shared-lisp-mode-map' are inherited by this map.")
|
|
323
|
|
324 (if lisp-mode-map
|
|
325 ()
|
|
326 ;; XEmacs changes
|
|
327 (setq lisp-mode-map (make-sparse-keymap))
|
|
328 (set-keymap-name lisp-mode-map 'lisp-mode-map)
|
|
329 (set-keymap-parents lisp-mode-map (list shared-lisp-mode-map))
|
|
330 (define-key lisp-mode-map "\e\C-x" 'lisp-send-defun)
|
|
331 ;; gag, no. use ilisp. -jwz
|
|
332 ;; (define-key lisp-mode-map "\C-c\C-z" 'run-lisp)
|
|
333 )
|
|
334
|
|
335 (defun lisp-mode ()
|
613
|
336 "Major mode for editing Lisp code for Lisps other than Emacs Lisp.
|
428
|
337 Commands:
|
|
338 Delete converts tabs to spaces as it moves back.
|
|
339 Blank lines separate paragraphs. Semicolons start comments.
|
|
340 \\{lisp-mode-map}
|
|
341 Note that `run-lisp' may be used either to start an inferior Lisp job
|
|
342 or to switch back to an existing one.
|
|
343
|
|
344 Entry to this mode calls the value of `lisp-mode-hook'
|
|
345 if that value is non-nil."
|
|
346 (interactive)
|
|
347 (kill-all-local-variables)
|
|
348 (use-local-map lisp-mode-map)
|
|
349 (setq major-mode 'lisp-mode)
|
|
350 (setq mode-name "Lisp")
|
|
351 (lisp-mode-variables t)
|
|
352 (set-syntax-table lisp-mode-syntax-table)
|
|
353 (run-hooks 'lisp-mode-hook))
|
|
354
|
|
355 ;; This will do unless shell.el is loaded.
|
|
356 ;; XEmacs change
|
|
357 (defun lisp-send-defun ()
|
|
358 "Send the current defun to the Lisp process made by \\[run-lisp]."
|
|
359 (interactive)
|
|
360 (error "Process lisp does not exist"))
|
|
361
|
|
362 ;; XEmacs change: emacs-lisp-mode-map is a more appropriate parent.
|
|
363 (defvar lisp-interaction-mode-map ()
|
|
364 "Keymap for Lisp Interaction mode.
|
|
365 All commands in `shared-lisp-mode-map' are inherited by this map.")
|
|
366
|
|
367 (if lisp-interaction-mode-map
|
|
368 ()
|
|
369 ;; XEmacs set keymap our way
|
|
370 (setq lisp-interaction-mode-map (make-sparse-keymap))
|
|
371 (set-keymap-name lisp-interaction-mode-map 'lisp-interaction-mode-map)
|
|
372 (set-keymap-parents lisp-interaction-mode-map (list emacs-lisp-mode-map))
|
|
373 (define-key lisp-interaction-mode-map "\e\C-x" 'eval-defun)
|
|
374 (define-key lisp-interaction-mode-map "\e\t" 'lisp-complete-symbol)
|
|
375 (define-key lisp-interaction-mode-map "\n" 'eval-print-last-sexp))
|
|
376
|
|
377 (defun lisp-interaction-mode ()
|
|
378 "Major mode for typing and evaluating Lisp forms.
|
|
379 Like Lisp mode except that \\[eval-print-last-sexp] evals the Lisp expression
|
|
380 before point, and prints its value into the buffer, advancing point.
|
|
381
|
|
382 Commands:
|
|
383 Delete converts tabs to spaces as it moves back.
|
|
384 Paragraphs are separated only by blank lines.
|
|
385 Semicolons start comments.
|
|
386 \\{lisp-interaction-mode-map}
|
|
387 Entry to this mode calls the value of `lisp-interaction-mode-hook'
|
|
388 if that value is non-nil."
|
|
389 (interactive)
|
|
390 (kill-all-local-variables)
|
|
391 (use-local-map lisp-interaction-mode-map)
|
|
392 (setq major-mode 'lisp-interaction-mode)
|
|
393 (setq mode-name "Lisp Interaction")
|
442
|
394 (setq mode-popup-menu lisp-interaction-mode-popup-menu)
|
|
395 (if (and (featurep 'menubar)
|
|
396 current-menubar)
|
|
397 (progn
|
|
398 ;; make a local copy of the menubar, so our modes don't
|
|
399 ;; change the global menubar
|
|
400 (set-buffer-menubar current-menubar)
|
|
401 (add-submenu nil lisp-interaction-mode-menubar-menu)))
|
428
|
402 (set-syntax-table emacs-lisp-mode-syntax-table)
|
|
403 (lisp-mode-variables nil)
|
|
404 (run-hooks 'lisp-interaction-mode-hook))
|
|
405
|
|
406 (defun eval-print-last-sexp ()
|
|
407 "Evaluate sexp before point; print value into current buffer."
|
|
408 (interactive)
|
|
409 (let ((standard-output (current-buffer)))
|
|
410 (terpri)
|
|
411 (eval-last-sexp t)
|
|
412 (terpri)))
|
|
413
|
|
414 ;; XEmacs change
|
|
415 (defcustom eval-interactive-verbose t
|
|
416 "*Non-nil means that interactive evaluation can print messages.
|
|
417 The messages are printed when the expression is treated differently
|
|
418 using `\\[eval-last-sexp]' and `\\[eval-defun]' than it than it would have been
|
|
419 treated noninteractively.
|
|
420
|
|
421 The printed messages are \"defvar treated as defconst\" and \"defcustom
|
|
422 evaluation forced\". See `eval-interactive' for more details."
|
|
423 :type 'boolean
|
|
424 :group 'lisp)
|
|
425
|
|
426 (defun eval-interactive (expr)
|
|
427 "Like `eval' except that it transforms defvars to defconsts.
|
|
428 The evaluation of defcustom forms is forced."
|
|
429 (cond ((and (eq (car-safe expr) 'defvar)
|
|
430 (> (length expr) 2))
|
|
431 (eval (cons 'defconst (cdr expr)))
|
|
432 (when eval-interactive-verbose
|
|
433 (message "defvar treated as defconst")
|
|
434 (sit-for 1)
|
|
435 (message ""))
|
|
436 (nth 1 expr))
|
|
437 ((and (eq (car-safe expr) 'defcustom)
|
|
438 (> (length expr) 2)
|
|
439 (default-boundp (nth 1 expr)))
|
|
440 ;; Force variable to be bound
|
|
441 ;; #### defcustom might specify a different :set method.
|
|
442 (set-default (nth 1 expr) (eval (nth 2 expr)))
|
|
443 ;; And evaluate the defcustom
|
|
444 (eval expr)
|
|
445 (when eval-interactive-verbose
|
|
446 (message "defcustom evaluation forced")
|
|
447 (sit-for 1)
|
|
448 (message ""))
|
|
449 (nth 1 expr))
|
|
450 (t
|
|
451 (eval expr))))
|
|
452
|
|
453 ;; XEmacs change, based on Bob Weiner suggestion
|
|
454 (defun eval-last-sexp (eval-last-sexp-arg-internal) ;dynamic scoping wonderment
|
|
455 "Evaluate sexp before point; print value in minibuffer.
|
|
456 With argument, print output into current buffer."
|
|
457 (interactive "P")
|
|
458 (let ((standard-output (if eval-last-sexp-arg-internal (current-buffer) t))
|
|
459 (opoint (point))
|
|
460 ignore-quotes)
|
|
461 (prin1 (eval-interactive
|
|
462 (letf (((syntax-table) emacs-lisp-mode-syntax-table))
|
|
463 (save-excursion
|
|
464 ;; If this sexp appears to be enclosed in `...' then
|
|
465 ;; ignore the surrounding quotes.
|
|
466 (setq ignore-quotes (or (eq (char-after) ?\')
|
|
467 (eq (char-before) ?\')))
|
|
468 (forward-sexp -1)
|
|
469 ;; vladimir@cs.ualberta.ca 30-Jul-1997: skip ` in
|
|
470 ;; `variable' so that the value is returned, not the
|
|
471 ;; name.
|
|
472 (if (and ignore-quotes
|
|
473 (eq (char-after) ?\`))
|
|
474 (forward-char))
|
|
475 (save-restriction
|
|
476 (narrow-to-region (point-min) opoint)
|
|
477 (let ((expr (read (current-buffer))))
|
|
478 (if (eq (car-safe expr) 'interactive)
|
|
479 ;; If it's an (interactive ...) form, it's
|
|
480 ;; more useful to show how an interactive call
|
|
481 ;; would use it.
|
|
482 `(call-interactively
|
|
483 (lambda (&rest args)
|
|
484 ,expr args))
|
|
485 expr)))))))))
|
|
486
|
|
487 (defun eval-defun (eval-defun-arg-internal)
|
|
488 "Evaluate defun that point is in or before.
|
|
489 Print value in minibuffer.
|
|
490 With argument, insert value in current buffer after the defun."
|
|
491 (interactive "P")
|
|
492 (let ((standard-output (if eval-defun-arg-internal (current-buffer) t)))
|
|
493 (prin1 (eval-interactive (save-excursion
|
|
494 (end-of-defun)
|
|
495 (beginning-of-defun)
|
|
496 (read (current-buffer)))))))
|
|
497
|
|
498
|
|
499 (defun lisp-comment-indent ()
|
|
500 (if (looking-at "\\s<\\s<\\s<")
|
|
501 (current-column)
|
|
502 (if (looking-at "\\s<\\s<")
|
|
503 ;; #### FSF has:
|
|
504 ;; (let ((tem (or (calculate-lisp-indent) (current-column)))) ...
|
|
505 (let ((tem (calculate-lisp-indent)))
|
|
506 (if (listp tem) (car tem) tem))
|
|
507 (skip-chars-backward " \t")
|
|
508 (max (if (bolp) 0 (1+ (current-column)))
|
|
509 comment-column))))
|
|
510
|
|
511 ;; XEmacs change
|
|
512 (defun lisp-indent-for-comment ()
|
|
513 "Indent this line's comment appropriately, or insert an empty comment.
|
|
514 If adding a new comment on a blank line, use `block-comment-start' instead
|
|
515 of `comment-start' to open the comment."
|
|
516 ;; by Stig@hackvan.com
|
|
517 ;; #### - This functionality, the recognition of block-comment-{start,end},
|
|
518 ;; will perhaps be standardized across modes and move to indent-for-comment.
|
|
519 (interactive)
|
|
520 (if (and block-comment-start
|
|
521 (save-excursion (beginning-of-line) (looking-at "^[ \t]*$")))
|
|
522 (insert block-comment-start))
|
|
523 (indent-for-comment))
|
|
524
|
|
525 (defvar lisp-indent-offset nil)
|
|
526 (defvar lisp-indent-function 'lisp-indent-function)
|
|
527
|
|
528 (defun lisp-indent-line (&optional whole-exp)
|
|
529 "Indent current line as Lisp code.
|
|
530 With argument, indent any additional lines of the same expression
|
|
531 rigidly along with this one."
|
|
532 (interactive "P")
|
|
533 (let ((indent (calculate-lisp-indent)) shift-amt beg end
|
|
534 (pos (- (point-max) (point))))
|
|
535 (beginning-of-line)
|
|
536 (setq beg (point))
|
|
537 (skip-chars-forward " \t")
|
|
538 (if (looking-at "\\s<\\s<\\s<")
|
|
539 ;; Don't alter indentation of a ;;; comment line.
|
|
540 (goto-char (- (point-max) pos))
|
|
541 (if (and (looking-at "\\s<") (not (looking-at "\\s<\\s<")))
|
|
542 ;; Single-semicolon comment lines should be indented
|
|
543 ;; as comment lines, not as code.
|
446
|
544 (progn (indent-for-comment) (backward-char 1))
|
428
|
545 (if (listp indent) (setq indent (car indent)))
|
|
546 (setq shift-amt (- indent (current-column)))
|
|
547 (if (zerop shift-amt)
|
|
548 nil
|
|
549 (delete-region beg (point))
|
|
550 (indent-to indent)))
|
|
551 ;; If initial point was within line's indentation,
|
|
552 ;; position after the indentation. Else stay at same point in text.
|
|
553 (if (> (- (point-max) pos) (point))
|
|
554 (goto-char (- (point-max) pos)))
|
|
555 ;; If desired, shift remaining lines of expression the same amount.
|
|
556 (and whole-exp (not (zerop shift-amt))
|
|
557 (save-excursion
|
|
558 (goto-char beg)
|
|
559 (forward-sexp 1)
|
|
560 (setq end (point))
|
|
561 (goto-char beg)
|
|
562 (forward-line 1)
|
|
563 (setq beg (point))
|
|
564 (> end beg))
|
|
565 (indent-code-rigidly beg end shift-amt)))))
|
|
566
|
|
567 (defvar calculate-lisp-indent-last-sexp)
|
|
568
|
|
569 (defun calculate-lisp-indent (&optional parse-start)
|
|
570 "Return appropriate indentation for current line as Lisp code.
|
|
571 In usual case returns an integer: the column to indent to.
|
|
572 Can instead return a list, whose car is the column to indent to.
|
|
573 This means that following lines at the same level of indentation
|
|
574 should not necessarily be indented the same way.
|
|
575 The second element of the list is the buffer position
|
|
576 of the start of the containing expression."
|
|
577 (save-excursion
|
|
578 (beginning-of-line)
|
|
579 (let ((indent-point (point))
|
|
580 ;; XEmacs change (remove paren-depth)
|
|
581 state ;;paren-depth
|
|
582 ;; setting this to a number inhibits calling hook
|
|
583 (desired-indent nil)
|
|
584 (retry t)
|
|
585 calculate-lisp-indent-last-sexp containing-sexp)
|
|
586 (if parse-start
|
|
587 (goto-char parse-start)
|
|
588 (beginning-of-defun))
|
|
589 ;; Find outermost containing sexp
|
|
590 (while (< (point) indent-point)
|
|
591 (setq state (parse-partial-sexp (point) indent-point 0)))
|
|
592 ;; Find innermost containing sexp
|
|
593 (while (and retry
|
|
594 state
|
|
595 ;; XEmacs change (remove paren-depth)
|
|
596 (> ;;(setq paren-depth (elt state 0))
|
|
597 (elt state 0)
|
|
598 0))
|
|
599 (setq retry nil)
|
|
600 (setq calculate-lisp-indent-last-sexp (elt state 2))
|
|
601 (setq containing-sexp (elt state 1))
|
|
602 ;; Position following last unclosed open.
|
|
603 (goto-char (1+ containing-sexp))
|
|
604 ;; Is there a complete sexp since then?
|
|
605 (if (and calculate-lisp-indent-last-sexp
|
|
606 (> calculate-lisp-indent-last-sexp (point)))
|
|
607 ;; Yes, but is there a containing sexp after that?
|
|
608 (let ((peek (parse-partial-sexp calculate-lisp-indent-last-sexp
|
|
609 indent-point 0)))
|
|
610 (if (setq retry (car (cdr peek))) (setq state peek)))))
|
|
611 (if retry
|
|
612 nil
|
|
613 ;; Innermost containing sexp found
|
|
614 (goto-char (1+ containing-sexp))
|
|
615 (if (not calculate-lisp-indent-last-sexp)
|
|
616 ;; indent-point immediately follows open paren.
|
|
617 ;; Don't call hook.
|
|
618 (setq desired-indent (current-column))
|
|
619 ;; Find the start of first element of containing sexp.
|
|
620 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
|
|
621 (cond ((looking-at "\\s(")
|
|
622 ;; First element of containing sexp is a list.
|
|
623 ;; Indent under that list.
|
|
624 )
|
|
625 ((> (save-excursion (forward-line 1) (point))
|
|
626 calculate-lisp-indent-last-sexp)
|
|
627 ;; This is the first line to start within the containing sexp.
|
|
628 ;; It's almost certainly a function call.
|
|
629 (if (= (point) calculate-lisp-indent-last-sexp)
|
|
630 ;; Containing sexp has nothing before this line
|
|
631 ;; except the first element. Indent under that element.
|
|
632 nil
|
|
633 ;; Skip the first element, find start of second (the first
|
|
634 ;; argument of the function call) and indent under.
|
|
635 (progn (forward-sexp 1)
|
|
636 (parse-partial-sexp (point)
|
|
637 calculate-lisp-indent-last-sexp
|
|
638 0 t)))
|
|
639 (backward-prefix-chars))
|
|
640 (t
|
|
641 ;; Indent beneath first sexp on same line as
|
|
642 ;; calculate-lisp-indent-last-sexp. Again, it's
|
|
643 ;; almost certainly a function call.
|
|
644 (goto-char calculate-lisp-indent-last-sexp)
|
|
645 (beginning-of-line)
|
|
646 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp
|
|
647 0 t)
|
|
648 (backward-prefix-chars)))))
|
|
649 ;; Point is at the point to indent under unless we are inside a string.
|
|
650 ;; Call indentation hook except when overridden by lisp-indent-offset
|
|
651 ;; or if the desired indentation has already been computed.
|
|
652 (let ((normal-indent (current-column)))
|
|
653 (cond ((elt state 3)
|
|
654 ;; Inside a string, don't change indentation.
|
|
655 (goto-char indent-point)
|
|
656 (skip-chars-forward " \t")
|
|
657 (current-column))
|
|
658 (desired-indent)
|
|
659 ((and (boundp 'lisp-indent-function)
|
|
660 lisp-indent-function
|
|
661 (not retry))
|
|
662 (or (funcall lisp-indent-function indent-point state)
|
|
663 normal-indent))
|
|
664 ;; XEmacs change:
|
|
665 ;; lisp-indent-offset shouldn't override lisp-indent-function !
|
|
666 ((and (integerp lisp-indent-offset) containing-sexp)
|
|
667 ;; Indent by constant offset
|
|
668 (goto-char containing-sexp)
|
|
669 (+ normal-indent lisp-indent-offset))
|
|
670 (t
|
|
671 normal-indent))))))
|
|
672
|
|
673 (defun lisp-indent-function (indent-point state)
|
|
674 ;; free reference to `calculate-lisp-indent-last-sexp'
|
|
675 ;; in #'calculate-lisp-indent
|
|
676 (let ((normal-indent (current-column)))
|
|
677 (goto-char (1+ (elt state 1)))
|
|
678 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
|
|
679 (if (and (elt state 2)
|
|
680 (not (looking-at "\\sw\\|\\s_")))
|
629
|
681 ;; car of form doesn't seem to be a symbol
|
428
|
682 (progn
|
|
683 (if (not (> (save-excursion (forward-line 1) (point))
|
|
684 calculate-lisp-indent-last-sexp))
|
|
685 (progn (goto-char calculate-lisp-indent-last-sexp)
|
|
686 (beginning-of-line)
|
|
687 (parse-partial-sexp (point)
|
|
688 calculate-lisp-indent-last-sexp 0 t)))
|
|
689 ;; Indent under the list or under the first sexp on the same
|
|
690 ;; line as calculate-lisp-indent-last-sexp. Note that first
|
|
691 ;; thing on that line has to be complete sexp since we are
|
|
692 ;; inside the innermost containing sexp.
|
|
693 (backward-prefix-chars)
|
|
694 (current-column))
|
|
695 (let ((function (buffer-substring (point)
|
|
696 (progn (forward-sexp 1) (point))))
|
|
697 method)
|
442
|
698 (if (condition-case nil
|
|
699 (save-excursion
|
|
700 (backward-up-list 1)
|
|
701 (backward-up-list 1)
|
|
702 (backward-up-list 1)
|
|
703 (looking-at "(flet\\s-"))
|
|
704 (error nil))
|
|
705 (setq method 'defun)
|
|
706 (setq method (or (get (intern-soft function) 'lisp-indent-function)
|
|
707 (get (intern-soft function) 'lisp-indent-hook))))
|
428
|
708 (cond ((or (eq method 'defun)
|
|
709 (and (null method)
|
|
710 (> (length function) 3)
|
|
711 (string-match "\\`def" function)))
|
|
712 (lisp-indent-defform state indent-point))
|
|
713 ((integerp method)
|
|
714 (lisp-indent-specform method state
|
|
715 indent-point normal-indent))
|
|
716 (method
|
|
717 (funcall method state indent-point)))))))
|
|
718
|
|
719 (defvar lisp-body-indent 2
|
|
720 "Number of columns to indent the second line of a `(def...)' form.")
|
|
721
|
|
722 (defun lisp-indent-specform (count state indent-point normal-indent)
|
|
723 (let ((containing-form-start (elt state 1))
|
|
724 (i count)
|
|
725 body-indent containing-form-column)
|
|
726 ;; Move to the start of containing form, calculate indentation
|
|
727 ;; to use for non-distinguished forms (> count), and move past the
|
|
728 ;; function symbol. lisp-indent-function guarantees that there is at
|
|
729 ;; least one word or symbol character following open paren of containing
|
|
730 ;; form.
|
|
731 (goto-char containing-form-start)
|
|
732 (setq containing-form-column (current-column))
|
|
733 (setq body-indent (+ lisp-body-indent containing-form-column))
|
|
734 (forward-char 1)
|
|
735 (forward-sexp 1)
|
|
736 ;; Now find the start of the last form.
|
|
737 (parse-partial-sexp (point) indent-point 1 t)
|
|
738 (while (and (< (point) indent-point)
|
|
739 (condition-case ()
|
|
740 (progn
|
|
741 (setq count (1- count))
|
|
742 (forward-sexp 1)
|
|
743 (parse-partial-sexp (point) indent-point 1 t))
|
|
744 (error nil))))
|
|
745 ;; Point is sitting on first character of last (or count) sexp.
|
|
746 (if (> count 0)
|
|
747 ;; A distinguished form. If it is the first or second form use double
|
|
748 ;; lisp-body-indent, else normal indent. With lisp-body-indent bound
|
|
749 ;; to 2 (the default), this just happens to work the same with if as
|
|
750 ;; the older code, but it makes unwind-protect, condition-case,
|
|
751 ;; with-output-to-temp-buffer, et. al. much more tasteful. The older,
|
|
752 ;; less hacked, behavior can be obtained by replacing below with
|
|
753 ;; (list normal-indent containing-form-start).
|
|
754 (if (<= (- i count) 1)
|
|
755 (list (+ containing-form-column (* 2 lisp-body-indent))
|
|
756 containing-form-start)
|
|
757 (list normal-indent containing-form-start))
|
|
758 ;; A non-distinguished form. Use body-indent if there are no
|
|
759 ;; distinguished forms and this is the first undistinguished form,
|
|
760 ;; or if this is the first undistinguished form and the preceding
|
|
761 ;; distinguished form has indentation at least as great as body-indent.
|
|
762 (if (or (and (= i 0) (= count 0))
|
|
763 (and (= count 0) (<= body-indent normal-indent)))
|
|
764 body-indent
|
|
765 normal-indent))))
|
|
766
|
|
767 (defun lisp-indent-defform (state indent-point)
|
|
768 (goto-char (car (cdr state)))
|
|
769 (forward-line 1)
|
|
770 (if (> (point) (car (cdr (cdr state))))
|
|
771 (progn
|
|
772 (goto-char (car (cdr state)))
|
|
773 (+ lisp-body-indent (current-column)))))
|
|
774
|
|
775
|
|
776 ;; (put 'progn 'lisp-indent-function 0), say, causes progn to be indented
|
|
777 ;; like defun if the first form is placed on the next line, otherwise
|
|
778 ;; it is indented like any other form (i.e. forms line up under first).
|
|
779
|
|
780 (put 'lambda 'lisp-indent-function 'defun)
|
|
781 (put 'autoload 'lisp-indent-function 'defun)
|
|
782 (put 'progn 'lisp-indent-function 0)
|
|
783 (put 'prog1 'lisp-indent-function 1)
|
|
784 (put 'prog2 'lisp-indent-function 2)
|
|
785 (put 'save-excursion 'lisp-indent-function 0)
|
|
786 (put 'save-window-excursion 'lisp-indent-function 0)
|
|
787 (put 'save-selected-window 'lisp-indent-function 0)
|
442
|
788 (put 'with-selected-window 'lisp-indent-function 1)
|
428
|
789 (put 'save-selected-frame 'lisp-indent-function 0)
|
|
790 (put 'with-selected-frame 'lisp-indent-function 1)
|
|
791 (put 'save-restriction 'lisp-indent-function 0)
|
|
792 (put 'save-match-data 'lisp-indent-function 0)
|
|
793 (put 'let 'lisp-indent-function 1)
|
|
794 (put 'let* 'lisp-indent-function 1)
|
|
795 (put 'let-specifier 'lisp-indent-function 1)
|
442
|
796 (put 'flet 'lisp-indent-function 1)
|
428
|
797 (put 'while 'lisp-indent-function 1)
|
|
798 (put 'if 'lisp-indent-function 2)
|
|
799 (put 'catch 'lisp-indent-function 1)
|
|
800 (put 'condition-case 'lisp-indent-function 2)
|
460
|
801 (put 'handler-case 'lisp-indent-function 1)
|
|
802 (put 'handler-bind 'lisp-indent-function 1)
|
428
|
803 (put 'call-with-condition-handler 'lisp-indent-function 2)
|
|
804 (put 'unwind-protect 'lisp-indent-function 1)
|
|
805 (put 'save-current-buffer 'lisp-indent-function 0)
|
|
806 (put 'with-current-buffer 'lisp-indent-function 1)
|
|
807 (put 'with-string-as-buffer-contents 'lisp-indent-function 1)
|
|
808 (put 'with-temp-file 'lisp-indent-function 1)
|
|
809 (put 'with-temp-buffer 'lisp-indent-function 0)
|
|
810 (put 'with-output-to-string 'lisp-indent-function 0)
|
|
811 (put 'with-output-to-temp-buffer 'lisp-indent-function 1)
|
460
|
812 (put 'with-slots 'lisp-indent-function 2)
|
|
813 (put 'with-open-file 'lisp-indent-function 1)
|
|
814 (put 'with-open-stream 'lisp-indent-function 1)
|
428
|
815 (put 'eval-after-load 'lisp-indent-function 1)
|
|
816 (put 'display-message 'lisp-indent-function 1)
|
|
817 (put 'display-warning 'lisp-indent-function 1)
|
|
818 (put 'lmessage 'lisp-indent-function 2)
|
|
819 (put 'lwarn 'lisp-indent-function 2)
|
|
820 (put 'global-set-key 'lisp-indent-function 1)
|
460
|
821 (put 'print-unreadable-object 'lisp-indent-function 1)
|
428
|
822
|
|
823 (defun indent-sexp (&optional endpos)
|
|
824 "Indent each line of the list starting just after point.
|
|
825 If optional arg ENDPOS is given, indent each line, stopping when
|
|
826 ENDPOS is encountered."
|
|
827 (interactive)
|
|
828 (let ((indent-stack (list nil))
|
|
829 (next-depth 0)
|
|
830 ;; If ENDPOS is non-nil, use nil as STARTING-POINT
|
|
831 ;; so that calculate-lisp-indent will find the beginning of
|
|
832 ;; the defun we are in.
|
|
833 ;; If ENDPOS is nil, it is safe not to scan before point
|
|
834 ;; since every line we indent is more deeply nested than point is.
|
|
835 (starting-point (if endpos nil (point)))
|
|
836 (last-point (point))
|
|
837 last-depth bol outer-loop-done inner-loop-done state this-indent)
|
|
838 (or endpos
|
|
839 ;; Get error now if we don't have a complete sexp after point.
|
|
840 (save-excursion (forward-sexp 1)))
|
|
841 (save-excursion
|
|
842 (setq outer-loop-done nil)
|
|
843 (while (if endpos (< (point) endpos)
|
|
844 (not outer-loop-done))
|
|
845 (setq last-depth next-depth
|
|
846 inner-loop-done nil)
|
|
847 ;; Parse this line so we can learn the state
|
|
848 ;; to indent the next line.
|
|
849 ;; This inner loop goes through only once
|
|
850 ;; unless a line ends inside a string.
|
|
851 (while (and (not inner-loop-done)
|
|
852 (not (setq outer-loop-done (eobp))))
|
|
853 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
|
|
854 nil nil state))
|
|
855 (setq next-depth (car state))
|
|
856 ;; If the line contains a comment other than the sort
|
|
857 ;; that is indented like code,
|
|
858 ;; indent it now with indent-for-comment.
|
|
859 ;; Comments indented like code are right already.
|
|
860 ;; In any case clear the in-comment flag in the state
|
|
861 ;; because parse-partial-sexp never sees the newlines.
|
|
862 (if (car (nthcdr 4 state))
|
|
863 (progn (indent-for-comment)
|
|
864 (end-of-line)
|
|
865 (setcar (nthcdr 4 state) nil)))
|
|
866 ;; If this line ends inside a string,
|
|
867 ;; go straight to next line, remaining within the inner loop,
|
|
868 ;; and turn off the \-flag.
|
|
869 (if (car (nthcdr 3 state))
|
|
870 (progn
|
|
871 (forward-line 1)
|
|
872 (setcar (nthcdr 5 state) nil))
|
|
873 (setq inner-loop-done t)))
|
|
874 (and endpos
|
|
875 (<= next-depth 0)
|
|
876 (progn
|
|
877 (setq indent-stack (append indent-stack
|
|
878 (make-list (- next-depth) nil))
|
|
879 last-depth (- last-depth next-depth)
|
|
880 next-depth 0)))
|
|
881 (or outer-loop-done endpos
|
|
882 (setq outer-loop-done (<= next-depth 0)))
|
|
883 (if outer-loop-done
|
|
884 (forward-line 1)
|
|
885 (while (> last-depth next-depth)
|
|
886 (setq indent-stack (cdr indent-stack)
|
|
887 last-depth (1- last-depth)))
|
|
888 (while (< last-depth next-depth)
|
|
889 (setq indent-stack (cons nil indent-stack)
|
|
890 last-depth (1+ last-depth)))
|
|
891 ;; Now go to the next line and indent it according
|
|
892 ;; to what we learned from parsing the previous one.
|
|
893 (forward-line 1)
|
|
894 (setq bol (point))
|
|
895 (skip-chars-forward " \t")
|
|
896 ;; But not if the line is blank, or just a comment
|
|
897 ;; (except for double-semi comments; indent them as usual).
|
|
898 (if (or (eobp) (looking-at "\\s<\\|\n"))
|
|
899 nil
|
|
900 (if (and (car indent-stack)
|
|
901 (>= (car indent-stack) 0))
|
|
902 (setq this-indent (car indent-stack))
|
|
903 (let ((val (calculate-lisp-indent
|
|
904 (if (car indent-stack) (- (car indent-stack))
|
|
905 starting-point))))
|
|
906 (if (integerp val)
|
|
907 (setcar indent-stack
|
|
908 (setq this-indent val))
|
|
909 (setcar indent-stack (- (car (cdr val))))
|
|
910 (setq this-indent (car val)))))
|
|
911 (if (/= (current-column) this-indent)
|
|
912 (progn (delete-region bol (point))
|
|
913 (indent-to this-indent)))))
|
|
914 (or outer-loop-done
|
|
915 (setq outer-loop-done (= (point) last-point))
|
|
916 (setq last-point (point)))))))
|
|
917
|
|
918 ;; Indent every line whose first char is between START and END inclusive.
|
|
919 (defun lisp-indent-region (start end)
|
|
920 (save-excursion
|
|
921 (let ((endmark (copy-marker end)))
|
|
922 (goto-char start)
|
|
923 (and (bolp) (not (eolp))
|
|
924 (lisp-indent-line))
|
|
925 (indent-sexp endmark)
|
|
926 (set-marker endmark nil))))
|
|
927
|
|
928 ;;;; Lisp paragraph filling commands.
|
|
929
|
|
930 (defun lisp-fill-paragraph (&optional justify)
|
|
931 "Like \\[fill-paragraph], but handle Emacs Lisp comments.
|
|
932 If any of the current line is a comment, fill the comment or the
|
|
933 paragraph of it that point is in, preserving the comment's indentation
|
|
934 and initial semicolons."
|
|
935 (interactive "P")
|
|
936 (let (
|
|
937 ;; Non-nil if the current line contains a comment.
|
|
938 has-comment
|
|
939
|
|
940 ;; Non-nil if the current line contains code and a comment.
|
|
941 has-code-and-comment
|
|
942
|
|
943 ;; If has-comment, the appropriate fill-prefix for the comment.
|
|
944 comment-fill-prefix
|
|
945 )
|
|
946
|
|
947 ;; Figure out what kind of comment we are looking at.
|
|
948 (save-excursion
|
|
949 (beginning-of-line)
|
|
950 (cond
|
|
951
|
|
952 ;; A line with nothing but a comment on it?
|
|
953 ((looking-at "[ \t]*;[; \t]*")
|
|
954 (setq has-comment t
|
|
955 comment-fill-prefix (buffer-substring (match-beginning 0)
|
|
956 (match-end 0))))
|
|
957
|
|
958 ;; A line with some code, followed by a comment? Remember that the
|
|
959 ;; semi which starts the comment shouldn't be part of a string or
|
|
960 ;; character.
|
|
961 ;; XEmacs Try this the FSF and see if it works.
|
|
962 ; ((progn
|
|
963 ; (while (not (looking-at ";\\|$"))
|
|
964 ; (skip-chars-forward "^;\n\"\\\\?")
|
|
965 ; (cond
|
|
966 ; ((eq (char-after (point)) ?\\) (forward-char 2))
|
|
967 ; ((memq (char-after (point)) '(?\" ??)) (forward-sexp 1))))
|
|
968 ; (looking-at ";+[\t ]*"))
|
|
969 ; (setq has-comment t)
|
|
970 ((condition-case nil
|
|
971 (save-restriction
|
|
972 (narrow-to-region (point-min)
|
|
973 (save-excursion (end-of-line) (point)))
|
|
974 (while (not (looking-at ";\\|$"))
|
|
975 (skip-chars-forward "^;\n\"\\\\?")
|
|
976 (cond
|
|
977 ((eq (char-after (point)) ?\\) (forward-char 2))
|
|
978 ((memq (char-after (point)) '(?\" ??)) (forward-sexp 1))))
|
|
979 (looking-at ";+[\t ]*"))
|
|
980 (error nil))
|
|
981 (setq has-comment t has-code-and-comment t)
|
|
982 (setq comment-fill-prefix
|
|
983 (concat (make-string (/ (current-column) 8) ?\t)
|
|
984 (make-string (% (current-column) 8) ?\ )
|
|
985 (buffer-substring (match-beginning 0) (match-end 0)))))))
|
|
986
|
|
987 (if (not has-comment)
|
|
988 (fill-paragraph justify)
|
|
989
|
|
990 ;; Narrow to include only the comment, and then fill the region.
|
|
991 (save-excursion
|
|
992 (save-restriction
|
|
993 (beginning-of-line)
|
|
994 (narrow-to-region
|
|
995 ;; Find the first line we should include in the region to fill.
|
|
996 (save-excursion
|
|
997 (while (and (zerop (forward-line -1))
|
|
998 (looking-at "^[ \t]*;")))
|
|
999 ;; We may have gone too far. Go forward again.
|
|
1000 (or (looking-at ".*;")
|
|
1001 (forward-line 1))
|
|
1002 (point))
|
|
1003 ;; Find the beginning of the first line past the region to fill.
|
|
1004 (save-excursion
|
|
1005 (while (progn (forward-line 1)
|
|
1006 (looking-at "^[ \t]*;")))
|
|
1007 (point)))
|
|
1008
|
|
1009 ;; Lines with only semicolons on them can be paragraph boundaries.
|
|
1010 (let* ((paragraph-start (concat paragraph-start "\\|[ \t;]*$"))
|
|
1011 (paragraph-separate (concat paragraph-start "\\|[ \t;]*$"))
|
|
1012 (paragraph-ignore-fill-prefix nil)
|
|
1013 (fill-prefix comment-fill-prefix)
|
|
1014 (after-line (if has-code-and-comment
|
|
1015 (save-excursion
|
|
1016 (forward-line 1) (point))))
|
|
1017 (end (progn
|
|
1018 (forward-paragraph)
|
|
1019 (or (bolp) (newline 1))
|
|
1020 (point)))
|
|
1021 ;; If this comment starts on a line with code,
|
|
1022 ;; include that like in the filling.
|
|
1023 (beg (progn (backward-paragraph)
|
|
1024 (if (eq (point) after-line)
|
|
1025 (forward-line -1))
|
|
1026 (point))))
|
|
1027 (fill-region-as-paragraph beg end
|
|
1028 justify nil
|
|
1029 (save-excursion
|
|
1030 (goto-char beg)
|
|
1031 (if (looking-at fill-prefix)
|
|
1032 nil
|
|
1033 (re-search-forward comment-start-skip)
|
|
1034 (point))))))))
|
|
1035 t))
|
|
1036
|
|
1037 (defun indent-code-rigidly (start end arg &optional nochange-regexp)
|
|
1038 "Indent all lines of code, starting in the region, sideways by ARG columns.
|
|
1039 Does not affect lines starting inside comments or strings, assuming that
|
|
1040 the start of the region is not inside them.
|
|
1041
|
|
1042 Called from a program, takes args START, END, COLUMNS and NOCHANGE-REGEXP.
|
|
1043 The last is a regexp which, if matched at the beginning of a line,
|
|
1044 means don't indent that line."
|
|
1045 (interactive "r\np")
|
|
1046 (let (state)
|
|
1047 (save-excursion
|
|
1048 (goto-char end)
|
|
1049 (setq end (point-marker))
|
|
1050 (goto-char start)
|
|
1051 (or (bolp)
|
|
1052 (setq state (parse-partial-sexp (point)
|
|
1053 (progn
|
|
1054 (forward-line 1) (point))
|
|
1055 nil nil state)))
|
|
1056 (while (< (point) end)
|
|
1057 (or (car (nthcdr 3 state))
|
|
1058 (and nochange-regexp
|
|
1059 (looking-at nochange-regexp))
|
|
1060 ;; If line does not start in string, indent it
|
|
1061 (let ((indent (current-indentation)))
|
|
1062 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
|
|
1063 (or (eolp)
|
|
1064 (indent-to (max 0 (+ indent arg)) 0))))
|
|
1065 (setq state (parse-partial-sexp (point)
|
|
1066 (progn
|
|
1067 (forward-line 1) (point))
|
|
1068 nil nil state))))))
|
|
1069
|
|
1070 (provide 'lisp-mode)
|
|
1071
|
|
1072 ;;; lisp-mode.el ends here
|