Commit Graph

9546 Commits

Author SHA1 Message Date
Vladimir Zhelezov
985f041d81 Make hard-coded completion word lists easy to find
A quick search for `wordlist=` should be enough to find hard-coded
completions if we need to change them
2021-02-28 08:33:17 +01:00
Vladimir Zhelezov
57c79d0050 Simplify condition
Knowing the cursor position is enough to decide whether to complete
subcommand or not, and subcommand is already known.
2021-02-28 08:33:17 +01:00
Vladimir Zhelezov
c139bb24a7 Minor refactor of main function
No functional changes except special treatment of `help` subcommand
2021-02-28 08:33:17 +01:00
Vladimir Zhelezov
b195826c0f Revert "Refactor _hledger_compreply_optarg()"
This reverts commit 2fd01d8ef51b897a63a2590556dbb3af7c13ffc9.

Well, I was so wrong...
2021-02-28 08:33:17 +01:00
Vladimir Zhelezov
d30913c6a8 Refactor _hledger_compreply_optarg()
It used to do a lot more work parsing the command line that is
no longer necessary. Clean up redundant code.
2021-02-28 08:33:17 +01:00
Vladimir Zhelezov
9d94f84781 Clarify comment in extension completion function 2021-02-28 08:33:17 +01:00
Vladimir Zhelezov
c729708343 Refactor _hledger_extension_completion_function()
Reduce number of instructions, remove variables used only once
2021-02-28 08:33:17 +01:00
Vladimir Zhelezov
c2da8ac2d4 Fix extension completion
When inserting $extensionName in $COMP_WORDS manually, $COMP_CWORD lags
behind by one. Needs a manual adjustment too.
2021-02-28 08:33:17 +01:00
Vladimir Zhelezov
4101fdf3bb Fix duplicate call of _hledger_compreply_optarg()
It was called in both conditional branches so it's moved out before
the conditional.
2021-02-28 08:33:17 +01:00
Vladimir Zhelezov
9dc65e4ee5 Helper _hledger_debug() now accepts any number of arguments 2021-02-28 08:33:17 +01:00
Vladimir Zhelezov
357a78e7bf Fix --long-opt= completion
Commit #64282f3f broke that somehow so I need to force the match on
an empty string after the equal sign, not the equal sign itself.

I think that _init_completion() does a bit more than I need...
2021-02-28 08:33:16 +01:00
Vladimir Zhelezov
9569cfc8f3 Style: unset compopt filenames if completing command or option
This is about the looks of the completion list -- if we have a
directory with the name of a subcommand it will be presented with
a trailing slash. This avoids that.
2021-02-28 08:33:16 +01:00
Vladimir Zhelezov
dfc8796b25 Style: replace $cur with $subcommand
It changes nothing but spells out intention more clearly
2021-02-28 08:33:16 +01:00
Vladimir Zhelezov
689ad83668 More option argument completions
Added handlers for:
--output-format
--close-acct
--open-acct
--debug

Added --drop to blocking list
2021-02-28 08:33:16 +01:00
Vladimir Zhelezov
fc89340c93 Add optional arguments $prefix and $match to _hledger_compgen()
This allows more flexibility when generating completion candidates
and we don't need to resort to external help from `sed` or others.
_hledger_compgen's arguments become:

$1 -> $wordlist:
   a newline separated wordlist with completion cadidates
$2 -> $prefix:
   (optional) a prefix string to add to generated completions
$3 -> $match:
   (optional) a word to match instead of $cur, the default.

If $match is null and $prefix is defined the match is done against $cur
stripped of $prefix. If both $prefix and $match are null we match against
$cur and no prefix is added to completions. Of course you can also pass
an empty string as $prefix and set $match to whatever you wish.
2021-02-28 08:33:16 +01:00
Vladimir Zhelezov
10cc8b72b9 Use _init_completion()
This handles a lot that we have to do manually otherwise. Without
this we need to handle e.g. redirections to get completion for say:
> hledger payees > <TAB>

