home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / modes / occam-mode.el < prev    next >
Encoding:
Text File  |  1992-04-20  |  6.4 KB  |  198 lines

  1. ;; OCCAM mode for GNU emacs at DIKU
  2. ;; by Jesper Larsson Traff, DIKU autumn 1989.
  3. ;; Copyright (C) Jesper Larsson Traff and DIKU
  4.  
  5. ;; LCD Archive Entry:
  6. ;; occam-mode|Jesper Larsson Traff||
  7. ;; OCCAM programing mode.|
  8. ;; 89-09||~/modes/occam-mode.el.Z|
  9.  
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY.  No author or distributor
  12. ;; accepts responsibility to anyone for the consequences of using it
  13. ;; or for whether it serves any particular purpose or works at all,
  14. ;; unless he says so in writing.  Refer to the GNU Emacs General Public
  15. ;; License for full details.
  16.  
  17. ;; Everyone is granted permission to copy, modify and redistribute
  18. ;; GNU Emacs, but only under the conditions described in the
  19. ;; GNU Emacs General Public License.   A copy of this license is
  20. ;; supposed to have been given to you along with GNU Emacs so you
  21. ;; can know your rights and responsibilities.  It should be in a
  22. ;; file named COPYING.  Among other things, the copyright notice
  23. ;; and this notice must be preserved on all copies.
  24.  
  25. (defconst occam-indent 2 
  26.   "*OCCAM standard indentation (do not change!)")
  27.  
  28. (defconst occam-process-keywords
  29.   '("SEQ"                         ; sequential process
  30.     "PAR"                         ; parallel process
  31.     "IF"                          ; conditional process
  32.     "ALT"                         ; alternative (special) process
  33.     "WHILE"                       ; iterative process
  34.     "CASE"                        ; selection process
  35.     "ELSE"                        ; default process in selection
  36.     "VALOF"
  37.     "PROC"
  38.     "PROCESSOR"
  39.     "PLACED"
  40.     "PRI"
  41.     )
  42.   "*OCCAM proccess keywords")
  43.  
  44. (defconst occam-reserved-words
  45.   '("INT" "INT16""INT32" "INT64"  ; integer declarations
  46.     "REAL" "REAL32" "REAL64"      ; real declarations
  47.     "BYTE"                        ; byte (character) declaration
  48.     "BOOL" "TRUE" "FALSE"         ; boolean declaration and constants
  49.     "CHAN"                        ; channel declaration
  50.     "OF"
  51.     "PROTOCOL"                    ; protocol declaration
  52.     "TIMER"                       ; timer declaration
  53.     "VAL"
  54.     "IS"
  55.     "RESULT"
  56.     "FOR"                         ; replicator keyword
  57.     "AT"
  58.     "SIZE"                        ; size operator
  59.     "FROM"                        ; array selector keyword
  60.     "SKIP" "STOP"                 ; special processes
  61.     )
  62.   "*OCCAM reserved words (will be capitalized)")
  63.  
  64. (defvar occam-mode-syntax-table nil
  65.   "Syntax table in use in OCCAM mode buffers")
  66. (if occam-mode-syntax-table
  67.     ()
  68.   (setq occam-mode-syntax-table (make-syntax-table))
  69.   (modify-syntax-entry ?. "w" occam-mode-syntax-table)
  70.   (modify-syntax-entry ?+ "." occam-mode-syntax-table)
  71.   (modify-syntax-entry ?- "." occam-mode-syntax-table)
  72.   (modify-syntax-entry ?* "." occam-mode-syntax-table)
  73.   (modify-syntax-entry ?/ "." occam-mode-syntax-table)
  74.   (modify-syntax-entry ?\: "." occam-mode-syntax-table)
  75.   (modify-syntax-entry ?\? "." occam-mode-syntax-table)
  76.   (modify-syntax-entry ?\! "." occam-mode-syntax-table)
  77.   (modify-syntax-entry ?\r ">" occam-mode-syntax-table)
  78.   (modify-syntax-entry ?- ".12" occam-mode-syntax-table))
  79.  
  80. (defvar occam-mode-map ()
  81.   "Keymap used in OCCAM mode")
  82. (if occam-mode-map
  83.     ()
  84.   (setq occam-mode-map (make-sparse-keymap))
  85.   (define-key occam-mode-map " " 'uppercase-occam-keyword)
  86.   (define-key occam-mode-map "\r" 'occam-indent-newline)
  87.   (define-key occam-mode-map "\177" 'backward-delete-unindent))
  88.  
  89. (defun occam-mode ()
  90.   "Major mode for editing OCCAM programs.
  91. TAB and CR automatically indents.
  92. All OCCAM keywords (which are separated into process keywords which force 
  93. indentation and reserved words) are recognized and uppercase'd.
  94.  
  95. Variables and constants controlling case change:
  96.     occam-indentation :      indentation, default 2
  97.     occam-process-keywords : list of process keywords
  98.     occam-reserved-words :   list of reserved words
  99.  
  100. The value of the variable occam-mode-hook (must be a function name) is called 
  101. with no arguments prior to entering  OCCAM mode if the value of that variable
  102. is non-nil"
  103.   (interactive)
  104.   (kill-all-local-variables)
  105.   (use-local-map occam-mode-map)
  106.   (make-local-variable 'indent-line-function)
  107.   (setq indent-line-function 'occam-indent-line)
  108.   (setq mode-name "OCCAM")
  109.   (setq major-mode 'occam-mode)
  110.   (run-hooks 'occam-mode-hook))
  111.  
  112. (defun occam-indent-line ()
  113.   "Indents current OCCAM line"
  114.   (interactive)
  115.   (save-excursion
  116.     (beginning-of-line)
  117.     (let ((p (point)))
  118.       (indent-to (calculate-occam-indent))
  119.       (while (looking-at "[ \t]")
  120.     (delete-char 1))
  121.       (untabify p (point)))))
  122.  
  123. (defun calculate-occam-indent ()
  124.   "calculate indentation for current OCCAM line"
  125.   (interactive)
  126.   (save-excursion
  127.     (beginning-of-line 0)
  128.     (while (and (looking-at "[ \t]*$") (not (bobp)))
  129.       (beginning-of-line 0))
  130.     (if (looking-at "[ \t]*$")
  131.     0
  132.       (progn
  133.     (skip-chars-forward " \t")
  134.     (let ((col (current-column)))
  135.       (forward-word 1)
  136.       (if (occam-keyword occam-process-keywords)
  137.           (+ col occam-indent)
  138.         col))))))
  139.  
  140. (defun uppercase-occam-keyword ()
  141.   "check if last word was an OCCAM keyword"
  142.   (interactive)
  143.   (occam-keyword (append occam-process-keywords occam-reserved-words))
  144.   (insert " "))
  145.  
  146. (defun occam-indent-newline ()
  147.   "Indent new line to current indentation unless previous line contained 
  148. a process keyword"
  149.   (interactive)
  150.   (save-excursion
  151.     (let ((eol (point)))
  152.       (beginning-of-line)
  153.       (if (looking-at "[ \t]*$")
  154.       (delete-region (point) eol)
  155.     ())))
  156.   (occam-keyword (append occam-process-keywords occam-reserved-words))
  157.   (newline)
  158.   (occam-indent-line)
  159.   (end-of-line))
  160.  
  161. (defun backward-delete-unindent ()
  162.   "Delete and unindent"
  163.   (interactive)
  164.   (let ((p (point)))
  165.     (skip-chars-backward " \t" (- p (current-column)))
  166.     (if (bolp)
  167.     (progn
  168.       (goto-char p)
  169.       (if (bolp)
  170.           (delete-char -1)
  171.         (delete-char (- occam-indent))))
  172.       (progn
  173.     (goto-char p)
  174.     (delete-char -1)))))
  175.  
  176. (defun occam-keyword (keywords)
  177.   "upcase current word and if OCCAM keyword, t if keyword"
  178.   (save-excursion
  179.     (let ((eow (point)))
  180.       (forward-word -1)
  181.       (let ((bow (point)))
  182.     (if (re-search-backward "\s<" (- bow (current-column)) t)
  183.         nil
  184.       (if (word-in-list (upcase (buffer-substring bow eow))
  185.                 keywords)
  186.           (not (upcase-region bow eow))
  187.         nil))))))
  188.  
  189. (defun word-in-list (word words)
  190.   "t if word occurs in words, nil otherwise"
  191.   (if (null words)
  192.       nil
  193.     (if (string-equal word (car words))
  194.     t
  195.       (word-in-list word (cdr words)))))
  196.  
  197.  
  198.