Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #1404, and more... #1410

Merged
merged 82 commits into from
Mar 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
82 commits
Select commit Hold shift + click to select a range
72b737a
Fix #1404, and more...
zhelezov Dec 6, 2020
d98ff3b
Fix quoted/escaped file path handling in _hledger()
zhelezov Dec 7, 2020
dee25d4
Fix: pass *all* accounts and rules files to hledger calls
zhelezov Dec 8, 2020
812a525
Basic settings for major editors: prevent mixing tabs and spaces
zhelezov Dec 8, 2020
31f7a32
Query filter completion for amt, real and status; clean whitespace
zhelezov Dec 8, 2020
3706636
Fix or silence shellcheck warnings
zhelezov Dec 8, 2020
10cc8b7
Use _init_completion()
zhelezov Dec 9, 2020
fc89340
Add optional arguments $prefix and $match to _hledger_compgen()
zhelezov Dec 9, 2020
689ad83
More option argument completions
zhelezov Dec 9, 2020
dfc8796
Style: replace $cur with $subcommand
zhelezov Dec 10, 2020
9569cfc
Style: unset compopt filenames if completing command or option
zhelezov Dec 10, 2020
357a78e
Fix --long-opt= completion
zhelezov Dec 10, 2020
9dc65e4
Helper _hledger_debug() now accepts any number of arguments
zhelezov Dec 10, 2020
4101fdf
Fix duplicate call of _hledger_compreply_optarg()
zhelezov Dec 10, 2020
c2da8ac
Fix extension completion
zhelezov Dec 10, 2020
c729708
Refactor _hledger_extension_completion_function()
zhelezov Dec 11, 2020
9d94f84
Clarify comment in extension completion function
zhelezov Dec 11, 2020
d30913c
Refactor _hledger_compreply_optarg()
zhelezov Dec 11, 2020
b195826
Revert "Refactor _hledger_compreply_optarg()"
zhelezov Dec 11, 2020
c139bb2
Minor refactor of main function
zhelezov Dec 11, 2020
57c79d0
Simplify condition
zhelezov Dec 11, 2020
985f041
Make hard-coded completion word lists easy to find
zhelezov Dec 11, 2020
8c14dfb
A non-recursive version of _hledger_compreply_optarg()
zhelezov Dec 11, 2020
d943fa6
Make sure `=` is present in COMP_WORDBREAKS
zhelezov Dec 11, 2020
e2cac96
Typo in comments
zhelezov Dec 11, 2020
ddf55a8
Rename $wordlist to $complist in _hledger_compgen()
zhelezov Dec 11, 2020
e6d54f7
Postpone options display until after entering a dash
zhelezov Dec 12, 2020
09132ac
f656ff8 made another early return possible, so do it
zhelezov Dec 12, 2020
8feb905
Append `=` to long options requiring an argument
zhelezov Dec 12, 2020
40a7549
Adjust comment obsoleted by previous commit: 4d2a4b0
zhelezov Dec 12, 2020
a5ccae8
Fix regular expression in output-options.sh
zhelezov Dec 12, 2020
5a88a14
Improve option extraction
zhelezov Dec 12, 2020
3a20e91
Further refinement of option extraction regex
zhelezov Dec 12, 2020
0361c81
Add a couple of sub-commands to the no-query list
zhelezov Dec 13, 2020
e2b2c2f
_hledger_compreply_query: minor refactor
zhelezov Dec 13, 2020
f5d2a6b
Move sub-command options reply out of the for-loop
zhelezov Dec 13, 2020
046421e
Simplify sub-command loop logic
zhelezov Dec 13, 2020
b6a4a88
White space
zhelezov Dec 13, 2020
eea7f0d
Add an early return from option argument completion
zhelezov Dec 13, 2020
3cabee2
Remove unnecessary quoting, a matter of consistency...
zhelezov Dec 13, 2020
83cfd10
Remove _hledger_quote(), unused.
zhelezov Dec 13, 2020
7504509
Break overly long lines for better readability
zhelezov Dec 13, 2020
05d4834
Remove redundant call of _get_comp_words_by_ref()
zhelezov Dec 13, 2020
1bbf04d
Edit comments
zhelezov Dec 13, 2020
78ce651
White space, fix alignment
zhelezov Dec 13, 2020
797301c
White space
zhelezov Dec 13, 2020
34dbe3b
Use compopt -o nospace only for a query prefix
zhelezov Dec 14, 2020
0f8c01f
Delay setting `compopt -o filenames`
zhelezov Dec 14, 2020
3eccfb8
Remove `compopt -o nospace` from --{close,open}-acct handler
zhelezov Dec 14, 2020
d82e13b
Add an automatic check for required option argument
zhelezov Dec 14, 2020
910e86b
Fix a7dc62d: set $subcommandOptions unconditionally
zhelezov Dec 14, 2020
fb3577d
return 0
zhelezov Dec 14, 2020
fdac46d
Fix _hledger_extension_completion when called by path...
zhelezov Dec 15, 2020
99af527
Remove _function from function names
zhelezov Dec 15, 2020
bee6667
Add a comment with example installation instructions
zhelezov Dec 15, 2020
13bf6fd
Amend installation comment
zhelezov Dec 15, 2020
bc66b23
Fix build race condition
zhelezov Dec 16, 2020
bb8118c
Isolate shell code in a stub file included by m4
zhelezov Dec 17, 2020
4a32ebf
Eliminate dependency on ‘paste’ and ‘parallel’
alerque Dec 16, 2020
0498b8f
Add stub file to m4 build target prerequisites
zhelezov Dec 18, 2020
1b6f968
Move commands and options parsing into the Makefile
zhelezov Dec 18, 2020
b97c204
Add install and uninstall build targets
zhelezov Dec 18, 2020
84a7a66
Modify .gitignore
zhelezov Dec 18, 2020
b3f0b43
Add example usage comment for debug targets
zhelezov Dec 18, 2020
effb1be
Avoid hard-coded reference to `commands.tmp`
zhelezov Dec 18, 2020
5c2dd6f
Move back commands/options parsing to separate shell scripts
zhelezov Dec 18, 2020
9e90d25
Add basic editor configuration to shell scripts
zhelezov Dec 18, 2020
b53e264
Update .gitignore
zhelezov Dec 18, 2020
0bb4d1a
Remove `api` from hledger extensions
zhelezov Dec 19, 2020
dea3504
Add phony clean-all build target
zhelezov Dec 19, 2020
ac143af
Do not use `set -e` in helper shell scripts
zhelezov Dec 19, 2020
62c3c8e
Exit build with an error if unable to parse hledger sub-commands
zhelezov Dec 19, 2020
0ca63dd
Install/uninstall completion for all extensions by default
zhelezov Dec 19, 2020
2a37479
Revert "Do not use `set -e` in helper shell scripts"
zhelezov Dec 19, 2020
2e44fe9
Portability: replace GNU extension `\s` with `[[:space:]]`
zhelezov Dec 19, 2020
419817e
Prevent unwanted m4 macro expansion + `-g` GNU compatibility flag
zhelezov Dec 20, 2020
3c4025a
BSDmakefile
zhelezov Dec 20, 2020
3ab796f
Update README
zhelezov Dec 20, 2020
36fca65
README: fix syntax highlighting, quotes
zhelezov Dec 20, 2020
3d3cd2d
Makefile: move all variable definitions to the top, before targets
zhelezov Dec 20, 2020
066fa96
Disable shell expansion in here-docs
zhelezov Dec 21, 2020
deb2a27
Rebuild completion after the rebase on upstream/master
zhelezov Feb 28, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion shell-completion/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
*.txt
options*.txt
generic-options.txt
commands*.txt
4 changes: 4 additions & 0 deletions shell-completion/BSDmakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.DONE:
@echo "GNU Make (gmake) required to build"
.DEFAULT:
@echo "GNU Make (gmake) required to build"
91 changes: 74 additions & 17 deletions shell-completion/Makefile
Original file line number Diff line number Diff line change
@@ -1,30 +1,87 @@
# Setting the number of job runners like this in the makefile only works in
# GNU Make 4.3 or later. Older versions will require that either an env
# variable be set before running or command line flag be passed at runtime to
# get parallel jobs.
MAKEFLAGS += --jobs=$(shell nproc 2>/dev/null || printf 8)

