Annotation of gforth/gforth.el, revision 1.49

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

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