File:  [gforth] / gforth / gforth.el
Revision 1.51: download - view: text, annotated - select for diffs
Sun Jun 17 16:13:50 2001 UTC (19 years, 11 months ago) by dvdkhlng
Branches: MAIN
CVS tags: HEAD
Improved interface for customizing hilighting/indentation.
Added imenu and speedbar support (needs further refinement, though).

    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: (defun forth-next-definition-starter ()
  532:   (progn
  533:     (let* ((regexp (car forth-compiled-defining-words))
  534: 	   (pos (re-search-forward regexp (point-max) t)))
  535:       (message "regexp: %s pos:%s" regexp pos)
  536:       (if pos
  537: 	  (if (or (text-property-not-all (match-beginning 0) (match-end 0) 
  538: 					  'forth-parsed nil)
  539: 		   (text-property-not-all (match-beginning 0) (match-end 0)
  540: 					  'forth-state nil)) 
  541: 	      (forth-next-definition-starter)
  542: 	    t)
  543: 	nil))))
  544: 
  545: (defun forth-create-index ()
  546:   (let* ((defwords 
  547: 	  (forth-filter (lambda (word) 
  548: 			  (and (eq (nth 1 word) 'definition-starter)
  549: 			       (> (length word) 3)))
  550: 			forth-words))
  551: 	 (forth-compiled-defining-words (forth-compile-wordlist defwords))
  552: 	 (index nil))
  553:     (goto-char (point-min))
  554:     (while (forth-next-definition-starter)
  555:       (if (looking-at "[ \t]*\\([^ \t\n]+\\)")
  556: 	  (setq index (cons (cons (match-string 1) (point)) index))))
  557:     (message "index: %s" index)
  558:     index))
  559: 
  560: (speedbar-add-supported-extension ".fs")
  561: (speedbar-add-supported-extension ".fb")
  562: 
  563: ;; (require 'profile)
  564: ;; (setq profile-functions-list '(forth-set-word-properties forth-next-known-forth-word forth-update-properties forth-delete-properties forth-get-regexp-branch))
  565: 
  566: ;;; Indentation
  567: ;;;
  568: 
  569: (defvar forth-indent-words nil 
  570:   "List of words that have indentation behaviour.
  571: Each element of `forth-indent-words' should have the form
  572:    (MATCHER INDENT1 INDENT2) 
  573:   
  574: MATCHER is either a list of strings to match, or a REGEXP.
  575:    If it's a REGEXP, it should not be surrounded by `\\<` or `\\>`, since 
  576:    that'll be done automatically by the search routines.
  577: 
  578: INDENT1 specifies how to indent a word that's located at a line's begin,
  579:    following any number of whitespaces.
  580: 
  581: INDENT2 specifies how to indent words that are not located at a line's begin.
  582: 
  583: INDENT1 and INDENT2 are indentation specifications of the form
  584:    (SELF-INDENT . NEXT-INDENT), where SELF-INDENT is a numerical value, 
  585:    specifying how the matching line and all following lines are to be 
  586:    indented, relative to previous lines. NEXT-INDENT specifies how to indent 
  587:    following lines, relative to the matching line.
  588:   
  589:    Even values of SELF-INDENT and NEXT-INDENT correspond to multiples of
  590:    `forth-indent-level'. Odd values get an additional 
  591:    `forth-minor-indent-level' added/substracted. Eg a value of -2 indents
  592:    1 * forth-indent-level  to the left, wheras 3 indents 
  593:    1 * forth-indent-level + forth-minor-indent-level  columns to the right.")
  594: 
  595: (setq forth-indent-words
  596:       '(((":" ":noname" "code" "if" "begin" "do" "?do" "+do" "-do" "u+do"
  597: 	  "u-do" "?dup-if" "?dup-0=-if" "case" "of" "try" "struct" 
  598: 	  "[if]" "[ifdef]" "[ifundef]" "[begin]" "[for]" "[do]" "[?do]"
  599: 	  "class" "interface" "m:" ":m")
  600: 	 (0 . 2) (0 . 2))
  601: 	((";" ";m") (-2 . 0) (0 . -2))
  602: 	(("end-code" "again" "repeat" "then" "endtry" "endcase" "endof" 
  603: 	  "end-struct" "[then]" "[endif]" "[loop]" "[+loop]" "[next]" 
  604: 	  "[until]" "[repeat]" "[again]" "end-class" "end-interface"
  605: 	  "end-class-noname" "end-interface-noname" "loop"
  606: 	  "class;")
  607: 	 (-2 . 0) (0 . -2))
  608: 	(("protected" "public" "how:") (-1 . 1) (0 . 0))
  609: 	(("+loop" "-loop" "until") (-2 . 0) (-2 . 0))
  610: 	(("else" "recover" "[else]") (-2 . 2) (0 . 0))
  611: 	(("while" "does>" "[while]") (-1 . 1) (0 . 0))
  612: 	(("\\g") (-2 . 2) (0 . 0))))
  613: 
  614: (defvar forth-local-indent-words nil 
  615:   "List of Forth words to prepend to `forth-indent-words', when a forth-mode
  616: buffer is created. Should be set by a Forth source, using a local variables 
  617: list at the end of the file (\"Local Variables: ... forth-local-words: ... 
  618: End:\" construct).")
  619: 
  620: (defvar forth-custom-indent-words nil
  621:   "List of Forth words to prepend to `forth-indent-words'. Should be set in
  622:  your .emacs.")
  623: 
  624: (defvar forth-indent-level 4
  625:   "Indentation of Forth statements.")
  626: (defvar forth-minor-indent-level 2
  627:   "Minor indentation of Forth statements.")
  628: (defvar forth-compiled-indent-words nil)
  629: 
  630: ;; Return, whether `pos' is the first forth word on its line
  631: (defun forth-first-word-on-line-p (pos)
  632:   (save-excursion
  633:     (beginning-of-line)
  634:     (skip-chars-forward " \t")
  635:     (= pos (point))))
  636: 
  637: ;; Return indentation data (SELF-INDENT . NEXT-INDENT) of next known 
  638: ;; indentation word, or nil if there is no word up to `to'. 
  639: ;; Position `point' at location just after found word, or at `to'. Parsed 
  640: ;; ranges of text will not be taken into consideration!
  641: (defun forth-next-known-indent-word (to)
  642:   (if (<= (point) to)
  643:       (progn
  644: 	(let* ((regexp (car forth-compiled-indent-words))
  645: 	       (pos (re-search-forward regexp to t)))
  646: 	  (if pos
  647: 	      (if (text-property-not-all (match-beginning 0) (match-end 0) 
  648: 					 'forth-parsed nil)
  649: 		  (forth-next-known-indent-word to)
  650: 		(let* ((branch (forth-get-regexp-branch))
  651: 		       (descr (cdr forth-compiled-indent-words))
  652: 		       (indent (cdr (assoc branch descr))))
  653: 		  (if (forth-first-word-on-line-p (match-beginning 0))
  654: 		      (nth 0 indent) (nth 1 indent))))
  655: 	    nil)))
  656:     nil))
  657:   
  658: ;; Translate indentation value `indent' to indentation column. Multiples of
  659: ;; 2 correspond to multiples of `forth-indent-level'. Odd numbers get an
  660: ;; additional `forth-minor-indent-level' added (or substracted).
  661: (defun forth-convert-to-column (indent)
  662:   (let* ((sign (if (< indent 0) -1 1))
  663: 	 (value (abs indent))
  664: 	 (major (* (/ value 2) forth-indent-level))
  665: 	 (minor (* (% value 2) forth-minor-indent-level)))
  666:     (* sign (+ major minor))))
  667: 
  668: ;; Return the column increment, that the current line of forth code does to
  669: ;; the current or following lines. `which' specifies which indentation values
  670: ;; to use. 0 means the indentation of following lines relative to current 
  671: ;; line, 1 means the indentation of the current line relative to the previous 
  672: ;; line. Return `nil', if there are no indentation words on the current line.
  673: (defun forth-get-column-incr (which)
  674:   (save-excursion
  675:     (let ((regexp (car forth-compiled-indent-words))
  676: 	  (word-indent)
  677: 	  (self-indent nil)
  678: 	  (next-indent nil)
  679: 	  (to (save-excursion (end-of-line) (point))))
  680:       (beginning-of-line)
  681:       (while (setq word-indent (forth-next-known-indent-word to))
  682: 	(let* ((self-incr (car word-indent))
  683: 	       (next-incr (cdr word-indent))
  684: 	       (self-column-incr (forth-convert-to-column self-incr))
  685: 	       (next-column-incr (forth-convert-to-column next-incr)))
  686: 	  (setq next-indent (if next-indent next-indent 0))
  687: 	  (setq self-indent (if self-indent self-indent 0))
  688: 	  (if (or (and (> next-indent 0) (< self-column-incr 0))
  689: 		  (and (< next-indent 0) (> self-column-incr 0)))
  690: 	      (setq next-indent (+ next-indent self-column-incr))
  691: 	    (setq self-indent (+ self-indent self-column-incr)))
  692: 	  (setq next-indent (+ next-indent next-column-incr))))
  693:       (nth which (list self-indent next-indent)))))
  694: 
  695: ;; Find previous line that contains indentation words, return the column,
  696: ;; to which following text should be indented to.
  697: (defun forth-get-anchor-column ()
  698:   (save-excursion
  699:     (if (/= 0 (forward-line -1)) 0
  700:       (let ((next-indent)
  701: 	    (self-indent))
  702: 	(while (not (or (setq indent (forth-get-column-incr 1))
  703: 			(<= (point) (point-min))))
  704: 	  (forward-line -1))
  705: 	(+ (current-indentation) (if indent indent 0))))))
  706: 
  707: (defun forth-indent-line (&optional flag)
  708:   "Correct indentation of the current Forth line."
  709:   (let* ((anchor (forth-get-anchor-column))
  710: 	 (column-incr (forth-get-column-incr 0)))
  711:     (forth-indent-to (if column-incr (+ anchor column-incr) anchor))))
  712: 
  713: (defun forth-current-column ()
  714:   (- (point) (save-excursion (beginning-of-line) (point))))
  715: (defun forth-current-indentation ()
  716:   (- (save-excursion (beginning-of-line) (forward-to-indentation 0) (point))
  717:      (save-excursion (beginning-of-line) (point))))
  718: 
  719: (defun forth-indent-to (x)
  720:   (let ((p nil))
  721:     (setq p (- (forth-current-column) (forth-current-indentation)))
  722:     (forth-delete-indentation)
  723:     (beginning-of-line)
  724:     (indent-to x)
  725:     (if (> p 0) (forward-char p))))
  726: 
  727: (defun forth-delete-indentation ()
  728:   (save-excursion
  729:     (delete-region 
  730:      (progn (beginning-of-line) (point)) 
  731:      (progn (back-to-indentation) (point)))))
  732: 
  733: (defun forth-indent-command ()
  734:   (interactive)
  735:   (forth-indent-line t))
  736: 
  737: ;; remove trailing whitespaces in current line
  738: (defun forth-remove-trailing ()
  739:   (save-excursion
  740:     (end-of-line)
  741:     (delete-region (point) (progn (skip-chars-backward " \t") (point)))))
  742: 
  743: ;; insert newline, removing any trailing whitespaces in the current line
  744: (defun forth-newline-remove-trailing ()
  745:   (save-excursion
  746:     (delete-region (point) (progn (skip-chars-backward " \t") (point))))
  747:   (newline))
  748: ;  (let ((was-point (point-marker)))
  749: ;    (unwind-protect 
  750: ;	(progn (forward-line -1) (forth-remove-trailing))
  751: ;      (goto-char (was-point)))))
  752: 
  753: ;; workaround for bug in `reindent-then-newline-and-indent'
  754: (defun forth-reindent-then-newline-and-indent ()
  755:   (interactive "*")
  756:   (indent-according-to-mode)
  757:   (forth-newline-remove-trailing)
  758:   (indent-according-to-mode))
  759: 
  760: ;;; end hilighting/indentation
  761: 
  762: ;;; Block file encoding/decoding  (dk)
  763: ;;;
  764: 
  765: (defconst forth-c/l 64 "Number of characters per block line")
  766: (defconst forth-l/b 16 "Number of lines per block")
  767: 
  768: ;; Check whether the unconverted block file line, point is in, does not
  769: ;; contain `\n' and `\t' characters.
  770: (defun forth-check-block-line (line)
  771:   (let ((end (save-excursion (beginning-of-line) (forward-char forth-c/l)
  772: 			     (point))))
  773:     (save-excursion 
  774:       (beginning-of-line)
  775:       (when (search-forward "\n" end t)
  776: 	(message "Warning: line %i contains newline character #10" line)
  777: 	(ding t))
  778:       (beginning-of-line)
  779:       (when (search-forward "\t" end t)
  780: 	(message "Warning: line %i contains tab character #8" line)
  781: 	(ding t)))))
  782: 
  783: (defun forth-convert-from-block (from to)
  784:   "Convert block file format to stream source in current buffer."
  785:   (let ((line (count-lines (point-min) from)))
  786:     (save-excursion
  787:       (goto-char from)
  788:       (set-mark to)
  789:       (while (< (+ (point) forth-c/l) (mark t))
  790: 	(setq line (1+ line))
  791: 	(forth-check-block-line line)
  792: 	(forward-char forth-c/l)
  793: 	(forth-newline-remove-trailing))
  794:       (when (= (+ (point) forth-c/l) (mark t))
  795: 	(forth-remove-trailing))
  796:       (mark t))))
  797: 
  798: ;; Pad a line of a block file up to `forth-c/l' characters, positioning `point'
  799: ;; at the end of line.
  800: (defun forth-pad-block-line ()
  801:   (save-excursion
  802:     (end-of-line)
  803:     (if (<= (current-column) forth-c/l)
  804: 	(move-to-column forth-c/l t)
  805:       (message "Line %i longer than %i characters, truncated"
  806: 	       (count-lines (point-min) (point)) forth-c/l)
  807:       (ding t)
  808:       (move-to-column forth-c/l t)
  809:       (delete-region (point) (progn (end-of-line) (point))))))
  810: 
  811: ;; Replace tab characters in current line by spaces.
  812: (defun forth-convert-tabs-in-line ()
  813:   (save-excursion
  814:     (beginning-of-line)
  815:     (while (search-forward "\t" (save-excursion (end-of-line) (point)) t)
  816:       (backward-char)
  817:       (delete-region (point) (1+ (point)))
  818:       (insert-char ?\  (- tab-width (% (current-column) tab-width))))))
  819: 
  820: ;; Delete newline at end of current line, concatenating it with the following
  821: ;; line. Place `point' at end of newly formed line.
  822: (defun forth-delete-newline ()
  823:   (end-of-line)
  824:   (delete-region (point) (progn (beginning-of-line 2) (point))))
  825: 
  826: (defun forth-convert-to-block (from to &optional original-buffer) 
  827:   "Convert range of text to block file format in current buffer."
  828:   (let* ((lines 0)) ; I have to count lines myself, since `count-lines' has
  829: 		    ; problems with trailing newlines...
  830:     (save-excursion
  831:       (goto-char from)
  832:       (set-mark to)
  833:       ;; pad lines to full length (`forth-c/l' characters per line)
  834:       (while (< (save-excursion (end-of-line) (point)) (mark t))
  835: 	(setq lines (1+ lines))
  836: 	(forth-pad-block-line)
  837: 	(forth-convert-tabs-in-line)
  838: 	(forward-line))
  839:       ;; also make sure the last line is padded, if `to' is at its end
  840:       (end-of-line)
  841:       (when (= (point) (mark t))
  842: 	(setq lines (1+ lines))
  843: 	(forth-pad-block-line)
  844: 	(forth-convert-tabs-in-line))
  845:       ;; remove newlines between lines
  846:       (goto-char from)
  847:       (while (< (save-excursion (end-of-line) (point)) (mark t))
  848: 	(forth-delete-newline))
  849:       ;; append empty lines, until last block is complete
  850:       (goto-char (mark t))
  851:       (let* ((required (* (/ (+ lines (1- forth-l/b)) forth-l/b) forth-l/b))
  852: 	     (pad-lines (- required lines)))
  853: 	(while (> pad-lines 0)
  854: 	  (insert-char ?\  forth-c/l)
  855: 	  (setq pad-lines (1- pad-lines))))
  856:       (point))))
  857: 
  858: (defun forth-detect-block-file-p ()
  859:   "Return non-nil if the current buffer is in block file format. Detection is
  860: done by checking whether the first line has 1024 characters or more."
  861:   (save-restriction 
  862:     (widen)
  863:     (save-excursion
  864:        (beginning-of-buffer)
  865:        (end-of-line)
  866:        (>= (current-column) 1024))))
  867: 
  868: ;; add block file conversion routines to `format-alist'
  869: (defconst forth-block-format-description
  870:   '(forth-blocks "Forth block source file" nil 
  871: 		 forth-convert-from-block forth-convert-to-block 
  872: 		 t normal-mode))
  873: (unless (memq forth-block-format-description format-alist)
  874:   (setq format-alist (cons forth-block-format-description format-alist)))
  875: 
  876: ;;; End block file encoding/decoding
  877: 
  878: ;;; Block file editing
  879: ;;;
  880: (defvar forth-overlay-arrow-string ">>")
  881: (defvar forth-block-base 1 "Number of first block in block file")
  882: (defvar forth-show-screen nil
  883:   "Non-nil means to show screen starts and numbers (for block files)")
  884: (defvar forth-warn-long-lines nil
  885:   "Non-nil means to warn about lines that are longer than 64 characters")
  886: 
  887: (defvar forth-screen-marker nil)
  888: 
  889: (defun forth-update-show-screen ()
  890:   "If `forth-show-screen' is non-nil, put overlay arrow to start of screen, 
  891: `point' is in. If arrow now points to different screen than before, display 
  892: screen number."
  893:   (if (not forth-show-screen)
  894:       (setq overlay-arrow-string nil)
  895:     (save-excursion
  896:       (let* ((line (count-lines (point-min) (min (point-max) (1+ (point)))))
  897: 	     (first-line (1+ (* (/ (1- line) forth-l/b) forth-l/b)))
  898: 	     (scr (+ forth-block-base (/ first-line forth-l/b))))
  899: 	(setq overlay-arrow-string forth-overlay-arrow-string)
  900: 	(goto-line first-line)
  901: 	(setq overlay-arrow-position forth-screen-marker)
  902: 	(set-marker forth-screen-marker 
  903: 		    (save-excursion (goto-line first-line) (point)))
  904: 	(setq forth-screen-number-string (format "%d" scr))))))
  905: 
  906: (add-hook 'forth-motion-hooks 'forth-update-show-screen)
  907: 
  908: (defun forth-update-warn-long-lines ()
  909:   "If `forth-warn-long-lines' is non-nil, display a warning whenever a line
  910: exceeds 64 characters."
  911:   (when forth-warn-long-lines
  912:     (when (> (save-excursion (end-of-line) (current-column)) forth-c/l)
  913:       (message "Warning: current line exceeds %i characters"
  914: 	       forth-c/l))))
  915: 
  916: (add-hook 'forth-motion-hooks 'forth-update-warn-long-lines)
  917:     
  918: ;;; End block file editing
  919: 
  920: 
  921: (defvar forth-mode-abbrev-table nil
  922:   "Abbrev table in use in Forth-mode buffers.")
  923: 
  924: (define-abbrev-table 'forth-mode-abbrev-table ())
  925: 
  926: (defvar forth-mode-map nil
  927:   "Keymap used in Forth mode.")
  928: 
  929: (if (not forth-mode-map)
  930:     (setq forth-mode-map (make-sparse-keymap)))
  931: 
  932: ;(define-key forth-mode-map "\M-\C-x" 'compile)
  933: (define-key forth-mode-map "\C-x\\" 'comment-region)
  934: (define-key forth-mode-map "\C-x~" 'forth-remove-tracers)
  935: (define-key forth-mode-map "\e\C-m" 'forth-send-paragraph)
  936: (define-key forth-mode-map "\eo" 'forth-send-buffer)
  937: (define-key forth-mode-map "\C-x\C-m" 'forth-split)
  938: (define-key forth-mode-map "\e " 'forth-reload)
  939: (define-key forth-mode-map "\t" 'forth-indent-command)
  940: (define-key forth-mode-map "\C-m" 'forth-reindent-then-newline-and-indent)
  941: (define-key forth-mode-map "\M-q" 'forth-fill-paragraph)
  942: (define-key forth-mode-map "\e." 'forth-find-tag)
  943: 
  944: ;;; hook into motion events (realy ugly!)  (dk)
  945: (define-key forth-mode-map "\C-n" 'forth-next-line)
  946: (define-key forth-mode-map "\C-p" 'forth-previous-line)
  947: (define-key forth-mode-map [down] 'forth-next-line)
  948: (define-key forth-mode-map [up] 'forth-previous-line)
  949: (define-key forth-mode-map "\C-f" 'forth-forward-char)
  950: (define-key forth-mode-map "\C-b" 'forth-backward-char)
  951: (define-key forth-mode-map [right] 'forth-forward-char)
  952: (define-key forth-mode-map [left] 'forth-backward-char)
  953: (define-key forth-mode-map "\M-f" 'forth-forward-word)
  954: (define-key forth-mode-map "\M-b" 'forth-backward-word)
  955: (define-key forth-mode-map [C-right] 'forth-forward-word)
  956: (define-key forth-mode-map [C-left] 'forth-backward-word)
  957: (define-key forth-mode-map "\M-v" 'forth-scroll-down)
  958: (define-key forth-mode-map "\C-v" 'forth-scroll-up)
  959: (define-key forth-mode-map [prior] 'forth-scroll-down)
  960: (define-key forth-mode-map [next] 'forth-scroll-up)
  961: 
  962: (defun forth-next-line (arg) 
  963:   (interactive "p") (next-line arg) (run-hooks 'forth-motion-hooks))
  964: (defun forth-previous-line (arg)
  965:   (interactive "p") (previous-line arg) (run-hooks 'forth-motion-hooks))
  966: (defun forth-backward-char (arg)
  967:   (interactive "p") (backward-char arg) (run-hooks 'forth-motion-hooks))
  968: (defun forth-forward-char (arg)
  969:   (interactive "p") (forward-char arg) (run-hooks 'forth-motion-hooks))
  970: (defun forth-forward-word (arg)
  971:   (interactive "p") (forward-word arg) (run-hooks 'forth-motion-hooks))
  972: (defun forth-backward-word (arg)
  973:   (interactive "p") (backward-word arg) (run-hooks 'forth-motion-hooks))
  974: (defun forth-scroll-down (arg)
  975:   (interactive "P") (scroll-down arg) (run-hooks 'forth-motion-hooks))
  976: (defun forth-scroll-up (arg)
  977:   (interactive "P") (scroll-up arg) (run-hooks 'forth-motion-hooks))
  978: 
  979: ;setup for C-h C-i to work
  980: (if (fboundp 'info-lookup-add-help)
  981:     (info-lookup-add-help
  982:      :topic 'symbol
  983:      :mode 'forth-mode
  984:      :regexp "[^ 	
  985: ]+"
  986:      :ignore-case t
  987:      :doc-spec '(("(gforth)Name Index" nil "`" "'  "))))
  988: 
  989: (load "etags")
  990: 
  991: (defun forth-find-tag (tagname &optional next-p regexp-p)
  992:   (interactive (find-tag-interactive "Find tag: "))
  993:   (switch-to-buffer
  994:    (find-tag-noselect (concat " " tagname " ") next-p regexp-p)))
  995: 
  996: (defvar forth-mode-syntax-table nil
  997:   "Syntax table in use in Forth-mode buffers.")
  998: 
  999: ;; Important: hilighting/indentation now depends on a correct syntax table.
 1000: ;; All characters, except whitespace *must* belong to the "word constituent"
 1001: ;; syntax class. If different behaviour is required, use of Categories might
 1002: ;; help.
 1003: (if (not forth-mode-syntax-table)
 1004:     (progn
 1005:       (setq forth-mode-syntax-table (make-syntax-table))
 1006:       (let ((char 0))
 1007: 	(while (< char ?!)
 1008: 	  (modify-syntax-entry char " " forth-mode-syntax-table)
 1009: 	  (setq char (1+ char)))
 1010: 	(while (< char 256)
 1011: 	  (modify-syntax-entry char "w" forth-mode-syntax-table)
 1012: 	  (setq char (1+ char))))
 1013:       ))
 1014: 
 1015: 
 1016: (defun forth-mode-variables ()
 1017:   (set-syntax-table forth-mode-syntax-table)
 1018:   (setq local-abbrev-table forth-mode-abbrev-table)
 1019:   (make-local-variable 'paragraph-start)
 1020:   (setq paragraph-start (concat "^$\\|" page-delimiter))
 1021:   (make-local-variable 'paragraph-separate)
 1022:   (setq paragraph-separate paragraph-start)
 1023:   (make-local-variable 'indent-line-function)
 1024:   (setq indent-line-function 'forth-indent-line)
 1025: ;  (make-local-variable 'require-final-newline)
 1026: ;  (setq require-final-newline t)
 1027:   (make-local-variable 'comment-start)
 1028:   (setq comment-start "\\ ")
 1029:   ;(make-local-variable 'comment-end)
 1030:   ;(setq comment-end " )")
 1031:   (make-local-variable 'comment-column)
 1032:   (setq comment-column 40)
 1033:   (make-local-variable 'comment-start-skip)
 1034:   (setq comment-start-skip "\\ ")
 1035:   (make-local-variable 'comment-indent-hook)
 1036:   (setq comment-indent-hook 'forth-comment-indent)
 1037:   (make-local-variable 'parse-sexp-ignore-comments)
 1038:   (setq parse-sexp-ignore-comments t)
 1039:   (setq case-fold-search t)
 1040:   (make-local-variable 'forth-words)
 1041:   (make-local-variable 'forth-compiled-words)
 1042:   (make-local-variable 'forth-compiled-indent-words)
 1043:   (make-local-variable 'forth-hilight-level)
 1044:   (make-local-variable 'after-change-functions)
 1045:   (make-local-variable 'forth-show-screen)
 1046:   (make-local-variable 'forth-screen-marker)
 1047:   (make-local-variable 'forth-warn-long-lines)
 1048:   (make-local-variable 'forth-screen-number-string)
 1049:   (make-local-variable 'forth-use-oof)
 1050:   (make-local-variable 'forth-use-objects) 
 1051:   (setq forth-screen-marker (copy-marker 0))
 1052:   (add-hook 'after-change-functions 'forth-change-function)
 1053:   (setq imenu-create-index-function 'forth-create-index))
 1054: 
 1055: ;;;###autoload
 1056: (defun forth-mode ()
 1057:   "
 1058: Major mode for editing Forth code. Tab indents for Forth code. Comments
 1059: are delimited with \\ and newline. Paragraphs are separated by blank lines
 1060: only. Block files are autodetected, when read, and converted to normal 
 1061: stream source format. See also `forth-block-mode'.
 1062: \\{forth-mode-map}
 1063:  Forth-split
 1064:     Positions the current buffer on top and a forth-interaction window
 1065:     below. The window size is controlled by the forth-percent-height
 1066:     variable (see below).
 1067:  Forth-reload
 1068:     Reloads the forth library and restarts the forth process.
 1069:  Forth-send-buffer
 1070:     Sends the current buffer, in text representation, as input to the
 1071:     forth process.
 1072:  Forth-send-paragraph
 1073:     Sends the previous or the current paragraph to the forth-process.
 1074:     Note that the cursor only need to be with in the paragraph to be sent.
 1075:  forth-documentation
 1076:     Search for documentation of forward adjacent to cursor. Note! To use
 1077:     this mode you have to add a line, to your .emacs file, defining the
 1078:     directories to search through for documentation files (se variable
 1079:     forth-help-load-path below) e.g. (setq forth-help-load-path '(nil)).
 1080: 
 1081: Variables controlling interaction and startup
 1082:  forth-percent-height
 1083:     Tells split how high to make the edit portion, in percent of the
 1084:     current screen height.
 1085:  forth-program-name
 1086:     Tells the library which program name to execute in the interation
 1087:     window.
 1088: 
 1089: Variables controlling syntax hilighting/recognition of parsed text:
 1090:  `forth-words'
 1091:     List of words that have a special parsing behaviour and/or should be
 1092:     hilighted. Add custom words by setting forth-custom-words in your
 1093:     .emacs, or by setting forth-local-words, in source-files' local 
 1094:     variables lists.
 1095:  forth-use-objects
 1096:     Set this variable to non-nil in your .emacs, or a local variables 
 1097:     list, to hilight and recognize the words from the \"Objects\" package 
 1098:     for object-oriented programming.
 1099:  forth-use-oof
 1100:     Same as above, just for the \"OOF\" package.
 1101:  forth-custom-words
 1102:     List of custom Forth words to prepend to `forth-words'. Should be set
 1103:     in your .emacs.
 1104:  forth-local-words
 1105:     List of words to prepend to `forth-words', whenever a forth-mode
 1106:     buffer is created. That variable should be set by Forth sources, using
 1107:     a local variables list at the end of file, to get file-specific
 1108:     hilighting.
 1109:     0 [IF]
 1110:        Local Variables: ... 
 1111:        forth-local-words: ...
 1112:        End:
 1113:     [THEN]
 1114:  forth-hilight-level
 1115:     Controls how much syntax hilighting is done. Should be in the range 
 1116:     0..3
 1117: 
 1118: Variables controlling indentation style:
 1119:  `forth-indent-words'
 1120:     List of words that influence indentation.
 1121:  forth-local-indent-words
 1122:     List of words to prepend to `forth-indent-words', similar to 
 1123:     forth-local-words. Should be used for specifying file-specific 
 1124:     indentation, using a local variables list.
 1125:  forth-custom-indent-words
 1126:     List of words to prepend to `forth-indent-words'. Should be set in your
 1127:     .emacs.    
 1128:  forth-indent-level
 1129:     Indentation increment/decrement of Forth statements.
 1130:  forth-minor-indent-level
 1131:     Minor indentation increment/decrement of Forth statemens.
 1132: 
 1133: Variables controlling block-file editing:
 1134:  forth-show-screen
 1135:     Non-nil means, that the start of the current screen is marked by an
 1136:     overlay arrow, and screen numbers are displayed in the mode line.
 1137:     This variable is by default nil for `forth-mode' and t for 
 1138:     `forth-block-mode'.
 1139:  forth-overlay-arrow-string
 1140:     String to display as the overlay arrow, when `forth-show-screen' is t.
 1141:     Setting this variable to nil disables the overlay arrow.
 1142:  forth-block-base
 1143:     Screen number of the first block in a block file. Defaults to 1.
 1144:  forth-warn-long-lines
 1145:     Non-nil means that a warning message is displayed whenever you edit or
 1146:     move over a line that is longer than 64 characters (the maximum line
 1147:     length that can be stored into a block file). This variable defaults to
 1148:     t for `forth-block-mode' and to nil for `forth-mode'.
 1149: 
 1150: Variables controling documentation search
 1151:  forth-help-load-path
 1152:     List of directories to search through to find *.doc
 1153:     (forth-help-file-suffix) files. Nil means current default directory.
 1154:     The specified directories must contain at least one .doc file. If it
 1155:     does not and you still want the load-path to scan that directory, create
 1156:     an empty file dummy.doc.
 1157:  forth-help-file-suffix
 1158:     The file names to search for in each directory specified by
 1159:     forth-help-load-path. Defaulted to '*.doc'. 
 1160: "
 1161:   (interactive)
 1162:   (kill-all-local-variables)
 1163:   (use-local-map forth-mode-map)
 1164:   (setq mode-name "Forth")
 1165:   (setq major-mode 'forth-mode)
 1166:   ;; convert buffer contents from block file format, if necessary
 1167:   (when (forth-detect-block-file-p)
 1168:     (widen)
 1169:     (message "Converting from Forth block source...")
 1170:     (forth-convert-from-block (point-min) (point-max))
 1171:     (message "Converting from Forth block source...done"))
 1172:   ;; if user switched from forth-block-mode to forth-mode, make sure the file
 1173:   ;; is now stored as normal strem source
 1174:   (when (equal buffer-file-format '(forth-blocks))
 1175:     (setq buffer-file-format nil))
 1176:   (forth-mode-variables)
 1177: ;  (if (not (forth-process-running-p))
 1178: ;      (run-forth forth-program-name))
 1179:   (run-hooks 'forth-mode-hook))
 1180: 
 1181: ;;;###autoload
 1182: (define-derived-mode forth-block-mode forth-mode "Forth Block Source" 
 1183:   "Major mode for editing Forth block source files, derived from 
 1184: `forth-mode'. Differences to `forth-mode' are:
 1185:  * files are converted to block format, when written (`buffer-file-format' 
 1186:    is set to `(forth-blocks)')
 1187:  * `forth-show-screen' and `forth-warn-long-lines' are t by default
 1188:   
 1189: Note that the length of lines in block files is limited to 64 characters.
 1190: When writing longer lines to a block file, a warning is displayed in the
 1191: echo area and the line is truncated. 
 1192: 
 1193: Another problem is imposed by block files that contain newline or tab 
 1194: characters. When Emacs converts such files back to block file format, 
 1195: it'll translate those characters to a number of spaces. However, when
 1196: you read such a file, a warning message is displayed in the echo area,
 1197: including a line number that may help you to locate and fix the problem.
 1198: 
 1199: So have a look at the *Messages* buffer, whenever you hear (or see) Emacs' 
 1200: bell during block file read/write operations."
 1201:   (setq buffer-file-format '(forth-blocks))
 1202:   (setq forth-show-screen t)
 1203:   (setq forth-warn-long-lines t)
 1204:   (setq forth-screen-number-string (format "%d" forth-block-base))
 1205:   (setq mode-line-format (append (reverse (cdr (reverse mode-line-format)))
 1206: 				 '("--S" forth-screen-number-string "-%-"))))
 1207: 
 1208: (add-hook 'forth-mode-hook
 1209:       '(lambda () 
 1210: 	 (make-local-variable 'compile-command)
 1211: 	 (setq compile-command "gforth ")
 1212: 	 (forth-hack-local-variables)
 1213: 	 (forth-customize-words)
 1214: 	 (forth-compile-words)
 1215: 	 (forth-change-function (point-min) (point-max) nil t)))
 1216: 
 1217: (defun forth-fill-paragraph () 
 1218:   "Fill comments (starting with '\'; do not fill code (block style
 1219: programmers who tend to fill code won't use emacs anyway:-)."
 1220:   ; Currently only comments at the start of the line are filled.
 1221:   ; Something like lisp-fill-paragraph may be better.  We cannot use
 1222:   ; fill-paragraph, because it removes the \ from the first comment
 1223:   ; line. Therefore we have to look for the first line of the comment
 1224:   ; and use fill-region.
 1225:   (interactive)
 1226:   (save-excursion
 1227:     (beginning-of-line)
 1228:     (while (and
 1229: 	     (= (forward-line -1) 0)
 1230: 	     (looking-at "[ \t]*\\\\g?[ \t]+")))
 1231:     (if (not (looking-at "[ \t]*\\\\g?[ \t]+"))
 1232: 	(forward-line 1))
 1233:     (let ((from (point))
 1234: 	  (to (save-excursion (forward-paragraph) (point))))
 1235:       (if (looking-at "[ \t]*\\\\g?[ \t]+")
 1236: 	  (progn (goto-char (match-end 0))
 1237: 		 (set-fill-prefix)
 1238: 		 (fill-region from to nil))))))
 1239: 
 1240: (defun forth-comment-indent ()
 1241:   (save-excursion
 1242:     (beginning-of-line)
 1243:     (if (looking-at ":[ \t]*")
 1244: 	(progn
 1245: 	  (end-of-line)
 1246: 	  (skip-chars-backward " \t\n")
 1247: 	  (1+ (current-column)))
 1248:       comment-column)))
 1249: 
 1250: 
 1251: ;; Forth commands
 1252: 
 1253: (defun forth-remove-tracers ()
 1254:   "Remove tracers of the form `~~ '. Queries the user for each occurrence."
 1255:   (interactive)
 1256:   (query-replace-regexp "\\(~~ \\| ~~$\\)" "" nil))
 1257: 
 1258: (defvar forth-program-name "gforth"
 1259:   "*Program invoked by the `run-forth' command.")
 1260: 
 1261: (defvar forth-band-name nil
 1262:   "*Band loaded by the `run-forth' command.")
 1263: 
 1264: (defvar forth-program-arguments nil
 1265:   "*Arguments passed to the Forth program by the `run-forth' command.")
 1266: 
 1267: (defun run-forth (command-line)
 1268:   "Run an inferior Forth process. Output goes to the buffer `*forth*'.
 1269: With argument, asks for a command line. Split up screen and run forth 
 1270: in the lower portion. The current-buffer when called will stay in the
 1271: upper portion of the screen, and all other windows are deleted.
 1272: Call run-forth again to make the *forth* buffer appear in the lower
 1273: part of the screen."
 1274:   (interactive
 1275:    (list (let ((default
 1276: 		 (or forth-process-command-line
 1277: 		     (forth-default-command-line))))
 1278: 	   (if current-prefix-arg
 1279: 	       (read-string "Run Forth: " default)
 1280: 	       default))))
 1281:   (setq forth-process-command-line command-line)
 1282:   (forth-start-process command-line)
 1283:   (forth-split)
 1284:   (forth-set-runlight forth-runlight:input))
 1285: 
 1286: (defun run-forth-if-not ()
 1287:   (if (not (forth-process-running-p))
 1288:       (run-forth forth-program-name)))
 1289: 
 1290: (defun reset-forth ()
 1291:   "Reset the Forth process."
 1292:   (interactive)
 1293:   (let ((process (get-process forth-program-name)))
 1294:     (cond ((or (not process)
 1295: 	       (not (eq (process-status process) 'run))
 1296: 	       (yes-or-no-p
 1297: "The Forth process is running, are you SURE you want to reset it? "))
 1298: 	   (message "Resetting Forth process...")
 1299: 	   (forth-reload)
 1300: 	   (message "Resetting Forth process...done")))))
 1301: 
 1302: (defun forth-default-command-line ()
 1303:   (concat forth-program-name
 1304: 	  (if forth-program-arguments
 1305: 	      (concat " " forth-program-arguments)
 1306: 	      "")))
 1307: 
 1308: ;;;; Internal Variables
 1309: 
 1310: (defvar forth-process-command-line nil
 1311:   "Command used to start the most recent Forth process.")
 1312: 
 1313: (defvar forth-previous-send ""
 1314:   "Most recent expression transmitted to the Forth process.")
 1315: 
 1316: (defvar forth-process-filter-queue '()
 1317:   "Queue used to synchronize filter actions properly.")
 1318: 
 1319: (defvar forth-prompt "ok"
 1320:   "The current forth prompt string.")
 1321: 
 1322: (defvar forth-start-hook nil
 1323:   "If non-nil, a procedure to call when the Forth process is started.
 1324: When called, the current buffer will be the Forth process-buffer.")
 1325: 
 1326: (defvar forth-signal-death-message nil
 1327:   "If non-nil, causes a message to be generated when the Forth process dies.")
 1328: 
 1329: (defvar forth-percent-height 50
 1330:   "Tells run-forth how high the upper window should be in percent.")
 1331: 
 1332: (defconst forth-runlight:input ?I
 1333:   "The character displayed when the Forth process is waiting for input.")
 1334: 
 1335: (defvar forth-mode-string ""
 1336:   "String displayed in the mode line when the Forth process is running.")
 1337: 
 1338: ;;;; Evaluation Commands
 1339: 
 1340: (defun forth-send-string (&rest strings)
 1341:   "Send the string arguments to the Forth process.
 1342: The strings are concatenated and terminated by a newline."
 1343:   (cond ((forth-process-running-p)
 1344: 	 (forth-send-string-1 strings))
 1345: 	((yes-or-no-p "The Forth process has died.  Reset it? ")
 1346: 	 (reset-forth)
 1347: 	 (goto-char (point-max))
 1348: 	 (forth-send-string-1 strings))))
 1349: 
 1350: (defun forth-send-string-1 (strings)
 1351:   (let ((string (apply 'concat strings)))
 1352:     (forth-send-string-2 string)))
 1353: 
 1354: (defun forth-send-string-2 (string)
 1355:   (let ((process (get-process forth-program-name)))
 1356:     (if (not (eq (current-buffer) (get-buffer forth-program-name)))
 1357: 	(progn
 1358: 	 (forth-process-filter-output string)
 1359: 	 (forth-process-filter:finish)))
 1360:     (send-string process (concat string "\n"))
 1361:     (if (eq (current-buffer) (process-buffer process))
 1362: 	(set-marker (process-mark process) (point)))))
 1363: 
 1364: 
 1365: (defun forth-send-region (start end)
 1366:   "Send the current region to the Forth process.
 1367: The region is sent terminated by a newline."
 1368:   (interactive "r")
 1369:   (let ((process (get-process forth-program-name)))
 1370:     (if (and process (eq (current-buffer) (process-buffer process)))
 1371: 	(progn (goto-char end)
 1372: 	       (set-marker (process-mark process) end))))
 1373:   (forth-send-string "\n" (buffer-substring start end) "\n"))
 1374: 
 1375: (defun forth-end-of-paragraph ()
 1376:   (if (looking-at "[\t\n ]+") (skip-chars-backward  "\t\n "))
 1377:   (if (not (re-search-forward "\n[ \t]*\n" nil t))
 1378:       (goto-char (point-max))))
 1379: 
 1380: (defun forth-send-paragraph ()
 1381:   "Send the current or the previous paragraph to the Forth process"
 1382:   (interactive)
 1383:   (let (end)
 1384:     (save-excursion
 1385:       (forth-end-of-paragraph)
 1386:       (skip-chars-backward  "\t\n ")
 1387:       (setq end (point))
 1388:       (if (re-search-backward "\n[ \t]*\n" nil t)
 1389: 	  (setq start (point))
 1390: 	(goto-char (point-min)))
 1391:       (skip-chars-forward  "\t\n ")
 1392:       (forth-send-region (point) end))))
 1393:   
 1394: (defun forth-send-buffer ()
 1395:   "Send the current buffer to the Forth process."
 1396:   (interactive)
 1397:   (if (eq (current-buffer) (forth-process-buffer))
 1398:       (error "Not allowed to send this buffer's contents to Forth"))
 1399:   (forth-send-region (point-min) (point-max)))
 1400: 
 1401: 
 1402: ;;;; Basic Process Control
 1403: 
 1404: (defun forth-start-process (command-line)
 1405:   (let ((buffer (get-buffer-create "*forth*")))
 1406:     (let ((process (get-buffer-process buffer)))
 1407:       (save-excursion
 1408: 	(set-buffer buffer)
 1409: 	(progn (if process (delete-process process))
 1410: 	       (goto-char (point-max))
 1411: 	       (setq mode-line-process '(": %s"))
 1412: 	       (add-to-global-mode-string 'forth-mode-string)
 1413: 	       (setq process
 1414: 		     (apply 'start-process
 1415: 			    (cons forth-program-name
 1416: 				  (cons buffer
 1417: 					(forth-parse-command-line
 1418: 					 command-line)))))
 1419: 	       (set-marker (process-mark process) (point-max))
 1420: 	       (forth-process-filter-initialize t)
 1421: 	       (forth-modeline-initialize)
 1422: 	       (set-process-sentinel process 'forth-process-sentinel)
 1423: 	       (set-process-filter process 'forth-process-filter)
 1424: 	       (run-hooks 'forth-start-hook)))
 1425:     buffer)))
 1426: 
 1427: (defun forth-parse-command-line (string)
 1428:   (setq string (substitute-in-file-name string))
 1429:   (let ((start 0)
 1430: 	(result '()))
 1431:     (while start
 1432:       (let ((index (string-match "[ \t]" string start)))
 1433: 	(setq start
 1434: 	      (cond ((not index)
 1435: 		     (setq result
 1436: 			   (cons (substring string start)
 1437: 				 result))
 1438: 		     nil)
 1439: 		    ((= index start)
 1440: 		     (string-match "[^ \t]" string start))
 1441: 		    (t
 1442: 		     (setq result
 1443: 			   (cons (substring string start index)
 1444: 				 result))
 1445: 		     (1+ index))))))
 1446:     (nreverse result)))
 1447: 
 1448: 
 1449: (defun forth-process-running-p ()
 1450:   "True iff there is a Forth process whose status is `run'."
 1451:   (let ((process (get-process forth-program-name)))
 1452:     (and process
 1453: 	 (eq (process-status process) 'run))))
 1454: 
 1455: (defun forth-process-buffer ()
 1456:   (let ((process (get-process forth-program-name)))
 1457:     (and process (process-buffer process))))
 1458: 
 1459: ;;;; Process Filter
 1460: 
 1461: (defun forth-process-sentinel (proc reason)
 1462:   (let ((inhibit-quit nil))
 1463:     (forth-process-filter-initialize (eq reason 'run))
 1464:     (if (eq reason 'run)
 1465: 	(forth-modeline-initialize)
 1466: 	(setq forth-mode-string "")))
 1467:   (if (and (not (memq reason '(run stop)))
 1468: 	   forth-signal-death-message)
 1469:       (progn (beep)
 1470: 	     (message
 1471: "The Forth process has died!  Do M-x reset-forth to restart it"))))
 1472: 
 1473: (defun forth-process-filter-initialize (running-p)
 1474:   (setq forth-process-filter-queue (cons '() '()))
 1475:   (setq forth-prompt "ok"))
 1476: 
 1477: 
 1478: (defun forth-process-filter (proc string)
 1479:   (forth-process-filter-output string)
 1480:   (forth-process-filter:finish))
 1481: 
 1482: (defun forth-process-filter:enqueue (action)
 1483:   (let ((next (cons action '())))
 1484:     (if (cdr forth-process-filter-queue)
 1485: 	(setcdr (cdr forth-process-filter-queue) next)
 1486: 	(setcar forth-process-filter-queue next))
 1487:     (setcdr forth-process-filter-queue next)))
 1488: 
 1489: (defun forth-process-filter:finish ()
 1490:   (while (car forth-process-filter-queue)
 1491:     (let ((next (car forth-process-filter-queue)))
 1492:       (setcar forth-process-filter-queue (cdr next))
 1493:       (if (not (cdr next))
 1494: 	  (setcdr forth-process-filter-queue '()))
 1495:       (apply (car (car next)) (cdr (car next))))))
 1496: 
 1497: ;;;; Process Filter Output
 1498: 
 1499: (defun forth-process-filter-output (&rest args)
 1500:   (if (not (and args
 1501: 		(null (cdr args))
 1502: 		(stringp (car args))
 1503: 		(string-equal "" (car args))))
 1504:       (forth-process-filter:enqueue
 1505:        (cons 'forth-process-filter-output-1 args))))
 1506: 
 1507: (defun forth-process-filter-output-1 (&rest args)
 1508:   (save-excursion
 1509:     (forth-goto-output-point)
 1510:     (apply 'insert-before-markers args)))
 1511: 
 1512: (defun forth-guarantee-newlines (n)
 1513:   (save-excursion
 1514:     (forth-goto-output-point)
 1515:     (let ((stop nil))
 1516:       (while (and (not stop)
 1517: 		  (bolp))
 1518: 	(setq n (1- n))
 1519: 	(if (bobp)
 1520: 	    (setq stop t)
 1521: 	  (backward-char))))
 1522:     (forth-goto-output-point)
 1523:     (while (> n 0)
 1524:       (insert-before-markers ?\n)
 1525:       (setq n (1- n)))))
 1526: 
 1527: (defun forth-goto-output-point ()
 1528:   (let ((process (get-process forth-program-name)))
 1529:     (set-buffer (process-buffer process))
 1530:     (goto-char (process-mark process))))
 1531: 
 1532: (defun forth-modeline-initialize ()
 1533:   (setq forth-mode-string "  "))
 1534: 
 1535: (defun forth-set-runlight (runlight)
 1536:   (aset forth-mode-string 0 runlight)
 1537:   (forth-modeline-redisplay))
 1538: 
 1539: (defun forth-modeline-redisplay ()
 1540:   (save-excursion (set-buffer (other-buffer)))
 1541:   (set-buffer-modified-p (buffer-modified-p))
 1542:   (sit-for 0))
 1543: 
 1544: ;;;; Process Filter Operations
 1545: 
 1546: (defun add-to-global-mode-string (x)
 1547:   (cond ((null global-mode-string)
 1548: 	 (setq global-mode-string (list "" x " ")))
 1549: 	((not (memq x global-mode-string))
 1550: 	 (setq global-mode-string
 1551: 	       (cons ""
 1552: 		     (cons x
 1553: 			   (cons " "
 1554: 				 (if (equal "" (car global-mode-string))
 1555: 				     (cdr global-mode-string)
 1556: 				     global-mode-string))))))))
 1557: 
 1558: 
 1559: ;; Misc
 1560: 
 1561: (setq auto-mode-alist (append auto-mode-alist
 1562: 				'(("\\.fs$" . forth-mode))))
 1563: 
 1564: (defun forth-split ()
 1565:   (interactive)
 1566:   (forth-split-1 "*forth*"))
 1567: 
 1568: (defun forth-split-1 (buffer)
 1569:   (if (not (eq (window-buffer) (get-buffer buffer)))
 1570:       (progn
 1571: 	(delete-other-windows)
 1572: 	(split-window-vertically
 1573: 	 (/ (* (screen-height) forth-percent-height) 100))
 1574: 	(other-window 1)
 1575: 	(switch-to-buffer buffer)
 1576: 	(goto-char (point-max))
 1577: 	(other-window 1))))
 1578:     
 1579: (defun forth-reload ()
 1580:   (interactive)
 1581:   (let ((process (get-process forth-program-name)))
 1582:     (if process (kill-process process t)))
 1583:   (sleep-for 0 100)
 1584:   (forth-mode))
 1585: 
 1586: 
 1587: ;; Special section for forth-help
 1588: 
 1589: (defvar forth-help-buffer "*Forth-help*"
 1590:   "Buffer used to display the requested documentation.")
 1591: 
 1592: (defvar forth-help-load-path nil
 1593:   "List of directories to search through to find *.doc
 1594:  (forth-help-file-suffix) files. Nil means current default directory.
 1595:  The specified directories must contain at least one .doc file. If it
 1596:  does not and you still want the load-path to scan that directory, create
 1597:  an empty file dummy.doc.")
 1598: 
 1599: (defvar forth-help-file-suffix "*.doc"
 1600:   "The file names to search for in each directory.")
 1601: 
 1602: (setq forth-search-command-prefix "grep -n \"^    [^(]* ")
 1603: (defvar forth-search-command-suffix "/dev/null")
 1604: (defvar forth-grep-error-regexp ": No such file or directory")
 1605: 
 1606: (defun forth-function-called-at-point ()
 1607:   "Return the space delimited word a point."
 1608:   (save-excursion
 1609:     (save-restriction
 1610:       (narrow-to-region (max (point-min) (- (point) 1000)) (point-max))
 1611:       (skip-chars-backward "^ \t\n" (point-min))
 1612:       (if (looking-at "[ \t\n]")
 1613: 	  (forward-char 1))
 1614:       (let (obj (p (point)))
 1615: 	(skip-chars-forward "^ \t\n")
 1616: 	(buffer-substring p (point))))))
 1617: 
 1618: (defun forth-help-names-extend-comp (path-list result)
 1619:   (cond ((null path-list) result)
 1620: 	((null (car path-list))
 1621: 	 (forth-help-names-extend-comp (cdr path-list) 
 1622: 	       (concat result forth-help-file-suffix " ")))
 1623: 	(t (forth-help-names-extend-comp
 1624: 	    (cdr path-list) (concat result
 1625: 				    (expand-file-name (car path-list)) "/"
 1626: 				    forth-help-file-suffix " ")))))
 1627: 
 1628: (defun forth-help-names-extended ()
 1629:   (if forth-help-load-path
 1630:       (forth-help-names-extend-comp forth-help-load-path "")
 1631:     (error "forth-help-load-path not specified")))
 1632: 
 1633: 
 1634: ;(define-key forth-mode-map "\C-hf" 'forth-documentation)
 1635: 
 1636: (defun forth-documentation (function)
 1637:   "Display the full documentation of FORTH word."
 1638:   (interactive
 1639:    (let ((fn (forth-function-called-at-point))
 1640: 	 (enable-recursive-minibuffers t)	     
 1641: 	 search-list
 1642: 	 val)
 1643:      (setq val (read-string (format "Describe forth word (default %s): " fn)))
 1644:      (list (if (equal val "") fn val))))
 1645:   (forth-get-doc (concat forth-search-command-prefix
 1646: 			 (grep-regexp-quote (concat function " ("))
 1647: 			 "[^)]*\-\-\" " (forth-help-names-extended)
 1648: 			 forth-search-command-suffix))
 1649:   (message "C-x C-m switches back to the forth interaction window"))
 1650: 
 1651: (defun forth-get-doc (command)
 1652:   "Display the full documentation of command."
 1653:   (let ((curwin (get-buffer-window (window-buffer)))
 1654: 	reswin
 1655: 	pointmax)
 1656:     (with-output-to-temp-buffer forth-help-buffer
 1657:       (progn
 1658: 	(call-process "sh" nil forth-help-buffer t "-c" command)
 1659: 	(setq reswin (get-buffer-window forth-help-buffer))))
 1660:     (setq reswin (get-buffer-window forth-help-buffer))
 1661:     (select-window reswin)
 1662:     (save-excursion
 1663:       (goto-char (setq pointmax (point-max)))
 1664:       (insert "--------------------\n\n"))
 1665:     (let (fd doc) 
 1666:       (while (setq fd (forth-get-file-data pointmax))
 1667: 	(setq doc (forth-get-doc-string fd))
 1668: 	(save-excursion
 1669: 	  (goto-char (point-max))
 1670: 	  (insert (substring (car fd) (string-match "[^/]*$" (car fd)))
 1671: 		  ":\n\n" doc "\n")))
 1672:       (if (not doc)
 1673: 	  (progn (goto-char (point-max)) (insert "Not found"))))
 1674:     (select-window curwin)))
 1675:   
 1676: (defun forth-skip-error-lines ()
 1677:   (let ((lines 0))
 1678:     (save-excursion
 1679:       (while (re-search-forward forth-grep-error-regexp nil t)
 1680: 	(beginning-of-line)
 1681: 	(forward-line 1)
 1682: 	(setq lines (1+ lines))))
 1683:     (forward-line lines)))
 1684: 
 1685: (defun forth-get-doc-string (fd)
 1686:   "Find file (car fd) and extract documentation from line (nth 1 fd)."
 1687:   (let (result)
 1688:     (save-window-excursion
 1689:       (find-file (car fd))
 1690:       (goto-line (nth 1 fd))
 1691:       (if (not (eq (nth 1 fd) (1+ (count-lines (point-min) (point)))))
 1692: 	  (error "forth-get-doc-string: serious error"))
 1693:       (if (not (re-search-backward "\n[\t ]*\n" nil t))
 1694: 	  (goto-char (point-min))
 1695: 	(goto-char (match-end 0)))
 1696:       (let ((p (point)))
 1697: 	(if (not (re-search-forward "\n[\t ]*\n" nil t))
 1698: 	    (goto-char (point-max)))
 1699: 	(setq result (buffer-substring p (point))))
 1700:       (bury-buffer (current-buffer)))
 1701:     result))
 1702: 
 1703: (defun forth-get-file-data (limit)
 1704:   "Parse grep output and return '(filename line#) list. Return nil when
 1705:  passing limit."
 1706:   (forth-skip-error-lines)
 1707:   (if (< (point) limit)
 1708:       (let ((result (forth-get-file-data-cont limit)))
 1709: 	(forward-line 1)
 1710: 	(beginning-of-line)
 1711: 	result)))
 1712: 
 1713: (defun forth-get-file-data-cont (limit)
 1714:   (let (result)
 1715:     (let ((p (point)))
 1716:       (skip-chars-forward "^:")
 1717:       (setq result (buffer-substring p (point))))
 1718:     (if (< (point) limit)
 1719: 	(let ((p (1+ (point))))
 1720: 	  (forward-char 1)
 1721: 	  (skip-chars-forward "^:")
 1722: 	  (list result (string-to-int (buffer-substring p (point))))))))
 1723: 
 1724: (defun grep-regexp-quote (str)
 1725:   (let ((i 0) (m 1) (res ""))
 1726:     (while (/= m 0)
 1727:       (setq m (string-to-char (substring str i)))
 1728:       (if (/= m 0)
 1729: 	  (progn
 1730: 	    (setq i (1+ i))
 1731: 	    (if (string-match (regexp-quote (char-to-string m))
 1732: 			      ".*\\^$[]")
 1733: 		(setq res (concat res "\\")))
 1734: 	    (setq res (concat res (char-to-string m))))))
 1735:     res))
 1736: 
 1737: 
 1738: (define-key forth-mode-map "\C-x\C-e" 'compile)
 1739: (define-key forth-mode-map "\C-x\C-n" 'next-error)
 1740: (require 'compile "compile")
 1741: 
 1742: (defvar forth-compile-command "gforth ")
 1743: ;(defvar forth-compilation-window-percent-height 30)
 1744: 
 1745: (defun forth-compile (command)
 1746:   (interactive (list (setq forth-compile-command (read-string "Compile command: " forth-compile-command))))
 1747:   (forth-split-1 "*compilation*")
 1748:   (setq ctools-compile-command command)
 1749:   (compile1 ctools-compile-command "No more errors"))
 1750: 
 1751: 
 1752: ;;; Forth menu
 1753: ;;; Mikael Karlsson <qramika@eras70.ericsson.se>
 1754: 
 1755: (cond ((string-match "XEmacs\\|Lucid" emacs-version)
 1756:        (require 'func-menu)
 1757: 
 1758:   (defconst fume-function-name-regexp-forth
 1759:    "^\\(:\\)[ \t]+\\([^ \t]*\\)"
 1760:    "Expression to get word definitions in Forth.")
 1761: 
 1762:   (setq fume-function-name-regexp-alist
 1763:       (append '((forth-mode . fume-function-name-regexp-forth) 
 1764:              ) fume-function-name-regexp-alist))
 1765: 
 1766:   ;; Find next forth word in the buffer
 1767:   (defun fume-find-next-forth-function-name (buffer)
 1768:     "Searches for the next forth word in BUFFER."
 1769:     (set-buffer buffer)
 1770:     (if (re-search-forward fume-function-name-regexp nil t)
 1771:       (let ((beg (match-beginning 2))
 1772:             (end (match-end 2)))
 1773:         (cons (buffer-substring beg end) beg))))
 1774: 
 1775:   (setq fume-find-function-name-method-alist
 1776:   (append '((forth-mode    . fume-find-next-forth-function-name))))
 1777: 
 1778:   ))
 1779: ;;; End Forth menu
 1780: 
 1781: ;;; File folding of forth-files
 1782: ;;; uses outline
 1783: ;;; Toggle activation with M-x fold-f (when editing a forth-file) 
 1784: ;;; Use f9 to expand, f10 to hide, Or the menubar in xemacs
 1785: ;;;
 1786: ;;; Works most of the times but loses sync with the cursor occasionally 
 1787: ;;; Could be improved by also folding on comments
 1788: 
 1789: (require 'outline)
 1790: 
 1791: (defun f-outline-level ()
 1792: 	(cond	((looking-at "\\`\\\\")
 1793: 			0)
 1794: 		((looking-at "\\\\ SEC")
 1795: 			0)
 1796: 		((looking-at "\\\\ \\\\ .*")
 1797: 			0)
 1798: 		((looking-at "\\\\ DEFS")
 1799: 			1)
 1800: 		((looking-at "\\/\\* ")
 1801: 			1)
 1802: 		((looking-at ": .*")
 1803: 			1)
 1804: 		((looking-at "\\\\G")
 1805: 			2)
 1806: 		((looking-at "[ \t]+\\\\")
 1807: 			3))
 1808: )			
 1809: 
 1810: (defun fold-f  ()
 1811:    (interactive)
 1812:    (add-hook 'outline-minor-mode-hook 'hide-body)
 1813: 
 1814:    ; outline mode header start, i.e. find word definitions
 1815: ;;;   (setq  outline-regexp  "^\\(:\\)[ \t]+\\([^ \t]*\\)")
 1816:    (setq  outline-regexp  "\\`\\\\\\|:\\|\\\\ SEC\\|\\\\G\\|[ \t]+\\\\\\|\\\\ DEFS\\|\\/\\*\\|\\\\ \\\\ .*")
 1817:    (setq outline-level 'f-outline-level)
 1818: 
 1819:    (outline-minor-mode)
 1820:    (define-key outline-minor-mode-map '(shift up) 'hide-sublevels)
 1821:    (define-key outline-minor-mode-map '(shift right) 'show-children)
 1822:    (define-key outline-minor-mode-map '(shift left) 'hide-subtree)
 1823:    (define-key outline-minor-mode-map '(shift down) 'show-subtree)
 1824: 
 1825: )
 1826: 
 1827: ;;(define-key global-map '(shift up) 'fold-f)
 1828: 
 1829: ;;; end file folding
 1830: 
 1831: ;;; func-menu is a package that scans your source file for function definitions
 1832: ;;; and makes a menubar entry that lets you jump to any particular function
 1833: ;;; definition by selecting it from the menu.  The following code turns this on
 1834: ;;; for all of the recognized languages.  Scanning the buffer takes some time,
 1835: ;;; but not much.
 1836: ;;;
 1837: (cond ((string-match "XEmacs\\|Lucid" emacs-version)
 1838:        (require 'func-menu)
 1839: ;;       (define-key global-map 'f8 'function-menu)
 1840:        (add-hook 'find-fible-hooks 'fume-add-menubar-entry)
 1841: ;       (define-key global-map "\C-cg" 'fume-prompt-function-goto)
 1842: ;       (define-key global-map '(shift button3) 'mouse-function-menu)
 1843: ))
 1844: 
 1845: ;;; gforth.el ends here

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