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")))
;;;###autoload
(defun +beancount/clone-transaction (transaction)
(defun +beancount/clone-transaction ()
"TODO"
;; TODO Ew. Refactor me!
(interactive
(save-restriction
(widen)
(list (completing-read
"Clone transaction: "
(nreverse
(cl-remove-if-not
(lambda (line)
(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))
(re-search-forward (concat "^" (regexp-quote transaction)))
(+beancount/clone-this-transaction t)))
(interactive)
(save-restriction
(widen)
(when-let (transaction
(completing-read
"Clone transaction: "
(string-lines (buffer-string))
(doom-partial #'string-match-p "^[0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [*!] ")
t))
(goto-char (point-min))
(re-search-forward (concat "^" (regexp-quote transaction)))
(+beancount/clone-this-transaction t))))
;;;###autoload
(defun +beancount/clone-this-transaction (&optional arg)
@ -153,16 +149,18 @@ Updates the date to today"
(interactive "P")
(if (and (not arg) (looking-at-p "^$"))
(call-interactively #'+beancount/clone-transaction)
(let ((transaction
(buffer-substring-no-properties
(save-excursion
(beancount-goto-transaction-begin)
(re-search-forward " " nil t)
(point))
(save-excursion
(beancount-goto-transaction-end)
(point)))))
(goto-char (point-max))
(delete-blank-lines)
(beancount-insert-date)
(insert transaction))))
(save-restriction
(widen)
(let ((transaction
(buffer-substring-no-properties
(save-excursion
(beancount-goto-transaction-begin)
(re-search-forward " " nil t)
(point))
(save-excursion
(beancount-goto-transaction-end)
(point)))))
(goto-char (point-max))
(delete-blank-lines)
(beancount-insert-date)
(insert transaction)))))