From 5fa4cf6d335e5a976f58d831874fe752f3d6f8ad Mon Sep 17 00:00:00 2001 From: punkfairie <23287005+punkfairie@users.noreply.github.com> Date: Sun, 10 Mar 2024 14:47:10 -0700 Subject: [PATCH] =?UTF-8?q?=E2=9E=95=20build(fish):=20fish=5Fplugins=20act?= =?UTF-8?q?ing=20up?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .config/fish/conf.d/autopair.fish | 39 +++++++++++++++++++ .config/fish/fish_plugins | 1 + .../fish/functions/_autopair_backspace.fish | 9 +++++ .../fish/functions/_autopair_insert_left.fish | 13 +++++++ .../functions/_autopair_insert_right.fish | 11 ++++++ .../fish/functions/_autopair_insert_same.fish | 20 ++++++++++ .config/fish/functions/_autopair_tab.fish | 7 ++++ 7 files changed, 100 insertions(+) create mode 100644 .config/fish/conf.d/autopair.fish create mode 100644 .config/fish/functions/_autopair_backspace.fish create mode 100644 .config/fish/functions/_autopair_insert_left.fish create mode 100644 .config/fish/functions/_autopair_insert_right.fish create mode 100644 .config/fish/functions/_autopair_insert_same.fish create mode 100644 .config/fish/functions/_autopair_tab.fish diff --git a/.config/fish/conf.d/autopair.fish b/.config/fish/conf.d/autopair.fish new file mode 100644 index 0000000..abb4bf3 --- /dev/null +++ b/.config/fish/conf.d/autopair.fish @@ -0,0 +1,39 @@ +status is-interactive || exit + +set --global autopair_left "(" "[" "{" '"' "'" +set --global autopair_right ")" "]" "}" '"' "'" +set --global autopair_pairs "()" "[]" "{}" '""' "''" + +function _autopair_fish_key_bindings --on-variable fish_key_bindings + set --query fish_key_bindings[1] || return + + test $fish_key_bindings = fish_default_key_bindings && + set --local mode default insert || + set --local mode insert default + + bind --mode $mode[-1] --erase \177 \b \t + + bind --mode $mode[1] \177 _autopair_backspace # macOS ⌫ + bind --mode $mode[1] \b _autopair_backspace + bind --mode $mode[1] \t _autopair_tab + + printf "%s\n" $autopair_pairs | while read --local left right --delimiter "" + bind --mode $mode[-1] --erase $left $right + if test $left = $right + bind --mode $mode[1] $left "_autopair_insert_same \\$left" + else + bind --mode $mode[1] $left "_autopair_insert_left \\$left \\$right" + bind --mode $mode[1] $right "_autopair_insert_right \\$right" + end + end +end + +_autopair_fish_key_bindings + +function _autopair_uninstall --on-event autopair_uninstall + string collect ( + bind --all | string replace --filter --regex -- "_autopair.*" --erase + set --names | string replace --filter --regex -- "^autopair" "set --erase autopair" + ) | source + functions --erase (functions --all | string match "_autopair_*") +end diff --git a/.config/fish/fish_plugins b/.config/fish/fish_plugins index bf96e2b..1ea7a0a 100644 --- a/.config/fish/fish_plugins +++ b/.config/fish/fish_plugins @@ -1,3 +1,4 @@ jorgebucaran/fisher catppuccin/fish decors/fish-colored-man +jorgebucaran/autopair.fish diff --git a/.config/fish/functions/_autopair_backspace.fish b/.config/fish/functions/_autopair_backspace.fish new file mode 100644 index 0000000..a43fa79 --- /dev/null +++ b/.config/fish/functions/_autopair_backspace.fish @@ -0,0 +1,9 @@ +function _autopair_backspace + set --local index (commandline --cursor) + set --local buffer (commandline) + + test $index -ge 1 && + contains -- (string sub --start=$index --length=2 -- "$buffer") $autopair_pairs && + commandline --function delete-char + commandline --function backward-delete-char +end diff --git a/.config/fish/functions/_autopair_insert_left.fish b/.config/fish/functions/_autopair_insert_left.fish new file mode 100644 index 0000000..f078e86 --- /dev/null +++ b/.config/fish/functions/_autopair_insert_left.fish @@ -0,0 +1,13 @@ +function _autopair_insert_left --argument-names left right + set --local buffer (commandline) + set --local before (commandline --cut-at-cursor) + + commandline --insert -- $left + + switch "$buffer" + case "$before"{," "\*,$autopair_right\*} + set --local index (commandline --cursor) + commandline --insert -- $right + commandline --cursor $index + end +end diff --git a/.config/fish/functions/_autopair_insert_right.fish b/.config/fish/functions/_autopair_insert_right.fish new file mode 100644 index 0000000..a0bd61c --- /dev/null +++ b/.config/fish/functions/_autopair_insert_right.fish @@ -0,0 +1,11 @@ +function _autopair_insert_right --argument-names key + set --local buffer (commandline) + set --local before (commandline --cut-at-cursor) + + switch "$buffer" + case "$before$key"\* + commandline --cursor (math (commandline --cursor) + 1) + case \* + commandline --insert -- $key + end +end diff --git a/.config/fish/functions/_autopair_insert_same.fish b/.config/fish/functions/_autopair_insert_same.fish new file mode 100644 index 0000000..27f971d --- /dev/null +++ b/.config/fish/functions/_autopair_insert_same.fish @@ -0,0 +1,20 @@ +function _autopair_insert_same --argument-names key + set --local buffer (commandline) + set --local index (commandline --cursor) + set --local next (string sub --start=(math $index + 1) --length=1 -- "$buffer") + + if test (math (count (string match --all --regex -- "$key" "$buffer")) % 2) = 0 + test $key = $next && commandline --cursor (math $index + 1) && return + + commandline --insert -- $key + + if test $index -lt 1 || + contains -- (string sub --start=$index --length=1 -- "$buffer") "" " " $autopair_left && + contains -- $next "" " " $autopair_right + commandline --insert -- $key + commandline --cursor (math $index + 1) + end + else + commandline --insert -- $key + end +end diff --git a/.config/fish/functions/_autopair_tab.fish b/.config/fish/functions/_autopair_tab.fish new file mode 100644 index 0000000..f2ab8eb --- /dev/null +++ b/.config/fish/functions/_autopair_tab.fish @@ -0,0 +1,7 @@ +function _autopair_tab + commandline --paging-mode && down-or-search && return + + string match --quiet --regex -- '\$[^\s]*"$' (commandline --current-token) && + commandline --function end-of-line --function backward-delete-char + commandline --function complete +end