" vim: filetype=vim :
" 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.
if executable('vim')
    set vicmd=vim
elseif executable('nvim')
    set vicmd=nvim
elseif executable('elvis')
    set vicmd=elvis\ -G\ termcap
elseif executable('vile')
    set vicmd=vile
elseif $EDITOR != ''
    echo 'Note: using `'.$EDITOR.'` as an editor'
    let &vicmd = $EDITOR
endif

" 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

" 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

" What should be saved automatically on restarting vifm.  Drop "savedirs"
" value if you don't want vifm to remember last visited directories for you.
set vifminfo=dhistory,chistory,state,tui,shistory,
            \phistory,fhistory,dirstack,registers,bookmarks,bmarks

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

" Automatically resolve symbolic links on l or Enter.
set nofollowlinks

" 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 Enter, l or Right Arrow, set this.
set norunexec

" List of color schemes to try (picks the first one supported by the terminal)
colorscheme base16-default-dark

" 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

" 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

" 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
if !has('win')
    set slowfs=curlftpfs
endif
set milleroptions=lsize:1,csize:1,rsize:1,rpreview:all

" set custom status line look
set statusline="  Hint: %z%= %A %10u:%-7g %15s %20d  "

"------------------------------------------------------------------------------
" Bookmarks
" ------------------------------------------------------------------------------

" :mark mark /full/directory/path [filename]

mark b ~/.local/bin/
mark h ~/

" ------------------------------------------------------------------------------
" Commands
" ------------------------------------------------------------------------------

" :com[mand][!] command_name action
"
" These are some of the macros that can be used in the action part:
"  %a for user arguments
"  %c for current file under the cursor
"  %C for current file under the cursor of inactive pane
"  %f for selected file(s)
"  %F for selected file(s) of inactive pane
"  %b is the same as %f %F
"  %d for current directory name
"  %D for current directory name of inactive pane
"  %r{x} for list of files in register {x}
"  %m runs the command in a menu window
"  %u uses command's output to build a file list
"  see `:help vifm-macros` and `:help vifm-filename-modifiers` for more

command! df df -h %m 2> /dev/null
command! diff vim -d %f %F
command! zip zip -r %c.zip %f
command! run !! ./%f
command! make !!make %a
command! mkcd :mkdir %a | cd %a
command! vgrep vim "+grep %a"
command! reload :write | restart full

" ------------------------------------------------------------------------------
" File types association
" ------------------------------------------------------------------------------

" :filetype pattern1,pattern2 defaultprogram,program2
" :fileviewer pattern1,pattern2 consoleviewer
"
" The first entry is the default program to be used with a matching file.
" The other programs for the file type can be accessed via :file command.
" The command macros like %f, %F, %d, %D may be used in the commands.
" The %a macro is ignored.  To use a % you must put %%.
" Spaces in an app name must be escaped, for example: QuickTime\ Player.app

