[vim/vim] getchar(2) waits for character without moving the cursor (PR #10603)

139 views
Skip to first unread message

ii14

unread,
Jun 21, 2022, 7:14:28 AM6/21/22
to vim/vim, Subscribed

getchar() is useful for mapping dynamic key sequences, but the downside is that it moves the cursor to the command line when waiting for input. If the action depends on where the cursor is, you can lose the visual feedback on where you're about to make an edit. getchar(2) behaves just like getchar(), but bypasses the move of the cursor.


You can view, comment on, or merge this pull request online at:

  https://github.com/vim/vim/pull/10603

Commit Summary

  • a4d6064 getchar(2) waits for character without moving the cursor

File Changes

(2 files)

Patch Links:


Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/10603@github.com>

ii14

unread,
Jun 21, 2022, 7:40:27 AM6/21/22
to vim/vim, Push

@ii14 pushed 1 commit.

  • 0924164 fix vim9script type checks


View it on GitHub or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/10603/push/10220360116@github.com>

ii14

unread,
Jun 21, 2022, 7:50:41 AM6/21/22
to vim/vim, Subscribed

@ii14 commented on this pull request.


In src/testdir/test_vim9_builtin.vim:

> @@ -1683,8 +1683,7 @@ def Test_getchar()
   endwhile
   getchar(true)->assert_equal(0)
   getchar(1)->assert_equal(0)
-  v9.CheckDefAndScriptFailure(['getchar(2)'], ['E1013: Argument 1: type mismatch, expected bool but got number', 'E1212: Bool required for argument 1'])
-  v9.CheckDefAndScriptFailure(['getchar("1")'], ['E1013: Argument 1: type mismatch, expected bool but got string', 'E1212: Bool required for argument 1'])
+  v9.CheckDefAndScriptFailure(['getchar("1")'], ['E1013: Argument 1: type mismatch, expected bool but got string', 'E1290: Bool or Number required for argument 1'])

These tests are probably wrong. I don't get how to change what types vim9script expects.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/10603/review/1013462909@github.com>

ii14

unread,
Jun 21, 2022, 8:04:23 AM6/21/22
to vim/vim, Push

@ii14 pushed 1 commit.

  • 202d526 fix vim9script getcharstr test

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/10603/push/10220587547@github.com>

ii14

unread,
Jun 21, 2022, 9:12:12 AM6/21/22
to vim/vim, Subscribed

@ii14 commented on this pull request.


In src/getchar.c:

> @@ -2067,20 +2067,31 @@ getchar_common(typval_T *argvars, typval_T *rettv)
     parse_queued_messages();
 #endif
 
-    // Position the cursor.  Needed after a message that ends in a space.
-    windgoto(msg_row, msg_col);
+    if (argvars[0].v_type != VAR_UNKNOWN) {
+	int error = FALSE;
+	arg = tv_get_number_chk(&argvars[0], &error);

note to self: vim9script throws Using a Bool as a Number here


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/10603/review/1013576985@github.com>

ii14

unread,
Jun 24, 2022, 9:01:51 PM6/24/22
to vim/vim, Subscribed

@brammool Hey, before I proceed, do you have any comments on this? Would you be okay with this feature? It works right now with the old vim script, vim9script is broken at the moment.

Here's an example script that adds a window pseudo-mode. CTRL-W to enter it, and then CTRL-{W,P,H,J,K,L} will sustain that mode until you escape, CTRL-C or press some other key.

let s:remap = {
  \ "\<c-w>": "\<c-w>w",
  \ "\<c-p>": "\<c-w>p",
  \ "\<c-h>": "\<c-w>h",
  \ "\<c-j>": "\<c-w>j",
  \ "\<c-k>": "\<c-w>k",
  \ "\<c-l>": "\<c-w>l",
  \ "w":      "\<c-w>w",
  \ "p":      "\<c-w>p",
  \ "h":      "\<c-w>h",
  \ "j":      "\<c-w>j",
  \ "k":      "\<c-w>k",
  \ "l":      "\<c-w>l",
  \ }

function Getch(...)
  let ch = getcharstr(2)
  if has_key(s:remap, ch)
    call feedkeys(s:remap[ch], 'n')
    call timer_start(0, function('Getch'))
  else
    call feedkeys("\<C-W>"..ch, 'n')
    echo ''
  end
endfunction

nno <nowait> <C-W> <cmd>echo '-- WINDOW --'<bar>call timer_start(0, function('Getch'))<CR>


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/10603/c1166159383@github.com>

zeertzjq

unread,
Jun 24, 2022, 9:08:17 PM6/24/22
to vim/vim, Subscribed

There is an entry in todo.txt:
https://github.com/vim/vim/blob/4e0fc8956649d3208aeaa1642c5efc44e385d77a/runtime/doc/todo.txt#L213


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/10603/c1166160841@github.com>

Bram Moolenaar

unread,
Jun 25, 2022, 6:54:09 AM6/25/22
to vim/vim, Subscribed


> @brammool Hey, before I proceed, do you have any comments on this?
> Would you be okay with this feature? It works right now with the old
> vim script, vim9script is broken at the moment.

Yes, it appears to be useful. I'm holding off until after the 9.0
release, no new features right now.

I would think that we need an extra argument for the flag to not
position the cursor. Then the first, existing argument can be used as
before.

The extra argument can be just a flag, or, if we think there might be
other flags in the future, a dictionary with options.

--
It's totally unfair to suggest - as many have - that engineers are socially
inept. Engineers simply have different objectives when it comes to social
interaction.
(Scott Adams - The Dilbert principle)

/// Bram Moolenaar -- ***@***.*** -- http://www.Moolenaar.net \\\
/// \\\
\\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/10603/c1166255653@github.com>

ii14

unread,
Jun 29, 2022, 9:34:15 AM6/29/22
to vim/vim, Subscribed

I would think that we need an extra argument for the flag to not
position the cursor. Then the first, existing argument can be used as
before.

@brammool The problem is that getchar() has the blocking behavior when the argument is skipped. I might be wrong, but I don't think you can skip the first argument (leaving it as VAR_UNKNOWN?) and just supply the second in vim script?


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/10603/c1169987461@github.com>

Bram Moolenaar

unread,
Jun 29, 2022, 9:46:41 AM6/29/22
to vim...@googlegroups.com, ii14

> > I would think that we need an extra argument for the flag to not
> > position the cursor. Then the first, existing argument can be used as
> > before.
>
> @brammool The problem is that `getchar()` has the blocking behavior
> when the argument is skipped. I might be wrong, but I don't think you
> can skip the first argument (leaving it as `VAR_UNKNOWN`?) and just
> supply the second in vim script?

For that we have the value v:none. Thus:

c = getchar()

can be made to work the same as:

c = getchar(v:none)

The implementation needs to be adjusted for this to work.

--
Giving a commit hash to refer to a patch is like giving longitude and
lattitude to refer to a city.

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\

ii14

unread,
Aug 12, 2022, 10:30:26 AM8/12/22
to vim/vim, Subscribed

@brammool any decision on how you'd like arguments for getchar to work?


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/10603/c1213176113@github.com>

Bram Moolenaar

unread,
Aug 13, 2022, 7:17:33 AM8/13/22
to vim/vim, Subscribed

Looks like we can use -1 for the first argument to get the same behavior as when it is omitted (blocking wait).

One other option that can be useful is to always return a string. Currently it can return a number or a string, which can be clumsy to handle. Thus it's best to make the second argument a dictionary, with an entry for each flag: "move" for moving the cursor (default true), "number" for possibly returning a number (default true). How about that?


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/10603/c1214140457@github.com>

ii14

unread,
Aug 13, 2022, 7:30:45 AM8/13/22
to vim/vim, Subscribed

So with number=false it would basically be getcharstr()?


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/10603/c1214141917@github.com>

Bram Moolenaar

unread,
Aug 13, 2022, 8:35:12 AM8/13/22
to vim/vim, Subscribed

Hmm, it might be better to also add the extra argument to getcharstr() and not have the "number" flag. More consistent.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/10603/c1214149810@github.com>

ii14

unread,
Aug 13, 2022, 8:40:51 AM8/13/22
to vim/vim, Subscribed

What about getchar() accepting either a number or a dictionary as the first argument? A number to be backwards compatible and a dictionary for all the potential new features.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/10603/c1214150627@github.com>

Bram Moolenaar

unread,
Oct 11, 2022, 4:32:42 AM10/11/22
to vim/vim, Subscribed


> What about `getchar()` accepting either a number or a dictionary as
> the first argument? A number to be backwards compatible and a
> dictionary for all the potential new features.

An argument that can be a number or a dictionary is a bit strange, I
rather not do it like that.

--
You got to work at a mill? Lucky! I got sent back to work in the
acid-mines for my daily crust of stale bread... which not even the
birds would eat.

/// Bram Moolenaar -- ***@***.*** -- http://www.Moolenaar.net \\\

/// \\\
\\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/10603/c1274307514@github.com>

Christian Brabandt

unread,
Nov 3, 2024, 3:14:08 PM11/3/24
to vim/vim, Subscribed

Closed #10603.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/10603/issue_event/15096287866@github.com>

Christian Brabandt

unread,
Nov 3, 2024, 3:14:09 PM11/3/24
to vim/vim, Subscribed

This seems to have stalled and the tests seems to be wrong. So I am closing this for now. If you have a working version, please consider opening a new PR. thanks!


Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/10603/c2453562810@github.com>

Reply all
Reply to author
Forward
0 new messages