xref: /vim-8.2.3635/src/netbeans.c (revision a0122dcd)
1 /* vi:set ts=8 sts=4 sw=4 noet:
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  * The Netbeans messages are received and queued in the gui event loop, or in
19  * the select loop when Vim runs in a terminal. These messages are processed
20  * by netbeans_parse_messages() which is invoked in the idle loop when Vim is
21  * waiting for user input. The function netbeans_parse_messages() is also
22  * called from the ":sleep" command, to allow the execution of test cases that
23  * may not invoke the idle loop.
24  */
25 
26 #include "vim.h"
27 
28 #if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
29 
30 #ifndef MSWIN
31 # include <netdb.h>
32 # ifdef HAVE_LIBGEN_H
33 #  include <libgen.h>
34 # endif
35 #endif
36 
37 #include "version.h"
38 
39 #define GUARDED		10000 // typenr for "guarded" annotation
40 #define GUARDEDOFFSET 1000000 // base for "guarded" sign id's
41 #define MAX_COLOR_LENGTH 32 // max length of color name in defineAnnoType
42 
43 // The first implementation (working only with Netbeans) returned "1.1".  The
44 // protocol implemented here also supports A-A-P.
45 static char *ExtEdProtocolVersion = "2.5";
46 
47 static long pos2off(buf_T *, pos_T *);
48 static pos_T *off2pos(buf_T *, long);
49 static pos_T *get_off_or_lnum(buf_T *buf, char_u **argp);
50 static long get_buf_size(buf_T *);
51 static int netbeans_keystring(char_u *keystr);
52 static void special_keys(char_u *args);
53 
54 static int getConnInfo(char *file, char **host, char **port, char **password);
55 
56 static void nb_init_graphics(void);
57 static void coloncmd(char *cmd, ...);
58 static void nb_set_curbuf(buf_T *buf);
59 static void nb_parse_cmd(char_u *);
60 static int  nb_do_cmd(int, char_u *, int, int, char_u *);
61 static void nb_send(char *buf, char *fun);
62 static void nb_free(void);
63 
64 #define NETBEANS_OPEN (channel_can_write_to(nb_channel))
65 static channel_T *nb_channel = NULL;
66 
67 static int r_cmdno;			// current command number for reply
68 static int dosetvisible = FALSE;
69 
70 /*
71  * Include the debugging code if wanted.
72  */
73 #ifdef NBDEBUG
74 # include "nbdebug.c"
75 #endif
76 
77 static int needupdate = 0;
78 static int inAtomic = 0;
79 
80 /*
81  * Callback invoked when the channel is closed.
82  */
83     static void
84 nb_channel_closed(void)
85 {
86     nb_channel = NULL;
87 }
88 
89 /*
90  * Close the connection and cleanup.
91  * May be called when the socket was closed earlier.
92  */
93     static void
94 netbeans_close(void)
95 {
96     if (NETBEANS_OPEN)
97     {
98 	netbeans_send_disconnect();
99 	if (nb_channel != NULL)
100 	{
101 	    // Close the socket and remove the input handlers.
102 	    channel_close(nb_channel, TRUE);
103 	    channel_clear(nb_channel);
104 	}
105 	nb_channel = NULL;
106     }
107 
108 #ifdef FEAT_BEVAL_GUI
109     bevalServers &= ~BEVAL_NETBEANS;
110 #endif
111 
112     needupdate = 0;
113     inAtomic = 0;
114     nb_free();
115 
116     // remove all signs and update the screen after gutter removal
117     coloncmd(":sign unplace *");
118     changed_window_setting();
119     update_screen(CLEAR);
120     setcursor();
121     cursor_on();
122     out_flush_cursor(TRUE, FALSE);
123 }
124 
125 #define NB_DEF_HOST "localhost"
126 #define NB_DEF_ADDR "3219"
127 #define NB_DEF_PASS "changeme"
128 
129     static int
130 netbeans_connect(char *params, int doabort)
131 {
132     int		port;
133     char	buf[32];
134     char	*hostname = NULL;
135     char	*address = NULL;
136     char	*password = NULL;
137     char	*fname;
138     char	*arg = NULL;
139 
140     if (*params == '=')
141     {
142 	// "=fname": Read info from specified file.
143 	if (getConnInfo(params + 1, &hostname, &address, &password) == FAIL)
144 	    return FAIL;
145     }
146     else
147     {
148 	if (*params == ':')
149 	    // ":<host>:<addr>:<password>": get info from argument
150 	    arg = params + 1;
151 	if (arg == NULL && (fname = getenv("__NETBEANS_CONINFO")) != NULL)
152 	{
153 	    // "": get info from file specified in environment
154 	    if (getConnInfo(fname, &hostname, &address, &password) == FAIL)
155 		return FAIL;
156 	}
157 	else
158 	{
159 	    if (arg != NULL)
160 	    {
161 		// ":<host>:<addr>:<password>": get info from argument
162 		hostname = arg;
163 		address = strchr(hostname, ':');
164 		if (address != NULL)
165 		{
166 		    *address++ = '\0';
167 		    password = strchr(address, ':');
168 		    if (password != NULL)
169 			*password++ = '\0';
170 		}
171 	    }
172 
173 	    // Get the missing values from the environment.
174 	    if (hostname == NULL || *hostname == '\0')
175 		hostname = getenv("__NETBEANS_HOST");
176 	    if (address == NULL)
177 		address = getenv("__NETBEANS_SOCKET");
178 	    if (password == NULL)
179 		password = getenv("__NETBEANS_VIM_PASSWORD");
180 
181 	    // Move values to allocated memory.
182 	    if (hostname != NULL)
183 		hostname = (char *)vim_strsave((char_u *)hostname);
184 	    if (address != NULL)
185 		address = (char *)vim_strsave((char_u *)address);
186 	    if (password != NULL)
187 		password = (char *)vim_strsave((char_u *)password);
188 	}
189     }
190 
191     // Use the default when a value is missing.
192     if (hostname == NULL || *hostname == '\0')
193     {
194 	vim_free(hostname);
195 	hostname = (char *)vim_strsave((char_u *)NB_DEF_HOST);
196     }
197     if (address == NULL || *address == '\0')
198     {
199 	vim_free(address);
200 	address = (char *)vim_strsave((char_u *)NB_DEF_ADDR);
201     }
202     if (password == NULL || *password == '\0')
203     {
204 	vim_free(password);
205 	password = (char *)vim_strsave((char_u *)NB_DEF_PASS);
206     }
207     if (hostname != NULL && address != NULL && password != NULL)
208     {
209 	port = atoi(address);
210 	nb_channel = channel_open(hostname, port, 3000, nb_channel_closed);
211 	if (nb_channel != NULL)
212 	{
213 	    // success
214 # ifdef FEAT_BEVAL_GUI
215 	    bevalServers |= BEVAL_NETBEANS;
216 # endif
217 
218 	    // success, login
219 	    vim_snprintf(buf, sizeof(buf), "AUTH %s\n", password);
220 	    nb_send(buf, "netbeans_connect");
221 
222 	    sprintf(buf, "0:version=0 \"%s\"\n", ExtEdProtocolVersion);
223 	    nb_send(buf, "externaleditor_version");
224 	}
225     }
226 
227     if (nb_channel == NULL && doabort)
228 	getout(1);
229 
230     vim_free(hostname);
231     vim_free(address);
232     vim_free(password);
233     return NETBEANS_OPEN ? OK : FAIL;
234 }
235 
236 /*
237  * Obtain the NetBeans hostname, port address and password from a file.
238  * Return the strings in allocated memory.
239  * Return FAIL if the file could not be read, OK otherwise (no matter what it
240  * contains).
241  */
242     static int
243 getConnInfo(char *file, char **host, char **port, char **auth)
244 {
245     FILE *fp;
246     char_u buf[BUFSIZ];
247     char_u *lp;
248     char_u *nlp;
249 #ifdef UNIX
250     stat_T	st;
251 
252     /*
253      * For Unix only accept the file when it's not accessible by others.
254      * The open will then fail if we don't own the file.
255      */
256     if (mch_stat(file, &st) == 0 && (st.st_mode & 0077) != 0)
257     {
258 	nbdebug(("Wrong access mode for NetBeans connection info file: \"%s\"\n",
259 								       file));
260 	semsg(_("E668: Wrong access mode for NetBeans connection info file: \"%s\""),
261 									file);
262 	return FAIL;
263     }
264 #endif
265 
266     fp = mch_fopen(file, "r");
267     if (fp == NULL)
268     {
269 	nbdebug(("Cannot open NetBeans connection info file\n"));
270 	PERROR("E660: Cannot open NetBeans connection info file");
271 	return FAIL;
272     }
273 
274     // Read the file. There should be one of each parameter
275     while ((lp = (char_u *)fgets((char *)buf, BUFSIZ, fp)) != NULL)
276     {
277 	if ((nlp = vim_strchr(lp, '\n')) != NULL)
278 	    *nlp = 0;	    // strip off the trailing newline
279 
280 	if (STRNCMP(lp, "host=", 5) == 0)
281 	{
282 	    vim_free(*host);
283 	    *host = (char *)vim_strsave(&buf[5]);
284 	}
285 	else if (STRNCMP(lp, "port=", 5) == 0)
286 	{
287 	    vim_free(*port);
288 	    *port = (char *)vim_strsave(&buf[5]);
289 	}
290 	else if (STRNCMP(lp, "auth=", 5) == 0)
291 	{
292 	    vim_free(*auth);
293 	    *auth = (char *)vim_strsave(&buf[5]);
294 	}
295     }
296     fclose(fp);
297 
298     return OK;
299 }
300 
301 
302 struct keyqueue
303 {
304     char_u	    *keystr;
305     struct keyqueue *next;
306     struct keyqueue *prev;
307 };
308 
309 typedef struct keyqueue keyQ_T;
310 
311 static keyQ_T keyHead; // dummy node, header for circular queue
312 
313 
314 /*
315  * Queue up key commands sent from netbeans.
316  * We store the string, because it may depend on the global mod_mask and
317  * :nbkey doesn't have a key number.
318  */
319     static void
320 postpone_keycommand(char_u *keystr)
321 {
322     keyQ_T *node;
323 
324     node = ALLOC_ONE(keyQ_T);
325     if (node == NULL)
326 	return;  // out of memory, drop the key
327 
328     if (keyHead.next == NULL) // initialize circular queue
329     {
330 	keyHead.next = &keyHead;
331 	keyHead.prev = &keyHead;
332     }
333 
334     // insert node at tail of queue
335     node->next = &keyHead;
336     node->prev = keyHead.prev;
337     keyHead.prev->next = node;
338     keyHead.prev = node;
339 
340     node->keystr = vim_strsave(keystr);
341 }
342 
343 /*
344  * Handle any queued-up NetBeans keycommands to be send.
345  */
346     static void
347 handle_key_queue(void)
348 {
349     int postponed = FALSE;
350 
351     while (!postponed && keyHead.next && keyHead.next != &keyHead)
352     {
353 	// first, unlink the node
354 	keyQ_T *node = keyHead.next;
355 	keyHead.next = node->next;
356 	node->next->prev = node->prev;
357 
358 	// Now, send the keycommand.  This may cause it to be postponed again
359 	// and change keyHead.
360 	if (node->keystr != NULL)
361 	    postponed = !netbeans_keystring(node->keystr);
362 	vim_free(node->keystr);
363 
364 	// Finally, dispose of the node
365 	vim_free(node);
366     }
367 }
368 
369 
370 /*
371  * While there's still a command in the work queue, parse and execute it.
372  */
373     void
374 netbeans_parse_messages(void)
375 {
376     readq_T	*node;
377     char_u	*buffer;
378     char_u	*p;
379     int		own_node;
380 
381     while (nb_channel != NULL)
382     {
383 	node = channel_peek(nb_channel, PART_SOCK);
384 	if (node == NULL)
385 	    break;	// nothing to read
386 
387 	// Locate the end of the first line in the first buffer.
388 	p = channel_first_nl(node);
389 	if (p == NULL)
390 	{
391 	    // Command isn't complete.  If there is no following buffer,
392 	    // return (wait for more). If there is another buffer following,
393 	    // prepend the text to that buffer and delete this one.
394 	    if (channel_collapse(nb_channel, PART_SOCK, TRUE) == FAIL)
395 		return;
396 	    continue;
397 	}
398 
399 	// There is a complete command at the start of the buffer.
400 	// Terminate it with a NUL.  When no more text is following unlink
401 	// the buffer.  Do this before executing, because new buffers can
402 	// be added while busy handling the command.
403 	*p++ = NUL;
404 	if (*p == NUL)
405 	{
406 	    own_node = TRUE;
407 	    buffer = channel_get(nb_channel, PART_SOCK, NULL);
408 	    // "node" is now invalid!
409 	}
410 	else
411 	{
412 	    own_node = FALSE;
413 	    buffer = node->rq_buffer;
414 	}
415 
416 	// Now, parse and execute the commands.  This may set nb_channel to
417 	// NULL if the channel is closed.
418 	nb_parse_cmd(buffer);
419 
420 	if (own_node)
421 	    // buffer finished, dispose of it
422 	    vim_free(buffer);
423 	else if (nb_channel != NULL)
424 	    // more follows, move it to the start
425 	    channel_consume(nb_channel, PART_SOCK, (int)(p - buffer));
426     }
427 }
428 
429 /*
430  * Handle one NUL terminated command.
431  *
432  * format of a command from netbeans:
433  *
434  *    6:setTitle!84 "a.c"
435  *
436  *    bufno
437  *     colon
438  *      cmd
439  *		!
440  *		 cmdno
441  *		    args
442  *
443  * for function calls, the ! is replaced by a /
444  */
445     static void
446 nb_parse_cmd(char_u *cmd)
447 {
448     char	*verb;
449     char	*q;
450     int		bufno;
451     int		isfunc = -1;
452 
453     if (STRCMP(cmd, "DISCONNECT") == 0)
454     {
455 	// We assume the server knows that we can safely exit!
456 	// Disconnect before exiting, Motif hangs in a Select error
457 	// message otherwise.
458 	netbeans_close();
459 	getout(0);
460 	// NOTREACHED
461     }
462 
463     if (STRCMP(cmd, "DETACH") == 0)
464     {
465 	buf_T	*buf;
466 
467 	FOR_ALL_BUFFERS(buf)
468 	    buf->b_has_sign_column = FALSE;
469 
470 	// The IDE is breaking the connection.
471 	netbeans_close();
472 	return;
473     }
474 
475     bufno = strtol((char *)cmd, &verb, 10);
476 
477     if (*verb != ':')
478     {
479 	nbdebug(("    missing colon: %s\n", cmd));
480 	semsg("E627: missing colon: %s", cmd);
481 	return;
482     }
483     ++verb; // skip colon
484 
485     for (q = verb; *q; q++)
486     {
487 	if (*q == '!')
488 	{
489 	    *q++ = NUL;
490 	    isfunc = 0;
491 	    break;
492 	}
493 	else if (*q == '/')
494 	{
495 	    *q++ = NUL;
496 	    isfunc = 1;
497 	    break;
498 	}
499     }
500 
501     if (isfunc < 0)
502     {
503 	nbdebug(("    missing ! or / in: %s\n", cmd));
504 	semsg("E628: missing ! or / in: %s", cmd);
505 	return;
506     }
507 
508     r_cmdno = strtol(q, &q, 10);
509 
510     q = (char *)skipwhite((char_u *)q);
511 
512     if (nb_do_cmd(bufno, (char_u *)verb, isfunc, r_cmdno, (char_u *)q) == FAIL)
513     {
514 #ifdef NBDEBUG
515 	/*
516 	 * This happens because the ExtEd can send a command or 2 after
517 	 * doing a stopDocumentListen command. It doesn't harm anything
518 	 * so I'm disabling it except for debugging.
519 	 */
520 	nbdebug(("nb_parse_cmd: Command error for \"%s\"\n", cmd));
521 	emsg("E629: bad return from nb_do_cmd");
522 #endif
523     }
524 }
525 
526 struct nbbuf_struct
527 {
528     buf_T		*bufp;
529     unsigned int	 fireChanges:1;
530     unsigned int	 initDone:1;
531     unsigned int	 insertDone:1;
532     unsigned int	 modified:1;
533     int			 nbbuf_number;
534     char		*displayname;
535     int			*signmap;
536     short_u		 signmaplen;
537     short_u		 signmapused;
538 };
539 
540 typedef struct nbbuf_struct nbbuf_T;
541 
542 static nbbuf_T *buf_list = NULL;
543 static int buf_list_size = 0;	// size of buf_list
544 static int buf_list_used = 0;	// nr of entries in buf_list actually in use
545 
546 static char **globalsignmap = NULL;
547 static int globalsignmaplen = 0;
548 static int globalsignmapused = 0;
549 
550 static int  mapsigntype(nbbuf_T *, int localsigntype);
551 static void addsigntype(nbbuf_T *, int localsigntype, char_u *typeName,
552 			char_u *tooltip, char_u *glyphfile,
553 			char_u *fg, char_u *bg);
554 static void print_read_msg(nbbuf_T *buf);
555 static void print_save_msg(nbbuf_T *buf, off_T nchars);
556 
557 static int curPCtype = -1;
558 
559 /*
560  * Free netbeans resources.
561  */
562     static void
563 nb_free(void)
564 {
565     keyQ_T *key_node = keyHead.next;
566     nbbuf_T buf;
567     int i;
568 
569     // free the netbeans buffer list
570     for (i = 0; i < buf_list_used; i++)
571     {
572 	buf = buf_list[i];
573 	vim_free(buf.displayname);
574 	vim_free(buf.signmap);
575 	if (buf.bufp != NULL && buf_valid(buf.bufp))
576 	{
577 	    buf.bufp->b_netbeans_file = FALSE;
578 	    buf.bufp->b_was_netbeans_file = FALSE;
579 	}
580     }
581     VIM_CLEAR(buf_list);
582     buf_list_size = 0;
583     buf_list_used = 0;
584 
585     // free the queued key commands
586     while (key_node != NULL && key_node != &keyHead)
587     {
588 	keyQ_T *next = key_node->next;
589 	vim_free(key_node->keystr);
590 	vim_free(key_node);
591 	if (next == &keyHead)
592 	{
593 	    keyHead.next = &keyHead;
594 	    keyHead.prev = &keyHead;
595 	    break;
596 	}
597 	key_node = next;
598     }
599 
600     // free the queued netbeans commands
601     if (nb_channel != NULL)
602 	channel_clear(nb_channel);
603 }
604 
605 /*
606  * Get the Netbeans buffer number for the specified buffer.
607  */
608     static int
609 nb_getbufno(buf_T *bufp)
610 {
611     int i;
612 
613     for (i = 0; i < buf_list_used; i++)
614 	if (buf_list[i].bufp == bufp)
615 	    return i;
616     return -1;
617 }
618 
619 /*
620  * Is this a NetBeans-owned buffer?
621  */
622     int
623 isNetbeansBuffer(buf_T *bufp)
624 {
625     return NETBEANS_OPEN && bufp->b_netbeans_file;
626 }
627 
628 /*
629  * NetBeans and Vim have different undo models. In Vim, the file isn't
630  * changed if changes are undone via the undo command. In NetBeans, once
631  * a change has been made the file is marked as modified until saved. It
632  * doesn't matter if the change was undone.
633  *
634  * So this function is for the corner case where Vim thinks a buffer is
635  * unmodified but NetBeans thinks it IS modified.
636  */
637     int
638 isNetbeansModified(buf_T *bufp)
639 {
640     if (isNetbeansBuffer(bufp))
641     {
642 	int bufno = nb_getbufno(bufp);
643 
644 	if (bufno > 0)
645 	    return buf_list[bufno].modified;
646 	else
647 	    return FALSE;
648     }
649     else
650 	return FALSE;
651 }
652 
653 /*
654  * Given a Netbeans buffer number, return the netbeans buffer.
655  * Returns NULL for 0 or a negative number. A 0 bufno means a
656  * non-buffer related command has been sent.
657  */
658     static nbbuf_T *
659 nb_get_buf(int bufno)
660 {
661     // find or create a buffer with the given number
662     int incr;
663 
664     if (bufno <= 0)
665 	return NULL;
666 
667     if (!buf_list)
668     {
669 	// initialize
670 	buf_list = alloc_clear(100 * sizeof(nbbuf_T));
671 	buf_list_size = 100;
672     }
673     if (bufno >= buf_list_used) // new
674     {
675 	if (bufno >= buf_list_size) // grow list
676 	{
677 	    nbbuf_T	*t_buf_list = buf_list;
678 	    size_t	bufsize;
679 
680 	    incr = bufno - buf_list_size + 90;
681 	    buf_list_size += incr;
682 	    bufsize = buf_list_size * sizeof(nbbuf_T);
683 	    if (bufsize == 0 || bufsize / sizeof(nbbuf_T)
684 						      != (size_t)buf_list_size)
685 	    {
686 		// list size overflow, bail out
687 		return NULL;
688 	    }
689 	    buf_list = vim_realloc(buf_list, bufsize);
690 	    if (buf_list == NULL)
691 	    {
692 		vim_free(t_buf_list);
693 		buf_list_size = 0;
694 		return NULL;
695 	    }
696 	    vim_memset(buf_list + buf_list_size - incr, 0,
697 						      incr * sizeof(nbbuf_T));
698 	}
699 
700 	while (buf_list_used <= bufno)
701 	{
702 	    // Default is to fire text changes.
703 	    buf_list[buf_list_used].fireChanges = 1;
704 	    ++buf_list_used;
705 	}
706     }
707 
708     return buf_list + bufno;
709 }
710 
711 /*
712  * Return the number of buffers that are modified.
713  */
714     static int
715 count_changed_buffers(void)
716 {
717     buf_T	*bufp;
718     int		n;
719 
720     n = 0;
721     FOR_ALL_BUFFERS(bufp)
722 	if (bufp->b_changed)
723 	    ++n;
724     return n;
725 }
726 
727 /*
728  * End the netbeans session.
729  */
730     void
731 netbeans_end(void)
732 {
733     int i;
734     static char buf[128];
735 
736     if (!NETBEANS_OPEN)
737 	return;
738 
739     for (i = 0; i < buf_list_used; i++)
740     {
741 	if (!buf_list[i].bufp)
742 	    continue;
743 	if (netbeansForcedQuit)
744 	{
745 	    // mark as unmodified so NetBeans won't put up dialog on "killed"
746 	    sprintf(buf, "%d:unmodified=%d\n", i, r_cmdno);
747 	    nbdebug(("EVT: %s", buf));
748 	    nb_send(buf, "netbeans_end");
749 	}
750 	sprintf(buf, "%d:killed=%d\n", i, r_cmdno);
751 	nbdebug(("EVT: %s", buf));
752 	// nb_send(buf, "netbeans_end");    avoid "write failed" messages
753 	nb_send(buf, NULL);
754 	buf_list[i].bufp = NULL;
755     }
756 }
757 
758 /*
759  * Send a message to netbeans.
760  * When "fun" is NULL no error is given.
761  */
762     static void
763 nb_send(char *buf, char *fun)
764 {
765     if (nb_channel != NULL)
766 	channel_send(nb_channel, PART_SOCK, (char_u *)buf,
767 						       (int)STRLEN(buf), fun);
768 }
769 
770 /*
771  * Some input received from netbeans requires a response. This function
772  * handles a response with no information (except the command number).
773  */
774     static void
775 nb_reply_nil(int cmdno)
776 {
777     char reply[32];
778 
779     nbdebug(("REP %d: <none>\n", cmdno));
780 
781     // Avoid printing an annoying error message.
782     if (!NETBEANS_OPEN)
783 	return;
784 
785     sprintf(reply, "%d\n", cmdno);
786     nb_send(reply, "nb_reply_nil");
787 }
788 
789 
790 /*
791  * Send a response with text.
792  * "result" must have been quoted already (using nb_quote()).
793  */
794     static void
795 nb_reply_text(int cmdno, char_u *result)
796 {
797     char_u *reply;
798 
799     nbdebug(("REP %d: %s\n", cmdno, (char *)result));
800 
801     reply = alloc(STRLEN(result) + 32);
802     sprintf((char *)reply, "%d %s\n", cmdno, (char *)result);
803     nb_send((char *)reply, "nb_reply_text");
804 
805     vim_free(reply);
806 }
807 
808 
809 /*
810  * Send a response with a number result code.
811  */
812     static void
813 nb_reply_nr(int cmdno, long result)
814 {
815     char reply[32];
816 
817     nbdebug(("REP %d: %ld\n", cmdno, result));
818 
819     sprintf(reply, "%d %ld\n", cmdno, result);
820     nb_send(reply, "nb_reply_nr");
821 }
822 
823 
824 /*
825  * Encode newline, ret, backslash, double quote for transmission to NetBeans.
826  */
827     static char_u *
828 nb_quote(char_u *txt)
829 {
830     char_u *buf = alloc(2 * STRLEN(txt) + 1);
831     char_u *p = txt;
832     char_u *q = buf;
833 
834     if (buf == NULL)
835 	return NULL;
836     for (; *p; p++)
837     {
838 	switch (*p)
839 	{
840 	    case '\"':
841 	    case '\\':
842 		*q++ = '\\'; *q++ = *p; break;
843 	 // case '\t':
844 	 //     *q++ = '\\'; *q++ = 't'; break;
845 	    case '\n':
846 		*q++ = '\\'; *q++ = 'n'; break;
847 	    case '\r':
848 		*q++ = '\\'; *q++ = 'r'; break;
849 	    default:
850 		*q++ = *p;
851 		break;
852 	}
853     }
854     *q = '\0';
855 
856     return buf;
857 }
858 
859 
860 /*
861  * Remove top level double quotes; convert backslashed chars.
862  * Returns an allocated string (NULL for failure).
863  * If "endp" is not NULL it is set to the character after the terminating
864  * quote.
865  */
866     static char *
867 nb_unquote(char_u *p, char_u **endp)
868 {
869     char *result = 0;
870     char *q;
871     int done = 0;
872 
873     // result is never longer than input
874     result = alloc_clear(STRLEN(p) + 1);
875     if (result == NULL)
876 	return NULL;
877 
878     if (*p++ != '"')
879     {
880 	nbdebug(("nb_unquote called with string that doesn't start with a quote!: %s\n",
881 			p));
882 	result[0] = NUL;
883 	return result;
884     }
885 
886     for (q = result; !done && *p != NUL;)
887     {
888 	switch (*p)
889 	{
890 	    case '"':
891 		/*
892 		 * Unbackslashed dquote marks the end, if first char was dquote.
893 		 */
894 		done = 1;
895 		break;
896 
897 	    case '\\':
898 		++p;
899 		switch (*p)
900 		{
901 		    case '\\':	*q++ = '\\';	break;
902 		    case 'n':	*q++ = '\n';	break;
903 		    case 't':	*q++ = '\t';	break;
904 		    case 'r':	*q++ = '\r';	break;
905 		    case '"':	*q++ = '"';	break;
906 		    case NUL:	--p;		break;
907 		    // default: skip over illegal chars
908 		}
909 		++p;
910 		break;
911 
912 	    default:
913 		*q++ = *p++;
914 	}
915     }
916 
917     if (endp != NULL)
918 	*endp = p;
919 
920     return result;
921 }
922 
923 /*
924  * Remove from "first" byte to "last" byte (inclusive), at line "lnum" of the
925  * current buffer.  Remove to end of line when "last" is MAXCOL.
926  */
927     static void
928 nb_partialremove(linenr_T lnum, colnr_T first, colnr_T last)
929 {
930     char_u *oldtext, *newtext;
931     int oldlen;
932     int lastbyte = last;
933 
934     oldtext = ml_get(lnum);
935     oldlen = (int)STRLEN(oldtext);
936     if (first >= (colnr_T)oldlen || oldlen == 0)  // just in case
937 	return;
938     if (lastbyte >= oldlen)
939 	lastbyte = oldlen - 1;
940     newtext = alloc(oldlen - (int)(lastbyte - first));
941     if (newtext != NULL)
942     {
943 	mch_memmove(newtext, oldtext, first);
944 	STRMOVE(newtext + first, oldtext + lastbyte + 1);
945 	nbdebug(("    NEW LINE %ld: %s\n", lnum, newtext));
946 	ml_replace(lnum, newtext, FALSE);
947     }
948 }
949 
950 /*
951  * Replace the "first" line with the concatenation of the "first" and
952  * the "other" line. The "other" line is not removed.
953  */
954     static void
955 nb_joinlines(linenr_T first, linenr_T other)
956 {
957     int len_first, len_other;
958     char_u *p;
959 
960     len_first = (int)STRLEN(ml_get(first));
961     len_other = (int)STRLEN(ml_get(other));
962     p = alloc(len_first + len_other + 1);
963     if (p != NULL)
964     {
965       mch_memmove(p, ml_get(first), len_first);
966       mch_memmove(p + len_first, ml_get(other), len_other + 1);
967       ml_replace(first, p, FALSE);
968     }
969 }
970 
971 #define SKIP_STOP 2
972 #define streq(a,b) (strcmp(a,b) == 0)
973 
974 /*
975  * Do the actual processing of a single netbeans command or function.
976  * The difference between a command and function is that a function
977  * gets a response (it's required) but a command does not.
978  * For arguments see comment for nb_parse_cmd().
979  */
980     static int
981 nb_do_cmd(
982     int		bufno,
983     char_u	*cmd,
984     int		func,
985     int		cmdno,
986     char_u	*args)	    // points to space before arguments or NUL
987 {
988     int		do_update = 0;
989     long	off = 0;
990     nbbuf_T	*buf = nb_get_buf(bufno);
991     static int	skip = 0;
992     int		retval = OK;
993     char	*cp;	    // for when a char pointer is needed
994 
995     nbdebug(("%s %d: (%d) %s %s\n", (func) ? "FUN" : "CMD", cmdno, bufno, cmd,
996 	STRCMP(cmd, "insert") == 0 ? "<text>" : (char *)args));
997 
998     if (func)
999     {
1000 // =====================================================================
1001 	if (streq((char *)cmd, "getModified"))
1002 	{
1003 	    if (buf == NULL || buf->bufp == NULL)
1004 		// Return the number of buffers that are modified.
1005 		nb_reply_nr(cmdno, (long)count_changed_buffers());
1006 	    else
1007 		// Return whether the buffer is modified.
1008 		nb_reply_nr(cmdno, (long)(buf->bufp->b_changed
1009 					   || isNetbeansModified(buf->bufp)));
1010 // =====================================================================
1011 	}
1012 	else if (streq((char *)cmd, "saveAndExit"))
1013 	{
1014 	    // Note: this will exit Vim if successful.
1015 	    coloncmd(":confirm qall");
1016 
1017 	    // We didn't exit: return the number of changed buffers.
1018 	    nb_reply_nr(cmdno, (long)count_changed_buffers());
1019 // =====================================================================
1020 	}
1021 	else if (streq((char *)cmd, "getCursor"))
1022 	{
1023 	    char_u text[200];
1024 
1025 	    // Note: nb_getbufno() may return -1.  This indicates the IDE
1026 	    // didn't assign a number to the current buffer in response to a
1027 	    // fileOpened event.
1028 	    sprintf((char *)text, "%d %ld %d %ld",
1029 		    nb_getbufno(curbuf),
1030 		    (long)curwin->w_cursor.lnum,
1031 		    (int)curwin->w_cursor.col,
1032 		    pos2off(curbuf, &curwin->w_cursor));
1033 	    nb_reply_text(cmdno, text);
1034 // =====================================================================
1035 	}
1036 	else if (streq((char *)cmd, "getAnno"))
1037 	{
1038 	    long linenum = 0;
1039 #ifdef FEAT_SIGNS
1040 	    if (buf == NULL || buf->bufp == NULL)
1041 	    {
1042 		nbdebug(("    Invalid buffer identifier in getAnno\n"));
1043 		emsg("E652: Invalid buffer identifier in getAnno");
1044 		retval = FAIL;
1045 	    }
1046 	    else
1047 	    {
1048 		int serNum;
1049 
1050 		cp = (char *)args;
1051 		serNum = strtol(cp, &cp, 10);
1052 		// If the sign isn't found linenum will be zero.
1053 		linenum = (long)buf_findsign(buf->bufp, serNum, NULL);
1054 	    }
1055 #endif
1056 	    nb_reply_nr(cmdno, linenum);
1057 // =====================================================================
1058 	}
1059 	else if (streq((char *)cmd, "getLength"))
1060 	{
1061 	    long len = 0;
1062 
1063 	    if (buf == NULL || buf->bufp == NULL)
1064 	    {
1065 		nbdebug(("    invalid buffer identifier in getLength\n"));
1066 		emsg("E632: invalid buffer identifier in getLength");
1067 		retval = FAIL;
1068 	    }
1069 	    else
1070 	    {
1071 		len = get_buf_size(buf->bufp);
1072 	    }
1073 	    nb_reply_nr(cmdno, len);
1074 // =====================================================================
1075 	}
1076 	else if (streq((char *)cmd, "getText"))
1077 	{
1078 	    long	len;
1079 	    linenr_T	nlines;
1080 	    char_u	*text = NULL;
1081 	    linenr_T	lno = 1;
1082 	    char_u	*p;
1083 	    char_u	*line;
1084 
1085 	    if (buf == NULL || buf->bufp == NULL)
1086 	    {
1087 		nbdebug(("    invalid buffer identifier in getText\n"));
1088 		emsg("E633: invalid buffer identifier in getText");
1089 		retval = FAIL;
1090 	    }
1091 	    else
1092 	    {
1093 		len = get_buf_size(buf->bufp);
1094 		nlines = buf->bufp->b_ml.ml_line_count;
1095 		text = alloc((len > 0) ? ((len + nlines) * 2) : 4);
1096 		if (text == NULL)
1097 		{
1098 		    nbdebug(("    nb_do_cmd: getText has null text field\n"));
1099 		    retval = FAIL;
1100 		}
1101 		else
1102 		{
1103 		    p = text;
1104 		    *p++ = '\"';
1105 		    for (; lno <= nlines ; lno++)
1106 		    {
1107 			line = nb_quote(ml_get_buf(buf->bufp, lno, FALSE));
1108 			if (line != NULL)
1109 			{
1110 			    STRCPY(p, line);
1111 			    p += STRLEN(line);
1112 			    *p++ = '\\';
1113 			    *p++ = 'n';
1114 			    vim_free(line);
1115 			}
1116 		    }
1117 		    *p++ = '\"';
1118 		    *p = '\0';
1119 		}
1120 	    }
1121 	    if (text == NULL)
1122 		nb_reply_text(cmdno, (char_u *)"");
1123 	    else
1124 	    {
1125 		nb_reply_text(cmdno, text);
1126 		vim_free(text);
1127 	    }
1128 // =====================================================================
1129 	}
1130 	else if (streq((char *)cmd, "remove"))
1131 	{
1132 	    long count;
1133 	    pos_T first, last;
1134 	    pos_T *pos;
1135 	    pos_T *next;
1136 	    linenr_T del_from_lnum, del_to_lnum;  // lines to be deleted as a whole
1137 	    int oldFire = netbeansFireChanges;
1138 	    int oldSuppress = netbeansSuppressNoLines;
1139 	    int wasChanged;
1140 
1141 	    if (skip >= SKIP_STOP)
1142 	    {
1143 		nbdebug(("    Skipping %s command\n", (char *) cmd));
1144 		nb_reply_nil(cmdno);
1145 		return OK;
1146 	    }
1147 
1148 	    if (buf == NULL || buf->bufp == NULL)
1149 	    {
1150 		nbdebug(("    invalid buffer identifier in remove\n"));
1151 		emsg("E634: invalid buffer identifier in remove");
1152 		retval = FAIL;
1153 	    }
1154 	    else
1155 	    {
1156 		netbeansFireChanges = FALSE;
1157 		netbeansSuppressNoLines = TRUE;
1158 
1159 		nb_set_curbuf(buf->bufp);
1160 		wasChanged = buf->bufp->b_changed;
1161 		cp = (char *)args;
1162 		off = strtol(cp, &cp, 10);
1163 		count = strtol(cp, &cp, 10);
1164 		args = (char_u *)cp;
1165 		// delete "count" chars, starting at "off"
1166 		pos = off2pos(buf->bufp, off);
1167 		if (!pos)
1168 		{
1169 		    nbdebug(("    !bad position\n"));
1170 		    nb_reply_text(cmdno, (char_u *)"!bad position");
1171 		    netbeansFireChanges = oldFire;
1172 		    netbeansSuppressNoLines = oldSuppress;
1173 		    return FAIL;
1174 		}
1175 		first = *pos;
1176 		nbdebug(("    FIRST POS: line %ld, col %d\n",
1177 						      first.lnum, first.col));
1178 		pos = off2pos(buf->bufp, off+count-1);
1179 		if (!pos)
1180 		{
1181 		    nbdebug(("    !bad count\n"));
1182 		    nb_reply_text(cmdno, (char_u *)"!bad count");
1183 		    netbeansFireChanges = oldFire;
1184 		    netbeansSuppressNoLines = oldSuppress;
1185 		    return FAIL;
1186 		}
1187 		last = *pos;
1188 		nbdebug(("    LAST POS: line %ld, col %d\n",
1189 							last.lnum, last.col));
1190 		del_from_lnum = first.lnum;
1191 		del_to_lnum = last.lnum;
1192 		do_update = 1;
1193 
1194 		// Get the position of the first byte after the deleted
1195 		// section.  "next" is NULL when deleting to the end of the
1196 		// file.
1197 		next = off2pos(buf->bufp, off + count);
1198 
1199 		// Remove part of the first line.
1200 		if (first.col != 0
1201 				|| (next != NULL && first.lnum == next->lnum))
1202 		{
1203 		    if (first.lnum != last.lnum
1204 			    || (next != NULL && first.lnum != next->lnum))
1205 		    {
1206 			// remove to the end of the first line
1207 			nb_partialremove(first.lnum, first.col,
1208 							     (colnr_T)MAXCOL);
1209 			if (first.lnum == last.lnum)
1210 			{
1211 			    // Partial line to remove includes the end of
1212 			    // line.  Join the line with the next one, have
1213 			    // the next line deleted below.
1214 			    nb_joinlines(first.lnum, next->lnum);
1215 			    del_to_lnum = next->lnum;
1216 			}
1217 		    }
1218 		    else
1219 		    {
1220 			// remove within one line
1221 			nb_partialremove(first.lnum, first.col, last.col);
1222 		    }
1223 		    ++del_from_lnum;  // don't delete the first line
1224 		}
1225 
1226 		// Remove part of the last line.
1227 		if (first.lnum != last.lnum && next != NULL
1228 			&& next->col != 0 && last.lnum == next->lnum)
1229 		{
1230 		    nb_partialremove(last.lnum, 0, last.col);
1231 		    if (del_from_lnum > first.lnum)
1232 		    {
1233 			// Join end of last line to start of first line; last
1234 			// line is deleted below.
1235 			nb_joinlines(first.lnum, last.lnum);
1236 		    }
1237 		    else
1238 			// First line is deleted as a whole, keep the last
1239 			// line.
1240 			--del_to_lnum;
1241 		}
1242 
1243 		// First is partial line; last line to remove includes
1244 		// the end of line; join first line to line following last
1245 		// line; line following last line is deleted below.
1246 		if (first.lnum != last.lnum && del_from_lnum > first.lnum
1247 			&& next != NULL && last.lnum != next->lnum)
1248 		{
1249 		    nb_joinlines(first.lnum, next->lnum);
1250 		    del_to_lnum = next->lnum;
1251 		}
1252 
1253 		// Delete whole lines if there are any.
1254 		if (del_to_lnum >= del_from_lnum)
1255 		{
1256 		    int i;
1257 
1258 		    // delete signs from the lines being deleted
1259 		    for (i = del_from_lnum; i <= del_to_lnum; i++)
1260 		    {
1261 			int id = buf_findsign_id(buf->bufp, (linenr_T)i, NULL);
1262 			if (id > 0)
1263 			{
1264 			    nbdebug(("    Deleting sign %d on line %d\n",
1265 								      id, i));
1266 			    buf_delsign(buf->bufp, 0, id, NULL);
1267 			}
1268 			else
1269 			{
1270 			    nbdebug(("    No sign on line %d\n", i));
1271 			}
1272 		    }
1273 
1274 		    nbdebug(("    Deleting lines %ld through %ld\n",
1275 						 del_from_lnum, del_to_lnum));
1276 		    curwin->w_cursor.lnum = del_from_lnum;
1277 		    curwin->w_cursor.col = 0;
1278 		    del_lines(del_to_lnum - del_from_lnum + 1, FALSE);
1279 		}
1280 
1281 		// Leave cursor at first deleted byte.
1282 		curwin->w_cursor = first;
1283 		check_cursor_lnum();
1284 		buf->bufp->b_changed = wasChanged; // logically unchanged
1285 		netbeansFireChanges = oldFire;
1286 		netbeansSuppressNoLines = oldSuppress;
1287 
1288 		u_blockfree(buf->bufp);
1289 		u_clearall(buf->bufp);
1290 	    }
1291 	    nb_reply_nil(cmdno);
1292 // =====================================================================
1293 	}
1294 	else if (streq((char *)cmd, "insert"))
1295 	{
1296 	    char_u	*to_free;
1297 
1298 	    if (skip >= SKIP_STOP)
1299 	    {
1300 		nbdebug(("    Skipping %s command\n", (char *) cmd));
1301 		nb_reply_nil(cmdno);
1302 		return OK;
1303 	    }
1304 
1305 	    // get offset
1306 	    cp = (char *)args;
1307 	    off = strtol(cp, &cp, 10);
1308 	    args = (char_u *)cp;
1309 
1310 	    // get text to be inserted
1311 	    args = skipwhite(args);
1312 	    args = to_free = (char_u *)nb_unquote(args, NULL);
1313 	    /*
1314 	    nbdebug(("    CHUNK[%d]: %d bytes at offset %d\n",
1315 		    buf->bufp->b_ml.ml_line_count, STRLEN(args), off));
1316 	    */
1317 
1318 	    if (buf == NULL || buf->bufp == NULL)
1319 	    {
1320 		nbdebug(("    invalid buffer identifier in insert\n"));
1321 		emsg("E635: invalid buffer identifier in insert");
1322 		retval = FAIL;
1323 	    }
1324 	    else if (args != NULL)
1325 	    {
1326 		int	ff_detected = EOL_UNKNOWN;
1327 		int	buf_was_empty = (buf->bufp->b_ml.ml_flags & ML_EMPTY);
1328 		size_t	len = 0;
1329 		int	added = 0;
1330 		int	oldFire = netbeansFireChanges;
1331 		int	old_b_changed;
1332 		char_u	*nlp;
1333 		linenr_T lnum;
1334 		linenr_T lnum_start;
1335 		pos_T	*pos;
1336 
1337 		netbeansFireChanges = 0;
1338 
1339 		// Jump to the buffer where we insert.  After this "curbuf"
1340 		// can be used.
1341 		nb_set_curbuf(buf->bufp);
1342 		old_b_changed = curbuf->b_changed;
1343 
1344 		// Convert the specified character offset into a lnum/col
1345 		// position.
1346 		pos = off2pos(curbuf, off);
1347 		if (pos != NULL)
1348 		{
1349 		    if (pos->lnum <= 0)
1350 			lnum_start = 1;
1351 		    else
1352 			lnum_start = pos->lnum;
1353 		}
1354 		else
1355 		{
1356 		    // If the given position is not found, assume we want
1357 		    // the end of the file.  See setLocAndSize HACK.
1358 		    if (buf_was_empty)
1359 			lnum_start = 1;	    // above empty line
1360 		    else
1361 			lnum_start = curbuf->b_ml.ml_line_count + 1;
1362 		}
1363 
1364 		// "lnum" is the line where we insert: either append to it or
1365 		// insert a new line above it.
1366 		lnum = lnum_start;
1367 
1368 		// Loop over the "\n" separated lines of the argument.
1369 		do_update = 1;
1370 		while (*args != NUL)
1371 		{
1372 		    nlp = vim_strchr(args, '\n');
1373 		    if (nlp == NULL)
1374 		    {
1375 			// Incomplete line, probably truncated.  Next "insert"
1376 			// command should append to this one.
1377 			len = STRLEN(args);
1378 		    }
1379 		    else
1380 		    {
1381 			len = nlp - args;
1382 
1383 			/*
1384 			 * We need to detect EOL style, because the commands
1385 			 * use a character offset.
1386 			 */
1387 			if (nlp > args && nlp[-1] == '\r')
1388 			{
1389 			    ff_detected = EOL_DOS;
1390 			    --len;
1391 			}
1392 			else
1393 			    ff_detected = EOL_UNIX;
1394 		    }
1395 		    args[len] = NUL;
1396 
1397 		    if (lnum == lnum_start
1398 			    && ((pos != NULL && pos->col > 0)
1399 				|| (lnum == 1 && buf_was_empty)))
1400 		    {
1401 			char_u	*oldline = ml_get(lnum);
1402 			char_u	*newline;
1403 			int	col = pos == NULL ? 0 : pos->col;
1404 
1405 			// Insert halfway a line.
1406 			newline = alloc(STRLEN(oldline) + len + 1);
1407 			if (newline != NULL)
1408 			{
1409 			    mch_memmove(newline, oldline, (size_t)col);
1410 			    newline[col] = NUL;
1411 			    STRCAT(newline, args);
1412 			    STRCAT(newline, oldline + col);
1413 			    ml_replace(lnum, newline, FALSE);
1414 			}
1415 		    }
1416 		    else
1417 		    {
1418 			// Append a new line.  Not that we always do this,
1419 			// also when the text doesn't end in a "\n".
1420 			ml_append((linenr_T)(lnum - 1), args,
1421 						   (colnr_T)(len + 1), FALSE);
1422 			++added;
1423 		    }
1424 
1425 		    if (nlp == NULL)
1426 			break;
1427 		    ++lnum;
1428 		    args = nlp + 1;
1429 		}
1430 
1431 		// Adjust the marks below the inserted lines.
1432 		appended_lines_mark(lnum_start - 1, (long)added);
1433 
1434 		/*
1435 		 * When starting with an empty buffer set the fileformat.
1436 		 * This is just guessing...
1437 		 */
1438 		if (buf_was_empty)
1439 		{
1440 		    if (ff_detected == EOL_UNKNOWN)
1441 #if defined(MSWIN)
1442 			ff_detected = EOL_DOS;
1443 #else
1444 			ff_detected = EOL_UNIX;
1445 #endif
1446 		    set_fileformat(ff_detected, OPT_LOCAL);
1447 		    curbuf->b_start_ffc = *curbuf->b_p_ff;
1448 		}
1449 
1450 		/*
1451 		 * XXX - GRP - Is the next line right? If I've inserted
1452 		 * text the buffer has been updated but not written. Will
1453 		 * netbeans guarantee to write it? Even if I do a :q! ?
1454 		 */
1455 		curbuf->b_changed = old_b_changed; // logically unchanged
1456 		netbeansFireChanges = oldFire;
1457 
1458 		// Undo info is invalid now...
1459 		u_blockfree(curbuf);
1460 		u_clearall(curbuf);
1461 	    }
1462 	    vim_free(to_free);
1463 	    nb_reply_nil(cmdno); // or !error
1464 	}
1465 	else
1466 	{
1467 	    nbdebug(("UNIMPLEMENTED FUNCTION: %s\n", cmd));
1468 	    nb_reply_nil(cmdno);
1469 	    retval = FAIL;
1470 	}
1471     }
1472     else // Not a function; no reply required.
1473     {
1474 // =====================================================================
1475 	if (streq((char *)cmd, "create"))
1476 	{
1477 	    // Create a buffer without a name.
1478 	    if (buf == NULL)
1479 	    {
1480 		nbdebug(("    invalid buffer identifier in create\n"));
1481 		emsg("E636: invalid buffer identifier in create");
1482 		return FAIL;
1483 	    }
1484 	    VIM_CLEAR(buf->displayname);
1485 
1486 	    netbeansReadFile = 0; // don't try to open disk file
1487 	    do_ecmd(0, NULL, 0, 0, ECMD_ONE, ECMD_HIDE + ECMD_OLDBUF, curwin);
1488 	    netbeansReadFile = 1;
1489 	    buf->bufp = curbuf;
1490 	    maketitle();
1491 	    buf->insertDone = FALSE;
1492 #if defined(FEAT_MENU) && defined(FEAT_GUI)
1493 	    if (gui.in_use)
1494 		gui_update_menus(0);
1495 #endif
1496 // =====================================================================
1497 	}
1498 	else if (streq((char *)cmd, "insertDone"))
1499 	{
1500 	    if (buf == NULL || buf->bufp == NULL)
1501 	    {
1502 		nbdebug(("    invalid buffer identifier in insertDone\n"));
1503 	    }
1504 	    else
1505 	    {
1506 		buf->bufp->b_start_eol = *args == 'T';
1507 		buf->insertDone = TRUE;
1508 		args += 2;
1509 		buf->bufp->b_p_ro = *args == 'T';
1510 		print_read_msg(buf);
1511 	    }
1512 // =====================================================================
1513 	}
1514 	else if (streq((char *)cmd, "saveDone"))
1515 	{
1516 	    long savedChars = atol((char *)args);
1517 
1518 	    if (buf == NULL || buf->bufp == NULL)
1519 		nbdebug(("    invalid buffer identifier in saveDone\n"));
1520 	    else
1521 		print_save_msg(buf, savedChars);
1522 // =====================================================================
1523 	}
1524 	else if (streq((char *)cmd, "startDocumentListen"))
1525 	{
1526 	    if (buf == NULL)
1527 	    {
1528 		nbdebug(("    invalid buffer identifier in startDocumentListen\n"));
1529 		emsg("E637: invalid buffer identifier in startDocumentListen");
1530 		return FAIL;
1531 	    }
1532 	    buf->fireChanges = 1;
1533 // =====================================================================
1534 	}
1535 	else if (streq((char *)cmd, "stopDocumentListen"))
1536 	{
1537 	    if (buf == NULL)
1538 	    {
1539 		nbdebug(("    invalid buffer identifier in stopDocumentListen\n"));
1540 		emsg("E638: invalid buffer identifier in stopDocumentListen");
1541 		return FAIL;
1542 	    }
1543 	    buf->fireChanges = 0;
1544 	    if (buf->bufp != NULL && buf->bufp->b_was_netbeans_file)
1545 	    {
1546 		if (!buf->bufp->b_netbeans_file)
1547 		{
1548 		    nbdebug(("E658: NetBeans connection lost for buffer %d\n", buf->bufp->b_fnum));
1549 		    semsg(_("E658: NetBeans connection lost for buffer %d"),
1550 							   buf->bufp->b_fnum);
1551 		}
1552 		else
1553 		{
1554 		    // NetBeans uses stopDocumentListen when it stops editing
1555 		    // a file.  It then expects the buffer in Vim to
1556 		    // disappear.
1557 		    do_bufdel(DOBUF_DEL, (char_u *)"", 1,
1558 				  buf->bufp->b_fnum, buf->bufp->b_fnum, TRUE);
1559 		    CLEAR_POINTER(buf);
1560 		}
1561 	    }
1562 // =====================================================================
1563 	}
1564 	else if (streq((char *)cmd, "setTitle"))
1565 	{
1566 	    if (buf == NULL)
1567 	    {
1568 		nbdebug(("    invalid buffer identifier in setTitle\n"));
1569 		emsg("E639: invalid buffer identifier in setTitle");
1570 		return FAIL;
1571 	    }
1572 	    vim_free(buf->displayname);
1573 	    buf->displayname = nb_unquote(args, NULL);
1574 // =====================================================================
1575 	}
1576 	else if (streq((char *)cmd, "initDone"))
1577 	{
1578 	    if (buf == NULL || buf->bufp == NULL)
1579 	    {
1580 		nbdebug(("    invalid buffer identifier in initDone\n"));
1581 		emsg("E640: invalid buffer identifier in initDone");
1582 		return FAIL;
1583 	    }
1584 	    do_update = 1;
1585 	    buf->initDone = TRUE;
1586 	    nb_set_curbuf(buf->bufp);
1587 	    apply_autocmds(EVENT_BUFREADPOST, 0, 0, FALSE, buf->bufp);
1588 
1589 	    // handle any postponed key commands
1590 	    handle_key_queue();
1591 // =====================================================================
1592 	}
1593 	else if (streq((char *)cmd, "setBufferNumber")
1594 		|| streq((char *)cmd, "putBufferNumber"))
1595 	{
1596 	    char_u	*path;
1597 	    buf_T	*bufp;
1598 
1599 	    if (buf == NULL)
1600 	    {
1601 		nbdebug(("    invalid buffer identifier in setBufferNumber\n"));
1602 		emsg("E641: invalid buffer identifier in setBufferNumber");
1603 		return FAIL;
1604 	    }
1605 	    path = (char_u *)nb_unquote(args, NULL);
1606 	    if (path == NULL)
1607 		return FAIL;
1608 	    bufp = buflist_findname(path);
1609 	    vim_free(path);
1610 	    if (bufp == NULL)
1611 	    {
1612 		nbdebug(("    File %s not found in setBufferNumber\n", args));
1613 		semsg("E642: File %s not found in setBufferNumber", args);
1614 		return FAIL;
1615 	    }
1616 	    buf->bufp = bufp;
1617 	    buf->nbbuf_number = bufp->b_fnum;
1618 
1619 	    // "setBufferNumber" has the side effect of jumping to the buffer
1620 	    // (don't know why!).  Don't do that for "putBufferNumber".
1621 	    if (*cmd != 'p')
1622 		coloncmd(":buffer %d", bufp->b_fnum);
1623 	    else
1624 	    {
1625 		buf->initDone = TRUE;
1626 
1627 		// handle any postponed key commands
1628 		handle_key_queue();
1629 	    }
1630 
1631 // =====================================================================
1632 	}
1633 	else if (streq((char *)cmd, "setFullName"))
1634 	{
1635 	    if (buf == NULL)
1636 	    {
1637 		nbdebug(("    invalid buffer identifier in setFullName\n"));
1638 		emsg("E643: invalid buffer identifier in setFullName");
1639 		return FAIL;
1640 	    }
1641 	    vim_free(buf->displayname);
1642 	    buf->displayname = nb_unquote(args, NULL);
1643 
1644 	    netbeansReadFile = 0; // don't try to open disk file
1645 	    do_ecmd(0, (char_u *)buf->displayname, 0, 0, ECMD_ONE,
1646 					     ECMD_HIDE + ECMD_OLDBUF, curwin);
1647 	    netbeansReadFile = 1;
1648 	    buf->bufp = curbuf;
1649 	    maketitle();
1650 #if defined(FEAT_MENU) && defined(FEAT_GUI)
1651 	    if (gui.in_use)
1652 		gui_update_menus(0);
1653 #endif
1654 // =====================================================================
1655 	}
1656 	else if (streq((char *)cmd, "editFile"))
1657 	{
1658 	    if (buf == NULL)
1659 	    {
1660 		nbdebug(("    invalid buffer identifier in editFile\n"));
1661 		emsg("E644: invalid buffer identifier in editFile");
1662 		return FAIL;
1663 	    }
1664 	    // Edit a file: like create + setFullName + read the file.
1665 	    vim_free(buf->displayname);
1666 	    buf->displayname = nb_unquote(args, NULL);
1667 	    do_ecmd(0, (char_u *)buf->displayname, NULL, NULL, ECMD_ONE,
1668 					     ECMD_HIDE + ECMD_OLDBUF, curwin);
1669 	    buf->bufp = curbuf;
1670 	    buf->initDone = TRUE;
1671 	    do_update = 1;
1672 #if defined(FEAT_TITLE)
1673 	    maketitle();
1674 #endif
1675 #if defined(FEAT_MENU) && defined(FEAT_GUI)
1676 	    if (gui.in_use)
1677 		gui_update_menus(0);
1678 #endif
1679 // =====================================================================
1680 	}
1681 	else if (streq((char *)cmd, "setVisible"))
1682 	{
1683 	    if (buf == NULL || buf->bufp == NULL)
1684 	    {
1685 		nbdebug(("    invalid buffer identifier in setVisible\n"));
1686 		// This message was commented out, probably because it can
1687 		// happen when shutting down.
1688 		if (p_verbose > 0)
1689 		    emsg("E645: invalid buffer identifier in setVisible");
1690 		return FAIL;
1691 	    }
1692 	    if (streq((char *)args, "T") && buf->bufp != curbuf)
1693 	    {
1694 		exarg_T exarg;
1695 		exarg.cmd = (char_u *)"goto";
1696 		exarg.forceit = FALSE;
1697 		dosetvisible = TRUE;
1698 		goto_buffer(&exarg, DOBUF_FIRST, FORWARD, buf->bufp->b_fnum);
1699 		do_update = 1;
1700 		dosetvisible = FALSE;
1701 
1702 #ifdef FEAT_GUI
1703 		// Side effect!!!.
1704 		if (gui.in_use)
1705 		    gui_mch_set_foreground();
1706 #endif
1707 	    }
1708 // =====================================================================
1709 	}
1710 	else if (streq((char *)cmd, "raise"))
1711 	{
1712 #ifdef FEAT_GUI
1713 	    // Bring gvim to the foreground.
1714 	    if (gui.in_use)
1715 		gui_mch_set_foreground();
1716 #endif
1717 // =====================================================================
1718 	}
1719 	else if (streq((char *)cmd, "setModified"))
1720 	{
1721 	    int prev_b_changed;
1722 
1723 	    if (buf == NULL || buf->bufp == NULL)
1724 	    {
1725 		nbdebug(("    invalid buffer identifier in setModified\n"));
1726 		// This message was commented out, probably because it can
1727 		// happen when shutting down.
1728 		if (p_verbose > 0)
1729 		    emsg("E646: invalid buffer identifier in setModified");
1730 		return FAIL;
1731 	    }
1732 	    prev_b_changed = buf->bufp->b_changed;
1733 	    if (streq((char *)args, "T"))
1734 		buf->bufp->b_changed = TRUE;
1735 	    else
1736 	    {
1737 		stat_T	st;
1738 
1739 		// Assume NetBeans stored the file.  Reset the timestamp to
1740 		// avoid "file changed" warnings.
1741 		if (buf->bufp->b_ffname != NULL
1742 			&& mch_stat((char *)buf->bufp->b_ffname, &st) >= 0)
1743 		    buf_store_time(buf->bufp, &st, buf->bufp->b_ffname);
1744 		buf->bufp->b_changed = FALSE;
1745 	    }
1746 	    buf->modified = buf->bufp->b_changed;
1747 	    if (prev_b_changed != buf->bufp->b_changed)
1748 	    {
1749 		check_status(buf->bufp);
1750 		redraw_tabline = TRUE;
1751 #ifdef FEAT_TITLE
1752 		maketitle();
1753 #endif
1754 		update_screen(0);
1755 	    }
1756 // =====================================================================
1757 	}
1758 	else if (streq((char *)cmd, "setModtime"))
1759 	{
1760 	    if (buf == NULL || buf->bufp == NULL)
1761 		nbdebug(("    invalid buffer identifier in setModtime\n"));
1762 	    else
1763 		buf->bufp->b_mtime = atoi((char *)args);
1764 // =====================================================================
1765 	}
1766 	else if (streq((char *)cmd, "setReadOnly"))
1767 	{
1768 	    if (buf == NULL || buf->bufp == NULL)
1769 		nbdebug(("    invalid buffer identifier in setReadOnly\n"));
1770 	    else if (streq((char *)args, "T"))
1771 		buf->bufp->b_p_ro = TRUE;
1772 	    else
1773 		buf->bufp->b_p_ro = FALSE;
1774 // =====================================================================
1775 	}
1776 	else if (streq((char *)cmd, "setMark"))
1777 	{
1778 	    // not yet
1779 // =====================================================================
1780 	}
1781 	else if (streq((char *)cmd, "showBalloon"))
1782 	{
1783 #if defined(FEAT_BEVAL_GUI)
1784 	    static char	*text = NULL;
1785 
1786 	    /*
1787 	     * Set up the Balloon Expression Evaluation area.
1788 	     * Ignore 'ballooneval' here.
1789 	     * The text pointer must remain valid for a while.
1790 	     */
1791 	    if (balloonEval != NULL)
1792 	    {
1793 		vim_free(text);
1794 		text = nb_unquote(args, NULL);
1795 		if (text != NULL)
1796 		    gui_mch_post_balloon(balloonEval, (char_u *)text);
1797 	    }
1798 #endif
1799 // =====================================================================
1800 	}
1801 	else if (streq((char *)cmd, "setDot"))
1802 	{
1803 	    pos_T *pos;
1804 #ifdef NBDEBUG
1805 	    char_u *s;
1806 #endif
1807 
1808 	    if (buf == NULL || buf->bufp == NULL)
1809 	    {
1810 		nbdebug(("    invalid buffer identifier in setDot\n"));
1811 		emsg("E647: invalid buffer identifier in setDot");
1812 		return FAIL;
1813 	    }
1814 
1815 	    nb_set_curbuf(buf->bufp);
1816 
1817 	    // Don't want Visual mode now.
1818 	    if (VIsual_active)
1819 		end_visual_mode();
1820 #ifdef NBDEBUG
1821 	    s = args;
1822 #endif
1823 	    pos = get_off_or_lnum(buf->bufp, &args);
1824 	    if (pos)
1825 	    {
1826 		curwin->w_cursor = *pos;
1827 		check_cursor();
1828 #ifdef FEAT_FOLDING
1829 		foldOpenCursor();
1830 #endif
1831 	    }
1832 	    else
1833 	    {
1834 		nbdebug(("    BAD POSITION in setDot: %s\n", s));
1835 	    }
1836 
1837 	    // gui_update_cursor(TRUE, FALSE);
1838 	    // update_curbuf(NOT_VALID);
1839 	    update_topline();		// scroll to show the line
1840 	    update_screen(VALID);
1841 	    setcursor();
1842 	    cursor_on();
1843 	    out_flush_cursor(TRUE, FALSE);
1844 
1845 	    // Quit a hit-return or more prompt.
1846 	    if (State == HITRETURN || State == ASKMORE)
1847 	    {
1848 #ifdef FEAT_GUI_GTK
1849 		if (gui.in_use && gtk_main_level() > 0)
1850 		    gtk_main_quit();
1851 #endif
1852 	    }
1853 // =====================================================================
1854 	}
1855 	else if (streq((char *)cmd, "close"))
1856 	{
1857 #ifdef NBDEBUG
1858 	    char *name = "<NONE>";
1859 #endif
1860 
1861 	    if (buf == NULL)
1862 	    {
1863 		nbdebug(("    invalid buffer identifier in close\n"));
1864 		emsg("E648: invalid buffer identifier in close");
1865 		return FAIL;
1866 	    }
1867 
1868 #ifdef NBDEBUG
1869 	    if (buf->displayname != NULL)
1870 		name = buf->displayname;
1871 #endif
1872 	    if (buf->bufp == NULL)
1873 	    {
1874 		nbdebug(("    invalid buffer identifier in close\n"));
1875 		// This message was commented out, probably because it can
1876 		// happen when shutting down.
1877 		if (p_verbose > 0)
1878 		    emsg("E649: invalid buffer identifier in close");
1879 	    }
1880 	    nbdebug(("    CLOSE %d: %s\n", bufno, name));
1881 #ifdef FEAT_GUI
1882 	    need_mouse_correct = TRUE;
1883 #endif
1884 	    if (buf->bufp != NULL)
1885 		do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD,
1886 						     buf->bufp->b_fnum, TRUE);
1887 	    buf->bufp = NULL;
1888 	    buf->initDone = FALSE;
1889 	    do_update = 1;
1890 // =====================================================================
1891 	}
1892 	else if (streq((char *)cmd, "setStyle")) // obsolete...
1893 	{
1894 	    nbdebug(("    setStyle is obsolete!\n"));
1895 // =====================================================================
1896 	}
1897 	else if (streq((char *)cmd, "setExitDelay"))
1898 	{
1899 	    // Only used in version 2.1.
1900 // =====================================================================
1901 	}
1902 	else if (streq((char *)cmd, "defineAnnoType"))
1903 	{
1904 #ifdef FEAT_SIGNS
1905 	    int typeNum;
1906 	    char_u *typeName;
1907 	    char_u *tooltip;
1908 	    char_u *p;
1909 	    char_u *glyphFile;
1910 	    int parse_error = FALSE;
1911 	    char_u *fg;
1912 	    char_u *bg;
1913 
1914 	    if (buf == NULL)
1915 	    {
1916 		nbdebug(("    invalid buffer identifier in defineAnnoType\n"));
1917 		emsg("E650: invalid buffer identifier in defineAnnoType");
1918 		return FAIL;
1919 	    }
1920 
1921 	    cp = (char *)args;
1922 	    typeNum = strtol(cp, &cp, 10);
1923 	    args = (char_u *)cp;
1924 	    args = skipwhite(args);
1925 	    typeName = (char_u *)nb_unquote(args, &args);
1926 	    args = skipwhite(args + 1);
1927 	    tooltip = (char_u *)nb_unquote(args, &args);
1928 	    args = skipwhite(args + 1);
1929 
1930 	    p = (char_u *)nb_unquote(args, &args);
1931 	    glyphFile = vim_strsave_escaped(p, escape_chars);
1932 	    vim_free(p);
1933 
1934 	    args = skipwhite(args + 1);
1935 	    p = skiptowhite(args);
1936 	    if (*p != NUL)
1937 	    {
1938 		*p = NUL;
1939 		p = skipwhite(p + 1);
1940 	    }
1941 	    fg = vim_strsave(args);
1942 	    bg = vim_strsave(p);
1943 	    if (STRLEN(fg) > MAX_COLOR_LENGTH || STRLEN(bg) > MAX_COLOR_LENGTH)
1944 	    {
1945 		emsg("E532: highlighting color name too long in defineAnnoType");
1946 		VIM_CLEAR(typeName);
1947 		parse_error = TRUE;
1948 	    }
1949 	    else if (typeName != NULL && tooltip != NULL && glyphFile != NULL)
1950 		addsigntype(buf, typeNum, typeName, tooltip, glyphFile, fg, bg);
1951 
1952 	    vim_free(typeName);
1953 	    vim_free(fg);
1954 	    vim_free(bg);
1955 	    vim_free(tooltip);
1956 	    vim_free(glyphFile);
1957 	    if (parse_error)
1958 		return FAIL;
1959 
1960 #endif
1961 // =====================================================================
1962 	}
1963 	else if (streq((char *)cmd, "addAnno"))
1964 	{
1965 #ifdef FEAT_SIGNS
1966 	    int serNum;
1967 	    int localTypeNum;
1968 	    int typeNum;
1969 	    pos_T *pos;
1970 
1971 	    if (buf == NULL || buf->bufp == NULL)
1972 	    {
1973 		nbdebug(("    invalid buffer identifier in addAnno\n"));
1974 		emsg("E651: invalid buffer identifier in addAnno");
1975 		return FAIL;
1976 	    }
1977 
1978 	    do_update = 1;
1979 
1980 	    cp = (char *)args;
1981 	    serNum = strtol(cp, &cp, 10);
1982 
1983 	    // Get the typenr specific for this buffer and convert it to
1984 	    // the global typenumber, as used for the sign name.
1985 	    localTypeNum = strtol(cp, &cp, 10);
1986 	    args = (char_u *)cp;
1987 	    typeNum = mapsigntype(buf, localTypeNum);
1988 
1989 	    pos = get_off_or_lnum(buf->bufp, &args);
1990 
1991 	    cp = (char *)args;
1992 	    vim_ignored = (int)strtol(cp, &cp, 10);
1993 	    args = (char_u *)cp;
1994 # ifdef NBDEBUG
1995 	    if (vim_ignored != -1)
1996 		nbdebug(("    partial line annotation -- Not Yet Implemented!\n"));
1997 # endif
1998 	    if (serNum >= GUARDEDOFFSET)
1999 	    {
2000 		nbdebug(("    too many annotations! ignoring...\n"));
2001 		return FAIL;
2002 	    }
2003 	    if (pos)
2004 	    {
2005 		coloncmd(":sign place %d line=%ld name=%d buffer=%d",
2006 			   serNum, pos->lnum, typeNum, buf->bufp->b_fnum);
2007 		if (typeNum == curPCtype)
2008 		    coloncmd(":sign jump %d buffer=%d", serNum,
2009 						       buf->bufp->b_fnum);
2010 	    }
2011 #endif
2012 // =====================================================================
2013 	}
2014 	else if (streq((char *)cmd, "removeAnno"))
2015 	{
2016 #ifdef FEAT_SIGNS
2017 	    int serNum;
2018 
2019 	    if (buf == NULL || buf->bufp == NULL)
2020 	    {
2021 		nbdebug(("    invalid buffer identifier in removeAnno\n"));
2022 		return FAIL;
2023 	    }
2024 	    do_update = 1;
2025 	    cp = (char *)args;
2026 	    serNum = strtol(cp, &cp, 10);
2027 	    args = (char_u *)cp;
2028 	    coloncmd(":sign unplace %d buffer=%d",
2029 		     serNum, buf->bufp->b_fnum);
2030 	    redraw_buf_later(buf->bufp, NOT_VALID);
2031 #endif
2032 // =====================================================================
2033 	}
2034 	else if (streq((char *)cmd, "moveAnnoToFront"))
2035 	{
2036 #ifdef FEAT_SIGNS
2037 	    nbdebug(("    moveAnnoToFront: Not Yet Implemented!\n"));
2038 #endif
2039 // =====================================================================
2040 	}
2041 	else if (streq((char *)cmd, "guard") || streq((char *)cmd, "unguard"))
2042 	{
2043 	    int len;
2044 	    pos_T first;
2045 	    pos_T last;
2046 	    pos_T *pos;
2047 	    int un = (cmd[0] == 'u');
2048 	    static int guardId = GUARDEDOFFSET;
2049 
2050 	    if (skip >= SKIP_STOP)
2051 	    {
2052 		nbdebug(("    Skipping %s command\n", (char *) cmd));
2053 		return OK;
2054 	    }
2055 
2056 	    nb_init_graphics();
2057 
2058 	    if (buf == NULL || buf->bufp == NULL)
2059 	    {
2060 		nbdebug(("    invalid buffer identifier in %s command\n", cmd));
2061 		return FAIL;
2062 	    }
2063 	    nb_set_curbuf(buf->bufp);
2064 	    cp = (char *)args;
2065 	    off = strtol(cp, &cp, 10);
2066 	    len = strtol(cp, NULL, 10);
2067 	    args = (char_u *)cp;
2068 	    pos = off2pos(buf->bufp, off);
2069 	    do_update = 1;
2070 	    if (!pos)
2071 		nbdebug(("    no such start pos in %s, %ld\n", cmd, off));
2072 	    else
2073 	    {
2074 		first = *pos;
2075 		pos = off2pos(buf->bufp, off + len - 1);
2076 		if (pos != NULL && pos->col == 0)
2077 		{
2078 			/*
2079 			 * In Java Swing the offset is a position between 2
2080 			 * characters. If col == 0 then we really want the
2081 			 * previous line as the end.
2082 			 */
2083 			pos = off2pos(buf->bufp, off + len - 2);
2084 		}
2085 		if (!pos)
2086 		    nbdebug(("    no such end pos in %s, %ld\n",
2087 			    cmd, off + len - 1));
2088 		else
2089 		{
2090 		    long lnum;
2091 		    last = *pos;
2092 		    // set highlight for region
2093 		    nbdebug(("    %sGUARD %ld,%d to %ld,%d\n", (un) ? "UN" : "",
2094 			     first.lnum, first.col,
2095 			     last.lnum, last.col));
2096 #ifdef FEAT_SIGNS
2097 		    for (lnum = first.lnum; lnum <= last.lnum; lnum++)
2098 		    {
2099 			if (un)
2100 			{
2101 			    // never used
2102 			}
2103 			else
2104 			{
2105 			    if (buf_findsigntype_id(buf->bufp, lnum,
2106 				GUARDED) == 0)
2107 			    {
2108 				coloncmd(
2109 				    ":sign place %d line=%ld name=%d buffer=%d",
2110 				     guardId++, lnum, GUARDED,
2111 				     buf->bufp->b_fnum);
2112 			    }
2113 			}
2114 		    }
2115 #endif
2116 		    redraw_buf_later(buf->bufp, NOT_VALID);
2117 		}
2118 	    }
2119 // =====================================================================
2120 	}
2121 	else if (streq((char *)cmd, "startAtomic"))
2122 	{
2123 	    inAtomic = 1;
2124 // =====================================================================
2125 	}
2126 	else if (streq((char *)cmd, "endAtomic"))
2127 	{
2128 	    inAtomic = 0;
2129 	    if (needupdate)
2130 	    {
2131 		do_update = 1;
2132 		needupdate = 0;
2133 	    }
2134 // =====================================================================
2135 	}
2136 	else if (streq((char *)cmd, "save"))
2137 	{
2138 	    /*
2139 	     * NOTE - This command is obsolete wrt NetBeans. It's left in
2140 	     * only for historical reasons.
2141 	     */
2142 	    if (buf == NULL || buf->bufp == NULL)
2143 	    {
2144 		nbdebug(("    invalid buffer identifier in %s command\n", cmd));
2145 		return FAIL;
2146 	    }
2147 
2148 	    // the following is taken from ex_cmds.c (do_wqall function)
2149 	    if (bufIsChanged(buf->bufp))
2150 	    {
2151 		// Only write if the buffer can be written.
2152 		if (p_write
2153 			&& !buf->bufp->b_p_ro
2154 			&& buf->bufp->b_ffname != NULL
2155 #ifdef FEAT_QUICKFIX
2156 			&& !bt_dontwrite(buf->bufp)
2157 #endif
2158 			)
2159 		{
2160 		    bufref_T bufref;
2161 
2162 		    set_bufref(&bufref, buf->bufp);
2163 		    buf_write_all(buf->bufp, FALSE);
2164 		    // an autocommand may have deleted the buffer
2165 		    if (!bufref_valid(&bufref))
2166 			buf->bufp = NULL;
2167 		}
2168 	    }
2169 	    else
2170 	    {
2171 		nbdebug(("    Buffer has no changes!\n"));
2172 	    }
2173 // =====================================================================
2174 	}
2175 	else if (streq((char *)cmd, "netbeansBuffer"))
2176 	{
2177 	    if (buf == NULL || buf->bufp == NULL)
2178 	    {
2179 		nbdebug(("    invalid buffer identifier in %s command\n", cmd));
2180 		return FAIL;
2181 	    }
2182 	    if (*args == 'T')
2183 	    {
2184 		buf->bufp->b_netbeans_file = TRUE;
2185 		buf->bufp->b_was_netbeans_file = TRUE;
2186 	    }
2187 	    else
2188 		buf->bufp->b_netbeans_file = FALSE;
2189 // =====================================================================
2190 	}
2191 	else if (streq((char *)cmd, "specialKeys"))
2192 	{
2193 	    special_keys(args);
2194 // =====================================================================
2195 	}
2196 	else if (streq((char *)cmd, "actionMenuItem"))
2197 	{
2198 	    // not used yet
2199 // =====================================================================
2200 	}
2201 	else if (streq((char *)cmd, "version"))
2202 	{
2203 	    // not used yet
2204 	}
2205 	else
2206 	{
2207 	    nbdebug(("Unrecognised command: %s\n", cmd));
2208 	}
2209 	/*
2210 	 * Unrecognized command is ignored.
2211 	 */
2212     }
2213     if (inAtomic && do_update)
2214     {
2215 	needupdate = 1;
2216 	do_update = 0;
2217     }
2218 
2219     /*
2220      * Is this needed? I moved the netbeans_Xt_connect() later during startup
2221      * and it may no longer be necessary. If it's not needed then needupdate
2222      * and do_update can also be removed.
2223      */
2224     if (buf != NULL && buf->initDone && do_update)
2225     {
2226 	update_screen(NOT_VALID);
2227 	setcursor();
2228 	cursor_on();
2229 	out_flush_cursor(TRUE, FALSE);
2230 
2231 	// Quit a hit-return or more prompt.
2232 	if (State == HITRETURN || State == ASKMORE)
2233 	{
2234 #ifdef FEAT_GUI_GTK
2235 	    if (gui.in_use && gtk_main_level() > 0)
2236 		gtk_main_quit();
2237 #endif
2238 	}
2239     }
2240 
2241     return retval;
2242 }
2243 
2244 
2245 /*
2246  * If "buf" is not the current buffer try changing to a window that edits this
2247  * buffer.  If there is no such window then close the current buffer and set
2248  * the current buffer as "buf".
2249  */
2250     static void
2251 nb_set_curbuf(buf_T *buf)
2252 {
2253     if (curbuf != buf) {
2254 	if (buf_jump_open_win(buf) != NULL)
2255 	    return;
2256 	if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf) != NULL)
2257 	    return;
2258 	set_curbuf(buf, DOBUF_GOTO);
2259     }
2260 }
2261 
2262 /*
2263  * Process a vim colon command.
2264  */
2265     static void
2266 coloncmd(char *cmd, ...)
2267 {
2268     char buf[1024];
2269     va_list ap;
2270 
2271     va_start(ap, cmd);
2272     vim_vsnprintf(buf, sizeof(buf), cmd, ap);
2273     va_end(ap);
2274 
2275     nbdebug(("    COLONCMD %s\n", buf));
2276 
2277     do_cmdline((char_u *)buf, NULL, NULL, DOCMD_NOWAIT | DOCMD_KEYTYPED);
2278 
2279     setcursor();		// restore the cursor position
2280     out_flush_cursor(TRUE, FALSE);
2281 }
2282 
2283 
2284 /*
2285  * Parse the specialKeys argument and issue the appropriate map commands.
2286  */
2287     static void
2288 special_keys(char_u *args)
2289 {
2290     char *save_str = nb_unquote(args, NULL);
2291     char *tok = strtok(save_str, " ");
2292     char *sep;
2293 #define KEYBUFLEN 64
2294     char keybuf[KEYBUFLEN];
2295     char cmdbuf[256];
2296 
2297     while (tok != NULL)
2298     {
2299 	int i = 0;
2300 
2301 	if ((sep = strchr(tok, '-')) != NULL)
2302 	{
2303 	    *sep = NUL;
2304 	    while (*tok)
2305 	    {
2306 		switch (*tok)
2307 		{
2308 		    case 'A':
2309 		    case 'M':
2310 		    case 'C':
2311 		    case 'S':
2312 			keybuf[i++] = *tok;
2313 			keybuf[i++] = '-';
2314 			break;
2315 		}
2316 		tok++;
2317 	    }
2318 	    tok++;
2319 	}
2320 
2321 	if (strlen(tok) + i < KEYBUFLEN)
2322 	{
2323 	    strcpy(&keybuf[i], tok);
2324 	    vim_snprintf(cmdbuf, sizeof(cmdbuf),
2325 				 "<silent><%s> :nbkey %s<CR>", keybuf, keybuf);
2326 	    do_map(0, (char_u *)cmdbuf, NORMAL, FALSE);
2327 	}
2328 	tok = strtok(NULL, " ");
2329     }
2330     vim_free(save_str);
2331 }
2332 
2333     void
2334 ex_nbclose(exarg_T *eap UNUSED)
2335 {
2336     netbeans_close();
2337 }
2338 
2339     void
2340 ex_nbkey(exarg_T *eap)
2341 {
2342     (void)netbeans_keystring(eap->arg);
2343 }
2344 
2345     void
2346 ex_nbstart(
2347     exarg_T	*eap)
2348 {
2349 #ifdef FEAT_GUI
2350 # if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)  \
2351 		&& !defined(FEAT_GUI_MSWIN)
2352     if (gui.in_use)
2353     {
2354 	emsg(_("E838: netbeans is not supported with this GUI"));
2355 	return;
2356     }
2357 # endif
2358 #endif
2359     netbeans_open((char *)eap->arg, FALSE);
2360 }
2361 
2362 /*
2363  * Initialize highlights and signs for use by netbeans  (mostly obsolete)
2364  */
2365     static void
2366 nb_init_graphics(void)
2367 {
2368     static int did_init = FALSE;
2369 
2370     if (!did_init)
2371     {
2372 	coloncmd(":highlight NBGuarded guibg=Cyan guifg=Black"
2373 			    " ctermbg=LightCyan ctermfg=Black");
2374 	coloncmd(":sign define %d linehl=NBGuarded", GUARDED);
2375 
2376 	did_init = TRUE;
2377     }
2378 }
2379 
2380 /*
2381  * Convert key to netbeans name.  This uses the global "mod_mask".
2382  */
2383     static void
2384 netbeans_keyname(int key, char *buf)
2385 {
2386     char *name = 0;
2387     char namebuf[2];
2388     int ctrl  = 0;
2389     int shift = 0;
2390     int alt   = 0;
2391 
2392     if (mod_mask & MOD_MASK_CTRL)
2393 	ctrl = 1;
2394     if (mod_mask & MOD_MASK_SHIFT)
2395 	shift = 1;
2396     if (mod_mask & MOD_MASK_ALT)
2397 	alt = 1;
2398 
2399 
2400     switch (key)
2401     {
2402 	case K_F1:		name = "F1";		break;
2403 	case K_S_F1:	name = "F1";	shift = 1;	break;
2404 	case K_F2:		name = "F2";		break;
2405 	case K_S_F2:	name = "F2";	shift = 1;	break;
2406 	case K_F3:		name = "F3";		break;
2407 	case K_S_F3:	name = "F3";	shift = 1;	break;
2408 	case K_F4:		name = "F4";		break;
2409 	case K_S_F4:	name = "F4";	shift = 1;	break;
2410 	case K_F5:		name = "F5";		break;
2411 	case K_S_F5:	name = "F5";	shift = 1;	break;
2412 	case K_F6:		name = "F6";		break;
2413 	case K_S_F6:	name = "F6";	shift = 1;	break;
2414 	case K_F7:		name = "F7";		break;
2415 	case K_S_F7:	name = "F7";	shift = 1;	break;
2416 	case K_F8:		name = "F8";		break;
2417 	case K_S_F8:	name = "F8";	shift = 1;	break;
2418 	case K_F9:		name = "F9";		break;
2419 	case K_S_F9:	name = "F9";	shift = 1;	break;
2420 	case K_F10:		name = "F10";		break;
2421 	case K_S_F10:	name = "F10";	shift = 1;	break;
2422 	case K_F11:		name = "F11";		break;
2423 	case K_S_F11:	name = "F11";	shift = 1;	break;
2424 	case K_F12:		name = "F12";		break;
2425 	case K_S_F12:	name = "F12";	shift = 1;	break;
2426 	default:
2427 			if (key >= ' ' && key <= '~')
2428 			{
2429 			    // Allow ASCII characters.
2430 			    name = namebuf;
2431 			    namebuf[0] = key;
2432 			    namebuf[1] = NUL;
2433 			}
2434 			else
2435 			    name = "X";
2436 			break;
2437     }
2438 
2439     buf[0] = '\0';
2440     if (ctrl)
2441 	strcat(buf, "C");
2442     if (shift)
2443 	strcat(buf, "S");
2444     if (alt)
2445 	strcat(buf, "M"); // META
2446     if (ctrl || shift || alt)
2447 	strcat(buf, "-");
2448     strcat(buf, name);
2449 }
2450 
2451 #if defined(FEAT_BEVAL) || defined(PROTO)
2452 /*
2453  * Function to be called for balloon evaluation.  Grabs the text under the
2454  * cursor and sends it to the debugger for evaluation.  The debugger should
2455  * respond with a showBalloon command when there is a useful result.
2456  */
2457     void
2458 netbeans_beval_cb(
2459 	BalloonEval	*beval,
2460 	int		 state UNUSED)
2461 {
2462     win_T	*wp;
2463     char_u	*text;
2464     linenr_T	lnum;
2465     int		col;
2466     char	*buf;
2467     char_u	*p;
2468 
2469     // Don't do anything when 'ballooneval' is off, messages scrolled the
2470     // windows up or we have no connection.
2471     if (!can_use_beval() || !NETBEANS_OPEN)
2472 	return;
2473 
2474     if (get_beval_info(beval, TRUE, &wp, &lnum, &text, &col) == OK)
2475     {
2476 	// Send debugger request.  Only when the text is of reasonable
2477 	// length.
2478 	if (text != NULL && text[0] != NUL && STRLEN(text) < MAXPATHL)
2479 	{
2480 	    buf = alloc(MAXPATHL * 2 + 25);
2481 	    if (buf != NULL)
2482 	    {
2483 		p = nb_quote(text);
2484 		if (p != NULL)
2485 		{
2486 		    vim_snprintf(buf, MAXPATHL * 2 + 25,
2487 				     "0:balloonText=%d \"%s\"\n", r_cmdno, p);
2488 		    vim_free(p);
2489 		}
2490 		nbdebug(("EVT: %s", buf));
2491 		nb_send(buf, "netbeans_beval_cb");
2492 		vim_free(buf);
2493 	    }
2494 	}
2495 	vim_free(text);
2496     }
2497 }
2498 #endif
2499 
2500 /*
2501  * Return TRUE when the netbeans connection is active.
2502  */
2503     int
2504 netbeans_active(void)
2505 {
2506     return NETBEANS_OPEN;
2507 }
2508 
2509 /*
2510  * Tell netbeans that the window was opened, ready for commands.
2511  */
2512     void
2513 netbeans_open(char *params, int doabort)
2514 {
2515     char *cmd = "0:startupDone=0\n";
2516 
2517     if (NETBEANS_OPEN)
2518     {
2519 	emsg(_("E511: netbeans already connected"));
2520 	return;
2521     }
2522 
2523     if (netbeans_connect(params, doabort) != OK)
2524 	return;
2525 
2526     nbdebug(("EVT: %s", cmd));
2527     nb_send(cmd, "netbeans_startup_done");
2528 
2529     // update the screen after having added the gutter
2530     changed_window_setting();
2531     update_screen(CLEAR);
2532     setcursor();
2533     cursor_on();
2534     out_flush_cursor(TRUE, FALSE);
2535 }
2536 
2537 /*
2538  * Tell netbeans that we're exiting. This should be called right
2539  * before calling exit.
2540  */
2541     void
2542 netbeans_send_disconnect(void)
2543 {
2544     char buf[128];
2545 
2546     if (NETBEANS_OPEN)
2547     {
2548 	sprintf(buf, "0:disconnect=%d\n", r_cmdno);
2549 	nbdebug(("EVT: %s", buf));
2550 	nb_send(buf, "netbeans_disconnect");
2551     }
2552 }
2553 
2554 #if defined(FEAT_EVAL) || defined(PROTO)
2555     int
2556 set_ref_in_nb_channel(int copyID)
2557 {
2558     int abort = FALSE;
2559     typval_T tv;
2560 
2561     if (nb_channel != NULL)
2562     {
2563 	tv.v_type = VAR_CHANNEL;
2564 	tv.vval.v_channel = nb_channel;
2565 	abort = set_ref_in_item(&tv, copyID, NULL, NULL);
2566     }
2567     return abort;
2568 }
2569 #endif
2570 
2571 #if defined(FEAT_GUI_X11) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
2572 /*
2573  * Tell netbeans that the window was moved or resized.
2574  */
2575     void
2576 netbeans_frame_moved(int new_x, int new_y)
2577 {
2578     char buf[128];
2579 
2580     if (!NETBEANS_OPEN)
2581 	return;
2582 
2583     sprintf(buf, "0:geometry=%d %d %d %d %d\n",
2584 		    r_cmdno, (int)Columns, (int)Rows, new_x, new_y);
2585     // nbdebug(("EVT: %s", buf)); happens too many times during a move
2586     nb_send(buf, "netbeans_frame_moved");
2587 }
2588 #endif
2589 
2590 /*
2591  * Tell netbeans the user opened or activated a file.
2592  */
2593     void
2594 netbeans_file_activated(buf_T *bufp)
2595 {
2596     int bufno = nb_getbufno(bufp);
2597     nbbuf_T *bp = nb_get_buf(bufno);
2598     char    buffer[2*MAXPATHL];
2599     char_u  *q;
2600 
2601     if (!NETBEANS_OPEN || !bufp->b_netbeans_file || dosetvisible)
2602 	return;
2603 
2604     q = nb_quote(bufp->b_ffname);
2605     if (q == NULL || bp == NULL)
2606 	return;
2607 
2608     vim_snprintf(buffer, sizeof(buffer),  "%d:fileOpened=%d \"%s\" %s %s\n",
2609 	    bufno,
2610 	    bufno,
2611 	    (char *)q,
2612 	    "T",  // open in NetBeans
2613 	    "F"); // modified
2614 
2615     vim_free(q);
2616     nbdebug(("EVT: %s", buffer));
2617 
2618     nb_send(buffer, "netbeans_file_opened");
2619 }
2620 
2621 /*
2622  * Tell netbeans the user opened a file.
2623  */
2624     void
2625 netbeans_file_opened(buf_T *bufp)
2626 {
2627     int bufno = nb_getbufno(bufp);
2628     char    buffer[2*MAXPATHL];
2629     char_u  *q;
2630     nbbuf_T *bp = nb_get_buf(nb_getbufno(bufp));
2631     int	    bnum;
2632 
2633     if (!NETBEANS_OPEN)
2634 	return;
2635 
2636     q = nb_quote(bufp->b_ffname);
2637     if (q == NULL)
2638 	return;
2639     if (bp != NULL)
2640 	bnum = bufno;
2641     else
2642 	bnum = 0;
2643 
2644     vim_snprintf(buffer, sizeof(buffer), "%d:fileOpened=%d \"%s\" %s %s\n",
2645 	    bnum,
2646 	    0,
2647 	    (char *)q,
2648 	    "T",  // open in NetBeans
2649 	    "F"); // modified
2650 
2651     vim_free(q);
2652     nbdebug(("EVT: %s", buffer));
2653 
2654     nb_send(buffer, "netbeans_file_opened");
2655     if (p_acd && vim_chdirfile(bufp->b_ffname, "auto") == OK)
2656 	shorten_fnames(TRUE);
2657 }
2658 
2659 /*
2660  * Tell netbeans that a file was deleted or wiped out.
2661  */
2662     void
2663 netbeans_file_killed(buf_T *bufp)
2664 {
2665     int		bufno = nb_getbufno(bufp);
2666     nbbuf_T	*nbbuf = nb_get_buf(bufno);
2667     char	buffer[2*MAXPATHL];
2668 
2669     if (!NETBEANS_OPEN || bufno == -1)
2670 	return;
2671 
2672     nbdebug(("netbeans_file_killed:\n"));
2673     nbdebug(("    Killing bufno: %d", bufno));
2674 
2675     sprintf(buffer, "%d:killed=%d\n", bufno, r_cmdno);
2676 
2677     nbdebug(("EVT: %s", buffer));
2678 
2679     nb_send(buffer, "netbeans_file_killed");
2680 
2681     if (nbbuf != NULL)
2682 	nbbuf->bufp = NULL;
2683 }
2684 
2685 /*
2686  * Get a pointer to the Netbeans buffer for Vim buffer "bufp".
2687  * Return NULL if there is no such buffer or changes are not to be reported.
2688  * Otherwise store the buffer number in "*bufnop".
2689  */
2690     static nbbuf_T *
2691 nb_bufp2nbbuf_fire(buf_T *bufp, int *bufnop)
2692 {
2693     int		bufno;
2694     nbbuf_T	*nbbuf;
2695 
2696     if (!NETBEANS_OPEN || !netbeansFireChanges)
2697 	return NULL;		// changes are not reported at all
2698 
2699     bufno = nb_getbufno(bufp);
2700     if (bufno <= 0)
2701 	return NULL;		// file is not known to NetBeans
2702 
2703     nbbuf = nb_get_buf(bufno);
2704     if (nbbuf != NULL && !nbbuf->fireChanges)
2705 	return NULL;		// changes in this buffer are not reported
2706 
2707     *bufnop = bufno;
2708     return nbbuf;
2709 }
2710 
2711 /*
2712  * Tell netbeans the user inserted some text.
2713  */
2714     void
2715 netbeans_inserted(
2716     buf_T	*bufp,
2717     linenr_T	linenr,
2718     colnr_T	col,
2719     char_u	*txt,
2720     int		newlen)
2721 {
2722     char_u	*buf;
2723     int		bufno;
2724     nbbuf_T	*nbbuf;
2725     pos_T	pos;
2726     long	off;
2727     char_u	*p;
2728     char_u	*newtxt;
2729 
2730     if (!NETBEANS_OPEN)
2731 	return;
2732 
2733     nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
2734     if (nbbuf == NULL)
2735 	return;
2736 
2737     // Don't mark as modified for initial read
2738     if (nbbuf->insertDone)
2739 	nbbuf->modified = 1;
2740 
2741     pos.lnum = linenr;
2742     pos.col = col;
2743     off = pos2off(bufp, &pos);
2744 
2745     // send the "insert" EVT
2746     newtxt = alloc(newlen + 1);
2747     vim_strncpy(newtxt, txt, newlen);
2748     p = nb_quote(newtxt);
2749     if (p != NULL)
2750     {
2751 	buf = alloc(128 + 2*newlen);
2752 	sprintf((char *)buf, "%d:insert=%d %ld \"%s\"\n",
2753 						      bufno, r_cmdno, off, p);
2754 	nbdebug(("EVT: %s", buf));
2755 	nb_send((char *)buf, "netbeans_inserted");
2756 	vim_free(p);
2757 	vim_free(buf);
2758     }
2759     vim_free(newtxt);
2760 }
2761 
2762 /*
2763  * Tell netbeans some bytes have been removed.
2764  */
2765     void
2766 netbeans_removed(
2767     buf_T	*bufp,
2768     linenr_T	linenr,
2769     colnr_T	col,
2770     long	len)
2771 {
2772     char_u	buf[128];
2773     int		bufno;
2774     nbbuf_T	*nbbuf;
2775     pos_T	pos;
2776     long	off;
2777 
2778     if (!NETBEANS_OPEN)
2779 	return;
2780 
2781     nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
2782     if (nbbuf == NULL)
2783 	return;
2784 
2785     if (len < 0)
2786     {
2787 	nbdebug(("Negative len %ld in netbeans_removed()!\n", len));
2788 	return;
2789     }
2790 
2791     nbbuf->modified = 1;
2792 
2793     pos.lnum = linenr;
2794     pos.col = col;
2795 
2796     off = pos2off(bufp, &pos);
2797 
2798     sprintf((char *)buf, "%d:remove=%d %ld %ld\n", bufno, r_cmdno, off, len);
2799     nbdebug(("EVT: %s", buf));
2800     nb_send((char *)buf, "netbeans_removed");
2801 }
2802 
2803 /*
2804  * Send netbeans an unmodified command.
2805  */
2806     void
2807 netbeans_unmodified(buf_T *bufp UNUSED)
2808 {
2809     // This is a no-op, because NetBeans considers a buffer modified
2810     // even when all changes have been undone.
2811 }
2812 
2813 /*
2814  * Send a button release event back to netbeans. It's up to netbeans
2815  * to decide what to do (if anything) with this event.
2816  */
2817     void
2818 netbeans_button_release(int button)
2819 {
2820     char	buf[128];
2821     int		bufno;
2822 
2823     if (!NETBEANS_OPEN)
2824 	return;
2825 
2826     bufno = nb_getbufno(curbuf);
2827 
2828     if (bufno >= 0 && curwin != NULL && curwin->w_buffer == curbuf)
2829     {
2830 	int col = mouse_col - curwin->w_wincol
2831 			      - ((curwin->w_p_nu || curwin->w_p_rnu) ? 9 : 1);
2832 	long off = pos2off(curbuf, &curwin->w_cursor);
2833 
2834 	// sync the cursor position
2835 	sprintf(buf, "%d:newDotAndMark=%d %ld %ld\n", bufno, r_cmdno, off, off);
2836 	nbdebug(("EVT: %s", buf));
2837 	nb_send(buf, "netbeans_button_release[newDotAndMark]");
2838 
2839 	sprintf(buf, "%d:buttonRelease=%d %d %ld %d\n", bufno, r_cmdno,
2840 				    button, (long)curwin->w_cursor.lnum, col);
2841 	nbdebug(("EVT: %s", buf));
2842 	nb_send(buf, "netbeans_button_release");
2843     }
2844 }
2845 
2846 
2847 /*
2848  * Send a keypress event back to netbeans. This usually simulates some
2849  * kind of function key press. This function operates on a key code.
2850  * Return TRUE when the key was sent, FALSE when the command has been
2851  * postponed.
2852  */
2853     int
2854 netbeans_keycommand(int key)
2855 {
2856     char	keyName[60];
2857 
2858     netbeans_keyname(key, keyName);
2859     return netbeans_keystring((char_u *)keyName);
2860 }
2861 
2862 
2863 /*
2864  * Send a keypress event back to netbeans. This usually simulates some
2865  * kind of function key press. This function operates on a key string.
2866  * Return TRUE when the key was sent, FALSE when the command has been
2867  * postponed.
2868  */
2869     static int
2870 netbeans_keystring(char_u *keyName)
2871 {
2872     char	buf[2*MAXPATHL];
2873     int		bufno = nb_getbufno(curbuf);
2874     long	off;
2875     char_u	*q;
2876 
2877     if (!NETBEANS_OPEN)
2878 	return TRUE;
2879 
2880     if (bufno == -1)
2881     {
2882 	nbdebug(("got keycommand for non-NetBeans buffer, opening...\n"));
2883 	q = curbuf->b_ffname == NULL ? (char_u *)""
2884 						 : nb_quote(curbuf->b_ffname);
2885 	if (q == NULL)
2886 	    return TRUE;
2887 	vim_snprintf(buf, sizeof(buf), "0:fileOpened=%d \"%s\" %s %s\n", 0,
2888 		q,
2889 		"T",  // open in NetBeans
2890 		"F"); // modified
2891 	if (curbuf->b_ffname != NULL)
2892 	    vim_free(q);
2893 	nbdebug(("EVT: %s", buf));
2894 	nb_send(buf, "netbeans_keycommand");
2895 
2896 	postpone_keycommand(keyName);
2897 	return FALSE;
2898     }
2899 
2900     // sync the cursor position
2901     off = pos2off(curbuf, &curwin->w_cursor);
2902     sprintf(buf, "%d:newDotAndMark=%d %ld %ld\n", bufno, r_cmdno, off, off);
2903     nbdebug(("EVT: %s", buf));
2904     nb_send(buf, "netbeans_keycommand");
2905 
2906     // To work on Win32 you must apply patch to ExtEditor module
2907     // from ExtEdCaret.java.diff - make EVT_newDotAndMark handler
2908     // more synchronous
2909 
2910     // now send keyCommand event
2911     vim_snprintf(buf, sizeof(buf), "%d:keyCommand=%d \"%s\"\n",
2912 						     bufno, r_cmdno, keyName);
2913     nbdebug(("EVT: %s", buf));
2914     nb_send(buf, "netbeans_keycommand");
2915 
2916     // New: do both at once and include the lnum/col.
2917     vim_snprintf(buf, sizeof(buf), "%d:keyAtPos=%d \"%s\" %ld %ld/%ld\n",
2918 	    bufno, r_cmdno, keyName,
2919 		off, (long)curwin->w_cursor.lnum, (long)curwin->w_cursor.col);
2920     nbdebug(("EVT: %s", buf));
2921     nb_send(buf, "netbeans_keycommand");
2922     return TRUE;
2923 }
2924 
2925 
2926 /*
2927  * Send a save event to netbeans.
2928  */
2929     void
2930 netbeans_save_buffer(buf_T *bufp)
2931 {
2932     char_u	buf[64];
2933     int		bufno;
2934     nbbuf_T	*nbbuf;
2935 
2936     if (!NETBEANS_OPEN)
2937 	return;
2938 
2939     nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
2940     if (nbbuf == NULL)
2941 	return;
2942 
2943     nbbuf->modified = 0;
2944 
2945     sprintf((char *)buf, "%d:save=%d\n", bufno, r_cmdno);
2946     nbdebug(("EVT: %s", buf));
2947     nb_send((char *)buf, "netbeans_save_buffer");
2948 }
2949 
2950 
2951 /*
2952  * Send remove command to netbeans (this command has been turned off).
2953  */
2954     void
2955 netbeans_deleted_all_lines(buf_T *bufp)
2956 {
2957     char_u	buf[64];
2958     int		bufno;
2959     nbbuf_T	*nbbuf;
2960 
2961     if (!NETBEANS_OPEN)
2962 	return;
2963 
2964     nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
2965     if (nbbuf == NULL)
2966 	return;
2967 
2968     // Don't mark as modified for initial read
2969     if (nbbuf->insertDone)
2970 	nbbuf->modified = 1;
2971 
2972     sprintf((char *)buf, "%d:remove=%d 0 -1\n", bufno, r_cmdno);
2973     nbdebug(("EVT(suppressed): %s", buf));
2974 //     nb_send(buf, "netbeans_deleted_all_lines");
2975 }
2976 
2977 
2978 /*
2979  * See if the lines are guarded. The top and bot parameters are from
2980  * u_savecommon(), these are the line above the change and the line below the
2981  * change.
2982  */
2983     int
2984 netbeans_is_guarded(linenr_T top, linenr_T bot)
2985 {
2986     sign_entry_T	*p;
2987     int			lnum;
2988 
2989     if (!NETBEANS_OPEN)
2990 	return FALSE;
2991 
2992     FOR_ALL_SIGNS_IN_BUF(curbuf, p)
2993 	if (p->se_id >= GUARDEDOFFSET)
2994 	    for (lnum = top + 1; lnum < bot; lnum++)
2995 		if (lnum == p->se_lnum)
2996 		    return TRUE;
2997 
2998     return FALSE;
2999 }
3000 
3001 #if defined(FEAT_GUI_X11) || defined(PROTO)
3002 /*
3003  * We have multiple signs to draw at the same location. Draw the
3004  * multi-sign indicator instead. This is the Motif version.
3005  */
3006     void
3007 netbeans_draw_multisign_indicator(int row)
3008 {
3009     int i;
3010     int y;
3011     int x;
3012 
3013     if (!NETBEANS_OPEN)
3014 	return;
3015 
3016     x = 0;
3017     y = row * gui.char_height + 2;
3018 
3019     for (i = 0; i < gui.char_height - 3; i++)
3020 	XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y++);
3021 
3022     XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+0, y);
3023     XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3024     XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+4, y++);
3025     XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+1, y);
3026     XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3027     XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+3, y++);
3028     XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3029 }
3030 #endif // FEAT_GUI_X11
3031 
3032 #if defined(FEAT_GUI_GTK) && !defined(PROTO)
3033 /*
3034  * We have multiple signs to draw at the same location. Draw the
3035  * multi-sign indicator instead. This is the GTK/Gnome version.
3036  */
3037     void
3038 netbeans_draw_multisign_indicator(int row)
3039 {
3040     int i;
3041     int y;
3042     int x;
3043 #if GTK_CHECK_VERSION(3,0,0)
3044     cairo_t *cr = NULL;
3045 #else
3046     GdkDrawable *drawable = gui.drawarea->window;
3047 #endif
3048 
3049     if (!NETBEANS_OPEN)
3050 	return;
3051 
3052 #if GTK_CHECK_VERSION(3,0,0)
3053     cr = cairo_create(gui.surface);
3054     cairo_set_source_rgba(cr,
3055 	    gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue,
3056 	    gui.fgcolor->alpha);
3057 #endif
3058 
3059     x = 0;
3060     y = row * gui.char_height + 2;
3061 
3062     for (i = 0; i < gui.char_height - 3; i++)
3063 #if GTK_CHECK_VERSION(3,0,0)
3064 	cairo_rectangle(cr, x+2, y++, 1, 1);
3065 #else
3066 	gdk_draw_point(drawable, gui.text_gc, x+2, y++);
3067 #endif
3068 
3069 #if GTK_CHECK_VERSION(3,0,0)
3070     cairo_rectangle(cr, x+0, y, 1, 1);
3071     cairo_rectangle(cr, x+2, y, 1, 1);
3072     cairo_rectangle(cr, x+4, y++, 1, 1);
3073     cairo_rectangle(cr, x+1, y, 1, 1);
3074     cairo_rectangle(cr, x+2, y, 1, 1);
3075     cairo_rectangle(cr, x+3, y++, 1, 1);
3076     cairo_rectangle(cr, x+2, y, 1, 1);
3077 #else
3078     gdk_draw_point(drawable, gui.text_gc, x+0, y);
3079     gdk_draw_point(drawable, gui.text_gc, x+2, y);
3080     gdk_draw_point(drawable, gui.text_gc, x+4, y++);
3081     gdk_draw_point(drawable, gui.text_gc, x+1, y);
3082     gdk_draw_point(drawable, gui.text_gc, x+2, y);
3083     gdk_draw_point(drawable, gui.text_gc, x+3, y++);
3084     gdk_draw_point(drawable, gui.text_gc, x+2, y);
3085 #endif
3086 
3087 #if GTK_CHECK_VERSION(3,0,0)
3088     cairo_destroy(cr);
3089 #endif
3090 }
3091 #endif // FEAT_GUI_GTK
3092 
3093 /*
3094  * If the mouse is clicked in the gutter of a line with multiple
3095  * annotations, cycle through the set of signs.
3096  */
3097     void
3098 netbeans_gutter_click(linenr_T lnum)
3099 {
3100     sign_entry_T	*p;
3101 
3102     if (!NETBEANS_OPEN)
3103 	return;
3104 
3105     FOR_ALL_SIGNS_IN_BUF(curbuf, p)
3106     {
3107 	if (p->se_lnum == lnum && p->se_next && p->se_next->se_lnum == lnum)
3108 	{
3109 	    sign_entry_T *tail;
3110 
3111 	    // remove "p" from list, reinsert it at the tail of the sublist
3112 	    if (p->se_prev)
3113 		p->se_prev->se_next = p->se_next;
3114 	    else
3115 		curbuf->b_signlist = p->se_next;
3116 	    p->se_next->se_prev = p->se_prev;
3117 	    // now find end of sublist and insert p
3118 	    for (tail = p->se_next;
3119 		  tail->se_next && tail->se_next->se_lnum == lnum
3120 				       && tail->se_next->se_id < GUARDEDOFFSET;
3121 		  tail = tail->se_next)
3122 		;
3123 	    // tail now points to last entry with same lnum (except
3124 	    // that "guarded" annotations are always last)
3125 	    p->se_next = tail->se_next;
3126 	    if (tail->se_next)
3127 		tail->se_next->se_prev = p;
3128 	    p->se_prev = tail;
3129 	    tail->se_next = p;
3130 	    update_debug_sign(curbuf, lnum);
3131 	    break;
3132 	}
3133     }
3134 }
3135 
3136 /*
3137  * Add a sign of the requested type at the requested location.
3138  *
3139  * Reverse engineering:
3140  * Apparently an annotation is defined the first time it is used in a buffer.
3141  * When the same annotation is used in two buffers, the second time we do not
3142  * need to define a new sign name but reuse the existing one.  But since the
3143  * ID number used in the second buffer starts counting at one again, a mapping
3144  * is made from the ID specifically for the buffer to the global sign name
3145  * (which is a number).
3146  *
3147  * globalsignmap[]	stores the signs that have been defined globally.
3148  * buf->signmapused[]	maps buffer-local annotation IDs to an index in
3149  *			globalsignmap[].
3150  */
3151     static void
3152 addsigntype(
3153     nbbuf_T	*buf,
3154     int		typeNum,
3155     char_u	*typeName,
3156     char_u	*tooltip UNUSED,
3157     char_u	*glyphFile,
3158     char_u	*fg,
3159     char_u	*bg)
3160 {
3161     int i, j;
3162     int use_fg = (*fg && STRCMP(fg, "none") != 0);
3163     int use_bg = (*bg && STRCMP(bg, "none") != 0);
3164 
3165     for (i = 0; i < globalsignmapused; i++)
3166 	if (STRCMP(typeName, globalsignmap[i]) == 0)
3167 	    break;
3168 
3169     if (i == globalsignmapused) // not found; add it to global map
3170     {
3171 	nbdebug(("DEFINEANNOTYPE(%d,%s,%s,%s,%s,%s)\n",
3172 			    typeNum, typeName, tooltip, glyphFile, fg, bg));
3173 	if (use_fg || use_bg)
3174 	{
3175 	    char fgbuf[2 * (8 + MAX_COLOR_LENGTH) + 1];
3176 	    char bgbuf[2 * (8 + MAX_COLOR_LENGTH) + 1];
3177 	    char *ptr;
3178 	    int value;
3179 
3180 	    value = strtol((char *)fg, &ptr, 10);
3181 	    if (ptr != (char *)fg)
3182 		sprintf(fgbuf, "guifg=#%06x", value & 0xFFFFFF);
3183 	    else
3184 		sprintf(fgbuf, "guifg=%s ctermfg=%s", fg, fg);
3185 
3186 	    value = strtol((char *)bg, &ptr, 10);
3187 	    if (ptr != (char *)bg)
3188 		sprintf(bgbuf, "guibg=#%06x", value & 0xFFFFFF);
3189 	    else
3190 		sprintf(bgbuf, "guibg=%s ctermbg=%s", bg, bg);
3191 
3192 	    coloncmd(":highlight NB_%s %s %s", typeName, (use_fg) ? fgbuf : "",
3193 		     (use_bg) ? bgbuf : "");
3194 	    if (*glyphFile == NUL)
3195 		// no glyph, line highlighting only
3196 		coloncmd(":sign define %d linehl=NB_%s", i + 1, typeName);
3197 	    else if (vim_strsize(glyphFile) <= 2)
3198 		// one- or two-character glyph name, use as text glyph with
3199 		// texthl
3200 		coloncmd(":sign define %d text=%s texthl=NB_%s", i + 1,
3201 							 glyphFile, typeName);
3202 	    else
3203 		// glyph, line highlighting
3204 		coloncmd(":sign define %d icon=%s linehl=NB_%s", i + 1,
3205 							 glyphFile, typeName);
3206 	}
3207 	else
3208 	    // glyph, no line highlighting
3209 	    coloncmd(":sign define %d icon=%s", i + 1, glyphFile);
3210 
3211 	if (STRCMP(typeName,"CurrentPC") == 0)
3212 	    curPCtype = typeNum;
3213 
3214 	if (globalsignmapused == globalsignmaplen)
3215 	{
3216 	    if (globalsignmaplen == 0) // first allocation
3217 	    {
3218 		globalsignmaplen = 20;
3219 		globalsignmap = ALLOC_CLEAR_MULT(char *, globalsignmaplen);
3220 	    }
3221 	    else    // grow it
3222 	    {
3223 		int incr;
3224 		int oldlen = globalsignmaplen;
3225 		char **t_globalsignmap = globalsignmap;
3226 
3227 		globalsignmaplen *= 2;
3228 		incr = globalsignmaplen - oldlen;
3229 		globalsignmap = vim_realloc(globalsignmap,
3230 					   globalsignmaplen * sizeof(char *));
3231 		if (globalsignmap == NULL)
3232 		{
3233 		    vim_free(t_globalsignmap);
3234 		    globalsignmaplen = 0;
3235 		    return;
3236 		}
3237 		vim_memset(globalsignmap + oldlen, 0, incr * sizeof(char *));
3238 	    }
3239 	}
3240 
3241 	globalsignmap[i] = (char *)vim_strsave(typeName);
3242 	globalsignmapused = i + 1;
3243     }
3244 
3245     // check local map; should *not* be found!
3246     for (j = 0; j < buf->signmapused; j++)
3247 	if (buf->signmap[j] == i + 1)
3248 	    return;
3249 
3250     // add to local map
3251     if (buf->signmapused == buf->signmaplen)
3252     {
3253 	if (buf->signmaplen == 0) // first allocation
3254 	{
3255 	    buf->signmaplen = 5;
3256 	    buf->signmap = ALLOC_CLEAR_MULT(int, buf->signmaplen);
3257 	}
3258 	else    // grow it
3259 	{
3260 	    int incr;
3261 	    int oldlen = buf->signmaplen;
3262 	    int *t_signmap = buf->signmap;
3263 
3264 	    buf->signmaplen *= 2;
3265 	    incr = buf->signmaplen - oldlen;
3266 	    buf->signmap = vim_realloc(buf->signmap,
3267 					       buf->signmaplen * sizeof(int));
3268 	    if (buf->signmap == NULL)
3269 	    {
3270 		vim_free(t_signmap);
3271 		buf->signmaplen = 0;
3272 		return;
3273 	    }
3274 	    vim_memset(buf->signmap + oldlen, 0, incr * sizeof(int));
3275 	}
3276     }
3277 
3278     buf->signmap[buf->signmapused++] = i + 1;
3279 
3280 }
3281 
3282 
3283 /*
3284  * See if we have the requested sign type in the buffer.
3285  */
3286     static int
3287 mapsigntype(nbbuf_T *buf, int localsigntype)
3288 {
3289     if (--localsigntype >= 0 && localsigntype < buf->signmapused)
3290 	return buf->signmap[localsigntype];
3291 
3292     return 0;
3293 }
3294 
3295 
3296 /*
3297  * Compute length of buffer, don't print anything.
3298  */
3299     static long
3300 get_buf_size(buf_T *bufp)
3301 {
3302     linenr_T	lnum;
3303     long	char_count = 0;
3304     int		eol_size;
3305     long	last_check = 100000L;
3306 
3307     if (bufp->b_ml.ml_flags & ML_EMPTY)
3308 	return 0;
3309     else
3310     {
3311 	if (get_fileformat(bufp) == EOL_DOS)
3312 	    eol_size = 2;
3313 	else
3314 	    eol_size = 1;
3315 	for (lnum = 1; lnum <= bufp->b_ml.ml_line_count; ++lnum)
3316 	{
3317 	    char_count += (long)STRLEN(ml_get_buf(bufp, lnum, FALSE))
3318 								   + eol_size;
3319 	    // Check for a CTRL-C every 100000 characters
3320 	    if (char_count > last_check)
3321 	    {
3322 		ui_breakcheck();
3323 		if (got_int)
3324 		    return char_count;
3325 		last_check = char_count + 100000L;
3326 	    }
3327 	}
3328 	// Correction for when last line doesn't have an EOL.
3329 	if (!bufp->b_p_eol && (bufp->b_p_bin || !bufp->b_p_fixeol))
3330 	    char_count -= eol_size;
3331     }
3332 
3333     return char_count;
3334 }
3335 
3336 /*
3337  * Convert character offset to lnum,col
3338  */
3339     static pos_T *
3340 off2pos(buf_T *buf, long offset)
3341 {
3342     linenr_T	 lnum;
3343     static pos_T pos;
3344 
3345     pos.lnum = 0;
3346     pos.col = 0;
3347     pos.coladd = 0;
3348 
3349     if (!(buf->b_ml.ml_flags & ML_EMPTY))
3350     {
3351 	if ((lnum = ml_find_line_or_offset(buf, (linenr_T)0, &offset)) < 0)
3352 	    return NULL;
3353 	pos.lnum = lnum;
3354 	pos.col = offset;
3355     }
3356 
3357     return &pos;
3358 }
3359 
3360 /*
3361  * Convert an argument in the form "1234" to an offset and compute the
3362  * lnum/col from it.  Convert an argument in the form "123/12" directly to a
3363  * lnum/col.
3364  * "argp" is advanced to after the argument.
3365  * Return a pointer to the position, NULL if something is wrong.
3366  */
3367     static pos_T *
3368 get_off_or_lnum(buf_T *buf, char_u **argp)
3369 {
3370     static pos_T	mypos;
3371     long		off;
3372 
3373     off = strtol((char *)*argp, (char **)argp, 10);
3374     if (**argp == '/')
3375     {
3376 	mypos.lnum = (linenr_T)off;
3377 	++*argp;
3378 	mypos.col = strtol((char *)*argp, (char **)argp, 10);
3379 	mypos.coladd = 0;
3380 	return &mypos;
3381     }
3382     return off2pos(buf, off);
3383 }
3384 
3385 
3386 /*
3387  * Convert (lnum,col) to byte offset in the file.
3388  */
3389     static long
3390 pos2off(buf_T *buf, pos_T *pos)
3391 {
3392     long	 offset = 0;
3393 
3394     if (!(buf->b_ml.ml_flags & ML_EMPTY))
3395     {
3396 	if ((offset = ml_find_line_or_offset(buf, pos->lnum, 0)) < 0)
3397 	    return 0;
3398 	offset += pos->col;
3399     }
3400 
3401     return offset;
3402 }
3403 
3404 
3405 /*
3406  * This message is printed after NetBeans opens a new file. It's
3407  * similar to the message readfile() uses, but since NetBeans
3408  * doesn't normally call readfile, we do our own.
3409  */
3410     static void
3411 print_read_msg(nbbuf_T *buf)
3412 {
3413     int	    lnum = buf->bufp->b_ml.ml_line_count;
3414     off_T   nchars = buf->bufp->b_orig_size;
3415     char_u  c;
3416 
3417     msg_add_fname(buf->bufp, buf->bufp->b_ffname);
3418     c = FALSE;
3419 
3420     if (buf->bufp->b_p_ro)
3421     {
3422 	STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]"));
3423 	c = TRUE;
3424     }
3425     if (!buf->bufp->b_start_eol)
3426     {
3427 	STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]")
3428 					       : _("[Incomplete last line]"));
3429 	c = TRUE;
3430     }
3431     msg_add_lines(c, (long)lnum, nchars);
3432 
3433     // Now display it
3434     VIM_CLEAR(keep_msg);
3435     msg_scrolled_ign = TRUE;
3436     msg_trunc_attr((char *)IObuff, FALSE, 0);
3437     msg_scrolled_ign = FALSE;
3438 }
3439 
3440 
3441 /*
3442  * Print a message after NetBeans writes the file. This message should be
3443  * identical to the standard message a non-netbeans user would see when
3444  * writing a file.
3445  */
3446     static void
3447 print_save_msg(nbbuf_T *buf, off_T nchars)
3448 {
3449     char_u	c;
3450     char_u	*p;
3451 
3452     if (nchars >= 0)
3453     {
3454 	// put fname in IObuff with quotes
3455 	msg_add_fname(buf->bufp, buf->bufp->b_ffname);
3456 	c = FALSE;
3457 
3458 	msg_add_lines(c, buf->bufp->b_ml.ml_line_count,
3459 						buf->bufp->b_orig_size);
3460 
3461 	VIM_CLEAR(keep_msg);
3462 	msg_scrolled_ign = TRUE;
3463 	p = (char_u *)msg_trunc_attr((char *)IObuff, FALSE, 0);
3464 	if ((msg_scrolled && !need_wait_return) || !buf->initDone)
3465 	{
3466 	    // Need to repeat the message after redrawing when:
3467 	    // - When reading from stdin (the screen will be cleared next).
3468 	    // - When restart_edit is set (otherwise there will be a delay
3469 	    //   before redrawing).
3470 	    // - When the screen was scrolled but there is no wait-return
3471 	    //   prompt.
3472 	    set_keep_msg(p, 0);
3473 	}
3474 	msg_scrolled_ign = FALSE;
3475 	// add_to_input_buf((char_u *)"\f", 1);
3476     }
3477     else
3478     {
3479 	char msgbuf[IOSIZE];
3480 
3481 	vim_snprintf(msgbuf, IOSIZE,
3482 		       _("E505: %s is read-only (add ! to override)"), IObuff);
3483 	nbdebug(("    %s\n", msgbuf));
3484 	emsg(msgbuf);
3485     }
3486 }
3487 
3488 #endif // defined(FEAT_NETBEANS_INTG)
3489