Also because this function assumes that we use `cur`, `prev`, `words`
and `cword` and sets them up for us, `wordToComplete`, `COMP_WORDS`
and `COMP_CWORD` are renamed accordingly. Those names are pretty much
hard-coded in bash completion so it is easier to follow the lead than
go with custom variable names.
2021-02-28 08:33:16 +01:00
Vladimir Zhelezov
3706636a76 Fix or silence shellcheck warnings
There are a couple of places where (un)quoting is intentionally
skipped so make those explicit.
2021-02-28 08:33:16 +01:00
Vladimir Zhelezov
31f7a32e36 Query filter completion for amt, real and status; clean whitespace 2021-02-28 08:33:16 +01:00
Vladimir Zhelezov
812a525acf Basic settings for major editors: prevent mixing tabs and spaces 2021-02-28 08:33:16 +01:00
Vladimir Zhelezov
dee25d4811 Fix: pass *all* accounts and rules files to hledger calls
_hledger_optarg() is used to parse the options on the command line
and provide their arguments for context aware completion suggestions
2021-02-28 08:33:16 +01:00
Vladimir Zhelezov
d98ff3b93d Fix quoted/escaped file path handling in _hledger()
When reading hledger (rules-)file from COMP_WORDS we need to unescape it
first. For once compgen is doing exactly what we need :)
2021-02-28 08:33:16 +01:00
Vladimir Zhelezov
72b737a42f Fix #1404, and more...
This was supposed to be just a fix for #1404 but upon visiting the source
several issues became apparent and that is why the commit grew a bit more than
expected. A complete list of changes bellow:

* Fix #1404
No more orphaned temporary directories. Commands, options, etc. that used to be
stored in there are included at build-time as here documents in the source.

* Fix artifacts in =/tmp= after build
Upon fixing the above I became aware that the build itself was leaving behind a
heap of artifacts in =/tmp= that were not taken care of with a ~make clean~.
Fixed by using temporary files and directories in the build directory. Makefile
and build scripts adjusted.

* Produce command aliases
Regular expressions in build scripts changed to produce all command aliases
except single letter ones (see below)

* Do not propose single letters completions
It is simply not useful and adds a lot of noise. It makes completion slower as
well because you need to hit yes on the prompt:
> Display all 200 possibilities? (y or n)
=output-options.sh= now excludes those.

* Query filters simplified
Keep only the prefix of the filter with the colon in =query-filters.txt=. This
change has two reasons:
- Single letter completions are not useful (see above)
- It allows for completion suggestions specific to each
- Bonus reason: it's a completion engine, not a user manual.

* Fix completion impacts on global environment
The completion script was making a couple of changes to the global environment
which had an impact for the rest of the shell session.

~set -o pipefail~: the change is hidden from the user and could lead to subtle
errors throughout the shell session

COMP_WORDBREAKS=" ": this affects subsequent completions for us and other
programs too. I exclude the colon =:= from its value and use
~compopt -o filenames~ to handle escaping of special characters for us. I would
like to find a solution without messing with COMP_WORDBREAKS but it is not
straight forward.

* Fix hiding of legit subcommands
Completion was hiding all possibilities if a subcommand happens to be the prefix
of another. On typing ~balance~, one should be proposed ~balancesheet~ and
~balancesheetequity~ as well.

* Return early
Try to complete depending on the current context and return immediately if
successful. Keep completion list relevant and as short as possible.

* Context aware completion
- Add handlers for option parameter completion, see _hledger_compreply_optarg()
- Add handlers for query filters:, see _hledger_compreply_query()
- Use --file and --rules-file arguments when proposing completions for the
  above, see _hledger()
- Propose only top level accounts at first. Again, keep it short and focused.

* Custom ~compgen~ wrapper
~compgen~ is fairly complicated. There is no way to feed it a word list with
literals. It will mangle your input in so many ways that we cannot trust it. To
work around this several wrappers are used: _hledger_compgen() works with
_hledger_quote_by_ref() to process and escape newline separated input which is
then fed to ~compgen~ and finally in ~COMPREPLY~ through _hledger_compreply()
and _hledger_compreply_append(). It sounds messy and I guess it is, I would like
to find a more straight forward way to do it. I think it is still a way better
and safer interface with ~readline~ than trying to ~grep~ our way through.

