" Command used to edit files in various contexts.  The default is vim.
" If you would like to use another vi clone such as Elvis or Vile
" you will need to change this setting.
set vicmd=nvim

" Preview by default
set quickview
set previewoptions=graphicsdelay:50000,hardgraphicsclear

" Don't wrap text in quickview
set nowrap

" This makes vifm perform file operations on its own instead of relying on
" standard utilities like `cp`.  While using `cp` and alike is a more universal
" solution, it's also much slower when processing large amounts of files and
" doesn't support progress measuring.
set syscalls

" Position indicator on the left
set number

" Trash Directory
" The default is to move files that are deleted with dd or :d to
" the trash directory.  If you change this you will not be able to move
" files by deleting them and then using p to put the file in the new location.
" I recommend not changing this until you are familiar with vifm.
" This probably shouldn't be an option.
set trash

" This is how many directories to store in the directory history.
set history=1000

" Automatically resolve symbolic links on l or Enter (only for files).
set nofollowlinks

" With this option turned on you can run partially entered commands with
" unambiguous beginning using :! (e.g. :!Te instead of :!Terminal or :!Te<tab>).
set fastrun

" Natural sort of (version) numbers within text.
set sortnumbers

" Maximum number of changes that can be undone.
set undolevels=100

" Use Vim's format of help file (has highlighting and "hyperlinks").
" If you would rather use a plain text help file set novimhelp.
set vimhelp

" If you would like to run an executable file when you
" press return on the file name set this.
set norunexec

" Selected color scheme
colorscheme custom

" Format for displaying time in file list. For example:
" TIME_STAMP_FORMAT=%m/%d-%H:%M
" See man date or man strftime for details.
set timefmt=%Y-%m-%d\ %H:%M

" Show list of matches on tab completion in command-line mode
set wildmenu

" Display completions in a form of popup with descriptions of the matches
set wildstyle=popup

" Show dotfiles
set dotfiles

" No ../
set dotdirs=treeleafsparent

" Show number of sub-items
set dirsize=nitems

" Display suggestions in normal, visual and view modes for keys, marks and
" registers (at most 5 files).  In other view, when available.
set suggestoptions=normal,visual,view,otherpane,keys,marks,registers

" Ignore case in search patterns unless it contains at least one uppercase letter
set ignorecase
set smartcase

" Case insensitive path completion
set caseoptions=p

" Don't highlight search results automatically
set nohlsearch

" Use increment searching (search while typing)
set incsearch

" Try to leave some space from cursor to upper/lower border in lists
set scrolloff=4

" Don't do too many requests to slow file systems
set slowfs=curlftpfs

" Miller mode
" set millerview
" set milleroptions=lsize:1,csize:3,rpreview:all

" Set custom status line look
set statusline=' %t%[ -> %T%]%= %A %10u:%-7g %5E  %5a %20d '

" Set custom ruler line look
set rulerformat='%2l/%S%[ +%x%]'

" What should be saved automatically between vifm sessions
set vifminfo=state,dhistory,chistory,shistory,phistory,bookmarks,bmarks

" Use ripgrep instead of grep
set grepprg='rg --follow --hidden --color never --column --line-number --no-heading --smart-case %i %a %s'

" Use fd instead of find
set findprg='fd --follow --hidden --exclude .git --color never %p %s'

" ------------------------------------------------------------------------------

" :com[mand][!] command_name action
" The following macros can be used in a command
" %a is replaced with the user arguments.
" %c the current file under the cursor.
" %C the current file under the cursor in the other directory.
" %f the current selected file, or files.
" %F the current selected file, or files in the other directory.
" %b same as %f %F.
" %d the current directory name.
" %D the other window directory name.
" %m run the command in a menu window

command! df df -h %m 2> /dev/null
command! diff nvim -d %f %F
command! gzip tar -cvzf %a %f
command! zstd tar --zstd -cvf %a %f
command! xz bsdtar -cvJf %a %f
command! extract !{{ xdg_dir.bin_home }}/extract.sh %f
command! shred shred -fu %f
command! run !!./%f
command! vgrep nvim "+grep %a"
command! reload :write | restart
command! exif exiftool -all= -overwrite_original %f

" fzf file
command! FZFfile : set noquickview
                \| let $FZF_RES = term('fzf --prompt "File: " --no-multi --preview "{{ xdg_dir.bin_home }}/preview.sh {} \$FZF_PREVIEW_COLUMNS \$FZF_PREVIEW_LINES 2>/dev/null" 2>&0')
                \| if $FZF_RES != ''
                \|     execute 'goto' fnameescape($FZF_RES)
                \| endif
                \| set quickview

