diff options
-rw-r--r-- | doc/ledger.1 | 453 | ||||
-rw-r--r-- | doc/ledger3.texi | 678 | ||||
-rw-r--r-- | src/journal.cc | 78 | ||||
-rw-r--r-- | src/journal.h | 4 | ||||
-rw-r--r-- | src/pstream.h | 5 | ||||
-rw-r--r-- | src/session.cc | 11 | ||||
-rw-r--r-- | src/session.h | 4 | ||||
-rw-r--r-- | src/textual.cc | 5 | ||||
-rw-r--r-- | src/wcwidth.cc | 4 | ||||
-rwxr-xr-x | test/DocTests.py | 48 | ||||
-rw-r--r-- | test/baseline/dir-alias-fail.test | 12 | ||||
-rw-r--r-- | test/baseline/dir-alias-recursive.test | 12 | ||||
-rw-r--r-- | test/baseline/dir-alias.test | 13 |
13 files changed, 988 insertions, 339 deletions
diff --git a/doc/ledger.1 b/doc/ledger.1 index 659d3fbb..7f4a78ac 100644 --- a/doc/ledger.1 +++ b/doc/ledger.1 @@ -182,7 +182,7 @@ Invert the value of amounts shown. .It Fl \-market Pq Fl V Show current market values for all amounts. This is determined in a somewhat magical fashion. It is probably more straightforward to use -.Fl \-exchange Pq Fl X . +.Fl \-exchange Ar commodity Pq Fl X . .It Fl \-period Ar time-period Pq Fl p Show postings only for the given .Ar time-period . @@ -281,193 +281,586 @@ transactions they are contained in. See the manual for more information. .Pp .Bl -tag -width -indent .It Fl \-abbrev-len Ar INT +Set the minimum length an account can be abbreviated to if it doesn't +fit inside the +.Nm account-width . +If +.Ar INT +is zero, then the +account name will be truncated on the right. If +.Ar INT +is greater +than +.Nm account-width +then the account will be truncated on the +left, with no shortening of the account names in order to fit into the +desired width. .It Fl \-account Ar STR +Prepend +.Ar STR +to all accounts reported. That is, the option +.Nm --account Personal +would tack +.Nm Personal: +to the beginning of every account reported in a balance report or register report. .It Fl \-account-width Ar INT +Set the width of the account column in the +.Nm register +report +to +.Ar INT +characters. .It Fl \-actual Pq Fl L +Report only real transactions, with no automated or virtual +transactions used. .It Fl \-add-budget +Show only un-budgeted postings. .It Fl \-amount Ar EXPR Pq Fl t +Apply the given value expression to the posting amount. Using +.Nm --amount Ar EXPR +you can apply an +arbitrary transformation to the postings. .It Fl \-amount-data Pq Fl j +On a register report print only the dates and amount of postings. +Useful for graphing and spreadsheet applications. .It Fl \-amount-width Ar INT +Set the width in characters of the amount column in the +.Nm register +report. .It Fl \-anon +Anonymize registry output, mostly for sending in bug reports. .It Fl \-args-only .It Fl \-auto-match .It Fl \-aux-date +Show auxiliary dates for all calculations. +Alias for +.Fl \-effective .It Fl \-average Pq Fl A +Print average values over the number of transactions instead of +running totals. .It Fl \-balance-format Ar FMT +Specify the format to use for the +.Nm balance +report. .It Fl \-base .It Fl \-basis Pq Fl B +Report the cost basis on all posting. +Alias for +.Fl \-cost .It Fl \-begin Ar DATE Pq Fl b +Specify the start +.Ar DATE +of all calculations. Transactions before +that date will be ignored. .It Fl \-bold-if Ar EXPR +Print the entire line in bold if the given value expression is true. .It Fl \-budget +Only display budgeted items. In a register report this +displays transaction in the budget, in a balance report this displays +accounts in the budget. .It Fl \-budget-format Ar FMT +Specify the format to use for the +.Nm budget +report. .It Fl \-by-payee Pq Fl P +Group postings in the register report by common payee names. .It Fl \-cache Ar FILE .It Fl \-check-payees +Enable strict and pedantic checking for payees as well as accounts, +commodities and tags. .It Fl \-cleared Pq Fl C +Display only cleared postings. .It Fl \-cleared-format Ar FMT +Specify the format to use for the +.Nm cleared +report .It Fl \-collapse Pq Fl n +By default ledger prints all accounts in an account tree. With +.Fl \-collapse +it prints only the top level account specified. .It Fl \-collapse-if-zero +Collapse the account display only if it has a zero balance. .It Fl \-color +Use color if the terminal supports it. +Alias for +.Fl \-ansi .It Fl \-columns Ar INT +Specify the width of the +.Nm register +report in characters. .It Fl \-cost -See +Report the cost basis on all posting. +Alias for .Fl \-basis . .It Fl \-count +Direct ledger to report the number of items when appended to the +commodities, accounts or payees command. .It Fl \-csv-format Ar FMT +Specify the format to use for the +.Nm csv +report .It Fl \-current Pq Fl c -.It Fl \-daily +Shorthand for +.Nm --limit 'date <= today' . +.It Fl \-daily Pq Fl D +Shorthand for +.Nm --period 'daily' . .It Fl \-date Ar EXPR +Transform the date of the transaction using +.Ar EXPR . .It Fl \-date-format Ar DATEFMT Pq Fl y +Specify the format ledger should use to print dates. .It Fl \-datetime-format Ar FMT .It Fl \-date-width Ar INT +Specify the width, in characters, of the date column in the +.Nm register +report. .It Fl \-day-break .It Fl \-dc +Display register or balance in debit/credit format If you use +.Fl \-dc +with either the register (reg) or balance (bal) commands, +you will now get separate columns for debits and credits. .It Fl \-debug Ar STR +If Ledger has been built with debug options this will provide extra +data during the run. .It Fl \-decimal-comma +Direct Ledger to parse journals using the European standard comma as +decimal separator, vice a period. .It Fl \-depth Ar INT +Limit the depth of the account tree. In a balance report, for example, +a +.Fl \-depth 2 +statement will print balances only for account with +two levels, i.e. +.Nm Expenses:Entertainment +but not +.Nm Expenses:entertainemnt:Dining . +This is a display predicate, which +means it only affects display, not the total calculations. .It Fl \-deviation Pq Fl D +Report each posting’s deviation from the average. It is only meaningful +in the register and prices reports. .It Fl \-display Ar EXPR Pq Fl d +Display lines that satisfy the expression +.Ar EXPR . .It Fl \-display-amount Ar EXPR +Apply a transformation to the +.Nm displayed +amount. This occurs after +calculations occur. .It Fl \-display-total Ar EXPR +Apply a transformation to the +.Nm displayed +total. This occurs after +calculations occur. .It Fl \-dow +Group transactions by the days of the week. +Alias for +.Fl \-days-of-week .It Fl \-download +Cause quotes to be automagically downloaded, as needed, by running +a script named +.Nm getquote +and expecting that script to return +a value understood by ledger. A sample implementation of +a +.Nm getquote +script, implemented in Perl, is provided in the +distribution. Downloaded quote price are then appended to the price +database, usually specified using the environment variable +.NmLEDGER_PRICE_DB . .It Fl \-empty Pq Fl E -.It Fl \-end Pq Fl e +Include empty accounts in report. +.It Fl \-end Ar DATE Pq Fl e +Specify the end +.Ar DATE +for a transaction to be considered in the +report. .It Fl \-equity +Related to the +.Nm equity +command. Gives current account balances in the form of a register +report. .It Fl \-exact -.It Fl \-exchange Ar COMM Oo , COMM, ... Oc Pq Fl X +.It Fl \-exchange Ar COMMODITY Oo , COMM, ... Oc Pq Fl X +Display values in terms of the given +.Ar COMMODITY . +The latest available price is used. .It Fl \-explicit .It Fl \-file Ar FILE +Read +.Ar FILE +as a ledger file. .It Fl \-first Ar INT -See +Print the first +.Ar INT +entries. Opposite of +.Fl \-tail Ar INT . +Alias for .Fl \-head . .It Fl \-flat +Force the full names of accounts to be used in the balance report. The +balance report will not use an indented tree. .It Fl \-force-color +Output TTY color codes even if the TTY doesn't support them. Useful +for TTYs that don't advertise their capabilities correctly. .It Fl \-force-pager +Force Ledger to paginate its output. .It Fl \-forecast-while Ar EXPR -(Also -.Fl \-forecast -). +Continue forecasting while +.Ar VEXPR +is true. +Alias for +.Fl \-forecast . .It Fl \-forecast-years Ar INT +Forecast at most +.Ar INT +years into the future. .It Fl \-format Ar FMT Pq Fl F -.It Fl \-full-help +Use the given format string +.Ar FMT +to print output. .It Fl \-gain Pq Fl G +Report net gain or loss for commodities that have a price history. .It Fl \-generated +Include auto-generated postings (such as those from automated +transactions) in the report, in cases where you normally wouldn't want +them. .It Fl \-group-by Ar EXPR +Group transaction together in the +.Nm register +report. +.Ar EXPR +can be anything, although most common would be +.Nm payee +or +.Nm commodity . +The +.Nm tags() +function is also useful here. .It Fl \-group-title-format Ar FMT +Set the format for the headers that separate reports section of +a grouped report. Only has effect with a +.Fl \-group-by Ar EXPR +register report. .It Fl \-head Ar INT +Print the first +.Ar INT +entries. Opposite of +.Fl \-tail Ar INT . +Alias for +.Fl \-first .It Fl \-help -.It Fl \-help-calc -.It Fl \-help-comm -.It Fl \-help-disp +Print a summary of all the options, and what they are used for. This +can be a handy way to remember which options do what. This help screen +is also printed if ledger is run without a command. .It Fl \-immediate -.It Fl \-import Ar STR +Instruct ledger to evaluate calculations immediately rather than lazily. .It Fl \-init-file Ar FILE +Causes +.Nm FILE +to be read by ledger before any other ledger file. +This file may not contain any postings, but it may contain option +settings. To specify options in the init file, use the same syntax as +the command-line, but put each option on its own line. .It Fl \-inject Ar STR +TODO .It Fl \-input-date-format Ar DATEFMT +Specify the input date format for journal entries. .It Fl \-invert +Change the sign of all reported values. .It Fl \-last Ar INT -See +Report only the last +.Ar INT +entries. Only useful on a register +report. +Alias for .Fl \-tail . .It Fl \-leeway Ar INT Pq Fl Z +Alias for +.Fl \-price-expr . .It Fl \-limit Ar EXPR Pq Fl l +Limit postings in calculations. .It Fl \-lot-dates +Report the date on which each commodity in a balance report was +purchased. .It Fl \-lot-notes +Report the tag attached to each commodity in a balance report. .It Fl \-lot-prices +Report the price at which each commodity in a balance report was +purchased. .It Fl \-lots +Report the date and price at which each commodity was purchased in +a balance report. .It Fl \-lots-actual .It Fl \-market Pq Fl V +Use the latest market value for all commodities. .It Fl \-master-account Ar STR +Prepend all account names with +.Ar STR .It Fl \-meta Ar EXPR +In the register report, prepend the transaction with the value of the given +.Ar TAG . .It Fl \-meta-width Ar INT +Specify the width of the Meta column used for the +.Fl \-meta Ar TAG +options. .It Fl \-monthly Pq Fl M +Shorthand for +.Fl \-period 'monthly' . +.It Fl \-no-aliases +Aliases are completely ignored. .It Fl \-no-color -.It Fl \-no-pager +Suppress any color TTY output. .It Fl \-no-rounding +Don't output +.Nm <Rounding> +postings. Note that this will cause the +running total to often not add up! It's main use is for +.Fl \-amount-data Pq Fl j +and +.Fl \-total-data Pq Fl J +reports. .It Fl \-no-titles +Suppress the output of group titles. .It Fl \-no-total +Suppress printing the final total line in a balance report. .It Fl \-now Ar DATE +Define the current date in case to you to do calculate in the past or +future using +.Fl \-current . .It Fl \-only Ar EXPR +This is a postings predicate that applies after certain transforms have +been executed, such as periodic gathering. .It Fl \-options +Display the options in effect for this Ledger invocation, along with +their values and the source of those values. .It Fl \-output Ar FILE Pq Fl o +Redirect the output of ledger to the file defined in +.Ar FILE . .It Fl \-pager Ar STR +Specify the pager program to use as +.Ar STR . .It Fl \-payee +Sets a value expression for formatting the payee. In the +.Nm register +report this prevents the second entry from having +a date and payee for each transaction. .It Fl \-payee-width Ar INT +Set the number of columns dedicated to the payee in the register +report to +.Ar INT . .It Fl \-pedantic +Accounts, tags or commodities not previously declared will cause errors. .It Fl \-pending -.It Fl \-percent Pq Fl \% +Use only postings that are marked pending. +.It Fl \-percent Pq Fl \b'%' +Calculate the percentage value of each account in a balance reports. +Only works for account that have a single commodity. .It Fl \-period Ar PERIOD Pq Fl p +Define a period expression that sets the time period during which +transactions are to be accounted. For a +.Nm register +report only +the transactions that satisfy the period expression with be displayed. +For a balance report only those transactions will be accounted in the +final balances. .It Fl \-period-sort +Sort the posting within transactions using the given value expression. .It Fl \-permissive .It Fl \-pivot Ar STR +Produce a balance pivot report +.Nm around +the given +.Ar TAG . .It Fl \-plot-amount-format Ar FMT +Define the output format for an amount data plot. .It Fl \-plot-total-format Ar FMT +Define the output format for a total data plot. .It Fl \-prepend-format Ar FMT +Prepend +.Ar STR +to every line of the output. .It Fl \-prepend-width Ar INT +Reserve +.Ar INT +spaces at the beginning of each line of the output. .It Fl \-price Pq Fl I +Use the price of the commodity purchase for performing calculations. .It Fl \-price-db Ar FILE -.It Fl \-price-exp Ar STR -See -.Fl \-leeway . +.It Fl \-price-exp Ar STR Pq Fl Z +Set the expected freshness of price quotes, in +.Ar INT +minutes. That +is, if the last known quote for any commodity is older than this value, +and if +.Fl \-download +is being used, then the Internet will be +consulted again for a newer price. Otherwise, the old price is still +considered to be fresh enough. +Alias for +.Fl \-leeway Ar INT Pq Fl Z .It Fl \-prices-format Ar FMT .It Fl \-pricedb-format Ar FMT .It Fl \-primary-date +Show primary dates for all calculations. Alias for +.Fl \-actual-dates .It Fl \-quantity Pq Fl O +Report commodity totals (this is the default). .It Fl \-quarterly +Synonym for +\Fl \-period 'quarterly' . .It Fl \-raw -For use only with the +In the .Nm print -command, it causes Ledger to print out matching entries exactly as they -appeared in the original journal file. +report, show transactions using the exact same syntax as +specified by the user in their data file. Don't do any massaging or +interpreting. Can be useful for minor cleanups, like just aligning +amounts. .It Fl \-real Pq Fl R +Account using only real transactions ignoring virtual and automatic +transactions. +.It Fl \-recursive-aliases +Causes ledger to try to expand aliases recursively, i.e. try to expand +the result of an earlier expansion again, until no more expansions apply. .It Fl \-register-format Ar FMT +Define the output format for the +.Nm register +report. .It Fl \-related Pq Fl r +In a register report show the related account. This is the other +.Nm side +of the transaction. .It Fl \-related-all +Show all postings in a transaction, similar to +.Fl \-related +but show +.Nm both sides +of each transaction. .It Fl \-revalued .It Fl \-revalued-only .It Fl \-revalued-total Ar EXPR .It Fl \-rich-data .It Fl \-seed Ar INT -.It Fl \-script +Set the random seed to +.Ar INT +for the +.Nm generate +command. Used as part of development testing. +.It Fl \-script Ar FILE +Execute a ledger script. .It Fl \-sort Ar EXPR Pq Fl S +Sort the register report based on the value expression given to sort. .It Fl \-sort-all .It Fl \-sort-xacts +Sort the posting within transactions using the given value expression. .It Fl \-start-of-week Ar STR +Tell ledger to use a particular day of the week to start its "weekly" +summary. +.Fl \-start-of-week=1 +specifies Monday as the start of the week. .It Fl \-strict +Accounts, tags or commodities not previously declared will cause warnings. .It Fl \-subtotal Pq Fl s +Report register as a single subtotal. .It Fl \-tail Ar INT +Report only the last +.Ar INT +entries. Only useful on a register report. Alias for +.Fl \-last Ar INT .It Fl \-time-report -.It Fl \-total Ar EXPR +.It Fl \-total Ar EXPR Pq Fl T +Define a value expression used to calculate the total in reports. .It Fl \-total-data Pq Fl J +Show only dates and totals to format the output for plots. .It Fl \-total-width Ar INT +Set the width of the total field in the register report. .It Fl \-trace Ar INT -.It Fl \-truncate +Enable tracing. The +.Ar INT +specifies the level of trace desired. +.It Fl \-truncate Ar CODE +Indicates how truncation should happen when the contents of columns +exceed their width. Valid arguments are +.Nm leading , Nm middle , +and +.Nm trailing . +The default is smarter than any of these three, +as it considers sub-names within the account name (that style is +called "abbreviate"). .It Fl \-unbudgeted +Show only un-budgeted postings. .It Fl \-uncleared Pq Fl U +Use only uncleared transactions in calculations and reports. .It Fl \-unrealized .It Fl \-unrealized-gains +Allow the user to specify what account name should be used for +unrealized gains. Defaults to +.Nm "Equity:Unrealized Gains" . +Often set in one's +.Nm ~/.ledgerrc +file to change the default. .It Fl \-unrealized-losses +Allow the user to specify what account name should be used for +unrealized gains. Defaults to +.Nm "Equity:Unrealized Losses" . +Often set in one's +.Nm ~/.ledgerrc +file to change the default. .It Fl \-unround +Perform all calculations without rounding and display results to full +precision. +.It Fl \-values .It Fl \-value-expr Ar EXPR .It Fl \-verbose +Print detailed information on the execution of Ledger. .It Fl \-verify +Enable additional assertions during run-time. This causes a significant +slowdown. When combined with +.Fl \-debug Ar CODE +ledger will produce memory trace information. .It Fl \-verify-memory .It Fl \-version +Print version information and exit. .It Fl \-weekly Pq Fl W +Synonym for +.Fl \-period 'weekly' . .It Fl \-wide Pq Fl w +Assume 132 columns instead of 80. .It Fl \-yearly Pq Fl Y +Synonym for +.Fl \-period 'yearly' . .El .Pp -.Sh PRECOMMANDS +.Sh PRE-COMMANDS +Pre-commands are useful when you aren't sure how a command or option +will work. The difference between a pre-command and a regular command +is that pre-commands ignore the journal data file completely, nor is +the user's init file read. .Pp .Bl -tag -width -indent -.It Nm args +.It Nm args / query +Evaluate the given arguments and report how Ledger interprets it against +the following model transaction. .It Nm eval +Evaluate the given value expression against the model transaction. .It Nm format -.It Nm parse +Print details of how ledger uses the given formatting description and +apply it against a model transaction. +.It Nm parse / expr +Print details of how ledger uses the given value expression description +and apply it against a model transaction. +.It Nm generate +Randomly generates syntactically valid Ledger data from a seed. Used +by the GenerateTests harness for development testing. .It Nm period -.It Nm python +Evaluate the given period and report how Ledger interprets it. +.It Nm script .It Nm template +Shows the insertion template that the +.Nm xact sub-command generates. +This is a debugging command. .El .Pp .Sh QUERIES diff --git a/doc/ledger3.texi b/doc/ledger3.texi index 4ab8f4fd..02454fdc 100644 --- a/doc/ledger3.texi +++ b/doc/ledger3.texi @@ -91,7 +91,7 @@ @c has configured. @c @c To manually run the tests in this file run: -@c $ ./test/DocTests.py -vv --ledger ./ledger --file ./test/ledger3.texi +@c $ ./test/DocTests.py -vv --ledger ./ledger --file ./doc/ledger3.texi @copying @@ -240,9 +240,9 @@ accounting tools. The next step up from a checkbook journal, is a journal that keeps track of all your accounts, not just checking. In such a journal, you record not only who gets paid---in the case of a debit---but where the -money came from. In a checkbook journal, its assumed that all the +money came from. In a checkbook journal, it's assumed that all the money comes from your checking account. But in a general journal, you -write posting two-lines: the source account and target account. +write postings in two lines: the source account and target account. @emph{There must always be a debit from at least one account for every credit made to another account}. This is what is meant by ``double-entry'' accounting: the journal must always balance to zero, @@ -288,10 +288,10 @@ Most private people consider an account to be something that holds money at an institution for them. Ledger uses a more general definition of the word. An account is anywhere money can go. Other finance programs use ``categories'', Ledger uses accounts. So, for -example, if you buy some groceries at Trader Joe's then more groceries -at Whole Foods Markets you might assign the transactions like this +example, if you buy some groceries at Trader Joe's, then more groceries +at Whole Food Market, you might assign the transactions like this -@smallexample +@smallexample @c input:validate 2011/03/15 Trader Joe's Expenses:Groceries $100.00 Assets:Checking @@ -385,14 +385,14 @@ $ ./configure && make install Ledger has a complete online help system based on GNU Info. This manual can be searched directly from the command line using the -following options: @code{ledger --help} bring up this entire manual in -your tty. +following options: @code{ledger --help} brings up this entire manual in +your TTY. If you need help on how to use Ledger, or run into problems, you can join the Ledger mailing list at @url{http://groups.google.com/group/ledger-cli}. -You can also find help at the @code{#ledger} channel on the IRC server +You can also find help in the @code{#ledger} channel on the IRC server @code{irc.freenode.net}. @node Ledger Tutorial, Principles of Accounting with Ledger, Introduction to Ledger, Top @@ -430,11 +430,11 @@ If you would rather start with your own journal right away please Please note that as a command line program, Ledger is controlled from your shell. There are several different command shells that all behave slightly differently with respect to some special characters. -In particular, the BASH shell will interpret @samp{$} signs +In particular, the ``bash'' shell will interpret @samp{$} signs differently than ledger and they must be escaped to reach the actual -program. Another example is zsh, which will interpret @samp{^} +program. Another example is ``zsh'', which will interpret @samp{^} differently than ledger expects. In all cases that follow you should -take that into account when entering the command line arguments given. +take that into account when entering the command line arguments as given. There are too many variations between shells to give concrete examples for each. @@ -645,7 +645,7 @@ shows the ``cleared'' balance. @cindex currency symbol display on windows Using ledger under the windows command shell has one significant -limitation. CMD.exe is limited to standard ASCII characters and as +limitation. CMD.EXE is limited to standard ASCII characters and as such cannot display any currency symbols other than dollar signs @samp{$}. @@ -669,9 +669,9 @@ such cannot display any currency symbols other than dollar signs Accounting is simply tracking your money. It can range from nothing, and just waiting for automatic overdraft protection to kick in, or -not, to a full blown double entry accounting system. Ledger +not, to a full-blown double-entry accounting system. Ledger accomplishes the latter. With ledger you can handle your personal -finances or your businesses. Double-entry accounting scales. +finances or your business's. Double-entry accounting scales. @node Stating where money goes, Assets and Liabilities, Accounting with Ledger, Principles of Accounting with Ledger @section Stating where money goes @@ -692,7 +692,7 @@ where the money comes from and where it goes to. For example, when you are paid a salary, you must add money to your bank account and also subtract it from an income account: -@smallexample +@smallexample @c input:validate 9/29 My Employer Assets:Checking $500.00 Income:Salary $-500.00 @@ -802,8 +802,12 @@ spend each month on X? Ledger provides a simple way of displaying monthly totals for any account. Here is an example that summarizes your monthly automobile expenses: -@smallexample -$ ledger -M register expenses:auto +@smallexample @c command:DB524E4 +$ ledger -M register -f drewr3.dat expenses:auto +@end smallexample + +@smallexample @c output:DB524E4 +11-Jan-01 - 11-Jan-31 Expenses:Auto $ 5,500.00 $ 5,500.00 @end smallexample This assumes, of course, that you use account names like @@ -827,7 +831,7 @@ This is fairly easy to do in ledger. When spending the money, spend it @emph{to} your Assets:Reimbursements, using a different account for each person or business that you spend money for. For example: -@smallexample +@smallexample @c input:validate 2004/09/29 Circuit City Assets:Reimbursements:Company XYZ $100.00 Liabilities:MasterCard @@ -838,7 +842,7 @@ expense was made on behalf of Company XYZ. Later, when Company XYZ pays the amount back, the money will transfer from that reimbursement account back to a regular asset account: -@smallexample +@smallexample @c input:validate 2004/09/29 Company XYZ Assets:Checking $100.00 Assets:Reimbursements:Company XYZ @@ -860,7 +864,7 @@ company accounts. But keeping them in one file involves the same kinds of postings, so those are what is shown here. First, the personal transaction, which shows the need for reimbursement: -@smallexample +@smallexample @c input:validate 2004/09/29 Circuit City Assets:Reimbursements:Company XYZ $100.00 Liabilities:MasterCard @@ -872,7 +876,7 @@ transaction should be immediately followed by an equivalent transaction, which shows the kind of expense, and also notes the fact that $100.00 is now payable to you: -@smallexample +@smallexample @c input:validate 2004/09/29 Circuit City Company XYZ:Expenses:Computer:Software $100.00 Company XYZ:Accounts Payable:Your Name @@ -885,7 +889,7 @@ paid back. These two transactions can also be merged, to make things a little clearer. Note that all amounts must be specified now: -@smallexample +@smallexample @c input:validate 2004/09/29 Circuit City Assets:Reimbursements:Company XYZ $100.00 Liabilities:MasterCard $-100.00 @@ -899,7 +903,7 @@ paying it to accounts payable, and then drawing it again from the reimbursement account, and paying it to your personal asset account. It's easier shown than said: -@smallexample +@smallexample @c input:validate 2004/10/15 Company XYZ Assets:Checking $100.00 Assets:Reimbursements:Company XYZ $-100.00 @@ -929,7 +933,7 @@ above. For example, for both the expense and the pay-back shown above, the following four transactions would be created. Two in your personal ledger file: -@smallexample +@smallexample @c input:990E0D1 2004/09/29 Circuit City Assets:Reimbursements:Company XYZ $100.00 Liabilities:MasterCard $-100.00 @@ -941,7 +945,7 @@ personal ledger file: And two in your company ledger file: -@smallexample +@smallexample @c input:990E0D1 apply account Company XYZ 2004/09/29 Circuit City @@ -964,6 +968,18 @@ was spent using your MasterCard on behalf of Company XYZ, and that Company XYZ spent the money on computer software and paid it back about two weeks later. +@smallexample @c command:990E0D1 +$ ledger balance --no-total +@end smallexample + +@smallexample @c output:990E0D1 + $100.00 Assets:Checking + 0 Company XYZ + $-100.00 Assets:Checking + $100.00 Expenses:Computer:Software + $-100.00 Liabilities:MasterCard +@end smallexample + @node Commodities and Currencies, Accounts and Inventories, Assets and Liabilities, Principles of Accounting with Ledger @section Commodities and Currencies @@ -1014,7 +1030,7 @@ be any commodity, in which case the balance will be computed in terms of that commodity. The usual way to specify prices is with a price history file, which might look like this: -@smallexample +@smallexample @c input:validate P 2004/06/21 02:18:01 FEQTX $22.49 P 2004/06/21 02:18:01 BORL $6.20 P 2004/06/21 02:18:02 AAPL $32.91 @@ -1072,7 +1088,7 @@ that commodity on that day. It is also possible, by recording price details in a ledger file, to specify other prices for commodities at any given time. Such price transactions might look like those below: -@smallexample +@smallexample @c input:validate P 2004/06/21 02:17:58 TWCUX $27.76 P 2004/06/21 02:17:59 AGTHX $25.41 P 2004/06/21 02:18:00 OPTFX $39.31 @@ -1098,7 +1114,7 @@ transfers an hour of time into a @samp{Billable} account, and another which decreases the same account by ten minutes. The resulting report will indicate that fifty minutes remain: -@smallexample +@smallexample @c input:DF3FEBE 2005/10/01 Work done for company Billable:Client 1h Project:XYZ @@ -1110,9 +1126,13 @@ will indicate that fifty minutes remain: Reporting the balance for this ledger file produces: -@smallexample - 50.0m Billable:Client - -50.0m Project:XYZ +@smallexample @c command:DF3FEBE +$ ledger --no-total balance Billable Project +@end smallexample + +@smallexample @c output:DF3FEBE + 50.0m Billable:Client + -50.0m Project:XYZ @end smallexample This example works because ledger already knows how to handle seconds, @@ -1121,7 +1141,7 @@ other equivalencies is simple. The following is an example that creates data equivalencies, helpful for tracking bytes, kilobytes, megabytes, and more: -@smallexample +@smallexample @c input:validate C 1.00 Kb = 1024 b C 1.00 Mb = 1024 Kb C 1.00 Gb = 1024 Mb @@ -1149,7 +1169,7 @@ various items in EverQuest, and want to keep track of them using a ledger. Just add items of whatever quantity you wish into your EverQuest account: -@smallexample +@smallexample @c input:48F4E47 9/29 Get some stuff at the Inn Places:Black's Tavern -3 Apples Places:Black's Tavern -5 Steaks @@ -1162,13 +1182,13 @@ Tavern in order to add to your Inventory account. Note that you don't have to use @samp{Places:Black's Tavern} as the source account. You could use @samp{EverQuest:System} to represent the fact that you acquired them online. The only purpose for choosing one kind of -source account over another is for generate more informative reports -later on. The more you know, the better analysis you can perform. +source account over another is to generate more informative reports +later on. The more you know, the better the analysis you can perform. If you later sell some of these items to another player, the transaction would look like: -@smallexample +@smallexample @c input:48F4E47 10/2 Sturm Brightblade EverQuest:Inventory -2 Steaks EverQuest:Inventory 15 Gold @@ -1177,6 +1197,16 @@ transaction would look like: Now you've turned 2 steaks into 15 gold, courtesy of your customer, Sturm Brightblade. +@smallexample @c command:48F4E47 +$ ledger balance EverQuest +@end smallexample + +@smallexample @c output:48F4E47 + 3 Apples + 15 Gold + 3 Steaks EverQuest:Inventory +@end smallexample + @node Understanding Equity, Dealing with Petty Cash, Accounts and Inventories, Principles of Accounting with Ledger @section Understanding Equity @@ -1186,9 +1216,9 @@ account---because starting balances can't come out of nowhere. When you first start your ledger, you will likely already have money in some of your accounts. Let's say there's $100 in your checking account; then add a transaction to your ledger to reflect this amount. -Where will money come from? The answer: your equity. +Where will the money come from? The answer: your equity. -@smallexample +@smallexample @c input:validate 10/2 Opening Balance Assets:Checking $100.00 Equity:Opening Balances @@ -1228,7 +1258,7 @@ One solution is: don't bother. Move your spending to a debit card, but in general ignore cash. Once you withdraw it from the ATM, mark it as already spent to an @samp{Expenses:Cash} category: -@smallexample +@smallexample @c input:validate 2004/03/15 ATM Expenses:Cash $100.00 Assets:Checking @@ -1238,7 +1268,7 @@ If at some point you make a large cash expense that you want to track, just @emph{move} the amount of the expense from @samp{Expenses:Cash} into the target account: -@smallexample +@smallexample @c input:validate 2004/03/20 Somebody Expenses:Food $65.00 Expenses:Cash @@ -1272,7 +1302,7 @@ reserves resources for later: @item Community fund @end itemize -The problem with this kind of setup is that when you spend money, it +The problem with this kind of setup is that, when you spend money, it comes from two or more places at once: the account and the fund. And yet, the correlation of amounts between funds and accounts is rarely one-to-one. What if the school fund has @samp{$500.00}, but @@ -1289,7 +1319,7 @@ This situation can be handled one of two ways. The first is using virtual postings to represent the fact that money is moving to and from two kind of accounts at the same time: -@smallexample +@smallexample @c input:396F24E 2004/03/20 Contributions Assets:Checking $500.00 Income:Donations @@ -1305,7 +1335,7 @@ virtual postings balance to zero. Now money can be spent directly from a fund at the same time as money is drawn from a physical account: -@smallexample +@smallexample @c input:396F24E 2004/03/25 Payment for books (paid from Checking) Expenses:Books $100.00 Assets:Checking $-100.00 @@ -1317,16 +1347,30 @@ funds. In this case, you will likely want to mask out your @samp{Assets} account, because otherwise the balance won't make much sense: -@smallexample -$ ledger bal -^Assets +@smallexample @c command:396F24E +$ ledger --no-total bal not ^Assets +@end smallexample + +@smallexample @c output:396F24E + $100.00 Expenses:Books + $400.00 Funds + $200.00 Building + $200.00 School + $-500.00 Income:Donations @end smallexample @findex --real If the @option{--real} option is used, the report will be in terms of the real accounts: -@smallexample -$ ledger --real bal +@smallexample @c command:2F1CB75,with_input:396F24E +$ ledger --real --no-total bal +@end smallexample + +@smallexample @c output:2F1CB75 + $400.00 Assets:Checking + $100.00 Expenses:Books + $-500.00 Income:Donations @end smallexample If more asset accounts are needed as the source of a posting, just @@ -1346,39 +1390,52 @@ set of postings. Basically, we are associating a transaction with a fund by setting its code. Here are two transactions that deposit money into, and spend money from, the @samp{Funds:School} fund: -@smallexample +@smallexample @c input:AD068BA 2004/03/25 (Funds:School) Donations Assets:Checking $100.00 Income:Donations +2004/03/25 (Funds:Building) Donations + Assets:Checking $20.00 + Income:Donations + 2004/04/25 (Funds:School) Payment for books Expenses:Books $50.00 Assets:Checking @end smallexample Note how the accounts now relate only to the real accounts, and any -balance or registers reports will reflect this. That the transactions +balance or register reports will reflect this. That the transactions relate to a particular fund is kept only in the code. -@findex --code-as-payee +@findex --payee=code @findex --by-payee How does this become a fund report? By using the -@option{--code-as-payee} option, you can generate a register report +@option{--payee=code} option, you can generate a register report where the payee for each posting shows the code. Alone, this is not terribly interesting; but when combined with the @option{--by-payee (-P)} option, you will now see account subtotals for any postings related to a specific fund. So, to see the current monetary balances of all funds, the command would be: -@smallexample -$ ledger --code-as-payee -P reg ^Assets +@smallexample @c command:AD068BA +$ ledger --payee=code -P reg ^Assets @end smallexample -Or to see a particular funds expenses, the @samp{School} fund in this +@smallexample @c output:AD068BA +04-Mar-25 Funds:Building Assets:Checking $20.00 $20.00 +04-Mar-25 Funds:School Assets:Checking $50.00 $70.00 +@end smallexample + +Or to see a particular fund's expenses, the @samp{School} fund in this case: -@smallexample -$ ledger --code-as-payee -P reg ^Expenses @@School +@smallexample @c command:E30B2FC,with_input:AD068BA +$ ledger --payee=code -P reg ^Expenses and code School +@end smallexample + +@smallexample @c output:E30B2FC +04-Apr-25 Funds:School Expenses:Books $50.00 $50.00 @end smallexample Both approaches yield different kinds of flexibility, depending on how @@ -1435,9 +1492,9 @@ posting. @section The Most Basic Entry Here is the Pacific Bell example from above, given as a Ledger -posting, with the additional of a check number: +posting, with the addition of a check number: -@smallexample +@smallexample @c input:validate 9/29 (1023) Pacific Bell Expenses:Utilities:Phone $23.00 Assets:Checking $-23.00 @@ -1449,7 +1506,7 @@ work better with Ledger's scheme of things. In fact, since Ledger is smart about many things, you don't need to specify the balanced amount, if it is the same as the first line: -@smallexample +@smallexample @c input:validate 9/29 (1023) Pacific Bell Expenses:Utilities:Phone $23.00 Assets:Checking @@ -1491,7 +1548,7 @@ basis of the opening entry for ledger. For example if you chose the beginning of 2011 as the date to start tracking finances with ledger, your opening balance entry could look like this: -@smallexample +@smallexample @c input:validate 2011/01/01 * Opening Balance Assets:Joint Checking $800.14 Assets:Other Checking $63.44 @@ -1547,13 +1604,13 @@ Expenses:Food:Hamburgers and Fries Comments are generally started using a @samp{;}. However, in order to increase compatibility with other text manipulation programs and -methods four additional comment characters are valid if used at the +methods, four additional comment characters are valid if used at the beginning of a line: @samp{#}, @samp{|}, and @samp{*} and @samp{%}. Block comments can be made by use @code{comment} ... @code{end comment}. -@smallexample +@smallexample @c input:validate ; This is a single line comment, # and this, % and this, @@ -1568,7 +1625,7 @@ end comment There are several forms of comments within a transaction, for example: -@smallexample +@smallexample @c input:validate ; this is a global comment that is not applied to a specific transaction ; it can start with any of the five characters but is not included in the ; output from 'print' or 'output' @@ -1592,7 +1649,7 @@ start with @samp{;} and are preserved as part of the transaction. The @cindex commodity Ledger is agnostic when it comes to how you value your accounts. -Dollars, Euros, Pounds, Francs, Shares etc. are just ``commodities''. +Dollars, Euros, Pounds, Francs, Shares etc. are all just ``commodities''. Holdings in stocks, bonds, mutual funds and other financial instruments can be labeled using whatever is convenient for you (stock ticker symbols are suggested for publicly traded assets).@footnote{You @@ -1613,21 +1670,21 @@ reporting capabilities to convert all commodities to a single commodity for reporting purposes without ever changing the underlying entry. -For example, the following entries reflect transaction made for a +For example, the following entries reflect transactions made for a business trip to Europe from the US: @smallexample @c input:82150D9 2011/09/23 Cash in Munich - Assets:Cash E50.00 + Assets:Cash €50.00 Assets:Checking $-66.00 2011/09/24 Dinner in Munich - Expenses:Business:Travel E35.00 + Expenses:Business:Travel €35.00 Assets:Cash @end smallexample This says that $66.00 came out of checking and turned into 50 -Euros. The implied exchange rate was $1.32. Then 35.00 Euros was +Euros. The implied exchange rate was $1.32. Then 35.00 Euros were spent on Dinner in Munich. Running a ledger balance report shows: @@ -1638,19 +1695,19 @@ $ ledger -f example.dat bal @smallexample @c output:82150D9 $-66.00 - E15.00 Assets - E15.00 Cash + €15.00 Assets + €15.00 Cash $-66.00 Checking - E35.00 Expenses:Business:Travel + €35.00 Expenses:Business:Travel -------------------- $-66.00 - E50.00 + €50.00 @end smallexample The top two lines show my current assets as $-66.00 in checking (in this very short example I didn't establish opening an opening balance -for the checking account) and E15.00. After spending on dinner I have -E15.00 in my wallet. The bottom line balances to zero, but is shown +for the checking account) and €15.00. After spending on dinner I have +€15.00 in my wallet. The bottom line balances to zero, but is shown in two lines since we haven't told ledger to convert commodities. @menu @@ -1664,10 +1721,10 @@ in two lines since we haven't told ledger to convert commodities. @subsection Naming Commodities Commodity names can have any character, including white-space. -However, if you include white-space or numeric characters the +However, if you include white-space or numeric characters, the commodity name must be enclosed in double quotes @samp{"}: -@smallexample +@smallexample @c input:validate 1999/06/09 ! Achat Actif:SG PEE STK 49.957 "Arcancia Équilibre 454" Actif:SG PEE STK $-234.90 @@ -1687,22 +1744,22 @@ multiple commodities in the same transaction. The type of the share unit you made the purchase in ($ for AAPL). Yes, the typical convention is as follows: -@smallexample +@smallexample @c input:validate 2004/05/01 Stock purchase Assets:Broker 50 AAPL @@ $30.00 Expenses:Broker:Commissions $19.95 - Assets:Broker $-1,500.00 + Assets:Broker $-1,519.95 @end smallexample -This assumes you have a brokerage account that is capable of managed +This assumes you have a brokerage account that is capable of managing both liquid and commodity assets. Now, on the day of the sale: -@smallexample +@smallexample @c input:validate 2005/08/01 Stock sale Assets:Broker -50 APPL @{$30.00@} @@ $50.00 Expenses:Broker:Commissions $19.95 Income:Capital Gains $-1,000.00 - Assets:Broker $2,500.00 + Assets:Broker $2,480.05 @end smallexample @noindent @@ -1719,7 +1776,7 @@ longest-held-first. @cindex fixing lot prices @cindex consumable commodity pricing -Commodities that you keep in order to sell them at a later time have +Commodities that you keep in order to sell at a later time have a variable value that fluctuates with the market prices. Commodities that you consume should not fluctuate in value, but stay at the lot price they were purchased at. As an extension of ``lot pricing'', you @@ -1733,7 +1790,7 @@ reported in terms of today's price. This is supported as follows: -@smallexample +@smallexample @c input:validate 2009/01/01 Shell Expenses:Gasoline 11 GAL @{=$2.299@} Assets:Checking @@ -1745,9 +1802,9 @@ price of gasoline. If you do not want price fixing, you can specify this same transaction in one of two ways, both equivalent (note the lack of the equal sign -from the transaction above): +compared to the transaction above): -@smallexample +@smallexample @c input:validate 2009/01/01 Shell Expenses:Gasoline 11 GAL @{$2.299@} Assets:Checking @@ -1760,7 +1817,7 @@ from the transaction above): There is no difference in meaning between these two forms. Why do both exist, you ask? To support things like this: -@smallexample +@smallexample @c input:validate 2009/01/01 Shell Expenses:Gasoline 11 GAL @{=$2.299@} @@ $2.30 Assets:Checking @@ -1780,12 +1837,12 @@ Assets:Checking because its amount is null. Ledger allows you to have very detailed control over how your commodities are valued. You can fine tune the results given using the @option{--market} or @option{--exchange @var{COMMODITY}} options. There -are now several points of interception, you can specify the valuation +are now several points of interception; you can specify the valuation method: @enumerate @item on a commodity itself, -@item on a posting, via metadata (affect is largely the same as #1), +@item on a posting, via metadata (effect is largely the same as #1), @item on an xact, which then applies to all postings in that xact, @item on any posting via an automated transaction, @item on a per-account basis, @@ -1938,9 +1995,9 @@ freeform text editor to enter transactions makes it easy to keep the data, but also easy to enter accounts or payees inconsistently or with spelling errors. -In order to combat inconsistency you can define allowable accounts and -or payees. For simplicity, create a separate text file and enter -define accounts a payees like +In order to combat inconsistency you can define allowable accounts and +payees. For simplicity, create a separate text file and define accounts +and payees like @smallexample account Expenses @@ -1966,7 +2023,7 @@ $ ledger accounts >> Accounts.dat @end smallexample @noindent -You will have to edit this file to add the @code{account} directive. +You will have to edit this file to add the @code{account} directive in front of every line. @node Journal Format, Converting from other formats, Keeping it Consistent, Keeping a Journal @section Journal Format @@ -2010,7 +2067,7 @@ payee, or a description of the posting. The format of each following posting is: @smallexample -ACCOUNT AMOUNT [; NOTE] + ACCOUNT AMOUNT [; NOTE] @end smallexample The @code{ACCOUNT} may be surrounded by parentheses if it is a virtual @@ -2037,24 +2094,24 @@ An automated transaction. A value expression must appear after the equal sign. After this initial line there should be a set of one or more postings, -just as if it were normal transaction. If the amounts of the postings -have no commodity, they will be applied as modifiers to whichever real +just as if it were a normal transaction. If the amounts of the postings +have no commodity, they will be applied as multipliers to whichever real posting is matched by the value expression (@pxref{Automated Transactions}). @item ~ -A period transaction. A period expression must appear after the tilde. +A periodic transaction. A period expression must appear after the tilde. After this initial line there should be a set of one or more -postings, just as if it were normal transaction. +postings, just as if it were a normal transaction. @item ; # % | * -A line beginning with a colon, pound, percent, bar or asterisk +A line beginning with a semicolon, pound, percent, bar or asterisk indicates a comment, and is ignored. Comments will not be returned in a ``print'' response. @item indented ; -If the semi colon is indented and occurs inside a transaction, it is +If the semicolon is indented and occurs inside a transaction, it is parsed as a persistent note for its preceding category. These notes or tags can be used to augment the reporting and filtering capabilities of Ledger. @@ -2073,13 +2130,13 @@ Command directives must occur at the beginning of a line. Use of @samp{!} and @samp{@@} is deprecated. @item account -Pre-declare valid account names. This only has effect if +Pre-declare valid account names. This only has an effect if @option{--strict} or @option{--pedantic} is used (see below). The @code{account} directive supports several optional sub-directives, if they immediately follow the account directive and if they begin with whitespace: -@smallexample +@smallexample @c input:validate account Expenses:Food note This account is all about the chicken! alias food @@ -2091,7 +2148,7 @@ account Expenses:Food @end smallexample The @code{note} sub-directive associates a textual note with the -account. This can be accessed later using the @code{note} valexpr +account. This can be accessed later using the @code{note} value expression function in any account context. The @code{alias} sub-directive, which can occur multiple times, allows @@ -2103,13 +2160,13 @@ provides regexes that identify the account if that payee is encountered and an account within its transaction ends in the name "Unknown". Example: -@smallexample +@smallexample @c input:validate 2012-02-27 KFC Expenses:Unknown $10.00 ; Read now as "Expenses:Food" Assets:Cash @end smallexample -The @code{check} and @code{assert} directives warn or error +The @code{check} and @code{assert} directives warn or raise an error (respectively) if the given value expression evaluates to false within the context of any posting. @@ -2123,16 +2180,16 @@ contain only a single posting. @item apply account @c instance_t::master_account_directive -Sets the root for all accounts following the directive. Ledger +Sets the root for all accounts following this directive. Ledger supports a hierarchical tree of accounts. It may be convenient to keep two ``root accounts''. For example you may be tracking your personal finances and your business finances. In order to keep them separate you could preface all personal accounts with @samp{personal:} -and all business account with @samp{business:}. You can easily split -out large groups of transaction without manually editing them using +and all business accounts with @samp{business:}. You can easily split +out large groups of transactions without manually editing them using the account directive. For example: -@smallexample +@smallexample @c input:validate apply account Personal 2011/11/15 Supermarket Expenses:Groceries $ 50.00 @@ -2141,14 +2198,14 @@ apply account Personal Would result in all postings going into @samp{Personal:Expenses:Groceries} and @samp{Personal:Assets:Checking} -until and @samp{end apply account} directive was found. +until an @samp{end apply account} directive was found. @item alias @c instance_t::alias_directive Define an alias for an account name. If you have a deeply nested tree of accounts, it may be convenient to define an alias, for example: -@smallexample +@smallexample @c input:validate alias Dining=Expenses:Entertainment:Dining alias Checking=Assets:Credit Union:Joint Checking Account @@ -2158,9 +2215,42 @@ alias Checking=Assets:Credit Union:Joint Checking Account @end smallexample The aliases are only in effect for transactions read in after the alias -is defined and are effected by @code{account} directives that precede +is defined and are affected by @code{account} directives that precede them. +@smallexample @c command:validate +$ ledger bal --no-total ^Exp +@end smallexample + +@smallexample + $10.00 Expenses:Entertainment:Dining +@end smallexample + +With the option @option{--recursive-aliases}, aliases can refer to other aliases, +the following example produces exactly the same transactions and account names +as the preceding one: + +@smallexample @c input:validate +alias Entertainment=Expenses:Entertainment +alias Dining=Entertainment:Dining +alias Checking=Assets:Credit Union:Joint Checking Account + +2011/11/30 ChopChop + Dining $10.00 + Checking +@end smallexample + +@smallexample @c command:validate +$ ledger balance --no-total --recursive-aliases ^Exp +@end smallexample + +@smallexample + $10.00 Expenses:Entertainment:Dining +@end smallexample + +The option @option{--no-aliases} completely disables alias expansion. +All accounts are read verbatim as they are in the ledger file. + @item assert @c instance_t::assert_directive An assertion can throw an error if a condition is not met during @@ -2181,7 +2271,7 @@ automatically generate an additional posting to the bucket account balancing the transaction. The following example set the @samp{Assets:Checking} as the bucket: -@smallexample +@smallexample @c input:validate bucket Assets:Checking 2011/01/25 Tom's Used Cars Expenses:Auto $ 5,500.00 @@ -2201,7 +2291,7 @@ bucket Assets:Checking Directs Ledger to replace any account matching a regex with the given account. For example: -@smallexample +@smallexample @c input:validate capture Expenses:Deductible:Medical Medical @end smallexample @@ -2228,7 +2318,7 @@ Start a block comment, closed by @code{end comment}. Pre-declare commodity names. This only has effect if @option{--strict} or @option{--pedantic} is used (see below). -@smallexample +@smallexample @c input:validate commodity $ commodity CAD @end smallexample @@ -2237,7 +2327,7 @@ The @code{commodity} directive supports several optional sub-directives, if they immediately follow the commodity directive and if they begin with whitespace: -@smallexample +@smallexample @c input:validate commodity $ note American Dollars format $1,000.00 @@ -2262,7 +2352,7 @@ The @code{default} directive marks this as the ``default'' commodity. @c instance_t::define_directive in textual.cc Allows you to define value expression for future use. For example: -@smallexample +@smallexample @c input:validate define var_name=$100 2011/12/01 Test @@ -2288,21 +2378,21 @@ use when entering many transactions with fixated prices. Thus, the following: -@smallexample +@smallexample @c input:validate fixed CAD $0.90 - 2012-04-10 Lunch in Canada - Assets:Wallet -15.50 CAD - Expenses:Food 15.50 CAD +2012-04-10 Lunch in Canada + Assets:Wallet -15.50 CAD + Expenses:Food 15.50 CAD - 2012-04-11 Second day Dinner in Canada - Assets:Wallet -25.75 CAD - Expenses:Food 25.75 CAD +2012-04-11 Second day Dinner in Canada + Assets:Wallet -25.75 CAD + Expenses:Food 25.75 CAD endfixed @end smallexample is equivalent to this: -@smallexample +@smallexample @c input:validate 2012-04-10 Lunch in Canada Assets:Wallet -15.50 CAD @{=$0.90@} Expenses:Food 15.50 CAD @{=$0.90@} @@ -2333,7 +2423,7 @@ The @code{payee} directive supports one optional sub-directive, if it immediately follows the payee directive and if it begins with whitespace: -@smallexample +@smallexample @c input:validate payee KFC alias KENTUCKY FRIED CHICKEN @end smallexample @@ -2354,7 +2444,7 @@ Ledger will display the mapped payees in @command{print} and Allows you to designate a block of transactions and assign the same tag to all. Tags can have values and may be nested. -@smallexample +@smallexample @c input:validate apply tag hastag apply tag nestedtag: true @@ -2367,34 +2457,34 @@ apply tag nestedtag: true Expenses:Books $20.00 Liabilities:MasterCard -end apply tag nestedtag +end apply tag 2011/12/01 Sale Assets:Checking:Business $ 30.00 Income:Sales -end apply tag hastag +end apply tag @end smallexample @noindent is the equivalent of: -@smallexample +@smallexample @c input:validate 2011/01/25 Tom's Used Cars - :hastag: - nestedtag: true + ; :hastag: + ; nestedtag: true Expenses:Auto $ 5,500.00 ; :nobudget: Assets:Checking 2011/01/27 Book Store - :hastag: - nestedtag: true + ; :hastag: + ; nestedtag: true Expenses:Books $20.00 Liabilities:MasterCard 2011/12/01 Sale - :hastag: + ; :hastag: Assets:Checking:Business $ 30.00 Income:Sales @end smallexample @@ -2407,7 +2497,7 @@ track. Pre-declares tag names. This only has effect if @option{--strict} or @option{--pedantic} is used (see below). -@smallexample +@smallexample @c input:validate tag Receipt tag CSV @end smallexample @@ -2416,7 +2506,7 @@ The @code{tag} directive supports two optional sub-directives, if they immediately follow the tag directive and if they begin with whitespace: -@smallexample +@smallexample @c input:validate tag Receipt check value =~ /pattern/ assert value != "foobar" @@ -2477,7 +2567,7 @@ whichever is seen last is used as the default commodity. For example, to set US dollars as the default commodity, while also setting the thousands flag and decimal flag for that commodity, use: -@smallexample +@smallexample @c input:validate D $1,000.00 @end smallexample @@ -2486,7 +2576,7 @@ Specifies a commodity conversion, where the first amount is given to be equivalent to the second amount. The first amount should use the decimal precision desired during reporting: -@smallexample +@smallexample @c input:validate C 1.00 Kb = 1024 bytes @end smallexample @@ -2612,7 +2702,7 @@ doing it. The most basic form of transaction is: -@smallexample +@smallexample @c input:validate 2012-03-10 KFC Expenses:Food $20.00 Assets:Cash $-20.00 @@ -2624,7 +2714,7 @@ posting specifies what action is taken related to that account. A transaction can have any number of postings: -@smallexample +@smallexample @c input:validate 2012-03-10 KFC Expenses:Food $20.00 Assets:Cash $-10.00 @@ -2638,7 +2728,7 @@ The first thing you can do to make things easier is elide amounts. That is, if exactly one posting has no amount specified, Ledger will infer the inverse of the other postings' amounts: -@smallexample +@smallexample @c input:validate 2012-03-10 KFC Expenses:Food $20.00 Assets:Cash $-10.00 @@ -2650,7 +2740,7 @@ If the other postings use multiple commodities, Ledger will copy the empty posting N times and fill in the negated values of the various commodities: -@smallexample +@smallexample @c input:validate 2012-03-10 KFC Expenses:Food $20.00 Expenses:Tips $2.00 @@ -2662,7 +2752,7 @@ commodities: @noindent This transaction is identical to writing: -@smallexample +@smallexample @c input:validate 2012-03-10 KFC Expenses:Food $20.00 Expenses:Tips $2.00 @@ -2680,7 +2770,7 @@ This transaction is identical to writing: You can associate a second date with a transaction by following the primary date with an equals sign: -@smallexample +@smallexample @c input:validate 2012-03-10=2012-03-08 KFC Expenses:Food $20.00 Assets:Cash $-20.00 @@ -2699,7 +2789,7 @@ only displayed by the print command. Checking accounts often use codes like DEP, XFER, etc., as well as check numbers. This is to give you a place to put those codes: -@smallexample +@smallexample @c input:validate 2012-03-10 (#100) KFC Expenses:Food $20.00 Assets:Checking @@ -2715,7 +2805,7 @@ A transaction can have a ``state'': cleared, pending, or uncleared. The default is uncleared. To mark a transaction cleared, put a * before the payee, and after date or code: -@smallexample +@smallexample @c input:validate 2012-03-10 * KFC Expenses:Food $20.00 Assets:Cash @@ -2724,7 +2814,7 @@ before the payee, and after date or code: @noindent To mark it pending, use a !: -@smallexample +@smallexample @c input:validate 2012-03-10 ! KFC Expenses:Food $20.00 Assets:Cash @@ -2742,7 +2832,7 @@ a reconciliation. When you clear a transaction, that's really just shorthand for clearing all of its postings. That is: -@smallexample +@smallexample @c input:validate 2012-03-10 * KFC Expenses:Food $20.00 Assets:Cash @@ -2751,7 +2841,7 @@ clearing all of its postings. That is: @noindent Is the same as writing: -@smallexample +@smallexample @c input:validate 2012-03-10 KFC * Expenses:Food $20.00 * Assets:Cash @@ -2761,7 +2851,7 @@ Is the same as writing: You can mark individual postings as cleared or pending, in case one ``side'' of the transaction has cleared, but the other hasn't yet: -@smallexample +@smallexample @c input:validate 2012-03-10 KFC Liabilities:Credit $100.00 * Assets:Checking @@ -2774,7 +2864,7 @@ After the payee, and after at least one tab or two spaces (or a space and a tab, which Ledger calls this a ``hard separator''), you may introduce a note about the transaction using the @samp{;} character: -@smallexample +@smallexample @c input:validate 2012-03-10 * KFC ; yum, chicken... Expenses:Food $20.00 Assets:Cash @@ -2784,7 +2874,7 @@ introduce a note about the transaction using the @samp{;} character: Notes can also appear on the next line, so long as that line begins with whitespace: -@smallexample +@smallexample @c input:validate 2012-03-10 * KFC ; yum, chicken... ; and more notes... Expenses:Food $20.00 @@ -2801,7 +2891,7 @@ significant when querying for metadata (see below). To specify that a note belongs only to one posting, place it after a hard separator after the amount, or on its own line preceded by whitespace: -@smallexample +@smallexample @c input:validate 2012-03-10 * KFC Expenses:Food $20.00 ; posting #1 note Assets:Cash @@ -2839,7 +2929,7 @@ The are two forms of metadata: tags and tag/value pairs. To tag an item, put any word not containing whitespace between two colons: -@smallexample +@smallexample @c input:validate 2012-03-10 * KFC Expenses:Food $20.00 Assets:Cash @@ -2848,7 +2938,7 @@ colons: You can gang up multiple tags by sharing colons: -@smallexample +@smallexample @c input:validate 2012-03-10 * KFC Expenses:Food $20.00 Assets:Cash @@ -2875,7 +2965,7 @@ This is useful when for example you deposit 4 checks at a time to the bank. On the bank statement, there is just one amount @samp{$400}, but you can specify from whom each check came from, as shown by example below: -@smallexample +@smallexample @c input:validate 2010-06-17 Sample Assets:Bank $400.00 Income:Check1 $-100.00 ; Payee: Person One @@ -2904,7 +2994,7 @@ To associate a value with a tag, use the syntax ``Key: Value'', where the value can be any string of characters. Whitespace is needed after the colon, and cannot appear in the Key: -@smallexample +@smallexample @c input:validate 2012-03-10 * KFC Expenses:Food $20.00 Assets:Cash @@ -2918,7 +3008,7 @@ If a metadata tag ends in ::, its value will be parsed as a value expression and stored internally as a value rather than as a string. For example, although I can specify a date textually like so: -@smallexample +@smallexample @c input:validate 2012-03-10 * KFC Expenses:Food $20.00 Assets:Cash @@ -2959,7 +3049,7 @@ reports using @option{--real}. To specify a virtual account, surround the account name with parentheses: -@smallexample +@smallexample @c input:validate 2012-03-10 * KFC Expenses:Food $20.00 Assets:Cash @@ -2970,7 +3060,7 @@ If you want, you can state that virtual postings @emph{should} balance against one or more other virtual postings by using brackets (which look ``harder'') rather than parentheses: -@smallexample +@smallexample @c input:validate 2012-03-10 * KFC Expenses:Food $20.00 Assets:Cash @@ -2985,7 +3075,7 @@ An amount is usually a numerical figure with an (optional) commodity, but it can also be any value expression. To indicate this, surround the amount expression with parentheses: -@smallexample +@smallexample @c input:validate 2012-03-10 * KFC Expenses:Food ($10.00 + $20.00) ; Ledger adds it up for you Assets:Cash @@ -3045,7 +3135,7 @@ Say your book-keeping has gotten a bit out of date, and your Ledger balance no longer matches your bank balance. You can create an adjustment transaction using balance assignments: -@smallexample +@smallexample @c input:validate 2012-03-10 Adjustment Assets:Cash = $500.00 Equity:Adjustments @@ -3091,7 +3181,7 @@ In those cases, Ledger will remember the ``cost'' of that transaction for you, and can use it during reporting in various ways. Here's an example of a stock purchase: -@smallexample +@smallexample @c input:validate 2012-03-10 My Broker Assets:Brokerage 10 AAPL Assets:Brokerage:Cash $-500.00 @@ -3107,7 +3197,7 @@ another. The resulting posting cost is $50.00 per share. You can make any posting's cost explicit using the @samp{@@} symbol after the amount or amount expression: -@smallexample +@smallexample @c input:validate 2012-03-10 My Broker Assets:Brokerage 10 AAPL @@ $50.00 Assets:Brokerage:Cash $-500.00 @@ -3116,7 +3206,7 @@ after the amount or amount expression: When you do this, since Ledger can now figure out the balancing amount from the first posting's cost, you can elide the other amount: -@smallexample +@smallexample @c input:validate 2012-03-10 My Broker Assets:Brokerage 10 AAPL @@ $50.00 Assets:Brokerage:Cash @@ -3152,7 +3242,7 @@ flag will never convert a primary commodity into any other commodity. Just as you can have amount expressions, you can have posting expressions: -@smallexample +@smallexample @c input:validate 2012-03-10 My Broker Assets:Brokerage 10 AAPL @@ ($500.00 / 10) Assets:Brokerage:Cash @@ -3160,7 +3250,7 @@ expressions: You can even have both: -@smallexample +@smallexample @c input:validate 2012-03-10 My Broker Assets:Brokerage (5 AAPL * 2) @@ ($500.00 / 10) Assets:Brokerage:Cash @@ -3173,7 +3263,7 @@ The cost figure following the @samp{@@} character specifies the @emph{per-unit} price for the commodity being transferred. If you'd like to specify the total cost instead, use @samp{@@@@}: -@smallexample +@smallexample @c input:validate 2012-03-10 My Broker Assets:Brokerage 10 AAPL @@@@ $500.00 Assets:Brokerage:Cash @@ -3181,7 +3271,7 @@ like to specify the total cost instead, use @samp{@@@@}: Ledger reads this as if you had written: -@smallexample +@smallexample @c input:validate 2012-03-10 My Broker Assets:Brokerage 10 AAPL @@ ($500.00 / 10) Assets:Brokerage:Cash @@ -3196,7 +3286,7 @@ Ledger's internal price history database. To prevent this from happening in the case of an exceptional transaction, surround the @samp{@@} or @samp{@@@@} with parentheses: -@smallexample +@smallexample @c input:validate 2012-03-10 My Brother Assets:Brokerage 1000 AAPL (@@) $1 Income:Gifts Received @@ -3214,7 +3304,7 @@ to show these hidden price figures. For example, consider the stock sale given above: -@smallexample +@smallexample @c input:validate 2012-03-10 My Broker Assets:Brokerage 10 AAPL @@ $50.00 Assets:Brokerage:Cash @@ -3229,7 +3319,7 @@ $5.00 was the price of that exchange. This becomes significant if you later sell that commodity again. For example, you might write this: -@smallexample +@smallexample @c input:validate 2012-04-10 My Broker Assets:Brokerage:Cash Assets:Brokerage -10 AAPL @@ $75.00 @@ -3238,7 +3328,7 @@ example, you might write this: And that would be perfectly fine, but how do you track the capital gains on the sale? It could be done with a virtual posting: -@smallexample +@smallexample @c input:validate 2012-04-10 My Broker Assets:Brokerage:Cash Assets:Brokerage -10 AAPL @@ $75.00 @@ -3274,7 +3364,7 @@ As a shorthand, you can specify the total price instead of the per-share price in doubled braces. This goes well with total costs, but is not required to be used with them: -@smallexample +@smallexample @c input:validate 2012-04-10 My Broker Assets:Brokerage:Cash $750.00 Assets:Brokerage -10 AAPL @{@{$500.00@}@} @@@@ $750.00 @@ -3504,7 +3594,7 @@ same query syntax as the Ledger command line. Consider this posting: -@smallexample +@smallexample @c input:validate 2012-03-10 KFC Expenses:Food $20.00 Assets:Cash @@ -3512,7 +3602,7 @@ Consider this posting: If I write this automated transaction before it in the file: -@smallexample +@smallexample @c input:validate = expr true Foo $50.00 Bar $-50.00 @@ -3521,7 +3611,7 @@ If I write this automated transaction before it in the file: Then the first transaction will be modified during parsing as if I'd written this: -@smallexample +@smallexample @c input:validate 2012-03-10 KFC Expenses:Food $20.00 Foo $50.00 @@ -3558,7 +3648,7 @@ As a special case, if an automated transaction's posting's amount (phew) has no commodity, it is taken as a multiplier upon the matching posting's cost. For example: -@smallexample +@smallexample @c input:validate = expr true Foo 50.00 Bar -50.00 @@ -3570,7 +3660,7 @@ posting's cost. For example: Then the latter transaction turns into this during parsing: -@smallexample +@smallexample @c input:validate 2012-03-10 KFC Expenses:Food $20.00 Foo $1000.00 @@ -3588,7 +3678,7 @@ posting, that expression has access to all the details of the matched posting. For example, you can refer to that posting's amount using the ``amount'' value expression variable: -@smallexample +@smallexample @c input:validate = expr true (Foo) (amount * 2) ; same as just "2" in this case @@ -3599,7 +3689,7 @@ the ``amount'' value expression variable: This becomes: -@smallexample +@smallexample @c input:validate 2012-03-10 KFC Expenses:Food $20.00 (Foo) $40.00 @@ -3614,7 +3704,7 @@ Sometimes want to refer to the account that matched in some way within the automated transaction itself. This is done by using the string $account, anywhere within the account part of the automated posting: -@smallexample +@smallexample @c input:validate = food (Budget:$account) 10 @@ -3625,7 +3715,7 @@ $account, anywhere within the account part of the automated posting: Becomes: -@smallexample +@smallexample @c input:validate 2012-03-10 KFC Expenses:Food $20.00 (Budget:Expenses:Food) $200.00 @@ -3639,7 +3729,7 @@ If the automated transaction has a transaction note, that note is copied (along with any metadata) to every posting that matches the predicate: -@smallexample +@smallexample @c input:validate = food ; Foo: Bar (Budget:$account) 10 @@ -3651,7 +3741,7 @@ predicate: Becomes: -@smallexample +@smallexample @c input:validate 2012-03-10 KFC Expenses:Food $20.00 ; Foo: Bar @@ -3665,7 +3755,7 @@ Becomes: If the automated transaction's posting has a note, that note is carried to the generated posting within the matched transaction: -@smallexample +@smallexample @c input:validate = food (Budget:$account) 10 ; Foo: Bar @@ -3677,7 +3767,7 @@ carried to the generated posting within the matched transaction: Becomes: -@smallexample +@smallexample @c input:validate 2012-03-10 KFC Expenses:Food $20.00 (Budget:Expenses:Food) $200.00 @@ -3713,7 +3803,7 @@ something like @cindex effective date of invoice -@smallexample +@smallexample @c input:validate 2008/01/01=2008/01/14 Client invoice ; estimated date you'll be paid Assets:Accounts Receivable $100.00 Income: Client name @@ -3721,7 +3811,7 @@ something like Then, when you receive the payment, you change it to -@smallexample +@smallexample @c input:validate 2008/01/01=2008/01/15 Client invoice ; actual date money received Assets:Accounts Receivable $100.00 Income: Client name @@ -3730,7 +3820,7 @@ Then, when you receive the payment, you change it to @noindent and add something like -@smallexample +@smallexample @c input:validate 2008/01/15 Client payment Assets:Checking $100.00 Assets:Accounts Receivable @@ -3738,14 +3828,14 @@ and add something like Now -@smallexample +@smallexample @c command:validate $ ledger --begin 2008/01/01 --end 2008/01/14 bal Income @end smallexample @noindent gives you your accrued income in the first two weeks of the year, and -@smallexample +@smallexample @c command:validate $ ledger --effective --begin 2008/01/01 --end 2008/01/14 bal Income @end smallexample @@ -3813,7 +3903,7 @@ complex, but if you have further interest, please consult the Web. Ledger makes this otherwise difficult law very easy. Just set up an automated posting at the top of your ledger file: -@smallexample +@smallexample @c input:C371854 ; This automated transaction will compute Huqúqu'lláh based on this ; journal's postings. Any that match will affect the ; Liabilities:Huququ'llah account by 19% of the value of that posting. @@ -3827,12 +3917,24 @@ ledger file. If any match the given value expression, 19% of the posting's value is applied to the @samp{Liabilities:Huququ'llah} account. So, if $1000 is earned from @samp{Income:Salary}, $190 is added to @samp{Liabilities:Huqúqu'lláh}; if $1000 is spent on Rent, -$190 is subtracted. The ultimate balance of Huqúqu'lláh reflects how +$190 is subtracted. + +@smallexample @c input:C371854 +2003/01/01 (99) Salary + Income:Salary -$1000 + Assets:Checking + +2003/01/01 (100) Rent + Expenses:Rent $500 + Assets:Checking +@end smallexample + +The ultimate balance of Huqúqu'lláh reflects how much is owed in order to fulfill one's obligation to Huqúqu'lláh. When ready to pay, just write a check to cover the amount shown in @samp{Liabilities:Huququ'llah}. That transaction would look like: -@smallexample +@smallexample @c input:validate 2003/01/01 (101) Baha'i Huqúqu'lláh Trust Liabilities:Huququ'llah $1,000.00 Assets:Checking @@ -3841,10 +3943,14 @@ When ready to pay, just write a check to cover the amount shown in That's it. To see how much Huqúq is currently owed based on your ledger transactions, use: -@smallexample +@smallexample @c command:C371854 $ ledger balance Liabilities:Huquq @end smallexample +@smallexample @c output:C371854 + $-95 Liabilities:Huququ'llah +@end smallexample + This works fine, but omits one aspect of the law: that Huquq is only due once the liability exceeds the value of 19 mithqáls of gold (which is roughly 2.22 ounces). So what we want is for the liability to @@ -3852,6 +3958,7 @@ appear in the balance report only when it exceeds the present day value of 2.22 ounces of gold. This can be accomplished using the command: +@c TODO: fix this @smallexample $ ledger -Q -t "/Liab.*Huquq/?(a/P@{2.22 AU@}<=@{-1.0@}&a):a" bal liab @end smallexample @@ -3861,6 +3968,7 @@ Huqúqu'lláh is reported only if its value exceeds that of 2.22 ounces of gold. If you wish the liability to be reflected in the parent subtotal either way, use this instead: +@c TODO: fix this @smallexample $ ledger -Q -T "/Liab.*Huquq/?(O/P@{2.22 AU@}<=@{-1.0@}&O):O" bal liab @end smallexample @@ -3869,7 +3977,7 @@ In some cases, you may wish to refer to the account of whichever posting matched your automated transaction's value expression. To do this, use the special account name @samp{$account}: -@smallexample +@smallexample @c input:validate = /^Some:Long:Account:Name/ [$account] -0.10 [Savings] 0.10 @@ -4007,9 +4115,12 @@ This second example looks for any account with @samp{Bo}, which is If you want to know exactly how much you have spent in a particular account on a particular payee, the following are equivalent: -@smallexample +@smallexample @c command:validate $ ledger balance Auto:Fuel and Chevron -$ ledger balance --limit "(account=~/Fuel/) & (payee=~/Chev/)" +@end smallexample + +@smallexample @c command:validate +$ ledger balance --limit 'account=~/Fuel/' and 'payee=~/Chev/' @end smallexample @noindent @@ -4022,8 +4133,8 @@ possibilities. If you want to exclude specific accounts from the report, you can exclude multiple accounts with parentheses: -@smallexample -$ ledger bal Expenses and not \(Expenses:Drinks or Expenses:Candy or Expenses:Gifts\) +@smallexample @c command:validate +$ ledger bal Expenses and not (Expenses:Drinks or Expenses:Candy or Expenses:Gifts) @end smallexample @node Controlling Formatting, , Controlling the Accounts and Payees, Balance Reports @@ -4039,8 +4150,9 @@ you want, or interface Ledger with other programs. A query such as the following shows all expenses since last October, sorted by total: +@c TODO: does not validate with @c command:validate, because "last oct" is split at the space @smallexample -$ ledger -b "last oct" -S T bal ^expenses +$ ledger -b "last oct" -f sample.dat -S T bal ^expenses @end smallexample From left to right the options mean: Show transactions since last @@ -4062,7 +4174,7 @@ for all accounts that begin with @samp{expenses}. The following query makes it easy to see monthly expenses, with each month's expenses sorted by the amount: -@smallexample +@smallexample @c command:validate $ ledger -M --period-sort "(amount)" reg ^expenses @end smallexample @@ -4070,7 +4182,7 @@ Now, you might wonder where the money came from to pay for these things. To see that report, add @option{--related (-r)}, which shows the ``related account'' postings: -@smallexample +@smallexample @c command:validate $ ledger -M --period-sort "(amount)" -r reg ^expenses @end smallexample @@ -4080,8 +4192,8 @@ requires the use of a display predicate, since the postings calculated must match @samp{^expenses}, while the postings displayed must match @samp{mastercard}. The command would be: -@smallexample -$ ledger -M -r --display "account =~ /mastercard/" reg ^expenses +@smallexample @c command:validate +$ ledger -M -r --display 'account=~/mastercard/' reg ^expenses @end smallexample This query says: Report monthly subtotals; report the ``related @@ -4091,8 +4203,8 @@ postings matching @samp{^expenses}. This works just as well for report the overall total, too: -@smallexample -$ ledger -s -r --display "account =~ /mastercard/" reg ^expenses +@smallexample @c command:validate +$ ledger -s -r --display "account=~/mastercard/" reg ^expenses @end smallexample The @option{--subtotal (-s)} option subtotals all postings, just as @@ -4155,7 +4267,7 @@ actual balances. For the three instruments listed above, those automatic transactions would look like: -@smallexample +@smallexample @c input:validate ; ; automatic calculations for asset allocation tracking ; @@ -4187,6 +4299,7 @@ the various asset classes how do we get a report that tells us our current allocation? Using the balance command and some tricky formatting! +@c TODO: does not @c command:validate due to multiple lines @smallexample ledger bal Allocation --current --format "\ %-17((depth_spacer)+(partial_account))\ @@ -4476,7 +4589,7 @@ directive to rewrite the @code{payee} field based on some rules. Then you can use the account and its @code{payee} directive to specify the account. I use it like this, for example: -@smallexample +@smallexample @c input:validate payee Aldi alias ^ALDI SUED SAGT DANKE account Aufwand:Einkauf:Lebensmittel @@ -5215,14 +5328,14 @@ instead, precede the regular expression with @samp{payee} or totals for rent, food and movies, but only those whose payee matches Freddie: -@smallexample +@smallexample @c command:validate $ ledger bal rent food movies payee freddie @end smallexample @noindent or -@smallexample +@smallexample @c command:validate $ ledger bal rent food movies @@freddie @end smallexample @@ -5295,7 +5408,7 @@ Print summary of all options. @item --version @itemx -v -Print version of ledger executable. +Print version information and exit. @item --file @var{FILE} @itemx -f @var{FILE} @@ -5585,7 +5698,7 @@ Report last known market value. @item --gain @itemx -G -Report net gain loss for commodities that have a price history. +Report net gain or loss for commodities that have a price history. @end ftable @@ -5774,8 +5887,11 @@ $ ledger -f drewr3.dat bal --no-total --master-account HUMBUG $ 200.00 Mortgage:Principal @end smallexample +@item --no-aliases +Ledger does not expand any aliases if this option is specified. + @item --pedantic -FIX THIS ENTRY @c FIXME thdox +Accounts, tags or commodities not previously declared will cause errors. @item --permissive FIX THIS ENTRY @c FIXME thdox @@ -5801,6 +5917,11 @@ correct, and if it finds a new account or commodity (same as a misspelled commodity or account) it will issue a warning giving you the file and line number of the problem. +@item --recursive-aliases +Normally, ledger only expands aliases once. With this option, ledger tries +to expand the result of alias expansion recursively, until no more expansions +apply. + @item --time-colon The @option{--time-colon} option will display the value for a seconds based commodity as real hours and minutes. @@ -5835,7 +5956,7 @@ desired width. @item --account @var{STR} Prepend @var{STR} to all accounts reported. That is, the option -@samp{--account Personal} would track @samp{Personal:} to the beginning +@samp{--account Personal} would tack @samp{Personal:} to the beginning of every account reported in a balance report or register report. @item --account-width @var{INT} @@ -5908,8 +6029,8 @@ that date will be ignored. Print the entire line in bold if the given value expression is true (@pxref{Value Expressions}). -@smallexample -$ ledger reg Expenses --begin Dec --bold-if "amount > 100" +@smallexample @c command:validate +$ ledger reg Expenses --begin Dec --bold-if "amount>100" @end smallexample @noindent @@ -5961,15 +6082,15 @@ Strings}). The default is: @item --collapse @itemx -n -By default ledger prints all account in an account tree. With -@option{--collapse} it print only the top level account specified. +By default ledger prints all accounts in an account tree. With +@option{--collapse} it prints only the top level account specified. @item --collapse-if-zero Collapse the account display only if it has a zero balance. @item --color @itemx --ansi -Use color is the tty supports it. +Use color if the terminal supports it. @item --columns @var{INT} Specify the width of the @command{register} report in characters. @@ -6005,7 +6126,7 @@ Transform the date of the transaction using @var{EXPR}. @item --date-format @var{DATE_FORMAT} @itemx -y @var{DATE_FORMAT} -Specify format ledger should use to print dates (@pxref{Date and Time +Specify the format ledger should use to print dates (@pxref{Date and Time Format Codes}). @item --date-width @var{INT} @@ -6085,21 +6206,21 @@ Report each posting’s deviation from the average. It is only meaningful in the register and prices reports. @item --display @var{EXPR} -Display lines that satisfy the expression given. +Display lines that satisfy the expression @var{EXPR}. @item --display-amount @var{EXPR} -Apply a transform to the @emph{displayed} amount. This occurs after +Apply a transformation to the @emph{displayed} amount. This occurs after calculations occur. @item --display-total @var{EXPR} -Apply a transform to the @emph{displayed} total. This occurs after +Apply a transformation to the @emph{displayed} total. This occurs after calculations occur. @item --dow @itemx --days-of-week Group transactions by the days of the week. -@smallexample +@smallexample @c command:validate $ ledger reg Expenses --dow --collapse @end smallexample @@ -6111,7 +6232,7 @@ Will print all Expenses totaled for each day of the week. Include empty accounts in the report. @item --end @var{DATE} -Specify the end @var{DATE} for transaction to be considered in the +Specify the end @var{DATE} for a transaction to be considered in the report. @item --equity @@ -6132,8 +6253,8 @@ Force the full names of accounts to be used in the balance report. The balance report will not use an indented tree. @item --force-color -Output tty color codes even if the tty doesn't support them. Useful -for TTY that don't advertise their capabilities correctly. +Output TTY color codes even if the TTY doesn't support them. Useful +for TTYs that don't advertise their capabilities correctly. @item --force-pager Force Ledger to paginate its output. @@ -6143,7 +6264,7 @@ Force Ledger to paginate its output. Continue forecasting while @var{VEXPR} is true. @item --forecast-years @var{INT} -Forecast at most @var{INT} years in the future. +Forecast at most @var{INT} years into the future. @item --format @var{FORMAT_STRING} @itemx -F @var{FORMAT_STRING} @@ -6199,7 +6320,7 @@ Use @code{Expected} amounts in calculations. In the case that you know that amount a transaction should be, but the actual transaction has the wrong value you can use metadata to put in the expected amount: -@smallexample +@smallexample @c input:validate 2012-03-12 Paycheck Income $-990; Expected:: $-1000.00 Checking @@ -6251,6 +6372,9 @@ Specify the width of the Meta column used for the @option{--meta @itemx -M Synonym for @samp{--period "monthly"}. +@item --no-aliases +Aliases are completely ignored. + @item --no-color Suppress any color TTY output. @@ -6286,7 +6410,7 @@ a date and payee for each transaction. @item --payee-width @var{INT} Set the number of columns dedicated to the payee in the register -report. +report to @var{INT}. @item --pending Use only postings that are marked pending. @@ -6354,13 +6478,13 @@ Show primary dates for all calculations (@pxref{Effective Dates}). @item --quantity @itemx -O -FIX THIS ENTRY +Report commodity totals (this is the default). @item --quarterly Synonym for @samp{--period "quarterly"}. @item --raw -In the print report, show transactions using the exact same syntax as +In the @command{print} report, show transactions using the exact same syntax as specified by the user in their data file. Don't do any massaging or interpreting. Can be useful for minor cleanups, like just aligning amounts. @@ -6371,7 +6495,7 @@ Account using only real transactions ignoring virtual and automatic transactions. @item --register-format @var{FORMAT_STRING} -FIX THIS ENTRY @c FIXME thdox +Define the output format for the @command{register} report. @item --related In a register report show the related account. This is the other @@ -6395,7 +6519,7 @@ FIX THIS ENTRY FIX THIS ENTRY @c FIXME thdox @item --seed @var{FIXME} -Set the random seed for the @code{generate} command. Used as part of +Set the random seed to @var{FIXME} for the @code{generate} command. Used as part of development testing. @item --sort @var{VEXPR} @@ -6432,7 +6556,7 @@ Define a value expression used to calculate the total in reports. @item --total-data @itemx -J -FIX THIS ENTRY +Show only dates and totals to format the output for plots. @item --total-width @var{INT} Set the width of the total field in the register report. @@ -6445,7 +6569,7 @@ as it considers sub-names within the account name (that style is called ``abbreviate''). @item --unbudgeted -FIX THIS ENTRY +Show only un-budgeted postings. @item --uncleared @itemx -U @@ -6457,12 +6581,12 @@ FIX THIS ENTRY @item --unrealized-gains @var{STR} Allow the user to specify what account name should be used for unrealized gains. Defaults to @samp{"Equity:Unrealized Gains"}. -Often set in one's @file{~/.ledgerrc} file to change default. +Often set in one's @file{~/.ledgerrc} file to change the default. @item --unrealized-losses @var{STR} Allow the user to specify what account name should be used for unrealized gains. Defaults to @samp{"Equity:Unrealized Losses"}. -Often set in one's @file{~/.ledgerrc} file to change default. +Often set in one's @file{~/.ledgerrc} file to change the default. @item --unround Perform all calculations without rounding and display results to full @@ -6582,7 +6706,7 @@ expression @var{EXPR}. This is most often useful when reporting monthly expenses, in order to view the highest expense categories at the top of each month: -@smallexample +@smallexample @c input:validate $ ledger -M --period-sort -At reg ^Expenses @end smallexample @@ -6823,6 +6947,7 @@ register report, for example, but they will not be displayed. This is useful for seeing last month's checking postings, against a running balance which includes all posting values: +@c TODO: does not @c command:validate due to space in "last month" @smallexample $ ledger -d "d>=[last month]" reg checking @end smallexample @@ -6831,6 +6956,7 @@ The output from this command is very different from the following, whose running total includes only postings from the last month onward: +@c TODO: does not @c command:validate due to space in "last month" @smallexample $ ledger -p "last month" reg checking @end smallexample @@ -7072,7 +7198,7 @@ If no @var{VALUE} property is specified, each posting is assumed to have a default, as if you'd specified a global, automated transaction as follows: -@smallexample +@smallexample @c input:validate = expr true ; VALUE:: market(amount, date, exchange) @end smallexample @@ -7086,7 +7212,7 @@ This definition emulates the present day behavior of @option{--market One thing many people have wanted to do is to fixate the valuation of old European currencies in terms of the Euro after a certain date: -@smallexample +@smallexample @c input:validate = expr commodity == "DM" ; VALUE:: date < [Jun 2008] ? market(amount, date, exchange) : 1.44 EUR @end smallexample @@ -7098,7 +7224,7 @@ past June 2008, use a fixed price for converting Deutsch Mark to Euro. Or how about never re-valuating commodities used in Expenses, since they cannot have a different future value: -@smallexample +@smallexample @c input:validate = /^Expenses:/ ; VALUE:: market(amount, post.date, exchange) @end smallexample @@ -7110,7 +7236,7 @@ the value of @option{--now @var{DATE}} (defaults to today). Or how about valuating miles based on a reimbursement rate during a specific time period: -@smallexample +@smallexample @c input:validate = expr commodity == "miles" and date >= [2007] and date < [2008] ; VALUE:: market($1.05, date, exchange) @end smallexample @@ -7124,7 +7250,7 @@ Note that you can have a valuation expression specific to a particular posting or transaction, by overriding these general defaults using specific meta-data: -@smallexample +@smallexample @c input:validate 2010-12-26 Example Expenses:Food $20 ; Just to be silly, always valuate *these* $20 as 30 DM, no matter what @@ -7144,7 +7270,7 @@ which allows you to report most everything in EUR if you use @samp{-X EUR}, except for certain accounts or postings which should always be valuated in another currency. For example: -@smallexample +@smallexample @c input:validate = /^Assets:Brokerage:CAD$/ ; Always report the value of commodities in this account in ; terms of present day dollars, despite what was asked for @@ -7325,11 +7451,11 @@ your expectations. To start keeping a budget, put some periodic transactions (@pxref{Periodic Transactions}) at the top of your ledger file. A -period transaction is almost identical to a regular transaction, except +periodic transaction is almost identical to a regular transaction, except that it begins with a tilde and has a period expression in place of a payee. For example: -@smallexample +@smallexample @c input:validate ~ Monthly Expenses:Rent $500.00 Expenses:Food $450.00 @@ -7346,7 +7472,7 @@ payee. For example: Assets @end smallexample -These two period transactions give the usual monthly expenses, as well +These two periodic transactions give the usual monthly expenses, as well as one typical yearly expense. For help on finding out what your average monthly expense is for any category, use a command like: @@ -7356,17 +7482,17 @@ $ ledger -p "this year" --monthly --average balance ^expenses The reported totals are the current year's average for each account. -Once these period transactions are defined, creating a budget report is +Once these periodic transactions are defined, creating a budget report is as easy as adding @option{--budget} to the command-line. For example, a typical monthly expense report would be: -@smallexample +@smallexample @c command:validate $ ledger --monthly register ^expenses @end smallexample To see the same report balanced against your budget, use: -@smallexample +@smallexample @c command:validate $ ledger --budget --monthly register ^expenses @end smallexample @@ -7375,7 +7501,7 @@ To see all expenses balanced against the budget, use @option{--add-budget}. You can even see only the un-budgeted expenses using @option{--unbudgeted}: -@smallexample +@smallexample @c command:validate $ ledger --unbudgeted --monthly register ^expenses @end smallexample @@ -7387,10 +7513,10 @@ You can also use these flags with the @command{balance} command. Sometimes it's useful to know what your finances will look like in the future, such as determining when an account will reach zero. Ledger -makes this easy to do, using the same period transactions as are used +makes this easy to do, using the same periodic transactions as are used for budgeting. An example forecast report can be generated with: -@smallexample +@smallexample @c command:validate $ ledger --forecast "T>@{\$-500.00@}" register ^assets ^liabilities @end smallexample @@ -7401,7 +7527,7 @@ show you what the total afterwards would be. Forecasting can also be used with the balance report, but by date only, and not against the running total: -@smallexample +@smallexample @c command:validate $ ledger --forecast "d<[2010]" bal ^assets ^liabilities @end smallexample @@ -7411,7 +7537,7 @@ $ ledger --forecast "d<[2010]" bal ^assets ^liabilities Ledger directly supports ``timelog'' entries, which have this form: -@smallexample +@smallexample @c input:validate i 2013/03/28 22:13:00 ACCOUNT[ PAYEE] o 2013/03/29 03:39:00 @end smallexample @@ -7468,7 +7594,7 @@ addition to a set of functions and variables. @c a display predicate that I use with the @command{balance} command: @c @smallexample -@c ledger -d /^Liabilities/?T<0:UT>100 balance +@c ledger -d '/^Liabilities/?T<0:UT>100' balance @c @end smallexample @c The effect is that account totals are displayed only if: 1) A @@ -8365,7 +8491,7 @@ Ledger data exists in one of two forms: raw and cooked. Raw objects are what you get from a traversal like the above, and represent exactly what was seen in the data file. Consider this journal: -@smallexample +@smallexample @c input:validate = true (Assets:Cash) $100 @@ -8376,7 +8502,7 @@ was seen in the data file. Consider this journal: In this case, the @emph{raw} regular transaction in this file is: -@smallexample +@smallexample @c input:validate 2012-03-01 KFC Expenses:Food $100 Assets:Credit @@ -8384,7 +8510,7 @@ In this case, the @emph{raw} regular transaction in this file is: While the @emph{cooked} form is: -@smallexample +@smallexample @c input:validate 2012-03-01 KFC Expenses:Food $100 Assets:Credit $-100 @@ -8725,7 +8851,7 @@ one or more @dfn{postings}, which describe how @dfn{amounts} flow from one @dfn{account} to another. Here is an example of the simplest of journal files: -@smallexample +@smallexample @c input:validate 2010/05/31 Just an example Expenses:Some:Account $100.00 Income:Another:Account @@ -8749,7 +8875,7 @@ It is also typical, though not enforced, to think of the first posting as the destination, and the final as the source. Thus, the amount of the first posting is typically positive. Consider: -@smallexample +@smallexample @c input:validate 2010/05/31 An income transaction Assets:Checking $1,000.00 Income:Salary @@ -8797,7 +8923,7 @@ spaces between the end of the post and the beginning of the amount In the simplest form, bare decimal numbers are accepted: -@smallexample +@smallexample @c input:validate 2010/05/31 An income transaction Assets:Checking 1000.00 Income:Salary @@ -8904,10 +9030,10 @@ amount for a posting. But what if the amount you paid for something was in one commodity, and the amount received was another? There are two main ways to express this: -@smallexample +@smallexample @c input:validate 2010/05/31 Farmer's Market Assets:My Larder 100 apples - Assets:Checking $20.00 + Assets:Checking -$20.00 @end smallexample In this example, you have paid twenty dollars for one hundred apples. @@ -8915,7 +9041,7 @@ The cost to you is twenty cents per apple, and Ledger calculates this implied cost for you. You can also make the cost explicit using a @dfn{cost amount}: -@smallexample +@smallexample @c input:validate 2010/05/31 Farmer's Market Assets:My Larder 100 apples @@ $0.200000 Assets:Checking @@ -8926,7 +9052,7 @@ amount; and since cost amount are @emph{unobserved}, the use of six decimal places has no effect on how dollar amounts are displayed in the final report. You can also specify the @dfn{total cost}: -@smallexample +@smallexample @c input:validate 2010/05/31 Farmer's Market Assets:My Larder 100 apples @@@@ $20 Assets:Checking @@ -8936,7 +9062,7 @@ These three forms have identical meaning. In most cases the first is preferred, but the second two are necessary when more than two postings are involved: -@smallexample +@smallexample @c input:validate 2010/05/31 Farmer's Market Assets:My Larder 100 apples @@ $0.200000 Assets:My Larder 100 pineapples @@ $0.33 @@ -8959,10 +9085,10 @@ to buy and sells units of the other commodity. In the fruit examples above, dollars are the primary commodity. This is decided by Ledger on the placement of the commodity in the transaction: -@smallexample +@smallexample @c input:validate 2010/05/31 Sample Transaction Expenses 100 secondary - Assets 50 primary + Assets -50 primary 2010/05/31 Sample Transaction Expenses 100 secondary @@ 0.5 primary @@ -9188,7 +9314,7 @@ true FIX THIS ENTRY @c FIXME thdox @item template -Shows the insertion template that @command{xact} sub-command generates. +Shows the insertion template that the @command{xact} sub-command generates. This is a debugging command. @end ftable @@ -9299,7 +9425,7 @@ The following journal file is included with the source distribution of ledger. It is called @file{drewr.dat} and exhibits many ledger features, include automatic and virtual transactions, -@smallexample +@smallexample @c input:validate ; -*- ledger -*- = /^Income/ @@ -9397,7 +9523,7 @@ $ ledger register Checking --sort d -d 'd>[2011/04/01]' until 2011/05/25 @node Ledger Files, , Invoking Ledger, Cookbook @subsection Ledger Files -@smallexample +@smallexample @c input:validate = /^Income:Taxable/ (Liabilities:Tithe Owed) -0.1 = /Noah/ diff --git a/src/journal.cc b/src/journal.cc index a014a964..160abe06 100644 --- a/src/journal.cc +++ b/src/journal.cc @@ -96,6 +96,7 @@ void journal_t::initialize() check_payees = false; day_break = false; checking_style = CHECK_PERMISSIVE; + recursive_aliases = false; } void journal_t::add_account(account_t * acct) @@ -121,26 +122,9 @@ account_t * journal_t::find_account_re(const string& regexp) account_t * journal_t::register_account(const string& name, post_t * post, account_t * master_account) { - account_t * result = NULL; - - // If there any account aliases, substitute before creating an account + // If there are any account aliases, substitute before creating an account // object. - if (account_aliases.size() > 0) { - accounts_map::const_iterator i = account_aliases.find(name); - if (i != account_aliases.end()) { - result = (*i).second; - } else { - // only check the very first account for alias expansion, in case - // that can be expanded successfully - size_t colon = name.find(':'); - if(colon != string::npos) { - accounts_map::const_iterator j = account_aliases.find(name.substr(0, colon)); - if (j != account_aliases.end()) { - result = find_account((*j).second->fullname() + name.substr(colon)); - } - } - } - } + account_t * result = expand_aliases(name); // Create the account object and associate it with the journal; this // is registering the account. @@ -178,7 +162,63 @@ account_t * journal_t::register_account(const string& name, post_t * post, } } } + return result; +} + +account_t * journal_t::expand_aliases(string name) { + // Aliases are expanded recursively, so if both alias Foo=Bar:Foo and + // alias Bar=Baaz:Bar are in effect, first Foo will be expanded to Bar:Foo, + // then Bar:Foo will be expanded to Baaz:Bar:Foo. + // The expansion loop keeps a list of already expanded names in order to + // prevent infinite excursion. Each alias may only be expanded at most once. + account_t * result = NULL; + if(no_aliases) + return result; + + bool keep_expanding = true; + std::list<string> already_seen; + // loop until no expansion can be found + do { + if (account_aliases.size() > 0) { + accounts_map::const_iterator i = account_aliases.find(name); + if (i != account_aliases.end()) { + if(std::find(already_seen.begin(), already_seen.end(), name) != already_seen.end()) { + throw_(std::runtime_error, + _f("Infinite recursion on alias expansion for %1%") + % name); + } + // there is an alias for the full account name, including colons + already_seen.push_back(name); + result = (*i).second; + name = result->fullname(); + } else { + // only check the very first account for alias expansion, in case + // that can be expanded successfully + size_t colon = name.find(':'); + if(colon != string::npos) { + string first_account_name = name.substr(0, colon); + accounts_map::const_iterator j = account_aliases.find(first_account_name); + if (j != account_aliases.end()) { + if(std::find(already_seen.begin(), already_seen.end(), first_account_name) != already_seen.end()) { + throw_(std::runtime_error, + _f("Infinite recursion on alias expansion for %1%") + % first_account_name); + } + already_seen.push_back(first_account_name); + result = find_account((*j).second->fullname() + name.substr(colon)); + name = result->fullname(); + } else { + keep_expanding = false; + } + } else { + keep_expanding = false; + } + } + } else { + keep_expanding = false; + } + } while(keep_expanding && recursive_aliases); return result; } diff --git a/src/journal.h b/src/journal.h index 0d06e9f0..e4763482 100644 --- a/src/journal.h +++ b/src/journal.h @@ -131,6 +131,8 @@ public: bool force_checking; bool check_payees; bool day_break; + bool recursive_aliases; + bool no_aliases; payee_mappings_t payee_mappings; account_mappings_t account_mappings; accounts_map account_aliases; @@ -167,6 +169,8 @@ public: account_t * find_account(const string& name, bool auto_create = true); account_t * find_account_re(const string& regexp); + account_t * expand_aliases(string name); + account_t * register_account(const string& name, post_t * post, account_t * master = NULL); string register_payee(const string& name, xact_t * xact); diff --git a/src/pstream.h b/src/pstream.h index bcbf6755..ed314068 100644 --- a/src/pstream.h +++ b/src/pstream.h @@ -83,7 +83,10 @@ class ptristream : public std::istream virtual pos_type seekoff(off_type off, ios_base::seekdir way, ios_base::openmode) { - switch (way) { + // cast to avoid gcc '-Wswitch' warning + // as ios_base::beg/cur/end are not necesssarily values of 'way' enum type ios_base::seekdir + // based on https://svn.boost.org/trac/boost/ticket/7644 + switch (static_cast<int>(way)) { case std::ios::cur: setg(ptr, gptr()+off, ptr+len); break; diff --git a/src/session.cc b/src/session.cc index dbd49a0e..b386607a 100644 --- a/src/session.cc +++ b/src/session.cc @@ -113,6 +113,11 @@ std::size_t session_t::read_data(const string& master_account) if (HANDLED(day_break)) journal->day_break = true; + if (HANDLED(recursive_aliases)) + journal->recursive_aliases = true; + if (HANDLED(no_aliases)) + journal->no_aliases = true; + if (HANDLED(permissive)) journal->checking_style = journal_t::CHECK_PERMISSIVE; else if (HANDLED(pedantic)) @@ -344,12 +349,18 @@ option_t<session_t> * session_t::lookup_option(const char * p) case 'm': OPT(master_account_); break; + case 'n': + OPT(no_aliases); + break; case 'p': OPT(price_db_); else OPT(price_exp_); else OPT(pedantic); else OPT(permissive); break; + case 'r': + OPT(recursive_aliases); + break; case 's': OPT(strict); break; diff --git a/src/session.h b/src/session.h index 3572b991..d20ba74a 100644 --- a/src/session.h +++ b/src/session.h @@ -109,6 +109,8 @@ public: HANDLER(permissive).report(out); HANDLER(price_db_).report(out); HANDLER(price_exp_).report(out); + HANDLER(recursive_aliases).report(out); + HANDLER(no_aliases).report(out); HANDLER(strict).report(out); HANDLER(value_expr_).report(out); } @@ -164,6 +166,8 @@ public: OPTION(session_t, price_db_); OPTION(session_t, strict); OPTION(session_t, value_expr_); + OPTION(session_t, recursive_aliases); + OPTION(session_t, no_aliases); }; /** diff --git a/src/textual.cc b/src/textual.cc index d8648c93..627a1835 100644 --- a/src/textual.cc +++ b/src/textual.cc @@ -977,6 +977,11 @@ void instance_t::account_alias_directive(account_t * account, string alias) // (account), add a reference to the account in the `account_aliases' // map, which is used by the post parser to resolve alias references. trim(alias); + // Ensure that no alias like "alias Foo=Foo" is registered. + if ( alias == account->fullname()) { + throw_(parse_error, _f("Illegal alias %1%=%2%") + % alias % account->fullname()); + } std::pair<accounts_map::iterator, bool> result = context.journal->account_aliases.insert (accounts_map::value_type(alias, account)); diff --git a/src/wcwidth.cc b/src/wcwidth.cc index 71eaf6d6..c23f83d7 100644 --- a/src/wcwidth.cc +++ b/src/wcwidth.cc @@ -69,8 +69,8 @@ namespace ledger { namespace { struct interval { - int first; - int last; + boost::uint32_t first; + boost::uint32_t last; }; } diff --git a/test/DocTests.py b/test/DocTests.py index cc540aa9..d2931686 100755 --- a/test/DocTests.py +++ b/test/DocTests.py @@ -22,6 +22,7 @@ class DocTests: self.testin_token = 'command' self.testout_token = 'output' self.testdat_token = 'input' + self.validate_token = 'validate' self.testwithdat_token = 'with_input' def read_example(self): @@ -31,14 +32,14 @@ class DocTests: line = self.file.readline() self.current_line += 1 if len(line) <= 0 or endexample.match(line): break - example += line + example += line.replace("@@","@").replace("@{","{").replace("@}","}") return example def test_id(self, example): return hashlib.sha1(example.rstrip()).hexdigest()[0:7].upper() def find_examples(self): - startexample = re.compile(r'^@smallexample\s+@c\s+(%s|%s|%s)(?::([\dA-Fa-f]+))?(?:,(.*))?' + startexample = re.compile(r'^@smallexample\s+@c\s+(%s|%s|%s)(?::([\dA-Fa-f]+|validate))?(?:,(.*))?' % (self.testin_token, self.testout_token, self.testdat_token)) while True: line = self.file.readline() @@ -67,10 +68,16 @@ class DocTests: test_id = self.test_id(example) if test_kind == self.testin_token: print >> sys.stderr, 'Use', self.test_id(example) - elif test_kind == self.testin_token and test_id != self.test_id(example): + elif test_kind == self.testin_token and test_id != self.validate_token and test_id != self.test_id(example): print >> sys.stderr, 'Expected test id', test_id, 'for example' \ , test_kind, 'on line', test_begin_line, 'to be', self.test_id(example) + if test_id == self.validate_token: + test_id = "Val-" + str(test_begin_line) + if test_kind == self.testin_token: + test_kind = "validate-command" + elif test_kind == self.testdat_token: + test_kind = "validate-data" try: self.examples[test_id] except KeyError: @@ -91,10 +98,17 @@ class DocTests: } def parse_command(self, test_id, example): + validate_command = False try: command = example[self.testin_token][self.testin_token] except KeyError: - return None + if 'validate-data' in example: + command = '$ ledger bal' + elif 'validate-command' in example: + validate_command = True + command = example['validate-command']['validate-command'] + else: + return None command = command.rstrip().split() if command[0] == '$': command.remove('$') @@ -110,12 +124,18 @@ class DocTests: except ValueError: findex = index+1 command.insert(findex, '--file') - command.insert(findex+1, test_id + '.dat') + if validate_command: + command.insert(findex+1, 'sample.dat') + else: + command.insert(findex+1, test_id + '.dat') return (command, findex+1) def test_examples(self): failed = set() for test_id in self.examples: + validation = False + if "validate-data" in self.examples[test_id] or "validate-command" in self.examples[test_id]: + validation = True example = self.examples[test_id] try: (command, findex) = self.parse_command(test_id, example) @@ -135,9 +155,12 @@ class DocTests: with_input = example[self.testin_token]['opts'][self.testwithdat_token] input = self.examples[with_input][self.testdat_token][self.testdat_token] except KeyError: - input = None + try: + input = example['validate-data']['validate-data'] + except KeyError: + input = None - if command and output: + if command and (output or validation): test_file_created = False if findex: scriptpath = os.path.dirname(os.path.realpath(__file__)) @@ -150,11 +173,13 @@ class DocTests: f.write(input) elif os.path.exists(test_input_dir + test_file): command[findex] = test_input_dir + test_file + error = False try: verify = subprocess.check_output(command) except: verify = str() - valid = (output == verify) + error = True + valid = (output == verify) or (not error and validation) if valid and test_file_created: os.remove(test_file) if self.verbose > 0: @@ -166,9 +191,10 @@ class DocTests: failed.add(test_id) if self.verbose > 1: print ' '.join(command) - for line in unified_diff(output.split('\n'), verify.split('\n'), fromfile='generated', tofile='expected'): - print(line) - print + if not validation: + for line in unified_diff(output.split('\n'), verify.split('\n'), fromfile='generated', tofile='expected'): + print(line) + print if not self.verbose: print if len(failed) > 0: diff --git a/test/baseline/dir-alias-fail.test b/test/baseline/dir-alias-fail.test new file mode 100644 index 00000000..e063a330 --- /dev/null +++ b/test/baseline/dir-alias-fail.test @@ -0,0 +1,12 @@ +--pedantic +--explicit +alias Foo=Foo + +2011-01-01 Test + Foo 10 EUR + Bar +test source -> 1 +__ERROR__ +While parsing file "$FILE", line 3: +Error: Illegal alias Foo=Foo +end test diff --git a/test/baseline/dir-alias-recursive.test b/test/baseline/dir-alias-recursive.test new file mode 100644 index 00000000..d9addcd1 --- /dev/null +++ b/test/baseline/dir-alias-recursive.test @@ -0,0 +1,12 @@ +alias A=B:A +alias B=C:B +alias C=D:C + +2001-01-01 Test + A 10 EUR + Foo + +test reg --recursive-aliases +01-Jan-01 Test D:C:B:A 10 EUR 10 EUR + Foo -10 EUR 0 +end test diff --git a/test/baseline/dir-alias.test b/test/baseline/dir-alias.test new file mode 100644 index 00000000..6245d944 --- /dev/null +++ b/test/baseline/dir-alias.test @@ -0,0 +1,13 @@ +alias A=B:A +alias B=C:B +alias C=D:C + +2001-01-01 Test + A 10 EUR + Foo + +test reg +01-Jan-01 Test B:A 10 EUR 10 EUR + Foo -10 EUR 0 +end test + |