File:  [gforth] / gforth / gforth.el
Revision 1.9: download - view: text, annotated - select for diffs
Thu Jan 12 18:37:53 1995 UTC (29 years, 3 months ago) by anton
Branches: MAIN
CVS tags: HEAD
fixed a few bugs in gforth.el
more documentation

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

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