dhruvasagar / vim-prosession

Handle vim sessions like a pro
254 stars 22 forks source link

How to restore settings for help buffers, including syntax highlighting? #39

Closed lacygoill closed 7 years ago

lacygoill commented 7 years ago

Hello,

I wrote a small plugin, which tries to do what vim-obsession does as well as some functionalities of vim-prosession (basically just restoring the last session automatically when Vim starts).

But it has an issue which I think vim-prosession also has. When my plugin restores automatically the session, if the latter contains a help buffer (and 'sessionoptions' contains help), its settings are not restored. Including the syntax highlighting.

I tried a workaround, invoking a function s:restore_help_settings_when_needed(), at the end of the restoration process. It iterates over the buffers and when it detects one whose path matches a help buffer, it sets the right options. For some reason, the settings don't survive if I reload the help buffer, so the function also installs an autocmd listening to BufRead:

call s:restore_help_settings_when_needed()

fu! s:restore_help_settings_when_needed() abort
    let cur_bufnr = bufnr('%')
    sil! bufdo if expand('%') =~# '/doc/.*\.txt$'
            \|     call s:restore_help_settings()
            \| endif
    if bufexists(cur_bufnr)
        exe 'b '.cur_bufnr
    endif
endfu

fu! s:restore_help_settings() abort
    setl ft=help nobuflisted noma ro
    so $VIMRUNTIME/syntax/help.vim
    augroup restore_help_settings
        au! * <buffer>
        au BufRead <buffer> setl ft=help nobuflisted noma ro
    augroup END
endfu

It works, but there's still an issue. If I try to jump to the definition of a tag defined in a help buffer which was restored by the plugin, again, there's no syntax highlighting in the new help buffer.

I don't know how the :help command works, but it seems that it reads the help buffers in a special way. So, I thought that the only way to restore a help buffer, was to use :help. Maybe using a code like this:

 exe 'h '.matchstr(expand('%'), '.*/doc/\zs.*\.txt')
 exe "norm! \<c-o>"

The first command would invoke :help on the current help buffer. The 2nd one would jump back to where the cursor was. It works but not immediately, I have to manually reload the buffer (and IIRC it opens another window, displaying the same buffer, so a window needs to be closed).

All in all, I haven't been able to find a way to restore a help buffer without some manual intervention. I tried vim-obsession + vim-prosession and they seem to have the same issue.

By any chance, do you know why this happens, and how to solve it?

Thank you very much for your plugin.