org.el (org-read-date-minibuffer-local-map): Check if we are at the beginning of...
[org-mode.git] / contrib / lisp / org-eval.el
blob6cd7f783d1ee4ffca5be525f60af8b5d017d2cfe
1 ;;; org-eval.el --- Display result of evaluating code in various languages
2 ;; Copyright (C) 2008-2013 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.
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.
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 ;;; Commentary:
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.
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.
41 ;; For example,
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
49 ;; #+end_src
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.
68 (require 'org)
70 ;;; Customization
72 (defgroup org-eval nil
73 "Options concerning including output from commands into the Org-mode buffer."
74 :tag "Org Eval"
75 :group 'org)
77 (defface org-eval
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."
88 :group 'org-eval
89 :group 'org-faces)
91 (defvar org-eval-regexp nil)
93 (defun org-eval-set-interpreters (var value)
94 (set-default var value)
95 (setq org-eval-regexp
96 (concat "<\\("
97 (mapconcat 'regexp-quote value "\\|")
98 "\\)"
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"
112 :group 'org-eval
113 :set 'org-eval-set-interpreters
114 :type '(set :greedy t
115 (const "lisp")
116 (const "perl")
117 (const "python")
118 (const "ruby")
119 (const "shell")))
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
124 for export)."
125 (let (a)
126 (while (setq a (text-property-any (point) (or limit (point-max))
127 'org-eval t))
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))
133 (end (match-end 0))
134 (kind (match-string 1))
135 (attr (match-string 2))
136 (code (match-string 3))
137 (value (org-eval-code kind code))
138 markup lang)
139 (if replace
140 (progn
141 (setq attr (save-match-data (org-eval-get-attributes attr))
142 markup (cdr (assoc "markup" attr))
143 lang (cdr (assoc "lang" attr)))
144 (replace-match
145 (concat (if markup (format "#+BEGIN_%s" (upcase markup)))
146 (if (and markup (equal (downcase markup) "src"))
147 (concat " " (or lang "fundamental")))
148 "\n"
149 value
150 (if markup (format "\n#+END_%s\n" (upcase markup))))
151 t t))
152 (add-text-properties
153 beg end
154 (list 'display value 'intangible t 'font-lock-multiline t
155 'face 'org-eval
156 'org-eval 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)
172 start (match-end 0))
173 (push (cons key value) rtn))
174 rtn))
176 (defun org-eval-code (interpreter code)
177 (cond
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."
188 (require 'pp)
189 (save-match-data
190 (condition-case err
191 (let ((object (eval (read form))))
192 (cond
193 ((stringp object) object)
194 ((and (listp object)
195 (not (eq object nil)))
196 (let ((string (pp-to-string object)))
197 (substring string 0 (1- (length string)))))
198 ((numberp object)
199 (number-to-string object))
200 ((eq object nil) "")
202 (pp-to-string object))))
203 (error
204 (org-display-warning (format "%s: Error evaluating %s: %s"
205 "???" form err))
206 "; INVALID LISP CODE"))))
208 (defun org-eval-run (cmd code)
209 (with-temp-buffer
210 (insert code)
211 (shell-command-on-region (point-min) (point-max) cmd nil 'replace)
212 (buffer-string)))
214 (provide 'org-eval)
216 ;;; org-eval.el ends here