forked from Skrylar/red.el
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ob-red.el
163 lines (144 loc) · 6.7 KB
/
ob-red.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
;;; ob-red.el --- org-babel functions for red evaluation
;; Copyright (C) your name here
;; Author: your name here
;; Keywords: literate programming, reproducible research
;; Homepage: http://orgmode.org
;; Version: 0.01
;;; License:
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;; This file is not intended to ever be loaded by org-babel, rather it
;; is a template for use in adding new language support to Org-babel.
;; Good first steps are to copy this file to a file named by the
;; language you are adding, and then use `query-replace' to replace
;; all strings of "template" in this file with the name of your new
;; language.
;;
;; If you have questions as to any of the portions of the file defined
;; below please look to existing language support for guidance.
;;
;; If you are planning on adding a language to org-babel we would ask
;; that if possible you fill out the FSF copyright assignment form
;; available at http://orgmode.org/request-assign-future.txt as this
;; will make it possible to include your language support in the core
;; of Org-mode, otherwise unassigned language support files can still
;; be included in the contrib/ directory of the Org-mode repository.
;;; Requirements:
;; Use this section to list the requirements of this language. Most
;; languages will require that at least the language be installed on
;; the user's system, and the Emacs major mode relevant to the
;; language be installed as well.
;;; Code:
(require 'ob)
(require 'ob-ref)
(require 'ob-comint)
(require 'ob-eval)
;; possibly require modes required for your language
;; optionally define a file extension for this language
(add-to-list 'org-babel-tangle-lang-exts '("red" . "red"))
;; optionally declare default header arguments for this language
(defvar org-babel-default-header-args:red '())
;; This function expands the body of a source code block by doing
;; things like prepending argument definitions to the body, it should
;; be called by the `org-babel-execute:red' function below.
;; (defun org-babel-expand-body:red (body params &optional processed-params)
;; "Expand BODY according to PARAMS, return the expanded body."
;; (require 'inf-template)
;; (let ((vars (nth 1 (or processed-params (org-babel-process-params params)))))
;; (concat
;; (mapconcat ;; define any variables
;; (lambda (pair)
;; (format "%s=%S"
;; (car pair) (org-babel-template-var-to-template (cdr pair))))
;; vars "\n") "\n" body "\n")))
(defun org-babel-expand-body:red (body params &optional processed-params)
"Expand BODY"
(concat "Red []\n" body))
;; This is the main function which is called to evaluate a code
;; block.
;;
;; This function will evaluate the body of the source code and
;; return the results as emacs-lisp depending on the value of the
;; :results header argument
;; - output means that the output to STDOUT will be captured and
;; returned
;; - value means that the value of the last statement in the
;; source code block will be returned
;;
;; The most common first step in this function is the expansion of the
;; PARAMS argument using `org-babel-process-params'.
;;
;; Please feel free to not implement options which aren't appropriate
;; for your language (e.g. not all languages support interactive
;; "session" evaluation). Also you are free to define any new header
;; arguments which you feel may be useful -- all header arguments
;; specified by the user will be available in the PARAMS variable.
(defun org-babel-execute:red (body params)
"Execute a block of red code with org-babel.
This function is called by `org-babel-execute-src-block'"
(message "executing red source code block")
(let* ((processed-params (org-babel-process-params params))
;; set the session if the session variable is non-nil
;; (session (org-babel-template-initiate-session (first processed-params)))
;; variables assigned for use in the block
(vars (second processed-params))
(result-params (third processed-params))
;; either OUTPUT or VALUE which should behave as described above
(result-type (fourth processed-params))
;; expand the body with `org-babel-expand-body:red'
(full-body (org-babel-expand-body:red body params processed-params))
;; (full-body (org-babel-expand-body:red body params processed-params))
)
;; actually execute the source-code block either in a session or
;; possibly by dropping it to a temporary file and evaluating the
;; file.
;;
;; for session based evaluation the functions defined in
;; `org-babel-comint' will probably be helpful.
;;
;; for external evaluation the functions defined in
;; `org-babel-eval' will probably be helpful.
;;
(let ((script-file (org-babel-temp-file "red-script-")))
(with-temp-file script-file
(insert "Red []\n")
(insert body))
(shell-command-to-string (concat "red " script-file)))
;; when forming a shell command, or a fragment of code in some
;; other language, please preprocess any file names involved with
;; the function `org-babel-process-file-name'. (See the way that
;; function is used in the language files)
))
;; This function should be used to assign any variables in params in
;; the context of the session environment.
;; (defun org-babel-prep-session:red (session params)
;; "Prepare SESSION according to the header arguments specified in PARAMS."
;; )
;; (defun org-babel-template-var-to-template (var)
;; "Convert an elisp var into a string of template source code
;; specifying a var of the same value."
;; (format "%S" var))
;; (defun org-babel-template-table-or-string (results)
;; "If the results look like a table, then convert them into an
;; Emacs-lisp table, otherwise return the results as a string."
;; )
;; (defun org-babel-template-initiate-session (&optional session)
;; "If there is not a current inferior-process-buffer in SESSION then create.
;; Return the initialized session."
;; (unless (string= session "none")
;; ))
(provide 'ob-red)
;;; ob-red.el ends here