diff --git a/.config/fish/conf.d/abbr_tips.fish b/.config/fish/conf.d/abbr_tips.fish new file mode 100644 index 00000000..1c6f180d --- /dev/null +++ b/.config/fish/conf.d/abbr_tips.fish @@ -0,0 +1,140 @@ +for mode in default insert + bind --mode $mode " " __abbr_tips_bind_space + bind --mode $mode \n __abbr_tips_bind_newline + bind --mode $mode \r __abbr_tips_bind_newline +end + +set -g __abbr_tips_used 0 + +# Trim simple/double quotes from args +function trim_value + echo "$argv" | string trim --left --right --chars '"\'' | string join ' ' +end + +function __abbr_tips_install --on-event abbr_tips_install + # Regexes used to find abbreviation inside command + # Only the first matching group will be tested as an abbr + set -Ux ABBR_TIPS_REGEXES + set -a ABBR_TIPS_REGEXES '(^(\w+\s+)+(-{1,2})\w+)(\s\S+)' + set -a ABBR_TIPS_REGEXES '(^(\s?(\w-?)+){3}).*' + set -a ABBR_TIPS_REGEXES '(^(\s?(\w-?)+){2}).*' + set -a ABBR_TIPS_REGEXES '(^(\s?(\w-?)+){1}).*' + + set -Ux ABBR_TIPS_PROMPT "\n💡 \e[1m{{ .abbr }}\e[0m => {{ .cmd }}" + set -gx ABBR_TIPS_AUTO_UPDATE background + + __abbr_tips_init +end + +function __abbr_tips --on-event fish_postexec -d "Abbreviation reminder for the current command" + set -l command (string split ' ' -- "$argv") + set -l cmd (string replace -r -a '\\s+' ' ' -- "$argv" ) + + # Update abbreviations lists when adding/removing abbreviations + if test "$command[1]" = abbr + # Parse args as abbr options + argparse --name abbr --ignore-unknown a/add e/erase g/global U/universal -- $command + + if set -q _flag_a + and not contains -- "$argv[2]" $__ABBR_TIPS_KEYS + set -a __ABBR_TIPS_KEYS "$argv[2]" + set -a __ABBR_TIPS_VALUES (trim_value "$argv[3..-1]") + else if set -q _flag_e + and set -l abb (contains -i -- "$argv[2]" $__ABBR_TIPS_KEYS) + set -e __ABBR_TIPS_KEYS[$abb] + set -e __ABBR_TIPS_VALUES[$abb] + end + else if test "$command[1]" = alias + # Update abbreviations list when adding aliases + set -l alias_key + set -l alias_value + + # Parse args as `alias` options + argparse --name alias --ignore-unknown s/save -- $command + + if string match -q '*=*' -- "$argv[2]" + set command_split (string split '=' -- $argv[2]) + set alias_key "a__$command_split[1]" + set alias_value $command_split[2..-1] + else + set alias_key "a__$argv[2]" + set alias_value $argv[3..-1] + end + + set alias_value (trim_value "$alias_value") + + if set -l abb (contains -i -- "$argv[3..-1]" $__ABBR_TIPS_KEYS) + set __ABBR_TIPS_KEYS[$abb] $alias_key + set __ABBR_TIPS_VALUES[$abb] $alias_value + else + set -a __ABBR_TIPS_KEYS $alias_key + set -a __ABBR_TIPS_VALUES $alias_value + end + else if test "$command[1]" = functions + # Parse args as `functions` options + argparse --name functions e/erase -- $command + + # Update abbreviations list when removing aliases + if set -q _flag_e + and set -l abb (contains -i -- a__{$argv[2]} $__ABBR_TIPS_KEYS) + set -e __ABBR_TIPS_KEYS[$abb] + set -e __ABBR_TIPS_VALUES[$abb] + end + end + + # Exit in the following cases : + # - abbreviation has been used + # - command is already an abbreviation + # - command not found + # - or it's a function (alias) + if test $__abbr_tips_used = 1 + set -g __abbr_tips_used 0 + return + else if abbr -q "$cmd" + or not type -q "$command[1]" + return + else if string match -q -- "alias $cmd *" (alias) + return + else if test (type -t "$command[1]") = function + and count $ABBR_TIPS_ALIAS_WHITELIST >/dev/null + and not contains "$command[1]" $ABBR_TIPS_ALIAS_WHITELIST + return + end + + set -l abb + if not set abb (contains -i -- "$cmd" $__ABBR_TIPS_VALUES) + for r in $ABBR_TIPS_REGEXES + if set abb (contains -i -- (string replace -r -a -- "$r" '$1' "$cmd") $__ABBR_TIPS_VALUES) + break + end + end + end + + if test -n "$abb" + if string match -q "a__*" -- "$__ABBR_TIPS_KEYS[$abb]" + set -l alias (string sub -s 4 -- "$__ABBR_TIPS_KEYS[$abb]") + if functions -q "$alias" + echo -e (string replace -a '{{ .cmd }}' -- "$__ABBR_TIPS_VALUES[$abb]" \ + (string replace -a '{{ .abbr }}' -- "$alias" "$ABBR_TIPS_PROMPT")) + else + set -e __ABBR_TIPS_KEYS[$abb] + set -e __ABBR_TIPS_VALUES[$abb] + end + else + echo -e (string replace -a '{{ .cmd }}' -- "$__ABBR_TIPS_VALUES[$abb]" \ + (string replace -a '{{ .abbr }}' -- "$__ABBR_TIPS_KEYS[$abb]" "$ABBR_TIPS_PROMPT")) + end + end + + return +end + +function __abbr_tips_update --on-event abbr_tips_update + __abbr_tips_clean + __abbr_tips_install +end + +function __abbr_tips_uninstall --on-event abbr_tips_uninstall + __abbr_tips_clean + functions --erase __abbr_tips_init +end diff --git a/.config/fish/fish_plugins b/.config/fish/fish_plugins index 332b1a83..4f6df3e0 100644 --- a/.config/fish/fish_plugins +++ b/.config/fish/fish_plugins @@ -9,3 +9,4 @@ jorgebucaran/autopair.fish nickeb96/puffer-fish paldepind/projectdo acomagu/fish-async-prompt +gazorby/fish-abbreviation-tips diff --git a/.config/fish/fish_variables b/.config/fish/fish_variables index 53bd6276..219a2d74 100644 --- a/.config/fish/fish_variables +++ b/.config/fish/fish_variables @@ -1,11 +1,16 @@ # This file contains fish universal variable definitions. # VERSION: 3.0 +SETUVAR --export ABBR_TIPS_PROMPT:\x5cn\U0001f4a1\x20\x5ce\x5b1m\x7b\x7b\x20\x2eabbr\x20\x7d\x7d\x5ce\x5b0m\x20\x3d\x3e\x20\x7b\x7b\x20\x2ecmd\x20\x7d\x7d +SETUVAR --export ABBR_TIPS_REGEXES:\x28\x5e\x28\x5cw\x2b\x5cs\x2b\x29\x2b\x28\x2d\x7b1\x2c2\x7d\x29\x5cw\x2b\x29\x28\x5cs\x5cS\x2b\x29\x1e\x28\x5e\x28\x5cs\x3f\x28\x5cw\x2d\x3f\x29\x2b\x29\x7b3\x7d\x29\x2e\x2a\x1e\x28\x5e\x28\x5cs\x3f\x28\x5cw\x2d\x3f\x29\x2b\x29\x7b2\x7d\x29\x2e\x2a\x1e\x28\x5e\x28\x5cs\x3f\x28\x5cw\x2d\x3f\x29\x2b\x29\x7b1\x7d\x29\x2e\x2a SETUVAR EDITOR:hx SETUVAR --export TERM:xterm\x2d256color SETUVAR Z_DATA_DIR:/home/fic/\x2elocal/share/z +SETUVAR --export __ABBR_TIPS_KEYS:a__autoyadm\x1ea__cat\x1ea__dnd\x1ea__h\x1ea__lg\x1ea__ls\x1ea__mailconf\x1ea__rm\x1ea__top\x1ea__yadmadd\x1ea__ylg\x1ea__z\x1ea__zi +SETUVAR --export __ABBR_TIPS_VALUES:AUTOYADMPUSH\x3d1\x20\x7e/dev/autoyadm/autoyadm\x2esh\x1ebat\x1edragon\x2ddrop\x1ehx\x1elazygit\x1eexa\x20\x2d\x2dlong\x20\x2d\x2dgit\x20\x2d\x2dheader\x20\x2d\x2dicons\x1ey\x20\x7e/\x2econfig/mail\x1egomi\x1ebtop\x1e\x7e/dev/autoyadm/yadmadd\x2esh\x1eyadm\x20enter\x20lazygit\x1e__zoxide_z\x1e__zoxide_zi SETUVAR __fish_initialized:3800 SETUVAR _fisher_acomagu_2F_fish_2D_async_2D_prompt_files:\x7e/\x2econfig/fish/conf\x2ed/__async_prompt\x2efish SETUVAR _fisher_franciscolourenco_2F_done_files:\x7e/\x2econfig/fish/conf\x2ed/done\x2efish +SETUVAR _fisher_gazorby_2F_fish_2D_abbreviation_2D_tips_files:\x7e/\x2econfig/fish/functions/__abbr_tips_bind_newline\x2efish\x1e\x7e/\x2econfig/fish/functions/__abbr_tips_bind_space\x2efish\x1e\x7e/\x2econfig/fish/functions/__abbr_tips_clean\x2efish\x1e\x7e/\x2econfig/fish/functions/__abbr_tips_init\x2efish\x1e\x7e/\x2econfig/fish/conf\x2ed/abbr_tips\x2efish SETUVAR _fisher_jorgebucaran_2F_autopair_2E_fish_files:\x7e/\x2econfig/fish/functions/_autopair_backspace\x2efish\x1e\x7e/\x2econfig/fish/functions/_autopair_insert_left\x2efish\x1e\x7e/\x2econfig/fish/functions/_autopair_insert_right\x2efish\x1e\x7e/\x2econfig/fish/functions/_autopair_insert_same\x2efish\x1e\x7e/\x2econfig/fish/functions/_autopair_tab\x2efish\x1e\x7e/\x2econfig/fish/conf\x2ed/autopair\x2efish SETUVAR _fisher_jorgebucaran_2F_fisher_files:\x7e/\x2econfig/fish/functions/fisher\x2efish\x1e\x7e/\x2econfig/fish/completions/fisher\x2efish SETUVAR _fisher_jorgebucaran_2F_replay_2E_fish_files:\x7e/\x2econfig/fish/functions/replay\x2efish\x1e\x7e/\x2econfig/fish/completions/replay\x2efish @@ -15,7 +20,7 @@ SETUVAR _fisher_meaningful_2D_ooo_2F_sponge_files:\x7e/\x2econfig/fish/functions SETUVAR _fisher_nickeb96_2F_puffer_2D_fish_files:\x7e/\x2econfig/fish/functions/_puffer_fish_expand_bang\x2efish\x1e\x7e/\x2econfig/fish/functions/_puffer_fish_expand_dots\x2efish\x1e\x7e/\x2econfig/fish/functions/_puffer_fish_expand_lastarg\x2efish\x1e\x7e/\x2econfig/fish/conf\x2ed/puffer_fish_key_bindings\x2efish SETUVAR _fisher_paldepind_2F_projectdo_files:\x7e/\x2econfig/fish/functions/projectdo_build\x2efish\x1e\x7e/\x2econfig/fish/functions/projectdo_run\x2efish\x1e\x7e/\x2econfig/fish/functions/projectdo_test\x2efish\x1e\x7e/\x2econfig/fish/functions/projectdo_tool\x2efish\x1e\x7e/\x2econfig/fish/completions/projectdo\x2efish SETUVAR _fisher_patrickf1_2F_fzf_2E_fish_files:\x7e/\x2econfig/fish/functions/_fzf_configure_bindings_help\x2efish\x1e\x7e/\x2econfig/fish/functions/_fzf_extract_var_info\x2efish\x1e\x7e/\x2econfig/fish/functions/_fzf_preview_changed_file\x2efish\x1e\x7e/\x2econfig/fish/functions/_fzf_preview_file\x2efish\x1e\x7e/\x2econfig/fish/functions/_fzf_report_diff_type\x2efish\x1e\x7e/\x2econfig/fish/functions/_fzf_report_file_type\x2efish\x1e\x7e/\x2econfig/fish/functions/_fzf_search_directory\x2efish\x1e\x7e/\x2econfig/fish/functions/_fzf_search_git_log\x2efish\x1e\x7e/\x2econfig/fish/functions/_fzf_search_git_status\x2efish\x1e\x7e/\x2econfig/fish/functions/_fzf_search_history\x2efish\x1e\x7e/\x2econfig/fish/functions/_fzf_search_processes\x2efish\x1e\x7e/\x2econfig/fish/functions/_fzf_search_variables\x2efish\x1e\x7e/\x2econfig/fish/functions/_fzf_wrapper\x2efish\x1e\x7e/\x2econfig/fish/functions/fzf_configure_bindings\x2efish\x1e\x7e/\x2econfig/fish/conf\x2ed/fzf\x2efish\x1e\x7e/\x2econfig/fish/completions/fzf_configure_bindings\x2efish -SETUVAR _fisher_plugins:jorgebucaran/fisher\x1epatrickf1/fzf\x2efish\x1efranciscolourenco/done\x1ejorgebucaran/replay\x2efish\x1ejorgebucaran/spark\x2efish\x1ejoseluisq/gitnow\x402\x2e12\x2e0\x1emeaningful\x2dooo/sponge\x1ejorgebucaran/autopair\x2efish\x1enickeb96/puffer\x2dfish\x1epaldepind/projectdo\x1eacomagu/fish\x2dasync\x2dprompt +SETUVAR _fisher_plugins:jorgebucaran/fisher\x1epatrickf1/fzf\x2efish\x1efranciscolourenco/done\x1ejorgebucaran/replay\x2efish\x1ejorgebucaran/spark\x2efish\x1ejoseluisq/gitnow\x402\x2e12\x2e0\x1emeaningful\x2dooo/sponge\x1ejorgebucaran/autopair\x2efish\x1enickeb96/puffer\x2dfish\x1epaldepind/projectdo\x1eacomagu/fish\x2dasync\x2dprompt\x1egazorby/fish\x2dabbreviation\x2dtips SETUVAR _fisher_upgraded_to_4_4:\x1d SETUVAR cac_ip:172\x2e30\x2e90\x2e10 SETUVAR fish_color_autosuggestion:89492A diff --git a/.config/fish/functions/__abbr_tips_bind_newline.fish b/.config/fish/functions/__abbr_tips_bind_newline.fish new file mode 100644 index 00000000..6d91aebd --- /dev/null +++ b/.config/fish/functions/__abbr_tips_bind_newline.fish @@ -0,0 +1,10 @@ +function __abbr_tips_bind_newline + if test $__abbr_tips_used != 1 + if abbr -q -- (string trim -- (commandline)) + set -g __abbr_tips_used 1 + else + set -g __abbr_tips_used 0 + end + end + commandline -f execute +end diff --git a/.config/fish/functions/__abbr_tips_bind_space.fish b/.config/fish/functions/__abbr_tips_bind_space.fish new file mode 100644 index 00000000..e83caf62 --- /dev/null +++ b/.config/fish/functions/__abbr_tips_bind_space.fish @@ -0,0 +1,11 @@ +function __abbr_tips_bind_space + commandline -i " " + if test $__abbr_tips_used != 1 + if abbr -q -- (string trim -- (commandline)) + set -g __abbr_tips_used 1 + else + set -g __abbr_tips_used 0 + end + end + commandline -f expand-abbr +end diff --git a/.config/fish/functions/__abbr_tips_clean.fish b/.config/fish/functions/__abbr_tips_clean.fish new file mode 100644 index 00000000..772bcc6d --- /dev/null +++ b/.config/fish/functions/__abbr_tips_clean.fish @@ -0,0 +1,16 @@ +function __abbr_tips_clean -d "Clean plugin variables and functions" + bind --erase \n + bind --erase \r + bind --erase " " + set --erase __abbr_tips_used + set --erase __abbr_tips_run_once + set --erase __ABBR_TIPS_VALUES + set --erase __ABBR_TIPS_KEYS + set --erase ABBR_TIPS_PROMPT + set --erase ABBR_TIPS_AUTO_UPDATE + set --erase ABBR_TIPS_ALIAS_WHITELIST + set --erase ABBR_TIPS_REGEXES + functions --erase __abbr_tips_bind_newline + functions --erase __abbr_tips_bind_space + functions --erase __abbr_tips +end diff --git a/.config/fish/functions/__abbr_tips_init.fish b/.config/fish/functions/__abbr_tips_init.fish new file mode 100644 index 00000000..9cda53ad --- /dev/null +++ b/.config/fish/functions/__abbr_tips_init.fish @@ -0,0 +1,24 @@ +function __abbr_tips_init -d "Initialize abbreviations variables for fish-abbr-tips" + set -e __ABBR_TIPS_KEYS + set -e __ABBR_TIPS_VALUES + set -Ux __ABBR_TIPS_KEYS + set -Ux __ABBR_TIPS_VALUES + + set -l i 1 + set -l abb (string replace -r '.*-- ' '' -- (abbr -s)) + while test $i -le (count $abb) + set -l current_abb (string split -m1 -- ' ' "$abb[$i]") + set -a __ABBR_TIPS_KEYS "$current_abb[1]" + set -a __ABBR_TIPS_VALUES (string trim -c '\'' -- "$current_abb[2]") + set i (math $i + 1) + end + + set -l i 1 + set -l abb (string replace -r '.*-- ' '' -- (alias -s)) + while test $i -le (count $abb) + set -l current_abb (string split -m2 -- ' ' "$abb[$i]") + set -a __ABBR_TIPS_KEYS "a__$current_abb[2]" + set -a __ABBR_TIPS_VALUES (string trim -c '\'' -- "$current_abb[3]") + set i (math $i + 1) + end +end