Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] multi select yank only yank the last line #4662

Closed
theowenyoung opened this issue Jul 5, 2022 · 9 comments
Closed

[BUG] multi select yank only yank the last line #4662

theowenyoung opened this issue Jul 5, 2022 · 9 comments
Labels

Comments

@theowenyoung
Copy link

Version of Kakoune

346b81b

Reproducer

CCCCC , then L select to end, 5 lines are selected, then y, yanked, then p, only last line is pasted.

Outcome

only last line is yanked

Expectations

all selected lines should be yanked.

Additional information

No response

@Screwtapello
Copy link
Contributor

Here's what I tried:

  1. Launch Kakoune with some test data:

     $ seq 5 | kak -n
    
  2. Select each line, including the newline, yank, then immediately paste

     CCCCLyp
    

Resulting output:

1
1
2
2
3
3
4
4
5
5

...i.e. all lines were pasted correctly.

Note that if you moved the cursor between yanking and pasting (for example, if you moved the cursor to the end of the buffer with gj to paste after the current contents instead of interleaved with it), then you'll only have one cursor, not 5, and Kakoune only pastes as many texts as there are cursors to paste them into. If you want all the yanked items at each cursor, you can use <a-p> instead of p.

@theowenyoung
Copy link
Author

theowenyoung commented Jul 6, 2022

I'm Sorry, I tried it again, yes, I think yank is ok in the internal, but it's not ok with my system clipboard, I use a custom shell script which copied from:

https://github.com/mawww/config/blob/604ef1c1c2f4722505d3d29a4fc95e763a1fddbb/kakrc#L78-L101

Here is keys: CCCCLLL<space>y,<space>p

# system clipboard handling
evaluate-commands %sh{
    if [ -n "$SSH_TTY" ]; then
        copy='printf "\033]52;;%s\033\\" $(base64 | tr -d "\n") > /dev/tty'
        paste='printf "paste unsupported through ssh"'
        backend="OSC 52"
    else
        case $(uname) in
            Linux)
                if [ -n "$WAYLAND_DISPLAY" ]; then
                    copy="wl-copy -p"; paste="wl-paste -p"; backend=Wayland
                else
                    copy="xclip -i"; paste="xclip -o"; backend=X11
                fi
                ;;
            Darwin)  copy="pbcopy"; paste="pbpaste"; backend=OSX ;;
        esac
    fi
    printf "map global user -docstring 'paste (after) from clipboard' p '<a-!>%s<ret>'\n" "$paste"
    printf "map global user -docstring 'paste (before) from clipboard' P '!%s<ret>'\n" "$paste"
    printf "map global user -docstring 'yank to primary' y '<a-|>%s<ret>:echo -markup %%{{Information}copied selection to %s primary}<ret>'\n" "$copy" "$backend"
    printf "map global user -docstring 'yank to clipboard' Y '<a-|>%s<ret>:echo -markup %%{{Information}copied selection to %s clipboard}<ret>'\n" "$copy -selection clipboard" "$backend"
    printf "map global user -docstring 'replace from clipboard' R '|%s<ret>'\n" "$paste"
}

It seems that the shell script has some problems.

Do you know how to deal with this?

@Screwtapello
Copy link
Contributor

Generally the system clipboard can only hold a single chunk of text at a time, not a list of chunks. When you have multiple selections in Kakoune and try to yank them all to the system clipboard, it processes each selection one-at-a-time, with the result that each selection replaces the previous contents of the clipboard. When you go to paste, the only thing on the clipboard is the contents of the last-yanked selection.

Most system clipboards these days support some kind of format negotiation, where an application can publish different kinds of data simultaneously, like a URL and HTML formatted text and plain text. Using this mechanism it would be possible for Kakoune to yank a "list of chunks of text" to the clipboard. However, I don't believe any of the tools mentioned in your config fragment (OSC 52, wl-copy, xclip, pbcopy, etc.) support such a thing.

@theowenyoung
Copy link
Author

Thanks for your explanation! I learned a lot.

