ob-emacs-lisp.el 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. ;;; ob-emacs-lisp.el --- Babel Functions for Emacs-lisp Code -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2009-2016 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; Org-Babel support for evaluating emacs-lisp code
  19. ;;; Code:
  20. (require 'ob)
  21. (defconst org-babel-header-args:emacs-lisp '((lexical . :any))
  22. "Emacs-lisp specific header arguments.")
  23. (defvar org-babel-default-header-args:emacs-lisp '((:lexical . "no"))
  24. "Default arguments for evaluating an emacs-lisp source block.
  25. A value of \"yes\" or t causes src blocks to be eval'd using
  26. lexical scoping. It can also be an alist mapping symbols to
  27. their value. It is used as the optional LEXICAL argument to
  28. `eval', which see.")
  29. (defun org-babel-expand-body:emacs-lisp (body params)
  30. "Expand BODY according to PARAMS, return the expanded body."
  31. (let* ((vars (org-babel--get-vars params))
  32. (result-params (cdr (assoc :result-params params)))
  33. (print-level nil) (print-length nil)
  34. (body (if (> (length vars) 0)
  35. (concat "(let ("
  36. (mapconcat
  37. (lambda (var)
  38. (format "%S" (print `(,(car var) ',(cdr var)))))
  39. vars "\n ")
  40. ")\n" body "\n)")
  41. (concat body "\n"))))
  42. (if (or (member "code" result-params)
  43. (member "pp" result-params))
  44. (concat "(pp " body ")") body)))
  45. (defun org-babel-execute:emacs-lisp (body params)
  46. "Execute a block of emacs-lisp code with Babel."
  47. (save-window-excursion
  48. (let* ((lexical (cdr (assq :lexical params)))
  49. (result
  50. (eval (read (format (if (member "output"
  51. (cdr (assq :result-params params)))
  52. "(with-output-to-string %s)"
  53. "(progn %s)")
  54. (org-babel-expand-body:emacs-lisp
  55. body params)))
  56. (if (listp lexical)
  57. lexical
  58. (member lexical '("yes" "t"))))))
  59. (org-babel-result-cond (cdr (assoc :result-params params))
  60. (let ((print-level nil)
  61. (print-length nil))
  62. (if (or (member "scalar" (cdr (assoc :result-params params)))
  63. (member "verbatim" (cdr (assoc :result-params params))))
  64. (format "%S" result)
  65. (format "%s" result)))
  66. (org-babel-reassemble-table
  67. result
  68. (org-babel-pick-name (cdr (assoc :colname-names params))
  69. (cdr (assoc :colnames params)))
  70. (org-babel-pick-name (cdr (assoc :rowname-names params))
  71. (cdr (assoc :rownames params))))))))
  72. (org-babel-make-language-alias "elisp" "emacs-lisp")
  73. (provide 'ob-emacs-lisp)
  74. ;;; ob-emacs-lisp.el ends here