org-eval-light.el 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. ;;; org-eval-light.el --- Display result of evaluating code in various languages (light)
  2. ;; Copyright (C) 2008-2013 Free Software Foundation, Inc.
  3. ;; Author: Carsten Dominik <carsten at orgmode dot org>,
  4. ;; Eric Schulte <schulte dot eric at gmail dot com>
  5. ;; Keywords: outlines, hypermedia, calendar, wp, literate programming,
  6. ;; reproducible research
  7. ;; Homepage: http://orgmode.org
  8. ;; Version: 0.04
  9. ;; This file is not yet part of GNU Emacs.
  10. ;; This program is free software; you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 3, or (at your option)
  13. ;; any later version.
  14. ;; This program is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;; GNU General Public License for more details.
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  20. ;;; Commentary:
  21. ;; This file is based off of org-eval, with the following changes.
  22. ;;
  23. ;; 1) forms are only executed manually, (allowing for the execution of
  24. ;; an entire subtree of forms)
  25. ;; 2) use the org-mode style src blocks, rather than the muse style
  26. ;; <code></code> blocks
  27. ;; 3) forms are not replaced by their outputs, but rather the output
  28. ;; is placed in the buffer immediately following the src block
  29. ;; commented by `org-eval-light-make-region-example' (when
  30. ;; evaluated with a prefix argument no output is placed in the
  31. ;; buffer)
  32. ;; 4) add defadvice to org-ctrl-c-ctrl-c so that when called inside of
  33. ;; a source block it will call `org-eval-light-current-snippet'
  34. ;;; Code:
  35. (require 'org)
  36. (defgroup org-eval-light nil
  37. "Options concerning including output from commands into the Org-mode buffer."
  38. :tag "Org Eval"
  39. :group 'org)
  40. (defvar org-eval-light-example-size-cutoff 10
  41. "The number of lines under which an example is considered
  42. 'small', and is exported with the '^:' syntax instead of in a
  43. large example block")
  44. (defvar org-eval-light-regexp nil)
  45. (defun org-eval-light-set-interpreters (var value)
  46. (set-default var value)
  47. (setq org-eval-light-regexp
  48. (concat "#\\+begin_src \\("
  49. (mapconcat 'regexp-quote value "\\|")
  50. "\\)\\([^\000]+?\\)#\\+end_src")))
  51. (defcustom org-eval-light-interpreters '("lisp" "emacs-lisp" "ruby" "shell")
  52. "Interpreters allows for evaluation tags.
  53. This is a list of program names (as strings) that can evaluate code and
  54. insert the output into an Org-mode buffer. Valid choices are
  55. lisp Interpret Emacs Lisp code and display the result
  56. shell Pass command to the shell and display the result
  57. perl The perl interpreter
  58. python Thy python interpreter
  59. ruby The ruby interpreter"
  60. :group 'org-eval-light
  61. :set 'org-eval-light-set-interpreters
  62. :type '(set :greedy t
  63. (const "lisp")
  64. (const "emacs-lisp")
  65. (const "perl")
  66. (const "python")
  67. (const "ruby")
  68. (const "shell")))
  69. ;;; functions
  70. (defun org-eval-light-inside-snippet ()
  71. (interactive)
  72. (save-excursion
  73. (let ((case-fold-search t)
  74. (start-re "^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n")
  75. (end-re "\n#\\+end_src")
  76. (pos (point))
  77. beg end)
  78. (if (and (setq beg (re-search-backward start-re nil t))
  79. (setq end (re-search-forward end-re nil t))
  80. (<= beg pos) (>= end pos))
  81. t))))
  82. (defun org-eval-light-make-region-example (beg end)
  83. "Comment out region using either the '^:' or the BEGIN_EXAMPLE
  84. syntax based on the size of the region as compared to
  85. `org-eval-light-example-size-cutoff'."
  86. (interactive "*r")
  87. (let ((size (abs (- (line-number-at-pos end)
  88. (line-number-at-pos beg)))))
  89. (if (= size 0)
  90. (let ((result (buffer-substring beg end)))
  91. (delete-region beg end)
  92. (insert (concat ": " result)))
  93. (if (<= size org-eval-light-example-size-cutoff)
  94. (save-excursion
  95. (goto-char beg)
  96. (dotimes (n size)
  97. (move-beginning-of-line 1) (insert ": ") (forward-line 1)))
  98. (let ((result (buffer-substring beg end)))
  99. (delete-region beg end)
  100. (insert (concat "#+BEGIN_EXAMPLE\n" result "#+END_EXAMPLE\n")))))))
  101. (defun org-eval-light-current-snippet (&optional arg)
  102. "Execute the current #+begin_src #+end_src block, and dump the
  103. results into the buffer immediately following the src block,
  104. commented by `org-eval-light-make-region-example'."
  105. (interactive "P")
  106. (let ((line (org-current-line))
  107. (case-fold-search t)
  108. (info (org-edit-src-find-region-and-lang))
  109. beg end lang result)
  110. (setq beg (nth 0 info)
  111. end (nth 1 info)
  112. lang (nth 2 info))
  113. (unless (member lang org-eval-light-interpreters)
  114. (error "Language is not in `org-eval-light-interpreters': %s" lang))
  115. (goto-line line)
  116. (setq result (org-eval-light-code lang (buffer-substring beg end)))
  117. (unless arg
  118. (save-excursion
  119. (re-search-forward "^#\\+end_src" nil t) (open-line 1) (forward-char 2)
  120. (let ((beg (point))
  121. (end (progn (insert result)
  122. (point))))
  123. (message (format "from %S %S" beg end))
  124. (org-eval-light-make-region-example beg end))))))
  125. (defun org-eval-light-eval-subtree (&optional arg)
  126. "Replace EVAL snippets in the entire subtree."
  127. (interactive "P")
  128. (save-excursion
  129. (org-narrow-to-subtree)
  130. (goto-char (point-min))
  131. (while (re-search-forward org-eval-light-regexp nil t)
  132. (org-eval-light-current-snippet arg))
  133. (widen)))
  134. (defun org-eval-light-code (interpreter code)
  135. (cond
  136. ((member interpreter '("lisp" "emacs-lisp"))
  137. (org-eval-light-lisp (concat "(progn\n" code "\n)")))
  138. ((equal interpreter "shell")
  139. (shell-command-to-string code))
  140. ((member interpreter '("perl" "python" "ruby"))
  141. (org-eval-light-run (executable-find interpreter) code))
  142. (t (error "Cannot evaluate code type %s" interpreter))))
  143. (defun org-eval-light-lisp (form)
  144. "Evaluate the given form and return the result as a string."
  145. (require 'pp)
  146. (save-match-data
  147. (condition-case err
  148. (let ((object (eval (read form))))
  149. (cond
  150. ((stringp object) object)
  151. ((and (listp object)
  152. (not (eq object nil)))
  153. (let ((string (pp-to-string object)))
  154. (substring string 0 (1- (length string)))))
  155. ((numberp object)
  156. (number-to-string object))
  157. ((eq object nil) "")
  158. (t
  159. (pp-to-string object))))
  160. (error
  161. (org-display-warning (format "%s: Error evaluating %s: %s"
  162. "???" form err))
  163. "; INVALID LISP CODE"))))
  164. (defun org-eval-light-run (cmd code)
  165. (with-temp-buffer
  166. (insert code)
  167. (shell-command-on-region (point-min) (point-max) cmd nil 'replace)
  168. (buffer-string)))
  169. (defadvice org-ctrl-c-ctrl-c (around org-cc-eval-source activate)
  170. (if (org-eval-light-inside-snippet)
  171. (call-interactively 'org-eval-light-current-snippet)
  172. ad-do-it))
  173. (provide 'org-eval-light)
  174. ;;; org-eval-light.el ends here