1 ;;; ob-J.el --- Babel Functions for J -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2011-2017 Free Software Foundation, Inc.
6 ;; Keywords: literate programming, reproducible research
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 <https://www.gnu.org/licenses/>.
26 ;; Org-Babel support for evaluating J code.
28 ;; Session interaction depends on `j-console' from package `j-mode'
29 ;; (available in MELPA).
35 (declare-function org-trim
"org" (s &optional keep-lead
))
36 (declare-function j-console-ensure-session
"ext:j-console" ())
38 (defcustom org-babel-J-command
"jconsole"
42 :package-version
'(Org .
"9.0")
45 (defun org-babel-expand-body:J
(body _params
&optional _processed-params
)
46 "Expand BODY according to PARAMS, return the expanded body.
47 PROCESSED-PARAMS isn't used yet."
48 (org-babel-J-interleave-echos-except-functions body
))
50 (defun org-babel-J-interleave-echos (body)
51 "Interleave echo',' between each source line of BODY."
52 (mapconcat #'identity
(split-string body
"\n") "\necho','\n"))
54 (defun org-babel-J-interleave-echos-except-functions (body)
55 "Interleave echo',' between source lines of BODY that aren't functions."
56 (if (obj-string-match-m "\\(?:^\\|\n\\)[^\n]*\\(?:0\\|1\\|2\\|3\\|4\\|dyad\\) : 0\n.*\n)\\(?:\n\\|$\\)" body
)
57 (let ((s1 (substring body
0 (match-beginning 0)))
58 (s2 (match-string 0 body
))
59 (s3 (substring body
(match-end 0))))
63 (concat (org-babel-J-interleave-echos s1
)
67 (org-babel-J-interleave-echos-except-functions s3
)))
68 (org-babel-J-interleave-echos body
)))
70 (defalias 'org-babel-execute
:j
'org-babel-execute
:J
)
72 (defun org-babel-execute:J
(body params
)
73 "Execute a block of J code BODY.
74 PARAMS are given by org-babel.
75 This function is called by `org-babel-execute-src-block'"
76 (message "executing J source code block")
77 (let* ((processed-params (org-babel-process-params params
))
78 (sessionp (cdr (assq :session params
)))
79 (full-body (org-babel-expand-body:J
80 body params processed-params
))
81 (tmp-script-file (org-babel-temp-file "J-src")))
82 (org-babel-j-initiate-session sessionp
)
83 (org-babel-J-strip-whitespace
84 (if (string= sessionp
"none")
86 (with-temp-file tmp-script-file
88 (org-babel-eval (format "%s < %s" org-babel-J-command tmp-script-file
) ""))
89 (org-babel-J-eval-string full-body
)))))
91 (defun org-babel-J-eval-string (str)
92 "Sends STR to the `j-console-cmd' session and executes it."
93 (let ((session (j-console-ensure-session)))
94 (with-current-buffer (process-buffer session
)
95 (goto-char (point-max))
96 (insert (format "\n%s\n" str
))
100 (buffer-substring-no-properties
103 (defun org-babel-J-strip-whitespace (str)
104 "Remove whitespace from jconsole output STR."
108 #'org-babel-J-print-block
109 (split-string str
"^ *,\n" t
)))
112 (defun obj-get-string-alignment (str)
113 "Return a number to describe STR alignment.
114 STR represents a table.
115 Positive/negative/zero result means right/left/undetermined.
116 Don't trust first line."
117 (let* ((str (org-trim str
))
118 (lines (split-string str
"\n" t
))
120 (cond ((<= (length lines
) 1)
122 ((= (length lines
) 2)
123 ;; numbers are right-aligned
125 (numberp (read (car lines
)))
126 (numberp (read (cadr lines
)))
127 (setq n1
(obj-match-second-space-right (nth 0 lines
)))
128 (setq n2
(obj-match-second-space-right (nth 1 lines
))))
131 ((not (obj-match-second-space-left (nth 0 lines
)))
134 (setq n1
(obj-match-second-space-left (nth 1 lines
)))
135 (setq n2
(obj-match-second-space-left (nth 2 lines
)))
139 (setq n1
(obj-match-second-space-right (nth 1 lines
)))
140 (setq n2
(obj-match-second-space-right (nth 2 lines
)))
145 (defun org-babel-J-print-block (x)
146 "Prettify jconsole output X."
147 (let* ((x (org-trim x
))
148 (a (obj-get-string-alignment x
))
149 (lines (split-string x
"\n" t
))
152 (setq b
(obj-match-second-space-right (nth 0 lines
)))
153 (concat (make-string (+ a b
) ?
) x
))
155 (setq b
(obj-match-second-space-left (nth 0 lines
)))
156 (concat (make-string (- a b
) ?
) x
))
159 (defun obj-match-second-space-left (s)
160 "Return position of leftmost space in second space block of S or nil."
161 (and (string-match "^ *[^ ]+\\( \\)" s
)
162 (match-beginning 1)))
164 (defun obj-match-second-space-right (s)
165 "Return position of rightmost space in second space block of S or nil."
166 (and (string-match "^ *[^ ]+ *\\( \\)[^ ]" s
)
167 (match-beginning 1)))
169 (defun obj-string-match-m (regexp string
&optional start
)
170 "Call (string-match REGEXP STRING START).
171 REGEXP is modified so that .* matches newlines as well."
173 (replace-regexp-in-string "\\.\\*" "[\0-\377[:nonascii:]]*" regexp
)
177 (defun org-babel-j-initiate-session (&optional session
)
178 "Initiate a J session.
179 SESSION is a parameter given by org-babel."
180 (unless (string= session
"none")
182 (j-console-ensure-session)))
186 ;;; ob-J.el ends here