org-eval.el 7.3 KB

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