ledger-xact.el 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. ;;; ledger-xact.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. ;; Utilities for running ledger synchronously.
  20. ;;; Code:
  21. (require 'eshell)
  22. (require 'ledger-regex)
  23. (require 'ledger-navigate)
  24. ;; TODO: This file depends on code in ledger-mode.el, which depends on this.
  25. (defcustom ledger-highlight-xact-under-point t
  26. "If t highlight xact under point."
  27. :type 'boolean
  28. :group 'ledger)
  29. (defcustom ledger-use-iso-dates nil
  30. "If non-nil, use the iso-8601 format for dates (YYYY-MM-DD)."
  31. :type 'boolean
  32. :group 'ledger
  33. :safe t)
  34. (defvar ledger-xact-highlight-overlay (list))
  35. (make-variable-buffer-local 'ledger-xact-highlight-overlay)
  36. (defun ledger-highlight-xact-under-point ()
  37. "Move the highlight overlay to the current transaction."
  38. (if ledger-highlight-xact-under-point
  39. (let ((exts (ledger-navigate-find-element-extents (point)))
  40. (ovl ledger-xact-highlight-overlay))
  41. (if (not ledger-xact-highlight-overlay)
  42. (setq ovl
  43. (setq ledger-xact-highlight-overlay
  44. (make-overlay (car exts)
  45. (cadr exts)
  46. (current-buffer) t nil)))
  47. (move-overlay ovl (car exts) (cadr exts)))
  48. (overlay-put ovl 'face 'ledger-font-xact-highlight-face)
  49. (overlay-put ovl 'priority '(nil . 99)))))
  50. (defun ledger-xact-payee ()
  51. "Return the payee of the transaction containing point or nil."
  52. (let ((i 0))
  53. (while (eq (ledger-context-line-type (ledger-context-other-line i)) 'acct-transaction)
  54. (setq i (- i 1)))
  55. (let ((context-info (ledger-context-other-line i)))
  56. (if (eq (ledger-context-line-type context-info) 'xact)
  57. (ledger-context-field-value context-info 'payee)
  58. nil))))
  59. (defun ledger-time-less-p (t1 t2)
  60. "Say whether time value T1 is less than time value T2."
  61. (or (< (car t1) (car t2))
  62. (and (= (car t1) (car t2))
  63. (< (nth 1 t1) (nth 1 t2)))))
  64. (defun ledger-xact-find-slot (moment)
  65. "Find the right place in the buffer for a transaction at MOMENT.
  66. MOMENT is an encoded date"
  67. (let (last-xact-start)
  68. (catch 'found
  69. (ledger-xact-iterate-transactions
  70. (function
  71. (lambda (start date mark desc)
  72. (setq last-xact-start start)
  73. (if (ledger-time-less-p moment date)
  74. (throw 'found t))))))
  75. (when (and (eobp) last-xact-start)
  76. (let ((end (cadr (ledger-navigate-find-xact-extents last-xact-start))))
  77. (goto-char end)
  78. (insert "\n")
  79. (forward-line)))))
  80. (defun ledger-xact-iterate-transactions (callback)
  81. "Iterate through each transaction call CALLBACK for each."
  82. (goto-char (point-min))
  83. (let* ((now (current-time))
  84. (current-year (nth 5 (decode-time now))))
  85. (while (not (eobp))
  86. (when (looking-at ledger-iterate-regex)
  87. (let ((found-y-p (match-string 2)))
  88. (if found-y-p
  89. (setq current-year (string-to-number found-y-p)) ;; a Y directive was found
  90. (let ((start (match-beginning 0))
  91. (year (match-string 4))
  92. (month (string-to-number (match-string 5)))
  93. (day (string-to-number (match-string 6)))
  94. (mark (match-string 7))
  95. (code (match-string 8))
  96. (desc (match-string 9)))
  97. (if (and year (> (length year) 0))
  98. (setq year (string-to-number year)))
  99. (funcall callback start
  100. (encode-time 0 0 0 day month
  101. (or year current-year))
  102. mark desc)))))
  103. (forward-line))))
  104. (defun ledger-year-and-month ()
  105. (let ((sep (if ledger-use-iso-dates
  106. "-"
  107. "/")))
  108. (concat ledger-year sep ledger-month sep)))
  109. (defun ledger-copy-transaction-at-point (date)
  110. "Ask for a new DATE and copy the transaction under point to that date. Leave point on the first amount."
  111. (interactive (list
  112. (ledger-read-date "Copy to date: ")))
  113. (let* ((here (point))
  114. (extents (ledger-navigate-find-xact-extents (point)))
  115. (transaction (buffer-substring-no-properties (car extents) (cadr extents)))
  116. encoded-date)
  117. (if (string-match ledger-iso-date-regexp date)
  118. (setq encoded-date
  119. (encode-time 0 0 0 (string-to-number (match-string 4 date))
  120. (string-to-number (match-string 3 date))
  121. (string-to-number (match-string 2 date)))))
  122. (ledger-xact-find-slot encoded-date)
  123. (insert transaction "\n")
  124. (beginning-of-line -1)
  125. (ledger-navigate-beginning-of-xact)
  126. (re-search-forward ledger-iso-date-regexp)
  127. (replace-match date)
  128. (ledger-next-amount)
  129. (if (re-search-forward "[-0-9]")
  130. (goto-char (match-beginning 0)))))
  131. (defun ledger-delete-current-transaction (pos)
  132. "Delete the transaction surrounging POS."
  133. (interactive "d")
  134. (let ((bounds (ledger-navigate-find-xact-extents pos)))
  135. (delete-region (car bounds) (cadr bounds))))
  136. (defun ledger-add-transaction (transaction-text &optional insert-at-point)
  137. "Use ledger xact TRANSACTION-TEXT to add a transaction to the buffer.
  138. If INSERT-AT-POINT is non-nil insert the transaction there,
  139. otherwise call `ledger-xact-find-slot' to insert it at the
  140. correct chronological place in the buffer."
  141. (interactive (list
  142. ;; Note: This isn't "just" the date - it can contain
  143. ;; other text too
  144. (ledger-read-date "Transaction: ")))
  145. (let* ((args (with-temp-buffer
  146. (insert transaction-text)
  147. (eshell-parse-arguments (point-min) (point-max))))
  148. (ledger-buf (current-buffer))
  149. exit-code)
  150. (unless insert-at-point
  151. (let ((date (car args)))
  152. (if (string-match ledger-iso-date-regexp date)
  153. (setq date
  154. (encode-time 0 0 0 (string-to-number (match-string 4 date))
  155. (string-to-number (match-string 3 date))
  156. (string-to-number (match-string 2 date)))))
  157. (ledger-xact-find-slot date)))
  158. (if (> (length args) 1)
  159. (save-excursion
  160. (insert
  161. (with-temp-buffer
  162. (setq exit-code
  163. (apply #'ledger-exec-ledger ledger-buf (current-buffer) "xact"
  164. (mapcar 'eval args)))
  165. (goto-char (point-min))
  166. (if (looking-at "Error: ")
  167. (error (concat "Error in ledger-add-transaction: " (buffer-string)))
  168. (ledger-post-align-postings (point-min) (point-max))
  169. (buffer-string)))
  170. "\n"))
  171. (progn
  172. (insert (car args) " \n\n")
  173. (end-of-line -1)))))
  174. (provide 'ledger-xact)
  175. ;;; ledger-xact.el ends here