ob-lisp.el 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. ;;; ob-lisp.el --- Babel Functions for Common Lisp -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2009-2016 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 SLY or SLIME
  21. ;;; for all eval.
  22. ;;; Requirements:
  23. ;; Requires SLY (Sylvester the Cat's Common Lisp IDE) or SLIME
  24. ;; (Superior Lisp Interaction Mode for Emacs). See:
  25. ;; - https://github.com/capitaomorte/sly
  26. ;; - http://common-lisp.net/project/slime/
  27. ;;; Code:
  28. (require 'ob)
  29. (defcustom org-babel-lisp-eval-fn "sly-eval"
  30. "The function to be called to evaluate code on the Lisp side.
  31. It can be set to either \"sly-eval\" or \"slime-val\"."
  32. :group 'org-babel
  33. :version "25.1"
  34. :package-version '(Org . "8.3")
  35. :options '("sly-eval" "slime-eval")
  36. :type 'stringp)
  37. (declare-function sly-eval "ext:sly" (sexp &optional package))
  38. (declare-function slime-eval "ext:slime" (sexp &optional package))
  39. (defvar org-babel-tangle-lang-exts)
  40. (add-to-list 'org-babel-tangle-lang-exts '("lisp" . "lisp"))
  41. (defvar org-babel-default-header-args:lisp '())
  42. (defvar org-babel-header-args:lisp '((package . :any)))
  43. (defcustom org-babel-lisp-dir-fmt
  44. "(let ((*default-pathname-defaults* #P%S\n)) %%s\n)"
  45. "Format string used to wrap code bodies to set the current directory.
  46. For example a value of \"(progn ;; %s\\n %%s)\" would ignore the
  47. current directory string."
  48. :group 'org-babel
  49. :version "24.1"
  50. :type 'string)
  51. (defun org-babel-expand-body:lisp (body params)
  52. "Expand BODY according to PARAMS, return the expanded body."
  53. (let* ((vars (org-babel--get-vars params))
  54. (result-params (cdr (assoc :result-params params)))
  55. (print-level nil) (print-length nil)
  56. (body (org-babel-trim
  57. (if (> (length vars) 0)
  58. (concat "(let ("
  59. (mapconcat
  60. (lambda (var)
  61. (format "(%S (quote %S))" (car var) (cdr var)))
  62. vars "\n ")
  63. ")\n" body ")")
  64. body))))
  65. (if (or (member "code" result-params)
  66. (member "pp" result-params))
  67. (format "(pprint %s)" body)
  68. body)))
  69. (defun org-babel-execute:lisp (body params)
  70. "Execute a block of Common Lisp code with Babel.
  71. BODY is the contents of the block, as a string. PARAMS is
  72. a property list containing the parameters of the block."
  73. (require (pcase org-babel-lisp-eval-fn
  74. ("slime-eval" 'slime)
  75. ("sly-eval" 'sly)))
  76. (org-babel-reassemble-table
  77. (let ((result
  78. (funcall (if (member "output" (cdr (assoc :result-params params)))
  79. #'car #'cadr)
  80. (with-temp-buffer
  81. (insert (org-babel-expand-body:lisp body params))
  82. (funcall org-babel-lisp-eval-fn
  83. `(swank:eval-and-grab-output
  84. ,(let ((dir (if (assoc :dir params)
  85. (cdr (assoc :dir params))
  86. default-directory)))
  87. (format
  88. (if dir (format org-babel-lisp-dir-fmt dir)
  89. "(progn %s\n)")
  90. (buffer-substring-no-properties
  91. (point-min) (point-max)))))
  92. (cdr (assoc :package params)))))))
  93. (org-babel-result-cond (cdr (assoc :result-params params))
  94. result
  95. (condition-case nil
  96. (read (org-babel-lisp-vector-to-list result))
  97. (error result))))
  98. (org-babel-pick-name (cdr (assoc :colname-names params))
  99. (cdr (assoc :colnames params)))
  100. (org-babel-pick-name (cdr (assoc :rowname-names params))
  101. (cdr (assoc :rownames params)))))
  102. (defun org-babel-lisp-vector-to-list (results)
  103. ;; TODO: better would be to replace #(...) with [...]
  104. (replace-regexp-in-string "#(" "(" results))
  105. (provide 'ob-lisp)
  106. ;;; ob-lisp.el ends here