From 19d7d0fa60af59fb2520a0c9b8c81732e747a6dc Mon Sep 17 00:00:00 2001 From: tsi Date: Thu, 23 Oct 2025 01:06:46 +0200 Subject: [PATCH] #1: Clean up comments, function name, variable names --- bash-abbr | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/bash-abbr b/bash-abbr index 2590e90..78d0507 100644 --- a/bash-abbr +++ b/bash-abbr @@ -4,13 +4,9 @@ # put into the bashrc drop-in directory ~/.bashrc.d/ # and then sourced from ~/.bashrc. -# WARNING: This plug-in overwrites the readling bindings for ' ', '|', '&', ';', '(', ')', '<', '>' -# It also creates new bindings '\e\C-a' where `` is one of the characters above. -# Additionally, it overwrites the binding for '\C-m' and creates a corresponding binding '\e\C-an'. -# Therefore, it might interfere with other plug-ins or custom bindings. -# You may change the '\e\C-a' binding-"prefix" to whatever you want as it's only used -# as a hook inside other readline macros. The bindings '|', '&', ';', '(', ')', '<', '>' (and their -# corresponding '\e\C-a' bindings) may also be dropped if you don't need them. +# WARNING: This plugin creates and overwrites various readline bindings. +# Therefore, it might interfere with other plugins/custom bindings. +# Some bindings may be changed (e.g. \e\C-a...) while others should not (e.g. \C-m) # Regexes for (heuristically) determining whether we're abbreviating the first word. # Construction put into functions to prevent global namespace pollution @@ -28,9 +24,9 @@ _control_op () { _control_op+="|$_semicolon_or_case_operators" _control_op+="|$_opening_paren" printf %s "$_control_op" + unset -f _control_op } declare _control_op="$(_control_op)" -unset -f _control_op _redir_or_assign_word () { declare -r _varredir_or_number='\{[[:alpha:]_][[:alnum:]_]*\}|[[:digit:]]*' \ @@ -64,9 +60,9 @@ _redir_or_assign_word () { _generic_redirection="($_redir_or_append_out_and_err|($_varredir_or_number)($_heredoc_herestring_in_out_etc))" declare -r _word_redirection_or_assignment="($_generic_redirection[[:space:]]*|$_identifier_equals)($_word)" printf %s "$_close_redirection|$_word_redirection_or_assignment" + unset -f _redir_or_assign_word } declare -r _redir_or_assign_word="$(_redir_or_assign_word)" -unset -f _redir_or_assign_word declare -r is_first_word_prefix="($_control_op)[[:space:]]*((($_redir_or_assign_word)[[:space:]])*)" # If the regex leads to problems/performance issues, you may try this simpler regex @@ -80,8 +76,8 @@ _try_abbrev_internal () { # Set the vars defined by $4, $5 and $6 to the updated values (line, point and mark). # $7 is the character/string to self-insert. declare -r line=$1 point=$2 mark=$3 new_line=$4 new_point=$5 new_mark=$6 \ - self_insertion=$7 \ level=${#FUNCNAME[@]} + declare self_insertion=$7 # `level` is needed to keep track of the recursion, because we don't have a return value but rather # set a variable defined in the calling context. This level is just appended to the identifier to make it unique # across all the recursive calls. @@ -116,10 +112,11 @@ _try_abbrev_internal () { # the regex can tell), then we bail and no alias expansion will happen. [[ ${replacement::1} =~ [*$] || ${line_until_point%"$word_at_point"} =~ $is_first_word_prefix$ ]]; then + # Prevent infinite recursion _expanded_aliases[$word_at_point]=1 # Account for inhibited self-insertion - [[ ${replacement::1} =~ [-$] ]] && set -- '' + [[ ${replacement::1} =~ [-$] ]] && self_insertion= # Strip special flags (or space) from the beginning of the replacement replacement=${replacement#[*$ -]} @@ -171,11 +168,11 @@ _try_abbrev () { unset -v _expanded_aliases } -main () { +bash_abbr () { # From the bash man page: # metacharacter # A character that, when unquoted, separates words. One of the following: - # | & ; ( ) < > space tab newline + # | & ; ( ) < > space tab newline # Note: double quote should be escaped, if added to the list of metacharacters declare ch for ch in ' ' '|' '&' ';' '(' ')' '<' '>'; do @@ -184,11 +181,14 @@ main () { # bind '"'"$ch"'": "\e\C-a'"$ch"'\e^"' # Moves point weirdly done - # Newline must be treated specially, because it doesn't self-insert - # and has the extra functionality of accepting a line. bind -x '"\e\C-an": _try_abbrev ""' bind '"\C-m": "\e\C-an\e^\C-j"' } -# Disable plugin by commenting this line -main +# Disable plugin by commenting this line and uncommenting the line below that +# This function name shall never change, so that script that source +# this plugin can detect for it. I.e., in your .bashrc, you may do +# `type bash_abbr &>/dev/null && shopt -u expand_aliases` +# after sourcing this plugin. +bash_abbr +# unset -f bash_abbr