Annotation of gforth/gforth.el, revision 1.6

1.1       anton       1: ;; This file is part of GNU Emacs.
                      2: ;; Changes by anton
1.6     ! anton       3: ;; This is a variant of forth.el that came with TILE.
1.5       anton       4: ;; I left most of this stuff untouched and made just a few changes for 
1.6     ! anton       5: ;; the things I use (mainly indentation and syntax tables).
        !             6: ;; So there is still a lot of work to do to adapt this to gforth.
1.1       anton       7: 
                      8: ;; GNU Emacs is distributed in the hope that it will be useful,
                      9: ;; but WITHOUT ANY WARRANTY.  No author or distributor
                     10: ;; accepts responsibility to anyone for the consequences of using it
                     11: ;; or for whether it serves any particular purpose or works at all,
                     12: ;; unless he says so in writing.  Refer to the GNU Emacs General Public
                     13: ;; License for full details.
                     14: 
                     15: ;; Everyone is granted permission to copy, modify and redistribute
                     16: ;; GNU Emacs, but only under the conditions described in the
                     17: ;; GNU Emacs General Public License.   A copy of this license is
                     18: ;; supposed to have been given to you along with GNU Emacs so you
                     19: ;; can know your rights and responsibilities.  It should be in a
                     20: ;; file named COPYING.  Among other things, the copyright notice
                     21: ;; and this notice must be preserved on all copies.
                     22: 