I want to do this operation because I'm trying to learn Kakoune from TRAMPOLINE, I want to copy the paragraph. Does it possible that Kakoune treat it as a paragraph with \n? This seems more intuitive? (I'm from VSCode, this operation is treated as paragraph in VSCode)

kakoune/contrib/TRAMPOLINE

Lines 93 to 108 in f3cb2e4

In order to move the cursor to a specific word, the search
command is the way to go. This functionality allows
.---, the user to jump to the next occurrence of a piece of text.
| / | Upon hitting the `/` key, a prompt reading "search"
`---' will pop up in the status bar in which you can type
your text and validate using the `<ret>` (return) key.
.---, .---, You'll notice that as you type, the cursor changes location
|alt|+| / | to automatically give you a preview of where the cursor
`---' `---' would be displaced to if you validated the search. However,
this behavior is only a preview, exiting prompt mode with
the `<esc>` (escape) key will leave the current position
.---, of the cursor unchanged. Note that you can also use a
| n | regular expression as input. By default the search
`---' function will look for results forward, starting from
the current location of the cursor, but you can search
.---, .---, backwards using `<a-/>` (alt + `/`).

@Screwtapello
Copy link
Contributor

You can select the entire paragraph with Shift and the arrow keys, just like in VS Code, and you'll get something like this:

image

That's one selection, which you can copy/paste to Kakoune's internal clipboard, or to the system clipboard, or whatever, and it should work as you expect.

You can also select the entire paragraph by pressing <a-i> ("select inside object") followed by p ("paragraph"), which will get you a selection like this:

image

Kakoune detects a paragraph as "one or more non-blank lines, surrounded by blank lines". To a human, that selection looks like two paragraphs, but because the ASCII-art key diagram on the left straddles the paragraph break, there is no line that is fully blank, so Kakoune does not see a paragraph break there.

However, you probably just want to select the text "column" and not include the ASCII-art on the left. As you've figured out, the way to do this is to use C to additional selections below the current one (since the paragraph has 16 lines, you can put the cursor on the "I" of "In" on the first line, then type 15C), then <s-end> (Shift+End) to extend the selection to the end of each line. That gets you 16 selections that look like this:

image

Because that's 16 selections rather than 1, it can't be (usefully) yanked to the system clipboard in the same way VS Code would do it, for the reasons discussed above. It's also a bit awkward to work with in Kakoune, since the selection doesn't include the line-break at the end of the line, so yanking this selection and pasting it elsewhere will produce a bunch of text all on the same line.

We can extend each selection to include the trailing line-break by pressing L one additional time:

image

Now we can press y to yank those selections ('yanked 16 selections to register "says Kakoune), create a new scratch buffer for our cleanup work with the:edit -scratchcommand, then paste all the yanked selections at the (one) current cursor with`:

image

Now we have the text isolated without the ASCII-art interleaved, we can select the entire paragraph with a single selection (Shift+arrows, or <a-i>p, as mentioned previously) and yank it to the system clipboard.

Adding more natural system clipboard to Kakoune would be a good thing (#3935) but it's not obvious how to cleanly integrate such a feature with the way Kakoune works.

@theowenyoung
Copy link
Author

Thanks, I tried your workflow, and it works fine. This is not a very frequent operation, so it is acceptable.

@theowenyoung
Copy link
Author

I found that my <a-i>p is broken, also ] is broken too. :( , I don't know how to figure it, I don't think it's my config issue, cause I tried deleting the config, it still can not select the whole paragraph. [ work fine, but not for ]

@theowenyoung
Copy link
Author

I also tried with helix and I think helix has the expected operation for this.

maybe they convert multiple selections? https://github.com/helix-editor/helix/blob/master/helix-view/src/clipboard.rs

@Screwtapello
Copy link
Contributor

I found that my p is broken, also ] is broken too. :(

Why do you say they're broken? What happens when you try to use them?

maybe they convert multiple selections?

The clipboard API you link to puts a single string on the clipboard and takes a single string from the clipboard. If Helix is squashing its internal representation to a single string to interact with a clipboard, it's happening somewhere else.

You could probably do the same thing with your clipboard plugin by automating the "paste to a new scratch buffer then re-yank" trick I described above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants