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