ob-picolisp.el 7.8 KB

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