[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/csv2ledger c558f2bf40 176/190: Add alternative account cre
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/csv2ledger c558f2bf40 176/190: Add alternative account creation function c2l-create-account-ask-matcher |
Date: |
Sun, 2 Jun 2024 15:59:56 -0400 (EDT) |
branch: elpa/csv2ledger
commit c558f2bf408f539ff16873259b20d69408aa5332
Author: Joost Kremers <joostkremers@fastmail.com>
Commit: Joost Kremers <joostkremers@fastmail.com>
Add alternative account creation function c2l-create-account-ask-matcher
---
README.md | 4 ++++
csv2ledger.el | 40 ++++++++++++++++++++++++++++++++++++++++
2 files changed, 44 insertions(+)
diff --git a/README.md b/README.md
index 4b2ee1849f..827404ad1a 100644
--- a/README.md
+++ b/README.md
@@ -229,6 +229,10 @@ Another important point to note is that the amount in the
`amount` field must be
The third function in `c2l-transaction-modify-functions` is
`c2l-create-account`. This is the function that checks the fields of the
transaction against the account matchers, and if one is not found, uses
`c2l-fallback-account` or asks the user. If you wish to use a different method
to set the account, you can replace this function with a custom one. It needs
to add an `account` field to the transaction, but there are no restrictions on
how the account is determined.
+`csv2ledger` provides an alternative function for creating an account:
`c2l-create-account-ask-matcher`. It also checks the transaction against the
account matchers, but if no match is found, `c2l-fallback-account` is not used.
Instead, you are asked for an account and also whether you want to add a
matcher for it. If you say yes, you are asked to provide a string to match
against. The new matcher is added to your account matchers file and is also
immediately available for further transa [...]
+
+If you want to use this function, remove `c2l-create-account` from
`c2l-transaction-modify-functions` and replace it with
`c2l-create-account-ask-matcher` to it.
+
### Creating the entry ###
diff --git a/csv2ledger.el b/csv2ledger.el
index 3cdda3224f..8ae49e91af 100644
--- a/csv2ledger.el
+++ b/csv2ledger.el
@@ -345,6 +345,46 @@ method yields an account, ask the user."
(push (cons 'account account) transaction)
transaction))
+(defun c2l-create-account-ask-matcher (transaction)
+ "Get the balancing account for TRANSACTION.
+First check if the account matchers provide a balancing account.
+If not, ask the user for an account and ask if they want to
+create an account matcher for it."
+ (let ((account (seq-some #'c2l--match-account
+ (mapcar #'cdr
+ (seq-filter (lambda (e) (memq (car e)
c2l-target-match-fields))
+ transaction)))))
+ (when (not account)
+ (setq account (completing-read (format "Account for transaction %s, %s
«%.75s» "
+ (alist-get 'title transaction
"Unknown payee")
+ (alist-get 'amount transaction
"0.00")
+ (alist-get 'description
transaction "?"))
+ c2l--accounts))
+ (if (y-or-n-p "Add a matcher for this account? ")
+ ;; If the user changes their mind, we still want to continue
+ ;; processing the transaction, so we capture quit.
+ (condition-case nil
+ (let ((matcher (read-string (format "Matcher for «%s»: "
account))))
+ (when (and matcher (not (string-empty-p matcher)))
+ (c2l-add-matcher matcher account c2l-account-matchers-file)
+ (push (cons (regexp-opt (list matcher)) account)
c2l-matcher-regexps)))
+ (quit nil))))
+ (push (cons 'account account) transaction)
+ transaction))
+
+(defun c2l-add-matcher (matcher account file)
+ "Add MATCHER to FILE.
+MATCHER will be linked to ACCOUNT."
+ (if (not (and (stringp file)
+ (file-writable-p file)))
+ (user-error "[Csv2Ledger] Cannot write to account matchers file `%s'"
file)
+ (with-temp-buffer
+ (insert-file-contents file)
+ (goto-char (point-max))
+ (ensure-empty-lines 0) ; Make sure we're on a new, empty line.
+ (insert (format "%s\t%s\n" matcher account))
+ (write-region (point-min) (point-max) file))))
+
(defun c2l-compose-entry (transaction)
"Create a ledger entry.
TRANSACTION is an alist containing (key . value) pairs that will
- [nongnu] elpa/csv2ledger 4f6bb9e1bd 184/190: Update example function jk/c2l-convert-amount in the README, (continued)
- [nongnu] elpa/csv2ledger 4f6bb9e1bd 184/190: Update example function jk/c2l-convert-amount in the README, ELPA Syncer, 2024/06/03
- [nongnu] elpa/csv2ledger be1366e6d4 031/190: c2l-payee-or-sender: first check if `c2l-account-holder` is even set., ELPA Syncer, 2024/06/03
- [nongnu] elpa/csv2ledger eaf2ad4fee 028/190: Do not test if file exists if file is nil., ELPA Syncer, 2024/06/03
- [nongnu] elpa/csv2ledger ac7abd896f 092/190: Guard against empty value of counterpart., ELPA Syncer, 2024/06/03
- [nongnu] elpa/csv2ledger d5f5712969 083/190: Add comment indicating interactive functions., ELPA Syncer, 2024/06/03
- [nongnu] elpa/csv2ledger 6fa69d4391 089/190: Rename regexes to regexps in the source., ELPA Syncer, 2024/06/03
- [nongnu] elpa/csv2ledger e08f335190 091/190: Change fallback value of c2l-title-is-payee-to-sender., ELPA Syncer, 2024/06/03
- [nongnu] elpa/csv2ledger 1048cf77d9 095/190: Update the README., ELPA Syncer, 2024/06/03
- [nongnu] elpa/csv2ledger 8f1c8fa2b6 090/190: Guard for the case where payee or sender does not exist., ELPA Syncer, 2024/06/03
- [nongnu] elpa/csv2ledger 95e107e9bd 123/190: Nicify the README a bit., ELPA Syncer, 2024/06/03
- [nongnu] elpa/csv2ledger c558f2bf40 176/190: Add alternative account creation function c2l-create-account-ask-matcher,
ELPA Syncer <=
- [nongnu] elpa/csv2ledger bc57cfd9ca 093/190: Remove `counterpart` field., ELPA Syncer, 2024/06/03
- [nongnu] elpa/csv2ledger a2479be0d5 085/190: Update doc string of c2l--csv-line-to-ledger., ELPA Syncer, 2024/06/03
- [nongnu] elpa/csv2ledger 58d45b6ca2 122/190: Update documentation., ELPA Syncer, 2024/06/03
- [nongnu] elpa/csv2ledger d1639d9c59 115/190: Add long description to the file header., ELPA Syncer, 2024/06/03
- [nongnu] elpa/csv2ledger 7c84259a9d 042/190: Update error messages with "[Csv2Ledger]" tag., ELPA Syncer, 2024/06/03
- [nongnu] elpa/csv2ledger b5a85b4589 045/190: Remove defvar declaration for ledger-post-amount-alignment-column., ELPA Syncer, 2024/06/03
- [nongnu] elpa/csv2ledger 150995f5c4 075/190: Fix doc strings of c2l-csv-columns and c2l-account-matchers-file., ELPA Syncer, 2024/06/03
- [nongnu] elpa/csv2ledger e4a823e552 071/190: Add helper function c2l--amount-p., ELPA Syncer, 2024/06/03
- [nongnu] elpa/csv2ledger 71e39431eb 078/190: Make c2l-title-function and c2l-amount-function dependent on c2l-csv-columns., ELPA Syncer, 2024/06/03
- [nongnu] elpa/csv2ledger 8c3c0fb0f3 077/190: Swap car and cdr of c2l-matcher-regexes., ELPA Syncer, 2024/06/03