org-eval.el 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. ;;; org-eval.el --- Display result of evaluating code in various languages
  2. ;; Copyright (C) 2008-2011 Free Software Foundation, Inc.
  3. ;;
  4. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  5. ;; Keywords: outlines, hypermedia, calendar, wp
  6. ;; Homepage: http://orgmode.org
  7. ;; Version: 0.04
  8. ;;
  9. ;; This file is not yet part of GNU Emacs.
  10. ;;
  11. ;; GNU Emacs is free software; you can redistribute it and/or modify
  12. ;; it under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation; either version 3, or (at your option)
  14. ;; any later version.
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;; GNU General Public License for more details.
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  21. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  22. ;; Boston, MA 02110-1301, USA.
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ;;
  25. ;;; Commentary:
  26. ;;
  27. ;; This modules allows to include output from various commands into an
  28. ;; Org-mode buffer, both for live display, and for export.
  29. ;; This technique has been copied from emacs-wiki and Emacs Muse, and
  30. ;; we try to make it work here in a way as similar as possible to
  31. ;; Muse, so that people who move between both worlds don't need to learn
  32. ;; new syntax.
  33. ;;
  34. ;; Basically it works like this:
  35. ;;
  36. ;; <lisp>(concat "aaa" "bbb")</lisp>
  37. ;;
  38. ;; will display "aaabbb" in the buffer and export like that as well.
  39. ;; The leading lisp tag will also accept the attributes "markup" and
  40. ;; "lang", to specify how the text should be formatted during export.
  41. ;; For example,
  42. ;;
  43. ;; <lisp markup="src" lang="emacs-lisp"> .... </lisp>
  44. ;;
  45. ;; will format the result of the lisp form as if it was lisp source
  46. ;; code. Internally, it will wrap the text into a
  47. ;;
  48. ;; #+begin_src emacs-lisp
  49. ;; #+end_src
  50. ;;
  51. ;; structure so that the right things happen when the exporter is running.
  52. ;;
  53. ;; By default, only the <lisp> tag is turned on, but you can configure
  54. ;; the variable `org-eval-interpreters' to add more interpreters like
  55. ;; `perl', `python', or the `shell'.
  56. ;;
  57. ;; You can edit the code snippets with "C-c '" (org-edit-src-code).
  58. ;;
  59. ;; Please note that this mechanism is potentially dangerous, because it
  60. ;; executes code that you don't even see. This gives you great power,
  61. ;; but also enough rope to hang yourself. And, it gives your friends
  62. ;; who send you Org files plenty of opportunity for good and bad jokes.
  63. ;; This is also why this module is not turned on by default, but only
  64. ;; available as a contributed package.
  65. ;;
  66. ;;
  67. ;;
  68. (require 'org)
  69. ;;; Customization
  70. (defgroup org-eval nil
  71. "Options concerning including output from commands into the Org-mode buffer."
  72. :tag "Org Eval"
  73. :group 'org)
  74. (defface org-eval
  75. (org-compatible-face nil
  76. '((((class color grayscale) (min-colors 88) (background light))
  77. (:foreground "grey40"))
  78. (((class color grayscale) (min-colors 88) (background dark))
  79. (:foreground "grey60"))
  80. (((class color) (min-colors 8) (background light))
  81. (:foreground "green"))
  82. (((class color) (min-colors 8) (background dark))
  83. (:foreground "yellow"))))
  84. "Face for command output that is included into an Org-mode buffer."
  85. :group 'org-eval
  86. :group 'org-faces
  87. :version "22.1")
  88. (defvar org-eval-regexp nil)
  89. (defun org-eval-set-interpreters (var value)
  90. (set-default var value)
  91. (setq org-eval-regexp
  92. (concat "<\\("
  93. (mapconcat 'regexp-quote value "\\|")
  94. "\\)"
  95. "\\([^>]\\{0,50\\}?\\)>"
  96. "\\([^\000]+?\\)</\\1>")))
  97. (defcustom org-eval-interpreters '("lisp")
  98. "Interpreters allows for evaluation tags.
  99. This is a list of program names (as strings) that can evaluate code and
  100. insert the output into an Org-mode buffer. Valid choices are
  101. lisp Interpret Emacs Lisp code and display the result
  102. shell Pass command to the shell and display the result
  103. perl The perl interpreter
  104. python Thy python interpreter
  105. ruby The ruby interpreter"
  106. :group 'org-eval
  107. :set 'org-eval-set-interpreters
  108. :type '(set :greedy t
  109. (const "lisp")
  110. (const "perl")
  111. (const "python")
  112. (const "ruby")
  113. (const "shell")))
  114. (defun org-eval-handle-snippets (limit &optional replace)
  115. "Evaluate code snippets and display the results as display property.
  116. When REPLACE is non-nil, replace the code region with the result (used
  117. for export)."
  118. (let (a)
  119. (while (setq a (text-property-any (point) (or limit (point-max))
  120. 'org-eval t))
  121. (remove-text-properties
  122. a (next-single-property-change a 'org-eval nil limit)
  123. '(display t intangible t org-eval t))))
  124. (while (re-search-forward org-eval-regexp limit t)
  125. (let* ((beg (match-beginning 0))
  126. (end (match-end 0))
  127. (kind (match-string 1))
  128. (attr (match-string 2))
  129. (code (match-string 3))
  130. (value (org-eval-code kind code))
  131. markup lang)
  132. (if replace
  133. (progn
  134. (setq attr (save-match-data (org-eval-get-attributes attr))
  135. markup (cdr (assoc "markup" attr))
  136. lang (cdr (assoc "lang" attr)))
  137. (replace-match
  138. (concat (if markup (format "#+BEGIN_%s" (upcase markup)))
  139. (if (and markup (equal (downcase markup) "src"))
  140. (concat " " (or lang "fundamental")))
  141. "\n"
  142. value
  143. (if markup (format "\n#+END_%s\n" (upcase markup))))
  144. t t))
  145. (add-text-properties
  146. beg end
  147. (list 'display value 'intangible t 'font-lock-multiline t
  148. 'face 'org-eval
  149. 'org-eval t))))))
  150. (defun org-eval-replace-snippts ()
  151. "Replace EVAL snippets in the entire buffer.
  152. This should go into the `org-export-preprocess-hook'."
  153. (goto-char (point-min))
  154. (org-eval-handle-snippets nil 'replace))
  155. (add-hook 'org-export-preprocess-hook 'org-eval-replace-snippts)
  156. (add-hook 'org-font-lock-hook 'org-eval-handle-snippets)
  157. (defun org-eval-get-attributes (str)
  158. (let ((start 0) key value rtn)
  159. (while (string-match "\\<\\([a-zA-Z]+\\)\\>=\"\\([^\"]+\\)\"" str start)
  160. (setq key (match-string 1 str)
  161. value (match-string 2 str)
  162. start (match-end 0))
  163. (push (cons key value) rtn))
  164. rtn))
  165. (defun org-eval-code (interpreter code)
  166. (cond
  167. ((equal interpreter "lisp")
  168. (org-eval-lisp (concat "(progn\n" code "\n)")))
  169. ((equal interpreter "shell")
  170. (shell-command-to-string code))
  171. ((member interpreter '("perl" "python" "ruby"))
  172. (org-eval-run (executable-find interpreter) code))
  173. (t (error "Cannot evaluate code type %s" interpreter))))
  174. (defun org-eval-lisp (form)
  175. "Evaluate the given form and return the result as a string."
  176. (require 'pp)
  177. (save-match-data
  178. (condition-case err
  179. (let ((object (eval (read form))))
  180. (cond
  181. ((stringp object) object)
  182. ((and (listp object)
  183. (not (eq object nil)))
  184. (let ((string (pp-to-string object)))
  185. (substring string 0 (1- (length string)))))
  186. ((numberp object)
  187. (number-to-string object))
  188. ((eq object nil) "")
  189. (t
  190. (pp-to-string object))))
  191. (error
  192. (org-display-warning (format "%s: Error evaluating %s: %s"
  193. "???" form err))
  194. "; INVALID LISP CODE"))))
  195. (defun org-eval-run (cmd code)
  196. (with-temp-buffer
  197. (insert code)
  198. (shell-command-on-region (point-min) (point-max) cmd nil 'replace)
  199. (buffer-string)))
  200. (provide 'org-eval)
  201. ;;; org-eval.el ends here