remove call to bibtex-reformat which often hangs
[org-mode/org-mode-NeilSmithlineMods.git] / lisp / ob-picolisp.el
blob06c9ab8df60afc713f02770b9d1a1777a1a08ede
1 ;;; ob-picolisp.el --- org-babel functions for picolisp evaluation
3 ;; Copyright (C) 2010-2012 Free Software Foundation, Inc.
5 ;; Authors: Thorsten Jolitz
6 ;; Eric Schulte
7 ;; Keywords: literate programming, reproducible research
8 ;; Homepage: http://orgmode.org
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; This library enables the use of PicoLisp in the multi-language
28 ;; programming framework Org-Babel. PicoLisp is a minimal yet
29 ;; fascinating lisp dialect and a highly productive application
30 ;; framework for web-based client-server applications on top of
31 ;; object-oriented databases. A good way to learn PicoLisp is to first
32 ;; read Paul Grahams essay "The hundred year language"
33 ;; (http://www.paulgraham.com/hundred.html) and then study the various
34 ;; documents and essays published in the PicoLisp wiki
35 ;; (http://picolisp.com/5000/-2.html). PicoLisp is included in some
36 ;; GNU/Linux Distributions, and can be downloaded here:
37 ;; http://software-lab.de/down.html. It ships with a picolisp-mode and
38 ;; a inferior-picolisp-mode for Emacs (to be found in the /lib/el/
39 ;; directory).
41 ;; Although it might seem more natural to use Emacs Lisp for most
42 ;; Lisp-based programming tasks inside Org-Mode, an Emacs library
43 ;; written in Emacs Lisp, PicoLisp has at least two outstanding
44 ;; features that make it a valuable addition to Org-Babel:
46 ;; PicoLisp _is_ an object-oriented database with a Prolog-based query
47 ;; language implemented in PicoLisp (Pilog). Database objects are
48 ;; first-class members of the language.
50 ;; PicoLisp is an extremely productive framework for the development
51 ;; of interactive web-applications (on top of a database).
53 ;;; Requirements:
55 ;;; Code:
56 (require 'ob)
57 (require 'ob-eval)
58 (require 'ob-comint)
59 (require 'comint)
60 (eval-when-compile (require 'cl))
62 (declare-function run-picolisp "ext:inferior-picolisp" (cmd))
64 ;; optionally define a file extension for this language
65 (add-to-list 'org-babel-tangle-lang-exts '("picolisp" . "l"))
67 ;;; interferes with settings in org-babel buffer?
68 ;; optionally declare default header arguments for this language
69 ;; (defvar org-babel-default-header-args:picolisp
70 ;; '((:colnames . "no"))
71 ;; "Default arguments for evaluating a picolisp source block.")
73 (defvar org-babel-picolisp-eoe "org-babel-picolisp-eoe"
74 "String to indicate that evaluation has completed.")
76 (defcustom org-babel-picolisp-cmd "pil"
77 "Name of command used to evaluate picolisp blocks."
78 :group 'org-babel
79 :version "24.1"
80 :type 'string)
82 (defun org-babel-expand-body:picolisp (body params &optional processed-params)
83 "Expand BODY according to PARAMS, return the expanded body."
84 (let ((vars (mapcar #'cdr (org-babel-get-header params :var)))
85 (result-params (cdr (assoc :result-params params)))
86 (print-level nil) (print-length nil))
87 (if (> (length vars) 0)
88 (concat "(prog (let ("
89 (mapconcat
90 (lambda (var)
91 (format "%S '%S)"
92 (print (car var))
93 (print (cdr var))))
94 vars "\n ")
95 " \n" body ") )")
96 body)))
98 (defun org-babel-execute:picolisp (body params)
99 "Execute a block of Picolisp code with org-babel. This function is
100 called by `org-babel-execute-src-block'"
101 (message "executing Picolisp source code block")
102 (let* (
103 ;; name of the session or "none"
104 (session-name (cdr (assoc :session params)))
105 ;; set the session if the session variable is non-nil
106 (session (org-babel-picolisp-initiate-session session-name))
107 ;; either OUTPUT or VALUE which should behave as described above
108 (result-type (cdr (assoc :result-type params)))
109 (result-params (cdr (assoc :result-params params)))
110 ;; expand the body with `org-babel-expand-body:picolisp'
111 (full-body (org-babel-expand-body:picolisp body params))
112 ;; wrap body appropriately for the type of evaluation and results
113 (wrapped-body
114 (cond
115 ((or (member "code" result-params)
116 (member "pp" result-params))
117 (format "(pretty (out \"/dev/null\" %s))" full-body))
118 ((and (member "value" result-params) (not session))
119 (format "(print (out \"/dev/null\" %s))" full-body))
120 ((member "value" result-params)
121 (format "(out \"/dev/null\" %s)" full-body))
122 (t full-body))))
124 ((lambda (result)
125 (if (or (member "verbatim" result-params)
126 (member "scalar" result-params)
127 (member "output" result-params)
128 (member "code" result-params)
129 (member "pp" result-params)
130 (= (length result) 0))
131 result
132 (read result)))
133 (if (not (string= session-name "none"))
134 ;; session based evaluation
135 (mapconcat ;; <- joins the list back together into a single string
136 #'identity
137 (butlast ;; <- remove the org-babel-picolisp-eoe line
138 (delq nil
139 (mapcar
140 (lambda (line)
141 (org-babel-chomp ;; remove trailing newlines
142 (when (> (length line) 0) ;; remove empty lines
143 (cond
144 ;; remove leading "-> " from return values
145 ((and (>= (length line) 3)
146 (string= "-> " (substring line 0 3)))
147 (substring line 3))
148 ;; remove trailing "-> <<return-value>>" on the
149 ;; last line of output
150 ((and (member "output" result-params)
151 (string-match-p "->" line))
152 (substring line 0 (string-match "->" line)))
153 (t line)
155 ;; (if (and (>= (length line) 3) ;; remove leading "<- "
156 ;; (string= "-> " (substring line 0 3)))
157 ;; (substring line 3)
158 ;; line)
160 ;; returns a list of the output of each evaluated expression
161 (org-babel-comint-with-output (session org-babel-picolisp-eoe)
162 (insert wrapped-body) (comint-send-input)
163 (insert "'" org-babel-picolisp-eoe) (comint-send-input)))))
164 "\n")
165 ;; external evaluation
166 (let ((script-file (org-babel-temp-file "picolisp-script-")))
167 (with-temp-file script-file
168 (insert (concat wrapped-body "(bye)")))
169 (org-babel-eval
170 (format "%s %s"
171 org-babel-picolisp-cmd
172 (org-babel-process-file-name script-file))
173 ""))))))
175 (defun org-babel-picolisp-initiate-session (&optional session-name)
176 "If there is not a current inferior-process-buffer in SESSION
177 then create. Return the initialized session."
178 (unless (string= session-name "none")
179 (require 'inferior-picolisp)
180 ;; provide a reasonable default session name
181 (let ((session (or session-name "*inferior-picolisp*")))
182 ;; check if we already have a live session by this name
183 (if (org-babel-comint-buffer-livep session)
184 (get-buffer session)
185 (save-window-excursion
186 (run-picolisp org-babel-picolisp-cmd)
187 (rename-buffer session-name)
188 (current-buffer))))))
190 (provide 'ob-picolisp)
194 ;;; ob-picolisp.el ends here