blob: e3e32008e94fc9e616e3571f9a8254c2bb85e6a8 [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
19#include "vim.h"
20
21#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
22
23/* Note: when making changes here also adjust configure.in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024# include <fcntl.h>
25#ifdef WIN32
26# ifdef DEBUG
27# include <tchar.h> /* for _T definition for TRACEn macros */
28# endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +000029# include "vimio.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +000030/* WinSock API is separated from C API, thus we can't use read(), write(),
31 * errno... */
32# define sock_errno WSAGetLastError()
33# define ECONNREFUSED WSAECONNREFUSED
34# ifdef EINTR
35# undef EINTR
36# endif
37# define EINTR WSAEINTR
38# define sock_write(sd, buf, len) send(sd, buf, len, 0)
39# define sock_read(sd, buf, len) recv(sd, buf, len, 0)
40# define sock_close(sd) closesocket(sd)
41# define sleep(t) Sleep(t*1000) /* WinAPI Sleep() accepts milliseconds */
42#else
43# include <netdb.h>
44# include <netinet/in.h>
45# include <sys/socket.h>
46# ifdef HAVE_LIBGEN_H
47# include <libgen.h>
48# endif
49# define sock_errno errno
50# define sock_write(sd, buf, len) write(sd, buf, len)
51# define sock_read(sd, buf, len) read(sd, buf, len)
52# define sock_close(sd) close(sd)
53#endif
54
55#include "version.h"
56
57#define INET_SOCKETS
58
59#define GUARDED 10000 /* typenr for "guarded" annotation */
60#define GUARDEDOFFSET 1000000 /* base for "guarded" sign id's */
61
62/* The first implementation (working only with Netbeans) returned "1.1". The
63 * protocol implemented here also supports A-A-P. */
Bram Moolenaarc65c4912006-11-14 17:29:46 +000064static char *ExtEdProtocolVersion = "2.4";
Bram Moolenaar071d4272004-06-13 20:20:40 +000065
66static long pos2off __ARGS((buf_T *, pos_T *));
67static pos_T *off2pos __ARGS((buf_T *, long));
68static pos_T *get_off_or_lnum __ARGS((buf_T *buf, char_u **argp));
69static long get_buf_size __ARGS((buf_T *));
Bram Moolenaar009b2592004-10-24 19:18:58 +000070static void netbeans_keystring __ARGS((int key, char *keystr));
71static void special_keys __ARGS((char_u *args));
Bram Moolenaar071d4272004-06-13 20:20:40 +000072
73static void netbeans_connect __ARGS((void));
74static int getConnInfo __ARGS((char *file, char **host, char **port, char **password));
75
76static void nb_init_graphics __ARGS((void));
77static void coloncmd __ARGS((char *cmd, ...));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000078static void nb_set_curbuf __ARGS((buf_T *buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000079#ifdef FEAT_GUI_MOTIF
80static void messageFromNetbeans __ARGS((XtPointer, int *, XtInputId *));
81#endif
82#ifdef FEAT_GUI_GTK
83static void messageFromNetbeans __ARGS((gpointer, gint, GdkInputCondition));
84#endif
85static void nb_parse_cmd __ARGS((char_u *));
86static int nb_do_cmd __ARGS((int, char_u *, int, int, char_u *));
87static void nb_send __ARGS((char *buf, char *fun));
Bram Moolenaar071d4272004-06-13 20:20:40 +000088
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000089#ifdef WIN64
90typedef __int64 NBSOCK;
91#else
92typedef int NBSOCK;
93#endif
94
95static NBSOCK sd = -1; /* socket fd for Netbeans connection */
Bram Moolenaar071d4272004-06-13 20:20:40 +000096#ifdef FEAT_GUI_MOTIF
97static XtInputId inputHandler; /* Cookie for input */
98#endif
99#ifdef FEAT_GUI_GTK
100static gint inputHandler; /* Cookie for input */
101#endif
102#ifdef FEAT_GUI_W32
103static int inputHandler = -1; /* simply ret.value of WSAAsyncSelect() */
104extern HWND s_hwnd; /* Gvim's Window handle */
105#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +0000106static int r_cmdno; /* current command number for reply */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107static int haveConnection = FALSE; /* socket is connected and
108 initialization is done */
Bram Moolenaar009b2592004-10-24 19:18:58 +0000109#ifdef FEAT_GUI_MOTIF
110static void netbeans_Xt_connect __ARGS((void *context));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000111#endif
112#ifdef FEAT_GUI_GTK
Bram Moolenaar009b2592004-10-24 19:18:58 +0000113static void netbeans_gtk_connect __ARGS((void));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000114#endif
115#ifdef FEAT_GUI_W32
Bram Moolenaar009b2592004-10-24 19:18:58 +0000116static void netbeans_w32_connect __ARGS((void));
Bram Moolenaar009b2592004-10-24 19:18:58 +0000117#endif
118
119static 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 Moolenaar009b2592004-10-24 19:18:58 +0000129#ifdef FEAT_GUI_MOTIF
130 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131netbeans_Xt_connect(void *context)
132{
133 netbeans_connect();
134 if (sd > 0)
135 {
136 /* tell notifier we are interested in being called
137 * when there is input on the editor connection socket
138 */
139 inputHandler = XtAppAddInput((XtAppContext)context, sd,
140 (XtPointer)(XtInputReadMask + XtInputExceptMask),
141 messageFromNetbeans, NULL);
142 }
143}
144
145 static void
146netbeans_disconnect(void)
147{
148 if (inputHandler != (XtInputId)NULL)
149 {
150 XtRemoveInput(inputHandler);
151 inputHandler = (XtInputId)NULL;
152 }
153 sd = -1;
154 haveConnection = FALSE;
Bram Moolenaard62bec82005-03-07 22:56:57 +0000155# ifdef FEAT_BEVAL
156 bevalServers &= ~BEVAL_NETBEANS;
157# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158}
159#endif /* FEAT_MOTIF_GUI */
160
Bram Moolenaar009b2592004-10-24 19:18:58 +0000161#ifdef FEAT_GUI_GTK
162 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163netbeans_gtk_connect(void)
164{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165 netbeans_connect();
166 if (sd > 0)
167 {
168 /*
169 * Tell gdk we are interested in being called when there
170 * is input on the editor connection socket
171 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000172 inputHandler = gdk_input_add((gint)sd, (GdkInputCondition)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
174 messageFromNetbeans, NULL);
175 }
176}
177
178 static void
179netbeans_disconnect(void)
180{
181 if (inputHandler != 0)
182 {
183 gdk_input_remove(inputHandler);
184 inputHandler = 0;
185 }
186 sd = -1;
187 haveConnection = FALSE;
Bram Moolenaard62bec82005-03-07 22:56:57 +0000188# ifdef FEAT_BEVAL
189 bevalServers &= ~BEVAL_NETBEANS;
190# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191}
192#endif /* FEAT_GUI_GTK */
193
Bram Moolenaar5b625c52005-01-31 18:55:55 +0000194#if defined(FEAT_GUI_W32) || defined(PROTO)
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000195 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196netbeans_w32_connect(void)
197{
198 netbeans_connect();
199 if (sd > 0)
200 {
201 /*
202 * Tell Windows we are interested in receiving message when there
203 * is input on the editor connection socket
204 */
205 inputHandler = WSAAsyncSelect(sd, s_hwnd, WM_NETBEANS, FD_READ);
206 }
207}
208
209 static void
210netbeans_disconnect(void)
211{
212 if (inputHandler == 0)
213 {
214 WSAAsyncSelect(sd, s_hwnd, 0, 0);
215 inputHandler = -1;
216 }
217 sd = -1;
218 haveConnection = FALSE;
Bram Moolenaard62bec82005-03-07 22:56:57 +0000219# ifdef FEAT_BEVAL
220 bevalServers &= ~BEVAL_NETBEANS;
221# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222}
223#endif /* FEAT_GUI_W32 */
224
225#define NB_DEF_HOST "localhost"
226#define NB_DEF_ADDR "3219"
227#define NB_DEF_PASS "changeme"
228
229 static void
230netbeans_connect(void)
231{
232#ifdef INET_SOCKETS
233 struct sockaddr_in server;
234 struct hostent * host;
235# ifdef FEAT_GUI_W32
236 u_short port;
237# else
238 int port;
239#endif
240#else
241 struct sockaddr_un server;
242#endif
243 char buf[32];
244 char *hostname = NULL;
245 char *address = NULL;
246 char *password = NULL;
247 char *fname;
248 char *arg = NULL;
249
250 if (netbeansArg[3] == '=')
251 {
252 /* "-nb=fname": Read info from specified file. */
253 if (getConnInfo(netbeansArg + 4, &hostname, &address, &password)
254 == FAIL)
255 return;
256 }
257 else
258 {
259 if (netbeansArg[3] == ':')
260 /* "-nb:<host>:<addr>:<password>": get info from argument */
261 arg = netbeansArg + 4;
262 if (arg == NULL && (fname = getenv("__NETBEANS_CONINFO")) != NULL)
263 {
264 /* "-nb": get info from file specified in environment */
265 if (getConnInfo(fname, &hostname, &address, &password) == FAIL)
266 return;
267 }
268 else
269 {
270 if (arg != NULL)
271 {
272 /* "-nb:<host>:<addr>:<password>": get info from argument */
273 hostname = arg;
274 address = strchr(hostname, ':');
275 if (address != NULL)
276 {
277 *address++ = '\0';
278 password = strchr(address, ':');
279 if (password != NULL)
280 *password++ = '\0';
281 }
282 }
283
284 /* Get the missing values from the environment. */
285 if (hostname == NULL || *hostname == '\0')
286 hostname = getenv("__NETBEANS_HOST");
287 if (address == NULL)
288 address = getenv("__NETBEANS_SOCKET");
289 if (password == NULL)
290 password = getenv("__NETBEANS_VIM_PASSWORD");
291
292 /* Move values to allocated memory. */
293 if (hostname != NULL)
294 hostname = (char *)vim_strsave((char_u *)hostname);
295 if (address != NULL)
296 address = (char *)vim_strsave((char_u *)address);
297 if (password != NULL)
298 password = (char *)vim_strsave((char_u *)password);
299 }
300 }
301
302 /* Use the default when a value is missing. */
303 if (hostname == NULL || *hostname == '\0')
304 {
305 vim_free(hostname);
306 hostname = (char *)vim_strsave((char_u *)NB_DEF_HOST);
307 }
308 if (address == NULL || *address == '\0')
309 {
310 vim_free(address);
311 address = (char *)vim_strsave((char_u *)NB_DEF_ADDR);
312 }
313 if (password == NULL || *password == '\0')
314 {
315 vim_free(password);
316 password = (char *)vim_strsave((char_u *)NB_DEF_PASS);
317 }
318 if (hostname == NULL || address == NULL || password == NULL)
319 goto theend; /* out of memory */
320
321#ifdef INET_SOCKETS
322 port = atoi(address);
323
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000324 if ((sd = (NBSOCK)socket(AF_INET, SOCK_STREAM, 0)) == (NBSOCK)-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325 {
326 PERROR("socket() in netbeans_connect()");
327 goto theend;
328 }
329
330 /* Get the server internet address and put into addr structure */
331 /* fill in the socket address structure and connect to server */
332 memset((char *)&server, '\0', sizeof(server));
333 server.sin_family = AF_INET;
334 server.sin_port = htons(port);
335 if ((host = gethostbyname(hostname)) == NULL)
336 {
337 if (mch_access(hostname, R_OK) >= 0)
338 {
339 /* DEBUG: input file */
340 sd = mch_open(hostname, O_RDONLY, 0);
341 goto theend;
342 }
343 PERROR("gethostbyname() in netbeans_connect()");
344 sd = -1;
345 goto theend;
346 }
347 memcpy((char *)&server.sin_addr, host->h_addr, host->h_length);
348#else
349 if ((sd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
350 {
351 PERROR("socket()");
352 goto theend;
353 }
354
355 server.sun_family = AF_UNIX;
356 strcpy(server.sun_path, address);
357#endif
358 /* Connect to server */
359 if (connect(sd, (struct sockaddr *)&server, sizeof(server)))
360 {
361 nbdebug(("netbeans_connect: Connect failed with errno %d\n", sock_errno));
362 if (sock_errno == ECONNREFUSED)
363 {
364 sock_close(sd);
365#ifdef INET_SOCKETS
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000366 if ((sd = (NBSOCK)socket(AF_INET, SOCK_STREAM, 0)) == (NBSOCK)-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367 {
368 PERROR("socket()#2 in netbeans_connect()");
369 goto theend;
370 }
371#else
372 if ((sd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
373 {
374 PERROR("socket()#2 in netbeans_connect()");
375 goto theend;
376 }
377#endif
378 if (connect(sd, (struct sockaddr *)&server, sizeof(server)))
379 {
380 int retries = 36;
381 int success = FALSE;
382 while (retries--
383 && ((sock_errno == ECONNREFUSED) || (sock_errno == EINTR)))
384 {
385 nbdebug(("retrying...\n"));
386 sleep(5);
387 if (connect(sd, (struct sockaddr *)&server,
388 sizeof(server)) == 0)
389 {
390 success = TRUE;
391 break;
392 }
393 }
394 if (!success)
395 {
396 /* Get here when the server can't be found. */
397 PERROR(_("Cannot connect to Netbeans #2"));
398 getout(1);
399 }
400 }
401
402 }
403 else
404 {
405 PERROR(_("Cannot connect to Netbeans"));
406 getout(1);
407 }
408 }
409
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000410 vim_snprintf(buf, sizeof(buf), "AUTH %s\n", password);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411 nb_send(buf, "netbeans_connect");
412
413 sprintf(buf, "0:version=0 \"%s\"\n", ExtEdProtocolVersion);
414 nb_send(buf, "externaleditor_version");
415
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416/* nb_init_graphics(); delay until needed */
417
418 haveConnection = TRUE;
419
420theend:
421 vim_free(hostname);
422 vim_free(address);
423 vim_free(password);
424 return;
425}
426
427/*
428 * Obtain the NetBeans hostname, port address and password from a file.
429 * Return the strings in allocated memory.
430 * Return FAIL if the file could not be read, OK otherwise (no matter what it
431 * contains).
432 */
433 static int
434getConnInfo(char *file, char **host, char **port, char **auth)
435{
436 FILE *fp;
437 char_u buf[BUFSIZ];
438 char_u *lp;
439 char_u *nl;
440#ifdef UNIX
441 struct stat st;
442
443 /*
444 * For Unix only accept the file when it's not accessible by others.
445 * The open will then fail if we don't own the file.
446 */
447 if (mch_stat(file, &st) == 0 && (st.st_mode & 0077) != 0)
448 {
449 EMSG2(_("E668: Wrong access mode for NetBeans connection info file: \"%s\""),
450 file);
451 return FAIL;
452 }
453#endif
454
455 fp = mch_fopen(file, "r");
456 if (fp == NULL)
457 {
458 PERROR("E660: Cannot open NetBeans connection info file");
459 return FAIL;
460 }
461
462 /* Read the file. There should be one of each parameter */
463 while ((lp = (char_u *)fgets((char *)buf, BUFSIZ, fp)) != NULL)
464 {
465 if ((nl = vim_strchr(lp, '\n')) != NULL)
466 *nl = 0; /* strip off the trailing newline */
467
468 if (STRNCMP(lp, "host=", 5) == 0)
469 {
470 vim_free(*host);
471 *host = (char *)vim_strsave(&buf[5]);
472 }
473 else if (STRNCMP(lp, "port=", 5) == 0)
474 {
475 vim_free(*port);
476 *port = (char *)vim_strsave(&buf[5]);
477 }
478 else if (STRNCMP(lp, "auth=", 5) == 0)
479 {
480 vim_free(*auth);
481 *auth = (char *)vim_strsave(&buf[5]);
482 }
483 }
484 fclose(fp);
485
486 return OK;
487}
488
489
490struct keyqueue
491{
492 int key;
493 struct keyqueue *next;
494 struct keyqueue *prev;
495};
496
497typedef struct keyqueue keyQ_T;
498
499static keyQ_T keyHead; /* dummy node, header for circular queue */
500
501
502/*
503 * Queue up key commands sent from netbeans.
504 */
505 static void
506postpone_keycommand(int key)
507{
508 keyQ_T *node;
509
510 node = (keyQ_T *)alloc(sizeof(keyQ_T));
511
512 if (keyHead.next == NULL) /* initialize circular queue */
513 {
514 keyHead.next = &keyHead;
515 keyHead.prev = &keyHead;
516 }
517
518 /* insert node at tail of queue */
519 node->next = &keyHead;
520 node->prev = keyHead.prev;
521 keyHead.prev->next = node;
522 keyHead.prev = node;
523
524 node->key = key;
525}
526
527/*
528 * Handle any queued-up NetBeans keycommands to be send.
529 */
530 static void
531handle_key_queue(void)
532{
533 while (keyHead.next && keyHead.next != &keyHead)
534 {
535 /* first, unlink the node */
536 keyQ_T *node = keyHead.next;
537 keyHead.next = node->next;
538 node->next->prev = node->prev;
539
540 /* now, send the keycommand */
541 netbeans_keycommand(node->key);
542
543 /* Finally, dispose of the node */
544 vim_free(node);
545 }
546}
547
548
549struct cmdqueue
550{
551 char_u *buffer;
552 struct cmdqueue *next;
553 struct cmdqueue *prev;
554};
555
556typedef struct cmdqueue queue_T;
557
558static queue_T head; /* dummy node, header for circular queue */
559
560
561/*
562 * Put the buffer on the work queue; possibly save it to a file as well.
563 */
564 static void
565save(char_u *buf, int len)
566{
567 queue_T *node;
568
569 node = (queue_T *)alloc(sizeof(queue_T));
570 if (node == NULL)
571 return; /* out of memory */
572 node->buffer = alloc(len + 1);
573 if (node->buffer == NULL)
574 {
575 vim_free(node);
576 return; /* out of memory */
577 }
578 mch_memmove(node->buffer, buf, (size_t)len);
579 node->buffer[len] = NUL;
580
581 if (head.next == NULL) /* initialize circular queue */
582 {
583 head.next = &head;
584 head.prev = &head;
585 }
586
587 /* insert node at tail of queue */
588 node->next = &head;
589 node->prev = head.prev;
590 head.prev->next = node;
591 head.prev = node;
592
593#ifdef NBDEBUG
594 {
595 static int outfd = -2;
596
597 /* possibly write buffer out to a file */
598 if (outfd == -3)
599 return;
600
601 if (outfd == -2)
602 {
603 char *file = getenv("__NETBEANS_SAVE");
604 if (file == NULL)
605 outfd = -3;
606 else
607 outfd = mch_open(file, O_WRONLY|O_CREAT|O_TRUNC, 0666);
608 }
609
610 if (outfd >= 0)
611 write(outfd, buf, len);
612 }
613#endif
614}
615
616
617/*
618 * While there's still a command in the work queue, parse and execute it.
619 */
620 static void
621nb_parse_messages(void)
622{
623 char_u *p;
624 queue_T *node;
625
626 while (head.next != &head)
627 {
628 node = head.next;
629
630 /* Locate the first line in the first buffer. */
631 p = vim_strchr(node->buffer, '\n');
632 if (p == NULL)
633 {
634 /* Command isn't complete. If there is no following buffer,
635 * return (wait for more). If there is another buffer following,
636 * prepend the text to that buffer and delete this one. */
637 if (node->next == &head)
638 return;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000639 p = alloc((unsigned)(STRLEN(node->buffer) + STRLEN(node->next->buffer) + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 if (p == NULL)
641 return; /* out of memory */
642 STRCPY(p, node->buffer);
643 STRCAT(p, node->next->buffer);
644 vim_free(node->next->buffer);
645 node->next->buffer = p;
646
647 /* dispose of the node and buffer */
648 head.next = node->next;
649 node->next->prev = node->prev;
650 vim_free(node->buffer);
651 vim_free(node);
652 }
653 else
654 {
655 /* There is a complete command at the start of the buffer.
656 * Terminate it with a NUL. When no more text is following unlink
657 * the buffer. Do this before executing, because new buffers can
658 * be added while busy handling the command. */
659 *p++ = NUL;
660 if (*p == NUL)
661 {
662 head.next = node->next;
663 node->next->prev = node->prev;
664 }
665
666 /* now, parse and execute the commands */
667 nb_parse_cmd(node->buffer);
668
669 if (*p == NUL)
670 {
671 /* buffer finished, dispose of the node and buffer */
672 vim_free(node->buffer);
673 vim_free(node);
674 }
675 else
676 {
677 /* more follows, move to the start */
678 mch_memmove(node->buffer, p, STRLEN(p) + 1);
679 }
680 }
681 }
682}
683
684/* Buffer size for reading incoming messages. */
685#define MAXMSGSIZE 4096
686
687/*
688 * Read and process a command from netbeans.
689 */
690/*ARGSUSED*/
691#if defined(FEAT_GUI_W32) || defined(PROTO)
692/* Use this one when generating prototypes, the others are static. */
693 void
694messageFromNetbeansW32()
695#else
696# ifdef FEAT_GUI_MOTIF
697 static void
698messageFromNetbeans(XtPointer clientData, int *unused1, XtInputId *unused2)
699# endif
700# ifdef FEAT_GUI_GTK
701 static void
702messageFromNetbeans(gpointer clientData, gint unused1,
703 GdkInputCondition unused2)
704# endif
705#endif
706{
707 static char_u *buf = NULL;
708 int len;
709 int readlen = 0;
710 static int level = 0;
711
712 if (sd < 0)
713 {
714 nbdebug(("messageFromNetbeans() called without a socket\n"));
715 return;
716 }
717
718 ++level; /* recursion guard; this will be called from the X event loop */
719
720 /* Allocate a buffer to read into. */
721 if (buf == NULL)
722 {
723 buf = alloc(MAXMSGSIZE);
724 if (buf == NULL)
725 return; /* out of memory! */
726 }
727
728 /* Keep on reading for as long as there is something to read. */
729 for (;;)
730 {
731 len = sock_read(sd, buf, MAXMSGSIZE);
732 if (len <= 0)
733 break; /* error or nothing more to read */
734
735 /* Store the read message in the queue. */
736 save(buf, len);
737 readlen += len;
738 if (len < MAXMSGSIZE)
739 break; /* did read everything that's available */
740 }
741
742 if (readlen <= 0)
743 {
744 /* read error or didn't read anything */
745 netbeans_disconnect();
746 nbdebug(("messageFromNetbeans: Error in read() from socket\n"));
747 if (len < 0)
748 PERROR(_("read from Netbeans socket"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000749 return; /* don't try to parse it */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 }
751
752 /* Parse the messages, but avoid recursion. */
753 if (level == 1)
754 nb_parse_messages();
755
756 --level;
757}
758
759/*
760 * Handle one NUL terminated command.
761 *
762 * format of a command from netbeans:
763 *
764 * 6:setTitle!84 "a.c"
765 *
766 * bufno
767 * colon
768 * cmd
769 * !
770 * cmdno
771 * args
772 *
773 * for function calls, the ! is replaced by a /
774 */
775 static void
776nb_parse_cmd(char_u *cmd)
777{
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000778 char *verb;
779 char *q;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 int bufno;
781 int isfunc = -1;
782
783 if (STRCMP(cmd, "DISCONNECT") == 0)
784 {
785 /* We assume the server knows that we can safely exit! */
786 if (sd >= 0)
787 sock_close(sd);
788 /* Disconnect before exiting, Motif hangs in a Select error
789 * message otherwise. */
790 netbeans_disconnect();
791 getout(0);
792 /* NOTREACHED */
793 }
794
795 if (STRCMP(cmd, "DETACH") == 0)
796 {
797 /* The IDE is breaking the connection. */
798 if (sd >= 0)
799 sock_close(sd);
800 netbeans_disconnect();
801 return;
802 }
803
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000804 bufno = strtol((char *)cmd, &verb, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805
806 if (*verb != ':')
807 {
808 EMSG2("E627: missing colon: %s", cmd);
809 return;
810 }
811 ++verb; /* skip colon */
812
813 for (q = verb; *q; q++)
814 {
815 if (*q == '!')
816 {
817 *q++ = NUL;
818 isfunc = 0;
819 break;
820 }
821 else if (*q == '/')
822 {
823 *q++ = NUL;
824 isfunc = 1;
825 break;
826 }
827 }
828
829 if (isfunc < 0)
830 {
831 EMSG2("E628: missing ! or / in: %s", cmd);
832 return;
833 }
834
Bram Moolenaar89d40322006-08-29 15:30:07 +0000835 r_cmdno = strtol(q, &q, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000837 q = (char *)skipwhite((char_u *)q);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838
Bram Moolenaar89d40322006-08-29 15:30:07 +0000839 if (nb_do_cmd(bufno, (char_u *)verb, isfunc, r_cmdno, (char_u *)q) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 {
Bram Moolenaar009b2592004-10-24 19:18:58 +0000841#ifdef NBDEBUG
842 /*
843 * This happens because the ExtEd can send a cammand or 2 after
844 * doing a stopDocumentListen command. It doesn't harm anything
845 * so I'm disabling it except for debugging.
846 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 nbdebug(("nb_parse_cmd: Command error for \"%s\"\n", cmd));
848 EMSG("E629: bad return from nb_do_cmd");
Bram Moolenaar009b2592004-10-24 19:18:58 +0000849#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 }
851}
852
853struct nbbuf_struct
854{
855 buf_T *bufp;
856 unsigned int fireChanges:1;
857 unsigned int initDone:1;
Bram Moolenaar009b2592004-10-24 19:18:58 +0000858 unsigned int insertDone:1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 unsigned int modified:1;
Bram Moolenaar009b2592004-10-24 19:18:58 +0000860 int nbbuf_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 char *displayname;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 int *signmap;
863 short_u signmaplen;
864 short_u signmapused;
865};
866
867typedef struct nbbuf_struct nbbuf_T;
868
869static nbbuf_T *buf_list = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000870static int buf_list_size = 0; /* size of buf_list */
871static int buf_list_used = 0; /* nr of entries in buf_list actually in use */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872
873static char **globalsignmap;
874static int globalsignmaplen;
875static int globalsignmapused;
876
877static int mapsigntype __ARGS((nbbuf_T *, int localsigntype));
878static void addsigntype __ARGS((nbbuf_T *, int localsigntype, char_u *typeName,
879 char_u *tooltip, char_u *glyphfile,
880 int usefg, int fg, int usebg, int bg));
Bram Moolenaar009b2592004-10-24 19:18:58 +0000881static void print_read_msg __ARGS((nbbuf_T *buf));
882static void print_save_msg __ARGS((nbbuf_T *buf, long nchars));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883
884static int curPCtype = -1;
885
886/*
887 * Get the Netbeans buffer number for the specified buffer.
888 */
889 static int
890nb_getbufno(buf_T *bufp)
891{
892 int i;
893
894 for (i = 0; i < buf_list_used; i++)
895 if (buf_list[i].bufp == bufp)
896 return i;
897 return -1;
898}
899
900/*
901 * Is this a NetBeans-owned buffer?
902 */
903 int
904isNetbeansBuffer(buf_T *bufp)
905{
Bram Moolenaar009b2592004-10-24 19:18:58 +0000906 return usingNetbeans && bufp->b_netbeans_file;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907}
908
909/*
910 * NetBeans and Vim have different undo models. In Vim, the file isn't
911 * changed if changes are undone via the undo command. In NetBeans, once
912 * a change has been made the file is marked as modified until saved. It
913 * doesn't matter if the change was undone.
914 *
915 * So this function is for the corner case where Vim thinks a buffer is
916 * unmodified but NetBeans thinks it IS modified.
917 */
918 int
919isNetbeansModified(buf_T *bufp)
920{
Bram Moolenaar009b2592004-10-24 19:18:58 +0000921 if (usingNetbeans && bufp->b_netbeans_file)
922 {
923 int bufno = nb_getbufno(bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924
Bram Moolenaar009b2592004-10-24 19:18:58 +0000925 if (bufno > 0)
926 return buf_list[bufno].modified;
927 else
928 return FALSE;
929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 else
931 return FALSE;
932}
933
934/*
935 * Given a Netbeans buffer number, return the netbeans buffer.
936 * Returns NULL for 0 or a negative number. A 0 bufno means a
937 * non-buffer related command has been sent.
938 */
939 static nbbuf_T *
940nb_get_buf(int bufno)
941{
942 /* find or create a buffer with the given number */
943 int incr;
944
945 if (bufno <= 0)
946 return NULL;
947
948 if (!buf_list)
949 {
950 /* initialize */
951 buf_list = (nbbuf_T *)alloc_clear(100 * sizeof(nbbuf_T));
952 buf_list_size = 100;
953 }
954 if (bufno >= buf_list_used) /* new */
955 {
956 if (bufno >= buf_list_size) /* grow list */
957 {
958 incr = bufno - buf_list_size + 90;
959 buf_list_size += incr;
960 buf_list = (nbbuf_T *)vim_realloc(
961 buf_list, buf_list_size * sizeof(nbbuf_T));
962 memset(buf_list + buf_list_size - incr, 0, incr * sizeof(nbbuf_T));
963 }
964
965 while (buf_list_used <= bufno)
966 {
967 /* Default is to fire text changes. */
968 buf_list[buf_list_used].fireChanges = 1;
969 ++buf_list_used;
970 }
971 }
972
973 return buf_list + bufno;
974}
975
976/*
977 * Return the number of buffers that are modified.
978 */
979 static int
980count_changed_buffers(void)
981{
982 buf_T *bufp;
983 int n;
984
985 n = 0;
986 for (bufp = firstbuf; bufp != NULL; bufp = bufp->b_next)
987 if (bufp->b_changed)
988 ++n;
989 return n;
990}
991
992/*
993 * End the netbeans session.
994 */
995 void
996netbeans_end(void)
997{
998 int i;
999 static char buf[128];
1000
1001 if (!haveConnection)
1002 return;
1003
1004 for (i = 0; i < buf_list_used; i++)
1005 {
1006 if (!buf_list[i].bufp)
1007 continue;
1008 if (netbeansForcedQuit)
1009 {
1010 /* mark as unmodified so NetBeans won't put up dialog on "killed" */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001011 sprintf(buf, "%d:unmodified=%d\n", i, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 nbdebug(("EVT: %s", buf));
1013 nb_send(buf, "netbeans_end");
1014 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001015 sprintf(buf, "%d:killed=%d\n", i, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 nbdebug(("EVT: %s", buf));
1017/* nb_send(buf, "netbeans_end"); avoid "write failed" messages */
1018 if (sd >= 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001019 sock_write(sd, buf, (int)STRLEN(buf)); /* ignore errors */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021}
1022
1023/*
1024 * Send a message to netbeans.
1025 */
1026 static void
1027nb_send(char *buf, char *fun)
1028{
1029 /* Avoid giving pages full of error messages when the other side has
1030 * exited, only mention the first error until the connection works again. */
1031 static int did_error = FALSE;
1032
1033 if (sd < 0)
1034 {
1035 if (!did_error)
1036 EMSG2("E630: %s(): write while not connected", fun);
1037 did_error = TRUE;
1038 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001039 else if (sock_write(sd, buf, (int)STRLEN(buf)) != (int)STRLEN(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 {
1041 if (!did_error)
1042 EMSG2("E631: %s(): write failed", fun);
1043 did_error = TRUE;
1044 }
1045 else
1046 did_error = FALSE;
1047}
1048
1049/*
1050 * Some input received from netbeans requires a response. This function
1051 * handles a response with no information (except the command number).
1052 */
1053 static void
1054nb_reply_nil(int cmdno)
1055{
1056 char reply[32];
1057
1058 if (!haveConnection)
1059 return;
1060
Bram Moolenaar009b2592004-10-24 19:18:58 +00001061 nbdebug(("REP %d: <none>\n", cmdno));
1062
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 sprintf(reply, "%d\n", cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 nb_send(reply, "nb_reply_nil");
1065}
1066
1067
1068/*
1069 * Send a response with text.
1070 * "result" must have been quoted already (using nb_quote()).
1071 */
1072 static void
1073nb_reply_text(int cmdno, char_u *result)
1074{
1075 char_u *reply;
1076
1077 if (!haveConnection)
1078 return;
1079
Bram Moolenaar009b2592004-10-24 19:18:58 +00001080 nbdebug(("REP %d: %s\n", cmdno, (char *)result));
1081
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001082 reply = alloc((unsigned)STRLEN(result) + 32);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 sprintf((char *)reply, "%d %s\n", cmdno, (char *)result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 nb_send((char *)reply, "nb_reply_text");
1085
1086 vim_free(reply);
1087}
1088
1089
1090/*
1091 * Send a response with a number result code.
1092 */
1093 static void
1094nb_reply_nr(int cmdno, long result)
1095{
1096 char reply[32];
1097
1098 if (!haveConnection)
1099 return;
1100
Bram Moolenaar009b2592004-10-24 19:18:58 +00001101 nbdebug(("REP %d: %ld\n", cmdno, result));
1102
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 sprintf(reply, "%d %ld\n", cmdno, result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 nb_send(reply, "nb_reply_nr");
1105}
1106
1107
1108/*
1109 * Encode newline, ret, backslash, double quote for transmission to NetBeans.
1110 */
1111 static char_u *
1112nb_quote(char_u *txt)
1113{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001114 char_u *buf = alloc((unsigned)(2 * STRLEN(txt) + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 char_u *p = txt;
1116 char_u *q = buf;
1117
1118 if (buf == NULL)
1119 return NULL;
1120 for (; *p; p++)
1121 {
1122 switch (*p)
1123 {
1124 case '\"':
1125 case '\\':
1126 *q++ = '\\'; *q++ = *p; break;
1127 /* case '\t': */
1128 /* *q++ = '\\'; *q++ = 't'; break; */
1129 case '\n':
1130 *q++ = '\\'; *q++ = 'n'; break;
1131 case '\r':
1132 *q++ = '\\'; *q++ = 'r'; break;
1133 default:
1134 *q++ = *p;
1135 break;
1136 }
1137 }
1138 *q++ = '\0';
1139
1140 return buf;
1141}
1142
1143
1144/*
1145 * Remove top level double quotes; convert backslashed chars.
1146 * Returns an allocated string (NULL for failure).
1147 * If "endp" is not NULL it is set to the character after the terminating
1148 * quote.
1149 */
1150 static char *
1151nb_unquote(char_u *p, char_u **endp)
1152{
1153 char *result = 0;
1154 char *q;
1155 int done = 0;
1156
1157 /* result is never longer than input */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001158 result = (char *)alloc_clear((unsigned)STRLEN(p) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 if (result == NULL)
1160 return NULL;
1161
1162 if (*p++ != '"')
1163 {
1164 nbdebug(("nb_unquote called with string that doesn't start with a quote!: %s\n",
1165 p));
1166 result[0] = NUL;
1167 return result;
1168 }
1169
1170 for (q = result; !done && *p != NUL;)
1171 {
1172 switch (*p)
1173 {
1174 case '"':
1175 /*
1176 * Unbackslashed dquote marks the end, if first char was dquote.
1177 */
1178 done = 1;
1179 break;
1180
1181 case '\\':
1182 ++p;
1183 switch (*p)
1184 {
1185 case '\\': *q++ = '\\'; break;
1186 case 'n': *q++ = '\n'; break;
1187 case 't': *q++ = '\t'; break;
1188 case 'r': *q++ = '\r'; break;
1189 case '"': *q++ = '"'; break;
1190 case NUL: --p; break;
1191 /* default: skip over illegal chars */
1192 }
1193 ++p;
1194 break;
1195
1196 default:
1197 *q++ = *p++;
1198 }
1199 }
1200
1201 if (endp != NULL)
1202 *endp = p;
1203
1204 return result;
1205}
1206
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001207/*
1208 * Remove from "first" byte to "last" byte (inclusive), at line "lnum" of the
1209 * current buffer. Remove to end of line when "last" is MAXCOL.
1210 */
1211 static void
1212nb_partialremove(linenr_T lnum, colnr_T first, colnr_T last)
1213{
1214 char_u *oldtext, *newtext;
1215 int oldlen;
1216 int lastbyte = last;
1217
1218 oldtext = ml_get(lnum);
1219 oldlen = STRLEN(oldtext);
Bram Moolenaarb3c70982008-01-18 10:40:55 +00001220 if (first >= (colnr_T)oldlen || oldlen == 0) /* just in case */
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001221 return;
1222 if (lastbyte >= oldlen)
1223 lastbyte = oldlen - 1;
1224 newtext = alloc(oldlen - (int)(lastbyte - first));
1225 if (newtext != NULL)
1226 {
1227 mch_memmove(newtext, oldtext, first);
1228 mch_memmove(newtext + first, oldtext + lastbyte + 1, STRLEN(oldtext + lastbyte + 1) + 1);
1229 nbdebug((" NEW LINE %d: %s\n", lnum, newtext));
1230 ml_replace(lnum, newtext, FALSE);
1231 }
1232}
1233
1234/*
1235 * Replace the "first" line with the concatenation of the "first" and
1236 * the "other" line. The "other" line is not removed.
1237 */
1238 static void
1239nb_joinlines(linenr_T first, linenr_T other)
1240{
1241 int len_first, len_other;
1242 char_u *p;
1243
1244 len_first = STRLEN(ml_get(first));
1245 len_other = STRLEN(ml_get(other));
1246 p = alloc((unsigned)(len_first + len_other + 1));
1247 if (p != NULL)
1248 {
1249 mch_memmove(p, ml_get(first), len_first);
1250 mch_memmove(p + len_first, ml_get(other), len_other + 1);
1251 ml_replace(first, p, FALSE);
1252 }
1253}
1254
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255#define SKIP_STOP 2
1256#define streq(a,b) (strcmp(a,b) == 0)
1257static int needupdate = 0;
1258static int inAtomic = 0;
1259
1260/*
1261 * Do the actual processing of a single netbeans command or function.
Bram Moolenaar143c38c2007-05-10 16:41:10 +00001262 * The difference between a command and function is that a function
1263 * gets a response (it's required) but a command does not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 * For arguments see comment for nb_parse_cmd().
1265 */
1266 static int
1267nb_do_cmd(
1268 int bufno,
1269 char_u *cmd,
1270 int func,
1271 int cmdno,
1272 char_u *args) /* points to space before arguments or NUL */
1273{
1274 int doupdate = 0;
1275 long off = 0;
1276 nbbuf_T *buf = nb_get_buf(bufno);
1277 static int skip = 0;
1278 int retval = OK;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001279 char *cp; /* for when a char pointer is needed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280
1281 nbdebug(("%s %d: (%d) %s %s\n", (func) ? "FUN" : "CMD", cmdno, bufno, cmd,
1282 STRCMP(cmd, "insert") == 0 ? "<text>" : (char *)args));
1283
1284 if (func)
1285 {
1286/* =====================================================================*/
1287 if (streq((char *)cmd, "getModified"))
1288 {
1289 if (buf == NULL || buf->bufp == NULL)
1290 /* Return the number of buffers that are modified. */
1291 nb_reply_nr(cmdno, (long)count_changed_buffers());
1292 else
1293 /* Return whether the buffer is modified. */
1294 nb_reply_nr(cmdno, (long)(buf->bufp->b_changed
1295 || isNetbeansModified(buf->bufp)));
1296/* =====================================================================*/
1297 }
1298 else if (streq((char *)cmd, "saveAndExit"))
1299 {
1300 /* Note: this will exit Vim if successful. */
1301 coloncmd(":confirm qall");
1302
1303 /* We didn't exit: return the number of changed buffers. */
1304 nb_reply_nr(cmdno, (long)count_changed_buffers());
1305/* =====================================================================*/
1306 }
1307 else if (streq((char *)cmd, "getCursor"))
1308 {
1309 char_u text[200];
1310
1311 /* Note: nb_getbufno() may return -1. This indicates the IDE
1312 * didn't assign a number to the current buffer in response to a
1313 * fileOpened event. */
1314 sprintf((char *)text, "%d %ld %d %ld",
1315 nb_getbufno(curbuf),
1316 (long)curwin->w_cursor.lnum,
1317 (int)curwin->w_cursor.col,
1318 pos2off(curbuf, &curwin->w_cursor));
1319 nb_reply_text(cmdno, text);
1320/* =====================================================================*/
1321 }
Bram Moolenaarc65c4912006-11-14 17:29:46 +00001322 else if (streq((char *)cmd, "getAnno"))
1323 {
1324 long linenum = 0;
1325#ifdef FEAT_SIGNS
1326 if (buf == NULL || buf->bufp == NULL)
1327 {
1328 nbdebug((" null bufp in getAnno"));
1329 EMSG("E652: null bufp in getAnno");
1330 retval = FAIL;
1331 }
1332 else
1333 {
1334 int serNum;
1335
1336 cp = (char *)args;
1337 serNum = strtol(cp, &cp, 10);
1338 /* If the sign isn't found linenum will be zero. */
1339 linenum = (long)buf_findsign(buf->bufp, serNum);
1340 }
1341#endif
1342 nb_reply_nr(cmdno, linenum);
1343/* =====================================================================*/
1344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 else if (streq((char *)cmd, "getLength"))
1346 {
1347 long len = 0;
1348
1349 if (buf == NULL || buf->bufp == NULL)
1350 {
1351 nbdebug((" null bufp in getLength"));
1352 EMSG("E632: null bufp in getLength");
1353 retval = FAIL;
1354 }
1355 else
1356 {
1357 len = get_buf_size(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 }
1359 nb_reply_nr(cmdno, len);
1360/* =====================================================================*/
1361 }
1362 else if (streq((char *)cmd, "getText"))
1363 {
1364 long len;
1365 linenr_T nlines;
1366 char_u *text = NULL;
1367 linenr_T lno = 1;
1368 char_u *p;
1369 char_u *line;
1370
1371 if (buf == NULL || buf->bufp == NULL)
1372 {
1373 nbdebug((" null bufp in getText"));
1374 EMSG("E633: null bufp in getText");
1375 retval = FAIL;
1376 }
1377 else
1378 {
1379 len = get_buf_size(buf->bufp);
1380 nlines = buf->bufp->b_ml.ml_line_count;
1381 text = alloc((unsigned)((len > 0)
1382 ? ((len + nlines) * 2) : 4));
1383 if (text == NULL)
1384 {
1385 nbdebug((" nb_do_cmd: getText has null text field\n"));
1386 retval = FAIL;
1387 }
1388 else
1389 {
1390 p = text;
1391 *p++ = '\"';
1392 for (; lno <= nlines ; lno++)
1393 {
1394 line = nb_quote(ml_get_buf(buf->bufp, lno, FALSE));
1395 if (line != NULL)
1396 {
1397 STRCPY(p, line);
1398 p += STRLEN(line);
1399 *p++ = '\\';
1400 *p++ = 'n';
Bram Moolenaar009b2592004-10-24 19:18:58 +00001401 vim_free(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 }
1404 *p++ = '\"';
1405 *p = '\0';
1406 }
1407 }
1408 if (text == NULL)
1409 nb_reply_text(cmdno, (char_u *)"");
1410 else
1411 {
1412 nb_reply_text(cmdno, text);
1413 vim_free(text);
1414 }
1415/* =====================================================================*/
1416 }
1417 else if (streq((char *)cmd, "remove"))
1418 {
1419 long count;
1420 pos_T first, last;
1421 pos_T *pos;
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001422 pos_T *next;
1423 linenr_T del_from_lnum, del_to_lnum; /* lines to be deleted as a whole */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 int oldFire = netbeansFireChanges;
1425 int oldSuppress = netbeansSuppressNoLines;
1426 int wasChanged;
1427
1428 if (skip >= SKIP_STOP)
1429 {
1430 nbdebug((" Skipping %s command\n", (char *) cmd));
1431 nb_reply_nil(cmdno);
1432 return OK;
1433 }
1434
1435 if (buf == NULL || buf->bufp == NULL)
1436 {
1437 nbdebug((" null bufp in remove"));
1438 EMSG("E634: null bufp in remove");
1439 retval = FAIL;
1440 }
1441 else
1442 {
1443 netbeansFireChanges = FALSE;
1444 netbeansSuppressNoLines = TRUE;
1445
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001446 nb_set_curbuf(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 wasChanged = buf->bufp->b_changed;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001448 cp = (char *)args;
1449 off = strtol(cp, &cp, 10);
1450 count = strtol(cp, &cp, 10);
1451 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 /* delete "count" chars, starting at "off" */
1453 pos = off2pos(buf->bufp, off);
1454 if (!pos)
1455 {
1456 nb_reply_text(cmdno, (char_u *)"!bad position");
1457 netbeansFireChanges = oldFire;
1458 netbeansSuppressNoLines = oldSuppress;
1459 return FAIL;
1460 }
1461 first = *pos;
1462 nbdebug((" FIRST POS: line %d, col %d\n", first.lnum, first.col));
1463 pos = off2pos(buf->bufp, off+count-1);
1464 if (!pos)
1465 {
1466 nb_reply_text(cmdno, (char_u *)"!bad count");
1467 netbeansFireChanges = oldFire;
1468 netbeansSuppressNoLines = oldSuppress;
1469 return FAIL;
1470 }
1471 last = *pos;
1472 nbdebug((" LAST POS: line %d, col %d\n", last.lnum, last.col));
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001473 del_from_lnum = first.lnum;
1474 del_to_lnum = last.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 doupdate = 1;
1476
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001477 /* Get the position of the first byte after the deleted
1478 * section. "next" is NULL when deleting to the end of the
1479 * file. */
1480 next = off2pos(buf->bufp, off + count);
1481
1482 /* Remove part of the first line. */
1483 if (first.col != 0 || (next != NULL && first.lnum == next->lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 {
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001485 if (first.lnum != last.lnum
1486 || (next != NULL && first.lnum != next->lnum))
1487 {
1488 /* remove to the end of the first line */
1489 nb_partialremove(first.lnum, first.col,
1490 (colnr_T)MAXCOL);
1491 if (first.lnum == last.lnum)
1492 {
1493 /* Partial line to remove includes the end of
1494 * line. Join the line with the next one, have
1495 * the next line deleted below. */
1496 nb_joinlines(first.lnum, next->lnum);
1497 del_to_lnum = next->lnum;
1498 }
1499 }
1500 else
1501 {
1502 /* remove within one line */
1503 nb_partialremove(first.lnum, first.col, last.col);
1504 }
1505 ++del_from_lnum; /* don't delete the first line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 }
1507
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001508 /* Remove part of the last line. */
1509 if (first.lnum != last.lnum && next != NULL
1510 && next->col != 0 && last.lnum == next->lnum)
1511 {
1512 nb_partialremove(last.lnum, 0, last.col);
1513 if (del_from_lnum > first.lnum)
1514 {
1515 /* Join end of last line to start of first line; last
1516 * line is deleted below. */
1517 nb_joinlines(first.lnum, last.lnum);
1518 }
1519 else
1520 /* First line is deleted as a whole, keep the last
1521 * line. */
1522 --del_to_lnum;
1523 }
1524
1525 /* First is partial line; last line to remove includes
1526 * the end of line; join first line to line following last
1527 * line; line following last line is deleted below. */
1528 if (first.lnum != last.lnum && del_from_lnum > first.lnum
1529 && next != NULL && last.lnum != next->lnum)
1530 {
1531 nb_joinlines(first.lnum, next->lnum);
1532 del_to_lnum = next->lnum;
1533 }
1534
1535 /* Delete whole lines if there are any. */
1536 if (del_to_lnum >= del_from_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 {
1538 int i;
1539
1540 /* delete signs from the lines being deleted */
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001541 for (i = del_from_lnum; i <= del_to_lnum; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 {
1543 int id = buf_findsign_id(buf->bufp, (linenr_T)i);
1544 if (id > 0)
1545 {
1546 nbdebug((" Deleting sign %d on line %d\n", id, i));
1547 buf_delsign(buf->bufp, id);
1548 }
1549 else
1550 nbdebug((" No sign on line %d\n", i));
1551 }
1552
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001553 nbdebug((" Deleting lines %d through %d\n", del_from_lnum, del_to_lnum));
1554 curwin->w_cursor.lnum = del_from_lnum;
1555 curwin->w_cursor.col = 0;
1556 del_lines(del_to_lnum - del_from_lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 }
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001558
1559 /* Leave cursor at first deleted byte. */
1560 curwin->w_cursor = first;
1561 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 buf->bufp->b_changed = wasChanged; /* logically unchanged */
1563 netbeansFireChanges = oldFire;
1564 netbeansSuppressNoLines = oldSuppress;
1565
1566 u_blockfree(buf->bufp);
1567 u_clearall(buf->bufp);
1568 }
1569 nb_reply_nil(cmdno);
1570/* =====================================================================*/
1571 }
1572 else if (streq((char *)cmd, "insert"))
1573 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 char_u *to_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575
1576 if (skip >= SKIP_STOP)
1577 {
1578 nbdebug((" Skipping %s command\n", (char *) cmd));
1579 nb_reply_nil(cmdno);
1580 return OK;
1581 }
1582
1583 /* get offset */
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001584 cp = (char *)args;
1585 off = strtol(cp, &cp, 10);
1586 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587
1588 /* get text to be inserted */
1589 args = skipwhite(args);
1590 args = to_free = (char_u *)nb_unquote(args, NULL);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001591 /*
1592 nbdebug((" CHUNK[%d]: %d bytes at offset %d\n",
1593 buf->bufp->b_ml.ml_line_count, STRLEN(args), off));
1594 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595
1596 if (buf == NULL || buf->bufp == NULL)
1597 {
1598 nbdebug((" null bufp in insert"));
1599 EMSG("E635: null bufp in insert");
1600 retval = FAIL;
1601 }
1602 else if (args != NULL)
1603 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001604 int ff_detected = EOL_UNKNOWN;
1605 int buf_was_empty = (buf->bufp->b_ml.ml_flags & ML_EMPTY);
1606 size_t len = 0;
1607 int added = 0;
1608 int oldFire = netbeansFireChanges;
1609 int old_b_changed;
1610 char_u *nl;
1611 linenr_T lnum;
1612 linenr_T lnum_start;
1613 pos_T *pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 netbeansFireChanges = 0;
1616
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001617 /* Jump to the buffer where we insert. After this "curbuf"
1618 * can be used. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001619 nb_set_curbuf(buf->bufp);
Bram Moolenaara3227e22006-03-08 21:32:40 +00001620 old_b_changed = curbuf->b_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001622 /* Convert the specified character offset into a lnum/col
1623 * position. */
Bram Moolenaara3227e22006-03-08 21:32:40 +00001624 pos = off2pos(curbuf, off);
1625 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001627 if (pos->lnum <= 0)
1628 lnum_start = 1;
1629 else
1630 lnum_start = pos->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 }
1632 else
1633 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001634 /* If the given position is not found, assume we want
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 * the end of the file. See setLocAndSize HACK. */
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001636 if (buf_was_empty)
1637 lnum_start = 1; /* above empty line */
1638 else
1639 lnum_start = curbuf->b_ml.ml_line_count + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001640 }
1641
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001642 /* "lnum" is the line where we insert: either append to it or
1643 * insert a new line above it. */
1644 lnum = lnum_start;
1645
1646 /* Loop over the "\n" separated lines of the argument. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 doupdate = 1;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001648 while (*args != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001650 nl = vim_strchr(args, '\n');
1651 if (nl == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001653 /* Incomplete line, probably truncated. Next "insert"
1654 * command should append to this one. */
1655 len = STRLEN(args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001657 else
1658 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001659 len = nl - args;
1660
1661 /*
1662 * We need to detect EOL style, because the commands
1663 * use a character offset.
1664 */
1665 if (nl > args && nl[-1] == '\r')
1666 {
1667 ff_detected = EOL_DOS;
1668 --len;
1669 }
1670 else
1671 ff_detected = EOL_UNIX;
1672 }
1673 args[len] = NUL;
1674
1675 if (lnum == lnum_start
1676 && ((pos != NULL && pos->col > 0)
1677 || (lnum == 1 && buf_was_empty)))
1678 {
1679 char_u *oldline = ml_get(lnum);
1680 char_u *newline;
1681
1682 /* Insert halfway a line. For simplicity we assume we
1683 * need to append to the line. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001684 newline = alloc_check((unsigned)(STRLEN(oldline) + len + 1));
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001685 if (newline != NULL)
1686 {
1687 STRCPY(newline, oldline);
1688 STRCAT(newline, args);
1689 ml_replace(lnum, newline, FALSE);
1690 }
1691 }
1692 else
1693 {
1694 /* Append a new line. Not that we always do this,
1695 * also when the text doesn't end in a "\n". */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001696 ml_append((linenr_T)(lnum - 1), args, (colnr_T)(len + 1), FALSE);
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001697 ++added;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001698 }
1699
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001700 if (nl == NULL)
1701 break;
1702 ++lnum;
1703 args = nl + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001704 }
1705
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001706 /* Adjust the marks below the inserted lines. */
1707 appended_lines_mark(lnum_start - 1, (long)added);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001709 /*
1710 * When starting with an empty buffer set the fileformat.
1711 * This is just guessing...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 */
1713 if (buf_was_empty)
1714 {
1715 if (ff_detected == EOL_UNKNOWN)
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001716#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 ff_detected = EOL_DOS;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001718#else
1719 ff_detected = EOL_UNIX;
1720#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 set_fileformat(ff_detected, OPT_LOCAL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00001722 curbuf->b_start_ffc = *curbuf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 }
1724
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 /*
1726 * XXX - GRP - Is the next line right? If I've inserted
1727 * text the buffer has been updated but not written. Will
1728 * netbeans guarantee to write it? Even if I do a :q! ?
1729 */
Bram Moolenaara3227e22006-03-08 21:32:40 +00001730 curbuf->b_changed = old_b_changed; /* logically unchanged */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 netbeansFireChanges = oldFire;
1732
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001733 /* Undo info is invalid now... */
Bram Moolenaara3227e22006-03-08 21:32:40 +00001734 u_blockfree(curbuf);
1735 u_clearall(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 }
1737 vim_free(to_free);
1738 nb_reply_nil(cmdno); /* or !error */
1739 }
1740 else
1741 {
1742 nbdebug(("UNIMPLEMENTED FUNCTION: %s\n", cmd));
1743 nb_reply_nil(cmdno);
1744 retval = FAIL;
1745 }
1746 }
1747 else /* Not a function; no reply required. */
1748 {
1749/* =====================================================================*/
1750 if (streq((char *)cmd, "create"))
1751 {
1752 /* Create a buffer without a name. */
1753 if (buf == NULL)
1754 {
1755 EMSG("E636: null buf in create");
1756 return FAIL;
1757 }
1758 vim_free(buf->displayname);
1759 buf->displayname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760
1761 netbeansReadFile = 0; /* don't try to open disk file */
1762 do_ecmd(0, NULL, 0, 0, ECMD_ONE, ECMD_HIDE + ECMD_OLDBUF);
1763 netbeansReadFile = 1;
1764 buf->bufp = curbuf;
1765 maketitle();
Bram Moolenaar009b2592004-10-24 19:18:58 +00001766 buf->insertDone = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 gui_update_menus(0);
1768/* =====================================================================*/
1769 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001770 else if (streq((char *)cmd, "insertDone"))
1771 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001772 if (buf == NULL || buf->bufp == NULL)
1773 {
1774 nbdebug((" null bufp in insertDone"));
1775 }
1776 else
1777 {
1778 buf->bufp->b_start_eol = *args == 'T';
1779 buf->insertDone = TRUE;
1780 args += 2;
1781 buf->bufp->b_p_ro = *args == 'T';
1782 print_read_msg(buf);
1783 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001784/* =====================================================================*/
1785 }
1786 else if (streq((char *)cmd, "saveDone"))
1787 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001788 long savedChars = atol((char *)args);
1789
1790 if (buf == NULL || buf->bufp == NULL)
1791 {
1792 nbdebug((" null bufp in saveDone"));
1793 }
1794 else
1795 print_save_msg(buf, savedChars);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001796/* =====================================================================*/
1797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 else if (streq((char *)cmd, "startDocumentListen"))
1799 {
1800 if (buf == NULL)
1801 {
1802 EMSG("E637: null buf in startDocumentListen");
1803 return FAIL;
1804 }
1805 buf->fireChanges = 1;
1806/* =====================================================================*/
1807 }
1808 else if (streq((char *)cmd, "stopDocumentListen"))
1809 {
1810 if (buf == NULL)
1811 {
1812 EMSG("E638: null buf in stopDocumentListen");
1813 return FAIL;
1814 }
1815 buf->fireChanges = 0;
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001816 if (buf->bufp != NULL && buf->bufp->b_was_netbeans_file)
Bram Moolenaar009b2592004-10-24 19:18:58 +00001817 {
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001818 if (!buf->bufp->b_netbeans_file)
Bram Moolenaar009b2592004-10-24 19:18:58 +00001819 EMSGN(_("E658: NetBeans connection lost for buffer %ld"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 buf->bufp->b_fnum);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001821 else
1822 {
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001823 /* NetBeans uses stopDocumentListen when it stops editing
1824 * a file. It then expects the buffer in Vim to
1825 * disappear. */
1826 do_bufdel(DOBUF_DEL, (char_u *)"", 1,
1827 buf->bufp->b_fnum, buf->bufp->b_fnum, TRUE);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001828 vim_memset(buf, 0, sizeof(nbbuf_T));
1829 }
1830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831/* =====================================================================*/
1832 }
1833 else if (streq((char *)cmd, "setTitle"))
1834 {
1835 if (buf == NULL)
1836 {
1837 EMSG("E639: null buf in setTitle");
1838 return FAIL;
1839 }
1840 vim_free(buf->displayname);
1841 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842/* =====================================================================*/
1843 }
1844 else if (streq((char *)cmd, "initDone"))
1845 {
1846 if (buf == NULL || buf->bufp == NULL)
1847 {
1848 EMSG("E640: null buf in initDone");
1849 return FAIL;
1850 }
1851 doupdate = 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001852 buf->initDone = TRUE;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001853 nb_set_curbuf(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854#if defined(FEAT_AUTOCMD)
1855 apply_autocmds(EVENT_BUFREADPOST, 0, 0, FALSE, buf->bufp);
1856#endif
1857
1858 /* handle any postponed key commands */
1859 handle_key_queue();
1860/* =====================================================================*/
1861 }
1862 else if (streq((char *)cmd, "setBufferNumber")
1863 || streq((char *)cmd, "putBufferNumber"))
1864 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00001865 char_u *path;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 buf_T *bufp;
1867
1868 if (buf == NULL)
1869 {
1870 EMSG("E641: null buf in setBufferNumber");
1871 return FAIL;
1872 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001873 path = (char_u *)nb_unquote(args, NULL);
1874 if (path == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 return FAIL;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001876 bufp = buflist_findname(path);
1877 vim_free(path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 if (bufp == NULL)
1879 {
1880 EMSG2("E642: File %s not found in setBufferNumber", args);
1881 return FAIL;
1882 }
1883 buf->bufp = bufp;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001884 buf->nbbuf_number = bufp->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885
1886 /* "setBufferNumber" has the side effect of jumping to the buffer
1887 * (don't know why!). Don't do that for "putBufferNumber". */
1888 if (*cmd != 'p')
1889 coloncmd(":buffer %d", bufp->b_fnum);
1890 else
1891 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00001892 buf->initDone = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893
1894 /* handle any postponed key commands */
1895 handle_key_queue();
1896 }
1897
1898#if 0 /* never used */
1899 buf->internalname = (char *)alloc_clear(8);
1900 sprintf(buf->internalname, "<%d>", bufno);
1901 buf->netbeansOwns = 0;
1902#endif
1903/* =====================================================================*/
1904 }
1905 else if (streq((char *)cmd, "setFullName"))
1906 {
1907 if (buf == NULL)
1908 {
1909 EMSG("E643: null buf in setFullName");
1910 return FAIL;
1911 }
1912 vim_free(buf->displayname);
1913 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914
1915 netbeansReadFile = 0; /* don't try to open disk file */
1916 do_ecmd(0, (char_u *)buf->displayname, 0, 0, ECMD_ONE,
1917 ECMD_HIDE + ECMD_OLDBUF);
1918 netbeansReadFile = 1;
1919 buf->bufp = curbuf;
1920 maketitle();
1921 gui_update_menus(0);
1922/* =====================================================================*/
1923 }
1924 else if (streq((char *)cmd, "editFile"))
1925 {
1926 if (buf == NULL)
1927 {
1928 EMSG("E644: null buf in editFile");
1929 return FAIL;
1930 }
1931 /* Edit a file: like create + setFullName + read the file. */
1932 vim_free(buf->displayname);
1933 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 do_ecmd(0, (char_u *)buf->displayname, NULL, NULL, ECMD_ONE,
1935 ECMD_HIDE + ECMD_OLDBUF);
1936 buf->bufp = curbuf;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001937 buf->initDone = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 doupdate = 1;
1939#if defined(FEAT_TITLE)
1940 maketitle();
1941#endif
1942 gui_update_menus(0);
1943/* =====================================================================*/
1944 }
1945 else if (streq((char *)cmd, "setVisible"))
1946 {
1947 if (buf == NULL || buf->bufp == NULL)
1948 {
1949/* EMSG("E645: null bufp in setVisible"); */
1950 return FAIL;
1951 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001952 if (streq((char *)args, "T") && buf->bufp != curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 {
1954 exarg_T exarg;
1955 exarg.cmd = (char_u *)"goto";
1956 exarg.forceit = FALSE;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001957 dosetvisible = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 goto_buffer(&exarg, DOBUF_FIRST, FORWARD, buf->bufp->b_fnum);
1959 doupdate = 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001960 dosetvisible = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961
1962 /* Side effect!!!. */
1963 if (!gui.starting)
1964 gui_mch_set_foreground();
1965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966/* =====================================================================*/
1967 }
1968 else if (streq((char *)cmd, "raise"))
1969 {
1970 /* Bring gvim to the foreground. */
1971 if (!gui.starting)
1972 gui_mch_set_foreground();
1973/* =====================================================================*/
1974 }
1975 else if (streq((char *)cmd, "setModified"))
1976 {
1977 if (buf == NULL || buf->bufp == NULL)
1978 {
1979/* EMSG("E646: null bufp in setModified"); */
1980 return FAIL;
1981 }
1982 if (streq((char *)args, "T"))
1983 buf->bufp->b_changed = 1;
1984 else
1985 {
1986 struct stat st;
1987
1988 /* Assume NetBeans stored the file. Reset the timestamp to
1989 * avoid "file changed" warnings. */
1990 if (buf->bufp->b_ffname != NULL
1991 && mch_stat((char *)buf->bufp->b_ffname, &st) >= 0)
1992 buf_store_time(buf->bufp, &st, buf->bufp->b_ffname);
1993 buf->bufp->b_changed = 0;
1994 }
1995 buf->modified = buf->bufp->b_changed;
1996/* =====================================================================*/
1997 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001998 else if (streq((char *)cmd, "setModtime"))
1999 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002000 if (buf == NULL || buf->bufp == NULL)
2001 nbdebug((" null bufp in setModtime"));
2002 else
2003 buf->bufp->b_mtime = atoi((char *)args);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002004/* =====================================================================*/
2005 }
2006 else if (streq((char *)cmd, "setReadOnly"))
2007 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002008 if (buf == NULL || buf->bufp == NULL)
2009 nbdebug((" null bufp in setReadOnly"));
2010 else if (streq((char *)args, "T"))
Bram Moolenaar009b2592004-10-24 19:18:58 +00002011 buf->bufp->b_p_ro = TRUE;
2012 else
2013 buf->bufp->b_p_ro = FALSE;
2014/* =====================================================================*/
2015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 else if (streq((char *)cmd, "setMark"))
2017 {
2018 /* not yet */
2019/* =====================================================================*/
2020 }
2021 else if (streq((char *)cmd, "showBalloon"))
2022 {
2023#if defined(FEAT_BEVAL)
2024 static char *text = NULL;
2025
2026 /*
2027 * Set up the Balloon Expression Evaluation area.
2028 * Ignore 'ballooneval' here.
2029 * The text pointer must remain valid for a while.
2030 */
2031 if (balloonEval != NULL)
2032 {
2033 vim_free(text);
2034 text = nb_unquote(args, NULL);
2035 if (text != NULL)
2036 gui_mch_post_balloon(balloonEval, (char_u *)text);
2037 }
2038#endif
2039/* =====================================================================*/
2040 }
2041 else if (streq((char *)cmd, "setDot"))
2042 {
2043 pos_T *pos;
2044#ifdef NBDEBUG
2045 char_u *s;
2046#endif
2047
2048 if (buf == NULL || buf->bufp == NULL)
2049 {
2050 EMSG("E647: null bufp in setDot");
2051 return FAIL;
2052 }
2053
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002054 nb_set_curbuf(buf->bufp);
2055
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056#ifdef FEAT_VISUAL
2057 /* Don't want Visual mode now. */
2058 if (VIsual_active)
2059 end_visual_mode();
2060#endif
2061#ifdef NBDEBUG
2062 s = args;
2063#endif
2064 pos = get_off_or_lnum(buf->bufp, &args);
2065 if (pos)
2066 {
2067 curwin->w_cursor = *pos;
2068 check_cursor();
2069#ifdef FEAT_FOLDING
2070 foldOpenCursor();
2071#endif
2072 }
2073 else
2074 nbdebug((" BAD POSITION in setDot: %s\n", s));
2075
2076 /* gui_update_cursor(TRUE, FALSE); */
2077 /* update_curbuf(NOT_VALID); */
2078 update_topline(); /* scroll to show the line */
2079 update_screen(VALID);
2080 setcursor();
2081 out_flush();
2082 gui_update_cursor(TRUE, FALSE);
2083 gui_mch_flush();
2084 /* Quit a hit-return or more prompt. */
2085 if (State == HITRETURN || State == ASKMORE)
2086 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087#ifdef FEAT_GUI_GTK
2088 if (gtk_main_level() > 0)
2089 gtk_main_quit();
2090#endif
2091 }
2092/* =====================================================================*/
2093 }
2094 else if (streq((char *)cmd, "close"))
2095 {
2096#ifdef NBDEBUG
2097 char *name = "<NONE>";
2098#endif
2099
2100 if (buf == NULL)
2101 {
2102 EMSG("E648: null buf in close");
2103 return FAIL;
2104 }
2105
2106#ifdef NBDEBUG
2107 if (buf->displayname != NULL)
2108 name = buf->displayname;
2109#endif
2110/* if (buf->bufp == NULL) */
2111/* EMSG("E649: null bufp in close"); */
2112 nbdebug((" CLOSE %d: %s\n", bufno, name));
2113 need_mouse_correct = TRUE;
2114 if (buf->bufp != NULL)
2115 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD,
2116 buf->bufp->b_fnum, TRUE);
Bram Moolenaar8d602722006-08-08 19:34:19 +00002117 buf->bufp = NULL;
2118 buf->initDone = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 doupdate = 1;
2120/* =====================================================================*/
2121 }
2122 else if (streq((char *)cmd, "setStyle")) /* obsolete... */
2123 {
2124 nbdebug((" setStyle is obsolete!"));
2125/* =====================================================================*/
2126 }
2127 else if (streq((char *)cmd, "setExitDelay"))
2128 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002129 /* Only used in version 2.1. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130/* =====================================================================*/
2131 }
2132 else if (streq((char *)cmd, "defineAnnoType"))
2133 {
2134#ifdef FEAT_SIGNS
2135 int typeNum;
2136 char_u *typeName;
2137 char_u *tooltip;
2138 char_u *p;
2139 char_u *glyphFile;
2140 int use_fg = 0;
2141 int use_bg = 0;
2142 int fg = -1;
2143 int bg = -1;
2144
2145 if (buf == NULL)
2146 {
2147 EMSG("E650: null buf in defineAnnoType");
2148 return FAIL;
2149 }
2150
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002151 cp = (char *)args;
2152 typeNum = strtol(cp, &cp, 10);
2153 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 args = skipwhite(args);
2155 typeName = (char_u *)nb_unquote(args, &args);
2156 args = skipwhite(args + 1);
2157 tooltip = (char_u *)nb_unquote(args, &args);
2158 args = skipwhite(args + 1);
2159
2160 p = (char_u *)nb_unquote(args, &args);
2161 glyphFile = vim_strsave_escaped(p, escape_chars);
2162 vim_free(p);
2163
2164 args = skipwhite(args + 1);
2165 if (STRNCMP(args, "none", 4) == 0)
2166 args += 5;
2167 else
2168 {
2169 use_fg = 1;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002170 cp = (char *)args;
2171 fg = strtol(cp, &cp, 10);
2172 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 }
2174 if (STRNCMP(args, "none", 4) == 0)
2175 args += 5;
2176 else
2177 {
2178 use_bg = 1;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002179 cp = (char *)args;
2180 bg = strtol(cp, &cp, 10);
2181 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 }
2183 if (typeName != NULL && tooltip != NULL && glyphFile != NULL)
2184 addsigntype(buf, typeNum, typeName, tooltip, glyphFile,
2185 use_fg, fg, use_bg, bg);
2186 else
2187 vim_free(typeName);
2188
2189 /* don't free typeName; it's used directly in addsigntype() */
2190 vim_free(tooltip);
2191 vim_free(glyphFile);
2192
2193#endif
2194/* =====================================================================*/
2195 }
2196 else if (streq((char *)cmd, "addAnno"))
2197 {
2198#ifdef FEAT_SIGNS
2199 int serNum;
2200 int localTypeNum;
2201 int typeNum;
2202# ifdef NBDEBUG
2203 int len;
2204# endif
2205 pos_T *pos;
2206
2207 if (buf == NULL || buf->bufp == NULL)
2208 {
2209 EMSG("E651: null bufp in addAnno");
2210 return FAIL;
2211 }
2212
2213 doupdate = 1;
2214
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002215 cp = (char *)args;
2216 serNum = strtol(cp, &cp, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217
2218 /* Get the typenr specific for this buffer and convert it to
2219 * the global typenumber, as used for the sign name. */
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002220 localTypeNum = strtol(cp, &cp, 10);
2221 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 typeNum = mapsigntype(buf, localTypeNum);
2223
2224 pos = get_off_or_lnum(buf->bufp, &args);
2225
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002226 cp = (char *)args;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227# ifdef NBDEBUG
2228 len =
2229# endif
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002230 strtol(cp, &cp, 10);
2231 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232# ifdef NBDEBUG
2233 if (len != -1)
2234 {
2235 nbdebug((" partial line annotation -- Not Yet Implemented!"));
2236 }
2237# endif
2238 if (serNum >= GUARDEDOFFSET)
2239 {
2240 nbdebug((" too many annotations! ignoring..."));
2241 return FAIL;
2242 }
2243 if (pos)
2244 {
2245 coloncmd(":sign place %d line=%d name=%d buffer=%d",
2246 serNum, pos->lnum, typeNum, buf->bufp->b_fnum);
2247 if (typeNum == curPCtype)
2248 coloncmd(":sign jump %d buffer=%d", serNum,
2249 buf->bufp->b_fnum);
2250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251#endif
2252/* =====================================================================*/
2253 }
2254 else if (streq((char *)cmd, "removeAnno"))
2255 {
2256#ifdef FEAT_SIGNS
2257 int serNum;
2258
2259 if (buf == NULL || buf->bufp == NULL)
2260 {
2261 nbdebug((" null bufp in removeAnno"));
2262 return FAIL;
2263 }
2264 doupdate = 1;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002265 cp = (char *)args;
2266 serNum = strtol(cp, &cp, 10);
2267 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 coloncmd(":sign unplace %d buffer=%d",
2269 serNum, buf->bufp->b_fnum);
2270 redraw_buf_later(buf->bufp, NOT_VALID);
2271#endif
2272/* =====================================================================*/
2273 }
2274 else if (streq((char *)cmd, "moveAnnoToFront"))
2275 {
2276#ifdef FEAT_SIGNS
2277 nbdebug((" moveAnnoToFront: Not Yet Implemented!"));
2278#endif
2279/* =====================================================================*/
2280 }
2281 else if (streq((char *)cmd, "guard") || streq((char *)cmd, "unguard"))
2282 {
2283 int len;
2284 pos_T first;
2285 pos_T last;
2286 pos_T *pos;
2287 int un = (cmd[0] == 'u');
2288 static int guardId = GUARDEDOFFSET;
2289
2290 if (skip >= SKIP_STOP)
2291 {
2292 nbdebug((" Skipping %s command\n", (char *) cmd));
2293 return OK;
2294 }
2295
2296 nb_init_graphics();
2297
2298 if (buf == NULL || buf->bufp == NULL)
2299 {
2300 nbdebug((" null bufp in %s command", cmd));
2301 return FAIL;
2302 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002303 nb_set_curbuf(buf->bufp);
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002304 cp = (char *)args;
2305 off = strtol(cp, &cp, 10);
2306 len = strtol(cp, NULL, 10);
2307 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 pos = off2pos(buf->bufp, off);
2309 doupdate = 1;
2310 if (!pos)
2311 nbdebug((" no such start pos in %s, %ld\n", cmd, off));
2312 else
2313 {
2314 first = *pos;
2315 pos = off2pos(buf->bufp, off + len - 1);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002316 if (pos != NULL && pos->col == 0)
2317 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 /*
2319 * In Java Swing the offset is a position between 2
2320 * characters. If col == 0 then we really want the
2321 * previous line as the end.
2322 */
2323 pos = off2pos(buf->bufp, off + len - 2);
2324 }
2325 if (!pos)
2326 nbdebug((" no such end pos in %s, %ld\n",
2327 cmd, off + len - 1));
2328 else
2329 {
2330 long lnum;
2331 last = *pos;
2332 /* set highlight for region */
2333 nbdebug((" %sGUARD %ld,%d to %ld,%d\n", (un) ? "UN" : "",
2334 first.lnum, first.col,
2335 last.lnum, last.col));
2336#ifdef FEAT_SIGNS
2337 for (lnum = first.lnum; lnum <= last.lnum; lnum++)
2338 {
2339 if (un)
2340 {
2341 /* never used */
2342 }
2343 else
2344 {
2345 if (buf_findsigntype_id(buf->bufp, lnum,
2346 GUARDED) == 0)
2347 {
2348 coloncmd(
2349 ":sign place %d line=%d name=%d buffer=%d",
2350 guardId++, lnum, GUARDED,
2351 buf->bufp->b_fnum);
2352 }
2353 }
2354 }
2355#endif
2356 redraw_buf_later(buf->bufp, NOT_VALID);
2357 }
2358 }
2359/* =====================================================================*/
2360 }
2361 else if (streq((char *)cmd, "startAtomic"))
2362 {
2363 inAtomic = 1;
2364/* =====================================================================*/
2365 }
2366 else if (streq((char *)cmd, "endAtomic"))
2367 {
2368 inAtomic = 0;
2369 if (needupdate)
2370 {
2371 doupdate = 1;
2372 needupdate = 0;
2373 }
2374/* =====================================================================*/
2375 }
2376 else if (streq((char *)cmd, "save"))
2377 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002378 /*
2379 * NOTE - This command is obsolete wrt NetBeans. Its left in
2380 * only for historical reasons.
2381 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 if (buf == NULL || buf->bufp == NULL)
2383 {
2384 nbdebug((" null bufp in %s command", cmd));
2385 return FAIL;
2386 }
2387
2388 /* the following is taken from ex_cmds.c (do_wqall function) */
2389 if (bufIsChanged(buf->bufp))
2390 {
2391 /* Only write if the buffer can be written. */
2392 if (p_write
2393 && !buf->bufp->b_p_ro
2394 && buf->bufp->b_ffname != NULL
2395#ifdef FEAT_QUICKFIX
2396 && !bt_dontwrite(buf->bufp)
2397#endif
2398 )
2399 {
2400 buf_write_all(buf->bufp, FALSE);
2401#ifdef FEAT_AUTOCMD
2402 /* an autocommand may have deleted the buffer */
2403 if (!buf_valid(buf->bufp))
2404 buf->bufp = NULL;
2405#endif
2406 }
2407 }
2408/* =====================================================================*/
2409 }
2410 else if (streq((char *)cmd, "netbeansBuffer"))
2411 {
2412 if (buf == NULL || buf->bufp == NULL)
2413 {
2414 nbdebug((" null bufp in %s command", cmd));
2415 return FAIL;
2416 }
2417 if (*args == 'T')
2418 {
2419 buf->bufp->b_netbeans_file = TRUE;
2420 buf->bufp->b_was_netbeans_file = TRUE;
2421 }
2422 else
2423 buf->bufp->b_netbeans_file = FALSE;
2424/* =====================================================================*/
2425 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002426 else if (streq((char *)cmd, "specialKeys"))
2427 {
2428 special_keys(args);
2429/* =====================================================================*/
2430 }
2431 else if (streq((char *)cmd, "actionMenuItem"))
2432 {
2433 /* not used yet */
2434/* =====================================================================*/
2435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 else if (streq((char *)cmd, "version"))
2437 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002438 /* not used yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 }
2440 /*
2441 * Unrecognized command is ignored.
2442 */
2443 }
2444 if (inAtomic && doupdate)
2445 {
2446 needupdate = 1;
2447 doupdate = 0;
2448 }
2449
Bram Moolenaar009b2592004-10-24 19:18:58 +00002450 /*
2451 * Is this needed? I moved the netbeans_Xt_connect() later during startup
2452 * and it may no longer be necessary. If its not needed then needupdate
2453 * and doupdate can also be removed.
2454 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 if (buf != NULL && buf->initDone && doupdate)
2456 {
2457 update_screen(NOT_VALID);
2458 setcursor();
2459 out_flush();
2460 gui_update_cursor(TRUE, FALSE);
2461 gui_mch_flush();
2462 /* Quit a hit-return or more prompt. */
2463 if (State == HITRETURN || State == ASKMORE)
2464 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465#ifdef FEAT_GUI_GTK
2466 if (gtk_main_level() > 0)
2467 gtk_main_quit();
2468#endif
2469 }
2470 }
2471
2472 return retval;
2473}
2474
2475
2476/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002477 * If "buf" is not the current buffer try changing to a window that edits this
2478 * buffer. If there is no such window then close the current buffer and set
2479 * the current buffer as "buf".
2480 */
2481 static void
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00002482nb_set_curbuf(buf_T *buf)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002483{
2484 if (curbuf != buf && buf_jump_open_win(buf) == NULL)
2485 set_curbuf(buf, DOBUF_GOTO);
2486}
2487
2488/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 * Process a vim colon command.
2490 */
2491 static void
2492coloncmd(char *cmd, ...)
2493{
2494 char buf[1024];
2495 va_list ap;
2496
2497 va_start(ap, cmd);
2498 vsprintf(buf, cmd, ap);
2499 va_end(ap);
2500
2501 nbdebug((" COLONCMD %s\n", buf));
2502
2503/* ALT_INPUT_LOCK_ON; */
2504 do_cmdline((char_u *)buf, NULL, NULL, DOCMD_NOWAIT | DOCMD_KEYTYPED);
2505/* ALT_INPUT_LOCK_OFF; */
2506
2507 setcursor(); /* restore the cursor position */
2508 out_flush(); /* make sure output has been written */
2509
2510 gui_update_cursor(TRUE, FALSE);
2511 gui_mch_flush();
2512}
2513
2514
2515/*
Bram Moolenaar009b2592004-10-24 19:18:58 +00002516 * Parse the specialKeys argument and issue the appropriate map commands.
2517 */
2518 static void
2519special_keys(char_u *args)
2520{
2521 char *save_str = nb_unquote(args, NULL);
2522 char *tok = strtok(save_str, " ");
2523 char *sep;
2524 char keybuf[64];
2525 char cmdbuf[256];
2526
2527 while (tok != NULL)
2528 {
2529 int i = 0;
2530
2531 if ((sep = strchr(tok, '-')) != NULL)
2532 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002533 *sep = NUL;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002534 while (*tok)
2535 {
2536 switch (*tok)
2537 {
2538 case 'A':
2539 case 'M':
2540 case 'C':
2541 case 'S':
2542 keybuf[i++] = *tok;
2543 keybuf[i++] = '-';
2544 break;
2545 }
2546 tok++;
2547 }
2548 tok++;
2549 }
2550
2551 strcpy(&keybuf[i], tok);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002552 vim_snprintf(cmdbuf, sizeof(cmdbuf),
2553 "<silent><%s> :nbkey %s<CR>", keybuf, keybuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002554 do_map(0, (char_u *)cmdbuf, NORMAL, FALSE);
2555 tok = strtok(NULL, " ");
2556 }
2557 vim_free(save_str);
2558}
2559
2560
2561 void
2562ex_nbkey(eap)
2563 exarg_T *eap;
2564{
2565 netbeans_keystring(0, (char *)eap->arg);
2566}
2567
2568
2569/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 * Initialize highlights and signs for use by netbeans (mostly obsolete)
2571 */
2572 static void
2573nb_init_graphics(void)
2574{
2575 static int did_init = FALSE;
2576
2577 if (!did_init)
2578 {
2579 coloncmd(":highlight NBGuarded guibg=Cyan guifg=Black");
2580 coloncmd(":sign define %d linehl=NBGuarded", GUARDED);
2581
2582 did_init = TRUE;
2583 }
2584}
2585
2586/*
2587 * Convert key to netbeans name.
2588 */
2589 static void
2590netbeans_keyname(int key, char *buf)
2591{
2592 char *name = 0;
2593 char namebuf[2];
2594 int ctrl = 0;
2595 int shift = 0;
2596 int alt = 0;
2597
2598 if (mod_mask & MOD_MASK_CTRL)
2599 ctrl = 1;
2600 if (mod_mask & MOD_MASK_SHIFT)
2601 shift = 1;
2602 if (mod_mask & MOD_MASK_ALT)
2603 alt = 1;
2604
2605
2606 switch (key)
2607 {
2608 case K_F1: name = "F1"; break;
2609 case K_S_F1: name = "F1"; shift = 1; break;
2610 case K_F2: name = "F2"; break;
2611 case K_S_F2: name = "F2"; shift = 1; break;
2612 case K_F3: name = "F3"; break;
2613 case K_S_F3: name = "F3"; shift = 1; break;
2614 case K_F4: name = "F4"; break;
2615 case K_S_F4: name = "F4"; shift = 1; break;
2616 case K_F5: name = "F5"; break;
2617 case K_S_F5: name = "F5"; shift = 1; break;
2618 case K_F6: name = "F6"; break;
2619 case K_S_F6: name = "F6"; shift = 1; break;
2620 case K_F7: name = "F7"; break;
2621 case K_S_F7: name = "F7"; shift = 1; break;
2622 case K_F8: name = "F8"; break;
2623 case K_S_F8: name = "F8"; shift = 1; break;
2624 case K_F9: name = "F9"; break;
2625 case K_S_F9: name = "F9"; shift = 1; break;
2626 case K_F10: name = "F10"; break;
2627 case K_S_F10: name = "F10"; shift = 1; break;
2628 case K_F11: name = "F11"; break;
2629 case K_S_F11: name = "F11"; shift = 1; break;
2630 case K_F12: name = "F12"; break;
2631 case K_S_F12: name = "F12"; shift = 1; break;
2632 default:
2633 if (key >= ' ' && key <= '~')
2634 {
2635 /* Allow ASCII characters. */
2636 name = namebuf;
2637 namebuf[0] = key;
2638 namebuf[1] = NUL;
2639 }
2640 else
2641 name = "X";
2642 break;
2643 }
2644
2645 buf[0] = '\0';
2646 if (ctrl)
2647 strcat(buf, "C");
2648 if (shift)
2649 strcat(buf, "S");
2650 if (alt)
2651 strcat(buf, "M"); /* META */
2652 if (ctrl || shift || alt)
2653 strcat(buf, "-");
2654 strcat(buf, name);
2655}
2656
2657#ifdef FEAT_BEVAL
2658/*
2659 * Function to be called for balloon evaluation. Grabs the text under the
2660 * cursor and sends it to the debugger for evaluation. The debugger should
2661 * respond with a showBalloon command when there is a useful result.
2662 */
2663/*ARGSUSED*/
Bram Moolenaard62bec82005-03-07 22:56:57 +00002664 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665netbeans_beval_cb(
2666 BalloonEval *beval,
2667 int state)
2668{
Bram Moolenaard62bec82005-03-07 22:56:57 +00002669 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 char_u *text;
Bram Moolenaard62bec82005-03-07 22:56:57 +00002671 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 int col;
2673 char buf[MAXPATHL * 2 + 25];
2674 char_u *p;
2675
2676 /* Don't do anything when 'ballooneval' is off, messages scrolled the
2677 * windows up or we have no connection. */
2678 if (!p_beval || msg_scrolled > 0 || !haveConnection)
2679 return;
2680
Bram Moolenaard62bec82005-03-07 22:56:57 +00002681 if (get_beval_info(beval, TRUE, &wp, &lnum, &text, &col) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 {
2683 /* Send debugger request. Only when the text is of reasonable
2684 * length. */
2685 if (text != NULL && text[0] != NUL && STRLEN(text) < MAXPATHL)
2686 {
2687 p = nb_quote(text);
2688 if (p != NULL)
Bram Moolenaar009b2592004-10-24 19:18:58 +00002689 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002690 vim_snprintf(buf, sizeof(buf),
Bram Moolenaar89d40322006-08-29 15:30:07 +00002691 "0:balloonText=%d \"%s\"\n", r_cmdno, p);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002692 vim_free(p);
2693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 nbdebug(("EVT: %s", buf));
2695 nb_send(buf, "netbeans_beval_cb");
2696 }
2697 vim_free(text);
2698 }
2699}
2700#endif
2701
2702/*
2703 * Tell netbeans that the window was opened, ready for commands.
2704 */
2705 void
2706netbeans_startup_done(void)
2707{
2708 char *cmd = "0:startupDone=0\n";
2709
Bram Moolenaar009b2592004-10-24 19:18:58 +00002710 if (usingNetbeans)
2711#ifdef FEAT_GUI_MOTIF
2712 netbeans_Xt_connect(app_context);
2713#else
2714# ifdef FEAT_GUI_GTK
2715 netbeans_gtk_connect();
Bram Moolenaardf177f62005-02-22 08:39:57 +00002716# else
2717# ifdef FEAT_GUI_W32
2718 netbeans_w32_connect();
2719# endif
Bram Moolenaar009b2592004-10-24 19:18:58 +00002720# endif
2721#endif
2722
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 if (!haveConnection)
2724 return;
2725
Bram Moolenaard62bec82005-03-07 22:56:57 +00002726#ifdef FEAT_BEVAL
2727 bevalServers |= BEVAL_NETBEANS;
2728#endif
2729
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 nbdebug(("EVT: %s", cmd));
2731 nb_send(cmd, "netbeans_startup_done");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732}
2733
Bram Moolenaar009b2592004-10-24 19:18:58 +00002734/*
2735 * Tell netbeans that we're exiting. This should be called right
2736 * before calling exit.
2737 */
2738 void
2739netbeans_send_disconnect()
2740{
2741 char buf[128];
2742
2743 if (haveConnection)
2744 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002745 sprintf(buf, "0:disconnect=%d\n", r_cmdno);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002746 nbdebug(("EVT: %s", buf));
2747 nb_send(buf, "netbeans_disconnect");
2748 }
2749}
2750
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_W32) || defined(PROTO)
2752/*
2753 * Tell netbeans that the window was moved or resized.
2754 */
2755 void
2756netbeans_frame_moved(int new_x, int new_y)
2757{
2758 char buf[128];
2759
2760 if (!haveConnection)
2761 return;
2762
2763 sprintf(buf, "0:geometry=%d %d %d %d %d\n",
Bram Moolenaar89d40322006-08-29 15:30:07 +00002764 r_cmdno, (int)Columns, (int)Rows, new_x, new_y);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002765 /*nbdebug(("EVT: %s", buf)); happens too many times during a move */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 nb_send(buf, "netbeans_frame_moved");
2767}
2768#endif
2769
2770/*
Bram Moolenaar009b2592004-10-24 19:18:58 +00002771 * Tell netbeans the user opened or activated a file.
2772 */
2773 void
2774netbeans_file_activated(buf_T *bufp)
2775{
2776 int bufno = nb_getbufno(bufp);
2777 nbbuf_T *bp = nb_get_buf(bufno);
2778 char buffer[2*MAXPATHL];
2779 char_u *q;
2780
2781 if (!haveConnection || dosetvisible)
2782 return;
2783
2784 q = nb_quote(bufp->b_ffname);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002785 if (q == NULL || bp == NULL)
Bram Moolenaar009b2592004-10-24 19:18:58 +00002786 return;
2787
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002788 vim_snprintf(buffer, sizeof(buffer), "%d:fileOpened=%d \"%s\" %s %s\n",
Bram Moolenaar009b2592004-10-24 19:18:58 +00002789 bufno,
2790 bufno,
2791 (char *)q,
2792 "T", /* open in NetBeans */
2793 "F"); /* modified */
2794
2795 vim_free(q);
2796 nbdebug(("EVT: %s", buffer));
2797
2798 nb_send(buffer, "netbeans_file_opened");
2799}
2800
2801/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 * Tell netbeans the user opened a file.
2803 */
2804 void
Bram Moolenaar009b2592004-10-24 19:18:58 +00002805netbeans_file_opened(buf_T *bufp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806{
Bram Moolenaar009b2592004-10-24 19:18:58 +00002807 int bufno = nb_getbufno(bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 char buffer[2*MAXPATHL];
2809 char_u *q;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002810 nbbuf_T *bp = nb_get_buf(nb_getbufno(bufp));
2811 int bnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812
2813 if (!haveConnection)
2814 return;
2815
Bram Moolenaar009b2592004-10-24 19:18:58 +00002816 q = nb_quote(bufp->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 if (q == NULL)
2818 return;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002819 if (bp != NULL)
2820 bnum = bufno;
2821 else
2822 bnum = 0;
2823
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002824 vim_snprintf(buffer, sizeof(buffer), "%d:fileOpened=%d \"%s\" %s %s\n",
Bram Moolenaar009b2592004-10-24 19:18:58 +00002825 bnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 0,
2827 (char *)q,
2828 "T", /* open in NetBeans */
2829 "F"); /* modified */
2830
2831 vim_free(q);
2832 nbdebug(("EVT: %s", buffer));
2833
2834 nb_send(buffer, "netbeans_file_opened");
Bram Moolenaar009b2592004-10-24 19:18:58 +00002835 if (p_acd && vim_chdirfile(bufp->b_ffname) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 shorten_fnames(TRUE);
2837}
2838
2839/*
2840 * Tell netbeans a file was closed.
2841 */
2842 void
2843netbeans_file_closed(buf_T *bufp)
2844{
2845 int bufno = nb_getbufno(bufp);
2846 nbbuf_T *nbbuf = nb_get_buf(bufno);
2847 char buffer[2*MAXPATHL];
2848
2849 if (!haveConnection || bufno < 0)
2850 return;
2851
2852 if (!netbeansCloseFile)
2853 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002854 nbdebug(("Ignoring file_closed for %s. File was closed from IDE\n",
2855 bufp->b_ffname));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 return;
2857 }
2858
Bram Moolenaar009b2592004-10-24 19:18:58 +00002859 nbdebug(("netbeans_file_closed:\n"));
2860 nbdebug((" Closing bufno: %d", bufno));
2861 if (curbuf != NULL && curbuf != bufp)
2862 {
2863 nbdebug((" Curbuf bufno: %d\n", nb_getbufno(curbuf)));
2864 }
2865 else if (curbuf == bufp)
2866 {
2867 nbdebug((" curbuf == bufp\n"));
2868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869
2870 if (bufno <= 0)
2871 return;
2872
Bram Moolenaar89d40322006-08-29 15:30:07 +00002873 sprintf(buffer, "%d:killed=%d\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874
2875 nbdebug(("EVT: %s", buffer));
2876
2877 nb_send(buffer, "netbeans_file_closed");
2878
2879 if (nbbuf != NULL)
2880 nbbuf->bufp = NULL;
2881}
2882
2883/*
2884 * Get a pointer to the Netbeans buffer for Vim buffer "bufp".
2885 * Return NULL if there is no such buffer or changes are not to be reported.
2886 * Otherwise store the buffer number in "*bufnop".
2887 */
2888 static nbbuf_T *
2889nb_bufp2nbbuf_fire(buf_T *bufp, int *bufnop)
2890{
2891 int bufno;
2892 nbbuf_T *nbbuf;
2893
2894 if (!haveConnection || !netbeansFireChanges)
2895 return NULL; /* changes are not reported at all */
2896
2897 bufno = nb_getbufno(bufp);
2898 if (bufno <= 0)
2899 return NULL; /* file is not known to NetBeans */
2900
2901 nbbuf = nb_get_buf(bufno);
2902 if (nbbuf != NULL && !nbbuf->fireChanges)
2903 return NULL; /* changes in this buffer are not reported */
2904
2905 *bufnop = bufno;
2906 return nbbuf;
2907}
2908
2909/*
2910 * Tell netbeans the user inserted some text.
2911 */
2912 void
2913netbeans_inserted(
2914 buf_T *bufp,
2915 linenr_T linenr,
2916 colnr_T col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 char_u *txt,
2918 int newlen)
2919{
2920 char_u *buf;
2921 int bufno;
2922 nbbuf_T *nbbuf;
2923 pos_T pos;
2924 long off;
2925 char_u *p;
2926 char_u *newtxt;
2927
2928 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
2929 if (nbbuf == NULL)
2930 return;
2931
Bram Moolenaar009b2592004-10-24 19:18:58 +00002932 /* Don't mark as modified for initial read */
2933 if (nbbuf->insertDone)
2934 nbbuf->modified = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935
2936 pos.lnum = linenr;
2937 pos.col = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 off = pos2off(bufp, &pos);
2939
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 /* send the "insert" EVT */
2941 newtxt = alloc(newlen + 1);
Bram Moolenaarb6356332005-07-18 21:40:44 +00002942 vim_strncpy(newtxt, txt, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 p = nb_quote(newtxt);
2944 if (p != NULL)
2945 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002946 buf = alloc(128 + 2*newlen);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002947 sprintf((char *)buf, "%d:insert=%d %ld \"%s\"\n",
2948 bufno, r_cmdno, off, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 nbdebug(("EVT: %s", buf));
2950 nb_send((char *)buf, "netbeans_inserted");
Bram Moolenaar009b2592004-10-24 19:18:58 +00002951 vim_free(p);
2952 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 vim_free(newtxt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955}
2956
2957/*
2958 * Tell netbeans some bytes have been removed.
2959 */
2960 void
2961netbeans_removed(
2962 buf_T *bufp,
2963 linenr_T linenr,
2964 colnr_T col,
2965 long len)
2966{
2967 char_u buf[128];
2968 int bufno;
2969 nbbuf_T *nbbuf;
2970 pos_T pos;
2971 long off;
2972
2973 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
2974 if (nbbuf == NULL)
2975 return;
2976
2977 if (len < 0)
2978 {
2979 nbdebug(("Negative len %ld in netbeans_removed()!", len));
2980 return;
2981 }
2982
2983 nbbuf->modified = 1;
2984
2985 pos.lnum = linenr;
2986 pos.col = col;
2987
2988 off = pos2off(bufp, &pos);
2989
Bram Moolenaar89d40322006-08-29 15:30:07 +00002990 sprintf((char *)buf, "%d:remove=%d %ld %ld\n", bufno, r_cmdno, off, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 nbdebug(("EVT: %s", buf));
2992 nb_send((char *)buf, "netbeans_removed");
2993}
2994
2995/*
2996 * Send netbeans an unmodufied command.
2997 */
2998/*ARGSUSED*/
2999 void
3000netbeans_unmodified(buf_T *bufp)
3001{
3002#if 0
3003 char_u buf[128];
3004 int bufno;
3005 nbbuf_T *nbbuf;
3006
3007 /* This has been disabled, because NetBeans considers a buffer modified
3008 * even when all changes have been undone. */
3009 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3010 if (nbbuf == NULL)
3011 return;
3012
3013 nbbuf->modified = 0;
3014
Bram Moolenaar89d40322006-08-29 15:30:07 +00003015 sprintf((char *)buf, "%d:unmodified=%d\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 nbdebug(("EVT: %s", buf));
3017 nb_send((char *)buf, "netbeans_unmodified");
3018#endif
3019}
3020
3021/*
3022 * Send a button release event back to netbeans. Its up to netbeans
3023 * to decide what to do (if anything) with this event.
3024 */
3025 void
3026netbeans_button_release(int button)
3027{
3028 char buf[128];
3029 int bufno;
3030
3031 bufno = nb_getbufno(curbuf);
3032
3033 if (bufno >= 0 && curwin != NULL && curwin->w_buffer == curbuf)
3034 {
Bram Moolenaarc92ad2e2005-01-19 22:08:28 +00003035 int col = mouse_col - W_WINCOL(curwin) - (curwin->w_p_nu ? 9 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 long off = pos2off(curbuf, &curwin->w_cursor);
3037
3038 /* sync the cursor position */
Bram Moolenaar89d40322006-08-29 15:30:07 +00003039 sprintf(buf, "%d:newDotAndMark=%d %ld %ld\n", bufno, r_cmdno, off, off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 nbdebug(("EVT: %s", buf));
3041 nb_send(buf, "netbeans_button_release[newDotAndMark]");
3042
Bram Moolenaar89d40322006-08-29 15:30:07 +00003043 sprintf(buf, "%d:buttonRelease=%d %d %ld %d\n", bufno, r_cmdno,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 button, (long)curwin->w_cursor.lnum, col);
3045 nbdebug(("EVT: %s", buf));
3046 nb_send(buf, "netbeans_button_release");
3047 }
3048}
3049
3050
3051/*
Bram Moolenaar143c38c2007-05-10 16:41:10 +00003052 * Send a keypress event back to netbeans. This usually simulates some
Bram Moolenaar009b2592004-10-24 19:18:58 +00003053 * kind of function key press. This function operates on a key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 */
3055 void
3056netbeans_keycommand(int key)
3057{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 char keyName[60];
Bram Moolenaar009b2592004-10-24 19:18:58 +00003059
3060 netbeans_keyname(key, keyName);
3061 netbeans_keystring(key, keyName);
3062}
3063
3064
3065/*
Bram Moolenaar143c38c2007-05-10 16:41:10 +00003066 * Send a keypress event back to netbeans. This usually simulates some
Bram Moolenaar009b2592004-10-24 19:18:58 +00003067 * kind of function key press. This function operates on a key string.
3068 */
3069 static void
3070netbeans_keystring(int key, char *keyName)
3071{
3072 char buf[2*MAXPATHL];
3073 int bufno = nb_getbufno(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 long off;
3075 char_u *q;
3076
3077 if (!haveConnection)
3078 return;
3079
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080
3081 if (bufno == -1)
3082 {
3083 nbdebug(("got keycommand for non-NetBeans buffer, opening...\n"));
3084 q = curbuf->b_ffname == NULL ? (char_u *)""
3085 : nb_quote(curbuf->b_ffname);
3086 if (q == NULL)
3087 return;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003088 vim_snprintf(buf, sizeof(buf), "0:fileOpened=%d \"%s\" %s %s\n", 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 q,
3090 "T", /* open in NetBeans */
3091 "F"); /* modified */
3092 if (curbuf->b_ffname != NULL)
3093 vim_free(q);
3094 nbdebug(("EVT: %s", buf));
3095 nb_send(buf, "netbeans_keycommand");
3096
Bram Moolenaar5b625c52005-01-31 18:55:55 +00003097 if (key > 0)
3098 postpone_keycommand(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 return;
3100 }
3101
3102 /* sync the cursor position */
3103 off = pos2off(curbuf, &curwin->w_cursor);
Bram Moolenaar89d40322006-08-29 15:30:07 +00003104 sprintf(buf, "%d:newDotAndMark=%d %ld %ld\n", bufno, r_cmdno, off, off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 nbdebug(("EVT: %s", buf));
3106 nb_send(buf, "netbeans_keycommand");
3107
3108 /* To work on Win32 you must apply patch to ExtEditor module
3109 * from ExtEdCaret.java.diff - make EVT_newDotAndMark handler
3110 * more synchronous
3111 */
3112
3113 /* now send keyCommand event */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003114 vim_snprintf(buf, sizeof(buf), "%d:keyCommand=%d \"%s\"\n",
Bram Moolenaar89d40322006-08-29 15:30:07 +00003115 bufno, r_cmdno, keyName);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 nbdebug(("EVT: %s", buf));
3117 nb_send(buf, "netbeans_keycommand");
3118
3119 /* New: do both at once and include the lnum/col. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003120 vim_snprintf(buf, sizeof(buf), "%d:keyAtPos=%d \"%s\" %ld %ld/%ld\n",
Bram Moolenaar89d40322006-08-29 15:30:07 +00003121 bufno, r_cmdno, keyName,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 off, (long)curwin->w_cursor.lnum, (long)curwin->w_cursor.col);
3123 nbdebug(("EVT: %s", buf));
3124 nb_send(buf, "netbeans_keycommand");
3125}
3126
3127
3128/*
3129 * Send a save event to netbeans.
3130 */
3131 void
3132netbeans_save_buffer(buf_T *bufp)
3133{
3134 char_u buf[64];
3135 int bufno;
3136 nbbuf_T *nbbuf;
3137
3138 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3139 if (nbbuf == NULL)
3140 return;
3141
3142 nbbuf->modified = 0;
3143
Bram Moolenaar89d40322006-08-29 15:30:07 +00003144 sprintf((char *)buf, "%d:save=%d\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 nbdebug(("EVT: %s", buf));
3146 nb_send((char *)buf, "netbeans_save_buffer");
3147}
3148
3149
3150/*
3151 * Send remove command to netbeans (this command has been turned off).
3152 */
3153 void
3154netbeans_deleted_all_lines(buf_T *bufp)
3155{
3156 char_u buf[64];
3157 int bufno;
3158 nbbuf_T *nbbuf;
3159
3160 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3161 if (nbbuf == NULL)
3162 return;
3163
Bram Moolenaar009b2592004-10-24 19:18:58 +00003164 /* Don't mark as modified for initial read */
3165 if (nbbuf->insertDone)
3166 nbbuf->modified = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167
Bram Moolenaar89d40322006-08-29 15:30:07 +00003168 sprintf((char *)buf, "%d:remove=%d 0 -1\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 nbdebug(("EVT(suppressed): %s", buf));
3170/* nb_send(buf, "netbeans_deleted_all_lines"); */
3171}
3172
3173
3174/*
3175 * See if the lines are guarded. The top and bot parameters are from
3176 * u_savecommon(), these are the line above the change and the line below the
3177 * change.
3178 */
3179 int
3180netbeans_is_guarded(linenr_T top, linenr_T bot)
3181{
3182 signlist_T *p;
3183 int lnum;
3184
3185 for (p = curbuf->b_signlist; p != NULL; p = p->next)
3186 if (p->id >= GUARDEDOFFSET)
3187 for (lnum = top + 1; lnum < bot; lnum++)
3188 if (lnum == p->lnum)
3189 return TRUE;
3190
3191 return FALSE;
3192}
3193
3194#if defined(FEAT_GUI_MOTIF) || defined(PROTO)
3195/*
3196 * We have multiple signs to draw at the same location. Draw the
3197 * multi-sign indicator instead. This is the Motif version.
3198 */
3199 void
3200netbeans_draw_multisign_indicator(int row)
3201{
3202 int i;
3203 int y;
3204 int x;
3205
3206 x = 0;
3207 y = row * gui.char_height + 2;
3208
3209 for (i = 0; i < gui.char_height - 3; i++)
3210 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y++);
3211
3212 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+0, y);
3213 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3214 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+4, y++);
3215 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+1, y);
3216 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3217 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+3, y++);
3218 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3219}
3220#endif /* FEAT_GUI_MOTIF */
3221
3222#ifdef FEAT_GUI_GTK
3223/*
3224 * We have multiple signs to draw at the same location. Draw the
3225 * multi-sign indicator instead. This is the GTK/Gnome version.
3226 */
3227 void
3228netbeans_draw_multisign_indicator(int row)
3229{
3230 int i;
3231 int y;
3232 int x;
3233 GdkDrawable *drawable = gui.drawarea->window;
3234
3235 x = 0;
3236 y = row * gui.char_height + 2;
3237
3238 for (i = 0; i < gui.char_height - 3; i++)
3239 gdk_draw_point(drawable, gui.text_gc, x+2, y++);
3240
3241 gdk_draw_point(drawable, gui.text_gc, x+0, y);
3242 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3243 gdk_draw_point(drawable, gui.text_gc, x+4, y++);
3244 gdk_draw_point(drawable, gui.text_gc, x+1, y);
3245 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3246 gdk_draw_point(drawable, gui.text_gc, x+3, y++);
3247 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3248}
3249#endif /* FEAT_GUI_GTK */
3250
3251/*
3252 * If the mouse is clicked in the gutter of a line with multiple
3253 * annotations, cycle through the set of signs.
3254 */
3255 void
3256netbeans_gutter_click(linenr_T lnum)
3257{
3258 signlist_T *p;
3259
3260 for (p = curbuf->b_signlist; p != NULL; p = p->next)
3261 {
3262 if (p->lnum == lnum && p->next && p->next->lnum == lnum)
3263 {
3264 signlist_T *tail;
3265
3266 /* remove "p" from list, reinsert it at the tail of the sublist */
3267 if (p->prev)
3268 p->prev->next = p->next;
3269 else
3270 curbuf->b_signlist = p->next;
3271 p->next->prev = p->prev;
3272 /* now find end of sublist and insert p */
3273 for (tail = p->next;
3274 tail->next && tail->next->lnum == lnum
3275 && tail->next->id < GUARDEDOFFSET;
3276 tail = tail->next)
3277 ;
3278 /* tail now points to last entry with same lnum (except
3279 * that "guarded" annotations are always last) */
3280 p->next = tail->next;
3281 if (tail->next)
3282 tail->next->prev = p;
3283 p->prev = tail;
3284 tail->next = p;
3285 update_debug_sign(curbuf, lnum);
3286 break;
3287 }
3288 }
3289}
3290
3291
3292/*
3293 * Add a sign of the reqested type at the requested location.
3294 *
3295 * Reverse engineering:
3296 * Apparently an annotation is defined the first time it is used in a buffer.
3297 * When the same annotation is used in two buffers, the second time we do not
3298 * need to define a new sign name but reuse the existing one. But since the
3299 * ID number used in the second buffer starts counting at one again, a mapping
3300 * is made from the ID specifically for the buffer to the global sign name
3301 * (which is a number).
3302 *
3303 * globalsignmap[] stores the signs that have been defined globally.
3304 * buf->signmapused[] maps buffer-local annotation IDs to an index in
3305 * globalsignmap[].
3306 */
3307/*ARGSUSED*/
3308 static void
3309addsigntype(
3310 nbbuf_T *buf,
3311 int typeNum,
3312 char_u *typeName,
3313 char_u *tooltip,
3314 char_u *glyphFile,
3315 int use_fg,
3316 int fg,
3317 int use_bg,
3318 int bg)
3319{
3320 char fgbuf[32];
3321 char bgbuf[32];
3322 int i, j;
3323
3324 for (i = 0; i < globalsignmapused; i++)
3325 if (STRCMP(typeName, globalsignmap[i]) == 0)
3326 break;
3327
3328 if (i == globalsignmapused) /* not found; add it to global map */
3329 {
3330 nbdebug(("DEFINEANNOTYPE(%d,%s,%s,%s,%d,%d)\n",
3331 typeNum, typeName, tooltip, glyphFile, fg, bg));
3332 if (use_fg || use_bg)
3333 {
3334 sprintf(fgbuf, "guifg=#%06x", fg & 0xFFFFFF);
3335 sprintf(bgbuf, "guibg=#%06x", bg & 0xFFFFFF);
3336
3337 coloncmd(":highlight NB_%s %s %s", typeName, (use_fg) ? fgbuf : "",
3338 (use_bg) ? bgbuf : "");
3339 if (*glyphFile == NUL)
3340 /* no glyph, line highlighting only */
3341 coloncmd(":sign define %d linehl=NB_%s", i + 1, typeName);
3342 else if (vim_strsize(glyphFile) <= 2)
3343 /* one- or two-character glyph name, use as text glyph with
3344 * texthl */
3345 coloncmd(":sign define %d text=%s texthl=NB_%s", i + 1,
3346 glyphFile, typeName);
3347 else
3348 /* glyph, line highlighting */
3349 coloncmd(":sign define %d icon=%s linehl=NB_%s", i + 1,
3350 glyphFile, typeName);
3351 }
3352 else
3353 /* glyph, no line highlighting */
3354 coloncmd(":sign define %d icon=%s", i + 1, glyphFile);
3355
3356 if (STRCMP(typeName,"CurrentPC") == 0)
3357 curPCtype = typeNum;
3358
3359 if (globalsignmapused == globalsignmaplen)
3360 {
3361 if (globalsignmaplen == 0) /* first allocation */
3362 {
3363 globalsignmaplen = 20;
3364 globalsignmap = (char **)alloc_clear(globalsignmaplen*sizeof(char *));
3365 }
3366 else /* grow it */
3367 {
3368 int incr;
3369 int oldlen = globalsignmaplen;
3370
3371 globalsignmaplen *= 2;
3372 incr = globalsignmaplen - oldlen;
3373 globalsignmap = (char **)vim_realloc(globalsignmap,
3374 globalsignmaplen * sizeof(char *));
3375 memset(globalsignmap + oldlen, 0, incr * sizeof(char *));
3376 }
3377 }
3378
3379 globalsignmap[i] = (char *)typeName;
3380 globalsignmapused = i + 1;
3381 }
3382
3383 /* check local map; should *not* be found! */
3384 for (j = 0; j < buf->signmapused; j++)
3385 if (buf->signmap[j] == i + 1)
3386 return;
3387
3388 /* add to local map */
3389 if (buf->signmapused == buf->signmaplen)
3390 {
3391 if (buf->signmaplen == 0) /* first allocation */
3392 {
3393 buf->signmaplen = 5;
3394 buf->signmap = (int *)alloc_clear(buf->signmaplen * sizeof(int *));
3395 }
3396 else /* grow it */
3397 {
3398 int incr;
3399 int oldlen = buf->signmaplen;
3400 buf->signmaplen *= 2;
3401 incr = buf->signmaplen - oldlen;
3402 buf->signmap = (int *)vim_realloc(buf->signmap,
3403 buf->signmaplen*sizeof(int *));
3404 memset(buf->signmap + oldlen, 0, incr * sizeof(int *));
3405 }
3406 }
3407
3408 buf->signmap[buf->signmapused++] = i + 1;
3409
3410}
3411
3412
3413/*
3414 * See if we have the requested sign type in the buffer.
3415 */
3416 static int
3417mapsigntype(nbbuf_T *buf, int localsigntype)
3418{
3419 if (--localsigntype >= 0 && localsigntype < buf->signmapused)
3420 return buf->signmap[localsigntype];
3421
3422 return 0;
3423}
3424
3425
3426/*
3427 * Compute length of buffer, don't print anything.
3428 */
3429 static long
3430get_buf_size(buf_T *bufp)
3431{
3432 linenr_T lnum;
3433 long char_count = 0;
3434 int eol_size;
3435 long last_check = 100000L;
3436
3437 if (bufp->b_ml.ml_flags & ML_EMPTY)
3438 return 0;
3439 else
3440 {
3441 if (get_fileformat(bufp) == EOL_DOS)
3442 eol_size = 2;
3443 else
3444 eol_size = 1;
3445 for (lnum = 1; lnum <= bufp->b_ml.ml_line_count; ++lnum)
3446 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003447 char_count += (long)STRLEN(ml_get(lnum)) + eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 /* Check for a CTRL-C every 100000 characters */
3449 if (char_count > last_check)
3450 {
3451 ui_breakcheck();
3452 if (got_int)
3453 return char_count;
3454 last_check = char_count + 100000L;
3455 }
3456 }
3457 /* Correction for when last line doesn't have an EOL. */
3458 if (!bufp->b_p_eol && bufp->b_p_bin)
3459 char_count -= eol_size;
3460 }
3461
3462 return char_count;
3463}
3464
3465/*
3466 * Convert character offset to lnum,col
3467 */
3468 static pos_T *
3469off2pos(buf_T *buf, long offset)
3470{
3471 linenr_T lnum;
3472 static pos_T pos;
3473
3474 pos.lnum = 0;
3475 pos.col = 0;
3476#ifdef FEAT_VIRTUALEDIT
3477 pos.coladd = 0;
3478#endif
3479
3480 if (!(buf->b_ml.ml_flags & ML_EMPTY))
3481 {
3482 if ((lnum = ml_find_line_or_offset(buf, (linenr_T)0, &offset)) < 0)
3483 return NULL;
3484 pos.lnum = lnum;
3485 pos.col = offset;
3486 }
3487
3488 return &pos;
3489}
3490
3491/*
3492 * Convert an argument in the form "1234" to an offset and compute the
3493 * lnum/col from it. Convert an argument in the form "123/12" directly to a
3494 * lnum/col.
3495 * "argp" is advanced to after the argument.
3496 * Return a pointer to the position, NULL if something is wrong.
3497 */
3498 static pos_T *
3499get_off_or_lnum(buf_T *buf, char_u **argp)
3500{
3501 static pos_T mypos;
3502 long off;
3503
3504 off = strtol((char *)*argp, (char **)argp, 10);
3505 if (**argp == '/')
3506 {
3507 mypos.lnum = (linenr_T)off;
3508 ++*argp;
3509 mypos.col = strtol((char *)*argp, (char **)argp, 10);
3510#ifdef FEAT_VIRTUALEDIT
3511 mypos.coladd = 0;
3512#endif
3513 return &mypos;
3514 }
3515 return off2pos(buf, off);
3516}
3517
3518
3519/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003520 * Convert (lnum,col) to byte offset in the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 */
3522 static long
3523pos2off(buf_T *buf, pos_T *pos)
3524{
3525 long offset = 0;
3526
3527 if (!(buf->b_ml.ml_flags & ML_EMPTY))
3528 {
3529 if ((offset = ml_find_line_or_offset(buf, pos->lnum, 0)) < 0)
3530 return 0;
3531 offset += pos->col;
3532 }
3533
3534 return offset;
3535}
3536
3537
Bram Moolenaar009b2592004-10-24 19:18:58 +00003538/*
3539 * This message is printed after NetBeans opens a new file. Its
3540 * similar to the message readfile() uses, but since NetBeans
3541 * doesn't normally call readfile, we do our own.
3542 */
3543 static void
3544print_read_msg(buf)
3545 nbbuf_T *buf;
3546{
3547 int lnum = buf->bufp->b_ml.ml_line_count;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003548 long nchars = (long)buf->bufp->b_orig_size;
Bram Moolenaar009b2592004-10-24 19:18:58 +00003549 char_u c;
3550
3551 msg_add_fname(buf->bufp, buf->bufp->b_ffname);
3552 c = FALSE;
3553
3554 if (buf->bufp->b_p_ro)
3555 {
3556 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]"));
3557 c = TRUE;
3558 }
3559 if (!buf->bufp->b_start_eol)
3560 {
3561 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]") : _("[Incomplete last line]"));
3562 c = TRUE;
3563 }
3564 msg_add_lines(c, (long)lnum, nchars);
3565
3566 /* Now display it */
3567 vim_free(keep_msg);
3568 keep_msg = NULL;
3569 msg_scrolled_ign = TRUE;
3570 msg_trunc_attr(IObuff, FALSE, 0);
3571 msg_scrolled_ign = FALSE;
3572}
3573
3574
3575/*
3576 * Print a message after NetBeans writes the file. This message should be identical
3577 * to the standard message a non-netbeans user would see when writing a file.
3578 */
3579 static void
3580print_save_msg(buf, nchars)
3581 nbbuf_T *buf;
3582 long nchars;
3583{
3584 char_u c;
3585 char_u *p;
3586
3587 if (nchars >= 0)
3588 {
3589 msg_add_fname(buf->bufp, buf->bufp->b_ffname); /* fname in IObuff with quotes */
3590 c = FALSE;
3591
3592 msg_add_lines(c, buf->bufp->b_ml.ml_line_count,
3593 (long)buf->bufp->b_orig_size);
3594
3595 vim_free(keep_msg);
3596 keep_msg = NULL;
3597 msg_scrolled_ign = TRUE;
3598 p = msg_trunc_attr(IObuff, FALSE, 0);
3599 if ((msg_scrolled && !need_wait_return) || !buf->initDone)
3600 {
3601 /* Need to repeat the message after redrawing when:
3602 * - When reading from stdin (the screen will be cleared next).
3603 * - When restart_edit is set (otherwise there will be a delay
3604 * before redrawing).
3605 * - When the screen was scrolled but there is no wait-return
3606 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003607 set_keep_msg(p, 0);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003608 }
3609 msg_scrolled_ign = FALSE;
3610 /* add_to_input_buf((char_u *)"\f", 1); */
3611 }
3612 else
3613 {
3614 char_u ebuf[BUFSIZ];
3615
3616 STRCPY(ebuf, (char_u *)_("E505: "));
3617 STRCAT(ebuf, IObuff);
3618 STRCAT(ebuf, (char_u *)_("is read-only (add ! to override)"));
3619 STRCPY(IObuff, ebuf);
3620 emsg(IObuff);
3621 }
3622}
3623
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624#endif /* defined(FEAT_NETBEANS_INTG) */