helix icon indicating copy to clipboard operation
helix copied to clipboard

Refactor and expand special registers

Open the-mikedavis opened this issue 2 years ago • 1 comments

This implements some special registers from Kakoune:

  • #: index numbers for the current selections
  • .: contents of the current selections
  • %: name of the current file (#5577)

And adds special registers for the system and primary clipboards: * and + respectively. This also re-uses the trick from #6889 to store the selections yanked to clipboards so they can be re-used on paste if the clipboard hasn't changed.

The strategy here mirrors how Kakoune implements these special registers and should extend to the remaining special registers in Kakoune (1..9 for regex captures), but it's an unfortunately large refactor that adds complexity and allocations and needs updates for all register callsites. The overview is...

  • Register becomes a trait implemented by the old basic register type and each special register.
  • read functions now return owned values. This is necessary because of the way some special registers produce their values. (This is not great though and it'd be nice to find a way around this.)
  • read, write and push functions now take the Editor as a parameter. This is the main change that enables special registers and is what mirrors Kakoune but causes a larger change:
    • Registers moves from a field on Editor to fields on the Context types to avoid borrowing conflicts.

the-mikedavis avatar May 06 '23 19:05 the-mikedavis

#: index numbers for the current selections .: contents of the current selections

Is the buffer save state exposed somehow? That would be useful.

Also, out of scope for this issue but is there any way to turn this into selection ranges?

edrex avatar May 22 '23 00:05 edrex

I can't imagine use-cases for either of these, could you expand on that in a separate discussion? This is specifically about the Kakoune registers & clipboard registers

the-mikedavis avatar May 26 '23 06:05 the-mikedavis

When and how is the # ever used? I found this line, but I can't find where it set...

https://github.com/helix-editor/helix/blob/a9849ebee41a8cb884a9351d36ebd399adb4111e/helix-term/src/commands.rs#L5480

gibbz00 avatar Jul 02 '23 11:07 gibbz00

Is there a pipe register too? https://github.com/helix-editor/helix/blob/a9849ebee41a8cb884a9351d36ebd399adb4111e/helix-term/src/commands.rs#L5204

gibbz00 avatar Jul 02 '23 11:07 gibbz00

When and how is the # ever used? I found this line, but I can't find where it set...

https://github.com/helix-editor/helix/blob/a9849ebee41a8cb884a9351d36ebd399adb4111e/helix-term/src/commands.rs#L5480

that register was a special case for that command in the past this PR adds it as a general purpose register.

Is there a pipe register too?

https://github.com/helix-editor/helix/blob/a9849ebee41a8cb884a9351d36ebd399adb4111e/helix-term/src/commands.rs#L5204

Is there a pipe register too?

https://github.com/helix-editor/helix/blob/a9849ebee41a8cb884a9351d36ebd399adb4111e/helix-term/src/commands.rs#L5204

Not in this PR. I don't think that makes too much sense for us given that we don't have an interactive shell yet

pascalkuthe avatar Jul 02 '23 14:07 pascalkuthe

Yeah the existing line you linked is used for "#<C-a> or "#<C-x> so you can increment/decrement by selection number. With this PR you can use "#P to paste the selection numbers before each cursor.

the-mikedavis avatar Jul 02 '23 14:07 the-mikedavis

Huh, cool! ~~Where # set?~~ Got it.

gibbz00 avatar Jul 02 '23 16:07 gibbz00

Pascal and I chatted about this and settled on something lighter-weight. The trait is replaced with matchs on the chars. The clipboard_provider moves from Editor to Registers and all clipboard commands switch to using the new clipboard registers. With that change we avoid the borrowing conflict and can keep registers on Editor plus we delete some clipboard-specific chunks of code out of the commands module.

the-mikedavis avatar Jul 11 '23 17:07 the-mikedavis

maybe we should document somewhere that registers are stored in reverse so that we can push at the end? It would be nice to have some docs for that. It's obvious to me now but might be confusing for somebody new to the codebase

pascalkuthe avatar Jul 11 '23 17:07 pascalkuthe