martanne / vis

A vi-like editor based on Plan 9's structural regular expressions
Other
4.19k stars 259 forks source link

Feature Request: Highlighted Yank #1062

Closed ghost closed 11 months ago

ghost commented 1 year ago

In the vein of https://github.com/machakann/vim-highlightedyank or Neovim's highlight.on_yank(). It's really very nice, and in my opinion small enough that it should really be part of the editor and not a plugin.

mcepl commented 1 year ago

vis is truly minimalist: the tarball has 395k, so I believe plugin would be a way preferable. Try to port that VimScript to Lua and we can then discuss whether it would be included as a permanent plugin into the official repo.

rnpnr commented 11 months ago

Personally I don't think its useful since I can't really imagine a situation where I wouldn't know what I'm yanking. However here is something I came up with if anyone wants it:

vis.events.subscribe(vis.events.INIT, function()
    local yank = vis:action_register("highlighted-yank", function()
        vis.win:style_define(vis.win.STYLE_SELECTION, "reverse")
        vis:redraw()
        local tstamp = os.clock()
        while os.clock() - tstamp < 0.5 do end
        vis.win:style_define(vis.win.STYLE_SELECTION, vis.lexers.STYLE_SELECTION)
        vis:redraw()
    end, "Yank operator with highlighting")
    vis:map(vis.modes.OPERATOR_PENDING, "y", yank)
    vis:map(vis.modes.VISUAL, "y", yank)
    vis:map(vis.modes.VISUAL_LINE, "y", yank)
end)

Note: you also need to highlight the selected range in OPERATOR_PENDING to obtain the same result as the vim plugin. It shouldn't be that difficult to do.

mcepl commented 11 months ago

It is actually rather convenient. With some weird combination of selection object, I am often not completely certain that I have copied exactly what I wanted.

Anyway, I have created https://git.sr.ht/~mcepl/vis-yank-highlight added

require('plugins/vis-yank-highlight')

to my ~/.config/vis/visrc.lua and nothing happens. When I run for example y$, text is correctly copied, but no highlighting happens.

rnpnr commented 11 months ago

That is what I mean by:

Note: you also need to highlight the selected range in OPERATOR_PENDING

Right now it only works in the especially useless case where the selection is already highlighted. Since you want to use it as a plugin I will see what I can do about adding that functionality. I'll email a patch if I figure it out.

mcepl commented 11 months ago

With https://lists.sr.ht/~mcepl/devel/patches/42973 applied and working, could we say that the plugin could work as a proof of concept and the evidence we can close this ticket as irrelevant for the vis itself?

rnpnr commented 11 months ago

Yes I think that is appropriate. There may be some limits to the plugin as I wrote it but I'll leave it up to anyone who wants to use it to address them. I think its a pretty good start.