blob: c0611d598d545193541b42174c3602c5a8b00892 [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.
17 */
18
19#include "vim.h"
20
21#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
22
Bram Moolenaarb26e6322010-05-22 21:34:09 +020023/* TODO: when should this not be defined? */
24#define INET_SOCKETS
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026/* Note: when making changes here also adjust configure.in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#ifdef WIN32
28# ifdef DEBUG
29# include <tchar.h> /* for _T definition for TRACEn macros */
30# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000031/* WinSock API is separated from C API, thus we can't use read(), write(),
32 * errno... */
Bram Moolenaarc39125d2010-05-23 12:06:58 +020033# define SOCK_ERRNO errno = WSAGetLastError()
Bram Moolenaarbd42a0f2009-06-16 14:57:26 +000034# undef ECONNREFUSED
Bram Moolenaar071d4272004-06-13 20:20:40 +000035# define ECONNREFUSED WSAECONNREFUSED
36# ifdef EINTR
37# undef EINTR
38# endif
39# define EINTR WSAEINTR
40# define sock_write(sd, buf, len) send(sd, buf, len, 0)
41# define sock_read(sd, buf, len) recv(sd, buf, len, 0)
42# define sock_close(sd) closesocket(sd)
43# define sleep(t) Sleep(t*1000) /* WinAPI Sleep() accepts milliseconds */
44#else
Bram Moolenaarb26e6322010-05-22 21:34:09 +020045# ifdef INET_SOCKETS
46# include <netdb.h>
47# include <netinet/in.h>
48# else
49# include <sys/un.h>
50# endif
51
Bram Moolenaar071d4272004-06-13 20:20:40 +000052# include <sys/socket.h>
53# ifdef HAVE_LIBGEN_H
54# include <libgen.h>
55# endif
Bram Moolenaarc39125d2010-05-23 12:06:58 +020056# define SOCK_ERRNO
Bram Moolenaar071d4272004-06-13 20:20:40 +000057# define sock_write(sd, buf, len) write(sd, buf, len)
58# define sock_read(sd, buf, len) read(sd, buf, len)
59# define sock_close(sd) close(sd)
60#endif
61
62#include "version.h"
63
Bram Moolenaar071d4272004-06-13 20:20:40 +000064#define GUARDED 10000 /* typenr for "guarded" annotation */
65#define GUARDEDOFFSET 1000000 /* base for "guarded" sign id's */
Bram Moolenaar67c53842010-05-22 18:28:27 +020066#define MAX_COLOR_LENGTH 32 /* max length of color name in defineAnnoType */
Bram Moolenaar071d4272004-06-13 20:20:40 +000067
68/* The first implementation (working only with Netbeans) returned "1.1". The
69 * protocol implemented here also supports A-A-P. */
Bram Moolenaar67c53842010-05-22 18:28:27 +020070static char *ExtEdProtocolVersion = "2.5";
Bram Moolenaar071d4272004-06-13 20:20:40 +000071
72static long pos2off __ARGS((buf_T *, pos_T *));
73static pos_T *off2pos __ARGS((buf_T *, long));
74static pos_T *get_off_or_lnum __ARGS((buf_T *buf, char_u **argp));
75static long get_buf_size __ARGS((buf_T *));
Bram Moolenaar8065d7f2010-01-19 15:13:14 +010076static int netbeans_keystring __ARGS((char_u *keystr));
77static void postpone_keycommand __ARGS((char_u *keystr));
Bram Moolenaar009b2592004-10-24 19:18:58 +000078static void special_keys __ARGS((char_u *args));
Bram Moolenaar071d4272004-06-13 20:20:40 +000079
Bram Moolenaarb26e6322010-05-22 21:34:09 +020080static int netbeans_connect __ARGS((char *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +000081static int getConnInfo __ARGS((char *file, char **host, char **port, char **password));
82
83static void nb_init_graphics __ARGS((void));
84static void coloncmd __ARGS((char *cmd, ...));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000085static void nb_set_curbuf __ARGS((buf_T *buf));
Bram Moolenaar173c9852010-09-29 17:27:01 +020086#ifdef FEAT_GUI_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +000087static void messageFromNetbeans __ARGS((XtPointer, int *, XtInputId *));
88#endif
89#ifdef FEAT_GUI_GTK
90static void messageFromNetbeans __ARGS((gpointer, gint, GdkInputCondition));
91#endif
92static void nb_parse_cmd __ARGS((char_u *));
93static int nb_do_cmd __ARGS((int, char_u *, int, int, char_u *));
94static void nb_send __ARGS((char *buf, char *fun));
Bram Moolenaarb26e6322010-05-22 21:34:09 +020095static void nb_free __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +000096
Bram Moolenaar67c53842010-05-22 18:28:27 +020097/* TRUE when netbeans is running with a GUI. */
98#ifdef FEAT_GUI
99# define NB_HAS_GUI (gui.in_use || gui.starting)
100#endif
101
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000102#ifdef WIN64
103typedef __int64 NBSOCK;
104#else
105typedef int NBSOCK;
106#endif
107
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200108static NBSOCK nbsock = -1; /* socket fd for Netbeans connection */
109#define NETBEANS_OPEN (nbsock != -1)
110
Bram Moolenaar173c9852010-09-29 17:27:01 +0200111#ifdef FEAT_GUI_X11
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200112static XtInputId inputHandler = (XtInputId)NULL; /* Cookie for input */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#endif
114#ifdef FEAT_GUI_GTK
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200115static gint inputHandler = 0; /* Cookie for input */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116#endif
117#ifdef FEAT_GUI_W32
118static int inputHandler = -1; /* simply ret.value of WSAAsyncSelect() */
119extern HWND s_hwnd; /* Gvim's Window handle */
120#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +0000121static int r_cmdno; /* current command number for reply */
Bram Moolenaar009b2592004-10-24 19:18:58 +0000122static int dosetvisible = FALSE;
123
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124/*
125 * Include the debugging code if wanted.
126 */
127#ifdef NBDEBUG
128# include "nbdebug.c"
129#endif
130
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200131static int needupdate = 0;
132static int inAtomic = 0;
133
Bram Moolenaar7ad7d012010-11-16 15:49:02 +0100134/*
135 * Close the socket and remove the input handlers.
136 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137 static void
Bram Moolenaar7ad7d012010-11-16 15:49:02 +0100138nb_close_socket(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139{
Bram Moolenaar173c9852010-09-29 17:27:01 +0200140#ifdef FEAT_GUI_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141 if (inputHandler != (XtInputId)NULL)
142 {
143 XtRemoveInput(inputHandler);
144 inputHandler = (XtInputId)NULL;
145 }
Bram Moolenaar67c53842010-05-22 18:28:27 +0200146#else
147# ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148 if (inputHandler != 0)
149 {
150 gdk_input_remove(inputHandler);
151 inputHandler = 0;
152 }
Bram Moolenaar67c53842010-05-22 18:28:27 +0200153# else
154# ifdef FEAT_GUI_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155 if (inputHandler == 0)
156 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200157 WSAAsyncSelect(nbsock, s_hwnd, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158 inputHandler = -1;
159 }
Bram Moolenaar67c53842010-05-22 18:28:27 +0200160# endif
161# endif
162#endif
163
Bram Moolenaar7ad7d012010-11-16 15:49:02 +0100164 sock_close(nbsock);
165 nbsock = -1;
166}
167
168/*
169 * Close the connection and cleanup.
170 * May be called when nb_close_socket() was called earlier.
171 */
172 static void
173netbeans_close(void)
174{
175 if (NETBEANS_OPEN)
176 {
177 netbeans_send_disconnect();
178 nb_close_socket();
179 }
180
Bram Moolenaar67c53842010-05-22 18:28:27 +0200181#ifdef FEAT_BEVAL
Bram Moolenaard62bec82005-03-07 22:56:57 +0000182 bevalServers &= ~BEVAL_NETBEANS;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200183#endif
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200184
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200185 needupdate = 0;
186 inAtomic = 0;
187 nb_free();
188
189 /* remove all signs and update the screen after gutter removal */
190 coloncmd(":sign unplace *");
191 changed_window_setting();
192 update_screen(CLEAR);
193 setcursor();
194 out_flush();
195#ifdef FEAT_GUI
Bram Moolenaard54a6882010-08-09 22:49:00 +0200196 if (gui.in_use)
197 {
198 gui_update_cursor(TRUE, FALSE);
199 gui_mch_flush();
200 }
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200201#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
204#define NB_DEF_HOST "localhost"
205#define NB_DEF_ADDR "3219"
206#define NB_DEF_PASS "changeme"
207
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200208 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200209netbeans_connect(char *params, int doabort)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210{
211#ifdef INET_SOCKETS
212 struct sockaddr_in server;
213 struct hostent * host;
214# ifdef FEAT_GUI_W32
215 u_short port;
216# else
217 int port;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200218# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219#else
220 struct sockaddr_un server;
221#endif
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200222 int sd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223 char buf[32];
224 char *hostname = NULL;
225 char *address = NULL;
226 char *password = NULL;
227 char *fname;
228 char *arg = NULL;
229
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200230 if (*params == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200232 /* "=fname": Read info from specified file. */
233 if (getConnInfo(params + 1, &hostname, &address, &password)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234 == FAIL)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200235 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236 }
237 else
238 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200239 if (*params == ':')
240 /* ":<host>:<addr>:<password>": get info from argument */
241 arg = params + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 if (arg == NULL && (fname = getenv("__NETBEANS_CONINFO")) != NULL)
243 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200244 /* "": get info from file specified in environment */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 if (getConnInfo(fname, &hostname, &address, &password) == FAIL)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200246 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247 }
248 else
249 {
250 if (arg != NULL)
251 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200252 /* ":<host>:<addr>:<password>": get info from argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 hostname = arg;
254 address = strchr(hostname, ':');
255 if (address != NULL)
256 {
257 *address++ = '\0';
258 password = strchr(address, ':');
259 if (password != NULL)
260 *password++ = '\0';
261 }
262 }
263
264 /* Get the missing values from the environment. */
265 if (hostname == NULL || *hostname == '\0')
266 hostname = getenv("__NETBEANS_HOST");
267 if (address == NULL)
268 address = getenv("__NETBEANS_SOCKET");
269 if (password == NULL)
270 password = getenv("__NETBEANS_VIM_PASSWORD");
271
272 /* Move values to allocated memory. */
273 if (hostname != NULL)
274 hostname = (char *)vim_strsave((char_u *)hostname);
275 if (address != NULL)
276 address = (char *)vim_strsave((char_u *)address);
277 if (password != NULL)
278 password = (char *)vim_strsave((char_u *)password);
279 }
280 }
281
282 /* Use the default when a value is missing. */
283 if (hostname == NULL || *hostname == '\0')
284 {
285 vim_free(hostname);
286 hostname = (char *)vim_strsave((char_u *)NB_DEF_HOST);
287 }
288 if (address == NULL || *address == '\0')
289 {
290 vim_free(address);
291 address = (char *)vim_strsave((char_u *)NB_DEF_ADDR);
292 }
293 if (password == NULL || *password == '\0')
294 {
295 vim_free(password);
296 password = (char *)vim_strsave((char_u *)NB_DEF_PASS);
297 }
298 if (hostname == NULL || address == NULL || password == NULL)
299 goto theend; /* out of memory */
300
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200301#ifdef FEAT_GUI_W32
302 netbeans_init_winsock();
303#endif
304
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305#ifdef INET_SOCKETS
306 port = atoi(address);
307
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000308 if ((sd = (NBSOCK)socket(AF_INET, SOCK_STREAM, 0)) == (NBSOCK)-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000310 nbdebug(("error in socket() in netbeans_connect()\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311 PERROR("socket() in netbeans_connect()");
312 goto theend;
313 }
314
315 /* Get the server internet address and put into addr structure */
316 /* fill in the socket address structure and connect to server */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200317 vim_memset((char *)&server, '\0', sizeof(server));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318 server.sin_family = AF_INET;
319 server.sin_port = htons(port);
320 if ((host = gethostbyname(hostname)) == NULL)
321 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000322 nbdebug(("error in gethostbyname() in netbeans_connect()\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323 PERROR("gethostbyname() in netbeans_connect()");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324 goto theend;
325 }
326 memcpy((char *)&server.sin_addr, host->h_addr, host->h_length);
327#else
328 if ((sd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
329 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000330 nbdebug(("error in socket() in netbeans_connect()\n"));
331 PERROR("socket() in netbeans_connect()");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332 goto theend;
333 }
334
335 server.sun_family = AF_UNIX;
336 strcpy(server.sun_path, address);
337#endif
338 /* Connect to server */
339 if (connect(sd, (struct sockaddr *)&server, sizeof(server)))
340 {
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200341 SOCK_ERRNO;
342 nbdebug(("netbeans_connect: Connect failed with errno %d\n", errno));
343 if (errno == ECONNREFUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344 {
345 sock_close(sd);
346#ifdef INET_SOCKETS
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000347 if ((sd = (NBSOCK)socket(AF_INET, SOCK_STREAM, 0)) == (NBSOCK)-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348 {
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200349 SOCK_ERRNO;
Bram Moolenaarf2330482008-06-24 20:19:36 +0000350 nbdebug(("socket()#2 in netbeans_connect()\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351 PERROR("socket()#2 in netbeans_connect()");
352 goto theend;
353 }
354#else
355 if ((sd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
356 {
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#endif
363 if (connect(sd, (struct sockaddr *)&server, sizeof(server)))
364 {
365 int retries = 36;
366 int success = FALSE;
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200367
368 SOCK_ERRNO;
369 while (retries-- && ((errno == ECONNREFUSED)
370 || (errno == EINTR)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 {
372 nbdebug(("retrying...\n"));
373 sleep(5);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200374 if (!doabort)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200375 {
376 ui_breakcheck();
377 if (got_int)
378 {
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200379 errno = EINTR;
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200380 break;
381 }
382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 if (connect(sd, (struct sockaddr *)&server,
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200384 sizeof(server)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385 {
386 success = TRUE;
387 break;
388 }
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200389 SOCK_ERRNO;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390 }
391 if (!success)
392 {
393 /* Get here when the server can't be found. */
Bram Moolenaarf2330482008-06-24 20:19:36 +0000394 nbdebug(("Cannot connect to Netbeans #2\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 PERROR(_("Cannot connect to Netbeans #2"));
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200396 if (doabort)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200397 getout(1);
398 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 }
400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 }
402 else
403 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000404 nbdebug(("Cannot connect to Netbeans\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405 PERROR(_("Cannot connect to Netbeans"));
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200406 if (doabort)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200407 getout(1);
408 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 }
410 }
411
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200412 nbsock = sd;
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000413 vim_snprintf(buf, sizeof(buf), "AUTH %s\n", password);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414 nb_send(buf, "netbeans_connect");
415
416 sprintf(buf, "0:version=0 \"%s\"\n", ExtEdProtocolVersion);
417 nb_send(buf, "externaleditor_version");
418
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419theend:
420 vim_free(hostname);
421 vim_free(address);
422 vim_free(password);
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200423 return NETBEANS_OPEN ? OK : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424}
425
426/*
427 * Obtain the NetBeans hostname, port address and password from a file.
428 * Return the strings in allocated memory.
429 * Return FAIL if the file could not be read, OK otherwise (no matter what it
430 * contains).
431 */
432 static int
433getConnInfo(char *file, char **host, char **port, char **auth)
434{
435 FILE *fp;
436 char_u buf[BUFSIZ];
437 char_u *lp;
438 char_u *nl;
439#ifdef UNIX
440 struct stat st;
441
442 /*
443 * For Unix only accept the file when it's not accessible by others.
444 * The open will then fail if we don't own the file.
445 */
446 if (mch_stat(file, &st) == 0 && (st.st_mode & 0077) != 0)
447 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000448 nbdebug(("Wrong access mode for NetBeans connection info file: \"%s\"\n",
449 file));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 EMSG2(_("E668: Wrong access mode for NetBeans connection info file: \"%s\""),
451 file);
452 return FAIL;
453 }
454#endif
455
456 fp = mch_fopen(file, "r");
457 if (fp == NULL)
458 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000459 nbdebug(("Cannot open NetBeans connection info file\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460 PERROR("E660: Cannot open NetBeans connection info file");
461 return FAIL;
462 }
463
464 /* Read the file. There should be one of each parameter */
465 while ((lp = (char_u *)fgets((char *)buf, BUFSIZ, fp)) != NULL)
466 {
467 if ((nl = vim_strchr(lp, '\n')) != NULL)
468 *nl = 0; /* strip off the trailing newline */
469
470 if (STRNCMP(lp, "host=", 5) == 0)
471 {
472 vim_free(*host);
473 *host = (char *)vim_strsave(&buf[5]);
474 }
475 else if (STRNCMP(lp, "port=", 5) == 0)
476 {
477 vim_free(*port);
478 *port = (char *)vim_strsave(&buf[5]);
479 }
480 else if (STRNCMP(lp, "auth=", 5) == 0)
481 {
482 vim_free(*auth);
483 *auth = (char *)vim_strsave(&buf[5]);
484 }
485 }
486 fclose(fp);
487
488 return OK;
489}
490
491
492struct keyqueue
493{
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100494 char_u *keystr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 struct keyqueue *next;
496 struct keyqueue *prev;
497};
498
499typedef struct keyqueue keyQ_T;
500
501static keyQ_T keyHead; /* dummy node, header for circular queue */
502
503
504/*
505 * Queue up key commands sent from netbeans.
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100506 * We store the string, because it may depend on the global mod_mask and
507 * :nbkey doesn't have a key number.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508 */
509 static void
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100510postpone_keycommand(char_u *keystr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511{
512 keyQ_T *node;
513
514 node = (keyQ_T *)alloc(sizeof(keyQ_T));
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100515 if (node == NULL)
516 return; /* out of memory, drop the key */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517
518 if (keyHead.next == NULL) /* initialize circular queue */
519 {
520 keyHead.next = &keyHead;
521 keyHead.prev = &keyHead;
522 }
523
524 /* insert node at tail of queue */
525 node->next = &keyHead;
526 node->prev = keyHead.prev;
527 keyHead.prev->next = node;
528 keyHead.prev = node;
529
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100530 node->keystr = vim_strsave(keystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531}
532
533/*
534 * Handle any queued-up NetBeans keycommands to be send.
535 */
536 static void
537handle_key_queue(void)
538{
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100539 int postponed = FALSE;
540
541 while (!postponed && keyHead.next && keyHead.next != &keyHead)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 {
543 /* first, unlink the node */
544 keyQ_T *node = keyHead.next;
545 keyHead.next = node->next;
546 node->next->prev = node->prev;
547
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100548 /* Now, send the keycommand. This may cause it to be postponed again
549 * and change keyHead. */
550 if (node->keystr != NULL)
551 postponed = !netbeans_keystring(node->keystr);
552 vim_free(node->keystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553
554 /* Finally, dispose of the node */
555 vim_free(node);
556 }
557}
558
559
560struct cmdqueue
561{
562 char_u *buffer;
563 struct cmdqueue *next;
564 struct cmdqueue *prev;
565};
566
567typedef struct cmdqueue queue_T;
568
569static queue_T head; /* dummy node, header for circular queue */
570
571
572/*
573 * Put the buffer on the work queue; possibly save it to a file as well.
574 */
575 static void
576save(char_u *buf, int len)
577{
578 queue_T *node;
579
580 node = (queue_T *)alloc(sizeof(queue_T));
581 if (node == NULL)
582 return; /* out of memory */
583 node->buffer = alloc(len + 1);
584 if (node->buffer == NULL)
585 {
586 vim_free(node);
587 return; /* out of memory */
588 }
589 mch_memmove(node->buffer, buf, (size_t)len);
590 node->buffer[len] = NUL;
591
592 if (head.next == NULL) /* initialize circular queue */
593 {
594 head.next = &head;
595 head.prev = &head;
596 }
597
598 /* insert node at tail of queue */
599 node->next = &head;
600 node->prev = head.prev;
601 head.prev->next = node;
602 head.prev = node;
603
604#ifdef NBDEBUG
605 {
606 static int outfd = -2;
607
608 /* possibly write buffer out to a file */
609 if (outfd == -3)
610 return;
611
612 if (outfd == -2)
613 {
614 char *file = getenv("__NETBEANS_SAVE");
615 if (file == NULL)
616 outfd = -3;
617 else
618 outfd = mch_open(file, O_WRONLY|O_CREAT|O_TRUNC, 0666);
619 }
620
621 if (outfd >= 0)
622 write(outfd, buf, len);
623 }
624#endif
625}
626
627
628/*
629 * While there's still a command in the work queue, parse and execute it.
630 */
Bram Moolenaarf2330482008-06-24 20:19:36 +0000631 void
632netbeans_parse_messages(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633{
634 char_u *p;
635 queue_T *node;
Bram Moolenaar863053d2010-12-02 17:09:54 +0100636 int own_node;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637
Bram Moolenaarf2330482008-06-24 20:19:36 +0000638 while (head.next != NULL && head.next != &head)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 {
640 node = head.next;
641
642 /* Locate the first line in the first buffer. */
643 p = vim_strchr(node->buffer, '\n');
644 if (p == NULL)
645 {
646 /* Command isn't complete. If there is no following buffer,
647 * return (wait for more). If there is another buffer following,
648 * prepend the text to that buffer and delete this one. */
649 if (node->next == &head)
650 return;
Bram Moolenaarf2330482008-06-24 20:19:36 +0000651 p = alloc((unsigned)(STRLEN(node->buffer)
652 + STRLEN(node->next->buffer) + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 if (p == NULL)
654 return; /* out of memory */
655 STRCPY(p, node->buffer);
656 STRCAT(p, node->next->buffer);
657 vim_free(node->next->buffer);
658 node->next->buffer = p;
659
660 /* dispose of the node and buffer */
661 head.next = node->next;
662 node->next->prev = node->prev;
663 vim_free(node->buffer);
664 vim_free(node);
665 }
666 else
667 {
668 /* There is a complete command at the start of the buffer.
669 * Terminate it with a NUL. When no more text is following unlink
670 * the buffer. Do this before executing, because new buffers can
671 * be added while busy handling the command. */
672 *p++ = NUL;
673 if (*p == NUL)
674 {
Bram Moolenaar863053d2010-12-02 17:09:54 +0100675 own_node = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 head.next = node->next;
677 node->next->prev = node->prev;
678 }
Bram Moolenaar863053d2010-12-02 17:09:54 +0100679 else
680 own_node = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681
682 /* now, parse and execute the commands */
683 nb_parse_cmd(node->buffer);
684
Bram Moolenaar863053d2010-12-02 17:09:54 +0100685 if (own_node)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 {
687 /* buffer finished, dispose of the node and buffer */
688 vim_free(node->buffer);
689 vim_free(node);
690 }
Bram Moolenaar863053d2010-12-02 17:09:54 +0100691 /* Check that "head" wasn't changed under our fingers, e.g. when a
692 * DETACH command was handled. */
693 else if (head.next == node)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 {
695 /* more follows, move to the start */
Bram Moolenaarf2330482008-06-24 20:19:36 +0000696 STRMOVE(node->buffer, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 }
698 }
699 }
700}
701
702/* Buffer size for reading incoming messages. */
703#define MAXMSGSIZE 4096
704
705/*
Bram Moolenaar67c53842010-05-22 18:28:27 +0200706 * Read a command from netbeans.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 */
Bram Moolenaar173c9852010-09-29 17:27:01 +0200708#ifdef FEAT_GUI_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 static void
Bram Moolenaara9d45512009-05-17 21:25:42 +0000710messageFromNetbeans(XtPointer clientData UNUSED,
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000711 int *unused1 UNUSED,
712 XtInputId *unused2 UNUSED)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200713{
714 netbeans_read();
715}
716#endif
717
718#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000720messageFromNetbeans(gpointer clientData UNUSED,
721 gint unused1 UNUSED,
722 GdkInputCondition unused2 UNUSED)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200723{
724 netbeans_read();
725}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726#endif
Bram Moolenaar67c53842010-05-22 18:28:27 +0200727
Bram Moolenaar7ad7d012010-11-16 15:49:02 +0100728#define DETACH_MSG "DETACH\n"
729
Bram Moolenaar67c53842010-05-22 18:28:27 +0200730 void
731netbeans_read()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732{
733 static char_u *buf = NULL;
Bram Moolenaar0b65f892010-05-15 14:49:02 +0200734 int len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 int readlen = 0;
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100736#ifdef HAVE_SELECT
737 struct timeval tval;
738 fd_set rfds;
739#else
740# ifdef HAVE_POLL
741 struct pollfd fds;
742# endif
743#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200745 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 {
747 nbdebug(("messageFromNetbeans() called without a socket\n"));
748 return;
749 }
750
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 /* Allocate a buffer to read into. */
752 if (buf == NULL)
753 {
754 buf = alloc(MAXMSGSIZE);
755 if (buf == NULL)
756 return; /* out of memory! */
757 }
758
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100759 /* Keep on reading for as long as there is something to read.
760 * Use select() or poll() to avoid blocking on a message that is exactly
761 * MAXMSGSIZE long. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 for (;;)
763 {
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100764#ifdef HAVE_SELECT
765 FD_ZERO(&rfds);
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200766 FD_SET(nbsock, &rfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +0200767 tval.tv_sec = 0;
768 tval.tv_usec = 0;
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200769 if (select(nbsock + 1, &rfds, NULL, NULL, &tval) <= 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200770 break;
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100771#else
772# ifdef HAVE_POLL
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200773 fds.fd = nbsock;
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100774 fds.events = POLLIN;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200775 if (poll(&fds, 1, 0) <= 0)
776 break;
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100777# endif
778#endif
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200779 len = sock_read(nbsock, buf, MAXMSGSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 if (len <= 0)
781 break; /* error or nothing more to read */
782
783 /* Store the read message in the queue. */
784 save(buf, len);
785 readlen += len;
786 if (len < MAXMSGSIZE)
787 break; /* did read everything that's available */
788 }
789
Bram Moolenaar7ad7d012010-11-16 15:49:02 +0100790 /* Reading a socket disconnection (readlen == 0), or a socket error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 if (readlen <= 0)
792 {
Bram Moolenaar7ad7d012010-11-16 15:49:02 +0100793 /* Queue a "DETACH" netbeans message in the command queue in order to
794 * terminate the netbeans session later. Do not end the session here
795 * directly as we may be running in the context of a call to
796 * netbeans_parse_messages():
797 * netbeans_parse_messages
798 * -> autocmd triggered while processing the netbeans cmd
799 * -> ui_breakcheck
800 * -> gui event loop or select loop
801 * -> netbeans_read()
802 */
803 save((char_u *)DETACH_MSG, strlen(DETACH_MSG));
804 nb_close_socket();
805
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 if (len < 0)
Bram Moolenaarf2330482008-06-24 20:19:36 +0000807 {
808 nbdebug(("read from Netbeans socket\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 PERROR(_("read from Netbeans socket"));
Bram Moolenaarf2330482008-06-24 20:19:36 +0000810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 }
812
Bram Moolenaar03531f72010-11-16 15:04:57 +0100813#if defined(NB_HAS_GUI) && defined(FEAT_GUI_GTK)
814 if (NB_HAS_GUI && gtk_main_level() > 0)
Bram Moolenaar7ad7d012010-11-16 15:49:02 +0100815 gtk_main_quit();
Bram Moolenaarf2330482008-06-24 20:19:36 +0000816#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817}
818
819/*
820 * Handle one NUL terminated command.
821 *
822 * format of a command from netbeans:
823 *
824 * 6:setTitle!84 "a.c"
825 *
826 * bufno
827 * colon
828 * cmd
829 * !
830 * cmdno
831 * args
832 *
833 * for function calls, the ! is replaced by a /
834 */
835 static void
836nb_parse_cmd(char_u *cmd)
837{
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000838 char *verb;
839 char *q;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 int bufno;
841 int isfunc = -1;
842
843 if (STRCMP(cmd, "DISCONNECT") == 0)
844 {
845 /* We assume the server knows that we can safely exit! */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 /* Disconnect before exiting, Motif hangs in a Select error
847 * message otherwise. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200848 netbeans_close();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 getout(0);
850 /* NOTREACHED */
851 }
852
853 if (STRCMP(cmd, "DETACH") == 0)
854 {
855 /* The IDE is breaking the connection. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200856 netbeans_close();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 return;
858 }
859
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000860 bufno = strtol((char *)cmd, &verb, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861
862 if (*verb != ':')
863 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000864 nbdebug((" missing colon: %s\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 EMSG2("E627: missing colon: %s", cmd);
866 return;
867 }
868 ++verb; /* skip colon */
869
870 for (q = verb; *q; q++)
871 {
872 if (*q == '!')
873 {
874 *q++ = NUL;
875 isfunc = 0;
876 break;
877 }
878 else if (*q == '/')
879 {
880 *q++ = NUL;
881 isfunc = 1;
882 break;
883 }
884 }
885
886 if (isfunc < 0)
887 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000888 nbdebug((" missing ! or / in: %s\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 EMSG2("E628: missing ! or / in: %s", cmd);
890 return;
891 }
892
Bram Moolenaar89d40322006-08-29 15:30:07 +0000893 r_cmdno = strtol(q, &q, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000895 q = (char *)skipwhite((char_u *)q);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896
Bram Moolenaar89d40322006-08-29 15:30:07 +0000897 if (nb_do_cmd(bufno, (char_u *)verb, isfunc, r_cmdno, (char_u *)q) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 {
Bram Moolenaar009b2592004-10-24 19:18:58 +0000899#ifdef NBDEBUG
900 /*
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100901 * This happens because the ExtEd can send a command or 2 after
Bram Moolenaar009b2592004-10-24 19:18:58 +0000902 * doing a stopDocumentListen command. It doesn't harm anything
903 * so I'm disabling it except for debugging.
904 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 nbdebug(("nb_parse_cmd: Command error for \"%s\"\n", cmd));
906 EMSG("E629: bad return from nb_do_cmd");
Bram Moolenaar009b2592004-10-24 19:18:58 +0000907#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 }
909}
910
911struct nbbuf_struct
912{
913 buf_T *bufp;
914 unsigned int fireChanges:1;
915 unsigned int initDone:1;
Bram Moolenaar009b2592004-10-24 19:18:58 +0000916 unsigned int insertDone:1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 unsigned int modified:1;
Bram Moolenaar009b2592004-10-24 19:18:58 +0000918 int nbbuf_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 char *displayname;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 int *signmap;
921 short_u signmaplen;
922 short_u signmapused;
923};
924
925typedef struct nbbuf_struct nbbuf_T;
926
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200927static nbbuf_T *buf_list = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000928static int buf_list_size = 0; /* size of buf_list */
929static int buf_list_used = 0; /* nr of entries in buf_list actually in use */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200931static char **globalsignmap = NULL;
932static int globalsignmaplen = 0;
933static int globalsignmapused = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934
935static int mapsigntype __ARGS((nbbuf_T *, int localsigntype));
936static void addsigntype __ARGS((nbbuf_T *, int localsigntype, char_u *typeName,
937 char_u *tooltip, char_u *glyphfile,
Bram Moolenaar67c53842010-05-22 18:28:27 +0200938 char_u *fg, char_u *bg));
Bram Moolenaar009b2592004-10-24 19:18:58 +0000939static void print_read_msg __ARGS((nbbuf_T *buf));
Bram Moolenaar914703b2010-05-31 21:59:46 +0200940static void print_save_msg __ARGS((nbbuf_T *buf, off_t nchars));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941
942static int curPCtype = -1;
943
944/*
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200945 * Free netbeans resources.
946 */
947 static void
948nb_free()
949{
950 keyQ_T *key_node = keyHead.next;
951 queue_T *cmd_node = head.next;
952 nbbuf_T buf;
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200953 int i;
954
955 /* free the netbeans buffer list */
956 for (i = 0; i < buf_list_used; i++)
957 {
958 buf = buf_list[i];
959 vim_free(buf.displayname);
960 vim_free(buf.signmap);
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100961 if (buf.bufp != NULL)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200962 {
963 buf.bufp->b_netbeans_file = FALSE;
964 buf.bufp->b_was_netbeans_file = FALSE;
965 }
966 }
967 vim_free(buf_list);
968 buf_list = NULL;
969 buf_list_size = 0;
970 buf_list_used = 0;
971
972 /* free the queued key commands */
973 while(key_node != NULL && key_node != &keyHead)
974 {
975 keyQ_T *next = key_node->next;
976 vim_free(key_node->keystr);
977 vim_free(key_node);
978 if (next == &keyHead)
979 {
980 keyHead.next = &keyHead;
981 keyHead.prev = &keyHead;
982 break;
983 }
984 key_node = next;
985 }
986
987 /* free the queued netbeans commands */
988 while(cmd_node != NULL && cmd_node != &head)
989 {
990 queue_T *next = cmd_node->next;
991 vim_free(cmd_node->buffer);
992 vim_free(cmd_node);
993 if (next == &head)
994 {
995 head.next = &head;
996 head.prev = &head;
997 break;
998 }
999 cmd_node = next;
1000 }
1001}
1002
1003/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 * Get the Netbeans buffer number for the specified buffer.
1005 */
1006 static int
1007nb_getbufno(buf_T *bufp)
1008{
1009 int i;
1010
1011 for (i = 0; i < buf_list_used; i++)
1012 if (buf_list[i].bufp == bufp)
1013 return i;
1014 return -1;
1015}
1016
1017/*
1018 * Is this a NetBeans-owned buffer?
1019 */
1020 int
1021isNetbeansBuffer(buf_T *bufp)
1022{
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001023 return NETBEANS_OPEN && bufp->b_netbeans_file;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024}
1025
1026/*
1027 * NetBeans and Vim have different undo models. In Vim, the file isn't
1028 * changed if changes are undone via the undo command. In NetBeans, once
1029 * a change has been made the file is marked as modified until saved. It
1030 * doesn't matter if the change was undone.
1031 *
1032 * So this function is for the corner case where Vim thinks a buffer is
1033 * unmodified but NetBeans thinks it IS modified.
1034 */
1035 int
1036isNetbeansModified(buf_T *bufp)
1037{
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001038 if (isNetbeansBuffer(bufp))
Bram Moolenaar009b2592004-10-24 19:18:58 +00001039 {
1040 int bufno = nb_getbufno(bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041
Bram Moolenaar009b2592004-10-24 19:18:58 +00001042 if (bufno > 0)
1043 return buf_list[bufno].modified;
1044 else
1045 return FALSE;
1046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 else
1048 return FALSE;
1049}
1050
1051/*
1052 * Given a Netbeans buffer number, return the netbeans buffer.
1053 * Returns NULL for 0 or a negative number. A 0 bufno means a
1054 * non-buffer related command has been sent.
1055 */
1056 static nbbuf_T *
1057nb_get_buf(int bufno)
1058{
1059 /* find or create a buffer with the given number */
1060 int incr;
1061
1062 if (bufno <= 0)
1063 return NULL;
1064
1065 if (!buf_list)
1066 {
1067 /* initialize */
1068 buf_list = (nbbuf_T *)alloc_clear(100 * sizeof(nbbuf_T));
1069 buf_list_size = 100;
1070 }
1071 if (bufno >= buf_list_used) /* new */
1072 {
1073 if (bufno >= buf_list_size) /* grow list */
1074 {
1075 incr = bufno - buf_list_size + 90;
1076 buf_list_size += incr;
1077 buf_list = (nbbuf_T *)vim_realloc(
1078 buf_list, buf_list_size * sizeof(nbbuf_T));
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02001079 vim_memset(buf_list + buf_list_size - incr, 0,
1080 incr * sizeof(nbbuf_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 }
1082
1083 while (buf_list_used <= bufno)
1084 {
1085 /* Default is to fire text changes. */
1086 buf_list[buf_list_used].fireChanges = 1;
1087 ++buf_list_used;
1088 }
1089 }
1090
1091 return buf_list + bufno;
1092}
1093
1094/*
1095 * Return the number of buffers that are modified.
1096 */
1097 static int
1098count_changed_buffers(void)
1099{
1100 buf_T *bufp;
1101 int n;
1102
1103 n = 0;
1104 for (bufp = firstbuf; bufp != NULL; bufp = bufp->b_next)
1105 if (bufp->b_changed)
1106 ++n;
1107 return n;
1108}
1109
1110/*
1111 * End the netbeans session.
1112 */
1113 void
1114netbeans_end(void)
1115{
1116 int i;
1117 static char buf[128];
1118
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001119 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 return;
1121
1122 for (i = 0; i < buf_list_used; i++)
1123 {
1124 if (!buf_list[i].bufp)
1125 continue;
1126 if (netbeansForcedQuit)
1127 {
1128 /* mark as unmodified so NetBeans won't put up dialog on "killed" */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001129 sprintf(buf, "%d:unmodified=%d\n", i, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 nbdebug(("EVT: %s", buf));
1131 nb_send(buf, "netbeans_end");
1132 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001133 sprintf(buf, "%d:killed=%d\n", i, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 nbdebug(("EVT: %s", buf));
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001135 /* nb_send(buf, "netbeans_end"); avoid "write failed" messages */
1136 ignored = sock_write(nbsock, buf, (int)STRLEN(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138}
1139
1140/*
1141 * Send a message to netbeans.
1142 */
1143 static void
1144nb_send(char *buf, char *fun)
1145{
1146 /* Avoid giving pages full of error messages when the other side has
1147 * exited, only mention the first error until the connection works again. */
1148 static int did_error = FALSE;
1149
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001150 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 {
1152 if (!did_error)
Bram Moolenaarf2330482008-06-24 20:19:36 +00001153 {
1154 nbdebug((" %s(): write while not connected\n", fun));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 EMSG2("E630: %s(): write while not connected", fun);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 did_error = TRUE;
1158 }
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001159 else if (sock_write(nbsock, buf, (int)STRLEN(buf)) != (int)STRLEN(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 {
1161 if (!did_error)
Bram Moolenaarf2330482008-06-24 20:19:36 +00001162 {
1163 nbdebug((" %s(): write failed\n", fun));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 EMSG2("E631: %s(): write failed", fun);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 did_error = TRUE;
1167 }
1168 else
1169 did_error = FALSE;
1170}
1171
1172/*
1173 * Some input received from netbeans requires a response. This function
1174 * handles a response with no information (except the command number).
1175 */
1176 static void
1177nb_reply_nil(int cmdno)
1178{
1179 char reply[32];
1180
Bram Moolenaar009b2592004-10-24 19:18:58 +00001181 nbdebug(("REP %d: <none>\n", cmdno));
1182
Bram Moolenaar7ad7d012010-11-16 15:49:02 +01001183 /* Avoid printing an annoying error message. */
1184 if (!NETBEANS_OPEN)
1185 return;
1186
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 sprintf(reply, "%d\n", cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 nb_send(reply, "nb_reply_nil");
1189}
1190
1191
1192/*
1193 * Send a response with text.
1194 * "result" must have been quoted already (using nb_quote()).
1195 */
1196 static void
1197nb_reply_text(int cmdno, char_u *result)
1198{
1199 char_u *reply;
1200
Bram Moolenaar009b2592004-10-24 19:18:58 +00001201 nbdebug(("REP %d: %s\n", cmdno, (char *)result));
1202
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001203 reply = alloc((unsigned)STRLEN(result) + 32);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 sprintf((char *)reply, "%d %s\n", cmdno, (char *)result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 nb_send((char *)reply, "nb_reply_text");
1206
1207 vim_free(reply);
1208}
1209
1210
1211/*
1212 * Send a response with a number result code.
1213 */
1214 static void
1215nb_reply_nr(int cmdno, long result)
1216{
1217 char reply[32];
1218
Bram Moolenaar009b2592004-10-24 19:18:58 +00001219 nbdebug(("REP %d: %ld\n", cmdno, result));
1220
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 sprintf(reply, "%d %ld\n", cmdno, result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 nb_send(reply, "nb_reply_nr");
1223}
1224
1225
1226/*
1227 * Encode newline, ret, backslash, double quote for transmission to NetBeans.
1228 */
1229 static char_u *
1230nb_quote(char_u *txt)
1231{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001232 char_u *buf = alloc((unsigned)(2 * STRLEN(txt) + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 char_u *p = txt;
1234 char_u *q = buf;
1235
1236 if (buf == NULL)
1237 return NULL;
1238 for (; *p; p++)
1239 {
1240 switch (*p)
1241 {
1242 case '\"':
1243 case '\\':
1244 *q++ = '\\'; *q++ = *p; break;
1245 /* case '\t': */
1246 /* *q++ = '\\'; *q++ = 't'; break; */
1247 case '\n':
1248 *q++ = '\\'; *q++ = 'n'; break;
1249 case '\r':
1250 *q++ = '\\'; *q++ = 'r'; break;
1251 default:
1252 *q++ = *p;
1253 break;
1254 }
1255 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01001256 *q = '\0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257
1258 return buf;
1259}
1260
1261
1262/*
1263 * Remove top level double quotes; convert backslashed chars.
1264 * Returns an allocated string (NULL for failure).
1265 * If "endp" is not NULL it is set to the character after the terminating
1266 * quote.
1267 */
1268 static char *
1269nb_unquote(char_u *p, char_u **endp)
1270{
1271 char *result = 0;
1272 char *q;
1273 int done = 0;
1274
1275 /* result is never longer than input */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001276 result = (char *)alloc_clear((unsigned)STRLEN(p) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 if (result == NULL)
1278 return NULL;
1279
1280 if (*p++ != '"')
1281 {
1282 nbdebug(("nb_unquote called with string that doesn't start with a quote!: %s\n",
1283 p));
1284 result[0] = NUL;
1285 return result;
1286 }
1287
1288 for (q = result; !done && *p != NUL;)
1289 {
1290 switch (*p)
1291 {
1292 case '"':
1293 /*
1294 * Unbackslashed dquote marks the end, if first char was dquote.
1295 */
1296 done = 1;
1297 break;
1298
1299 case '\\':
1300 ++p;
1301 switch (*p)
1302 {
1303 case '\\': *q++ = '\\'; break;
1304 case 'n': *q++ = '\n'; break;
1305 case 't': *q++ = '\t'; break;
1306 case 'r': *q++ = '\r'; break;
1307 case '"': *q++ = '"'; break;
1308 case NUL: --p; break;
1309 /* default: skip over illegal chars */
1310 }
1311 ++p;
1312 break;
1313
1314 default:
1315 *q++ = *p++;
1316 }
1317 }
1318
1319 if (endp != NULL)
1320 *endp = p;
1321
1322 return result;
1323}
1324
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001325/*
1326 * Remove from "first" byte to "last" byte (inclusive), at line "lnum" of the
1327 * current buffer. Remove to end of line when "last" is MAXCOL.
1328 */
1329 static void
1330nb_partialremove(linenr_T lnum, colnr_T first, colnr_T last)
1331{
1332 char_u *oldtext, *newtext;
1333 int oldlen;
1334 int lastbyte = last;
1335
1336 oldtext = ml_get(lnum);
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001337 oldlen = (int)STRLEN(oldtext);
Bram Moolenaarb3c70982008-01-18 10:40:55 +00001338 if (first >= (colnr_T)oldlen || oldlen == 0) /* just in case */
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001339 return;
1340 if (lastbyte >= oldlen)
1341 lastbyte = oldlen - 1;
1342 newtext = alloc(oldlen - (int)(lastbyte - first));
1343 if (newtext != NULL)
1344 {
1345 mch_memmove(newtext, oldtext, first);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001346 STRMOVE(newtext + first, oldtext + lastbyte + 1);
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001347 nbdebug((" NEW LINE %d: %s\n", lnum, newtext));
1348 ml_replace(lnum, newtext, FALSE);
1349 }
1350}
1351
1352/*
1353 * Replace the "first" line with the concatenation of the "first" and
1354 * the "other" line. The "other" line is not removed.
1355 */
1356 static void
1357nb_joinlines(linenr_T first, linenr_T other)
1358{
1359 int len_first, len_other;
1360 char_u *p;
1361
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001362 len_first = (int)STRLEN(ml_get(first));
1363 len_other = (int)STRLEN(ml_get(other));
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001364 p = alloc((unsigned)(len_first + len_other + 1));
1365 if (p != NULL)
1366 {
1367 mch_memmove(p, ml_get(first), len_first);
1368 mch_memmove(p + len_first, ml_get(other), len_other + 1);
1369 ml_replace(first, p, FALSE);
1370 }
1371}
1372
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373#define SKIP_STOP 2
1374#define streq(a,b) (strcmp(a,b) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375
1376/*
1377 * Do the actual processing of a single netbeans command or function.
Bram Moolenaar143c38c2007-05-10 16:41:10 +00001378 * The difference between a command and function is that a function
1379 * gets a response (it's required) but a command does not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 * For arguments see comment for nb_parse_cmd().
1381 */
1382 static int
1383nb_do_cmd(
1384 int bufno,
1385 char_u *cmd,
1386 int func,
1387 int cmdno,
1388 char_u *args) /* points to space before arguments or NUL */
1389{
1390 int doupdate = 0;
1391 long off = 0;
1392 nbbuf_T *buf = nb_get_buf(bufno);
1393 static int skip = 0;
1394 int retval = OK;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001395 char *cp; /* for when a char pointer is needed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396
1397 nbdebug(("%s %d: (%d) %s %s\n", (func) ? "FUN" : "CMD", cmdno, bufno, cmd,
1398 STRCMP(cmd, "insert") == 0 ? "<text>" : (char *)args));
1399
1400 if (func)
1401 {
1402/* =====================================================================*/
1403 if (streq((char *)cmd, "getModified"))
1404 {
1405 if (buf == NULL || buf->bufp == NULL)
1406 /* Return the number of buffers that are modified. */
1407 nb_reply_nr(cmdno, (long)count_changed_buffers());
1408 else
1409 /* Return whether the buffer is modified. */
1410 nb_reply_nr(cmdno, (long)(buf->bufp->b_changed
1411 || isNetbeansModified(buf->bufp)));
1412/* =====================================================================*/
1413 }
1414 else if (streq((char *)cmd, "saveAndExit"))
1415 {
1416 /* Note: this will exit Vim if successful. */
1417 coloncmd(":confirm qall");
1418
1419 /* We didn't exit: return the number of changed buffers. */
1420 nb_reply_nr(cmdno, (long)count_changed_buffers());
1421/* =====================================================================*/
1422 }
1423 else if (streq((char *)cmd, "getCursor"))
1424 {
1425 char_u text[200];
1426
1427 /* Note: nb_getbufno() may return -1. This indicates the IDE
1428 * didn't assign a number to the current buffer in response to a
1429 * fileOpened event. */
1430 sprintf((char *)text, "%d %ld %d %ld",
1431 nb_getbufno(curbuf),
1432 (long)curwin->w_cursor.lnum,
1433 (int)curwin->w_cursor.col,
1434 pos2off(curbuf, &curwin->w_cursor));
1435 nb_reply_text(cmdno, text);
1436/* =====================================================================*/
1437 }
Bram Moolenaarc65c4912006-11-14 17:29:46 +00001438 else if (streq((char *)cmd, "getAnno"))
1439 {
1440 long linenum = 0;
1441#ifdef FEAT_SIGNS
1442 if (buf == NULL || buf->bufp == NULL)
1443 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001444 nbdebug((" Invalid buffer identifier in getAnno\n"));
1445 EMSG("E652: Invalid buffer identifier in getAnno");
Bram Moolenaarc65c4912006-11-14 17:29:46 +00001446 retval = FAIL;
1447 }
1448 else
1449 {
1450 int serNum;
1451
1452 cp = (char *)args;
1453 serNum = strtol(cp, &cp, 10);
1454 /* If the sign isn't found linenum will be zero. */
1455 linenum = (long)buf_findsign(buf->bufp, serNum);
1456 }
1457#endif
1458 nb_reply_nr(cmdno, linenum);
1459/* =====================================================================*/
1460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 else if (streq((char *)cmd, "getLength"))
1462 {
1463 long len = 0;
1464
1465 if (buf == NULL || buf->bufp == NULL)
1466 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001467 nbdebug((" invalid buffer identifier in getLength\n"));
1468 EMSG("E632: invalid buffer identifier in getLength");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 retval = FAIL;
1470 }
1471 else
1472 {
1473 len = get_buf_size(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 }
1475 nb_reply_nr(cmdno, len);
1476/* =====================================================================*/
1477 }
1478 else if (streq((char *)cmd, "getText"))
1479 {
1480 long len;
1481 linenr_T nlines;
1482 char_u *text = NULL;
1483 linenr_T lno = 1;
1484 char_u *p;
1485 char_u *line;
1486
1487 if (buf == NULL || buf->bufp == NULL)
1488 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001489 nbdebug((" invalid buffer identifier in getText\n"));
1490 EMSG("E633: invalid buffer identifier in getText");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 retval = FAIL;
1492 }
1493 else
1494 {
1495 len = get_buf_size(buf->bufp);
1496 nlines = buf->bufp->b_ml.ml_line_count;
1497 text = alloc((unsigned)((len > 0)
1498 ? ((len + nlines) * 2) : 4));
1499 if (text == NULL)
1500 {
1501 nbdebug((" nb_do_cmd: getText has null text field\n"));
1502 retval = FAIL;
1503 }
1504 else
1505 {
1506 p = text;
1507 *p++ = '\"';
1508 for (; lno <= nlines ; lno++)
1509 {
1510 line = nb_quote(ml_get_buf(buf->bufp, lno, FALSE));
1511 if (line != NULL)
1512 {
1513 STRCPY(p, line);
1514 p += STRLEN(line);
1515 *p++ = '\\';
1516 *p++ = 'n';
Bram Moolenaar009b2592004-10-24 19:18:58 +00001517 vim_free(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 }
1520 *p++ = '\"';
1521 *p = '\0';
1522 }
1523 }
1524 if (text == NULL)
1525 nb_reply_text(cmdno, (char_u *)"");
1526 else
1527 {
1528 nb_reply_text(cmdno, text);
1529 vim_free(text);
1530 }
1531/* =====================================================================*/
1532 }
1533 else if (streq((char *)cmd, "remove"))
1534 {
1535 long count;
1536 pos_T first, last;
1537 pos_T *pos;
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001538 pos_T *next;
1539 linenr_T del_from_lnum, del_to_lnum; /* lines to be deleted as a whole */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 int oldFire = netbeansFireChanges;
1541 int oldSuppress = netbeansSuppressNoLines;
1542 int wasChanged;
1543
1544 if (skip >= SKIP_STOP)
1545 {
1546 nbdebug((" Skipping %s command\n", (char *) cmd));
1547 nb_reply_nil(cmdno);
1548 return OK;
1549 }
1550
1551 if (buf == NULL || buf->bufp == NULL)
1552 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001553 nbdebug((" invalid buffer identifier in remove\n"));
1554 EMSG("E634: invalid buffer identifier in remove");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 retval = FAIL;
1556 }
1557 else
1558 {
1559 netbeansFireChanges = FALSE;
1560 netbeansSuppressNoLines = TRUE;
1561
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001562 nb_set_curbuf(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 wasChanged = buf->bufp->b_changed;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001564 cp = (char *)args;
1565 off = strtol(cp, &cp, 10);
1566 count = strtol(cp, &cp, 10);
1567 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 /* delete "count" chars, starting at "off" */
1569 pos = off2pos(buf->bufp, off);
1570 if (!pos)
1571 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001572 nbdebug((" !bad position\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 nb_reply_text(cmdno, (char_u *)"!bad position");
1574 netbeansFireChanges = oldFire;
1575 netbeansSuppressNoLines = oldSuppress;
1576 return FAIL;
1577 }
1578 first = *pos;
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001579 nbdebug((" FIRST POS: line %d, col %d\n",
1580 first.lnum, first.col));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 pos = off2pos(buf->bufp, off+count-1);
1582 if (!pos)
1583 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001584 nbdebug((" !bad count\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 nb_reply_text(cmdno, (char_u *)"!bad count");
1586 netbeansFireChanges = oldFire;
1587 netbeansSuppressNoLines = oldSuppress;
1588 return FAIL;
1589 }
1590 last = *pos;
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001591 nbdebug((" LAST POS: line %d, col %d\n",
1592 last.lnum, last.col));
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001593 del_from_lnum = first.lnum;
1594 del_to_lnum = last.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 doupdate = 1;
1596
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001597 /* Get the position of the first byte after the deleted
1598 * section. "next" is NULL when deleting to the end of the
1599 * file. */
1600 next = off2pos(buf->bufp, off + count);
1601
1602 /* Remove part of the first line. */
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001603 if (first.col != 0
1604 || (next != NULL && first.lnum == next->lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 {
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001606 if (first.lnum != last.lnum
1607 || (next != NULL && first.lnum != next->lnum))
1608 {
1609 /* remove to the end of the first line */
1610 nb_partialremove(first.lnum, first.col,
1611 (colnr_T)MAXCOL);
1612 if (first.lnum == last.lnum)
1613 {
1614 /* Partial line to remove includes the end of
1615 * line. Join the line with the next one, have
1616 * the next line deleted below. */
1617 nb_joinlines(first.lnum, next->lnum);
1618 del_to_lnum = next->lnum;
1619 }
1620 }
1621 else
1622 {
1623 /* remove within one line */
1624 nb_partialremove(first.lnum, first.col, last.col);
1625 }
1626 ++del_from_lnum; /* don't delete the first line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 }
1628
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001629 /* Remove part of the last line. */
1630 if (first.lnum != last.lnum && next != NULL
1631 && next->col != 0 && last.lnum == next->lnum)
1632 {
1633 nb_partialremove(last.lnum, 0, last.col);
1634 if (del_from_lnum > first.lnum)
1635 {
1636 /* Join end of last line to start of first line; last
1637 * line is deleted below. */
1638 nb_joinlines(first.lnum, last.lnum);
1639 }
1640 else
1641 /* First line is deleted as a whole, keep the last
1642 * line. */
1643 --del_to_lnum;
1644 }
1645
1646 /* First is partial line; last line to remove includes
1647 * the end of line; join first line to line following last
1648 * line; line following last line is deleted below. */
1649 if (first.lnum != last.lnum && del_from_lnum > first.lnum
1650 && next != NULL && last.lnum != next->lnum)
1651 {
1652 nb_joinlines(first.lnum, next->lnum);
1653 del_to_lnum = next->lnum;
1654 }
1655
1656 /* Delete whole lines if there are any. */
1657 if (del_to_lnum >= del_from_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 {
1659 int i;
1660
1661 /* delete signs from the lines being deleted */
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001662 for (i = del_from_lnum; i <= del_to_lnum; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 {
1664 int id = buf_findsign_id(buf->bufp, (linenr_T)i);
1665 if (id > 0)
1666 {
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001667 nbdebug((" Deleting sign %d on line %d\n",
1668 id, i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 buf_delsign(buf->bufp, id);
1670 }
1671 else
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001672 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 nbdebug((" No sign on line %d\n", i));
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 }
1676
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001677 nbdebug((" Deleting lines %d through %d\n",
1678 del_from_lnum, del_to_lnum));
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001679 curwin->w_cursor.lnum = del_from_lnum;
1680 curwin->w_cursor.col = 0;
1681 del_lines(del_to_lnum - del_from_lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 }
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001683
1684 /* Leave cursor at first deleted byte. */
1685 curwin->w_cursor = first;
1686 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 buf->bufp->b_changed = wasChanged; /* logically unchanged */
1688 netbeansFireChanges = oldFire;
1689 netbeansSuppressNoLines = oldSuppress;
1690
1691 u_blockfree(buf->bufp);
1692 u_clearall(buf->bufp);
1693 }
1694 nb_reply_nil(cmdno);
1695/* =====================================================================*/
1696 }
1697 else if (streq((char *)cmd, "insert"))
1698 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 char_u *to_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700
1701 if (skip >= SKIP_STOP)
1702 {
1703 nbdebug((" Skipping %s command\n", (char *) cmd));
1704 nb_reply_nil(cmdno);
1705 return OK;
1706 }
1707
1708 /* get offset */
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001709 cp = (char *)args;
1710 off = strtol(cp, &cp, 10);
1711 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712
1713 /* get text to be inserted */
1714 args = skipwhite(args);
1715 args = to_free = (char_u *)nb_unquote(args, NULL);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001716 /*
1717 nbdebug((" CHUNK[%d]: %d bytes at offset %d\n",
1718 buf->bufp->b_ml.ml_line_count, STRLEN(args), off));
1719 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720
1721 if (buf == NULL || buf->bufp == NULL)
1722 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001723 nbdebug((" invalid buffer identifier in insert\n"));
1724 EMSG("E635: invalid buffer identifier in insert");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 retval = FAIL;
1726 }
1727 else if (args != NULL)
1728 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001729 int ff_detected = EOL_UNKNOWN;
1730 int buf_was_empty = (buf->bufp->b_ml.ml_flags & ML_EMPTY);
1731 size_t len = 0;
1732 int added = 0;
1733 int oldFire = netbeansFireChanges;
1734 int old_b_changed;
1735 char_u *nl;
1736 linenr_T lnum;
1737 linenr_T lnum_start;
1738 pos_T *pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 netbeansFireChanges = 0;
1741
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001742 /* Jump to the buffer where we insert. After this "curbuf"
1743 * can be used. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001744 nb_set_curbuf(buf->bufp);
Bram Moolenaara3227e22006-03-08 21:32:40 +00001745 old_b_changed = curbuf->b_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001747 /* Convert the specified character offset into a lnum/col
1748 * position. */
Bram Moolenaara3227e22006-03-08 21:32:40 +00001749 pos = off2pos(curbuf, off);
1750 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001752 if (pos->lnum <= 0)
1753 lnum_start = 1;
1754 else
1755 lnum_start = pos->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 }
1757 else
1758 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001759 /* If the given position is not found, assume we want
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 * the end of the file. See setLocAndSize HACK. */
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001761 if (buf_was_empty)
1762 lnum_start = 1; /* above empty line */
1763 else
1764 lnum_start = curbuf->b_ml.ml_line_count + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001765 }
1766
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001767 /* "lnum" is the line where we insert: either append to it or
1768 * insert a new line above it. */
1769 lnum = lnum_start;
1770
1771 /* Loop over the "\n" separated lines of the argument. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 doupdate = 1;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001773 while (*args != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001775 nl = vim_strchr(args, '\n');
1776 if (nl == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001778 /* Incomplete line, probably truncated. Next "insert"
1779 * command should append to this one. */
1780 len = STRLEN(args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001782 else
1783 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001784 len = nl - args;
1785
1786 /*
1787 * We need to detect EOL style, because the commands
1788 * use a character offset.
1789 */
1790 if (nl > args && nl[-1] == '\r')
1791 {
1792 ff_detected = EOL_DOS;
1793 --len;
1794 }
1795 else
1796 ff_detected = EOL_UNIX;
1797 }
1798 args[len] = NUL;
1799
1800 if (lnum == lnum_start
1801 && ((pos != NULL && pos->col > 0)
1802 || (lnum == 1 && buf_was_empty)))
1803 {
1804 char_u *oldline = ml_get(lnum);
1805 char_u *newline;
1806
1807 /* Insert halfway a line. For simplicity we assume we
1808 * need to append to the line. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001809 newline = alloc_check((unsigned)(STRLEN(oldline) + len + 1));
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001810 if (newline != NULL)
1811 {
1812 STRCPY(newline, oldline);
1813 STRCAT(newline, args);
1814 ml_replace(lnum, newline, FALSE);
1815 }
1816 }
1817 else
1818 {
1819 /* Append a new line. Not that we always do this,
1820 * also when the text doesn't end in a "\n". */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001821 ml_append((linenr_T)(lnum - 1), args, (colnr_T)(len + 1), FALSE);
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001822 ++added;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001823 }
1824
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001825 if (nl == NULL)
1826 break;
1827 ++lnum;
1828 args = nl + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001829 }
1830
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001831 /* Adjust the marks below the inserted lines. */
1832 appended_lines_mark(lnum_start - 1, (long)added);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001834 /*
1835 * When starting with an empty buffer set the fileformat.
1836 * This is just guessing...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 */
1838 if (buf_was_empty)
1839 {
1840 if (ff_detected == EOL_UNKNOWN)
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001841#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 ff_detected = EOL_DOS;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001843#else
1844 ff_detected = EOL_UNIX;
1845#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 set_fileformat(ff_detected, OPT_LOCAL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00001847 curbuf->b_start_ffc = *curbuf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 }
1849
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 /*
1851 * XXX - GRP - Is the next line right? If I've inserted
1852 * text the buffer has been updated but not written. Will
1853 * netbeans guarantee to write it? Even if I do a :q! ?
1854 */
Bram Moolenaara3227e22006-03-08 21:32:40 +00001855 curbuf->b_changed = old_b_changed; /* logically unchanged */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 netbeansFireChanges = oldFire;
1857
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001858 /* Undo info is invalid now... */
Bram Moolenaara3227e22006-03-08 21:32:40 +00001859 u_blockfree(curbuf);
1860 u_clearall(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 }
1862 vim_free(to_free);
1863 nb_reply_nil(cmdno); /* or !error */
1864 }
1865 else
1866 {
1867 nbdebug(("UNIMPLEMENTED FUNCTION: %s\n", cmd));
1868 nb_reply_nil(cmdno);
1869 retval = FAIL;
1870 }
1871 }
1872 else /* Not a function; no reply required. */
1873 {
1874/* =====================================================================*/
1875 if (streq((char *)cmd, "create"))
1876 {
1877 /* Create a buffer without a name. */
1878 if (buf == NULL)
1879 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001880 nbdebug((" invalid buffer identifier in create\n"));
1881 EMSG("E636: invalid buffer identifier in create");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 return FAIL;
1883 }
1884 vim_free(buf->displayname);
1885 buf->displayname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886
1887 netbeansReadFile = 0; /* don't try to open disk file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001888 do_ecmd(0, NULL, 0, 0, ECMD_ONE, ECMD_HIDE + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 netbeansReadFile = 1;
1890 buf->bufp = curbuf;
1891 maketitle();
Bram Moolenaar009b2592004-10-24 19:18:58 +00001892 buf->insertDone = FALSE;
Bram Moolenaar67c53842010-05-22 18:28:27 +02001893#if defined(FEAT_MENU) && defined(FEAT_GUI)
Bram Moolenaard54a6882010-08-09 22:49:00 +02001894 if (gui.in_use)
1895 gui_update_menus(0);
Bram Moolenaar67c53842010-05-22 18:28:27 +02001896#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897/* =====================================================================*/
1898 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001899 else if (streq((char *)cmd, "insertDone"))
1900 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001901 if (buf == NULL || buf->bufp == NULL)
1902 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001903 nbdebug((" invalid buffer identifier in insertDone\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001904 }
1905 else
1906 {
1907 buf->bufp->b_start_eol = *args == 'T';
1908 buf->insertDone = TRUE;
1909 args += 2;
1910 buf->bufp->b_p_ro = *args == 'T';
1911 print_read_msg(buf);
1912 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001913/* =====================================================================*/
1914 }
1915 else if (streq((char *)cmd, "saveDone"))
1916 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001917 long savedChars = atol((char *)args);
1918
1919 if (buf == NULL || buf->bufp == NULL)
1920 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001921 nbdebug((" invalid buffer identifier in saveDone\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001922 }
1923 else
1924 print_save_msg(buf, savedChars);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001925/* =====================================================================*/
1926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 else if (streq((char *)cmd, "startDocumentListen"))
1928 {
1929 if (buf == NULL)
1930 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001931 nbdebug((" invalid buffer identifier in startDocumentListen\n"));
1932 EMSG("E637: invalid buffer identifier in startDocumentListen");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 return FAIL;
1934 }
1935 buf->fireChanges = 1;
1936/* =====================================================================*/
1937 }
1938 else if (streq((char *)cmd, "stopDocumentListen"))
1939 {
1940 if (buf == NULL)
1941 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001942 nbdebug((" invalid buffer identifier in stopDocumentListen\n"));
1943 EMSG("E638: invalid buffer identifier in stopDocumentListen");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 return FAIL;
1945 }
1946 buf->fireChanges = 0;
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001947 if (buf->bufp != NULL && buf->bufp->b_was_netbeans_file)
Bram Moolenaar009b2592004-10-24 19:18:58 +00001948 {
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001949 if (!buf->bufp->b_netbeans_file)
Bram Moolenaarf2330482008-06-24 20:19:36 +00001950 {
1951 nbdebug(("E658: NetBeans connection lost for buffer %ld\n", buf->bufp->b_fnum));
Bram Moolenaar009b2592004-10-24 19:18:58 +00001952 EMSGN(_("E658: NetBeans connection lost for buffer %ld"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 buf->bufp->b_fnum);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001954 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001955 else
1956 {
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001957 /* NetBeans uses stopDocumentListen when it stops editing
1958 * a file. It then expects the buffer in Vim to
1959 * disappear. */
1960 do_bufdel(DOBUF_DEL, (char_u *)"", 1,
1961 buf->bufp->b_fnum, buf->bufp->b_fnum, TRUE);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001962 vim_memset(buf, 0, sizeof(nbbuf_T));
1963 }
1964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965/* =====================================================================*/
1966 }
1967 else if (streq((char *)cmd, "setTitle"))
1968 {
1969 if (buf == NULL)
1970 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001971 nbdebug((" invalid buffer identifier in setTitle\n"));
1972 EMSG("E639: invalid buffer identifier in setTitle");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 return FAIL;
1974 }
1975 vim_free(buf->displayname);
1976 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977/* =====================================================================*/
1978 }
1979 else if (streq((char *)cmd, "initDone"))
1980 {
1981 if (buf == NULL || buf->bufp == NULL)
1982 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001983 nbdebug((" invalid buffer identifier in initDone\n"));
1984 EMSG("E640: invalid buffer identifier in initDone");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 return FAIL;
1986 }
1987 doupdate = 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001988 buf->initDone = TRUE;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001989 nb_set_curbuf(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990#if defined(FEAT_AUTOCMD)
1991 apply_autocmds(EVENT_BUFREADPOST, 0, 0, FALSE, buf->bufp);
1992#endif
1993
1994 /* handle any postponed key commands */
1995 handle_key_queue();
1996/* =====================================================================*/
1997 }
1998 else if (streq((char *)cmd, "setBufferNumber")
1999 || streq((char *)cmd, "putBufferNumber"))
2000 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002001 char_u *path;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 buf_T *bufp;
2003
2004 if (buf == NULL)
2005 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002006 nbdebug((" invalid buffer identifier in setBufferNumber\n"));
2007 EMSG("E641: invalid buffer identifier in setBufferNumber");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 return FAIL;
2009 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002010 path = (char_u *)nb_unquote(args, NULL);
2011 if (path == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 return FAIL;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002013 bufp = buflist_findname(path);
2014 vim_free(path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 if (bufp == NULL)
2016 {
Bram Moolenaarec906222009-02-21 21:14:00 +00002017 nbdebug((" File %s not found in setBufferNumber\n", args));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 EMSG2("E642: File %s not found in setBufferNumber", args);
2019 return FAIL;
2020 }
2021 buf->bufp = bufp;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002022 buf->nbbuf_number = bufp->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023
2024 /* "setBufferNumber" has the side effect of jumping to the buffer
2025 * (don't know why!). Don't do that for "putBufferNumber". */
2026 if (*cmd != 'p')
2027 coloncmd(":buffer %d", bufp->b_fnum);
2028 else
2029 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002030 buf->initDone = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031
2032 /* handle any postponed key commands */
2033 handle_key_queue();
2034 }
2035
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036/* =====================================================================*/
2037 }
2038 else if (streq((char *)cmd, "setFullName"))
2039 {
2040 if (buf == NULL)
2041 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002042 nbdebug((" invalid buffer identifier in setFullName\n"));
2043 EMSG("E643: invalid buffer identifier in setFullName");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 return FAIL;
2045 }
2046 vim_free(buf->displayname);
2047 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048
2049 netbeansReadFile = 0; /* don't try to open disk file */
2050 do_ecmd(0, (char_u *)buf->displayname, 0, 0, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002051 ECMD_HIDE + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 netbeansReadFile = 1;
2053 buf->bufp = curbuf;
2054 maketitle();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002055#if defined(FEAT_MENU) && defined(FEAT_GUI)
Bram Moolenaard54a6882010-08-09 22:49:00 +02002056 if (gui.in_use)
2057 gui_update_menus(0);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002058#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059/* =====================================================================*/
2060 }
2061 else if (streq((char *)cmd, "editFile"))
2062 {
2063 if (buf == NULL)
2064 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002065 nbdebug((" invalid buffer identifier in editFile\n"));
2066 EMSG("E644: invalid buffer identifier in editFile");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 return FAIL;
2068 }
2069 /* Edit a file: like create + setFullName + read the file. */
2070 vim_free(buf->displayname);
2071 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 do_ecmd(0, (char_u *)buf->displayname, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002073 ECMD_HIDE + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 buf->bufp = curbuf;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002075 buf->initDone = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 doupdate = 1;
2077#if defined(FEAT_TITLE)
2078 maketitle();
2079#endif
Bram Moolenaar67c53842010-05-22 18:28:27 +02002080#if defined(FEAT_MENU) && defined(FEAT_GUI)
Bram Moolenaard54a6882010-08-09 22:49:00 +02002081 if (gui.in_use)
2082 gui_update_menus(0);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002083#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084/* =====================================================================*/
2085 }
2086 else if (streq((char *)cmd, "setVisible"))
2087 {
2088 if (buf == NULL || buf->bufp == NULL)
2089 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002090 nbdebug((" invalid buffer identifier in setVisible\n"));
2091 /* This message was commented out, probably because it can
2092 * happen when shutting down. */
2093 if (p_verbose > 0)
2094 EMSG("E645: invalid buffer identifier in setVisible");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 return FAIL;
2096 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002097 if (streq((char *)args, "T") && buf->bufp != curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 {
2099 exarg_T exarg;
2100 exarg.cmd = (char_u *)"goto";
2101 exarg.forceit = FALSE;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002102 dosetvisible = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 goto_buffer(&exarg, DOBUF_FIRST, FORWARD, buf->bufp->b_fnum);
2104 doupdate = 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002105 dosetvisible = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106
Bram Moolenaar67c53842010-05-22 18:28:27 +02002107#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 /* Side effect!!!. */
Bram Moolenaard54a6882010-08-09 22:49:00 +02002109 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 gui_mch_set_foreground();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002111#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113/* =====================================================================*/
2114 }
2115 else if (streq((char *)cmd, "raise"))
2116 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002117#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 /* Bring gvim to the foreground. */
Bram Moolenaard54a6882010-08-09 22:49:00 +02002119 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 gui_mch_set_foreground();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002121#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122/* =====================================================================*/
2123 }
2124 else if (streq((char *)cmd, "setModified"))
2125 {
Bram Moolenaarff064e12008-06-09 13:10:45 +00002126 int prev_b_changed;
2127
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 if (buf == NULL || buf->bufp == NULL)
2129 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002130 nbdebug((" invalid buffer identifier in setModified\n"));
2131 /* This message was commented out, probably because it can
2132 * happen when shutting down. */
2133 if (p_verbose > 0)
2134 EMSG("E646: invalid buffer identifier in setModified");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 return FAIL;
2136 }
Bram Moolenaarff064e12008-06-09 13:10:45 +00002137 prev_b_changed = buf->bufp->b_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 if (streq((char *)args, "T"))
Bram Moolenaarff064e12008-06-09 13:10:45 +00002139 buf->bufp->b_changed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 else
2141 {
2142 struct stat st;
2143
2144 /* Assume NetBeans stored the file. Reset the timestamp to
2145 * avoid "file changed" warnings. */
2146 if (buf->bufp->b_ffname != NULL
2147 && mch_stat((char *)buf->bufp->b_ffname, &st) >= 0)
2148 buf_store_time(buf->bufp, &st, buf->bufp->b_ffname);
Bram Moolenaarff064e12008-06-09 13:10:45 +00002149 buf->bufp->b_changed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 }
2151 buf->modified = buf->bufp->b_changed;
Bram Moolenaarff064e12008-06-09 13:10:45 +00002152 if (prev_b_changed != buf->bufp->b_changed)
2153 {
2154#ifdef FEAT_WINDOWS
2155 check_status(buf->bufp);
2156 redraw_tabline = TRUE;
2157#endif
2158#ifdef FEAT_TITLE
2159 maketitle();
2160#endif
2161 update_screen(0);
2162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163/* =====================================================================*/
2164 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002165 else if (streq((char *)cmd, "setModtime"))
2166 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002167 if (buf == NULL || buf->bufp == NULL)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002168 nbdebug((" invalid buffer identifier in setModtime\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002169 else
2170 buf->bufp->b_mtime = atoi((char *)args);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002171/* =====================================================================*/
2172 }
2173 else if (streq((char *)cmd, "setReadOnly"))
2174 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002175 if (buf == NULL || buf->bufp == NULL)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002176 nbdebug((" invalid buffer identifier in setReadOnly\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002177 else if (streq((char *)args, "T"))
Bram Moolenaar009b2592004-10-24 19:18:58 +00002178 buf->bufp->b_p_ro = TRUE;
2179 else
2180 buf->bufp->b_p_ro = FALSE;
2181/* =====================================================================*/
2182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 else if (streq((char *)cmd, "setMark"))
2184 {
2185 /* not yet */
2186/* =====================================================================*/
2187 }
2188 else if (streq((char *)cmd, "showBalloon"))
2189 {
2190#if defined(FEAT_BEVAL)
2191 static char *text = NULL;
2192
2193 /*
2194 * Set up the Balloon Expression Evaluation area.
2195 * Ignore 'ballooneval' here.
2196 * The text pointer must remain valid for a while.
2197 */
2198 if (balloonEval != NULL)
2199 {
2200 vim_free(text);
2201 text = nb_unquote(args, NULL);
2202 if (text != NULL)
2203 gui_mch_post_balloon(balloonEval, (char_u *)text);
2204 }
2205#endif
2206/* =====================================================================*/
2207 }
2208 else if (streq((char *)cmd, "setDot"))
2209 {
2210 pos_T *pos;
2211#ifdef NBDEBUG
2212 char_u *s;
2213#endif
2214
2215 if (buf == NULL || buf->bufp == NULL)
2216 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002217 nbdebug((" invalid buffer identifier in setDot\n"));
2218 EMSG("E647: invalid buffer identifier in setDot");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 return FAIL;
2220 }
2221
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002222 nb_set_curbuf(buf->bufp);
2223
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224#ifdef FEAT_VISUAL
2225 /* Don't want Visual mode now. */
2226 if (VIsual_active)
2227 end_visual_mode();
2228#endif
2229#ifdef NBDEBUG
2230 s = args;
2231#endif
2232 pos = get_off_or_lnum(buf->bufp, &args);
2233 if (pos)
2234 {
2235 curwin->w_cursor = *pos;
2236 check_cursor();
2237#ifdef FEAT_FOLDING
2238 foldOpenCursor();
2239#endif
2240 }
2241 else
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002242 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 nbdebug((" BAD POSITION in setDot: %s\n", s));
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245
2246 /* gui_update_cursor(TRUE, FALSE); */
2247 /* update_curbuf(NOT_VALID); */
2248 update_topline(); /* scroll to show the line */
2249 update_screen(VALID);
2250 setcursor();
2251 out_flush();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002252#ifdef FEAT_GUI
Bram Moolenaard54a6882010-08-09 22:49:00 +02002253 if (gui.in_use)
2254 {
2255 gui_update_cursor(TRUE, FALSE);
2256 gui_mch_flush();
2257 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02002258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 /* Quit a hit-return or more prompt. */
2260 if (State == HITRETURN || State == ASKMORE)
2261 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262#ifdef FEAT_GUI_GTK
Bram Moolenaard54a6882010-08-09 22:49:00 +02002263 if (gui.in_use && gtk_main_level() > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 gtk_main_quit();
2265#endif
2266 }
2267/* =====================================================================*/
2268 }
2269 else if (streq((char *)cmd, "close"))
2270 {
2271#ifdef NBDEBUG
2272 char *name = "<NONE>";
2273#endif
2274
2275 if (buf == NULL)
2276 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002277 nbdebug((" invalid buffer identifier in close\n"));
2278 EMSG("E648: invalid buffer identifier in close");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 return FAIL;
2280 }
2281
2282#ifdef NBDEBUG
2283 if (buf->displayname != NULL)
2284 name = buf->displayname;
2285#endif
Bram Moolenaarf2330482008-06-24 20:19:36 +00002286 if (buf->bufp == NULL)
2287 {
2288 nbdebug((" invalid buffer identifier in close\n"));
2289 /* This message was commented out, probably because it can
2290 * happen when shutting down. */
2291 if (p_verbose > 0)
2292 EMSG("E649: invalid buffer identifier in close");
2293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 nbdebug((" CLOSE %d: %s\n", bufno, name));
Bram Moolenaar67c53842010-05-22 18:28:27 +02002295#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 need_mouse_correct = TRUE;
Bram Moolenaar67c53842010-05-22 18:28:27 +02002297#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 if (buf->bufp != NULL)
2299 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD,
2300 buf->bufp->b_fnum, TRUE);
Bram Moolenaar8d602722006-08-08 19:34:19 +00002301 buf->bufp = NULL;
2302 buf->initDone = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 doupdate = 1;
2304/* =====================================================================*/
2305 }
2306 else if (streq((char *)cmd, "setStyle")) /* obsolete... */
2307 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002308 nbdebug((" setStyle is obsolete!\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309/* =====================================================================*/
2310 }
2311 else if (streq((char *)cmd, "setExitDelay"))
2312 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002313 /* Only used in version 2.1. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314/* =====================================================================*/
2315 }
2316 else if (streq((char *)cmd, "defineAnnoType"))
2317 {
2318#ifdef FEAT_SIGNS
2319 int typeNum;
2320 char_u *typeName;
2321 char_u *tooltip;
2322 char_u *p;
2323 char_u *glyphFile;
Bram Moolenaar67c53842010-05-22 18:28:27 +02002324 int parse_error = FALSE;
2325 char_u *fg;
2326 char_u *bg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327
2328 if (buf == NULL)
2329 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002330 nbdebug((" invalid buffer identifier in defineAnnoType\n"));
2331 EMSG("E650: invalid buffer identifier in defineAnnoType");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 return FAIL;
2333 }
2334
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002335 cp = (char *)args;
2336 typeNum = strtol(cp, &cp, 10);
2337 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 args = skipwhite(args);
2339 typeName = (char_u *)nb_unquote(args, &args);
2340 args = skipwhite(args + 1);
2341 tooltip = (char_u *)nb_unquote(args, &args);
2342 args = skipwhite(args + 1);
2343
2344 p = (char_u *)nb_unquote(args, &args);
2345 glyphFile = vim_strsave_escaped(p, escape_chars);
2346 vim_free(p);
2347
2348 args = skipwhite(args + 1);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002349 p = skiptowhite(args);
2350 if (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002352 *p = NUL;
2353 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02002355 fg = vim_strsave(args);
2356 bg = vim_strsave(p);
2357 if (STRLEN(fg) > MAX_COLOR_LENGTH || STRLEN(bg) > MAX_COLOR_LENGTH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002359 EMSG("E532: highlighting color name too long in defineAnnoType");
2360 vim_free(typeName);
2361 parse_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02002363 else if (typeName != NULL && tooltip != NULL && glyphFile != NULL)
2364 addsigntype(buf, typeNum, typeName, tooltip, glyphFile, fg, bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 else
2366 vim_free(typeName);
2367
2368 /* don't free typeName; it's used directly in addsigntype() */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002369 vim_free(fg);
2370 vim_free(bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 vim_free(tooltip);
2372 vim_free(glyphFile);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002373 if (parse_error)
2374 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375
2376#endif
2377/* =====================================================================*/
2378 }
2379 else if (streq((char *)cmd, "addAnno"))
2380 {
2381#ifdef FEAT_SIGNS
2382 int serNum;
2383 int localTypeNum;
2384 int typeNum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 pos_T *pos;
2386
2387 if (buf == NULL || buf->bufp == NULL)
2388 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002389 nbdebug((" invalid buffer identifier in addAnno\n"));
2390 EMSG("E651: invalid buffer identifier in addAnno");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 return FAIL;
2392 }
2393
2394 doupdate = 1;
2395
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002396 cp = (char *)args;
2397 serNum = strtol(cp, &cp, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398
2399 /* Get the typenr specific for this buffer and convert it to
2400 * the global typenumber, as used for the sign name. */
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002401 localTypeNum = strtol(cp, &cp, 10);
2402 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 typeNum = mapsigntype(buf, localTypeNum);
2404
2405 pos = get_off_or_lnum(buf->bufp, &args);
2406
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002407 cp = (char *)args;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002408 ignored = (int)strtol(cp, &cp, 10);
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002409 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410# ifdef NBDEBUG
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002411 if (ignored != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002413 nbdebug((" partial line annotation -- Not Yet Implemented!\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 }
2415# endif
2416 if (serNum >= GUARDEDOFFSET)
2417 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002418 nbdebug((" too many annotations! ignoring...\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 return FAIL;
2420 }
2421 if (pos)
2422 {
Bram Moolenaarec906222009-02-21 21:14:00 +00002423 coloncmd(":sign place %d line=%ld name=%d buffer=%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 serNum, pos->lnum, typeNum, buf->bufp->b_fnum);
2425 if (typeNum == curPCtype)
2426 coloncmd(":sign jump %d buffer=%d", serNum,
2427 buf->bufp->b_fnum);
2428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429#endif
2430/* =====================================================================*/
2431 }
2432 else if (streq((char *)cmd, "removeAnno"))
2433 {
2434#ifdef FEAT_SIGNS
2435 int serNum;
2436
2437 if (buf == NULL || buf->bufp == NULL)
2438 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002439 nbdebug((" invalid buffer identifier in removeAnno\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 return FAIL;
2441 }
2442 doupdate = 1;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002443 cp = (char *)args;
2444 serNum = strtol(cp, &cp, 10);
2445 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 coloncmd(":sign unplace %d buffer=%d",
2447 serNum, buf->bufp->b_fnum);
2448 redraw_buf_later(buf->bufp, NOT_VALID);
2449#endif
2450/* =====================================================================*/
2451 }
2452 else if (streq((char *)cmd, "moveAnnoToFront"))
2453 {
2454#ifdef FEAT_SIGNS
Bram Moolenaarf2330482008-06-24 20:19:36 +00002455 nbdebug((" moveAnnoToFront: Not Yet Implemented!\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456#endif
2457/* =====================================================================*/
2458 }
2459 else if (streq((char *)cmd, "guard") || streq((char *)cmd, "unguard"))
2460 {
2461 int len;
2462 pos_T first;
2463 pos_T last;
2464 pos_T *pos;
2465 int un = (cmd[0] == 'u');
2466 static int guardId = GUARDEDOFFSET;
2467
2468 if (skip >= SKIP_STOP)
2469 {
2470 nbdebug((" Skipping %s command\n", (char *) cmd));
2471 return OK;
2472 }
2473
2474 nb_init_graphics();
2475
2476 if (buf == NULL || buf->bufp == NULL)
2477 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002478 nbdebug((" invalid buffer identifier in %s command\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 return FAIL;
2480 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002481 nb_set_curbuf(buf->bufp);
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002482 cp = (char *)args;
2483 off = strtol(cp, &cp, 10);
2484 len = strtol(cp, NULL, 10);
2485 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 pos = off2pos(buf->bufp, off);
2487 doupdate = 1;
2488 if (!pos)
2489 nbdebug((" no such start pos in %s, %ld\n", cmd, off));
2490 else
2491 {
2492 first = *pos;
2493 pos = off2pos(buf->bufp, off + len - 1);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002494 if (pos != NULL && pos->col == 0)
2495 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 /*
2497 * In Java Swing the offset is a position between 2
2498 * characters. If col == 0 then we really want the
2499 * previous line as the end.
2500 */
2501 pos = off2pos(buf->bufp, off + len - 2);
2502 }
2503 if (!pos)
2504 nbdebug((" no such end pos in %s, %ld\n",
2505 cmd, off + len - 1));
2506 else
2507 {
2508 long lnum;
2509 last = *pos;
2510 /* set highlight for region */
2511 nbdebug((" %sGUARD %ld,%d to %ld,%d\n", (un) ? "UN" : "",
2512 first.lnum, first.col,
2513 last.lnum, last.col));
2514#ifdef FEAT_SIGNS
2515 for (lnum = first.lnum; lnum <= last.lnum; lnum++)
2516 {
2517 if (un)
2518 {
2519 /* never used */
2520 }
2521 else
2522 {
2523 if (buf_findsigntype_id(buf->bufp, lnum,
2524 GUARDED) == 0)
2525 {
2526 coloncmd(
Bram Moolenaarec906222009-02-21 21:14:00 +00002527 ":sign place %d line=%ld name=%d buffer=%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 guardId++, lnum, GUARDED,
2529 buf->bufp->b_fnum);
2530 }
2531 }
2532 }
2533#endif
2534 redraw_buf_later(buf->bufp, NOT_VALID);
2535 }
2536 }
2537/* =====================================================================*/
2538 }
2539 else if (streq((char *)cmd, "startAtomic"))
2540 {
2541 inAtomic = 1;
2542/* =====================================================================*/
2543 }
2544 else if (streq((char *)cmd, "endAtomic"))
2545 {
2546 inAtomic = 0;
2547 if (needupdate)
2548 {
2549 doupdate = 1;
2550 needupdate = 0;
2551 }
2552/* =====================================================================*/
2553 }
2554 else if (streq((char *)cmd, "save"))
2555 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002556 /*
2557 * NOTE - This command is obsolete wrt NetBeans. Its left in
2558 * only for historical reasons.
2559 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 if (buf == NULL || buf->bufp == NULL)
2561 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002562 nbdebug((" invalid buffer identifier in %s command\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 return FAIL;
2564 }
2565
2566 /* the following is taken from ex_cmds.c (do_wqall function) */
2567 if (bufIsChanged(buf->bufp))
2568 {
2569 /* Only write if the buffer can be written. */
2570 if (p_write
2571 && !buf->bufp->b_p_ro
2572 && buf->bufp->b_ffname != NULL
2573#ifdef FEAT_QUICKFIX
2574 && !bt_dontwrite(buf->bufp)
2575#endif
2576 )
2577 {
2578 buf_write_all(buf->bufp, FALSE);
2579#ifdef FEAT_AUTOCMD
2580 /* an autocommand may have deleted the buffer */
2581 if (!buf_valid(buf->bufp))
2582 buf->bufp = NULL;
2583#endif
2584 }
2585 }
Bram Moolenaarf2330482008-06-24 20:19:36 +00002586 else
2587 {
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002588 nbdebug((" Buffer has no changes!\n"));
Bram Moolenaarf2330482008-06-24 20:19:36 +00002589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590/* =====================================================================*/
2591 }
2592 else if (streq((char *)cmd, "netbeansBuffer"))
2593 {
2594 if (buf == NULL || buf->bufp == NULL)
2595 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002596 nbdebug((" invalid buffer identifier in %s command\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 return FAIL;
2598 }
2599 if (*args == 'T')
2600 {
2601 buf->bufp->b_netbeans_file = TRUE;
2602 buf->bufp->b_was_netbeans_file = TRUE;
2603 }
2604 else
2605 buf->bufp->b_netbeans_file = FALSE;
2606/* =====================================================================*/
2607 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002608 else if (streq((char *)cmd, "specialKeys"))
2609 {
2610 special_keys(args);
2611/* =====================================================================*/
2612 }
2613 else if (streq((char *)cmd, "actionMenuItem"))
2614 {
2615 /* not used yet */
2616/* =====================================================================*/
2617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 else if (streq((char *)cmd, "version"))
2619 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002620 /* not used yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 }
Bram Moolenaarf2330482008-06-24 20:19:36 +00002622 else
2623 {
2624 nbdebug(("Unrecognised command: %s\n", cmd));
2625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 /*
2627 * Unrecognized command is ignored.
2628 */
2629 }
2630 if (inAtomic && doupdate)
2631 {
2632 needupdate = 1;
2633 doupdate = 0;
2634 }
2635
Bram Moolenaar009b2592004-10-24 19:18:58 +00002636 /*
2637 * Is this needed? I moved the netbeans_Xt_connect() later during startup
2638 * and it may no longer be necessary. If its not needed then needupdate
2639 * and doupdate can also be removed.
2640 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 if (buf != NULL && buf->initDone && doupdate)
2642 {
2643 update_screen(NOT_VALID);
2644 setcursor();
2645 out_flush();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002646#ifdef FEAT_GUI
Bram Moolenaard54a6882010-08-09 22:49:00 +02002647 if (gui.in_use)
2648 {
2649 gui_update_cursor(TRUE, FALSE);
2650 gui_mch_flush();
2651 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02002652#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 /* Quit a hit-return or more prompt. */
2654 if (State == HITRETURN || State == ASKMORE)
2655 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656#ifdef FEAT_GUI_GTK
Bram Moolenaard54a6882010-08-09 22:49:00 +02002657 if (gui.in_use && gtk_main_level() > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 gtk_main_quit();
2659#endif
2660 }
2661 }
2662
2663 return retval;
2664}
2665
2666
2667/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002668 * If "buf" is not the current buffer try changing to a window that edits this
2669 * buffer. If there is no such window then close the current buffer and set
2670 * the current buffer as "buf".
2671 */
2672 static void
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00002673nb_set_curbuf(buf_T *buf)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002674{
2675 if (curbuf != buf && buf_jump_open_win(buf) == NULL)
2676 set_curbuf(buf, DOBUF_GOTO);
2677}
2678
2679/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 * Process a vim colon command.
2681 */
2682 static void
2683coloncmd(char *cmd, ...)
2684{
2685 char buf[1024];
2686 va_list ap;
2687
2688 va_start(ap, cmd);
Bram Moolenaar0dc79e82009-06-24 14:50:12 +00002689 vim_vsnprintf(buf, sizeof(buf), cmd, ap, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 va_end(ap);
2691
2692 nbdebug((" COLONCMD %s\n", buf));
2693
2694/* ALT_INPUT_LOCK_ON; */
2695 do_cmdline((char_u *)buf, NULL, NULL, DOCMD_NOWAIT | DOCMD_KEYTYPED);
2696/* ALT_INPUT_LOCK_OFF; */
2697
2698 setcursor(); /* restore the cursor position */
2699 out_flush(); /* make sure output has been written */
2700
Bram Moolenaar67c53842010-05-22 18:28:27 +02002701#ifdef FEAT_GUI
Bram Moolenaard54a6882010-08-09 22:49:00 +02002702 if (gui.in_use)
2703 {
2704 gui_update_cursor(TRUE, FALSE);
2705 gui_mch_flush();
2706 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02002707#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708}
2709
2710
2711/*
Bram Moolenaar009b2592004-10-24 19:18:58 +00002712 * Parse the specialKeys argument and issue the appropriate map commands.
2713 */
2714 static void
2715special_keys(char_u *args)
2716{
2717 char *save_str = nb_unquote(args, NULL);
2718 char *tok = strtok(save_str, " ");
2719 char *sep;
2720 char keybuf[64];
2721 char cmdbuf[256];
2722
2723 while (tok != NULL)
2724 {
2725 int i = 0;
2726
2727 if ((sep = strchr(tok, '-')) != NULL)
2728 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002729 *sep = NUL;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002730 while (*tok)
2731 {
2732 switch (*tok)
2733 {
2734 case 'A':
2735 case 'M':
2736 case 'C':
2737 case 'S':
2738 keybuf[i++] = *tok;
2739 keybuf[i++] = '-';
2740 break;
2741 }
2742 tok++;
2743 }
2744 tok++;
2745 }
2746
2747 strcpy(&keybuf[i], tok);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002748 vim_snprintf(cmdbuf, sizeof(cmdbuf),
2749 "<silent><%s> :nbkey %s<CR>", keybuf, keybuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002750 do_map(0, (char_u *)cmdbuf, NORMAL, FALSE);
2751 tok = strtok(NULL, " ");
2752 }
2753 vim_free(save_str);
2754}
2755
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002756 void
2757ex_nbclose(eap)
2758 exarg_T *eap UNUSED;
2759{
2760 netbeans_close();
2761}
Bram Moolenaar009b2592004-10-24 19:18:58 +00002762
2763 void
2764ex_nbkey(eap)
2765 exarg_T *eap;
2766{
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002767 (void)netbeans_keystring(eap->arg);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002768}
2769
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002770 void
2771ex_nbstart(eap)
2772 exarg_T *eap;
2773{
Bram Moolenaarc2a406b2010-09-30 21:03:26 +02002774#ifdef FEAT_GUI
2775# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar7ad7d012010-11-16 15:49:02 +01002776 && !defined(FEAT_GUI_W32)
Bram Moolenaarc2a406b2010-09-30 21:03:26 +02002777 if (gui.in_use)
2778 {
Bram Moolenaar7ad7d012010-11-16 15:49:02 +01002779 EMSG(_("E838: netbeans is not supported with this GUI"));
2780 return;
Bram Moolenaarc2a406b2010-09-30 21:03:26 +02002781 }
2782# endif
2783#endif
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002784 netbeans_open((char *)eap->arg, FALSE);
2785}
Bram Moolenaar009b2592004-10-24 19:18:58 +00002786
2787/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 * Initialize highlights and signs for use by netbeans (mostly obsolete)
2789 */
2790 static void
2791nb_init_graphics(void)
2792{
2793 static int did_init = FALSE;
2794
2795 if (!did_init)
2796 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002797 coloncmd(":highlight NBGuarded guibg=Cyan guifg=Black"
2798 " ctermbg=LightCyan ctermfg=Black");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 coloncmd(":sign define %d linehl=NBGuarded", GUARDED);
2800
2801 did_init = TRUE;
2802 }
2803}
2804
2805/*
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002806 * Convert key to netbeans name. This uses the global "mod_mask".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 */
2808 static void
2809netbeans_keyname(int key, char *buf)
2810{
2811 char *name = 0;
2812 char namebuf[2];
2813 int ctrl = 0;
2814 int shift = 0;
2815 int alt = 0;
2816
2817 if (mod_mask & MOD_MASK_CTRL)
2818 ctrl = 1;
2819 if (mod_mask & MOD_MASK_SHIFT)
2820 shift = 1;
2821 if (mod_mask & MOD_MASK_ALT)
2822 alt = 1;
2823
2824
2825 switch (key)
2826 {
2827 case K_F1: name = "F1"; break;
2828 case K_S_F1: name = "F1"; shift = 1; break;
2829 case K_F2: name = "F2"; break;
2830 case K_S_F2: name = "F2"; shift = 1; break;
2831 case K_F3: name = "F3"; break;
2832 case K_S_F3: name = "F3"; shift = 1; break;
2833 case K_F4: name = "F4"; break;
2834 case K_S_F4: name = "F4"; shift = 1; break;
2835 case K_F5: name = "F5"; break;
2836 case K_S_F5: name = "F5"; shift = 1; break;
2837 case K_F6: name = "F6"; break;
2838 case K_S_F6: name = "F6"; shift = 1; break;
2839 case K_F7: name = "F7"; break;
2840 case K_S_F7: name = "F7"; shift = 1; break;
2841 case K_F8: name = "F8"; break;
2842 case K_S_F8: name = "F8"; shift = 1; break;
2843 case K_F9: name = "F9"; break;
2844 case K_S_F9: name = "F9"; shift = 1; break;
2845 case K_F10: name = "F10"; break;
2846 case K_S_F10: name = "F10"; shift = 1; break;
2847 case K_F11: name = "F11"; break;
2848 case K_S_F11: name = "F11"; shift = 1; break;
2849 case K_F12: name = "F12"; break;
2850 case K_S_F12: name = "F12"; shift = 1; break;
2851 default:
2852 if (key >= ' ' && key <= '~')
2853 {
2854 /* Allow ASCII characters. */
2855 name = namebuf;
2856 namebuf[0] = key;
2857 namebuf[1] = NUL;
2858 }
2859 else
2860 name = "X";
2861 break;
2862 }
2863
2864 buf[0] = '\0';
2865 if (ctrl)
2866 strcat(buf, "C");
2867 if (shift)
2868 strcat(buf, "S");
2869 if (alt)
2870 strcat(buf, "M"); /* META */
2871 if (ctrl || shift || alt)
2872 strcat(buf, "-");
2873 strcat(buf, name);
2874}
2875
Bram Moolenaar67c53842010-05-22 18:28:27 +02002876#if defined(FEAT_BEVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877/*
2878 * Function to be called for balloon evaluation. Grabs the text under the
2879 * cursor and sends it to the debugger for evaluation. The debugger should
2880 * respond with a showBalloon command when there is a useful result.
2881 */
Bram Moolenaard62bec82005-03-07 22:56:57 +00002882 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883netbeans_beval_cb(
2884 BalloonEval *beval,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002885 int state UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886{
Bram Moolenaard62bec82005-03-07 22:56:57 +00002887 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 char_u *text;
Bram Moolenaard62bec82005-03-07 22:56:57 +00002889 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 int col;
2891 char buf[MAXPATHL * 2 + 25];
2892 char_u *p;
2893
2894 /* Don't do anything when 'ballooneval' is off, messages scrolled the
2895 * windows up or we have no connection. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002896 if (!p_beval || msg_scrolled > 0 || !NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 return;
2898
Bram Moolenaard62bec82005-03-07 22:56:57 +00002899 if (get_beval_info(beval, TRUE, &wp, &lnum, &text, &col) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 {
2901 /* Send debugger request. Only when the text is of reasonable
2902 * length. */
2903 if (text != NULL && text[0] != NUL && STRLEN(text) < MAXPATHL)
2904 {
2905 p = nb_quote(text);
2906 if (p != NULL)
Bram Moolenaar009b2592004-10-24 19:18:58 +00002907 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002908 vim_snprintf(buf, sizeof(buf),
Bram Moolenaar89d40322006-08-29 15:30:07 +00002909 "0:balloonText=%d \"%s\"\n", r_cmdno, p);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002910 vim_free(p);
2911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 nbdebug(("EVT: %s", buf));
2913 nb_send(buf, "netbeans_beval_cb");
2914 }
2915 vim_free(text);
2916 }
2917}
2918#endif
2919
2920/*
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002921 * Return TRUE when the netbeans connection is closed.
2922 */
2923 int
2924netbeans_active(void)
2925{
2926 return NETBEANS_OPEN;
2927}
2928
2929/*
Bram Moolenaar67c53842010-05-22 18:28:27 +02002930 * Return netbeans file descriptor.
2931 */
2932 int
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002933netbeans_filedesc(void)
Bram Moolenaar67c53842010-05-22 18:28:27 +02002934{
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002935 return nbsock;
Bram Moolenaar67c53842010-05-22 18:28:27 +02002936}
2937
2938#if defined(FEAT_GUI) || defined(PROTO)
2939/*
2940 * Register our file descriptor with the gui event handling system.
2941 */
2942 void
2943netbeans_gui_register(void)
2944{
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002945 if (!NB_HAS_GUI || !NETBEANS_OPEN)
Bram Moolenaar67c53842010-05-22 18:28:27 +02002946 return;
2947
Bram Moolenaar173c9852010-09-29 17:27:01 +02002948# ifdef FEAT_GUI_X11
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002949 /* tell notifier we are interested in being called
2950 * when there is input on the editor connection socket
2951 */
2952 if (inputHandler == (XtInputId)NULL)
2953 inputHandler = XtAppAddInput((XtAppContext)app_context, nbsock,
2954 (XtPointer)(XtInputReadMask + XtInputExceptMask),
2955 messageFromNetbeans, NULL);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002956# else
2957# ifdef FEAT_GUI_GTK
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002958 /*
2959 * Tell gdk we are interested in being called when there
2960 * is input on the editor connection socket
2961 */
2962 if (inputHandler == 0)
2963 inputHandler = gdk_input_add((gint)nbsock, (GdkInputCondition)
2964 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
2965 messageFromNetbeans, NULL);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002966# else
2967# ifdef FEAT_GUI_W32
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002968 /*
2969 * Tell Windows we are interested in receiving message when there
2970 * is input on the editor connection socket
2971 */
2972 if (inputHandler == -1)
2973 inputHandler = WSAAsyncSelect(nbsock, s_hwnd, WM_NETBEANS, FD_READ);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002974# endif
2975# endif
2976# endif
Bram Moolenaar67c53842010-05-22 18:28:27 +02002977
2978# ifdef FEAT_BEVAL
2979 bevalServers |= BEVAL_NETBEANS;
2980# endif
2981}
2982#endif
2983
2984/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 * Tell netbeans that the window was opened, ready for commands.
2986 */
2987 void
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02002988netbeans_open(char *params, int doabort)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989{
2990 char *cmd = "0:startupDone=0\n";
2991
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002992 if (NETBEANS_OPEN)
2993 {
2994 EMSG(_("E511: netbeans already connected"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02002998 if (netbeans_connect(params, doabort) != OK)
Bram Moolenaar67c53842010-05-22 18:28:27 +02002999 return;
3000#ifdef FEAT_GUI
3001 netbeans_gui_register();
Bram Moolenaard62bec82005-03-07 22:56:57 +00003002#endif
3003
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 nbdebug(("EVT: %s", cmd));
3005 nb_send(cmd, "netbeans_startup_done");
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003006
3007 /* update the screen after having added the gutter */
3008 changed_window_setting();
3009 update_screen(CLEAR);
3010 setcursor();
3011 out_flush();
3012#ifdef FEAT_GUI
Bram Moolenaard54a6882010-08-09 22:49:00 +02003013 if (gui.in_use)
3014 {
3015 gui_update_cursor(TRUE, FALSE);
3016 gui_mch_flush();
3017 }
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003018#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019}
3020
Bram Moolenaar009b2592004-10-24 19:18:58 +00003021/*
3022 * Tell netbeans that we're exiting. This should be called right
3023 * before calling exit.
3024 */
3025 void
3026netbeans_send_disconnect()
3027{
3028 char buf[128];
3029
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003030 if (NETBEANS_OPEN)
Bram Moolenaar009b2592004-10-24 19:18:58 +00003031 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00003032 sprintf(buf, "0:disconnect=%d\n", r_cmdno);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003033 nbdebug(("EVT: %s", buf));
3034 nb_send(buf, "netbeans_disconnect");
3035 }
3036}
3037
Bram Moolenaar173c9852010-09-29 17:27:01 +02003038#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_W32) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039/*
3040 * Tell netbeans that the window was moved or resized.
3041 */
3042 void
3043netbeans_frame_moved(int new_x, int new_y)
3044{
3045 char buf[128];
3046
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003047 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 return;
3049
3050 sprintf(buf, "0:geometry=%d %d %d %d %d\n",
Bram Moolenaar89d40322006-08-29 15:30:07 +00003051 r_cmdno, (int)Columns, (int)Rows, new_x, new_y);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003052 /*nbdebug(("EVT: %s", buf)); happens too many times during a move */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 nb_send(buf, "netbeans_frame_moved");
3054}
3055#endif
3056
3057/*
Bram Moolenaar009b2592004-10-24 19:18:58 +00003058 * Tell netbeans the user opened or activated a file.
3059 */
3060 void
3061netbeans_file_activated(buf_T *bufp)
3062{
3063 int bufno = nb_getbufno(bufp);
3064 nbbuf_T *bp = nb_get_buf(bufno);
3065 char buffer[2*MAXPATHL];
3066 char_u *q;
3067
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003068 if (!NETBEANS_OPEN || !bufp->b_netbeans_file || dosetvisible)
Bram Moolenaar009b2592004-10-24 19:18:58 +00003069 return;
3070
3071 q = nb_quote(bufp->b_ffname);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003072 if (q == NULL || bp == NULL)
Bram Moolenaar009b2592004-10-24 19:18:58 +00003073 return;
3074
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003075 vim_snprintf(buffer, sizeof(buffer), "%d:fileOpened=%d \"%s\" %s %s\n",
Bram Moolenaar009b2592004-10-24 19:18:58 +00003076 bufno,
3077 bufno,
3078 (char *)q,
3079 "T", /* open in NetBeans */
3080 "F"); /* modified */
3081
3082 vim_free(q);
3083 nbdebug(("EVT: %s", buffer));
3084
3085 nb_send(buffer, "netbeans_file_opened");
3086}
3087
3088/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 * Tell netbeans the user opened a file.
3090 */
3091 void
Bram Moolenaar009b2592004-10-24 19:18:58 +00003092netbeans_file_opened(buf_T *bufp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093{
Bram Moolenaar009b2592004-10-24 19:18:58 +00003094 int bufno = nb_getbufno(bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 char buffer[2*MAXPATHL];
3096 char_u *q;
Bram Moolenaar009b2592004-10-24 19:18:58 +00003097 nbbuf_T *bp = nb_get_buf(nb_getbufno(bufp));
3098 int bnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003100 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 return;
3102
Bram Moolenaar009b2592004-10-24 19:18:58 +00003103 q = nb_quote(bufp->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 if (q == NULL)
3105 return;
Bram Moolenaar009b2592004-10-24 19:18:58 +00003106 if (bp != NULL)
3107 bnum = bufno;
3108 else
3109 bnum = 0;
3110
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003111 vim_snprintf(buffer, sizeof(buffer), "%d:fileOpened=%d \"%s\" %s %s\n",
Bram Moolenaar009b2592004-10-24 19:18:58 +00003112 bnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 0,
3114 (char *)q,
3115 "T", /* open in NetBeans */
3116 "F"); /* modified */
3117
3118 vim_free(q);
3119 nbdebug(("EVT: %s", buffer));
3120
3121 nb_send(buffer, "netbeans_file_opened");
Bram Moolenaar009b2592004-10-24 19:18:58 +00003122 if (p_acd && vim_chdirfile(bufp->b_ffname) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 shorten_fnames(TRUE);
3124}
3125
3126/*
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00003127 * Tell netbeans that a file was deleted or wiped out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 */
3129 void
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00003130netbeans_file_killed(buf_T *bufp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131{
3132 int bufno = nb_getbufno(bufp);
3133 nbbuf_T *nbbuf = nb_get_buf(bufno);
3134 char buffer[2*MAXPATHL];
3135
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003136 if (!NETBEANS_OPEN || bufno == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 return;
3138
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00003139 nbdebug(("netbeans_file_killed:\n"));
3140 nbdebug((" Killing bufno: %d", bufno));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141
Bram Moolenaar89d40322006-08-29 15:30:07 +00003142 sprintf(buffer, "%d:killed=%d\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143
3144 nbdebug(("EVT: %s", buffer));
3145
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00003146 nb_send(buffer, "netbeans_file_killed");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147
3148 if (nbbuf != NULL)
3149 nbbuf->bufp = NULL;
3150}
3151
3152/*
3153 * Get a pointer to the Netbeans buffer for Vim buffer "bufp".
3154 * Return NULL if there is no such buffer or changes are not to be reported.
3155 * Otherwise store the buffer number in "*bufnop".
3156 */
3157 static nbbuf_T *
3158nb_bufp2nbbuf_fire(buf_T *bufp, int *bufnop)
3159{
3160 int bufno;
3161 nbbuf_T *nbbuf;
3162
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003163 if (!NETBEANS_OPEN || !netbeansFireChanges)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 return NULL; /* changes are not reported at all */
3165
3166 bufno = nb_getbufno(bufp);
3167 if (bufno <= 0)
3168 return NULL; /* file is not known to NetBeans */
3169
3170 nbbuf = nb_get_buf(bufno);
3171 if (nbbuf != NULL && !nbbuf->fireChanges)
3172 return NULL; /* changes in this buffer are not reported */
3173
3174 *bufnop = bufno;
3175 return nbbuf;
3176}
3177
3178/*
3179 * Tell netbeans the user inserted some text.
3180 */
3181 void
3182netbeans_inserted(
3183 buf_T *bufp,
3184 linenr_T linenr,
3185 colnr_T col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 char_u *txt,
3187 int newlen)
3188{
3189 char_u *buf;
3190 int bufno;
3191 nbbuf_T *nbbuf;
3192 pos_T pos;
3193 long off;
3194 char_u *p;
3195 char_u *newtxt;
3196
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003197 if (!NETBEANS_OPEN)
3198 return;
3199
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3201 if (nbbuf == NULL)
3202 return;
3203
Bram Moolenaar009b2592004-10-24 19:18:58 +00003204 /* Don't mark as modified for initial read */
3205 if (nbbuf->insertDone)
3206 nbbuf->modified = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207
3208 pos.lnum = linenr;
3209 pos.col = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 off = pos2off(bufp, &pos);
3211
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 /* send the "insert" EVT */
3213 newtxt = alloc(newlen + 1);
Bram Moolenaarb6356332005-07-18 21:40:44 +00003214 vim_strncpy(newtxt, txt, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 p = nb_quote(newtxt);
3216 if (p != NULL)
3217 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00003218 buf = alloc(128 + 2*newlen);
Bram Moolenaar89d40322006-08-29 15:30:07 +00003219 sprintf((char *)buf, "%d:insert=%d %ld \"%s\"\n",
3220 bufno, r_cmdno, off, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 nbdebug(("EVT: %s", buf));
3222 nb_send((char *)buf, "netbeans_inserted");
Bram Moolenaar009b2592004-10-24 19:18:58 +00003223 vim_free(p);
3224 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 vim_free(newtxt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227}
3228
3229/*
3230 * Tell netbeans some bytes have been removed.
3231 */
3232 void
3233netbeans_removed(
3234 buf_T *bufp,
3235 linenr_T linenr,
3236 colnr_T col,
3237 long len)
3238{
3239 char_u buf[128];
3240 int bufno;
3241 nbbuf_T *nbbuf;
3242 pos_T pos;
3243 long off;
3244
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003245 if (!NETBEANS_OPEN)
3246 return;
3247
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3249 if (nbbuf == NULL)
3250 return;
3251
3252 if (len < 0)
3253 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00003254 nbdebug(("Negative len %ld in netbeans_removed()!\n", len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 return;
3256 }
3257
3258 nbbuf->modified = 1;
3259
3260 pos.lnum = linenr;
3261 pos.col = col;
3262
3263 off = pos2off(bufp, &pos);
3264
Bram Moolenaar89d40322006-08-29 15:30:07 +00003265 sprintf((char *)buf, "%d:remove=%d %ld %ld\n", bufno, r_cmdno, off, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 nbdebug(("EVT: %s", buf));
3267 nb_send((char *)buf, "netbeans_removed");
3268}
3269
3270/*
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003271 * Send netbeans an unmodified command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003274netbeans_unmodified(buf_T *bufp UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275{
Bram Moolenaar09092152010-08-08 16:38:42 +02003276 /* This is a no-op, because NetBeans considers a buffer modified
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 * even when all changes have been undone. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278}
3279
3280/*
3281 * Send a button release event back to netbeans. Its up to netbeans
3282 * to decide what to do (if anything) with this event.
3283 */
3284 void
3285netbeans_button_release(int button)
3286{
3287 char buf[128];
3288 int bufno;
3289
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003290 if (!NETBEANS_OPEN)
3291 return;
3292
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 bufno = nb_getbufno(curbuf);
3294
3295 if (bufno >= 0 && curwin != NULL && curwin->w_buffer == curbuf)
3296 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003297 int col = mouse_col - W_WINCOL(curwin)
Bram Moolenaar67c53842010-05-22 18:28:27 +02003298 - ((curwin->w_p_nu || curwin->w_p_rnu) ? 9 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 long off = pos2off(curbuf, &curwin->w_cursor);
3300
3301 /* sync the cursor position */
Bram Moolenaar89d40322006-08-29 15:30:07 +00003302 sprintf(buf, "%d:newDotAndMark=%d %ld %ld\n", bufno, r_cmdno, off, off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 nbdebug(("EVT: %s", buf));
3304 nb_send(buf, "netbeans_button_release[newDotAndMark]");
3305
Bram Moolenaar89d40322006-08-29 15:30:07 +00003306 sprintf(buf, "%d:buttonRelease=%d %d %ld %d\n", bufno, r_cmdno,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 button, (long)curwin->w_cursor.lnum, col);
3308 nbdebug(("EVT: %s", buf));
3309 nb_send(buf, "netbeans_button_release");
3310 }
3311}
3312
3313
3314/*
Bram Moolenaar143c38c2007-05-10 16:41:10 +00003315 * Send a keypress event back to netbeans. This usually simulates some
Bram Moolenaar009b2592004-10-24 19:18:58 +00003316 * kind of function key press. This function operates on a key code.
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003317 * Return TRUE when the key was sent, FALSE when the command has been
3318 * postponed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 */
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003320 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321netbeans_keycommand(int key)
3322{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 char keyName[60];
Bram Moolenaar009b2592004-10-24 19:18:58 +00003324
3325 netbeans_keyname(key, keyName);
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003326 return netbeans_keystring((char_u *)keyName);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003327}
3328
3329
3330/*
Bram Moolenaar143c38c2007-05-10 16:41:10 +00003331 * Send a keypress event back to netbeans. This usually simulates some
Bram Moolenaar009b2592004-10-24 19:18:58 +00003332 * kind of function key press. This function operates on a key string.
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003333 * Return TRUE when the key was sent, FALSE when the command has been
3334 * postponed.
Bram Moolenaar009b2592004-10-24 19:18:58 +00003335 */
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003336 static int
3337netbeans_keystring(char_u *keyName)
Bram Moolenaar009b2592004-10-24 19:18:58 +00003338{
3339 char buf[2*MAXPATHL];
3340 int bufno = nb_getbufno(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 long off;
3342 char_u *q;
3343
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003344 if (!NETBEANS_OPEN)
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003345 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 if (bufno == -1)
3348 {
3349 nbdebug(("got keycommand for non-NetBeans buffer, opening...\n"));
3350 q = curbuf->b_ffname == NULL ? (char_u *)""
3351 : nb_quote(curbuf->b_ffname);
3352 if (q == NULL)
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003353 return TRUE;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003354 vim_snprintf(buf, sizeof(buf), "0:fileOpened=%d \"%s\" %s %s\n", 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 q,
3356 "T", /* open in NetBeans */
3357 "F"); /* modified */
3358 if (curbuf->b_ffname != NULL)
3359 vim_free(q);
3360 nbdebug(("EVT: %s", buf));
3361 nb_send(buf, "netbeans_keycommand");
3362
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003363 postpone_keycommand(keyName);
3364 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 }
3366
3367 /* sync the cursor position */
3368 off = pos2off(curbuf, &curwin->w_cursor);
Bram Moolenaar89d40322006-08-29 15:30:07 +00003369 sprintf(buf, "%d:newDotAndMark=%d %ld %ld\n", bufno, r_cmdno, off, off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 nbdebug(("EVT: %s", buf));
3371 nb_send(buf, "netbeans_keycommand");
3372
3373 /* To work on Win32 you must apply patch to ExtEditor module
3374 * from ExtEdCaret.java.diff - make EVT_newDotAndMark handler
3375 * more synchronous
3376 */
3377
3378 /* now send keyCommand event */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003379 vim_snprintf(buf, sizeof(buf), "%d:keyCommand=%d \"%s\"\n",
Bram Moolenaar89d40322006-08-29 15:30:07 +00003380 bufno, r_cmdno, keyName);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 nbdebug(("EVT: %s", buf));
3382 nb_send(buf, "netbeans_keycommand");
3383
3384 /* New: do both at once and include the lnum/col. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003385 vim_snprintf(buf, sizeof(buf), "%d:keyAtPos=%d \"%s\" %ld %ld/%ld\n",
Bram Moolenaar89d40322006-08-29 15:30:07 +00003386 bufno, r_cmdno, keyName,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 off, (long)curwin->w_cursor.lnum, (long)curwin->w_cursor.col);
3388 nbdebug(("EVT: %s", buf));
3389 nb_send(buf, "netbeans_keycommand");
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003390 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391}
3392
3393
3394/*
3395 * Send a save event to netbeans.
3396 */
3397 void
3398netbeans_save_buffer(buf_T *bufp)
3399{
3400 char_u buf[64];
3401 int bufno;
3402 nbbuf_T *nbbuf;
3403
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003404 if (!NETBEANS_OPEN)
3405 return;
3406
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3408 if (nbbuf == NULL)
3409 return;
3410
3411 nbbuf->modified = 0;
3412
Bram Moolenaar89d40322006-08-29 15:30:07 +00003413 sprintf((char *)buf, "%d:save=%d\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 nbdebug(("EVT: %s", buf));
3415 nb_send((char *)buf, "netbeans_save_buffer");
3416}
3417
3418
3419/*
3420 * Send remove command to netbeans (this command has been turned off).
3421 */
3422 void
3423netbeans_deleted_all_lines(buf_T *bufp)
3424{
3425 char_u buf[64];
3426 int bufno;
3427 nbbuf_T *nbbuf;
3428
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003429 if (!NETBEANS_OPEN)
3430 return;
3431
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3433 if (nbbuf == NULL)
3434 return;
3435
Bram Moolenaar009b2592004-10-24 19:18:58 +00003436 /* Don't mark as modified for initial read */
3437 if (nbbuf->insertDone)
3438 nbbuf->modified = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439
Bram Moolenaar89d40322006-08-29 15:30:07 +00003440 sprintf((char *)buf, "%d:remove=%d 0 -1\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 nbdebug(("EVT(suppressed): %s", buf));
3442/* nb_send(buf, "netbeans_deleted_all_lines"); */
3443}
3444
3445
3446/*
3447 * See if the lines are guarded. The top and bot parameters are from
3448 * u_savecommon(), these are the line above the change and the line below the
3449 * change.
3450 */
3451 int
3452netbeans_is_guarded(linenr_T top, linenr_T bot)
3453{
3454 signlist_T *p;
3455 int lnum;
3456
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003457 if (!NETBEANS_OPEN)
3458 return FALSE;
3459
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 for (p = curbuf->b_signlist; p != NULL; p = p->next)
3461 if (p->id >= GUARDEDOFFSET)
3462 for (lnum = top + 1; lnum < bot; lnum++)
3463 if (lnum == p->lnum)
3464 return TRUE;
3465
3466 return FALSE;
3467}
3468
Bram Moolenaar173c9852010-09-29 17:27:01 +02003469#if defined(FEAT_GUI_X11) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470/*
3471 * We have multiple signs to draw at the same location. Draw the
3472 * multi-sign indicator instead. This is the Motif version.
3473 */
3474 void
3475netbeans_draw_multisign_indicator(int row)
3476{
3477 int i;
3478 int y;
3479 int x;
3480
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003481 if (!NETBEANS_OPEN)
3482 return;
3483
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 x = 0;
3485 y = row * gui.char_height + 2;
3486
3487 for (i = 0; i < gui.char_height - 3; i++)
3488 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y++);
3489
3490 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+0, y);
3491 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3492 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+4, y++);
3493 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+1, y);
3494 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3495 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+3, y++);
3496 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3497}
Bram Moolenaar173c9852010-09-29 17:27:01 +02003498#endif /* FEAT_GUI_X11 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499
Bram Moolenaar64404472010-06-26 06:24:45 +02003500#if defined(FEAT_GUI_GTK) && !defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501/*
3502 * We have multiple signs to draw at the same location. Draw the
3503 * multi-sign indicator instead. This is the GTK/Gnome version.
3504 */
3505 void
3506netbeans_draw_multisign_indicator(int row)
3507{
3508 int i;
3509 int y;
3510 int x;
3511 GdkDrawable *drawable = gui.drawarea->window;
3512
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003513 if (!NETBEANS_OPEN)
3514 return;
3515
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 x = 0;
3517 y = row * gui.char_height + 2;
3518
3519 for (i = 0; i < gui.char_height - 3; i++)
3520 gdk_draw_point(drawable, gui.text_gc, x+2, y++);
3521
3522 gdk_draw_point(drawable, gui.text_gc, x+0, y);
3523 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3524 gdk_draw_point(drawable, gui.text_gc, x+4, y++);
3525 gdk_draw_point(drawable, gui.text_gc, x+1, y);
3526 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3527 gdk_draw_point(drawable, gui.text_gc, x+3, y++);
3528 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3529}
3530#endif /* FEAT_GUI_GTK */
3531
3532/*
3533 * If the mouse is clicked in the gutter of a line with multiple
3534 * annotations, cycle through the set of signs.
3535 */
3536 void
3537netbeans_gutter_click(linenr_T lnum)
3538{
3539 signlist_T *p;
3540
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003541 if (!NETBEANS_OPEN)
3542 return;
3543
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 for (p = curbuf->b_signlist; p != NULL; p = p->next)
3545 {
3546 if (p->lnum == lnum && p->next && p->next->lnum == lnum)
3547 {
3548 signlist_T *tail;
3549
3550 /* remove "p" from list, reinsert it at the tail of the sublist */
3551 if (p->prev)
3552 p->prev->next = p->next;
3553 else
3554 curbuf->b_signlist = p->next;
3555 p->next->prev = p->prev;
3556 /* now find end of sublist and insert p */
3557 for (tail = p->next;
3558 tail->next && tail->next->lnum == lnum
3559 && tail->next->id < GUARDEDOFFSET;
3560 tail = tail->next)
3561 ;
3562 /* tail now points to last entry with same lnum (except
3563 * that "guarded" annotations are always last) */
3564 p->next = tail->next;
3565 if (tail->next)
3566 tail->next->prev = p;
3567 p->prev = tail;
3568 tail->next = p;
3569 update_debug_sign(curbuf, lnum);
3570 break;
3571 }
3572 }
3573}
3574
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575/*
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003576 * Add a sign of the requested type at the requested location.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 *
3578 * Reverse engineering:
3579 * Apparently an annotation is defined the first time it is used in a buffer.
3580 * When the same annotation is used in two buffers, the second time we do not
3581 * need to define a new sign name but reuse the existing one. But since the
3582 * ID number used in the second buffer starts counting at one again, a mapping
3583 * is made from the ID specifically for the buffer to the global sign name
3584 * (which is a number).
3585 *
3586 * globalsignmap[] stores the signs that have been defined globally.
3587 * buf->signmapused[] maps buffer-local annotation IDs to an index in
3588 * globalsignmap[].
3589 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 static void
3591addsigntype(
3592 nbbuf_T *buf,
3593 int typeNum,
3594 char_u *typeName,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003595 char_u *tooltip UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 char_u *glyphFile,
Bram Moolenaar67c53842010-05-22 18:28:27 +02003597 char_u *fg,
3598 char_u *bg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 int i, j;
Bram Moolenaar67c53842010-05-22 18:28:27 +02003601 int use_fg = (*fg && STRCMP(fg, "none") != 0);
3602 int use_bg = (*bg && STRCMP(bg, "none") != 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603
3604 for (i = 0; i < globalsignmapused; i++)
3605 if (STRCMP(typeName, globalsignmap[i]) == 0)
3606 break;
3607
3608 if (i == globalsignmapused) /* not found; add it to global map */
3609 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003610 nbdebug(("DEFINEANNOTYPE(%d,%s,%s,%s,%s,%s)\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 typeNum, typeName, tooltip, glyphFile, fg, bg));
3612 if (use_fg || use_bg)
3613 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003614 char fgbuf[2 * (8 + MAX_COLOR_LENGTH) + 1];
3615 char bgbuf[2 * (8 + MAX_COLOR_LENGTH) + 1];
3616 char *ptr;
3617 int value;
3618
3619 value = strtol((char *)fg, &ptr, 10);
3620 if (ptr != (char *)fg)
3621 sprintf(fgbuf, "guifg=#%06x", value & 0xFFFFFF);
3622 else
3623 sprintf(fgbuf, "guifg=%s ctermfg=%s", fg, fg);
3624
3625 value = strtol((char *)bg, &ptr, 10);
3626 if (ptr != (char *)bg)
3627 sprintf(bgbuf, "guibg=#%06x", value & 0xFFFFFF);
3628 else
3629 sprintf(bgbuf, "guibg=%s ctermbg=%s", bg, bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630
3631 coloncmd(":highlight NB_%s %s %s", typeName, (use_fg) ? fgbuf : "",
3632 (use_bg) ? bgbuf : "");
3633 if (*glyphFile == NUL)
3634 /* no glyph, line highlighting only */
3635 coloncmd(":sign define %d linehl=NB_%s", i + 1, typeName);
3636 else if (vim_strsize(glyphFile) <= 2)
3637 /* one- or two-character glyph name, use as text glyph with
3638 * texthl */
3639 coloncmd(":sign define %d text=%s texthl=NB_%s", i + 1,
3640 glyphFile, typeName);
3641 else
3642 /* glyph, line highlighting */
3643 coloncmd(":sign define %d icon=%s linehl=NB_%s", i + 1,
3644 glyphFile, typeName);
3645 }
3646 else
3647 /* glyph, no line highlighting */
3648 coloncmd(":sign define %d icon=%s", i + 1, glyphFile);
3649
3650 if (STRCMP(typeName,"CurrentPC") == 0)
3651 curPCtype = typeNum;
3652
3653 if (globalsignmapused == globalsignmaplen)
3654 {
3655 if (globalsignmaplen == 0) /* first allocation */
3656 {
3657 globalsignmaplen = 20;
3658 globalsignmap = (char **)alloc_clear(globalsignmaplen*sizeof(char *));
3659 }
3660 else /* grow it */
3661 {
3662 int incr;
3663 int oldlen = globalsignmaplen;
3664
3665 globalsignmaplen *= 2;
3666 incr = globalsignmaplen - oldlen;
3667 globalsignmap = (char **)vim_realloc(globalsignmap,
3668 globalsignmaplen * sizeof(char *));
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003669 vim_memset(globalsignmap + oldlen, 0, incr * sizeof(char *));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 }
3671 }
3672
3673 globalsignmap[i] = (char *)typeName;
3674 globalsignmapused = i + 1;
3675 }
3676
3677 /* check local map; should *not* be found! */
3678 for (j = 0; j < buf->signmapused; j++)
3679 if (buf->signmap[j] == i + 1)
3680 return;
3681
3682 /* add to local map */
3683 if (buf->signmapused == buf->signmaplen)
3684 {
3685 if (buf->signmaplen == 0) /* first allocation */
3686 {
3687 buf->signmaplen = 5;
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003688 buf->signmap = (int *)alloc_clear(buf->signmaplen * sizeof(int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 }
3690 else /* grow it */
3691 {
3692 int incr;
3693 int oldlen = buf->signmaplen;
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003694
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 buf->signmaplen *= 2;
3696 incr = buf->signmaplen - oldlen;
3697 buf->signmap = (int *)vim_realloc(buf->signmap,
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003698 buf->signmaplen * sizeof(int));
3699 vim_memset(buf->signmap + oldlen, 0, incr * sizeof(int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 }
3701 }
3702
3703 buf->signmap[buf->signmapused++] = i + 1;
3704
3705}
3706
3707
3708/*
3709 * See if we have the requested sign type in the buffer.
3710 */
3711 static int
3712mapsigntype(nbbuf_T *buf, int localsigntype)
3713{
3714 if (--localsigntype >= 0 && localsigntype < buf->signmapused)
3715 return buf->signmap[localsigntype];
3716
3717 return 0;
3718}
3719
3720
3721/*
3722 * Compute length of buffer, don't print anything.
3723 */
3724 static long
3725get_buf_size(buf_T *bufp)
3726{
3727 linenr_T lnum;
3728 long char_count = 0;
3729 int eol_size;
3730 long last_check = 100000L;
3731
3732 if (bufp->b_ml.ml_flags & ML_EMPTY)
3733 return 0;
3734 else
3735 {
3736 if (get_fileformat(bufp) == EOL_DOS)
3737 eol_size = 2;
3738 else
3739 eol_size = 1;
3740 for (lnum = 1; lnum <= bufp->b_ml.ml_line_count; ++lnum)
3741 {
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00003742 char_count += (long)STRLEN(ml_get_buf(bufp, lnum, FALSE))
3743 + eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 /* Check for a CTRL-C every 100000 characters */
3745 if (char_count > last_check)
3746 {
3747 ui_breakcheck();
3748 if (got_int)
3749 return char_count;
3750 last_check = char_count + 100000L;
3751 }
3752 }
3753 /* Correction for when last line doesn't have an EOL. */
3754 if (!bufp->b_p_eol && bufp->b_p_bin)
3755 char_count -= eol_size;
3756 }
3757
3758 return char_count;
3759}
3760
3761/*
3762 * Convert character offset to lnum,col
3763 */
3764 static pos_T *
3765off2pos(buf_T *buf, long offset)
3766{
3767 linenr_T lnum;
3768 static pos_T pos;
3769
3770 pos.lnum = 0;
3771 pos.col = 0;
3772#ifdef FEAT_VIRTUALEDIT
3773 pos.coladd = 0;
3774#endif
3775
3776 if (!(buf->b_ml.ml_flags & ML_EMPTY))
3777 {
3778 if ((lnum = ml_find_line_or_offset(buf, (linenr_T)0, &offset)) < 0)
3779 return NULL;
3780 pos.lnum = lnum;
3781 pos.col = offset;
3782 }
3783
3784 return &pos;
3785}
3786
3787/*
3788 * Convert an argument in the form "1234" to an offset and compute the
3789 * lnum/col from it. Convert an argument in the form "123/12" directly to a
3790 * lnum/col.
3791 * "argp" is advanced to after the argument.
3792 * Return a pointer to the position, NULL if something is wrong.
3793 */
3794 static pos_T *
3795get_off_or_lnum(buf_T *buf, char_u **argp)
3796{
3797 static pos_T mypos;
3798 long off;
3799
3800 off = strtol((char *)*argp, (char **)argp, 10);
3801 if (**argp == '/')
3802 {
3803 mypos.lnum = (linenr_T)off;
3804 ++*argp;
3805 mypos.col = strtol((char *)*argp, (char **)argp, 10);
3806#ifdef FEAT_VIRTUALEDIT
3807 mypos.coladd = 0;
3808#endif
3809 return &mypos;
3810 }
3811 return off2pos(buf, off);
3812}
3813
3814
3815/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003816 * Convert (lnum,col) to byte offset in the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 */
3818 static long
3819pos2off(buf_T *buf, pos_T *pos)
3820{
3821 long offset = 0;
3822
3823 if (!(buf->b_ml.ml_flags & ML_EMPTY))
3824 {
3825 if ((offset = ml_find_line_or_offset(buf, pos->lnum, 0)) < 0)
3826 return 0;
3827 offset += pos->col;
3828 }
3829
3830 return offset;
3831}
3832
3833
Bram Moolenaar009b2592004-10-24 19:18:58 +00003834/*
3835 * This message is printed after NetBeans opens a new file. Its
3836 * similar to the message readfile() uses, but since NetBeans
3837 * doesn't normally call readfile, we do our own.
3838 */
3839 static void
3840print_read_msg(buf)
3841 nbbuf_T *buf;
3842{
3843 int lnum = buf->bufp->b_ml.ml_line_count;
Bram Moolenaar914703b2010-05-31 21:59:46 +02003844 off_t nchars = buf->bufp->b_orig_size;
Bram Moolenaar009b2592004-10-24 19:18:58 +00003845 char_u c;
3846
3847 msg_add_fname(buf->bufp, buf->bufp->b_ffname);
3848 c = FALSE;
3849
3850 if (buf->bufp->b_p_ro)
3851 {
3852 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]"));
3853 c = TRUE;
3854 }
3855 if (!buf->bufp->b_start_eol)
3856 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003857 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]")
3858 : _("[Incomplete last line]"));
Bram Moolenaar009b2592004-10-24 19:18:58 +00003859 c = TRUE;
3860 }
3861 msg_add_lines(c, (long)lnum, nchars);
3862
3863 /* Now display it */
3864 vim_free(keep_msg);
3865 keep_msg = NULL;
3866 msg_scrolled_ign = TRUE;
3867 msg_trunc_attr(IObuff, FALSE, 0);
3868 msg_scrolled_ign = FALSE;
3869}
3870
3871
3872/*
Bram Moolenaar67c53842010-05-22 18:28:27 +02003873 * Print a message after NetBeans writes the file. This message should be
3874 * identical to the standard message a non-netbeans user would see when
3875 * writing a file.
Bram Moolenaar009b2592004-10-24 19:18:58 +00003876 */
3877 static void
3878print_save_msg(buf, nchars)
3879 nbbuf_T *buf;
Bram Moolenaar914703b2010-05-31 21:59:46 +02003880 off_t nchars;
Bram Moolenaar009b2592004-10-24 19:18:58 +00003881{
3882 char_u c;
3883 char_u *p;
3884
3885 if (nchars >= 0)
3886 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003887 /* put fname in IObuff with quotes */
3888 msg_add_fname(buf->bufp, buf->bufp->b_ffname);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003889 c = FALSE;
3890
3891 msg_add_lines(c, buf->bufp->b_ml.ml_line_count,
Bram Moolenaar914703b2010-05-31 21:59:46 +02003892 buf->bufp->b_orig_size);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003893
3894 vim_free(keep_msg);
3895 keep_msg = NULL;
3896 msg_scrolled_ign = TRUE;
3897 p = msg_trunc_attr(IObuff, FALSE, 0);
3898 if ((msg_scrolled && !need_wait_return) || !buf->initDone)
3899 {
3900 /* Need to repeat the message after redrawing when:
3901 * - When reading from stdin (the screen will be cleared next).
3902 * - When restart_edit is set (otherwise there will be a delay
3903 * before redrawing).
3904 * - When the screen was scrolled but there is no wait-return
3905 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003906 set_keep_msg(p, 0);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003907 }
3908 msg_scrolled_ign = FALSE;
3909 /* add_to_input_buf((char_u *)"\f", 1); */
3910 }
3911 else
3912 {
3913 char_u ebuf[BUFSIZ];
3914
3915 STRCPY(ebuf, (char_u *)_("E505: "));
3916 STRCAT(ebuf, IObuff);
3917 STRCAT(ebuf, (char_u *)_("is read-only (add ! to override)"));
3918 STRCPY(IObuff, ebuf);
Bram Moolenaarf2330482008-06-24 20:19:36 +00003919 nbdebug((" %s\n", ebuf ));
Bram Moolenaar009b2592004-10-24 19:18:58 +00003920 emsg(IObuff);
3921 }
3922}
3923
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924#endif /* defined(FEAT_NETBEANS_INTG) */