Commit Graph

68 Commits

Author SHA1 Message Date
Vladimir Zhelezov
066fa964b6 Disable shell expansion in here-docs
Better be safe that sorry...
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
bb8118c771 Isolate shell code in a stub file included by m4
This way we can easily edit m4 in m4-mode and the shell script stub
in sh-mode and prevent subtle errors coming from accidental quoting
issues or macros (mis)interpreted by m4.
2021-02-28 08:33:18 +01:00
Vladimir Zhelezov
13bf6fdcd8 Amend installation comment 2021-02-28 08:33:18 +01:00
Vladimir Zhelezov
bee666704b Add a comment with example installation instructions 2021-02-28 08:33:18 +01:00
Vladimir Zhelezov
99af527f16 Remove _function from function names 2021-02-28 08:33:18 +01:00
Vladimir Zhelezov
fdac46d68a Fix _hledger_extension_completion when called by path...
Could break if called with ./funky-path/with-dashes/hledger-ui
2021-02-28 08:33:18 +01:00
Vladimir Zhelezov
fb3577d3e0 return 0
Shame on me...
2021-02-28 08:33:18 +01:00
Vladimir Zhelezov
910e86b4b2 Fix a7dc62d: set $subcommandOptions unconditionally 2021-02-28 08:33:18 +01:00
Vladimir Zhelezov
d82e13bb51 Add an automatic check for required option argument
Get rid of manually listing unhandled long options:

Instead of listing options requiring an argument one by one in the
case statement in _hledger_compreply_optarg(), the option completion
lists are searched and if the option does require an argument an
empty COMPREPLY is send. Short options are uncounted for and still
need a manual entry.

This necessitated setting $subcommandOptions beforehand, so it is
moved back where it was -- in the sub-command loop in main().
2021-02-28 08:33:18 +01:00
Vladimir Zhelezov
3eccfb85e0 Remove compopt -o nospace from --{close,open}-acct handler 2021-02-28 08:33:18 +01:00
Vladimir Zhelezov
0f8c01f033 Delay setting compopt -o filenames
...until just before query completion. It is not desired for
commands and options completion and is better used selectively
only when really needed.
2021-02-28 08:33:18 +01:00
Vladimir Zhelezov
34dbe3b3a8 Use compopt -o nospace only for a query prefix
Similarly to long options treatment, suspend space only when
completing what looks like a query or account prefix i.e. something
ending with a colon (:).
2021-02-28 08:33:18 +01:00
Vladimir Zhelezov
797301c3dc White space 2021-02-28 08:33:18 +01:00
Vladimir Zhelezov
78ce651531 White space, fix alignment 2021-02-28 08:33:18 +01:00
Vladimir Zhelezov
1bbf04d310 Edit comments 2021-02-28 08:33:18 +01:00
Vladimir Zhelezov
05d483494b Remove redundant call of _get_comp_words_by_ref() 2021-02-28 08:33:18 +01:00
Vladimir Zhelezov
750450915d Break overly long lines for better readability
Hopefully differences are aligned and easily discernible this way
2021-02-28 08:33:18 +01:00
Vladimir Zhelezov
83cfd104f9 Remove _hledger_quote(), unused. 2021-02-28 08:33:18 +01:00
Vladimir Zhelezov
3cabee2470 Remove unnecessary quoting, a matter of consistency... 2021-02-28 08:33:18 +01:00
Vladimir Zhelezov
eea7f0d506 Add an early return from option argument completion
Do not even enter the case statement if preceding words don't look
like an option
2021-02-28 08:33:18 +01:00
Vladimir Zhelezov
b6a4a887b9 White space 2021-02-28 08:33:18 +01:00
Vladimir Zhelezov
046421e712 Simplify sub-command loop logic
If the cursor is on the sub-command, just offer sub-command
completions and be done with it. It was way over complicated.
2021-02-28 08:33:18 +01:00
Vladimir Zhelezov
f5d2a6bcc3 Move sub-command options reply out of the for-loop
It was just before the `break` statement anyways
2021-02-28 08:33:18 +01:00
Vladimir Zhelezov
e2b2c2fb2e _hledger_compreply_query: minor refactor 2021-02-28 08:33:18 +01:00
Vladimir Zhelezov
0361c81da2 Add a couple of sub-commands to the no-query list 2021-02-28 08:33:18 +01:00
Vladimir Zhelezov
8feb9053ec Append = to long options requiring an argument
Make it obvious that the option expects an argument by appending
the equal sign on completion. Suspend space in this case. The
regular expression in `output-options.sh` is adjusted to take into
account this on processing option strings.
2021-02-28 08:33:18 +01:00
Vladimir Zhelezov
09132ace80 f656ff8 made another early return possible, so do it
Also it made a couple of statements redundant, cleaned up.
2021-02-28 08:33:18 +01:00
Vladimir Zhelezov
e6d54f79d7 Postpone options display until after entering a dash
I was looking at how other programs that have an overwhelming
number of sub-commands and options deal with completion, namely
how git does it, and I liked the clean workflow not spitting every
available option until asked for. Hledger's main workflow is:

> hledger COMMAND QUERY

so I have tried to reproduce this with this change. Options are of
course still there, but not shown until you ask for them by entering
a dash on the command line. Also, the `acct:` filter proposes only top
level accounts until there is some input from the user because accounts
tend to be numerous as well.
2021-02-28 08:33:17 +01:00
Vladimir Zhelezov
ddf55a86a4 Rename $wordlist to $complist in _hledger_compgen()
Keep to established conventions
2021-02-28 08:33:17 +01:00
Vladimir Zhelezov
e2cac96449 Typo in comments 2021-02-28 08:33:17 +01:00
Vladimir Zhelezov
d943fa613e Make sure = is present in COMP_WORDBREAKS
Currently option processing logic is based on the assumption that `=` is
a word-breaking character, so make sure it is present in COMP_WORDBREAKS
2021-02-28 08:33:17 +01:00
Vladimir Zhelezov
8c14dfb3eb A non-recursive version of _hledger_compreply_optarg()
Feels more streamlined and readable
2021-02-28 08:33:17 +01:00
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