• R/O
  • SSH

vim: Commit

Mirror of the Vim source from https://github.com/vim/vim


Commit MetaInfo

Revisiona9f1656f13c9a400a31d489f73ccd08db831327a (tree)
Time2019-09-11 04:30:05
AuthorBram Moolenaar <Bram@vim....>
CommiterBram Moolenaar

Log Message

patch 8.1.2020: it is not easy to change the window layout

Commit: https://github.com/vim/vim/commit/d20dcb3d011da6111153109f6e46fbd5c7fe9fb6
Author: Bram Moolenaar <Bram@vim.org>
Date: Tue Sep 10 21:22:58 2019 +0200

patch 8.1.2020: it is not easy to change the window layout
Problem: It is not easy to change the window layout.
Solution: Add win_splitmove(). (Andy Massimino, closes https://github.com/vim/vim/issues/4561)

Change Summary

Incremental Difference

diff -r ceafe0986ad7 -r a9f1656f13c9 runtime/doc/eval.txt
--- a/runtime/doc/eval.txt Mon Sep 09 22:15:04 2019 +0200
+++ b/runtime/doc/eval.txt Tue Sep 10 21:30:05 2019 +0200
@@ -2875,6 +2875,8 @@
28752875 win_id2tabwin({expr}) List get tab and window nr from window ID
28762876 win_id2win({expr}) Number get window nr from window ID
28772877 win_screenpos({nr}) List get screen position of window {nr}
2878+win_splitmove({nr}, {target} [, {options}])
2879+ none move window {nr} to split of {target}
28782880 winbufnr({nr}) Number buffer number of window {nr}
28792881 wincol() Number window column of the cursor
28802882 winheight({nr}) Number height of window {nr}
@@ -10144,6 +10146,28 @@
1014410146 Can also be used as a |method|: >
1014510147 GetWinid()->win_screenpos()
1014610148 <
10149+win_splitmove({nr}, {target} [, {options}]) *win_splitmove()*
10150+ Move the window {nr} to a new split of the window {target}.
10151+ This is similar to moving to {target}, creating a new window
10152+ using |:split| but having the same contents as window {nr}, and
10153+ then closing {nr}.
10154+
10155+ Both {nr} and {target} can be window numbers or |window-ID|s.
10156+
10157+ Returns zero for success, non-zero for failure.
10158+
10159+ {options} is a Dictionary with the following optional entries:
10160+ "vertical" When TRUE, the split is created vertically,
10161+ like with |:vsplit|.
10162+ "rightbelow" When TRUE, the split is made below or to the
10163+ right (if vertical). When FALSE, it is done
10164+ above or to the left (if vertical). When not
10165+ present, the values of 'splitbelow' and
10166+ 'splitright' are used.
10167+
10168+ Can also be used as a |method|: >
10169+ GetWinid()->win_splitmove(target)
10170+<
1014710171 *winbufnr()*
1014810172 winbufnr({nr}) The result is a Number, which is the number of the buffer
1014910173 associated with window {nr}. {nr} can be the window number or
diff -r ceafe0986ad7 -r a9f1656f13c9 src/evalfunc.c
--- a/src/evalfunc.c Mon Sep 09 22:15:04 2019 +0200
+++ b/src/evalfunc.c Tue Sep 10 21:30:05 2019 +0200
@@ -850,6 +850,7 @@
850850 {"win_id2tabwin", 1, 1, FEARG_1, f_win_id2tabwin},
851851 {"win_id2win", 1, 1, FEARG_1, f_win_id2win},
852852 {"win_screenpos", 1, 1, FEARG_1, f_win_screenpos},
853+ {"win_splitmove", 2, 3, FEARG_1, f_win_splitmove},
853854 {"winbufnr", 1, 1, FEARG_1, f_winbufnr},
854855 {"wincol", 0, 0, 0, f_wincol},
855856 {"winheight", 1, 1, FEARG_1, f_winheight},
diff -r ceafe0986ad7 -r a9f1656f13c9 src/evalwindow.c
--- a/src/evalwindow.c Mon Sep 09 22:15:04 2019 +0200
+++ b/src/evalwindow.c Tue Sep 10 21:30:05 2019 +0200
@@ -744,6 +744,92 @@
744744 }
745745
746746 /*
747+ * Move the window wp into a new split of targetwin in a given direction
748+ */
749+ static void
750+win_move_into_split(win_T *wp, win_T *targetwin, int size, int flags)
751+{
752+ int dir;
753+ int height = wp->w_height;
754+ win_T *oldwin = curwin;
755+
756+ if (wp == targetwin)
757+ return;
758+
759+ // Jump to the target window
760+ if (curwin != targetwin)
761+ win_goto(targetwin);
762+
763+ // Remove the old window and frame from the tree of frames
764+ (void)winframe_remove(wp, &dir, NULL);
765+ win_remove(wp, NULL);
766+ last_status(FALSE); // may need to remove last status line
767+ (void)win_comp_pos(); // recompute window positions
768+
769+ // Split a window on the desired side and put the old window there
770+ (void)win_split_ins(size, flags, wp, dir);
771+
772+ // If splitting horizontally, try to preserve height
773+ if (size == 0 && !(flags & WSP_VERT))
774+ {
775+ win_setheight_win(height, wp);
776+ if (p_ea)
777+ win_equal(wp, TRUE, 'v');
778+ }
779+
780+#if defined(FEAT_GUI)
781+ // When 'guioptions' includes 'L' or 'R' may have to remove or add
782+ // scrollbars. Have to update them anyway.
783+ gui_may_update_scrollbars();
784+#endif
785+
786+ if (oldwin != curwin)
787+ win_goto(oldwin);
788+}
789+
790+/*
791+ * "win_splitmove()" function
792+ */
793+ void
794+f_win_splitmove(typval_T *argvars, typval_T *rettv)
795+{
796+ win_T *wp;
797+ win_T *targetwin;
798+ int flags = 0, size = 0;
799+
800+ wp = find_win_by_nr_or_id(&argvars[0]);
801+ targetwin = find_win_by_nr_or_id(&argvars[1]);
802+
803+ if (wp == NULL || targetwin == NULL || wp == targetwin)
804+ {
805+ emsg(_(e_invalwindow));
806+ rettv->vval.v_number = -1;
807+ return;
808+ }
809+
810+ if (argvars[2].v_type != VAR_UNKNOWN)
811+ {
812+ dict_T *d;
813+ dictitem_T *di;
814+
815+ if (argvars[2].v_type != VAR_DICT || argvars[2].vval.v_dict == NULL)
816+ {
817+ emsg(_(e_invarg));
818+ return;
819+ }
820+
821+ d = argvars[2].vval.v_dict;
822+ if (dict_get_number(d, (char_u *)"vertical"))
823+ flags |= WSP_VERT;
824+ if ((di = dict_find(d, (char_u *)"rightbelow", -1)) != NULL)
825+ flags |= tv_get_number(&di->di_tv) ? WSP_BELOW : WSP_ABOVE;
826+ size = (int)dict_get_number(d, (char_u *)"size");
827+ }
828+
829+ win_move_into_split(wp, targetwin, size, flags);
830+}
831+
832+/*
747833 * "winbufnr(nr)" function
748834 */
749835 void
diff -r ceafe0986ad7 -r a9f1656f13c9 src/proto/evalwindow.pro
--- a/src/proto/evalwindow.pro Mon Sep 09 22:15:04 2019 +0200
+++ b/src/proto/evalwindow.pro Tue Sep 10 21:30:05 2019 +0200
@@ -19,6 +19,7 @@
1919 void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
2020 void f_win_id2win(typval_T *argvars, typval_T *rettv);
2121 void f_win_screenpos(typval_T *argvars, typval_T *rettv);
22+void f_win_splitmove(typval_T *argvars, typval_T *rettv);
2223 void f_winbufnr(typval_T *argvars, typval_T *rettv);
2324 void f_wincol(typval_T *argvars, typval_T *rettv);
2425 void f_winheight(typval_T *argvars, typval_T *rettv);
diff -r ceafe0986ad7 -r a9f1656f13c9 src/testdir/test_window_cmd.vim
--- a/src/testdir/test_window_cmd.vim Mon Sep 09 22:15:04 2019 +0200
+++ b/src/testdir/test_window_cmd.vim Tue Sep 10 21:30:05 2019 +0200
@@ -857,4 +857,37 @@
857857 bwipe!
858858 endfunc
859859
860+func Test_win_splitmove()
861+ edit a
862+ leftabove split b
863+ leftabove vsplit c
864+ leftabove split d
865+ call assert_equal(0, win_splitmove(winnr(), winnr('l')))
866+ call assert_equal(bufname(winbufnr(1)), 'c')
867+ call assert_equal(bufname(winbufnr(2)), 'd')
868+ call assert_equal(bufname(winbufnr(3)), 'b')
869+ call assert_equal(bufname(winbufnr(4)), 'a')
870+ call assert_equal(0, win_splitmove(winnr(), winnr('j'), {'vertical': 1}))
871+ call assert_equal(0, win_splitmove(winnr(), winnr('j'), {'vertical': 1}))
872+ call assert_equal(bufname(winbufnr(1)), 'c')
873+ call assert_equal(bufname(winbufnr(2)), 'b')
874+ call assert_equal(bufname(winbufnr(3)), 'd')
875+ call assert_equal(bufname(winbufnr(4)), 'a')
876+ call assert_equal(0, win_splitmove(winnr(), winnr('k'), {'vertical': 1}))
877+ call assert_equal(bufname(winbufnr(1)), 'd')
878+ call assert_equal(bufname(winbufnr(2)), 'c')
879+ call assert_equal(bufname(winbufnr(3)), 'b')
880+ call assert_equal(bufname(winbufnr(4)), 'a')
881+ call assert_equal(0, win_splitmove(winnr(), winnr('j'), {'rightbelow': v:true}))
882+ call assert_equal(bufname(winbufnr(1)), 'c')
883+ call assert_equal(bufname(winbufnr(2)), 'b')
884+ call assert_equal(bufname(winbufnr(3)), 'a')
885+ call assert_equal(bufname(winbufnr(4)), 'd')
886+ only | bd
887+
888+ call assert_fails('call win_splitmove(winnr(), 123)', 'E957:')
889+ call assert_fails('call win_splitmove(123, winnr())', 'E957:')
890+ call assert_fails('call win_splitmove(winnr(), winnr())', 'E957:')
891+endfunc
892+
860893 " vim: shiftwidth=2 sts=2 expandtab
diff -r ceafe0986ad7 -r a9f1656f13c9 src/version.c
--- a/src/version.c Mon Sep 09 22:15:04 2019 +0200
+++ b/src/version.c Tue Sep 10 21:30:05 2019 +0200
@@ -758,6 +758,8 @@
758758 static int included_patches[] =
759759 { /* Add new patch number below this line */
760760 /**/
761+ 2020,
762+/**/
761763 2019,
762764 /**/
763765 2018,
Show on old repository browser