ob-J.el 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. ;; Session interaction depends on `j-console'.
  19. ;;; Code:
  20. (require 'ob)
  21. (defun org-babel-expand-body:J (body params &optional processed-params)
  22. "Expand BODY according to PARAMS, return the expanded body.
  23. PROCESSED-PARAMS isn't used yet."
  24. (org-babel-J-interleave-echos-except-functions body))
  25. (defun org-babel-J-interleave-echos (body)
  26. "Interleave echo'' between each source line of BODY."
  27. (mapconcat #'identity (split-string body "\n") "\necho''\n"))
  28. (defun org-babel-J-interleave-echos-except-functions (body)
  29. "Interleave echo'' between source lines of BODY that aren't functions."
  30. (if (string-match-m "\\(?:^\\|\n\\)[^\n]*\\(?:1\\|2\\|3\\|4\\) : 0\n.*)" body)
  31. (let ((s1 (substring body 0 (match-beginning 0)))
  32. (s2 (match-string 0 body))
  33. (s3 (substring body (match-end 0))))
  34. (concat
  35. (org-babel-J-interleave-echos s1)
  36. "\necho''\n"
  37. s2
  38. (org-babel-J-interleave-echos-except-functions s3)))
  39. (org-babel-J-interleave-echos body)))
  40. (defun org-babel-execute:J (body params)
  41. "Execute a block of J code BODY.
  42. PARAMS are given by org-babel.
  43. This function is called by `org-babel-execute-src-block'"
  44. (message "executing J source code block")
  45. (let* ((processed-params (org-babel-process-params params))
  46. (sessionp (cdr (assoc :session params)))
  47. (session (org-babel-j-initiate-session sessionp))
  48. (vars (second processed-params))
  49. (result-params (third processed-params))
  50. (result-type (fourth processed-params))
  51. (full-body (org-babel-expand-body:J
  52. body params processed-params))
  53. (tmp-script-file (org-babel-temp-file "J-src")))
  54. (org-babel-J-strip-whitespace
  55. (if (string= sessionp "none")
  56. (progn
  57. (with-temp-file tmp-script-file
  58. (insert full-body))
  59. (org-babel-eval (format "jconsole < %s" tmp-script-file) ""))
  60. (org-babel-J-eval-string full-body)))))
  61. (defun org-babel-J-eval-string (str)
  62. "Sends STR to the `j-console-cmd' session and exectues it."
  63. (let ((session (j-console-ensure-session)))
  64. (with-current-buffer (process-buffer session)
  65. (goto-char (point-max))
  66. (insert (format "\n%s\n" str))
  67. (let ((beg (point)))
  68. (comint-send-input)
  69. (sit-for .1)
  70. (buffer-substring-no-properties
  71. beg (point-max))))))
  72. (defun org-babel-J-strip-whitespace (str)
  73. "Remove whitespace from jconsole output STR."
  74. (let ((strs (split-string str "\n" t))
  75. out cur s)
  76. (while (setq s (pop strs))
  77. (if (string-match "^ *$" s)
  78. (progn (push (nreverse cur) out)
  79. (setq cur))
  80. (push s cur)))
  81. (mapconcat #'org-babel-J-print-block
  82. (delq nil (nreverse out))
  83. "\n\n")))
  84. (defun org-babel-J-print-block (x)
  85. "Prettify jconsole output X."
  86. (if (= 1 (length x))
  87. (obj-strip-leading-ws (car x))
  88. ;; assume only first row is misaligned
  89. (let ((n1 (obj-match-second-space (car x)))
  90. (n2 (obj-match-second-space (cadr x))))
  91. (setcar
  92. x
  93. (if (and n1 n2)
  94. (substring (car x) (- n1 n2))
  95. (obj-strip-leading-ws (car x))))
  96. (mapconcat #'identity x "\n"))))
  97. (defun obj-match-second-space (s)
  98. "Return position of second space in S or nil."
  99. (and (string-match "^ *[^ ]+\\( \\)" s)
  100. (match-beginning 1)))
  101. (defun obj-strip-leading-ws (s)
  102. "String leading whitespace from S."
  103. (and (string-match "^ *\\([^ ].*\\)" s)
  104. (match-string 1 s)))
  105. (defun org-babel-j-initiate-session (&optional session)
  106. "Initiate a J session.
  107. SESSION is a parameter given by org-babel."
  108. (unless (string= session "none")
  109. (require 'j-console)
  110. (j-console-ensure-session)))
  111. (provide 'ob-J)
  112. ;;; ob-J.el ends here