Annotation of gforth/gforth.el, revision 1.5

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

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