litorgy.el 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. ;;; litorgy.el --- literate programing 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 ((case-fold-search t))
  30. (if (save-excursion
  31. (beginning-of-line 1)
  32. (looking-at litorgy-src-block-regexp))
  33. (progn (call-interactively 'litorgy-execute-src-block)
  34. t) ;; to signal that we took action
  35. nil))) ;; to signal that we did not
  36. (add-hook 'org-ctrl-c-ctrl-c-hook 'litorgy-execute-src-block-maybe)
  37. (defvar litorgy-src-block-regexp nil
  38. "Regexp used to test when inside of a litorgical src-block")
  39. (defun litorgy-set-interpreters (var value)
  40. (set-default var value)
  41. (setq litorgy-src-block-regexp
  42. (concat "#\\+begin_src \\("
  43. (mapconcat 'regexp-quote value "\\|")
  44. "\\)"
  45. "\\([ \t]+\\([^\n]+\\)\\)?\n" ;; match header arguments
  46. "\\([^\000]+?\\)#\\+end_src")))
  47. (defun litorgy-add-interpreter (interpreter)
  48. "Add INTERPRETER to `litorgy-interpreters' and update
  49. `litorgy-src-block-regexp' appropriately."
  50. (unless (member interpreter litorgy-interpreters)
  51. (setq litorgy-interpreters (cons interpreter litorgy-interpreters))
  52. (litorgy-set-interpreters 'litorgy-interpreters litorgy-interpreters)))
  53. (defcustom litorgy-interpreters '()
  54. "Interpreters allows for evaluation tags.
  55. This is a list of program names (as strings) that can evaluate code and
  56. insert the output into an Org-mode buffer. Valid choices are
  57. R Evaluate R code
  58. emacs-lisp Evaluate Emacs Lisp code and display the result
  59. sh Pass command to the shell and display the result
  60. perl The perl interpreter
  61. python The python interpreter
  62. ruby The ruby interpreter
  63. The source block regexp `litorgy-src-block-regexp' is updated
  64. when a new interpreter is added to this list through the
  65. customize interface. To add interpreters to this variable from
  66. lisp code use the `litorgy-add-interpreter' function."
  67. :group 'litorgy
  68. :set 'litorgy-set-interpreters
  69. :type '(set :greedy t
  70. (const "R")
  71. (const "emacs-lisp")
  72. (const "sh")
  73. (const "perl")
  74. (const "python")
  75. (const "ruby")))
  76. ;;; functions
  77. (defun litorgy-execute-src-block (&optional arg)
  78. "Execute the current source code block, and dump the results
  79. into the buffer immediately following the block. Results are
  80. commented by `org-toggle-fixed-width-section'. With optional
  81. prefix don't dump results into buffer."
  82. (interactive "P")
  83. (let* ((info (litorgy-get-src-block-info))
  84. (lang (first info))
  85. (body (second info))
  86. (params (third info))
  87. (cmd (intern (concat "litorgy-execute:" lang)))
  88. result)
  89. (unless (member lang litorgy-interpreters)
  90. (error "Language is not in `litorgy-interpreters': %s" lang))
  91. (setq result (funcall cmd body params))
  92. (if arg
  93. (message (format "%S" result))
  94. (litorgy-insert-result result (assoc :replace params)))))
  95. (defun litorgy-eval-buffer (&optional arg)
  96. "Replace EVAL snippets in the entire buffer."
  97. (interactive "P")
  98. (save-excursion
  99. (goto-char (point-min))
  100. (while (re-search-forward litorgy-regexp nil t)
  101. (litorgy-eval-src-block arg))))
  102. (defun litorgy-eval-subtree (&optional arg)
  103. "Replace EVAL snippets in the entire subtree."
  104. (interactive "P")
  105. (save-excursion
  106. (org-narrow-to-subtree)
  107. (litorgy-eval-buffer)
  108. (widen)))
  109. (defun litorgy-get-src-block-info ()
  110. "Return the information of the current source block (the point
  111. should be on the '#+begin_src' line) as a list of the following
  112. form. (language body header-arguments-alist)"
  113. (unless (save-excursion
  114. (beginning-of-line 1)
  115. (looking-at litorgy-src-block-regexp))
  116. (error "not looking at src-block"))
  117. (let ((lang (litorgy-clean-text-properties (match-string 1)))
  118. (args (litorgy-clean-text-properties (or (match-string 3) "")))
  119. (body (litorgy-clean-text-properties (match-string 4))))
  120. (list lang body (litorgy-parse-header-arguments args))))
  121. (defun litorgy-parse-header-arguments (arg-string)
  122. "Parse a string of header arguments returning an alist."
  123. (delq nil
  124. (mapcar
  125. (lambda (arg) (if (string-match "\\([^ \f\t\n\r\v]+\\)[ \f\t\n\r\v]*\\([^ \f\t\n\r\v]*\\)" arg)
  126. (cons (intern (concat ":" (match-string 1 arg))) (match-string 2 arg))))
  127. (split-string (concat " " arg-string) "[ \f\t\n\r\v]+:"))))
  128. (defun litorgy-insert-result (result &optional replace)
  129. "Insert RESULT into the current buffer after the end of the
  130. current source block. With optional argument REPLACE replace any
  131. existing results currently located after the source block."
  132. (if replace (litorgy-remove-result (listp result)))
  133. (when (and (stringp result)
  134. (not (or (string-equal (substring result -1) "\n")
  135. (string-equal (substring result -1) "\r"))))
  136. (setq result (concat result "\n")))
  137. (save-excursion
  138. (re-search-forward "^#\\+end_src" nil t) (open-line 1) (forward-char 2)
  139. (if (stringp result)
  140. (litorgy-examplize-region (point) (progn (insert result) (point)))
  141. (progn
  142. (insert ;; for now lets assume the result is a table if it's not a string
  143. (concat (orgtbl-to-orgtbl
  144. (if (consp (car result)) result (list result))
  145. '(:fmt (lambda (cell) (format "%S" cell)))) "\n"))
  146. (forward-line -1)
  147. (org-cycle)))))
  148. (defun litorgy-remove-result (&optional table)
  149. "Remove the result following the current source block. If
  150. optional argument TABLE is supplied then remove the table
  151. following the block rather than the fixed width example."
  152. (save-excursion
  153. (re-search-forward "^#\\+end_src" nil t)
  154. (forward-char 1)
  155. (delete-region (point)
  156. (save-excursion (forward-line 1)
  157. (if table
  158. (org-table-end)
  159. (while (if (looking-at ": ")
  160. (progn (while (looking-at ": ")
  161. (forward-line 1)) t))
  162. (forward-line 1))
  163. (forward-line -1)
  164. (point))))))
  165. (defun litorgy-examplize-region (beg end)
  166. "Comment out region using the ': ' org example quote."
  167. (interactive "*r")
  168. (let ((size (abs (- (line-number-at-pos end)
  169. (line-number-at-pos beg)))))
  170. (if (= size 0)
  171. (let ((result (buffer-substring beg end)))
  172. (delete-region beg end)
  173. (insert (concat ": " result)))
  174. (save-excursion
  175. (goto-char beg)
  176. (dotimes (n size)
  177. (move-beginning-of-line 1) (insert ": ") (forward-line 1))))))
  178. (defun litorgy-clean-text-properties (text)
  179. "Strip all properties from text return."
  180. (set-text-properties 0 (length text) nil text) text)
  181. (provide 'litorgy)
  182. ;;; litorgy.el ends here