org.el (org-read-date-minibuffer-local-map): Check if we are at the beginning of...
[org-mode.git] / contrib / lisp / org-eval-light.el
blob34a2e99e2241d7cb8ff4f4d6124972dc5fada439
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
10 ;; Version: 0.04
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)
17 ;; any later version.
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/>.
27 ;;; Commentary:
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
39 ;; buffer)
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'
43 ;;; Code:
44 (require 'org)
46 (defgroup org-eval-light nil
47 "Options concerning including output from commands into the Org-mode buffer."
48 :tag "Org Eval"
49 :group 'org)
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
54 large example block")
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
77 :type '(set :greedy t
78 (const "lisp")
79 (const "emacs-lisp")
80 (const "perl")
81 (const "python")
82 (const "ruby")
83 (const "shell")))
85 ;;; functions
86 (defun org-eval-light-inside-snippet ()
87 (interactive)
88 (save-excursion
89 (let ((case-fold-search t)
90 (start-re "^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n")
91 (end-re "\n#\\+end_src")
92 (pos (point))
93 beg end)
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))
97 t))))
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'."
103 (interactive "*r")
104 (let ((size (abs (- (line-number-at-pos end)
105 (line-number-at-pos beg)))))
106 (if (= size 0)
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)
111 (save-excursion
112 (goto-char beg)
113 (dotimes (n size)
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'."
123 (interactive "P")
124 (let ((line (org-current-line))
125 (case-fold-search t)
126 (info (org-edit-src-find-region-and-lang))
127 beg end lang result)
128 (setq beg (nth 0 info)
129 end (nth 1 info)
130 lang (nth 2 info))
131 (unless (member lang org-eval-light-interpreters)
132 (error "Language is not in `org-eval-light-interpreters': %s" lang))
133 (goto-line line)
134 (setq result (org-eval-light-code lang (buffer-substring beg end)))
135 (unless arg
136 (save-excursion
137 (re-search-forward "^#\\+end_src" nil t) (open-line 1) (forward-char 2)
138 (let ((beg (point))
139 (end (progn (insert result)
140 (point))))
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."
146 (interactive "P")
147 (save-excursion
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))
152 (widen)))
154 (defun org-eval-light-code (interpreter code)
155 (cond
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."
166 (require 'pp)
167 (save-match-data
168 (condition-case err
169 (let ((object (eval (read form))))
170 (cond
171 ((stringp object) object)
172 ((and (listp object)
173 (not (eq object nil)))
174 (let ((string (pp-to-string object)))
175 (substring string 0 (1- (length string)))))
176 ((numberp object)
177 (number-to-string object))
178 ((eq object nil) "")
180 (pp-to-string object))))
181 (error
182 (org-display-warning (format "%s: Error evaluating %s: %s"
183 "???" form err))
184 "; INVALID LISP CODE"))))
186 (defun org-eval-light-run (cmd code)
187 (with-temp-buffer
188 (insert code)
189 (shell-command-on-region (point-min) (point-max) cmd nil 'replace)
190 (buffer-string)))
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)
195 ad-do-it))
197 (provide 'org-eval-light)
199 ;;; org-eval-light.el ends here