Skip to content

Commit 9787000

Browse files
committed
patch 8.0.0818: cannot get the cursor position of a terminal
Problem: Cannot get the cursor position of a terminal. Solution: Add term_getcursor().
1 parent 22aad2f commit 9787000

5 files changed

Lines changed: 50 additions & 13 deletions

File tree

runtime/doc/eval.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2370,6 +2370,7 @@ tan({expr}) Float tangent of {expr}
23702370
tanh({expr}) Float hyperbolic tangent of {expr}
23712371
tempname() String name for a temporary file
23722372
term_getattr({attr}, {what} Number get the value of attribute {what}
2373+
term_getcursor({buf}) List get the cursor position of a terminal
23732374
term_getjob({buf}) Job get the job associated with a terminal
23742375
term_getline({buf}[, {row}]) String get a line of text from a terminal
23752376
term_getsize({buf}) List get the size of a terminal
@@ -7910,6 +7911,19 @@ term_getattr({attr}, {what}) *term_getattr()*
79107911
strike
79117912
reverse
79127913

7914+
term_getcursor({buf}) *term_getcursor()*
7915+
Get the cusor position of terminal {buf}. Returns a list with
7916+
three numbers: [rows, cols, visible]. "rows" and "cols" are
7917+
zero based. "visible" is one when the cursor is visible, zero
7918+
when it is hidden.
7919+
7920+
This is the cursor position of the terminal itself, not of the
7921+
Vim window.
7922+
7923+
{buf} must be the buffer number of a terminal window. If the
7924+
buffer does not exist or is not a terminal window, an empty
7925+
list is returned.
7926+
79137927
term_getjob({buf}) *term_getjob()*
79147928
Get the Job associated with terminal window {buf}.
79157929
{buf} is used as with |term_getsize()|.

src/evalfunc.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,7 @@ static struct fst
832832
{"tempname", 0, 0, f_tempname},
833833
#ifdef FEAT_TERMINAL
834834
{"term_getattr", 2, 2, f_term_getattr},
835+
{"term_getcursor", 1, 1, f_term_getcursor},
835836
{"term_getjob", 1, 1, f_term_getjob},
836837
{"term_getline", 1, 2, f_term_getline},
837838
{"term_getsize", 1, 1, f_term_getsize},

src/proto/terminal.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ int term_get_attr(buf_T *buf, linenr_T lnum, int col);
1616
char_u *term_get_status_text(term_T *term);
1717
int set_ref_in_term(int copyID);
1818
void f_term_getattr(typval_T *argvars, typval_T *rettv);
19+
void f_term_getcursor(typval_T *argvars, typval_T *rettv);
1920
void f_term_getjob(typval_T *argvars, typval_T *rettv);
2021
void f_term_getline(typval_T *argvars, typval_T *rettv);
2122
void f_term_getsize(typval_T *argvars, typval_T *rettv);

src/terminal.c

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@
5353
* :term <24x80> <close> vim notes.txt
5454
* - To set BS correctly, check get_stty(); Pass the fd of the pty.
5555
* - do not store terminal window in viminfo. Or prefix term:// ?
56-
* - add term_getcursor() - return cursor position: [row, col, visible]
5756
* - add a character in :ls output
5857
* - add 't' to mode()
5958
* - when closing window and job has not ended, make terminal hidden?
@@ -1636,6 +1635,24 @@ set_ref_in_term(int copyID)
16361635
return abort;
16371636
}
16381637

1638+
/*
1639+
* Get the buffer from the first argument in "argvars".
1640+
* Returns NULL when the buffer is not for a terminal window.
1641+
*/
1642+
static buf_T *
1643+
term_get_buf(typval_T *argvars)
1644+
{
1645+
buf_T *buf;
1646+
1647+
(void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1648+
++emsg_off;
1649+
buf = get_buf_tv(&argvars[0], FALSE);
1650+
--emsg_off;
1651+
if (buf == NULL || buf->b_term == NULL)
1652+
return NULL;
1653+
return buf;
1654+
}
1655+
16391656
/*
16401657
* "term_getattr(attr, name)" function
16411658
*/
@@ -1671,21 +1688,23 @@ f_term_getattr(typval_T *argvars, typval_T *rettv)
16711688
}
16721689

16731690
/*
1674-
* Get the buffer from the first argument in "argvars".
1675-
* Returns NULL when the buffer is not for a terminal window.
1691+
* "term_getcursor(buf)" function
16761692
*/
1677-
static buf_T *
1678-
term_get_buf(typval_T *argvars)
1693+
void
1694+
f_term_getcursor(typval_T *argvars, typval_T *rettv)
16791695
{
1680-
buf_T *buf;
1696+
buf_T *buf = term_get_buf(argvars);
1697+
list_T *l;
16811698

1682-
(void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1683-
++emsg_off;
1684-
buf = get_buf_tv(&argvars[0], FALSE);
1685-
--emsg_off;
1686-
if (buf == NULL || buf->b_term == NULL)
1687-
return NULL;
1688-
return buf;
1699+
if (rettv_list_alloc(rettv) == FAIL)
1700+
return;
1701+
if (buf == NULL)
1702+
return;
1703+
1704+
l = rettv->vval.v_list;
1705+
list_append_number(l, buf->b_term->tl_cursor_pos.row);
1706+
list_append_number(l, buf->b_term->tl_cursor_pos.col);
1707+
list_append_number(l, buf->b_term->tl_cursor_visible);
16891708
}
16901709

16911710
/*

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,8 @@ static char *(features[]) =
769769

770770
static int included_patches[] =
771771
{ /* Add new patch number below this line */
772+
/**/
773+
818,
772774
/**/
773775
817,
774776
/**/

0 commit comments

Comments
 (0)