" fzf dir
command! FZFdir : set noquickview
               \| let $FZF_RES = term('fd --type d --follow --hidden --exclude .git --color never | fzf --no-multi --prompt "Directory: " --preview "lsd -1FAL --group-dirs first --icon always --color always {} 2>/dev/null" 2> /dev/tty')
               \| if $FZF_RES != ''
               \|     execute 'cd' fnameescape($FZF_RES)
               \| endif
               \| set quickview

" fzf zoxide
command! FZFzoxide : set noquickview
                  \| let $FZF_RES = term('zoxide query --list | fzf --no-multi --prompt "Zoxide: " --no-multi 2>&0')
                  \| if $FZF_RES != ''
                  \|     execute 'cd' fnameescape($FZF_RES)
                  \| endif
                  \| set quickview

" ------------------------------------------------------------------------------

mark h ~/
mark C ~/Code
mark D ~/Downloads
mark P ~/Pictures
mark V ~/Videos
mark U ~/Music
mark f {{ xdg_dir.config_home }}
mark l {{ xdg_dir.data_home }}
mark e /etc
mark u /usr/share
mark o /opt
mark b /boot
mark m /media
mark M /mnt
mark A /run/media
mark v /var
mark t /tmp
mark d /dev
mark s /srv
mark r /

" ------------------------------------------------------------------------------

" PDF, PostScript, djvu, comics, epub
filextype {*.pdf,*.ps,*.eps,*.ps.gz,*.djvu,*.djv,*.epub,*.cbr,*.cbz,*.cbt,*.cb7},<application/pdf,application/postscript,application/epub+zip,image/vnd.djvu,image/x-djvu>
        \ zathura %f %i,

filextype {*.mobi,*.pdb}
        \ ebook-viewer %f %i,
        \ FBReader %f %i,

