mirror of https://github.com/Chizi123/.emacs.d.git

Chizi123
2018-11-18 76bbd07de7add0f9d13c6914f158d19630fe2f62
commit | author | age
76bbd0 1 ;;; ob-forth.el --- Babel Functions for Forth        -*- lexical-binding: t; -*-
C 2
3 ;; Copyright (C) 2014-2018 Free Software Foundation, Inc.
4
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research, forth
7 ;; Homepage: https://orgmode.org
8
9 ;; This file is part of GNU Emacs.
10
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.
15
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.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; Requires the gforth forth compiler and `forth-mode' (see below).
27 ;; https://www.gnu.org/software/gforth/
28
29 ;;; Requirements:
30
31 ;; Session evaluation requires the gforth forth compiler as well as
32 ;; `forth-mode' which is distributed with gforth (in gforth.el).
33
34 ;;; Code:
35 (require 'ob)
36
37 (declare-function forth-proc "ext:gforth" ())
38 (declare-function org-trim "org" (s &optional keep-lead))
39
40 (defvar org-babel-default-header-args:forth '((:session . "yes"))
41   "Default header arguments for forth code blocks.")
42
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 (assq :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 (assq :result-params params)))
50       (mapconcat #'identity all-results "\n")
51     (car (last all-results))))))
52
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)))))
84
85 (provide 'ob-forth)
86
87 ;;; ob-forth.el ends here