# Basic Settings
set shell zsh
set shellopts '-euy'
set ifs "\n"
set errorfmt "\033[1;31m%s\033[0m"
set promptfmt "\033[34;1m%d\033[0m\033[1m%f\033[0m%S%F"
set cursorparentfmt "\033[7;90m"
set wrapscroll
set sortby time
set info size:time
set reverse
set incsearch
set hidden
set previewer lfview
set ratios 3:3
set icons
set nohistory
recol

%[ $LF_LEVEL -eq 1 ] || echo "Warning: You're in a nested lf instance!"

# Custom Functions
cmd open ${{
    case $(file --mime-type "$(readlink -f $f)" -b) in
        text/*|application/json) lf -remote "send $id \$vix \$fx";;
        image/svg+xml) display -- $f ;;
	image/*) setsid qimgv $f 2>/dev/null & ;;
	audio/*) mpv $f ;;
	video/*) setsid mpv $f -quiet >/dev/null 2>&1 & ;;
	application/cbz|application/cbr|image/vnd.djvu|application/pdf|application/octet-stream|application/postscript|application/pdf|application/vnd*|application/epub*) setsid -f zathura $fx >/dev/null 2>&1 ;;
        *) for f in $fx; do $OPENER $f > /dev/null 2> /dev/null & done;;
    esac
}}

cmd recol &{{
    w=$(tput cols)
    if [ $w -le 80 ]; then
        lf -remote "send $id set ratios 1:1"
    elif [ $w -le 160 ]; then
        lf -remote "send $id set ratios 2:2"
    else
        lf -remote "send $id set ratios 2:2:4"
    fi
}}

cmd bulkrename ${{
    old="$(mktemp)"
    new="$(mktemp)"
    if [ -n "$fs" ]; then
        fs="$(basename -a $fs)"
    else
        fs="$(ls)"
    fi
    printf '%s\n' "$fs" >"$old"
    printf '%s\n' "$fs" >"$new"
    $EDITOR "$new"
    [ "$(wc -l < "$new")" -ne "$(wc -l < "$old")" ] && exit
    paste "$old" "$new" | while IFS= read -r names; do
        src="$(printf '%s' "$names" | cut -f1)"
        dst="$(printf '%s' "$names" | cut -f2)"
        if [ "$src" = "$dst" ] || [ -e "$dst" ]; then
            continue
        fi
        mv -- "$src" "$dst"
    done
    rm -- "$old" "$new"
    lf -remote "send $id unselect"
}}

cmd paste-rsync &{{
    set -- $(cat ~/.local/share/lf/files)
    mode="$1"
    shift
    case "$mode" in
        copy)
            rsync -av --ignore-existing --progress -- "$@" . |
            stdbuf -i0 -o0 -e0 tr '\r' '\n' |
            while IFS= read -r line; do
                lf -remote "send $id echo $line"
            done
            ;;
        move) mv -n -- "$@" .;;
    esac
    rm ~/.local/share/lf/files
    lf -remote "send clear"
}}

cmd paste-overwrite %{{
    mode=$(head -1 ~/.local/share/lf/files)
    list=$(sed 1d ~/.local/share/lf/files)
    if [ $mode = 'copy' ]; then
        cp -r $list .
    elif [ $mode = 'move' ]; then
        mv $list .
    fi
    lf -remote 'send clear'
}}

cmd fzf_jump ${{
    res="$(find . -maxdepth 3 | sed "s|^\./||" | fzf --reverse --header='Jump to location' | sed 's/\\/\\\\/g;s/"/\\"/g')"
    if [ -n "$res" ]; then
        if [ -d "$res" ]; then
            cmd="cd"
        else
            cmd="select"
        fi
        res="$(printf '%s' "$res")"
        lf -remote "send $id $cmd \"$res\""
    fi
}}

cmd Link %{{
    set -- $(cat ~/.local/share/lf/files)
    mode="$1"
    shift
    if [ "$#" -lt 1 ]; then
        lf -remote "send $id echo no files to link"
        exit 0
    fi
    case "$mode" in
        # symbolically copy mode is indicating a soft link
        copy) ln -sr -t . -- "$@";;
        # while a move mode is indicating a hard link
        move) ln -t . -- "$@";;
    esac
    rm ~/.local/share/lf/files
    lf -remote "send clear"
}}

cmd git_branch ${{
    git branch | fzf | xargs git checkout
    pwd_shell=$(pwd | sed 's/\\/\\\\/g;s/"/\\"/g')
    lf -remote "send $id updir"
    lf -remote "send $id cd \"$pwd_shell\""
}}

# toggle single column view
cmd no_preview :{{
    set nopreview
    set ratios 2
    set info size:time
    map ~ yes_preview
}}

cmd yes_preview :{{
    set ratios 2:2
    set preview
    set info
    map ~ no_preview
}}

cmd yank-dirname $dirname -- "$f" | head -c-1 | xclip -i -selection clipboard
cmd yank-path $printf '%s' "$fx" | xclip -i -selection clipboard
cmd yank-basename $basename -a -- $fx | head -c-1 | xclip -i -selection clipboard

cmd yank-basename-without-extension ${{
    echo "$fx" |
      xargs -r -d '\n' basename -a |
      awk -e '{
        for (i=length($0); i > 0; i--) {
          if (substr($0, i, 1) == ".") {
            if (i == 1) print $0
            else print substr($0, 0, i-1)

            break
          }
        }

        if (i == 0)
          print $0
      }' |
      if [ -n "$fs" ]; then cat; else tr -d '\n'; fi |
      xclip -i -selection clipboard
}}

cmd mkdir $mkdir -p "$(echo $* | tr ' ' '\ ')"

# compress current file or selected files with tar and gunzip
cmd tar ${{
    set -f
    mkdir $1
    cp -r $fx $1
    tar czf $1.tar.gz $1
    rm -rf $1
}}

# compress current file or selected files with zip
cmd zip ${{
    set -f
    mkdir $1
    cp -r $fx $1
    zip -r $1.zip $1
    rm -rf $1
}}

# Bindings
# Remove some defaults
map "'"
map '"'
map m
map d
map p
map y
map c
map r

map <f-2> :rename; cmd-end; cmd-delete-home
map <f-3> cut
map <f-4> copy
map <f-5> reload
map <f-6> push $touch<space>
map <f-7> push :mkdir<space>
map <f-8> delete
map <f-9> paste

map <enter> open
map <delete> delete
map . set hidden!
map ~ no_preview
map | top
map ¿ bottom
map x clear
map dd cut
map yy copy
map yn yank-dirname
map yp yank-path
map yn yank-basename
map yw yank-basename-without-extension
map pp :paste; clear
map po paste-overwrite; clear
map pr paste-rsync; clear
map o &mimeopen $f
map O $mimeopen --ask $f
map cx $chmod +x "$f"
map bg $setbg $f
map du %du -sh $f
map Y %cp -ri $fs .
map D %mv -i $fs .
map zf $file-roller "$f"
map zc $zip -9 -r "$f".cbz $f
map zx $zx "$f"
map zm $zip "$f"
map i rename
map I :rename; cmd-home
map a :rename; cmd-delete-home
map A :rename; cmd-end
map B bulkrename
map L :Link
map f fzf_jump

map Gb :git_branch
map Gp ${{clear; git pull --rebase || true; echo "press ENTER"; read ENTER}}
map Gs ${{clear; git status; echo "press ENTER"; read ENTER}}
map Gl ${{clear; git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit}}
map da $git add $fx

# Movement
map gR cd /
map gE cd /etc
map gU cd /usr
map gM cd /media
map gl cd ~/.local/share
map gb cd ~/.local/bin
map g. cd ~/.config
map gn cd ~/.config/nvim
map gz cd ~/.config/zsh
map gd cd ~/doc
map gm cd ~/mus
map ga cd ~/prg
map gv cd ~/vid
map ge cd ~/etc
map gi cd ~/img
map gc cd /mnt/cell
map gx cd /mnt/vb/graveyard
map gw cd /mnt/wd1tb/catacombs
map gy cd /mnt/240green/crypt
