ob-emacs-lisp.el 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. ;;; ob-emacs-lisp.el --- org-babel functions for emacs-lisp code evaluation
  2. ;; Copyright (C) 2009-2014 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. (defvar org-babel-default-header-args:emacs-lisp
  22. '((:hlines . "yes") (:colnames . "no"))
  23. "Default arguments for evaluating an emacs-lisp source block.")
  24. (declare-function orgtbl-to-generic "org-table" (table params))
  25. (defun org-babel-expand-body:emacs-lisp (body params)
  26. "Expand BODY according to PARAMS, return the expanded body."
  27. (let* ((vars (mapcar #'cdr (org-babel-get-header params :var)))
  28. (result-params (cdr (assoc :result-params params)))
  29. (print-level nil) (print-length nil)
  30. (body (if (> (length vars) 0)
  31. (concat "(let ("
  32. (mapconcat
  33. (lambda (var)
  34. (format "%S" (print `(,(car var) ',(cdr var)))))
  35. vars "\n ")
  36. ")\n" body "\n)")
  37. (concat body "\n"))))
  38. (if (or (member "code" result-params)
  39. (member "pp" result-params))
  40. (concat "(pp " body ")") body)))
  41. (defun org-babel-execute:emacs-lisp (body params)
  42. "Execute a block of emacs-lisp code with Babel."
  43. (save-window-excursion
  44. (let ((result
  45. (eval (read (format (if (member "output"
  46. (cdr (assoc :result-params params)))
  47. "(with-output-to-string %s)"
  48. "(progn %s)")
  49. (org-babel-expand-body:emacs-lisp
  50. body params))))))
  51. (org-babel-result-cond (cdr (assoc :result-params params))
  52. (let ((print-level nil)
  53. (print-length nil))
  54. (if (or (member "scalar" (cdr (assoc :result-params params)))
  55. (member "verbatim" (cdr (assoc :result-params params))))
  56. (format "%S" result)
  57. (format "%s" result)))
  58. (org-babel-reassemble-table
  59. result
  60. (org-babel-pick-name (cdr (assoc :colname-names params))
  61. (cdr (assoc :colnames params)))
  62. (org-babel-pick-name (cdr (assoc :rowname-names params))
  63. (cdr (assoc :rownames params))))))))
  64. (provide 'ob-emacs-lisp)
  65. ;;; ob-emacs-lisp.el ends here