" For automated FUSE mounts, you must register an extension with :file[x]type
" in one of the following formats:
"
" :filetype patterns FUSE_MOUNT|mount_cmd %SOURCE_FILE %DESTINATION_DIR
"
" %SOURCE_FILE and %DESTINATION_DIR are filled in at runtime.
"
" Example:
"   :filetype *.zip,*.[jwe]ar FUSE_MOUNT|fuse-zip %SOURCE_FILE %DESTINATION_DIR
"
" :filetype patterns FUSE_MOUNT2|mount_cmd %PARAM %DESTINATION_DIR
"
" %PARAM and %DESTINATION_DIR are filled in at runtime.
"
" Example:
"   :filetype *.ssh FUSE_MOUNT2|sshfs %PARAM %DESTINATION_DIR
"
" %PARAM value is the first line of the matched file, example: root@127.0.0.1:/
"
" You can also add %CLEAR if you want to clear screen before running FUSE
" program.  There is also %FOREGROUND, which is useful for entering passwords.
fileviewer .*/,*/ ls --group-directories-first --classify --color %c
fileviewer {*.pdf},<application/pdf> pdftotext -nopgbrk %c -
filetype {*.pdf},<application/pdf>,<video/*>
       \ xdg-open
fileviewer {*.bmp,*.jpg,*.jpeg,*.png,*.gif,*.xpm},<image/*>
       \ chafa -f sixel --polite on -s %pwx%ph --animate off %c
       \ %pd %N
filetype {*.bmp,*.jpg,*.jpeg,*.png,*.gif,*.xpm},<image/*>
       \ xdg-open

fileviewer *.zip,*.jar,*.war,*.ear,*.oxt tar -tf %f
fileviewer *.tgz,*.tar.gz tar -tzf %c
fileviewer *.tar.bz2,*.tbz2 tar -tjf %c
fileviewer *.tar.xz,*.txz tar -tJf %c
fileviewer *.tar.zst,*.tzst tar -t --zstd -f %c
fileviewer {*.tar},<application/x-tar> tar -tf %c

" rar archives
fileviewer {*.rar},<application/x-rar> unrar v %c

fileviewer {*.7z},<application/x-7z-compressed> 7z l %c
fileviewer <text/*> bat --color always --wrap never --pager never %c -p
fileviewer * echo "Unknown file type..."
" Customize view columns a bit (enable ellipsis for truncated file names)
set viewcolumns=-{name}..,6{}.

" Show vertical border
set fillchars=vborder:

" Filter-out build and temporary files
" filter! {*.lo,*.o,*.d,*.class,*.pyc,*.pyo,.*~}

" ------------------------------------------------------------------------------
" Sample keyboard mappings
" ------------------------------------------------------------------------------

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

" Display sorting dialog
nnoremap S :sort<cr>

" Toggle visibility of preview window
nnoremap w :view<cr>
vnoremap w :view<cr>gv

if $DISPLAY && executable('gvim')
    " Open file in existing instance of gvim
    nnoremap o :!gvim --remote-tab-silent %f<cr>
    " Open file in new instance of gvim
    nnoremap O :!gvim %f<cr>
endif

" Open file in the background using its default program
nnoremap gb :file &<cr>l

" Interaction with system clipboard
if has('win')
    " Yank current directory path to Windows clipboard with forward slashes
    nnoremap yp :!echo %"d:gs!\!/! %i | clip<cr>
    " Yank path to current file to Windows clipboard with forward slashes
    nnoremap yf :!echo %"c:gs!\!/! %i | clip<cr>
elseif $WAYLAND_DISPLAY != ''
    if executable('wl-copy')
        " Yank current directory path into primary and selection clipboards
        nnoremap yd :!echo -n %d | wl-copy %i &&
                    \ echo -n %d | wl-copy -p %i<cr>
        " Yank current file path into into primary and selection clipboards
        nnoremap yf :!echo -n %c:p | wl-copy %i &&
                    \ echo -n %c:p | wl-copy -p %i<cr>
    endif
elseif $DISPLAY != ''
    if executable('xclip')
        " Yank current directory path into the clipboard
        nnoremap yd :!echo %d | xclip %i<cr>
        " Yank current file path into the clipboard
        nnoremap yf :!echo %c:p | xclip %i<cr>
    elseif executable('xsel')
        " Yank current directory path into primary and selection clipboards
        nnoremap yd :!echo -n %d | xsel --input --primary %i &&
                    \ echo -n %d | xsel --clipboard --input %i<cr>
        " Yank current file path into into primary and selection clipboards
        nnoremap yf :!echo -n %c:p | xsel --input --primary %i &&
                    \ echo -n %c:p | xsel --clipboard --input %i<cr>
    endif
endif

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

" As above, but without the file extension
" nnoremap I cW<c-a>
" nnoremap cc cW<c-u>
" nnoremap A cW

" Open console in current directory
if $DISPLAY != '' && executable('xterm')
    nnoremap ,t :!xterm &<cr>
elseif $TERMINAL != ''
    nnoremap ,t :!$TERMINAL &<cr>
endif

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

" Open gvim to edit vifmrc
if $DISPLAY && executable('gvim')
    nnoremap ,C :!gvim --remote-tab-silent $MYVIFMRC &<cr>
endif

" Toggle wrap setting on ,w key
nnoremap ,w :set wrap!<cr>

" Example of standard two-panel file managers mappings
nnoremap <f3> :!less %f<cr>
nnoremap <f4> :edit<cr>
nnoremap <f5> :copy<cr>
nnoremap <f6> :move<cr>
nnoremap <f7> :mkdir<space>
nnoremap <f8> :delete<cr>

" Midnight commander alike mappings
" Open current directory in the other pane
nnoremap <a-i> :sync<cr>
" Open directory under cursor in the other pane
nnoremap <a-o> :sync %c<cr>
" Swap panes
nnoremap <c-u> <c-w>x

" source some plugins
source ~/.config/vifm/devicons.vifm

" ------------------------------------------------------------------------------
" Various customization examples
" ------------------------------------------------------------------------------

" Use ag (the silver searcher) instead of grep
" set grepprg='ag --line-numbers %i %a %s'

" Add additional place to look for executables
" let $PATH = $HOME.'/bin/fuse:'.$PATH

" Block particular shortcut
" nnoremap <left> <nop>

" Export IPC name of current instance as environment variable and use it to
" communicate with the instance later.
"
" It can be used in some shell script that gets run from inside vifm, for
" example, like this:
"     vifm --server-name "$VIFM_SERVER_NAME" --remote +"cd '$PWD'"
"
" let $VIFM_SERVER_NAME = v:servername

" Activate screen/tmux support
" screen!

" ------------------------------------------------------------------------------
" Icon decorations example
" ------------------------------------------------------------------------------

" https://github.com/cirala/vifm_devicons