1.6     ! anton      23: ;;; $Header: /usr/local/lib/cvs-repository/src-master/gforth/gforth.el,v 1.5 1994/07/29 11:16:21 anton Exp $
1.1       anton      24: 
                     25: ;;-------------------------------------------------------------------
                     26: ;; A Forth indentation, documentation search and interaction library
                     27: ;;-------------------------------------------------------------------
                     28: ;;
                     29: ;; Written by Goran Rydqvist, gorry@ida.liu.se, Summer 1988
                     30: ;; Started:    16 July 88
                     31: ;; Version:    2.10
                     32: ;; Last update:        5 December 1989 by Mikael Patel, mip@ida.liu.se
                     33: ;; Last update:        25 June 1990 by Goran Rydqvist, gorry@ida.liu.se
                     34: ;;
                     35: ;; Documentation: See forth-mode (^HF forth-mode)
                     36: ;;-------------------------------------------------------------------
                     37: 
                     38: 
                     39: (defvar forth-positives
1.6     ! anton      40:   " : :noname begin do ?do while if ?dup-if ?dup-not-if else case create does> exception> struct [if] [else] "
1.1       anton      41:   "Contains all words which will cause the indent-level to be incremented
                     42: on the next line.
                     43: OBS! All words in forth-positives must be surrounded by spaces.")
                     44: 
                     45: (defvar forth-negatives
1.6     ! anton      46:   " ; until repeat while +loop loop s+loop else then endif again endcase does> end-struct [then] [else] "
1.1       anton      47:   "Contains all words which will cause the indent-level to be decremented
                     48: on the current line.
                     49: OBS! All words in forth-negatives must be surrounded by spaces.")
                     50: 
                     51: (defvar forth-zeroes
1.5       anton      52:   " : :noname "
1.1       anton      53:   "Contains all words which causes the indent to go to zero")
                     54: 
1.6     ! anton      55: (defvar forth-prefixes
        !            56:   " postpone [compile] ['] [char] "
        !            57:   "words that prefix and escape other words")
        !            58: 
1.1       anton      59: (defvar forth-mode-abbrev-table nil
                     60:   "Abbrev table in use in Forth-mode buffers.")
                     61: 
                     62: (define-abbrev-table 'forth-mode-abbrev-table ())
                     63: 
                     64: (defvar forth-mode-map nil
                     65:   "Keymap used in Forth mode.")
                     66: 
                     67: (if (not forth-mode-map)
                     68:     (setq forth-mode-map (make-sparse-keymap)))
                     69: 
                     70: (global-set-key "\e\C-m" 'forth-send-paragraph)
                     71: (global-set-key "\C-x\C-m" 'forth-split)
                     72: (global-set-key "\e " 'forth-reload)
                     73: 
1.6     ! anton      74: (define-key forth-mode-map "\M-\C-x" 'compile)
1.1       anton      75: (define-key forth-mode-map "\e\C-m" 'forth-send-paragraph)
                     76: (define-key forth-mode-map "\eo" 'forth-send-buffer)
                     77: (define-key forth-mode-map "\C-x\C-m" 'forth-split)
                     78: (define-key forth-mode-map "\e " 'forth-reload)
                     79: (define-key forth-mode-map "\t" 'forth-indent-command)
                     80: (define-key forth-mode-map "\C-m" 'reindent-then-newline-and-indent)
                     81: 
                     82: (defvar forth-mode-syntax-table nil
                     83:   "Syntax table in use in Forth-mode buffers.")
                     84: 
                     85: (if (not forth-mode-syntax-table)
                     86:     (progn
                     87:       (setq forth-mode-syntax-table (make-syntax-table))
1.6     ! anton      88:       (let ((char 0))
        !            89:        (while (< char ?!)
        !            90:          (modify-syntax-entry char " " forth-mode-syntax-table)
        !            91:          (setq char (1+ char)))
        !            92:        (while (< char 256)
        !            93:          (modify-syntax-entry char "w" forth-mode-syntax-table)
        !            94:          (setq char (1+ char))))
        !            95:       (modify-syntax-entry ?\" "\"" forth-mode-syntax-table)
        !            96:       (modify-syntax-entry ?\\ "<" forth-mode-syntax-table)
        !            97:       (modify-syntax-entry ?\n ">" forth-mode-syntax-table)
        !            98:       ))
        !            99: ;I do not define '(' and ')' as comment delimiters, because emacs
        !           100: ;only supports one comment syntax (and a hack to accomodate C++); I
        !           101: ;use '\' for natural language comments and '(' for formal comments
        !           102: ;like stack comments, so for me it's better to have emacs treat '\'
        !           103: ;comments as comments. I you want it different, make the appropriate
        !           104: ;changes (best in your .emacs file).
        !           105: ;
        !           106: ;Hmm, the C++ hack could be used to support both comment syntaxes: we
        !           107: ;can have different comment styles, if both comments start with the
        !           108: ;same character. we could use ' ' as first and '(' and '\' as second
        !           109: ;character. However this would fail for G\ comments.
1.1       anton     110: 
                    111: (defconst forth-indent-level 4
                    112:   "Indentation of Forth statements.")
                    113: 
                    114: (defun forth-mode-variables ()
                    115:   (set-syntax-table forth-mode-syntax-table)
                    116:   (setq local-abbrev-table forth-mode-abbrev-table)
                    117:   (make-local-variable 'paragraph-start)
                    118:   (setq paragraph-start (concat "^$\\|" page-delimiter))
                    119:   (make-local-variable 'paragraph-separate)
                    120:   (setq paragraph-separate paragraph-start)
                    121:   (make-local-variable 'indent-line-function)
                    122:   (setq indent-line-function 'forth-indent-line)
1.6     ! anton     123: ;  (make-local-variable 'require-final-newline)
        !           124: ;  (setq require-final-newline t)
1.1       anton     125:   (make-local-variable 'comment-start)
1.3       anton     126:   (setq comment-start "\\ ")
                    127:   ;(make-local-variable 'comment-end)
                    128:   ;(setq comment-end " )")
1.1       anton     129:   (make-local-variable 'comment-column)
                    130:   (setq comment-column 40)
                    131:   (make-local-variable 'comment-start-skip)
1.3       anton     132:   (setq comment-start-skip "\\ ")
1.1       anton     133:   (make-local-variable 'comment-indent-hook)
                    134:   (setq comment-indent-hook 'forth-comment-indent)
                    135:   (make-local-variable 'parse-sexp-ignore-comments)
                    136:   (setq parse-sexp-ignore-comments t))
                    137:   
1.2       anton     138: ;;;###autoload
1.1       anton     139: (defun forth-mode ()
                    140:   "
                    141: Major mode for editing Forth code. Tab indents for Forth code. Comments
                    142: are delimited with ( ). Paragraphs are separated by blank lines only.
                    143: Delete converts tabs to spaces as it moves back.
                    144: \\{forth-mode-map}
                    145:  Forth-split
                    146:     Positions the current buffer on top and a forth-interaction window
                    147:     below. The window size is controlled by the forth-percent-height
                    148:     variable (see below).
                    149:  Forth-reload
                    150:     Reloads the forth library and restarts the forth process.
                    151:  Forth-send-buffer
                    152:     Sends the current buffer, in text representation, as input to the
                    153:     forth process.
                    154:  Forth-send-paragraph
                    155:     Sends the previous or the current paragraph to the forth-process.
                    156:     Note that the cursor only need to be with in the paragraph to be sent.
                    157: forth-documentation
                    158:     Search for documentation of forward adjacent to cursor. Note! To use
                    159:     this mode you have to add a line, to your .emacs file, defining the
                    160:     directories to search through for documentation files (se variable
                    161:     forth-help-load-path below) e.g. (setq forth-help-load-path '(nil)).
                    162: 
                    163: Variables controlling interaction and startup
                    164:  forth-percent-height
                    165:     Tells split how high to make the edit portion, in percent of the
                    166:     current screen height.
                    167:  forth-program-name
                    168:     Tells the library which program name to execute in the interation
                    169:     window.
                    170: 
                    171: Variables controlling indentation style:
                    172:  forth-positives
                    173:     A string containing all words which causes the indent-level of the
                    174:     following line to be incremented.
                    175:     OBS! Each word must be surronded by spaces.
                    176:  forth-negatives
                    177:     A string containing all words which causes the indentation of the
                    178:     current line to be decremented, if the word begin the line. These
                    179:     words also has a cancelling effect on the indent-level of the
                    180:     following line, independent of position.
                    181:     OBS! Each word must be surronded by spaces.
                    182:  forth-zeroes
                    183:     A string containing all words which causes the indentation of the
                    184:     current line to go to zero, if the word begin the line.
                    185:     OBS! Each word must be surronded by spaces.
                    186:  forth-indent-level
                    187:     Indentation increment/decrement of Forth statements.
                    188: 
                    189:  Note! A word which decrements the indentation of the current line, may
                    190:     also be mentioned in forth-positives to cause the indentation to
                    191:     resume the previous level.
                    192: 
                    193: Variables controling documentation search
                    194:  forth-help-load-path
                    195:     List of directories to search through to find *.doc
                    196:     (forth-help-file-suffix) files. Nil means current default directory.
                    197:     The specified directories must contain at least one .doc file. If it
                    198:     does not and you still want the load-path to scan that directory, create
                    199:     an empty file dummy.doc.
                    200:  forth-help-file-suffix
                    201:     The file names to search for in each directory specified by
                    202:     forth-help-load-path. Defaulted to '*.doc'. 
                    203: "
                    204:   (interactive)
                    205:   (kill-all-local-variables)
                    206:   (use-local-map forth-mode-map)
                    207:   (setq mode-name "Forth")
                    208:   (setq major-mode 'forth-mode)
                    209:   (forth-mode-variables)
                    210: ;  (if (not (forth-process-running-p))
                    211: ;      (run-forth forth-program-name))
                    212:   (run-hooks 'forth-mode-hook))
                    213: 
1.6     ! anton     214: (setq forth-mode-hook
        !           215:       '(lambda () (setq compile-command "gforth ")))
        !           216: 
1.1       anton     217: (defun forth-comment-indent ()
                    218:   (save-excursion
                    219:     (beginning-of-line)
                    220:     (if (looking-at ":[ \t]*")
                    221:        (progn
                    222:          (end-of-line)
                    223:          (skip-chars-backward " \t\n")
                    224:          (1+ (current-column)))
                    225:       comment-column)))
                    226: 
                    227: (defun forth-current-indentation ()
                    228:   (save-excursion
                    229:     (beginning-of-line)
                    230:     (back-to-indentation)
                    231:     (current-column)))
                    232: 
                    233: (defun forth-delete-indentation ()
                    234:   (let ((b nil) (m nil))
                    235:     (save-excursion
                    236:       (beginning-of-line)
                    237:       (setq b (point))
                    238:       (back-to-indentation)
                    239:       (setq m (point)))
                    240:     (delete-region b m)))
                    241: 
                    242: (defun forth-indent-line (&optional flag)
                    243:   "Correct indentation of the current Forth line."
                    244:   (let ((x (forth-calculate-indent)))
                    245:     (forth-indent-to x)))
                    246:   
                    247: (defun forth-indent-command ()
                    248:   (interactive)
                    249:   (forth-indent-line t))
                    250: 
                    251: (defun forth-indent-to (x)
                    252:   (let ((p nil))
                    253:     (setq p (- (current-column) (forth-current-indentation)))
                    254:     (forth-delete-indentation)
                    255:     (beginning-of-line)
                    256:     (indent-to x)
                    257:     (if (> p 0) (forward-char p))))
                    258: 
                    259: ;;Calculate indent
                    260: (defun forth-calculate-indent ()
                    261:   (let ((w1 nil) (indent 0) (centre 0))
                    262:     (save-excursion
                    263:       (beginning-of-line)
                    264:       (skip-chars-backward " \t\n")
                    265:       (beginning-of-line)
                    266:       (back-to-indentation)
                    267:       (setq indent (current-column))
                    268:       (setq centre indent)
                    269:       (setq indent (+ indent (forth-sum-line-indentation))))
                    270:     (save-excursion
                    271:       (beginning-of-line)
                    272:       (back-to-indentation)
                    273:       (let ((p (point)))
                    274:        (skip-chars-forward "^ \t\n")
                    275:        (setq w1 (buffer-substring p (point)))))
                    276:     (if (> (- indent centre) forth-indent-level)
                    277:        (setq indent (+ centre forth-indent-level)))
                    278:     (if (> (- centre indent) forth-indent-level)
                    279:        (setq indent (- centre forth-indent-level)))
                    280:     (if (< indent 0) (setq indent 0))
                    281:     (setq indent (- indent
                    282:                    (if (string-match 
                    283:                         (regexp-quote (concat " " w1 " "))
                    284:                         forth-negatives)
                    285:                        forth-indent-level 0)))
                    286:     (if (string-match (regexp-quote (concat " " w1 " ")) forth-zeroes)
                    287:        (setq indent 0))
                    288:     indent))
                    289: 
                    290: (defun forth-sum-line-indentation ()
                    291:   "Add upp the positive and negative weights of all words on the current line."
                    292:   (let ((b (point)) (e nil) (sum 0) (w nil) (t1 nil) (t2 nil) (first t))
                    293:     (end-of-line) (setq e (point))
                    294:     (goto-char b)
                    295:     (while (< (point) e)
                    296:       (setq w (forth-next-word))
                    297:       (setq t1 (string-match (regexp-quote (concat " " w " "))
                    298:                             forth-positives))
                    299:       (setq t2 (string-match (regexp-quote (concat " " w " "))
                    300:                             forth-negatives))
                    301:       (if t1
                    302:          (setq sum (+ sum forth-indent-level)))
                    303:       (if (and t2 (not first))
                    304:          (setq sum (- sum forth-indent-level)))
                    305:       (skip-chars-forward " \t")
                    306:       (setq first nil))
                    307:     sum))
                    308: 
                    309: 
                    310: (defun forth-next-word ()
1.6     ! anton     311:   "Return the next forth-word. Skip anything that the forth-word takes from
        !           312: the input stream (comments, arguments, etc.)"
        !           313: ;actually, it would be better to use commands based on the
        !           314: ;syntax-table or comment-start etc.
1.1       anton     315:   (let ((w1 nil))
                    316:     (while (not w1)
                    317:       (skip-chars-forward " \t\n")
                    318:       (let ((p (point)))
                    319:        (skip-chars-forward "^ \t\n")
                    320:        (setq w1 (buffer-substring p (point))))
                    321:       (cond ((string-match "\"" w1)
                    322:             (progn
1.6     ! anton     323:               (skip-chars-forward "^\"\n")
        !           324:               (forward-char)))
        !           325:            ((string-match "\\\\" w1)
        !           326:             (progn
        !           327:               (end-of-line)
        !           328:               ))
        !           329:            ((or (equal "(" w1) (equal ".(" w1))
1.1       anton     330:             (progn
1.6     ! anton     331:               (skip-chars-forward "^)\n")
        !           332:               (forward-char)))
        !           333:            ((string-match (regexp-quote (concat " " w1 " ")) forth-prefixes)
        !           334:             (progn (skip-chars-forward " \t\n")
        !           335:                    (skip-chars-forward "^ \t\n")))
1.1       anton     336:            (t nil)))
                    337:     w1))
                    338:       
                    339: 
                    340: ;; Forth commands
                    341: 
1.5       anton     342: (defvar forth-program-name "gforth"
1.1       anton     343:   "*Program invoked by the `run-forth' command.")
                    344: 
                    345: (defvar forth-band-name nil
                    346:   "*Band loaded by the `run-forth' command.")
                    347: 
                    348: (defvar forth-program-arguments nil
                    349:   "*Arguments passed to the Forth program by the `run-forth' command.")
                    350: 
                    351: (defun run-forth (command-line)
                    352:   "Run an inferior Forth process. Output goes to the buffer `*forth*'.
                    353: With argument, asks for a command line. Split up screen and run forth 
                    354: in the lower portion. The current-buffer when called will stay in the
                    355: upper portion of the screen, and all other windows are deleted.
                    356: Call run-forth again to make the *forth* buffer appear in the lower
                    357: part of the screen."
                    358:   (interactive
                    359:    (list (let ((default
                    360:                 (or forth-process-command-line
                    361:                     (forth-default-command-line))))
                    362:           (if current-prefix-arg
                    363:               (read-string "Run Forth: " default)
                    364:               default))))
                    365:   (setq forth-process-command-line command-line)
                    366:   (forth-start-process command-line)
                    367:   (forth-split)
                    368:   (forth-set-runlight forth-runlight:input))
                    369: 
                    370: (defun reset-forth ()
                    371:   "Reset the Forth process."
                    372:   (interactive)
                    373:   (let ((process (get-process forth-program-name)))
                    374:     (cond ((or (not process)
                    375:               (not (eq (process-status process) 'run))
                    376:               (yes-or-no-p
                    377: "The Forth process is running, are you SURE you want to reset it? "))
                    378:           (message "Resetting Forth process...")
                    379:           (forth-reload)
                    380:           (message "Resetting Forth process...done")))))
                    381: 
                    382: (defun forth-default-command-line ()
                    383:   (concat forth-program-name " -emacs"
                    384:          (if forth-program-arguments
                    385:              (concat " " forth-program-arguments)
                    386:              "")
                    387:          (if forth-band-name
                    388:              (concat " -band " forth-band-name)
                    389:              "")))
                    390: 
                    391: ;;;; Internal Variables
                    392: 
                    393: (defvar forth-process-command-line nil
                    394:   "Command used to start the most recent Forth process.")
                    395: 
                    396: (defvar forth-previous-send ""
                    397:   "Most recent expression transmitted to the Forth process.")
                    398: 
                    399: (defvar forth-process-filter-queue '()
                    400:   "Queue used to synchronize filter actions properly.")
                    401: 
                    402: (defvar forth-prompt "ok"
                    403:   "The current forth prompt string.")
                    404: 
                    405: (defvar forth-start-hook nil
                    406:   "If non-nil, a procedure to call when the Forth process is started.
                    407: When called, the current buffer will be the Forth process-buffer.")
                    408: 
                    409: (defvar forth-signal-death-message nil
                    410:   "If non-nil, causes a message to be generated when the Forth process dies.")
                    411: 
                    412: (defvar forth-percent-height 62
                    413:   "Tells run-forth how high the upper window should be in percent.")
                    414: 
                    415: (defconst forth-runlight:input ?I
                    416:   "The character displayed when the Forth process is waiting for input.")
                    417: 
                    418: (defvar forth-mode-string ""
                    419:   "String displayed in the mode line when the Forth process is running.")
                    420: 
                    421: ;;;; Evaluation Commands
                    422: 
                    423: (defun forth-send-string (&rest strings)
                    424:   "Send the string arguments to the Forth process.
                    425: The strings are concatenated and terminated by a newline."
                    426:   (cond ((forth-process-running-p)
                    427:         (forth-send-string-1 strings))
                    428:        ((yes-or-no-p "The Forth process has died.  Reset it? ")
                    429:         (reset-forth)
                    430:         (goto-char (point-max))
                    431:         (forth-send-string-1 strings))))
                    432: 
                    433: (defun forth-send-string-1 (strings)
                    434:   (let ((string (apply 'concat strings)))
                    435:     (forth-send-string-2 string)))
                    436: 
                    437: (defun forth-send-string-2 (string)
                    438:   (let ((process (get-process forth-program-name)))
                    439:     (if (not (eq (current-buffer) (get-buffer forth-program-name)))
                    440:        (progn
                    441:         (forth-process-filter-output string)
                    442:         (forth-process-filter:finish)))
                    443:     (send-string process (concat string "\n"))
                    444:     (if (eq (current-buffer) (process-buffer process))
                    445:        (set-marker (process-mark process) (point)))))
                    446: 
                    447: 
                    448: (defun forth-send-region (start end)
                    449:   "Send the current region to the Forth process.
                    450: The region is sent terminated by a newline."
                    451:   (interactive "r")
                    452:   (let ((process (get-process forth-program-name)))
                    453:     (if (and process (eq (current-buffer) (process-buffer process)))
                    454:        (progn (goto-char end)
                    455:               (set-marker (process-mark process) end))))
                    456:   (forth-send-string "\n" (buffer-substring start end) "\n"))
                    457: 
                    458: (defun forth-end-of-paragraph ()
                    459:   (if (looking-at "[\t\n ]+") (skip-chars-backward  "\t\n "))
                    460:   (if (not (re-search-forward "\n[ \t]*\n" nil t))
                    461:       (goto-char (point-max))))
                    462: 
                    463: (defun forth-send-paragraph ()
                    464:   "Send the current or the previous paragraph to the Forth process"
                    465:   (interactive)
                    466:   (let (end)
                    467:     (save-excursion
                    468:       (forth-end-of-paragraph)
                    469:       (skip-chars-backward  "\t\n ")
                    470:       (setq end (point))
                    471:       (if (re-search-backward "\n[ \t]*\n" nil t)
                    472:          (setq start (point))
                    473:        (goto-char (point-min)))
                    474:       (skip-chars-forward  "\t\n ")
                    475:       (forth-send-region (point) end))))
                    476:   
                    477: (defun forth-send-buffer ()
                    478:   "Send the current buffer to the Forth process."
                    479:   (interactive)
                    480:   (if (eq (current-buffer) (forth-process-buffer))
                    481:       (error "Not allowed to send this buffer's contents to Forth"))
                    482:   (forth-send-region (point-min) (point-max)))
                    483: 
                    484: 
                    485: ;;;; Basic Process Control
                    486: 
                    487: (defun forth-start-process (command-line)
                    488:   (let ((buffer (get-buffer-create "*forth*")))
                    489:     (let ((process (get-buffer-process buffer)))
                    490:       (save-excursion
                    491:        (set-buffer buffer)
                    492:        (progn (if process (delete-process process))
                    493:               (goto-char (point-max))
                    494:               (setq mode-line-process '(": %s"))
                    495:               (add-to-global-mode-string 'forth-mode-string)
                    496:               (setq process
                    497:                     (apply 'start-process
                    498:                            (cons forth-program-name
                    499:                                  (cons buffer
                    500:                                        (forth-parse-command-line
                    501:                                         command-line)))))
                    502:               (set-marker (process-mark process) (point-max))
                    503:               (forth-process-filter-initialize t)
                    504:               (forth-modeline-initialize)
                    505:               (set-process-sentinel process 'forth-process-sentinel)
                    506:               (set-process-filter process 'forth-process-filter)
                    507:               (run-hooks 'forth-start-hook)))
                    508:     buffer)))
                    509: 
                    510: (defun forth-parse-command-line (string)
                    511:   (setq string (substitute-in-file-name string))
                    512:   (let ((start 0)
                    513:        (result '()))
                    514:     (while start
                    515:       (let ((index (string-match "[ \t]" string start)))
                    516:        (setq start
                    517:              (cond ((not index)
                    518:                     (setq result
                    519:                           (cons (substring string start)
                    520:                                 result))
                    521:                     nil)
                    522:                    ((= index start)
                    523:                     (string-match "[^ \t]" string start))
                    524:                    (t
                    525:                     (setq result
                    526:                           (cons (substring string start index)
                    527:                                 result))
                    528:                     (1+ index))))))
                    529:     (nreverse result)))
                    530: 
                    531: 
                    532: (defun forth-process-running-p ()
                    533:   "True iff there is a Forth process whose status is `run'."
                    534:   (let ((process (get-process forth-program-name)))
                    535:     (and process
                    536:         (eq (process-status process) 'run))))
                    537: 
                    538: (defun forth-process-buffer ()
                    539:   (let ((process (get-process forth-program-name)))
                    540:     (and process (process-buffer process))))
                    541: 
                    542: ;;;; Process Filter
                    543: 
                    544: (defun forth-process-sentinel (proc reason)
                    545:   (let ((inhibit-quit nil))
                    546:     (forth-process-filter-initialize (eq reason 'run))
                    547:     (if (eq reason 'run)
                    548:        (forth-modeline-initialize)
                    549:        (setq forth-mode-string "")))
                    550:   (if (and (not (memq reason '(run stop)))
                    551:           forth-signal-death-message)
                    552:       (progn (beep)
                    553:             (message
                    554: "The Forth process has died!  Do M-x reset-forth to restart it"))))
                    555: 
                    556: (defun forth-process-filter-initialize (running-p)
                    557:   (setq forth-process-filter-queue (cons '() '()))
                    558:   (setq forth-prompt "ok"))
                    559: 
                    560: 
                    561: (defun forth-process-filter (proc string)
                    562:   (forth-process-filter-output string)
                    563:   (forth-process-filter:finish))
                    564: 
                    565: (defun forth-process-filter:enqueue (action)
                    566:   (let ((next (cons action '())))
                    567:     (if (cdr forth-process-filter-queue)
                    568:        (setcdr (cdr forth-process-filter-queue) next)
                    569:        (setcar forth-process-filter-queue next))
                    570:     (setcdr forth-process-filter-queue next)))
                    571: 
                    572: (defun forth-process-filter:finish ()
                    573:   (while (car forth-process-filter-queue)
                    574:     (let ((next (car forth-process-filter-queue)))
                    575:       (setcar forth-process-filter-queue (cdr next))
                    576:       (if (not (cdr next))
                    577:          (setcdr forth-process-filter-queue '()))
                    578:       (apply (car (car next)) (cdr (car next))))))
                    579: 
                    580: ;;;; Process Filter Output
                    581: 
                    582: (defun forth-process-filter-output (&rest args)
                    583:   (if (not (and args
                    584:                (null (cdr args))
                    585:                (stringp (car args))
                    586:                (string-equal "" (car args))))
                    587:       (forth-process-filter:enqueue
                    588:        (cons 'forth-process-filter-output-1 args))))
                    589: 
                    590: (defun forth-process-filter-output-1 (&rest args)
                    591:   (save-excursion
                    592:     (forth-goto-output-point)
                    593:     (apply 'insert-before-markers args)))
                    594: 
                    595: (defun forth-guarantee-newlines (n)
                    596:   (save-excursion
                    597:     (forth-goto-output-point)
                    598:     (let ((stop nil))
                    599:       (while (and (not stop)
                    600:                  (bolp))
                    601:        (setq n (1- n))
                    602:        (if (bobp)
                    603:            (setq stop t)
                    604:          (backward-char))))
                    605:     (forth-goto-output-point)
                    606:     (while (> n 0)
                    607:       (insert-before-markers ?\n)
                    608:       (setq n (1- n)))))
                    609: 
                    610: (defun forth-goto-output-point ()
                    611:   (let ((process (get-process forth-program-name)))
                    612:     (set-buffer (process-buffer process))
                    613:     (goto-char (process-mark process))))
                    614: 
                    615: (defun forth-modeline-initialize ()
                    616:   (setq forth-mode-string "  "))
                    617: 
                    618: (defun forth-set-runlight (runlight)
                    619:   (aset forth-mode-string 0 runlight)
                    620:   (forth-modeline-redisplay))
                    621: 
                    622: (defun forth-modeline-redisplay ()
                    623:   (save-excursion (set-buffer (other-buffer)))
                    624:   (set-buffer-modified-p (buffer-modified-p))
                    625:   (sit-for 0))
                    626: 
                    627: ;;;; Process Filter Operations
                    628: 
                    629: (defun add-to-global-mode-string (x)
                    630:   (cond ((null global-mode-string)
                    631:         (setq global-mode-string (list "" x " ")))
                    632:        ((not (memq x global-mode-string))
                    633:         (setq global-mode-string
                    634:               (cons ""
                    635:                     (cons x
                    636:                           (cons " "
                    637:                                 (if (equal "" (car global-mode-string))
                    638:                                     (cdr global-mode-string)
                    639:                                     global-mode-string))))))))
                    640: 
                    641: 
                    642: ;; Misc
                    643: 
                    644: (setq auto-mode-alist (append auto-mode-alist
1.4       pazsan    645:                                '(("\\.fs$" . forth-mode))))
1.1       anton     646: 
                    647: (defun forth-split ()
                    648:   (interactive)
                    649:   (forth-split-1 "*forth*"))
                    650: 
                    651: (defun forth-split-1 (buffer)
                    652:   (if (not (eq (window-buffer) (get-buffer buffer)))
                    653:       (progn
                    654:        (delete-other-windows)
                    655:        (split-window-vertically
                    656:         (/ (* (screen-height) forth-percent-height) 100))
                    657:        (other-window 1)
                    658:        (switch-to-buffer buffer)
                    659:        (goto-char (point-max))
                    660:        (other-window 1))))
                    661:     
                    662: (defun forth-reload ()
                    663:   (interactive)
                    664:   (let ((process (get-process forth-program-name)))
                    665:     (if process (kill-process process t)))
                    666:   (sleep-for-millisecs 100)
                    667:   (forth-mode))
                    668: 
                    669: 
                    670: ;; Special section for forth-help
                    671: 
                    672: (defvar forth-help-buffer "*Forth-help*"
                    673:   "Buffer used to display the requested documentation.")
                    674: 
                    675: (defvar forth-help-load-path nil
                    676:   "List of directories to search through to find *.doc
                    677:  (forth-help-file-suffix) files. Nil means current default directory.
                    678:  The specified directories must contain at least one .doc file. If it
                    679:  does not and you still want the load-path to scan that directory, create
                    680:  an empty file dummy.doc.")
                    681: 
                    682: (defvar forth-help-file-suffix "*.doc"
                    683:   "The file names to search for in each directory.")
                    684: 
                    685: (setq forth-search-command-prefix "grep -n \"^    [^(]* ")
                    686: (defvar forth-search-command-suffix "/dev/null")
                    687: (defvar forth-grep-error-regexp ": No such file or directory")
                    688: 
                    689: (defun forth-function-called-at-point ()
                    690:   "Return the space delimited word a point."
                    691:   (save-excursion
                    692:     (save-restriction
                    693:       (narrow-to-region (max (point-min) (- (point) 1000)) (point-max))
                    694:       (skip-chars-backward "^ \t\n" (point-min))
                    695:       (if (looking-at "[ \t\n]")
                    696:          (forward-char 1))
                    697:       (let (obj (p (point)))
                    698:        (skip-chars-forward "^ \t\n")
                    699:        (buffer-substring p (point))))))
                    700: 
                    701: (defun forth-help-names-extend-comp (path-list result)
                    702:   (cond ((null path-list) result)
                    703:        ((null (car path-list))
                    704:         (forth-help-names-extend-comp (cdr path-list) 
                    705:               (concat result forth-help-file-suffix " ")))
                    706:        (t (forth-help-names-extend-comp
                    707:            (cdr path-list) (concat result
                    708:                                    (expand-file-name (car path-list)) "/"
                    709:                                    forth-help-file-suffix " ")))))
                    710: 
                    711: (defun forth-help-names-extended ()
                    712:   (if forth-help-load-path
                    713:       (forth-help-names-extend-comp forth-help-load-path "")
                    714:     (error "forth-help-load-path not specified")))
                    715: 
                    716: 
                    717: (define-key forth-mode-map "\C-hf" 'forth-documentation)
                    718: 
                    719: (defun forth-documentation (function)
                    720:   "Display the full documentation of FORTH word."
                    721:   (interactive
                    722:    (let ((fn (forth-function-called-at-point))
                    723:         (enable-recursive-minibuffers t)            
                    724:         search-list
                    725:         val)
                    726:      (setq val (read-string (format "Describe forth word (default %s): " fn)))
                    727:      (list (if (equal val "") fn val))))
                    728:   (forth-get-doc (concat forth-search-command-prefix
                    729:                         (grep-regexp-quote (concat function " ("))
                    730:                         "[^)]*\-\-\" " (forth-help-names-extended)
                    731:                         forth-search-command-suffix))
                    732:   (message "C-x C-m switches back to the forth interaction window"))
                    733: 
                    734: (defun forth-get-doc (command)
                    735:   "Display the full documentation of command."
                    736:   (let ((curwin (get-buffer-window (window-buffer)))
                    737:        reswin
                    738:        pointmax)
                    739:     (with-output-to-temp-buffer forth-help-buffer
                    740:       (progn
                    741:        (call-process "sh" nil forth-help-buffer t "-c" command)
                    742:        (setq reswin (get-buffer-window forth-help-buffer))))
                    743:     (setq reswin (get-buffer-window forth-help-buffer))
                    744:     (select-window reswin)
                    745:     (save-excursion
                    746:       (goto-char (setq pointmax (point-max)))
                    747:       (insert "--------------------\n\n"))
                    748:     (let (fd doc) 
                    749:       (while (setq fd (forth-get-file-data pointmax))
                    750:        (setq doc (forth-get-doc-string fd))
                    751:        (save-excursion
                    752:          (goto-char (point-max))
                    753:          (insert (substring (car fd) (string-match "[^/]*$" (car fd)))
                    754:                  ":\n\n" doc "\n")))
                    755:       (if (not doc)
                    756:          (progn (goto-char (point-max)) (insert "Not found"))))
                    757:     (select-window curwin)))
                    758:   
                    759: (defun forth-skip-error-lines ()
                    760:   (let ((lines 0))
                    761:     (save-excursion
                    762:       (while (re-search-forward forth-grep-error-regexp nil t)
                    763:        (beginning-of-line)
                    764:        (forward-line 1)
                    765:        (setq lines (1+ lines))))
                    766:     (forward-line lines)))
                    767: 
                    768: (defun forth-get-doc-string (fd)
                    769:   "Find file (car fd) and extract documentation from line (nth 1 fd)."
                    770:   (let (result)
                    771:     (save-window-excursion
                    772:       (find-file (car fd))
                    773:       (goto-line (nth 1 fd))
                    774:       (if (not (eq (nth 1 fd) (1+ (count-lines (point-min) (point)))))
                    775:          (error "forth-get-doc-string: serious error"))
                    776:       (if (not (re-search-backward "\n[\t ]*\n" nil t))
                    777:          (goto-char (point-min))
                    778:        (goto-char (match-end 0)))
                    779:       (let ((p (point)))
                    780:        (if (not (re-search-forward "\n[\t ]*\n" nil t))
                    781:            (goto-char (point-max)))
                    782:        (setq result (buffer-substring p (point))))
                    783:       (bury-buffer (current-buffer)))
                    784:     result))
                    785: 
                    786: (defun forth-get-file-data (limit)
                    787:   "Parse grep output and return '(filename line#) list. Return nil when
                    788:  passing limit."
                    789:   (forth-skip-error-lines)
                    790:   (if (< (point) limit)
                    791:       (let ((result (forth-get-file-data-cont limit)))
                    792:        (forward-line 1)
                    793:        (beginning-of-line)
                    794:        result)))
                    795: 
                    796: (defun forth-get-file-data-cont (limit)
                    797:   (let (result)
                    798:     (let ((p (point)))
                    799:       (skip-chars-forward "^:")
                    800:       (setq result (buffer-substring p (point))))
                    801:     (if (< (point) limit)
                    802:        (let ((p (1+ (point))))
                    803:          (forward-char 1)
                    804:          (skip-chars-forward "^:")
                    805:          (list result (string-to-int (buffer-substring p (point))))))))
                    806: 
                    807: (defun grep-regexp-quote (str)
                    808:   (let ((i 0) (m 1) (res ""))
                    809:     (while (/= m 0)
                    810:       (setq m (string-to-char (substring str i)))
                    811:       (if (/= m 0)
                    812:          (progn
                    813:            (setq i (1+ i))
                    814:            (if (string-match (regexp-quote (char-to-string m))
                    815:                              ".*\\^$[]")
                    816:                (setq res (concat res "\\")))
                    817:            (setq res (concat res (char-to-string m))))))
                    818:     res))
                    819: 
                    820: 
                    821: (define-key forth-mode-map "\C-x\C-e" 'forth-compile)
                    822: (define-key forth-mode-map "\C-x\C-n" 'next-error)
                    823: (require 'compile "compile")
                    824: 
1.6     ! anton     825: (defvar forth-compile-command "gforth ")
1.1       anton     826: (defvar forth-compilation-window-percent-height 30)
                    827: 
                    828: (defun forth-compile (command)
                    829:   (interactive (list (setq forth-compile-command (read-string "Compile command: " forth-compile-command))))
                    830:   (forth-split-1 "*compilation*")
                    831:   (setq ctools-compile-command command)
                    832:   (compile1 ctools-compile-command "No more errors"))
                    833: 
                    834: 

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