Created
January 26, 2019 10:31
-
-
Save stettberger/aa991777b778290c9e5956a2820b91f4 to your computer and use it in GitHub Desktop.
ledger-account-rename
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun ledger-account--rename (account-old account-new) | |
"Rename account in ledger file." | |
(let ((count 0)) | |
(beginning-of-buffer) | |
(while (not (eobp)) | |
(let* ((left-point (point)) | |
(next-change | |
(or (next-single-property-change (point) 'face (current-buffer)) | |
(point-max))) | |
(face (face-at-point left-point)) | |
(right-point next-change)) | |
;; Are we looking at Account Name? | |
(when (member face '(ledger-font-posting-account-face | |
ledger-font-account-name-face)) | |
;; Virtual Posting? | |
(when (member (string (char-after left-point)) '("(" "[")) | |
(incf left-point) | |
(decf right-point)) | |
;; Replace Account Name | |
(when (string-equal account-old | |
(buffer-substring left-point right-point)) | |
(delete-region left-point right-point) | |
(insert account-new) | |
(incf count))) | |
(goto-char next-change))) | |
(message "Renamed at %s places (%s -> %s)" count account-old account-new))) | |
(defun ledger-account-rename () | |
"Command to rename accounts in current ledger buffer" | |
(interactive) | |
(let* ((account-old (completing-read "Old account> " | |
(ledger-accounts-list-in-buffer))) | |
(account-new (read-from-minibuffer | |
"New account name> " | |
account-old))) | |
(save-excursion | |
(ledger-account--rename account-old account-new)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment