Skip to content

Commit d4aa83a

Browse files
committed
patch 8.1.1307: cannot reconnect to the X server after it restarted
Problem: Cannot reconnect to the X server after it restarted. Solution: Add the :xrestore command. (Adrian Kocis, closes #844)
1 parent eae1b91 commit d4aa83a

File tree

10 files changed

+87
-8
lines changed

10 files changed

+87
-8
lines changed

runtime/doc/index.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1714,6 +1714,7 @@ tag command action ~
17141714
|:xmapclear| :xmapc[lear] remove all mappings for Visual mode
17151715
|:xmap| :xm[ap] like ":map" but for Visual mode
17161716
|:xmenu| :xme[nu] add menu for Visual mode
1717+
|:xrestore| :xr[estore] restores the X server connection
17171718
|:xnoremap| :xn[oremap] like ":noremap" but for Visual mode
17181719
|:xnoremenu| :xnoreme[nu] like ":noremenu" but for Visual mode
17191720
|:xunmap| :xu[nmap] like ":unmap" but for Visual mode

runtime/doc/various.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,21 @@ K Run a program to lookup the keyword under the
704704
available when compiled with the |+netbeans_intg|
705705
feature}
706706

707+
*:xrestore* *:xr*
708+
:xr[estore] [display] Reinitializes the connection to the X11 server. Useful
709+
after the X server restarts, e.g. when running Vim for
710+
long time inside screen/tmux and connecting from
711+
different machines).
712+
[display] should be in the format of the $DISPLAY
713+
environment variable (e.g. "localhost:10.0")
714+
If [display] is omitted, then it reinitializes the
715+
connection to the X11 server using the same value as
716+
was used for the previous execution of this command.
717+
If the value was never specified, then it uses the
718+
value of $DISPLAY environment variable as it was when
719+
Vim was started.
720+
{only available when compiled with the |+clipboard|
721+
feature}
707722

708723
*g_CTRL-A*
709724
g CTRL-A Only when Vim was compiled with MEM_PROFILING defined

src/ex_cmdidxs.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ static const unsigned short cmdidxs1[26] =
2929
/* v */ 503,
3030
/* w */ 521,
3131
/* x */ 535,
32-
/* y */ 544,
33-
/* z */ 545
32+
/* y */ 545,
33+
/* z */ 546
3434
};
3535

3636
/*
@@ -64,9 +64,9 @@ static const unsigned char cmdidxs2[26][26] =
6464
/* u */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
6565
/* v */ { 0, 0, 0, 0, 1, 0, 0, 0, 4, 0, 0, 0, 9, 12, 0, 0, 0, 0, 15, 0, 16, 0, 0, 0, 0, 0 },
6666
/* w */ { 2, 0, 0, 0, 0, 0, 0, 3, 4, 0, 0, 0, 0, 8, 0, 9, 10, 0, 0, 0, 12, 13, 0, 0, 0, 0 },
67-
/* x */ { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 5, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0 },
67+
/* x */ { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 5, 0, 0, 0, 7, 0, 0, 8, 0, 0, 0, 0, 0 },
6868
/* y */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
6969
/* z */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
7070
};
7171

72-
static const int command_count = 558;
72+
static const int command_count = 559;

