Fix the following issue, return a patch or set of patches.
In a situation where both tags that start with - and tags with spaces which require quoted results are returned by bab complete, bab search --<tab> and bab search '--<tab> result in bab search '-, incorrectly removing the second dash in both cases.
#!/bin/bash
# Ensure ':' is *not* treated as a word-break character by Readline when the
# shell performs programmable completion. We do this **once**, when the
# script is sourced, so that Bash constructs the COMP_WORDS array correctly
# *before* the completion function is invoked. This is more reliable than
# tweaking COMP_WORDBREAKS inside the completion function (which comes too
# late, because Bash has already tokenised the command line by then).
if [[ $COMP_WORDBREAKS == *:* ]]; then
COMP_WORDBREAKS=${COMP_WORDBREAKS//:}
fi
# This script provides bash completion for the bab command.
# It is designed to handle tags with spaces and colons correctly.
# _bab_get_prefixed_completions fetches the list of all tags from the bab
# command and adds a prefix (+ or -) to each tag if one is present in the
# current word being completed.
_bab_get_prefixed_completions() {
local cur="$1"
local prefix=""
local core_cur="$cur"
shift
local search_terms=("$@")
# Handle quote before prefix: 'prefix or prefix'
local quoted=false
if [[ "$cur" == \'* ]]; then
quoted=true
core_cur="${cur:1}" # Strip leading quote
fi
# Detect explicit + or - prefix (sign)
if [[ "$core_cur" == \+* ]]; then
prefix="+"
# Only strip the + if it's not followed by another + or -
# This preserves cases like +-tag where we want to keep the +
if [[ "$core_cur" != ?([+]|[-])[+]* && "$core_cur" != ?([+]|[-])[-]* ]]; then
prefix="+"
core_cur="${core_cur:1}"
fi
elif [[ "$core_cur" == -* ]]; then
prefix="-"
core_cur="${core_cur:1}"
fi
# Decode backslash escapes: turn "\x" into "x", while preserving a literal
# backslash written as "\\".
local search_cur
search_cur=$(printf '%s' "$core_cur" | sed 's/\\\(\)/\\/g; s/\\\([^\\]\)/\1/g')
# Decode each prior search term (remove surrounding quotes/backslashes) before forwarding
local decoded_terms=()
for st in "${search_terms[@]}"; do
# Preserve leading + or - sign if present
local sign=""
local core="$st"
if [[ "$core" == [+-]* ]]; then
sign="${core:0:1}"
core="${core:1}"
fi
# Strip surrounding single or double quotes if both present
if [[ "$core" == \"*\" && "$core" == *\" ]]; then
core="${core:1:-1}"
elif [[ "$core" == \'*\' && "$core" == *\' ]]; then
core="${core:1:-1}"
fi
# Decode backslash escapes within the term
core=$(printf '%s' "$core" | sed 's/\\\\/\\/g; s/\\\([^\\]\)/\1/g')
decoded_terms+=("${sign}${core}")
done
local completions
completions=$(bab complete "${decoded_terms[@]}" "$search_cur")
if [[ -n "$completions" ]]; then
# Handle the case where user provided a prefix
echo "$completions" | awk -v p="$prefix" '{ print p $0 }'
else
echo "$completions"
fi
}
# _bab_unquote_path removes quotes from a file path to check if it exists
# Handles both single quotes and backslash escapes
_bab_unquote_path() {
local path="$1"
# Remove surrounding single quotes
if [[ "$path" == \'*\' ]]; then
path="${path:1:-1}"
fi
# Simple backslash unescape for spaces
path="${path//\\ / }"
printf '%s' "$path"
}
# _bab_apply_consistent_quoting applies the same quoting logic to any array of completion items
# This ensures file paths and tags use consistent quoting when mixed together
_bab_apply_consistent_quoting() {
local input_cur="$1"
shift
local items=("$@")
# If no items, return empty
if [[ ${#items[@]} -eq 0 ]]; then
return
fi
# Check if any item needs quoting
local needs_quoting=false
for item in "${items[@]}"; do
if [[ "$item" =~ [[:space:]] ]] || [[ "$item" == *\\* ]] || [[ "$item" == *\"* ]] || [[ "$item" == *\'* ]]; then
needs_quoting=true
break
fi
done
# Apply consistent quoting based on input and needs
if [[ -z "$input_cur" ]] || [[ "$input_cur" == "+" ]] || [[ "$input_cur" == "-" ]] || [[ "$input_cur" == "'" ]]; then
# Empty input - show unquoted for clean browsing
printf '%s\n' "${items[@]}"
elif $needs_quoting; then
# Quote everything consistently using single quotes
for item in "${items[@]}"; do
local quoted="${item//\'/\'\\\'\'}"
printf '%s\n' "'$quoted'"
done
else
# No quoting needed
printf '%s\n' "${items[@]}"
fi
}
# _bab_complete_tags is a helper function that provides completions for tags.
# It relies on `bab complete` to filter tags.
# If mixed_mode is true, returns raw unquoted results for consistent processing
_bab_complete_tags() {
local cur="$1"
shift
local mixed_mode="false"
if [[ "$1" == "true" || "$1" == "false" ]]; then
mixed_mode="$1"
shift
fi
local search_terms=("$@")
local OLD_IFS="$IFS"
IFS=$'\n'
# Get completions from the bab command itself. It should handle the filtering.
local completions=$(_bab_get_prefixed_completions "$cur" "${search_terms[@]}")
# If nothing is returned, avoid emitting an empty quoted string which
# Bash would treat as a literal completion candidate.
if [[ -z "$completions" ]]; then
COMPREPLY=()
IFS="$OLD_IFS"
return
fi
# Collect all non-empty lines
local lines=()
while IFS= read -r line; do
[[ -z "$line" ]] && continue
lines+=("$line")
done <<< "$completions"
if [[ "$mixed_mode" == "true" ]]; then
# Return raw results for mixed completion
COMPREPLY=("${lines[@]}")
else
# Use the consistent quoting helper for standalone tag completion
mapfile -t COMPREPLY < <(_bab_apply_consistent_quoting "$cur" "${lines[@]}")
fi
IFS="$OLD_IFS"
}
# _bab_complete is the main completion function. It is called by the wrapper
# function and contains the main logic for completing commands and their
# arguments.
_bab_complete() {
local cur="${COMP_WORDS[COMP_CWORD]}"
local prev="${COMP_WORDS[COMP_CWORD-1]}"
local command="${COMP_WORDS[1]}"
# If we are completing the subcommand itself
if [[ $COMP_CWORD -eq 1 ]]; then
COMPREPLY=($(compgen -W "tag search remove complete help" -- "$cur"))
return 0
fi
case "$command" in
tag)
local tags_only=false
# Check for -- or a non-file argument in words before the current one
for ((i=2; i < COMP_CWORD; i++)); do
local word="${COMP_WORDS[i]}"
if [[ "$word" == "--" ]]; then
tags_only=true
break
else
# Unquote the word and check if it's a file
local unquoted_word
unquoted_word=$(_bab_unquote_path "$word")
if ! [[ -e "$unquoted_word" ]]; then
tags_only=true
break
fi
fi
done
if $tags_only; then
# After --, or if we have a prefix, or after a non-file argument,
# only complete tags.
_bab_complete_tags "$cur"
# Set completion options based on number of results
if [[ ${#COMPREPLY[@]} -eq 1 ]]; then
compopt +o nospace -o default # Single match: allow space
else
compopt -o nospace +o default # Multiple/no matches: no space
fi
else
# Before --, complete files, dirs, --, and tags with consistent quoting
mapfile -t file_comp < <(compgen -f -d -- "$cur")
_bab_complete_tags "$cur" "true" # mixed_mode=true for raw results
local tag_comp=("${COMPREPLY[@]}")
local dash_comp=()
if [[ "--" == "$cur"* ]]; then
dash_comp+=("--")
fi
# Apply consistent quoting to all completion types and combine
local all_items=()
all_items+=("${file_comp[@]}")
all_items+=("${dash_comp[@]}")
all_items+=("${tag_comp[@]}")
mapfile -t COMPREPLY < <(_bab_apply_consistent_quoting "$cur" "${all_items[@]}")
# Set completion options based on total number of results
if [[ ${#COMPREPLY[@]} -eq 1 ]]; then
compopt +o nospace -o default # Single match: allow space
else
compopt -o nospace +o default # Multiple/no matches: no space
fi
fi
;;
search)
# Extract existing search terms before current word for context-aware completion
local search_terms=("${COMP_WORDS[@]:2:$((COMP_CWORD-2))}")
_bab_complete_tags "$cur" "${search_terms[@]}"
# Set completion options based on number of results
if [[ ${#COMPREPLY[@]} -eq 1 ]]; then
compopt +o nospace -o default # Single match: allow space
else
compopt -o nospace +o default # Multiple/no matches: no space
fi
;;
remove)
# Use bash-native file completion
compopt -o default
;;
complete)
# Gather existing search terms (words after 'complete' and before the current word)
local search_terms=()
if (( COMP_CWORD > 2 )); then
for ((i=2; i<COMP_CWORD; i++)); do
search_terms+=("${COMP_WORDS[i]}")
done
fi
_bab_complete_tags "$cur" "${search_terms[@]}"
# Set completion options based on number of results
if [[ ${#COMPREPLY[@]} -eq 1 ]]; then
compopt +o nospace -o default # Single match: allow space
else
compopt -o nospace +o default # Multiple/no matches: no space
fi
;;
help)
if [[ $COMP_CWORD -eq 2 ]]; then
COMPREPLY=($(compgen -W "tag search remove complete help" -- "$cur"))
# Set completion options based on number of results
if [[ ${#COMPREPLY[@]} -eq 1 ]]; then
compopt +o nospace -o default # Single match: allow space
else
compopt -o nospace +o default # Multiple/no matches: no space
fi
else
# No completions after first argument
compopt +o default
fi
;;
*)
compopt +o default
;;
esac
}
# _bab_complete_wrapper is the entry point for the completion. Since we have
# already removed ':' from COMP_WORDBREAKS at load-time, we do not need to
# fiddle with it dynamically any more. The wrapper remains in case we need to
# add extra session-specific tweaks later.
_bab_complete_wrapper() {
_bab_complete
}
complete -F _bab_complete_wrapper bab