From 6924e8390cdf3796799ed74756b95e071432e8ef Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Wed, 27 Jun 2018 11:26:08 +0100 Subject: [PATCH] lib: journal: let alias/apply account affect account directives (#825) --- hledger-lib/Hledger/Read/Common.hs | 4 +++- hledger-lib/Hledger/Read/JournalReader.hs | 2 +- hledger-lib/hledger_journal.m4.md | 7 +++++++ tests/journal/accounts.test | 19 +++++++++++++++++++ 4 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 tests/journal/accounts.test diff --git a/hledger-lib/Hledger/Read/Common.hs b/hledger-lib/Hledger/Read/Common.hs index d1fe031c5..e75058d8d 100644 --- a/hledger-lib/Hledger/Read/Common.hs +++ b/hledger-lib/Hledger/Read/Common.hs @@ -459,7 +459,9 @@ secondarydatep primaryDate = char '=' *> datep' (Just primaryYear) --- ** account names --- | Parse an account name, then apply any parent account prefix and/or account aliases currently in effect. +-- | Parse an account name (plus one following space if present), +-- then apply any parent account prefix and/or account aliases currently in effect, +-- in that order. (Ie first add the parent account prefix, then rewrite with aliases). modifiedaccountnamep :: JournalParser m AccountName modifiedaccountnamep = do parent <- getParentAccount diff --git a/hledger-lib/Hledger/Read/JournalReader.hs b/hledger-lib/Hledger/Read/JournalReader.hs index d6cdb9f56..f2a18d495 100644 --- a/hledger-lib/Hledger/Read/JournalReader.hs +++ b/hledger-lib/Hledger/Read/JournalReader.hs @@ -248,7 +248,7 @@ accountdirectivep :: JournalParser m () accountdirectivep = do string "account" lift (skipSome spacenonewline) - acct <- lift accountnamep -- eats single spaces + acct <- modifiedaccountnamep -- account directives can be modified by alias/apply account macode' :: Maybe String <- (optional $ lift $ skipSome spacenonewline >> some digitChar) let macode :: Maybe AccountCode = read <$> macode' newline diff --git a/hledger-lib/hledger_journal.m4.md b/hledger-lib/hledger_journal.m4.md index dc1769208..865e43116 100644 --- a/hledger-lib/hledger_journal.m4.md +++ b/hledger-lib/hledger_journal.m4.md @@ -928,6 +928,9 @@ You can clear (forget) all currently defined aliases with the `end aliases` dire end aliases ``` +Account aliases also affect [account directives](#declaring-accounts). +They do not affect account names being entered via hledger add or hledger-web. + ### Default parent account You can specify a parent account which will be prepended to all accounts @@ -963,6 +966,10 @@ include personal.journal Prior to hledger 1.0, legacy `account` and `end` spellings were also supported. +A default parent account also affects [account directives](#declaring-accounts). +It does not affect account names being entered via hledger add or hledger-web. +If account aliases are present, they are applied after the default parent account. + ## Periodic transactions Periodic transaction rules describe transactions that recur. diff --git a/tests/journal/accounts.test b/tests/journal/accounts.test new file mode 100644 index 000000000..5f87bcef5 --- /dev/null +++ b/tests/journal/accounts.test @@ -0,0 +1,19 @@ +# account names + +# 1. account directives are affected by aliases and apply account. +< +apply account c +alias c:a=b +account a +$ hledger -f - accounts +b + + + +# TODO +# a trailing : should give a clear error +# 2009/1/1 +# a: 1 +# $ hledger -f - print +# >2 /accountname seems ill-formed: a:/ +# >= 1