src/ex_cmds.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1739,6 +1739,9 @@ EX(CMD_xnoremap, "xnoremap", ex_map,
17391739
EX(CMD_xnoremenu, "xnoremenu", ex_menu,
17401740
RANGE|ZEROR|EXTRA|TRLBAR|NOTRLCOM|USECTRLV|CMDWIN,
17411741
ADDR_OTHER),
1742+
EX(CMD_xrestore, "xrestore", ex_xrestore,
1743+
EXTRA|TRLBAR|CMDWIN,
1744+
ADDR_NONE),
17421745
EX(CMD_xunmap, "xunmap", ex_unmap,
17431746
EXTRA|TRLBAR|NOTRLCOM|USECTRLV|CMDWIN,
17441747
ADDR_NONE),

src/ex_docmd.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,9 @@ static void ex_folddo(exarg_T *eap);
394394
#ifndef FEAT_TERMINAL
395395
# define ex_terminal ex_ni
396396
#endif
397+
#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
398+
# define ex_xrestore ex_ni
399+
#endif
397400

398401
/*
399402
* Declare cmdnames[].

src/globals.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,9 +1279,14 @@ EXTERN linenr_T printer_page_num;
12791279
#endif
12801280

12811281
#ifdef FEAT_XCLIPBOARD
1282-
EXTERN char *xterm_display INIT(= NULL); /* xterm display name; points
1283-
into argv[] */
1284-
EXTERN Display *xterm_dpy INIT(= NULL); /* xterm display pointer */
1282+
// xterm display name
1283+
EXTERN char *xterm_display INIT(= NULL);
1284+
1285+
// whether xterm_display was allocated, when FALSE it points into argv[]
1286+
EXTERN int xterm_display_allocated INIT(= FALSE);
1287+
1288+
// xterm display pointer
1289+
EXTERN Display *xterm_dpy INIT(= NULL);
12851290
#endif
12861291
#if defined(FEAT_XCLIPBOARD) || defined(FEAT_GUI_X11)
12871292
EXTERN XtAppContext app_context INIT(= (XtAppContext)NULL);

src/os_unix.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1659,6 +1659,25 @@ may_restore_clipboard(void)
16591659
get_x11_title(FALSE);
16601660
}
16611661
}
1662+
1663+
void
1664+
ex_xrestore(exarg_T *eap)
1665+
{
1666+
if (eap->arg != NULL && STRLEN(eap->arg) > 0)
1667+
{
1668+
if (xterm_display_allocated)
1669+
vim_free(xterm_display);
1670+
xterm_display = (char *)vim_strsave(eap->arg);
1671+
xterm_display_allocated = TRUE;
1672+
}
1673+
smsg(_("restoring display %s"), xterm_display == NULL
1674+
? (char *)mch_getenv("DISPLAY") : xterm_display);
1675+
1676+
clear_xterm_clip();
1677+
x11_window = 0;
1678+
xterm_dpy_retry_count = 5; // Try reconnecting five times
1679+
may_restore_clipboard();
1680+
}
16621681
#endif
16631682

16641683
/*
@@ -1761,6 +1780,10 @@ get_x11_windis(void)
17611780
x11_window = (Window)atol(winid);
17621781

17631782
#ifdef FEAT_XCLIPBOARD
1783+
if (xterm_dpy == x11_display)
1784+
// x11_display may have been set to xterm_dpy elsewhere
1785+
x11_display_from = XD_XTERM;
1786+
17641787
if (xterm_dpy != NULL && x11_window != 0)
17651788
{
17661789
/* We may have checked it already, but Gnome terminal can move us to
@@ -7661,7 +7684,7 @@ do_xterm_trace(void)
76617684
return TRUE;
76627685
}
76637686

7664-
# if defined(FEAT_GUI) || defined(PROTO)
7687+
# if defined(FEAT_GUI) || defined(FEAT_XCLIPBOARD) || defined(PROTO)
76657688
/*
76667689
* Destroy the display, window and app_context. Required for GTK.
76677690
*/

src/proto/os_unix.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ void reset_signals(void);
1313
int vim_handle_signal(int sig);
1414
int mch_check_win(int argc, char **argv);
1515
int mch_input_isatty(void);
16+
void ex_xrestore(exarg_T *eap);
1617
int mch_can_restore_title(void);
1718
int mch_can_restore_icon(void);
1819
void mch_settitle(char_u *title, char_u *icon);

src/testdir/test_paste.vim

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,29 @@ func Test_paste_visual_mode()
110110

111111
bwipe!
112112
endfunc
113+
114+
func CheckCopyPaste()
115+
call setline(1, ['copy this', ''])
116+
normal 1G0"*y$
117+
normal j"*p
118+
call assert_equal('copy this', getline(2))
119+
endfunc
120+
121+
func Test_xrestore()
122+
if !has('xterm_clipboard')
123+
return
124+
endif
125+
call ch_logfile('logfile', 'w')
126+
let display = $DISPLAY
127+
new
128+
call CheckCopyPaste()
129+
130+
xrestore
131+
call CheckCopyPaste()
132+
133+
exe "xrestore " .. display
134+
call CheckCopyPaste()
135+
136+
call ch_logfile('', '')
137+
bwipe!
138+
endfunc

src/version.c

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

768768
static int included_patches[] =
769769
{ /* Add new patch number below this line */
770+
/**/
771+
1307,
770772
/**/
771773
1306,
772774
/**/

0 commit comments

Comments
 (0)