ob-J.el 5.9 KB

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