ob-forth.el 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. ;;; ob-forth.el --- Babel Functions for Forth -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2014-2018 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research, forth
  5. ;; Homepage: http://orgmode.org
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; Requires the gforth forth compiler and `forth-mode' (see below).
  19. ;; https://www.gnu.org/software/gforth/
  20. ;;; Requirements:
  21. ;; Session evaluation requires the gforth forth compiler as well as
  22. ;; `forth-mode' which is distributed with gforth (in gforth.el).
  23. ;;; Code:
  24. (require 'ob)
  25. (declare-function forth-proc "ext:gforth" ())
  26. (declare-function org-trim "org" (s &optional keep-lead))
  27. (defvar org-babel-default-header-args:forth '((:session . "yes"))
  28. "Default header arguments for forth code blocks.")
  29. (defun org-babel-execute:forth (body params)
  30. "Execute a block of Forth code with org-babel.
  31. This function is called by `org-babel-execute-src-block'"
  32. (if (string= "none" (cdr (assq :session params)))
  33. (error "Non-session evaluation not supported for Forth code blocks")
  34. (let ((all-results (org-babel-forth-session-execute body params)))
  35. (if (member "output" (cdr (assq :result-params params)))
  36. (mapconcat #'identity all-results "\n")
  37. (car (last all-results))))))
  38. (defun org-babel-forth-session-execute (body params)
  39. (require 'forth-mode)
  40. (let ((proc (forth-proc))
  41. (rx " \\(\n:\\|compiled\n\\\|ok\n\\)")
  42. (result-start))
  43. (with-current-buffer (process-buffer (forth-proc))
  44. (mapcar (lambda (line)
  45. (setq result-start (progn (goto-char (process-mark proc))
  46. (point)))
  47. (comint-send-string proc (concat line "\n"))
  48. ;; wait for forth to say "ok"
  49. (while (not (progn (goto-char result-start)
  50. (re-search-forward rx nil t)))
  51. (accept-process-output proc 0.01))
  52. (let ((case (match-string 1)))
  53. (cond
  54. ((string= "ok\n" case)
  55. ;; Collect intermediate output.
  56. (buffer-substring (+ result-start 1 (length line))
  57. (match-beginning 0)))
  58. ((string= "compiled\n" case))
  59. ;; Ignore partial compilation.
  60. ((string= "\n:" case)
  61. ;; Report errors.
  62. (org-babel-eval-error-notify 1
  63. (buffer-substring
  64. (+ (match-beginning 0) 1) (point-max))) nil))))
  65. (split-string (org-trim
  66. (org-babel-expand-body:generic body params))
  67. "\n"
  68. 'omit-nulls)))))
  69. (provide 'ob-forth)
  70. ;;; ob-forth.el ends here