dotemacs

My Emacs configuration
git clone git://git.entf.net/dotemacs
Log | Files | Refs | LICENSE

ob-ledger.el (2441B)


      1 ;;; ob-ledger.el --- Babel Functions for Ledger      -*- lexical-binding: t; -*-
      2 
      3 ;; Copyright (C) 2010-2021 Free Software Foundation, Inc.
      4 
      5 ;; Author: Eric S Fraga
      6 ;; Maintainer: Eric S Fraga
      7 ;; Keywords: literate programming, reproducible research, accounting
      8 ;; Homepage: https://git.sr.ht/~bzg/org-contrib
      9 
     10 ;; This file is not 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 <https://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 ledger 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.
     48 This function is called by `org-babel-execute-src-block'."
     49   (message "executing Ledger source code block")
     50   (let ((cmdline (cdr (assq :cmdline params)))
     51         (in-file (org-babel-temp-file "ledger-"))
     52 	(out-file (org-babel-temp-file "ledger-output-")))
     53     (with-temp-file in-file (insert body))
     54     (message "%s" (concat "ledger"
     55 			  " -f " (org-babel-process-file-name in-file)
     56 			  " " cmdline))
     57     (with-output-to-string
     58       (shell-command (concat "ledger"
     59 			     " -f " (org-babel-process-file-name in-file)
     60 			     " " cmdline
     61 			     " > " (org-babel-process-file-name out-file))))
     62     (with-temp-buffer (insert-file-contents out-file) (buffer-string))))
     63 
     64 (defun org-babel-prep-session:ledger (_session _params)
     65   (error "Ledger does not support sessions"))
     66 
     67 (provide 'ob-ledger)
     68 
     69 ;;; ob-ledger.el ends here