ledger-init.el 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. ;;; ledger-init.el --- Helper code for use with the "ledger" command-line tool
  2. ;; Copyright (C) 2003-2016 John Wiegley (johnw AT gnu DOT org)
  3. ;; This file is not part of GNU Emacs.
  4. ;; This is free software; you can redistribute it and/or modify it under
  5. ;; the terms of the GNU General Public License as published by the Free
  6. ;; Software Foundation; either version 2, or (at your option) any later
  7. ;; version.
  8. ;;
  9. ;; This is distributed in the hope that it will be useful, but WITHOUT
  10. ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. ;; for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  16. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17. ;; MA 02110-1301 USA.
  18. ;;; Commentary:
  19. ;; Determine the ledger environment
  20. (require 'ledger-regex)
  21. ;;; Code:
  22. (defcustom ledger-init-file-name "~/.ledgerrc"
  23. "Location of the ledger initialization file. nil if you don't have one."
  24. :group 'ledger-exec)
  25. (defvar ledger-environment-alist nil)
  26. (defvar ledger-default-date-format "%Y/%m/%d")
  27. (defun ledger-init-parse-initialization (buffer)
  28. "Parse the .ledgerrc file in BUFFER."
  29. (with-current-buffer buffer
  30. (let (environment-alist)
  31. (goto-char (point-min))
  32. (while (re-search-forward ledger-init-string-regex nil t )
  33. (let ((matchb (match-beginning 0)) ;; save the match data, string-match stamp on it
  34. (matche (match-end 0)))
  35. (end-of-line)
  36. (setq environment-alist
  37. (append environment-alist
  38. (list (cons (let ((flag (buffer-substring-no-properties (+ 2 matchb) matche)))
  39. (if (string-match "[ \t\n\r]+\\'" flag)
  40. (replace-match "" t t flag)
  41. flag))
  42. (let ((value (buffer-substring-no-properties matche (point) )))
  43. (if (> (length value) 0)
  44. value
  45. t))))))))
  46. environment-alist)))
  47. (defun ledger-init-load-init-file ()
  48. "Load and parse the .ledgerrc file."
  49. (interactive)
  50. (let ((init-base-name (file-name-nondirectory ledger-init-file-name)))
  51. (if (get-buffer init-base-name) ;; init file already loaded, parse it and leave it
  52. (setq ledger-environment-alist
  53. (ledger-init-parse-initialization init-base-name))
  54. (when (and ledger-init-file-name
  55. (file-exists-p ledger-init-file-name)
  56. (file-readable-p ledger-init-file-name))
  57. (find-file-noselect ledger-init-file-name)
  58. (setq ledger-environment-alist
  59. (ledger-init-parse-initialization init-base-name))
  60. (kill-buffer init-base-name)))))
  61. (provide 'ledger-init)
  62. ;;; ledger-init.el ends here