blob: 14f274df1098a2c6062bef134a874888795b346d [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 Moolenaarb26e6322010-05-22 21:34:09 +020030/* TODO: when should this not be defined? */
31#define INET_SOCKETS
32
Bram Moolenaar071d4272004-06-13 20:20:40 +000033/* Note: when making changes here also adjust configure.in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000034#ifdef WIN32
35# ifdef DEBUG
36# include <tchar.h> /* for _T definition for TRACEn macros */
37# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000038/* WinSock API is separated from C API, thus we can't use read(), write(),
39 * errno... */
Bram Moolenaarc39125d2010-05-23 12:06:58 +020040# define SOCK_ERRNO errno = WSAGetLastError()
Bram Moolenaarbd42a0f2009-06-16 14:57:26 +000041# undef ECONNREFUSED
Bram Moolenaar071d4272004-06-13 20:20:40 +000042# define ECONNREFUSED WSAECONNREFUSED
43# ifdef EINTR
44# undef EINTR
45# endif
46# define EINTR WSAEINTR
47# define sock_write(sd, buf, len) send(sd, buf, len, 0)
48# define sock_read(sd, buf, len) recv(sd, buf, len, 0)
49# define sock_close(sd) closesocket(sd)
50# define sleep(t) Sleep(t*1000) /* WinAPI Sleep() accepts milliseconds */
51#else
Bram Moolenaarb26e6322010-05-22 21:34:09 +020052# ifdef INET_SOCKETS
53# include <netdb.h>
54# include <netinet/in.h>
55# else
56# include <sys/un.h>
57# endif
58
Bram Moolenaar071d4272004-06-13 20:20:40 +000059# include <sys/socket.h>
60# ifdef HAVE_LIBGEN_H
61# include <libgen.h>
62# endif
Bram Moolenaarc39125d2010-05-23 12:06:58 +020063# define SOCK_ERRNO
Bram Moolenaar071d4272004-06-13 20:20:40 +000064# define sock_write(sd, buf, len) write(sd, buf, len)
65# define sock_read(sd, buf, len) read(sd, buf, len)
66# define sock_close(sd) close(sd)
67#endif
68
69#include "version.h"
70
Bram Moolenaar071d4272004-06-13 20:20:40 +000071#define GUARDED 10000 /* typenr for "guarded" annotation */
72#define GUARDEDOFFSET 1000000 /* base for "guarded" sign id's */
Bram Moolenaar67c53842010-05-22 18:28:27 +020073#define MAX_COLOR_LENGTH 32 /* max length of color name in defineAnnoType */
Bram Moolenaar071d4272004-06-13 20:20:40 +000074
75/* The first implementation (working only with Netbeans) returned "1.1". The
76 * protocol implemented here also supports A-A-P. */
Bram Moolenaar67c53842010-05-22 18:28:27 +020077static char *ExtEdProtocolVersion = "2.5";
Bram Moolenaar071d4272004-06-13 20:20:40 +000078
79static long pos2off __ARGS((buf_T *, pos_T *));
80static pos_T *off2pos __ARGS((buf_T *, long));
81static pos_T *get_off_or_lnum __ARGS((buf_T *buf, char_u **argp));
82static long get_buf_size __ARGS((buf_T *));
Bram Moolenaar8065d7f2010-01-19 15:13:14 +010083static int netbeans_keystring __ARGS((char_u *keystr));
84static void postpone_keycommand __ARGS((char_u *keystr));
Bram Moolenaar009b2592004-10-24 19:18:58 +000085static void special_keys __ARGS((char_u *args));
Bram Moolenaar071d4272004-06-13 20:20:40 +000086
Bram Moolenaarb26e6322010-05-22 21:34:09 +020087static int netbeans_connect __ARGS((char *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +000088static int getConnInfo __ARGS((char *file, char **host, char **port, char **password));
89
90static void nb_init_graphics __ARGS((void));
91static void coloncmd __ARGS((char *cmd, ...));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000092static void nb_set_curbuf __ARGS((buf_T *buf));
Bram Moolenaar173c9852010-09-29 17:27:01 +020093#ifdef FEAT_GUI_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +000094static void messageFromNetbeans __ARGS((XtPointer, int *, XtInputId *));
95#endif
96#ifdef FEAT_GUI_GTK
97static void messageFromNetbeans __ARGS((gpointer, gint, GdkInputCondition));
98#endif
99static void nb_parse_cmd __ARGS((char_u *));
100static int nb_do_cmd __ARGS((int, char_u *, int, int, char_u *));
101static void nb_send __ARGS((char *buf, char *fun));
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200102static void nb_free __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103
Bram Moolenaar67c53842010-05-22 18:28:27 +0200104/* TRUE when netbeans is running with a GUI. */
105#ifdef FEAT_GUI
106# define NB_HAS_GUI (gui.in_use || gui.starting)
107#endif
108
Bram Moolenaare0874f82016-01-24 20:36:41 +0100109static sock_T nbsock = -1; /* socket fd for Netbeans connection */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200110#define NETBEANS_OPEN (nbsock != -1)
111
Bram Moolenaar173c9852010-09-29 17:27:01 +0200112#ifdef FEAT_GUI_X11
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200113static XtInputId inputHandler = (XtInputId)NULL; /* Cookie for input */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114#endif
115#ifdef FEAT_GUI_GTK
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200116static gint inputHandler = 0; /* Cookie for input */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117#endif
118#ifdef FEAT_GUI_W32
119static int inputHandler = -1; /* simply ret.value of WSAAsyncSelect() */
120extern HWND s_hwnd; /* Gvim's Window handle */
121#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +0000122static int r_cmdno; /* current command number for reply */
Bram Moolenaar009b2592004-10-24 19:18:58 +0000123static int dosetvisible = FALSE;
124
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125/*
126 * Include the debugging code if wanted.
127 */
128#ifdef NBDEBUG
129# include "nbdebug.c"
130#endif
131
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200132static int needupdate = 0;
133static int inAtomic = 0;
134
Bram Moolenaar7ad7d012010-11-16 15:49:02 +0100135/*
136 * Close the socket and remove the input handlers.
137 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138 static void
Bram Moolenaar7ad7d012010-11-16 15:49:02 +0100139nb_close_socket(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140{
Bram Moolenaar3b7b8362015-03-20 18:11:48 +0100141 buf_T *buf;
142
143 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
144 buf->b_has_sign_column = FALSE;
145
Bram Moolenaar173c9852010-09-29 17:27:01 +0200146#ifdef FEAT_GUI_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147 if (inputHandler != (XtInputId)NULL)
148 {
149 XtRemoveInput(inputHandler);
150 inputHandler = (XtInputId)NULL;
151 }
Bram Moolenaar67c53842010-05-22 18:28:27 +0200152#else
153# ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154 if (inputHandler != 0)
155 {
156 gdk_input_remove(inputHandler);
157 inputHandler = 0;
158 }
Bram Moolenaar67c53842010-05-22 18:28:27 +0200159# else
160# ifdef FEAT_GUI_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 if (inputHandler == 0)
162 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200163 WSAAsyncSelect(nbsock, s_hwnd, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164 inputHandler = -1;
165 }
Bram Moolenaar67c53842010-05-22 18:28:27 +0200166# endif
167# endif
168#endif
169
Bram Moolenaar7ad7d012010-11-16 15:49:02 +0100170 sock_close(nbsock);
171 nbsock = -1;
Bram Moolenaare0874f82016-01-24 20:36:41 +0100172 channel_remove_netbeans();
Bram Moolenaar7ad7d012010-11-16 15:49:02 +0100173}
174
175/*
176 * Close the connection and cleanup.
177 * May be called when nb_close_socket() was called earlier.
178 */
179 static void
180netbeans_close(void)
181{
182 if (NETBEANS_OPEN)
183 {
184 netbeans_send_disconnect();
185 nb_close_socket();
186 }
187
Bram Moolenaar67c53842010-05-22 18:28:27 +0200188#ifdef FEAT_BEVAL
Bram Moolenaard62bec82005-03-07 22:56:57 +0000189 bevalServers &= ~BEVAL_NETBEANS;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200190#endif
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200191
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200192 needupdate = 0;
193 inAtomic = 0;
194 nb_free();
195
196 /* remove all signs and update the screen after gutter removal */
197 coloncmd(":sign unplace *");
198 changed_window_setting();
199 update_screen(CLEAR);
200 setcursor();
Bram Moolenaar96bcc5e2011-04-01 15:33:59 +0200201 cursor_on();
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200202 out_flush();
203#ifdef FEAT_GUI
Bram Moolenaard54a6882010-08-09 22:49:00 +0200204 if (gui.in_use)
205 {
206 gui_update_cursor(TRUE, FALSE);
207 gui_mch_flush();
208 }
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200209#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211
212#define NB_DEF_HOST "localhost"
213#define NB_DEF_ADDR "3219"
214#define NB_DEF_PASS "changeme"
215
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200216 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200217netbeans_connect(char *params, int doabort)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218{
219#ifdef INET_SOCKETS
220 struct sockaddr_in server;
221 struct hostent * host;
222# ifdef FEAT_GUI_W32
223 u_short port;
224# else
225 int port;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200226# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227#else
228 struct sockaddr_un server;
229#endif
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200230 int sd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231 char buf[32];
232 char *hostname = NULL;
233 char *address = NULL;
234 char *password = NULL;
235 char *fname;
236 char *arg = NULL;
237
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200238 if (*params == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200240 /* "=fname": Read info from specified file. */
Bram Moolenaare0874f82016-01-24 20:36:41 +0100241 if (getConnInfo(params + 1, &hostname, &address, &password) == FAIL)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200242 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243 }
244 else
245 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200246 if (*params == ':')
247 /* ":<host>:<addr>:<password>": get info from argument */
248 arg = params + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 if (arg == NULL && (fname = getenv("__NETBEANS_CONINFO")) != NULL)
250 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200251 /* "": get info from file specified in environment */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 if (getConnInfo(fname, &hostname, &address, &password) == FAIL)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200253 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254 }
255 else
256 {
257 if (arg != NULL)
258 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200259 /* ":<host>:<addr>:<password>": get info from argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260 hostname = arg;
261 address = strchr(hostname, ':');
262 if (address != NULL)
263 {
264 *address++ = '\0';
265 password = strchr(address, ':');
266 if (password != NULL)
267 *password++ = '\0';
268 }
269 }
270
271 /* Get the missing values from the environment. */
272 if (hostname == NULL || *hostname == '\0')
273 hostname = getenv("__NETBEANS_HOST");
274 if (address == NULL)
275 address = getenv("__NETBEANS_SOCKET");
276 if (password == NULL)
277 password = getenv("__NETBEANS_VIM_PASSWORD");
278
279 /* Move values to allocated memory. */
280 if (hostname != NULL)
281 hostname = (char *)vim_strsave((char_u *)hostname);
282 if (address != NULL)
283 address = (char *)vim_strsave((char_u *)address);
284 if (password != NULL)
285 password = (char *)vim_strsave((char_u *)password);
286 }
287 }
288
289 /* Use the default when a value is missing. */
290 if (hostname == NULL || *hostname == '\0')
291 {
292 vim_free(hostname);
293 hostname = (char *)vim_strsave((char_u *)NB_DEF_HOST);
294 }
295 if (address == NULL || *address == '\0')
296 {
297 vim_free(address);
298 address = (char *)vim_strsave((char_u *)NB_DEF_ADDR);
299 }
300 if (password == NULL || *password == '\0')
301 {
302 vim_free(password);
303 password = (char *)vim_strsave((char_u *)NB_DEF_PASS);
304 }
305 if (hostname == NULL || address == NULL || password == NULL)
306 goto theend; /* out of memory */
307
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200308#ifdef FEAT_GUI_W32
Bram Moolenaare0874f82016-01-24 20:36:41 +0100309 channel_init_winsock();
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200310#endif
311
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312#ifdef INET_SOCKETS
313 port = atoi(address);
314
Bram Moolenaare0874f82016-01-24 20:36:41 +0100315 if ((sd = (sock_T)socket(AF_INET, SOCK_STREAM, 0)) == (sock_T)-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000317 nbdebug(("error in socket() in netbeans_connect()\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318 PERROR("socket() in netbeans_connect()");
319 goto theend;
320 }
321
322 /* Get the server internet address and put into addr structure */
323 /* fill in the socket address structure and connect to server */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200324 vim_memset((char *)&server, '\0', sizeof(server));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325 server.sin_family = AF_INET;
326 server.sin_port = htons(port);
327 if ((host = gethostbyname(hostname)) == NULL)
328 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000329 nbdebug(("error in gethostbyname() in netbeans_connect()\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330 PERROR("gethostbyname() in netbeans_connect()");
Bram Moolenaar870b05c2011-01-04 18:11:43 +0100331 sock_close(sd);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332 goto theend;
333 }
334 memcpy((char *)&server.sin_addr, host->h_addr, host->h_length);
335#else
336 if ((sd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
337 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000338 nbdebug(("error in socket() in netbeans_connect()\n"));
339 PERROR("socket() in netbeans_connect()");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 goto theend;
341 }
342
343 server.sun_family = AF_UNIX;
344 strcpy(server.sun_path, address);
345#endif
346 /* Connect to server */
347 if (connect(sd, (struct sockaddr *)&server, sizeof(server)))
348 {
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200349 SOCK_ERRNO;
350 nbdebug(("netbeans_connect: Connect failed with errno %d\n", errno));
351 if (errno == ECONNREFUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 {
353 sock_close(sd);
354#ifdef INET_SOCKETS
Bram Moolenaare0874f82016-01-24 20:36:41 +0100355 if ((sd = (sock_T)socket(AF_INET, SOCK_STREAM, 0)) == (sock_T)-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356 {
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200357 SOCK_ERRNO;
Bram Moolenaarf2330482008-06-24 20:19:36 +0000358 nbdebug(("socket()#2 in netbeans_connect()\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 PERROR("socket()#2 in netbeans_connect()");
360 goto theend;
361 }
362#else
363 if ((sd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
364 {
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200365 SOCK_ERRNO;
Bram Moolenaarf2330482008-06-24 20:19:36 +0000366 nbdebug(("socket()#2 in netbeans_connect()\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367 PERROR("socket()#2 in netbeans_connect()");
368 goto theend;
369 }
370#endif
371 if (connect(sd, (struct sockaddr *)&server, sizeof(server)))
372 {
373 int retries = 36;
374 int success = FALSE;
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200375
376 SOCK_ERRNO;
377 while (retries-- && ((errno == ECONNREFUSED)
378 || (errno == EINTR)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 {
380 nbdebug(("retrying...\n"));
Bram Moolenaar870b05c2011-01-04 18:11:43 +0100381 mch_delay(3000L, TRUE);
382 ui_breakcheck();
383 if (got_int)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200384 {
Bram Moolenaar870b05c2011-01-04 18:11:43 +0100385 errno = EINTR;
386 break;
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 if (connect(sd, (struct sockaddr *)&server,
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200389 sizeof(server)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390 {
391 success = TRUE;
392 break;
393 }
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200394 SOCK_ERRNO;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 }
396 if (!success)
397 {
398 /* Get here when the server can't be found. */
Bram Moolenaarf2330482008-06-24 20:19:36 +0000399 nbdebug(("Cannot connect to Netbeans #2\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 PERROR(_("Cannot connect to Netbeans #2"));
Bram Moolenaar870b05c2011-01-04 18:11:43 +0100401 sock_close(sd);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200402 if (doabort)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200403 getout(1);
404 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405 }
406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 }
408 else
409 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000410 nbdebug(("Cannot connect to Netbeans\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411 PERROR(_("Cannot connect to Netbeans"));
Bram Moolenaar870b05c2011-01-04 18:11:43 +0100412 sock_close(sd);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200413 if (doabort)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200414 getout(1);
415 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 }
417 }
418
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200419 nbsock = sd;
Bram Moolenaare0874f82016-01-24 20:36:41 +0100420 channel_add_netbeans(nbsock);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000421 vim_snprintf(buf, sizeof(buf), "AUTH %s\n", password);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422 nb_send(buf, "netbeans_connect");
423
424 sprintf(buf, "0:version=0 \"%s\"\n", ExtEdProtocolVersion);
425 nb_send(buf, "externaleditor_version");
426
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427theend:
428 vim_free(hostname);
429 vim_free(address);
430 vim_free(password);
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200431 return NETBEANS_OPEN ? OK : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432}
433
434/*
435 * Obtain the NetBeans hostname, port address and password from a file.
436 * Return the strings in allocated memory.
437 * Return FAIL if the file could not be read, OK otherwise (no matter what it
438 * contains).
439 */
440 static int
441getConnInfo(char *file, char **host, char **port, char **auth)
442{
443 FILE *fp;
444 char_u buf[BUFSIZ];
445 char_u *lp;
Bram Moolenaar309cbc32012-01-10 22:31:31 +0100446 char_u *nlp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447#ifdef UNIX
448 struct stat st;
449
450 /*
451 * For Unix only accept the file when it's not accessible by others.
452 * The open will then fail if we don't own the file.
453 */
454 if (mch_stat(file, &st) == 0 && (st.st_mode & 0077) != 0)
455 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000456 nbdebug(("Wrong access mode for NetBeans connection info file: \"%s\"\n",
457 file));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 EMSG2(_("E668: Wrong access mode for NetBeans connection info file: \"%s\""),
459 file);
460 return FAIL;
461 }
462#endif
463
464 fp = mch_fopen(file, "r");
465 if (fp == NULL)
466 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000467 nbdebug(("Cannot open NetBeans connection info file\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468 PERROR("E660: Cannot open NetBeans connection info file");
469 return FAIL;
470 }
471
472 /* Read the file. There should be one of each parameter */
473 while ((lp = (char_u *)fgets((char *)buf, BUFSIZ, fp)) != NULL)
474 {
Bram Moolenaar309cbc32012-01-10 22:31:31 +0100475 if ((nlp = vim_strchr(lp, '\n')) != NULL)
476 *nlp = 0; /* strip off the trailing newline */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477
478 if (STRNCMP(lp, "host=", 5) == 0)
479 {
480 vim_free(*host);
481 *host = (char *)vim_strsave(&buf[5]);
482 }
483 else if (STRNCMP(lp, "port=", 5) == 0)
484 {
485 vim_free(*port);
486 *port = (char *)vim_strsave(&buf[5]);
487 }
488 else if (STRNCMP(lp, "auth=", 5) == 0)
489 {
490 vim_free(*auth);
491 *auth = (char *)vim_strsave(&buf[5]);
492 }
493 }
494 fclose(fp);
495
496 return OK;
497}
498
499
500struct keyqueue
501{
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100502 char_u *keystr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 struct keyqueue *next;
504 struct keyqueue *prev;
505};
506
507typedef struct keyqueue keyQ_T;
508
509static keyQ_T keyHead; /* dummy node, header for circular queue */
510
511
512/*
513 * Queue up key commands sent from netbeans.
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100514 * We store the string, because it may depend on the global mod_mask and
515 * :nbkey doesn't have a key number.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 */
517 static void
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100518postpone_keycommand(char_u *keystr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519{
520 keyQ_T *node;
521
522 node = (keyQ_T *)alloc(sizeof(keyQ_T));
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100523 if (node == NULL)
524 return; /* out of memory, drop the key */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525
526 if (keyHead.next == NULL) /* initialize circular queue */
527 {
528 keyHead.next = &keyHead;
529 keyHead.prev = &keyHead;
530 }
531
532 /* insert node at tail of queue */
533 node->next = &keyHead;
534 node->prev = keyHead.prev;
535 keyHead.prev->next = node;
536 keyHead.prev = node;
537
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100538 node->keystr = vim_strsave(keystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539}
540
541/*
542 * Handle any queued-up NetBeans keycommands to be send.
543 */
544 static void
545handle_key_queue(void)
546{
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100547 int postponed = FALSE;
548
549 while (!postponed && keyHead.next && keyHead.next != &keyHead)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 {
551 /* first, unlink the node */
552 keyQ_T *node = keyHead.next;
553 keyHead.next = node->next;
554 node->next->prev = node->prev;
555
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100556 /* Now, send the keycommand. This may cause it to be postponed again
557 * and change keyHead. */
558 if (node->keystr != NULL)
559 postponed = !netbeans_keystring(node->keystr);
560 vim_free(node->keystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561
562 /* Finally, dispose of the node */
563 vim_free(node);
564 }
565}
566
567
568struct cmdqueue
569{
570 char_u *buffer;
571 struct cmdqueue *next;
572 struct cmdqueue *prev;
573};
574
575typedef struct cmdqueue queue_T;
576
577static queue_T head; /* dummy node, header for circular queue */
578
579
580/*
581 * Put the buffer on the work queue; possibly save it to a file as well.
582 */
583 static void
584save(char_u *buf, int len)
585{
586 queue_T *node;
587
588 node = (queue_T *)alloc(sizeof(queue_T));
589 if (node == NULL)
590 return; /* out of memory */
591 node->buffer = alloc(len + 1);
592 if (node->buffer == NULL)
593 {
594 vim_free(node);
595 return; /* out of memory */
596 }
597 mch_memmove(node->buffer, buf, (size_t)len);
598 node->buffer[len] = NUL;
599
600 if (head.next == NULL) /* initialize circular queue */
601 {
602 head.next = &head;
603 head.prev = &head;
604 }
605
606 /* insert node at tail of queue */
607 node->next = &head;
608 node->prev = head.prev;
609 head.prev->next = node;
610 head.prev = node;
611
612#ifdef NBDEBUG
613 {
614 static int outfd = -2;
615
616 /* possibly write buffer out to a file */
617 if (outfd == -3)
618 return;
619
620 if (outfd == -2)
621 {
622 char *file = getenv("__NETBEANS_SAVE");
623 if (file == NULL)
624 outfd = -3;
625 else
626 outfd = mch_open(file, O_WRONLY|O_CREAT|O_TRUNC, 0666);
627 }
628
629 if (outfd >= 0)
630 write(outfd, buf, len);
631 }
632#endif
633}
634
635
636/*
637 * While there's still a command in the work queue, parse and execute it.
638 */
Bram Moolenaarf2330482008-06-24 20:19:36 +0000639 void
640netbeans_parse_messages(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641{
642 char_u *p;
643 queue_T *node;
Bram Moolenaar863053d2010-12-02 17:09:54 +0100644 int own_node;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645
Bram Moolenaarf2330482008-06-24 20:19:36 +0000646 while (head.next != NULL && head.next != &head)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 {
648 node = head.next;
649
650 /* Locate the first line in the first buffer. */
651 p = vim_strchr(node->buffer, '\n');
652 if (p == NULL)
653 {
654 /* Command isn't complete. If there is no following buffer,
655 * return (wait for more). If there is another buffer following,
656 * prepend the text to that buffer and delete this one. */
657 if (node->next == &head)
658 return;
Bram Moolenaarf2330482008-06-24 20:19:36 +0000659 p = alloc((unsigned)(STRLEN(node->buffer)
660 + STRLEN(node->next->buffer) + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 if (p == NULL)
662 return; /* out of memory */
663 STRCPY(p, node->buffer);
664 STRCAT(p, node->next->buffer);
665 vim_free(node->next->buffer);
666 node->next->buffer = p;
667
668 /* dispose of the node and buffer */
669 head.next = node->next;
670 node->next->prev = node->prev;
671 vim_free(node->buffer);
672 vim_free(node);
673 }
674 else
675 {
676 /* There is a complete command at the start of the buffer.
677 * Terminate it with a NUL. When no more text is following unlink
678 * the buffer. Do this before executing, because new buffers can
679 * be added while busy handling the command. */
680 *p++ = NUL;
681 if (*p == NUL)
682 {
Bram Moolenaar863053d2010-12-02 17:09:54 +0100683 own_node = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 head.next = node->next;
685 node->next->prev = node->prev;
686 }
Bram Moolenaar863053d2010-12-02 17:09:54 +0100687 else
688 own_node = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689
690 /* now, parse and execute the commands */
691 nb_parse_cmd(node->buffer);
692
Bram Moolenaar863053d2010-12-02 17:09:54 +0100693 if (own_node)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 {
695 /* buffer finished, dispose of the node and buffer */
696 vim_free(node->buffer);
697 vim_free(node);
698 }
Bram Moolenaar863053d2010-12-02 17:09:54 +0100699 /* Check that "head" wasn't changed under our fingers, e.g. when a
700 * DETACH command was handled. */
701 else if (head.next == node)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 {
703 /* more follows, move to the start */
Bram Moolenaarf2330482008-06-24 20:19:36 +0000704 STRMOVE(node->buffer, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 }
706 }
707 }
708}
709
710/* Buffer size for reading incoming messages. */
711#define MAXMSGSIZE 4096
712
713/*
Bram Moolenaar67c53842010-05-22 18:28:27 +0200714 * Read a command from netbeans.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 */
Bram Moolenaar173c9852010-09-29 17:27:01 +0200716#ifdef FEAT_GUI_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 static void
Bram Moolenaara9d45512009-05-17 21:25:42 +0000718messageFromNetbeans(XtPointer clientData UNUSED,
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000719 int *unused1 UNUSED,
720 XtInputId *unused2 UNUSED)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200721{
722 netbeans_read();
723}
724#endif
725
726#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000728messageFromNetbeans(gpointer clientData UNUSED,
729 gint unused1 UNUSED,
730 GdkInputCondition unused2 UNUSED)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200731{
732 netbeans_read();
733}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734#endif
Bram Moolenaar67c53842010-05-22 18:28:27 +0200735
Bram Moolenaar7ad7d012010-11-16 15:49:02 +0100736#define DETACH_MSG "DETACH\n"
737
Bram Moolenaar67c53842010-05-22 18:28:27 +0200738 void
739netbeans_read()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740{
741 static char_u *buf = NULL;
Bram Moolenaar0b65f892010-05-15 14:49:02 +0200742 int len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 int readlen = 0;
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100744#ifdef HAVE_SELECT
745 struct timeval tval;
746 fd_set rfds;
747#else
748# ifdef HAVE_POLL
749 struct pollfd fds;
750# endif
751#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200753 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 {
755 nbdebug(("messageFromNetbeans() called without a socket\n"));
756 return;
757 }
758
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 /* Allocate a buffer to read into. */
760 if (buf == NULL)
761 {
762 buf = alloc(MAXMSGSIZE);
763 if (buf == NULL)
764 return; /* out of memory! */
765 }
766
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100767 /* Keep on reading for as long as there is something to read.
768 * Use select() or poll() to avoid blocking on a message that is exactly
769 * MAXMSGSIZE long. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 for (;;)
771 {
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100772#ifdef HAVE_SELECT
773 FD_ZERO(&rfds);
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200774 FD_SET(nbsock, &rfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +0200775 tval.tv_sec = 0;
776 tval.tv_usec = 0;
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200777 if (select(nbsock + 1, &rfds, NULL, NULL, &tval) <= 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200778 break;
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100779#else
780# ifdef HAVE_POLL
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200781 fds.fd = nbsock;
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100782 fds.events = POLLIN;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200783 if (poll(&fds, 1, 0) <= 0)
784 break;
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100785# endif
786#endif
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200787 len = sock_read(nbsock, buf, MAXMSGSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 if (len <= 0)
789 break; /* error or nothing more to read */
790
791 /* Store the read message in the queue. */
792 save(buf, len);
793 readlen += len;
794 if (len < MAXMSGSIZE)
795 break; /* did read everything that's available */
796 }
797
Bram Moolenaar7ad7d012010-11-16 15:49:02 +0100798 /* Reading a socket disconnection (readlen == 0), or a socket error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 if (readlen <= 0)
800 {
Bram Moolenaar7ad7d012010-11-16 15:49:02 +0100801 /* Queue a "DETACH" netbeans message in the command queue in order to
802 * terminate the netbeans session later. Do not end the session here
803 * directly as we may be running in the context of a call to
804 * netbeans_parse_messages():
805 * netbeans_parse_messages
806 * -> autocmd triggered while processing the netbeans cmd
807 * -> ui_breakcheck
808 * -> gui event loop or select loop
809 * -> netbeans_read()
810 */
Bram Moolenaarde1b0922010-12-24 14:00:17 +0100811 save((char_u *)DETACH_MSG, (int)strlen(DETACH_MSG));
Bram Moolenaar7ad7d012010-11-16 15:49:02 +0100812 nb_close_socket();
813
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 if (len < 0)
Bram Moolenaarf2330482008-06-24 20:19:36 +0000815 {
816 nbdebug(("read from Netbeans socket\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 PERROR(_("read from Netbeans socket"));
Bram Moolenaarf2330482008-06-24 20:19:36 +0000818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 }
820
Bram Moolenaar03531f72010-11-16 15:04:57 +0100821#if defined(NB_HAS_GUI) && defined(FEAT_GUI_GTK)
822 if (NB_HAS_GUI && gtk_main_level() > 0)
Bram Moolenaar7ad7d012010-11-16 15:49:02 +0100823 gtk_main_quit();
Bram Moolenaarf2330482008-06-24 20:19:36 +0000824#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825}
826
827/*
828 * Handle one NUL terminated command.
829 *
830 * format of a command from netbeans:
831 *
832 * 6:setTitle!84 "a.c"
833 *
834 * bufno
835 * colon
836 * cmd
837 * !
838 * cmdno
839 * args
840 *
841 * for function calls, the ! is replaced by a /
842 */
843 static void
844nb_parse_cmd(char_u *cmd)
845{
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000846 char *verb;
847 char *q;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 int bufno;
849 int isfunc = -1;
850
851 if (STRCMP(cmd, "DISCONNECT") == 0)
852 {
853 /* We assume the server knows that we can safely exit! */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 /* Disconnect before exiting, Motif hangs in a Select error
855 * message otherwise. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200856 netbeans_close();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 getout(0);
858 /* NOTREACHED */
859 }
860
861 if (STRCMP(cmd, "DETACH") == 0)
862 {
863 /* The IDE is breaking the connection. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200864 netbeans_close();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 return;
866 }
867
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000868 bufno = strtol((char *)cmd, &verb, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869
870 if (*verb != ':')
871 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000872 nbdebug((" missing colon: %s\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 EMSG2("E627: missing colon: %s", cmd);
874 return;
875 }
876 ++verb; /* skip colon */
877
878 for (q = verb; *q; q++)
879 {
880 if (*q == '!')
881 {
882 *q++ = NUL;
883 isfunc = 0;
884 break;
885 }
886 else if (*q == '/')
887 {
888 *q++ = NUL;
889 isfunc = 1;
890 break;
891 }
892 }
893
894 if (isfunc < 0)
895 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000896 nbdebug((" missing ! or / in: %s\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 EMSG2("E628: missing ! or / in: %s", cmd);
898 return;
899 }
900
Bram Moolenaar89d40322006-08-29 15:30:07 +0000901 r_cmdno = strtol(q, &q, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000903 q = (char *)skipwhite((char_u *)q);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904
Bram Moolenaar89d40322006-08-29 15:30:07 +0000905 if (nb_do_cmd(bufno, (char_u *)verb, isfunc, r_cmdno, (char_u *)q) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 {
Bram Moolenaar009b2592004-10-24 19:18:58 +0000907#ifdef NBDEBUG
908 /*
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100909 * This happens because the ExtEd can send a command or 2 after
Bram Moolenaar009b2592004-10-24 19:18:58 +0000910 * doing a stopDocumentListen command. It doesn't harm anything
911 * so I'm disabling it except for debugging.
912 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 nbdebug(("nb_parse_cmd: Command error for \"%s\"\n", cmd));
914 EMSG("E629: bad return from nb_do_cmd");
Bram Moolenaar009b2592004-10-24 19:18:58 +0000915#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 }
917}
918
919struct nbbuf_struct
920{
921 buf_T *bufp;
922 unsigned int fireChanges:1;
923 unsigned int initDone:1;
Bram Moolenaar009b2592004-10-24 19:18:58 +0000924 unsigned int insertDone:1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 unsigned int modified:1;
Bram Moolenaar009b2592004-10-24 19:18:58 +0000926 int nbbuf_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 char *displayname;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 int *signmap;
929 short_u signmaplen;
930 short_u signmapused;
931};
932
933typedef struct nbbuf_struct nbbuf_T;
934
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200935static nbbuf_T *buf_list = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000936static int buf_list_size = 0; /* size of buf_list */
937static int buf_list_used = 0; /* nr of entries in buf_list actually in use */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200939static char **globalsignmap = NULL;
940static int globalsignmaplen = 0;
941static int globalsignmapused = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942
943static int mapsigntype __ARGS((nbbuf_T *, int localsigntype));
944static void addsigntype __ARGS((nbbuf_T *, int localsigntype, char_u *typeName,
945 char_u *tooltip, char_u *glyphfile,
Bram Moolenaar67c53842010-05-22 18:28:27 +0200946 char_u *fg, char_u *bg));
Bram Moolenaar009b2592004-10-24 19:18:58 +0000947static void print_read_msg __ARGS((nbbuf_T *buf));
Bram Moolenaar914703b2010-05-31 21:59:46 +0200948static void print_save_msg __ARGS((nbbuf_T *buf, off_t nchars));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949
950static int curPCtype = -1;
951
952/*
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200953 * Free netbeans resources.
954 */
955 static void
956nb_free()
957{
958 keyQ_T *key_node = keyHead.next;
959 queue_T *cmd_node = head.next;
960 nbbuf_T buf;
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200961 int i;
962
963 /* free the netbeans buffer list */
964 for (i = 0; i < buf_list_used; i++)
965 {
966 buf = buf_list[i];
967 vim_free(buf.displayname);
968 vim_free(buf.signmap);
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100969 if (buf.bufp != NULL)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200970 {
971 buf.bufp->b_netbeans_file = FALSE;
972 buf.bufp->b_was_netbeans_file = FALSE;
973 }
974 }
975 vim_free(buf_list);
976 buf_list = NULL;
977 buf_list_size = 0;
978 buf_list_used = 0;
979
980 /* free the queued key commands */
Bram Moolenaar8d4eecc2012-11-20 17:19:01 +0100981 while (key_node != NULL && key_node != &keyHead)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200982 {
983 keyQ_T *next = key_node->next;
984 vim_free(key_node->keystr);
985 vim_free(key_node);
986 if (next == &keyHead)
987 {
988 keyHead.next = &keyHead;
989 keyHead.prev = &keyHead;
990 break;
991 }
992 key_node = next;
993 }
994
995 /* free the queued netbeans commands */
Bram Moolenaar8d4eecc2012-11-20 17:19:01 +0100996 while (cmd_node != NULL && cmd_node != &head)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200997 {
998 queue_T *next = cmd_node->next;
999 vim_free(cmd_node->buffer);
1000 vim_free(cmd_node);
1001 if (next == &head)
1002 {
1003 head.next = &head;
1004 head.prev = &head;
1005 break;
1006 }
1007 cmd_node = next;
1008 }
1009}
1010
1011/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 * Get the Netbeans buffer number for the specified buffer.
1013 */
1014 static int
1015nb_getbufno(buf_T *bufp)
1016{
1017 int i;
1018
1019 for (i = 0; i < buf_list_used; i++)
1020 if (buf_list[i].bufp == bufp)
1021 return i;
1022 return -1;
1023}
1024
1025/*
1026 * Is this a NetBeans-owned buffer?
1027 */
1028 int
1029isNetbeansBuffer(buf_T *bufp)
1030{
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001031 return NETBEANS_OPEN && bufp->b_netbeans_file;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032}
1033
1034/*
1035 * NetBeans and Vim have different undo models. In Vim, the file isn't
1036 * changed if changes are undone via the undo command. In NetBeans, once
1037 * a change has been made the file is marked as modified until saved. It
1038 * doesn't matter if the change was undone.
1039 *
1040 * So this function is for the corner case where Vim thinks a buffer is
1041 * unmodified but NetBeans thinks it IS modified.
1042 */
1043 int
1044isNetbeansModified(buf_T *bufp)
1045{
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001046 if (isNetbeansBuffer(bufp))
Bram Moolenaar009b2592004-10-24 19:18:58 +00001047 {
1048 int bufno = nb_getbufno(bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049
Bram Moolenaar009b2592004-10-24 19:18:58 +00001050 if (bufno > 0)
1051 return buf_list[bufno].modified;
1052 else
1053 return FALSE;
1054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 else
1056 return FALSE;
1057}
1058
1059/*
1060 * Given a Netbeans buffer number, return the netbeans buffer.
1061 * Returns NULL for 0 or a negative number. A 0 bufno means a
1062 * non-buffer related command has been sent.
1063 */
1064 static nbbuf_T *
1065nb_get_buf(int bufno)
1066{
1067 /* find or create a buffer with the given number */
1068 int incr;
1069
1070 if (bufno <= 0)
1071 return NULL;
1072
1073 if (!buf_list)
1074 {
1075 /* initialize */
1076 buf_list = (nbbuf_T *)alloc_clear(100 * sizeof(nbbuf_T));
1077 buf_list_size = 100;
1078 }
1079 if (bufno >= buf_list_used) /* new */
1080 {
1081 if (bufno >= buf_list_size) /* grow list */
1082 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001083 nbbuf_T *t_buf_list = buf_list;
1084
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 incr = bufno - buf_list_size + 90;
1086 buf_list_size += incr;
1087 buf_list = (nbbuf_T *)vim_realloc(
1088 buf_list, buf_list_size * sizeof(nbbuf_T));
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001089 if (buf_list == NULL)
1090 {
1091 vim_free(t_buf_list);
1092 buf_list_size = 0;
1093 return NULL;
1094 }
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02001095 vim_memset(buf_list + buf_list_size - incr, 0,
1096 incr * sizeof(nbbuf_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 }
1098
1099 while (buf_list_used <= bufno)
1100 {
1101 /* Default is to fire text changes. */
1102 buf_list[buf_list_used].fireChanges = 1;
1103 ++buf_list_used;
1104 }
1105 }
1106
1107 return buf_list + bufno;
1108}
1109
1110/*
1111 * Return the number of buffers that are modified.
1112 */
1113 static int
1114count_changed_buffers(void)
1115{
1116 buf_T *bufp;
1117 int n;
1118
1119 n = 0;
1120 for (bufp = firstbuf; bufp != NULL; bufp = bufp->b_next)
1121 if (bufp->b_changed)
1122 ++n;
1123 return n;
1124}
1125
1126/*
1127 * End the netbeans session.
1128 */
1129 void
1130netbeans_end(void)
1131{
1132 int i;
1133 static char buf[128];
1134
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001135 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 return;
1137
1138 for (i = 0; i < buf_list_used; i++)
1139 {
1140 if (!buf_list[i].bufp)
1141 continue;
1142 if (netbeansForcedQuit)
1143 {
1144 /* mark as unmodified so NetBeans won't put up dialog on "killed" */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001145 sprintf(buf, "%d:unmodified=%d\n", i, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 nbdebug(("EVT: %s", buf));
1147 nb_send(buf, "netbeans_end");
1148 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001149 sprintf(buf, "%d:killed=%d\n", i, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 nbdebug(("EVT: %s", buf));
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001151 /* nb_send(buf, "netbeans_end"); avoid "write failed" messages */
1152 ignored = sock_write(nbsock, buf, (int)STRLEN(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154}
1155
1156/*
1157 * Send a message to netbeans.
1158 */
1159 static void
1160nb_send(char *buf, char *fun)
1161{
1162 /* Avoid giving pages full of error messages when the other side has
1163 * exited, only mention the first error until the connection works again. */
1164 static int did_error = FALSE;
1165
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001166 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 {
1168 if (!did_error)
Bram Moolenaarf2330482008-06-24 20:19:36 +00001169 {
1170 nbdebug((" %s(): write while not connected\n", fun));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 EMSG2("E630: %s(): write while not connected", fun);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 did_error = TRUE;
1174 }
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001175 else if (sock_write(nbsock, buf, (int)STRLEN(buf)) != (int)STRLEN(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 {
1177 if (!did_error)
Bram Moolenaarf2330482008-06-24 20:19:36 +00001178 {
1179 nbdebug((" %s(): write failed\n", fun));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 EMSG2("E631: %s(): write failed", fun);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 did_error = TRUE;
1183 }
1184 else
1185 did_error = FALSE;
1186}
1187
1188/*
1189 * Some input received from netbeans requires a response. This function
1190 * handles a response with no information (except the command number).
1191 */
1192 static void
1193nb_reply_nil(int cmdno)
1194{
1195 char reply[32];
1196
Bram Moolenaar009b2592004-10-24 19:18:58 +00001197 nbdebug(("REP %d: <none>\n", cmdno));
1198
Bram Moolenaar7ad7d012010-11-16 15:49:02 +01001199 /* Avoid printing an annoying error message. */
1200 if (!NETBEANS_OPEN)
1201 return;
1202
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 sprintf(reply, "%d\n", cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 nb_send(reply, "nb_reply_nil");
1205}
1206
1207
1208/*
1209 * Send a response with text.
1210 * "result" must have been quoted already (using nb_quote()).
1211 */
1212 static void
1213nb_reply_text(int cmdno, char_u *result)
1214{
1215 char_u *reply;
1216
Bram Moolenaar009b2592004-10-24 19:18:58 +00001217 nbdebug(("REP %d: %s\n", cmdno, (char *)result));
1218
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001219 reply = alloc((unsigned)STRLEN(result) + 32);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 sprintf((char *)reply, "%d %s\n", cmdno, (char *)result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 nb_send((char *)reply, "nb_reply_text");
1222
1223 vim_free(reply);
1224}
1225
1226
1227/*
1228 * Send a response with a number result code.
1229 */
1230 static void
1231nb_reply_nr(int cmdno, long result)
1232{
1233 char reply[32];
1234
Bram Moolenaar009b2592004-10-24 19:18:58 +00001235 nbdebug(("REP %d: %ld\n", cmdno, result));
1236
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 sprintf(reply, "%d %ld\n", cmdno, result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 nb_send(reply, "nb_reply_nr");
1239}
1240
1241
1242/*
1243 * Encode newline, ret, backslash, double quote for transmission to NetBeans.
1244 */
1245 static char_u *
1246nb_quote(char_u *txt)
1247{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001248 char_u *buf = alloc((unsigned)(2 * STRLEN(txt) + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 char_u *p = txt;
1250 char_u *q = buf;
1251
1252 if (buf == NULL)
1253 return NULL;
1254 for (; *p; p++)
1255 {
1256 switch (*p)
1257 {
1258 case '\"':
1259 case '\\':
1260 *q++ = '\\'; *q++ = *p; break;
1261 /* case '\t': */
1262 /* *q++ = '\\'; *q++ = 't'; break; */
1263 case '\n':
1264 *q++ = '\\'; *q++ = 'n'; break;
1265 case '\r':
1266 *q++ = '\\'; *q++ = 'r'; break;
1267 default:
1268 *q++ = *p;
1269 break;
1270 }
1271 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01001272 *q = '\0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273
1274 return buf;
1275}
1276
1277
1278/*
1279 * Remove top level double quotes; convert backslashed chars.
1280 * Returns an allocated string (NULL for failure).
1281 * If "endp" is not NULL it is set to the character after the terminating
1282 * quote.
1283 */
1284 static char *
1285nb_unquote(char_u *p, char_u **endp)
1286{
1287 char *result = 0;
1288 char *q;
1289 int done = 0;
1290
1291 /* result is never longer than input */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001292 result = (char *)alloc_clear((unsigned)STRLEN(p) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 if (result == NULL)
1294 return NULL;
1295
1296 if (*p++ != '"')
1297 {
1298 nbdebug(("nb_unquote called with string that doesn't start with a quote!: %s\n",
1299 p));
1300 result[0] = NUL;
1301 return result;
1302 }
1303
1304 for (q = result; !done && *p != NUL;)
1305 {
1306 switch (*p)
1307 {
1308 case '"':
1309 /*
1310 * Unbackslashed dquote marks the end, if first char was dquote.
1311 */
1312 done = 1;
1313 break;
1314
1315 case '\\':
1316 ++p;
1317 switch (*p)
1318 {
1319 case '\\': *q++ = '\\'; break;
1320 case 'n': *q++ = '\n'; break;
1321 case 't': *q++ = '\t'; break;
1322 case 'r': *q++ = '\r'; break;
1323 case '"': *q++ = '"'; break;
1324 case NUL: --p; break;
1325 /* default: skip over illegal chars */
1326 }
1327 ++p;
1328 break;
1329
1330 default:
1331 *q++ = *p++;
1332 }
1333 }
1334
1335 if (endp != NULL)
1336 *endp = p;
1337
1338 return result;
1339}
1340
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001341/*
1342 * Remove from "first" byte to "last" byte (inclusive), at line "lnum" of the
1343 * current buffer. Remove to end of line when "last" is MAXCOL.
1344 */
1345 static void
1346nb_partialremove(linenr_T lnum, colnr_T first, colnr_T last)
1347{
1348 char_u *oldtext, *newtext;
1349 int oldlen;
1350 int lastbyte = last;
1351
1352 oldtext = ml_get(lnum);
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001353 oldlen = (int)STRLEN(oldtext);
Bram Moolenaarb3c70982008-01-18 10:40:55 +00001354 if (first >= (colnr_T)oldlen || oldlen == 0) /* just in case */
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001355 return;
1356 if (lastbyte >= oldlen)
1357 lastbyte = oldlen - 1;
1358 newtext = alloc(oldlen - (int)(lastbyte - first));
1359 if (newtext != NULL)
1360 {
1361 mch_memmove(newtext, oldtext, first);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001362 STRMOVE(newtext + first, oldtext + lastbyte + 1);
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001363 nbdebug((" NEW LINE %d: %s\n", lnum, newtext));
1364 ml_replace(lnum, newtext, FALSE);
1365 }
1366}
1367
1368/*
1369 * Replace the "first" line with the concatenation of the "first" and
1370 * the "other" line. The "other" line is not removed.
1371 */
1372 static void
1373nb_joinlines(linenr_T first, linenr_T other)
1374{
1375 int len_first, len_other;
1376 char_u *p;
1377
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001378 len_first = (int)STRLEN(ml_get(first));
1379 len_other = (int)STRLEN(ml_get(other));
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001380 p = alloc((unsigned)(len_first + len_other + 1));
1381 if (p != NULL)
1382 {
1383 mch_memmove(p, ml_get(first), len_first);
1384 mch_memmove(p + len_first, ml_get(other), len_other + 1);
1385 ml_replace(first, p, FALSE);
1386 }
1387}
1388
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389#define SKIP_STOP 2
1390#define streq(a,b) (strcmp(a,b) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391
1392/*
1393 * Do the actual processing of a single netbeans command or function.
Bram Moolenaar143c38c2007-05-10 16:41:10 +00001394 * The difference between a command and function is that a function
1395 * gets a response (it's required) but a command does not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 * For arguments see comment for nb_parse_cmd().
1397 */
1398 static int
1399nb_do_cmd(
1400 int bufno,
1401 char_u *cmd,
1402 int func,
1403 int cmdno,
1404 char_u *args) /* points to space before arguments or NUL */
1405{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001406 int do_update = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 long off = 0;
1408 nbbuf_T *buf = nb_get_buf(bufno);
1409 static int skip = 0;
1410 int retval = OK;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001411 char *cp; /* for when a char pointer is needed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412
1413 nbdebug(("%s %d: (%d) %s %s\n", (func) ? "FUN" : "CMD", cmdno, bufno, cmd,
1414 STRCMP(cmd, "insert") == 0 ? "<text>" : (char *)args));
1415
1416 if (func)
1417 {
1418/* =====================================================================*/
1419 if (streq((char *)cmd, "getModified"))
1420 {
1421 if (buf == NULL || buf->bufp == NULL)
1422 /* Return the number of buffers that are modified. */
1423 nb_reply_nr(cmdno, (long)count_changed_buffers());
1424 else
1425 /* Return whether the buffer is modified. */
1426 nb_reply_nr(cmdno, (long)(buf->bufp->b_changed
1427 || isNetbeansModified(buf->bufp)));
1428/* =====================================================================*/
1429 }
1430 else if (streq((char *)cmd, "saveAndExit"))
1431 {
1432 /* Note: this will exit Vim if successful. */
1433 coloncmd(":confirm qall");
1434
1435 /* We didn't exit: return the number of changed buffers. */
1436 nb_reply_nr(cmdno, (long)count_changed_buffers());
1437/* =====================================================================*/
1438 }
1439 else if (streq((char *)cmd, "getCursor"))
1440 {
1441 char_u text[200];
1442
1443 /* Note: nb_getbufno() may return -1. This indicates the IDE
1444 * didn't assign a number to the current buffer in response to a
1445 * fileOpened event. */
1446 sprintf((char *)text, "%d %ld %d %ld",
1447 nb_getbufno(curbuf),
1448 (long)curwin->w_cursor.lnum,
1449 (int)curwin->w_cursor.col,
1450 pos2off(curbuf, &curwin->w_cursor));
1451 nb_reply_text(cmdno, text);
1452/* =====================================================================*/
1453 }
Bram Moolenaarc65c4912006-11-14 17:29:46 +00001454 else if (streq((char *)cmd, "getAnno"))
1455 {
1456 long linenum = 0;
1457#ifdef FEAT_SIGNS
1458 if (buf == NULL || buf->bufp == NULL)
1459 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001460 nbdebug((" Invalid buffer identifier in getAnno\n"));
1461 EMSG("E652: Invalid buffer identifier in getAnno");
Bram Moolenaarc65c4912006-11-14 17:29:46 +00001462 retval = FAIL;
1463 }
1464 else
1465 {
1466 int serNum;
1467
1468 cp = (char *)args;
1469 serNum = strtol(cp, &cp, 10);
1470 /* If the sign isn't found linenum will be zero. */
1471 linenum = (long)buf_findsign(buf->bufp, serNum);
1472 }
1473#endif
1474 nb_reply_nr(cmdno, linenum);
1475/* =====================================================================*/
1476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 else if (streq((char *)cmd, "getLength"))
1478 {
1479 long len = 0;
1480
1481 if (buf == NULL || buf->bufp == NULL)
1482 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001483 nbdebug((" invalid buffer identifier in getLength\n"));
1484 EMSG("E632: invalid buffer identifier in getLength");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 retval = FAIL;
1486 }
1487 else
1488 {
1489 len = get_buf_size(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 }
1491 nb_reply_nr(cmdno, len);
1492/* =====================================================================*/
1493 }
1494 else if (streq((char *)cmd, "getText"))
1495 {
1496 long len;
1497 linenr_T nlines;
1498 char_u *text = NULL;
1499 linenr_T lno = 1;
1500 char_u *p;
1501 char_u *line;
1502
1503 if (buf == NULL || buf->bufp == NULL)
1504 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001505 nbdebug((" invalid buffer identifier in getText\n"));
1506 EMSG("E633: invalid buffer identifier in getText");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 retval = FAIL;
1508 }
1509 else
1510 {
1511 len = get_buf_size(buf->bufp);
1512 nlines = buf->bufp->b_ml.ml_line_count;
1513 text = alloc((unsigned)((len > 0)
1514 ? ((len + nlines) * 2) : 4));
1515 if (text == NULL)
1516 {
1517 nbdebug((" nb_do_cmd: getText has null text field\n"));
1518 retval = FAIL;
1519 }
1520 else
1521 {
1522 p = text;
1523 *p++ = '\"';
1524 for (; lno <= nlines ; lno++)
1525 {
1526 line = nb_quote(ml_get_buf(buf->bufp, lno, FALSE));
1527 if (line != NULL)
1528 {
1529 STRCPY(p, line);
1530 p += STRLEN(line);
1531 *p++ = '\\';
1532 *p++ = 'n';
Bram Moolenaar009b2592004-10-24 19:18:58 +00001533 vim_free(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 }
1536 *p++ = '\"';
1537 *p = '\0';
1538 }
1539 }
1540 if (text == NULL)
1541 nb_reply_text(cmdno, (char_u *)"");
1542 else
1543 {
1544 nb_reply_text(cmdno, text);
1545 vim_free(text);
1546 }
1547/* =====================================================================*/
1548 }
1549 else if (streq((char *)cmd, "remove"))
1550 {
1551 long count;
1552 pos_T first, last;
1553 pos_T *pos;
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001554 pos_T *next;
1555 linenr_T del_from_lnum, del_to_lnum; /* lines to be deleted as a whole */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 int oldFire = netbeansFireChanges;
1557 int oldSuppress = netbeansSuppressNoLines;
1558 int wasChanged;
1559
1560 if (skip >= SKIP_STOP)
1561 {
1562 nbdebug((" Skipping %s command\n", (char *) cmd));
1563 nb_reply_nil(cmdno);
1564 return OK;
1565 }
1566
1567 if (buf == NULL || buf->bufp == NULL)
1568 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001569 nbdebug((" invalid buffer identifier in remove\n"));
1570 EMSG("E634: invalid buffer identifier in remove");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 retval = FAIL;
1572 }
1573 else
1574 {
1575 netbeansFireChanges = FALSE;
1576 netbeansSuppressNoLines = TRUE;
1577
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001578 nb_set_curbuf(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 wasChanged = buf->bufp->b_changed;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001580 cp = (char *)args;
1581 off = strtol(cp, &cp, 10);
1582 count = strtol(cp, &cp, 10);
1583 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 /* delete "count" chars, starting at "off" */
1585 pos = off2pos(buf->bufp, off);
1586 if (!pos)
1587 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001588 nbdebug((" !bad position\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 nb_reply_text(cmdno, (char_u *)"!bad position");
1590 netbeansFireChanges = oldFire;
1591 netbeansSuppressNoLines = oldSuppress;
1592 return FAIL;
1593 }
1594 first = *pos;
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001595 nbdebug((" FIRST POS: line %d, col %d\n",
1596 first.lnum, first.col));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 pos = off2pos(buf->bufp, off+count-1);
1598 if (!pos)
1599 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001600 nbdebug((" !bad count\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 nb_reply_text(cmdno, (char_u *)"!bad count");
1602 netbeansFireChanges = oldFire;
1603 netbeansSuppressNoLines = oldSuppress;
1604 return FAIL;
1605 }
1606 last = *pos;
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001607 nbdebug((" LAST POS: line %d, col %d\n",
1608 last.lnum, last.col));
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001609 del_from_lnum = first.lnum;
1610 del_to_lnum = last.lnum;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001611 do_update = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001613 /* Get the position of the first byte after the deleted
1614 * section. "next" is NULL when deleting to the end of the
1615 * file. */
1616 next = off2pos(buf->bufp, off + count);
1617
1618 /* Remove part of the first line. */
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001619 if (first.col != 0
1620 || (next != NULL && first.lnum == next->lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 {
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001622 if (first.lnum != last.lnum
1623 || (next != NULL && first.lnum != next->lnum))
1624 {
1625 /* remove to the end of the first line */
1626 nb_partialremove(first.lnum, first.col,
1627 (colnr_T)MAXCOL);
1628 if (first.lnum == last.lnum)
1629 {
1630 /* Partial line to remove includes the end of
1631 * line. Join the line with the next one, have
1632 * the next line deleted below. */
1633 nb_joinlines(first.lnum, next->lnum);
1634 del_to_lnum = next->lnum;
1635 }
1636 }
1637 else
1638 {
1639 /* remove within one line */
1640 nb_partialremove(first.lnum, first.col, last.col);
1641 }
1642 ++del_from_lnum; /* don't delete the first line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 }
1644
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001645 /* Remove part of the last line. */
1646 if (first.lnum != last.lnum && next != NULL
1647 && next->col != 0 && last.lnum == next->lnum)
1648 {
1649 nb_partialremove(last.lnum, 0, last.col);
1650 if (del_from_lnum > first.lnum)
1651 {
1652 /* Join end of last line to start of first line; last
1653 * line is deleted below. */
1654 nb_joinlines(first.lnum, last.lnum);
1655 }
1656 else
1657 /* First line is deleted as a whole, keep the last
1658 * line. */
1659 --del_to_lnum;
1660 }
1661
1662 /* First is partial line; last line to remove includes
1663 * the end of line; join first line to line following last
1664 * line; line following last line is deleted below. */
1665 if (first.lnum != last.lnum && del_from_lnum > first.lnum
1666 && next != NULL && last.lnum != next->lnum)
1667 {
1668 nb_joinlines(first.lnum, next->lnum);
1669 del_to_lnum = next->lnum;
1670 }
1671
1672 /* Delete whole lines if there are any. */
1673 if (del_to_lnum >= del_from_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 {
1675 int i;
1676
1677 /* delete signs from the lines being deleted */
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001678 for (i = del_from_lnum; i <= del_to_lnum; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 {
1680 int id = buf_findsign_id(buf->bufp, (linenr_T)i);
1681 if (id > 0)
1682 {
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001683 nbdebug((" Deleting sign %d on line %d\n",
1684 id, i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 buf_delsign(buf->bufp, id);
1686 }
1687 else
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001688 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 nbdebug((" No sign on line %d\n", i));
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 }
1692
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001693 nbdebug((" Deleting lines %d through %d\n",
1694 del_from_lnum, del_to_lnum));
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001695 curwin->w_cursor.lnum = del_from_lnum;
1696 curwin->w_cursor.col = 0;
1697 del_lines(del_to_lnum - del_from_lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 }
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001699
1700 /* Leave cursor at first deleted byte. */
1701 curwin->w_cursor = first;
1702 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 buf->bufp->b_changed = wasChanged; /* logically unchanged */
1704 netbeansFireChanges = oldFire;
1705 netbeansSuppressNoLines = oldSuppress;
1706
1707 u_blockfree(buf->bufp);
1708 u_clearall(buf->bufp);
1709 }
1710 nb_reply_nil(cmdno);
1711/* =====================================================================*/
1712 }
1713 else if (streq((char *)cmd, "insert"))
1714 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 char_u *to_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716
1717 if (skip >= SKIP_STOP)
1718 {
1719 nbdebug((" Skipping %s command\n", (char *) cmd));
1720 nb_reply_nil(cmdno);
1721 return OK;
1722 }
1723
1724 /* get offset */
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001725 cp = (char *)args;
1726 off = strtol(cp, &cp, 10);
1727 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728
1729 /* get text to be inserted */
1730 args = skipwhite(args);
1731 args = to_free = (char_u *)nb_unquote(args, NULL);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001732 /*
1733 nbdebug((" CHUNK[%d]: %d bytes at offset %d\n",
1734 buf->bufp->b_ml.ml_line_count, STRLEN(args), off));
1735 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736
1737 if (buf == NULL || buf->bufp == NULL)
1738 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001739 nbdebug((" invalid buffer identifier in insert\n"));
1740 EMSG("E635: invalid buffer identifier in insert");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 retval = FAIL;
1742 }
1743 else if (args != NULL)
1744 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001745 int ff_detected = EOL_UNKNOWN;
1746 int buf_was_empty = (buf->bufp->b_ml.ml_flags & ML_EMPTY);
1747 size_t len = 0;
1748 int added = 0;
1749 int oldFire = netbeansFireChanges;
1750 int old_b_changed;
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001751 char_u *nlp;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001752 linenr_T lnum;
1753 linenr_T lnum_start;
1754 pos_T *pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 netbeansFireChanges = 0;
1757
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001758 /* Jump to the buffer where we insert. After this "curbuf"
1759 * can be used. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001760 nb_set_curbuf(buf->bufp);
Bram Moolenaara3227e22006-03-08 21:32:40 +00001761 old_b_changed = curbuf->b_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001763 /* Convert the specified character offset into a lnum/col
1764 * position. */
Bram Moolenaara3227e22006-03-08 21:32:40 +00001765 pos = off2pos(curbuf, off);
1766 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001768 if (pos->lnum <= 0)
1769 lnum_start = 1;
1770 else
1771 lnum_start = pos->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 }
1773 else
1774 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001775 /* If the given position is not found, assume we want
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 * the end of the file. See setLocAndSize HACK. */
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001777 if (buf_was_empty)
1778 lnum_start = 1; /* above empty line */
1779 else
1780 lnum_start = curbuf->b_ml.ml_line_count + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001781 }
1782
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001783 /* "lnum" is the line where we insert: either append to it or
1784 * insert a new line above it. */
1785 lnum = lnum_start;
1786
1787 /* Loop over the "\n" separated lines of the argument. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001788 do_update = 1;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001789 while (*args != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 {
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001791 nlp = vim_strchr(args, '\n');
1792 if (nlp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001794 /* Incomplete line, probably truncated. Next "insert"
1795 * command should append to this one. */
1796 len = STRLEN(args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001798 else
1799 {
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001800 len = nlp - args;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001801
1802 /*
1803 * We need to detect EOL style, because the commands
1804 * use a character offset.
1805 */
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001806 if (nlp > args && nlp[-1] == '\r')
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001807 {
1808 ff_detected = EOL_DOS;
1809 --len;
1810 }
1811 else
1812 ff_detected = EOL_UNIX;
1813 }
1814 args[len] = NUL;
1815
1816 if (lnum == lnum_start
1817 && ((pos != NULL && pos->col > 0)
1818 || (lnum == 1 && buf_was_empty)))
1819 {
1820 char_u *oldline = ml_get(lnum);
1821 char_u *newline;
1822
Bram Moolenaare4365282012-04-20 19:47:05 +02001823 /* Insert halfway a line. */
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001824 newline = alloc_check(
1825 (unsigned)(STRLEN(oldline) + len + 1));
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001826 if (newline != NULL)
1827 {
Bram Moolenaare4365282012-04-20 19:47:05 +02001828 mch_memmove(newline, oldline, (size_t)pos->col);
1829 newline[pos->col] = NUL;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001830 STRCAT(newline, args);
Bram Moolenaare4365282012-04-20 19:47:05 +02001831 STRCAT(newline, oldline + pos->col);
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001832 ml_replace(lnum, newline, FALSE);
1833 }
1834 }
1835 else
1836 {
1837 /* Append a new line. Not that we always do this,
1838 * also when the text doesn't end in a "\n". */
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001839 ml_append((linenr_T)(lnum - 1), args,
1840 (colnr_T)(len + 1), FALSE);
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001841 ++added;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001842 }
1843
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001844 if (nlp == NULL)
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001845 break;
1846 ++lnum;
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001847 args = nlp + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001848 }
1849
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001850 /* Adjust the marks below the inserted lines. */
1851 appended_lines_mark(lnum_start - 1, (long)added);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001853 /*
1854 * When starting with an empty buffer set the fileformat.
1855 * This is just guessing...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 */
1857 if (buf_was_empty)
1858 {
1859 if (ff_detected == EOL_UNKNOWN)
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001860#if defined(MSDOS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 ff_detected = EOL_DOS;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001862#else
1863 ff_detected = EOL_UNIX;
1864#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 set_fileformat(ff_detected, OPT_LOCAL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00001866 curbuf->b_start_ffc = *curbuf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 }
1868
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 /*
1870 * XXX - GRP - Is the next line right? If I've inserted
1871 * text the buffer has been updated but not written. Will
1872 * netbeans guarantee to write it? Even if I do a :q! ?
1873 */
Bram Moolenaara3227e22006-03-08 21:32:40 +00001874 curbuf->b_changed = old_b_changed; /* logically unchanged */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 netbeansFireChanges = oldFire;
1876
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001877 /* Undo info is invalid now... */
Bram Moolenaara3227e22006-03-08 21:32:40 +00001878 u_blockfree(curbuf);
1879 u_clearall(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 }
1881 vim_free(to_free);
1882 nb_reply_nil(cmdno); /* or !error */
1883 }
1884 else
1885 {
1886 nbdebug(("UNIMPLEMENTED FUNCTION: %s\n", cmd));
1887 nb_reply_nil(cmdno);
1888 retval = FAIL;
1889 }
1890 }
1891 else /* Not a function; no reply required. */
1892 {
1893/* =====================================================================*/
1894 if (streq((char *)cmd, "create"))
1895 {
1896 /* Create a buffer without a name. */
1897 if (buf == NULL)
1898 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001899 nbdebug((" invalid buffer identifier in create\n"));
1900 EMSG("E636: invalid buffer identifier in create");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 return FAIL;
1902 }
1903 vim_free(buf->displayname);
1904 buf->displayname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905
1906 netbeansReadFile = 0; /* don't try to open disk file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001907 do_ecmd(0, NULL, 0, 0, ECMD_ONE, ECMD_HIDE + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 netbeansReadFile = 1;
1909 buf->bufp = curbuf;
1910 maketitle();
Bram Moolenaar009b2592004-10-24 19:18:58 +00001911 buf->insertDone = FALSE;
Bram Moolenaar67c53842010-05-22 18:28:27 +02001912#if defined(FEAT_MENU) && defined(FEAT_GUI)
Bram Moolenaard54a6882010-08-09 22:49:00 +02001913 if (gui.in_use)
1914 gui_update_menus(0);
Bram Moolenaar67c53842010-05-22 18:28:27 +02001915#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916/* =====================================================================*/
1917 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001918 else if (streq((char *)cmd, "insertDone"))
1919 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001920 if (buf == NULL || buf->bufp == NULL)
1921 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001922 nbdebug((" invalid buffer identifier in insertDone\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001923 }
1924 else
1925 {
1926 buf->bufp->b_start_eol = *args == 'T';
1927 buf->insertDone = TRUE;
1928 args += 2;
1929 buf->bufp->b_p_ro = *args == 'T';
1930 print_read_msg(buf);
1931 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001932/* =====================================================================*/
1933 }
1934 else if (streq((char *)cmd, "saveDone"))
1935 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001936 long savedChars = atol((char *)args);
1937
1938 if (buf == NULL || buf->bufp == NULL)
1939 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001940 nbdebug((" invalid buffer identifier in saveDone\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001941 }
1942 else
1943 print_save_msg(buf, savedChars);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001944/* =====================================================================*/
1945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 else if (streq((char *)cmd, "startDocumentListen"))
1947 {
1948 if (buf == NULL)
1949 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001950 nbdebug((" invalid buffer identifier in startDocumentListen\n"));
1951 EMSG("E637: invalid buffer identifier in startDocumentListen");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 return FAIL;
1953 }
1954 buf->fireChanges = 1;
1955/* =====================================================================*/
1956 }
1957 else if (streq((char *)cmd, "stopDocumentListen"))
1958 {
1959 if (buf == NULL)
1960 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001961 nbdebug((" invalid buffer identifier in stopDocumentListen\n"));
1962 EMSG("E638: invalid buffer identifier in stopDocumentListen");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 return FAIL;
1964 }
1965 buf->fireChanges = 0;
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001966 if (buf->bufp != NULL && buf->bufp->b_was_netbeans_file)
Bram Moolenaar009b2592004-10-24 19:18:58 +00001967 {
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001968 if (!buf->bufp->b_netbeans_file)
Bram Moolenaarf2330482008-06-24 20:19:36 +00001969 {
1970 nbdebug(("E658: NetBeans connection lost for buffer %ld\n", buf->bufp->b_fnum));
Bram Moolenaar009b2592004-10-24 19:18:58 +00001971 EMSGN(_("E658: NetBeans connection lost for buffer %ld"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 buf->bufp->b_fnum);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001973 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001974 else
1975 {
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001976 /* NetBeans uses stopDocumentListen when it stops editing
1977 * a file. It then expects the buffer in Vim to
1978 * disappear. */
1979 do_bufdel(DOBUF_DEL, (char_u *)"", 1,
1980 buf->bufp->b_fnum, buf->bufp->b_fnum, TRUE);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001981 vim_memset(buf, 0, sizeof(nbbuf_T));
1982 }
1983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984/* =====================================================================*/
1985 }
1986 else if (streq((char *)cmd, "setTitle"))
1987 {
1988 if (buf == NULL)
1989 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001990 nbdebug((" invalid buffer identifier in setTitle\n"));
1991 EMSG("E639: invalid buffer identifier in setTitle");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 return FAIL;
1993 }
1994 vim_free(buf->displayname);
1995 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996/* =====================================================================*/
1997 }
1998 else if (streq((char *)cmd, "initDone"))
1999 {
2000 if (buf == NULL || buf->bufp == NULL)
2001 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002002 nbdebug((" invalid buffer identifier in initDone\n"));
2003 EMSG("E640: invalid buffer identifier in initDone");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 return FAIL;
2005 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002006 do_update = 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002007 buf->initDone = TRUE;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002008 nb_set_curbuf(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009#if defined(FEAT_AUTOCMD)
2010 apply_autocmds(EVENT_BUFREADPOST, 0, 0, FALSE, buf->bufp);
2011#endif
2012
2013 /* handle any postponed key commands */
2014 handle_key_queue();
2015/* =====================================================================*/
2016 }
2017 else if (streq((char *)cmd, "setBufferNumber")
2018 || streq((char *)cmd, "putBufferNumber"))
2019 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002020 char_u *path;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 buf_T *bufp;
2022
2023 if (buf == NULL)
2024 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002025 nbdebug((" invalid buffer identifier in setBufferNumber\n"));
2026 EMSG("E641: invalid buffer identifier in setBufferNumber");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 return FAIL;
2028 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002029 path = (char_u *)nb_unquote(args, NULL);
2030 if (path == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 return FAIL;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002032 bufp = buflist_findname(path);
2033 vim_free(path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 if (bufp == NULL)
2035 {
Bram Moolenaarec906222009-02-21 21:14:00 +00002036 nbdebug((" File %s not found in setBufferNumber\n", args));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 EMSG2("E642: File %s not found in setBufferNumber", args);
2038 return FAIL;
2039 }
2040 buf->bufp = bufp;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002041 buf->nbbuf_number = bufp->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042
2043 /* "setBufferNumber" has the side effect of jumping to the buffer
2044 * (don't know why!). Don't do that for "putBufferNumber". */
2045 if (*cmd != 'p')
2046 coloncmd(":buffer %d", bufp->b_fnum);
2047 else
2048 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002049 buf->initDone = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050
2051 /* handle any postponed key commands */
2052 handle_key_queue();
2053 }
2054
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055/* =====================================================================*/
2056 }
2057 else if (streq((char *)cmd, "setFullName"))
2058 {
2059 if (buf == NULL)
2060 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002061 nbdebug((" invalid buffer identifier in setFullName\n"));
2062 EMSG("E643: invalid buffer identifier in setFullName");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 return FAIL;
2064 }
2065 vim_free(buf->displayname);
2066 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067
2068 netbeansReadFile = 0; /* don't try to open disk file */
2069 do_ecmd(0, (char_u *)buf->displayname, 0, 0, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002070 ECMD_HIDE + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 netbeansReadFile = 1;
2072 buf->bufp = curbuf;
2073 maketitle();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002074#if defined(FEAT_MENU) && defined(FEAT_GUI)
Bram Moolenaard54a6882010-08-09 22:49:00 +02002075 if (gui.in_use)
2076 gui_update_menus(0);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002077#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078/* =====================================================================*/
2079 }
2080 else if (streq((char *)cmd, "editFile"))
2081 {
2082 if (buf == NULL)
2083 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002084 nbdebug((" invalid buffer identifier in editFile\n"));
2085 EMSG("E644: invalid buffer identifier in editFile");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 return FAIL;
2087 }
2088 /* Edit a file: like create + setFullName + read the file. */
2089 vim_free(buf->displayname);
2090 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 do_ecmd(0, (char_u *)buf->displayname, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002092 ECMD_HIDE + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 buf->bufp = curbuf;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002094 buf->initDone = TRUE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002095 do_update = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096#if defined(FEAT_TITLE)
2097 maketitle();
2098#endif
Bram Moolenaar67c53842010-05-22 18:28:27 +02002099#if defined(FEAT_MENU) && defined(FEAT_GUI)
Bram Moolenaard54a6882010-08-09 22:49:00 +02002100 if (gui.in_use)
2101 gui_update_menus(0);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002102#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103/* =====================================================================*/
2104 }
2105 else if (streq((char *)cmd, "setVisible"))
2106 {
2107 if (buf == NULL || buf->bufp == NULL)
2108 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002109 nbdebug((" invalid buffer identifier in setVisible\n"));
2110 /* This message was commented out, probably because it can
2111 * happen when shutting down. */
2112 if (p_verbose > 0)
2113 EMSG("E645: invalid buffer identifier in setVisible");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 return FAIL;
2115 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002116 if (streq((char *)args, "T") && buf->bufp != curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 {
2118 exarg_T exarg;
2119 exarg.cmd = (char_u *)"goto";
2120 exarg.forceit = FALSE;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002121 dosetvisible = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 goto_buffer(&exarg, DOBUF_FIRST, FORWARD, buf->bufp->b_fnum);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002123 do_update = 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002124 dosetvisible = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125
Bram Moolenaar67c53842010-05-22 18:28:27 +02002126#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 /* Side effect!!!. */
Bram Moolenaard54a6882010-08-09 22:49:00 +02002128 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 gui_mch_set_foreground();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132/* =====================================================================*/
2133 }
2134 else if (streq((char *)cmd, "raise"))
2135 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002136#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 /* Bring gvim to the foreground. */
Bram Moolenaard54a6882010-08-09 22:49:00 +02002138 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 gui_mch_set_foreground();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141/* =====================================================================*/
2142 }
2143 else if (streq((char *)cmd, "setModified"))
2144 {
Bram Moolenaarff064e12008-06-09 13:10:45 +00002145 int prev_b_changed;
2146
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 if (buf == NULL || buf->bufp == NULL)
2148 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002149 nbdebug((" invalid buffer identifier in setModified\n"));
2150 /* This message was commented out, probably because it can
2151 * happen when shutting down. */
2152 if (p_verbose > 0)
2153 EMSG("E646: invalid buffer identifier in setModified");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 return FAIL;
2155 }
Bram Moolenaarff064e12008-06-09 13:10:45 +00002156 prev_b_changed = buf->bufp->b_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 if (streq((char *)args, "T"))
Bram Moolenaarff064e12008-06-09 13:10:45 +00002158 buf->bufp->b_changed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 else
2160 {
2161 struct stat st;
2162
2163 /* Assume NetBeans stored the file. Reset the timestamp to
2164 * avoid "file changed" warnings. */
2165 if (buf->bufp->b_ffname != NULL
2166 && mch_stat((char *)buf->bufp->b_ffname, &st) >= 0)
2167 buf_store_time(buf->bufp, &st, buf->bufp->b_ffname);
Bram Moolenaarff064e12008-06-09 13:10:45 +00002168 buf->bufp->b_changed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 }
2170 buf->modified = buf->bufp->b_changed;
Bram Moolenaarff064e12008-06-09 13:10:45 +00002171 if (prev_b_changed != buf->bufp->b_changed)
2172 {
2173#ifdef FEAT_WINDOWS
2174 check_status(buf->bufp);
2175 redraw_tabline = TRUE;
2176#endif
2177#ifdef FEAT_TITLE
2178 maketitle();
2179#endif
2180 update_screen(0);
2181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182/* =====================================================================*/
2183 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002184 else if (streq((char *)cmd, "setModtime"))
2185 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002186 if (buf == NULL || buf->bufp == NULL)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002187 nbdebug((" invalid buffer identifier in setModtime\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002188 else
2189 buf->bufp->b_mtime = atoi((char *)args);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002190/* =====================================================================*/
2191 }
2192 else if (streq((char *)cmd, "setReadOnly"))
2193 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002194 if (buf == NULL || buf->bufp == NULL)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002195 nbdebug((" invalid buffer identifier in setReadOnly\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002196 else if (streq((char *)args, "T"))
Bram Moolenaar009b2592004-10-24 19:18:58 +00002197 buf->bufp->b_p_ro = TRUE;
2198 else
2199 buf->bufp->b_p_ro = FALSE;
2200/* =====================================================================*/
2201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 else if (streq((char *)cmd, "setMark"))
2203 {
2204 /* not yet */
2205/* =====================================================================*/
2206 }
2207 else if (streq((char *)cmd, "showBalloon"))
2208 {
2209#if defined(FEAT_BEVAL)
2210 static char *text = NULL;
2211
2212 /*
2213 * Set up the Balloon Expression Evaluation area.
2214 * Ignore 'ballooneval' here.
2215 * The text pointer must remain valid for a while.
2216 */
2217 if (balloonEval != NULL)
2218 {
2219 vim_free(text);
2220 text = nb_unquote(args, NULL);
2221 if (text != NULL)
2222 gui_mch_post_balloon(balloonEval, (char_u *)text);
2223 }
2224#endif
2225/* =====================================================================*/
2226 }
2227 else if (streq((char *)cmd, "setDot"))
2228 {
2229 pos_T *pos;
2230#ifdef NBDEBUG
2231 char_u *s;
2232#endif
2233
2234 if (buf == NULL || buf->bufp == NULL)
2235 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002236 nbdebug((" invalid buffer identifier in setDot\n"));
2237 EMSG("E647: invalid buffer identifier in setDot");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 return FAIL;
2239 }
2240
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002241 nb_set_curbuf(buf->bufp);
2242
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 /* Don't want Visual mode now. */
2244 if (VIsual_active)
2245 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246#ifdef NBDEBUG
2247 s = args;
2248#endif
2249 pos = get_off_or_lnum(buf->bufp, &args);
2250 if (pos)
2251 {
2252 curwin->w_cursor = *pos;
2253 check_cursor();
2254#ifdef FEAT_FOLDING
2255 foldOpenCursor();
2256#endif
2257 }
2258 else
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002259 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 nbdebug((" BAD POSITION in setDot: %s\n", s));
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262
2263 /* gui_update_cursor(TRUE, FALSE); */
2264 /* update_curbuf(NOT_VALID); */
2265 update_topline(); /* scroll to show the line */
2266 update_screen(VALID);
2267 setcursor();
Bram Moolenaar96bcc5e2011-04-01 15:33:59 +02002268 cursor_on();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 out_flush();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002270#ifdef FEAT_GUI
Bram Moolenaard54a6882010-08-09 22:49:00 +02002271 if (gui.in_use)
2272 {
2273 gui_update_cursor(TRUE, FALSE);
2274 gui_mch_flush();
2275 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02002276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 /* Quit a hit-return or more prompt. */
2278 if (State == HITRETURN || State == ASKMORE)
2279 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280#ifdef FEAT_GUI_GTK
Bram Moolenaard54a6882010-08-09 22:49:00 +02002281 if (gui.in_use && gtk_main_level() > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 gtk_main_quit();
2283#endif
2284 }
2285/* =====================================================================*/
2286 }
2287 else if (streq((char *)cmd, "close"))
2288 {
2289#ifdef NBDEBUG
2290 char *name = "<NONE>";
2291#endif
2292
2293 if (buf == NULL)
2294 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002295 nbdebug((" invalid buffer identifier in close\n"));
2296 EMSG("E648: invalid buffer identifier in close");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 return FAIL;
2298 }
2299
2300#ifdef NBDEBUG
2301 if (buf->displayname != NULL)
2302 name = buf->displayname;
2303#endif
Bram Moolenaarf2330482008-06-24 20:19:36 +00002304 if (buf->bufp == NULL)
2305 {
2306 nbdebug((" invalid buffer identifier in close\n"));
2307 /* This message was commented out, probably because it can
2308 * happen when shutting down. */
2309 if (p_verbose > 0)
2310 EMSG("E649: invalid buffer identifier in close");
2311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 nbdebug((" CLOSE %d: %s\n", bufno, name));
Bram Moolenaar67c53842010-05-22 18:28:27 +02002313#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 need_mouse_correct = TRUE;
Bram Moolenaar67c53842010-05-22 18:28:27 +02002315#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 if (buf->bufp != NULL)
2317 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD,
2318 buf->bufp->b_fnum, TRUE);
Bram Moolenaar8d602722006-08-08 19:34:19 +00002319 buf->bufp = NULL;
2320 buf->initDone = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002321 do_update = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322/* =====================================================================*/
2323 }
2324 else if (streq((char *)cmd, "setStyle")) /* obsolete... */
2325 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002326 nbdebug((" setStyle is obsolete!\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327/* =====================================================================*/
2328 }
2329 else if (streq((char *)cmd, "setExitDelay"))
2330 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002331 /* Only used in version 2.1. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332/* =====================================================================*/
2333 }
2334 else if (streq((char *)cmd, "defineAnnoType"))
2335 {
2336#ifdef FEAT_SIGNS
2337 int typeNum;
2338 char_u *typeName;
2339 char_u *tooltip;
2340 char_u *p;
2341 char_u *glyphFile;
Bram Moolenaar67c53842010-05-22 18:28:27 +02002342 int parse_error = FALSE;
2343 char_u *fg;
2344 char_u *bg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345
2346 if (buf == NULL)
2347 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002348 nbdebug((" invalid buffer identifier in defineAnnoType\n"));
2349 EMSG("E650: invalid buffer identifier in defineAnnoType");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 return FAIL;
2351 }
2352
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002353 cp = (char *)args;
2354 typeNum = strtol(cp, &cp, 10);
2355 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 args = skipwhite(args);
2357 typeName = (char_u *)nb_unquote(args, &args);
2358 args = skipwhite(args + 1);
2359 tooltip = (char_u *)nb_unquote(args, &args);
2360 args = skipwhite(args + 1);
2361
2362 p = (char_u *)nb_unquote(args, &args);
2363 glyphFile = vim_strsave_escaped(p, escape_chars);
2364 vim_free(p);
2365
2366 args = skipwhite(args + 1);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002367 p = skiptowhite(args);
2368 if (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002370 *p = NUL;
2371 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02002373 fg = vim_strsave(args);
2374 bg = vim_strsave(p);
2375 if (STRLEN(fg) > MAX_COLOR_LENGTH || STRLEN(bg) > MAX_COLOR_LENGTH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002377 EMSG("E532: highlighting color name too long in defineAnnoType");
2378 vim_free(typeName);
2379 parse_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02002381 else if (typeName != NULL && tooltip != NULL && glyphFile != NULL)
2382 addsigntype(buf, typeNum, typeName, tooltip, glyphFile, fg, bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 else
2384 vim_free(typeName);
2385
2386 /* don't free typeName; it's used directly in addsigntype() */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002387 vim_free(fg);
2388 vim_free(bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 vim_free(tooltip);
2390 vim_free(glyphFile);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002391 if (parse_error)
2392 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393
2394#endif
2395/* =====================================================================*/
2396 }
2397 else if (streq((char *)cmd, "addAnno"))
2398 {
2399#ifdef FEAT_SIGNS
2400 int serNum;
2401 int localTypeNum;
2402 int typeNum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 pos_T *pos;
2404
2405 if (buf == NULL || buf->bufp == NULL)
2406 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002407 nbdebug((" invalid buffer identifier in addAnno\n"));
2408 EMSG("E651: invalid buffer identifier in addAnno");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 return FAIL;
2410 }
2411
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002412 do_update = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002414 cp = (char *)args;
2415 serNum = strtol(cp, &cp, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416
2417 /* Get the typenr specific for this buffer and convert it to
2418 * the global typenumber, as used for the sign name. */
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002419 localTypeNum = strtol(cp, &cp, 10);
2420 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 typeNum = mapsigntype(buf, localTypeNum);
2422
2423 pos = get_off_or_lnum(buf->bufp, &args);
2424
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002425 cp = (char *)args;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002426 ignored = (int)strtol(cp, &cp, 10);
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002427 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428# ifdef NBDEBUG
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002429 if (ignored != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002431 nbdebug((" partial line annotation -- Not Yet Implemented!\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 }
2433# endif
2434 if (serNum >= GUARDEDOFFSET)
2435 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002436 nbdebug((" too many annotations! ignoring...\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 return FAIL;
2438 }
2439 if (pos)
2440 {
Bram Moolenaarec906222009-02-21 21:14:00 +00002441 coloncmd(":sign place %d line=%ld name=%d buffer=%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 serNum, pos->lnum, typeNum, buf->bufp->b_fnum);
2443 if (typeNum == curPCtype)
2444 coloncmd(":sign jump %d buffer=%d", serNum,
2445 buf->bufp->b_fnum);
2446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447#endif
2448/* =====================================================================*/
2449 }
2450 else if (streq((char *)cmd, "removeAnno"))
2451 {
2452#ifdef FEAT_SIGNS
2453 int serNum;
2454
2455 if (buf == NULL || buf->bufp == NULL)
2456 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002457 nbdebug((" invalid buffer identifier in removeAnno\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 return FAIL;
2459 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002460 do_update = 1;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002461 cp = (char *)args;
2462 serNum = strtol(cp, &cp, 10);
2463 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 coloncmd(":sign unplace %d buffer=%d",
2465 serNum, buf->bufp->b_fnum);
2466 redraw_buf_later(buf->bufp, NOT_VALID);
2467#endif
2468/* =====================================================================*/
2469 }
2470 else if (streq((char *)cmd, "moveAnnoToFront"))
2471 {
2472#ifdef FEAT_SIGNS
Bram Moolenaarf2330482008-06-24 20:19:36 +00002473 nbdebug((" moveAnnoToFront: Not Yet Implemented!\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474#endif
2475/* =====================================================================*/
2476 }
2477 else if (streq((char *)cmd, "guard") || streq((char *)cmd, "unguard"))
2478 {
2479 int len;
2480 pos_T first;
2481 pos_T last;
2482 pos_T *pos;
2483 int un = (cmd[0] == 'u');
2484 static int guardId = GUARDEDOFFSET;
2485
2486 if (skip >= SKIP_STOP)
2487 {
2488 nbdebug((" Skipping %s command\n", (char *) cmd));
2489 return OK;
2490 }
2491
2492 nb_init_graphics();
2493
2494 if (buf == NULL || buf->bufp == NULL)
2495 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002496 nbdebug((" invalid buffer identifier in %s command\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 return FAIL;
2498 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002499 nb_set_curbuf(buf->bufp);
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002500 cp = (char *)args;
2501 off = strtol(cp, &cp, 10);
2502 len = strtol(cp, NULL, 10);
2503 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 pos = off2pos(buf->bufp, off);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002505 do_update = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 if (!pos)
2507 nbdebug((" no such start pos in %s, %ld\n", cmd, off));
2508 else
2509 {
2510 first = *pos;
2511 pos = off2pos(buf->bufp, off + len - 1);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002512 if (pos != NULL && pos->col == 0)
2513 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 /*
2515 * In Java Swing the offset is a position between 2
2516 * characters. If col == 0 then we really want the
2517 * previous line as the end.
2518 */
2519 pos = off2pos(buf->bufp, off + len - 2);
2520 }
2521 if (!pos)
2522 nbdebug((" no such end pos in %s, %ld\n",
2523 cmd, off + len - 1));
2524 else
2525 {
2526 long lnum;
2527 last = *pos;
2528 /* set highlight for region */
2529 nbdebug((" %sGUARD %ld,%d to %ld,%d\n", (un) ? "UN" : "",
2530 first.lnum, first.col,
2531 last.lnum, last.col));
2532#ifdef FEAT_SIGNS
2533 for (lnum = first.lnum; lnum <= last.lnum; lnum++)
2534 {
2535 if (un)
2536 {
2537 /* never used */
2538 }
2539 else
2540 {
2541 if (buf_findsigntype_id(buf->bufp, lnum,
2542 GUARDED) == 0)
2543 {
2544 coloncmd(
Bram Moolenaarec906222009-02-21 21:14:00 +00002545 ":sign place %d line=%ld name=%d buffer=%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 guardId++, lnum, GUARDED,
2547 buf->bufp->b_fnum);
2548 }
2549 }
2550 }
2551#endif
2552 redraw_buf_later(buf->bufp, NOT_VALID);
2553 }
2554 }
2555/* =====================================================================*/
2556 }
2557 else if (streq((char *)cmd, "startAtomic"))
2558 {
2559 inAtomic = 1;
2560/* =====================================================================*/
2561 }
2562 else if (streq((char *)cmd, "endAtomic"))
2563 {
2564 inAtomic = 0;
2565 if (needupdate)
2566 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002567 do_update = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 needupdate = 0;
2569 }
2570/* =====================================================================*/
2571 }
2572 else if (streq((char *)cmd, "save"))
2573 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002574 /*
2575 * NOTE - This command is obsolete wrt NetBeans. Its left in
2576 * only for historical reasons.
2577 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 if (buf == NULL || buf->bufp == NULL)
2579 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002580 nbdebug((" invalid buffer identifier in %s command\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 return FAIL;
2582 }
2583
2584 /* the following is taken from ex_cmds.c (do_wqall function) */
2585 if (bufIsChanged(buf->bufp))
2586 {
2587 /* Only write if the buffer can be written. */
2588 if (p_write
2589 && !buf->bufp->b_p_ro
2590 && buf->bufp->b_ffname != NULL
2591#ifdef FEAT_QUICKFIX
2592 && !bt_dontwrite(buf->bufp)
2593#endif
2594 )
2595 {
2596 buf_write_all(buf->bufp, FALSE);
2597#ifdef FEAT_AUTOCMD
2598 /* an autocommand may have deleted the buffer */
2599 if (!buf_valid(buf->bufp))
2600 buf->bufp = NULL;
2601#endif
2602 }
2603 }
Bram Moolenaarf2330482008-06-24 20:19:36 +00002604 else
2605 {
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002606 nbdebug((" Buffer has no changes!\n"));
Bram Moolenaarf2330482008-06-24 20:19:36 +00002607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608/* =====================================================================*/
2609 }
2610 else if (streq((char *)cmd, "netbeansBuffer"))
2611 {
2612 if (buf == NULL || buf->bufp == NULL)
2613 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002614 nbdebug((" invalid buffer identifier in %s command\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 return FAIL;
2616 }
2617 if (*args == 'T')
2618 {
2619 buf->bufp->b_netbeans_file = TRUE;
2620 buf->bufp->b_was_netbeans_file = TRUE;
2621 }
2622 else
2623 buf->bufp->b_netbeans_file = FALSE;
2624/* =====================================================================*/
2625 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002626 else if (streq((char *)cmd, "specialKeys"))
2627 {
2628 special_keys(args);
2629/* =====================================================================*/
2630 }
2631 else if (streq((char *)cmd, "actionMenuItem"))
2632 {
2633 /* not used yet */
2634/* =====================================================================*/
2635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 else if (streq((char *)cmd, "version"))
2637 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002638 /* not used yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 }
Bram Moolenaarf2330482008-06-24 20:19:36 +00002640 else
2641 {
2642 nbdebug(("Unrecognised command: %s\n", cmd));
2643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 /*
2645 * Unrecognized command is ignored.
2646 */
2647 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002648 if (inAtomic && do_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 {
2650 needupdate = 1;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002651 do_update = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 }
2653
Bram Moolenaar009b2592004-10-24 19:18:58 +00002654 /*
2655 * Is this needed? I moved the netbeans_Xt_connect() later during startup
2656 * and it may no longer be necessary. If its not needed then needupdate
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002657 * and do_update can also be removed.
Bram Moolenaar009b2592004-10-24 19:18:58 +00002658 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002659 if (buf != NULL && buf->initDone && do_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 {
2661 update_screen(NOT_VALID);
2662 setcursor();
Bram Moolenaar96bcc5e2011-04-01 15:33:59 +02002663 cursor_on();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 out_flush();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002665#ifdef FEAT_GUI
Bram Moolenaard54a6882010-08-09 22:49:00 +02002666 if (gui.in_use)
2667 {
2668 gui_update_cursor(TRUE, FALSE);
2669 gui_mch_flush();
2670 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02002671#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 /* Quit a hit-return or more prompt. */
2673 if (State == HITRETURN || State == ASKMORE)
2674 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675#ifdef FEAT_GUI_GTK
Bram Moolenaard54a6882010-08-09 22:49:00 +02002676 if (gui.in_use && gtk_main_level() > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 gtk_main_quit();
2678#endif
2679 }
2680 }
2681
2682 return retval;
2683}
2684
2685
2686/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002687 * If "buf" is not the current buffer try changing to a window that edits this
2688 * buffer. If there is no such window then close the current buffer and set
2689 * the current buffer as "buf".
2690 */
2691 static void
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00002692nb_set_curbuf(buf_T *buf)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002693{
Bram Moolenaar404c9422015-03-14 15:35:52 +01002694 if (curbuf != buf) {
2695 if (buf_jump_open_win(buf) != NULL)
2696 return;
2697# ifdef FEAT_WINDOWS
2698 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf) != NULL)
2699 return;
2700# endif
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002701 set_curbuf(buf, DOBUF_GOTO);
Bram Moolenaar404c9422015-03-14 15:35:52 +01002702 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002703}
2704
2705/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 * Process a vim colon command.
2707 */
2708 static void
2709coloncmd(char *cmd, ...)
2710{
2711 char buf[1024];
2712 va_list ap;
2713
2714 va_start(ap, cmd);
Bram Moolenaar0dc79e82009-06-24 14:50:12 +00002715 vim_vsnprintf(buf, sizeof(buf), cmd, ap, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 va_end(ap);
2717
2718 nbdebug((" COLONCMD %s\n", buf));
2719
2720/* ALT_INPUT_LOCK_ON; */
2721 do_cmdline((char_u *)buf, NULL, NULL, DOCMD_NOWAIT | DOCMD_KEYTYPED);
2722/* ALT_INPUT_LOCK_OFF; */
2723
2724 setcursor(); /* restore the cursor position */
2725 out_flush(); /* make sure output has been written */
2726
Bram Moolenaar67c53842010-05-22 18:28:27 +02002727#ifdef FEAT_GUI
Bram Moolenaard54a6882010-08-09 22:49:00 +02002728 if (gui.in_use)
2729 {
2730 gui_update_cursor(TRUE, FALSE);
2731 gui_mch_flush();
2732 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02002733#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734}
2735
2736
2737/*
Bram Moolenaar009b2592004-10-24 19:18:58 +00002738 * Parse the specialKeys argument and issue the appropriate map commands.
2739 */
2740 static void
2741special_keys(char_u *args)
2742{
2743 char *save_str = nb_unquote(args, NULL);
2744 char *tok = strtok(save_str, " ");
2745 char *sep;
2746 char keybuf[64];
2747 char cmdbuf[256];
2748
2749 while (tok != NULL)
2750 {
2751 int i = 0;
2752
2753 if ((sep = strchr(tok, '-')) != NULL)
2754 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002755 *sep = NUL;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002756 while (*tok)
2757 {
2758 switch (*tok)
2759 {
2760 case 'A':
2761 case 'M':
2762 case 'C':
2763 case 'S':
2764 keybuf[i++] = *tok;
2765 keybuf[i++] = '-';
2766 break;
2767 }
2768 tok++;
2769 }
2770 tok++;
2771 }
2772
2773 strcpy(&keybuf[i], tok);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002774 vim_snprintf(cmdbuf, sizeof(cmdbuf),
2775 "<silent><%s> :nbkey %s<CR>", keybuf, keybuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002776 do_map(0, (char_u *)cmdbuf, NORMAL, FALSE);
2777 tok = strtok(NULL, " ");
2778 }
2779 vim_free(save_str);
2780}
2781
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002782 void
2783ex_nbclose(eap)
2784 exarg_T *eap UNUSED;
2785{
2786 netbeans_close();
2787}
Bram Moolenaar009b2592004-10-24 19:18:58 +00002788
2789 void
2790ex_nbkey(eap)
2791 exarg_T *eap;
2792{
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002793 (void)netbeans_keystring(eap->arg);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002794}
2795
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002796 void
2797ex_nbstart(eap)
2798 exarg_T *eap;
2799{
Bram Moolenaarc2a406b2010-09-30 21:03:26 +02002800#ifdef FEAT_GUI
2801# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar7ad7d012010-11-16 15:49:02 +01002802 && !defined(FEAT_GUI_W32)
Bram Moolenaarc2a406b2010-09-30 21:03:26 +02002803 if (gui.in_use)
2804 {
Bram Moolenaar7ad7d012010-11-16 15:49:02 +01002805 EMSG(_("E838: netbeans is not supported with this GUI"));
2806 return;
Bram Moolenaarc2a406b2010-09-30 21:03:26 +02002807 }
2808# endif
2809#endif
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002810 netbeans_open((char *)eap->arg, FALSE);
2811}
Bram Moolenaar009b2592004-10-24 19:18:58 +00002812
2813/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 * Initialize highlights and signs for use by netbeans (mostly obsolete)
2815 */
2816 static void
2817nb_init_graphics(void)
2818{
2819 static int did_init = FALSE;
2820
2821 if (!did_init)
2822 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002823 coloncmd(":highlight NBGuarded guibg=Cyan guifg=Black"
2824 " ctermbg=LightCyan ctermfg=Black");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 coloncmd(":sign define %d linehl=NBGuarded", GUARDED);
2826
2827 did_init = TRUE;
2828 }
2829}
2830
2831/*
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002832 * Convert key to netbeans name. This uses the global "mod_mask".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 */
2834 static void
2835netbeans_keyname(int key, char *buf)
2836{
2837 char *name = 0;
2838 char namebuf[2];
2839 int ctrl = 0;
2840 int shift = 0;
2841 int alt = 0;
2842
2843 if (mod_mask & MOD_MASK_CTRL)
2844 ctrl = 1;
2845 if (mod_mask & MOD_MASK_SHIFT)
2846 shift = 1;
2847 if (mod_mask & MOD_MASK_ALT)
2848 alt = 1;
2849
2850
2851 switch (key)
2852 {
2853 case K_F1: name = "F1"; break;
2854 case K_S_F1: name = "F1"; shift = 1; break;
2855 case K_F2: name = "F2"; break;
2856 case K_S_F2: name = "F2"; shift = 1; break;
2857 case K_F3: name = "F3"; break;
2858 case K_S_F3: name = "F3"; shift = 1; break;
2859 case K_F4: name = "F4"; break;
2860 case K_S_F4: name = "F4"; shift = 1; break;
2861 case K_F5: name = "F5"; break;
2862 case K_S_F5: name = "F5"; shift = 1; break;
2863 case K_F6: name = "F6"; break;
2864 case K_S_F6: name = "F6"; shift = 1; break;
2865 case K_F7: name = "F7"; break;
2866 case K_S_F7: name = "F7"; shift = 1; break;
2867 case K_F8: name = "F8"; break;
2868 case K_S_F8: name = "F8"; shift = 1; break;
2869 case K_F9: name = "F9"; break;
2870 case K_S_F9: name = "F9"; shift = 1; break;
2871 case K_F10: name = "F10"; break;
2872 case K_S_F10: name = "F10"; shift = 1; break;
2873 case K_F11: name = "F11"; break;
2874 case K_S_F11: name = "F11"; shift = 1; break;
2875 case K_F12: name = "F12"; break;
2876 case K_S_F12: name = "F12"; shift = 1; break;
2877 default:
2878 if (key >= ' ' && key <= '~')
2879 {
2880 /* Allow ASCII characters. */
2881 name = namebuf;
2882 namebuf[0] = key;
2883 namebuf[1] = NUL;
2884 }
2885 else
2886 name = "X";
2887 break;
2888 }
2889
2890 buf[0] = '\0';
2891 if (ctrl)
2892 strcat(buf, "C");
2893 if (shift)
2894 strcat(buf, "S");
2895 if (alt)
2896 strcat(buf, "M"); /* META */
2897 if (ctrl || shift || alt)
2898 strcat(buf, "-");
2899 strcat(buf, name);
2900}
2901
Bram Moolenaar67c53842010-05-22 18:28:27 +02002902#if defined(FEAT_BEVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903/*
2904 * Function to be called for balloon evaluation. Grabs the text under the
2905 * cursor and sends it to the debugger for evaluation. The debugger should
2906 * respond with a showBalloon command when there is a useful result.
2907 */
Bram Moolenaard62bec82005-03-07 22:56:57 +00002908 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909netbeans_beval_cb(
2910 BalloonEval *beval,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002911 int state UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912{
Bram Moolenaard62bec82005-03-07 22:56:57 +00002913 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 char_u *text;
Bram Moolenaard62bec82005-03-07 22:56:57 +00002915 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 int col;
Bram Moolenaard9462e32011-04-11 21:35:11 +02002917 char *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 char_u *p;
2919
2920 /* Don't do anything when 'ballooneval' is off, messages scrolled the
2921 * windows up or we have no connection. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002922 if (!p_beval || msg_scrolled > 0 || !NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 return;
2924
Bram Moolenaard62bec82005-03-07 22:56:57 +00002925 if (get_beval_info(beval, TRUE, &wp, &lnum, &text, &col) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 {
2927 /* Send debugger request. Only when the text is of reasonable
2928 * length. */
2929 if (text != NULL && text[0] != NUL && STRLEN(text) < MAXPATHL)
2930 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02002931 buf = (char *)alloc(MAXPATHL * 2 + 25);
2932 if (buf != NULL)
Bram Moolenaar009b2592004-10-24 19:18:58 +00002933 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02002934 p = nb_quote(text);
2935 if (p != NULL)
2936 {
2937 vim_snprintf(buf, MAXPATHL * 2 + 25,
2938 "0:balloonText=%d \"%s\"\n", r_cmdno, p);
2939 vim_free(p);
2940 }
2941 nbdebug(("EVT: %s", buf));
2942 nb_send(buf, "netbeans_beval_cb");
2943 vim_free(buf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 }
2946 vim_free(text);
2947 }
2948}
2949#endif
2950
2951/*
Bram Moolenaare0874f82016-01-24 20:36:41 +01002952 * Return TRUE when the netbeans connection is active.
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002953 */
2954 int
2955netbeans_active(void)
2956{
2957 return NETBEANS_OPEN;
2958}
2959
Bram Moolenaar67c53842010-05-22 18:28:27 +02002960#if defined(FEAT_GUI) || defined(PROTO)
2961/*
2962 * Register our file descriptor with the gui event handling system.
2963 */
2964 void
2965netbeans_gui_register(void)
2966{
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002967 if (!NB_HAS_GUI || !NETBEANS_OPEN)
Bram Moolenaar67c53842010-05-22 18:28:27 +02002968 return;
2969
Bram Moolenaar173c9852010-09-29 17:27:01 +02002970# ifdef FEAT_GUI_X11
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002971 /* tell notifier we are interested in being called
2972 * when there is input on the editor connection socket
2973 */
2974 if (inputHandler == (XtInputId)NULL)
2975 inputHandler = XtAppAddInput((XtAppContext)app_context, nbsock,
2976 (XtPointer)(XtInputReadMask + XtInputExceptMask),
2977 messageFromNetbeans, NULL);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002978# else
2979# ifdef FEAT_GUI_GTK
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002980 /*
2981 * Tell gdk we are interested in being called when there
2982 * is input on the editor connection socket
2983 */
2984 if (inputHandler == 0)
2985 inputHandler = gdk_input_add((gint)nbsock, (GdkInputCondition)
2986 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
2987 messageFromNetbeans, NULL);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002988# else
2989# ifdef FEAT_GUI_W32
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002990 /*
2991 * Tell Windows we are interested in receiving message when there
2992 * is input on the editor connection socket
2993 */
2994 if (inputHandler == -1)
2995 inputHandler = WSAAsyncSelect(nbsock, s_hwnd, WM_NETBEANS, FD_READ);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002996# endif
2997# endif
2998# endif
Bram Moolenaar67c53842010-05-22 18:28:27 +02002999
3000# ifdef FEAT_BEVAL
3001 bevalServers |= BEVAL_NETBEANS;
3002# endif
3003}
3004#endif
3005
3006/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 * Tell netbeans that the window was opened, ready for commands.
3008 */
3009 void
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003010netbeans_open(char *params, int doabort)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011{
3012 char *cmd = "0:startupDone=0\n";
3013
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003014 if (NETBEANS_OPEN)
3015 {
3016 EMSG(_("E511: netbeans already connected"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003020 if (netbeans_connect(params, doabort) != OK)
Bram Moolenaar67c53842010-05-22 18:28:27 +02003021 return;
3022#ifdef FEAT_GUI
3023 netbeans_gui_register();
Bram Moolenaard62bec82005-03-07 22:56:57 +00003024#endif
3025
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 nbdebug(("EVT: %s", cmd));
3027 nb_send(cmd, "netbeans_startup_done");
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003028
3029 /* update the screen after having added the gutter */
3030 changed_window_setting();
3031 update_screen(CLEAR);
3032 setcursor();
Bram Moolenaar96bcc5e2011-04-01 15:33:59 +02003033 cursor_on();
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003034 out_flush();
3035#ifdef FEAT_GUI
Bram Moolenaard54a6882010-08-09 22:49:00 +02003036 if (gui.in_use)
3037 {
3038 gui_update_cursor(TRUE, FALSE);
3039 gui_mch_flush();
3040 }
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003041#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042}
3043
Bram Moolenaar009b2592004-10-24 19:18:58 +00003044/*
3045 * Tell netbeans that we're exiting. This should be called right
3046 * before calling exit.
3047 */
3048 void
3049netbeans_send_disconnect()
3050{
3051 char buf[128];
3052
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003053 if (NETBEANS_OPEN)
Bram Moolenaar009b2592004-10-24 19:18:58 +00003054 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00003055 sprintf(buf, "0:disconnect=%d\n", r_cmdno);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003056 nbdebug(("EVT: %s", buf));
3057 nb_send(buf, "netbeans_disconnect");
3058 }
3059}
3060
Bram Moolenaar173c9852010-09-29 17:27:01 +02003061#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_W32) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062/*
3063 * Tell netbeans that the window was moved or resized.
3064 */
3065 void
3066netbeans_frame_moved(int new_x, int new_y)
3067{
3068 char buf[128];
3069
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003070 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 return;
3072
3073 sprintf(buf, "0:geometry=%d %d %d %d %d\n",
Bram Moolenaar89d40322006-08-29 15:30:07 +00003074 r_cmdno, (int)Columns, (int)Rows, new_x, new_y);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003075 /*nbdebug(("EVT: %s", buf)); happens too many times during a move */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 nb_send(buf, "netbeans_frame_moved");
3077}
3078#endif
3079
3080/*
Bram Moolenaar009b2592004-10-24 19:18:58 +00003081 * Tell netbeans the user opened or activated a file.
3082 */
3083 void
3084netbeans_file_activated(buf_T *bufp)
3085{
3086 int bufno = nb_getbufno(bufp);
3087 nbbuf_T *bp = nb_get_buf(bufno);
3088 char buffer[2*MAXPATHL];
3089 char_u *q;
3090
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003091 if (!NETBEANS_OPEN || !bufp->b_netbeans_file || dosetvisible)
Bram Moolenaar009b2592004-10-24 19:18:58 +00003092 return;
3093
3094 q = nb_quote(bufp->b_ffname);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003095 if (q == NULL || bp == NULL)
Bram Moolenaar009b2592004-10-24 19:18:58 +00003096 return;
3097
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003098 vim_snprintf(buffer, sizeof(buffer), "%d:fileOpened=%d \"%s\" %s %s\n",
Bram Moolenaar009b2592004-10-24 19:18:58 +00003099 bufno,
3100 bufno,
3101 (char *)q,
3102 "T", /* open in NetBeans */
3103 "F"); /* modified */
3104
3105 vim_free(q);
3106 nbdebug(("EVT: %s", buffer));
3107
3108 nb_send(buffer, "netbeans_file_opened");
3109}
3110
3111/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 * Tell netbeans the user opened a file.
3113 */
3114 void
Bram Moolenaar009b2592004-10-24 19:18:58 +00003115netbeans_file_opened(buf_T *bufp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116{
Bram Moolenaar009b2592004-10-24 19:18:58 +00003117 int bufno = nb_getbufno(bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 char buffer[2*MAXPATHL];
3119 char_u *q;
Bram Moolenaar009b2592004-10-24 19:18:58 +00003120 nbbuf_T *bp = nb_get_buf(nb_getbufno(bufp));
3121 int bnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003123 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 return;
3125
Bram Moolenaar009b2592004-10-24 19:18:58 +00003126 q = nb_quote(bufp->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 if (q == NULL)
3128 return;
Bram Moolenaar009b2592004-10-24 19:18:58 +00003129 if (bp != NULL)
3130 bnum = bufno;
3131 else
3132 bnum = 0;
3133
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003134 vim_snprintf(buffer, sizeof(buffer), "%d:fileOpened=%d \"%s\" %s %s\n",
Bram Moolenaar009b2592004-10-24 19:18:58 +00003135 bnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 0,
3137 (char *)q,
3138 "T", /* open in NetBeans */
3139 "F"); /* modified */
3140
3141 vim_free(q);
3142 nbdebug(("EVT: %s", buffer));
3143
3144 nb_send(buffer, "netbeans_file_opened");
Bram Moolenaar009b2592004-10-24 19:18:58 +00003145 if (p_acd && vim_chdirfile(bufp->b_ffname) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 shorten_fnames(TRUE);
3147}
3148
3149/*
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00003150 * Tell netbeans that a file was deleted or wiped out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 */
3152 void
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00003153netbeans_file_killed(buf_T *bufp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154{
3155 int bufno = nb_getbufno(bufp);
3156 nbbuf_T *nbbuf = nb_get_buf(bufno);
3157 char buffer[2*MAXPATHL];
3158
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003159 if (!NETBEANS_OPEN || bufno == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 return;
3161
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00003162 nbdebug(("netbeans_file_killed:\n"));
3163 nbdebug((" Killing bufno: %d", bufno));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164
Bram Moolenaar89d40322006-08-29 15:30:07 +00003165 sprintf(buffer, "%d:killed=%d\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166
3167 nbdebug(("EVT: %s", buffer));
3168
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00003169 nb_send(buffer, "netbeans_file_killed");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170
3171 if (nbbuf != NULL)
3172 nbbuf->bufp = NULL;
3173}
3174
3175/*
3176 * Get a pointer to the Netbeans buffer for Vim buffer "bufp".
3177 * Return NULL if there is no such buffer or changes are not to be reported.
3178 * Otherwise store the buffer number in "*bufnop".
3179 */
3180 static nbbuf_T *
3181nb_bufp2nbbuf_fire(buf_T *bufp, int *bufnop)
3182{
3183 int bufno;
3184 nbbuf_T *nbbuf;
3185
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003186 if (!NETBEANS_OPEN || !netbeansFireChanges)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 return NULL; /* changes are not reported at all */
3188
3189 bufno = nb_getbufno(bufp);
3190 if (bufno <= 0)
3191 return NULL; /* file is not known to NetBeans */
3192
3193 nbbuf = nb_get_buf(bufno);
3194 if (nbbuf != NULL && !nbbuf->fireChanges)
3195 return NULL; /* changes in this buffer are not reported */
3196
3197 *bufnop = bufno;
3198 return nbbuf;
3199}
3200
3201/*
3202 * Tell netbeans the user inserted some text.
3203 */
3204 void
3205netbeans_inserted(
3206 buf_T *bufp,
3207 linenr_T linenr,
3208 colnr_T col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 char_u *txt,
3210 int newlen)
3211{
3212 char_u *buf;
3213 int bufno;
3214 nbbuf_T *nbbuf;
3215 pos_T pos;
3216 long off;
3217 char_u *p;
3218 char_u *newtxt;
3219
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003220 if (!NETBEANS_OPEN)
3221 return;
3222
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3224 if (nbbuf == NULL)
3225 return;
3226
Bram Moolenaar009b2592004-10-24 19:18:58 +00003227 /* Don't mark as modified for initial read */
3228 if (nbbuf->insertDone)
3229 nbbuf->modified = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230
3231 pos.lnum = linenr;
3232 pos.col = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 off = pos2off(bufp, &pos);
3234
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 /* send the "insert" EVT */
3236 newtxt = alloc(newlen + 1);
Bram Moolenaarb6356332005-07-18 21:40:44 +00003237 vim_strncpy(newtxt, txt, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 p = nb_quote(newtxt);
3239 if (p != NULL)
3240 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00003241 buf = alloc(128 + 2*newlen);
Bram Moolenaar89d40322006-08-29 15:30:07 +00003242 sprintf((char *)buf, "%d:insert=%d %ld \"%s\"\n",
3243 bufno, r_cmdno, off, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 nbdebug(("EVT: %s", buf));
3245 nb_send((char *)buf, "netbeans_inserted");
Bram Moolenaar009b2592004-10-24 19:18:58 +00003246 vim_free(p);
3247 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 vim_free(newtxt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250}
3251
3252/*
3253 * Tell netbeans some bytes have been removed.
3254 */
3255 void
3256netbeans_removed(
3257 buf_T *bufp,
3258 linenr_T linenr,
3259 colnr_T col,
3260 long len)
3261{
3262 char_u buf[128];
3263 int bufno;
3264 nbbuf_T *nbbuf;
3265 pos_T pos;
3266 long off;
3267
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003268 if (!NETBEANS_OPEN)
3269 return;
3270
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3272 if (nbbuf == NULL)
3273 return;
3274
3275 if (len < 0)
3276 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00003277 nbdebug(("Negative len %ld in netbeans_removed()!\n", len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 return;
3279 }
3280
3281 nbbuf->modified = 1;
3282
3283 pos.lnum = linenr;
3284 pos.col = col;
3285
3286 off = pos2off(bufp, &pos);
3287
Bram Moolenaar89d40322006-08-29 15:30:07 +00003288 sprintf((char *)buf, "%d:remove=%d %ld %ld\n", bufno, r_cmdno, off, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 nbdebug(("EVT: %s", buf));
3290 nb_send((char *)buf, "netbeans_removed");
3291}
3292
3293/*
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003294 * Send netbeans an unmodified command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003297netbeans_unmodified(buf_T *bufp UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298{
Bram Moolenaar09092152010-08-08 16:38:42 +02003299 /* This is a no-op, because NetBeans considers a buffer modified
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 * even when all changes have been undone. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301}
3302
3303/*
3304 * Send a button release event back to netbeans. Its up to netbeans
3305 * to decide what to do (if anything) with this event.
3306 */
3307 void
3308netbeans_button_release(int button)
3309{
3310 char buf[128];
3311 int bufno;
3312
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003313 if (!NETBEANS_OPEN)
3314 return;
3315
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 bufno = nb_getbufno(curbuf);
3317
3318 if (bufno >= 0 && curwin != NULL && curwin->w_buffer == curbuf)
3319 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003320 int col = mouse_col - W_WINCOL(curwin)
Bram Moolenaar67c53842010-05-22 18:28:27 +02003321 - ((curwin->w_p_nu || curwin->w_p_rnu) ? 9 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 long off = pos2off(curbuf, &curwin->w_cursor);
3323
3324 /* sync the cursor position */
Bram Moolenaar89d40322006-08-29 15:30:07 +00003325 sprintf(buf, "%d:newDotAndMark=%d %ld %ld\n", bufno, r_cmdno, off, off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 nbdebug(("EVT: %s", buf));
3327 nb_send(buf, "netbeans_button_release[newDotAndMark]");
3328
Bram Moolenaar89d40322006-08-29 15:30:07 +00003329 sprintf(buf, "%d:buttonRelease=%d %d %ld %d\n", bufno, r_cmdno,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 button, (long)curwin->w_cursor.lnum, col);
3331 nbdebug(("EVT: %s", buf));
3332 nb_send(buf, "netbeans_button_release");
3333 }
3334}
3335
3336
3337/*
Bram Moolenaar143c38c2007-05-10 16:41:10 +00003338 * Send a keypress event back to netbeans. This usually simulates some
Bram Moolenaar009b2592004-10-24 19:18:58 +00003339 * kind of function key press. This function operates on a key code.
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003340 * Return TRUE when the key was sent, FALSE when the command has been
3341 * postponed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 */
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003343 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344netbeans_keycommand(int key)
3345{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 char keyName[60];
Bram Moolenaar009b2592004-10-24 19:18:58 +00003347
3348 netbeans_keyname(key, keyName);
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003349 return netbeans_keystring((char_u *)keyName);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003350}
3351
3352
3353/*
Bram Moolenaar143c38c2007-05-10 16:41:10 +00003354 * Send a keypress event back to netbeans. This usually simulates some
Bram Moolenaar009b2592004-10-24 19:18:58 +00003355 * kind of function key press. This function operates on a key string.
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003356 * Return TRUE when the key was sent, FALSE when the command has been
3357 * postponed.
Bram Moolenaar009b2592004-10-24 19:18:58 +00003358 */
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003359 static int
3360netbeans_keystring(char_u *keyName)
Bram Moolenaar009b2592004-10-24 19:18:58 +00003361{
3362 char buf[2*MAXPATHL];
3363 int bufno = nb_getbufno(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 long off;
3365 char_u *q;
3366
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003367 if (!NETBEANS_OPEN)
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003368 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 if (bufno == -1)
3371 {
3372 nbdebug(("got keycommand for non-NetBeans buffer, opening...\n"));
3373 q = curbuf->b_ffname == NULL ? (char_u *)""
3374 : nb_quote(curbuf->b_ffname);
3375 if (q == NULL)
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003376 return TRUE;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003377 vim_snprintf(buf, sizeof(buf), "0:fileOpened=%d \"%s\" %s %s\n", 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 q,
3379 "T", /* open in NetBeans */
3380 "F"); /* modified */
3381 if (curbuf->b_ffname != NULL)
3382 vim_free(q);
3383 nbdebug(("EVT: %s", buf));
3384 nb_send(buf, "netbeans_keycommand");
3385
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003386 postpone_keycommand(keyName);
3387 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 }
3389
3390 /* sync the cursor position */
3391 off = pos2off(curbuf, &curwin->w_cursor);
Bram Moolenaar89d40322006-08-29 15:30:07 +00003392 sprintf(buf, "%d:newDotAndMark=%d %ld %ld\n", bufno, r_cmdno, off, off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 nbdebug(("EVT: %s", buf));
3394 nb_send(buf, "netbeans_keycommand");
3395
3396 /* To work on Win32 you must apply patch to ExtEditor module
3397 * from ExtEdCaret.java.diff - make EVT_newDotAndMark handler
3398 * more synchronous
3399 */
3400
3401 /* now send keyCommand event */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003402 vim_snprintf(buf, sizeof(buf), "%d:keyCommand=%d \"%s\"\n",
Bram Moolenaar89d40322006-08-29 15:30:07 +00003403 bufno, r_cmdno, keyName);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 nbdebug(("EVT: %s", buf));
3405 nb_send(buf, "netbeans_keycommand");
3406
3407 /* New: do both at once and include the lnum/col. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003408 vim_snprintf(buf, sizeof(buf), "%d:keyAtPos=%d \"%s\" %ld %ld/%ld\n",
Bram Moolenaar89d40322006-08-29 15:30:07 +00003409 bufno, r_cmdno, keyName,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 off, (long)curwin->w_cursor.lnum, (long)curwin->w_cursor.col);
3411 nbdebug(("EVT: %s", buf));
3412 nb_send(buf, "netbeans_keycommand");
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003413 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414}
3415
3416
3417/*
3418 * Send a save event to netbeans.
3419 */
3420 void
3421netbeans_save_buffer(buf_T *bufp)
3422{
3423 char_u buf[64];
3424 int bufno;
3425 nbbuf_T *nbbuf;
3426
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003427 if (!NETBEANS_OPEN)
3428 return;
3429
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3431 if (nbbuf == NULL)
3432 return;
3433
3434 nbbuf->modified = 0;
3435
Bram Moolenaar89d40322006-08-29 15:30:07 +00003436 sprintf((char *)buf, "%d:save=%d\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 nbdebug(("EVT: %s", buf));
3438 nb_send((char *)buf, "netbeans_save_buffer");
3439}
3440
3441
3442/*
3443 * Send remove command to netbeans (this command has been turned off).
3444 */
3445 void
3446netbeans_deleted_all_lines(buf_T *bufp)
3447{
3448 char_u buf[64];
3449 int bufno;
3450 nbbuf_T *nbbuf;
3451
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003452 if (!NETBEANS_OPEN)
3453 return;
3454
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3456 if (nbbuf == NULL)
3457 return;
3458
Bram Moolenaar009b2592004-10-24 19:18:58 +00003459 /* Don't mark as modified for initial read */
3460 if (nbbuf->insertDone)
3461 nbbuf->modified = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462
Bram Moolenaar89d40322006-08-29 15:30:07 +00003463 sprintf((char *)buf, "%d:remove=%d 0 -1\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 nbdebug(("EVT(suppressed): %s", buf));
3465/* nb_send(buf, "netbeans_deleted_all_lines"); */
3466}
3467
3468
3469/*
3470 * See if the lines are guarded. The top and bot parameters are from
3471 * u_savecommon(), these are the line above the change and the line below the
3472 * change.
3473 */
3474 int
3475netbeans_is_guarded(linenr_T top, linenr_T bot)
3476{
3477 signlist_T *p;
3478 int lnum;
3479
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003480 if (!NETBEANS_OPEN)
3481 return FALSE;
3482
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 for (p = curbuf->b_signlist; p != NULL; p = p->next)
3484 if (p->id >= GUARDEDOFFSET)
3485 for (lnum = top + 1; lnum < bot; lnum++)
3486 if (lnum == p->lnum)
3487 return TRUE;
3488
3489 return FALSE;
3490}
3491
Bram Moolenaar173c9852010-09-29 17:27:01 +02003492#if defined(FEAT_GUI_X11) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493/*
3494 * We have multiple signs to draw at the same location. Draw the
3495 * multi-sign indicator instead. This is the Motif version.
3496 */
3497 void
3498netbeans_draw_multisign_indicator(int row)
3499{
3500 int i;
3501 int y;
3502 int x;
3503
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003504 if (!NETBEANS_OPEN)
3505 return;
3506
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 x = 0;
3508 y = row * gui.char_height + 2;
3509
3510 for (i = 0; i < gui.char_height - 3; i++)
3511 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y++);
3512
3513 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+0, y);
3514 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3515 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+4, y++);
3516 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+1, y);
3517 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3518 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+3, y++);
3519 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3520}
Bram Moolenaar173c9852010-09-29 17:27:01 +02003521#endif /* FEAT_GUI_X11 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522
Bram Moolenaar64404472010-06-26 06:24:45 +02003523#if defined(FEAT_GUI_GTK) && !defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524/*
3525 * We have multiple signs to draw at the same location. Draw the
3526 * multi-sign indicator instead. This is the GTK/Gnome version.
3527 */
3528 void
3529netbeans_draw_multisign_indicator(int row)
3530{
3531 int i;
3532 int y;
3533 int x;
3534 GdkDrawable *drawable = gui.drawarea->window;
3535
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003536 if (!NETBEANS_OPEN)
3537 return;
3538
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 x = 0;
3540 y = row * gui.char_height + 2;
3541
3542 for (i = 0; i < gui.char_height - 3; i++)
3543 gdk_draw_point(drawable, gui.text_gc, x+2, y++);
3544
3545 gdk_draw_point(drawable, gui.text_gc, x+0, y);
3546 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3547 gdk_draw_point(drawable, gui.text_gc, x+4, y++);
3548 gdk_draw_point(drawable, gui.text_gc, x+1, y);
3549 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3550 gdk_draw_point(drawable, gui.text_gc, x+3, y++);
3551 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3552}
3553#endif /* FEAT_GUI_GTK */
3554
3555/*
3556 * If the mouse is clicked in the gutter of a line with multiple
3557 * annotations, cycle through the set of signs.
3558 */
3559 void
3560netbeans_gutter_click(linenr_T lnum)
3561{
3562 signlist_T *p;
3563
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003564 if (!NETBEANS_OPEN)
3565 return;
3566
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 for (p = curbuf->b_signlist; p != NULL; p = p->next)
3568 {
3569 if (p->lnum == lnum && p->next && p->next->lnum == lnum)
3570 {
3571 signlist_T *tail;
3572
3573 /* remove "p" from list, reinsert it at the tail of the sublist */
3574 if (p->prev)
3575 p->prev->next = p->next;
3576 else
3577 curbuf->b_signlist = p->next;
3578 p->next->prev = p->prev;
3579 /* now find end of sublist and insert p */
3580 for (tail = p->next;
3581 tail->next && tail->next->lnum == lnum
3582 && tail->next->id < GUARDEDOFFSET;
3583 tail = tail->next)
3584 ;
3585 /* tail now points to last entry with same lnum (except
3586 * that "guarded" annotations are always last) */
3587 p->next = tail->next;
3588 if (tail->next)
3589 tail->next->prev = p;
3590 p->prev = tail;
3591 tail->next = p;
3592 update_debug_sign(curbuf, lnum);
3593 break;
3594 }
3595 }
3596}
3597
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598/*
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003599 * Add a sign of the requested type at the requested location.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 *
3601 * Reverse engineering:
3602 * Apparently an annotation is defined the first time it is used in a buffer.
3603 * When the same annotation is used in two buffers, the second time we do not
3604 * need to define a new sign name but reuse the existing one. But since the
3605 * ID number used in the second buffer starts counting at one again, a mapping
3606 * is made from the ID specifically for the buffer to the global sign name
3607 * (which is a number).
3608 *
3609 * globalsignmap[] stores the signs that have been defined globally.
3610 * buf->signmapused[] maps buffer-local annotation IDs to an index in
3611 * globalsignmap[].
3612 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 static void
3614addsigntype(
3615 nbbuf_T *buf,
3616 int typeNum,
3617 char_u *typeName,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003618 char_u *tooltip UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 char_u *glyphFile,
Bram Moolenaar67c53842010-05-22 18:28:27 +02003620 char_u *fg,
3621 char_u *bg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 int i, j;
Bram Moolenaar67c53842010-05-22 18:28:27 +02003624 int use_fg = (*fg && STRCMP(fg, "none") != 0);
3625 int use_bg = (*bg && STRCMP(bg, "none") != 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626
3627 for (i = 0; i < globalsignmapused; i++)
3628 if (STRCMP(typeName, globalsignmap[i]) == 0)
3629 break;
3630
3631 if (i == globalsignmapused) /* not found; add it to global map */
3632 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003633 nbdebug(("DEFINEANNOTYPE(%d,%s,%s,%s,%s,%s)\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 typeNum, typeName, tooltip, glyphFile, fg, bg));
3635 if (use_fg || use_bg)
3636 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003637 char fgbuf[2 * (8 + MAX_COLOR_LENGTH) + 1];
3638 char bgbuf[2 * (8 + MAX_COLOR_LENGTH) + 1];
3639 char *ptr;
3640 int value;
3641
3642 value = strtol((char *)fg, &ptr, 10);
3643 if (ptr != (char *)fg)
3644 sprintf(fgbuf, "guifg=#%06x", value & 0xFFFFFF);
3645 else
3646 sprintf(fgbuf, "guifg=%s ctermfg=%s", fg, fg);
3647
3648 value = strtol((char *)bg, &ptr, 10);
3649 if (ptr != (char *)bg)
3650 sprintf(bgbuf, "guibg=#%06x", value & 0xFFFFFF);
3651 else
3652 sprintf(bgbuf, "guibg=%s ctermbg=%s", bg, bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653
3654 coloncmd(":highlight NB_%s %s %s", typeName, (use_fg) ? fgbuf : "",
3655 (use_bg) ? bgbuf : "");
3656 if (*glyphFile == NUL)
3657 /* no glyph, line highlighting only */
3658 coloncmd(":sign define %d linehl=NB_%s", i + 1, typeName);
3659 else if (vim_strsize(glyphFile) <= 2)
3660 /* one- or two-character glyph name, use as text glyph with
3661 * texthl */
3662 coloncmd(":sign define %d text=%s texthl=NB_%s", i + 1,
3663 glyphFile, typeName);
3664 else
3665 /* glyph, line highlighting */
3666 coloncmd(":sign define %d icon=%s linehl=NB_%s", i + 1,
3667 glyphFile, typeName);
3668 }
3669 else
3670 /* glyph, no line highlighting */
3671 coloncmd(":sign define %d icon=%s", i + 1, glyphFile);
3672
3673 if (STRCMP(typeName,"CurrentPC") == 0)
3674 curPCtype = typeNum;
3675
3676 if (globalsignmapused == globalsignmaplen)
3677 {
3678 if (globalsignmaplen == 0) /* first allocation */
3679 {
3680 globalsignmaplen = 20;
3681 globalsignmap = (char **)alloc_clear(globalsignmaplen*sizeof(char *));
3682 }
3683 else /* grow it */
3684 {
3685 int incr;
3686 int oldlen = globalsignmaplen;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003687 char **t_globalsignmap = globalsignmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688
3689 globalsignmaplen *= 2;
3690 incr = globalsignmaplen - oldlen;
3691 globalsignmap = (char **)vim_realloc(globalsignmap,
3692 globalsignmaplen * sizeof(char *));
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003693 if (globalsignmap == NULL)
3694 {
3695 vim_free(t_globalsignmap);
3696 globalsignmaplen = 0;
3697 return;
3698 }
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003699 vim_memset(globalsignmap + oldlen, 0, incr * sizeof(char *));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 }
3701 }
3702
3703 globalsignmap[i] = (char *)typeName;
3704 globalsignmapused = i + 1;
3705 }
3706
3707 /* check local map; should *not* be found! */
3708 for (j = 0; j < buf->signmapused; j++)
3709 if (buf->signmap[j] == i + 1)
3710 return;
3711
3712 /* add to local map */
3713 if (buf->signmapused == buf->signmaplen)
3714 {
3715 if (buf->signmaplen == 0) /* first allocation */
3716 {
3717 buf->signmaplen = 5;
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003718 buf->signmap = (int *)alloc_clear(buf->signmaplen * sizeof(int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 }
3720 else /* grow it */
3721 {
3722 int incr;
3723 int oldlen = buf->signmaplen;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003724 int *t_signmap = buf->signmap;
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003725
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 buf->signmaplen *= 2;
3727 incr = buf->signmaplen - oldlen;
3728 buf->signmap = (int *)vim_realloc(buf->signmap,
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003729 buf->signmaplen * sizeof(int));
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003730 if (buf->signmap == NULL)
3731 {
3732 vim_free(t_signmap);
3733 buf->signmaplen = 0;
3734 return;
3735 }
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003736 vim_memset(buf->signmap + oldlen, 0, incr * sizeof(int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 }
3738 }
3739
3740 buf->signmap[buf->signmapused++] = i + 1;
3741
3742}
3743
3744
3745/*
3746 * See if we have the requested sign type in the buffer.
3747 */
3748 static int
3749mapsigntype(nbbuf_T *buf, int localsigntype)
3750{
3751 if (--localsigntype >= 0 && localsigntype < buf->signmapused)
3752 return buf->signmap[localsigntype];
3753
3754 return 0;
3755}
3756
3757
3758/*
3759 * Compute length of buffer, don't print anything.
3760 */
3761 static long
3762get_buf_size(buf_T *bufp)
3763{
3764 linenr_T lnum;
3765 long char_count = 0;
3766 int eol_size;
3767 long last_check = 100000L;
3768
3769 if (bufp->b_ml.ml_flags & ML_EMPTY)
3770 return 0;
3771 else
3772 {
3773 if (get_fileformat(bufp) == EOL_DOS)
3774 eol_size = 2;
3775 else
3776 eol_size = 1;
3777 for (lnum = 1; lnum <= bufp->b_ml.ml_line_count; ++lnum)
3778 {
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00003779 char_count += (long)STRLEN(ml_get_buf(bufp, lnum, FALSE))
3780 + eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 /* Check for a CTRL-C every 100000 characters */
3782 if (char_count > last_check)
3783 {
3784 ui_breakcheck();
3785 if (got_int)
3786 return char_count;
3787 last_check = char_count + 100000L;
3788 }
3789 }
3790 /* Correction for when last line doesn't have an EOL. */
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003791 if (!bufp->b_p_eol && (bufp->b_p_bin || !bufp->b_p_fixeol))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 char_count -= eol_size;
3793 }
3794
3795 return char_count;
3796}
3797
3798/*
3799 * Convert character offset to lnum,col
3800 */
3801 static pos_T *
3802off2pos(buf_T *buf, long offset)
3803{
3804 linenr_T lnum;
3805 static pos_T pos;
3806
3807 pos.lnum = 0;
3808 pos.col = 0;
3809#ifdef FEAT_VIRTUALEDIT
3810 pos.coladd = 0;
3811#endif
3812
3813 if (!(buf->b_ml.ml_flags & ML_EMPTY))
3814 {
3815 if ((lnum = ml_find_line_or_offset(buf, (linenr_T)0, &offset)) < 0)
3816 return NULL;
3817 pos.lnum = lnum;
3818 pos.col = offset;
3819 }
3820
3821 return &pos;
3822}
3823
3824/*
3825 * Convert an argument in the form "1234" to an offset and compute the
3826 * lnum/col from it. Convert an argument in the form "123/12" directly to a
3827 * lnum/col.
3828 * "argp" is advanced to after the argument.
3829 * Return a pointer to the position, NULL if something is wrong.
3830 */
3831 static pos_T *
3832get_off_or_lnum(buf_T *buf, char_u **argp)
3833{
3834 static pos_T mypos;
3835 long off;
3836
3837 off = strtol((char *)*argp, (char **)argp, 10);
3838 if (**argp == '/')
3839 {
3840 mypos.lnum = (linenr_T)off;
3841 ++*argp;
3842 mypos.col = strtol((char *)*argp, (char **)argp, 10);
3843#ifdef FEAT_VIRTUALEDIT
3844 mypos.coladd = 0;
3845#endif
3846 return &mypos;
3847 }
3848 return off2pos(buf, off);
3849}
3850
3851
3852/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003853 * Convert (lnum,col) to byte offset in the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 */
3855 static long
3856pos2off(buf_T *buf, pos_T *pos)
3857{
3858 long offset = 0;
3859
3860 if (!(buf->b_ml.ml_flags & ML_EMPTY))
3861 {
3862 if ((offset = ml_find_line_or_offset(buf, pos->lnum, 0)) < 0)
3863 return 0;
3864 offset += pos->col;
3865 }
3866
3867 return offset;
3868}
3869
3870
Bram Moolenaar009b2592004-10-24 19:18:58 +00003871/*
3872 * This message is printed after NetBeans opens a new file. Its
3873 * similar to the message readfile() uses, but since NetBeans
3874 * doesn't normally call readfile, we do our own.
3875 */
3876 static void
3877print_read_msg(buf)
3878 nbbuf_T *buf;
3879{
3880 int lnum = buf->bufp->b_ml.ml_line_count;
Bram Moolenaar914703b2010-05-31 21:59:46 +02003881 off_t nchars = buf->bufp->b_orig_size;
Bram Moolenaar009b2592004-10-24 19:18:58 +00003882 char_u c;
3883
3884 msg_add_fname(buf->bufp, buf->bufp->b_ffname);
3885 c = FALSE;
3886
3887 if (buf->bufp->b_p_ro)
3888 {
3889 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]"));
3890 c = TRUE;
3891 }
3892 if (!buf->bufp->b_start_eol)
3893 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003894 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]")
3895 : _("[Incomplete last line]"));
Bram Moolenaar009b2592004-10-24 19:18:58 +00003896 c = TRUE;
3897 }
3898 msg_add_lines(c, (long)lnum, nchars);
3899
3900 /* Now display it */
3901 vim_free(keep_msg);
3902 keep_msg = NULL;
3903 msg_scrolled_ign = TRUE;
3904 msg_trunc_attr(IObuff, FALSE, 0);
3905 msg_scrolled_ign = FALSE;
3906}
3907
3908
3909/*
Bram Moolenaar67c53842010-05-22 18:28:27 +02003910 * Print a message after NetBeans writes the file. This message should be
3911 * identical to the standard message a non-netbeans user would see when
3912 * writing a file.
Bram Moolenaar009b2592004-10-24 19:18:58 +00003913 */
3914 static void
3915print_save_msg(buf, nchars)
3916 nbbuf_T *buf;
Bram Moolenaar914703b2010-05-31 21:59:46 +02003917 off_t nchars;
Bram Moolenaar009b2592004-10-24 19:18:58 +00003918{
3919 char_u c;
3920 char_u *p;
3921
3922 if (nchars >= 0)
3923 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003924 /* put fname in IObuff with quotes */
3925 msg_add_fname(buf->bufp, buf->bufp->b_ffname);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003926 c = FALSE;
3927
3928 msg_add_lines(c, buf->bufp->b_ml.ml_line_count,
Bram Moolenaar914703b2010-05-31 21:59:46 +02003929 buf->bufp->b_orig_size);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003930
3931 vim_free(keep_msg);
3932 keep_msg = NULL;
3933 msg_scrolled_ign = TRUE;
3934 p = msg_trunc_attr(IObuff, FALSE, 0);
3935 if ((msg_scrolled && !need_wait_return) || !buf->initDone)
3936 {
3937 /* Need to repeat the message after redrawing when:
3938 * - When reading from stdin (the screen will be cleared next).
3939 * - When restart_edit is set (otherwise there will be a delay
3940 * before redrawing).
3941 * - When the screen was scrolled but there is no wait-return
3942 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003943 set_keep_msg(p, 0);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003944 }
3945 msg_scrolled_ign = FALSE;
3946 /* add_to_input_buf((char_u *)"\f", 1); */
3947 }
3948 else
3949 {
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003950 char_u msgbuf[IOSIZE];
Bram Moolenaar009b2592004-10-24 19:18:58 +00003951
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003952 vim_snprintf((char *)msgbuf, IOSIZE,
3953 _("E505: %s is read-only (add ! to override)"), IObuff);
3954 nbdebug((" %s\n", msgbuf));
3955 emsg(msgbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003956 }
3957}
3958
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959#endif /* defined(FEAT_NETBEANS_INTG) */