diff --git a/hledger-lib/Hledger/Data/Journal.hs b/hledger-lib/Hledger/Data/Journal.hs index 7818bd4c5..7364980ad 100644 --- a/hledger-lib/Hledger/Data/Journal.hs +++ b/hledger-lib/Hledger/Data/Journal.hs @@ -41,6 +41,7 @@ nulljournal = Journal { jmodifiertxns = [] , historical_prices = [] , final_comment_lines = [] , filepath = "" + , allfilepaths = [] , filereadtime = TOD 0 0 , jtext = "" } @@ -216,7 +217,7 @@ journalFinalise :: ClockTime -> LocalTime -> FilePath -> String -> Journal -> Jo journalFinalise tclock tlocal path txt j = journalCanonicaliseAmounts $ journalApplyHistoricalPrices $ journalCloseTimeLogEntries tlocal - j{filepath=path, filereadtime=tclock, jtext=txt} + j{filepath=path, allfilepaths=path:(allfilepaths j), filereadtime=tclock, jtext=txt} -- | Convert all the journal's amounts to their canonical display -- settings. Ie, all amounts in a given commodity will use (a) the diff --git a/hledger-lib/Hledger/Data/Types.hs b/hledger-lib/Hledger/Data/Types.hs index 82c7595b7..e36fdc489 100644 --- a/hledger-lib/Hledger/Data/Types.hs +++ b/hledger-lib/Hledger/Data/Types.hs @@ -129,6 +129,7 @@ data Journal = Journal { historical_prices :: [HistoricalPrice], final_comment_lines :: String, -- ^ any trailing comments from the journal file filepath :: FilePath, -- ^ file path of this journal + allfilepaths :: [FilePath], -- ^ file paths of this and any included journals filereadtime :: ClockTime, -- ^ when this journal was read from its file jtext :: String -- ^ the raw text read from the journal's file } deriving (Eq, Typeable) diff --git a/hledger-lib/Hledger/Read/Common.hs b/hledger-lib/Hledger/Read/Common.hs index 435f72602..dfbac025c 100644 --- a/hledger-lib/Hledger/Read/Common.hs +++ b/hledger-lib/Hledger/Read/Common.hs @@ -28,6 +28,9 @@ data Reader = Reader {rFormat :: String -- or raise an error. type JournalUpdate = ErrorT String IO (Journal -> Journal) +juSequence :: [JournalUpdate] -> JournalUpdate +juSequence us = liftM (foldr (.) id) $ sequence us + -- | Given a JournalUpdate-generating parsec parser, file path and data string, -- parse and post-process a Journal so that it's ready to use, or give an error. parseJournalWith :: (GenParser Char JournalContext JournalUpdate) -> FilePath -> String -> ErrorT String IO Journal diff --git a/hledger-lib/Hledger/Read/Journal.hs b/hledger-lib/Hledger/Read/Journal.hs index 090b66d70..5f742f5e7 100644 --- a/hledger-lib/Hledger/Read/Journal.hs +++ b/hledger-lib/Hledger/Read/Journal.hs @@ -156,9 +156,9 @@ parse = parseJournalWith journalFile -- error-raising "JournalUpdate" which can be applied to an empty journal -- to get the final result. journalFile :: GenParser Char JournalContext JournalUpdate -journalFile = do items <- many journalItem +journalFile = do journalupdates <- many journalItem eof - return $ liftM (foldr (.) id) $ sequence items + return $ juSequence journalupdates where -- As all journal line types can be distinguished by the first -- character, excepting transactions versus empty (blank or @@ -175,6 +175,9 @@ journalFile = do items <- many journalItem , emptyLine >> return (return id) ] "journal transaction or directive" +journalAddFilePath :: FilePath -> Journal -> Journal +journalAddFilePath f j@Journal{allfilepaths=fs} = j{allfilepaths=fs++[f]} + emptyLine :: GenParser Char st () emptyLine = do many spacenonewline optional $ (char ';' "comment") >> many (noneOf "\n") @@ -217,7 +220,7 @@ ledgerInclude = do contents <- readFileOrError outerPos filepath let inIncluded = show outerPos ++ " in included file " ++ show filename ++ ":\n" case runParser journalFile outerState filepath contents of - Right ju -> ju `catchError` (throwError . (inIncluded ++)) + Right ju -> juSequence [return $ journalAddFilePath filepath, ju] `catchError` (throwError . (inIncluded ++)) Left err -> throwError $ inIncluded ++ show err where readFileOrError pos fp = ErrorT $ liftM Right (readFile fp) `catch`