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