ledger-exec.el 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. ;;; ledger-exec.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. ;; Code for executing ledger synchronously.
  20. ;;; Code:
  21. (defconst ledger-version-needed "3.0.0"
  22. "The version of ledger executable needed for interactive features.")
  23. (defvar ledger-works nil
  24. "Flag showing whether the ledger binary can support `ledger-mode' interactive features.")
  25. (defgroup ledger-exec nil
  26. "Interface to the Ledger command-line accounting program."
  27. :group 'ledger)
  28. (defcustom ledger-mode-should-check-version t
  29. "Should Ledger-mode verify that the executable is working?"
  30. :type 'boolean
  31. :group 'ledger-exec)
  32. (defcustom ledger-binary-path "ledger"
  33. "Path to the ledger executable."
  34. :type 'file
  35. :group 'ledger-exec)
  36. (defun ledger-exec-handle-error (ledger-output)
  37. "Deal with ledger errors contained in LEDGER-OUTPUT."
  38. (with-current-buffer (get-buffer-create "*Ledger Error*")
  39. (insert-buffer-substring ledger-output)
  40. (view-mode)
  41. (setq buffer-read-only t)))
  42. (defun ledger-exec-success-p (ledger-output-buffer)
  43. "Return t if the ledger output in LEDGER-OUTPUT-BUFFER is successful."
  44. (with-current-buffer ledger-output-buffer
  45. (goto-char (point-min))
  46. (if (and (> (buffer-size) 1) (looking-at (regexp-quote "While")))
  47. nil ;; failure, there is an error starting with "While"
  48. ledger-output-buffer)))
  49. (defun ledger-exec-ledger (input-buffer &optional output-buffer &rest args)
  50. "Run Ledger using INPUT-BUFFER and optionally capturing output in OUTPUT-BUFFER with ARGS."
  51. (if (null ledger-binary-path)
  52. (error "The variable `ledger-binary-path' has not been set")
  53. (let ((buf (or input-buffer (current-buffer)))
  54. (outbuf (or output-buffer
  55. (generate-new-buffer " *ledger-tmp*"))))
  56. (with-current-buffer buf
  57. (let ((coding-system-for-write 'utf-8)
  58. (coding-system-for-read 'utf-8))
  59. (apply #'call-process-region
  60. (append (list (point-min) (point-max)
  61. ledger-binary-path nil outbuf nil "-f" "-")
  62. args)))
  63. (if (ledger-exec-success-p outbuf)
  64. outbuf
  65. (ledger-exec-handle-error outbuf))))))
  66. (defun ledger-version-greater-p (needed)
  67. "Verify the ledger binary is usable for `ledger-mode' (version greater than NEEDED)."
  68. (let ((buffer ledger-buf)
  69. (version-strings '()))
  70. (with-temp-buffer
  71. (when (ledger-exec-ledger (current-buffer) (current-buffer) "--version")
  72. (goto-char (point-min))
  73. (delete-horizontal-space)
  74. (setq version-strings (split-string
  75. (buffer-substring-no-properties (point)
  76. (point-max))))
  77. (if (and (string-match (regexp-quote "Ledger") (car version-strings))
  78. (or (string= needed (cadr version-strings))
  79. (string< needed (cadr version-strings))))
  80. t ;; success
  81. nil))))) ;;failure
  82. (defun ledger-check-version ()
  83. "Verify that ledger works and is modern enough."
  84. (interactive)
  85. (if ledger-mode-should-check-version
  86. (if (setq ledger-works (ledger-version-greater-p ledger-version-needed))
  87. (message "Good Ledger Version")
  88. (message "Bad Ledger Version"))))
  89. (provide 'ledger-exec)
  90. ;;; ledger-exec.el ends here