ob-J.el 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. ;;; ob-J.el --- Babel Functions for J -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2011-2021 Free Software Foundation, Inc.
  3. ;; Author: Oleh Krehel
  4. ;; Maintainer: Joseph Novakovich <josephnovakovich@gmail.com>
  5. ;; Keywords: literate programming, reproducible research
  6. ;; Homepage: https://orgmode.org
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; Org-Babel support for evaluating J code.
  20. ;;
  21. ;; Session interaction depends on `j-console' from package `j-mode'
  22. ;; (available in MELPA).
  23. ;;; Code:
  24. (require 'ob)
  25. (require 'org-macs)
  26. (declare-function j-console-ensure-session "ext:j-console" ())
  27. (defcustom org-babel-J-command "jconsole"
  28. "Command to call J."
  29. :group 'org-babel
  30. :version "26.1"
  31. :package-version '(Org . "9.0")
  32. :type 'string)
  33. (defun org-babel-expand-body:J (body _params &optional _processed-params)
  34. "Expand BODY according to PARAMS, return the expanded body.
  35. PROCESSED-PARAMS isn't used yet."
  36. (org-babel-J-interleave-echos-except-functions body))
  37. (defun org-babel-J-interleave-echos (body)
  38. "Interleave echo',' between each source line of BODY."
  39. (mapconcat #'identity (split-string body "\n") "\necho','\n"))
  40. (defun org-babel-J-interleave-echos-except-functions (body)
  41. "Interleave echo',' between source lines of BODY that aren't functions."
  42. (if (obj-string-match-m "\\(?:^\\|\n\\)[^\n]*\\(?:0\\|1\\|2\\|3\\|4\\|dyad\\) : 0\n.*\n)\\(?:\n\\|$\\)" body)
  43. (let ((s1 (substring body 0 (match-beginning 0)))
  44. (s2 (match-string 0 body))
  45. (s3 (substring body (match-end 0))))
  46. (concat
  47. (if (string= s1 "")
  48. ""
  49. (concat (org-babel-J-interleave-echos s1)
  50. "\necho','\n"))
  51. s2
  52. "\necho','\n"
  53. (org-babel-J-interleave-echos-except-functions s3)))
  54. (org-babel-J-interleave-echos body)))
  55. (defalias 'org-babel-execute:j 'org-babel-execute:J)
  56. (defun org-babel-execute:J (body params)
  57. "Execute a block of J code BODY.
  58. PARAMS are given by org-babel.
  59. This function is called by `org-babel-execute-src-block'."
  60. (message "executing J source code block")
  61. (let* ((processed-params (org-babel-process-params params))
  62. (sessionp (cdr (assq :session params)))
  63. (sit-time (let ((sit (assq :sit params)))
  64. (if sit (cdr sit) .1)))
  65. (full-body (org-babel-expand-body:J
  66. body params processed-params))
  67. (tmp-script-file (org-babel-temp-file "J-src")))
  68. (org-babel-j-initiate-session sessionp)
  69. (org-babel-J-strip-whitespace
  70. (if (string= sessionp "none")
  71. (progn
  72. (with-temp-file tmp-script-file
  73. (insert full-body))
  74. (org-babel-eval (format "%s < %s" org-babel-J-command tmp-script-file) ""))
  75. (org-babel-J-eval-string full-body sit-time)))))
  76. (defun org-babel-J-eval-string (str sit-time)
  77. "Sends STR to the `j-console-cmd' session and executes it."
  78. (let ((session (j-console-ensure-session)))
  79. (with-current-buffer (process-buffer session)
  80. (goto-char (point-max))
  81. (insert (format "\n%s\n" str))
  82. (let ((beg (point)))
  83. (comint-send-input)
  84. (sit-for sit-time)
  85. (buffer-substring-no-properties
  86. beg (point-max))))))
  87. (defun org-babel-J-strip-whitespace (str)
  88. "Remove whitespace from jconsole output STR."
  89. (mapconcat
  90. #'identity
  91. (delete "" (mapcar
  92. #'org-babel-J-print-block
  93. (split-string str "^ *,\n" t)))
  94. "\n\n"))
  95. (defun obj-get-string-alignment (str)
  96. "Return a number to describe STR alignment.
  97. STR represents a table.
  98. Positive/negative/zero result means right/left/undetermined.
  99. Don't trust first line."
  100. (let* ((str (org-trim str))
  101. (lines (split-string str "\n" t))
  102. n1 n2)
  103. (cond ((<= (length lines) 1)
  104. 0)
  105. ((= (length lines) 2)
  106. ;; numbers are right-aligned
  107. (if (and
  108. (numberp (read (car lines)))
  109. (numberp (read (cadr lines)))
  110. (setq n1 (obj-match-second-space-right (nth 0 lines)))
  111. (setq n2 (obj-match-second-space-right (nth 1 lines))))
  112. n2
  113. 0))
  114. ((not (obj-match-second-space-left (nth 0 lines)))
  115. 0)
  116. ((and
  117. (setq n1 (obj-match-second-space-left (nth 1 lines)))
  118. (setq n2 (obj-match-second-space-left (nth 2 lines)))
  119. (= n1 n2))
  120. n1)
  121. ((and
  122. (setq n1 (obj-match-second-space-right (nth 1 lines)))
  123. (setq n2 (obj-match-second-space-right (nth 2 lines)))
  124. (= n1 n2))
  125. (- n1))
  126. (t 0))))
  127. (defun org-babel-J-print-block (x)
  128. "Prettify jconsole output X."
  129. (let* ((x (org-trim x))
  130. (a (obj-get-string-alignment x))
  131. (lines (split-string x "\n" t))
  132. b)
  133. (cond ((< a 0)
  134. (setq b (obj-match-second-space-right (nth 0 lines)))
  135. (concat (make-string (+ a b) ? ) x))
  136. ((> a 0)
  137. (setq b (obj-match-second-space-left (nth 0 lines)))
  138. (concat (make-string (- a b) ? ) x))
  139. (t x))))
  140. (defun obj-match-second-space-left (s)
  141. "Return position of leftmost space in second space block of S or nil."
  142. (and (string-match "^ *[^ ]+\\( \\)" s)
  143. (match-beginning 1)))
  144. (defun obj-match-second-space-right (s)
  145. "Return position of rightmost space in second space block of S or nil."
  146. (and (string-match "^ *[^ ]+ *\\( \\)[^ ]" s)
  147. (match-beginning 1)))
  148. (defun obj-string-match-m (regexp string &optional start)
  149. "Call (string-match REGEXP STRING START).
  150. REGEXP is modified so that .* matches newlines as well."
  151. (string-match
  152. (replace-regexp-in-string "\\.\\*" "[\0-\377[:nonascii:]]*" regexp)
  153. string
  154. start))
  155. (defun org-babel-j-initiate-session (&optional session)
  156. "Initiate a J session.
  157. SESSION is a parameter given by org-babel."
  158. (unless (string= session "none")
  159. (require 'j-console)
  160. (j-console-ensure-session)))
  161. (provide 'ob-J)
  162. ;;; ob-J.el ends here