"##############################################################################
" My config file for the vifm terminal file manager
"##############################################################################
set vicmd=nvim
set syscalls
set trash
set history=100
set nofollowlinks
set sortnumbers
set undolevels=100
set vimhelp
set norunexec
set timefmt=%d/%m/%Y\ %H:%M
" set viewcolumns=*{name}..,6{}.
set viewcolumns=-{name}..,10{}.
set rulerformat="%l/%S%[ +%x%] "
set wildmenu
set wildstyle=popup
set suggestoptions=normal,visual,view,otherpane,keys,marks,registers
set ignorecase
set smartcase
set nohlsearch
set incsearch
set scrolloff=4
set statusline=" %A %10u:%-7g %8s [%8E] %18d   %{&sort} %= %a/%c "
set vifminfo=dhistory,savedirs,chistory,state,tui,shistory,
    \phistory,fhistory,dirstack,registers,bookmarks,bmarks
set slowfs=curlftpfs

source $HOME/.config/vifm/icons.vifm
colorscheme my_material

"##############################################################################
" File associations
"##############################################################################
" Pdf, Djvu -------------------------------------------------------------------
" filextype *.pdf atril %c %i &, apvlv %c, xpdf %c
filetype *.pdf,*.djvu
  \ {View in atril}
  \ atril %f &,
  \ {View in qpdfview}
  \ qpdfview %f &,
  \ {View in zathura}
  \ zathura %f,
  \ {View in apvlv}
  \ apvlv %f
fileviewer *.pdf
  \ vifmimg pdfpreview %px %py %pw %ph %c
  \ %pc
  \ vifmimg clear
  " \ pdftotext -nopgbrk %c -

" PostScript ------------------------------------------------------------------
filetype *.ps,*.eps,*.ps.gz
  \ {View in zathura}
  \ zathura %f,
  \ {View in gv}
  \ gv %c %i &,

" Audio -----------------------------------------------------------------------
filetype *.wav,*.mp3,*.flac,*.m4a,*.wma,*.ape,*.ac3,*.og[agx],*.spx,*.opus
  \ {View using deadbeef}
  \ deadbeef %f %i &,
  \ {View using cmus}
  \ alacritty -e cmus %f &,
  \ {Play using ffplay}
  \ ffplay -nodisp -autoexit %c,
  \ {Play using MPlayer}
  \ mplayer %f %i &
fileviewer *.mp3 mp3info
fileviewer *.flac soxi

" Video -----------------------------------------------------------------------
filetype *.avi,*.mp4,*.wmv,*.dat,*.3gp,*.ogv,*.mkv,*.mpg,*.mpeg,*.vob,
  \*.fl[icv],*.m2v,*.mov,*.webm,*.ts,*.mts,*.m4v,*.r[am],*.qt,*.divx,
  \*.as[fx]
  \ {View using smplayer}
  \ smplayer %f %i &,
  \ {View using ffplay}
  \ ffplay -fs -autoexit %f
fileviewer *.avi,*.mp4,*.wmv,*.dat,*.3gp,*.ogv,*.mkv,*.mpg,*.mpeg,*.vob,
  \*.fl[icv],*.m2v,*.mov,*.webm,*.ts,*.mts,*.m4v,*.r[am],*.qt,*.divx,
  \*.as[fx]
  \ vifmimg videopreview %px %py %pw %ph %c
  \ %pc
  \ vifmimg clear

" Web -------------------------------------------------------------------------
filetype *.html,*.htm
  \ {Open with vim}
  \ alacritty -e vim %c &,
  \ {Open with gvim}
  \ gvim %c &,
  \ {Open with dwb}
  \ dwb %f %i &,
  \ {Open with firefox}
  \ firefox %f &,
  \ {Open with uzbl}
  \ uzbl-browser %f %i &
filetype *.html,*.htm links, lynx

" Object ----------------------------------------------------------------------
filetype *.o nm %f | less

