blob: 78107bf65983a254b7160dd1070fab9d2378fa61 [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
29# include <io.h>
30/* 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, ...));
78#ifdef FEAT_GUI_MOTIF
79static void messageFromNetbeans __ARGS((XtPointer, int *, XtInputId *));
80#endif
81#ifdef FEAT_GUI_GTK
82static void messageFromNetbeans __ARGS((gpointer, gint, GdkInputCondition));
83#endif
84static void nb_parse_cmd __ARGS((char_u *));
85static int nb_do_cmd __ARGS((int, char_u *, int, int, char_u *));
86static void nb_send __ARGS((char *buf, char *fun));
Bram Moolenaar071d4272004-06-13 20:20:40 +000087
88static int sd = -1; /* socket fd for Netbeans connection */
89#ifdef FEAT_GUI_MOTIF
90static XtInputId inputHandler; /* Cookie for input */
91#endif
92#ifdef FEAT_GUI_GTK
93static gint inputHandler; /* Cookie for input */
94#endif
95#ifdef FEAT_GUI_W32
96static int inputHandler = -1; /* simply ret.value of WSAAsyncSelect() */
97extern HWND s_hwnd; /* Gvim's Window handle */
98#endif
99static int cmdno; /* current command number for reply */
100static int haveConnection = FALSE; /* socket is connected and
101 initialization is done */
102static int oldFire = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103
Bram Moolenaar009b2592004-10-24 19:18:58 +0000104#ifdef FEAT_GUI_MOTIF
105static void netbeans_Xt_connect __ARGS((void *context));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000106#endif
107#ifdef FEAT_GUI_GTK
Bram Moolenaar009b2592004-10-24 19:18:58 +0000108static void netbeans_gtk_connect __ARGS((void));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000109#endif
110#ifdef FEAT_GUI_W32
Bram Moolenaar009b2592004-10-24 19:18:58 +0000111static void netbeans_w32_connect __ARGS((void));
Bram Moolenaar009b2592004-10-24 19:18:58 +0000112#endif
113
114static int dosetvisible = FALSE;
115
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116/*
117 * Include the debugging code if wanted.
118 */
119#ifdef NBDEBUG
120# include "nbdebug.c"
121#endif
122
123/* Connect back to Netbeans process */
Bram Moolenaar009b2592004-10-24 19:18:58 +0000124#ifdef FEAT_GUI_MOTIF
125 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126netbeans_Xt_connect(void *context)
127{
128 netbeans_connect();
129 if (sd > 0)
130 {
131 /* tell notifier we are interested in being called
132 * when there is input on the editor connection socket
133 */
134 inputHandler = XtAppAddInput((XtAppContext)context, sd,
135 (XtPointer)(XtInputReadMask + XtInputExceptMask),
136 messageFromNetbeans, NULL);
137 }
138}
139
140 static void
141netbeans_disconnect(void)
142{
143 if (inputHandler != (XtInputId)NULL)
144 {
145 XtRemoveInput(inputHandler);
146 inputHandler = (XtInputId)NULL;
147 }
148 sd = -1;
149 haveConnection = FALSE;
Bram Moolenaard62bec82005-03-07 22:56:57 +0000150# ifdef FEAT_BEVAL
151 bevalServers &= ~BEVAL_NETBEANS;
152# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153}
154#endif /* FEAT_MOTIF_GUI */
155
Bram Moolenaar009b2592004-10-24 19:18:58 +0000156#ifdef FEAT_GUI_GTK
157 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158netbeans_gtk_connect(void)
159{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160 netbeans_connect();
161 if (sd > 0)
162 {
163 /*
164 * Tell gdk we are interested in being called when there
165 * is input on the editor connection socket
166 */
167 inputHandler = gdk_input_add(sd, (GdkInputCondition)
168 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
169 messageFromNetbeans, NULL);
170 }
171}
172
173 static void
174netbeans_disconnect(void)
175{
176 if (inputHandler != 0)
177 {
178 gdk_input_remove(inputHandler);
179 inputHandler = 0;
180 }
181 sd = -1;
182 haveConnection = FALSE;
Bram Moolenaard62bec82005-03-07 22:56:57 +0000183# ifdef FEAT_BEVAL
184 bevalServers &= ~BEVAL_NETBEANS;
185# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186}
187#endif /* FEAT_GUI_GTK */
188
Bram Moolenaar5b625c52005-01-31 18:55:55 +0000189#if defined(FEAT_GUI_W32) || defined(PROTO)
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000190 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191netbeans_w32_connect(void)
192{
193 netbeans_connect();
194 if (sd > 0)
195 {
196 /*
197 * Tell Windows we are interested in receiving message when there
198 * is input on the editor connection socket
199 */
200 inputHandler = WSAAsyncSelect(sd, s_hwnd, WM_NETBEANS, FD_READ);
201 }
202}
203
204 static void
205netbeans_disconnect(void)
206{
207 if (inputHandler == 0)
208 {
209 WSAAsyncSelect(sd, s_hwnd, 0, 0);
210 inputHandler = -1;
211 }
212 sd = -1;
213 haveConnection = FALSE;
Bram Moolenaard62bec82005-03-07 22:56:57 +0000214# ifdef FEAT_BEVAL
215 bevalServers &= ~BEVAL_NETBEANS;
216# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217}
218#endif /* FEAT_GUI_W32 */
219
220#define NB_DEF_HOST "localhost"
221#define NB_DEF_ADDR "3219"
222#define NB_DEF_PASS "changeme"
223
224 static void
225netbeans_connect(void)
226{
227#ifdef INET_SOCKETS
228 struct sockaddr_in server;
229 struct hostent * host;
230# ifdef FEAT_GUI_W32
231 u_short port;
232# else
233 int port;
234#endif
235#else
236 struct sockaddr_un server;
237#endif
238 char buf[32];
239 char *hostname = NULL;
240 char *address = NULL;
241 char *password = NULL;
242 char *fname;
243 char *arg = NULL;
244
245 if (netbeansArg[3] == '=')
246 {
247 /* "-nb=fname": Read info from specified file. */
248 if (getConnInfo(netbeansArg + 4, &hostname, &address, &password)
249 == FAIL)
250 return;
251 }
252 else
253 {
254 if (netbeansArg[3] == ':')
255 /* "-nb:<host>:<addr>:<password>": get info from argument */
256 arg = netbeansArg + 4;
257 if (arg == NULL && (fname = getenv("__NETBEANS_CONINFO")) != NULL)
258 {
259 /* "-nb": get info from file specified in environment */
260 if (getConnInfo(fname, &hostname, &address, &password) == FAIL)
261 return;
262 }
263 else
264 {
265 if (arg != NULL)
266 {
267 /* "-nb:<host>:<addr>:<password>": get info from argument */
268 hostname = arg;
269 address = strchr(hostname, ':');
270 if (address != NULL)
271 {
272 *address++ = '\0';
273 password = strchr(address, ':');
274 if (password != NULL)
275 *password++ = '\0';
276 }
277 }
278
279 /* Get the missing values from the environment. */
280 if (hostname == NULL || *hostname == '\0')
281 hostname = getenv("__NETBEANS_HOST");
282 if (address == NULL)
283 address = getenv("__NETBEANS_SOCKET");
284 if (password == NULL)
285 password = getenv("__NETBEANS_VIM_PASSWORD");
286
287 /* Move values to allocated memory. */
288 if (hostname != NULL)
289 hostname = (char *)vim_strsave((char_u *)hostname);
290 if (address != NULL)
291 address = (char *)vim_strsave((char_u *)address);
292 if (password != NULL)
293 password = (char *)vim_strsave((char_u *)password);
294 }
295 }
296
297 /* Use the default when a value is missing. */
298 if (hostname == NULL || *hostname == '\0')
299 {
300 vim_free(hostname);
301 hostname = (char *)vim_strsave((char_u *)NB_DEF_HOST);
302 }
303 if (address == NULL || *address == '\0')
304 {
305 vim_free(address);
306 address = (char *)vim_strsave((char_u *)NB_DEF_ADDR);
307 }
308 if (password == NULL || *password == '\0')
309 {
310 vim_free(password);
311 password = (char *)vim_strsave((char_u *)NB_DEF_PASS);
312 }
313 if (hostname == NULL || address == NULL || password == NULL)
314 goto theend; /* out of memory */
315
316#ifdef INET_SOCKETS
317 port = atoi(address);
318
319 if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
320 {
321 PERROR("socket() in netbeans_connect()");
322 goto theend;
323 }
324
325 /* Get the server internet address and put into addr structure */
326 /* fill in the socket address structure and connect to server */
327 memset((char *)&server, '\0', sizeof(server));
328 server.sin_family = AF_INET;
329 server.sin_port = htons(port);
330 if ((host = gethostbyname(hostname)) == NULL)
331 {
332 if (mch_access(hostname, R_OK) >= 0)
333 {
334 /* DEBUG: input file */
335 sd = mch_open(hostname, O_RDONLY, 0);
336 goto theend;
337 }
338 PERROR("gethostbyname() in netbeans_connect()");
339 sd = -1;
340 goto theend;
341 }
342 memcpy((char *)&server.sin_addr, host->h_addr, host->h_length);
343#else
344 if ((sd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
345 {
346 PERROR("socket()");
347 goto theend;
348 }
349
350 server.sun_family = AF_UNIX;
351 strcpy(server.sun_path, address);
352#endif
353 /* Connect to server */
354 if (connect(sd, (struct sockaddr *)&server, sizeof(server)))
355 {
356 nbdebug(("netbeans_connect: Connect failed with errno %d\n", sock_errno));
357 if (sock_errno == ECONNREFUSED)
358 {
359 sock_close(sd);
360#ifdef INET_SOCKETS
361 if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
362 {
363 PERROR("socket()#2 in netbeans_connect()");
364 goto theend;
365 }
366#else
367 if ((sd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
368 {
369 PERROR("socket()#2 in netbeans_connect()");
370 goto theend;
371 }
372#endif
373 if (connect(sd, (struct sockaddr *)&server, sizeof(server)))
374 {
375 int retries = 36;
376 int success = FALSE;
377 while (retries--
378 && ((sock_errno == ECONNREFUSED) || (sock_errno == EINTR)))
379 {
380 nbdebug(("retrying...\n"));
381 sleep(5);
382 if (connect(sd, (struct sockaddr *)&server,
383 sizeof(server)) == 0)
384 {
385 success = TRUE;
386 break;
387 }
388 }
389 if (!success)
390 {
391 /* Get here when the server can't be found. */
392 PERROR(_("Cannot connect to Netbeans #2"));
393 getout(1);
394 }
395 }
396
397 }
398 else
399 {
400 PERROR(_("Cannot connect to Netbeans"));
401 getout(1);
402 }
403 }
404
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000405 vim_snprintf(buf, sizeof(buf), "AUTH %s\n", password);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 nb_send(buf, "netbeans_connect");
407
408 sprintf(buf, "0:version=0 \"%s\"\n", ExtEdProtocolVersion);
409 nb_send(buf, "externaleditor_version");
410
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411/* nb_init_graphics(); delay until needed */
412
413 haveConnection = TRUE;
414
415theend:
416 vim_free(hostname);
417 vim_free(address);
418 vim_free(password);
419 return;
420}
421
422/*
423 * Obtain the NetBeans hostname, port address and password from a file.
424 * Return the strings in allocated memory.
425 * Return FAIL if the file could not be read, OK otherwise (no matter what it
426 * contains).
427 */
428 static int
429getConnInfo(char *file, char **host, char **port, char **auth)
430{
431 FILE *fp;
432 char_u buf[BUFSIZ];
433 char_u *lp;
434 char_u *nl;
435#ifdef UNIX
436 struct stat st;
437
438 /*
439 * For Unix only accept the file when it's not accessible by others.
440 * The open will then fail if we don't own the file.
441 */
442 if (mch_stat(file, &st) == 0 && (st.st_mode & 0077) != 0)
443 {
444 EMSG2(_("E668: Wrong access mode for NetBeans connection info file: \"%s\""),
445 file);
446 return FAIL;
447 }
448#endif
449
450 fp = mch_fopen(file, "r");
451 if (fp == NULL)
452 {
453 PERROR("E660: Cannot open NetBeans connection info file");
454 return FAIL;
455 }
456
457 /* Read the file. There should be one of each parameter */
458 while ((lp = (char_u *)fgets((char *)buf, BUFSIZ, fp)) != NULL)
459 {
460 if ((nl = vim_strchr(lp, '\n')) != NULL)
461 *nl = 0; /* strip off the trailing newline */
462
463 if (STRNCMP(lp, "host=", 5) == 0)
464 {
465 vim_free(*host);
466 *host = (char *)vim_strsave(&buf[5]);
467 }
468 else if (STRNCMP(lp, "port=", 5) == 0)
469 {
470 vim_free(*port);
471 *port = (char *)vim_strsave(&buf[5]);
472 }
473 else if (STRNCMP(lp, "auth=", 5) == 0)
474 {
475 vim_free(*auth);
476 *auth = (char *)vim_strsave(&buf[5]);
477 }
478 }
479 fclose(fp);
480
481 return OK;
482}
483
484
485struct keyqueue
486{
487 int key;
488 struct keyqueue *next;
489 struct keyqueue *prev;
490};
491
492typedef struct keyqueue keyQ_T;
493
494static keyQ_T keyHead; /* dummy node, header for circular queue */
495
496
497/*
498 * Queue up key commands sent from netbeans.
499 */
500 static void
501postpone_keycommand(int key)
502{
503 keyQ_T *node;
504
505 node = (keyQ_T *)alloc(sizeof(keyQ_T));
506
507 if (keyHead.next == NULL) /* initialize circular queue */
508 {
509 keyHead.next = &keyHead;
510 keyHead.prev = &keyHead;
511 }
512
513 /* insert node at tail of queue */
514 node->next = &keyHead;
515 node->prev = keyHead.prev;
516 keyHead.prev->next = node;
517 keyHead.prev = node;
518
519 node->key = key;
520}
521
522/*
523 * Handle any queued-up NetBeans keycommands to be send.
524 */
525 static void
526handle_key_queue(void)
527{
528 while (keyHead.next && keyHead.next != &keyHead)
529 {
530 /* first, unlink the node */
531 keyQ_T *node = keyHead.next;
532 keyHead.next = node->next;
533 node->next->prev = node->prev;
534
535 /* now, send the keycommand */
536 netbeans_keycommand(node->key);
537
538 /* Finally, dispose of the node */
539 vim_free(node);
540 }
541}
542
543
544struct cmdqueue
545{
546 char_u *buffer;
547 struct cmdqueue *next;
548 struct cmdqueue *prev;
549};
550
551typedef struct cmdqueue queue_T;
552
553static queue_T head; /* dummy node, header for circular queue */
554
555
556/*
557 * Put the buffer on the work queue; possibly save it to a file as well.
558 */
559 static void
560save(char_u *buf, int len)
561{
562 queue_T *node;
563
564 node = (queue_T *)alloc(sizeof(queue_T));
565 if (node == NULL)
566 return; /* out of memory */
567 node->buffer = alloc(len + 1);
568 if (node->buffer == NULL)
569 {
570 vim_free(node);
571 return; /* out of memory */
572 }
573 mch_memmove(node->buffer, buf, (size_t)len);
574 node->buffer[len] = NUL;
575
576 if (head.next == NULL) /* initialize circular queue */
577 {
578 head.next = &head;
579 head.prev = &head;
580 }
581
582 /* insert node at tail of queue */
583 node->next = &head;
584 node->prev = head.prev;
585 head.prev->next = node;
586 head.prev = node;
587
588#ifdef NBDEBUG
589 {
590 static int outfd = -2;
591
592 /* possibly write buffer out to a file */
593 if (outfd == -3)
594 return;
595
596 if (outfd == -2)
597 {
598 char *file = getenv("__NETBEANS_SAVE");
599 if (file == NULL)
600 outfd = -3;
601 else
602 outfd = mch_open(file, O_WRONLY|O_CREAT|O_TRUNC, 0666);
603 }
604
605 if (outfd >= 0)
606 write(outfd, buf, len);
607 }
608#endif
609}
610
611
612/*
613 * While there's still a command in the work queue, parse and execute it.
614 */
615 static void
616nb_parse_messages(void)
617{
618 char_u *p;
619 queue_T *node;
620
621 while (head.next != &head)
622 {
623 node = head.next;
624
625 /* Locate the first line in the first buffer. */
626 p = vim_strchr(node->buffer, '\n');
627 if (p == NULL)
628 {
629 /* Command isn't complete. If there is no following buffer,
630 * return (wait for more). If there is another buffer following,
631 * prepend the text to that buffer and delete this one. */
632 if (node->next == &head)
633 return;
634 p = alloc(STRLEN(node->buffer) + STRLEN(node->next->buffer) + 1);
635 if (p == NULL)
636 return; /* out of memory */
637 STRCPY(p, node->buffer);
638 STRCAT(p, node->next->buffer);
639 vim_free(node->next->buffer);
640 node->next->buffer = p;
641
642 /* dispose of the node and buffer */
643 head.next = node->next;
644 node->next->prev = node->prev;
645 vim_free(node->buffer);
646 vim_free(node);
647 }
648 else
649 {
650 /* There is a complete command at the start of the buffer.
651 * Terminate it with a NUL. When no more text is following unlink
652 * the buffer. Do this before executing, because new buffers can
653 * be added while busy handling the command. */
654 *p++ = NUL;
655 if (*p == NUL)
656 {
657 head.next = node->next;
658 node->next->prev = node->prev;
659 }
660
661 /* now, parse and execute the commands */
662 nb_parse_cmd(node->buffer);
663
664 if (*p == NUL)
665 {
666 /* buffer finished, dispose of the node and buffer */
667 vim_free(node->buffer);
668 vim_free(node);
669 }
670 else
671 {
672 /* more follows, move to the start */
673 mch_memmove(node->buffer, p, STRLEN(p) + 1);
674 }
675 }
676 }
677}
678
679/* Buffer size for reading incoming messages. */
680#define MAXMSGSIZE 4096
681
682/*
683 * Read and process a command from netbeans.
684 */
685/*ARGSUSED*/
686#if defined(FEAT_GUI_W32) || defined(PROTO)
687/* Use this one when generating prototypes, the others are static. */
688 void
689messageFromNetbeansW32()
690#else
691# ifdef FEAT_GUI_MOTIF
692 static void
693messageFromNetbeans(XtPointer clientData, int *unused1, XtInputId *unused2)
694# endif
695# ifdef FEAT_GUI_GTK
696 static void
697messageFromNetbeans(gpointer clientData, gint unused1,
698 GdkInputCondition unused2)
699# endif
700#endif
701{
702 static char_u *buf = NULL;
703 int len;
704 int readlen = 0;
705 static int level = 0;
706
707 if (sd < 0)
708 {
709 nbdebug(("messageFromNetbeans() called without a socket\n"));
710 return;
711 }
712
713 ++level; /* recursion guard; this will be called from the X event loop */
714
715 /* Allocate a buffer to read into. */
716 if (buf == NULL)
717 {
718 buf = alloc(MAXMSGSIZE);
719 if (buf == NULL)
720 return; /* out of memory! */
721 }
722
723 /* Keep on reading for as long as there is something to read. */
724 for (;;)
725 {
726 len = sock_read(sd, buf, MAXMSGSIZE);
727 if (len <= 0)
728 break; /* error or nothing more to read */
729
730 /* Store the read message in the queue. */
731 save(buf, len);
732 readlen += len;
733 if (len < MAXMSGSIZE)
734 break; /* did read everything that's available */
735 }
736
737 if (readlen <= 0)
738 {
739 /* read error or didn't read anything */
740 netbeans_disconnect();
741 nbdebug(("messageFromNetbeans: Error in read() from socket\n"));
742 if (len < 0)
743 PERROR(_("read from Netbeans socket"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000744 return; /* don't try to parse it */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 }
746
747 /* Parse the messages, but avoid recursion. */
748 if (level == 1)
749 nb_parse_messages();
750
751 --level;
752}
753
754/*
755 * Handle one NUL terminated command.
756 *
757 * format of a command from netbeans:
758 *
759 * 6:setTitle!84 "a.c"
760 *
761 * bufno
762 * colon
763 * cmd
764 * !
765 * cmdno
766 * args
767 *
768 * for function calls, the ! is replaced by a /
769 */
770 static void
771nb_parse_cmd(char_u *cmd)
772{
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000773 char *verb;
774 char *q;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 int bufno;
776 int isfunc = -1;
777
778 if (STRCMP(cmd, "DISCONNECT") == 0)
779 {
780 /* We assume the server knows that we can safely exit! */
781 if (sd >= 0)
782 sock_close(sd);
783 /* Disconnect before exiting, Motif hangs in a Select error
784 * message otherwise. */
785 netbeans_disconnect();
786 getout(0);
787 /* NOTREACHED */
788 }
789
790 if (STRCMP(cmd, "DETACH") == 0)
791 {
792 /* The IDE is breaking the connection. */
793 if (sd >= 0)
794 sock_close(sd);
795 netbeans_disconnect();
796 return;
797 }
798
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000799 bufno = strtol((char *)cmd, &verb, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800
801 if (*verb != ':')
802 {
803 EMSG2("E627: missing colon: %s", cmd);
804 return;
805 }
806 ++verb; /* skip colon */
807
808 for (q = verb; *q; q++)
809 {
810 if (*q == '!')
811 {
812 *q++ = NUL;
813 isfunc = 0;
814 break;
815 }
816 else if (*q == '/')
817 {
818 *q++ = NUL;
819 isfunc = 1;
820 break;
821 }
822 }
823
824 if (isfunc < 0)
825 {
826 EMSG2("E628: missing ! or / in: %s", cmd);
827 return;
828 }
829
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000830 cmdno = strtol(q, &q, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000832 q = (char *)skipwhite((char_u *)q);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000834 if (nb_do_cmd(bufno, (char_u *)verb, isfunc, cmdno, (char_u *)q) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 {
Bram Moolenaar009b2592004-10-24 19:18:58 +0000836#ifdef NBDEBUG
837 /*
838 * This happens because the ExtEd can send a cammand or 2 after
839 * doing a stopDocumentListen command. It doesn't harm anything
840 * so I'm disabling it except for debugging.
841 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 nbdebug(("nb_parse_cmd: Command error for \"%s\"\n", cmd));
843 EMSG("E629: bad return from nb_do_cmd");
Bram Moolenaar009b2592004-10-24 19:18:58 +0000844#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 }
846}
847
848struct nbbuf_struct
849{
850 buf_T *bufp;
851 unsigned int fireChanges:1;
852 unsigned int initDone:1;
Bram Moolenaar009b2592004-10-24 19:18:58 +0000853 unsigned int insertDone:1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 unsigned int modified:1;
Bram Moolenaar009b2592004-10-24 19:18:58 +0000855 int nbbuf_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 char *displayname;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 int *signmap;
858 short_u signmaplen;
859 short_u signmapused;
860};
861
862typedef struct nbbuf_struct nbbuf_T;
863
864static nbbuf_T *buf_list = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000865static int buf_list_size = 0; /* size of buf_list */
866static int buf_list_used = 0; /* nr of entries in buf_list actually in use */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867
868static char **globalsignmap;
869static int globalsignmaplen;
870static int globalsignmapused;
871
872static int mapsigntype __ARGS((nbbuf_T *, int localsigntype));
873static void addsigntype __ARGS((nbbuf_T *, int localsigntype, char_u *typeName,
874 char_u *tooltip, char_u *glyphfile,
875 int usefg, int fg, int usebg, int bg));
Bram Moolenaar009b2592004-10-24 19:18:58 +0000876static void print_read_msg __ARGS((nbbuf_T *buf));
877static void print_save_msg __ARGS((nbbuf_T *buf, long nchars));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878
879static int curPCtype = -1;
880
881/*
882 * Get the Netbeans buffer number for the specified buffer.
883 */
884 static int
885nb_getbufno(buf_T *bufp)
886{
887 int i;
888
889 for (i = 0; i < buf_list_used; i++)
890 if (buf_list[i].bufp == bufp)
891 return i;
892 return -1;
893}
894
895/*
896 * Is this a NetBeans-owned buffer?
897 */
898 int
899isNetbeansBuffer(buf_T *bufp)
900{
Bram Moolenaar009b2592004-10-24 19:18:58 +0000901 return usingNetbeans && bufp->b_netbeans_file;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902}
903
904/*
905 * NetBeans and Vim have different undo models. In Vim, the file isn't
906 * changed if changes are undone via the undo command. In NetBeans, once
907 * a change has been made the file is marked as modified until saved. It
908 * doesn't matter if the change was undone.
909 *
910 * So this function is for the corner case where Vim thinks a buffer is
911 * unmodified but NetBeans thinks it IS modified.
912 */
913 int
914isNetbeansModified(buf_T *bufp)
915{
Bram Moolenaar009b2592004-10-24 19:18:58 +0000916 if (usingNetbeans && bufp->b_netbeans_file)
917 {
918 int bufno = nb_getbufno(bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919
Bram Moolenaar009b2592004-10-24 19:18:58 +0000920 if (bufno > 0)
921 return buf_list[bufno].modified;
922 else
923 return FALSE;
924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 else
926 return FALSE;
927}
928
929/*
930 * Given a Netbeans buffer number, return the netbeans buffer.
931 * Returns NULL for 0 or a negative number. A 0 bufno means a
932 * non-buffer related command has been sent.
933 */
934 static nbbuf_T *
935nb_get_buf(int bufno)
936{
937 /* find or create a buffer with the given number */
938 int incr;
939
940 if (bufno <= 0)
941 return NULL;
942
943 if (!buf_list)
944 {
945 /* initialize */
946 buf_list = (nbbuf_T *)alloc_clear(100 * sizeof(nbbuf_T));
947 buf_list_size = 100;
948 }
949 if (bufno >= buf_list_used) /* new */
950 {
951 if (bufno >= buf_list_size) /* grow list */
952 {
953 incr = bufno - buf_list_size + 90;
954 buf_list_size += incr;
955 buf_list = (nbbuf_T *)vim_realloc(
956 buf_list, buf_list_size * sizeof(nbbuf_T));
957 memset(buf_list + buf_list_size - incr, 0, incr * sizeof(nbbuf_T));
958 }
959
960 while (buf_list_used <= bufno)
961 {
962 /* Default is to fire text changes. */
963 buf_list[buf_list_used].fireChanges = 1;
964 ++buf_list_used;
965 }
966 }
967
968 return buf_list + bufno;
969}
970
971/*
972 * Return the number of buffers that are modified.
973 */
974 static int
975count_changed_buffers(void)
976{
977 buf_T *bufp;
978 int n;
979
980 n = 0;
981 for (bufp = firstbuf; bufp != NULL; bufp = bufp->b_next)
982 if (bufp->b_changed)
983 ++n;
984 return n;
985}
986
987/*
988 * End the netbeans session.
989 */
990 void
991netbeans_end(void)
992{
993 int i;
994 static char buf[128];
995
996 if (!haveConnection)
997 return;
998
999 for (i = 0; i < buf_list_used; i++)
1000 {
1001 if (!buf_list[i].bufp)
1002 continue;
1003 if (netbeansForcedQuit)
1004 {
1005 /* mark as unmodified so NetBeans won't put up dialog on "killed" */
1006 sprintf(buf, "%d:unmodified=%d\n", i, cmdno);
1007 nbdebug(("EVT: %s", buf));
1008 nb_send(buf, "netbeans_end");
1009 }
1010 sprintf(buf, "%d:killed=%d\n", i, cmdno);
1011 nbdebug(("EVT: %s", buf));
1012/* nb_send(buf, "netbeans_end"); avoid "write failed" messages */
1013 if (sd >= 0)
1014 sock_write(sd, buf, STRLEN(buf)); /* ignore errors */
1015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016}
1017
1018/*
1019 * Send a message to netbeans.
1020 */
1021 static void
1022nb_send(char *buf, char *fun)
1023{
1024 /* Avoid giving pages full of error messages when the other side has
1025 * exited, only mention the first error until the connection works again. */
1026 static int did_error = FALSE;
1027
1028 if (sd < 0)
1029 {
1030 if (!did_error)
1031 EMSG2("E630: %s(): write while not connected", fun);
1032 did_error = TRUE;
1033 }
1034 else if (sock_write(sd, buf, STRLEN(buf)) != (int)STRLEN(buf))
1035 {
1036 if (!did_error)
1037 EMSG2("E631: %s(): write failed", fun);
1038 did_error = TRUE;
1039 }
1040 else
1041 did_error = FALSE;
1042}
1043
1044/*
1045 * Some input received from netbeans requires a response. This function
1046 * handles a response with no information (except the command number).
1047 */
1048 static void
1049nb_reply_nil(int cmdno)
1050{
1051 char reply[32];
1052
1053 if (!haveConnection)
1054 return;
1055
Bram Moolenaar009b2592004-10-24 19:18:58 +00001056 nbdebug(("REP %d: <none>\n", cmdno));
1057
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 sprintf(reply, "%d\n", cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 nb_send(reply, "nb_reply_nil");
1060}
1061
1062
1063/*
1064 * Send a response with text.
1065 * "result" must have been quoted already (using nb_quote()).
1066 */
1067 static void
1068nb_reply_text(int cmdno, char_u *result)
1069{
1070 char_u *reply;
1071
1072 if (!haveConnection)
1073 return;
1074
Bram Moolenaar009b2592004-10-24 19:18:58 +00001075 nbdebug(("REP %d: %s\n", cmdno, (char *)result));
1076
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 reply = alloc(STRLEN(result) + 32);
1078 sprintf((char *)reply, "%d %s\n", cmdno, (char *)result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 nb_send((char *)reply, "nb_reply_text");
1080
1081 vim_free(reply);
1082}
1083
1084
1085/*
1086 * Send a response with a number result code.
1087 */
1088 static void
1089nb_reply_nr(int cmdno, long result)
1090{
1091 char reply[32];
1092
1093 if (!haveConnection)
1094 return;
1095
Bram Moolenaar009b2592004-10-24 19:18:58 +00001096 nbdebug(("REP %d: %ld\n", cmdno, result));
1097
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 sprintf(reply, "%d %ld\n", cmdno, result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 nb_send(reply, "nb_reply_nr");
1100}
1101
1102
1103/*
1104 * Encode newline, ret, backslash, double quote for transmission to NetBeans.
1105 */
1106 static char_u *
1107nb_quote(char_u *txt)
1108{
1109 char_u *buf = alloc(2 * STRLEN(txt) + 1);
1110 char_u *p = txt;
1111 char_u *q = buf;
1112
1113 if (buf == NULL)
1114 return NULL;
1115 for (; *p; p++)
1116 {
1117 switch (*p)
1118 {
1119 case '\"':
1120 case '\\':
1121 *q++ = '\\'; *q++ = *p; break;
1122 /* case '\t': */
1123 /* *q++ = '\\'; *q++ = 't'; break; */
1124 case '\n':
1125 *q++ = '\\'; *q++ = 'n'; break;
1126 case '\r':
1127 *q++ = '\\'; *q++ = 'r'; break;
1128 default:
1129 *q++ = *p;
1130 break;
1131 }
1132 }
1133 *q++ = '\0';
1134
1135 return buf;
1136}
1137
1138
1139/*
1140 * Remove top level double quotes; convert backslashed chars.
1141 * Returns an allocated string (NULL for failure).
1142 * If "endp" is not NULL it is set to the character after the terminating
1143 * quote.
1144 */
1145 static char *
1146nb_unquote(char_u *p, char_u **endp)
1147{
1148 char *result = 0;
1149 char *q;
1150 int done = 0;
1151
1152 /* result is never longer than input */
1153 result = (char *)alloc_clear(STRLEN(p) + 1);
1154 if (result == NULL)
1155 return NULL;
1156
1157 if (*p++ != '"')
1158 {
1159 nbdebug(("nb_unquote called with string that doesn't start with a quote!: %s\n",
1160 p));
1161 result[0] = NUL;
1162 return result;
1163 }
1164
1165 for (q = result; !done && *p != NUL;)
1166 {
1167 switch (*p)
1168 {
1169 case '"':
1170 /*
1171 * Unbackslashed dquote marks the end, if first char was dquote.
1172 */
1173 done = 1;
1174 break;
1175
1176 case '\\':
1177 ++p;
1178 switch (*p)
1179 {
1180 case '\\': *q++ = '\\'; break;
1181 case 'n': *q++ = '\n'; break;
1182 case 't': *q++ = '\t'; break;
1183 case 'r': *q++ = '\r'; break;
1184 case '"': *q++ = '"'; break;
1185 case NUL: --p; break;
1186 /* default: skip over illegal chars */
1187 }
1188 ++p;
1189 break;
1190
1191 default:
1192 *q++ = *p++;
1193 }
1194 }
1195
1196 if (endp != NULL)
1197 *endp = p;
1198
1199 return result;
1200}
1201
1202#define SKIP_STOP 2
1203#define streq(a,b) (strcmp(a,b) == 0)
1204static int needupdate = 0;
1205static int inAtomic = 0;
1206
1207/*
1208 * Do the actual processing of a single netbeans command or function.
1209 * The differance between a command and function is that a function
1210 * gets a response (its required) but a command does not.
1211 * For arguments see comment for nb_parse_cmd().
1212 */
1213 static int
1214nb_do_cmd(
1215 int bufno,
1216 char_u *cmd,
1217 int func,
1218 int cmdno,
1219 char_u *args) /* points to space before arguments or NUL */
1220{
1221 int doupdate = 0;
1222 long off = 0;
1223 nbbuf_T *buf = nb_get_buf(bufno);
1224 static int skip = 0;
1225 int retval = OK;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001226 char *cp; /* for when a char pointer is needed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227
1228 nbdebug(("%s %d: (%d) %s %s\n", (func) ? "FUN" : "CMD", cmdno, bufno, cmd,
1229 STRCMP(cmd, "insert") == 0 ? "<text>" : (char *)args));
1230
1231 if (func)
1232 {
1233/* =====================================================================*/
1234 if (streq((char *)cmd, "getModified"))
1235 {
1236 if (buf == NULL || buf->bufp == NULL)
1237 /* Return the number of buffers that are modified. */
1238 nb_reply_nr(cmdno, (long)count_changed_buffers());
1239 else
1240 /* Return whether the buffer is modified. */
1241 nb_reply_nr(cmdno, (long)(buf->bufp->b_changed
1242 || isNetbeansModified(buf->bufp)));
1243/* =====================================================================*/
1244 }
1245 else if (streq((char *)cmd, "saveAndExit"))
1246 {
1247 /* Note: this will exit Vim if successful. */
1248 coloncmd(":confirm qall");
1249
1250 /* We didn't exit: return the number of changed buffers. */
1251 nb_reply_nr(cmdno, (long)count_changed_buffers());
1252/* =====================================================================*/
1253 }
1254 else if (streq((char *)cmd, "getCursor"))
1255 {
1256 char_u text[200];
1257
1258 /* Note: nb_getbufno() may return -1. This indicates the IDE
1259 * didn't assign a number to the current buffer in response to a
1260 * fileOpened event. */
1261 sprintf((char *)text, "%d %ld %d %ld",
1262 nb_getbufno(curbuf),
1263 (long)curwin->w_cursor.lnum,
1264 (int)curwin->w_cursor.col,
1265 pos2off(curbuf, &curwin->w_cursor));
1266 nb_reply_text(cmdno, text);
1267/* =====================================================================*/
1268 }
1269 else if (streq((char *)cmd, "getLength"))
1270 {
1271 long len = 0;
1272
1273 if (buf == NULL || buf->bufp == NULL)
1274 {
1275 nbdebug((" null bufp in getLength"));
1276 EMSG("E632: null bufp in getLength");
1277 retval = FAIL;
1278 }
1279 else
1280 {
1281 len = get_buf_size(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 }
1283 nb_reply_nr(cmdno, len);
1284/* =====================================================================*/
1285 }
1286 else if (streq((char *)cmd, "getText"))
1287 {
1288 long len;
1289 linenr_T nlines;
1290 char_u *text = NULL;
1291 linenr_T lno = 1;
1292 char_u *p;
1293 char_u *line;
1294
1295 if (buf == NULL || buf->bufp == NULL)
1296 {
1297 nbdebug((" null bufp in getText"));
1298 EMSG("E633: null bufp in getText");
1299 retval = FAIL;
1300 }
1301 else
1302 {
1303 len = get_buf_size(buf->bufp);
1304 nlines = buf->bufp->b_ml.ml_line_count;
1305 text = alloc((unsigned)((len > 0)
1306 ? ((len + nlines) * 2) : 4));
1307 if (text == NULL)
1308 {
1309 nbdebug((" nb_do_cmd: getText has null text field\n"));
1310 retval = FAIL;
1311 }
1312 else
1313 {
1314 p = text;
1315 *p++ = '\"';
1316 for (; lno <= nlines ; lno++)
1317 {
1318 line = nb_quote(ml_get_buf(buf->bufp, lno, FALSE));
1319 if (line != NULL)
1320 {
1321 STRCPY(p, line);
1322 p += STRLEN(line);
1323 *p++ = '\\';
1324 *p++ = 'n';
Bram Moolenaar009b2592004-10-24 19:18:58 +00001325 vim_free(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 }
1328 *p++ = '\"';
1329 *p = '\0';
1330 }
1331 }
1332 if (text == NULL)
1333 nb_reply_text(cmdno, (char_u *)"");
1334 else
1335 {
1336 nb_reply_text(cmdno, text);
1337 vim_free(text);
1338 }
1339/* =====================================================================*/
1340 }
1341 else if (streq((char *)cmd, "remove"))
1342 {
1343 long count;
1344 pos_T first, last;
1345 pos_T *pos;
1346 int oldFire = netbeansFireChanges;
1347 int oldSuppress = netbeansSuppressNoLines;
1348 int wasChanged;
1349
1350 if (skip >= SKIP_STOP)
1351 {
1352 nbdebug((" Skipping %s command\n", (char *) cmd));
1353 nb_reply_nil(cmdno);
1354 return OK;
1355 }
1356
1357 if (buf == NULL || buf->bufp == NULL)
1358 {
1359 nbdebug((" null bufp in remove"));
1360 EMSG("E634: null bufp in remove");
1361 retval = FAIL;
1362 }
1363 else
1364 {
1365 netbeansFireChanges = FALSE;
1366 netbeansSuppressNoLines = TRUE;
1367
1368 if (curbuf != buf->bufp)
1369 set_curbuf(buf->bufp, DOBUF_GOTO);
1370 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
1491 if (curbuf != buf->bufp)
1492 set_curbuf(buf->bufp, DOBUF_GOTO);
1493 old_b_changed = buf->bufp->b_changed;
1494
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 pos = off2pos(buf->bufp, off);
1496 if (pos)
1497 {
1498 if (pos->lnum == 0)
1499 pos->lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 }
1501 else
1502 {
1503 /* if the given position is not found, assume we want
1504 * the end of the file. See setLocAndSize HACK. */
1505 pos = &mypos;
1506 pos->col = 0;
1507#ifdef FEAT_VIRTUALEDIT
1508 pos->coladd = 0;
1509#endif
1510 pos->lnum = buf->bufp->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 }
1512 lnum = pos->lnum;
1513 old_w_cursor = curwin->w_cursor;
1514 curwin->w_cursor = *pos;
1515
Bram Moolenaar009b2592004-10-24 19:18:58 +00001516 if (buf->bufp->b_start_eol == FALSE && lnum > 0)
1517 {
1518 /* Append to a partial line */
1519 char_u *partial = ml_get(lnum);
1520
1521 if (partial != IObuff)
1522 STRCPY(lbuf, partial);
1523 lbuf_len = STRLEN(partial);
1524 ml_delete(lnum, FALSE);
1525 }
1526
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 doupdate = 1;
1528 while (*args)
1529 {
1530 nl = (char_u *)strchr((char *)args, '\n');
Bram Moolenaar009b2592004-10-24 19:18:58 +00001531 if (nl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00001533 STRNCAT(lbuf, args, nl - args);
1534 lbuf[lbuf_len + nl - args] = '\0';
1535 args += nl - args + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001537 else
1538 {
1539 STRCPY(lbuf, args);
1540 args += STRLEN(lbuf);
1541 }
1542
1543 /*
1544 * EOL detecting. Not sure how to deal with '\n' on Mac.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 */
Bram Moolenaar009b2592004-10-24 19:18:58 +00001546 if (buf_was_empty && nl && *(nl - 1) != '\r')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 ff_detected = EOL_UNIX;
1548
Bram Moolenaar009b2592004-10-24 19:18:58 +00001549 /* nbdebug((" INSERT[%d]: %s\n", lnum, lbuf)); */
1550 ml_append((linenr_T)(lnum++ - 1), lbuf,
1551 STRLEN(lbuf) + 1, FALSE);
1552 lbuf[0] = '\0'; /* empty buffer */
1553 lbuf_len = 0;
1554 }
1555
1556 if (*(args - 1) == '\n')
1557 {
1558 buf->bufp->b_p_eol = TRUE;
1559 buf->bufp->b_start_eol = TRUE;
1560 }
1561 else
1562 {
1563 buf->bufp->b_p_eol = FALSE;
1564 buf->bufp->b_start_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 }
1566
1567 appended_lines_mark(pos->lnum - 1, lnum - pos->lnum);
1568
1569 /* We can change initial ff without consequences
1570 * Isn't it a kind of hacking?
1571 */
1572 if (buf_was_empty)
1573 {
1574 if (ff_detected == EOL_UNKNOWN)
1575 ff_detected = EOL_DOS;
1576 set_fileformat(ff_detected, OPT_LOCAL);
1577 buf->bufp->b_start_ffc = *buf->bufp->b_p_ff;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001578 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 }
1580
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 curwin->w_cursor = old_w_cursor;
1582
1583 /*
1584 * XXX - GRP - Is the next line right? If I've inserted
1585 * text the buffer has been updated but not written. Will
1586 * netbeans guarantee to write it? Even if I do a :q! ?
1587 */
1588 buf->bufp->b_changed = old_b_changed; /* logically unchanged */
1589 netbeansFireChanges = oldFire;
1590
1591 u_blockfree(buf->bufp);
1592 u_clearall(buf->bufp);
1593 }
1594 vim_free(to_free);
1595 nb_reply_nil(cmdno); /* or !error */
1596 }
1597 else
1598 {
1599 nbdebug(("UNIMPLEMENTED FUNCTION: %s\n", cmd));
1600 nb_reply_nil(cmdno);
1601 retval = FAIL;
1602 }
1603 }
1604 else /* Not a function; no reply required. */
1605 {
1606/* =====================================================================*/
1607 if (streq((char *)cmd, "create"))
1608 {
1609 /* Create a buffer without a name. */
1610 if (buf == NULL)
1611 {
1612 EMSG("E636: null buf in create");
1613 return FAIL;
1614 }
1615 vim_free(buf->displayname);
1616 buf->displayname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617
1618 netbeansReadFile = 0; /* don't try to open disk file */
1619 do_ecmd(0, NULL, 0, 0, ECMD_ONE, ECMD_HIDE + ECMD_OLDBUF);
1620 netbeansReadFile = 1;
1621 buf->bufp = curbuf;
1622 maketitle();
Bram Moolenaar009b2592004-10-24 19:18:58 +00001623 buf->insertDone = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 gui_update_menus(0);
1625/* =====================================================================*/
1626 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001627 else if (streq((char *)cmd, "insertDone"))
1628 {
1629 buf->bufp->b_start_eol = *args == 'T';
1630 buf->insertDone = TRUE;
1631 args += 2;
1632 buf->bufp->b_p_ro = *args == 'T';
1633 print_read_msg(buf);
1634/* =====================================================================*/
1635 }
1636 else if (streq((char *)cmd, "saveDone"))
1637 {
1638 long savedChars = atol((char *) args);
1639 print_save_msg(buf, savedChars);
1640/* =====================================================================*/
1641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 else if (streq((char *)cmd, "startDocumentListen"))
1643 {
1644 if (buf == NULL)
1645 {
1646 EMSG("E637: null buf in startDocumentListen");
1647 return FAIL;
1648 }
1649 buf->fireChanges = 1;
1650/* =====================================================================*/
1651 }
1652 else if (streq((char *)cmd, "stopDocumentListen"))
1653 {
1654 if (buf == NULL)
1655 {
1656 EMSG("E638: null buf in stopDocumentListen");
1657 return FAIL;
1658 }
1659 buf->fireChanges = 0;
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001660 if (buf->bufp != NULL && buf->bufp->b_was_netbeans_file)
Bram Moolenaar009b2592004-10-24 19:18:58 +00001661 {
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001662 if (!buf->bufp->b_netbeans_file)
Bram Moolenaar009b2592004-10-24 19:18:58 +00001663 EMSGN(_("E658: NetBeans connection lost for buffer %ld"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 buf->bufp->b_fnum);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001665 else
1666 {
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001667 /* NetBeans uses stopDocumentListen when it stops editing
1668 * a file. It then expects the buffer in Vim to
1669 * disappear. */
1670 do_bufdel(DOBUF_DEL, (char_u *)"", 1,
1671 buf->bufp->b_fnum, buf->bufp->b_fnum, TRUE);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001672 vim_memset(buf, 0, sizeof(nbbuf_T));
1673 }
1674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675/* =====================================================================*/
1676 }
1677 else if (streq((char *)cmd, "setTitle"))
1678 {
1679 if (buf == NULL)
1680 {
1681 EMSG("E639: null buf in setTitle");
1682 return FAIL;
1683 }
1684 vim_free(buf->displayname);
1685 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686/* =====================================================================*/
1687 }
1688 else if (streq((char *)cmd, "initDone"))
1689 {
1690 if (buf == NULL || buf->bufp == NULL)
1691 {
1692 EMSG("E640: null buf in initDone");
1693 return FAIL;
1694 }
1695 doupdate = 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001696 buf->initDone = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 if (curbuf != buf->bufp)
1698 set_curbuf(buf->bufp, DOBUF_GOTO);
1699#if defined(FEAT_AUTOCMD)
1700 apply_autocmds(EVENT_BUFREADPOST, 0, 0, FALSE, buf->bufp);
1701#endif
1702
1703 /* handle any postponed key commands */
1704 handle_key_queue();
1705/* =====================================================================*/
1706 }
1707 else if (streq((char *)cmd, "setBufferNumber")
1708 || streq((char *)cmd, "putBufferNumber"))
1709 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00001710 char_u *path;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 buf_T *bufp;
1712
1713 if (buf == NULL)
1714 {
1715 EMSG("E641: null buf in setBufferNumber");
1716 return FAIL;
1717 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001718 path = (char_u *)nb_unquote(args, NULL);
1719 if (path == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 return FAIL;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001721 bufp = buflist_findname(path);
1722 vim_free(path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 if (bufp == NULL)
1724 {
1725 EMSG2("E642: File %s not found in setBufferNumber", args);
1726 return FAIL;
1727 }
1728 buf->bufp = bufp;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001729 buf->nbbuf_number = bufp->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730
1731 /* "setBufferNumber" has the side effect of jumping to the buffer
1732 * (don't know why!). Don't do that for "putBufferNumber". */
1733 if (*cmd != 'p')
1734 coloncmd(":buffer %d", bufp->b_fnum);
1735 else
1736 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00001737 buf->initDone = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738
1739 /* handle any postponed key commands */
1740 handle_key_queue();
1741 }
1742
1743#if 0 /* never used */
1744 buf->internalname = (char *)alloc_clear(8);
1745 sprintf(buf->internalname, "<%d>", bufno);
1746 buf->netbeansOwns = 0;
1747#endif
1748/* =====================================================================*/
1749 }
1750 else if (streq((char *)cmd, "setFullName"))
1751 {
1752 if (buf == NULL)
1753 {
1754 EMSG("E643: null buf in setFullName");
1755 return FAIL;
1756 }
1757 vim_free(buf->displayname);
1758 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759
1760 netbeansReadFile = 0; /* don't try to open disk file */
1761 do_ecmd(0, (char_u *)buf->displayname, 0, 0, ECMD_ONE,
1762 ECMD_HIDE + ECMD_OLDBUF);
1763 netbeansReadFile = 1;
1764 buf->bufp = curbuf;
1765 maketitle();
1766 gui_update_menus(0);
1767/* =====================================================================*/
1768 }
1769 else if (streq((char *)cmd, "editFile"))
1770 {
1771 if (buf == NULL)
1772 {
1773 EMSG("E644: null buf in editFile");
1774 return FAIL;
1775 }
1776 /* Edit a file: like create + setFullName + read the file. */
1777 vim_free(buf->displayname);
1778 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 do_ecmd(0, (char_u *)buf->displayname, NULL, NULL, ECMD_ONE,
1780 ECMD_HIDE + ECMD_OLDBUF);
1781 buf->bufp = curbuf;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001782 buf->initDone = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 doupdate = 1;
1784#if defined(FEAT_TITLE)
1785 maketitle();
1786#endif
1787 gui_update_menus(0);
1788/* =====================================================================*/
1789 }
1790 else if (streq((char *)cmd, "setVisible"))
1791 {
1792 if (buf == NULL || buf->bufp == NULL)
1793 {
1794/* EMSG("E645: null bufp in setVisible"); */
1795 return FAIL;
1796 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001797 if (streq((char *)args, "T") && buf->bufp != curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 {
1799 exarg_T exarg;
1800 exarg.cmd = (char_u *)"goto";
1801 exarg.forceit = FALSE;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001802 dosetvisible = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 goto_buffer(&exarg, DOBUF_FIRST, FORWARD, buf->bufp->b_fnum);
1804 doupdate = 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001805 dosetvisible = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806
1807 /* Side effect!!!. */
1808 if (!gui.starting)
1809 gui_mch_set_foreground();
1810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811/* =====================================================================*/
1812 }
1813 else if (streq((char *)cmd, "raise"))
1814 {
1815 /* Bring gvim to the foreground. */
1816 if (!gui.starting)
1817 gui_mch_set_foreground();
1818/* =====================================================================*/
1819 }
1820 else if (streq((char *)cmd, "setModified"))
1821 {
1822 if (buf == NULL || buf->bufp == NULL)
1823 {
1824/* EMSG("E646: null bufp in setModified"); */
1825 return FAIL;
1826 }
1827 if (streq((char *)args, "T"))
1828 buf->bufp->b_changed = 1;
1829 else
1830 {
1831 struct stat st;
1832
1833 /* Assume NetBeans stored the file. Reset the timestamp to
1834 * avoid "file changed" warnings. */
1835 if (buf->bufp->b_ffname != NULL
1836 && mch_stat((char *)buf->bufp->b_ffname, &st) >= 0)
1837 buf_store_time(buf->bufp, &st, buf->bufp->b_ffname);
1838 buf->bufp->b_changed = 0;
1839 }
1840 buf->modified = buf->bufp->b_changed;
1841/* =====================================================================*/
1842 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001843 else if (streq((char *)cmd, "setModtime"))
1844 {
1845 buf->bufp->b_mtime = atoi((char *) args);
1846/* =====================================================================*/
1847 }
1848 else if (streq((char *)cmd, "setReadOnly"))
1849 {
1850 if (streq((char *)args, "T"))
1851 buf->bufp->b_p_ro = TRUE;
1852 else
1853 buf->bufp->b_p_ro = FALSE;
1854/* =====================================================================*/
1855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 else if (streq((char *)cmd, "setMark"))
1857 {
1858 /* not yet */
1859/* =====================================================================*/
1860 }
1861 else if (streq((char *)cmd, "showBalloon"))
1862 {
1863#if defined(FEAT_BEVAL)
1864 static char *text = NULL;
1865
1866 /*
1867 * Set up the Balloon Expression Evaluation area.
1868 * Ignore 'ballooneval' here.
1869 * The text pointer must remain valid for a while.
1870 */
1871 if (balloonEval != NULL)
1872 {
1873 vim_free(text);
1874 text = nb_unquote(args, NULL);
1875 if (text != NULL)
1876 gui_mch_post_balloon(balloonEval, (char_u *)text);
1877 }
1878#endif
1879/* =====================================================================*/
1880 }
1881 else if (streq((char *)cmd, "setDot"))
1882 {
1883 pos_T *pos;
1884#ifdef NBDEBUG
1885 char_u *s;
1886#endif
1887
1888 if (buf == NULL || buf->bufp == NULL)
1889 {
1890 EMSG("E647: null bufp in setDot");
1891 return FAIL;
1892 }
1893
1894 if (curbuf != buf->bufp)
1895 set_curbuf(buf->bufp, DOBUF_GOTO);
1896#ifdef FEAT_VISUAL
1897 /* Don't want Visual mode now. */
1898 if (VIsual_active)
1899 end_visual_mode();
1900#endif
1901#ifdef NBDEBUG
1902 s = args;
1903#endif
1904 pos = get_off_or_lnum(buf->bufp, &args);
1905 if (pos)
1906 {
1907 curwin->w_cursor = *pos;
1908 check_cursor();
1909#ifdef FEAT_FOLDING
1910 foldOpenCursor();
1911#endif
1912 }
1913 else
1914 nbdebug((" BAD POSITION in setDot: %s\n", s));
1915
1916 /* gui_update_cursor(TRUE, FALSE); */
1917 /* update_curbuf(NOT_VALID); */
1918 update_topline(); /* scroll to show the line */
1919 update_screen(VALID);
1920 setcursor();
1921 out_flush();
1922 gui_update_cursor(TRUE, FALSE);
1923 gui_mch_flush();
1924 /* Quit a hit-return or more prompt. */
1925 if (State == HITRETURN || State == ASKMORE)
1926 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927#ifdef FEAT_GUI_GTK
1928 if (gtk_main_level() > 0)
1929 gtk_main_quit();
1930#endif
1931 }
1932/* =====================================================================*/
1933 }
1934 else if (streq((char *)cmd, "close"))
1935 {
1936#ifdef NBDEBUG
1937 char *name = "<NONE>";
1938#endif
1939
1940 if (buf == NULL)
1941 {
1942 EMSG("E648: null buf in close");
1943 return FAIL;
1944 }
1945
1946#ifdef NBDEBUG
1947 if (buf->displayname != NULL)
1948 name = buf->displayname;
1949#endif
1950/* if (buf->bufp == NULL) */
1951/* EMSG("E649: null bufp in close"); */
1952 nbdebug((" CLOSE %d: %s\n", bufno, name));
1953 need_mouse_correct = TRUE;
1954 if (buf->bufp != NULL)
1955 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD,
1956 buf->bufp->b_fnum, TRUE);
1957 doupdate = 1;
1958/* =====================================================================*/
1959 }
1960 else if (streq((char *)cmd, "setStyle")) /* obsolete... */
1961 {
1962 nbdebug((" setStyle is obsolete!"));
1963/* =====================================================================*/
1964 }
1965 else if (streq((char *)cmd, "setExitDelay"))
1966 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00001967 /* Only used in version 2.1. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968/* =====================================================================*/
1969 }
1970 else if (streq((char *)cmd, "defineAnnoType"))
1971 {
1972#ifdef FEAT_SIGNS
1973 int typeNum;
1974 char_u *typeName;
1975 char_u *tooltip;
1976 char_u *p;
1977 char_u *glyphFile;
1978 int use_fg = 0;
1979 int use_bg = 0;
1980 int fg = -1;
1981 int bg = -1;
1982
1983 if (buf == NULL)
1984 {
1985 EMSG("E650: null buf in defineAnnoType");
1986 return FAIL;
1987 }
1988
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001989 cp = (char *)args;
1990 typeNum = strtol(cp, &cp, 10);
1991 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 args = skipwhite(args);
1993 typeName = (char_u *)nb_unquote(args, &args);
1994 args = skipwhite(args + 1);
1995 tooltip = (char_u *)nb_unquote(args, &args);
1996 args = skipwhite(args + 1);
1997
1998 p = (char_u *)nb_unquote(args, &args);
1999 glyphFile = vim_strsave_escaped(p, escape_chars);
2000 vim_free(p);
2001
2002 args = skipwhite(args + 1);
2003 if (STRNCMP(args, "none", 4) == 0)
2004 args += 5;
2005 else
2006 {
2007 use_fg = 1;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002008 cp = (char *)args;
2009 fg = strtol(cp, &cp, 10);
2010 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 }
2012 if (STRNCMP(args, "none", 4) == 0)
2013 args += 5;
2014 else
2015 {
2016 use_bg = 1;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002017 cp = (char *)args;
2018 bg = strtol(cp, &cp, 10);
2019 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 }
2021 if (typeName != NULL && tooltip != NULL && glyphFile != NULL)
2022 addsigntype(buf, typeNum, typeName, tooltip, glyphFile,
2023 use_fg, fg, use_bg, bg);
2024 else
2025 vim_free(typeName);
2026
2027 /* don't free typeName; it's used directly in addsigntype() */
2028 vim_free(tooltip);
2029 vim_free(glyphFile);
2030
2031#endif
2032/* =====================================================================*/
2033 }
2034 else if (streq((char *)cmd, "addAnno"))
2035 {
2036#ifdef FEAT_SIGNS
2037 int serNum;
2038 int localTypeNum;
2039 int typeNum;
2040# ifdef NBDEBUG
2041 int len;
2042# endif
2043 pos_T *pos;
2044
2045 if (buf == NULL || buf->bufp == NULL)
2046 {
2047 EMSG("E651: null bufp in addAnno");
2048 return FAIL;
2049 }
2050
2051 doupdate = 1;
2052
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002053 cp = (char *)args;
2054 serNum = strtol(cp, &cp, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055
2056 /* Get the typenr specific for this buffer and convert it to
2057 * the global typenumber, as used for the sign name. */
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002058 localTypeNum = strtol(cp, &cp, 10);
2059 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 typeNum = mapsigntype(buf, localTypeNum);
2061
2062 pos = get_off_or_lnum(buf->bufp, &args);
2063
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002064 cp = (char *)args;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065# ifdef NBDEBUG
2066 len =
2067# endif
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002068 strtol(cp, &cp, 10);
2069 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070# ifdef NBDEBUG
2071 if (len != -1)
2072 {
2073 nbdebug((" partial line annotation -- Not Yet Implemented!"));
2074 }
2075# endif
2076 if (serNum >= GUARDEDOFFSET)
2077 {
2078 nbdebug((" too many annotations! ignoring..."));
2079 return FAIL;
2080 }
2081 if (pos)
2082 {
2083 coloncmd(":sign place %d line=%d name=%d buffer=%d",
2084 serNum, pos->lnum, typeNum, buf->bufp->b_fnum);
2085 if (typeNum == curPCtype)
2086 coloncmd(":sign jump %d buffer=%d", serNum,
2087 buf->bufp->b_fnum);
2088 }
2089 /* XXX only redraw what changed. */
2090 redraw_later(CLEAR);
2091#endif
2092/* =====================================================================*/
2093 }
2094 else if (streq((char *)cmd, "removeAnno"))
2095 {
2096#ifdef FEAT_SIGNS
2097 int serNum;
2098
2099 if (buf == NULL || buf->bufp == NULL)
2100 {
2101 nbdebug((" null bufp in removeAnno"));
2102 return FAIL;
2103 }
2104 doupdate = 1;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002105 cp = (char *)args;
2106 serNum = strtol(cp, &cp, 10);
2107 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 coloncmd(":sign unplace %d buffer=%d",
2109 serNum, buf->bufp->b_fnum);
2110 redraw_buf_later(buf->bufp, NOT_VALID);
2111#endif
2112/* =====================================================================*/
2113 }
2114 else if (streq((char *)cmd, "moveAnnoToFront"))
2115 {
2116#ifdef FEAT_SIGNS
2117 nbdebug((" moveAnnoToFront: Not Yet Implemented!"));
2118#endif
2119/* =====================================================================*/
2120 }
2121 else if (streq((char *)cmd, "guard") || streq((char *)cmd, "unguard"))
2122 {
2123 int len;
2124 pos_T first;
2125 pos_T last;
2126 pos_T *pos;
2127 int un = (cmd[0] == 'u');
2128 static int guardId = GUARDEDOFFSET;
2129
2130 if (skip >= SKIP_STOP)
2131 {
2132 nbdebug((" Skipping %s command\n", (char *) cmd));
2133 return OK;
2134 }
2135
2136 nb_init_graphics();
2137
2138 if (buf == NULL || buf->bufp == NULL)
2139 {
2140 nbdebug((" null bufp in %s command", cmd));
2141 return FAIL;
2142 }
2143 if (curbuf != buf->bufp)
2144 set_curbuf(buf->bufp, DOBUF_GOTO);
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002145 cp = (char *)args;
2146 off = strtol(cp, &cp, 10);
2147 len = strtol(cp, NULL, 10);
2148 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 pos = off2pos(buf->bufp, off);
2150 doupdate = 1;
2151 if (!pos)
2152 nbdebug((" no such start pos in %s, %ld\n", cmd, off));
2153 else
2154 {
2155 first = *pos;
2156 pos = off2pos(buf->bufp, off + len - 1);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002157 if (pos != NULL && pos->col == 0)
2158 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 /*
2160 * In Java Swing the offset is a position between 2
2161 * characters. If col == 0 then we really want the
2162 * previous line as the end.
2163 */
2164 pos = off2pos(buf->bufp, off + len - 2);
2165 }
2166 if (!pos)
2167 nbdebug((" no such end pos in %s, %ld\n",
2168 cmd, off + len - 1));
2169 else
2170 {
2171 long lnum;
2172 last = *pos;
2173 /* set highlight for region */
2174 nbdebug((" %sGUARD %ld,%d to %ld,%d\n", (un) ? "UN" : "",
2175 first.lnum, first.col,
2176 last.lnum, last.col));
2177#ifdef FEAT_SIGNS
2178 for (lnum = first.lnum; lnum <= last.lnum; lnum++)
2179 {
2180 if (un)
2181 {
2182 /* never used */
2183 }
2184 else
2185 {
2186 if (buf_findsigntype_id(buf->bufp, lnum,
2187 GUARDED) == 0)
2188 {
2189 coloncmd(
2190 ":sign place %d line=%d name=%d buffer=%d",
2191 guardId++, lnum, GUARDED,
2192 buf->bufp->b_fnum);
2193 }
2194 }
2195 }
2196#endif
2197 redraw_buf_later(buf->bufp, NOT_VALID);
2198 }
2199 }
2200/* =====================================================================*/
2201 }
2202 else if (streq((char *)cmd, "startAtomic"))
2203 {
2204 inAtomic = 1;
2205/* =====================================================================*/
2206 }
2207 else if (streq((char *)cmd, "endAtomic"))
2208 {
2209 inAtomic = 0;
2210 if (needupdate)
2211 {
2212 doupdate = 1;
2213 needupdate = 0;
2214 }
2215/* =====================================================================*/
2216 }
2217 else if (streq((char *)cmd, "save"))
2218 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002219 /*
2220 * NOTE - This command is obsolete wrt NetBeans. Its left in
2221 * only for historical reasons.
2222 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 if (buf == NULL || buf->bufp == NULL)
2224 {
2225 nbdebug((" null bufp in %s command", cmd));
2226 return FAIL;
2227 }
2228
2229 /* the following is taken from ex_cmds.c (do_wqall function) */
2230 if (bufIsChanged(buf->bufp))
2231 {
2232 /* Only write if the buffer can be written. */
2233 if (p_write
2234 && !buf->bufp->b_p_ro
2235 && buf->bufp->b_ffname != NULL
2236#ifdef FEAT_QUICKFIX
2237 && !bt_dontwrite(buf->bufp)
2238#endif
2239 )
2240 {
2241 buf_write_all(buf->bufp, FALSE);
2242#ifdef FEAT_AUTOCMD
2243 /* an autocommand may have deleted the buffer */
2244 if (!buf_valid(buf->bufp))
2245 buf->bufp = NULL;
2246#endif
2247 }
2248 }
2249/* =====================================================================*/
2250 }
2251 else if (streq((char *)cmd, "netbeansBuffer"))
2252 {
2253 if (buf == NULL || buf->bufp == NULL)
2254 {
2255 nbdebug((" null bufp in %s command", cmd));
2256 return FAIL;
2257 }
2258 if (*args == 'T')
2259 {
2260 buf->bufp->b_netbeans_file = TRUE;
2261 buf->bufp->b_was_netbeans_file = TRUE;
2262 }
2263 else
2264 buf->bufp->b_netbeans_file = FALSE;
2265/* =====================================================================*/
2266 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002267 else if (streq((char *)cmd, "specialKeys"))
2268 {
2269 special_keys(args);
2270/* =====================================================================*/
2271 }
2272 else if (streq((char *)cmd, "actionMenuItem"))
2273 {
2274 /* not used yet */
2275/* =====================================================================*/
2276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 else if (streq((char *)cmd, "version"))
2278 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002279 /* not used yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 }
2281 /*
2282 * Unrecognized command is ignored.
2283 */
2284 }
2285 if (inAtomic && doupdate)
2286 {
2287 needupdate = 1;
2288 doupdate = 0;
2289 }
2290
Bram Moolenaar009b2592004-10-24 19:18:58 +00002291 /*
2292 * Is this needed? I moved the netbeans_Xt_connect() later during startup
2293 * and it may no longer be necessary. If its not needed then needupdate
2294 * and doupdate can also be removed.
2295 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 if (buf != NULL && buf->initDone && doupdate)
2297 {
2298 update_screen(NOT_VALID);
2299 setcursor();
2300 out_flush();
2301 gui_update_cursor(TRUE, FALSE);
2302 gui_mch_flush();
2303 /* Quit a hit-return or more prompt. */
2304 if (State == HITRETURN || State == ASKMORE)
2305 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306#ifdef FEAT_GUI_GTK
2307 if (gtk_main_level() > 0)
2308 gtk_main_quit();
2309#endif
2310 }
2311 }
2312
2313 return retval;
2314}
2315
2316
2317/*
2318 * Process a vim colon command.
2319 */
2320 static void
2321coloncmd(char *cmd, ...)
2322{
2323 char buf[1024];
2324 va_list ap;
2325
2326 va_start(ap, cmd);
2327 vsprintf(buf, cmd, ap);
2328 va_end(ap);
2329
2330 nbdebug((" COLONCMD %s\n", buf));
2331
2332/* ALT_INPUT_LOCK_ON; */
2333 do_cmdline((char_u *)buf, NULL, NULL, DOCMD_NOWAIT | DOCMD_KEYTYPED);
2334/* ALT_INPUT_LOCK_OFF; */
2335
2336 setcursor(); /* restore the cursor position */
2337 out_flush(); /* make sure output has been written */
2338
2339 gui_update_cursor(TRUE, FALSE);
2340 gui_mch_flush();
2341}
2342
2343
2344/*
Bram Moolenaar009b2592004-10-24 19:18:58 +00002345 * Parse the specialKeys argument and issue the appropriate map commands.
2346 */
2347 static void
2348special_keys(char_u *args)
2349{
2350 char *save_str = nb_unquote(args, NULL);
2351 char *tok = strtok(save_str, " ");
2352 char *sep;
2353 char keybuf[64];
2354 char cmdbuf[256];
2355
2356 while (tok != NULL)
2357 {
2358 int i = 0;
2359
2360 if ((sep = strchr(tok, '-')) != NULL)
2361 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002362 *sep = NUL;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002363 while (*tok)
2364 {
2365 switch (*tok)
2366 {
2367 case 'A':
2368 case 'M':
2369 case 'C':
2370 case 'S':
2371 keybuf[i++] = *tok;
2372 keybuf[i++] = '-';
2373 break;
2374 }
2375 tok++;
2376 }
2377 tok++;
2378 }
2379
2380 strcpy(&keybuf[i], tok);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002381 vim_snprintf(cmdbuf, sizeof(cmdbuf),
2382 "<silent><%s> :nbkey %s<CR>", keybuf, keybuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002383 do_map(0, (char_u *)cmdbuf, NORMAL, FALSE);
2384 tok = strtok(NULL, " ");
2385 }
2386 vim_free(save_str);
2387}
2388
2389
2390 void
2391ex_nbkey(eap)
2392 exarg_T *eap;
2393{
2394 netbeans_keystring(0, (char *)eap->arg);
2395}
2396
2397
2398/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 * Initialize highlights and signs for use by netbeans (mostly obsolete)
2400 */
2401 static void
2402nb_init_graphics(void)
2403{
2404 static int did_init = FALSE;
2405
2406 if (!did_init)
2407 {
2408 coloncmd(":highlight NBGuarded guibg=Cyan guifg=Black");
2409 coloncmd(":sign define %d linehl=NBGuarded", GUARDED);
2410
2411 did_init = TRUE;
2412 }
2413}
2414
2415/*
2416 * Convert key to netbeans name.
2417 */
2418 static void
2419netbeans_keyname(int key, char *buf)
2420{
2421 char *name = 0;
2422 char namebuf[2];
2423 int ctrl = 0;
2424 int shift = 0;
2425 int alt = 0;
2426
2427 if (mod_mask & MOD_MASK_CTRL)
2428 ctrl = 1;
2429 if (mod_mask & MOD_MASK_SHIFT)
2430 shift = 1;
2431 if (mod_mask & MOD_MASK_ALT)
2432 alt = 1;
2433
2434
2435 switch (key)
2436 {
2437 case K_F1: name = "F1"; break;
2438 case K_S_F1: name = "F1"; shift = 1; break;
2439 case K_F2: name = "F2"; break;
2440 case K_S_F2: name = "F2"; shift = 1; break;
2441 case K_F3: name = "F3"; break;
2442 case K_S_F3: name = "F3"; shift = 1; break;
2443 case K_F4: name = "F4"; break;
2444 case K_S_F4: name = "F4"; shift = 1; break;
2445 case K_F5: name = "F5"; break;
2446 case K_S_F5: name = "F5"; shift = 1; break;
2447 case K_F6: name = "F6"; break;
2448 case K_S_F6: name = "F6"; shift = 1; break;
2449 case K_F7: name = "F7"; break;
2450 case K_S_F7: name = "F7"; shift = 1; break;
2451 case K_F8: name = "F8"; break;
2452 case K_S_F8: name = "F8"; shift = 1; break;
2453 case K_F9: name = "F9"; break;
2454 case K_S_F9: name = "F9"; shift = 1; break;
2455 case K_F10: name = "F10"; break;
2456 case K_S_F10: name = "F10"; shift = 1; break;
2457 case K_F11: name = "F11"; break;
2458 case K_S_F11: name = "F11"; shift = 1; break;
2459 case K_F12: name = "F12"; break;
2460 case K_S_F12: name = "F12"; shift = 1; break;
2461 default:
2462 if (key >= ' ' && key <= '~')
2463 {
2464 /* Allow ASCII characters. */
2465 name = namebuf;
2466 namebuf[0] = key;
2467 namebuf[1] = NUL;
2468 }
2469 else
2470 name = "X";
2471 break;
2472 }
2473
2474 buf[0] = '\0';
2475 if (ctrl)
2476 strcat(buf, "C");
2477 if (shift)
2478 strcat(buf, "S");
2479 if (alt)
2480 strcat(buf, "M"); /* META */
2481 if (ctrl || shift || alt)
2482 strcat(buf, "-");
2483 strcat(buf, name);
2484}
2485
2486#ifdef FEAT_BEVAL
2487/*
2488 * Function to be called for balloon evaluation. Grabs the text under the
2489 * cursor and sends it to the debugger for evaluation. The debugger should
2490 * respond with a showBalloon command when there is a useful result.
2491 */
2492/*ARGSUSED*/
Bram Moolenaard62bec82005-03-07 22:56:57 +00002493 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494netbeans_beval_cb(
2495 BalloonEval *beval,
2496 int state)
2497{
Bram Moolenaard62bec82005-03-07 22:56:57 +00002498 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 char_u *text;
Bram Moolenaard62bec82005-03-07 22:56:57 +00002500 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 int col;
2502 char buf[MAXPATHL * 2 + 25];
2503 char_u *p;
2504
2505 /* Don't do anything when 'ballooneval' is off, messages scrolled the
2506 * windows up or we have no connection. */
2507 if (!p_beval || msg_scrolled > 0 || !haveConnection)
2508 return;
2509
Bram Moolenaard62bec82005-03-07 22:56:57 +00002510 if (get_beval_info(beval, TRUE, &wp, &lnum, &text, &col) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 {
2512 /* Send debugger request. Only when the text is of reasonable
2513 * length. */
2514 if (text != NULL && text[0] != NUL && STRLEN(text) < MAXPATHL)
2515 {
2516 p = nb_quote(text);
2517 if (p != NULL)
Bram Moolenaar009b2592004-10-24 19:18:58 +00002518 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002519 vim_snprintf(buf, sizeof(buf),
2520 "0:balloonText=%d \"%s\"\n", cmdno, p);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002521 vim_free(p);
2522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 nbdebug(("EVT: %s", buf));
2524 nb_send(buf, "netbeans_beval_cb");
2525 }
2526 vim_free(text);
2527 }
2528}
2529#endif
2530
2531/*
2532 * Tell netbeans that the window was opened, ready for commands.
2533 */
2534 void
2535netbeans_startup_done(void)
2536{
2537 char *cmd = "0:startupDone=0\n";
2538
Bram Moolenaar009b2592004-10-24 19:18:58 +00002539 if (usingNetbeans)
2540#ifdef FEAT_GUI_MOTIF
2541 netbeans_Xt_connect(app_context);
2542#else
2543# ifdef FEAT_GUI_GTK
2544 netbeans_gtk_connect();
Bram Moolenaardf177f62005-02-22 08:39:57 +00002545# else
2546# ifdef FEAT_GUI_W32
2547 netbeans_w32_connect();
2548# endif
Bram Moolenaar009b2592004-10-24 19:18:58 +00002549# endif
2550#endif
2551
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 if (!haveConnection)
2553 return;
2554
Bram Moolenaard62bec82005-03-07 22:56:57 +00002555#ifdef FEAT_BEVAL
2556 bevalServers |= BEVAL_NETBEANS;
2557#endif
2558
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 nbdebug(("EVT: %s", cmd));
2560 nb_send(cmd, "netbeans_startup_done");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561}
2562
Bram Moolenaar009b2592004-10-24 19:18:58 +00002563/*
2564 * Tell netbeans that we're exiting. This should be called right
2565 * before calling exit.
2566 */
2567 void
2568netbeans_send_disconnect()
2569{
2570 char buf[128];
2571
2572 if (haveConnection)
2573 {
2574 sprintf(buf, "0:disconnect=%d\n", cmdno);
2575 nbdebug(("EVT: %s", buf));
2576 nb_send(buf, "netbeans_disconnect");
2577 }
2578}
2579
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_W32) || defined(PROTO)
2581/*
2582 * Tell netbeans that the window was moved or resized.
2583 */
2584 void
2585netbeans_frame_moved(int new_x, int new_y)
2586{
2587 char buf[128];
2588
2589 if (!haveConnection)
2590 return;
2591
2592 sprintf(buf, "0:geometry=%d %d %d %d %d\n",
2593 cmdno, (int)Columns, (int)Rows, new_x, new_y);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002594 /*nbdebug(("EVT: %s", buf)); happens too many times during a move */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 nb_send(buf, "netbeans_frame_moved");
2596}
2597#endif
2598
2599/*
Bram Moolenaar009b2592004-10-24 19:18:58 +00002600 * Tell netbeans the user opened or activated a file.
2601 */
2602 void
2603netbeans_file_activated(buf_T *bufp)
2604{
2605 int bufno = nb_getbufno(bufp);
2606 nbbuf_T *bp = nb_get_buf(bufno);
2607 char buffer[2*MAXPATHL];
2608 char_u *q;
2609
2610 if (!haveConnection || dosetvisible)
2611 return;
2612
2613 q = nb_quote(bufp->b_ffname);
2614 if (q == NULL || bp == NULL || bufp == NULL)
2615 return;
2616
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002617 vim_snprintf(buffer, sizeof(buffer), "%d:fileOpened=%d \"%s\" %s %s\n",
Bram Moolenaar009b2592004-10-24 19:18:58 +00002618 bufno,
2619 bufno,
2620 (char *)q,
2621 "T", /* open in NetBeans */
2622 "F"); /* modified */
2623
2624 vim_free(q);
2625 nbdebug(("EVT: %s", buffer));
2626
2627 nb_send(buffer, "netbeans_file_opened");
2628}
2629
2630/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 * Tell netbeans the user opened a file.
2632 */
2633 void
Bram Moolenaar009b2592004-10-24 19:18:58 +00002634netbeans_file_opened(buf_T *bufp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635{
Bram Moolenaar009b2592004-10-24 19:18:58 +00002636 int bufno = nb_getbufno(bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 char buffer[2*MAXPATHL];
2638 char_u *q;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002639 nbbuf_T *bp = nb_get_buf(nb_getbufno(bufp));
2640 int bnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641
2642 if (!haveConnection)
2643 return;
2644
Bram Moolenaar009b2592004-10-24 19:18:58 +00002645 q = nb_quote(bufp->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 if (q == NULL)
2647 return;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002648 if (bp != NULL)
2649 bnum = bufno;
2650 else
2651 bnum = 0;
2652
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002653 vim_snprintf(buffer, sizeof(buffer), "%d:fileOpened=%d \"%s\" %s %s\n",
Bram Moolenaar009b2592004-10-24 19:18:58 +00002654 bnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 0,
2656 (char *)q,
2657 "T", /* open in NetBeans */
2658 "F"); /* modified */
2659
2660 vim_free(q);
2661 nbdebug(("EVT: %s", buffer));
2662
2663 nb_send(buffer, "netbeans_file_opened");
Bram Moolenaar009b2592004-10-24 19:18:58 +00002664 if (p_acd && vim_chdirfile(bufp->b_ffname) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 shorten_fnames(TRUE);
2666}
2667
2668/*
2669 * Tell netbeans a file was closed.
2670 */
2671 void
2672netbeans_file_closed(buf_T *bufp)
2673{
2674 int bufno = nb_getbufno(bufp);
2675 nbbuf_T *nbbuf = nb_get_buf(bufno);
2676 char buffer[2*MAXPATHL];
2677
2678 if (!haveConnection || bufno < 0)
2679 return;
2680
2681 if (!netbeansCloseFile)
2682 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002683 nbdebug(("Ignoring file_closed for %s. File was closed from IDE\n",
2684 bufp->b_ffname));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 return;
2686 }
2687
Bram Moolenaar009b2592004-10-24 19:18:58 +00002688 nbdebug(("netbeans_file_closed:\n"));
2689 nbdebug((" Closing bufno: %d", bufno));
2690 if (curbuf != NULL && curbuf != bufp)
2691 {
2692 nbdebug((" Curbuf bufno: %d\n", nb_getbufno(curbuf)));
2693 }
2694 else if (curbuf == bufp)
2695 {
2696 nbdebug((" curbuf == bufp\n"));
2697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698
2699 if (bufno <= 0)
2700 return;
2701
2702 sprintf(buffer, "%d:killed=%d\n", bufno, cmdno);
2703
2704 nbdebug(("EVT: %s", buffer));
2705
2706 nb_send(buffer, "netbeans_file_closed");
2707
2708 if (nbbuf != NULL)
2709 nbbuf->bufp = NULL;
2710}
2711
2712/*
2713 * Get a pointer to the Netbeans buffer for Vim buffer "bufp".
2714 * Return NULL if there is no such buffer or changes are not to be reported.
2715 * Otherwise store the buffer number in "*bufnop".
2716 */
2717 static nbbuf_T *
2718nb_bufp2nbbuf_fire(buf_T *bufp, int *bufnop)
2719{
2720 int bufno;
2721 nbbuf_T *nbbuf;
2722
2723 if (!haveConnection || !netbeansFireChanges)
2724 return NULL; /* changes are not reported at all */
2725
2726 bufno = nb_getbufno(bufp);
2727 if (bufno <= 0)
2728 return NULL; /* file is not known to NetBeans */
2729
2730 nbbuf = nb_get_buf(bufno);
2731 if (nbbuf != NULL && !nbbuf->fireChanges)
2732 return NULL; /* changes in this buffer are not reported */
2733
2734 *bufnop = bufno;
2735 return nbbuf;
2736}
2737
2738/*
2739 * Tell netbeans the user inserted some text.
2740 */
2741 void
2742netbeans_inserted(
2743 buf_T *bufp,
2744 linenr_T linenr,
2745 colnr_T col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 char_u *txt,
2747 int newlen)
2748{
2749 char_u *buf;
2750 int bufno;
2751 nbbuf_T *nbbuf;
2752 pos_T pos;
2753 long off;
2754 char_u *p;
2755 char_u *newtxt;
2756
2757 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
2758 if (nbbuf == NULL)
2759 return;
2760
Bram Moolenaar009b2592004-10-24 19:18:58 +00002761 /* Don't mark as modified for initial read */
2762 if (nbbuf->insertDone)
2763 nbbuf->modified = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764
2765 pos.lnum = linenr;
2766 pos.col = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 off = pos2off(bufp, &pos);
2768
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 /* send the "insert" EVT */
2770 newtxt = alloc(newlen + 1);
Bram Moolenaarb6356332005-07-18 21:40:44 +00002771 vim_strncpy(newtxt, txt, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 p = nb_quote(newtxt);
2773 if (p != NULL)
2774 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002775 buf = alloc(128 + 2*newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 sprintf((char *)buf, "%d:insert=%d %ld \"%s\"\n", bufno, cmdno, off, p);
2777 nbdebug(("EVT: %s", buf));
2778 nb_send((char *)buf, "netbeans_inserted");
Bram Moolenaar009b2592004-10-24 19:18:58 +00002779 vim_free(p);
2780 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 vim_free(newtxt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783}
2784
2785/*
2786 * Tell netbeans some bytes have been removed.
2787 */
2788 void
2789netbeans_removed(
2790 buf_T *bufp,
2791 linenr_T linenr,
2792 colnr_T col,
2793 long len)
2794{
2795 char_u buf[128];
2796 int bufno;
2797 nbbuf_T *nbbuf;
2798 pos_T pos;
2799 long off;
2800
2801 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
2802 if (nbbuf == NULL)
2803 return;
2804
2805 if (len < 0)
2806 {
2807 nbdebug(("Negative len %ld in netbeans_removed()!", len));
2808 return;
2809 }
2810
2811 nbbuf->modified = 1;
2812
2813 pos.lnum = linenr;
2814 pos.col = col;
2815
2816 off = pos2off(bufp, &pos);
2817
2818 sprintf((char *)buf, "%d:remove=%d %ld %ld\n", bufno, cmdno, off, len);
2819 nbdebug(("EVT: %s", buf));
2820 nb_send((char *)buf, "netbeans_removed");
2821}
2822
2823/*
2824 * Send netbeans an unmodufied command.
2825 */
2826/*ARGSUSED*/
2827 void
2828netbeans_unmodified(buf_T *bufp)
2829{
2830#if 0
2831 char_u buf[128];
2832 int bufno;
2833 nbbuf_T *nbbuf;
2834
2835 /* This has been disabled, because NetBeans considers a buffer modified
2836 * even when all changes have been undone. */
2837 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
2838 if (nbbuf == NULL)
2839 return;
2840
2841 nbbuf->modified = 0;
2842
2843 sprintf((char *)buf, "%d:unmodified=%d\n", bufno, cmdno);
2844 nbdebug(("EVT: %s", buf));
2845 nb_send((char *)buf, "netbeans_unmodified");
2846#endif
2847}
2848
2849/*
2850 * Send a button release event back to netbeans. Its up to netbeans
2851 * to decide what to do (if anything) with this event.
2852 */
2853 void
2854netbeans_button_release(int button)
2855{
2856 char buf[128];
2857 int bufno;
2858
2859 bufno = nb_getbufno(curbuf);
2860
2861 if (bufno >= 0 && curwin != NULL && curwin->w_buffer == curbuf)
2862 {
Bram Moolenaarc92ad2e2005-01-19 22:08:28 +00002863 int col = mouse_col - W_WINCOL(curwin) - (curwin->w_p_nu ? 9 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 long off = pos2off(curbuf, &curwin->w_cursor);
2865
2866 /* sync the cursor position */
2867 sprintf(buf, "%d:newDotAndMark=%d %ld %ld\n", bufno, cmdno, off, off);
2868 nbdebug(("EVT: %s", buf));
2869 nb_send(buf, "netbeans_button_release[newDotAndMark]");
2870
2871 sprintf(buf, "%d:buttonRelease=%d %d %ld %d\n", bufno, cmdno,
2872 button, (long)curwin->w_cursor.lnum, col);
2873 nbdebug(("EVT: %s", buf));
2874 nb_send(buf, "netbeans_button_release");
2875 }
2876}
2877
2878
2879/*
2880 * Send a keypress event back to netbeans. This usualy simulates some
Bram Moolenaar009b2592004-10-24 19:18:58 +00002881 * kind of function key press. This function operates on a key code.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 */
2883 void
2884netbeans_keycommand(int key)
2885{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 char keyName[60];
Bram Moolenaar009b2592004-10-24 19:18:58 +00002887
2888 netbeans_keyname(key, keyName);
2889 netbeans_keystring(key, keyName);
2890}
2891
2892
2893/*
2894 * Send a keypress event back to netbeans. This usualy simulates some
2895 * kind of function key press. This function operates on a key string.
2896 */
2897 static void
2898netbeans_keystring(int key, char *keyName)
2899{
2900 char buf[2*MAXPATHL];
2901 int bufno = nb_getbufno(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 long off;
2903 char_u *q;
2904
2905 if (!haveConnection)
2906 return;
2907
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908
2909 if (bufno == -1)
2910 {
2911 nbdebug(("got keycommand for non-NetBeans buffer, opening...\n"));
2912 q = curbuf->b_ffname == NULL ? (char_u *)""
2913 : nb_quote(curbuf->b_ffname);
2914 if (q == NULL)
2915 return;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002916 vim_snprintf(buf, sizeof(buf), "0:fileOpened=%d \"%s\" %s %s\n", 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 q,
2918 "T", /* open in NetBeans */
2919 "F"); /* modified */
2920 if (curbuf->b_ffname != NULL)
2921 vim_free(q);
2922 nbdebug(("EVT: %s", buf));
2923 nb_send(buf, "netbeans_keycommand");
2924
Bram Moolenaar5b625c52005-01-31 18:55:55 +00002925 if (key > 0)
2926 postpone_keycommand(key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 return;
2928 }
2929
2930 /* sync the cursor position */
2931 off = pos2off(curbuf, &curwin->w_cursor);
2932 sprintf(buf, "%d:newDotAndMark=%d %ld %ld\n", bufno, cmdno, off, off);
2933 nbdebug(("EVT: %s", buf));
2934 nb_send(buf, "netbeans_keycommand");
2935
2936 /* To work on Win32 you must apply patch to ExtEditor module
2937 * from ExtEdCaret.java.diff - make EVT_newDotAndMark handler
2938 * more synchronous
2939 */
2940
2941 /* now send keyCommand event */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002942 vim_snprintf(buf, sizeof(buf), "%d:keyCommand=%d \"%s\"\n",
2943 bufno, cmdno, keyName);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 nbdebug(("EVT: %s", buf));
2945 nb_send(buf, "netbeans_keycommand");
2946
2947 /* New: do both at once and include the lnum/col. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002948 vim_snprintf(buf, sizeof(buf), "%d:keyAtPos=%d \"%s\" %ld %ld/%ld\n",
2949 bufno, cmdno, keyName,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 off, (long)curwin->w_cursor.lnum, (long)curwin->w_cursor.col);
2951 nbdebug(("EVT: %s", buf));
2952 nb_send(buf, "netbeans_keycommand");
2953}
2954
2955
2956/*
2957 * Send a save event to netbeans.
2958 */
2959 void
2960netbeans_save_buffer(buf_T *bufp)
2961{
2962 char_u buf[64];
2963 int bufno;
2964 nbbuf_T *nbbuf;
2965
2966 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
2967 if (nbbuf == NULL)
2968 return;
2969
2970 nbbuf->modified = 0;
2971
2972 sprintf((char *)buf, "%d:save=%d\n", bufno, cmdno);
2973 nbdebug(("EVT: %s", buf));
2974 nb_send((char *)buf, "netbeans_save_buffer");
2975}
2976
2977
2978/*
2979 * Send remove command to netbeans (this command has been turned off).
2980 */
2981 void
2982netbeans_deleted_all_lines(buf_T *bufp)
2983{
2984 char_u buf[64];
2985 int bufno;
2986 nbbuf_T *nbbuf;
2987
2988 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
2989 if (nbbuf == NULL)
2990 return;
2991
Bram Moolenaar009b2592004-10-24 19:18:58 +00002992 /* Don't mark as modified for initial read */
2993 if (nbbuf->insertDone)
2994 nbbuf->modified = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995
2996 sprintf((char *)buf, "%d:remove=%d 0 -1\n", bufno, cmdno);
2997 nbdebug(("EVT(suppressed): %s", buf));
2998/* nb_send(buf, "netbeans_deleted_all_lines"); */
2999}
3000
3001
3002/*
3003 * See if the lines are guarded. The top and bot parameters are from
3004 * u_savecommon(), these are the line above the change and the line below the
3005 * change.
3006 */
3007 int
3008netbeans_is_guarded(linenr_T top, linenr_T bot)
3009{
3010 signlist_T *p;
3011 int lnum;
3012
3013 for (p = curbuf->b_signlist; p != NULL; p = p->next)
3014 if (p->id >= GUARDEDOFFSET)
3015 for (lnum = top + 1; lnum < bot; lnum++)
3016 if (lnum == p->lnum)
3017 return TRUE;
3018
3019 return FALSE;
3020}
3021
3022#if defined(FEAT_GUI_MOTIF) || defined(PROTO)
3023/*
3024 * We have multiple signs to draw at the same location. Draw the
3025 * multi-sign indicator instead. This is the Motif version.
3026 */
3027 void
3028netbeans_draw_multisign_indicator(int row)
3029{
3030 int i;
3031 int y;
3032 int x;
3033
3034 x = 0;
3035 y = row * gui.char_height + 2;
3036
3037 for (i = 0; i < gui.char_height - 3; i++)
3038 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y++);
3039
3040 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+0, y);
3041 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3042 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+4, y++);
3043 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+1, y);
3044 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3045 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+3, y++);
3046 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3047}
3048#endif /* FEAT_GUI_MOTIF */
3049
3050#ifdef FEAT_GUI_GTK
3051/*
3052 * We have multiple signs to draw at the same location. Draw the
3053 * multi-sign indicator instead. This is the GTK/Gnome version.
3054 */
3055 void
3056netbeans_draw_multisign_indicator(int row)
3057{
3058 int i;
3059 int y;
3060 int x;
3061 GdkDrawable *drawable = gui.drawarea->window;
3062
3063 x = 0;
3064 y = row * gui.char_height + 2;
3065
3066 for (i = 0; i < gui.char_height - 3; i++)
3067 gdk_draw_point(drawable, gui.text_gc, x+2, y++);
3068
3069 gdk_draw_point(drawable, gui.text_gc, x+0, y);
3070 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3071 gdk_draw_point(drawable, gui.text_gc, x+4, y++);
3072 gdk_draw_point(drawable, gui.text_gc, x+1, y);
3073 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3074 gdk_draw_point(drawable, gui.text_gc, x+3, y++);
3075 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3076}
3077#endif /* FEAT_GUI_GTK */
3078
3079/*
3080 * If the mouse is clicked in the gutter of a line with multiple
3081 * annotations, cycle through the set of signs.
3082 */
3083 void
3084netbeans_gutter_click(linenr_T lnum)
3085{
3086 signlist_T *p;
3087
3088 for (p = curbuf->b_signlist; p != NULL; p = p->next)
3089 {
3090 if (p->lnum == lnum && p->next && p->next->lnum == lnum)
3091 {
3092 signlist_T *tail;
3093
3094 /* remove "p" from list, reinsert it at the tail of the sublist */
3095 if (p->prev)
3096 p->prev->next = p->next;
3097 else
3098 curbuf->b_signlist = p->next;
3099 p->next->prev = p->prev;
3100 /* now find end of sublist and insert p */
3101 for (tail = p->next;
3102 tail->next && tail->next->lnum == lnum
3103 && tail->next->id < GUARDEDOFFSET;
3104 tail = tail->next)
3105 ;
3106 /* tail now points to last entry with same lnum (except
3107 * that "guarded" annotations are always last) */
3108 p->next = tail->next;
3109 if (tail->next)
3110 tail->next->prev = p;
3111 p->prev = tail;
3112 tail->next = p;
3113 update_debug_sign(curbuf, lnum);
3114 break;
3115 }
3116 }
3117}
3118
3119
3120/*
3121 * Add a sign of the reqested type at the requested location.
3122 *
3123 * Reverse engineering:
3124 * Apparently an annotation is defined the first time it is used in a buffer.
3125 * When the same annotation is used in two buffers, the second time we do not
3126 * need to define a new sign name but reuse the existing one. But since the
3127 * ID number used in the second buffer starts counting at one again, a mapping
3128 * is made from the ID specifically for the buffer to the global sign name
3129 * (which is a number).
3130 *
3131 * globalsignmap[] stores the signs that have been defined globally.
3132 * buf->signmapused[] maps buffer-local annotation IDs to an index in
3133 * globalsignmap[].
3134 */
3135/*ARGSUSED*/
3136 static void
3137addsigntype(
3138 nbbuf_T *buf,
3139 int typeNum,
3140 char_u *typeName,
3141 char_u *tooltip,
3142 char_u *glyphFile,
3143 int use_fg,
3144 int fg,
3145 int use_bg,
3146 int bg)
3147{
3148 char fgbuf[32];
3149 char bgbuf[32];
3150 int i, j;
3151
3152 for (i = 0; i < globalsignmapused; i++)
3153 if (STRCMP(typeName, globalsignmap[i]) == 0)
3154 break;
3155
3156 if (i == globalsignmapused) /* not found; add it to global map */
3157 {
3158 nbdebug(("DEFINEANNOTYPE(%d,%s,%s,%s,%d,%d)\n",
3159 typeNum, typeName, tooltip, glyphFile, fg, bg));
3160 if (use_fg || use_bg)
3161 {
3162 sprintf(fgbuf, "guifg=#%06x", fg & 0xFFFFFF);
3163 sprintf(bgbuf, "guibg=#%06x", bg & 0xFFFFFF);
3164
3165 coloncmd(":highlight NB_%s %s %s", typeName, (use_fg) ? fgbuf : "",
3166 (use_bg) ? bgbuf : "");
3167 if (*glyphFile == NUL)
3168 /* no glyph, line highlighting only */
3169 coloncmd(":sign define %d linehl=NB_%s", i + 1, typeName);
3170 else if (vim_strsize(glyphFile) <= 2)
3171 /* one- or two-character glyph name, use as text glyph with
3172 * texthl */
3173 coloncmd(":sign define %d text=%s texthl=NB_%s", i + 1,
3174 glyphFile, typeName);
3175 else
3176 /* glyph, line highlighting */
3177 coloncmd(":sign define %d icon=%s linehl=NB_%s", i + 1,
3178 glyphFile, typeName);
3179 }
3180 else
3181 /* glyph, no line highlighting */
3182 coloncmd(":sign define %d icon=%s", i + 1, glyphFile);
3183
3184 if (STRCMP(typeName,"CurrentPC") == 0)
3185 curPCtype = typeNum;
3186
3187 if (globalsignmapused == globalsignmaplen)
3188 {
3189 if (globalsignmaplen == 0) /* first allocation */
3190 {
3191 globalsignmaplen = 20;
3192 globalsignmap = (char **)alloc_clear(globalsignmaplen*sizeof(char *));
3193 }
3194 else /* grow it */
3195 {
3196 int incr;
3197 int oldlen = globalsignmaplen;
3198
3199 globalsignmaplen *= 2;
3200 incr = globalsignmaplen - oldlen;
3201 globalsignmap = (char **)vim_realloc(globalsignmap,
3202 globalsignmaplen * sizeof(char *));
3203 memset(globalsignmap + oldlen, 0, incr * sizeof(char *));
3204 }
3205 }
3206
3207 globalsignmap[i] = (char *)typeName;
3208 globalsignmapused = i + 1;
3209 }
3210
3211 /* check local map; should *not* be found! */
3212 for (j = 0; j < buf->signmapused; j++)
3213 if (buf->signmap[j] == i + 1)
3214 return;
3215
3216 /* add to local map */
3217 if (buf->signmapused == buf->signmaplen)
3218 {
3219 if (buf->signmaplen == 0) /* first allocation */
3220 {
3221 buf->signmaplen = 5;
3222 buf->signmap = (int *)alloc_clear(buf->signmaplen * sizeof(int *));
3223 }
3224 else /* grow it */
3225 {
3226 int incr;
3227 int oldlen = buf->signmaplen;
3228 buf->signmaplen *= 2;
3229 incr = buf->signmaplen - oldlen;
3230 buf->signmap = (int *)vim_realloc(buf->signmap,
3231 buf->signmaplen*sizeof(int *));
3232 memset(buf->signmap + oldlen, 0, incr * sizeof(int *));
3233 }
3234 }
3235
3236 buf->signmap[buf->signmapused++] = i + 1;
3237
3238}
3239
3240
3241/*
3242 * See if we have the requested sign type in the buffer.
3243 */
3244 static int
3245mapsigntype(nbbuf_T *buf, int localsigntype)
3246{
3247 if (--localsigntype >= 0 && localsigntype < buf->signmapused)
3248 return buf->signmap[localsigntype];
3249
3250 return 0;
3251}
3252
3253
3254/*
3255 * Compute length of buffer, don't print anything.
3256 */
3257 static long
3258get_buf_size(buf_T *bufp)
3259{
3260 linenr_T lnum;
3261 long char_count = 0;
3262 int eol_size;
3263 long last_check = 100000L;
3264
3265 if (bufp->b_ml.ml_flags & ML_EMPTY)
3266 return 0;
3267 else
3268 {
3269 if (get_fileformat(bufp) == EOL_DOS)
3270 eol_size = 2;
3271 else
3272 eol_size = 1;
3273 for (lnum = 1; lnum <= bufp->b_ml.ml_line_count; ++lnum)
3274 {
3275 char_count += STRLEN(ml_get(lnum)) + eol_size;
3276 /* Check for a CTRL-C every 100000 characters */
3277 if (char_count > last_check)
3278 {
3279 ui_breakcheck();
3280 if (got_int)
3281 return char_count;
3282 last_check = char_count + 100000L;
3283 }
3284 }
3285 /* Correction for when last line doesn't have an EOL. */
3286 if (!bufp->b_p_eol && bufp->b_p_bin)
3287 char_count -= eol_size;
3288 }
3289
3290 return char_count;
3291}
3292
3293/*
3294 * Convert character offset to lnum,col
3295 */
3296 static pos_T *
3297off2pos(buf_T *buf, long offset)
3298{
3299 linenr_T lnum;
3300 static pos_T pos;
3301
3302 pos.lnum = 0;
3303 pos.col = 0;
3304#ifdef FEAT_VIRTUALEDIT
3305 pos.coladd = 0;
3306#endif
3307
3308 if (!(buf->b_ml.ml_flags & ML_EMPTY))
3309 {
3310 if ((lnum = ml_find_line_or_offset(buf, (linenr_T)0, &offset)) < 0)
3311 return NULL;
3312 pos.lnum = lnum;
3313 pos.col = offset;
3314 }
3315
3316 return &pos;
3317}
3318
3319/*
3320 * Convert an argument in the form "1234" to an offset and compute the
3321 * lnum/col from it. Convert an argument in the form "123/12" directly to a
3322 * lnum/col.
3323 * "argp" is advanced to after the argument.
3324 * Return a pointer to the position, NULL if something is wrong.
3325 */
3326 static pos_T *
3327get_off_or_lnum(buf_T *buf, char_u **argp)
3328{
3329 static pos_T mypos;
3330 long off;
3331
3332 off = strtol((char *)*argp, (char **)argp, 10);
3333 if (**argp == '/')
3334 {
3335 mypos.lnum = (linenr_T)off;
3336 ++*argp;
3337 mypos.col = strtol((char *)*argp, (char **)argp, 10);
3338#ifdef FEAT_VIRTUALEDIT
3339 mypos.coladd = 0;
3340#endif
3341 return &mypos;
3342 }
3343 return off2pos(buf, off);
3344}
3345
3346
3347/*
3348 * Convert lnum,col to character offset
3349 */
3350 static long
3351pos2off(buf_T *buf, pos_T *pos)
3352{
3353 long offset = 0;
3354
3355 if (!(buf->b_ml.ml_flags & ML_EMPTY))
3356 {
3357 if ((offset = ml_find_line_or_offset(buf, pos->lnum, 0)) < 0)
3358 return 0;
3359 offset += pos->col;
3360 }
3361
3362 return offset;
3363}
3364
3365
Bram Moolenaar009b2592004-10-24 19:18:58 +00003366/*
3367 * This message is printed after NetBeans opens a new file. Its
3368 * similar to the message readfile() uses, but since NetBeans
3369 * doesn't normally call readfile, we do our own.
3370 */
3371 static void
3372print_read_msg(buf)
3373 nbbuf_T *buf;
3374{
3375 int lnum = buf->bufp->b_ml.ml_line_count;
3376 long nchars = buf->bufp->b_orig_size;
3377 char_u c;
3378
3379 msg_add_fname(buf->bufp, buf->bufp->b_ffname);
3380 c = FALSE;
3381
3382 if (buf->bufp->b_p_ro)
3383 {
3384 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]"));
3385 c = TRUE;
3386 }
3387 if (!buf->bufp->b_start_eol)
3388 {
3389 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]") : _("[Incomplete last line]"));
3390 c = TRUE;
3391 }
3392 msg_add_lines(c, (long)lnum, nchars);
3393
3394 /* Now display it */
3395 vim_free(keep_msg);
3396 keep_msg = NULL;
3397 msg_scrolled_ign = TRUE;
3398 msg_trunc_attr(IObuff, FALSE, 0);
3399 msg_scrolled_ign = FALSE;
3400}
3401
3402
3403/*
3404 * Print a message after NetBeans writes the file. This message should be identical
3405 * to the standard message a non-netbeans user would see when writing a file.
3406 */
3407 static void
3408print_save_msg(buf, nchars)
3409 nbbuf_T *buf;
3410 long nchars;
3411{
3412 char_u c;
3413 char_u *p;
3414
3415 if (nchars >= 0)
3416 {
3417 msg_add_fname(buf->bufp, buf->bufp->b_ffname); /* fname in IObuff with quotes */
3418 c = FALSE;
3419
3420 msg_add_lines(c, buf->bufp->b_ml.ml_line_count,
3421 (long)buf->bufp->b_orig_size);
3422
3423 vim_free(keep_msg);
3424 keep_msg = NULL;
3425 msg_scrolled_ign = TRUE;
3426 p = msg_trunc_attr(IObuff, FALSE, 0);
3427 if ((msg_scrolled && !need_wait_return) || !buf->initDone)
3428 {
3429 /* Need to repeat the message after redrawing when:
3430 * - When reading from stdin (the screen will be cleared next).
3431 * - When restart_edit is set (otherwise there will be a delay
3432 * before redrawing).
3433 * - When the screen was scrolled but there is no wait-return
3434 * prompt. */
3435 set_keep_msg(p);
3436 keep_msg_attr = 0;
3437 }
3438 msg_scrolled_ign = FALSE;
3439 /* add_to_input_buf((char_u *)"\f", 1); */
3440 }
3441 else
3442 {
3443 char_u ebuf[BUFSIZ];
3444
3445 STRCPY(ebuf, (char_u *)_("E505: "));
3446 STRCAT(ebuf, IObuff);
3447 STRCAT(ebuf, (char_u *)_("is read-only (add ! to override)"));
3448 STRCPY(IObuff, ebuf);
3449 emsg(IObuff);
3450 }
3451}
3452
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453#endif /* defined(FEAT_NETBEANS_INTG) */