Skip to content

Commit 5a4bab4

Browse files
ddimitrovdrdv
authored andcommitted
Add functionality for transaction folding
1 parent 356d804 commit 5a4bab4

File tree

2 files changed

+120
-1
lines changed

2 files changed

+120
-1
lines changed

ledger-occur.el

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
(require 'cl-lib)
3333
(require 'ledger-navigate)
34+
(require 'hideshow)
3435

3536
(defconst ledger-occur-overlay-property-name 'ledger-occur-custom-buffer-grep)
3637

@@ -119,7 +120,9 @@ long, otherwise it is the word at point."
119120
"Make an overlay for an invisible portion of the buffer, from BEG to END."
120121
(let ((ovl (make-overlay beg end)))
121122
(overlay-put ovl ledger-occur-overlay-property-name t)
122-
(overlay-put ovl 'invisible t)))
123+
(overlay-put ovl 'invisible t)
124+
;; required to not display ... when using together with transaction folding
125+
(overlay-put ovl 'display "")))
123126

124127
(defun ledger-occur-create-overlays (ovl-bounds)
125128
"Create the overlays for the visible transactions.
@@ -173,6 +176,78 @@ BUFFER-MATCHES should be a list of (BEG END) lists."
173176
(setq current-end (cadr match))))
174177
(nreverse (push (list current-beginning current-end) points)))))
175178

179+
;; -----------------------------------------------------------------------------
180+
;; transactions folding
181+
;; FIXME: probably this is not the best file to include this functionality.
182+
;; -----------------------------------------------------------------------------
183+
(defvar-local ledger-mode-folding-transactions-hidden nil
184+
"Whether transactions are globally hidden or not.")
185+
186+
(defvar-local ledger-mode-toggle-invisible-transactions t
187+
"Toggle invisible transactions (see ledger-occur-mode).")
188+
189+
(defun ledger-mode-transaction-toggle-folding ()
190+
"Toggle hiding of transaction block under point.
191+
A transaction block is identified as in ledger-highlight-xact-under-point.
192+
193+
Overlay of type `code' is used so that hidden blocks are
194+
temporarily opened when doing incremental search."
195+
(interactive)
196+
(if (not (and (boundp 'hs-minor-mode) hs-minor-mode))
197+
(message "Enable hs-minor-mode to use this functionality.")
198+
(let ((exts (ledger-navigate-find-element-extents (point))))
199+
(let ((b (car exts))
200+
(e (cadr exts))
201+
(p (point)))
202+
(when (and (> (- e b) 1) ; not an empty line
203+
(<= p e) (>= p b) ; point is within the boundaries
204+
(not (region-active-p))) ; no active region
205+
(goto-char b)
206+
(save-excursion
207+
(goto-char (line-end-position))
208+
(if (hs-overlay-at (point)) ;; if transaction is hidden show it
209+
(progn
210+
(save-excursion (hs-show-block))
211+
(goto-char b))
212+
(goto-char b)
213+
(hs-discard-overlays (line-end-position) e)
214+
(hs-make-overlay (line-end-position) e 'code)
215+
(run-hooks 'hs-hide-hook))))))))
216+
217+
(defun ledger-mode-request-toggle-transaction-hiding-p ()
218+
"Decide whether to request transaction folding.
219+
Assume that point is at the first transaction delimiter."
220+
(goto-char (line-end-position))
221+
(let ((ov-hs (hs-overlay-at (point)))
222+
(ovs (overlays-at (point)))
223+
(to_request t))
224+
(when (not (eq (not ledger-mode-folding-transactions-hidden)
225+
(overlayp ov-hs)))
226+
;; handle behavior in invisible regions (see ledger-occur-mode)
227+
(when (not ledger-mode-toggle-invisible-transactions)
228+
(dolist (ov ovs)
229+
(when (and (overlay-get ov 'invisible)
230+
(overlay-get ov ledger-occur-overlay-property-name))
231+
(setq to_request nil))))
232+
to_request)))
233+
234+
(defun ledger-mode-folding-toggle-transactions ()
235+
"Toggle hiding of all transactions in the current buffer."
236+
(interactive)
237+
(if (not (and (boundp 'hs-minor-mode) hs-minor-mode))
238+
(message "Enable hs-minor-mode to use this functionality.")
239+
(hs-life-goes-on
240+
(save-excursion
241+
(goto-char (point-min))
242+
(while (re-search-forward "^[=~[:digit:]]" nil t)
243+
(beginning-of-line)
244+
(when (ledger-mode-request-toggle-transaction-hiding-p)
245+
(ledger-mode-transaction-toggle-folding)
246+
(goto-char (line-end-position))))
247+
(setq ledger-mode-folding-transactions-hidden
248+
(not ledger-mode-folding-transactions-hidden))))))
249+
;; -----------------------------------------------------------------------------
250+
176251
(provide 'ledger-occur)
177252

178253
;;; ledger-occur.el ends here

test/occur-test.el

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,50 @@ https://github.com/ledger/ledger-mode/issues/415"
181181
Income:Salary
182182
"))))
183183

184+
(ert-deftest ledger-occur/test-005 ()
185+
"Test transaction folding"
186+
:tags '(transaction folding)
187+
188+
(ledger-tests-with-temp-file
189+
"2011/01/02 Grocery Store
190+
Expenses:Food:Groceries $ 65.00
191+
* Assets:Checking
192+
193+
2011/01/05 Employer
194+
* Assets:Checking $ 2000.00
195+
Income:Salary
196+
"
197+
(progn
198+
(hs-minor-mode t)
199+
(ledger-mode-folding-toggle-transactions))
200+
(should
201+
(equal (ledger-test-visible-buffer-string)
202+
"2011/01/02 Grocery Store
203+
204+
2011/01/05 Employer
205+
"))))
206+
207+
(ert-deftest ledger-occur/test-006 ()
208+
"Test transaction folding together with ledger-occur-mode"
209+
:tags '(transaction folding)
210+
211+
(ledger-tests-with-temp-file
212+
"2011/01/02 Grocery Store
213+
Expenses:Food:Groceries $ 65.00
214+
* Assets:Checking
215+
216+
2011/01/05 Employer
217+
* Assets:Checking $ 2000.00
218+
Income:Salary
219+
"
220+
(progn
221+
(hs-minor-mode t)
222+
(ledger-occur "Groceries")
223+
(ledger-mode-folding-toggle-transactions))
224+
(should
225+
(equal (ledger-test-visible-buffer-string)
226+
"2011/01/02 Grocery Store
227+
"))))
184228

185229
(provide 'occur-test)
186230

0 commit comments

Comments
 (0)