comparison lisp/lisp-mode.el @ 209:41ff10fd062f r20-4b3

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