blob: bc3b8e680b7d335102fbdfea781284d435c14533 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010047static long pos2off(buf_T *, pos_T *);
48static pos_T *off2pos(buf_T *, long);
49static pos_T *get_off_or_lnum(buf_T *buf, char_u **argp);
50static long get_buf_size(buf_T *);
51static int netbeans_keystring(char_u *keystr);
52static void postpone_keycommand(char_u *keystr);
53static void special_keys(char_u *args);
Bram Moolenaar071d4272004-06-13 20:20:40 +000054
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010055static int netbeans_connect(char *, int);
56static int getConnInfo(char *file, char **host, char **port, char **password);
Bram Moolenaar071d4272004-06-13 20:20:40 +000057
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010058static void nb_init_graphics(void);
59static void coloncmd(char *cmd, ...);
60static void nb_set_curbuf(buf_T *buf);
61static void nb_parse_cmd(char_u *);
62static int nb_do_cmd(int, char_u *, int, int, char_u *);
63static void nb_send(char *buf, char *fun);
64static void nb_free(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000065
Bram Moolenaar77073442016-02-13 23:23:53 +010066#define NETBEANS_OPEN (channel_can_write_to(nb_channel))
67static channel_T *nb_channel = NULL;
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 Moolenaar77073442016-02-13 23:23:53 +010088 nb_channel = NULL;
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 Moolenaar77073442016-02-13 23:23:53 +0100101 if (nb_channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +0100102 {
Bram Moolenaard04a0202016-01-26 23:30:18 +0100103 /* Close the socket and remove the input handlers. */
Bram Moolenaar8b374212016-02-24 20:43:06 +0100104 channel_close(nb_channel, TRUE);
Bram Moolenaar187db502016-02-27 14:44:26 +0100105 channel_clear(nb_channel);
106 }
Bram Moolenaar77073442016-02-13 23:23:53 +0100107 nb_channel = NULL;
Bram Moolenaar7ad7d012010-11-16 15:49:02 +0100108 }
109
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100110#ifdef FEAT_BEVAL_GUI
Bram Moolenaard62bec82005-03-07 22:56:57 +0000111 bevalServers &= ~BEVAL_NETBEANS;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200112#endif
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200113
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200114 needupdate = 0;
115 inAtomic = 0;
116 nb_free();
117
118 /* remove all signs and update the screen after gutter removal */
119 coloncmd(":sign unplace *");
120 changed_window_setting();
121 update_screen(CLEAR);
122 setcursor();
Bram Moolenaar96bcc5e2011-04-01 15:33:59 +0200123 cursor_on();
Bram Moolenaara338adc2018-01-31 20:51:47 +0100124 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126
127#define NB_DEF_HOST "localhost"
128#define NB_DEF_ADDR "3219"
129#define NB_DEF_PASS "changeme"
130
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200131 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200132netbeans_connect(char *params, int doabort)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133{
Bram Moolenaard04a0202016-01-26 23:30:18 +0100134 int port;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135 char buf[32];
136 char *hostname = NULL;
137 char *address = NULL;
138 char *password = NULL;
139 char *fname;
140 char *arg = NULL;
141
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200142 if (*params == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200144 /* "=fname": Read info from specified file. */
Bram Moolenaare0874f82016-01-24 20:36:41 +0100145 if (getConnInfo(params + 1, &hostname, &address, &password) == FAIL)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200146 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147 }
148 else
149 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200150 if (*params == ':')
151 /* ":<host>:<addr>:<password>": get info from argument */
152 arg = params + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153 if (arg == NULL && (fname = getenv("__NETBEANS_CONINFO")) != NULL)
154 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200155 /* "": get info from file specified in environment */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156 if (getConnInfo(fname, &hostname, &address, &password) == FAIL)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200157 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158 }
159 else
160 {
161 if (arg != NULL)
162 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200163 /* ":<host>:<addr>:<password>": get info from argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164 hostname = arg;
165 address = strchr(hostname, ':');
166 if (address != NULL)
167 {
168 *address++ = '\0';
169 password = strchr(address, ':');
170 if (password != NULL)
171 *password++ = '\0';
172 }
173 }
174
175 /* Get the missing values from the environment. */
176 if (hostname == NULL || *hostname == '\0')
177 hostname = getenv("__NETBEANS_HOST");
178 if (address == NULL)
179 address = getenv("__NETBEANS_SOCKET");
180 if (password == NULL)
181 password = getenv("__NETBEANS_VIM_PASSWORD");
182
183 /* Move values to allocated memory. */
184 if (hostname != NULL)
185 hostname = (char *)vim_strsave((char_u *)hostname);
186 if (address != NULL)
187 address = (char *)vim_strsave((char_u *)address);
188 if (password != NULL)
189 password = (char *)vim_strsave((char_u *)password);
190 }
191 }
192
193 /* Use the default when a value is missing. */
194 if (hostname == NULL || *hostname == '\0')
195 {
196 vim_free(hostname);
197 hostname = (char *)vim_strsave((char_u *)NB_DEF_HOST);
198 }
199 if (address == NULL || *address == '\0')
200 {
201 vim_free(address);
202 address = (char *)vim_strsave((char_u *)NB_DEF_ADDR);
203 }
204 if (password == NULL || *password == '\0')
205 {
206 vim_free(password);
207 password = (char *)vim_strsave((char_u *)NB_DEF_PASS);
208 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100209 if (hostname != NULL && address != NULL && password != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210 {
Bram Moolenaard04a0202016-01-26 23:30:18 +0100211 port = atoi(address);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100212 nb_channel = channel_open(hostname, port, 3000, nb_channel_closed);
Bram Moolenaar77073442016-02-13 23:23:53 +0100213 if (nb_channel != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214 {
Bram Moolenaard04a0202016-01-26 23:30:18 +0100215 /* success */
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100216# ifdef FEAT_BEVAL_GUI
Bram Moolenaard04a0202016-01-26 23:30:18 +0100217 bevalServers |= BEVAL_NETBEANS;
218# endif
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200219
Bram Moolenaard04a0202016-01-26 23:30:18 +0100220 /* success, login */
221 vim_snprintf(buf, sizeof(buf), "AUTH %s\n", password);
222 nb_send(buf, "netbeans_connect");
223
224 sprintf(buf, "0:version=0 \"%s\"\n", ExtEdProtocolVersion);
225 nb_send(buf, "externaleditor_version");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226 }
227 }
228
Bram Moolenaar77073442016-02-13 23:23:53 +0100229 if (nb_channel == NULL && doabort)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100230 getout(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232 vim_free(hostname);
233 vim_free(address);
234 vim_free(password);
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200235 return NETBEANS_OPEN ? OK : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236}
237
238/*
239 * Obtain the NetBeans hostname, port address and password from a file.
240 * Return the strings in allocated memory.
241 * Return FAIL if the file could not be read, OK otherwise (no matter what it
242 * contains).
243 */
244 static int
245getConnInfo(char *file, char **host, char **port, char **auth)
246{
247 FILE *fp;
248 char_u buf[BUFSIZ];
249 char_u *lp;
Bram Moolenaar309cbc32012-01-10 22:31:31 +0100250 char_u *nlp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +0200252 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253
254 /*
255 * For Unix only accept the file when it's not accessible by others.
256 * The open will then fail if we don't own the file.
257 */
258 if (mch_stat(file, &st) == 0 && (st.st_mode & 0077) != 0)
259 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000260 nbdebug(("Wrong access mode for NetBeans connection info file: \"%s\"\n",
261 file));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262 EMSG2(_("E668: Wrong access mode for NetBeans connection info file: \"%s\""),
263 file);
264 return FAIL;
265 }
266#endif
267
268 fp = mch_fopen(file, "r");
269 if (fp == NULL)
270 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000271 nbdebug(("Cannot open NetBeans connection info file\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 PERROR("E660: Cannot open NetBeans connection info file");
273 return FAIL;
274 }
275
276 /* Read the file. There should be one of each parameter */
277 while ((lp = (char_u *)fgets((char *)buf, BUFSIZ, fp)) != NULL)
278 {
Bram Moolenaar309cbc32012-01-10 22:31:31 +0100279 if ((nlp = vim_strchr(lp, '\n')) != NULL)
280 *nlp = 0; /* strip off the trailing newline */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281
282 if (STRNCMP(lp, "host=", 5) == 0)
283 {
284 vim_free(*host);
285 *host = (char *)vim_strsave(&buf[5]);
286 }
287 else if (STRNCMP(lp, "port=", 5) == 0)
288 {
289 vim_free(*port);
290 *port = (char *)vim_strsave(&buf[5]);
291 }
292 else if (STRNCMP(lp, "auth=", 5) == 0)
293 {
294 vim_free(*auth);
295 *auth = (char *)vim_strsave(&buf[5]);
296 }
297 }
298 fclose(fp);
299
300 return OK;
301}
302
303
304struct keyqueue
305{
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100306 char_u *keystr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307 struct keyqueue *next;
308 struct keyqueue *prev;
309};
310
311typedef struct keyqueue keyQ_T;
312
313static keyQ_T keyHead; /* dummy node, header for circular queue */
314
315
316/*
317 * Queue up key commands sent from netbeans.
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100318 * We store the string, because it may depend on the global mod_mask and
319 * :nbkey doesn't have a key number.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320 */
321 static void
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100322postpone_keycommand(char_u *keystr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323{
324 keyQ_T *node;
325
326 node = (keyQ_T *)alloc(sizeof(keyQ_T));
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100327 if (node == NULL)
328 return; /* out of memory, drop the key */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329
330 if (keyHead.next == NULL) /* initialize circular queue */
331 {
332 keyHead.next = &keyHead;
333 keyHead.prev = &keyHead;
334 }
335
336 /* insert node at tail of queue */
337 node->next = &keyHead;
338 node->prev = keyHead.prev;
339 keyHead.prev->next = node;
340 keyHead.prev = node;
341
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100342 node->keystr = vim_strsave(keystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343}
344
345/*
346 * Handle any queued-up NetBeans keycommands to be send.
347 */
348 static void
349handle_key_queue(void)
350{
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100351 int postponed = FALSE;
352
353 while (!postponed && keyHead.next && keyHead.next != &keyHead)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354 {
355 /* first, unlink the node */
356 keyQ_T *node = keyHead.next;
357 keyHead.next = node->next;
358 node->next->prev = node->prev;
359
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100360 /* Now, send the keycommand. This may cause it to be postponed again
361 * and change keyHead. */
362 if (node->keystr != NULL)
363 postponed = !netbeans_keystring(node->keystr);
364 vim_free(node->keystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365
366 /* Finally, dispose of the node */
367 vim_free(node);
368 }
369}
370
371
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372/*
373 * While there's still a command in the work queue, parse and execute it.
374 */
Bram Moolenaarf2330482008-06-24 20:19:36 +0000375 void
376netbeans_parse_messages(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377{
Bram Moolenaar5f1032d2016-06-07 22:16:36 +0200378 readq_T *node;
Bram Moolenaard04a0202016-01-26 23:30:18 +0100379 char_u *buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380 char_u *p;
Bram Moolenaar863053d2010-12-02 17:09:54 +0100381 int own_node;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382
Bram Moolenaar77073442016-02-13 23:23:53 +0100383 while (nb_channel != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384 {
Bram Moolenaar5f1032d2016-06-07 22:16:36 +0200385 node = channel_peek(nb_channel, PART_SOCK);
386 if (node == NULL)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100387 break; /* nothing to read */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388
Bram Moolenaar5ce4a0b2016-06-08 20:17:23 +0200389 /* Locate the end of the first line in the first buffer. */
Bram Moolenaar5f1032d2016-06-07 22:16:36 +0200390 p = channel_first_nl(node);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391 if (p == NULL)
392 {
393 /* Command isn't complete. If there is no following buffer,
394 * return (wait for more). If there is another buffer following,
395 * prepend the text to that buffer and delete this one. */
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +0200396 if (channel_collapse(nb_channel, PART_SOCK, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397 return;
Bram Moolenaar5ce4a0b2016-06-08 20:17:23 +0200398 continue;
399 }
400
401 /* There is a complete command at the start of the buffer.
402 * Terminate it with a NUL. When no more text is following unlink
403 * the buffer. Do this before executing, because new buffers can
404 * be added while busy handling the command. */
405 *p++ = NUL;
406 if (*p == NUL)
407 {
408 own_node = TRUE;
409 buffer = channel_get(nb_channel, PART_SOCK);
410 /* "node" is now invalid! */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411 }
412 else
413 {
Bram Moolenaar5ce4a0b2016-06-08 20:17:23 +0200414 own_node = FALSE;
415 buffer = node->rq_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 }
Bram Moolenaar5ce4a0b2016-06-08 20:17:23 +0200417
Bram Moolenaar24cf2332016-07-01 12:50:54 +0200418 /* Now, parse and execute the commands. This may set nb_channel to
419 * NULL if the channel is closed. */
Bram Moolenaar5ce4a0b2016-06-08 20:17:23 +0200420 nb_parse_cmd(buffer);
421
422 if (own_node)
423 /* buffer finished, dispose of it */
424 vim_free(buffer);
Bram Moolenaar24cf2332016-07-01 12:50:54 +0200425 else if (nb_channel != NULL)
Bram Moolenaar5ce4a0b2016-06-08 20:17:23 +0200426 /* more follows, move it to the start */
427 channel_consume(nb_channel, PART_SOCK, (int)(p - buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428 }
429}
430
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431/*
432 * Handle one NUL terminated command.
433 *
434 * format of a command from netbeans:
435 *
436 * 6:setTitle!84 "a.c"
437 *
438 * bufno
439 * colon
440 * cmd
441 * !
442 * cmdno
443 * args
444 *
445 * for function calls, the ! is replaced by a /
446 */
447 static void
448nb_parse_cmd(char_u *cmd)
449{
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000450 char *verb;
451 char *q;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452 int bufno;
453 int isfunc = -1;
454
455 if (STRCMP(cmd, "DISCONNECT") == 0)
456 {
457 /* We assume the server knows that we can safely exit! */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 /* Disconnect before exiting, Motif hangs in a Select error
459 * message otherwise. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200460 netbeans_close();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461 getout(0);
462 /* NOTREACHED */
463 }
464
Bram Moolenaareed284a2016-02-22 23:13:33 +0100465 if (STRCMP(cmd, "DETACH") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466 {
Bram Moolenaard04a0202016-01-26 23:30:18 +0100467 buf_T *buf;
468
Bram Moolenaar29323592016-07-24 22:04:11 +0200469 FOR_ALL_BUFFERS(buf)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100470 buf->b_has_sign_column = FALSE;
471
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472 /* The IDE is breaking the connection. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200473 netbeans_close();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 return;
475 }
476
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000477 bufno = strtol((char *)cmd, &verb, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478
479 if (*verb != ':')
480 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000481 nbdebug((" missing colon: %s\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 EMSG2("E627: missing colon: %s", cmd);
483 return;
484 }
485 ++verb; /* skip colon */
486
487 for (q = verb; *q; q++)
488 {
489 if (*q == '!')
490 {
491 *q++ = NUL;
492 isfunc = 0;
493 break;
494 }
495 else if (*q == '/')
496 {
497 *q++ = NUL;
498 isfunc = 1;
499 break;
500 }
501 }
502
503 if (isfunc < 0)
504 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000505 nbdebug((" missing ! or / in: %s\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 EMSG2("E628: missing ! or / in: %s", cmd);
507 return;
508 }
509
Bram Moolenaar89d40322006-08-29 15:30:07 +0000510 r_cmdno = strtol(q, &q, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000512 q = (char *)skipwhite((char_u *)q);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513
Bram Moolenaar89d40322006-08-29 15:30:07 +0000514 if (nb_do_cmd(bufno, (char_u *)verb, isfunc, r_cmdno, (char_u *)q) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 {
Bram Moolenaar009b2592004-10-24 19:18:58 +0000516#ifdef NBDEBUG
517 /*
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100518 * This happens because the ExtEd can send a command or 2 after
Bram Moolenaar009b2592004-10-24 19:18:58 +0000519 * doing a stopDocumentListen command. It doesn't harm anything
520 * so I'm disabling it except for debugging.
521 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 nbdebug(("nb_parse_cmd: Command error for \"%s\"\n", cmd));
523 EMSG("E629: bad return from nb_do_cmd");
Bram Moolenaar009b2592004-10-24 19:18:58 +0000524#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 }
526}
527
528struct nbbuf_struct
529{
530 buf_T *bufp;
531 unsigned int fireChanges:1;
532 unsigned int initDone:1;
Bram Moolenaar009b2592004-10-24 19:18:58 +0000533 unsigned int insertDone:1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 unsigned int modified:1;
Bram Moolenaar009b2592004-10-24 19:18:58 +0000535 int nbbuf_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 char *displayname;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 int *signmap;
538 short_u signmaplen;
539 short_u signmapused;
540};
541
542typedef struct nbbuf_struct nbbuf_T;
543
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200544static nbbuf_T *buf_list = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000545static int buf_list_size = 0; /* size of buf_list */
546static int buf_list_used = 0; /* nr of entries in buf_list actually in use */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200548static char **globalsignmap = NULL;
549static int globalsignmaplen = 0;
550static int globalsignmapused = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100552static int mapsigntype(nbbuf_T *, int localsigntype);
553static void addsigntype(nbbuf_T *, int localsigntype, char_u *typeName,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 char_u *tooltip, char_u *glyphfile,
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100555 char_u *fg, char_u *bg);
556static void print_read_msg(nbbuf_T *buf);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200557static void print_save_msg(nbbuf_T *buf, off_T nchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558
559static int curPCtype = -1;
560
561/*
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200562 * Free netbeans resources.
563 */
564 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100565nb_free(void)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200566{
567 keyQ_T *key_node = keyHead.next;
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200568 nbbuf_T buf;
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200569 int i;
570
571 /* free the netbeans buffer list */
572 for (i = 0; i < buf_list_used; i++)
573 {
574 buf = buf_list[i];
575 vim_free(buf.displayname);
576 vim_free(buf.signmap);
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100577 if (buf.bufp != NULL)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200578 {
579 buf.bufp->b_netbeans_file = FALSE;
580 buf.bufp->b_was_netbeans_file = FALSE;
581 }
582 }
Bram Moolenaard23a8232018-02-10 18:45:26 +0100583 VIM_CLEAR(buf_list);
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200584 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 Moolenaar77073442016-02-13 23:23:53 +0100603 if (nb_channel != NULL)
604 channel_clear(nb_channel);
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;
Bram Moolenaar29323592016-07-24 22:04:11 +0200716 FOR_ALL_BUFFERS(bufp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 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 Moolenaar77073442016-02-13 23:23:53 +0100759 if (nb_channel != NULL)
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +0200760 channel_send(nb_channel, PART_SOCK, (char_u *)buf,
761 (int)STRLEN(buf), fun);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762}
763
764/*
765 * Some input received from netbeans requires a response. This function
766 * handles a response with no information (except the command number).
767 */
768 static void
769nb_reply_nil(int cmdno)
770{
771 char reply[32];
772
Bram Moolenaar009b2592004-10-24 19:18:58 +0000773 nbdebug(("REP %d: <none>\n", cmdno));
774
Bram Moolenaar7ad7d012010-11-16 15:49:02 +0100775 /* Avoid printing an annoying error message. */
776 if (!NETBEANS_OPEN)
777 return;
778
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 sprintf(reply, "%d\n", cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 nb_send(reply, "nb_reply_nil");
781}
782
783
784/*
785 * Send a response with text.
786 * "result" must have been quoted already (using nb_quote()).
787 */
788 static void
789nb_reply_text(int cmdno, char_u *result)
790{
791 char_u *reply;
792
Bram Moolenaar009b2592004-10-24 19:18:58 +0000793 nbdebug(("REP %d: %s\n", cmdno, (char *)result));
794
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000795 reply = alloc((unsigned)STRLEN(result) + 32);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 sprintf((char *)reply, "%d %s\n", cmdno, (char *)result);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 nb_send((char *)reply, "nb_reply_text");
798
799 vim_free(reply);
800}
801
802
803/*
804 * Send a response with a number result code.
805 */
806 static void
807nb_reply_nr(int cmdno, long result)
808{
809 char reply[32];
810
Bram Moolenaar009b2592004-10-24 19:18:58 +0000811 nbdebug(("REP %d: %ld\n", cmdno, result));
812
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 sprintf(reply, "%d %ld\n", cmdno, result);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 nb_send(reply, "nb_reply_nr");
815}
816
817
818/*
819 * Encode newline, ret, backslash, double quote for transmission to NetBeans.
820 */
821 static char_u *
822nb_quote(char_u *txt)
823{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000824 char_u *buf = alloc((unsigned)(2 * STRLEN(txt) + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 char_u *p = txt;
826 char_u *q = buf;
827
828 if (buf == NULL)
829 return NULL;
830 for (; *p; p++)
831 {
832 switch (*p)
833 {
834 case '\"':
835 case '\\':
836 *q++ = '\\'; *q++ = *p; break;
837 /* case '\t': */
838 /* *q++ = '\\'; *q++ = 't'; break; */
839 case '\n':
840 *q++ = '\\'; *q++ = 'n'; break;
841 case '\r':
842 *q++ = '\\'; *q++ = 'r'; break;
843 default:
844 *q++ = *p;
845 break;
846 }
847 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100848 *q = '\0';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849
850 return buf;
851}
852
853
854/*
855 * Remove top level double quotes; convert backslashed chars.
856 * Returns an allocated string (NULL for failure).
857 * If "endp" is not NULL it is set to the character after the terminating
858 * quote.
859 */
860 static char *
861nb_unquote(char_u *p, char_u **endp)
862{
863 char *result = 0;
864 char *q;
865 int done = 0;
866
867 /* result is never longer than input */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000868 result = (char *)alloc_clear((unsigned)STRLEN(p) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 if (result == NULL)
870 return NULL;
871
872 if (*p++ != '"')
873 {
874 nbdebug(("nb_unquote called with string that doesn't start with a quote!: %s\n",
875 p));
876 result[0] = NUL;
877 return result;
878 }
879
880 for (q = result; !done && *p != NUL;)
881 {
882 switch (*p)
883 {
884 case '"':
885 /*
886 * Unbackslashed dquote marks the end, if first char was dquote.
887 */
888 done = 1;
889 break;
890
891 case '\\':
892 ++p;
893 switch (*p)
894 {
895 case '\\': *q++ = '\\'; break;
896 case 'n': *q++ = '\n'; break;
897 case 't': *q++ = '\t'; break;
898 case 'r': *q++ = '\r'; break;
899 case '"': *q++ = '"'; break;
900 case NUL: --p; break;
901 /* default: skip over illegal chars */
902 }
903 ++p;
904 break;
905
906 default:
907 *q++ = *p++;
908 }
909 }
910
911 if (endp != NULL)
912 *endp = p;
913
914 return result;
915}
916
Bram Moolenaar5eaf8722008-01-05 17:07:13 +0000917/*
918 * Remove from "first" byte to "last" byte (inclusive), at line "lnum" of the
919 * current buffer. Remove to end of line when "last" is MAXCOL.
920 */
921 static void
922nb_partialremove(linenr_T lnum, colnr_T first, colnr_T last)
923{
924 char_u *oldtext, *newtext;
925 int oldlen;
926 int lastbyte = last;
927
928 oldtext = ml_get(lnum);
Bram Moolenaarcb4cef22008-03-16 15:04:34 +0000929 oldlen = (int)STRLEN(oldtext);
Bram Moolenaarb3c70982008-01-18 10:40:55 +0000930 if (first >= (colnr_T)oldlen || oldlen == 0) /* just in case */
Bram Moolenaar5eaf8722008-01-05 17:07:13 +0000931 return;
932 if (lastbyte >= oldlen)
933 lastbyte = oldlen - 1;
934 newtext = alloc(oldlen - (int)(lastbyte - first));
935 if (newtext != NULL)
936 {
937 mch_memmove(newtext, oldtext, first);
Bram Moolenaarf2330482008-06-24 20:19:36 +0000938 STRMOVE(newtext + first, oldtext + lastbyte + 1);
Bram Moolenaar5eaf8722008-01-05 17:07:13 +0000939 nbdebug((" NEW LINE %d: %s\n", lnum, newtext));
940 ml_replace(lnum, newtext, FALSE);
941 }
942}
943
944/*
945 * Replace the "first" line with the concatenation of the "first" and
946 * the "other" line. The "other" line is not removed.
947 */
948 static void
949nb_joinlines(linenr_T first, linenr_T other)
950{
951 int len_first, len_other;
952 char_u *p;
953
Bram Moolenaarcb4cef22008-03-16 15:04:34 +0000954 len_first = (int)STRLEN(ml_get(first));
955 len_other = (int)STRLEN(ml_get(other));
Bram Moolenaar5eaf8722008-01-05 17:07:13 +0000956 p = alloc((unsigned)(len_first + len_other + 1));
957 if (p != NULL)
958 {
959 mch_memmove(p, ml_get(first), len_first);
960 mch_memmove(p + len_first, ml_get(other), len_other + 1);
961 ml_replace(first, p, FALSE);
962 }
963}
964
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965#define SKIP_STOP 2
966#define streq(a,b) (strcmp(a,b) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967
968/*
969 * Do the actual processing of a single netbeans command or function.
Bram Moolenaar143c38c2007-05-10 16:41:10 +0000970 * The difference between a command and function is that a function
971 * gets a response (it's required) but a command does not.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 * For arguments see comment for nb_parse_cmd().
973 */
974 static int
975nb_do_cmd(
976 int bufno,
977 char_u *cmd,
978 int func,
979 int cmdno,
980 char_u *args) /* points to space before arguments or NUL */
981{
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100982 int do_update = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 long off = 0;
984 nbbuf_T *buf = nb_get_buf(bufno);
985 static int skip = 0;
986 int retval = OK;
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000987 char *cp; /* for when a char pointer is needed */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988
989 nbdebug(("%s %d: (%d) %s %s\n", (func) ? "FUN" : "CMD", cmdno, bufno, cmd,
990 STRCMP(cmd, "insert") == 0 ? "<text>" : (char *)args));
991
992 if (func)
993 {
994/* =====================================================================*/
995 if (streq((char *)cmd, "getModified"))
996 {
997 if (buf == NULL || buf->bufp == NULL)
998 /* Return the number of buffers that are modified. */
999 nb_reply_nr(cmdno, (long)count_changed_buffers());
1000 else
1001 /* Return whether the buffer is modified. */
1002 nb_reply_nr(cmdno, (long)(buf->bufp->b_changed
1003 || isNetbeansModified(buf->bufp)));
1004/* =====================================================================*/
1005 }
1006 else if (streq((char *)cmd, "saveAndExit"))
1007 {
1008 /* Note: this will exit Vim if successful. */
1009 coloncmd(":confirm qall");
1010
1011 /* We didn't exit: return the number of changed buffers. */
1012 nb_reply_nr(cmdno, (long)count_changed_buffers());
1013/* =====================================================================*/
1014 }
1015 else if (streq((char *)cmd, "getCursor"))
1016 {
1017 char_u text[200];
1018
1019 /* Note: nb_getbufno() may return -1. This indicates the IDE
1020 * didn't assign a number to the current buffer in response to a
1021 * fileOpened event. */
1022 sprintf((char *)text, "%d %ld %d %ld",
1023 nb_getbufno(curbuf),
1024 (long)curwin->w_cursor.lnum,
1025 (int)curwin->w_cursor.col,
1026 pos2off(curbuf, &curwin->w_cursor));
1027 nb_reply_text(cmdno, text);
1028/* =====================================================================*/
1029 }
Bram Moolenaarc65c4912006-11-14 17:29:46 +00001030 else if (streq((char *)cmd, "getAnno"))
1031 {
1032 long linenum = 0;
1033#ifdef FEAT_SIGNS
1034 if (buf == NULL || buf->bufp == NULL)
1035 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001036 nbdebug((" Invalid buffer identifier in getAnno\n"));
1037 EMSG("E652: Invalid buffer identifier in getAnno");
Bram Moolenaarc65c4912006-11-14 17:29:46 +00001038 retval = FAIL;
1039 }
1040 else
1041 {
1042 int serNum;
1043
1044 cp = (char *)args;
1045 serNum = strtol(cp, &cp, 10);
1046 /* If the sign isn't found linenum will be zero. */
1047 linenum = (long)buf_findsign(buf->bufp, serNum);
1048 }
1049#endif
1050 nb_reply_nr(cmdno, linenum);
1051/* =====================================================================*/
1052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 else if (streq((char *)cmd, "getLength"))
1054 {
1055 long len = 0;
1056
1057 if (buf == NULL || buf->bufp == NULL)
1058 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001059 nbdebug((" invalid buffer identifier in getLength\n"));
1060 EMSG("E632: invalid buffer identifier in getLength");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 retval = FAIL;
1062 }
1063 else
1064 {
1065 len = get_buf_size(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 }
1067 nb_reply_nr(cmdno, len);
1068/* =====================================================================*/
1069 }
1070 else if (streq((char *)cmd, "getText"))
1071 {
1072 long len;
1073 linenr_T nlines;
1074 char_u *text = NULL;
1075 linenr_T lno = 1;
1076 char_u *p;
1077 char_u *line;
1078
1079 if (buf == NULL || buf->bufp == NULL)
1080 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001081 nbdebug((" invalid buffer identifier in getText\n"));
1082 EMSG("E633: invalid buffer identifier in getText");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 retval = FAIL;
1084 }
1085 else
1086 {
1087 len = get_buf_size(buf->bufp);
1088 nlines = buf->bufp->b_ml.ml_line_count;
1089 text = alloc((unsigned)((len > 0)
1090 ? ((len + nlines) * 2) : 4));
1091 if (text == NULL)
1092 {
1093 nbdebug((" nb_do_cmd: getText has null text field\n"));
1094 retval = FAIL;
1095 }
1096 else
1097 {
1098 p = text;
1099 *p++ = '\"';
1100 for (; lno <= nlines ; lno++)
1101 {
1102 line = nb_quote(ml_get_buf(buf->bufp, lno, FALSE));
1103 if (line != NULL)
1104 {
1105 STRCPY(p, line);
1106 p += STRLEN(line);
1107 *p++ = '\\';
1108 *p++ = 'n';
Bram Moolenaar009b2592004-10-24 19:18:58 +00001109 vim_free(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 }
1112 *p++ = '\"';
1113 *p = '\0';
1114 }
1115 }
1116 if (text == NULL)
1117 nb_reply_text(cmdno, (char_u *)"");
1118 else
1119 {
1120 nb_reply_text(cmdno, text);
1121 vim_free(text);
1122 }
1123/* =====================================================================*/
1124 }
1125 else if (streq((char *)cmd, "remove"))
1126 {
1127 long count;
1128 pos_T first, last;
1129 pos_T *pos;
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001130 pos_T *next;
1131 linenr_T del_from_lnum, del_to_lnum; /* lines to be deleted as a whole */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 int oldFire = netbeansFireChanges;
1133 int oldSuppress = netbeansSuppressNoLines;
1134 int wasChanged;
1135
1136 if (skip >= SKIP_STOP)
1137 {
1138 nbdebug((" Skipping %s command\n", (char *) cmd));
1139 nb_reply_nil(cmdno);
1140 return OK;
1141 }
1142
1143 if (buf == NULL || buf->bufp == NULL)
1144 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001145 nbdebug((" invalid buffer identifier in remove\n"));
1146 EMSG("E634: invalid buffer identifier in remove");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 retval = FAIL;
1148 }
1149 else
1150 {
1151 netbeansFireChanges = FALSE;
1152 netbeansSuppressNoLines = TRUE;
1153
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001154 nb_set_curbuf(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 wasChanged = buf->bufp->b_changed;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001156 cp = (char *)args;
1157 off = strtol(cp, &cp, 10);
1158 count = strtol(cp, &cp, 10);
1159 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 /* delete "count" chars, starting at "off" */
1161 pos = off2pos(buf->bufp, off);
1162 if (!pos)
1163 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001164 nbdebug((" !bad position\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 nb_reply_text(cmdno, (char_u *)"!bad position");
1166 netbeansFireChanges = oldFire;
1167 netbeansSuppressNoLines = oldSuppress;
1168 return FAIL;
1169 }
1170 first = *pos;
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001171 nbdebug((" FIRST POS: line %d, col %d\n",
1172 first.lnum, first.col));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 pos = off2pos(buf->bufp, off+count-1);
1174 if (!pos)
1175 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001176 nbdebug((" !bad count\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 nb_reply_text(cmdno, (char_u *)"!bad count");
1178 netbeansFireChanges = oldFire;
1179 netbeansSuppressNoLines = oldSuppress;
1180 return FAIL;
1181 }
1182 last = *pos;
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001183 nbdebug((" LAST POS: line %d, col %d\n",
1184 last.lnum, last.col));
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001185 del_from_lnum = first.lnum;
1186 del_to_lnum = last.lnum;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001187 do_update = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001189 /* Get the position of the first byte after the deleted
1190 * section. "next" is NULL when deleting to the end of the
1191 * file. */
1192 next = off2pos(buf->bufp, off + count);
1193
1194 /* Remove part of the first line. */
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001195 if (first.col != 0
1196 || (next != NULL && first.lnum == next->lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 {
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001198 if (first.lnum != last.lnum
1199 || (next != NULL && first.lnum != next->lnum))
1200 {
1201 /* remove to the end of the first line */
1202 nb_partialremove(first.lnum, first.col,
1203 (colnr_T)MAXCOL);
1204 if (first.lnum == last.lnum)
1205 {
1206 /* Partial line to remove includes the end of
1207 * line. Join the line with the next one, have
1208 * the next line deleted below. */
1209 nb_joinlines(first.lnum, next->lnum);
1210 del_to_lnum = next->lnum;
1211 }
1212 }
1213 else
1214 {
1215 /* remove within one line */
1216 nb_partialremove(first.lnum, first.col, last.col);
1217 }
1218 ++del_from_lnum; /* don't delete the first line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 }
1220
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001221 /* Remove part of the last line. */
1222 if (first.lnum != last.lnum && next != NULL
1223 && next->col != 0 && last.lnum == next->lnum)
1224 {
1225 nb_partialremove(last.lnum, 0, last.col);
1226 if (del_from_lnum > first.lnum)
1227 {
1228 /* Join end of last line to start of first line; last
1229 * line is deleted below. */
1230 nb_joinlines(first.lnum, last.lnum);
1231 }
1232 else
1233 /* First line is deleted as a whole, keep the last
1234 * line. */
1235 --del_to_lnum;
1236 }
1237
1238 /* First is partial line; last line to remove includes
1239 * the end of line; join first line to line following last
1240 * line; line following last line is deleted below. */
1241 if (first.lnum != last.lnum && del_from_lnum > first.lnum
1242 && next != NULL && last.lnum != next->lnum)
1243 {
1244 nb_joinlines(first.lnum, next->lnum);
1245 del_to_lnum = next->lnum;
1246 }
1247
1248 /* Delete whole lines if there are any. */
1249 if (del_to_lnum >= del_from_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 {
1251 int i;
1252
1253 /* delete signs from the lines being deleted */
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001254 for (i = del_from_lnum; i <= del_to_lnum; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 {
1256 int id = buf_findsign_id(buf->bufp, (linenr_T)i);
1257 if (id > 0)
1258 {
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001259 nbdebug((" Deleting sign %d on line %d\n",
1260 id, i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 buf_delsign(buf->bufp, id);
1262 }
1263 else
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001264 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 nbdebug((" No sign on line %d\n", i));
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 }
1268
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001269 nbdebug((" Deleting lines %d through %d\n",
1270 del_from_lnum, del_to_lnum));
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001271 curwin->w_cursor.lnum = del_from_lnum;
1272 curwin->w_cursor.col = 0;
1273 del_lines(del_to_lnum - del_from_lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 }
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001275
1276 /* Leave cursor at first deleted byte. */
1277 curwin->w_cursor = first;
1278 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 buf->bufp->b_changed = wasChanged; /* logically unchanged */
1280 netbeansFireChanges = oldFire;
1281 netbeansSuppressNoLines = oldSuppress;
1282
1283 u_blockfree(buf->bufp);
1284 u_clearall(buf->bufp);
1285 }
1286 nb_reply_nil(cmdno);
1287/* =====================================================================*/
1288 }
1289 else if (streq((char *)cmd, "insert"))
1290 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 char_u *to_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292
1293 if (skip >= SKIP_STOP)
1294 {
1295 nbdebug((" Skipping %s command\n", (char *) cmd));
1296 nb_reply_nil(cmdno);
1297 return OK;
1298 }
1299
1300 /* get offset */
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001301 cp = (char *)args;
1302 off = strtol(cp, &cp, 10);
1303 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304
1305 /* get text to be inserted */
1306 args = skipwhite(args);
1307 args = to_free = (char_u *)nb_unquote(args, NULL);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001308 /*
1309 nbdebug((" CHUNK[%d]: %d bytes at offset %d\n",
1310 buf->bufp->b_ml.ml_line_count, STRLEN(args), off));
1311 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312
1313 if (buf == NULL || buf->bufp == NULL)
1314 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001315 nbdebug((" invalid buffer identifier in insert\n"));
1316 EMSG("E635: invalid buffer identifier in insert");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 retval = FAIL;
1318 }
1319 else if (args != NULL)
1320 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001321 int ff_detected = EOL_UNKNOWN;
1322 int buf_was_empty = (buf->bufp->b_ml.ml_flags & ML_EMPTY);
1323 size_t len = 0;
1324 int added = 0;
1325 int oldFire = netbeansFireChanges;
1326 int old_b_changed;
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001327 char_u *nlp;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001328 linenr_T lnum;
1329 linenr_T lnum_start;
1330 pos_T *pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 netbeansFireChanges = 0;
1333
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001334 /* Jump to the buffer where we insert. After this "curbuf"
1335 * can be used. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001336 nb_set_curbuf(buf->bufp);
Bram Moolenaara3227e22006-03-08 21:32:40 +00001337 old_b_changed = curbuf->b_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001339 /* Convert the specified character offset into a lnum/col
1340 * position. */
Bram Moolenaara3227e22006-03-08 21:32:40 +00001341 pos = off2pos(curbuf, off);
1342 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001344 if (pos->lnum <= 0)
1345 lnum_start = 1;
1346 else
1347 lnum_start = pos->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 }
1349 else
1350 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001351 /* If the given position is not found, assume we want
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 * the end of the file. See setLocAndSize HACK. */
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001353 if (buf_was_empty)
1354 lnum_start = 1; /* above empty line */
1355 else
1356 lnum_start = curbuf->b_ml.ml_line_count + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001357 }
1358
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001359 /* "lnum" is the line where we insert: either append to it or
1360 * insert a new line above it. */
1361 lnum = lnum_start;
1362
1363 /* Loop over the "\n" separated lines of the argument. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001364 do_update = 1;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001365 while (*args != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 {
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001367 nlp = vim_strchr(args, '\n');
1368 if (nlp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001370 /* Incomplete line, probably truncated. Next "insert"
1371 * command should append to this one. */
1372 len = STRLEN(args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001374 else
1375 {
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001376 len = nlp - args;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001377
1378 /*
1379 * We need to detect EOL style, because the commands
1380 * use a character offset.
1381 */
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001382 if (nlp > args && nlp[-1] == '\r')
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001383 {
1384 ff_detected = EOL_DOS;
1385 --len;
1386 }
1387 else
1388 ff_detected = EOL_UNIX;
1389 }
1390 args[len] = NUL;
1391
1392 if (lnum == lnum_start
1393 && ((pos != NULL && pos->col > 0)
1394 || (lnum == 1 && buf_was_empty)))
1395 {
1396 char_u *oldline = ml_get(lnum);
1397 char_u *newline;
1398
Bram Moolenaare4365282012-04-20 19:47:05 +02001399 /* Insert halfway a line. */
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001400 newline = alloc_check(
1401 (unsigned)(STRLEN(oldline) + len + 1));
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001402 if (newline != NULL)
1403 {
Bram Moolenaare4365282012-04-20 19:47:05 +02001404 mch_memmove(newline, oldline, (size_t)pos->col);
1405 newline[pos->col] = NUL;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001406 STRCAT(newline, args);
Bram Moolenaare4365282012-04-20 19:47:05 +02001407 STRCAT(newline, oldline + pos->col);
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001408 ml_replace(lnum, newline, FALSE);
1409 }
1410 }
1411 else
1412 {
1413 /* Append a new line. Not that we always do this,
1414 * also when the text doesn't end in a "\n". */
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001415 ml_append((linenr_T)(lnum - 1), args,
1416 (colnr_T)(len + 1), FALSE);
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001417 ++added;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001418 }
1419
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001420 if (nlp == NULL)
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001421 break;
1422 ++lnum;
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001423 args = nlp + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001424 }
1425
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001426 /* Adjust the marks below the inserted lines. */
1427 appended_lines_mark(lnum_start - 1, (long)added);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001429 /*
1430 * When starting with an empty buffer set the fileformat.
1431 * This is just guessing...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 */
1433 if (buf_was_empty)
1434 {
1435 if (ff_detected == EOL_UNKNOWN)
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001436#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 ff_detected = EOL_DOS;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001438#else
1439 ff_detected = EOL_UNIX;
1440#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 set_fileformat(ff_detected, OPT_LOCAL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00001442 curbuf->b_start_ffc = *curbuf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 }
1444
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 /*
1446 * XXX - GRP - Is the next line right? If I've inserted
1447 * text the buffer has been updated but not written. Will
1448 * netbeans guarantee to write it? Even if I do a :q! ?
1449 */
Bram Moolenaara3227e22006-03-08 21:32:40 +00001450 curbuf->b_changed = old_b_changed; /* logically unchanged */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 netbeansFireChanges = oldFire;
1452
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001453 /* Undo info is invalid now... */
Bram Moolenaara3227e22006-03-08 21:32:40 +00001454 u_blockfree(curbuf);
1455 u_clearall(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 }
1457 vim_free(to_free);
1458 nb_reply_nil(cmdno); /* or !error */
1459 }
1460 else
1461 {
1462 nbdebug(("UNIMPLEMENTED FUNCTION: %s\n", cmd));
1463 nb_reply_nil(cmdno);
1464 retval = FAIL;
1465 }
1466 }
1467 else /* Not a function; no reply required. */
1468 {
1469/* =====================================================================*/
1470 if (streq((char *)cmd, "create"))
1471 {
1472 /* Create a buffer without a name. */
1473 if (buf == NULL)
1474 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001475 nbdebug((" invalid buffer identifier in create\n"));
1476 EMSG("E636: invalid buffer identifier in create");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 return FAIL;
1478 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01001479 VIM_CLEAR(buf->displayname);
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 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02001736 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737
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 {
Bram Moolenaarff064e12008-06-09 13:10:45 +00001748 check_status(buf->bufp);
1749 redraw_tabline = TRUE;
Bram Moolenaarff064e12008-06-09 13:10:45 +00001750#ifdef FEAT_TITLE
1751 maketitle();
1752#endif
1753 update_screen(0);
1754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755/* =====================================================================*/
1756 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001757 else if (streq((char *)cmd, "setModtime"))
1758 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001759 if (buf == NULL || buf->bufp == NULL)
Bram Moolenaarf2330482008-06-24 20:19:36 +00001760 nbdebug((" invalid buffer identifier in setModtime\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001761 else
1762 buf->bufp->b_mtime = atoi((char *)args);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001763/* =====================================================================*/
1764 }
1765 else if (streq((char *)cmd, "setReadOnly"))
1766 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001767 if (buf == NULL || buf->bufp == NULL)
Bram Moolenaarf2330482008-06-24 20:19:36 +00001768 nbdebug((" invalid buffer identifier in setReadOnly\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001769 else if (streq((char *)args, "T"))
Bram Moolenaar009b2592004-10-24 19:18:58 +00001770 buf->bufp->b_p_ro = TRUE;
1771 else
1772 buf->bufp->b_p_ro = FALSE;
1773/* =====================================================================*/
1774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 else if (streq((char *)cmd, "setMark"))
1776 {
1777 /* not yet */
1778/* =====================================================================*/
1779 }
1780 else if (streq((char *)cmd, "showBalloon"))
1781 {
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001782#if defined(FEAT_BEVAL_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 static char *text = NULL;
1784
1785 /*
1786 * Set up the Balloon Expression Evaluation area.
1787 * Ignore 'ballooneval' here.
1788 * The text pointer must remain valid for a while.
1789 */
1790 if (balloonEval != NULL)
1791 {
1792 vim_free(text);
1793 text = nb_unquote(args, NULL);
1794 if (text != NULL)
1795 gui_mch_post_balloon(balloonEval, (char_u *)text);
1796 }
1797#endif
1798/* =====================================================================*/
1799 }
1800 else if (streq((char *)cmd, "setDot"))
1801 {
1802 pos_T *pos;
1803#ifdef NBDEBUG
1804 char_u *s;
1805#endif
1806
1807 if (buf == NULL || buf->bufp == NULL)
1808 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001809 nbdebug((" invalid buffer identifier in setDot\n"));
1810 EMSG("E647: invalid buffer identifier in setDot");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 return FAIL;
1812 }
1813
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001814 nb_set_curbuf(buf->bufp);
1815
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 /* Don't want Visual mode now. */
1817 if (VIsual_active)
1818 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819#ifdef NBDEBUG
1820 s = args;
1821#endif
1822 pos = get_off_or_lnum(buf->bufp, &args);
1823 if (pos)
1824 {
1825 curwin->w_cursor = *pos;
1826 check_cursor();
1827#ifdef FEAT_FOLDING
1828 foldOpenCursor();
1829#endif
1830 }
1831 else
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001832 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 nbdebug((" BAD POSITION in setDot: %s\n", s));
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835
1836 /* gui_update_cursor(TRUE, FALSE); */
1837 /* update_curbuf(NOT_VALID); */
1838 update_topline(); /* scroll to show the line */
1839 update_screen(VALID);
1840 setcursor();
Bram Moolenaar96bcc5e2011-04-01 15:33:59 +02001841 cursor_on();
Bram Moolenaara338adc2018-01-31 20:51:47 +01001842 out_flush_cursor(TRUE, FALSE);
1843
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 /* Quit a hit-return or more prompt. */
1845 if (State == HITRETURN || State == ASKMORE)
1846 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847#ifdef FEAT_GUI_GTK
Bram Moolenaard54a6882010-08-09 22:49:00 +02001848 if (gui.in_use && gtk_main_level() > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 gtk_main_quit();
1850#endif
1851 }
1852/* =====================================================================*/
1853 }
1854 else if (streq((char *)cmd, "close"))
1855 {
1856#ifdef NBDEBUG
1857 char *name = "<NONE>";
1858#endif
1859
1860 if (buf == NULL)
1861 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001862 nbdebug((" invalid buffer identifier in close\n"));
1863 EMSG("E648: invalid buffer identifier in close");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 return FAIL;
1865 }
1866
1867#ifdef NBDEBUG
1868 if (buf->displayname != NULL)
1869 name = buf->displayname;
1870#endif
Bram Moolenaarf2330482008-06-24 20:19:36 +00001871 if (buf->bufp == NULL)
1872 {
1873 nbdebug((" invalid buffer identifier in close\n"));
1874 /* This message was commented out, probably because it can
1875 * happen when shutting down. */
1876 if (p_verbose > 0)
1877 EMSG("E649: invalid buffer identifier in close");
1878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 nbdebug((" CLOSE %d: %s\n", bufno, name));
Bram Moolenaar67c53842010-05-22 18:28:27 +02001880#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 need_mouse_correct = TRUE;
Bram Moolenaar67c53842010-05-22 18:28:27 +02001882#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 if (buf->bufp != NULL)
1884 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD,
1885 buf->bufp->b_fnum, TRUE);
Bram Moolenaar8d602722006-08-08 19:34:19 +00001886 buf->bufp = NULL;
1887 buf->initDone = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001888 do_update = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889/* =====================================================================*/
1890 }
1891 else if (streq((char *)cmd, "setStyle")) /* obsolete... */
1892 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001893 nbdebug((" setStyle is obsolete!\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894/* =====================================================================*/
1895 }
1896 else if (streq((char *)cmd, "setExitDelay"))
1897 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00001898 /* Only used in version 2.1. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899/* =====================================================================*/
1900 }
1901 else if (streq((char *)cmd, "defineAnnoType"))
1902 {
1903#ifdef FEAT_SIGNS
1904 int typeNum;
1905 char_u *typeName;
1906 char_u *tooltip;
1907 char_u *p;
1908 char_u *glyphFile;
Bram Moolenaar67c53842010-05-22 18:28:27 +02001909 int parse_error = FALSE;
1910 char_u *fg;
1911 char_u *bg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912
1913 if (buf == NULL)
1914 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001915 nbdebug((" invalid buffer identifier in defineAnnoType\n"));
1916 EMSG("E650: invalid buffer identifier in defineAnnoType");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 return FAIL;
1918 }
1919
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001920 cp = (char *)args;
1921 typeNum = strtol(cp, &cp, 10);
1922 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 args = skipwhite(args);
1924 typeName = (char_u *)nb_unquote(args, &args);
1925 args = skipwhite(args + 1);
1926 tooltip = (char_u *)nb_unquote(args, &args);
1927 args = skipwhite(args + 1);
1928
1929 p = (char_u *)nb_unquote(args, &args);
1930 glyphFile = vim_strsave_escaped(p, escape_chars);
1931 vim_free(p);
1932
1933 args = skipwhite(args + 1);
Bram Moolenaar67c53842010-05-22 18:28:27 +02001934 p = skiptowhite(args);
1935 if (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02001937 *p = NUL;
1938 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02001940 fg = vim_strsave(args);
1941 bg = vim_strsave(p);
1942 if (STRLEN(fg) > MAX_COLOR_LENGTH || STRLEN(bg) > MAX_COLOR_LENGTH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02001944 EMSG("E532: highlighting color name too long in defineAnnoType");
1945 vim_free(typeName);
1946 parse_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02001948 else if (typeName != NULL && tooltip != NULL && glyphFile != NULL)
1949 addsigntype(buf, typeNum, typeName, tooltip, glyphFile, fg, bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 else
1951 vim_free(typeName);
1952
1953 /* don't free typeName; it's used directly in addsigntype() */
Bram Moolenaar67c53842010-05-22 18:28:27 +02001954 vim_free(fg);
1955 vim_free(bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 vim_free(tooltip);
1957 vim_free(glyphFile);
Bram Moolenaar67c53842010-05-22 18:28:27 +02001958 if (parse_error)
1959 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960
1961#endif
1962/* =====================================================================*/
1963 }
1964 else if (streq((char *)cmd, "addAnno"))
1965 {
1966#ifdef FEAT_SIGNS
1967 int serNum;
1968 int localTypeNum;
1969 int typeNum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 pos_T *pos;
1971
1972 if (buf == NULL || buf->bufp == NULL)
1973 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001974 nbdebug((" invalid buffer identifier in addAnno\n"));
1975 EMSG("E651: invalid buffer identifier in addAnno");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 return FAIL;
1977 }
1978
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001979 do_update = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001981 cp = (char *)args;
1982 serNum = strtol(cp, &cp, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983
1984 /* Get the typenr specific for this buffer and convert it to
1985 * the global typenumber, as used for the sign name. */
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001986 localTypeNum = strtol(cp, &cp, 10);
1987 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 typeNum = mapsigntype(buf, localTypeNum);
1989
1990 pos = get_off_or_lnum(buf->bufp, &args);
1991
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001992 cp = (char *)args;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00001993 ignored = (int)strtol(cp, &cp, 10);
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001994 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995# ifdef NBDEBUG
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00001996 if (ignored != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001998 nbdebug((" partial line annotation -- Not Yet Implemented!\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 }
2000# endif
2001 if (serNum >= GUARDEDOFFSET)
2002 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002003 nbdebug((" too many annotations! ignoring...\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 return FAIL;
2005 }
2006 if (pos)
2007 {
Bram Moolenaarec906222009-02-21 21:14:00 +00002008 coloncmd(":sign place %d line=%ld name=%d buffer=%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 serNum, pos->lnum, typeNum, buf->bufp->b_fnum);
2010 if (typeNum == curPCtype)
2011 coloncmd(":sign jump %d buffer=%d", serNum,
2012 buf->bufp->b_fnum);
2013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014#endif
2015/* =====================================================================*/
2016 }
2017 else if (streq((char *)cmd, "removeAnno"))
2018 {
2019#ifdef FEAT_SIGNS
2020 int serNum;
2021
2022 if (buf == NULL || buf->bufp == NULL)
2023 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002024 nbdebug((" invalid buffer identifier in removeAnno\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 return FAIL;
2026 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002027 do_update = 1;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002028 cp = (char *)args;
2029 serNum = strtol(cp, &cp, 10);
2030 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 coloncmd(":sign unplace %d buffer=%d",
2032 serNum, buf->bufp->b_fnum);
2033 redraw_buf_later(buf->bufp, NOT_VALID);
2034#endif
2035/* =====================================================================*/
2036 }
2037 else if (streq((char *)cmd, "moveAnnoToFront"))
2038 {
2039#ifdef FEAT_SIGNS
Bram Moolenaarf2330482008-06-24 20:19:36 +00002040 nbdebug((" moveAnnoToFront: Not Yet Implemented!\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041#endif
2042/* =====================================================================*/
2043 }
2044 else if (streq((char *)cmd, "guard") || streq((char *)cmd, "unguard"))
2045 {
2046 int len;
2047 pos_T first;
2048 pos_T last;
2049 pos_T *pos;
2050 int un = (cmd[0] == 'u');
2051 static int guardId = GUARDEDOFFSET;
2052
2053 if (skip >= SKIP_STOP)
2054 {
2055 nbdebug((" Skipping %s command\n", (char *) cmd));
2056 return OK;
2057 }
2058
2059 nb_init_graphics();
2060
2061 if (buf == NULL || buf->bufp == NULL)
2062 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002063 nbdebug((" invalid buffer identifier in %s command\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 return FAIL;
2065 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002066 nb_set_curbuf(buf->bufp);
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002067 cp = (char *)args;
2068 off = strtol(cp, &cp, 10);
2069 len = strtol(cp, NULL, 10);
2070 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 pos = off2pos(buf->bufp, off);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002072 do_update = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 if (!pos)
2074 nbdebug((" no such start pos in %s, %ld\n", cmd, off));
2075 else
2076 {
2077 first = *pos;
2078 pos = off2pos(buf->bufp, off + len - 1);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002079 if (pos != NULL && pos->col == 0)
2080 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 /*
2082 * In Java Swing the offset is a position between 2
2083 * characters. If col == 0 then we really want the
2084 * previous line as the end.
2085 */
2086 pos = off2pos(buf->bufp, off + len - 2);
2087 }
2088 if (!pos)
2089 nbdebug((" no such end pos in %s, %ld\n",
2090 cmd, off + len - 1));
2091 else
2092 {
2093 long lnum;
2094 last = *pos;
2095 /* set highlight for region */
2096 nbdebug((" %sGUARD %ld,%d to %ld,%d\n", (un) ? "UN" : "",
2097 first.lnum, first.col,
2098 last.lnum, last.col));
2099#ifdef FEAT_SIGNS
2100 for (lnum = first.lnum; lnum <= last.lnum; lnum++)
2101 {
2102 if (un)
2103 {
2104 /* never used */
2105 }
2106 else
2107 {
2108 if (buf_findsigntype_id(buf->bufp, lnum,
2109 GUARDED) == 0)
2110 {
2111 coloncmd(
Bram Moolenaarec906222009-02-21 21:14:00 +00002112 ":sign place %d line=%ld name=%d buffer=%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 guardId++, lnum, GUARDED,
2114 buf->bufp->b_fnum);
2115 }
2116 }
2117 }
2118#endif
2119 redraw_buf_later(buf->bufp, NOT_VALID);
2120 }
2121 }
2122/* =====================================================================*/
2123 }
2124 else if (streq((char *)cmd, "startAtomic"))
2125 {
2126 inAtomic = 1;
2127/* =====================================================================*/
2128 }
2129 else if (streq((char *)cmd, "endAtomic"))
2130 {
2131 inAtomic = 0;
2132 if (needupdate)
2133 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002134 do_update = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 needupdate = 0;
2136 }
2137/* =====================================================================*/
2138 }
2139 else if (streq((char *)cmd, "save"))
2140 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002141 /*
Bram Moolenaar9af41842016-09-25 21:45:05 +02002142 * NOTE - This command is obsolete wrt NetBeans. It's left in
Bram Moolenaar009b2592004-10-24 19:18:58 +00002143 * only for historical reasons.
2144 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 if (buf == NULL || buf->bufp == NULL)
2146 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002147 nbdebug((" invalid buffer identifier in %s command\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 return FAIL;
2149 }
2150
2151 /* the following is taken from ex_cmds.c (do_wqall function) */
2152 if (bufIsChanged(buf->bufp))
2153 {
2154 /* Only write if the buffer can be written. */
2155 if (p_write
2156 && !buf->bufp->b_p_ro
2157 && buf->bufp->b_ffname != NULL
2158#ifdef FEAT_QUICKFIX
2159 && !bt_dontwrite(buf->bufp)
2160#endif
2161 )
2162 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002163#ifdef FEAT_AUTOCMD
2164 bufref_T bufref;
2165
2166 set_bufref(&bufref, buf->bufp);
2167#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 buf_write_all(buf->bufp, FALSE);
2169#ifdef FEAT_AUTOCMD
2170 /* an autocommand may have deleted the buffer */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002171 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 buf->bufp = NULL;
2173#endif
2174 }
2175 }
Bram Moolenaarf2330482008-06-24 20:19:36 +00002176 else
2177 {
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002178 nbdebug((" Buffer has no changes!\n"));
Bram Moolenaarf2330482008-06-24 20:19:36 +00002179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180/* =====================================================================*/
2181 }
2182 else if (streq((char *)cmd, "netbeansBuffer"))
2183 {
2184 if (buf == NULL || buf->bufp == NULL)
2185 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002186 nbdebug((" invalid buffer identifier in %s command\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 return FAIL;
2188 }
2189 if (*args == 'T')
2190 {
2191 buf->bufp->b_netbeans_file = TRUE;
2192 buf->bufp->b_was_netbeans_file = TRUE;
2193 }
2194 else
2195 buf->bufp->b_netbeans_file = FALSE;
2196/* =====================================================================*/
2197 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002198 else if (streq((char *)cmd, "specialKeys"))
2199 {
2200 special_keys(args);
2201/* =====================================================================*/
2202 }
2203 else if (streq((char *)cmd, "actionMenuItem"))
2204 {
2205 /* not used yet */
2206/* =====================================================================*/
2207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 else if (streq((char *)cmd, "version"))
2209 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002210 /* not used yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 }
Bram Moolenaarf2330482008-06-24 20:19:36 +00002212 else
2213 {
2214 nbdebug(("Unrecognised command: %s\n", cmd));
2215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 /*
2217 * Unrecognized command is ignored.
2218 */
2219 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002220 if (inAtomic && do_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 {
2222 needupdate = 1;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002223 do_update = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 }
2225
Bram Moolenaar009b2592004-10-24 19:18:58 +00002226 /*
2227 * Is this needed? I moved the netbeans_Xt_connect() later during startup
Bram Moolenaar9af41842016-09-25 21:45:05 +02002228 * and it may no longer be necessary. If it's not needed then needupdate
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002229 * and do_update can also be removed.
Bram Moolenaar009b2592004-10-24 19:18:58 +00002230 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002231 if (buf != NULL && buf->initDone && do_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 {
2233 update_screen(NOT_VALID);
2234 setcursor();
Bram Moolenaar96bcc5e2011-04-01 15:33:59 +02002235 cursor_on();
Bram Moolenaara338adc2018-01-31 20:51:47 +01002236 out_flush_cursor(TRUE, FALSE);
2237
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 /* Quit a hit-return or more prompt. */
2239 if (State == HITRETURN || State == ASKMORE)
2240 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241#ifdef FEAT_GUI_GTK
Bram Moolenaard54a6882010-08-09 22:49:00 +02002242 if (gui.in_use && gtk_main_level() > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 gtk_main_quit();
2244#endif
2245 }
2246 }
2247
2248 return retval;
2249}
2250
2251
2252/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002253 * If "buf" is not the current buffer try changing to a window that edits this
2254 * buffer. If there is no such window then close the current buffer and set
2255 * the current buffer as "buf".
2256 */
2257 static void
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00002258nb_set_curbuf(buf_T *buf)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002259{
Bram Moolenaar404c9422015-03-14 15:35:52 +01002260 if (curbuf != buf) {
2261 if (buf_jump_open_win(buf) != NULL)
2262 return;
Bram Moolenaar404c9422015-03-14 15:35:52 +01002263 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf) != NULL)
2264 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002265 set_curbuf(buf, DOBUF_GOTO);
Bram Moolenaar404c9422015-03-14 15:35:52 +01002266 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002267}
2268
2269/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 * Process a vim colon command.
2271 */
2272 static void
2273coloncmd(char *cmd, ...)
2274{
2275 char buf[1024];
2276 va_list ap;
2277
2278 va_start(ap, cmd);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02002279 vim_vsnprintf(buf, sizeof(buf), cmd, ap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 va_end(ap);
2281
2282 nbdebug((" COLONCMD %s\n", buf));
2283
2284/* ALT_INPUT_LOCK_ON; */
2285 do_cmdline((char_u *)buf, NULL, NULL, DOCMD_NOWAIT | DOCMD_KEYTYPED);
2286/* ALT_INPUT_LOCK_OFF; */
2287
2288 setcursor(); /* restore the cursor position */
Bram Moolenaara338adc2018-01-31 20:51:47 +01002289 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290}
2291
2292
2293/*
Bram Moolenaar009b2592004-10-24 19:18:58 +00002294 * Parse the specialKeys argument and issue the appropriate map commands.
2295 */
2296 static void
2297special_keys(char_u *args)
2298{
2299 char *save_str = nb_unquote(args, NULL);
2300 char *tok = strtok(save_str, " ");
2301 char *sep;
Bram Moolenaarca24e2c2017-01-22 15:19:22 +01002302#define KEYBUFLEN 64
2303 char keybuf[KEYBUFLEN];
Bram Moolenaar009b2592004-10-24 19:18:58 +00002304 char cmdbuf[256];
2305
2306 while (tok != NULL)
2307 {
2308 int i = 0;
2309
2310 if ((sep = strchr(tok, '-')) != NULL)
2311 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002312 *sep = NUL;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002313 while (*tok)
2314 {
2315 switch (*tok)
2316 {
2317 case 'A':
2318 case 'M':
2319 case 'C':
2320 case 'S':
2321 keybuf[i++] = *tok;
2322 keybuf[i++] = '-';
2323 break;
2324 }
2325 tok++;
2326 }
2327 tok++;
2328 }
2329
Bram Moolenaarca24e2c2017-01-22 15:19:22 +01002330 if (strlen(tok) + i < KEYBUFLEN)
2331 {
2332 strcpy(&keybuf[i], tok);
2333 vim_snprintf(cmdbuf, sizeof(cmdbuf),
2334 "<silent><%s> :nbkey %s<CR>", keybuf, keybuf);
2335 do_map(0, (char_u *)cmdbuf, NORMAL, FALSE);
2336 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002337 tok = strtok(NULL, " ");
2338 }
2339 vim_free(save_str);
2340}
2341
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002342 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002343ex_nbclose(exarg_T *eap UNUSED)
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002344{
2345 netbeans_close();
2346}
Bram Moolenaar009b2592004-10-24 19:18:58 +00002347
2348 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002349ex_nbkey(exarg_T *eap)
Bram Moolenaar009b2592004-10-24 19:18:58 +00002350{
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002351 (void)netbeans_keystring(eap->arg);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002352}
2353
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002354 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002355ex_nbstart(
2356 exarg_T *eap)
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002357{
Bram Moolenaarc2a406b2010-09-30 21:03:26 +02002358#ifdef FEAT_GUI
2359# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar7ad7d012010-11-16 15:49:02 +01002360 && !defined(FEAT_GUI_W32)
Bram Moolenaarc2a406b2010-09-30 21:03:26 +02002361 if (gui.in_use)
2362 {
Bram Moolenaar7ad7d012010-11-16 15:49:02 +01002363 EMSG(_("E838: netbeans is not supported with this GUI"));
2364 return;
Bram Moolenaarc2a406b2010-09-30 21:03:26 +02002365 }
2366# endif
2367#endif
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002368 netbeans_open((char *)eap->arg, FALSE);
2369}
Bram Moolenaar009b2592004-10-24 19:18:58 +00002370
2371/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 * Initialize highlights and signs for use by netbeans (mostly obsolete)
2373 */
2374 static void
2375nb_init_graphics(void)
2376{
2377 static int did_init = FALSE;
2378
2379 if (!did_init)
2380 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002381 coloncmd(":highlight NBGuarded guibg=Cyan guifg=Black"
2382 " ctermbg=LightCyan ctermfg=Black");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 coloncmd(":sign define %d linehl=NBGuarded", GUARDED);
2384
2385 did_init = TRUE;
2386 }
2387}
2388
2389/*
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002390 * Convert key to netbeans name. This uses the global "mod_mask".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 */
2392 static void
2393netbeans_keyname(int key, char *buf)
2394{
2395 char *name = 0;
2396 char namebuf[2];
2397 int ctrl = 0;
2398 int shift = 0;
2399 int alt = 0;
2400
2401 if (mod_mask & MOD_MASK_CTRL)
2402 ctrl = 1;
2403 if (mod_mask & MOD_MASK_SHIFT)
2404 shift = 1;
2405 if (mod_mask & MOD_MASK_ALT)
2406 alt = 1;
2407
2408
2409 switch (key)
2410 {
2411 case K_F1: name = "F1"; break;
2412 case K_S_F1: name = "F1"; shift = 1; break;
2413 case K_F2: name = "F2"; break;
2414 case K_S_F2: name = "F2"; shift = 1; break;
2415 case K_F3: name = "F3"; break;
2416 case K_S_F3: name = "F3"; shift = 1; break;
2417 case K_F4: name = "F4"; break;
2418 case K_S_F4: name = "F4"; shift = 1; break;
2419 case K_F5: name = "F5"; break;
2420 case K_S_F5: name = "F5"; shift = 1; break;
2421 case K_F6: name = "F6"; break;
2422 case K_S_F6: name = "F6"; shift = 1; break;
2423 case K_F7: name = "F7"; break;
2424 case K_S_F7: name = "F7"; shift = 1; break;
2425 case K_F8: name = "F8"; break;
2426 case K_S_F8: name = "F8"; shift = 1; break;
2427 case K_F9: name = "F9"; break;
2428 case K_S_F9: name = "F9"; shift = 1; break;
2429 case K_F10: name = "F10"; break;
2430 case K_S_F10: name = "F10"; shift = 1; break;
2431 case K_F11: name = "F11"; break;
2432 case K_S_F11: name = "F11"; shift = 1; break;
2433 case K_F12: name = "F12"; break;
2434 case K_S_F12: name = "F12"; shift = 1; break;
2435 default:
2436 if (key >= ' ' && key <= '~')
2437 {
2438 /* Allow ASCII characters. */
2439 name = namebuf;
2440 namebuf[0] = key;
2441 namebuf[1] = NUL;
2442 }
2443 else
2444 name = "X";
2445 break;
2446 }
2447
2448 buf[0] = '\0';
2449 if (ctrl)
2450 strcat(buf, "C");
2451 if (shift)
2452 strcat(buf, "S");
2453 if (alt)
2454 strcat(buf, "M"); /* META */
2455 if (ctrl || shift || alt)
2456 strcat(buf, "-");
2457 strcat(buf, name);
2458}
2459
Bram Moolenaar67c53842010-05-22 18:28:27 +02002460#if defined(FEAT_BEVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461/*
2462 * Function to be called for balloon evaluation. Grabs the text under the
2463 * cursor and sends it to the debugger for evaluation. The debugger should
2464 * respond with a showBalloon command when there is a useful result.
2465 */
Bram Moolenaard62bec82005-03-07 22:56:57 +00002466 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467netbeans_beval_cb(
2468 BalloonEval *beval,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002469 int state UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470{
Bram Moolenaard62bec82005-03-07 22:56:57 +00002471 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 char_u *text;
Bram Moolenaard62bec82005-03-07 22:56:57 +00002473 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 int col;
Bram Moolenaard9462e32011-04-11 21:35:11 +02002475 char *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 char_u *p;
2477
2478 /* Don't do anything when 'ballooneval' is off, messages scrolled the
2479 * windows up or we have no connection. */
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01002480 if (!can_use_beval() || !NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 return;
2482
Bram Moolenaard62bec82005-03-07 22:56:57 +00002483 if (get_beval_info(beval, TRUE, &wp, &lnum, &text, &col) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 {
2485 /* Send debugger request. Only when the text is of reasonable
2486 * length. */
2487 if (text != NULL && text[0] != NUL && STRLEN(text) < MAXPATHL)
2488 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02002489 buf = (char *)alloc(MAXPATHL * 2 + 25);
2490 if (buf != NULL)
Bram Moolenaar009b2592004-10-24 19:18:58 +00002491 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02002492 p = nb_quote(text);
2493 if (p != NULL)
2494 {
2495 vim_snprintf(buf, MAXPATHL * 2 + 25,
2496 "0:balloonText=%d \"%s\"\n", r_cmdno, p);
2497 vim_free(p);
2498 }
2499 nbdebug(("EVT: %s", buf));
2500 nb_send(buf, "netbeans_beval_cb");
2501 vim_free(buf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 }
2504 vim_free(text);
2505 }
2506}
2507#endif
2508
2509/*
Bram Moolenaare0874f82016-01-24 20:36:41 +01002510 * Return TRUE when the netbeans connection is active.
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002511 */
2512 int
2513netbeans_active(void)
2514{
2515 return NETBEANS_OPEN;
2516}
2517
Bram Moolenaar67c53842010-05-22 18:28:27 +02002518/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 * Tell netbeans that the window was opened, ready for commands.
2520 */
2521 void
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02002522netbeans_open(char *params, int doabort)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523{
2524 char *cmd = "0:startupDone=0\n";
2525
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002526 if (NETBEANS_OPEN)
2527 {
2528 EMSG(_("E511: netbeans already connected"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02002532 if (netbeans_connect(params, doabort) != OK)
Bram Moolenaar67c53842010-05-22 18:28:27 +02002533 return;
Bram Moolenaard62bec82005-03-07 22:56:57 +00002534
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 nbdebug(("EVT: %s", cmd));
2536 nb_send(cmd, "netbeans_startup_done");
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002537
2538 /* update the screen after having added the gutter */
2539 changed_window_setting();
2540 update_screen(CLEAR);
2541 setcursor();
Bram Moolenaar96bcc5e2011-04-01 15:33:59 +02002542 cursor_on();
Bram Moolenaara338adc2018-01-31 20:51:47 +01002543 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544}
2545
Bram Moolenaar009b2592004-10-24 19:18:58 +00002546/*
2547 * Tell netbeans that we're exiting. This should be called right
2548 * before calling exit.
2549 */
2550 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002551netbeans_send_disconnect(void)
Bram Moolenaar009b2592004-10-24 19:18:58 +00002552{
2553 char buf[128];
2554
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002555 if (NETBEANS_OPEN)
Bram Moolenaar009b2592004-10-24 19:18:58 +00002556 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002557 sprintf(buf, "0:disconnect=%d\n", r_cmdno);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002558 nbdebug(("EVT: %s", buf));
2559 nb_send(buf, "netbeans_disconnect");
2560 }
2561}
2562
Bram Moolenaar3266c852016-04-30 18:07:05 +02002563#if defined(FEAT_EVAL) || defined(PROTO)
2564 int
2565set_ref_in_nb_channel(int copyID)
2566{
2567 int abort = FALSE;
2568 typval_T tv;
2569
2570 if (nb_channel != NULL)
2571 {
2572 tv.v_type = VAR_CHANNEL;
2573 tv.vval.v_channel = nb_channel;
2574 abort = set_ref_in_item(&tv, copyID, NULL, NULL);
2575 }
2576 return abort;
2577}
2578#endif
2579
Bram Moolenaar173c9852010-09-29 17:27:01 +02002580#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_W32) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581/*
2582 * Tell netbeans that the window was moved or resized.
2583 */
2584 void
2585netbeans_frame_moved(int new_x, int new_y)
2586{
2587 char buf[128];
2588
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002589 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 return;
2591
2592 sprintf(buf, "0:geometry=%d %d %d %d %d\n",
Bram Moolenaar89d40322006-08-29 15:30:07 +00002593 r_cmdno, (int)Columns, (int)Rows, new_x, new_y);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002594 /*nbdebug(("EVT: %s", buf)); happens too many times during a move */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 nb_send(buf, "netbeans_frame_moved");
2596}
2597#endif
2598
2599/*
Bram Moolenaar009b2592004-10-24 19:18:58 +00002600 * Tell netbeans the user opened or activated a file.
2601 */
2602 void
2603netbeans_file_activated(buf_T *bufp)
2604{
2605 int bufno = nb_getbufno(bufp);
2606 nbbuf_T *bp = nb_get_buf(bufno);
2607 char buffer[2*MAXPATHL];
2608 char_u *q;
2609
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002610 if (!NETBEANS_OPEN || !bufp->b_netbeans_file || dosetvisible)
Bram Moolenaar009b2592004-10-24 19:18:58 +00002611 return;
2612
2613 q = nb_quote(bufp->b_ffname);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002614 if (q == NULL || bp == NULL)
Bram Moolenaar009b2592004-10-24 19:18:58 +00002615 return;
2616
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002617 vim_snprintf(buffer, sizeof(buffer), "%d:fileOpened=%d \"%s\" %s %s\n",
Bram Moolenaar009b2592004-10-24 19:18:58 +00002618 bufno,
2619 bufno,
2620 (char *)q,
2621 "T", /* open in NetBeans */
2622 "F"); /* modified */
2623
2624 vim_free(q);
2625 nbdebug(("EVT: %s", buffer));
2626
2627 nb_send(buffer, "netbeans_file_opened");
2628}
2629
2630/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 * Tell netbeans the user opened a file.
2632 */
2633 void
Bram Moolenaar009b2592004-10-24 19:18:58 +00002634netbeans_file_opened(buf_T *bufp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635{
Bram Moolenaar009b2592004-10-24 19:18:58 +00002636 int bufno = nb_getbufno(bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 char buffer[2*MAXPATHL];
2638 char_u *q;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002639 nbbuf_T *bp = nb_get_buf(nb_getbufno(bufp));
2640 int bnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002642 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 return;
2644
Bram Moolenaar009b2592004-10-24 19:18:58 +00002645 q = nb_quote(bufp->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 if (q == NULL)
2647 return;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002648 if (bp != NULL)
2649 bnum = bufno;
2650 else
2651 bnum = 0;
2652
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002653 vim_snprintf(buffer, sizeof(buffer), "%d:fileOpened=%d \"%s\" %s %s\n",
Bram Moolenaar009b2592004-10-24 19:18:58 +00002654 bnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 0,
2656 (char *)q,
2657 "T", /* open in NetBeans */
2658 "F"); /* modified */
2659
2660 vim_free(q);
2661 nbdebug(("EVT: %s", buffer));
2662
2663 nb_send(buffer, "netbeans_file_opened");
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002664 if (p_acd && vim_chdirfile(bufp->b_ffname, "auto") == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 shorten_fnames(TRUE);
2666}
2667
2668/*
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00002669 * Tell netbeans that a file was deleted or wiped out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 */
2671 void
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00002672netbeans_file_killed(buf_T *bufp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673{
2674 int bufno = nb_getbufno(bufp);
2675 nbbuf_T *nbbuf = nb_get_buf(bufno);
2676 char buffer[2*MAXPATHL];
2677
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002678 if (!NETBEANS_OPEN || bufno == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 return;
2680
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00002681 nbdebug(("netbeans_file_killed:\n"));
2682 nbdebug((" Killing bufno: %d", bufno));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683
Bram Moolenaar89d40322006-08-29 15:30:07 +00002684 sprintf(buffer, "%d:killed=%d\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685
2686 nbdebug(("EVT: %s", buffer));
2687
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00002688 nb_send(buffer, "netbeans_file_killed");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689
2690 if (nbbuf != NULL)
2691 nbbuf->bufp = NULL;
2692}
2693
2694/*
2695 * Get a pointer to the Netbeans buffer for Vim buffer "bufp".
2696 * Return NULL if there is no such buffer or changes are not to be reported.
2697 * Otherwise store the buffer number in "*bufnop".
2698 */
2699 static nbbuf_T *
2700nb_bufp2nbbuf_fire(buf_T *bufp, int *bufnop)
2701{
2702 int bufno;
2703 nbbuf_T *nbbuf;
2704
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002705 if (!NETBEANS_OPEN || !netbeansFireChanges)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 return NULL; /* changes are not reported at all */
2707
2708 bufno = nb_getbufno(bufp);
2709 if (bufno <= 0)
2710 return NULL; /* file is not known to NetBeans */
2711
2712 nbbuf = nb_get_buf(bufno);
2713 if (nbbuf != NULL && !nbbuf->fireChanges)
2714 return NULL; /* changes in this buffer are not reported */
2715
2716 *bufnop = bufno;
2717 return nbbuf;
2718}
2719
2720/*
2721 * Tell netbeans the user inserted some text.
2722 */
2723 void
2724netbeans_inserted(
2725 buf_T *bufp,
2726 linenr_T linenr,
2727 colnr_T col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 char_u *txt,
2729 int newlen)
2730{
2731 char_u *buf;
2732 int bufno;
2733 nbbuf_T *nbbuf;
2734 pos_T pos;
2735 long off;
2736 char_u *p;
2737 char_u *newtxt;
2738
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002739 if (!NETBEANS_OPEN)
2740 return;
2741
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
2743 if (nbbuf == NULL)
2744 return;
2745
Bram Moolenaar009b2592004-10-24 19:18:58 +00002746 /* Don't mark as modified for initial read */
2747 if (nbbuf->insertDone)
2748 nbbuf->modified = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749
2750 pos.lnum = linenr;
2751 pos.col = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 off = pos2off(bufp, &pos);
2753
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 /* send the "insert" EVT */
2755 newtxt = alloc(newlen + 1);
Bram Moolenaarb6356332005-07-18 21:40:44 +00002756 vim_strncpy(newtxt, txt, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 p = nb_quote(newtxt);
2758 if (p != NULL)
2759 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002760 buf = alloc(128 + 2*newlen);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002761 sprintf((char *)buf, "%d:insert=%d %ld \"%s\"\n",
2762 bufno, r_cmdno, off, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 nbdebug(("EVT: %s", buf));
2764 nb_send((char *)buf, "netbeans_inserted");
Bram Moolenaar009b2592004-10-24 19:18:58 +00002765 vim_free(p);
2766 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 vim_free(newtxt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769}
2770
2771/*
2772 * Tell netbeans some bytes have been removed.
2773 */
2774 void
2775netbeans_removed(
2776 buf_T *bufp,
2777 linenr_T linenr,
2778 colnr_T col,
2779 long len)
2780{
2781 char_u buf[128];
2782 int bufno;
2783 nbbuf_T *nbbuf;
2784 pos_T pos;
2785 long off;
2786
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002787 if (!NETBEANS_OPEN)
2788 return;
2789
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
2791 if (nbbuf == NULL)
2792 return;
2793
2794 if (len < 0)
2795 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002796 nbdebug(("Negative len %ld in netbeans_removed()!\n", len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 return;
2798 }
2799
2800 nbbuf->modified = 1;
2801
2802 pos.lnum = linenr;
2803 pos.col = col;
2804
2805 off = pos2off(bufp, &pos);
2806
Bram Moolenaar89d40322006-08-29 15:30:07 +00002807 sprintf((char *)buf, "%d:remove=%d %ld %ld\n", bufno, r_cmdno, off, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 nbdebug(("EVT: %s", buf));
2809 nb_send((char *)buf, "netbeans_removed");
2810}
2811
2812/*
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002813 * Send netbeans an unmodified command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002816netbeans_unmodified(buf_T *bufp UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817{
Bram Moolenaar09092152010-08-08 16:38:42 +02002818 /* This is a no-op, because NetBeans considers a buffer modified
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 * even when all changes have been undone. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820}
2821
2822/*
Bram Moolenaar9af41842016-09-25 21:45:05 +02002823 * Send a button release event back to netbeans. It's up to netbeans
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 * to decide what to do (if anything) with this event.
2825 */
2826 void
2827netbeans_button_release(int button)
2828{
2829 char buf[128];
2830 int bufno;
2831
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002832 if (!NETBEANS_OPEN)
2833 return;
2834
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 bufno = nb_getbufno(curbuf);
2836
2837 if (bufno >= 0 && curwin != NULL && curwin->w_buffer == curbuf)
2838 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02002839 int col = mouse_col - curwin->w_wincol
Bram Moolenaar67c53842010-05-22 18:28:27 +02002840 - ((curwin->w_p_nu || curwin->w_p_rnu) ? 9 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 long off = pos2off(curbuf, &curwin->w_cursor);
2842
2843 /* sync the cursor position */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002844 sprintf(buf, "%d:newDotAndMark=%d %ld %ld\n", bufno, r_cmdno, off, off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 nbdebug(("EVT: %s", buf));
2846 nb_send(buf, "netbeans_button_release[newDotAndMark]");
2847
Bram Moolenaar89d40322006-08-29 15:30:07 +00002848 sprintf(buf, "%d:buttonRelease=%d %d %ld %d\n", bufno, r_cmdno,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 button, (long)curwin->w_cursor.lnum, col);
2850 nbdebug(("EVT: %s", buf));
2851 nb_send(buf, "netbeans_button_release");
2852 }
2853}
2854
2855
2856/*
Bram Moolenaar143c38c2007-05-10 16:41:10 +00002857 * Send a keypress event back to netbeans. This usually simulates some
Bram Moolenaar009b2592004-10-24 19:18:58 +00002858 * kind of function key press. This function operates on a key code.
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002859 * Return TRUE when the key was sent, FALSE when the command has been
2860 * postponed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 */
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002862 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863netbeans_keycommand(int key)
2864{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 char keyName[60];
Bram Moolenaar009b2592004-10-24 19:18:58 +00002866
2867 netbeans_keyname(key, keyName);
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002868 return netbeans_keystring((char_u *)keyName);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002869}
2870
2871
2872/*
Bram Moolenaar143c38c2007-05-10 16:41:10 +00002873 * Send a keypress event back to netbeans. This usually simulates some
Bram Moolenaar009b2592004-10-24 19:18:58 +00002874 * kind of function key press. This function operates on a key string.
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002875 * Return TRUE when the key was sent, FALSE when the command has been
2876 * postponed.
Bram Moolenaar009b2592004-10-24 19:18:58 +00002877 */
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002878 static int
2879netbeans_keystring(char_u *keyName)
Bram Moolenaar009b2592004-10-24 19:18:58 +00002880{
2881 char buf[2*MAXPATHL];
2882 int bufno = nb_getbufno(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 long off;
2884 char_u *q;
2885
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002886 if (!NETBEANS_OPEN)
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002887 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 if (bufno == -1)
2890 {
2891 nbdebug(("got keycommand for non-NetBeans buffer, opening...\n"));
2892 q = curbuf->b_ffname == NULL ? (char_u *)""
2893 : nb_quote(curbuf->b_ffname);
2894 if (q == NULL)
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002895 return TRUE;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002896 vim_snprintf(buf, sizeof(buf), "0:fileOpened=%d \"%s\" %s %s\n", 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 q,
2898 "T", /* open in NetBeans */
2899 "F"); /* modified */
2900 if (curbuf->b_ffname != NULL)
2901 vim_free(q);
2902 nbdebug(("EVT: %s", buf));
2903 nb_send(buf, "netbeans_keycommand");
2904
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002905 postpone_keycommand(keyName);
2906 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 }
2908
2909 /* sync the cursor position */
2910 off = pos2off(curbuf, &curwin->w_cursor);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002911 sprintf(buf, "%d:newDotAndMark=%d %ld %ld\n", bufno, r_cmdno, off, off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 nbdebug(("EVT: %s", buf));
2913 nb_send(buf, "netbeans_keycommand");
2914
2915 /* To work on Win32 you must apply patch to ExtEditor module
2916 * from ExtEdCaret.java.diff - make EVT_newDotAndMark handler
2917 * more synchronous
2918 */
2919
2920 /* now send keyCommand event */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002921 vim_snprintf(buf, sizeof(buf), "%d:keyCommand=%d \"%s\"\n",
Bram Moolenaar89d40322006-08-29 15:30:07 +00002922 bufno, r_cmdno, keyName);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 nbdebug(("EVT: %s", buf));
2924 nb_send(buf, "netbeans_keycommand");
2925
2926 /* New: do both at once and include the lnum/col. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002927 vim_snprintf(buf, sizeof(buf), "%d:keyAtPos=%d \"%s\" %ld %ld/%ld\n",
Bram Moolenaar89d40322006-08-29 15:30:07 +00002928 bufno, r_cmdno, keyName,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 off, (long)curwin->w_cursor.lnum, (long)curwin->w_cursor.col);
2930 nbdebug(("EVT: %s", buf));
2931 nb_send(buf, "netbeans_keycommand");
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002932 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933}
2934
2935
2936/*
2937 * Send a save event to netbeans.
2938 */
2939 void
2940netbeans_save_buffer(buf_T *bufp)
2941{
2942 char_u buf[64];
2943 int bufno;
2944 nbbuf_T *nbbuf;
2945
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002946 if (!NETBEANS_OPEN)
2947 return;
2948
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
2950 if (nbbuf == NULL)
2951 return;
2952
2953 nbbuf->modified = 0;
2954
Bram Moolenaar89d40322006-08-29 15:30:07 +00002955 sprintf((char *)buf, "%d:save=%d\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 nbdebug(("EVT: %s", buf));
2957 nb_send((char *)buf, "netbeans_save_buffer");
2958}
2959
2960
2961/*
2962 * Send remove command to netbeans (this command has been turned off).
2963 */
2964 void
2965netbeans_deleted_all_lines(buf_T *bufp)
2966{
2967 char_u buf[64];
2968 int bufno;
2969 nbbuf_T *nbbuf;
2970
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002971 if (!NETBEANS_OPEN)
2972 return;
2973
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
2975 if (nbbuf == NULL)
2976 return;
2977
Bram Moolenaar009b2592004-10-24 19:18:58 +00002978 /* Don't mark as modified for initial read */
2979 if (nbbuf->insertDone)
2980 nbbuf->modified = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981
Bram Moolenaar89d40322006-08-29 15:30:07 +00002982 sprintf((char *)buf, "%d:remove=%d 0 -1\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 nbdebug(("EVT(suppressed): %s", buf));
2984/* nb_send(buf, "netbeans_deleted_all_lines"); */
2985}
2986
2987
2988/*
2989 * See if the lines are guarded. The top and bot parameters are from
2990 * u_savecommon(), these are the line above the change and the line below the
2991 * change.
2992 */
2993 int
2994netbeans_is_guarded(linenr_T top, linenr_T bot)
2995{
2996 signlist_T *p;
2997 int lnum;
2998
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002999 if (!NETBEANS_OPEN)
3000 return FALSE;
3001
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 for (p = curbuf->b_signlist; p != NULL; p = p->next)
3003 if (p->id >= GUARDEDOFFSET)
3004 for (lnum = top + 1; lnum < bot; lnum++)
3005 if (lnum == p->lnum)
3006 return TRUE;
3007
3008 return FALSE;
3009}
3010
Bram Moolenaar173c9852010-09-29 17:27:01 +02003011#if defined(FEAT_GUI_X11) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012/*
3013 * We have multiple signs to draw at the same location. Draw the
3014 * multi-sign indicator instead. This is the Motif version.
3015 */
3016 void
3017netbeans_draw_multisign_indicator(int row)
3018{
3019 int i;
3020 int y;
3021 int x;
3022
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003023 if (!NETBEANS_OPEN)
3024 return;
3025
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 x = 0;
3027 y = row * gui.char_height + 2;
3028
3029 for (i = 0; i < gui.char_height - 3; i++)
3030 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y++);
3031
3032 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+0, y);
3033 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3034 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+4, y++);
3035 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+1, y);
3036 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3037 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+3, y++);
3038 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3039}
Bram Moolenaar173c9852010-09-29 17:27:01 +02003040#endif /* FEAT_GUI_X11 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041
Bram Moolenaar64404472010-06-26 06:24:45 +02003042#if defined(FEAT_GUI_GTK) && !defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043/*
3044 * We have multiple signs to draw at the same location. Draw the
3045 * multi-sign indicator instead. This is the GTK/Gnome version.
3046 */
3047 void
3048netbeans_draw_multisign_indicator(int row)
3049{
3050 int i;
3051 int y;
3052 int x;
Bram Moolenaar98921892016-02-23 17:14:37 +01003053#if GTK_CHECK_VERSION(3,0,0)
3054 cairo_t *cr = NULL;
3055#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 GdkDrawable *drawable = gui.drawarea->window;
Bram Moolenaar98921892016-02-23 17:14:37 +01003057#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003059 if (!NETBEANS_OPEN)
3060 return;
3061
Bram Moolenaar98921892016-02-23 17:14:37 +01003062#if GTK_CHECK_VERSION(3,0,0)
3063 cr = cairo_create(gui.surface);
Bram Moolenaar36edf062016-07-21 22:10:12 +02003064 cairo_set_source_rgba(cr,
3065 gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue,
3066 gui.fgcolor->alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01003067#endif
3068
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 x = 0;
3070 y = row * gui.char_height + 2;
3071
3072 for (i = 0; i < gui.char_height - 3; i++)
Bram Moolenaar98921892016-02-23 17:14:37 +01003073#if GTK_CHECK_VERSION(3,0,0)
3074 cairo_rectangle(cr, x+2, y++, 1, 1);
3075#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 gdk_draw_point(drawable, gui.text_gc, x+2, y++);
Bram Moolenaar98921892016-02-23 17:14:37 +01003077#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078
Bram Moolenaar98921892016-02-23 17:14:37 +01003079#if GTK_CHECK_VERSION(3,0,0)
3080 cairo_rectangle(cr, x+0, y, 1, 1);
3081 cairo_rectangle(cr, x+2, y, 1, 1);
3082 cairo_rectangle(cr, x+4, y++, 1, 1);
3083 cairo_rectangle(cr, x+1, y, 1, 1);
3084 cairo_rectangle(cr, x+2, y, 1, 1);
3085 cairo_rectangle(cr, x+3, y++, 1, 1);
3086 cairo_rectangle(cr, x+2, y, 1, 1);
3087#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 gdk_draw_point(drawable, gui.text_gc, x+0, y);
3089 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3090 gdk_draw_point(drawable, gui.text_gc, x+4, y++);
3091 gdk_draw_point(drawable, gui.text_gc, x+1, y);
3092 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3093 gdk_draw_point(drawable, gui.text_gc, x+3, y++);
3094 gdk_draw_point(drawable, gui.text_gc, x+2, y);
Bram Moolenaar98921892016-02-23 17:14:37 +01003095#endif
3096
3097#if GTK_CHECK_VERSION(3,0,0)
3098 cairo_destroy(cr);
3099#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100}
3101#endif /* FEAT_GUI_GTK */
3102
3103/*
3104 * If the mouse is clicked in the gutter of a line with multiple
3105 * annotations, cycle through the set of signs.
3106 */
3107 void
3108netbeans_gutter_click(linenr_T lnum)
3109{
3110 signlist_T *p;
3111
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003112 if (!NETBEANS_OPEN)
3113 return;
3114
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 for (p = curbuf->b_signlist; p != NULL; p = p->next)
3116 {
3117 if (p->lnum == lnum && p->next && p->next->lnum == lnum)
3118 {
3119 signlist_T *tail;
3120
3121 /* remove "p" from list, reinsert it at the tail of the sublist */
3122 if (p->prev)
3123 p->prev->next = p->next;
3124 else
3125 curbuf->b_signlist = p->next;
3126 p->next->prev = p->prev;
3127 /* now find end of sublist and insert p */
3128 for (tail = p->next;
3129 tail->next && tail->next->lnum == lnum
3130 && tail->next->id < GUARDEDOFFSET;
3131 tail = tail->next)
3132 ;
3133 /* tail now points to last entry with same lnum (except
3134 * that "guarded" annotations are always last) */
3135 p->next = tail->next;
3136 if (tail->next)
3137 tail->next->prev = p;
3138 p->prev = tail;
3139 tail->next = p;
3140 update_debug_sign(curbuf, lnum);
3141 break;
3142 }
3143 }
3144}
3145
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146/*
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003147 * Add a sign of the requested type at the requested location.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 *
3149 * Reverse engineering:
3150 * Apparently an annotation is defined the first time it is used in a buffer.
3151 * When the same annotation is used in two buffers, the second time we do not
3152 * need to define a new sign name but reuse the existing one. But since the
3153 * ID number used in the second buffer starts counting at one again, a mapping
3154 * is made from the ID specifically for the buffer to the global sign name
3155 * (which is a number).
3156 *
3157 * globalsignmap[] stores the signs that have been defined globally.
3158 * buf->signmapused[] maps buffer-local annotation IDs to an index in
3159 * globalsignmap[].
3160 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 static void
3162addsigntype(
3163 nbbuf_T *buf,
3164 int typeNum,
3165 char_u *typeName,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003166 char_u *tooltip UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 char_u *glyphFile,
Bram Moolenaar67c53842010-05-22 18:28:27 +02003168 char_u *fg,
3169 char_u *bg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 int i, j;
Bram Moolenaar67c53842010-05-22 18:28:27 +02003172 int use_fg = (*fg && STRCMP(fg, "none") != 0);
3173 int use_bg = (*bg && STRCMP(bg, "none") != 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174
3175 for (i = 0; i < globalsignmapused; i++)
3176 if (STRCMP(typeName, globalsignmap[i]) == 0)
3177 break;
3178
3179 if (i == globalsignmapused) /* not found; add it to global map */
3180 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003181 nbdebug(("DEFINEANNOTYPE(%d,%s,%s,%s,%s,%s)\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 typeNum, typeName, tooltip, glyphFile, fg, bg));
3183 if (use_fg || use_bg)
3184 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003185 char fgbuf[2 * (8 + MAX_COLOR_LENGTH) + 1];
3186 char bgbuf[2 * (8 + MAX_COLOR_LENGTH) + 1];
3187 char *ptr;
3188 int value;
3189
3190 value = strtol((char *)fg, &ptr, 10);
3191 if (ptr != (char *)fg)
3192 sprintf(fgbuf, "guifg=#%06x", value & 0xFFFFFF);
3193 else
3194 sprintf(fgbuf, "guifg=%s ctermfg=%s", fg, fg);
3195
3196 value = strtol((char *)bg, &ptr, 10);
3197 if (ptr != (char *)bg)
3198 sprintf(bgbuf, "guibg=#%06x", value & 0xFFFFFF);
3199 else
3200 sprintf(bgbuf, "guibg=%s ctermbg=%s", bg, bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201
3202 coloncmd(":highlight NB_%s %s %s", typeName, (use_fg) ? fgbuf : "",
3203 (use_bg) ? bgbuf : "");
3204 if (*glyphFile == NUL)
3205 /* no glyph, line highlighting only */
3206 coloncmd(":sign define %d linehl=NB_%s", i + 1, typeName);
3207 else if (vim_strsize(glyphFile) <= 2)
3208 /* one- or two-character glyph name, use as text glyph with
3209 * texthl */
3210 coloncmd(":sign define %d text=%s texthl=NB_%s", i + 1,
3211 glyphFile, typeName);
3212 else
3213 /* glyph, line highlighting */
3214 coloncmd(":sign define %d icon=%s linehl=NB_%s", i + 1,
3215 glyphFile, typeName);
3216 }
3217 else
3218 /* glyph, no line highlighting */
3219 coloncmd(":sign define %d icon=%s", i + 1, glyphFile);
3220
3221 if (STRCMP(typeName,"CurrentPC") == 0)
3222 curPCtype = typeNum;
3223
3224 if (globalsignmapused == globalsignmaplen)
3225 {
3226 if (globalsignmaplen == 0) /* first allocation */
3227 {
3228 globalsignmaplen = 20;
3229 globalsignmap = (char **)alloc_clear(globalsignmaplen*sizeof(char *));
3230 }
3231 else /* grow it */
3232 {
3233 int incr;
3234 int oldlen = globalsignmaplen;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003235 char **t_globalsignmap = globalsignmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236
3237 globalsignmaplen *= 2;
3238 incr = globalsignmaplen - oldlen;
3239 globalsignmap = (char **)vim_realloc(globalsignmap,
3240 globalsignmaplen * sizeof(char *));
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003241 if (globalsignmap == NULL)
3242 {
3243 vim_free(t_globalsignmap);
3244 globalsignmaplen = 0;
3245 return;
3246 }
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003247 vim_memset(globalsignmap + oldlen, 0, incr * sizeof(char *));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 }
3249 }
3250
3251 globalsignmap[i] = (char *)typeName;
3252 globalsignmapused = i + 1;
3253 }
3254
3255 /* check local map; should *not* be found! */
3256 for (j = 0; j < buf->signmapused; j++)
3257 if (buf->signmap[j] == i + 1)
3258 return;
3259
3260 /* add to local map */
3261 if (buf->signmapused == buf->signmaplen)
3262 {
3263 if (buf->signmaplen == 0) /* first allocation */
3264 {
3265 buf->signmaplen = 5;
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003266 buf->signmap = (int *)alloc_clear(buf->signmaplen * sizeof(int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 }
3268 else /* grow it */
3269 {
3270 int incr;
3271 int oldlen = buf->signmaplen;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003272 int *t_signmap = buf->signmap;
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003273
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 buf->signmaplen *= 2;
3275 incr = buf->signmaplen - oldlen;
3276 buf->signmap = (int *)vim_realloc(buf->signmap,
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003277 buf->signmaplen * sizeof(int));
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003278 if (buf->signmap == NULL)
3279 {
3280 vim_free(t_signmap);
3281 buf->signmaplen = 0;
3282 return;
3283 }
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003284 vim_memset(buf->signmap + oldlen, 0, incr * sizeof(int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 }
3286 }
3287
3288 buf->signmap[buf->signmapused++] = i + 1;
3289
3290}
3291
3292
3293/*
3294 * See if we have the requested sign type in the buffer.
3295 */
3296 static int
3297mapsigntype(nbbuf_T *buf, int localsigntype)
3298{
3299 if (--localsigntype >= 0 && localsigntype < buf->signmapused)
3300 return buf->signmap[localsigntype];
3301
3302 return 0;
3303}
3304
3305
3306/*
3307 * Compute length of buffer, don't print anything.
3308 */
3309 static long
3310get_buf_size(buf_T *bufp)
3311{
3312 linenr_T lnum;
3313 long char_count = 0;
3314 int eol_size;
3315 long last_check = 100000L;
3316
3317 if (bufp->b_ml.ml_flags & ML_EMPTY)
3318 return 0;
3319 else
3320 {
3321 if (get_fileformat(bufp) == EOL_DOS)
3322 eol_size = 2;
3323 else
3324 eol_size = 1;
3325 for (lnum = 1; lnum <= bufp->b_ml.ml_line_count; ++lnum)
3326 {
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00003327 char_count += (long)STRLEN(ml_get_buf(bufp, lnum, FALSE))
3328 + eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 /* Check for a CTRL-C every 100000 characters */
3330 if (char_count > last_check)
3331 {
3332 ui_breakcheck();
3333 if (got_int)
3334 return char_count;
3335 last_check = char_count + 100000L;
3336 }
3337 }
3338 /* Correction for when last line doesn't have an EOL. */
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003339 if (!bufp->b_p_eol && (bufp->b_p_bin || !bufp->b_p_fixeol))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 char_count -= eol_size;
3341 }
3342
3343 return char_count;
3344}
3345
3346/*
3347 * Convert character offset to lnum,col
3348 */
3349 static pos_T *
3350off2pos(buf_T *buf, long offset)
3351{
3352 linenr_T lnum;
3353 static pos_T pos;
3354
3355 pos.lnum = 0;
3356 pos.col = 0;
3357#ifdef FEAT_VIRTUALEDIT
3358 pos.coladd = 0;
3359#endif
3360
3361 if (!(buf->b_ml.ml_flags & ML_EMPTY))
3362 {
3363 if ((lnum = ml_find_line_or_offset(buf, (linenr_T)0, &offset)) < 0)
3364 return NULL;
3365 pos.lnum = lnum;
3366 pos.col = offset;
3367 }
3368
3369 return &pos;
3370}
3371
3372/*
3373 * Convert an argument in the form "1234" to an offset and compute the
3374 * lnum/col from it. Convert an argument in the form "123/12" directly to a
3375 * lnum/col.
3376 * "argp" is advanced to after the argument.
3377 * Return a pointer to the position, NULL if something is wrong.
3378 */
3379 static pos_T *
3380get_off_or_lnum(buf_T *buf, char_u **argp)
3381{
3382 static pos_T mypos;
3383 long off;
3384
3385 off = strtol((char *)*argp, (char **)argp, 10);
3386 if (**argp == '/')
3387 {
3388 mypos.lnum = (linenr_T)off;
3389 ++*argp;
3390 mypos.col = strtol((char *)*argp, (char **)argp, 10);
3391#ifdef FEAT_VIRTUALEDIT
3392 mypos.coladd = 0;
3393#endif
3394 return &mypos;
3395 }
3396 return off2pos(buf, off);
3397}
3398
3399
3400/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003401 * Convert (lnum,col) to byte offset in the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 */
3403 static long
3404pos2off(buf_T *buf, pos_T *pos)
3405{
3406 long offset = 0;
3407
3408 if (!(buf->b_ml.ml_flags & ML_EMPTY))
3409 {
3410 if ((offset = ml_find_line_or_offset(buf, pos->lnum, 0)) < 0)
3411 return 0;
3412 offset += pos->col;
3413 }
3414
3415 return offset;
3416}
3417
3418
Bram Moolenaar009b2592004-10-24 19:18:58 +00003419/*
Bram Moolenaar9af41842016-09-25 21:45:05 +02003420 * This message is printed after NetBeans opens a new file. It's
Bram Moolenaar009b2592004-10-24 19:18:58 +00003421 * similar to the message readfile() uses, but since NetBeans
3422 * doesn't normally call readfile, we do our own.
3423 */
3424 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003425print_read_msg(nbbuf_T *buf)
Bram Moolenaar009b2592004-10-24 19:18:58 +00003426{
3427 int lnum = buf->bufp->b_ml.ml_line_count;
Bram Moolenaar8767f522016-07-01 17:17:39 +02003428 off_T nchars = buf->bufp->b_orig_size;
Bram Moolenaar009b2592004-10-24 19:18:58 +00003429 char_u c;
3430
3431 msg_add_fname(buf->bufp, buf->bufp->b_ffname);
3432 c = FALSE;
3433
3434 if (buf->bufp->b_p_ro)
3435 {
3436 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]"));
3437 c = TRUE;
3438 }
3439 if (!buf->bufp->b_start_eol)
3440 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003441 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]")
3442 : _("[Incomplete last line]"));
Bram Moolenaar009b2592004-10-24 19:18:58 +00003443 c = TRUE;
3444 }
3445 msg_add_lines(c, (long)lnum, nchars);
3446
3447 /* Now display it */
Bram Moolenaard23a8232018-02-10 18:45:26 +01003448 VIM_CLEAR(keep_msg);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003449 msg_scrolled_ign = TRUE;
3450 msg_trunc_attr(IObuff, FALSE, 0);
3451 msg_scrolled_ign = FALSE;
3452}
3453
3454
3455/*
Bram Moolenaar67c53842010-05-22 18:28:27 +02003456 * Print a message after NetBeans writes the file. This message should be
3457 * identical to the standard message a non-netbeans user would see when
3458 * writing a file.
Bram Moolenaar009b2592004-10-24 19:18:58 +00003459 */
3460 static void
Bram Moolenaar8767f522016-07-01 17:17:39 +02003461print_save_msg(nbbuf_T *buf, off_T nchars)
Bram Moolenaar009b2592004-10-24 19:18:58 +00003462{
3463 char_u c;
3464 char_u *p;
3465
3466 if (nchars >= 0)
3467 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003468 /* put fname in IObuff with quotes */
3469 msg_add_fname(buf->bufp, buf->bufp->b_ffname);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003470 c = FALSE;
3471
3472 msg_add_lines(c, buf->bufp->b_ml.ml_line_count,
Bram Moolenaar914703b2010-05-31 21:59:46 +02003473 buf->bufp->b_orig_size);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003474
Bram Moolenaard23a8232018-02-10 18:45:26 +01003475 VIM_CLEAR(keep_msg);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003476 msg_scrolled_ign = TRUE;
3477 p = msg_trunc_attr(IObuff, FALSE, 0);
3478 if ((msg_scrolled && !need_wait_return) || !buf->initDone)
3479 {
3480 /* Need to repeat the message after redrawing when:
3481 * - When reading from stdin (the screen will be cleared next).
3482 * - When restart_edit is set (otherwise there will be a delay
3483 * before redrawing).
3484 * - When the screen was scrolled but there is no wait-return
3485 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003486 set_keep_msg(p, 0);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003487 }
3488 msg_scrolled_ign = FALSE;
3489 /* add_to_input_buf((char_u *)"\f", 1); */
3490 }
3491 else
3492 {
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003493 char_u msgbuf[IOSIZE];
Bram Moolenaar009b2592004-10-24 19:18:58 +00003494
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003495 vim_snprintf((char *)msgbuf, IOSIZE,
3496 _("E505: %s is read-only (add ! to override)"), IObuff);
3497 nbdebug((" %s\n", msgbuf));
3498 emsg(msgbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003499 }
3500}
3501
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502#endif /* defined(FEAT_NETBEANS_INTG) */