hledger/shell-completion/hledger-completion.bash.m4

133 lines
4.1 KiB
Plaintext
Raw Normal View History

2019-01-10 21:08:46 +03:00
# 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
2019-01-12 22:05:13 +03:00
# Note: grep "^$wordToComplete" is (functional) not safe to use if the word
2019-01-12 14:25:07 +03:00
# contains regex special chars. But it might be no problem because of
# COMP_WORDBREAKS.
2019-01-10 21:08:46 +03:00
2019-01-12 22:05:13 +03:00
# 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
2019-01-12 22:05:13 +03:00
# best if compgen could read from stdin but it does not.
2019-01-12 22:05:13 +03:00
# 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.
2019-01-10 21:08:46 +03:00
# Therefore, we create temp files in RAM for completion suggestions (see below).
readonly _HLEDGER_COMPLETION_TEMPDIR=$(mktemp -d)
2019-01-10 21:08:46 +03:00
_hledger_completion_function() {
2019-01-11 01:56:56 +03:00
#declare cmd=$1
2019-01-10 21:08:46 +03:00
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") )
2019-01-10 21:08:46 +03:00
break
fi
subcommand=
done
if [[ -z $subcommand ]]; then
declare completeFiles filenameSoFar
case $precedingWord in
-f|--file|--rules-file)
completeFiles=1
filenameSoFar=$wordToComplete
;;
2019-01-12 01:59:28 +03:00
=)
completeFiles=1
filenameSoFar=$wordToComplete
;;
2019-01-10 21:08:46 +03:00
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[@]}" )
2019-01-10 21:08:46 +03:00
else
COMPREPLY+=( $(grep -h "^$wordToComplete" -- "$_HLEDGER_COMPLETION_TEMPDIR/commands.txt" "$_HLEDGER_COMPLETION_TEMPDIR/generic-options.txt") )
2019-01-10 21:08:46 +03:00
fi
else
2019-01-11 01:56:56 +03:00
# Almost all subcommands accept [QUERY]
# -> always add accounts to completion list
2019-01-10 21:08:46 +03:00
2019-01-12 14:25:07 +03:00
# 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
2019-01-19 02:23:42 +03:00
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!
2019-01-10 21:08:46 +03:00
COMP_WORDBREAKS=' '
fi
}
2019-02-10 23:04:55 +03:00
_hledger_extension_completion_function() {
declare cmd=$1
# Change parameters and arguments and call the
# normal hledger completion function.
declare extensionName=${cmd#*-}
export -a COMP_WORDS=( "hledger" "$extensionName" "${COMP_WORDS[@]:1}" )
#echo; echo "debug: ${COMP_WORDS[@]}"
shift
_hledger_completion_function "hledger" "$@"
}
2019-01-11 01:56:56 +03:00
# Register completion function for hledger:
complete -F _hledger_completion_function hledger
2019-01-10 21:08:46 +03:00
2019-02-10 23:04:55 +03:00
# Register completion functions for hledger extensions:
complete -F _hledger_extension_completion_function hledger-ui
complete -F _hledger_extension_completion_function hledger-web
2019-01-11 01:56:56 +03:00
# Include lists of commands and options generated by the Makefile using the
# m4 macro processor.
2019-01-10 21:08:46 +03:00
# Included files must have exactly one newline at EOF to prevent weired errors.
cat <<TEXT > "$_HLEDGER_COMPLETION_TEMPDIR/commands.txt"
2019-01-10 21:08:46 +03:00
include(`commands.txt')dnl
TEXT
2019-01-19 02:23:42 +03:00
cat <<TEXT > "$_HLEDGER_COMPLETION_TEMPDIR/query-filters.txt"
include(`query-filters.txt')dnl
TEXT
cat <<TEXT > "$_HLEDGER_COMPLETION_TEMPDIR/generic-options.txt"
2019-01-10 21:08:46 +03:00
include(`generic-options.txt')dnl
TEXT
include(`foreach2.m4')
foreach(`cmd', (include(`commands-list.txt')), `
cat <<TEXT > "$_HLEDGER_COMPLETION_TEMPDIR/options-cmd.txt"
2019-01-10 21:08:46 +03:00
include(options-cmd.txt)dnl
TEXT
')