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