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