Commit Graph

24 Commits

Author SHA1 Message Date
Vladimir Zhelezov
3d3cd2d4a2 Makefile: move all variable definitions to the top, before targets 2021-02-28 08:33:18 +01:00
Vladimir Zhelezov
419817e656 Prevent unwanted m4 macro expansion + -g GNU compatibility flag
Most of the included files are meant to be output literally, without any
macro processing. Using `undivert` in place of `include` achieves that.
This is a safety net against unsanitized input generated during the
build. Also, developers editing the shell code stub shouldn't be
constantly alert about triggering accidental macro expansion.

In order that `undivert` mimics GNU behaviour on BSDs, the `-g` flag is
used for the m4 invocation.
2021-02-28 08:33:18 +01:00
Vladimir Zhelezov
0ca63ddc84 Install/uninstall completion for all extensions by default
Install the symlinks unconditionally. This way the user don't need
to reinstall completion after adding an extension. Of course fine-
grained control is possible with: `make install EXTENSIONS=web` e.g.
2021-02-28 08:33:18 +01:00
Vladimir Zhelezov
62c3c8e6da Exit build with an error if unable to parse hledger sub-commands 2021-02-28 08:33:18 +01:00
Vladimir Zhelezov
dea35043bd Add phony clean-all build target
A `make clean` before commit removes hledger-completion.bash and
it is supposed to be in the repository. `make clean` removes build
artifacts while keeping the latter. Do a `make clean-all` to purge
everything.
2021-02-28 08:33:18 +01:00
Vladimir Zhelezov
0bb4d1a6eb Remove api from hledger extensions
Superseded by hledger-web
2021-02-28 08:33:18 +01:00
Vladimir Zhelezov
5c2dd6fa2f Move back commands/options parsing to separate shell scripts
It made the Makefile more difficult to read and this way we can
take advantage of `set -e`, `-o pipefail` and friends.

Clean up debugging targets
2021-02-28 08:33:18 +01:00
Vladimir Zhelezov
effb1be0e6 Avoid hard-coded reference to commands.tmp 2021-02-28 08:33:18 +01:00
Vladimir Zhelezov
b3f0b437a5 Add example usage comment for debug targets 2021-02-28 08:33:18 +01:00
Vladimir Zhelezov
b97c2043b8 Add install and uninstall build targets 2021-02-28 08:33:18 +01:00
Vladimir Zhelezov
1b6f968a6a Move commands and options parsing into the Makefile
It is just a pipe of sed regex filters and all can be viewed/edited
in one place. Add a couple of debug targets to see easily the
effects of regex changes.
2021-02-28 08:33:18 +01:00
Vladimir Zhelezov
0498b8ff7c Add stub file to m4 build target prerequisites
And as it becomes unwieldy, put all the dependencies in a variable
2021-02-28 08:33:18 +01:00
Caleb Maclennan
4a32ebf8b5 Eliminate dependency on ‘paste’ and ‘parallel’ 2021-02-28 08:33:18 +01:00
Vladimir Zhelezov
bc66b23520 Fix build race condition
Make `command-options` a prerequisite of `hledger-completion.bash`.
Currently the build succeeds only because the former takes less time
to finish than all the prerequisites of the latter. If you run a
`make clean && make -j 4`, the build would fail as they are built
in parallel.
2021-02-28 08:33:18 +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
Jakob Schöttl
1a88ee6265 Add some external commands to completion 2019-02-16 06:52:21 -08:00
Jakob Schöttl
550f47b3fa Some fixes in hledger help output parsing 2019-02-16 06:52:21 -08:00
Jakob Schöttl
903fe734e3 Add doc 2019-01-23 16:46:17 -08:00
Jakob Schöttl
3f407a326d Add completions for query filters 2019-01-23 16:46:17 -08:00
Jakob Schöttl
ae060d9f13 Refactor Makefile
hledger-completion.bash does not depend on command-options because this
phony target is so slow. Use  make  once and subsequently only
make hledger-completion.bash
2019-01-23 16:46:17 -08:00
Jakob Schöttl
db00f5b452 Speed up Makefile script a little bit 2019-01-23 16:46:17 -08:00
Jakob Schöttl
78d08455c0 Refactor helper script 2019-01-23 16:46:17 -08:00
Jakob Schöttl
e5134db9a4 Remove unused code 2019-01-23 16:46:17 -08:00
Jakob Schöttl
dc73b55b7b Add files for bash completion 2019-01-23 16:46:17 -08:00