Mercurial > vim
annotate src/if_xcmdsrv.c @ 12987:1ffbd9b53be0
Added tag v8.0.1369 for changeset 18e6f4addce91f5c103828c598a79f66e11b2846
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Tue, 05 Dec 2017 13:30:05 +0100 |
parents | de1ae9d8ef57 |
children | 8412df1479a3 |
rev | line source |
---|---|
10042
4aead6a9b7a9
commit https://github.com/vim/vim/commit/edf3f97ae2af024708ebb4ac614227327033ca47
Christian Brabandt <cb@256bit.org>
parents:
8732
diff
changeset
|
1 /* vi:set ts=8 sts=4 sw=4 noet: |
7 | 2 * |
3 * VIM - Vi IMproved by Bram Moolenaar | |
4 * X command server by Flemming Madsen | |
5 * | |
6 * Do ":help uganda" in Vim to read copying and usage conditions. | |
7 * Do ":help credits" in Vim to see a list of people who contributed. | |
8 * See README.txt for an overview of the Vim source code. | |
9 * | |
10 * if_xcmdsrv.c: Functions for passing commands through an X11 display. | |
11 * | |
12 */ | |
13 | |
14 #include "vim.h" | |
15 #include "version.h" | |
16 | |
17 #if defined(FEAT_CLIENTSERVER) || defined(PROTO) | |
18 | |
19 # ifdef FEAT_X11 | |
20 # include <X11/Intrinsic.h> | |
21 # include <X11/Xatom.h> | |
22 # endif | |
23 | |
24 /* | |
1121 | 25 * This file provides procedures that implement the command server |
26 * functionality of Vim when in contact with an X11 server. | |
7 | 27 * |
28 * Adapted from TCL/TK's send command in tkSend.c of the tk 3.6 distribution. | |
29 * Adapted for use in Vim by Flemming Madsen. Protocol changed to that of tk 4 | |
30 */ | |
31 | |
32 /* | |
33 * Copyright (c) 1989-1993 The Regents of the University of California. | |
34 * All rights reserved. | |
35 * | |
36 * Permission is hereby granted, without written agreement and without | |
37 * license or royalty fees, to use, copy, modify, and distribute this | |
38 * software and its documentation for any purpose, provided that the | |
39 * above copyright notice and the following two paragraphs appear in | |
40 * all copies of this software. | |
41 * | |
42 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR | |
43 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT | |
44 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF | |
45 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
46 * | |
47 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, | |
48 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY | |
49 * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS | |
50 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO | |
51 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. | |
52 */ | |
53 | |
54 | |
55 /* | |
56 * When a result is being awaited from a sent command, one of | |
57 * the following structures is present on a list of all outstanding | |
58 * sent commands. The information in the structure is used to | |
59 * process the result when it arrives. You're probably wondering | |
60 * how there could ever be multiple outstanding sent commands. | |
61 * This could happen if Vim instances invoke each other recursively. | |
62 * It's unlikely, but possible. | |
63 */ | |
64 | |
65 typedef struct PendingCommand | |
66 { | |
67 int serial; /* Serial number expected in result. */ | |
68 int code; /* Result Code. 0 is OK */ | |
69 char_u *result; /* String result for command (malloc'ed). | |
70 * NULL means command still pending. */ | |
71 struct PendingCommand *nextPtr; | |
72 /* Next in list of all outstanding commands. | |
73 * NULL means end of list. */ | |
74 } PendingCommand; | |
75 | |
76 static PendingCommand *pendingCommands = NULL; | |
77 /* List of all commands currently | |
78 * being waited for. */ | |
79 | |
80 /* | |
81 * The information below is used for communication between processes | |
82 * during "send" commands. Each process keeps a private window, never | |
83 * even mapped, with one property, "Comm". When a command is sent to | |
84 * an interpreter, the command is appended to the comm property of the | |
85 * communication window associated with the interp's process. Similarly, | |
86 * when a result is returned from a sent command, it is also appended | |
87 * to the comm property. | |
88 * | |
89 * Each command and each result takes the form of ASCII text. For a | |
90 * command, the text consists of a nul character followed by several | |
39 | 91 * nul-terminated ASCII strings. The first string consists of a |
92 * single letter: | |
93 * "c" for an expression | |
94 * "k" for keystrokes | |
95 * "r" for reply | |
96 * "n" for notification. | |
97 * Subsequent strings have the form "option value" where the following options | |
98 * are supported: | |
7 | 99 * |
100 * -r commWindow serial | |
101 * | |
102 * This option means that a response should be sent to the window | |
103 * whose X identifier is "commWindow" (in hex), and the response should | |
104 * be identified with the serial number given by "serial" (in decimal). | |
105 * If this option isn't specified then the send is asynchronous and | |
106 * no response is sent. | |
107 * | |
108 * -n name | |
109 * "Name" gives the name of the application for which the command is | |
110 * intended. This option must be present. | |
111 * | |
39 | 112 * -E encoding |
113 * Encoding name used for the text. This is the 'encoding' of the | |
114 * sender. The receiver may want to do conversion to his 'encoding'. | |
115 * | |
7 | 116 * -s script |
117 * "Script" is the script to be executed. This option must be | |
118 * present. Taken as a series of keystrokes in a "k" command where | |
119 * <Key>'s are expanded | |
120 * | |
121 * The options may appear in any order. The -n and -s options must be | |
122 * present, but -r may be omitted for asynchronous RPCs. For compatibility | |
123 * with future releases that may add new features, there may be additional | |
124 * options present; as long as they start with a "-" character, they will | |
125 * be ignored. | |
126 * | |
127 * A result also consists of a zero character followed by several null- | |
128 * terminated ASCII strings. The first string consists of the single | |
129 * letter "r". Subsequent strings have the form "option value" where | |
130 * the following options are supported: | |
131 * | |
132 * -s serial | |
133 * Identifies the command for which this is the result. It is the | |
134 * same as the "serial" field from the -s option in the command. This | |
135 * option must be present. | |
136 * | |
137 * -r result | |
138 * "Result" is the result string for the script, which may be either | |
139 * a result or an error message. If this field is omitted then it | |
140 * defaults to an empty string. | |
141 * | |
142 * -c code | |
143 * 0: for OK. This is the default. | |
144 * 1: for error: Result is the last error | |
145 * | |
146 * -i errorInfo | |
147 * -e errorCode | |
148 * Not applicable for Vim | |
149 * | |
150 * Options may appear in any order, and only the -s option must be | |
151 * present. As with commands, there may be additional options besides | |
152 * these; unknown options are ignored. | |
153 */ | |
154 | |
155 /* | |
156 * Maximum size property that can be read at one time by | |
157 * this module: | |
158 */ | |
159 | |
160 #define MAX_PROP_WORDS 100000 | |
161 | |
162 struct ServerReply | |
163 { | |
164 Window id; | |
165 garray_T strings; | |
166 }; | |
167 static garray_T serverReply = { 0, 0, 0, 0, 0 }; | |
168 enum ServerReplyOp { SROP_Find, SROP_Add, SROP_Delete }; | |
169 | |
7803
37c929c4a073
commit https://github.com/vim/vim/commit/92b8b2d307e34117f146319872010b0ccc9d2713
Christian Brabandt <cb@256bit.org>
parents:
7172
diff
changeset
|
170 typedef int (*EndCond)(void *); |
7 | 171 |
7109
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
172 struct x_cmdqueue |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
173 { |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
174 char_u *propInfo; |
7123
077ae8b63e15
commit https://github.com/vim/vim/commit/b8603882b1679385b287f14c527fa61eee60a9dd
Christian Brabandt <cb@256bit.org>
parents:
7109
diff
changeset
|
175 long_u len; |
7109
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
176 struct x_cmdqueue *next; |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
177 struct x_cmdqueue *prev; |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
178 }; |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
179 |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
180 typedef struct x_cmdqueue x_queue_T; |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
181 |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
182 /* dummy node, header for circular queue */ |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
183 static x_queue_T head = {NULL, 0, NULL, NULL}; |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
184 |
7 | 185 /* |
186 * Forward declarations for procedures defined later in this file: | |
187 */ | |
188 | |
7803
37c929c4a073
commit https://github.com/vim/vim/commit/92b8b2d307e34117f146319872010b0ccc9d2713
Christian Brabandt <cb@256bit.org>
parents:
7172
diff
changeset
|
189 static Window LookupName(Display *dpy, char_u *name, int delete, char_u **loose); |
37c929c4a073
commit https://github.com/vim/vim/commit/92b8b2d307e34117f146319872010b0ccc9d2713
Christian Brabandt <cb@256bit.org>
parents:
7172
diff
changeset
|
190 static int SendInit(Display *dpy); |
37c929c4a073
commit https://github.com/vim/vim/commit/92b8b2d307e34117f146319872010b0ccc9d2713
Christian Brabandt <cb@256bit.org>
parents:
7172
diff
changeset
|
191 static int DoRegisterName(Display *dpy, char_u *name); |
37c929c4a073
commit https://github.com/vim/vim/commit/92b8b2d307e34117f146319872010b0ccc9d2713
Christian Brabandt <cb@256bit.org>
parents:
7172
diff
changeset
|
192 static void DeleteAnyLingerer(Display *dpy, Window w); |
37c929c4a073
commit https://github.com/vim/vim/commit/92b8b2d307e34117f146319872010b0ccc9d2713
Christian Brabandt <cb@256bit.org>
parents:
7172
diff
changeset
|
193 static int GetRegProp(Display *dpy, char_u **regPropp, long_u *numItemsp, int domsg); |
37c929c4a073
commit https://github.com/vim/vim/commit/92b8b2d307e34117f146319872010b0ccc9d2713
Christian Brabandt <cb@256bit.org>
parents:
7172
diff
changeset
|
194 static int WaitForPend(void *p); |
37c929c4a073
commit https://github.com/vim/vim/commit/92b8b2d307e34117f146319872010b0ccc9d2713
Christian Brabandt <cb@256bit.org>
parents:
7172
diff
changeset
|
195 static int WaitForReply(void *p); |
37c929c4a073
commit https://github.com/vim/vim/commit/92b8b2d307e34117f146319872010b0ccc9d2713
Christian Brabandt <cb@256bit.org>
parents:
7172
diff
changeset
|
196 static int WindowValid(Display *dpy, Window w); |
37c929c4a073
commit https://github.com/vim/vim/commit/92b8b2d307e34117f146319872010b0ccc9d2713
Christian Brabandt <cb@256bit.org>
parents:
7172
diff
changeset
|
197 static void ServerWait(Display *dpy, Window w, EndCond endCond, void *endData, int localLoop, int seconds); |
37c929c4a073
commit https://github.com/vim/vim/commit/92b8b2d307e34117f146319872010b0ccc9d2713
Christian Brabandt <cb@256bit.org>
parents:
7172
diff
changeset
|
198 static struct ServerReply *ServerReplyFind(Window w, enum ServerReplyOp op); |
37c929c4a073
commit https://github.com/vim/vim/commit/92b8b2d307e34117f146319872010b0ccc9d2713
Christian Brabandt <cb@256bit.org>
parents:
7172
diff
changeset
|
199 static int AppendPropCarefully(Display *display, Window window, Atom property, char_u *value, int length); |
37c929c4a073
commit https://github.com/vim/vim/commit/92b8b2d307e34117f146319872010b0ccc9d2713
Christian Brabandt <cb@256bit.org>
parents:
7172
diff
changeset
|
200 static int x_error_check(Display *dpy, XErrorEvent *error_event); |
37c929c4a073
commit https://github.com/vim/vim/commit/92b8b2d307e34117f146319872010b0ccc9d2713
Christian Brabandt <cb@256bit.org>
parents:
7172
diff
changeset
|
201 static int IsSerialName(char_u *name); |
37c929c4a073
commit https://github.com/vim/vim/commit/92b8b2d307e34117f146319872010b0ccc9d2713
Christian Brabandt <cb@256bit.org>
parents:
7172
diff
changeset
|
202 static void save_in_queue(char_u *buf, long_u len); |
37c929c4a073
commit https://github.com/vim/vim/commit/92b8b2d307e34117f146319872010b0ccc9d2713
Christian Brabandt <cb@256bit.org>
parents:
7172
diff
changeset
|
203 static void server_parse_message(Display *dpy, char_u *propInfo, long_u numItems); |
7 | 204 |
205 /* Private variables for the "server" functionality */ | |
206 static Atom registryProperty = None; | |
207 static Atom vimProperty = None; | |
208 static int got_x_error = FALSE; | |
209 | |
210 static char_u *empty_prop = (char_u *)""; /* empty GetRegProp() result */ | |
211 | |
212 /* | |
213 * Associate an ASCII name with Vim. Try real hard to get a unique one. | |
214 * Returns FAIL or OK. | |
215 */ | |
216 int | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
217 serverRegisterName( |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
218 Display *dpy, /* display to register with */ |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
219 char_u *name) /* the name that will be used as a base */ |
7 | 220 { |
221 int i; | |
222 int res; | |
223 char_u *p = NULL; | |
224 | |
225 res = DoRegisterName(dpy, name); | |
226 if (res < 0) | |
227 { | |
228 i = 1; | |
229 do | |
230 { | |
231 if (res < -1 || i >= 1000) | |
232 { | |
233 MSG_ATTR(_("Unable to register a command server name"), | |
11158
501f46f7644c
patch 8.0.0466: still macros that should be all-caps
Christian Brabandt <cb@256bit.org>
parents:
10942
diff
changeset
|
234 HL_ATTR(HLF_W)); |
7 | 235 return FAIL; |
236 } | |
237 if (p == NULL) | |
238 p = alloc(STRLEN(name) + 10); | |
239 if (p == NULL) | |
240 { | |
241 res = -10; | |
242 continue; | |
243 } | |
244 sprintf((char *)p, "%s%d", name, i++); | |
245 res = DoRegisterName(dpy, p); | |
246 } | |
247 while (res < 0) | |
248 ; | |
249 vim_free(p); | |
250 } | |
251 return OK; | |
252 } | |
253 | |
254 static int | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
255 DoRegisterName(Display *dpy, char_u *name) |
7 | 256 { |
257 Window w; | |
258 XErrorHandler old_handler; | |
259 #define MAX_NAME_LENGTH 100 | |
260 char_u propInfo[MAX_NAME_LENGTH + 20]; | |
261 | |
262 if (commProperty == None) | |
263 { | |
264 if (SendInit(dpy) < 0) | |
265 return -2; | |
266 } | |
267 | |
268 /* | |
269 * Make sure the name is unique, and append info about it to | |
270 * the registry property. It's important to lock the server | |
271 * here to prevent conflicting changes to the registry property. | |
272 * WARNING: Do not step through this while debugging, it will hangup the X | |
273 * server! | |
274 */ | |
275 XGrabServer(dpy); | |
276 w = LookupName(dpy, name, FALSE, NULL); | |
277 if (w != (Window)0) | |
278 { | |
279 Status status; | |
280 int dummyInt; | |
281 unsigned int dummyUns; | |
282 Window dummyWin; | |
283 | |
284 /* | |
285 * The name is currently registered. See if the commWindow | |
286 * associated with the name exists. If not, or if the commWindow | |
287 * is *our* commWindow, then just unregister the old name (this | |
288 * could happen if an application dies without cleaning up the | |
289 * registry). | |
290 */ | |
291 old_handler = XSetErrorHandler(x_error_check); | |
292 status = XGetGeometry(dpy, w, &dummyWin, &dummyInt, &dummyInt, | |
293 &dummyUns, &dummyUns, &dummyUns, &dummyUns); | |
294 (void)XSetErrorHandler(old_handler); | |
295 if (status != Success && w != commWindow) | |
296 { | |
297 XUngrabServer(dpy); | |
298 XFlush(dpy); | |
299 return -1; | |
300 } | |
301 (void)LookupName(dpy, name, /*delete=*/TRUE, NULL); | |
302 } | |
303 sprintf((char *)propInfo, "%x %.*s", (int_u)commWindow, | |
304 MAX_NAME_LENGTH, name); | |
305 old_handler = XSetErrorHandler(x_error_check); | |
306 got_x_error = FALSE; | |
307 XChangeProperty(dpy, RootWindow(dpy, 0), registryProperty, XA_STRING, 8, | |
308 PropModeAppend, propInfo, STRLEN(propInfo) + 1); | |
309 XUngrabServer(dpy); | |
310 XSync(dpy, False); | |
311 (void)XSetErrorHandler(old_handler); | |
312 | |
313 if (!got_x_error) | |
314 { | |
315 #ifdef FEAT_EVAL | |
316 set_vim_var_string(VV_SEND_SERVER, name, -1); | |
317 #endif | |
318 serverName = vim_strsave(name); | |
319 #ifdef FEAT_TITLE | |
320 need_maketitle = TRUE; | |
321 #endif | |
322 return 0; | |
323 } | |
324 return -2; | |
325 } | |
326 | |
327 #if defined(FEAT_GUI) || defined(PROTO) | |
328 /* | |
329 * Clean out new ID from registry and set it as comm win. | |
330 * Change any registered window ID. | |
331 */ | |
332 void | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
333 serverChangeRegisteredWindow( |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
334 Display *dpy, /* Display to register with */ |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
335 Window newwin) /* Re-register to this ID */ |
7 | 336 { |
337 char_u propInfo[MAX_NAME_LENGTH + 20]; | |
338 | |
339 commWindow = newwin; | |
340 | |
341 /* Always call SendInit() here, to make sure commWindow is marked as a Vim | |
342 * window. */ | |
343 if (SendInit(dpy) < 0) | |
344 return; | |
345 | |
346 /* WARNING: Do not step through this while debugging, it will hangup the X | |
347 * server! */ | |
348 XGrabServer(dpy); | |
349 DeleteAnyLingerer(dpy, newwin); | |
350 if (serverName != NULL) | |
351 { | |
352 /* Reinsert name if we was already registered */ | |
353 (void)LookupName(dpy, serverName, /*delete=*/TRUE, NULL); | |
354 sprintf((char *)propInfo, "%x %.*s", | |
355 (int_u)newwin, MAX_NAME_LENGTH, serverName); | |
356 XChangeProperty(dpy, RootWindow(dpy, 0), registryProperty, XA_STRING, 8, | |
357 PropModeAppend, (char_u *)propInfo, | |
358 STRLEN(propInfo) + 1); | |
359 } | |
360 XUngrabServer(dpy); | |
361 } | |
362 #endif | |
363 | |
364 /* | |
365 * Send to an instance of Vim via the X display. | |
366 * Returns 0 for OK, negative for an error. | |
367 */ | |
368 int | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
369 serverSendToVim( |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
370 Display *dpy, /* Where to send. */ |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
371 char_u *name, /* Where to send. */ |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
372 char_u *cmd, /* What to send. */ |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
373 char_u **result, /* Result of eval'ed expression */ |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
374 Window *server, /* Actual ID of receiving app */ |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
375 Bool asExpr, /* Interpret as keystrokes or expr ? */ |
11211
71311d899b42
patch 8.0.0492: a failing client-server request can make Vim hang
Christian Brabandt <cb@256bit.org>
parents:
11181
diff
changeset
|
376 int timeout, /* seconds to wait or zero */ |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
377 Bool localLoop, /* Throw away everything but result */ |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
378 int silent) /* don't complain about no server */ |
7 | 379 { |
380 Window w; | |
381 char_u *property; | |
382 int length; | |
383 int res; | |
384 static int serial = 0; /* Running count of sent commands. | |
385 * Used to give each command a | |
386 * different serial number. */ | |
387 PendingCommand pending; | |
388 char_u *loosename = NULL; | |
389 | |
390 if (result != NULL) | |
391 *result = NULL; | |
392 if (name == NULL || *name == NUL) | |
393 name = (char_u *)"GVIM"; /* use a default name */ | |
394 | |
395 if (commProperty == None && dpy != NULL) | |
396 { | |
397 if (SendInit(dpy) < 0) | |
398 return -1; | |
399 } | |
400 | |
401 /* Execute locally if no display or target is ourselves */ | |
402 if (dpy == NULL || (serverName != NULL && STRICMP(name, serverName) == 0)) | |
11177
76fb679a310e
patch 8.0.0475: not enough testing for the client-server feature
Christian Brabandt <cb@256bit.org>
parents:
11158
diff
changeset
|
403 return sendToLocalVim(cmd, asExpr, result); |
7 | 404 |
405 /* | |
406 * Bind the server name to a communication window. | |
407 * | |
408 * Find any survivor with a serialno attached to the name if the | |
409 * original registrant of the wanted name is no longer present. | |
410 * | |
411 * Delete any lingering names from dead editors. | |
412 */ | |
413 while (TRUE) | |
414 { | |
415 w = LookupName(dpy, name, FALSE, &loosename); | |
416 /* Check that the window is hot */ | |
417 if (w != None) | |
418 { | |
419 if (!WindowValid(dpy, w)) | |
420 { | |
421 LookupName(dpy, loosename ? loosename : name, | |
422 /*DELETE=*/TRUE, NULL); | |
12399
de1ae9d8ef57
patch 8.0.1079: memory leak when remote_foreground() fails
Christian Brabandt <cb@256bit.org>
parents:
11211
diff
changeset
|
423 vim_free(loosename); |
7 | 424 continue; |
425 } | |
426 } | |
427 break; | |
428 } | |
429 if (w == None) | |
430 { | |
431 if (!silent) | |
432 EMSG2(_(e_noserver), name); | |
433 return -1; | |
434 } | |
435 else if (loosename != NULL) | |
436 name = loosename; | |
437 if (server != NULL) | |
438 *server = w; | |
439 | |
440 /* | |
441 * Send the command to target interpreter by appending it to the | |
442 * comm window in the communication window. | |
500 | 443 * Length must be computed exactly! |
7 | 444 */ |
39 | 445 #ifdef FEAT_MBYTE |
500 | 446 length = STRLEN(name) + STRLEN(p_enc) + STRLEN(cmd) + 14; |
447 #else | |
448 length = STRLEN(name) + STRLEN(cmd) + 10; | |
39 | 449 #endif |
450 property = (char_u *)alloc((unsigned)length + 30); | |
7 | 451 |
39 | 452 #ifdef FEAT_MBYTE |
453 sprintf((char *)property, "%c%c%c-n %s%c-E %s%c-s %s", | |
454 0, asExpr ? 'c' : 'k', 0, name, 0, p_enc, 0, cmd); | |
455 #else | |
7 | 456 sprintf((char *)property, "%c%c%c-n %s%c-s %s", |
457 0, asExpr ? 'c' : 'k', 0, name, 0, cmd); | |
39 | 458 #endif |
7 | 459 if (name == loosename) |
460 vim_free(loosename); | |
461 /* Add a back reference to our comm window */ | |
462 serial++; | |
463 sprintf((char *)property + length, "%c-r %x %d", | |
464 0, (int_u)commWindow, serial); | |
500 | 465 /* Add length of what "-r %x %d" resulted in, skipping the NUL. */ |
7 | 466 length += STRLEN(property + length + 1) + 1; |
467 | |
468 res = AppendPropCarefully(dpy, w, commProperty, property, length + 1); | |
469 vim_free(property); | |
470 if (res < 0) | |
471 { | |
472 EMSG(_("E248: Failed to send command to the destination program")); | |
473 return -1; | |
474 } | |
475 | |
476 if (!asExpr) /* There is no answer for this - Keys are sent async */ | |
477 return 0; | |
478 | |
479 /* | |
480 * Register the fact that we're waiting for a command to | |
481 * complete (this is needed by SendEventProc and by | |
482 * AppendErrorProc to pass back the command's results). | |
483 */ | |
484 pending.serial = serial; | |
485 pending.code = 0; | |
486 pending.result = NULL; | |
487 pending.nextPtr = pendingCommands; | |
488 pendingCommands = &pending; | |
489 | |
11211
71311d899b42
patch 8.0.0492: a failing client-server request can make Vim hang
Christian Brabandt <cb@256bit.org>
parents:
11181
diff
changeset
|
490 ServerWait(dpy, w, WaitForPend, &pending, localLoop, |
71311d899b42
patch 8.0.0492: a failing client-server request can make Vim hang
Christian Brabandt <cb@256bit.org>
parents:
11181
diff
changeset
|
491 timeout > 0 ? timeout : 600); |
7 | 492 |
493 /* | |
494 * Unregister the information about the pending command | |
495 * and return the result. | |
496 */ | |
497 if (pendingCommands == &pending) | |
498 pendingCommands = pending.nextPtr; | |
499 else | |
500 { | |
501 PendingCommand *pcPtr; | |
502 | |
503 for (pcPtr = pendingCommands; pcPtr != NULL; pcPtr = pcPtr->nextPtr) | |
504 if (pcPtr->nextPtr == &pending) | |
505 { | |
506 pcPtr->nextPtr = pending.nextPtr; | |
507 break; | |
508 } | |
509 } | |
510 if (result != NULL) | |
511 *result = pending.result; | |
512 else | |
513 vim_free(pending.result); | |
514 | |
515 return pending.code == 0 ? 0 : -1; | |
516 } | |
517 | |
518 static int | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
519 WaitForPend(void *p) |
7 | 520 { |
521 PendingCommand *pending = (PendingCommand *) p; | |
522 return pending->result != NULL; | |
523 } | |
524 | |
525 /* | |
526 * Return TRUE if window "w" exists and has a "Vim" property on it. | |
527 */ | |
528 static int | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
529 WindowValid(Display *dpy, Window w) |
7 | 530 { |
531 XErrorHandler old_handler; | |
532 Atom *plist; | |
533 int numProp; | |
534 int i; | |
535 | |
536 old_handler = XSetErrorHandler(x_error_check); | |
537 got_x_error = 0; | |
538 plist = XListProperties(dpy, w, &numProp); | |
539 XSync(dpy, False); | |
540 XSetErrorHandler(old_handler); | |
541 if (plist == NULL || got_x_error) | |
542 return FALSE; | |
543 | |
544 for (i = 0; i < numProp; i++) | |
545 if (plist[i] == vimProperty) | |
546 { | |
547 XFree(plist); | |
548 return TRUE; | |
549 } | |
550 XFree(plist); | |
551 return FALSE; | |
552 } | |
553 | |
554 /* | |
555 * Enter a loop processing X events & polling chars until we see a result | |
556 */ | |
557 static void | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
558 ServerWait( |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
559 Display *dpy, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
560 Window w, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
561 EndCond endCond, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
562 void *endData, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
563 int localLoop, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
564 int seconds) |
7 | 565 { |
566 time_t start; | |
567 time_t now; | |
568 XEvent event; | |
3670 | 569 |
570 #define UI_MSEC_DELAY 50 | |
571 #define SEND_MSEC_POLL 500 | |
572 #ifndef HAVE_SELECT | |
573 struct pollfd fds; | |
574 | |
575 fds.fd = ConnectionNumber(dpy); | |
576 fds.events = POLLIN; | |
577 #else | |
578 fd_set fds; | |
579 struct timeval tv; | |
580 | |
581 tv.tv_sec = 0; | |
582 tv.tv_usec = SEND_MSEC_POLL * 1000; | |
583 FD_ZERO(&fds); | |
584 FD_SET(ConnectionNumber(dpy), &fds); | |
585 #endif | |
7 | 586 |
587 time(&start); | |
3670 | 588 while (TRUE) |
7 | 589 { |
3670 | 590 while (XCheckWindowEvent(dpy, commWindow, PropertyChangeMask, &event)) |
7109
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
591 serverEventProc(dpy, &event, 1); |
8732
29765df3a866
commit https://github.com/vim/vim/commit/1e7885abe8daa793fd9328d0fd6c456214cb467e
Christian Brabandt <cb@256bit.org>
parents:
7823
diff
changeset
|
592 server_parse_messages(); |
3670 | 593 |
594 if (endCond(endData) != 0) | |
595 break; | |
596 if (!WindowValid(dpy, w)) | |
597 break; | |
7 | 598 time(&now); |
599 if (seconds >= 0 && (now - start) >= seconds) | |
600 break; | |
3670 | 601 |
11181
13544aa85dc0
patch 8.0.0477: the client-server test may hang when failing
Christian Brabandt <cb@256bit.org>
parents:
11177
diff
changeset
|
602 #ifdef FEAT_TIMERS |
13544aa85dc0
patch 8.0.0477: the client-server test may hang when failing
Christian Brabandt <cb@256bit.org>
parents:
11177
diff
changeset
|
603 check_due_timer(); |
13544aa85dc0
patch 8.0.0477: the client-server test may hang when failing
Christian Brabandt <cb@256bit.org>
parents:
11177
diff
changeset
|
604 #endif |
13544aa85dc0
patch 8.0.0477: the client-server test may hang when failing
Christian Brabandt <cb@256bit.org>
parents:
11177
diff
changeset
|
605 |
3670 | 606 /* Just look out for the answer without calling back into Vim */ |
7 | 607 if (localLoop) |
608 { | |
609 #ifndef HAVE_SELECT | |
610 if (poll(&fds, 1, SEND_MSEC_POLL) < 0) | |
611 break; | |
612 #else | |
3670 | 613 if (select(FD_SETSIZE, &fds, NULL, NULL, &tv) < 0) |
7 | 614 break; |
615 #endif | |
616 } | |
617 else | |
618 { | |
619 if (got_int) | |
620 break; | |
3670 | 621 ui_delay((long)UI_MSEC_DELAY, TRUE); |
7 | 622 ui_breakcheck(); |
623 } | |
624 } | |
625 } | |
626 | |
627 | |
628 /* | |
629 * Fetch a list of all the Vim instance names currently registered for the | |
630 * display. | |
631 * | |
632 * Returns a newline separated list in allocated memory or NULL. | |
633 */ | |
634 char_u * | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
635 serverGetVimNames(Display *dpy) |
7 | 636 { |
637 char_u *regProp; | |
638 char_u *entry; | |
639 char_u *p; | |
640 long_u numItems; | |
641 int_u w; | |
642 garray_T ga; | |
643 | |
644 if (registryProperty == None) | |
645 { | |
646 if (SendInit(dpy) < 0) | |
647 return NULL; | |
648 } | |
649 | |
650 /* | |
651 * Read the registry property. | |
652 */ | |
653 if (GetRegProp(dpy, ®Prop, &numItems, TRUE) == FAIL) | |
654 return NULL; | |
655 | |
656 /* | |
657 * Scan all of the names out of the property. | |
658 */ | |
659 ga_init2(&ga, 1, 100); | |
1880 | 660 for (p = regProp; (long_u)(p - regProp) < numItems; p++) |
7 | 661 { |
662 entry = p; | |
663 while (*p != 0 && !isspace(*p)) | |
664 p++; | |
665 if (*p != 0) | |
666 { | |
667 w = None; | |
668 sscanf((char *)entry, "%x", &w); | |
669 if (WindowValid(dpy, (Window)w)) | |
670 { | |
671 ga_concat(&ga, p + 1); | |
672 ga_concat(&ga, (char_u *)"\n"); | |
673 } | |
674 while (*p != 0) | |
675 p++; | |
676 } | |
677 } | |
678 if (regProp != empty_prop) | |
679 XFree(regProp); | |
21 | 680 ga_append(&ga, NUL); |
7 | 681 return ga.ga_data; |
682 } | |
683 | |
684 /* ---------------------------------------------------------- | |
685 * Reply stuff | |
686 */ | |
687 | |
688 static struct ServerReply * | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
689 ServerReplyFind(Window w, enum ServerReplyOp op) |
7 | 690 { |
691 struct ServerReply *p; | |
692 struct ServerReply e; | |
693 int i; | |
694 | |
695 p = (struct ServerReply *) serverReply.ga_data; | |
696 for (i = 0; i < serverReply.ga_len; i++, p++) | |
697 if (p->id == w) | |
698 break; | |
699 if (i >= serverReply.ga_len) | |
700 p = NULL; | |
701 | |
702 if (p == NULL && op == SROP_Add) | |
703 { | |
704 if (serverReply.ga_growsize == 0) | |
705 ga_init2(&serverReply, sizeof(struct ServerReply), 1); | |
706 if (ga_grow(&serverReply, 1) == OK) | |
707 { | |
708 p = ((struct ServerReply *) serverReply.ga_data) | |
709 + serverReply.ga_len; | |
710 e.id = w; | |
711 ga_init2(&e.strings, 1, 100); | |
1740 | 712 mch_memmove(p, &e, sizeof(e)); |
7 | 713 serverReply.ga_len++; |
714 } | |
715 } | |
716 else if (p != NULL && op == SROP_Delete) | |
717 { | |
718 ga_clear(&p->strings); | |
719 mch_memmove(p, p + 1, (serverReply.ga_len - i - 1) * sizeof(*p)); | |
720 serverReply.ga_len--; | |
721 } | |
722 | |
723 return p; | |
724 } | |
725 | |
726 /* | |
727 * Convert string to windowid. | |
728 * Issue an error if the id is invalid. | |
729 */ | |
730 Window | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
731 serverStrToWin(char_u *str) |
7 | 732 { |
733 unsigned id = None; | |
734 | |
735 sscanf((char *)str, "0x%x", &id); | |
736 if (id == None) | |
737 EMSG2(_("E573: Invalid server id used: %s"), str); | |
738 | |
739 return (Window)id; | |
740 } | |
741 | |
742 /* | |
39 | 743 * Send a reply string (notification) to client with id "name". |
7 | 744 * Return -1 if the window is invalid. |
745 */ | |
746 int | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
747 serverSendReply(char_u *name, char_u *str) |
7 | 748 { |
749 char_u *property; | |
750 int length; | |
751 int res; | |
752 Display *dpy = X_DISPLAY; | |
753 Window win = serverStrToWin(name); | |
754 | |
755 if (commProperty == None) | |
756 { | |
757 if (SendInit(dpy) < 0) | |
758 return -2; | |
759 } | |
760 if (!WindowValid(dpy, win)) | |
761 return -1; | |
762 | |
39 | 763 #ifdef FEAT_MBYTE |
500 | 764 length = STRLEN(p_enc) + STRLEN(str) + 14; |
765 #else | |
766 length = STRLEN(str) + 10; | |
39 | 767 #endif |
768 if ((property = (char_u *)alloc((unsigned)length + 30)) != NULL) | |
7 | 769 { |
39 | 770 #ifdef FEAT_MBYTE |
771 sprintf((char *)property, "%cn%c-E %s%c-n %s%c-w %x", | |
772 0, 0, p_enc, 0, str, 0, (unsigned int)commWindow); | |
773 #else | |
774 sprintf((char *)property, "%cn%c-n %s%c-w %x", | |
775 0, 0, str, 0, (unsigned int)commWindow); | |
776 #endif | |
500 | 777 /* Add length of what "%x" resulted in. */ |
7 | 778 length += STRLEN(property + length); |
779 res = AppendPropCarefully(dpy, win, commProperty, property, length + 1); | |
780 vim_free(property); | |
781 return res; | |
782 } | |
783 return -1; | |
784 } | |
785 | |
786 static int | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
787 WaitForReply(void *p) |
7 | 788 { |
789 Window *w = (Window *) p; | |
11177
76fb679a310e
patch 8.0.0475: not enough testing for the client-server feature
Christian Brabandt <cb@256bit.org>
parents:
11158
diff
changeset
|
790 |
7 | 791 return ServerReplyFind(*w, SROP_Find) != NULL; |
792 } | |
793 | |
794 /* | |
795 * Wait for replies from id (win) | |
11211
71311d899b42
patch 8.0.0492: a failing client-server request can make Vim hang
Christian Brabandt <cb@256bit.org>
parents:
11181
diff
changeset
|
796 * When "timeout" is non-zero wait up to this many seconds. |
7 | 797 * Return 0 and the malloc'ed string when a reply is available. |
798 * Return -1 if the window becomes invalid while waiting. | |
799 */ | |
800 int | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
801 serverReadReply( |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
802 Display *dpy, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
803 Window win, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
804 char_u **str, |
11211
71311d899b42
patch 8.0.0492: a failing client-server request can make Vim hang
Christian Brabandt <cb@256bit.org>
parents:
11181
diff
changeset
|
805 int localLoop, |
71311d899b42
patch 8.0.0492: a failing client-server request can make Vim hang
Christian Brabandt <cb@256bit.org>
parents:
11181
diff
changeset
|
806 int timeout) |
7 | 807 { |
808 int len; | |
809 char_u *s; | |
810 struct ServerReply *p; | |
811 | |
11211
71311d899b42
patch 8.0.0492: a failing client-server request can make Vim hang
Christian Brabandt <cb@256bit.org>
parents:
11181
diff
changeset
|
812 ServerWait(dpy, win, WaitForReply, &win, localLoop, |
71311d899b42
patch 8.0.0492: a failing client-server request can make Vim hang
Christian Brabandt <cb@256bit.org>
parents:
11181
diff
changeset
|
813 timeout > 0 ? timeout : -1); |
7 | 814 |
815 if ((p = ServerReplyFind(win, SROP_Find)) != NULL && p->strings.ga_len > 0) | |
816 { | |
817 *str = vim_strsave(p->strings.ga_data); | |
818 len = STRLEN(*str) + 1; | |
819 if (len < p->strings.ga_len) | |
820 { | |
821 s = (char_u *) p->strings.ga_data; | |
822 mch_memmove(s, s + len, p->strings.ga_len - len); | |
823 p->strings.ga_len -= len; | |
824 } | |
825 else | |
826 { | |
827 /* Last string read. Remove from list */ | |
828 ga_clear(&p->strings); | |
829 ServerReplyFind(win, SROP_Delete); | |
830 } | |
831 return 0; | |
832 } | |
833 return -1; | |
834 } | |
835 | |
836 /* | |
837 * Check for replies from id (win). | |
838 * Return TRUE and a non-malloc'ed string if there is. Else return FALSE. | |
839 */ | |
840 int | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
841 serverPeekReply(Display *dpy, Window win, char_u **str) |
7 | 842 { |
843 struct ServerReply *p; | |
844 | |
845 if ((p = ServerReplyFind(win, SROP_Find)) != NULL && p->strings.ga_len > 0) | |
846 { | |
847 if (str != NULL) | |
848 *str = p->strings.ga_data; | |
849 return 1; | |
850 } | |
851 if (!WindowValid(dpy, win)) | |
852 return -1; | |
853 return 0; | |
854 } | |
855 | |
856 | |
857 /* | |
858 * Initialize the communication channels for sending commands and receiving | |
859 * results. | |
860 */ | |
861 static int | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
862 SendInit(Display *dpy) |
7 | 863 { |
864 XErrorHandler old_handler; | |
865 | |
866 /* | |
867 * Create the window used for communication, and set up an | |
868 * event handler for it. | |
869 */ | |
870 old_handler = XSetErrorHandler(x_error_check); | |
871 got_x_error = FALSE; | |
872 | |
873 if (commProperty == None) | |
874 commProperty = XInternAtom(dpy, "Comm", False); | |
875 if (vimProperty == None) | |
876 vimProperty = XInternAtom(dpy, "Vim", False); | |
877 if (registryProperty == None) | |
878 registryProperty = XInternAtom(dpy, "VimRegistry", False); | |
879 | |
880 if (commWindow == None) | |
881 { | |
882 commWindow = XCreateSimpleWindow(dpy, XDefaultRootWindow(dpy), | |
883 getpid(), 0, 10, 10, 0, | |
884 WhitePixel(dpy, DefaultScreen(dpy)), | |
885 WhitePixel(dpy, DefaultScreen(dpy))); | |
886 XSelectInput(dpy, commWindow, PropertyChangeMask); | |
887 /* WARNING: Do not step through this while debugging, it will hangup | |
888 * the X server! */ | |
889 XGrabServer(dpy); | |
890 DeleteAnyLingerer(dpy, commWindow); | |
891 XUngrabServer(dpy); | |
892 } | |
893 | |
894 /* Make window recognizable as a vim window */ | |
895 XChangeProperty(dpy, commWindow, vimProperty, XA_STRING, | |
896 8, PropModeReplace, (char_u *)VIM_VERSION_SHORT, | |
897 (int)STRLEN(VIM_VERSION_SHORT) + 1); | |
898 | |
899 XSync(dpy, False); | |
900 (void)XSetErrorHandler(old_handler); | |
901 | |
902 return got_x_error ? -1 : 0; | |
903 } | |
904 | |
905 /* | |
906 * Given a server name, see if the name exists in the registry for a | |
907 * particular display. | |
908 * | |
909 * If the given name is registered, return the ID of the window associated | |
910 * with the name. If the name isn't registered, then return 0. | |
911 * | |
912 * Side effects: | |
913 * If the registry property is improperly formed, then it is deleted. | |
914 * If "delete" is non-zero, then if the named server is found it is | |
915 * removed from the registry property. | |
916 */ | |
917 static Window | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
918 LookupName( |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
919 Display *dpy, /* Display whose registry to check. */ |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
920 char_u *name, /* Name of a server. */ |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
921 int delete, /* If non-zero, delete info about name. */ |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
922 char_u **loose) /* Do another search matching -999 if not found |
7 | 923 Return result here if a match is found */ |
924 { | |
925 char_u *regProp, *entry; | |
926 char_u *p; | |
927 long_u numItems; | |
928 int_u returnValue; | |
929 | |
930 /* | |
931 * Read the registry property. | |
932 */ | |
933 if (GetRegProp(dpy, ®Prop, &numItems, FALSE) == FAIL) | |
934 return 0; | |
935 | |
936 /* | |
937 * Scan the property for the desired name. | |
938 */ | |
939 returnValue = (int_u)None; | |
940 entry = NULL; /* Not needed, but eliminates compiler warning. */ | |
1880 | 941 for (p = regProp; (long_u)(p - regProp) < numItems; ) |
7 | 942 { |
943 entry = p; | |
944 while (*p != 0 && !isspace(*p)) | |
945 p++; | |
946 if (*p != 0 && STRICMP(name, p + 1) == 0) | |
947 { | |
948 sscanf((char *)entry, "%x", &returnValue); | |
949 break; | |
950 } | |
951 while (*p != 0) | |
952 p++; | |
953 p++; | |
954 } | |
955 | |
956 if (loose != NULL && returnValue == (int_u)None && !IsSerialName(name)) | |
957 { | |
1880 | 958 for (p = regProp; (long_u)(p - regProp) < numItems; ) |
7 | 959 { |
960 entry = p; | |
961 while (*p != 0 && !isspace(*p)) | |
962 p++; | |
963 if (*p != 0 && IsSerialName(p + 1) | |
964 && STRNICMP(name, p + 1, STRLEN(name)) == 0) | |
965 { | |
966 sscanf((char *)entry, "%x", &returnValue); | |
967 *loose = vim_strsave(p + 1); | |
968 break; | |
969 } | |
970 while (*p != 0) | |
971 p++; | |
972 p++; | |
973 } | |
974 } | |
975 | |
976 /* | |
977 * Delete the property, if that is desired (copy down the | |
978 * remainder of the registry property to overlay the deleted | |
979 * info, then rewrite the property). | |
980 */ | |
981 if (delete && returnValue != (int_u)None) | |
982 { | |
983 int count; | |
984 | |
985 while (*p != 0) | |
986 p++; | |
987 p++; | |
988 count = numItems - (p - regProp); | |
989 if (count > 0) | |
1740 | 990 mch_memmove(entry, p, count); |
7 | 991 XChangeProperty(dpy, RootWindow(dpy, 0), registryProperty, XA_STRING, |
992 8, PropModeReplace, regProp, | |
993 (int)(numItems - (p - entry))); | |
994 XSync(dpy, False); | |
995 } | |
996 | |
997 if (regProp != empty_prop) | |
998 XFree(regProp); | |
999 return (Window)returnValue; | |
1000 } | |
1001 | |
1002 /* | |
1197 | 1003 * Delete any lingering occurrence of window id. We promise that any |
1004 * occurrence is not ours since it is not yet put into the registry (by us) | |
7 | 1005 * |
1006 * This is necessary in the following scenario: | |
1007 * 1. There is an old windowid for an exit'ed vim in the registry | |
1008 * 2. We get that id for our commWindow but only want to send, not register. | |
1009 * 3. The window will mistakenly be regarded valid because of own commWindow | |
1010 */ | |
1011 static void | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1012 DeleteAnyLingerer( |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1013 Display *dpy, /* Display whose registry to check. */ |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1014 Window win) /* Window to remove */ |
7 | 1015 { |
1016 char_u *regProp, *entry = NULL; | |
1017 char_u *p; | |
1018 long_u numItems; | |
28 | 1019 int_u wwin; |
7 | 1020 |
1021 /* | |
1022 * Read the registry property. | |
1023 */ | |
1024 if (GetRegProp(dpy, ®Prop, &numItems, FALSE) == FAIL) | |
1025 return; | |
1026 | |
1027 /* Scan the property for the window id. */ | |
1880 | 1028 for (p = regProp; (long_u)(p - regProp) < numItems; ) |
7 | 1029 { |
1030 if (*p != 0) | |
1031 { | |
28 | 1032 sscanf((char *)p, "%x", &wwin); |
1033 if ((Window)wwin == win) | |
7 | 1034 { |
1035 int lastHalf; | |
1036 | |
1037 /* Copy down the remainder to delete entry */ | |
1038 entry = p; | |
1039 while (*p != 0) | |
1040 p++; | |
1041 p++; | |
1042 lastHalf = numItems - (p - regProp); | |
1043 if (lastHalf > 0) | |
1740 | 1044 mch_memmove(entry, p, lastHalf); |
7 | 1045 numItems = (entry - regProp) + lastHalf; |
1046 p = entry; | |
1047 continue; | |
1048 } | |
1049 } | |
1050 while (*p != 0) | |
1051 p++; | |
1052 p++; | |
1053 } | |
1054 | |
1055 if (entry != NULL) | |
1056 { | |
1057 XChangeProperty(dpy, RootWindow(dpy, 0), registryProperty, | |
1058 XA_STRING, 8, PropModeReplace, regProp, | |
1059 (int)(p - regProp)); | |
1060 XSync(dpy, False); | |
1061 } | |
1062 | |
1063 if (regProp != empty_prop) | |
1064 XFree(regProp); | |
1065 } | |
1066 | |
1067 /* | |
1068 * Read the registry property. Delete it when it's formatted wrong. | |
1069 * Return the property in "regPropp". "empty_prop" is used when it doesn't | |
1070 * exist yet. | |
1071 * Return OK when successful. | |
1072 */ | |
1073 static int | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1074 GetRegProp( |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1075 Display *dpy, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1076 char_u **regPropp, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1077 long_u *numItemsp, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1078 int domsg) /* When TRUE give error message. */ |
7 | 1079 { |
1080 int result, actualFormat; | |
1081 long_u bytesAfter; | |
1082 Atom actualType; | |
819 | 1083 XErrorHandler old_handler; |
7 | 1084 |
1085 *regPropp = NULL; | |
819 | 1086 old_handler = XSetErrorHandler(x_error_check); |
1087 got_x_error = FALSE; | |
1088 | |
7 | 1089 result = XGetWindowProperty(dpy, RootWindow(dpy, 0), registryProperty, 0L, |
1090 (long)MAX_PROP_WORDS, False, | |
1091 XA_STRING, &actualType, | |
1092 &actualFormat, numItemsp, &bytesAfter, | |
1093 regPropp); | |
1094 | |
819 | 1095 XSync(dpy, FALSE); |
1096 (void)XSetErrorHandler(old_handler); | |
1097 if (got_x_error) | |
1098 return FAIL; | |
1099 | |
7 | 1100 if (actualType == None) |
1101 { | |
1102 /* No prop yet. Logically equal to the empty list */ | |
1103 *numItemsp = 0; | |
1104 *regPropp = empty_prop; | |
1105 return OK; | |
1106 } | |
1107 | |
1108 /* If the property is improperly formed, then delete it. */ | |
1109 if (result != Success || actualFormat != 8 || actualType != XA_STRING) | |
1110 { | |
1111 if (*regPropp != NULL) | |
1112 XFree(*regPropp); | |
1113 XDeleteProperty(dpy, RootWindow(dpy, 0), registryProperty); | |
1114 if (domsg) | |
1115 EMSG(_("E251: VIM instance registry property is badly formed. Deleted!")); | |
1116 return FAIL; | |
1117 } | |
1118 return OK; | |
1119 } | |
1120 | |
7109
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1121 |
7 | 1122 /* |
1197 | 1123 * This procedure is invoked by the various X event loops throughout Vims when |
7 | 1124 * a property changes on the communication window. This procedure reads the |
7109
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1125 * property and enqueues command requests and responses. If immediate is true, |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1126 * it runs the event immediatly instead of enqueuing it. Immediate can cause |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1127 * unintended behavior and should only be used for code that blocks for a |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1128 * response. |
7 | 1129 */ |
1130 void | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1131 serverEventProc( |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1132 Display *dpy, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1133 XEvent *eventPtr, /* Information about event. */ |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1134 int immediate) /* Run event immediately. Should mostly be 0. */ |
7 | 1135 { |
1136 char_u *propInfo; | |
7109
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1137 int result, actualFormat; |
7 | 1138 long_u numItems, bytesAfter; |
1139 Atom actualType; | |
1140 | |
1141 if (eventPtr != NULL) | |
1142 { | |
1143 if (eventPtr->xproperty.atom != commProperty | |
1144 || eventPtr->xproperty.state != PropertyNewValue) | |
1145 return; | |
1146 } | |
1147 | |
1148 /* | |
1149 * Read the comm property and delete it. | |
1150 */ | |
1151 propInfo = NULL; | |
1152 result = XGetWindowProperty(dpy, commWindow, commProperty, 0L, | |
1153 (long)MAX_PROP_WORDS, True, | |
1154 XA_STRING, &actualType, | |
1155 &actualFormat, &numItems, &bytesAfter, | |
1156 &propInfo); | |
1157 | |
1158 /* If the property doesn't exist or is improperly formed then ignore it. */ | |
1159 if (result != Success || actualType != XA_STRING || actualFormat != 8) | |
1160 { | |
1161 if (propInfo != NULL) | |
1162 XFree(propInfo); | |
1163 return; | |
1164 } | |
7109
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1165 if (immediate) |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1166 server_parse_message(dpy, propInfo, numItems); |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1167 else |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1168 save_in_queue(propInfo, numItems); |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1169 } |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1170 |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1171 /* |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1172 * Saves x clientserver commands in a queue so that they can be called when |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1173 * vim is idle. |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1174 */ |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1175 static void |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1176 save_in_queue(char_u *propInfo, long_u len) |
7109
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1177 { |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1178 x_queue_T *node; |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1179 |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1180 node = (x_queue_T *)alloc(sizeof(x_queue_T)); |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1181 if (node == NULL) |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1182 return; /* out of memory */ |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1183 node->propInfo = propInfo; |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1184 node->len = len; |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1185 |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1186 if (head.next == NULL) /* initialize circular queue */ |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1187 { |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1188 head.next = &head; |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1189 head.prev = &head; |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1190 } |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1191 |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1192 /* insert node at tail of queue */ |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1193 node->next = &head; |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1194 node->prev = head.prev; |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1195 head.prev->next = node; |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1196 head.prev = node; |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1197 } |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1198 |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1199 /* |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1200 * Parses queued clientserver messages. |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1201 */ |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1202 void |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1203 server_parse_messages(void) |
7109
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1204 { |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1205 x_queue_T *node; |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1206 |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1207 if (!X_DISPLAY) |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1208 return; /* cannot happen? */ |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1209 while (head.next != NULL && head.next != &head) |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1210 { |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1211 node = head.next; |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1212 head.next = node->next; |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1213 node->next->prev = node->prev; |
7172
ad57f5b5bd6c
commit https://github.com/vim/vim/commit/4e86150ec5b5158da92b28938ea55819dc890a14
Christian Brabandt <cb@256bit.org>
parents:
7123
diff
changeset
|
1214 server_parse_message(X_DISPLAY, node->propInfo, node->len); |
7109
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1215 vim_free(node); |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1216 } |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1217 } |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1218 |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1219 /* |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1220 * Returns a non-zero value if there are clientserver messages waiting |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1221 * int the queue. |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1222 */ |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1223 int |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1224 server_waiting(void) |
7109
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1225 { |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1226 return head.next != NULL && head.next != &head; |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1227 } |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1228 |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1229 /* |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1230 * Prases a single clientserver message. A single message may contain multiple |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1231 * commands. |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1232 * "propInfo" will be freed. |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1233 */ |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1234 static void |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1235 server_parse_message( |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1236 Display *dpy, |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1237 char_u *propInfo, /* A string containing 0 or more X commands */ |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1238 long_u numItems) /* The size of propInfo in bytes. */ |
7109
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1239 { |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1240 char_u *p; |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1241 int code; |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1242 char_u *tofree; |
7 | 1243 |
1244 /* | |
1245 * Several commands and results could arrive in the property at | |
1246 * one time; each iteration through the outer loop handles a | |
1247 * single command or result. | |
1248 */ | |
1880 | 1249 for (p = propInfo; (long_u)(p - propInfo) < numItems; ) |
7 | 1250 { |
1251 /* | |
1252 * Ignore leading NULs; each command or result starts with a | |
1253 * NUL so that no matter how badly formed a preceding command | |
1254 * is, we'll be able to tell that a new command/result is | |
1255 * starting. | |
1256 */ | |
1257 if (*p == 0) | |
1258 { | |
1259 p++; | |
1260 continue; | |
1261 } | |
1262 | |
1263 if ((*p == 'c' || *p == 'k') && (p[1] == 0)) | |
1264 { | |
1265 Window resWindow; | |
3668 | 1266 char_u *name, *script, *serial, *end; |
7 | 1267 Bool asKeys = *p == 'k'; |
39 | 1268 char_u *enc; |
7 | 1269 |
1270 /* | |
1271 * This is an incoming command from some other application. | |
1272 * Iterate over all of its options. Stop when we reach | |
1273 * the end of the property or something that doesn't look | |
1274 * like an option. | |
1275 */ | |
1276 p += 2; | |
1277 name = NULL; | |
1278 resWindow = None; | |
1279 serial = (char_u *)""; | |
1280 script = NULL; | |
39 | 1281 enc = NULL; |
1880 | 1282 while ((long_u)(p - propInfo) < numItems && *p == '-') |
7 | 1283 { |
1284 switch (p[1]) | |
1285 { | |
1286 case 'r': | |
1287 end = skipwhite(p + 2); | |
1288 resWindow = 0; | |
1289 while (vim_isxdigit(*end)) | |
1290 { | |
1291 resWindow = 16 * resWindow + (long_u)hex2nr(*end); | |
1292 ++end; | |
1293 } | |
1294 if (end == p + 2 || *end != ' ') | |
1295 resWindow = None; | |
1296 else | |
1297 { | |
1298 p = serial = end + 1; | |
1299 clientWindow = resWindow; /* Remember in global */ | |
1300 } | |
1301 break; | |
1302 case 'n': | |
1303 if (p[2] == ' ') | |
1304 name = p + 3; | |
1305 break; | |
1306 case 's': | |
1307 if (p[2] == ' ') | |
1308 script = p + 3; | |
1309 break; | |
39 | 1310 case 'E': |
1311 if (p[2] == ' ') | |
1312 enc = p + 3; | |
1313 break; | |
7 | 1314 } |
1315 while (*p != 0) | |
1316 p++; | |
1317 p++; | |
1318 } | |
1319 | |
1320 if (script == NULL || name == NULL) | |
1321 continue; | |
1322 | |
7109
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1323 if (serverName != NULL && STRICMP(name, serverName) == 0) |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1324 { |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1325 script = serverConvert(enc, script, &tofree); |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1326 if (asKeys) |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1327 server_to_input_buf(script); |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1328 else |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1329 { |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1330 char_u *res; |
3668 | 1331 |
7109
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1332 res = eval_client_expr_to_string(script); |
3668 | 1333 if (resWindow != None) |
1334 { | |
1335 garray_T reply; | |
1336 | |
1337 /* Initialize the result property. */ | |
1338 ga_init2(&reply, 1, 100); | |
39 | 1339 #ifdef FEAT_MBYTE |
7009 | 1340 (void)ga_grow(&reply, 50 + STRLEN(p_enc)); |
3668 | 1341 sprintf(reply.ga_data, "%cr%c-E %s%c-s %s%c-r ", |
39 | 1342 0, 0, p_enc, 0, serial, 0); |
3668 | 1343 reply.ga_len = 14 + STRLEN(p_enc) + STRLEN(serial); |
39 | 1344 #else |
7009 | 1345 (void)ga_grow(&reply, 50); |
3668 | 1346 sprintf(reply.ga_data, "%cr%c-s %s%c-r ", |
1347 0, 0, serial, 0); | |
1348 reply.ga_len = 10 + STRLEN(serial); | |
39 | 1349 #endif |
3668 | 1350 |
1351 /* Evaluate the expression and return the result. */ | |
1352 if (res != NULL) | |
1353 ga_concat(&reply, res); | |
1354 else | |
1355 { | |
1356 ga_concat(&reply, (char_u *)_(e_invexprmsg)); | |
1357 ga_append(&reply, 0); | |
1358 ga_concat(&reply, (char_u *)"-c 1"); | |
1359 } | |
1360 ga_append(&reply, NUL); | |
1361 (void)AppendPropCarefully(dpy, resWindow, commProperty, | |
1362 reply.ga_data, reply.ga_len); | |
1363 ga_clear(&reply); | |
1364 } | |
7109
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1365 vim_free(res); |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1366 } |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1367 vim_free(tofree); |
fa95595fbc52
commit https://github.com/vim/vim/commit/93c88e0f6a4a8f7634ed84721daf4af46fc0d5db
Christian Brabandt <cb@256bit.org>
parents:
7009
diff
changeset
|
1368 } |
7 | 1369 } |
1370 else if (*p == 'r' && p[1] == 0) | |
1371 { | |
39 | 1372 int serial, gotSerial; |
1373 char_u *res; | |
1374 PendingCommand *pcPtr; | |
1375 char_u *enc; | |
7 | 1376 |
1377 /* | |
1378 * This is a reply to some command that we sent out. Iterate | |
1379 * over all of its options. Stop when we reach the end of the | |
1380 * property or something that doesn't look like an option. | |
1381 */ | |
1382 p += 2; | |
1383 gotSerial = 0; | |
1384 res = (char_u *)""; | |
1385 code = 0; | |
39 | 1386 enc = NULL; |
1880 | 1387 while ((long_u)(p - propInfo) < numItems && *p == '-') |
7 | 1388 { |
1389 switch (p[1]) | |
1390 { | |
1391 case 'r': | |
1392 if (p[2] == ' ') | |
1393 res = p + 3; | |
1394 break; | |
39 | 1395 case 'E': |
1396 if (p[2] == ' ') | |
1397 enc = p + 3; | |
1398 break; | |
7 | 1399 case 's': |
1400 if (sscanf((char *)p + 2, " %d", &serial) == 1) | |
1401 gotSerial = 1; | |
1402 break; | |
1403 case 'c': | |
1404 if (sscanf((char *)p + 2, " %d", &code) != 1) | |
1405 code = 0; | |
1406 break; | |
1407 } | |
1408 while (*p != 0) | |
1409 p++; | |
1410 p++; | |
1411 } | |
1412 | |
1413 if (!gotSerial) | |
1414 continue; | |
1415 | |
1416 /* | |
1417 * Give the result information to anyone who's | |
1418 * waiting for it. | |
1419 */ | |
1420 for (pcPtr = pendingCommands; pcPtr != NULL; pcPtr = pcPtr->nextPtr) | |
1421 { | |
1422 if (serial != pcPtr->serial || pcPtr->result != NULL) | |
1423 continue; | |
1424 | |
1425 pcPtr->code = code; | |
7009 | 1426 res = serverConvert(enc, res, &tofree); |
1427 if (tofree == NULL) | |
1428 res = vim_strsave(res); | |
1429 pcPtr->result = res; | |
7 | 1430 break; |
1431 } | |
1432 } | |
1433 else if (*p == 'n' && p[1] == 0) | |
1434 { | |
1435 Window win = 0; | |
1436 unsigned int u; | |
1437 int gotWindow; | |
1438 char_u *str; | |
1439 struct ServerReply *r; | |
39 | 1440 char_u *enc; |
7 | 1441 |
1442 /* | |
10942
e05695e59f6d
patch 8.0.0360: sometimes VimL is used instead of "Vim script"
Christian Brabandt <cb@256bit.org>
parents:
10042
diff
changeset
|
1443 * This is a (n)otification. Sent with serverreply_send in Vim |
e05695e59f6d
patch 8.0.0360: sometimes VimL is used instead of "Vim script"
Christian Brabandt <cb@256bit.org>
parents:
10042
diff
changeset
|
1444 * script. Execute any autocommand and save it for later retrieval |
7 | 1445 */ |
1446 p += 2; | |
1447 gotWindow = 0; | |
1448 str = (char_u *)""; | |
39 | 1449 enc = NULL; |
1880 | 1450 while ((long_u)(p - propInfo) < numItems && *p == '-') |
7 | 1451 { |
1452 switch (p[1]) | |
1453 { | |
1454 case 'n': | |
1455 if (p[2] == ' ') | |
1456 str = p + 3; | |
1457 break; | |
39 | 1458 case 'E': |
1459 if (p[2] == ' ') | |
1460 enc = p + 3; | |
1461 break; | |
7 | 1462 case 'w': |
1463 if (sscanf((char *)p + 2, " %x", &u) == 1) | |
1464 { | |
1465 win = u; | |
1466 gotWindow = 1; | |
1467 } | |
1468 break; | |
1469 } | |
1470 while (*p != 0) | |
1471 p++; | |
1472 p++; | |
1473 } | |
1474 | |
1475 if (!gotWindow) | |
1476 continue; | |
39 | 1477 str = serverConvert(enc, str, &tofree); |
7 | 1478 if ((r = ServerReplyFind(win, SROP_Add)) != NULL) |
1479 { | |
1480 ga_concat(&(r->strings), str); | |
21 | 1481 ga_append(&(r->strings), NUL); |
7 | 1482 } |
1483 #ifdef FEAT_AUTOCMD | |
1698 | 1484 { |
1485 char_u winstr[30]; | |
1486 | |
1487 sprintf((char *)winstr, "0x%x", (unsigned int)win); | |
1488 apply_autocmds(EVENT_REMOTEREPLY, winstr, str, TRUE, curbuf); | |
1489 } | |
7 | 1490 #endif |
39 | 1491 vim_free(tofree); |
7 | 1492 } |
1493 else | |
1494 { | |
1495 /* | |
1496 * Didn't recognize this thing. Just skip through the next | |
1497 * null character and try again. | |
1498 * Even if we get an 'r'(eply) we will throw it away as we | |
1499 * never specify (and thus expect) one | |
1500 */ | |
1501 while (*p != 0) | |
1502 p++; | |
1503 p++; | |
1504 } | |
1505 } | |
1506 XFree(propInfo); | |
1507 } | |
1508 | |
1509 /* | |
1510 * Append a given property to a given window, but set up an X error handler so | |
1511 * that if the append fails this procedure can return an error code rather | |
1512 * than having Xlib panic. | |
1513 * Return: 0 for OK, -1 for error | |
1514 */ | |
1515 static int | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1516 AppendPropCarefully( |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1517 Display *dpy, /* Display on which to operate. */ |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1518 Window window, /* Window whose property is to be modified. */ |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1519 Atom property, /* Name of property. */ |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1520 char_u *value, /* Characters to append to property. */ |
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1521 int length) /* How much to append */ |
7 | 1522 { |
1523 XErrorHandler old_handler; | |
1524 | |
1525 old_handler = XSetErrorHandler(x_error_check); | |
1526 got_x_error = FALSE; | |
1527 XChangeProperty(dpy, window, property, XA_STRING, 8, | |
1528 PropModeAppend, value, length); | |
1529 XSync(dpy, False); | |
1530 (void) XSetErrorHandler(old_handler); | |
1531 return got_x_error ? -1 : 0; | |
1532 } | |
1533 | |
1534 | |
1535 /* | |
1536 * Another X Error handler, just used to check for errors. | |
1537 */ | |
1538 static int | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1539 x_error_check(Display *dpy UNUSED, XErrorEvent *error_event UNUSED) |
7 | 1540 { |
1541 got_x_error = TRUE; | |
1542 return 0; | |
1543 } | |
1544 | |
1545 /* | |
1546 * Check if "str" looks like it had a serial number appended. | |
1547 * Actually just checks if the name ends in a digit. | |
1548 */ | |
1549 static int | |
7823
bcef391c101c
commit https://github.com/vim/vim/commit/68c2f638e65d914dc6e84eb7ce2624f08af525c0
Christian Brabandt <cb@256bit.org>
parents:
7803
diff
changeset
|
1550 IsSerialName(char_u *str) |
7 | 1551 { |
1552 int len = STRLEN(str); | |
1553 | |
1554 return (len > 1 && vim_isdigit(str[len - 1])); | |
1555 } | |
1556 #endif /* FEAT_CLIENTSERVER */ |