ob-lisp.el 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. (declare-function sly-eval "ext:sly" (sexp &optional package))
  30. (declare-function slime-eval "ext:slime" (sexp &optional package))
  31. (declare-function org-trim "org" (s &optional keep-lead))
  32. (defvar org-babel-tangle-lang-exts)
  33. (add-to-list 'org-babel-tangle-lang-exts '("lisp" . "lisp"))
  34. (defvar org-babel-default-header-args:lisp '())
  35. (defvar org-babel-header-args:lisp '((package . :any)))
  36. (defcustom org-babel-lisp-eval-fn #'slime-eval
  37. "The function to be called to evaluate code on the Lisp side.
  38. Valid values include `slime-eval' and `sly-eval'."
  39. :group 'org-babel
  40. :version "25.2"
  41. :package-version '(Org . "9.0")
  42. :type 'function)
  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 (assq :result-params params)))
  55. (print-level nil) (print-length nil)
  56. (body (if (null vars) (org-trim body)
  57. (concat "(let ("
  58. (mapconcat
  59. (lambda (var)
  60. (format "(%S (quote %S))" (car var) (cdr var)))
  61. vars "\n ")
  62. ")\n" body ")"))))
  63. (if (or (member "code" result-params)
  64. (member "pp" result-params))
  65. (format "(pprint %s)" body)
  66. body)))
  67. (defun org-babel-execute:lisp (body params)
  68. "Execute a block of Common Lisp code with Babel.
  69. BODY is the contents of the block, as a string. PARAMS is
  70. a property list containing the parameters of the block."
  71. (require (pcase org-babel-lisp-eval-fn
  72. (`slime-eval 'slime)
  73. (`sly-eval 'sly)))
  74. (org-babel-reassemble-table
  75. (let ((result
  76. (funcall (if (member "output" (cdr (assq :result-params params)))
  77. #'car #'cadr)
  78. (with-temp-buffer
  79. (insert (org-babel-expand-body:lisp body params))
  80. (funcall org-babel-lisp-eval-fn
  81. `(swank:eval-and-grab-output
  82. ,(let ((dir (if (assq :dir params)
  83. (cdr (assq :dir params))
  84. default-directory)))
  85. (format
  86. (if dir (format org-babel-lisp-dir-fmt dir)
  87. "(progn %s\n)")
  88. (buffer-substring-no-properties
  89. (point-min) (point-max)))))
  90. (cdr (assq :package params)))))))
  91. (org-babel-result-cond (cdr (assq :result-params params))
  92. result
  93. (condition-case nil
  94. (read (org-babel-lisp-vector-to-list result))
  95. (error result))))
  96. (org-babel-pick-name (cdr (assq :colname-names params))
  97. (cdr (assq :colnames params)))
  98. (org-babel-pick-name (cdr (assq :rowname-names params))
  99. (cdr (assq :rownames params)))))
  100. (defun org-babel-lisp-vector-to-list (results)
  101. ;; TODO: better would be to replace #(...) with [...]
  102. (replace-regexp-in-string "#(" "(" results))
  103. (provide 'ob-lisp)
  104. ;;; ob-lisp.el ends here