* Replace ~declare~ with ~local~
Again, this script is sourced by the shell -- keep variable scopes as narrow as
possible.

* Use ~compopt -o nosort~
Often I resort to using it to keep different groups of completions together.
Whether this is more ergonomic or not is subjective. But our input lists are
already sorted at build-time so why not. Sort manually =query-filters.txt= when
changing it.

* Remove irrelevant comments
And add some new ones :)

I think that is all. Give it a spin, try to abuse it, in and outside of quotes,
with some funky accounts, payees, tags, whatever, and tell me where it breaks or
behaves unexpectedly.
2021-02-28 08:33:16 +01:00
Simon Michael
4c9b72ea15 ;bal: mention --valuechange (#1480) 2021-02-27 18:53:05 -08:00
Simon Michael
f63c38a7e2 bal: docs rewritten, and updated for new flags 2021-02-27 18:51:44 -08:00
Stephen Morgan
7f2536a2a7 lib,cli: Add --valuechange report type for calculating change of value
of accounts, restore --value=end behaviour to that of hledger-1.19.
2021-02-27 18:42:02 -08:00
Stephen Morgan
351648e4fa lib,cli: Add --periodic option to indicate PeriodChange accumulation (renamed from --change). 2021-02-27 18:42:02 -08:00
Dmitry Astapov
18623ec287 doc: explain roi-unrealised.ledger, reference to cookbook 2021-02-25 15:11:35 -08:00
Simon Michael
7bfbcde627 Merge branch pr/Xitian9/1445 (Fix some space leaks, #1445)
- Consume list immediately in commodityStylesFromAmounts.

  This and/or the other strictness fixes below reduce memory and time
  usage, a lot, for journals with many @@ total prices.

- Include sign in TotalPrice in Amount, rather than relying on the sign of aquantity.
  Journal entries still require a positive @@ price, but now the sign is
  set after parsing, rather than when converting in amountToCost.
  The reason for this change is that, if we're going to perform arithmetic
  on Amount with TotalCost, then the presence of aquantity=0 means that
  amountToCost would render the total cost as 0, because signum 0 == 0.

  This makes amount arithmetic more consistent, and allows negative
  prices to be represented. It also means that total prices override
  the primary amount: 0A @@ 1B is now 1B, not 0.

- Make fields of Amount, AmountPrice, AmountStyle, and DigitGroupStyle
  strict.

- Distinguish between an Amount having quantity (or rounded quantity 0),
  and having both quantity and totalprice 0 (or rounded to 0).

- normaliseHelper now uses a strict Map for combining amounts
  internally, closing a big space leak.

  This also now combines Amounts with TotalPrices in the same commodity
  when normalising; amounts with TotalPrices were previously never
  combined.

- (amount|mixedAmount)(Looks|Is)Zero functions now check whether both
  the quantity and the cost are zero. This is usually what you want, but
  if you do only want to check whether the quantity is zero, you can run
  mixedAmountStripPrices (or similar) before this.
2021-02-23 15:22:51 -08:00
Dmitry Astapov
582a1651b3 roi: cmdline help simplified 2021-02-23 15:13:19 -08:00
Stephen Morgan
f0655d1c7f lib: (amount|mixedAmount)(Looks|Is)Zero functions now check whether
both the quantity and the cost are zero. This is usually what you want,
but if you do only want to check whether the quantity is zero, you
can run mixedAmountStripPrices (or similar) before this.

(multiply|divide)(Mixed)?Amount now also multiply or divide the
TotalPrice if it is present, and the old
(multiply|divide)(Mixed)?AmountAndPrice functions are removed.
2021-02-23 14:54:39 -08:00
Stephen Morgan
9d527a9926 lib: normaliseHelper now uses a strict Map for combining amounts
internally, closing a big space leak.

This also now combines Amounts with TotalPrices in the same commodity
when normalising; amounts with TotalPrices were previously never
combined.
2021-02-23 14:54:39 -08:00
Stephen Morgan
ecca7f4e0c lib: Distinguish between an Amount having quantity (or rounded quantity 0), and having both quantity and totalprice 0 (or rounded to 0). 2021-02-23 14:54:39 -08:00
Stephen Morgan
81b778a389 lib: Make fields of Amount, AmountPrice, AmountStyle, and DigitGroupStyle strict. 2021-02-23 14:54:39 -08:00
Stephen Morgan
2ada289e28 lib: Include sign in TotalPrice in Amount, rather than relying on the sign of
aquantity.

Journal entries still require a positive @@ price, but now the sign is
set after parsing, rather than when converting in amountToCost.

The reason for this change is that, if we're going to perform arithmetic
on Amount with TotalCost, then the presence of aquantity=0 means that
amountToCost would render the total cost as 0, because signum 0 == 0.
This makes journal entries like the following impossible to balance:
2000-01-01
    a  0 @@ 10 A
    b  -10 A
2021-02-23 14:54:39 -08:00
Stephen Morgan
a65ef7cd19 lib: Consume list immediately in commodityStylesFromAmounts.
This reduced the maximum heap size per thread from ~850K to ~430K in a
real-world register test.
2021-02-23 14:54:39 -08:00
Dmitry Astapov
150cf3f862 roi: roi-unrealised.example now works based on price directives 2021-02-23 14:11:29 -08:00
Dmitry Astapov
0fa51482c6 roi: check valuation on dates we have price directives for 2021-02-23 14:11:29 -08:00
Dmitry Astapov
8013d650f3 roi: honor all kinds of --value switches 2021-02-23 14:11:29 -08:00
Simon Michael
f4d81377af print --match: show --match's argument in debug output
If you forget that the argument is required by the --match
option, and not the command as a whole - eg if instead of

  hledger print --match somebank -x

you write:

  hledger print --match -x somebank

that gets quietly parsed as:

  hledger print --match="-x"

which is not great. Adding --debug now at least gives some insight:

  hledger print --match -x somebank --debug
  finding best match for description: "-x"
  similar transactions:
  ...
2021-02-20 14:29:40 -08:00
Simon Michael
5e7c4fc7bc add, print --match: prioritise infix matches
If the search description occurs in full within the other description,
that match gets a +0.5 score boost.
2021-02-20 14:08:47 -08:00
Simon Michael
44508f58c8 add, lib: debug output, refactor similar transactions lookup
add --debug=1 shows the top hits for similar past transactions.

added:
Hledger.Cli.Utils.journalSimilarTransaction
 provides --debug=1 output

changed:
Hledger.Cli.Commands.Add.transactionsSimilarTo -> Hledger.Data.Journal.journalTransactionsSimilarTo
 now takes an extra number-of-results argument
2021-02-20 13:41:37 -08:00
Simon Michael
7979c7d74c ;import: doc: tweak 2021-02-18 18:43:22 -08:00
Simon Michael
125ba49992 ;import: doc: tweak 2021-02-18 18:38:51 -08:00
Simon Michael
554f7a59fd ;import, print: better deduplication docs 2021-02-18 18:35:06 -08:00
Simon Michael
f7bbb39a77 ;regenerate cabal files with the hpack version in current stack
To minimise warnings. They got regenerated by a commit for #1471.
2021-02-18 13:42:40 -08:00
Simon Michael
286efc7b12 ;shake: use --markdown-headings to silence warning (& require pandoc-2.11.2+) 2021-02-17 12:11:15 -08:00
Simon Michael
b92a842728 ;update manuals 2021-02-17 12:07:23 -08:00
Simon Michael
f4c8b52885 ;update CLI usage texts 2021-02-17 12:07:06 -08:00
Arnout Engelen
ad2ab3c823 Make sure to round up for the 'to' date 2021-02-17 09:05:13 -08:00
Arnout Engelen
fe6e9a79fe Allow selecting the date range from the chart
By dragging a region with the mouse
2021-02-17 09:05:13 -08:00