File:  [gforth] / gforth / gforth.el
Revision 1.25: download - view: text, annotated - select for diffs
Sun Apr 12 21:49:13 1998 UTC (25 years, 11 months ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Added mini-oof and example, started docu for that.
Corrected "load" bug in blocks.fs (seems to be that really noone uses blocks.fs).
Minor changes do gforth.el

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

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