ob-emacs-lisp.el 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. ;;; ob-emacs-lisp.el --- Babel Functions for Emacs-lisp Code -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2009-2017 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 (assq :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 (assq :result-params params))
  60. (let ((print-level nil)
  61. (print-length nil))
  62. (if (or (member "scalar" (cdr (assq :result-params params)))
  63. (member "verbatim" (cdr (assq :result-params params))))
  64. (format "%S" result)
  65. (format "%s" result)))
  66. (org-babel-reassemble-table
  67. result
  68. (org-babel-pick-name (cdr (assq :colname-names params))
  69. (cdr (assq :colnames params)))
  70. (org-babel-pick-name (cdr (assq :rowname-names params))
  71. (cdr (assq :rownames params))))))))
  72. (org-babel-make-language-alias "elisp" "emacs-lisp")
  73. (provide 'ob-emacs-lisp)
  74. ;;; ob-emacs-lisp.el ends here