refactor(beancount): +beancount/clone-transaction

This commit is contained in:
Henrik Lissner 2022-04-03 20:42:22 +02:00
parent c309e61eff
commit 86a8b41e37
No known key found for this signature in database
GPG key ID: B60957CA074D39A3

View file

@ -127,24 +127,20 @@ If REVERSE (the prefix arg) is non-nil, sort the transactions in reverst order."
(beancount--run "bean-report" buffer-file-name "bal"))) (beancount--run "bean-report" buffer-file-name "bal")))
;;;###autoload ;;;###autoload
(defun +beancount/clone-transaction (transaction) (defun +beancount/clone-transaction ()
"TODO" "TODO"
;; TODO Ew. Refactor me! (interactive)
(interactive
(save-restriction (save-restriction
(widen) (widen)
(list (completing-read (when-let (transaction
(completing-read
"Clone transaction: " "Clone transaction: "
(nreverse (string-lines (buffer-string))
(cl-remove-if-not (doom-partial #'string-match-p "^[0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [*!] ")
(lambda (line) t))
(string-match-p "^[0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [*!] \"" line))
(split-string (substring-no-properties (buffer-string))
"\n")))))))
(when transaction
(goto-char (point-min)) (goto-char (point-min))
(re-search-forward (concat "^" (regexp-quote transaction))) (re-search-forward (concat "^" (regexp-quote transaction)))
(+beancount/clone-this-transaction t))) (+beancount/clone-this-transaction t))))
;;;###autoload ;;;###autoload
(defun +beancount/clone-this-transaction (&optional arg) (defun +beancount/clone-this-transaction (&optional arg)
@ -153,6 +149,8 @@ Updates the date to today"
(interactive "P") (interactive "P")
(if (and (not arg) (looking-at-p "^$")) (if (and (not arg) (looking-at-p "^$"))
(call-interactively #'+beancount/clone-transaction) (call-interactively #'+beancount/clone-transaction)
(save-restriction
(widen)
(let ((transaction (let ((transaction
(buffer-substring-no-properties (buffer-substring-no-properties
(save-excursion (save-excursion
@ -165,4 +163,4 @@ Updates the date to today"
(goto-char (point-max)) (goto-char (point-max))
(delete-blank-lines) (delete-blank-lines)
(beancount-insert-date) (beancount-insert-date)
(insert transaction)))) (insert transaction)))))