ob-lisp.el 3.5 KB

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