|
31 | 31 |
|
32 | 32 | (require 'cl-lib)
|
33 | 33 | (require 'ledger-navigate)
|
| 34 | +(require 'hideshow) |
34 | 35 |
|
35 | 36 | (defconst ledger-occur-overlay-property-name 'ledger-occur-custom-buffer-grep)
|
36 | 37 |
|
@@ -119,7 +120,9 @@ long, otherwise it is the word at point."
|
119 | 120 | "Make an overlay for an invisible portion of the buffer, from BEG to END."
|
120 | 121 | (let ((ovl (make-overlay beg end)))
|
121 | 122 | (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 ""))) |
123 | 126 |
|
124 | 127 | (defun ledger-occur-create-overlays (ovl-bounds)
|
125 | 128 | "Create the overlays for the visible transactions.
|
@@ -173,6 +176,78 @@ BUFFER-MATCHES should be a list of (BEG END) lists."
|
173 | 176 | (setq current-end (cadr match))))
|
174 | 177 | (nreverse (push (list current-beginning current-end) points)))))
|
175 | 178 |
|
| 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 | + |
176 | 251 | (provide 'ledger-occur)
|
177 | 252 |
|
178 | 253 | ;;; ledger-occur.el ends here
|
0 commit comments