# ===================================================================
# == Options
# ===================================================================
set viewmode miller
set column_ratios 1,2,4

set scroll_offset 8
set show_cursor false
set mouse_enabled true
set confirm_on_delete always
set colorscheme default

set shorten_title 4
set update_title true
# set update_tmux_title true
set tilde_in_titlebar true
set hostname_in_titlebar true
set max_history_size 20
set max_console_history_size 50
set save_console_history true

set open_all_images false
set preview_images false
set use_preview_script true
set preview_images_method w3m
set preview_script ~/.config/ranger/scope.sh

set automatically_count_files true
set preview_max_size 0
set preview_files true
set preview_directories true
set collapse_preview true

set unicode_ellipsis true
set padding_right false
set draw_borders true
set dirname_in_tabs true
set status_bar_on_top false
set draw_progress_bar_in_status_bar true

set display_size_in_main_column true
set display_size_in_status_bar false
set display_tags_in_all_columns true
set flushinput true
set autosave_bookmarks true
set autoupdate_cumulative_size false

# Enable this if key combinations with the Alt Key don't work for you. (Especially on xterm)
set xterm_alt_key false

set sort natural
set sort_reverse false
set sort_unicode false
set sort_case_insensitive true
set sort_directories_first true

set vcs_aware true
set vcs_backend_git enabled
set vcs_backend_hg disabled
set vcs_backend_bzr disabled
set vcs_backend_svn disabled

set cd_bookmarks true
set cd_tab_case sensitive
set cd_tab_fuzzy true
set show_selection_in_titlebar true

set idle_delay 2000
set metadata_deep_search false
set clear_filters_on_dir_change true
set line_numbers false
set one_indexed false
set save_tabs_on_exit true
set wrap_scroll false
set global_inode_type_filter
set show_hidden true
set show_hidden_bookmarks true
set hidden_filter ^\.|\.(?:pyc|pyo|bak|old|swp)$|^lost\+found$|^__(py)?cache__$

# ===================================================================
# == Local Options
# ===================================================================
# You can set local options that only affect a single directory.

# Examples:
# setlocal path=~/downloads sort mtime


# ===================================================================
# == Command Aliases in the Console
# ===================================================================

# aliases
alias e     edit
alias q     quit
alias q!    quit!
alias qa    quitall
alias qa!   quitall!
alias qall  quitall
alias qall! quitall!
alias setl  setlocal

alias filter     scout -prt
alias find       scout -aeit
alias mark       scout -mr
alias unmark     scout -Mr
alias search     scout -rs
alias search_inc scout -rts
alias travel     scout -aefklst

# ===================================================================
# == Define keys for the browser
# ===================================================================

# Basic
map Q quitall
map q quit
copymap q ZZ ZQ

map R reload_cwd
map F set freeze_files!
map <C-r> reset
map <C-l> redraw_window
map <C-c> abort
map <Esc> change_mode normal
map ~ set viewmode!

map i display_file
map ? help
map W display_log
map w taskview_open
map S shell $SHELL

map : console
map ; console
map ! console shell%space
map @ console -p6 shell  %%s
map # console shell -p%space
map s console shell%space
map f console find%space
map r chain draw_possible_programs; console open_with%%space
map cd console cd%space

# Tagging / Marking
map t tag_toggle
map ut tag_remove
map "<any> tag_toggle tag=%any
map <Space> mark_files toggle=True
map v toggle_visual_mode
map uv toggle_visual_mode reverse=True
map V mark_files all=True toggle=True
map uV mark_files all=True val=False

# For the nostalgics: Midnight Commander bindings
map <F1> help
map <F2> rename_append
map <F3> display_file
map <F4> edit
map <F5> copy
map <F6> cut
map <F7> console mkdir%space
map <F8> console delete
map <F10> exit

# In case you work on a keyboard with dvorak layout
map <UP>       move up=1
map <DOWN>     move down=1
map <LEFT>     move left=1
map <RIGHT>    move right=1
map <HOME>     move to=0
map <END>      move to=-1
map <PAGEDOWN> move down=1   pages=True
map <PAGEUP>   move up=1     pages=True
map <CR>       move right=1
map <DELETE>   console delete
map <INSERT>   console touch%space

# VIM like
copymap <Up> k
copymap <Down> j
copymap <Left> h
copymap <Right> l
copymap <Home> gg
copymap <End> G
copymap <PAGEDOWN> <C-F>
copymap <PAGEUP>   <C-B>
map J move down=0.5 pages=True
map K move up=0.5 pages=True

# Jumping around
map L history_go 1
map H history_go -1
map ] move_parent 1
map [ move_parent -1
map } traverse
map ) jump_non

map E edit

# Better file renaming
map a rename_append
map cw eval fm.execute_console("bulkrename") if fm.thisdir.marked_items else fm.execute_console("rename_append")
map A eval fm.open_console('rename ' + fm.thisfile.relative_path.replace("%", "%%"))
map I eval fm.open_console('rename ' + fm.thisfile.relative_path.replace("%", "%%"), position=7)