.PHONY: command-options clean
EUID := $(shell id -u)

all: command-options hledger-completion.bash
ifeq ($(EUID),0)
PREFIX := /usr/local
endif

hledger-completion.bash: hledger-completion.bash.m4 commands-list.txt query-filters.txt generic-options.txt
m4 hledger-completion.bash.m4 > $@
ifdef PREFIX
BASHCOMPDIR := $(PREFIX)/share/bash-completion/completions
else
XDG_DATA_HOME ?= $(HOME)/.local/share
BASH_COMPLETION_USER_DIR ?= $(XDG_DATA_HOME)/bash-completion
BASHCOMPDIR := $(BASH_COMPLETION_USER_DIR)/completions
endif

generic-options.txt:
hledger -h | ./output-options.sh | sort -u > $@
DESTDIR ?=

PARSE_COMMANDS := ./parse-commands.sh
PARSE_OPTIONS := ./parse-options.sh

EXTENSIONS := ui web
INSTALLED_EXTENSIONS := $(foreach EXT,$(EXTENSIONS),$(shell type hledger-$(EXT) >/dev/null 2>&1 && echo $(EXT)))

COMMANDS := $(sort $(shell $(PARSE_COMMANDS)) $(INSTALLED_EXTENSIONS))

ifneq ($(.SHELLSTATUS),0)
$(error Error running $(PARSE_COMMANDS))
endif

CMDOPTFILES := $(foreach CMD,$(COMMANDS),options-$(CMD).txt)

define M4DEPS :=
hledger-completion.bash.m4 \
hledger-completion.bash.stub \
commands.txt \
commands-list.txt \
query-filters.txt \
generic-options.txt \
$(CMDOPTFILES)
endef


