[vim/vim] Allow 'opfunc' to be local. (Issue #18881)

11 views
Skip to first unread message

ubaldot

unread,
5:16 AM (15 hours ago) 5:16 AM
to vim/vim, Subscribed
ubaldot created an issue (vim/vim#18881)

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.Message ID: <vim/vim/issues/18881@github.com>

zeertzjq

unread,
5:29 AM (15 hours ago) 5:29 AM
to vim/vim, Subscribed
zeertzjq left a comment (vim/vim#18881)

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.Message ID: <vim/vim/issues/18881/3621895864@github.com>

ubaldot

unread,
6:02 AM (15 hours ago) 6:02 AM
to vim/vim, Subscribed
ubaldot left a comment (vim/vim#18881)

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.Message ID: <vim/vim/issues/18881/3621916034@github.com>

Enno

unread,
8:36 AM (12 hours ago) 8:36 AM
to vim/vim, Subscribed
Konfekt left a comment (vim/vim#18881)

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.Message ID: <vim/vim/issues/18881/3622062731@github.com>

Reply all
Reply to author
Forward
0 new messages