1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
4 * Netbeans integration by David Weatherford
5 * Adopted for Win32 by Sergey Khorev
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.
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.
18 * The Netbeans messages are received and queued in the gui event loop, or in
19 * the select loop when Vim runs in a terminal. These messages are processed
20 * by netbeans_parse_messages() which is invoked in the idle loop when Vim is
21 * waiting for user input. The function netbeans_parse_messages() is also
22 * called from the ":sleep" command, to allow the execution of test cases that
23 * may not invoke the idle loop.
28 #if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
30 /* TODO: when should this not be defined? */
33 /* Note: when making changes here also adjust configure.in. */
36 # include <tchar.h> /* for _T definition for TRACEn macros */
38 /* WinSock API is separated from C API, thus we can't use read(), write(),
40 # define SOCK_ERRNO errno = WSAGetLastError()
42 # define ECONNREFUSED WSAECONNREFUSED
46 # define EINTR WSAEINTR
47 # define sock_write(sd, buf, len) send(sd, buf, len, 0)
48 # define sock_read(sd, buf, len) recv(sd, buf, len, 0)
49 # define sock_close(sd) closesocket(sd)
50 # define sleep(t) Sleep(t*1000) /* WinAPI Sleep() accepts milliseconds */
54 # include <netinet/in.h>
59 # include <sys/socket.h>
64 # define sock_write(sd, buf, len) write(sd, buf, len)
65 # define sock_read(sd, buf, len) read(sd, buf, len)
66 # define sock_close(sd) close(sd)
71 #define GUARDED 10000 /* typenr for "guarded" annotation */
72 #define GUARDEDOFFSET 1000000 /* base for "guarded" sign id's */
73 #define MAX_COLOR_LENGTH 32 /* max length of color name in defineAnnoType */
75 /* The first implementation (working only with Netbeans) returned "1.1". The
76 * protocol implemented here also supports A-A-P. */
77 static char *ExtEdProtocolVersion = "2.5";
79 static long pos2off __ARGS((buf_T *, pos_T *));
80 static pos_T *off2pos __ARGS((buf_T *, long));
81 static pos_T *get_off_or_lnum __ARGS((buf_T *buf, char_u **argp));
82 static long get_buf_size __ARGS((buf_T *));
83 static int netbeans_keystring __ARGS((char_u *keystr));
84 static void postpone_keycommand __ARGS((char_u *keystr));
85 static void special_keys __ARGS((char_u *args));
87 static int netbeans_connect __ARGS((char *, int));
88 static int getConnInfo __ARGS((char *file, char **host, char **port, char **password));
90 static void nb_init_graphics __ARGS((void));
91 static void coloncmd __ARGS((char *cmd, ...));
92 static void nb_set_curbuf __ARGS((buf_T *buf));
94 static void messageFromNetbeans __ARGS((XtPointer, int *, XtInputId *));
97 static void messageFromNetbeans __ARGS((gpointer, gint, GdkInputCondition));
99 static void nb_parse_cmd __ARGS((char_u *));
100 static int nb_do_cmd __ARGS((int, char_u *, int, int, char_u *));
101 static void nb_send __ARGS((char *buf, char *fun));
102 static void nb_free __ARGS((void));
104 /* TRUE when netbeans is running with a GUI. */
106 # define NB_HAS_GUI (gui.in_use || gui.starting)
110 typedef __int64 NBSOCK;
115 static NBSOCK nbsock = -1; /* socket fd for Netbeans connection */
116 #define NETBEANS_OPEN (nbsock != -1)
119 static XtInputId inputHandler = (XtInputId)NULL; /* Cookie for input */
122 static gint inputHandler = 0; /* Cookie for input */
125 static int inputHandler = -1; /* simply ret.value of WSAAsyncSelect() */
126 extern HWND s_hwnd; /* Gvim's Window handle */
128 static int r_cmdno; /* current command number for reply */
129 static int dosetvisible = FALSE;
132 * Include the debugging code if wanted.
135 # include "nbdebug.c"
138 static int needupdate = 0;
139 static int inAtomic = 0;
142 * Close the socket and remove the input handlers.
145 nb_close_socket(void)
148 if (inputHandler != (XtInputId)NULL)
150 XtRemoveInput(inputHandler);
151 inputHandler = (XtInputId)NULL;
155 if (inputHandler != 0)
157 gdk_input_remove(inputHandler);
162 if (inputHandler == 0)
164 WSAAsyncSelect(nbsock, s_hwnd, 0, 0);
176 * Close the connection and cleanup.
177 * May be called when nb_close_socket() was called earlier.
184 netbeans_send_disconnect();
189 bevalServers &= ~BEVAL_NETBEANS;
196 /* remove all signs and update the screen after gutter removal */
197 coloncmd(":sign unplace *");
198 changed_window_setting();
199 update_screen(CLEAR);
206 gui_update_cursor(TRUE, FALSE);
212 #define NB_DEF_HOST "localhost"
213 #define NB_DEF_ADDR "3219"
214 #define NB_DEF_PASS "changeme"
217 netbeans_connect(char *params, int doabort)
220 struct sockaddr_in server;
221 struct hostent * host;
228 struct sockaddr_un server;
232 char *hostname = NULL;
233 char *address = NULL;
234 char *password = NULL;
240 /* "=fname": Read info from specified file. */
241 if (getConnInfo(params + 1, &hostname, &address, &password)
248 /* ":<host>:<addr>:<password>": get info from argument */
250 if (arg == NULL && (fname = getenv("__NETBEANS_CONINFO")) != NULL)
252 /* "": get info from file specified in environment */
253 if (getConnInfo(fname, &hostname, &address, &password) == FAIL)
260 /* ":<host>:<addr>:<password>": get info from argument */
262 address = strchr(hostname, ':');
266 password = strchr(address, ':');
267 if (password != NULL)
272 /* Get the missing values from the environment. */
273 if (hostname == NULL || *hostname == '\0')
274 hostname = getenv("__NETBEANS_HOST");
276 address = getenv("__NETBEANS_SOCKET");
277 if (password == NULL)
278 password = getenv("__NETBEANS_VIM_PASSWORD");
280 /* Move values to allocated memory. */
281 if (hostname != NULL)
282 hostname = (char *)vim_strsave((char_u *)hostname);
284 address = (char *)vim_strsave((char_u *)address);
285 if (password != NULL)
286 password = (char *)vim_strsave((char_u *)password);
290 /* Use the default when a value is missing. */
291 if (hostname == NULL || *hostname == '\0')
294 hostname = (char *)vim_strsave((char_u *)NB_DEF_HOST);
296 if (address == NULL || *address == '\0')
299 address = (char *)vim_strsave((char_u *)NB_DEF_ADDR);
301 if (password == NULL || *password == '\0')
304 password = (char *)vim_strsave((char_u *)NB_DEF_PASS);
306 if (hostname == NULL || address == NULL || password == NULL)
307 goto theend; /* out of memory */
310 netbeans_init_winsock();
314 port = atoi(address);
316 if ((sd = (NBSOCK)socket(AF_INET, SOCK_STREAM, 0)) == (NBSOCK)-1)
318 nbdebug(("error in socket() in netbeans_connect()\n"));
319 PERROR("socket() in netbeans_connect()");
323 /* Get the server internet address and put into addr structure */
324 /* fill in the socket address structure and connect to server */
325 vim_memset((char *)&server, '\0', sizeof(server));
326 server.sin_family = AF_INET;
327 server.sin_port = htons(port);
328 if ((host = gethostbyname(hostname)) == NULL)
330 nbdebug(("error in gethostbyname() in netbeans_connect()\n"));
331 PERROR("gethostbyname() in netbeans_connect()");
335 memcpy((char *)&server.sin_addr, host->h_addr, host->h_length);
337 if ((sd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
339 nbdebug(("error in socket() in netbeans_connect()\n"));
340 PERROR("socket() in netbeans_connect()");
344 server.sun_family = AF_UNIX;
345 strcpy(server.sun_path, address);
347 /* Connect to server */
348 if (connect(sd, (struct sockaddr *)&server, sizeof(server)))
351 nbdebug(("netbeans_connect: Connect failed with errno %d\n", errno));
352 if (errno == ECONNREFUSED)
356 if ((sd = (NBSOCK)socket(AF_INET, SOCK_STREAM, 0)) == (NBSOCK)-1)
359 nbdebug(("socket()#2 in netbeans_connect()\n"));
360 PERROR("socket()#2 in netbeans_connect()");
364 if ((sd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
367 nbdebug(("socket()#2 in netbeans_connect()\n"));
368 PERROR("socket()#2 in netbeans_connect()");
372 if (connect(sd, (struct sockaddr *)&server, sizeof(server)))
378 while (retries-- && ((errno == ECONNREFUSED)
379 || (errno == EINTR)))
381 nbdebug(("retrying...\n"));
382 mch_delay(3000L, TRUE);
389 if (connect(sd, (struct sockaddr *)&server,
390 sizeof(server)) == 0)
399 /* Get here when the server can't be found. */
400 nbdebug(("Cannot connect to Netbeans #2\n"));
401 PERROR(_("Cannot connect to Netbeans #2"));
411 nbdebug(("Cannot connect to Netbeans\n"));
412 PERROR(_("Cannot connect to Netbeans"));
421 vim_snprintf(buf, sizeof(buf), "AUTH %s\n", password);
422 nb_send(buf, "netbeans_connect");
424 sprintf(buf, "0:version=0 \"%s\"\n", ExtEdProtocolVersion);
425 nb_send(buf, "externaleditor_version");
431 return NETBEANS_OPEN ? OK : FAIL;
435 * Obtain the NetBeans hostname, port address and password from a file.
436 * Return the strings in allocated memory.
437 * Return FAIL if the file could not be read, OK otherwise (no matter what it
441 getConnInfo(char *file, char **host, char **port, char **auth)
451 * For Unix only accept the file when it's not accessible by others.
452 * The open will then fail if we don't own the file.
454 if (mch_stat(file, &st) == 0 && (st.st_mode & 0077) != 0)
456 nbdebug(("Wrong access mode for NetBeans connection info file: \"%s\"\n",
458 EMSG2(_("E668: Wrong access mode for NetBeans connection info file: \"%s\""),
464 fp = mch_fopen(file, "r");
467 nbdebug(("Cannot open NetBeans connection info file\n"));
468 PERROR("E660: Cannot open NetBeans connection info file");
472 /* Read the file. There should be one of each parameter */
473 while ((lp = (char_u *)fgets((char *)buf, BUFSIZ, fp)) != NULL)
475 if ((nl = vim_strchr(lp, '\n')) != NULL)
476 *nl = 0; /* strip off the trailing newline */
478 if (STRNCMP(lp, "host=", 5) == 0)
481 *host = (char *)vim_strsave(&buf[5]);
483 else if (STRNCMP(lp, "port=", 5) == 0)
486 *port = (char *)vim_strsave(&buf[5]);
488 else if (STRNCMP(lp, "auth=", 5) == 0)
491 *auth = (char *)vim_strsave(&buf[5]);
503 struct keyqueue *next;
504 struct keyqueue *prev;
507 typedef struct keyqueue keyQ_T;
509 static keyQ_T keyHead; /* dummy node, header for circular queue */
513 * Queue up key commands sent from netbeans.
514 * We store the string, because it may depend on the global mod_mask and
515 * :nbkey doesn't have a key number.
518 postpone_keycommand(char_u *keystr)
522 node = (keyQ_T *)alloc(sizeof(keyQ_T));
524 return; /* out of memory, drop the key */
526 if (keyHead.next == NULL) /* initialize circular queue */
528 keyHead.next = &keyHead;
529 keyHead.prev = &keyHead;
532 /* insert node at tail of queue */
533 node->next = &keyHead;
534 node->prev = keyHead.prev;
535 keyHead.prev->next = node;
538 node->keystr = vim_strsave(keystr);
542 * Handle any queued-up NetBeans keycommands to be send.
545 handle_key_queue(void)
547 int postponed = FALSE;
549 while (!postponed && keyHead.next && keyHead.next != &keyHead)
551 /* first, unlink the node */
552 keyQ_T *node = keyHead.next;
553 keyHead.next = node->next;
554 node->next->prev = node->prev;
556 /* Now, send the keycommand. This may cause it to be postponed again
557 * and change keyHead. */
558 if (node->keystr != NULL)
559 postponed = !netbeans_keystring(node->keystr);
560 vim_free(node->keystr);
562 /* Finally, dispose of the node */
571 struct cmdqueue *next;
572 struct cmdqueue *prev;
575 typedef struct cmdqueue queue_T;
577 static queue_T head; /* dummy node, header for circular queue */
581 * Put the buffer on the work queue; possibly save it to a file as well.
584 save(char_u *buf, int len)
588 node = (queue_T *)alloc(sizeof(queue_T));
590 return; /* out of memory */
591 node->buffer = alloc(len + 1);
592 if (node->buffer == NULL)
595 return; /* out of memory */
597 mch_memmove(node->buffer, buf, (size_t)len);
598 node->buffer[len] = NUL;
600 if (head.next == NULL) /* initialize circular queue */
606 /* insert node at tail of queue */
608 node->prev = head.prev;
609 head.prev->next = node;
614 static int outfd = -2;
616 /* possibly write buffer out to a file */
622 char *file = getenv("__NETBEANS_SAVE");
626 outfd = mch_open(file, O_WRONLY|O_CREAT|O_TRUNC, 0666);
630 write(outfd, buf, len);
637 * While there's still a command in the work queue, parse and execute it.
640 netbeans_parse_messages(void)
646 while (head.next != NULL && head.next != &head)
650 /* Locate the first line in the first buffer. */
651 p = vim_strchr(node->buffer, '\n');
654 /* Command isn't complete. If there is no following buffer,
655 * return (wait for more). If there is another buffer following,
656 * prepend the text to that buffer and delete this one. */
657 if (node->next == &head)
659 p = alloc((unsigned)(STRLEN(node->buffer)
660 + STRLEN(node->next->buffer) + 1));
662 return; /* out of memory */
663 STRCPY(p, node->buffer);
664 STRCAT(p, node->next->buffer);
665 vim_free(node->next->buffer);
666 node->next->buffer = p;
668 /* dispose of the node and buffer */
669 head.next = node->next;
670 node->next->prev = node->prev;
671 vim_free(node->buffer);
676 /* There is a complete command at the start of the buffer.
677 * Terminate it with a NUL. When no more text is following unlink
678 * the buffer. Do this before executing, because new buffers can
679 * be added while busy handling the command. */
684 head.next = node->next;
685 node->next->prev = node->prev;
690 /* now, parse and execute the commands */
691 nb_parse_cmd(node->buffer);
695 /* buffer finished, dispose of the node and buffer */
696 vim_free(node->buffer);
699 /* Check that "head" wasn't changed under our fingers, e.g. when a
700 * DETACH command was handled. */
701 else if (head.next == node)
703 /* more follows, move to the start */
704 STRMOVE(node->buffer, p);
710 /* Buffer size for reading incoming messages. */
711 #define MAXMSGSIZE 4096
714 * Read a command from netbeans.
718 messageFromNetbeans(XtPointer clientData UNUSED,
720 XtInputId *unused2 UNUSED)
728 messageFromNetbeans(gpointer clientData UNUSED,
730 GdkInputCondition unused2 UNUSED)
736 #define DETACH_MSG "DETACH\n"
741 static char_u *buf = NULL;
755 nbdebug(("messageFromNetbeans() called without a socket\n"));
759 /* Allocate a buffer to read into. */
762 buf = alloc(MAXMSGSIZE);
764 return; /* out of memory! */
767 /* Keep on reading for as long as there is something to read.
768 * Use select() or poll() to avoid blocking on a message that is exactly
769 * MAXMSGSIZE long. */
774 FD_SET(nbsock, &rfds);
777 if (select(nbsock + 1, &rfds, NULL, NULL, &tval) <= 0)
783 if (poll(&fds, 1, 0) <= 0)
787 len = sock_read(nbsock, buf, MAXMSGSIZE);
789 break; /* error or nothing more to read */
791 /* Store the read message in the queue. */
794 if (len < MAXMSGSIZE)
795 break; /* did read everything that's available */
798 /* Reading a socket disconnection (readlen == 0), or a socket error. */
801 /* Queue a "DETACH" netbeans message in the command queue in order to
802 * terminate the netbeans session later. Do not end the session here
803 * directly as we may be running in the context of a call to
804 * netbeans_parse_messages():
805 * netbeans_parse_messages
806 * -> autocmd triggered while processing the netbeans cmd
808 * -> gui event loop or select loop
811 save((char_u *)DETACH_MSG, (int)strlen(DETACH_MSG));
816 nbdebug(("read from Netbeans socket\n"));
817 PERROR(_("read from Netbeans socket"));
821 #if defined(NB_HAS_GUI) && defined(FEAT_GUI_GTK)
822 if (NB_HAS_GUI && gtk_main_level() > 0)
828 * Handle one NUL terminated command.
830 * format of a command from netbeans:
832 * 6:setTitle!84 "a.c"
841 * for function calls, the ! is replaced by a /
844 nb_parse_cmd(char_u *cmd)
851 if (STRCMP(cmd, "DISCONNECT") == 0)
853 /* We assume the server knows that we can safely exit! */
854 /* Disconnect before exiting, Motif hangs in a Select error
855 * message otherwise. */
861 if (STRCMP(cmd, "DETACH") == 0)
863 /* The IDE is breaking the connection. */
868 bufno = strtol((char *)cmd, &verb, 10);
872 nbdebug((" missing colon: %s\n", cmd));
873 EMSG2("E627: missing colon: %s", cmd);
876 ++verb; /* skip colon */
878 for (q = verb; *q; q++)
896 nbdebug((" missing ! or / in: %s\n", cmd));
897 EMSG2("E628: missing ! or / in: %s", cmd);
901 r_cmdno = strtol(q, &q, 10);
903 q = (char *)skipwhite((char_u *)q);
905 if (nb_do_cmd(bufno, (char_u *)verb, isfunc, r_cmdno, (char_u *)q) == FAIL)
909 * This happens because the ExtEd can send a command or 2 after
910 * doing a stopDocumentListen command. It doesn't harm anything
911 * so I'm disabling it except for debugging.
913 nbdebug(("nb_parse_cmd: Command error for \"%s\"\n", cmd));
914 EMSG("E629: bad return from nb_do_cmd");
922 unsigned int fireChanges:1;
923 unsigned int initDone:1;
924 unsigned int insertDone:1;
925 unsigned int modified:1;
933 typedef struct nbbuf_struct nbbuf_T;
935 static nbbuf_T *buf_list = NULL;
936 static int buf_list_size = 0; /* size of buf_list */
937 static int buf_list_used = 0; /* nr of entries in buf_list actually in use */
939 static char **globalsignmap = NULL;
940 static int globalsignmaplen = 0;
941 static int globalsignmapused = 0;
943 static int mapsigntype __ARGS((nbbuf_T *, int localsigntype));
944 static void addsigntype __ARGS((nbbuf_T *, int localsigntype, char_u *typeName,
945 char_u *tooltip, char_u *glyphfile,
946 char_u *fg, char_u *bg));
947 static void print_read_msg __ARGS((nbbuf_T *buf));
948 static void print_save_msg __ARGS((nbbuf_T *buf, off_t nchars));
950 static int curPCtype = -1;
953 * Free netbeans resources.
958 keyQ_T *key_node = keyHead.next;
959 queue_T *cmd_node = head.next;
963 /* free the netbeans buffer list */
964 for (i = 0; i < buf_list_used; i++)
967 vim_free(buf.displayname);
968 vim_free(buf.signmap);
969 if (buf.bufp != NULL)
971 buf.bufp->b_netbeans_file = FALSE;
972 buf.bufp->b_was_netbeans_file = FALSE;
980 /* free the queued key commands */
981 while(key_node != NULL && key_node != &keyHead)
983 keyQ_T *next = key_node->next;
984 vim_free(key_node->keystr);
986 if (next == &keyHead)
988 keyHead.next = &keyHead;
989 keyHead.prev = &keyHead;
995 /* free the queued netbeans commands */
996 while(cmd_node != NULL && cmd_node != &head)
998 queue_T *next = cmd_node->next;
999 vim_free(cmd_node->buffer);
1012 * Get the Netbeans buffer number for the specified buffer.
1015 nb_getbufno(buf_T *bufp)
1019 for (i = 0; i < buf_list_used; i++)
1020 if (buf_list[i].bufp == bufp)
1026 * Is this a NetBeans-owned buffer?
1029 isNetbeansBuffer(buf_T *bufp)
1031 return NETBEANS_OPEN && bufp->b_netbeans_file;
1035 * NetBeans and Vim have different undo models. In Vim, the file isn't
1036 * changed if changes are undone via the undo command. In NetBeans, once
1037 * a change has been made the file is marked as modified until saved. It
1038 * doesn't matter if the change was undone.
1040 * So this function is for the corner case where Vim thinks a buffer is
1041 * unmodified but NetBeans thinks it IS modified.
1044 isNetbeansModified(buf_T *bufp)
1046 if (isNetbeansBuffer(bufp))
1048 int bufno = nb_getbufno(bufp);
1051 return buf_list[bufno].modified;
1060 * Given a Netbeans buffer number, return the netbeans buffer.
1061 * Returns NULL for 0 or a negative number. A 0 bufno means a
1062 * non-buffer related command has been sent.
1065 nb_get_buf(int bufno)
1067 /* find or create a buffer with the given number */
1076 buf_list = (nbbuf_T *)alloc_clear(100 * sizeof(nbbuf_T));
1077 buf_list_size = 100;
1079 if (bufno >= buf_list_used) /* new */
1081 if (bufno >= buf_list_size) /* grow list */
1083 incr = bufno - buf_list_size + 90;
1084 buf_list_size += incr;
1085 buf_list = (nbbuf_T *)vim_realloc(
1086 buf_list, buf_list_size * sizeof(nbbuf_T));
1087 vim_memset(buf_list + buf_list_size - incr, 0,
1088 incr * sizeof(nbbuf_T));
1091 while (buf_list_used <= bufno)
1093 /* Default is to fire text changes. */
1094 buf_list[buf_list_used].fireChanges = 1;
1099 return buf_list + bufno;
1103 * Return the number of buffers that are modified.
1106 count_changed_buffers(void)
1112 for (bufp = firstbuf; bufp != NULL; bufp = bufp->b_next)
1113 if (bufp->b_changed)
1119 * End the netbeans session.
1125 static char buf[128];
1130 for (i = 0; i < buf_list_used; i++)
1132 if (!buf_list[i].bufp)
1134 if (netbeansForcedQuit)
1136 /* mark as unmodified so NetBeans won't put up dialog on "killed" */
1137 sprintf(buf, "%d:unmodified=%d\n", i, r_cmdno);
1138 nbdebug(("EVT: %s", buf));
1139 nb_send(buf, "netbeans_end");
1141 sprintf(buf, "%d:killed=%d\n", i, r_cmdno);
1142 nbdebug(("EVT: %s", buf));
1143 /* nb_send(buf, "netbeans_end"); avoid "write failed" messages */
1144 ignored = sock_write(nbsock, buf, (int)STRLEN(buf));
1149 * Send a message to netbeans.
1152 nb_send(char *buf, char *fun)
1154 /* Avoid giving pages full of error messages when the other side has
1155 * exited, only mention the first error until the connection works again. */
1156 static int did_error = FALSE;
1162 nbdebug((" %s(): write while not connected\n", fun));
1163 EMSG2("E630: %s(): write while not connected", fun);
1167 else if (sock_write(nbsock, buf, (int)STRLEN(buf)) != (int)STRLEN(buf))
1171 nbdebug((" %s(): write failed\n", fun));
1172 EMSG2("E631: %s(): write failed", fun);
1181 * Some input received from netbeans requires a response. This function
1182 * handles a response with no information (except the command number).
1185 nb_reply_nil(int cmdno)
1189 nbdebug(("REP %d: <none>\n", cmdno));
1191 /* Avoid printing an annoying error message. */
1195 sprintf(reply, "%d\n", cmdno);
1196 nb_send(reply, "nb_reply_nil");
1201 * Send a response with text.
1202 * "result" must have been quoted already (using nb_quote()).
1205 nb_reply_text(int cmdno, char_u *result)
1209 nbdebug(("REP %d: %s\n", cmdno, (char *)result));
1211 reply = alloc((unsigned)STRLEN(result) + 32);
1212 sprintf((char *)reply, "%d %s\n", cmdno, (char *)result);
1213 nb_send((char *)reply, "nb_reply_text");
1220 * Send a response with a number result code.
1223 nb_reply_nr(int cmdno, long result)
1227 nbdebug(("REP %d: %ld\n", cmdno, result));
1229 sprintf(reply, "%d %ld\n", cmdno, result);
1230 nb_send(reply, "nb_reply_nr");
1235 * Encode newline, ret, backslash, double quote for transmission to NetBeans.
1238 nb_quote(char_u *txt)
1240 char_u *buf = alloc((unsigned)(2 * STRLEN(txt) + 1));
1252 *q++ = '\\'; *q++ = *p; break;
1254 /* *q++ = '\\'; *q++ = 't'; break; */
1256 *q++ = '\\'; *q++ = 'n'; break;
1258 *q++ = '\\'; *q++ = 'r'; break;
1271 * Remove top level double quotes; convert backslashed chars.
1272 * Returns an allocated string (NULL for failure).
1273 * If "endp" is not NULL it is set to the character after the terminating
1277 nb_unquote(char_u *p, char_u **endp)
1283 /* result is never longer than input */
1284 result = (char *)alloc_clear((unsigned)STRLEN(p) + 1);
1290 nbdebug(("nb_unquote called with string that doesn't start with a quote!: %s\n",
1296 for (q = result; !done && *p != NUL;)
1302 * Unbackslashed dquote marks the end, if first char was dquote.
1311 case '\\': *q++ = '\\'; break;
1312 case 'n': *q++ = '\n'; break;
1313 case 't': *q++ = '\t'; break;
1314 case 'r': *q++ = '\r'; break;
1315 case '"': *q++ = '"'; break;
1316 case NUL: --p; break;
1317 /* default: skip over illegal chars */
1334 * Remove from "first" byte to "last" byte (inclusive), at line "lnum" of the
1335 * current buffer. Remove to end of line when "last" is MAXCOL.
1338 nb_partialremove(linenr_T lnum, colnr_T first, colnr_T last)
1340 char_u *oldtext, *newtext;
1342 int lastbyte = last;
1344 oldtext = ml_get(lnum);
1345 oldlen = (int)STRLEN(oldtext);
1346 if (first >= (colnr_T)oldlen || oldlen == 0) /* just in case */
1348 if (lastbyte >= oldlen)
1349 lastbyte = oldlen - 1;
1350 newtext = alloc(oldlen - (int)(lastbyte - first));
1351 if (newtext != NULL)
1353 mch_memmove(newtext, oldtext, first);
1354 STRMOVE(newtext + first, oldtext + lastbyte + 1);
1355 nbdebug((" NEW LINE %d: %s\n", lnum, newtext));
1356 ml_replace(lnum, newtext, FALSE);
1361 * Replace the "first" line with the concatenation of the "first" and
1362 * the "other" line. The "other" line is not removed.
1365 nb_joinlines(linenr_T first, linenr_T other)
1367 int len_first, len_other;
1370 len_first = (int)STRLEN(ml_get(first));
1371 len_other = (int)STRLEN(ml_get(other));
1372 p = alloc((unsigned)(len_first + len_other + 1));
1375 mch_memmove(p, ml_get(first), len_first);
1376 mch_memmove(p + len_first, ml_get(other), len_other + 1);
1377 ml_replace(first, p, FALSE);
1382 #define streq(a,b) (strcmp(a,b) == 0)
1385 * Do the actual processing of a single netbeans command or function.
1386 * The difference between a command and function is that a function
1387 * gets a response (it's required) but a command does not.
1388 * For arguments see comment for nb_parse_cmd().
1396 char_u *args) /* points to space before arguments or NUL */
1400 nbbuf_T *buf = nb_get_buf(bufno);
1401 static int skip = 0;
1403 char *cp; /* for when a char pointer is needed */
1405 nbdebug(("%s %d: (%d) %s %s\n", (func) ? "FUN" : "CMD", cmdno, bufno, cmd,
1406 STRCMP(cmd, "insert") == 0 ? "<text>" : (char *)args));
1410 /* =====================================================================*/
1411 if (streq((char *)cmd, "getModified"))
1413 if (buf == NULL || buf->bufp == NULL)
1414 /* Return the number of buffers that are modified. */
1415 nb_reply_nr(cmdno, (long)count_changed_buffers());
1417 /* Return whether the buffer is modified. */
1418 nb_reply_nr(cmdno, (long)(buf->bufp->b_changed
1419 || isNetbeansModified(buf->bufp)));
1420 /* =====================================================================*/
1422 else if (streq((char *)cmd, "saveAndExit"))
1424 /* Note: this will exit Vim if successful. */
1425 coloncmd(":confirm qall");
1427 /* We didn't exit: return the number of changed buffers. */
1428 nb_reply_nr(cmdno, (long)count_changed_buffers());
1429 /* =====================================================================*/
1431 else if (streq((char *)cmd, "getCursor"))
1435 /* Note: nb_getbufno() may return -1. This indicates the IDE
1436 * didn't assign a number to the current buffer in response to a
1437 * fileOpened event. */
1438 sprintf((char *)text, "%d %ld %d %ld",
1439 nb_getbufno(curbuf),
1440 (long)curwin->w_cursor.lnum,
1441 (int)curwin->w_cursor.col,
1442 pos2off(curbuf, &curwin->w_cursor));
1443 nb_reply_text(cmdno, text);
1444 /* =====================================================================*/
1446 else if (streq((char *)cmd, "getAnno"))
1450 if (buf == NULL || buf->bufp == NULL)
1452 nbdebug((" Invalid buffer identifier in getAnno\n"));
1453 EMSG("E652: Invalid buffer identifier in getAnno");
1461 serNum = strtol(cp, &cp, 10);
1462 /* If the sign isn't found linenum will be zero. */
1463 linenum = (long)buf_findsign(buf->bufp, serNum);
1466 nb_reply_nr(cmdno, linenum);
1467 /* =====================================================================*/
1469 else if (streq((char *)cmd, "getLength"))
1473 if (buf == NULL || buf->bufp == NULL)
1475 nbdebug((" invalid buffer identifier in getLength\n"));
1476 EMSG("E632: invalid buffer identifier in getLength");
1481 len = get_buf_size(buf->bufp);
1483 nb_reply_nr(cmdno, len);
1484 /* =====================================================================*/
1486 else if (streq((char *)cmd, "getText"))
1490 char_u *text = NULL;
1495 if (buf == NULL || buf->bufp == NULL)
1497 nbdebug((" invalid buffer identifier in getText\n"));
1498 EMSG("E633: invalid buffer identifier in getText");
1503 len = get_buf_size(buf->bufp);
1504 nlines = buf->bufp->b_ml.ml_line_count;
1505 text = alloc((unsigned)((len > 0)
1506 ? ((len + nlines) * 2) : 4));
1509 nbdebug((" nb_do_cmd: getText has null text field\n"));
1516 for (; lno <= nlines ; lno++)
1518 line = nb_quote(ml_get_buf(buf->bufp, lno, FALSE));
1533 nb_reply_text(cmdno, (char_u *)"");
1536 nb_reply_text(cmdno, text);
1539 /* =====================================================================*/
1541 else if (streq((char *)cmd, "remove"))
1547 linenr_T del_from_lnum, del_to_lnum; /* lines to be deleted as a whole */
1548 int oldFire = netbeansFireChanges;
1549 int oldSuppress = netbeansSuppressNoLines;
1552 if (skip >= SKIP_STOP)
1554 nbdebug((" Skipping %s command\n", (char *) cmd));
1555 nb_reply_nil(cmdno);
1559 if (buf == NULL || buf->bufp == NULL)
1561 nbdebug((" invalid buffer identifier in remove\n"));
1562 EMSG("E634: invalid buffer identifier in remove");
1567 netbeansFireChanges = FALSE;
1568 netbeansSuppressNoLines = TRUE;
1570 nb_set_curbuf(buf->bufp);
1571 wasChanged = buf->bufp->b_changed;
1573 off = strtol(cp, &cp, 10);
1574 count = strtol(cp, &cp, 10);
1575 args = (char_u *)cp;
1576 /* delete "count" chars, starting at "off" */
1577 pos = off2pos(buf->bufp, off);
1580 nbdebug((" !bad position\n"));
1581 nb_reply_text(cmdno, (char_u *)"!bad position");
1582 netbeansFireChanges = oldFire;
1583 netbeansSuppressNoLines = oldSuppress;
1587 nbdebug((" FIRST POS: line %d, col %d\n",
1588 first.lnum, first.col));
1589 pos = off2pos(buf->bufp, off+count-1);
1592 nbdebug((" !bad count\n"));
1593 nb_reply_text(cmdno, (char_u *)"!bad count");
1594 netbeansFireChanges = oldFire;
1595 netbeansSuppressNoLines = oldSuppress;
1599 nbdebug((" LAST POS: line %d, col %d\n",
1600 last.lnum, last.col));
1601 del_from_lnum = first.lnum;
1602 del_to_lnum = last.lnum;
1605 /* Get the position of the first byte after the deleted
1606 * section. "next" is NULL when deleting to the end of the
1608 next = off2pos(buf->bufp, off + count);
1610 /* Remove part of the first line. */
1612 || (next != NULL && first.lnum == next->lnum))
1614 if (first.lnum != last.lnum
1615 || (next != NULL && first.lnum != next->lnum))
1617 /* remove to the end of the first line */
1618 nb_partialremove(first.lnum, first.col,
1620 if (first.lnum == last.lnum)
1622 /* Partial line to remove includes the end of
1623 * line. Join the line with the next one, have
1624 * the next line deleted below. */
1625 nb_joinlines(first.lnum, next->lnum);
1626 del_to_lnum = next->lnum;
1631 /* remove within one line */
1632 nb_partialremove(first.lnum, first.col, last.col);
1634 ++del_from_lnum; /* don't delete the first line */
1637 /* Remove part of the last line. */
1638 if (first.lnum != last.lnum && next != NULL
1639 && next->col != 0 && last.lnum == next->lnum)
1641 nb_partialremove(last.lnum, 0, last.col);
1642 if (del_from_lnum > first.lnum)
1644 /* Join end of last line to start of first line; last
1645 * line is deleted below. */
1646 nb_joinlines(first.lnum, last.lnum);
1649 /* First line is deleted as a whole, keep the last
1654 /* First is partial line; last line to remove includes
1655 * the end of line; join first line to line following last
1656 * line; line following last line is deleted below. */
1657 if (first.lnum != last.lnum && del_from_lnum > first.lnum
1658 && next != NULL && last.lnum != next->lnum)
1660 nb_joinlines(first.lnum, next->lnum);
1661 del_to_lnum = next->lnum;
1664 /* Delete whole lines if there are any. */
1665 if (del_to_lnum >= del_from_lnum)
1669 /* delete signs from the lines being deleted */
1670 for (i = del_from_lnum; i <= del_to_lnum; i++)
1672 int id = buf_findsign_id(buf->bufp, (linenr_T)i);
1675 nbdebug((" Deleting sign %d on line %d\n",
1677 buf_delsign(buf->bufp, id);
1681 nbdebug((" No sign on line %d\n", i));
1685 nbdebug((" Deleting lines %d through %d\n",
1686 del_from_lnum, del_to_lnum));
1687 curwin->w_cursor.lnum = del_from_lnum;
1688 curwin->w_cursor.col = 0;
1689 del_lines(del_to_lnum - del_from_lnum + 1, FALSE);
1692 /* Leave cursor at first deleted byte. */
1693 curwin->w_cursor = first;
1694 check_cursor_lnum();
1695 buf->bufp->b_changed = wasChanged; /* logically unchanged */
1696 netbeansFireChanges = oldFire;
1697 netbeansSuppressNoLines = oldSuppress;
1699 u_blockfree(buf->bufp);
1700 u_clearall(buf->bufp);
1702 nb_reply_nil(cmdno);
1703 /* =====================================================================*/
1705 else if (streq((char *)cmd, "insert"))
1709 if (skip >= SKIP_STOP)
1711 nbdebug((" Skipping %s command\n", (char *) cmd));
1712 nb_reply_nil(cmdno);
1718 off = strtol(cp, &cp, 10);
1719 args = (char_u *)cp;
1721 /* get text to be inserted */
1722 args = skipwhite(args);
1723 args = to_free = (char_u *)nb_unquote(args, NULL);
1725 nbdebug((" CHUNK[%d]: %d bytes at offset %d\n",
1726 buf->bufp->b_ml.ml_line_count, STRLEN(args), off));
1729 if (buf == NULL || buf->bufp == NULL)
1731 nbdebug((" invalid buffer identifier in insert\n"));
1732 EMSG("E635: invalid buffer identifier in insert");
1735 else if (args != NULL)
1737 int ff_detected = EOL_UNKNOWN;
1738 int buf_was_empty = (buf->bufp->b_ml.ml_flags & ML_EMPTY);
1741 int oldFire = netbeansFireChanges;
1745 linenr_T lnum_start;
1748 netbeansFireChanges = 0;
1750 /* Jump to the buffer where we insert. After this "curbuf"
1752 nb_set_curbuf(buf->bufp);
1753 old_b_changed = curbuf->b_changed;
1755 /* Convert the specified character offset into a lnum/col
1757 pos = off2pos(curbuf, off);
1763 lnum_start = pos->lnum;
1767 /* If the given position is not found, assume we want
1768 * the end of the file. See setLocAndSize HACK. */
1770 lnum_start = 1; /* above empty line */
1772 lnum_start = curbuf->b_ml.ml_line_count + 1;
1775 /* "lnum" is the line where we insert: either append to it or
1776 * insert a new line above it. */
1779 /* Loop over the "\n" separated lines of the argument. */
1781 while (*args != NUL)
1783 nl = vim_strchr(args, '\n');
1786 /* Incomplete line, probably truncated. Next "insert"
1787 * command should append to this one. */
1795 * We need to detect EOL style, because the commands
1796 * use a character offset.
1798 if (nl > args && nl[-1] == '\r')
1800 ff_detected = EOL_DOS;
1804 ff_detected = EOL_UNIX;
1808 if (lnum == lnum_start
1809 && ((pos != NULL && pos->col > 0)
1810 || (lnum == 1 && buf_was_empty)))
1812 char_u *oldline = ml_get(lnum);
1815 /* Insert halfway a line. For simplicity we assume we
1816 * need to append to the line. */
1817 newline = alloc_check((unsigned)(STRLEN(oldline) + len + 1));
1818 if (newline != NULL)
1820 STRCPY(newline, oldline);
1821 STRCAT(newline, args);
1822 ml_replace(lnum, newline, FALSE);
1827 /* Append a new line. Not that we always do this,
1828 * also when the text doesn't end in a "\n". */
1829 ml_append((linenr_T)(lnum - 1), args, (colnr_T)(len + 1), FALSE);
1839 /* Adjust the marks below the inserted lines. */
1840 appended_lines_mark(lnum_start - 1, (long)added);
1843 * When starting with an empty buffer set the fileformat.
1844 * This is just guessing...
1848 if (ff_detected == EOL_UNKNOWN)
1849 #if defined(MSDOS) || defined(MSWIN) || defined(OS2)
1850 ff_detected = EOL_DOS;
1852 ff_detected = EOL_UNIX;
1854 set_fileformat(ff_detected, OPT_LOCAL);
1855 curbuf->b_start_ffc = *curbuf->b_p_ff;
1859 * XXX - GRP - Is the next line right? If I've inserted
1860 * text the buffer has been updated but not written. Will
1861 * netbeans guarantee to write it? Even if I do a :q! ?
1863 curbuf->b_changed = old_b_changed; /* logically unchanged */
1864 netbeansFireChanges = oldFire;
1866 /* Undo info is invalid now... */
1867 u_blockfree(curbuf);
1871 nb_reply_nil(cmdno); /* or !error */
1875 nbdebug(("UNIMPLEMENTED FUNCTION: %s\n", cmd));
1876 nb_reply_nil(cmdno);
1880 else /* Not a function; no reply required. */
1882 /* =====================================================================*/
1883 if (streq((char *)cmd, "create"))
1885 /* Create a buffer without a name. */
1888 nbdebug((" invalid buffer identifier in create\n"));
1889 EMSG("E636: invalid buffer identifier in create");
1892 vim_free(buf->displayname);
1893 buf->displayname = NULL;
1895 netbeansReadFile = 0; /* don't try to open disk file */
1896 do_ecmd(0, NULL, 0, 0, ECMD_ONE, ECMD_HIDE + ECMD_OLDBUF, curwin);
1897 netbeansReadFile = 1;
1900 buf->insertDone = FALSE;
1901 #if defined(FEAT_MENU) && defined(FEAT_GUI)
1903 gui_update_menus(0);
1905 /* =====================================================================*/
1907 else if (streq((char *)cmd, "insertDone"))
1909 if (buf == NULL || buf->bufp == NULL)
1911 nbdebug((" invalid buffer identifier in insertDone\n"));
1915 buf->bufp->b_start_eol = *args == 'T';
1916 buf->insertDone = TRUE;
1918 buf->bufp->b_p_ro = *args == 'T';
1919 print_read_msg(buf);
1921 /* =====================================================================*/
1923 else if (streq((char *)cmd, "saveDone"))
1925 long savedChars = atol((char *)args);
1927 if (buf == NULL || buf->bufp == NULL)
1929 nbdebug((" invalid buffer identifier in saveDone\n"));
1932 print_save_msg(buf, savedChars);
1933 /* =====================================================================*/
1935 else if (streq((char *)cmd, "startDocumentListen"))
1939 nbdebug((" invalid buffer identifier in startDocumentListen\n"));
1940 EMSG("E637: invalid buffer identifier in startDocumentListen");
1943 buf->fireChanges = 1;
1944 /* =====================================================================*/
1946 else if (streq((char *)cmd, "stopDocumentListen"))
1950 nbdebug((" invalid buffer identifier in stopDocumentListen\n"));
1951 EMSG("E638: invalid buffer identifier in stopDocumentListen");
1954 buf->fireChanges = 0;
1955 if (buf->bufp != NULL && buf->bufp->b_was_netbeans_file)
1957 if (!buf->bufp->b_netbeans_file)
1959 nbdebug(("E658: NetBeans connection lost for buffer %ld\n", buf->bufp->b_fnum));
1960 EMSGN(_("E658: NetBeans connection lost for buffer %ld"),
1965 /* NetBeans uses stopDocumentListen when it stops editing
1966 * a file. It then expects the buffer in Vim to
1968 do_bufdel(DOBUF_DEL, (char_u *)"", 1,
1969 buf->bufp->b_fnum, buf->bufp->b_fnum, TRUE);
1970 vim_memset(buf, 0, sizeof(nbbuf_T));
1973 /* =====================================================================*/
1975 else if (streq((char *)cmd, "setTitle"))
1979 nbdebug((" invalid buffer identifier in setTitle\n"));
1980 EMSG("E639: invalid buffer identifier in setTitle");
1983 vim_free(buf->displayname);
1984 buf->displayname = nb_unquote(args, NULL);
1985 /* =====================================================================*/
1987 else if (streq((char *)cmd, "initDone"))
1989 if (buf == NULL || buf->bufp == NULL)
1991 nbdebug((" invalid buffer identifier in initDone\n"));
1992 EMSG("E640: invalid buffer identifier in initDone");
1996 buf->initDone = TRUE;
1997 nb_set_curbuf(buf->bufp);
1998 #if defined(FEAT_AUTOCMD)
1999 apply_autocmds(EVENT_BUFREADPOST, 0, 0, FALSE, buf->bufp);
2002 /* handle any postponed key commands */
2004 /* =====================================================================*/
2006 else if (streq((char *)cmd, "setBufferNumber")
2007 || streq((char *)cmd, "putBufferNumber"))
2014 nbdebug((" invalid buffer identifier in setBufferNumber\n"));
2015 EMSG("E641: invalid buffer identifier in setBufferNumber");
2018 path = (char_u *)nb_unquote(args, NULL);
2021 bufp = buflist_findname(path);
2025 nbdebug((" File %s not found in setBufferNumber\n", args));
2026 EMSG2("E642: File %s not found in setBufferNumber", args);
2030 buf->nbbuf_number = bufp->b_fnum;
2032 /* "setBufferNumber" has the side effect of jumping to the buffer
2033 * (don't know why!). Don't do that for "putBufferNumber". */
2035 coloncmd(":buffer %d", bufp->b_fnum);
2038 buf->initDone = TRUE;
2040 /* handle any postponed key commands */
2044 /* =====================================================================*/
2046 else if (streq((char *)cmd, "setFullName"))
2050 nbdebug((" invalid buffer identifier in setFullName\n"));
2051 EMSG("E643: invalid buffer identifier in setFullName");
2054 vim_free(buf->displayname);
2055 buf->displayname = nb_unquote(args, NULL);
2057 netbeansReadFile = 0; /* don't try to open disk file */
2058 do_ecmd(0, (char_u *)buf->displayname, 0, 0, ECMD_ONE,
2059 ECMD_HIDE + ECMD_OLDBUF, curwin);
2060 netbeansReadFile = 1;
2063 #if defined(FEAT_MENU) && defined(FEAT_GUI)
2065 gui_update_menus(0);
2067 /* =====================================================================*/
2069 else if (streq((char *)cmd, "editFile"))
2073 nbdebug((" invalid buffer identifier in editFile\n"));
2074 EMSG("E644: invalid buffer identifier in editFile");
2077 /* Edit a file: like create + setFullName + read the file. */
2078 vim_free(buf->displayname);
2079 buf->displayname = nb_unquote(args, NULL);
2080 do_ecmd(0, (char_u *)buf->displayname, NULL, NULL, ECMD_ONE,
2081 ECMD_HIDE + ECMD_OLDBUF, curwin);
2083 buf->initDone = TRUE;
2085 #if defined(FEAT_TITLE)
2088 #if defined(FEAT_MENU) && defined(FEAT_GUI)
2090 gui_update_menus(0);
2092 /* =====================================================================*/
2094 else if (streq((char *)cmd, "setVisible"))
2096 if (buf == NULL || buf->bufp == NULL)
2098 nbdebug((" invalid buffer identifier in setVisible\n"));
2099 /* This message was commented out, probably because it can
2100 * happen when shutting down. */
2102 EMSG("E645: invalid buffer identifier in setVisible");
2105 if (streq((char *)args, "T") && buf->bufp != curbuf)
2108 exarg.cmd = (char_u *)"goto";
2109 exarg.forceit = FALSE;
2110 dosetvisible = TRUE;
2111 goto_buffer(&exarg, DOBUF_FIRST, FORWARD, buf->bufp->b_fnum);
2113 dosetvisible = FALSE;
2116 /* Side effect!!!. */
2118 gui_mch_set_foreground();
2121 /* =====================================================================*/
2123 else if (streq((char *)cmd, "raise"))
2126 /* Bring gvim to the foreground. */
2128 gui_mch_set_foreground();
2130 /* =====================================================================*/
2132 else if (streq((char *)cmd, "setModified"))
2136 if (buf == NULL || buf->bufp == NULL)
2138 nbdebug((" invalid buffer identifier in setModified\n"));
2139 /* This message was commented out, probably because it can
2140 * happen when shutting down. */
2142 EMSG("E646: invalid buffer identifier in setModified");
2145 prev_b_changed = buf->bufp->b_changed;
2146 if (streq((char *)args, "T"))
2147 buf->bufp->b_changed = TRUE;
2152 /* Assume NetBeans stored the file. Reset the timestamp to
2153 * avoid "file changed" warnings. */
2154 if (buf->bufp->b_ffname != NULL
2155 && mch_stat((char *)buf->bufp->b_ffname, &st) >= 0)
2156 buf_store_time(buf->bufp, &st, buf->bufp->b_ffname);
2157 buf->bufp->b_changed = FALSE;
2159 buf->modified = buf->bufp->b_changed;
2160 if (prev_b_changed != buf->bufp->b_changed)
2163 check_status(buf->bufp);
2164 redraw_tabline = TRUE;
2171 /* =====================================================================*/
2173 else if (streq((char *)cmd, "setModtime"))
2175 if (buf == NULL || buf->bufp == NULL)
2176 nbdebug((" invalid buffer identifier in setModtime\n"));
2178 buf->bufp->b_mtime = atoi((char *)args);
2179 /* =====================================================================*/
2181 else if (streq((char *)cmd, "setReadOnly"))
2183 if (buf == NULL || buf->bufp == NULL)
2184 nbdebug((" invalid buffer identifier in setReadOnly\n"));
2185 else if (streq((char *)args, "T"))
2186 buf->bufp->b_p_ro = TRUE;
2188 buf->bufp->b_p_ro = FALSE;
2189 /* =====================================================================*/
2191 else if (streq((char *)cmd, "setMark"))
2194 /* =====================================================================*/
2196 else if (streq((char *)cmd, "showBalloon"))
2198 #if defined(FEAT_BEVAL)
2199 static char *text = NULL;
2202 * Set up the Balloon Expression Evaluation area.
2203 * Ignore 'ballooneval' here.
2204 * The text pointer must remain valid for a while.
2206 if (balloonEval != NULL)
2209 text = nb_unquote(args, NULL);
2211 gui_mch_post_balloon(balloonEval, (char_u *)text);
2214 /* =====================================================================*/
2216 else if (streq((char *)cmd, "setDot"))
2223 if (buf == NULL || buf->bufp == NULL)
2225 nbdebug((" invalid buffer identifier in setDot\n"));
2226 EMSG("E647: invalid buffer identifier in setDot");
2230 nb_set_curbuf(buf->bufp);
2233 /* Don't want Visual mode now. */
2240 pos = get_off_or_lnum(buf->bufp, &args);
2243 curwin->w_cursor = *pos;
2251 nbdebug((" BAD POSITION in setDot: %s\n", s));
2254 /* gui_update_cursor(TRUE, FALSE); */
2255 /* update_curbuf(NOT_VALID); */
2256 update_topline(); /* scroll to show the line */
2257 update_screen(VALID);
2264 gui_update_cursor(TRUE, FALSE);
2268 /* Quit a hit-return or more prompt. */
2269 if (State == HITRETURN || State == ASKMORE)
2272 if (gui.in_use && gtk_main_level() > 0)
2276 /* =====================================================================*/
2278 else if (streq((char *)cmd, "close"))
2281 char *name = "<NONE>";
2286 nbdebug((" invalid buffer identifier in close\n"));
2287 EMSG("E648: invalid buffer identifier in close");
2292 if (buf->displayname != NULL)
2293 name = buf->displayname;
2295 if (buf->bufp == NULL)
2297 nbdebug((" invalid buffer identifier in close\n"));
2298 /* This message was commented out, probably because it can
2299 * happen when shutting down. */
2301 EMSG("E649: invalid buffer identifier in close");
2303 nbdebug((" CLOSE %d: %s\n", bufno, name));
2305 need_mouse_correct = TRUE;
2307 if (buf->bufp != NULL)
2308 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD,
2309 buf->bufp->b_fnum, TRUE);
2311 buf->initDone = FALSE;
2313 /* =====================================================================*/
2315 else if (streq((char *)cmd, "setStyle")) /* obsolete... */
2317 nbdebug((" setStyle is obsolete!\n"));
2318 /* =====================================================================*/
2320 else if (streq((char *)cmd, "setExitDelay"))
2322 /* Only used in version 2.1. */
2323 /* =====================================================================*/
2325 else if (streq((char *)cmd, "defineAnnoType"))
2333 int parse_error = FALSE;
2339 nbdebug((" invalid buffer identifier in defineAnnoType\n"));
2340 EMSG("E650: invalid buffer identifier in defineAnnoType");
2345 typeNum = strtol(cp, &cp, 10);
2346 args = (char_u *)cp;
2347 args = skipwhite(args);
2348 typeName = (char_u *)nb_unquote(args, &args);
2349 args = skipwhite(args + 1);
2350 tooltip = (char_u *)nb_unquote(args, &args);
2351 args = skipwhite(args + 1);
2353 p = (char_u *)nb_unquote(args, &args);
2354 glyphFile = vim_strsave_escaped(p, escape_chars);
2357 args = skipwhite(args + 1);
2358 p = skiptowhite(args);
2362 p = skipwhite(p + 1);
2364 fg = vim_strsave(args);
2365 bg = vim_strsave(p);
2366 if (STRLEN(fg) > MAX_COLOR_LENGTH || STRLEN(bg) > MAX_COLOR_LENGTH)
2368 EMSG("E532: highlighting color name too long in defineAnnoType");
2372 else if (typeName != NULL && tooltip != NULL && glyphFile != NULL)
2373 addsigntype(buf, typeNum, typeName, tooltip, glyphFile, fg, bg);
2377 /* don't free typeName; it's used directly in addsigntype() */
2381 vim_free(glyphFile);
2386 /* =====================================================================*/
2388 else if (streq((char *)cmd, "addAnno"))
2396 if (buf == NULL || buf->bufp == NULL)
2398 nbdebug((" invalid buffer identifier in addAnno\n"));
2399 EMSG("E651: invalid buffer identifier in addAnno");
2406 serNum = strtol(cp, &cp, 10);
2408 /* Get the typenr specific for this buffer and convert it to
2409 * the global typenumber, as used for the sign name. */
2410 localTypeNum = strtol(cp, &cp, 10);
2411 args = (char_u *)cp;
2412 typeNum = mapsigntype(buf, localTypeNum);
2414 pos = get_off_or_lnum(buf->bufp, &args);
2417 ignored = (int)strtol(cp, &cp, 10);
2418 args = (char_u *)cp;
2422 nbdebug((" partial line annotation -- Not Yet Implemented!\n"));
2425 if (serNum >= GUARDEDOFFSET)
2427 nbdebug((" too many annotations! ignoring...\n"));
2432 coloncmd(":sign place %d line=%ld name=%d buffer=%d",
2433 serNum, pos->lnum, typeNum, buf->bufp->b_fnum);
2434 if (typeNum == curPCtype)
2435 coloncmd(":sign jump %d buffer=%d", serNum,
2439 /* =====================================================================*/
2441 else if (streq((char *)cmd, "removeAnno"))
2446 if (buf == NULL || buf->bufp == NULL)
2448 nbdebug((" invalid buffer identifier in removeAnno\n"));
2453 serNum = strtol(cp, &cp, 10);
2454 args = (char_u *)cp;
2455 coloncmd(":sign unplace %d buffer=%d",
2456 serNum, buf->bufp->b_fnum);
2457 redraw_buf_later(buf->bufp, NOT_VALID);
2459 /* =====================================================================*/
2461 else if (streq((char *)cmd, "moveAnnoToFront"))
2464 nbdebug((" moveAnnoToFront: Not Yet Implemented!\n"));
2466 /* =====================================================================*/
2468 else if (streq((char *)cmd, "guard") || streq((char *)cmd, "unguard"))
2474 int un = (cmd[0] == 'u');
2475 static int guardId = GUARDEDOFFSET;
2477 if (skip >= SKIP_STOP)
2479 nbdebug((" Skipping %s command\n", (char *) cmd));
2485 if (buf == NULL || buf->bufp == NULL)
2487 nbdebug((" invalid buffer identifier in %s command\n", cmd));
2490 nb_set_curbuf(buf->bufp);
2492 off = strtol(cp, &cp, 10);
2493 len = strtol(cp, NULL, 10);
2494 args = (char_u *)cp;
2495 pos = off2pos(buf->bufp, off);
2498 nbdebug((" no such start pos in %s, %ld\n", cmd, off));
2502 pos = off2pos(buf->bufp, off + len - 1);
2503 if (pos != NULL && pos->col == 0)
2506 * In Java Swing the offset is a position between 2
2507 * characters. If col == 0 then we really want the
2508 * previous line as the end.
2510 pos = off2pos(buf->bufp, off + len - 2);
2513 nbdebug((" no such end pos in %s, %ld\n",
2514 cmd, off + len - 1));
2519 /* set highlight for region */
2520 nbdebug((" %sGUARD %ld,%d to %ld,%d\n", (un) ? "UN" : "",
2521 first.lnum, first.col,
2522 last.lnum, last.col));
2524 for (lnum = first.lnum; lnum <= last.lnum; lnum++)
2532 if (buf_findsigntype_id(buf->bufp, lnum,
2536 ":sign place %d line=%ld name=%d buffer=%d",
2537 guardId++, lnum, GUARDED,
2543 redraw_buf_later(buf->bufp, NOT_VALID);
2546 /* =====================================================================*/
2548 else if (streq((char *)cmd, "startAtomic"))
2551 /* =====================================================================*/
2553 else if (streq((char *)cmd, "endAtomic"))
2561 /* =====================================================================*/
2563 else if (streq((char *)cmd, "save"))
2566 * NOTE - This command is obsolete wrt NetBeans. Its left in
2567 * only for historical reasons.
2569 if (buf == NULL || buf->bufp == NULL)
2571 nbdebug((" invalid buffer identifier in %s command\n", cmd));
2575 /* the following is taken from ex_cmds.c (do_wqall function) */
2576 if (bufIsChanged(buf->bufp))
2578 /* Only write if the buffer can be written. */
2580 && !buf->bufp->b_p_ro
2581 && buf->bufp->b_ffname != NULL
2582 #ifdef FEAT_QUICKFIX
2583 && !bt_dontwrite(buf->bufp)
2587 buf_write_all(buf->bufp, FALSE);
2589 /* an autocommand may have deleted the buffer */
2590 if (!buf_valid(buf->bufp))
2597 nbdebug((" Buffer has no changes!\n"));
2599 /* =====================================================================*/
2601 else if (streq((char *)cmd, "netbeansBuffer"))
2603 if (buf == NULL || buf->bufp == NULL)
2605 nbdebug((" invalid buffer identifier in %s command\n", cmd));
2610 buf->bufp->b_netbeans_file = TRUE;
2611 buf->bufp->b_was_netbeans_file = TRUE;
2614 buf->bufp->b_netbeans_file = FALSE;
2615 /* =====================================================================*/
2617 else if (streq((char *)cmd, "specialKeys"))
2620 /* =====================================================================*/
2622 else if (streq((char *)cmd, "actionMenuItem"))
2625 /* =====================================================================*/
2627 else if (streq((char *)cmd, "version"))
2633 nbdebug(("Unrecognised command: %s\n", cmd));
2636 * Unrecognized command is ignored.
2639 if (inAtomic && doupdate)
2646 * Is this needed? I moved the netbeans_Xt_connect() later during startup
2647 * and it may no longer be necessary. If its not needed then needupdate
2648 * and doupdate can also be removed.
2650 if (buf != NULL && buf->initDone && doupdate)
2652 update_screen(NOT_VALID);
2659 gui_update_cursor(TRUE, FALSE);
2663 /* Quit a hit-return or more prompt. */
2664 if (State == HITRETURN || State == ASKMORE)
2667 if (gui.in_use && gtk_main_level() > 0)
2678 * If "buf" is not the current buffer try changing to a window that edits this
2679 * buffer. If there is no such window then close the current buffer and set
2680 * the current buffer as "buf".
2683 nb_set_curbuf(buf_T *buf)
2685 if (curbuf != buf && buf_jump_open_win(buf) == NULL)
2686 set_curbuf(buf, DOBUF_GOTO);
2690 * Process a vim colon command.
2693 coloncmd(char *cmd, ...)
2699 vim_vsnprintf(buf, sizeof(buf), cmd, ap, NULL);
2702 nbdebug((" COLONCMD %s\n", buf));
2704 /* ALT_INPUT_LOCK_ON; */
2705 do_cmdline((char_u *)buf, NULL, NULL, DOCMD_NOWAIT | DOCMD_KEYTYPED);
2706 /* ALT_INPUT_LOCK_OFF; */
2708 setcursor(); /* restore the cursor position */
2709 out_flush(); /* make sure output has been written */
2714 gui_update_cursor(TRUE, FALSE);
2722 * Parse the specialKeys argument and issue the appropriate map commands.
2725 special_keys(char_u *args)
2727 char *save_str = nb_unquote(args, NULL);
2728 char *tok = strtok(save_str, " ");
2737 if ((sep = strchr(tok, '-')) != NULL)
2757 strcpy(&keybuf[i], tok);
2758 vim_snprintf(cmdbuf, sizeof(cmdbuf),
2759 "<silent><%s> :nbkey %s<CR>", keybuf, keybuf);
2760 do_map(0, (char_u *)cmdbuf, NORMAL, FALSE);
2761 tok = strtok(NULL, " ");
2768 exarg_T *eap UNUSED;
2777 (void)netbeans_keystring(eap->arg);
2785 # if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
2786 && !defined(FEAT_GUI_W32)
2789 EMSG(_("E838: netbeans is not supported with this GUI"));
2794 netbeans_open((char *)eap->arg, FALSE);
2798 * Initialize highlights and signs for use by netbeans (mostly obsolete)
2801 nb_init_graphics(void)
2803 static int did_init = FALSE;
2807 coloncmd(":highlight NBGuarded guibg=Cyan guifg=Black"
2808 " ctermbg=LightCyan ctermfg=Black");
2809 coloncmd(":sign define %d linehl=NBGuarded", GUARDED);
2816 * Convert key to netbeans name. This uses the global "mod_mask".
2819 netbeans_keyname(int key, char *buf)
2827 if (mod_mask & MOD_MASK_CTRL)
2829 if (mod_mask & MOD_MASK_SHIFT)
2831 if (mod_mask & MOD_MASK_ALT)
2837 case K_F1: name = "F1"; break;
2838 case K_S_F1: name = "F1"; shift = 1; break;
2839 case K_F2: name = "F2"; break;
2840 case K_S_F2: name = "F2"; shift = 1; break;
2841 case K_F3: name = "F3"; break;
2842 case K_S_F3: name = "F3"; shift = 1; break;
2843 case K_F4: name = "F4"; break;
2844 case K_S_F4: name = "F4"; shift = 1; break;
2845 case K_F5: name = "F5"; break;
2846 case K_S_F5: name = "F5"; shift = 1; break;
2847 case K_F6: name = "F6"; break;
2848 case K_S_F6: name = "F6"; shift = 1; break;
2849 case K_F7: name = "F7"; break;
2850 case K_S_F7: name = "F7"; shift = 1; break;
2851 case K_F8: name = "F8"; break;
2852 case K_S_F8: name = "F8"; shift = 1; break;
2853 case K_F9: name = "F9"; break;
2854 case K_S_F9: name = "F9"; shift = 1; break;
2855 case K_F10: name = "F10"; break;
2856 case K_S_F10: name = "F10"; shift = 1; break;
2857 case K_F11: name = "F11"; break;
2858 case K_S_F11: name = "F11"; shift = 1; break;
2859 case K_F12: name = "F12"; break;
2860 case K_S_F12: name = "F12"; shift = 1; break;
2862 if (key >= ' ' && key <= '~')
2864 /* Allow ASCII characters. */
2880 strcat(buf, "M"); /* META */
2881 if (ctrl || shift || alt)
2886 #if defined(FEAT_BEVAL) || defined(PROTO)
2888 * Function to be called for balloon evaluation. Grabs the text under the
2889 * cursor and sends it to the debugger for evaluation. The debugger should
2890 * respond with a showBalloon command when there is a useful result.
2904 /* Don't do anything when 'ballooneval' is off, messages scrolled the
2905 * windows up or we have no connection. */
2906 if (!p_beval || msg_scrolled > 0 || !NETBEANS_OPEN)
2909 if (get_beval_info(beval, TRUE, &wp, &lnum, &text, &col) == OK)
2911 /* Send debugger request. Only when the text is of reasonable
2913 if (text != NULL && text[0] != NUL && STRLEN(text) < MAXPATHL)
2915 buf = (char *)alloc(MAXPATHL * 2 + 25);
2921 vim_snprintf(buf, MAXPATHL * 2 + 25,
2922 "0:balloonText=%d \"%s\"\n", r_cmdno, p);
2925 nbdebug(("EVT: %s", buf));
2926 nb_send(buf, "netbeans_beval_cb");
2936 * Return TRUE when the netbeans connection is closed.
2939 netbeans_active(void)
2941 return NETBEANS_OPEN;
2945 * Return netbeans file descriptor.
2948 netbeans_filedesc(void)
2953 #if defined(FEAT_GUI) || defined(PROTO)
2955 * Register our file descriptor with the gui event handling system.
2958 netbeans_gui_register(void)
2960 if (!NB_HAS_GUI || !NETBEANS_OPEN)
2963 # ifdef FEAT_GUI_X11
2964 /* tell notifier we are interested in being called
2965 * when there is input on the editor connection socket
2967 if (inputHandler == (XtInputId)NULL)
2968 inputHandler = XtAppAddInput((XtAppContext)app_context, nbsock,
2969 (XtPointer)(XtInputReadMask + XtInputExceptMask),
2970 messageFromNetbeans, NULL);
2972 # ifdef FEAT_GUI_GTK
2974 * Tell gdk we are interested in being called when there
2975 * is input on the editor connection socket
2977 if (inputHandler == 0)
2978 inputHandler = gdk_input_add((gint)nbsock, (GdkInputCondition)
2979 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
2980 messageFromNetbeans, NULL);
2982 # ifdef FEAT_GUI_W32
2984 * Tell Windows we are interested in receiving message when there
2985 * is input on the editor connection socket
2987 if (inputHandler == -1)
2988 inputHandler = WSAAsyncSelect(nbsock, s_hwnd, WM_NETBEANS, FD_READ);
2994 bevalServers |= BEVAL_NETBEANS;
3000 * Tell netbeans that the window was opened, ready for commands.
3003 netbeans_open(char *params, int doabort)
3005 char *cmd = "0:startupDone=0\n";
3009 EMSG(_("E511: netbeans already connected"));
3013 if (netbeans_connect(params, doabort) != OK)
3016 netbeans_gui_register();
3019 nbdebug(("EVT: %s", cmd));
3020 nb_send(cmd, "netbeans_startup_done");
3022 /* update the screen after having added the gutter */
3023 changed_window_setting();
3024 update_screen(CLEAR);
3031 gui_update_cursor(TRUE, FALSE);
3038 * Tell netbeans that we're exiting. This should be called right
3039 * before calling exit.
3042 netbeans_send_disconnect()
3048 sprintf(buf, "0:disconnect=%d\n", r_cmdno);
3049 nbdebug(("EVT: %s", buf));
3050 nb_send(buf, "netbeans_disconnect");
3054 #if defined(FEAT_GUI_X11) || defined(FEAT_GUI_W32) || defined(PROTO)
3056 * Tell netbeans that the window was moved or resized.
3059 netbeans_frame_moved(int new_x, int new_y)
3066 sprintf(buf, "0:geometry=%d %d %d %d %d\n",
3067 r_cmdno, (int)Columns, (int)Rows, new_x, new_y);
3068 /*nbdebug(("EVT: %s", buf)); happens too many times during a move */
3069 nb_send(buf, "netbeans_frame_moved");
3074 * Tell netbeans the user opened or activated a file.
3077 netbeans_file_activated(buf_T *bufp)
3079 int bufno = nb_getbufno(bufp);
3080 nbbuf_T *bp = nb_get_buf(bufno);
3081 char buffer[2*MAXPATHL];
3084 if (!NETBEANS_OPEN || !bufp->b_netbeans_file || dosetvisible)
3087 q = nb_quote(bufp->b_ffname);
3088 if (q == NULL || bp == NULL)
3091 vim_snprintf(buffer, sizeof(buffer), "%d:fileOpened=%d \"%s\" %s %s\n",
3095 "T", /* open in NetBeans */
3096 "F"); /* modified */
3099 nbdebug(("EVT: %s", buffer));
3101 nb_send(buffer, "netbeans_file_opened");
3105 * Tell netbeans the user opened a file.
3108 netbeans_file_opened(buf_T *bufp)
3110 int bufno = nb_getbufno(bufp);
3111 char buffer[2*MAXPATHL];
3113 nbbuf_T *bp = nb_get_buf(nb_getbufno(bufp));
3119 q = nb_quote(bufp->b_ffname);
3127 vim_snprintf(buffer, sizeof(buffer), "%d:fileOpened=%d \"%s\" %s %s\n",
3131 "T", /* open in NetBeans */
3132 "F"); /* modified */
3135 nbdebug(("EVT: %s", buffer));
3137 nb_send(buffer, "netbeans_file_opened");
3138 if (p_acd && vim_chdirfile(bufp->b_ffname) == OK)
3139 shorten_fnames(TRUE);
3143 * Tell netbeans that a file was deleted or wiped out.
3146 netbeans_file_killed(buf_T *bufp)
3148 int bufno = nb_getbufno(bufp);
3149 nbbuf_T *nbbuf = nb_get_buf(bufno);
3150 char buffer[2*MAXPATHL];
3152 if (!NETBEANS_OPEN || bufno == -1)
3155 nbdebug(("netbeans_file_killed:\n"));
3156 nbdebug((" Killing bufno: %d", bufno));
3158 sprintf(buffer, "%d:killed=%d\n", bufno, r_cmdno);
3160 nbdebug(("EVT: %s", buffer));
3162 nb_send(buffer, "netbeans_file_killed");
3169 * Get a pointer to the Netbeans buffer for Vim buffer "bufp".
3170 * Return NULL if there is no such buffer or changes are not to be reported.
3171 * Otherwise store the buffer number in "*bufnop".
3174 nb_bufp2nbbuf_fire(buf_T *bufp, int *bufnop)
3179 if (!NETBEANS_OPEN || !netbeansFireChanges)
3180 return NULL; /* changes are not reported at all */
3182 bufno = nb_getbufno(bufp);
3184 return NULL; /* file is not known to NetBeans */
3186 nbbuf = nb_get_buf(bufno);
3187 if (nbbuf != NULL && !nbbuf->fireChanges)
3188 return NULL; /* changes in this buffer are not reported */
3195 * Tell netbeans the user inserted some text.
3216 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3220 /* Don't mark as modified for initial read */
3221 if (nbbuf->insertDone)
3222 nbbuf->modified = 1;
3226 off = pos2off(bufp, &pos);
3228 /* send the "insert" EVT */
3229 newtxt = alloc(newlen + 1);
3230 vim_strncpy(newtxt, txt, newlen);
3231 p = nb_quote(newtxt);
3234 buf = alloc(128 + 2*newlen);
3235 sprintf((char *)buf, "%d:insert=%d %ld \"%s\"\n",
3236 bufno, r_cmdno, off, p);
3237 nbdebug(("EVT: %s", buf));
3238 nb_send((char *)buf, "netbeans_inserted");
3246 * Tell netbeans some bytes have been removed.
3264 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3270 nbdebug(("Negative len %ld in netbeans_removed()!\n", len));
3274 nbbuf->modified = 1;
3279 off = pos2off(bufp, &pos);
3281 sprintf((char *)buf, "%d:remove=%d %ld %ld\n", bufno, r_cmdno, off, len);
3282 nbdebug(("EVT: %s", buf));
3283 nb_send((char *)buf, "netbeans_removed");
3287 * Send netbeans an unmodified command.
3290 netbeans_unmodified(buf_T *bufp UNUSED)
3292 /* This is a no-op, because NetBeans considers a buffer modified
3293 * even when all changes have been undone. */
3297 * Send a button release event back to netbeans. Its up to netbeans
3298 * to decide what to do (if anything) with this event.
3301 netbeans_button_release(int button)
3309 bufno = nb_getbufno(curbuf);
3311 if (bufno >= 0 && curwin != NULL && curwin->w_buffer == curbuf)
3313 int col = mouse_col - W_WINCOL(curwin)
3314 - ((curwin->w_p_nu || curwin->w_p_rnu) ? 9 : 1);
3315 long off = pos2off(curbuf, &curwin->w_cursor);
3317 /* sync the cursor position */
3318 sprintf(buf, "%d:newDotAndMark=%d %ld %ld\n", bufno, r_cmdno, off, off);
3319 nbdebug(("EVT: %s", buf));
3320 nb_send(buf, "netbeans_button_release[newDotAndMark]");
3322 sprintf(buf, "%d:buttonRelease=%d %d %ld %d\n", bufno, r_cmdno,
3323 button, (long)curwin->w_cursor.lnum, col);
3324 nbdebug(("EVT: %s", buf));
3325 nb_send(buf, "netbeans_button_release");
3331 * Send a keypress event back to netbeans. This usually simulates some
3332 * kind of function key press. This function operates on a key code.
3333 * Return TRUE when the key was sent, FALSE when the command has been
3337 netbeans_keycommand(int key)
3341 netbeans_keyname(key, keyName);
3342 return netbeans_keystring((char_u *)keyName);
3347 * Send a keypress event back to netbeans. This usually simulates some
3348 * kind of function key press. This function operates on a key string.
3349 * Return TRUE when the key was sent, FALSE when the command has been
3353 netbeans_keystring(char_u *keyName)
3355 char buf[2*MAXPATHL];
3356 int bufno = nb_getbufno(curbuf);
3365 nbdebug(("got keycommand for non-NetBeans buffer, opening...\n"));
3366 q = curbuf->b_ffname == NULL ? (char_u *)""
3367 : nb_quote(curbuf->b_ffname);
3370 vim_snprintf(buf, sizeof(buf), "0:fileOpened=%d \"%s\" %s %s\n", 0,
3372 "T", /* open in NetBeans */
3373 "F"); /* modified */
3374 if (curbuf->b_ffname != NULL)
3376 nbdebug(("EVT: %s", buf));
3377 nb_send(buf, "netbeans_keycommand");
3379 postpone_keycommand(keyName);
3383 /* sync the cursor position */
3384 off = pos2off(curbuf, &curwin->w_cursor);
3385 sprintf(buf, "%d:newDotAndMark=%d %ld %ld\n", bufno, r_cmdno, off, off);
3386 nbdebug(("EVT: %s", buf));
3387 nb_send(buf, "netbeans_keycommand");
3389 /* To work on Win32 you must apply patch to ExtEditor module
3390 * from ExtEdCaret.java.diff - make EVT_newDotAndMark handler
3394 /* now send keyCommand event */
3395 vim_snprintf(buf, sizeof(buf), "%d:keyCommand=%d \"%s\"\n",
3396 bufno, r_cmdno, keyName);
3397 nbdebug(("EVT: %s", buf));
3398 nb_send(buf, "netbeans_keycommand");
3400 /* New: do both at once and include the lnum/col. */
3401 vim_snprintf(buf, sizeof(buf), "%d:keyAtPos=%d \"%s\" %ld %ld/%ld\n",
3402 bufno, r_cmdno, keyName,
3403 off, (long)curwin->w_cursor.lnum, (long)curwin->w_cursor.col);
3404 nbdebug(("EVT: %s", buf));
3405 nb_send(buf, "netbeans_keycommand");
3411 * Send a save event to netbeans.
3414 netbeans_save_buffer(buf_T *bufp)
3423 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3427 nbbuf->modified = 0;
3429 sprintf((char *)buf, "%d:save=%d\n", bufno, r_cmdno);
3430 nbdebug(("EVT: %s", buf));
3431 nb_send((char *)buf, "netbeans_save_buffer");
3436 * Send remove command to netbeans (this command has been turned off).
3439 netbeans_deleted_all_lines(buf_T *bufp)
3448 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3452 /* Don't mark as modified for initial read */
3453 if (nbbuf->insertDone)
3454 nbbuf->modified = 1;
3456 sprintf((char *)buf, "%d:remove=%d 0 -1\n", bufno, r_cmdno);
3457 nbdebug(("EVT(suppressed): %s", buf));
3458 /* nb_send(buf, "netbeans_deleted_all_lines"); */
3463 * See if the lines are guarded. The top and bot parameters are from
3464 * u_savecommon(), these are the line above the change and the line below the
3468 netbeans_is_guarded(linenr_T top, linenr_T bot)
3476 for (p = curbuf->b_signlist; p != NULL; p = p->next)
3477 if (p->id >= GUARDEDOFFSET)
3478 for (lnum = top + 1; lnum < bot; lnum++)
3479 if (lnum == p->lnum)
3485 #if defined(FEAT_GUI_X11) || defined(PROTO)
3487 * We have multiple signs to draw at the same location. Draw the
3488 * multi-sign indicator instead. This is the Motif version.
3491 netbeans_draw_multisign_indicator(int row)
3501 y = row * gui.char_height + 2;
3503 for (i = 0; i < gui.char_height - 3; i++)
3504 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y++);
3506 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+0, y);
3507 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3508 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+4, y++);
3509 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+1, y);
3510 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3511 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+3, y++);
3512 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3514 #endif /* FEAT_GUI_X11 */
3516 #if defined(FEAT_GUI_GTK) && !defined(PROTO)
3518 * We have multiple signs to draw at the same location. Draw the
3519 * multi-sign indicator instead. This is the GTK/Gnome version.
3522 netbeans_draw_multisign_indicator(int row)
3527 GdkDrawable *drawable = gui.drawarea->window;
3533 y = row * gui.char_height + 2;
3535 for (i = 0; i < gui.char_height - 3; i++)
3536 gdk_draw_point(drawable, gui.text_gc, x+2, y++);
3538 gdk_draw_point(drawable, gui.text_gc, x+0, y);
3539 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3540 gdk_draw_point(drawable, gui.text_gc, x+4, y++);
3541 gdk_draw_point(drawable, gui.text_gc, x+1, y);
3542 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3543 gdk_draw_point(drawable, gui.text_gc, x+3, y++);
3544 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3546 #endif /* FEAT_GUI_GTK */
3549 * If the mouse is clicked in the gutter of a line with multiple
3550 * annotations, cycle through the set of signs.
3553 netbeans_gutter_click(linenr_T lnum)
3560 for (p = curbuf->b_signlist; p != NULL; p = p->next)
3562 if (p->lnum == lnum && p->next && p->next->lnum == lnum)
3566 /* remove "p" from list, reinsert it at the tail of the sublist */
3568 p->prev->next = p->next;
3570 curbuf->b_signlist = p->next;
3571 p->next->prev = p->prev;
3572 /* now find end of sublist and insert p */
3573 for (tail = p->next;
3574 tail->next && tail->next->lnum == lnum
3575 && tail->next->id < GUARDEDOFFSET;
3578 /* tail now points to last entry with same lnum (except
3579 * that "guarded" annotations are always last) */
3580 p->next = tail->next;
3582 tail->next->prev = p;
3585 update_debug_sign(curbuf, lnum);
3592 * Add a sign of the requested type at the requested location.
3594 * Reverse engineering:
3595 * Apparently an annotation is defined the first time it is used in a buffer.
3596 * When the same annotation is used in two buffers, the second time we do not
3597 * need to define a new sign name but reuse the existing one. But since the
3598 * ID number used in the second buffer starts counting at one again, a mapping
3599 * is made from the ID specifically for the buffer to the global sign name
3600 * (which is a number).
3602 * globalsignmap[] stores the signs that have been defined globally.
3603 * buf->signmapused[] maps buffer-local annotation IDs to an index in
3611 char_u *tooltip UNUSED,
3617 int use_fg = (*fg && STRCMP(fg, "none") != 0);
3618 int use_bg = (*bg && STRCMP(bg, "none") != 0);
3620 for (i = 0; i < globalsignmapused; i++)
3621 if (STRCMP(typeName, globalsignmap[i]) == 0)
3624 if (i == globalsignmapused) /* not found; add it to global map */
3626 nbdebug(("DEFINEANNOTYPE(%d,%s,%s,%s,%s,%s)\n",
3627 typeNum, typeName, tooltip, glyphFile, fg, bg));
3628 if (use_fg || use_bg)
3630 char fgbuf[2 * (8 + MAX_COLOR_LENGTH) + 1];
3631 char bgbuf[2 * (8 + MAX_COLOR_LENGTH) + 1];
3635 value = strtol((char *)fg, &ptr, 10);
3636 if (ptr != (char *)fg)
3637 sprintf(fgbuf, "guifg=#%06x", value & 0xFFFFFF);
3639 sprintf(fgbuf, "guifg=%s ctermfg=%s", fg, fg);
3641 value = strtol((char *)bg, &ptr, 10);
3642 if (ptr != (char *)bg)
3643 sprintf(bgbuf, "guibg=#%06x", value & 0xFFFFFF);
3645 sprintf(bgbuf, "guibg=%s ctermbg=%s", bg, bg);
3647 coloncmd(":highlight NB_%s %s %s", typeName, (use_fg) ? fgbuf : "",
3648 (use_bg) ? bgbuf : "");
3649 if (*glyphFile == NUL)
3650 /* no glyph, line highlighting only */
3651 coloncmd(":sign define %d linehl=NB_%s", i + 1, typeName);
3652 else if (vim_strsize(glyphFile) <= 2)
3653 /* one- or two-character glyph name, use as text glyph with
3655 coloncmd(":sign define %d text=%s texthl=NB_%s", i + 1,
3656 glyphFile, typeName);
3658 /* glyph, line highlighting */
3659 coloncmd(":sign define %d icon=%s linehl=NB_%s", i + 1,
3660 glyphFile, typeName);
3663 /* glyph, no line highlighting */
3664 coloncmd(":sign define %d icon=%s", i + 1, glyphFile);
3666 if (STRCMP(typeName,"CurrentPC") == 0)
3667 curPCtype = typeNum;
3669 if (globalsignmapused == globalsignmaplen)
3671 if (globalsignmaplen == 0) /* first allocation */
3673 globalsignmaplen = 20;
3674 globalsignmap = (char **)alloc_clear(globalsignmaplen*sizeof(char *));
3679 int oldlen = globalsignmaplen;
3681 globalsignmaplen *= 2;
3682 incr = globalsignmaplen - oldlen;
3683 globalsignmap = (char **)vim_realloc(globalsignmap,
3684 globalsignmaplen * sizeof(char *));
3685 vim_memset(globalsignmap + oldlen, 0, incr * sizeof(char *));
3689 globalsignmap[i] = (char *)typeName;
3690 globalsignmapused = i + 1;
3693 /* check local map; should *not* be found! */
3694 for (j = 0; j < buf->signmapused; j++)
3695 if (buf->signmap[j] == i + 1)
3698 /* add to local map */
3699 if (buf->signmapused == buf->signmaplen)
3701 if (buf->signmaplen == 0) /* first allocation */
3703 buf->signmaplen = 5;
3704 buf->signmap = (int *)alloc_clear(buf->signmaplen * sizeof(int));
3709 int oldlen = buf->signmaplen;
3711 buf->signmaplen *= 2;
3712 incr = buf->signmaplen - oldlen;
3713 buf->signmap = (int *)vim_realloc(buf->signmap,
3714 buf->signmaplen * sizeof(int));
3715 vim_memset(buf->signmap + oldlen, 0, incr * sizeof(int));
3719 buf->signmap[buf->signmapused++] = i + 1;
3725 * See if we have the requested sign type in the buffer.
3728 mapsigntype(nbbuf_T *buf, int localsigntype)
3730 if (--localsigntype >= 0 && localsigntype < buf->signmapused)
3731 return buf->signmap[localsigntype];
3738 * Compute length of buffer, don't print anything.
3741 get_buf_size(buf_T *bufp)
3744 long char_count = 0;
3746 long last_check = 100000L;
3748 if (bufp->b_ml.ml_flags & ML_EMPTY)
3752 if (get_fileformat(bufp) == EOL_DOS)
3756 for (lnum = 1; lnum <= bufp->b_ml.ml_line_count; ++lnum)
3758 char_count += (long)STRLEN(ml_get_buf(bufp, lnum, FALSE))
3760 /* Check for a CTRL-C every 100000 characters */
3761 if (char_count > last_check)
3766 last_check = char_count + 100000L;
3769 /* Correction for when last line doesn't have an EOL. */
3770 if (!bufp->b_p_eol && bufp->b_p_bin)
3771 char_count -= eol_size;
3778 * Convert character offset to lnum,col
3781 off2pos(buf_T *buf, long offset)
3788 #ifdef FEAT_VIRTUALEDIT
3792 if (!(buf->b_ml.ml_flags & ML_EMPTY))
3794 if ((lnum = ml_find_line_or_offset(buf, (linenr_T)0, &offset)) < 0)
3804 * Convert an argument in the form "1234" to an offset and compute the
3805 * lnum/col from it. Convert an argument in the form "123/12" directly to a
3807 * "argp" is advanced to after the argument.
3808 * Return a pointer to the position, NULL if something is wrong.
3811 get_off_or_lnum(buf_T *buf, char_u **argp)
3816 off = strtol((char *)*argp, (char **)argp, 10);
3819 mypos.lnum = (linenr_T)off;
3821 mypos.col = strtol((char *)*argp, (char **)argp, 10);
3822 #ifdef FEAT_VIRTUALEDIT
3827 return off2pos(buf, off);
3832 * Convert (lnum,col) to byte offset in the file.
3835 pos2off(buf_T *buf, pos_T *pos)
3839 if (!(buf->b_ml.ml_flags & ML_EMPTY))
3841 if ((offset = ml_find_line_or_offset(buf, pos->lnum, 0)) < 0)
3851 * This message is printed after NetBeans opens a new file. Its
3852 * similar to the message readfile() uses, but since NetBeans
3853 * doesn't normally call readfile, we do our own.
3859 int lnum = buf->bufp->b_ml.ml_line_count;
3860 off_t nchars = buf->bufp->b_orig_size;
3863 msg_add_fname(buf->bufp, buf->bufp->b_ffname);
3866 if (buf->bufp->b_p_ro)
3868 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]"));
3871 if (!buf->bufp->b_start_eol)
3873 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]")
3874 : _("[Incomplete last line]"));
3877 msg_add_lines(c, (long)lnum, nchars);
3879 /* Now display it */
3882 msg_scrolled_ign = TRUE;
3883 msg_trunc_attr(IObuff, FALSE, 0);
3884 msg_scrolled_ign = FALSE;
3889 * Print a message after NetBeans writes the file. This message should be
3890 * identical to the standard message a non-netbeans user would see when
3894 print_save_msg(buf, nchars)
3903 /* put fname in IObuff with quotes */
3904 msg_add_fname(buf->bufp, buf->bufp->b_ffname);
3907 msg_add_lines(c, buf->bufp->b_ml.ml_line_count,
3908 buf->bufp->b_orig_size);
3912 msg_scrolled_ign = TRUE;
3913 p = msg_trunc_attr(IObuff, FALSE, 0);
3914 if ((msg_scrolled && !need_wait_return) || !buf->initDone)
3916 /* Need to repeat the message after redrawing when:
3917 * - When reading from stdin (the screen will be cleared next).
3918 * - When restart_edit is set (otherwise there will be a delay
3919 * before redrawing).
3920 * - When the screen was scrolled but there is no wait-return
3924 msg_scrolled_ign = FALSE;
3925 /* add_to_input_buf((char_u *)"\f", 1); */
3929 char_u msgbuf[IOSIZE];
3931 vim_snprintf((char *)msgbuf, IOSIZE,
3932 _("E505: %s is read-only (add ! to override)"), IObuff);
3933 nbdebug((" %s\n", msgbuf));
3938 #endif /* defined(FEAT_NETBEANS_INTG) */