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