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