blob: 889ad3ce2394358fa8da573e238ea2470c753f32 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * Netbeans integration by David Weatherford
5 * Adopted for Win32 by Sergey Khorev
6 *
7 * Do ":help uganda" in Vim to read copying and usage conditions.
8 * Do ":help credits" in Vim to see a list of people who contributed.
9 */
10
11/*
12 * Implements client side of org.netbeans.modules.emacs editor
13 * integration protocol. Be careful! The protocol uses offsets
14 * which are *between* characters, whereas vim uses line number
15 * and column number which are *on* characters.
16 * See ":help netbeans-protocol" for explanation.
17 */
18
19#include "vim.h"
20
21#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
22
23/* Note: when making changes here also adjust configure.in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024# include <fcntl.h>
25#ifdef WIN32
26# ifdef DEBUG
27# include <tchar.h> /* for _T definition for TRACEn macros */
28# endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +000029# include "vimio.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +000030/* WinSock API is separated from C API, thus we can't use read(), write(),
31 * errno... */
32# define sock_errno WSAGetLastError()
33# define ECONNREFUSED WSAECONNREFUSED
34# ifdef EINTR
35# undef EINTR
36# endif
37# define EINTR WSAEINTR
38# define sock_write(sd, buf, len) send(sd, buf, len, 0)
39# define sock_read(sd, buf, len) recv(sd, buf, len, 0)
40# define sock_close(sd) closesocket(sd)
41# define sleep(t) Sleep(t*1000) /* WinAPI Sleep() accepts milliseconds */
42#else
43# include <netdb.h>
44# include <netinet/in.h>
45# include <sys/socket.h>
46# ifdef HAVE_LIBGEN_H
47# include <libgen.h>
48# endif
49# define sock_errno errno
50# define sock_write(sd, buf, len) write(sd, buf, len)
51# define sock_read(sd, buf, len) read(sd, buf, len)
52# define sock_close(sd) close(sd)
53#endif
54
55#include "version.h"
56
57#define INET_SOCKETS
58
59#define GUARDED 10000 /* typenr for "guarded" annotation */
60#define GUARDEDOFFSET 1000000 /* base for "guarded" sign id's */
61
62/* The first implementation (working only with Netbeans) returned "1.1". The
63 * protocol implemented here also supports A-A-P. */
Bram Moolenaar009b2592004-10-24 19:18:58 +000064static char *ExtEdProtocolVersion = "2.3";
Bram Moolenaar071d4272004-06-13 20:20:40 +000065
66static long pos2off __ARGS((buf_T *, pos_T *));
67static pos_T *off2pos __ARGS((buf_T *, long));
68static pos_T *get_off_or_lnum __ARGS((buf_T *buf, char_u **argp));
69static long get_buf_size __ARGS((buf_T *));
Bram Moolenaar009b2592004-10-24 19:18:58 +000070static void netbeans_keystring __ARGS((int key, char *keystr));
71static void special_keys __ARGS((char_u *args));
Bram Moolenaar071d4272004-06-13 20:20:40 +000072
73static void netbeans_connect __ARGS((void));
74static int getConnInfo __ARGS((char *file, char **host, char **port, char **password));
75
76static void nb_init_graphics __ARGS((void));
77static void coloncmd __ARGS((char *cmd, ...));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000078static void nb_set_curbuf __ARGS((buf_T *buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000079#ifdef FEAT_GUI_MOTIF
80static void messageFromNetbeans __ARGS((XtPointer, int *, XtInputId *));
81#endif
82#ifdef FEAT_GUI_GTK
83static void messageFromNetbeans __ARGS((gpointer, gint, GdkInputCondition));
84#endif
85static void nb_parse_cmd __ARGS((char_u *));
86static int nb_do_cmd __ARGS((int, char_u *, int, int, char_u *));
87static void nb_send __ARGS((char *buf, char *fun));
Bram Moolenaar071d4272004-06-13 20:20:40 +000088
89static int sd = -1; /* socket fd for Netbeans connection */
90#ifdef FEAT_GUI_MOTIF
91static XtInputId inputHandler; /* Cookie for input */
92#endif
93#ifdef FEAT_GUI_GTK
94static gint inputHandler; /* Cookie for input */
95#endif
96#ifdef FEAT_GUI_W32
97static int inputHandler = -1; /* simply ret.value of WSAAsyncSelect() */
98extern HWND s_hwnd; /* Gvim's Window handle */
99#endif
100static int cmdno; /* current command number for reply */
101static int haveConnection = FALSE; /* socket is connected and
102 initialization is done */
103static int oldFire = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104
Bram Moolenaar009b2592004-10-24 19:18:58 +0000105#ifdef FEAT_GUI_MOTIF
106static void netbeans_Xt_connect __ARGS((void *context));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000107#endif
108#ifdef FEAT_GUI_GTK
Bram Moolenaar009b2592004-10-24 19:18:58 +0000109static void netbeans_gtk_connect __ARGS((void));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000110#endif
111#ifdef FEAT_GUI_W32
Bram Moolenaar009b2592004-10-24 19:18:58 +0000112static void netbeans_w32_connect __ARGS((void));
Bram Moolenaar009b2592004-10-24 19:18:58 +0000113#endif
114
115static int dosetvisible = FALSE;
116
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117/*
118 * Include the debugging code if wanted.
119 */
120#ifdef NBDEBUG
121# include "nbdebug.c"
122#endif
123
124/* Connect back to Netbeans process */
Bram Moolenaar009b2592004-10-24 19:18:58 +0000125#ifdef FEAT_GUI_MOTIF
126 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127netbeans_Xt_connect(void *context)
128{
129 netbeans_connect();
130 if (sd > 0)
131 {
132 /* tell notifier we are interested in being called
133 * when there is input on the editor connection socket
134 */
135 inputHandler = XtAppAddInput((XtAppContext)context, sd,
136 (XtPointer)(XtInputReadMask + XtInputExceptMask),
137 messageFromNetbeans, NULL);
138 }
139}
140
141 static void
142netbeans_disconnect(void)
143{
144 if (inputHandler != (XtInputId)NULL)
145 {
146 XtRemoveInput(inputHandler);
147 inputHandler = (XtInputId)NULL;
148 }
149 sd = -1;
150 haveConnection = FALSE;
Bram Moolenaard62bec82005-03-07 22:56:57 +0000151# ifdef FEAT_BEVAL
152 bevalServers &= ~BEVAL_NETBEANS;
153# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154}
155#endif /* FEAT_MOTIF_GUI */
156
Bram Moolenaar009b2592004-10-24 19:18:58 +0000157#ifdef FEAT_GUI_GTK
158 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159netbeans_gtk_connect(void)
160{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 netbeans_connect();
162 if (sd > 0)
163 {
164 /*
165 * Tell gdk we are interested in being called when there
166 * is input on the editor connection socket
167 */
168 inputHandler = gdk_input_add(sd, (GdkInputCondition)
169 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
170 messageFromNetbeans, NULL);
171 }
172}
173
174 static void
175netbeans_disconnect(void)
176{
177 if (inputHandler != 0)
178 {
179 gdk_input_remove(inputHandler);
180 inputHandler = 0;
181 }
182 sd = -1;
183 haveConnection = FALSE;
Bram Moolenaard62bec82005-03-07 22:56:57 +0000184# ifdef FEAT_BEVAL
185 bevalServers &= ~BEVAL_NETBEANS;
186# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187}
188#endif /* FEAT_GUI_GTK */
189
Bram Moolenaar5b625c52005-01-31 18:55:55 +0000190#if defined(FEAT_GUI_W32) || defined(PROTO)
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000191 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192netbeans_w32_connect(void)
193{
194 netbeans_connect();
195 if (sd > 0)
196 {
197 /*
198 * Tell Windows we are interested in receiving message when there
199 * is input on the editor connection socket
200 */
201 inputHandler = WSAAsyncSelect(sd, s_hwnd, WM_NETBEANS, FD_READ);
202 }
203}
204
205 static void
206netbeans_disconnect(void)
207{
208 if (inputHandler == 0)
209 {
210 WSAAsyncSelect(sd, s_hwnd, 0, 0);
211 inputHandler = -1;
212 }
213 sd = -1;
214 haveConnection = FALSE;
Bram Moolenaard62bec82005-03-07 22:56:57 +0000215# ifdef FEAT_BEVAL
216 bevalServers &= ~BEVAL_NETBEANS;
217# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218}
219#endif /* FEAT_GUI_W32 */
220
221#define NB_DEF_HOST "localhost"
222#define NB_DEF_ADDR "3219"
223#define NB_DEF_PASS "changeme"
224
225 static void
226netbeans_connect(void)
227{
228#ifdef INET_SOCKETS
229 struct sockaddr_in server;
230 struct hostent * host;
231# ifdef FEAT_GUI_W32
232 u_short port;
233# else
234 int port;
235#endif
236#else
237 struct sockaddr_un server;
238#endif
239 char buf[32];
240 char *hostname = NULL;
241 char *address = NULL;
242 char *password = NULL;
243 char *fname;
244 char *arg = NULL;
245
246 if (netbeansArg[3] == '=')
247 {
248 /* "-nb=fname": Read info from specified file. */
249 if (getConnInfo(netbeansArg + 4, &hostname, &address, &password)
250 == FAIL)
251 return;
252 }
253 else
254 {
255 if (netbeansArg[3] == ':')
256 /* "-nb:<host>:<addr>:<password>": get info from argument */
257 arg = netbeansArg + 4;
258 if (arg == NULL && (fname = getenv("__NETBEANS_CONINFO")) != NULL)
259 {
260 /* "-nb": get info from file specified in environment */
261 if (getConnInfo(fname, &hostname, &address, &password) == FAIL)
262 return;
263 }
264 else
265 {
266 if (arg != NULL)
267 {
268 /* "-nb:<host>:<addr>:<password>": get info from argument */
269 hostname = arg;
270 address = strchr(hostname, ':');
271 if (address != NULL)
272 {
273 *address++ = '\0';
274 password = strchr(address, ':');
275 if (password != NULL)
276 *password++ = '\0';
277 }
278 }
279
280 /* Get the missing values from the environment. */
281 if (hostname == NULL || *hostname == '\0')
282 hostname = getenv("__NETBEANS_HOST");
283 if (address == NULL)
284 address = getenv("__NETBEANS_SOCKET");
285 if (password == NULL)
286 password = getenv("__NETBEANS_VIM_PASSWORD");
287
288 /* Move values to allocated memory. */
289 if (hostname != NULL)
290 hostname = (char *)vim_strsave((char_u *)hostname);
291 if (address != NULL)
292 address = (char *)vim_strsave((char_u *)address);
293 if (password != NULL)
294 password = (char *)vim_strsave((char_u *)password);
295 }
296 }
297
298 /* Use the default when a value is missing. */
299 if (hostname == NULL || *hostname == '\0')
300 {
301 vim_free(hostname);
302 hostname = (char *)vim_strsave((char_u *)NB_DEF_HOST);
303 }
304 if (address == NULL || *address == '\0')
305 {
306 vim_free(address);
307 address = (char *)vim_strsave((char_u *)NB_DEF_ADDR);
308 }
309 if (password == NULL || *password == '\0')
310 {
311 vim_free(password);
312 password = (char *)vim_strsave((char_u *)NB_DEF_PASS);
313 }
314 if (hostname == NULL || address == NULL || password == NULL)
315 goto theend; /* out of memory */
316
317#ifdef INET_SOCKETS
318 port = atoi(address);
319
320 if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
321 {
322 PERROR("socket() in netbeans_connect()");
323 goto theend;
324 }
325
326 /* Get the server internet address and put into addr structure */
327 /* fill in the socket address structure and connect to server */
328 memset((char *)&server, '\0', sizeof(server));
329 server.sin_family = AF_INET;
330 server.sin_port = htons(port);
331 if ((host = gethostbyname(hostname)) == NULL)
332 {
333 if (mch_access(hostname, R_OK) >= 0)
334 {
335 /* DEBUG: input file */
336 sd = mch_open(hostname, O_RDONLY, 0);
337 goto theend;
338 }
339 PERROR("gethostbyname() in netbeans_connect()");
340 sd = -1;
341 goto theend;
342 }
343 memcpy((char *)&server.sin_addr, host->h_addr, host->h_length);
344#else
345 if ((sd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
346 {
347 PERROR("socket()");
348 goto theend;
349 }
350
351 server.sun_family = AF_UNIX;
352 strcpy(server.sun_path, address);
353#endif
354 /* Connect to server */
355 if (connect(sd, (struct sockaddr *)&server, sizeof(server)))
356 {
357 nbdebug(("netbeans_connect: Connect failed with errno %d\n", sock_errno));
358 if (sock_errno == ECONNREFUSED)
359 {
360 sock_close(sd);
361#ifdef INET_SOCKETS
362 if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
363 {
364 PERROR("socket()#2 in netbeans_connect()");
365 goto theend;
366 }
367#else
368 if ((sd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
369 {
370 PERROR("socket()#2 in netbeans_connect()");
371 goto theend;
372 }
373#endif
374 if (connect(sd, (struct sockaddr *)&server, sizeof(server)))
375 {
376 int retries = 36;
377 int success = FALSE;
378 while (retries--
379 && ((sock_errno == ECONNREFUSED) || (sock_errno == EINTR)))
380 {
381 nbdebug(("retrying...\n"));
382 sleep(5);
383 if (connect(sd, (struct sockaddr *)&server,
384 sizeof(server)) == 0)
385 {
386 success = TRUE;
387 break;
388 }
389 }
390 if (!success)
391 {
392 /* Get here when the server can't be found. */
393 PERROR(_("Cannot connect to Netbeans #2"));
394 getout(1);
395 }
396 }
397
398 }
399 else
400 {
401 PERROR(_("Cannot connect to Netbeans"));
402 getout(1);
403 }
404 }
405
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000406 vim_snprintf(buf, sizeof(buf), "AUTH %s\n", password);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 nb_send(buf, "netbeans_connect");
408
409 sprintf(buf, "0:version=0 \"%s\"\n", ExtEdProtocolVersion);
410 nb_send(buf, "externaleditor_version");
411
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412/* nb_init_graphics(); delay until needed */
413
414 haveConnection = TRUE;
415
416theend:
417 vim_free(hostname);
418 vim_free(address);
419 vim_free(password);
420 return;
421}
422
423/*
424 * Obtain the NetBeans hostname, port address and password from a file.
425 * Return the strings in allocated memory.
426 * Return FAIL if the file could not be read, OK otherwise (no matter what it
427 * contains).
428 */
429 static int
430getConnInfo(char *file, char **host, char **port, char **auth)
431{
432 FILE *fp;
433 char_u buf[BUFSIZ];
434 char_u *lp;
435 char_u *nl;
436#ifdef UNIX
437 struct stat st;
438
439 /*
440 * For Unix only accept the file when it's not accessible by others.
441 * The open will then fail if we don't own the file.
442 */
443 if (mch_stat(file, &st) == 0 && (st.st_mode & 0077) != 0)
444 {
445 EMSG2(_("E668: Wrong access mode for NetBeans connection info file: \"%s\""),
446 file);
447 return FAIL;
448 }
449#endif
450
451 fp = mch_fopen(file, "r");
452 if (fp == NULL)
453 {
454 PERROR("E660: Cannot open NetBeans connection info file");
455 return FAIL;
456 }
457
458 /* Read the file. There should be one of each parameter */
459 while ((lp = (char_u *)fgets((char *)buf, BUFSIZ, fp)) != NULL)
460 {
461 if ((nl = vim_strchr(lp, '\n')) != NULL)
462 *nl = 0; /* strip off the trailing newline */
463
464 if (STRNCMP(lp, "host=", 5) == 0)
465 {
466 vim_free(*host);
467 *host = (char *)vim_strsave(&buf[5]);
468 }
469 else if (STRNCMP(lp, "port=", 5) == 0)
470 {
471 vim_free(*port);
472 *port = (char *)vim_strsave(&buf[5]);
473 }
474 else if (STRNCMP(lp, "auth=", 5) == 0)
475 {
476 vim_free(*auth);
477 *auth = (char *)vim_strsave(&buf[5]);
478 }
479 }
480 fclose(fp);
481
482 return OK;
483}
484
485
486struct keyqueue
487{
488 int key;
489 struct keyqueue *next;
490 struct keyqueue *prev;
491};
492
493typedef struct keyqueue keyQ_T;
494
495static keyQ_T keyHead; /* dummy node, header for circular queue */
496
497
498/*
499 * Queue up key commands sent from netbeans.
500 */
501 static void
502postpone_keycommand(int key)
503{
504 keyQ_T *node;
505
506 node = (keyQ_T *)alloc(sizeof(keyQ_T));
507
508 if (keyHead.next == NULL) /* initialize circular queue */
509 {
510 keyHead.next = &keyHead;
511 keyHead.prev = &keyHead;
512 }
513
514 /* insert node at tail of queue */
515 node->next = &keyHead;
516 node->prev = keyHead.prev;
517 keyHead.prev->next = node;
518 keyHead.prev = node;
519
520 node->key = key;
521}
522
523/*
524 * Handle any queued-up NetBeans keycommands to be send.
525 */
526 static void
527handle_key_queue(void)
528{
529 while (keyHead.next && keyHead.next != &keyHead)
530 {
531 /* first, unlink the node */
532 keyQ_T *node = keyHead.next;
533 keyHead.next = node->next;
534 node->next->prev = node->prev;
535
536 /* now, send the keycommand */
537 netbeans_keycommand(node->key);
538
539 /* Finally, dispose of the node */
540 vim_free(node);
541 }
542}
543
544
545struct cmdqueue
546{
547 char_u *buffer;
548 struct cmdqueue *next;
549 struct cmdqueue *prev;
550};
551
552typedef struct cmdqueue queue_T;
553
554static queue_T head; /* dummy node, header for circular queue */
555
556
557/*
558 * Put the buffer on the work queue; possibly save it to a file as well.
559 */
560 static void
561save(char_u *buf, int len)
562{
563 queue_T *node;
564
565 node = (queue_T *)alloc(sizeof(queue_T));
566 if (node == NULL)
567 return; /* out of memory */
568 node->buffer = alloc(len + 1);
569 if (node->buffer == NULL)
570 {
571 vim_free(node);
572 return; /* out of memory */
573 }
574 mch_memmove(node->buffer, buf, (size_t)len);
575 node->buffer[len] = NUL;
576
577 if (head.next == NULL) /* initialize circular queue */
578 {
579 head.next = &head;
580 head.prev = &head;
581 }
582
583 /* insert node at tail of queue */
584 node->next = &head;
585 node->prev = head.prev;
586 head.prev->next = node;
587 head.prev = node;
588
589#ifdef NBDEBUG
590 {
591 static int outfd = -2;
592
593 /* possibly write buffer out to a file */
594 if (outfd == -3)
595 return;
596
597 if (outfd == -2)
598 {
599 char *file = getenv("__NETBEANS_SAVE");
600 if (file == NULL)
601 outfd = -3;
602 else
603 outfd = mch_open(file, O_WRONLY|O_CREAT|O_TRUNC, 0666);
604 }
605
606 if (outfd >= 0)
607 write(outfd, buf, len);
608 }
609#endif
610}
611
612
613/*
614 * While there's still a command in the work queue, parse and execute it.
615 */
616 static void
617nb_parse_messages(void)
618{
619 char_u *p;
620 queue_T *node;
621
622 while (head.next != &head)
623 {
624 node = head.next;
625
626 /* Locate the first line in the first buffer. */
627 p = vim_strchr(node->buffer, '\n');
628 if (p == NULL)
629 {
630 /* Command isn't complete. If there is no following buffer,
631 * return (wait for more). If there is another buffer following,
632 * prepend the text to that buffer and delete this one. */
633 if (node->next == &head)
634 return;
635 p = alloc(STRLEN(node->buffer) + STRLEN(node->next->buffer) + 1);
636 if (p == NULL)
637 return; /* out of memory */
638 STRCPY(p, node->buffer);
639 STRCAT(p, node->next->buffer);
640 vim_free(node->next->buffer);
641 node->next->buffer = p;
642
643 /* dispose of the node and buffer */
644 head.next = node->next;
645 node->next->prev = node->prev;
646 vim_free(node->buffer);
647 vim_free(node);
648 }
649 else
650 {
651 /* There is a complete command at the start of the buffer.
652 * Terminate it with a NUL. When no more text is following unlink
653 * the buffer. Do this before executing, because new buffers can
654 * be added while busy handling the command. */
655 *p++ = NUL;
656 if (*p == NUL)
657 {
658 head.next = node->next;
659 node->next->prev = node->prev;
660 }
661
662 /* now, parse and execute the commands */
663 nb_parse_cmd(node->buffer);
664
665 if (*p == NUL)
666 {
667 /* buffer finished, dispose of the node and buffer */
668 vim_free(node->buffer);
669 vim_free(node);
670 }
671 else
672 {
673 /* more follows, move to the start */
674 mch_memmove(node->buffer, p, STRLEN(p) + 1);
675 }
676 }
677 }
678}
679
680/* Buffer size for reading incoming messages. */
681#define MAXMSGSIZE 4096
682
683/*
684 * Read and process a command from netbeans.
685 */
686/*ARGSUSED*/
687#if defined(FEAT_GUI_W32) || defined(PROTO)
688/* Use this one when generating prototypes, the others are static. */
689 void
690messageFromNetbeansW32()
691#else
692# ifdef FEAT_GUI_MOTIF
693 static void
694messageFromNetbeans(XtPointer clientData, int *unused1, XtInputId *unused2)
695# endif
696# ifdef FEAT_GUI_GTK
697 static void
698messageFromNetbeans(gpointer clientData, gint unused1,
699 GdkInputCondition unused2)
700# endif
701#endif
702{
703 static char_u *buf = NULL;
704 int len;
705 int readlen = 0;
706 static int level = 0;
707
708 if (sd < 0)
709 {
710 nbdebug(("messageFromNetbeans() called without a socket\n"));
711 return;
712 }
713
714 ++level; /* recursion guard; this will be called from the X event loop */
715
716 /* Allocate a buffer to read into. */
717 if (buf == NULL)
718 {
719 buf = alloc(MAXMSGSIZE);
720 if (buf == NULL)
721 return; /* out of memory! */
722 }
723
724 /* Keep on reading for as long as there is something to read. */
725 for (;;)
726 {
727 len = sock_read(sd, buf, MAXMSGSIZE);
728 if (len <= 0)
729 break; /* error or nothing more to read */
730
731 /* Store the read message in the queue. */
732 save(buf, len);
733 readlen += len;
734 if (len < MAXMSGSIZE)
735 break; /* did read everything that's available */
736 }
737
738 if (readlen <= 0)
739 {
740 /* read error or didn't read anything */
741 netbeans_disconnect();
742 nbdebug(("messageFromNetbeans: Error in read() from socket\n"));
743 if (len < 0)
744 PERROR(_("read from Netbeans socket"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000745 return; /* don't try to parse it */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 }
747
748 /* Parse the messages, but avoid recursion. */
749 if (level == 1)
750 nb_parse_messages();
751
752 --level;
753}
754
755/*
756 * Handle one NUL terminated command.
757 *
758 * format of a command from netbeans:
759 *
760 * 6:setTitle!84 "a.c"
761 *
762 * bufno
763 * colon
764 * cmd
765 * !
766 * cmdno
767 * args
768 *
769 * for function calls, the ! is replaced by a /
770 */
771 static void
772nb_parse_cmd(char_u *cmd)
773{
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000774 char *verb;
775 char *q;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 int bufno;
777 int isfunc = -1;
778
779 if (STRCMP(cmd, "DISCONNECT") == 0)
780 {
781 /* We assume the server knows that we can safely exit! */
782 if (sd >= 0)
783 sock_close(sd);
784 /* Disconnect before exiting, Motif hangs in a Select error
785 * message otherwise. */
786 netbeans_disconnect();
787 getout(0);
788 /* NOTREACHED */
789 }
790
791 if (STRCMP(cmd, "DETACH") == 0)
792 {
793 /* The IDE is breaking the connection. */
794 if (sd >= 0)
795 sock_close(sd);
796 netbeans_disconnect();
797 return;
798 }
799
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000800 bufno = strtol((char *)cmd, &verb, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801
802 if (*verb != ':')
803 {
804 EMSG2("E627: missing colon: %s", cmd);
805 return;
806 }
807 ++verb; /* skip colon */
808
809 for (q = verb; *q; q++)
810 {
811 if (*q == '!')
812 {
813 *q++ = NUL;
814 isfunc = 0;
815 break;
816 }
817 else if (*q == '/')
818 {
819 *q++ = NUL;
820 isfunc = 1;
821 break;
822 }
823 }
824
825 if (isfunc < 0)
826 {
827 EMSG2("E628: missing ! or / in: %s", cmd);
828 return;
829 }
830
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000831 cmdno = strtol(q, &q, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000833 q = (char *)skipwhite((char_u *)q);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000835 if (nb_do_cmd(bufno, (char_u *)verb, isfunc, cmdno, (char_u *)q) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 {
Bram Moolenaar009b2592004-10-24 19:18:58 +0000837#ifdef NBDEBUG
838 /*
839 * This happens because the ExtEd can send a cammand or 2 after
840 * doing a stopDocumentListen command. It doesn't harm anything
841 * so I'm disabling it except for debugging.
842 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 nbdebug(("nb_parse_cmd: Command error for \"%s\"\n", cmd));
844 EMSG("E629: bad return from nb_do_cmd");
Bram Moolenaar009b2592004-10-24 19:18:58 +0000845#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 }
847}
848
849struct nbbuf_struct
850{
851 buf_T *bufp;
852 unsigned int fireChanges:1;
853 unsigned int initDone:1;
Bram Moolenaar009b2592004-10-24 19:18:58 +0000854 unsigned int insertDone:1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 unsigned int modified:1;
Bram Moolenaar009b2592004-10-24 19:18:58 +0000856 int nbbuf_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 char *displayname;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 int *signmap;
859 short_u signmaplen;
860 short_u signmapused;
861};
862
863typedef struct nbbuf_struct nbbuf_T;
864
865static nbbuf_T *buf_list = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000866static int buf_list_size = 0; /* size of buf_list */
867static int buf_list_used = 0; /* nr of entries in buf_list actually in use */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868
869static char **globalsignmap;
870static int globalsignmaplen;
871static int globalsignmapused;
872
873static int mapsigntype __ARGS((nbbuf_T *, int localsigntype));
874static void addsigntype __ARGS((nbbuf_T *, int localsigntype, char_u *typeName,
875 char_u *tooltip, char_u *glyphfile,
876 int usefg, int fg, int usebg, int bg));
Bram Moolenaar009b2592004-10-24 19:18:58 +0000877static void print_read_msg __ARGS((nbbuf_T *buf));
878static void print_save_msg __ARGS((nbbuf_T *buf, long nchars));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879
880static int curPCtype = -1;
881
882/*
883 * Get the Netbeans buffer number for the specified buffer.
884 */
885 static int
886nb_getbufno(buf_T *bufp)
887{
888 int i;
889
890 for (i = 0; i < buf_list_used; i++)
891 if (buf_list[i].bufp == bufp)
892 return i;
893 return -1;
894}
895
896/*
897 * Is this a NetBeans-owned buffer?
898 */
899 int
900isNetbeansBuffer(buf_T *bufp)
901{
Bram Moolenaar009b2592004-10-24 19:18:58 +0000902 return usingNetbeans && bufp->b_netbeans_file;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903}
904
905/*
906 * NetBeans and Vim have different undo models. In Vim, the file isn't
907 * changed if changes are undone via the undo command. In NetBeans, once
908 * a change has been made the file is marked as modified until saved. It
909 * doesn't matter if the change was undone.
910 *
911 * So this function is for the corner case where Vim thinks a buffer is
912 * unmodified but NetBeans thinks it IS modified.
913 */
914 int
915isNetbeansModified(buf_T *bufp)
916{
Bram Moolenaar009b2592004-10-24 19:18:58 +0000917 if (usingNetbeans && bufp->b_netbeans_file)
918 {
919 int bufno = nb_getbufno(bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920
Bram Moolenaar009b2592004-10-24 19:18:58 +0000921 if (bufno > 0)
922 return buf_list[bufno].modified;
923 else
924 return FALSE;
925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 else
927 return FALSE;
928}
929
930/*
931 * Given a Netbeans buffer number, return the netbeans buffer.
932 * Returns NULL for 0 or a negative number. A 0 bufno means a
933 * non-buffer related command has been sent.
934 */
935 static nbbuf_T *
936nb_get_buf(int bufno)
937{
938 /* find or create a buffer with the given number */
939 int incr;
940
941 if (bufno <= 0)
942 return NULL;
943
944 if (!buf_list)
945 {
946 /* initialize */
947 buf_list = (nbbuf_T *)alloc_clear(100 * sizeof(nbbuf_T));
948 buf_list_size = 100;
949 }
950 if (bufno >= buf_list_used) /* new */
951 {
952 if (bufno >= buf_list_size) /* grow list */
953 {
954 incr = bufno - buf_list_size + 90;
955 buf_list_size += incr;
956 buf_list = (nbbuf_T *)vim_realloc(
957 buf_list, buf_list_size * sizeof(nbbuf_T));
958 memset(buf_list + buf_list_size - incr, 0, incr * sizeof(nbbuf_T));
959 }
960
961 while (buf_list_used <= bufno)
962 {
963 /* Default is to fire text changes. */
964 buf_list[buf_list_used].fireChanges = 1;
965 ++buf_list_used;
966 }
967 }
968
969 return buf_list + bufno;
970}
971
972/*
973 * Return the number of buffers that are modified.
974 */
975 static int
976count_changed_buffers(void)
977{
978 buf_T *bufp;
979 int n;
980
981 n = 0;
982 for (bufp = firstbuf; bufp != NULL; bufp = bufp->b_next)
983 if (bufp->b_changed)
984 ++n;
985 return n;
986}
987
988/*
989 * End the netbeans session.
990 */
991 void
992netbeans_end(void)
993{
994 int i;
995 static char buf[128];
996
997 if (!haveConnection)
998 return;
999
1000 for (i = 0; i < buf_list_used; i++)
1001 {
1002 if (!buf_list[i].bufp)
1003 continue;
1004 if (netbeansForcedQuit)
1005 {
1006 /* mark as unmodified so NetBeans won't put up dialog on "killed" */
1007 sprintf(buf, "%d:unmodified=%d\n", i, cmdno);
1008 nbdebug(("EVT: %s", buf));
1009 nb_send(buf, "netbeans_end");
1010 }
1011 sprintf(buf, "%d:killed=%d\n", i, cmdno);
1012 nbdebug(("EVT: %s", buf));
1013/* nb_send(buf, "netbeans_end"); avoid "write failed" messages */
1014 if (sd >= 0)
1015 sock_write(sd, buf, STRLEN(buf)); /* ignore errors */
1016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017}
1018
1019/*
1020 * Send a message to netbeans.
1021 */
1022 static void
1023nb_send(char *buf, char *fun)
1024{
1025 /* Avoid giving pages full of error messages when the other side has
1026 * exited, only mention the first error until the connection works again. */
1027 static int did_error = FALSE;
1028
1029 if (sd < 0)
1030 {
1031 if (!did_error)
1032 EMSG2("E630: %s(): write while not connected", fun);
1033 did_error = TRUE;
1034 }
1035 else if (sock_write(sd, buf, STRLEN(buf)) != (int)STRLEN(buf))
1036 {
1037 if (!did_error)
1038 EMSG2("E631: %s(): write failed", fun);
1039 did_error = TRUE;
1040 }
1041 else
1042 did_error = FALSE;
1043}
1044
1045/*
1046 * Some input received from netbeans requires a response. This function
1047 * handles a response with no information (except the command number).
1048 */
1049 static void
1050nb_reply_nil(int cmdno)
1051{
1052 char reply[32];
1053
1054 if (!haveConnection)
1055 return;
1056
Bram Moolenaar009b2592004-10-24 19:18:58 +00001057 nbdebug(("REP %d: <none>\n", cmdno));
1058
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 sprintf(reply, "%d\n", cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 nb_send(reply, "nb_reply_nil");
1061}
1062
1063
1064/*
1065 * Send a response with text.
1066 * "result" must have been quoted already (using nb_quote()).
1067 */
1068 static void
1069nb_reply_text(int cmdno, char_u *result)
1070{
1071 char_u *reply;
1072
1073 if (!haveConnection)
1074 return;
1075
Bram Moolenaar009b2592004-10-24 19:18:58 +00001076 nbdebug(("REP %d: %s\n", cmdno, (char *)result));
1077
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 reply = alloc(STRLEN(result) + 32);
1079 sprintf((char *)reply, "%d %s\n", cmdno, (char *)result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 nb_send((char *)reply, "nb_reply_text");
1081
1082 vim_free(reply);
1083}
1084
1085
1086/*
1087 * Send a response with a number result code.
1088 */
1089 static void
1090nb_reply_nr(int cmdno, long result)
1091{
1092 char reply[32];
1093
1094 if (!haveConnection)
1095 return;
1096
Bram Moolenaar009b2592004-10-24 19:18:58 +00001097 nbdebug(("REP %d: %ld\n", cmdno, result));
1098
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 sprintf(reply, "%d %ld\n", cmdno, result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 nb_send(reply, "nb_reply_nr");
1101}
1102
1103
1104/*
1105 * Encode newline, ret, backslash, double quote for transmission to NetBeans.
1106 */
1107 static char_u *
1108nb_quote(char_u *txt)
1109{
1110 char_u *buf = alloc(2 * STRLEN(txt) + 1);
1111 char_u *p = txt;
1112 char_u *q = buf;
1113
1114 if (buf == NULL)
1115 return NULL;
1116 for (; *p; p++)
1117 {
1118 switch (*p)
1119 {
1120 case '\"':
1121 case '\\':
1122 *q++ = '\\'; *q++ = *p; break;
1123 /* case '\t': */
1124 /* *q++ = '\\'; *q++ = 't'; break; */
1125 case '\n':
1126 *q++ = '\\'; *q++ = 'n'; break;
1127 case '\r':
1128 *q++ = '\\'; *q++ = 'r'; break;
1129 default:
1130 *q++ = *p;
1131 break;
1132 }
1133 }
1134 *q++ = '\0';
1135
1136 return buf;
1137}
1138
1139
1140/*
1141 * Remove top level double quotes; convert backslashed chars.
1142 * Returns an allocated string (NULL for failure).
1143 * If "endp" is not NULL it is set to the character after the terminating
1144 * quote.
1145 */
1146 static char *
1147nb_unquote(char_u *p, char_u **endp)
1148{
1149 char *result = 0;
1150 char *q;
1151 int done = 0;
1152
1153 /* result is never longer than input */
1154 result = (char *)alloc_clear(STRLEN(p) + 1);
1155 if (result == NULL)
1156 return NULL;
1157
1158 if (*p++ != '"')
1159 {
1160 nbdebug(("nb_unquote called with string that doesn't start with a quote!: %s\n",
1161 p));
1162 result[0] = NUL;
1163 return result;
1164 }
1165
1166 for (q = result; !done && *p != NUL;)
1167 {
1168 switch (*p)
1169 {
1170 case '"':
1171 /*
1172 * Unbackslashed dquote marks the end, if first char was dquote.
1173 */
1174 done = 1;
1175 break;
1176
1177 case '\\':
1178 ++p;
1179 switch (*p)
1180 {
1181 case '\\': *q++ = '\\'; break;
1182 case 'n': *q++ = '\n'; break;
1183 case 't': *q++ = '\t'; break;
1184 case 'r': *q++ = '\r'; break;
1185 case '"': *q++ = '"'; break;
1186 case NUL: --p; break;
1187 /* default: skip over illegal chars */
1188 }
1189 ++p;
1190 break;
1191
1192 default:
1193 *q++ = *p++;
1194 }
1195 }
1196
1197 if (endp != NULL)
1198 *endp = p;
1199
1200 return result;
1201}
1202
1203#define SKIP_STOP 2
1204#define streq(a,b) (strcmp(a,b) == 0)
1205static int needupdate = 0;
1206static int inAtomic = 0;
1207
1208/*
1209 * Do the actual processing of a single netbeans command or function.
1210 * The differance between a command and function is that a function
1211 * gets a response (its required) but a command does not.
1212 * For arguments see comment for nb_parse_cmd().
1213 */
1214 static int
1215nb_do_cmd(
1216 int bufno,
1217 char_u *cmd,
1218 int func,
1219 int cmdno,
1220 char_u *args) /* points to space before arguments or NUL */
1221{
1222 int doupdate = 0;
1223 long off = 0;
1224 nbbuf_T *buf = nb_get_buf(bufno);
1225 static int skip = 0;
1226 int retval = OK;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001227 char *cp; /* for when a char pointer is needed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228
1229 nbdebug(("%s %d: (%d) %s %s\n", (func) ? "FUN" : "CMD", cmdno, bufno, cmd,
1230 STRCMP(cmd, "insert") == 0 ? "<text>" : (char *)args));
1231
1232 if (func)
1233 {
1234/* =====================================================================*/
1235 if (streq((char *)cmd, "getModified"))
1236 {
1237 if (buf == NULL || buf->bufp == NULL)
1238 /* Return the number of buffers that are modified. */
1239 nb_reply_nr(cmdno, (long)count_changed_buffers());
1240 else
1241 /* Return whether the buffer is modified. */
1242 nb_reply_nr(cmdno, (long)(buf->bufp->b_changed
1243 || isNetbeansModified(buf->bufp)));
1244/* =====================================================================*/
1245 }
1246 else if (streq((char *)cmd, "saveAndExit"))
1247 {
1248 /* Note: this will exit Vim if successful. */
1249 coloncmd(":confirm qall");
1250
1251 /* We didn't exit: return the number of changed buffers. */
1252 nb_reply_nr(cmdno, (long)count_changed_buffers());
1253/* =====================================================================*/
1254 }
1255 else if (streq((char *)cmd, "getCursor"))
1256 {
1257 char_u text[200];
1258
1259 /* Note: nb_getbufno() may return -1. This indicates the IDE
1260 * didn't assign a number to the current buffer in response to a
1261 * fileOpened event. */
1262 sprintf((char *)text, "%d %ld %d %ld",
1263 nb_getbufno(curbuf),
1264 (long)curwin->w_cursor.lnum,
1265 (int)curwin->w_cursor.col,
1266 pos2off(curbuf, &curwin->w_cursor));
1267 nb_reply_text(cmdno, text);
1268/* =====================================================================*/
1269 }
1270 else if (streq((char *)cmd, "getLength"))
1271 {
1272 long len = 0;
1273
1274 if (buf == NULL || buf->bufp == NULL)
1275 {
1276 nbdebug((" null bufp in getLength"));
1277 EMSG("E632: null bufp in getLength");
1278 retval = FAIL;
1279 }
1280 else
1281 {
1282 len = get_buf_size(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 }
1284 nb_reply_nr(cmdno, len);
1285/* =====================================================================*/
1286 }
1287 else if (streq((char *)cmd, "getText"))
1288 {
1289 long len;
1290 linenr_T nlines;
1291 char_u *text = NULL;
1292 linenr_T lno = 1;
1293 char_u *p;
1294 char_u *line;
1295
1296 if (buf == NULL || buf->bufp == NULL)
1297 {
1298 nbdebug((" null bufp in getText"));
1299 EMSG("E633: null bufp in getText");
1300 retval = FAIL;
1301 }
1302 else
1303 {
1304 len = get_buf_size(buf->bufp);
1305 nlines = buf->bufp->b_ml.ml_line_count;
1306 text = alloc((unsigned)((len > 0)
1307 ? ((len + nlines) * 2) : 4));
1308 if (text == NULL)
1309 {
1310 nbdebug((" nb_do_cmd: getText has null text field\n"));
1311 retval = FAIL;
1312 }
1313 else
1314 {
1315 p = text;
1316 *p++ = '\"';
1317 for (; lno <= nlines ; lno++)
1318 {
1319 line = nb_quote(ml_get_buf(buf->bufp, lno, FALSE));
1320 if (line != NULL)
1321 {
1322 STRCPY(p, line);
1323 p += STRLEN(line);
1324 *p++ = '\\';
1325 *p++ = 'n';
Bram Moolenaar009b2592004-10-24 19:18:58 +00001326 vim_free(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 }
1329 *p++ = '\"';
1330 *p = '\0';
1331 }
1332 }
1333 if (text == NULL)
1334 nb_reply_text(cmdno, (char_u *)"");
1335 else
1336 {
1337 nb_reply_text(cmdno, text);
1338 vim_free(text);
1339 }
1340/* =====================================================================*/
1341 }
1342 else if (streq((char *)cmd, "remove"))
1343 {
1344 long count;
1345 pos_T first, last;
1346 pos_T *pos;
1347 int oldFire = netbeansFireChanges;
1348 int oldSuppress = netbeansSuppressNoLines;
1349 int wasChanged;
1350
1351 if (skip >= SKIP_STOP)
1352 {
1353 nbdebug((" Skipping %s command\n", (char *) cmd));
1354 nb_reply_nil(cmdno);
1355 return OK;
1356 }
1357
1358 if (buf == NULL || buf->bufp == NULL)
1359 {
1360 nbdebug((" null bufp in remove"));
1361 EMSG("E634: null bufp in remove");
1362 retval = FAIL;
1363 }
1364 else
1365 {
1366 netbeansFireChanges = FALSE;
1367 netbeansSuppressNoLines = TRUE;
1368
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001369 nb_set_curbuf(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 wasChanged = buf->bufp->b_changed;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001371 cp = (char *)args;
1372 off = strtol(cp, &cp, 10);
1373 count = strtol(cp, &cp, 10);
1374 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 /* delete "count" chars, starting at "off" */
1376 pos = off2pos(buf->bufp, off);
1377 if (!pos)
1378 {
1379 nb_reply_text(cmdno, (char_u *)"!bad position");
1380 netbeansFireChanges = oldFire;
1381 netbeansSuppressNoLines = oldSuppress;
1382 return FAIL;
1383 }
1384 first = *pos;
1385 nbdebug((" FIRST POS: line %d, col %d\n", first.lnum, first.col));
1386 pos = off2pos(buf->bufp, off+count-1);
1387 if (!pos)
1388 {
1389 nb_reply_text(cmdno, (char_u *)"!bad count");
1390 netbeansFireChanges = oldFire;
1391 netbeansSuppressNoLines = oldSuppress;
1392 return FAIL;
1393 }
1394 last = *pos;
1395 nbdebug((" LAST POS: line %d, col %d\n", last.lnum, last.col));
1396 curwin->w_cursor = first;
1397 doupdate = 1;
1398
1399 /* keep part of first line */
1400 if (first.lnum == last.lnum && first.col != last.col)
1401 {
1402 /* deletion is within one line */
1403 char_u *p = ml_get(first.lnum);
1404 mch_memmove(p + first.col, p + last.col + 1, STRLEN(p + last.col) + 1);
1405 nbdebug((" NEW LINE %d: %s\n", first.lnum, p));
1406 ml_replace(first.lnum, p, TRUE);
1407 }
1408
1409 if (first.lnum < last.lnum)
1410 {
1411 int i;
1412
1413 /* delete signs from the lines being deleted */
1414 for (i = first.lnum; i <= last.lnum; i++)
1415 {
1416 int id = buf_findsign_id(buf->bufp, (linenr_T)i);
1417 if (id > 0)
1418 {
1419 nbdebug((" Deleting sign %d on line %d\n", id, i));
1420 buf_delsign(buf->bufp, id);
1421 }
1422 else
1423 nbdebug((" No sign on line %d\n", i));
1424 }
1425
1426 /* delete whole lines */
1427 nbdebug((" Deleting lines %d through %d\n", first.lnum, last.lnum));
1428 del_lines(last.lnum - first.lnum + 1, FALSE);
1429 }
1430 buf->bufp->b_changed = wasChanged; /* logically unchanged */
1431 netbeansFireChanges = oldFire;
1432 netbeansSuppressNoLines = oldSuppress;
1433
1434 u_blockfree(buf->bufp);
1435 u_clearall(buf->bufp);
1436 }
1437 nb_reply_nil(cmdno);
1438/* =====================================================================*/
1439 }
1440 else if (streq((char *)cmd, "insert"))
1441 {
1442 pos_T *pos;
1443 pos_T mypos;
1444 char_u *to_free;
1445 char_u *nl;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001446 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 pos_T old_w_cursor;
1448 int old_b_changed;
1449
1450 if (skip >= SKIP_STOP)
1451 {
1452 nbdebug((" Skipping %s command\n", (char *) cmd));
1453 nb_reply_nil(cmdno);
1454 return OK;
1455 }
1456
1457 /* get offset */
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001458 cp = (char *)args;
1459 off = strtol(cp, &cp, 10);
1460 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461
1462 /* get text to be inserted */
1463 args = skipwhite(args);
1464 args = to_free = (char_u *)nb_unquote(args, NULL);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001465 /*
1466 nbdebug((" CHUNK[%d]: %d bytes at offset %d\n",
1467 buf->bufp->b_ml.ml_line_count, STRLEN(args), off));
1468 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469
1470 if (buf == NULL || buf->bufp == NULL)
1471 {
1472 nbdebug((" null bufp in insert"));
1473 EMSG("E635: null bufp in insert");
1474 retval = FAIL;
1475 }
1476 else if (args != NULL)
1477 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00001478 /*
1479 * We need to detect EOL style
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 * because addAnno passes char-offset
1481 */
1482 int ff_detected = EOL_UNKNOWN;
1483 int buf_was_empty = (buf->bufp->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001484 char_u lbuf[4096]; /* size of largest insert sent by exted */
1485 int lbuf_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486
1487 oldFire = netbeansFireChanges;
1488 netbeansFireChanges = 0;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001489 lbuf[0] = '\0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001491 nb_set_curbuf(buf->bufp);
Bram Moolenaara3227e22006-03-08 21:32:40 +00001492 old_b_changed = curbuf->b_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493
Bram Moolenaara3227e22006-03-08 21:32:40 +00001494 pos = off2pos(curbuf, off);
1495 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 {
1497 if (pos->lnum == 0)
1498 pos->lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 }
1500 else
1501 {
1502 /* if the given position is not found, assume we want
1503 * the end of the file. See setLocAndSize HACK. */
1504 pos = &mypos;
1505 pos->col = 0;
1506#ifdef FEAT_VIRTUALEDIT
1507 pos->coladd = 0;
1508#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00001509 pos->lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 }
1511 lnum = pos->lnum;
1512 old_w_cursor = curwin->w_cursor;
1513 curwin->w_cursor = *pos;
1514
Bram Moolenaara3227e22006-03-08 21:32:40 +00001515 if (curbuf->b_start_eol == FALSE
1516 && lnum > 0
1517 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar009b2592004-10-24 19:18:58 +00001518 {
1519 /* Append to a partial line */
1520 char_u *partial = ml_get(lnum);
1521
Bram Moolenaara3227e22006-03-08 21:32:40 +00001522 STRCPY(lbuf, partial);
1523 lbuf_len = STRLEN(partial);
1524 ml_delete(lnum, FALSE);
1525 buf_was_empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001526 }
1527
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 doupdate = 1;
1529 while (*args)
1530 {
1531 nl = (char_u *)strchr((char *)args, '\n');
Bram Moolenaara3227e22006-03-08 21:32:40 +00001532 if (nl != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00001534 STRNCAT(lbuf, args, nl - args);
1535 lbuf[lbuf_len + nl - args] = '\0';
1536 args += nl - args + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001538 else
1539 {
1540 STRCPY(lbuf, args);
1541 args += STRLEN(lbuf);
1542 }
1543
1544 /*
1545 * EOL detecting. Not sure how to deal with '\n' on Mac.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 */
Bram Moolenaar009b2592004-10-24 19:18:58 +00001547 if (buf_was_empty && nl && *(nl - 1) != '\r')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 ff_detected = EOL_UNIX;
1549
Bram Moolenaar009b2592004-10-24 19:18:58 +00001550 /* nbdebug((" INSERT[%d]: %s\n", lnum, lbuf)); */
1551 ml_append((linenr_T)(lnum++ - 1), lbuf,
1552 STRLEN(lbuf) + 1, FALSE);
1553 lbuf[0] = '\0'; /* empty buffer */
1554 lbuf_len = 0;
1555 }
1556
1557 if (*(args - 1) == '\n')
1558 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00001559 curbuf->b_p_eol = TRUE;
1560 curbuf->b_start_eol = TRUE;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001561 }
1562 else
1563 {
Bram Moolenaara3227e22006-03-08 21:32:40 +00001564 curbuf->b_p_eol = FALSE;
1565 curbuf->b_start_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 }
1567
1568 appended_lines_mark(pos->lnum - 1, lnum - pos->lnum);
1569
1570 /* We can change initial ff without consequences
1571 * Isn't it a kind of hacking?
1572 */
1573 if (buf_was_empty)
1574 {
1575 if (ff_detected == EOL_UNKNOWN)
1576 ff_detected = EOL_DOS;
1577 set_fileformat(ff_detected, OPT_LOCAL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00001578 curbuf->b_start_ffc = *curbuf->b_p_ff;
1579
1580 /* Safety check: only delete empty line */
1581 if (*ml_get(curbuf->b_ml.ml_line_count) == NUL)
1582 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 }
1584
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 curwin->w_cursor = old_w_cursor;
1586
1587 /*
1588 * XXX - GRP - Is the next line right? If I've inserted
1589 * text the buffer has been updated but not written. Will
1590 * netbeans guarantee to write it? Even if I do a :q! ?
1591 */
Bram Moolenaara3227e22006-03-08 21:32:40 +00001592 curbuf->b_changed = old_b_changed; /* logically unchanged */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 netbeansFireChanges = oldFire;
1594
Bram Moolenaara3227e22006-03-08 21:32:40 +00001595 u_blockfree(curbuf);
1596 u_clearall(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 }
1598 vim_free(to_free);
1599 nb_reply_nil(cmdno); /* or !error */
1600 }
1601 else
1602 {
1603 nbdebug(("UNIMPLEMENTED FUNCTION: %s\n", cmd));
1604 nb_reply_nil(cmdno);
1605 retval = FAIL;
1606 }
1607 }
1608 else /* Not a function; no reply required. */
1609 {
1610/* =====================================================================*/
1611 if (streq((char *)cmd, "create"))
1612 {
1613 /* Create a buffer without a name. */
1614 if (buf == NULL)
1615 {
1616 EMSG("E636: null buf in create");
1617 return FAIL;
1618 }
1619 vim_free(buf->displayname);
1620 buf->displayname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621
1622 netbeansReadFile = 0; /* don't try to open disk file */
1623 do_ecmd(0, NULL, 0, 0, ECMD_ONE, ECMD_HIDE + ECMD_OLDBUF);
1624 netbeansReadFile = 1;
1625 buf->bufp = curbuf;
1626 maketitle();
Bram Moolenaar009b2592004-10-24 19:18:58 +00001627 buf->insertDone = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 gui_update_menus(0);
1629/* =====================================================================*/
1630 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001631 else if (streq((char *)cmd, "insertDone"))
1632 {
1633 buf->bufp->b_start_eol = *args == 'T';
1634 buf->insertDone = TRUE;
1635 args += 2;
1636 buf->bufp->b_p_ro = *args == 'T';
1637 print_read_msg(buf);
1638/* =====================================================================*/
1639 }
1640 else if (streq((char *)cmd, "saveDone"))
1641 {
1642 long savedChars = atol((char *) args);
1643 print_save_msg(buf, savedChars);
1644/* =====================================================================*/
1645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 else if (streq((char *)cmd, "startDocumentListen"))
1647 {
1648 if (buf == NULL)
1649 {
1650 EMSG("E637: null buf in startDocumentListen");
1651 return FAIL;
1652 }
1653 buf->fireChanges = 1;
1654/* =====================================================================*/
1655 }
1656 else if (streq((char *)cmd, "stopDocumentListen"))
1657 {
1658 if (buf == NULL)
1659 {
1660 EMSG("E638: null buf in stopDocumentListen");
1661 return FAIL;
1662 }
1663 buf->fireChanges = 0;
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001664 if (buf->bufp != NULL && buf->bufp->b_was_netbeans_file)
Bram Moolenaar009b2592004-10-24 19:18:58 +00001665 {
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001666 if (!buf->bufp->b_netbeans_file)
Bram Moolenaar009b2592004-10-24 19:18:58 +00001667 EMSGN(_("E658: NetBeans connection lost for buffer %ld"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 buf->bufp->b_fnum);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001669 else
1670 {
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001671 /* NetBeans uses stopDocumentListen when it stops editing
1672 * a file. It then expects the buffer in Vim to
1673 * disappear. */
1674 do_bufdel(DOBUF_DEL, (char_u *)"", 1,
1675 buf->bufp->b_fnum, buf->bufp->b_fnum, TRUE);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001676 vim_memset(buf, 0, sizeof(nbbuf_T));
1677 }
1678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679/* =====================================================================*/
1680 }
1681 else if (streq((char *)cmd, "setTitle"))
1682 {
1683 if (buf == NULL)
1684 {
1685 EMSG("E639: null buf in setTitle");
1686 return FAIL;
1687 }
1688 vim_free(buf->displayname);
1689 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690/* =====================================================================*/
1691 }
1692 else if (streq((char *)cmd, "initDone"))
1693 {
1694 if (buf == NULL || buf->bufp == NULL)
1695 {
1696 EMSG("E640: null buf in initDone");
1697 return FAIL;
1698 }
1699 doupdate = 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001700 buf->initDone = TRUE;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001701 nb_set_curbuf(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702#if defined(FEAT_AUTOCMD)
1703 apply_autocmds(EVENT_BUFREADPOST, 0, 0, FALSE, buf->bufp);
1704#endif
1705
1706 /* handle any postponed key commands */
1707 handle_key_queue();
1708/* =====================================================================*/
1709 }
1710 else if (streq((char *)cmd, "setBufferNumber")
1711 || streq((char *)cmd, "putBufferNumber"))
1712 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00001713 char_u *path;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 buf_T *bufp;
1715
1716 if (buf == NULL)
1717 {
1718 EMSG("E641: null buf in setBufferNumber");
1719 return FAIL;
1720 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001721 path = (char_u *)nb_unquote(args, NULL);
1722 if (path == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 return FAIL;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001724 bufp = buflist_findname(path);
1725 vim_free(path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 if (bufp == NULL)
1727 {
1728 EMSG2("E642: File %s not found in setBufferNumber", args);
1729 return FAIL;
1730 }
1731 buf->bufp = bufp;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001732 buf->nbbuf_number = bufp->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733
1734 /* "setBufferNumber" has the side effect of jumping to the buffer
1735 * (don't know why!). Don't do that for "putBufferNumber". */
1736 if (*cmd != 'p')
1737 coloncmd(":buffer %d", bufp->b_fnum);
1738 else
1739 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00001740 buf->initDone = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741
1742 /* handle any postponed key commands */
1743 handle_key_queue();
1744 }
1745
1746#if 0 /* never used */
1747 buf->internalname = (char *)alloc_clear(8);
1748 sprintf(buf->internalname, "<%d>", bufno);
1749 buf->netbeansOwns = 0;
1750#endif
1751/* =====================================================================*/
1752 }
1753 else if (streq((char *)cmd, "setFullName"))
1754 {
1755 if (buf == NULL)
1756 {
1757 EMSG("E643: null buf in setFullName");
1758 return FAIL;
1759 }
1760 vim_free(buf->displayname);
1761 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762
1763 netbeansReadFile = 0; /* don't try to open disk file */
1764 do_ecmd(0, (char_u *)buf->displayname, 0, 0, ECMD_ONE,
1765 ECMD_HIDE + ECMD_OLDBUF);
1766 netbeansReadFile = 1;
1767 buf->bufp = curbuf;
1768 maketitle();
1769 gui_update_menus(0);
1770/* =====================================================================*/
1771 }
1772 else if (streq((char *)cmd, "editFile"))
1773 {
1774 if (buf == NULL)
1775 {
1776 EMSG("E644: null buf in editFile");
1777 return FAIL;
1778 }
1779 /* Edit a file: like create + setFullName + read the file. */
1780 vim_free(buf->displayname);
1781 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 do_ecmd(0, (char_u *)buf->displayname, NULL, NULL, ECMD_ONE,
1783 ECMD_HIDE + ECMD_OLDBUF);
1784 buf->bufp = curbuf;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001785 buf->initDone = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 doupdate = 1;
1787#if defined(FEAT_TITLE)
1788 maketitle();
1789#endif
1790 gui_update_menus(0);
1791/* =====================================================================*/
1792 }
1793 else if (streq((char *)cmd, "setVisible"))
1794 {
1795 if (buf == NULL || buf->bufp == NULL)
1796 {
1797/* EMSG("E645: null bufp in setVisible"); */
1798 return FAIL;
1799 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001800 if (streq((char *)args, "T") && buf->bufp != curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 {
1802 exarg_T exarg;
1803 exarg.cmd = (char_u *)"goto";
1804 exarg.forceit = FALSE;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001805 dosetvisible = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 goto_buffer(&exarg, DOBUF_FIRST, FORWARD, buf->bufp->b_fnum);
1807 doupdate = 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001808 dosetvisible = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809
1810 /* Side effect!!!. */
1811 if (!gui.starting)
1812 gui_mch_set_foreground();
1813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814/* =====================================================================*/
1815 }
1816 else if (streq((char *)cmd, "raise"))
1817 {
1818 /* Bring gvim to the foreground. */
1819 if (!gui.starting)
1820 gui_mch_set_foreground();
1821/* =====================================================================*/
1822 }
1823 else if (streq((char *)cmd, "setModified"))
1824 {
1825 if (buf == NULL || buf->bufp == NULL)
1826 {
1827/* EMSG("E646: null bufp in setModified"); */
1828 return FAIL;
1829 }
1830 if (streq((char *)args, "T"))
1831 buf->bufp->b_changed = 1;
1832 else
1833 {
1834 struct stat st;
1835
1836 /* Assume NetBeans stored the file. Reset the timestamp to
1837 * avoid "file changed" warnings. */
1838 if (buf->bufp->b_ffname != NULL
1839 && mch_stat((char *)buf->bufp->b_ffname, &st) >= 0)
1840 buf_store_time(buf->bufp, &st, buf->bufp->b_ffname);
1841 buf->bufp->b_changed = 0;
1842 }
1843 buf->modified = buf->bufp->b_changed;
1844/* =====================================================================*/
1845 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001846 else if (streq((char *)cmd, "setModtime"))
1847 {
1848 buf->bufp->b_mtime = atoi((char *) args);
1849/* =====================================================================*/
1850 }
1851 else if (streq((char *)cmd, "setReadOnly"))
1852 {
1853 if (streq((char *)args, "T"))
1854 buf->bufp->b_p_ro = TRUE;
1855 else
1856 buf->bufp->b_p_ro = FALSE;
1857/* =====================================================================*/
1858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 else if (streq((char *)cmd, "setMark"))
1860 {
1861 /* not yet */
1862/* =====================================================================*/
1863 }
1864 else if (streq((char *)cmd, "showBalloon"))
1865 {
1866#if defined(FEAT_BEVAL)
1867 static char *text = NULL;
1868
1869 /*
1870 * Set up the Balloon Expression Evaluation area.
1871 * Ignore 'ballooneval' here.
1872 * The text pointer must remain valid for a while.
1873 */
1874 if (balloonEval != NULL)
1875 {
1876 vim_free(text);
1877 text = nb_unquote(args, NULL);
1878 if (text != NULL)
1879 gui_mch_post_balloon(balloonEval, (char_u *)text);
1880 }
1881#endif
1882/* =====================================================================*/
1883 }
1884 else if (streq((char *)cmd, "setDot"))
1885 {
1886 pos_T *pos;
1887#ifdef NBDEBUG
1888 char_u *s;
1889#endif
1890
1891 if (buf == NULL || buf->bufp == NULL)
1892 {
1893 EMSG("E647: null bufp in setDot");
1894 return FAIL;
1895 }
1896
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001897 nb_set_curbuf(buf->bufp);
1898
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899#ifdef FEAT_VISUAL
1900 /* Don't want Visual mode now. */
1901 if (VIsual_active)
1902 end_visual_mode();
1903#endif
1904#ifdef NBDEBUG
1905 s = args;
1906#endif
1907 pos = get_off_or_lnum(buf->bufp, &args);
1908 if (pos)
1909 {
1910 curwin->w_cursor = *pos;
1911 check_cursor();
1912#ifdef FEAT_FOLDING
1913 foldOpenCursor();
1914#endif
1915 }
1916 else
1917 nbdebug((" BAD POSITION in setDot: %s\n", s));
1918
1919 /* gui_update_cursor(TRUE, FALSE); */
1920 /* update_curbuf(NOT_VALID); */
1921 update_topline(); /* scroll to show the line */
1922 update_screen(VALID);
1923 setcursor();
1924 out_flush();
1925 gui_update_cursor(TRUE, FALSE);
1926 gui_mch_flush();
1927 /* Quit a hit-return or more prompt. */
1928 if (State == HITRETURN || State == ASKMORE)
1929 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930#ifdef FEAT_GUI_GTK
1931 if (gtk_main_level() > 0)
1932 gtk_main_quit();
1933#endif
1934 }
1935/* =====================================================================*/
1936 }
1937 else if (streq((char *)cmd, "close"))
1938 {
1939#ifdef NBDEBUG
1940 char *name = "<NONE>";
1941#endif
1942
1943 if (buf == NULL)
1944 {
1945 EMSG("E648: null buf in close");
1946 return FAIL;
1947 }
1948
1949#ifdef NBDEBUG
1950 if (buf->displayname != NULL)
1951 name = buf->displayname;
1952#endif
1953/* if (buf->bufp == NULL) */
1954/* EMSG("E649: null bufp in close"); */
1955 nbdebug((" CLOSE %d: %s\n", bufno, name));
1956 need_mouse_correct = TRUE;
1957 if (buf->bufp != NULL)
1958 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD,
1959 buf->bufp->b_fnum, TRUE);
1960 doupdate = 1;
1961/* =====================================================================*/
1962 }
1963 else if (streq((char *)cmd, "setStyle")) /* obsolete... */
1964 {
1965 nbdebug((" setStyle is obsolete!"));
1966/* =====================================================================*/
1967 }
1968 else if (streq((char *)cmd, "setExitDelay"))
1969 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00001970 /* Only used in version 2.1. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971/* =====================================================================*/
1972 }
1973 else if (streq((char *)cmd, "defineAnnoType"))
1974 {
1975#ifdef FEAT_SIGNS
1976 int typeNum;
1977 char_u *typeName;
1978 char_u *tooltip;
1979 char_u *p;
1980 char_u *glyphFile;
1981 int use_fg = 0;
1982 int use_bg = 0;
1983 int fg = -1;
1984 int bg = -1;
1985
1986 if (buf == NULL)
1987 {
1988 EMSG("E650: null buf in defineAnnoType");
1989 return FAIL;
1990 }
1991
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001992 cp = (char *)args;
1993 typeNum = strtol(cp, &cp, 10);
1994 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 args = skipwhite(args);
1996 typeName = (char_u *)nb_unquote(args, &args);
1997 args = skipwhite(args + 1);
1998 tooltip = (char_u *)nb_unquote(args, &args);
1999 args = skipwhite(args + 1);
2000
2001 p = (char_u *)nb_unquote(args, &args);
2002 glyphFile = vim_strsave_escaped(p, escape_chars);
2003 vim_free(p);
2004
2005 args = skipwhite(args + 1);
2006 if (STRNCMP(args, "none", 4) == 0)
2007 args += 5;
2008 else
2009 {
2010 use_fg = 1;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002011 cp = (char *)args;
2012 fg = strtol(cp, &cp, 10);
2013 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 }
2015 if (STRNCMP(args, "none", 4) == 0)
2016 args += 5;
2017 else
2018 {
2019 use_bg = 1;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002020 cp = (char *)args;
2021 bg = strtol(cp, &cp, 10);
2022 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 }
2024 if (typeName != NULL && tooltip != NULL && glyphFile != NULL)
2025 addsigntype(buf, typeNum, typeName, tooltip, glyphFile,
2026 use_fg, fg, use_bg, bg);
2027 else
2028 vim_free(typeName);
2029
2030 /* don't free typeName; it's used directly in addsigntype() */
2031 vim_free(tooltip);
2032 vim_free(glyphFile);
2033
2034#endif
2035/* =====================================================================*/
2036 }
2037 else if (streq((char *)cmd, "addAnno"))
2038 {
2039#ifdef FEAT_SIGNS
2040 int serNum;
2041 int localTypeNum;
2042 int typeNum;
2043# ifdef NBDEBUG
2044 int len;
2045# endif
2046 pos_T *pos;
2047
2048 if (buf == NULL || buf->bufp == NULL)
2049 {
2050 EMSG("E651: null bufp in addAnno");
2051 return FAIL;
2052 }
2053
2054 doupdate = 1;
2055
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002056 cp = (char *)args;
2057 serNum = strtol(cp, &cp, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058
2059 /* Get the typenr specific for this buffer and convert it to
2060 * the global typenumber, as used for the sign name. */
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002061 localTypeNum = strtol(cp, &cp, 10);
2062 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 typeNum = mapsigntype(buf, localTypeNum);
2064
2065 pos = get_off_or_lnum(buf->bufp, &args);
2066
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002067 cp = (char *)args;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068# ifdef NBDEBUG
2069 len =
2070# endif
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002071 strtol(cp, &cp, 10);
2072 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073# ifdef NBDEBUG
2074 if (len != -1)
2075 {
2076 nbdebug((" partial line annotation -- Not Yet Implemented!"));
2077 }
2078# endif
2079 if (serNum >= GUARDEDOFFSET)
2080 {
2081 nbdebug((" too many annotations! ignoring..."));
2082 return FAIL;
2083 }
2084 if (pos)
2085 {
2086 coloncmd(":sign place %d line=%d name=%d buffer=%d",
2087 serNum, pos->lnum, typeNum, buf->bufp->b_fnum);
2088 if (typeNum == curPCtype)
2089 coloncmd(":sign jump %d buffer=%d", serNum,
2090 buf->bufp->b_fnum);
2091 }
2092 /* XXX only redraw what changed. */
2093 redraw_later(CLEAR);
2094#endif
2095/* =====================================================================*/
2096 }
2097 else if (streq((char *)cmd, "removeAnno"))
2098 {
2099#ifdef FEAT_SIGNS
2100 int serNum;
2101
2102 if (buf == NULL || buf->bufp == NULL)
2103 {
2104 nbdebug((" null bufp in removeAnno"));
2105 return FAIL;
2106 }
2107 doupdate = 1;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002108 cp = (char *)args;
2109 serNum = strtol(cp, &cp, 10);
2110 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 coloncmd(":sign unplace %d buffer=%d",
2112 serNum, buf->bufp->b_fnum);
2113 redraw_buf_later(buf->bufp, NOT_VALID);
2114#endif
2115/* =====================================================================*/
2116 }
2117 else if (streq((char *)cmd, "moveAnnoToFront"))
2118 {
2119#ifdef FEAT_SIGNS
2120 nbdebug((" moveAnnoToFront: Not Yet Implemented!"));
2121#endif
2122/* =====================================================================*/
2123 }
2124 else if (streq((char *)cmd, "guard") || streq((char *)cmd, "unguard"))
2125 {
2126 int len;
2127 pos_T first;
2128 pos_T last;
2129 pos_T *pos;
2130 int un = (cmd[0] == 'u');
2131 static int guardId = GUARDEDOFFSET;
2132
2133 if (skip >= SKIP_STOP)
2134 {
2135 nbdebug((" Skipping %s command\n", (char *) cmd));
2136 return OK;
2137 }
2138
2139 nb_init_graphics();
2140
2141 if (buf == NULL || buf->bufp == NULL)
2142 {
2143 nbdebug((" null bufp in %s command", cmd));
2144 return FAIL;
2145 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002146 nb_set_curbuf(buf->bufp);
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002147 cp = (char *)args;
2148 off = strtol(cp, &cp, 10);
2149 len = strtol(cp, NULL, 10);
2150 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 pos = off2pos(buf->bufp, off);
2152 doupdate = 1;
2153 if (!pos)
2154 nbdebug((" no such start pos in %s, %ld\n", cmd, off));
2155 else
2156 {
2157 first = *pos;
2158 pos = off2pos(buf->bufp, off + len - 1);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002159 if (pos != NULL && pos->col == 0)
2160 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 /*
2162 * In Java Swing the offset is a position between 2
2163 * characters. If col == 0 then we really want the
2164 * previous line as the end.
2165 */
2166 pos = off2pos(buf->bufp, off + len - 2);
2167 }
2168 if (!pos)
2169 nbdebug((" no such end pos in %s, %ld\n",
2170 cmd, off + len - 1));
2171 else
2172 {
2173 long lnum;
2174 last = *pos;
2175 /* set highlight for region */
2176 nbdebug((" %sGUARD %ld,%d to %ld,%d\n", (un) ? "UN" : "",
2177 first.lnum, first.col,
2178 last.lnum, last.col));
2179#ifdef FEAT_SIGNS
2180 for (lnum = first.lnum; lnum <= last.lnum; lnum++)
2181 {
2182 if (un)
2183 {
2184 /* never used */
2185 }
2186 else
2187 {
2188 if (buf_findsigntype_id(buf->bufp, lnum,
2189 GUARDED) == 0)
2190 {
2191 coloncmd(
2192 ":sign place %d line=%d name=%d buffer=%d",
2193 guardId++, lnum, GUARDED,
2194 buf->bufp->b_fnum);
2195 }
2196 }
2197 }
2198#endif
2199 redraw_buf_later(buf->bufp, NOT_VALID);
2200 }
2201 }
2202/* =====================================================================*/
2203 }
2204 else if (streq((char *)cmd, "startAtomic"))
2205 {
2206 inAtomic = 1;
2207/* =====================================================================*/
2208 }
2209 else if (streq((char *)cmd, "endAtomic"))
2210 {
2211 inAtomic = 0;
2212 if (needupdate)
2213 {
2214 doupdate = 1;
2215 needupdate = 0;
2216 }
2217/* =====================================================================*/
2218 }
2219 else if (streq((char *)cmd, "save"))
2220 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002221 /*
2222 * NOTE - This command is obsolete wrt NetBeans. Its left in
2223 * only for historical reasons.
2224 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 if (buf == NULL || buf->bufp == NULL)
2226 {
2227 nbdebug((" null bufp in %s command", cmd));
2228 return FAIL;
2229 }
2230
2231 /* the following is taken from ex_cmds.c (do_wqall function) */
2232 if (bufIsChanged(buf->bufp))
2233 {
2234 /* Only write if the buffer can be written. */
2235 if (p_write
2236 && !buf->bufp->b_p_ro
2237 && buf->bufp->b_ffname != NULL
2238#ifdef FEAT_QUICKFIX
2239 && !bt_dontwrite(buf->bufp)
2240#endif
2241 )
2242 {
2243 buf_write_all(buf->bufp, FALSE);
2244#ifdef FEAT_AUTOCMD
2245 /* an autocommand may have deleted the buffer */
2246 if (!buf_valid(buf->bufp))
2247 buf->bufp = NULL;
2248#endif
2249 }
2250 }
2251/* =====================================================================*/
2252 }
2253 else if (streq((char *)cmd, "netbeansBuffer"))
2254 {
2255 if (buf == NULL || buf->bufp == NULL)
2256 {
2257 nbdebug((" null bufp in %s command", cmd));
2258 return FAIL;
2259 }
2260 if (*args == 'T')
2261 {
2262 buf->bufp->b_netbeans_file = TRUE;
2263 buf->bufp->b_was_netbeans_file = TRUE;
2264 }
2265 else
2266 buf->bufp->b_netbeans_file = FALSE;
2267/* =====================================================================*/
2268 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002269 else if (streq((char *)cmd, "specialKeys"))
2270 {
2271 special_keys(args);
2272/* =====================================================================*/
2273 }
2274 else if (streq((char *)cmd, "actionMenuItem"))
2275 {
2276 /* not used yet */
2277/* =====================================================================*/
2278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 else if (streq((char *)cmd, "version"))
2280 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002281 /* not used yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 }
2283 /*
2284 * Unrecognized command is ignored.
2285 */
2286 }
2287 if (inAtomic && doupdate)
2288 {
2289 needupdate = 1;
2290 doupdate = 0;
2291 }
2292
Bram Moolenaar009b2592004-10-24 19:18:58 +00002293 /*
2294 * Is this needed? I moved the netbeans_Xt_connect() later during startup
2295 * and it may no longer be necessary. If its not needed then needupdate
2296 * and doupdate can also be removed.
2297 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 if (buf != NULL && buf->initDone && doupdate)
2299 {
2300 update_screen(NOT_VALID);
2301 setcursor();
2302 out_flush();
2303 gui_update_cursor(TRUE, FALSE);
2304 gui_mch_flush();
2305 /* Quit a hit-return or more prompt. */
2306 if (State == HITRETURN || State == ASKMORE)
2307 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308#ifdef FEAT_GUI_GTK
2309 if (gtk_main_level() > 0)
2310 gtk_main_quit();
2311#endif
2312 }
2313 }
2314
2315 return retval;
2316}
2317
2318
2319/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002320 * If "buf" is not the current buffer try changing to a window that edits this
2321 * buffer. If there is no such window then close the current buffer and set
2322 * the current buffer as "buf".
2323 */
2324 static void
2325nb_set_curbuf(buf)
2326 buf_T *buf;
2327{
2328 if (curbuf != buf && buf_jump_open_win(buf) == NULL)
2329 set_curbuf(buf, DOBUF_GOTO);
2330}
2331
2332/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 * Process a vim colon command.
2334 */
2335 static void
2336coloncmd(char *cmd, ...)
2337{
2338 char buf[1024];
2339 va_list ap;
2340
2341 va_start(ap, cmd);
2342 vsprintf(buf, cmd, ap);
2343 va_end(ap);
2344
2345 nbdebug((" COLONCMD %s\n", buf));
2346
2347/* ALT_INPUT_LOCK_ON; */
2348 do_cmdline((char_u *)buf, NULL, NULL, DOCMD_NOWAIT | DOCMD_KEYTYPED);
2349/* ALT_INPUT_LOCK_OFF; */
2350
2351 setcursor(); /* restore the cursor position */
2352 out_flush(); /* make sure output has been written */
2353
2354 gui_update_cursor(TRUE, FALSE);
2355 gui_mch_flush();
2356}
2357
2358
2359/*
Bram Moolenaar009b2592004-10-24 19:18:58 +00002360 * Parse the specialKeys argument and issue the appropriate map commands.
2361 */
2362 static void
2363special_keys(char_u *args)
2364{
2365 char *save_str = nb_unquote(args, NULL);
2366 char *tok = strtok(save_str, " ");
2367 char *sep;
2368 char keybuf[64];
2369 char cmdbuf[256];
2370
2371 while (tok != NULL)
2372 {
2373 int i = 0;
2374
2375 if ((sep = strchr(tok, '-')) != NULL)
2376 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002377 *sep = NUL;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002378 while (*tok)
2379 {
2380 switch (*tok)
2381 {
2382 case 'A':
2383 case 'M':
2384 case 'C':
2385 case 'S':
2386 keybuf[i++] = *tok;
2387 keybuf[i++] = '-';
2388 break;
2389 }
2390 tok++;
2391 }
2392 tok++;
2393 }
2394
2395 strcpy(&keybuf[i], tok);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002396 vim_snprintf(cmdbuf, sizeof(cmdbuf),
2397 "<silent><%s> :nbkey %s<CR>", keybuf, keybuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002398 do_map(0, (char_u *)cmdbuf, NORMAL, FALSE);
2399 tok = strtok(NULL, " ");
2400 }
2401 vim_free(save_str);
2402}
2403
2404
2405 void
2406ex_nbkey(eap)
2407 exarg_T *eap;
2408{
2409 netbeans_keystring(0, (char *)eap->arg);
2410}
2411
2412
2413/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 * Initialize highlights and signs for use by netbeans (mostly obsolete)
2415 */
2416 static void
2417nb_init_graphics(void)
2418{
2419 static int did_init = FALSE;
2420
2421 if (!did_init)
2422 {
2423 coloncmd(":highlight NBGuarded guibg=Cyan guifg=Black");
2424 coloncmd(":sign define %d linehl=NBGuarded", GUARDED);
2425
2426 did_init = TRUE;
2427 }
2428}
2429
2430/*
2431 * Convert key to netbeans name.
2432 */
2433 static void
2434netbeans_keyname(int key, char *buf)
2435{
2436 char *name = 0;
2437 char namebuf[2];
2438 int ctrl = 0;
2439 int shift = 0;
2440 int alt = 0;
2441
2442 if (mod_mask & MOD_MASK_CTRL)
2443 ctrl = 1;
2444 if (mod_mask & MOD_MASK_SHIFT)
2445 shift = 1;
2446 if (mod_mask & MOD_MASK_ALT)
2447 alt = 1;
2448
2449
2450 switch (key)
2451 {
2452 case K_F1: name = "F1"; break;
2453 case K_S_F1: name = "F1"; shift = 1; break;
2454 case K_F2: name = "F2"; break;
2455 case K_S_F2: name = "F2"; shift = 1; break;
2456 case K_F3: name = "F3"; break;
2457 case K_S_F3: name = "F3"; shift = 1; break;
2458 case K_F4: name = "F4"; break;
2459 case K_S_F4: name = "F4"; shift = 1; break;
2460 case K_F5: name = "F5"; break;
2461 case K_S_F5: name = "F5"; shift = 1; break;
2462 case K_F6: name = "F6"; break;
2463 case K_S_F6: name = "F6"; shift = 1; break;
2464 case K_F7: name = "F7"; break;
2465 case K_S_F7: name = "F7"; shift = 1; break;
2466 case K_F8: name = "F8"; break;
2467 case K_S_F8: name = "F8"; shift = 1; break;
2468 case K_F9: name = "F9"; break;
2469 case K_S_F9: name = "F9"; shift = 1; break;
2470 case K_F10: name = "F10"; break;
2471 case K_S_F10: name = "F10"; shift = 1; break;
2472 case K_F11: name = "F11"; break;
2473 case K_S_F11: name = "F11"; shift = 1; break;
2474 case K_F12: name = "F12"; break;
2475 case K_S_F12: name = "F12"; shift = 1; break;
2476 default:
2477 if (key >= ' ' && key <= '~')
2478 {
2479 /* Allow ASCII characters. */
2480 name = namebuf;
2481 namebuf[0] = key;
2482 namebuf[1] = NUL;
2483 }
2484 else
2485 name = "X";
2486 break;
2487 }
2488
2489 buf[0] = '\0';
2490 if (ctrl)
2491 strcat(buf, "C");
2492 if (shift)
2493 strcat(buf, "S");
2494 if (alt)
2495 strcat(buf, "M"); /* META */
2496 if (ctrl || shift || alt)
2497 strcat(buf, "-");
2498 strcat(buf, name);
2499}
2500
2501#ifdef FEAT_BEVAL
2502/*
2503 * Function to be called for balloon evaluation. Grabs the text under the
2504 * cursor and sends it to the debugger for evaluation. The debugger should
2505 * respond with a showBalloon command when there is a useful result.
2506 */
2507/*ARGSUSED*/
Bram Moolenaard62bec82005-03-07 22:56:57 +00002508 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509netbeans_beval_cb(
2510 BalloonEval *beval,
2511 int state)
2512{
Bram Moolenaard62bec82005-03-07 22:56:57 +00002513 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 char_u *text;
Bram Moolenaard62bec82005-03-07 22:56:57 +00002515 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 int col;
2517 char buf[MAXPATHL * 2 + 25];
2518 char_u *p;
2519
2520 /* Don't do anything when 'ballooneval' is off, messages scrolled the
2521 * windows up or we have no connection. */
2522 if (!p_beval || msg_scrolled > 0 || !haveConnection)
2523 return;
2524
Bram Moolenaard62bec82005-03-07 22:56:57 +00002525 if (get_beval_info(beval, TRUE, &wp, &lnum, &text, &col) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 {
2527 /* Send debugger request. Only when the text is of reasonable
2528 * length. */
2529 if (text != NULL && text[0] != NUL && STRLEN(text) < MAXPATHL)
2530 {
2531 p = nb_quote(text);
2532 if (p != NULL)
Bram Moolenaar009b2592004-10-24 19:18:58 +00002533 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002534 vim_snprintf(buf, sizeof(buf),
2535 "0:balloonText=%d \"%s\"\n", cmdno, p);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002536 vim_free(p);
2537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 nbdebug(("EVT: %s", buf));
2539 nb_send(buf, "netbeans_beval_cb");
2540 }
2541 vim_free(text);
2542 }
2543}
2544#endif
2545
2546/*
2547 * Tell netbeans that the window was opened, ready for commands.
2548 */
2549 void
2550netbeans_startup_done(void)
2551{
2552 char *cmd = "0:startupDone=0\n";
2553
Bram Moolenaar009b2592004-10-24 19:18:58 +00002554 if (usingNetbeans)
2555#ifdef FEAT_GUI_MOTIF
2556 netbeans_Xt_connect(app_context);
2557#else
2558# ifdef FEAT_GUI_GTK
2559 netbeans_gtk_connect();
Bram Moolenaardf177f62005-02-22 08:39:57 +00002560# else
2561# ifdef FEAT_GUI_W32
2562 netbeans_w32_connect();
2563# endif
Bram Moolenaar009b2592004-10-24 19:18:58 +00002564# endif
2565#endif
2566
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 if (!haveConnection)
2568 return;
2569
Bram Moolenaard62bec82005-03-07 22:56:57 +00002570#ifdef FEAT_BEVAL
2571 bevalServers |= BEVAL_NETBEANS;
2572#endif
2573
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 nbdebug(("EVT: %s", cmd));
2575 nb_send(cmd, "netbeans_startup_done");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576}
2577
Bram Moolenaar009b2592004-10-24 19:18:58 +00002578/*
2579 * Tell netbeans that we're exiting. This should be called right
2580 * before calling exit.
2581 */
2582 void
2583netbeans_send_disconnect()
2584{
2585 char buf[128];
2586
2587 if (haveConnection)
2588 {
2589 sprintf(buf, "0:disconnect=%d\n", cmdno);
2590 nbdebug(("EVT: %s", buf));
2591 nb_send(buf, "netbeans_disconnect");
2592 }
2593}
2594
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_W32) || defined(PROTO)
2596/*
2597 * Tell netbeans that the window was moved or resized.
2598 */
2599 void
2600netbeans_frame_moved(int new_x, int new_y)
2601{
2602 char buf[128];
2603
2604 if (!haveConnection)
2605 return;
2606
2607 sprintf(buf, "0:geometry=%d %d %d %d %d\n",
2608 cmdno, (int)Columns, (int)Rows, new_x, new_y);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002609 /*nbdebug(("EVT: %s", buf)); happens too many times during a move */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 nb_send(buf, "netbeans_frame_moved");
2611}
2612#endif
2613
2614/*
Bram Moolenaar009b2592004-10-24 19:18:58 +00002615 * Tell netbeans the user opened or activated a file.
2616 */
2617 void
2618netbeans_file_activated(buf_T *bufp)
2619{
2620 int bufno = nb_getbufno(bufp);
2621 nbbuf_T *bp = nb_get_buf(bufno);
2622 char buffer[2*MAXPATHL];
2623 char_u *q;
2624
2625 if (!haveConnection || dosetvisible)
2626 return;
2627
2628 q = nb_quote(bufp->b_ffname);
2629 if (q == NULL || bp == NULL || bufp == NULL)
2630 return;
2631
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002632 vim_snprintf(buffer, sizeof(buffer), "%d:fileOpened=%d \"%s\" %s %s\n",
Bram Moolenaar009b2592004-10-24 19:18:58 +00002633 bufno,
2634 bufno,
2635 (char *)q,
2636 "T", /* open in NetBeans */
2637 "F"); /* modified */
2638
2639 vim_free(q);
2640 nbdebug(("EVT: %s", buffer));
2641
2642 nb_send(buffer, "netbeans_file_opened");
2643}
2644
2645/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 * Tell netbeans the user opened a file.
2647 */
2648 void
Bram Moolenaar009b2592004-10-24 19:18:58 +00002649netbeans_file_opened(buf_T *bufp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650{
Bram Moolenaar009b2592004-10-24 19:18:58 +00002651 int bufno = nb_getbufno(bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 char buffer[2*MAXPATHL];
2653 char_u *q;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002654 nbbuf_T *bp = nb_get_buf(nb_getbufno(bufp));
2655 int bnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656
2657 if (!haveConnection)
2658 return;
2659
Bram Moolenaar009b2592004-10-24 19:18:58 +00002660 q = nb_quote(bufp->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 if (q == NULL)
2662 return;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002663 if (bp != NULL)
2664 bnum = bufno;
2665 else
2666 bnum = 0;
2667
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002668 vim_snprintf(buffer, sizeof(buffer), "%d:fileOpened=%d \"%s\" %s %s\n",
Bram Moolenaar009b2592004-10-24 19:18:58 +00002669 bnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 0,
2671 (char *)q,
2672 "T", /* open in NetBeans */
2673 "F"); /* modified */
2674
2675 vim_free(q);
2676 nbdebug(("EVT: %s", buffer));
2677
2678 nb_send(buffer, "netbeans_file_opened");
Bram Moolenaar009b2592004-10-24 19:18:58 +00002679 if (p_acd && vim_chdirfile(bufp->b_ffname) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 shorten_fnames(TRUE);
2681}
2682
2683/*
2684 * Tell netbeans a file was closed.
2685 */
2686 void
2687netbeans_file_closed(buf_T *bufp)
2688{
2689 int bufno = nb_getbufno(bufp);
2690 nbbuf_T *nbbuf = nb_get_buf(bufno);
2691 char buffer[2*MAXPATHL];
2692
2693 if (!haveConnection || bufno < 0)
2694 return;
2695
2696 if (!netbeansCloseFile)
2697 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002698 nbdebug(("Ignoring file_closed for %s. File was closed from IDE\n",
2699 bufp->b_ffname));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 return;
2701 }
2702
Bram Moolenaar009b2592004-10-24 19:18:58 +00002703 nbdebug(("netbeans_file_closed:\n"));
2704 nbdebug((" Closing bufno: %d", bufno));
2705 if (curbuf != NULL && curbuf != bufp)
2706 {
2707 nbdebug((" Curbuf bufno: %d\n", nb_getbufno(curbuf)));
2708 }
2709 else if (curbuf == bufp)
2710 {
2711 nbdebug((" curbuf == bufp\n"));
2712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713
2714 if (bufno <= 0)
2715 return;
2716
2717 sprintf(buffer, "%d:killed=%d\n", bufno, cmdno);
2718
2719 nbdebug(("EVT: %s", buffer));
2720
2721 nb_send(buffer, "netbeans_file_closed");
2722
2723 if (nbbuf != NULL)
2724 nbbuf->bufp = NULL;
2725}
2726
2727/*
2728 * Get a pointer to the Netbeans buffer for Vim buffer "bufp".
2729 * Return NULL if there is no such buffer or changes are not to be reported.
2730 * Otherwise store the buffer number in "*bufnop".
2731 */
2732 static nbbuf_T *
2733nb_bufp2nbbuf_fire(buf_T *bufp, int *bufnop)
2734{
2735 int bufno;
2736 nbbuf_T *nbbuf;
2737
2738 if (!haveConnection || !netbeansFireChanges)
2739 return NULL; /* changes are not reported at all */
2740
2741 bufno = nb_getbufno(bufp);
2742 if (bufno <= 0)
2743 return NULL; /* file is not known to NetBeans */
2744
2745 nbbuf = nb_get_buf(bufno);
2746 if (nbbuf != NULL && !nbbuf->fireChanges)
2747 return NULL; /* changes in this buffer are not reported */
2748
2749 *bufnop = bufno;
2750 return nbbuf;
2751}
2752
2753/*
2754 * Tell netbeans the user inserted some text.
2755 */
2756 void
2757netbeans_inserted(
2758 buf_T *bufp,
2759 linenr_T linenr,
2760 colnr_T col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 char_u *txt,
2762 int newlen)
2763{
2764 char_u *buf;
2765 int bufno;
2766 nbbuf_T *nbbuf;
2767 pos_T pos;
2768 long off;
2769 char_u *p;
2770 char_u *newtxt;
2771
2772 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
2773 if (nbbuf == NULL)
2774 return;
2775
Bram Moolenaar009b2592004-10-24 19:18:58 +00002776 /* Don't mark as modified for initial read */
2777 if (nbbuf->insertDone)
2778 nbbuf->modified = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779
2780 pos.lnum = linenr;
2781 pos.col = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 off = pos2off(bufp, &pos);
2783
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 /* send the "insert" EVT */
2785 newtxt = alloc(newlen + 1);
Bram Moolenaarb6356332005-07-18 21:40:44 +00002786 vim_strncpy(newtxt, txt, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 p = nb_quote(newtxt);
2788 if (p != NULL)
2789 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002790 buf = alloc(128 + 2*newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 sprintf((char *)buf, "%d:insert=%d %ld \"%s\"\n", bufno, cmdno, off, p);
2792 nbdebug(("EVT: %s", buf));
2793 nb_send((char *)buf, "netbeans_inserted");
Bram Moolenaar009b2592004-10-24 19:18:58 +00002794 vim_free(p);
2795 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 vim_free(newtxt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798}
2799
2800/*
2801 * Tell netbeans some bytes have been removed.
2802 */
2803 void
2804netbeans_removed(
2805 buf_T *bufp,
2806 linenr_T linenr,
2807 colnr_T col,
2808 long len)
2809{
2810 char_u buf[128];
2811 int bufno;
2812 nbbuf_T *nbbuf;
2813 pos_T pos;
2814 long off;
2815
2816 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
2817 if (nbbuf == NULL)
2818 return;
2819
2820 if (len < 0)
2821 {
2822 nbdebug(("Negative len %ld in netbeans_removed()!", len));
2823 return;
2824 }
2825
2826 nbbuf->modified = 1;
2827
2828 pos.lnum = linenr;
2829 pos.col = col;
2830
2831 off = pos2off(bufp, &pos);
2832
2833 sprintf((char *)buf, "%d:remove=%d %ld %ld\n", bufno, cmdno, off, len);
2834 nbdebug(("EVT: %s", buf));
2835 nb_send((char *)buf, "netbeans_removed");
2836}
2837
2838/*
2839 * Send netbeans an unmodufied command.
2840 */
2841/*ARGSUSED*/
2842 void
2843netbeans_unmodified(buf_T *bufp)
2844{
2845#if 0
2846 char_u buf[128];
2847 int bufno;
2848 nbbuf_T *nbbuf;
2849
2850 /* This has been disabled, because NetBeans considers a buffer modified
2851 * even when all changes have been undone. */
2852 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
2853 if (nbbuf == NULL)
2854 return;
2855
2856 nbbuf->modified = 0;
2857
2858 sprintf((char *)buf, "%d:unmodified=%d\n", bufno, cmdno);
2859 nbdebug(("EVT: %s", buf));
2860 nb_send((char *)buf, "netbeans_unmodified");
2861#endif
2862}
2863
2864/*
2865 * Send a button release event back to netbeans. Its up to netbeans
2866 * to decide what to do (if anything) with this event.
2867 */
2868 void
2869netbeans_button_release(int button)
2870{
2871 char buf[128];
2872 int bufno;
2873
2874 bufno = nb_getbufno(curbuf);
2875
2876 if (bufno >= 0 && curwin != NULL && curwin->w_buffer == curbuf)
2877 {
Bram Moolenaarc92ad2e2005-01-19 22:08:28 +00002878 int col = mouse_col - W_WINCOL(curwin) - (curwin->w_p_nu ? 9 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 long off = pos2off(curbuf, &curwin->w_cursor);
2880
2881 /* sync the cursor position */
2882 sprintf(buf, "%d:newDotAndMark=%d %ld %ld\n", bufno, cmdno, off, off);
2883 nbdebug(("EVT: %s", buf));
2884 nb_send(buf, "netbeans_button_release[newDotAndMark]");
2885
2886 sprintf(buf, "%d:buttonRelease=%d %d %ld %d\n", bufno, cmdno,
2887 button, (long)curwin->w_cursor.lnum, col);
2888 nbdebug(("EVT: %s", buf));
2889 nb_send(buf, "netbeans_button_release");
2890 }
2891}
2892
2893
2894/*
2895 * Send a keypress event back to netbeans. This usualy simulates some
Bram Moolenaar009b2592004-10-24 19:18:58 +00002896 * kind of function key press. This function operates on a key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 */
2898 void
2899netbeans_keycommand(int key)
2900{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 char keyName[60];
Bram Moolenaar009b2592004-10-24 19:18:58 +00002902
2903 netbeans_keyname(key, keyName);
2904 netbeans_keystring(key, keyName);
2905}
2906
2907
2908/*
2909 * Send a keypress event back to netbeans. This usualy simulates some
2910 * kind of function key press. This function operates on a key string.
2911 */
2912 static void
2913netbeans_keystring(int key, char *keyName)
2914{
2915 char buf[2*MAXPATHL];
2916 int bufno = nb_getbufno(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 long off;
2918 char_u *q;
2919
2920 if (!haveConnection)
2921 return;
2922
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923
2924 if (bufno == -1)
2925 {
2926 nbdebug(("got keycommand for non-NetBeans buffer, opening...\n"));
2927 q = curbuf->b_ffname == NULL ? (char_u *)""
2928 : nb_quote(curbuf->b_ffname);
2929 if (q == NULL)
2930 return;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002931 vim_snprintf(buf, sizeof(buf), "0:fileOpened=%d \"%s\" %s %s\n", 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 q,
2933 "T", /* open in NetBeans */
2934 "F"); /* modified */
2935 if (curbuf->b_ffname != NULL)
2936 vim_free(q);
2937 nbdebug(("EVT: %s", buf));
2938 nb_send(buf, "netbeans_keycommand");
2939
Bram Moolenaar5b625c52005-01-31 18:55:55 +00002940 if (key > 0)
2941 postpone_keycommand(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 return;
2943 }
2944
2945 /* sync the cursor position */
2946 off = pos2off(curbuf, &curwin->w_cursor);
2947 sprintf(buf, "%d:newDotAndMark=%d %ld %ld\n", bufno, cmdno, off, off);
2948 nbdebug(("EVT: %s", buf));
2949 nb_send(buf, "netbeans_keycommand");
2950
2951 /* To work on Win32 you must apply patch to ExtEditor module
2952 * from ExtEdCaret.java.diff - make EVT_newDotAndMark handler
2953 * more synchronous
2954 */
2955
2956 /* now send keyCommand event */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002957 vim_snprintf(buf, sizeof(buf), "%d:keyCommand=%d \"%s\"\n",
2958 bufno, cmdno, keyName);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 nbdebug(("EVT: %s", buf));
2960 nb_send(buf, "netbeans_keycommand");
2961
2962 /* New: do both at once and include the lnum/col. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002963 vim_snprintf(buf, sizeof(buf), "%d:keyAtPos=%d \"%s\" %ld %ld/%ld\n",
2964 bufno, cmdno, keyName,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 off, (long)curwin->w_cursor.lnum, (long)curwin->w_cursor.col);
2966 nbdebug(("EVT: %s", buf));
2967 nb_send(buf, "netbeans_keycommand");
2968}
2969
2970
2971/*
2972 * Send a save event to netbeans.
2973 */
2974 void
2975netbeans_save_buffer(buf_T *bufp)
2976{
2977 char_u buf[64];
2978 int bufno;
2979 nbbuf_T *nbbuf;
2980
2981 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
2982 if (nbbuf == NULL)
2983 return;
2984
2985 nbbuf->modified = 0;
2986
2987 sprintf((char *)buf, "%d:save=%d\n", bufno, cmdno);
2988 nbdebug(("EVT: %s", buf));
2989 nb_send((char *)buf, "netbeans_save_buffer");
2990}
2991
2992
2993/*
2994 * Send remove command to netbeans (this command has been turned off).
2995 */
2996 void
2997netbeans_deleted_all_lines(buf_T *bufp)
2998{
2999 char_u buf[64];
3000 int bufno;
3001 nbbuf_T *nbbuf;
3002
3003 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3004 if (nbbuf == NULL)
3005 return;
3006
Bram Moolenaar009b2592004-10-24 19:18:58 +00003007 /* Don't mark as modified for initial read */
3008 if (nbbuf->insertDone)
3009 nbbuf->modified = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010
3011 sprintf((char *)buf, "%d:remove=%d 0 -1\n", bufno, cmdno);
3012 nbdebug(("EVT(suppressed): %s", buf));
3013/* nb_send(buf, "netbeans_deleted_all_lines"); */
3014}
3015
3016
3017/*
3018 * See if the lines are guarded. The top and bot parameters are from
3019 * u_savecommon(), these are the line above the change and the line below the
3020 * change.
3021 */
3022 int
3023netbeans_is_guarded(linenr_T top, linenr_T bot)
3024{
3025 signlist_T *p;
3026 int lnum;
3027
3028 for (p = curbuf->b_signlist; p != NULL; p = p->next)
3029 if (p->id >= GUARDEDOFFSET)
3030 for (lnum = top + 1; lnum < bot; lnum++)
3031 if (lnum == p->lnum)
3032 return TRUE;
3033
3034 return FALSE;
3035}
3036
3037#if defined(FEAT_GUI_MOTIF) || defined(PROTO)
3038/*
3039 * We have multiple signs to draw at the same location. Draw the
3040 * multi-sign indicator instead. This is the Motif version.
3041 */
3042 void
3043netbeans_draw_multisign_indicator(int row)
3044{
3045 int i;
3046 int y;
3047 int x;
3048
3049 x = 0;
3050 y = row * gui.char_height + 2;
3051
3052 for (i = 0; i < gui.char_height - 3; i++)
3053 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y++);
3054
3055 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+0, y);
3056 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3057 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+4, y++);
3058 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+1, y);
3059 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3060 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+3, y++);
3061 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3062}
3063#endif /* FEAT_GUI_MOTIF */
3064
3065#ifdef FEAT_GUI_GTK
3066/*
3067 * We have multiple signs to draw at the same location. Draw the
3068 * multi-sign indicator instead. This is the GTK/Gnome version.
3069 */
3070 void
3071netbeans_draw_multisign_indicator(int row)
3072{
3073 int i;
3074 int y;
3075 int x;
3076 GdkDrawable *drawable = gui.drawarea->window;
3077
3078 x = 0;
3079 y = row * gui.char_height + 2;
3080
3081 for (i = 0; i < gui.char_height - 3; i++)
3082 gdk_draw_point(drawable, gui.text_gc, x+2, y++);
3083
3084 gdk_draw_point(drawable, gui.text_gc, x+0, y);
3085 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3086 gdk_draw_point(drawable, gui.text_gc, x+4, y++);
3087 gdk_draw_point(drawable, gui.text_gc, x+1, y);
3088 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3089 gdk_draw_point(drawable, gui.text_gc, x+3, y++);
3090 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3091}
3092#endif /* FEAT_GUI_GTK */
3093
3094/*
3095 * If the mouse is clicked in the gutter of a line with multiple
3096 * annotations, cycle through the set of signs.
3097 */
3098 void
3099netbeans_gutter_click(linenr_T lnum)
3100{
3101 signlist_T *p;
3102
3103 for (p = curbuf->b_signlist; p != NULL; p = p->next)
3104 {
3105 if (p->lnum == lnum && p->next && p->next->lnum == lnum)
3106 {
3107 signlist_T *tail;
3108
3109 /* remove "p" from list, reinsert it at the tail of the sublist */
3110 if (p->prev)
3111 p->prev->next = p->next;
3112 else
3113 curbuf->b_signlist = p->next;
3114 p->next->prev = p->prev;
3115 /* now find end of sublist and insert p */
3116 for (tail = p->next;
3117 tail->next && tail->next->lnum == lnum
3118 && tail->next->id < GUARDEDOFFSET;
3119 tail = tail->next)
3120 ;
3121 /* tail now points to last entry with same lnum (except
3122 * that "guarded" annotations are always last) */
3123 p->next = tail->next;
3124 if (tail->next)
3125 tail->next->prev = p;
3126 p->prev = tail;
3127 tail->next = p;
3128 update_debug_sign(curbuf, lnum);
3129 break;
3130 }
3131 }
3132}
3133
3134
3135/*
3136 * Add a sign of the reqested type at the requested location.
3137 *
3138 * Reverse engineering:
3139 * Apparently an annotation is defined the first time it is used in a buffer.
3140 * When the same annotation is used in two buffers, the second time we do not
3141 * need to define a new sign name but reuse the existing one. But since the
3142 * ID number used in the second buffer starts counting at one again, a mapping
3143 * is made from the ID specifically for the buffer to the global sign name
3144 * (which is a number).
3145 *
3146 * globalsignmap[] stores the signs that have been defined globally.
3147 * buf->signmapused[] maps buffer-local annotation IDs to an index in
3148 * globalsignmap[].
3149 */
3150/*ARGSUSED*/
3151 static void
3152addsigntype(
3153 nbbuf_T *buf,
3154 int typeNum,
3155 char_u *typeName,
3156 char_u *tooltip,
3157 char_u *glyphFile,
3158 int use_fg,
3159 int fg,
3160 int use_bg,
3161 int bg)
3162{
3163 char fgbuf[32];
3164 char bgbuf[32];
3165 int i, j;
3166
3167 for (i = 0; i < globalsignmapused; i++)
3168 if (STRCMP(typeName, globalsignmap[i]) == 0)
3169 break;
3170
3171 if (i == globalsignmapused) /* not found; add it to global map */
3172 {
3173 nbdebug(("DEFINEANNOTYPE(%d,%s,%s,%s,%d,%d)\n",
3174 typeNum, typeName, tooltip, glyphFile, fg, bg));
3175 if (use_fg || use_bg)
3176 {
3177 sprintf(fgbuf, "guifg=#%06x", fg & 0xFFFFFF);
3178 sprintf(bgbuf, "guibg=#%06x", bg & 0xFFFFFF);
3179
3180 coloncmd(":highlight NB_%s %s %s", typeName, (use_fg) ? fgbuf : "",
3181 (use_bg) ? bgbuf : "");
3182 if (*glyphFile == NUL)
3183 /* no glyph, line highlighting only */
3184 coloncmd(":sign define %d linehl=NB_%s", i + 1, typeName);
3185 else if (vim_strsize(glyphFile) <= 2)
3186 /* one- or two-character glyph name, use as text glyph with
3187 * texthl */
3188 coloncmd(":sign define %d text=%s texthl=NB_%s", i + 1,
3189 glyphFile, typeName);
3190 else
3191 /* glyph, line highlighting */
3192 coloncmd(":sign define %d icon=%s linehl=NB_%s", i + 1,
3193 glyphFile, typeName);
3194 }
3195 else
3196 /* glyph, no line highlighting */
3197 coloncmd(":sign define %d icon=%s", i + 1, glyphFile);
3198
3199 if (STRCMP(typeName,"CurrentPC") == 0)
3200 curPCtype = typeNum;
3201
3202 if (globalsignmapused == globalsignmaplen)
3203 {
3204 if (globalsignmaplen == 0) /* first allocation */
3205 {
3206 globalsignmaplen = 20;
3207 globalsignmap = (char **)alloc_clear(globalsignmaplen*sizeof(char *));
3208 }
3209 else /* grow it */
3210 {
3211 int incr;
3212 int oldlen = globalsignmaplen;
3213
3214 globalsignmaplen *= 2;
3215 incr = globalsignmaplen - oldlen;
3216 globalsignmap = (char **)vim_realloc(globalsignmap,
3217 globalsignmaplen * sizeof(char *));
3218 memset(globalsignmap + oldlen, 0, incr * sizeof(char *));
3219 }
3220 }
3221
3222 globalsignmap[i] = (char *)typeName;
3223 globalsignmapused = i + 1;
3224 }
3225
3226 /* check local map; should *not* be found! */
3227 for (j = 0; j < buf->signmapused; j++)
3228 if (buf->signmap[j] == i + 1)
3229 return;
3230
3231 /* add to local map */
3232 if (buf->signmapused == buf->signmaplen)
3233 {
3234 if (buf->signmaplen == 0) /* first allocation */
3235 {
3236 buf->signmaplen = 5;
3237 buf->signmap = (int *)alloc_clear(buf->signmaplen * sizeof(int *));
3238 }
3239 else /* grow it */
3240 {
3241 int incr;
3242 int oldlen = buf->signmaplen;
3243 buf->signmaplen *= 2;
3244 incr = buf->signmaplen - oldlen;
3245 buf->signmap = (int *)vim_realloc(buf->signmap,
3246 buf->signmaplen*sizeof(int *));
3247 memset(buf->signmap + oldlen, 0, incr * sizeof(int *));
3248 }
3249 }
3250
3251 buf->signmap[buf->signmapused++] = i + 1;
3252
3253}
3254
3255
3256/*
3257 * See if we have the requested sign type in the buffer.
3258 */
3259 static int
3260mapsigntype(nbbuf_T *buf, int localsigntype)
3261{
3262 if (--localsigntype >= 0 && localsigntype < buf->signmapused)
3263 return buf->signmap[localsigntype];
3264
3265 return 0;
3266}
3267
3268
3269/*
3270 * Compute length of buffer, don't print anything.
3271 */
3272 static long
3273get_buf_size(buf_T *bufp)
3274{
3275 linenr_T lnum;
3276 long char_count = 0;
3277 int eol_size;
3278 long last_check = 100000L;
3279
3280 if (bufp->b_ml.ml_flags & ML_EMPTY)
3281 return 0;
3282 else
3283 {
3284 if (get_fileformat(bufp) == EOL_DOS)
3285 eol_size = 2;
3286 else
3287 eol_size = 1;
3288 for (lnum = 1; lnum <= bufp->b_ml.ml_line_count; ++lnum)
3289 {
3290 char_count += STRLEN(ml_get(lnum)) + eol_size;
3291 /* Check for a CTRL-C every 100000 characters */
3292 if (char_count > last_check)
3293 {
3294 ui_breakcheck();
3295 if (got_int)
3296 return char_count;
3297 last_check = char_count + 100000L;
3298 }
3299 }
3300 /* Correction for when last line doesn't have an EOL. */
3301 if (!bufp->b_p_eol && bufp->b_p_bin)
3302 char_count -= eol_size;
3303 }
3304
3305 return char_count;
3306}
3307
3308/*
3309 * Convert character offset to lnum,col
3310 */
3311 static pos_T *
3312off2pos(buf_T *buf, long offset)
3313{
3314 linenr_T lnum;
3315 static pos_T pos;
3316
3317 pos.lnum = 0;
3318 pos.col = 0;
3319#ifdef FEAT_VIRTUALEDIT
3320 pos.coladd = 0;
3321#endif
3322
3323 if (!(buf->b_ml.ml_flags & ML_EMPTY))
3324 {
3325 if ((lnum = ml_find_line_or_offset(buf, (linenr_T)0, &offset)) < 0)
3326 return NULL;
3327 pos.lnum = lnum;
3328 pos.col = offset;
3329 }
3330
3331 return &pos;
3332}
3333
3334/*
3335 * Convert an argument in the form "1234" to an offset and compute the
3336 * lnum/col from it. Convert an argument in the form "123/12" directly to a
3337 * lnum/col.
3338 * "argp" is advanced to after the argument.
3339 * Return a pointer to the position, NULL if something is wrong.
3340 */
3341 static pos_T *
3342get_off_or_lnum(buf_T *buf, char_u **argp)
3343{
3344 static pos_T mypos;
3345 long off;
3346
3347 off = strtol((char *)*argp, (char **)argp, 10);
3348 if (**argp == '/')
3349 {
3350 mypos.lnum = (linenr_T)off;
3351 ++*argp;
3352 mypos.col = strtol((char *)*argp, (char **)argp, 10);
3353#ifdef FEAT_VIRTUALEDIT
3354 mypos.coladd = 0;
3355#endif
3356 return &mypos;
3357 }
3358 return off2pos(buf, off);
3359}
3360
3361
3362/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003363 * Convert (lnum,col) to byte offset in the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 */
3365 static long
3366pos2off(buf_T *buf, pos_T *pos)
3367{
3368 long offset = 0;
3369
3370 if (!(buf->b_ml.ml_flags & ML_EMPTY))
3371 {
3372 if ((offset = ml_find_line_or_offset(buf, pos->lnum, 0)) < 0)
3373 return 0;
3374 offset += pos->col;
3375 }
3376
3377 return offset;
3378}
3379
3380
Bram Moolenaar009b2592004-10-24 19:18:58 +00003381/*
3382 * This message is printed after NetBeans opens a new file. Its
3383 * similar to the message readfile() uses, but since NetBeans
3384 * doesn't normally call readfile, we do our own.
3385 */
3386 static void
3387print_read_msg(buf)
3388 nbbuf_T *buf;
3389{
3390 int lnum = buf->bufp->b_ml.ml_line_count;
3391 long nchars = buf->bufp->b_orig_size;
3392 char_u c;
3393
3394 msg_add_fname(buf->bufp, buf->bufp->b_ffname);
3395 c = FALSE;
3396
3397 if (buf->bufp->b_p_ro)
3398 {
3399 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]"));
3400 c = TRUE;
3401 }
3402 if (!buf->bufp->b_start_eol)
3403 {
3404 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]") : _("[Incomplete last line]"));
3405 c = TRUE;
3406 }
3407 msg_add_lines(c, (long)lnum, nchars);
3408
3409 /* Now display it */
3410 vim_free(keep_msg);
3411 keep_msg = NULL;
3412 msg_scrolled_ign = TRUE;
3413 msg_trunc_attr(IObuff, FALSE, 0);
3414 msg_scrolled_ign = FALSE;
3415}
3416
3417
3418/*
3419 * Print a message after NetBeans writes the file. This message should be identical
3420 * to the standard message a non-netbeans user would see when writing a file.
3421 */
3422 static void
3423print_save_msg(buf, nchars)
3424 nbbuf_T *buf;
3425 long nchars;
3426{
3427 char_u c;
3428 char_u *p;
3429
3430 if (nchars >= 0)
3431 {
3432 msg_add_fname(buf->bufp, buf->bufp->b_ffname); /* fname in IObuff with quotes */
3433 c = FALSE;
3434
3435 msg_add_lines(c, buf->bufp->b_ml.ml_line_count,
3436 (long)buf->bufp->b_orig_size);
3437
3438 vim_free(keep_msg);
3439 keep_msg = NULL;
3440 msg_scrolled_ign = TRUE;
3441 p = msg_trunc_attr(IObuff, FALSE, 0);
3442 if ((msg_scrolled && !need_wait_return) || !buf->initDone)
3443 {
3444 /* Need to repeat the message after redrawing when:
3445 * - When reading from stdin (the screen will be cleared next).
3446 * - When restart_edit is set (otherwise there will be a delay
3447 * before redrawing).
3448 * - When the screen was scrolled but there is no wait-return
3449 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003450 set_keep_msg(p, 0);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003451 }
3452 msg_scrolled_ign = FALSE;
3453 /* add_to_input_buf((char_u *)"\f", 1); */
3454 }
3455 else
3456 {
3457 char_u ebuf[BUFSIZ];
3458
3459 STRCPY(ebuf, (char_u *)_("E505: "));
3460 STRCAT(ebuf, IObuff);
3461 STRCAT(ebuf, (char_u *)_("is read-only (add ! to override)"));
3462 STRCPY(IObuff, ebuf);
3463 emsg(IObuff);
3464 }
3465}
3466
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467#endif /* defined(FEAT_NETBEANS_INTG) */