#1: Clean up comments, function name, variable names
This commit is contained in:
parent
3780b38734
commit
19d7d0fa60
1 changed files with 17 additions and 17 deletions
32
bash-abbr
32
bash-abbr
|
|
@ -4,13 +4,9 @@
|
||||||
# put into the bashrc drop-in directory ~/.bashrc.d/
|
# put into the bashrc drop-in directory ~/.bashrc.d/
|
||||||
# and then sourced from ~/.bashrc.
|
# and then sourced from ~/.bashrc.
|
||||||
|
|
||||||
# WARNING: This plug-in overwrites the readling bindings for ' ', '|', '&', ';', '(', ')', '<', '>'
|
# WARNING: This plugin creates and overwrites various readline bindings.
|
||||||
# It also creates new bindings '\e\C-a<x>' where `<x>` is one of the characters above.
|
# Therefore, it might interfere with other plugins/custom bindings.
|
||||||
# Additionally, it overwrites the binding for '\C-m' and creates a corresponding binding '\e\C-an'.
|
# Some bindings may be changed (e.g. \e\C-a...) while others should not (e.g. \C-m)
|
||||||
# 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.
|
|
||||||
|
|
||||||
# Regexes for (heuristically) determining whether we're abbreviating the first word.
|
# Regexes for (heuristically) determining whether we're abbreviating the first word.
|
||||||
# Construction put into functions to prevent global namespace pollution
|
# Construction put into functions to prevent global namespace pollution
|
||||||
|
|
@ -28,9 +24,9 @@ _control_op () {
|
||||||
_control_op+="|$_semicolon_or_case_operators"
|
_control_op+="|$_semicolon_or_case_operators"
|
||||||
_control_op+="|$_opening_paren"
|
_control_op+="|$_opening_paren"
|
||||||
printf %s "$_control_op"
|
printf %s "$_control_op"
|
||||||
|
unset -f _control_op
|
||||||
}
|
}
|
||||||
declare _control_op="$(_control_op)"
|
declare _control_op="$(_control_op)"
|
||||||
unset -f _control_op
|
|
||||||
|
|
||||||
_redir_or_assign_word () {
|
_redir_or_assign_word () {
|
||||||
declare -r _varredir_or_number='\{[[:alpha:]_][[:alnum:]_]*\}|[[:digit:]]*' \
|
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))"
|
_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)"
|
declare -r _word_redirection_or_assignment="($_generic_redirection[[:space:]]*|$_identifier_equals)($_word)"
|
||||||
printf %s "$_close_redirection|$_word_redirection_or_assignment"
|
printf %s "$_close_redirection|$_word_redirection_or_assignment"
|
||||||
|
unset -f _redir_or_assign_word
|
||||||
}
|
}
|
||||||
declare -r _redir_or_assign_word="$(_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:]])*)"
|
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
|
# 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).
|
# 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.
|
# $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 \
|
declare -r line=$1 point=$2 mark=$3 new_line=$4 new_point=$5 new_mark=$6 \
|
||||||
self_insertion=$7 \
|
|
||||||
level=${#FUNCNAME[@]}
|
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
|
# `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
|
# 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.
|
# 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.
|
# 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
|
[[ ${replacement::1} =~ [*$] || ${line_until_point%"$word_at_point"} =~ $is_first_word_prefix$ ]]; then
|
||||||
|
|
||||||
|
# Prevent infinite recursion
|
||||||
_expanded_aliases[$word_at_point]=1
|
_expanded_aliases[$word_at_point]=1
|
||||||
|
|
||||||
# Account for inhibited self-insertion
|
# Account for inhibited self-insertion
|
||||||
[[ ${replacement::1} =~ [-$] ]] && set -- ''
|
[[ ${replacement::1} =~ [-$] ]] && self_insertion=
|
||||||
|
|
||||||
# Strip special flags (or space) from the beginning of the replacement
|
# Strip special flags (or space) from the beginning of the replacement
|
||||||
replacement=${replacement#[*$ -]}
|
replacement=${replacement#[*$ -]}
|
||||||
|
|
@ -171,7 +168,7 @@ _try_abbrev () {
|
||||||
unset -v _expanded_aliases
|
unset -v _expanded_aliases
|
||||||
}
|
}
|
||||||
|
|
||||||
main () {
|
bash_abbr () {
|
||||||
# From the bash man page:
|
# From the bash man page:
|
||||||
# metacharacter
|
# metacharacter
|
||||||
# A character that, when unquoted, separates words. One of the following:
|
# A character that, when unquoted, separates words. One of the following:
|
||||||
|
|
@ -184,11 +181,14 @@ main () {
|
||||||
# bind '"'"$ch"'": "\e\C-a'"$ch"'\e^"' # Moves point weirdly
|
# bind '"'"$ch"'": "\e\C-a'"$ch"'\e^"' # Moves point weirdly
|
||||||
done
|
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 -x '"\e\C-an": _try_abbrev ""'
|
||||||
bind '"\C-m": "\e\C-an\e^\C-j"'
|
bind '"\C-m": "\e\C-an\e^\C-j"'
|
||||||
}
|
}
|
||||||
|
|
||||||
# Disable plugin by commenting this line
|
# Disable plugin by commenting this line and uncommenting the line below that
|
||||||
main
|
# 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
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue