1 ;;; org-eval-light.el --- Display result of evaluating code in various languages (light)
3 ;; Copyright (C) 2008-2013 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>,
6 ;; Eric Schulte <schulte dot eric at gmail dot com>
7 ;; Keywords: outlines, hypermedia, calendar, wp, literate programming,
8 ;; reproducible research
9 ;; Homepage: http://orgmode.org
12 ;; This file is not yet part of GNU Emacs.
14 ;; GNU Emacs is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 3, or (at your option)
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
27 ;; Boston, MA 02110-1301, USA.
31 ;; This file is based off of org-eval, with the following changes.
33 ;; 1) forms are only executed manually, (allowing for the execution of
34 ;; an entire subtree of forms)
35 ;; 2) use the org-mode style src blocks, rather than the muse style
36 ;; <code></code> blocks
37 ;; 3) forms are not replaced by their outputs, but rather the output
38 ;; is placed in the buffer immediately following the src block
39 ;; commented by `org-eval-light-make-region-example' (when
40 ;; evaluated with a prefix argument no output is placed in the
42 ;; 4) add defadvice to org-ctrl-c-ctrl-c so that when called inside of
43 ;; a source block it will call `org-eval-light-current-snippet'
48 (defgroup org-eval-light nil
49 "Options concerning including output from commands into the Org-mode buffer."
53 (defvar org-eval-light-example-size-cutoff
10
54 "The number of lines under which an example is considered
55 'small', and is exported with the '^:' syntax instead of in a
58 (defvar org-eval-light-regexp nil
)
60 (defun org-eval-light-set-interpreters (var value
)
61 (set-default var value
)
62 (setq org-eval-light-regexp
63 (concat "#\\+begin_src \\("
64 (mapconcat 'regexp-quote value
"\\|")
65 "\\)\\([^\000]+?\\)#\\+end_src")))
67 (defcustom org-eval-light-interpreters
'("lisp" "emacs-lisp" "ruby" "shell")
68 "Interpreters allows for evaluation tags.
69 This is a list of program names (as strings) that can evaluate code and
70 insert the output into an Org-mode buffer. Valid choices are
72 lisp Interpret Emacs Lisp code and display the result
73 shell Pass command to the shell and display the result
74 perl The perl interpreter
75 python Thy python interpreter
76 ruby The ruby interpreter"
77 :group
'org-eval-light
78 :set
'org-eval-light-set-interpreters
88 (defun org-eval-light-inside-snippet ()
91 (let ((case-fold-search t
)
92 (start-re "^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n")
93 (end-re "\n#\\+end_src")
96 (if (and (setq beg
(re-search-backward start-re nil t
))
97 (setq end
(re-search-forward end-re nil t
))
98 (<= beg pos
) (>= end pos
))
101 (defun org-eval-light-make-region-example (beg end
)
102 "Comment out region using either the '^:' or the BEGIN_EXAMPLE
103 syntax based on the size of the region as compared to
104 `org-eval-light-example-size-cutoff'."
106 (let ((size (abs (- (line-number-at-pos end
)
107 (line-number-at-pos beg
)))))
109 (let ((result (buffer-substring beg end
)))
110 (delete-region beg end
)
111 (insert (concat ": " result
)))
112 (if (<= size org-eval-light-example-size-cutoff
)
116 (move-beginning-of-line 1) (insert ": ") (forward-line 1)))
117 (let ((result (buffer-substring beg end
)))
118 (delete-region beg end
)
119 (insert (concat "#+BEGIN_EXAMPLE\n" result
"#+END_EXAMPLE\n")))))))
121 (defun org-eval-light-current-snippet (&optional arg
)
122 "Execute the current #+begin_src #+end_src block, and dump the
123 results into the buffer immediately following the src block,
124 commented by `org-eval-light-make-region-example'."
126 (let ((line (org-current-line))
128 (info (org-edit-src-find-region-and-lang))
130 (setq beg
(nth 0 info
)
133 (unless (member lang org-eval-light-interpreters
)
134 (error "Language is not in `org-eval-light-interpreters': %s" lang
))
136 (setq result
(org-eval-light-code lang
(buffer-substring beg end
)))
139 (re-search-forward "^#\\+end_src" nil t
) (open-line 1) (forward-char 2)
141 (end (progn (insert result
)
143 (message (format "from %S %S" beg end
))
144 (org-eval-light-make-region-example beg end
))))))
146 (defun org-eval-light-eval-subtree (&optional arg
)
147 "Replace EVAL snippets in the entire subtree."
150 (org-narrow-to-subtree)
151 (goto-char (point-min))
152 (while (re-search-forward org-eval-light-regexp nil t
)
153 (org-eval-light-current-snippet arg
))
156 (defun org-eval-light-code (interpreter code
)
158 ((member interpreter
'("lisp" "emacs-lisp"))
159 (org-eval-light-lisp (concat "(progn\n" code
"\n)")))
160 ((equal interpreter
"shell")
161 (shell-command-to-string code
))
162 ((member interpreter
'("perl" "python" "ruby"))
163 (org-eval-light-run (executable-find interpreter
) code
))
164 (t (error "Cannot evaluate code type %s" interpreter
))))
166 (defun org-eval-light-lisp (form)
167 "Evaluate the given form and return the result as a string."
171 (let ((object (eval (read form
))))
173 ((stringp object
) object
)
175 (not (eq object nil
)))
176 (let ((string (pp-to-string object
)))
177 (substring string
0 (1- (length string
)))))
179 (number-to-string object
))
182 (pp-to-string object
))))
184 (org-display-warning (format "%s: Error evaluating %s: %s"
186 "; INVALID LISP CODE"))))
188 (defun org-eval-light-run (cmd code
)
191 (shell-command-on-region (point-min) (point-max) cmd nil
'replace
)
194 (defadvice org-ctrl-c-ctrl-c
(around org-cc-eval-source activate
)
195 (if (org-eval-light-inside-snippet)
196 (call-interactively 'org-eval-light-current-snippet
)
199 (provide 'org-eval-light
)
201 ;;; org-eval-light.el ends here