Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Unique winnr()
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Brandon Coleman  
View profile  
 More options Oct 4 2012, 8:06 pm
From: Brandon Coleman <metrix1...@gmail.com>
Date: Thu, 4 Oct 2012 17:06:22 -0700 (PDT)
Local: Thurs, Oct 4 2012 8:06 pm
Subject: Unique winnr()

I had the idea of creating a command that would "link" two windows together so that whatever buffer is loaded in window 1 would be in the window 5 on tab 2.  The problem with this idea is that there is not a unique ID for each window,  So I came up with the proof of concept unique number patch below.  

But first a few question:
Is this something that is implemented and I just missed the code?  
Is this the correct way to implement a unique window ID?
Would the VIM community be open to accepting a patch if I brought this code up to standards?

Brandon

diff -r 61a7c3f01088 -r b0c51aaf018c src/eval.c
--- a/src/eval.c        Wed Oct 03 21:48:44 2012 +0200
+++ b/src/eval.c        Thu Oct 04 14:30:52 2012 -0500
@@ -409,6 +409,9 @@
 static void item_lock __ARGS((typval_T *tv, int deep, int lock));
 static int tv_islocked __ARGS((typval_T *tv));

+static void f_setwinbufnr __ARGS((typval_T *argvars, typval_T *rettv));
+static void setwinbufnr __ARGS((int uwinnr, int bufnr));
+
 static int eval0 __ARGS((char_u *arg,  typval_T *rettv, char_u **nextcmd, int e                                                                                                                               valuate));
 static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
 static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
@@ -484,6 +487,7 @@
 static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
 static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
 static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
+static void f_bufuwinnr __ARGS((typval_T *argvars, typval_T *rettv));
 static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
 static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
 static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
@@ -725,6 +729,7 @@
 static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
 static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
 static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
+static void f_utabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
 static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
 static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
 static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
@@ -750,6 +755,7 @@
 static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
 static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
 static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
+static void f_uwinnr __ARGS((typval_T *argvars, typval_T *rettv));
 static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
 static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
 static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
@@ -825,7 +831,9 @@
 static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
 static void free_funccal __ARGS((funccall_T *fc, int free_val));
 static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber                                                                                                                               _T nr));
-static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
+static win_T *find_win_by_vp __ARGS((typval_T *vp, tabpage_T *tp));
+static win_T *find_win_by_winnr __ARGS((int winnr, tabpage_T *tp));
+static win_T *find_win_by_uwinnr __ARGS((int winnr, tabpage_T *tp));
 static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
 static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
 static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp)                                                                                                                               );
@@ -7843,6 +7851,7 @@
     {"bufloaded",      1, 1, f_bufloaded},
     {"bufname",                1, 1, f_bufname},
     {"bufnr",          1, 2, f_bufnr},
+    {"bufuwinnr",      1, 1, f_bufuwinnr},
     {"bufwinnr",       1, 1, f_bufwinnr},
     {"byte2line",      1, 1, f_byte2line},
     {"byteidx",                2, 2, f_byteidx},
@@ -8049,6 +8058,7 @@
     {"setreg",         2, 3, f_setreg},
     {"settabvar",      3, 3, f_settabvar},
     {"settabwinvar",   4, 4, f_settabwinvar},
+    {"setwinbufnr",     2, 2, f_setwinbufnr},
     {"setwinvar",      3, 3, f_setwinvar},
     {"shellescape",    1, 2, f_shellescape},
     {"simplify",       1, 1, f_simplify},
@@ -8104,8 +8114,11 @@
     {"trunc",          1, 1, f_trunc},
 #endif
     {"type",           1, 1, f_type},
+    {"ubufnr",         1, 2, f_bufnr},
     {"undofile",       1, 1, f_undofile},
     {"undotree",       0, 0, f_undotree},
+    {"utabpagewinnr",  1, 2, f_utabpagewinnr},
+    {"uwinnr",         0, 1, f_uwinnr},
     {"values",         1, 1, f_values},
     {"virtcol",                1, 1, f_virtcol},
     {"visualmode",     0, 1, f_visualmode},
@@ -9123,6 +9136,46 @@
 }

 /*
+ * "bufwinnr(nr)" function
+ */
+    static void
+f_bufuwinnr(argvars, rettv)
+    typval_T   *argvars;
+    typval_T   *rettv;
+{
+#ifdef FEAT_WINDOWS
+    win_T      *wp;
+    int                winnr = 0;
+#endif
+    buf_T      *buf;
+
+    (void)get_tv_number(&argvars[0]);      /* issue errmsg if type error */
+    ++emsg_off;
+    buf = get_buf_tv(&argvars[0]);
+#ifdef FEAT_WINDOWS
+    for (wp = firstwin; wp; wp = wp->w_next)
+    {
+       if (wp->w_buffer == buf)
+            winnr = wp->w_num;
+           break;
+    }
+    rettv->vval.v_number = (wp != NULL ? winnr : -1);
+#else
+    rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
+#endif
+    --emsg_off;
+}
+
+
+
+
+
+
+
+
+
+
+/*
  * "byte2line(byte)" function
  */
     static void
