File:  [gforth] / gforth / gforth.el
Revision 1.52: download - view: text, annotated - select for diffs
Tue Jun 19 14:49:23 2001 UTC (20 years, 1 month ago) by dvdkhlng
Branches: MAIN
CVS tags: HEAD
Improved imenu support.

    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: ;  (when (not regexp-p)
  996: ;      (setq tagname (concat "^\\(" (regexp-opt (list tagname)) 
  997: ;			    "\\)$"))
  998: ;      (setq regexp-p t))
  999:   (switch-to-buffer
 1000:    (find-tag-noselect (concat " " tagname " ") next-p regexp-p)))
 1001: 
 1002: (defvar forth-mode-syntax-table nil
 1003:   "Syntax table in use in Forth-mode buffers.")
 1004: 
 1005: ;; Important: hilighting/indentation now depends on a correct syntax table.
 1006: ;; All characters, except whitespace *must* belong to the "word constituent"
 1007: ;; syntax class. If different behaviour is required, use of Categories might
 1008: ;; help.
 1009: (if (not forth-mode-syntax-table)
 1010:     (progn
 1011:       (setq forth-mode-syntax-table (make-syntax-table))
 1012:       (let ((char 0))
 1013: 	(while (< char ?!)
 1014: 	  (modify-syntax-entry char " " forth-mode-syntax-table)
 1015: 	  (setq char (1+ char)))
 1016: 	(while (< char 256)
 1017: 	  (modify-syntax-entry char "w" forth-mode-syntax-table)
 1018: 	  (setq char (1+ char))))
 1019:       ))
 1020: 
 1021: 
 1022: (defun forth-mode-variables ()
 1023:   (set-syntax-table forth-mode-syntax-table)
 1024:   (setq local-abbrev-table forth-mode-abbrev-table)
 1025:   (make-local-variable 'paragraph-start)
 1026:   (setq paragraph-start (concat "^$\\|" page-delimiter))
 1027:   (make-local-variable 'paragraph-separate)
 1028:   (setq paragraph-separate paragraph-start)
 1029:   (make-local-variable 'indent-line-function)
 1030:   (setq indent-line-function 'forth-indent-line)
 1031: ;  (make-local-variable 'require-final-newline)
 1032: ;  (setq require-final-newline t)
 1033:   (make-local-variable 'comment-start)
 1034:   (setq comment-start "\\ ")
 1035:   ;(make-local-variable 'comment-end)
 1036:   ;(setq comment-end " )")
 1037:   (make-local-variable 'comment-column)
 1038:   (setq comment-column 40)
 1039:   (make-local-variable 'comment-start-skip)
 1040:   (setq comment-start-skip "\\ ")
 1041:   (make-local-variable 'comment-indent-hook)
 1042:   (setq comment-indent-hook 'forth-comment-indent)
 1043:   (make-local-variable 'parse-sexp-ignore-comments)
 1044:   (setq parse-sexp-ignore-comments t)
 1045:   (setq case-fold-search t)
 1046:   (make-local-variable 'forth-words)
 1047:   (make-local-variable 'forth-compiled-words)
 1048:   (make-local-variable 'forth-compiled-indent-words)
 1049:   (make-local-variable 'forth-hilight-level)
 1050:   (make-local-variable 'after-change-functions)
 1051:   (make-local-variable 'forth-show-screen)
 1052:   (make-local-variable 'forth-screen-marker)
 1053:   (make-local-variable 'forth-warn-long-lines)
 1054:   (make-local-variable 'forth-screen-number-string)
 1055:   (make-local-variable 'forth-use-oof)
 1056:   (make-local-variable 'forth-use-objects) 
 1057:   (setq forth-screen-marker (copy-marker 0))
 1058:   (add-hook 'after-change-functions 'forth-change-function)
 1059:   (setq imenu-create-index-function 'forth-create-index))
 1060: 
 1061: ;;;###autoload
 1062: (defun forth-mode ()
 1063:   "
 1064: Major mode for editing Forth code. Tab indents for Forth code. Comments
 1065: are delimited with \\ and newline. Paragraphs are separated by blank lines
 1066: only. Block files are autodetected, when read, and converted to normal 
 1067: stream source format. See also `forth-block-mode'.
 1068: \\{forth-mode-map}
 1069:  Forth-split
 1070:     Positions the current buffer on top and a forth-interaction window
 1071:     below. The window size is controlled by the forth-percent-height
 1072:     variable (see below).
 1073:  Forth-reload
 1074:     Reloads the forth library and restarts the forth process.
 1075:  Forth-send-buffer
 1076:     Sends the current buffer, in text representation, as input to the
 1077:     forth process.
 1078:  Forth-send-paragraph
 1079:     Sends the previous or the current paragraph to the forth-process.
 1080:     Note that the cursor only need to be with in the paragraph to be sent.
 1081:  forth-documentation
 1082:     Search for documentation of forward adjacent to cursor. Note! To use
 1083:     this mode you have to add a line, to your .emacs file, defining the
 1084:     directories to search through for documentation files (se variable
 1085:     forth-help-load-path below) e.g. (setq forth-help-load-path '(nil)).
 1086: 
 1087: Variables controlling interaction and startup
 1088:  forth-percent-height
 1089:     Tells split how high to make the edit portion, in percent of the
 1090:     current screen height.
 1091:  forth-program-name
 1092:     Tells the library which program name to execute in the interation
 1093:     window.
 1094: 
 1095: Variables controlling syntax hilighting/recognition of parsed text:
 1096:  `forth-words'
 1097:     List of words that have a special parsing behaviour and/or should be
 1098:     hilighted. Add custom words by setting forth-custom-words in your
 1099:     .emacs, or by setting forth-local-words, in source-files' local 
 1100:     variables lists.
 1101:  forth-use-objects
 1102:     Set this variable to non-nil in your .emacs, or a local variables 
 1103:     list, to hilight and recognize the words from the \"Objects\" package 
 1104:     for object-oriented programming.
 1105:  forth-use-oof
 1106:     Same as above, just for the \"OOF\" package.
 1107:  forth-custom-words
 1108:     List of custom Forth words to prepend to `forth-words'. Should be set
 1109:     in your .emacs.
 1110:  forth-local-words
 1111:     List of words to prepend to `forth-words', whenever a forth-mode
 1112:     buffer is created. That variable should be set by Forth sources, using
 1113:     a local variables list at the end of file, to get file-specific
 1114:     hilighting.
 1115:     0 [IF]
 1116:        Local Variables: ... 
 1117:        forth-local-words: ...
 1118:        End:
 1119:     [THEN]
 1120:  forth-hilight-level
 1121:     Controls how much syntax hilighting is done. Should be in the range 
 1122:     0..3
 1123: 
 1124: Variables controlling indentation style:
 1125:  `forth-indent-words'
 1126:     List of words that influence indentation.
 1127:  forth-local-indent-words
 1128:     List of words to prepend to `forth-indent-words', similar to 
 1129:     forth-local-words. Should be used for specifying file-specific 
 1130:     indentation, using a local variables list.
 1131:  forth-custom-indent-words
 1132:     List of words to prepend to `forth-indent-words'. Should be set in your
 1133:     .emacs.    
 1134:  forth-indent-level
 1135:     Indentation increment/decrement of Forth statements.
 1136:  forth-minor-indent-level
 1137:     Minor indentation increment/decrement of Forth statemens.
 1138: 
 1139: Variables controlling block-file editing:
 1140:  forth-show-screen
 1141:     Non-nil means, that the start of the current screen is marked by an
 1142:     overlay arrow, and screen numbers are displayed in the mode line.
 1143:     This variable is by default nil for `forth-mode' and t for 
 1144:     `forth-block-mode'.
 1145:  forth-overlay-arrow-string
 1146:     String to display as the overlay arrow, when `forth-show-screen' is t.
 1147:     Setting this variable to nil disables the overlay arrow.
 1148:  forth-block-base
 1149:     Screen number of the first block in a block file. Defaults to 1.
 1150:  forth-warn-long-lines
 1151:     Non-nil means that a warning message is displayed whenever you edit or
 1152:     move over a line that is longer than 64 characters (the maximum line
 1153:     length that can be stored into a block file). This variable defaults to
 1154:     t for `forth-block-mode' and to nil for `forth-mode'.
 1155: 
 1156: Variables controling documentation search
 1157:  forth-help-load-path
 1158:     List of directories to search through to find *.doc
 1159:     (forth-help-file-suffix) files. Nil means current default directory.
 1160:     The specified directories must contain at least one .doc file. If it
 1161:     does not and you still want the load-path to scan that directory, create
 1162:     an empty file dummy.doc.
 1163:  forth-help-file-suffix
 1164:     The file names to search for in each directory specified by
 1165:     forth-help-load-path. Defaulted to '*.doc'. 
 1166: "
 1167:   (interactive)
 1168:   (kill-all-local-variables)
 1169:   (use-local-map forth-mode-map)
 1170:   (setq mode-name "Forth")
 1171:   (setq major-mode 'forth-mode)
 1172:   ;; convert buffer contents from block file format, if necessary
 1173:   (when (forth-detect-block-file-p)
 1174:     (widen)
 1175:     (message "Converting from Forth block source...")
 1176:     (forth-convert-from-block (point-min) (point-max))
 1177:     (message "Converting from Forth block source...done"))
 1178:   ;; if user switched from forth-block-mode to forth-mode, make sure the file
 1179:   ;; is now stored as normal strem source
 1180:   (when (equal buffer-file-format '(forth-blocks))
 1181:     (setq buffer-file-format nil))
 1182:   (forth-mode-variables)
 1183: ;  (if (not (forth-process-running-p))
 1184: ;      (run-forth forth-program-name))
 1185:   (run-hooks 'forth-mode-hook))
 1186: 
 1187: ;;;###autoload
 1188: (define-derived-mode forth-block-mode forth-mode "Forth Block Source" 
 1189:   "Major mode for editing Forth block source files, derived from 
 1190: `forth-mode'. Differences to `forth-mode' are:
 1191:  * files are converted to block format, when written (`buffer-file-format' 
 1192:    is set to `(forth-blocks)')
 1193:  * `forth-show-screen' and `forth-warn-long-lines' are t by default
 1194:   
 1195: Note that the length of lines in block files is limited to 64 characters.
 1196: When writing longer lines to a block file, a warning is displayed in the
 1197: echo area and the line is truncated. 
 1198: 
 1199: Another problem is imposed by block files that contain newline or tab 
 1200: characters. When Emacs converts such files back to block file format, 
 1201: it'll translate those characters to a number of spaces. However, when
 1202: you read such a file, a warning message is displayed in the echo area,
 1203: including a line number that may help you to locate and fix the problem.
 1204: 
 1205: So have a look at the *Messages* buffer, whenever you hear (or see) Emacs' 
 1206: bell during block file read/write operations."
 1207:   (setq buffer-file-format '(forth-blocks))
 1208:   (setq forth-show-screen t)
 1209:   (setq forth-warn-long-lines t)
 1210:   (setq forth-screen-number-string (format "%d" forth-block-base))
 1211:   (setq mode-line-format (append (reverse (cdr (reverse mode-line-format)))
 1212: 				 '("--S" forth-screen-number-string "-%-"))))
 1213: 
 1214: (add-hook 'forth-mode-hook
 1215:       '(lambda () 
 1216: 	 (make-local-variable 'compile-command)
 1217: 	 (setq compile-command "gforth ")
 1218: 	 (forth-hack-local-variables)
 1219: 	 (forth-customize-words)
 1220: 	 (forth-compile-words)
 1221: 	 (forth-change-function (point-min) (point-max) nil t)))
 1222: 
 1223: (defun forth-fill-paragraph () 
 1224:   "Fill comments (starting with '\'; do not fill code (block style
 1225: programmers who tend to fill code won't use emacs anyway:-)."
 1226:   ; Currently only comments at the start of the line are filled.
 1227:   ; Something like lisp-fill-paragraph may be better.  We cannot use
 1228:   ; fill-paragraph, because it removes the \ from the first comment
 1229:   ; line. Therefore we have to look for the first line of the comment
 1230:   ; and use fill-region.
 1231:   (interactive)
 1232:   (save-excursion
 1233:     (beginning-of-line)
 1234:     (while (and
 1235: 	     (= (forward-line -1) 0)
 1236: 	     (looking-at "[ \t]*\\\\g?[ \t]+")))
 1237:     (if (not (looking-at "[ \t]*\\\\g?[ \t]+"))
 1238: 	(forward-line 1))
 1239:     (let ((from (point))
 1240: 	  (to (save-excursion (forward-paragraph) (point))))
 1241:       (if (looking-at "[ \t]*\\\\g?[ \t]+")
 1242: 	  (progn (goto-char (match-end 0))
 1243: 		 (set-fill-prefix)
 1244: 		 (fill-region from to nil))))))
 1245: 
 1246: (defun forth-comment-indent ()
 1247:   (save-excursion
 1248:     (beginning-of-line)
 1249:     (if (looking-at ":[ \t]*")
 1250: 	(progn
 1251: 	  (end-of-line)
 1252: 	  (skip-chars-backward " \t\n")
 1253: 	  (1+ (current-column)))
 1254:       comment-column)))
 1255: 
 1256: 
 1257: ;; Forth commands
 1258: 
 1259: (defun forth-remove-tracers ()
 1260:   "Remove tracers of the form `~~ '. Queries the user for each occurrence."
 1261:   (interactive)
 1262:   (query-replace-regexp "\\(~~ \\| ~~$\\)" "" nil))
 1263: 
 1264: (defvar forth-program-name "gforth"
 1265:   "*Program invoked by the `run-forth' command.")
 1266: 
 1267: (defvar forth-band-name nil
 1268:   "*Band loaded by the `run-forth' command.")
 1269: 
 1270: (defvar forth-program-arguments nil
 1271:   "*Arguments passed to the Forth program by the `run-forth' command.")
 1272: 
 1273: (defun run-forth (command-line)
 1274:   "Run an inferior Forth process. Output goes to the buffer `*forth*'.
 1275: With argument, asks for a command line. Split up screen and run forth 
 1276: in the lower portion. The current-buffer when called will stay in the
 1277: upper portion of the screen, and all other windows are deleted.
 1278: Call run-forth again to make the *forth* buffer appear in the lower
 1279: part of the screen."
 1280:   (interactive
 1281:    (list (let ((default
 1282: 		 (or forth-process-command-line
 1283: 		     (forth-default-command-line))))
 1284: 	   (if current-prefix-arg
 1285: 	       (read-string "Run Forth: " default)
 1286: 	       default))))
 1287:   (setq forth-process-command-line command-line)
 1288:   (forth-start-process command-line)
 1289:   (forth-split)
 1290:   (forth-set-runlight forth-runlight:input))
 1291: 
 1292: (defun run-forth-if-not ()
 1293:   (if (not (forth-process-running-p))
 1294:       (run-forth forth-program-name)))
 1295: 
 1296: (defun reset-forth ()
 1297:   "Reset the Forth process."
 1298:   (interactive)
 1299:   (let ((process (get-process forth-program-name)))
 1300:     (cond ((or (not process)
 1301: 	       (not (eq (process-status process) 'run))
 1302: 	       (yes-or-no-p
 1303: "The Forth process is running, are you SURE you want to reset it? "))
 1304: 	   (message "Resetting Forth process...")
 1305: 	   (forth-reload)
 1306: 	   (message "Resetting Forth process...done")))))
 1307: 
 1308: (defun forth-default-command-line ()
 1309:   (concat forth-program-name
 1310: 	  (if forth-program-arguments
 1311: 	      (concat " " forth-program-arguments)
 1312: 	      "")))
 1313: 
 1314: ;;;; Internal Variables
 1315: 
 1316: (defvar forth-process-command-line nil
 1317:   "Command used to start the most recent Forth process.")
 1318: 
 1319: (defvar forth-previous-send ""
 1320:   "Most recent expression transmitted to the Forth process.")
 1321: 
 1322: (defvar forth-process-filter-queue '()
 1323:   "Queue used to synchronize filter actions properly.")
 1324: 
 1325: (defvar forth-prompt "ok"
 1326:   "The current forth prompt string.")
 1327: 
 1328: (defvar forth-start-hook nil
 1329:   "If non-nil, a procedure to call when the Forth process is started.
 1330: When called, the current buffer will be the Forth process-buffer.")
 1331: 
 1332: (defvar forth-signal-death-message nil
 1333:   "If non-nil, causes a message to be generated when the Forth process dies.")
 1334: 
 1335: (defvar forth-percent-height 50
 1336:   "Tells run-forth how high the upper window should be in percent.")
 1337: 
 1338: (defconst forth-runlight:input ?I
 1339:   "The character displayed when the Forth process is waiting for input.")
 1340: 
 1341: (defvar forth-mode-string ""
 1342:   "String displayed in the mode line when the Forth process is running.")
 1343: 
 1344: ;;;; Evaluation Commands
 1345: 
 1346: (defun forth-send-string (&rest strings)
 1347:   "Send the string arguments to the Forth process.
 1348: The strings are concatenated and terminated by a newline."
 1349:   (cond ((forth-process-running-p)
 1350: 	 (forth-send-string-1 strings))
 1351: 	((yes-or-no-p "The Forth process has died.  Reset it? ")
 1352: 	 (reset-forth)
 1353: 	 (goto-char (point-max))
 1354: 	 (forth-send-string-1 strings))))
 1355: 
 1356: (defun forth-send-string-1 (strings)
 1357:   (let ((string (apply 'concat strings)))
 1358:     (forth-send-string-2 string)))
 1359: 
 1360: (defun forth-send-string-2 (string)
 1361:   (let ((process (get-process forth-program-name)))
 1362:     (if (not (eq (current-buffer) (get-buffer forth-program-name)))
 1363: 	(progn
 1364: 	 (forth-process-filter-output string)
 1365: 	 (forth-process-filter:finish)))
 1366:     (send-string process (concat string "\n"))
 1367:     (if (eq (current-buffer) (process-buffer process))
 1368: 	(set-marker (process-mark process) (point)))))
 1369: 
 1370: 
 1371: (defun forth-send-region (start end)
 1372:   "Send the current region to the Forth process.
 1373: The region is sent terminated by a newline."
 1374:   (interactive "r")
 1375:   (let ((process (get-process forth-program-name)))
 1376:     (if (and process (eq (current-buffer) (process-buffer process)))
 1377: 	(progn (goto-char end)
 1378: 	       (set-marker (process-mark process) end))))
 1379:   (forth-send-string "\n" (buffer-substring start end) "\n"))
 1380: 
 1381: (defun forth-end-of-paragraph ()
 1382:   (if (looking-at "[\t\n ]+") (skip-chars-backward  "\t\n "))
 1383:   (if (not (re-search-forward "\n[ \t]*\n" nil t))
 1384:       (goto-char (point-max))))
 1385: 
 1386: (defun forth-send-paragraph ()
 1387:   "Send the current or the previous paragraph to the Forth process"
 1388:   (interactive)
 1389:   (let (end)
 1390:     (save-excursion
 1391:       (forth-end-of-paragraph)
 1392:       (skip-chars-backward  "\t\n ")
 1393:       (setq end (point))
 1394:       (if (re-search-backward "\n[ \t]*\n" nil t)
 1395: 	  (setq start (point))
 1396: 	(goto-char (point-min)))
 1397:       (skip-chars-forward  "\t\n ")
 1398:       (forth-send-region (point) end))))
 1399:   
 1400: (defun forth-send-buffer ()
 1401:   "Send the current buffer to the Forth process."
 1402:   (interactive)
 1403:   (if (eq (current-buffer) (forth-process-buffer))
 1404:       (error "Not allowed to send this buffer's contents to Forth"))
 1405:   (forth-send-region (point-min) (point-max)))
 1406: 
 1407: 
 1408: ;;;; Basic Process Control
 1409: 
 1410: (defun forth-start-process (command-line)
 1411:   (let ((buffer (get-buffer-create "*forth*")))
 1412:     (let ((process (get-buffer-process buffer)))
 1413:       (save-excursion
 1414: 	(set-buffer buffer)
 1415: 	(progn (if process (delete-process process))
 1416: 	       (goto-char (point-max))
 1417: 	       (setq mode-line-process '(": %s"))
 1418: 	       (add-to-global-mode-string 'forth-mode-string)
 1419: 	       (setq process
 1420: 		     (apply 'start-process
 1421: 			    (cons forth-program-name
 1422: 				  (cons buffer
 1423: 					(forth-parse-command-line
 1424: 					 command-line)))))
 1425: 	       (set-marker (process-mark process) (point-max))
 1426: 	       (forth-process-filter-initialize t)
 1427: 	       (forth-modeline-initialize)
 1428: 	       (set-process-sentinel process 'forth-process-sentinel)
 1429: 	       (set-process-filter process 'forth-process-filter)
 1430: 	       (run-hooks 'forth-start-hook)))
 1431:     buffer)))
 1432: 
 1433: (defun forth-parse-command-line (string)
 1434:   (setq string (substitute-in-file-name string))
 1435:   (let ((start 0)
 1436: 	(result '()))
 1437:     (while start
 1438:       (let ((index (string-match "[ \t]" string start)))
 1439: 	(setq start
 1440: 	      (cond ((not index)
 1441: 		     (setq result
 1442: 			   (cons (substring string start)
 1443: 				 result))
 1444: 		     nil)
 1445: 		    ((= index start)
 1446: 		     (string-match "[^ \t]" string start))
 1447: 		    (t
 1448: 		     (setq result
 1449: 			   (cons (substring string start index)
 1450: 				 result))
 1451: 		     (1+ index))))))
 1452:     (nreverse result)))
 1453: 
 1454: 
 1455: (defun forth-process-running-p ()
 1456:   "True iff there is a Forth process whose status is `run'."
 1457:   (let ((process (get-process forth-program-name)))
 1458:     (and process
 1459: 	 (eq (process-status process) 'run))))
 1460: 
 1461: (defun forth-process-buffer ()
 1462:   (let ((process (get-process forth-program-name)))
 1463:     (and process (process-buffer process))))
 1464: 
 1465: ;;;; Process Filter
 1466: 
 1467: (defun forth-process-sentinel (proc reason)
 1468:   (let ((inhibit-quit nil))
 1469:     (forth-process-filter-initialize (eq reason 'run))
 1470:     (if (eq reason 'run)
 1471: 	(forth-modeline-initialize)
 1472: 	(setq forth-mode-string "")))
 1473:   (if (and (not (memq reason '(run stop)))
 1474: 	   forth-signal-death-message)
 1475:       (progn (beep)
 1476: 	     (message
 1477: "The Forth process has died!  Do M-x reset-forth to restart it"))))
 1478: 
 1479: (defun forth-process-filter-initialize (running-p)
 1480:   (setq forth-process-filter-queue (cons '() '()))
 1481:   (setq forth-prompt "ok"))
 1482: 
 1483: 
 1484: (defun forth-process-filter (proc string)
 1485:   (forth-process-filter-output string)
 1486:   (forth-process-filter:finish))
 1487: 
 1488: (defun forth-process-filter:enqueue (action)
 1489:   (let ((next (cons action '())))
 1490:     (if (cdr forth-process-filter-queue)
 1491: 	(setcdr (cdr forth-process-filter-queue) next)
 1492: 	(setcar forth-process-filter-queue next))
 1493:     (setcdr forth-process-filter-queue next)))
 1494: 
 1495: (defun forth-process-filter:finish ()
 1496:   (while (car forth-process-filter-queue)
 1497:     (let ((next (car forth-process-filter-queue)))
 1498:       (setcar forth-process-filter-queue (cdr next))
 1499:       (if (not (cdr next))
 1500: 	  (setcdr forth-process-filter-queue '()))
 1501:       (apply (car (car next)) (cdr (car next))))))
 1502: 
 1503: ;;;; Process Filter Output
 1504: 
 1505: (defun forth-process-filter-output (&rest args)
 1506:   (if (not (and args
 1507: 		(null (cdr args))
 1508: 		(stringp (car args))
 1509: 		(string-equal "" (car args))))
 1510:       (forth-process-filter:enqueue
 1511:        (cons 'forth-process-filter-output-1 args))))
 1512: 
 1513: (defun forth-process-filter-output-1 (&rest args)
 1514:   (save-excursion
 1515:     (forth-goto-output-point)
 1516:     (apply 'insert-before-markers args)))
 1517: 
 1518: (defun forth-guarantee-newlines (n)
 1519:   (save-excursion
 1520:     (forth-goto-output-point)
 1521:     (let ((stop nil))
 1522:       (while (and (not stop)
 1523: 		  (bolp))
 1524: 	(setq n (1- n))
 1525: 	(if (bobp)
 1526: 	    (setq stop t)
 1527: 	  (backward-char))))
 1528:     (forth-goto-output-point)
 1529:     (while (> n 0)
 1530:       (insert-before-markers ?\n)
 1531:       (setq n (1- n)))))
 1532: 
 1533: (defun forth-goto-output-point ()
 1534:   (let ((process (get-process forth-program-name)))
 1535:     (set-buffer (process-buffer process))
 1536:     (goto-char (process-mark process))))
 1537: 
 1538: (defun forth-modeline-initialize ()
 1539:   (setq forth-mode-string "  "))
 1540: 
 1541: (defun forth-set-runlight (runlight)
 1542:   (aset forth-mode-string 0 runlight)
 1543:   (forth-modeline-redisplay))
 1544: 
 1545: (defun forth-modeline-redisplay ()
 1546:   (save-excursion (set-buffer (other-buffer)))
 1547:   (set-buffer-modified-p (buffer-modified-p))
 1548:   (sit-for 0))
 1549: 
 1550: ;;;; Process Filter Operations
 1551: 
 1552: (defun add-to-global-mode-string (x)
 1553:   (cond ((null global-mode-string)
 1554: 	 (setq global-mode-string (list "" x " ")))
 1555: 	((not (memq x global-mode-string))
 1556: 	 (setq global-mode-string
 1557: 	       (cons ""
 1558: 		     (cons x
 1559: 			   (cons " "
 1560: 				 (if (equal "" (car global-mode-string))
 1561: 				     (cdr global-mode-string)
 1562: 				     global-mode-string))))))))
 1563: 
 1564: 
 1565: ;; Misc
 1566: 
 1567: (setq auto-mode-alist (append auto-mode-alist
 1568: 				'(("\\.fs$" . forth-mode))))
 1569: 
 1570: (defun forth-split ()
 1571:   (interactive)
 1572:   (forth-split-1 "*forth*"))
 1573: 
 1574: (defun forth-split-1 (buffer)
 1575:   (if (not (eq (window-buffer) (get-buffer buffer)))
 1576:       (progn
 1577: 	(delete-other-windows)
 1578: 	(split-window-vertically
 1579: 	 (/ (* (screen-height) forth-percent-height) 100))
 1580: 	(other-window 1)
 1581: 	(switch-to-buffer buffer)
 1582: 	(goto-char (point-max))
 1583: 	(other-window 1))))
 1584:     
 1585: (defun forth-reload ()
 1586:   (interactive)
 1587:   (let ((process (get-process forth-program-name)))
 1588:     (if process (kill-process process t)))
 1589:   (sleep-for 0 100)
 1590:   (forth-mode))
 1591: 
 1592: 
 1593: ;; Special section for forth-help
 1594: 
 1595: (defvar forth-help-buffer "*Forth-help*"
 1596:   "Buffer used to display the requested documentation.")
 1597: 
 1598: (defvar forth-help-load-path nil
 1599:   "List of directories to search through to find *.doc
 1600:  (forth-help-file-suffix) files. Nil means current default directory.
 1601:  The specified directories must contain at least one .doc file. If it
 1602:  does not and you still want the load-path to scan that directory, create
 1603:  an empty file dummy.doc.")
 1604: 
 1605: (defvar forth-help-file-suffix "*.doc"
 1606:   "The file names to search for in each directory.")
 1607: 
 1608: (setq forth-search-command-prefix "grep -n \"^    [^(]* ")
 1609: (defvar forth-search-command-suffix "/dev/null")
 1610: (defvar forth-grep-error-regexp ": No such file or directory")
 1611: 
 1612: (defun forth-function-called-at-point ()
 1613:   "Return the space delimited word a point."
 1614:   (save-excursion
 1615:     (save-restriction
 1616:       (narrow-to-region (max (point-min) (- (point) 1000)) (point-max))
 1617:       (skip-chars-backward "^ \t\n" (point-min))
 1618:       (if (looking-at "[ \t\n]")
 1619: 	  (forward-char 1))
 1620:       (let (obj (p (point)))
 1621: 	(skip-chars-forward "^ \t\n")
 1622: 	(buffer-substring p (point))))))
 1623: 
 1624: (defun forth-help-names-extend-comp (path-list result)
 1625:   (cond ((null path-list) result)
 1626: 	((null (car path-list))
 1627: 	 (forth-help-names-extend-comp (cdr path-list) 
 1628: 	       (concat result forth-help-file-suffix " ")))
 1629: 	(t (forth-help-names-extend-comp
 1630: 	    (cdr path-list) (concat result
 1631: 				    (expand-file-name (car path-list)) "/"
 1632: 				    forth-help-file-suffix " ")))))
 1633: 
 1634: (defun forth-help-names-extended ()
 1635:   (if forth-help-load-path
 1636:       (forth-help-names-extend-comp forth-help-load-path "")
 1637:     (error "forth-help-load-path not specified")))
 1638: 
 1639: 
 1640: ;(define-key forth-mode-map "\C-hf" 'forth-documentation)
 1641: 
 1642: (defun forth-documentation (function)
 1643:   "Display the full documentation of FORTH word."
 1644:   (interactive
 1645:    (let ((fn (forth-function-called-at-point))
 1646: 	 (enable-recursive-minibuffers t)	     
 1647: 	 search-list
 1648: 	 val)
 1649:      (setq val (read-string (format "Describe forth word (default %s): " fn)))
 1650:      (list (if (equal val "") fn val))))
 1651:   (forth-get-doc (concat forth-search-command-prefix
 1652: 			 (grep-regexp-quote (concat function " ("))
 1653: 			 "[^)]*\-\-\" " (forth-help-names-extended)
 1654: 			 forth-search-command-suffix))
 1655:   (message "C-x C-m switches back to the forth interaction window"))
 1656: 
 1657: (defun forth-get-doc (command)
 1658:   "Display the full documentation of command."
 1659:   (let ((curwin (get-buffer-window (window-buffer)))
 1660: 	reswin
 1661: 	pointmax)
 1662:     (with-output-to-temp-buffer forth-help-buffer
 1663:       (progn
 1664: 	(call-process "sh" nil forth-help-buffer t "-c" command)
 1665: 	(setq reswin (get-buffer-window forth-help-buffer))))
 1666:     (setq reswin (get-buffer-window forth-help-buffer))
 1667:     (select-window reswin)
 1668:     (save-excursion
 1669:       (goto-char (setq pointmax (point-max)))
 1670:       (insert "--------------------\n\n"))
 1671:     (let (fd doc) 
 1672:       (while (setq fd (forth-get-file-data pointmax))
 1673: 	(setq doc (forth-get-doc-string fd))
 1674: 	(save-excursion
 1675: 	  (goto-char (point-max))
 1676: 	  (insert (substring (car fd) (string-match "[^/]*$" (car fd)))
 1677: 		  ":\n\n" doc "\n")))
 1678:       (if (not doc)
 1679: 	  (progn (goto-char (point-max)) (insert "Not found"))))
 1680:     (select-window curwin)))
 1681:   
 1682: (defun forth-skip-error-lines ()
 1683:   (let ((lines 0))
 1684:     (save-excursion
 1685:       (while (re-search-forward forth-grep-error-regexp nil t)
 1686: 	(beginning-of-line)
 1687: 	(forward-line 1)
 1688: 	(setq lines (1+ lines))))
 1689:     (forward-line lines)))
 1690: 
 1691: (defun forth-get-doc-string (fd)
 1692:   "Find file (car fd) and extract documentation from line (nth 1 fd)."
 1693:   (let (result)
 1694:     (save-window-excursion
 1695:       (find-file (car fd))
 1696:       (goto-line (nth 1 fd))
 1697:       (if (not (eq (nth 1 fd) (1+ (count-lines (point-min) (point)))))
 1698: 	  (error "forth-get-doc-string: serious error"))
 1699:       (if (not (re-search-backward "\n[\t ]*\n" nil t))
 1700: 	  (goto-char (point-min))
 1701: 	(goto-char (match-end 0)))
 1702:       (let ((p (point)))
 1703: 	(if (not (re-search-forward "\n[\t ]*\n" nil t))
 1704: 	    (goto-char (point-max)))
 1705: 	(setq result (buffer-substring p (point))))
 1706:       (bury-buffer (current-buffer)))
 1707:     result))
 1708: 
 1709: (defun forth-get-file-data (limit)
 1710:   "Parse grep output and return '(filename line#) list. Return nil when
 1711:  passing limit."
 1712:   (forth-skip-error-lines)
 1713:   (if (< (point) limit)
 1714:       (let ((result (forth-get-file-data-cont limit)))
 1715: 	(forward-line 1)
 1716: 	(beginning-of-line)
 1717: 	result)))
 1718: 
 1719: (defun forth-get-file-data-cont (limit)
 1720:   (let (result)
 1721:     (let ((p (point)))
 1722:       (skip-chars-forward "^:")
 1723:       (setq result (buffer-substring p (point))))
 1724:     (if (< (point) limit)
 1725: 	(let ((p (1+ (point))))
 1726: 	  (forward-char 1)
 1727: 	  (skip-chars-forward "^:")
 1728: 	  (list result (string-to-int (buffer-substring p (point))))))))
 1729: 
 1730: (defun grep-regexp-quote (str)
 1731:   (let ((i 0) (m 1) (res ""))
 1732:     (while (/= m 0)
 1733:       (setq m (string-to-char (substring str i)))
 1734:       (if (/= m 0)
 1735: 	  (progn
 1736: 	    (setq i (1+ i))
 1737: 	    (if (string-match (regexp-quote (char-to-string m))
 1738: 			      ".*\\^$[]")
 1739: 		(setq res (concat res "\\")))
 1740: 	    (setq res (concat res (char-to-string m))))))
 1741:     res))
 1742: 
 1743: 
 1744: (define-key forth-mode-map "\C-x\C-e" 'compile)
 1745: (define-key forth-mode-map "\C-x\C-n" 'next-error)
 1746: (require 'compile "compile")
 1747: 
 1748: (defvar forth-compile-command "gforth ")
 1749: ;(defvar forth-compilation-window-percent-height 30)
 1750: 
 1751: (defun forth-compile (command)
 1752:   (interactive (list (setq forth-compile-command (read-string "Compile command: " forth-compile-command))))
 1753:   (forth-split-1 "*compilation*")
 1754:   (setq ctools-compile-command command)
 1755:   (compile1 ctools-compile-command "No more errors"))
 1756: 
 1757: 
 1758: ;;; Forth menu
 1759: ;;; Mikael Karlsson <qramika@eras70.ericsson.se>
 1760: 
 1761: (cond ((string-match "XEmacs\\|Lucid" emacs-version)
 1762:        (require 'func-menu)
 1763: 
 1764:   (defconst fume-function-name-regexp-forth
 1765:    "^\\(:\\)[ \t]+\\([^ \t]*\\)"
 1766:    "Expression to get word definitions in Forth.")
 1767: 
 1768:   (setq fume-function-name-regexp-alist
 1769:       (append '((forth-mode . fume-function-name-regexp-forth) 
 1770:              ) fume-function-name-regexp-alist))
 1771: 
 1772:   ;; Find next forth word in the buffer
 1773:   (defun fume-find-next-forth-function-name (buffer)
 1774:     "Searches for the next forth word in BUFFER."
 1775:     (set-buffer buffer)
 1776:     (if (re-search-forward fume-function-name-regexp nil t)
 1777:       (let ((beg (match-beginning 2))
 1778:             (end (match-end 2)))
 1779:         (cons (buffer-substring beg end) beg))))
 1780: 
 1781:   (setq fume-find-function-name-method-alist
 1782:   (append '((forth-mode    . fume-find-next-forth-function-name))))
 1783: 
 1784:   ))
 1785: ;;; End Forth menu
 1786: 
 1787: ;;; File folding of forth-files
 1788: ;;; uses outline
 1789: ;;; Toggle activation with M-x fold-f (when editing a forth-file) 
 1790: ;;; Use f9 to expand, f10 to hide, Or the menubar in xemacs
 1791: ;;;
 1792: ;;; Works most of the times but loses sync with the cursor occasionally 
 1793: ;;; Could be improved by also folding on comments
 1794: 
 1795: (require 'outline)
 1796: 
 1797: (defun f-outline-level ()
 1798: 	(cond	((looking-at "\\`\\\\")
 1799: 			0)
 1800: 		((looking-at "\\\\ SEC")
 1801: 			0)
 1802: 		((looking-at "\\\\ \\\\ .*")
 1803: 			0)
 1804: 		((looking-at "\\\\ DEFS")
 1805: 			1)
 1806: 		((looking-at "\\/\\* ")
 1807: 			1)
 1808: 		((looking-at ": .*")
 1809: 			1)
 1810: 		((looking-at "\\\\G")
 1811: 			2)
 1812: 		((looking-at "[ \t]+\\\\")
 1813: 			3))
 1814: )			
 1815: 
 1816: (defun fold-f  ()
 1817:    (interactive)
 1818:    (add-hook 'outline-minor-mode-hook 'hide-body)
 1819: 
 1820:    ; outline mode header start, i.e. find word definitions
 1821: ;;;   (setq  outline-regexp  "^\\(:\\)[ \t]+\\([^ \t]*\\)")
 1822:    (setq  outline-regexp  "\\`\\\\\\|:\\|\\\\ SEC\\|\\\\G\\|[ \t]+\\\\\\|\\\\ DEFS\\|\\/\\*\\|\\\\ \\\\ .*")
 1823:    (setq outline-level 'f-outline-level)
 1824: 
 1825:    (outline-minor-mode)
 1826:    (define-key outline-minor-mode-map '(shift up) 'hide-sublevels)
 1827:    (define-key outline-minor-mode-map '(shift right) 'show-children)
 1828:    (define-key outline-minor-mode-map '(shift left) 'hide-subtree)
 1829:    (define-key outline-minor-mode-map '(shift down) 'show-subtree)
 1830: 
 1831: )
 1832: 
 1833: ;;(define-key global-map '(shift up) 'fold-f)
 1834: 
 1835: ;;; end file folding
 1836: 
 1837: ;;; func-menu is a package that scans your source file for function definitions
 1838: ;;; and makes a menubar entry that lets you jump to any particular function
 1839: ;;; definition by selecting it from the menu.  The following code turns this on
 1840: ;;; for all of the recognized languages.  Scanning the buffer takes some time,
 1841: ;;; but not much.
 1842: ;;;
 1843: (cond ((string-match "XEmacs\\|Lucid" emacs-version)
 1844:        (require 'func-menu)
 1845: ;;       (define-key global-map 'f8 'function-menu)
 1846:        (add-hook 'find-fible-hooks 'fume-add-menubar-entry)
 1847: ;       (define-key global-map "\C-cg" 'fume-prompt-function-goto)
 1848: ;       (define-key global-map '(shift button3) 'mouse-function-menu)
 1849: ))
 1850: 
 1851: ;;; gforth.el ends here

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