org-babel-emacs-lisp.el 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. ;;; org-babel-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 'org-babel)
  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. (defun org-babel-expand-body:emacs-lisp (body params &optional processed-params)
  32. (let* ((processed-params (or processed-params (org-babel-process-params params)))
  33. (vars (second processed-params))
  34. (processed-params (org-babel-process-params params))
  35. (result-params (third processed-params))
  36. (print-level nil) (print-length nil)
  37. (body (if (> (length vars) 0)
  38. (concat "(let ("
  39. (mapconcat
  40. (lambda (var) (format "%S" (print `(,(car var) ',(cdr var)))))
  41. vars "\n ")
  42. ")\n" body ")")
  43. body)))
  44. (if (or (member "code" result-params)
  45. (member "pp" result-params))
  46. (concat "(pp " body ")") body)))
  47. (defun org-babel-execute:emacs-lisp (body params)
  48. "Execute a block of emacs-lisp code with org-babel."
  49. (message "executing emacs-lisp code block...")
  50. (save-window-excursion
  51. (let ((processed-params (org-babel-process-params params)))
  52. (org-babel-reassemble-table
  53. (eval (read (format "(progn %s)"
  54. (org-babel-expand-body:emacs-lisp body params))))
  55. (org-babel-pick-name (nth 4 processed-params) (cdr (assoc :colnames params)))
  56. (org-babel-pick-name (nth 5 processed-params) (cdr (assoc :rownames params)))))))
  57. (provide 'org-babel-emacs-lisp)
  58. ;;; org-babel-emacs-lisp.el ends here