Skip to content

Commit cf62f64

Browse files
authored
Merge pull request #96 from mizlan/sessions
Sessions
2 parents c678c7e + 21d10ef commit cf62f64

File tree

4 files changed

+64
-6
lines changed

4 files changed

+64
-6
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,22 @@ selected file in a tab, instead of the current window. <kbd>ctrl-x</kbd> will
9393
open in a split an so on. Meanwhile for multi selected files will be loaded in
9494
the buffer list.
9595

96+
#### Persistent session
97+
98+
You can configure n³ to use a session so it remembers your place when
99+
you reopen it.
100+
101+
```vim
102+
" use the same nnn session within a vim session
103+
let g:nnn#session = 'local'
104+
105+
" use the same nnn session everywhere (including outside vim)
106+
let g:nnn#session = 'global'
107+
```
108+
109+
Note: If desired, an n³ session can be disabled temporarily by passing
110+
`session: false` as an option to `nnn#pick()`.
111+
96112
#### Command override
97113

98114
When you want to override the default n³ command and add some extra flags.

autoload/nnn.vim

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@ let s:temp_file = ""
22
let s:action = ""
33
let s:tbuf = 0
44

5+
let s:local_ses = 'nnn_vim_'
6+
" Add timestamp for convenience
7+
" :h strftime() -- strftime is not portable
8+
if exists('*strftime')
9+
let s:local_ses .= strftime('%Y_%m_%dT%H_%M_%SZ')
10+
else
11+
" HACK: cannot use / in a session name
12+
let s:local_ses .= substitute(tempname(), '/', '_', 'g')
13+
endif
14+
515
function! s:statusline()
616
setlocal statusline=%#StatusLineTerm#\ nnn\ %#StatusLineTermNC#
717
endfunction
@@ -242,8 +252,8 @@ function! s:switch_back(opts, Cmd)
242252
execute win_id2win(l:term_wins.term.winhandle) . 'close'
243253
endif
244254
catch /E444: Cannot close last window/
245-
" In case Vim complains it is the last window, fail silently.
246-
endtry
255+
" In case Vim complains it is the last window, fail silently.
256+
endtry
247257
if bufexists(l:term_wins.term.buf)
248258
execute 'bwipeout!' l:term_wins.term.buf
249259
endif
@@ -273,6 +283,8 @@ function! s:create_term_buf(opts)
273283
endif
274284
endfunction
275285

286+
let s:nnn_conf_dir = (!empty($XDG_CONFIG_HOME) ? $XDG_CONFIG_HOME : $HOME.'/.config') . '/nnn'
287+
276288
function! s:create_on_exit_callback(opts)
277289
let l:opts = a:opts
278290
function! s:callback(id, code, ...) closure
@@ -283,8 +295,7 @@ function! s:create_on_exit_callback(opts)
283295

284296
call s:eval_temp_file(l:opts)
285297

286-
let fdir = !empty($XDG_CONFIG_HOME) ? $XDG_CONFIG_HOME : $HOME.'/.config'
287-
let fname = fdir . '/nnn/.lastd'
298+
let fname = s:nnn_conf_dir.'/.lastd'
288299
if !empty(glob(fname))
289300
let firstline = readfile(fname)[0]
290301
let lastd = split(firstline, '"')[1]
@@ -342,7 +353,20 @@ function! nnn#pick(...) abort
342353
let l:default_opts = { 'edit': 'edit' }
343354
let l:opts = extend(l:default_opts, get(a:, 2, {}))
344355
let s:temp_file = tempname()
345-
let l:cmd = g:nnn#command.' -p '.shellescape(s:temp_file).' '.(l:directory != '' ? shellescape(l:directory): '')
356+
357+
if g:nnn#session ==# 'none' || !get(l:opts, 'session', 1)
358+
let l:sess_cfg = ' '
359+
elseif g:nnn#session ==# 'global'
360+
let l:sess_cfg = ' -S '
361+
elseif g:nnn#session ==# 'local'
362+
let l:sess_cfg = ' -S -s '.s:local_ses.' '
363+
let session_file = s:nnn_conf_dir.'/sessions/'.s:local_ses
364+
execute 'augroup NnnSession | autocmd! VimLeavePre * call delete(fnameescape("'.session_file.'")) | augroup End'
365+
else
366+
let l:sess_cfg = ' '
367+
endif
368+
369+
let l:cmd = g:nnn#command.l:sess_cfg.' -p '.shellescape(s:temp_file).' '.(l:directory != '' ? shellescape(l:directory): '')
346370
let l:layout = exists('l:opts.layout') ? l:opts.layout : g:nnn#layout
347371

348372
let l:opts.layout = l:layout

doc/nnn.txt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ buffer opens running an n³ process in picker mode.
4444
Once you select (see https://github.com/jarun/nnn/wiki/concepts#selection)
4545
one or more files and press enter, vim quits the n³ window and opens the
4646
first selected file and add the remaining files to the arg list/buffer
47-
list.
47+
list. If |nnn#session| is enabled, then n³ will remember where you left off
48+
of when you reopen it.
4849

4950
Pressing enter on a file in n³ will pick any earlier selection, pick
5051
the file and exit n³.
@@ -146,6 +147,19 @@ g:nnn#action *g:nnn#action*
146147
endfunction
147148
<
148149

150+
g:nnn#session *g:nnn#session*
151+
Default: 'none'
152+
How n³ should utilize sessions. Default is 'none' meaning no
153+
persistent sessions. Other options are 'local' for the same
154+
n³ session across a vim session, or 'global' for the same n³
155+
session across everywhere.
156+
>
157+
" use the same nnn session within a vim session, for
158+
" example, so that consecutive uses of :Np will remember
159+
" where you last left off
160+
let g:nnn#session = 'local'
161+
<
162+
149163
g:nnn#command *g:nnn#command*
150164
Default: 'nnn'
151165
When you want to override the default n³ command and add

plugin/nnn.vim

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ if !(exists("g:nnn#shell"))
3131
let g:nnn#shell = &shell
3232
endif
3333

34+
if !(exists("g:nnn#session"))
35+
let g:nnn#session = "none"
36+
endif
37+
3438
command! -bar -nargs=? -complete=dir NnnPicker call nnn#pick(<f-args>)
3539
command! -bar -nargs=? -complete=dir Np call nnn#pick(<f-args>)
3640

0 commit comments

Comments
 (0)