ob-emacs-lisp.el 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. ;;; ob-emacs-lisp.el --- org-babel functions for emacs-lisp code evaluation
  2. ;; Copyright (C) 2009 Eric Schulte
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 0.01
  7. ;;; License:
  8. ;; This program 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, or (at your option)
  11. ;; any later version.
  12. ;;
  13. ;; This program 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. ;;
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  20. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. ;; Boston, MA 02110-1301, USA.
  22. ;;; Commentary:
  23. ;; Org-Babel support for evaluating emacs-lisp code
  24. ;;; Code:
  25. (require 'ob)
  26. (org-babel-add-interpreter "emacs-lisp")
  27. (add-to-list 'org-babel-tangle-langs '("emacs-lisp" "el"))
  28. (defvar org-babel-default-header-args:emacs-lisp
  29. '((:hlines . "yes") (:colnames . "no"))
  30. "Default arguments to use when evaluating an emacs-lisp source block.")
  31. (declare-function org-babel-comint-with-output "ob-comint" (&rest body))
  32. (declare-function org-babel-comint-buffer-livep "ob-comint" (buffer))
  33. (declare-function org-babel-comint-wait-for-output "ob-comint" (buffer))
  34. (declare-function org-babel-comint-in-buffer "ob-comint" (buffer &rest body))
  35. (declare-function orgtbl-to-generic "org-table" (table params))
  36. (defun org-babel-expand-body:emacs-lisp (body params &optional processed-params)
  37. "Expand BODY according to PARAMS, return the expanded body."
  38. (let* ((processed-params (or processed-params (org-babel-process-params params)))
  39. (vars (nth 1 processed-params))
  40. (processed-params (org-babel-process-params params))
  41. (result-params (nth 2 processed-params))
  42. (print-level nil) (print-length nil)
  43. (body (if (> (length vars) 0)
  44. (concat "(let ("
  45. (mapconcat
  46. (lambda (var) (format "%S" (print `(,(car var) ',(cdr var)))))
  47. vars "\n ")
  48. ")\n" body ")")
  49. body)))
  50. (if (or (member "code" result-params)
  51. (member "pp" result-params))
  52. (concat "(pp " body ")") body)))
  53. (defun org-babel-execute:emacs-lisp (body params)
  54. "Execute a block of emacs-lisp code with org-babel."
  55. (message "executing emacs-lisp code block...")
  56. (save-window-excursion
  57. (let ((processed-params (org-babel-process-params params)))
  58. (org-babel-reassemble-table
  59. (eval (read (format "(progn %s)"
  60. (org-babel-expand-body:emacs-lisp body params))))
  61. (org-babel-pick-name (nth 4 processed-params) (cdr (assoc :colnames params)))
  62. (org-babel-pick-name (nth 5 processed-params) (cdr (assoc :rownames params)))))))
  63. (provide 'obemacs-lisp)
  64. ;;; ob-emacs-lisp.el ends here