ox-md.el: Add export-block
[org-mode.git] / lisp / ob-J.el
blob97928ec84a7edaf5e86a4e3fe38b58659ee08f02
1 ;;; ob-J.el --- org-babel functions for J evaluation
3 ;; Copyright (C) 2011-2013 Free Software Foundation, Inc.
5 ;; Author: Oleh Krehel
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/>.
24 ;;; Commentary:
26 ;; Org-Babel support for evaluating J code.
28 ;; Session interaction depends on `j-console' from package `j-mode'
29 ;; (available in MELPA).
31 ;;; Code:
32 (require 'ob)
34 (declare-function j-console-ensure-session "ext:j-console" ())
36 (defun org-babel-expand-body:J (body params &optional processed-params)
37 "Expand BODY according to PARAMS, return the expanded body.
38 PROCESSED-PARAMS isn't used yet."
39 (org-babel-J-interleave-echos-except-functions body))
41 (defun org-babel-J-interleave-echos (body)
42 "Interleave echo',' between each source line of BODY."
43 (mapconcat #'identity (split-string body "\n") "\necho','\n"))
45 (defun org-babel-J-interleave-echos-except-functions (body)
46 "Interleave echo',' between source lines of BODY that aren't functions."
47 (if (obj-string-match-m "\\(?:^\\|\n\\)[^\n]*\\(?:0\\|1\\|2\\|3\\|4\\|dyad\\) : 0\n.*\n)\\(?:\n\\|$\\)" body)
48 (let ((s1 (substring body 0 (match-beginning 0)))
49 (s2 (match-string 0 body))
50 (s3 (substring body (match-end 0))))
51 (concat
52 (if (string= s1 "")
54 (concat (org-babel-J-interleave-echos s1)
55 "\necho','\n"))
57 "\necho','\n"
58 (org-babel-J-interleave-echos-except-functions s3)))
59 (org-babel-J-interleave-echos body)))
61 (defun org-babel-execute:J (body params)
62 "Execute a block of J code BODY.
63 PARAMS are given by org-babel.
64 This function is called by `org-babel-execute-src-block'"
65 (message "executing J source code block")
66 (let* ((processed-params (org-babel-process-params params))
67 (sessionp (cdr (assoc :session params)))
68 (session (org-babel-j-initiate-session sessionp))
69 (vars (second processed-params))
70 (result-params (third processed-params))
71 (result-type (fourth processed-params))
72 (full-body (org-babel-expand-body:J
73 body params processed-params))
74 (tmp-script-file (org-babel-temp-file "J-src")))
75 (org-babel-J-strip-whitespace
76 (if (string= sessionp "none")
77 (progn
78 (with-temp-file tmp-script-file
79 (insert full-body))
80 (org-babel-eval (format "jconsole < %s" tmp-script-file) ""))
81 (org-babel-J-eval-string full-body)))))
83 (defun org-babel-J-eval-string (str)
84 "Sends STR to the `j-console-cmd' session and exectues it."
85 (let ((session (j-console-ensure-session)))
86 (with-current-buffer (process-buffer session)
87 (goto-char (point-max))
88 (insert (format "\n%s\n" str))
89 (let ((beg (point)))
90 (comint-send-input)
91 (sit-for .1)
92 (buffer-substring-no-properties
93 beg (point-max))))))
95 (defun org-babel-J-strip-whitespace (str)
96 "Remove whitespace from jconsole output STR."
97 (mapconcat
98 #'identity
99 (delete "" (mapcar
100 #'org-babel-J-print-block
101 (split-string str "^ *,\n" t)))
102 "\n\n"))
104 (defun obj-get-string-alignment (str)
105 "Return a number to describe STR alignment.
106 STR represents a table.
107 Positive/negative/zero result means right/left/undetermined.
108 Don't trust first line."
109 (let* ((str (org-trim str))
110 (lines (split-string str "\n" t))
111 n1 n2)
112 (cond ((<= (length lines) 1)
114 ((= (length lines) 2)
115 ;; numbers are right-aligned
116 (if (and
117 (numberp (read (car lines)))
118 (numberp (read (cadr lines)))
119 (setq n1 (obj-match-second-space-right (nth 0 lines)))
120 (setq n2 (obj-match-second-space-right (nth 1 lines))))
123 ((not (obj-match-second-space-left (nth 0 lines)))
125 ((and
126 (setq n1 (obj-match-second-space-left (nth 1 lines)))
127 (setq n2 (obj-match-second-space-left (nth 2 lines)))
128 (= n1 n2))
130 ((and
131 (setq n1 (obj-match-second-space-right (nth 1 lines)))
132 (setq n2 (obj-match-second-space-right (nth 2 lines)))
133 (= n1 n2))
134 (- n1))
135 (t 0))))
137 (defun org-babel-J-print-block (x)
138 "Prettify jconsole output X."
139 (let* ((x (org-trim x))
140 (a (obj-get-string-alignment x))
141 (lines (split-string x "\n" t))
143 (cond ((minusp a)
144 (setq b (obj-match-second-space-right (nth 0 lines)))
145 (concat (make-string (+ a b) ? ) x))
146 ((plusp a)
147 (setq b (obj-match-second-space-left (nth 0 lines)))
148 (concat (make-string (- a b) ? ) x))
149 (t x))))
151 (defun obj-match-second-space-left (s)
152 "Return position of leftmost space in second space block of S or nil."
153 (and (string-match "^ *[^ ]+\\( \\)" s)
154 (match-beginning 1)))
156 (defun obj-match-second-space-right (s)
157 "Return position of rightmost space in second space block of S or nil."
158 (and (string-match "^ *[^ ]+ *\\( \\)[^ ]" s)
159 (match-beginning 1)))
161 (defun obj-string-match-m (regexp string &optional start)
162 "Call (string-match REGEXP STRING START).
163 REGEXP is modified so that .* matches newlines as well."
164 (string-match
165 (replace-regexp-in-string "\\.\\*" "[\0-\377[:nonascii:]]*" regexp)
166 string
167 start))
169 (defun org-babel-j-initiate-session (&optional session)
170 "Initiate a J session.
171 SESSION is a parameter given by org-babel."
172 (unless (string= session "none")
173 (require 'j-console)
174 (j-console-ensure-session)))
176 (provide 'ob-J)
178 ;;; ob-J.el ends here