org-babel-lob.el 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. ;;; org-babel-lob.el --- The Library of Babel: off-the-shelf functions for data analysis and plotting using org-babel
  2. ;; Copyright (C) 2009 Dan Davison, Eric Schulte
  3. ;; Author: Dan Davison, 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. ;; See org-babel.org in the parent directory for more information
  24. ;;; Code:
  25. (require 'org)
  26. (defvar org-babel-lob-regexp
  27. (concat "#\\+babel[ \t]*"
  28. "\\([ \t]+\\([^\n]+\\)\\)?\n") ;; match header arguments
  29. "Regexp used to test when on a babel library call line")
  30. (defvar org-babel-lob-inline-regexp nil
  31. "Regexp used to test whether at an inline babel library call")
  32. (defun org-babel-lob-execute-maybe ()
  33. "Detect if this is a babel library line and if so
  34. then run `org-babel-lob-execute'."
  35. (interactive)
  36. (let ((info (org-babel-get-src-block-info)))
  37. (if info (progn (org-babel-execute-src-block current-prefix-arg info) t) nil)))
  38. (add-hook 'org-ctrl-c-ctrl-c-hook 'org-babel-lob-call)
  39. (defun org-babel-lob-execute ()
  40. "Execute an org-babel library function."
  41. (interactive))
  42. (defun org-babel-lob-get-src-block-info ()
  43. "This is taken from `org-babel-get-src-block-info'. Maybe we could abstract and unify.
  44. Return the information of the current source block as a list
  45. of the following form. (language body header-arguments-alist)"
  46. (let ((case-fold-search t) head)
  47. (if (setq head (org-babel-lob-where-is-block-head))
  48. (save-excursion (goto-char head) (org-babel-lob-parse-lob-line-match))
  49. (if (save-excursion ;; inline source block
  50. (re-search-backward "[ \f\t\n\r\v]" nil t)
  51. (forward-char 1)
  52. (looking-at org-babel-lob-inline-regexp))
  53. (org-babel-parse-inline-src-block-match)
  54. nil)))) ;; indicate that no source block was found
  55. (defun org-babel-lob-parse-lob-line-match ()
  56. (list nil ;; no language
  57. nil ;; no head
  58. (org-combine-plists
  59. org-babel-default-header-args
  60. (org-babel-parse-header-arguments
  61. (org-babel-clean-text-properties
  62. (or (match-string 3) ""))))))
  63. (defun org-babel-lob-where-is-block-head ()
  64. "Return point at beginning of #+babel line."
  65. (save-excursion
  66. (beginning-of-line 1)
  67. (and (looking-at org-babel-lob-regexp)
  68. (point))))
  69. (provide 'org-babel-lob)