# Open file in a new tmux pane
map eh eval import shlex; fm.run("tmux splitw -h rifle " + shlex.quote(fm.thisfile.basename))
map ev eval import shlex; fm.run("tmux splitw -v rifle " + shlex.quote(fm.thisfile.basename))
map ew eval import shlex; fm.run("tmux new-window rifle " + shlex.quote(fm.thisfile.basename))

map pp paste
map phl paste_hardlink
map pP paste append=True
map pr shell sudo cp %c %d
map po paste overwrite=True
map pht paste_hardlinked_subtree
map pL paste_symlink relative=True
map pl paste_symlink relative=False
map pO paste overwrite=True append=True

map dd cut
map ud uncut
map da cut mode=add
map dr cut mode=remove
map dt cut mode=toggle
map dc get_cumulative_size
map dD console delete
map DD shell rm -rf %s

# External Programs
map E  edit
map du shell -p du --max-depth=1 -h --apparent-size
map dU shell -p du --max-depth=1 -h --apparent-size | sort -rh
map yp yank path
map yd yank dir
map yn yank name

map yy copy
map uy uncut
map ya copy mode=add
map yr copy mode=remove
map yt copy mode=toggle
map yd shell -f echo -n %d | xsel -i; xsel -o | xsel -i -b
map yn shell -f echo -n %f | xsel -i; xsel -o | xsel -i -b
map yp shell -f echo -n %d/%f | xsel -i; xsel -o | xsel -i -b

# Searching
map n search_next
map / console search%space
map f console scout -ftsea%space

# Sorting
map or set sort_reverse!
map os chain set sort=size; set sort_reverse=False
map ob chain set sort=basename; set sort_reverse=False
map on chain set sort=natural; set sort_reverse=False
map ot chain set sort=type; set sort_reverse=False
map oe chain set sort=extension; set sort_reverse=False

# Settings
map zi set preview_images!
map zp set preview_files!
map zP set preview_directories!
map zv set use_preview_script!
map zd set sort_directories_first!
map zc set collapse_preview!
map zI set flushinput!
map zm set mouse_enabled!
map zh set show_hidden!
map zf console filter%space

# Bookmarks
map `<any>  enter_bookmark %any
map '<any>  enter_bookmark %any
map \<any>  set_bookmark %any
map u\<any> unset_bookmark %any
map \<bg>   draw_bookmarks
copymap \<bg> u\<bg> `<bg> '<bg>

# Tabs
map tq tab_close
map tr tab_restore
map <TAB> tab_move 1
map <S-TAB> tab_move -1
map <A-Right> tab_move 1
map <A-Left>  tab_move -1
map <A-l>  tab_move 1
map <A-h>  tab_move -1
map <C-n>  tab_new
map <C-w>  tab_close
map tn eval fm.tab_new('%d')
map uq     tab_restore
map <a-1>  tab_open 1
map <a-2>  tab_open 2
map <a-3>  tab_open 3
map <a-4>  tab_open 4
map <a-5>  tab_open 5
map <a-6>  tab_open 6
map <a-7>  tab_open 7
map <a-8>  tab_open 8
map <a-9>  tab_open 9

# Generate all the chmod bindings with some python help
eval for arg in "rwxXst": cmd("map +u{0} shell -f chmod u+{0} %s".format(arg))
eval for arg in "rwxXst": cmd("map +g{0} shell -f chmod g+{0} %s".format(arg))
eval for arg in "rwxXst": cmd("map +o{0} shell -f chmod o+{0} %s".format(arg))
eval for arg in "rwxXst": cmd("map +a{0} shell -f chmod a+{0} %s".format(arg))
eval for arg in "rwxXst": cmd("map +{0} shell -f chmod u+{0} %s".format(arg))
eval for arg in "rwxXst": cmd("map -u{0} shell -f chmod u-{0} %s".format(arg))
eval for arg in "rwxXst": cmd("map -g{0} shell -f chmod g-{0} %s".format(arg))
eval for arg in "rwxXst": cmd("map -o{0} shell -f chmod o-{0} %s".format(arg))
eval for arg in "rwxXst": cmd("map -a{0} shell -f chmod a-{0} %s".format(arg))
eval for arg in "rwxXst": cmd("map -{0} shell -f chmod u-{0} %s".format(arg))

# ===================================================================
# == Define keys for the console
# ===================================================================

# Basic
cmap <C-l>   redraw_window
cmap <Tab>   eval fm.ui.console.tab()
cmap <ESC>   eval fm.ui.console.close()
cmap <CR>    eval fm.ui.console.execute()
cmap <S-Tab> eval fm.ui.console.tab(-1)
copycmap <CR> <C-j>
copycmap <ESC> <C-c>

