Add support for file extension aliases

This commit is contained in:
tsi 2025-10-26 18:42:18 +01:00
parent 03302fd770
commit 2d6f7ff2ab
No known key found for this signature in database

View file

@ -88,14 +88,18 @@ _try_abbrev_internal () {
# Check if the character at point is a shell metacharacter or EOL # Check if the character at point is a shell metacharacter or EOL
[[ $point -eq ${#line} || "${line:point:1}" = [\<\>\&\;\(\)\|[:space:]] ]] && [[ $point -eq ${#line} || "${line:point:1}" = [\<\>\&\;\(\)\|[:space:]] ]] &&
# Determine word to complete. # Determine word to complete
# Uses the same word separators as bash (| & ; ( ) space tab newline) (aka "shell metacharacters") # Uses the same word separators as bash (| & ; ( ) space tab newline) (aka "shell metacharacters")
# But < and > are ignored, since they always expect a file/delimiter/string afterwards and not a command # But < and > are ignored, since they always expect a file/delimiter/string afterwards and not a command
declare -r word_at_point=${line_until_point##*[&;()|[:space:]]} && declare -r word_at_point=${line_until_point##*[&;()|[:space:]]} &&
# Select replacement (if any). # Select replacement (if any)
declare replacement=${BASH_ALIASES["${word_at_point:-__nonexistent}"]-} && declare replacement=${BASH_ALIASES["${word_at_point:-__nonexistent}"]-} &&
# Account for file extension aliases. Normal aliases take precedence
declare file_extension_replacement=${BASH_ALIASES[".${word_at_point##*.}"]-} &&
[[ $replacement ]] || { replacement=${file_extension_replacement:+${file_extension_replacement%!} $word_at_point${file_extension_replacement: -1}} && replacement=${replacement%[^!]}; } &&
[[ $replacement ]] && [[ $replacement ]] &&
# Determine if it's the first word of a simple command, unless we're expanding a global alias (prefix: "*" or "$"). # Determine if it's the first word of a simple command, unless we're expanding a global alias (prefix: "*" or "$").
@ -123,7 +127,7 @@ _try_abbrev_internal () {
# Recursively expand replacement if we haven't expanded it already # Recursively expand replacement if we haven't expanded it already
declare tmp=" $replacement" declare tmp=" $replacement"
[[ $tmp =~ ^([[:space:]]*$is_first_word_prefix) ]] && tmp=${tmp#"${BASH_REMATCH[1]}"} || echo NO [[ $tmp =~ ^([[:space:]]*$is_first_word_prefix) ]] && tmp=${tmp#"${BASH_REMATCH[1]}"}
declare -r word_to_expand=${tmp%%[<>&;()|[:space:]]*} declare -r word_to_expand=${tmp%%[<>&;()|[:space:]]*}
if [[ ! ${_expanded_aliases[$word_to_expand]-} ]]; then if [[ ! ${_expanded_aliases[$word_to_expand]-} ]]; then
declare -r point_for_replacement=$((${#BASH_REMATCH[1]}+${#word_to_expand})) declare -r point_for_replacement=$((${#BASH_REMATCH[1]}+${#word_to_expand}))