# Completion script for hledger. # Created using a Makefile and real hledger. # No set -e because this file is sourced and is not supposed to quit the current shell. set -o pipefail # Note: grep "^$wordToComplete" is (functional) not safe to use if the word # contains regex special chars. But it might be no problem because of # COMP_WORDBREAKS. # Note: compgen and compopt is pretty complicated. Piping to # grep "^$wordToComplete" # seems like a hack - I'd rather use # compgen ... -- "$wordToComplete" # But what options to use? I don't want to use -W because it may exceed the # maximum command line length. -C "cat file" is not working either. It would be # best if compgen could read from stdin but it does not. # Note: Working with bash arrays is nasty compared to editing a text file. # Consider for example grepping an array or mapping a substitution on it. # Therefore, we create temp files in RAM for completion suggestions (see below). readonly _HLEDGER_COMPLETION_TEMPDIR=$(mktemp -d) _hledger_completion_function() { #declare cmd=$1 declare wordToComplete=$2 declare precedingWord=$3 declare subcommand for subcommand in "${COMP_WORDS[@]}"; do if grep -Fxqe "$subcommand" "$_HLEDGER_COMPLETION_TEMPDIR/commands.txt"; then COMPREPLY+=( $(grep -h "^$wordToComplete" -- "$_HLEDGER_COMPLETION_TEMPDIR/options-$subcommand.txt") ) break fi subcommand= done if [[ -z $subcommand ]]; then declare completeFiles filenameSoFar case $precedingWord in -f|--file|--rules-file) completeFiles=1 filenameSoFar=$wordToComplete ;; =) completeFiles=1 filenameSoFar=$wordToComplete ;; esac if [[ -n $completeFiles ]]; then #COMP_WORDBREAKS='= ' declare -a files # This does not work because assignment to 'files' in the "pipe # subshell" has no effect! #compgen -df | grep "^$filenameSoFar" | readarray -t files compopt -o filenames -o dirnames readarray -t files < <(compgen -f -- "$filenameSoFar") COMPREPLY=( "${files[@]}" ) else COMPREPLY+=( $(grep -h "^$wordToComplete" -- "$_HLEDGER_COMPLETION_TEMPDIR/commands.txt" "$_HLEDGER_COMPLETION_TEMPDIR/generic-options.txt") ) fi else # Almost all subcommands accept [QUERY] # -> always add accounts to completion list # TODO Get ledger file from -f --file arguments from COMP_WORDS and pass it to # the 'hledger accounts' call. Note that --rules-file - if present - must also # be passed! declare -a accounts readarray -t accounts < <({ cat "$_HLEDGER_COMPLETION_TEMPDIR/query-filters.txt"; hledger accounts --flat; } | grep "^$wordToComplete") compopt -o nospace COMPREPLY+=( "${accounts[@]}" ) # Special characters (e.g. '-', ':') are allowed in account names. # Account names with spaces must be still be quoted (e.g. '"Expens') # for completion. Setting COMP_WORDBREAKS='' would not help here! COMP_WORDBREAKS=' ' fi } # Register completion function for hledger: complete -F _hledger_completion_function hledger # Include lists of commands and options generated by the Makefile using the # m4 macro processor. # Included files must have exactly one newline at EOF to prevent weired errors. cat < "$_HLEDGER_COMPLETION_TEMPDIR/commands.txt" add import balancesheet balancesheetequity cashflow incomestatement accounts activity balance prices print register stats tags ui web close rewrite api check-dates check-dupes print-unique register-match roi test outlet help bs bse cf is TEXT cat < "$_HLEDGER_COMPLETION_TEMPDIR/query-filters.txt" not: acct: amt: amt:< amt:<= amt:> amt:>= code: cur: desc: date: date2: depth: note: payee: real: real:0 status: status:! status:* tag: inacct: TEXT cat < "$_HLEDGER_COMPLETION_TEMPDIR/generic-options.txt" -f -I -b -e -D -W -M -Q -Y -p -U -C -P -C -R -N -E -B -V -h -h -h --file --rules-file --separator --alias --anon --pivot --ignore-assertions --begin --end --daily --weekly --monthly --quarterly --yearly --period --date2 --unmarked --pending --cleared --real --depth --empty --cost --value --auto --forecast --help --debug --version TEXT cat < "$_HLEDGER_COMPLETION_TEMPDIR/options-add.txt" -f -I -h --no-new-accounts --file --rules-file --separator --alias --anon --pivot --ignore-assertions --help --debug --version TEXT cat < "$_HLEDGER_COMPLETION_TEMPDIR/options-import.txt" -f -I -b -e -D -W -M -Q -Y -p -U -C -P -C -R -N -E -B -V -h --dry-run --new --dry-run --file --rules-file --separator --alias --anon --pivot --ignore-assertions --begin --end --daily --weekly --monthly --quarterly --yearly --period --date2 --unmarked --pending --cleared --real --depth --empty --cost --value --auto --forecast --help --debug --version TEXT cat < "$_HLEDGER_COMPLETION_TEMPDIR/options-balancesheet.txt" -H -N -A -T -S -O -o -f -I -b -e -D -W -M -Q -Y -p -U -C -P -C -R -N -E -B -V -h --change --cumulative --historical --flat --drop --no-total --tree --average --row-total --no-elide --format --pretty-tables --sort-amount --output-format --output-file --file --rules-file --separator --alias --anon --pivot --ignore-assertions --begin --end --daily --weekly --monthly --quarterly --yearly --period --date2 --unmarked --pending --cleared --real --depth --empty --cost --value --auto --forecast --help --debug --version TEXT cat < "$_HLEDGER_COMPLETION_TEMPDIR/options-balancesheetequity.txt" -H -N -A -T -S -O -o -f -I -b -e -D -W -M -Q -Y -p -U -C -P -C -R -N -E -B -V -h --change --cumulative --historical --flat --drop --no-total --tree --average --row-total --no-elide --format --pretty-tables --sort-amount --output-format --output-file --file --rules-file --separator --alias --anon --pivot --ignore-assertions --begin --end --daily --weekly --monthly --quarterly --yearly --period --date2 --unmarked --pending --cleared --real --depth --empty --cost --value --auto --forecast --help --debug --version TEXT cat < "$_HLEDGER_COMPLETION_TEMPDIR/options-cashflow.txt" -H -N -A -T -S -O -o -f -I -b -e -D -W -M -Q -Y -p -U -C -P -C -R -N -E -B -V -h --change --cumulative --historical --flat --drop --no-total --tree --average --row-total --no-elide --format --pretty-tables --sort-amount --output-format --output-file --file --rules-file --separator --alias --anon --pivot --ignore-assertions --begin --end --daily --weekly --monthly --quarterly --yearly --period --date2 --unmarked --pending --cleared --real --depth --empty --cost --value --auto --forecast --help --debug --version TEXT cat < "$_HLEDGER_COMPLETION_TEMPDIR/options-incomestatement.txt" -H -N -A -T -S -O -o -f -I -b -e -D -W -M -Q -Y -p -U -C -P -C -R -N -E -B -V -h --change --cumulative --historical --flat --drop --no-total --tree --average --row-total --no-elide --format --pretty-tables --sort-amount --output-format --output-file --file --rules-file --separator --alias --anon --pivot --ignore-assertions --begin --end --daily --weekly --monthly --quarterly --yearly --period --date2 --unmarked --pending --cleared --real --depth --empty --cost --value --auto --forecast --help --debug --version TEXT cat < "$_HLEDGER_COMPLETION_TEMPDIR/options-accounts.txt" -f -I -b -e -D -W -M -Q -Y -p -U -C -P -C -R -N -E -B -V -h --declared --used --tree --flat --drop --file --rules-file --separator --alias --anon --pivot --ignore-assertions --begin --end --daily --weekly --monthly --quarterly --yearly --period --date2 --unmarked --pending --cleared --real --depth --empty --cost --value --auto --forecast --help --debug --version TEXT cat < "$_HLEDGER_COMPLETION_TEMPDIR/options-activity.txt" -f -I -b -e -D -W -M -Q -Y -p -U -C -P -C -R -N -E -B -V -h --file --rules-file --separator --alias --anon --pivot --ignore-assertions --begin --end --daily --weekly --monthly --quarterly --yearly --period --date2 --unmarked --pending --cleared --real --depth --empty --cost --value --auto --forecast --help --debug --version TEXT cat < "$_HLEDGER_COMPLETION_TEMPDIR/options-balance.txt" -H -A -T -N -S -O -o -f -I -b -e -D -W -M -Q -Y -p -U -C -P -C -R -N -E -B -V -h --change --cumulative --historical --tree --flat --average --row-total --no-total --drop --no-elide --format --pretty-tables --sort-amount --budget --budget --invert --output-format --output-file --file --rules-file --separator --alias --anon --pivot --ignore-assertions --begin --end --daily --weekly --monthly --quarterly --yearly --period --date2 --unmarked --pending --cleared --real --depth --empty --cost --value --auto --forecast --help --debug --version TEXT cat < "$_HLEDGER_COMPLETION_TEMPDIR/options-prices.txt" -f -I -b -e -D -W -M -Q -Y -p -U -C -P -C -R -N -E -B -V -h --costs --inverted-costs --costs --inverted-costs --file --rules-file --separator --alias --anon --pivot --ignore-assertions --begin --end --daily --weekly --monthly --quarterly --yearly --period --date2 --unmarked --pending --cleared --real --depth --empty --cost --value --auto --forecast --help --debug --version TEXT cat < "$_HLEDGER_COMPLETION_TEMPDIR/options-print.txt" -m -x -O -o -f -I -b -e -D -W -M -Q -Y -p -U -C -P -C -R -N -E -B -V -h --date2 --match --explicit --new --output-format --output-file --file --rules-file --separator --alias --anon --pivot --ignore-assertions --begin --end --daily --weekly --monthly --quarterly --yearly --period --date2 --unmarked --pending --cleared --real --depth --empty --cost --value --auto --forecast --help --debug --version TEXT cat < "$_HLEDGER_COMPLETION_TEMPDIR/options-register.txt" -H -A -r -w -w -O -o -f -I -b -e -D -W -M -Q -Y -p -U -C -P -C -R -N -E -B -V -h --date2 --cumulative --historical --average --empty --related --width --output-format --output-file --file --rules-file --separator --alias --anon --pivot --ignore-assertions --begin --end --daily --weekly --monthly --quarterly --yearly --period --date2 --unmarked --pending --cleared --real --depth --empty --cost --value --auto --forecast --help --debug --version TEXT cat < "$_HLEDGER_COMPLETION_TEMPDIR/options-stats.txt" -o -f -I -b -e -D -W -M -Q -Y -p -U -C -P -C -R -N -E -B -V -h --output-file --file --rules-file --separator --alias --anon --pivot --ignore-assertions --begin --end --daily --weekly --monthly --quarterly --yearly --period --date2 --unmarked --pending --cleared --real --depth --empty --cost --value --auto --forecast --help --debug --version TEXT cat < "$_HLEDGER_COMPLETION_TEMPDIR/options-tags.txt" -f -f -I -b -e -D -W -M -Q -Y -p -U -C -P -C -R -N -E -B -V -h --file --rules-file --separator --alias --anon --pivot --ignore-assertions --begin --end --daily --weekly --monthly --quarterly --yearly --period --date2 --unmarked --pending --cleared --real --depth --empty --cost --value --auto --forecast --help --debug --version TEXT cat < "$_HLEDGER_COMPLETION_TEMPDIR/options-ui.txt" -F -T -f -I -b -e -D -W -M -Q -Y -p -U -C -P -C -R -N -E -B -V -h --watch --theme --register --change --flat --tree --future --file --rules-file --separator --alias --anon --pivot --ignore-assertions --begin --end --daily --weekly --monthly --quarterly --yearly --period --date2 --unmarked --pending --cleared --real --depth --empty --cost --value --auto --forecast --help --debug --version TEXT cat < "$_HLEDGER_COMPLETION_TEMPDIR/options-web.txt" -f -I -b -e -D -W -M -Q -Y -p -U -C -P -C -R -N -E -B -V -h --server --host --port --base-url --file-url --capabilities --capabilities-header --file --rules-file --separator --alias --anon --pivot --ignore-assertions --begin --end --daily --weekly --monthly --quarterly --yearly --period --date2 --unmarked --pending --cleared --real --depth --empty --cost --value --auto --forecast --help --debug --version TEXT cat < "$_HLEDGER_COMPLETION_TEMPDIR/options-close.txt" -f -I -b -e -D -W -M -Q -Y -p -U -C -P -C -R -N -E -B -V -h -R -e -e -f -f -f -1 --opening --closing --file --rules-file --separator --alias --anon --pivot --ignore-assertions --begin --end --daily --weekly --monthly --quarterly --yearly --period --date2 --unmarked --pending --cleared --real --depth --empty --cost --value --auto --forecast --help --debug --version TEXT cat < "$_HLEDGER_COMPLETION_TEMPDIR/options-rewrite.txt" -f -I -b -e -D -W -M -Q -Y -p -U -C -P -C -R -N -E -B -V -h -f -f -f -f -f -1 -2 --add-posting --auto --add-posting --diff --file --rules-file --separator --alias --anon --pivot --ignore-assertions --begin --end --daily --weekly --monthly --quarterly --yearly --period --date2 --unmarked --pending --cleared --real --depth --empty --cost --value --auto --forecast --help --debug --version --add-posting --add-posting --add-posting --add-posting --add-posting --add-posting --add-posting --add-posting --add-posting --auto --auto --auto --auto --auto TEXT cat < "$_HLEDGER_COMPLETION_TEMPDIR/options-api.txt" -h -f -d -p -h --swagger --version --file --static-dir --host --port --version --help TEXT cat < "$_HLEDGER_COMPLETION_TEMPDIR/options-check-dates.txt" -f -f -I -b -e -D -W -M -Q -Y -p -U -C -P -C -R -N -E -B -V -h --date2 --strict --strict --file --rules-file --separator --alias --anon --pivot --ignore-assertions --begin --end --daily --weekly --monthly --quarterly --yearly --period --date2 --unmarked --pending --cleared --real --depth --empty --cost --value --auto --forecast --help --debug --version TEXT cat < "$_HLEDGER_COMPLETION_TEMPDIR/options-check-dupes.txt" -f -I -b -e -D -W -M -Q -Y -p -U -C -P -C -R -N -E -B -V -h --file --rules-file --separator --alias --anon --pivot --ignore-assertions --begin --end --daily --weekly --monthly --quarterly --yearly --period --date2 --unmarked --pending --cleared --real --depth --empty --cost --value --auto --forecast --help --debug --version TEXT cat < "$_HLEDGER_COMPLETION_TEMPDIR/options-print-unique.txt" -f -I -b -e -D -W -M -Q -Y -p -U -C -P -C -R -N -E -B -V -h --file --rules-file --separator --alias --anon --pivot --ignore-assertions --begin --end --daily --weekly --monthly --quarterly --yearly --period --date2 --unmarked --pending --cleared --real --depth --empty --cost --value --auto --forecast --help --debug --version TEXT cat < "$_HLEDGER_COMPLETION_TEMPDIR/options-register-match.txt" -f -I -b -e -D -W -M -Q -Y -p -U -C -P -C -R -N -E -B -V -h --file --rules-file --separator --alias --anon --pivot --ignore-assertions --begin --end --daily --weekly --monthly --quarterly --yearly --period --date2 --unmarked --pending --cleared --real --depth --empty --cost --value --auto --forecast --help --debug --version TEXT cat < "$_HLEDGER_COMPLETION_TEMPDIR/options-roi.txt" -f -I -b -e -D -W -M -Q -Y -p -U -C -P -C -R -N -E -B -V -h --cashflow --investment --pnl --file --rules-file --separator --alias --anon --pivot --ignore-assertions --begin --end --daily --weekly --monthly --quarterly --yearly --period --date2 --unmarked --pending --cleared --real --depth --empty --cost --value --auto --forecast --help --debug --version TEXT cat < "$_HLEDGER_COMPLETION_TEMPDIR/options-test.txt" -h --help --debug --version TEXT cat < "$_HLEDGER_COMPLETION_TEMPDIR/options-outlet.txt" -n -h TEXT cat < "$_HLEDGER_COMPLETION_TEMPDIR/options-help.txt" -h --info --man --pager --cat --help TEXT cat < "$_HLEDGER_COMPLETION_TEMPDIR/options-bs.txt" -H -N -A -T -S -O -o -f -I -b -e -D -W -M -Q -Y -p -U -C -P -C -R -N -E -B -V -h --change --cumulative --historical --flat --drop --no-total --tree --average --row-total --no-elide --format --pretty-tables --sort-amount --output-format --output-file --file --rules-file --separator --alias --anon --pivot --ignore-assertions --begin --end --daily --weekly --monthly --quarterly --yearly --period --date2 --unmarked --pending --cleared --real --depth --empty --cost --value --auto --forecast --help --debug --version TEXT cat < "$_HLEDGER_COMPLETION_TEMPDIR/options-bse.txt" -H -N -A -T -S -O -o -f -I -b -e -D -W -M -Q -Y -p -U -C -P -C -R -N -E -B -V -h --change --cumulative --historical --flat --drop --no-total --tree --average --row-total --no-elide --format --pretty-tables --sort-amount --output-format --output-file --file --rules-file --separator --alias --anon --pivot --ignore-assertions --begin --end --daily --weekly --monthly --quarterly --yearly --period --date2 --unmarked --pending --cleared --real --depth --empty --cost --value --auto --forecast --help --debug --version TEXT cat < "$_HLEDGER_COMPLETION_TEMPDIR/options-cf.txt" -H -N -A -T -S -O -o -f -I -b -e -D -W -M -Q -Y -p -U -C -P -C -R -N -E -B -V -h --change --cumulative --historical --flat --drop --no-total --tree --average --row-total --no-elide --format --pretty-tables --sort-amount --output-format --output-file --file --rules-file --separator --alias --anon --pivot --ignore-assertions --begin --end --daily --weekly --monthly --quarterly --yearly --period --date2 --unmarked --pending --cleared --real --depth --empty --cost --value --auto --forecast --help --debug --version TEXT cat < "$_HLEDGER_COMPLETION_TEMPDIR/options-is.txt" -H -N -A -T -S -O -o -f -I -b -e -D -W -M -Q -Y -p -U -C -P -C -R -N -E -B -V -h --change --cumulative --historical --flat --drop --no-total --tree --average --row-total --no-elide --format --pretty-tables --sort-amount --output-format --output-file --file --rules-file --separator --alias --anon --pivot --ignore-assertions --begin --end --daily --weekly --monthly --quarterly --yearly --period --date2 --unmarked --pending --cleared --real --depth --empty --cost --value --auto --forecast --help --debug --version TEXT