# interpreter for shell commands
set shell sh

# set '-eu' options for shell commands
# These options are used to have safer shell commands. Option '-e' is used to
# exit on error and option '-u' is used to give error for unset variables.
# Option '-f' disables pathname expansion which can be useful when $f, $fs, and
# $fx variables contain names with '*' or '?' characters. However, this option
# is used selectively within individual commands as it can be limiting at
# times.
set shellopts '-eu'

# set internal field separator (IFS) to "\n" for shell commands
# This is useful to automatically split file names in $fs and $fx properly
# since default file separator used in these variables (i.e. 'filesep' option)
# is newline. You need to consider the values of these options and create your
# commands accordingly.
set ifs "\n"

# leave some space at the top and the bottom of the screen
set scrolloff 5

# Basic Settings
set hidden true
#set drawbox true
set icons true
set ignorecase true
set smartcase true
set mouse
set previewer preview
set preview

cmd open $$OPENER $f

cmd on-cd &{{
	sed -i "\|^$PWD$|d" ${XDG_DATA_HOME:-$HOME/.local/share}/cdhist
	echo "$PWD" >> ${XDG_DATA_HOME:-$HOME/.local/share}/cdhist
}}

cmd extract ${{
	# extract the current file with the right command
	# (xkcd link: https://xkcd.com/1168/)
	set -f
	for f in $fx; do case "$f" in
				*.tar) tar xvf "$f" ;;
				*.tar.bz|*.tar.bz2|*.tbz|*.tbz2) tar xjvf "$f" ;;
				*.tar.gz|*.tgz) tar xzvf "$f" ;;
				*.tar.xz|*.txz) tar xJvf "$f" ;;
				*.gz) gunzip "$f" ;;
				*.zip) unzip "$f" ;;
				*.rar) unrar x "$f" ;;
				*.7z) 7z x "$f" ;;
				*.zst) unzstd "$f" ;;
				*) echo "Unsupported format" >&2; exit 1 ;;
		esac done
}}

cmd link_index %{{
	case "$f" in
		*.html) index="index.html" ;;
		*) echo "Unsupported file"; exit 1 ;;
	esac
	[ -e "$index" ] && [ ! -L "$index" ] && {
		echo "Failed: $index already exists and is not a symbolic link"
		exit
	}
	ln -sf "$f" "$index"
}}

cmd select_eval ${{
	res="$(eval "$@")"
	[ -d "$res" ] &&
		lf -remote "send $id cd \"$res\"" ||
		lf -remote "send $id select \"$res\""
}}

cmd link %{{
    set -- $(cat ~/.local/share/lf/files)
    mode="$1"
    shift
    [ "$#" -lt 1 ] &&
	{ lf -remote "send $id echo no files to link"; exit 0; }
    case "$mode" in
        copy) ln -sr -t . -- "$@";;
        move) ln -t . -- "$@";;
    esac
    rm ~/.local/share/lf/files
    lf -remote "send clear"
}}

# Basic Functions
cmap <tab> cmd-menu-complete
cmap <backtab> cmd-menu-complete-back
map <enter> shell
map <delete> trash
map <c-space> set preview!
map <c-n> push $mkdir<space>""<c-b>
map <a-s> :source ~/.config/lf/lfrc
map <tab> :toggle; down
map <backtab> :toggle; up
map Z :toggle; up
map <c-j> select_eval "fzf --reverse --header-first --header='Jump to location'"
map <backspace> ${{ lf -remote "send $id select \"$(cat ${XDG_DATA_HOME:-$HOME/.local/share}/openhist |
	fzf --tac --reverse --header-first --header='File History')\"" }}
map <c-k> ${{ lf -remote "send $id cd \"$(cat ${XDG_DATA_HOME:-$HOME/.local/share}/cdhist |
	fzf --tac --reverse --header-first --header='Path History')\"" }}
map <c-c> ${{ lf -remote "send $id cd \"$(find -mindepth 1 -type d |
	fzf --reverse --header-first --header='Jump to directory')\"" }}

map c
map d
map I :rename; cmd-home
map i :rename
map a :rename; cmd-right
map A :rename; cmd-end
map S push A<c-u>
map o $LESSOPEN="|preview %s" less $f
map O $mimeopen --ask $f
map . set hidden!
map x cut
map M $lf -remote "send $id select \"$(fmd)\""
map D delete
map W $setsid -f $TERMINAL >/dev/null 2>&1
map X !$f
map T !dev test $f
map b $vidir
map V push :!nvim<space>""<c-b>
map P :link

map ge extract $f
map gl clear
map gx ${{ chmod u+x $fx; lf -remote "send $id reload"; }}
map gX ${{ chmod -x $fx; lf -remote "send $id reload"; }}
map gm push $chmod<space>--<space>$fx<c-a><a-f><space>
map gL %lf -remote "send ${id} select '$(readlink $f)'"
map gd !du -hd1
map gf !file $f
map gy %{{ printf "%s" "$f" | xsel -b }}
map gY %{{ printf "%s" "$fs" | xsel -b }}
map gi link_index
map gsh $setbg $f
map gsl $setbg -l $f
map zpp set previewer preview
map zpc set previewer cat
map zpb set previewer preview-bat
map zph set previewer preview-highlight

# Trash Mappings
map dd  ${{ echo "$fx" | xargs -I{} trash-put "{}" }}
map dtc $trash-empty
map dtr $trash-restore
# Dragon Mapping
map dr %dragon-drop -a -x $fx
map ds %dragon-drop -a $fx
map di %dragon-drop $fx
map dm %cpdragon
map dc %mvdragon
map dl %dlfile

source ~/.config/lf/shortcutrc