home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / contrib / emacs / el.txt < prev    next >
Internet Message Format  |  1997-03-11  |  27KB

  1. From icon-group-sender Sun Mar  9 09:32:43 1997
  2. Received: from cheltenham.cs.arizona.edu by jupiter.cs.arizona.edu; (5.65/1.1.8.2/08Nov94-0446PM)
  3.     id AA14828; Sun, 9 Mar 1997 09:32:29 -0700
  4. Received: by cheltenham.cs.arizona.edu; Sun, 9 Mar 1997 09:30:28 MST
  5. Date: Sun, 9 Mar 1997 09:10:12 -0700 (MST)
  6. From: Dan Nicolaescu <done@ece.arizona.edu>
  7. X-Sender: done@florence
  8. To: icon-group@cs.arizona.edu
  9. Subject: icon.el updated
  10. Message-Id: <Pine.SOL.3.91.970308095151.913B-100000@florence>
  11. Mime-Version: 1.0
  12. Content-Type: TEXT/PLAIN; charset=US-ASCII
  13. Content-Length: 26545
  14. Errors-To: icon-group-errors@cs.arizona.edu
  15. Status: R
  16.  
  17. Hi all!
  18.  
  19. I have tweaked a little the icon.el file to add support for syntax 
  20. coloring for icon files in emacs. 
  21. Use (setq font-lock-maximum-decoration t) to see all the effects.
  22.  
  23. I also added imenu support (do a M-x imenu-add-to-menubar and you will 
  24. get a menu containing the procedures in the icon file where when 
  25. selecting an entry the point is moved the the beginning of the 
  26. coresponding procedure).
  27.  
  28. I added hideshow support. This enables hiding the bodies of the 
  29. procedures (and when emacs-19.35 will be out also the comments) to ease 
  30. the navigation in an icon file. Use M-x hs-minor-mode and then 
  31. M-x hs-hide-all to see the effects.
  32.  
  33. About syntax coloring there are some limitations:
  34. -curently the "end" "global" "initial" "local" "link" "procedure" 
  35. "$include" "$define" keywords are showed in the same color as 
  36. the types. The next version of font-lock.el (it was posted in 
  37. gnu.emacs.sources in november-december I think) will have a new face 
  38. font-lock-builtin-face which will be used for this. 
  39. -the only the first variable from a local/global declaration will be 
  40. colored using font-lock-variable-face. it's not very likely to that I am 
  41. going to fix this, so if somebody does it, please let me know. If you 
  42. want to do this, take a look how this is done for c/c++.
  43. -am working on adding some more coloring in a $define statement.
  44.  
  45. Please take a look at the way I have grouped the keywords for coloring 
  46. and email me your ideas, comments and critics. 
  47.  
  48. Everything was only teste using emacs-19.34. I have no idea if it works 
  49. with any other versions.
  50.  
  51. Enjoy,
  52.     Dan
  53.  
  54. Here is the file:
  55.  
  56. ;;; icon.el --- mode for editing Icon code
  57.  
  58. ;; Copyright (C) 1989 Free Software Foundation, Inc.
  59.  
  60. ;; Author: Chris Smith <csmith@convex.com>
  61. ;; Created: 15 Feb 89
  62. ;; Keywords: languages
  63.  
  64. ;; This file is part of GNU Emacs.
  65.  
  66. ;; GNU Emacs is free software; you can redistribute it and/or modify
  67. ;; it under the terms of the GNU General Public License as published by
  68. ;; the Free Software Foundation; either version 2, or (at your option)
  69. ;; any later version.
  70.  
  71. ;; GNU Emacs is distributed in the hope that it will be useful,
  72. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  73. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  74. ;; GNU General Public License for more details.
  75.  
  76. ;; You should have received a copy of the GNU General Public License
  77. ;; along with GNU Emacs; see the file COPYING.  If not, write to the
  78. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  79. ;; Boston, MA 02111-1307, USA.
  80.  
  81. ;;; Commentary:
  82.  
  83. ;; A major mode for editing the Icon programming language.
  84.  
  85. ;;; Code:
  86.  
  87. (defvar icon-mode-abbrev-table nil
  88.   "Abbrev table in use in Icon-mode buffers.")
  89. (define-abbrev-table 'icon-mode-abbrev-table ())
  90.  
  91. (defvar icon-mode-map ()
  92.   "Keymap used in Icon mode.")
  93. (if icon-mode-map
  94.     ()
  95.   (let ((map (make-sparse-keymap "Icon")))
  96.     (setq icon-mode-map (make-sparse-keymap))
  97.     (define-key icon-mode-map "{" 'electric-icon-brace)
  98.     (define-key icon-mode-map "}" 'electric-icon-brace)
  99.     (define-key icon-mode-map "\e\C-h" 'mark-icon-function)
  100.     (define-key icon-mode-map "\e\C-a" 'beginning-of-icon-defun)
  101.     (define-key icon-mode-map "\e\C-e" 'end-of-icon-defun)
  102.     (define-key icon-mode-map "\e\C-q" 'indent-icon-exp)
  103.     (define-key icon-mode-map "\177" 'backward-delete-char-untabify)
  104.     (define-key icon-mode-map "\t" 'icon-indent-command)
  105.   
  106.     (define-key icon-mode-map [menu-bar] (make-sparse-keymap))
  107.     (define-key icon-mode-map [menu-bar icon]
  108.       (cons "Icon" map))
  109.     (define-key map [beginning-of-icon-defun] '("Beginning of function" . beginning-of-icon-defun))
  110.     (define-key map [end-of-icon-defun] '("End of function" . end-of-icon-defun))
  111.     (define-key map [comment-region] '("Comment Out Region" . comment-region))
  112.     (define-key map [indent-region] '("Indent Region" . indent-region))
  113.     (define-key map [indent-line] '("Indent Line" . icon-indent-command))
  114.     (put 'eval-region 'menu-enable 'mark-active)
  115.     (put 'comment-region 'menu-enable 'mark-active)
  116.     (put 'indent-region 'menu-enable 'mark-active)))
  117.  
  118.  
  119. (defvar icon-mode-syntax-table nil
  120.   "Syntax table in use in Icon-mode buffers.")
  121.  
  122. (if icon-mode-syntax-table
  123.     ()
  124.   (setq icon-mode-syntax-table (make-syntax-table))
  125.   (modify-syntax-entry ?\\ "\\" icon-mode-syntax-table)
  126.   (modify-syntax-entry ?# "<" icon-mode-syntax-table)
  127.   (modify-syntax-entry ?\n ">" icon-mode-syntax-table)
  128.   (modify-syntax-entry ?$ "." icon-mode-syntax-table)
  129.   (modify-syntax-entry ?/ "." icon-mode-syntax-table)
  130.   (modify-syntax-entry ?* "." icon-mode-syntax-table)
  131.   (modify-syntax-entry ?+ "." icon-mode-syntax-table)
  132.   (modify-syntax-entry ?- "." icon-mode-syntax-table)
  133.   (modify-syntax-entry ?= "." icon-mode-syntax-table)
  134.   (modify-syntax-entry ?% "." icon-mode-syntax-table)
  135.   (modify-syntax-entry ?< "." icon-mode-syntax-table)
  136.   (modify-syntax-entry ?> "." icon-mode-syntax-table)
  137.   (modify-syntax-entry ?& "." icon-mode-syntax-table)
  138.   (modify-syntax-entry ?| "." icon-mode-syntax-table)
  139.   (modify-syntax-entry ?\' "\"" icon-mode-syntax-table))
  140.  
  141. (defvar icon-indent-level 4
  142.   "*Indentation of Icon statements with respect to containing block.")
  143. (defvar icon-brace-imaginary-offset 0
  144.   "*Imagined indentation of a Icon open brace that actually follows a statement.")
  145. (defvar icon-brace-offset 0
  146.   "*Extra indentation for braces, compared with other text in same context.")
  147. (defvar icon-continued-statement-offset 4
  148.   "*Extra indent for lines not starting new statements.")
  149. (defvar icon-continued-brace-offset 0
  150.   "*Extra indent for substatements that start with open-braces.
  151. This is in addition to icon-continued-statement-offset.")
  152.  
  153. (defvar icon-auto-newline nil
  154.   "*Non-nil means automatically newline before and after braces
  155. inserted in Icon code.")
  156.  
  157. (defvar icon-tab-always-indent t
  158.   "*Non-nil means TAB in Icon mode should always reindent the current line,
  159. regardless of where in the line point is when the TAB command is used.")
  160.  
  161. (defvar icon-imenu-generic-expression
  162.       '((nil "^[ \t]*procedure[ \t]*\\(\\sw+\\)[ \t]*("  1))
  163.   "Imenu expression for Icon-mode.  See `imenu-generic-expression'.")
  164.  
  165.  
  166.  
  167. ;;;###autoload
  168. (defun icon-mode ()
  169.   "Major mode for editing Icon code.
  170. Expression and list commands understand all Icon brackets.
  171. Tab indents for Icon code.
  172. Paragraphs are separated by blank lines only.
  173. Delete converts tabs to spaces as it moves back.
  174. \\{icon-mode-map}
  175. Variables controlling indentation style:
  176.  icon-tab-always-indent
  177.     Non-nil means TAB in Icon mode should always reindent the current line,
  178.     regardless of where in the line point is when the TAB command is used.
  179.  icon-auto-newline
  180.     Non-nil means automatically newline before and after braces
  181.     inserted in Icon code.
  182.  icon-indent-level
  183.     Indentation of Icon statements within surrounding block.
  184.     The surrounding block's indentation is the indentation
  185.     of the line on which the open-brace appears.
  186.  icon-continued-statement-offset
  187.     Extra indentation given to a substatement, such as the
  188.     then-clause of an if or body of a while.
  189.  icon-continued-brace-offset
  190.     Extra indentation given to a brace that starts a substatement.
  191.     This is in addition to `icon-continued-statement-offset'.
  192.  icon-brace-offset
  193.     Extra indentation for line if it starts with an open brace.
  194.  icon-brace-imaginary-offset
  195.     An open brace following other text is treated as if it were
  196.     this far to the right of the start of its line.
  197.  
  198. Turning on Icon mode calls the value of the variable `icon-mode-hook'
  199. with no args, if that value is non-nil."
  200.   (interactive)
  201.   (kill-all-local-variables)
  202.   (use-local-map icon-mode-map)
  203.   (setq major-mode 'icon-mode)
  204.   (setq mode-name "Icon")
  205.   (setq local-abbrev-table icon-mode-abbrev-table)
  206.   (set-syntax-table icon-mode-syntax-table)
  207.   (make-local-variable 'paragraph-start)
  208.   (setq paragraph-start (concat "$\\|" page-delimiter))
  209.   (make-local-variable 'paragraph-separate)
  210.   (setq paragraph-separate paragraph-start)
  211.   (make-local-variable 'indent-line-function)
  212.   (setq indent-line-function 'icon-indent-line)
  213.   (make-local-variable 'require-final-newline)
  214.   (setq require-final-newline t)
  215.   (make-local-variable 'comment-start)
  216.   (setq comment-start "# ")
  217.   (make-local-variable 'comment-end)
  218.   (setq comment-end "")
  219.   (make-local-variable 'comment-column)
  220.   (setq comment-column 32)
  221.   (make-local-variable 'comment-start-skip)
  222.   (setq comment-start-skip "# *")
  223.   (make-local-variable 'comment-indent-function)
  224.   (setq comment-indent-function 'icon-comment-indent)
  225.   (make-local-variable 'font-lock-defaults)
  226.   (setq font-lock-defaults  
  227.     '((
  228.        icon-font-lock-keywords
  229.        icon-font-lock-keywords-1 
  230.        icon-font-lock-keywords-2
  231.        )
  232.       nil nil ((?_ . "w")) beginning-of-defun
  233.       (font-lock-comment-start-regexp . "#")
  234.       (font-lock-mark-block-function . mark-defun)))
  235.   (make-local-variable 'imenu-generic-expression)
  236.   (setq imenu-generic-expression icon-imenu-generic-expression)
  237.   (pushnew '(icon-mode  "procedure" "end" icon-forward-sexp-function)
  238.        hs-special-modes-alist :test 'equal)
  239. ;  (setq hs-block-start-regexp "procedure")
  240. ;  (setq hs-block-end-regexp "^end")
  241. ;  (setq hs-forward-sexp-func 'icon-forward-sexp-function)
  242.   (run-hooks 'icon-mode-hook))
  243.  
  244. ;; This is used by indent-for-comment to decide how much to
  245. ;; indent a comment in Icon code based on its context.
  246. (defun icon-comment-indent ()
  247.   (if (looking-at "^#")
  248.       0    
  249.     (save-excursion
  250.       (skip-chars-backward " \t")
  251.       (max (if (bolp) 0 (1+ (current-column)))
  252.        comment-column))))
  253.  
  254. (defun electric-icon-brace (arg)
  255.   "Insert character and correct line's indentation."
  256.   (interactive "P")
  257.   (let (insertpos)
  258.     (if (and (not arg)
  259.          (eolp)
  260.          (or (save-excursion
  261.            (skip-chars-backward " \t")
  262.            (bolp))
  263.          (if icon-auto-newline
  264.              (progn (icon-indent-line) (newline) t)
  265.            nil)))
  266.     (progn
  267.       (insert last-command-char)
  268.       (icon-indent-line)
  269.       (if icon-auto-newline
  270.           (progn
  271.         (newline)
  272.         ;; (newline) may have done auto-fill
  273.         (setq insertpos (- (point) 2))
  274.         (icon-indent-line)))
  275.       (save-excursion
  276.         (if insertpos (goto-char (1+ insertpos)))
  277.         (delete-char -1))))
  278.     (if insertpos
  279.     (save-excursion
  280.       (goto-char insertpos)
  281.       (self-insert-command (prefix-numeric-value arg)))
  282.       (self-insert-command (prefix-numeric-value arg)))))
  283.  
  284. (defun icon-indent-command (&optional whole-exp)
  285.   (interactive "P")
  286.   "Indent current line as Icon code, or in some cases insert a tab character.
  287. If `icon-tab-always-indent' is non-nil (the default), always indent current
  288. line.  Otherwise, indent the current line only if point is at the left margin
  289. or in the line's indentation; otherwise insert a tab.
  290.  
  291. A numeric argument, regardless of its value, means indent rigidly all the
  292. lines of the expression starting after point so that this line becomes
  293. properly indented.  The relative indentation among the lines of the
  294. expression are preserved."
  295.   (if whole-exp
  296.       ;; If arg, always indent this line as Icon
  297.       ;; and shift remaining lines of expression the same amount.
  298.       (let ((shift-amt (icon-indent-line))
  299.         beg end)
  300.     (save-excursion
  301.       (if icon-tab-always-indent
  302.           (beginning-of-line))
  303.       (setq beg (point))
  304.       (forward-sexp 1)
  305.       (setq end (point))
  306.       (goto-char beg)
  307.       (forward-line 1)
  308.       (setq beg (point)))
  309.     (if (> end beg)
  310.         (indent-code-rigidly beg end shift-amt "#")))
  311.     (if (and (not icon-tab-always-indent)
  312.          (save-excursion
  313.            (skip-chars-backward " \t")
  314.            (not (bolp))))
  315.     (insert-tab)
  316.       (icon-indent-line))))
  317.  
  318. (defun icon-indent-line ()
  319.   "Indent current line as Icon code.
  320. Return the amount the indentation changed by."
  321.   (let ((indent (calculate-icon-indent nil))
  322.     beg shift-amt
  323.     (case-fold-search nil)
  324.     (pos (- (point-max) (point))))
  325.     (beginning-of-line)
  326.     (setq beg (point))
  327.     (cond ((eq indent nil)
  328.        (setq indent (current-indentation)))
  329.       ((eq indent t)
  330.        (setq indent (calculate-icon-indent-within-comment)))
  331.       ((looking-at "[ \t]*#")
  332.        (setq indent 0))
  333.       (t
  334.        (skip-chars-forward " \t")
  335.        (if (listp indent) (setq indent (car indent)))
  336.        (cond ((and (looking-at "else\\b")
  337.                (not (looking-at "else\\s_")))
  338.           (setq indent (save-excursion
  339.                  (icon-backward-to-start-of-if)
  340.                  (current-indentation))))
  341.          ((or (= (following-char) ?})
  342.               (looking-at "end\\b"))
  343.           (setq indent (- indent icon-indent-level)))
  344.          ((= (following-char) ?{)
  345.           (setq indent (+ indent icon-brace-offset))))))
  346.     (skip-chars-forward " \t")
  347.     (setq shift-amt (- indent (current-column)))
  348.     (if (zerop shift-amt)
  349.     (if (> (- (point-max) pos) (point))
  350.         (goto-char (- (point-max) pos)))
  351.       (delete-region beg (point))
  352.       (indent-to indent)
  353.       ;; If initial point was within line's indentation,
  354.       ;; position after the indentation.  Else stay at same point in text.
  355.       (if (> (- (point-max) pos) (point))
  356.       (goto-char (- (point-max) pos))))
  357.     shift-amt))
  358.  
  359. (defun calculate-icon-indent (&optional parse-start)
  360.   "Return appropriate indentation for current line as Icon code.
  361. In usual case returns an integer: the column to indent to.
  362. Returns nil if line starts inside a string, t if in a comment."
  363.   (save-excursion
  364.     (beginning-of-line)
  365.     (let ((indent-point (point))
  366.       (case-fold-search nil)
  367.       state
  368.       containing-sexp
  369.       toplevel)
  370.       (if parse-start
  371.       (goto-char parse-start)
  372.     (setq toplevel (beginning-of-icon-defun)))
  373.       (while (< (point) indent-point)
  374.     (setq parse-start (point))
  375.     (setq state (parse-partial-sexp (point) indent-point 0))
  376.     (setq containing-sexp (car (cdr state))))
  377.       (cond ((or (nth 3 state) (nth 4 state))
  378.          ;; return nil or t if should not change this line
  379.          (nth 4 state))
  380.         ((and containing-sexp
  381.           (/= (char-after containing-sexp) ?{))
  382.          ;; line is expression, not statement:
  383.          ;; indent to just after the surrounding open.
  384.          (goto-char (1+ containing-sexp))
  385.          (current-column))
  386.         (t
  387.           (if toplevel
  388.           ;; Outside any procedures.
  389.           (progn (icon-backward-to-noncomment (point-min))
  390.              (if (icon-is-continuation-line)
  391.                  icon-continued-statement-offset 0))
  392.         ;; Statement level.
  393.         (if (null containing-sexp)
  394.             (progn (beginning-of-icon-defun)
  395.                (setq containing-sexp (point))))
  396.         (goto-char indent-point)
  397.         ;; Is it a continuation or a new statement?
  398.         ;; Find previous non-comment character.
  399.         (icon-backward-to-noncomment containing-sexp)
  400.         ;; Now we get the answer.
  401.         (if (icon-is-continuation-line)
  402.             ;; This line is continuation of preceding line's statement;
  403.             ;; indent  icon-continued-statement-offset  more than the
  404.             ;; first line of the statement.
  405.             (progn
  406.               (icon-backward-to-start-of-continued-exp containing-sexp)
  407.               (+ icon-continued-statement-offset (current-column)
  408.              (if (save-excursion (goto-char indent-point)
  409.                          (skip-chars-forward " \t")
  410.                          (eq (following-char) ?{))
  411.                  icon-continued-brace-offset 0)))
  412.           ;; This line starts a new statement.
  413.           ;; Position following last unclosed open.
  414.           (goto-char containing-sexp)
  415.           ;; Is line first statement after an open-brace?
  416.           (or
  417.             ;; If no, find that first statement and indent like it.
  418.             (save-excursion
  419.               (if (looking-at "procedure\\s ")
  420.               (forward-sexp 3)
  421.             (forward-char 1))
  422.               (while (progn (skip-chars-forward " \t\n")
  423.                     (looking-at "#"))
  424.             ;; Skip over comments following openbrace.
  425.             (forward-line 1))
  426.               ;; The first following code counts
  427.               ;; if it is before the line we want to indent.
  428.               (and (< (point) indent-point)
  429.                (current-column)))
  430.             ;; If no previous statement,
  431.             ;; indent it relative to line brace is on.
  432.             ;; For open brace in column zero, don't let statement
  433.             ;; start there too.  If icon-indent-level is zero,
  434.             ;; use icon-brace-offset + icon-continued-statement-offset
  435.             ;; instead.
  436.             ;; For open-braces not the first thing in a line,
  437.             ;; add in icon-brace-imaginary-offset.
  438.             (+ (if (and (bolp) (zerop icon-indent-level))
  439.                (+ icon-brace-offset
  440.                   icon-continued-statement-offset)
  441.              icon-indent-level)
  442.                ;; Move back over whitespace before the openbrace.
  443.                ;; If openbrace is not first nonwhite thing on the line,
  444.                ;; add the icon-brace-imaginary-offset.
  445.                (progn (skip-chars-backward " \t")
  446.                   (if (bolp) 0 icon-brace-imaginary-offset))
  447.                ;; Get initial indentation of the line we are on.
  448.                (current-indentation))))))))))
  449.  
  450. ;; List of words to check for as the last thing on a line.
  451. ;; If cdr is t, next line is a continuation of the same statement,
  452. ;; if cdr is nil, next line starts a new (possibly indented) statement.
  453.  
  454. (defconst icon-resword-alist
  455.   '(("by" . t) ("case" . t) ("create") ("do") ("dynamic" . t) ("else")
  456.     ("every" . t) ("if" . t) ("global" . t) ("initial" . t)
  457.     ("link" . t) ("local" . t) ("of") ("record" . t) ("repeat" . t)
  458.     ("static" . t) ("then") ("to" . t) ("until" . t) ("while" . t)))
  459.  
  460. (defun icon-is-continuation-line ()
  461.   (let* ((ch (preceding-char))
  462.      (ch-syntax (char-syntax ch)))
  463.     (if (eq ch-syntax ?w)
  464.     (assoc (buffer-substring
  465.         (progn (forward-word -1) (point))
  466.         (progn (forward-word 1) (point)))
  467.            icon-resword-alist)
  468.       (not (memq ch '(0 ?\; ?\} ?\{ ?\) ?\] ?\" ?\' ?\n))))))
  469.  
  470. (defun icon-backward-to-noncomment (lim)
  471.   (let (opoint stop)
  472.     (while (not stop)
  473.       (skip-chars-backward " \t\n\f" lim)
  474.       (setq opoint (point))
  475.       (beginning-of-line)
  476.       (if (and (nth 5 (parse-partial-sexp (point) opoint))
  477.            (< lim (point)))
  478.       (search-backward "#")
  479.     (setq stop t)))))
  480.  
  481. (defun icon-backward-to-start-of-continued-exp (lim)
  482.   (if (memq (preceding-char) '(?\) ?\]))
  483.       (forward-sexp -1))
  484.   (beginning-of-line)
  485.   (skip-chars-forward " \t")
  486.   (cond
  487.    ((<= (point) lim) (goto-char (1+ lim)))
  488.    ((not (icon-is-continued-line)) 0)
  489.    ((and (eq (char-syntax (following-char)) ?w)
  490.      (cdr
  491.       (assoc (buffer-substring (point)
  492.                    (save-excursion (forward-word 1) (point)))
  493.          icon-resword-alist))) 0)
  494.    (t (end-of-line 0) (icon-backward-to-start-of-continued-exp lim))))
  495.  
  496. (defun icon-is-continued-line ()
  497.   (save-excursion
  498.     (end-of-line 0)
  499.     (icon-is-continuation-line)))
  500.  
  501. (defun icon-backward-to-start-of-if (&optional limit)
  502.   "Move to the start of the last \"unbalanced\" if."
  503.   (or limit (setq limit (save-excursion (beginning-of-icon-defun) (point))))
  504.   (let ((if-level 1)
  505.     (case-fold-search nil))
  506.     (while (not (zerop if-level))
  507.       (backward-sexp 1)
  508.       (cond ((looking-at "else\\b")
  509.          (setq if-level (1+ if-level)))
  510.         ((looking-at "if\\b")
  511.          (setq if-level (1- if-level)))
  512.         ((< (point) limit)
  513.          (setq if-level 0)
  514.          (goto-char limit))))))
  515.  
  516. (defun mark-icon-function ()
  517.   "Put mark at end of Icon function, point at beginning."
  518.   (interactive)
  519.   (push-mark (point))
  520.   (end-of-icon-defun)
  521.   (push-mark (point))
  522.   (beginning-of-line 0)
  523.   (beginning-of-icon-defun))
  524.  
  525. (defun beginning-of-icon-defun ()
  526.   "Go to the start of the enclosing procedure; return t if at top level."
  527.   (interactive)
  528.   (if (re-search-backward "^procedure\\s \\|^end[ \t\n]" (point-min) 'move)
  529.       (looking-at "e")
  530.     t))
  531.  
  532. (defun end-of-icon-defun ()
  533.   (interactive)
  534.   (if (not (bobp)) (forward-char -1))
  535.   (re-search-forward "\\(\\s \\|^\\)end\\(\\s \\|$\\)" (point-max) 'move)
  536.   (forward-word -1)
  537.   (forward-line 1))
  538.  
  539. (defun indent-icon-exp ()
  540.   "Indent each line of the Icon grouping following point."
  541.   (interactive)
  542.   (let ((indent-stack (list nil))
  543.     (contain-stack (list (point)))
  544.     (case-fold-search nil)
  545.     restart outer-loop-done inner-loop-done state ostate
  546.     this-indent last-sexp
  547.     at-else at-brace at-do
  548.     (opoint (point))
  549.     (next-depth 0))
  550.     (save-excursion
  551.       (forward-sexp 1))
  552.     (save-excursion
  553.       (setq outer-loop-done nil)
  554.       (while (and (not (eobp)) (not outer-loop-done))
  555.     (setq last-depth next-depth)
  556.     ;; Compute how depth changes over this line
  557.     ;; plus enough other lines to get to one that
  558.     ;; does not end inside a comment or string.
  559.     ;; Meanwhile, do appropriate indentation on comment lines.
  560.     (setq innerloop-done nil)
  561.     (while (and (not innerloop-done)
  562.             (not (and (eobp) (setq outer-loop-done t))))
  563.       (setq ostate state)
  564.       (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
  565.                       nil nil state))
  566.       (setq next-depth (car state))
  567.       (if (and (car (cdr (cdr state)))
  568.            (>= (car (cdr (cdr state))) 0))
  569.           (setq last-sexp (car (cdr (cdr state)))))
  570.       (if (or (nth 4 ostate))
  571.           (icon-indent-line))
  572.       (if (or (nth 3 state))
  573.           (forward-line 1)
  574.         (setq innerloop-done t)))
  575.     (if (<= next-depth 0)
  576.         (setq outer-loop-done t))
  577.     (if outer-loop-done
  578.         nil
  579.       (if (/= last-depth next-depth)
  580.           (setq last-sexp nil))
  581.       (while (> last-depth next-depth)
  582.         (setq indent-stack (cdr indent-stack)
  583.           contain-stack (cdr contain-stack)
  584.           last-depth (1- last-depth)))
  585.       (while (< last-depth next-depth)
  586.         (setq indent-stack (cons nil indent-stack)
  587.           contain-stack (cons nil contain-stack)
  588.           last-depth (1+ last-depth)))
  589.       (if (null (car contain-stack))
  590.           (setcar contain-stack (or (car (cdr state))
  591.                     (save-excursion (forward-sexp -1)
  592.                             (point)))))
  593.       (forward-line 1)
  594.       (skip-chars-forward " \t")
  595.       (if (eolp)
  596.           nil
  597.         (if (and (car indent-stack)
  598.              (>= (car indent-stack) 0))
  599.         ;; Line is on an existing nesting level.
  600.         ;; Lines inside parens are handled specially.
  601.         (if (/= (char-after (car contain-stack)) ?{)
  602.             (setq this-indent (car indent-stack))
  603.           ;; Line is at statement level.
  604.           ;; Is it a new statement?  Is it an else?
  605.           ;; Find last non-comment character before this line
  606.           (save-excursion
  607.             (setq at-else (looking-at "else\\W"))
  608.             (setq at-brace (= (following-char) ?{))
  609.             (icon-backward-to-noncomment opoint)
  610.             (if (icon-is-continuation-line)
  611.             ;; Preceding line did not end in comma or semi;
  612.             ;; indent this line  icon-continued-statement-offset
  613.             ;; more than previous.
  614.             (progn
  615.               (icon-backward-to-start-of-continued-exp (car contain-stack))
  616.               (setq this-indent
  617.                 (+ icon-continued-statement-offset (current-column)
  618.                    (if at-brace icon-continued-brace-offset 0))))
  619.               ;; Preceding line ended in comma or semi;
  620.               ;; use the standard indent for this level.
  621.               (if at-else
  622.               (progn (icon-backward-to-start-of-if opoint)
  623.                  (setq this-indent (current-indentation)))
  624.             (setq this-indent (car indent-stack))))))
  625.           ;; Just started a new nesting level.
  626.           ;; Compute the standard indent for this level.
  627.           (let ((val (calculate-icon-indent
  628.                (if (car indent-stack)
  629.                    (- (car indent-stack))))))
  630.         (setcar indent-stack
  631.             (setq this-indent val))))
  632.         ;; Adjust line indentation according to its contents
  633.         (if (or (= (following-char) ?})
  634.             (looking-at "end\\b"))
  635.         (setq this-indent (- this-indent icon-indent-level)))
  636.         (if (= (following-char) ?{)
  637.         (setq this-indent (+ this-indent icon-brace-offset)))
  638.         ;; Put chosen indentation into effect.
  639.         (or (= (current-column) this-indent)
  640.         (progn
  641.           (delete-region (point) (progn (beginning-of-line) (point)))
  642.           (indent-to this-indent)))
  643.         ;; Indent any comment following the text.
  644.         (or (looking-at comment-start-skip)
  645.         (if (re-search-forward comment-start-skip (save-excursion (end-of-line) (point)) t)
  646.             (progn (indent-for-comment) (beginning-of-line))))))))))
  647.  
  648. ;;"break" "do" "next" "repeat" "to" "by" "else" "if" "not" "return" "until" "case" "of" "static" "while" "create" "every" "suspend" "default" "fail" "record" "then" "?"
  649. (defconst icon-keywords "?\\|b\\(reak\\|y\\)\\|c\\(ase\\|reate\\)\\|d\\(efault\\|o\\)\\|e\\(lse\\|very\\)\\|fail\\|if\\|n\\(ext\\|ot\\)\\|of\\|re\\(cord\\|peat\\|turn\\)\\|s\\(tatic\\|uspend\\)\\|t\\(hen\\|o\\)\\|until\\|while")
  650.  
  651. ;; "end" "global" "initial" "local" "link" "procedure" "$include" "$define"
  652. (defconst icon-builtin "$\\(define\\|include\\)\\|end\\|global\\|initial\\|l\\(ink\\|ocal\\)\\|procedure")
  653.  
  654. ;;"null" "string" "co-expression" "table" "integer" "cset"  "set" "real" "file" "list"
  655. (defconst icon-type-types "c\\(o-expression\\|set\\)\\|file\\|integer\\|list\\|null\\|real\\|s\\(et\\|tring\\)\\|table")
  656.  
  657. ;;"&allocated" "&ascii" "&clock" "&col" "&collections" "&column" "&control" "&cset" "¤t" "&date" "&dateline" "&digits" "&dump" "&error" "&errornumber" "&errortext" "&errorvalue" "&errout" "&eventcode" "&eventsource" "&eventvalue" "&fail" "&features"
  658.  "&file" "&host" "&input" "&interval" "&lcase" "&ldrag" "&letters" "&level" "&line" "&lpress" "&lrelease" "&main" "&mdrag" "&meta" "&mpress" "&mrelease" "&null" "&output" "&phi" "&pi" "&pos" "&progname" "&random" "&rdrag" "®ions" "&resize" "&row" "&rpr
  659. ess" "&rrelease" "&shift" "&source" "&storage" "&subject" "&time" "&trace" "&ucase" "&version" "&window" "&x" "&y"
  660. (defconst icon-functions "&\\(a\\(llocated\\|scii\\)\\|c\\(lock\\|o\\(l\\(\\|lections\\|umn\\)\\|ntrol\\)\\|set\\|urrent\\)\\|d\\(ate\\(\\|line\\)\\|igits\\|ump\\)\\|e\\(rro\\(r\\(\\|number\\|text\\|value\\)\\|ut\\)\\|vent\\(code\\|source\\|value\\)\\)\\|
  661. f\\(ail\\|eatures\\|ile\\)\\|host\\|in\\(put\\|terval\\)\\|l\\(case\\|drag\\|e\\(tters\\|vel\\)\\|ine\\|press\\|release\\)\\|m\\(ain\\|drag\\|eta\\|press\\|release\\)\\|null\\|output\\|p\\(hi\\|i\\|os\\|rogname\\)\\|r\\(andom\\|drag\\|e\\(gions\\|size\\)\
  662. \|ow\\|press\\|release\\)\\|s\\(hift\\|ource\\|torage\\|ubject\\)\\|t\\(ime\\|race\\)\\|ucase\\|version\\|window\\|[exy]\\)")
  663.  
  664. (defvar icon-font-lock-keywords-1 nil)
  665. (defvar icon-font-lock-keywords icon-font-lock-keywords-1)
  666.  
  667. (setq icon-font-lock-keywords-1
  668.   (list
  669.    ;; Fontify function name definitions.
  670.    (list (concat "^[ \t]*procedure[ \t]*\\(\\sw+\\)[ \t]*(") 
  671.      1 'font-lock-function-name-face)
  672.    ))
  673.  
  674. (defvar icon-font-lock-keywords-2 nil)
  675. (setq icon-font-lock-keywords-2
  676.   (append icon-font-lock-keywords-1
  677.    (list
  678.     ;; Fontify all type specifiers.
  679.     (cons (concat "\\<\\(" icon-type-types "\\)\\>") 'font-lock-type-face)
  680.     ;;
  681.     ;; Fontify all builtin keywords.
  682.     (cons (concat "\\(" icon-keywords "\\)") 'font-lock-keyword-face)
  683.     (cons (concat "\\(" icon-builtin "\\)") 'font-lock-type-face)
  684.     (cons (concat "\\(" icon-functions "\\)") 'font-lock-reference-face)
  685.  
  686.     (list "^[ \t]*\\(global\\|local\\)[ \t]+\\(\\sw+\\)+\\([ \t]*,[ \t]*\\(\\sw+\\)\\)"  2 'font-lock-variable-name-face)
  687.     )))
  688.  
  689. (defun icon-forward-sexp-function (arg)
  690.   (if (> arg 0)
  691.       (re-search-forward "^[ \t]*end")
  692.     (re-search-backward "^[ \t]procedure")))
  693.  
  694.  
  695. ;;; icon.el ends here
  696.  
  697.  
  698.  
  699.