# Move around
cmap <Left> eval fm.ui.console.move(left=1)
cmap <Right> eval fm.ui.console.move(right=1)
cmap <Up> eval fm.ui.console.history_move(-1)
cmap <Down> eval fm.ui.console.history_move(1)
cmap <A-left> eval fm.ui.console.move_word(left=1)
cmap <A-right> eval fm.ui.console.move_word(right=1)
cmap <Home> eval fm.ui.console.move(right=0, absolute=True)
cmap <End> eval fm.ui.console.move(right=-1, absolute=True)

# Line Editing
cmap <C-y> eval fm.ui.console.paste()
cmap <Delete> eval fm.ui.console.delete(0)
cmap <C-w> eval fm.ui.console.delete_word()
cmap <C-k> eval fm.ui.console.delete_rest(1)
cmap <C-u> eval fm.ui.console.delete_rest(-1)
cmap <Backspace> eval fm.ui.console.delete(-1)
cmap <A-d> eval fm.ui.console.delete_word(backward=False)

cmap <allow_quantifiers> false
copycmap <Backspace> <Backspace2>

# ===================================================================
# == Pager Keybindings
# ===================================================================

# Movement
pmap <Up> pager_move up=1
pmap <Home> pager_move to=0
pmap <End> pager_move to=-1
pmap <Down> pager_move down=1
pmap <Left> pager_move left=4
pmap <Right> pager_move right=4
pmap <C-u> pager_move up=0.5 pages=True
pmap <C-d> pager_move down=0.5 pages=True
pmap <Pageup> pager_move up=1.0 pages=True
pmap <Pagedown> pager_move down=1.0 pages=True

copypmap <End> G
copypmap <C-d> d
copypmap <C-u> u
copypmap <Home> g
copypmap <Left> h
copypmap <Right> l
copypmap <Up> k <C-p>
copypmap <Down> j <C-n> <CR>
copypmap <Pageup> p b <C-B>
copypmap <Pagedown> n f <C-F> <Space>

# Basic
pmap E edit_file
pmap <ESC> pager_close
pmap <C-l> redraw_window
copypmap <ESC> q Q i <F3>

# ===================================================================
# == Taskview Keybindings
# ===================================================================

# Movement
tmap <up>       taskview_move up=1
tmap <down>     taskview_move down=1
tmap <home>     taskview_move to=0
tmap <end>      taskview_move to=-1
tmap <pagedown> taskview_move down=1.0  pages=True
tmap <pageup>   taskview_move up=1.0    pages=True
tmap <C-d>      taskview_move down=0.5  pages=True
tmap <C-u>      taskview_move up=0.5    pages=True

copytmap <UP>       k  <C-p>
copytmap <DOWN>     j  <C-n> <CR>
copytmap <HOME>     g
copytmap <END>      G
copytmap <C-u>      u
copytmap <PAGEDOWN> n  f  <C-F>  <Space>
copytmap <PAGEUP>   p  b  <C-B>

# Changing priority and deleting tasks
tmap J          eval -q fm.ui.taskview.task_move(-1)
tmap K          eval -q fm.ui.taskview.task_move(0)
tmap dd         eval -q fm.ui.taskview.task_remove()
tmap <pagedown> eval -q fm.ui.taskview.task_move(-1)
tmap <pageup>   eval -q fm.ui.taskview.task_move(0)
tmap <delete>   eval -q fm.ui.taskview.task_remove()

# Basic
tmap <C-l> redraw_window
tmap <ESC> taskview_close
copytmap <ESC> q Q w <C-c>

# ===================================================================
# == Additional Keybinds
# ===================================================================

## New
map Nf console touch%space
map Nd console mkdir%space

## Set background
map sZ shell nitrogen --set-zoom-fill --head=0  --save %f
map sz shell nitrogen --set-zoom-fill --head=1  --save %f
map sS shell nitrogen --set-scaled    --head=0  --save %f
map ss shell nitrogen --set-scaled    --head=1  --save %f
map sF shell nitrogen --set-zoom-fill --head=-1 --save %f
map sf shell nitrogen --set-scaled    --head=-1 --save %f

## Go / cd
map gh cd ~
map g/ cd /
map gE cd /etc
map gu cd /usr
map gb cd ~/bin
map gm cd ~/Music
map g. cd ~/.config
map gp cd ~/Pictures
map gd cd ~/Downloads
map gr cd ~/.config/ranger
map g? cd /usr/share/doc/ranger

## Move
map mD shell mv %s ~/Documents/
map md shell mv %s ~/Downloads/
map mp shell mv %s ~/Pictures/
map m. shell mv %s ~/.config/
map mv shell mv %s ~/Videos/
map mm shell mv %s ~/Music/
map mb shell mv %s ~/bin/
map mh shell mv %s ~/

## Copy
map YD shell cp -r %s ~/Documents/
map Yd shell cp -r %s ~/Downloads/
map Yp shell cp -r %s ~/Pictures/
map Y. shell cp -r %s ~/.config/
map Yv shell cp -r %s ~/Videos/
map Ym shell cp -r %s ~/Music/
map Yb shell cp -r %s ~/bin/
map Yh shell cp -r %s ~/
