ob-picolisp.el 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. ;;; ob-picolisp.el --- org-babel functions for picolisp evaluation
  2. ;; Copyright (C) 2010-2011 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. ;; optionally define a file extension for this language
  45. (add-to-list 'org-babel-tangle-lang-exts '("picolisp" . "l"))
  46. ;;; interferes with settings in org-babel buffer?
  47. ;; optionally declare default header arguments for this language
  48. ;; (defvar org-babel-default-header-args:picolisp
  49. ;; '((:colnames . "no"))
  50. ;; "Default arguments for evaluating a picolisp source block.")
  51. (defvar org-babel-picolisp-eoe "org-babel-picolisp-eoe"
  52. "String to indicate that evaluation has completed.")
  53. (defcustom org-babel-picolisp-cmd "pil"
  54. "Name of command used to evaluate picolisp blocks."
  55. :group 'org-babel
  56. :type 'string)
  57. (defun org-babel-expand-body:picolisp (body params &optional processed-params)
  58. "Expand BODY according to PARAMS, return the expanded body."
  59. (let ((vars (mapcar #'cdr (org-babel-get-header params :var)))
  60. (result-params (cdr (assoc :result-params params)))
  61. (print-level nil) (print-length nil))
  62. (if (> (length vars) 0)
  63. (concat "(prog (let ("
  64. (mapconcat
  65. (lambda (var)
  66. (format "%S '%S)"
  67. (print (car var))
  68. (print (cdr var))))
  69. vars "\n ")
  70. " \n" body ") )")
  71. body)))
  72. (defun org-babel-execute:picolisp (body params)
  73. "Execute a block of Picolisp code with org-babel. This function is
  74. called by `org-babel-execute-src-block'"
  75. (message "executing Picolisp source code block")
  76. (let* (
  77. ;; name of the session or "none"
  78. (session-name (cdr (assoc :session params)))
  79. ;; set the session if the session variable is non-nil
  80. (session (org-babel-picolisp-initiate-session session-name))
  81. ;; either OUTPUT or VALUE which should behave as described above
  82. (result-type (cdr (assoc :result-type params)))
  83. ;; expand the body with `org-babel-expand-body:picolisp'
  84. (full-body (org-babel-expand-body:picolisp body params))
  85. ;; wrap body appropriately for the type of evaluation and results
  86. (wrapped-body
  87. (cond
  88. ((or (member "code" result-params)
  89. (member "pp" result-params))
  90. (format "(pretty (out \"/dev/null\" %s))" full-body))
  91. ((and (member "value" result-params) (not session))
  92. (format "(print (out \"/dev/null\" %s))" full-body))
  93. ((member "value" result-params)
  94. (format "(out \"/dev/null\" %s)" full-body))
  95. (t full-body))))
  96. ((lambda (result)
  97. (if (or (member "verbatim" result-params)
  98. (member "scalar" result-params)
  99. (member "output" result-params)
  100. (member "code" result-params)
  101. (member "pp" result-params)
  102. (= (length result) 0))
  103. result
  104. (read result)))
  105. (if (not (string= session-name "none"))
  106. ;; session based evaluation
  107. (mapconcat ;; <- joins the list back together into a single string
  108. #'identity
  109. (butlast ;; <- remove the org-babel-picolisp-eoe line
  110. (delq nil
  111. (mapcar
  112. (lambda (line)
  113. (org-babel-chomp ;; remove trailing newlines
  114. (when (> (length line) 0) ;; remove empty lines
  115. (cond
  116. ;; remove leading "-> " from return values
  117. ((and (>= (length line) 3)
  118. (string= "-> " (subseq line 0 3)))
  119. (subseq line 3))
  120. ;; remove trailing "-> <<return-value>>" on the
  121. ;; last line of output
  122. ((and (member "output" result-params)
  123. (string-match-p "->" line))
  124. (subseq line 0 (string-match "->" line)))
  125. (t line)
  126. )
  127. ;; (if (and (>= (length line) 3) ;; remove leading "<- "
  128. ;; (string= "-> " (subseq line 0 3)))
  129. ;; (subseq line 3)
  130. ;; line)
  131. )))
  132. ;; returns a list of the output of each evaluated expression
  133. (org-babel-comint-with-output (session org-babel-picolisp-eoe)
  134. (insert wrapped-body) (comint-send-input)
  135. (insert "'" org-babel-picolisp-eoe) (comint-send-input)))))
  136. "\n")
  137. ;; external evaluation
  138. (let ((script-file (org-babel-temp-file "picolisp-script-")))
  139. (with-temp-file script-file
  140. (insert (concat wrapped-body "(bye)")))
  141. (org-babel-eval
  142. (format "%s %s"
  143. org-babel-picolisp-cmd
  144. (org-babel-process-file-name script-file))
  145. ""))))))
  146. (defun org-babel-picolisp-initiate-session (&optional session-name)
  147. "If there is not a current inferior-process-buffer in SESSION
  148. then create. Return the initialized session."
  149. (unless (string= session-name "none")
  150. (require 'inferior-picolisp)
  151. ;; provide a reasonable default session name
  152. (let ((session (or session-name "*inferior-picolisp*")))
  153. ;; check if we already have a live session by this name
  154. (if (org-babel-comint-buffer-livep session)
  155. (get-buffer session)
  156. (save-window-excursion
  157. (run-picolisp org-babel-picolisp-cmd)
  158. (rename-buffer session-name)
  159. (current-buffer))))))
  160. (provide 'ob-picolisp)
  161. ;;; ob-picolisp.el ends here