org-lparse.el: Fix issues with formatting of custom links
[org-mode.git] / contrib / lisp / org-eval-light.el
blob097fd44a12decb0a7fb7ae521dac701e1ca65c42
1 ;;; org-eval-light.el --- Display result of evaluating code in various languages (light)
3 ;; Copyright (C) 2008-2011 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 ;; 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)
17 ;; any later version.
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.
29 ;;; Commentary:
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
41 ;; buffer)
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'
45 ;;; Code:
46 (require 'org)
48 (defgroup org-eval-light nil
49 "Options concerning including output from commands into the Org-mode buffer."
50 :tag "Org Eval"
51 :group 'org)
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
56 large example block")
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
79 :type '(set :greedy t
80 (const "lisp")
81 (const "emacs-lisp")
82 (const "perl")
83 (const "python")
84 (const "ruby")
85 (const "shell")))
87 ;;; functions
88 (defun org-eval-light-inside-snippet ()
89 (interactive)
90 (save-excursion
91 (let ((case-fold-search t)
92 (start-re "^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n")
93 (end-re "\n#\\+end_src")
94 (pos (point))
95 beg end)
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))
99 t))))
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'."
105 (interactive "*r")
106 (let ((size (abs (- (line-number-at-pos end)
107 (line-number-at-pos beg)))))
108 (if (= size 0)
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)
113 (save-excursion
114 (goto-char beg)
115 (dotimes (n size)
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'."
125 (interactive "P")
126 (let ((line (org-current-line))
127 (case-fold-search t)
128 (info (org-edit-src-find-region-and-lang))
129 beg end lang result)
130 (setq beg (nth 0 info)
131 end (nth 1 info)
132 lang (nth 2 info))
133 (unless (member lang org-eval-light-interpreters)
134 (error "Language is not in `org-eval-light-interpreters': %s" lang))
135 (goto-line line)
136 (setq result (org-eval-light-code lang (buffer-substring beg end)))
137 (unless arg
138 (save-excursion
139 (re-search-forward "^#\\+end_src" nil t) (open-line 1) (forward-char 2)
140 (let ((beg (point))
141 (end (progn (insert result)
142 (point))))
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."
148 (interactive "P")
149 (save-excursion
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))
154 (widen)))
156 (defun org-eval-light-code (interpreter code)
157 (cond
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."
168 (require 'pp)
169 (save-match-data
170 (condition-case err
171 (let ((object (eval (read form))))
172 (cond
173 ((stringp object) object)
174 ((and (listp object)
175 (not (eq object nil)))
176 (let ((string (pp-to-string object)))
177 (substring string 0 (1- (length string)))))
178 ((numberp object)
179 (number-to-string object))
180 ((eq object nil) "")
182 (pp-to-string object))))
183 (error
184 (org-display-warning (format "%s: Error evaluating %s: %s"
185 "???" form err))
186 "; INVALID LISP CODE"))))
188 (defun org-eval-light-run (cmd code)
189 (with-temp-buffer
190 (insert code)
191 (shell-command-on-region (point-min) (point-max) cmd nil 'replace)
192 (buffer-string)))
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)
197 ad-do-it))
199 (provide 'org-eval-light)
201 ;;; org-eval-light.el ends here