1 ;;; org-eval.el --- Display result of evaluating code in various languanges
2 ;; Copyright (C) 2008 Free Software Foundation, Inc.
4 ;; Author: Carsten Dominik <carsten at orgmode dot org>
5 ;; Keywords: outlines, hypermedia, calendar, wp
6 ;; Homepage: http://orgmode.org
9 ;; This file is not yet part of GNU Emacs.
11 ;; GNU Emacs 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)
16 ;; GNU Emacs 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; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
33 (defgroup org-eval nil
34 "Options concerning global entry identifiers in Org-mode."
39 (org-compatible-face nil
40 '((((class color grayscale
) (min-colors 88) (background light
))
41 (:foreground
"grey20"))
42 (((class color grayscale
) (min-colors 88) (background dark
))
43 (:foreground
"grey80"))
44 (((class color
) (min-colors 8) (background light
))
45 (:foreground
"green"))
46 (((class color
) (min-colors 8) (background dark
))
47 (:foreground
"yellow"))))
48 "Face for fixed-with text like code snippets."
53 (defun org-eval-handle-snippets (limit &optional replace
)
55 (while (setq a
(text-property-any (point) (or limit
(point-max))
57 (remove-text-properties
58 a
(next-single-property-change a
'org-eval nil limit
)
59 '(display t intangible t org-eval t
))))
60 (while (re-search-forward "<\\(lisp\\)>\\([^\000]+?\\)</\\1>" limit t
)
61 (let* ((beg (match-beginning 0))
63 (kind (match-string 1))
64 (code (match-string 2))
65 (value (org-eval-code kind code
)))
67 (replace-match value t t
)
70 (list 'display value
'intangible t
'font-lock-multiline t
74 (defun org-eval-replace-snippts ()
75 "Replace EVAL snippets in the entire buffer.
76 This should go into the `org-export-preprocess-hook'."
77 (goto-char (point-min))
78 (org-eval-handle-snippets nil
'replace
))
80 (add-hook 'org-export-preprocess-hook
'org-eval-replace-snippts
)
81 (add-hook 'org-font-lock-hook
'org-eval-handle-snippets
)
83 (defun org-eval-code (interpreter code
)
85 ((equal interpreter
"lisp")
86 (org-eval-lisp (concat "(progn\n" code
"\n)")))
87 (t (error "Cannot evaluate code type %s" interpreter
))))
89 (defun org-eval-lisp (form)
90 "Evaluate the given form and return the result as a string."
94 (let ((object (eval (read form
))))
96 ((stringp object
) object
)
98 (not (eq object nil
)))
99 (let ((string (pp-to-string object
)))
100 (substring string
0 (1- (length string
)))))
102 (number-to-string object
))
105 (pp-to-string object
))))
107 (org-display-warning (format "%s: Error evaluating %s: %s"
109 "; INVALID LISP CODE"))))
111 (defun org-display-warning (message)
112 "Display the given MESSAGE as a warning."
113 (if (fboundp 'display-warning
)
114 (display-warning 'org message
115 (if (featurep 'xemacs
)
118 (let ((buf (get-buffer-create "*Org warnings*")))
119 (with-current-buffer buf
120 (goto-char (point-max))
121 (insert "Warning (Org): " message
)
129 ;;; org-eval.el ends here