config

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

ledger-init.el (3577B)


      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)
     57   "Format DATE according to the current preferred date format.
     58 Returns the current date if DATE is nil or not supplied."
     59   (format-time-string
     60    (or (cdr (assoc "input-date-format" ledger-environment-alist))
     61        ledger-default-date-format)
     62    date))
     63 
     64 
     65 (defun ledger-init-parse-initialization (buffer)
     66   "Parse the .ledgerrc file in BUFFER."
     67   (with-current-buffer buffer
     68     (let (environment-alist)
     69       (goto-char (point-min))
     70       (while (re-search-forward ledger-init-string-regex nil t)
     71         (let ((matchb (match-beginning 0)) ;; save the match data, string-match stamp on it
     72               (matche (match-end 0)))
     73           (end-of-line)
     74           (push (cons (let ((flag (buffer-substring-no-properties (+ 2 matchb) matche)))
     75                         (if (string-match "[ \t\n\r]+\\'" flag)
     76                             (replace-match "" t t flag)
     77                           flag))
     78                       (let ((value (buffer-substring-no-properties matche (point))))
     79                         (if (> (length value) 0)
     80                             value
     81                           t)))
     82                 environment-alist)))
     83       (nreverse environment-alist))))
     84 
     85 (defun ledger-init-load-init-file ()
     86   "Load and parse the .ledgerrc file into `ledger-environment-alist'."
     87   (interactive)
     88   (when (and ledger-init-file-name
     89              (file-readable-p ledger-init-file-name))
     90     (with-temp-buffer
     91       (insert-file-contents ledger-init-file-name)
     92       (setq ledger-environment-alist
     93             (ledger-init-parse-initialization (current-buffer))))))
     94 
     95 (provide 'ledger-init)
     96 
     97 ;;; ledger-init.el ends here