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
Message from discussion Unique winnr()
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 (wp == NULL)
        rettv->vval.v_number = -1;
     else
@@ -18508,7 +18668,7 @@
 {
     win_T      *wp;

-    wp = find_win_by_nr(&argvars[0], NULL);
+    wp = find_win_by_vp(&argvars[0], NULL);
     if (wp == NULL)
        rettv->vval.v_number = -1;
     else
@@ -18544,6 +18704,70 @@
 }

 /*
+ * "uwinnr()" function
+ */
+    static void
+f_uwinnr(argvars, rettv)
+    typval_T   *argvars UNUSED;
+    typval_T   *rettv;
+{
+    int                nr = 1;
+
+#ifdef FEAT_WINDOWS
+    nr = get_uwinnr(curtab, &argvars[0]);
+#endif
+    rettv->vval.v_number = nr;
+}
+
+/*
+ * "setwinbufnr()" function
+ */
+    static void
+f_setwinbufnr(argvars, rettv)
+    typval_T   *argvars UNUSED;
+    typval_T   *rettv;
+{
+    int                uwinnr;
+    int                ubufnr;
+    int                nr = 1;
+
+    uwinnr = (int)get_tv_number(&argvars[0]);
+    ubufnr = (int)get_tv_number(&argvars[1]);
+
+#ifdef FEAT_WINDOWS
+    setwinbufnr(uwinnr, ubufnr);
+#endif
+    rettv->vval.v_number = nr;
+}
+
+#ifdef FEAT_WINDOWS
+static void
+setwinbufnr(uwinnr,bufnr)
+    int                uwinnr;
+    int        bufnr;
+{
+    win_T      *window;
+    buf_T      *buffer;
+
+    window = NULL;
+    buffer = NULL;
+
+    buffer = buflist_findnr(bufnr);
+    window = find_win_by_uwinnr(uwinnr,NULL);
+
+    window->w_buffer = buffer;
+    redraw_win_later(window,must_redraw);
+    /* updateWindow(window); */
+    /* update_screen(must_redraw); */
+
+}
+#endif
+
+
+
+
+
+/*
  * "winrestcmd()" function
  */
     static void
@@ -18664,7 +18888,7 @@
 {
     win_T      *wp;

-    wp = find_win_by_nr(&argvars[0], NULL);
+    wp = find_win_by_vp(&argvars[0], NULL);
     if (wp == NULL)
        rettv->vval.v_number = -1;
     else
diff -r 61a7c3f01088 -r b0c51aaf018c src/structs.h
--- a/src/structs.h     Wed Oct 03 21:48:44 2012 +0200
+++ b/src/structs.h     Thu Oct 04 14:30:52 2012 -0500
@@ -1854,6 +1854,7 @@
  *
  * All row numbers are relative to the start of the window, except w_winrow.
  */
+static int top_window_num = 1;     /* Highest window number */
 struct window_S
 {
     buf_T      *w_buffer;          /* buffer we are a window into (used
@@ -1864,6 +1865,7 @@
 #endif

 #ifdef FEAT_WINDOWS
+    int         w_num;              /* unique window number. */
     win_T      *w_prev;            /* link to previous window */
     win_T      *w_next;            /* link to next window */
 #endif
diff -r 61a7c3f01088 -r b0c51aaf018c src/window.c
--- a/src/window.c      Wed Oct 03 21:48:44 2012 +0200
+++ b/src/window.c      Thu Oct 04 14:30:52 2012 -0500
@@ -8,6 +8,16 @@
  */

 #include "vim.h"
+void test_winnr(int i, int j);
+void test_winnr(int i,int j)
+{
+FILE *file;
+file = fopen("winnr.txt","a+");
+fprintf(file,"winnnumber is, %i called in position %i \n",i,j); /*writes*/
+fclose(file); /*done!*/
+}
+
+

 static int path_is_url __ARGS((char_u *p));
 #if defined(FEAT_WINDOWS) || defined(PROTO)
@@ -1249,6 +1259,9 @@
     ++newp->w_alist->al_refcount;
     newp->w_arg_idx = oldp->w_arg_idx;

+/*    newp->w_num = oldp->w_num;   /* top_window_num++; */
+test_winnr(newp->w_num,1);
+
     /* copy options from existing window */
     win_copy_options(oldp, newp);
 }
@@ -3298,6 +3311,9 @@
 #ifdef FEAT_VIRTUALEDIT
     wp->w_cursor.coladd = 0;
 #endif
+ /* test_winnr(wp->w_num,2);
+    wp->w_num=top_window_num++;
+ test_winnr(wp->w_num,2); */
     wp->w_pcmark.lnum = 1;     /* pcmark not cleared but set to line 1 */
     wp->w_pcmark.col = 0;
     wp->w_prev_pcmark.lnum = 0;
@@ -4010,6 +4026,27 @@
     return curwin;
 # endif
 }
+
+/*
+ * Find unique window number "uwinnr"
+ */
+    win_T *
+win_find_unr(uwinnr)
+    int                uwinnr;
+{
+    win_T      *wp;
+
+# ifdef FEAT_WINDOWS
+    for (wp = firstwin; wp != NULL; wp = wp->w_next)
+       if (wp->w_num == uwinnr)
+           break;
+    return wp;
+# else
+    return curwin;
+# endif
+}
+
+
 #endif

 #ifdef FEAT_VERTSPLIT
@@ -4382,6 +4419,10 @@
        new_wp->w_width = Columns;
 #endif

+test_winnr(new_wp->w_num,3);
+        new_wp->w_num = top_window_num++;
+test_winnr(new_wp->w_num,3);
+
        /* position the display and the cursor at the top of the file. */
        new_wp->w_topline = 1;
 #ifdef FEAT_DIFF
@@ -6680,3 +6721,7 @@
     return cur;
 }
 #endif
+
+
+
+


 
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.