blob: a753a4f9740a63df4193d072d5ac3a8e84b5d49b [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 apply_autocmds(EVENT_BUFREADPOST, 0, 0, FALSE, buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585
1586 /* handle any postponed key commands */
1587 handle_key_queue();
1588/* =====================================================================*/
1589 }
1590 else if (streq((char *)cmd, "setBufferNumber")
1591 || streq((char *)cmd, "putBufferNumber"))
1592 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00001593 char_u *path;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 buf_T *bufp;
1595
1596 if (buf == NULL)
1597 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001598 nbdebug((" invalid buffer identifier in setBufferNumber\n"));
1599 EMSG("E641: invalid buffer identifier in setBufferNumber");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 return FAIL;
1601 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001602 path = (char_u *)nb_unquote(args, NULL);
1603 if (path == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 return FAIL;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001605 bufp = buflist_findname(path);
1606 vim_free(path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 if (bufp == NULL)
1608 {
Bram Moolenaarec906222009-02-21 21:14:00 +00001609 nbdebug((" File %s not found in setBufferNumber\n", args));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 EMSG2("E642: File %s not found in setBufferNumber", args);
1611 return FAIL;
1612 }
1613 buf->bufp = bufp;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001614 buf->nbbuf_number = bufp->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615
1616 /* "setBufferNumber" has the side effect of jumping to the buffer
1617 * (don't know why!). Don't do that for "putBufferNumber". */
1618 if (*cmd != 'p')
1619 coloncmd(":buffer %d", bufp->b_fnum);
1620 else
1621 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00001622 buf->initDone = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623
1624 /* handle any postponed key commands */
1625 handle_key_queue();
1626 }
1627
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628/* =====================================================================*/
1629 }
1630 else if (streq((char *)cmd, "setFullName"))
1631 {
1632 if (buf == NULL)
1633 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001634 nbdebug((" invalid buffer identifier in setFullName\n"));
1635 EMSG("E643: invalid buffer identifier in setFullName");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 return FAIL;
1637 }
1638 vim_free(buf->displayname);
1639 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640
1641 netbeansReadFile = 0; /* don't try to open disk file */
1642 do_ecmd(0, (char_u *)buf->displayname, 0, 0, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001643 ECMD_HIDE + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 netbeansReadFile = 1;
1645 buf->bufp = curbuf;
1646 maketitle();
Bram Moolenaar67c53842010-05-22 18:28:27 +02001647#if defined(FEAT_MENU) && defined(FEAT_GUI)
Bram Moolenaard54a6882010-08-09 22:49:00 +02001648 if (gui.in_use)
1649 gui_update_menus(0);
Bram Moolenaar67c53842010-05-22 18:28:27 +02001650#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651/* =====================================================================*/
1652 }
1653 else if (streq((char *)cmd, "editFile"))
1654 {
1655 if (buf == NULL)
1656 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001657 nbdebug((" invalid buffer identifier in editFile\n"));
1658 EMSG("E644: invalid buffer identifier in editFile");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 return FAIL;
1660 }
1661 /* Edit a file: like create + setFullName + read the file. */
1662 vim_free(buf->displayname);
1663 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 do_ecmd(0, (char_u *)buf->displayname, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001665 ECMD_HIDE + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 buf->bufp = curbuf;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001667 buf->initDone = TRUE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001668 do_update = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669#if defined(FEAT_TITLE)
1670 maketitle();
1671#endif
Bram Moolenaar67c53842010-05-22 18:28:27 +02001672#if defined(FEAT_MENU) && defined(FEAT_GUI)
Bram Moolenaard54a6882010-08-09 22:49:00 +02001673 if (gui.in_use)
1674 gui_update_menus(0);
Bram Moolenaar67c53842010-05-22 18:28:27 +02001675#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676/* =====================================================================*/
1677 }
1678 else if (streq((char *)cmd, "setVisible"))
1679 {
1680 if (buf == NULL || buf->bufp == NULL)
1681 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001682 nbdebug((" invalid buffer identifier in setVisible\n"));
1683 /* This message was commented out, probably because it can
1684 * happen when shutting down. */
1685 if (p_verbose > 0)
1686 EMSG("E645: invalid buffer identifier in setVisible");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 return FAIL;
1688 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001689 if (streq((char *)args, "T") && buf->bufp != curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 {
1691 exarg_T exarg;
1692 exarg.cmd = (char_u *)"goto";
1693 exarg.forceit = FALSE;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001694 dosetvisible = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 goto_buffer(&exarg, DOBUF_FIRST, FORWARD, buf->bufp->b_fnum);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001696 do_update = 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001697 dosetvisible = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698
Bram Moolenaar67c53842010-05-22 18:28:27 +02001699#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 /* Side effect!!!. */
Bram Moolenaard54a6882010-08-09 22:49:00 +02001701 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 gui_mch_set_foreground();
Bram Moolenaar67c53842010-05-22 18:28:27 +02001703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705/* =====================================================================*/
1706 }
1707 else if (streq((char *)cmd, "raise"))
1708 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02001709#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 /* Bring gvim to the foreground. */
Bram Moolenaard54a6882010-08-09 22:49:00 +02001711 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 gui_mch_set_foreground();
Bram Moolenaar67c53842010-05-22 18:28:27 +02001713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714/* =====================================================================*/
1715 }
1716 else if (streq((char *)cmd, "setModified"))
1717 {
Bram Moolenaarff064e12008-06-09 13:10:45 +00001718 int prev_b_changed;
1719
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 if (buf == NULL || buf->bufp == NULL)
1721 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001722 nbdebug((" invalid buffer identifier in setModified\n"));
1723 /* This message was commented out, probably because it can
1724 * happen when shutting down. */
1725 if (p_verbose > 0)
1726 EMSG("E646: invalid buffer identifier in setModified");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 return FAIL;
1728 }
Bram Moolenaarff064e12008-06-09 13:10:45 +00001729 prev_b_changed = buf->bufp->b_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 if (streq((char *)args, "T"))
Bram Moolenaarff064e12008-06-09 13:10:45 +00001731 buf->bufp->b_changed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 else
1733 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02001734 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735
1736 /* Assume NetBeans stored the file. Reset the timestamp to
1737 * avoid "file changed" warnings. */
1738 if (buf->bufp->b_ffname != NULL
1739 && mch_stat((char *)buf->bufp->b_ffname, &st) >= 0)
1740 buf_store_time(buf->bufp, &st, buf->bufp->b_ffname);
Bram Moolenaarff064e12008-06-09 13:10:45 +00001741 buf->bufp->b_changed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 }
1743 buf->modified = buf->bufp->b_changed;
Bram Moolenaarff064e12008-06-09 13:10:45 +00001744 if (prev_b_changed != buf->bufp->b_changed)
1745 {
Bram Moolenaarff064e12008-06-09 13:10:45 +00001746 check_status(buf->bufp);
1747 redraw_tabline = TRUE;
Bram Moolenaarff064e12008-06-09 13:10:45 +00001748#ifdef FEAT_TITLE
1749 maketitle();
1750#endif
1751 update_screen(0);
1752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753/* =====================================================================*/
1754 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001755 else if (streq((char *)cmd, "setModtime"))
1756 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001757 if (buf == NULL || buf->bufp == NULL)
Bram Moolenaarf2330482008-06-24 20:19:36 +00001758 nbdebug((" invalid buffer identifier in setModtime\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001759 else
1760 buf->bufp->b_mtime = atoi((char *)args);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001761/* =====================================================================*/
1762 }
1763 else if (streq((char *)cmd, "setReadOnly"))
1764 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001765 if (buf == NULL || buf->bufp == NULL)
Bram Moolenaarf2330482008-06-24 20:19:36 +00001766 nbdebug((" invalid buffer identifier in setReadOnly\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001767 else if (streq((char *)args, "T"))
Bram Moolenaar009b2592004-10-24 19:18:58 +00001768 buf->bufp->b_p_ro = TRUE;
1769 else
1770 buf->bufp->b_p_ro = FALSE;
1771/* =====================================================================*/
1772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 else if (streq((char *)cmd, "setMark"))
1774 {
1775 /* not yet */
1776/* =====================================================================*/
1777 }
1778 else if (streq((char *)cmd, "showBalloon"))
1779 {
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001780#if defined(FEAT_BEVAL_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 static char *text = NULL;
1782
1783 /*
1784 * Set up the Balloon Expression Evaluation area.
1785 * Ignore 'ballooneval' here.
1786 * The text pointer must remain valid for a while.
1787 */
1788 if (balloonEval != NULL)
1789 {
1790 vim_free(text);
1791 text = nb_unquote(args, NULL);
1792 if (text != NULL)
1793 gui_mch_post_balloon(balloonEval, (char_u *)text);
1794 }
1795#endif
1796/* =====================================================================*/
1797 }
1798 else if (streq((char *)cmd, "setDot"))
1799 {
1800 pos_T *pos;
1801#ifdef NBDEBUG
1802 char_u *s;
1803#endif
1804
1805 if (buf == NULL || buf->bufp == NULL)
1806 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001807 nbdebug((" invalid buffer identifier in setDot\n"));
1808 EMSG("E647: invalid buffer identifier in setDot");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 return FAIL;
1810 }
1811
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001812 nb_set_curbuf(buf->bufp);
1813
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 /* Don't want Visual mode now. */
1815 if (VIsual_active)
1816 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817#ifdef NBDEBUG
1818 s = args;
1819#endif
1820 pos = get_off_or_lnum(buf->bufp, &args);
1821 if (pos)
1822 {
1823 curwin->w_cursor = *pos;
1824 check_cursor();
1825#ifdef FEAT_FOLDING
1826 foldOpenCursor();
1827#endif
1828 }
1829 else
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001830 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 nbdebug((" BAD POSITION in setDot: %s\n", s));
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833
1834 /* gui_update_cursor(TRUE, FALSE); */
1835 /* update_curbuf(NOT_VALID); */
1836 update_topline(); /* scroll to show the line */
1837 update_screen(VALID);
1838 setcursor();
Bram Moolenaar96bcc5e2011-04-01 15:33:59 +02001839 cursor_on();
Bram Moolenaara338adc2018-01-31 20:51:47 +01001840 out_flush_cursor(TRUE, FALSE);
1841
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 /* Quit a hit-return or more prompt. */
1843 if (State == HITRETURN || State == ASKMORE)
1844 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845#ifdef FEAT_GUI_GTK
Bram Moolenaard54a6882010-08-09 22:49:00 +02001846 if (gui.in_use && gtk_main_level() > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 gtk_main_quit();
1848#endif
1849 }
1850/* =====================================================================*/
1851 }
1852 else if (streq((char *)cmd, "close"))
1853 {
1854#ifdef NBDEBUG
1855 char *name = "<NONE>";
1856#endif
1857
1858 if (buf == NULL)
1859 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001860 nbdebug((" invalid buffer identifier in close\n"));
1861 EMSG("E648: invalid buffer identifier in close");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 return FAIL;
1863 }
1864
1865#ifdef NBDEBUG
1866 if (buf->displayname != NULL)
1867 name = buf->displayname;
1868#endif
Bram Moolenaarf2330482008-06-24 20:19:36 +00001869 if (buf->bufp == NULL)
1870 {
1871 nbdebug((" invalid buffer identifier in close\n"));
1872 /* This message was commented out, probably because it can
1873 * happen when shutting down. */
1874 if (p_verbose > 0)
1875 EMSG("E649: invalid buffer identifier in close");
1876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 nbdebug((" CLOSE %d: %s\n", bufno, name));
Bram Moolenaar67c53842010-05-22 18:28:27 +02001878#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 need_mouse_correct = TRUE;
Bram Moolenaar67c53842010-05-22 18:28:27 +02001880#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 if (buf->bufp != NULL)
1882 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD,
1883 buf->bufp->b_fnum, TRUE);
Bram Moolenaar8d602722006-08-08 19:34:19 +00001884 buf->bufp = NULL;
1885 buf->initDone = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001886 do_update = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887/* =====================================================================*/
1888 }
1889 else if (streq((char *)cmd, "setStyle")) /* obsolete... */
1890 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001891 nbdebug((" setStyle is obsolete!\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892/* =====================================================================*/
1893 }
1894 else if (streq((char *)cmd, "setExitDelay"))
1895 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00001896 /* Only used in version 2.1. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897/* =====================================================================*/
1898 }
1899 else if (streq((char *)cmd, "defineAnnoType"))
1900 {
1901#ifdef FEAT_SIGNS
1902 int typeNum;
1903 char_u *typeName;
1904 char_u *tooltip;
1905 char_u *p;
1906 char_u *glyphFile;
Bram Moolenaar67c53842010-05-22 18:28:27 +02001907 int parse_error = FALSE;
1908 char_u *fg;
1909 char_u *bg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910
1911 if (buf == NULL)
1912 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001913 nbdebug((" invalid buffer identifier in defineAnnoType\n"));
1914 EMSG("E650: invalid buffer identifier in defineAnnoType");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 return FAIL;
1916 }
1917
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001918 cp = (char *)args;
1919 typeNum = strtol(cp, &cp, 10);
1920 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 args = skipwhite(args);
1922 typeName = (char_u *)nb_unquote(args, &args);
1923 args = skipwhite(args + 1);
1924 tooltip = (char_u *)nb_unquote(args, &args);
1925 args = skipwhite(args + 1);
1926
1927 p = (char_u *)nb_unquote(args, &args);
1928 glyphFile = vim_strsave_escaped(p, escape_chars);
1929 vim_free(p);
1930
1931 args = skipwhite(args + 1);
Bram Moolenaar67c53842010-05-22 18:28:27 +02001932 p = skiptowhite(args);
1933 if (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02001935 *p = NUL;
1936 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02001938 fg = vim_strsave(args);
1939 bg = vim_strsave(p);
1940 if (STRLEN(fg) > MAX_COLOR_LENGTH || STRLEN(bg) > MAX_COLOR_LENGTH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02001942 EMSG("E532: highlighting color name too long in defineAnnoType");
1943 vim_free(typeName);
1944 parse_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02001946 else if (typeName != NULL && tooltip != NULL && glyphFile != NULL)
1947 addsigntype(buf, typeNum, typeName, tooltip, glyphFile, fg, bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 else
1949 vim_free(typeName);
1950
1951 /* don't free typeName; it's used directly in addsigntype() */
Bram Moolenaar67c53842010-05-22 18:28:27 +02001952 vim_free(fg);
1953 vim_free(bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 vim_free(tooltip);
1955 vim_free(glyphFile);
Bram Moolenaar67c53842010-05-22 18:28:27 +02001956 if (parse_error)
1957 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958
1959#endif
1960/* =====================================================================*/
1961 }
1962 else if (streq((char *)cmd, "addAnno"))
1963 {
1964#ifdef FEAT_SIGNS
1965 int serNum;
1966 int localTypeNum;
1967 int typeNum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 pos_T *pos;
1969
1970 if (buf == NULL || buf->bufp == NULL)
1971 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001972 nbdebug((" invalid buffer identifier in addAnno\n"));
1973 EMSG("E651: invalid buffer identifier in addAnno");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 return FAIL;
1975 }
1976
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001977 do_update = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001979 cp = (char *)args;
1980 serNum = strtol(cp, &cp, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981
1982 /* Get the typenr specific for this buffer and convert it to
1983 * the global typenumber, as used for the sign name. */
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001984 localTypeNum = strtol(cp, &cp, 10);
1985 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 typeNum = mapsigntype(buf, localTypeNum);
1987
1988 pos = get_off_or_lnum(buf->bufp, &args);
1989
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001990 cp = (char *)args;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00001991 ignored = (int)strtol(cp, &cp, 10);
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001992 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993# ifdef NBDEBUG
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00001994 if (ignored != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001996 nbdebug((" partial line annotation -- Not Yet Implemented!\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 }
1998# endif
1999 if (serNum >= GUARDEDOFFSET)
2000 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002001 nbdebug((" too many annotations! ignoring...\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 return FAIL;
2003 }
2004 if (pos)
2005 {
Bram Moolenaarec906222009-02-21 21:14:00 +00002006 coloncmd(":sign place %d line=%ld name=%d buffer=%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 serNum, pos->lnum, typeNum, buf->bufp->b_fnum);
2008 if (typeNum == curPCtype)
2009 coloncmd(":sign jump %d buffer=%d", serNum,
2010 buf->bufp->b_fnum);
2011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012#endif
2013/* =====================================================================*/
2014 }
2015 else if (streq((char *)cmd, "removeAnno"))
2016 {
2017#ifdef FEAT_SIGNS
2018 int serNum;
2019
2020 if (buf == NULL || buf->bufp == NULL)
2021 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002022 nbdebug((" invalid buffer identifier in removeAnno\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 return FAIL;
2024 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002025 do_update = 1;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002026 cp = (char *)args;
2027 serNum = strtol(cp, &cp, 10);
2028 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 coloncmd(":sign unplace %d buffer=%d",
2030 serNum, buf->bufp->b_fnum);
2031 redraw_buf_later(buf->bufp, NOT_VALID);
2032#endif
2033/* =====================================================================*/
2034 }
2035 else if (streq((char *)cmd, "moveAnnoToFront"))
2036 {
2037#ifdef FEAT_SIGNS
Bram Moolenaarf2330482008-06-24 20:19:36 +00002038 nbdebug((" moveAnnoToFront: Not Yet Implemented!\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039#endif
2040/* =====================================================================*/
2041 }
2042 else if (streq((char *)cmd, "guard") || streq((char *)cmd, "unguard"))
2043 {
2044 int len;
2045 pos_T first;
2046 pos_T last;
2047 pos_T *pos;
2048 int un = (cmd[0] == 'u');
2049 static int guardId = GUARDEDOFFSET;
2050
2051 if (skip >= SKIP_STOP)
2052 {
2053 nbdebug((" Skipping %s command\n", (char *) cmd));
2054 return OK;
2055 }
2056
2057 nb_init_graphics();
2058
2059 if (buf == NULL || buf->bufp == NULL)
2060 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002061 nbdebug((" invalid buffer identifier in %s command\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 return FAIL;
2063 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002064 nb_set_curbuf(buf->bufp);
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002065 cp = (char *)args;
2066 off = strtol(cp, &cp, 10);
2067 len = strtol(cp, NULL, 10);
2068 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 pos = off2pos(buf->bufp, off);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002070 do_update = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 if (!pos)
2072 nbdebug((" no such start pos in %s, %ld\n", cmd, off));
2073 else
2074 {
2075 first = *pos;
2076 pos = off2pos(buf->bufp, off + len - 1);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002077 if (pos != NULL && pos->col == 0)
2078 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 /*
2080 * In Java Swing the offset is a position between 2
2081 * characters. If col == 0 then we really want the
2082 * previous line as the end.
2083 */
2084 pos = off2pos(buf->bufp, off + len - 2);
2085 }
2086 if (!pos)
2087 nbdebug((" no such end pos in %s, %ld\n",
2088 cmd, off + len - 1));
2089 else
2090 {
2091 long lnum;
2092 last = *pos;
2093 /* set highlight for region */
2094 nbdebug((" %sGUARD %ld,%d to %ld,%d\n", (un) ? "UN" : "",
2095 first.lnum, first.col,
2096 last.lnum, last.col));
2097#ifdef FEAT_SIGNS
2098 for (lnum = first.lnum; lnum <= last.lnum; lnum++)
2099 {
2100 if (un)
2101 {
2102 /* never used */
2103 }
2104 else
2105 {
2106 if (buf_findsigntype_id(buf->bufp, lnum,
2107 GUARDED) == 0)
2108 {
2109 coloncmd(
Bram Moolenaarec906222009-02-21 21:14:00 +00002110 ":sign place %d line=%ld name=%d buffer=%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 guardId++, lnum, GUARDED,
2112 buf->bufp->b_fnum);
2113 }
2114 }
2115 }
2116#endif
2117 redraw_buf_later(buf->bufp, NOT_VALID);
2118 }
2119 }
2120/* =====================================================================*/
2121 }
2122 else if (streq((char *)cmd, "startAtomic"))
2123 {
2124 inAtomic = 1;
2125/* =====================================================================*/
2126 }
2127 else if (streq((char *)cmd, "endAtomic"))
2128 {
2129 inAtomic = 0;
2130 if (needupdate)
2131 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002132 do_update = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 needupdate = 0;
2134 }
2135/* =====================================================================*/
2136 }
2137 else if (streq((char *)cmd, "save"))
2138 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002139 /*
Bram Moolenaar9af41842016-09-25 21:45:05 +02002140 * NOTE - This command is obsolete wrt NetBeans. It's left in
Bram Moolenaar009b2592004-10-24 19:18:58 +00002141 * only for historical reasons.
2142 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 if (buf == NULL || buf->bufp == NULL)
2144 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002145 nbdebug((" invalid buffer identifier in %s command\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 return FAIL;
2147 }
2148
2149 /* the following is taken from ex_cmds.c (do_wqall function) */
2150 if (bufIsChanged(buf->bufp))
2151 {
2152 /* Only write if the buffer can be written. */
2153 if (p_write
2154 && !buf->bufp->b_p_ro
2155 && buf->bufp->b_ffname != NULL
2156#ifdef FEAT_QUICKFIX
2157 && !bt_dontwrite(buf->bufp)
2158#endif
2159 )
2160 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002161 bufref_T bufref;
2162
2163 set_bufref(&bufref, buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 buf_write_all(buf->bufp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 /* an autocommand may have deleted the buffer */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002166 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 buf->bufp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 }
2169 }
Bram Moolenaarf2330482008-06-24 20:19:36 +00002170 else
2171 {
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002172 nbdebug((" Buffer has no changes!\n"));
Bram Moolenaarf2330482008-06-24 20:19:36 +00002173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174/* =====================================================================*/
2175 }
2176 else if (streq((char *)cmd, "netbeansBuffer"))
2177 {
2178 if (buf == NULL || buf->bufp == NULL)
2179 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002180 nbdebug((" invalid buffer identifier in %s command\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 return FAIL;
2182 }
2183 if (*args == 'T')
2184 {
2185 buf->bufp->b_netbeans_file = TRUE;
2186 buf->bufp->b_was_netbeans_file = TRUE;
2187 }
2188 else
2189 buf->bufp->b_netbeans_file = FALSE;
2190/* =====================================================================*/
2191 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002192 else if (streq((char *)cmd, "specialKeys"))
2193 {
2194 special_keys(args);
2195/* =====================================================================*/
2196 }
2197 else if (streq((char *)cmd, "actionMenuItem"))
2198 {
2199 /* not used yet */
2200/* =====================================================================*/
2201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 else if (streq((char *)cmd, "version"))
2203 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002204 /* not used yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 }
Bram Moolenaarf2330482008-06-24 20:19:36 +00002206 else
2207 {
2208 nbdebug(("Unrecognised command: %s\n", cmd));
2209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 /*
2211 * Unrecognized command is ignored.
2212 */
2213 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002214 if (inAtomic && do_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 {
2216 needupdate = 1;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002217 do_update = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 }
2219
Bram Moolenaar009b2592004-10-24 19:18:58 +00002220 /*
2221 * Is this needed? I moved the netbeans_Xt_connect() later during startup
Bram Moolenaar9af41842016-09-25 21:45:05 +02002222 * and it may no longer be necessary. If it's not needed then needupdate
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002223 * and do_update can also be removed.
Bram Moolenaar009b2592004-10-24 19:18:58 +00002224 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002225 if (buf != NULL && buf->initDone && do_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 {
2227 update_screen(NOT_VALID);
2228 setcursor();
Bram Moolenaar96bcc5e2011-04-01 15:33:59 +02002229 cursor_on();
Bram Moolenaara338adc2018-01-31 20:51:47 +01002230 out_flush_cursor(TRUE, FALSE);
2231
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 /* Quit a hit-return or more prompt. */
2233 if (State == HITRETURN || State == ASKMORE)
2234 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235#ifdef FEAT_GUI_GTK
Bram Moolenaard54a6882010-08-09 22:49:00 +02002236 if (gui.in_use && gtk_main_level() > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 gtk_main_quit();
2238#endif
2239 }
2240 }
2241
2242 return retval;
2243}
2244
2245
2246/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002247 * If "buf" is not the current buffer try changing to a window that edits this
2248 * buffer. If there is no such window then close the current buffer and set
2249 * the current buffer as "buf".
2250 */
2251 static void
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00002252nb_set_curbuf(buf_T *buf)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002253{
Bram Moolenaar404c9422015-03-14 15:35:52 +01002254 if (curbuf != buf) {
2255 if (buf_jump_open_win(buf) != NULL)
2256 return;
Bram Moolenaar404c9422015-03-14 15:35:52 +01002257 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf) != NULL)
2258 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002259 set_curbuf(buf, DOBUF_GOTO);
Bram Moolenaar404c9422015-03-14 15:35:52 +01002260 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002261}
2262
2263/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 * Process a vim colon command.
2265 */
2266 static void
2267coloncmd(char *cmd, ...)
2268{
2269 char buf[1024];
2270 va_list ap;
2271
2272 va_start(ap, cmd);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02002273 vim_vsnprintf(buf, sizeof(buf), cmd, ap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 va_end(ap);
2275
2276 nbdebug((" COLONCMD %s\n", buf));
2277
2278/* ALT_INPUT_LOCK_ON; */
2279 do_cmdline((char_u *)buf, NULL, NULL, DOCMD_NOWAIT | DOCMD_KEYTYPED);
2280/* ALT_INPUT_LOCK_OFF; */
2281
2282 setcursor(); /* restore the cursor position */
Bram Moolenaara338adc2018-01-31 20:51:47 +01002283 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284}
2285
2286
2287/*
Bram Moolenaar009b2592004-10-24 19:18:58 +00002288 * Parse the specialKeys argument and issue the appropriate map commands.
2289 */
2290 static void
2291special_keys(char_u *args)
2292{
2293 char *save_str = nb_unquote(args, NULL);
2294 char *tok = strtok(save_str, " ");
2295 char *sep;
Bram Moolenaarca24e2c2017-01-22 15:19:22 +01002296#define KEYBUFLEN 64
2297 char keybuf[KEYBUFLEN];
Bram Moolenaar009b2592004-10-24 19:18:58 +00002298 char cmdbuf[256];
2299
2300 while (tok != NULL)
2301 {
2302 int i = 0;
2303
2304 if ((sep = strchr(tok, '-')) != NULL)
2305 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002306 *sep = NUL;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002307 while (*tok)
2308 {
2309 switch (*tok)
2310 {
2311 case 'A':
2312 case 'M':
2313 case 'C':
2314 case 'S':
2315 keybuf[i++] = *tok;
2316 keybuf[i++] = '-';
2317 break;
2318 }
2319 tok++;
2320 }
2321 tok++;
2322 }
2323
Bram Moolenaarca24e2c2017-01-22 15:19:22 +01002324 if (strlen(tok) + i < KEYBUFLEN)
2325 {
2326 strcpy(&keybuf[i], tok);
2327 vim_snprintf(cmdbuf, sizeof(cmdbuf),
2328 "<silent><%s> :nbkey %s<CR>", keybuf, keybuf);
2329 do_map(0, (char_u *)cmdbuf, NORMAL, FALSE);
2330 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002331 tok = strtok(NULL, " ");
2332 }
2333 vim_free(save_str);
2334}
2335
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002336 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002337ex_nbclose(exarg_T *eap UNUSED)
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002338{
2339 netbeans_close();
2340}
Bram Moolenaar009b2592004-10-24 19:18:58 +00002341
2342 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002343ex_nbkey(exarg_T *eap)
Bram Moolenaar009b2592004-10-24 19:18:58 +00002344{
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002345 (void)netbeans_keystring(eap->arg);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002346}
2347
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002348 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002349ex_nbstart(
2350 exarg_T *eap)
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002351{
Bram Moolenaarc2a406b2010-09-30 21:03:26 +02002352#ifdef FEAT_GUI
2353# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar7ad7d012010-11-16 15:49:02 +01002354 && !defined(FEAT_GUI_W32)
Bram Moolenaarc2a406b2010-09-30 21:03:26 +02002355 if (gui.in_use)
2356 {
Bram Moolenaar7ad7d012010-11-16 15:49:02 +01002357 EMSG(_("E838: netbeans is not supported with this GUI"));
2358 return;
Bram Moolenaarc2a406b2010-09-30 21:03:26 +02002359 }
2360# endif
2361#endif
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002362 netbeans_open((char *)eap->arg, FALSE);
2363}
Bram Moolenaar009b2592004-10-24 19:18:58 +00002364
2365/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 * Initialize highlights and signs for use by netbeans (mostly obsolete)
2367 */
2368 static void
2369nb_init_graphics(void)
2370{
2371 static int did_init = FALSE;
2372
2373 if (!did_init)
2374 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002375 coloncmd(":highlight NBGuarded guibg=Cyan guifg=Black"
2376 " ctermbg=LightCyan ctermfg=Black");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 coloncmd(":sign define %d linehl=NBGuarded", GUARDED);
2378
2379 did_init = TRUE;
2380 }
2381}
2382
2383/*
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002384 * Convert key to netbeans name. This uses the global "mod_mask".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 */
2386 static void
2387netbeans_keyname(int key, char *buf)
2388{
2389 char *name = 0;
2390 char namebuf[2];
2391 int ctrl = 0;
2392 int shift = 0;
2393 int alt = 0;
2394
2395 if (mod_mask & MOD_MASK_CTRL)
2396 ctrl = 1;
2397 if (mod_mask & MOD_MASK_SHIFT)
2398 shift = 1;
2399 if (mod_mask & MOD_MASK_ALT)
2400 alt = 1;
2401
2402
2403 switch (key)
2404 {
2405 case K_F1: name = "F1"; break;
2406 case K_S_F1: name = "F1"; shift = 1; break;
2407 case K_F2: name = "F2"; break;
2408 case K_S_F2: name = "F2"; shift = 1; break;
2409 case K_F3: name = "F3"; break;
2410 case K_S_F3: name = "F3"; shift = 1; break;
2411 case K_F4: name = "F4"; break;
2412 case K_S_F4: name = "F4"; shift = 1; break;
2413 case K_F5: name = "F5"; break;
2414 case K_S_F5: name = "F5"; shift = 1; break;
2415 case K_F6: name = "F6"; break;
2416 case K_S_F6: name = "F6"; shift = 1; break;
2417 case K_F7: name = "F7"; break;
2418 case K_S_F7: name = "F7"; shift = 1; break;
2419 case K_F8: name = "F8"; break;
2420 case K_S_F8: name = "F8"; shift = 1; break;
2421 case K_F9: name = "F9"; break;
2422 case K_S_F9: name = "F9"; shift = 1; break;
2423 case K_F10: name = "F10"; break;
2424 case K_S_F10: name = "F10"; shift = 1; break;
2425 case K_F11: name = "F11"; break;
2426 case K_S_F11: name = "F11"; shift = 1; break;
2427 case K_F12: name = "F12"; break;
2428 case K_S_F12: name = "F12"; shift = 1; break;
2429 default:
2430 if (key >= ' ' && key <= '~')
2431 {
2432 /* Allow ASCII characters. */
2433 name = namebuf;
2434 namebuf[0] = key;
2435 namebuf[1] = NUL;
2436 }
2437 else
2438 name = "X";
2439 break;
2440 }
2441
2442 buf[0] = '\0';
2443 if (ctrl)
2444 strcat(buf, "C");
2445 if (shift)
2446 strcat(buf, "S");
2447 if (alt)
2448 strcat(buf, "M"); /* META */
2449 if (ctrl || shift || alt)
2450 strcat(buf, "-");
2451 strcat(buf, name);
2452}
2453
Bram Moolenaar67c53842010-05-22 18:28:27 +02002454#if defined(FEAT_BEVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455/*
2456 * Function to be called for balloon evaluation. Grabs the text under the
2457 * cursor and sends it to the debugger for evaluation. The debugger should
2458 * respond with a showBalloon command when there is a useful result.
2459 */
Bram Moolenaard62bec82005-03-07 22:56:57 +00002460 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461netbeans_beval_cb(
2462 BalloonEval *beval,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002463 int state UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464{
Bram Moolenaard62bec82005-03-07 22:56:57 +00002465 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 char_u *text;
Bram Moolenaard62bec82005-03-07 22:56:57 +00002467 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 int col;
Bram Moolenaard9462e32011-04-11 21:35:11 +02002469 char *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 char_u *p;
2471
2472 /* Don't do anything when 'ballooneval' is off, messages scrolled the
2473 * windows up or we have no connection. */
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01002474 if (!can_use_beval() || !NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 return;
2476
Bram Moolenaard62bec82005-03-07 22:56:57 +00002477 if (get_beval_info(beval, TRUE, &wp, &lnum, &text, &col) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 {
2479 /* Send debugger request. Only when the text is of reasonable
2480 * length. */
2481 if (text != NULL && text[0] != NUL && STRLEN(text) < MAXPATHL)
2482 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02002483 buf = (char *)alloc(MAXPATHL * 2 + 25);
2484 if (buf != NULL)
Bram Moolenaar009b2592004-10-24 19:18:58 +00002485 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02002486 p = nb_quote(text);
2487 if (p != NULL)
2488 {
2489 vim_snprintf(buf, MAXPATHL * 2 + 25,
2490 "0:balloonText=%d \"%s\"\n", r_cmdno, p);
2491 vim_free(p);
2492 }
2493 nbdebug(("EVT: %s", buf));
2494 nb_send(buf, "netbeans_beval_cb");
2495 vim_free(buf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 }
2498 vim_free(text);
2499 }
2500}
2501#endif
2502
2503/*
Bram Moolenaare0874f82016-01-24 20:36:41 +01002504 * Return TRUE when the netbeans connection is active.
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002505 */
2506 int
2507netbeans_active(void)
2508{
2509 return NETBEANS_OPEN;
2510}
2511
Bram Moolenaar67c53842010-05-22 18:28:27 +02002512/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 * Tell netbeans that the window was opened, ready for commands.
2514 */
2515 void
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02002516netbeans_open(char *params, int doabort)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517{
2518 char *cmd = "0:startupDone=0\n";
2519
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002520 if (NETBEANS_OPEN)
2521 {
2522 EMSG(_("E511: netbeans already connected"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02002526 if (netbeans_connect(params, doabort) != OK)
Bram Moolenaar67c53842010-05-22 18:28:27 +02002527 return;
Bram Moolenaard62bec82005-03-07 22:56:57 +00002528
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 nbdebug(("EVT: %s", cmd));
2530 nb_send(cmd, "netbeans_startup_done");
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002531
2532 /* update the screen after having added the gutter */
2533 changed_window_setting();
2534 update_screen(CLEAR);
2535 setcursor();
Bram Moolenaar96bcc5e2011-04-01 15:33:59 +02002536 cursor_on();
Bram Moolenaara338adc2018-01-31 20:51:47 +01002537 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538}
2539
Bram Moolenaar009b2592004-10-24 19:18:58 +00002540/*
2541 * Tell netbeans that we're exiting. This should be called right
2542 * before calling exit.
2543 */
2544 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002545netbeans_send_disconnect(void)
Bram Moolenaar009b2592004-10-24 19:18:58 +00002546{
2547 char buf[128];
2548
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002549 if (NETBEANS_OPEN)
Bram Moolenaar009b2592004-10-24 19:18:58 +00002550 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002551 sprintf(buf, "0:disconnect=%d\n", r_cmdno);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002552 nbdebug(("EVT: %s", buf));
2553 nb_send(buf, "netbeans_disconnect");
2554 }
2555}
2556
Bram Moolenaar3266c852016-04-30 18:07:05 +02002557#if defined(FEAT_EVAL) || defined(PROTO)
2558 int
2559set_ref_in_nb_channel(int copyID)
2560{
2561 int abort = FALSE;
2562 typval_T tv;
2563
2564 if (nb_channel != NULL)
2565 {
2566 tv.v_type = VAR_CHANNEL;
2567 tv.vval.v_channel = nb_channel;
2568 abort = set_ref_in_item(&tv, copyID, NULL, NULL);
2569 }
2570 return abort;
2571}
2572#endif
2573
Bram Moolenaar173c9852010-09-29 17:27:01 +02002574#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_W32) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575/*
2576 * Tell netbeans that the window was moved or resized.
2577 */
2578 void
2579netbeans_frame_moved(int new_x, int new_y)
2580{
2581 char buf[128];
2582
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002583 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 return;
2585
2586 sprintf(buf, "0:geometry=%d %d %d %d %d\n",
Bram Moolenaar89d40322006-08-29 15:30:07 +00002587 r_cmdno, (int)Columns, (int)Rows, new_x, new_y);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002588 /*nbdebug(("EVT: %s", buf)); happens too many times during a move */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 nb_send(buf, "netbeans_frame_moved");
2590}
2591#endif
2592
2593/*
Bram Moolenaar009b2592004-10-24 19:18:58 +00002594 * Tell netbeans the user opened or activated a file.
2595 */
2596 void
2597netbeans_file_activated(buf_T *bufp)
2598{
2599 int bufno = nb_getbufno(bufp);
2600 nbbuf_T *bp = nb_get_buf(bufno);
2601 char buffer[2*MAXPATHL];
2602 char_u *q;
2603
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002604 if (!NETBEANS_OPEN || !bufp->b_netbeans_file || dosetvisible)
Bram Moolenaar009b2592004-10-24 19:18:58 +00002605 return;
2606
2607 q = nb_quote(bufp->b_ffname);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002608 if (q == NULL || bp == NULL)
Bram Moolenaar009b2592004-10-24 19:18:58 +00002609 return;
2610
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002611 vim_snprintf(buffer, sizeof(buffer), "%d:fileOpened=%d \"%s\" %s %s\n",
Bram Moolenaar009b2592004-10-24 19:18:58 +00002612 bufno,
2613 bufno,
2614 (char *)q,
2615 "T", /* open in NetBeans */
2616 "F"); /* modified */
2617
2618 vim_free(q);
2619 nbdebug(("EVT: %s", buffer));
2620
2621 nb_send(buffer, "netbeans_file_opened");
2622}
2623
2624/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 * Tell netbeans the user opened a file.
2626 */
2627 void
Bram Moolenaar009b2592004-10-24 19:18:58 +00002628netbeans_file_opened(buf_T *bufp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629{
Bram Moolenaar009b2592004-10-24 19:18:58 +00002630 int bufno = nb_getbufno(bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 char buffer[2*MAXPATHL];
2632 char_u *q;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002633 nbbuf_T *bp = nb_get_buf(nb_getbufno(bufp));
2634 int bnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002636 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 return;
2638
Bram Moolenaar009b2592004-10-24 19:18:58 +00002639 q = nb_quote(bufp->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 if (q == NULL)
2641 return;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002642 if (bp != NULL)
2643 bnum = bufno;
2644 else
2645 bnum = 0;
2646
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002647 vim_snprintf(buffer, sizeof(buffer), "%d:fileOpened=%d \"%s\" %s %s\n",
Bram Moolenaar009b2592004-10-24 19:18:58 +00002648 bnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 0,
2650 (char *)q,
2651 "T", /* open in NetBeans */
2652 "F"); /* modified */
2653
2654 vim_free(q);
2655 nbdebug(("EVT: %s", buffer));
2656
2657 nb_send(buffer, "netbeans_file_opened");
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002658 if (p_acd && vim_chdirfile(bufp->b_ffname, "auto") == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 shorten_fnames(TRUE);
2660}
2661
2662/*
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00002663 * Tell netbeans that a file was deleted or wiped out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 */
2665 void
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00002666netbeans_file_killed(buf_T *bufp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667{
2668 int bufno = nb_getbufno(bufp);
2669 nbbuf_T *nbbuf = nb_get_buf(bufno);
2670 char buffer[2*MAXPATHL];
2671
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002672 if (!NETBEANS_OPEN || bufno == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 return;
2674
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00002675 nbdebug(("netbeans_file_killed:\n"));
2676 nbdebug((" Killing bufno: %d", bufno));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677
Bram Moolenaar89d40322006-08-29 15:30:07 +00002678 sprintf(buffer, "%d:killed=%d\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679
2680 nbdebug(("EVT: %s", buffer));
2681
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00002682 nb_send(buffer, "netbeans_file_killed");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683
2684 if (nbbuf != NULL)
2685 nbbuf->bufp = NULL;
2686}
2687
2688/*
2689 * Get a pointer to the Netbeans buffer for Vim buffer "bufp".
2690 * Return NULL if there is no such buffer or changes are not to be reported.
2691 * Otherwise store the buffer number in "*bufnop".
2692 */
2693 static nbbuf_T *
2694nb_bufp2nbbuf_fire(buf_T *bufp, int *bufnop)
2695{
2696 int bufno;
2697 nbbuf_T *nbbuf;
2698
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002699 if (!NETBEANS_OPEN || !netbeansFireChanges)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 return NULL; /* changes are not reported at all */
2701
2702 bufno = nb_getbufno(bufp);
2703 if (bufno <= 0)
2704 return NULL; /* file is not known to NetBeans */
2705
2706 nbbuf = nb_get_buf(bufno);
2707 if (nbbuf != NULL && !nbbuf->fireChanges)
2708 return NULL; /* changes in this buffer are not reported */
2709
2710 *bufnop = bufno;
2711 return nbbuf;
2712}
2713
2714/*
2715 * Tell netbeans the user inserted some text.
2716 */
2717 void
2718netbeans_inserted(
2719 buf_T *bufp,
2720 linenr_T linenr,
2721 colnr_T col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 char_u *txt,
2723 int newlen)
2724{
2725 char_u *buf;
2726 int bufno;
2727 nbbuf_T *nbbuf;
2728 pos_T pos;
2729 long off;
2730 char_u *p;
2731 char_u *newtxt;
2732
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002733 if (!NETBEANS_OPEN)
2734 return;
2735
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
2737 if (nbbuf == NULL)
2738 return;
2739
Bram Moolenaar009b2592004-10-24 19:18:58 +00002740 /* Don't mark as modified for initial read */
2741 if (nbbuf->insertDone)
2742 nbbuf->modified = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743
2744 pos.lnum = linenr;
2745 pos.col = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 off = pos2off(bufp, &pos);
2747
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 /* send the "insert" EVT */
2749 newtxt = alloc(newlen + 1);
Bram Moolenaarb6356332005-07-18 21:40:44 +00002750 vim_strncpy(newtxt, txt, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 p = nb_quote(newtxt);
2752 if (p != NULL)
2753 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002754 buf = alloc(128 + 2*newlen);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002755 sprintf((char *)buf, "%d:insert=%d %ld \"%s\"\n",
2756 bufno, r_cmdno, off, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 nbdebug(("EVT: %s", buf));
2758 nb_send((char *)buf, "netbeans_inserted");
Bram Moolenaar009b2592004-10-24 19:18:58 +00002759 vim_free(p);
2760 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 vim_free(newtxt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763}
2764
2765/*
2766 * Tell netbeans some bytes have been removed.
2767 */
2768 void
2769netbeans_removed(
2770 buf_T *bufp,
2771 linenr_T linenr,
2772 colnr_T col,
2773 long len)
2774{
2775 char_u buf[128];
2776 int bufno;
2777 nbbuf_T *nbbuf;
2778 pos_T pos;
2779 long off;
2780
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002781 if (!NETBEANS_OPEN)
2782 return;
2783
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
2785 if (nbbuf == NULL)
2786 return;
2787
2788 if (len < 0)
2789 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002790 nbdebug(("Negative len %ld in netbeans_removed()!\n", len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 return;
2792 }
2793
2794 nbbuf->modified = 1;
2795
2796 pos.lnum = linenr;
2797 pos.col = col;
2798
2799 off = pos2off(bufp, &pos);
2800
Bram Moolenaar89d40322006-08-29 15:30:07 +00002801 sprintf((char *)buf, "%d:remove=%d %ld %ld\n", bufno, r_cmdno, off, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 nbdebug(("EVT: %s", buf));
2803 nb_send((char *)buf, "netbeans_removed");
2804}
2805
2806/*
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002807 * Send netbeans an unmodified command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002810netbeans_unmodified(buf_T *bufp UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811{
Bram Moolenaar09092152010-08-08 16:38:42 +02002812 /* This is a no-op, because NetBeans considers a buffer modified
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 * even when all changes have been undone. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814}
2815
2816/*
Bram Moolenaar9af41842016-09-25 21:45:05 +02002817 * Send a button release event back to netbeans. It's up to netbeans
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 * to decide what to do (if anything) with this event.
2819 */
2820 void
2821netbeans_button_release(int button)
2822{
2823 char buf[128];
2824 int bufno;
2825
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002826 if (!NETBEANS_OPEN)
2827 return;
2828
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 bufno = nb_getbufno(curbuf);
2830
2831 if (bufno >= 0 && curwin != NULL && curwin->w_buffer == curbuf)
2832 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02002833 int col = mouse_col - curwin->w_wincol
Bram Moolenaar67c53842010-05-22 18:28:27 +02002834 - ((curwin->w_p_nu || curwin->w_p_rnu) ? 9 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 long off = pos2off(curbuf, &curwin->w_cursor);
2836
2837 /* sync the cursor position */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002838 sprintf(buf, "%d:newDotAndMark=%d %ld %ld\n", bufno, r_cmdno, off, off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 nbdebug(("EVT: %s", buf));
2840 nb_send(buf, "netbeans_button_release[newDotAndMark]");
2841
Bram Moolenaar89d40322006-08-29 15:30:07 +00002842 sprintf(buf, "%d:buttonRelease=%d %d %ld %d\n", bufno, r_cmdno,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 button, (long)curwin->w_cursor.lnum, col);
2844 nbdebug(("EVT: %s", buf));
2845 nb_send(buf, "netbeans_button_release");
2846 }
2847}
2848
2849
2850/*
Bram Moolenaar143c38c2007-05-10 16:41:10 +00002851 * Send a keypress event back to netbeans. This usually simulates some
Bram Moolenaar009b2592004-10-24 19:18:58 +00002852 * kind of function key press. This function operates on a key code.
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002853 * Return TRUE when the key was sent, FALSE when the command has been
2854 * postponed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 */
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002856 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857netbeans_keycommand(int key)
2858{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 char keyName[60];
Bram Moolenaar009b2592004-10-24 19:18:58 +00002860
2861 netbeans_keyname(key, keyName);
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002862 return netbeans_keystring((char_u *)keyName);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002863}
2864
2865
2866/*
Bram Moolenaar143c38c2007-05-10 16:41:10 +00002867 * Send a keypress event back to netbeans. This usually simulates some
Bram Moolenaar009b2592004-10-24 19:18:58 +00002868 * kind of function key press. This function operates on a key string.
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002869 * Return TRUE when the key was sent, FALSE when the command has been
2870 * postponed.
Bram Moolenaar009b2592004-10-24 19:18:58 +00002871 */
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002872 static int
2873netbeans_keystring(char_u *keyName)
Bram Moolenaar009b2592004-10-24 19:18:58 +00002874{
2875 char buf[2*MAXPATHL];
2876 int bufno = nb_getbufno(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 long off;
2878 char_u *q;
2879
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002880 if (!NETBEANS_OPEN)
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002881 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 if (bufno == -1)
2884 {
2885 nbdebug(("got keycommand for non-NetBeans buffer, opening...\n"));
2886 q = curbuf->b_ffname == NULL ? (char_u *)""
2887 : nb_quote(curbuf->b_ffname);
2888 if (q == NULL)
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002889 return TRUE;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002890 vim_snprintf(buf, sizeof(buf), "0:fileOpened=%d \"%s\" %s %s\n", 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 q,
2892 "T", /* open in NetBeans */
2893 "F"); /* modified */
2894 if (curbuf->b_ffname != NULL)
2895 vim_free(q);
2896 nbdebug(("EVT: %s", buf));
2897 nb_send(buf, "netbeans_keycommand");
2898
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002899 postpone_keycommand(keyName);
2900 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 }
2902
2903 /* sync the cursor position */
2904 off = pos2off(curbuf, &curwin->w_cursor);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002905 sprintf(buf, "%d:newDotAndMark=%d %ld %ld\n", bufno, r_cmdno, off, off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 nbdebug(("EVT: %s", buf));
2907 nb_send(buf, "netbeans_keycommand");
2908
2909 /* To work on Win32 you must apply patch to ExtEditor module
2910 * from ExtEdCaret.java.diff - make EVT_newDotAndMark handler
2911 * more synchronous
2912 */
2913
2914 /* now send keyCommand event */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002915 vim_snprintf(buf, sizeof(buf), "%d:keyCommand=%d \"%s\"\n",
Bram Moolenaar89d40322006-08-29 15:30:07 +00002916 bufno, r_cmdno, keyName);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 nbdebug(("EVT: %s", buf));
2918 nb_send(buf, "netbeans_keycommand");
2919
2920 /* New: do both at once and include the lnum/col. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002921 vim_snprintf(buf, sizeof(buf), "%d:keyAtPos=%d \"%s\" %ld %ld/%ld\n",
Bram Moolenaar89d40322006-08-29 15:30:07 +00002922 bufno, r_cmdno, keyName,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 off, (long)curwin->w_cursor.lnum, (long)curwin->w_cursor.col);
2924 nbdebug(("EVT: %s", buf));
2925 nb_send(buf, "netbeans_keycommand");
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002926 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927}
2928
2929
2930/*
2931 * Send a save event to netbeans.
2932 */
2933 void
2934netbeans_save_buffer(buf_T *bufp)
2935{
2936 char_u buf[64];
2937 int bufno;
2938 nbbuf_T *nbbuf;
2939
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002940 if (!NETBEANS_OPEN)
2941 return;
2942
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
2944 if (nbbuf == NULL)
2945 return;
2946
2947 nbbuf->modified = 0;
2948
Bram Moolenaar89d40322006-08-29 15:30:07 +00002949 sprintf((char *)buf, "%d:save=%d\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 nbdebug(("EVT: %s", buf));
2951 nb_send((char *)buf, "netbeans_save_buffer");
2952}
2953
2954
2955/*
2956 * Send remove command to netbeans (this command has been turned off).
2957 */
2958 void
2959netbeans_deleted_all_lines(buf_T *bufp)
2960{
2961 char_u buf[64];
2962 int bufno;
2963 nbbuf_T *nbbuf;
2964
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002965 if (!NETBEANS_OPEN)
2966 return;
2967
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
2969 if (nbbuf == NULL)
2970 return;
2971
Bram Moolenaar009b2592004-10-24 19:18:58 +00002972 /* Don't mark as modified for initial read */
2973 if (nbbuf->insertDone)
2974 nbbuf->modified = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975
Bram Moolenaar89d40322006-08-29 15:30:07 +00002976 sprintf((char *)buf, "%d:remove=%d 0 -1\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 nbdebug(("EVT(suppressed): %s", buf));
2978/* nb_send(buf, "netbeans_deleted_all_lines"); */
2979}
2980
2981
2982/*
2983 * See if the lines are guarded. The top and bot parameters are from
2984 * u_savecommon(), these are the line above the change and the line below the
2985 * change.
2986 */
2987 int
2988netbeans_is_guarded(linenr_T top, linenr_T bot)
2989{
2990 signlist_T *p;
2991 int lnum;
2992
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002993 if (!NETBEANS_OPEN)
2994 return FALSE;
2995
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 for (p = curbuf->b_signlist; p != NULL; p = p->next)
2997 if (p->id >= GUARDEDOFFSET)
2998 for (lnum = top + 1; lnum < bot; lnum++)
2999 if (lnum == p->lnum)
3000 return TRUE;
3001
3002 return FALSE;
3003}
3004
Bram Moolenaar173c9852010-09-29 17:27:01 +02003005#if defined(FEAT_GUI_X11) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006/*
3007 * We have multiple signs to draw at the same location. Draw the
3008 * multi-sign indicator instead. This is the Motif version.
3009 */
3010 void
3011netbeans_draw_multisign_indicator(int row)
3012{
3013 int i;
3014 int y;
3015 int x;
3016
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003017 if (!NETBEANS_OPEN)
3018 return;
3019
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 x = 0;
3021 y = row * gui.char_height + 2;
3022
3023 for (i = 0; i < gui.char_height - 3; i++)
3024 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y++);
3025
3026 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+0, y);
3027 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3028 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+4, y++);
3029 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+1, y);
3030 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3031 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+3, y++);
3032 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3033}
Bram Moolenaar173c9852010-09-29 17:27:01 +02003034#endif /* FEAT_GUI_X11 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035
Bram Moolenaar64404472010-06-26 06:24:45 +02003036#if defined(FEAT_GUI_GTK) && !defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037/*
3038 * We have multiple signs to draw at the same location. Draw the
3039 * multi-sign indicator instead. This is the GTK/Gnome version.
3040 */
3041 void
3042netbeans_draw_multisign_indicator(int row)
3043{
3044 int i;
3045 int y;
3046 int x;
Bram Moolenaar98921892016-02-23 17:14:37 +01003047#if GTK_CHECK_VERSION(3,0,0)
3048 cairo_t *cr = NULL;
3049#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 GdkDrawable *drawable = gui.drawarea->window;
Bram Moolenaar98921892016-02-23 17:14:37 +01003051#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003053 if (!NETBEANS_OPEN)
3054 return;
3055
Bram Moolenaar98921892016-02-23 17:14:37 +01003056#if GTK_CHECK_VERSION(3,0,0)
3057 cr = cairo_create(gui.surface);
Bram Moolenaar36edf062016-07-21 22:10:12 +02003058 cairo_set_source_rgba(cr,
3059 gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue,
3060 gui.fgcolor->alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01003061#endif
3062
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 x = 0;
3064 y = row * gui.char_height + 2;
3065
3066 for (i = 0; i < gui.char_height - 3; i++)
Bram Moolenaar98921892016-02-23 17:14:37 +01003067#if GTK_CHECK_VERSION(3,0,0)
3068 cairo_rectangle(cr, x+2, y++, 1, 1);
3069#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 gdk_draw_point(drawable, gui.text_gc, x+2, y++);
Bram Moolenaar98921892016-02-23 17:14:37 +01003071#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072
Bram Moolenaar98921892016-02-23 17:14:37 +01003073#if GTK_CHECK_VERSION(3,0,0)
3074 cairo_rectangle(cr, x+0, y, 1, 1);
3075 cairo_rectangle(cr, x+2, y, 1, 1);
3076 cairo_rectangle(cr, x+4, y++, 1, 1);
3077 cairo_rectangle(cr, x+1, y, 1, 1);
3078 cairo_rectangle(cr, x+2, y, 1, 1);
3079 cairo_rectangle(cr, x+3, y++, 1, 1);
3080 cairo_rectangle(cr, x+2, y, 1, 1);
3081#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 gdk_draw_point(drawable, gui.text_gc, x+0, y);
3083 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3084 gdk_draw_point(drawable, gui.text_gc, x+4, y++);
3085 gdk_draw_point(drawable, gui.text_gc, x+1, y);
3086 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3087 gdk_draw_point(drawable, gui.text_gc, x+3, y++);
3088 gdk_draw_point(drawable, gui.text_gc, x+2, y);
Bram Moolenaar98921892016-02-23 17:14:37 +01003089#endif
3090
3091#if GTK_CHECK_VERSION(3,0,0)
3092 cairo_destroy(cr);
3093#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094}
3095#endif /* FEAT_GUI_GTK */
3096
3097/*
3098 * If the mouse is clicked in the gutter of a line with multiple
3099 * annotations, cycle through the set of signs.
3100 */
3101 void
3102netbeans_gutter_click(linenr_T lnum)
3103{
3104 signlist_T *p;
3105
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003106 if (!NETBEANS_OPEN)
3107 return;
3108
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 for (p = curbuf->b_signlist; p != NULL; p = p->next)
3110 {
3111 if (p->lnum == lnum && p->next && p->next->lnum == lnum)
3112 {
3113 signlist_T *tail;
3114
3115 /* remove "p" from list, reinsert it at the tail of the sublist */
3116 if (p->prev)
3117 p->prev->next = p->next;
3118 else
3119 curbuf->b_signlist = p->next;
3120 p->next->prev = p->prev;
3121 /* now find end of sublist and insert p */
3122 for (tail = p->next;
3123 tail->next && tail->next->lnum == lnum
3124 && tail->next->id < GUARDEDOFFSET;
3125 tail = tail->next)
3126 ;
3127 /* tail now points to last entry with same lnum (except
3128 * that "guarded" annotations are always last) */
3129 p->next = tail->next;
3130 if (tail->next)
3131 tail->next->prev = p;
3132 p->prev = tail;
3133 tail->next = p;
3134 update_debug_sign(curbuf, lnum);
3135 break;
3136 }
3137 }
3138}
3139
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140/*
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003141 * Add a sign of the requested type at the requested location.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 *
3143 * Reverse engineering:
3144 * Apparently an annotation is defined the first time it is used in a buffer.
3145 * When the same annotation is used in two buffers, the second time we do not
3146 * need to define a new sign name but reuse the existing one. But since the
3147 * ID number used in the second buffer starts counting at one again, a mapping
3148 * is made from the ID specifically for the buffer to the global sign name
3149 * (which is a number).
3150 *
3151 * globalsignmap[] stores the signs that have been defined globally.
3152 * buf->signmapused[] maps buffer-local annotation IDs to an index in
3153 * globalsignmap[].
3154 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 static void
3156addsigntype(
3157 nbbuf_T *buf,
3158 int typeNum,
3159 char_u *typeName,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003160 char_u *tooltip UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 char_u *glyphFile,
Bram Moolenaar67c53842010-05-22 18:28:27 +02003162 char_u *fg,
3163 char_u *bg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 int i, j;
Bram Moolenaar67c53842010-05-22 18:28:27 +02003166 int use_fg = (*fg && STRCMP(fg, "none") != 0);
3167 int use_bg = (*bg && STRCMP(bg, "none") != 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168
3169 for (i = 0; i < globalsignmapused; i++)
3170 if (STRCMP(typeName, globalsignmap[i]) == 0)
3171 break;
3172
3173 if (i == globalsignmapused) /* not found; add it to global map */
3174 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003175 nbdebug(("DEFINEANNOTYPE(%d,%s,%s,%s,%s,%s)\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 typeNum, typeName, tooltip, glyphFile, fg, bg));
3177 if (use_fg || use_bg)
3178 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003179 char fgbuf[2 * (8 + MAX_COLOR_LENGTH) + 1];
3180 char bgbuf[2 * (8 + MAX_COLOR_LENGTH) + 1];
3181 char *ptr;
3182 int value;
3183
3184 value = strtol((char *)fg, &ptr, 10);
3185 if (ptr != (char *)fg)
3186 sprintf(fgbuf, "guifg=#%06x", value & 0xFFFFFF);
3187 else
3188 sprintf(fgbuf, "guifg=%s ctermfg=%s", fg, fg);
3189
3190 value = strtol((char *)bg, &ptr, 10);
3191 if (ptr != (char *)bg)
3192 sprintf(bgbuf, "guibg=#%06x", value & 0xFFFFFF);
3193 else
3194 sprintf(bgbuf, "guibg=%s ctermbg=%s", bg, bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195
3196 coloncmd(":highlight NB_%s %s %s", typeName, (use_fg) ? fgbuf : "",
3197 (use_bg) ? bgbuf : "");
3198 if (*glyphFile == NUL)
3199 /* no glyph, line highlighting only */
3200 coloncmd(":sign define %d linehl=NB_%s", i + 1, typeName);
3201 else if (vim_strsize(glyphFile) <= 2)
3202 /* one- or two-character glyph name, use as text glyph with
3203 * texthl */
3204 coloncmd(":sign define %d text=%s texthl=NB_%s", i + 1,
3205 glyphFile, typeName);
3206 else
3207 /* glyph, line highlighting */
3208 coloncmd(":sign define %d icon=%s linehl=NB_%s", i + 1,
3209 glyphFile, typeName);
3210 }
3211 else
3212 /* glyph, no line highlighting */
3213 coloncmd(":sign define %d icon=%s", i + 1, glyphFile);
3214
3215 if (STRCMP(typeName,"CurrentPC") == 0)
3216 curPCtype = typeNum;
3217
3218 if (globalsignmapused == globalsignmaplen)
3219 {
3220 if (globalsignmaplen == 0) /* first allocation */
3221 {
3222 globalsignmaplen = 20;
3223 globalsignmap = (char **)alloc_clear(globalsignmaplen*sizeof(char *));
3224 }
3225 else /* grow it */
3226 {
3227 int incr;
3228 int oldlen = globalsignmaplen;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003229 char **t_globalsignmap = globalsignmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230
3231 globalsignmaplen *= 2;
3232 incr = globalsignmaplen - oldlen;
3233 globalsignmap = (char **)vim_realloc(globalsignmap,
3234 globalsignmaplen * sizeof(char *));
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003235 if (globalsignmap == NULL)
3236 {
3237 vim_free(t_globalsignmap);
3238 globalsignmaplen = 0;
3239 return;
3240 }
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003241 vim_memset(globalsignmap + oldlen, 0, incr * sizeof(char *));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 }
3243 }
3244
3245 globalsignmap[i] = (char *)typeName;
3246 globalsignmapused = i + 1;
3247 }
3248
3249 /* check local map; should *not* be found! */
3250 for (j = 0; j < buf->signmapused; j++)
3251 if (buf->signmap[j] == i + 1)
3252 return;
3253
3254 /* add to local map */
3255 if (buf->signmapused == buf->signmaplen)
3256 {
3257 if (buf->signmaplen == 0) /* first allocation */
3258 {
3259 buf->signmaplen = 5;
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003260 buf->signmap = (int *)alloc_clear(buf->signmaplen * sizeof(int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 }
3262 else /* grow it */
3263 {
3264 int incr;
3265 int oldlen = buf->signmaplen;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003266 int *t_signmap = buf->signmap;
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003267
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 buf->signmaplen *= 2;
3269 incr = buf->signmaplen - oldlen;
3270 buf->signmap = (int *)vim_realloc(buf->signmap,
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003271 buf->signmaplen * sizeof(int));
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003272 if (buf->signmap == NULL)
3273 {
3274 vim_free(t_signmap);
3275 buf->signmaplen = 0;
3276 return;
3277 }
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003278 vim_memset(buf->signmap + oldlen, 0, incr * sizeof(int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 }
3280 }
3281
3282 buf->signmap[buf->signmapused++] = i + 1;
3283
3284}
3285
3286
3287/*
3288 * See if we have the requested sign type in the buffer.
3289 */
3290 static int
3291mapsigntype(nbbuf_T *buf, int localsigntype)
3292{
3293 if (--localsigntype >= 0 && localsigntype < buf->signmapused)
3294 return buf->signmap[localsigntype];
3295
3296 return 0;
3297}
3298
3299
3300/*
3301 * Compute length of buffer, don't print anything.
3302 */
3303 static long
3304get_buf_size(buf_T *bufp)
3305{
3306 linenr_T lnum;
3307 long char_count = 0;
3308 int eol_size;
3309 long last_check = 100000L;
3310
3311 if (bufp->b_ml.ml_flags & ML_EMPTY)
3312 return 0;
3313 else
3314 {
3315 if (get_fileformat(bufp) == EOL_DOS)
3316 eol_size = 2;
3317 else
3318 eol_size = 1;
3319 for (lnum = 1; lnum <= bufp->b_ml.ml_line_count; ++lnum)
3320 {
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00003321 char_count += (long)STRLEN(ml_get_buf(bufp, lnum, FALSE))
3322 + eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 /* Check for a CTRL-C every 100000 characters */
3324 if (char_count > last_check)
3325 {
3326 ui_breakcheck();
3327 if (got_int)
3328 return char_count;
3329 last_check = char_count + 100000L;
3330 }
3331 }
3332 /* Correction for when last line doesn't have an EOL. */
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003333 if (!bufp->b_p_eol && (bufp->b_p_bin || !bufp->b_p_fixeol))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 char_count -= eol_size;
3335 }
3336
3337 return char_count;
3338}
3339
3340/*
3341 * Convert character offset to lnum,col
3342 */
3343 static pos_T *
3344off2pos(buf_T *buf, long offset)
3345{
3346 linenr_T lnum;
3347 static pos_T pos;
3348
3349 pos.lnum = 0;
3350 pos.col = 0;
3351#ifdef FEAT_VIRTUALEDIT
3352 pos.coladd = 0;
3353#endif
3354
3355 if (!(buf->b_ml.ml_flags & ML_EMPTY))
3356 {
3357 if ((lnum = ml_find_line_or_offset(buf, (linenr_T)0, &offset)) < 0)
3358 return NULL;
3359 pos.lnum = lnum;
3360 pos.col = offset;
3361 }
3362
3363 return &pos;
3364}
3365
3366/*
3367 * Convert an argument in the form "1234" to an offset and compute the
3368 * lnum/col from it. Convert an argument in the form "123/12" directly to a
3369 * lnum/col.
3370 * "argp" is advanced to after the argument.
3371 * Return a pointer to the position, NULL if something is wrong.
3372 */
3373 static pos_T *
3374get_off_or_lnum(buf_T *buf, char_u **argp)
3375{
3376 static pos_T mypos;
3377 long off;
3378
3379 off = strtol((char *)*argp, (char **)argp, 10);
3380 if (**argp == '/')
3381 {
3382 mypos.lnum = (linenr_T)off;
3383 ++*argp;
3384 mypos.col = strtol((char *)*argp, (char **)argp, 10);
3385#ifdef FEAT_VIRTUALEDIT
3386 mypos.coladd = 0;
3387#endif
3388 return &mypos;
3389 }
3390 return off2pos(buf, off);
3391}
3392
3393
3394/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003395 * Convert (lnum,col) to byte offset in the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 */
3397 static long
3398pos2off(buf_T *buf, pos_T *pos)
3399{
3400 long offset = 0;
3401
3402 if (!(buf->b_ml.ml_flags & ML_EMPTY))
3403 {
3404 if ((offset = ml_find_line_or_offset(buf, pos->lnum, 0)) < 0)
3405 return 0;
3406 offset += pos->col;
3407 }
3408
3409 return offset;
3410}
3411
3412
Bram Moolenaar009b2592004-10-24 19:18:58 +00003413/*
Bram Moolenaar9af41842016-09-25 21:45:05 +02003414 * This message is printed after NetBeans opens a new file. It's
Bram Moolenaar009b2592004-10-24 19:18:58 +00003415 * similar to the message readfile() uses, but since NetBeans
3416 * doesn't normally call readfile, we do our own.
3417 */
3418 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003419print_read_msg(nbbuf_T *buf)
Bram Moolenaar009b2592004-10-24 19:18:58 +00003420{
3421 int lnum = buf->bufp->b_ml.ml_line_count;
Bram Moolenaar8767f522016-07-01 17:17:39 +02003422 off_T nchars = buf->bufp->b_orig_size;
Bram Moolenaar009b2592004-10-24 19:18:58 +00003423 char_u c;
3424
3425 msg_add_fname(buf->bufp, buf->bufp->b_ffname);
3426 c = FALSE;
3427
3428 if (buf->bufp->b_p_ro)
3429 {
3430 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]"));
3431 c = TRUE;
3432 }
3433 if (!buf->bufp->b_start_eol)
3434 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003435 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]")
3436 : _("[Incomplete last line]"));
Bram Moolenaar009b2592004-10-24 19:18:58 +00003437 c = TRUE;
3438 }
3439 msg_add_lines(c, (long)lnum, nchars);
3440
3441 /* Now display it */
Bram Moolenaard23a8232018-02-10 18:45:26 +01003442 VIM_CLEAR(keep_msg);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003443 msg_scrolled_ign = TRUE;
3444 msg_trunc_attr(IObuff, FALSE, 0);
3445 msg_scrolled_ign = FALSE;
3446}
3447
3448
3449/*
Bram Moolenaar67c53842010-05-22 18:28:27 +02003450 * Print a message after NetBeans writes the file. This message should be
3451 * identical to the standard message a non-netbeans user would see when
3452 * writing a file.
Bram Moolenaar009b2592004-10-24 19:18:58 +00003453 */
3454 static void
Bram Moolenaar8767f522016-07-01 17:17:39 +02003455print_save_msg(nbbuf_T *buf, off_T nchars)
Bram Moolenaar009b2592004-10-24 19:18:58 +00003456{
3457 char_u c;
3458 char_u *p;
3459
3460 if (nchars >= 0)
3461 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003462 /* put fname in IObuff with quotes */
3463 msg_add_fname(buf->bufp, buf->bufp->b_ffname);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003464 c = FALSE;
3465
3466 msg_add_lines(c, buf->bufp->b_ml.ml_line_count,
Bram Moolenaar914703b2010-05-31 21:59:46 +02003467 buf->bufp->b_orig_size);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003468
Bram Moolenaard23a8232018-02-10 18:45:26 +01003469 VIM_CLEAR(keep_msg);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003470 msg_scrolled_ign = TRUE;
3471 p = msg_trunc_attr(IObuff, FALSE, 0);
3472 if ((msg_scrolled && !need_wait_return) || !buf->initDone)
3473 {
3474 /* Need to repeat the message after redrawing when:
3475 * - When reading from stdin (the screen will be cleared next).
3476 * - When restart_edit is set (otherwise there will be a delay
3477 * before redrawing).
3478 * - When the screen was scrolled but there is no wait-return
3479 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003480 set_keep_msg(p, 0);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003481 }
3482 msg_scrolled_ign = FALSE;
3483 /* add_to_input_buf((char_u *)"\f", 1); */
3484 }
3485 else
3486 {
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003487 char_u msgbuf[IOSIZE];
Bram Moolenaar009b2592004-10-24 19:18:58 +00003488
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003489 vim_snprintf((char *)msgbuf, IOSIZE,
3490 _("E505: %s is read-only (add ! to override)"), IObuff);
3491 nbdebug((" %s\n", msgbuf));
3492 emsg(msgbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003493 }
3494}
3495
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496#endif /* defined(FEAT_NETBEANS_INTG) */