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