ob-forth.el 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. ;;; ob-forth.el --- Babel Functions for Forth -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2014 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 <http://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. (defvar org-babel-default-header-args:forth '((:session . "yes"))
  27. "Default header arguments for forth code blocks.")
  28. (defun org-babel-execute:forth (body params)
  29. "Execute a block of Forth code with org-babel.
  30. This function is called by `org-babel-execute-src-block'"
  31. (if (string= "none" (cdr (assoc :session params)))
  32. (error "Non-session evaluation not supported for Forth code blocks")
  33. (let ((all-results (org-babel-forth-session-execute body params)))
  34. (if (member "output" (cdr (assoc :result-params params)))
  35. (mapconcat #'identity all-results "\n")
  36. (car (last all-results))))))
  37. (defun org-babel-forth-session-execute (body params)
  38. (require 'forth-mode)
  39. (let ((proc (forth-proc))
  40. (rx " \\(\n:\\|compiled\n\\\|ok\n\\)")
  41. (result-start))
  42. (with-current-buffer (process-buffer (forth-proc))
  43. (mapcar (lambda (line)
  44. (setq result-start (progn (goto-char (process-mark proc))
  45. (point)))
  46. (comint-send-string proc (concat line "\n"))
  47. ;; wait for forth to say "ok"
  48. (while (not (progn (goto-char result-start)
  49. (re-search-forward rx nil t)))
  50. (accept-process-output proc 0.01))
  51. (let ((case (match-string 1)))
  52. (cond
  53. ((string= "ok\n" case)
  54. ;; Collect intermediate output.
  55. (buffer-substring (+ result-start 1 (length line))
  56. (match-beginning 0)))
  57. ((string= "compiled\n" case))
  58. ;; Ignore partial compilation.
  59. ((string= "\n:" case)
  60. ;; Report errors.
  61. (org-babel-eval-error-notify 1
  62. (buffer-substring
  63. (+ (match-beginning 0) 1) (point-max))) nil))))
  64. (split-string (org-babel-trim
  65. (org-babel-expand-body:generic
  66. body params))
  67. "\n" 'omit-nulls)))))
  68. (provide 'ob-forth)
  69. ;;; ob-forth.el ends here