Mercurial > hg > xemacs-beta
annotate lisp/lisp-mode.el @ 5142:f965e31a35f0
reduce lcrecord headers to 2 words, rename printing_unreadable_object
-------------------- ChangeLog entries follow: --------------------
man/ChangeLog addition:
2010-03-13 Ben Wing <ben@xemacs.org>
* internals/internals.texi (Working with Lisp Objects):
* internals/internals.texi (Writing Macros):
* internals/internals.texi (lrecords):
More rewriting to correspond with changes from
*LRECORD* to *LISP_OBJECT*.
modules/ChangeLog addition:
2010-03-13 Ben Wing <ben@xemacs.org>
* postgresql/postgresql.c (print_pgconn):
* postgresql/postgresql.c (print_pgresult):
printing_unreadable_object -> printing_unreadable_object_fmt.
2010-03-13 Ben Wing <ben@xemacs.org>
* ldap/eldap.c (print_ldap):
printing_unreadable_object -> printing_unreadable_object_fmt.
src/ChangeLog addition:
2010-03-13 Ben Wing <ben@xemacs.org>
* alloc.c (alloc_sized_lrecord_1):
* alloc.c (alloc_sized_lrecord_array):
* alloc.c (old_alloc_sized_lcrecord):
* alloc.c (disksave_object_finalization_1):
* alloc.c (mark_lcrecord_list):
* alloc.c (alloc_managed_lcrecord):
* alloc.c (free_managed_lcrecord):
* alloc.c (tick_lcrecord_stats):
* alloc.c (sweep_lcrecords_1):
* buffer.c (print_buffer):
* buffer.c (DEFVAR_BUFFER_LOCAL_1):
* casetab.c:
* casetab.c (print_case_table):
* console.c (print_console):
* console.c (DEFVAR_CONSOLE_LOCAL_1):
* data.c (print_weak_list):
* data.c (print_weak_box):
* data.c (print_ephemeron):
* data.c (ephemeron_equal):
* database.c (print_database):
* database.c (finalize_database):
* device-msw.c (sync_printer_with_devmode):
* device-msw.c (print_devmode):
* device-msw.c (finalize_devmode):
* device.c:
* device.c (print_device):
* elhash.c:
* elhash.c (print_hash_table):
* eval.c (print_subr):
* eval.c (print_multiple_value):
* event-stream.c (event_stream_resignal_wakeup):
* events.c (clear_event_resource):
* events.c (zero_event):
* events.c (print_event):
* extents.c:
* extents.c (print_extent):
* file-coding.c (print_coding_system):
* font-mgr.c:
* font-mgr.c (Ffc_init):
* frame.c:
* frame.c (print_frame):
* gc.c:
* gc.c (GC_CHECK_NOT_FREE):
* glyphs.c:
* glyphs.c (print_image_instance):
* glyphs.c (print_glyph):
* gui.c (print_gui_item):
* gui.c (copy_gui_item):
* keymap.c (print_keymap):
* keymap.c (MARKED_SLOT):
* lisp.h:
* lisp.h (struct Lisp_String):
* lisp.h (DEFUN):
* lisp.h (DEFUN_NORETURN):
* lrecord.h:
* lrecord.h (NORMAL_LISP_OBJECT_UID):
* lrecord.h (struct lrecord_header):
* lrecord.h (set_lheader_implementation):
* lrecord.h (struct old_lcrecord_header):
* lrecord.h (struct free_lcrecord_header):
* marker.c (print_marker):
* mule-charset.c:
* mule-charset.c (print_charset):
* objects.c (print_color_instance):
* objects.c (print_font_instance):
* objects.c (finalize_font_instance):
* print.c (print_cons):
* print.c (printing_unreadable_object_fmt):
* print.c (printing_unreadable_lisp_object):
* print.c (external_object_printer):
* print.c (internal_object_printer):
* print.c (debug_p4):
* print.c (ext_print_begin):
* process.c (print_process):
* rangetab.c (print_range_table):
* rangetab.c (range_table_equal):
* scrollbar.c (free_scrollbar_instance):
* specifier.c (print_specifier):
* specifier.c (finalize_specifier):
* symbols.c (guts_of_unbound_marker):
* symeval.h:
* symeval.h (DEFVAR_SYMVAL_FWD):
* tooltalk.c:
* tooltalk.c (print_tooltalk_message):
* tooltalk.c (print_tooltalk_pattern):
* ui-gtk.c (ffi_object_printer):
* ui-gtk.c (emacs_gtk_object_printer):
* ui-gtk.c (emacs_gtk_boxed_printer):
* window.c (print_window):
* window.c (free_window_mirror):
* window.c (debug_print_window):
* xemacs.def.in.in:
(1) printing_unreadable_object -> printing_unreadable_object_fmt.
(2) printing_unreadable_lcrecord -> printing_unreadable_lisp_object
and fix up so it no longer requires an lcrecord.
These previous changes eliminate most of the remaining places where
the terms `lcrecord' and `lrecord' occurred outside of specialized
code.
(3) Fairly major change: Reduce the number of words in an lcrecord
from 3 to 2. The third word consisted of a uid that duplicated the
lrecord uid, and a single free bit, which was moved into the lrecord
structure. This reduces the size of the `uid' slot from 21 bits to
20 bits. Arguably this isn't enough -- we could easily have more than
1,000,000 or so objects created in a session. The answer is
(a) It doesn't really matter if we overflow the uid field because
it's only used for debugging, to identify an object uniquely
(or pretty much so).
(b) If we cared about it overflowing and wanted to reduce this,
we could make it so that cons, string, float and certain other
frob-block types that never print out the uid simply don't
store a uid in them and don't increment the lrecord_uid_counter.
(4) In conjunction with (3), create new macro NORMAL_LISP_OBJECT_UID()
and use it to abstract out the differences between NEWGC and old-GC
in accessing the `uid' value from a "normal Lisp Object pointer".
(5) In events.c, use zero_nonsized_lisp_object() in place of custom-
written equivalent. In font-mgr.c use external_object_printer()
in place of custom-written equivalents.
author | Ben Wing <ben@xemacs.org> |
---|---|
date | Sat, 13 Mar 2010 05:38:08 -0600 |
parents | e4bbe5622a80 |
children | 77907bd57d25 |
rev | line source |
---|---|
428 | 1 ;;; lisp-mode.el --- Lisp mode, and its idiosyncratic commands. |
2 | |
3 ;; Copyright (C) 1985, 1996, 1997 Free Software Foundation, Inc. | |
442 | 4 ;; Copyright (C) 1995 Tinker Systems. |
4942
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
5 ;; Copyright (C) 2002, 2010 Ben Wing. |
428 | 6 |
7 ;; Maintainer: FSF | |
8 ;; Keywords: lisp, languages, dumped | |
9 | |
10 ;; This file is part of XEmacs. | |
11 | |
12 ;; XEmacs is free software; you can redistribute it and/or modify it | |
13 ;; under the terms of the GNU General Public License as published by | |
14 ;; the Free Software Foundation; either version 2, or (at your option) | |
15 ;; any later version. | |
16 | |
17 ;; XEmacs is distributed in the hope that it will be useful, but | |
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
20 ;; General Public License for more details. | |
21 | |
22 ;; You should have received a copy of the GNU General Public License | |
23 ;; along with XEmacs; see the file COPYING. If not, write to the Free | |
24 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
25 ;; 02111-1307, USA. | |
26 | |
27 ;;; Synched up with: FSF 19.34 (but starting to diverge). | |
28 | |
29 ;;; Commentary: | |
30 | |
31 ;; This file is dumped with XEmacs. | |
32 | |
33 ;; The base major mode for editing Lisp code (used also for Emacs Lisp). | |
34 ;; This mode is documented in the Emacs manual | |
35 | |
36 ;;; Code: | |
37 | |
38 (defgroup lisp nil | |
39 "Lisp support, including Emacs Lisp." | |
40 :group 'languages | |
41 :group 'development) | |
42 | |
43 (defvar lisp-mode-syntax-table nil) | |
44 (defvar emacs-lisp-mode-syntax-table nil) | |
45 (defvar lisp-mode-abbrev-table nil) | |
46 | |
442 | 47 (defun construct-lisp-mode-menu (popup-p emacs-lisp-p) |
48 (flet ((popup-wrap (form) | |
49 (if popup-p `(menu-call-at-event ',form) form))) | |
50 `(,@(if emacs-lisp-p | |
51 `(["%_Byte-Compile This File" ,(popup-wrap | |
52 'emacs-lisp-byte-compile)] | |
53 ["B%_yte-Compile/Load This File" | |
54 ,(popup-wrap 'emacs-lisp-byte-compile-and-load)] | |
55 ["Byte-%_Recompile Directory..." | |
56 ,(popup-wrap 'byte-recompile-directory)] | |
57 "---")) | |
58 ["%_Evaluate Region or Defun" | |
59 ,(popup-wrap '(if (region-exists-p) | |
60 (call-interactively 'eval-region) | |
61 (call-interactively 'eval-defun)))] | |
62 ["Evaluate %_Whole Buffer" ,(popup-wrap 'eval-current-buffer)] | |
63 ["Evaluate Last %_S-expression" ,(popup-wrap 'eval-last-sexp)] | |
64 "---" | |
800 | 65 ["%_Indent Region or Balanced Expression" |
66 ,(popup-wrap '(if (region-exists-p) | |
67 (call-interactively 'indent-region) | |
68 (call-interactively 'indent-sexp)))] | |
69 ["I%_ndent Defun" | |
70 ,(popup-wrap '(progn | |
71 (beginning-of-defun) | |
72 (indent-sexp)))] | |
73 "---" | |
74 ["%_Comment Out Region" comment-region :active (region-exists-p)] | |
1333 | 75 ["Unc%_omment Region" uncomment-region :active (region-exists-p)] |
800 | 76 "---" |
442 | 77 ,@(if popup-p |
78 '(["%_Find Function" | |
79 (find-function (menu-call-at-event '(function-at-point))) | |
80 :suffix (let ((fun (menu-call-at-event '(function-at-point)))) | |
81 (if fun (symbol-name fun) "")) | |
82 :active (and (fboundp 'find-function) | |
83 (menu-call-at-event '(function-at-point)))] | |
84 ["%_Find Variable" | |
85 (find-variable (menu-call-at-event '(variable-at-point))) | |
86 :suffix (let ((fun (menu-call-at-event '(variable-at-point)))) | |
87 (if fun (symbol-name fun) "")) | |
88 :active (and (fboundp 'find-variable) | |
89 (menu-call-at-event '(variable-at-point)))] | |
90 ["%_Help on Function" | |
91 (describe-function (menu-call-at-event '(function-at-point))) | |
92 :suffix (let ((fun (menu-call-at-event '(function-at-point)))) | |
93 (if fun (symbol-name fun) "")) | |
94 :active (and (fboundp 'describe-function) | |
95 (menu-call-at-event '(function-at-point)))] | |
96 ["%_Help on Variable" | |
97 (describe-variable (menu-call-at-event '(variable-at-point))) | |
98 :suffix (let ((fun (menu-call-at-event '(variable-at-point)))) | |
99 (if fun (symbol-name fun) "")) | |
100 :active (and (fboundp 'describe-variable) | |
101 (menu-call-at-event '(variable-at-point)))]) | |
102 '(["Find %_Function..." find-function | |
103 :active (fboundp 'find-function)] | |
104 ["Find %_Variable..." find-variable | |
105 :active (fboundp 'find-variable)] | |
106 ["%_Help on Function..." describe-function | |
107 :active (fboundp 'describe-function)] | |
108 ["Hel%_p on Variable..." describe-variable | |
109 :active (fboundp 'describe-variable)])) | |
110 "---" | |
111 ["Instrument This Defun for %_Debugging" ,(popup-wrap 'edebug-defun)] | |
112 ["%_Trace Function..." trace-function-background] | |
113 ["%_Untrace All Functions" untrace-all | |
114 :active (fboundp 'untrace-all)] | |
115 "---" | |
800 | 116 ["Display %_Macro Expansion of Balanced Expression" |
117 ,(popup-wrap 'macroexpand-sexp)] | |
118 ["Display Recursive Macro E%_xpansion of Balanced Expression" | |
119 ,(popup-wrap 'macroexpand-all-sexp)] | |
442 | 120 "---" |
121 "Look for debug-on-error under Options->Troubleshooting" | |
122 ))) | |
428 | 123 |
442 | 124 (defvar lisp-interaction-mode-popup-menu |
125 (cons "Lisp-Interaction" (construct-lisp-mode-menu t nil))) | |
126 | |
127 (defvar emacs-lisp-mode-popup-menu | |
128 (cons "Emacs-Lisp" (construct-lisp-mode-menu t t))) | |
428 | 129 |
130 ;Don't have a menubar entry in Lisp Interaction mode. Otherwise, the | |
131 ;*scratch* buffer has a Lisp menubar item! Very confusing. | |
442 | 132 ;Jan Vroonhof really wants this, so it's back. --ben |
133 (defvar lisp-interaction-mode-menubar-menu | |
134 (cons "%_Lisp" (construct-lisp-mode-menu nil nil))) | |
428 | 135 |
442 | 136 (defvar emacs-lisp-mode-menubar-menu |
137 (cons "%_Lisp" (construct-lisp-mode-menu nil t))) | |
428 | 138 |
139 (if (not emacs-lisp-mode-syntax-table) | |
140 (let ((i 0)) | |
141 (setq emacs-lisp-mode-syntax-table (make-syntax-table)) | |
142 (while (< i ?0) | |
143 (modify-syntax-entry i "_ " emacs-lisp-mode-syntax-table) | |
144 (setq i (1+ i))) | |
145 (setq i (1+ ?9)) | |
146 (while (< i ?A) | |
147 (modify-syntax-entry i "_ " emacs-lisp-mode-syntax-table) | |
148 (setq i (1+ i))) | |
149 (setq i (1+ ?Z)) | |
150 (while (< i ?a) | |
151 (modify-syntax-entry i "_ " emacs-lisp-mode-syntax-table) | |
152 (setq i (1+ i))) | |
153 (setq i (1+ ?z)) | |
154 (while (< i 128) | |
155 (modify-syntax-entry i "_ " emacs-lisp-mode-syntax-table) | |
156 (setq i (1+ i))) | |
157 (modify-syntax-entry ? " " emacs-lisp-mode-syntax-table) | |
158 (modify-syntax-entry ?\t " " emacs-lisp-mode-syntax-table) | |
159 (modify-syntax-entry ?\f " " emacs-lisp-mode-syntax-table) | |
160 (modify-syntax-entry ?\n "> " emacs-lisp-mode-syntax-table) | |
161 ;; Give CR the same syntax as newline, for selective-display. | |
162 (modify-syntax-entry ?\^m "> " emacs-lisp-mode-syntax-table) | |
163 (modify-syntax-entry ?\; "< " emacs-lisp-mode-syntax-table) | |
164 (modify-syntax-entry ?` "' " emacs-lisp-mode-syntax-table) | |
165 (modify-syntax-entry ?' "' " emacs-lisp-mode-syntax-table) | |
166 (modify-syntax-entry ?, "' " emacs-lisp-mode-syntax-table) | |
167 ;; Used to be singlequote; changed for flonums. | |
168 (modify-syntax-entry ?. "_ " emacs-lisp-mode-syntax-table) | |
169 (modify-syntax-entry ?# "' " emacs-lisp-mode-syntax-table) | |
170 (modify-syntax-entry ?\" "\" " emacs-lisp-mode-syntax-table) | |
171 (modify-syntax-entry ?\\ "\\ " emacs-lisp-mode-syntax-table) | |
172 (modify-syntax-entry ?\( "() " emacs-lisp-mode-syntax-table) | |
173 (modify-syntax-entry ?\) ")( " emacs-lisp-mode-syntax-table) | |
174 (modify-syntax-entry ?\[ "(] " emacs-lisp-mode-syntax-table) | |
175 (modify-syntax-entry ?\] ")[ " emacs-lisp-mode-syntax-table))) | |
176 | |
177 (if (not lisp-mode-syntax-table) | |
178 (progn (setq lisp-mode-syntax-table | |
179 (copy-syntax-table emacs-lisp-mode-syntax-table)) | |
180 (modify-syntax-entry ?\[ "_ " lisp-mode-syntax-table) | |
181 ;; XEmacs changes | |
182 (modify-syntax-entry ?\] "_ " lisp-mode-syntax-table) | |
460 | 183 (modify-syntax-entry ?# "' 58" lisp-mode-syntax-table) |
184 (modify-syntax-entry ?| "\" 67" lisp-mode-syntax-table))) | |
428 | 185 |
186 (define-abbrev-table 'lisp-mode-abbrev-table ()) | |
187 | |
188 (defvar lisp-imenu-generic-expression | |
189 '( | |
190 (nil | |
191 "^\\s-*(def\\(un\\|subst\\|macro\\|advice\\)\\s-+\\([-A-Za-z0-9+*|:]+\\)" 2) | |
192 ("Variables" | |
193 "^\\s-*(def\\(var\\|const\\|custom\\)\\s-+\\([-A-Za-z0-9+*|:]+\\)" 2) | |
194 ("Types" | |
195 "^\\s-*(def\\(group\\|type\\|struct\\|class\\|ine-condition\\)\\s-+\\([-A-Za-z0-9+*|:]+\\)" | |
196 2)) | |
197 | |
198 "Imenu generic expression for Lisp mode. See `imenu-generic-expression'.") | |
199 | |
200 (defun lisp-mode-variables (lisp-syntax) | |
201 (cond (lisp-syntax | |
202 (set-syntax-table lisp-mode-syntax-table))) | |
203 (setq local-abbrev-table lisp-mode-abbrev-table) | |
204 (make-local-variable 'paragraph-start) | |
205 (setq paragraph-start (concat page-delimiter "\\|$" )) | |
206 (make-local-variable 'paragraph-separate) | |
207 (setq paragraph-separate paragraph-start) | |
208 (make-local-variable 'paragraph-ignore-fill-prefix) | |
209 (setq paragraph-ignore-fill-prefix t) | |
210 (make-local-variable 'fill-paragraph-function) | |
211 (setq fill-paragraph-function 'lisp-fill-paragraph) | |
212 ;; Adaptive fill mode gets in the way of auto-fill, | |
213 ;; and should make no difference for explicit fill | |
214 ;; because lisp-fill-paragraph should do the job. | |
215 (make-local-variable 'adaptive-fill-mode) | |
216 (setq adaptive-fill-mode nil) | |
217 (make-local-variable 'indent-line-function) | |
218 (setq indent-line-function 'lisp-indent-line) | |
219 (make-local-variable 'indent-region-function) | |
220 (setq indent-region-function 'lisp-indent-region) | |
221 (make-local-variable 'parse-sexp-ignore-comments) | |
222 (setq parse-sexp-ignore-comments t) | |
223 (make-local-variable 'outline-regexp) | |
224 (setq outline-regexp ";;; \\|(....") | |
225 (make-local-variable 'comment-start) | |
226 (setq comment-start ";") | |
227 ;; XEmacs change | |
228 (set (make-local-variable 'block-comment-start) ";;") | |
229 (make-local-variable 'comment-start-skip) | |
230 ;; Look within the line for a ; following an even number of backslashes | |
231 ;; after either a non-backslash or the line beginning. | |
232 (setq comment-start-skip "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\);+ *") | |
233 (make-local-variable 'comment-column) | |
234 (setq comment-column 40) | |
235 (make-local-variable 'comment-indent-function) | |
236 (setq comment-indent-function 'lisp-comment-indent) | |
237 ;; XEmacs change | |
238 (set (make-local-variable 'dabbrev-case-fold-search) nil) | |
239 (set (make-local-variable 'dabbrev-case-replace) nil) | |
240 (make-local-variable 'imenu-generic-expression) | |
241 (setq imenu-generic-expression lisp-imenu-generic-expression)) | |
242 | |
243 (defvar shared-lisp-mode-map () | |
244 "Keymap for commands shared by all sorts of Lisp modes.") | |
245 | |
246 (if shared-lisp-mode-map | |
247 () | |
248 (setq shared-lisp-mode-map (make-sparse-keymap)) | |
249 ;; XEmacs changes | |
250 (set-keymap-name shared-lisp-mode-map 'shared-lisp-mode-map) | |
251 (define-key shared-lisp-mode-map "\M-;" 'lisp-indent-for-comment) | |
252 (define-key shared-lisp-mode-map "\e\C-q" 'indent-sexp)) | |
253 | |
254 (defvar emacs-lisp-mode-map () | |
255 "Keymap for Emacs Lisp mode. | |
256 All commands in `shared-lisp-mode-map' are inherited by this map.") | |
257 | |
258 (if emacs-lisp-mode-map | |
259 () | |
260 ;; XEmacs: Ignore FSF nconc stuff | |
261 (setq emacs-lisp-mode-map (make-sparse-keymap)) | |
262 (set-keymap-name emacs-lisp-mode-map 'emacs-lisp-mode-map) | |
263 (set-keymap-parents emacs-lisp-mode-map (list shared-lisp-mode-map)) | |
264 (define-key emacs-lisp-mode-map "\e\t" 'lisp-complete-symbol) | |
265 (define-key emacs-lisp-mode-map "\e\C-x" 'eval-defun) | |
266 ;; XEmacs: Not sure what the FSF menu bindings are. I hope XEmacs | |
267 ;; doesn't need them. | |
268 ) | |
269 | |
1428 | 270 ;; XEmacs: add docstrings to the hooks |
271 (defvar emacs-lisp-mode-hook nil | |
272 "Hook to run when entering emacs-lisp-mode.") | |
273 | |
274 (defvar lisp-mode-hook nil | |
275 "Hook to run when entering lisp-mode.") | |
276 | |
277 (defvar lisp-interaction-mode-hook nil | |
278 "Hook to run when entering lisp-interaction-mode.") | |
279 | |
428 | 280 (defun emacs-lisp-byte-compile () |
281 "Byte compile the file containing the current buffer." | |
282 (interactive) | |
283 (if buffer-file-name | |
284 ;; XEmacs change. Force buffer save first | |
285 (progn | |
286 (save-buffer) | |
287 (byte-compile-file buffer-file-name)) | |
288 (error "The buffer must be saved in a file first."))) | |
289 | |
290 (defun emacs-lisp-byte-compile-and-load () | |
291 "Byte-compile the current file (if it has changed), then load compiled code." | |
292 (interactive) | |
293 (or buffer-file-name | |
294 (error "The buffer must be saved in a file first")) | |
295 (require 'bytecomp) | |
296 ;; Recompile if file or buffer has changed since last compilation. | |
297 (if (and (buffer-modified-p) | |
298 (y-or-n-p (format "save buffer %s first? " (buffer-name)))) | |
299 (save-buffer)) | |
300 (let ((compiled-file-name (byte-compile-dest-file buffer-file-name))) | |
301 (if (file-newer-than-file-p compiled-file-name buffer-file-name) | |
302 (load-file compiled-file-name) | |
303 (byte-compile-file buffer-file-name t)))) | |
304 | |
305 (defun emacs-lisp-mode () | |
306 "Major mode for editing Lisp code to run in Emacs. | |
307 Commands: | |
308 Delete converts tabs to spaces as it moves back. | |
309 Blank lines separate paragraphs. Semicolons start comments. | |
310 \\{emacs-lisp-mode-map} | |
311 Entry to this mode calls the value of `emacs-lisp-mode-hook' | |
312 if that value is non-nil." | |
313 (interactive) | |
314 (kill-all-local-variables) | |
315 (use-local-map emacs-lisp-mode-map) | |
316 (set-syntax-table emacs-lisp-mode-syntax-table) | |
317 ;; XEmacs changes | |
318 (setq major-mode 'emacs-lisp-mode | |
442 | 319 mode-popup-menu emacs-lisp-mode-popup-menu |
428 | 320 mode-name "Emacs-Lisp") |
442 | 321 (if (and (featurep 'menubar) |
322 current-menubar) | |
323 (progn | |
428 | 324 ;; make a local copy of the menubar, so our modes don't |
325 ;; change the global menubar | |
442 | 326 (set-buffer-menubar current-menubar) |
327 (add-submenu nil emacs-lisp-mode-menubar-menu))) | |
428 | 328 (lisp-mode-variables nil) |
329 (run-hooks 'emacs-lisp-mode-hook)) | |
330 | |
430 | 331 (put 'emacs-lisp-mode 'font-lock-lisp-like t) |
332 | |
428 | 333 (defvar lisp-mode-map () |
334 "Keymap for ordinary Lisp mode. | |
335 All commands in `shared-lisp-mode-map' are inherited by this map.") | |
336 | |
337 (if lisp-mode-map | |
338 () | |
339 ;; XEmacs changes | |
340 (setq lisp-mode-map (make-sparse-keymap)) | |
341 (set-keymap-name lisp-mode-map 'lisp-mode-map) | |
342 (set-keymap-parents lisp-mode-map (list shared-lisp-mode-map)) | |
343 ;; gag, no. use ilisp. -jwz | |
344 ;; (define-key lisp-mode-map "\C-c\C-z" 'run-lisp) | |
345 ) | |
346 | |
347 (defun lisp-mode () | |
613 | 348 "Major mode for editing Lisp code for Lisps other than Emacs Lisp. |
428 | 349 Commands: |
350 Delete converts tabs to spaces as it moves back. | |
351 Blank lines separate paragraphs. Semicolons start comments. | |
352 \\{lisp-mode-map} | |
353 Note that `run-lisp' may be used either to start an inferior Lisp job | |
354 or to switch back to an existing one. | |
355 | |
356 Entry to this mode calls the value of `lisp-mode-hook' | |
357 if that value is non-nil." | |
358 (interactive) | |
359 (kill-all-local-variables) | |
360 (use-local-map lisp-mode-map) | |
361 (setq major-mode 'lisp-mode) | |
362 (setq mode-name "Lisp") | |
363 (lisp-mode-variables t) | |
364 (set-syntax-table lisp-mode-syntax-table) | |
365 (run-hooks 'lisp-mode-hook)) | |
366 | |
367 ;; XEmacs change: emacs-lisp-mode-map is a more appropriate parent. | |
368 (defvar lisp-interaction-mode-map () | |
369 "Keymap for Lisp Interaction mode. | |
370 All commands in `shared-lisp-mode-map' are inherited by this map.") | |
371 | |
372 (if lisp-interaction-mode-map | |
373 () | |
374 ;; XEmacs set keymap our way | |
375 (setq lisp-interaction-mode-map (make-sparse-keymap)) | |
376 (set-keymap-name lisp-interaction-mode-map 'lisp-interaction-mode-map) | |
377 (set-keymap-parents lisp-interaction-mode-map (list emacs-lisp-mode-map)) | |
378 (define-key lisp-interaction-mode-map "\e\C-x" 'eval-defun) | |
379 (define-key lisp-interaction-mode-map "\e\t" 'lisp-complete-symbol) | |
380 (define-key lisp-interaction-mode-map "\n" 'eval-print-last-sexp)) | |
381 | |
382 (defun lisp-interaction-mode () | |
383 "Major mode for typing and evaluating Lisp forms. | |
384 Like Lisp mode except that \\[eval-print-last-sexp] evals the Lisp expression | |
385 before point, and prints its value into the buffer, advancing point. | |
386 | |
387 Commands: | |
388 Delete converts tabs to spaces as it moves back. | |
389 Paragraphs are separated only by blank lines. | |
390 Semicolons start comments. | |
391 \\{lisp-interaction-mode-map} | |
392 Entry to this mode calls the value of `lisp-interaction-mode-hook' | |
393 if that value is non-nil." | |
394 (interactive) | |
395 (kill-all-local-variables) | |
396 (use-local-map lisp-interaction-mode-map) | |
397 (setq major-mode 'lisp-interaction-mode) | |
398 (setq mode-name "Lisp Interaction") | |
442 | 399 (setq mode-popup-menu lisp-interaction-mode-popup-menu) |
400 (if (and (featurep 'menubar) | |
401 current-menubar) | |
402 (progn | |
403 ;; make a local copy of the menubar, so our modes don't | |
404 ;; change the global menubar | |
405 (set-buffer-menubar current-menubar) | |
406 (add-submenu nil lisp-interaction-mode-menubar-menu))) | |
428 | 407 (set-syntax-table emacs-lisp-mode-syntax-table) |
408 (lisp-mode-variables nil) | |
409 (run-hooks 'lisp-interaction-mode-hook)) | |
410 | |
411 (defun eval-print-last-sexp () | |
412 "Evaluate sexp before point; print value into current buffer." | |
413 (interactive) | |
414 (let ((standard-output (current-buffer))) | |
415 (terpri) | |
416 (eval-last-sexp t) | |
417 (terpri))) | |
418 | |
419 ;; XEmacs change | |
420 (defcustom eval-interactive-verbose t | |
421 "*Non-nil means that interactive evaluation can print messages. | |
422 The messages are printed when the expression is treated differently | |
2116 | 423 using `\\[eval-last-sexp]' and `\\[eval-defun]' than it would have |
424 been treated noninteractively. | |
428 | 425 |
426 The printed messages are \"defvar treated as defconst\" and \"defcustom | |
4677
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
427 evaluation forced\". See `eval-interactive' for more details." |
428 | 428 :type 'boolean |
429 :group 'lisp) | |
430 | |
431 (defun eval-interactive (expr) | |
4677
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
432 "Evaluate EXPR; pass back multiple values, transform defvars to defconsts. |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
433 |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
434 Always returns a list. The length of this list will be something other than |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
435 one if the form returned multiple values. It will be zero if the form |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
436 returned a single zero-length multiple value." |
428 | 437 (cond ((and (eq (car-safe expr) 'defvar) |
438 (> (length expr) 2)) | |
4677
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
439 (setq expr (multiple-value-list (eval (cons 'defconst (cdr expr))))) |
428 | 440 (when eval-interactive-verbose |
441 (message "defvar treated as defconst") | |
442 (sit-for 1) | |
443 (message "")) | |
4677
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
444 expr) |
428 | 445 ((and (eq (car-safe expr) 'defcustom) |
446 (> (length expr) 2) | |
447 (default-boundp (nth 1 expr))) | |
448 ;; Force variable to be bound | |
4677
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
449 (funcall |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
450 (or (plist-get expr :set) #'custom-set-default) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
451 (nth 1 expr) (eval (nth 2 expr))) |
428 | 452 ;; And evaluate the defcustom |
4677
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
453 (setq expr (multiple-value-list (eval expr))) |
428 | 454 (when eval-interactive-verbose |
455 (message "defcustom evaluation forced") | |
456 (sit-for 1) | |
457 (message "")) | |
4677
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
458 expr) |
428 | 459 (t |
4677
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
460 (multiple-value-list (eval expr))))) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
461 |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
462 (defun prin1-list-as-multiple-values (multiple-value-list &optional stream) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
463 "Call `prin1' on each element of MULTIPLE-VALUE-LIST, separated by \" ;\\n\" |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
464 |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
465 If MULTIPLE-VALUE-LIST is zero-length, print the text |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
466 \"#<zero length multiple value> ;\\n\". Always returns nil." |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
467 (loop for value in multiple-value-list |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
468 with seen-first = nil |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
469 do |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
470 (if seen-first |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
471 (princ " ;\n" stream) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
472 (setq seen-first t)) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
473 (prin1 value stream) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
474 finally (unless seen-first |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
475 (princ "#<zero length multiple value> ;" stream)))) |
428 | 476 |
477 ;; XEmacs change, based on Bob Weiner suggestion | |
478 (defun eval-last-sexp (eval-last-sexp-arg-internal) ;dynamic scoping wonderment | |
479 "Evaluate sexp before point; print value in minibuffer. | |
480 With argument, print output into current buffer." | |
481 (interactive "P") | |
482 (let ((standard-output (if eval-last-sexp-arg-internal (current-buffer) t)) | |
483 (opoint (point)) | |
484 ignore-quotes) | |
4677
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
485 (prin1-list-as-multiple-values |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
486 (eval-interactive |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
487 (letf (((syntax-table) emacs-lisp-mode-syntax-table)) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
488 (save-excursion |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
489 ;; If this sexp appears to be enclosed in `...' then |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
490 ;; ignore the surrounding quotes. |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
491 (setq ignore-quotes (or (eq (char-after) ?\') |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
492 (eq (char-before) ?\'))) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
493 (forward-sexp -1) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
494 ;; vladimir@cs.ualberta.ca 30-Jul-1997: skip ` in |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
495 ;; `variable' so that the value is returned, not the |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
496 ;; name. |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
497 (if (and ignore-quotes |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
498 (eq (char-after) ?\`)) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
499 (forward-char)) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
500 (save-restriction |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
501 (narrow-to-region (point-min) opoint) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
502 (let ((expr (read (current-buffer)))) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
503 (if (eq (car-safe expr) 'interactive) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
504 ;; If it's an (interactive ...) form, it's |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
505 ;; more useful to show how an interactive call |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
506 ;; would use it. |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
507 `(call-interactively |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
508 (lambda (&rest args) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
509 ,expr args)) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
510 expr))))))))) |
428 | 511 |
512 (defun eval-defun (eval-defun-arg-internal) | |
513 "Evaluate defun that point is in or before. | |
514 Print value in minibuffer. | |
515 With argument, insert value in current buffer after the defun." | |
516 (interactive "P") | |
517 (let ((standard-output (if eval-defun-arg-internal (current-buffer) t))) | |
4677
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
518 (prin1-list-as-multiple-values |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
519 (eval-interactive |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
520 (save-excursion |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
521 (end-of-defun) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
522 (beginning-of-defun) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
523 (read (current-buffer))))))) |
428 | 524 |
525 (defun lisp-comment-indent () | |
526 (if (looking-at "\\s<\\s<\\s<") | |
527 (current-column) | |
528 (if (looking-at "\\s<\\s<") | |
529 ;; #### FSF has: | |
530 ;; (let ((tem (or (calculate-lisp-indent) (current-column)))) ... | |
531 (let ((tem (calculate-lisp-indent))) | |
532 (if (listp tem) (car tem) tem)) | |
533 (skip-chars-backward " \t") | |
534 (max (if (bolp) 0 (1+ (current-column))) | |
535 comment-column)))) | |
536 | |
537 ;; XEmacs change | |
538 (defun lisp-indent-for-comment () | |
539 "Indent this line's comment appropriately, or insert an empty comment. | |
540 If adding a new comment on a blank line, use `block-comment-start' instead | |
541 of `comment-start' to open the comment." | |
542 ;; by Stig@hackvan.com | |
543 ;; #### - This functionality, the recognition of block-comment-{start,end}, | |
544 ;; will perhaps be standardized across modes and move to indent-for-comment. | |
545 (interactive) | |
546 (if (and block-comment-start | |
547 (save-excursion (beginning-of-line) (looking-at "^[ \t]*$"))) | |
548 (insert block-comment-start)) | |
549 (indent-for-comment)) | |
550 | |
551 (defvar lisp-indent-offset nil) | |
552 (defvar lisp-indent-function 'lisp-indent-function) | |
553 | |
554 (defun lisp-indent-line (&optional whole-exp) | |
555 "Indent current line as Lisp code. | |
556 With argument, indent any additional lines of the same expression | |
557 rigidly along with this one." | |
558 (interactive "P") | |
559 (let ((indent (calculate-lisp-indent)) shift-amt beg end | |
560 (pos (- (point-max) (point)))) | |
561 (beginning-of-line) | |
562 (setq beg (point)) | |
563 (skip-chars-forward " \t") | |
564 (if (looking-at "\\s<\\s<\\s<") | |
565 ;; Don't alter indentation of a ;;; comment line. | |
566 (goto-char (- (point-max) pos)) | |
567 (if (and (looking-at "\\s<") (not (looking-at "\\s<\\s<"))) | |
568 ;; Single-semicolon comment lines should be indented | |
569 ;; as comment lines, not as code. | |
446 | 570 (progn (indent-for-comment) (backward-char 1)) |
428 | 571 (if (listp indent) (setq indent (car indent))) |
572 (setq shift-amt (- indent (current-column))) | |
573 (if (zerop shift-amt) | |
574 nil | |
575 (delete-region beg (point)) | |
576 (indent-to indent))) | |
577 ;; If initial point was within line's indentation, | |
578 ;; position after the indentation. Else stay at same point in text. | |
579 (if (> (- (point-max) pos) (point)) | |
580 (goto-char (- (point-max) pos))) | |
581 ;; If desired, shift remaining lines of expression the same amount. | |
582 (and whole-exp (not (zerop shift-amt)) | |
583 (save-excursion | |
584 (goto-char beg) | |
585 (forward-sexp 1) | |
586 (setq end (point)) | |
587 (goto-char beg) | |
588 (forward-line 1) | |
589 (setq beg (point)) | |
590 (> end beg)) | |
591 (indent-code-rigidly beg end shift-amt))))) | |
592 | |
593 (defvar calculate-lisp-indent-last-sexp) | |
594 | |
595 (defun calculate-lisp-indent (&optional parse-start) | |
596 "Return appropriate indentation for current line as Lisp code. | |
597 In usual case returns an integer: the column to indent to. | |
598 Can instead return a list, whose car is the column to indent to. | |
599 This means that following lines at the same level of indentation | |
600 should not necessarily be indented the same way. | |
601 The second element of the list is the buffer position | |
602 of the start of the containing expression." | |
603 (save-excursion | |
604 (beginning-of-line) | |
605 (let ((indent-point (point)) | |
606 ;; XEmacs change (remove paren-depth) | |
607 state ;;paren-depth | |
608 ;; setting this to a number inhibits calling hook | |
609 (desired-indent nil) | |
610 (retry t) | |
611 calculate-lisp-indent-last-sexp containing-sexp) | |
612 (if parse-start | |
613 (goto-char parse-start) | |
614 (beginning-of-defun)) | |
615 ;; Find outermost containing sexp | |
616 (while (< (point) indent-point) | |
617 (setq state (parse-partial-sexp (point) indent-point 0))) | |
618 ;; Find innermost containing sexp | |
619 (while (and retry | |
620 state | |
621 ;; XEmacs change (remove paren-depth) | |
622 (> ;;(setq paren-depth (elt state 0)) | |
623 (elt state 0) | |
624 0)) | |
625 (setq retry nil) | |
626 (setq calculate-lisp-indent-last-sexp (elt state 2)) | |
627 (setq containing-sexp (elt state 1)) | |
628 ;; Position following last unclosed open. | |
629 (goto-char (1+ containing-sexp)) | |
630 ;; Is there a complete sexp since then? | |
631 (if (and calculate-lisp-indent-last-sexp | |
632 (> calculate-lisp-indent-last-sexp (point))) | |
633 ;; Yes, but is there a containing sexp after that? | |
634 (let ((peek (parse-partial-sexp calculate-lisp-indent-last-sexp | |
635 indent-point 0))) | |
636 (if (setq retry (car (cdr peek))) (setq state peek))))) | |
637 (if retry | |
638 nil | |
639 ;; Innermost containing sexp found | |
640 (goto-char (1+ containing-sexp)) | |
641 (if (not calculate-lisp-indent-last-sexp) | |
642 ;; indent-point immediately follows open paren. | |
643 ;; Don't call hook. | |
644 (setq desired-indent (current-column)) | |
645 ;; Find the start of first element of containing sexp. | |
646 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t) | |
647 (cond ((looking-at "\\s(") | |
648 ;; First element of containing sexp is a list. | |
649 ;; Indent under that list. | |
650 ) | |
651 ((> (save-excursion (forward-line 1) (point)) | |
652 calculate-lisp-indent-last-sexp) | |
653 ;; This is the first line to start within the containing sexp. | |
654 ;; It's almost certainly a function call. | |
655 (if (= (point) calculate-lisp-indent-last-sexp) | |
656 ;; Containing sexp has nothing before this line | |
657 ;; except the first element. Indent under that element. | |
658 nil | |
659 ;; Skip the first element, find start of second (the first | |
660 ;; argument of the function call) and indent under. | |
661 (progn (forward-sexp 1) | |
662 (parse-partial-sexp (point) | |
663 calculate-lisp-indent-last-sexp | |
664 0 t))) | |
665 (backward-prefix-chars)) | |
666 (t | |
667 ;; Indent beneath first sexp on same line as | |
668 ;; calculate-lisp-indent-last-sexp. Again, it's | |
669 ;; almost certainly a function call. | |
670 (goto-char calculate-lisp-indent-last-sexp) | |
671 (beginning-of-line) | |
672 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp | |
673 0 t) | |
674 (backward-prefix-chars))))) | |
675 ;; Point is at the point to indent under unless we are inside a string. | |
676 ;; Call indentation hook except when overridden by lisp-indent-offset | |
677 ;; or if the desired indentation has already been computed. | |
678 (let ((normal-indent (current-column))) | |
679 (cond ((elt state 3) | |
680 ;; Inside a string, don't change indentation. | |
681 (goto-char indent-point) | |
682 (skip-chars-forward " \t") | |
683 (current-column)) | |
684 (desired-indent) | |
685 ((and (boundp 'lisp-indent-function) | |
686 lisp-indent-function | |
687 (not retry)) | |
688 (or (funcall lisp-indent-function indent-point state) | |
689 normal-indent)) | |
690 ;; XEmacs change: | |
691 ;; lisp-indent-offset shouldn't override lisp-indent-function ! | |
692 ((and (integerp lisp-indent-offset) containing-sexp) | |
693 ;; Indent by constant offset | |
694 (goto-char containing-sexp) | |
695 (+ normal-indent lisp-indent-offset)) | |
696 (t | |
697 normal-indent)))))) | |
698 | |
872 | 699 (defvar lisp-function-and-type-regexp |
700 (concat "def\\(" | |
701 ;; def but not define-. | |
702 "\\(un\\|advice\\|alias\\|macro\\*?\\|setf\\|subst\\*?\\|" | |
703 "-edebug-spec\\|" | |
704 ;; CLOS | |
705 "method\\|generic\\|" | |
706 ;; define-* | |
707 "ine-\\(?:" | |
708 ;; basic Lisp stuff | |
709 "compiler-macro\\|function\\|function-when-void\\|modify-macro\\|" | |
710 "setf-method\\|" | |
711 ;; obsolete/compatible support, XEmacs-specific | |
712 "compatible-function-alias\\|obsolete-function-alias\\|" | |
713 ;; XEmacs-specific, supporting stuff inside of XEmacs | |
714 "ccl-program\\|device-method\\*?\\|prefix-command\\|skeleton" | |
715 "\\)\\)\\|" | |
716 ;; Structure declarations. | |
717 "\\(class\\|struct\\|type\\)\\)\\>") | |
718 "Regular expression to match the function and type keywords used in Lisp. | |
719 This matches, for example, the string \"defun\", as well as defsetf, | |
720 defsubst*, define-prefix-command, etc. Match string 1 matches everything | |
721 but the three-letter \"def\" string at the beginning. Match string 2 | |
722 matches everything after that, when it's *NOT* a \"type\" declaration -- | |
723 which includes defclass, defstruct, and deftype. Match string 3 is similar | |
724 to match string 2 in that it matches everything after the \"def\", when | |
725 \(and only when) the keyword matched *IS* a type declaration. You can use | |
726 match strings 2 and 3 to easily determine whether a function or type was | |
727 matched. The regex is terminated with a \\\> so that there must be a | |
728 word-end; i.e. defunbbb won't match.") | |
729 | |
730 (defvar lisp-flet-regexp | |
731 "(\\(flet\\|macrolet\\|labels\\)\\(\\s-\\|\n\\)") | |
732 | |
428 | 733 (defun lisp-indent-function (indent-point state) |
734 ;; free reference to `calculate-lisp-indent-last-sexp' | |
735 ;; in #'calculate-lisp-indent | |
736 (let ((normal-indent (current-column))) | |
737 (goto-char (1+ (elt state 1))) | |
738 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t) | |
739 (if (and (elt state 2) | |
740 (not (looking-at "\\sw\\|\\s_"))) | |
629 | 741 ;; car of form doesn't seem to be a symbol |
428 | 742 (progn |
743 (if (not (> (save-excursion (forward-line 1) (point)) | |
744 calculate-lisp-indent-last-sexp)) | |
745 (progn (goto-char calculate-lisp-indent-last-sexp) | |
746 (beginning-of-line) | |
747 (parse-partial-sexp (point) | |
748 calculate-lisp-indent-last-sexp 0 t))) | |
749 ;; Indent under the list or under the first sexp on the same | |
750 ;; line as calculate-lisp-indent-last-sexp. Note that first | |
751 ;; thing on that line has to be complete sexp since we are | |
752 ;; inside the innermost containing sexp. | |
753 (backward-prefix-chars) | |
754 (current-column)) | |
872 | 755 |
756 ;; Now come a bunch of ad-hoc checks to see if we're in particular | |
757 ;; places (defining an flet function, in the argument list of a | |
758 ;; regular or flet function, in a quoted list, etc.) where the | |
759 ;; regular indenting doesn't work well. | |
760 | |
761 ;; #### All this stuff here should be generalized so that | |
762 ;; you can specify, for various functions, how you want | |
763 ;; particular arguments handled -- in some way that works | |
764 ;; recursively, so it can handle flet and such. | |
765 | |
766 (let* ((function (buffer-substring (point) | |
428 | 767 (progn (forward-sexp 1) (point)))) |
872 | 768 (quote (condition-case nil |
769 (save-excursion | |
770 (backward-up-list 1) | |
771 (memq (char-before (point)) '(?' ?`))))) | |
772 method) | |
773 (cond | |
774 ;; if we're indenting a quoted list, and the first word is not | |
775 ;; lambda, line up second line below first with no indentation, | |
776 ;; so that property lists indent correctly. | |
777 ((and quote (not (equal function "lambda"))) | |
778 (setq method 'lisp-indent-quoteform)) | |
779 ;; do the same if we're indenting the arg list of a def* form. | |
780 ((let ((p (point))) | |
781 (condition-case nil | |
442 | 782 (save-excursion |
872 | 783 (backward-up-list 2) |
784 ;; make sure we're indeed the second argument of the | |
785 ;; def* form. | |
786 (and (eq (point) (save-excursion | |
787 (goto-char p) | |
788 (backward-up-list 1) | |
789 (backward-sexp 2) | |
790 (1- (point)))) | |
791 ;; check to see that the function is a def* type | |
792 (eq (char-after) ?\() | |
793 (progn (forward-char 1) t) | |
794 (looking-at lisp-function-and-type-regexp) | |
795 ;; defstruct may have slot option specs, which | |
796 ;; should probably be reverse-indented like | |
797 ;; normal, because the slot name is the first | |
798 ;; in the list. #### Review this. | |
799 (not (equal (match-string 0) "defstruct")))) | |
800 (error nil))) | |
801 (setq method 'lisp-indent-quoteform)) | |
802 | |
803 ;; handle functions in flet forms | |
804 ((let ((p (point))) | |
805 (condition-case nil | |
806 (save-excursion | |
807 (backward-up-list 3) | |
808 ;; make sure we're indeed a function, i.e. inside the | |
809 ;; first form after the flet. | |
810 (and (eq (point) (save-excursion | |
811 (goto-char p) | |
812 (backward-up-list 2) | |
813 (backward-sexp 1) | |
814 (1- (point)))) | |
815 (looking-at lisp-flet-regexp))) | |
816 (error nil))) | |
817 (setq method 'defun)) | |
818 | |
819 ;; handle the arg lists in functions in flet forms | |
820 ((let ((p (point))) | |
821 (condition-case nil | |
822 (save-excursion | |
823 (backward-up-list 2) | |
824 ;; make sure we're indeed the arg list -- i.e. the first | |
825 ;; element after the function name. | |
826 (and (eq (point) (save-excursion | |
827 (goto-char p) | |
828 (backward-up-list 1) | |
829 (backward-sexp 1) | |
830 (1- (point)))) | |
831 (progn | |
832 (backward-up-list 2) | |
833 (looking-at lisp-flet-regexp)))) | |
834 (error nil))) | |
835 (setq method 'lisp-indent-quoteform)) | |
836 (t | |
837 (setq method | |
838 (or (get (intern-soft function) 'lisp-indent-function) | |
839 (get (intern-soft function) 'lisp-indent-hook))))) | |
428 | 840 (cond ((or (eq method 'defun) |
841 (and (null method) | |
842 (> (length function) 3) | |
843 (string-match "\\`def" function))) | |
844 (lisp-indent-defform state indent-point)) | |
845 ((integerp method) | |
846 (lisp-indent-specform method state | |
847 indent-point normal-indent)) | |
848 (method | |
4942
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
849 (funcall method state indent-point)) |
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
850 ((and (> (length function) 5) |
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
851 (string-match "\\`with-" function)) |
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
852 (lisp-indent-specform 0 state indent-point normal-indent |
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
853 t))))))) |
428 | 854 |
872 | 855 (defun lisp-indent-quoteform (state indent-point) |
856 (goto-char (car (cdr state))) | |
857 (forward-line 1) | |
858 (if (> (point) (car (cdr (cdr state)))) | |
859 (progn | |
860 (goto-char (car (cdr state))) | |
861 (+ 1 (current-column))))) | |
862 | |
428 | 863 (defvar lisp-body-indent 2 |
864 "Number of columns to indent the second line of a `(def...)' form.") | |
865 | |
4958
e4bbe5622a80
finish doc comment on `lisp-indent-specform'
Ben Wing <ben@xemacs.org>
parents:
4942
diff
changeset
|
866 ;; Calculate the appropriate indentation for a special form (e.g. defun) or |
e4bbe5622a80
finish doc comment on `lisp-indent-specform'
Ben Wing <ben@xemacs.org>
parents:
4942
diff
changeset
|
867 ;; a macro that behaves like a special form (e.g. with-temp-buffer). |
e4bbe5622a80
finish doc comment on `lisp-indent-specform'
Ben Wing <ben@xemacs.org>
parents:
4942
diff
changeset
|
868 ;; Return value is as for `calculate-lisp-indent' (q.v.). |
e4bbe5622a80
finish doc comment on `lisp-indent-specform'
Ben Wing <ben@xemacs.org>
parents:
4942
diff
changeset
|
869 ;; |
4942
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
870 ;; COUNT is the number of "distinguished" forms (e.g. arguments before the |
4958
e4bbe5622a80
finish doc comment on `lisp-indent-specform'
Ben Wing <ben@xemacs.org>
parents:
4942
diff
changeset
|
871 ;; "body", in a macro taking a body as its last argument). |
e4bbe5622a80
finish doc comment on `lisp-indent-specform'
Ben Wing <ben@xemacs.org>
parents:
4942
diff
changeset
|
872 ;; |
e4bbe5622a80
finish doc comment on `lisp-indent-specform'
Ben Wing <ben@xemacs.org>
parents:
4942
diff
changeset
|
873 ;; INDENT-POINT is usually the value of (point) at the beginning of the |
e4bbe5622a80
finish doc comment on `lisp-indent-specform'
Ben Wing <ben@xemacs.org>
parents:
4942
diff
changeset
|
874 ;; line containing the form to be indented. |
e4bbe5622a80
finish doc comment on `lisp-indent-specform'
Ben Wing <ben@xemacs.org>
parents:
4942
diff
changeset
|
875 ;; |
e4bbe5622a80
finish doc comment on `lisp-indent-specform'
Ben Wing <ben@xemacs.org>
parents:
4942
diff
changeset
|
876 ;; STATE is the result of calling (parse-partial-sexp (point) INDENT-POINT) |
e4bbe5622a80
finish doc comment on `lisp-indent-specform'
Ben Wing <ben@xemacs.org>
parents:
4942
diff
changeset
|
877 ;; when (point) is sitting on (i.e. just before) the outermost containing |
e4bbe5622a80
finish doc comment on `lisp-indent-specform'
Ben Wing <ben@xemacs.org>
parents:
4942
diff
changeset
|
878 ;; left paren for the form to be indented. |
e4bbe5622a80
finish doc comment on `lisp-indent-specform'
Ben Wing <ben@xemacs.org>
parents:
4942
diff
changeset
|
879 ;; |
e4bbe5622a80
finish doc comment on `lisp-indent-specform'
Ben Wing <ben@xemacs.org>
parents:
4942
diff
changeset
|
880 ;; NORMAL-INDENT is the amount of "full" indentation used for arguments |
e4bbe5622a80
finish doc comment on `lisp-indent-specform'
Ben Wing <ben@xemacs.org>
parents:
4942
diff
changeset
|
881 ;; that aren't given some special indentation (usually less), and normally |
e4bbe5622a80
finish doc comment on `lisp-indent-specform'
Ben Wing <ben@xemacs.org>
parents:
4942
diff
changeset
|
882 ;; it lines up with the first word after the function name. forms are |
e4bbe5622a80
finish doc comment on `lisp-indent-specform'
Ben Wing <ben@xemacs.org>
parents:
4942
diff
changeset
|
883 ;; indented as follows: |
4942
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
884 ;; |
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
885 ;; -- The first and second distinguished forms are given 2 times body indent |
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
886 ;; (determined by `lisp-body-indent') |
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
887 ;; -- Other distinguished forms are given normal indent |
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
888 ;; -- The first nondistinguished form (the body, usually) is given body indent |
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
889 ;; -- Normally, any further nondistinguished forms are given normal indent |
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
890 ;; -- However, if INDENT-REMAINING-AS-BODY was specified, any further |
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
891 ;; nondistinguished forms are given body indent. This is useful when the |
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
892 ;; number of distinguished forms isn't known or may vary. |
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
893 |
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
894 (defun lisp-indent-specform (count state indent-point normal-indent |
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
895 &optional indent-remaining-as-body) |
428 | 896 (let ((containing-form-start (elt state 1)) |
897 (i count) | |
898 body-indent containing-form-column) | |
899 ;; Move to the start of containing form, calculate indentation | |
900 ;; to use for non-distinguished forms (> count), and move past the | |
901 ;; function symbol. lisp-indent-function guarantees that there is at | |
902 ;; least one word or symbol character following open paren of containing | |
903 ;; form. | |
904 (goto-char containing-form-start) | |
905 (setq containing-form-column (current-column)) | |
906 (setq body-indent (+ lisp-body-indent containing-form-column)) | |
907 (forward-char 1) | |
908 (forward-sexp 1) | |
909 ;; Now find the start of the last form. | |
910 (parse-partial-sexp (point) indent-point 1 t) | |
911 (while (and (< (point) indent-point) | |
912 (condition-case () | |
913 (progn | |
914 (setq count (1- count)) | |
915 (forward-sexp 1) | |
916 (parse-partial-sexp (point) indent-point 1 t)) | |
917 (error nil)))) | |
918 ;; Point is sitting on first character of last (or count) sexp. | |
919 (if (> count 0) | |
920 ;; A distinguished form. If it is the first or second form use double | |
921 ;; lisp-body-indent, else normal indent. With lisp-body-indent bound | |
922 ;; to 2 (the default), this just happens to work the same with if as | |
923 ;; the older code, but it makes unwind-protect, condition-case, | |
924 ;; with-output-to-temp-buffer, et. al. much more tasteful. The older, | |
925 ;; less hacked, behavior can be obtained by replacing below with | |
926 ;; (list normal-indent containing-form-start). | |
927 (if (<= (- i count) 1) | |
928 (list (+ containing-form-column (* 2 lisp-body-indent)) | |
929 containing-form-start) | |
930 (list normal-indent containing-form-start)) | |
931 ;; A non-distinguished form. Use body-indent if there are no | |
932 ;; distinguished forms and this is the first undistinguished form, | |
933 ;; or if this is the first undistinguished form and the preceding | |
934 ;; distinguished form has indentation at least as great as body-indent. | |
935 (if (or (and (= i 0) (= count 0)) | |
4942
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
936 (and (or (= count 0) indent-remaining-as-body) |
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
937 (<= body-indent normal-indent))) |
428 | 938 body-indent |
939 normal-indent)))) | |
940 | |
941 (defun lisp-indent-defform (state indent-point) | |
942 (goto-char (car (cdr state))) | |
943 (forward-line 1) | |
944 (if (> (point) (car (cdr (cdr state)))) | |
945 (progn | |
946 (goto-char (car (cdr state))) | |
947 (+ lisp-body-indent (current-column))))) | |
948 | |
949 | |
950 ;; (put 'progn 'lisp-indent-function 0), say, causes progn to be indented | |
951 ;; like defun if the first form is placed on the next line, otherwise | |
952 ;; it is indented like any other form (i.e. forms line up under first). | |
953 | |
776 | 954 ;; NOTE: All def* functions are handled specially, no need to put them here. |
955 ;; NOTE: All cl-* constructs are handled in cl.el. | |
956 | |
428 | 957 (put 'lambda 'lisp-indent-function 'defun) |
958 (put 'autoload 'lisp-indent-function 'defun) | |
959 (put 'progn 'lisp-indent-function 0) | |
960 (put 'prog1 'lisp-indent-function 1) | |
961 (put 'prog2 'lisp-indent-function 2) | |
962 (put 'save-excursion 'lisp-indent-function 0) | |
963 (put 'save-window-excursion 'lisp-indent-function 0) | |
964 (put 'save-selected-window 'lisp-indent-function 0) | |
442 | 965 (put 'with-selected-window 'lisp-indent-function 1) |
428 | 966 (put 'save-selected-frame 'lisp-indent-function 0) |
967 (put 'with-selected-frame 'lisp-indent-function 1) | |
872 | 968 (put 'save-selected-device 'lisp-indent-function 0) |
969 (put 'with-selected-device 'lisp-indent-function 1) | |
428 | 970 (put 'save-restriction 'lisp-indent-function 0) |
971 (put 'save-match-data 'lisp-indent-function 0) | |
972 (put 'let 'lisp-indent-function 1) | |
973 (put 'let* 'lisp-indent-function 1) | |
974 (put 'let-specifier 'lisp-indent-function 1) | |
975 (put 'while 'lisp-indent-function 1) | |
976 (put 'if 'lisp-indent-function 2) | |
977 (put 'catch 'lisp-indent-function 1) | |
978 (put 'condition-case 'lisp-indent-function 2) | |
460 | 979 (put 'handler-case 'lisp-indent-function 1) |
980 (put 'handler-bind 'lisp-indent-function 1) | |
428 | 981 (put 'call-with-condition-handler 'lisp-indent-function 2) |
982 (put 'unwind-protect 'lisp-indent-function 1) | |
983 (put 'save-current-buffer 'lisp-indent-function 0) | |
984 (put 'with-current-buffer 'lisp-indent-function 1) | |
985 (put 'with-string-as-buffer-contents 'lisp-indent-function 1) | |
986 (put 'with-temp-file 'lisp-indent-function 1) | |
987 (put 'with-temp-buffer 'lisp-indent-function 0) | |
988 (put 'with-output-to-string 'lisp-indent-function 0) | |
989 (put 'with-output-to-temp-buffer 'lisp-indent-function 1) | |
460 | 990 (put 'with-slots 'lisp-indent-function 2) |
991 (put 'with-open-file 'lisp-indent-function 1) | |
992 (put 'with-open-stream 'lisp-indent-function 1) | |
428 | 993 (put 'eval-after-load 'lisp-indent-function 1) |
994 (put 'display-message 'lisp-indent-function 1) | |
995 (put 'display-warning 'lisp-indent-function 1) | |
996 (put 'lmessage 'lisp-indent-function 2) | |
997 (put 'lwarn 'lisp-indent-function 2) | |
998 (put 'global-set-key 'lisp-indent-function 1) | |
460 | 999 (put 'print-unreadable-object 'lisp-indent-function 1) |
428 | 1000 |
1001 (defun indent-sexp (&optional endpos) | |
1002 "Indent each line of the list starting just after point. | |
1003 If optional arg ENDPOS is given, indent each line, stopping when | |
1004 ENDPOS is encountered." | |
1005 (interactive) | |
1006 (let ((indent-stack (list nil)) | |
1007 (next-depth 0) | |
1008 ;; If ENDPOS is non-nil, use nil as STARTING-POINT | |
1009 ;; so that calculate-lisp-indent will find the beginning of | |
1010 ;; the defun we are in. | |
1011 ;; If ENDPOS is nil, it is safe not to scan before point | |
1012 ;; since every line we indent is more deeply nested than point is. | |
1013 (starting-point (if endpos nil (point))) | |
1014 (last-point (point)) | |
1015 last-depth bol outer-loop-done inner-loop-done state this-indent) | |
1016 (or endpos | |
1017 ;; Get error now if we don't have a complete sexp after point. | |
1018 (save-excursion (forward-sexp 1))) | |
1019 (save-excursion | |
1020 (setq outer-loop-done nil) | |
1021 (while (if endpos (< (point) endpos) | |
1022 (not outer-loop-done)) | |
1023 (setq last-depth next-depth | |
1024 inner-loop-done nil) | |
1025 ;; Parse this line so we can learn the state | |
1026 ;; to indent the next line. | |
1027 ;; This inner loop goes through only once | |
1028 ;; unless a line ends inside a string. | |
1029 (while (and (not inner-loop-done) | |
1030 (not (setq outer-loop-done (eobp)))) | |
1031 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point)) | |
1032 nil nil state)) | |
1033 (setq next-depth (car state)) | |
1034 ;; If the line contains a comment other than the sort | |
1035 ;; that is indented like code, | |
1036 ;; indent it now with indent-for-comment. | |
1037 ;; Comments indented like code are right already. | |
1038 ;; In any case clear the in-comment flag in the state | |
1039 ;; because parse-partial-sexp never sees the newlines. | |
1040 (if (car (nthcdr 4 state)) | |
1041 (progn (indent-for-comment) | |
1042 (end-of-line) | |
1043 (setcar (nthcdr 4 state) nil))) | |
1044 ;; If this line ends inside a string, | |
1045 ;; go straight to next line, remaining within the inner loop, | |
1046 ;; and turn off the \-flag. | |
1047 (if (car (nthcdr 3 state)) | |
1048 (progn | |
1049 (forward-line 1) | |
1050 (setcar (nthcdr 5 state) nil)) | |
1051 (setq inner-loop-done t))) | |
1052 (and endpos | |
1053 (<= next-depth 0) | |
1054 (progn | |
1055 (setq indent-stack (append indent-stack | |
1056 (make-list (- next-depth) nil)) | |
1057 last-depth (- last-depth next-depth) | |
1058 next-depth 0))) | |
1059 (or outer-loop-done endpos | |
1060 (setq outer-loop-done (<= next-depth 0))) | |
1061 (if outer-loop-done | |
1062 (forward-line 1) | |
1063 (while (> last-depth next-depth) | |
1064 (setq indent-stack (cdr indent-stack) | |
1065 last-depth (1- last-depth))) | |
1066 (while (< last-depth next-depth) | |
1067 (setq indent-stack (cons nil indent-stack) | |
1068 last-depth (1+ last-depth))) | |
1069 ;; Now go to the next line and indent it according | |
1070 ;; to what we learned from parsing the previous one. | |
1071 (forward-line 1) | |
1072 (setq bol (point)) | |
1073 (skip-chars-forward " \t") | |
1074 ;; But not if the line is blank, or just a comment | |
1075 ;; (except for double-semi comments; indent them as usual). | |
1076 (if (or (eobp) (looking-at "\\s<\\|\n")) | |
1077 nil | |
1078 (if (and (car indent-stack) | |
1079 (>= (car indent-stack) 0)) | |
1080 (setq this-indent (car indent-stack)) | |
1081 (let ((val (calculate-lisp-indent | |
1082 (if (car indent-stack) (- (car indent-stack)) | |
1083 starting-point)))) | |
1084 (if (integerp val) | |
1085 (setcar indent-stack | |
1086 (setq this-indent val)) | |
1087 (setcar indent-stack (- (car (cdr val)))) | |
1088 (setq this-indent (car val))))) | |
1089 (if (/= (current-column) this-indent) | |
1090 (progn (delete-region bol (point)) | |
1091 (indent-to this-indent))))) | |
1092 (or outer-loop-done | |
1093 (setq outer-loop-done (= (point) last-point)) | |
1094 (setq last-point (point))))))) | |
1095 | |
1096 ;; Indent every line whose first char is between START and END inclusive. | |
1097 (defun lisp-indent-region (start end) | |
1098 (save-excursion | |
1099 (let ((endmark (copy-marker end))) | |
1100 (goto-char start) | |
1101 (and (bolp) (not (eolp)) | |
1102 (lisp-indent-line)) | |
1103 (indent-sexp endmark) | |
1104 (set-marker endmark nil)))) | |
1105 | |
1106 ;;;; Lisp paragraph filling commands. | |
1107 | |
1108 (defun lisp-fill-paragraph (&optional justify) | |
1109 "Like \\[fill-paragraph], but handle Emacs Lisp comments. | |
1110 If any of the current line is a comment, fill the comment or the | |
1111 paragraph of it that point is in, preserving the comment's indentation | |
1112 and initial semicolons." | |
1113 (interactive "P") | |
1114 (let ( | |
1115 ;; Non-nil if the current line contains a comment. | |
1116 has-comment | |
1117 | |
1118 ;; Non-nil if the current line contains code and a comment. | |
1119 has-code-and-comment | |
1120 | |
1121 ;; If has-comment, the appropriate fill-prefix for the comment. | |
1122 comment-fill-prefix | |
1123 ) | |
1124 | |
1125 ;; Figure out what kind of comment we are looking at. | |
1126 (save-excursion | |
1127 (beginning-of-line) | |
1128 (cond | |
1129 | |
1130 ;; A line with nothing but a comment on it? | |
1131 ((looking-at "[ \t]*;[; \t]*") | |
1132 (setq has-comment t | |
1133 comment-fill-prefix (buffer-substring (match-beginning 0) | |
1134 (match-end 0)))) | |
1135 | |
1136 ;; A line with some code, followed by a comment? Remember that the | |
1137 ;; semi which starts the comment shouldn't be part of a string or | |
1138 ;; character. | |
1139 ;; XEmacs Try this the FSF and see if it works. | |
1140 ; ((progn | |
1141 ; (while (not (looking-at ";\\|$")) | |
1142 ; (skip-chars-forward "^;\n\"\\\\?") | |
1143 ; (cond | |
1144 ; ((eq (char-after (point)) ?\\) (forward-char 2)) | |
1145 ; ((memq (char-after (point)) '(?\" ??)) (forward-sexp 1)))) | |
1146 ; (looking-at ";+[\t ]*")) | |
1147 ; (setq has-comment t) | |
1148 ((condition-case nil | |
1149 (save-restriction | |
1150 (narrow-to-region (point-min) | |
1151 (save-excursion (end-of-line) (point))) | |
1152 (while (not (looking-at ";\\|$")) | |
1153 (skip-chars-forward "^;\n\"\\\\?") | |
1154 (cond | |
1155 ((eq (char-after (point)) ?\\) (forward-char 2)) | |
1156 ((memq (char-after (point)) '(?\" ??)) (forward-sexp 1)))) | |
844 | 1157 ;; don't do comment filling in a string, or we will mess up |
1158 ;; doc strings and other places where semicolons are used. | |
1159 (and (not (eq 'string (buffer-syntactic-context))) | |
1160 (looking-at ";+[\t ]*"))) | |
428 | 1161 (error nil)) |
1162 (setq has-comment t has-code-and-comment t) | |
1163 (setq comment-fill-prefix | |
1164 (concat (make-string (/ (current-column) 8) ?\t) | |
1165 (make-string (% (current-column) 8) ?\ ) | |
1166 (buffer-substring (match-beginning 0) (match-end 0))))))) | |
1167 | |
1168 (if (not has-comment) | |
1169 (fill-paragraph justify) | |
1170 | |
1171 ;; Narrow to include only the comment, and then fill the region. | |
1172 (save-excursion | |
1173 (save-restriction | |
1174 (beginning-of-line) | |
1175 (narrow-to-region | |
1176 ;; Find the first line we should include in the region to fill. | |
1177 (save-excursion | |
1178 (while (and (zerop (forward-line -1)) | |
1179 (looking-at "^[ \t]*;"))) | |
1180 ;; We may have gone too far. Go forward again. | |
1181 (or (looking-at ".*;") | |
1182 (forward-line 1)) | |
1183 (point)) | |
1184 ;; Find the beginning of the first line past the region to fill. | |
1185 (save-excursion | |
1186 (while (progn (forward-line 1) | |
1187 (looking-at "^[ \t]*;"))) | |
1188 (point))) | |
1189 | |
1190 ;; Lines with only semicolons on them can be paragraph boundaries. | |
1191 (let* ((paragraph-start (concat paragraph-start "\\|[ \t;]*$")) | |
1192 (paragraph-separate (concat paragraph-start "\\|[ \t;]*$")) | |
1193 (paragraph-ignore-fill-prefix nil) | |
1194 (fill-prefix comment-fill-prefix) | |
1195 (after-line (if has-code-and-comment | |
1196 (save-excursion | |
1197 (forward-line 1) (point)))) | |
1198 (end (progn | |
1199 (forward-paragraph) | |
1200 (or (bolp) (newline 1)) | |
1201 (point))) | |
1202 ;; If this comment starts on a line with code, | |
1203 ;; include that like in the filling. | |
1204 (beg (progn (backward-paragraph) | |
1205 (if (eq (point) after-line) | |
1206 (forward-line -1)) | |
1207 (point)))) | |
1208 (fill-region-as-paragraph beg end | |
1209 justify nil | |
1210 (save-excursion | |
1211 (goto-char beg) | |
1212 (if (looking-at fill-prefix) | |
1213 nil | |
1214 (re-search-forward comment-start-skip) | |
1215 (point)))))))) | |
1216 t)) | |
1217 | |
1218 (defun indent-code-rigidly (start end arg &optional nochange-regexp) | |
1219 "Indent all lines of code, starting in the region, sideways by ARG columns. | |
1220 Does not affect lines starting inside comments or strings, assuming that | |
1221 the start of the region is not inside them. | |
1222 | |
1223 Called from a program, takes args START, END, COLUMNS and NOCHANGE-REGEXP. | |
1224 The last is a regexp which, if matched at the beginning of a line, | |
1225 means don't indent that line." | |
1226 (interactive "r\np") | |
1227 (let (state) | |
1228 (save-excursion | |
1229 (goto-char end) | |
1230 (setq end (point-marker)) | |
1231 (goto-char start) | |
1232 (or (bolp) | |
1233 (setq state (parse-partial-sexp (point) | |
1234 (progn | |
1235 (forward-line 1) (point)) | |
1236 nil nil state))) | |
1237 (while (< (point) end) | |
1238 (or (car (nthcdr 3 state)) | |
1239 (and nochange-regexp | |
1240 (looking-at nochange-regexp)) | |
1241 ;; If line does not start in string, indent it | |
1242 (let ((indent (current-indentation))) | |
1243 (delete-region (point) (progn (skip-chars-forward " \t") (point))) | |
1244 (or (eolp) | |
1245 (indent-to (max 0 (+ indent arg)) 0)))) | |
1246 (setq state (parse-partial-sexp (point) | |
1247 (progn | |
1248 (forward-line 1) (point)) | |
1249 nil nil state)))))) | |
1250 | |
1251 (provide 'lisp-mode) | |
1252 | |
1253 ;;; lisp-mode.el ends here |