diff --git a/config/fish/completions/fisher.fish b/config/fish/completions/fisher.fish new file mode 100644 index 0000000..841a50e --- /dev/null +++ b/config/fish/completions/fisher.fish @@ -0,0 +1 @@ +fisher self-complete diff --git a/config/fish/conf.d/fisher.fish b/config/fish/conf.d/fisher.fish new file mode 100644 index 0000000..b1e84fd --- /dev/null +++ b/config/fish/conf.d/fisher.fish @@ -0,0 +1 @@ +fisher copy-user-key-bindings diff --git a/config/fish/conf.d/fzf.fish b/config/fish/conf.d/fzf.fish new file mode 100644 index 0000000..7ed84a4 --- /dev/null +++ b/config/fish/conf.d/fzf.fish @@ -0,0 +1,9 @@ +set -q FZF_TMUX_HEIGHT; or set -U FZF_TMUX_HEIGHT "40%" +set -q FZF_DEFAULT_OPTS; or set -U FZF_DEFAULT_OPTS "--height $FZF_TMUX_HEIGHT" +set -q FZF_LEGACY_KEYBINDINGS; or set -U FZF_LEGACY_KEYBINDINGS 1 +set -q FZF_PREVIEW_FILE_CMD; or set -U FZF_PREVIEW_FILE_CMD "head -n 10" +set -q FZF_PREVIEW_DIR_CMD; or set -U FZF_PREVIEW_DIR_CMD "ls" + +function fzf_uninstall -e fzf_uninstall + # Erase env vars and __fzf functions here. +end diff --git a/config/fish/conf.d/fzf_key_bindings.fish b/config/fish/conf.d/fzf_key_bindings.fish new file mode 100644 index 0000000..7634dc8 --- /dev/null +++ b/config/fish/conf.d/fzf_key_bindings.fish @@ -0,0 +1,41 @@ +if test "$FZF_LEGACY_KEYBINDINGS" -eq 1 + bind \ct '__fzf_find_file' + bind \cr '__fzf_reverse_isearch' + bind \ec '__fzf_cd' + bind \eC '__fzf_cd --hidden' + bind \cg '__fzf_open' + bind \co '__fzf_open --editor' + + if bind -M insert >/dev/null 2>/dev/null + bind -M insert \ct '__fzf_find_file' + bind -M insert \cr '__fzf_reverse_isearch' + bind -M insert \ec '__fzf_cd' + bind -M insert \eC '__fzf_cd --hidden' + bind -M insert \cg '__fzf_open' + bind -M insert \co '__fzf_open --editor' + end +else + bind \cf '__fzf_find_file' + bind \cr '__fzf_reverse_isearch' + bind \eo '__fzf_cd' + bind \eO '__fzf_cd --hidden' + bind \cg '__fzf_open' + bind \co '__fzf_open --editor' + + if bind -M insert >/dev/null 2>/dev/null + bind -M insert \cf '__fzf_find_file' + bind -M insert \cr '__fzf_reverse_isearch' + bind -M insert \eo '__fzf_cd' + bind -M insert \eO '__fzf_cd --hidden' + bind -M insert \cg '__fzf_open' + bind -M insert \co '__fzf_open --editor' + end +end + +if set -q FZF_COMPLETE + bind \t '__fzf_complete' +end + +function fzf_key_bindings_uninstall -e fzf_key_bindings_uninstall + # Erase key bindings here. +end diff --git a/config/fish/conf.d/omf.fish b/config/fish/conf.d/omf.fish deleted file mode 100644 index 3e0f6d6..0000000 --- a/config/fish/conf.d/omf.fish +++ /dev/null @@ -1,7 +0,0 @@ -# Path to Oh My Fish install. -set -q XDG_DATA_HOME - and set -gx OMF_PATH "$XDG_DATA_HOME/omf" - or set -gx OMF_PATH "$HOME/.local/share/omf" - -# Load Oh My Fish configuration. -source $OMF_PATH/init.fish diff --git a/config/fish/conf.d/z.fish b/config/fish/conf.d/z.fish new file mode 100644 index 0000000..4033eba --- /dev/null +++ b/config/fish/conf.d/z.fish @@ -0,0 +1,44 @@ +if test -z "$Z_DATA" + if test -z "$XDG_DATA_HOME" + set -U Z_DATA_DIR "$HOME/.local/share/z" + else + set -U Z_DATA_DIR "$XDG_DATA_HOME/z" + end + set -U Z_DATA "$Z_DATA_DIR/data" +end + +if test ! -e "$Z_DATA" + if test ! -e "$Z_DATA_DIR" + mkdir -p -m 700 "$Z_DATA_DIR" + end + touch "$Z_DATA" +end + +if test -z "$Z_CMD" + set -U Z_CMD "z" +end + +set -U ZO_CMD "$Z_CMD"o + +if test ! -z $Z_CMD + function $Z_CMD -d "jump around" + __z $argv + end +end + +if test ! -z $ZO_CMD + function $ZO_CMD -d "open target dir" + __z -d $argv + end +end + +if not set -q Z_EXCLUDE + set -U Z_EXCLUDE $HOME +end + +# Setup completions once first +__z_complete + +function __z_on_variable_pwd --on-variable PWD + __z_add +end diff --git a/config/fish/fishd.8c85907056c9 b/config/fish/fishd.8c85907056c9 index 3a35bdd..1f704f9 100644 --- a/config/fish/fishd.8c85907056c9 +++ b/config/fish/fishd.8c85907056c9 @@ -5,6 +5,11 @@ SET FZF_LEGACY_KEYBINDINGS:1 SET FZF_PREVIEW_DIR_CMD:ls SET FZF_PREVIEW_FILE_CMD:head\x20\x2dn\x2010 SET FZF_TMUX_HEIGHT:40\x25 +SET ZO_CMD:zo +SET Z_CMD:z +SET Z_DATA:/Users/colinpowell/\x2elocal/share/z/data +SET Z_DATA_DIR:/Users/colinpowell/\x2elocal/share/z +SET Z_EXCLUDE:/Users/colinpowell SET __fish_init_2_39_8:\x1d SET __fish_init_2_3_0:\x1d SET __tf_func:function\x20__tf_alias\x20\x2dd\x20\x22Correct\x20your\x20previous\x20console\x20command\x22\x3b\x20\x20set\x20\x2dl\x20fucked_up_command\x20\x24history\x5b1\x5d\x3b\x20\x20env\x20TF_SHELL\x3dfish\x20TF_ALIAS\x3d__tf_alias\x20PYTHONIOENCODING\x3dutf\x2d8\x20thefuck\x20\x24fucked_up_command\x20\x7c\x20read\x20\x2dl\x20unfucked_command\x3b\x20\x20if\x20\x5b\x20\x22\x24unfucked_command\x22\x20\x21\x3d\x20\x22\x22\x20\x5d\x3b\x20\x20\x20\x20eval\x20\x24unfucked_command\x3b\x20\x20\x20\x20builtin\x20history\x20delete\x20\x2d\x2dexact\x20\x2d\x2dcase\x2dsensitive\x20\x2d\x2d\x20\x24fucked_up_command\x3b\x20\x20\x20\x20builtin\x20history\x20merge\x20\x5e\x20/dev/null\x3b\x20\x20end\x3bend\x3b diff --git a/config/fish/fishfile b/config/fish/fishfile new file mode 100644 index 0000000..0181bda --- /dev/null +++ b/config/fish/fishfile @@ -0,0 +1,3 @@ +jethrokuan/z +jethrokuan/fzf +simnalamburt/shellder diff --git a/config/fish/functions/__fzf_cd.fish b/config/fish/functions/__fzf_cd.fish new file mode 100644 index 0000000..c79a725 --- /dev/null +++ b/config/fish/functions/__fzf_cd.fish @@ -0,0 +1,49 @@ +function __fzf_cd -d "Change directory" + set -l commandline (__fzf_parse_commandline) + set -l dir $commandline[1] + set -l fzf_query $commandline[2] + + if not type -q argparse + # Fallback for fish shell version < 2.7 + function argparse + functions -e argparse # deletes itself + end + if contains -- --hidden $argv; or contains -- -h $argv + set _flag_hidden "yes" + end + end + + # Fish shell version >= v2.7, use argparse + set -l options "h/hidden" + argparse $options -- $argv + + set -l COMMAND + + set -q FZF_CD_COMMAND + or set -l FZF_CD_COMMAND " + command find -L \$dir -mindepth 1 \\( -path \$dir'*/\\.*' -o -fstype 'sysfs' -o -fstype 'devfs' -o -fstype 'devtmpfs' \\) -prune \ + -o -type d -print 2> /dev/null | sed 's@^\./@@'" + + set -q FZF_CD_WITH_HIDDEN_COMMAND + or set -l FZF_CD_WITH_HIDDEN_COMMAND " + command find -L \$dir \ + \\( -path '*/\\.git*' -o -fstype 'dev' -o -fstype 'proc' \\) -prune \ + -o -type d -print 2> /dev/null | sed 1d | cut -b3-" + + if set -q _flag_hidden + set COMMAND $FZF_CD_WITH_HIDDEN_COMMAND + else + set COMMAND $FZF_CD_COMMAND + end + + eval "$COMMAND | "(__fzfcmd)" +m $FZF_DEFAULT_OPTS $FZF_CD_OPTS --query \"$fzf_query\"" | read -l select + + if not test -z "$select" + builtin cd "$select" + + # Remove last token from commandline. + commandline -t "" + end + + commandline -f repaint +end diff --git a/config/fish/functions/__fzf_complete.fish b/config/fish/functions/__fzf_complete.fish new file mode 100644 index 0000000..cb4fe58 --- /dev/null +++ b/config/fish/functions/__fzf_complete.fish @@ -0,0 +1,162 @@ +## +# Use fzf as fish completion widget. +# +# +# When FZF_COMPLETE variable is set, fzf is used as completion +# widget for the fish shell by binding the TAB key. +# +# FZF_COMPLETE can have some special numeric values: +# +# `set FZF_COMPLETE 0` basic widget accepts with TAB key +# `set FZF_COMPLETE 1` extends 0 with candidate preview window +# `set FZF_COMPLETE 2` same as 1 but TAB walks on candidates +# `set FZF_COMPLETE 3` multi TAB selection, RETURN accepts selected ones. +# +# Any other value of FZF_COMPLETE is given directly as options to fzf. +# +# If you prefer to set more advanced options, take a look at the +# `__fzf_complete_opts` function and override that in your environment. + + +# modified from https://github.com/junegunn/fzf/wiki/Examples-(fish)#completion +function __fzf_complete -d 'fzf completion and print selection back to commandline' + # As of 2.6, fish's "complete" function does not understand + # subcommands. Instead, we use the same hack as __fish_complete_subcommand and + # extract the subcommand manually. + set -l cmd (commandline -co) (commandline -ct) + + switch $cmd[1] + case env sudo + for i in (seq 2 (count $cmd)) + switch $cmd[$i] + case '-*' + case '*=*' + case '*' + set cmd $cmd[$i..-1] + break + end + end + end + + set -l cmd_lastw $cmd[-1] + set cmd (string join -- ' ' $cmd) + + set -l initial_query '' + test -n "$cmd_lastw"; and set initial_query --query="$cmd_lastw" + + set -l complist (complete -C$cmd) + set -l result + + # do nothing if there is nothing to select from + test -z "$complist"; and return + + set -l compwc (echo $complist | wc -w) + if test $compwc -eq 1 + # if there is only one option dont open fzf + set result "$complist" + else + + set -l query + string join -- \n $complist \ + | sort \ + | eval (__fzfcmd) $initial_query --print-query (__fzf_complete_opts) \ + | cut -f1 \ + | while read -l r + # first line is the user entered query + if test -z "$query" + set query $r + # rest of lines are selected candidates + else + set result $result $r + end + end + + # exit if user canceled + if test -z "$query" ;and test -z "$result" + return + end + + # if user accepted but no candidate matches, use the input as result + if test -z "$result" + set result $query + end + end + + set prefix (string sub -s 1 -l 1 -- (commandline -t)) + for i in (seq (count $result)) + set -l r $result[$i] + switch $prefix + case "'" + commandline -t -- (string escape -- $r) + case '"' + if string match '*"*' -- $r >/dev/null + commandline -t -- (string escape -- $r) + else + commandline -t -- '"'$r'"' + end + case '~' + commandline -t -- (string sub -s 2 (string escape -n -- $r)) + case '*' + commandline -t -- (string escape -n -- $r) + end + [ $i -lt (count $result) ]; and commandline -i ' ' + end + + commandline -f repaint +end + +function __fzf_complete_opts_common + echo --cycle --reverse --inline-info +end + +function __fzf_complete_opts_tab_accepts + echo --bind tab:accept,btab:cancel +end + +function __fzf_complete_opts_tab_walks + echo --bind tab:down,btab:up +end + +function __fzf_complete_opts_preview + set -l file (status -f) + echo --with-nth=1 --preview-window=right:wrap --preview="fish\ '$file'\ __fzf_complete_preview\ '{1}'\ '{2..}'" +end + +test "$argv[1]" = "__fzf_complete_preview"; and __fzf_complete_preview $argv[2..3] + +function __fzf_complete_opts_0 -d 'basic single selection with tab accept' + __fzf_complete_opts_common + echo --no-multi + __fzf_complete_opts_tab_accepts +end + +function __fzf_complete_opts_1 -d 'single selection with preview and tab accept' + __fzf_complete_opts_0 + __fzf_complete_opts_preview +end + +function __fzf_complete_opts_2 -d 'single selection with preview and tab walks' + __fzf_complete_opts_1 + __fzf_complete_opts_tab_walks +end + +function __fzf_complete_opts_3 -d 'multi selection with preview' + __fzf_complete_opts_common + echo --multi + __fzf_complete_opts_preview +end + +function __fzf_complete_opts -d 'fzf options for fish tab completion' + switch $FZF_COMPLETE + case 0 + __fzf_complete_opts_0 + case 1 + __fzf_complete_opts_1 + case 2 + __fzf_complete_opts_2 + case 3 + __fzf_complete_opts_3 + case '*' + echo $FZF_COMPLETE + end +end diff --git a/config/fish/functions/__fzf_complete_preview.fish b/config/fish/functions/__fzf_complete_preview.fish new file mode 100644 index 0000000..2305c60 --- /dev/null +++ b/config/fish/functions/__fzf_complete_preview.fish @@ -0,0 +1,29 @@ +function __fzf_complete_preview -d 'generate preview for completion widget. + argv[1] is the currently selected candidate in fzf + argv[2] is a string containing the rest of the output produced by `complete -Ccmd` + ' + + if test "$argv[2]" = "Redefine variable" + # show environment variables current value + set -l evar (echo $argv[1] | cut -d= -f1) + echo $argv[1]$$evar + else + echo $argv[1] + end + + # list directories on preview + if test -d "$argv[1]" + eval $FZF_PREVIEW_DIR_CMD (string escape $argv[1]) + end + + # show ten lines of non-binary files preview + if test -f "$argv[1]"; and grep -qI . "$argv[1]" + eval $FZF_PREVIEW_FILE_CMD (string escape $argv[1]) + end + + # if fish knows about it, let it show info + type -q "$argv[1]" 2>/dev/null; and type -a "$argv[1]" + + # show aditional data + echo $argv[2] +end diff --git a/config/fish/functions/__fzf_find_file.fish b/config/fish/functions/__fzf_find_file.fish new file mode 100644 index 0000000..1900006 --- /dev/null +++ b/config/fish/functions/__fzf_find_file.fish @@ -0,0 +1,29 @@ +function __fzf_find_file -d "List files and folders" + set -l commandline (__fzf_parse_commandline) + set -l dir $commandline[1] + set -l fzf_query $commandline[2] + + set -q FZF_FIND_FILE_COMMAND + or set -l FZF_FIND_FILE_COMMAND " + command find -L \$dir -mindepth 1 \\( -path \$dir'*/\\.*' -o -fstype 'sysfs' -o -fstype 'devfs' -o -fstype 'devtmpfs' \\) -prune \ + -o -type f -print \ + -o -type d -print \ + -o -type l -print 2> /dev/null | sed 's@^\./@@'" + + begin + eval "$FZF_FIND_FILE_COMMAND | "(__fzfcmd) "-m $FZF_DEFAULT_OPTS $FZF_FIND_FILE_OPTS --query \"$fzf_query\"" | while read -l s; set results $results $s; end + end + + if test -z "$results" + commandline -f repaint + return + else + commandline -t "" + end + + for result in $results + commandline -it -- (string escape $result) + commandline -it -- " " + end + commandline -f repaint +end diff --git a/config/fish/functions/__fzf_get_dir.fish b/config/fish/functions/__fzf_get_dir.fish new file mode 100644 index 0000000..a32ac83 --- /dev/null +++ b/config/fish/functions/__fzf_get_dir.fish @@ -0,0 +1,17 @@ +function __fzf_get_dir -d 'Find the longest existing filepath from input string' + set dir $argv + + # Strip all trailing slashes. Ignore if $dir is root dir (/) + if [ (string length $dir) -gt 1 ] + set dir (string replace -r '/*$' '' $dir) + end + + # Iteratively check if dir exists and strip tail end of path + while [ ! -d "$dir" ] + # If path is absolute, this can keep going until ends up at / + # If path is relative, this can keep going until entire input is consumed, dirname returns "." + set dir (dirname "$dir") + end + + echo $dir +end diff --git a/config/fish/functions/__fzf_open.fish b/config/fish/functions/__fzf_open.fish new file mode 100644 index 0000000..c91c093 --- /dev/null +++ b/config/fish/functions/__fzf_open.fish @@ -0,0 +1,63 @@ +function __fzf_open -d "Open files and directories." + function __fzf_open_get_open_cmd -d "Find appropriate open command." + if type -q xdg-open + echo "xdg-open" + else if type -q open + echo "open" + end + end + + set -l commandline (__fzf_parse_commandline) + set -l dir $commandline[1] + set -l fzf_query $commandline[2] + + if not type -q argparse + set created_argparse + function argparse + functions -e argparse # deletes itself + end + if contains -- --editor $argv; or contains -- -e $argv + set _flag_editor "yes" + end + if contains -- --preview $argv; or contains -- -p $argv + set _flag_preview "yes" + end + end + + set -l options "e/editor" "p/preview=?" + argparse $options -- $argv + + set -l preview_cmd + if set -q FZF_ENABLE_OPEN_PREVIEW + set preview_cmd "--preview-window=right:wrap --preview='fish -c \"__fzf_complete_preview {}\"'" + end + + set -q FZF_OPEN_COMMAND + or set -l FZF_OPEN_COMMAND " + command find -L \$dir -mindepth 1 \\( -path \$dir'*/\\.*' -o -fstype 'sysfs' -o -fstype 'devfs' -o -fstype 'devtmpfs' \\) -prune \ + -o -type f -print \ + -o -type d -print \ + -o -type l -print 2> /dev/null | sed 's@^\./@@'" + + eval "$FZF_OPEN_COMMAND | "(__fzfcmd) $preview_cmd "-m $FZF_DEFAULT_OPTS $FZF_OPEN_OPTS --query \"$fzf_query\"" | read -l select + + # set how to open + set -l open_cmd + if set -q _flag_editor + set open_cmd "$EDITOR" + else + set open_cmd (__fzf_open_get_open_cmd) + if test -z "$open_cmd" + echo "Couldn't find appropriate open command to use. Do you have 'xdg-open' or 'open' installed?"; and return 1 + end + end + + set -l open_status 0 + if not test -z "$select" + commandline "$open_cmd \"$select\"" ;and commandline -f execute + set open_status $status + end + + commandline -f repaint + return $open_status +end diff --git a/config/fish/functions/__fzf_parse_commandline.fish b/config/fish/functions/__fzf_parse_commandline.fish new file mode 100644 index 0000000..80bf3eb --- /dev/null +++ b/config/fish/functions/__fzf_parse_commandline.fish @@ -0,0 +1,23 @@ +function __fzf_parse_commandline -d 'Parse the current command line token and return split of existing filepath and rest of token' + # eval is used to do shell expansion on paths + set -l commandline (eval "printf '%s' "(commandline -t)) + + if [ -z $commandline ] + # Default to current directory with no --query + set dir '.' + set fzf_query '' + else + set dir (__fzf_get_dir $commandline) + + if [ "$dir" = "." -a (string sub -l 1 $commandline) != '.' ] + # if $dir is "." but commandline is not a relative path, this means no file path found + set fzf_query $commandline + else + # Also remove trailing slash after dir, to "split" input properly + set fzf_query (string replace -r "^$dir/?" '' "$commandline") + end + end + + echo $dir + echo $fzf_query +end diff --git a/config/fish/functions/__fzf_reverse_isearch.fish b/config/fish/functions/__fzf_reverse_isearch.fish new file mode 100644 index 0000000..71cbb09 --- /dev/null +++ b/config/fish/functions/__fzf_reverse_isearch.fish @@ -0,0 +1,5 @@ +function __fzf_reverse_isearch + history -z | eval (__fzfcmd) --read0 --tiebreak=index --toggle-sort=ctrl-r $FZF_DEFAULT_OPTS $FZF_REVERSE_ISEARCH_OPTS -q '(commandline)' | perl -pe 'chomp if eof' | read -lz result + and commandline -- $result + commandline -f repaint +end diff --git a/config/fish/functions/__fzfcmd.fish b/config/fish/functions/__fzfcmd.fish new file mode 100644 index 0000000..3acdbad --- /dev/null +++ b/config/fish/functions/__fzfcmd.fish @@ -0,0 +1,9 @@ +function __fzfcmd + set -q FZF_TMUX; or set FZF_TMUX 0 + set -q FZF_TMUX_HEIGHT; or set FZF_TMUX_HEIGHT 40% + if [ $FZF_TMUX -eq 1 ] + echo "fzf-tmux -d$FZF_TMUX_HEIGHT" + else + echo "fzf" + end +end diff --git a/config/fish/functions/__z.fish b/config/fish/functions/__z.fish new file mode 100644 index 0000000..0254195 --- /dev/null +++ b/config/fish/functions/__z.fish @@ -0,0 +1,151 @@ +function __z -d "Jump to a recent directory." + function __print_help -d "Print z help." + printf "Usage: $Z_CMD [-celrth] regex1 regex2...\n\n" + printf " -c --clean Removes directories that no longer exist from $Z_DATA\n" + printf " -d --dir Opens matching directory using system file manager.\n" + printf " -e --echo Prints best match, no cd\n" + printf " -l --list List matches and scores, no cd\n" + printf " -p --purge Delete all entries from $Z_DATA\n" + printf " -r --rank Search by rank\n" + printf " -t --recent Search by recency\n" + printf " -x --delete Removes the current directory from $Z_DATA\n" + printf " -h --help Print this help\n\n" + + if type -q fisher + printf "Run `fisher help z` for more information.\n" + end + end + + set -l options "h/help" "c/clean" "e/echo" "l/list" "p/purge" "r/rank" "t/recent" "d/directory" "x/delete" + + argparse $options -- $argv + + if set -q _flag_help + __print_help + return 0 + else if set -q _flag_clean + __z_clean + printf "%s cleaned!\n" $Z_DATA + return 0 + else if set -q _flag_purge + echo > $Z_DATA + printf "%s purged!\n" $Z_DATA + return 0 + else if set -q _flag_delete + sed -i -e "\:^$PWD|.*:d" $Z_DATA + return 0 + end + + set -l typ + + if set -q _flag_rank + set typ "rank" + else if set -q _flag_recent + set typ "recent" + end + + set -l z_script ' + function frecent(rank, time) { + dx = t-time + if( dx < 3600 ) return rank*4 + if( dx < 86400 ) return rank*2 + if( dx < 604800 ) return rank/2 + return rank/4 + } + + function output(matches, best_match, common) { + # list or return the desired directory + if( list ) { + cmd = "sort -nr" + for( x in matches ) { + if( matches[x] ) { + printf "%-10s %s\n", matches[x], x | cmd + } + } + if( common ) { + printf "%-10s %s\n", "common:", common > "/dev/stderr" + } + } else { + if( common ) best_match = common + print best_match + } + } + + function common(matches) { + # find the common root of a list of matches, if it exists + for( x in matches ) { + if( matches[x] && (!short || length(x) < length(short)) ) { + short = x + } + } + if( short == "/" ) return + for( x in matches ) if( matches[x] && index(x, short) != 1 ) { + return + } + return short + } + + BEGIN { + gsub(" ", ".*", q) + hi_rank = ihi_rank = -9999999999 + } + { + if( typ == "rank" ) { + rank = $2 + } else if( typ == "recent" ) { + rank = $3 - t + } else rank = frecent($2, $3) + if( $1 ~ q ) { + matches[$1] = rank + } else if( tolower($1) ~ tolower(q) ) imatches[$1] = rank + if( matches[$1] && matches[$1] > hi_rank ) { + best_match = $1 + hi_rank = matches[$1] + } else if( imatches[$1] && imatches[$1] > ihi_rank ) { + ibest_match = $1 + ihi_rank = imatches[$1] + } + } + + END { + # prefer case sensitive + if( best_match ) { + output(matches, best_match, common(matches)) + } else if( ibest_match ) { + output(imatches, ibest_match, common(imatches)) + } + } + ' + + if set -q _flag_list + # Handle list separately as it can print common path information to stderr + # which cannot be captured from a subcommand. + command awk -v t=(date +%s) -v list="list" -v typ="$typ" -v q="$argv" -F "|" $z_script "$Z_DATA" + else + set target (command awk -v t=(date +%s) -v typ="$typ" -v q="$argv" -F "|" $z_script "$Z_DATA") + + if test "$status" -gt 0 + return + end + + if test -z "$target" + printf "'%s' did not match any results\n" "$argv" + return 1 + end + + if set -q _flag_list + echo "$target" | tr ";" "\n" | sort -nr + return 0 + end + + if set -q _flag_echo + printf "%s\n" "$target" + else if set -q _flag_directory + type -q xdg-open;and xdg-open "$target"; and return $status; + type -q open;and open "$target"; and return $status; + echo "Not sure how to open file manager"; and return 1; + else + pushd "$target" + end + end +end diff --git a/config/fish/functions/__z_add.fish b/config/fish/functions/__z_add.fish new file mode 100644 index 0000000..69d921d --- /dev/null +++ b/config/fish/functions/__z_add.fish @@ -0,0 +1,48 @@ +function __z_add -d "Add PATH to .z file" + for i in $Z_EXCLUDE + if contains -- $PWD $i + return 0 #Path excluded + end + end + + set -l tmpfile (mktemp $Z_DATA.XXXXXX) + + if test -f $tmpfile + command awk -v path="$PWD" -v now=(date +%s) -F "|" ' + BEGIN { + rank[path] = 1 + time[path] = now + } + $2 >= 1 { + if( $1 == path ) { + rank[$1] = $2 + 1 + time[$1] = now + } + else { + rank[$1] = $2 + time[$1] = $3 + } + count += $2 + } + END { + if( count > 1000 ) { + for( i in rank ) print i "|" 0.9*rank[i] "|" time[i] # aging + } + else for( i in rank ) print i "|" rank[i] "|" time[i] + } + ' $Z_DATA 2>/dev/null >$tmpfile + + if test ! -z "$Z_OWNER" + chown $Z_OWNER:(id -ng $Z_OWNER) $tmpfile + end + # + # Don't use redirection here as it can lead to a race condition where $Z_DATA is clobbered. + # Note: There is a still a possible race condition where an old version of $Z_DATA is + # read by one instance of Fish before another instance of Fish writes its copy. + # + command mv $tmpfile $Z_DATA + or command rm $tmpfile + end + + __z_complete +end diff --git a/config/fish/functions/__z_clean.fish b/config/fish/functions/__z_clean.fish new file mode 100644 index 0000000..2b65851 --- /dev/null +++ b/config/fish/functions/__z_clean.fish @@ -0,0 +1,10 @@ +function __z_clean -d "Clean up .z file to remove paths no longer valid" + set -l tmpfile (mktemp $Z_DATA.XXXXXX) + + if test -f $tmpfile + command awk -F "|" 'system("test -d \"" $1 "\"") == 0 { print $0 }' $Z_DATA > $tmpfile + command mv -f $tmpfile $Z_DATA + end + + __z_complete +end diff --git a/config/fish/functions/__z_complete.fish b/config/fish/functions/__z_complete.fish new file mode 100644 index 0000000..ea0ab53 --- /dev/null +++ b/config/fish/functions/__z_complete.fish @@ -0,0 +1,14 @@ +function __z_complete -d "add completions" + set -l __z_marks (string replace -r '\|.*' '' < $Z_DATA | string escape) + + complete -c $Z_CMD -a "$__z_marks" -f + complete -c $ZO_CMD -a "$__z_marks" -f + + complete -c $Z_CMD -s c -l clean -d "Cleans out $Z_DATA" + complete -c $Z_CMD -s e -l echo -d "Prints best match, no cd" + complete -c $Z_CMD -s l -l list -d "List matches, no cd" + complete -c $Z_CMD -s p -l purge -d "Purges $Z_DATA" + complete -c $Z_CMD -s r -l rank -d "Searches by rank, cd" + complete -c $Z_CMD -s t -l recent -d "Searches by recency, cd" + complete -c $Z_CMD -s h -l help -d "Print help" +end diff --git a/config/fish/functions/fish_mode_prompt.fish b/config/fish/functions/fish_mode_prompt.fish new file mode 100644 index 0000000..36b0b94 --- /dev/null +++ b/config/fish/functions/fish_mode_prompt.fish @@ -0,0 +1,15 @@ +function fish_mode_prompt + if test "$fish_key_bindings" = "fish_vi_key_bindings" + or test "$fish_key_bindings" = "fish_hybrid_key_bindings" + switch $fish_bind_mode + case default + prompt_segment blue black "N" + case insert + prompt_segment green black "I" + case replace_one + prompt_segment red black "R" + case visual + prompt_segment magenta black "V" + end + end +end diff --git a/config/fish/functions/fisher.fish b/config/fish/functions/fisher.fish new file mode 100644 index 0000000..12fcdcb --- /dev/null +++ b/config/fish/functions/fisher.fish @@ -0,0 +1,452 @@ +set -g fisher_version 3.1.1 + +type source >/dev/null; or function source; . $argv; end + +switch (command uname) + case Darwin FreeBSD + function _fisher_now -a elapsed + command perl -MTime::HiRes -e 'printf("%.0f\n", (Time::HiRes::time() * 1000) - $ARGV[0])' $elapsed + end + case \* + function _fisher_now -a elapsed + command date "+%s%3N" | command awk -v ELAPSED="$elapsed" '{ sub(/%?3N$/, "000") } $0 -= ELAPSED' + end +end + +function fisher -a cmd -d "fish package manager" + if not command which curl >/dev/null + echo "curl is required to use fisher -- install curl and try again" >&2 + return 1 + end + + test -z "$XDG_CACHE_HOME"; and set XDG_CACHE_HOME ~/.cache + test -z "$XDG_CONFIG_HOME"; and set XDG_CONFIG_HOME ~/.config + + set -g fish_config $XDG_CONFIG_HOME/fish + set -g fisher_cache $XDG_CACHE_HOME/fisher + set -g fisher_config $XDG_CONFIG_HOME/fisher + + test -z "$fisher_path"; and set -g fisher_path $fish_config + + command mkdir -p {$fish_config,$fisher_path}/{functions,completions,conf.d} $fisher_cache + + if test ! -e "$fisher_path/completions/fisher.fish" + echo "fisher self-complete" > $fisher_path/completions/fisher.fish + _fisher_self_complete + end + + if test -e "$fisher_path/conf.d/fisher.fish" + command rm -f $fisher_path/conf.d/fisher.fish + end + + switch "$version" + case \*-\* + case 2\* + echo "fisher copy-user-key-bindings" > $fisher_path/conf.d/fisher.fish + end + + switch "$cmd" + case self-complete + _fisher_self_complete + case copy-user-key-bindings + _fisher_copy_user_key_bindings + case ls + _fisher_ls | command sed "s|$HOME|~|" + case -v {,--}version + _fisher_version (status -f) + case -h {,--}help + _fisher_help + case self-update + _fisher_self_update (status -f); or return + _fisher_self_complete + case self-uninstall + _fisher_self_uninstall + case add rm "" + if test ! -z "$argv" + if not isatty + while read -l i + set argv $argv $i + end + end + end + _fisher_commit $argv; or return + _fisher_self_complete + case \* + echo "error: unknown flag or command \"$cmd\"" >&2 + _fisher_help >&2 + return 1 + end +end + +function _fisher_self_complete + complete -c fisher --erase + complete -xc fisher -n __fish_use_subcommand -a version -d "Show version" + complete -xc fisher -n __fish_use_subcommand -a help -d "Show help" + complete -xc fisher -n __fish_use_subcommand -a self-update -d "Update fisher" + complete -xc fisher -n __fish_use_subcommand -a ls -d "List installed packages" + complete -xc fisher -n __fish_use_subcommand -a rm -d "Remove packages" + complete -xc fisher -n __fish_use_subcommand -a add -d "Add packages" + for pkg in (_fisher_ls) + complete -xc fisher -n "__fish_seen_subcommand_from rm" -a $pkg + end +end + +function _fisher_copy_user_key_bindings + if functions -q fish_user_key_bindings + functions -c fish_user_key_bindings fish_user_key_bindings_copy + end + function fish_user_key_bindings + for file in $fisher_path/conf.d/*_key_bindings.fish + source $file >/dev/null 2>/dev/null + end + if functions -q fish_user_key_bindings_copy + fish_user_key_bindings_copy + end + end +end + +function _fisher_ls + set -l pkgs $fisher_config/*/*/* + for pkg in $pkgs + command readlink $pkg; and continue; or echo $pkg + end | command sed "s|$fisher_config/*||;s|github\.com/||" +end + +function _fisher_version -a file + echo "fisher version $fisher_version $file" | command sed "s|$HOME|~|" +end + +function _fisher_help + echo "usage: " + echo " fisher add add packages" + echo " fisher rm remove packages" + echo " fisher update installed packages" + echo " fisher ls show installed packages" + echo " fisher help show this help" + echo " fisher version show version" + echo " fisher self-update update fisher" + echo " fisher self-uninstall uninstall fisher" + echo + echo "examples:" + echo " fisher add jethrokuan/z rafaelrinaldi/pure" + echo " fisher add gitlab.com/owner/foobar@v2" + echo " fisher add ~/path/to/myfish/pkg" + echo " fisher rm rafaelrinaldi/pure" + echo " fisher ls | fisher rm" + echo " fisher add < bundle" +end + +function _fisher_self_update -a file + set -l url "https://raw.githubusercontent.com/jorgebucaran/fisher/master/fisher.fish" + echo "fetching $url" >&2 + + curl -s "$url?nocache" >$file@ + + set -l next_version (awk 'NR == 1 { print $4; exit }' < $file@) + switch "$next_version" + case "" $fisher_version + command rm -f $file@ + if test -z "$next_version" + echo "cannot update fisher -- are you offline?" >&2 + return 1 + end + echo "fisher is already up-to-date" >&2 + case \* + echo "linking $file" | command sed "s|$HOME|~|" >&2 + command mv -f $file@ $file + source $file + echo "updated fisher to $fisher_version -- hooray!" >&2 + end +end + +function _fisher_self_uninstall + set -l current_pkgs $fisher_config/*/*/* + for path in $fisher_cache (_fisher_pkg_remove_all $current_pkgs) $fisher_config $fisher_path/{functions,completions,conf.d}/fisher.fish $fish_config/fishfile + echo "removing $path" + command rm -rf $path 2>/dev/null + end | command sed "s|$HOME|~|" >&2 + + set -e fisher_cache + set -e fisher_config + set -e fisher_path + set -e fisher_version + + complete -c fisher --erase + functions -e (functions -a | command awk '/^_fisher/') fisher + + echo "done -- see you again!" >&2 +end + +function _fisher_commit + set -l elapsed (_fisher_now) + set -l current_pkgs $fisher_config/*/*/* + set -l removed_pkgs (_fisher_pkg_remove_all $current_pkgs) + command rm -rf $fisher_config + command mkdir -p $fisher_config + + set -l fishfile $fish_config/fishfile + if test ! -e "$fishfile" + command touch $fishfile + echo "created empty fishfile in $fishfile" | command sed "s|$HOME|~|" >&2 + end + printf "%s\n" (_fisher_fishfile_format ( + echo -s $argv\;) ( + echo -s $removed_pkgs\; + ) < $fishfile) > $fishfile + + set -l expected_pkgs (_fisher_fishfile_read < $fishfile) + set -l added_pkgs (_fisher_pkg_fetch_all $expected_pkgs) + set -l updated_pkgs ( + for pkg in $removed_pkgs + set pkg (echo $pkg | command sed "s|$fisher_config/*||") + if contains -- $pkg $added_pkgs + echo $pkg + end + end) + + if test -z "$added_pkgs$updated_pkgs$removed_pkgs$expected_pkgs" + echo "nothing to commit -- try adding some packages" >&2 + return 1 + end + + _fisher_status (count $added_pkgs) (count $updated_pkgs) (count $removed_pkgs) (_fisher_now $elapsed) >&2 +end + +function _fisher_pkg_remove_all + for pkg in $argv + echo $pkg + _fisher_pkg_uninstall $pkg + end +end + +function _fisher_pkg_fetch_all + set -l pkg_jobs + set -l local_pkgs + set -l actual_pkgs + set -l expected_pkgs + + for id in $argv + switch $id + case \~\* /\* + set -l path (echo "$id" | command sed "s|~|$HOME|") + if test -e "$path" + set local_pkgs $local_pkgs $path + else + echo "cannot install \"$id\" -- is this a valid file?" >&2 + end + continue + end + + command awk -v ID=$id -v FS=/ 'BEGIN { + if (split(ID, tmp, /@+|:/) > 2) { + if (tmp[4]) sub("@"tmp[4], "", ID) + print ID "\t" tmp[2]"/"tmp[1]"/"tmp[3] "\t" (tmp[4] ? tmp[4] : "master") + } else { + pkg = split(ID, _, "/") <= 2 ? "github.com/"tmp[1] : tmp[1] + tag = tmp[2] ? tmp[2] : "master" + print (\ + pkg ~ /^github/ ? "https://codeload."pkg"/tar.gz/"tag : \ + pkg ~ /^gitlab/ ? "https://"pkg"/-/archive/"tag"/"tmp[split(pkg, tmp, "/")]"-"tag".tar.gz" : \ + pkg ~ /^bitbucket/ ? "https://"pkg"/get/"tag".tar.gz" : pkg \ + ) "\t" pkg + } + }' | read -l url pkg tag + + if test ! -d "$fisher_config/$pkg" + fish -c " + echo fetching $url >&2 + command mkdir -p \"$fisher_config/$pkg\" + if test ! -z \"$tag\" + command git clone $url \"$fisher_config/$pkg\" --branch $tag --depth 1 2>/dev/null + or echo cannot clone \"$url\" -- is this a valid url\? >&2 + else if command curl -Ss $url 2>&1 | command tar -xzf- -C \"$fisher_config/$pkg\" --strip-components=1 2>/dev/null + command mkdir -p \"$fisher_cache/$pkg\" + command cp -Rf \"$fisher_config/$pkg\" \"$fisher_cache/$pkg/..\" + else if test -d \"$fisher_cache/$pkg\" + echo cannot connect to server -- searching in \"$fisher_cache/$pkg\" | command sed 's|$HOME|~|' >&2 + command cp -Rf \"$fisher_cache/$pkg\" \"$fisher_config/$pkg/..\" + else + command rm -rf \"$fisher_config/$pkg\" + echo cannot install \"$pkg\" -- is this a valid package\? >&2 + end + " >/dev/null & + + set pkg_jobs $pkg_jobs (_fisher_jobs --last) + set expected_pkgs $expected_pkgs "$pkg" + end + end + + if test ! -z "$pkg_jobs" + _fisher_wait $pkg_jobs + for pkg in $expected_pkgs + if test -d "$fisher_config/$pkg" + set actual_pkgs $actual_pkgs $pkg + _fisher_pkg_install $fisher_config/$pkg + end + end + end + + for pkg in $local_pkgs + set -l path local/$USER + set -l name (command basename $pkg) + command mkdir -p $fisher_config/$path + command ln -sf $pkg $fisher_config/$path + set actual_pkgs $actual_pkgs $path/$name + _fisher_pkg_install $fisher_config/$path/$name + end + + if test ! -z "$actual_pkgs" + _fisher_pkg_fetch_all (_fisher_pkg_get_deps $actual_pkgs | command sort --unique) + printf "%s\n" $actual_pkgs + end +end + +function _fisher_pkg_get_deps + for pkg in $argv + set -l path $fisher_config/$pkg + if test ! -d "$path" + echo $pkg + else if test -s "$path/fishfile" + _fisher_pkg_get_deps (_fisher_fishfile_format < $path/fishfile | _fisher_fishfile_read) + end + end +end + +function _fisher_pkg_install -a pkg + set -l name (command basename $pkg) + set -l files $pkg/{functions,completions,conf.d}/**.* $pkg/*.fish + for source in $files + set -l target (command basename $source) + switch $source + case $pkg/conf.d\* + set target $fisher_path/conf.d/$target + case $pkg/completions\* + set target $fisher_path/completions/$target + case $pkg/{functions,}\* + switch $target + case uninstall.fish + continue + case init.fish key_bindings.fish + set target $fisher_path/conf.d/$name\_$target + case \* + set target $fisher_path/functions/$target + end + end + echo "linking $target" | command sed "s|$HOME|~|" >&2 + command cp -f $source $target + switch $target + case \*.fish + source $target >/dev/null 2>/dev/null + end + end +end + +function _fisher_pkg_uninstall -a pkg + set -l name (command basename $pkg) + set -l files $pkg/{conf.d,completions,functions}/**.* $pkg/*.fish + for source in $files + set -l target (command basename $source) + set -l filename (command basename $target .fish) + switch $source + case $pkg/conf.d\* + test "$filename.fish" = "$target"; and emit "$filename"_uninstall + set target conf.d/$target + case $pkg/completions\* + test "$filename.fish" = "$target"; and complete -ec $filename + set target completions/$target + case $pkg/{,functions}\* + test "$filename.fish" = "$target"; and functions -e $filename + switch $target + case uninstall.fish + source $source + continue + case init.fish key_bindings.fish + set target conf.d/$name\_$target + case \* + set target functions/$target + end + end + command rm -f $fisher_path/$target + end + if not functions -q fish_prompt + source "$__fish_datadir$__fish_data_dir/functions/fish_prompt.fish" + end +end + +function _fisher_fishfile_read + command awk -v FS=\# '!/^#/ && NF { print $1 }' +end + +function _fisher_fishfile_format -a pkgs removed_pkgs + command awk -v PWD=$PWD -v HOME=$HOME -v PKGS="$pkgs" -v REMOVED_PKGS="$removed_pkgs" ' + BEGIN { + pkg_count = split(PKGS, pkgs, ";") - 1 + cmd = pkgs[1] + for (i = 2; i <= pkg_count; i++) { + pkg_ids[i - 1] = get_pkg_id( pkgs[i] = normalize(pkgs[i]) ) + } + } { + if (NF) { + $0 = normalize($0) + newln = newln > 0 ? "" : newln + if (/^#/) print newln$0 + else if (!seen_pkgs[(pkg_id = get_pkg_id($0))]++) { + for (i = 1; i < pkg_count; i++) { + if (pkg_ids[i] == pkg_id) { + if (cmd == "rm") next + $0 = pkgs[i + 1] + break + } + } + print newln$0 + } + newln = NF + } else if (newln) newln = "\n"(newln > 0 ? "" : newln) + } + END { + if (cmd == "rm" || pkg_count <= 1) { + split(REMOVED_PKGS, tmp, ";") + for (i in tmp) removed_pkgs[normalize(tmp[i])] = i + for (i in pkg_ids) if (!(pkg_ids[i] in removed_pkgs)) { + print "cannot remove \"" pkg_ids[i] "\" -- package not found" > "/dev/stderr" + } + exit + } + for (i in pkg_ids) if (!seen_pkgs[pkg_ids[i]]) print pkgs[i+1] + } + function normalize(s) { + gsub(/^[ \t]*(https?:\/\/)?(.*github\.com\/)?|[\/ \t]*$/, "", s) + sub(/^\.\//, PWD"/", s) + sub(HOME, "~", s) + return s + } + function get_pkg_id(s) { + return (split(s, tmp, /@+|:/) > 2) ? tmp[2]"/"tmp[1]"/"tmp[3] : tmp[1] + } + ' +end + +function _fisher_status -a added updated removed elapsed + command awk -v ADDED=$added -v UPDATED=$updated -v REMOVED=$removed -v ELAPSED=$elapsed ' + BEGIN { + if (ADDED = ADDED - UPDATED) res = msg(res, "added", ADDED) + if (UPDATED) res = msg(res, "updated", UPDATED) + if (REMOVED = REMOVED - UPDATED) res = msg(res, "removed", REMOVED) + printf((res ? res : "done") " in %.2fs\n", ELAPSED / 1000) + } + function msg(res, str, n) { + return (res ? res ", " : "") str " " n " package" (n > 1 ? "s" : "") + } + ' +end + +function _fisher_jobs + jobs $argv | command awk '/[0-9]+\t/ { print $1 }' +end + +function _fisher_wait + while for job in $argv + contains -- $job (_fisher_jobs); and break + end + end +end diff --git a/config/fish/functions/zd.fish b/config/fish/functions/zd.fish new file mode 100644 index 0000000..6b8661b --- /dev/null +++ b/config/fish/functions/zd.fish @@ -0,0 +1,11 @@ +# Function to filter through recently used directories +function zd --argument-names 'name' + set -l zd_command "command fasd -Rdl $name 2> /dev/null" + fish -c "$zd_command" | __fzfcmd -1 -0 --no-sort -m | read -la select + if test ! (count $select) -eq 0 + cd "$select" + end +end + +# Set above function to even shorter j alias +alias j "zd"