Annotation of gforth/gforth.el, revision 1.69

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))
                    437:        (("repeat" "[repeat]") (-4 . 0) (0 . -4))
1.57      dvdkhlng  438:        (("\\g") (-2 . 2) (0 . 0))))
                    439: 
                    440: (defvar forth-local-indent-words nil 
                    441:   "List of Forth words to prepend to `forth-indent-words', when a forth-mode
                    442: buffer is created. Should be set by a Forth source, using a local variables 
                    443: list at the end of the file (\"Local Variables: ... forth-local-words: ... 
                    444: End:\" construct).")
1.51      dvdkhlng  445: 
1.57      dvdkhlng  446: (defvar forth-custom-indent-words nil
                    447:   "List of Forth words to prepend to `forth-indent-words'. Should be set in
                    448:  your .emacs.")
1.48      pazsan    449: 
1.57      dvdkhlng  450: (defvar forth-indent-level 4
                    451:   "*Indentation of Forth statements.")
                    452: (defvar forth-minor-indent-level 2
                    453:   "*Minor indentation of Forth statements.")
                    454: (defvar forth-compiled-indent-words nil)
1.48      pazsan    455: 
1.55      dvdkhlng  456: ;(setq debug-on-error t)
1.48      pazsan    457: 
1.50      dvdkhlng  458: ;; Filter list by predicate. This is a somewhat standard function for 
1.48      pazsan    459: ;; functional programming languages. So why isn't it already implemented 
                    460: ;; in Lisp??
1.50      dvdkhlng  461: (defun forth-filter (predicate list)
1.48      pazsan    462:   (let ((filtered nil))
                    463:     (mapcar (lambda (item)
1.50      dvdkhlng  464:              (when (funcall predicate item)
1.48      pazsan    465:                (if filtered
                    466:                    (nconc filtered (list item))
                    467:                  (setq filtered (cons item nil))))
                    468:              nil) list)
                    469:     filtered))
                    470: 
                    471: ;; Helper function for `forth-compile-word': return whether word has to be
                    472: ;; added to the compiled word list, for syntactic parsing and hilighting.
                    473: (defun forth-words-filter (word)
                    474:   (let* ((hilight (nth 2 word))
                    475:         (level (cdr hilight))
                    476:         (parsing-flag (nth 3 word)))
                    477:     (or parsing-flag 
                    478:        (<= level forth-hilight-level))))
                    479: 
                    480: ;; Helper function for `forth-compile-word': translate one entry from 
                    481: ;; `forth-words' into the form  (regexp regexp-depth word-description)
                    482: (defun forth-compile-words-mapper (word)
1.59      dvdkhlng  483:   ;; warning: we cannot rely on regexp-opt's PAREN argument, since
                    484:   ;; XEmacs will use shy parens by default :-(
1.48      pazsan    485:   (let* ((matcher (car word))
1.59      dvdkhlng  486:         (regexp 
                    487:          (concat "\\(" (cond ((stringp matcher) matcher)
                    488:                              ((listp matcher) (regexp-opt matcher))
                    489:                              (t (error "Invalid matcher `%s'")))
                    490:                  "\\)"))
1.48      pazsan    491:         (depth (regexp-opt-depth regexp))
                    492:         (description (cdr word)))
                    493:     (list regexp depth description)))
                    494: 
                    495: ;; Read `words' and create a compiled representation suitable for efficient
                    496: ;; parsing of the form  
                    497: ;; (regexp (subexp-count word-description) (subexp-count2 word-description2)
                    498: ;;  ...)
1.49      dvdkhlng  499: (defun forth-compile-wordlist (words)
1.48      pazsan    500:   (let* ((mapped (mapcar 'forth-compile-words-mapper words))
                    501:         (regexp (concat "\\<\\(" 
                    502:                         (mapconcat 'car mapped "\\|")
                    503:                         "\\)\\>"))
                    504:         (sub-count 2)
                    505:         (sub-list (mapcar 
                    506:                    (lambda (i) 
                    507:                      (let ((sub (cons sub-count (nth 2 i))))
                    508:                        (setq sub-count (+ sub-count (nth 1 i)))
                    509:                        sub 
                    510:                        )) 
                    511:                    mapped)))
                    512:     (let ((result (cons regexp sub-list)))
                    513:       (byte-compile 'result)
                    514:       result)))
                    515: 
1.49      dvdkhlng  516: (defun forth-compile-words ()
                    517:   "Compile the the words from `forth-words' and `forth-indent-words' into
                    518:  the format that's later used for doing the actual hilighting/indentation.
