ob-lisp.el 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. ;;; ob-lisp.el --- org-babel functions for common lisp evaluation
  2. ;; Copyright (C) 2009, 2010, 2011 Free Software Foundation, Inc.
  3. ;; Author: Joel Boehland, Eric Schulte, David T. O'Toole <dto@gnu.org>
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 7.6
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs 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 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;;; support for evaluating common lisp code, relies on slime for all eval
  20. ;;; Requirements:
  21. ;; Requires SLIME (Superior Lisp Interaction Mode for Emacs.)
  22. ;; See http://common-lisp.net/project/slime/
  23. ;;; Code:
  24. (require 'ob)
  25. (declare-function slime-eval "ext:slime" (sexp &optional package))
  26. (defvar org-babel-tangle-lang-exts)
  27. (add-to-list 'org-babel-tangle-lang-exts '("lisp" . "lisp"))
  28. (defvar org-babel-default-header-args:lisp '())
  29. (defvar org-babel-header-arg-names:lisp '(package))
  30. (defcustom org-babel-lisp-dir-fmt
  31. "(let ((*default-pathname-defaults* #P%S)) %%s)"
  32. "Format string used to wrap code bodies to set the current directory.
  33. For example a value of \"(progn ;; %s\\n %%s)\" would ignore the
  34. current directory string."
  35. :group 'org-babel
  36. :type 'string)
  37. (defun org-babel-expand-body:lisp (body params)
  38. "Expand BODY according to PARAMS, return the expanded body."
  39. (let* ((vars (mapcar #'cdr (org-babel-get-header params :var)))
  40. (result-params (cdr (assoc :result-params params)))
  41. (print-level nil) (print-length nil)
  42. (body (org-babel-trim
  43. (if (> (length vars) 0)
  44. (concat "(let ("
  45. (mapconcat
  46. (lambda (var)
  47. (format "(%S (quote %S))" (car var) (cdr var)))
  48. vars "\n ")
  49. ")\n" body ")")
  50. body))))
  51. (if (or (member "code" result-params)
  52. (member "pp" result-params))
  53. (format "(pprint %s)" body)
  54. body)))
  55. (defun org-babel-execute:lisp (body params)
  56. "Execute a block of Common Lisp code with Babel."
  57. (require 'slime)
  58. (org-babel-reassemble-table
  59. ((lambda (result)
  60. (if (member "output" (cdr (assoc :result-params params)))
  61. (car result)
  62. (condition-case nil
  63. (read (org-bable-lisp-vector-to-list (cadr result)))
  64. (error (cadr result)))))
  65. (with-temp-buffer
  66. (insert (org-babel-expand-body:lisp body params))
  67. (slime-eval `(swank:eval-and-grab-output
  68. ,(let ((dir (if (assoc :dir params)
  69. (cdr (assoc :dir params))
  70. default-directory)))
  71. (format
  72. (if dir (format org-babel-lisp-dir-fmt dir) "(progn %s)")
  73. (buffer-substring-no-properties
  74. (point-min) (point-max)))))
  75. (cdr (assoc :package params)))))
  76. (org-babel-pick-name (cdr (assoc :colname-names params))
  77. (cdr (assoc :colnames params)))
  78. (org-babel-pick-name (cdr (assoc :rowname-names params))
  79. (cdr (assoc :rownames params)))))
  80. (defun org-bable-lisp-vector-to-list (results)
  81. ;; TODO: better would be to replace #(...) with [...]
  82. (replace-regexp-in-string "#(" "(" results))
  83. (provide 'ob-lisp)
  84. ;; arch-tag: 18086168-009f-4947-bbb5-3532375d851d
  85. ;;; ob-lisp.el ends here