litorgy.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. ;;; litorgy.el --- literate programming in org-mode
  2. ;; Copyright (C) 2009 Eric Schulte, Dan Davison, Austin F. Frank
  3. ;; Author: Eric Schulte, Dan Davison, Austin F. Frank
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 0.01
  7. ;;; License:
  8. ;; This program 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, or (at your option)
  11. ;; any later version.
  12. ;;
  13. ;; This program is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;;
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  20. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. ;; Boston, MA 02110-1301, USA.
  22. ;;; Commentary:
  23. ;; See rorg.org in this directory for more information
  24. ;;; Code:
  25. (require 'org)
  26. (defun litorgy-execute-src-block-maybe ()
  27. "Detect if this is context for a litorgical src-block and if so
  28. then run `litorgy-execute-src-block'."
  29. (let ((info (litorgy-get-src-block-info)))
  30. (if info (progn (litorgy-execute-src-block nil info) t) nil)))
  31. (add-hook 'org-ctrl-c-ctrl-c-hook 'litorgy-execute-src-block-maybe)
  32. (defvar litorgy-inline-header-args '((:results . "silent") (:exports . "results"))
  33. "Default arguments to use when evaluating an inline source block.")
  34. (defvar litorgy-src-block-regexp nil
  35. "Regexp used to test when inside of a litorgical src-block")
  36. (defvar litorgy-inline-src-block-regexp nil
  37. "Regexp used to test when on an inline litorgical src-block")
  38. (defun litorgy-set-interpreters (var value)
  39. (set-default var value)
  40. (setq litorgy-src-block-regexp
  41. (concat "#\\+begin_src \\("
  42. (mapconcat 'regexp-quote value "\\|")
  43. "\\)"
  44. "\\([ \t]+\\([^\n]+\\)\\)?\n" ;; match header arguments
  45. "\\([^\000]+?\\)#\\+end_src"))
  46. (setq litorgy-inline-src-block-regexp
  47. (concat "src_\\("
  48. (mapconcat 'regexp-quote value "\\|")
  49. "\\)"
  50. "{\\([^\n]+\\)}")))
  51. (defun litorgy-add-interpreter (interpreter)
  52. "Add INTERPRETER to `litorgy-interpreters' and update
  53. `litorgy-src-block-regexp' appropriately."
  54. (unless (member interpreter litorgy-interpreters)
  55. (setq litorgy-interpreters (cons interpreter litorgy-interpreters))
  56. (litorgy-set-interpreters 'litorgy-interpreters litorgy-interpreters)))
  57. (defcustom litorgy-interpreters '()
  58. "Interpreters allows for evaluation tags.
  59. This is a list of program names (as strings) that can evaluate code and
  60. insert the output into an Org-mode buffer. Valid choices are
  61. R Evaluate R code
  62. emacs-lisp Evaluate Emacs Lisp code and display the result
  63. sh Pass command to the shell and display the result
  64. perl The perl interpreter
  65. python The python interpreter
  66. ruby The ruby interpreter
  67. The source block regexp `litorgy-src-block-regexp' is updated
  68. when a new interpreter is added to this list through the
  69. customize interface. To add interpreters to this variable from
  70. lisp code use the `litorgy-add-interpreter' function."
  71. :group 'litorgy
  72. :set 'litorgy-set-interpreters
  73. :type '(set :greedy t
  74. (const "R")
  75. (const "emacs-lisp")
  76. (const "sh")
  77. (const "perl")
  78. (const "python")
  79. (const "ruby")))
  80. ;;; functions
  81. (defun litorgy-execute-src-block (&optional arg info params)
  82. "Execute the current source code block, and dump the results
  83. into the buffer immediately following the block. Results are
  84. commented by `org-toggle-fixed-width-section'. With optional
  85. prefix don't dump results into buffer but rather return the
  86. results in raw elisp (this is useful for automated execution of a
  87. source block).
  88. Optionally supply a value for INFO in the form returned by
  89. `litorgy-get-src-block-info'.
  90. Optionally supply a value for PARAMS which will be merged with
  91. the header arguments specified at the source code block." ; TODO implement!!
  92. (interactive "P")
  93. (let* ((info (or info (litorgy-get-src-block-info)))
  94. (lang (first info))
  95. (body (second info))
  96. (params (org-combine-plists (third info) params))
  97. (cmd (intern (concat "litorgy-execute:" lang)))
  98. result)
  99. (unless (member lang litorgy-interpreters)
  100. (error "Language is not in `litorgy-interpreters': %s" lang))
  101. (setq result (funcall cmd body params))
  102. (if arg
  103. (progn (message (format "%S" result)) result)
  104. (litorgy-insert-result result (cdr (assoc :results params))))))
  105. (defun litorgy-eval-buffer (&optional arg)
  106. "Replace EVAL snippets in the entire buffer."
  107. (interactive "P")
  108. (save-excursion
  109. (goto-char (point-min))
  110. (while (re-search-forward litorgy-regexp nil t)
  111. (litorgy-eval-src-block arg))))
  112. (defun litorgy-eval-subtree (&optional arg)
  113. "Replace EVAL snippets in the entire subtree."
  114. (interactive "P")
  115. (save-excursion
  116. (org-narrow-to-subtree)
  117. (litorgy-eval-buffer)
  118. (widen)))
  119. (defun litorgy-get-src-block-info ()
  120. "Return the information of the current source block as a list
  121. of the following form. (language body header-arguments-alist)"
  122. (let ((case-fold-search t))
  123. (if (save-excursion ;; full source block
  124. (beginning-of-line 1)
  125. (looking-at litorgy-src-block-regexp))
  126. (litorgy-parse-src-block-match)
  127. (if (save-excursion ;; inline source block
  128. (re-search-backward "[ \f\t\n\r\v]" nil t)
  129. (forward-char 1)
  130. (looking-at litorgy-inline-src-block-regexp))
  131. (litorgy-parse-inline-src-block-match)
  132. nil)))) ;; indicate that no source block was found
  133. (defun litorgy-parse-src-block-match ()
  134. (list (litorgy-clean-text-properties (match-string 1))
  135. (litorgy-clean-text-properties (match-string 4))
  136. (litorgy-parse-header-arguments (litorgy-clean-text-properties (or (match-string 3) "")))))
  137. (defun litorgy-parse-inline-src-block-match ()
  138. (list (litorgy-clean-text-properties (match-string 1))
  139. (litorgy-clean-text-properties (match-string 2))
  140. litorgy-inline-header-args))
  141. (defun litorgy-parse-header-arguments (arg-string)
  142. "Parse a string of header arguments returning an alist."
  143. (delq nil
  144. (mapcar
  145. (lambda (arg) (if (string-match "\\([^ \f\t\n\r\v]+\\)[ \f\t\n\r\v]*\\([^ \f\t\n\r\v]+.*\\)" arg)
  146. (cons (intern (concat ":" (match-string 1 arg))) (match-string 2 arg))))
  147. (split-string (concat " " arg-string) "[ \f\t\n\r\v]+:" t))))
  148. (defun litorgy-insert-result (result &optional insert)
  149. "Insert RESULT into the current buffer after the end of the
  150. current source block. With optional argument INSERT controls
  151. insertion of results in the org-mode file. INSERT can take the
  152. following values...
  153. t ------ the default options, simply insert the results after the
  154. source block
  155. replace - insert results after the source block replacing any
  156. previously inserted results
  157. silent -- no results are inserted"
  158. (if (stringp result)
  159. (setq result (litorgy-clean-text-properties result))
  160. (unless (listp result) (setq result (format "%S" result))))
  161. (if (and insert (string-equal insert "replace"))
  162. (litorgy-remove-result (listp result)))
  163. (if (= (length result) 0)
  164. (message "no result returned by source block")
  165. (if (and insert (string-equal insert "silent"))
  166. (message (format "%S" result))
  167. (when (and (stringp result)
  168. (not (or (string-equal (substring result -1) "\n")
  169. (string-equal (substring result -1) "\r"))))
  170. (setq result (concat result "\n")))
  171. (save-excursion
  172. (if (re-search-forward "^#\\+end_src" nil t)
  173. (progn (open-line 1) (forward-char 2))
  174. (progn (open-line 1) (forward-char 1)))
  175. (if (stringp result) ;; assume the result is a table if it's not a string
  176. (litorgy-examplize-region (point) (progn (insert result) (point)))
  177. (progn
  178. (insert
  179. (concat (orgtbl-to-orgtbl
  180. (if (consp (car result)) result (list result))
  181. '(:fmt (lambda (cell) (format "%S" cell)))) "\n"))
  182. (forward-line -1)
  183. (org-cycle))))
  184. (message "finished"))))
  185. (defun litorgy-result-to-org-string (result)
  186. "Return RESULT as a string in org-mode format. This function
  187. relies on `litorgy-insert-result'."
  188. (with-temp-buffer (litorgy-insert-result result) (buffer-string)))
  189. (defun litorgy-remove-result (&optional table)
  190. "Remove the result following the current source block. If
  191. optional argument TABLE is supplied then remove the table
  192. following the block rather than the fixed width example."
  193. (save-excursion
  194. (re-search-forward "^#\\+end_src" nil t)
  195. (forward-char 1)
  196. (delete-region (point)
  197. (save-excursion (forward-line 1)
  198. (if table
  199. (org-table-end)
  200. (while (if (looking-at ": ")
  201. (progn (while (looking-at ": ")
  202. (forward-line 1)) t))
  203. (forward-line 1))
  204. (forward-line -1)
  205. (point))))))
  206. (defun litorgy-examplize-region (beg end)
  207. "Comment out region using the ': ' org example quote."
  208. (interactive "*r")
  209. (let ((size (abs (- (line-number-at-pos end)
  210. (line-number-at-pos beg)))))
  211. (if (= size 0)
  212. (let ((result (buffer-substring beg end)))
  213. (delete-region beg end)
  214. (insert (concat ": " result)))
  215. (save-excursion
  216. (goto-char beg)
  217. (dotimes (n size)
  218. (move-beginning-of-line 1) (insert ": ") (forward-line 1))))))
  219. (defun litorgy-clean-text-properties (text)
  220. "Strip all properties from text return."
  221. (set-text-properties 0 (length text) nil text) text)
  222. (defun litorgy-read (cell)
  223. "Convert the string value of CELL to a number if appropriate.
  224. Otherwise if cell looks like a list (meaning it starts with a
  225. '(') then read it as lisp, otherwise return it unmodified as a
  226. string.
  227. This is taken almost directly from `org-read-prop'."
  228. (if (and (stringp cell) (not (equal cell "")))
  229. (let ((out (string-to-number cell)))
  230. (if (equal out 0)
  231. (if (or (equal "(" (substring cell 0 1))
  232. (equal "'" (substring cell 0 1)))
  233. (read cell)
  234. (if (string-match "^\\(+0\\|-0\\|0\\)$" cell)
  235. 0
  236. (progn (set-text-properties 0 (length cell) nil cell)
  237. cell)))
  238. out))
  239. cell))
  240. (provide 'litorgy)
  241. ;;; litorgy.el ends here