Mercurial > emacs
annotate lisp/=mim-mode.el @ 669:4c64c671426f
*** empty log message ***
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Mon, 01 Jun 1992 08:28:58 +0000 |
parents | 505130d1ddf8 |
children | 4f28bd14272c |
rev | line source |
---|---|
659
505130d1ddf8
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
584
diff
changeset
|
1 ;;; mim-mode.el --- Mim (MDL in MDL) mode. |
505130d1ddf8
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
584
diff
changeset
|
2 |
229 | 3 ;; Copyright (C) 1985 Free Software Foundation, Inc. |
4 ;; Principal author K. Shane Hartman | |
5 | |
6 ;; This file is part of GNU Emacs. | |
7 | |
8 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
9 ;; it under the terms of the GNU General Public License as published by | |
10 ;; the Free Software Foundation; either version 1, or (at your option) | |
11 ;; any later version. | |
12 | |
13 ;; GNU Emacs is distributed in the hope that it will be useful, | |
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 ;; GNU General Public License for more details. | |
17 | |
18 ;; You should have received a copy of the GNU General Public License | |
19 ;; along with GNU Emacs; see the file COPYING. If not, write to | |
20 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
21 | |
22 | |
23 (autoload 'fast-syntax-check-mim "mim-syntax" | |
24 "Checks Mim syntax quickly. | |
25 Answers correct or incorrect, cannot point out the error context." | |
26 t) | |
27 | |
28 (autoload 'slow-syntax-check-mim "mim-syntax" | |
29 "Check Mim syntax slowly. | |
30 Points out the context of the error, if the syntax is incorrect." | |
31 t) | |
32 | |
33 (defvar mim-mode-hysterical-bindings t | |
34 "*Non-nil means bind list manipulation commands to Meta keys as well as | |
35 Control-Meta keys for historical reasons. Otherwise, only the latter keys | |
36 are bound.") | |
37 | |
38 (defvar mim-mode-map nil) | |
39 | |
40 (defvar mim-mode-syntax-table nil) | |
41 | |
42 (if mim-mode-syntax-table | |
43 () | |
44 (let ((i -1)) | |
45 (setq mim-mode-syntax-table (make-syntax-table)) | |
46 (while (< i ?\ ) | |
47 (modify-syntax-entry (setq i (1+ i)) " " mim-mode-syntax-table)) | |
48 (while (< i 127) | |
49 (modify-syntax-entry (setq i (1+ i)) "_ " mim-mode-syntax-table)) | |
50 (setq i (1- ?a)) | |
51 (while (< i ?z) | |
52 (modify-syntax-entry (setq i (1+ i)) "w " mim-mode-syntax-table)) | |
53 (setq i (1- ?A)) | |
54 (while (< i ?Z) | |
55 (modify-syntax-entry (setq i (1+ i)) "w " mim-mode-syntax-table)) | |
56 (setq i (1- ?0)) | |
57 (while (< i ?9) | |
58 (modify-syntax-entry (setq i (1+ i)) "w " mim-mode-syntax-table)) | |
59 (modify-syntax-entry ?: " " mim-mode-syntax-table) ; make : symbol delimiter | |
60 (modify-syntax-entry ?, "' " mim-mode-syntax-table) | |
61 (modify-syntax-entry ?. "' " mim-mode-syntax-table) | |
62 (modify-syntax-entry ?' "' " mim-mode-syntax-table) | |
63 (modify-syntax-entry ?` "' " mim-mode-syntax-table) | |
64 (modify-syntax-entry ?~ "' " mim-mode-syntax-table) | |
65 (modify-syntax-entry ?\; "' " mim-mode-syntax-table) ; comments are prefixed objects | |
66 (modify-syntax-entry ?# "' " mim-mode-syntax-table) | |
67 (modify-syntax-entry ?% "' " mim-mode-syntax-table) | |
68 (modify-syntax-entry ?! "' " mim-mode-syntax-table) | |
69 (modify-syntax-entry ?\" "\" " mim-mode-syntax-table) | |
70 (modify-syntax-entry ?\\ "\\ " mim-mode-syntax-table) | |
71 (modify-syntax-entry ?\( "\() " mim-mode-syntax-table) | |
72 (modify-syntax-entry ?\< "\(> " mim-mode-syntax-table) | |
73 (modify-syntax-entry ?\{ "\(} " mim-mode-syntax-table) | |
74 (modify-syntax-entry ?\[ "\(] " mim-mode-syntax-table) | |
75 (modify-syntax-entry ?\) "\)( " mim-mode-syntax-table) | |
76 (modify-syntax-entry ?\> "\)< " mim-mode-syntax-table) | |
77 (modify-syntax-entry ?\} "\){ " mim-mode-syntax-table) | |
78 (modify-syntax-entry ?\] "\)[ " mim-mode-syntax-table))) | |
79 | |
80 (defconst mim-whitespace "\000- ") | |
81 | |
82 (defvar mim-mode-hook nil | |
83 "*User function run after mim mode initialization. Usage: | |
84 \(setq mim-mode-hook '(lambda () ... your init forms ...)).") | |
85 | |
86 (define-abbrev-table 'mim-mode-abbrev-table nil) | |
87 | |
88 (defconst indent-mim-function 'indent-mim-function | |
89 "Controls (via properties) indenting of special forms. | |
90 \(put 'FOO 'indent-mim-function n\), integer n, means lines inside | |
91 <FOO ...> will be indented n spaces from start of form. | |
92 \(put 'FOO 'indent-mim-function 'DEFINE\) is like above but means use | |
93 value of mim-body-indent as offset from start of form. | |
94 \(put 'FOO 'indent-mim-function <cons>\) where <cons> is a list or pointted list | |
95 of integers, means indent each form in <FOO ...> by the amount specified | |
96 in <cons>. When <cons> is exhausted, indent remaining forms by | |
97 `mim-body-indent' unless <cons> is a pointed list, in which case the last | |
98 cdr is used. Confused? Here is an example: | |
99 \(put 'FROBIT 'indent-mim-function '\(4 2 . 1\)\) | |
100 <FROBIT | |
101 <CHOMP-IT> | |
102 <CHOMP-SOME-MORE> | |
103 <DIGEST> | |
104 <BELCH> | |
105 ...> | |
106 Finally, the property can be a function name (read the code).") | |
107 | |
108 (defvar indent-mim-comment t | |
109 "*Non-nil means indent string comments.") | |
110 | |
111 (defvar mim-body-indent 2 | |
112 "*Amount to indent in special forms which have DEFINE property on | |
113 `indent-mim-function'.") | |
114 | |
115 (defvar indent-mim-arglist t | |
116 "*nil means indent arglists like ordinary lists. | |
117 t means strings stack under start of arglist and variables stack to | |
118 right of them. Otherwise, strings stack under last string (or start | |
119 of arglist if none) and variables stack to right of them. | |
120 Examples (for values 'stack, t, nil): | |
121 | |
122 \(FOO \"OPT\" BAR \(FOO \"OPT\" BAR \(FOO \"OPT\" BAR | |
123 BAZ MUMBLE BAZ MUMBLE BAZ MUMBLE | |
124 \"AUX\" \"AUX\" \"AUX\" | |
125 BLETCH ... BLETCH ... BLETCH ...") | |
126 | |
127 (put 'DEFINE 'indent-mim-function 'DEFINE) | |
128 (put 'DEFMAC 'indent-mim-function 'DEFINE) | |
129 (put 'BIND 'indent-mim-function 'DEFINE) | |
130 (put 'PROG 'indent-mim-function 'DEFINE) | |
131 (put 'REPEAT 'indent-mim-function 'DEFINE) | |
132 (put 'CASE 'indent-mim-function 'DEFINE) | |
133 (put 'FUNCTION 'indent-mim-function 'DEFINE) | |
134 (put 'MAPF 'indent-mim-function 'DEFINE) | |
135 (put 'MAPR 'indent-mim-function 'DEFINE) | |
136 (put 'UNWIND 'indent-mim-function (cons (* 2 mim-body-indent) mim-body-indent)) | |
137 | |
138 (defvar mim-down-parens-only t | |
139 "*nil means treat ADECLs and ATOM trailers like structures when | |
140 moving down a level of structure.") | |
141 | |
142 (defvar mim-stop-for-slop t | |
143 "*Non-nil means {next previous}-mim-object consider any | |
144 non-whitespace character in column 0 to be a toplevel object, otherwise | |
145 only open paren syntax characters will be considered.") | |
146 | |
147 (fset 'mdl-mode 'mim-mode) | |
148 | |
149 (defun mim-mode () | |
150 "Major mode for editing Mim (MDL in MDL) code. | |
151 Commands: | |
152 If value of `mim-mode-hysterical-bindings' is non-nil, then following | |
153 commands are assigned to escape keys as well (e.g. ESC f = ESC C-f). | |
154 The default action is bind the escape keys. | |
155 \\{mim-mode-map} | |
156 Other Commands: | |
157 Use \\[describe-function] to obtain documentation. | |
158 replace-in-mim-object find-mim-definition fast-syntax-check-mim | |
159 slow-syntax-check-mim backward-down-mim-object forward-up-mim-object | |
160 Variables: | |
161 Use \\[describe-variable] to obtain documentation. | |
162 mim-mode-hook indent-mim-comment indent-mim-arglist indent-mim-function | |
163 mim-body-indent mim-down-parens-only mim-stop-for-slop | |
164 mim-mode-hysterical-bindings | |
165 Entry to this mode calls the value of mim-mode-hook if non-nil." | |
166 (interactive) | |
167 (kill-all-local-variables) | |
168 (if (not mim-mode-map) | |
169 (progn | |
170 (setq mim-mode-map (make-sparse-keymap)) | |
171 (define-key mim-mode-map "\e\^o" 'open-mim-line) | |
172 (define-key mim-mode-map "\e\^q" 'indent-mim-object) | |
173 (define-key mim-mode-map "\e\^p" 'previous-mim-object) | |
174 (define-key mim-mode-map "\e\^n" 'next-mim-object) | |
175 (define-key mim-mode-map "\e\^a" 'beginning-of-DEFINE) | |
176 (define-key mim-mode-map "\e\^e" 'end-of-DEFINE) | |
177 (define-key mim-mode-map "\e\^t" 'transpose-mim-objects) | |
178 (define-key mim-mode-map "\e\^u" 'backward-up-mim-object) | |
179 (define-key mim-mode-map "\e\^d" 'forward-down-mim-object) | |
180 (define-key mim-mode-map "\e\^h" 'mark-mim-object) | |
181 (define-key mim-mode-map "\e\^k" 'forward-kill-mim-object) | |
182 (define-key mim-mode-map "\e\^f" 'forward-mim-object) | |
183 (define-key mim-mode-map "\e\^b" 'backward-mim-object) | |
184 (define-key mim-mode-map "\e^" 'raise-mim-line) | |
185 (define-key mim-mode-map "\e\\" 'fixup-whitespace) | |
186 (define-key mim-mode-map "\177" 'backward-delete-char-untabify) | |
187 (define-key mim-mode-map "\e\177" 'backward-kill-mim-object) | |
188 (define-key mim-mode-map "\^j" 'newline-and-mim-indent) | |
189 (define-key mim-mode-map "\e;" 'begin-mim-comment) | |
190 (define-key mim-mode-map "\t" 'indent-mim-line) | |
191 (define-key mim-mode-map "\e\t" 'indent-mim-object) | |
192 (if (not mim-mode-hysterical-bindings) | |
193 nil | |
194 ;; i really hate this but too many people are accustomed to these. | |
195 (define-key mim-mode-map "\e!" 'line-to-top-of-window) | |
196 (define-key mim-mode-map "\eo" 'open-mim-line) | |
197 (define-key mim-mode-map "\ep" 'previous-mim-object) | |
198 (define-key mim-mode-map "\en" 'next-mim-object) | |
199 (define-key mim-mode-map "\ea" 'beginning-of-DEFINE) | |
200 (define-key mim-mode-map "\ee" 'end-of-DEFINE) | |
201 (define-key mim-mode-map "\et" 'transpose-mim-objects) | |
202 (define-key mim-mode-map "\eu" 'backward-up-mim-object) | |
203 (define-key mim-mode-map "\ed" 'forward-down-mim-object) | |
204 (define-key mim-mode-map "\ek" 'forward-kill-mim-object) | |
205 (define-key mim-mode-map "\ef" 'forward-mim-object) | |
206 (define-key mim-mode-map "\eb" 'backward-mim-object)))) | |
207 (use-local-map mim-mode-map) | |
208 (set-syntax-table mim-mode-syntax-table) | |
209 (make-local-variable 'paragraph-start) | |
210 (setq paragraph-start (concat "^$\\|" page-delimiter)) | |
211 (make-local-variable 'paragraph-separate) | |
212 (setq paragraph-separate paragraph-start) | |
213 (make-local-variable 'paragraph-ignore-fill-prefix) | |
214 (setq paragraph-ignore-fill-prefix t) | |
215 ;; Most people use string comments. | |
216 (make-local-variable 'comment-start) | |
217 (setq comment-start ";\"") | |
218 (make-local-variable 'comment-start-skip) | |
219 (setq comment-start-skip ";\"") | |
220 (make-local-variable 'comment-end) | |
221 (setq comment-end "\"") | |
222 (make-local-variable 'comment-column) | |
223 (setq comment-column 40) | |
224 (make-local-variable 'comment-indent-hook) | |
225 (setq comment-indent-hook 'indent-mim-comment) | |
226 ;; tell generic indenter how to indent. | |
227 (make-local-variable 'indent-line-function) | |
228 (setq indent-line-function 'indent-mim-line) | |
229 ;; look for that paren | |
230 (make-local-variable 'blink-matching-paren-distance) | |
231 (setq blink-matching-paren-distance nil) | |
232 ;; so people who dont like tabs can turn them off locally in indenter. | |
233 (make-local-variable 'indent-tabs-mode) | |
234 (setq indent-tabs-mode t) | |
235 (setq local-abbrev-table mim-mode-abbrev-table) | |
236 (setq major-mode 'mim-mode) | |
237 (setq mode-name "Mim") | |
238 (run-hooks 'mim-mode-hook)) | |
239 | |
240 (defun line-to-top-of-window () | |
241 "Move current line to top of window." | |
242 (interactive) ; for lazy people | |
243 (recenter 0)) | |
244 | |
245 (defun forward-mim-object (arg) | |
246 "Move forward across Mim object. | |
247 With ARG, move forward that many objects." | |
248 (interactive "p") | |
249 ;; this function is wierd because it emulates the behavior of the old | |
250 ;; (gosling) mim-mode - if the arg is 1 and we are `inside' an ADECL, | |
251 ;; more than one character into the ATOM part and not sitting on the | |
252 ;; colon, then we move to the DECL part (just past colon) instead of | |
253 ;; the end of the object (the entire ADECL). otherwise, ADECL's are | |
254 ;; atomic objects. likewise for ATOM trailers. | |
255 (if (= (abs arg) 1) | |
256 (if (inside-atom-p) | |
257 ;; Move to end of ATOM or to trailer (!) or to ADECL (:). | |
258 (forward-sexp arg) | |
259 ;; Either scan an sexp or move over one bracket. | |
260 (forward-mim-objects arg t)) | |
261 ;; in the multi-object case, don't perform any magic. | |
262 ;; treats ATOM trailers and ADECLs atomically, stops at unmatched | |
263 ;; brackets with error. | |
264 (forward-mim-objects arg))) | |
265 | |
266 (defun inside-atom-p () | |
267 ;; Returns t iff inside an atom (takes account of trailers) | |
268 (let ((c1 (preceding-char)) | |
269 (c2 (following-char))) | |
270 (and (or (= (char-syntax c1) ?w) (= (char-syntax c1) ?_) (= c1 ?!)) | |
271 (or (= (char-syntax c2) ?w) (= (char-syntax c2) ?_) (= c2 ?!))))) | |
272 | |
273 (defun forward-mim-objects (arg &optional skip-bracket-p) | |
274 ;; Move over arg objects ignoring ADECLs and trailers. If | |
275 ;; skip-bracket-p is non-nil, then move over one bracket on error. | |
276 (let ((direction (sign arg))) | |
277 (condition-case conditions | |
278 (while (/= arg 0) | |
279 (forward-sexp direction) | |
280 (if (not (inside-adecl-or-trailer-p direction)) | |
281 (setq arg (- arg direction)))) | |
282 (error (if (not skip-bracket-p) | |
283 (signal 'error (cdr conditions)) | |
284 (skip-mim-whitespace direction) | |
285 (goto-char (+ (point) direction))))) | |
286 ;; If we moved too far move back to first interesting character. | |
287 (if (= (point) (buffer-end direction)) (skip-mim-whitespace (- direction))))) | |
288 | |
289 (defun backward-mim-object (&optional arg) | |
290 "Move backward across Mim object. | |
291 With ARG, move backward that many objects." | |
292 (interactive "p") | |
293 (forward-mim-object (if arg (- arg) -1))) | |
294 | |
295 (defun mark-mim-object (&optional arg) | |
296 "Mark following Mim object. | |
297 With ARG, mark that many following (preceding, ARG < 0) objects." | |
298 (interactive "p") | |
299 (push-mark (save-excursion (forward-mim-object (or arg 1)) (point)))) | |
300 | |
301 (defun forward-kill-mim-object (&optional arg) | |
302 "Kill following Mim object. | |
303 With ARG, kill that many objects." | |
304 (interactive "*p") | |
305 (kill-region (point) (progn (forward-mim-object (or arg 1)) (point)))) | |
306 | |
307 (defun backward-kill-mim-object (&optional arg) | |
308 "Kill preceding Mim object. | |
309 With ARG, kill that many objects." | |
310 (interactive "*p") | |
311 (forward-kill-mim-object (- (or arg 1)))) | |
312 | |
313 (defun raise-mim-line (&optional arg) | |
314 "Raise following line, fixing up whitespace at join. | |
315 With ARG raise that many following lines. | |
316 A negative ARG will raise current line and previous lines." | |
317 (interactive "*p") | |
318 (let* ((increment (sign (or arg (setq arg 1)))) | |
319 (direction (if (> arg 0) 1 0))) | |
320 (save-excursion | |
321 (while (/= arg 0) | |
322 ;; move over eol and kill it | |
323 (forward-line direction) | |
324 (delete-region (point) (1- (point))) | |
325 (fixup-whitespace) | |
326 (setq arg (- arg increment)))))) | |
327 | |
328 (defun forward-down-mim-object (&optional arg) | |
329 "Move down a level of Mim structure forwards. | |
330 With ARG, move down that many levels forwards (backwards, ARG < 0)." | |
331 (interactive "p") | |
332 ;; another wierdo - going down `inside' an ADECL or ATOM trailer | |
333 ;; depends on the value of mim-down-parens-only. if nil, treat | |
334 ;; ADECLs and trailers as structured objects. | |
335 (let ((direction (sign (or arg (setq arg 1))))) | |
336 (if (and (= (abs arg) 1) (not mim-down-parens-only)) | |
337 (goto-char | |
338 (save-excursion | |
339 (skip-mim-whitespace direction) | |
340 (if (> direction 0) (re-search-forward "\\s'*")) | |
341 (or (and (let ((c (next-char direction))) | |
342 (or (= (char-syntax c) ?_) | |
343 (= (char-syntax c) ?w))) | |
344 (progn (forward-sexp direction) | |
345 (if (inside-adecl-or-trailer-p direction) | |
346 (point)))) | |
347 (scan-lists (point) direction -1) | |
348 (buffer-end direction)))) | |
349 (while (/= arg 0) | |
350 (goto-char (or (scan-lists (point) direction -1) (buffer-end direction))) | |
351 (setq arg (- arg direction)))))) | |
352 | |
353 (defun backward-down-mim-object (&optional arg) | |
354 "Move down a level of Mim structure backwards. | |
355 With ARG, move down that many levels backwards (forwards, ARG < 0)." | |
356 (interactive "p") | |
357 (forward-down-mim-object (if arg (- arg) -1))) | |
358 | |
359 (defun forward-up-mim-object (&optional arg) | |
360 "Move up a level of Mim structure forwards | |
361 With ARG, move up that many levels forwards (backwards, ARG < 0)." | |
362 (interactive "p") | |
363 (let ((direction (sign (or arg (setq arg 1))))) | |
364 (while (/= arg 0) | |
365 (goto-char (or (scan-lists (point) direction 1) (buffer-end arg))) | |
366 (setq arg (- arg direction))) | |
367 (if (< direction 0) (backward-prefix-chars)))) | |
368 | |
369 (defun backward-up-mim-object (&optional arg) | |
370 "Move up a level of Mim structure backwards | |
371 With ARG, move up that many levels backwards (forwards, ARG > 0)." | |
372 (interactive "p") | |
373 (forward-up-mim-object (if arg (- arg) -1))) | |
374 | |
375 (defun replace-in-mim-object (old new) | |
376 "Replace string in following Mim object." | |
377 (interactive "*sReplace in object: \nsReplace %s with: ") | |
378 (save-restriction | |
379 (narrow-to-region (point) (save-excursion (forward-mim-object 1) (point))) | |
380 (replace-string old new))) | |
381 | |
382 (defun transpose-mim-objects (&optional arg) | |
383 "Transpose Mim objects around point. | |
384 With ARG, transpose preceding object that many times with following objects. | |
385 A negative ARG will transpose backwards." | |
386 (interactive "*p") | |
387 (transpose-subr 'forward-mim-object (or arg 1))) | |
388 | |
389 (defun beginning-of-DEFINE (&optional arg move) | |
390 "Move backward to beginning of surrounding or previous toplevel Mim form. | |
391 With ARG, do it that many times. Stops at last toplevel form seen if buffer | |
392 end is reached." | |
393 (interactive "p") | |
394 (let ((direction (sign (or arg (setq arg 1))))) | |
395 (if (not move) (setq move t)) | |
396 (if (< direction 0) (goto-char (1+ (point)))) | |
397 (while (and (/= arg 0) (re-search-backward "^<" nil move direction)) | |
398 (setq arg (- arg direction))) | |
399 (if (< direction 0) | |
400 (goto-char (1- (point)))))) | |
401 | |
402 (defun end-of-DEFINE (&optional arg) | |
403 "Move forward to end of surrounding or next toplevel mim form. | |
404 With ARG, do it that many times. Stops at end of last toplevel form seen | |
405 if buffer end is reached." | |
406 (interactive "p") | |
407 (if (not arg) (setq arg 1)) | |
408 (if (< arg 0) | |
409 (beginning-of-DEFINE (- (1- arg))) | |
410 (if (not (looking-at "^<")) (setq arg (1+ arg))) | |
411 (beginning-of-DEFINE (- arg) 'move) | |
412 (beginning-of-DEFINE 1)) | |
413 (forward-mim-object 1) | |
414 (forward-line 1)) | |
415 | |
416 (defun next-mim-object (&optional arg) | |
417 "Move to beginning of next toplevel Mim object. | |
418 With ARG, do it that many times. Stops at last object seen if buffer end | |
419 is reached." | |
420 (interactive "p") | |
421 (let ((search-string (if mim-stop-for-slop "^\\S " "^\\s(")) | |
422 (direction (sign (or arg (setq arg 1))))) | |
423 (if (> direction 0) | |
424 (goto-char (1+ (point)))) ; no error if end of buffer | |
425 (while (and (/= arg 0) | |
426 (re-search-forward search-string nil t direction)) | |
427 (setq arg (- arg direction))) | |
428 (if (> direction 0) | |
429 (goto-char (1- (point)))) ; no error if beginning of buffer | |
430 ;; scroll to top of window if moving forward and end not visible. | |
431 (if (not (or (< direction 0) | |
432 (save-excursion (forward-mim-object 1) | |
433 (pos-visible-in-window-p (point))))) | |
434 (recenter 0)))) | |
435 | |
436 (defun previous-mim-object (&optional arg) | |
437 "Move to beginning of previous toplevel Mim object. | |
438 With ARG do it that many times. Stops at last object seen if buffer end | |
439 is reached." | |
440 (interactive "p") | |
441 (next-mim-object (- (or arg 1)))) | |
442 | |
443 (defun calculate-mim-indent (&optional parse-start) | |
444 "Calculate indentation for Mim line. Returns column." | |
445 (save-excursion ; some excursion, huh, toto? | |
446 (beginning-of-line) | |
447 (let ((indent-point (point)) retry state containing-sexp last-sexp | |
448 desired-indent start peek where paren-depth) | |
449 (if parse-start | |
450 (goto-char parse-start) ; should be containing environment | |
451 (catch 'from-the-top | |
452 ;; find a place to start parsing. going backwards is fastest. | |
453 ;; forward-sexp signals error on encountering unmatched open. | |
454 (setq retry t) | |
455 (while retry | |
456 (condition-case nil (forward-sexp -1) (error (setq retry nil))) | |
457 (if (looking-at ".?[ \t]*\"") | |
458 ;; cant parse backward in presence of strings, go forward. | |
459 (progn | |
460 (goto-char indent-point) | |
461 (re-search-backward "^\\s(" nil 'move 1) ; to top of object | |
462 (throw 'from-the-top nil))) | |
463 (setq retry (and retry (/= (current-column) 0)))) | |
464 (skip-chars-backward mim-whitespace) | |
465 (if (not (bobp)) (forward-char -1)) ; onto unclosed open | |
466 (backward-prefix-chars))) | |
467 ;; find outermost containing sexp if we started inside an sexp. | |
468 (while (< (point) indent-point) | |
469 (setq state (parse-partial-sexp (point) indent-point 0))) | |
470 ;; find usual column to indent under (not in string or toplevel). | |
471 ;; on termination, state will correspond to containing environment | |
472 ;; (if retry is nil), where will be position of character to indent | |
473 ;; under normally, and desired-indent will be the column to indent to | |
474 ;; except if inside form, string, or at toplevel. point will be in | |
475 ;; in column to indent to unless inside string. | |
476 (setq retry t) | |
477 (while (and retry (setq paren-depth (car state)) (> paren-depth 0)) | |
478 ;; find innermost containing sexp. | |
479 (setq retry nil) | |
480 (setq last-sexp (car (nthcdr 2 state))) | |
481 (setq containing-sexp (car (cdr state))) | |
482 (goto-char (1+ containing-sexp)) ; to last unclosed open | |
483 (if (and last-sexp (> last-sexp (point))) | |
484 ;; is the last sexp a containing sexp? | |
485 (progn (setq peek (parse-partial-sexp last-sexp indent-point 0)) | |
486 (if (setq retry (car (cdr peek))) (setq state peek)))) | |
487 (if retry | |
488 nil | |
489 (setq where (1+ containing-sexp)) ; innermost containing sexp | |
490 (goto-char where) | |
491 (cond | |
492 ((not last-sexp) ; indent-point after bracket | |
493 (setq desired-indent (current-column))) | |
494 ((= (preceding-char) ?\<) ; it's a form | |
495 (cond ((> (progn (forward-sexp 1) (point)) last-sexp) | |
496 (goto-char where)) ; only one frob | |
497 ((> (save-excursion (forward-line 1) (point)) last-sexp) | |
498 (skip-chars-forward " \t") ; last-sexp is on same line | |
499 (setq where (point))) ; as containing-sexp | |
500 ((progn | |
501 (goto-char last-sexp) | |
502 (beginning-of-line) | |
503 (parse-partial-sexp (point) last-sexp 0 t) | |
504 (or (= (point) last-sexp) | |
505 (save-excursion | |
506 (= (car (parse-partial-sexp (point) last-sexp 0)) | |
507 0)))) | |
508 (backward-prefix-chars) ; last-sexp 1st on line or 1st | |
509 (setq where (point))) ; frob on that line level 0 | |
510 (t (goto-char where)))) ; punt, should never occur | |
511 ((and indent-mim-arglist ; maybe hack arglist | |
512 (= (preceding-char) ?\() ; its a list | |
513 (save-excursion ; look for magic atoms | |
514 (setq peek 0) ; using peek as counter | |
515 (forward-char -1) ; back over containing paren | |
516 (while (and (< (setq peek (1+ peek)) 6) | |
517 (condition-case nil | |
518 (progn (forward-sexp -1) t) | |
519 (error nil)))) | |
520 (and (< peek 6) (looking-at "DEFINE\\|DEFMAC\\|FUNCTION")))) | |
521 ;; frobs stack under strings they belong to or under first | |
522 ;; frob to right of strings they belong to unless luser has | |
523 ;; frob (non-string) on preceding line with different | |
524 ;; indentation. strings stack under start of arglist unless | |
525 ;; mim-indent-arglist is not t, in which case they stack | |
526 ;; under the last string, if any, else the start of the arglist. | |
527 (let ((eol 0) last-string) | |
528 (while (< (point) last-sexp) ; find out where the strings are | |
529 (skip-chars-forward mim-whitespace last-sexp) | |
530 (if (> (setq start (point)) eol) | |
531 (progn ; simultaneously keeping track | |
532 (setq where (min where start)) | |
533 (end-of-line) ; of indentation of first frob | |
534 (setq eol (point)) ; on each line | |
535 (goto-char start))) | |
536 (if (= (following-char) ?\") | |
537 (progn (setq last-string (point)) | |
538 (forward-sexp 1) | |
539 (if (= last-string last-sexp) | |
540 (setq where last-sexp) | |
541 (skip-chars-forward mim-whitespace last-sexp) | |
542 (setq where (point)))) | |
543 (forward-sexp 1))) | |
544 (goto-char indent-point) ; if string is first on | |
545 (skip-chars-forward " \t" (point-max)) ; line we are indenting, it | |
546 (if (= (following-char) ?\") ; goes under arglist start | |
547 (if (and last-string (not (equal indent-mim-arglist t))) | |
548 (setq where last-string) ; or under last string. | |
549 (setq where (1+ containing-sexp))))) | |
550 (goto-char where) | |
551 (setq desired-indent (current-column))) | |
552 (t ; plain vanilla structure | |
553 (cond ((> (save-excursion (forward-line 1) (point)) last-sexp) | |
554 (skip-chars-forward " \t") ; last-sexp is on same line | |
555 (setq where (point))) ; as containing-sexp | |
556 ((progn | |
557 (goto-char last-sexp) | |
558 (beginning-of-line) | |
559 (parse-partial-sexp (point) last-sexp 0 t) | |
560 (or (= (point) last-sexp) | |
561 (save-excursion | |
562 (= (car (parse-partial-sexp (point) last-sexp 0)) | |
563 0)))) | |
564 (backward-prefix-chars) ; last-sexp 1st on line or 1st | |
565 (setq where (point))) ; frob on that line level 0 | |
566 (t (goto-char where))) ; punt, should never occur | |
567 (setq desired-indent (current-column)))))) | |
568 ;; state is innermost containing environment unless toplevel or string. | |
569 (if (car (nthcdr 3 state)) ; inside string | |
570 (progn | |
571 (if last-sexp ; string must be next | |
572 (progn (goto-char last-sexp) | |
573 (forward-sexp 1) | |
574 (search-forward "\"") | |
575 (forward-char -1)) | |
576 (goto-char indent-point) ; toplevel string, look for it | |
577 (re-search-backward "[^\\]\"") | |
578 (forward-char 1)) | |
579 (setq start (point)) ; opening double quote | |
580 (skip-chars-backward " \t") | |
581 (backward-prefix-chars) | |
582 ;; see if the string is really a comment. | |
583 (if (and (looking-at ";[ \t]*\"") indent-mim-comment) | |
584 ;; it's a comment, line up under the start unless disabled. | |
585 (goto-char (1+ start)) | |
586 ;; it's a string, dont mung the indentation. | |
587 (goto-char indent-point) | |
588 (skip-chars-forward " \t")) | |
589 (setq desired-indent (current-column)))) | |
590 ;; point is sitting in usual column to indent to and if retry is nil | |
591 ;; then state corresponds to containing environment. if desired | |
592 ;; indentation not determined, we are inside a form, so call hook. | |
593 (or desired-indent | |
594 (and indent-mim-function | |
595 (not retry) | |
596 (setq desired-indent | |
597 (funcall indent-mim-function state indent-point))) | |
598 (setq desired-indent (current-column))) | |
599 (goto-char indent-point) ; back to where we started | |
600 desired-indent))) ; return column to indent to | |
601 | |
602 (defun indent-mim-function (state indent-point) | |
603 "Compute indentation for Mim special forms. Returns column or nil." | |
604 (let ((containing-sexp (car (cdr state))) (current-indent (point))) | |
605 (save-excursion | |
606 (goto-char (1+ containing-sexp)) | |
607 (backward-prefix-chars) | |
608 ;; make sure we are looking at a symbol. if so, see if it is a special | |
609 ;; symbol. if so, add the special indentation to the indentation of | |
610 ;; the start of the special symbol, unless the property is not | |
611 ;; an integer and not nil (in this case, call the property, it must | |
612 ;; be a function which returns the appropriate indentation or nil and | |
613 ;; does not change the buffer). | |
614 (if (looking-at "\\sw\\|\\s_") | |
615 (let* ((start (current-column)) | |
616 (function | |
617 (intern-soft (buffer-substring (point) | |
618 (progn (forward-sexp 1) | |
619 (point))))) | |
620 (method (get function 'indent-mim-function))) | |
621 (if (or (if (equal method 'DEFINE) (setq method mim-body-indent)) | |
622 (integerp method)) | |
623 ;; only use method if its first line after containing-sexp. | |
624 ;; we could have done this in calculate-mim-indent, but someday | |
625 ;; someone might want to format frobs in a special form based | |
626 ;; on position instead of indenting uniformly (like lisp if), | |
627 ;; so preserve right for posterity. if not first line, | |
628 ;; calculate-mim-indent already knows right indentation - | |
629 ;; give luser chance to change indentation manually by changing | |
630 ;; 1st line after containing-sexp. | |
631 (if (> (progn (forward-line 1) (point)) (car (nthcdr 2 state))) | |
632 (+ method start)) | |
633 (goto-char current-indent) | |
634 (if (consp method) | |
635 ;; list or pointted list of explicit indentations | |
636 (indent-mim-offset state indent-point) | |
637 (if (and (symbolp method) (fboundp method)) | |
638 ;; luser function - s/he better know what's going on. | |
639 ;; should take state and indent-point as arguments - for | |
640 ;; description of state, see parse-partial-sexp | |
641 ;; documentation the function is guaranteed the following: | |
642 ;; (1) state describes the closest surrounding form, | |
643 ;; (2) indent-point is the beginning of the line being | |
644 ;; indented, (3) point points to char in column that would | |
645 ;; normally be used for indentation, (4) function is bound | |
646 ;; to the special ATOM. See indent-mim-offset for example | |
647 ;; of a special function. | |
648 (funcall method state indent-point))))))))) | |
649 | |
650 (defun indent-mim-offset (state indent-point) | |
651 ;; offset forms explicitly according to list of indentations. | |
652 (let ((mim-body-indent mim-body-indent) | |
653 (indentations (get function 'indent-mim-function)) | |
654 (containing-sexp (car (cdr state))) | |
655 (last-sexp (car (nthcdr 2 state))) | |
656 indentation) | |
657 (goto-char (1+ containing-sexp)) | |
658 ;; determine wheich of the indentations to use. | |
659 (while (and (< (point) indent-point) | |
660 (condition-case nil | |
661 (progn (forward-sexp 1) | |
662 (parse-partial-sexp (point) indent-point 1 t)) | |
663 (error nil))) | |
664 (skip-chars-backward " \t") | |
665 (backward-prefix-chars) | |
666 (if (= (following-char) ?\;) | |
667 nil ; ignore comments | |
668 (setq indentation (car indentations)) | |
669 (if (integerp (setq indentations (cdr indentations))) | |
670 ;; if last cdr is integer, that is indentation to use for all | |
671 ;; all the rest of the forms. | |
672 (progn (setq mim-body-indent indentations) | |
673 (setq indentations nil))))) | |
674 (goto-char (1+ containing-sexp)) | |
675 (+ (current-column) (or indentation mim-body-indent)))) | |
676 | |
677 (defun indent-mim-comment (&optional start) | |
678 "Indent a one line (string) Mim comment following object, if any." | |
679 (let* ((old-point (point)) (eol (progn (end-of-line) (point))) state last-sexp) | |
680 ;; this function assumes that comment indenting is enabled. it is caller's | |
681 ;; responsibility to check the indent-mim-comment flag before calling. | |
682 (beginning-of-line) | |
683 (catch 'no-comment | |
684 (setq state (parse-partial-sexp (point) eol)) | |
685 ;; determine if there is an existing regular comment. a `regular' | |
686 ;; comment is defined as a commented string which is the last thing | |
687 ;; on the line and does not extend beyond the end of the line. | |
688 (if (or (not (setq last-sexp (car (nthcdr 2 state)))) | |
689 (car (nthcdr 3 state))) | |
690 ;; empty line or inside string (multiple line). | |
691 (throw 'no-comment nil)) | |
692 ;; could be a comment, but make sure its not the only object. | |
693 (beginning-of-line) | |
694 (parse-partial-sexp (point) eol 0 t) | |
695 (if (= (point) last-sexp) | |
696 ;; only one object on line | |
697 (throw 'no-comment t)) | |
698 (goto-char last-sexp) | |
699 (skip-chars-backward " \t") | |
700 (backward-prefix-chars) | |
701 (if (not (looking-at ";[ \t]*\"")) | |
702 ;; aint no comment | |
703 (throw 'no-comment nil)) | |
704 ;; there is an existing regular comment | |
705 (delete-horizontal-space) | |
706 ;; move it to comment-column if possible else to tab-stop | |
707 (if (< (current-column) comment-column) | |
708 (indent-to comment-column) | |
709 (tab-to-tab-stop))) | |
710 (goto-char old-point))) | |
711 | |
712 (defun indent-mim-line () | |
713 "Indent line of Mim code." | |
714 (interactive "*") | |
715 (let* ((position (- (point-max) (point))) | |
716 (bol (progn (beginning-of-line) (point))) | |
717 (indent (calculate-mim-indent))) | |
718 (skip-chars-forward " \t") | |
719 (if (/= (current-column) indent) | |
720 (progn (delete-region bol (point)) (indent-to indent))) | |
721 (if (> (- (point-max) position) (point)) (goto-char (- (point-max) position))))) | |
722 | |
723 (defun newline-and-mim-indent () | |
724 "Insert newline at point and indent." | |
725 (interactive "*") | |
726 ;; commented code would correct indentation of line in arglist which | |
727 ;; starts with string, but it would indent every line twice. luser can | |
728 ;; just say tab after typing string to get same effect. | |
729 ;(if indent-mim-arglist (indent-mim-line)) | |
730 (newline) | |
731 (indent-mim-line)) | |
732 | |
733 (defun open-mim-line (&optional lines) | |
734 "Insert newline before point and indent. | |
735 With ARG insert that many newlines." | |
736 (interactive "*p") | |
737 (beginning-of-line) | |
738 (let ((indent (calculate-mim-indent))) | |
739 (while (> lines 0) | |
740 (newline) | |
741 (forward-line -1) | |
742 (indent-to indent) | |
743 (setq lines (1- lines))))) | |
744 | |
745 (defun indent-mim-object (&optional dont-indent-first-line) | |
746 "Indent object following point and all lines contained inside it. | |
747 With ARG, idents only contained lines (skips first line)." | |
748 (interactive "*P") | |
749 (let (end bol indent start) | |
750 (save-excursion (parse-partial-sexp (point) (point-max) 0 t) | |
751 (setq start (point)) | |
752 (forward-sexp 1) | |
753 (setq end (- (point-max) (point)))) | |
754 (save-excursion | |
755 (if (not dont-indent-first-line) (indent-mim-line)) | |
756 (while (progn (forward-line 1) (> (- (point-max) (point)) end)) | |
757 (setq indent (calculate-mim-indent start)) | |
758 (setq bol (point)) | |
759 (skip-chars-forward " \t") | |
760 (if (/= indent (current-column)) | |
761 (progn (delete-region bol (point)) (indent-to indent))) | |
762 (if indent-mim-comment (indent-mim-comment)))))) | |
763 | |
764 (defun find-mim-definition (name) | |
765 "Search for definition of function, macro, or gfcn. | |
766 You need type only enough of the name to be unambiguous." | |
767 (interactive "sName: ") | |
768 (let (where) | |
769 (save-excursion | |
770 (goto-char (point-min)) | |
771 (condition-case nil | |
772 (progn | |
773 (re-search-forward | |
774 (concat "^<\\(DEFINE\\|\\DEFMAC\\|FCN\\|GFCN\\)\\([ \t]*\\)" | |
775 name)) | |
776 (setq where (point))) | |
777 (error (error "Can't find %s" name)))) | |
778 (if where | |
779 (progn (push-mark) | |
780 (goto-char where) | |
781 (beginning-of-line) | |
782 (recenter 0))))) | |
783 | |
784 (defun begin-mim-comment () | |
785 "Move to existing comment or insert empty comment." | |
786 (interactive "*") | |
787 (let* ((eol (progn (end-of-line) (point))) | |
788 (bol (progn (beginning-of-line) (point)))) | |
789 ;; check for existing comment first. | |
790 (if (re-search-forward ";[ \t]*\"" eol t) | |
791 ;; found it. indent if desired and go there. | |
792 (if indent-mim-comment | |
793 (let ((where (- (point-max) (point)))) | |
794 (indent-mim-comment) | |
795 (goto-char (- (point-max) where)))) | |
796 ;; nothing there, make a comment. | |
797 (let (state last-sexp) | |
798 ;; skip past all the sexps on the line | |
799 (goto-char bol) | |
800 (while (and (equal (car (setq state (parse-partial-sexp (point) eol 0))) | |
801 0) | |
802 (car (nthcdr 2 state))) | |
803 (setq last-sexp (car (nthcdr 2 state)))) | |
804 (if (car (nthcdr 3 state)) | |
805 nil ; inside a string, punt | |
806 (delete-region (point) eol) ; flush trailing whitespace | |
807 (if (and (not last-sexp) (equal (car state) 0)) | |
808 (indent-to (calculate-mim-indent)) ; empty, indent like code | |
809 (if (> (current-column) comment-column) ; indent to comment column | |
810 (tab-to-tab-stop) ; unless past it, else to | |
811 (indent-to comment-column))) ; tab-stop | |
812 ;; if luser changes comment-{start end} to something besides semi | |
813 ;; followed by zero or more whitespace characters followed by string | |
814 ;; delimiters, the code above fails to find existing comments, but as | |
815 ;; taa says, `let the losers lose'. | |
816 (insert comment-start) | |
817 (save-excursion (insert comment-end))))))) | |
818 | |
819 (defun skip-mim-whitespace (direction) | |
820 (if (>= direction 0) | |
821 (skip-chars-forward mim-whitespace (point-max)) | |
822 (skip-chars-backward mim-whitespace (point-min)))) | |
823 | |
824 (defun inside-adecl-or-trailer-p (direction) | |
825 (if (>= direction 0) | |
826 (looking-at ":\\|!-") | |
827 (or (= (preceding-char) ?:) | |
828 (looking-at "!-")))) | |
829 | |
830 (defun sign (n) | |
831 "Returns -1 if N < 0, else 1." | |
832 (if (>= n 0) 1 -1)) | |
833 | |
834 (defun abs (n) | |
835 "Returns the absolute value of N." | |
836 (if (>= n 0) n (- n))) | |
837 | |
838 (defun next-char (direction) | |
839 "Returns preceding-char if DIRECTION < 0, otherwise following-char." | |
840 (if (>= direction 0) (following-char) (preceding-char))) | |
584 | 841 |
842 (provide 'mim-mode) | |
843 | |
659
505130d1ddf8
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
584
diff
changeset
|
844 ;;; mim-mode.el ends here |