dotemacs

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

ob-hledger.el (2714B)


      1 ;;; ob-hledger.el --- Babel Functions for hledger      -*- lexical-binding: t; -*-
      2 
      3 ;; Copyright (C) 2010-2021 Free Software Foundation, Inc.
      4 
      5 ;; Author: Simon Michael
      6 ;; Keywords: literate programming, reproducible research, plain text accounting
      7 ;; Homepage: https://git.sr.ht/~bzg/org-contrib
      8 
      9 ;; This file is not part of GNU Emacs.
     10 
     11 ;; GNU Emacs is free software: you can redistribute it and/or modify
     12 ;; it under the terms of the GNU General Public License as published by
     13 ;; the Free Software Foundation, either version 3 of the License, or
     14 ;; (at your option) any later version.
     15 
     16 ;; GNU Emacs is distributed in the hope that it will be useful,
     17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
     18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     19 ;; GNU General Public License for more details.
     20 
     21 ;; You should have received a copy of the GNU General Public License
     22 ;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.
     23 
     24 ;;; Commentary:
     25 
     26 ;; Babel support for evaluating hledger entries.
     27 ;;
     28 ;; Based on ob-ledger.el.
     29 ;; If the source block is empty, hledger will use a default journal file,
     30 ;; probably ~/.hledger.journal (it may not notice your $LEDGER_FILE env var).
     31 ;; So make ~/.hledger.journal a symbolic link to the real file if necessary.
     32 
     33 ;; TODO Unit tests are more than welcome, too.
     34 
     35 ;;; Code:
     36 (require 'ob)
     37 
     38 (defvar org-babel-default-header-args:hledger
     39   '((:results . "output") (:exports . "results") (:cmdline . "bal"))
     40   "Default arguments to use when evaluating a hledger source block.")
     41 
     42 (defun org-babel-execute:hledger (body params)
     43   "Execute a block of hledger entries with org-babel.
     44 This function is called by `org-babel-execute-src-block'."
     45   (message "executing hledger source code block")
     46   (letrec ( ;(result-params (split-string (or (cdr (assq :results params)) "")))
     47 	   (cmdline (cdr (assq :cmdline params)))
     48 	   (in-file (org-babel-temp-file "hledger-"))
     49 	   (out-file (org-babel-temp-file "hledger-output-"))
     50 	   (hledgercmd (concat "hledger"
     51 			       (if (> (length body) 0)
     52 				   (concat " -f " (org-babel-process-file-name in-file))
     53 				 "")
     54 			       " " cmdline)))
     55     (with-temp-file in-file (insert body))
     56 ;; TODO This is calling for some refactoring:
     57 ;;  (concat "hledger" (if ...) " " cmdline)
     58 ;; could be built only once and bound to a symbol.
     59     (message "%s" hledgercmd)
     60     (with-output-to-string
     61       (shell-command (concat hledgercmd " > " (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:hledger (_session _params)
     65   (error "hledger does not support sessions"))
     66 
     67 (provide 'ob-hledger)
     68 
     69 ;;; ob-hledger.el ends here