ob-emacs-lisp.el 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. ;;; ob-emacs-lisp.el --- org-babel functions for emacs-lisp code evaluation
  2. ;; Copyright (C) 2009, 2010 Free Software Foundation, Inc
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 7.6
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; Org-Babel support for evaluating emacs-lisp code
  20. ;;; Code:
  21. (require 'ob)
  22. (eval-when-compile (require 'ob-comint))
  23. (defvar org-babel-default-header-args:emacs-lisp
  24. '((:hlines . "yes") (:colnames . "no"))
  25. "Default arguments for evaluating an emacs-lisp source block.")
  26. (declare-function orgtbl-to-generic "org-table" (table params))
  27. (defun org-babel-expand-body:emacs-lisp (body params)
  28. "Expand BODY according to PARAMS, return the expanded body."
  29. (let* ((vars (mapcar #'cdr (org-babel-get-header params :var)))
  30. (result-params (cdr (assoc :result-params params)))
  31. (print-level nil) (print-length nil)
  32. (body (if (> (length vars) 0)
  33. (concat "(let ("
  34. (mapconcat
  35. (lambda (var)
  36. (format "%S" (print `(,(car var) ',(cdr var)))))
  37. vars "\n ")
  38. ")\n" body ")")
  39. body)))
  40. (if (or (member "code" result-params)
  41. (member "pp" result-params))
  42. (concat "(pp " body ")") body)))
  43. (defun org-babel-execute:emacs-lisp (body params)
  44. "Execute a block of emacs-lisp code with Babel."
  45. (save-window-excursion
  46. ((lambda (result)
  47. (if (or (member "scalar" (cdr (assoc :result-params params)))
  48. (member "verbatim" (cdr (assoc :result-params params))))
  49. (let ((print-level nil)
  50. (print-length nil))
  51. (format "%S" result))
  52. (org-babel-reassemble-table
  53. result
  54. (org-babel-pick-name (cdr (assoc :colname-names params))
  55. (cdr (assoc :colnames params)))
  56. (org-babel-pick-name (cdr (assoc :rowname-names params))
  57. (cdr (assoc :rownames params))))))
  58. (eval (read (format (if (member "output"
  59. (cdr (assoc :result-params params)))
  60. "(with-output-to-string %s)"
  61. "(progn %s)")
  62. (org-babel-expand-body:emacs-lisp body params)))))))
  63. (provide 'ob-emacs-lisp)
  64. ;; arch-tag: e9a3acca-dc84-472a-9f5a-23c35befbcd6
  65. ;;; ob-emacs-lisp.el ends here