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