blob: f1bae5991ea92dcb9fff7a0f51d167cbb1c94548 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * Netbeans integration by David Weatherford
5 * Adopted for Win32 by Sergey Khorev
6 *
7 * Do ":help uganda" in Vim to read copying and usage conditions.
8 * Do ":help credits" in Vim to see a list of people who contributed.
9 */
10
11/*
12 * Implements client side of org.netbeans.modules.emacs editor
13 * integration protocol. Be careful! The protocol uses offsets
14 * which are *between* characters, whereas vim uses line number
15 * and column number which are *on* characters.
16 * See ":help netbeans-protocol" for explanation.
17 */
18
Bram Moolenaarc236c162008-07-13 17:41:49 +000019#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
Bram Moolenaarff064e12008-06-09 13:10:45 +000020# include "vimio.h" /* for mch_open(), must be before vim.h */
21#endif
22
Bram Moolenaar071d4272004-06-13 20:20:40 +000023#include "vim.h"
24
25#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
26
27/* Note: when making changes here also adjust configure.in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000028#ifdef WIN32
29# ifdef DEBUG
30# include <tchar.h> /* for _T definition for TRACEn macros */
31# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000032/* WinSock API is separated from C API, thus we can't use read(), write(),
33 * errno... */
34# define sock_errno WSAGetLastError()
Bram Moolenaarbd42a0f2009-06-16 14:57:26 +000035# undef ECONNREFUSED
Bram Moolenaar071d4272004-06-13 20:20:40 +000036# define ECONNREFUSED WSAECONNREFUSED
37# ifdef EINTR
38# undef EINTR
39# endif
40# define EINTR WSAEINTR
41# define sock_write(sd, buf, len) send(sd, buf, len, 0)
42# define sock_read(sd, buf, len) recv(sd, buf, len, 0)
43# define sock_close(sd) closesocket(sd)
44# define sleep(t) Sleep(t*1000) /* WinAPI Sleep() accepts milliseconds */
45#else
46# include <netdb.h>
47# include <netinet/in.h>
48# include <sys/socket.h>
49# ifdef HAVE_LIBGEN_H
50# include <libgen.h>
51# endif
52# define sock_errno errno
53# define sock_write(sd, buf, len) write(sd, buf, len)
54# define sock_read(sd, buf, len) read(sd, buf, len)
55# define sock_close(sd) close(sd)
56#endif
57
58#include "version.h"
59
60#define INET_SOCKETS
61
62#define GUARDED 10000 /* typenr for "guarded" annotation */
63#define GUARDEDOFFSET 1000000 /* base for "guarded" sign id's */
Bram Moolenaar67c53842010-05-22 18:28:27 +020064#define MAX_COLOR_LENGTH 32 /* max length of color name in defineAnnoType */
Bram Moolenaar071d4272004-06-13 20:20:40 +000065
66/* The first implementation (working only with Netbeans) returned "1.1". The
67 * protocol implemented here also supports A-A-P. */
Bram Moolenaar67c53842010-05-22 18:28:27 +020068static char *ExtEdProtocolVersion = "2.5";
Bram Moolenaar071d4272004-06-13 20:20:40 +000069
70static long pos2off __ARGS((buf_T *, pos_T *));
71static pos_T *off2pos __ARGS((buf_T *, long));
72static pos_T *get_off_or_lnum __ARGS((buf_T *buf, char_u **argp));
73static long get_buf_size __ARGS((buf_T *));
Bram Moolenaar8065d7f2010-01-19 15:13:14 +010074static int netbeans_keystring __ARGS((char_u *keystr));
75static void postpone_keycommand __ARGS((char_u *keystr));
Bram Moolenaar009b2592004-10-24 19:18:58 +000076static void special_keys __ARGS((char_u *args));
Bram Moolenaar071d4272004-06-13 20:20:40 +000077
78static void netbeans_connect __ARGS((void));
79static int getConnInfo __ARGS((char *file, char **host, char **port, char **password));
80
81static void nb_init_graphics __ARGS((void));
82static void coloncmd __ARGS((char *cmd, ...));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000083static void nb_set_curbuf __ARGS((buf_T *buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000084#ifdef FEAT_GUI_MOTIF
85static void messageFromNetbeans __ARGS((XtPointer, int *, XtInputId *));
86#endif
87#ifdef FEAT_GUI_GTK
88static void messageFromNetbeans __ARGS((gpointer, gint, GdkInputCondition));
89#endif
90static void nb_parse_cmd __ARGS((char_u *));
91static int nb_do_cmd __ARGS((int, char_u *, int, int, char_u *));
92static void nb_send __ARGS((char *buf, char *fun));
Bram Moolenaar071d4272004-06-13 20:20:40 +000093
Bram Moolenaar67c53842010-05-22 18:28:27 +020094/* TRUE when netbeans is running with a GUI. */
95#ifdef FEAT_GUI
96# define NB_HAS_GUI (gui.in_use || gui.starting)
97#endif
98
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000099#ifdef WIN64
100typedef __int64 NBSOCK;
101#else
102typedef int NBSOCK;
103#endif
104
105static NBSOCK sd = -1; /* socket fd for Netbeans connection */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106#ifdef FEAT_GUI_MOTIF
107static XtInputId inputHandler; /* Cookie for input */
108#endif
109#ifdef FEAT_GUI_GTK
110static gint inputHandler; /* Cookie for input */
111#endif
112#ifdef FEAT_GUI_W32
113static int inputHandler = -1; /* simply ret.value of WSAAsyncSelect() */
114extern HWND s_hwnd; /* Gvim's Window handle */
115#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +0000116static int r_cmdno; /* current command number for reply */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117static int haveConnection = FALSE; /* socket is connected and
118 initialization is done */
Bram Moolenaar009b2592004-10-24 19:18:58 +0000119static int dosetvisible = FALSE;
120
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121/*
122 * Include the debugging code if wanted.
123 */
124#ifdef NBDEBUG
125# include "nbdebug.c"
126#endif
127
128/* Connect back to Netbeans process */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129 static void
130netbeans_disconnect(void)
131{
Bram Moolenaar67c53842010-05-22 18:28:27 +0200132#ifdef FEAT_GUI_MOTIF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133 if (inputHandler != (XtInputId)NULL)
134 {
135 XtRemoveInput(inputHandler);
136 inputHandler = (XtInputId)NULL;
137 }
Bram Moolenaar67c53842010-05-22 18:28:27 +0200138#else
139# ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140 if (inputHandler != 0)
141 {
142 gdk_input_remove(inputHandler);
143 inputHandler = 0;
144 }
Bram Moolenaar67c53842010-05-22 18:28:27 +0200145# else
146# ifdef FEAT_GUI_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147 if (inputHandler == 0)
148 {
149 WSAAsyncSelect(sd, s_hwnd, 0, 0);
150 inputHandler = -1;
151 }
Bram Moolenaar67c53842010-05-22 18:28:27 +0200152# endif
153# endif
154#endif
155
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156 sd = -1;
157 haveConnection = FALSE;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200158#ifdef FEAT_BEVAL
Bram Moolenaard62bec82005-03-07 22:56:57 +0000159 bevalServers &= ~BEVAL_NETBEANS;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162
163#define NB_DEF_HOST "localhost"
164#define NB_DEF_ADDR "3219"
165#define NB_DEF_PASS "changeme"
166
167 static void
168netbeans_connect(void)
169{
170#ifdef INET_SOCKETS
171 struct sockaddr_in server;
172 struct hostent * host;
173# ifdef FEAT_GUI_W32
174 u_short port;
175# else
176 int port;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200177# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178#else
179 struct sockaddr_un server;
180#endif
181 char buf[32];
182 char *hostname = NULL;
183 char *address = NULL;
184 char *password = NULL;
185 char *fname;
186 char *arg = NULL;
187
188 if (netbeansArg[3] == '=')
189 {
190 /* "-nb=fname": Read info from specified file. */
191 if (getConnInfo(netbeansArg + 4, &hostname, &address, &password)
192 == FAIL)
193 return;
194 }
195 else
196 {
197 if (netbeansArg[3] == ':')
198 /* "-nb:<host>:<addr>:<password>": get info from argument */
199 arg = netbeansArg + 4;
200 if (arg == NULL && (fname = getenv("__NETBEANS_CONINFO")) != NULL)
201 {
202 /* "-nb": get info from file specified in environment */
203 if (getConnInfo(fname, &hostname, &address, &password) == FAIL)
204 return;
205 }
206 else
207 {
208 if (arg != NULL)
209 {
210 /* "-nb:<host>:<addr>:<password>": get info from argument */
211 hostname = arg;
212 address = strchr(hostname, ':');
213 if (address != NULL)
214 {
215 *address++ = '\0';
216 password = strchr(address, ':');
217 if (password != NULL)
218 *password++ = '\0';
219 }
220 }
221
222 /* Get the missing values from the environment. */
223 if (hostname == NULL || *hostname == '\0')
224 hostname = getenv("__NETBEANS_HOST");
225 if (address == NULL)
226 address = getenv("__NETBEANS_SOCKET");
227 if (password == NULL)
228 password = getenv("__NETBEANS_VIM_PASSWORD");
229
230 /* Move values to allocated memory. */
231 if (hostname != NULL)
232 hostname = (char *)vim_strsave((char_u *)hostname);
233 if (address != NULL)
234 address = (char *)vim_strsave((char_u *)address);
235 if (password != NULL)
236 password = (char *)vim_strsave((char_u *)password);
237 }
238 }
239
240 /* Use the default when a value is missing. */
241 if (hostname == NULL || *hostname == '\0')
242 {
243 vim_free(hostname);
244 hostname = (char *)vim_strsave((char_u *)NB_DEF_HOST);
245 }
246 if (address == NULL || *address == '\0')
247 {
248 vim_free(address);
249 address = (char *)vim_strsave((char_u *)NB_DEF_ADDR);
250 }
251 if (password == NULL || *password == '\0')
252 {
253 vim_free(password);
254 password = (char *)vim_strsave((char_u *)NB_DEF_PASS);
255 }
256 if (hostname == NULL || address == NULL || password == NULL)
257 goto theend; /* out of memory */
258
259#ifdef INET_SOCKETS
260 port = atoi(address);
261
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000262 if ((sd = (NBSOCK)socket(AF_INET, SOCK_STREAM, 0)) == (NBSOCK)-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000264 nbdebug(("error in socket() in netbeans_connect()\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265 PERROR("socket() in netbeans_connect()");
266 goto theend;
267 }
268
269 /* Get the server internet address and put into addr structure */
270 /* fill in the socket address structure and connect to server */
271 memset((char *)&server, '\0', sizeof(server));
272 server.sin_family = AF_INET;
273 server.sin_port = htons(port);
274 if ((host = gethostbyname(hostname)) == NULL)
275 {
276 if (mch_access(hostname, R_OK) >= 0)
277 {
278 /* DEBUG: input file */
279 sd = mch_open(hostname, O_RDONLY, 0);
280 goto theend;
281 }
Bram Moolenaarf2330482008-06-24 20:19:36 +0000282 nbdebug(("error in gethostbyname() in netbeans_connect()\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283 PERROR("gethostbyname() in netbeans_connect()");
284 sd = -1;
285 goto theend;
286 }
287 memcpy((char *)&server.sin_addr, host->h_addr, host->h_length);
288#else
289 if ((sd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
290 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000291 nbdebug(("error in socket() in netbeans_connect()\n"));
292 PERROR("socket() in netbeans_connect()");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293 goto theend;
294 }
295
296 server.sun_family = AF_UNIX;
297 strcpy(server.sun_path, address);
298#endif
299 /* Connect to server */
300 if (connect(sd, (struct sockaddr *)&server, sizeof(server)))
301 {
302 nbdebug(("netbeans_connect: Connect failed with errno %d\n", sock_errno));
303 if (sock_errno == ECONNREFUSED)
304 {
305 sock_close(sd);
306#ifdef INET_SOCKETS
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000307 if ((sd = (NBSOCK)socket(AF_INET, SOCK_STREAM, 0)) == (NBSOCK)-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000309 nbdebug(("socket()#2 in netbeans_connect()\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310 PERROR("socket()#2 in netbeans_connect()");
311 goto theend;
312 }
313#else
314 if ((sd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
315 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000316 nbdebug(("socket()#2 in netbeans_connect()\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317 PERROR("socket()#2 in netbeans_connect()");
318 goto theend;
319 }
320#endif
321 if (connect(sd, (struct sockaddr *)&server, sizeof(server)))
322 {
323 int retries = 36;
324 int success = FALSE;
325 while (retries--
326 && ((sock_errno == ECONNREFUSED) || (sock_errno == EINTR)))
327 {
328 nbdebug(("retrying...\n"));
329 sleep(5);
330 if (connect(sd, (struct sockaddr *)&server,
331 sizeof(server)) == 0)
332 {
333 success = TRUE;
334 break;
335 }
336 }
337 if (!success)
338 {
339 /* Get here when the server can't be found. */
Bram Moolenaarf2330482008-06-24 20:19:36 +0000340 nbdebug(("Cannot connect to Netbeans #2\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 PERROR(_("Cannot connect to Netbeans #2"));
342 getout(1);
343 }
344 }
345
346 }
347 else
348 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000349 nbdebug(("Cannot connect to Netbeans\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 PERROR(_("Cannot connect to Netbeans"));
351 getout(1);
352 }
353 }
354
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000355 vim_snprintf(buf, sizeof(buf), "AUTH %s\n", password);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356 nb_send(buf, "netbeans_connect");
357
358 sprintf(buf, "0:version=0 \"%s\"\n", ExtEdProtocolVersion);
359 nb_send(buf, "externaleditor_version");
360
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361/* nb_init_graphics(); delay until needed */
362
363 haveConnection = TRUE;
364
365theend:
366 vim_free(hostname);
367 vim_free(address);
368 vim_free(password);
369 return;
370}
371
372/*
373 * Obtain the NetBeans hostname, port address and password from a file.
374 * Return the strings in allocated memory.
375 * Return FAIL if the file could not be read, OK otherwise (no matter what it
376 * contains).
377 */
378 static int
379getConnInfo(char *file, char **host, char **port, char **auth)
380{
381 FILE *fp;
382 char_u buf[BUFSIZ];
383 char_u *lp;
384 char_u *nl;
385#ifdef UNIX
386 struct stat st;
387
388 /*
389 * For Unix only accept the file when it's not accessible by others.
390 * The open will then fail if we don't own the file.
391 */
392 if (mch_stat(file, &st) == 0 && (st.st_mode & 0077) != 0)
393 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000394 nbdebug(("Wrong access mode for NetBeans connection info file: \"%s\"\n",
395 file));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396 EMSG2(_("E668: Wrong access mode for NetBeans connection info file: \"%s\""),
397 file);
398 return FAIL;
399 }
400#endif
401
402 fp = mch_fopen(file, "r");
403 if (fp == NULL)
404 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000405 nbdebug(("Cannot open NetBeans connection info file\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 PERROR("E660: Cannot open NetBeans connection info file");
407 return FAIL;
408 }
409
410 /* Read the file. There should be one of each parameter */
411 while ((lp = (char_u *)fgets((char *)buf, BUFSIZ, fp)) != NULL)
412 {
413 if ((nl = vim_strchr(lp, '\n')) != NULL)
414 *nl = 0; /* strip off the trailing newline */
415
416 if (STRNCMP(lp, "host=", 5) == 0)
417 {
418 vim_free(*host);
419 *host = (char *)vim_strsave(&buf[5]);
420 }
421 else if (STRNCMP(lp, "port=", 5) == 0)
422 {
423 vim_free(*port);
424 *port = (char *)vim_strsave(&buf[5]);
425 }
426 else if (STRNCMP(lp, "auth=", 5) == 0)
427 {
428 vim_free(*auth);
429 *auth = (char *)vim_strsave(&buf[5]);
430 }
431 }
432 fclose(fp);
433
434 return OK;
435}
436
437
438struct keyqueue
439{
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100440 char_u *keystr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 struct keyqueue *next;
442 struct keyqueue *prev;
443};
444
445typedef struct keyqueue keyQ_T;
446
447static keyQ_T keyHead; /* dummy node, header for circular queue */
448
449
450/*
451 * Queue up key commands sent from netbeans.
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100452 * We store the string, because it may depend on the global mod_mask and
453 * :nbkey doesn't have a key number.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454 */
455 static void
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100456postpone_keycommand(char_u *keystr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457{
458 keyQ_T *node;
459
460 node = (keyQ_T *)alloc(sizeof(keyQ_T));
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100461 if (node == NULL)
462 return; /* out of memory, drop the key */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463
464 if (keyHead.next == NULL) /* initialize circular queue */
465 {
466 keyHead.next = &keyHead;
467 keyHead.prev = &keyHead;
468 }
469
470 /* insert node at tail of queue */
471 node->next = &keyHead;
472 node->prev = keyHead.prev;
473 keyHead.prev->next = node;
474 keyHead.prev = node;
475
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100476 node->keystr = vim_strsave(keystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477}
478
479/*
480 * Handle any queued-up NetBeans keycommands to be send.
481 */
482 static void
483handle_key_queue(void)
484{
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100485 int postponed = FALSE;
486
487 while (!postponed && keyHead.next && keyHead.next != &keyHead)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488 {
489 /* first, unlink the node */
490 keyQ_T *node = keyHead.next;
491 keyHead.next = node->next;
492 node->next->prev = node->prev;
493
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100494 /* Now, send the keycommand. This may cause it to be postponed again
495 * and change keyHead. */
496 if (node->keystr != NULL)
497 postponed = !netbeans_keystring(node->keystr);
498 vim_free(node->keystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499
500 /* Finally, dispose of the node */
501 vim_free(node);
502 }
503}
504
505
506struct cmdqueue
507{
508 char_u *buffer;
509 struct cmdqueue *next;
510 struct cmdqueue *prev;
511};
512
513typedef struct cmdqueue queue_T;
514
515static queue_T head; /* dummy node, header for circular queue */
516
517
518/*
519 * Put the buffer on the work queue; possibly save it to a file as well.
520 */
521 static void
522save(char_u *buf, int len)
523{
524 queue_T *node;
525
526 node = (queue_T *)alloc(sizeof(queue_T));
527 if (node == NULL)
528 return; /* out of memory */
529 node->buffer = alloc(len + 1);
530 if (node->buffer == NULL)
531 {
532 vim_free(node);
533 return; /* out of memory */
534 }
535 mch_memmove(node->buffer, buf, (size_t)len);
536 node->buffer[len] = NUL;
537
538 if (head.next == NULL) /* initialize circular queue */
539 {
540 head.next = &head;
541 head.prev = &head;
542 }
543
544 /* insert node at tail of queue */
545 node->next = &head;
546 node->prev = head.prev;
547 head.prev->next = node;
548 head.prev = node;
549
550#ifdef NBDEBUG
551 {
552 static int outfd = -2;
553
554 /* possibly write buffer out to a file */
555 if (outfd == -3)
556 return;
557
558 if (outfd == -2)
559 {
560 char *file = getenv("__NETBEANS_SAVE");
561 if (file == NULL)
562 outfd = -3;
563 else
564 outfd = mch_open(file, O_WRONLY|O_CREAT|O_TRUNC, 0666);
565 }
566
567 if (outfd >= 0)
568 write(outfd, buf, len);
569 }
570#endif
571}
572
573
574/*
575 * While there's still a command in the work queue, parse and execute it.
576 */
Bram Moolenaarf2330482008-06-24 20:19:36 +0000577 void
578netbeans_parse_messages(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579{
580 char_u *p;
581 queue_T *node;
582
Bram Moolenaarf2330482008-06-24 20:19:36 +0000583 while (head.next != NULL && head.next != &head)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 {
585 node = head.next;
586
587 /* Locate the first line in the first buffer. */
588 p = vim_strchr(node->buffer, '\n');
589 if (p == NULL)
590 {
591 /* Command isn't complete. If there is no following buffer,
592 * return (wait for more). If there is another buffer following,
593 * prepend the text to that buffer and delete this one. */
594 if (node->next == &head)
595 return;
Bram Moolenaarf2330482008-06-24 20:19:36 +0000596 p = alloc((unsigned)(STRLEN(node->buffer)
597 + STRLEN(node->next->buffer) + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 if (p == NULL)
599 return; /* out of memory */
600 STRCPY(p, node->buffer);
601 STRCAT(p, node->next->buffer);
602 vim_free(node->next->buffer);
603 node->next->buffer = p;
604
605 /* dispose of the node and buffer */
606 head.next = node->next;
607 node->next->prev = node->prev;
608 vim_free(node->buffer);
609 vim_free(node);
610 }
611 else
612 {
613 /* There is a complete command at the start of the buffer.
614 * Terminate it with a NUL. When no more text is following unlink
615 * the buffer. Do this before executing, because new buffers can
616 * be added while busy handling the command. */
617 *p++ = NUL;
618 if (*p == NUL)
619 {
620 head.next = node->next;
621 node->next->prev = node->prev;
622 }
623
624 /* now, parse and execute the commands */
625 nb_parse_cmd(node->buffer);
626
627 if (*p == NUL)
628 {
629 /* buffer finished, dispose of the node and buffer */
630 vim_free(node->buffer);
631 vim_free(node);
632 }
633 else
634 {
635 /* more follows, move to the start */
Bram Moolenaarf2330482008-06-24 20:19:36 +0000636 STRMOVE(node->buffer, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 }
638 }
639 }
640}
641
642/* Buffer size for reading incoming messages. */
643#define MAXMSGSIZE 4096
644
645/*
Bram Moolenaar67c53842010-05-22 18:28:27 +0200646 * Read a command from netbeans.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 */
Bram Moolenaar67c53842010-05-22 18:28:27 +0200648#ifdef FEAT_GUI_MOTIF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 static void
Bram Moolenaara9d45512009-05-17 21:25:42 +0000650messageFromNetbeans(XtPointer clientData UNUSED,
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000651 int *unused1 UNUSED,
652 XtInputId *unused2 UNUSED)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200653{
654 netbeans_read();
655}
656#endif
657
658#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000660messageFromNetbeans(gpointer clientData UNUSED,
661 gint unused1 UNUSED,
662 GdkInputCondition unused2 UNUSED)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200663{
664 netbeans_read();
665}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666#endif
Bram Moolenaar67c53842010-05-22 18:28:27 +0200667
668 void
669netbeans_read()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670{
671 static char_u *buf = NULL;
Bram Moolenaar0b65f892010-05-15 14:49:02 +0200672 int len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 int readlen = 0;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200674#if defined(NB_HAS_GUI) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_W32)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 static int level = 0;
Bram Moolenaarf2330482008-06-24 20:19:36 +0000676#endif
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100677#ifdef HAVE_SELECT
678 struct timeval tval;
679 fd_set rfds;
680#else
681# ifdef HAVE_POLL
682 struct pollfd fds;
683# endif
684#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685
686 if (sd < 0)
687 {
688 nbdebug(("messageFromNetbeans() called without a socket\n"));
689 return;
690 }
691
Bram Moolenaar67c53842010-05-22 18:28:27 +0200692#if defined(NB_HAS_GUI) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_W32)
693 /* recursion guard; this will be called from the X event loop at unknown
694 * moments */
695 if (NB_HAS_GUI)
696 ++level;
Bram Moolenaarf2330482008-06-24 20:19:36 +0000697#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698
699 /* Allocate a buffer to read into. */
700 if (buf == NULL)
701 {
702 buf = alloc(MAXMSGSIZE);
703 if (buf == NULL)
704 return; /* out of memory! */
705 }
706
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100707 /* Keep on reading for as long as there is something to read.
708 * Use select() or poll() to avoid blocking on a message that is exactly
709 * MAXMSGSIZE long. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 for (;;)
711 {
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100712#ifdef HAVE_SELECT
713 FD_ZERO(&rfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +0200714 FD_SET(sd, &rfds);
715 tval.tv_sec = 0;
716 tval.tv_usec = 0;
717 if (select(sd + 1, &rfds, NULL, NULL, &tval) <= 0)
718 break;
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100719#else
720# ifdef HAVE_POLL
721 fds.fd = sd;
722 fds.events = POLLIN;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200723 if (poll(&fds, 1, 0) <= 0)
724 break;
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100725# endif
726#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 len = sock_read(sd, buf, MAXMSGSIZE);
728 if (len <= 0)
729 break; /* error or nothing more to read */
730
731 /* Store the read message in the queue. */
732 save(buf, len);
733 readlen += len;
734 if (len < MAXMSGSIZE)
735 break; /* did read everything that's available */
736 }
737
738 if (readlen <= 0)
739 {
740 /* read error or didn't read anything */
741 netbeans_disconnect();
742 nbdebug(("messageFromNetbeans: Error in read() from socket\n"));
743 if (len < 0)
Bram Moolenaarf2330482008-06-24 20:19:36 +0000744 {
745 nbdebug(("read from Netbeans socket\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 PERROR(_("read from Netbeans socket"));
Bram Moolenaarf2330482008-06-24 20:19:36 +0000747 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000748 return; /* don't try to parse it */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 }
750
Bram Moolenaar67c53842010-05-22 18:28:27 +0200751#if defined(NB_HAS_GUI) && !defined(FEAT_GUI_W32)
Bram Moolenaar61665aa2008-12-24 11:20:53 +0000752 /* Let the main loop handle messages. */
Bram Moolenaar67c53842010-05-22 18:28:27 +0200753 if (NB_HAS_GUI)
754 {
Bram Moolenaar61665aa2008-12-24 11:20:53 +0000755# ifdef FEAT_GUI_GTK
Bram Moolenaar67c53842010-05-22 18:28:27 +0200756 if (gtk_main_level() > 0)
757 gtk_main_quit();
758# else
759 /* Parse the messages now, but avoid recursion. */
760 if (level == 1)
761 netbeans_parse_messages();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762
Bram Moolenaar67c53842010-05-22 18:28:27 +0200763 --level;
764# endif
765 }
Bram Moolenaarf2330482008-06-24 20:19:36 +0000766#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767}
768
769/*
770 * Handle one NUL terminated command.
771 *
772 * format of a command from netbeans:
773 *
774 * 6:setTitle!84 "a.c"
775 *
776 * bufno
777 * colon
778 * cmd
779 * !
780 * cmdno
781 * args
782 *
783 * for function calls, the ! is replaced by a /
784 */
785 static void
786nb_parse_cmd(char_u *cmd)
787{
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000788 char *verb;
789 char *q;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 int bufno;
791 int isfunc = -1;
792
793 if (STRCMP(cmd, "DISCONNECT") == 0)
794 {
795 /* We assume the server knows that we can safely exit! */
796 if (sd >= 0)
797 sock_close(sd);
798 /* Disconnect before exiting, Motif hangs in a Select error
799 * message otherwise. */
800 netbeans_disconnect();
801 getout(0);
802 /* NOTREACHED */
803 }
804
805 if (STRCMP(cmd, "DETACH") == 0)
806 {
807 /* The IDE is breaking the connection. */
808 if (sd >= 0)
809 sock_close(sd);
810 netbeans_disconnect();
811 return;
812 }
813
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000814 bufno = strtol((char *)cmd, &verb, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815
816 if (*verb != ':')
817 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000818 nbdebug((" missing colon: %s\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 EMSG2("E627: missing colon: %s", cmd);
820 return;
821 }
822 ++verb; /* skip colon */
823
824 for (q = verb; *q; q++)
825 {
826 if (*q == '!')
827 {
828 *q++ = NUL;
829 isfunc = 0;
830 break;
831 }
832 else if (*q == '/')
833 {
834 *q++ = NUL;
835 isfunc = 1;
836 break;
837 }
838 }
839
840 if (isfunc < 0)
841 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000842 nbdebug((" missing ! or / in: %s\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 EMSG2("E628: missing ! or / in: %s", cmd);
844 return;
845 }
846
Bram Moolenaar89d40322006-08-29 15:30:07 +0000847 r_cmdno = strtol(q, &q, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000849 q = (char *)skipwhite((char_u *)q);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850
Bram Moolenaar89d40322006-08-29 15:30:07 +0000851 if (nb_do_cmd(bufno, (char_u *)verb, isfunc, r_cmdno, (char_u *)q) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 {
Bram Moolenaar009b2592004-10-24 19:18:58 +0000853#ifdef NBDEBUG
854 /*
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100855 * This happens because the ExtEd can send a command or 2 after
Bram Moolenaar009b2592004-10-24 19:18:58 +0000856 * doing a stopDocumentListen command. It doesn't harm anything
857 * so I'm disabling it except for debugging.
858 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 nbdebug(("nb_parse_cmd: Command error for \"%s\"\n", cmd));
860 EMSG("E629: bad return from nb_do_cmd");
Bram Moolenaar009b2592004-10-24 19:18:58 +0000861#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 }
863}
864
865struct nbbuf_struct
866{
867 buf_T *bufp;
868 unsigned int fireChanges:1;
869 unsigned int initDone:1;
Bram Moolenaar009b2592004-10-24 19:18:58 +0000870 unsigned int insertDone:1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 unsigned int modified:1;
Bram Moolenaar009b2592004-10-24 19:18:58 +0000872 int nbbuf_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 char *displayname;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 int *signmap;
875 short_u signmaplen;
876 short_u signmapused;
877};
878
879typedef struct nbbuf_struct nbbuf_T;
880
881static nbbuf_T *buf_list = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000882static int buf_list_size = 0; /* size of buf_list */
883static int buf_list_used = 0; /* nr of entries in buf_list actually in use */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884
885static char **globalsignmap;
886static int globalsignmaplen;
887static int globalsignmapused;
888
889static int mapsigntype __ARGS((nbbuf_T *, int localsigntype));
890static void addsigntype __ARGS((nbbuf_T *, int localsigntype, char_u *typeName,
891 char_u *tooltip, char_u *glyphfile,
Bram Moolenaar67c53842010-05-22 18:28:27 +0200892 char_u *fg, char_u *bg));
Bram Moolenaar009b2592004-10-24 19:18:58 +0000893static void print_read_msg __ARGS((nbbuf_T *buf));
894static void print_save_msg __ARGS((nbbuf_T *buf, long nchars));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895
896static int curPCtype = -1;
897
898/*
899 * Get the Netbeans buffer number for the specified buffer.
900 */
901 static int
902nb_getbufno(buf_T *bufp)
903{
904 int i;
905
906 for (i = 0; i < buf_list_used; i++)
907 if (buf_list[i].bufp == bufp)
908 return i;
909 return -1;
910}
911
912/*
913 * Is this a NetBeans-owned buffer?
914 */
915 int
916isNetbeansBuffer(buf_T *bufp)
917{
Bram Moolenaar009b2592004-10-24 19:18:58 +0000918 return usingNetbeans && bufp->b_netbeans_file;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919}
920
921/*
922 * NetBeans and Vim have different undo models. In Vim, the file isn't
923 * changed if changes are undone via the undo command. In NetBeans, once
924 * a change has been made the file is marked as modified until saved. It
925 * doesn't matter if the change was undone.
926 *
927 * So this function is for the corner case where Vim thinks a buffer is
928 * unmodified but NetBeans thinks it IS modified.
929 */
930 int
931isNetbeansModified(buf_T *bufp)
932{
Bram Moolenaar009b2592004-10-24 19:18:58 +0000933 if (usingNetbeans && bufp->b_netbeans_file)
934 {
935 int bufno = nb_getbufno(bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936
Bram Moolenaar009b2592004-10-24 19:18:58 +0000937 if (bufno > 0)
938 return buf_list[bufno].modified;
939 else
940 return FALSE;
941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 else
943 return FALSE;
944}
945
946/*
947 * Given a Netbeans buffer number, return the netbeans buffer.
948 * Returns NULL for 0 or a negative number. A 0 bufno means a
949 * non-buffer related command has been sent.
950 */
951 static nbbuf_T *
952nb_get_buf(int bufno)
953{
954 /* find or create a buffer with the given number */
955 int incr;
956
957 if (bufno <= 0)
958 return NULL;
959
960 if (!buf_list)
961 {
962 /* initialize */
963 buf_list = (nbbuf_T *)alloc_clear(100 * sizeof(nbbuf_T));
964 buf_list_size = 100;
965 }
966 if (bufno >= buf_list_used) /* new */
967 {
968 if (bufno >= buf_list_size) /* grow list */
969 {
970 incr = bufno - buf_list_size + 90;
971 buf_list_size += incr;
972 buf_list = (nbbuf_T *)vim_realloc(
973 buf_list, buf_list_size * sizeof(nbbuf_T));
974 memset(buf_list + buf_list_size - incr, 0, incr * sizeof(nbbuf_T));
975 }
976
977 while (buf_list_used <= bufno)
978 {
979 /* Default is to fire text changes. */
980 buf_list[buf_list_used].fireChanges = 1;
981 ++buf_list_used;
982 }
983 }
984
985 return buf_list + bufno;
986}
987
988/*
989 * Return the number of buffers that are modified.
990 */
991 static int
992count_changed_buffers(void)
993{
994 buf_T *bufp;
995 int n;
996
997 n = 0;
998 for (bufp = firstbuf; bufp != NULL; bufp = bufp->b_next)
999 if (bufp->b_changed)
1000 ++n;
1001 return n;
1002}
1003
1004/*
1005 * End the netbeans session.
1006 */
1007 void
1008netbeans_end(void)
1009{
1010 int i;
1011 static char buf[128];
1012
1013 if (!haveConnection)
1014 return;
1015
1016 for (i = 0; i < buf_list_used; i++)
1017 {
1018 if (!buf_list[i].bufp)
1019 continue;
1020 if (netbeansForcedQuit)
1021 {
1022 /* mark as unmodified so NetBeans won't put up dialog on "killed" */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001023 sprintf(buf, "%d:unmodified=%d\n", i, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 nbdebug(("EVT: %s", buf));
1025 nb_send(buf, "netbeans_end");
1026 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001027 sprintf(buf, "%d:killed=%d\n", i, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 nbdebug(("EVT: %s", buf));
1029/* nb_send(buf, "netbeans_end"); avoid "write failed" messages */
1030 if (sd >= 0)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00001031 ignored = sock_write(sd, buf, (int)STRLEN(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033}
1034
1035/*
1036 * Send a message to netbeans.
1037 */
1038 static void
1039nb_send(char *buf, char *fun)
1040{
1041 /* Avoid giving pages full of error messages when the other side has
1042 * exited, only mention the first error until the connection works again. */
1043 static int did_error = FALSE;
1044
1045 if (sd < 0)
1046 {
1047 if (!did_error)
Bram Moolenaarf2330482008-06-24 20:19:36 +00001048 {
1049 nbdebug((" %s(): write while not connected\n", fun));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 EMSG2("E630: %s(): write while not connected", fun);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 did_error = TRUE;
1053 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001054 else if (sock_write(sd, buf, (int)STRLEN(buf)) != (int)STRLEN(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 {
1056 if (!did_error)
Bram Moolenaarf2330482008-06-24 20:19:36 +00001057 {
1058 nbdebug((" %s(): write failed\n", fun));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 EMSG2("E631: %s(): write failed", fun);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 did_error = TRUE;
1062 }
1063 else
1064 did_error = FALSE;
1065}
1066
1067/*
1068 * Some input received from netbeans requires a response. This function
1069 * handles a response with no information (except the command number).
1070 */
1071 static void
1072nb_reply_nil(int cmdno)
1073{
1074 char reply[32];
1075
1076 if (!haveConnection)
1077 return;
1078
Bram Moolenaar009b2592004-10-24 19:18:58 +00001079 nbdebug(("REP %d: <none>\n", cmdno));
1080
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 sprintf(reply, "%d\n", cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 nb_send(reply, "nb_reply_nil");
1083}
1084
1085
1086/*
1087 * Send a response with text.
1088 * "result" must have been quoted already (using nb_quote()).
1089 */
1090 static void
1091nb_reply_text(int cmdno, char_u *result)
1092{
1093 char_u *reply;
1094
1095 if (!haveConnection)
1096 return;
1097
Bram Moolenaar009b2592004-10-24 19:18:58 +00001098 nbdebug(("REP %d: %s\n", cmdno, (char *)result));
1099
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001100 reply = alloc((unsigned)STRLEN(result) + 32);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 sprintf((char *)reply, "%d %s\n", cmdno, (char *)result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 nb_send((char *)reply, "nb_reply_text");
1103
1104 vim_free(reply);
1105}
1106
1107
1108/*
1109 * Send a response with a number result code.
1110 */
1111 static void
1112nb_reply_nr(int cmdno, long result)
1113{
1114 char reply[32];
1115
1116 if (!haveConnection)
1117 return;
1118
Bram Moolenaar009b2592004-10-24 19:18:58 +00001119 nbdebug(("REP %d: %ld\n", cmdno, result));
1120
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 sprintf(reply, "%d %ld\n", cmdno, result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 nb_send(reply, "nb_reply_nr");
1123}
1124
1125
1126/*
1127 * Encode newline, ret, backslash, double quote for transmission to NetBeans.
1128 */
1129 static char_u *
1130nb_quote(char_u *txt)
1131{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001132 char_u *buf = alloc((unsigned)(2 * STRLEN(txt) + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 char_u *p = txt;
1134 char_u *q = buf;
1135
1136 if (buf == NULL)
1137 return NULL;
1138 for (; *p; p++)
1139 {
1140 switch (*p)
1141 {
1142 case '\"':
1143 case '\\':
1144 *q++ = '\\'; *q++ = *p; break;
1145 /* case '\t': */
1146 /* *q++ = '\\'; *q++ = 't'; break; */
1147 case '\n':
1148 *q++ = '\\'; *q++ = 'n'; break;
1149 case '\r':
1150 *q++ = '\\'; *q++ = 'r'; break;
1151 default:
1152 *q++ = *p;
1153 break;
1154 }
1155 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01001156 *q = '\0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157
1158 return buf;
1159}
1160
1161
1162/*
1163 * Remove top level double quotes; convert backslashed chars.
1164 * Returns an allocated string (NULL for failure).
1165 * If "endp" is not NULL it is set to the character after the terminating
1166 * quote.
1167 */
1168 static char *
1169nb_unquote(char_u *p, char_u **endp)
1170{
1171 char *result = 0;
1172 char *q;
1173 int done = 0;
1174
1175 /* result is never longer than input */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001176 result = (char *)alloc_clear((unsigned)STRLEN(p) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 if (result == NULL)
1178 return NULL;
1179
1180 if (*p++ != '"')
1181 {
1182 nbdebug(("nb_unquote called with string that doesn't start with a quote!: %s\n",
1183 p));
1184 result[0] = NUL;
1185 return result;
1186 }
1187
1188 for (q = result; !done && *p != NUL;)
1189 {
1190 switch (*p)
1191 {
1192 case '"':
1193 /*
1194 * Unbackslashed dquote marks the end, if first char was dquote.
1195 */
1196 done = 1;
1197 break;
1198
1199 case '\\':
1200 ++p;
1201 switch (*p)
1202 {
1203 case '\\': *q++ = '\\'; break;
1204 case 'n': *q++ = '\n'; break;
1205 case 't': *q++ = '\t'; break;
1206 case 'r': *q++ = '\r'; break;
1207 case '"': *q++ = '"'; break;
1208 case NUL: --p; break;
1209 /* default: skip over illegal chars */
1210 }
1211 ++p;
1212 break;
1213
1214 default:
1215 *q++ = *p++;
1216 }
1217 }
1218
1219 if (endp != NULL)
1220 *endp = p;
1221
1222 return result;
1223}
1224
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001225/*
1226 * Remove from "first" byte to "last" byte (inclusive), at line "lnum" of the
1227 * current buffer. Remove to end of line when "last" is MAXCOL.
1228 */
1229 static void
1230nb_partialremove(linenr_T lnum, colnr_T first, colnr_T last)
1231{
1232 char_u *oldtext, *newtext;
1233 int oldlen;
1234 int lastbyte = last;
1235
1236 oldtext = ml_get(lnum);
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001237 oldlen = (int)STRLEN(oldtext);
Bram Moolenaarb3c70982008-01-18 10:40:55 +00001238 if (first >= (colnr_T)oldlen || oldlen == 0) /* just in case */
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001239 return;
1240 if (lastbyte >= oldlen)
1241 lastbyte = oldlen - 1;
1242 newtext = alloc(oldlen - (int)(lastbyte - first));
1243 if (newtext != NULL)
1244 {
1245 mch_memmove(newtext, oldtext, first);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001246 STRMOVE(newtext + first, oldtext + lastbyte + 1);
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001247 nbdebug((" NEW LINE %d: %s\n", lnum, newtext));
1248 ml_replace(lnum, newtext, FALSE);
1249 }
1250}
1251
1252/*
1253 * Replace the "first" line with the concatenation of the "first" and
1254 * the "other" line. The "other" line is not removed.
1255 */
1256 static void
1257nb_joinlines(linenr_T first, linenr_T other)
1258{
1259 int len_first, len_other;
1260 char_u *p;
1261
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001262 len_first = (int)STRLEN(ml_get(first));
1263 len_other = (int)STRLEN(ml_get(other));
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001264 p = alloc((unsigned)(len_first + len_other + 1));
1265 if (p != NULL)
1266 {
1267 mch_memmove(p, ml_get(first), len_first);
1268 mch_memmove(p + len_first, ml_get(other), len_other + 1);
1269 ml_replace(first, p, FALSE);
1270 }
1271}
1272
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273#define SKIP_STOP 2
1274#define streq(a,b) (strcmp(a,b) == 0)
1275static int needupdate = 0;
1276static int inAtomic = 0;
1277
1278/*
1279 * Do the actual processing of a single netbeans command or function.
Bram Moolenaar143c38c2007-05-10 16:41:10 +00001280 * The difference between a command and function is that a function
1281 * gets a response (it's required) but a command does not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 * For arguments see comment for nb_parse_cmd().
1283 */
1284 static int
1285nb_do_cmd(
1286 int bufno,
1287 char_u *cmd,
1288 int func,
1289 int cmdno,
1290 char_u *args) /* points to space before arguments or NUL */
1291{
1292 int doupdate = 0;
1293 long off = 0;
1294 nbbuf_T *buf = nb_get_buf(bufno);
1295 static int skip = 0;
1296 int retval = OK;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001297 char *cp; /* for when a char pointer is needed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298
1299 nbdebug(("%s %d: (%d) %s %s\n", (func) ? "FUN" : "CMD", cmdno, bufno, cmd,
1300 STRCMP(cmd, "insert") == 0 ? "<text>" : (char *)args));
1301
1302 if (func)
1303 {
1304/* =====================================================================*/
1305 if (streq((char *)cmd, "getModified"))
1306 {
1307 if (buf == NULL || buf->bufp == NULL)
1308 /* Return the number of buffers that are modified. */
1309 nb_reply_nr(cmdno, (long)count_changed_buffers());
1310 else
1311 /* Return whether the buffer is modified. */
1312 nb_reply_nr(cmdno, (long)(buf->bufp->b_changed
1313 || isNetbeansModified(buf->bufp)));
1314/* =====================================================================*/
1315 }
1316 else if (streq((char *)cmd, "saveAndExit"))
1317 {
1318 /* Note: this will exit Vim if successful. */
1319 coloncmd(":confirm qall");
1320
1321 /* We didn't exit: return the number of changed buffers. */
1322 nb_reply_nr(cmdno, (long)count_changed_buffers());
1323/* =====================================================================*/
1324 }
1325 else if (streq((char *)cmd, "getCursor"))
1326 {
1327 char_u text[200];
1328
1329 /* Note: nb_getbufno() may return -1. This indicates the IDE
1330 * didn't assign a number to the current buffer in response to a
1331 * fileOpened event. */
1332 sprintf((char *)text, "%d %ld %d %ld",
1333 nb_getbufno(curbuf),
1334 (long)curwin->w_cursor.lnum,
1335 (int)curwin->w_cursor.col,
1336 pos2off(curbuf, &curwin->w_cursor));
1337 nb_reply_text(cmdno, text);
1338/* =====================================================================*/
1339 }
Bram Moolenaarc65c4912006-11-14 17:29:46 +00001340 else if (streq((char *)cmd, "getAnno"))
1341 {
1342 long linenum = 0;
1343#ifdef FEAT_SIGNS
1344 if (buf == NULL || buf->bufp == NULL)
1345 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001346 nbdebug((" Invalid buffer identifier in getAnno\n"));
1347 EMSG("E652: Invalid buffer identifier in getAnno");
Bram Moolenaarc65c4912006-11-14 17:29:46 +00001348 retval = FAIL;
1349 }
1350 else
1351 {
1352 int serNum;
1353
1354 cp = (char *)args;
1355 serNum = strtol(cp, &cp, 10);
1356 /* If the sign isn't found linenum will be zero. */
1357 linenum = (long)buf_findsign(buf->bufp, serNum);
1358 }
1359#endif
1360 nb_reply_nr(cmdno, linenum);
1361/* =====================================================================*/
1362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 else if (streq((char *)cmd, "getLength"))
1364 {
1365 long len = 0;
1366
1367 if (buf == NULL || buf->bufp == NULL)
1368 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001369 nbdebug((" invalid buffer identifier in getLength\n"));
1370 EMSG("E632: invalid buffer identifier in getLength");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 retval = FAIL;
1372 }
1373 else
1374 {
1375 len = get_buf_size(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 }
1377 nb_reply_nr(cmdno, len);
1378/* =====================================================================*/
1379 }
1380 else if (streq((char *)cmd, "getText"))
1381 {
1382 long len;
1383 linenr_T nlines;
1384 char_u *text = NULL;
1385 linenr_T lno = 1;
1386 char_u *p;
1387 char_u *line;
1388
1389 if (buf == NULL || buf->bufp == NULL)
1390 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001391 nbdebug((" invalid buffer identifier in getText\n"));
1392 EMSG("E633: invalid buffer identifier in getText");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 retval = FAIL;
1394 }
1395 else
1396 {
1397 len = get_buf_size(buf->bufp);
1398 nlines = buf->bufp->b_ml.ml_line_count;
1399 text = alloc((unsigned)((len > 0)
1400 ? ((len + nlines) * 2) : 4));
1401 if (text == NULL)
1402 {
1403 nbdebug((" nb_do_cmd: getText has null text field\n"));
1404 retval = FAIL;
1405 }
1406 else
1407 {
1408 p = text;
1409 *p++ = '\"';
1410 for (; lno <= nlines ; lno++)
1411 {
1412 line = nb_quote(ml_get_buf(buf->bufp, lno, FALSE));
1413 if (line != NULL)
1414 {
1415 STRCPY(p, line);
1416 p += STRLEN(line);
1417 *p++ = '\\';
1418 *p++ = 'n';
Bram Moolenaar009b2592004-10-24 19:18:58 +00001419 vim_free(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 }
1422 *p++ = '\"';
1423 *p = '\0';
1424 }
1425 }
1426 if (text == NULL)
1427 nb_reply_text(cmdno, (char_u *)"");
1428 else
1429 {
1430 nb_reply_text(cmdno, text);
1431 vim_free(text);
1432 }
1433/* =====================================================================*/
1434 }
1435 else if (streq((char *)cmd, "remove"))
1436 {
1437 long count;
1438 pos_T first, last;
1439 pos_T *pos;
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001440 pos_T *next;
1441 linenr_T del_from_lnum, del_to_lnum; /* lines to be deleted as a whole */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 int oldFire = netbeansFireChanges;
1443 int oldSuppress = netbeansSuppressNoLines;
1444 int wasChanged;
1445
1446 if (skip >= SKIP_STOP)
1447 {
1448 nbdebug((" Skipping %s command\n", (char *) cmd));
1449 nb_reply_nil(cmdno);
1450 return OK;
1451 }
1452
1453 if (buf == NULL || buf->bufp == NULL)
1454 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001455 nbdebug((" invalid buffer identifier in remove\n"));
1456 EMSG("E634: invalid buffer identifier in remove");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 retval = FAIL;
1458 }
1459 else
1460 {
1461 netbeansFireChanges = FALSE;
1462 netbeansSuppressNoLines = TRUE;
1463
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001464 nb_set_curbuf(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 wasChanged = buf->bufp->b_changed;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001466 cp = (char *)args;
1467 off = strtol(cp, &cp, 10);
1468 count = strtol(cp, &cp, 10);
1469 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 /* delete "count" chars, starting at "off" */
1471 pos = off2pos(buf->bufp, off);
1472 if (!pos)
1473 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001474 nbdebug((" !bad position\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 nb_reply_text(cmdno, (char_u *)"!bad position");
1476 netbeansFireChanges = oldFire;
1477 netbeansSuppressNoLines = oldSuppress;
1478 return FAIL;
1479 }
1480 first = *pos;
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001481 nbdebug((" FIRST POS: line %d, col %d\n",
1482 first.lnum, first.col));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 pos = off2pos(buf->bufp, off+count-1);
1484 if (!pos)
1485 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001486 nbdebug((" !bad count\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 nb_reply_text(cmdno, (char_u *)"!bad count");
1488 netbeansFireChanges = oldFire;
1489 netbeansSuppressNoLines = oldSuppress;
1490 return FAIL;
1491 }
1492 last = *pos;
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001493 nbdebug((" LAST POS: line %d, col %d\n",
1494 last.lnum, last.col));
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001495 del_from_lnum = first.lnum;
1496 del_to_lnum = last.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 doupdate = 1;
1498
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001499 /* Get the position of the first byte after the deleted
1500 * section. "next" is NULL when deleting to the end of the
1501 * file. */
1502 next = off2pos(buf->bufp, off + count);
1503
1504 /* Remove part of the first line. */
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001505 if (first.col != 0
1506 || (next != NULL && first.lnum == next->lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 {
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001508 if (first.lnum != last.lnum
1509 || (next != NULL && first.lnum != next->lnum))
1510 {
1511 /* remove to the end of the first line */
1512 nb_partialremove(first.lnum, first.col,
1513 (colnr_T)MAXCOL);
1514 if (first.lnum == last.lnum)
1515 {
1516 /* Partial line to remove includes the end of
1517 * line. Join the line with the next one, have
1518 * the next line deleted below. */
1519 nb_joinlines(first.lnum, next->lnum);
1520 del_to_lnum = next->lnum;
1521 }
1522 }
1523 else
1524 {
1525 /* remove within one line */
1526 nb_partialremove(first.lnum, first.col, last.col);
1527 }
1528 ++del_from_lnum; /* don't delete the first line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 }
1530
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001531 /* Remove part of the last line. */
1532 if (first.lnum != last.lnum && next != NULL
1533 && next->col != 0 && last.lnum == next->lnum)
1534 {
1535 nb_partialremove(last.lnum, 0, last.col);
1536 if (del_from_lnum > first.lnum)
1537 {
1538 /* Join end of last line to start of first line; last
1539 * line is deleted below. */
1540 nb_joinlines(first.lnum, last.lnum);
1541 }
1542 else
1543 /* First line is deleted as a whole, keep the last
1544 * line. */
1545 --del_to_lnum;
1546 }
1547
1548 /* First is partial line; last line to remove includes
1549 * the end of line; join first line to line following last
1550 * line; line following last line is deleted below. */
1551 if (first.lnum != last.lnum && del_from_lnum > first.lnum
1552 && next != NULL && last.lnum != next->lnum)
1553 {
1554 nb_joinlines(first.lnum, next->lnum);
1555 del_to_lnum = next->lnum;
1556 }
1557
1558 /* Delete whole lines if there are any. */
1559 if (del_to_lnum >= del_from_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 {
1561 int i;
1562
1563 /* delete signs from the lines being deleted */
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001564 for (i = del_from_lnum; i <= del_to_lnum; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 {
1566 int id = buf_findsign_id(buf->bufp, (linenr_T)i);
1567 if (id > 0)
1568 {
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001569 nbdebug((" Deleting sign %d on line %d\n",
1570 id, i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 buf_delsign(buf->bufp, id);
1572 }
1573 else
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001574 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 nbdebug((" No sign on line %d\n", i));
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 }
1578
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001579 nbdebug((" Deleting lines %d through %d\n",
1580 del_from_lnum, del_to_lnum));
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001581 curwin->w_cursor.lnum = del_from_lnum;
1582 curwin->w_cursor.col = 0;
1583 del_lines(del_to_lnum - del_from_lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 }
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001585
1586 /* Leave cursor at first deleted byte. */
1587 curwin->w_cursor = first;
1588 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 buf->bufp->b_changed = wasChanged; /* logically unchanged */
1590 netbeansFireChanges = oldFire;
1591 netbeansSuppressNoLines = oldSuppress;
1592
1593 u_blockfree(buf->bufp);
1594 u_clearall(buf->bufp);
1595 }
1596 nb_reply_nil(cmdno);
1597/* =====================================================================*/
1598 }
1599 else if (streq((char *)cmd, "insert"))
1600 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 char_u *to_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602
1603 if (skip >= SKIP_STOP)
1604 {
1605 nbdebug((" Skipping %s command\n", (char *) cmd));
1606 nb_reply_nil(cmdno);
1607 return OK;
1608 }
1609
1610 /* get offset */
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001611 cp = (char *)args;
1612 off = strtol(cp, &cp, 10);
1613 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614
1615 /* get text to be inserted */
1616 args = skipwhite(args);
1617 args = to_free = (char_u *)nb_unquote(args, NULL);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001618 /*
1619 nbdebug((" CHUNK[%d]: %d bytes at offset %d\n",
1620 buf->bufp->b_ml.ml_line_count, STRLEN(args), off));
1621 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622
1623 if (buf == NULL || buf->bufp == NULL)
1624 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001625 nbdebug((" invalid buffer identifier in insert\n"));
1626 EMSG("E635: invalid buffer identifier in insert");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 retval = FAIL;
1628 }
1629 else if (args != NULL)
1630 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001631 int ff_detected = EOL_UNKNOWN;
1632 int buf_was_empty = (buf->bufp->b_ml.ml_flags & ML_EMPTY);
1633 size_t len = 0;
1634 int added = 0;
1635 int oldFire = netbeansFireChanges;
1636 int old_b_changed;
1637 char_u *nl;
1638 linenr_T lnum;
1639 linenr_T lnum_start;
1640 pos_T *pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 netbeansFireChanges = 0;
1643
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001644 /* Jump to the buffer where we insert. After this "curbuf"
1645 * can be used. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001646 nb_set_curbuf(buf->bufp);
Bram Moolenaara3227e22006-03-08 21:32:40 +00001647 old_b_changed = curbuf->b_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001649 /* Convert the specified character offset into a lnum/col
1650 * position. */
Bram Moolenaara3227e22006-03-08 21:32:40 +00001651 pos = off2pos(curbuf, off);
1652 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001654 if (pos->lnum <= 0)
1655 lnum_start = 1;
1656 else
1657 lnum_start = pos->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 }
1659 else
1660 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001661 /* If the given position is not found, assume we want
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 * the end of the file. See setLocAndSize HACK. */
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001663 if (buf_was_empty)
1664 lnum_start = 1; /* above empty line */
1665 else
1666 lnum_start = curbuf->b_ml.ml_line_count + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001667 }
1668
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001669 /* "lnum" is the line where we insert: either append to it or
1670 * insert a new line above it. */
1671 lnum = lnum_start;
1672
1673 /* Loop over the "\n" separated lines of the argument. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 doupdate = 1;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001675 while (*args != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001677 nl = vim_strchr(args, '\n');
1678 if (nl == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001680 /* Incomplete line, probably truncated. Next "insert"
1681 * command should append to this one. */
1682 len = STRLEN(args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001684 else
1685 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001686 len = nl - args;
1687
1688 /*
1689 * We need to detect EOL style, because the commands
1690 * use a character offset.
1691 */
1692 if (nl > args && nl[-1] == '\r')
1693 {
1694 ff_detected = EOL_DOS;
1695 --len;
1696 }
1697 else
1698 ff_detected = EOL_UNIX;
1699 }
1700 args[len] = NUL;
1701
1702 if (lnum == lnum_start
1703 && ((pos != NULL && pos->col > 0)
1704 || (lnum == 1 && buf_was_empty)))
1705 {
1706 char_u *oldline = ml_get(lnum);
1707 char_u *newline;
1708
1709 /* Insert halfway a line. For simplicity we assume we
1710 * need to append to the line. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001711 newline = alloc_check((unsigned)(STRLEN(oldline) + len + 1));
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001712 if (newline != NULL)
1713 {
1714 STRCPY(newline, oldline);
1715 STRCAT(newline, args);
1716 ml_replace(lnum, newline, FALSE);
1717 }
1718 }
1719 else
1720 {
1721 /* Append a new line. Not that we always do this,
1722 * also when the text doesn't end in a "\n". */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001723 ml_append((linenr_T)(lnum - 1), args, (colnr_T)(len + 1), FALSE);
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001724 ++added;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001725 }
1726
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001727 if (nl == NULL)
1728 break;
1729 ++lnum;
1730 args = nl + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001731 }
1732
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001733 /* Adjust the marks below the inserted lines. */
1734 appended_lines_mark(lnum_start - 1, (long)added);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001736 /*
1737 * When starting with an empty buffer set the fileformat.
1738 * This is just guessing...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 */
1740 if (buf_was_empty)
1741 {
1742 if (ff_detected == EOL_UNKNOWN)
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001743#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 ff_detected = EOL_DOS;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001745#else
1746 ff_detected = EOL_UNIX;
1747#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 set_fileformat(ff_detected, OPT_LOCAL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00001749 curbuf->b_start_ffc = *curbuf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 }
1751
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 /*
1753 * XXX - GRP - Is the next line right? If I've inserted
1754 * text the buffer has been updated but not written. Will
1755 * netbeans guarantee to write it? Even if I do a :q! ?
1756 */
Bram Moolenaara3227e22006-03-08 21:32:40 +00001757 curbuf->b_changed = old_b_changed; /* logically unchanged */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 netbeansFireChanges = oldFire;
1759
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001760 /* Undo info is invalid now... */
Bram Moolenaara3227e22006-03-08 21:32:40 +00001761 u_blockfree(curbuf);
1762 u_clearall(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 }
1764 vim_free(to_free);
1765 nb_reply_nil(cmdno); /* or !error */
1766 }
1767 else
1768 {
1769 nbdebug(("UNIMPLEMENTED FUNCTION: %s\n", cmd));
1770 nb_reply_nil(cmdno);
1771 retval = FAIL;
1772 }
1773 }
1774 else /* Not a function; no reply required. */
1775 {
1776/* =====================================================================*/
1777 if (streq((char *)cmd, "create"))
1778 {
1779 /* Create a buffer without a name. */
1780 if (buf == NULL)
1781 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001782 nbdebug((" invalid buffer identifier in create\n"));
1783 EMSG("E636: invalid buffer identifier in create");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 return FAIL;
1785 }
1786 vim_free(buf->displayname);
1787 buf->displayname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788
1789 netbeansReadFile = 0; /* don't try to open disk file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001790 do_ecmd(0, NULL, 0, 0, ECMD_ONE, ECMD_HIDE + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 netbeansReadFile = 1;
1792 buf->bufp = curbuf;
1793 maketitle();
Bram Moolenaar009b2592004-10-24 19:18:58 +00001794 buf->insertDone = FALSE;
Bram Moolenaar67c53842010-05-22 18:28:27 +02001795#if defined(FEAT_MENU) && defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 gui_update_menus(0);
Bram Moolenaar67c53842010-05-22 18:28:27 +02001797#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798/* =====================================================================*/
1799 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001800 else if (streq((char *)cmd, "insertDone"))
1801 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001802 if (buf == NULL || buf->bufp == NULL)
1803 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001804 nbdebug((" invalid buffer identifier in insertDone\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001805 }
1806 else
1807 {
1808 buf->bufp->b_start_eol = *args == 'T';
1809 buf->insertDone = TRUE;
1810 args += 2;
1811 buf->bufp->b_p_ro = *args == 'T';
1812 print_read_msg(buf);
1813 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001814/* =====================================================================*/
1815 }
1816 else if (streq((char *)cmd, "saveDone"))
1817 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001818 long savedChars = atol((char *)args);
1819
1820 if (buf == NULL || buf->bufp == NULL)
1821 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001822 nbdebug((" invalid buffer identifier in saveDone\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001823 }
1824 else
1825 print_save_msg(buf, savedChars);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001826/* =====================================================================*/
1827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 else if (streq((char *)cmd, "startDocumentListen"))
1829 {
1830 if (buf == NULL)
1831 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001832 nbdebug((" invalid buffer identifier in startDocumentListen\n"));
1833 EMSG("E637: invalid buffer identifier in startDocumentListen");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 return FAIL;
1835 }
1836 buf->fireChanges = 1;
1837/* =====================================================================*/
1838 }
1839 else if (streq((char *)cmd, "stopDocumentListen"))
1840 {
1841 if (buf == NULL)
1842 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001843 nbdebug((" invalid buffer identifier in stopDocumentListen\n"));
1844 EMSG("E638: invalid buffer identifier in stopDocumentListen");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 return FAIL;
1846 }
1847 buf->fireChanges = 0;
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001848 if (buf->bufp != NULL && buf->bufp->b_was_netbeans_file)
Bram Moolenaar009b2592004-10-24 19:18:58 +00001849 {
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001850 if (!buf->bufp->b_netbeans_file)
Bram Moolenaarf2330482008-06-24 20:19:36 +00001851 {
1852 nbdebug(("E658: NetBeans connection lost for buffer %ld\n", buf->bufp->b_fnum));
Bram Moolenaar009b2592004-10-24 19:18:58 +00001853 EMSGN(_("E658: NetBeans connection lost for buffer %ld"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 buf->bufp->b_fnum);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001855 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001856 else
1857 {
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001858 /* NetBeans uses stopDocumentListen when it stops editing
1859 * a file. It then expects the buffer in Vim to
1860 * disappear. */
1861 do_bufdel(DOBUF_DEL, (char_u *)"", 1,
1862 buf->bufp->b_fnum, buf->bufp->b_fnum, TRUE);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001863 vim_memset(buf, 0, sizeof(nbbuf_T));
1864 }
1865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866/* =====================================================================*/
1867 }
1868 else if (streq((char *)cmd, "setTitle"))
1869 {
1870 if (buf == NULL)
1871 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001872 nbdebug((" invalid buffer identifier in setTitle\n"));
1873 EMSG("E639: invalid buffer identifier in setTitle");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 return FAIL;
1875 }
1876 vim_free(buf->displayname);
1877 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878/* =====================================================================*/
1879 }
1880 else if (streq((char *)cmd, "initDone"))
1881 {
1882 if (buf == NULL || buf->bufp == NULL)
1883 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001884 nbdebug((" invalid buffer identifier in initDone\n"));
1885 EMSG("E640: invalid buffer identifier in initDone");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 return FAIL;
1887 }
1888 doupdate = 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001889 buf->initDone = TRUE;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001890 nb_set_curbuf(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891#if defined(FEAT_AUTOCMD)
1892 apply_autocmds(EVENT_BUFREADPOST, 0, 0, FALSE, buf->bufp);
1893#endif
1894
1895 /* handle any postponed key commands */
1896 handle_key_queue();
1897/* =====================================================================*/
1898 }
1899 else if (streq((char *)cmd, "setBufferNumber")
1900 || streq((char *)cmd, "putBufferNumber"))
1901 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00001902 char_u *path;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 buf_T *bufp;
1904
1905 if (buf == NULL)
1906 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001907 nbdebug((" invalid buffer identifier in setBufferNumber\n"));
1908 EMSG("E641: invalid buffer identifier in setBufferNumber");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 return FAIL;
1910 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001911 path = (char_u *)nb_unquote(args, NULL);
1912 if (path == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 return FAIL;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001914 bufp = buflist_findname(path);
1915 vim_free(path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 if (bufp == NULL)
1917 {
Bram Moolenaarec906222009-02-21 21:14:00 +00001918 nbdebug((" File %s not found in setBufferNumber\n", args));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 EMSG2("E642: File %s not found in setBufferNumber", args);
1920 return FAIL;
1921 }
1922 buf->bufp = bufp;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001923 buf->nbbuf_number = bufp->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924
1925 /* "setBufferNumber" has the side effect of jumping to the buffer
1926 * (don't know why!). Don't do that for "putBufferNumber". */
1927 if (*cmd != 'p')
1928 coloncmd(":buffer %d", bufp->b_fnum);
1929 else
1930 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00001931 buf->initDone = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932
1933 /* handle any postponed key commands */
1934 handle_key_queue();
1935 }
1936
1937#if 0 /* never used */
1938 buf->internalname = (char *)alloc_clear(8);
1939 sprintf(buf->internalname, "<%d>", bufno);
1940 buf->netbeansOwns = 0;
1941#endif
1942/* =====================================================================*/
1943 }
1944 else if (streq((char *)cmd, "setFullName"))
1945 {
1946 if (buf == NULL)
1947 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001948 nbdebug((" invalid buffer identifier in setFullName\n"));
1949 EMSG("E643: invalid buffer identifier in setFullName");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 return FAIL;
1951 }
1952 vim_free(buf->displayname);
1953 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954
1955 netbeansReadFile = 0; /* don't try to open disk file */
1956 do_ecmd(0, (char_u *)buf->displayname, 0, 0, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001957 ECMD_HIDE + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 netbeansReadFile = 1;
1959 buf->bufp = curbuf;
1960 maketitle();
Bram Moolenaar67c53842010-05-22 18:28:27 +02001961#if defined(FEAT_MENU) && defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 gui_update_menus(0);
Bram Moolenaar67c53842010-05-22 18:28:27 +02001963#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964/* =====================================================================*/
1965 }
1966 else if (streq((char *)cmd, "editFile"))
1967 {
1968 if (buf == NULL)
1969 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001970 nbdebug((" invalid buffer identifier in editFile\n"));
1971 EMSG("E644: invalid buffer identifier in editFile");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 return FAIL;
1973 }
1974 /* Edit a file: like create + setFullName + read the file. */
1975 vim_free(buf->displayname);
1976 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 do_ecmd(0, (char_u *)buf->displayname, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001978 ECMD_HIDE + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 buf->bufp = curbuf;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001980 buf->initDone = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 doupdate = 1;
1982#if defined(FEAT_TITLE)
1983 maketitle();
1984#endif
Bram Moolenaar67c53842010-05-22 18:28:27 +02001985#if defined(FEAT_MENU) && defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 gui_update_menus(0);
Bram Moolenaar67c53842010-05-22 18:28:27 +02001987#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988/* =====================================================================*/
1989 }
1990 else if (streq((char *)cmd, "setVisible"))
1991 {
1992 if (buf == NULL || buf->bufp == NULL)
1993 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001994 nbdebug((" invalid buffer identifier in setVisible\n"));
1995 /* This message was commented out, probably because it can
1996 * happen when shutting down. */
1997 if (p_verbose > 0)
1998 EMSG("E645: invalid buffer identifier in setVisible");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 return FAIL;
2000 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002001 if (streq((char *)args, "T") && buf->bufp != curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 {
2003 exarg_T exarg;
2004 exarg.cmd = (char_u *)"goto";
2005 exarg.forceit = FALSE;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002006 dosetvisible = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 goto_buffer(&exarg, DOBUF_FIRST, FORWARD, buf->bufp->b_fnum);
2008 doupdate = 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002009 dosetvisible = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010
Bram Moolenaar67c53842010-05-22 18:28:27 +02002011#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 /* Side effect!!!. */
2013 if (!gui.starting)
2014 gui_mch_set_foreground();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002015#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017/* =====================================================================*/
2018 }
2019 else if (streq((char *)cmd, "raise"))
2020 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002021#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 /* Bring gvim to the foreground. */
2023 if (!gui.starting)
2024 gui_mch_set_foreground();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002025#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026/* =====================================================================*/
2027 }
2028 else if (streq((char *)cmd, "setModified"))
2029 {
Bram Moolenaarff064e12008-06-09 13:10:45 +00002030 int prev_b_changed;
2031
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 if (buf == NULL || buf->bufp == NULL)
2033 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002034 nbdebug((" invalid buffer identifier in setModified\n"));
2035 /* This message was commented out, probably because it can
2036 * happen when shutting down. */
2037 if (p_verbose > 0)
2038 EMSG("E646: invalid buffer identifier in setModified");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 return FAIL;
2040 }
Bram Moolenaarff064e12008-06-09 13:10:45 +00002041 prev_b_changed = buf->bufp->b_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 if (streq((char *)args, "T"))
Bram Moolenaarff064e12008-06-09 13:10:45 +00002043 buf->bufp->b_changed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 else
2045 {
2046 struct stat st;
2047
2048 /* Assume NetBeans stored the file. Reset the timestamp to
2049 * avoid "file changed" warnings. */
2050 if (buf->bufp->b_ffname != NULL
2051 && mch_stat((char *)buf->bufp->b_ffname, &st) >= 0)
2052 buf_store_time(buf->bufp, &st, buf->bufp->b_ffname);
Bram Moolenaarff064e12008-06-09 13:10:45 +00002053 buf->bufp->b_changed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 }
2055 buf->modified = buf->bufp->b_changed;
Bram Moolenaarff064e12008-06-09 13:10:45 +00002056 if (prev_b_changed != buf->bufp->b_changed)
2057 {
2058#ifdef FEAT_WINDOWS
2059 check_status(buf->bufp);
2060 redraw_tabline = TRUE;
2061#endif
2062#ifdef FEAT_TITLE
2063 maketitle();
2064#endif
2065 update_screen(0);
2066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067/* =====================================================================*/
2068 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002069 else if (streq((char *)cmd, "setModtime"))
2070 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002071 if (buf == NULL || buf->bufp == NULL)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002072 nbdebug((" invalid buffer identifier in setModtime\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002073 else
2074 buf->bufp->b_mtime = atoi((char *)args);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002075/* =====================================================================*/
2076 }
2077 else if (streq((char *)cmd, "setReadOnly"))
2078 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002079 if (buf == NULL || buf->bufp == NULL)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002080 nbdebug((" invalid buffer identifier in setReadOnly\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002081 else if (streq((char *)args, "T"))
Bram Moolenaar009b2592004-10-24 19:18:58 +00002082 buf->bufp->b_p_ro = TRUE;
2083 else
2084 buf->bufp->b_p_ro = FALSE;
2085/* =====================================================================*/
2086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 else if (streq((char *)cmd, "setMark"))
2088 {
2089 /* not yet */
2090/* =====================================================================*/
2091 }
2092 else if (streq((char *)cmd, "showBalloon"))
2093 {
2094#if defined(FEAT_BEVAL)
2095 static char *text = NULL;
2096
2097 /*
2098 * Set up the Balloon Expression Evaluation area.
2099 * Ignore 'ballooneval' here.
2100 * The text pointer must remain valid for a while.
2101 */
2102 if (balloonEval != NULL)
2103 {
2104 vim_free(text);
2105 text = nb_unquote(args, NULL);
2106 if (text != NULL)
2107 gui_mch_post_balloon(balloonEval, (char_u *)text);
2108 }
2109#endif
2110/* =====================================================================*/
2111 }
2112 else if (streq((char *)cmd, "setDot"))
2113 {
2114 pos_T *pos;
2115#ifdef NBDEBUG
2116 char_u *s;
2117#endif
2118
2119 if (buf == NULL || buf->bufp == NULL)
2120 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002121 nbdebug((" invalid buffer identifier in setDot\n"));
2122 EMSG("E647: invalid buffer identifier in setDot");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 return FAIL;
2124 }
2125
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002126 nb_set_curbuf(buf->bufp);
2127
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128#ifdef FEAT_VISUAL
2129 /* Don't want Visual mode now. */
2130 if (VIsual_active)
2131 end_visual_mode();
2132#endif
2133#ifdef NBDEBUG
2134 s = args;
2135#endif
2136 pos = get_off_or_lnum(buf->bufp, &args);
2137 if (pos)
2138 {
2139 curwin->w_cursor = *pos;
2140 check_cursor();
2141#ifdef FEAT_FOLDING
2142 foldOpenCursor();
2143#endif
2144 }
2145 else
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002146 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 nbdebug((" BAD POSITION in setDot: %s\n", s));
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149
2150 /* gui_update_cursor(TRUE, FALSE); */
2151 /* update_curbuf(NOT_VALID); */
2152 update_topline(); /* scroll to show the line */
2153 update_screen(VALID);
2154 setcursor();
2155 out_flush();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002156#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 gui_update_cursor(TRUE, FALSE);
2158 gui_mch_flush();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002159#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 /* Quit a hit-return or more prompt. */
2161 if (State == HITRETURN || State == ASKMORE)
2162 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163#ifdef FEAT_GUI_GTK
2164 if (gtk_main_level() > 0)
2165 gtk_main_quit();
2166#endif
2167 }
2168/* =====================================================================*/
2169 }
2170 else if (streq((char *)cmd, "close"))
2171 {
2172#ifdef NBDEBUG
2173 char *name = "<NONE>";
2174#endif
2175
2176 if (buf == NULL)
2177 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002178 nbdebug((" invalid buffer identifier in close\n"));
2179 EMSG("E648: invalid buffer identifier in close");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 return FAIL;
2181 }
2182
2183#ifdef NBDEBUG
2184 if (buf->displayname != NULL)
2185 name = buf->displayname;
2186#endif
Bram Moolenaarf2330482008-06-24 20:19:36 +00002187 if (buf->bufp == NULL)
2188 {
2189 nbdebug((" invalid buffer identifier in close\n"));
2190 /* This message was commented out, probably because it can
2191 * happen when shutting down. */
2192 if (p_verbose > 0)
2193 EMSG("E649: invalid buffer identifier in close");
2194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 nbdebug((" CLOSE %d: %s\n", bufno, name));
Bram Moolenaar67c53842010-05-22 18:28:27 +02002196#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 need_mouse_correct = TRUE;
Bram Moolenaar67c53842010-05-22 18:28:27 +02002198#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 if (buf->bufp != NULL)
2200 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD,
2201 buf->bufp->b_fnum, TRUE);
Bram Moolenaar8d602722006-08-08 19:34:19 +00002202 buf->bufp = NULL;
2203 buf->initDone = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 doupdate = 1;
2205/* =====================================================================*/
2206 }
2207 else if (streq((char *)cmd, "setStyle")) /* obsolete... */
2208 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002209 nbdebug((" setStyle is obsolete!\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210/* =====================================================================*/
2211 }
2212 else if (streq((char *)cmd, "setExitDelay"))
2213 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002214 /* Only used in version 2.1. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215/* =====================================================================*/
2216 }
2217 else if (streq((char *)cmd, "defineAnnoType"))
2218 {
2219#ifdef FEAT_SIGNS
2220 int typeNum;
2221 char_u *typeName;
2222 char_u *tooltip;
2223 char_u *p;
2224 char_u *glyphFile;
Bram Moolenaar67c53842010-05-22 18:28:27 +02002225 int parse_error = FALSE;
2226 char_u *fg;
2227 char_u *bg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228
2229 if (buf == NULL)
2230 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002231 nbdebug((" invalid buffer identifier in defineAnnoType\n"));
2232 EMSG("E650: invalid buffer identifier in defineAnnoType");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 return FAIL;
2234 }
2235
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002236 cp = (char *)args;
2237 typeNum = strtol(cp, &cp, 10);
2238 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 args = skipwhite(args);
2240 typeName = (char_u *)nb_unquote(args, &args);
2241 args = skipwhite(args + 1);
2242 tooltip = (char_u *)nb_unquote(args, &args);
2243 args = skipwhite(args + 1);
2244
2245 p = (char_u *)nb_unquote(args, &args);
2246 glyphFile = vim_strsave_escaped(p, escape_chars);
2247 vim_free(p);
2248
2249 args = skipwhite(args + 1);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002250 p = skiptowhite(args);
2251 if (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002253 *p = NUL;
2254 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02002256 fg = vim_strsave(args);
2257 bg = vim_strsave(p);
2258 if (STRLEN(fg) > MAX_COLOR_LENGTH || STRLEN(bg) > MAX_COLOR_LENGTH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002260 EMSG("E532: highlighting color name too long in defineAnnoType");
2261 vim_free(typeName);
2262 parse_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02002264 else if (typeName != NULL && tooltip != NULL && glyphFile != NULL)
2265 addsigntype(buf, typeNum, typeName, tooltip, glyphFile, fg, bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 else
2267 vim_free(typeName);
2268
2269 /* don't free typeName; it's used directly in addsigntype() */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002270 vim_free(fg);
2271 vim_free(bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 vim_free(tooltip);
2273 vim_free(glyphFile);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002274 if (parse_error)
2275 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276
2277#endif
2278/* =====================================================================*/
2279 }
2280 else if (streq((char *)cmd, "addAnno"))
2281 {
2282#ifdef FEAT_SIGNS
2283 int serNum;
2284 int localTypeNum;
2285 int typeNum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 pos_T *pos;
2287
2288 if (buf == NULL || buf->bufp == NULL)
2289 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002290 nbdebug((" invalid buffer identifier in addAnno\n"));
2291 EMSG("E651: invalid buffer identifier in addAnno");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 return FAIL;
2293 }
2294
2295 doupdate = 1;
2296
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002297 cp = (char *)args;
2298 serNum = strtol(cp, &cp, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299
2300 /* Get the typenr specific for this buffer and convert it to
2301 * the global typenumber, as used for the sign name. */
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002302 localTypeNum = strtol(cp, &cp, 10);
2303 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 typeNum = mapsigntype(buf, localTypeNum);
2305
2306 pos = get_off_or_lnum(buf->bufp, &args);
2307
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002308 cp = (char *)args;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002309 ignored = (int)strtol(cp, &cp, 10);
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002310 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311# ifdef NBDEBUG
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002312 if (ignored != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002314 nbdebug((" partial line annotation -- Not Yet Implemented!\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 }
2316# endif
2317 if (serNum >= GUARDEDOFFSET)
2318 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002319 nbdebug((" too many annotations! ignoring...\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 return FAIL;
2321 }
2322 if (pos)
2323 {
Bram Moolenaarec906222009-02-21 21:14:00 +00002324 coloncmd(":sign place %d line=%ld name=%d buffer=%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 serNum, pos->lnum, typeNum, buf->bufp->b_fnum);
2326 if (typeNum == curPCtype)
2327 coloncmd(":sign jump %d buffer=%d", serNum,
2328 buf->bufp->b_fnum);
2329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330#endif
2331/* =====================================================================*/
2332 }
2333 else if (streq((char *)cmd, "removeAnno"))
2334 {
2335#ifdef FEAT_SIGNS
2336 int serNum;
2337
2338 if (buf == NULL || buf->bufp == NULL)
2339 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002340 nbdebug((" invalid buffer identifier in removeAnno\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 return FAIL;
2342 }
2343 doupdate = 1;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002344 cp = (char *)args;
2345 serNum = strtol(cp, &cp, 10);
2346 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 coloncmd(":sign unplace %d buffer=%d",
2348 serNum, buf->bufp->b_fnum);
2349 redraw_buf_later(buf->bufp, NOT_VALID);
2350#endif
2351/* =====================================================================*/
2352 }
2353 else if (streq((char *)cmd, "moveAnnoToFront"))
2354 {
2355#ifdef FEAT_SIGNS
Bram Moolenaarf2330482008-06-24 20:19:36 +00002356 nbdebug((" moveAnnoToFront: Not Yet Implemented!\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357#endif
2358/* =====================================================================*/
2359 }
2360 else if (streq((char *)cmd, "guard") || streq((char *)cmd, "unguard"))
2361 {
2362 int len;
2363 pos_T first;
2364 pos_T last;
2365 pos_T *pos;
2366 int un = (cmd[0] == 'u');
2367 static int guardId = GUARDEDOFFSET;
2368
2369 if (skip >= SKIP_STOP)
2370 {
2371 nbdebug((" Skipping %s command\n", (char *) cmd));
2372 return OK;
2373 }
2374
2375 nb_init_graphics();
2376
2377 if (buf == NULL || buf->bufp == NULL)
2378 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002379 nbdebug((" invalid buffer identifier in %s command\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 return FAIL;
2381 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002382 nb_set_curbuf(buf->bufp);
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002383 cp = (char *)args;
2384 off = strtol(cp, &cp, 10);
2385 len = strtol(cp, NULL, 10);
2386 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 pos = off2pos(buf->bufp, off);
2388 doupdate = 1;
2389 if (!pos)
2390 nbdebug((" no such start pos in %s, %ld\n", cmd, off));
2391 else
2392 {
2393 first = *pos;
2394 pos = off2pos(buf->bufp, off + len - 1);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002395 if (pos != NULL && pos->col == 0)
2396 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 /*
2398 * In Java Swing the offset is a position between 2
2399 * characters. If col == 0 then we really want the
2400 * previous line as the end.
2401 */
2402 pos = off2pos(buf->bufp, off + len - 2);
2403 }
2404 if (!pos)
2405 nbdebug((" no such end pos in %s, %ld\n",
2406 cmd, off + len - 1));
2407 else
2408 {
2409 long lnum;
2410 last = *pos;
2411 /* set highlight for region */
2412 nbdebug((" %sGUARD %ld,%d to %ld,%d\n", (un) ? "UN" : "",
2413 first.lnum, first.col,
2414 last.lnum, last.col));
2415#ifdef FEAT_SIGNS
2416 for (lnum = first.lnum; lnum <= last.lnum; lnum++)
2417 {
2418 if (un)
2419 {
2420 /* never used */
2421 }
2422 else
2423 {
2424 if (buf_findsigntype_id(buf->bufp, lnum,
2425 GUARDED) == 0)
2426 {
2427 coloncmd(
Bram Moolenaarec906222009-02-21 21:14:00 +00002428 ":sign place %d line=%ld name=%d buffer=%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 guardId++, lnum, GUARDED,
2430 buf->bufp->b_fnum);
2431 }
2432 }
2433 }
2434#endif
2435 redraw_buf_later(buf->bufp, NOT_VALID);
2436 }
2437 }
2438/* =====================================================================*/
2439 }
2440 else if (streq((char *)cmd, "startAtomic"))
2441 {
2442 inAtomic = 1;
2443/* =====================================================================*/
2444 }
2445 else if (streq((char *)cmd, "endAtomic"))
2446 {
2447 inAtomic = 0;
2448 if (needupdate)
2449 {
2450 doupdate = 1;
2451 needupdate = 0;
2452 }
2453/* =====================================================================*/
2454 }
2455 else if (streq((char *)cmd, "save"))
2456 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002457 /*
2458 * NOTE - This command is obsolete wrt NetBeans. Its left in
2459 * only for historical reasons.
2460 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 if (buf == NULL || buf->bufp == NULL)
2462 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002463 nbdebug((" invalid buffer identifier in %s command\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 return FAIL;
2465 }
2466
2467 /* the following is taken from ex_cmds.c (do_wqall function) */
2468 if (bufIsChanged(buf->bufp))
2469 {
2470 /* Only write if the buffer can be written. */
2471 if (p_write
2472 && !buf->bufp->b_p_ro
2473 && buf->bufp->b_ffname != NULL
2474#ifdef FEAT_QUICKFIX
2475 && !bt_dontwrite(buf->bufp)
2476#endif
2477 )
2478 {
2479 buf_write_all(buf->bufp, FALSE);
2480#ifdef FEAT_AUTOCMD
2481 /* an autocommand may have deleted the buffer */
2482 if (!buf_valid(buf->bufp))
2483 buf->bufp = NULL;
2484#endif
2485 }
2486 }
Bram Moolenaarf2330482008-06-24 20:19:36 +00002487 else
2488 {
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002489 nbdebug((" Buffer has no changes!\n"));
Bram Moolenaarf2330482008-06-24 20:19:36 +00002490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491/* =====================================================================*/
2492 }
2493 else if (streq((char *)cmd, "netbeansBuffer"))
2494 {
2495 if (buf == NULL || buf->bufp == NULL)
2496 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002497 nbdebug((" invalid buffer identifier in %s command\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 return FAIL;
2499 }
2500 if (*args == 'T')
2501 {
2502 buf->bufp->b_netbeans_file = TRUE;
2503 buf->bufp->b_was_netbeans_file = TRUE;
2504 }
2505 else
2506 buf->bufp->b_netbeans_file = FALSE;
2507/* =====================================================================*/
2508 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002509 else if (streq((char *)cmd, "specialKeys"))
2510 {
2511 special_keys(args);
2512/* =====================================================================*/
2513 }
2514 else if (streq((char *)cmd, "actionMenuItem"))
2515 {
2516 /* not used yet */
2517/* =====================================================================*/
2518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 else if (streq((char *)cmd, "version"))
2520 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002521 /* not used yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 }
Bram Moolenaarf2330482008-06-24 20:19:36 +00002523 else
2524 {
2525 nbdebug(("Unrecognised command: %s\n", cmd));
2526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 /*
2528 * Unrecognized command is ignored.
2529 */
2530 }
2531 if (inAtomic && doupdate)
2532 {
2533 needupdate = 1;
2534 doupdate = 0;
2535 }
2536
Bram Moolenaar009b2592004-10-24 19:18:58 +00002537 /*
2538 * Is this needed? I moved the netbeans_Xt_connect() later during startup
2539 * and it may no longer be necessary. If its not needed then needupdate
2540 * and doupdate can also be removed.
2541 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 if (buf != NULL && buf->initDone && doupdate)
2543 {
2544 update_screen(NOT_VALID);
2545 setcursor();
2546 out_flush();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002547#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 gui_update_cursor(TRUE, FALSE);
2549 gui_mch_flush();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002550#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 /* Quit a hit-return or more prompt. */
2552 if (State == HITRETURN || State == ASKMORE)
2553 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554#ifdef FEAT_GUI_GTK
2555 if (gtk_main_level() > 0)
2556 gtk_main_quit();
2557#endif
2558 }
2559 }
2560
2561 return retval;
2562}
2563
2564
2565/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002566 * If "buf" is not the current buffer try changing to a window that edits this
2567 * buffer. If there is no such window then close the current buffer and set
2568 * the current buffer as "buf".
2569 */
2570 static void
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00002571nb_set_curbuf(buf_T *buf)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002572{
2573 if (curbuf != buf && buf_jump_open_win(buf) == NULL)
2574 set_curbuf(buf, DOBUF_GOTO);
2575}
2576
2577/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 * Process a vim colon command.
2579 */
2580 static void
2581coloncmd(char *cmd, ...)
2582{
2583 char buf[1024];
2584 va_list ap;
2585
2586 va_start(ap, cmd);
Bram Moolenaar0dc79e82009-06-24 14:50:12 +00002587 vim_vsnprintf(buf, sizeof(buf), cmd, ap, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 va_end(ap);
2589
2590 nbdebug((" COLONCMD %s\n", buf));
2591
2592/* ALT_INPUT_LOCK_ON; */
2593 do_cmdline((char_u *)buf, NULL, NULL, DOCMD_NOWAIT | DOCMD_KEYTYPED);
2594/* ALT_INPUT_LOCK_OFF; */
2595
2596 setcursor(); /* restore the cursor position */
2597 out_flush(); /* make sure output has been written */
2598
Bram Moolenaar67c53842010-05-22 18:28:27 +02002599#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 gui_update_cursor(TRUE, FALSE);
2601 gui_mch_flush();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002602#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603}
2604
2605
2606/*
Bram Moolenaar009b2592004-10-24 19:18:58 +00002607 * Parse the specialKeys argument and issue the appropriate map commands.
2608 */
2609 static void
2610special_keys(char_u *args)
2611{
2612 char *save_str = nb_unquote(args, NULL);
2613 char *tok = strtok(save_str, " ");
2614 char *sep;
2615 char keybuf[64];
2616 char cmdbuf[256];
2617
2618 while (tok != NULL)
2619 {
2620 int i = 0;
2621
2622 if ((sep = strchr(tok, '-')) != NULL)
2623 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002624 *sep = NUL;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002625 while (*tok)
2626 {
2627 switch (*tok)
2628 {
2629 case 'A':
2630 case 'M':
2631 case 'C':
2632 case 'S':
2633 keybuf[i++] = *tok;
2634 keybuf[i++] = '-';
2635 break;
2636 }
2637 tok++;
2638 }
2639 tok++;
2640 }
2641
2642 strcpy(&keybuf[i], tok);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002643 vim_snprintf(cmdbuf, sizeof(cmdbuf),
2644 "<silent><%s> :nbkey %s<CR>", keybuf, keybuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002645 do_map(0, (char_u *)cmdbuf, NORMAL, FALSE);
2646 tok = strtok(NULL, " ");
2647 }
2648 vim_free(save_str);
2649}
2650
2651
2652 void
2653ex_nbkey(eap)
2654 exarg_T *eap;
2655{
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002656 (void)netbeans_keystring(eap->arg);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002657}
2658
2659
2660/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 * Initialize highlights and signs for use by netbeans (mostly obsolete)
2662 */
2663 static void
2664nb_init_graphics(void)
2665{
2666 static int did_init = FALSE;
2667
2668 if (!did_init)
2669 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002670 coloncmd(":highlight NBGuarded guibg=Cyan guifg=Black"
2671 " ctermbg=LightCyan ctermfg=Black");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 coloncmd(":sign define %d linehl=NBGuarded", GUARDED);
2673
2674 did_init = TRUE;
2675 }
2676}
2677
2678/*
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002679 * Convert key to netbeans name. This uses the global "mod_mask".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 */
2681 static void
2682netbeans_keyname(int key, char *buf)
2683{
2684 char *name = 0;
2685 char namebuf[2];
2686 int ctrl = 0;
2687 int shift = 0;
2688 int alt = 0;
2689
2690 if (mod_mask & MOD_MASK_CTRL)
2691 ctrl = 1;
2692 if (mod_mask & MOD_MASK_SHIFT)
2693 shift = 1;
2694 if (mod_mask & MOD_MASK_ALT)
2695 alt = 1;
2696
2697
2698 switch (key)
2699 {
2700 case K_F1: name = "F1"; break;
2701 case K_S_F1: name = "F1"; shift = 1; break;
2702 case K_F2: name = "F2"; break;
2703 case K_S_F2: name = "F2"; shift = 1; break;
2704 case K_F3: name = "F3"; break;
2705 case K_S_F3: name = "F3"; shift = 1; break;
2706 case K_F4: name = "F4"; break;
2707 case K_S_F4: name = "F4"; shift = 1; break;
2708 case K_F5: name = "F5"; break;
2709 case K_S_F5: name = "F5"; shift = 1; break;
2710 case K_F6: name = "F6"; break;
2711 case K_S_F6: name = "F6"; shift = 1; break;
2712 case K_F7: name = "F7"; break;
2713 case K_S_F7: name = "F7"; shift = 1; break;
2714 case K_F8: name = "F8"; break;
2715 case K_S_F8: name = "F8"; shift = 1; break;
2716 case K_F9: name = "F9"; break;
2717 case K_S_F9: name = "F9"; shift = 1; break;
2718 case K_F10: name = "F10"; break;
2719 case K_S_F10: name = "F10"; shift = 1; break;
2720 case K_F11: name = "F11"; break;
2721 case K_S_F11: name = "F11"; shift = 1; break;
2722 case K_F12: name = "F12"; break;
2723 case K_S_F12: name = "F12"; shift = 1; break;
2724 default:
2725 if (key >= ' ' && key <= '~')
2726 {
2727 /* Allow ASCII characters. */
2728 name = namebuf;
2729 namebuf[0] = key;
2730 namebuf[1] = NUL;
2731 }
2732 else
2733 name = "X";
2734 break;
2735 }
2736
2737 buf[0] = '\0';
2738 if (ctrl)
2739 strcat(buf, "C");
2740 if (shift)
2741 strcat(buf, "S");
2742 if (alt)
2743 strcat(buf, "M"); /* META */
2744 if (ctrl || shift || alt)
2745 strcat(buf, "-");
2746 strcat(buf, name);
2747}
2748
Bram Moolenaar67c53842010-05-22 18:28:27 +02002749#if defined(FEAT_BEVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750/*
2751 * Function to be called for balloon evaluation. Grabs the text under the
2752 * cursor and sends it to the debugger for evaluation. The debugger should
2753 * respond with a showBalloon command when there is a useful result.
2754 */
Bram Moolenaard62bec82005-03-07 22:56:57 +00002755 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756netbeans_beval_cb(
2757 BalloonEval *beval,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002758 int state UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759{
Bram Moolenaard62bec82005-03-07 22:56:57 +00002760 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 char_u *text;
Bram Moolenaard62bec82005-03-07 22:56:57 +00002762 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 int col;
2764 char buf[MAXPATHL * 2 + 25];
2765 char_u *p;
2766
2767 /* Don't do anything when 'ballooneval' is off, messages scrolled the
2768 * windows up or we have no connection. */
2769 if (!p_beval || msg_scrolled > 0 || !haveConnection)
2770 return;
2771
Bram Moolenaard62bec82005-03-07 22:56:57 +00002772 if (get_beval_info(beval, TRUE, &wp, &lnum, &text, &col) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 {
2774 /* Send debugger request. Only when the text is of reasonable
2775 * length. */
2776 if (text != NULL && text[0] != NUL && STRLEN(text) < MAXPATHL)
2777 {
2778 p = nb_quote(text);
2779 if (p != NULL)
Bram Moolenaar009b2592004-10-24 19:18:58 +00002780 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002781 vim_snprintf(buf, sizeof(buf),
Bram Moolenaar89d40322006-08-29 15:30:07 +00002782 "0:balloonText=%d \"%s\"\n", r_cmdno, p);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002783 vim_free(p);
2784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 nbdebug(("EVT: %s", buf));
2786 nb_send(buf, "netbeans_beval_cb");
2787 }
2788 vim_free(text);
2789 }
2790}
2791#endif
2792
2793/*
Bram Moolenaar67c53842010-05-22 18:28:27 +02002794 * Return netbeans file descriptor.
2795 */
2796 int
2797netbeans_filedesc (void)
2798{
2799 return sd;
2800}
2801
2802#if defined(FEAT_GUI) || defined(PROTO)
2803/*
2804 * Register our file descriptor with the gui event handling system.
2805 */
2806 void
2807netbeans_gui_register(void)
2808{
2809 if (!NB_HAS_GUI)
2810 return;
2811
2812 if (sd > 0)
2813 {
2814# ifdef FEAT_GUI_MOTIF
2815 /* tell notifier we are interested in being called
2816 * when there is input on the editor connection socket
2817 */
2818 if (inputHandler == (XtInputId)NULL)
2819 inputHandler = XtAppAddInput((XtAppContext)app_context, sd,
2820 (XtPointer)(XtInputReadMask + XtInputExceptMask),
2821 messageFromNetbeans, NULL);
2822# else
2823# ifdef FEAT_GUI_GTK
2824 /*
2825 * Tell gdk we are interested in being called when there
2826 * is input on the editor connection socket
2827 */
2828 if (inputHandler == 0)
2829 inputHandler = gdk_input_add((gint)sd, (GdkInputCondition)
2830 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
2831 messageFromNetbeans, NULL);
2832# else
2833# ifdef FEAT_GUI_W32
2834 /*
2835 * Tell Windows we are interested in receiving message when there
2836 * is input on the editor connection socket
2837 */
2838 if (inputHandler == -1)
2839 inputHandler = WSAAsyncSelect(sd, s_hwnd, WM_NETBEANS, FD_READ);
2840# endif
2841# endif
2842# endif
2843 }
2844
2845# ifdef FEAT_BEVAL
2846 bevalServers |= BEVAL_NETBEANS;
2847# endif
2848}
2849#endif
2850
2851/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 * Tell netbeans that the window was opened, ready for commands.
2853 */
2854 void
2855netbeans_startup_done(void)
2856{
2857 char *cmd = "0:startupDone=0\n";
2858
Bram Moolenaar67c53842010-05-22 18:28:27 +02002859 if (!usingNetbeans)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 return;
2861
Bram Moolenaar67c53842010-05-22 18:28:27 +02002862 netbeans_connect();
2863 if (!haveConnection)
2864 return;
2865#ifdef FEAT_GUI
2866 netbeans_gui_register();
Bram Moolenaard62bec82005-03-07 22:56:57 +00002867#endif
2868
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 nbdebug(("EVT: %s", cmd));
2870 nb_send(cmd, "netbeans_startup_done");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871}
2872
Bram Moolenaar009b2592004-10-24 19:18:58 +00002873/*
2874 * Tell netbeans that we're exiting. This should be called right
2875 * before calling exit.
2876 */
2877 void
2878netbeans_send_disconnect()
2879{
2880 char buf[128];
2881
2882 if (haveConnection)
2883 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002884 sprintf(buf, "0:disconnect=%d\n", r_cmdno);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002885 nbdebug(("EVT: %s", buf));
2886 nb_send(buf, "netbeans_disconnect");
2887 }
2888}
2889
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_W32) || defined(PROTO)
2891/*
2892 * Tell netbeans that the window was moved or resized.
2893 */
2894 void
2895netbeans_frame_moved(int new_x, int new_y)
2896{
2897 char buf[128];
2898
2899 if (!haveConnection)
2900 return;
2901
2902 sprintf(buf, "0:geometry=%d %d %d %d %d\n",
Bram Moolenaar89d40322006-08-29 15:30:07 +00002903 r_cmdno, (int)Columns, (int)Rows, new_x, new_y);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002904 /*nbdebug(("EVT: %s", buf)); happens too many times during a move */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 nb_send(buf, "netbeans_frame_moved");
2906}
2907#endif
2908
2909/*
Bram Moolenaar009b2592004-10-24 19:18:58 +00002910 * Tell netbeans the user opened or activated a file.
2911 */
2912 void
2913netbeans_file_activated(buf_T *bufp)
2914{
2915 int bufno = nb_getbufno(bufp);
2916 nbbuf_T *bp = nb_get_buf(bufno);
2917 char buffer[2*MAXPATHL];
2918 char_u *q;
2919
2920 if (!haveConnection || dosetvisible)
2921 return;
2922
2923 q = nb_quote(bufp->b_ffname);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002924 if (q == NULL || bp == NULL)
Bram Moolenaar009b2592004-10-24 19:18:58 +00002925 return;
2926
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002927 vim_snprintf(buffer, sizeof(buffer), "%d:fileOpened=%d \"%s\" %s %s\n",
Bram Moolenaar009b2592004-10-24 19:18:58 +00002928 bufno,
2929 bufno,
2930 (char *)q,
2931 "T", /* open in NetBeans */
2932 "F"); /* modified */
2933
2934 vim_free(q);
2935 nbdebug(("EVT: %s", buffer));
2936
2937 nb_send(buffer, "netbeans_file_opened");
2938}
2939
2940/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 * Tell netbeans the user opened a file.
2942 */
2943 void
Bram Moolenaar009b2592004-10-24 19:18:58 +00002944netbeans_file_opened(buf_T *bufp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945{
Bram Moolenaar009b2592004-10-24 19:18:58 +00002946 int bufno = nb_getbufno(bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 char buffer[2*MAXPATHL];
2948 char_u *q;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002949 nbbuf_T *bp = nb_get_buf(nb_getbufno(bufp));
2950 int bnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951
2952 if (!haveConnection)
2953 return;
2954
Bram Moolenaar009b2592004-10-24 19:18:58 +00002955 q = nb_quote(bufp->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 if (q == NULL)
2957 return;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002958 if (bp != NULL)
2959 bnum = bufno;
2960 else
2961 bnum = 0;
2962
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002963 vim_snprintf(buffer, sizeof(buffer), "%d:fileOpened=%d \"%s\" %s %s\n",
Bram Moolenaar009b2592004-10-24 19:18:58 +00002964 bnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 0,
2966 (char *)q,
2967 "T", /* open in NetBeans */
2968 "F"); /* modified */
2969
2970 vim_free(q);
2971 nbdebug(("EVT: %s", buffer));
2972
2973 nb_send(buffer, "netbeans_file_opened");
Bram Moolenaar009b2592004-10-24 19:18:58 +00002974 if (p_acd && vim_chdirfile(bufp->b_ffname) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 shorten_fnames(TRUE);
2976}
2977
2978/*
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00002979 * Tell netbeans that a file was deleted or wiped out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 */
2981 void
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00002982netbeans_file_killed(buf_T *bufp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983{
2984 int bufno = nb_getbufno(bufp);
2985 nbbuf_T *nbbuf = nb_get_buf(bufno);
2986 char buffer[2*MAXPATHL];
2987
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00002988 if (!haveConnection || bufno == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 return;
2990
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00002991 nbdebug(("netbeans_file_killed:\n"));
2992 nbdebug((" Killing bufno: %d", bufno));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993
Bram Moolenaar89d40322006-08-29 15:30:07 +00002994 sprintf(buffer, "%d:killed=%d\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995
2996 nbdebug(("EVT: %s", buffer));
2997
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00002998 nb_send(buffer, "netbeans_file_killed");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999
3000 if (nbbuf != NULL)
3001 nbbuf->bufp = NULL;
3002}
3003
3004/*
3005 * Get a pointer to the Netbeans buffer for Vim buffer "bufp".
3006 * Return NULL if there is no such buffer or changes are not to be reported.
3007 * Otherwise store the buffer number in "*bufnop".
3008 */
3009 static nbbuf_T *
3010nb_bufp2nbbuf_fire(buf_T *bufp, int *bufnop)
3011{
3012 int bufno;
3013 nbbuf_T *nbbuf;
3014
3015 if (!haveConnection || !netbeansFireChanges)
3016 return NULL; /* changes are not reported at all */
3017
3018 bufno = nb_getbufno(bufp);
3019 if (bufno <= 0)
3020 return NULL; /* file is not known to NetBeans */
3021
3022 nbbuf = nb_get_buf(bufno);
3023 if (nbbuf != NULL && !nbbuf->fireChanges)
3024 return NULL; /* changes in this buffer are not reported */
3025
3026 *bufnop = bufno;
3027 return nbbuf;
3028}
3029
3030/*
3031 * Tell netbeans the user inserted some text.
3032 */
3033 void
3034netbeans_inserted(
3035 buf_T *bufp,
3036 linenr_T linenr,
3037 colnr_T col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 char_u *txt,
3039 int newlen)
3040{
3041 char_u *buf;
3042 int bufno;
3043 nbbuf_T *nbbuf;
3044 pos_T pos;
3045 long off;
3046 char_u *p;
3047 char_u *newtxt;
3048
3049 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3050 if (nbbuf == NULL)
3051 return;
3052
Bram Moolenaar009b2592004-10-24 19:18:58 +00003053 /* Don't mark as modified for initial read */
3054 if (nbbuf->insertDone)
3055 nbbuf->modified = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056
3057 pos.lnum = linenr;
3058 pos.col = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 off = pos2off(bufp, &pos);
3060
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 /* send the "insert" EVT */
3062 newtxt = alloc(newlen + 1);
Bram Moolenaarb6356332005-07-18 21:40:44 +00003063 vim_strncpy(newtxt, txt, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 p = nb_quote(newtxt);
3065 if (p != NULL)
3066 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00003067 buf = alloc(128 + 2*newlen);
Bram Moolenaar89d40322006-08-29 15:30:07 +00003068 sprintf((char *)buf, "%d:insert=%d %ld \"%s\"\n",
3069 bufno, r_cmdno, off, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 nbdebug(("EVT: %s", buf));
3071 nb_send((char *)buf, "netbeans_inserted");
Bram Moolenaar009b2592004-10-24 19:18:58 +00003072 vim_free(p);
3073 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 vim_free(newtxt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076}
3077
3078/*
3079 * Tell netbeans some bytes have been removed.
3080 */
3081 void
3082netbeans_removed(
3083 buf_T *bufp,
3084 linenr_T linenr,
3085 colnr_T col,
3086 long len)
3087{
3088 char_u buf[128];
3089 int bufno;
3090 nbbuf_T *nbbuf;
3091 pos_T pos;
3092 long off;
3093
3094 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3095 if (nbbuf == NULL)
3096 return;
3097
3098 if (len < 0)
3099 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00003100 nbdebug(("Negative len %ld in netbeans_removed()!\n", len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 return;
3102 }
3103
3104 nbbuf->modified = 1;
3105
3106 pos.lnum = linenr;
3107 pos.col = col;
3108
3109 off = pos2off(bufp, &pos);
3110
Bram Moolenaar89d40322006-08-29 15:30:07 +00003111 sprintf((char *)buf, "%d:remove=%d %ld %ld\n", bufno, r_cmdno, off, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 nbdebug(("EVT: %s", buf));
3113 nb_send((char *)buf, "netbeans_removed");
3114}
3115
3116/*
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003117 * Send netbeans an unmodified command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003120netbeans_unmodified(buf_T *bufp UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121{
3122#if 0
3123 char_u buf[128];
3124 int bufno;
3125 nbbuf_T *nbbuf;
3126
3127 /* This has been disabled, because NetBeans considers a buffer modified
3128 * even when all changes have been undone. */
3129 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3130 if (nbbuf == NULL)
3131 return;
3132
3133 nbbuf->modified = 0;
3134
Bram Moolenaar89d40322006-08-29 15:30:07 +00003135 sprintf((char *)buf, "%d:unmodified=%d\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 nbdebug(("EVT: %s", buf));
3137 nb_send((char *)buf, "netbeans_unmodified");
3138#endif
3139}
3140
3141/*
3142 * Send a button release event back to netbeans. Its up to netbeans
3143 * to decide what to do (if anything) with this event.
3144 */
3145 void
3146netbeans_button_release(int button)
3147{
3148 char buf[128];
3149 int bufno;
3150
3151 bufno = nb_getbufno(curbuf);
3152
3153 if (bufno >= 0 && curwin != NULL && curwin->w_buffer == curbuf)
3154 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003155 int col = mouse_col - W_WINCOL(curwin)
Bram Moolenaar67c53842010-05-22 18:28:27 +02003156 - ((curwin->w_p_nu || curwin->w_p_rnu) ? 9 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 long off = pos2off(curbuf, &curwin->w_cursor);
3158
3159 /* sync the cursor position */
Bram Moolenaar89d40322006-08-29 15:30:07 +00003160 sprintf(buf, "%d:newDotAndMark=%d %ld %ld\n", bufno, r_cmdno, off, off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 nbdebug(("EVT: %s", buf));
3162 nb_send(buf, "netbeans_button_release[newDotAndMark]");
3163
Bram Moolenaar89d40322006-08-29 15:30:07 +00003164 sprintf(buf, "%d:buttonRelease=%d %d %ld %d\n", bufno, r_cmdno,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 button, (long)curwin->w_cursor.lnum, col);
3166 nbdebug(("EVT: %s", buf));
3167 nb_send(buf, "netbeans_button_release");
3168 }
3169}
3170
3171
3172/*
Bram Moolenaar143c38c2007-05-10 16:41:10 +00003173 * Send a keypress event back to netbeans. This usually simulates some
Bram Moolenaar009b2592004-10-24 19:18:58 +00003174 * kind of function key press. This function operates on a key code.
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003175 * Return TRUE when the key was sent, FALSE when the command has been
3176 * postponed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 */
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003178 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179netbeans_keycommand(int key)
3180{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 char keyName[60];
Bram Moolenaar009b2592004-10-24 19:18:58 +00003182
3183 netbeans_keyname(key, keyName);
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003184 return netbeans_keystring((char_u *)keyName);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003185}
3186
3187
3188/*
Bram Moolenaar143c38c2007-05-10 16:41:10 +00003189 * Send a keypress event back to netbeans. This usually simulates some
Bram Moolenaar009b2592004-10-24 19:18:58 +00003190 * kind of function key press. This function operates on a key string.
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003191 * Return TRUE when the key was sent, FALSE when the command has been
3192 * postponed.
Bram Moolenaar009b2592004-10-24 19:18:58 +00003193 */
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003194 static int
3195netbeans_keystring(char_u *keyName)
Bram Moolenaar009b2592004-10-24 19:18:58 +00003196{
3197 char buf[2*MAXPATHL];
3198 int bufno = nb_getbufno(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 long off;
3200 char_u *q;
3201
3202 if (!haveConnection)
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003203 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205
3206 if (bufno == -1)
3207 {
3208 nbdebug(("got keycommand for non-NetBeans buffer, opening...\n"));
3209 q = curbuf->b_ffname == NULL ? (char_u *)""
3210 : nb_quote(curbuf->b_ffname);
3211 if (q == NULL)
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003212 return TRUE;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003213 vim_snprintf(buf, sizeof(buf), "0:fileOpened=%d \"%s\" %s %s\n", 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 q,
3215 "T", /* open in NetBeans */
3216 "F"); /* modified */
3217 if (curbuf->b_ffname != NULL)
3218 vim_free(q);
3219 nbdebug(("EVT: %s", buf));
3220 nb_send(buf, "netbeans_keycommand");
3221
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003222 postpone_keycommand(keyName);
3223 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 }
3225
3226 /* sync the cursor position */
3227 off = pos2off(curbuf, &curwin->w_cursor);
Bram Moolenaar89d40322006-08-29 15:30:07 +00003228 sprintf(buf, "%d:newDotAndMark=%d %ld %ld\n", bufno, r_cmdno, off, off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 nbdebug(("EVT: %s", buf));
3230 nb_send(buf, "netbeans_keycommand");
3231
3232 /* To work on Win32 you must apply patch to ExtEditor module
3233 * from ExtEdCaret.java.diff - make EVT_newDotAndMark handler
3234 * more synchronous
3235 */
3236
3237 /* now send keyCommand event */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003238 vim_snprintf(buf, sizeof(buf), "%d:keyCommand=%d \"%s\"\n",
Bram Moolenaar89d40322006-08-29 15:30:07 +00003239 bufno, r_cmdno, keyName);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 nbdebug(("EVT: %s", buf));
3241 nb_send(buf, "netbeans_keycommand");
3242
3243 /* New: do both at once and include the lnum/col. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003244 vim_snprintf(buf, sizeof(buf), "%d:keyAtPos=%d \"%s\" %ld %ld/%ld\n",
Bram Moolenaar89d40322006-08-29 15:30:07 +00003245 bufno, r_cmdno, keyName,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 off, (long)curwin->w_cursor.lnum, (long)curwin->w_cursor.col);
3247 nbdebug(("EVT: %s", buf));
3248 nb_send(buf, "netbeans_keycommand");
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003249 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250}
3251
3252
3253/*
3254 * Send a save event to netbeans.
3255 */
3256 void
3257netbeans_save_buffer(buf_T *bufp)
3258{
3259 char_u buf[64];
3260 int bufno;
3261 nbbuf_T *nbbuf;
3262
3263 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3264 if (nbbuf == NULL)
3265 return;
3266
3267 nbbuf->modified = 0;
3268
Bram Moolenaar89d40322006-08-29 15:30:07 +00003269 sprintf((char *)buf, "%d:save=%d\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 nbdebug(("EVT: %s", buf));
3271 nb_send((char *)buf, "netbeans_save_buffer");
3272}
3273
3274
3275/*
3276 * Send remove command to netbeans (this command has been turned off).
3277 */
3278 void
3279netbeans_deleted_all_lines(buf_T *bufp)
3280{
3281 char_u buf[64];
3282 int bufno;
3283 nbbuf_T *nbbuf;
3284
3285 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3286 if (nbbuf == NULL)
3287 return;
3288
Bram Moolenaar009b2592004-10-24 19:18:58 +00003289 /* Don't mark as modified for initial read */
3290 if (nbbuf->insertDone)
3291 nbbuf->modified = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292
Bram Moolenaar89d40322006-08-29 15:30:07 +00003293 sprintf((char *)buf, "%d:remove=%d 0 -1\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 nbdebug(("EVT(suppressed): %s", buf));
3295/* nb_send(buf, "netbeans_deleted_all_lines"); */
3296}
3297
3298
3299/*
3300 * See if the lines are guarded. The top and bot parameters are from
3301 * u_savecommon(), these are the line above the change and the line below the
3302 * change.
3303 */
3304 int
3305netbeans_is_guarded(linenr_T top, linenr_T bot)
3306{
3307 signlist_T *p;
3308 int lnum;
3309
3310 for (p = curbuf->b_signlist; p != NULL; p = p->next)
3311 if (p->id >= GUARDEDOFFSET)
3312 for (lnum = top + 1; lnum < bot; lnum++)
3313 if (lnum == p->lnum)
3314 return TRUE;
3315
3316 return FALSE;
3317}
3318
3319#if defined(FEAT_GUI_MOTIF) || defined(PROTO)
3320/*
3321 * We have multiple signs to draw at the same location. Draw the
3322 * multi-sign indicator instead. This is the Motif version.
3323 */
3324 void
3325netbeans_draw_multisign_indicator(int row)
3326{
3327 int i;
3328 int y;
3329 int x;
3330
3331 x = 0;
3332 y = row * gui.char_height + 2;
3333
3334 for (i = 0; i < gui.char_height - 3; i++)
3335 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y++);
3336
3337 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+0, y);
3338 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3339 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+4, y++);
3340 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+1, y);
3341 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3342 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+3, y++);
3343 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3344}
3345#endif /* FEAT_GUI_MOTIF */
3346
3347#ifdef FEAT_GUI_GTK
3348/*
3349 * We have multiple signs to draw at the same location. Draw the
3350 * multi-sign indicator instead. This is the GTK/Gnome version.
3351 */
3352 void
3353netbeans_draw_multisign_indicator(int row)
3354{
3355 int i;
3356 int y;
3357 int x;
3358 GdkDrawable *drawable = gui.drawarea->window;
3359
3360 x = 0;
3361 y = row * gui.char_height + 2;
3362
3363 for (i = 0; i < gui.char_height - 3; i++)
3364 gdk_draw_point(drawable, gui.text_gc, x+2, y++);
3365
3366 gdk_draw_point(drawable, gui.text_gc, x+0, y);
3367 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3368 gdk_draw_point(drawable, gui.text_gc, x+4, y++);
3369 gdk_draw_point(drawable, gui.text_gc, x+1, y);
3370 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3371 gdk_draw_point(drawable, gui.text_gc, x+3, y++);
3372 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3373}
3374#endif /* FEAT_GUI_GTK */
3375
3376/*
3377 * If the mouse is clicked in the gutter of a line with multiple
3378 * annotations, cycle through the set of signs.
3379 */
3380 void
3381netbeans_gutter_click(linenr_T lnum)
3382{
3383 signlist_T *p;
3384
3385 for (p = curbuf->b_signlist; p != NULL; p = p->next)
3386 {
3387 if (p->lnum == lnum && p->next && p->next->lnum == lnum)
3388 {
3389 signlist_T *tail;
3390
3391 /* remove "p" from list, reinsert it at the tail of the sublist */
3392 if (p->prev)
3393 p->prev->next = p->next;
3394 else
3395 curbuf->b_signlist = p->next;
3396 p->next->prev = p->prev;
3397 /* now find end of sublist and insert p */
3398 for (tail = p->next;
3399 tail->next && tail->next->lnum == lnum
3400 && tail->next->id < GUARDEDOFFSET;
3401 tail = tail->next)
3402 ;
3403 /* tail now points to last entry with same lnum (except
3404 * that "guarded" annotations are always last) */
3405 p->next = tail->next;
3406 if (tail->next)
3407 tail->next->prev = p;
3408 p->prev = tail;
3409 tail->next = p;
3410 update_debug_sign(curbuf, lnum);
3411 break;
3412 }
3413 }
3414}
3415
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416/*
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003417 * Add a sign of the requested type at the requested location.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 *
3419 * Reverse engineering:
3420 * Apparently an annotation is defined the first time it is used in a buffer.
3421 * When the same annotation is used in two buffers, the second time we do not
3422 * need to define a new sign name but reuse the existing one. But since the
3423 * ID number used in the second buffer starts counting at one again, a mapping
3424 * is made from the ID specifically for the buffer to the global sign name
3425 * (which is a number).
3426 *
3427 * globalsignmap[] stores the signs that have been defined globally.
3428 * buf->signmapused[] maps buffer-local annotation IDs to an index in
3429 * globalsignmap[].
3430 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 static void
3432addsigntype(
3433 nbbuf_T *buf,
3434 int typeNum,
3435 char_u *typeName,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003436 char_u *tooltip UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 char_u *glyphFile,
Bram Moolenaar67c53842010-05-22 18:28:27 +02003438 char_u *fg,
3439 char_u *bg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 int i, j;
Bram Moolenaar67c53842010-05-22 18:28:27 +02003442 int use_fg = (*fg && STRCMP(fg, "none") != 0);
3443 int use_bg = (*bg && STRCMP(bg, "none") != 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444
3445 for (i = 0; i < globalsignmapused; i++)
3446 if (STRCMP(typeName, globalsignmap[i]) == 0)
3447 break;
3448
3449 if (i == globalsignmapused) /* not found; add it to global map */
3450 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003451 nbdebug(("DEFINEANNOTYPE(%d,%s,%s,%s,%s,%s)\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 typeNum, typeName, tooltip, glyphFile, fg, bg));
3453 if (use_fg || use_bg)
3454 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003455 char fgbuf[2 * (8 + MAX_COLOR_LENGTH) + 1];
3456 char bgbuf[2 * (8 + MAX_COLOR_LENGTH) + 1];
3457 char *ptr;
3458 int value;
3459
3460 value = strtol((char *)fg, &ptr, 10);
3461 if (ptr != (char *)fg)
3462 sprintf(fgbuf, "guifg=#%06x", value & 0xFFFFFF);
3463 else
3464 sprintf(fgbuf, "guifg=%s ctermfg=%s", fg, fg);
3465
3466 value = strtol((char *)bg, &ptr, 10);
3467 if (ptr != (char *)bg)
3468 sprintf(bgbuf, "guibg=#%06x", value & 0xFFFFFF);
3469 else
3470 sprintf(bgbuf, "guibg=%s ctermbg=%s", bg, bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471
3472 coloncmd(":highlight NB_%s %s %s", typeName, (use_fg) ? fgbuf : "",
3473 (use_bg) ? bgbuf : "");
3474 if (*glyphFile == NUL)
3475 /* no glyph, line highlighting only */
3476 coloncmd(":sign define %d linehl=NB_%s", i + 1, typeName);
3477 else if (vim_strsize(glyphFile) <= 2)
3478 /* one- or two-character glyph name, use as text glyph with
3479 * texthl */
3480 coloncmd(":sign define %d text=%s texthl=NB_%s", i + 1,
3481 glyphFile, typeName);
3482 else
3483 /* glyph, line highlighting */
3484 coloncmd(":sign define %d icon=%s linehl=NB_%s", i + 1,
3485 glyphFile, typeName);
3486 }
3487 else
3488 /* glyph, no line highlighting */
3489 coloncmd(":sign define %d icon=%s", i + 1, glyphFile);
3490
3491 if (STRCMP(typeName,"CurrentPC") == 0)
3492 curPCtype = typeNum;
3493
3494 if (globalsignmapused == globalsignmaplen)
3495 {
3496 if (globalsignmaplen == 0) /* first allocation */
3497 {
3498 globalsignmaplen = 20;
3499 globalsignmap = (char **)alloc_clear(globalsignmaplen*sizeof(char *));
3500 }
3501 else /* grow it */
3502 {
3503 int incr;
3504 int oldlen = globalsignmaplen;
3505
3506 globalsignmaplen *= 2;
3507 incr = globalsignmaplen - oldlen;
3508 globalsignmap = (char **)vim_realloc(globalsignmap,
3509 globalsignmaplen * sizeof(char *));
3510 memset(globalsignmap + oldlen, 0, incr * sizeof(char *));
3511 }
3512 }
3513
3514 globalsignmap[i] = (char *)typeName;
3515 globalsignmapused = i + 1;
3516 }
3517
3518 /* check local map; should *not* be found! */
3519 for (j = 0; j < buf->signmapused; j++)
3520 if (buf->signmap[j] == i + 1)
3521 return;
3522
3523 /* add to local map */
3524 if (buf->signmapused == buf->signmaplen)
3525 {
3526 if (buf->signmaplen == 0) /* first allocation */
3527 {
3528 buf->signmaplen = 5;
3529 buf->signmap = (int *)alloc_clear(buf->signmaplen * sizeof(int *));
3530 }
3531 else /* grow it */
3532 {
3533 int incr;
3534 int oldlen = buf->signmaplen;
3535 buf->signmaplen *= 2;
3536 incr = buf->signmaplen - oldlen;
3537 buf->signmap = (int *)vim_realloc(buf->signmap,
3538 buf->signmaplen*sizeof(int *));
3539 memset(buf->signmap + oldlen, 0, incr * sizeof(int *));
3540 }
3541 }
3542
3543 buf->signmap[buf->signmapused++] = i + 1;
3544
3545}
3546
3547
3548/*
3549 * See if we have the requested sign type in the buffer.
3550 */
3551 static int
3552mapsigntype(nbbuf_T *buf, int localsigntype)
3553{
3554 if (--localsigntype >= 0 && localsigntype < buf->signmapused)
3555 return buf->signmap[localsigntype];
3556
3557 return 0;
3558}
3559
3560
3561/*
3562 * Compute length of buffer, don't print anything.
3563 */
3564 static long
3565get_buf_size(buf_T *bufp)
3566{
3567 linenr_T lnum;
3568 long char_count = 0;
3569 int eol_size;
3570 long last_check = 100000L;
3571
3572 if (bufp->b_ml.ml_flags & ML_EMPTY)
3573 return 0;
3574 else
3575 {
3576 if (get_fileformat(bufp) == EOL_DOS)
3577 eol_size = 2;
3578 else
3579 eol_size = 1;
3580 for (lnum = 1; lnum <= bufp->b_ml.ml_line_count; ++lnum)
3581 {
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00003582 char_count += (long)STRLEN(ml_get_buf(bufp, lnum, FALSE))
3583 + eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 /* Check for a CTRL-C every 100000 characters */
3585 if (char_count > last_check)
3586 {
3587 ui_breakcheck();
3588 if (got_int)
3589 return char_count;
3590 last_check = char_count + 100000L;
3591 }
3592 }
3593 /* Correction for when last line doesn't have an EOL. */
3594 if (!bufp->b_p_eol && bufp->b_p_bin)
3595 char_count -= eol_size;
3596 }
3597
3598 return char_count;
3599}
3600
3601/*
3602 * Convert character offset to lnum,col
3603 */
3604 static pos_T *
3605off2pos(buf_T *buf, long offset)
3606{
3607 linenr_T lnum;
3608 static pos_T pos;
3609
3610 pos.lnum = 0;
3611 pos.col = 0;
3612#ifdef FEAT_VIRTUALEDIT
3613 pos.coladd = 0;
3614#endif
3615
3616 if (!(buf->b_ml.ml_flags & ML_EMPTY))
3617 {
3618 if ((lnum = ml_find_line_or_offset(buf, (linenr_T)0, &offset)) < 0)
3619 return NULL;
3620 pos.lnum = lnum;
3621 pos.col = offset;
3622 }
3623
3624 return &pos;
3625}
3626
3627/*
3628 * Convert an argument in the form "1234" to an offset and compute the
3629 * lnum/col from it. Convert an argument in the form "123/12" directly to a
3630 * lnum/col.
3631 * "argp" is advanced to after the argument.
3632 * Return a pointer to the position, NULL if something is wrong.
3633 */
3634 static pos_T *
3635get_off_or_lnum(buf_T *buf, char_u **argp)
3636{
3637 static pos_T mypos;
3638 long off;
3639
3640 off = strtol((char *)*argp, (char **)argp, 10);
3641 if (**argp == '/')
3642 {
3643 mypos.lnum = (linenr_T)off;
3644 ++*argp;
3645 mypos.col = strtol((char *)*argp, (char **)argp, 10);
3646#ifdef FEAT_VIRTUALEDIT
3647 mypos.coladd = 0;
3648#endif
3649 return &mypos;
3650 }
3651 return off2pos(buf, off);
3652}
3653
3654
3655/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003656 * Convert (lnum,col) to byte offset in the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 */
3658 static long
3659pos2off(buf_T *buf, pos_T *pos)
3660{
3661 long offset = 0;
3662
3663 if (!(buf->b_ml.ml_flags & ML_EMPTY))
3664 {
3665 if ((offset = ml_find_line_or_offset(buf, pos->lnum, 0)) < 0)
3666 return 0;
3667 offset += pos->col;
3668 }
3669
3670 return offset;
3671}
3672
3673
Bram Moolenaar009b2592004-10-24 19:18:58 +00003674/*
3675 * This message is printed after NetBeans opens a new file. Its
3676 * similar to the message readfile() uses, but since NetBeans
3677 * doesn't normally call readfile, we do our own.
3678 */
3679 static void
3680print_read_msg(buf)
3681 nbbuf_T *buf;
3682{
3683 int lnum = buf->bufp->b_ml.ml_line_count;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003684 long nchars = (long)buf->bufp->b_orig_size;
Bram Moolenaar009b2592004-10-24 19:18:58 +00003685 char_u c;
3686
3687 msg_add_fname(buf->bufp, buf->bufp->b_ffname);
3688 c = FALSE;
3689
3690 if (buf->bufp->b_p_ro)
3691 {
3692 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]"));
3693 c = TRUE;
3694 }
3695 if (!buf->bufp->b_start_eol)
3696 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003697 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]")
3698 : _("[Incomplete last line]"));
Bram Moolenaar009b2592004-10-24 19:18:58 +00003699 c = TRUE;
3700 }
3701 msg_add_lines(c, (long)lnum, nchars);
3702
3703 /* Now display it */
3704 vim_free(keep_msg);
3705 keep_msg = NULL;
3706 msg_scrolled_ign = TRUE;
3707 msg_trunc_attr(IObuff, FALSE, 0);
3708 msg_scrolled_ign = FALSE;
3709}
3710
3711
3712/*
Bram Moolenaar67c53842010-05-22 18:28:27 +02003713 * Print a message after NetBeans writes the file. This message should be
3714 * identical to the standard message a non-netbeans user would see when
3715 * writing a file.
Bram Moolenaar009b2592004-10-24 19:18:58 +00003716 */
3717 static void
3718print_save_msg(buf, nchars)
3719 nbbuf_T *buf;
3720 long nchars;
3721{
3722 char_u c;
3723 char_u *p;
3724
3725 if (nchars >= 0)
3726 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003727 /* put fname in IObuff with quotes */
3728 msg_add_fname(buf->bufp, buf->bufp->b_ffname);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003729 c = FALSE;
3730
3731 msg_add_lines(c, buf->bufp->b_ml.ml_line_count,
3732 (long)buf->bufp->b_orig_size);
3733
3734 vim_free(keep_msg);
3735 keep_msg = NULL;
3736 msg_scrolled_ign = TRUE;
3737 p = msg_trunc_attr(IObuff, FALSE, 0);
3738 if ((msg_scrolled && !need_wait_return) || !buf->initDone)
3739 {
3740 /* Need to repeat the message after redrawing when:
3741 * - When reading from stdin (the screen will be cleared next).
3742 * - When restart_edit is set (otherwise there will be a delay
3743 * before redrawing).
3744 * - When the screen was scrolled but there is no wait-return
3745 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003746 set_keep_msg(p, 0);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003747 }
3748 msg_scrolled_ign = FALSE;
3749 /* add_to_input_buf((char_u *)"\f", 1); */
3750 }
3751 else
3752 {
3753 char_u ebuf[BUFSIZ];
3754
3755 STRCPY(ebuf, (char_u *)_("E505: "));
3756 STRCAT(ebuf, IObuff);
3757 STRCAT(ebuf, (char_u *)_("is read-only (add ! to override)"));
3758 STRCPY(IObuff, ebuf);
Bram Moolenaarf2330482008-06-24 20:19:36 +00003759 nbdebug((" %s\n", ebuf ));
Bram Moolenaar009b2592004-10-24 19:18:58 +00003760 emsg(IObuff);
3761 }
3762}
3763
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764#endif /* defined(FEAT_NETBEANS_INTG) */