| 1 | ;;; ob-ledger.el --- org-babel functions for ledger evaluation |
| 2 | |
| 3 | ;; Copyright (C) 2010-2011 Free Software Foundation, Inc. |
| 4 | |
| 5 | ;; Author: Eric S Fraga |
| 6 | ;; Keywords: literate programming, reproducible research, accounting |
| 7 | ;; Homepage: http://orgmode.org |
| 8 | ;; Version: 7.4 |
| 9 | |
| 10 | ;; This file is part of GNU Emacs. |
| 11 | |
| 12 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
| 13 | ;; it under the terms of the GNU General Public License as published by |
| 14 | ;; the Free Software Foundation, either version 3 of the License, or |
| 15 | ;; (at your option) any later version. |
| 16 | |
| 17 | ;; GNU Emacs is distributed in the hope that it will be useful, |
| 18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | ;; GNU General Public License for more details. |
| 21 | |
| 22 | ;; You should have received a copy of the GNU General Public License |
| 23 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
| 24 | |
| 25 | ;;; Commentary: |
| 26 | |
| 27 | ;; Org-Babel support for evaluating ledger entries. |
| 28 | ;; |
| 29 | ;; This differs from most standard languages in that |
| 30 | ;; |
| 31 | ;; 1) there is no such thing as a "session" in ledger |
| 32 | ;; |
| 33 | ;; 2) we are generally only going to return output from the leger program |
| 34 | ;; |
| 35 | ;; 3) we are adding the "cmdline" header argument |
| 36 | ;; |
| 37 | ;; 4) there are no variables |
| 38 | |
| 39 | ;;; Code: |
| 40 | (require 'ob) |
| 41 | |
| 42 | (defvar org-babel-default-header-args:ledger |
| 43 | '((:results . "output") (:cmdline . "bal")) |
| 44 | "Default arguments to use when evaluating a ledger source block.") |
| 45 | |
| 46 | (defun org-babel-execute:ledger (body params) |
| 47 | "Execute a block of Ledger entries with org-babel. This function is |
| 48 | called by `org-babel-execute-src-block'." |
| 49 | (message "executing Ledger source code block") |
| 50 | (let ((result-params (split-string (or (cdr (assoc :results params)) ""))) |
| 51 | (cmdline (cdr (assoc :cmdline params))) |
| 52 | (in-file (org-babel-temp-file "ledger-")) |
| 53 | (out-file (org-babel-temp-file "ledger-output-"))) |
| 54 | (with-temp-file in-file (insert body)) |
| 55 | (message (concat "ledger" |
| 56 | " -f " (org-babel-process-file-name in-file) |
| 57 | " " cmdline)) |
| 58 | (with-output-to-string |
| 59 | (shell-command (concat "ledger" |
| 60 | " -f " (org-babel-process-file-name in-file) |
| 61 | " " cmdline |
| 62 | " > " (org-babel-process-file-name out-file)))) |
| 63 | (with-temp-buffer (insert-file-contents out-file) (buffer-string)))) |
| 64 | |
| 65 | (defun org-babel-prep-session:ledger (session params) |
| 66 | (error "Ledger does not support sessions")) |
| 67 | |
| 68 | (provide 'ob-ledger) |
| 69 | |
| 70 | |
| 71 | ;;; ob-ledger.el ends here |