mirror of https://github.com/Chizi123/.emacs.d.git

Chizi123
2018-11-18 76bbd07de7add0f9d13c6914f158d19630fe2f62
commit | author | age
76bbd0 1 ;;; ob-J.el --- Babel Functions for J                -*- lexical-binding: t; -*-
C 2
3 ;; Copyright (C) 2011-2018 Free Software Foundation, Inc.
4
5 ;; Author: Oleh Krehel
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: https://orgmode.org
8
9 ;; This file is part of GNU Emacs.
10
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.
15
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.
20
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/>.
23
24 ;;; Commentary:
25
26 ;; Org-Babel support for evaluating J code.
27 ;;
28 ;; Session interaction depends on `j-console' from package `j-mode'
29 ;; (available in MELPA).
30
31 ;;; Code:
32
33 (require 'ob)
34
35 (declare-function org-trim "org" (s &optional keep-lead))
36 (declare-function j-console-ensure-session "ext:j-console" ())
37
38 (defcustom org-babel-J-command "jconsole"
39   "Command to call J."
40   :group 'org-babel
41   :version "26.1"
42   :package-version '(Org . "9.0")
43   :type 'string)
44
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))
49
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"))
53
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))))
60     (concat
61      (if (string= s1 "")
62          ""
63        (concat (org-babel-J-interleave-echos s1)
64            "\necho','\n"))
65      s2
66      "\necho','\n"
67      (org-babel-J-interleave-echos-except-functions s3)))
68     (org-babel-J-interleave-echos body)))
69
70 (defalias 'org-babel-execute:j 'org-babel-execute:J)
71
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")
85      (progn
86        (with-temp-file tmp-script-file
87          (insert full-body))
88        (org-babel-eval (format "%s < %s" org-babel-J-command tmp-script-file) ""))
89        (org-babel-J-eval-string full-body)))))
90
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))
97       (let ((beg (point)))
98     (comint-send-input)
99     (sit-for .1)
100     (buffer-substring-no-properties
101      beg (point-max))))))
102
103 (defun org-babel-J-strip-whitespace (str)
104   "Remove whitespace from jconsole output STR."
105   (mapconcat
106    #'identity
107    (delete "" (mapcar
108            #'org-babel-J-print-block
109            (split-string str "^ *,\n" t)))
110    "\n\n"))
111
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))
119      n1 n2)
120     (cond ((<= (length lines) 1)
121        0)
122       ((= (length lines) 2)
123        ;; numbers are right-aligned
124        (if (and
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))))
129            n2
130          0))
131       ((not (obj-match-second-space-left (nth 0 lines)))
132        0)
133       ((and
134         (setq n1 (obj-match-second-space-left (nth 1 lines)))
135         (setq n2 (obj-match-second-space-left (nth 2 lines)))
136         (= n1 n2))
137        n1)
138       ((and
139         (setq n1 (obj-match-second-space-right (nth 1 lines)))
140         (setq n2 (obj-match-second-space-right (nth 2 lines)))
141         (= n1 n2))
142        (- n1))
143       (t 0))))
144
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))
150      b)
151     (cond ((< a 0)
152        (setq b (obj-match-second-space-right (nth 0 lines)))
153        (concat (make-string (+ a b) ? ) x))
154       ((> a 0)
155        (setq b (obj-match-second-space-left (nth 0 lines)))
156        (concat (make-string (- a b) ? ) x))
157       (t x))))
158
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)))
163
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)))
168
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."
172   (string-match
173    (replace-regexp-in-string "\\.\\*" "[\0-\377[:nonascii:]]*" regexp)
174    string
175    start))
176
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")
181     (require 'j-console)
182     (j-console-ensure-session)))
183
184 (provide 'ob-J)
185
186 ;;; ob-J.el ends here