blob: 0db0eaa3e3c1d01edb80fcfb4b1da12d6d71774d [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.
Bram Moolenaare3cc6d42011-10-20 21:58:34 +020017 *
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.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024 */
25
26#include "vim.h"
27
28#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
29
Bram Moolenaard04a0202016-01-26 23:30:18 +010030#ifndef WIN32
Bram Moolenaar3e53c702016-01-24 22:17:03 +010031# include <netdb.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032# ifdef HAVE_LIBGEN_H
33# include <libgen.h>
34# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000035#endif
36
37#include "version.h"
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039#define GUARDED 10000 /* typenr for "guarded" annotation */
40#define GUARDEDOFFSET 1000000 /* base for "guarded" sign id's */
Bram Moolenaar67c53842010-05-22 18:28:27 +020041#define MAX_COLOR_LENGTH 32 /* max length of color name in defineAnnoType */
Bram Moolenaar071d4272004-06-13 20:20:40 +000042
43/* The first implementation (working only with Netbeans) returned "1.1". The
44 * protocol implemented here also supports A-A-P. */
Bram Moolenaar67c53842010-05-22 18:28:27 +020045static char *ExtEdProtocolVersion = "2.5";
Bram Moolenaar071d4272004-06-13 20:20:40 +000046
47static long pos2off __ARGS((buf_T *, pos_T *));
48static pos_T *off2pos __ARGS((buf_T *, long));
49static pos_T *get_off_or_lnum __ARGS((buf_T *buf, char_u **argp));
50static long get_buf_size __ARGS((buf_T *));
Bram Moolenaar8065d7f2010-01-19 15:13:14 +010051static int netbeans_keystring __ARGS((char_u *keystr));
52static void postpone_keycommand __ARGS((char_u *keystr));
Bram Moolenaar009b2592004-10-24 19:18:58 +000053static void special_keys __ARGS((char_u *args));
Bram Moolenaar071d4272004-06-13 20:20:40 +000054
Bram Moolenaarb26e6322010-05-22 21:34:09 +020055static int netbeans_connect __ARGS((char *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +000056static int getConnInfo __ARGS((char *file, char **host, char **port, char **password));
57
58static void nb_init_graphics __ARGS((void));
59static void coloncmd __ARGS((char *cmd, ...));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000060static void nb_set_curbuf __ARGS((buf_T *buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000061static void nb_parse_cmd __ARGS((char_u *));
62static int nb_do_cmd __ARGS((int, char_u *, int, int, char_u *));
63static void nb_send __ARGS((char *buf, char *fun));
Bram Moolenaarb26e6322010-05-22 21:34:09 +020064static void nb_free __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +000065
Bram Moolenaard04a0202016-01-26 23:30:18 +010066#define NETBEANS_OPEN (nb_channel_idx >= 0 && channel_is_open(nb_channel_idx))
67static int nb_channel_idx = -1;
Bram Moolenaar67c53842010-05-22 18:28:27 +020068
Bram Moolenaar89d40322006-08-29 15:30:07 +000069static int r_cmdno; /* current command number for reply */
Bram Moolenaar009b2592004-10-24 19:18:58 +000070static int dosetvisible = FALSE;
71
Bram Moolenaar071d4272004-06-13 20:20:40 +000072/*
73 * Include the debugging code if wanted.
74 */
75#ifdef NBDEBUG
76# include "nbdebug.c"
77#endif
78
Bram Moolenaarb26e6322010-05-22 21:34:09 +020079static int needupdate = 0;
80static int inAtomic = 0;
81
Bram Moolenaar7ad7d012010-11-16 15:49:02 +010082/*
Bram Moolenaard04a0202016-01-26 23:30:18 +010083 * Callback invoked when the channel is closed.
Bram Moolenaar7ad7d012010-11-16 15:49:02 +010084 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000085 static void
Bram Moolenaard04a0202016-01-26 23:30:18 +010086nb_channel_closed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000087{
Bram Moolenaard04a0202016-01-26 23:30:18 +010088 nb_channel_idx = -1;
Bram Moolenaar7ad7d012010-11-16 15:49:02 +010089}
90
91/*
92 * Close the connection and cleanup.
Bram Moolenaard04a0202016-01-26 23:30:18 +010093 * May be called when the socket was closed earlier.
Bram Moolenaar7ad7d012010-11-16 15:49:02 +010094 */
95 static void
96netbeans_close(void)
97{
98 if (NETBEANS_OPEN)
99 {
100 netbeans_send_disconnect();
Bram Moolenaard04a0202016-01-26 23:30:18 +0100101 if (nb_channel_idx >= 0)
102 /* Close the socket and remove the input handlers. */
103 channel_close(nb_channel_idx);
104 nb_channel_idx = -1;
Bram Moolenaar7ad7d012010-11-16 15:49:02 +0100105 }
106
Bram Moolenaar67c53842010-05-22 18:28:27 +0200107#ifdef FEAT_BEVAL
Bram Moolenaard62bec82005-03-07 22:56:57 +0000108 bevalServers &= ~BEVAL_NETBEANS;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200109#endif
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200110
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200111 needupdate = 0;
112 inAtomic = 0;
113 nb_free();
114
115 /* remove all signs and update the screen after gutter removal */
116 coloncmd(":sign unplace *");
117 changed_window_setting();
118 update_screen(CLEAR);
119 setcursor();
Bram Moolenaar96bcc5e2011-04-01 15:33:59 +0200120 cursor_on();
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200121 out_flush();
122#ifdef FEAT_GUI
Bram Moolenaard54a6882010-08-09 22:49:00 +0200123 if (gui.in_use)
124 {
125 gui_update_cursor(TRUE, FALSE);
126 gui_mch_flush();
127 }
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200128#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130
131#define NB_DEF_HOST "localhost"
132#define NB_DEF_ADDR "3219"
133#define NB_DEF_PASS "changeme"
134
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200135 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200136netbeans_connect(char *params, int doabort)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137{
Bram Moolenaard04a0202016-01-26 23:30:18 +0100138 int port;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139 char buf[32];
140 char *hostname = NULL;
141 char *address = NULL;
142 char *password = NULL;
143 char *fname;
144 char *arg = NULL;
145
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200146 if (*params == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200148 /* "=fname": Read info from specified file. */
Bram Moolenaare0874f82016-01-24 20:36:41 +0100149 if (getConnInfo(params + 1, &hostname, &address, &password) == FAIL)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200150 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151 }
152 else
153 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200154 if (*params == ':')
155 /* ":<host>:<addr>:<password>": get info from argument */
156 arg = params + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157 if (arg == NULL && (fname = getenv("__NETBEANS_CONINFO")) != NULL)
158 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200159 /* "": get info from file specified in environment */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160 if (getConnInfo(fname, &hostname, &address, &password) == FAIL)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200161 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162 }
163 else
164 {
165 if (arg != NULL)
166 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200167 /* ":<host>:<addr>:<password>": get info from argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168 hostname = arg;
169 address = strchr(hostname, ':');
170 if (address != NULL)
171 {
172 *address++ = '\0';
173 password = strchr(address, ':');
174 if (password != NULL)
175 *password++ = '\0';
176 }
177 }
178
179 /* Get the missing values from the environment. */
180 if (hostname == NULL || *hostname == '\0')
181 hostname = getenv("__NETBEANS_HOST");
182 if (address == NULL)
183 address = getenv("__NETBEANS_SOCKET");
184 if (password == NULL)
185 password = getenv("__NETBEANS_VIM_PASSWORD");
186
187 /* Move values to allocated memory. */
188 if (hostname != NULL)
189 hostname = (char *)vim_strsave((char_u *)hostname);
190 if (address != NULL)
191 address = (char *)vim_strsave((char_u *)address);
192 if (password != NULL)
193 password = (char *)vim_strsave((char_u *)password);
194 }
195 }
196
197 /* Use the default when a value is missing. */
198 if (hostname == NULL || *hostname == '\0')
199 {
200 vim_free(hostname);
201 hostname = (char *)vim_strsave((char_u *)NB_DEF_HOST);
202 }
203 if (address == NULL || *address == '\0')
204 {
205 vim_free(address);
206 address = (char *)vim_strsave((char_u *)NB_DEF_ADDR);
207 }
208 if (password == NULL || *password == '\0')
209 {
210 vim_free(password);
211 password = (char *)vim_strsave((char_u *)NB_DEF_PASS);
212 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100213 if (hostname != NULL && address != NULL && password != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214 {
Bram Moolenaard04a0202016-01-26 23:30:18 +0100215 port = atoi(address);
216 nb_channel_idx = channel_open(hostname, port, nb_channel_closed);
217 if (nb_channel_idx >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218 {
Bram Moolenaard04a0202016-01-26 23:30:18 +0100219 /* success */
220# ifdef FEAT_BEVAL
221 bevalServers |= BEVAL_NETBEANS;
222# endif
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200223
Bram Moolenaard04a0202016-01-26 23:30:18 +0100224 /* success, login */
225 vim_snprintf(buf, sizeof(buf), "AUTH %s\n", password);
226 nb_send(buf, "netbeans_connect");
227
228 sprintf(buf, "0:version=0 \"%s\"\n", ExtEdProtocolVersion);
229 nb_send(buf, "externaleditor_version");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230 }
231 }
232
Bram Moolenaard04a0202016-01-26 23:30:18 +0100233 if (nb_channel_idx < 0 && doabort)
234 getout(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236 vim_free(hostname);
237 vim_free(address);
238 vim_free(password);
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200239 return NETBEANS_OPEN ? OK : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240}
241
242/*
243 * Obtain the NetBeans hostname, port address and password from a file.
244 * Return the strings in allocated memory.
245 * Return FAIL if the file could not be read, OK otherwise (no matter what it
246 * contains).
247 */
248 static int
249getConnInfo(char *file, char **host, char **port, char **auth)
250{
251 FILE *fp;
252 char_u buf[BUFSIZ];
253 char_u *lp;
Bram Moolenaar309cbc32012-01-10 22:31:31 +0100254 char_u *nlp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255#ifdef UNIX
256 struct stat st;
257
258 /*
259 * For Unix only accept the file when it's not accessible by others.
260 * The open will then fail if we don't own the file.
261 */
262 if (mch_stat(file, &st) == 0 && (st.st_mode & 0077) != 0)
263 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000264 nbdebug(("Wrong access mode for NetBeans connection info file: \"%s\"\n",
265 file));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266 EMSG2(_("E668: Wrong access mode for NetBeans connection info file: \"%s\""),
267 file);
268 return FAIL;
269 }
270#endif
271
272 fp = mch_fopen(file, "r");
273 if (fp == NULL)
274 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000275 nbdebug(("Cannot open NetBeans connection info file\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276 PERROR("E660: Cannot open NetBeans connection info file");
277 return FAIL;
278 }
279
280 /* Read the file. There should be one of each parameter */
281 while ((lp = (char_u *)fgets((char *)buf, BUFSIZ, fp)) != NULL)
282 {
Bram Moolenaar309cbc32012-01-10 22:31:31 +0100283 if ((nlp = vim_strchr(lp, '\n')) != NULL)
284 *nlp = 0; /* strip off the trailing newline */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285
286 if (STRNCMP(lp, "host=", 5) == 0)
287 {
288 vim_free(*host);
289 *host = (char *)vim_strsave(&buf[5]);
290 }
291 else if (STRNCMP(lp, "port=", 5) == 0)
292 {
293 vim_free(*port);
294 *port = (char *)vim_strsave(&buf[5]);
295 }
296 else if (STRNCMP(lp, "auth=", 5) == 0)
297 {
298 vim_free(*auth);
299 *auth = (char *)vim_strsave(&buf[5]);
300 }
301 }
302 fclose(fp);
303
304 return OK;
305}
306
307
308struct keyqueue
309{
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100310 char_u *keystr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311 struct keyqueue *next;
312 struct keyqueue *prev;
313};
314
315typedef struct keyqueue keyQ_T;
316
317static keyQ_T keyHead; /* dummy node, header for circular queue */
318
319
320/*
321 * Queue up key commands sent from netbeans.
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100322 * We store the string, because it may depend on the global mod_mask and
323 * :nbkey doesn't have a key number.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324 */
325 static void
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100326postpone_keycommand(char_u *keystr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327{
328 keyQ_T *node;
329
330 node = (keyQ_T *)alloc(sizeof(keyQ_T));
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100331 if (node == NULL)
332 return; /* out of memory, drop the key */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333
334 if (keyHead.next == NULL) /* initialize circular queue */
335 {
336 keyHead.next = &keyHead;
337 keyHead.prev = &keyHead;
338 }
339
340 /* insert node at tail of queue */
341 node->next = &keyHead;
342 node->prev = keyHead.prev;
343 keyHead.prev->next = node;
344 keyHead.prev = node;
345
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100346 node->keystr = vim_strsave(keystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347}
348
349/*
350 * Handle any queued-up NetBeans keycommands to be send.
351 */
352 static void
353handle_key_queue(void)
354{
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100355 int postponed = FALSE;
356
357 while (!postponed && keyHead.next && keyHead.next != &keyHead)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 {
359 /* first, unlink the node */
360 keyQ_T *node = keyHead.next;
361 keyHead.next = node->next;
362 node->next->prev = node->prev;
363
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100364 /* Now, send the keycommand. This may cause it to be postponed again
365 * and change keyHead. */
366 if (node->keystr != NULL)
367 postponed = !netbeans_keystring(node->keystr);
368 vim_free(node->keystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369
370 /* Finally, dispose of the node */
371 vim_free(node);
372 }
373}
374
375
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376/*
377 * While there's still a command in the work queue, parse and execute it.
378 */
Bram Moolenaarf2330482008-06-24 20:19:36 +0000379 void
380netbeans_parse_messages(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381{
Bram Moolenaard04a0202016-01-26 23:30:18 +0100382 char_u *buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 char_u *p;
Bram Moolenaar863053d2010-12-02 17:09:54 +0100384 int own_node;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385
Bram Moolenaard04a0202016-01-26 23:30:18 +0100386 while (nb_channel_idx >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 {
Bram Moolenaard04a0202016-01-26 23:30:18 +0100388 buffer = channel_peek(nb_channel_idx);
389 if (buffer == NULL)
390 break; /* nothing to read */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391
392 /* Locate the first line in the first buffer. */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100393 p = vim_strchr(buffer, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 if (p == NULL)
395 {
396 /* Command isn't complete. If there is no following buffer,
397 * return (wait for more). If there is another buffer following,
398 * prepend the text to that buffer and delete this one. */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100399 if (channel_collapse(nb_channel_idx) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 }
402 else
403 {
404 /* There is a complete command at the start of the buffer.
405 * Terminate it with a NUL. When no more text is following unlink
406 * the buffer. Do this before executing, because new buffers can
407 * be added while busy handling the command. */
408 *p++ = NUL;
409 if (*p == NUL)
410 {
Bram Moolenaar863053d2010-12-02 17:09:54 +0100411 own_node = TRUE;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100412 channel_get(nb_channel_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413 }
Bram Moolenaar863053d2010-12-02 17:09:54 +0100414 else
415 own_node = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416
417 /* now, parse and execute the commands */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100418 nb_parse_cmd(buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419
Bram Moolenaar863053d2010-12-02 17:09:54 +0100420 if (own_node)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100421 /* buffer finished, dispose of it */
422 vim_free(buffer);
423 else
424 /* more follows, move it to the start */
425 STRMOVE(buffer, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426 }
427 }
428}
429
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430/*
431 * Handle one NUL terminated command.
432 *
433 * format of a command from netbeans:
434 *
435 * 6:setTitle!84 "a.c"
436 *
437 * bufno
438 * colon
439 * cmd
440 * !
441 * cmdno
442 * args
443 *
444 * for function calls, the ! is replaced by a /
445 */
446 static void
447nb_parse_cmd(char_u *cmd)
448{
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000449 char *verb;
450 char *q;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451 int bufno;
452 int isfunc = -1;
453
454 if (STRCMP(cmd, "DISCONNECT") == 0)
455 {
456 /* We assume the server knows that we can safely exit! */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457 /* Disconnect before exiting, Motif hangs in a Select error
458 * message otherwise. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200459 netbeans_close();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460 getout(0);
461 /* NOTREACHED */
462 }
463
Bram Moolenaard04a0202016-01-26 23:30:18 +0100464 if (STRCMP(cmd, "\"DETACH\"") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 {
Bram Moolenaard04a0202016-01-26 23:30:18 +0100466 buf_T *buf;
467
468 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
469 buf->b_has_sign_column = FALSE;
470
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471 /* The IDE is breaking the connection. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200472 netbeans_close();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473 return;
474 }
475
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000476 bufno = strtol((char *)cmd, &verb, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477
478 if (*verb != ':')
479 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000480 nbdebug((" missing colon: %s\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481 EMSG2("E627: missing colon: %s", cmd);
482 return;
483 }
484 ++verb; /* skip colon */
485
486 for (q = verb; *q; q++)
487 {
488 if (*q == '!')
489 {
490 *q++ = NUL;
491 isfunc = 0;
492 break;
493 }
494 else if (*q == '/')
495 {
496 *q++ = NUL;
497 isfunc = 1;
498 break;
499 }
500 }
501
502 if (isfunc < 0)
503 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000504 nbdebug((" missing ! or / in: %s\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 EMSG2("E628: missing ! or / in: %s", cmd);
506 return;
507 }
508
Bram Moolenaar89d40322006-08-29 15:30:07 +0000509 r_cmdno = strtol(q, &q, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000511 q = (char *)skipwhite((char_u *)q);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512
Bram Moolenaar89d40322006-08-29 15:30:07 +0000513 if (nb_do_cmd(bufno, (char_u *)verb, isfunc, r_cmdno, (char_u *)q) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 {
Bram Moolenaar009b2592004-10-24 19:18:58 +0000515#ifdef NBDEBUG
516 /*
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100517 * This happens because the ExtEd can send a command or 2 after
Bram Moolenaar009b2592004-10-24 19:18:58 +0000518 * doing a stopDocumentListen command. It doesn't harm anything
519 * so I'm disabling it except for debugging.
520 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 nbdebug(("nb_parse_cmd: Command error for \"%s\"\n", cmd));
522 EMSG("E629: bad return from nb_do_cmd");
Bram Moolenaar009b2592004-10-24 19:18:58 +0000523#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 }
525}
526
527struct nbbuf_struct
528{
529 buf_T *bufp;
530 unsigned int fireChanges:1;
531 unsigned int initDone:1;
Bram Moolenaar009b2592004-10-24 19:18:58 +0000532 unsigned int insertDone:1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 unsigned int modified:1;
Bram Moolenaar009b2592004-10-24 19:18:58 +0000534 int nbbuf_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 char *displayname;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 int *signmap;
537 short_u signmaplen;
538 short_u signmapused;
539};
540
541typedef struct nbbuf_struct nbbuf_T;
542
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200543static nbbuf_T *buf_list = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000544static int buf_list_size = 0; /* size of buf_list */
545static int buf_list_used = 0; /* nr of entries in buf_list actually in use */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200547static char **globalsignmap = NULL;
548static int globalsignmaplen = 0;
549static int globalsignmapused = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550
551static int mapsigntype __ARGS((nbbuf_T *, int localsigntype));
552static void addsigntype __ARGS((nbbuf_T *, int localsigntype, char_u *typeName,
553 char_u *tooltip, char_u *glyphfile,
Bram Moolenaar67c53842010-05-22 18:28:27 +0200554 char_u *fg, char_u *bg));
Bram Moolenaar009b2592004-10-24 19:18:58 +0000555static void print_read_msg __ARGS((nbbuf_T *buf));
Bram Moolenaar914703b2010-05-31 21:59:46 +0200556static void print_save_msg __ARGS((nbbuf_T *buf, off_t nchars));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557
558static int curPCtype = -1;
559
560/*
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200561 * Free netbeans resources.
562 */
563 static void
564nb_free()
565{
566 keyQ_T *key_node = keyHead.next;
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200567 nbbuf_T buf;
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200568 int i;
569
570 /* free the netbeans buffer list */
571 for (i = 0; i < buf_list_used; i++)
572 {
573 buf = buf_list[i];
574 vim_free(buf.displayname);
575 vim_free(buf.signmap);
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100576 if (buf.bufp != NULL)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200577 {
578 buf.bufp->b_netbeans_file = FALSE;
579 buf.bufp->b_was_netbeans_file = FALSE;
580 }
581 }
582 vim_free(buf_list);
583 buf_list = NULL;
584 buf_list_size = 0;
585 buf_list_used = 0;
586
587 /* free the queued key commands */
Bram Moolenaar8d4eecc2012-11-20 17:19:01 +0100588 while (key_node != NULL && key_node != &keyHead)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200589 {
590 keyQ_T *next = key_node->next;
591 vim_free(key_node->keystr);
592 vim_free(key_node);
593 if (next == &keyHead)
594 {
595 keyHead.next = &keyHead;
596 keyHead.prev = &keyHead;
597 break;
598 }
599 key_node = next;
600 }
601
602 /* free the queued netbeans commands */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100603 if (nb_channel_idx >= 0)
604 channel_clear(nb_channel_idx);
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200605}
606
607/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 * Get the Netbeans buffer number for the specified buffer.
609 */
610 static int
611nb_getbufno(buf_T *bufp)
612{
613 int i;
614
615 for (i = 0; i < buf_list_used; i++)
616 if (buf_list[i].bufp == bufp)
617 return i;
618 return -1;
619}
620
621/*
622 * Is this a NetBeans-owned buffer?
623 */
624 int
625isNetbeansBuffer(buf_T *bufp)
626{
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200627 return NETBEANS_OPEN && bufp->b_netbeans_file;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628}
629
630/*
631 * NetBeans and Vim have different undo models. In Vim, the file isn't
632 * changed if changes are undone via the undo command. In NetBeans, once
633 * a change has been made the file is marked as modified until saved. It
634 * doesn't matter if the change was undone.
635 *
636 * So this function is for the corner case where Vim thinks a buffer is
637 * unmodified but NetBeans thinks it IS modified.
638 */
639 int
640isNetbeansModified(buf_T *bufp)
641{
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200642 if (isNetbeansBuffer(bufp))
Bram Moolenaar009b2592004-10-24 19:18:58 +0000643 {
644 int bufno = nb_getbufno(bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645
Bram Moolenaar009b2592004-10-24 19:18:58 +0000646 if (bufno > 0)
647 return buf_list[bufno].modified;
648 else
649 return FALSE;
650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 else
652 return FALSE;
653}
654
655/*
656 * Given a Netbeans buffer number, return the netbeans buffer.
657 * Returns NULL for 0 or a negative number. A 0 bufno means a
658 * non-buffer related command has been sent.
659 */
660 static nbbuf_T *
661nb_get_buf(int bufno)
662{
663 /* find or create a buffer with the given number */
664 int incr;
665
666 if (bufno <= 0)
667 return NULL;
668
669 if (!buf_list)
670 {
671 /* initialize */
672 buf_list = (nbbuf_T *)alloc_clear(100 * sizeof(nbbuf_T));
673 buf_list_size = 100;
674 }
675 if (bufno >= buf_list_used) /* new */
676 {
677 if (bufno >= buf_list_size) /* grow list */
678 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +0100679 nbbuf_T *t_buf_list = buf_list;
680
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 incr = bufno - buf_list_size + 90;
682 buf_list_size += incr;
683 buf_list = (nbbuf_T *)vim_realloc(
684 buf_list, buf_list_size * sizeof(nbbuf_T));
Bram Moolenaar9abd5c62015-02-10 18:34:01 +0100685 if (buf_list == NULL)
686 {
687 vim_free(t_buf_list);
688 buf_list_size = 0;
689 return NULL;
690 }
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200691 vim_memset(buf_list + buf_list_size - incr, 0,
692 incr * sizeof(nbbuf_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 }
694
695 while (buf_list_used <= bufno)
696 {
697 /* Default is to fire text changes. */
698 buf_list[buf_list_used].fireChanges = 1;
699 ++buf_list_used;
700 }
701 }
702
703 return buf_list + bufno;
704}
705
706/*
707 * Return the number of buffers that are modified.
708 */
709 static int
710count_changed_buffers(void)
711{
712 buf_T *bufp;
713 int n;
714
715 n = 0;
716 for (bufp = firstbuf; bufp != NULL; bufp = bufp->b_next)
717 if (bufp->b_changed)
718 ++n;
719 return n;
720}
721
722/*
723 * End the netbeans session.
724 */
725 void
726netbeans_end(void)
727{
728 int i;
729 static char buf[128];
730
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200731 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 return;
733
734 for (i = 0; i < buf_list_used; i++)
735 {
736 if (!buf_list[i].bufp)
737 continue;
738 if (netbeansForcedQuit)
739 {
740 /* mark as unmodified so NetBeans won't put up dialog on "killed" */
Bram Moolenaar89d40322006-08-29 15:30:07 +0000741 sprintf(buf, "%d:unmodified=%d\n", i, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 nbdebug(("EVT: %s", buf));
743 nb_send(buf, "netbeans_end");
744 }
Bram Moolenaar89d40322006-08-29 15:30:07 +0000745 sprintf(buf, "%d:killed=%d\n", i, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 nbdebug(("EVT: %s", buf));
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200747 /* nb_send(buf, "netbeans_end"); avoid "write failed" messages */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100748 nb_send(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750}
751
752/*
753 * Send a message to netbeans.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100754 * When "fun" is NULL no error is given.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 */
756 static void
757nb_send(char *buf, char *fun)
758{
Bram Moolenaard04a0202016-01-26 23:30:18 +0100759 if (nb_channel_idx >= 0)
760 channel_send(nb_channel_idx, (char_u *)buf, fun);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761}
762
763/*
764 * Some input received from netbeans requires a response. This function
765 * handles a response with no information (except the command number).
766 */
767 static void
768nb_reply_nil(int cmdno)
769{
770 char reply[32];
771
Bram Moolenaar009b2592004-10-24 19:18:58 +0000772 nbdebug(("REP %d: <none>\n", cmdno));
773
Bram Moolenaar7ad7d012010-11-16 15:49:02 +0100774 /* Avoid printing an annoying error message. */
775 if (!NETBEANS_OPEN)
776 return;
777
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 sprintf(reply, "%d\n", cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 nb_send(reply, "nb_reply_nil");
780}
781
782
783/*
784 * Send a response with text.
785 * "result" must have been quoted already (using nb_quote()).
786 */
787 static void
788nb_reply_text(int cmdno, char_u *result)
789{
790 char_u *reply;
791
Bram Moolenaar009b2592004-10-24 19:18:58 +0000792 nbdebug(("REP %d: %s\n", cmdno, (char *)result));
793
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000794 reply = alloc((unsigned)STRLEN(result) + 32);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 sprintf((char *)reply, "%d %s\n", cmdno, (char *)result);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 nb_send((char *)reply, "nb_reply_text");
797
798 vim_free(reply);
799}
800
801
802/*
803 * Send a response with a number result code.
804 */
805 static void
806nb_reply_nr(int cmdno, long result)
807{
808 char reply[32];
809
Bram Moolenaar009b2592004-10-24 19:18:58 +0000810 nbdebug(("REP %d: %ld\n", cmdno, result));
811
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 sprintf(reply, "%d %ld\n", cmdno, result);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 nb_send(reply, "nb_reply_nr");
814}
815
816
817/*
818 * Encode newline, ret, backslash, double quote for transmission to NetBeans.
819 */
820 static char_u *
821nb_quote(char_u *txt)
822{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000823 char_u *buf = alloc((unsigned)(2 * STRLEN(txt) + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 char_u *p = txt;
825 char_u *q = buf;
826
827 if (buf == NULL)
828 return NULL;
829 for (; *p; p++)
830 {
831 switch (*p)
832 {
833 case '\"':
834 case '\\':
835 *q++ = '\\'; *q++ = *p; break;
836 /* case '\t': */
837 /* *q++ = '\\'; *q++ = 't'; break; */
838 case '\n':
839 *q++ = '\\'; *q++ = 'n'; break;
840 case '\r':
841 *q++ = '\\'; *q++ = 'r'; break;
842 default:
843 *q++ = *p;
844 break;
845 }
846 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100847 *q = '\0';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848
849 return buf;
850}
851
852
853/*
854 * Remove top level double quotes; convert backslashed chars.
855 * Returns an allocated string (NULL for failure).
856 * If "endp" is not NULL it is set to the character after the terminating
857 * quote.
858 */
859 static char *
860nb_unquote(char_u *p, char_u **endp)
861{
862 char *result = 0;
863 char *q;
864 int done = 0;
865
866 /* result is never longer than input */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000867 result = (char *)alloc_clear((unsigned)STRLEN(p) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 if (result == NULL)
869 return NULL;
870
871 if (*p++ != '"')
872 {
873 nbdebug(("nb_unquote called with string that doesn't start with a quote!: %s\n",
874 p));
875 result[0] = NUL;
876 return result;
877 }
878
879 for (q = result; !done && *p != NUL;)
880 {
881 switch (*p)
882 {
883 case '"':
884 /*
885 * Unbackslashed dquote marks the end, if first char was dquote.
886 */
887 done = 1;
888 break;
889
890 case '\\':
891 ++p;
892 switch (*p)
893 {
894 case '\\': *q++ = '\\'; break;
895 case 'n': *q++ = '\n'; break;
896 case 't': *q++ = '\t'; break;
897 case 'r': *q++ = '\r'; break;
898 case '"': *q++ = '"'; break;
899 case NUL: --p; break;
900 /* default: skip over illegal chars */
901 }
902 ++p;
903 break;
904
905 default:
906 *q++ = *p++;
907 }
908 }
909
910 if (endp != NULL)
911 *endp = p;
912
913 return result;
914}
915
Bram Moolenaar5eaf8722008-01-05 17:07:13 +0000916/*
917 * Remove from "first" byte to "last" byte (inclusive), at line "lnum" of the
918 * current buffer. Remove to end of line when "last" is MAXCOL.
919 */
920 static void
921nb_partialremove(linenr_T lnum, colnr_T first, colnr_T last)
922{
923 char_u *oldtext, *newtext;
924 int oldlen;
925 int lastbyte = last;
926
927 oldtext = ml_get(lnum);
Bram Moolenaarcb4cef22008-03-16 15:04:34 +0000928 oldlen = (int)STRLEN(oldtext);
Bram Moolenaarb3c70982008-01-18 10:40:55 +0000929 if (first >= (colnr_T)oldlen || oldlen == 0) /* just in case */
Bram Moolenaar5eaf8722008-01-05 17:07:13 +0000930 return;
931 if (lastbyte >= oldlen)
932 lastbyte = oldlen - 1;
933 newtext = alloc(oldlen - (int)(lastbyte - first));
934 if (newtext != NULL)
935 {
936 mch_memmove(newtext, oldtext, first);
Bram Moolenaarf2330482008-06-24 20:19:36 +0000937 STRMOVE(newtext + first, oldtext + lastbyte + 1);
Bram Moolenaar5eaf8722008-01-05 17:07:13 +0000938 nbdebug((" NEW LINE %d: %s\n", lnum, newtext));
939 ml_replace(lnum, newtext, FALSE);
940 }
941}
942
943/*
944 * Replace the "first" line with the concatenation of the "first" and
945 * the "other" line. The "other" line is not removed.
946 */
947 static void
948nb_joinlines(linenr_T first, linenr_T other)
949{
950 int len_first, len_other;
951 char_u *p;
952
Bram Moolenaarcb4cef22008-03-16 15:04:34 +0000953 len_first = (int)STRLEN(ml_get(first));
954 len_other = (int)STRLEN(ml_get(other));
Bram Moolenaar5eaf8722008-01-05 17:07:13 +0000955 p = alloc((unsigned)(len_first + len_other + 1));
956 if (p != NULL)
957 {
958 mch_memmove(p, ml_get(first), len_first);
959 mch_memmove(p + len_first, ml_get(other), len_other + 1);
960 ml_replace(first, p, FALSE);
961 }
962}
963
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964#define SKIP_STOP 2
965#define streq(a,b) (strcmp(a,b) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966
967/*
968 * Do the actual processing of a single netbeans command or function.
Bram Moolenaar143c38c2007-05-10 16:41:10 +0000969 * The difference between a command and function is that a function
970 * gets a response (it's required) but a command does not.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 * For arguments see comment for nb_parse_cmd().
972 */
973 static int
974nb_do_cmd(
975 int bufno,
976 char_u *cmd,
977 int func,
978 int cmdno,
979 char_u *args) /* points to space before arguments or NUL */
980{
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100981 int do_update = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 long off = 0;
983 nbbuf_T *buf = nb_get_buf(bufno);
984 static int skip = 0;
985 int retval = OK;
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000986 char *cp; /* for when a char pointer is needed */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987
988 nbdebug(("%s %d: (%d) %s %s\n", (func) ? "FUN" : "CMD", cmdno, bufno, cmd,
989 STRCMP(cmd, "insert") == 0 ? "<text>" : (char *)args));
990
991 if (func)
992 {
993/* =====================================================================*/
994 if (streq((char *)cmd, "getModified"))
995 {
996 if (buf == NULL || buf->bufp == NULL)
997 /* Return the number of buffers that are modified. */
998 nb_reply_nr(cmdno, (long)count_changed_buffers());
999 else
1000 /* Return whether the buffer is modified. */
1001 nb_reply_nr(cmdno, (long)(buf->bufp->b_changed
1002 || isNetbeansModified(buf->bufp)));
1003/* =====================================================================*/
1004 }
1005 else if (streq((char *)cmd, "saveAndExit"))
1006 {
1007 /* Note: this will exit Vim if successful. */
1008 coloncmd(":confirm qall");
1009
1010 /* We didn't exit: return the number of changed buffers. */
1011 nb_reply_nr(cmdno, (long)count_changed_buffers());
1012/* =====================================================================*/
1013 }
1014 else if (streq((char *)cmd, "getCursor"))
1015 {
1016 char_u text[200];
1017
1018 /* Note: nb_getbufno() may return -1. This indicates the IDE
1019 * didn't assign a number to the current buffer in response to a
1020 * fileOpened event. */
1021 sprintf((char *)text, "%d %ld %d %ld",
1022 nb_getbufno(curbuf),
1023 (long)curwin->w_cursor.lnum,
1024 (int)curwin->w_cursor.col,
1025 pos2off(curbuf, &curwin->w_cursor));
1026 nb_reply_text(cmdno, text);
1027/* =====================================================================*/
1028 }
Bram Moolenaarc65c4912006-11-14 17:29:46 +00001029 else if (streq((char *)cmd, "getAnno"))
1030 {
1031 long linenum = 0;
1032#ifdef FEAT_SIGNS
1033 if (buf == NULL || buf->bufp == NULL)
1034 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001035 nbdebug((" Invalid buffer identifier in getAnno\n"));
1036 EMSG("E652: Invalid buffer identifier in getAnno");
Bram Moolenaarc65c4912006-11-14 17:29:46 +00001037 retval = FAIL;
1038 }
1039 else
1040 {
1041 int serNum;
1042
1043 cp = (char *)args;
1044 serNum = strtol(cp, &cp, 10);
1045 /* If the sign isn't found linenum will be zero. */
1046 linenum = (long)buf_findsign(buf->bufp, serNum);
1047 }
1048#endif
1049 nb_reply_nr(cmdno, linenum);
1050/* =====================================================================*/
1051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 else if (streq((char *)cmd, "getLength"))
1053 {
1054 long len = 0;
1055
1056 if (buf == NULL || buf->bufp == NULL)
1057 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001058 nbdebug((" invalid buffer identifier in getLength\n"));
1059 EMSG("E632: invalid buffer identifier in getLength");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 retval = FAIL;
1061 }
1062 else
1063 {
1064 len = get_buf_size(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 }
1066 nb_reply_nr(cmdno, len);
1067/* =====================================================================*/
1068 }
1069 else if (streq((char *)cmd, "getText"))
1070 {
1071 long len;
1072 linenr_T nlines;
1073 char_u *text = NULL;
1074 linenr_T lno = 1;
1075 char_u *p;
1076 char_u *line;
1077
1078 if (buf == NULL || buf->bufp == NULL)
1079 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001080 nbdebug((" invalid buffer identifier in getText\n"));
1081 EMSG("E633: invalid buffer identifier in getText");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 retval = FAIL;
1083 }
1084 else
1085 {
1086 len = get_buf_size(buf->bufp);
1087 nlines = buf->bufp->b_ml.ml_line_count;
1088 text = alloc((unsigned)((len > 0)
1089 ? ((len + nlines) * 2) : 4));
1090 if (text == NULL)
1091 {
1092 nbdebug((" nb_do_cmd: getText has null text field\n"));
1093 retval = FAIL;
1094 }
1095 else
1096 {
1097 p = text;
1098 *p++ = '\"';
1099 for (; lno <= nlines ; lno++)
1100 {
1101 line = nb_quote(ml_get_buf(buf->bufp, lno, FALSE));
1102 if (line != NULL)
1103 {
1104 STRCPY(p, line);
1105 p += STRLEN(line);
1106 *p++ = '\\';
1107 *p++ = 'n';
Bram Moolenaar009b2592004-10-24 19:18:58 +00001108 vim_free(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 }
1111 *p++ = '\"';
1112 *p = '\0';
1113 }
1114 }
1115 if (text == NULL)
1116 nb_reply_text(cmdno, (char_u *)"");
1117 else
1118 {
1119 nb_reply_text(cmdno, text);
1120 vim_free(text);
1121 }
1122/* =====================================================================*/
1123 }
1124 else if (streq((char *)cmd, "remove"))
1125 {
1126 long count;
1127 pos_T first, last;
1128 pos_T *pos;
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001129 pos_T *next;
1130 linenr_T del_from_lnum, del_to_lnum; /* lines to be deleted as a whole */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 int oldFire = netbeansFireChanges;
1132 int oldSuppress = netbeansSuppressNoLines;
1133 int wasChanged;
1134
1135 if (skip >= SKIP_STOP)
1136 {
1137 nbdebug((" Skipping %s command\n", (char *) cmd));
1138 nb_reply_nil(cmdno);
1139 return OK;
1140 }
1141
1142 if (buf == NULL || buf->bufp == NULL)
1143 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001144 nbdebug((" invalid buffer identifier in remove\n"));
1145 EMSG("E634: invalid buffer identifier in remove");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 retval = FAIL;
1147 }
1148 else
1149 {
1150 netbeansFireChanges = FALSE;
1151 netbeansSuppressNoLines = TRUE;
1152
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001153 nb_set_curbuf(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 wasChanged = buf->bufp->b_changed;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001155 cp = (char *)args;
1156 off = strtol(cp, &cp, 10);
1157 count = strtol(cp, &cp, 10);
1158 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 /* delete "count" chars, starting at "off" */
1160 pos = off2pos(buf->bufp, off);
1161 if (!pos)
1162 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001163 nbdebug((" !bad position\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 nb_reply_text(cmdno, (char_u *)"!bad position");
1165 netbeansFireChanges = oldFire;
1166 netbeansSuppressNoLines = oldSuppress;
1167 return FAIL;
1168 }
1169 first = *pos;
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001170 nbdebug((" FIRST POS: line %d, col %d\n",
1171 first.lnum, first.col));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 pos = off2pos(buf->bufp, off+count-1);
1173 if (!pos)
1174 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001175 nbdebug((" !bad count\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 nb_reply_text(cmdno, (char_u *)"!bad count");
1177 netbeansFireChanges = oldFire;
1178 netbeansSuppressNoLines = oldSuppress;
1179 return FAIL;
1180 }
1181 last = *pos;
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001182 nbdebug((" LAST POS: line %d, col %d\n",
1183 last.lnum, last.col));
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001184 del_from_lnum = first.lnum;
1185 del_to_lnum = last.lnum;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001186 do_update = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001188 /* Get the position of the first byte after the deleted
1189 * section. "next" is NULL when deleting to the end of the
1190 * file. */
1191 next = off2pos(buf->bufp, off + count);
1192
1193 /* Remove part of the first line. */
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001194 if (first.col != 0
1195 || (next != NULL && first.lnum == next->lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 {
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001197 if (first.lnum != last.lnum
1198 || (next != NULL && first.lnum != next->lnum))
1199 {
1200 /* remove to the end of the first line */
1201 nb_partialremove(first.lnum, first.col,
1202 (colnr_T)MAXCOL);
1203 if (first.lnum == last.lnum)
1204 {
1205 /* Partial line to remove includes the end of
1206 * line. Join the line with the next one, have
1207 * the next line deleted below. */
1208 nb_joinlines(first.lnum, next->lnum);
1209 del_to_lnum = next->lnum;
1210 }
1211 }
1212 else
1213 {
1214 /* remove within one line */
1215 nb_partialremove(first.lnum, first.col, last.col);
1216 }
1217 ++del_from_lnum; /* don't delete the first line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 }
1219
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001220 /* Remove part of the last line. */
1221 if (first.lnum != last.lnum && next != NULL
1222 && next->col != 0 && last.lnum == next->lnum)
1223 {
1224 nb_partialremove(last.lnum, 0, last.col);
1225 if (del_from_lnum > first.lnum)
1226 {
1227 /* Join end of last line to start of first line; last
1228 * line is deleted below. */
1229 nb_joinlines(first.lnum, last.lnum);
1230 }
1231 else
1232 /* First line is deleted as a whole, keep the last
1233 * line. */
1234 --del_to_lnum;
1235 }
1236
1237 /* First is partial line; last line to remove includes
1238 * the end of line; join first line to line following last
1239 * line; line following last line is deleted below. */
1240 if (first.lnum != last.lnum && del_from_lnum > first.lnum
1241 && next != NULL && last.lnum != next->lnum)
1242 {
1243 nb_joinlines(first.lnum, next->lnum);
1244 del_to_lnum = next->lnum;
1245 }
1246
1247 /* Delete whole lines if there are any. */
1248 if (del_to_lnum >= del_from_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 {
1250 int i;
1251
1252 /* delete signs from the lines being deleted */
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001253 for (i = del_from_lnum; i <= del_to_lnum; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 {
1255 int id = buf_findsign_id(buf->bufp, (linenr_T)i);
1256 if (id > 0)
1257 {
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001258 nbdebug((" Deleting sign %d on line %d\n",
1259 id, i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 buf_delsign(buf->bufp, id);
1261 }
1262 else
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001263 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 nbdebug((" No sign on line %d\n", i));
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 }
1267
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001268 nbdebug((" Deleting lines %d through %d\n",
1269 del_from_lnum, del_to_lnum));
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001270 curwin->w_cursor.lnum = del_from_lnum;
1271 curwin->w_cursor.col = 0;
1272 del_lines(del_to_lnum - del_from_lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 }
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001274
1275 /* Leave cursor at first deleted byte. */
1276 curwin->w_cursor = first;
1277 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 buf->bufp->b_changed = wasChanged; /* logically unchanged */
1279 netbeansFireChanges = oldFire;
1280 netbeansSuppressNoLines = oldSuppress;
1281
1282 u_blockfree(buf->bufp);
1283 u_clearall(buf->bufp);
1284 }
1285 nb_reply_nil(cmdno);
1286/* =====================================================================*/
1287 }
1288 else if (streq((char *)cmd, "insert"))
1289 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 char_u *to_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291
1292 if (skip >= SKIP_STOP)
1293 {
1294 nbdebug((" Skipping %s command\n", (char *) cmd));
1295 nb_reply_nil(cmdno);
1296 return OK;
1297 }
1298
1299 /* get offset */
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001300 cp = (char *)args;
1301 off = strtol(cp, &cp, 10);
1302 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303
1304 /* get text to be inserted */
1305 args = skipwhite(args);
1306 args = to_free = (char_u *)nb_unquote(args, NULL);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001307 /*
1308 nbdebug((" CHUNK[%d]: %d bytes at offset %d\n",
1309 buf->bufp->b_ml.ml_line_count, STRLEN(args), off));
1310 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311
1312 if (buf == NULL || buf->bufp == NULL)
1313 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001314 nbdebug((" invalid buffer identifier in insert\n"));
1315 EMSG("E635: invalid buffer identifier in insert");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 retval = FAIL;
1317 }
1318 else if (args != NULL)
1319 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001320 int ff_detected = EOL_UNKNOWN;
1321 int buf_was_empty = (buf->bufp->b_ml.ml_flags & ML_EMPTY);
1322 size_t len = 0;
1323 int added = 0;
1324 int oldFire = netbeansFireChanges;
1325 int old_b_changed;
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001326 char_u *nlp;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001327 linenr_T lnum;
1328 linenr_T lnum_start;
1329 pos_T *pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 netbeansFireChanges = 0;
1332
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001333 /* Jump to the buffer where we insert. After this "curbuf"
1334 * can be used. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001335 nb_set_curbuf(buf->bufp);
Bram Moolenaara3227e22006-03-08 21:32:40 +00001336 old_b_changed = curbuf->b_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001338 /* Convert the specified character offset into a lnum/col
1339 * position. */
Bram Moolenaara3227e22006-03-08 21:32:40 +00001340 pos = off2pos(curbuf, off);
1341 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001343 if (pos->lnum <= 0)
1344 lnum_start = 1;
1345 else
1346 lnum_start = pos->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 }
1348 else
1349 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001350 /* If the given position is not found, assume we want
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 * the end of the file. See setLocAndSize HACK. */
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001352 if (buf_was_empty)
1353 lnum_start = 1; /* above empty line */
1354 else
1355 lnum_start = curbuf->b_ml.ml_line_count + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001356 }
1357
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001358 /* "lnum" is the line where we insert: either append to it or
1359 * insert a new line above it. */
1360 lnum = lnum_start;
1361
1362 /* Loop over the "\n" separated lines of the argument. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001363 do_update = 1;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001364 while (*args != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 {
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001366 nlp = vim_strchr(args, '\n');
1367 if (nlp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001369 /* Incomplete line, probably truncated. Next "insert"
1370 * command should append to this one. */
1371 len = STRLEN(args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001373 else
1374 {
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001375 len = nlp - args;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001376
1377 /*
1378 * We need to detect EOL style, because the commands
1379 * use a character offset.
1380 */
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001381 if (nlp > args && nlp[-1] == '\r')
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001382 {
1383 ff_detected = EOL_DOS;
1384 --len;
1385 }
1386 else
1387 ff_detected = EOL_UNIX;
1388 }
1389 args[len] = NUL;
1390
1391 if (lnum == lnum_start
1392 && ((pos != NULL && pos->col > 0)
1393 || (lnum == 1 && buf_was_empty)))
1394 {
1395 char_u *oldline = ml_get(lnum);
1396 char_u *newline;
1397
Bram Moolenaare4365282012-04-20 19:47:05 +02001398 /* Insert halfway a line. */
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001399 newline = alloc_check(
1400 (unsigned)(STRLEN(oldline) + len + 1));
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001401 if (newline != NULL)
1402 {
Bram Moolenaare4365282012-04-20 19:47:05 +02001403 mch_memmove(newline, oldline, (size_t)pos->col);
1404 newline[pos->col] = NUL;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001405 STRCAT(newline, args);
Bram Moolenaare4365282012-04-20 19:47:05 +02001406 STRCAT(newline, oldline + pos->col);
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001407 ml_replace(lnum, newline, FALSE);
1408 }
1409 }
1410 else
1411 {
1412 /* Append a new line. Not that we always do this,
1413 * also when the text doesn't end in a "\n". */
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001414 ml_append((linenr_T)(lnum - 1), args,
1415 (colnr_T)(len + 1), FALSE);
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001416 ++added;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001417 }
1418
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001419 if (nlp == NULL)
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001420 break;
1421 ++lnum;
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001422 args = nlp + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001423 }
1424
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001425 /* Adjust the marks below the inserted lines. */
1426 appended_lines_mark(lnum_start - 1, (long)added);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001428 /*
1429 * When starting with an empty buffer set the fileformat.
1430 * This is just guessing...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 */
1432 if (buf_was_empty)
1433 {
1434 if (ff_detected == EOL_UNKNOWN)
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001435#if defined(MSDOS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 ff_detected = EOL_DOS;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001437#else
1438 ff_detected = EOL_UNIX;
1439#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 set_fileformat(ff_detected, OPT_LOCAL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00001441 curbuf->b_start_ffc = *curbuf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 }
1443
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 /*
1445 * XXX - GRP - Is the next line right? If I've inserted
1446 * text the buffer has been updated but not written. Will
1447 * netbeans guarantee to write it? Even if I do a :q! ?
1448 */
Bram Moolenaara3227e22006-03-08 21:32:40 +00001449 curbuf->b_changed = old_b_changed; /* logically unchanged */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 netbeansFireChanges = oldFire;
1451
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001452 /* Undo info is invalid now... */
Bram Moolenaara3227e22006-03-08 21:32:40 +00001453 u_blockfree(curbuf);
1454 u_clearall(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 }
1456 vim_free(to_free);
1457 nb_reply_nil(cmdno); /* or !error */
1458 }
1459 else
1460 {
1461 nbdebug(("UNIMPLEMENTED FUNCTION: %s\n", cmd));
1462 nb_reply_nil(cmdno);
1463 retval = FAIL;
1464 }
1465 }
1466 else /* Not a function; no reply required. */
1467 {
1468/* =====================================================================*/
1469 if (streq((char *)cmd, "create"))
1470 {
1471 /* Create a buffer without a name. */
1472 if (buf == NULL)
1473 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001474 nbdebug((" invalid buffer identifier in create\n"));
1475 EMSG("E636: invalid buffer identifier in create");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 return FAIL;
1477 }
1478 vim_free(buf->displayname);
1479 buf->displayname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480
1481 netbeansReadFile = 0; /* don't try to open disk file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001482 do_ecmd(0, NULL, 0, 0, ECMD_ONE, ECMD_HIDE + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 netbeansReadFile = 1;
1484 buf->bufp = curbuf;
1485 maketitle();
Bram Moolenaar009b2592004-10-24 19:18:58 +00001486 buf->insertDone = FALSE;
Bram Moolenaar67c53842010-05-22 18:28:27 +02001487#if defined(FEAT_MENU) && defined(FEAT_GUI)
Bram Moolenaard54a6882010-08-09 22:49:00 +02001488 if (gui.in_use)
1489 gui_update_menus(0);
Bram Moolenaar67c53842010-05-22 18:28:27 +02001490#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491/* =====================================================================*/
1492 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001493 else if (streq((char *)cmd, "insertDone"))
1494 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001495 if (buf == NULL || buf->bufp == NULL)
1496 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001497 nbdebug((" invalid buffer identifier in insertDone\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001498 }
1499 else
1500 {
1501 buf->bufp->b_start_eol = *args == 'T';
1502 buf->insertDone = TRUE;
1503 args += 2;
1504 buf->bufp->b_p_ro = *args == 'T';
1505 print_read_msg(buf);
1506 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001507/* =====================================================================*/
1508 }
1509 else if (streq((char *)cmd, "saveDone"))
1510 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001511 long savedChars = atol((char *)args);
1512
1513 if (buf == NULL || buf->bufp == NULL)
1514 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001515 nbdebug((" invalid buffer identifier in saveDone\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001516 }
1517 else
1518 print_save_msg(buf, savedChars);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001519/* =====================================================================*/
1520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 else if (streq((char *)cmd, "startDocumentListen"))
1522 {
1523 if (buf == NULL)
1524 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001525 nbdebug((" invalid buffer identifier in startDocumentListen\n"));
1526 EMSG("E637: invalid buffer identifier in startDocumentListen");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 return FAIL;
1528 }
1529 buf->fireChanges = 1;
1530/* =====================================================================*/
1531 }
1532 else if (streq((char *)cmd, "stopDocumentListen"))
1533 {
1534 if (buf == NULL)
1535 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001536 nbdebug((" invalid buffer identifier in stopDocumentListen\n"));
1537 EMSG("E638: invalid buffer identifier in stopDocumentListen");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 return FAIL;
1539 }
1540 buf->fireChanges = 0;
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001541 if (buf->bufp != NULL && buf->bufp->b_was_netbeans_file)
Bram Moolenaar009b2592004-10-24 19:18:58 +00001542 {
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001543 if (!buf->bufp->b_netbeans_file)
Bram Moolenaarf2330482008-06-24 20:19:36 +00001544 {
1545 nbdebug(("E658: NetBeans connection lost for buffer %ld\n", buf->bufp->b_fnum));
Bram Moolenaar009b2592004-10-24 19:18:58 +00001546 EMSGN(_("E658: NetBeans connection lost for buffer %ld"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 buf->bufp->b_fnum);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001548 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001549 else
1550 {
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001551 /* NetBeans uses stopDocumentListen when it stops editing
1552 * a file. It then expects the buffer in Vim to
1553 * disappear. */
1554 do_bufdel(DOBUF_DEL, (char_u *)"", 1,
1555 buf->bufp->b_fnum, buf->bufp->b_fnum, TRUE);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001556 vim_memset(buf, 0, sizeof(nbbuf_T));
1557 }
1558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559/* =====================================================================*/
1560 }
1561 else if (streq((char *)cmd, "setTitle"))
1562 {
1563 if (buf == NULL)
1564 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001565 nbdebug((" invalid buffer identifier in setTitle\n"));
1566 EMSG("E639: invalid buffer identifier in setTitle");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 return FAIL;
1568 }
1569 vim_free(buf->displayname);
1570 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571/* =====================================================================*/
1572 }
1573 else if (streq((char *)cmd, "initDone"))
1574 {
1575 if (buf == NULL || buf->bufp == NULL)
1576 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001577 nbdebug((" invalid buffer identifier in initDone\n"));
1578 EMSG("E640: invalid buffer identifier in initDone");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 return FAIL;
1580 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001581 do_update = 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001582 buf->initDone = TRUE;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001583 nb_set_curbuf(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584#if defined(FEAT_AUTOCMD)
1585 apply_autocmds(EVENT_BUFREADPOST, 0, 0, FALSE, buf->bufp);
1586#endif
1587
1588 /* handle any postponed key commands */
1589 handle_key_queue();
1590/* =====================================================================*/
1591 }
1592 else if (streq((char *)cmd, "setBufferNumber")
1593 || streq((char *)cmd, "putBufferNumber"))
1594 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00001595 char_u *path;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 buf_T *bufp;
1597
1598 if (buf == NULL)
1599 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001600 nbdebug((" invalid buffer identifier in setBufferNumber\n"));
1601 EMSG("E641: invalid buffer identifier in setBufferNumber");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 return FAIL;
1603 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001604 path = (char_u *)nb_unquote(args, NULL);
1605 if (path == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 return FAIL;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001607 bufp = buflist_findname(path);
1608 vim_free(path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 if (bufp == NULL)
1610 {
Bram Moolenaarec906222009-02-21 21:14:00 +00001611 nbdebug((" File %s not found in setBufferNumber\n", args));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 EMSG2("E642: File %s not found in setBufferNumber", args);
1613 return FAIL;
1614 }
1615 buf->bufp = bufp;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001616 buf->nbbuf_number = bufp->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617
1618 /* "setBufferNumber" has the side effect of jumping to the buffer
1619 * (don't know why!). Don't do that for "putBufferNumber". */
1620 if (*cmd != 'p')
1621 coloncmd(":buffer %d", bufp->b_fnum);
1622 else
1623 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00001624 buf->initDone = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625
1626 /* handle any postponed key commands */
1627 handle_key_queue();
1628 }
1629
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630/* =====================================================================*/
1631 }
1632 else if (streq((char *)cmd, "setFullName"))
1633 {
1634 if (buf == NULL)
1635 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001636 nbdebug((" invalid buffer identifier in setFullName\n"));
1637 EMSG("E643: invalid buffer identifier in setFullName");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 return FAIL;
1639 }
1640 vim_free(buf->displayname);
1641 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642
1643 netbeansReadFile = 0; /* don't try to open disk file */
1644 do_ecmd(0, (char_u *)buf->displayname, 0, 0, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001645 ECMD_HIDE + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 netbeansReadFile = 1;
1647 buf->bufp = curbuf;
1648 maketitle();
Bram Moolenaar67c53842010-05-22 18:28:27 +02001649#if defined(FEAT_MENU) && defined(FEAT_GUI)
Bram Moolenaard54a6882010-08-09 22:49:00 +02001650 if (gui.in_use)
1651 gui_update_menus(0);
Bram Moolenaar67c53842010-05-22 18:28:27 +02001652#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653/* =====================================================================*/
1654 }
1655 else if (streq((char *)cmd, "editFile"))
1656 {
1657 if (buf == NULL)
1658 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001659 nbdebug((" invalid buffer identifier in editFile\n"));
1660 EMSG("E644: invalid buffer identifier in editFile");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 return FAIL;
1662 }
1663 /* Edit a file: like create + setFullName + read the file. */
1664 vim_free(buf->displayname);
1665 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 do_ecmd(0, (char_u *)buf->displayname, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001667 ECMD_HIDE + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 buf->bufp = curbuf;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001669 buf->initDone = TRUE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001670 do_update = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671#if defined(FEAT_TITLE)
1672 maketitle();
1673#endif
Bram Moolenaar67c53842010-05-22 18:28:27 +02001674#if defined(FEAT_MENU) && defined(FEAT_GUI)
Bram Moolenaard54a6882010-08-09 22:49:00 +02001675 if (gui.in_use)
1676 gui_update_menus(0);
Bram Moolenaar67c53842010-05-22 18:28:27 +02001677#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678/* =====================================================================*/
1679 }
1680 else if (streq((char *)cmd, "setVisible"))
1681 {
1682 if (buf == NULL || buf->bufp == NULL)
1683 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001684 nbdebug((" invalid buffer identifier in setVisible\n"));
1685 /* This message was commented out, probably because it can
1686 * happen when shutting down. */
1687 if (p_verbose > 0)
1688 EMSG("E645: invalid buffer identifier in setVisible");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 return FAIL;
1690 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001691 if (streq((char *)args, "T") && buf->bufp != curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 {
1693 exarg_T exarg;
1694 exarg.cmd = (char_u *)"goto";
1695 exarg.forceit = FALSE;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001696 dosetvisible = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 goto_buffer(&exarg, DOBUF_FIRST, FORWARD, buf->bufp->b_fnum);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001698 do_update = 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001699 dosetvisible = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700
Bram Moolenaar67c53842010-05-22 18:28:27 +02001701#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 /* Side effect!!!. */
Bram Moolenaard54a6882010-08-09 22:49:00 +02001703 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 gui_mch_set_foreground();
Bram Moolenaar67c53842010-05-22 18:28:27 +02001705#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707/* =====================================================================*/
1708 }
1709 else if (streq((char *)cmd, "raise"))
1710 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02001711#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 /* Bring gvim to the foreground. */
Bram Moolenaard54a6882010-08-09 22:49:00 +02001713 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 gui_mch_set_foreground();
Bram Moolenaar67c53842010-05-22 18:28:27 +02001715#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716/* =====================================================================*/
1717 }
1718 else if (streq((char *)cmd, "setModified"))
1719 {
Bram Moolenaarff064e12008-06-09 13:10:45 +00001720 int prev_b_changed;
1721
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 if (buf == NULL || buf->bufp == NULL)
1723 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001724 nbdebug((" invalid buffer identifier in setModified\n"));
1725 /* This message was commented out, probably because it can
1726 * happen when shutting down. */
1727 if (p_verbose > 0)
1728 EMSG("E646: invalid buffer identifier in setModified");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 return FAIL;
1730 }
Bram Moolenaarff064e12008-06-09 13:10:45 +00001731 prev_b_changed = buf->bufp->b_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 if (streq((char *)args, "T"))
Bram Moolenaarff064e12008-06-09 13:10:45 +00001733 buf->bufp->b_changed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 else
1735 {
1736 struct stat st;
1737
1738 /* Assume NetBeans stored the file. Reset the timestamp to
1739 * avoid "file changed" warnings. */
1740 if (buf->bufp->b_ffname != NULL
1741 && mch_stat((char *)buf->bufp->b_ffname, &st) >= 0)
1742 buf_store_time(buf->bufp, &st, buf->bufp->b_ffname);
Bram Moolenaarff064e12008-06-09 13:10:45 +00001743 buf->bufp->b_changed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 }
1745 buf->modified = buf->bufp->b_changed;
Bram Moolenaarff064e12008-06-09 13:10:45 +00001746 if (prev_b_changed != buf->bufp->b_changed)
1747 {
1748#ifdef FEAT_WINDOWS
1749 check_status(buf->bufp);
1750 redraw_tabline = TRUE;
1751#endif
1752#ifdef FEAT_TITLE
1753 maketitle();
1754#endif
1755 update_screen(0);
1756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757/* =====================================================================*/
1758 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001759 else if (streq((char *)cmd, "setModtime"))
1760 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001761 if (buf == NULL || buf->bufp == NULL)
Bram Moolenaarf2330482008-06-24 20:19:36 +00001762 nbdebug((" invalid buffer identifier in setModtime\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001763 else
1764 buf->bufp->b_mtime = atoi((char *)args);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001765/* =====================================================================*/
1766 }
1767 else if (streq((char *)cmd, "setReadOnly"))
1768 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001769 if (buf == NULL || buf->bufp == NULL)
Bram Moolenaarf2330482008-06-24 20:19:36 +00001770 nbdebug((" invalid buffer identifier in setReadOnly\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001771 else if (streq((char *)args, "T"))
Bram Moolenaar009b2592004-10-24 19:18:58 +00001772 buf->bufp->b_p_ro = TRUE;
1773 else
1774 buf->bufp->b_p_ro = FALSE;
1775/* =====================================================================*/
1776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 else if (streq((char *)cmd, "setMark"))
1778 {
1779 /* not yet */
1780/* =====================================================================*/
1781 }
1782 else if (streq((char *)cmd, "showBalloon"))
1783 {
1784#if defined(FEAT_BEVAL)
1785 static char *text = NULL;
1786
1787 /*
1788 * Set up the Balloon Expression Evaluation area.
1789 * Ignore 'ballooneval' here.
1790 * The text pointer must remain valid for a while.
1791 */
1792 if (balloonEval != NULL)
1793 {
1794 vim_free(text);
1795 text = nb_unquote(args, NULL);
1796 if (text != NULL)
1797 gui_mch_post_balloon(balloonEval, (char_u *)text);
1798 }
1799#endif
1800/* =====================================================================*/
1801 }
1802 else if (streq((char *)cmd, "setDot"))
1803 {
1804 pos_T *pos;
1805#ifdef NBDEBUG
1806 char_u *s;
1807#endif
1808
1809 if (buf == NULL || buf->bufp == NULL)
1810 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001811 nbdebug((" invalid buffer identifier in setDot\n"));
1812 EMSG("E647: invalid buffer identifier in setDot");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 return FAIL;
1814 }
1815
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001816 nb_set_curbuf(buf->bufp);
1817
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 /* Don't want Visual mode now. */
1819 if (VIsual_active)
1820 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821#ifdef NBDEBUG
1822 s = args;
1823#endif
1824 pos = get_off_or_lnum(buf->bufp, &args);
1825 if (pos)
1826 {
1827 curwin->w_cursor = *pos;
1828 check_cursor();
1829#ifdef FEAT_FOLDING
1830 foldOpenCursor();
1831#endif
1832 }
1833 else
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001834 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 nbdebug((" BAD POSITION in setDot: %s\n", s));
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837
1838 /* gui_update_cursor(TRUE, FALSE); */
1839 /* update_curbuf(NOT_VALID); */
1840 update_topline(); /* scroll to show the line */
1841 update_screen(VALID);
1842 setcursor();
Bram Moolenaar96bcc5e2011-04-01 15:33:59 +02001843 cursor_on();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 out_flush();
Bram Moolenaar67c53842010-05-22 18:28:27 +02001845#ifdef FEAT_GUI
Bram Moolenaard54a6882010-08-09 22:49:00 +02001846 if (gui.in_use)
1847 {
1848 gui_update_cursor(TRUE, FALSE);
1849 gui_mch_flush();
1850 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02001851#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 /* Quit a hit-return or more prompt. */
1853 if (State == HITRETURN || State == ASKMORE)
1854 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855#ifdef FEAT_GUI_GTK
Bram Moolenaard54a6882010-08-09 22:49:00 +02001856 if (gui.in_use && gtk_main_level() > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 gtk_main_quit();
1858#endif
1859 }
1860/* =====================================================================*/
1861 }
1862 else if (streq((char *)cmd, "close"))
1863 {
1864#ifdef NBDEBUG
1865 char *name = "<NONE>";
1866#endif
1867
1868 if (buf == NULL)
1869 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001870 nbdebug((" invalid buffer identifier in close\n"));
1871 EMSG("E648: invalid buffer identifier in close");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 return FAIL;
1873 }
1874
1875#ifdef NBDEBUG
1876 if (buf->displayname != NULL)
1877 name = buf->displayname;
1878#endif
Bram Moolenaarf2330482008-06-24 20:19:36 +00001879 if (buf->bufp == NULL)
1880 {
1881 nbdebug((" invalid buffer identifier in close\n"));
1882 /* This message was commented out, probably because it can
1883 * happen when shutting down. */
1884 if (p_verbose > 0)
1885 EMSG("E649: invalid buffer identifier in close");
1886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 nbdebug((" CLOSE %d: %s\n", bufno, name));
Bram Moolenaar67c53842010-05-22 18:28:27 +02001888#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 need_mouse_correct = TRUE;
Bram Moolenaar67c53842010-05-22 18:28:27 +02001890#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 if (buf->bufp != NULL)
1892 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD,
1893 buf->bufp->b_fnum, TRUE);
Bram Moolenaar8d602722006-08-08 19:34:19 +00001894 buf->bufp = NULL;
1895 buf->initDone = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001896 do_update = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897/* =====================================================================*/
1898 }
1899 else if (streq((char *)cmd, "setStyle")) /* obsolete... */
1900 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001901 nbdebug((" setStyle is obsolete!\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902/* =====================================================================*/
1903 }
1904 else if (streq((char *)cmd, "setExitDelay"))
1905 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00001906 /* Only used in version 2.1. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907/* =====================================================================*/
1908 }
1909 else if (streq((char *)cmd, "defineAnnoType"))
1910 {
1911#ifdef FEAT_SIGNS
1912 int typeNum;
1913 char_u *typeName;
1914 char_u *tooltip;
1915 char_u *p;
1916 char_u *glyphFile;
Bram Moolenaar67c53842010-05-22 18:28:27 +02001917 int parse_error = FALSE;
1918 char_u *fg;
1919 char_u *bg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920
1921 if (buf == NULL)
1922 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001923 nbdebug((" invalid buffer identifier in defineAnnoType\n"));
1924 EMSG("E650: invalid buffer identifier in defineAnnoType");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 return FAIL;
1926 }
1927
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001928 cp = (char *)args;
1929 typeNum = strtol(cp, &cp, 10);
1930 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 args = skipwhite(args);
1932 typeName = (char_u *)nb_unquote(args, &args);
1933 args = skipwhite(args + 1);
1934 tooltip = (char_u *)nb_unquote(args, &args);
1935 args = skipwhite(args + 1);
1936
1937 p = (char_u *)nb_unquote(args, &args);
1938 glyphFile = vim_strsave_escaped(p, escape_chars);
1939 vim_free(p);
1940
1941 args = skipwhite(args + 1);
Bram Moolenaar67c53842010-05-22 18:28:27 +02001942 p = skiptowhite(args);
1943 if (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02001945 *p = NUL;
1946 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02001948 fg = vim_strsave(args);
1949 bg = vim_strsave(p);
1950 if (STRLEN(fg) > MAX_COLOR_LENGTH || STRLEN(bg) > MAX_COLOR_LENGTH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02001952 EMSG("E532: highlighting color name too long in defineAnnoType");
1953 vim_free(typeName);
1954 parse_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02001956 else if (typeName != NULL && tooltip != NULL && glyphFile != NULL)
1957 addsigntype(buf, typeNum, typeName, tooltip, glyphFile, fg, bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 else
1959 vim_free(typeName);
1960
1961 /* don't free typeName; it's used directly in addsigntype() */
Bram Moolenaar67c53842010-05-22 18:28:27 +02001962 vim_free(fg);
1963 vim_free(bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 vim_free(tooltip);
1965 vim_free(glyphFile);
Bram Moolenaar67c53842010-05-22 18:28:27 +02001966 if (parse_error)
1967 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968
1969#endif
1970/* =====================================================================*/
1971 }
1972 else if (streq((char *)cmd, "addAnno"))
1973 {
1974#ifdef FEAT_SIGNS
1975 int serNum;
1976 int localTypeNum;
1977 int typeNum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 pos_T *pos;
1979
1980 if (buf == NULL || buf->bufp == NULL)
1981 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001982 nbdebug((" invalid buffer identifier in addAnno\n"));
1983 EMSG("E651: invalid buffer identifier in addAnno");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 return FAIL;
1985 }
1986
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001987 do_update = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001989 cp = (char *)args;
1990 serNum = strtol(cp, &cp, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991
1992 /* Get the typenr specific for this buffer and convert it to
1993 * the global typenumber, as used for the sign name. */
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001994 localTypeNum = strtol(cp, &cp, 10);
1995 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 typeNum = mapsigntype(buf, localTypeNum);
1997
1998 pos = get_off_or_lnum(buf->bufp, &args);
1999
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002000 cp = (char *)args;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002001 ignored = (int)strtol(cp, &cp, 10);
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002002 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003# ifdef NBDEBUG
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002004 if (ignored != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002006 nbdebug((" partial line annotation -- Not Yet Implemented!\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 }
2008# endif
2009 if (serNum >= GUARDEDOFFSET)
2010 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002011 nbdebug((" too many annotations! ignoring...\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 return FAIL;
2013 }
2014 if (pos)
2015 {
Bram Moolenaarec906222009-02-21 21:14:00 +00002016 coloncmd(":sign place %d line=%ld name=%d buffer=%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 serNum, pos->lnum, typeNum, buf->bufp->b_fnum);
2018 if (typeNum == curPCtype)
2019 coloncmd(":sign jump %d buffer=%d", serNum,
2020 buf->bufp->b_fnum);
2021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022#endif
2023/* =====================================================================*/
2024 }
2025 else if (streq((char *)cmd, "removeAnno"))
2026 {
2027#ifdef FEAT_SIGNS
2028 int serNum;
2029
2030 if (buf == NULL || buf->bufp == NULL)
2031 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002032 nbdebug((" invalid buffer identifier in removeAnno\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 return FAIL;
2034 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002035 do_update = 1;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002036 cp = (char *)args;
2037 serNum = strtol(cp, &cp, 10);
2038 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 coloncmd(":sign unplace %d buffer=%d",
2040 serNum, buf->bufp->b_fnum);
2041 redraw_buf_later(buf->bufp, NOT_VALID);
2042#endif
2043/* =====================================================================*/
2044 }
2045 else if (streq((char *)cmd, "moveAnnoToFront"))
2046 {
2047#ifdef FEAT_SIGNS
Bram Moolenaarf2330482008-06-24 20:19:36 +00002048 nbdebug((" moveAnnoToFront: Not Yet Implemented!\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049#endif
2050/* =====================================================================*/
2051 }
2052 else if (streq((char *)cmd, "guard") || streq((char *)cmd, "unguard"))
2053 {
2054 int len;
2055 pos_T first;
2056 pos_T last;
2057 pos_T *pos;
2058 int un = (cmd[0] == 'u');
2059 static int guardId = GUARDEDOFFSET;
2060
2061 if (skip >= SKIP_STOP)
2062 {
2063 nbdebug((" Skipping %s command\n", (char *) cmd));
2064 return OK;
2065 }
2066
2067 nb_init_graphics();
2068
2069 if (buf == NULL || buf->bufp == NULL)
2070 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002071 nbdebug((" invalid buffer identifier in %s command\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 return FAIL;
2073 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002074 nb_set_curbuf(buf->bufp);
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002075 cp = (char *)args;
2076 off = strtol(cp, &cp, 10);
2077 len = strtol(cp, NULL, 10);
2078 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 pos = off2pos(buf->bufp, off);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002080 do_update = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 if (!pos)
2082 nbdebug((" no such start pos in %s, %ld\n", cmd, off));
2083 else
2084 {
2085 first = *pos;
2086 pos = off2pos(buf->bufp, off + len - 1);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002087 if (pos != NULL && pos->col == 0)
2088 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 /*
2090 * In Java Swing the offset is a position between 2
2091 * characters. If col == 0 then we really want the
2092 * previous line as the end.
2093 */
2094 pos = off2pos(buf->bufp, off + len - 2);
2095 }
2096 if (!pos)
2097 nbdebug((" no such end pos in %s, %ld\n",
2098 cmd, off + len - 1));
2099 else
2100 {
2101 long lnum;
2102 last = *pos;
2103 /* set highlight for region */
2104 nbdebug((" %sGUARD %ld,%d to %ld,%d\n", (un) ? "UN" : "",
2105 first.lnum, first.col,
2106 last.lnum, last.col));
2107#ifdef FEAT_SIGNS
2108 for (lnum = first.lnum; lnum <= last.lnum; lnum++)
2109 {
2110 if (un)
2111 {
2112 /* never used */
2113 }
2114 else
2115 {
2116 if (buf_findsigntype_id(buf->bufp, lnum,
2117 GUARDED) == 0)
2118 {
2119 coloncmd(
Bram Moolenaarec906222009-02-21 21:14:00 +00002120 ":sign place %d line=%ld name=%d buffer=%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 guardId++, lnum, GUARDED,
2122 buf->bufp->b_fnum);
2123 }
2124 }
2125 }
2126#endif
2127 redraw_buf_later(buf->bufp, NOT_VALID);
2128 }
2129 }
2130/* =====================================================================*/
2131 }
2132 else if (streq((char *)cmd, "startAtomic"))
2133 {
2134 inAtomic = 1;
2135/* =====================================================================*/
2136 }
2137 else if (streq((char *)cmd, "endAtomic"))
2138 {
2139 inAtomic = 0;
2140 if (needupdate)
2141 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002142 do_update = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 needupdate = 0;
2144 }
2145/* =====================================================================*/
2146 }
2147 else if (streq((char *)cmd, "save"))
2148 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002149 /*
2150 * NOTE - This command is obsolete wrt NetBeans. Its left in
2151 * only for historical reasons.
2152 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 if (buf == NULL || buf->bufp == NULL)
2154 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002155 nbdebug((" invalid buffer identifier in %s command\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 return FAIL;
2157 }
2158
2159 /* the following is taken from ex_cmds.c (do_wqall function) */
2160 if (bufIsChanged(buf->bufp))
2161 {
2162 /* Only write if the buffer can be written. */
2163 if (p_write
2164 && !buf->bufp->b_p_ro
2165 && buf->bufp->b_ffname != NULL
2166#ifdef FEAT_QUICKFIX
2167 && !bt_dontwrite(buf->bufp)
2168#endif
2169 )
2170 {
2171 buf_write_all(buf->bufp, FALSE);
2172#ifdef FEAT_AUTOCMD
2173 /* an autocommand may have deleted the buffer */
2174 if (!buf_valid(buf->bufp))
2175 buf->bufp = NULL;
2176#endif
2177 }
2178 }
Bram Moolenaarf2330482008-06-24 20:19:36 +00002179 else
2180 {
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002181 nbdebug((" Buffer has no changes!\n"));
Bram Moolenaarf2330482008-06-24 20:19:36 +00002182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183/* =====================================================================*/
2184 }
2185 else if (streq((char *)cmd, "netbeansBuffer"))
2186 {
2187 if (buf == NULL || buf->bufp == NULL)
2188 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002189 nbdebug((" invalid buffer identifier in %s command\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 return FAIL;
2191 }
2192 if (*args == 'T')
2193 {
2194 buf->bufp->b_netbeans_file = TRUE;
2195 buf->bufp->b_was_netbeans_file = TRUE;
2196 }
2197 else
2198 buf->bufp->b_netbeans_file = FALSE;
2199/* =====================================================================*/
2200 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002201 else if (streq((char *)cmd, "specialKeys"))
2202 {
2203 special_keys(args);
2204/* =====================================================================*/
2205 }
2206 else if (streq((char *)cmd, "actionMenuItem"))
2207 {
2208 /* not used yet */
2209/* =====================================================================*/
2210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 else if (streq((char *)cmd, "version"))
2212 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002213 /* not used yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 }
Bram Moolenaarf2330482008-06-24 20:19:36 +00002215 else
2216 {
2217 nbdebug(("Unrecognised command: %s\n", cmd));
2218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 /*
2220 * Unrecognized command is ignored.
2221 */
2222 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002223 if (inAtomic && do_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 {
2225 needupdate = 1;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002226 do_update = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 }
2228
Bram Moolenaar009b2592004-10-24 19:18:58 +00002229 /*
2230 * Is this needed? I moved the netbeans_Xt_connect() later during startup
2231 * and it may no longer be necessary. If its not needed then needupdate
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002232 * and do_update can also be removed.
Bram Moolenaar009b2592004-10-24 19:18:58 +00002233 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002234 if (buf != NULL && buf->initDone && do_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 {
2236 update_screen(NOT_VALID);
2237 setcursor();
Bram Moolenaar96bcc5e2011-04-01 15:33:59 +02002238 cursor_on();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 out_flush();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002240#ifdef FEAT_GUI
Bram Moolenaard54a6882010-08-09 22:49:00 +02002241 if (gui.in_use)
2242 {
2243 gui_update_cursor(TRUE, FALSE);
2244 gui_mch_flush();
2245 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02002246#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 /* Quit a hit-return or more prompt. */
2248 if (State == HITRETURN || State == ASKMORE)
2249 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250#ifdef FEAT_GUI_GTK
Bram Moolenaard54a6882010-08-09 22:49:00 +02002251 if (gui.in_use && gtk_main_level() > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 gtk_main_quit();
2253#endif
2254 }
2255 }
2256
2257 return retval;
2258}
2259
2260
2261/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002262 * If "buf" is not the current buffer try changing to a window that edits this
2263 * buffer. If there is no such window then close the current buffer and set
2264 * the current buffer as "buf".
2265 */
2266 static void
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00002267nb_set_curbuf(buf_T *buf)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002268{
Bram Moolenaar404c9422015-03-14 15:35:52 +01002269 if (curbuf != buf) {
2270 if (buf_jump_open_win(buf) != NULL)
2271 return;
2272# ifdef FEAT_WINDOWS
2273 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf) != NULL)
2274 return;
2275# endif
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002276 set_curbuf(buf, DOBUF_GOTO);
Bram Moolenaar404c9422015-03-14 15:35:52 +01002277 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002278}
2279
2280/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 * Process a vim colon command.
2282 */
2283 static void
2284coloncmd(char *cmd, ...)
2285{
2286 char buf[1024];
2287 va_list ap;
2288
2289 va_start(ap, cmd);
Bram Moolenaar0dc79e82009-06-24 14:50:12 +00002290 vim_vsnprintf(buf, sizeof(buf), cmd, ap, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 va_end(ap);
2292
2293 nbdebug((" COLONCMD %s\n", buf));
2294
2295/* ALT_INPUT_LOCK_ON; */
2296 do_cmdline((char_u *)buf, NULL, NULL, DOCMD_NOWAIT | DOCMD_KEYTYPED);
2297/* ALT_INPUT_LOCK_OFF; */
2298
2299 setcursor(); /* restore the cursor position */
2300 out_flush(); /* make sure output has been written */
2301
Bram Moolenaar67c53842010-05-22 18:28:27 +02002302#ifdef FEAT_GUI
Bram Moolenaard54a6882010-08-09 22:49:00 +02002303 if (gui.in_use)
2304 {
2305 gui_update_cursor(TRUE, FALSE);
2306 gui_mch_flush();
2307 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02002308#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309}
2310
2311
2312/*
Bram Moolenaar009b2592004-10-24 19:18:58 +00002313 * Parse the specialKeys argument and issue the appropriate map commands.
2314 */
2315 static void
2316special_keys(char_u *args)
2317{
2318 char *save_str = nb_unquote(args, NULL);
2319 char *tok = strtok(save_str, " ");
2320 char *sep;
2321 char keybuf[64];
2322 char cmdbuf[256];
2323
2324 while (tok != NULL)
2325 {
2326 int i = 0;
2327
2328 if ((sep = strchr(tok, '-')) != NULL)
2329 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002330 *sep = NUL;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002331 while (*tok)
2332 {
2333 switch (*tok)
2334 {
2335 case 'A':
2336 case 'M':
2337 case 'C':
2338 case 'S':
2339 keybuf[i++] = *tok;
2340 keybuf[i++] = '-';
2341 break;
2342 }
2343 tok++;
2344 }
2345 tok++;
2346 }
2347
2348 strcpy(&keybuf[i], tok);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002349 vim_snprintf(cmdbuf, sizeof(cmdbuf),
2350 "<silent><%s> :nbkey %s<CR>", keybuf, keybuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002351 do_map(0, (char_u *)cmdbuf, NORMAL, FALSE);
2352 tok = strtok(NULL, " ");
2353 }
2354 vim_free(save_str);
2355}
2356
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002357 void
2358ex_nbclose(eap)
2359 exarg_T *eap UNUSED;
2360{
2361 netbeans_close();
2362}
Bram Moolenaar009b2592004-10-24 19:18:58 +00002363
2364 void
2365ex_nbkey(eap)
2366 exarg_T *eap;
2367{
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002368 (void)netbeans_keystring(eap->arg);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002369}
2370
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002371 void
2372ex_nbstart(eap)
2373 exarg_T *eap;
2374{
Bram Moolenaarc2a406b2010-09-30 21:03:26 +02002375#ifdef FEAT_GUI
2376# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar7ad7d012010-11-16 15:49:02 +01002377 && !defined(FEAT_GUI_W32)
Bram Moolenaarc2a406b2010-09-30 21:03:26 +02002378 if (gui.in_use)
2379 {
Bram Moolenaar7ad7d012010-11-16 15:49:02 +01002380 EMSG(_("E838: netbeans is not supported with this GUI"));
2381 return;
Bram Moolenaarc2a406b2010-09-30 21:03:26 +02002382 }
2383# endif
2384#endif
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002385 netbeans_open((char *)eap->arg, FALSE);
2386}
Bram Moolenaar009b2592004-10-24 19:18:58 +00002387
2388/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 * Initialize highlights and signs for use by netbeans (mostly obsolete)
2390 */
2391 static void
2392nb_init_graphics(void)
2393{
2394 static int did_init = FALSE;
2395
2396 if (!did_init)
2397 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002398 coloncmd(":highlight NBGuarded guibg=Cyan guifg=Black"
2399 " ctermbg=LightCyan ctermfg=Black");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 coloncmd(":sign define %d linehl=NBGuarded", GUARDED);
2401
2402 did_init = TRUE;
2403 }
2404}
2405
2406/*
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002407 * Convert key to netbeans name. This uses the global "mod_mask".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 */
2409 static void
2410netbeans_keyname(int key, char *buf)
2411{
2412 char *name = 0;
2413 char namebuf[2];
2414 int ctrl = 0;
2415 int shift = 0;
2416 int alt = 0;
2417
2418 if (mod_mask & MOD_MASK_CTRL)
2419 ctrl = 1;
2420 if (mod_mask & MOD_MASK_SHIFT)
2421 shift = 1;
2422 if (mod_mask & MOD_MASK_ALT)
2423 alt = 1;
2424
2425
2426 switch (key)
2427 {
2428 case K_F1: name = "F1"; break;
2429 case K_S_F1: name = "F1"; shift = 1; break;
2430 case K_F2: name = "F2"; break;
2431 case K_S_F2: name = "F2"; shift = 1; break;
2432 case K_F3: name = "F3"; break;
2433 case K_S_F3: name = "F3"; shift = 1; break;
2434 case K_F4: name = "F4"; break;
2435 case K_S_F4: name = "F4"; shift = 1; break;
2436 case K_F5: name = "F5"; break;
2437 case K_S_F5: name = "F5"; shift = 1; break;
2438 case K_F6: name = "F6"; break;
2439 case K_S_F6: name = "F6"; shift = 1; break;
2440 case K_F7: name = "F7"; break;
2441 case K_S_F7: name = "F7"; shift = 1; break;
2442 case K_F8: name = "F8"; break;
2443 case K_S_F8: name = "F8"; shift = 1; break;
2444 case K_F9: name = "F9"; break;
2445 case K_S_F9: name = "F9"; shift = 1; break;
2446 case K_F10: name = "F10"; break;
2447 case K_S_F10: name = "F10"; shift = 1; break;
2448 case K_F11: name = "F11"; break;
2449 case K_S_F11: name = "F11"; shift = 1; break;
2450 case K_F12: name = "F12"; break;
2451 case K_S_F12: name = "F12"; shift = 1; break;
2452 default:
2453 if (key >= ' ' && key <= '~')
2454 {
2455 /* Allow ASCII characters. */
2456 name = namebuf;
2457 namebuf[0] = key;
2458 namebuf[1] = NUL;
2459 }
2460 else
2461 name = "X";
2462 break;
2463 }
2464
2465 buf[0] = '\0';
2466 if (ctrl)
2467 strcat(buf, "C");
2468 if (shift)
2469 strcat(buf, "S");
2470 if (alt)
2471 strcat(buf, "M"); /* META */
2472 if (ctrl || shift || alt)
2473 strcat(buf, "-");
2474 strcat(buf, name);
2475}
2476
Bram Moolenaar67c53842010-05-22 18:28:27 +02002477#if defined(FEAT_BEVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478/*
2479 * Function to be called for balloon evaluation. Grabs the text under the
2480 * cursor and sends it to the debugger for evaluation. The debugger should
2481 * respond with a showBalloon command when there is a useful result.
2482 */
Bram Moolenaard62bec82005-03-07 22:56:57 +00002483 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484netbeans_beval_cb(
2485 BalloonEval *beval,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002486 int state UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487{
Bram Moolenaard62bec82005-03-07 22:56:57 +00002488 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 char_u *text;
Bram Moolenaard62bec82005-03-07 22:56:57 +00002490 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 int col;
Bram Moolenaard9462e32011-04-11 21:35:11 +02002492 char *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 char_u *p;
2494
2495 /* Don't do anything when 'ballooneval' is off, messages scrolled the
2496 * windows up or we have no connection. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002497 if (!p_beval || msg_scrolled > 0 || !NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 return;
2499
Bram Moolenaard62bec82005-03-07 22:56:57 +00002500 if (get_beval_info(beval, TRUE, &wp, &lnum, &text, &col) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 {
2502 /* Send debugger request. Only when the text is of reasonable
2503 * length. */
2504 if (text != NULL && text[0] != NUL && STRLEN(text) < MAXPATHL)
2505 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02002506 buf = (char *)alloc(MAXPATHL * 2 + 25);
2507 if (buf != NULL)
Bram Moolenaar009b2592004-10-24 19:18:58 +00002508 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02002509 p = nb_quote(text);
2510 if (p != NULL)
2511 {
2512 vim_snprintf(buf, MAXPATHL * 2 + 25,
2513 "0:balloonText=%d \"%s\"\n", r_cmdno, p);
2514 vim_free(p);
2515 }
2516 nbdebug(("EVT: %s", buf));
2517 nb_send(buf, "netbeans_beval_cb");
2518 vim_free(buf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 }
2521 vim_free(text);
2522 }
2523}
2524#endif
2525
2526/*
Bram Moolenaare0874f82016-01-24 20:36:41 +01002527 * Return TRUE when the netbeans connection is active.
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002528 */
2529 int
2530netbeans_active(void)
2531{
2532 return NETBEANS_OPEN;
2533}
2534
Bram Moolenaar67c53842010-05-22 18:28:27 +02002535/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 * Tell netbeans that the window was opened, ready for commands.
2537 */
2538 void
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02002539netbeans_open(char *params, int doabort)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540{
2541 char *cmd = "0:startupDone=0\n";
2542
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002543 if (NETBEANS_OPEN)
2544 {
2545 EMSG(_("E511: netbeans already connected"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02002549 if (netbeans_connect(params, doabort) != OK)
Bram Moolenaar67c53842010-05-22 18:28:27 +02002550 return;
Bram Moolenaard62bec82005-03-07 22:56:57 +00002551
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 nbdebug(("EVT: %s", cmd));
2553 nb_send(cmd, "netbeans_startup_done");
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002554
2555 /* update the screen after having added the gutter */
2556 changed_window_setting();
2557 update_screen(CLEAR);
2558 setcursor();
Bram Moolenaar96bcc5e2011-04-01 15:33:59 +02002559 cursor_on();
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002560 out_flush();
2561#ifdef FEAT_GUI
Bram Moolenaard54a6882010-08-09 22:49:00 +02002562 if (gui.in_use)
2563 {
2564 gui_update_cursor(TRUE, FALSE);
2565 gui_mch_flush();
2566 }
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002567#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568}
2569
Bram Moolenaar009b2592004-10-24 19:18:58 +00002570/*
2571 * Tell netbeans that we're exiting. This should be called right
2572 * before calling exit.
2573 */
2574 void
2575netbeans_send_disconnect()
2576{
2577 char buf[128];
2578
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002579 if (NETBEANS_OPEN)
Bram Moolenaar009b2592004-10-24 19:18:58 +00002580 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002581 sprintf(buf, "0:disconnect=%d\n", r_cmdno);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002582 nbdebug(("EVT: %s", buf));
2583 nb_send(buf, "netbeans_disconnect");
2584 }
2585}
2586
Bram Moolenaar173c9852010-09-29 17:27:01 +02002587#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_W32) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588/*
2589 * Tell netbeans that the window was moved or resized.
2590 */
2591 void
2592netbeans_frame_moved(int new_x, int new_y)
2593{
2594 char buf[128];
2595
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002596 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 return;
2598
2599 sprintf(buf, "0:geometry=%d %d %d %d %d\n",
Bram Moolenaar89d40322006-08-29 15:30:07 +00002600 r_cmdno, (int)Columns, (int)Rows, new_x, new_y);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002601 /*nbdebug(("EVT: %s", buf)); happens too many times during a move */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 nb_send(buf, "netbeans_frame_moved");
2603}
2604#endif
2605
2606/*
Bram Moolenaar009b2592004-10-24 19:18:58 +00002607 * Tell netbeans the user opened or activated a file.
2608 */
2609 void
2610netbeans_file_activated(buf_T *bufp)
2611{
2612 int bufno = nb_getbufno(bufp);
2613 nbbuf_T *bp = nb_get_buf(bufno);
2614 char buffer[2*MAXPATHL];
2615 char_u *q;
2616
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002617 if (!NETBEANS_OPEN || !bufp->b_netbeans_file || dosetvisible)
Bram Moolenaar009b2592004-10-24 19:18:58 +00002618 return;
2619
2620 q = nb_quote(bufp->b_ffname);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002621 if (q == NULL || bp == NULL)
Bram Moolenaar009b2592004-10-24 19:18:58 +00002622 return;
2623
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002624 vim_snprintf(buffer, sizeof(buffer), "%d:fileOpened=%d \"%s\" %s %s\n",
Bram Moolenaar009b2592004-10-24 19:18:58 +00002625 bufno,
2626 bufno,
2627 (char *)q,
2628 "T", /* open in NetBeans */
2629 "F"); /* modified */
2630
2631 vim_free(q);
2632 nbdebug(("EVT: %s", buffer));
2633
2634 nb_send(buffer, "netbeans_file_opened");
2635}
2636
2637/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 * Tell netbeans the user opened a file.
2639 */
2640 void
Bram Moolenaar009b2592004-10-24 19:18:58 +00002641netbeans_file_opened(buf_T *bufp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642{
Bram Moolenaar009b2592004-10-24 19:18:58 +00002643 int bufno = nb_getbufno(bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 char buffer[2*MAXPATHL];
2645 char_u *q;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002646 nbbuf_T *bp = nb_get_buf(nb_getbufno(bufp));
2647 int bnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002649 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 return;
2651
Bram Moolenaar009b2592004-10-24 19:18:58 +00002652 q = nb_quote(bufp->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 if (q == NULL)
2654 return;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002655 if (bp != NULL)
2656 bnum = bufno;
2657 else
2658 bnum = 0;
2659
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002660 vim_snprintf(buffer, sizeof(buffer), "%d:fileOpened=%d \"%s\" %s %s\n",
Bram Moolenaar009b2592004-10-24 19:18:58 +00002661 bnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 0,
2663 (char *)q,
2664 "T", /* open in NetBeans */
2665 "F"); /* modified */
2666
2667 vim_free(q);
2668 nbdebug(("EVT: %s", buffer));
2669
2670 nb_send(buffer, "netbeans_file_opened");
Bram Moolenaar009b2592004-10-24 19:18:58 +00002671 if (p_acd && vim_chdirfile(bufp->b_ffname) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 shorten_fnames(TRUE);
2673}
2674
2675/*
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00002676 * Tell netbeans that a file was deleted or wiped out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 */
2678 void
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00002679netbeans_file_killed(buf_T *bufp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680{
2681 int bufno = nb_getbufno(bufp);
2682 nbbuf_T *nbbuf = nb_get_buf(bufno);
2683 char buffer[2*MAXPATHL];
2684
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002685 if (!NETBEANS_OPEN || bufno == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 return;
2687
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00002688 nbdebug(("netbeans_file_killed:\n"));
2689 nbdebug((" Killing bufno: %d", bufno));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690
Bram Moolenaar89d40322006-08-29 15:30:07 +00002691 sprintf(buffer, "%d:killed=%d\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692
2693 nbdebug(("EVT: %s", buffer));
2694
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00002695 nb_send(buffer, "netbeans_file_killed");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696
2697 if (nbbuf != NULL)
2698 nbbuf->bufp = NULL;
2699}
2700
2701/*
2702 * Get a pointer to the Netbeans buffer for Vim buffer "bufp".
2703 * Return NULL if there is no such buffer or changes are not to be reported.
2704 * Otherwise store the buffer number in "*bufnop".
2705 */
2706 static nbbuf_T *
2707nb_bufp2nbbuf_fire(buf_T *bufp, int *bufnop)
2708{
2709 int bufno;
2710 nbbuf_T *nbbuf;
2711
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002712 if (!NETBEANS_OPEN || !netbeansFireChanges)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 return NULL; /* changes are not reported at all */
2714
2715 bufno = nb_getbufno(bufp);
2716 if (bufno <= 0)
2717 return NULL; /* file is not known to NetBeans */
2718
2719 nbbuf = nb_get_buf(bufno);
2720 if (nbbuf != NULL && !nbbuf->fireChanges)
2721 return NULL; /* changes in this buffer are not reported */
2722
2723 *bufnop = bufno;
2724 return nbbuf;
2725}
2726
2727/*
2728 * Tell netbeans the user inserted some text.
2729 */
2730 void
2731netbeans_inserted(
2732 buf_T *bufp,
2733 linenr_T linenr,
2734 colnr_T col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 char_u *txt,
2736 int newlen)
2737{
2738 char_u *buf;
2739 int bufno;
2740 nbbuf_T *nbbuf;
2741 pos_T pos;
2742 long off;
2743 char_u *p;
2744 char_u *newtxt;
2745
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002746 if (!NETBEANS_OPEN)
2747 return;
2748
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
2750 if (nbbuf == NULL)
2751 return;
2752
Bram Moolenaar009b2592004-10-24 19:18:58 +00002753 /* Don't mark as modified for initial read */
2754 if (nbbuf->insertDone)
2755 nbbuf->modified = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756
2757 pos.lnum = linenr;
2758 pos.col = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 off = pos2off(bufp, &pos);
2760
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 /* send the "insert" EVT */
2762 newtxt = alloc(newlen + 1);
Bram Moolenaarb6356332005-07-18 21:40:44 +00002763 vim_strncpy(newtxt, txt, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 p = nb_quote(newtxt);
2765 if (p != NULL)
2766 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002767 buf = alloc(128 + 2*newlen);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002768 sprintf((char *)buf, "%d:insert=%d %ld \"%s\"\n",
2769 bufno, r_cmdno, off, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 nbdebug(("EVT: %s", buf));
2771 nb_send((char *)buf, "netbeans_inserted");
Bram Moolenaar009b2592004-10-24 19:18:58 +00002772 vim_free(p);
2773 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 vim_free(newtxt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776}
2777
2778/*
2779 * Tell netbeans some bytes have been removed.
2780 */
2781 void
2782netbeans_removed(
2783 buf_T *bufp,
2784 linenr_T linenr,
2785 colnr_T col,
2786 long len)
2787{
2788 char_u buf[128];
2789 int bufno;
2790 nbbuf_T *nbbuf;
2791 pos_T pos;
2792 long off;
2793
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002794 if (!NETBEANS_OPEN)
2795 return;
2796
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
2798 if (nbbuf == NULL)
2799 return;
2800
2801 if (len < 0)
2802 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002803 nbdebug(("Negative len %ld in netbeans_removed()!\n", len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 return;
2805 }
2806
2807 nbbuf->modified = 1;
2808
2809 pos.lnum = linenr;
2810 pos.col = col;
2811
2812 off = pos2off(bufp, &pos);
2813
Bram Moolenaar89d40322006-08-29 15:30:07 +00002814 sprintf((char *)buf, "%d:remove=%d %ld %ld\n", bufno, r_cmdno, off, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 nbdebug(("EVT: %s", buf));
2816 nb_send((char *)buf, "netbeans_removed");
2817}
2818
2819/*
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002820 * Send netbeans an unmodified command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002823netbeans_unmodified(buf_T *bufp UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824{
Bram Moolenaar09092152010-08-08 16:38:42 +02002825 /* This is a no-op, because NetBeans considers a buffer modified
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 * even when all changes have been undone. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827}
2828
2829/*
2830 * Send a button release event back to netbeans. Its up to netbeans
2831 * to decide what to do (if anything) with this event.
2832 */
2833 void
2834netbeans_button_release(int button)
2835{
2836 char buf[128];
2837 int bufno;
2838
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002839 if (!NETBEANS_OPEN)
2840 return;
2841
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 bufno = nb_getbufno(curbuf);
2843
2844 if (bufno >= 0 && curwin != NULL && curwin->w_buffer == curbuf)
2845 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002846 int col = mouse_col - W_WINCOL(curwin)
Bram Moolenaar67c53842010-05-22 18:28:27 +02002847 - ((curwin->w_p_nu || curwin->w_p_rnu) ? 9 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 long off = pos2off(curbuf, &curwin->w_cursor);
2849
2850 /* sync the cursor position */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002851 sprintf(buf, "%d:newDotAndMark=%d %ld %ld\n", bufno, r_cmdno, off, off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 nbdebug(("EVT: %s", buf));
2853 nb_send(buf, "netbeans_button_release[newDotAndMark]");
2854
Bram Moolenaar89d40322006-08-29 15:30:07 +00002855 sprintf(buf, "%d:buttonRelease=%d %d %ld %d\n", bufno, r_cmdno,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 button, (long)curwin->w_cursor.lnum, col);
2857 nbdebug(("EVT: %s", buf));
2858 nb_send(buf, "netbeans_button_release");
2859 }
2860}
2861
2862
2863/*
Bram Moolenaar143c38c2007-05-10 16:41:10 +00002864 * Send a keypress event back to netbeans. This usually simulates some
Bram Moolenaar009b2592004-10-24 19:18:58 +00002865 * kind of function key press. This function operates on a key code.
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002866 * Return TRUE when the key was sent, FALSE when the command has been
2867 * postponed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 */
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002869 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870netbeans_keycommand(int key)
2871{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 char keyName[60];
Bram Moolenaar009b2592004-10-24 19:18:58 +00002873
2874 netbeans_keyname(key, keyName);
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002875 return netbeans_keystring((char_u *)keyName);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002876}
2877
2878
2879/*
Bram Moolenaar143c38c2007-05-10 16:41:10 +00002880 * Send a keypress event back to netbeans. This usually simulates some
Bram Moolenaar009b2592004-10-24 19:18:58 +00002881 * kind of function key press. This function operates on a key string.
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002882 * Return TRUE when the key was sent, FALSE when the command has been
2883 * postponed.
Bram Moolenaar009b2592004-10-24 19:18:58 +00002884 */
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002885 static int
2886netbeans_keystring(char_u *keyName)
Bram Moolenaar009b2592004-10-24 19:18:58 +00002887{
2888 char buf[2*MAXPATHL];
2889 int bufno = nb_getbufno(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 long off;
2891 char_u *q;
2892
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002893 if (!NETBEANS_OPEN)
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002894 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 if (bufno == -1)
2897 {
2898 nbdebug(("got keycommand for non-NetBeans buffer, opening...\n"));
2899 q = curbuf->b_ffname == NULL ? (char_u *)""
2900 : nb_quote(curbuf->b_ffname);
2901 if (q == NULL)
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002902 return TRUE;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002903 vim_snprintf(buf, sizeof(buf), "0:fileOpened=%d \"%s\" %s %s\n", 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 q,
2905 "T", /* open in NetBeans */
2906 "F"); /* modified */
2907 if (curbuf->b_ffname != NULL)
2908 vim_free(q);
2909 nbdebug(("EVT: %s", buf));
2910 nb_send(buf, "netbeans_keycommand");
2911
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002912 postpone_keycommand(keyName);
2913 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 }
2915
2916 /* sync the cursor position */
2917 off = pos2off(curbuf, &curwin->w_cursor);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002918 sprintf(buf, "%d:newDotAndMark=%d %ld %ld\n", bufno, r_cmdno, off, off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 nbdebug(("EVT: %s", buf));
2920 nb_send(buf, "netbeans_keycommand");
2921
2922 /* To work on Win32 you must apply patch to ExtEditor module
2923 * from ExtEdCaret.java.diff - make EVT_newDotAndMark handler
2924 * more synchronous
2925 */
2926
2927 /* now send keyCommand event */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002928 vim_snprintf(buf, sizeof(buf), "%d:keyCommand=%d \"%s\"\n",
Bram Moolenaar89d40322006-08-29 15:30:07 +00002929 bufno, r_cmdno, keyName);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 nbdebug(("EVT: %s", buf));
2931 nb_send(buf, "netbeans_keycommand");
2932
2933 /* New: do both at once and include the lnum/col. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002934 vim_snprintf(buf, sizeof(buf), "%d:keyAtPos=%d \"%s\" %ld %ld/%ld\n",
Bram Moolenaar89d40322006-08-29 15:30:07 +00002935 bufno, r_cmdno, keyName,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 off, (long)curwin->w_cursor.lnum, (long)curwin->w_cursor.col);
2937 nbdebug(("EVT: %s", buf));
2938 nb_send(buf, "netbeans_keycommand");
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002939 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940}
2941
2942
2943/*
2944 * Send a save event to netbeans.
2945 */
2946 void
2947netbeans_save_buffer(buf_T *bufp)
2948{
2949 char_u buf[64];
2950 int bufno;
2951 nbbuf_T *nbbuf;
2952
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002953 if (!NETBEANS_OPEN)
2954 return;
2955
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
2957 if (nbbuf == NULL)
2958 return;
2959
2960 nbbuf->modified = 0;
2961
Bram Moolenaar89d40322006-08-29 15:30:07 +00002962 sprintf((char *)buf, "%d:save=%d\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 nbdebug(("EVT: %s", buf));
2964 nb_send((char *)buf, "netbeans_save_buffer");
2965}
2966
2967
2968/*
2969 * Send remove command to netbeans (this command has been turned off).
2970 */
2971 void
2972netbeans_deleted_all_lines(buf_T *bufp)
2973{
2974 char_u buf[64];
2975 int bufno;
2976 nbbuf_T *nbbuf;
2977
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002978 if (!NETBEANS_OPEN)
2979 return;
2980
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
2982 if (nbbuf == NULL)
2983 return;
2984
Bram Moolenaar009b2592004-10-24 19:18:58 +00002985 /* Don't mark as modified for initial read */
2986 if (nbbuf->insertDone)
2987 nbbuf->modified = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988
Bram Moolenaar89d40322006-08-29 15:30:07 +00002989 sprintf((char *)buf, "%d:remove=%d 0 -1\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 nbdebug(("EVT(suppressed): %s", buf));
2991/* nb_send(buf, "netbeans_deleted_all_lines"); */
2992}
2993
2994
2995/*
2996 * See if the lines are guarded. The top and bot parameters are from
2997 * u_savecommon(), these are the line above the change and the line below the
2998 * change.
2999 */
3000 int
3001netbeans_is_guarded(linenr_T top, linenr_T bot)
3002{
3003 signlist_T *p;
3004 int lnum;
3005
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003006 if (!NETBEANS_OPEN)
3007 return FALSE;
3008
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 for (p = curbuf->b_signlist; p != NULL; p = p->next)
3010 if (p->id >= GUARDEDOFFSET)
3011 for (lnum = top + 1; lnum < bot; lnum++)
3012 if (lnum == p->lnum)
3013 return TRUE;
3014
3015 return FALSE;
3016}
3017
Bram Moolenaar173c9852010-09-29 17:27:01 +02003018#if defined(FEAT_GUI_X11) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019/*
3020 * We have multiple signs to draw at the same location. Draw the
3021 * multi-sign indicator instead. This is the Motif version.
3022 */
3023 void
3024netbeans_draw_multisign_indicator(int row)
3025{
3026 int i;
3027 int y;
3028 int x;
3029
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003030 if (!NETBEANS_OPEN)
3031 return;
3032
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 x = 0;
3034 y = row * gui.char_height + 2;
3035
3036 for (i = 0; i < gui.char_height - 3; i++)
3037 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y++);
3038
3039 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+0, y);
3040 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3041 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+4, y++);
3042 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+1, y);
3043 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3044 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+3, y++);
3045 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3046}
Bram Moolenaar173c9852010-09-29 17:27:01 +02003047#endif /* FEAT_GUI_X11 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048
Bram Moolenaar64404472010-06-26 06:24:45 +02003049#if defined(FEAT_GUI_GTK) && !defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050/*
3051 * We have multiple signs to draw at the same location. Draw the
3052 * multi-sign indicator instead. This is the GTK/Gnome version.
3053 */
3054 void
3055netbeans_draw_multisign_indicator(int row)
3056{
3057 int i;
3058 int y;
3059 int x;
3060 GdkDrawable *drawable = gui.drawarea->window;
3061
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003062 if (!NETBEANS_OPEN)
3063 return;
3064
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 x = 0;
3066 y = row * gui.char_height + 2;
3067
3068 for (i = 0; i < gui.char_height - 3; i++)
3069 gdk_draw_point(drawable, gui.text_gc, x+2, y++);
3070
3071 gdk_draw_point(drawable, gui.text_gc, x+0, y);
3072 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3073 gdk_draw_point(drawable, gui.text_gc, x+4, y++);
3074 gdk_draw_point(drawable, gui.text_gc, x+1, y);
3075 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3076 gdk_draw_point(drawable, gui.text_gc, x+3, y++);
3077 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3078}
3079#endif /* FEAT_GUI_GTK */
3080
3081/*
3082 * If the mouse is clicked in the gutter of a line with multiple
3083 * annotations, cycle through the set of signs.
3084 */
3085 void
3086netbeans_gutter_click(linenr_T lnum)
3087{
3088 signlist_T *p;
3089
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003090 if (!NETBEANS_OPEN)
3091 return;
3092
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 for (p = curbuf->b_signlist; p != NULL; p = p->next)
3094 {
3095 if (p->lnum == lnum && p->next && p->next->lnum == lnum)
3096 {
3097 signlist_T *tail;
3098
3099 /* remove "p" from list, reinsert it at the tail of the sublist */
3100 if (p->prev)
3101 p->prev->next = p->next;
3102 else
3103 curbuf->b_signlist = p->next;
3104 p->next->prev = p->prev;
3105 /* now find end of sublist and insert p */
3106 for (tail = p->next;
3107 tail->next && tail->next->lnum == lnum
3108 && tail->next->id < GUARDEDOFFSET;
3109 tail = tail->next)
3110 ;
3111 /* tail now points to last entry with same lnum (except
3112 * that "guarded" annotations are always last) */
3113 p->next = tail->next;
3114 if (tail->next)
3115 tail->next->prev = p;
3116 p->prev = tail;
3117 tail->next = p;
3118 update_debug_sign(curbuf, lnum);
3119 break;
3120 }
3121 }
3122}
3123
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124/*
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003125 * Add a sign of the requested type at the requested location.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 *
3127 * Reverse engineering:
3128 * Apparently an annotation is defined the first time it is used in a buffer.
3129 * When the same annotation is used in two buffers, the second time we do not
3130 * need to define a new sign name but reuse the existing one. But since the
3131 * ID number used in the second buffer starts counting at one again, a mapping
3132 * is made from the ID specifically for the buffer to the global sign name
3133 * (which is a number).
3134 *
3135 * globalsignmap[] stores the signs that have been defined globally.
3136 * buf->signmapused[] maps buffer-local annotation IDs to an index in
3137 * globalsignmap[].
3138 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 static void
3140addsigntype(
3141 nbbuf_T *buf,
3142 int typeNum,
3143 char_u *typeName,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003144 char_u *tooltip UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 char_u *glyphFile,
Bram Moolenaar67c53842010-05-22 18:28:27 +02003146 char_u *fg,
3147 char_u *bg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 int i, j;
Bram Moolenaar67c53842010-05-22 18:28:27 +02003150 int use_fg = (*fg && STRCMP(fg, "none") != 0);
3151 int use_bg = (*bg && STRCMP(bg, "none") != 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152
3153 for (i = 0; i < globalsignmapused; i++)
3154 if (STRCMP(typeName, globalsignmap[i]) == 0)
3155 break;
3156
3157 if (i == globalsignmapused) /* not found; add it to global map */
3158 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003159 nbdebug(("DEFINEANNOTYPE(%d,%s,%s,%s,%s,%s)\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 typeNum, typeName, tooltip, glyphFile, fg, bg));
3161 if (use_fg || use_bg)
3162 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003163 char fgbuf[2 * (8 + MAX_COLOR_LENGTH) + 1];
3164 char bgbuf[2 * (8 + MAX_COLOR_LENGTH) + 1];
3165 char *ptr;
3166 int value;
3167
3168 value = strtol((char *)fg, &ptr, 10);
3169 if (ptr != (char *)fg)
3170 sprintf(fgbuf, "guifg=#%06x", value & 0xFFFFFF);
3171 else
3172 sprintf(fgbuf, "guifg=%s ctermfg=%s", fg, fg);
3173
3174 value = strtol((char *)bg, &ptr, 10);
3175 if (ptr != (char *)bg)
3176 sprintf(bgbuf, "guibg=#%06x", value & 0xFFFFFF);
3177 else
3178 sprintf(bgbuf, "guibg=%s ctermbg=%s", bg, bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179
3180 coloncmd(":highlight NB_%s %s %s", typeName, (use_fg) ? fgbuf : "",
3181 (use_bg) ? bgbuf : "");
3182 if (*glyphFile == NUL)
3183 /* no glyph, line highlighting only */
3184 coloncmd(":sign define %d linehl=NB_%s", i + 1, typeName);
3185 else if (vim_strsize(glyphFile) <= 2)
3186 /* one- or two-character glyph name, use as text glyph with
3187 * texthl */
3188 coloncmd(":sign define %d text=%s texthl=NB_%s", i + 1,
3189 glyphFile, typeName);
3190 else
3191 /* glyph, line highlighting */
3192 coloncmd(":sign define %d icon=%s linehl=NB_%s", i + 1,
3193 glyphFile, typeName);
3194 }
3195 else
3196 /* glyph, no line highlighting */
3197 coloncmd(":sign define %d icon=%s", i + 1, glyphFile);
3198
3199 if (STRCMP(typeName,"CurrentPC") == 0)
3200 curPCtype = typeNum;
3201
3202 if (globalsignmapused == globalsignmaplen)
3203 {
3204 if (globalsignmaplen == 0) /* first allocation */
3205 {
3206 globalsignmaplen = 20;
3207 globalsignmap = (char **)alloc_clear(globalsignmaplen*sizeof(char *));
3208 }
3209 else /* grow it */
3210 {
3211 int incr;
3212 int oldlen = globalsignmaplen;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003213 char **t_globalsignmap = globalsignmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214
3215 globalsignmaplen *= 2;
3216 incr = globalsignmaplen - oldlen;
3217 globalsignmap = (char **)vim_realloc(globalsignmap,
3218 globalsignmaplen * sizeof(char *));
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003219 if (globalsignmap == NULL)
3220 {
3221 vim_free(t_globalsignmap);
3222 globalsignmaplen = 0;
3223 return;
3224 }
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003225 vim_memset(globalsignmap + oldlen, 0, incr * sizeof(char *));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 }
3227 }
3228
3229 globalsignmap[i] = (char *)typeName;
3230 globalsignmapused = i + 1;
3231 }
3232
3233 /* check local map; should *not* be found! */
3234 for (j = 0; j < buf->signmapused; j++)
3235 if (buf->signmap[j] == i + 1)
3236 return;
3237
3238 /* add to local map */
3239 if (buf->signmapused == buf->signmaplen)
3240 {
3241 if (buf->signmaplen == 0) /* first allocation */
3242 {
3243 buf->signmaplen = 5;
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003244 buf->signmap = (int *)alloc_clear(buf->signmaplen * sizeof(int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 }
3246 else /* grow it */
3247 {
3248 int incr;
3249 int oldlen = buf->signmaplen;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003250 int *t_signmap = buf->signmap;
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003251
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 buf->signmaplen *= 2;
3253 incr = buf->signmaplen - oldlen;
3254 buf->signmap = (int *)vim_realloc(buf->signmap,
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003255 buf->signmaplen * sizeof(int));
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003256 if (buf->signmap == NULL)
3257 {
3258 vim_free(t_signmap);
3259 buf->signmaplen = 0;
3260 return;
3261 }
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003262 vim_memset(buf->signmap + oldlen, 0, incr * sizeof(int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 }
3264 }
3265
3266 buf->signmap[buf->signmapused++] = i + 1;
3267
3268}
3269
3270
3271/*
3272 * See if we have the requested sign type in the buffer.
3273 */
3274 static int
3275mapsigntype(nbbuf_T *buf, int localsigntype)
3276{
3277 if (--localsigntype >= 0 && localsigntype < buf->signmapused)
3278 return buf->signmap[localsigntype];
3279
3280 return 0;
3281}
3282
3283
3284/*
3285 * Compute length of buffer, don't print anything.
3286 */
3287 static long
3288get_buf_size(buf_T *bufp)
3289{
3290 linenr_T lnum;
3291 long char_count = 0;
3292 int eol_size;
3293 long last_check = 100000L;
3294
3295 if (bufp->b_ml.ml_flags & ML_EMPTY)
3296 return 0;
3297 else
3298 {
3299 if (get_fileformat(bufp) == EOL_DOS)
3300 eol_size = 2;
3301 else
3302 eol_size = 1;
3303 for (lnum = 1; lnum <= bufp->b_ml.ml_line_count; ++lnum)
3304 {
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00003305 char_count += (long)STRLEN(ml_get_buf(bufp, lnum, FALSE))
3306 + eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 /* Check for a CTRL-C every 100000 characters */
3308 if (char_count > last_check)
3309 {
3310 ui_breakcheck();
3311 if (got_int)
3312 return char_count;
3313 last_check = char_count + 100000L;
3314 }
3315 }
3316 /* Correction for when last line doesn't have an EOL. */
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003317 if (!bufp->b_p_eol && (bufp->b_p_bin || !bufp->b_p_fixeol))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 char_count -= eol_size;
3319 }
3320
3321 return char_count;
3322}
3323
3324/*
3325 * Convert character offset to lnum,col
3326 */
3327 static pos_T *
3328off2pos(buf_T *buf, long offset)
3329{
3330 linenr_T lnum;
3331 static pos_T pos;
3332
3333 pos.lnum = 0;
3334 pos.col = 0;
3335#ifdef FEAT_VIRTUALEDIT
3336 pos.coladd = 0;
3337#endif
3338
3339 if (!(buf->b_ml.ml_flags & ML_EMPTY))
3340 {
3341 if ((lnum = ml_find_line_or_offset(buf, (linenr_T)0, &offset)) < 0)
3342 return NULL;
3343 pos.lnum = lnum;
3344 pos.col = offset;
3345 }
3346
3347 return &pos;
3348}
3349
3350/*
3351 * Convert an argument in the form "1234" to an offset and compute the
3352 * lnum/col from it. Convert an argument in the form "123/12" directly to a
3353 * lnum/col.
3354 * "argp" is advanced to after the argument.
3355 * Return a pointer to the position, NULL if something is wrong.
3356 */
3357 static pos_T *
3358get_off_or_lnum(buf_T *buf, char_u **argp)
3359{
3360 static pos_T mypos;
3361 long off;
3362
3363 off = strtol((char *)*argp, (char **)argp, 10);
3364 if (**argp == '/')
3365 {
3366 mypos.lnum = (linenr_T)off;
3367 ++*argp;
3368 mypos.col = strtol((char *)*argp, (char **)argp, 10);
3369#ifdef FEAT_VIRTUALEDIT
3370 mypos.coladd = 0;
3371#endif
3372 return &mypos;
3373 }
3374 return off2pos(buf, off);
3375}
3376
3377
3378/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003379 * Convert (lnum,col) to byte offset in the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 */
3381 static long
3382pos2off(buf_T *buf, pos_T *pos)
3383{
3384 long offset = 0;
3385
3386 if (!(buf->b_ml.ml_flags & ML_EMPTY))
3387 {
3388 if ((offset = ml_find_line_or_offset(buf, pos->lnum, 0)) < 0)
3389 return 0;
3390 offset += pos->col;
3391 }
3392
3393 return offset;
3394}
3395
3396
Bram Moolenaar009b2592004-10-24 19:18:58 +00003397/*
3398 * This message is printed after NetBeans opens a new file. Its
3399 * similar to the message readfile() uses, but since NetBeans
3400 * doesn't normally call readfile, we do our own.
3401 */
3402 static void
3403print_read_msg(buf)
3404 nbbuf_T *buf;
3405{
3406 int lnum = buf->bufp->b_ml.ml_line_count;
Bram Moolenaar914703b2010-05-31 21:59:46 +02003407 off_t nchars = buf->bufp->b_orig_size;
Bram Moolenaar009b2592004-10-24 19:18:58 +00003408 char_u c;
3409
3410 msg_add_fname(buf->bufp, buf->bufp->b_ffname);
3411 c = FALSE;
3412
3413 if (buf->bufp->b_p_ro)
3414 {
3415 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]"));
3416 c = TRUE;
3417 }
3418 if (!buf->bufp->b_start_eol)
3419 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003420 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]")
3421 : _("[Incomplete last line]"));
Bram Moolenaar009b2592004-10-24 19:18:58 +00003422 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/*
Bram Moolenaar67c53842010-05-22 18:28:27 +02003436 * Print a message after NetBeans writes the file. This message should be
3437 * identical to the standard message a non-netbeans user would see when
3438 * writing a file.
Bram Moolenaar009b2592004-10-24 19:18:58 +00003439 */
3440 static void
3441print_save_msg(buf, nchars)
3442 nbbuf_T *buf;
Bram Moolenaar914703b2010-05-31 21:59:46 +02003443 off_t nchars;
Bram Moolenaar009b2592004-10-24 19:18:58 +00003444{
3445 char_u c;
3446 char_u *p;
3447
3448 if (nchars >= 0)
3449 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003450 /* put fname in IObuff with quotes */
3451 msg_add_fname(buf->bufp, buf->bufp->b_ffname);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003452 c = FALSE;
3453
3454 msg_add_lines(c, buf->bufp->b_ml.ml_line_count,
Bram Moolenaar914703b2010-05-31 21:59:46 +02003455 buf->bufp->b_orig_size);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003456
3457 vim_free(keep_msg);
3458 keep_msg = NULL;
3459 msg_scrolled_ign = TRUE;
3460 p = msg_trunc_attr(IObuff, FALSE, 0);
3461 if ((msg_scrolled && !need_wait_return) || !buf->initDone)
3462 {
3463 /* Need to repeat the message after redrawing when:
3464 * - When reading from stdin (the screen will be cleared next).
3465 * - When restart_edit is set (otherwise there will be a delay
3466 * before redrawing).
3467 * - When the screen was scrolled but there is no wait-return
3468 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003469 set_keep_msg(p, 0);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003470 }
3471 msg_scrolled_ign = FALSE;
3472 /* add_to_input_buf((char_u *)"\f", 1); */
3473 }
3474 else
3475 {
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003476 char_u msgbuf[IOSIZE];
Bram Moolenaar009b2592004-10-24 19:18:58 +00003477
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003478 vim_snprintf((char *)msgbuf, IOSIZE,
3479 _("E505: %s is read-only (add ! to override)"), IObuff);
3480 nbdebug((" %s\n", msgbuf));
3481 emsg(msgbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003482 }
3483}
3484
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485#endif /* defined(FEAT_NETBEANS_INTG) */