File:  [gforth] / gforth / gforth.el
Revision 1.57: download - view: text, annotated - select for diffs
Thu Jan 17 19:26:34 2002 UTC (22 years, 3 months ago) by dvdkhlng
Branches: MAIN
CVS tags: HEAD
added and fixed `require's, fixed byte-code-compilation relevant
things (avoid free variables, top-level `require's etc), added
`(provide 'forth-mode)'

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

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