org.el (org-todo): When changing from one state to the same state, throw a more accur...
[org-mode.git] / lisp / ob-J.el
blob13ea3fe7bee672c96b0f11f673becdbc35f951fc
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 org-trim "org" (S))
35 (declare-function j-console-ensure-session "ext:j-console" ())
37 (defun org-babel-expand-body:J (body params &optional processed-params)
38 "Expand BODY according to PARAMS, return the expanded body.
39 PROCESSED-PARAMS isn't used yet."
40 (org-babel-J-interleave-echos-except-functions body))
42 (defun org-babel-J-interleave-echos (body)
43 "Interleave echo',' between each source line of BODY."
44 (mapconcat #'identity (split-string body "\n") "\necho','\n"))
46 (defun org-babel-J-interleave-echos-except-functions (body)
47 "Interleave echo',' between source lines of BODY that aren't functions."
48 (if (obj-string-match-m "\\(?:^\\|\n\\)[^\n]*\\(?:0\\|1\\|2\\|3\\|4\\|dyad\\) : 0\n.*\n)\\(?:\n\\|$\\)" body)
49 (let ((s1 (substring body 0 (match-beginning 0)))
50 (s2 (match-string 0 body))
51 (s3 (substring body (match-end 0))))
52 (concat
53 (if (string= s1 "")
55 (concat (org-babel-J-interleave-echos s1)
56 "\necho','\n"))
58 "\necho','\n"
59 (org-babel-J-interleave-echos-except-functions s3)))
60 (org-babel-J-interleave-echos body)))
62 (defun org-babel-execute:J (body params)
63 "Execute a block of J code BODY.
64 PARAMS are given by org-babel.
65 This function is called by `org-babel-execute-src-block'"
66 (message "executing J source code block")
67 (let* ((processed-params (org-babel-process-params params))
68 (sessionp (cdr (assoc :session params)))
69 (session (org-babel-j-initiate-session sessionp))
70 (vars (second processed-params))
71 (result-params (third processed-params))
72 (result-type (fourth processed-params))
73 (full-body (org-babel-expand-body:J
74 body params processed-params))
75 (tmp-script-file (org-babel-temp-file "J-src")))
76 (org-babel-J-strip-whitespace
77 (if (string= sessionp "none")
78 (progn
79 (with-temp-file tmp-script-file
80 (insert full-body))
81 (org-babel-eval (format "jconsole < %s" tmp-script-file) ""))
82 (org-babel-J-eval-string full-body)))))
84 (defun org-babel-J-eval-string (str)
85 "Sends STR to the `j-console-cmd' session and exectues it."
86 (let ((session (j-console-ensure-session)))
87 (with-current-buffer (process-buffer session)
88 (goto-char (point-max))
89 (insert (format "\n%s\n" str))
90 (let ((beg (point)))
91 (comint-send-input)
92 (sit-for .1)
93 (buffer-substring-no-properties
94 beg (point-max))))))
96 (defun org-babel-J-strip-whitespace (str)
97 "Remove whitespace from jconsole output STR."
98 (mapconcat
99 #'identity
100 (delete "" (mapcar
101 #'org-babel-J-print-block
102 (split-string str "^ *,\n" t)))
103 "\n\n"))
105 (defun obj-get-string-alignment (str)
106 "Return a number to describe STR alignment.
107 STR represents a table.
108 Positive/negative/zero result means right/left/undetermined.
109 Don't trust first line."
110 (let* ((str (org-trim str))
111 (lines (split-string str "\n" t))
112 n1 n2)
113 (cond ((<= (length lines) 1)
115 ((= (length lines) 2)
116 ;; numbers are right-aligned
117 (if (and
118 (numberp (read (car lines)))
119 (numberp (read (cadr lines)))
120 (setq n1 (obj-match-second-space-right (nth 0 lines)))
121 (setq n2 (obj-match-second-space-right (nth 1 lines))))
124 ((not (obj-match-second-space-left (nth 0 lines)))
126 ((and
127 (setq n1 (obj-match-second-space-left (nth 1 lines)))
128 (setq n2 (obj-match-second-space-left (nth 2 lines)))
129 (= n1 n2))
131 ((and
132 (setq n1 (obj-match-second-space-right (nth 1 lines)))
133 (setq n2 (obj-match-second-space-right (nth 2 lines)))
134 (= n1 n2))
135 (- n1))
136 (t 0))))
138 (defun org-babel-J-print-block (x)
139 "Prettify jconsole output X."
140 (let* ((x (org-trim x))
141 (a (obj-get-string-alignment x))
142 (lines (split-string x "\n" t))
144 (cond ((minusp a)
145 (setq b (obj-match-second-space-right (nth 0 lines)))
146 (concat (make-string (+ a b) ? ) x))
147 ((plusp a)
148 (setq b (obj-match-second-space-left (nth 0 lines)))
149 (concat (make-string (- a b) ? ) x))
150 (t x))))
152 (defun obj-match-second-space-left (s)
153 "Return position of leftmost space in second space block of S or nil."
154 (and (string-match "^ *[^ ]+\\( \\)" s)
155 (match-beginning 1)))
157 (defun obj-match-second-space-right (s)
158 "Return position of rightmost space in second space block of S or nil."
159 (and (string-match "^ *[^ ]+ *\\( \\)[^ ]" s)
160 (match-beginning 1)))
162 (defun obj-string-match-m (regexp string &optional start)
163 "Call (string-match REGEXP STRING START).
164 REGEXP is modified so that .* matches newlines as well."
165 (string-match
166 (replace-regexp-in-string "\\.\\*" "[\0-\377[:nonascii:]]*" regexp)
167 string
168 start))
170 (defun org-babel-j-initiate-session (&optional session)
171 "Initiate a J session.
172 SESSION is a parameter given by org-babel."
173 (unless (string= session "none")
174 (require 'j-console)
175 (j-console-ensure-session)))
177 (provide 'ob-J)
179 ;;; ob-J.el ends here