all: hledger-completion.bash

.PHONY: install
install:
@install -v -d "$(DESTDIR)$(BASHCOMPDIR)"
zhelezov marked this conversation as resolved.
Show resolved Hide resolved
@install -v -m 0644 hledger-completion.bash "$(DESTDIR)$(BASHCOMPDIR)/hledger"
@for ext in $(EXTENSIONS); do \
printf "symlink " ; \
ln -sfv hledger "$(DESTDIR)$(BASHCOMPDIR)/hledger-$$ext" ; \
done

.PHONY: uninstall
uninstall:
@rm -vf "$(DESTDIR)$(BASHCOMPDIR)/hledger"
@for ext in $(EXTENSIONS); do \
rm -vf "$(DESTDIR)$(BASHCOMPDIR)/hledger-$$ext" ; \
done

hledger-completion.bash: $(M4DEPS)
m4 -g hledger-completion.bash.m4 > $@

commands.txt:
hledger | ./output-commands.sh | grep -v ^hledger > $@
echo ui >> $@
echo web >> $@
echo api >> $@
printf "%s\n" $(COMMANDS) > $@

commands-list.txt: commands.txt
paste -sd, $^ | tr -d '\n' > $@
commands-list.txt:
printf "%s," $(COMMANDS) | sed 's/,$$//' > $@

#query-filters.txt:
# The query filters are hard to extract!
# hledger help --cat hledger | sed -n '/^QUERIES/,/^[A-Z]/p'
generic-options.txt:
$(PARSE_OPTIONS) > $@

command-options: commands.txt
parallel -j8 'hledger {} -h | ./output-options.sh | sort -u > options-{}.txt' < commands.txt
options-%.txt:
$(PARSE_OPTIONS) $* > $@

.PHONY: clean
clean:
rm -f commands*.txt generic-options.txt options-*.txt

.PHONY: clean-all
clean-all: clean
rm -f hledger-completion.bash
57 changes: 41 additions & 16 deletions shell-completion/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ The completions can handle hledger's CLI:

- commands and generic options
- command-specific options
- filenames for options that take a filename as argument
- account names from journal files (but not yet for files named by `--file`)
- most option arguments
- account names, tags, payees, etc. from journal files
- query filter keywords like `status:`, `tag:`, or `amt:`

Installation for end users
Expand All @@ -27,22 +27,45 @@ Completions are currently only implemented for the Bash shell.
Please check first if the completions for hledger are already installed on your
distribution. Refer to the last paragraph of this section for how to test that.

To install the completions manually, follow this steps:
To install from the repository, do:

- Download or copy the file `shell-completion/hledger-completion.bash` and save
it as `~/.hledger-completion.bash`.
```sh
cd /path-to-repo/shell-completion
make install
```

- Add the command `source ~/.hledger-completion.bash` this to the end of your
`~/.bashrc` file.
Completions installed this way will be loaded dynamically after you use the hledger
command. Upon the first invocation of a command that has no predefined completion
bash looks for a file with the same name in a set of predefined locations in this order:

- Then, you have to start a new Bash, e.g. by typing `bash` on the current
shell.
- `$BASH_COMPLETION_USER_DIR/completions`
- `$XDG_DATA_HOME/bash-completion/completions`
- `$HOME/.local/share/bash-completion/completions`
- etc.

Example installation script:
You can manually achieve the effects of `make install` by copying
`shell-completion/hledger-completion.bash` to one of the above, and renaming it
to `hledger`, `_hledger` or `hledger.bash`. For the gory details, type this in a
bash shell:

```sh
type __load_completion
```
cp hledger-completion.bash ~/.hledger-completion.bash
echo 'source ~/.hledger-completion.bash' >> ~/.bashrc

To install the completions manually, you can also just download and copy
`shell-completion/hledger-completion.bash` to a directory of your choosing, and
source it from your shell start up files. This way completions are loaded
eagerly and that adds a delay to shell start up time.

Example:

```sh
cp hledger-completion.bash ~/.bash_completion.d/hledger
echo 'source ~/.bash_completion.d/hledger' >> ~/.bashrc
# Restart shell
exec bash
# Confirm that completion is loaded
complete -p hledger
```

Now, try it by typing `hledger` (with a space after the command) and press the
Expand All @@ -63,21 +86,23 @@ Information for developers

Generate the completion script for Bash:

```
```sh
# change into this folder:
cd shell-completion/
make
```

Hint: GNU make, GNU m4, and GNU parallel must be installed to call `make`.
The first two usually are.
Hint: GNU make and GNU m4 must be installed to call `make`.
These are present on most systems anyway.
Additionally the build will run a lot faster with parallell jobs.
Use `make -j$(nproc)` for best effect.

The generated completion script must be installed. The package maintainer for
your distribution should be responsible for this.

For now, or to live-test the script, you can use these two commands:

```
```sh
ln -s hledger-completion.bash ~/.hledger-completion.bash
echo 'source ~/.hledger-completion.bash' >> ~/.bashrc
```
Expand Down
Loading