" Man page --------------------------------------------------------------------
filetype *.[1-8] man ./%c
fileviewer *.[1-8] man ./%c | col -b

" Images ----------------------------------------------------------------------
filetype *.bmp,*.jpg,*.jpeg,*.png,*.gif,*.xpm
  \ {View in XnViewMP}
  \ xnviewmp %f %i &,
  \ {View in ristretto}
  \ ristretto %f &,
  \ {View in sxiv}
  \ sxiv -ia %f &,
  \ {View in imv}
  \ imv-x11 -b 1D2330 -d %d &,
  \ {View in feh}
  \ feh %d &,
fileviewer *.bmp,*.jpg,*.jpeg,*.png,*.xpm
  \ vifmimg draw %px %py %pw %ph %c
  \ %pc
  \ vifmimg clear
fileviewer *.gif
  \ vifmimg gifpreview %px %py %pw %ph %c
  \ %pc
  \ vifmimg clear

" OpenRaster ------------------------------------------------------------------
filetype *.ora
  \ {Edit in MyPaint}
  \ mypaint %f,

" Mindmap ---------------------------------------------------------------------
filetype *.vym
  \ {Open with VYM}
  \ vym %f &,

" MD5 -------------------------------------------------------------------------
filetype *.md5
  \ {Check MD5 hash sum}
  \ md5sum -c %f %S,

" SHA1 ------------------------------------------------------------------------
filetype *.sha1
  \ {Check SHA1 hash sum}
  \ sha1sum -c %f %S,

" SHA256 ----------------------------------------------------------------------
filetype *.sha256
  \ {Check SHA256 hash sum}
  \ sha256sum -c %f %S,

" SHA512 ----------------------------------------------------------------------
filetype *.sha512
  \ {Check SHA512 hash sum}
  \ sha512sum -c %f %S,

" GPG signature ---------------------------------------------------------------
filetype *.asc
  \ {Check signature}
  \ !!gpg --verify %c,

" Torrent ---------------------------------------------------------------------
filetype *.torrent tixati %f &
fileviewer *.torrent dumptorrent -v %c

" FuseZipMount ----------------------------------------------------------------
filetype *.zip,*.jar,*.war,*.ear,*.oxt,*.apkg
  \ {View contents}
  \ engrampa %f,
  \ {Mount with fuse-zip}
  \ FUSE_MOUNT|fuse-zip %SOURCE_FILE %DESTINATION_DIR,
  \ {View contents}
  \ zip -sf %c | less,
  \ {Extract here}
  \ tar -xf %c,
fileviewer *.zip,*.jar,*.war,*.ear,*.oxt zip -sf %c

" ArchiveMount ----------------------------------------------------------------
filetype *.tar,*.tar.bz2,*.tbz2,*.tgz,*.tar.gz,*.tar.xz,*.txz
  \ {View contents}
  \ engrampa %f,
  \ {Mount with archivemount}
  \ FUSE_MOUNT|archivemount %SOURCE_FILE %DESTINATION_DIR,
fileviewer *.tgz,*.tar.gz tar -tzf %c
fileviewer *.tar.bz2,*.tbz2 tar -tjf %c
fileviewer *.tar.txz,*.txz xz --list %c
fileviewer *.tar tar -tf %c

" Rar2FsMount and rar archives ------------------------------------------------
filetype *.rar
  \ {View contents}
  \ engrampa %f,
  \ {Mount with rar2fs}
  \ FUSE_MOUNT|rar2fs %SOURCE_FILE %DESTINATION_DIR,
fileviewer *.rar unrar v %c

" Fuse7z and 7z archives ------------------------------------------------------
filetype *.7z
  \ {View contents}
  \ engrampa %f,
  \ {Mount with fuse-7z}
  \ FUSE_MOUNT|fuse-7z %SOURCE_FILE %DESTINATION_DIR,
fileviewer *.7z 7z l %c

