ob-picolisp.el 7.6 KB

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