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