config

Personal configuration.
git clone git://code.dwrz.net/config
Log | Files | Refs

ledger-init.el (3775B)


      1 ;;; ledger-init.el --- Helper code for use with the "ledger" command-line tool  -*- lexical-binding: t; -*-
      2 
      3 ;; Copyright (C) 2003-2016 John Wiegley (johnw AT gnu DOT org)
      4 
      5 ;; This file is not part of GNU Emacs.
      6 
      7 ;; This is free software; you can redistribute it and/or modify it under
      8 ;; the terms of the GNU General Public License as published by the Free
      9 ;; Software Foundation; either version 2, or (at your option) any later
     10 ;; version.
     11 ;;
     12 ;; This is distributed in the hope that it will be useful, but WITHOUT
     13 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
     14 ;; FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
     15 ;; for more details.
     16 ;;
     17 ;; You should have received a copy of the GNU General Public License
     18 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
     19 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
     20 ;; MA 02110-1301 USA.
     21 
     22 ;;; Commentary:
     23 ;; Determine the ledger environment
     24 
     25 (require 'ledger-regex)
     26 
     27 ;;; Code:
     28 
     29 (defcustom ledger-init-file-name "~/.ledgerrc"
     30   "Location of the ledger initialization file.  nil if you don't have one."
     31   :type '(choice (const :tag "Do not read ledger initialization file" nil)
     32                  file)
     33   :group 'ledger-exec)
     34 
     35 (defvar ledger-environment-alist nil
     36   "Variable to hold details about ledger-mode's environment.
     37 
     38 Adding the dotted pair (\"decimal-comma\" . t) will tell ledger
     39 to treat commas as decimal separator.
     40 
     41 This variable is automatically populated by
     42 `ledger-init-load-init-file', which is called in the body of
     43 `ledger-mode'.")
     44 
     45 (defconst ledger-iso-date-format "%Y-%m-%d"
     46   "The format for ISO 8601 dates.")
     47 
     48 (defcustom ledger-default-date-format "%Y/%m/%d"
     49   "The date format that ledger uses throughout.
     50 Set this to the value of `ledger-iso-date-format' if you prefer
     51 ISO 8601 dates."
     52   :type 'string
     53   :package-version '(ledger-mode . "4.0.0")
     54   :group 'ledger)
     55 
     56 (defun ledger-format-date (&optional date format)
     57   "Format DATE according to the current preferred date format.
     58 Returns the current date if DATE is nil or not supplied.
     59 
     60 If FORMAT is provided, use that as the date format.  Otherwise,
     61 use the --input-date-format specified in `ledger-init-file-name',
     62 or if none, use `ledger-default-date-format'."
     63   (format-time-string
     64    (or format
     65        (cdr (assoc "input-date-format" ledger-environment-alist))
     66        ledger-default-date-format)
     67    date))
     68 
     69 
     70 (defun ledger-init-parse-initialization (buffer)
     71   "Parse the .ledgerrc file in BUFFER."
     72   (with-current-buffer buffer
     73     (let (environment-alist)
     74       (goto-char (point-min))
     75       (while (re-search-forward ledger-init-string-regex nil t)
     76         (let ((matchb (match-beginning 0)) ;; save the match data, string-match stamp on it
     77               (matche (match-end 0)))
     78           (end-of-line)
     79           (push (cons (let ((flag (buffer-substring-no-properties (+ 2 matchb) matche)))
     80                         (if (string-match "[ \t\n\r]+\\'" flag)
     81                             (replace-match "" t t flag)
     82                           flag))
     83                       (let ((value (buffer-substring-no-properties matche (point))))
     84                         (if (> (length value) 0)
     85                             value
     86                           t)))
     87                 environment-alist)))
     88       (nreverse environment-alist))))
     89 
     90 (defun ledger-init-load-init-file ()
     91   "Load and parse the .ledgerrc file into `ledger-environment-alist'."
     92   (interactive)
     93   (when (and ledger-init-file-name
     94              (file-readable-p ledger-init-file-name))
     95     (with-temp-buffer
     96       (insert-file-contents ledger-init-file-name)
     97       (setq ledger-environment-alist
     98             (ledger-init-parse-initialization (current-buffer))))))
     99 
    100 (provide 'ledger-init)
    101 
    102 ;;; ledger-init.el ends here