[emacs] Turn ledger-occur-mode off via a separate command, and allow refreshing

This commit is contained in:
Steve Purcell 2014-12-10 09:37:20 +00:00
parent 637679c0cb
commit e16e601435

View file

@ -48,27 +48,43 @@
"Pattern currently applied to narrow the buffer.") "Pattern currently applied to narrow the buffer.")
(make-variable-buffer-local 'ledger-occur-current-regex) (make-variable-buffer-local 'ledger-occur-current-regex)
(defvar ledger-occur-mode-map (make-sparse-keymap))
(define-minor-mode ledger-occur-mode (define-minor-mode ledger-occur-mode
"A minor mode which display only transactions matching `ledger-occur-current-regex'." "A minor mode which display only transactions matching `ledger-occur-current-regex'."
nil (:eval (format " Ledger-Narrow(%s)" ledger-occur-current-regex)) nil nil
(:eval (format " Ledger-Narrow(%s)" ledger-occur-current-regex))
ledger-occur-mode-map
(if ledger-occur-mode (if ledger-occur-mode
(let ((matches (ledger-occur-compress-matches (ledger-occur-refresh)
(ledger-occur-find-matches ledger-occur-current-regex))))
(unless matches
(error "No matches found for '%s'" ledger-occur-current-regex))
(ledger-occur-create-overlays matches))
(ledger-occur-remove-overlays))) (ledger-occur-remove-overlays)))
(defun ledger-occur-quit ()
"Disable `ledger-occur-mode', turning off narrowing."
(interactive)
(ledger-occur-mode -1))
(define-key ledger-occur-mode-map (kbd "C-c C-f") 'ledger-occur-quit)
(define-key ledger-occur-mode-map (kbd "C-c C-g") 'ledger-occur-refresh)
(defun ledger-occur-refresh ()
"Re-apply the current narrowing expression."
(interactive)
(let ((matches (ledger-occur-compress-matches
(ledger-occur-find-matches ledger-occur-current-regex))))
(if matches
(ledger-occur-create-overlays matches)
(message "No matches found for '%s'" ledger-occur-current-regex)
(ledger-occur-mode -1))))
(defun ledger-occur (regex) (defun ledger-occur (regex)
"Show only transactions in the current buffer which match REGEX. "Show only transactions in the current buffer which match REGEX.
This command hides all xact in the current buffer except those This command hides all xact in the current buffer except those
matching REGEX. When called interactively, a second call of the matching REGEX. If REGEX is nil or empty, turn off any narrowing
function redisplays the hidden transactions." currently active."
(interactive (interactive
(if ledger-occur-mode (list (read-regexp "Regexp" (ledger-occur-prompt) 'ledger-occur-history)))
(list nil)
(list (read-regexp "Regexp" (ledger-occur-prompt) 'ledger-occur-history))))
(if (or (null regex) (if (or (null regex)
(zerop (length regex))) ; empty regex, or already have narrowed, clear narrowing (zerop (length regex))) ; empty regex, or already have narrowed, clear narrowing
(ledger-occur-mode -1) (ledger-occur-mode -1)