File:  [gforth] / gforth / gforth.el
Revision 1.53: download - view: text, annotated - select for diffs
Tue Jun 26 19:51:49 2001 UTC (22 years, 9 months ago) by dvdkhlng
Branches: MAIN
CVS tags: HEAD
Fixed some old bug in etags.fs and refined tag search in gforth.el. See my
not in etags.fs for details.

    1: ;;; gforth.el --- major mode for editing (G)Forth sources
    2: 
    3: ;; Copyright (C) 1995,1996,1997,1998,2000 Free Software Foundation, Inc.
    4: 
    5: ;; This file is part of Gforth.
    6: 
    7: ;; GForth is distributed in the hope that it will be useful,
    8: ;; but WITHOUT ANY WARRANTY.  No author or distributor
    9: ;; accepts responsibility to anyone for the consequences of using it
   10: ;; or for whether it serves any particular purpose or works at all,
   11: ;; unless he says so in writing.  Refer to the GNU Emacs General Public
   12: ;; License for full details.
   13: 
   14: ;; Everyone is granted permission to copy, modify and redistribute
   15: ;; GNU Emacs, but only under the conditions described in the
   16: ;; GNU Emacs General Public License.   A copy of this license is
   17: ;; supposed to have been given to you along with Gforth so you
   18: ;; can know your rights and responsibilities.  It should be in a
   19: ;; file named COPYING.  Among other things, the copyright notice
   20: ;; and this notice must be preserved on all copies.
   21: 
   22: ;; Author: Goran Rydqvist <gorry@ida.liu.se>
   23: ;; Maintainer: David Kühling <dvdkhlng@gmx.de>
   24: ;; Created: 16 July 88 by Goran Rydqvist
   25: ;; Keywords: forth, gforth
   26: 
   27: ;; Changes by anton
   28: ;; This is a variant of forth.el that came with TILE.
   29: ;; I left most of this stuff untouched and made just a few changes for 
   30: ;; the things I use (mainly indentation and syntax tables).
   31: ;; So there is still a lot of work to do to adapt this to gforth.
   32: 
   33: ;; Changes by David
   34: ;; Added a syntax-hilighting engine, rewrote auto-indentation engine.
   35: ;; Added support for block files.
   36:  
   37: ;;-------------------------------------------------------------------
   38: ;; A Forth indentation, documentation search and interaction library
   39: ;;-------------------------------------------------------------------
   40: ;;
   41: ;; Written by Goran Rydqvist, gorry@ida.liu.se, Summer 1988
   42: ;; Started:	16 July 88
   43: ;; Version:	2.10
   44: ;; Last update:	5 December 1989 by Mikael Patel, mip@ida.liu.se
   45: ;; Last update:	25 June 1990 by Goran Rydqvist, gorry@ida.liu.se
   46: ;;
   47: ;; Documentation: See forth-mode (^HF forth-mode)
   48: ;;-------------------------------------------------------------------
   49: 
   50: ;;; Code:
   51: 
   52:  
   53: 
   54: ;;; Hilighting and indentation engine				(dk)
   55: ;;;
   56: 
   57: (defvar forth-words nil 
   58:   "List of words for hilighting and recognition of parsed text areas. 
   59: You can enable hilighting of object-oriented Forth code, by appending either
   60: `forth-objects-words' or `forth-oof-words' to the list, depending on which
   61: OOP package you're using. After `forth-words' changed, `forth-compile-words' 
   62: must be called to make the changes take effect.
   63: 
   64: Each item of `forth-words' has the form 
   65:    (MATCHER TYPE HILIGHT . &optional PARSED-TEXT ...)
   66: 
   67: MATCHER is either a list of strings to match, or a REGEXP.
   68:    If it's a REGEXP, it should not be surrounded by '\\<' or '\\>', since 
   69:    that'll be done automatically by the search routines.
   70: 
   71: TYPE should be one of 'definiton-starter', 'definition-ender', 'compile-only',
   72:    'immediate' or 'non-immediate'. Those information are required to determine
   73:    whether a word actually parses (and whether that parsed text needs to be
   74:    hilighted).
   75: 
   76: HILIGHT is a cons cell of the form (FACE . MINIMUM-LEVEL)
   77:    Where MINIMUM-LEVEL specifies the minimum value of `forth-hilight-level',
   78:    that's required for matching text to be hilighted.
   79: 
   80: PARSED-TEXT specifies whether and how a word parses following text. You can
   81:    specify as many subsequent PARSED-TEXT as you wish, but that shouldn't be
   82:    necessary very often. It has the following form:
   83:    (DELIM-REGEXP SKIP-LEADING-FLAG PARSED-TYPE HILIGHT)
   84: 
   85: DELIM-REGEXP is a regular expression that should match strings of length 1,
   86:    which are delimiters for the parsed text.
   87: 
   88: A non-nil value for PARSE-LEADING-FLAG means, that leading delimiter strings
   89:    before parsed text should be skipped. This is the parsing behaviour of the
   90:    Forth word WORD. Set it to t for name-parsing words, nil for comments and
   91:    strings.
   92: 
   93: PARSED-TYPE specifies what kind of text is parsed. It should be on of 'name',
   94:    'string' or 'comment'.")
   95: (setq forth-words
   96:       '(
   97: 	(("[") definition-ender (font-lock-keyword-face . 1))
   98: 	(("]" "]l") definition-starter (font-lock-keyword-face . 1))
   99: 	((":") definition-starter (font-lock-keyword-face . 1)
  100: 	 "[ \t\n]" t name (font-lock-function-name-face . 3))
  101: 	(("immediate" "compile-only" "restrict")
  102: 	 immediate (font-lock-keyword-face . 1))
  103: 	(("does>") compile-only (font-lock-keyword-face . 1))
  104: 	((":noname") definition-starter (font-lock-keyword-face . 1))
  105: 	((";" ";code") definition-ender (font-lock-keyword-face . 1))
  106: 	(("include" "require" "needs" "use") 
  107: 	 non-immediate (font-lock-keyword-face . 1) 
  108: 	 "[\n\t ]" t string (font-lock-string-face . 1))
  109: 	(("included" "required" "thru" "load")
  110: 	 non-immediate (font-lock-keyword-face . 1))
  111: 	(("[char]") compile-only (font-lock-keyword-face . 1)
  112: 	 "[ \t\n]" t string (font-lock-string-face . 1))
  113: 	(("char") non-immediate (font-lock-keyword-face . 1)
  114: 	 "[ \t\n]" t string (font-lock-string-face . 1))
  115: 	(("s\"" "c\"") immediate (font-lock-string-face . 1)
  116: 	 "[\"\n]" nil string (font-lock-string-face . 1))
  117: 	((".\"") compile-only (font-lock-string-face . 1)
  118: 	 "[\"\n]" nil string (font-lock-string-face . 1))
  119: 	(("abort\"") compile-only (font-lock-keyword-face . 1)
  120: 	 "[\"\n]" nil string (font-lock-string-face . 1))
  121: 	(("{") compile-only (font-lock-variable-name-face . 1)
  122: 	 "[\n}]" nil name (font-lock-variable-name-face . 1))
  123: 	((".(" "(") immediate (font-lock-comment-face . 1)
  124: 	  ")" nil comment (font-lock-comment-face . 1))
  125: 	(("\\" "\\G") immediate (font-lock-comment-face . 1)
  126: 	 "[\n]" nil comment (font-lock-comment-face . 1))
  127: 	  
  128: 	(("[if]" "[?do]" "[do]" "[for]" "[begin]" 
  129: 	  "[endif]" "[then]" "[loop]" "[+loop]" "[next]" "[until]" "[repeat]"
  130: 	  "[again]" "[while]" "[else]")
  131: 	 immediate (font-lock-keyword-face . 2))
  132: 	(("[ifdef]" "[ifundef]") immediate (font-lock-keyword-face . 2)
  133: 	 "[ \t\n]" t name (font-lock-function-name-face . 3))
  134: 	(("if" "begin" "ahead" "do" "?do" "+do" "u+do" "-do" "u-do" "for" 
  135: 	  "case" "of" "?dup-if" "?dup-0=-if" "then" "until" "repeat" "again" 
  136: 	  "loop" "+loop" "-loop" "next" "endcase" "endof" "else" "while" "try"
  137: 	  "recover" "endtry" "assert(" "assert0(" "assert1(" "assert2(" 
  138: 	  "assert3(" ")" "<interpretation" "<compilation" "interpretation>" 
  139: 	  "compilation>")
  140: 	 compile-only (font-lock-keyword-face . 2))
  141: 
  142: 	(("true" "false" "c/l" "bl" "cell" "pi" "w/o" "r/o" "r/w") 
  143: 	 non-immediate (font-lock-constant-face . 2))
  144: 	(("~~") compile-only (font-lock-warning-face . 2))
  145: 	(("postpone" "[is]" "defers" "[']" "[compile]") 
  146: 	 compile-only (font-lock-keyword-face . 2)
  147: 	 "[ \t\n]" t name (font-lock-function-name-face . 3))
  148: 	(("is" "what's") immediate (font-lock-keyword-face . 2)
  149: 	 "[ \t\n]" t name (font-lock-function-name-face . 3))
  150: 	(("<is>" "'") non-immediate (font-lock-keyword-face . 2)
  151: 	 "[ \t\n]" t name (font-lock-function-name-face . 3))
  152: 	(("[to]") compile-only (font-lock-keyword-face . 2)
  153: 	 "[ \t\n]" t name (font-lock-variable-name-face . 3))
  154: 	(("to") immediate (font-lock-keyword-face . 2)
  155: 	 "[ \t\n]" t name (font-lock-variable-name-face . 3))
  156: 	(("<to>") non-immediate (font-lock-keyword-face . 2)
  157: 	 "[ \t\n]" t name (font-lock-variable-name-face . 3))
  158: 
  159: 	(("create" "variable" "constant" "2variable" "2constant" "fvariable"
  160: 	  "fconstant" "value" "field" "user" "vocabulary" 
  161: 	  "create-interpret/compile")
  162: 	 non-immediate (font-lock-type-face . 2)
  163: 	 "[ \t\n]" t name (font-lock-variable-name-face . 3))
  164: 	(("defer" "alias" "create-interpret/compile:") 
  165: 	 non-immediate (font-lock-type-face . 1)
  166: 	 "[ \t\n]" t name (font-lock-function-name-face . 3))
  167: 	(("end-struct") non-immediate (font-lock-keyword-face . 2)
  168: 	 "[ \t\n]" t name (font-lock-type-face . 3))
  169: 	(("struct") non-immediate (font-lock-keyword-face . 2))
  170: 	("-?[0-9]+\\(\\.[0-9]*e\\(-?[0-9]+\\)?\\|\\.?[0-9a-f]*\\)" 
  171: 	 immediate (font-lock-constant-face . 3))
  172: 	))
  173: 
  174: (defvar forth-use-objects nil 
  175:   "*Non-nil makes forth-mode also hilight words from the \"Objects\" package.")
  176: (defvar forth-objects-words nil
  177:   "Hilighting description for words of the \"Objects\" package")
  178: (setq forth-objects-words 
  179:       '(((":m") definition-starter (font-lock-keyword-face . 1)
  180: 	 "[ \t\n]" t name (font-lock-function-name-face . 3))
  181: 	(("m:") definition-starter (font-lock-keyword-face . 1))
  182: 	((";m") definition-ender (font-lock-keyword-face . 1))
  183: 	(("[current]" "[parent]") compile-only (font-lock-keyword-face . 1)
  184: 	 "[ \t\n]" t name (font-lock-function-name-face . 3))
  185: 	(("current" "overrides") non-immediate (font-lock-keyword-face . 2)
  186: 	 "[ \t\n]" t name (font-lock-function-name-face . 3))
  187: 	(("[to-inst]") compile-only (font-lock-keyword-face . 2)
  188: 	 "[ \t\n]" t name (font-lock-variable-name-face . 3))
  189: 	(("[bind]") compile-only (font-lock-keyword-face . 2)
  190: 	 "[ \t\n]" t name (font-lock-type-face . 3)
  191: 	 "[ \t\n]" t name (font-lock-function-name-face . 3))
  192: 	(("bind") non-immediate (font-lock-keyword-face . 2)
  193: 	 "[ \t\n]" t name (font-lock-type-face . 3)
  194: 	 "[ \t\n]" t name (font-lock-function-name-face . 3))
  195: 	(("inst-var" "inst-value") non-immediate (font-lock-type-face . 2)
  196: 	 "[ \t\n]" t name (font-lock-variable-name-face . 3))
  197: 	(("method" "selector")
  198: 	 non-immediate (font-lock-type-face . 1)
  199: 	 "[ \t\n]" t name (font-lock-function-name-face . 3))
  200: 	(("end-class" "end-interface")
  201: 	 non-immediate (font-lock-keyword-face . 2)
  202: 	 "[ \t\n]" t name (font-lock-type-face . 3))
  203: 	(("public" "protected" "class" "exitm" "implementation" "interface"
  204: 	  "methods" "end-methods" "this") 
  205: 	 non-immediate (font-lock-keyword-face . 2))
  206: 	(("object") non-immediate (font-lock-type-face . 2))))
  207: 
  208: (defvar forth-use-oof nil 
  209:   "*Non-nil makes forth-mode also hilight words from the \"OOF\" package.")
  210: (defvar forth-oof-words nil
  211:   "Hilighting description for words of the \"OOF\" package")
  212: (setq forth-oof-words 
  213:       '((("class") non-immediate (font-lock-keyword-face . 2)
  214: 	 "[ \t\n]" t name (font-lock-type-face . 3))
  215: 	(("var") non-immediate (font-lock-type-face . 2)
  216: 	 "[ \t\n]" t name (font-lock-variable-name-face . 3))
  217: 	(("method") non-immediate (font-lock-type-face . 2)
  218: 	 "[ \t\n]" t name (font-lock-function-name-face . 3))
  219: 	(("::" "super" "bind" "bound" "link") 
  220: 	 immediate (font-lock-keyword-face . 2)
  221: 	 "[ \t\n]" t name (font-lock-function-name-face . 3))
  222: 	(("ptr" "asptr" "[]") 
  223: 	 immediate (font-lock-keyword-face . 2)
  224: 	 "[ \t\n]" t name (font-lock-variable-name-face . 3))
  225: 	(("class;" "how:" "self" "new" "new[]" "definitions" "class?" "with"
  226: 	  "endwith")
  227: 	 non-immediate (font-lock-keyword-face . 2))
  228: 	(("object") non-immediate (font-lock-type-face . 2))))
  229: 
  230: (defvar forth-local-words nil 
  231:   "List of Forth words to prepend to `forth-words'. Should be set by a 
  232:  forth source, using a local variables list at the end of the file 
  233:  (\"Local Variables: ... forth-local-words: ... End:\" construct).") 
  234: 
  235: (defvar forth-custom-words nil
  236:   "List of Forth words to prepend to `forth-words'. Should be set in your
  237:  .emacs.")
  238: 
  239: (defvar forth-hilight-level 3 "*Level of hilighting of Forth code.")
  240: 
  241: (defvar forth-compiled-words nil "Compiled representation of `forth-words'.")
  242: 
  243: 
  244: ; todo:
  245: ;
  246: 
  247: ; Wörter ordentlich hilighten, die nicht auf whitespace beginning ( ..)IF
  248: ; Additional `forth-use-objects' or
  249: ; `forth-use-oof' could be set to non-nil for automatical adding of those
  250: ; word-lists. Using local variable list?
  251: ;
  252: ; Konfiguration über customization groups
  253: ;
  254: ; Bereich nur auf Wortanfang/ende ausweiten, wenn anfang bzw ende in einem 
  255: ; Wort liegen (?) -- speed!
  256: ;
  257: ; User interface
  258: ;
  259: ; 'forth-word' property muss eindeutig sein!
  260: ;
  261: ; imenu support schlauer machen
  262: 
  263: (setq debug-on-error t)
  264: 
  265: ;; Filter list by predicate. This is a somewhat standard function for 
  266: ;; functional programming languages. So why isn't it already implemented 
  267: ;; in Lisp??
  268: (defun forth-filter (predicate list)
  269:   (let ((filtered nil))
  270:     (mapcar (lambda (item)
  271: 	      (when (funcall predicate item)
  272: 		(if filtered
  273: 		    (nconc filtered (list item))
  274: 		  (setq filtered (cons item nil))))
  275: 	      nil) list)
  276:     filtered))
  277: 
  278: ;; Helper function for `forth-compile-word': return whether word has to be
  279: ;; added to the compiled word list, for syntactic parsing and hilighting.
  280: (defun forth-words-filter (word)
  281:   (let* ((hilight (nth 2 word))
  282: 	 (level (cdr hilight))
  283: 	 (parsing-flag (nth 3 word)))
  284:     (or parsing-flag 
  285: 	(<= level forth-hilight-level))))
  286: 
  287: ;; Helper function for `forth-compile-word': translate one entry from 
  288: ;; `forth-words' into the form  (regexp regexp-depth word-description)
  289: (defun forth-compile-words-mapper (word)
  290:   (let* ((matcher (car word))
  291: 	 (regexp (if (stringp matcher) (concat "\\(" matcher "\\)")
  292: 		   (if (listp matcher) (regexp-opt matcher t)
  293: 		     (error "Invalid matcher (stringp or listp expected `%s'" 
  294: 			    matcher))))
  295: 	 (depth (regexp-opt-depth regexp))
  296: 	 (description (cdr word)))
  297:     (list regexp depth description)))
  298: 
  299: ;; Read `words' and create a compiled representation suitable for efficient
  300: ;; parsing of the form  
  301: ;; (regexp (subexp-count word-description) (subexp-count2 word-description2)
  302: ;;  ...)
  303: (defun forth-compile-wordlist (words)
  304:   (let* ((mapped (mapcar 'forth-compile-words-mapper words))
  305: 	 (regexp (concat "\\<\\(" 
  306: 			 (mapconcat 'car mapped "\\|")
  307: 			 "\\)\\>"))
  308: 	 (sub-count 2)
  309: 	 (sub-list (mapcar 
  310: 		    (lambda (i) 
  311: 		      (let ((sub (cons sub-count (nth 2 i))))
  312: 			(setq sub-count (+ sub-count (nth 1 i)))
  313: 			sub 
  314: 			)) 
  315: 		    mapped)))
  316:     (let ((result (cons regexp sub-list)))
  317:       (byte-compile 'result)
  318:       result)))
  319: 
  320: (defun forth-compile-words ()
  321:   "Compile the the words from `forth-words' and `forth-indent-words' into
  322:  the format that's later used for doing the actual hilighting/indentation.
  323:  Store the resulting compiled wordlists in `forth-compiled-words' and 
  324: `forth-compiled-indent-words', respective"
  325:   (setq forth-compiled-words 
  326: 	(forth-compile-wordlist 
  327: 	 (forth-filter 'forth-words-filter forth-words)))
  328:   (setq forth-compiled-indent-words 
  329: 	(forth-compile-wordlist forth-indent-words)))
  330: 
  331: (defun forth-hack-local-variables ()
  332:   "Parse and bind local variables, set in the contents of the current 
  333:  forth-mode buffer. Prepend `forth-local-words' to `forth-words' and 
  334:  `forth-local-indent-words' to `forth-indent-words'."
  335:   (hack-local-variables)
  336:   (setq forth-words (append forth-local-words forth-words))
  337:   (setq forth-indent-words (append forth-local-indent-words 
  338: 				   forth-indent-words)))
  339: 
  340: (defun forth-customize-words ()
  341:   "Add the words from `forth-custom-words' and `forth-custom-indent-words'
  342:  to `forth-words' and `forth-indent-words', respective. Add 
  343:  `forth-objects-words' and/or `forth-oof-words' to `forth-words', if
  344:  `forth-use-objects' and/or `forth-use-oof', respective is set."
  345:   (setq forth-words (append forth-custom-words forth-words
  346: 			    (if forth-use-oof forth-oof-words nil)
  347: 			    (if forth-use-objects forth-objects-words nil)))
  348:   (setq forth-indent-words (append 
  349: 			    forth-custom-indent-words forth-indent-words)))
  350: 
  351: 
  352: 
  353: ;; get location of first character of previous forth word that's got 
  354: ;; properties
  355: (defun forth-previous-start (pos)
  356:   (let* ((word (get-text-property pos 'forth-word))
  357: 	 (prev (previous-single-property-change 
  358: 		(min (point-max) (1+ pos)) 'forth-word 
  359: 		(current-buffer) (point-min))))
  360:     (if (or (= (point-min) prev) word) prev
  361:       (if (get-text-property (1- prev) 'forth-word)
  362: 	  (previous-single-property-change 
  363: 	   prev 'forth-word (current-buffer) (point-min))
  364: 	(point-min)))))
  365: 
  366: ;; Get location of the last character of the current/next forth word that's
  367: ;; got properties, text that's parsed by the word is considered as parts of 
  368: ;; the word.
  369: (defun forth-next-end (pos)
  370:   (let* ((word (get-text-property pos 'forth-word))
  371: 	 (next (next-single-property-change pos 'forth-word 
  372: 					    (current-buffer) (point-max))))
  373:     (if word next
  374:       (if (get-text-property next 'forth-word)
  375: 	  (next-single-property-change 
  376: 	   next 'forth-word (current-buffer) (point-max))
  377: 	(point-max)))))
  378: 
  379: (defun forth-next-whitespace (pos)
  380:   (save-excursion
  381:     (goto-char pos)
  382:     (skip-syntax-forward "-" (point-max))
  383:     (point)))
  384: (defun forth-previous-word (pos)
  385:   (save-excursion
  386:     (goto-char pos)
  387:     (re-search-backward "\\<" pos (point-min) 1)
  388:     (point)))
  389: 
  390: ;; Delete all properties, used by Forth mode, from `from' to `to'.
  391: (defun forth-delete-properties (from to)
  392:   (remove-text-properties 
  393:    from to '(face nil forth-parsed nil forth-word nil forth-state nil)))
  394: 
  395: ;; Get the index of the branch of the most recently evaluated regular 
  396: ;; expression that matched. (used for identifying branches "a\\|b\\|c...")
  397: (defun forth-get-regexp-branch ()
  398:   (let ((count 2))
  399:     (while (not (match-beginning count))
  400:       (setq count (1+ count)))
  401:     count))
  402: 
  403: ;; seek to next forth-word and return its "word-description"
  404: (defun forth-next-known-forth-word (to)
  405:   (if (<= (point) to)
  406:       (progn
  407: 	(let* ((regexp (car forth-compiled-words))
  408: 	       (pos (re-search-forward regexp to t)))
  409: 	  (if pos (let ((branch (forth-get-regexp-branch))
  410: 			(descr (cdr forth-compiled-words)))
  411: 		    (goto-char (match-beginning 0))
  412: 		    (cdr (assoc branch descr)))
  413: 	    'nil)))
  414:     nil))
  415: 
  416: ;; Set properties of forth word at `point', eventually parsing subsequent 
  417: ;; words, and parsing all whitespaces. Set point to delimiter after word.
  418: ;; The word, including it's parsed text gets the `forth-word' property, whose 
  419: ;; value is unique, and may be used for getting the word's start/end 
  420: ;; positions.
  421: (defun forth-set-word-properties (state data)
  422:   (let* ((start (point))
  423: 	 (end (progn (re-search-forward "[ \t]\\|$" (point-max) 1)
  424: 		     (point)))
  425: 	 (type (car data))
  426: 	 (hilight (nth 1 data))
  427: 	 (bad-word (and (not state) (eq type 'compile-only)))
  428: 	 (hlface (if bad-word font-lock-warning-face
  429: 		   (if (<= (cdr hilight) forth-hilight-level)
  430: 		       (car hilight) nil))))
  431:     (when hlface (put-text-property start end 'face hlface))
  432:     ;; if word parses in current state, process parsed range of text
  433:     (when (or (not state) (eq type 'compile-only) (eq type 'immediate))
  434:       (let ((parse-data (nthcdr 2 data)))
  435: 	(while parse-data
  436: 	  (let ((delim (nth 0 parse-data))
  437: 		(skip-leading (nth 1 parse-data))
  438: 		(parse-type (nth 2 parse-data))
  439: 		(parsed-hilight (nth 3 parse-data))
  440: 		(parse-start (point))
  441: 		(parse-end))
  442: 	    (when skip-leading
  443: 	      (while (and (looking-at delim) (> (match-end 0) (point))
  444: 			  (not (looking-at "\n")))
  445: 		(forward-char)))
  446: 	    (re-search-forward delim (point-max) 1)
  447: 	    (setq parse-end (point))
  448: 	    (forth-delete-properties end parse-end)
  449: 	    (when (<= (cdr parsed-hilight) forth-hilight-level)
  450: 	      (put-text-property 
  451: 	       parse-start parse-end 'face (car parsed-hilight)))
  452: 	    (put-text-property 
  453: 	     parse-start parse-end 'forth-parsed parse-type)
  454: 	    (setq end parse-end)
  455: 	    (setq parse-data (nthcdr 4 parse-data))))))
  456:     (put-text-property start end 'forth-word start)))
  457: 
  458: ;; Search for known Forth words in the range `from' to `to', using 
  459: ;; `forth-next-known-forth-word' and set their properties via 
  460: ;; `forth-set-word-properties'.
  461: (defun forth-update-properties (from to &optional loudly)
  462:   (save-excursion
  463:     (let ((msg-count 0) (state) (word-descr) (last-location))
  464:       (goto-char (forth-previous-word (forth-previous-start 
  465: 				       (max (point-min) (1- from)))))
  466:       (setq to (forth-next-end (min (point-max) (1+ to))))
  467:       ;; `to' must be on a space delimiter, if a parsing word was changed
  468:       (setq to (forth-next-whitespace to))
  469:       (setq state (get-text-property (point) 'forth-state))
  470:       (setq last-location (point))
  471:       (forth-delete-properties (point) to)
  472:       ;; hilight loop...
  473:       (while (setq word-descr (forth-next-known-forth-word to))
  474: 	(when loudly
  475: 	  (when (equal 0 (% msg-count 100))
  476: 	    (message "Parsing Forth code...%s"
  477: 		     (make-string (/ msg-count 100) ?.)))
  478: 	  (setq msg-count (1+ msg-count)))
  479: 	(forth-set-word-properties state word-descr)
  480: 	(when state (put-text-property last-location (point) 'forth-state t))
  481: 	(let ((type (car word-descr)))
  482: 	  (if (eq type 'definition-starter) (setq state t))
  483: 	  (if (eq type 'definition-ender) (setq state nil))
  484: 	  (setq last-location (point))))
  485:       ;; update state property up to `to'
  486:       (if (and state (< (point) to))
  487: 	  (put-text-property last-location to 'forth-state t))
  488:       ;; extend search if following state properties differ from current state
  489:       (if (< to (point-max))
  490: 	  (if (not (equal state (get-text-property (1+ to) 'forth-state)))
  491: 	      (let ((extend-to (next-single-property-change 
  492: 				to 'forth-state (current-buffer) (point-max))))
  493: 		(forth-update-properties to extend-to))
  494: 	    ))
  495:       )))
  496: 
  497: ;; save-buffer-state borrowed from `font-lock.el'
  498: (eval-when-compile 
  499:   (defmacro forth-save-buffer-state (varlist &rest body)
  500:     "Bind variables according to VARLIST and eval BODY restoring buffer state."
  501:     (` (let* ((,@ (append varlist
  502: 		   '((modified (buffer-modified-p)) (buffer-undo-list t)
  503: 		     (inhibit-read-only t) (inhibit-point-motion-hooks t)
  504: 		     before-change-functions after-change-functions
  505: 		     deactivate-mark buffer-file-name buffer-file-truename))))
  506: 	 (,@ body)
  507: 	 (when (and (not modified) (buffer-modified-p))
  508: 	   (set-buffer-modified-p nil))))))
  509: 
  510: ;; Function that is added to the `change-functions' hook. Calls 
  511: ;; `forth-update-properties' and keeps care of disabling undo information
  512: ;; and stuff like that.
  513: (defun forth-change-function (from to len &optional loudly)
  514:   (save-match-data
  515:     (forth-save-buffer-state () 
  516:      (unwind-protect 
  517: 	 (progn 
  518: 	   (forth-update-properties from to loudly)
  519: 	   (forth-update-show-screen)
  520: 	   (forth-update-warn-long-lines))))))
  521: 
  522: (eval-when-compile
  523:   (byte-compile 'forth-set-word-properties)
  524:   (byte-compile 'forth-next-known-forth-word)
  525:   (byte-compile 'forth-update-properties)
  526:   (byte-compile 'forth-delete-properties)
  527:   (byte-compile 'forth-get-regexp-branch)) 
  528: 
  529: ;;; imenu support
  530: ;;;
  531: (defvar forth-defining-words 
  532:   '("VARIABLE" "CONSTANT" "2VARIABLE" "2CONSTANT" "FVARIABLE" "FCONSTANT"
  533:    "USER" "VALUE" "field" "end-struct" "VOCABULARY" "CREATE" ":" "CODE"
  534:    "DEFER" "ALIAS")
  535:   "List of words, that define the following word.
  536:  Used for imenu index generation")
  537: 
  538:  
  539: (defun forth-next-definition-starter ()
  540:   (progn
  541:     (let* ((pos (re-search-forward forth-defining-words-regexp (point-max) t)))
  542:       (if pos
  543: 	  (if (or (text-property-not-all (match-beginning 0) (match-end 0) 
  544: 					 'forth-parsed nil)
  545: 		  (text-property-not-all (match-beginning 0) (match-end 0)
  546: 					 'forth-state nil)) 
  547: 	      (forth-next-definition-starter)
  548: 	    t)
  549: 	nil))))
  550: 
  551: (defun forth-create-index ()
  552:   (let* ((forth-defining-words-regexp 
  553: 	  (concat "\\<\\(" (regexp-opt forth-defining-words) "\\)\\>"))
  554: 	 (index nil))
  555:     (goto-char (point-min))
  556:     (while (forth-next-definition-starter)
  557:       (if (looking-at "[ \t]*\\([^ \t\n]+\\)")
  558: 	  (setq index (cons (cons (match-string 1) (point)) index))))
  559:     index))
  560: 
  561: (require 'speedbar)
  562: (speedbar-add-supported-extension ".fs")
  563: (speedbar-add-supported-extension ".fb")
  564: 
  565: ;; (require 'profile)
  566: ;; (setq profile-functions-list '(forth-set-word-properties forth-next-known-forth-word forth-update-properties forth-delete-properties forth-get-regexp-branch))
  567: 
  568: ;;; Indentation
  569: ;;;
  570: 
  571: (defvar forth-indent-words nil 
  572:   "List of words that have indentation behaviour.
  573: Each element of `forth-indent-words' should have the form
  574:    (MATCHER INDENT1 INDENT2) 
  575:   
  576: MATCHER is either a list of strings to match, or a REGEXP.
  577:    If it's a REGEXP, it should not be surrounded by `\\<` or `\\>`, since 
  578:    that'll be done automatically by the search routines.
  579: 
  580: INDENT1 specifies how to indent a word that's located at a line's begin,
  581:    following any number of whitespaces.
  582: 
  583: INDENT2 specifies how to indent words that are not located at a line's begin.
  584: 
  585: INDENT1 and INDENT2 are indentation specifications of the form
  586:    (SELF-INDENT . NEXT-INDENT), where SELF-INDENT is a numerical value, 
  587:    specifying how the matching line and all following lines are to be 
  588:    indented, relative to previous lines. NEXT-INDENT specifies how to indent 
  589:    following lines, relative to the matching line.
  590:   
  591:    Even values of SELF-INDENT and NEXT-INDENT correspond to multiples of
  592:    `forth-indent-level'. Odd values get an additional 
  593:    `forth-minor-indent-level' added/substracted. Eg a value of -2 indents
  594:    1 * forth-indent-level  to the left, wheras 3 indents 
  595:    1 * forth-indent-level + forth-minor-indent-level  columns to the right.")
  596: 
  597: (setq forth-indent-words
  598:       '(((":" ":noname" "code" "if" "begin" "do" "?do" "+do" "-do" "u+do"
  599: 	  "u-do" "?dup-if" "?dup-0=-if" "case" "of" "try" "struct" 
  600: 	  "[if]" "[ifdef]" "[ifundef]" "[begin]" "[for]" "[do]" "[?do]"
  601: 	  "class" "interface" "m:" ":m")
  602: 	 (0 . 2) (0 . 2))
  603: 	((";" ";m") (-2 . 0) (0 . -2))
  604: 	(("end-code" "again" "repeat" "then" "endtry" "endcase" "endof" 
  605: 	  "end-struct" "[then]" "[endif]" "[loop]" "[+loop]" "[next]" 
  606: 	  "[until]" "[repeat]" "[again]" "end-class" "end-interface"
  607: 	  "end-class-noname" "end-interface-noname" "loop"
  608: 	  "class;")
  609: 	 (-2 . 0) (0 . -2))
  610: 	(("protected" "public" "how:") (-1 . 1) (0 . 0))
  611: 	(("+loop" "-loop" "until") (-2 . 0) (-2 . 0))
  612: 	(("else" "recover" "[else]") (-2 . 2) (0 . 0))
  613: 	(("while" "does>" "[while]") (-1 . 1) (0 . 0))
  614: 	(("\\g") (-2 . 2) (0 . 0))))
  615: 
  616: (defvar forth-local-indent-words nil 
  617:   "List of Forth words to prepend to `forth-indent-words', when a forth-mode
  618: buffer is created. Should be set by a Forth source, using a local variables 
  619: list at the end of the file (\"Local Variables: ... forth-local-words: ... 
  620: End:\" construct).")
  621: 
  622: (defvar forth-custom-indent-words nil
  623:   "List of Forth words to prepend to `forth-indent-words'. Should be set in
  624:  your .emacs.")
  625: 
  626: (defvar forth-indent-level 4
  627:   "Indentation of Forth statements.")
  628: (defvar forth-minor-indent-level 2
  629:   "Minor indentation of Forth statements.")
  630: (defvar forth-compiled-indent-words nil)
  631: 
  632: ;; Return, whether `pos' is the first forth word on its line
  633: (defun forth-first-word-on-line-p (pos)
  634:   (save-excursion
  635:     (beginning-of-line)
  636:     (skip-chars-forward " \t")
  637:     (= pos (point))))
  638: 
  639: ;; Return indentation data (SELF-INDENT . NEXT-INDENT) of next known 
  640: ;; indentation word, or nil if there is no word up to `to'. 
  641: ;; Position `point' at location just after found word, or at `to'. Parsed 
  642: ;; ranges of text will not be taken into consideration!
  643: (defun forth-next-known-indent-word (to)
  644:   (if (<= (point) to)
  645:       (progn
  646: 	(let* ((regexp (car forth-compiled-indent-words))
  647: 	       (pos (re-search-forward regexp to t)))
  648: 	  (if pos
  649: 	      (if (text-property-not-all (match-beginning 0) (match-end 0) 
  650: 					 'forth-parsed nil)
  651: 		  (forth-next-known-indent-word to)
  652: 		(let* ((branch (forth-get-regexp-branch))
  653: 		       (descr (cdr forth-compiled-indent-words))
  654: 		       (indent (cdr (assoc branch descr))))
  655: 		  (if (forth-first-word-on-line-p (match-beginning 0))
  656: 		      (nth 0 indent) (nth 1 indent))))
  657: 	    nil)))
  658:     nil))
  659:   
  660: ;; Translate indentation value `indent' to indentation column. Multiples of
  661: ;; 2 correspond to multiples of `forth-indent-level'. Odd numbers get an
  662: ;; additional `forth-minor-indent-level' added (or substracted).
  663: (defun forth-convert-to-column (indent)
  664:   (let* ((sign (if (< indent 0) -1 1))
  665: 	 (value (abs indent))
  666: 	 (major (* (/ value 2) forth-indent-level))
  667: 	 (minor (* (% value 2) forth-minor-indent-level)))
  668:     (* sign (+ major minor))))
  669: 
  670: ;; Return the column increment, that the current line of forth code does to
  671: ;; the current or following lines. `which' specifies which indentation values
  672: ;; to use. 0 means the indentation of following lines relative to current 
  673: ;; line, 1 means the indentation of the current line relative to the previous 
  674: ;; line. Return `nil', if there are no indentation words on the current line.
  675: (defun forth-get-column-incr (which)
  676:   (save-excursion
  677:     (let ((regexp (car forth-compiled-indent-words))
  678: 	  (word-indent)
  679: 	  (self-indent nil)
  680: 	  (next-indent nil)
  681: 	  (to (save-excursion (end-of-line) (point))))
  682:       (beginning-of-line)
  683:       (while (setq word-indent (forth-next-known-indent-word to))
  684: 	(let* ((self-incr (car word-indent))
  685: 	       (next-incr (cdr word-indent))
  686: 	       (self-column-incr (forth-convert-to-column self-incr))
  687: 	       (next-column-incr (forth-convert-to-column next-incr)))
  688: 	  (setq next-indent (if next-indent next-indent 0))
  689: 	  (setq self-indent (if self-indent self-indent 0))
  690: 	  (if (or (and (> next-indent 0) (< self-column-incr 0))
  691: 		  (and (< next-indent 0) (> self-column-incr 0)))
  692: 	      (setq next-indent (+ next-indent self-column-incr))
  693: 	    (setq self-indent (+ self-indent self-column-incr)))
  694: 	  (setq next-indent (+ next-indent next-column-incr))))
  695:       (nth which (list self-indent next-indent)))))
  696: 
  697: ;; Find previous line that contains indentation words, return the column,
  698: ;; to which following text should be indented to.
  699: (defun forth-get-anchor-column ()
  700:   (save-excursion
  701:     (if (/= 0 (forward-line -1)) 0
  702:       (let ((next-indent)
  703: 	    (self-indent))
  704: 	(while (not (or (setq indent (forth-get-column-incr 1))
  705: 			(<= (point) (point-min))))
  706: 	  (forward-line -1))
  707: 	(+ (current-indentation) (if indent indent 0))))))
  708: 
  709: (defun forth-indent-line (&optional flag)
  710:   "Correct indentation of the current Forth line."
  711:   (let* ((anchor (forth-get-anchor-column))
  712: 	 (column-incr (forth-get-column-incr 0)))
  713:     (forth-indent-to (if column-incr (+ anchor column-incr) anchor))))
  714: 
  715: (defun forth-current-column ()
  716:   (- (point) (save-excursion (beginning-of-line) (point))))
  717: (defun forth-current-indentation ()
  718:   (- (save-excursion (beginning-of-line) (forward-to-indentation 0) (point))
  719:      (save-excursion (beginning-of-line) (point))))
  720: 
  721: (defun forth-indent-to (x)
  722:   (let ((p nil))
  723:     (setq p (- (forth-current-column) (forth-current-indentation)))
  724:     (forth-delete-indentation)
  725:     (beginning-of-line)
  726:     (indent-to x)
  727:     (if (> p 0) (forward-char p))))
  728: 
  729: (defun forth-delete-indentation ()
  730:   (save-excursion
  731:     (delete-region 
  732:      (progn (beginning-of-line) (point)) 
  733:      (progn (back-to-indentation) (point)))))
  734: 
  735: (defun forth-indent-command ()
  736:   (interactive)
  737:   (forth-indent-line t))
  738: 
  739: ;; remove trailing whitespaces in current line
  740: (defun forth-remove-trailing ()
  741:   (save-excursion
  742:     (end-of-line)
  743:     (delete-region (point) (progn (skip-chars-backward " \t") (point)))))
  744: 
  745: ;; insert newline, removing any trailing whitespaces in the current line
  746: (defun forth-newline-remove-trailing ()
  747:   (save-excursion
  748:     (delete-region (point) (progn (skip-chars-backward " \t") (point))))
  749:   (newline))
  750: ;  (let ((was-point (point-marker)))
  751: ;    (unwind-protect 
  752: ;	(progn (forward-line -1) (forth-remove-trailing))
  753: ;      (goto-char (was-point)))))
  754: 
  755: ;; workaround for bug in `reindent-then-newline-and-indent'
  756: (defun forth-reindent-then-newline-and-indent ()
  757:   (interactive "*")
  758:   (indent-according-to-mode)
  759:   (forth-newline-remove-trailing)
  760:   (indent-according-to-mode))
  761: 
  762: ;;; end hilighting/indentation
  763: 
  764: ;;; Block file encoding/decoding  (dk)
  765: ;;;
  766: 
  767: (defconst forth-c/l 64 "Number of characters per block line")
  768: (defconst forth-l/b 16 "Number of lines per block")
  769: 
  770: ;; Check whether the unconverted block file line, point is in, does not
  771: ;; contain `\n' and `\t' characters.
  772: (defun forth-check-block-line (line)
  773:   (let ((end (save-excursion (beginning-of-line) (forward-char forth-c/l)
  774: 			     (point))))
  775:     (save-excursion 
  776:       (beginning-of-line)
  777:       (when (search-forward "\n" end t)
  778: 	(message "Warning: line %i contains newline character #10" line)
  779: 	(ding t))
  780:       (beginning-of-line)
  781:       (when (search-forward "\t" end t)
  782: 	(message "Warning: line %i contains tab character #8" line)
  783: 	(ding t)))))
  784: 
  785: (defun forth-convert-from-block (from to)
  786:   "Convert block file format to stream source in current buffer."
  787:   (let ((line (count-lines (point-min) from)))
  788:     (save-excursion
  789:       (goto-char from)
  790:       (set-mark to)
  791:       (while (< (+ (point) forth-c/l) (mark t))
  792: 	(setq line (1+ line))
  793: 	(forth-check-block-line line)
  794: 	(forward-char forth-c/l)
  795: 	(forth-newline-remove-trailing))
  796:       (when (= (+ (point) forth-c/l) (mark t))
  797: 	(forth-remove-trailing))
  798:       (mark t))))
  799: 
  800: ;; Pad a line of a block file up to `forth-c/l' characters, positioning `point'
  801: ;; at the end of line.
  802: (defun forth-pad-block-line ()
  803:   (save-excursion
  804:     (end-of-line)
  805:     (if (<= (current-column) forth-c/l)
  806: 	(move-to-column forth-c/l t)
  807:       (message "Line %i longer than %i characters, truncated"
  808: 	       (count-lines (point-min) (point)) forth-c/l)
  809:       (ding t)
  810:       (move-to-column forth-c/l t)
  811:       (delete-region (point) (progn (end-of-line) (point))))))
  812: 
  813: ;; Replace tab characters in current line by spaces.
  814: (defun forth-convert-tabs-in-line ()
  815:   (save-excursion
  816:     (beginning-of-line)
  817:     (while (search-forward "\t" (save-excursion (end-of-line) (point)) t)
  818:       (backward-char)
  819:       (delete-region (point) (1+ (point)))
  820:       (insert-char ?\  (- tab-width (% (current-column) tab-width))))))
  821: 
  822: ;; Delete newline at end of current line, concatenating it with the following
  823: ;; line. Place `point' at end of newly formed line.
  824: (defun forth-delete-newline ()
  825:   (end-of-line)
  826:   (delete-region (point) (progn (beginning-of-line 2) (point))))
  827: 
  828: (defun forth-convert-to-block (from to &optional original-buffer) 
  829:   "Convert range of text to block file format in current buffer."
  830:   (let* ((lines 0)) ; I have to count lines myself, since `count-lines' has
  831: 		    ; problems with trailing newlines...
  832:     (save-excursion
  833:       (goto-char from)
  834:       (set-mark to)
  835:       ;; pad lines to full length (`forth-c/l' characters per line)
  836:       (while (< (save-excursion (end-of-line) (point)) (mark t))
  837: 	(setq lines (1+ lines))
  838: 	(forth-pad-block-line)
  839: 	(forth-convert-tabs-in-line)
  840: 	(forward-line))
  841:       ;; also make sure the last line is padded, if `to' is at its end
  842:       (end-of-line)
  843:       (when (= (point) (mark t))
  844: 	(setq lines (1+ lines))
  845: 	(forth-pad-block-line)
  846: 	(forth-convert-tabs-in-line))
  847:       ;; remove newlines between lines
  848:       (goto-char from)
  849:       (while (< (save-excursion (end-of-line) (point)) (mark t))
  850: 	(forth-delete-newline))
  851:       ;; append empty lines, until last block is complete
  852:       (goto-char (mark t))
  853:       (let* ((required (* (/ (+ lines (1- forth-l/b)) forth-l/b) forth-l/b))
  854: 	     (pad-lines (- required lines)))
  855: 	(while (> pad-lines 0)
  856: 	  (insert-char ?\  forth-c/l)
  857: 	  (setq pad-lines (1- pad-lines))))
  858:       (point))))
  859: 
  860: (defun forth-detect-block-file-p ()
  861:   "Return non-nil if the current buffer is in block file format. Detection is
  862: done by checking whether the first line has 1024 characters or more."
  863:   (save-restriction 
  864:     (widen)
  865:     (save-excursion
  866:        (beginning-of-buffer)
  867:        (end-of-line)
  868:        (>= (current-column) 1024))))
  869: 
  870: ;; add block file conversion routines to `format-alist'
  871: (defconst forth-block-format-description
  872:   '(forth-blocks "Forth block source file" nil 
  873: 		 forth-convert-from-block forth-convert-to-block 
  874: 		 t normal-mode))
  875: (unless (memq forth-block-format-description format-alist)
  876:   (setq format-alist (cons forth-block-format-description format-alist)))
  877: 
  878: ;;; End block file encoding/decoding
  879: 
  880: ;;; Block file editing
  881: ;;;
  882: (defvar forth-overlay-arrow-string ">>")
  883: (defvar forth-block-base 1 "Number of first block in block file")
  884: (defvar forth-show-screen nil
  885:   "Non-nil means to show screen starts and numbers (for block files)")
  886: (defvar forth-warn-long-lines nil
  887:   "Non-nil means to warn about lines that are longer than 64 characters")
  888: 
  889: (defvar forth-screen-marker nil)
  890: 
  891: (defun forth-update-show-screen ()
  892:   "If `forth-show-screen' is non-nil, put overlay arrow to start of screen, 
  893: `point' is in. If arrow now points to different screen than before, display 
  894: screen number."
  895:   (if (not forth-show-screen)
  896:       (setq overlay-arrow-string nil)
  897:     (save-excursion
  898:       (let* ((line (count-lines (point-min) (min (point-max) (1+ (point)))))
  899: 	     (first-line (1+ (* (/ (1- line) forth-l/b) forth-l/b)))
  900: 	     (scr (+ forth-block-base (/ first-line forth-l/b))))
  901: 	(setq overlay-arrow-string forth-overlay-arrow-string)
  902: 	(goto-line first-line)
  903: 	(setq overlay-arrow-position forth-screen-marker)
  904: 	(set-marker forth-screen-marker 
  905: 		    (save-excursion (goto-line first-line) (point)))
  906: 	(setq forth-screen-number-string (format "%d" scr))))))
  907: 
  908: (add-hook 'forth-motion-hooks 'forth-update-show-screen)
  909: 
  910: (defun forth-update-warn-long-lines ()
  911:   "If `forth-warn-long-lines' is non-nil, display a warning whenever a line
  912: exceeds 64 characters."
  913:   (when forth-warn-long-lines
  914:     (when (> (save-excursion (end-of-line) (current-column)) forth-c/l)
  915:       (message "Warning: current line exceeds %i characters"
  916: 	       forth-c/l))))
  917: 
  918: (add-hook 'forth-motion-hooks 'forth-update-warn-long-lines)
  919:     
  920: ;;; End block file editing
  921: 
  922: 
  923: (defvar forth-mode-abbrev-table nil
  924:   "Abbrev table in use in Forth-mode buffers.")
  925: 
  926: (define-abbrev-table 'forth-mode-abbrev-table ())
  927: 
  928: (defvar forth-mode-map nil
  929:   "Keymap used in Forth mode.")
  930: 
  931: (if (not forth-mode-map)
  932:     (setq forth-mode-map (make-sparse-keymap)))
  933: 
  934: ;(define-key forth-mode-map "\M-\C-x" 'compile)
  935: (define-key forth-mode-map "\C-x\\" 'comment-region)
  936: (define-key forth-mode-map "\C-x~" 'forth-remove-tracers)
  937: (define-key forth-mode-map "\e\C-m" 'forth-send-paragraph)
  938: (define-key forth-mode-map "\eo" 'forth-send-buffer)
  939: (define-key forth-mode-map "\C-x\C-m" 'forth-split)
  940: (define-key forth-mode-map "\e " 'forth-reload)
  941: (define-key forth-mode-map "\t" 'forth-indent-command)
  942: (define-key forth-mode-map "\C-m" 'forth-reindent-then-newline-and-indent)
  943: (define-key forth-mode-map "\M-q" 'forth-fill-paragraph)
  944: (define-key forth-mode-map "\e." 'forth-find-tag)
  945: 
  946: ;;; hook into motion events (realy ugly!)  (dk)
  947: (define-key forth-mode-map "\C-n" 'forth-next-line)
  948: (define-key forth-mode-map "\C-p" 'forth-previous-line)
  949: (define-key forth-mode-map [down] 'forth-next-line)
  950: (define-key forth-mode-map [up] 'forth-previous-line)
  951: (define-key forth-mode-map "\C-f" 'forth-forward-char)
  952: (define-key forth-mode-map "\C-b" 'forth-backward-char)
  953: (define-key forth-mode-map [right] 'forth-forward-char)
  954: (define-key forth-mode-map [left] 'forth-backward-char)
  955: (define-key forth-mode-map "\M-f" 'forth-forward-word)
  956: (define-key forth-mode-map "\M-b" 'forth-backward-word)
  957: (define-key forth-mode-map [C-right] 'forth-forward-word)
  958: (define-key forth-mode-map [C-left] 'forth-backward-word)
  959: (define-key forth-mode-map "\M-v" 'forth-scroll-down)
  960: (define-key forth-mode-map "\C-v" 'forth-scroll-up)
  961: (define-key forth-mode-map [prior] 'forth-scroll-down)
  962: (define-key forth-mode-map [next] 'forth-scroll-up)
  963: 
  964: (defun forth-next-line (arg) 
  965:   (interactive "p") (next-line arg) (run-hooks 'forth-motion-hooks))
  966: (defun forth-previous-line (arg)
  967:   (interactive "p") (previous-line arg) (run-hooks 'forth-motion-hooks))
  968: (defun forth-backward-char (arg)
  969:   (interactive "p") (backward-char arg) (run-hooks 'forth-motion-hooks))
  970: (defun forth-forward-char (arg)
  971:   (interactive "p") (forward-char arg) (run-hooks 'forth-motion-hooks))
  972: (defun forth-forward-word (arg)
  973:   (interactive "p") (forward-word arg) (run-hooks 'forth-motion-hooks))
  974: (defun forth-backward-word (arg)
  975:   (interactive "p") (backward-word arg) (run-hooks 'forth-motion-hooks))
  976: (defun forth-scroll-down (arg)
  977:   (interactive "P") (scroll-down arg) (run-hooks 'forth-motion-hooks))
  978: (defun forth-scroll-up (arg)
  979:   (interactive "P") (scroll-up arg) (run-hooks 'forth-motion-hooks))
  980: 
  981: ;setup for C-h C-i to work
  982: (if (fboundp 'info-lookup-add-help)
  983:     (info-lookup-add-help
  984:      :topic 'symbol
  985:      :mode 'forth-mode
  986:      :regexp "[^ 	
  987: ]+"
  988:      :ignore-case t
  989:      :doc-spec '(("(gforth)Name Index" nil "`" "'  "))))
  990: 
  991: (load "etags")
  992: 
  993: (defun forth-find-tag (tagname &optional next-p regexp-p)
  994:   (interactive (find-tag-interactive "Find tag: "))
  995:   (unless (or regexp-p next-p)
  996:     (setq tagname (concat "\\(^\\|\\s-\\)\\(" (regexp-quote tagname) 
  997: 			    "\\)\\(\\s-\\|$\\)")))
  998:   (switch-to-buffer
  999:    (find-tag-noselect tagname next-p t)))
 1000: 
 1001: (defvar forth-mode-syntax-table nil
 1002:   "Syntax table in use in Forth-mode buffers.")
 1003: 
 1004: ;; Important: hilighting/indentation now depends on a correct syntax table.
 1005: ;; All characters, except whitespace *must* belong to the "word constituent"
 1006: ;; syntax class. If different behaviour is required, use of Categories might
 1007: ;; help.
 1008: (if (not forth-mode-syntax-table)
 1009:     (progn
 1010:       (setq forth-mode-syntax-table (make-syntax-table))
 1011:       (let ((char 0))
 1012: 	(while (< char ?!)
 1013: 	  (modify-syntax-entry char " " forth-mode-syntax-table)
 1014: 	  (setq char (1+ char)))
 1015: 	(while (< char 256)
 1016: 	  (modify-syntax-entry char "w" forth-mode-syntax-table)
 1017: 	  (setq char (1+ char))))
 1018:       ))
 1019: 
 1020: 
 1021: (defun forth-mode-variables ()
 1022:   (set-syntax-table forth-mode-syntax-table)
 1023:   (setq local-abbrev-table forth-mode-abbrev-table)
 1024:   (make-local-variable 'paragraph-start)
 1025:   (setq paragraph-start (concat "^$\\|" page-delimiter))
 1026:   (make-local-variable 'paragraph-separate)
 1027:   (setq paragraph-separate paragraph-start)
 1028:   (make-local-variable 'indent-line-function)
 1029:   (setq indent-line-function 'forth-indent-line)
 1030: ;  (make-local-variable 'require-final-newline)
 1031: ;  (setq require-final-newline t)
 1032:   (make-local-variable 'comment-start)
 1033:   (setq comment-start "\\ ")
 1034:   ;(make-local-variable 'comment-end)
 1035:   ;(setq comment-end " )")
 1036:   (make-local-variable 'comment-column)
 1037:   (setq comment-column 40)
 1038:   (make-local-variable 'comment-start-skip)
 1039:   (setq comment-start-skip "\\ ")
 1040:   (make-local-variable 'comment-indent-hook)
 1041:   (setq comment-indent-hook 'forth-comment-indent)
 1042:   (make-local-variable 'parse-sexp-ignore-comments)
 1043:   (setq parse-sexp-ignore-comments t)
 1044:   (setq case-fold-search t)
 1045:   (make-local-variable 'forth-words)
 1046:   (make-local-variable 'forth-compiled-words)
 1047:   (make-local-variable 'forth-compiled-indent-words)
 1048:   (make-local-variable 'forth-hilight-level)
 1049:   (make-local-variable 'after-change-functions)
 1050:   (make-local-variable 'forth-show-screen)
 1051:   (make-local-variable 'forth-screen-marker)
 1052:   (make-local-variable 'forth-warn-long-lines)
 1053:   (make-local-variable 'forth-screen-number-string)
 1054:   (make-local-variable 'forth-use-oof)
 1055:   (make-local-variable 'forth-use-objects) 
 1056:   (setq forth-screen-marker (copy-marker 0))
 1057:   (add-hook 'after-change-functions 'forth-change-function)
 1058:   (setq imenu-create-index-function 'forth-create-index))
 1059: 
 1060: ;;;###autoload
 1061: (defun forth-mode ()
 1062:   "
 1063: Major mode for editing Forth code. Tab indents for Forth code. Comments
 1064: are delimited with \\ and newline. Paragraphs are separated by blank lines
 1065: only. Block files are autodetected, when read, and converted to normal 
 1066: stream source format. See also `forth-block-mode'.
 1067: \\{forth-mode-map}
 1068:  Forth-split
 1069:     Positions the current buffer on top and a forth-interaction window
 1070:     below. The window size is controlled by the forth-percent-height
 1071:     variable (see below).
 1072:  Forth-reload
 1073:     Reloads the forth library and restarts the forth process.
 1074:  Forth-send-buffer
 1075:     Sends the current buffer, in text representation, as input to the
 1076:     forth process.
 1077:  Forth-send-paragraph
 1078:     Sends the previous or the current paragraph to the forth-process.
 1079:     Note that the cursor only need to be with in the paragraph to be sent.
 1080:  forth-documentation
 1081:     Search for documentation of forward adjacent to cursor. Note! To use
 1082:     this mode you have to add a line, to your .emacs file, defining the
 1083:     directories to search through for documentation files (se variable
 1084:     forth-help-load-path below) e.g. (setq forth-help-load-path '(nil)).
 1085: 
 1086: Variables controlling interaction and startup
 1087:  forth-percent-height
 1088:     Tells split how high to make the edit portion, in percent of the
 1089:     current screen height.
 1090:  forth-program-name
 1091:     Tells the library which program name to execute in the interation
 1092:     window.
 1093: 
 1094: Variables controlling syntax hilighting/recognition of parsed text:
 1095:  `forth-words'
 1096:     List of words that have a special parsing behaviour and/or should be
 1097:     hilighted. Add custom words by setting forth-custom-words in your
 1098:     .emacs, or by setting forth-local-words, in source-files' local 
 1099:     variables lists.
 1100:  forth-use-objects
 1101:     Set this variable to non-nil in your .emacs, or a local variables 
 1102:     list, to hilight and recognize the words from the \"Objects\" package 
 1103:     for object-oriented programming.
 1104:  forth-use-oof
 1105:     Same as above, just for the \"OOF\" package.
 1106:  forth-custom-words
 1107:     List of custom Forth words to prepend to `forth-words'. Should be set
 1108:     in your .emacs.
 1109:  forth-local-words
 1110:     List of words to prepend to `forth-words', whenever a forth-mode
 1111:     buffer is created. That variable should be set by Forth sources, using
 1112:     a local variables list at the end of file, to get file-specific
 1113:     hilighting.
 1114:     0 [IF]
 1115:        Local Variables: ... 
 1116:        forth-local-words: ...
 1117:        End:
 1118:     [THEN]
 1119:  forth-hilight-level
 1120:     Controls how much syntax hilighting is done. Should be in the range 
 1121:     0..3
 1122: 
 1123: Variables controlling indentation style:
 1124:  `forth-indent-words'
 1125:     List of words that influence indentation.
 1126:  forth-local-indent-words
 1127:     List of words to prepend to `forth-indent-words', similar to 
 1128:     forth-local-words. Should be used for specifying file-specific 
 1129:     indentation, using a local variables list.
 1130:  forth-custom-indent-words
 1131:     List of words to prepend to `forth-indent-words'. Should be set in your
 1132:     .emacs.    
 1133:  forth-indent-level
 1134:     Indentation increment/decrement of Forth statements.
 1135:  forth-minor-indent-level
 1136:     Minor indentation increment/decrement of Forth statemens.
 1137: 
 1138: Variables controlling block-file editing:
 1139:  forth-show-screen
 1140:     Non-nil means, that the start of the current screen is marked by an
 1141:     overlay arrow, and screen numbers are displayed in the mode line.
 1142:     This variable is by default nil for `forth-mode' and t for 
 1143:     `forth-block-mode'.
 1144:  forth-overlay-arrow-string
 1145:     String to display as the overlay arrow, when `forth-show-screen' is t.
 1146:     Setting this variable to nil disables the overlay arrow.
 1147:  forth-block-base
 1148:     Screen number of the first block in a block file. Defaults to 1.
 1149:  forth-warn-long-lines
 1150:     Non-nil means that a warning message is displayed whenever you edit or
 1151:     move over a line that is longer than 64 characters (the maximum line
 1152:     length that can be stored into a block file). This variable defaults to
 1153:     t for `forth-block-mode' and to nil for `forth-mode'.
 1154: 
 1155: Variables controling documentation search
 1156:  forth-help-load-path
 1157:     List of directories to search through to find *.doc
 1158:     (forth-help-file-suffix) files. Nil means current default directory.
 1159:     The specified directories must contain at least one .doc file. If it
 1160:     does not and you still want the load-path to scan that directory, create
 1161:     an empty file dummy.doc.
 1162:  forth-help-file-suffix
 1163:     The file names to search for in each directory specified by
 1164:     forth-help-load-path. Defaulted to '*.doc'. 
 1165: "
 1166:   (interactive)
 1167:   (kill-all-local-variables)
 1168:   (use-local-map forth-mode-map)
 1169:   (setq mode-name "Forth")
 1170:   (setq major-mode 'forth-mode)
 1171:   ;; convert buffer contents from block file format, if necessary
 1172:   (when (forth-detect-block-file-p)
 1173:     (widen)
 1174:     (message "Converting from Forth block source...")
 1175:     (forth-convert-from-block (point-min) (point-max))
 1176:     (message "Converting from Forth block source...done"))
 1177:   ;; if user switched from forth-block-mode to forth-mode, make sure the file
 1178:   ;; is now stored as normal strem source
 1179:   (when (equal buffer-file-format '(forth-blocks))
 1180:     (setq buffer-file-format nil))
 1181:   (forth-mode-variables)
 1182: ;  (if (not (forth-process-running-p))
 1183: ;      (run-forth forth-program-name))
 1184:   (run-hooks 'forth-mode-hook))
 1185: 
 1186: ;;;###autoload
 1187: (define-derived-mode forth-block-mode forth-mode "Forth Block Source" 
 1188:   "Major mode for editing Forth block source files, derived from 
 1189: `forth-mode'. Differences to `forth-mode' are:
 1190:  * files are converted to block format, when written (`buffer-file-format' 
 1191:    is set to `(forth-blocks)')
 1192:  * `forth-show-screen' and `forth-warn-long-lines' are t by default
 1193:   
 1194: Note that the length of lines in block files is limited to 64 characters.
 1195: When writing longer lines to a block file, a warning is displayed in the
 1196: echo area and the line is truncated. 
 1197: 
 1198: Another problem is imposed by block files that contain newline or tab 
 1199: characters. When Emacs converts such files back to block file format, 
 1200: it'll translate those characters to a number of spaces. However, when
 1201: you read such a file, a warning message is displayed in the echo area,
 1202: including a line number that may help you to locate and fix the problem.
 1203: 
 1204: So have a look at the *Messages* buffer, whenever you hear (or see) Emacs' 
 1205: bell during block file read/write operations."
 1206:   (setq buffer-file-format '(forth-blocks))
 1207:   (setq forth-show-screen t)
 1208:   (setq forth-warn-long-lines t)
 1209:   (setq forth-screen-number-string (format "%d" forth-block-base))
 1210:   (setq mode-line-format (append (reverse (cdr (reverse mode-line-format)))
 1211: 				 '("--S" forth-screen-number-string "-%-"))))
 1212: 
 1213: (add-hook 'forth-mode-hook
 1214:       '(lambda () 
 1215: 	 (make-local-variable 'compile-command)
 1216: 	 (setq compile-command "gforth ")
 1217: 	 (forth-hack-local-variables)
 1218: 	 (forth-customize-words)
 1219: 	 (forth-compile-words)
 1220: 	 (forth-change-function (point-min) (point-max) nil t)))
 1221: 
 1222: (defun forth-fill-paragraph () 
 1223:   "Fill comments (starting with '\'; do not fill code (block style
 1224: programmers who tend to fill code won't use emacs anyway:-)."
 1225:   ; Currently only comments at the start of the line are filled.
 1226:   ; Something like lisp-fill-paragraph may be better.  We cannot use
 1227:   ; fill-paragraph, because it removes the \ from the first comment
 1228:   ; line. Therefore we have to look for the first line of the comment
 1229:   ; and use fill-region.
 1230:   (interactive)
 1231:   (save-excursion
 1232:     (beginning-of-line)
 1233:     (while (and
 1234: 	     (= (forward-line -1) 0)
 1235: 	     (looking-at "[ \t]*\\\\g?[ \t]+")))
 1236:     (if (not (looking-at "[ \t]*\\\\g?[ \t]+"))
 1237: 	(forward-line 1))
 1238:     (let ((from (point))
 1239: 	  (to (save-excursion (forward-paragraph) (point))))
 1240:       (if (looking-at "[ \t]*\\\\g?[ \t]+")
 1241: 	  (progn (goto-char (match-end 0))
 1242: 		 (set-fill-prefix)
 1243: 		 (fill-region from to nil))))))
 1244: 
 1245: (defun forth-comment-indent ()
 1246:   (save-excursion
 1247:     (beginning-of-line)
 1248:     (if (looking-at ":[ \t]*")
 1249: 	(progn
 1250: 	  (end-of-line)
 1251: 	  (skip-chars-backward " \t\n")
 1252: 	  (1+ (current-column)))
 1253:       comment-column)))
 1254: 
 1255: 
 1256: ;; Forth commands
 1257: 
 1258: (defun forth-remove-tracers ()
 1259:   "Remove tracers of the form `~~ '. Queries the user for each occurrence."
 1260:   (interactive)
 1261:   (query-replace-regexp "\\(~~ \\| ~~$\\)" "" nil))
 1262: 
 1263: (defvar forth-program-name "gforth"
 1264:   "*Program invoked by the `run-forth' command.")
 1265: 
 1266: (defvar forth-band-name nil
 1267:   "*Band loaded by the `run-forth' command.")
 1268: 
 1269: (defvar forth-program-arguments nil
 1270:   "*Arguments passed to the Forth program by the `run-forth' command.")
 1271: 
 1272: (defun run-forth (command-line)
 1273:   "Run an inferior Forth process. Output goes to the buffer `*forth*'.
 1274: With argument, asks for a command line. Split up screen and run forth 
 1275: in the lower portion. The current-buffer when called will stay in the
 1276: upper portion of the screen, and all other windows are deleted.
 1277: Call run-forth again to make the *forth* buffer appear in the lower
 1278: part of the screen."
 1279:   (interactive
 1280:    (list (let ((default
 1281: 		 (or forth-process-command-line
 1282: 		     (forth-default-command-line))))
 1283: 	   (if current-prefix-arg
 1284: 	       (read-string "Run Forth: " default)
 1285: 	       default))))
 1286:   (setq forth-process-command-line command-line)
 1287:   (forth-start-process command-line)
 1288:   (forth-split)
 1289:   (forth-set-runlight forth-runlight:input))
 1290: 
 1291: (defun run-forth-if-not ()
 1292:   (if (not (forth-process-running-p))
 1293:       (run-forth forth-program-name)))
 1294: 
 1295: (defun reset-forth ()
 1296:   "Reset the Forth process."
 1297:   (interactive)
 1298:   (let ((process (get-process forth-program-name)))
 1299:     (cond ((or (not process)
 1300: 	       (not (eq (process-status process) 'run))
 1301: 	       (yes-or-no-p
 1302: "The Forth process is running, are you SURE you want to reset it? "))
 1303: 	   (message "Resetting Forth process...")
 1304: 	   (forth-reload)
 1305: 	   (message "Resetting Forth process...done")))))
 1306: 
 1307: (defun forth-default-command-line ()
 1308:   (concat forth-program-name
 1309: 	  (if forth-program-arguments
 1310: 	      (concat " " forth-program-arguments)
 1311: 	      "")))
 1312: 
 1313: ;;;; Internal Variables
 1314: 
 1315: (defvar forth-process-command-line nil
 1316:   "Command used to start the most recent Forth process.")
 1317: 
 1318: (defvar forth-previous-send ""
 1319:   "Most recent expression transmitted to the Forth process.")
 1320: 
 1321: (defvar forth-process-filter-queue '()
 1322:   "Queue used to synchronize filter actions properly.")
 1323: 
 1324: (defvar forth-prompt "ok"
 1325:   "The current forth prompt string.")
 1326: 
 1327: (defvar forth-start-hook nil
 1328:   "If non-nil, a procedure to call when the Forth process is started.
 1329: When called, the current buffer will be the Forth process-buffer.")
 1330: 
 1331: (defvar forth-signal-death-message nil
 1332:   "If non-nil, causes a message to be generated when the Forth process dies.")
 1333: 
 1334: (defvar forth-percent-height 50
 1335:   "Tells run-forth how high the upper window should be in percent.")
 1336: 
 1337: (defconst forth-runlight:input ?I
 1338:   "The character displayed when the Forth process is waiting for input.")
 1339: 
 1340: (defvar forth-mode-string ""
 1341:   "String displayed in the mode line when the Forth process is running.")
 1342: 
 1343: ;;;; Evaluation Commands
 1344: 
 1345: (defun forth-send-string (&rest strings)
 1346:   "Send the string arguments to the Forth process.
 1347: The strings are concatenated and terminated by a newline."
 1348:   (cond ((forth-process-running-p)
 1349: 	 (forth-send-string-1 strings))
 1350: 	((yes-or-no-p "The Forth process has died.  Reset it? ")
 1351: 	 (reset-forth)
 1352: 	 (goto-char (point-max))
 1353: 	 (forth-send-string-1 strings))))
 1354: 
 1355: (defun forth-send-string-1 (strings)
 1356:   (let ((string (apply 'concat strings)))
 1357:     (forth-send-string-2 string)))
 1358: 
 1359: (defun forth-send-string-2 (string)
 1360:   (let ((process (get-process forth-program-name)))
 1361:     (if (not (eq (current-buffer) (get-buffer forth-program-name)))
 1362: 	(progn
 1363: 	 (forth-process-filter-output string)
 1364: 	 (forth-process-filter:finish)))
 1365:     (send-string process (concat string "\n"))
 1366:     (if (eq (current-buffer) (process-buffer process))
 1367: 	(set-marker (process-mark process) (point)))))
 1368: 
 1369: 
 1370: (defun forth-send-region (start end)
 1371:   "Send the current region to the Forth process.
 1372: The region is sent terminated by a newline."
 1373:   (interactive "r")
 1374:   (let ((process (get-process forth-program-name)))
 1375:     (if (and process (eq (current-buffer) (process-buffer process)))
 1376: 	(progn (goto-char end)
 1377: 	       (set-marker (process-mark process) end))))
 1378:   (forth-send-string "\n" (buffer-substring start end) "\n"))
 1379: 
 1380: (defun forth-end-of-paragraph ()
 1381:   (if (looking-at "[\t\n ]+") (skip-chars-backward  "\t\n "))
 1382:   (if (not (re-search-forward "\n[ \t]*\n" nil t))
 1383:       (goto-char (point-max))))
 1384: 
 1385: (defun forth-send-paragraph ()
 1386:   "Send the current or the previous paragraph to the Forth process"
 1387:   (interactive)
 1388:   (let (end)
 1389:     (save-excursion
 1390:       (forth-end-of-paragraph)
 1391:       (skip-chars-backward  "\t\n ")
 1392:       (setq end (point))
 1393:       (if (re-search-backward "\n[ \t]*\n" nil t)
 1394: 	  (setq start (point))
 1395: 	(goto-char (point-min)))
 1396:       (skip-chars-forward  "\t\n ")
 1397:       (forth-send-region (point) end))))
 1398:   
 1399: (defun forth-send-buffer ()
 1400:   "Send the current buffer to the Forth process."
 1401:   (interactive)
 1402:   (if (eq (current-buffer) (forth-process-buffer))
 1403:       (error "Not allowed to send this buffer's contents to Forth"))
 1404:   (forth-send-region (point-min) (point-max)))
 1405: 
 1406: 
 1407: ;;;; Basic Process Control
 1408: 
 1409: (defun forth-start-process (command-line)
 1410:   (let ((buffer (get-buffer-create "*forth*")))
 1411:     (let ((process (get-buffer-process buffer)))
 1412:       (save-excursion
 1413: 	(set-buffer buffer)
 1414: 	(progn (if process (delete-process process))
 1415: 	       (goto-char (point-max))
 1416: 	       (setq mode-line-process '(": %s"))
 1417: 	       (add-to-global-mode-string 'forth-mode-string)
 1418: 	       (setq process
 1419: 		     (apply 'start-process
 1420: 			    (cons forth-program-name
 1421: 				  (cons buffer
 1422: 					(forth-parse-command-line
 1423: 					 command-line)))))
 1424: 	       (set-marker (process-mark process) (point-max))
 1425: 	       (forth-process-filter-initialize t)
 1426: 	       (forth-modeline-initialize)
 1427: 	       (set-process-sentinel process 'forth-process-sentinel)
 1428: 	       (set-process-filter process 'forth-process-filter)
 1429: 	       (run-hooks 'forth-start-hook)))
 1430:     buffer)))
 1431: 
 1432: (defun forth-parse-command-line (string)
 1433:   (setq string (substitute-in-file-name string))
 1434:   (let ((start 0)
 1435: 	(result '()))
 1436:     (while start
 1437:       (let ((index (string-match "[ \t]" string start)))
 1438: 	(setq start
 1439: 	      (cond ((not index)
 1440: 		     (setq result
 1441: 			   (cons (substring string start)
 1442: 				 result))
 1443: 		     nil)
 1444: 		    ((= index start)
 1445: 		     (string-match "[^ \t]" string start))
 1446: 		    (t
 1447: 		     (setq result
 1448: 			   (cons (substring string start index)
 1449: 				 result))
 1450: 		     (1+ index))))))
 1451:     (nreverse result)))
 1452: 
 1453: 
 1454: (defun forth-process-running-p ()
 1455:   "True iff there is a Forth process whose status is `run'."
 1456:   (let ((process (get-process forth-program-name)))
 1457:     (and process
 1458: 	 (eq (process-status process) 'run))))
 1459: 
 1460: (defun forth-process-buffer ()
 1461:   (let ((process (get-process forth-program-name)))
 1462:     (and process (process-buffer process))))
 1463: 
 1464: ;;;; Process Filter
 1465: 
 1466: (defun forth-process-sentinel (proc reason)
 1467:   (let ((inhibit-quit nil))
 1468:     (forth-process-filter-initialize (eq reason 'run))
 1469:     (if (eq reason 'run)
 1470: 	(forth-modeline-initialize)
 1471: 	(setq forth-mode-string "")))
 1472:   (if (and (not (memq reason '(run stop)))
 1473: 	   forth-signal-death-message)
 1474:       (progn (beep)
 1475: 	     (message
 1476: "The Forth process has died!  Do M-x reset-forth to restart it"))))
 1477: 
 1478: (defun forth-process-filter-initialize (running-p)
 1479:   (setq forth-process-filter-queue (cons '() '()))
 1480:   (setq forth-prompt "ok"))
 1481: 
 1482: 
 1483: (defun forth-process-filter (proc string)
 1484:   (forth-process-filter-output string)
 1485:   (forth-process-filter:finish))
 1486: 
 1487: (defun forth-process-filter:enqueue (action)
 1488:   (let ((next (cons action '())))
 1489:     (if (cdr forth-process-filter-queue)
 1490: 	(setcdr (cdr forth-process-filter-queue) next)
 1491: 	(setcar forth-process-filter-queue next))
 1492:     (setcdr forth-process-filter-queue next)))
 1493: 
 1494: (defun forth-process-filter:finish ()
 1495:   (while (car forth-process-filter-queue)
 1496:     (let ((next (car forth-process-filter-queue)))
 1497:       (setcar forth-process-filter-queue (cdr next))
 1498:       (if (not (cdr next))
 1499: 	  (setcdr forth-process-filter-queue '()))
 1500:       (apply (car (car next)) (cdr (car next))))))
 1501: 
 1502: ;;;; Process Filter Output
 1503: 
 1504: (defun forth-process-filter-output (&rest args)
 1505:   (if (not (and args
 1506: 		(null (cdr args))
 1507: 		(stringp (car args))
 1508: 		(string-equal "" (car args))))
 1509:       (forth-process-filter:enqueue
 1510:        (cons 'forth-process-filter-output-1 args))))
 1511: 
 1512: (defun forth-process-filter-output-1 (&rest args)
 1513:   (save-excursion
 1514:     (forth-goto-output-point)
 1515:     (apply 'insert-before-markers args)))
 1516: 
 1517: (defun forth-guarantee-newlines (n)
 1518:   (save-excursion
 1519:     (forth-goto-output-point)
 1520:     (let ((stop nil))
 1521:       (while (and (not stop)
 1522: 		  (bolp))
 1523: 	(setq n (1- n))
 1524: 	(if (bobp)
 1525: 	    (setq stop t)
 1526: 	  (backward-char))))
 1527:     (forth-goto-output-point)
 1528:     (while (> n 0)
 1529:       (insert-before-markers ?\n)
 1530:       (setq n (1- n)))))
 1531: 
 1532: (defun forth-goto-output-point ()
 1533:   (let ((process (get-process forth-program-name)))
 1534:     (set-buffer (process-buffer process))
 1535:     (goto-char (process-mark process))))
 1536: 
 1537: (defun forth-modeline-initialize ()
 1538:   (setq forth-mode-string "  "))
 1539: 
 1540: (defun forth-set-runlight (runlight)
 1541:   (aset forth-mode-string 0 runlight)
 1542:   (forth-modeline-redisplay))
 1543: 
 1544: (defun forth-modeline-redisplay ()
 1545:   (save-excursion (set-buffer (other-buffer)))
 1546:   (set-buffer-modified-p (buffer-modified-p))
 1547:   (sit-for 0))
 1548: 
 1549: ;;;; Process Filter Operations
 1550: 
 1551: (defun add-to-global-mode-string (x)
 1552:   (cond ((null global-mode-string)
 1553: 	 (setq global-mode-string (list "" x " ")))
 1554: 	((not (memq x global-mode-string))
 1555: 	 (setq global-mode-string
 1556: 	       (cons ""
 1557: 		     (cons x
 1558: 			   (cons " "
 1559: 				 (if (equal "" (car global-mode-string))
 1560: 				     (cdr global-mode-string)
 1561: 				     global-mode-string))))))))
 1562: 
 1563: 
 1564: ;; Misc
 1565: 
 1566: (setq auto-mode-alist (append auto-mode-alist
 1567: 				'(("\\.fs$" . forth-mode))))
 1568: 
 1569: (defun forth-split ()
 1570:   (interactive)
 1571:   (forth-split-1 "*forth*"))
 1572: 
 1573: (defun forth-split-1 (buffer)
 1574:   (if (not (eq (window-buffer) (get-buffer buffer)))
 1575:       (progn
 1576: 	(delete-other-windows)
 1577: 	(split-window-vertically
 1578: 	 (/ (* (screen-height) forth-percent-height) 100))
 1579: 	(other-window 1)
 1580: 	(switch-to-buffer buffer)
 1581: 	(goto-char (point-max))
 1582: 	(other-window 1))))
 1583:     
 1584: (defun forth-reload ()
 1585:   (interactive)
 1586:   (let ((process (get-process forth-program-name)))
 1587:     (if process (kill-process process t)))
 1588:   (sleep-for 0 100)
 1589:   (forth-mode))
 1590: 
 1591: 
 1592: ;; Special section for forth-help
 1593: 
 1594: (defvar forth-help-buffer "*Forth-help*"
 1595:   "Buffer used to display the requested documentation.")
 1596: 
 1597: (defvar forth-help-load-path nil
 1598:   "List of directories to search through to find *.doc
 1599:  (forth-help-file-suffix) files. Nil means current default directory.
 1600:  The specified directories must contain at least one .doc file. If it
 1601:  does not and you still want the load-path to scan that directory, create
 1602:  an empty file dummy.doc.")
 1603: 
 1604: (defvar forth-help-file-suffix "*.doc"
 1605:   "The file names to search for in each directory.")
 1606: 
 1607: (setq forth-search-command-prefix "grep -n \"^    [^(]* ")
 1608: (defvar forth-search-command-suffix "/dev/null")
 1609: (defvar forth-grep-error-regexp ": No such file or directory")
 1610: 
 1611: (defun forth-function-called-at-point ()
 1612:   "Return the space delimited word a point."
 1613:   (save-excursion
 1614:     (save-restriction
 1615:       (narrow-to-region (max (point-min) (- (point) 1000)) (point-max))
 1616:       (skip-chars-backward "^ \t\n" (point-min))
 1617:       (if (looking-at "[ \t\n]")
 1618: 	  (forward-char 1))
 1619:       (let (obj (p (point)))
 1620: 	(skip-chars-forward "^ \t\n")
 1621: 	(buffer-substring p (point))))))
 1622: 
 1623: (defun forth-help-names-extend-comp (path-list result)
 1624:   (cond ((null path-list) result)
 1625: 	((null (car path-list))
 1626: 	 (forth-help-names-extend-comp (cdr path-list) 
 1627: 	       (concat result forth-help-file-suffix " ")))
 1628: 	(t (forth-help-names-extend-comp
 1629: 	    (cdr path-list) (concat result
 1630: 				    (expand-file-name (car path-list)) "/"
 1631: 				    forth-help-file-suffix " ")))))
 1632: 
 1633: (defun forth-help-names-extended ()
 1634:   (if forth-help-load-path
 1635:       (forth-help-names-extend-comp forth-help-load-path "")
 1636:     (error "forth-help-load-path not specified")))
 1637: 
 1638: 
 1639: ;(define-key forth-mode-map "\C-hf" 'forth-documentation)
 1640: 
 1641: (defun forth-documentation (function)
 1642:   "Display the full documentation of FORTH word."
 1643:   (interactive
 1644:    (let ((fn (forth-function-called-at-point))
 1645: 	 (enable-recursive-minibuffers t)	     
 1646: 	 search-list
 1647: 	 val)
 1648:      (setq val (read-string (format "Describe forth word (default %s): " fn)))
 1649:      (list (if (equal val "") fn val))))
 1650:   (forth-get-doc (concat forth-search-command-prefix
 1651: 			 (grep-regexp-quote (concat function " ("))
 1652: 			 "[^)]*\-\-\" " (forth-help-names-extended)
 1653: 			 forth-search-command-suffix))
 1654:   (message "C-x C-m switches back to the forth interaction window"))
 1655: 
 1656: (defun forth-get-doc (command)
 1657:   "Display the full documentation of command."
 1658:   (let ((curwin (get-buffer-window (window-buffer)))
 1659: 	reswin
 1660: 	pointmax)
 1661:     (with-output-to-temp-buffer forth-help-buffer
 1662:       (progn
 1663: 	(call-process "sh" nil forth-help-buffer t "-c" command)
 1664: 	(setq reswin (get-buffer-window forth-help-buffer))))
 1665:     (setq reswin (get-buffer-window forth-help-buffer))
 1666:     (select-window reswin)
 1667:     (save-excursion
 1668:       (goto-char (setq pointmax (point-max)))
 1669:       (insert "--------------------\n\n"))
 1670:     (let (fd doc) 
 1671:       (while (setq fd (forth-get-file-data pointmax))
 1672: 	(setq doc (forth-get-doc-string fd))
 1673: 	(save-excursion
 1674: 	  (goto-char (point-max))
 1675: 	  (insert (substring (car fd) (string-match "[^/]*$" (car fd)))
 1676: 		  ":\n\n" doc "\n")))
 1677:       (if (not doc)
 1678: 	  (progn (goto-char (point-max)) (insert "Not found"))))
 1679:     (select-window curwin)))
 1680:   
 1681: (defun forth-skip-error-lines ()
 1682:   (let ((lines 0))
 1683:     (save-excursion
 1684:       (while (re-search-forward forth-grep-error-regexp nil t)
 1685: 	(beginning-of-line)
 1686: 	(forward-line 1)
 1687: 	(setq lines (1+ lines))))
 1688:     (forward-line lines)))
 1689: 
 1690: (defun forth-get-doc-string (fd)
 1691:   "Find file (car fd) and extract documentation from line (nth 1 fd)."
 1692:   (let (result)
 1693:     (save-window-excursion
 1694:       (find-file (car fd))
 1695:       (goto-line (nth 1 fd))
 1696:       (if (not (eq (nth 1 fd) (1+ (count-lines (point-min) (point)))))
 1697: 	  (error "forth-get-doc-string: serious error"))
 1698:       (if (not (re-search-backward "\n[\t ]*\n" nil t))
 1699: 	  (goto-char (point-min))
 1700: 	(goto-char (match-end 0)))
 1701:       (let ((p (point)))
 1702: 	(if (not (re-search-forward "\n[\t ]*\n" nil t))
 1703: 	    (goto-char (point-max)))
 1704: 	(setq result (buffer-substring p (point))))
 1705:       (bury-buffer (current-buffer)))
 1706:     result))
 1707: 
 1708: (defun forth-get-file-data (limit)
 1709:   "Parse grep output and return '(filename line#) list. Return nil when
 1710:  passing limit."
 1711:   (forth-skip-error-lines)
 1712:   (if (< (point) limit)
 1713:       (let ((result (forth-get-file-data-cont limit)))
 1714: 	(forward-line 1)
 1715: 	(beginning-of-line)
 1716: 	result)))
 1717: 
 1718: (defun forth-get-file-data-cont (limit)
 1719:   (let (result)
 1720:     (let ((p (point)))
 1721:       (skip-chars-forward "^:")
 1722:       (setq result (buffer-substring p (point))))
 1723:     (if (< (point) limit)
 1724: 	(let ((p (1+ (point))))
 1725: 	  (forward-char 1)
 1726: 	  (skip-chars-forward "^:")
 1727: 	  (list result (string-to-int (buffer-substring p (point))))))))
 1728: 
 1729: (defun grep-regexp-quote (str)
 1730:   (let ((i 0) (m 1) (res ""))
 1731:     (while (/= m 0)
 1732:       (setq m (string-to-char (substring str i)))
 1733:       (if (/= m 0)
 1734: 	  (progn
 1735: 	    (setq i (1+ i))
 1736: 	    (if (string-match (regexp-quote (char-to-string m))
 1737: 			      ".*\\^$[]")
 1738: 		(setq res (concat res "\\")))
 1739: 	    (setq res (concat res (char-to-string m))))))
 1740:     res))
 1741: 
 1742: 
 1743: (define-key forth-mode-map "\C-x\C-e" 'compile)
 1744: (define-key forth-mode-map "\C-x\C-n" 'next-error)
 1745: (require 'compile "compile")
 1746: 
 1747: (defvar forth-compile-command "gforth ")
 1748: ;(defvar forth-compilation-window-percent-height 30)
 1749: 
 1750: (defun forth-compile (command)
 1751:   (interactive (list (setq forth-compile-command (read-string "Compile command: " forth-compile-command))))
 1752:   (forth-split-1 "*compilation*")
 1753:   (setq ctools-compile-command command)
 1754:   (compile1 ctools-compile-command "No more errors"))
 1755: 
 1756: 
 1757: ;;; Forth menu
 1758: ;;; Mikael Karlsson <qramika@eras70.ericsson.se>
 1759: 
 1760: (cond ((string-match "XEmacs\\|Lucid" emacs-version)
 1761:        (require 'func-menu)
 1762: 
 1763:   (defconst fume-function-name-regexp-forth
 1764:    "^\\(:\\)[ \t]+\\([^ \t]*\\)"
 1765:    "Expression to get word definitions in Forth.")
 1766: 
 1767:   (setq fume-function-name-regexp-alist
 1768:       (append '((forth-mode . fume-function-name-regexp-forth) 
 1769:              ) fume-function-name-regexp-alist))
 1770: 
 1771:   ;; Find next forth word in the buffer
 1772:   (defun fume-find-next-forth-function-name (buffer)
 1773:     "Searches for the next forth word in BUFFER."
 1774:     (set-buffer buffer)
 1775:     (if (re-search-forward fume-function-name-regexp nil t)
 1776:       (let ((beg (match-beginning 2))
 1777:             (end (match-end 2)))
 1778:         (cons (buffer-substring beg end) beg))))
 1779: 
 1780:   (setq fume-find-function-name-method-alist
 1781:   (append '((forth-mode    . fume-find-next-forth-function-name))))
 1782: 
 1783:   ))
 1784: ;;; End Forth menu
 1785: 
 1786: ;;; File folding of forth-files
 1787: ;;; uses outline
 1788: ;;; Toggle activation with M-x fold-f (when editing a forth-file) 
 1789: ;;; Use f9 to expand, f10 to hide, Or the menubar in xemacs
 1790: ;;;
 1791: ;;; Works most of the times but loses sync with the cursor occasionally 
 1792: ;;; Could be improved by also folding on comments
 1793: 
 1794: (require 'outline)
 1795: 
 1796: (defun f-outline-level ()
 1797: 	(cond	((looking-at "\\`\\\\")
 1798: 			0)
 1799: 		((looking-at "\\\\ SEC")
 1800: 			0)
 1801: 		((looking-at "\\\\ \\\\ .*")
 1802: 			0)
 1803: 		((looking-at "\\\\ DEFS")
 1804: 			1)
 1805: 		((looking-at "\\/\\* ")
 1806: 			1)
 1807: 		((looking-at ": .*")
 1808: 			1)
 1809: 		((looking-at "\\\\G")
 1810: 			2)
 1811: 		((looking-at "[ \t]+\\\\")
 1812: 			3))
 1813: )			
 1814: 
 1815: (defun fold-f  ()
 1816:    (interactive)
 1817:    (add-hook 'outline-minor-mode-hook 'hide-body)
 1818: 
 1819:    ; outline mode header start, i.e. find word definitions
 1820: ;;;   (setq  outline-regexp  "^\\(:\\)[ \t]+\\([^ \t]*\\)")
 1821:    (setq  outline-regexp  "\\`\\\\\\|:\\|\\\\ SEC\\|\\\\G\\|[ \t]+\\\\\\|\\\\ DEFS\\|\\/\\*\\|\\\\ \\\\ .*")
 1822:    (setq outline-level 'f-outline-level)
 1823: 
 1824:    (outline-minor-mode)
 1825:    (define-key outline-minor-mode-map '(shift up) 'hide-sublevels)
 1826:    (define-key outline-minor-mode-map '(shift right) 'show-children)
 1827:    (define-key outline-minor-mode-map '(shift left) 'hide-subtree)
 1828:    (define-key outline-minor-mode-map '(shift down) 'show-subtree)
 1829: 
 1830: )
 1831: 
 1832: ;;(define-key global-map '(shift up) 'fold-f)
 1833: 
 1834: ;;; end file folding
 1835: 
 1836: ;;; func-menu is a package that scans your source file for function definitions
 1837: ;;; and makes a menubar entry that lets you jump to any particular function
 1838: ;;; definition by selecting it from the menu.  The following code turns this on
 1839: ;;; for all of the recognized languages.  Scanning the buffer takes some time,
 1840: ;;; but not much.
 1841: ;;;
 1842: (cond ((string-match "XEmacs\\|Lucid" emacs-version)
 1843:        (require 'func-menu)
 1844: ;;       (define-key global-map 'f8 'function-menu)
 1845:        (add-hook 'find-fible-hooks 'fume-add-menubar-entry)
 1846: ;       (define-key global-map "\C-cg" 'fume-prompt-function-goto)
 1847: ;       (define-key global-map '(shift button3) 'mouse-function-menu)
 1848: ))
 1849: 
 1850: ;;; gforth.el ends here

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>