blob: 731b98b24b7ec87c9bd413ca336af3deac795d7b [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 Moolenaar009b2592004-10-24 19:18:58 +000064static char *ExtEdProtocolVersion = "2.3";
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
106static int cmdno; /* current command number for reply */
107static 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 Moolenaar349b2f62004-10-11 10:00:50 +0000835 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 Moolenaar349b2f62004-10-11 10:00:50 +0000839 if (nb_do_cmd(bufno, (char_u *)verb, isfunc, 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" */
1011 sprintf(buf, "%d:unmodified=%d\n", i, cmdno);
1012 nbdebug(("EVT: %s", buf));
1013 nb_send(buf, "netbeans_end");
1014 }
1015 sprintf(buf, "%d:killed=%d\n", i, cmdno);
1016 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
1207#define SKIP_STOP 2
1208#define streq(a,b) (strcmp(a,b) == 0)
1209static int needupdate = 0;
1210static int inAtomic = 0;
1211
1212/*
1213 * Do the actual processing of a single netbeans command or function.
1214 * The differance between a command and function is that a function
1215 * gets a response (its required) but a command does not.
1216 * For arguments see comment for nb_parse_cmd().
1217 */
1218 static int
1219nb_do_cmd(
1220 int bufno,
1221 char_u *cmd,
1222 int func,
1223 int cmdno,
1224 char_u *args) /* points to space before arguments or NUL */
1225{
1226 int doupdate = 0;
1227 long off = 0;
1228 nbbuf_T *buf = nb_get_buf(bufno);
1229 static int skip = 0;
1230 int retval = OK;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001231 char *cp; /* for when a char pointer is needed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232
1233 nbdebug(("%s %d: (%d) %s %s\n", (func) ? "FUN" : "CMD", cmdno, bufno, cmd,
1234 STRCMP(cmd, "insert") == 0 ? "<text>" : (char *)args));
1235
1236 if (func)
1237 {
1238/* =====================================================================*/
1239 if (streq((char *)cmd, "getModified"))
1240 {
1241 if (buf == NULL || buf->bufp == NULL)
1242 /* Return the number of buffers that are modified. */
1243 nb_reply_nr(cmdno, (long)count_changed_buffers());
1244 else
1245 /* Return whether the buffer is modified. */
1246 nb_reply_nr(cmdno, (long)(buf->bufp->b_changed
1247 || isNetbeansModified(buf->bufp)));
1248/* =====================================================================*/
1249 }
1250 else if (streq((char *)cmd, "saveAndExit"))
1251 {
1252 /* Note: this will exit Vim if successful. */
1253 coloncmd(":confirm qall");
1254
1255 /* We didn't exit: return the number of changed buffers. */
1256 nb_reply_nr(cmdno, (long)count_changed_buffers());
1257/* =====================================================================*/
1258 }
1259 else if (streq((char *)cmd, "getCursor"))
1260 {
1261 char_u text[200];
1262
1263 /* Note: nb_getbufno() may return -1. This indicates the IDE
1264 * didn't assign a number to the current buffer in response to a
1265 * fileOpened event. */
1266 sprintf((char *)text, "%d %ld %d %ld",
1267 nb_getbufno(curbuf),
1268 (long)curwin->w_cursor.lnum,
1269 (int)curwin->w_cursor.col,
1270 pos2off(curbuf, &curwin->w_cursor));
1271 nb_reply_text(cmdno, text);
1272/* =====================================================================*/
1273 }
1274 else if (streq((char *)cmd, "getLength"))
1275 {
1276 long len = 0;
1277
1278 if (buf == NULL || buf->bufp == NULL)
1279 {
1280 nbdebug((" null bufp in getLength"));
1281 EMSG("E632: null bufp in getLength");
1282 retval = FAIL;
1283 }
1284 else
1285 {
1286 len = get_buf_size(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 }
1288 nb_reply_nr(cmdno, len);
1289/* =====================================================================*/
1290 }
1291 else if (streq((char *)cmd, "getText"))
1292 {
1293 long len;
1294 linenr_T nlines;
1295 char_u *text = NULL;
1296 linenr_T lno = 1;
1297 char_u *p;
1298 char_u *line;
1299
1300 if (buf == NULL || buf->bufp == NULL)
1301 {
1302 nbdebug((" null bufp in getText"));
1303 EMSG("E633: null bufp in getText");
1304 retval = FAIL;
1305 }
1306 else
1307 {
1308 len = get_buf_size(buf->bufp);
1309 nlines = buf->bufp->b_ml.ml_line_count;
1310 text = alloc((unsigned)((len > 0)
1311 ? ((len + nlines) * 2) : 4));
1312 if (text == NULL)
1313 {
1314 nbdebug((" nb_do_cmd: getText has null text field\n"));
1315 retval = FAIL;
1316 }
1317 else
1318 {
1319 p = text;
1320 *p++ = '\"';
1321 for (; lno <= nlines ; lno++)
1322 {
1323 line = nb_quote(ml_get_buf(buf->bufp, lno, FALSE));
1324 if (line != NULL)
1325 {
1326 STRCPY(p, line);
1327 p += STRLEN(line);
1328 *p++ = '\\';
1329 *p++ = 'n';
Bram Moolenaar009b2592004-10-24 19:18:58 +00001330 vim_free(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 }
1333 *p++ = '\"';
1334 *p = '\0';
1335 }
1336 }
1337 if (text == NULL)
1338 nb_reply_text(cmdno, (char_u *)"");
1339 else
1340 {
1341 nb_reply_text(cmdno, text);
1342 vim_free(text);
1343 }
1344/* =====================================================================*/
1345 }
1346 else if (streq((char *)cmd, "remove"))
1347 {
1348 long count;
1349 pos_T first, last;
1350 pos_T *pos;
1351 int oldFire = netbeansFireChanges;
1352 int oldSuppress = netbeansSuppressNoLines;
1353 int wasChanged;
1354
1355 if (skip >= SKIP_STOP)
1356 {
1357 nbdebug((" Skipping %s command\n", (char *) cmd));
1358 nb_reply_nil(cmdno);
1359 return OK;
1360 }
1361
1362 if (buf == NULL || buf->bufp == NULL)
1363 {
1364 nbdebug((" null bufp in remove"));
1365 EMSG("E634: null bufp in remove");
1366 retval = FAIL;
1367 }
1368 else
1369 {
1370 netbeansFireChanges = FALSE;
1371 netbeansSuppressNoLines = TRUE;
1372
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001373 nb_set_curbuf(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 wasChanged = buf->bufp->b_changed;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001375 cp = (char *)args;
1376 off = strtol(cp, &cp, 10);
1377 count = strtol(cp, &cp, 10);
1378 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 /* delete "count" chars, starting at "off" */
1380 pos = off2pos(buf->bufp, off);
1381 if (!pos)
1382 {
1383 nb_reply_text(cmdno, (char_u *)"!bad position");
1384 netbeansFireChanges = oldFire;
1385 netbeansSuppressNoLines = oldSuppress;
1386 return FAIL;
1387 }
1388 first = *pos;
1389 nbdebug((" FIRST POS: line %d, col %d\n", first.lnum, first.col));
1390 pos = off2pos(buf->bufp, off+count-1);
1391 if (!pos)
1392 {
1393 nb_reply_text(cmdno, (char_u *)"!bad count");
1394 netbeansFireChanges = oldFire;
1395 netbeansSuppressNoLines = oldSuppress;
1396 return FAIL;
1397 }
1398 last = *pos;
1399 nbdebug((" LAST POS: line %d, col %d\n", last.lnum, last.col));
1400 curwin->w_cursor = first;
1401 doupdate = 1;
1402
1403 /* keep part of first line */
1404 if (first.lnum == last.lnum && first.col != last.col)
1405 {
1406 /* deletion is within one line */
1407 char_u *p = ml_get(first.lnum);
1408 mch_memmove(p + first.col, p + last.col + 1, STRLEN(p + last.col) + 1);
1409 nbdebug((" NEW LINE %d: %s\n", first.lnum, p));
1410 ml_replace(first.lnum, p, TRUE);
1411 }
1412
1413 if (first.lnum < last.lnum)
1414 {
1415 int i;
1416
1417 /* delete signs from the lines being deleted */
1418 for (i = first.lnum; i <= last.lnum; i++)
1419 {
1420 int id = buf_findsign_id(buf->bufp, (linenr_T)i);
1421 if (id > 0)
1422 {
1423 nbdebug((" Deleting sign %d on line %d\n", id, i));
1424 buf_delsign(buf->bufp, id);
1425 }
1426 else
1427 nbdebug((" No sign on line %d\n", i));
1428 }
1429
1430 /* delete whole lines */
1431 nbdebug((" Deleting lines %d through %d\n", first.lnum, last.lnum));
1432 del_lines(last.lnum - first.lnum + 1, FALSE);
1433 }
1434 buf->bufp->b_changed = wasChanged; /* logically unchanged */
1435 netbeansFireChanges = oldFire;
1436 netbeansSuppressNoLines = oldSuppress;
1437
1438 u_blockfree(buf->bufp);
1439 u_clearall(buf->bufp);
1440 }
1441 nb_reply_nil(cmdno);
1442/* =====================================================================*/
1443 }
1444 else if (streq((char *)cmd, "insert"))
1445 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 char_u *to_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447
1448 if (skip >= SKIP_STOP)
1449 {
1450 nbdebug((" Skipping %s command\n", (char *) cmd));
1451 nb_reply_nil(cmdno);
1452 return OK;
1453 }
1454
1455 /* get offset */
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001456 cp = (char *)args;
1457 off = strtol(cp, &cp, 10);
1458 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459
1460 /* get text to be inserted */
1461 args = skipwhite(args);
1462 args = to_free = (char_u *)nb_unquote(args, NULL);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001463 /*
1464 nbdebug((" CHUNK[%d]: %d bytes at offset %d\n",
1465 buf->bufp->b_ml.ml_line_count, STRLEN(args), off));
1466 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467
1468 if (buf == NULL || buf->bufp == NULL)
1469 {
1470 nbdebug((" null bufp in insert"));
1471 EMSG("E635: null bufp in insert");
1472 retval = FAIL;
1473 }
1474 else if (args != NULL)
1475 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001476 int ff_detected = EOL_UNKNOWN;
1477 int buf_was_empty = (buf->bufp->b_ml.ml_flags & ML_EMPTY);
1478 size_t len = 0;
1479 int added = 0;
1480 int oldFire = netbeansFireChanges;
1481 int old_b_changed;
1482 char_u *nl;
1483 linenr_T lnum;
1484 linenr_T lnum_start;
1485 pos_T *pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 netbeansFireChanges = 0;
1488
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001489 /* Jump to the buffer where we insert. After this "curbuf"
1490 * can be used. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001491 nb_set_curbuf(buf->bufp);
Bram Moolenaara3227e22006-03-08 21:32:40 +00001492 old_b_changed = curbuf->b_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001494 /* Convert the specified character offset into a lnum/col
1495 * position. */
Bram Moolenaara3227e22006-03-08 21:32:40 +00001496 pos = off2pos(curbuf, off);
1497 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001499 if (pos->lnum <= 0)
1500 lnum_start = 1;
1501 else
1502 lnum_start = pos->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 }
1504 else
1505 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001506 /* If the given position is not found, assume we want
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 * the end of the file. See setLocAndSize HACK. */
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001508 if (buf_was_empty)
1509 lnum_start = 1; /* above empty line */
1510 else
1511 lnum_start = curbuf->b_ml.ml_line_count + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001512 }
1513
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001514 /* "lnum" is the line where we insert: either append to it or
1515 * insert a new line above it. */
1516 lnum = lnum_start;
1517
1518 /* Loop over the "\n" separated lines of the argument. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 doupdate = 1;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001520 while (*args != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001522 nl = vim_strchr(args, '\n');
1523 if (nl == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001525 /* Incomplete line, probably truncated. Next "insert"
1526 * command should append to this one. */
1527 len = STRLEN(args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001529 else
1530 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001531 len = nl - args;
1532
1533 /*
1534 * We need to detect EOL style, because the commands
1535 * use a character offset.
1536 */
1537 if (nl > args && nl[-1] == '\r')
1538 {
1539 ff_detected = EOL_DOS;
1540 --len;
1541 }
1542 else
1543 ff_detected = EOL_UNIX;
1544 }
1545 args[len] = NUL;
1546
1547 if (lnum == lnum_start
1548 && ((pos != NULL && pos->col > 0)
1549 || (lnum == 1 && buf_was_empty)))
1550 {
1551 char_u *oldline = ml_get(lnum);
1552 char_u *newline;
1553
1554 /* Insert halfway a line. For simplicity we assume we
1555 * need to append to the line. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001556 newline = alloc_check((unsigned)(STRLEN(oldline) + len + 1));
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001557 if (newline != NULL)
1558 {
1559 STRCPY(newline, oldline);
1560 STRCAT(newline, args);
1561 ml_replace(lnum, newline, FALSE);
1562 }
1563 }
1564 else
1565 {
1566 /* Append a new line. Not that we always do this,
1567 * also when the text doesn't end in a "\n". */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001568 ml_append((linenr_T)(lnum - 1), args, (colnr_T)(len + 1), FALSE);
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001569 ++added;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001570 }
1571
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001572 if (nl == NULL)
1573 break;
1574 ++lnum;
1575 args = nl + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001576 }
1577
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001578 /* Adjust the marks below the inserted lines. */
1579 appended_lines_mark(lnum_start - 1, (long)added);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001581 /*
1582 * When starting with an empty buffer set the fileformat.
1583 * This is just guessing...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 */
1585 if (buf_was_empty)
1586 {
1587 if (ff_detected == EOL_UNKNOWN)
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001588#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 ff_detected = EOL_DOS;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001590#else
1591 ff_detected = EOL_UNIX;
1592#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 set_fileformat(ff_detected, OPT_LOCAL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00001594 curbuf->b_start_ffc = *curbuf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 }
1596
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 /*
1598 * XXX - GRP - Is the next line right? If I've inserted
1599 * text the buffer has been updated but not written. Will
1600 * netbeans guarantee to write it? Even if I do a :q! ?
1601 */
Bram Moolenaara3227e22006-03-08 21:32:40 +00001602 curbuf->b_changed = old_b_changed; /* logically unchanged */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 netbeansFireChanges = oldFire;
1604
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001605 /* Undo info is invalid now... */
Bram Moolenaara3227e22006-03-08 21:32:40 +00001606 u_blockfree(curbuf);
1607 u_clearall(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 }
1609 vim_free(to_free);
1610 nb_reply_nil(cmdno); /* or !error */
1611 }
1612 else
1613 {
1614 nbdebug(("UNIMPLEMENTED FUNCTION: %s\n", cmd));
1615 nb_reply_nil(cmdno);
1616 retval = FAIL;
1617 }
1618 }
1619 else /* Not a function; no reply required. */
1620 {
1621/* =====================================================================*/
1622 if (streq((char *)cmd, "create"))
1623 {
1624 /* Create a buffer without a name. */
1625 if (buf == NULL)
1626 {
1627 EMSG("E636: null buf in create");
1628 return FAIL;
1629 }
1630 vim_free(buf->displayname);
1631 buf->displayname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632
1633 netbeansReadFile = 0; /* don't try to open disk file */
1634 do_ecmd(0, NULL, 0, 0, ECMD_ONE, ECMD_HIDE + ECMD_OLDBUF);
1635 netbeansReadFile = 1;
1636 buf->bufp = curbuf;
1637 maketitle();
Bram Moolenaar009b2592004-10-24 19:18:58 +00001638 buf->insertDone = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 gui_update_menus(0);
1640/* =====================================================================*/
1641 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001642 else if (streq((char *)cmd, "insertDone"))
1643 {
1644 buf->bufp->b_start_eol = *args == 'T';
1645 buf->insertDone = TRUE;
1646 args += 2;
1647 buf->bufp->b_p_ro = *args == 'T';
1648 print_read_msg(buf);
1649/* =====================================================================*/
1650 }
1651 else if (streq((char *)cmd, "saveDone"))
1652 {
1653 long savedChars = atol((char *) args);
1654 print_save_msg(buf, savedChars);
1655/* =====================================================================*/
1656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 else if (streq((char *)cmd, "startDocumentListen"))
1658 {
1659 if (buf == NULL)
1660 {
1661 EMSG("E637: null buf in startDocumentListen");
1662 return FAIL;
1663 }
1664 buf->fireChanges = 1;
1665/* =====================================================================*/
1666 }
1667 else if (streq((char *)cmd, "stopDocumentListen"))
1668 {
1669 if (buf == NULL)
1670 {
1671 EMSG("E638: null buf in stopDocumentListen");
1672 return FAIL;
1673 }
1674 buf->fireChanges = 0;
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001675 if (buf->bufp != NULL && buf->bufp->b_was_netbeans_file)
Bram Moolenaar009b2592004-10-24 19:18:58 +00001676 {
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001677 if (!buf->bufp->b_netbeans_file)
Bram Moolenaar009b2592004-10-24 19:18:58 +00001678 EMSGN(_("E658: NetBeans connection lost for buffer %ld"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 buf->bufp->b_fnum);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001680 else
1681 {
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001682 /* NetBeans uses stopDocumentListen when it stops editing
1683 * a file. It then expects the buffer in Vim to
1684 * disappear. */
1685 do_bufdel(DOBUF_DEL, (char_u *)"", 1,
1686 buf->bufp->b_fnum, buf->bufp->b_fnum, TRUE);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001687 vim_memset(buf, 0, sizeof(nbbuf_T));
1688 }
1689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690/* =====================================================================*/
1691 }
1692 else if (streq((char *)cmd, "setTitle"))
1693 {
1694 if (buf == NULL)
1695 {
1696 EMSG("E639: null buf in setTitle");
1697 return FAIL;
1698 }
1699 vim_free(buf->displayname);
1700 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701/* =====================================================================*/
1702 }
1703 else if (streq((char *)cmd, "initDone"))
1704 {
1705 if (buf == NULL || buf->bufp == NULL)
1706 {
1707 EMSG("E640: null buf in initDone");
1708 return FAIL;
1709 }
1710 doupdate = 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001711 buf->initDone = TRUE;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001712 nb_set_curbuf(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713#if defined(FEAT_AUTOCMD)
1714 apply_autocmds(EVENT_BUFREADPOST, 0, 0, FALSE, buf->bufp);
1715#endif
1716
1717 /* handle any postponed key commands */
1718 handle_key_queue();
1719/* =====================================================================*/
1720 }
1721 else if (streq((char *)cmd, "setBufferNumber")
1722 || streq((char *)cmd, "putBufferNumber"))
1723 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00001724 char_u *path;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 buf_T *bufp;
1726
1727 if (buf == NULL)
1728 {
1729 EMSG("E641: null buf in setBufferNumber");
1730 return FAIL;
1731 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001732 path = (char_u *)nb_unquote(args, NULL);
1733 if (path == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 return FAIL;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001735 bufp = buflist_findname(path);
1736 vim_free(path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 if (bufp == NULL)
1738 {
1739 EMSG2("E642: File %s not found in setBufferNumber", args);
1740 return FAIL;
1741 }
1742 buf->bufp = bufp;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001743 buf->nbbuf_number = bufp->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744
1745 /* "setBufferNumber" has the side effect of jumping to the buffer
1746 * (don't know why!). Don't do that for "putBufferNumber". */
1747 if (*cmd != 'p')
1748 coloncmd(":buffer %d", bufp->b_fnum);
1749 else
1750 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00001751 buf->initDone = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752
1753 /* handle any postponed key commands */
1754 handle_key_queue();
1755 }
1756
1757#if 0 /* never used */
1758 buf->internalname = (char *)alloc_clear(8);
1759 sprintf(buf->internalname, "<%d>", bufno);
1760 buf->netbeansOwns = 0;
1761#endif
1762/* =====================================================================*/
1763 }
1764 else if (streq((char *)cmd, "setFullName"))
1765 {
1766 if (buf == NULL)
1767 {
1768 EMSG("E643: null buf in setFullName");
1769 return FAIL;
1770 }
1771 vim_free(buf->displayname);
1772 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773
1774 netbeansReadFile = 0; /* don't try to open disk file */
1775 do_ecmd(0, (char_u *)buf->displayname, 0, 0, ECMD_ONE,
1776 ECMD_HIDE + ECMD_OLDBUF);
1777 netbeansReadFile = 1;
1778 buf->bufp = curbuf;
1779 maketitle();
1780 gui_update_menus(0);
1781/* =====================================================================*/
1782 }
1783 else if (streq((char *)cmd, "editFile"))
1784 {
1785 if (buf == NULL)
1786 {
1787 EMSG("E644: null buf in editFile");
1788 return FAIL;
1789 }
1790 /* Edit a file: like create + setFullName + read the file. */
1791 vim_free(buf->displayname);
1792 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 do_ecmd(0, (char_u *)buf->displayname, NULL, NULL, ECMD_ONE,
1794 ECMD_HIDE + ECMD_OLDBUF);
1795 buf->bufp = curbuf;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001796 buf->initDone = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 doupdate = 1;
1798#if defined(FEAT_TITLE)
1799 maketitle();
1800#endif
1801 gui_update_menus(0);
1802/* =====================================================================*/
1803 }
1804 else if (streq((char *)cmd, "setVisible"))
1805 {
1806 if (buf == NULL || buf->bufp == NULL)
1807 {
1808/* EMSG("E645: null bufp in setVisible"); */
1809 return FAIL;
1810 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001811 if (streq((char *)args, "T") && buf->bufp != curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 {
1813 exarg_T exarg;
1814 exarg.cmd = (char_u *)"goto";
1815 exarg.forceit = FALSE;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001816 dosetvisible = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 goto_buffer(&exarg, DOBUF_FIRST, FORWARD, buf->bufp->b_fnum);
1818 doupdate = 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001819 dosetvisible = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820
1821 /* Side effect!!!. */
1822 if (!gui.starting)
1823 gui_mch_set_foreground();
1824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825/* =====================================================================*/
1826 }
1827 else if (streq((char *)cmd, "raise"))
1828 {
1829 /* Bring gvim to the foreground. */
1830 if (!gui.starting)
1831 gui_mch_set_foreground();
1832/* =====================================================================*/
1833 }
1834 else if (streq((char *)cmd, "setModified"))
1835 {
1836 if (buf == NULL || buf->bufp == NULL)
1837 {
1838/* EMSG("E646: null bufp in setModified"); */
1839 return FAIL;
1840 }
1841 if (streq((char *)args, "T"))
1842 buf->bufp->b_changed = 1;
1843 else
1844 {
1845 struct stat st;
1846
1847 /* Assume NetBeans stored the file. Reset the timestamp to
1848 * avoid "file changed" warnings. */
1849 if (buf->bufp->b_ffname != NULL
1850 && mch_stat((char *)buf->bufp->b_ffname, &st) >= 0)
1851 buf_store_time(buf->bufp, &st, buf->bufp->b_ffname);
1852 buf->bufp->b_changed = 0;
1853 }
1854 buf->modified = buf->bufp->b_changed;
1855/* =====================================================================*/
1856 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001857 else if (streq((char *)cmd, "setModtime"))
1858 {
1859 buf->bufp->b_mtime = atoi((char *) args);
1860/* =====================================================================*/
1861 }
1862 else if (streq((char *)cmd, "setReadOnly"))
1863 {
1864 if (streq((char *)args, "T"))
1865 buf->bufp->b_p_ro = TRUE;
1866 else
1867 buf->bufp->b_p_ro = FALSE;
1868/* =====================================================================*/
1869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 else if (streq((char *)cmd, "setMark"))
1871 {
1872 /* not yet */
1873/* =====================================================================*/
1874 }
1875 else if (streq((char *)cmd, "showBalloon"))
1876 {
1877#if defined(FEAT_BEVAL)
1878 static char *text = NULL;
1879
1880 /*
1881 * Set up the Balloon Expression Evaluation area.
1882 * Ignore 'ballooneval' here.
1883 * The text pointer must remain valid for a while.
1884 */
1885 if (balloonEval != NULL)
1886 {
1887 vim_free(text);
1888 text = nb_unquote(args, NULL);
1889 if (text != NULL)
1890 gui_mch_post_balloon(balloonEval, (char_u *)text);
1891 }
1892#endif
1893/* =====================================================================*/
1894 }
1895 else if (streq((char *)cmd, "setDot"))
1896 {
1897 pos_T *pos;
1898#ifdef NBDEBUG
1899 char_u *s;
1900#endif
1901
1902 if (buf == NULL || buf->bufp == NULL)
1903 {
1904 EMSG("E647: null bufp in setDot");
1905 return FAIL;
1906 }
1907
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001908 nb_set_curbuf(buf->bufp);
1909
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910#ifdef FEAT_VISUAL
1911 /* Don't want Visual mode now. */
1912 if (VIsual_active)
1913 end_visual_mode();
1914#endif
1915#ifdef NBDEBUG
1916 s = args;
1917#endif
1918 pos = get_off_or_lnum(buf->bufp, &args);
1919 if (pos)
1920 {
1921 curwin->w_cursor = *pos;
1922 check_cursor();
1923#ifdef FEAT_FOLDING
1924 foldOpenCursor();
1925#endif
1926 }
1927 else
1928 nbdebug((" BAD POSITION in setDot: %s\n", s));
1929
1930 /* gui_update_cursor(TRUE, FALSE); */
1931 /* update_curbuf(NOT_VALID); */
1932 update_topline(); /* scroll to show the line */
1933 update_screen(VALID);
1934 setcursor();
1935 out_flush();
1936 gui_update_cursor(TRUE, FALSE);
1937 gui_mch_flush();
1938 /* Quit a hit-return or more prompt. */
1939 if (State == HITRETURN || State == ASKMORE)
1940 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941#ifdef FEAT_GUI_GTK
1942 if (gtk_main_level() > 0)
1943 gtk_main_quit();
1944#endif
1945 }
1946/* =====================================================================*/
1947 }
1948 else if (streq((char *)cmd, "close"))
1949 {
1950#ifdef NBDEBUG
1951 char *name = "<NONE>";
1952#endif
1953
1954 if (buf == NULL)
1955 {
1956 EMSG("E648: null buf in close");
1957 return FAIL;
1958 }
1959
1960#ifdef NBDEBUG
1961 if (buf->displayname != NULL)
1962 name = buf->displayname;
1963#endif
1964/* if (buf->bufp == NULL) */
1965/* EMSG("E649: null bufp in close"); */
1966 nbdebug((" CLOSE %d: %s\n", bufno, name));
1967 need_mouse_correct = TRUE;
1968 if (buf->bufp != NULL)
1969 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD,
1970 buf->bufp->b_fnum, TRUE);
1971 doupdate = 1;
1972/* =====================================================================*/
1973 }
1974 else if (streq((char *)cmd, "setStyle")) /* obsolete... */
1975 {
1976 nbdebug((" setStyle is obsolete!"));
1977/* =====================================================================*/
1978 }
1979 else if (streq((char *)cmd, "setExitDelay"))
1980 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00001981 /* Only used in version 2.1. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982/* =====================================================================*/
1983 }
1984 else if (streq((char *)cmd, "defineAnnoType"))
1985 {
1986#ifdef FEAT_SIGNS
1987 int typeNum;
1988 char_u *typeName;
1989 char_u *tooltip;
1990 char_u *p;
1991 char_u *glyphFile;
1992 int use_fg = 0;
1993 int use_bg = 0;
1994 int fg = -1;
1995 int bg = -1;
1996
1997 if (buf == NULL)
1998 {
1999 EMSG("E650: null buf in defineAnnoType");
2000 return FAIL;
2001 }
2002
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002003 cp = (char *)args;
2004 typeNum = strtol(cp, &cp, 10);
2005 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 args = skipwhite(args);
2007 typeName = (char_u *)nb_unquote(args, &args);
2008 args = skipwhite(args + 1);
2009 tooltip = (char_u *)nb_unquote(args, &args);
2010 args = skipwhite(args + 1);
2011
2012 p = (char_u *)nb_unquote(args, &args);
2013 glyphFile = vim_strsave_escaped(p, escape_chars);
2014 vim_free(p);
2015
2016 args = skipwhite(args + 1);
2017 if (STRNCMP(args, "none", 4) == 0)
2018 args += 5;
2019 else
2020 {
2021 use_fg = 1;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002022 cp = (char *)args;
2023 fg = strtol(cp, &cp, 10);
2024 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 }
2026 if (STRNCMP(args, "none", 4) == 0)
2027 args += 5;
2028 else
2029 {
2030 use_bg = 1;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002031 cp = (char *)args;
2032 bg = strtol(cp, &cp, 10);
2033 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 }
2035 if (typeName != NULL && tooltip != NULL && glyphFile != NULL)
2036 addsigntype(buf, typeNum, typeName, tooltip, glyphFile,
2037 use_fg, fg, use_bg, bg);
2038 else
2039 vim_free(typeName);
2040
2041 /* don't free typeName; it's used directly in addsigntype() */
2042 vim_free(tooltip);
2043 vim_free(glyphFile);
2044
2045#endif
2046/* =====================================================================*/
2047 }
2048 else if (streq((char *)cmd, "addAnno"))
2049 {
2050#ifdef FEAT_SIGNS
2051 int serNum;
2052 int localTypeNum;
2053 int typeNum;
2054# ifdef NBDEBUG
2055 int len;
2056# endif
2057 pos_T *pos;
2058
2059 if (buf == NULL || buf->bufp == NULL)
2060 {
2061 EMSG("E651: null bufp in addAnno");
2062 return FAIL;
2063 }
2064
2065 doupdate = 1;
2066
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002067 cp = (char *)args;
2068 serNum = strtol(cp, &cp, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069
2070 /* Get the typenr specific for this buffer and convert it to
2071 * the global typenumber, as used for the sign name. */
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002072 localTypeNum = strtol(cp, &cp, 10);
2073 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 typeNum = mapsigntype(buf, localTypeNum);
2075
2076 pos = get_off_or_lnum(buf->bufp, &args);
2077
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002078 cp = (char *)args;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079# ifdef NBDEBUG
2080 len =
2081# endif
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002082 strtol(cp, &cp, 10);
2083 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084# ifdef NBDEBUG
2085 if (len != -1)
2086 {
2087 nbdebug((" partial line annotation -- Not Yet Implemented!"));
2088 }
2089# endif
2090 if (serNum >= GUARDEDOFFSET)
2091 {
2092 nbdebug((" too many annotations! ignoring..."));
2093 return FAIL;
2094 }
2095 if (pos)
2096 {
2097 coloncmd(":sign place %d line=%d name=%d buffer=%d",
2098 serNum, pos->lnum, typeNum, buf->bufp->b_fnum);
2099 if (typeNum == curPCtype)
2100 coloncmd(":sign jump %d buffer=%d", serNum,
2101 buf->bufp->b_fnum);
2102 }
2103 /* XXX only redraw what changed. */
2104 redraw_later(CLEAR);
2105#endif
2106/* =====================================================================*/
2107 }
2108 else if (streq((char *)cmd, "removeAnno"))
2109 {
2110#ifdef FEAT_SIGNS
2111 int serNum;
2112
2113 if (buf == NULL || buf->bufp == NULL)
2114 {
2115 nbdebug((" null bufp in removeAnno"));
2116 return FAIL;
2117 }
2118 doupdate = 1;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002119 cp = (char *)args;
2120 serNum = strtol(cp, &cp, 10);
2121 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 coloncmd(":sign unplace %d buffer=%d",
2123 serNum, buf->bufp->b_fnum);
2124 redraw_buf_later(buf->bufp, NOT_VALID);
2125#endif
2126/* =====================================================================*/
2127 }
2128 else if (streq((char *)cmd, "moveAnnoToFront"))
2129 {
2130#ifdef FEAT_SIGNS
2131 nbdebug((" moveAnnoToFront: Not Yet Implemented!"));
2132#endif
2133/* =====================================================================*/
2134 }
2135 else if (streq((char *)cmd, "guard") || streq((char *)cmd, "unguard"))
2136 {
2137 int len;
2138 pos_T first;
2139 pos_T last;
2140 pos_T *pos;
2141 int un = (cmd[0] == 'u');
2142 static int guardId = GUARDEDOFFSET;
2143
2144 if (skip >= SKIP_STOP)
2145 {
2146 nbdebug((" Skipping %s command\n", (char *) cmd));
2147 return OK;
2148 }
2149
2150 nb_init_graphics();
2151
2152 if (buf == NULL || buf->bufp == NULL)
2153 {
2154 nbdebug((" null bufp in %s command", cmd));
2155 return FAIL;
2156 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002157 nb_set_curbuf(buf->bufp);
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002158 cp = (char *)args;
2159 off = strtol(cp, &cp, 10);
2160 len = strtol(cp, NULL, 10);
2161 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 pos = off2pos(buf->bufp, off);
2163 doupdate = 1;
2164 if (!pos)
2165 nbdebug((" no such start pos in %s, %ld\n", cmd, off));
2166 else
2167 {
2168 first = *pos;
2169 pos = off2pos(buf->bufp, off + len - 1);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002170 if (pos != NULL && pos->col == 0)
2171 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 /*
2173 * In Java Swing the offset is a position between 2
2174 * characters. If col == 0 then we really want the
2175 * previous line as the end.
2176 */
2177 pos = off2pos(buf->bufp, off + len - 2);
2178 }
2179 if (!pos)
2180 nbdebug((" no such end pos in %s, %ld\n",
2181 cmd, off + len - 1));
2182 else
2183 {
2184 long lnum;
2185 last = *pos;
2186 /* set highlight for region */
2187 nbdebug((" %sGUARD %ld,%d to %ld,%d\n", (un) ? "UN" : "",
2188 first.lnum, first.col,
2189 last.lnum, last.col));
2190#ifdef FEAT_SIGNS
2191 for (lnum = first.lnum; lnum <= last.lnum; lnum++)
2192 {
2193 if (un)
2194 {
2195 /* never used */
2196 }
2197 else
2198 {
2199 if (buf_findsigntype_id(buf->bufp, lnum,
2200 GUARDED) == 0)
2201 {
2202 coloncmd(
2203 ":sign place %d line=%d name=%d buffer=%d",
2204 guardId++, lnum, GUARDED,
2205 buf->bufp->b_fnum);
2206 }
2207 }
2208 }
2209#endif
2210 redraw_buf_later(buf->bufp, NOT_VALID);
2211 }
2212 }
2213/* =====================================================================*/
2214 }
2215 else if (streq((char *)cmd, "startAtomic"))
2216 {
2217 inAtomic = 1;
2218/* =====================================================================*/
2219 }
2220 else if (streq((char *)cmd, "endAtomic"))
2221 {
2222 inAtomic = 0;
2223 if (needupdate)
2224 {
2225 doupdate = 1;
2226 needupdate = 0;
2227 }
2228/* =====================================================================*/
2229 }
2230 else if (streq((char *)cmd, "save"))
2231 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002232 /*
2233 * NOTE - This command is obsolete wrt NetBeans. Its left in
2234 * only for historical reasons.
2235 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 if (buf == NULL || buf->bufp == NULL)
2237 {
2238 nbdebug((" null bufp in %s command", cmd));
2239 return FAIL;
2240 }
2241
2242 /* the following is taken from ex_cmds.c (do_wqall function) */
2243 if (bufIsChanged(buf->bufp))
2244 {
2245 /* Only write if the buffer can be written. */
2246 if (p_write
2247 && !buf->bufp->b_p_ro
2248 && buf->bufp->b_ffname != NULL
2249#ifdef FEAT_QUICKFIX
2250 && !bt_dontwrite(buf->bufp)
2251#endif
2252 )
2253 {
2254 buf_write_all(buf->bufp, FALSE);
2255#ifdef FEAT_AUTOCMD
2256 /* an autocommand may have deleted the buffer */
2257 if (!buf_valid(buf->bufp))
2258 buf->bufp = NULL;
2259#endif
2260 }
2261 }
2262/* =====================================================================*/
2263 }
2264 else if (streq((char *)cmd, "netbeansBuffer"))
2265 {
2266 if (buf == NULL || buf->bufp == NULL)
2267 {
2268 nbdebug((" null bufp in %s command", cmd));
2269 return FAIL;
2270 }
2271 if (*args == 'T')
2272 {
2273 buf->bufp->b_netbeans_file = TRUE;
2274 buf->bufp->b_was_netbeans_file = TRUE;
2275 }
2276 else
2277 buf->bufp->b_netbeans_file = FALSE;
2278/* =====================================================================*/
2279 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002280 else if (streq((char *)cmd, "specialKeys"))
2281 {
2282 special_keys(args);
2283/* =====================================================================*/
2284 }
2285 else if (streq((char *)cmd, "actionMenuItem"))
2286 {
2287 /* not used yet */
2288/* =====================================================================*/
2289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 else if (streq((char *)cmd, "version"))
2291 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002292 /* not used yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 }
2294 /*
2295 * Unrecognized command is ignored.
2296 */
2297 }
2298 if (inAtomic && doupdate)
2299 {
2300 needupdate = 1;
2301 doupdate = 0;
2302 }
2303
Bram Moolenaar009b2592004-10-24 19:18:58 +00002304 /*
2305 * Is this needed? I moved the netbeans_Xt_connect() later during startup
2306 * and it may no longer be necessary. If its not needed then needupdate
2307 * and doupdate can also be removed.
2308 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 if (buf != NULL && buf->initDone && doupdate)
2310 {
2311 update_screen(NOT_VALID);
2312 setcursor();
2313 out_flush();
2314 gui_update_cursor(TRUE, FALSE);
2315 gui_mch_flush();
2316 /* Quit a hit-return or more prompt. */
2317 if (State == HITRETURN || State == ASKMORE)
2318 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319#ifdef FEAT_GUI_GTK
2320 if (gtk_main_level() > 0)
2321 gtk_main_quit();
2322#endif
2323 }
2324 }
2325
2326 return retval;
2327}
2328
2329
2330/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002331 * If "buf" is not the current buffer try changing to a window that edits this
2332 * buffer. If there is no such window then close the current buffer and set
2333 * the current buffer as "buf".
2334 */
2335 static void
2336nb_set_curbuf(buf)
2337 buf_T *buf;
2338{
2339 if (curbuf != buf && buf_jump_open_win(buf) == NULL)
2340 set_curbuf(buf, DOBUF_GOTO);
2341}
2342
2343/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 * Process a vim colon command.
2345 */
2346 static void
2347coloncmd(char *cmd, ...)
2348{
2349 char buf[1024];
2350 va_list ap;
2351
2352 va_start(ap, cmd);
2353 vsprintf(buf, cmd, ap);
2354 va_end(ap);
2355
2356 nbdebug((" COLONCMD %s\n", buf));
2357
2358/* ALT_INPUT_LOCK_ON; */
2359 do_cmdline((char_u *)buf, NULL, NULL, DOCMD_NOWAIT | DOCMD_KEYTYPED);
2360/* ALT_INPUT_LOCK_OFF; */
2361
2362 setcursor(); /* restore the cursor position */
2363 out_flush(); /* make sure output has been written */
2364
2365 gui_update_cursor(TRUE, FALSE);
2366 gui_mch_flush();
2367}
2368
2369
2370/*
Bram Moolenaar009b2592004-10-24 19:18:58 +00002371 * Parse the specialKeys argument and issue the appropriate map commands.
2372 */
2373 static void
2374special_keys(char_u *args)
2375{
2376 char *save_str = nb_unquote(args, NULL);
2377 char *tok = strtok(save_str, " ");
2378 char *sep;
2379 char keybuf[64];
2380 char cmdbuf[256];
2381
2382 while (tok != NULL)
2383 {
2384 int i = 0;
2385
2386 if ((sep = strchr(tok, '-')) != NULL)
2387 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002388 *sep = NUL;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002389 while (*tok)
2390 {
2391 switch (*tok)
2392 {
2393 case 'A':
2394 case 'M':
2395 case 'C':
2396 case 'S':
2397 keybuf[i++] = *tok;
2398 keybuf[i++] = '-';
2399 break;
2400 }
2401 tok++;
2402 }
2403 tok++;
2404 }
2405
2406 strcpy(&keybuf[i], tok);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002407 vim_snprintf(cmdbuf, sizeof(cmdbuf),
2408 "<silent><%s> :nbkey %s<CR>", keybuf, keybuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002409 do_map(0, (char_u *)cmdbuf, NORMAL, FALSE);
2410 tok = strtok(NULL, " ");
2411 }
2412 vim_free(save_str);
2413}
2414
2415
2416 void
2417ex_nbkey(eap)
2418 exarg_T *eap;
2419{
2420 netbeans_keystring(0, (char *)eap->arg);
2421}
2422
2423
2424/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 * Initialize highlights and signs for use by netbeans (mostly obsolete)
2426 */
2427 static void
2428nb_init_graphics(void)
2429{
2430 static int did_init = FALSE;
2431
2432 if (!did_init)
2433 {
2434 coloncmd(":highlight NBGuarded guibg=Cyan guifg=Black");
2435 coloncmd(":sign define %d linehl=NBGuarded", GUARDED);
2436
2437 did_init = TRUE;
2438 }
2439}
2440
2441/*
2442 * Convert key to netbeans name.
2443 */
2444 static void
2445netbeans_keyname(int key, char *buf)
2446{
2447 char *name = 0;
2448 char namebuf[2];
2449 int ctrl = 0;
2450 int shift = 0;
2451 int alt = 0;
2452
2453 if (mod_mask & MOD_MASK_CTRL)
2454 ctrl = 1;
2455 if (mod_mask & MOD_MASK_SHIFT)
2456 shift = 1;
2457 if (mod_mask & MOD_MASK_ALT)
2458 alt = 1;
2459
2460
2461 switch (key)
2462 {
2463 case K_F1: name = "F1"; break;
2464 case K_S_F1: name = "F1"; shift = 1; break;
2465 case K_F2: name = "F2"; break;
2466 case K_S_F2: name = "F2"; shift = 1; break;
2467 case K_F3: name = "F3"; break;
2468 case K_S_F3: name = "F3"; shift = 1; break;
2469 case K_F4: name = "F4"; break;
2470 case K_S_F4: name = "F4"; shift = 1; break;
2471 case K_F5: name = "F5"; break;
2472 case K_S_F5: name = "F5"; shift = 1; break;
2473 case K_F6: name = "F6"; break;
2474 case K_S_F6: name = "F6"; shift = 1; break;
2475 case K_F7: name = "F7"; break;
2476 case K_S_F7: name = "F7"; shift = 1; break;
2477 case K_F8: name = "F8"; break;
2478 case K_S_F8: name = "F8"; shift = 1; break;
2479 case K_F9: name = "F9"; break;
2480 case K_S_F9: name = "F9"; shift = 1; break;
2481 case K_F10: name = "F10"; break;
2482 case K_S_F10: name = "F10"; shift = 1; break;
2483 case K_F11: name = "F11"; break;
2484 case K_S_F11: name = "F11"; shift = 1; break;
2485 case K_F12: name = "F12"; break;
2486 case K_S_F12: name = "F12"; shift = 1; break;
2487 default:
2488 if (key >= ' ' && key <= '~')
2489 {
2490 /* Allow ASCII characters. */
2491 name = namebuf;
2492 namebuf[0] = key;
2493 namebuf[1] = NUL;
2494 }
2495 else
2496 name = "X";
2497 break;
2498 }
2499
2500 buf[0] = '\0';
2501 if (ctrl)
2502 strcat(buf, "C");
2503 if (shift)
2504 strcat(buf, "S");
2505 if (alt)
2506 strcat(buf, "M"); /* META */
2507 if (ctrl || shift || alt)
2508 strcat(buf, "-");
2509 strcat(buf, name);
2510}
2511
2512#ifdef FEAT_BEVAL
2513/*
2514 * Function to be called for balloon evaluation. Grabs the text under the
2515 * cursor and sends it to the debugger for evaluation. The debugger should
2516 * respond with a showBalloon command when there is a useful result.
2517 */
2518/*ARGSUSED*/
Bram Moolenaard62bec82005-03-07 22:56:57 +00002519 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520netbeans_beval_cb(
2521 BalloonEval *beval,
2522 int state)
2523{
Bram Moolenaard62bec82005-03-07 22:56:57 +00002524 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 char_u *text;
Bram Moolenaard62bec82005-03-07 22:56:57 +00002526 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 int col;
2528 char buf[MAXPATHL * 2 + 25];
2529 char_u *p;
2530
2531 /* Don't do anything when 'ballooneval' is off, messages scrolled the
2532 * windows up or we have no connection. */
2533 if (!p_beval || msg_scrolled > 0 || !haveConnection)
2534 return;
2535
Bram Moolenaard62bec82005-03-07 22:56:57 +00002536 if (get_beval_info(beval, TRUE, &wp, &lnum, &text, &col) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 {
2538 /* Send debugger request. Only when the text is of reasonable
2539 * length. */
2540 if (text != NULL && text[0] != NUL && STRLEN(text) < MAXPATHL)
2541 {
2542 p = nb_quote(text);
2543 if (p != NULL)
Bram Moolenaar009b2592004-10-24 19:18:58 +00002544 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002545 vim_snprintf(buf, sizeof(buf),
2546 "0:balloonText=%d \"%s\"\n", cmdno, p);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002547 vim_free(p);
2548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 nbdebug(("EVT: %s", buf));
2550 nb_send(buf, "netbeans_beval_cb");
2551 }
2552 vim_free(text);
2553 }
2554}
2555#endif
2556
2557/*
2558 * Tell netbeans that the window was opened, ready for commands.
2559 */
2560 void
2561netbeans_startup_done(void)
2562{
2563 char *cmd = "0:startupDone=0\n";
2564
Bram Moolenaar009b2592004-10-24 19:18:58 +00002565 if (usingNetbeans)
2566#ifdef FEAT_GUI_MOTIF
2567 netbeans_Xt_connect(app_context);
2568#else
2569# ifdef FEAT_GUI_GTK
2570 netbeans_gtk_connect();
Bram Moolenaardf177f62005-02-22 08:39:57 +00002571# else
2572# ifdef FEAT_GUI_W32
2573 netbeans_w32_connect();
2574# endif
Bram Moolenaar009b2592004-10-24 19:18:58 +00002575# endif
2576#endif
2577
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 if (!haveConnection)
2579 return;
2580
Bram Moolenaard62bec82005-03-07 22:56:57 +00002581#ifdef FEAT_BEVAL
2582 bevalServers |= BEVAL_NETBEANS;
2583#endif
2584
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 nbdebug(("EVT: %s", cmd));
2586 nb_send(cmd, "netbeans_startup_done");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587}
2588
Bram Moolenaar009b2592004-10-24 19:18:58 +00002589/*
2590 * Tell netbeans that we're exiting. This should be called right
2591 * before calling exit.
2592 */
2593 void
2594netbeans_send_disconnect()
2595{
2596 char buf[128];
2597
2598 if (haveConnection)
2599 {
2600 sprintf(buf, "0:disconnect=%d\n", cmdno);
2601 nbdebug(("EVT: %s", buf));
2602 nb_send(buf, "netbeans_disconnect");
2603 }
2604}
2605
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_W32) || defined(PROTO)
2607/*
2608 * Tell netbeans that the window was moved or resized.
2609 */
2610 void
2611netbeans_frame_moved(int new_x, int new_y)
2612{
2613 char buf[128];
2614
2615 if (!haveConnection)
2616 return;
2617
2618 sprintf(buf, "0:geometry=%d %d %d %d %d\n",
2619 cmdno, (int)Columns, (int)Rows, new_x, new_y);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002620 /*nbdebug(("EVT: %s", buf)); happens too many times during a move */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 nb_send(buf, "netbeans_frame_moved");
2622}
2623#endif
2624
2625/*
Bram Moolenaar009b2592004-10-24 19:18:58 +00002626 * Tell netbeans the user opened or activated a file.
2627 */
2628 void
2629netbeans_file_activated(buf_T *bufp)
2630{
2631 int bufno = nb_getbufno(bufp);
2632 nbbuf_T *bp = nb_get_buf(bufno);
2633 char buffer[2*MAXPATHL];
2634 char_u *q;
2635
2636 if (!haveConnection || dosetvisible)
2637 return;
2638
2639 q = nb_quote(bufp->b_ffname);
2640 if (q == NULL || bp == NULL || bufp == NULL)
2641 return;
2642
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002643 vim_snprintf(buffer, sizeof(buffer), "%d:fileOpened=%d \"%s\" %s %s\n",
Bram Moolenaar009b2592004-10-24 19:18:58 +00002644 bufno,
2645 bufno,
2646 (char *)q,
2647 "T", /* open in NetBeans */
2648 "F"); /* modified */
2649
2650 vim_free(q);
2651 nbdebug(("EVT: %s", buffer));
2652
2653 nb_send(buffer, "netbeans_file_opened");
2654}
2655
2656/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 * Tell netbeans the user opened a file.
2658 */
2659 void
Bram Moolenaar009b2592004-10-24 19:18:58 +00002660netbeans_file_opened(buf_T *bufp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661{
Bram Moolenaar009b2592004-10-24 19:18:58 +00002662 int bufno = nb_getbufno(bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 char buffer[2*MAXPATHL];
2664 char_u *q;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002665 nbbuf_T *bp = nb_get_buf(nb_getbufno(bufp));
2666 int bnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667
2668 if (!haveConnection)
2669 return;
2670
Bram Moolenaar009b2592004-10-24 19:18:58 +00002671 q = nb_quote(bufp->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 if (q == NULL)
2673 return;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002674 if (bp != NULL)
2675 bnum = bufno;
2676 else
2677 bnum = 0;
2678
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002679 vim_snprintf(buffer, sizeof(buffer), "%d:fileOpened=%d \"%s\" %s %s\n",
Bram Moolenaar009b2592004-10-24 19:18:58 +00002680 bnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 0,
2682 (char *)q,
2683 "T", /* open in NetBeans */
2684 "F"); /* modified */
2685
2686 vim_free(q);
2687 nbdebug(("EVT: %s", buffer));
2688
2689 nb_send(buffer, "netbeans_file_opened");
Bram Moolenaar009b2592004-10-24 19:18:58 +00002690 if (p_acd && vim_chdirfile(bufp->b_ffname) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 shorten_fnames(TRUE);
2692}
2693
2694/*
2695 * Tell netbeans a file was closed.
2696 */
2697 void
2698netbeans_file_closed(buf_T *bufp)
2699{
2700 int bufno = nb_getbufno(bufp);
2701 nbbuf_T *nbbuf = nb_get_buf(bufno);
2702 char buffer[2*MAXPATHL];
2703
2704 if (!haveConnection || bufno < 0)
2705 return;
2706
2707 if (!netbeansCloseFile)
2708 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002709 nbdebug(("Ignoring file_closed for %s. File was closed from IDE\n",
2710 bufp->b_ffname));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 return;
2712 }
2713
Bram Moolenaar009b2592004-10-24 19:18:58 +00002714 nbdebug(("netbeans_file_closed:\n"));
2715 nbdebug((" Closing bufno: %d", bufno));
2716 if (curbuf != NULL && curbuf != bufp)
2717 {
2718 nbdebug((" Curbuf bufno: %d\n", nb_getbufno(curbuf)));
2719 }
2720 else if (curbuf == bufp)
2721 {
2722 nbdebug((" curbuf == bufp\n"));
2723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724
2725 if (bufno <= 0)
2726 return;
2727
2728 sprintf(buffer, "%d:killed=%d\n", bufno, cmdno);
2729
2730 nbdebug(("EVT: %s", buffer));
2731
2732 nb_send(buffer, "netbeans_file_closed");
2733
2734 if (nbbuf != NULL)
2735 nbbuf->bufp = NULL;
2736}
2737
2738/*
2739 * Get a pointer to the Netbeans buffer for Vim buffer "bufp".
2740 * Return NULL if there is no such buffer or changes are not to be reported.
2741 * Otherwise store the buffer number in "*bufnop".
2742 */
2743 static nbbuf_T *
2744nb_bufp2nbbuf_fire(buf_T *bufp, int *bufnop)
2745{
2746 int bufno;
2747 nbbuf_T *nbbuf;
2748
2749 if (!haveConnection || !netbeansFireChanges)
2750 return NULL; /* changes are not reported at all */
2751
2752 bufno = nb_getbufno(bufp);
2753 if (bufno <= 0)
2754 return NULL; /* file is not known to NetBeans */
2755
2756 nbbuf = nb_get_buf(bufno);
2757 if (nbbuf != NULL && !nbbuf->fireChanges)
2758 return NULL; /* changes in this buffer are not reported */
2759
2760 *bufnop = bufno;
2761 return nbbuf;
2762}
2763
2764/*
2765 * Tell netbeans the user inserted some text.
2766 */
2767 void
2768netbeans_inserted(
2769 buf_T *bufp,
2770 linenr_T linenr,
2771 colnr_T col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 char_u *txt,
2773 int newlen)
2774{
2775 char_u *buf;
2776 int bufno;
2777 nbbuf_T *nbbuf;
2778 pos_T pos;
2779 long off;
2780 char_u *p;
2781 char_u *newtxt;
2782
2783 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
2784 if (nbbuf == NULL)
2785 return;
2786
Bram Moolenaar009b2592004-10-24 19:18:58 +00002787 /* Don't mark as modified for initial read */
2788 if (nbbuf->insertDone)
2789 nbbuf->modified = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790
2791 pos.lnum = linenr;
2792 pos.col = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 off = pos2off(bufp, &pos);
2794
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 /* send the "insert" EVT */
2796 newtxt = alloc(newlen + 1);
Bram Moolenaarb6356332005-07-18 21:40:44 +00002797 vim_strncpy(newtxt, txt, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 p = nb_quote(newtxt);
2799 if (p != NULL)
2800 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002801 buf = alloc(128 + 2*newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 sprintf((char *)buf, "%d:insert=%d %ld \"%s\"\n", bufno, cmdno, off, p);
2803 nbdebug(("EVT: %s", buf));
2804 nb_send((char *)buf, "netbeans_inserted");
Bram Moolenaar009b2592004-10-24 19:18:58 +00002805 vim_free(p);
2806 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 vim_free(newtxt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809}
2810
2811/*
2812 * Tell netbeans some bytes have been removed.
2813 */
2814 void
2815netbeans_removed(
2816 buf_T *bufp,
2817 linenr_T linenr,
2818 colnr_T col,
2819 long len)
2820{
2821 char_u buf[128];
2822 int bufno;
2823 nbbuf_T *nbbuf;
2824 pos_T pos;
2825 long off;
2826
2827 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
2828 if (nbbuf == NULL)
2829 return;
2830
2831 if (len < 0)
2832 {
2833 nbdebug(("Negative len %ld in netbeans_removed()!", len));
2834 return;
2835 }
2836
2837 nbbuf->modified = 1;
2838
2839 pos.lnum = linenr;
2840 pos.col = col;
2841
2842 off = pos2off(bufp, &pos);
2843
2844 sprintf((char *)buf, "%d:remove=%d %ld %ld\n", bufno, cmdno, off, len);
2845 nbdebug(("EVT: %s", buf));
2846 nb_send((char *)buf, "netbeans_removed");
2847}
2848
2849/*
2850 * Send netbeans an unmodufied command.
2851 */
2852/*ARGSUSED*/
2853 void
2854netbeans_unmodified(buf_T *bufp)
2855{
2856#if 0
2857 char_u buf[128];
2858 int bufno;
2859 nbbuf_T *nbbuf;
2860
2861 /* This has been disabled, because NetBeans considers a buffer modified
2862 * even when all changes have been undone. */
2863 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
2864 if (nbbuf == NULL)
2865 return;
2866
2867 nbbuf->modified = 0;
2868
2869 sprintf((char *)buf, "%d:unmodified=%d\n", bufno, cmdno);
2870 nbdebug(("EVT: %s", buf));
2871 nb_send((char *)buf, "netbeans_unmodified");
2872#endif
2873}
2874
2875/*
2876 * Send a button release event back to netbeans. Its up to netbeans
2877 * to decide what to do (if anything) with this event.
2878 */
2879 void
2880netbeans_button_release(int button)
2881{
2882 char buf[128];
2883 int bufno;
2884
2885 bufno = nb_getbufno(curbuf);
2886
2887 if (bufno >= 0 && curwin != NULL && curwin->w_buffer == curbuf)
2888 {
Bram Moolenaarc92ad2e2005-01-19 22:08:28 +00002889 int col = mouse_col - W_WINCOL(curwin) - (curwin->w_p_nu ? 9 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 long off = pos2off(curbuf, &curwin->w_cursor);
2891
2892 /* sync the cursor position */
2893 sprintf(buf, "%d:newDotAndMark=%d %ld %ld\n", bufno, cmdno, off, off);
2894 nbdebug(("EVT: %s", buf));
2895 nb_send(buf, "netbeans_button_release[newDotAndMark]");
2896
2897 sprintf(buf, "%d:buttonRelease=%d %d %ld %d\n", bufno, cmdno,
2898 button, (long)curwin->w_cursor.lnum, col);
2899 nbdebug(("EVT: %s", buf));
2900 nb_send(buf, "netbeans_button_release");
2901 }
2902}
2903
2904
2905/*
2906 * Send a keypress event back to netbeans. This usualy simulates some
Bram Moolenaar009b2592004-10-24 19:18:58 +00002907 * kind of function key press. This function operates on a key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 */
2909 void
2910netbeans_keycommand(int key)
2911{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 char keyName[60];
Bram Moolenaar009b2592004-10-24 19:18:58 +00002913
2914 netbeans_keyname(key, keyName);
2915 netbeans_keystring(key, keyName);
2916}
2917
2918
2919/*
2920 * Send a keypress event back to netbeans. This usualy simulates some
2921 * kind of function key press. This function operates on a key string.
2922 */
2923 static void
2924netbeans_keystring(int key, char *keyName)
2925{
2926 char buf[2*MAXPATHL];
2927 int bufno = nb_getbufno(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 long off;
2929 char_u *q;
2930
2931 if (!haveConnection)
2932 return;
2933
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934
2935 if (bufno == -1)
2936 {
2937 nbdebug(("got keycommand for non-NetBeans buffer, opening...\n"));
2938 q = curbuf->b_ffname == NULL ? (char_u *)""
2939 : nb_quote(curbuf->b_ffname);
2940 if (q == NULL)
2941 return;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002942 vim_snprintf(buf, sizeof(buf), "0:fileOpened=%d \"%s\" %s %s\n", 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 q,
2944 "T", /* open in NetBeans */
2945 "F"); /* modified */
2946 if (curbuf->b_ffname != NULL)
2947 vim_free(q);
2948 nbdebug(("EVT: %s", buf));
2949 nb_send(buf, "netbeans_keycommand");
2950
Bram Moolenaar5b625c52005-01-31 18:55:55 +00002951 if (key > 0)
2952 postpone_keycommand(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 return;
2954 }
2955
2956 /* sync the cursor position */
2957 off = pos2off(curbuf, &curwin->w_cursor);
2958 sprintf(buf, "%d:newDotAndMark=%d %ld %ld\n", bufno, cmdno, off, off);
2959 nbdebug(("EVT: %s", buf));
2960 nb_send(buf, "netbeans_keycommand");
2961
2962 /* To work on Win32 you must apply patch to ExtEditor module
2963 * from ExtEdCaret.java.diff - make EVT_newDotAndMark handler
2964 * more synchronous
2965 */
2966
2967 /* now send keyCommand event */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002968 vim_snprintf(buf, sizeof(buf), "%d:keyCommand=%d \"%s\"\n",
2969 bufno, cmdno, keyName);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 nbdebug(("EVT: %s", buf));
2971 nb_send(buf, "netbeans_keycommand");
2972
2973 /* New: do both at once and include the lnum/col. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002974 vim_snprintf(buf, sizeof(buf), "%d:keyAtPos=%d \"%s\" %ld %ld/%ld\n",
2975 bufno, cmdno, keyName,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 off, (long)curwin->w_cursor.lnum, (long)curwin->w_cursor.col);
2977 nbdebug(("EVT: %s", buf));
2978 nb_send(buf, "netbeans_keycommand");
2979}
2980
2981
2982/*
2983 * Send a save event to netbeans.
2984 */
2985 void
2986netbeans_save_buffer(buf_T *bufp)
2987{
2988 char_u buf[64];
2989 int bufno;
2990 nbbuf_T *nbbuf;
2991
2992 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
2993 if (nbbuf == NULL)
2994 return;
2995
2996 nbbuf->modified = 0;
2997
2998 sprintf((char *)buf, "%d:save=%d\n", bufno, cmdno);
2999 nbdebug(("EVT: %s", buf));
3000 nb_send((char *)buf, "netbeans_save_buffer");
3001}
3002
3003
3004/*
3005 * Send remove command to netbeans (this command has been turned off).
3006 */
3007 void
3008netbeans_deleted_all_lines(buf_T *bufp)
3009{
3010 char_u buf[64];
3011 int bufno;
3012 nbbuf_T *nbbuf;
3013
3014 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3015 if (nbbuf == NULL)
3016 return;
3017
Bram Moolenaar009b2592004-10-24 19:18:58 +00003018 /* Don't mark as modified for initial read */
3019 if (nbbuf->insertDone)
3020 nbbuf->modified = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021
3022 sprintf((char *)buf, "%d:remove=%d 0 -1\n", bufno, cmdno);
3023 nbdebug(("EVT(suppressed): %s", buf));
3024/* nb_send(buf, "netbeans_deleted_all_lines"); */
3025}
3026
3027
3028/*
3029 * See if the lines are guarded. The top and bot parameters are from
3030 * u_savecommon(), these are the line above the change and the line below the
3031 * change.
3032 */
3033 int
3034netbeans_is_guarded(linenr_T top, linenr_T bot)
3035{
3036 signlist_T *p;
3037 int lnum;
3038
3039 for (p = curbuf->b_signlist; p != NULL; p = p->next)
3040 if (p->id >= GUARDEDOFFSET)
3041 for (lnum = top + 1; lnum < bot; lnum++)
3042 if (lnum == p->lnum)
3043 return TRUE;
3044
3045 return FALSE;
3046}
3047
3048#if defined(FEAT_GUI_MOTIF) || defined(PROTO)
3049/*
3050 * We have multiple signs to draw at the same location. Draw the
3051 * multi-sign indicator instead. This is the Motif version.
3052 */
3053 void
3054netbeans_draw_multisign_indicator(int row)
3055{
3056 int i;
3057 int y;
3058 int x;
3059
3060 x = 0;
3061 y = row * gui.char_height + 2;
3062
3063 for (i = 0; i < gui.char_height - 3; i++)
3064 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y++);
3065
3066 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+0, y);
3067 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3068 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+4, y++);
3069 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+1, y);
3070 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3071 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+3, y++);
3072 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3073}
3074#endif /* FEAT_GUI_MOTIF */
3075
3076#ifdef FEAT_GUI_GTK
3077/*
3078 * We have multiple signs to draw at the same location. Draw the
3079 * multi-sign indicator instead. This is the GTK/Gnome version.
3080 */
3081 void
3082netbeans_draw_multisign_indicator(int row)
3083{
3084 int i;
3085 int y;
3086 int x;
3087 GdkDrawable *drawable = gui.drawarea->window;
3088
3089 x = 0;
3090 y = row * gui.char_height + 2;
3091
3092 for (i = 0; i < gui.char_height - 3; i++)
3093 gdk_draw_point(drawable, gui.text_gc, x+2, y++);
3094
3095 gdk_draw_point(drawable, gui.text_gc, x+0, y);
3096 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3097 gdk_draw_point(drawable, gui.text_gc, x+4, y++);
3098 gdk_draw_point(drawable, gui.text_gc, x+1, y);
3099 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3100 gdk_draw_point(drawable, gui.text_gc, x+3, y++);
3101 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3102}
3103#endif /* FEAT_GUI_GTK */
3104
3105/*
3106 * If the mouse is clicked in the gutter of a line with multiple
3107 * annotations, cycle through the set of signs.
3108 */
3109 void
3110netbeans_gutter_click(linenr_T lnum)
3111{
3112 signlist_T *p;
3113
3114 for (p = curbuf->b_signlist; p != NULL; p = p->next)
3115 {
3116 if (p->lnum == lnum && p->next && p->next->lnum == lnum)
3117 {
3118 signlist_T *tail;
3119
3120 /* remove "p" from list, reinsert it at the tail of the sublist */
3121 if (p->prev)
3122 p->prev->next = p->next;
3123 else
3124 curbuf->b_signlist = p->next;
3125 p->next->prev = p->prev;
3126 /* now find end of sublist and insert p */
3127 for (tail = p->next;
3128 tail->next && tail->next->lnum == lnum
3129 && tail->next->id < GUARDEDOFFSET;
3130 tail = tail->next)
3131 ;
3132 /* tail now points to last entry with same lnum (except
3133 * that "guarded" annotations are always last) */
3134 p->next = tail->next;
3135 if (tail->next)
3136 tail->next->prev = p;
3137 p->prev = tail;
3138 tail->next = p;
3139 update_debug_sign(curbuf, lnum);
3140 break;
3141 }
3142 }
3143}
3144
3145
3146/*
3147 * Add a sign of the reqested type at the requested location.
3148 *
3149 * Reverse engineering:
3150 * Apparently an annotation is defined the first time it is used in a buffer.
3151 * When the same annotation is used in two buffers, the second time we do not
3152 * need to define a new sign name but reuse the existing one. But since the
3153 * ID number used in the second buffer starts counting at one again, a mapping
3154 * is made from the ID specifically for the buffer to the global sign name
3155 * (which is a number).
3156 *
3157 * globalsignmap[] stores the signs that have been defined globally.
3158 * buf->signmapused[] maps buffer-local annotation IDs to an index in
3159 * globalsignmap[].
3160 */
3161/*ARGSUSED*/
3162 static void
3163addsigntype(
3164 nbbuf_T *buf,
3165 int typeNum,
3166 char_u *typeName,
3167 char_u *tooltip,
3168 char_u *glyphFile,
3169 int use_fg,
3170 int fg,
3171 int use_bg,
3172 int bg)
3173{
3174 char fgbuf[32];
3175 char bgbuf[32];
3176 int i, j;
3177
3178 for (i = 0; i < globalsignmapused; i++)
3179 if (STRCMP(typeName, globalsignmap[i]) == 0)
3180 break;
3181
3182 if (i == globalsignmapused) /* not found; add it to global map */
3183 {
3184 nbdebug(("DEFINEANNOTYPE(%d,%s,%s,%s,%d,%d)\n",
3185 typeNum, typeName, tooltip, glyphFile, fg, bg));
3186 if (use_fg || use_bg)
3187 {
3188 sprintf(fgbuf, "guifg=#%06x", fg & 0xFFFFFF);
3189 sprintf(bgbuf, "guibg=#%06x", bg & 0xFFFFFF);
3190
3191 coloncmd(":highlight NB_%s %s %s", typeName, (use_fg) ? fgbuf : "",
3192 (use_bg) ? bgbuf : "");
3193 if (*glyphFile == NUL)
3194 /* no glyph, line highlighting only */
3195 coloncmd(":sign define %d linehl=NB_%s", i + 1, typeName);
3196 else if (vim_strsize(glyphFile) <= 2)
3197 /* one- or two-character glyph name, use as text glyph with
3198 * texthl */
3199 coloncmd(":sign define %d text=%s texthl=NB_%s", i + 1,
3200 glyphFile, typeName);
3201 else
3202 /* glyph, line highlighting */
3203 coloncmd(":sign define %d icon=%s linehl=NB_%s", i + 1,
3204 glyphFile, typeName);
3205 }
3206 else
3207 /* glyph, no line highlighting */
3208 coloncmd(":sign define %d icon=%s", i + 1, glyphFile);
3209
3210 if (STRCMP(typeName,"CurrentPC") == 0)
3211 curPCtype = typeNum;
3212
3213 if (globalsignmapused == globalsignmaplen)
3214 {
3215 if (globalsignmaplen == 0) /* first allocation */
3216 {
3217 globalsignmaplen = 20;
3218 globalsignmap = (char **)alloc_clear(globalsignmaplen*sizeof(char *));
3219 }
3220 else /* grow it */
3221 {
3222 int incr;
3223 int oldlen = globalsignmaplen;
3224
3225 globalsignmaplen *= 2;
3226 incr = globalsignmaplen - oldlen;
3227 globalsignmap = (char **)vim_realloc(globalsignmap,
3228 globalsignmaplen * sizeof(char *));
3229 memset(globalsignmap + oldlen, 0, incr * sizeof(char *));
3230 }
3231 }
3232
3233 globalsignmap[i] = (char *)typeName;
3234 globalsignmapused = i + 1;
3235 }
3236
3237 /* check local map; should *not* be found! */
3238 for (j = 0; j < buf->signmapused; j++)
3239 if (buf->signmap[j] == i + 1)
3240 return;
3241
3242 /* add to local map */
3243 if (buf->signmapused == buf->signmaplen)
3244 {
3245 if (buf->signmaplen == 0) /* first allocation */
3246 {
3247 buf->signmaplen = 5;
3248 buf->signmap = (int *)alloc_clear(buf->signmaplen * sizeof(int *));
3249 }
3250 else /* grow it */
3251 {
3252 int incr;
3253 int oldlen = buf->signmaplen;
3254 buf->signmaplen *= 2;
3255 incr = buf->signmaplen - oldlen;
3256 buf->signmap = (int *)vim_realloc(buf->signmap,
3257 buf->signmaplen*sizeof(int *));
3258 memset(buf->signmap + oldlen, 0, incr * sizeof(int *));
3259 }
3260 }
3261
3262 buf->signmap[buf->signmapused++] = i + 1;
3263
3264}
3265
3266
3267/*
3268 * See if we have the requested sign type in the buffer.
3269 */
3270 static int
3271mapsigntype(nbbuf_T *buf, int localsigntype)
3272{
3273 if (--localsigntype >= 0 && localsigntype < buf->signmapused)
3274 return buf->signmap[localsigntype];
3275
3276 return 0;
3277}
3278
3279
3280/*
3281 * Compute length of buffer, don't print anything.
3282 */
3283 static long
3284get_buf_size(buf_T *bufp)
3285{
3286 linenr_T lnum;
3287 long char_count = 0;
3288 int eol_size;
3289 long last_check = 100000L;
3290
3291 if (bufp->b_ml.ml_flags & ML_EMPTY)
3292 return 0;
3293 else
3294 {
3295 if (get_fileformat(bufp) == EOL_DOS)
3296 eol_size = 2;
3297 else
3298 eol_size = 1;
3299 for (lnum = 1; lnum <= bufp->b_ml.ml_line_count; ++lnum)
3300 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003301 char_count += (long)STRLEN(ml_get(lnum)) + eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 /* Check for a CTRL-C every 100000 characters */
3303 if (char_count > last_check)
3304 {
3305 ui_breakcheck();
3306 if (got_int)
3307 return char_count;
3308 last_check = char_count + 100000L;
3309 }
3310 }
3311 /* Correction for when last line doesn't have an EOL. */
3312 if (!bufp->b_p_eol && bufp->b_p_bin)
3313 char_count -= eol_size;
3314 }
3315
3316 return char_count;
3317}
3318
3319/*
3320 * Convert character offset to lnum,col
3321 */
3322 static pos_T *
3323off2pos(buf_T *buf, long offset)
3324{
3325 linenr_T lnum;
3326 static pos_T pos;
3327
3328 pos.lnum = 0;
3329 pos.col = 0;
3330#ifdef FEAT_VIRTUALEDIT
3331 pos.coladd = 0;
3332#endif
3333
3334 if (!(buf->b_ml.ml_flags & ML_EMPTY))
3335 {
3336 if ((lnum = ml_find_line_or_offset(buf, (linenr_T)0, &offset)) < 0)
3337 return NULL;
3338 pos.lnum = lnum;
3339 pos.col = offset;
3340 }
3341
3342 return &pos;
3343}
3344
3345/*
3346 * Convert an argument in the form "1234" to an offset and compute the
3347 * lnum/col from it. Convert an argument in the form "123/12" directly to a
3348 * lnum/col.
3349 * "argp" is advanced to after the argument.
3350 * Return a pointer to the position, NULL if something is wrong.
3351 */
3352 static pos_T *
3353get_off_or_lnum(buf_T *buf, char_u **argp)
3354{
3355 static pos_T mypos;
3356 long off;
3357
3358 off = strtol((char *)*argp, (char **)argp, 10);
3359 if (**argp == '/')
3360 {
3361 mypos.lnum = (linenr_T)off;
3362 ++*argp;
3363 mypos.col = strtol((char *)*argp, (char **)argp, 10);
3364#ifdef FEAT_VIRTUALEDIT
3365 mypos.coladd = 0;
3366#endif
3367 return &mypos;
3368 }
3369 return off2pos(buf, off);
3370}
3371
3372
3373/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003374 * Convert (lnum,col) to byte offset in the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 */
3376 static long
3377pos2off(buf_T *buf, pos_T *pos)
3378{
3379 long offset = 0;
3380
3381 if (!(buf->b_ml.ml_flags & ML_EMPTY))
3382 {
3383 if ((offset = ml_find_line_or_offset(buf, pos->lnum, 0)) < 0)
3384 return 0;
3385 offset += pos->col;
3386 }
3387
3388 return offset;
3389}
3390
3391
Bram Moolenaar009b2592004-10-24 19:18:58 +00003392/*
3393 * This message is printed after NetBeans opens a new file. Its
3394 * similar to the message readfile() uses, but since NetBeans
3395 * doesn't normally call readfile, we do our own.
3396 */
3397 static void
3398print_read_msg(buf)
3399 nbbuf_T *buf;
3400{
3401 int lnum = buf->bufp->b_ml.ml_line_count;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003402 long nchars = (long)buf->bufp->b_orig_size;
Bram Moolenaar009b2592004-10-24 19:18:58 +00003403 char_u c;
3404
3405 msg_add_fname(buf->bufp, buf->bufp->b_ffname);
3406 c = FALSE;
3407
3408 if (buf->bufp->b_p_ro)
3409 {
3410 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]"));
3411 c = TRUE;
3412 }
3413 if (!buf->bufp->b_start_eol)
3414 {
3415 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]") : _("[Incomplete last line]"));
3416 c = TRUE;
3417 }
3418 msg_add_lines(c, (long)lnum, nchars);
3419
3420 /* Now display it */
3421 vim_free(keep_msg);
3422 keep_msg = NULL;
3423 msg_scrolled_ign = TRUE;
3424 msg_trunc_attr(IObuff, FALSE, 0);
3425 msg_scrolled_ign = FALSE;
3426}
3427
3428
3429/*
3430 * Print a message after NetBeans writes the file. This message should be identical
3431 * to the standard message a non-netbeans user would see when writing a file.
3432 */
3433 static void
3434print_save_msg(buf, nchars)
3435 nbbuf_T *buf;
3436 long nchars;
3437{
3438 char_u c;
3439 char_u *p;
3440
3441 if (nchars >= 0)
3442 {
3443 msg_add_fname(buf->bufp, buf->bufp->b_ffname); /* fname in IObuff with quotes */
3444 c = FALSE;
3445
3446 msg_add_lines(c, buf->bufp->b_ml.ml_line_count,
3447 (long)buf->bufp->b_orig_size);
3448
3449 vim_free(keep_msg);
3450 keep_msg = NULL;
3451 msg_scrolled_ign = TRUE;
3452 p = msg_trunc_attr(IObuff, FALSE, 0);
3453 if ((msg_scrolled && !need_wait_return) || !buf->initDone)
3454 {
3455 /* Need to repeat the message after redrawing when:
3456 * - When reading from stdin (the screen will be cleared next).
3457 * - When restart_edit is set (otherwise there will be a delay
3458 * before redrawing).
3459 * - When the screen was scrolled but there is no wait-return
3460 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003461 set_keep_msg(p, 0);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003462 }
3463 msg_scrolled_ign = FALSE;
3464 /* add_to_input_buf((char_u *)"\f", 1); */
3465 }
3466 else
3467 {
3468 char_u ebuf[BUFSIZ];
3469
3470 STRCPY(ebuf, (char_u *)_("E505: "));
3471 STRCAT(ebuf, IObuff);
3472 STRCAT(ebuf, (char_u *)_("is read-only (add ! to override)"));
3473 STRCPY(IObuff, ebuf);
3474 emsg(IObuff);
3475 }
3476}
3477
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478#endif /* defined(FEAT_NETBEANS_INTG) */