ob-lisp.el 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. ;;; ob-lisp.el --- org-babel functions for Common Lisp
  2. ;; Copyright (C) 2010 Free Software Foundation
  3. ;; Author: David T. O'Toole <dto@gnu.org>, Eric Schulte
  4. ;; Keywords: literate programming, reproducible research, lisp
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 0.01
  7. ;;; License:
  8. ;; This program is free software; you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation; either version 3, or (at your option)
  11. ;; any later version.
  12. ;;
  13. ;; This program is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;;
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  20. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. ;; Boston, MA 02110-1301, USA.
  22. ;;; Commentary:
  23. ;; Now working with SBCL for both session and external evaluation.
  24. ;;
  25. ;; This certainly isn't optimally robust, but it seems to be working
  26. ;; for the basic use cases.
  27. ;;; Requirements:
  28. ;; Requires SLIME (Superior Lisp Interaction Mode for Emacs.)
  29. ;; See http://common-lisp.net/project/slime/
  30. ;;; Code:
  31. (require 'ob)
  32. (require 'ob-ref)
  33. (require 'ob-comint)
  34. (require 'ob-eval)
  35. (require 'slime)
  36. (defvar org-babel-default-header-args:lisp '()
  37. "Default header arguments for lisp code blocks.")
  38. (defcustom org-babel-lisp-cmd "sbcl --script"
  39. "Name of command used to evaluate lisp blocks.")
  40. (defun org-babel-expand-body:lisp (body params &optional processed-params)
  41. "Expand BODY according to PARAMS, return the expanded body."
  42. (let ((vars (nth 1 (or processed-params (org-babel-process-params params)))))
  43. (if (> (length vars) 0)
  44. (concat "(let ("
  45. (mapconcat
  46. (lambda (var) (format "%S" (print `(,(car var) ',(cdr var)))))
  47. vars "\n ")
  48. ")\n" body ")")
  49. body)))
  50. (defun org-babel-execute:lisp (body params)
  51. "Execute a block of Lisp code with org-babel.
  52. This function is called by `org-babel-execute-src-block'"
  53. (message "executing Lisp source code block")
  54. (let* ((processed-params (org-babel-process-params params))
  55. (session (org-babel-lisp-initiate-session (first processed-params)))
  56. (result-type (fourth processed-params))
  57. (full-body (org-babel-expand-body:lisp body params processed-params)))
  58. (read
  59. (if session
  60. ;; session evaluation
  61. (save-window-excursion
  62. (cadr (slime-eval `(swank:eval-and-grab-output ,full-body))))
  63. ;; external evaluation
  64. (let ((script-file (make-temp-file "ob-lisp-script")))
  65. (with-temp-file script-file
  66. (insert
  67. ;; return the value or the output
  68. (if (string= result-type "value")
  69. (format "(print %s)" full-body)
  70. full-body)))
  71. (org-babel-eval (format "%s %s" org-babel-lisp-cmd script-file) ""))))))
  72. ;; This function should be used to assign any variables in params in
  73. ;; the context of the session environment.
  74. (defun org-babel-prep-session:lisp (session params)
  75. "Prepare SESSION according to the header arguments specified in PARAMS."
  76. (error "not yet implemented"))
  77. (defun org-babel-lisp-initiate-session (&optional session)
  78. "If there is not a current inferior-process-buffer in SESSION
  79. then create. Return the initialized session."
  80. (unless (string= session "none")
  81. (save-window-excursion
  82. (or (slime-connected-p)
  83. (slime-process)))))
  84. (provide 'ob-lisp)
  85. ;; arch-tag: 18086168-009f-4947-bbb5-3532375d851d
  86. ;;; ob-lisp.el ends here