@@ -11649,7 +11702,7 @@
        wp = NULL;
        if (argvars[0].v_type != VAR_UNKNOWN)   /* getloclist() */
        {
-           wp = find_win_by_nr(&argvars[0], NULL);
+           wp = find_win_by_vp(&argvars[0], NULL);
            if (wp == NULL)
                return;
        }
@@ -11819,18 +11872,29 @@
  * Find window specified by "vp" in tabpage "tp".
  */
     static win_T *
-find_win_by_nr(vp, tp)
+find_win_by_vp(vp, tp)
     typval_T   *vp;
     tabpage_T  *tp;        /* NULL for current tab page */
 {
+    int                nr;
+
+    nr = get_tv_number_chk(vp, NULL);
+
+    return find_win_by_winnr(nr,tp);
+}
+
+
+/*
+ * Find window specified by "winr" in tabpage "tp".
+ */
+    static win_T *
+find_win_by_winnr(nr, tp)
+    int                nr;
+    tabpage_T  *tp;        /* NULL for current tab page */
+{
 #ifdef FEAT_WINDOWS
     win_T      *wp;
-#endif
-    int                nr;
-
-    nr = get_tv_number_chk(vp, NULL);
-
-#ifdef FEAT_WINDOWS
+
     if (nr < 0)
        return NULL;
     if (nr == 0)
@@ -11848,6 +11912,39 @@
 #endif
 }

+
+/*
+ * Find window specified by "uwinnr" in tabpage "tp".
+ */
+    static win_T *
+find_win_by_uwinnr(nr, tp)
+    int                nr;
+    tabpage_T  *tp;        /* NULL for current tab page */
+{
+#ifdef FEAT_WINDOWS
+    win_T      *wp;
+
+    if (nr < 0)
+       return NULL;
+    if (nr == 0)
+       return curwin;
+
+    for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
+                                                 wp != NULL; wp = wp->w_next)
+       if (wp->w_num == nr)
+           break;
+    return wp;
+#else
+    if (nr == 0 || nr == 1)
+       return curwin;
+    return NULL;
+#endif
+}
+
+
+
+
+
 /*
  * "getwinvar()" function
  */
@@ -11879,7 +11976,7 @@
     else
        tp = curtab;
 #endif
-    win = find_win_by_nr(&argvars[off], tp);
+    win = find_win_by_vp(&argvars[off], tp);
     varname = get_tv_string_chk(&argvars[off + 1]);
     ++emsg_off;

@@ -16302,7 +16399,7 @@

     rettv->vval.v_number = -1;

-    win = find_win_by_nr(&argvars[0], NULL);
+    win = find_win_by_vp(&argvars[0], NULL);
     if (win != NULL)
        set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
 }
@@ -16583,7 +16680,7 @@
     else
        tp = curtab;
 #endif
-    win = find_win_by_nr(&argvars[off], tp);
+    win = find_win_by_vp(&argvars[off], tp);
     varname = get_tv_string_chk(&argvars[off + 1]);
     varp = &argvars[off + 2];

@@ -17906,8 +18003,71 @@

 #ifdef FEAT_WINDOWS
+static int get_uwinnr __ARGS((tabpage_T *tp, typval_T *argvar));
+
+/*
+ * Common code for utabpagewinnr() and uwinnr().
+ */
+    static int
+get_uwinnr(tp, argvar)
+    tabpage_T  *tp;
+    typval_T   *argvar;
+{
+    win_T      *twin;
+    int                nr = 1;
+    char_u     *arg;
+
+    twin = (tp == curtab) ? curwin : tp->tp_curwin;
+    if (argvar->v_type != VAR_UNKNOWN)
+    {
+       arg = get_tv_string_chk(argvar);
+       if (arg == NULL)
+           nr = 0;             /* type error; errmsg already given */
+       else if (STRCMP(arg, "$") == 0)
+           twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
+       else if (STRCMP(arg, "#") == 0)
+       {
+           twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
+           if (twin == NULL)
+               return 0;
+       }
+       else
+       {
+           EMSG2(_(e_invexpr2), arg);
+           nr = 0;
+       }
+    }
+    return twin->w_num;
+}
+#endif
+
+/*
+ * "utabpagewinnr()" function
+ */
+    static void
+f_utabpagewinnr(argvars, rettv)
+    typval_T   *argvars UNUSED;
+    typval_T   *rettv;
+{
+    int                nr = 1;
+#ifdef FEAT_WINDOWS
+    tabpage_T  *tp;
+
+    tp = find_tabpage((int)get_tv_number(&argvars[0]));
+    if (tp == NULL)
+       nr = 0;
+    else
+       nr = get_uwinnr(tp, &argvars[1]);
+#endif
+    rettv->vval.v_number = nr;
+}
+
+
+
+
 static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));

