# ── Core ─────────────────────────────────────────────────────────────────────
set shell bash
set shellopts '-eu'
set ifs "\n"
set scrolloff 8
set icons true
set period 1
set hiddenfiles ".*:*.aux:*.log:*.bbl:*.bcf:*.blg:*.run.xml"
set cursorpreviewfmt "\033[7m"

# ── UI ───────────────────────────────────────────────────────────────────────
set drawbox true
set ratios 1:2:3
set info size:time
set sortby natural
set dirfirst true

# ── Preview ──────────────────────────────────────────────────────────────────
set previewer ~/.config/lf/preview
set cleaner ~/.config/lf/cleaner

# ── Keybindings ──────────────────────────────────────────────────────────────

# unmap defaults we're replacing
map m
map d
map e
map f

# navigation (vim)
map h updir
map l open
map j down
map k up
map g top
map G bottom
map <c-d> half-down
map <c-u> half-up

# enter key — writes selection path and quits (for tmux-lf-helix script)
map <enter> select-and-quit

# go-to shortcuts
map gh cd ~
map gc cd ~/.config
map gd cd ~/Downloads
map gb cd ~/bin

# file operations
map dd cut
map dD delete
map yy copy
map p  paste
map r  rename
map R  bulk-rename

map .  set hidden!
map <space> toggle

# new file/dir
map mf push %touch<space>
map md push %mkdir<space>-p<space>

# ── Commands ─────────────────────────────────────────────────────────────────

cmd select-and-quit &{{
    if [ -n "$fs" ]; then
        # if files are toggled/selected use those
        echo "$fs" > "$lf_selection_path_file"
    else
        # otherwise use the hovered file
        echo "$f" > "$lf_selection_path_file"
    fi
    lf -remote "send $id quit"
}}

cmd bulk-rename ${{
    old=$(mktemp)
    new=$(mktemp)
    [ -n "$fs" ] && fs=$(ls)
    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)
        [ "$src" = "$dst" ] && continue
        mv -- "$src" "$dst"
    done
    rm "$old" "$new"
    lf -remote "send $id unselect"
}}

cmd open ${{
    case $(file --mime-type -b "$f") in
        text/*|application/json|application/toml|application/yaml)
            $EDITOR "$f" ;;
        *)
            xdg-open "$f" >/dev/null 2>&1 & ;;
    esac
}}
