1 ;;; ob-J.el --- org-babel functions for J evaluation
3 ;; Copyright (C) 2011-2013 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 <http://www.gnu.org/licenses/>.
26 ;; Session interaction depends on `j-console'.
31 (defun org-babel-expand-body:J
(body params
&optional processed-params
)
32 "Expand BODY according to PARAMS, return the expanded body.
33 PROCESSED-PARAMS isn't used yet."
34 (org-babel-J-interleave-echos-except-functions body
))
36 (defun org-babel-J-interleave-echos (body)
37 "Interleave echo'' between each source line of BODY."
38 (mapconcat #'identity
(split-string body
"\n") "\necho''\n"))
40 (defun org-babel-J-interleave-echos-except-functions (body)
41 "Interleave echo'' between source lines of BODY that aren't functions."
42 (if (obj-string-match-m "\\(?:^\\|\n\\)[^\n]*\\(?:1\\|2\\|3\\|4\\) : 0\n.*)\\(?:\n\\|$\\)" body
)
43 (let ((s1 (substring body
0 (match-beginning 0)))
44 (s2 (match-string 0 body
))
45 (s3 (substring body
(match-end 0))))
47 (org-babel-J-interleave-echos s1
)
50 (org-babel-J-interleave-echos-except-functions s3
)))
51 (org-babel-J-interleave-echos body
)))
53 (defun org-babel-execute:J
(body params
)
54 "Execute a block of J code BODY.
55 PARAMS are given by org-babel.
56 This function is called by `org-babel-execute-src-block'"
57 (message "executing J source code block")
58 (let* ((processed-params (org-babel-process-params params
))
59 (sessionp (cdr (assoc :session params
)))
60 (session (org-babel-j-initiate-session sessionp
))
61 (vars (second processed-params
))
62 (result-params (third processed-params
))
63 (result-type (fourth processed-params
))
64 (full-body (org-babel-expand-body:J
65 body params processed-params
))
66 (tmp-script-file (org-babel-temp-file "J-src")))
67 (org-babel-J-strip-whitespace
68 (if (string= sessionp
"none")
70 (with-temp-file tmp-script-file
72 (org-babel-eval (format "jconsole < %s" tmp-script-file
) ""))
73 (org-babel-J-eval-string full-body
)))))
75 (defun org-babel-J-eval-string (str)
76 "Sends STR to the `j-console-cmd' session and exectues it."
77 (let ((session (j-console-ensure-session)))
78 (with-current-buffer (process-buffer session
)
79 (goto-char (point-max))
80 (insert (format "\n%s\n" str
))
84 (buffer-substring-no-properties
87 (defun org-babel-J-strip-whitespace (str)
88 "Remove whitespace from jconsole output STR."
89 (let ((strs (split-string str
"\n" t
))
91 (while (setq s
(pop strs
))
92 (if (string-match "^ *$" s
)
93 (progn (push (nreverse cur
) out
)
96 (mapconcat #'org-babel-J-print-block
97 (delq nil
(nreverse out
))
100 (defun org-babel-J-print-block (x)
101 "Prettify jconsole output X."
104 ;; assume only first row is misaligned
105 (let ((n1 (obj-match-second-space (car x
)))
106 (n2 (obj-match-second-space (cadr x
))))
110 (substring (car x
) (- n1 n2
))
112 (mapconcat #'identity x
"\n"))))
114 (defun obj-match-second-space (s)
115 "Return position of second space in S or nil."
116 (and (string-match "^ *[^ ]+\\( \\)" s
)
117 (match-beginning 1)))
119 (defun obj-string-match-m (regexp string
&optional start
)
120 "Like `sting-match', only .* includes newlines too."
122 (replace-regexp-in-string "\\.\\*" "[\0-\377[:nonascii:]]*" regexp
)
126 (defun org-babel-j-initiate-session (&optional session
)
127 "Initiate a J session.
128 SESSION is a parameter given by org-babel."
129 (unless (string= session
"none")
131 (j-console-ensure-session)))
135 ;;; ob-J.el ends here