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