diff --git a/hledger-lib/Hledger/Data/Journal.hs b/hledger-lib/Hledger/Data/Journal.hs index 9c8264312..908f7d124 100644 --- a/hledger-lib/Hledger/Data/Journal.hs +++ b/hledger-lib/Hledger/Data/Journal.hs @@ -199,7 +199,9 @@ instance Show Journal where -- Note that (<>) is right-biased, so nulljournal is only a left identity. -- In particular, this prevents Journal from being a monoid. instance Semigroup Journal where - j1 <> j2 = Journal { + j1 <> j2 = + journalRenumberAccountDeclarations $ + Journal { jparsedefaultyear = jparsedefaultyear j2 ,jparsedefaultcommodity = jparsedefaultcommodity j2 ,jparsedecimalmark = jparsedecimalmark j2 @@ -226,6 +228,14 @@ instance Semigroup Journal where ,jlastreadtime = max (jlastreadtime j1) (jlastreadtime j2) } +-- | Renumber all the account declarations. Call this after combining two journals into one, +-- so that account declarations have a total order again. +journalRenumberAccountDeclarations :: Journal -> Journal +journalRenumberAccountDeclarations j = j{jdeclaredaccounts=jdas'} + where + jdas' = [(a, adi{adideclarationorder=n}) | (n, (a,adi)) <- zip [1..] $ jdeclaredaccounts j] + -- XXX the per-file declaration order saved during parsing is discarded; it seems unneeded + instance Default Journal where def = nulljournal diff --git a/hledger-lib/Hledger/Read/JournalReader.hs b/hledger-lib/Hledger/Read/JournalReader.hs index 6377a9835..d28d4db6a 100644 --- a/hledger-lib/Hledger/Read/JournalReader.hs +++ b/hledger-lib/Hledger/Read/JournalReader.hs @@ -401,6 +401,9 @@ addAccountDeclaration (a,cmt,tags) = adicomment = cmt ,aditags = tags ,adideclarationorder = length decls + 1 + -- this restarts from 1 in each file, which is not that useful + -- when there are multiple files; so it gets renumbered + -- automatically when combining Journals with <> }) in j{jdeclaredaccounts = d:decls})