" Audio
filetype {*.wav,*.mp3,*.flac,*.m4a,*.wma,*.ape,*.ac3,*.og[agx],*.spx,*.opus},<audio/*>
       \ ffplay -hide_banner -nodisp -autoexit %c %s,
       \ mpv --no-video %c %s,

" Video
filextype {*.avi,*.mp4,*.wmv,*.dat,*.3gp,*.ogv,*.mkv,*.mpg,*.mpeg,*.vob,*.fl[icv],*.m2v,*.mov,*.webm,,*.m4v,*.r[am],*.divx,*.as[fx]},<video/*>
        \ mpv %c %i,
        \ ffplay -hide_banner -autoexit %f %i,

" Web
filextype {*.html,*.htm,*.xhtml}
        \ firefox %f %i,
        \ qutebrowser %f %i,
        \ librewolf %f %i,
        \ chromium-browser %f %i,
        \ vieb %f %i,

" Object
filetype {*.o},<application/x-object> nm %f | less

" Man page
filetype {*.[1-8]} man ./%c

" Images
filextype {*.bmp,*.jpg,*.jpeg,*.png,*.webp,*.gif,*.xpm},<image/*>
        \ qimgv %f %i,
        \ vimiv %f %i,
        \ imv %f %i,

filextype *.svg
        \ inkscape %f %i,

" OpenRaster
filextype *.ora
        \ krita %f %i,

" Mindmap
filextype *.vym
        \ vym %f %i,

" MD5
filetype *.md5
       \ md5sum -c %f %S,

" SHA1
filetype *.sha1
       \ sha1sum -c %f %S,

" SHA256
filetype *.sha256
       \ sha256sum -c %f %S,

" SHA512
filetype *.sha512
       \ sha512sum -c %f %S,

" FuseZipMount
filetype {*.zip,*.jar,*.war,*.ear,*.oxt,*.apkg},<application/zip,application/java-archive>
       \ {Mount with fuse-zip}
       \ FUSE_MOUNT|fuse-zip %SOURCE_FILE %DESTINATION_DIR,
       \ {View contents}
       \ tar -tf %f | less,
       \ {Extract here}
       \ tar -vxf %c,

" ArchiveMount
filetype {*.tar,*.tar.bz2,*.tbz2,*.tgz,*.tar.gz,*.tar.xz,*.txz,*.tar.zst,*.tzst},<application/x-tar>
       \ {Mount with archivemount}
       \ FUSE_MOUNT|archivemount %SOURCE_FILE %DESTINATION_DIR,

" Rar2FsMount and rar archives
filetype {*.rar},<application/x-rar>
       \ {Mount with rar2fs}
       \ FUSE_MOUNT|rar2fs %SOURCE_FILE %DESTINATION_DIR,

" IsoMount
filetype {*.iso},<application/x-iso9660-image>
       \ {Mount with fuseiso}
       \ FUSE_MOUNT|fuseiso %SOURCE_FILE %DESTINATION_DIR,

" SshMount
filetype *.ssh
       \ {Mount with sshfs}
       \ FUSE_MOUNT2|sshfs %PARAM %DESTINATION_DIR %FOREGROUND,

" FtpMount
filetype *.ftp
       \ {Mount with curlftpfs}
       \ FUSE_MOUNT2|curlftpfs -o ftp_port=-,,disable_eprt %PARAM %DESTINATION_DIR %FOREGROUND,

" Fuse7z and 7z archives
filetype {*.7z},<application/x-7z-compressed>
       \ {Mount with fuse-7z}
       \ FUSE_MOUNT|fuse-7z %SOURCE_FILE %DESTINATION_DIR,

" Office files
filextype {*.odt,*.doc,*.docx,*.sxw,*.xls,*.xlsx,*.ods,*.odp,*.pptx},
          \<application/vnd.openxmlformats-officedocument.*,
          \application/msword,
          \application/vnd.ms-excel>
        \ libreoffice %f %i,

" TuDu files
filetype *.tudu tudu -f %c

" Qt projects
filextype *.pro qtcreator %f %i

" mbox
filetype *.mbox aerc %f

" ------------------------------------------------------------------------------

{% set preview_cmd = xdg_dir.bin_home ~ '/preview.sh %c:p %pw %ph' -%}
" Overwrite the image previewer
fileviewer <image/vnd.djvu,image/x-djvu> {{ preview_cmd }} 2>/dev/null

" https://www.arewesixelyet.com/
if $TERM != 'alacritty'
    fileviewer <image/*,video/*,application/x-font*,font/*>
                \ {{ preview_cmd }} %pd %N 2>/dev/null
endif

fileviewer <*> {{ preview_cmd }} 2>/dev/null

" ------------------------------------------------------------------------------

" Easier to quit
nnoremap qq :quit!<cr>
nnoremap QQ :quit<cr>
nnoremap <C-q> :quit!<cr>

" Quick way to select all files in the view
nnoremap VV :%select<cr>

" Tag a file and move down to the next one
nmap <space> tj

" Start shell in current directory
nnoremap s :shell<cr>

" Display sorting dialog
nnoremap S :sort<cr>

" Toggle preview, and within preview toggle wrap
nnoremap w :view<cr>
vnoremap w :view<cr>gv
nnoremap W :set wrap!<cr>

" Grow/shrink window by 4 columns at a time
nnoremap < 4<c-w><
nnoremap > 4<c-w>>

" go to project root
nnoremap gr :cd $PWD<cr>

" Yank current directory path into the clipboard
nnoremap yd :!echo %d | wl-copy %i<cr>
" Yank current file path into the clipboard
nnoremap yf :!echo %c:p | wl-copy %i<cr>

" Mappings for faster renaming
nnoremap ci cw<c-a>
nnoremap cc cw<c-u>

" Open editor to edit vifmrc and apply settings after returning to vifm
nnoremap ,c :write | edit $MYVIFMRC | restart<cr>

" Mappings for useful commands
nnoremap E :edit<cr>
nnoremap F :mkdir<space>
nnoremap T :touch<space>

" Tabs
nnoremap <C-t> :tabnew<cr>

" Map for fzf commands
nnoremap ff :FZFfile<cr>
nnoremap fd :FZFdir<cr>
nnoremap fz :FZFzoxide<cr>

" Quickly see the file's metadata
nnoremap I :set noquickview | redraw | !exiftool %f %m<cr>

" Scroll in preview window
nnoremap <PageDown> <C-w>w5j<C-w>w
nnoremap <PageUp>   <C-w>w5k<C-w>w
nnoremap J <C-w>w5j<C-w>w
nnoremap K <C-w>w5k<C-w>w

" ------------------------------------------------------------------------------

" Icons
set classify='󰝰 :dir:/, :exe:,| :fifo:, :reg:, :link:,? :?:, ::../::'
set classify+='=:sock:'
set classify+='-:char:'
set classify+='+:block:'

set classify+=' ::.Xdefaults,,.Xauthority,,.Xresources,,.xinitpurc,,recently-used.xbel,,user-dirs.dirs,,mimeapps.list::'
set classify+=' ::.bash_profile,,.bashrc,,.profile,,.zprofile,,.zshrc,,.zshenv::'
set classify+=' ::license,,copyright,,copying,,LICENSE,,COPYRIGHT,,COPYING::'
set classify+=' ::authors,,AUTHORS,,.mailmap::'
set classify+=' ::VERSION::'
set classify+=' ::.gitignore,,.gitconfig,,.gitattributes,,.gitmodules::'
set classify+=' ::.gitlab-ci.yml::'
set classify+=' ::.editorconfig::'
set classify+='󱂚 ::ansible.cfg::'
set classify+=' ::yarn.lock,,.yarnrc::'
set classify+=' ::pom.xml::'
set classify+='󰟓 ::go.mod::'
set classify+=' ::build.cake::'
set classify+=' ::.bowerrc::'
set classify+=' ::babel.config.json,,.babelrc.json::'
set classify+=' ::favicon.*::'

set classify+=' ::/^Gruntfile\.(coffee|js)$/::'
set classify+=' ::/^(g|G)ulpfile(\.[0-9a-zA-Z]+)?\.[jt]s$/::'
set classify+=' ::/^rollup\.config\.m?js$/::'
set classify+=' ::/^\.?stylelintrc(\.config)?(\.(json|[mc]?js|ya?ml))?$/::'
set classify+=' ::/^\.eslintrc(\.(json|ya?ml|c?js))?$/::'
set classify+=' ::/^Pulumi(\.[0-9a-zA-Z]+)?\.yaml$/::'
set classify+='󰡨 ::/^Dockerfile(\.[0-9a-zA-Z]+)?$/::'
set classify+=' ::/(^Makefile(\.[0-9a-zA-Z]+)?|\.mk)$/::'
set classify+=' ::/(^BUILD|^BUCK|^WORKSPACE|\.(bazel|bzl|star))$/::'

set classify+=' ::/\.(h?ledger|beancount)$/::'
set classify+=' ::/\.lock$/::'
set classify+=' ::/\.(info|[0-8])$/::'
set classify+=' ::/\.(tf(vars)?|hcl)$/::'
set classify+=' ::/\.(g?vim(rc)?|vifm)$/::'
set classify+=' ::/\.(tt[fc]|otf|woff2?)$/::'
set classify+='󱂅 ::/\.log$/::'
set classify+=' ::/\.asm$/::'
set classify+='󰯄 ::/\.kdbx$/::'
set classify+=' ::/\.styl$/::'
set classify+='󰅍 ::/\.(txt|csv)$/::'
set classify+=' ::/\.s[ca]ss$/::'
set classify+='󰌜 ::/\.(c|le)ss$/::'
set classify+=' ::/\.(py[cxdo]?|ipynb)$/::'
set classify+=' ::/\.r(data|ds|da|history)?$/::'
set classify+=' ::/\.php$/::'
set classify+=' ::/\.bicep$/::'
set classify+=' ::/\.zig$/::'
set classify+=' ::/\.(markdown|mk?d|[rR]md)$/::'
set classify+='󰍘 ::/\.a(scii)?doc$/::'
set classify+=' ::/\.[mc]?js$/::'
set classify+=' ::/\.[mc]?ts$/::'
set classify+='󰜈 ::/\.[jt]?sx$/::'
set classify+=' ::/\.svelte$/::'
set classify+='󰡄 ::/\.vue$/::'
set classify+=' ::/\.purs$/::'
set classify+=' ::/\.res$/::'
set classify+=' ::/\.graphql$/::'
set classify+=' ::/\.v$/::'
set classify+='󰌔 ::/\.kk$/::'
set classify+=' ::/\.cr$/::'
set classify+=' ::/\.wasm$/::'
set classify+=' ::/\.(j2|jinja)$/::'
set classify+='󰮄 ::/\.((j|lib)sonnet|k|cue|ncl|dhall)$/::'
set classify+=' ::/\.(json[c5]?|ya?ml|kdl)$/::'
set classify+=' ::/\.toml$/::'
set classify+='󱅧 ::/\.rego$/::'
set classify+=' ::/\.(ejs|slim|xml|xul)$/::'
set classify+=' ::/\.hx(ml)?$/::'
set classify+=' ::/\.x?html?$/::'
set classify+=' ::/\.templ$/::'
set classify+=' ::/\.gd$/::'
set classify+=' ::/\.mustasche$/::'
set classify+=' ::/(^\.?conf|^\.?config|\.(conf|config|ini|rc|cfg))$/::'
set classify+=' ::/\.rss$/::'
set classify+=' ::/\.coffee$/::'
set classify+=' ::/\.twig$/::'
set classify+=' ::/\.(c++|cc|[hc](pp)?|cxx)$/::'
set classify+=' ::/\.cuda$/::'
set classify+=' ::/\.l?hs$/::'
set classify+=' ::/(\.elc?|^\.doomrc)$/::'
set classify+=' ::/\.lisp$/::'
set classify+=' ::/\.scm$/::'
set classify+=' ::/\.nix$/::'
set classify+=' ::/\.nim$/::'
set classify+=' ::/\.hack$/::'
set classify+=' ::/\.org$/::'
set classify+=' ::/\.kt$/::'
set classify+='󰢱 ::/\.lua$/::'
set classify+=' ::/\.fnl$/::'
set classify+=' ::/\.ino$/::'
set classify+=' ::/\.haml$/::'
set classify+=' ::/\.liquid$/::'
set classify+=' ::/\.ls$/::'
set classify+=' ::/\.tex$/::'
set classify+=' ::/\.jl$/::'
set classify+=' ::/\.go$/::'
set classify+=' ::/\.(rs|rlib)$/::'
set classify+=' ::/\.(db|dump|sql(ite)?)$/::'
set classify+=' ::/\.(sln|suo)$/::'
set classify+=' ::/\.exe$/::'
set classify+=' ::/\.(diff|patch|sum|md5|sha[0-9]+)$/::'
set classify+=' ::/\.scala$/::'
set classify+=' ::/\.pug$/::'
set classify+=' ::/\.pp$/::'
set classify+='󰬷 ::/\.(java|jar)$/::'
set classify+=' ::/\.(clj[src]?|edn)$/::'
set classify+=' ::/\.(pl|pm|t)$/::'
set classify+=' ::/\.(mli?)$/::'
set classify+=' ::/\.opam$/::'
set classify+=' ::/\.rb$/::'
set classify+=' ::/\.((fi|ba|z|k|x)?sh|nu|ps1|bat|elv)$/::'
set classify+=' ::/\.dart$/::'
set classify+=' ::/\.elm$/::'
set classify+=' ::/\.swift$/::'
set classify+='󰌛 ::/\.cs$/::'
set classify+=' ::/\.(f#|fs(i|script|x)?)$/::'
set classify+=' ::/\.d$/::'
set classify+=' ::/\.[eh]rl$/::'
set classify+=' ::/\.ex$/::'
set classify+=' ::/\.exs$/::'
set classify+=' ::/\.hex$/::'
set classify+=' ::/\.ai$/::'
set classify+=' ::/\.ps[bd]$/::'
set classify+=' ::/\.(aac|anx|asf|au|axa|flac|m[24]a|midi?|mp3|mpc|og[agx]|ram?|rm|spx|wav|wma|ac3)$/::'
set classify+=' ::/\.(bmp|gif|ico|jxl|jpe?g|png|webp|tga|tiff?|xmb|xcf|xpm|xspf|xwd|cr2|dng|3fr|ar[iw]|bay|cr[w3]|cap|dc[srf]|eip|erf|fff|gpr|iiq|k25|[km]dc|mef|mos|mrw|obm|orf|pef|ptx|pxn|r3d|raf|rw[l2z]|sr[2fw]|x3f)$/::'
set classify+=' ::/\.svgz?$/::'
set classify+=' ::/\.(avi|flv|m[ko]v|mp4|mpe?g|webm)$/::'
set classify+=' ::/\.(epub|pdf|fb2|djvu?)$/::'
set classify+=' ::/\.(7z|apk|bz2|cab|cpio|deb|gem|gz|gzip|lh|lzh|lzma|rar|rpm|tar|tgz|xz|zip|zst)$/::'
set classify+=' ::/\.cb[rz]$/::'
set classify+=' ::/\.(docx?|odt)$/::'
set classify+=' ::/\.(xls(x|mx)?|ods)$/::'
set classify+=' ::/\.(pptx?|odp)$/::'

" special dir icons
set classify+=' ::.git/::/,
             \  ::node_modules/::/,
             \ 󰃨 ::.cache/::/,
             \  ::.config/::/,
             \  ::.local/::/,
             \  ::Android/::/,
             \  ::Books/::/,
             \  ::Code/::/,
             \  ::Desktop/::/,
             \  ::Documents/::/,
             \  ::Downloads/::/,
             \ 󰌳 ::Music/::/,
             \  ::Pictures/::/,
             \  ::Public/::/,
             \  ::Template/::/,
             \ 󰨜 ::Videos/::/'
