blob: 3eaf2a2b81629e9efaf614f7b4abaf832d7edd0b [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * Netbeans integration by David Weatherford
5 * Adopted for Win32 by Sergey Khorev
6 *
7 * Do ":help uganda" in Vim to read copying and usage conditions.
8 * Do ":help credits" in Vim to see a list of people who contributed.
9 */
10
11/*
12 * Implements client side of org.netbeans.modules.emacs editor
13 * integration protocol. Be careful! The protocol uses offsets
14 * which are *between* characters, whereas vim uses line number
15 * and column number which are *on* characters.
16 * See ":help netbeans-protocol" for explanation.
Bram Moolenaare3cc6d42011-10-20 21:58:34 +020017 *
18 * The Netbeans messages are received and queued in the gui event loop, or in
19 * the select loop when Vim runs in a terminal. These messages are processed
20 * by netbeans_parse_messages() which is invoked in the idle loop when Vim is
21 * waiting for user input. The function netbeans_parse_messages() is also
22 * called from the ":sleep" command, to allow the execution of test cases that
23 * may not invoke the idle loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024 */
25
26#include "vim.h"
27
28#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
29
Bram Moolenaard04a0202016-01-26 23:30:18 +010030#ifndef WIN32
Bram Moolenaar3e53c702016-01-24 22:17:03 +010031# include <netdb.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032# ifdef HAVE_LIBGEN_H
33# include <libgen.h>
34# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000035#endif
36
37#include "version.h"
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039#define GUARDED 10000 /* typenr for "guarded" annotation */
40#define GUARDEDOFFSET 1000000 /* base for "guarded" sign id's */
Bram Moolenaar67c53842010-05-22 18:28:27 +020041#define MAX_COLOR_LENGTH 32 /* max length of color name in defineAnnoType */
Bram Moolenaar071d4272004-06-13 20:20:40 +000042
43/* The first implementation (working only with Netbeans) returned "1.1". The
44 * protocol implemented here also supports A-A-P. */
Bram Moolenaar67c53842010-05-22 18:28:27 +020045static char *ExtEdProtocolVersion = "2.5";
Bram Moolenaar071d4272004-06-13 20:20:40 +000046
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 Moolenaar67c53842010-05-22 18:28:27 +0200110#ifdef FEAT_BEVAL
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 Moolenaarb26e6322010-05-22 21:34:09 +0200124 out_flush();
125#ifdef FEAT_GUI
Bram Moolenaard54a6882010-08-09 22:49:00 +0200126 if (gui.in_use)
127 {
128 gui_update_cursor(TRUE, FALSE);
129 gui_mch_flush();
130 }
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133
134#define NB_DEF_HOST "localhost"
135#define NB_DEF_ADDR "3219"
136#define NB_DEF_PASS "changeme"
137
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200138 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200139netbeans_connect(char *params, int doabort)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140{
Bram Moolenaard04a0202016-01-26 23:30:18 +0100141 int port;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142 char buf[32];
143 char *hostname = NULL;
144 char *address = NULL;
145 char *password = NULL;
146 char *fname;
147 char *arg = NULL;
148
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200149 if (*params == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200151 /* "=fname": Read info from specified file. */
Bram Moolenaare0874f82016-01-24 20:36:41 +0100152 if (getConnInfo(params + 1, &hostname, &address, &password) == FAIL)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200153 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154 }
155 else
156 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200157 if (*params == ':')
158 /* ":<host>:<addr>:<password>": get info from argument */
159 arg = params + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160 if (arg == NULL && (fname = getenv("__NETBEANS_CONINFO")) != NULL)
161 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200162 /* "": get info from file specified in environment */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163 if (getConnInfo(fname, &hostname, &address, &password) == FAIL)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200164 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165 }
166 else
167 {
168 if (arg != NULL)
169 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200170 /* ":<host>:<addr>:<password>": get info from argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171 hostname = arg;
172 address = strchr(hostname, ':');
173 if (address != NULL)
174 {
175 *address++ = '\0';
176 password = strchr(address, ':');
177 if (password != NULL)
178 *password++ = '\0';
179 }
180 }
181
182 /* Get the missing values from the environment. */
183 if (hostname == NULL || *hostname == '\0')
184 hostname = getenv("__NETBEANS_HOST");
185 if (address == NULL)
186 address = getenv("__NETBEANS_SOCKET");
187 if (password == NULL)
188 password = getenv("__NETBEANS_VIM_PASSWORD");
189
190 /* Move values to allocated memory. */
191 if (hostname != NULL)
192 hostname = (char *)vim_strsave((char_u *)hostname);
193 if (address != NULL)
194 address = (char *)vim_strsave((char_u *)address);
195 if (password != NULL)
196 password = (char *)vim_strsave((char_u *)password);
197 }
198 }
199
200 /* Use the default when a value is missing. */
201 if (hostname == NULL || *hostname == '\0')
202 {
203 vim_free(hostname);
204 hostname = (char *)vim_strsave((char_u *)NB_DEF_HOST);
205 }
206 if (address == NULL || *address == '\0')
207 {
208 vim_free(address);
209 address = (char *)vim_strsave((char_u *)NB_DEF_ADDR);
210 }
211 if (password == NULL || *password == '\0')
212 {
213 vim_free(password);
214 password = (char *)vim_strsave((char_u *)NB_DEF_PASS);
215 }
Bram Moolenaard04a0202016-01-26 23:30:18 +0100216 if (hostname != NULL && address != NULL && password != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217 {
Bram Moolenaard04a0202016-01-26 23:30:18 +0100218 port = atoi(address);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100219 nb_channel = channel_open(hostname, port, 3000, nb_channel_closed);
Bram Moolenaar77073442016-02-13 23:23:53 +0100220 if (nb_channel != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221 {
Bram Moolenaard04a0202016-01-26 23:30:18 +0100222 /* success */
223# ifdef FEAT_BEVAL
224 bevalServers |= BEVAL_NETBEANS;
225# endif
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200226
Bram Moolenaard04a0202016-01-26 23:30:18 +0100227 /* success, login */
228 vim_snprintf(buf, sizeof(buf), "AUTH %s\n", password);
229 nb_send(buf, "netbeans_connect");
230
231 sprintf(buf, "0:version=0 \"%s\"\n", ExtEdProtocolVersion);
232 nb_send(buf, "externaleditor_version");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 }
234 }
235
Bram Moolenaar77073442016-02-13 23:23:53 +0100236 if (nb_channel == NULL && doabort)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100237 getout(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 vim_free(hostname);
240 vim_free(address);
241 vim_free(password);
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200242 return NETBEANS_OPEN ? OK : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243}
244
245/*
246 * Obtain the NetBeans hostname, port address and password from a file.
247 * Return the strings in allocated memory.
248 * Return FAIL if the file could not be read, OK otherwise (no matter what it
249 * contains).
250 */
251 static int
252getConnInfo(char *file, char **host, char **port, char **auth)
253{
254 FILE *fp;
255 char_u buf[BUFSIZ];
256 char_u *lp;
Bram Moolenaar309cbc32012-01-10 22:31:31 +0100257 char_u *nlp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258#ifdef UNIX
259 struct stat st;
260
261 /*
262 * For Unix only accept the file when it's not accessible by others.
263 * The open will then fail if we don't own the file.
264 */
265 if (mch_stat(file, &st) == 0 && (st.st_mode & 0077) != 0)
266 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000267 nbdebug(("Wrong access mode for NetBeans connection info file: \"%s\"\n",
268 file));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 EMSG2(_("E668: Wrong access mode for NetBeans connection info file: \"%s\""),
270 file);
271 return FAIL;
272 }
273#endif
274
275 fp = mch_fopen(file, "r");
276 if (fp == NULL)
277 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000278 nbdebug(("Cannot open NetBeans connection info file\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279 PERROR("E660: Cannot open NetBeans connection info file");
280 return FAIL;
281 }
282
283 /* Read the file. There should be one of each parameter */
284 while ((lp = (char_u *)fgets((char *)buf, BUFSIZ, fp)) != NULL)
285 {
Bram Moolenaar309cbc32012-01-10 22:31:31 +0100286 if ((nlp = vim_strchr(lp, '\n')) != NULL)
287 *nlp = 0; /* strip off the trailing newline */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288
289 if (STRNCMP(lp, "host=", 5) == 0)
290 {
291 vim_free(*host);
292 *host = (char *)vim_strsave(&buf[5]);
293 }
294 else if (STRNCMP(lp, "port=", 5) == 0)
295 {
296 vim_free(*port);
297 *port = (char *)vim_strsave(&buf[5]);
298 }
299 else if (STRNCMP(lp, "auth=", 5) == 0)
300 {
301 vim_free(*auth);
302 *auth = (char *)vim_strsave(&buf[5]);
303 }
304 }
305 fclose(fp);
306
307 return OK;
308}
309
310
311struct keyqueue
312{
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100313 char_u *keystr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314 struct keyqueue *next;
315 struct keyqueue *prev;
316};
317
318typedef struct keyqueue keyQ_T;
319
320static keyQ_T keyHead; /* dummy node, header for circular queue */
321
322
323/*
324 * Queue up key commands sent from netbeans.
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100325 * We store the string, because it may depend on the global mod_mask and
326 * :nbkey doesn't have a key number.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327 */
328 static void
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100329postpone_keycommand(char_u *keystr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330{
331 keyQ_T *node;
332
333 node = (keyQ_T *)alloc(sizeof(keyQ_T));
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100334 if (node == NULL)
335 return; /* out of memory, drop the key */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336
337 if (keyHead.next == NULL) /* initialize circular queue */
338 {
339 keyHead.next = &keyHead;
340 keyHead.prev = &keyHead;
341 }
342
343 /* insert node at tail of queue */
344 node->next = &keyHead;
345 node->prev = keyHead.prev;
346 keyHead.prev->next = node;
347 keyHead.prev = node;
348
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100349 node->keystr = vim_strsave(keystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350}
351
352/*
353 * Handle any queued-up NetBeans keycommands to be send.
354 */
355 static void
356handle_key_queue(void)
357{
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100358 int postponed = FALSE;
359
360 while (!postponed && keyHead.next && keyHead.next != &keyHead)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 {
362 /* first, unlink the node */
363 keyQ_T *node = keyHead.next;
364 keyHead.next = node->next;
365 node->next->prev = node->prev;
366
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100367 /* Now, send the keycommand. This may cause it to be postponed again
368 * and change keyHead. */
369 if (node->keystr != NULL)
370 postponed = !netbeans_keystring(node->keystr);
371 vim_free(node->keystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372
373 /* Finally, dispose of the node */
374 vim_free(node);
375 }
376}
377
378
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379/*
380 * While there's still a command in the work queue, parse and execute it.
381 */
Bram Moolenaarf2330482008-06-24 20:19:36 +0000382 void
383netbeans_parse_messages(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384{
Bram Moolenaard04a0202016-01-26 23:30:18 +0100385 char_u *buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 char_u *p;
Bram Moolenaar863053d2010-12-02 17:09:54 +0100387 int own_node;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388
Bram Moolenaar77073442016-02-13 23:23:53 +0100389 while (nb_channel != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100391 buffer = channel_peek(nb_channel, PART_SOCK);
Bram Moolenaard04a0202016-01-26 23:30:18 +0100392 if (buffer == NULL)
393 break; /* nothing to read */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394
395 /* Locate the first line in the first buffer. */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100396 p = vim_strchr(buffer, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397 if (p == NULL)
398 {
399 /* Command isn't complete. If there is no following buffer,
400 * return (wait for more). If there is another buffer following,
401 * prepend the text to that buffer and delete this one. */
Bram Moolenaar9ed96ef2016-06-04 17:17:11 +0200402 if (channel_collapse(nb_channel, PART_SOCK, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404 }
405 else
406 {
407 /* There is a complete command at the start of the buffer.
408 * Terminate it with a NUL. When no more text is following unlink
409 * the buffer. Do this before executing, because new buffers can
410 * be added while busy handling the command. */
411 *p++ = NUL;
412 if (*p == NUL)
413 {
Bram Moolenaar863053d2010-12-02 17:09:54 +0100414 own_node = TRUE;
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100415 channel_get(nb_channel, PART_SOCK);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 }
Bram Moolenaar863053d2010-12-02 17:09:54 +0100417 else
418 own_node = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419
420 /* now, parse and execute the commands */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100421 nb_parse_cmd(buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422
Bram Moolenaar863053d2010-12-02 17:09:54 +0100423 if (own_node)
Bram Moolenaard04a0202016-01-26 23:30:18 +0100424 /* buffer finished, dispose of it */
425 vim_free(buffer);
426 else
427 /* more follows, move it to the start */
428 STRMOVE(buffer, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429 }
430 }
431}
432
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433/*
434 * Handle one NUL terminated command.
435 *
436 * format of a command from netbeans:
437 *
438 * 6:setTitle!84 "a.c"
439 *
440 * bufno
441 * colon
442 * cmd
443 * !
444 * cmdno
445 * args
446 *
447 * for function calls, the ! is replaced by a /
448 */
449 static void
450nb_parse_cmd(char_u *cmd)
451{
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000452 char *verb;
453 char *q;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454 int bufno;
455 int isfunc = -1;
456
457 if (STRCMP(cmd, "DISCONNECT") == 0)
458 {
459 /* We assume the server knows that we can safely exit! */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460 /* Disconnect before exiting, Motif hangs in a Select error
461 * message otherwise. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200462 netbeans_close();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463 getout(0);
464 /* NOTREACHED */
465 }
466
Bram Moolenaareed284a2016-02-22 23:13:33 +0100467 if (STRCMP(cmd, "DETACH") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468 {
Bram Moolenaard04a0202016-01-26 23:30:18 +0100469 buf_T *buf;
470
471 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
472 buf->b_has_sign_column = FALSE;
473
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 /* The IDE is breaking the connection. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200475 netbeans_close();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476 return;
477 }
478
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000479 bufno = strtol((char *)cmd, &verb, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480
481 if (*verb != ':')
482 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000483 nbdebug((" missing colon: %s\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 EMSG2("E627: missing colon: %s", cmd);
485 return;
486 }
487 ++verb; /* skip colon */
488
489 for (q = verb; *q; q++)
490 {
491 if (*q == '!')
492 {
493 *q++ = NUL;
494 isfunc = 0;
495 break;
496 }
497 else if (*q == '/')
498 {
499 *q++ = NUL;
500 isfunc = 1;
501 break;
502 }
503 }
504
505 if (isfunc < 0)
506 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000507 nbdebug((" missing ! or / in: %s\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508 EMSG2("E628: missing ! or / in: %s", cmd);
509 return;
510 }
511
Bram Moolenaar89d40322006-08-29 15:30:07 +0000512 r_cmdno = strtol(q, &q, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000514 q = (char *)skipwhite((char_u *)q);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515
Bram Moolenaar89d40322006-08-29 15:30:07 +0000516 if (nb_do_cmd(bufno, (char_u *)verb, isfunc, r_cmdno, (char_u *)q) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 {
Bram Moolenaar009b2592004-10-24 19:18:58 +0000518#ifdef NBDEBUG
519 /*
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100520 * This happens because the ExtEd can send a command or 2 after
Bram Moolenaar009b2592004-10-24 19:18:58 +0000521 * doing a stopDocumentListen command. It doesn't harm anything
522 * so I'm disabling it except for debugging.
523 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 nbdebug(("nb_parse_cmd: Command error for \"%s\"\n", cmd));
525 EMSG("E629: bad return from nb_do_cmd");
Bram Moolenaar009b2592004-10-24 19:18:58 +0000526#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 }
528}
529
530struct nbbuf_struct
531{
532 buf_T *bufp;
533 unsigned int fireChanges:1;
534 unsigned int initDone:1;
Bram Moolenaar009b2592004-10-24 19:18:58 +0000535 unsigned int insertDone:1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 unsigned int modified:1;
Bram Moolenaar009b2592004-10-24 19:18:58 +0000537 int nbbuf_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 char *displayname;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 int *signmap;
540 short_u signmaplen;
541 short_u signmapused;
542};
543
544typedef struct nbbuf_struct nbbuf_T;
545
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200546static nbbuf_T *buf_list = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000547static int buf_list_size = 0; /* size of buf_list */
548static int buf_list_used = 0; /* nr of entries in buf_list actually in use */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200550static char **globalsignmap = NULL;
551static int globalsignmaplen = 0;
552static int globalsignmapused = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100554static int mapsigntype(nbbuf_T *, int localsigntype);
555static void addsigntype(nbbuf_T *, int localsigntype, char_u *typeName,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 char_u *tooltip, char_u *glyphfile,
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100557 char_u *fg, char_u *bg);
558static void print_read_msg(nbbuf_T *buf);
559static void print_save_msg(nbbuf_T *buf, off_t nchars);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560
561static int curPCtype = -1;
562
563/*
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200564 * Free netbeans resources.
565 */
566 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100567nb_free(void)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200568{
569 keyQ_T *key_node = keyHead.next;
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200570 nbbuf_T buf;
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200571 int i;
572
573 /* free the netbeans buffer list */
574 for (i = 0; i < buf_list_used; i++)
575 {
576 buf = buf_list[i];
577 vim_free(buf.displayname);
578 vim_free(buf.signmap);
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100579 if (buf.bufp != NULL)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200580 {
581 buf.bufp->b_netbeans_file = FALSE;
582 buf.bufp->b_was_netbeans_file = FALSE;
583 }
584 }
585 vim_free(buf_list);
586 buf_list = NULL;
587 buf_list_size = 0;
588 buf_list_used = 0;
589
590 /* free the queued key commands */
Bram Moolenaar8d4eecc2012-11-20 17:19:01 +0100591 while (key_node != NULL && key_node != &keyHead)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200592 {
593 keyQ_T *next = key_node->next;
594 vim_free(key_node->keystr);
595 vim_free(key_node);
596 if (next == &keyHead)
597 {
598 keyHead.next = &keyHead;
599 keyHead.prev = &keyHead;
600 break;
601 }
602 key_node = next;
603 }
604
605 /* free the queued netbeans commands */
Bram Moolenaar77073442016-02-13 23:23:53 +0100606 if (nb_channel != NULL)
607 channel_clear(nb_channel);
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200608}
609
610/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 * Get the Netbeans buffer number for the specified buffer.
612 */
613 static int
614nb_getbufno(buf_T *bufp)
615{
616 int i;
617
618 for (i = 0; i < buf_list_used; i++)
619 if (buf_list[i].bufp == bufp)
620 return i;
621 return -1;
622}
623
624/*
625 * Is this a NetBeans-owned buffer?
626 */
627 int
628isNetbeansBuffer(buf_T *bufp)
629{
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200630 return NETBEANS_OPEN && bufp->b_netbeans_file;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631}
632
633/*
634 * NetBeans and Vim have different undo models. In Vim, the file isn't
635 * changed if changes are undone via the undo command. In NetBeans, once
636 * a change has been made the file is marked as modified until saved. It
637 * doesn't matter if the change was undone.
638 *
639 * So this function is for the corner case where Vim thinks a buffer is
640 * unmodified but NetBeans thinks it IS modified.
641 */
642 int
643isNetbeansModified(buf_T *bufp)
644{
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200645 if (isNetbeansBuffer(bufp))
Bram Moolenaar009b2592004-10-24 19:18:58 +0000646 {
647 int bufno = nb_getbufno(bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648
Bram Moolenaar009b2592004-10-24 19:18:58 +0000649 if (bufno > 0)
650 return buf_list[bufno].modified;
651 else
652 return FALSE;
653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 else
655 return FALSE;
656}
657
658/*
659 * Given a Netbeans buffer number, return the netbeans buffer.
660 * Returns NULL for 0 or a negative number. A 0 bufno means a
661 * non-buffer related command has been sent.
662 */
663 static nbbuf_T *
664nb_get_buf(int bufno)
665{
666 /* find or create a buffer with the given number */
667 int incr;
668
669 if (bufno <= 0)
670 return NULL;
671
672 if (!buf_list)
673 {
674 /* initialize */
675 buf_list = (nbbuf_T *)alloc_clear(100 * sizeof(nbbuf_T));
676 buf_list_size = 100;
677 }
678 if (bufno >= buf_list_used) /* new */
679 {
680 if (bufno >= buf_list_size) /* grow list */
681 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +0100682 nbbuf_T *t_buf_list = buf_list;
683
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 incr = bufno - buf_list_size + 90;
685 buf_list_size += incr;
686 buf_list = (nbbuf_T *)vim_realloc(
687 buf_list, buf_list_size * sizeof(nbbuf_T));
Bram Moolenaar9abd5c62015-02-10 18:34:01 +0100688 if (buf_list == NULL)
689 {
690 vim_free(t_buf_list);
691 buf_list_size = 0;
692 return NULL;
693 }
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200694 vim_memset(buf_list + buf_list_size - incr, 0,
695 incr * sizeof(nbbuf_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 }
697
698 while (buf_list_used <= bufno)
699 {
700 /* Default is to fire text changes. */
701 buf_list[buf_list_used].fireChanges = 1;
702 ++buf_list_used;
703 }
704 }
705
706 return buf_list + bufno;
707}
708
709/*
710 * Return the number of buffers that are modified.
711 */
712 static int
713count_changed_buffers(void)
714{
715 buf_T *bufp;
716 int n;
717
718 n = 0;
719 for (bufp = firstbuf; bufp != NULL; bufp = bufp->b_next)
720 if (bufp->b_changed)
721 ++n;
722 return n;
723}
724
725/*
726 * End the netbeans session.
727 */
728 void
729netbeans_end(void)
730{
731 int i;
732 static char buf[128];
733
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200734 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 return;
736
737 for (i = 0; i < buf_list_used; i++)
738 {
739 if (!buf_list[i].bufp)
740 continue;
741 if (netbeansForcedQuit)
742 {
743 /* mark as unmodified so NetBeans won't put up dialog on "killed" */
Bram Moolenaar89d40322006-08-29 15:30:07 +0000744 sprintf(buf, "%d:unmodified=%d\n", i, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 nbdebug(("EVT: %s", buf));
746 nb_send(buf, "netbeans_end");
747 }
Bram Moolenaar89d40322006-08-29 15:30:07 +0000748 sprintf(buf, "%d:killed=%d\n", i, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 nbdebug(("EVT: %s", buf));
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200750 /* nb_send(buf, "netbeans_end"); avoid "write failed" messages */
Bram Moolenaard04a0202016-01-26 23:30:18 +0100751 nb_send(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753}
754
755/*
756 * Send a message to netbeans.
Bram Moolenaard04a0202016-01-26 23:30:18 +0100757 * When "fun" is NULL no error is given.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 */
759 static void
760nb_send(char *buf, char *fun)
761{
Bram Moolenaar77073442016-02-13 23:23:53 +0100762 if (nb_channel != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +0100763 channel_send(nb_channel, PART_SOCK, (char_u *)buf, fun);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764}
765
766/*
767 * Some input received from netbeans requires a response. This function
768 * handles a response with no information (except the command number).
769 */
770 static void
771nb_reply_nil(int cmdno)
772{
773 char reply[32];
774
Bram Moolenaar009b2592004-10-24 19:18:58 +0000775 nbdebug(("REP %d: <none>\n", cmdno));
776
Bram Moolenaar7ad7d012010-11-16 15:49:02 +0100777 /* Avoid printing an annoying error message. */
778 if (!NETBEANS_OPEN)
779 return;
780
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 sprintf(reply, "%d\n", cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 nb_send(reply, "nb_reply_nil");
783}
784
785
786/*
787 * Send a response with text.
788 * "result" must have been quoted already (using nb_quote()).
789 */
790 static void
791nb_reply_text(int cmdno, char_u *result)
792{
793 char_u *reply;
794
Bram Moolenaar009b2592004-10-24 19:18:58 +0000795 nbdebug(("REP %d: %s\n", cmdno, (char *)result));
796
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000797 reply = alloc((unsigned)STRLEN(result) + 32);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 sprintf((char *)reply, "%d %s\n", cmdno, (char *)result);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 nb_send((char *)reply, "nb_reply_text");
800
801 vim_free(reply);
802}
803
804
805/*
806 * Send a response with a number result code.
807 */
808 static void
809nb_reply_nr(int cmdno, long result)
810{
811 char reply[32];
812
Bram Moolenaar009b2592004-10-24 19:18:58 +0000813 nbdebug(("REP %d: %ld\n", cmdno, result));
814
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 sprintf(reply, "%d %ld\n", cmdno, result);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 nb_send(reply, "nb_reply_nr");
817}
818
819
820/*
821 * Encode newline, ret, backslash, double quote for transmission to NetBeans.
822 */
823 static char_u *
824nb_quote(char_u *txt)
825{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000826 char_u *buf = alloc((unsigned)(2 * STRLEN(txt) + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 char_u *p = txt;
828 char_u *q = buf;
829
830 if (buf == NULL)
831 return NULL;
832 for (; *p; p++)
833 {
834 switch (*p)
835 {
836 case '\"':
837 case '\\':
838 *q++ = '\\'; *q++ = *p; break;
839 /* case '\t': */
840 /* *q++ = '\\'; *q++ = 't'; break; */
841 case '\n':
842 *q++ = '\\'; *q++ = 'n'; break;
843 case '\r':
844 *q++ = '\\'; *q++ = 'r'; break;
845 default:
846 *q++ = *p;
847 break;
848 }
849 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100850 *q = '\0';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851
852 return buf;
853}
854
855
856/*
857 * Remove top level double quotes; convert backslashed chars.
858 * Returns an allocated string (NULL for failure).
859 * If "endp" is not NULL it is set to the character after the terminating
860 * quote.
861 */
862 static char *
863nb_unquote(char_u *p, char_u **endp)
864{
865 char *result = 0;
866 char *q;
867 int done = 0;
868
869 /* result is never longer than input */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000870 result = (char *)alloc_clear((unsigned)STRLEN(p) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 if (result == NULL)
872 return NULL;
873
874 if (*p++ != '"')
875 {
876 nbdebug(("nb_unquote called with string that doesn't start with a quote!: %s\n",
877 p));
878 result[0] = NUL;
879 return result;
880 }
881
882 for (q = result; !done && *p != NUL;)
883 {
884 switch (*p)
885 {
886 case '"':
887 /*
888 * Unbackslashed dquote marks the end, if first char was dquote.
889 */
890 done = 1;
891 break;
892
893 case '\\':
894 ++p;
895 switch (*p)
896 {
897 case '\\': *q++ = '\\'; break;
898 case 'n': *q++ = '\n'; break;
899 case 't': *q++ = '\t'; break;
900 case 'r': *q++ = '\r'; break;
901 case '"': *q++ = '"'; break;
902 case NUL: --p; break;
903 /* default: skip over illegal chars */
904 }
905 ++p;
906 break;
907
908 default:
909 *q++ = *p++;
910 }
911 }
912
913 if (endp != NULL)
914 *endp = p;
915
916 return result;
917}
918
Bram Moolenaar5eaf8722008-01-05 17:07:13 +0000919/*
920 * Remove from "first" byte to "last" byte (inclusive), at line "lnum" of the
921 * current buffer. Remove to end of line when "last" is MAXCOL.
922 */
923 static void
924nb_partialremove(linenr_T lnum, colnr_T first, colnr_T last)
925{
926 char_u *oldtext, *newtext;
927 int oldlen;
928 int lastbyte = last;
929
930 oldtext = ml_get(lnum);
Bram Moolenaarcb4cef22008-03-16 15:04:34 +0000931 oldlen = (int)STRLEN(oldtext);
Bram Moolenaarb3c70982008-01-18 10:40:55 +0000932 if (first >= (colnr_T)oldlen || oldlen == 0) /* just in case */
Bram Moolenaar5eaf8722008-01-05 17:07:13 +0000933 return;
934 if (lastbyte >= oldlen)
935 lastbyte = oldlen - 1;
936 newtext = alloc(oldlen - (int)(lastbyte - first));
937 if (newtext != NULL)
938 {
939 mch_memmove(newtext, oldtext, first);
Bram Moolenaarf2330482008-06-24 20:19:36 +0000940 STRMOVE(newtext + first, oldtext + lastbyte + 1);
Bram Moolenaar5eaf8722008-01-05 17:07:13 +0000941 nbdebug((" NEW LINE %d: %s\n", lnum, newtext));
942 ml_replace(lnum, newtext, FALSE);
943 }
944}
945
946/*
947 * Replace the "first" line with the concatenation of the "first" and
948 * the "other" line. The "other" line is not removed.
949 */
950 static void
951nb_joinlines(linenr_T first, linenr_T other)
952{
953 int len_first, len_other;
954 char_u *p;
955
Bram Moolenaarcb4cef22008-03-16 15:04:34 +0000956 len_first = (int)STRLEN(ml_get(first));
957 len_other = (int)STRLEN(ml_get(other));
Bram Moolenaar5eaf8722008-01-05 17:07:13 +0000958 p = alloc((unsigned)(len_first + len_other + 1));
959 if (p != NULL)
960 {
961 mch_memmove(p, ml_get(first), len_first);
962 mch_memmove(p + len_first, ml_get(other), len_other + 1);
963 ml_replace(first, p, FALSE);
964 }
965}
966
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967#define SKIP_STOP 2
968#define streq(a,b) (strcmp(a,b) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969
970/*
971 * Do the actual processing of a single netbeans command or function.
Bram Moolenaar143c38c2007-05-10 16:41:10 +0000972 * The difference between a command and function is that a function
973 * gets a response (it's required) but a command does not.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 * For arguments see comment for nb_parse_cmd().
975 */
976 static int
977nb_do_cmd(
978 int bufno,
979 char_u *cmd,
980 int func,
981 int cmdno,
982 char_u *args) /* points to space before arguments or NUL */
983{
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100984 int do_update = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 long off = 0;
986 nbbuf_T *buf = nb_get_buf(bufno);
987 static int skip = 0;
988 int retval = OK;
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000989 char *cp; /* for when a char pointer is needed */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990
991 nbdebug(("%s %d: (%d) %s %s\n", (func) ? "FUN" : "CMD", cmdno, bufno, cmd,
992 STRCMP(cmd, "insert") == 0 ? "<text>" : (char *)args));
993
994 if (func)
995 {
996/* =====================================================================*/
997 if (streq((char *)cmd, "getModified"))
998 {
999 if (buf == NULL || buf->bufp == NULL)
1000 /* Return the number of buffers that are modified. */
1001 nb_reply_nr(cmdno, (long)count_changed_buffers());
1002 else
1003 /* Return whether the buffer is modified. */
1004 nb_reply_nr(cmdno, (long)(buf->bufp->b_changed
1005 || isNetbeansModified(buf->bufp)));
1006/* =====================================================================*/
1007 }
1008 else if (streq((char *)cmd, "saveAndExit"))
1009 {
1010 /* Note: this will exit Vim if successful. */
1011 coloncmd(":confirm qall");
1012
1013 /* We didn't exit: return the number of changed buffers. */
1014 nb_reply_nr(cmdno, (long)count_changed_buffers());
1015/* =====================================================================*/
1016 }
1017 else if (streq((char *)cmd, "getCursor"))
1018 {
1019 char_u text[200];
1020
1021 /* Note: nb_getbufno() may return -1. This indicates the IDE
1022 * didn't assign a number to the current buffer in response to a
1023 * fileOpened event. */
1024 sprintf((char *)text, "%d %ld %d %ld",
1025 nb_getbufno(curbuf),
1026 (long)curwin->w_cursor.lnum,
1027 (int)curwin->w_cursor.col,
1028 pos2off(curbuf, &curwin->w_cursor));
1029 nb_reply_text(cmdno, text);
1030/* =====================================================================*/
1031 }
Bram Moolenaarc65c4912006-11-14 17:29:46 +00001032 else if (streq((char *)cmd, "getAnno"))
1033 {
1034 long linenum = 0;
1035#ifdef FEAT_SIGNS
1036 if (buf == NULL || buf->bufp == NULL)
1037 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001038 nbdebug((" Invalid buffer identifier in getAnno\n"));
1039 EMSG("E652: Invalid buffer identifier in getAnno");
Bram Moolenaarc65c4912006-11-14 17:29:46 +00001040 retval = FAIL;
1041 }
1042 else
1043 {
1044 int serNum;
1045
1046 cp = (char *)args;
1047 serNum = strtol(cp, &cp, 10);
1048 /* If the sign isn't found linenum will be zero. */
1049 linenum = (long)buf_findsign(buf->bufp, serNum);
1050 }
1051#endif
1052 nb_reply_nr(cmdno, linenum);
1053/* =====================================================================*/
1054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 else if (streq((char *)cmd, "getLength"))
1056 {
1057 long len = 0;
1058
1059 if (buf == NULL || buf->bufp == NULL)
1060 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001061 nbdebug((" invalid buffer identifier in getLength\n"));
1062 EMSG("E632: invalid buffer identifier in getLength");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 retval = FAIL;
1064 }
1065 else
1066 {
1067 len = get_buf_size(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 }
1069 nb_reply_nr(cmdno, len);
1070/* =====================================================================*/
1071 }
1072 else if (streq((char *)cmd, "getText"))
1073 {
1074 long len;
1075 linenr_T nlines;
1076 char_u *text = NULL;
1077 linenr_T lno = 1;
1078 char_u *p;
1079 char_u *line;
1080
1081 if (buf == NULL || buf->bufp == NULL)
1082 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001083 nbdebug((" invalid buffer identifier in getText\n"));
1084 EMSG("E633: invalid buffer identifier in getText");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 retval = FAIL;
1086 }
1087 else
1088 {
1089 len = get_buf_size(buf->bufp);
1090 nlines = buf->bufp->b_ml.ml_line_count;
1091 text = alloc((unsigned)((len > 0)
1092 ? ((len + nlines) * 2) : 4));
1093 if (text == NULL)
1094 {
1095 nbdebug((" nb_do_cmd: getText has null text field\n"));
1096 retval = FAIL;
1097 }
1098 else
1099 {
1100 p = text;
1101 *p++ = '\"';
1102 for (; lno <= nlines ; lno++)
1103 {
1104 line = nb_quote(ml_get_buf(buf->bufp, lno, FALSE));
1105 if (line != NULL)
1106 {
1107 STRCPY(p, line);
1108 p += STRLEN(line);
1109 *p++ = '\\';
1110 *p++ = 'n';
Bram Moolenaar009b2592004-10-24 19:18:58 +00001111 vim_free(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 }
1114 *p++ = '\"';
1115 *p = '\0';
1116 }
1117 }
1118 if (text == NULL)
1119 nb_reply_text(cmdno, (char_u *)"");
1120 else
1121 {
1122 nb_reply_text(cmdno, text);
1123 vim_free(text);
1124 }
1125/* =====================================================================*/
1126 }
1127 else if (streq((char *)cmd, "remove"))
1128 {
1129 long count;
1130 pos_T first, last;
1131 pos_T *pos;
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001132 pos_T *next;
1133 linenr_T del_from_lnum, del_to_lnum; /* lines to be deleted as a whole */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 int oldFire = netbeansFireChanges;
1135 int oldSuppress = netbeansSuppressNoLines;
1136 int wasChanged;
1137
1138 if (skip >= SKIP_STOP)
1139 {
1140 nbdebug((" Skipping %s command\n", (char *) cmd));
1141 nb_reply_nil(cmdno);
1142 return OK;
1143 }
1144
1145 if (buf == NULL || buf->bufp == NULL)
1146 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001147 nbdebug((" invalid buffer identifier in remove\n"));
1148 EMSG("E634: invalid buffer identifier in remove");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 retval = FAIL;
1150 }
1151 else
1152 {
1153 netbeansFireChanges = FALSE;
1154 netbeansSuppressNoLines = TRUE;
1155
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001156 nb_set_curbuf(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 wasChanged = buf->bufp->b_changed;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001158 cp = (char *)args;
1159 off = strtol(cp, &cp, 10);
1160 count = strtol(cp, &cp, 10);
1161 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 /* delete "count" chars, starting at "off" */
1163 pos = off2pos(buf->bufp, off);
1164 if (!pos)
1165 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001166 nbdebug((" !bad position\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 nb_reply_text(cmdno, (char_u *)"!bad position");
1168 netbeansFireChanges = oldFire;
1169 netbeansSuppressNoLines = oldSuppress;
1170 return FAIL;
1171 }
1172 first = *pos;
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001173 nbdebug((" FIRST POS: line %d, col %d\n",
1174 first.lnum, first.col));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 pos = off2pos(buf->bufp, off+count-1);
1176 if (!pos)
1177 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001178 nbdebug((" !bad count\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 nb_reply_text(cmdno, (char_u *)"!bad count");
1180 netbeansFireChanges = oldFire;
1181 netbeansSuppressNoLines = oldSuppress;
1182 return FAIL;
1183 }
1184 last = *pos;
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001185 nbdebug((" LAST POS: line %d, col %d\n",
1186 last.lnum, last.col));
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001187 del_from_lnum = first.lnum;
1188 del_to_lnum = last.lnum;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001189 do_update = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001191 /* Get the position of the first byte after the deleted
1192 * section. "next" is NULL when deleting to the end of the
1193 * file. */
1194 next = off2pos(buf->bufp, off + count);
1195
1196 /* Remove part of the first line. */
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001197 if (first.col != 0
1198 || (next != NULL && first.lnum == next->lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 {
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001200 if (first.lnum != last.lnum
1201 || (next != NULL && first.lnum != next->lnum))
1202 {
1203 /* remove to the end of the first line */
1204 nb_partialremove(first.lnum, first.col,
1205 (colnr_T)MAXCOL);
1206 if (first.lnum == last.lnum)
1207 {
1208 /* Partial line to remove includes the end of
1209 * line. Join the line with the next one, have
1210 * the next line deleted below. */
1211 nb_joinlines(first.lnum, next->lnum);
1212 del_to_lnum = next->lnum;
1213 }
1214 }
1215 else
1216 {
1217 /* remove within one line */
1218 nb_partialremove(first.lnum, first.col, last.col);
1219 }
1220 ++del_from_lnum; /* don't delete the first line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 }
1222
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001223 /* Remove part of the last line. */
1224 if (first.lnum != last.lnum && next != NULL
1225 && next->col != 0 && last.lnum == next->lnum)
1226 {
1227 nb_partialremove(last.lnum, 0, last.col);
1228 if (del_from_lnum > first.lnum)
1229 {
1230 /* Join end of last line to start of first line; last
1231 * line is deleted below. */
1232 nb_joinlines(first.lnum, last.lnum);
1233 }
1234 else
1235 /* First line is deleted as a whole, keep the last
1236 * line. */
1237 --del_to_lnum;
1238 }
1239
1240 /* First is partial line; last line to remove includes
1241 * the end of line; join first line to line following last
1242 * line; line following last line is deleted below. */
1243 if (first.lnum != last.lnum && del_from_lnum > first.lnum
1244 && next != NULL && last.lnum != next->lnum)
1245 {
1246 nb_joinlines(first.lnum, next->lnum);
1247 del_to_lnum = next->lnum;
1248 }
1249
1250 /* Delete whole lines if there are any. */
1251 if (del_to_lnum >= del_from_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 {
1253 int i;
1254
1255 /* delete signs from the lines being deleted */
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001256 for (i = del_from_lnum; i <= del_to_lnum; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 {
1258 int id = buf_findsign_id(buf->bufp, (linenr_T)i);
1259 if (id > 0)
1260 {
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001261 nbdebug((" Deleting sign %d on line %d\n",
1262 id, i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 buf_delsign(buf->bufp, id);
1264 }
1265 else
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001266 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 nbdebug((" No sign on line %d\n", i));
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 }
1270
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001271 nbdebug((" Deleting lines %d through %d\n",
1272 del_from_lnum, del_to_lnum));
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001273 curwin->w_cursor.lnum = del_from_lnum;
1274 curwin->w_cursor.col = 0;
1275 del_lines(del_to_lnum - del_from_lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 }
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001277
1278 /* Leave cursor at first deleted byte. */
1279 curwin->w_cursor = first;
1280 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 buf->bufp->b_changed = wasChanged; /* logically unchanged */
1282 netbeansFireChanges = oldFire;
1283 netbeansSuppressNoLines = oldSuppress;
1284
1285 u_blockfree(buf->bufp);
1286 u_clearall(buf->bufp);
1287 }
1288 nb_reply_nil(cmdno);
1289/* =====================================================================*/
1290 }
1291 else if (streq((char *)cmd, "insert"))
1292 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 char_u *to_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294
1295 if (skip >= SKIP_STOP)
1296 {
1297 nbdebug((" Skipping %s command\n", (char *) cmd));
1298 nb_reply_nil(cmdno);
1299 return OK;
1300 }
1301
1302 /* get offset */
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001303 cp = (char *)args;
1304 off = strtol(cp, &cp, 10);
1305 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306
1307 /* get text to be inserted */
1308 args = skipwhite(args);
1309 args = to_free = (char_u *)nb_unquote(args, NULL);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001310 /*
1311 nbdebug((" CHUNK[%d]: %d bytes at offset %d\n",
1312 buf->bufp->b_ml.ml_line_count, STRLEN(args), off));
1313 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314
1315 if (buf == NULL || buf->bufp == NULL)
1316 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001317 nbdebug((" invalid buffer identifier in insert\n"));
1318 EMSG("E635: invalid buffer identifier in insert");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 retval = FAIL;
1320 }
1321 else if (args != NULL)
1322 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001323 int ff_detected = EOL_UNKNOWN;
1324 int buf_was_empty = (buf->bufp->b_ml.ml_flags & ML_EMPTY);
1325 size_t len = 0;
1326 int added = 0;
1327 int oldFire = netbeansFireChanges;
1328 int old_b_changed;
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001329 char_u *nlp;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001330 linenr_T lnum;
1331 linenr_T lnum_start;
1332 pos_T *pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 netbeansFireChanges = 0;
1335
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001336 /* Jump to the buffer where we insert. After this "curbuf"
1337 * can be used. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001338 nb_set_curbuf(buf->bufp);
Bram Moolenaara3227e22006-03-08 21:32:40 +00001339 old_b_changed = curbuf->b_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001341 /* Convert the specified character offset into a lnum/col
1342 * position. */
Bram Moolenaara3227e22006-03-08 21:32:40 +00001343 pos = off2pos(curbuf, off);
1344 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001346 if (pos->lnum <= 0)
1347 lnum_start = 1;
1348 else
1349 lnum_start = pos->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 }
1351 else
1352 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001353 /* If the given position is not found, assume we want
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 * the end of the file. See setLocAndSize HACK. */
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001355 if (buf_was_empty)
1356 lnum_start = 1; /* above empty line */
1357 else
1358 lnum_start = curbuf->b_ml.ml_line_count + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001359 }
1360
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001361 /* "lnum" is the line where we insert: either append to it or
1362 * insert a new line above it. */
1363 lnum = lnum_start;
1364
1365 /* Loop over the "\n" separated lines of the argument. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001366 do_update = 1;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001367 while (*args != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 {
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001369 nlp = vim_strchr(args, '\n');
1370 if (nlp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001372 /* Incomplete line, probably truncated. Next "insert"
1373 * command should append to this one. */
1374 len = STRLEN(args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001376 else
1377 {
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001378 len = nlp - args;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001379
1380 /*
1381 * We need to detect EOL style, because the commands
1382 * use a character offset.
1383 */
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001384 if (nlp > args && nlp[-1] == '\r')
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001385 {
1386 ff_detected = EOL_DOS;
1387 --len;
1388 }
1389 else
1390 ff_detected = EOL_UNIX;
1391 }
1392 args[len] = NUL;
1393
1394 if (lnum == lnum_start
1395 && ((pos != NULL && pos->col > 0)
1396 || (lnum == 1 && buf_was_empty)))
1397 {
1398 char_u *oldline = ml_get(lnum);
1399 char_u *newline;
1400
Bram Moolenaare4365282012-04-20 19:47:05 +02001401 /* Insert halfway a line. */
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001402 newline = alloc_check(
1403 (unsigned)(STRLEN(oldline) + len + 1));
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001404 if (newline != NULL)
1405 {
Bram Moolenaare4365282012-04-20 19:47:05 +02001406 mch_memmove(newline, oldline, (size_t)pos->col);
1407 newline[pos->col] = NUL;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001408 STRCAT(newline, args);
Bram Moolenaare4365282012-04-20 19:47:05 +02001409 STRCAT(newline, oldline + pos->col);
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001410 ml_replace(lnum, newline, FALSE);
1411 }
1412 }
1413 else
1414 {
1415 /* Append a new line. Not that we always do this,
1416 * also when the text doesn't end in a "\n". */
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001417 ml_append((linenr_T)(lnum - 1), args,
1418 (colnr_T)(len + 1), FALSE);
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001419 ++added;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001420 }
1421
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001422 if (nlp == NULL)
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001423 break;
1424 ++lnum;
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001425 args = nlp + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001426 }
1427
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001428 /* Adjust the marks below the inserted lines. */
1429 appended_lines_mark(lnum_start - 1, (long)added);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001431 /*
1432 * When starting with an empty buffer set the fileformat.
1433 * This is just guessing...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 */
1435 if (buf_was_empty)
1436 {
1437 if (ff_detected == EOL_UNKNOWN)
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001438#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 ff_detected = EOL_DOS;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001440#else
1441 ff_detected = EOL_UNIX;
1442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 set_fileformat(ff_detected, OPT_LOCAL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00001444 curbuf->b_start_ffc = *curbuf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 }
1446
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 /*
1448 * XXX - GRP - Is the next line right? If I've inserted
1449 * text the buffer has been updated but not written. Will
1450 * netbeans guarantee to write it? Even if I do a :q! ?
1451 */
Bram Moolenaara3227e22006-03-08 21:32:40 +00001452 curbuf->b_changed = old_b_changed; /* logically unchanged */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 netbeansFireChanges = oldFire;
1454
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001455 /* Undo info is invalid now... */
Bram Moolenaara3227e22006-03-08 21:32:40 +00001456 u_blockfree(curbuf);
1457 u_clearall(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 }
1459 vim_free(to_free);
1460 nb_reply_nil(cmdno); /* or !error */
1461 }
1462 else
1463 {
1464 nbdebug(("UNIMPLEMENTED FUNCTION: %s\n", cmd));
1465 nb_reply_nil(cmdno);
1466 retval = FAIL;
1467 }
1468 }
1469 else /* Not a function; no reply required. */
1470 {
1471/* =====================================================================*/
1472 if (streq((char *)cmd, "create"))
1473 {
1474 /* Create a buffer without a name. */
1475 if (buf == NULL)
1476 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001477 nbdebug((" invalid buffer identifier in create\n"));
1478 EMSG("E636: invalid buffer identifier in create");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 return FAIL;
1480 }
1481 vim_free(buf->displayname);
1482 buf->displayname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483
1484 netbeansReadFile = 0; /* don't try to open disk file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001485 do_ecmd(0, NULL, 0, 0, ECMD_ONE, ECMD_HIDE + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 netbeansReadFile = 1;
1487 buf->bufp = curbuf;
1488 maketitle();
Bram Moolenaar009b2592004-10-24 19:18:58 +00001489 buf->insertDone = FALSE;
Bram Moolenaar67c53842010-05-22 18:28:27 +02001490#if defined(FEAT_MENU) && defined(FEAT_GUI)
Bram Moolenaard54a6882010-08-09 22:49:00 +02001491 if (gui.in_use)
1492 gui_update_menus(0);
Bram Moolenaar67c53842010-05-22 18:28:27 +02001493#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494/* =====================================================================*/
1495 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001496 else if (streq((char *)cmd, "insertDone"))
1497 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001498 if (buf == NULL || buf->bufp == NULL)
1499 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001500 nbdebug((" invalid buffer identifier in insertDone\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001501 }
1502 else
1503 {
1504 buf->bufp->b_start_eol = *args == 'T';
1505 buf->insertDone = TRUE;
1506 args += 2;
1507 buf->bufp->b_p_ro = *args == 'T';
1508 print_read_msg(buf);
1509 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001510/* =====================================================================*/
1511 }
1512 else if (streq((char *)cmd, "saveDone"))
1513 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001514 long savedChars = atol((char *)args);
1515
1516 if (buf == NULL || buf->bufp == NULL)
1517 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001518 nbdebug((" invalid buffer identifier in saveDone\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001519 }
1520 else
1521 print_save_msg(buf, savedChars);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001522/* =====================================================================*/
1523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 else if (streq((char *)cmd, "startDocumentListen"))
1525 {
1526 if (buf == NULL)
1527 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001528 nbdebug((" invalid buffer identifier in startDocumentListen\n"));
1529 EMSG("E637: invalid buffer identifier in startDocumentListen");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 return FAIL;
1531 }
1532 buf->fireChanges = 1;
1533/* =====================================================================*/
1534 }
1535 else if (streq((char *)cmd, "stopDocumentListen"))
1536 {
1537 if (buf == NULL)
1538 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001539 nbdebug((" invalid buffer identifier in stopDocumentListen\n"));
1540 EMSG("E638: invalid buffer identifier in stopDocumentListen");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 return FAIL;
1542 }
1543 buf->fireChanges = 0;
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001544 if (buf->bufp != NULL && buf->bufp->b_was_netbeans_file)
Bram Moolenaar009b2592004-10-24 19:18:58 +00001545 {
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001546 if (!buf->bufp->b_netbeans_file)
Bram Moolenaarf2330482008-06-24 20:19:36 +00001547 {
1548 nbdebug(("E658: NetBeans connection lost for buffer %ld\n", buf->bufp->b_fnum));
Bram Moolenaar009b2592004-10-24 19:18:58 +00001549 EMSGN(_("E658: NetBeans connection lost for buffer %ld"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 buf->bufp->b_fnum);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001551 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001552 else
1553 {
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001554 /* NetBeans uses stopDocumentListen when it stops editing
1555 * a file. It then expects the buffer in Vim to
1556 * disappear. */
1557 do_bufdel(DOBUF_DEL, (char_u *)"", 1,
1558 buf->bufp->b_fnum, buf->bufp->b_fnum, TRUE);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001559 vim_memset(buf, 0, sizeof(nbbuf_T));
1560 }
1561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562/* =====================================================================*/
1563 }
1564 else if (streq((char *)cmd, "setTitle"))
1565 {
1566 if (buf == NULL)
1567 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001568 nbdebug((" invalid buffer identifier in setTitle\n"));
1569 EMSG("E639: invalid buffer identifier in setTitle");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 return FAIL;
1571 }
1572 vim_free(buf->displayname);
1573 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574/* =====================================================================*/
1575 }
1576 else if (streq((char *)cmd, "initDone"))
1577 {
1578 if (buf == NULL || buf->bufp == NULL)
1579 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001580 nbdebug((" invalid buffer identifier in initDone\n"));
1581 EMSG("E640: invalid buffer identifier in initDone");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 return FAIL;
1583 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001584 do_update = 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001585 buf->initDone = TRUE;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001586 nb_set_curbuf(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587#if defined(FEAT_AUTOCMD)
1588 apply_autocmds(EVENT_BUFREADPOST, 0, 0, FALSE, buf->bufp);
1589#endif
1590
1591 /* handle any postponed key commands */
1592 handle_key_queue();
1593/* =====================================================================*/
1594 }
1595 else if (streq((char *)cmd, "setBufferNumber")
1596 || streq((char *)cmd, "putBufferNumber"))
1597 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00001598 char_u *path;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 buf_T *bufp;
1600
1601 if (buf == NULL)
1602 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001603 nbdebug((" invalid buffer identifier in setBufferNumber\n"));
1604 EMSG("E641: invalid buffer identifier in setBufferNumber");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 return FAIL;
1606 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001607 path = (char_u *)nb_unquote(args, NULL);
1608 if (path == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 return FAIL;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001610 bufp = buflist_findname(path);
1611 vim_free(path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 if (bufp == NULL)
1613 {
Bram Moolenaarec906222009-02-21 21:14:00 +00001614 nbdebug((" File %s not found in setBufferNumber\n", args));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 EMSG2("E642: File %s not found in setBufferNumber", args);
1616 return FAIL;
1617 }
1618 buf->bufp = bufp;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001619 buf->nbbuf_number = bufp->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620
1621 /* "setBufferNumber" has the side effect of jumping to the buffer
1622 * (don't know why!). Don't do that for "putBufferNumber". */
1623 if (*cmd != 'p')
1624 coloncmd(":buffer %d", bufp->b_fnum);
1625 else
1626 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00001627 buf->initDone = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628
1629 /* handle any postponed key commands */
1630 handle_key_queue();
1631 }
1632
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633/* =====================================================================*/
1634 }
1635 else if (streq((char *)cmd, "setFullName"))
1636 {
1637 if (buf == NULL)
1638 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001639 nbdebug((" invalid buffer identifier in setFullName\n"));
1640 EMSG("E643: invalid buffer identifier in setFullName");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 return FAIL;
1642 }
1643 vim_free(buf->displayname);
1644 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645
1646 netbeansReadFile = 0; /* don't try to open disk file */
1647 do_ecmd(0, (char_u *)buf->displayname, 0, 0, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001648 ECMD_HIDE + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 netbeansReadFile = 1;
1650 buf->bufp = curbuf;
1651 maketitle();
Bram Moolenaar67c53842010-05-22 18:28:27 +02001652#if defined(FEAT_MENU) && defined(FEAT_GUI)
Bram Moolenaard54a6882010-08-09 22:49:00 +02001653 if (gui.in_use)
1654 gui_update_menus(0);
Bram Moolenaar67c53842010-05-22 18:28:27 +02001655#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656/* =====================================================================*/
1657 }
1658 else if (streq((char *)cmd, "editFile"))
1659 {
1660 if (buf == NULL)
1661 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001662 nbdebug((" invalid buffer identifier in editFile\n"));
1663 EMSG("E644: invalid buffer identifier in editFile");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 return FAIL;
1665 }
1666 /* Edit a file: like create + setFullName + read the file. */
1667 vim_free(buf->displayname);
1668 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 do_ecmd(0, (char_u *)buf->displayname, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001670 ECMD_HIDE + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 buf->bufp = curbuf;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001672 buf->initDone = TRUE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001673 do_update = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674#if defined(FEAT_TITLE)
1675 maketitle();
1676#endif
Bram Moolenaar67c53842010-05-22 18:28:27 +02001677#if defined(FEAT_MENU) && defined(FEAT_GUI)
Bram Moolenaard54a6882010-08-09 22:49:00 +02001678 if (gui.in_use)
1679 gui_update_menus(0);
Bram Moolenaar67c53842010-05-22 18:28:27 +02001680#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681/* =====================================================================*/
1682 }
1683 else if (streq((char *)cmd, "setVisible"))
1684 {
1685 if (buf == NULL || buf->bufp == NULL)
1686 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001687 nbdebug((" invalid buffer identifier in setVisible\n"));
1688 /* This message was commented out, probably because it can
1689 * happen when shutting down. */
1690 if (p_verbose > 0)
1691 EMSG("E645: invalid buffer identifier in setVisible");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 return FAIL;
1693 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001694 if (streq((char *)args, "T") && buf->bufp != curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 {
1696 exarg_T exarg;
1697 exarg.cmd = (char_u *)"goto";
1698 exarg.forceit = FALSE;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001699 dosetvisible = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 goto_buffer(&exarg, DOBUF_FIRST, FORWARD, buf->bufp->b_fnum);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001701 do_update = 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001702 dosetvisible = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703
Bram Moolenaar67c53842010-05-22 18:28:27 +02001704#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 /* Side effect!!!. */
Bram Moolenaard54a6882010-08-09 22:49:00 +02001706 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 gui_mch_set_foreground();
Bram Moolenaar67c53842010-05-22 18:28:27 +02001708#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710/* =====================================================================*/
1711 }
1712 else if (streq((char *)cmd, "raise"))
1713 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02001714#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 /* Bring gvim to the foreground. */
Bram Moolenaard54a6882010-08-09 22:49:00 +02001716 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 gui_mch_set_foreground();
Bram Moolenaar67c53842010-05-22 18:28:27 +02001718#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719/* =====================================================================*/
1720 }
1721 else if (streq((char *)cmd, "setModified"))
1722 {
Bram Moolenaarff064e12008-06-09 13:10:45 +00001723 int prev_b_changed;
1724
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 if (buf == NULL || buf->bufp == NULL)
1726 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001727 nbdebug((" invalid buffer identifier in setModified\n"));
1728 /* This message was commented out, probably because it can
1729 * happen when shutting down. */
1730 if (p_verbose > 0)
1731 EMSG("E646: invalid buffer identifier in setModified");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 return FAIL;
1733 }
Bram Moolenaarff064e12008-06-09 13:10:45 +00001734 prev_b_changed = buf->bufp->b_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 if (streq((char *)args, "T"))
Bram Moolenaarff064e12008-06-09 13:10:45 +00001736 buf->bufp->b_changed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 else
1738 {
1739 struct stat st;
1740
1741 /* Assume NetBeans stored the file. Reset the timestamp to
1742 * avoid "file changed" warnings. */
1743 if (buf->bufp->b_ffname != NULL
1744 && mch_stat((char *)buf->bufp->b_ffname, &st) >= 0)
1745 buf_store_time(buf->bufp, &st, buf->bufp->b_ffname);
Bram Moolenaarff064e12008-06-09 13:10:45 +00001746 buf->bufp->b_changed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 }
1748 buf->modified = buf->bufp->b_changed;
Bram Moolenaarff064e12008-06-09 13:10:45 +00001749 if (prev_b_changed != buf->bufp->b_changed)
1750 {
1751#ifdef FEAT_WINDOWS
1752 check_status(buf->bufp);
1753 redraw_tabline = TRUE;
1754#endif
1755#ifdef FEAT_TITLE
1756 maketitle();
1757#endif
1758 update_screen(0);
1759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760/* =====================================================================*/
1761 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001762 else if (streq((char *)cmd, "setModtime"))
1763 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001764 if (buf == NULL || buf->bufp == NULL)
Bram Moolenaarf2330482008-06-24 20:19:36 +00001765 nbdebug((" invalid buffer identifier in setModtime\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001766 else
1767 buf->bufp->b_mtime = atoi((char *)args);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001768/* =====================================================================*/
1769 }
1770 else if (streq((char *)cmd, "setReadOnly"))
1771 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001772 if (buf == NULL || buf->bufp == NULL)
Bram Moolenaarf2330482008-06-24 20:19:36 +00001773 nbdebug((" invalid buffer identifier in setReadOnly\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001774 else if (streq((char *)args, "T"))
Bram Moolenaar009b2592004-10-24 19:18:58 +00001775 buf->bufp->b_p_ro = TRUE;
1776 else
1777 buf->bufp->b_p_ro = FALSE;
1778/* =====================================================================*/
1779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 else if (streq((char *)cmd, "setMark"))
1781 {
1782 /* not yet */
1783/* =====================================================================*/
1784 }
1785 else if (streq((char *)cmd, "showBalloon"))
1786 {
1787#if defined(FEAT_BEVAL)
1788 static char *text = NULL;
1789
1790 /*
1791 * Set up the Balloon Expression Evaluation area.
1792 * Ignore 'ballooneval' here.
1793 * The text pointer must remain valid for a while.
1794 */
1795 if (balloonEval != NULL)
1796 {
1797 vim_free(text);
1798 text = nb_unquote(args, NULL);
1799 if (text != NULL)
1800 gui_mch_post_balloon(balloonEval, (char_u *)text);
1801 }
1802#endif
1803/* =====================================================================*/
1804 }
1805 else if (streq((char *)cmd, "setDot"))
1806 {
1807 pos_T *pos;
1808#ifdef NBDEBUG
1809 char_u *s;
1810#endif
1811
1812 if (buf == NULL || buf->bufp == NULL)
1813 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001814 nbdebug((" invalid buffer identifier in setDot\n"));
1815 EMSG("E647: invalid buffer identifier in setDot");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 return FAIL;
1817 }
1818
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001819 nb_set_curbuf(buf->bufp);
1820
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 /* Don't want Visual mode now. */
1822 if (VIsual_active)
1823 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824#ifdef NBDEBUG
1825 s = args;
1826#endif
1827 pos = get_off_or_lnum(buf->bufp, &args);
1828 if (pos)
1829 {
1830 curwin->w_cursor = *pos;
1831 check_cursor();
1832#ifdef FEAT_FOLDING
1833 foldOpenCursor();
1834#endif
1835 }
1836 else
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001837 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 nbdebug((" BAD POSITION in setDot: %s\n", s));
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840
1841 /* gui_update_cursor(TRUE, FALSE); */
1842 /* update_curbuf(NOT_VALID); */
1843 update_topline(); /* scroll to show the line */
1844 update_screen(VALID);
1845 setcursor();
Bram Moolenaar96bcc5e2011-04-01 15:33:59 +02001846 cursor_on();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 out_flush();
Bram Moolenaar67c53842010-05-22 18:28:27 +02001848#ifdef FEAT_GUI
Bram Moolenaard54a6882010-08-09 22:49:00 +02001849 if (gui.in_use)
1850 {
1851 gui_update_cursor(TRUE, FALSE);
1852 gui_mch_flush();
1853 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02001854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 /* Quit a hit-return or more prompt. */
1856 if (State == HITRETURN || State == ASKMORE)
1857 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858#ifdef FEAT_GUI_GTK
Bram Moolenaard54a6882010-08-09 22:49:00 +02001859 if (gui.in_use && gtk_main_level() > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 gtk_main_quit();
1861#endif
1862 }
1863/* =====================================================================*/
1864 }
1865 else if (streq((char *)cmd, "close"))
1866 {
1867#ifdef NBDEBUG
1868 char *name = "<NONE>";
1869#endif
1870
1871 if (buf == NULL)
1872 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001873 nbdebug((" invalid buffer identifier in close\n"));
1874 EMSG("E648: invalid buffer identifier in close");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 return FAIL;
1876 }
1877
1878#ifdef NBDEBUG
1879 if (buf->displayname != NULL)
1880 name = buf->displayname;
1881#endif
Bram Moolenaarf2330482008-06-24 20:19:36 +00001882 if (buf->bufp == NULL)
1883 {
1884 nbdebug((" invalid buffer identifier in close\n"));
1885 /* This message was commented out, probably because it can
1886 * happen when shutting down. */
1887 if (p_verbose > 0)
1888 EMSG("E649: invalid buffer identifier in close");
1889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 nbdebug((" CLOSE %d: %s\n", bufno, name));
Bram Moolenaar67c53842010-05-22 18:28:27 +02001891#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 need_mouse_correct = TRUE;
Bram Moolenaar67c53842010-05-22 18:28:27 +02001893#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 if (buf->bufp != NULL)
1895 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD,
1896 buf->bufp->b_fnum, TRUE);
Bram Moolenaar8d602722006-08-08 19:34:19 +00001897 buf->bufp = NULL;
1898 buf->initDone = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001899 do_update = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900/* =====================================================================*/
1901 }
1902 else if (streq((char *)cmd, "setStyle")) /* obsolete... */
1903 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001904 nbdebug((" setStyle is obsolete!\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905/* =====================================================================*/
1906 }
1907 else if (streq((char *)cmd, "setExitDelay"))
1908 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00001909 /* Only used in version 2.1. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910/* =====================================================================*/
1911 }
1912 else if (streq((char *)cmd, "defineAnnoType"))
1913 {
1914#ifdef FEAT_SIGNS
1915 int typeNum;
1916 char_u *typeName;
1917 char_u *tooltip;
1918 char_u *p;
1919 char_u *glyphFile;
Bram Moolenaar67c53842010-05-22 18:28:27 +02001920 int parse_error = FALSE;
1921 char_u *fg;
1922 char_u *bg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923
1924 if (buf == NULL)
1925 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001926 nbdebug((" invalid buffer identifier in defineAnnoType\n"));
1927 EMSG("E650: invalid buffer identifier in defineAnnoType");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 return FAIL;
1929 }
1930
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001931 cp = (char *)args;
1932 typeNum = strtol(cp, &cp, 10);
1933 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 args = skipwhite(args);
1935 typeName = (char_u *)nb_unquote(args, &args);
1936 args = skipwhite(args + 1);
1937 tooltip = (char_u *)nb_unquote(args, &args);
1938 args = skipwhite(args + 1);
1939
1940 p = (char_u *)nb_unquote(args, &args);
1941 glyphFile = vim_strsave_escaped(p, escape_chars);
1942 vim_free(p);
1943
1944 args = skipwhite(args + 1);
Bram Moolenaar67c53842010-05-22 18:28:27 +02001945 p = skiptowhite(args);
1946 if (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02001948 *p = NUL;
1949 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02001951 fg = vim_strsave(args);
1952 bg = vim_strsave(p);
1953 if (STRLEN(fg) > MAX_COLOR_LENGTH || STRLEN(bg) > MAX_COLOR_LENGTH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02001955 EMSG("E532: highlighting color name too long in defineAnnoType");
1956 vim_free(typeName);
1957 parse_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02001959 else if (typeName != NULL && tooltip != NULL && glyphFile != NULL)
1960 addsigntype(buf, typeNum, typeName, tooltip, glyphFile, fg, bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 else
1962 vim_free(typeName);
1963
1964 /* don't free typeName; it's used directly in addsigntype() */
Bram Moolenaar67c53842010-05-22 18:28:27 +02001965 vim_free(fg);
1966 vim_free(bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 vim_free(tooltip);
1968 vim_free(glyphFile);
Bram Moolenaar67c53842010-05-22 18:28:27 +02001969 if (parse_error)
1970 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971
1972#endif
1973/* =====================================================================*/
1974 }
1975 else if (streq((char *)cmd, "addAnno"))
1976 {
1977#ifdef FEAT_SIGNS
1978 int serNum;
1979 int localTypeNum;
1980 int typeNum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 pos_T *pos;
1982
1983 if (buf == NULL || buf->bufp == NULL)
1984 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001985 nbdebug((" invalid buffer identifier in addAnno\n"));
1986 EMSG("E651: invalid buffer identifier in addAnno");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 return FAIL;
1988 }
1989
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001990 do_update = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001992 cp = (char *)args;
1993 serNum = strtol(cp, &cp, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994
1995 /* Get the typenr specific for this buffer and convert it to
1996 * the global typenumber, as used for the sign name. */
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001997 localTypeNum = strtol(cp, &cp, 10);
1998 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 typeNum = mapsigntype(buf, localTypeNum);
2000
2001 pos = get_off_or_lnum(buf->bufp, &args);
2002
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002003 cp = (char *)args;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002004 ignored = (int)strtol(cp, &cp, 10);
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002005 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006# ifdef NBDEBUG
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002007 if (ignored != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002009 nbdebug((" partial line annotation -- Not Yet Implemented!\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 }
2011# endif
2012 if (serNum >= GUARDEDOFFSET)
2013 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002014 nbdebug((" too many annotations! ignoring...\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 return FAIL;
2016 }
2017 if (pos)
2018 {
Bram Moolenaarec906222009-02-21 21:14:00 +00002019 coloncmd(":sign place %d line=%ld name=%d buffer=%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 serNum, pos->lnum, typeNum, buf->bufp->b_fnum);
2021 if (typeNum == curPCtype)
2022 coloncmd(":sign jump %d buffer=%d", serNum,
2023 buf->bufp->b_fnum);
2024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025#endif
2026/* =====================================================================*/
2027 }
2028 else if (streq((char *)cmd, "removeAnno"))
2029 {
2030#ifdef FEAT_SIGNS
2031 int serNum;
2032
2033 if (buf == NULL || buf->bufp == NULL)
2034 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002035 nbdebug((" invalid buffer identifier in removeAnno\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 return FAIL;
2037 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002038 do_update = 1;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002039 cp = (char *)args;
2040 serNum = strtol(cp, &cp, 10);
2041 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 coloncmd(":sign unplace %d buffer=%d",
2043 serNum, buf->bufp->b_fnum);
2044 redraw_buf_later(buf->bufp, NOT_VALID);
2045#endif
2046/* =====================================================================*/
2047 }
2048 else if (streq((char *)cmd, "moveAnnoToFront"))
2049 {
2050#ifdef FEAT_SIGNS
Bram Moolenaarf2330482008-06-24 20:19:36 +00002051 nbdebug((" moveAnnoToFront: Not Yet Implemented!\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052#endif
2053/* =====================================================================*/
2054 }
2055 else if (streq((char *)cmd, "guard") || streq((char *)cmd, "unguard"))
2056 {
2057 int len;
2058 pos_T first;
2059 pos_T last;
2060 pos_T *pos;
2061 int un = (cmd[0] == 'u');
2062 static int guardId = GUARDEDOFFSET;
2063
2064 if (skip >= SKIP_STOP)
2065 {
2066 nbdebug((" Skipping %s command\n", (char *) cmd));
2067 return OK;
2068 }
2069
2070 nb_init_graphics();
2071
2072 if (buf == NULL || buf->bufp == NULL)
2073 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002074 nbdebug((" invalid buffer identifier in %s command\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 return FAIL;
2076 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002077 nb_set_curbuf(buf->bufp);
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002078 cp = (char *)args;
2079 off = strtol(cp, &cp, 10);
2080 len = strtol(cp, NULL, 10);
2081 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 pos = off2pos(buf->bufp, off);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002083 do_update = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 if (!pos)
2085 nbdebug((" no such start pos in %s, %ld\n", cmd, off));
2086 else
2087 {
2088 first = *pos;
2089 pos = off2pos(buf->bufp, off + len - 1);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002090 if (pos != NULL && pos->col == 0)
2091 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 /*
2093 * In Java Swing the offset is a position between 2
2094 * characters. If col == 0 then we really want the
2095 * previous line as the end.
2096 */
2097 pos = off2pos(buf->bufp, off + len - 2);
2098 }
2099 if (!pos)
2100 nbdebug((" no such end pos in %s, %ld\n",
2101 cmd, off + len - 1));
2102 else
2103 {
2104 long lnum;
2105 last = *pos;
2106 /* set highlight for region */
2107 nbdebug((" %sGUARD %ld,%d to %ld,%d\n", (un) ? "UN" : "",
2108 first.lnum, first.col,
2109 last.lnum, last.col));
2110#ifdef FEAT_SIGNS
2111 for (lnum = first.lnum; lnum <= last.lnum; lnum++)
2112 {
2113 if (un)
2114 {
2115 /* never used */
2116 }
2117 else
2118 {
2119 if (buf_findsigntype_id(buf->bufp, lnum,
2120 GUARDED) == 0)
2121 {
2122 coloncmd(
Bram Moolenaarec906222009-02-21 21:14:00 +00002123 ":sign place %d line=%ld name=%d buffer=%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 guardId++, lnum, GUARDED,
2125 buf->bufp->b_fnum);
2126 }
2127 }
2128 }
2129#endif
2130 redraw_buf_later(buf->bufp, NOT_VALID);
2131 }
2132 }
2133/* =====================================================================*/
2134 }
2135 else if (streq((char *)cmd, "startAtomic"))
2136 {
2137 inAtomic = 1;
2138/* =====================================================================*/
2139 }
2140 else if (streq((char *)cmd, "endAtomic"))
2141 {
2142 inAtomic = 0;
2143 if (needupdate)
2144 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002145 do_update = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 needupdate = 0;
2147 }
2148/* =====================================================================*/
2149 }
2150 else if (streq((char *)cmd, "save"))
2151 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002152 /*
2153 * NOTE - This command is obsolete wrt NetBeans. Its left in
2154 * only for historical reasons.
2155 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 if (buf == NULL || buf->bufp == NULL)
2157 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002158 nbdebug((" invalid buffer identifier in %s command\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 return FAIL;
2160 }
2161
2162 /* the following is taken from ex_cmds.c (do_wqall function) */
2163 if (bufIsChanged(buf->bufp))
2164 {
2165 /* Only write if the buffer can be written. */
2166 if (p_write
2167 && !buf->bufp->b_p_ro
2168 && buf->bufp->b_ffname != NULL
2169#ifdef FEAT_QUICKFIX
2170 && !bt_dontwrite(buf->bufp)
2171#endif
2172 )
2173 {
2174 buf_write_all(buf->bufp, FALSE);
2175#ifdef FEAT_AUTOCMD
2176 /* an autocommand may have deleted the buffer */
2177 if (!buf_valid(buf->bufp))
2178 buf->bufp = NULL;
2179#endif
2180 }
2181 }
Bram Moolenaarf2330482008-06-24 20:19:36 +00002182 else
2183 {
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002184 nbdebug((" Buffer has no changes!\n"));
Bram Moolenaarf2330482008-06-24 20:19:36 +00002185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186/* =====================================================================*/
2187 }
2188 else if (streq((char *)cmd, "netbeansBuffer"))
2189 {
2190 if (buf == NULL || buf->bufp == NULL)
2191 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002192 nbdebug((" invalid buffer identifier in %s command\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 return FAIL;
2194 }
2195 if (*args == 'T')
2196 {
2197 buf->bufp->b_netbeans_file = TRUE;
2198 buf->bufp->b_was_netbeans_file = TRUE;
2199 }
2200 else
2201 buf->bufp->b_netbeans_file = FALSE;
2202/* =====================================================================*/
2203 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002204 else if (streq((char *)cmd, "specialKeys"))
2205 {
2206 special_keys(args);
2207/* =====================================================================*/
2208 }
2209 else if (streq((char *)cmd, "actionMenuItem"))
2210 {
2211 /* not used yet */
2212/* =====================================================================*/
2213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 else if (streq((char *)cmd, "version"))
2215 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002216 /* not used yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 }
Bram Moolenaarf2330482008-06-24 20:19:36 +00002218 else
2219 {
2220 nbdebug(("Unrecognised command: %s\n", cmd));
2221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 /*
2223 * Unrecognized command is ignored.
2224 */
2225 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002226 if (inAtomic && do_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 {
2228 needupdate = 1;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002229 do_update = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 }
2231
Bram Moolenaar009b2592004-10-24 19:18:58 +00002232 /*
2233 * Is this needed? I moved the netbeans_Xt_connect() later during startup
2234 * and it may no longer be necessary. If its not needed then needupdate
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002235 * and do_update can also be removed.
Bram Moolenaar009b2592004-10-24 19:18:58 +00002236 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002237 if (buf != NULL && buf->initDone && do_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 {
2239 update_screen(NOT_VALID);
2240 setcursor();
Bram Moolenaar96bcc5e2011-04-01 15:33:59 +02002241 cursor_on();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 out_flush();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002243#ifdef FEAT_GUI
Bram Moolenaard54a6882010-08-09 22:49:00 +02002244 if (gui.in_use)
2245 {
2246 gui_update_cursor(TRUE, FALSE);
2247 gui_mch_flush();
2248 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02002249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 /* Quit a hit-return or more prompt. */
2251 if (State == HITRETURN || State == ASKMORE)
2252 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253#ifdef FEAT_GUI_GTK
Bram Moolenaard54a6882010-08-09 22:49:00 +02002254 if (gui.in_use && gtk_main_level() > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 gtk_main_quit();
2256#endif
2257 }
2258 }
2259
2260 return retval;
2261}
2262
2263
2264/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002265 * If "buf" is not the current buffer try changing to a window that edits this
2266 * buffer. If there is no such window then close the current buffer and set
2267 * the current buffer as "buf".
2268 */
2269 static void
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00002270nb_set_curbuf(buf_T *buf)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002271{
Bram Moolenaar404c9422015-03-14 15:35:52 +01002272 if (curbuf != buf) {
2273 if (buf_jump_open_win(buf) != NULL)
2274 return;
2275# ifdef FEAT_WINDOWS
2276 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf) != NULL)
2277 return;
2278# endif
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002279 set_curbuf(buf, DOBUF_GOTO);
Bram Moolenaar404c9422015-03-14 15:35:52 +01002280 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002281}
2282
2283/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 * Process a vim colon command.
2285 */
2286 static void
2287coloncmd(char *cmd, ...)
2288{
2289 char buf[1024];
2290 va_list ap;
2291
2292 va_start(ap, cmd);
Bram Moolenaar0dc79e82009-06-24 14:50:12 +00002293 vim_vsnprintf(buf, sizeof(buf), cmd, ap, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 va_end(ap);
2295
2296 nbdebug((" COLONCMD %s\n", buf));
2297
2298/* ALT_INPUT_LOCK_ON; */
2299 do_cmdline((char_u *)buf, NULL, NULL, DOCMD_NOWAIT | DOCMD_KEYTYPED);
2300/* ALT_INPUT_LOCK_OFF; */
2301
2302 setcursor(); /* restore the cursor position */
2303 out_flush(); /* make sure output has been written */
2304
Bram Moolenaar67c53842010-05-22 18:28:27 +02002305#ifdef FEAT_GUI
Bram Moolenaard54a6882010-08-09 22:49:00 +02002306 if (gui.in_use)
2307 {
2308 gui_update_cursor(TRUE, FALSE);
2309 gui_mch_flush();
2310 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02002311#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312}
2313
2314
2315/*
Bram Moolenaar009b2592004-10-24 19:18:58 +00002316 * Parse the specialKeys argument and issue the appropriate map commands.
2317 */
2318 static void
2319special_keys(char_u *args)
2320{
2321 char *save_str = nb_unquote(args, NULL);
2322 char *tok = strtok(save_str, " ");
2323 char *sep;
2324 char keybuf[64];
2325 char cmdbuf[256];
2326
2327 while (tok != NULL)
2328 {
2329 int i = 0;
2330
2331 if ((sep = strchr(tok, '-')) != NULL)
2332 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002333 *sep = NUL;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002334 while (*tok)
2335 {
2336 switch (*tok)
2337 {
2338 case 'A':
2339 case 'M':
2340 case 'C':
2341 case 'S':
2342 keybuf[i++] = *tok;
2343 keybuf[i++] = '-';
2344 break;
2345 }
2346 tok++;
2347 }
2348 tok++;
2349 }
2350
2351 strcpy(&keybuf[i], tok);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002352 vim_snprintf(cmdbuf, sizeof(cmdbuf),
2353 "<silent><%s> :nbkey %s<CR>", keybuf, keybuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002354 do_map(0, (char_u *)cmdbuf, NORMAL, FALSE);
2355 tok = strtok(NULL, " ");
2356 }
2357 vim_free(save_str);
2358}
2359
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002360 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002361ex_nbclose(exarg_T *eap UNUSED)
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002362{
2363 netbeans_close();
2364}
Bram Moolenaar009b2592004-10-24 19:18:58 +00002365
2366 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002367ex_nbkey(exarg_T *eap)
Bram Moolenaar009b2592004-10-24 19:18:58 +00002368{
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002369 (void)netbeans_keystring(eap->arg);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002370}
2371
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002372 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002373ex_nbstart(
2374 exarg_T *eap)
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002375{
Bram Moolenaarc2a406b2010-09-30 21:03:26 +02002376#ifdef FEAT_GUI
2377# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar7ad7d012010-11-16 15:49:02 +01002378 && !defined(FEAT_GUI_W32)
Bram Moolenaarc2a406b2010-09-30 21:03:26 +02002379 if (gui.in_use)
2380 {
Bram Moolenaar7ad7d012010-11-16 15:49:02 +01002381 EMSG(_("E838: netbeans is not supported with this GUI"));
2382 return;
Bram Moolenaarc2a406b2010-09-30 21:03:26 +02002383 }
2384# endif
2385#endif
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002386 netbeans_open((char *)eap->arg, FALSE);
2387}
Bram Moolenaar009b2592004-10-24 19:18:58 +00002388
2389/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 * Initialize highlights and signs for use by netbeans (mostly obsolete)
2391 */
2392 static void
2393nb_init_graphics(void)
2394{
2395 static int did_init = FALSE;
2396
2397 if (!did_init)
2398 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002399 coloncmd(":highlight NBGuarded guibg=Cyan guifg=Black"
2400 " ctermbg=LightCyan ctermfg=Black");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 coloncmd(":sign define %d linehl=NBGuarded", GUARDED);
2402
2403 did_init = TRUE;
2404 }
2405}
2406
2407/*
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002408 * Convert key to netbeans name. This uses the global "mod_mask".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 */
2410 static void
2411netbeans_keyname(int key, char *buf)
2412{
2413 char *name = 0;
2414 char namebuf[2];
2415 int ctrl = 0;
2416 int shift = 0;
2417 int alt = 0;
2418
2419 if (mod_mask & MOD_MASK_CTRL)
2420 ctrl = 1;
2421 if (mod_mask & MOD_MASK_SHIFT)
2422 shift = 1;
2423 if (mod_mask & MOD_MASK_ALT)
2424 alt = 1;
2425
2426
2427 switch (key)
2428 {
2429 case K_F1: name = "F1"; break;
2430 case K_S_F1: name = "F1"; shift = 1; break;
2431 case K_F2: name = "F2"; break;
2432 case K_S_F2: name = "F2"; shift = 1; break;
2433 case K_F3: name = "F3"; break;
2434 case K_S_F3: name = "F3"; shift = 1; break;
2435 case K_F4: name = "F4"; break;
2436 case K_S_F4: name = "F4"; shift = 1; break;
2437 case K_F5: name = "F5"; break;
2438 case K_S_F5: name = "F5"; shift = 1; break;
2439 case K_F6: name = "F6"; break;
2440 case K_S_F6: name = "F6"; shift = 1; break;
2441 case K_F7: name = "F7"; break;
2442 case K_S_F7: name = "F7"; shift = 1; break;
2443 case K_F8: name = "F8"; break;
2444 case K_S_F8: name = "F8"; shift = 1; break;
2445 case K_F9: name = "F9"; break;
2446 case K_S_F9: name = "F9"; shift = 1; break;
2447 case K_F10: name = "F10"; break;
2448 case K_S_F10: name = "F10"; shift = 1; break;
2449 case K_F11: name = "F11"; break;
2450 case K_S_F11: name = "F11"; shift = 1; break;
2451 case K_F12: name = "F12"; break;
2452 case K_S_F12: name = "F12"; shift = 1; break;
2453 default:
2454 if (key >= ' ' && key <= '~')
2455 {
2456 /* Allow ASCII characters. */
2457 name = namebuf;
2458 namebuf[0] = key;
2459 namebuf[1] = NUL;
2460 }
2461 else
2462 name = "X";
2463 break;
2464 }
2465
2466 buf[0] = '\0';
2467 if (ctrl)
2468 strcat(buf, "C");
2469 if (shift)
2470 strcat(buf, "S");
2471 if (alt)
2472 strcat(buf, "M"); /* META */
2473 if (ctrl || shift || alt)
2474 strcat(buf, "-");
2475 strcat(buf, name);
2476}
2477
Bram Moolenaar67c53842010-05-22 18:28:27 +02002478#if defined(FEAT_BEVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479/*
2480 * Function to be called for balloon evaluation. Grabs the text under the
2481 * cursor and sends it to the debugger for evaluation. The debugger should
2482 * respond with a showBalloon command when there is a useful result.
2483 */
Bram Moolenaard62bec82005-03-07 22:56:57 +00002484 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485netbeans_beval_cb(
2486 BalloonEval *beval,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002487 int state UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488{
Bram Moolenaard62bec82005-03-07 22:56:57 +00002489 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 char_u *text;
Bram Moolenaard62bec82005-03-07 22:56:57 +00002491 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 int col;
Bram Moolenaard9462e32011-04-11 21:35:11 +02002493 char *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 char_u *p;
2495
2496 /* Don't do anything when 'ballooneval' is off, messages scrolled the
2497 * windows up or we have no connection. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002498 if (!p_beval || msg_scrolled > 0 || !NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 return;
2500
Bram Moolenaard62bec82005-03-07 22:56:57 +00002501 if (get_beval_info(beval, TRUE, &wp, &lnum, &text, &col) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 {
2503 /* Send debugger request. Only when the text is of reasonable
2504 * length. */
2505 if (text != NULL && text[0] != NUL && STRLEN(text) < MAXPATHL)
2506 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02002507 buf = (char *)alloc(MAXPATHL * 2 + 25);
2508 if (buf != NULL)
Bram Moolenaar009b2592004-10-24 19:18:58 +00002509 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02002510 p = nb_quote(text);
2511 if (p != NULL)
2512 {
2513 vim_snprintf(buf, MAXPATHL * 2 + 25,
2514 "0:balloonText=%d \"%s\"\n", r_cmdno, p);
2515 vim_free(p);
2516 }
2517 nbdebug(("EVT: %s", buf));
2518 nb_send(buf, "netbeans_beval_cb");
2519 vim_free(buf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 }
2522 vim_free(text);
2523 }
2524}
2525#endif
2526
2527/*
Bram Moolenaare0874f82016-01-24 20:36:41 +01002528 * Return TRUE when the netbeans connection is active.
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002529 */
2530 int
2531netbeans_active(void)
2532{
2533 return NETBEANS_OPEN;
2534}
2535
Bram Moolenaar67c53842010-05-22 18:28:27 +02002536/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 * Tell netbeans that the window was opened, ready for commands.
2538 */
2539 void
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02002540netbeans_open(char *params, int doabort)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541{
2542 char *cmd = "0:startupDone=0\n";
2543
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002544 if (NETBEANS_OPEN)
2545 {
2546 EMSG(_("E511: netbeans already connected"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02002550 if (netbeans_connect(params, doabort) != OK)
Bram Moolenaar67c53842010-05-22 18:28:27 +02002551 return;
Bram Moolenaard62bec82005-03-07 22:56:57 +00002552
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 nbdebug(("EVT: %s", cmd));
2554 nb_send(cmd, "netbeans_startup_done");
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002555
2556 /* update the screen after having added the gutter */
2557 changed_window_setting();
2558 update_screen(CLEAR);
2559 setcursor();
Bram Moolenaar96bcc5e2011-04-01 15:33:59 +02002560 cursor_on();
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002561 out_flush();
2562#ifdef FEAT_GUI
Bram Moolenaard54a6882010-08-09 22:49:00 +02002563 if (gui.in_use)
2564 {
2565 gui_update_cursor(TRUE, FALSE);
2566 gui_mch_flush();
2567 }
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002568#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569}
2570
Bram Moolenaar009b2592004-10-24 19:18:58 +00002571/*
2572 * Tell netbeans that we're exiting. This should be called right
2573 * before calling exit.
2574 */
2575 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002576netbeans_send_disconnect(void)
Bram Moolenaar009b2592004-10-24 19:18:58 +00002577{
2578 char buf[128];
2579
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002580 if (NETBEANS_OPEN)
Bram Moolenaar009b2592004-10-24 19:18:58 +00002581 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002582 sprintf(buf, "0:disconnect=%d\n", r_cmdno);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002583 nbdebug(("EVT: %s", buf));
2584 nb_send(buf, "netbeans_disconnect");
2585 }
2586}
2587
Bram Moolenaar3266c852016-04-30 18:07:05 +02002588#if defined(FEAT_EVAL) || defined(PROTO)
2589 int
2590set_ref_in_nb_channel(int copyID)
2591{
2592 int abort = FALSE;
2593 typval_T tv;
2594
2595 if (nb_channel != NULL)
2596 {
2597 tv.v_type = VAR_CHANNEL;
2598 tv.vval.v_channel = nb_channel;
2599 abort = set_ref_in_item(&tv, copyID, NULL, NULL);
2600 }
2601 return abort;
2602}
2603#endif
2604
Bram Moolenaar173c9852010-09-29 17:27:01 +02002605#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_W32) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606/*
2607 * Tell netbeans that the window was moved or resized.
2608 */
2609 void
2610netbeans_frame_moved(int new_x, int new_y)
2611{
2612 char buf[128];
2613
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002614 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 return;
2616
2617 sprintf(buf, "0:geometry=%d %d %d %d %d\n",
Bram Moolenaar89d40322006-08-29 15:30:07 +00002618 r_cmdno, (int)Columns, (int)Rows, new_x, new_y);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002619 /*nbdebug(("EVT: %s", buf)); happens too many times during a move */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 nb_send(buf, "netbeans_frame_moved");
2621}
2622#endif
2623
2624/*
Bram Moolenaar009b2592004-10-24 19:18:58 +00002625 * Tell netbeans the user opened or activated a file.
2626 */
2627 void
2628netbeans_file_activated(buf_T *bufp)
2629{
2630 int bufno = nb_getbufno(bufp);
2631 nbbuf_T *bp = nb_get_buf(bufno);
2632 char buffer[2*MAXPATHL];
2633 char_u *q;
2634
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002635 if (!NETBEANS_OPEN || !bufp->b_netbeans_file || dosetvisible)
Bram Moolenaar009b2592004-10-24 19:18:58 +00002636 return;
2637
2638 q = nb_quote(bufp->b_ffname);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002639 if (q == NULL || bp == NULL)
Bram Moolenaar009b2592004-10-24 19:18:58 +00002640 return;
2641
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002642 vim_snprintf(buffer, sizeof(buffer), "%d:fileOpened=%d \"%s\" %s %s\n",
Bram Moolenaar009b2592004-10-24 19:18:58 +00002643 bufno,
2644 bufno,
2645 (char *)q,
2646 "T", /* open in NetBeans */
2647 "F"); /* modified */
2648
2649 vim_free(q);
2650 nbdebug(("EVT: %s", buffer));
2651
2652 nb_send(buffer, "netbeans_file_opened");
2653}
2654
2655/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 * Tell netbeans the user opened a file.
2657 */
2658 void
Bram Moolenaar009b2592004-10-24 19:18:58 +00002659netbeans_file_opened(buf_T *bufp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660{
Bram Moolenaar009b2592004-10-24 19:18:58 +00002661 int bufno = nb_getbufno(bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 char buffer[2*MAXPATHL];
2663 char_u *q;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002664 nbbuf_T *bp = nb_get_buf(nb_getbufno(bufp));
2665 int bnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002667 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 return;
2669
Bram Moolenaar009b2592004-10-24 19:18:58 +00002670 q = nb_quote(bufp->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 if (q == NULL)
2672 return;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002673 if (bp != NULL)
2674 bnum = bufno;
2675 else
2676 bnum = 0;
2677
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002678 vim_snprintf(buffer, sizeof(buffer), "%d:fileOpened=%d \"%s\" %s %s\n",
Bram Moolenaar009b2592004-10-24 19:18:58 +00002679 bnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 0,
2681 (char *)q,
2682 "T", /* open in NetBeans */
2683 "F"); /* modified */
2684
2685 vim_free(q);
2686 nbdebug(("EVT: %s", buffer));
2687
2688 nb_send(buffer, "netbeans_file_opened");
Bram Moolenaar009b2592004-10-24 19:18:58 +00002689 if (p_acd && vim_chdirfile(bufp->b_ffname) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 shorten_fnames(TRUE);
2691}
2692
2693/*
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00002694 * Tell netbeans that a file was deleted or wiped out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 */
2696 void
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00002697netbeans_file_killed(buf_T *bufp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698{
2699 int bufno = nb_getbufno(bufp);
2700 nbbuf_T *nbbuf = nb_get_buf(bufno);
2701 char buffer[2*MAXPATHL];
2702
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002703 if (!NETBEANS_OPEN || bufno == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 return;
2705
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00002706 nbdebug(("netbeans_file_killed:\n"));
2707 nbdebug((" Killing bufno: %d", bufno));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708
Bram Moolenaar89d40322006-08-29 15:30:07 +00002709 sprintf(buffer, "%d:killed=%d\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710
2711 nbdebug(("EVT: %s", buffer));
2712
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00002713 nb_send(buffer, "netbeans_file_killed");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714
2715 if (nbbuf != NULL)
2716 nbbuf->bufp = NULL;
2717}
2718
2719/*
2720 * Get a pointer to the Netbeans buffer for Vim buffer "bufp".
2721 * Return NULL if there is no such buffer or changes are not to be reported.
2722 * Otherwise store the buffer number in "*bufnop".
2723 */
2724 static nbbuf_T *
2725nb_bufp2nbbuf_fire(buf_T *bufp, int *bufnop)
2726{
2727 int bufno;
2728 nbbuf_T *nbbuf;
2729
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002730 if (!NETBEANS_OPEN || !netbeansFireChanges)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 return NULL; /* changes are not reported at all */
2732
2733 bufno = nb_getbufno(bufp);
2734 if (bufno <= 0)
2735 return NULL; /* file is not known to NetBeans */
2736
2737 nbbuf = nb_get_buf(bufno);
2738 if (nbbuf != NULL && !nbbuf->fireChanges)
2739 return NULL; /* changes in this buffer are not reported */
2740
2741 *bufnop = bufno;
2742 return nbbuf;
2743}
2744
2745/*
2746 * Tell netbeans the user inserted some text.
2747 */
2748 void
2749netbeans_inserted(
2750 buf_T *bufp,
2751 linenr_T linenr,
2752 colnr_T col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 char_u *txt,
2754 int newlen)
2755{
2756 char_u *buf;
2757 int bufno;
2758 nbbuf_T *nbbuf;
2759 pos_T pos;
2760 long off;
2761 char_u *p;
2762 char_u *newtxt;
2763
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002764 if (!NETBEANS_OPEN)
2765 return;
2766
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
2768 if (nbbuf == NULL)
2769 return;
2770
Bram Moolenaar009b2592004-10-24 19:18:58 +00002771 /* Don't mark as modified for initial read */
2772 if (nbbuf->insertDone)
2773 nbbuf->modified = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774
2775 pos.lnum = linenr;
2776 pos.col = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 off = pos2off(bufp, &pos);
2778
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 /* send the "insert" EVT */
2780 newtxt = alloc(newlen + 1);
Bram Moolenaarb6356332005-07-18 21:40:44 +00002781 vim_strncpy(newtxt, txt, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 p = nb_quote(newtxt);
2783 if (p != NULL)
2784 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002785 buf = alloc(128 + 2*newlen);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002786 sprintf((char *)buf, "%d:insert=%d %ld \"%s\"\n",
2787 bufno, r_cmdno, off, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 nbdebug(("EVT: %s", buf));
2789 nb_send((char *)buf, "netbeans_inserted");
Bram Moolenaar009b2592004-10-24 19:18:58 +00002790 vim_free(p);
2791 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 vim_free(newtxt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794}
2795
2796/*
2797 * Tell netbeans some bytes have been removed.
2798 */
2799 void
2800netbeans_removed(
2801 buf_T *bufp,
2802 linenr_T linenr,
2803 colnr_T col,
2804 long len)
2805{
2806 char_u buf[128];
2807 int bufno;
2808 nbbuf_T *nbbuf;
2809 pos_T pos;
2810 long off;
2811
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002812 if (!NETBEANS_OPEN)
2813 return;
2814
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
2816 if (nbbuf == NULL)
2817 return;
2818
2819 if (len < 0)
2820 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002821 nbdebug(("Negative len %ld in netbeans_removed()!\n", len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 return;
2823 }
2824
2825 nbbuf->modified = 1;
2826
2827 pos.lnum = linenr;
2828 pos.col = col;
2829
2830 off = pos2off(bufp, &pos);
2831
Bram Moolenaar89d40322006-08-29 15:30:07 +00002832 sprintf((char *)buf, "%d:remove=%d %ld %ld\n", bufno, r_cmdno, off, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 nbdebug(("EVT: %s", buf));
2834 nb_send((char *)buf, "netbeans_removed");
2835}
2836
2837/*
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01002838 * Send netbeans an unmodified command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002841netbeans_unmodified(buf_T *bufp UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842{
Bram Moolenaar09092152010-08-08 16:38:42 +02002843 /* This is a no-op, because NetBeans considers a buffer modified
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 * even when all changes have been undone. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845}
2846
2847/*
2848 * Send a button release event back to netbeans. Its up to netbeans
2849 * to decide what to do (if anything) with this event.
2850 */
2851 void
2852netbeans_button_release(int button)
2853{
2854 char buf[128];
2855 int bufno;
2856
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002857 if (!NETBEANS_OPEN)
2858 return;
2859
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 bufno = nb_getbufno(curbuf);
2861
2862 if (bufno >= 0 && curwin != NULL && curwin->w_buffer == curbuf)
2863 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002864 int col = mouse_col - W_WINCOL(curwin)
Bram Moolenaar67c53842010-05-22 18:28:27 +02002865 - ((curwin->w_p_nu || curwin->w_p_rnu) ? 9 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 long off = pos2off(curbuf, &curwin->w_cursor);
2867
2868 /* sync the cursor position */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002869 sprintf(buf, "%d:newDotAndMark=%d %ld %ld\n", bufno, r_cmdno, off, off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 nbdebug(("EVT: %s", buf));
2871 nb_send(buf, "netbeans_button_release[newDotAndMark]");
2872
Bram Moolenaar89d40322006-08-29 15:30:07 +00002873 sprintf(buf, "%d:buttonRelease=%d %d %ld %d\n", bufno, r_cmdno,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 button, (long)curwin->w_cursor.lnum, col);
2875 nbdebug(("EVT: %s", buf));
2876 nb_send(buf, "netbeans_button_release");
2877 }
2878}
2879
2880
2881/*
Bram Moolenaar143c38c2007-05-10 16:41:10 +00002882 * Send a keypress event back to netbeans. This usually simulates some
Bram Moolenaar009b2592004-10-24 19:18:58 +00002883 * kind of function key press. This function operates on a key code.
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002884 * Return TRUE when the key was sent, FALSE when the command has been
2885 * postponed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 */
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002887 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888netbeans_keycommand(int key)
2889{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 char keyName[60];
Bram Moolenaar009b2592004-10-24 19:18:58 +00002891
2892 netbeans_keyname(key, keyName);
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002893 return netbeans_keystring((char_u *)keyName);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002894}
2895
2896
2897/*
Bram Moolenaar143c38c2007-05-10 16:41:10 +00002898 * Send a keypress event back to netbeans. This usually simulates some
Bram Moolenaar009b2592004-10-24 19:18:58 +00002899 * kind of function key press. This function operates on a key string.
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002900 * Return TRUE when the key was sent, FALSE when the command has been
2901 * postponed.
Bram Moolenaar009b2592004-10-24 19:18:58 +00002902 */
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002903 static int
2904netbeans_keystring(char_u *keyName)
Bram Moolenaar009b2592004-10-24 19:18:58 +00002905{
2906 char buf[2*MAXPATHL];
2907 int bufno = nb_getbufno(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 long off;
2909 char_u *q;
2910
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002911 if (!NETBEANS_OPEN)
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002912 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 if (bufno == -1)
2915 {
2916 nbdebug(("got keycommand for non-NetBeans buffer, opening...\n"));
2917 q = curbuf->b_ffname == NULL ? (char_u *)""
2918 : nb_quote(curbuf->b_ffname);
2919 if (q == NULL)
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002920 return TRUE;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002921 vim_snprintf(buf, sizeof(buf), "0:fileOpened=%d \"%s\" %s %s\n", 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 q,
2923 "T", /* open in NetBeans */
2924 "F"); /* modified */
2925 if (curbuf->b_ffname != NULL)
2926 vim_free(q);
2927 nbdebug(("EVT: %s", buf));
2928 nb_send(buf, "netbeans_keycommand");
2929
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002930 postpone_keycommand(keyName);
2931 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 }
2933
2934 /* sync the cursor position */
2935 off = pos2off(curbuf, &curwin->w_cursor);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002936 sprintf(buf, "%d:newDotAndMark=%d %ld %ld\n", bufno, r_cmdno, off, off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 nbdebug(("EVT: %s", buf));
2938 nb_send(buf, "netbeans_keycommand");
2939
2940 /* To work on Win32 you must apply patch to ExtEditor module
2941 * from ExtEdCaret.java.diff - make EVT_newDotAndMark handler
2942 * more synchronous
2943 */
2944
2945 /* now send keyCommand event */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002946 vim_snprintf(buf, sizeof(buf), "%d:keyCommand=%d \"%s\"\n",
Bram Moolenaar89d40322006-08-29 15:30:07 +00002947 bufno, r_cmdno, keyName);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 nbdebug(("EVT: %s", buf));
2949 nb_send(buf, "netbeans_keycommand");
2950
2951 /* New: do both at once and include the lnum/col. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002952 vim_snprintf(buf, sizeof(buf), "%d:keyAtPos=%d \"%s\" %ld %ld/%ld\n",
Bram Moolenaar89d40322006-08-29 15:30:07 +00002953 bufno, r_cmdno, keyName,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 off, (long)curwin->w_cursor.lnum, (long)curwin->w_cursor.col);
2955 nbdebug(("EVT: %s", buf));
2956 nb_send(buf, "netbeans_keycommand");
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002957 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958}
2959
2960
2961/*
2962 * Send a save event to netbeans.
2963 */
2964 void
2965netbeans_save_buffer(buf_T *bufp)
2966{
2967 char_u buf[64];
2968 int bufno;
2969 nbbuf_T *nbbuf;
2970
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002971 if (!NETBEANS_OPEN)
2972 return;
2973
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
2975 if (nbbuf == NULL)
2976 return;
2977
2978 nbbuf->modified = 0;
2979
Bram Moolenaar89d40322006-08-29 15:30:07 +00002980 sprintf((char *)buf, "%d:save=%d\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 nbdebug(("EVT: %s", buf));
2982 nb_send((char *)buf, "netbeans_save_buffer");
2983}
2984
2985
2986/*
2987 * Send remove command to netbeans (this command has been turned off).
2988 */
2989 void
2990netbeans_deleted_all_lines(buf_T *bufp)
2991{
2992 char_u buf[64];
2993 int bufno;
2994 nbbuf_T *nbbuf;
2995
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002996 if (!NETBEANS_OPEN)
2997 return;
2998
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3000 if (nbbuf == NULL)
3001 return;
3002
Bram Moolenaar009b2592004-10-24 19:18:58 +00003003 /* Don't mark as modified for initial read */
3004 if (nbbuf->insertDone)
3005 nbbuf->modified = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006
Bram Moolenaar89d40322006-08-29 15:30:07 +00003007 sprintf((char *)buf, "%d:remove=%d 0 -1\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 nbdebug(("EVT(suppressed): %s", buf));
3009/* nb_send(buf, "netbeans_deleted_all_lines"); */
3010}
3011
3012
3013/*
3014 * See if the lines are guarded. The top and bot parameters are from
3015 * u_savecommon(), these are the line above the change and the line below the
3016 * change.
3017 */
3018 int
3019netbeans_is_guarded(linenr_T top, linenr_T bot)
3020{
3021 signlist_T *p;
3022 int lnum;
3023
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003024 if (!NETBEANS_OPEN)
3025 return FALSE;
3026
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 for (p = curbuf->b_signlist; p != NULL; p = p->next)
3028 if (p->id >= GUARDEDOFFSET)
3029 for (lnum = top + 1; lnum < bot; lnum++)
3030 if (lnum == p->lnum)
3031 return TRUE;
3032
3033 return FALSE;
3034}
3035
Bram Moolenaar173c9852010-09-29 17:27:01 +02003036#if defined(FEAT_GUI_X11) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037/*
3038 * We have multiple signs to draw at the same location. Draw the
3039 * multi-sign indicator instead. This is the Motif version.
3040 */
3041 void
3042netbeans_draw_multisign_indicator(int row)
3043{
3044 int i;
3045 int y;
3046 int x;
3047
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003048 if (!NETBEANS_OPEN)
3049 return;
3050
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 x = 0;
3052 y = row * gui.char_height + 2;
3053
3054 for (i = 0; i < gui.char_height - 3; i++)
3055 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y++);
3056
3057 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+0, y);
3058 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3059 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+4, y++);
3060 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+1, y);
3061 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3062 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+3, y++);
3063 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3064}
Bram Moolenaar173c9852010-09-29 17:27:01 +02003065#endif /* FEAT_GUI_X11 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066
Bram Moolenaar64404472010-06-26 06:24:45 +02003067#if defined(FEAT_GUI_GTK) && !defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068/*
3069 * We have multiple signs to draw at the same location. Draw the
3070 * multi-sign indicator instead. This is the GTK/Gnome version.
3071 */
3072 void
3073netbeans_draw_multisign_indicator(int row)
3074{
3075 int i;
3076 int y;
3077 int x;
Bram Moolenaar98921892016-02-23 17:14:37 +01003078#if GTK_CHECK_VERSION(3,0,0)
3079 cairo_t *cr = NULL;
3080#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 GdkDrawable *drawable = gui.drawarea->window;
Bram Moolenaar98921892016-02-23 17:14:37 +01003082#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003084 if (!NETBEANS_OPEN)
3085 return;
3086
Bram Moolenaar98921892016-02-23 17:14:37 +01003087#if GTK_CHECK_VERSION(3,0,0)
3088 cr = cairo_create(gui.surface);
3089 {
3090 GdkVisual *visual = NULL;
3091 guint32 r_mask, g_mask, b_mask;
3092 gint r_shift, g_shift, b_shift;
3093
3094 visual = gdk_window_get_visual(gtk_widget_get_window(gui.drawarea));
3095 if (visual != NULL)
3096 {
3097 gdk_visual_get_red_pixel_details(visual, &r_mask, &r_shift, NULL);
3098 gdk_visual_get_green_pixel_details(visual, &g_mask, &g_shift, NULL);
3099 gdk_visual_get_blue_pixel_details(visual, &b_mask, &b_shift, NULL);
3100
3101 cairo_set_source_rgb(cr,
3102 ((gui.fgcolor->red & r_mask) >> r_shift) / 255.0,
3103 ((gui.fgcolor->green & g_mask) >> g_shift) / 255.0,
3104 ((gui.fgcolor->blue & b_mask) >> b_shift) / 255.0);
3105 }
3106 }
3107#endif
3108
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 x = 0;
3110 y = row * gui.char_height + 2;
3111
3112 for (i = 0; i < gui.char_height - 3; i++)
Bram Moolenaar98921892016-02-23 17:14:37 +01003113#if GTK_CHECK_VERSION(3,0,0)
3114 cairo_rectangle(cr, x+2, y++, 1, 1);
3115#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 gdk_draw_point(drawable, gui.text_gc, x+2, y++);
Bram Moolenaar98921892016-02-23 17:14:37 +01003117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118
Bram Moolenaar98921892016-02-23 17:14:37 +01003119#if GTK_CHECK_VERSION(3,0,0)
3120 cairo_rectangle(cr, x+0, y, 1, 1);
3121 cairo_rectangle(cr, x+2, y, 1, 1);
3122 cairo_rectangle(cr, x+4, y++, 1, 1);
3123 cairo_rectangle(cr, x+1, y, 1, 1);
3124 cairo_rectangle(cr, x+2, y, 1, 1);
3125 cairo_rectangle(cr, x+3, y++, 1, 1);
3126 cairo_rectangle(cr, x+2, y, 1, 1);
3127#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 gdk_draw_point(drawable, gui.text_gc, x+0, y);
3129 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3130 gdk_draw_point(drawable, gui.text_gc, x+4, y++);
3131 gdk_draw_point(drawable, gui.text_gc, x+1, y);
3132 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3133 gdk_draw_point(drawable, gui.text_gc, x+3, y++);
3134 gdk_draw_point(drawable, gui.text_gc, x+2, y);
Bram Moolenaar98921892016-02-23 17:14:37 +01003135#endif
3136
3137#if GTK_CHECK_VERSION(3,0,0)
3138 cairo_destroy(cr);
3139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140}
3141#endif /* FEAT_GUI_GTK */
3142
3143/*
3144 * If the mouse is clicked in the gutter of a line with multiple
3145 * annotations, cycle through the set of signs.
3146 */
3147 void
3148netbeans_gutter_click(linenr_T lnum)
3149{
3150 signlist_T *p;
3151
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003152 if (!NETBEANS_OPEN)
3153 return;
3154
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 for (p = curbuf->b_signlist; p != NULL; p = p->next)
3156 {
3157 if (p->lnum == lnum && p->next && p->next->lnum == lnum)
3158 {
3159 signlist_T *tail;
3160
3161 /* remove "p" from list, reinsert it at the tail of the sublist */
3162 if (p->prev)
3163 p->prev->next = p->next;
3164 else
3165 curbuf->b_signlist = p->next;
3166 p->next->prev = p->prev;
3167 /* now find end of sublist and insert p */
3168 for (tail = p->next;
3169 tail->next && tail->next->lnum == lnum
3170 && tail->next->id < GUARDEDOFFSET;
3171 tail = tail->next)
3172 ;
3173 /* tail now points to last entry with same lnum (except
3174 * that "guarded" annotations are always last) */
3175 p->next = tail->next;
3176 if (tail->next)
3177 tail->next->prev = p;
3178 p->prev = tail;
3179 tail->next = p;
3180 update_debug_sign(curbuf, lnum);
3181 break;
3182 }
3183 }
3184}
3185
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186/*
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003187 * Add a sign of the requested type at the requested location.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 *
3189 * Reverse engineering:
3190 * Apparently an annotation is defined the first time it is used in a buffer.
3191 * When the same annotation is used in two buffers, the second time we do not
3192 * need to define a new sign name but reuse the existing one. But since the
3193 * ID number used in the second buffer starts counting at one again, a mapping
3194 * is made from the ID specifically for the buffer to the global sign name
3195 * (which is a number).
3196 *
3197 * globalsignmap[] stores the signs that have been defined globally.
3198 * buf->signmapused[] maps buffer-local annotation IDs to an index in
3199 * globalsignmap[].
3200 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 static void
3202addsigntype(
3203 nbbuf_T *buf,
3204 int typeNum,
3205 char_u *typeName,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003206 char_u *tooltip UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 char_u *glyphFile,
Bram Moolenaar67c53842010-05-22 18:28:27 +02003208 char_u *fg,
3209 char_u *bg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 int i, j;
Bram Moolenaar67c53842010-05-22 18:28:27 +02003212 int use_fg = (*fg && STRCMP(fg, "none") != 0);
3213 int use_bg = (*bg && STRCMP(bg, "none") != 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214
3215 for (i = 0; i < globalsignmapused; i++)
3216 if (STRCMP(typeName, globalsignmap[i]) == 0)
3217 break;
3218
3219 if (i == globalsignmapused) /* not found; add it to global map */
3220 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003221 nbdebug(("DEFINEANNOTYPE(%d,%s,%s,%s,%s,%s)\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 typeNum, typeName, tooltip, glyphFile, fg, bg));
3223 if (use_fg || use_bg)
3224 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003225 char fgbuf[2 * (8 + MAX_COLOR_LENGTH) + 1];
3226 char bgbuf[2 * (8 + MAX_COLOR_LENGTH) + 1];
3227 char *ptr;
3228 int value;
3229
3230 value = strtol((char *)fg, &ptr, 10);
3231 if (ptr != (char *)fg)
3232 sprintf(fgbuf, "guifg=#%06x", value & 0xFFFFFF);
3233 else
3234 sprintf(fgbuf, "guifg=%s ctermfg=%s", fg, fg);
3235
3236 value = strtol((char *)bg, &ptr, 10);
3237 if (ptr != (char *)bg)
3238 sprintf(bgbuf, "guibg=#%06x", value & 0xFFFFFF);
3239 else
3240 sprintf(bgbuf, "guibg=%s ctermbg=%s", bg, bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241
3242 coloncmd(":highlight NB_%s %s %s", typeName, (use_fg) ? fgbuf : "",
3243 (use_bg) ? bgbuf : "");
3244 if (*glyphFile == NUL)
3245 /* no glyph, line highlighting only */
3246 coloncmd(":sign define %d linehl=NB_%s", i + 1, typeName);
3247 else if (vim_strsize(glyphFile) <= 2)
3248 /* one- or two-character glyph name, use as text glyph with
3249 * texthl */
3250 coloncmd(":sign define %d text=%s texthl=NB_%s", i + 1,
3251 glyphFile, typeName);
3252 else
3253 /* glyph, line highlighting */
3254 coloncmd(":sign define %d icon=%s linehl=NB_%s", i + 1,
3255 glyphFile, typeName);
3256 }
3257 else
3258 /* glyph, no line highlighting */
3259 coloncmd(":sign define %d icon=%s", i + 1, glyphFile);
3260
3261 if (STRCMP(typeName,"CurrentPC") == 0)
3262 curPCtype = typeNum;
3263
3264 if (globalsignmapused == globalsignmaplen)
3265 {
3266 if (globalsignmaplen == 0) /* first allocation */
3267 {
3268 globalsignmaplen = 20;
3269 globalsignmap = (char **)alloc_clear(globalsignmaplen*sizeof(char *));
3270 }
3271 else /* grow it */
3272 {
3273 int incr;
3274 int oldlen = globalsignmaplen;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003275 char **t_globalsignmap = globalsignmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276
3277 globalsignmaplen *= 2;
3278 incr = globalsignmaplen - oldlen;
3279 globalsignmap = (char **)vim_realloc(globalsignmap,
3280 globalsignmaplen * sizeof(char *));
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003281 if (globalsignmap == NULL)
3282 {
3283 vim_free(t_globalsignmap);
3284 globalsignmaplen = 0;
3285 return;
3286 }
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003287 vim_memset(globalsignmap + oldlen, 0, incr * sizeof(char *));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 }
3289 }
3290
3291 globalsignmap[i] = (char *)typeName;
3292 globalsignmapused = i + 1;
3293 }
3294
3295 /* check local map; should *not* be found! */
3296 for (j = 0; j < buf->signmapused; j++)
3297 if (buf->signmap[j] == i + 1)
3298 return;
3299
3300 /* add to local map */
3301 if (buf->signmapused == buf->signmaplen)
3302 {
3303 if (buf->signmaplen == 0) /* first allocation */
3304 {
3305 buf->signmaplen = 5;
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003306 buf->signmap = (int *)alloc_clear(buf->signmaplen * sizeof(int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 }
3308 else /* grow it */
3309 {
3310 int incr;
3311 int oldlen = buf->signmaplen;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003312 int *t_signmap = buf->signmap;
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003313
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 buf->signmaplen *= 2;
3315 incr = buf->signmaplen - oldlen;
3316 buf->signmap = (int *)vim_realloc(buf->signmap,
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003317 buf->signmaplen * sizeof(int));
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003318 if (buf->signmap == NULL)
3319 {
3320 vim_free(t_signmap);
3321 buf->signmaplen = 0;
3322 return;
3323 }
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003324 vim_memset(buf->signmap + oldlen, 0, incr * sizeof(int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 }
3326 }
3327
3328 buf->signmap[buf->signmapused++] = i + 1;
3329
3330}
3331
3332
3333/*
3334 * See if we have the requested sign type in the buffer.
3335 */
3336 static int
3337mapsigntype(nbbuf_T *buf, int localsigntype)
3338{
3339 if (--localsigntype >= 0 && localsigntype < buf->signmapused)
3340 return buf->signmap[localsigntype];
3341
3342 return 0;
3343}
3344
3345
3346/*
3347 * Compute length of buffer, don't print anything.
3348 */
3349 static long
3350get_buf_size(buf_T *bufp)
3351{
3352 linenr_T lnum;
3353 long char_count = 0;
3354 int eol_size;
3355 long last_check = 100000L;
3356
3357 if (bufp->b_ml.ml_flags & ML_EMPTY)
3358 return 0;
3359 else
3360 {
3361 if (get_fileformat(bufp) == EOL_DOS)
3362 eol_size = 2;
3363 else
3364 eol_size = 1;
3365 for (lnum = 1; lnum <= bufp->b_ml.ml_line_count; ++lnum)
3366 {
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00003367 char_count += (long)STRLEN(ml_get_buf(bufp, lnum, FALSE))
3368 + eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 /* Check for a CTRL-C every 100000 characters */
3370 if (char_count > last_check)
3371 {
3372 ui_breakcheck();
3373 if (got_int)
3374 return char_count;
3375 last_check = char_count + 100000L;
3376 }
3377 }
3378 /* Correction for when last line doesn't have an EOL. */
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003379 if (!bufp->b_p_eol && (bufp->b_p_bin || !bufp->b_p_fixeol))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 char_count -= eol_size;
3381 }
3382
3383 return char_count;
3384}
3385
3386/*
3387 * Convert character offset to lnum,col
3388 */
3389 static pos_T *
3390off2pos(buf_T *buf, long offset)
3391{
3392 linenr_T lnum;
3393 static pos_T pos;
3394
3395 pos.lnum = 0;
3396 pos.col = 0;
3397#ifdef FEAT_VIRTUALEDIT
3398 pos.coladd = 0;
3399#endif
3400
3401 if (!(buf->b_ml.ml_flags & ML_EMPTY))
3402 {
3403 if ((lnum = ml_find_line_or_offset(buf, (linenr_T)0, &offset)) < 0)
3404 return NULL;
3405 pos.lnum = lnum;
3406 pos.col = offset;
3407 }
3408
3409 return &pos;
3410}
3411
3412/*
3413 * Convert an argument in the form "1234" to an offset and compute the
3414 * lnum/col from it. Convert an argument in the form "123/12" directly to a
3415 * lnum/col.
3416 * "argp" is advanced to after the argument.
3417 * Return a pointer to the position, NULL if something is wrong.
3418 */
3419 static pos_T *
3420get_off_or_lnum(buf_T *buf, char_u **argp)
3421{
3422 static pos_T mypos;
3423 long off;
3424
3425 off = strtol((char *)*argp, (char **)argp, 10);
3426 if (**argp == '/')
3427 {
3428 mypos.lnum = (linenr_T)off;
3429 ++*argp;
3430 mypos.col = strtol((char *)*argp, (char **)argp, 10);
3431#ifdef FEAT_VIRTUALEDIT
3432 mypos.coladd = 0;
3433#endif
3434 return &mypos;
3435 }
3436 return off2pos(buf, off);
3437}
3438
3439
3440/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003441 * Convert (lnum,col) to byte offset in the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 */
3443 static long
3444pos2off(buf_T *buf, pos_T *pos)
3445{
3446 long offset = 0;
3447
3448 if (!(buf->b_ml.ml_flags & ML_EMPTY))
3449 {
3450 if ((offset = ml_find_line_or_offset(buf, pos->lnum, 0)) < 0)
3451 return 0;
3452 offset += pos->col;
3453 }
3454
3455 return offset;
3456}
3457
3458
Bram Moolenaar009b2592004-10-24 19:18:58 +00003459/*
3460 * This message is printed after NetBeans opens a new file. Its
3461 * similar to the message readfile() uses, but since NetBeans
3462 * doesn't normally call readfile, we do our own.
3463 */
3464 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003465print_read_msg(nbbuf_T *buf)
Bram Moolenaar009b2592004-10-24 19:18:58 +00003466{
3467 int lnum = buf->bufp->b_ml.ml_line_count;
Bram Moolenaar914703b2010-05-31 21:59:46 +02003468 off_t nchars = buf->bufp->b_orig_size;
Bram Moolenaar009b2592004-10-24 19:18:58 +00003469 char_u c;
3470
3471 msg_add_fname(buf->bufp, buf->bufp->b_ffname);
3472 c = FALSE;
3473
3474 if (buf->bufp->b_p_ro)
3475 {
3476 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]"));
3477 c = TRUE;
3478 }
3479 if (!buf->bufp->b_start_eol)
3480 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003481 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]")
3482 : _("[Incomplete last line]"));
Bram Moolenaar009b2592004-10-24 19:18:58 +00003483 c = TRUE;
3484 }
3485 msg_add_lines(c, (long)lnum, nchars);
3486
3487 /* Now display it */
3488 vim_free(keep_msg);
3489 keep_msg = NULL;
3490 msg_scrolled_ign = TRUE;
3491 msg_trunc_attr(IObuff, FALSE, 0);
3492 msg_scrolled_ign = FALSE;
3493}
3494
3495
3496/*
Bram Moolenaar67c53842010-05-22 18:28:27 +02003497 * Print a message after NetBeans writes the file. This message should be
3498 * identical to the standard message a non-netbeans user would see when
3499 * writing a file.
Bram Moolenaar009b2592004-10-24 19:18:58 +00003500 */
3501 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003502print_save_msg(nbbuf_T *buf, off_t nchars)
Bram Moolenaar009b2592004-10-24 19:18:58 +00003503{
3504 char_u c;
3505 char_u *p;
3506
3507 if (nchars >= 0)
3508 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003509 /* put fname in IObuff with quotes */
3510 msg_add_fname(buf->bufp, buf->bufp->b_ffname);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003511 c = FALSE;
3512
3513 msg_add_lines(c, buf->bufp->b_ml.ml_line_count,
Bram Moolenaar914703b2010-05-31 21:59:46 +02003514 buf->bufp->b_orig_size);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003515
3516 vim_free(keep_msg);
3517 keep_msg = NULL;
3518 msg_scrolled_ign = TRUE;
3519 p = msg_trunc_attr(IObuff, FALSE, 0);
3520 if ((msg_scrolled && !need_wait_return) || !buf->initDone)
3521 {
3522 /* Need to repeat the message after redrawing when:
3523 * - When reading from stdin (the screen will be cleared next).
3524 * - When restart_edit is set (otherwise there will be a delay
3525 * before redrawing).
3526 * - When the screen was scrolled but there is no wait-return
3527 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003528 set_keep_msg(p, 0);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003529 }
3530 msg_scrolled_ign = FALSE;
3531 /* add_to_input_buf((char_u *)"\f", 1); */
3532 }
3533 else
3534 {
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003535 char_u msgbuf[IOSIZE];
Bram Moolenaar009b2592004-10-24 19:18:58 +00003536
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003537 vim_snprintf((char *)msgbuf, IOSIZE,
3538 _("E505: %s is read-only (add ! to override)"), IObuff);
3539 nbdebug((" %s\n", msgbuf));
3540 emsg(msgbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003541 }
3542}
3543
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544#endif /* defined(FEAT_NETBEANS_INTG) */