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..4b495cf --- /dev/null +++ b/.config/fish/conf.d/fzf.fish @@ -0,0 +1,13 @@ +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 + set -l _vars (set | command grep -E "^FZF.*\$" | command awk '{print $1;}') + for var in $_vars + echo $var + eval (set -e $var) + end +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..8cdd61e --- /dev/null +++ b/.config/fish/conf.d/fzf_key_bindings.fish @@ -0,0 +1,44 @@ +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 + set -l _bindings (bind -a | sed -En "s/(')?__fzf.*\$//p" | sed 's/bind/bind -e/') + for binding in $_bindings + eval $binding + end +end 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/config.fish b/.config/fish/config.fish index cdb2cae..63e3b8a 100644 --- a/.config/fish/config.fish +++ b/.config/fish/config.fish @@ -1,6 +1,6 @@ -xinput set-prop "Atmel maXTouch Touchpad" 260 {1} 2>/dev/null -xinput set-prop "Atmel maXTouch Touchpad" 262 {0} 2>/dev/null -xinput set-prop "Atmel maXTouch Touchpad" 280 {0.2} 2>/dev/null +xinput set-prop "Atmel maXTouch Touchpad" 259 {1} +xinput set-prop "Atmel maXTouch Touchpad" 261 {0} 2>/dev/null +xinput set-prop "Atmel maXTouch Touchpad" 279 {0.2} alias brightness 'sudo nano /sys/class/backlight/backlight/brightness' function battery set y (cat /sys/class/power_supply/sbs-9-000b/charge_full) @@ -17,9 +17,9 @@ alias add 'git add .config/fish/config.fish .config/pulse/3cfb1915dbe54ca3a92143 sudo rfkill block bluetooth alias pik 'pikaur --nodiff --edit' -function fish_prompt - /usr/bin/powerline-go -error $status -shell bare -modules nix-shell,venv,user,host,ssh,cwd,perms,git,hg,root,vgo -end +#function fish_prompt +# /usr/bin/powerline-go -error $status -shell bare -modules nix-shell,venv,user,host,ssh,cwd,perms,git,hg,root,vgo +#end function ss sleep $argv > /dev/null 2>&1 @@ -39,4 +39,6 @@ function getvol twmnc -t "Volume: $vol" #twmnc --remote activate #notify-send -t 50 'Volume: '$vol'%' -end \ No newline at end of file +end + + diff --git a/.config/fish/fishd.alarm b/.config/fish/fishd.alarm index c8b39a6..b7df891 100644 --- a/.config/fish/fishd.alarm +++ b/.config/fish/fishd.alarm @@ -1,5 +1,15 @@ # This file is automatically generated by the fish. # Do NOT edit it directly, your changes will be overwritten. +SET FZF_DEFAULT_OPTS:\x2d\x2dheight\x2040\x25 +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:/home/cole/\x2elocal/share/z/data +SET Z_DATA_DIR:/home/cole/\x2elocal/share/z +SET Z_EXCLUDE:/home/cole SET __fish_classic_git_prompt_initialized:\x1d SET __fish_init_2_39_8:\x1d SET __fish_init_2_3_0:\x1d diff --git a/.config/fish/fishfile b/.config/fish/fishfile new file mode 100644 index 0000000..785ea1a --- /dev/null +++ b/.config/fish/fishfile @@ -0,0 +1,2 @@ +jethrokuan/z +jethrokuan/fzf 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/fisher.fish b/.config/fish/functions/fisher.fish new file mode 100644 index 0000000..beb531b --- /dev/null +++ b/.config/fish/functions/fisher.fish @@ -0,0 +1,476 @@ +set -g fisher_version 3.2.2 + +function fisher -a cmd -d "fish package manager" + set -q XDG_CACHE_HOME; or set XDG_CACHE_HOME ~/.cache + set -q XDG_CONFIG_HOME; or 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 + + set -q fisher_path; or set -g fisher_path $fish_config + + for path in {$fish_config,$fisher_path}/{functions,completions,conf.d} $fisher_cache + if test ! -d $path + command mkdir -p $path + end + end + + 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 + switch "$version" + case \*-\* + command rm -f $fisher_path/conf.d/fisher.fish + case 2\* + case \* + command rm -f $fisher_path/conf.d/fisher.fish + end + else + switch "$version" + case \*-\* + case 2\* + echo "fisher copy-user-key-bindings" >$fisher_path/conf.d/fisher.fish + end + end + + switch "$cmd" + case self-complete + _fisher_self_complete + case copy-user-key-bindings + _fisher_copy_user_key_bindings + case ls + _fisher_ls | _fisher_fmt + case self-update + _fisher_self_update (status -f) + case self-uninstall + _fisher_self_uninstall + case -v {,--}version + _fisher_version (status -f) + case -h {,--}help + _fisher_help + case "" + _fisher_commit -- $argv + case add rm + if not isatty + while read -l arg + set argv $argv $arg + end + end + + if test (count $argv) = 1 + echo "invalid number of arguments" >&2 + _fisher_help >&2 + return 1 + end + + _fisher_commit $argv + case \* + echo "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 add -d "Add packages" + complete -xc fisher -n __fish_use_subcommand -a rm -d "Remove packages" + complete -xc fisher -n __fish_use_subcommand -a ls -d "List added packages" + complete -xc fisher -n __fish_use_subcommand -a help -d "Show usage help" + complete -xc fisher -n __fish_use_subcommand -a version -d "$fisher_version" + complete -xc fisher -n __fish_use_subcommand -a self-update -d "Update to the latest version" + for pkg in (_fisher_ls | _fisher_fmt) + 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; or echo $pkg + end +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 all packages" + echo " fisher ls List added packages" + echo " fisher help Show this help" + echo " fisher version Show the current version" + echo " fisher self-update Update to the latest version" + echo " fisher self-uninstall Uninstall from your system" + echo + echo "examples:" + echo " fisher add jethrokuan/z rafaelrinaldi/pure" + echo " fisher add gitlab.com/foo/bar@v2" + echo " fisher add ~/path/to/local/pkg" + echo " fisher rm rafaelrinaldi/pure" + 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 + command curl -s "$url?nocache" >$file. + + set -l next_version (command awk 'NR == 1 { print $4 }' < $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 to $fisher_version -- hooray!" >&2 + _fisher_self_complete + end +end + +function _fisher_self_uninstall + for pkg in (_fisher_ls) + _fisher_rm $pkg + end + + for file in $fisher_cache $fisher_config $fisher_path/{functions,completions,conf.d}/fisher.fish $fisher_path/fishfile + echo "removing $file" + command rm -Rf $file 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 -a cmd + set -e argv[1] + set -l elapsed (_fisher_now) + set -l fishfile $fisher_path/fishfile + + if test ! -e "$fishfile" + command touch $fishfile + echo "created new fishfile in $fishfile" | command sed "s|$HOME|~|" >&2 + end + + set -l rm_pkgs (_fisher_ls | _fisher_fmt) + for pkg in (_fisher_ls) + _fisher_rm $pkg + end + command rm -Rf $fisher_config + command mkdir -p $fisher_config + + set -l next_pkgs (_fisher_fmt < $fishfile | _fisher_read $cmd (printf "%s\n" $argv | _fisher_fmt)) + set -l new_pkgs (_fisher_fetch $next_pkgs) + set -l old_pkgs + for pkg in $rm_pkgs + if contains -- $pkg $new_pkgs + set old_pkgs $old_pkgs $pkg + end + end + + if test -z "$new_pkgs$old_pkgs$rm_pkgs$next_pkgs" + echo "nothing to commit -- try adding some packages" >&2 + return 1 + end + + set -l actual_pkgs + if test "$cmd" = "rm" + set actual_pkgs $next_pkgs + else + for pkg in $next_pkgs + if contains -- (echo $pkg | command sed "s|@.*||") $new_pkgs + set actual_pkgs $actual_pkgs $pkg + end + end + end + + _fisher_fmt <$fishfile | _fisher_write $cmd $actual_pkgs >$fishfile. + command mv -f $fishfile. $fishfile + + _fisher_self_complete + + command awk -v N=(count $new_pkgs) -v O=(count $old_pkgs) -v R=(count $rm_pkgs) -v E=(_fisher_now $elapsed) ' + BEGIN { + if (N = N - O) res = msg(res, "added", N) + if (O) res = msg(res, "updated", O) + if (R = R - O) res = msg(res, "removed", R) + printf((res ? res : "done") " in %.2fs\n", E / 1000) + } + function msg(res, str, n) { + return (res ? res ", " : "") str " " n " package" (n > 1 ? "s" : "") + } + ' >&2 +end + +function _fisher_fmt + command sed "s|^[[:space:]]*||;s|^$fisher_config/||;s|^$HOME|~|;s|^\.\/|$PWD/|;s|^github\.com/||;s|^https*://||;s|/*\$||" +end + +function _fisher_read -a cmd + set -e argv[1] + command awk -v FS=\# -v CMD="$cmd" -v ARGS="$argv" ' + BEGIN { + split(ARGS, args, " ") + for (i in args) { + if (!((k = getkey(args[i])) in pkgs)) { + pkgs[k] = args[i] + if (CMD == "add") out[n++] = args[i] + } + } + } + !/^#/ && NF { + if (!file[k = getkey($1)]++ && !(k in pkgs)) out[n++] = $1 + } + END { + for (i = 0; i < n; i++) print out[i] + if (CMD == "rm") { + for (pkg in pkgs) { + if (!(pkg in file)) { + print "cannot remove \"" pkg "\" -- package not found" > "/dev/stderr" + } + } + } + } + function getkey(s) { + return (split(s, a, /@+|:/) > 2) ? a[2]"/"a[1]"/"a[3] : a[1] + } + ' +end + +function _fisher_write -a cmd + set -e argv[1] + command awk -v CMD="$cmd" -v ARGS="$argv" ' + BEGIN { + split(ARGS, args, " ") + for (i in args) pkgs[getkey(args[i])] = args[i] + } + { + if (/^#/ || !NF) print $0 + else { + k = getkey($0) + if (out = pkgs[k] != 0 ? pkgs[k] : CMD != "rm" ? $0 : "") print out + pkgs[k] = 0 + } + } + END { + for (k in pkgs) if (pkgs[k]) print pkgs[k] + } + function getkey(s) { + return (split(s, a, /@+|:/) > 2) ? a[2]"/"a[1]"/"a[3] : a[1] + } + ' +end + +function _fisher_fetch + set -l pkg_jobs + set -l next_pkgs + set -l local_pkgs + set -l actual_pkgs + set -q fisher_user_api_token; and set -l user_info -u $fisher_user_api_token + + for i in $argv + switch $i + case \~\* /\* + set -l path (echo "$i" | command sed "s|~|$HOME|") + if test -e "$path" + set local_pkgs $local_pkgs $path + else + echo "cannot add \"$i\" -- is this a valid file?" >&2 + end + continue + end + + command awk -v NAME=$i -v FS=/ 'BEGIN { + if (split(NAME, tmp, /@+|:/) > 2) { + if (tmp[4]) sub("@"tmp[4], "", NAME) + print NAME "\t" tmp[2]"/"tmp[1]"/"tmp[3] "\t" (tmp[4] ? tmp[4] : "master") + } else { + pkg = split(NAME, _, "/") <= 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 branch + + if test ! -d "$fisher_config/$pkg" + fish -c " + echo fetching $url >&2 + command mkdir -p $fisher_config/$pkg $fisher_cache/(dirname $pkg) + if test ! -z \"$branch\" + command git clone $url $fisher_config/$pkg --branch $branch --depth 1 2>/dev/null + or echo cannot clone \"$url\" -- is this a valid url\? >&2 + else if command curl $user_info -Ss $url 2>&1 | command tar -xzf- -C $fisher_config/$pkg 2>/dev/null + command rm -Rf $fisher_cache/$pkg + command mv -f $fisher_config/$pkg/* $fisher_cache/$pkg + command rm -Rf $fisher_config/$pkg + command cp -Rf {$fisher_cache,$fisher_config}/$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 add \"$pkg\" -- is this a valid package\? >&2 + end + " >/dev/null & + + set pkg_jobs $pkg_jobs (_fisher_jobs --last) + set next_pkgs $next_pkgs "$fisher_config/$pkg" + end + end + + if test ! -z "$pkg_jobs" + _fisher_wait $pkg_jobs + for pkg in $next_pkgs + if test -d "$pkg" + set actual_pkgs $actual_pkgs $pkg + _fisher_add $pkg + end + end + end + + set -l local_path $fisher_config/local/$USER + for src in $local_pkgs + command mkdir -p $local_path + command ln -sf $src $local_path/(command basename $src) + set actual_pkgs $actual_pkgs $src + _fisher_add $src --link + end + + if test ! -z "$actual_pkgs" + _fisher_fetch (_fisher_deps $actual_pkgs | command awk '!seen[$0]++') + printf "%s\n" $actual_pkgs | _fisher_fmt + end +end + +function _fisher_deps + for pkg in $argv + if test ! -d "$pkg" + echo $pkg + else if test -s "$pkg/fishfile" + _fisher_deps (_fisher_fmt < $pkg/fishfile | _fisher_read) + end + end +end + +function _fisher_add -a pkg opts + set -l name (command basename $pkg) + set -l files $pkg/{functions,completions,conf.d}/**.* $pkg/*.fish + for src in $files + set -l target (command basename $src) + switch $src + 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 + if test -z "$opts" + command cp -f $src $target + else + command ln -sf $src $target + end + switch $target + case \*.fish + source $target >/dev/null 2>/dev/null + end + end +end + +function _fisher_rm -a pkg + set -l name (command basename $pkg) + set -l files $pkg/{conf.d,completions,functions}/**.* $pkg/*.fish + for src in $files + set -l target (command basename $src) + set -l filename (command basename $target .fish) + switch $src + 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 $src + 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_jobs + builtin 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 + +function _fisher_now -a elapsed + switch (command uname) + case Darwin FreeBSD + command perl -MTime::HiRes -e 'printf("%.0f\n", (Time::HiRes::time() * 1000) - $ARGV[0])' $elapsed + case \* + command date "+%s%3N" | command awk -v ELAPSED="$elapsed" '{ sub(/%?3N$/, "000") } $0 -= ELAPSED' + end +end diff --git a/.config/fish/functions/transfer.fish b/.config/fish/functions/transfer.fish index afd0524..8659d45 100644 --- a/.config/fish/functions/transfer.fish +++ b/.config/fish/functions/transfer.fish @@ -1,11 +1,45 @@ -function transfer --description 'Upload a file to transfer.sh' - if [ $argv[1] ] - # write to output to tmpfile because of progress bar - set -l tmpfile ( mktemp -t transferXXX ) - curl --progress-bar --upload-file "$argv[1]" https://transfer.sh/(basename $argv[1]) >> $tmpfile - cat $tmpfile - command rm -f $tmpfile - else - echo 'usage: transfer FILE_TO_TRANSFER' +function transfer + if test (count $argv) -eq 0 + echo "No arguments specified. Usage:\necho transfer /tmp/test.md\ncat /tmp/test.md | transfer test.md" + return 1 end + + ## get temporarily filename, output is written to this file show progress can be showed + set tmpfile ( mktemp -t transferXXX ) + + ## upload stdin or file + set file $argv[1] + + #if tty -s; + #then + set basefile (basename "$file" | sed -e 's/[^a-zA-Z0-9._-]/-/g') + + # if [ ! -e $file ]; + # then + # echo "File $file doesn't exists." + # return 1 + # fi + + if test -d $file + # zip directory and transfer + set zipfile ( mktemp -t transferXXX.zip ) + # echo (dirname $file) + #cd (dirname $file) and echo (pwd) + zip -r -q - $file >> $zipfile + curl --progress-bar --upload-file "$zipfile" "https://transfer.sh/$basefile.zip" >> $tmpfile + rm -f $zipfile + else + # transfer file + curl --progress-bar --upload-file "$file" "https://transfer.sh/$basefile" >> $tmpfile + end + #else + # # transfer pipe + # curl --progress-bar --upload-file "-" "https://transfer.sh/$file" >> $tmpfile + #fi + + ## cat output link + cat $tmpfile | tr -d '\n' + + ## cleanup + rm -f $tmpfile end