From fc89340c93e1c6a44fbac7b0bbc1e7896416f6d3 Mon Sep 17 00:00:00 2001 From: Vladimir Zhelezov Date: Wed, 9 Dec 2020 12:23:09 +0100 Subject: [PATCH] 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. --- shell-completion/hledger-completion.bash | 16 +++++++++++++--- shell-completion/hledger-completion.bash.m4 | 16 +++++++++++++--- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/shell-completion/hledger-completion.bash b/shell-completion/hledger-completion.bash index 4d035876b..82e5cc870 100644 --- a/shell-completion/hledger-completion.bash +++ b/shell-completion/hledger-completion.bash @@ -158,8 +158,18 @@ _hledger_compreply_append() { # wordlist to compgen -- it will eat your quotes, drink your booze and... # Completion candidates are quoted accordingly first and then we leave it to # compgen to deal with readline. +# +# Arguments: +# $1: a newline separated wordlist with completion cadidates +# $2: (optional) a prefix string to add to generated completions +# $3: (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. _hledger_compgen() { local wordlist=$1 + local prefix=$2 + local match=$3 local quoted=() local word local i=0 @@ -170,7 +180,7 @@ _hledger_compgen() { done <<< "$wordlist" local IFS=$'\n' - compgen -W "${quoted[*]}" -- "$cur" + compgen -P "$prefix" -W "${quoted[*]}" -- "${match:-${cur:${#prefix}}}" } # Try required option argument completion. Set COMPREPLY and return 0 on @@ -258,8 +268,8 @@ _hledger_compreply_query() { _hledger_compreply "$( _hledger_compgen "$( - _hledger "${hledgerArgs[@]}" | sed "s/^/$query/g" - )" + _hledger "${hledgerArgs[@]}" + )" "$query" )" return 0 diff --git a/shell-completion/hledger-completion.bash.m4 b/shell-completion/hledger-completion.bash.m4 index afce257b8..7c5e36382 100644 --- a/shell-completion/hledger-completion.bash.m4 +++ b/shell-completion/hledger-completion.bash.m4 @@ -158,8 +158,18 @@ _hledger_compreply_append() { # wordlist to compgen -- it will eat your quotes, drink your booze and... # Completion candidates are quoted accordingly first and then we leave it to # compgen to deal with readline. +# +# Arguments: +# $1: a newline separated wordlist with completion cadidates +# $2: (optional) a prefix string to add to generated completions +# $3: (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. _hledger_compgen() { local wordlist=$1 + local prefix=$2 + local match=$3 local quoted=() local word local i=0 @@ -170,7 +180,7 @@ _hledger_compgen() { done <<< "$wordlist" local IFS=$'\n' - compgen -W "${quoted[*]}" -- "$cur" + compgen -P "$prefix" -W "${quoted[*]}" -- "${match:-${cur:${#prefix}}}" } # Try required option argument completion. Set COMPREPLY and return 0 on @@ -258,8 +268,8 @@ _hledger_compreply_query() { _hledger_compreply "$( _hledger_compgen "$( - _hledger "${hledgerArgs[@]}" | sed "s/^/$query/g" - )" + _hledger "${hledgerArgs[@]}" + )" "$query" )" return 0