1.51      dvdkhlng  519:  Store the resulting compiled wordlists in `forth-compiled-words' and 
1.49      dvdkhlng  520: `forth-compiled-indent-words', respective"
                    521:   (setq forth-compiled-words 
                    522:        (forth-compile-wordlist 
                    523:         (forth-filter 'forth-words-filter forth-words)))
                    524:   (setq forth-compiled-indent-words 
                    525:        (forth-compile-wordlist forth-indent-words)))
                    526: 
                    527: (defun forth-hack-local-variables ()
1.51      dvdkhlng  528:   "Parse and bind local variables, set in the contents of the current 
                    529:  forth-mode buffer. Prepend `forth-local-words' to `forth-words' and 
                    530:  `forth-local-indent-words' to `forth-indent-words'."
1.49      dvdkhlng  531:   (hack-local-variables)
                    532:   (setq forth-words (append forth-local-words forth-words))
                    533:   (setq forth-indent-words (append forth-local-indent-words 
                    534:                                   forth-indent-words)))
                    535: 
1.51      dvdkhlng  536: (defun forth-customize-words ()
                    537:   "Add the words from `forth-custom-words' and `forth-custom-indent-words'
                    538:  to `forth-words' and `forth-indent-words', respective. Add 
                    539:  `forth-objects-words' and/or `forth-oof-words' to `forth-words', if
                    540:  `forth-use-objects' and/or `forth-use-oof', respective is set."
                    541:   (setq forth-words (append forth-custom-words forth-words
                    542:                            (if forth-use-oof forth-oof-words nil)
                    543:                            (if forth-use-objects forth-objects-words nil)))
                    544:   (setq forth-indent-words (append 
                    545:                            forth-custom-indent-words forth-indent-words)))
                    546: 
                    547: 
                    548: 
1.48      pazsan    549: ;; get location of first character of previous forth word that's got 
                    550: ;; properties
                    551: (defun forth-previous-start (pos)
                    552:   (let* ((word (get-text-property pos 'forth-word))
                    553:         (prev (previous-single-property-change 
                    554:                (min (point-max) (1+ pos)) 'forth-word 
                    555:                (current-buffer) (point-min))))
                    556:     (if (or (= (point-min) prev) word) prev
                    557:       (if (get-text-property (1- prev) 'forth-word)
                    558:          (previous-single-property-change 
                    559:           prev 'forth-word (current-buffer) (point-min))
                    560:        (point-min)))))
                    561: 
                    562: ;; Get location of the last character of the current/next forth word that's
                    563: ;; got properties, text that's parsed by the word is considered as parts of 
                    564: ;; the word.
                    565: (defun forth-next-end (pos)
                    566:   (let* ((word (get-text-property pos 'forth-word))
                    567:         (next (next-single-property-change pos 'forth-word 
                    568:                                            (current-buffer) (point-max))))
                    569:     (if word next
                    570:       (if (get-text-property next 'forth-word)
                    571:          (next-single-property-change 
                    572:           next 'forth-word (current-buffer) (point-max))
                    573:        (point-max)))))
                    574: 
                    575: (defun forth-next-whitespace (pos)
                    576:   (save-excursion
                    577:     (goto-char pos)
                    578:     (skip-syntax-forward "-" (point-max))
                    579:     (point)))
                    580: (defun forth-previous-word (pos)
                    581:   (save-excursion
                    582:     (goto-char pos)
                    583:     (re-search-backward "\\<" pos (point-min) 1)
                    584:     (point)))
                    585: 
                    586: ;; Delete all properties, used by Forth mode, from `from' to `to'.
                    587: (defun forth-delete-properties (from to)
                    588:   (remove-text-properties 
1.55      dvdkhlng  589:    from to '(face nil fontified nil 
                    590:                  forth-parsed nil forth-word nil forth-state nil)))
1.48      pazsan    591: 
                    592: ;; Get the index of the branch of the most recently evaluated regular 
                    593: ;; expression that matched. (used for identifying branches "a\\|b\\|c...")
                    594: (defun forth-get-regexp-branch ()
                    595:   (let ((count 2))
1.59      dvdkhlng  596:     (while (not (condition-case err (match-beginning count)
                    597:                  (args-out-of-range t)))  ; XEmacs requires error handling
1.48      pazsan    598:       (setq count (1+ count)))
                    599:     count))
                    600: 
                    601: ;; seek to next forth-word and return its "word-description"
                    602: (defun forth-next-known-forth-word (to)
                    603:   (if (<= (point) to)
                    604:       (progn
                    605:        (let* ((regexp (car forth-compiled-words))
                    606:               (pos (re-search-forward regexp to t)))
                    607:          (if pos (let ((branch (forth-get-regexp-branch))
                    608:                        (descr (cdr forth-compiled-words)))
                    609:                    (goto-char (match-beginning 0))
                    610:                    (cdr (assoc branch descr)))
                    611:            'nil)))
                    612:     nil))
                    613: 
                    614: ;; Set properties of forth word at `point', eventually parsing subsequent 
                    615: ;; words, and parsing all whitespaces. Set point to delimiter after word.
                    616: ;; The word, including it's parsed text gets the `forth-word' property, whose 
                    617: ;; value is unique, and may be used for getting the word's start/end 
                    618: ;; positions.
                    619: (defun forth-set-word-properties (state data)
                    620:   (let* ((start (point))
                    621:         (end (progn (re-search-forward "[ \t]\\|$" (point-max) 1)
                    622:                     (point)))
                    623:         (type (car data))
                    624:         (hilight (nth 1 data))
                    625:         (bad-word (and (not state) (eq type 'compile-only)))
                    626:         (hlface (if bad-word font-lock-warning-face
                    627:                   (if (<= (cdr hilight) forth-hilight-level)
                    628:                       (car hilight) nil))))
                    629:     (when hlface (put-text-property start end 'face hlface))
                    630:     ;; if word parses in current state, process parsed range of text
                    631:     (when (or (not state) (eq type 'compile-only) (eq type 'immediate))
                    632:       (let ((parse-data (nthcdr 2 data)))
                    633:        (while parse-data
                    634:          (let ((delim (nth 0 parse-data))
                    635:                (skip-leading (nth 1 parse-data))
                    636:                (parse-type (nth 2 parse-data))
                    637:                (parsed-hilight (nth 3 parse-data))
                    638:                (parse-start (point))
                    639:                (parse-end))
                    640:            (when skip-leading
                    641:              (while (and (looking-at delim) (> (match-end 0) (point))
                    642:                          (not (looking-at "\n")))
                    643:                (forward-char)))
                    644:            (re-search-forward delim (point-max) 1)
                    645:            (setq parse-end (point))
                    646:            (forth-delete-properties end parse-end)
                    647:            (when (<= (cdr parsed-hilight) forth-hilight-level)
                    648:              (put-text-property 
                    649:               parse-start parse-end 'face (car parsed-hilight)))
                    650:            (put-text-property 
                    651:             parse-start parse-end 'forth-parsed parse-type)
                    652:            (setq end parse-end)
                    653:            (setq parse-data (nthcdr 4 parse-data))))))
                    654:     (put-text-property start end 'forth-word start)))
                    655: 
                    656: ;; Search for known Forth words in the range `from' to `to', using 
                    657: ;; `forth-next-known-forth-word' and set their properties via 
                    658: ;; `forth-set-word-properties'.
1.51      dvdkhlng  659: (defun forth-update-properties (from to &optional loudly)
1.48      pazsan    660:   (save-excursion
1.51      dvdkhlng  661:     (let ((msg-count 0) (state) (word-descr) (last-location))
1.48      pazsan    662:       (goto-char (forth-previous-word (forth-previous-start 
                    663:                                       (max (point-min) (1- from)))))
                    664:       (setq to (forth-next-end (min (point-max) (1+ to))))
                    665:       ;; `to' must be on a space delimiter, if a parsing word was changed
                    666:       (setq to (forth-next-whitespace to))
                    667:       (setq state (get-text-property (point) 'forth-state))
                    668:       (setq last-location (point))
                    669:       (forth-delete-properties (point) to)
1.55      dvdkhlng  670:       (put-text-property (point) to 'fontified t)
1.48      pazsan    671:       ;; hilight loop...
                    672:       (while (setq word-descr (forth-next-known-forth-word to))
1.51      dvdkhlng  673:        (when loudly
                    674:          (when (equal 0 (% msg-count 100))
                    675:            (message "Parsing Forth code...%s"
                    676:                     (make-string (/ msg-count 100) ?.)))
                    677:          (setq msg-count (1+ msg-count)))
1.48      pazsan    678:        (forth-set-word-properties state word-descr)
                    679:        (when state (put-text-property last-location (point) 'forth-state t))
                    680:        (let ((type (car word-descr)))
                    681:          (if (eq type 'definition-starter) (setq state t))
                    682:          (if (eq type 'definition-ender) (setq state nil))
                    683:          (setq last-location (point))))
                    684:       ;; update state property up to `to'
                    685:       (if (and state (< (point) to))
                    686:          (put-text-property last-location to 'forth-state t))
                    687:       ;; extend search if following state properties differ from current state
                    688:       (if (< to (point-max))
                    689:          (if (not (equal state (get-text-property (1+ to) 'forth-state)))
                    690:              (let ((extend-to (next-single-property-change 
                    691:                                to 'forth-state (current-buffer) (point-max))))
                    692:                (forth-update-properties to extend-to))
                    693:            ))
                    694:       )))
                    695: 
                    696: ;; save-buffer-state borrowed from `font-lock.el'
                    697: (eval-when-compile 
                    698:   (defmacro forth-save-buffer-state (varlist &rest body)
                    699:     "Bind variables according to VARLIST and eval BODY restoring buffer state."
                    700:     (` (let* ((,@ (append varlist
                    701:                   '((modified (buffer-modified-p)) (buffer-undo-list t)
                    702:                     (inhibit-read-only t) (inhibit-point-motion-hooks t)
                    703:                     before-change-functions after-change-functions
                    704:                     deactivate-mark buffer-file-name buffer-file-truename))))
                    705:         (,@ body)
                    706:         (when (and (not modified) (buffer-modified-p))
                    707:           (set-buffer-modified-p nil))))))
                    708: 
                    709: ;; Function that is added to the `change-functions' hook. Calls 
                    710: ;; `forth-update-properties' and keeps care of disabling undo information
                    711: ;; and stuff like that.
1.51      dvdkhlng  712: (defun forth-change-function (from to len &optional loudly)
1.48      pazsan    713:   (save-match-data
1.55      dvdkhlng  714:     (forth-save-buffer-state 
                    715:      () 
                    716:      (unless forth-disable-parser (forth-update-properties from to loudly))
                    717:      (forth-update-warn-long-lines))))
                    718: 
                    719: (defun forth-fontification-function (from)
                    720:   "Function to be called from `fontification-functions' of Emacs 21."
                    721:   (save-match-data
                    722:     (forth-save-buffer-state
                    723:      ((to (min (point-max) (+ from 100))))
                    724:      (unless (or forth-disable-parser (not forth-jit-parser)
                    725:                 (get-text-property from 'fontified))
                    726:        (forth-update-properties from to)))))
1.48      pazsan    727: 
                    728: (eval-when-compile
                    729:   (byte-compile 'forth-set-word-properties)
                    730:   (byte-compile 'forth-next-known-forth-word)
                    731:   (byte-compile 'forth-update-properties)
                    732:   (byte-compile 'forth-delete-properties)
                    733:   (byte-compile 'forth-get-regexp-branch)) 
                    734: 
1.51      dvdkhlng  735: ;;; imenu support
                    736: ;;;
1.52      dvdkhlng  737: (defvar forth-defining-words 
                    738:   '("VARIABLE" "CONSTANT" "2VARIABLE" "2CONSTANT" "FVARIABLE" "FCONSTANT"
                    739:    "USER" "VALUE" "field" "end-struct" "VOCABULARY" "CREATE" ":" "CODE"
                    740:    "DEFER" "ALIAS")
1.53      dvdkhlng  741:   "List of words, that define the following word.
1.55      dvdkhlng  742: Used for imenu index generation.")
1.52      dvdkhlng  743: 
1.57      dvdkhlng  744: (defvar forth-defining-words-regexp nil 
                    745:   "Regexp that's generated for matching `forth-defining-words'")
1.52      dvdkhlng  746:  
1.51      dvdkhlng  747: (defun forth-next-definition-starter ()
                    748:   (progn
1.52      dvdkhlng  749:     (let* ((pos (re-search-forward forth-defining-words-regexp (point-max) t)))
1.51      dvdkhlng  750:       (if pos
                    751:          (if (or (text-property-not-all (match-beginning 0) (match-end 0) 
1.52      dvdkhlng  752:                                         'forth-parsed nil)
                    753:                  (text-property-not-all (match-beginning 0) (match-end 0)
                    754:                                         'forth-state nil)) 
1.51      dvdkhlng  755:              (forth-next-definition-starter)
                    756:            t)
                    757:        nil))))
                    758: 
                    759: (defun forth-create-index ()
1.52      dvdkhlng  760:   (let* ((forth-defining-words-regexp 
                    761:          (concat "\\<\\(" (regexp-opt forth-defining-words) "\\)\\>"))
1.51      dvdkhlng  762:         (index nil))
                    763:     (goto-char (point-min))
                    764:     (while (forth-next-definition-starter)
                    765:       (if (looking-at "[ \t]*\\([^ \t\n]+\\)")
                    766:          (setq index (cons (cons (match-string 1) (point)) index))))
                    767:     index))
                    768: 
1.57      dvdkhlng  769: ;; top-level require is executed at byte-compile and load time
1.58      dvdkhlng  770: (eval-and-compile (forth-require 'speedbar))
1.57      dvdkhlng  771: 
                    772: ;; this code is executed at load-time only
1.58      dvdkhlng  773: (when (memq 'speedbar features)
1.57      dvdkhlng  774:   (speedbar-add-supported-extension ".fs")
                    775:   (speedbar-add-supported-extension ".fb"))
1.51      dvdkhlng  776: 
1.48      pazsan    777: ;; (require 'profile)
                    778: ;; (setq profile-functions-list '(forth-set-word-properties forth-next-known-forth-word forth-update-properties forth-delete-properties forth-get-regexp-branch))
                    779: 
                    780: ;;; Indentation
                    781: ;;;
                    782: 
                    783: ;; Return, whether `pos' is the first forth word on its line
                    784: (defun forth-first-word-on-line-p (pos)
                    785:   (save-excursion
                    786:     (beginning-of-line)
                    787:     (skip-chars-forward " \t")
                    788:     (= pos (point))))
                    789: 
                    790: ;; Return indentation data (SELF-INDENT . NEXT-INDENT) of next known 
                    791: ;; indentation word, or nil if there is no word up to `to'. 
                    792: ;; Position `point' at location just after found word, or at `to'. Parsed 
                    793: ;; ranges of text will not be taken into consideration!
                    794: (defun forth-next-known-indent-word (to)
                    795:   (if (<= (point) to)
                    796:       (progn
                    797:        (let* ((regexp (car forth-compiled-indent-words))
                    798:               (pos (re-search-forward regexp to t)))
                    799:          (if pos
1.54      dvdkhlng  800:              (let* ((start (match-beginning 0))
                    801:                     (end (match-end 0))
                    802:                     (branch (forth-get-regexp-branch))
                    803:                     (descr (cdr forth-compiled-indent-words))
                    804:                     (indent (cdr (assoc branch descr)))
                    805:                     (type (nth 2 indent)))
                    806:                ;; skip words that are parsed (strings/comments) and 
                    807:                ;; non-immediate words inside definitions
                    808:                (if (or (text-property-not-all start end 'forth-parsed nil)
                    809:                        (and (eq type 'non-immediate) 
                    810:                             (text-property-not-all start end 
                    811:                                                    'forth-state nil)))
                    812:                    (forth-next-known-indent-word to)
1.48      pazsan    813:                  (if (forth-first-word-on-line-p (match-beginning 0))
                    814:                      (nth 0 indent) (nth 1 indent))))
                    815:            nil)))
                    816:     nil))
                    817:   
                    818: ;; Translate indentation value `indent' to indentation column. Multiples of
                    819: ;; 2 correspond to multiples of `forth-indent-level'. Odd numbers get an
                    820: ;; additional `forth-minor-indent-level' added (or substracted).
                    821: (defun forth-convert-to-column (indent)
                    822:   (let* ((sign (if (< indent 0) -1 1))
                    823:         (value (abs indent))
                    824:         (major (* (/ value 2) forth-indent-level))
                    825:         (minor (* (% value 2) forth-minor-indent-level)))
                    826:     (* sign (+ major minor))))
                    827: 
                    828: ;; Return the column increment, that the current line of forth code does to
                    829: ;; the current or following lines. `which' specifies which indentation values
                    830: ;; to use. 0 means the indentation of following lines relative to current 
                    831: ;; line, 1 means the indentation of the current line relative to the previous 
                    832: ;; line. Return `nil', if there are no indentation words on the current line.
                    833: (defun forth-get-column-incr (which)
                    834:   (save-excursion
                    835:     (let ((regexp (car forth-compiled-indent-words))
                    836:          (word-indent)
                    837:          (self-indent nil)
                    838:          (next-indent nil)
                    839:          (to (save-excursion (end-of-line) (point))))
                    840:       (beginning-of-line)
                    841:       (while (setq word-indent (forth-next-known-indent-word to))
                    842:        (let* ((self-incr (car word-indent))
                    843:               (next-incr (cdr word-indent))
                    844:               (self-column-incr (forth-convert-to-column self-incr))
                    845:               (next-column-incr (forth-convert-to-column next-incr)))
                    846:          (setq next-indent (if next-indent next-indent 0))
                    847:          (setq self-indent (if self-indent self-indent 0))
                    848:          (if (or (and (> next-indent 0) (< self-column-incr 0))
                    849:                  (and (< next-indent 0) (> self-column-incr 0)))
                    850:              (setq next-indent (+ next-indent self-column-incr))
                    851:            (setq self-indent (+ self-indent self-column-incr)))
                    852:          (setq next-indent (+ next-indent next-column-incr))))
                    853:       (nth which (list self-indent next-indent)))))
                    854: 
                    855: ;; Find previous line that contains indentation words, return the column,
                    856: ;; to which following text should be indented to.
                    857: (defun forth-get-anchor-column ()
                    858:   (save-excursion
                    859:     (if (/= 0 (forward-line -1)) 0
1.54      dvdkhlng  860:       (let ((indent))
1.48      pazsan    861:        (while (not (or (setq indent (forth-get-column-incr 1))
                    862:                        (<= (point) (point-min))))
                    863:          (forward-line -1))
                    864:        (+ (current-indentation) (if indent indent 0))))))
                    865: 
                    866: (defun forth-indent-line (&optional flag)
                    867:   "Correct indentation of the current Forth line."
                    868:   (let* ((anchor (forth-get-anchor-column))
                    869:         (column-incr (forth-get-column-incr 0)))
                    870:     (forth-indent-to (if column-incr (+ anchor column-incr) anchor))))
                    871: 
1.49      dvdkhlng  872: (defun forth-current-column ()
                    873:   (- (point) (save-excursion (beginning-of-line) (point))))
                    874: (defun forth-current-indentation ()
                    875:   (- (save-excursion (beginning-of-line) (forward-to-indentation 0) (point))
                    876:      (save-excursion (beginning-of-line) (point))))
                    877: 
1.48      pazsan    878: (defun forth-indent-to (x)
                    879:   (let ((p nil))
1.49      dvdkhlng  880:     (setq p (- (forth-current-column) (forth-current-indentation)))
1.48      pazsan    881:     (forth-delete-indentation)
                    882:     (beginning-of-line)
                    883:     (indent-to x)
                    884:     (if (> p 0) (forward-char p))))
                    885: 
                    886: (defun forth-delete-indentation ()
                    887:   (save-excursion
                    888:     (delete-region 
                    889:      (progn (beginning-of-line) (point)) 
                    890:      (progn (back-to-indentation) (point)))))
                    891: 
                    892: (defun forth-indent-command ()
                    893:   (interactive)
                    894:   (forth-indent-line t))
                    895: 
                    896: ;; remove trailing whitespaces in current line
                    897: (defun forth-remove-trailing ()
                    898:   (save-excursion
                    899:     (end-of-line)
                    900:     (delete-region (point) (progn (skip-chars-backward " \t") (point)))))
                    901: 
                    902: ;; insert newline, removing any trailing whitespaces in the current line
                    903: (defun forth-newline-remove-trailing ()
                    904:   (save-excursion
1.49      dvdkhlng  905:     (delete-region (point) (progn (skip-chars-backward " \t") (point))))
                    906:   (newline))
                    907: ;  (let ((was-point (point-marker)))
                    908: ;    (unwind-protect 
                    909: ;      (progn (forward-line -1) (forth-remove-trailing))
                    910: ;      (goto-char (was-point)))))
1.48      pazsan    911: 
                    912: ;; workaround for bug in `reindent-then-newline-and-indent'
                    913: (defun forth-reindent-then-newline-and-indent ()
                    914:   (interactive "*")
                    915:   (indent-according-to-mode)
                    916:   (forth-newline-remove-trailing)
                    917:   (indent-according-to-mode))
                    918: 
                    919: 
                    920: ;;; Block file encoding/decoding  (dk)
                    921: ;;;
                    922: 
                    923: (defconst forth-c/l 64 "Number of characters per block line")
                    924: (defconst forth-l/b 16 "Number of lines per block")
                    925: 
                    926: ;; Check whether the unconverted block file line, point is in, does not
                    927: ;; contain `\n' and `\t' characters.
                    928: (defun forth-check-block-line (line)
                    929:   (let ((end (save-excursion (beginning-of-line) (forward-char forth-c/l)
                    930:                             (point))))
                    931:     (save-excursion 
                    932:       (beginning-of-line)
                    933:       (when (search-forward "\n" end t)
                    934:        (message "Warning: line %i contains newline character #10" line)
                    935:        (ding t))
                    936:       (beginning-of-line)
                    937:       (when (search-forward "\t" end t)
                    938:        (message "Warning: line %i contains tab character #8" line)
                    939:        (ding t)))))
                    940: 
                    941: (defun forth-convert-from-block (from to)
                    942:   "Convert block file format to stream source in current buffer."
                    943:   (let ((line (count-lines (point-min) from)))
                    944:     (save-excursion
                    945:       (goto-char from)
                    946:       (set-mark to)
                    947:       (while (< (+ (point) forth-c/l) (mark t))
                    948:        (setq line (1+ line))
                    949:        (forth-check-block-line line)
                    950:        (forward-char forth-c/l)
                    951:        (forth-newline-remove-trailing))
                    952:       (when (= (+ (point) forth-c/l) (mark t))
                    953:        (forth-remove-trailing))
                    954:       (mark t))))
                    955: 
                    956: ;; Pad a line of a block file up to `forth-c/l' characters, positioning `point'
                    957: ;; at the end of line.
                    958: (defun forth-pad-block-line ()
                    959:   (save-excursion
                    960:     (end-of-line)
                    961:     (if (<= (current-column) forth-c/l)
                    962:        (move-to-column forth-c/l t)
                    963:       (message "Line %i longer than %i characters, truncated"
                    964:               (count-lines (point-min) (point)) forth-c/l)
                    965:       (ding t)
                    966:       (move-to-column forth-c/l t)
                    967:       (delete-region (point) (progn (end-of-line) (point))))))
                    968: 
                    969: ;; Replace tab characters in current line by spaces.
                    970: (defun forth-convert-tabs-in-line ()
                    971:   (save-excursion
                    972:     (beginning-of-line)
                    973:     (while (search-forward "\t" (save-excursion (end-of-line) (point)) t)
                    974:       (backward-char)
                    975:       (delete-region (point) (1+ (point)))
                    976:       (insert-char ?\  (- tab-width (% (current-column) tab-width))))))
                    977: 
                    978: ;; Delete newline at end of current line, concatenating it with the following
                    979: ;; line. Place `point' at end of newly formed line.
                    980: (defun forth-delete-newline ()
                    981:   (end-of-line)
                    982:   (delete-region (point) (progn (beginning-of-line 2) (point))))
                    983: 
                    984: (defun forth-convert-to-block (from to &optional original-buffer) 
                    985:   "Convert range of text to block file format in current buffer."
                    986:   (let* ((lines 0)) ; I have to count lines myself, since `count-lines' has
                    987:                    ; problems with trailing newlines...
                    988:     (save-excursion
                    989:       (goto-char from)
                    990:       (set-mark to)
                    991:       ;; pad lines to full length (`forth-c/l' characters per line)
                    992:       (while (< (save-excursion (end-of-line) (point)) (mark t))
                    993:        (setq lines (1+ lines))
                    994:        (forth-pad-block-line)
                    995:        (forth-convert-tabs-in-line)
                    996:        (forward-line))
                    997:       ;; also make sure the last line is padded, if `to' is at its end
                    998:       (end-of-line)
                    999:       (when (= (point) (mark t))
                   1000:        (setq lines (1+ lines))
                   1001:        (forth-pad-block-line)
                   1002:        (forth-convert-tabs-in-line))
                   1003:       ;; remove newlines between lines
                   1004:       (goto-char from)
                   1005:       (while (< (save-excursion (end-of-line) (point)) (mark t))
                   1006:        (forth-delete-newline))
                   1007:       ;; append empty lines, until last block is complete
                   1008:       (goto-char (mark t))
                   1009:       (let* ((required (* (/ (+ lines (1- forth-l/b)) forth-l/b) forth-l/b))
                   1010:             (pad-lines (- required lines)))
                   1011:        (while (> pad-lines 0)
                   1012:          (insert-char ?\  forth-c/l)
                   1013:          (setq pad-lines (1- pad-lines))))
                   1014:       (point))))
                   1015: 
                   1016: (defun forth-detect-block-file-p ()
                   1017:   "Return non-nil if the current buffer is in block file format. Detection is
                   1018: done by checking whether the first line has 1024 characters or more."
                   1019:   (save-restriction 
                   1020:     (widen)
                   1021:     (save-excursion
1.54      dvdkhlng 1022:        (goto-char (point-min))
1.48      pazsan   1023:        (end-of-line)
                   1024:        (>= (current-column) 1024))))
                   1025: 
                   1026: ;; add block file conversion routines to `format-alist'
                   1027: (defconst forth-block-format-description
                   1028:   '(forth-blocks "Forth block source file" nil 
                   1029:                 forth-convert-from-block forth-convert-to-block 
                   1030:                 t normal-mode))
                   1031: (unless (memq forth-block-format-description format-alist)
                   1032:   (setq format-alist (cons forth-block-format-description format-alist)))
                   1033: 
                   1034: ;;; End block file encoding/decoding
                   1035: 
                   1036: ;;; Block file editing
                   1037: ;;;
                   1038: (defvar forth-overlay-arrow-string ">>")
                   1039: (defvar forth-block-base 1 "Number of first block in block file")
                   1040: (defvar forth-show-screen nil
                   1041:   "Non-nil means to show screen starts and numbers (for block files)")
                   1042: (defvar forth-warn-long-lines nil
                   1043:   "Non-nil means to warn about lines that are longer than 64 characters")
                   1044: 
                   1045: (defvar forth-screen-marker nil)
1.57      dvdkhlng 1046: (defvar forth-screen-number-string nil)
1.48      pazsan   1047: 
                   1048: (defun forth-update-show-screen ()
                   1049:   "If `forth-show-screen' is non-nil, put overlay arrow to start of screen, 
                   1050: `point' is in. If arrow now points to different screen than before, display 
                   1051: screen number."
                   1052:   (if (not forth-show-screen)
                   1053:       (setq overlay-arrow-string nil)
                   1054:     (save-excursion
                   1055:       (let* ((line (count-lines (point-min) (min (point-max) (1+ (point)))))
                   1056:             (first-line (1+ (* (/ (1- line) forth-l/b) forth-l/b)))
                   1057:             (scr (+ forth-block-base (/ first-line forth-l/b))))
                   1058:        (setq overlay-arrow-string forth-overlay-arrow-string)
                   1059:        (goto-line first-line)
                   1060:        (setq overlay-arrow-position forth-screen-marker)
1.50      dvdkhlng 1061:        (set-marker forth-screen-marker 
                   1062:                    (save-excursion (goto-line first-line) (point)))
                   1063:        (setq forth-screen-number-string (format "%d" scr))))))
1.48      pazsan   1064: 
                   1065: (add-hook 'forth-motion-hooks 'forth-update-show-screen)
                   1066: 
                   1067: (defun forth-update-warn-long-lines ()
                   1068:   "If `forth-warn-long-lines' is non-nil, display a warning whenever a line
                   1069: exceeds 64 characters."
                   1070:   (when forth-warn-long-lines
                   1071:     (when (> (save-excursion (end-of-line) (current-column)) forth-c/l)
                   1072:       (message "Warning: current line exceeds %i characters"
                   1073:               forth-c/l))))
                   1074: 
                   1075: (add-hook 'forth-motion-hooks 'forth-update-warn-long-lines)
1.55      dvdkhlng 1076: 
1.48      pazsan   1077: ;;; End block file editing
1.1       anton    1078: 
1.6       anton    1079: 
1.1       anton    1080: (defvar forth-mode-abbrev-table nil
                   1081:   "Abbrev table in use in Forth-mode buffers.")
                   1082: 
                   1083: (define-abbrev-table 'forth-mode-abbrev-table ())
                   1084: 
                   1085: (defvar forth-mode-map nil
                   1086:   "Keymap used in Forth mode.")
                   1087: 
                   1088: (if (not forth-mode-map)
                   1089:     (setq forth-mode-map (make-sparse-keymap)))
                   1090: 
1.9       anton    1091: ;(define-key forth-mode-map "\M-\C-x" 'compile)
1.7       anton    1092: (define-key forth-mode-map "\C-x\\" 'comment-region)
                   1093: (define-key forth-mode-map "\C-x~" 'forth-remove-tracers)
1.1       anton    1094: (define-key forth-mode-map "\C-x\C-m" 'forth-split)
                   1095: (define-key forth-mode-map "\e " 'forth-reload)
                   1096: (define-key forth-mode-map "\t" 'forth-indent-command)
1.48      pazsan   1097: (define-key forth-mode-map "\C-m" 'forth-reindent-then-newline-and-indent)
1.7       anton    1098: (define-key forth-mode-map "\M-q" 'forth-fill-paragraph)
1.13      pazsan   1099: (define-key forth-mode-map "\e." 'forth-find-tag)
1.1       anton    1100: 
1.61      dvdkhlng 1101: ;; setup for C-h C-i to work
1.58      dvdkhlng 1102: (eval-and-compile (forth-require 'info-look))
                   1103: (when (memq 'info-look features)
1.61      dvdkhlng 1104:   (defvar forth-info-lookup '(symbol (forth-mode "\\S-+" t 
1.59      dvdkhlng 1105:                                                  (("(gforth)Word Index"))
1.61      dvdkhlng 1106:                                                  "\\S-+")))
1.59      dvdkhlng 1107:   (unless (memq forth-info-lookup info-lookup-alist)
1.61      dvdkhlng 1108:     (setq info-lookup-alist (cons forth-info-lookup info-lookup-alist)))
                   1109:   ;; in X-Emacs C-h C-i is by default bound to Info-query
1.69    ! dvdkhlng 1110:   (define-key forth-mode-map [?\C-h ?\C-i] 'info-lookup-symbol))
1.59      dvdkhlng 1111: 
                   1112: ;;   (info-lookup-add-help
                   1113: ;;    :topic 'symbol
                   1114: ;;    :mode 'forth-mode
                   1115: ;;    :regexp "[^      
                   1116: ;; ]+"
                   1117: ;;    :ignore-case t
                   1118: ;;    :doc-spec '(("(gforth)Name Index" nil "`" "'  "))))
1.35      anton    1119: 
1.57      dvdkhlng 1120: (require 'etags)
1.13      pazsan   1121: 
                   1122: (defun forth-find-tag (tagname &optional next-p regexp-p)
                   1123:   (interactive (find-tag-interactive "Find tag: "))
1.53      dvdkhlng 1124:   (unless (or regexp-p next-p)
1.67      dvdkhlng 1125:     (setq tagname (concat "\\(^\\|\\s-+\\)\\(" (regexp-quote tagname) 
                   1126:                            "\\)\\s-*\x7f")))
1.13      pazsan   1127:   (switch-to-buffer
1.53      dvdkhlng 1128:    (find-tag-noselect tagname next-p t)))
1.13      pazsan   1129: 
1.1       anton    1130: (defvar forth-mode-syntax-table nil
                   1131:   "Syntax table in use in Forth-mode buffers.")
                   1132: 
1.48      pazsan   1133: ;; Important: hilighting/indentation now depends on a correct syntax table.
                   1134: ;; All characters, except whitespace *must* belong to the "word constituent"
                   1135: ;; syntax class. If different behaviour is required, use of Categories might
                   1136: ;; help.
                   1137: (if (not forth-mode-syntax-table)
1.1       anton    1138:     (progn
                   1139:       (setq forth-mode-syntax-table (make-syntax-table))
1.6       anton    1140:       (let ((char 0))
                   1141:        (while (< char ?!)
                   1142:          (modify-syntax-entry char " " forth-mode-syntax-table)
                   1143:          (setq char (1+ char)))
                   1144:        (while (< char 256)
                   1145:          (modify-syntax-entry char "w" forth-mode-syntax-table)
                   1146:          (setq char (1+ char))))
                   1147:       ))
1.1       anton    1148: 
                   1149: (defun forth-mode-variables ()
                   1150:   (set-syntax-table forth-mode-syntax-table)
                   1151:   (setq local-abbrev-table forth-mode-abbrev-table)
                   1152:   (make-local-variable 'paragraph-start)
                   1153:   (setq paragraph-start (concat "^$\\|" page-delimiter))
                   1154:   (make-local-variable 'paragraph-separate)
                   1155:   (setq paragraph-separate paragraph-start)
                   1156:   (make-local-variable 'indent-line-function)
                   1157:   (setq indent-line-function 'forth-indent-line)
1.6       anton    1158: ;  (make-local-variable 'require-final-newline)
                   1159: ;  (setq require-final-newline t)
1.1       anton    1160:   (make-local-variable 'comment-start)
1.3       anton    1161:   (setq comment-start "\\ ")
                   1162:   ;(make-local-variable 'comment-end)
                   1163:   ;(setq comment-end " )")
1.1       anton    1164:   (make-local-variable 'comment-column)
                   1165:   (setq comment-column 40)
                   1166:   (make-local-variable 'comment-start-skip)
1.65      dvdkhlng 1167:   (setq comment-start-skip "\\\\ ")
1.57      dvdkhlng 1168:   (make-local-variable 'comment-indent-function)
                   1169:   (setq comment-indent-function 'forth-comment-indent)
1.1       anton    1170:   (make-local-variable 'parse-sexp-ignore-comments)
1.47      pazsan   1171:   (setq parse-sexp-ignore-comments t)
1.48      pazsan   1172:   (setq case-fold-search t)
1.55      dvdkhlng 1173:   (make-local-variable 'forth-was-point)
                   1174:   (setq forth-was-point -1)
1.48      pazsan   1175:   (make-local-variable 'forth-words)
                   1176:   (make-local-variable 'forth-compiled-words)
                   1177:   (make-local-variable 'forth-compiled-indent-words)
                   1178:   (make-local-variable 'forth-hilight-level)
                   1179:   (make-local-variable 'after-change-functions)
                   1180:   (make-local-variable 'forth-show-screen)
                   1181:   (make-local-variable 'forth-screen-marker)
                   1182:   (make-local-variable 'forth-warn-long-lines)
1.50      dvdkhlng 1183:   (make-local-variable 'forth-screen-number-string)
1.51      dvdkhlng 1184:   (make-local-variable 'forth-use-oof)
                   1185:   (make-local-variable 'forth-use-objects) 
1.48      pazsan   1186:   (setq forth-screen-marker (copy-marker 0))
1.51      dvdkhlng 1187:   (add-hook 'after-change-functions 'forth-change-function)
1.62      dvdkhlng 1188:   (if (and forth-jit-parser (>= emacs-major-version 21))
1.55      dvdkhlng 1189:       (add-hook 'fontification-functions 'forth-fontification-function))
1.51      dvdkhlng 1190:   (setq imenu-create-index-function 'forth-create-index))
1.47      pazsan   1191: 
1.2       anton    1192: ;;;###autoload
1.1       anton    1193: (defun forth-mode ()
                   1194:   "
                   1195: Major mode for editing Forth code. Tab indents for Forth code. Comments
1.9       anton    1196: are delimited with \\ and newline. Paragraphs are separated by blank lines
1.49      dvdkhlng 1197: only. Block files are autodetected, when read, and converted to normal 
                   1198: stream source format. See also `forth-block-mode'.
1.1       anton    1199: \\{forth-mode-map}
                   1200: 
1.48      pazsan   1201: Variables controlling syntax hilighting/recognition of parsed text:
                   1202:  `forth-words'
                   1203:     List of words that have a special parsing behaviour and/or should be
1.51      dvdkhlng 1204:     hilighted. Add custom words by setting forth-custom-words in your
                   1205:     .emacs, or by setting forth-local-words, in source-files' local 
                   1206:     variables lists.
                   1207:  forth-use-objects
1.55      dvdkhlng 1208:     Set this variable to non-nil in your .emacs, or in a local variables 
1.51      dvdkhlng 1209:     list, to hilight and recognize the words from the \"Objects\" package 
                   1210:     for object-oriented programming.
                   1211:  forth-use-oof
                   1212:     Same as above, just for the \"OOF\" package.
                   1213:  forth-custom-words
                   1214:     List of custom Forth words to prepend to `forth-words'. Should be set
                   1215:     in your .emacs.
1.49      dvdkhlng 1216:  forth-local-words
                   1217:     List of words to prepend to `forth-words', whenever a forth-mode
                   1218:     buffer is created. That variable should be set by Forth sources, using
                   1219:     a local variables list at the end of file, to get file-specific
                   1220:     hilighting.
                   1221:     0 [IF]
                   1222:        Local Variables: ... 
                   1223:        forth-local-words: ...
                   1224:        End:
                   1225:     [THEN]
1.48      pazsan   1226:  forth-hilight-level
                   1227:     Controls how much syntax hilighting is done. Should be in the range 
1.51      dvdkhlng 1228:     0..3
1.48      pazsan   1229: 
1.1       anton    1230: Variables controlling indentation style:
1.48      pazsan   1231:  `forth-indent-words'
                   1232:     List of words that influence indentation.
1.51      dvdkhlng 1233:  forth-local-indent-words
1.49      dvdkhlng 1234:     List of words to prepend to `forth-indent-words', similar to 
1.51      dvdkhlng 1235:     forth-local-words. Should be used for specifying file-specific 
1.49      dvdkhlng 1236:     indentation, using a local variables list.
1.51      dvdkhlng 1237:  forth-custom-indent-words
                   1238:     List of words to prepend to `forth-indent-words'. Should be set in your
                   1239:     .emacs.    
1.1       anton    1240:  forth-indent-level
                   1241:     Indentation increment/decrement of Forth statements.
1.48      pazsan   1242:  forth-minor-indent-level
                   1243:     Minor indentation increment/decrement of Forth statemens.
1.1       anton    1244: 
1.48      pazsan   1245: Variables controlling block-file editing:
1.51      dvdkhlng 1246:  forth-show-screen
1.48      pazsan   1247:     Non-nil means, that the start of the current screen is marked by an
1.50      dvdkhlng 1248:     overlay arrow, and screen numbers are displayed in the mode line.
                   1249:     This variable is by default nil for `forth-mode' and t for 
                   1250:     `forth-block-mode'.
1.51      dvdkhlng 1251:  forth-overlay-arrow-string
1.48      pazsan   1252:     String to display as the overlay arrow, when `forth-show-screen' is t.
                   1253:     Setting this variable to nil disables the overlay arrow.
1.51      dvdkhlng 1254:  forth-block-base
1.48      pazsan   1255:     Screen number of the first block in a block file. Defaults to 1.
1.51      dvdkhlng 1256:  forth-warn-long-lines
1.48      pazsan   1257:     Non-nil means that a warning message is displayed whenever you edit or
                   1258:     move over a line that is longer than 64 characters (the maximum line
                   1259:     length that can be stored into a block file). This variable defaults to
                   1260:     t for `forth-block-mode' and to nil for `forth-mode'.
1.1       anton    1261: 
1.66      dvdkhlng 1262: Variables controlling interaction with the Forth-process (also see
                   1263: `run-forth'):
                   1264:   forth-program-name
                   1265:     Program invoked by the `run-forth' command (including arguments).
                   1266:   inferior-forth-mode-hook
                   1267:     Hook for customising inferior-forth-mode.
                   1268:   forth-compile-command
                   1269:     Default command to execute on `compile'.
                   1270: " 
1.1       anton    1271:   (interactive)
                   1272:   (kill-all-local-variables)
                   1273:   (use-local-map forth-mode-map)
                   1274:   (setq mode-name "Forth")
                   1275:   (setq major-mode 'forth-mode)
1.62      dvdkhlng 1276:   (forth-install-motion-hook)
1.48      pazsan   1277:   ;; convert buffer contents from block file format, if necessary
                   1278:   (when (forth-detect-block-file-p)
                   1279:     (widen)
                   1280:     (message "Converting from Forth block source...")
                   1281:     (forth-convert-from-block (point-min) (point-max))
                   1282:     (message "Converting from Forth block source...done"))
                   1283:   ;; if user switched from forth-block-mode to forth-mode, make sure the file
                   1284:   ;; is now stored as normal strem source
                   1285:   (when (equal buffer-file-format '(forth-blocks))
                   1286:     (setq buffer-file-format nil))
1.1       anton    1287:   (forth-mode-variables)
                   1288: ;  (if (not (forth-process-running-p))
                   1289: ;      (run-forth forth-program-name))
1.49      dvdkhlng 1290:   (run-hooks 'forth-mode-hook))
1.48      pazsan   1291: 
1.50      dvdkhlng 1292: ;;;###autoload
1.48      pazsan   1293: (define-derived-mode forth-block-mode forth-mode "Forth Block Source" 
                   1294:   "Major mode for editing Forth block source files, derived from 
                   1295: `forth-mode'. Differences to `forth-mode' are:
                   1296:  * files are converted to block format, when written (`buffer-file-format' 
                   1297:    is set to `(forth-blocks)')
                   1298:  * `forth-show-screen' and `forth-warn-long-lines' are t by default
                   1299:   
                   1300: Note that the length of lines in block files is limited to 64 characters.
                   1301: When writing longer lines to a block file, a warning is displayed in the
                   1302: echo area and the line is truncated. 
                   1303: 
                   1304: Another problem is imposed by block files that contain newline or tab 
                   1305: characters. When Emacs converts such files back to block file format, 
1.50      dvdkhlng 1306: it'll translate those characters to a number of spaces. However, when
1.48      pazsan   1307: you read such a file, a warning message is displayed in the echo area,
                   1308: including a line number that may help you to locate and fix the problem.
                   1309: 
                   1310: So have a look at the *Messages* buffer, whenever you hear (or see) Emacs' 
                   1311: bell during block file read/write operations."
                   1312:   (setq buffer-file-format '(forth-blocks))
                   1313:   (setq forth-show-screen t)
1.50      dvdkhlng 1314:   (setq forth-warn-long-lines t)
                   1315:   (setq forth-screen-number-string (format "%d" forth-block-base))
                   1316:   (setq mode-line-format (append (reverse (cdr (reverse mode-line-format)))
                   1317:                                 '("--S" forth-screen-number-string "-%-"))))
1.1       anton    1318: 
1.44      anton    1319: (add-hook 'forth-mode-hook
1.9       anton    1320:       '(lambda () 
                   1321:         (make-local-variable 'compile-command)
1.49      dvdkhlng 1322:         (setq compile-command "gforth ")
                   1323:         (forth-hack-local-variables)
1.51      dvdkhlng 1324:         (forth-customize-words)
1.49      dvdkhlng 1325:         (forth-compile-words)
1.55      dvdkhlng 1326:         (unless (and forth-jit-parser (>= emacs-major-version 21))
                   1327:           (forth-change-function (point-min) (point-max) nil t))))
1.6       anton    1328: 
1.7       anton    1329: (defun forth-fill-paragraph () 
                   1330:   "Fill comments (starting with '\'; do not fill code (block style
                   1331: programmers who tend to fill code won't use emacs anyway:-)."
1.9       anton    1332:   ; Currently only comments at the start of the line are filled.
                   1333:   ; Something like lisp-fill-paragraph may be better.  We cannot use
                   1334:   ; fill-paragraph, because it removes the \ from the first comment
                   1335:   ; line. Therefore we have to look for the first line of the comment
                   1336:   ; and use fill-region.
1.7       anton    1337:   (interactive)
                   1338:   (save-excursion
                   1339:     (beginning-of-line)
1.9       anton    1340:     (while (and
                   1341:             (= (forward-line -1) 0)
1.14      anton    1342:             (looking-at "[ \t]*\\\\g?[ \t]+")))
                   1343:     (if (not (looking-at "[ \t]*\\\\g?[ \t]+"))
1.9       anton    1344:        (forward-line 1))
                   1345:     (let ((from (point))
                   1346:          (to (save-excursion (forward-paragraph) (point))))
1.14      anton    1347:       (if (looking-at "[ \t]*\\\\g?[ \t]+")
1.9       anton    1348:          (progn (goto-char (match-end 0))
                   1349:                 (set-fill-prefix)
                   1350:                 (fill-region from to nil))))))
1.7       anton    1351: 
1.1       anton    1352: (defun forth-comment-indent ()
                   1353:   (save-excursion
                   1354:     (beginning-of-line)
                   1355:     (if (looking-at ":[ \t]*")
                   1356:        (progn
                   1357:          (end-of-line)
                   1358:          (skip-chars-backward " \t\n")
                   1359:          (1+ (current-column)))
                   1360:       comment-column)))
                   1361: 
                   1362: 
                   1363: ;; Forth commands
                   1364: 
1.7       anton    1365: (defun forth-remove-tracers ()
                   1366:   "Remove tracers of the form `~~ '. Queries the user for each occurrence."
                   1367:   (interactive)
1.16      anton    1368:   (query-replace-regexp "\\(~~ \\| ~~$\\)" "" nil))
1.7       anton    1369: 
1.66      dvdkhlng 1370: (define-key forth-mode-map "\C-x\C-e" 'compile)
                   1371: (define-key forth-mode-map "\C-x\C-n" 'next-error)
                   1372: (require 'compile)
1.1       anton    1373: 
1.66      dvdkhlng 1374: (defvar forth-compile-command "gforth ")
                   1375: ;(defvar forth-compilation-window-percent-height 30)
1.1       anton    1376: 
                   1377: (defun forth-split ()
                   1378:   (interactive)
                   1379:   (forth-split-1 "*forth*"))
                   1380: 
                   1381: (defun forth-split-1 (buffer)
                   1382:   (if (not (eq (window-buffer) (get-buffer buffer)))
                   1383:       (progn
                   1384:        (delete-other-windows)
                   1385:        (split-window-vertically
                   1386:         (/ (* (screen-height) forth-percent-height) 100))
                   1387:        (other-window 1)
                   1388:        (switch-to-buffer buffer)
                   1389:        (goto-char (point-max))
                   1390:        (other-window 1))))
                   1391: 
                   1392: (defun forth-compile (command)
                   1393:   (interactive (list (setq forth-compile-command (read-string "Compile command: " forth-compile-command))))
                   1394:   (forth-split-1 "*compilation*")
                   1395:   (setq ctools-compile-command command)
                   1396:   (compile1 ctools-compile-command "No more errors"))
                   1397: 
1.12      pazsan   1398: ;;; Forth menu
                   1399: ;;; Mikael Karlsson <qramika@eras70.ericsson.se>
                   1400: 
1.61      dvdkhlng 1401: ;; (dk) code commented out due to complaints of XEmacs users.  After
                   1402: ;; all, there's imenu/speedbar, which uses much smarter scanning
                   1403: ;; rules.
                   1404: 
                   1405: ;; (cond ((string-match "XEmacs\\|Lucid" emacs-version)
                   1406: ;;        (require 'func-menu)
                   1407: 
                   1408: ;;   (defconst fume-function-name-regexp-forth
                   1409: ;;    "^\\(:\\)[ \t]+\\([^ \t]*\\)"
                   1410: ;;    "Expression to get word definitions in Forth.")
                   1411: 
                   1412: ;;   (setq fume-function-name-regexp-alist
                   1413: ;;       (append '((forth-mode . fume-function-name-regexp-forth) 
                   1414: ;;              ) fume-function-name-regexp-alist))
                   1415: 
                   1416: ;;   ;; Find next forth word in the buffer
                   1417: ;;   (defun fume-find-next-forth-function-name (buffer)
                   1418: ;;     "Searches for the next forth word in BUFFER."
                   1419: ;;     (set-buffer buffer)
                   1420: ;;     (if (re-search-forward fume-function-name-regexp nil t)
                   1421: ;;       (let ((beg (match-beginning 2))
                   1422: ;;             (end (match-end 2)))
                   1423: ;;         (cons (buffer-substring beg end) beg))))
1.12      pazsan   1424: 
1.61      dvdkhlng 1425: ;;   (setq fume-find-function-name-method-alist
                   1426: ;;   (append '((forth-mode    . fume-find-next-forth-function-name))))
1.12      pazsan   1427: 
1.61      dvdkhlng 1428: ;;   ))
1.12      pazsan   1429: ;;; End Forth menu
                   1430: 
                   1431: ;;; File folding of forth-files
                   1432: ;;; uses outline
                   1433: ;;; Toggle activation with M-x fold-f (when editing a forth-file) 
                   1434: ;;; Use f9 to expand, f10 to hide, Or the menubar in xemacs
                   1435: ;;;
                   1436: ;;; Works most of the times but loses sync with the cursor occasionally 
                   1437: ;;; Could be improved by also folding on comments
                   1438: 
1.61      dvdkhlng 1439: ;; (dk) This code needs a rewrite; just too ugly and doesn't use the
                   1440: ;; newer and smarter scanning rules of `imenu'. Who needs it anyway??
1.12      pazsan   1441: 
1.61      dvdkhlng 1442: ;; (require 'outline)
                   1443: 
                   1444: ;; (defun f-outline-level ()
                   1445: ;;   (cond     ((looking-at "\\`\\\\")
                   1446: ;;      0)
                   1447: ;;     ((looking-at "\\\\ SEC")
                   1448: ;;      0)
                   1449: ;;     ((looking-at "\\\\ \\\\ .*")
                   1450: ;;      0)
                   1451: ;;     ((looking-at "\\\\ DEFS")
                   1452: ;;      1)
                   1453: ;;     ((looking-at "\\/\\* ")
                   1454: ;;      1)
                   1455: ;;     ((looking-at ": .*")
                   1456: ;;      1)
                   1457: ;;     ((looking-at "\\\\G")
                   1458: ;;      2)
                   1459: ;;     ((looking-at "[ \t]+\\\\")
                   1460: ;;      3)))
1.57      dvdkhlng 1461:   
1.61      dvdkhlng 1462: ;; (defun fold-f  ()
                   1463: ;;    (interactive)
                   1464: ;;    (add-hook 'outline-minor-mode-hook 'hide-body)
                   1465: 
                   1466: ;;    ; outline mode header start, i.e. find word definitions
                   1467: ;; ;;;   (setq  outline-regexp  "^\\(:\\)[ \t]+\\([^ \t]*\\)")
                   1468: ;;    (setq  outline-regexp  "\\`\\\\\\|:\\|\\\\ SEC\\|\\\\G\\|[ \t]+\\\\\\|\\\\ DEFS\\|\\/\\*\\|\\\\ \\\\ .*")
                   1469: ;;    (setq outline-level 'f-outline-level)
                   1470: 
                   1471: ;;    (outline-minor-mode)
                   1472: ;;    (define-key outline-minor-mode-map '(shift up) 'hide-sublevels)
                   1473: ;;    (define-key outline-minor-mode-map '(shift right) 'show-children)
                   1474: ;;    (define-key outline-minor-mode-map '(shift left) 'hide-subtree)
                   1475: ;;    (define-key outline-minor-mode-map '(shift down) 'show-subtree))
                   1476: 
1.29      pazsan   1477: 
                   1478: ;;(define-key global-map '(shift up) 'fold-f)
                   1479: 
1.12      pazsan   1480: ;;; end file folding
                   1481: 
                   1482: ;;; func-menu is a package that scans your source file for function definitions
                   1483: ;;; and makes a menubar entry that lets you jump to any particular function
                   1484: ;;; definition by selecting it from the menu.  The following code turns this on
                   1485: ;;; for all of the recognized languages.  Scanning the buffer takes some time,
                   1486: ;;; but not much.
                   1487: ;;;
1.61      dvdkhlng 1488: ;; (cond ((string-match "XEmacs\\|Lucid" emacs-version)
                   1489: ;;        (require 'func-menu)
                   1490: ;; ;;       (define-key global-map 'f8 'function-menu)
                   1491: ;;        (add-hook 'find-fible-hooks 'fume-add-menubar-entry)
                   1492: ;; ;       (define-key global-map "\C-cg" 'fume-prompt-function-goto)
                   1493: ;; ;       (define-key global-map '(shift button3) 'mouse-function-menu)
                   1494: ;; ))
1.66      dvdkhlng 1495: 
                   1496: ;;;
                   1497: ;;; Inferior Forth interpreter 
                   1498: ;;;    -- mostly copied from `cmuscheme.el' of Emacs 21.2
                   1499: ;;;
                   1500: 
                   1501: (eval-and-compile (forth-require 'comint))
                   1502: 
                   1503: (when (memq 'comint features)
                   1504: 
                   1505:   (defvar forth-program-name "gforth"
                   1506:     "*Program invoked by the `run-forth' command, including program arguments")
                   1507: 
                   1508:   (defcustom inferior-forth-mode-hook nil
                   1509:     "*Hook for customising inferior-forth-mode."
                   1510:     :type 'hook
                   1511:     :group 'forth)
                   1512: 
                   1513:   (defvar inferior-forth-mode-map
                   1514:     (let ((m (make-sparse-keymap)))
                   1515:       (define-key m "\r" 'comint-send-input)
                   1516:       (define-key m "\M-\C-x" 'forth-send-paragraph-and-go)
                   1517:       (define-key m "\C-c\C-l" 'forth-load-file)
                   1518:       m))
                   1519:   ;; Install the process communication commands in the forth-mode keymap.
                   1520:   (define-key forth-mode-map "\e\C-m" 'forth-send-paragraph-and-go)
                   1521:   (define-key forth-mode-map "\eo" 'forth-send-buffer-and-go)
                   1522: 
                   1523:   (define-key forth-mode-map "\M-\C-x" 'forth-send-paragraph-and-go)
                   1524:   (define-key forth-mode-map "\C-c\C-r" 'forth-send-region)
                   1525:   (define-key forth-mode-map "\C-c\M-r" 'forth-send-region-and-go)
                   1526:   (define-key forth-mode-map "\C-c\C-z" 'forth-switch-to-interactive)
                   1527:   (define-key forth-mode-map "\C-c\C-l" 'forth-load-file)
                   1528: 
                   1529:   (defvar forth-process-buffer)
                   1530: 
                   1531:   (define-derived-mode inferior-forth-mode comint-mode "Inferior Forth"
                   1532:     "Major mode for interacting with an inferior Forth process.
                   1533: 
                   1534: The following commands are available:
                   1535: \\{inferior-forth-mode-map}
                   1536: 
                   1537: A Forth process can be fired up with M-x run-forth.
                   1538: 
                   1539: Customisation: Entry to this mode runs the hooks on comint-mode-hook and
                   1540: inferior-forth-mode-hook (in that order).
                   1541: 
                   1542: You can send text to the inferior Forth process from other buffers containing
                   1543: Forth source.
                   1544:     forth-switch-to-interactive switches the current buffer to the Forth
                   1545:         process buffer. 
                   1546:     forth-send-paragraph sends the current paragraph to the Forth process.
                   1547:     forth-send-region sends the current region to the Forth process.
                   1548:     forth-send-buffer sends the current buffer to the Forth process.
                   1549: 
                   1550:     forth-send-paragraph-and-go, forth-send-region-and-go,
                   1551:         forth-send-buffer-and-go switch to the Forth process buffer after
                   1552:         sending their text.
                   1553: For information on running multiple processes in multiple buffers, see
                   1554: documentation for variable `forth-process-buffer'.
                   1555: 
                   1556: Commands:
                   1557: Return after the end of the process' output sends the text from the
                   1558: end of process to point. If you accidentally suspend your process, use
                   1559: \\[comint-continue-subjob] to continue it. "
                   1560:     ;; Customise in inferior-forth-mode-hook
                   1561:     (setq comint-prompt-regexp "^") 
                   1562:     (setq mode-line-process '(":%s")))
                   1563: 
                   1564:   (defun forth-args-to-list (string)
                   1565:     (let ((where (string-match "[ \t]" string)))
                   1566:       (cond ((null where) (list string))
                   1567:            ((not (= where 0))
                   1568:             (cons (substring string 0 where)
                   1569:                   (forth-args-to-list (substring string (+ 1 where)
                   1570:                                                  (length string)))))
                   1571:            (t (let ((pos (string-match "[^ \t]" string)))
                   1572:                 (if (null pos)
                   1573:                     nil
                   1574:                   (forth-args-to-list (substring string pos
                   1575:                                                  (length string)))))))))
                   1576: 
                   1577: ;;;###autoload
                   1578:   (defun run-forth (cmd)
                   1579:     "Run an inferior Forth process, input and output via buffer *forth*.
                   1580: If there is a process already running in `*forth*', switch to that buffer.
                   1581: With argument, allows you to edit the command line (default is value
                   1582: of `forth-program-name').  Runs the hooks `inferior-forth-mode-hook'
                   1583: \(after the `comint-mode-hook' is run).
                   1584: \(Type \\[describe-mode] in the process buffer for a list of commands.)"
                   1585: 
                   1586:     (interactive (list (if current-prefix-arg
                   1587:                           (read-string "Run Forth: " forth-program-name)
                   1588:                         forth-program-name)))
                   1589:     (if (not (comint-check-proc "*forth*"))
                   1590:        (let ((cmdlist (forth-args-to-list cmd)))
                   1591:          (set-buffer (apply 'make-comint "forth" (car cmdlist)
                   1592:                             nil (cdr cmdlist)))
                   1593:          (inferior-forth-mode)))
                   1594:     (setq forth-program-name cmd)
                   1595:     (setq forth-process-buffer "*forth*")
                   1596:     (pop-to-buffer "*forth*"))
                   1597: 
                   1598:   (defun forth-send-region (start end)
                   1599:     "Send the current region to the inferior Forth process."
                   1600:     (interactive "r")
                   1601:     (comint-send-region (forth-proc) start end)
                   1602:     (comint-send-string (forth-proc) "\n"))
                   1603: 
                   1604:   (defun forth-end-of-paragraph ()
                   1605:     (if (looking-at "[\t\n ]+") (skip-chars-backward  "\t\n "))
                   1606:     (if (not (re-search-forward "\n[ \t]*\n" nil t))
                   1607:        (goto-char (point-max))))
                   1608: 
                   1609:   (defun forth-send-paragraph ()
                   1610:     "Send the current or the previous paragraph to the Forth process"
                   1611:     (interactive)
                   1612:     (let (end)
                   1613:       (save-excursion
                   1614:        (forth-end-of-paragraph)
                   1615:        (skip-chars-backward  "\t\n ")
                   1616:        (setq end (point))
                   1617:        (if (re-search-backward "\n[ \t]*\n" nil t)
                   1618:            (setq start (point))
                   1619:          (goto-char (point-min)))
                   1620:        (skip-chars-forward  "\t\n ")
                   1621:        (forth-send-region (point) end))))
                   1622: 
                   1623:   (defun forth-send-paragraph-and-go ()
                   1624:     "Send the current or the previous paragraph to the Forth process.
                   1625: Then switch to the process buffer."
                   1626:     (interactive)
                   1627:     (forth-send-paragraph)
                   1628:     (forth-switch-to-interactive t))
                   1629: 
                   1630:   (defun forth-send-buffer ()
                   1631:     "Send the current buffer to the Forth process."
                   1632:     (interactive)
                   1633:     (if (eq (current-buffer) forth-process-buffer)
                   1634:        (error "Not allowed to send this buffer's contents to Forth"))
                   1635:     (forth-send-region (point-min) (point-max)))
                   1636: 
                   1637:   (defun forth-send-buffer-and-go ()
                   1638:     "Send the current buffer to the Forth process.
                   1639: Then switch to the process buffer."
                   1640:     (interactive)
                   1641:     (forth-send-buffer)
                   1642:     (forth-switch-to-interactive t))
                   1643: 
                   1644: 
                   1645:   (defun forth-switch-to-interactive (eob-p)
                   1646:     "Switch to the Forth process buffer.
                   1647: With argument, position cursor at end of buffer."
                   1648:     (interactive "P")
                   1649:     (if (get-buffer forth-process-buffer)
                   1650:        (pop-to-buffer forth-process-buffer)
                   1651:       (error "No current process buffer.  See variable `forth-process-buffer'"))
                   1652:     (cond (eob-p
                   1653:           (push-mark)
                   1654:           (goto-char (point-max)))))
                   1655: 
                   1656:   (defun forth-send-region-and-go (start end)
                   1657:     "Send the current region to the inferior Forth process.
                   1658: Then switch to the process buffer."
                   1659:     (interactive "r")
                   1660:     (forth-send-region start end)
                   1661:     (forth-switch-to-interactive t))
                   1662: 
                   1663:   (defcustom forth-source-modes '(forth-mode forth-block-mode)
                   1664:     "*Used to determine if a buffer contains Forth source code.
                   1665: If it's loaded into a buffer that is in one of these major modes, it's
                   1666: considered a Forth source file by `forth-load-file' and `forth-compile-file'.
                   1667: Used by these commands to determine defaults."
                   1668:     :type '(repeat function)
                   1669:     :group 'forth)
                   1670: 
                   1671:   (defvar forth-prev-l/c-dir/file nil
                   1672:     "Caches the last (directory . file) pair.
                   1673: Caches the last pair used in the last `forth-load-file' or
                   1674: `forth-compile-file' command. Used for determining the default in the
                   1675: next one.")
                   1676: 
                   1677:   (defun forth-load-file (file-name)
                   1678:     "Load a Forth file FILE-NAME into the inferior Forth process."
                   1679:     (interactive (comint-get-source "Load Forth file: " forth-prev-l/c-dir/file
                   1680:                                    forth-source-modes t)) ; T because LOAD
                   1681:                                        ; needs an exact name
                   1682:     (comint-check-source file-name) ; Check to see if buffer needs saved.
                   1683:     (setq forth-prev-l/c-dir/file (cons (file-name-directory    file-name)
                   1684:                                        (file-name-nondirectory file-name)))
                   1685:     (comint-send-string (forth-proc) (concat "(load \""
                   1686:                                             file-name
                   1687:                                             "\"\)\n")))
                   1688: 
                   1689: 
                   1690:   
                   1691:   (defvar forth-process-buffer nil "*The current Forth process buffer.
                   1692: 
                   1693: See `scheme-buffer' for an explanation on how to run multiple Forth 
                   1694: processes.")
                   1695: 
                   1696:   (defun forth-proc ()
                   1697:     "Return the current Forth process.  See variable `forth-process-buffer'."
                   1698:     (let ((proc (get-buffer-process (if (eq major-mode 'inferior-forth-mode)
                   1699:                                        (current-buffer)
                   1700:                                      forth-process-buffer))))
                   1701:       (or proc
                   1702:          (error "No current process.  See variable `forth-process-buffer'"))))
                   1703:   )  ; (memq 'comint features)
1.57      dvdkhlng 1704: 
                   1705: (provide 'forth-mode)
1.29      pazsan   1706: 
1.48      pazsan   1707: ;;; gforth.el ends here

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