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