Closed kimabrandt-flx closed 4 months ago
For my use-case, I needed the lines to be inserted after the cd
-command and before the badd
-commands; inside the session-file.
diff --git a/plugin/obsession.vim b/plugin/obsession.vim
index e36729d..851f2b4 100644
--- a/plugin/obsession.vim
+++ b/plugin/obsession.vim
@@ -77,6 +77,28 @@ function! s:persist() abort
let body = readfile(tmp)
call insert(body, 'let g:this_session = v:this_session', -3)
call insert(body, 'let g:this_obsession = v:this_session', -3)
+ if type(get(g:, 'obsession_prepend')) == type([])
+ let l:idx = 0
+ for l:i in range(0, len(body) - 1)
+ if match(body[l:i], "if &shortmess =\\~ 'A'") == 0
+ let l:idx = l:i + 5 " place after the `shortmess` if-else-block
+ break
+ endif
+ endfor
+ for l:Item in g:obsession_prepend
+ if type(l:Item) == v:t_string
+ call insert(body, l:Item, l:idx)
+ elseif type(l:Item) == v:t_list
+ for l:line in reverse(l:Item)
+ call insert(body, l:line, l:idx)
+ endfor
+ elseif type(l:Item) == v:t_func
+ for l:line in reverse(l:Item())
+ call insert(body, l:line, l:idx)
+ endfor
+ endif
+ endfor
+ endif
if type(get(g:, 'obsession_append')) == type([])
for line in g:obsession_append
call insert(body, line, -3)
I figured, I could trigger my function on the Obsession
-event.
Thank you, for this!
function! s:save_global_marks() abort
" ... code from https://github.com/tpope/vim-obsession/issues/81#issue-2272389561
let l:body = readfile(g:this_obsession)
let l:idx = 0
for l:i in range(0, len(l:body) - 1)
if match(l:body[l:i], "if &shortmess =\\~ 'A'") == 0
let l:idx = l:i + 5 " place after the `shortmess` if-else-block
break
endif
endfor
for l:line in reverse(flatten(l:lines))
call insert(l:body, l:line, l:idx)
endfor
call writefile(l:body, g:this_obsession)
endfunction
augroup my_obsession
autocmd!
autocmd User Obsession call s:save_global_marks()
augroup END
That's probably a more robust solution. g:obsession_append
is designed for static inclusions. For something like global marks you'd need to compute them each time, which is kind of awkward.
Would you be willing to extend vim-obsession and add a
g:obsession_prepend
-variable; in addition tog:obsession_append
?My use-case is: Keep global marks for a project in the session-file.
I've made a change in my copy of vim-obsession, where I'm using a
funcref
that's used to call-back a function in my config. The callback-function returns a list of lines - related to restoring my global marks - which are added (by vim-obsession) to the top of the session-file; so that they don't interfere withbadd
and other commands.Here's the change in
obsession.vim
, that I'm currently using:I'm not aware of a better way to restore global marks, other than editing a marked file, reapplying the cursor-position and then setting the mark!?
Here's the code - from my config - that gets called, by passing a
funcref
via theg:obsession_prepend
-variable:I'm using: