Annotation of gforth/gforth.el, revision 1.70

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

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