1 ;;; org-eval-light.el --- Display result of evaluating code in various languages (light)
3 ;; Copyright (C) 2008-2014 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 ;; This program 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 ;; This program 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. If not, see <http://www.gnu.org/licenses/>.
29 ;; This file is based off of org-eval, with the following changes.
31 ;; 1) forms are only executed manually, (allowing for the execution of
32 ;; an entire subtree of forms)
33 ;; 2) use the org-mode style src blocks, rather than the muse style
34 ;; <code></code> blocks
35 ;; 3) forms are not replaced by their outputs, but rather the output
36 ;; is placed in the buffer immediately following the src block
37 ;; commented by `org-eval-light-make-region-example' (when
38 ;; evaluated with a prefix argument no output is placed in the
40 ;; 4) add defadvice to org-ctrl-c-ctrl-c so that when called inside of
41 ;; a source block it will call `org-eval-light-current-snippet'
46 (defgroup org-eval-light nil
47 "Options concerning including output from commands into the Org-mode buffer."
51 (defvar org-eval-light-example-size-cutoff
10
52 "The number of lines under which an example is considered
53 'small', and is exported with the '^:' syntax instead of in a
56 (defvar org-eval-light-regexp nil
)
58 (defun org-eval-light-set-interpreters (var value
)
59 (set-default var value
)
60 (setq org-eval-light-regexp
61 (concat "#\\+begin_src \\("
62 (mapconcat 'regexp-quote value
"\\|")
63 "\\)\\([^\000]+?\\)#\\+end_src")))
65 (defcustom org-eval-light-interpreters
'("lisp" "emacs-lisp" "ruby" "shell")
66 "Interpreters allows for evaluation tags.
67 This is a list of program names (as strings) that can evaluate code and
68 insert the output into an Org-mode buffer. Valid choices are
70 lisp Interpret Emacs Lisp code and display the result
71 shell Pass command to the shell and display the result
72 perl The perl interpreter
73 python Thy python interpreter
74 ruby The ruby interpreter"
75 :group
'org-eval-light
76 :set
'org-eval-light-set-interpreters
86 (defun org-eval-light-inside-snippet ()
89 (let ((case-fold-search t
)
90 (start-re "^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n")
91 (end-re "\n#\\+end_src")
94 (if (and (setq beg
(re-search-backward start-re nil t
))
95 (setq end
(re-search-forward end-re nil t
))
96 (<= beg pos
) (>= end pos
))
99 (defun org-eval-light-make-region-example (beg end
)
100 "Comment out region using either the '^:' or the BEGIN_EXAMPLE
101 syntax based on the size of the region as compared to
102 `org-eval-light-example-size-cutoff'."
104 (let ((size (abs (- (line-number-at-pos end
)
105 (line-number-at-pos beg
)))))
107 (let ((result (buffer-substring beg end
)))
108 (delete-region beg end
)
109 (insert (concat ": " result
)))
110 (if (<= size org-eval-light-example-size-cutoff
)
114 (move-beginning-of-line 1) (insert ": ") (forward-line 1)))
115 (let ((result (buffer-substring beg end
)))
116 (delete-region beg end
)
117 (insert (concat "#+BEGIN_EXAMPLE\n" result
"#+END_EXAMPLE\n")))))))
119 (defun org-eval-light-current-snippet (&optional arg
)
120 "Execute the current #+begin_src #+end_src block, and dump the
121 results into the buffer immediately following the src block,
122 commented by `org-eval-light-make-region-example'."
124 (let ((line (org-current-line))
126 (info (org-edit-src-find-region-and-lang))
128 (setq beg
(nth 0 info
)
131 (unless (member lang org-eval-light-interpreters
)
132 (error "Language is not in `org-eval-light-interpreters': %s" lang
))
134 (setq result
(org-eval-light-code lang
(buffer-substring beg end
)))
137 (re-search-forward "^#\\+end_src" nil t
) (open-line 1) (forward-char 2)
139 (end (progn (insert result
)
141 (message (format "from %S %S" beg end
))
142 (org-eval-light-make-region-example beg end
))))))
144 (defun org-eval-light-eval-subtree (&optional arg
)
145 "Replace EVAL snippets in the entire subtree."
148 (org-narrow-to-subtree)
149 (goto-char (point-min))
150 (while (re-search-forward org-eval-light-regexp nil t
)
151 (org-eval-light-current-snippet arg
))
154 (defun org-eval-light-code (interpreter code
)
156 ((member interpreter
'("lisp" "emacs-lisp"))
157 (org-eval-light-lisp (concat "(progn\n" code
"\n)")))
158 ((equal interpreter
"shell")
159 (shell-command-to-string code
))
160 ((member interpreter
'("perl" "python" "ruby"))
161 (org-eval-light-run (executable-find interpreter
) code
))
162 (t (error "Cannot evaluate code type %s" interpreter
))))
164 (defun org-eval-light-lisp (form)
165 "Evaluate the given form and return the result as a string."
169 (let ((object (eval (read form
))))
171 ((stringp object
) object
)
173 (not (eq object nil
)))
174 (let ((string (pp-to-string object
)))
175 (substring string
0 (1- (length string
)))))
177 (number-to-string object
))
180 (pp-to-string object
))))
182 (org-display-warning (format "%s: Error evaluating %s: %s"
184 "; INVALID LISP CODE"))))
186 (defun org-eval-light-run (cmd code
)
189 (shell-command-on-region (point-min) (point-max) cmd nil
'replace
)
192 (defadvice org-ctrl-c-ctrl-c
(around org-cc-eval-source activate
)
193 (if (org-eval-light-inside-snippet)
194 (call-interactively 'org-eval-light-current-snippet
)
197 (provide 'org-eval-light
)
199 ;;; org-eval-light.el ends here