* ob-vbnet.el: Org-babel functions for VB.Net evaluation
[org-mode/org-tableheadings.git] / lisp / ob-J.el
blobcbcdf3949dd543be73d6ef3c05a13fe326db5d17
1 ;;; ob-J.el --- Babel Functions for J -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2011-2016 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 &optional keep-lead))
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 (full-body (org-babel-expand-body:J
70 body params processed-params))
71 (tmp-script-file (org-babel-temp-file "J-src")))
72 (org-babel-j-initiate-session sessionp)
73 (org-babel-J-strip-whitespace
74 (if (string= sessionp "none")
75 (progn
76 (with-temp-file tmp-script-file
77 (insert full-body))
78 (org-babel-eval (format "jconsole < %s" tmp-script-file) ""))
79 (org-babel-J-eval-string full-body)))))
81 (defun org-babel-J-eval-string (str)
82 "Sends STR to the `j-console-cmd' session and exectues it."
83 (let ((session (j-console-ensure-session)))
84 (with-current-buffer (process-buffer session)
85 (goto-char (point-max))
86 (insert (format "\n%s\n" str))
87 (let ((beg (point)))
88 (comint-send-input)
89 (sit-for .1)
90 (buffer-substring-no-properties
91 beg (point-max))))))
93 (defun org-babel-J-strip-whitespace (str)
94 "Remove whitespace from jconsole output STR."
95 (mapconcat
96 #'identity
97 (delete "" (mapcar
98 #'org-babel-J-print-block
99 (split-string str "^ *,\n" t)))
100 "\n\n"))
102 (defun obj-get-string-alignment (str)
103 "Return a number to describe STR alignment.
104 STR represents a table.
105 Positive/negative/zero result means right/left/undetermined.
106 Don't trust first line."
107 (let* ((str (org-trim str))
108 (lines (split-string str "\n" t))
109 n1 n2)
110 (cond ((<= (length lines) 1)
112 ((= (length lines) 2)
113 ;; numbers are right-aligned
114 (if (and
115 (numberp (read (car lines)))
116 (numberp (read (cadr lines)))
117 (setq n1 (obj-match-second-space-right (nth 0 lines)))
118 (setq n2 (obj-match-second-space-right (nth 1 lines))))
121 ((not (obj-match-second-space-left (nth 0 lines)))
123 ((and
124 (setq n1 (obj-match-second-space-left (nth 1 lines)))
125 (setq n2 (obj-match-second-space-left (nth 2 lines)))
126 (= n1 n2))
128 ((and
129 (setq n1 (obj-match-second-space-right (nth 1 lines)))
130 (setq n2 (obj-match-second-space-right (nth 2 lines)))
131 (= n1 n2))
132 (- n1))
133 (t 0))))
135 (defun org-babel-J-print-block (x)
136 "Prettify jconsole output X."
137 (let* ((x (org-trim x))
138 (a (obj-get-string-alignment x))
139 (lines (split-string x "\n" t))
141 (cond ((< a 0)
142 (setq b (obj-match-second-space-right (nth 0 lines)))
143 (concat (make-string (+ a b) ? ) x))
144 ((> a 0)
145 (setq b (obj-match-second-space-left (nth 0 lines)))
146 (concat (make-string (- a b) ? ) x))
147 (t x))))
149 (defun obj-match-second-space-left (s)
150 "Return position of leftmost space in second space block of S or nil."
151 (and (string-match "^ *[^ ]+\\( \\)" s)
152 (match-beginning 1)))
154 (defun obj-match-second-space-right (s)
155 "Return position of rightmost space in second space block of S or nil."
156 (and (string-match "^ *[^ ]+ *\\( \\)[^ ]" s)
157 (match-beginning 1)))
159 (defun obj-string-match-m (regexp string &optional start)
160 "Call (string-match REGEXP STRING START).
161 REGEXP is modified so that .* matches newlines as well."
162 (string-match
163 (replace-regexp-in-string "\\.\\*" "[\0-\377[:nonascii:]]*" regexp)
164 string
165 start))
167 (defun org-babel-j-initiate-session (&optional session)
168 "Initiate a J session.
169 SESSION is a parameter given by org-babel."
170 (unless (string= session "none")
171 (require 'j-console)
172 (j-console-ensure-session)))
174 (provide 'ob-J)
176 ;;; ob-J.el ends here