1 ;;; org-eval.el --- Display result of evaluating code in various languages
2 ;; Copyright (C) 2008-2018 Free Software Foundation, Inc.
4 ;; Author: Carsten Dominik <carsten at orgmode dot org>
5 ;; Keywords: outlines, hypermedia, calendar, wp
6 ;; Homepage: https://orgmode.org
9 ;; This file is not yet part of GNU Emacs.
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)
16 ;; This program is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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
34 ;; Basically it works like this:
36 ;; <lisp>(concat "aaa" "bbb")</lisp>
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.
43 ;; <lisp markup="src" lang="emacs-lisp"> .... </lisp>
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
48 ;; #+begin_src emacs-lisp
51 ;; structure so that the right things happen when the exporter is running.
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'.
57 ;; You can edit the code snippets with "C-c '" (org-edit-src-code).
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.
72 (defgroup org-eval nil
73 "Options concerning including output from commands into the Org-mode buffer."
78 (org-compatible-face nil
79 '((((class color grayscale
) (min-colors 88) (background light
))
80 (:foreground
"grey40"))
81 (((class color grayscale
) (min-colors 88) (background dark
))
82 (:foreground
"grey60"))
83 (((class color
) (min-colors 8) (background light
))
84 (:foreground
"green"))
85 (((class color
) (min-colors 8) (background dark
))
86 (:foreground
"yellow"))))
87 "Face for command output that is included into an Org-mode buffer."
91 (defvar org-eval-regexp nil
)
93 (defun org-eval-set-interpreters (var value
)
94 (set-default var value
)
97 (mapconcat 'regexp-quote value
"\\|")
99 "\\([^>]\\{0,50\\}?\\)>"
100 "\\([^\000]+?\\)</\\1>")))
102 (defcustom org-eval-interpreters
'("lisp")
103 "Interpreters allows for evaluation tags.
104 This is a list of program names (as strings) that can evaluate code and
105 insert the output into an Org-mode buffer. Valid choices are
107 lisp Interpret Emacs Lisp code and display the result
108 shell Pass command to the shell and display the result
109 perl The perl interpreter
110 python Thy python interpreter
111 ruby The ruby interpreter"
113 :set
'org-eval-set-interpreters
114 :type
'(set :greedy t
121 (defun org-eval-handle-snippets (limit &optional replace
)
122 "Evaluate code snippets and display the results as display property.
123 When REPLACE is non-nil, replace the code region with the result (used
126 (while (setq a
(text-property-any (point) (or limit
(point-max))
128 (remove-text-properties
129 a
(next-single-property-change a
'org-eval nil limit
)
130 '(display t intangible t org-eval t
))))
131 (while (re-search-forward org-eval-regexp limit t
)
132 (let* ((beg (match-beginning 0))
134 (kind (match-string 1))
135 (attr (match-string 2))
136 (code (match-string 3))
137 (value (org-eval-code kind code
))
141 (setq attr
(save-match-data (org-eval-get-attributes attr
))
142 markup
(cdr (assoc "markup" attr
))
143 lang
(cdr (assoc "lang" attr
)))
145 (concat (if markup
(format "#+BEGIN_%s" (upcase markup
)))
146 (if (and markup
(equal (downcase markup
) "src"))
147 (concat " " (or lang
"fundamental")))
150 (if markup
(format "\n#+END_%s\n" (upcase markup
))))
154 (list 'display value
'intangible t
'font-lock-multiline t
158 (defun org-eval-replace-snippts ()
159 "Replace EVAL snippets in the entire buffer.
160 This should go into the `org-export-preprocess-hook'."
161 (goto-char (point-min))
162 (org-eval-handle-snippets nil
'replace
))
164 (add-hook 'org-export-preprocess-hook
'org-eval-replace-snippts
)
165 (add-hook 'org-font-lock-hook
'org-eval-handle-snippets
)
167 (defun org-eval-get-attributes (str)
168 (let ((start 0) key value rtn
)
169 (while (string-match "\\<\\([a-zA-Z]+\\)\\>=\"\\([^\"]+\\)\"" str start
)
170 (setq key
(match-string 1 str
)
171 value
(match-string 2 str
)
173 (push (cons key value
) rtn
))
176 (defun org-eval-code (interpreter code
)
178 ((equal interpreter
"lisp")
179 (org-eval-lisp (concat "(progn\n" code
"\n)")))
180 ((equal interpreter
"shell")
181 (shell-command-to-string code
))
182 ((member interpreter
'("perl" "python" "ruby"))
183 (org-eval-run (executable-find interpreter
) code
))
184 (t (error "Cannot evaluate code type %s" interpreter
))))
186 (defun org-eval-lisp (form)
187 "Evaluate the given form and return the result as a string."
191 (let ((object (eval (read form
))))
193 ((stringp object
) object
)
195 (not (eq object nil
)))
196 (let ((string (pp-to-string object
)))
197 (substring string
0 (1- (length string
)))))
199 (number-to-string object
))
202 (pp-to-string object
))))
204 (org-display-warning (format "%s: Error evaluating %s: %s"
206 "; INVALID LISP CODE"))))
208 (defun org-eval-run (cmd code
)
211 (shell-command-on-region (point-min) (point-max) cmd nil
'replace
)
216 ;;; org-eval.el ends here