ob-lisp.el 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. ;;; ob-lisp.el --- Babel Functions for Common Lisp -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2009-2015 Free Software Foundation, Inc.
  3. ;; Authors: 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-args:lisp '((package . :any)))
  31. (defcustom org-babel-lisp-dir-fmt
  32. "(let ((*default-pathname-defaults* #P%S\n)) %%s\n)"
  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 (org-babel--get-vars params))
  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. (let ((result
  62. (funcall (if (member "output" (cdr (assoc :result-params params)))
  63. #'car #'cadr)
  64. (with-temp-buffer
  65. (insert (org-babel-expand-body:lisp body params))
  66. (slime-eval `(swank:eval-and-grab-output
  67. ,(let ((dir (if (assoc :dir params)
  68. (cdr (assoc :dir params))
  69. default-directory)))
  70. (format
  71. (if dir (format org-babel-lisp-dir-fmt dir)
  72. "(progn %s\n)")
  73. (buffer-substring-no-properties
  74. (point-min) (point-max)))))
  75. (cdr (assoc :package params)))))))
  76. (org-babel-result-cond (cdr (assoc :result-params params))
  77. result
  78. (condition-case nil
  79. (read (org-babel-lisp-vector-to-list result))
  80. (error result))))
  81. (org-babel-pick-name (cdr (assoc :colname-names params))
  82. (cdr (assoc :colnames params)))
  83. (org-babel-pick-name (cdr (assoc :rowname-names params))
  84. (cdr (assoc :rownames params)))))
  85. (defun org-babel-lisp-vector-to-list (results)
  86. ;; TODO: better would be to replace #(...) with [...]
  87. (replace-regexp-in-string "#(" "(" results))
  88. (provide 'ob-lisp)
  89. ;;; ob-lisp.el ends here