home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / pc / source / tile.lzh / tile.5 < prev    next >
Encoding:
Text File  |  1990-07-26  |  28.5 KB  |  849 lines

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