blob: 199cfd63cd817036f7b6089167f731a2e917cc4e [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 }
583 vim_free(buf_list);
584 buf_list = NULL;
585 buf_list_size = 0;
586 buf_list_used = 0;
587
588 /* free the queued key commands */
Bram Moolenaar8d4eecc2012-11-20 17:19:01 +0100589 while (key_node != NULL && key_node != &keyHead)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200590 {
591 keyQ_T *next = key_node->next;
592 vim_free(key_node->keystr);
593 vim_free(key_node);
594 if (next == &keyHead)
595 {
596 keyHead.next = &keyHead;
597 keyHead.prev = &keyHead;
598 break;
599 }
600 key_node = next;
601 }
602
603 /* free the queued netbeans commands */
Bram Moolenaar77073442016-02-13 23:23:53 +0100604 if (nb_channel != NULL)
605 channel_clear(nb_channel);
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200606}
607
608/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 * Get the Netbeans buffer number for the specified buffer.
610 */
611 static int
612nb_getbufno(buf_T *bufp)
613{
614 int i;
615
616 for (i = 0; i < buf_list_used; i++)
617 if (buf_list[i].bufp == bufp)
618 return i;
619 return -1;
620}
621
622/*
623 * Is this a NetBeans-owned buffer?
624 */
625 int
626isNetbeansBuffer(buf_T *bufp)
627{
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200628 return NETBEANS_OPEN && bufp->b_netbeans_file;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629}
630
631/*
632 * NetBeans and Vim have different undo models. In Vim, the file isn't
633 * changed if changes are undone via the undo command. In NetBeans, once
634 * a change has been made the file is marked as modified until saved. It
635 * doesn't matter if the change was undone.
636 *
637 * So this function is for the corner case where Vim thinks a buffer is
638 * unmodified but NetBeans thinks it IS modified.
639 */
640 int
641isNetbeansModified(buf_T *bufp)
642{
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200643 if (isNetbeansBuffer(bufp))
Bram Moolenaar009b2592004-10-24 19:18:58 +0000644 {
645 int bufno = nb_getbufno(bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646
Bram Moolenaar009b2592004-10-24 19:18:58 +0000647 if (bufno > 0)
648 return buf_list[bufno].modified;
649 else
650 return FALSE;
651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 else
653 return FALSE;
654}
655
656/*
657 * Given a Netbeans buffer number, return the netbeans buffer.
658 * Returns NULL for 0 or a negative number. A 0 bufno means a
659 * non-buffer related command has been sent.
660 */
661 static nbbuf_T *
662nb_get_buf(int bufno)
663{
664 /* find or create a buffer with the given number */
665 int incr;
666
667 if (bufno <= 0)
668 return NULL;
669
670 if (!buf_list)
671 {
672 /* initialize */
673 buf_list = (nbbuf_T *)alloc_clear(100 * sizeof(nbbuf_T));
674 buf_list_size = 100;
675 }
676 if (bufno >= buf_list_used) /* new */
677 {
678 if (bufno >= buf_list_size) /* grow list */
679 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +0100680 nbbuf_T *t_buf_list = buf_list;
681
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 incr = bufno - buf_list_size + 90;
683 buf_list_size += incr;
684 buf_list = (nbbuf_T *)vim_realloc(
685 buf_list, buf_list_size * sizeof(nbbuf_T));
Bram Moolenaar9abd5c62015-02-10 18:34:01 +0100686 if (buf_list == NULL)
687 {
688 vim_free(t_buf_list);
689 buf_list_size = 0;
690 return NULL;
691 }
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200692 vim_memset(buf_list + buf_list_size - incr, 0,
693 incr * sizeof(nbbuf_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 }
695
696 while (buf_list_used <= bufno)
697 {
698 /* Default is to fire text changes. */
699 buf_list[buf_list_used].fireChanges = 1;
700 ++buf_list_used;
701 }
702 }
703
704 return buf_list + bufno;
705}
706
707/*
708 * Return the number of buffers that are modified.
709 */
710 static int
711count_changed_buffers(void)
712{
713 buf_T *bufp;
714 int n;
715
716 n = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +0200717 FOR_ALL_BUFFERS(bufp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 if (bufp->b_changed)
719 ++n;
720 return n;
721}
722
723/*
724 * End the netbeans session.
725 */
726 void
727netbeans_end(void)
728{
729 int i;
730 static char buf[128];
731
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200732 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 return;
734
735 for (i = 0; i < buf_list_used; i++)
736 {
737 if (!buf_list[i].bufp)
738 continue;
739 if (netbeansForcedQuit)
740 {
741 /* mark as unmodified so NetBeans won't put up dialog on "killed" */
Bram Moolenaar89d40322006-08-29 15:30:07 +0000742 sprintf(buf, "%d:unmodified=%d\n", i, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 nbdebug(("EVT: %s", buf));
744 nb_send(buf, "netbeans_end");
745 }
Bram Moolenaar89d40322006-08-29 15:30:07 +0000746 sprintf(buf, "%d:killed=%d\n", i, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 nbdebug(("EVT: %s", buf));
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200748 /* nb_send(buf, "netbeans_end"); avoid "write failed" messages */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100749 nb_send(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751}
752
753/*
754 * Send a message to netbeans.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100755 * When "fun" is NULL no error is given.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 */
757 static void
758nb_send(char *buf, char *fun)
759{
Bram Moolenaar77073442016-02-13 23:23:53 +0100760 if (nb_channel != NULL)
Bram Moolenaarbf2cc5f2016-07-07 20:45:06 +0200761 channel_send(nb_channel, PART_SOCK, (char_u *)buf,
762 (int)STRLEN(buf), fun);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763}
764
765/*
766 * Some input received from netbeans requires a response. This function
767 * handles a response with no information (except the command number).
768 */
769 static void
770nb_reply_nil(int cmdno)
771{
772 char reply[32];
773
Bram Moolenaar009b2592004-10-24 19:18:58 +0000774 nbdebug(("REP %d: <none>\n", cmdno));
775
Bram Moolenaar7ad7d012010-11-16 15:49:02 +0100776 /* Avoid printing an annoying error message. */
777 if (!NETBEANS_OPEN)
778 return;
779
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 sprintf(reply, "%d\n", cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 nb_send(reply, "nb_reply_nil");
782}
783
784
785/*
786 * Send a response with text.
787 * "result" must have been quoted already (using nb_quote()).
788 */
789 static void
790nb_reply_text(int cmdno, char_u *result)
791{
792 char_u *reply;
793
Bram Moolenaar009b2592004-10-24 19:18:58 +0000794 nbdebug(("REP %d: %s\n", cmdno, (char *)result));
795
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000796 reply = alloc((unsigned)STRLEN(result) + 32);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 sprintf((char *)reply, "%d %s\n", cmdno, (char *)result);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 nb_send((char *)reply, "nb_reply_text");
799
800 vim_free(reply);
801}
802
803
804/*
805 * Send a response with a number result code.
806 */
807 static void
808nb_reply_nr(int cmdno, long result)
809{
810 char reply[32];
811
Bram Moolenaar009b2592004-10-24 19:18:58 +0000812 nbdebug(("REP %d: %ld\n", cmdno, result));
813
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 sprintf(reply, "%d %ld\n", cmdno, result);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 nb_send(reply, "nb_reply_nr");
816}
817
818
819/*
820 * Encode newline, ret, backslash, double quote for transmission to NetBeans.
821 */
822 static char_u *
823nb_quote(char_u *txt)
824{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000825 char_u *buf = alloc((unsigned)(2 * STRLEN(txt) + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 char_u *p = txt;
827 char_u *q = buf;
828
829 if (buf == NULL)
830 return NULL;
831 for (; *p; p++)
832 {
833 switch (*p)
834 {
835 case '\"':
836 case '\\':
837 *q++ = '\\'; *q++ = *p; break;
838 /* case '\t': */
839 /* *q++ = '\\'; *q++ = 't'; break; */
840 case '\n':
841 *q++ = '\\'; *q++ = 'n'; break;
842 case '\r':
843 *q++ = '\\'; *q++ = 'r'; break;
844 default:
845 *q++ = *p;
846 break;
847 }
848 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100849 *q = '\0';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850
851 return buf;
852}
853
854
855/*
856 * Remove top level double quotes; convert backslashed chars.
857 * Returns an allocated string (NULL for failure).
858 * If "endp" is not NULL it is set to the character after the terminating
859 * quote.
860 */
861 static char *
862nb_unquote(char_u *p, char_u **endp)
863{
864 char *result = 0;
865 char *q;
866 int done = 0;
867
868 /* result is never longer than input */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000869 result = (char *)alloc_clear((unsigned)STRLEN(p) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 if (result == NULL)
871 return NULL;
872
873 if (*p++ != '"')
874 {
875 nbdebug(("nb_unquote called with string that doesn't start with a quote!: %s\n",
876 p));
877 result[0] = NUL;
878 return result;
879 }
880
881 for (q = result; !done && *p != NUL;)
882 {
883 switch (*p)
884 {
885 case '"':
886 /*
887 * Unbackslashed dquote marks the end, if first char was dquote.
888 */
889 done = 1;
890 break;
891
892 case '\\':
893 ++p;
894 switch (*p)
895 {
896 case '\\': *q++ = '\\'; break;
897 case 'n': *q++ = '\n'; break;
898 case 't': *q++ = '\t'; break;
899 case 'r': *q++ = '\r'; break;
900 case '"': *q++ = '"'; break;
901 case NUL: --p; break;
902 /* default: skip over illegal chars */
903 }
904 ++p;
905 break;
906
907 default:
908 *q++ = *p++;
909 }
910 }
911
912 if (endp != NULL)
913 *endp = p;
914
915 return result;
916}
917
Bram Moolenaar5eaf8722008-01-05 17:07:13 +0000918/*
919 * Remove from "first" byte to "last" byte (inclusive), at line "lnum" of the
920 * current buffer. Remove to end of line when "last" is MAXCOL.
921 */
922 static void
923nb_partialremove(linenr_T lnum, colnr_T first, colnr_T last)
924{
925 char_u *oldtext, *newtext;
926 int oldlen;
927 int lastbyte = last;
928
929 oldtext = ml_get(lnum);
Bram Moolenaarcb4cef22008-03-16 15:04:34 +0000930 oldlen = (int)STRLEN(oldtext);
Bram Moolenaarb3c70982008-01-18 10:40:55 +0000931 if (first >= (colnr_T)oldlen || oldlen == 0) /* just in case */
Bram Moolenaar5eaf8722008-01-05 17:07:13 +0000932 return;
933 if (lastbyte >= oldlen)
934 lastbyte = oldlen - 1;
935 newtext = alloc(oldlen - (int)(lastbyte - first));
936 if (newtext != NULL)
937 {
938 mch_memmove(newtext, oldtext, first);
Bram Moolenaarf2330482008-06-24 20:19:36 +0000939 STRMOVE(newtext + first, oldtext + lastbyte + 1);
Bram Moolenaar5eaf8722008-01-05 17:07:13 +0000940 nbdebug((" NEW LINE %d: %s\n", lnum, newtext));
941 ml_replace(lnum, newtext, FALSE);
942 }
943}
944
945/*
946 * Replace the "first" line with the concatenation of the "first" and
947 * the "other" line. The "other" line is not removed.
948 */
949 static void
950nb_joinlines(linenr_T first, linenr_T other)
951{
952 int len_first, len_other;
953 char_u *p;
954
Bram Moolenaarcb4cef22008-03-16 15:04:34 +0000955 len_first = (int)STRLEN(ml_get(first));
956 len_other = (int)STRLEN(ml_get(other));
Bram Moolenaar5eaf8722008-01-05 17:07:13 +0000957 p = alloc((unsigned)(len_first + len_other + 1));
958 if (p != NULL)
959 {
960 mch_memmove(p, ml_get(first), len_first);
961 mch_memmove(p + len_first, ml_get(other), len_other + 1);
962 ml_replace(first, p, FALSE);
963 }
964}
965
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966#define SKIP_STOP 2
967#define streq(a,b) (strcmp(a,b) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968
969/*
970 * Do the actual processing of a single netbeans command or function.
Bram Moolenaar143c38c2007-05-10 16:41:10 +0000971 * The difference between a command and function is that a function
972 * gets a response (it's required) but a command does not.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 * For arguments see comment for nb_parse_cmd().
974 */
975 static int
976nb_do_cmd(
977 int bufno,
978 char_u *cmd,
979 int func,
980 int cmdno,
981 char_u *args) /* points to space before arguments or NUL */
982{
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100983 int do_update = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 long off = 0;
985 nbbuf_T *buf = nb_get_buf(bufno);
986 static int skip = 0;
987 int retval = OK;
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000988 char *cp; /* for when a char pointer is needed */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989
990 nbdebug(("%s %d: (%d) %s %s\n", (func) ? "FUN" : "CMD", cmdno, bufno, cmd,
991 STRCMP(cmd, "insert") == 0 ? "<text>" : (char *)args));
992
993 if (func)
994 {
995/* =====================================================================*/
996 if (streq((char *)cmd, "getModified"))
997 {
998 if (buf == NULL || buf->bufp == NULL)
999 /* Return the number of buffers that are modified. */
1000 nb_reply_nr(cmdno, (long)count_changed_buffers());
1001 else
1002 /* Return whether the buffer is modified. */
1003 nb_reply_nr(cmdno, (long)(buf->bufp->b_changed
1004 || isNetbeansModified(buf->bufp)));
1005/* =====================================================================*/
1006 }
1007 else if (streq((char *)cmd, "saveAndExit"))
1008 {
1009 /* Note: this will exit Vim if successful. */
1010 coloncmd(":confirm qall");
1011
1012 /* We didn't exit: return the number of changed buffers. */
1013 nb_reply_nr(cmdno, (long)count_changed_buffers());
1014/* =====================================================================*/
1015 }
1016 else if (streq((char *)cmd, "getCursor"))
1017 {
1018 char_u text[200];
1019
1020 /* Note: nb_getbufno() may return -1. This indicates the IDE
1021 * didn't assign a number to the current buffer in response to a
1022 * fileOpened event. */
1023 sprintf((char *)text, "%d %ld %d %ld",
1024 nb_getbufno(curbuf),
1025 (long)curwin->w_cursor.lnum,
1026 (int)curwin->w_cursor.col,
1027 pos2off(curbuf, &curwin->w_cursor));
1028 nb_reply_text(cmdno, text);
1029/* =====================================================================*/
1030 }
Bram Moolenaarc65c4912006-11-14 17:29:46 +00001031 else if (streq((char *)cmd, "getAnno"))
1032 {
1033 long linenum = 0;
1034#ifdef FEAT_SIGNS
1035 if (buf == NULL || buf->bufp == NULL)
1036 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001037 nbdebug((" Invalid buffer identifier in getAnno\n"));
1038 EMSG("E652: Invalid buffer identifier in getAnno");
Bram Moolenaarc65c4912006-11-14 17:29:46 +00001039 retval = FAIL;
1040 }
1041 else
1042 {
1043 int serNum;
1044
1045 cp = (char *)args;
1046 serNum = strtol(cp, &cp, 10);
1047 /* If the sign isn't found linenum will be zero. */
1048 linenum = (long)buf_findsign(buf->bufp, serNum);
1049 }
1050#endif
1051 nb_reply_nr(cmdno, linenum);
1052/* =====================================================================*/
1053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 else if (streq((char *)cmd, "getLength"))
1055 {
1056 long len = 0;
1057
1058 if (buf == NULL || buf->bufp == NULL)
1059 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001060 nbdebug((" invalid buffer identifier in getLength\n"));
1061 EMSG("E632: invalid buffer identifier in getLength");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 retval = FAIL;
1063 }
1064 else
1065 {
1066 len = get_buf_size(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 }
1068 nb_reply_nr(cmdno, len);
1069/* =====================================================================*/
1070 }
1071 else if (streq((char *)cmd, "getText"))
1072 {
1073 long len;
1074 linenr_T nlines;
1075 char_u *text = NULL;
1076 linenr_T lno = 1;
1077 char_u *p;
1078 char_u *line;
1079
1080 if (buf == NULL || buf->bufp == NULL)
1081 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001082 nbdebug((" invalid buffer identifier in getText\n"));
1083 EMSG("E633: invalid buffer identifier in getText");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 retval = FAIL;
1085 }
1086 else
1087 {
1088 len = get_buf_size(buf->bufp);
1089 nlines = buf->bufp->b_ml.ml_line_count;
1090 text = alloc((unsigned)((len > 0)
1091 ? ((len + nlines) * 2) : 4));
1092 if (text == NULL)
1093 {
1094 nbdebug((" nb_do_cmd: getText has null text field\n"));
1095 retval = FAIL;
1096 }
1097 else
1098 {
1099 p = text;
1100 *p++ = '\"';
1101 for (; lno <= nlines ; lno++)
1102 {
1103 line = nb_quote(ml_get_buf(buf->bufp, lno, FALSE));
1104 if (line != NULL)
1105 {
1106 STRCPY(p, line);
1107 p += STRLEN(line);
1108 *p++ = '\\';
1109 *p++ = 'n';
Bram Moolenaar009b2592004-10-24 19:18:58 +00001110 vim_free(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 }
1113 *p++ = '\"';
1114 *p = '\0';
1115 }
1116 }
1117 if (text == NULL)
1118 nb_reply_text(cmdno, (char_u *)"");
1119 else
1120 {
1121 nb_reply_text(cmdno, text);
1122 vim_free(text);
1123 }
1124/* =====================================================================*/
1125 }
1126 else if (streq((char *)cmd, "remove"))
1127 {
1128 long count;
1129 pos_T first, last;
1130 pos_T *pos;
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001131 pos_T *next;
1132 linenr_T del_from_lnum, del_to_lnum; /* lines to be deleted as a whole */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 int oldFire = netbeansFireChanges;
1134 int oldSuppress = netbeansSuppressNoLines;
1135 int wasChanged;
1136
1137 if (skip >= SKIP_STOP)
1138 {
1139 nbdebug((" Skipping %s command\n", (char *) cmd));
1140 nb_reply_nil(cmdno);
1141 return OK;
1142 }
1143
1144 if (buf == NULL || buf->bufp == NULL)
1145 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001146 nbdebug((" invalid buffer identifier in remove\n"));
1147 EMSG("E634: invalid buffer identifier in remove");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 retval = FAIL;
1149 }
1150 else
1151 {
1152 netbeansFireChanges = FALSE;
1153 netbeansSuppressNoLines = TRUE;
1154
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001155 nb_set_curbuf(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 wasChanged = buf->bufp->b_changed;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001157 cp = (char *)args;
1158 off = strtol(cp, &cp, 10);
1159 count = strtol(cp, &cp, 10);
1160 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 /* delete "count" chars, starting at "off" */
1162 pos = off2pos(buf->bufp, off);
1163 if (!pos)
1164 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001165 nbdebug((" !bad position\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 nb_reply_text(cmdno, (char_u *)"!bad position");
1167 netbeansFireChanges = oldFire;
1168 netbeansSuppressNoLines = oldSuppress;
1169 return FAIL;
1170 }
1171 first = *pos;
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001172 nbdebug((" FIRST POS: line %d, col %d\n",
1173 first.lnum, first.col));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 pos = off2pos(buf->bufp, off+count-1);
1175 if (!pos)
1176 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001177 nbdebug((" !bad count\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 nb_reply_text(cmdno, (char_u *)"!bad count");
1179 netbeansFireChanges = oldFire;
1180 netbeansSuppressNoLines = oldSuppress;
1181 return FAIL;
1182 }
1183 last = *pos;
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001184 nbdebug((" LAST POS: line %d, col %d\n",
1185 last.lnum, last.col));
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001186 del_from_lnum = first.lnum;
1187 del_to_lnum = last.lnum;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001188 do_update = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001190 /* Get the position of the first byte after the deleted
1191 * section. "next" is NULL when deleting to the end of the
1192 * file. */
1193 next = off2pos(buf->bufp, off + count);
1194
1195 /* Remove part of the first line. */
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001196 if (first.col != 0
1197 || (next != NULL && first.lnum == next->lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 {
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001199 if (first.lnum != last.lnum
1200 || (next != NULL && first.lnum != next->lnum))
1201 {
1202 /* remove to the end of the first line */
1203 nb_partialremove(first.lnum, first.col,
1204 (colnr_T)MAXCOL);
1205 if (first.lnum == last.lnum)
1206 {
1207 /* Partial line to remove includes the end of
1208 * line. Join the line with the next one, have
1209 * the next line deleted below. */
1210 nb_joinlines(first.lnum, next->lnum);
1211 del_to_lnum = next->lnum;
1212 }
1213 }
1214 else
1215 {
1216 /* remove within one line */
1217 nb_partialremove(first.lnum, first.col, last.col);
1218 }
1219 ++del_from_lnum; /* don't delete the first line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 }
1221
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001222 /* Remove part of the last line. */
1223 if (first.lnum != last.lnum && next != NULL
1224 && next->col != 0 && last.lnum == next->lnum)
1225 {
1226 nb_partialremove(last.lnum, 0, last.col);
1227 if (del_from_lnum > first.lnum)
1228 {
1229 /* Join end of last line to start of first line; last
1230 * line is deleted below. */
1231 nb_joinlines(first.lnum, last.lnum);
1232 }
1233 else
1234 /* First line is deleted as a whole, keep the last
1235 * line. */
1236 --del_to_lnum;
1237 }
1238
1239 /* First is partial line; last line to remove includes
1240 * the end of line; join first line to line following last
1241 * line; line following last line is deleted below. */
1242 if (first.lnum != last.lnum && del_from_lnum > first.lnum
1243 && next != NULL && last.lnum != next->lnum)
1244 {
1245 nb_joinlines(first.lnum, next->lnum);
1246 del_to_lnum = next->lnum;
1247 }
1248
1249 /* Delete whole lines if there are any. */
1250 if (del_to_lnum >= del_from_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 {
1252 int i;
1253
1254 /* delete signs from the lines being deleted */
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001255 for (i = del_from_lnum; i <= del_to_lnum; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 {
1257 int id = buf_findsign_id(buf->bufp, (linenr_T)i);
1258 if (id > 0)
1259 {
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001260 nbdebug((" Deleting sign %d on line %d\n",
1261 id, i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 buf_delsign(buf->bufp, id);
1263 }
1264 else
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001265 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 nbdebug((" No sign on line %d\n", i));
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 }
1269
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001270 nbdebug((" Deleting lines %d through %d\n",
1271 del_from_lnum, del_to_lnum));
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001272 curwin->w_cursor.lnum = del_from_lnum;
1273 curwin->w_cursor.col = 0;
1274 del_lines(del_to_lnum - del_from_lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 }
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001276
1277 /* Leave cursor at first deleted byte. */
1278 curwin->w_cursor = first;
1279 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 buf->bufp->b_changed = wasChanged; /* logically unchanged */
1281 netbeansFireChanges = oldFire;
1282 netbeansSuppressNoLines = oldSuppress;
1283
1284 u_blockfree(buf->bufp);
1285 u_clearall(buf->bufp);
1286 }
1287 nb_reply_nil(cmdno);
1288/* =====================================================================*/
1289 }
1290 else if (streq((char *)cmd, "insert"))
1291 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 char_u *to_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293
1294 if (skip >= SKIP_STOP)
1295 {
1296 nbdebug((" Skipping %s command\n", (char *) cmd));
1297 nb_reply_nil(cmdno);
1298 return OK;
1299 }
1300
1301 /* get offset */
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001302 cp = (char *)args;
1303 off = strtol(cp, &cp, 10);
1304 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305
1306 /* get text to be inserted */
1307 args = skipwhite(args);
1308 args = to_free = (char_u *)nb_unquote(args, NULL);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001309 /*
1310 nbdebug((" CHUNK[%d]: %d bytes at offset %d\n",
1311 buf->bufp->b_ml.ml_line_count, STRLEN(args), off));
1312 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313
1314 if (buf == NULL || buf->bufp == NULL)
1315 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001316 nbdebug((" invalid buffer identifier in insert\n"));
1317 EMSG("E635: invalid buffer identifier in insert");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 retval = FAIL;
1319 }
1320 else if (args != NULL)
1321 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001322 int ff_detected = EOL_UNKNOWN;
1323 int buf_was_empty = (buf->bufp->b_ml.ml_flags & ML_EMPTY);
1324 size_t len = 0;
1325 int added = 0;
1326 int oldFire = netbeansFireChanges;
1327 int old_b_changed;
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001328 char_u *nlp;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001329 linenr_T lnum;
1330 linenr_T lnum_start;
1331 pos_T *pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 netbeansFireChanges = 0;
1334
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001335 /* Jump to the buffer where we insert. After this "curbuf"
1336 * can be used. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001337 nb_set_curbuf(buf->bufp);
Bram Moolenaara3227e22006-03-08 21:32:40 +00001338 old_b_changed = curbuf->b_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001340 /* Convert the specified character offset into a lnum/col
1341 * position. */
Bram Moolenaara3227e22006-03-08 21:32:40 +00001342 pos = off2pos(curbuf, off);
1343 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001345 if (pos->lnum <= 0)
1346 lnum_start = 1;
1347 else
1348 lnum_start = pos->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 }
1350 else
1351 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001352 /* If the given position is not found, assume we want
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 * the end of the file. See setLocAndSize HACK. */
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001354 if (buf_was_empty)
1355 lnum_start = 1; /* above empty line */
1356 else
1357 lnum_start = curbuf->b_ml.ml_line_count + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001358 }
1359
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001360 /* "lnum" is the line where we insert: either append to it or
1361 * insert a new line above it. */
1362 lnum = lnum_start;
1363
1364 /* Loop over the "\n" separated lines of the argument. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001365 do_update = 1;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001366 while (*args != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 {
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001368 nlp = vim_strchr(args, '\n');
1369 if (nlp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001371 /* Incomplete line, probably truncated. Next "insert"
1372 * command should append to this one. */
1373 len = STRLEN(args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001375 else
1376 {
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001377 len = nlp - args;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001378
1379 /*
1380 * We need to detect EOL style, because the commands
1381 * use a character offset.
1382 */
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001383 if (nlp > args && nlp[-1] == '\r')
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001384 {
1385 ff_detected = EOL_DOS;
1386 --len;
1387 }
1388 else
1389 ff_detected = EOL_UNIX;
1390 }
1391 args[len] = NUL;
1392
1393 if (lnum == lnum_start
1394 && ((pos != NULL && pos->col > 0)
1395 || (lnum == 1 && buf_was_empty)))
1396 {
1397 char_u *oldline = ml_get(lnum);
1398 char_u *newline;
1399
Bram Moolenaare4365282012-04-20 19:47:05 +02001400 /* Insert halfway a line. */
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001401 newline = alloc_check(
1402 (unsigned)(STRLEN(oldline) + len + 1));
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001403 if (newline != NULL)
1404 {
Bram Moolenaare4365282012-04-20 19:47:05 +02001405 mch_memmove(newline, oldline, (size_t)pos->col);
1406 newline[pos->col] = NUL;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001407 STRCAT(newline, args);
Bram Moolenaare4365282012-04-20 19:47:05 +02001408 STRCAT(newline, oldline + pos->col);
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001409 ml_replace(lnum, newline, FALSE);
1410 }
1411 }
1412 else
1413 {
1414 /* Append a new line. Not that we always do this,
1415 * also when the text doesn't end in a "\n". */
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001416 ml_append((linenr_T)(lnum - 1), args,
1417 (colnr_T)(len + 1), FALSE);
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001418 ++added;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001419 }
1420
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001421 if (nlp == NULL)
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001422 break;
1423 ++lnum;
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001424 args = nlp + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001425 }
1426
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001427 /* Adjust the marks below the inserted lines. */
1428 appended_lines_mark(lnum_start - 1, (long)added);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001430 /*
1431 * When starting with an empty buffer set the fileformat.
1432 * This is just guessing...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 */
1434 if (buf_was_empty)
1435 {
1436 if (ff_detected == EOL_UNKNOWN)
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001437#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 ff_detected = EOL_DOS;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001439#else
1440 ff_detected = EOL_UNIX;
1441#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 set_fileformat(ff_detected, OPT_LOCAL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00001443 curbuf->b_start_ffc = *curbuf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 }
1445
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 /*
1447 * XXX - GRP - Is the next line right? If I've inserted
1448 * text the buffer has been updated but not written. Will
1449 * netbeans guarantee to write it? Even if I do a :q! ?
1450 */
Bram Moolenaara3227e22006-03-08 21:32:40 +00001451 curbuf->b_changed = old_b_changed; /* logically unchanged */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 netbeansFireChanges = oldFire;
1453
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001454 /* Undo info is invalid now... */
Bram Moolenaara3227e22006-03-08 21:32:40 +00001455 u_blockfree(curbuf);
1456 u_clearall(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 }
1458 vim_free(to_free);
1459 nb_reply_nil(cmdno); /* or !error */
1460 }
1461 else
1462 {
1463 nbdebug(("UNIMPLEMENTED FUNCTION: %s\n", cmd));
1464 nb_reply_nil(cmdno);
1465 retval = FAIL;
1466 }
1467 }
1468 else /* Not a function; no reply required. */
1469 {
1470/* =====================================================================*/
1471 if (streq((char *)cmd, "create"))
1472 {
1473 /* Create a buffer without a name. */
1474 if (buf == NULL)
1475 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001476 nbdebug((" invalid buffer identifier in create\n"));
1477 EMSG("E636: invalid buffer identifier in create");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 return FAIL;
1479 }
1480 vim_free(buf->displayname);
1481 buf->displayname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482
1483 netbeansReadFile = 0; /* don't try to open disk file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001484 do_ecmd(0, NULL, 0, 0, ECMD_ONE, ECMD_HIDE + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 netbeansReadFile = 1;
1486 buf->bufp = curbuf;
1487 maketitle();
Bram Moolenaar009b2592004-10-24 19:18:58 +00001488 buf->insertDone = FALSE;
Bram Moolenaar67c53842010-05-22 18:28:27 +02001489#if defined(FEAT_MENU) && defined(FEAT_GUI)
Bram Moolenaard54a6882010-08-09 22:49:00 +02001490 if (gui.in_use)
1491 gui_update_menus(0);
Bram Moolenaar67c53842010-05-22 18:28:27 +02001492#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493/* =====================================================================*/
1494 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001495 else if (streq((char *)cmd, "insertDone"))
1496 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001497 if (buf == NULL || buf->bufp == NULL)
1498 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001499 nbdebug((" invalid buffer identifier in insertDone\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001500 }
1501 else
1502 {
1503 buf->bufp->b_start_eol = *args == 'T';
1504 buf->insertDone = TRUE;
1505 args += 2;
1506 buf->bufp->b_p_ro = *args == 'T';
1507 print_read_msg(buf);
1508 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001509/* =====================================================================*/
1510 }
1511 else if (streq((char *)cmd, "saveDone"))
1512 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001513 long savedChars = atol((char *)args);
1514
1515 if (buf == NULL || buf->bufp == NULL)
1516 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001517 nbdebug((" invalid buffer identifier in saveDone\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001518 }
1519 else
1520 print_save_msg(buf, savedChars);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001521/* =====================================================================*/
1522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 else if (streq((char *)cmd, "startDocumentListen"))
1524 {
1525 if (buf == NULL)
1526 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001527 nbdebug((" invalid buffer identifier in startDocumentListen\n"));
1528 EMSG("E637: invalid buffer identifier in startDocumentListen");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 return FAIL;
1530 }
1531 buf->fireChanges = 1;
1532/* =====================================================================*/
1533 }
1534 else if (streq((char *)cmd, "stopDocumentListen"))
1535 {
1536 if (buf == NULL)
1537 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001538 nbdebug((" invalid buffer identifier in stopDocumentListen\n"));
1539 EMSG("E638: invalid buffer identifier in stopDocumentListen");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 return FAIL;
1541 }
1542 buf->fireChanges = 0;
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001543 if (buf->bufp != NULL && buf->bufp->b_was_netbeans_file)
Bram Moolenaar009b2592004-10-24 19:18:58 +00001544 {
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001545 if (!buf->bufp->b_netbeans_file)
Bram Moolenaarf2330482008-06-24 20:19:36 +00001546 {
1547 nbdebug(("E658: NetBeans connection lost for buffer %ld\n", buf->bufp->b_fnum));
Bram Moolenaar009b2592004-10-24 19:18:58 +00001548 EMSGN(_("E658: NetBeans connection lost for buffer %ld"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 buf->bufp->b_fnum);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001550 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001551 else
1552 {
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001553 /* NetBeans uses stopDocumentListen when it stops editing
1554 * a file. It then expects the buffer in Vim to
1555 * disappear. */
1556 do_bufdel(DOBUF_DEL, (char_u *)"", 1,
1557 buf->bufp->b_fnum, buf->bufp->b_fnum, TRUE);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001558 vim_memset(buf, 0, sizeof(nbbuf_T));
1559 }
1560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561/* =====================================================================*/
1562 }
1563 else if (streq((char *)cmd, "setTitle"))
1564 {
1565 if (buf == NULL)
1566 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001567 nbdebug((" invalid buffer identifier in setTitle\n"));
1568 EMSG("E639: invalid buffer identifier in setTitle");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 return FAIL;
1570 }
1571 vim_free(buf->displayname);
1572 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573/* =====================================================================*/
1574 }
1575 else if (streq((char *)cmd, "initDone"))
1576 {
1577 if (buf == NULL || buf->bufp == NULL)
1578 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001579 nbdebug((" invalid buffer identifier in initDone\n"));
1580 EMSG("E640: invalid buffer identifier in initDone");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 return FAIL;
1582 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001583 do_update = 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001584 buf->initDone = TRUE;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001585 nb_set_curbuf(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586#if defined(FEAT_AUTOCMD)
1587 apply_autocmds(EVENT_BUFREADPOST, 0, 0, FALSE, buf->bufp);
1588#endif
1589
1590 /* handle any postponed key commands */
1591 handle_key_queue();
1592/* =====================================================================*/
1593 }
1594 else if (streq((char *)cmd, "setBufferNumber")
1595 || streq((char *)cmd, "putBufferNumber"))
1596 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00001597 char_u *path;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 buf_T *bufp;
1599
1600 if (buf == NULL)
1601 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001602 nbdebug((" invalid buffer identifier in setBufferNumber\n"));
1603 EMSG("E641: invalid buffer identifier in setBufferNumber");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 return FAIL;
1605 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001606 path = (char_u *)nb_unquote(args, NULL);
1607 if (path == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 return FAIL;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001609 bufp = buflist_findname(path);
1610 vim_free(path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 if (bufp == NULL)
1612 {
Bram Moolenaarec906222009-02-21 21:14:00 +00001613 nbdebug((" File %s not found in setBufferNumber\n", args));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 EMSG2("E642: File %s not found in setBufferNumber", args);
1615 return FAIL;
1616 }
1617 buf->bufp = bufp;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001618 buf->nbbuf_number = bufp->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619
1620 /* "setBufferNumber" has the side effect of jumping to the buffer
1621 * (don't know why!). Don't do that for "putBufferNumber". */
1622 if (*cmd != 'p')
1623 coloncmd(":buffer %d", bufp->b_fnum);
1624 else
1625 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00001626 buf->initDone = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627
1628 /* handle any postponed key commands */
1629 handle_key_queue();
1630 }
1631
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632/* =====================================================================*/
1633 }
1634 else if (streq((char *)cmd, "setFullName"))
1635 {
1636 if (buf == NULL)
1637 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001638 nbdebug((" invalid buffer identifier in setFullName\n"));
1639 EMSG("E643: invalid buffer identifier in setFullName");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 return FAIL;
1641 }
1642 vim_free(buf->displayname);
1643 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644
1645 netbeansReadFile = 0; /* don't try to open disk file */
1646 do_ecmd(0, (char_u *)buf->displayname, 0, 0, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001647 ECMD_HIDE + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 netbeansReadFile = 1;
1649 buf->bufp = curbuf;
1650 maketitle();
Bram Moolenaar67c53842010-05-22 18:28:27 +02001651#if defined(FEAT_MENU) && defined(FEAT_GUI)
Bram Moolenaard54a6882010-08-09 22:49:00 +02001652 if (gui.in_use)
1653 gui_update_menus(0);
Bram Moolenaar67c53842010-05-22 18:28:27 +02001654#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655/* =====================================================================*/
1656 }
1657 else if (streq((char *)cmd, "editFile"))
1658 {
1659 if (buf == NULL)
1660 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001661 nbdebug((" invalid buffer identifier in editFile\n"));
1662 EMSG("E644: invalid buffer identifier in editFile");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 return FAIL;
1664 }
1665 /* Edit a file: like create + setFullName + read the file. */
1666 vim_free(buf->displayname);
1667 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 do_ecmd(0, (char_u *)buf->displayname, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001669 ECMD_HIDE + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 buf->bufp = curbuf;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001671 buf->initDone = TRUE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001672 do_update = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673#if defined(FEAT_TITLE)
1674 maketitle();
1675#endif
Bram Moolenaar67c53842010-05-22 18:28:27 +02001676#if defined(FEAT_MENU) && defined(FEAT_GUI)
Bram Moolenaard54a6882010-08-09 22:49:00 +02001677 if (gui.in_use)
1678 gui_update_menus(0);
Bram Moolenaar67c53842010-05-22 18:28:27 +02001679#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680/* =====================================================================*/
1681 }
1682 else if (streq((char *)cmd, "setVisible"))
1683 {
1684 if (buf == NULL || buf->bufp == NULL)
1685 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001686 nbdebug((" invalid buffer identifier in setVisible\n"));
1687 /* This message was commented out, probably because it can
1688 * happen when shutting down. */
1689 if (p_verbose > 0)
1690 EMSG("E645: invalid buffer identifier in setVisible");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 return FAIL;
1692 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001693 if (streq((char *)args, "T") && buf->bufp != curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 {
1695 exarg_T exarg;
1696 exarg.cmd = (char_u *)"goto";
1697 exarg.forceit = FALSE;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001698 dosetvisible = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 goto_buffer(&exarg, DOBUF_FIRST, FORWARD, buf->bufp->b_fnum);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001700 do_update = 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001701 dosetvisible = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702
Bram Moolenaar67c53842010-05-22 18:28:27 +02001703#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 /* Side effect!!!. */
Bram Moolenaard54a6882010-08-09 22:49:00 +02001705 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 gui_mch_set_foreground();
Bram Moolenaar67c53842010-05-22 18:28:27 +02001707#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709/* =====================================================================*/
1710 }
1711 else if (streq((char *)cmd, "raise"))
1712 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02001713#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 /* Bring gvim to the foreground. */
Bram Moolenaard54a6882010-08-09 22:49:00 +02001715 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 gui_mch_set_foreground();
Bram Moolenaar67c53842010-05-22 18:28:27 +02001717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718/* =====================================================================*/
1719 }
1720 else if (streq((char *)cmd, "setModified"))
1721 {
Bram Moolenaarff064e12008-06-09 13:10:45 +00001722 int prev_b_changed;
1723
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 if (buf == NULL || buf->bufp == NULL)
1725 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001726 nbdebug((" invalid buffer identifier in setModified\n"));
1727 /* This message was commented out, probably because it can
1728 * happen when shutting down. */
1729 if (p_verbose > 0)
1730 EMSG("E646: invalid buffer identifier in setModified");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 return FAIL;
1732 }
Bram Moolenaarff064e12008-06-09 13:10:45 +00001733 prev_b_changed = buf->bufp->b_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 if (streq((char *)args, "T"))
Bram Moolenaarff064e12008-06-09 13:10:45 +00001735 buf->bufp->b_changed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 else
1737 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02001738 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739
1740 /* Assume NetBeans stored the file. Reset the timestamp to
1741 * avoid "file changed" warnings. */
1742 if (buf->bufp->b_ffname != NULL
1743 && mch_stat((char *)buf->bufp->b_ffname, &st) >= 0)
1744 buf_store_time(buf->bufp, &st, buf->bufp->b_ffname);
Bram Moolenaarff064e12008-06-09 13:10:45 +00001745 buf->bufp->b_changed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 }
1747 buf->modified = buf->bufp->b_changed;
Bram Moolenaarff064e12008-06-09 13:10:45 +00001748 if (prev_b_changed != buf->bufp->b_changed)
1749 {
Bram Moolenaarff064e12008-06-09 13:10:45 +00001750 check_status(buf->bufp);
1751 redraw_tabline = TRUE;
Bram Moolenaarff064e12008-06-09 13:10:45 +00001752#ifdef FEAT_TITLE
1753 maketitle();
1754#endif
1755 update_screen(0);
1756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757/* =====================================================================*/
1758 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001759 else if (streq((char *)cmd, "setModtime"))
1760 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001761 if (buf == NULL || buf->bufp == NULL)
Bram Moolenaarf2330482008-06-24 20:19:36 +00001762 nbdebug((" invalid buffer identifier in setModtime\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001763 else
1764 buf->bufp->b_mtime = atoi((char *)args);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001765/* =====================================================================*/
1766 }
1767 else if (streq((char *)cmd, "setReadOnly"))
1768 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001769 if (buf == NULL || buf->bufp == NULL)
Bram Moolenaarf2330482008-06-24 20:19:36 +00001770 nbdebug((" invalid buffer identifier in setReadOnly\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001771 else if (streq((char *)args, "T"))
Bram Moolenaar009b2592004-10-24 19:18:58 +00001772 buf->bufp->b_p_ro = TRUE;
1773 else
1774 buf->bufp->b_p_ro = FALSE;
1775/* =====================================================================*/
1776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 else if (streq((char *)cmd, "setMark"))
1778 {
1779 /* not yet */
1780/* =====================================================================*/
1781 }
1782 else if (streq((char *)cmd, "showBalloon"))
1783 {
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001784#if defined(FEAT_BEVAL_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 static char *text = NULL;
1786
1787 /*
1788 * Set up the Balloon Expression Evaluation area.
1789 * Ignore 'ballooneval' here.
1790 * The text pointer must remain valid for a while.
1791 */
1792 if (balloonEval != NULL)
1793 {
1794 vim_free(text);
1795 text = nb_unquote(args, NULL);
1796 if (text != NULL)
1797 gui_mch_post_balloon(balloonEval, (char_u *)text);
1798 }
1799#endif
1800/* =====================================================================*/
1801 }
1802 else if (streq((char *)cmd, "setDot"))
1803 {
1804 pos_T *pos;
1805#ifdef NBDEBUG
1806 char_u *s;
1807#endif
1808
1809 if (buf == NULL || buf->bufp == NULL)
1810 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001811 nbdebug((" invalid buffer identifier in setDot\n"));
1812 EMSG("E647: invalid buffer identifier in setDot");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 return FAIL;
1814 }
1815
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001816 nb_set_curbuf(buf->bufp);
1817
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 /* Don't want Visual mode now. */
1819 if (VIsual_active)
1820 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821#ifdef NBDEBUG
1822 s = args;
1823#endif
1824 pos = get_off_or_lnum(buf->bufp, &args);
1825 if (pos)
1826 {
1827 curwin->w_cursor = *pos;
1828 check_cursor();
1829#ifdef FEAT_FOLDING
1830 foldOpenCursor();
1831#endif
1832 }
1833 else
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001834 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 nbdebug((" BAD POSITION in setDot: %s\n", s));
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837
1838 /* gui_update_cursor(TRUE, FALSE); */
1839 /* update_curbuf(NOT_VALID); */
1840 update_topline(); /* scroll to show the line */
1841 update_screen(VALID);
1842 setcursor();
Bram Moolenaar96bcc5e2011-04-01 15:33:59 +02001843 cursor_on();
Bram Moolenaara338adc2018-01-31 20:51:47 +01001844 out_flush_cursor(TRUE, FALSE);
1845
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 /* Quit a hit-return or more prompt. */
1847 if (State == HITRETURN || State == ASKMORE)
1848 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849#ifdef FEAT_GUI_GTK
Bram Moolenaard54a6882010-08-09 22:49:00 +02001850 if (gui.in_use && gtk_main_level() > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 gtk_main_quit();
1852#endif
1853 }
1854/* =====================================================================*/
1855 }
1856 else if (streq((char *)cmd, "close"))
1857 {
1858#ifdef NBDEBUG
1859 char *name = "<NONE>";
1860#endif
1861
1862 if (buf == NULL)
1863 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001864 nbdebug((" invalid buffer identifier in close\n"));
1865 EMSG("E648: invalid buffer identifier in close");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 return FAIL;
1867 }
1868
1869#ifdef NBDEBUG
1870 if (buf->displayname != NULL)
1871 name = buf->displayname;
1872#endif
Bram Moolenaarf2330482008-06-24 20:19:36 +00001873 if (buf->bufp == NULL)
1874 {
1875 nbdebug((" invalid buffer identifier in close\n"));
1876 /* This message was commented out, probably because it can
1877 * happen when shutting down. */
1878 if (p_verbose > 0)
1879 EMSG("E649: invalid buffer identifier in close");
1880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 nbdebug((" CLOSE %d: %s\n", bufno, name));
Bram Moolenaar67c53842010-05-22 18:28:27 +02001882#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 need_mouse_correct = TRUE;
Bram Moolenaar67c53842010-05-22 18:28:27 +02001884#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 if (buf->bufp != NULL)
1886 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD,
1887 buf->bufp->b_fnum, TRUE);
Bram Moolenaar8d602722006-08-08 19:34:19 +00001888 buf->bufp = NULL;
1889 buf->initDone = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001890 do_update = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891/* =====================================================================*/
1892 }
1893 else if (streq((char *)cmd, "setStyle")) /* obsolete... */
1894 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001895 nbdebug((" setStyle is obsolete!\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896/* =====================================================================*/
1897 }
1898 else if (streq((char *)cmd, "setExitDelay"))
1899 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00001900 /* Only used in version 2.1. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901/* =====================================================================*/
1902 }
1903 else if (streq((char *)cmd, "defineAnnoType"))
1904 {
1905#ifdef FEAT_SIGNS
1906 int typeNum;
1907 char_u *typeName;
1908 char_u *tooltip;
1909 char_u *p;
1910 char_u *glyphFile;
Bram Moolenaar67c53842010-05-22 18:28:27 +02001911 int parse_error = FALSE;
1912 char_u *fg;
1913 char_u *bg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914
1915 if (buf == NULL)
1916 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001917 nbdebug((" invalid buffer identifier in defineAnnoType\n"));
1918 EMSG("E650: invalid buffer identifier in defineAnnoType");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 return FAIL;
1920 }
1921
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001922 cp = (char *)args;
1923 typeNum = strtol(cp, &cp, 10);
1924 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 args = skipwhite(args);
1926 typeName = (char_u *)nb_unquote(args, &args);
1927 args = skipwhite(args + 1);
1928 tooltip = (char_u *)nb_unquote(args, &args);
1929 args = skipwhite(args + 1);
1930
1931 p = (char_u *)nb_unquote(args, &args);
1932 glyphFile = vim_strsave_escaped(p, escape_chars);
1933 vim_free(p);
1934
1935 args = skipwhite(args + 1);
Bram Moolenaar67c53842010-05-22 18:28:27 +02001936 p = skiptowhite(args);
1937 if (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02001939 *p = NUL;
1940 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02001942 fg = vim_strsave(args);
1943 bg = vim_strsave(p);
1944 if (STRLEN(fg) > MAX_COLOR_LENGTH || STRLEN(bg) > MAX_COLOR_LENGTH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02001946 EMSG("E532: highlighting color name too long in defineAnnoType");
1947 vim_free(typeName);
1948 parse_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02001950 else if (typeName != NULL && tooltip != NULL && glyphFile != NULL)
1951 addsigntype(buf, typeNum, typeName, tooltip, glyphFile, fg, bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 else
1953 vim_free(typeName);
1954
1955 /* don't free typeName; it's used directly in addsigntype() */
Bram Moolenaar67c53842010-05-22 18:28:27 +02001956 vim_free(fg);
1957 vim_free(bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 vim_free(tooltip);
1959 vim_free(glyphFile);
Bram Moolenaar67c53842010-05-22 18:28:27 +02001960 if (parse_error)
1961 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962
1963#endif
1964/* =====================================================================*/
1965 }
1966 else if (streq((char *)cmd, "addAnno"))
1967 {
1968#ifdef FEAT_SIGNS
1969 int serNum;
1970 int localTypeNum;
1971 int typeNum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 pos_T *pos;
1973
1974 if (buf == NULL || buf->bufp == NULL)
1975 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001976 nbdebug((" invalid buffer identifier in addAnno\n"));
1977 EMSG("E651: invalid buffer identifier in addAnno");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 return FAIL;
1979 }
1980
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001981 do_update = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001983 cp = (char *)args;
1984 serNum = strtol(cp, &cp, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985
1986 /* Get the typenr specific for this buffer and convert it to
1987 * the global typenumber, as used for the sign name. */
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001988 localTypeNum = strtol(cp, &cp, 10);
1989 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 typeNum = mapsigntype(buf, localTypeNum);
1991
1992 pos = get_off_or_lnum(buf->bufp, &args);
1993
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001994 cp = (char *)args;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00001995 ignored = (int)strtol(cp, &cp, 10);
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001996 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997# ifdef NBDEBUG
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00001998 if (ignored != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002000 nbdebug((" partial line annotation -- Not Yet Implemented!\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 }
2002# endif
2003 if (serNum >= GUARDEDOFFSET)
2004 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002005 nbdebug((" too many annotations! ignoring...\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 return FAIL;
2007 }
2008 if (pos)
2009 {
Bram Moolenaarec906222009-02-21 21:14:00 +00002010 coloncmd(":sign place %d line=%ld name=%d buffer=%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 serNum, pos->lnum, typeNum, buf->bufp->b_fnum);
2012 if (typeNum == curPCtype)
2013 coloncmd(":sign jump %d buffer=%d", serNum,
2014 buf->bufp->b_fnum);
2015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016#endif
2017/* =====================================================================*/
2018 }
2019 else if (streq((char *)cmd, "removeAnno"))
2020 {
2021#ifdef FEAT_SIGNS
2022 int serNum;
2023
2024 if (buf == NULL || buf->bufp == NULL)
2025 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002026 nbdebug((" invalid buffer identifier in removeAnno\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 return FAIL;
2028 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002029 do_update = 1;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002030 cp = (char *)args;
2031 serNum = strtol(cp, &cp, 10);
2032 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 coloncmd(":sign unplace %d buffer=%d",
2034 serNum, buf->bufp->b_fnum);
2035 redraw_buf_later(buf->bufp, NOT_VALID);
2036#endif
2037/* =====================================================================*/
2038 }
2039 else if (streq((char *)cmd, "moveAnnoToFront"))
2040 {
2041#ifdef FEAT_SIGNS
Bram Moolenaarf2330482008-06-24 20:19:36 +00002042 nbdebug((" moveAnnoToFront: Not Yet Implemented!\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043#endif
2044/* =====================================================================*/
2045 }
2046 else if (streq((char *)cmd, "guard") || streq((char *)cmd, "unguard"))
2047 {
2048 int len;
2049 pos_T first;
2050 pos_T last;
2051 pos_T *pos;
2052 int un = (cmd[0] == 'u');
2053 static int guardId = GUARDEDOFFSET;
2054
2055 if (skip >= SKIP_STOP)
2056 {
2057 nbdebug((" Skipping %s command\n", (char *) cmd));
2058 return OK;
2059 }
2060
2061 nb_init_graphics();
2062
2063 if (buf == NULL || buf->bufp == NULL)
2064 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002065 nbdebug((" invalid buffer identifier in %s command\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 return FAIL;
2067 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002068 nb_set_curbuf(buf->bufp);
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002069 cp = (char *)args;
2070 off = strtol(cp, &cp, 10);
2071 len = strtol(cp, NULL, 10);
2072 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 pos = off2pos(buf->bufp, off);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002074 do_update = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 if (!pos)
2076 nbdebug((" no such start pos in %s, %ld\n", cmd, off));
2077 else
2078 {
2079 first = *pos;
2080 pos = off2pos(buf->bufp, off + len - 1);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002081 if (pos != NULL && pos->col == 0)
2082 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 /*
2084 * In Java Swing the offset is a position between 2
2085 * characters. If col == 0 then we really want the
2086 * previous line as the end.
2087 */
2088 pos = off2pos(buf->bufp, off + len - 2);
2089 }
2090 if (!pos)
2091 nbdebug((" no such end pos in %s, %ld\n",
2092 cmd, off + len - 1));
2093 else
2094 {
2095 long lnum;
2096 last = *pos;
2097 /* set highlight for region */
2098 nbdebug((" %sGUARD %ld,%d to %ld,%d\n", (un) ? "UN" : "",
2099 first.lnum, first.col,
2100 last.lnum, last.col));
2101#ifdef FEAT_SIGNS
2102 for (lnum = first.lnum; lnum <= last.lnum; lnum++)
2103 {
2104 if (un)
2105 {
2106 /* never used */
2107 }
2108 else
2109 {
2110 if (buf_findsigntype_id(buf->bufp, lnum,
2111 GUARDED) == 0)
2112 {
2113 coloncmd(
Bram Moolenaarec906222009-02-21 21:14:00 +00002114 ":sign place %d line=%ld name=%d buffer=%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 guardId++, lnum, GUARDED,
2116 buf->bufp->b_fnum);
2117 }
2118 }
2119 }
2120#endif
2121 redraw_buf_later(buf->bufp, NOT_VALID);
2122 }
2123 }
2124/* =====================================================================*/
2125 }
2126 else if (streq((char *)cmd, "startAtomic"))
2127 {
2128 inAtomic = 1;
2129/* =====================================================================*/
2130 }
2131 else if (streq((char *)cmd, "endAtomic"))
2132 {
2133 inAtomic = 0;
2134 if (needupdate)
2135 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002136 do_update = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 needupdate = 0;
2138 }
2139/* =====================================================================*/
2140 }
2141 else if (streq((char *)cmd, "save"))
2142 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002143 /*
Bram Moolenaar9af41842016-09-25 21:45:05 +02002144 * NOTE - This command is obsolete wrt NetBeans. It's left in
Bram Moolenaar009b2592004-10-24 19:18:58 +00002145 * only for historical reasons.
2146 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 if (buf == NULL || buf->bufp == NULL)
2148 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002149 nbdebug((" invalid buffer identifier in %s command\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 return FAIL;
2151 }
2152
2153 /* the following is taken from ex_cmds.c (do_wqall function) */
2154 if (bufIsChanged(buf->bufp))
2155 {
2156 /* Only write if the buffer can be written. */
2157 if (p_write
2158 && !buf->bufp->b_p_ro
2159 && buf->bufp->b_ffname != NULL
2160#ifdef FEAT_QUICKFIX
2161 && !bt_dontwrite(buf->bufp)
2162#endif
2163 )
2164 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002165#ifdef FEAT_AUTOCMD
2166 bufref_T bufref;
2167
2168 set_bufref(&bufref, buf->bufp);
2169#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 buf_write_all(buf->bufp, FALSE);
2171#ifdef FEAT_AUTOCMD
2172 /* an autocommand may have deleted the buffer */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002173 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 buf->bufp = NULL;
2175#endif
2176 }
2177 }
Bram Moolenaarf2330482008-06-24 20:19:36 +00002178 else
2179 {
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002180 nbdebug((" Buffer has no changes!\n"));
Bram Moolenaarf2330482008-06-24 20:19:36 +00002181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182/* =====================================================================*/
2183 }
2184 else if (streq((char *)cmd, "netbeansBuffer"))
2185 {
2186 if (buf == NULL || buf->bufp == NULL)
2187 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002188 nbdebug((" invalid buffer identifier in %s command\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 return FAIL;
2190 }
2191 if (*args == 'T')
2192 {
2193 buf->bufp->b_netbeans_file = TRUE;
2194 buf->bufp->b_was_netbeans_file = TRUE;
2195 }
2196 else
2197 buf->bufp->b_netbeans_file = FALSE;
2198/* =====================================================================*/
2199 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002200 else if (streq((char *)cmd, "specialKeys"))
2201 {
2202 special_keys(args);
2203/* =====================================================================*/
2204 }
2205 else if (streq((char *)cmd, "actionMenuItem"))
2206 {
2207 /* not used yet */
2208/* =====================================================================*/
2209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 else if (streq((char *)cmd, "version"))
2211 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002212 /* not used yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 }
Bram Moolenaarf2330482008-06-24 20:19:36 +00002214 else
2215 {
2216 nbdebug(("Unrecognised command: %s\n", cmd));
2217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 /*
2219 * Unrecognized command is ignored.
2220 */
2221 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002222 if (inAtomic && do_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 {
2224 needupdate = 1;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002225 do_update = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 }
2227
Bram Moolenaar009b2592004-10-24 19:18:58 +00002228 /*
2229 * Is this needed? I moved the netbeans_Xt_connect() later during startup
Bram Moolenaar9af41842016-09-25 21:45:05 +02002230 * and it may no longer be necessary. If it's not needed then needupdate
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002231 * and do_update can also be removed.
Bram Moolenaar009b2592004-10-24 19:18:58 +00002232 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002233 if (buf != NULL && buf->initDone && do_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 {
2235 update_screen(NOT_VALID);
2236 setcursor();
Bram Moolenaar96bcc5e2011-04-01 15:33:59 +02002237 cursor_on();
Bram Moolenaara338adc2018-01-31 20:51:47 +01002238 out_flush_cursor(TRUE, FALSE);
2239
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 /* Quit a hit-return or more prompt. */
2241 if (State == HITRETURN || State == ASKMORE)
2242 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243#ifdef FEAT_GUI_GTK
Bram Moolenaard54a6882010-08-09 22:49:00 +02002244 if (gui.in_use && gtk_main_level() > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 gtk_main_quit();
2246#endif
2247 }
2248 }
2249
2250 return retval;
2251}
2252
2253
2254/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002255 * If "buf" is not the current buffer try changing to a window that edits this
2256 * buffer. If there is no such window then close the current buffer and set
2257 * the current buffer as "buf".
2258 */
2259 static void
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00002260nb_set_curbuf(buf_T *buf)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002261{
Bram Moolenaar404c9422015-03-14 15:35:52 +01002262 if (curbuf != buf) {
2263 if (buf_jump_open_win(buf) != NULL)
2264 return;
Bram Moolenaar404c9422015-03-14 15:35:52 +01002265 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf) != NULL)
2266 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002267 set_curbuf(buf, DOBUF_GOTO);
Bram Moolenaar404c9422015-03-14 15:35:52 +01002268 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002269}
2270
2271/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 * Process a vim colon command.
2273 */
2274 static void
2275coloncmd(char *cmd, ...)
2276{
2277 char buf[1024];
2278 va_list ap;
2279
2280 va_start(ap, cmd);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02002281 vim_vsnprintf(buf, sizeof(buf), cmd, ap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 va_end(ap);
2283
2284 nbdebug((" COLONCMD %s\n", buf));
2285
2286/* ALT_INPUT_LOCK_ON; */
2287 do_cmdline((char_u *)buf, NULL, NULL, DOCMD_NOWAIT | DOCMD_KEYTYPED);
2288/* ALT_INPUT_LOCK_OFF; */
2289
2290 setcursor(); /* restore the cursor position */
Bram Moolenaara338adc2018-01-31 20:51:47 +01002291 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292}
2293
2294
2295/*
Bram Moolenaar009b2592004-10-24 19:18:58 +00002296 * Parse the specialKeys argument and issue the appropriate map commands.
2297 */
2298 static void
2299special_keys(char_u *args)
2300{
2301 char *save_str = nb_unquote(args, NULL);
2302 char *tok = strtok(save_str, " ");
2303 char *sep;
Bram Moolenaarca24e2c2017-01-22 15:19:22 +01002304#define KEYBUFLEN 64
2305 char keybuf[KEYBUFLEN];
Bram Moolenaar009b2592004-10-24 19:18:58 +00002306 char cmdbuf[256];
2307
2308 while (tok != NULL)
2309 {
2310 int i = 0;
2311
2312 if ((sep = strchr(tok, '-')) != NULL)
2313 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002314 *sep = NUL;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002315 while (*tok)
2316 {
2317 switch (*tok)
2318 {
2319 case 'A':
2320 case 'M':
2321 case 'C':
2322 case 'S':
2323 keybuf[i++] = *tok;
2324 keybuf[i++] = '-';
2325 break;
2326 }
2327 tok++;
2328 }
2329 tok++;
2330 }
2331
Bram Moolenaarca24e2c2017-01-22 15:19:22 +01002332 if (strlen(tok) + i < KEYBUFLEN)
2333 {
2334 strcpy(&keybuf[i], tok);
2335 vim_snprintf(cmdbuf, sizeof(cmdbuf),
2336 "<silent><%s> :nbkey %s<CR>", keybuf, keybuf);
2337 do_map(0, (char_u *)cmdbuf, NORMAL, FALSE);
2338 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002339 tok = strtok(NULL, " ");
2340 }
2341 vim_free(save_str);
2342}
2343
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002344 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002345ex_nbclose(exarg_T *eap UNUSED)
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002346{
2347 netbeans_close();
2348}
Bram Moolenaar009b2592004-10-24 19:18:58 +00002349
2350 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002351ex_nbkey(exarg_T *eap)
Bram Moolenaar009b2592004-10-24 19:18:58 +00002352{
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002353 (void)netbeans_keystring(eap->arg);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002354}
2355
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002356 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002357ex_nbstart(
2358 exarg_T *eap)
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002359{
Bram Moolenaarc2a406b2010-09-30 21:03:26 +02002360#ifdef FEAT_GUI
2361# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar7ad7d012010-11-16 15:49:02 +01002362 && !defined(FEAT_GUI_W32)
Bram Moolenaarc2a406b2010-09-30 21:03:26 +02002363 if (gui.in_use)
2364 {
Bram Moolenaar7ad7d012010-11-16 15:49:02 +01002365 EMSG(_("E838: netbeans is not supported with this GUI"));
2366 return;
Bram Moolenaarc2a406b2010-09-30 21:03:26 +02002367 }
2368# endif
2369#endif
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002370 netbeans_open((char *)eap->arg, FALSE);
2371}
Bram Moolenaar009b2592004-10-24 19:18:58 +00002372
2373/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 * Initialize highlights and signs for use by netbeans (mostly obsolete)
2375 */
2376 static void
2377nb_init_graphics(void)
2378{
2379 static int did_init = FALSE;
2380
2381 if (!did_init)
2382 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002383 coloncmd(":highlight NBGuarded guibg=Cyan guifg=Black"
2384 " ctermbg=LightCyan ctermfg=Black");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 coloncmd(":sign define %d linehl=NBGuarded", GUARDED);
2386
2387 did_init = TRUE;
2388 }
2389}
2390
2391/*
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002392 * Convert key to netbeans name. This uses the global "mod_mask".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 */
2394 static void
2395netbeans_keyname(int key, char *buf)
2396{
2397 char *name = 0;
2398 char namebuf[2];
2399 int ctrl = 0;
2400 int shift = 0;
2401 int alt = 0;
2402
2403 if (mod_mask & MOD_MASK_CTRL)
2404 ctrl = 1;
2405 if (mod_mask & MOD_MASK_SHIFT)
2406 shift = 1;
2407 if (mod_mask & MOD_MASK_ALT)
2408 alt = 1;
2409
2410
2411 switch (key)
2412 {
2413 case K_F1: name = "F1"; break;
2414 case K_S_F1: name = "F1"; shift = 1; break;
2415 case K_F2: name = "F2"; break;
2416 case K_S_F2: name = "F2"; shift = 1; break;
2417 case K_F3: name = "F3"; break;
2418 case K_S_F3: name = "F3"; shift = 1; break;
2419 case K_F4: name = "F4"; break;
2420 case K_S_F4: name = "F4"; shift = 1; break;
2421 case K_F5: name = "F5"; break;
2422 case K_S_F5: name = "F5"; shift = 1; break;
2423 case K_F6: name = "F6"; break;
2424 case K_S_F6: name = "F6"; shift = 1; break;
2425 case K_F7: name = "F7"; break;
2426 case K_S_F7: name = "F7"; shift = 1; break;
2427 case K_F8: name = "F8"; break;
2428 case K_S_F8: name = "F8"; shift = 1; break;
2429 case K_F9: name = "F9"; break;
2430 case K_S_F9: name = "F9"; shift = 1; break;
2431 case K_F10: name = "F10"; break;
2432 case K_S_F10: name = "F10"; shift = 1; break;
2433 case K_F11: name = "F11"; break;
2434 case K_S_F11: name = "F11"; shift = 1; break;
2435 case K_F12: name = "F12"; break;
2436 case K_S_F12: name = "F12"; shift = 1; break;
2437 default:
2438 if (key >= ' ' && key <= '~')
2439 {
2440 /* Allow ASCII characters. */
2441 name = namebuf;
2442 namebuf[0] = key;
2443 namebuf[1] = NUL;
2444 }
2445 else
2446 name = "X";
2447 break;
2448 }
2449
2450 buf[0] = '\0';
2451 if (ctrl)
2452 strcat(buf, "C");
2453 if (shift)
2454 strcat(buf, "S");
2455 if (alt)
2456 strcat(buf, "M"); /* META */
2457 if (ctrl || shift || alt)
2458 strcat(buf, "-");
2459 strcat(buf, name);
2460}
2461
Bram Moolenaar67c53842010-05-22 18:28:27 +02002462#if defined(FEAT_BEVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463/*
2464 * Function to be called for balloon evaluation. Grabs the text under the
2465 * cursor and sends it to the debugger for evaluation. The debugger should
2466 * respond with a showBalloon command when there is a useful result.
2467 */
Bram Moolenaard62bec82005-03-07 22:56:57 +00002468 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469netbeans_beval_cb(
2470 BalloonEval *beval,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002471 int state UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472{
Bram Moolenaard62bec82005-03-07 22:56:57 +00002473 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 char_u *text;
Bram Moolenaard62bec82005-03-07 22:56:57 +00002475 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 int col;
Bram Moolenaard9462e32011-04-11 21:35:11 +02002477 char *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 char_u *p;
2479
2480 /* Don't do anything when 'ballooneval' is off, messages scrolled the
2481 * windows up or we have no connection. */
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01002482 if (!can_use_beval() || !NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 return;
2484
Bram Moolenaard62bec82005-03-07 22:56:57 +00002485 if (get_beval_info(beval, TRUE, &wp, &lnum, &text, &col) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 {
2487 /* Send debugger request. Only when the text is of reasonable
2488 * length. */
2489 if (text != NULL && text[0] != NUL && STRLEN(text) < MAXPATHL)
2490 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02002491 buf = (char *)alloc(MAXPATHL * 2 + 25);
2492 if (buf != NULL)
Bram Moolenaar009b2592004-10-24 19:18:58 +00002493 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02002494 p = nb_quote(text);
2495 if (p != NULL)
2496 {
2497 vim_snprintf(buf, MAXPATHL * 2 + 25,
2498 "0:balloonText=%d \"%s\"\n", r_cmdno, p);
2499 vim_free(p);
2500 }
2501 nbdebug(("EVT: %s", buf));
2502 nb_send(buf, "netbeans_beval_cb");
2503 vim_free(buf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 }
2506 vim_free(text);
2507 }
2508}
2509#endif
2510
2511/*
Bram Moolenaare0874f82016-01-24 20:36:41 +01002512 * Return TRUE when the netbeans connection is active.
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002513 */
2514 int
2515netbeans_active(void)
2516{
2517 return NETBEANS_OPEN;
2518}
2519
Bram Moolenaar67c53842010-05-22 18:28:27 +02002520/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 * Tell netbeans that the window was opened, ready for commands.
2522 */
2523 void
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02002524netbeans_open(char *params, int doabort)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525{
2526 char *cmd = "0:startupDone=0\n";
2527
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002528 if (NETBEANS_OPEN)
2529 {
2530 EMSG(_("E511: netbeans already connected"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02002534 if (netbeans_connect(params, doabort) != OK)
Bram Moolenaar67c53842010-05-22 18:28:27 +02002535 return;
Bram Moolenaard62bec82005-03-07 22:56:57 +00002536
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 nbdebug(("EVT: %s", cmd));
2538 nb_send(cmd, "netbeans_startup_done");
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002539
2540 /* update the screen after having added the gutter */
2541 changed_window_setting();
2542 update_screen(CLEAR);
2543 setcursor();
Bram Moolenaar96bcc5e2011-04-01 15:33:59 +02002544 cursor_on();
Bram Moolenaara338adc2018-01-31 20:51:47 +01002545 out_flush_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546}
2547
Bram Moolenaar009b2592004-10-24 19:18:58 +00002548/*
2549 * Tell netbeans that we're exiting. This should be called right
2550 * before calling exit.
2551 */
2552 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002553netbeans_send_disconnect(void)
Bram Moolenaar009b2592004-10-24 19:18:58 +00002554{
2555 char buf[128];
2556
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002557 if (NETBEANS_OPEN)
Bram Moolenaar009b2592004-10-24 19:18:58 +00002558 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002559 sprintf(buf, "0:disconnect=%d\n", r_cmdno);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002560 nbdebug(("EVT: %s", buf));
2561 nb_send(buf, "netbeans_disconnect");
2562 }
2563}
2564
Bram Moolenaar3266c852016-04-30 18:07:05 +02002565#if defined(FEAT_EVAL) || defined(PROTO)
2566 int
2567set_ref_in_nb_channel(int copyID)
2568{
2569 int abort = FALSE;
2570 typval_T tv;
2571
2572 if (nb_channel != NULL)
2573 {
2574 tv.v_type = VAR_CHANNEL;
2575 tv.vval.v_channel = nb_channel;
2576 abort = set_ref_in_item(&tv, copyID, NULL, NULL);
2577 }
2578 return abort;
2579}
2580#endif
2581
Bram Moolenaar173c9852010-09-29 17:27:01 +02002582#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_W32) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583/*
2584 * Tell netbeans that the window was moved or resized.
2585 */
2586 void
2587netbeans_frame_moved(int new_x, int new_y)
2588{
2589 char buf[128];
2590
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002591 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 return;
2593
2594 sprintf(buf, "0:geometry=%d %d %d %d %d\n",
Bram Moolenaar89d40322006-08-29 15:30:07 +00002595 r_cmdno, (int)Columns, (int)Rows, new_x, new_y);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002596 /*nbdebug(("EVT: %s", buf)); happens too many times during a move */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 nb_send(buf, "netbeans_frame_moved");
2598}
2599#endif
2600
2601/*
Bram Moolenaar009b2592004-10-24 19:18:58 +00002602 * Tell netbeans the user opened or activated a file.
2603 */
2604 void
2605netbeans_file_activated(buf_T *bufp)
2606{
2607 int bufno = nb_getbufno(bufp);
2608 nbbuf_T *bp = nb_get_buf(bufno);
2609 char buffer[2*MAXPATHL];
2610 char_u *q;
2611
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002612 if (!NETBEANS_OPEN || !bufp->b_netbeans_file || dosetvisible)
Bram Moolenaar009b2592004-10-24 19:18:58 +00002613 return;
2614
2615 q = nb_quote(bufp->b_ffname);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002616 if (q == NULL || bp == NULL)
Bram Moolenaar009b2592004-10-24 19:18:58 +00002617 return;
2618
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002619 vim_snprintf(buffer, sizeof(buffer), "%d:fileOpened=%d \"%s\" %s %s\n",
Bram Moolenaar009b2592004-10-24 19:18:58 +00002620 bufno,
2621 bufno,
2622 (char *)q,
2623 "T", /* open in NetBeans */
2624 "F"); /* modified */
2625
2626 vim_free(q);
2627 nbdebug(("EVT: %s", buffer));
2628
2629 nb_send(buffer, "netbeans_file_opened");
2630}
2631
2632/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 * Tell netbeans the user opened a file.
2634 */
2635 void
Bram Moolenaar009b2592004-10-24 19:18:58 +00002636netbeans_file_opened(buf_T *bufp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637{
Bram Moolenaar009b2592004-10-24 19:18:58 +00002638 int bufno = nb_getbufno(bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 char buffer[2*MAXPATHL];
2640 char_u *q;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002641 nbbuf_T *bp = nb_get_buf(nb_getbufno(bufp));
2642 int bnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002644 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 return;
2646
Bram Moolenaar009b2592004-10-24 19:18:58 +00002647 q = nb_quote(bufp->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 if (q == NULL)
2649 return;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002650 if (bp != NULL)
2651 bnum = bufno;
2652 else
2653 bnum = 0;
2654
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002655 vim_snprintf(buffer, sizeof(buffer), "%d:fileOpened=%d \"%s\" %s %s\n",
Bram Moolenaar009b2592004-10-24 19:18:58 +00002656 bnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 0,
2658 (char *)q,
2659 "T", /* open in NetBeans */
2660 "F"); /* modified */
2661
2662 vim_free(q);
2663 nbdebug(("EVT: %s", buffer));
2664
2665 nb_send(buffer, "netbeans_file_opened");
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002666 if (p_acd && vim_chdirfile(bufp->b_ffname, "auto") == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 shorten_fnames(TRUE);
2668}
2669
2670/*
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00002671 * Tell netbeans that a file was deleted or wiped out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 */
2673 void
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00002674netbeans_file_killed(buf_T *bufp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675{
2676 int bufno = nb_getbufno(bufp);
2677 nbbuf_T *nbbuf = nb_get_buf(bufno);
2678 char buffer[2*MAXPATHL];
2679
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002680 if (!NETBEANS_OPEN || bufno == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 return;
2682
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00002683 nbdebug(("netbeans_file_killed:\n"));
2684 nbdebug((" Killing bufno: %d", bufno));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685
Bram Moolenaar89d40322006-08-29 15:30:07 +00002686 sprintf(buffer, "%d:killed=%d\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687
2688 nbdebug(("EVT: %s", buffer));
2689
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00002690 nb_send(buffer, "netbeans_file_killed");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691
2692 if (nbbuf != NULL)
2693 nbbuf->bufp = NULL;
2694}
2695
2696/*
2697 * Get a pointer to the Netbeans buffer for Vim buffer "bufp".
2698 * Return NULL if there is no such buffer or changes are not to be reported.
2699 * Otherwise store the buffer number in "*bufnop".
2700 */
2701 static nbbuf_T *
2702nb_bufp2nbbuf_fire(buf_T *bufp, int *bufnop)
2703{
2704 int bufno;
2705 nbbuf_T *nbbuf;
2706
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002707 if (!NETBEANS_OPEN || !netbeansFireChanges)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 return NULL; /* changes are not reported at all */
2709
2710 bufno = nb_getbufno(bufp);
2711 if (bufno <= 0)
2712 return NULL; /* file is not known to NetBeans */
2713
2714 nbbuf = nb_get_buf(bufno);
2715 if (nbbuf != NULL && !nbbuf->fireChanges)
2716 return NULL; /* changes in this buffer are not reported */
2717
2718 *bufnop = bufno;
2719 return nbbuf;
2720}
2721
2722/*
2723 * Tell netbeans the user inserted some text.
2724 */
2725 void
2726netbeans_inserted(
2727 buf_T *bufp,
2728 linenr_T linenr,
2729 colnr_T col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 char_u *txt,
2731 int newlen)
2732{
2733 char_u *buf;
2734 int bufno;
2735 nbbuf_T *nbbuf;
2736 pos_T pos;
2737 long off;
2738 char_u *p;
2739 char_u *newtxt;
2740
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002741 if (!NETBEANS_OPEN)
2742 return;
2743
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
2745 if (nbbuf == NULL)
2746 return;
2747
Bram Moolenaar009b2592004-10-24 19:18:58 +00002748 /* Don't mark as modified for initial read */
2749 if (nbbuf->insertDone)
2750 nbbuf->modified = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751
2752 pos.lnum = linenr;
2753 pos.col = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 off = pos2off(bufp, &pos);
2755
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 /* send the "insert" EVT */
2757 newtxt = alloc(newlen + 1);
Bram Moolenaarb6356332005-07-18 21:40:44 +00002758 vim_strncpy(newtxt, txt, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 p = nb_quote(newtxt);
2760 if (p != NULL)
2761 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002762 buf = alloc(128 + 2*newlen);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002763 sprintf((char *)buf, "%d:insert=%d %ld \"%s\"\n",
2764 bufno, r_cmdno, off, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 nbdebug(("EVT: %s", buf));
2766 nb_send((char *)buf, "netbeans_inserted");
Bram Moolenaar009b2592004-10-24 19:18:58 +00002767 vim_free(p);
2768 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 vim_free(newtxt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771}
2772
2773/*
2774 * Tell netbeans some bytes have been removed.
2775 */
2776 void
2777netbeans_removed(
2778 buf_T *bufp,
2779 linenr_T linenr,
2780 colnr_T col,
2781 long len)
2782{
2783 char_u buf[128];
2784 int bufno;
2785 nbbuf_T *nbbuf;
2786 pos_T pos;
2787 long off;
2788
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002789 if (!NETBEANS_OPEN)
2790 return;
2791
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
2793 if (nbbuf == NULL)
2794 return;
2795
2796 if (len < 0)
2797 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002798 nbdebug(("Negative len %ld in netbeans_removed()!\n", len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 return;
2800 }
2801
2802 nbbuf->modified = 1;
2803
2804 pos.lnum = linenr;
2805 pos.col = col;
2806
2807 off = pos2off(bufp, &pos);
2808
Bram Moolenaar89d40322006-08-29 15:30:07 +00002809 sprintf((char *)buf, "%d:remove=%d %ld %ld\n", bufno, r_cmdno, off, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 nbdebug(("EVT: %s", buf));
2811 nb_send((char *)buf, "netbeans_removed");
2812}
2813
2814/*
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002815 * Send netbeans an unmodified command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002818netbeans_unmodified(buf_T *bufp UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819{
Bram Moolenaar09092152010-08-08 16:38:42 +02002820 /* This is a no-op, because NetBeans considers a buffer modified
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 * even when all changes have been undone. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822}
2823
2824/*
Bram Moolenaar9af41842016-09-25 21:45:05 +02002825 * Send a button release event back to netbeans. It's up to netbeans
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 * to decide what to do (if anything) with this event.
2827 */
2828 void
2829netbeans_button_release(int button)
2830{
2831 char buf[128];
2832 int bufno;
2833
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002834 if (!NETBEANS_OPEN)
2835 return;
2836
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 bufno = nb_getbufno(curbuf);
2838
2839 if (bufno >= 0 && curwin != NULL && curwin->w_buffer == curbuf)
2840 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02002841 int col = mouse_col - curwin->w_wincol
Bram Moolenaar67c53842010-05-22 18:28:27 +02002842 - ((curwin->w_p_nu || curwin->w_p_rnu) ? 9 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 long off = pos2off(curbuf, &curwin->w_cursor);
2844
2845 /* sync the cursor position */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002846 sprintf(buf, "%d:newDotAndMark=%d %ld %ld\n", bufno, r_cmdno, off, off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 nbdebug(("EVT: %s", buf));
2848 nb_send(buf, "netbeans_button_release[newDotAndMark]");
2849
Bram Moolenaar89d40322006-08-29 15:30:07 +00002850 sprintf(buf, "%d:buttonRelease=%d %d %ld %d\n", bufno, r_cmdno,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 button, (long)curwin->w_cursor.lnum, col);
2852 nbdebug(("EVT: %s", buf));
2853 nb_send(buf, "netbeans_button_release");
2854 }
2855}
2856
2857
2858/*
Bram Moolenaar143c38c2007-05-10 16:41:10 +00002859 * Send a keypress event back to netbeans. This usually simulates some
Bram Moolenaar009b2592004-10-24 19:18:58 +00002860 * kind of function key press. This function operates on a key code.
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002861 * Return TRUE when the key was sent, FALSE when the command has been
2862 * postponed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 */
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002864 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865netbeans_keycommand(int key)
2866{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 char keyName[60];
Bram Moolenaar009b2592004-10-24 19:18:58 +00002868
2869 netbeans_keyname(key, keyName);
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002870 return netbeans_keystring((char_u *)keyName);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002871}
2872
2873
2874/*
Bram Moolenaar143c38c2007-05-10 16:41:10 +00002875 * Send a keypress event back to netbeans. This usually simulates some
Bram Moolenaar009b2592004-10-24 19:18:58 +00002876 * kind of function key press. This function operates on a key string.
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002877 * Return TRUE when the key was sent, FALSE when the command has been
2878 * postponed.
Bram Moolenaar009b2592004-10-24 19:18:58 +00002879 */
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002880 static int
2881netbeans_keystring(char_u *keyName)
Bram Moolenaar009b2592004-10-24 19:18:58 +00002882{
2883 char buf[2*MAXPATHL];
2884 int bufno = nb_getbufno(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 long off;
2886 char_u *q;
2887
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002888 if (!NETBEANS_OPEN)
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002889 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 if (bufno == -1)
2892 {
2893 nbdebug(("got keycommand for non-NetBeans buffer, opening...\n"));
2894 q = curbuf->b_ffname == NULL ? (char_u *)""
2895 : nb_quote(curbuf->b_ffname);
2896 if (q == NULL)
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002897 return TRUE;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002898 vim_snprintf(buf, sizeof(buf), "0:fileOpened=%d \"%s\" %s %s\n", 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 q,
2900 "T", /* open in NetBeans */
2901 "F"); /* modified */
2902 if (curbuf->b_ffname != NULL)
2903 vim_free(q);
2904 nbdebug(("EVT: %s", buf));
2905 nb_send(buf, "netbeans_keycommand");
2906
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002907 postpone_keycommand(keyName);
2908 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 }
2910
2911 /* sync the cursor position */
2912 off = pos2off(curbuf, &curwin->w_cursor);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002913 sprintf(buf, "%d:newDotAndMark=%d %ld %ld\n", bufno, r_cmdno, off, off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 nbdebug(("EVT: %s", buf));
2915 nb_send(buf, "netbeans_keycommand");
2916
2917 /* To work on Win32 you must apply patch to ExtEditor module
2918 * from ExtEdCaret.java.diff - make EVT_newDotAndMark handler
2919 * more synchronous
2920 */
2921
2922 /* now send keyCommand event */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002923 vim_snprintf(buf, sizeof(buf), "%d:keyCommand=%d \"%s\"\n",
Bram Moolenaar89d40322006-08-29 15:30:07 +00002924 bufno, r_cmdno, keyName);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 nbdebug(("EVT: %s", buf));
2926 nb_send(buf, "netbeans_keycommand");
2927
2928 /* New: do both at once and include the lnum/col. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002929 vim_snprintf(buf, sizeof(buf), "%d:keyAtPos=%d \"%s\" %ld %ld/%ld\n",
Bram Moolenaar89d40322006-08-29 15:30:07 +00002930 bufno, r_cmdno, keyName,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 off, (long)curwin->w_cursor.lnum, (long)curwin->w_cursor.col);
2932 nbdebug(("EVT: %s", buf));
2933 nb_send(buf, "netbeans_keycommand");
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002934 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935}
2936
2937
2938/*
2939 * Send a save event to netbeans.
2940 */
2941 void
2942netbeans_save_buffer(buf_T *bufp)
2943{
2944 char_u buf[64];
2945 int bufno;
2946 nbbuf_T *nbbuf;
2947
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002948 if (!NETBEANS_OPEN)
2949 return;
2950
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
2952 if (nbbuf == NULL)
2953 return;
2954
2955 nbbuf->modified = 0;
2956
Bram Moolenaar89d40322006-08-29 15:30:07 +00002957 sprintf((char *)buf, "%d:save=%d\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 nbdebug(("EVT: %s", buf));
2959 nb_send((char *)buf, "netbeans_save_buffer");
2960}
2961
2962
2963/*
2964 * Send remove command to netbeans (this command has been turned off).
2965 */
2966 void
2967netbeans_deleted_all_lines(buf_T *bufp)
2968{
2969 char_u buf[64];
2970 int bufno;
2971 nbbuf_T *nbbuf;
2972
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002973 if (!NETBEANS_OPEN)
2974 return;
2975
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
2977 if (nbbuf == NULL)
2978 return;
2979
Bram Moolenaar009b2592004-10-24 19:18:58 +00002980 /* Don't mark as modified for initial read */
2981 if (nbbuf->insertDone)
2982 nbbuf->modified = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983
Bram Moolenaar89d40322006-08-29 15:30:07 +00002984 sprintf((char *)buf, "%d:remove=%d 0 -1\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 nbdebug(("EVT(suppressed): %s", buf));
2986/* nb_send(buf, "netbeans_deleted_all_lines"); */
2987}
2988
2989
2990/*
2991 * See if the lines are guarded. The top and bot parameters are from
2992 * u_savecommon(), these are the line above the change and the line below the
2993 * change.
2994 */
2995 int
2996netbeans_is_guarded(linenr_T top, linenr_T bot)
2997{
2998 signlist_T *p;
2999 int lnum;
3000
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003001 if (!NETBEANS_OPEN)
3002 return FALSE;
3003
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 for (p = curbuf->b_signlist; p != NULL; p = p->next)
3005 if (p->id >= GUARDEDOFFSET)
3006 for (lnum = top + 1; lnum < bot; lnum++)
3007 if (lnum == p->lnum)
3008 return TRUE;
3009
3010 return FALSE;
3011}
3012
Bram Moolenaar173c9852010-09-29 17:27:01 +02003013#if defined(FEAT_GUI_X11) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014/*
3015 * We have multiple signs to draw at the same location. Draw the
3016 * multi-sign indicator instead. This is the Motif version.
3017 */
3018 void
3019netbeans_draw_multisign_indicator(int row)
3020{
3021 int i;
3022 int y;
3023 int x;
3024
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003025 if (!NETBEANS_OPEN)
3026 return;
3027
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 x = 0;
3029 y = row * gui.char_height + 2;
3030
3031 for (i = 0; i < gui.char_height - 3; i++)
3032 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y++);
3033
3034 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+0, y);
3035 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3036 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+4, y++);
3037 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+1, y);
3038 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3039 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+3, y++);
3040 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3041}
Bram Moolenaar173c9852010-09-29 17:27:01 +02003042#endif /* FEAT_GUI_X11 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043
Bram Moolenaar64404472010-06-26 06:24:45 +02003044#if defined(FEAT_GUI_GTK) && !defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045/*
3046 * We have multiple signs to draw at the same location. Draw the
3047 * multi-sign indicator instead. This is the GTK/Gnome version.
3048 */
3049 void
3050netbeans_draw_multisign_indicator(int row)
3051{
3052 int i;
3053 int y;
3054 int x;
Bram Moolenaar98921892016-02-23 17:14:37 +01003055#if GTK_CHECK_VERSION(3,0,0)
3056 cairo_t *cr = NULL;
3057#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 GdkDrawable *drawable = gui.drawarea->window;
Bram Moolenaar98921892016-02-23 17:14:37 +01003059#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003061 if (!NETBEANS_OPEN)
3062 return;
3063
Bram Moolenaar98921892016-02-23 17:14:37 +01003064#if GTK_CHECK_VERSION(3,0,0)
3065 cr = cairo_create(gui.surface);
Bram Moolenaar36edf062016-07-21 22:10:12 +02003066 cairo_set_source_rgba(cr,
3067 gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue,
3068 gui.fgcolor->alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01003069#endif
3070
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 x = 0;
3072 y = row * gui.char_height + 2;
3073
3074 for (i = 0; i < gui.char_height - 3; i++)
Bram Moolenaar98921892016-02-23 17:14:37 +01003075#if GTK_CHECK_VERSION(3,0,0)
3076 cairo_rectangle(cr, x+2, y++, 1, 1);
3077#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 gdk_draw_point(drawable, gui.text_gc, x+2, y++);
Bram Moolenaar98921892016-02-23 17:14:37 +01003079#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080
Bram Moolenaar98921892016-02-23 17:14:37 +01003081#if GTK_CHECK_VERSION(3,0,0)
3082 cairo_rectangle(cr, x+0, y, 1, 1);
3083 cairo_rectangle(cr, x+2, y, 1, 1);
3084 cairo_rectangle(cr, x+4, y++, 1, 1);
3085 cairo_rectangle(cr, x+1, y, 1, 1);
3086 cairo_rectangle(cr, x+2, y, 1, 1);
3087 cairo_rectangle(cr, x+3, y++, 1, 1);
3088 cairo_rectangle(cr, x+2, y, 1, 1);
3089#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 gdk_draw_point(drawable, gui.text_gc, x+0, y);
3091 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3092 gdk_draw_point(drawable, gui.text_gc, x+4, y++);
3093 gdk_draw_point(drawable, gui.text_gc, x+1, y);
3094 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3095 gdk_draw_point(drawable, gui.text_gc, x+3, y++);
3096 gdk_draw_point(drawable, gui.text_gc, x+2, y);
Bram Moolenaar98921892016-02-23 17:14:37 +01003097#endif
3098
3099#if GTK_CHECK_VERSION(3,0,0)
3100 cairo_destroy(cr);
3101#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102}
3103#endif /* FEAT_GUI_GTK */
3104
3105/*
3106 * If the mouse is clicked in the gutter of a line with multiple
3107 * annotations, cycle through the set of signs.
3108 */
3109 void
3110netbeans_gutter_click(linenr_T lnum)
3111{
3112 signlist_T *p;
3113
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003114 if (!NETBEANS_OPEN)
3115 return;
3116
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 for (p = curbuf->b_signlist; p != NULL; p = p->next)
3118 {
3119 if (p->lnum == lnum && p->next && p->next->lnum == lnum)
3120 {
3121 signlist_T *tail;
3122
3123 /* remove "p" from list, reinsert it at the tail of the sublist */
3124 if (p->prev)
3125 p->prev->next = p->next;
3126 else
3127 curbuf->b_signlist = p->next;
3128 p->next->prev = p->prev;
3129 /* now find end of sublist and insert p */
3130 for (tail = p->next;
3131 tail->next && tail->next->lnum == lnum
3132 && tail->next->id < GUARDEDOFFSET;
3133 tail = tail->next)
3134 ;
3135 /* tail now points to last entry with same lnum (except
3136 * that "guarded" annotations are always last) */
3137 p->next = tail->next;
3138 if (tail->next)
3139 tail->next->prev = p;
3140 p->prev = tail;
3141 tail->next = p;
3142 update_debug_sign(curbuf, lnum);
3143 break;
3144 }
3145 }
3146}
3147
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148/*
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003149 * Add a sign of the requested type at the requested location.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 *
3151 * Reverse engineering:
3152 * Apparently an annotation is defined the first time it is used in a buffer.
3153 * When the same annotation is used in two buffers, the second time we do not
3154 * need to define a new sign name but reuse the existing one. But since the
3155 * ID number used in the second buffer starts counting at one again, a mapping
3156 * is made from the ID specifically for the buffer to the global sign name
3157 * (which is a number).
3158 *
3159 * globalsignmap[] stores the signs that have been defined globally.
3160 * buf->signmapused[] maps buffer-local annotation IDs to an index in
3161 * globalsignmap[].
3162 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 static void
3164addsigntype(
3165 nbbuf_T *buf,
3166 int typeNum,
3167 char_u *typeName,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003168 char_u *tooltip UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 char_u *glyphFile,
Bram Moolenaar67c53842010-05-22 18:28:27 +02003170 char_u *fg,
3171 char_u *bg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 int i, j;
Bram Moolenaar67c53842010-05-22 18:28:27 +02003174 int use_fg = (*fg && STRCMP(fg, "none") != 0);
3175 int use_bg = (*bg && STRCMP(bg, "none") != 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176
3177 for (i = 0; i < globalsignmapused; i++)
3178 if (STRCMP(typeName, globalsignmap[i]) == 0)
3179 break;
3180
3181 if (i == globalsignmapused) /* not found; add it to global map */
3182 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003183 nbdebug(("DEFINEANNOTYPE(%d,%s,%s,%s,%s,%s)\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 typeNum, typeName, tooltip, glyphFile, fg, bg));
3185 if (use_fg || use_bg)
3186 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003187 char fgbuf[2 * (8 + MAX_COLOR_LENGTH) + 1];
3188 char bgbuf[2 * (8 + MAX_COLOR_LENGTH) + 1];
3189 char *ptr;
3190 int value;
3191
3192 value = strtol((char *)fg, &ptr, 10);
3193 if (ptr != (char *)fg)
3194 sprintf(fgbuf, "guifg=#%06x", value & 0xFFFFFF);
3195 else
3196 sprintf(fgbuf, "guifg=%s ctermfg=%s", fg, fg);
3197
3198 value = strtol((char *)bg, &ptr, 10);
3199 if (ptr != (char *)bg)
3200 sprintf(bgbuf, "guibg=#%06x", value & 0xFFFFFF);
3201 else
3202 sprintf(bgbuf, "guibg=%s ctermbg=%s", bg, bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203
3204 coloncmd(":highlight NB_%s %s %s", typeName, (use_fg) ? fgbuf : "",
3205 (use_bg) ? bgbuf : "");
3206 if (*glyphFile == NUL)
3207 /* no glyph, line highlighting only */
3208 coloncmd(":sign define %d linehl=NB_%s", i + 1, typeName);
3209 else if (vim_strsize(glyphFile) <= 2)
3210 /* one- or two-character glyph name, use as text glyph with
3211 * texthl */
3212 coloncmd(":sign define %d text=%s texthl=NB_%s", i + 1,
3213 glyphFile, typeName);
3214 else
3215 /* glyph, line highlighting */
3216 coloncmd(":sign define %d icon=%s linehl=NB_%s", i + 1,
3217 glyphFile, typeName);
3218 }
3219 else
3220 /* glyph, no line highlighting */
3221 coloncmd(":sign define %d icon=%s", i + 1, glyphFile);
3222
3223 if (STRCMP(typeName,"CurrentPC") == 0)
3224 curPCtype = typeNum;
3225
3226 if (globalsignmapused == globalsignmaplen)
3227 {
3228 if (globalsignmaplen == 0) /* first allocation */
3229 {
3230 globalsignmaplen = 20;
3231 globalsignmap = (char **)alloc_clear(globalsignmaplen*sizeof(char *));
3232 }
3233 else /* grow it */
3234 {
3235 int incr;
3236 int oldlen = globalsignmaplen;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003237 char **t_globalsignmap = globalsignmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238
3239 globalsignmaplen *= 2;
3240 incr = globalsignmaplen - oldlen;
3241 globalsignmap = (char **)vim_realloc(globalsignmap,
3242 globalsignmaplen * sizeof(char *));
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003243 if (globalsignmap == NULL)
3244 {
3245 vim_free(t_globalsignmap);
3246 globalsignmaplen = 0;
3247 return;
3248 }
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003249 vim_memset(globalsignmap + oldlen, 0, incr * sizeof(char *));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 }
3251 }
3252
3253 globalsignmap[i] = (char *)typeName;
3254 globalsignmapused = i + 1;
3255 }
3256
3257 /* check local map; should *not* be found! */
3258 for (j = 0; j < buf->signmapused; j++)
3259 if (buf->signmap[j] == i + 1)
3260 return;
3261
3262 /* add to local map */
3263 if (buf->signmapused == buf->signmaplen)
3264 {
3265 if (buf->signmaplen == 0) /* first allocation */
3266 {
3267 buf->signmaplen = 5;
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003268 buf->signmap = (int *)alloc_clear(buf->signmaplen * sizeof(int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 }
3270 else /* grow it */
3271 {
3272 int incr;
3273 int oldlen = buf->signmaplen;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003274 int *t_signmap = buf->signmap;
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003275
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 buf->signmaplen *= 2;
3277 incr = buf->signmaplen - oldlen;
3278 buf->signmap = (int *)vim_realloc(buf->signmap,
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003279 buf->signmaplen * sizeof(int));
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003280 if (buf->signmap == NULL)
3281 {
3282 vim_free(t_signmap);
3283 buf->signmaplen = 0;
3284 return;
3285 }
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003286 vim_memset(buf->signmap + oldlen, 0, incr * sizeof(int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 }
3288 }
3289
3290 buf->signmap[buf->signmapused++] = i + 1;
3291
3292}
3293
3294
3295/*
3296 * See if we have the requested sign type in the buffer.
3297 */
3298 static int
3299mapsigntype(nbbuf_T *buf, int localsigntype)
3300{
3301 if (--localsigntype >= 0 && localsigntype < buf->signmapused)
3302 return buf->signmap[localsigntype];
3303
3304 return 0;
3305}
3306
3307
3308/*
3309 * Compute length of buffer, don't print anything.
3310 */
3311 static long
3312get_buf_size(buf_T *bufp)
3313{
3314 linenr_T lnum;
3315 long char_count = 0;
3316 int eol_size;
3317 long last_check = 100000L;
3318
3319 if (bufp->b_ml.ml_flags & ML_EMPTY)
3320 return 0;
3321 else
3322 {
3323 if (get_fileformat(bufp) == EOL_DOS)
3324 eol_size = 2;
3325 else
3326 eol_size = 1;
3327 for (lnum = 1; lnum <= bufp->b_ml.ml_line_count; ++lnum)
3328 {
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00003329 char_count += (long)STRLEN(ml_get_buf(bufp, lnum, FALSE))
3330 + eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 /* Check for a CTRL-C every 100000 characters */
3332 if (char_count > last_check)
3333 {
3334 ui_breakcheck();
3335 if (got_int)
3336 return char_count;
3337 last_check = char_count + 100000L;
3338 }
3339 }
3340 /* Correction for when last line doesn't have an EOL. */
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003341 if (!bufp->b_p_eol && (bufp->b_p_bin || !bufp->b_p_fixeol))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 char_count -= eol_size;
3343 }
3344
3345 return char_count;
3346}
3347
3348/*
3349 * Convert character offset to lnum,col
3350 */
3351 static pos_T *
3352off2pos(buf_T *buf, long offset)
3353{
3354 linenr_T lnum;
3355 static pos_T pos;
3356
3357 pos.lnum = 0;
3358 pos.col = 0;
3359#ifdef FEAT_VIRTUALEDIT
3360 pos.coladd = 0;
3361#endif
3362
3363 if (!(buf->b_ml.ml_flags & ML_EMPTY))
3364 {
3365 if ((lnum = ml_find_line_or_offset(buf, (linenr_T)0, &offset)) < 0)
3366 return NULL;
3367 pos.lnum = lnum;
3368 pos.col = offset;
3369 }
3370
3371 return &pos;
3372}
3373
3374/*
3375 * Convert an argument in the form "1234" to an offset and compute the
3376 * lnum/col from it. Convert an argument in the form "123/12" directly to a
3377 * lnum/col.
3378 * "argp" is advanced to after the argument.
3379 * Return a pointer to the position, NULL if something is wrong.
3380 */
3381 static pos_T *
3382get_off_or_lnum(buf_T *buf, char_u **argp)
3383{
3384 static pos_T mypos;
3385 long off;
3386
3387 off = strtol((char *)*argp, (char **)argp, 10);
3388 if (**argp == '/')
3389 {
3390 mypos.lnum = (linenr_T)off;
3391 ++*argp;
3392 mypos.col = strtol((char *)*argp, (char **)argp, 10);
3393#ifdef FEAT_VIRTUALEDIT
3394 mypos.coladd = 0;
3395#endif
3396 return &mypos;
3397 }
3398 return off2pos(buf, off);
3399}
3400
3401
3402/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003403 * Convert (lnum,col) to byte offset in the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 */
3405 static long
3406pos2off(buf_T *buf, pos_T *pos)
3407{
3408 long offset = 0;
3409
3410 if (!(buf->b_ml.ml_flags & ML_EMPTY))
3411 {
3412 if ((offset = ml_find_line_or_offset(buf, pos->lnum, 0)) < 0)
3413 return 0;
3414 offset += pos->col;
3415 }
3416
3417 return offset;
3418}
3419
3420
Bram Moolenaar009b2592004-10-24 19:18:58 +00003421/*
Bram Moolenaar9af41842016-09-25 21:45:05 +02003422 * This message is printed after NetBeans opens a new file. It's
Bram Moolenaar009b2592004-10-24 19:18:58 +00003423 * similar to the message readfile() uses, but since NetBeans
3424 * doesn't normally call readfile, we do our own.
3425 */
3426 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003427print_read_msg(nbbuf_T *buf)
Bram Moolenaar009b2592004-10-24 19:18:58 +00003428{
3429 int lnum = buf->bufp->b_ml.ml_line_count;
Bram Moolenaar8767f522016-07-01 17:17:39 +02003430 off_T nchars = buf->bufp->b_orig_size;
Bram Moolenaar009b2592004-10-24 19:18:58 +00003431 char_u c;
3432
3433 msg_add_fname(buf->bufp, buf->bufp->b_ffname);
3434 c = FALSE;
3435
3436 if (buf->bufp->b_p_ro)
3437 {
3438 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]"));
3439 c = TRUE;
3440 }
3441 if (!buf->bufp->b_start_eol)
3442 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003443 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]")
3444 : _("[Incomplete last line]"));
Bram Moolenaar009b2592004-10-24 19:18:58 +00003445 c = TRUE;
3446 }
3447 msg_add_lines(c, (long)lnum, nchars);
3448
3449 /* Now display it */
3450 vim_free(keep_msg);
3451 keep_msg = NULL;
3452 msg_scrolled_ign = TRUE;
3453 msg_trunc_attr(IObuff, FALSE, 0);
3454 msg_scrolled_ign = FALSE;
3455}
3456
3457
3458/*
Bram Moolenaar67c53842010-05-22 18:28:27 +02003459 * Print a message after NetBeans writes the file. This message should be
3460 * identical to the standard message a non-netbeans user would see when
3461 * writing a file.
Bram Moolenaar009b2592004-10-24 19:18:58 +00003462 */
3463 static void
Bram Moolenaar8767f522016-07-01 17:17:39 +02003464print_save_msg(nbbuf_T *buf, off_T nchars)
Bram Moolenaar009b2592004-10-24 19:18:58 +00003465{
3466 char_u c;
3467 char_u *p;
3468
3469 if (nchars >= 0)
3470 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003471 /* put fname in IObuff with quotes */
3472 msg_add_fname(buf->bufp, buf->bufp->b_ffname);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003473 c = FALSE;
3474
3475 msg_add_lines(c, buf->bufp->b_ml.ml_line_count,
Bram Moolenaar914703b2010-05-31 21:59:46 +02003476 buf->bufp->b_orig_size);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003477
3478 vim_free(keep_msg);
3479 keep_msg = NULL;
3480 msg_scrolled_ign = TRUE;
3481 p = msg_trunc_attr(IObuff, FALSE, 0);
3482 if ((msg_scrolled && !need_wait_return) || !buf->initDone)
3483 {
3484 /* Need to repeat the message after redrawing when:
3485 * - When reading from stdin (the screen will be cleared next).
3486 * - When restart_edit is set (otherwise there will be a delay
3487 * before redrawing).
3488 * - When the screen was scrolled but there is no wait-return
3489 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003490 set_keep_msg(p, 0);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003491 }
3492 msg_scrolled_ign = FALSE;
3493 /* add_to_input_buf((char_u *)"\f", 1); */
3494 }
3495 else
3496 {
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003497 char_u msgbuf[IOSIZE];
Bram Moolenaar009b2592004-10-24 19:18:58 +00003498
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003499 vim_snprintf((char *)msgbuf, IOSIZE,
3500 _("E505: %s is read-only (add ! to override)"), IObuff);
3501 nbdebug((" %s\n", msgbuf));
3502 emsg(msgbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003503 }
3504}
3505
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506#endif /* defined(FEAT_NETBEANS_INTG) */