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

add insert_character_interactive command #11411

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

jyn514
Copy link
Contributor

@jyn514 jyn514 commented Aug 3, 2024

this command inserts a character of the user's choice underneath the current cursor, without exiting normal mode. multiple cursors and more than 1 counts are handled correctly.

i chose C-y for the default mapping because it doesn't conflict with anything else. i would have preferred C-i (i for insert), but that conflicts with jump_forward; and additionally most terminals do not distinguish C-i from tab, so it's not a good choice for the default.

this command does not have a precedent in vim or kakoune to my knowledge, but at least vim allows replicating it with remap command (https://superuser.com/questions/581572/insert-single-character-in-vim), while helix currently does not, since there's no way to say "a character entered by the user".

i wasn't sure what to do when inserting multiple newlines, so that just panics for now. presumably only the last one should get indentation and all the rest should be blank lines?

here is a demo of it working (click for video): an asciinema recording of me opening helix, selecting 4 lines at once, and inserting 5 semicolons into each without exiting normal mode

Comment on lines +3809 to +3814
fn insert_tab_impl(cx: &mut Context, count: usize) {
let (view, doc) = current!(cx.editor);
// TODO: round out to nearest indentation level (for example a line with 3 spaces should
// indent by one to reach 4 spaces).

let indent = Tendril::from(doc.indent_style.as_str());
let indent = Tendril::from(doc.indent_style.as_str().repeat(count));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handling count is done here rather than in the new insert_char command because it needs to be taken into account if this TODO is ever fixed; putting it outside insert_tab makes it more likely to be forgotten.

@@ -397,6 +397,7 @@ impl MappableCommand {
smart_tab, "Insert tab if all cursors have all whitespace to their left; otherwise, run a separate command.",
insert_tab, "Insert tab char",
insert_newline, "Insert newline char",
insert_char_interactive, "Insert an interactively-chosen char",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm not super happy with this name or description, but hopefully it gets the idea across.

@kirawi kirawi added the A-command Area: Commands label Aug 4, 2024
Copy link
Member

@the-mikedavis the-mikedavis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand the motivation for this - it's to avoid switching modes?

As part of introducing a plugin system I imagine we would expose the on_next_key functionality so this should be possible via scripting in the long run

helix-term/src/commands.rs Outdated Show resolved Hide resolved
helix-term/src/commands.rs Outdated Show resolved Hide resolved
helix-term/src/keymap/default.rs Outdated Show resolved Hide resolved
this command inserts a character of the user's choice underneath the current cursor, without exiting normal mode. multiple cursors are handled correctly.

i chose `C-y` for the default mapping because it doesn't conflict with anything else. i would have preferred `C-i` (i for insert), but that conflicts with `jump_forward`; and additionally most terminals do not distinguish `C-i` from `tab`, so it's not a good choice for the default.

this command does not have precedence in vim or kakoune to my knowledge, but at least vim allows replicating it with remap command (https://superuser.com/questions/581572/insert-single-character-in-vim), while helix currently does not, since there's no way to say "a character entered by the user".
Copy link
Contributor Author

@jyn514 jyn514 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand the motivation for this - it's to avoid switching modes?

yes, exactly. it's like r but without deleting the character under the cursor.

helix-term/src/commands.rs Outdated Show resolved Hide resolved
helix-term/src/commands.rs Outdated Show resolved Hide resolved
@jyn514
Copy link
Contributor Author

jyn514 commented Aug 8, 2024

As part of introducing a plugin system I imagine we would expose the on_next_key functionality so this should be possible via scripting in the long run

by the way, do you happen to know a vague idea of how long that would take to land? i don't mind keeping this in my fork of helix until scripting lands, but it would be nice not to need a fork for multiple months 😅

@pascalkuthe
Copy link
Member

Plugin system will definitely take multiple months.

@the-mikedavis
Copy link
Member

Yeah it will definitely take some time. I'm not necessarily against this change as is though. Especially if it doesn't have a default keybinding it's a very slim change and the command itself is very straightforward. I might recommand adding an append_character_interactive which does the same at the end of the selection though

this behaves like regular append with respect to selections: the newly
inserted text will be selected when the command finishes, unlike when
using `insert_char_interactive`.
@jyn514
Copy link
Contributor Author

jyn514 commented Sep 25, 2024

I might recommand adding an append_character_interactive which does the same at the end of the selection though

i've done this :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-command Area: Commands
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants