ob-table.el 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. ;;; ob-table.el --- support for calling org-babel functions from tables
  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: 0.01
  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. ;; Should allow calling functions from org-mode tables using the
  20. ;; function `sbe' as so...
  21. ;; #+begin_src emacs-lisp :results silent
  22. ;; (defun fibbd (n) (if (< n 2) 1 (+ (fibbd (- n 1)) (fibbd (- n 2)))))
  23. ;; #+end_src
  24. ;; #+srcname: fibbd
  25. ;; #+begin_src emacs-lisp :var n=2 :results silent
  26. ;; (fibbd n)
  27. ;; #+end_src
  28. ;; | original | fibbd |
  29. ;; |----------+--------|
  30. ;; | 0 | |
  31. ;; | 1 | |
  32. ;; | 2 | |
  33. ;; | 3 | |
  34. ;; | 4 | |
  35. ;; | 5 | |
  36. ;; | 6 | |
  37. ;; | 7 | |
  38. ;; | 8 | |
  39. ;; | 9 | |
  40. ;; #+TBLFM: $2='(sbe 'fibbd (n $1))
  41. ;;; Code:
  42. (require 'ob)
  43. (defun org-babel-table-truncate-at-newline (string)
  44. "If STRING ends in a newline character, then remove the newline
  45. character and replace it with ellipses."
  46. (if (and (stringp string) (string-match "[\n\r]" string))
  47. (concat (substring string 0 (match-beginning 0)) "...")
  48. string))
  49. (defmacro sbe (source-block &rest variables)
  50. "Return the results of calling SOURCE-BLOCK assigning every
  51. variable in VARIABLES. Each element of VARIABLES should be a two
  52. element list, whose first element is the name of the variable and
  53. second element is a string of its value. The following call to
  54. `sbe' would be equivalent to the following source code block.
  55. (sbe 'source-block (n $2) (m 3))
  56. #+begin_src emacs-lisp :var results=source-block(n=val_at_col_2, m=3) :results silent
  57. results
  58. #+end_src
  59. NOTE: by default string variable names are interpreted as
  60. references to source-code blocks, to force interpretation of a
  61. cell's value as a string, prefix the identifier with two \"$\"s
  62. rather than a single \"$\" (i.e. \"$$2\" instead of \"$2\" in the
  63. example above."
  64. (let ((variables (mapcar
  65. (lambda (var)
  66. (if (and (= 3 (length var)) (eq (nth 1 var) '$))
  67. (list (car var) (format "\"%s\"" (last var)))
  68. var))
  69. variables)))
  70. (unless (stringp source-block) (setq source-block (symbol-name source-block)))
  71. (org-babel-table-truncate-at-newline ;; org-table cells can't be multi-line
  72. (if (and source-block (> (length source-block) 0))
  73. (let ((params
  74. (eval `(org-babel-parse-header-arguments
  75. (concat ":var results="
  76. ,source-block
  77. "("
  78. (mapconcat (lambda (var-spec)
  79. (format "%S=%s" (nth 0 var-spec) (nth 1 var-spec)))
  80. ',variables ", ")
  81. ")")))))
  82. (org-babel-execute-src-block
  83. nil (list "emacs-lisp" "results"
  84. (org-babel-merge-params '((:results . "silent")) params))))
  85. ""))))
  86. (provide 'ob-table)
  87. ;; arch-tag: 4234cc7c-4fc8-4e92-abb0-2892de1a493b
  88. ;;; ob-table.el ends here