org-eval-light.el 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. ;;; org-eval-light.el --- Display result of evaluating code in various languages (light)
  2. ;; Copyright (C) 2008-2012 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. ;; GNU Emacs 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. ;; GNU Emacs 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; 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. ;; This file is based off of org-eval, with the following changes.
  24. ;;
  25. ;; 1) forms are only executed manually, (allowing for the execution of
  26. ;; an entire subtree of forms)
  27. ;; 2) use the org-mode style src blocks, rather than the muse style
  28. ;; <code></code> blocks
  29. ;; 3) forms are not replaced by their outputs, but rather the output
  30. ;; is placed in the buffer immediately following the src block
  31. ;; commented by `org-eval-light-make-region-example' (when
  32. ;; evaluated with a prefix argument no output is placed in the
  33. ;; buffer)
  34. ;; 4) add defadvice to org-ctrl-c-ctrl-c so that when called inside of
  35. ;; a source block it will call `org-eval-light-current-snippet'
  36. ;;; Code:
  37. (require 'org)
  38. (defgroup org-eval-light nil
  39. "Options concerning including output from commands into the Org-mode buffer."
  40. :tag "Org Eval"
  41. :group 'org)
  42. (defvar org-eval-light-example-size-cutoff 10
  43. "The number of lines under which an example is considered
  44. 'small', and is exported with the '^:' syntax instead of in a
  45. large example block")
  46. (defvar org-eval-light-regexp nil)
  47. (defun org-eval-light-set-interpreters (var value)
  48. (set-default var value)
  49. (setq org-eval-light-regexp
  50. (concat "#\\+begin_src \\("
  51. (mapconcat 'regexp-quote value "\\|")
  52. "\\)\\([^\000]+?\\)#\\+end_src")))
  53. (defcustom org-eval-light-interpreters '("lisp" "emacs-lisp" "ruby" "shell")
  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. lisp Interpret Emacs Lisp code and display the result
  58. shell Pass command to the shell and display the result
  59. perl The perl interpreter
  60. python Thy python interpreter
  61. ruby The ruby interpreter"
  62. :group 'org-eval-light
  63. :set 'org-eval-light-set-interpreters
  64. :type '(set :greedy t
  65. (const "lisp")
  66. (const "emacs-lisp")
  67. (const "perl")
  68. (const "python")
  69. (const "ruby")
  70. (const "shell")))
  71. ;;; functions
  72. (defun org-eval-light-inside-snippet ()
  73. (interactive)
  74. (save-excursion
  75. (let ((case-fold-search t)
  76. (start-re "^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n")
  77. (end-re "\n#\\+end_src")
  78. (pos (point))
  79. beg end)
  80. (if (and (setq beg (re-search-backward start-re nil t))
  81. (setq end (re-search-forward end-re nil t))
  82. (<= beg pos) (>= end pos))
  83. t))))
  84. (defun org-eval-light-make-region-example (beg end)
  85. "Comment out region using either the '^:' or the BEGIN_EXAMPLE
  86. syntax based on the size of the region as compared to
  87. `org-eval-light-example-size-cutoff'."
  88. (interactive "*r")
  89. (let ((size (abs (- (line-number-at-pos end)
  90. (line-number-at-pos beg)))))
  91. (if (= size 0)
  92. (let ((result (buffer-substring beg end)))
  93. (delete-region beg end)
  94. (insert (concat ": " result)))
  95. (if (<= size org-eval-light-example-size-cutoff)
  96. (save-excursion
  97. (goto-char beg)
  98. (dotimes (n size)
  99. (move-beginning-of-line 1) (insert ": ") (forward-line 1)))
  100. (let ((result (buffer-substring beg end)))
  101. (delete-region beg end)
  102. (insert (concat "#+BEGIN_EXAMPLE\n" result "#+END_EXAMPLE\n")))))))
  103. (defun org-eval-light-current-snippet (&optional arg)
  104. "Execute the current #+begin_src #+end_src block, and dump the
  105. results into the buffer immediately following the src block,
  106. commented by `org-eval-light-make-region-example'."
  107. (interactive "P")
  108. (let ((line (org-current-line))
  109. (case-fold-search t)
  110. (info (org-edit-src-find-region-and-lang))
  111. beg end lang result)
  112. (setq beg (nth 0 info)
  113. end (nth 1 info)
  114. lang (nth 2 info))
  115. (unless (member lang org-eval-light-interpreters)
  116. (error "Language is not in `org-eval-light-interpreters': %s" lang))
  117. (goto-line line)
  118. (setq result (org-eval-light-code lang (buffer-substring beg end)))
  119. (unless arg
  120. (save-excursion
  121. (re-search-forward "^#\\+end_src" nil t) (open-line 1) (forward-char 2)
  122. (let ((beg (point))
  123. (end (progn (insert result)
  124. (point))))
  125. (message (format "from %S %S" beg end))
  126. (org-eval-light-make-region-example beg end))))))
  127. (defun org-eval-light-eval-subtree (&optional arg)
  128. "Replace EVAL snippets in the entire subtree."
  129. (interactive "P")
  130. (save-excursion
  131. (org-narrow-to-subtree)
  132. (goto-char (point-min))
  133. (while (re-search-forward org-eval-light-regexp nil t)
  134. (org-eval-light-current-snippet arg))
  135. (widen)))
  136. (defun org-eval-light-code (interpreter code)
  137. (cond
  138. ((member interpreter '("lisp" "emacs-lisp"))
  139. (org-eval-light-lisp (concat "(progn\n" code "\n)")))
  140. ((equal interpreter "shell")
  141. (shell-command-to-string code))
  142. ((member interpreter '("perl" "python" "ruby"))
  143. (org-eval-light-run (executable-find interpreter) code))
  144. (t (error "Cannot evaluate code type %s" interpreter))))
  145. (defun org-eval-light-lisp (form)
  146. "Evaluate the given form and return the result as a string."
  147. (require 'pp)
  148. (save-match-data
  149. (condition-case err
  150. (let ((object (eval (read form))))
  151. (cond
  152. ((stringp object) object)
  153. ((and (listp object)
  154. (not (eq object nil)))
  155. (let ((string (pp-to-string object)))
  156. (substring string 0 (1- (length string)))))
  157. ((numberp object)
  158. (number-to-string object))
  159. ((eq object nil) "")
  160. (t
  161. (pp-to-string object))))
  162. (error
  163. (org-display-warning (format "%s: Error evaluating %s: %s"
  164. "???" form err))
  165. "; INVALID LISP CODE"))))
  166. (defun org-eval-light-run (cmd code)
  167. (with-temp-buffer
  168. (insert code)
  169. (shell-command-on-region (point-min) (point-max) cmd nil 'replace)
  170. (buffer-string)))
  171. (defadvice org-ctrl-c-ctrl-c (around org-cc-eval-source activate)
  172. (if (org-eval-light-inside-snippet)
  173. (call-interactively 'org-eval-light-current-snippet)
  174. ad-do-it))
  175. (provide 'org-eval-light)
  176. ;;; org-eval-light.el ends here