christianrondeau / vim-base64

Vim plugin to encode/decode base64 strings
MIT License
43 stars 14 forks source link

how do you operate on the visual selection #10

Closed stumash closed 2 years ago

stumash commented 2 years ago

I'd love if you could explain to me (somewhat a noob) how this code works.

https://github.com/christianrondeau/vim-base64/blob/d15253105f6a329cd0632bf9dcbf2591fb5944b8/autoload/base64.vim#L29-L41

I'm trying to add functionality where instead of just b64 encoding/decoding, you can also do gzip_encode_then_b64_encode and b64_decode_then_gzip_decode.

christianrondeau commented 2 years ago

Hi @stumash! Those are vimscript (viml) commands. To make sense of them you do have to understand basic vim navigation and scripting, this was really the read that worked for me: https://learnvimscriptthehardway.stevelosh.com/

If you're not feeling like learning the whole thing to just add a simple feature, I'll need a more precise question :) To avoid explaining what's already in the comments, the line that might cause trouble here is the "execute" one; it actually runs ctrl+r, which allows running an expression (= is the "expression register") and inserting the result into the text without leaving insert mode. The function will be base64#encode for example, which can be found in that same file.

If you look at base64#encode for example, you'll see it simply runs a system executable to do the actual encoding; gzip would be similar, you could simply pipe the two commands (for linux at least) rather than doing it through vim (remember that vim mostly deals with strings, not bytes).

For example, return base64#strip(system('gzip | base64 --wrap=0', a:input)) (not tested)

stumash commented 2 years ago

hey, thanks for all the info! and thanks for the great library!

I was wondering specifically:

thanks for all your help!

christianrondeau commented 2 years ago

set paste means "enable paste mode" (see doc in vim with :help paste. This avoids commands interfering with keyboard input, so pasting doesn't runs random commands :) Without it, things like line breaks and insert mode commands will run during the insertion of the new text. It's actually unrelated to the clipboard.

normal will then run commands in "normal" mode, and ! will avoid having your custom normal commands interfere with the plugin. gv will simply reselect whatever was selected before since we use c-u to avoid running the commands in range mode: https://github.com/christianrondeau/vim-base64/blob/d15253105f6a329cd0632bf9dcbf2591fb5944b8/plugin/base64.vim#L4

Once we're in paste mode and we have the visual selection back, we can run c to "change" the selection, to insert a register, = to run a command register, base64#something( to run our function, @" to send what we just copied from c as the parameter to the function, which then passes it as stdin to the command in system.

Phew! Vim is crazy amazing but you can't underestimate its learning curve :)

stumash commented 2 years ago

wow, okay got it. thanks again. I'm sorry to ask all these questions, thanks for taking the time. one last question, I swear I'll close the issue after.

You said that gv will reselect what was selected before. What I want to know is: when was the user's visual selection deselected (by yank or just by hitting escape, for example) in the first place so that gv will go back to it. Because if I select some random text in visual mode, and then hit gv, it will go to the prior visual seleciton, not retain the current one if I'm currently in visual mode.

christianrondeau commented 2 years ago

No problem :)

It's from <c-u> in the keyboard map. It's ignoring the range and calling the function, so I have to re-select to act on it. There might be better ways to do it 🤷

stumash commented 2 years ago

ooohhhhhhh. got it! thanks!

stumash commented 2 years ago

just gonna leave this here in case it helps anyone:

I didn't know you could pass the contents of a register (say, register x)to a vim function by doing, e.g.:

:call myVimFunction(@x)

so that's how you pass the yanked text (which gets put in the default register ") to the text-transform function!

stumash commented 2 years ago

I ended up making this https://github.com/stumash/shellvis

I can't thank you enough for all the help to make it possible

christianrondeau commented 2 years ago

Very cool @stumash :)