Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sessions #96

Merged
merged 14 commits into from
May 21, 2021
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,18 @@ selected file in a tab, instead of the current window. <kbd>ctrl-x</kbd> will
open in a split an so on. Meanwhile for multi selected files will be loaded in
the buffer list.

#### Persistent session

You can configure n³ to use a session to remember your place when you reopen it.

```vim
" use the same nnn session within a vim session
let g:nnn#session = 'local'

" use the same nnn session everywhere (including outside vim)
let g:nnn#session = 'global'
```

#### Command override

When you want to override the default n³ command and add some extra flags.
Expand Down
21 changes: 18 additions & 3 deletions autoload/nnn.vim
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
let s:temp_file = ""
let s:action = ""
let s:tbuf = 0
" HACK: cannot use / in a session name
let s:local_ses = substitute(tempname(), '/', '-', 'g')

function! s:statusline()
setlocal statusline=%#StatusLineTerm#\ nnn\ %#StatusLineTermNC#
Expand Down Expand Up @@ -273,6 +275,8 @@ function! s:create_term_buf(opts)
endif
endfunction

let s:nnn_conf_dir = (!empty($XDG_CONFIG_HOME) ? $XDG_CONFIG_HOME : $HOME.'/.config') . '/nnn'

function! s:create_on_exit_callback(opts)
let l:opts = a:opts
function! s:callback(id, code, ...) closure
Expand All @@ -283,8 +287,7 @@ function! s:create_on_exit_callback(opts)

call s:eval_temp_file(l:opts)

let fdir = !empty($XDG_CONFIG_HOME) ? $XDG_CONFIG_HOME : $HOME.'/.config'
let fname = fdir . '/nnn/.lastd'
let fname = s:nnn_conf_dir.'/.lastd'
if !empty(glob(fname))
let firstline = readfile(fname)[0]
let lastd = split(firstline, '"')[1]
Expand Down Expand Up @@ -342,7 +345,19 @@ function! nnn#pick(...) abort
let l:default_opts = { 'edit': 'edit' }
let l:opts = extend(l:default_opts, get(a:, 2, {}))
let s:temp_file = tempname()
let l:cmd = g:nnn#command.' -p '.shellescape(s:temp_file).' '.(l:directory != '' ? shellescape(l:directory): '')
if g:nnn#session ==# 'none'
let l:sess_cfg = ' '
elseif g:nnn#session ==# 'global'
let l:sess_cfg = ' -S '
elseif g:nnn#session ==# 'local'
mizlan marked this conversation as resolved.
Show resolved Hide resolved
let l:sess_cfg = ' -S -s '.s:local_ses.' '
let session_file = s:nnn_conf_dir.'/sessions/'.s:local_ses
if !(exists('g:nnn_ses_autocmd'))
execute 'autocmd VimLeavePre * call delete(fnameescape("'.session_file.'"))'
let g:nnn_ses_autocmd = 1
mizlan marked this conversation as resolved.
Show resolved Hide resolved
endif
endif
let l:cmd = g:nnn#command.l:sess_cfg.' -p '.shellescape(s:temp_file).' '.(l:directory != '' ? shellescape(l:directory): '')
let l:layout = exists('l:opts.layout') ? l:opts.layout : g:nnn#layout

let l:opts.layout = l:layout
Expand Down
4 changes: 4 additions & 0 deletions plugin/nnn.vim
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ if !(exists("g:nnn#shell"))
let g:nnn#shell = &shell
endif

if !(exists("g:nnn#session"))
let g:nnn#session = "none"
endif

command! -bar -nargs=? -complete=dir NnnPicker call nnn#pick(<f-args>)
command! -bar -nargs=? -complete=dir Np call nnn#pick(<f-args>)

Expand Down