*** empty log message ***
[emacs.git] / lisp / progmodes / icon.el
blobcab251427ba6a70417831ad201fe4a92bfc6ea59
1 ;;; icon.el --- mode for editing Icon code
3 ;; Note: use
4 ;; (autoload 'icon-mode "icon" nil t)
5 ;; (setq auto-mode-alist (cons '("\\.icn$" . icon-mode) auto-mode-alist))
6 ;; if not permanently installed in your emacs
8 ;; Icon code editing commands for Emacs
9 ;; Derived from c-mode.el 15-Feb-89 Chris Smith convex!csmith
10 ;; Copyright (C) 1989 Free Software Foundation, Inc.
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 1, or (at your option)
17 ;; any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to
26 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
29 (defvar icon-mode-abbrev-table nil
30 "Abbrev table in use in Icon-mode buffers.")
31 (define-abbrev-table 'icon-mode-abbrev-table ())
33 (defvar icon-mode-map ()
34 "Keymap used in Icon mode.")
35 (if icon-mode-map
37 (setq icon-mode-map (make-sparse-keymap))
38 (define-key icon-mode-map "{" 'electric-icon-brace)
39 (define-key icon-mode-map "}" 'electric-icon-brace)
40 (define-key icon-mode-map "\e\C-h" 'mark-icon-function)
41 (define-key icon-mode-map "\e\C-a" 'beginning-of-icon-defun)
42 (define-key icon-mode-map "\e\C-e" 'end-of-icon-defun)
43 (define-key icon-mode-map "\e\C-q" 'indent-icon-exp)
44 (define-key icon-mode-map "\177" 'backward-delete-char-untabify)
45 (define-key icon-mode-map "\t" 'icon-indent-command))
47 (defvar icon-mode-syntax-table nil
48 "Syntax table in use in Icon-mode buffers.")
50 (if icon-mode-syntax-table
52 (setq icon-mode-syntax-table (make-syntax-table))
53 (modify-syntax-entry ?\\ "\\" icon-mode-syntax-table)
54 (modify-syntax-entry ?# "<" icon-mode-syntax-table)
55 (modify-syntax-entry ?\n ">" icon-mode-syntax-table)
56 (modify-syntax-entry ?$ "." icon-mode-syntax-table)
57 (modify-syntax-entry ?/ "." icon-mode-syntax-table)
58 (modify-syntax-entry ?* "." icon-mode-syntax-table)
59 (modify-syntax-entry ?+ "." icon-mode-syntax-table)
60 (modify-syntax-entry ?- "." icon-mode-syntax-table)
61 (modify-syntax-entry ?= "." icon-mode-syntax-table)
62 (modify-syntax-entry ?% "." icon-mode-syntax-table)
63 (modify-syntax-entry ?< "." icon-mode-syntax-table)
64 (modify-syntax-entry ?> "." icon-mode-syntax-table)
65 (modify-syntax-entry ?& "." icon-mode-syntax-table)
66 (modify-syntax-entry ?| "." icon-mode-syntax-table)
67 (modify-syntax-entry ?\' "\"" icon-mode-syntax-table))
69 (defconst icon-indent-level 4
70 "*Indentation of Icon statements with respect to containing block.")
71 (defconst icon-brace-imaginary-offset 0
72 "*Imagined indentation of a Icon open brace that actually follows a statement.")
73 (defconst icon-brace-offset 0
74 "*Extra indentation for braces, compared with other text in same context.")
75 (defconst icon-continued-statement-offset 4
76 "*Extra indent for lines not starting new statements.")
77 (defconst icon-continued-brace-offset 0
78 "*Extra indent for substatements that start with open-braces.
79 This is in addition to icon-continued-statement-offset.")
81 (defconst icon-auto-newline nil
82 "*Non-nil means automatically newline before and after braces
83 inserted in Icon code.")
85 (defconst icon-tab-always-indent t
86 "*Non-nil means TAB in Icon mode should always reindent the current line,
87 regardless of where in the line point is when the TAB command is used.")
89 (defun icon-mode ()
90 "Major mode for editing Icon code.
91 Expression and list commands understand all Icon brackets.
92 Tab indents for Icon code.
93 Paragraphs are separated by blank lines only.
94 Delete converts tabs to spaces as it moves back.
95 \\{icon-mode-map}
96 Variables controlling indentation style:
97 icon-tab-always-indent
98 Non-nil means TAB in Icon mode should always reindent the current line,
99 regardless of where in the line point is when the TAB command is used.
100 icon-auto-newline
101 Non-nil means automatically newline before and after braces
102 inserted in Icon code.
103 icon-indent-level
104 Indentation of Icon statements within surrounding block.
105 The surrounding block's indentation is the indentation
106 of the line on which the open-brace appears.
107 icon-continued-statement-offset
108 Extra indentation given to a substatement, such as the
109 then-clause of an if or body of a while.
110 icon-continued-brace-offset
111 Extra indentation given to a brace that starts a substatement.
112 This is in addition to `icon-continued-statement-offset'.
113 icon-brace-offset
114 Extra indentation for line if it starts with an open brace.
115 icon-brace-imaginary-offset
116 An open brace following other text is treated as if it were
117 this far to the right of the start of its line.
119 Turning on Icon mode calls the value of the variable `icon-mode-hook'
120 with no args, if that value is non-nil."
121 (interactive)
122 (kill-all-local-variables)
123 (use-local-map icon-mode-map)
124 (setq major-mode 'icon-mode)
125 (setq mode-name "Icon")
126 (setq local-abbrev-table icon-mode-abbrev-table)
127 (set-syntax-table icon-mode-syntax-table)
128 (make-local-variable 'paragraph-start)
129 (setq paragraph-start (concat "^$\\|" page-delimiter))
130 (make-local-variable 'paragraph-separate)
131 (setq paragraph-separate paragraph-start)
132 (make-local-variable 'indent-line-function)
133 (setq indent-line-function 'icon-indent-line)
134 (make-local-variable 'require-final-newline)
135 (setq require-final-newline t)
136 (make-local-variable 'comment-start)
137 (setq comment-start "# ")
138 (make-local-variable 'comment-end)
139 (setq comment-end "")
140 (make-local-variable 'comment-column)
141 (setq comment-column 32)
142 (make-local-variable 'comment-start-skip)
143 (setq comment-start-skip "# *")
144 (make-local-variable 'comment-indent-hook)
145 (setq comment-indent-hook 'icon-comment-indent)
146 (run-hooks 'icon-mode-hook))
148 ;; This is used by indent-for-comment to decide how much to
149 ;; indent a comment in Icon code based on its context.
150 (defun icon-comment-indent ()
151 (if (looking-at "^#")
153 (save-excursion
154 (skip-chars-backward " \t")
155 (max (if (bolp) 0 (1+ (current-column)))
156 comment-column))))
158 (defun electric-icon-brace (arg)
159 "Insert character and correct line's indentation."
160 (interactive "P")
161 (let (insertpos)
162 (if (and (not arg)
163 (eolp)
164 (or (save-excursion
165 (skip-chars-backward " \t")
166 (bolp))
167 (if icon-auto-newline
168 (progn (icon-indent-line) (newline) t)
169 nil)))
170 (progn
171 (insert last-command-char)
172 (icon-indent-line)
173 (if icon-auto-newline
174 (progn
175 (newline)
176 ;; (newline) may have done auto-fill
177 (setq insertpos (- (point) 2))
178 (icon-indent-line)))
179 (save-excursion
180 (if insertpos (goto-char (1+ insertpos)))
181 (delete-char -1))))
182 (if insertpos
183 (save-excursion
184 (goto-char insertpos)
185 (self-insert-command (prefix-numeric-value arg)))
186 (self-insert-command (prefix-numeric-value arg)))))
188 (defun icon-indent-command (&optional whole-exp)
189 (interactive "P")
190 "Indent current line as Icon code, or in some cases insert a tab character.
191 If `icon-tab-always-indent' is non-nil (the default), always indent current
192 line. Otherwise, indent the current line only if point is at the left margin
193 or in the line's indentation; otherwise insert a tab.
195 A numeric argument, regardless of its value, means indent rigidly all the
196 lines of the expression starting after point so that this line becomes
197 properly indented. The relative indentation among the lines of the
198 expression are preserved."
199 (if whole-exp
200 ;; If arg, always indent this line as Icon
201 ;; and shift remaining lines of expression the same amount.
202 (let ((shift-amt (icon-indent-line))
203 beg end)
204 (save-excursion
205 (if icon-tab-always-indent
206 (beginning-of-line))
207 (setq beg (point))
208 (forward-sexp 1)
209 (setq end (point))
210 (goto-char beg)
211 (forward-line 1)
212 (setq beg (point)))
213 (if (> end beg)
214 (indent-code-rigidly beg end shift-amt "#")))
215 (if (and (not icon-tab-always-indent)
216 (save-excursion
217 (skip-chars-backward " \t")
218 (not (bolp))))
219 (insert-tab)
220 (icon-indent-line))))
222 (defun icon-indent-line ()
223 "Indent current line as Icon code.
224 Return the amount the indentation changed by."
225 (let ((indent (calculate-icon-indent nil))
226 beg shift-amt
227 (case-fold-search nil)
228 (pos (- (point-max) (point))))
229 (beginning-of-line)
230 (setq beg (point))
231 (cond ((eq indent nil)
232 (setq indent (current-indentation)))
233 ((eq indent t)
234 (setq indent (calculate-icon-indent-within-comment)))
235 ((looking-at "[ \t]*#")
236 (setq indent 0))
238 (skip-chars-forward " \t")
239 (if (listp indent) (setq indent (car indent)))
240 (cond ((and (looking-at "else\\b")
241 (not (looking-at "else\\s_")))
242 (setq indent (save-excursion
243 (icon-backward-to-start-of-if)
244 (current-indentation))))
245 ((or (= (following-char) ?})
246 (looking-at "end\\b"))
247 (setq indent (- indent icon-indent-level)))
248 ((= (following-char) ?{)
249 (setq indent (+ indent icon-brace-offset))))))
250 (skip-chars-forward " \t")
251 (setq shift-amt (- indent (current-column)))
252 (if (zerop shift-amt)
253 (if (> (- (point-max) pos) (point))
254 (goto-char (- (point-max) pos)))
255 (delete-region beg (point))
256 (indent-to indent)
257 ;; If initial point was within line's indentation,
258 ;; position after the indentation. Else stay at same point in text.
259 (if (> (- (point-max) pos) (point))
260 (goto-char (- (point-max) pos))))
261 shift-amt))
263 (defun calculate-icon-indent (&optional parse-start)
264 "Return appropriate indentation for current line as Icon code.
265 In usual case returns an integer: the column to indent to.
266 Returns nil if line starts inside a string, t if in a comment."
267 (save-excursion
268 (beginning-of-line)
269 (let ((indent-point (point))
270 (case-fold-search nil)
271 state
272 containing-sexp
273 toplevel)
274 (if parse-start
275 (goto-char parse-start)
276 (setq toplevel (beginning-of-icon-defun)))
277 (while (< (point) indent-point)
278 (setq parse-start (point))
279 (setq state (parse-partial-sexp (point) indent-point 0))
280 (setq containing-sexp (car (cdr state))))
281 (cond ((or (nth 3 state) (nth 4 state))
282 ;; return nil or t if should not change this line
283 (nth 4 state))
284 ((and containing-sexp
285 (/= (char-after containing-sexp) ?{))
286 ;; line is expression, not statement:
287 ;; indent to just after the surrounding open.
288 (goto-char (1+ containing-sexp))
289 (current-column))
291 (if toplevel
292 ;; Outside any procedures.
293 (progn (icon-backward-to-noncomment (point-min))
294 (if (icon-is-continuation-line)
295 icon-continued-statement-offset 0))
296 ;; Statement level.
297 (if (null containing-sexp)
298 (progn (beginning-of-icon-defun)
299 (setq containing-sexp (point))))
300 (goto-char indent-point)
301 ;; Is it a continuation or a new statement?
302 ;; Find previous non-comment character.
303 (icon-backward-to-noncomment containing-sexp)
304 ;; Now we get the answer.
305 (if (icon-is-continuation-line)
306 ;; This line is continuation of preceding line's statement;
307 ;; indent icon-continued-statement-offset more than the
308 ;; first line of the statement.
309 (progn
310 (icon-backward-to-start-of-continued-exp containing-sexp)
311 (+ icon-continued-statement-offset (current-column)
312 (if (save-excursion (goto-char indent-point)
313 (skip-chars-forward " \t")
314 (eq (following-char) ?{))
315 icon-continued-brace-offset 0)))
316 ;; This line starts a new statement.
317 ;; Position following last unclosed open.
318 (goto-char containing-sexp)
319 ;; Is line first statement after an open-brace?
321 ;; If no, find that first statement and indent like it.
322 (save-excursion
323 (if (looking-at "procedure\\s ")
324 (forward-sexp 3)
325 (forward-char 1))
326 (while (progn (skip-chars-forward " \t\n")
327 (looking-at "#"))
328 ;; Skip over comments following openbrace.
329 (forward-line 1))
330 ;; The first following code counts
331 ;; if it is before the line we want to indent.
332 (and (< (point) indent-point)
333 (current-column)))
334 ;; If no previous statement,
335 ;; indent it relative to line brace is on.
336 ;; For open brace in column zero, don't let statement
337 ;; start there too. If icon-indent-level is zero,
338 ;; use icon-brace-offset + icon-continued-statement-offset
339 ;; instead.
340 ;; For open-braces not the first thing in a line,
341 ;; add in icon-brace-imaginary-offset.
342 (+ (if (and (bolp) (zerop icon-indent-level))
343 (+ icon-brace-offset
344 icon-continued-statement-offset)
345 icon-indent-level)
346 ;; Move back over whitespace before the openbrace.
347 ;; If openbrace is not first nonwhite thing on the line,
348 ;; add the icon-brace-imaginary-offset.
349 (progn (skip-chars-backward " \t")
350 (if (bolp) 0 icon-brace-imaginary-offset))
351 ;; Get initial indentation of the line we are on.
352 (current-indentation))))))))))
354 ;; List of words to check for as the last thing on a line.
355 ;; If cdr is t, next line is a continuation of the same statement,
356 ;; if cdr is nil, next line starts a new (possibly indented) statement.
358 (defconst icon-resword-alist
359 '(("by" . t) ("case" . t) ("create") ("do") ("dynamic" . t) ("else")
360 ("every" . t) ("if" . t) ("global" . t) ("initial" . t)
361 ("link" . t) ("local" . t) ("of") ("record" . t) ("repeat" . t)
362 ("static" . t) ("then") ("to" . t) ("until" . t) ("while" . t)))
364 (defun icon-is-continuation-line ()
365 (let* ((ch (preceding-char))
366 (ch-syntax (char-syntax ch)))
367 (if (eq ch-syntax ?w)
368 (assoc (buffer-substring
369 (progn (forward-word -1) (point))
370 (progn (forward-word 1) (point)))
371 icon-resword-alist)
372 (not (memq ch '(0 ?\; ?\} ?\{ ?\) ?\] ?\" ?\' ?\n))))))
374 (defun icon-backward-to-noncomment (lim)
375 (let (opoint stop)
376 (while (not stop)
377 (skip-chars-backward " \t\n\f" lim)
378 (setq opoint (point))
379 (beginning-of-line)
380 (if (and (nth 5 (parse-partial-sexp (point) opoint))
381 (< lim (point)))
382 (search-backward "#")
383 (setq stop t)))))
385 (defun icon-backward-to-start-of-continued-exp (lim)
386 (if (memq (preceding-char) '(?\) ?\]))
387 (forward-sexp -1))
388 (beginning-of-line)
389 (skip-chars-forward " \t")
390 (cond
391 ((<= (point) lim) (goto-char (1+ lim)))
392 ((not (icon-is-continued-line)) 0)
393 ((and (eq (char-syntax (following-char)) ?w)
394 (cdr
395 (assoc (buffer-substring (point)
396 (save-excursion (forward-word 1) (point)))
397 icon-resword-alist))) 0)
398 (t (end-of-line 0) (icon-backward-to-start-of-continued-exp lim))))
400 (defun icon-is-continued-line ()
401 (save-excursion
402 (end-of-line 0)
403 (icon-is-continuation-line)))
405 (defun icon-backward-to-start-of-if (&optional limit)
406 "Move to the start of the last \"unbalanced\" if."
407 (or limit (setq limit (save-excursion (beginning-of-icon-defun) (point))))
408 (let ((if-level 1)
409 (case-fold-search nil))
410 (while (not (zerop if-level))
411 (backward-sexp 1)
412 (cond ((looking-at "else\\b")
413 (setq if-level (1+ if-level)))
414 ((looking-at "if\\b")
415 (setq if-level (1- if-level)))
416 ((< (point) limit)
417 (setq if-level 0)
418 (goto-char limit))))))
420 (defun mark-icon-function ()
421 "Put mark at end of Icon function, point at beginning."
422 (interactive)
423 (push-mark (point))
424 (end-of-icon-defun)
425 (push-mark (point))
426 (beginning-of-line 0)
427 (beginning-of-icon-defun))
429 (defun beginning-of-icon-defun ()
430 "Go to the start of the enclosing procedure; return t if at top level."
431 (interactive)
432 (if (re-search-backward "^procedure\\s \\|^end[ \t\n]" (point-min) 'move)
433 (looking-at "e")
436 (defun end-of-icon-defun ()
437 (interactive)
438 (if (not (bobp)) (forward-char -1))
439 (re-search-forward "\\(\\s \\|^\\)end\\(\\s \\|$\\)" (point-max) 'move)
440 (forward-word -1)
441 (forward-line 1))
443 (defun indent-icon-exp ()
444 "Indent each line of the Icon grouping following point."
445 (interactive)
446 (let ((indent-stack (list nil))
447 (contain-stack (list (point)))
448 (case-fold-search nil)
449 restart outer-loop-done inner-loop-done state ostate
450 this-indent last-sexp
451 at-else at-brace at-do
452 (opoint (point))
453 (next-depth 0))
454 (save-excursion
455 (forward-sexp 1))
456 (save-excursion
457 (setq outer-loop-done nil)
458 (while (and (not (eobp)) (not outer-loop-done))
459 (setq last-depth next-depth)
460 ;; Compute how depth changes over this line
461 ;; plus enough other lines to get to one that
462 ;; does not end inside a comment or string.
463 ;; Meanwhile, do appropriate indentation on comment lines.
464 (setq innerloop-done nil)
465 (while (and (not innerloop-done)
466 (not (and (eobp) (setq outer-loop-done t))))
467 (setq ostate state)
468 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
469 nil nil state))
470 (setq next-depth (car state))
471 (if (and (car (cdr (cdr state)))
472 (>= (car (cdr (cdr state))) 0))
473 (setq last-sexp (car (cdr (cdr state)))))
474 (if (or (nth 4 ostate))
475 (icon-indent-line))
476 (if (or (nth 3 state))
477 (forward-line 1)
478 (setq innerloop-done t)))
479 (if (<= next-depth 0)
480 (setq outer-loop-done t))
481 (if outer-loop-done
483 (if (/= last-depth next-depth)
484 (setq last-sexp nil))
485 (while (> last-depth next-depth)
486 (setq indent-stack (cdr indent-stack)
487 contain-stack (cdr contain-stack)
488 last-depth (1- last-depth)))
489 (while (< last-depth next-depth)
490 (setq indent-stack (cons nil indent-stack)
491 contain-stack (cons nil contain-stack)
492 last-depth (1+ last-depth)))
493 (if (null (car contain-stack))
494 (setcar contain-stack (or (car (cdr state))
495 (save-excursion (forward-sexp -1)
496 (point)))))
497 (forward-line 1)
498 (skip-chars-forward " \t")
499 (if (eolp)
501 (if (and (car indent-stack)
502 (>= (car indent-stack) 0))
503 ;; Line is on an existing nesting level.
504 ;; Lines inside parens are handled specially.
505 (if (/= (char-after (car contain-stack)) ?{)
506 (setq this-indent (car indent-stack))
507 ;; Line is at statement level.
508 ;; Is it a new statement? Is it an else?
509 ;; Find last non-comment character before this line
510 (save-excursion
511 (setq at-else (looking-at "else\\W"))
512 (setq at-brace (= (following-char) ?{))
513 (icon-backward-to-noncomment opoint)
514 (if (icon-is-continuation-line)
515 ;; Preceding line did not end in comma or semi;
516 ;; indent this line icon-continued-statement-offset
517 ;; more than previous.
518 (progn
519 (icon-backward-to-start-of-continued-exp (car contain-stack))
520 (setq this-indent
521 (+ icon-continued-statement-offset (current-column)
522 (if at-brace icon-continued-brace-offset 0))))
523 ;; Preceding line ended in comma or semi;
524 ;; use the standard indent for this level.
525 (if at-else
526 (progn (icon-backward-to-start-of-if opoint)
527 (setq this-indent (current-indentation)))
528 (setq this-indent (car indent-stack))))))
529 ;; Just started a new nesting level.
530 ;; Compute the standard indent for this level.
531 (let ((val (calculate-icon-indent
532 (if (car indent-stack)
533 (- (car indent-stack))))))
534 (setcar indent-stack
535 (setq this-indent val))))
536 ;; Adjust line indentation according to its contents
537 (if (or (= (following-char) ?})
538 (looking-at "end\\b"))
539 (setq this-indent (- this-indent icon-indent-level)))
540 (if (= (following-char) ?{)
541 (setq this-indent (+ this-indent icon-brace-offset)))
542 ;; Put chosen indentation into effect.
543 (or (= (current-column) this-indent)
544 (progn
545 (delete-region (point) (progn (beginning-of-line) (point)))
546 (indent-to this-indent)))
547 ;; Indent any comment following the text.
548 (or (looking-at comment-start-skip)
549 (if (re-search-forward comment-start-skip (save-excursion (end-of-line) (point)) t)
550 (progn (indent-for-comment) (beginning-of-line))))))))))
552 ;;; icon.el ends here