1 ;;; icon.el --- mode for editing Icon code
3 ;; Copyright (C) 1989, 2001-2016 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-strictly -
1) (point))
408 (progn (forward-word-strictly 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-strictly 1)
436 icon-resword-alist
))) 0)
437 (t (end-of-line 0) (icon-backward-to-start-of-continued-exp lim
))))
439 (defun icon-is-continued-line ()
442 (icon-is-continuation-line)))
444 (defun icon-backward-to-start-of-if (&optional limit
)
445 "Move to the start of the last \"unbalanced\" if."
446 (or limit
(setq limit
(save-excursion (beginning-of-icon-defun) (point))))
448 (case-fold-search nil
))
449 (while (not (zerop if-level
))
451 (cond ((looking-at "else\\b")
452 (setq if-level
(1+ if-level
)))
453 ((looking-at "if\\b")
454 (setq if-level
(1- if-level
)))
457 (goto-char limit
))))))
459 (defun mark-icon-function ()
460 "Put mark at end of Icon function, point at beginning."
465 (beginning-of-line 0)
466 (beginning-of-icon-defun))
468 (defun beginning-of-icon-defun ()
469 "Go to the start of the enclosing procedure; return t if at top level."
471 (if (re-search-backward "^procedure\\s \\|^end[ \t\n]" (point-min) 'move
)
475 (defun end-of-icon-defun ()
477 (if (not (bobp)) (forward-char -
1))
478 (re-search-forward "\\(\\s \\|^\\)end\\(\\s \\|$\\)" (point-max) 'move
)
479 (forward-word-strictly -
1)
482 (defun indent-icon-exp ()
483 "Indent each line of the Icon grouping following point."
485 (let ((indent-stack (list nil
))
486 (contain-stack (list (point)))
487 (case-fold-search nil
)
488 outer-loop-done inner-loop-done state ostate
489 this-indent last-depth
496 (setq outer-loop-done nil
)
497 (while (and (not (eobp)) (not outer-loop-done
))
498 (setq last-depth next-depth
)
499 ;; Compute how depth changes over this line
500 ;; plus enough other lines to get to one that
501 ;; does not end inside a comment or string.
502 ;; Meanwhile, do appropriate indentation on comment lines.
503 (setq inner-loop-done nil
)
504 (while (and (not inner-loop-done
)
505 (not (and (eobp) (setq outer-loop-done t
))))
507 (setq state
(parse-partial-sexp (point) (progn (end-of-line) (point))
509 (setq next-depth
(car state
))
510 (if (or (nth 4 ostate
))
512 (if (or (nth 3 state
))
514 (setq inner-loop-done t
)))
515 (if (<= next-depth
0)
516 (setq outer-loop-done t
))
519 (while (> last-depth next-depth
)
520 (setq indent-stack
(cdr indent-stack
)
521 contain-stack
(cdr contain-stack
)
522 last-depth
(1- last-depth
)))
523 (while (< last-depth next-depth
)
524 (setq indent-stack
(cons nil indent-stack
)
525 contain-stack
(cons nil contain-stack
)
526 last-depth
(1+ last-depth
)))
527 (if (null (car contain-stack
))
528 (setcar contain-stack
(or (car (cdr state
))
529 (save-excursion (forward-sexp -
1)
532 (skip-chars-forward " \t")
535 (if (and (car indent-stack
)
536 (>= (car indent-stack
) 0))
537 ;; Line is on an existing nesting level.
538 ;; Lines inside parens are handled specially.
539 (if (/= (char-after (car contain-stack
)) ?
{)
540 (setq this-indent
(car indent-stack
))
541 ;; Line is at statement level.
542 ;; Is it a new statement? Is it an else?
543 ;; Find last non-comment character before this line
545 (setq at-else
(looking-at "else\\W"))
546 (setq at-brace
(= (following-char) ?
{))
547 (icon-backward-to-noncomment opoint
)
548 (if (icon-is-continuation-line)
549 ;; Preceding line did not end in comma or semi;
550 ;; indent this line icon-continued-statement-offset
551 ;; more than previous.
553 (icon-backward-to-start-of-continued-exp (car contain-stack
))
555 (+ icon-continued-statement-offset
(current-column)
556 (if at-brace icon-continued-brace-offset
0))))
557 ;; Preceding line ended in comma or semi;
558 ;; use the standard indent for this level.
560 (progn (icon-backward-to-start-of-if opoint
)
561 (setq this-indent
(current-indentation)))
562 (setq this-indent
(car indent-stack
))))))
563 ;; Just started a new nesting level.
564 ;; Compute the standard indent for this level.
565 (let ((val (calculate-icon-indent
566 (if (car indent-stack
)
567 (- (car indent-stack
))))))
569 (setq this-indent val
))))
570 ;; Adjust line indentation according to its contents
571 (if (or (= (following-char) ?
})
572 (looking-at "end\\b"))
573 (setq this-indent
(- this-indent icon-indent-level
)))
574 (if (= (following-char) ?
{)
575 (setq this-indent
(+ this-indent icon-brace-offset
)))
576 ;; Put chosen indentation into effect.
577 (or (= (current-column) this-indent
)
579 (delete-region (point) (progn (beginning-of-line) (point)))
580 (indent-to this-indent
)))
581 ;; Indent any comment following the text.
582 (or (looking-at comment-start-skip
)
583 (if (re-search-forward comment-start-skip
(line-end-position) t
)
584 (progn (indent-for-comment) (beginning-of-line))))))))))
586 (defconst icon-font-lock-keywords-1
589 ;; Fontify procedure name definitions.
590 '("^[ \t]*\\(procedure\\)\\>[ \t]*\\(\\sw+\\)?"
591 (1 font-lock-builtin-face
) (2 font-lock-function-name-face nil t
))))
592 "Subdued level highlighting for Icon mode.")
594 (defconst icon-font-lock-keywords-2
596 icon-font-lock-keywords-1
599 ;; Fontify all type specifiers.
601 (regexp-opt '("null" "string" "co-expression" "table" "integer"
602 "cset" "set" "real" "file" "list") 'words
)
603 'font-lock-type-face
)
604 ;; Fontify all keywords.
608 '("break" "do" "next" "repeat" "to" "by" "else" "if" "not" "return"
609 "until" "case" "of" "while" "create" "every" "suspend" "default"
610 "fail" "record" "then") 'words
)
611 'font-lock-keyword-face
)
613 (cons (regexp-opt '("end" "initial") 'words
)
614 'font-lock-builtin-face
)
615 ;; Fontify all system variables.
618 '("&allocated" "&ascii" "&clock" "&col" "&collections" "&column"
619 "&control" "&cset" "¤t" "&date" "&dateline" "&digits" "&dump"
620 "&e" "&error" "&errornumber" "&errortext" "&errorvalue" "&errout"
621 "&eventcode" "&eventsource" "&eventvalue" "&fail" "&features"
622 "&file" "&host" "&input" "&interval" "&lcase" "&ldrag" "&letters"
623 "&level" "&line" "&lpress" "&lrelease" "&main" "&mdrag" "&meta"
624 "&mpress" "&mrelease" "&null" "&output" "&phi" "&pi" "&pos"
625 "&progname" "&random" "&rdrag" "®ions" "&resize" "&row"
626 "&rpress" "&rrelease" "&shift" "&source" "&storage" "&subject"
627 "&time" "&trace" "&ucase" "&version" "&window" "&x" "&y") t
)
628 'font-lock-constant-face
)
629 (cons ;; global local static declarations and link files
632 (regexp-opt '("global" "link" "local" "static") t
)
634 '((1 font-lock-builtin-face
)
635 (font-lock-match-c-style-declaration-item-and-skip-to-next
636 (goto-char (or (match-beginning 2) (match-end 1))) nil
637 (1 (if (match-beginning 2)
638 font-lock-function-name-face
639 font-lock-variable-name-face
)))))
641 (cons ;; $define $elif $ifdef $ifndef $undef
643 (regexp-opt'("$define" "$elif" "$ifdef" "$ifndef" "$undef") t
)
644 "\\>[ \t]*\\([^ \t\n]+\\)?")
645 '((1 font-lock-builtin-face
)
646 (4 font-lock-variable-name-face nil t
)))
647 (cons ;; $dump $endif $else $include
649 "^" (regexp-opt'("$dump" "$endif" "$else" "$include") t
) "\\>" )
650 'font-lock-builtin-face
)
651 (cons ;; $warning $error
652 (concat "^" (regexp-opt '("$warning" "$error") t
)
653 "\\>[ \t]*\\(.+\\)?")
654 '((1 font-lock-builtin-face
) (3 font-lock-warning-face nil t
))))))
655 "Gaudy level highlighting for Icon mode.")
657 (defvar icon-font-lock-keywords icon-font-lock-keywords-1
658 "Default expressions to highlight in `icon-mode'.")
660 ;;;used by hs-minor-mode
661 (defun icon-forward-sexp-function (arg)
663 (beginning-of-icon-defun)
669 ;;; icon.el ends here