+#ifdef FEAT_WINDOWS
 /*
  * Common code for tabpagewinnr() and winnr().
  */
@@ -18479,7 +18639,7 @@
 {
     win_T      *wp;

-    wp = find_win_by_nr(&argvars[0], NULL);
+    wp = find_win_by_vp(&argvars[0], NULL);
     if
...

read more »


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ben Fritz  
View profile  
 More options Oct 4 2012, 9:35 pm
From: Ben Fritz <fritzophre...@gmail.com>
Date: Thu, 4 Oct 2012 18:35:40 -0700 (PDT)
Local: Thurs, Oct 4 2012 9:35 pm
Subject: Re: Unique winnr()

On Thursday, October 4, 2012 7:06:22 PM UTC-5, Brandon Coleman wrote:
> I had the idea of creating a command that would "link" two windows together so that whatever buffer is loaded in window 1 would be in the window 5 on tab 2.  The problem with this idea is that there is not a unique ID for each window,  So I came up with the proof of concept unique number patch below.  

> But first a few question:
> Is this something that is implemented and I just missed the code?  
> Is this the correct way to implement a unique window ID?
> Would the VIM community be open to accepting a patch if I brought this code up to standards?

Can you tell us why you think you need unique window IDs? This seems like something that could be done with a mixture of buffer-local, window-local, and/or tab-local variables or options.

At the very least, you should certainly not REPLACE the existing window number. The window number is set in a predictable way and can be used to switch to specific windows with <C-W><C-W>. Making the window number unique and static would make this impossible without first calling a function to figure out what the window number is.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Brandon Coleman  
View profile  
 More options Oct 5 2012, 4:26 am
From: Brandon Coleman <metrix1...@gmail.com>
Date: Fri, 5 Oct 2012 01:26:28 -0700 (PDT)
Local: Fri, Oct 5 2012 4:26 am
Subject: Re: Unique winnr()

1. bufnr() is unique, but winnr() and tabpagenr() are not.. I feel that this is confusing and unexpected from the point of view of someone creating a vim script.

2. The real problem for me is that find_win_by_winnr(4,null) returns the fourth window in the current page.  I can't find and work with a specific window.  

> At the very least, you should certainly not REPLACE the existing window number. The window number is set in a predictable way and can be used to switch to specific windows with <C-W><C-W>. Making the window number unique and static would make this impossible without first calling a function to figure out what the window number is.

This patch should create the function uwinnr(), leaving winnr() alone.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Andy Wokula  
View profile  
 More options Oct 5 2012, 4:51 am
From: Andy Wokula <anw...@yahoo.de>
Date: Fri, 05 Oct 2012 10:51:30 +0200
Local: Fri, Oct 5 2012 4:51 am
Subject: Re: Unique winnr()
Am 05.10.2012 10:26, schrieb Brandon Coleman:

As Ben said ...
You can "tag" a window by creating a window-local variable and you can
"tag" a tab page by creating a tab-page-local variable.  Both combined
result in "unique" window ID.

To find the window with this ID again, you have to go through all
windows in all tab pages (is this what you are worried about?) -- but
this is only the worst case, when the layout has changed.  Of course you
will check the last remembered tabpagenr+winnr first, then all windows
in the last remembered tab page, and after that all windows in all tab
pages.  It's something that a vimscript can do.

--
Andy

I'd like to know why everybody wants to further bloat Vim's core.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Brandon Coleman  
View profile  
 More options Oct 5 2012, 8:14 am
From: Brandon Coleman <metrix1...@gmail.com>
Date: Fri, 5 Oct 2012 05:14:22 -0700 (PDT)
Local: Fri, Oct 5 2012 8:14 am
Subject: Re: Unique winnr()

It seems like there should be a standard way of finding a window/buffer/tabpage without every script creating a new way of looping through windows.  Having said that, I can understand the need to keep bloat out of the project.

I will loop through windows.

Brandon


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »