Sometimes I am in need of using different 'opfunc' for different buffers.
For example, I would need a 'opfunc' for text filetype, another one for python filetypes and so on.
It would be handy to make this option local.
—
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.![]()
Usually 'opfunc' is only set temporarily, so there is no need for local 'opfunc' values. What is the use case of setting 'opfunc' permanently?
—
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.![]()
I have exactly this use-case:
In ~/.vim/autoload/utils.vim:
def UndoFormatting() if v:shell_error != 0 undo echoerr $"'{&l:formatprg->matchstr('^\s*\S*')}' returned errors." else # Display format command redraw if !empty(&l:formatprg) echo $'{&l:formatprg}' else echo "'formatprg' is empty. Using default formatter." endif endif enddef export def FormatWithoutMoving(type: string = '') # 'gq' is remapped to use this function. # However, when running 'gq', we need to use as it is # shipped with Vim, and therefore we use the bang in 'normal!', below var view = winsaveview() # defer so if the function gets error of any kind, the following are run # anyways defer UndoFormatting() defer winrestview(view) var start = 0 var end = 0 if type != '' start = line("'[") end = line("']") endif if start == 0 && end == 0 normal! gggqG else var interval = end - start + 1 silent exe $"normal! {start}gg{interval}gqq" endif enddef
in ~/.vim/ftplugin/markdown.vim:
import autoload '../autoload/mde_utils.vim' as utils # ... def SetMarkdownOpFunc() &l:opfunc = function('utils.FormatWithoutMoving') enddef # Hack on 'gq' nnoremap <buffer> <silent> gq <ScriptCmd>SetMarkdownOpFunc()<cr>g@ xnoremap <buffer> <silent> gq <ScriptCmd>SetMarkdownOpFunc()<cr>g@
When I am editing a markdown file, I get what I expect when I use gq.
When I switch to a e.g. text-filetype file, then the gq mappings are
reset, but 'opfunc' stays set to function('utils.FormatWithoutMoving') , i.e. it is not reset.
—
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.![]()
Do you maybe only want to keep the cursor position when operating on the whole buffer? If no formatprg/expr is set, then gw is gq keeping the cursor
—
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.![]()