1 ;;; icon.el --- mode for editing Icon code
3 ;; Copyright (C) 1989, 2001-2012 Free Software Foundation, Inc.
5 ;; Author: Chris Smith <csmith@convex.com>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; A major mode for editing the Icon programming language.
30 (defvar icon-mode-abbrev-table nil
31 "Abbrev table in use in Icon-mode buffers.")
32 (define-abbrev-table 'icon-mode-abbrev-table
())
34 (defvar icon-mode-map
()
35 "Keymap used in Icon mode.")
38 (let ((map (make-sparse-keymap "Icon")))
39 (setq icon-mode-map
(make-sparse-keymap))
40 (define-key icon-mode-map
"{" 'electric-icon-brace
)
41 (define-key icon-mode-map
"}" 'electric-icon-brace
)
42 (define-key icon-mode-map
"\e\C-h" 'mark-icon-function
)
43 (define-key icon-mode-map
"\e\C-a" 'beginning-of-icon-defun
)
44 (define-key icon-mode-map
"\e\C-e" 'end-of-icon-defun
)
45 (define-key icon-mode-map
"\e\C-q" 'indent-icon-exp
)
46 (define-key icon-mode-map
"\177" 'backward-delete-char-untabify
)
48 (define-key icon-mode-map
[menu-bar
] (make-sparse-keymap "Icon"))
49 (define-key icon-mode-map
[menu-bar icon
]
51 (define-key map
[beginning-of-icon-defun
] '("Beginning of function" . beginning-of-icon-defun
))
52 (define-key map
[end-of-icon-defun
] '("End of function" . end-of-icon-defun
))
53 (define-key map
[comment-region
] '("Comment Out Region" . comment-region
))
54 (define-key map
[indent-region
] '("Indent Region" . indent-region
))
55 (define-key map
[indent-line
] '("Indent Line" . icon-indent-command
))
56 (put 'eval-region
'menu-enable
'mark-active
)
57 (put 'comment-region
'menu-enable
'mark-active
)
58 (put 'indent-region
'menu-enable
'mark-active
)))
60 (defvar icon-mode-syntax-table nil
61 "Syntax table in use in Icon-mode buffers.")
63 (if icon-mode-syntax-table
65 (setq icon-mode-syntax-table
(make-syntax-table))
66 (modify-syntax-entry ?
\\ "\\" icon-mode-syntax-table
)
67 (modify-syntax-entry ?
# "<" icon-mode-syntax-table
)
68 (modify-syntax-entry ?
\n ">" 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 (modify-syntax-entry ?
= "." icon-mode-syntax-table
)
75 (modify-syntax-entry ?%
"." icon-mode-syntax-table
)
76 (modify-syntax-entry ?
< "." icon-mode-syntax-table
)
77 (modify-syntax-entry ?
> "." icon-mode-syntax-table
)
78 (modify-syntax-entry ?
& "." icon-mode-syntax-table
)
79 (modify-syntax-entry ?|
"." icon-mode-syntax-table
)
80 (modify-syntax-entry ?
\' "\"" icon-mode-syntax-table
))
83 "Mode for editing Icon code."
84 :link
'(custom-group-link :tag
"Font Lock Faces group" font-lock-faces
)
87 (defcustom icon-indent-level
4
88 "*Indentation of Icon statements with respect to containing block."
92 (defcustom icon-brace-imaginary-offset
0
93 "*Imagined indentation of a Icon open brace that actually follows a statement."
97 (defcustom icon-brace-offset
0
98 "*Extra indentation for braces, compared with other text in same context."
102 (defcustom icon-continued-statement-offset
4
103 "*Extra indent for Icon lines not starting new statements."
107 (defcustom icon-continued-brace-offset
0
108 "*Extra indent for Icon substatements that start with open-braces.
109 This is in addition to `icon-continued-statement-offset'."
113 (defcustom icon-auto-newline nil
114 "*Non-nil means automatically newline before and after braces Icon code.
115 This applies when braces are inserted."
119 (defcustom icon-tab-always-indent t
120 "*Non-nil means TAB in Icon mode should always reindent the current line.
121 It will then reindent, regardless of where in the line point is
122 when the TAB command is used."
126 (defvar icon-imenu-generic-expression
127 '((nil "^[ \t]*procedure[ \t]+\\(\\sw+\\)[ \t]*(" 1))
128 "Imenu expression for Icon mode. See `imenu-generic-expression'.")
133 (define-derived-mode icon-mode prog-mode
"Icon"
134 "Major mode for editing Icon code.
135 Expression and list commands understand all Icon brackets.
136 Tab indents for Icon code.
137 Paragraphs are separated by blank lines only.
138 Delete converts tabs to spaces as it moves back.
140 Variables controlling indentation style:
141 icon-tab-always-indent
142 Non-nil means TAB in Icon mode should always reindent the current line,
143 regardless of where in the line point is when the TAB command is used.
145 Non-nil means automatically newline before and after braces
146 inserted in Icon code.
148 Indentation of Icon statements within surrounding block.
149 The surrounding block's indentation is the indentation
150 of the line on which the open-brace appears.
151 icon-continued-statement-offset
152 Extra indentation given to a substatement, such as the
153 then-clause of an if or body of a while.
154 icon-continued-brace-offset
155 Extra indentation given to a brace that starts a substatement.
156 This is in addition to `icon-continued-statement-offset'.
158 Extra indentation for line if it starts with an open brace.
159 icon-brace-imaginary-offset
160 An open brace following other text is treated as if it were
161 this far to the right of the start of its line.
163 Turning on Icon mode calls the value of the variable `icon-mode-hook'
164 with no args, if that value is non-nil."
165 :abbrev-table icon-mode-abbrev-table
166 (set (make-local-variable 'paragraph-start
) (concat "$\\|" page-delimiter
))
167 (set (make-local-variable 'paragraph-separate
) paragraph-start
)
168 (set (make-local-variable 'indent-line-function
) #'icon-indent-line
)
169 (set (make-local-variable 'comment-start
) "# ")
170 (set (make-local-variable 'comment-end
) "")
171 (set (make-local-variable 'comment-start-skip
) "# *")
172 (set (make-local-variable 'comment-indent-function
) 'icon-comment-indent
)
173 (set (make-local-variable 'indent-line-function
) 'icon-indent-line
)
175 (set (make-local-variable 'font-lock-defaults
)
176 '((icon-font-lock-keywords
177 icon-font-lock-keywords-1 icon-font-lock-keywords-2
)
178 nil nil
((?_ .
"w")) beginning-of-defun
179 ;; Obsoleted by Emacs 19.35 parse-partial-sexp's COMMENTSTOP.
180 ;;(font-lock-comment-start-regexp . "#")
181 (font-lock-mark-block-function . mark-defun
)))
183 (set (make-local-variable 'imenu-generic-expression
)
184 icon-imenu-generic-expression
)
186 ;; we start from the assertion that `hs-special-modes-alist' is autoloaded.
187 (unless (assq 'icon-mode hs-special-modes-alist
)
188 (setq hs-special-modes-alist
189 (cons '(icon-mode "\\<procedure\\>" "\\<end\\>" nil
190 icon-forward-sexp-function
)
191 hs-special-modes-alist
))))
193 ;; This is used by indent-for-comment to decide how much to
194 ;; indent a comment in Icon code based on its context.
195 (defun icon-comment-indent ()
196 (if (looking-at "^#") 0 comment-column
))
198 (defun electric-icon-brace (arg)
199 "Insert character and correct line's indentation."
205 (skip-chars-backward " \t")
207 (if icon-auto-newline
208 (progn (icon-indent-line) (newline) t
)
211 (insert last-command-event
)
213 (if icon-auto-newline
216 ;; (newline) may have done auto-fill
217 (setq insertpos
(- (point) 2))
220 (if insertpos
(goto-char (1+ insertpos
)))
224 (goto-char insertpos
)
225 (self-insert-command (prefix-numeric-value arg
)))
226 (self-insert-command (prefix-numeric-value arg
)))))
228 (defun icon-indent-command (&optional whole-exp
)
229 "Indent current line as Icon code, or in some cases insert a tab character.
230 If `icon-tab-always-indent' is non-nil (the default), always indent current
231 line. Otherwise, indent the current line only if point is at the left margin
232 or in the line's indentation; otherwise insert a tab.
234 A numeric argument, regardless of its value, means indent rigidly all the
235 lines of the expression starting after point so that this line becomes
236 properly indented. The relative indentation among the lines of the
237 expression are preserved."
240 ;; If arg, always indent this line as Icon
241 ;; and shift remaining lines of expression the same amount.
242 (let ((shift-amt (icon-indent-line))
245 (if icon-tab-always-indent
254 (indent-code-rigidly beg end shift-amt
"#")))
255 (if (and (not icon-tab-always-indent
)
257 (skip-chars-backward " \t")
260 (icon-indent-line))))
262 (defun icon-indent-line ()
263 "Indent current line as Icon code.
264 Return the amount the indentation changed by."
265 (let ((indent (calculate-icon-indent nil
))
267 (case-fold-search nil
)
268 (pos (- (point-max) (point))))
271 (cond ((eq indent nil
)
272 (setq indent
(current-indentation)))
276 (skip-chars-forward " \t")
277 (if (listp indent
) (setq indent
(car indent
)))
278 (cond ((and (looking-at "else\\b")
279 (not (looking-at "else\\s_")))
280 (setq indent
(save-excursion
281 (icon-backward-to-start-of-if)
282 (current-indentation))))
283 ((or (= (following-char) ?
})
284 (looking-at "end\\b"))
285 (setq indent
(- indent icon-indent-level
)))
286 ((= (following-char) ?
{)
287 (setq indent
(+ indent icon-brace-offset
))))))
288 (skip-chars-forward " \t")
289 (setq shift-amt
(- indent
(current-column)))
290 (if (zerop shift-amt
)
291 (if (> (- (point-max) pos
) (point))
292 (goto-char (- (point-max) pos
)))
293 (delete-region beg
(point))
295 ;; If initial point was within line's indentation,
296 ;; position after the indentation. Else stay at same point in text.
297 (if (> (- (point-max) pos
) (point))
298 (goto-char (- (point-max) pos
))))
301 (defun calculate-icon-indent (&optional parse-start
)
302 "Return appropriate indentation for current line as Icon code.
303 In usual case returns an integer: the column to indent to.
304 Returns nil if line starts inside a string, t if in a comment."
307 (let ((indent-point (point))
308 (case-fold-search nil
)
313 (goto-char parse-start
)
314 (setq toplevel
(beginning-of-icon-defun)))
315 (while (< (point) indent-point
)
316 (setq parse-start
(point))
317 (setq state
(parse-partial-sexp (point) indent-point
0))
318 (setq containing-sexp
(car (cdr state
))))
319 (cond ((or (nth 3 state
) (nth 4 state
))
320 ;; return nil or t if should not change this line
322 ((and containing-sexp
323 (/= (char-after containing-sexp
) ?
{))
324 ;; line is expression, not statement:
325 ;; indent to just after the surrounding open.
326 (goto-char (1+ containing-sexp
))
330 ;; Outside any procedures.
331 (progn (icon-backward-to-noncomment (point-min))
332 (if (icon-is-continuation-line)
333 icon-continued-statement-offset
0))
335 (if (null containing-sexp
)
336 (progn (beginning-of-icon-defun)
337 (setq containing-sexp
(point))))
338 (goto-char indent-point
)
339 ;; Is it a continuation or a new statement?
340 ;; Find previous non-comment character.
341 (icon-backward-to-noncomment containing-sexp
)
342 ;; Now we get the answer.
343 (if (icon-is-continuation-line)
344 ;; This line is continuation of preceding line's statement;
345 ;; indent icon-continued-statement-offset more than the
346 ;; first line of the statement.
348 (icon-backward-to-start-of-continued-exp containing-sexp
)
349 (+ icon-continued-statement-offset
(current-column)
350 (if (save-excursion (goto-char indent-point
)
351 (skip-chars-forward " \t")
352 (eq (following-char) ?
{))
353 icon-continued-brace-offset
0)))
354 ;; This line starts a new statement.
355 ;; Position following last unclosed open.
356 (goto-char containing-sexp
)
357 ;; Is line first statement after an open-brace?
359 ;; If no, find that first statement and indent like it.
361 (if (looking-at "procedure\\s ")
364 (while (progn (skip-chars-forward " \t\n")
366 ;; Skip over comments following openbrace.
368 ;; The first following code counts
369 ;; if it is before the line we want to indent.
370 (and (< (point) indent-point
)
372 ;; If no previous statement,
373 ;; indent it relative to line brace is on.
374 ;; For open brace in column zero, don't let statement
375 ;; start there too. If icon-indent-level is zero,
376 ;; use icon-brace-offset + icon-continued-statement-offset
378 ;; For open-braces not the first thing in a line,
379 ;; add in icon-brace-imaginary-offset.
380 (+ (if (and (bolp) (zerop icon-indent-level
))
382 icon-continued-statement-offset
)
384 ;; Move back over whitespace before the openbrace.
385 ;; If openbrace is not first nonwhite thing on the line,
386 ;; add the icon-brace-imaginary-offset.
387 (progn (skip-chars-backward " \t")
388 (if (bolp) 0 icon-brace-imaginary-offset
))
389 ;; Get initial indentation of the line we are on.
390 (current-indentation))))))))))
392 ;; List of words to check for as the last thing on a line.
393 ;; If cdr is t, next line is a continuation of the same statement,
394 ;; if cdr is nil, next line starts a new (possibly indented) statement.
396 (defconst icon-resword-alist
397 '(("by" . t
) ("case" . t
) ("create") ("do") ("dynamic" . t
) ("else")
398 ("every" . t
) ("if" . t
) ("global" . t
) ("initial" . t
)
399 ("link" . t
) ("local" . t
) ("of") ("record" . t
) ("repeat" . t
)
400 ("static" . t
) ("then") ("to" . t
) ("until" . t
) ("while" . t
)))
402 (defun icon-is-continuation-line ()
403 (let* ((ch (preceding-char))
404 (ch-syntax (char-syntax ch
)))
405 (if (eq ch-syntax ?w
)
406 (assoc (buffer-substring
407 (progn (forward-word -
1) (point))
408 (progn (forward-word 1) (point)))
410 (not (memq ch
'(0 ?\
; ?\} ?\{ ?\) ?\] ?\" ?\' ?\# ?\, ?\. ?\n))))))
412 (defun icon-backward-to-noncomment (lim)
415 (skip-chars-backward " \t\n\f" lim
)
416 (setq opoint
(point))
418 (if (and (nth 5 (parse-partial-sexp (point) opoint
))
420 (search-backward "#")
423 (defun icon-backward-to-start-of-continued-exp (lim)
424 (if (memq (preceding-char) '(?\
) ?\
]))
427 (skip-chars-forward " \t")
429 ((<= (point) lim
) (goto-char (1+ lim
)))
430 ((not (icon-is-continued-line)) 0)
431 ((and (eq (char-syntax (following-char)) ?w
)
433 (assoc (buffer-substring (point)
434 (save-excursion (forward-word 1) (point)))
435 icon-resword-alist
))) 0)
436 (t (end-of-line 0) (icon-backward-to-start-of-continued-exp lim
))))
438 (defun icon-is-continued-line ()
441 (icon-is-continuation-line)))
443 (defun icon-backward-to-start-of-if (&optional limit
)
444 "Move to the start of the last \"unbalanced\" if."
445 (or limit
(setq limit
(save-excursion (beginning-of-icon-defun) (point))))
447 (case-fold-search nil
))
448 (while (not (zerop if-level
))
450 (cond ((looking-at "else\\b")
451 (setq if-level
(1+ if-level
)))
452 ((looking-at "if\\b")
453 (setq if-level
(1- if-level
)))
456 (goto-char limit
))))))
458 (defun mark-icon-function ()
459 "Put mark at end of Icon function, point at beginning."
464 (beginning-of-line 0)
465 (beginning-of-icon-defun))
467 (defun beginning-of-icon-defun ()
468 "Go to the start of the enclosing procedure; return t if at top level."
470 (if (re-search-backward "^procedure\\s \\|^end[ \t\n]" (point-min) 'move
)
474 (defun end-of-icon-defun ()
476 (if (not (bobp)) (forward-char -
1))
477 (re-search-forward "\\(\\s \\|^\\)end\\(\\s \\|$\\)" (point-max) 'move
)
481 (defun indent-icon-exp ()
482 "Indent each line of the Icon grouping following point."
484 (let ((indent-stack (list nil
))
485 (contain-stack (list (point)))
486 (case-fold-search nil
)
487 outer-loop-done inner-loop-done state ostate
488 this-indent last-depth
495 (setq outer-loop-done nil
)
496 (while (and (not (eobp)) (not outer-loop-done
))
497 (setq last-depth next-depth
)
498 ;; Compute how depth changes over this line
499 ;; plus enough other lines to get to one that
500 ;; does not end inside a comment or string.
501 ;; Meanwhile, do appropriate indentation on comment lines.
502 (setq inner-loop-done nil
)
503 (while (and (not inner-loop-done
)
504 (not (and (eobp) (setq outer-loop-done t
))))
506 (setq state
(parse-partial-sexp (point) (progn (end-of-line) (point))
508 (setq next-depth
(car state
))
509 (if (or (nth 4 ostate
))
511 (if (or (nth 3 state
))
513 (setq inner-loop-done t
)))
514 (if (<= next-depth
0)
515 (setq outer-loop-done t
))
518 (while (> last-depth next-depth
)
519 (setq indent-stack
(cdr indent-stack
)
520 contain-stack
(cdr contain-stack
)
521 last-depth
(1- last-depth
)))
522 (while (< last-depth next-depth
)
523 (setq indent-stack
(cons nil indent-stack
)
524 contain-stack
(cons nil contain-stack
)
525 last-depth
(1+ last-depth
)))
526 (if (null (car contain-stack
))
527 (setcar contain-stack
(or (car (cdr state
))
528 (save-excursion (forward-sexp -
1)
531 (skip-chars-forward " \t")
534 (if (and (car indent-stack
)
535 (>= (car indent-stack
) 0))
536 ;; Line is on an existing nesting level.
537 ;; Lines inside parens are handled specially.
538 (if (/= (char-after (car contain-stack
)) ?
{)
539 (setq this-indent
(car indent-stack
))
540 ;; Line is at statement level.
541 ;; Is it a new statement? Is it an else?
542 ;; Find last non-comment character before this line
544 (setq at-else
(looking-at "else\\W"))
545 (setq at-brace
(= (following-char) ?
{))
546 (icon-backward-to-noncomment opoint
)
547 (if (icon-is-continuation-line)
548 ;; Preceding line did not end in comma or semi;
549 ;; indent this line icon-continued-statement-offset
550 ;; more than previous.
552 (icon-backward-to-start-of-continued-exp (car contain-stack
))
554 (+ icon-continued-statement-offset
(current-column)
555 (if at-brace icon-continued-brace-offset
0))))
556 ;; Preceding line ended in comma or semi;
557 ;; use the standard indent for this level.
559 (progn (icon-backward-to-start-of-if opoint
)
560 (setq this-indent
(current-indentation)))
561 (setq this-indent
(car indent-stack
))))))
562 ;; Just started a new nesting level.
563 ;; Compute the standard indent for this level.
564 (let ((val (calculate-icon-indent
565 (if (car indent-stack
)
566 (- (car indent-stack
))))))
568 (setq this-indent val
))))
569 ;; Adjust line indentation according to its contents
570 (if (or (= (following-char) ?
})
571 (looking-at "end\\b"))
572 (setq this-indent
(- this-indent icon-indent-level
)))
573 (if (= (following-char) ?
{)
574 (setq this-indent
(+ this-indent icon-brace-offset
)))
575 ;; Put chosen indentation into effect.
576 (or (= (current-column) this-indent
)
578 (delete-region (point) (progn (beginning-of-line) (point)))
579 (indent-to this-indent
)))
580 ;; Indent any comment following the text.
581 (or (looking-at comment-start-skip
)
582 (if (re-search-forward comment-start-skip
(line-end-position) t
)
583 (progn (indent-for-comment) (beginning-of-line))))))))))
585 (defconst icon-font-lock-keywords-1
588 ;; Fontify procedure name definitions.
589 '("^[ \t]*\\(procedure\\)\\>[ \t]*\\(\\sw+\\)?"
590 (1 font-lock-builtin-face
) (2 font-lock-function-name-face nil t
))))
591 "Subdued level highlighting for Icon mode.")
593 (defconst icon-font-lock-keywords-2
595 icon-font-lock-keywords-1
598 ;; Fontify all type specifiers.
600 (regexp-opt '("null" "string" "co-expression" "table" "integer"
601 "cset" "set" "real" "file" "list") 'words
)
602 'font-lock-type-face
)
603 ;; Fontify all keywords.
607 '("break" "do" "next" "repeat" "to" "by" "else" "if" "not" "return"
608 "until" "case" "of" "while" "create" "every" "suspend" "default"
609 "fail" "record" "then") 'words
)
610 'font-lock-keyword-face
)
612 (cons (regexp-opt '("end" "initial") 'words
)
613 'font-lock-builtin-face
)
614 ;; Fontify all system variables.
617 '("&allocated" "&ascii" "&clock" "&col" "&collections" "&column"
618 "&control" "&cset" "¤t" "&date" "&dateline" "&digits" "&dump"
619 "&e" "&error" "&errornumber" "&errortext" "&errorvalue" "&errout"
620 "&eventcode" "&eventsource" "&eventvalue" "&fail" "&features"
621 "&file" "&host" "&input" "&interval" "&lcase" "&ldrag" "&letters"
622 "&level" "&line" "&lpress" "&lrelease" "&main" "&mdrag" "&meta"
623 "&mpress" "&mrelease" "&null" "&output" "&phi" "&pi" "&pos"
624 "&progname" "&random" "&rdrag" "®ions" "&resize" "&row"
625 "&rpress" "&rrelease" "&shift" "&source" "&storage" "&subject"
626 "&time" "&trace" "&ucase" "&version" "&window" "&x" "&y") t
)
627 'font-lock-constant-face
)
628 (cons ;; global local static declarations and link files
631 (regexp-opt '("global" "link" "local" "static") t
)
633 '((1 font-lock-builtin-face
)
634 (font-lock-match-c-style-declaration-item-and-skip-to-next
635 (goto-char (or (match-beginning 2) (match-end 1))) nil
636 (1 (if (match-beginning 2)
637 font-lock-function-name-face
638 font-lock-variable-name-face
)))))
640 (cons ;; $define $elif $ifdef $ifndef $undef
642 (regexp-opt'("$define" "$elif" "$ifdef" "$ifndef" "$undef") t
)
643 "\\>[ \t]*\\([^ \t\n]+\\)?")
644 '((1 font-lock-builtin-face
)
645 (4 font-lock-variable-name-face nil t
)))
646 (cons ;; $dump $endif $else $include
648 "^" (regexp-opt'("$dump" "$endif" "$else" "$include") t
) "\\>" )
649 'font-lock-builtin-face
)
650 (cons ;; $warning $error
651 (concat "^" (regexp-opt '("$warning" "$error") t
)
652 "\\>[ \t]*\\(.+\\)?")
653 '((1 font-lock-builtin-face
) (3 font-lock-warning-face nil t
))))))
654 "Gaudy level highlighting for Icon mode.")
656 (defvar icon-font-lock-keywords icon-font-lock-keywords-1
657 "Default expressions to highlight in `icon-mode'.")
659 ;;;used by hs-minor-mode
660 (defun icon-forward-sexp-function (arg)
662 (beginning-of-icon-defun)
668 ;;; icon.el ends here