" IsoMount --------------------------------------------------------------------
filetype *.iso
  \ {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,

" Office files ----------------------------------------------------------------
filetype *.odt,*.doc,*.docx,*.xls,*.xlsx,*.odp,*.pptx,*.ppt wps %f &, libreoffice %f &
fileviewer *.doc catdoc %c
fileviewer *.docx docx2txt.pl %f -

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

" Qt projects -----------------------------------------------------------------
" filextype *.pro qtcreator %f &
filextype *.pro gvim %f &
filetype *.pro alacritty -e vim %f &

" Directories -----------------------------------------------------------------
filetype */
  \ {View in dolphin}
  \ dolphin %f &,
  \ {View in thunar}
  \ Thunar %f &,
  \ {View in Vifm}
  \ vifm %f &,
  \ {View in Ranger}
  \ ranger %f &,

" Config & exec files ---------------------------------------------------------
filextype *.conf,*rc,*.txt,*.yml,*.json,config,*.fish,*.sh,*.ini,*.css,*.bak,*.backup,*info,*.md,*.dat,*.xml,*.Xresources,*.asm,*.py,*.c,*.cpp,*.h,*.awk,*bash*,*.git*,*.zsh,*zshrc,*.profile,*.log,*.desktop,*.vifm,*.vim,*.rs,*.hs,*.lua,*.org,*.el gvim %f &

filetype *.conf,*rc,*.txt,*.yml,*.json,config,*.fish,*.sh,*.ini,*.css,*.bak,*.backup,*info,*.md,*.dat,*.xml,*.Xresources,*.asm,*.py,*.c,*.cpp,*.h,*.awk,*bash*,*.git*,*.zsh,*zshrc,*.profile,*.log,*.desktop,*.vifm,*.vim,*.rs,*.hs,*.lua,*.org,*.el alacritty -e vim %f &

" Books -----------------------------------------------------------------------
filetype *.fb2,*.epub cr3 %f &

" Fonts -----------------------------------------------------------------------
filetype *.ttf,*.otf gnome-font-viewer %f %i &

" .deb ------------------------------------------------------------------------
filextype *.deb gdebi-gtk %f &, engrampa %f &

"##############################################################################
" Syntax highlighting in preview
"##############################################################################
" Explicitly set highlight type for some extensions ---------------------------
" 256-color terminal
" fileviewer *.[ch],*.[ch]pp highlight -O xterm256 -s dante --syntax c %c
" fileviewer Makefile,Makefile.* highlight -O xterm256 -s dante --syntax make %c
"
" 16-color terminal
" fileviewer *.c,*.h highlight -O ansi -s dante %c
"
" Or leave it for automatic detection -----------------------------------------
"
" fileviewer *[^/] pygmentize -O style=monokai -f console256 -g

" Displaying pictures in terminal ---------------------------------------------
"
" fileviewer *.jpg,*.png shellpic %c

" Open all other files with default system programs (you can also remove all
" :file[x]type commands above to ensure they don't interfere with system-wide
" settings).  By default all unknown files are opened with 'vi[x]cmd'
" uncommenting one of lines below will result in ignoring 'vi[x]cmd' option
" for unknown file types.
" For *nix:
" filetype * xdg-open
" For OS X:
" filetype * open
" For Windows:
" filetype * start, explorer

"##############################################################################
" Marks
"##############################################################################
" :mark mark /full/directory/path [filename]
mark h ~/
mark d ~/Desktop
mark w ~/Downloads
mark b ~/OS/Linux/+backup
mark p ~/Projects
mark c ~/.config
mark s ~/.myScripts
mark l ~/.local/share
mark a ~/.cache

mark M /run/media
mark R /

"##############################################################################
" Commands
"##############################################################################
" Archives -------------------------------------------------------------------
command! zip zip -9 -r %c.zip %f
command! unzip unzip %f
command! tar tar -czvf %c.tar.gz %f
command! untar tar -xzvf %f

" Copy and move (instead of copy and move) WITH menu dialog -------------------
command! cp :yank | winrun , put
command! mv :yank | winrun , put!

command! diff vim -d %f %F
command! run !! ./%f
command! mkcd :mkdir %a | cd %a
command! reload :write | restart

" Toggle detailed/brief information about files -------------------------------
command! togglefilesinfo
    \ : if &viewcolumns == '-{name}..,10{}.'
    \ |   set viewcolumns=*{name}..,10{size}.
    \ |   echo 'Files information: detailed'
    \ | else
    \ |   set viewcolumns='-{name}..,10{}.'
    \ |   echo 'Files information: brief'
    \ | endif

command! togglefilesmoreinfo
    \ : if &viewcolumns == '-{name}..,10{}.'
    " \ |   set viewcolumns=*{name}.,10{size}.,12{perms},10{uname},-7{gname},18{mtime}
    " \ |   set viewcolumns=*{name}..,{ext},10{size}.,12{perms},10{uname},-10{gname},18{mtime}
    \ |   set viewcolumns=*{name}..,10{size}.,18{mtime}
    \ |   echo 'Files information: detailed'
    \ | else
    \ |   set viewcolumns='-{name}..,10{}.'
    \ |   echo 'Files information: brief'
    \ | endif

" Reset sort ------------------------------------------------------------------
command! resetsort
    \ : if &sort != '+name'
    \ |   set sort=+name
    \ |   echo 'Reset sort to +name'
    \ | endif

" Open most used tabs ---------------------------------------------------------
command! openProgrammingDirs
    \ :tabonly
    \ | tabnew
    \ | tabname Rust
    \ | cd ~/Documents/forProgramming/Rust/Books/ ~/Projects/Rust/
    \ | tabnew
    \ | tabname Lua
    \ | cd ~/Documents/forProgramming/Lua/Books/ ~/Projects/Lua/
    \ | tabnew
    \ | tabname Haskell
    \ | cd ~/Documents/forProgramming/Haskell/Books/ ~/Projects/Haskell/
    \ | tabnext 1
    \ | tabclose

command! openBackupDirs
    \ :tabonly
    \ | tabnew
    \ | tabname Backups
    \ | cd ~/OS/Linux/+backup/DropboxDesktop/Desktop/ ~/.config/
    \ | tabnew
    \ | tabname yay
    \ | cd ~/.cache/yay ~/.cache/yay_Backups
    \ | tabnew
    \ | tabname Kernel
    \ | cd ~/OS/Linux/+backup/Drivers/NVidia/GeForce9600GT/WorksOnLinuxKernel419/Kernel419/ /var/cache/pacman/pkg/
    \ | tabnext 1
    \ | tabclose

"##############################################################################
" Mappings
"##############################################################################
" Archives ---------------------------------------------------------------------
nnoremap <wait> ,cz :!$TERMINAL -e zip -9 -r %c.zip %f & <cr>
nnoremap <wait> ,xz :!$TERMINAL -e unzip %f & <cr>
nnoremap <wait> ,ct :!$TERMINAL -e tar -czvf %c.tar.gz %f & <cr>
nnoremap <wait> ,xt :!$TERMINAL -e tar -xzvf %f & <cr>

" Open all images in current directory in sxiv thumbnail mode ------------------
nnoremap sx :!sxiv -t %d & <cr>

" Open selected images in gimp -------------------------------------------------
nnoremap gp :!gimp %f %i & <cr>

" 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

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

" yank current directory path into the clipboard ------------------------------
nnoremap yd :!echo -n %d | xclip -sel clipboard %i<cr>:echo expand('%"d') "is yanked to clipboard"<cr>

" yank current file path into the clipboard -----------------------------------
nnoremap yf :!echo -n %c:p | xclip -sel clipboard %i<cr>:echo expand('%"c:p') "is yanked to clipboard"<cr>

" yank current filename without path into the clipboard -----------------------
nnoremap yn :!echo -n %c | xclip -sel clipboard %i<cr>:echo expand('%"c') "is yanked to clipboard"<cr>

" yank root of current file's name into the clipboard -------------------------
nnoremap yr :!echo -n %c:r | xclip -sel clipboard %i<cr>:echo expand('%"c:r') "is yanked to clipboard"<cr>

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

" Open console in current directory -------------------------------------------
nnoremap <wait> ,t :!$TERMINAL &<cr>

" Reload ----------------------------------------------------------------------
nnoremap <wait> ,r :reload<cr>

" Open bookmarks --------------------------------------------------------------
nnoremap <wait> ,b :bmarks<cr>
nnoremap <wait> ,bt :bmarks<space>

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

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

" Toggle detailed/brief information about files -------------------------------
nnoremap <wait> ,ti :togglefilesinfo<cr>
nnoremap <wait> ,tmi :togglefilesmoreinfo<cr>

" Reset sort to +name ---------------------------------------------------------
nnoremap <wait> ,s :resetsort<cr>

" Mount/Unmount windows disks: /dev/sda1 (C) and /dev/sda2 (D) ----------------
nnoremap <wait> ,mc :!~/.myScripts/winC_mount.sh<cr>
nnoremap <wait> ,md :!~/.myScripts/winD_mount.sh<cr>
nnoremap <wait> ,uc :!~/.myScripts/winC_unmount.sh<cr>
nnoremap <wait> ,ud :!~/.myScripts/winD_unmount.sh<cr>

" Quit ------------------------------------------------------------------------
map <wait> <Esc><Esc> :q!<cr>
" Quit all windows without saving
nnoremap ,,q :qa!<cr>
" Quit all windows with saving
nnoremap ,,x :xa<cr>
" Quit the current windows with saving
nnoremap ,,z :x<cr>

" Tabs ------------------------------------------------------------------------
nnoremap ,tn :tabnew<cr>
nnoremap ,[ :tabprevious<cr>
nnoremap ,] :tabnext<cr>
nnoremap ,n :tabname<space>
nnoremap ,1 1gt
nnoremap ,2 2gt
nnoremap ,3 3gt
nnoremap ,4 4gt
nnoremap ,5 5gt
nnoremap ,6 6gt
nnoremap ,7 7gt
nnoremap ,8 8gt
nnoremap ,9 9gt

" Move focus between panes ----------------------------------------------------
noremap <C-k> <C-w>k
noremap <C-j> <C-w>j
noremap <C-h> <C-w>h
noremap <BS> <C-w>h
noremap <C-l> <C-w>l

" Adjust pane size (left for decreasing, right for increasing) ----------------
nnoremap <left> <C-w><
nnoremap <right> <C-w>>

" Moving cursor in another pane -----------------------------------------------
nnoremap <silent> J <tab>j<tab>
nnoremap <silent> K <tab>k<tab>

" Example of standard two-panel file managers mappings ------------------------
nnoremap <f3> :!$TERMINAL -e bat --paging=always %f<cr>
" nnoremap <f4> :edit<cr>
nnoremap <f4> :!$TERMINAL -e vim %f &<cr>
" nnoremap <f5> :copy<cr>
" nnoremap <f6> :move<cr>
nnoremap <f5> :cp<cr>
nnoremap <f6> :mv<cr>
nnoremap <f7> :mkdir<space>
nnoremap <f10> :q<cr>
" nnoremap <f11> :<cr>
" nnoremap <f12> :<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

" " Try to open several tabs
" tabname Rust
" cd ~/Documents/forProgramming/Rust/Books/ ~/Projects/Rust/
"
" tabnew
" tabname Lua
" cd ~/Documents/forProgramming/Lua/Books/ ~/Projects/Lua/
"
" tabnew
" tabname Haskell
" cd ~/Documents/forProgramming/Haskell/Books/ ~/Projects/Haskell/
"
" " start on the first tab
" tabnext 1

