ob-J.el 5.6 KB

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