2015-10-25 20:56:00 -04:00
|
|
|
|
;;; ob-J.el --- Babel Functions for J -*- lexical-binding: t; -*-
|
2013-12-20 08:43:59 -05:00
|
|
|
|
|
2021-01-01 14:55:31 -05:00
|
|
|
|
;; Copyright (C) 2011-2021 Free Software Foundation, Inc.
|
2013-12-20 08:43:59 -05:00
|
|
|
|
|
|
|
|
|
;; Author: Oleh Krehel
|
2020-09-05 02:06:01 -04:00
|
|
|
|
;; Maintainer: Joseph Novakovich <josephnovakovich@gmail.com>
|
2013-12-20 08:43:59 -05:00
|
|
|
|
;; Keywords: literate programming, reproducible research
|
2018-01-16 11:22:00 -05:00
|
|
|
|
;; Homepage: https://orgmode.org
|
2013-12-20 08:43:59 -05:00
|
|
|
|
|
|
|
|
|
;; This file is part of GNU Emacs.
|
|
|
|
|
|
|
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
|
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
;; (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
2017-09-13 18:52:52 -04:00
|
|
|
|
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
|
2013-12-20 08:43:59 -05:00
|
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
2013-12-31 16:46:02 -05:00
|
|
|
|
;; Org-Babel support for evaluating J code.
|
2014-01-02 05:07:27 -05:00
|
|
|
|
;;
|
|
|
|
|
;; Session interaction depends on `j-console' from package `j-mode'
|
|
|
|
|
;; (available in MELPA).
|
2013-12-20 08:43:59 -05:00
|
|
|
|
|
|
|
|
|
;;; Code:
|
2016-09-01 06:14:32 -04:00
|
|
|
|
|
2016-09-14 17:27:17 -04:00
|
|
|
|
(require 'ob)
|
2018-05-09 20:04:12 -04:00
|
|
|
|
(require 'org-macs)
|
2016-09-14 17:27:17 -04:00
|
|
|
|
|
|
|
|
|
(declare-function j-console-ensure-session "ext:j-console" ())
|
2016-09-01 06:14:32 -04:00
|
|
|
|
|
2016-09-14 17:27:17 -04:00
|
|
|
|
(defcustom org-babel-J-command "jconsole"
|
2016-09-01 06:14:32 -04:00
|
|
|
|
"Command to call J."
|
|
|
|
|
:group 'org-babel
|
2017-01-25 23:39:18 -05:00
|
|
|
|
:version "26.1"
|
2016-09-14 17:27:17 -04:00
|
|
|
|
:package-version '(Org . "9.0")
|
2016-09-01 06:14:32 -04:00
|
|
|
|
:type 'string)
|
2013-12-20 08:43:59 -05:00
|
|
|
|
|
2015-10-25 20:56:00 -04:00
|
|
|
|
(defun org-babel-expand-body:J (body _params &optional _processed-params)
|
2013-12-20 08:43:59 -05:00
|
|
|
|
"Expand BODY according to PARAMS, return the expanded body.
|
|
|
|
|
PROCESSED-PARAMS isn't used yet."
|
|
|
|
|
(org-babel-J-interleave-echos-except-functions body))
|
|
|
|
|
|
|
|
|
|
(defun org-babel-J-interleave-echos (body)
|
2014-01-02 05:07:27 -05:00
|
|
|
|
"Interleave echo',' between each source line of BODY."
|
2013-12-31 16:46:02 -05:00
|
|
|
|
(mapconcat #'identity (split-string body "\n") "\necho','\n"))
|
2013-12-20 08:43:59 -05:00
|
|
|
|
|
|
|
|
|
(defun org-babel-J-interleave-echos-except-functions (body)
|
2014-01-02 05:07:27 -05:00
|
|
|
|
"Interleave echo',' between source lines of BODY that aren't functions."
|
2013-12-31 16:46:02 -05:00
|
|
|
|
(if (obj-string-match-m "\\(?:^\\|\n\\)[^\n]*\\(?:0\\|1\\|2\\|3\\|4\\|dyad\\) : 0\n.*\n)\\(?:\n\\|$\\)" body)
|
2013-12-20 08:43:59 -05:00
|
|
|
|
(let ((s1 (substring body 0 (match-beginning 0)))
|
|
|
|
|
(s2 (match-string 0 body))
|
|
|
|
|
(s3 (substring body (match-end 0))))
|
|
|
|
|
(concat
|
2013-12-31 16:46:02 -05:00
|
|
|
|
(if (string= s1 "")
|
|
|
|
|
""
|
|
|
|
|
(concat (org-babel-J-interleave-echos s1)
|
|
|
|
|
"\necho','\n"))
|
2013-12-20 08:43:59 -05:00
|
|
|
|
s2
|
2013-12-31 16:46:02 -05:00
|
|
|
|
"\necho','\n"
|
2013-12-20 08:43:59 -05:00
|
|
|
|
(org-babel-J-interleave-echos-except-functions s3)))
|
|
|
|
|
(org-babel-J-interleave-echos body)))
|
|
|
|
|
|
2016-09-05 05:11:27 -04:00
|
|
|
|
(defalias 'org-babel-execute:j 'org-babel-execute:J)
|
|
|
|
|
|
2013-12-20 08:43:59 -05:00
|
|
|
|
(defun org-babel-execute:J (body params)
|
|
|
|
|
"Execute a block of J code BODY.
|
|
|
|
|
PARAMS are given by org-babel.
|
2019-09-20 18:27:53 -04:00
|
|
|
|
This function is called by `org-babel-execute-src-block'."
|
2013-12-20 08:43:59 -05:00
|
|
|
|
(message "executing J source code block")
|
|
|
|
|
(let* ((processed-params (org-babel-process-params params))
|
2016-09-22 13:45:15 -04:00
|
|
|
|
(sessionp (cdr (assq :session params)))
|
2020-08-31 10:14:15 -04:00
|
|
|
|
(sit-time (let ((sit (assq :sit params)))
|
|
|
|
|
(if sit (cdr sit) .1)))
|
2013-12-20 08:43:59 -05:00
|
|
|
|
(full-body (org-babel-expand-body:J
|
|
|
|
|
body params processed-params))
|
|
|
|
|
(tmp-script-file (org-babel-temp-file "J-src")))
|
2015-10-25 20:56:00 -04:00
|
|
|
|
(org-babel-j-initiate-session sessionp)
|
2013-12-20 08:43:59 -05:00
|
|
|
|
(org-babel-J-strip-whitespace
|
|
|
|
|
(if (string= sessionp "none")
|
|
|
|
|
(progn
|
|
|
|
|
(with-temp-file tmp-script-file
|
|
|
|
|
(insert full-body))
|
2016-09-01 06:14:32 -04:00
|
|
|
|
(org-babel-eval (format "%s < %s" org-babel-J-command tmp-script-file) ""))
|
2020-08-31 10:14:15 -04:00
|
|
|
|
(org-babel-J-eval-string full-body sit-time)))))
|
2013-12-20 08:43:59 -05:00
|
|
|
|
|
2020-08-31 10:14:15 -04:00
|
|
|
|
(defun org-babel-J-eval-string (str sit-time)
|
2017-07-06 03:23:30 -04:00
|
|
|
|
"Sends STR to the `j-console-cmd' session and executes it."
|
2013-12-20 08:43:59 -05:00
|
|
|
|
(let ((session (j-console-ensure-session)))
|
|
|
|
|
(with-current-buffer (process-buffer session)
|
|
|
|
|
(goto-char (point-max))
|
|
|
|
|
(insert (format "\n%s\n" str))
|
|
|
|
|
(let ((beg (point)))
|
|
|
|
|
(comint-send-input)
|
2020-08-31 10:14:15 -04:00
|
|
|
|
(sit-for sit-time)
|
2013-12-20 08:43:59 -05:00
|
|
|
|
(buffer-substring-no-properties
|
|
|
|
|
beg (point-max))))))
|
|
|
|
|
|
|
|
|
|
(defun org-babel-J-strip-whitespace (str)
|
|
|
|
|
"Remove whitespace from jconsole output STR."
|
2013-12-31 16:46:02 -05:00
|
|
|
|
(mapconcat
|
|
|
|
|
#'identity
|
|
|
|
|
(delete "" (mapcar
|
|
|
|
|
#'org-babel-J-print-block
|
|
|
|
|
(split-string str "^ *,\n" t)))
|
|
|
|
|
"\n\n"))
|
|
|
|
|
|
|
|
|
|
(defun obj-get-string-alignment (str)
|
|
|
|
|
"Return a number to describe STR alignment.
|
2014-01-02 05:07:27 -05:00
|
|
|
|
STR represents a table.
|
|
|
|
|
Positive/negative/zero result means right/left/undetermined.
|
2013-12-31 16:46:02 -05:00
|
|
|
|
Don't trust first line."
|
|
|
|
|
(let* ((str (org-trim str))
|
|
|
|
|
(lines (split-string str "\n" t))
|
|
|
|
|
n1 n2)
|
|
|
|
|
(cond ((<= (length lines) 1)
|
|
|
|
|
0)
|
|
|
|
|
((= (length lines) 2)
|
|
|
|
|
;; numbers are right-aligned
|
|
|
|
|
(if (and
|
|
|
|
|
(numberp (read (car lines)))
|
|
|
|
|
(numberp (read (cadr lines)))
|
|
|
|
|
(setq n1 (obj-match-second-space-right (nth 0 lines)))
|
|
|
|
|
(setq n2 (obj-match-second-space-right (nth 1 lines))))
|
|
|
|
|
n2
|
|
|
|
|
0))
|
2013-12-31 16:55:10 -05:00
|
|
|
|
((not (obj-match-second-space-left (nth 0 lines)))
|
2013-12-31 16:46:02 -05:00
|
|
|
|
0)
|
|
|
|
|
((and
|
|
|
|
|
(setq n1 (obj-match-second-space-left (nth 1 lines)))
|
|
|
|
|
(setq n2 (obj-match-second-space-left (nth 2 lines)))
|
|
|
|
|
(= n1 n2))
|
|
|
|
|
n1)
|
|
|
|
|
((and
|
|
|
|
|
(setq n1 (obj-match-second-space-right (nth 1 lines)))
|
|
|
|
|
(setq n2 (obj-match-second-space-right (nth 2 lines)))
|
|
|
|
|
(= n1 n2))
|
|
|
|
|
(- n1))
|
|
|
|
|
(t 0))))
|
2013-12-20 08:43:59 -05:00
|
|
|
|
|
|
|
|
|
(defun org-babel-J-print-block (x)
|
|
|
|
|
"Prettify jconsole output X."
|
2013-12-31 16:46:02 -05:00
|
|
|
|
(let* ((x (org-trim x))
|
|
|
|
|
(a (obj-get-string-alignment x))
|
|
|
|
|
(lines (split-string x "\n" t))
|
|
|
|
|
b)
|
2014-06-01 08:09:39 -04:00
|
|
|
|
(cond ((< a 0)
|
2013-12-31 16:46:02 -05:00
|
|
|
|
(setq b (obj-match-second-space-right (nth 0 lines)))
|
|
|
|
|
(concat (make-string (+ a b) ? ) x))
|
2014-06-01 08:09:39 -04:00
|
|
|
|
((> a 0)
|
2013-12-31 16:46:02 -05:00
|
|
|
|
(setq b (obj-match-second-space-left (nth 0 lines)))
|
|
|
|
|
(concat (make-string (- a b) ? ) x))
|
|
|
|
|
(t x))))
|
|
|
|
|
|
|
|
|
|
(defun obj-match-second-space-left (s)
|
|
|
|
|
"Return position of leftmost space in second space block of S or nil."
|
2013-12-20 08:43:59 -05:00
|
|
|
|
(and (string-match "^ *[^ ]+\\( \\)" s)
|
|
|
|
|
(match-beginning 1)))
|
|
|
|
|
|
2013-12-31 16:46:02 -05:00
|
|
|
|
(defun obj-match-second-space-right (s)
|
|
|
|
|
"Return position of rightmost space in second space block of S or nil."
|
|
|
|
|
(and (string-match "^ *[^ ]+ *\\( \\)[^ ]" s)
|
|
|
|
|
(match-beginning 1)))
|
|
|
|
|
|
2013-12-22 12:48:29 -05:00
|
|
|
|
(defun obj-string-match-m (regexp string &optional start)
|
2014-01-02 05:07:27 -05:00
|
|
|
|
"Call (string-match REGEXP STRING START).
|
|
|
|
|
REGEXP is modified so that .* matches newlines as well."
|
2013-12-22 12:48:29 -05:00
|
|
|
|
(string-match
|
|
|
|
|
(replace-regexp-in-string "\\.\\*" "[\0-\377[:nonascii:]]*" regexp)
|
|
|
|
|
string
|
|
|
|
|
start))
|
|
|
|
|
|
2013-12-20 08:43:59 -05:00
|
|
|
|
(defun org-babel-j-initiate-session (&optional session)
|
|
|
|
|
"Initiate a J session.
|
|
|
|
|
SESSION is a parameter given by org-babel."
|
|
|
|
|
(unless (string= session "none")
|
|
|
|
|
(require 'j-console)
|
|
|
|
|
(j-console-ensure-session)))
|
|
|
|
|
|
|
|
|
|
(provide 'ob-J)
|
|
|
|
|
|
|
|
|
|
;;; ob-J.el ends here
|