ob-J.el 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. ;;; ob-J.el --- Babel Functions for J -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2011-2013 Free Software Foundation, Inc.
  3. ;; Author: Oleh Krehel
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://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 <http://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. (declare-function org-trim "org" (S))
  25. (declare-function j-console-ensure-session "ext:j-console" ())
  26. (defun org-babel-expand-body:J (body _params &optional _processed-params)
  27. "Expand BODY according to PARAMS, return the expanded body.
  28. PROCESSED-PARAMS isn't used yet."
  29. (org-babel-J-interleave-echos-except-functions body))
  30. (defun org-babel-J-interleave-echos (body)
  31. "Interleave echo',' between each source line of BODY."
  32. (mapconcat #'identity (split-string body "\n") "\necho','\n"))
  33. (defun org-babel-J-interleave-echos-except-functions (body)
  34. "Interleave echo',' between source lines of BODY that aren't functions."
  35. (if (obj-string-match-m "\\(?:^\\|\n\\)[^\n]*\\(?:0\\|1\\|2\\|3\\|4\\|dyad\\) : 0\n.*\n)\\(?:\n\\|$\\)" body)
  36. (let ((s1 (substring body 0 (match-beginning 0)))
  37. (s2 (match-string 0 body))
  38. (s3 (substring body (match-end 0))))
  39. (concat
  40. (if (string= s1 "")
  41. ""
  42. (concat (org-babel-J-interleave-echos s1)
  43. "\necho','\n"))
  44. s2
  45. "\necho','\n"
  46. (org-babel-J-interleave-echos-except-functions s3)))
  47. (org-babel-J-interleave-echos body)))
  48. (defun org-babel-execute:J (body params)
  49. "Execute a block of J code BODY.
  50. PARAMS are given by org-babel.
  51. This function is called by `org-babel-execute-src-block'"
  52. (message "executing J source code block")
  53. (let* ((processed-params (org-babel-process-params params))
  54. (sessionp (cdr (assoc :session params)))
  55. (full-body (org-babel-expand-body:J
  56. body params processed-params))
  57. (tmp-script-file (org-babel-temp-file "J-src")))
  58. (org-babel-j-initiate-session sessionp)
  59. (org-babel-J-strip-whitespace
  60. (if (string= sessionp "none")
  61. (progn
  62. (with-temp-file tmp-script-file
  63. (insert full-body))
  64. (org-babel-eval (format "jconsole < %s" tmp-script-file) ""))
  65. (org-babel-J-eval-string full-body)))))
  66. (defun org-babel-J-eval-string (str)
  67. "Sends STR to the `j-console-cmd' session and exectues it."
  68. (let ((session (j-console-ensure-session)))
  69. (with-current-buffer (process-buffer session)
  70. (goto-char (point-max))
  71. (insert (format "\n%s\n" str))
  72. (let ((beg (point)))
  73. (comint-send-input)
  74. (sit-for .1)
  75. (buffer-substring-no-properties
  76. beg (point-max))))))
  77. (defun org-babel-J-strip-whitespace (str)
  78. "Remove whitespace from jconsole output STR."
  79. (mapconcat
  80. #'identity
  81. (delete "" (mapcar
  82. #'org-babel-J-print-block
  83. (split-string str "^ *,\n" t)))
  84. "\n\n"))
  85. (defun obj-get-string-alignment (str)
  86. "Return a number to describe STR alignment.
  87. STR represents a table.
  88. Positive/negative/zero result means right/left/undetermined.
  89. Don't trust first line."
  90. (let* ((str (org-trim str))
  91. (lines (split-string str "\n" t))
  92. n1 n2)
  93. (cond ((<= (length lines) 1)
  94. 0)
  95. ((= (length lines) 2)
  96. ;; numbers are right-aligned
  97. (if (and
  98. (numberp (read (car lines)))
  99. (numberp (read (cadr lines)))
  100. (setq n1 (obj-match-second-space-right (nth 0 lines)))
  101. (setq n2 (obj-match-second-space-right (nth 1 lines))))
  102. n2
  103. 0))
  104. ((not (obj-match-second-space-left (nth 0 lines)))
  105. 0)
  106. ((and
  107. (setq n1 (obj-match-second-space-left (nth 1 lines)))
  108. (setq n2 (obj-match-second-space-left (nth 2 lines)))
  109. (= n1 n2))
  110. n1)
  111. ((and
  112. (setq n1 (obj-match-second-space-right (nth 1 lines)))
  113. (setq n2 (obj-match-second-space-right (nth 2 lines)))
  114. (= n1 n2))
  115. (- n1))
  116. (t 0))))
  117. (defun org-babel-J-print-block (x)
  118. "Prettify jconsole output X."
  119. (let* ((x (org-trim x))
  120. (a (obj-get-string-alignment x))
  121. (lines (split-string x "\n" t))
  122. b)
  123. (cond ((< a 0)
  124. (setq b (obj-match-second-space-right (nth 0 lines)))
  125. (concat (make-string (+ a b) ? ) x))
  126. ((> a 0)
  127. (setq b (obj-match-second-space-left (nth 0 lines)))
  128. (concat (make-string (- a b) ? ) x))
  129. (t x))))
  130. (defun obj-match-second-space-left (s)
  131. "Return position of leftmost space in second space block of S or nil."
  132. (and (string-match "^ *[^ ]+\\( \\)" s)
  133. (match-beginning 1)))
  134. (defun obj-match-second-space-right (s)
  135. "Return position of rightmost space in second space block of S or nil."
  136. (and (string-match "^ *[^ ]+ *\\( \\)[^ ]" s)
  137. (match-beginning 1)))
  138. (defun obj-string-match-m (regexp string &optional start)
  139. "Call (string-match REGEXP STRING START).
  140. REGEXP is modified so that .* matches newlines as well."
  141. (string-match
  142. (replace-regexp-in-string "\\.\\*" "[\0-\377[:nonascii:]]*" regexp)
  143. string
  144. start))
  145. (defun org-babel-j-initiate-session (&optional session)
  146. "Initiate a J session.
  147. SESSION is a parameter given by org-babel."
  148. (unless (string= session "none")
  149. (require 'j-console)
  150. (j-console-ensure-session)))
  151. (provide 'ob-J)
  152. ;;; ob-J.el ends here