blob: 50882e0fb9178d0f940293357a7ef1c59b77f066 [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
Bram Moolenaarc236c162008-07-13 17:41:49 +000019#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
Bram Moolenaarff064e12008-06-09 13:10:45 +000020# include "vimio.h" /* for mch_open(), must be before vim.h */
21#endif
22
Bram Moolenaar071d4272004-06-13 20:20:40 +000023#include "vim.h"
24
25#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
26
Bram Moolenaarb26e6322010-05-22 21:34:09 +020027/* TODO: when should this not be defined? */
28#define INET_SOCKETS
29
Bram Moolenaar071d4272004-06-13 20:20:40 +000030/* Note: when making changes here also adjust configure.in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000031#ifdef WIN32
32# ifdef DEBUG
33# include <tchar.h> /* for _T definition for TRACEn macros */
34# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000035/* WinSock API is separated from C API, thus we can't use read(), write(),
36 * errno... */
37# define sock_errno WSAGetLastError()
Bram Moolenaarbd42a0f2009-06-16 14:57:26 +000038# undef ECONNREFUSED
Bram Moolenaar071d4272004-06-13 20:20:40 +000039# define ECONNREFUSED WSAECONNREFUSED
40# ifdef EINTR
41# undef EINTR
42# endif
43# define EINTR WSAEINTR
44# define sock_write(sd, buf, len) send(sd, buf, len, 0)
45# define sock_read(sd, buf, len) recv(sd, buf, len, 0)
46# define sock_close(sd) closesocket(sd)
47# define sleep(t) Sleep(t*1000) /* WinAPI Sleep() accepts milliseconds */
48#else
Bram Moolenaarb26e6322010-05-22 21:34:09 +020049# ifdef INET_SOCKETS
50# include <netdb.h>
51# include <netinet/in.h>
52# else
53# include <sys/un.h>
54# endif
55
Bram Moolenaar071d4272004-06-13 20:20:40 +000056# include <sys/socket.h>
57# ifdef HAVE_LIBGEN_H
58# include <libgen.h>
59# endif
60# define sock_errno errno
61# define sock_write(sd, buf, len) write(sd, buf, len)
62# define sock_read(sd, buf, len) read(sd, buf, len)
63# define sock_close(sd) close(sd)
64#endif
65
66#include "version.h"
67
Bram Moolenaar071d4272004-06-13 20:20:40 +000068#define GUARDED 10000 /* typenr for "guarded" annotation */
69#define GUARDEDOFFSET 1000000 /* base for "guarded" sign id's */
Bram Moolenaar67c53842010-05-22 18:28:27 +020070#define MAX_COLOR_LENGTH 32 /* max length of color name in defineAnnoType */
Bram Moolenaar071d4272004-06-13 20:20:40 +000071
72/* The first implementation (working only with Netbeans) returned "1.1". The
73 * protocol implemented here also supports A-A-P. */
Bram Moolenaar67c53842010-05-22 18:28:27 +020074static char *ExtEdProtocolVersion = "2.5";
Bram Moolenaar071d4272004-06-13 20:20:40 +000075
76static long pos2off __ARGS((buf_T *, pos_T *));
77static pos_T *off2pos __ARGS((buf_T *, long));
78static pos_T *get_off_or_lnum __ARGS((buf_T *buf, char_u **argp));
79static long get_buf_size __ARGS((buf_T *));
Bram Moolenaar8065d7f2010-01-19 15:13:14 +010080static int netbeans_keystring __ARGS((char_u *keystr));
81static void postpone_keycommand __ARGS((char_u *keystr));
Bram Moolenaar009b2592004-10-24 19:18:58 +000082static void special_keys __ARGS((char_u *args));
Bram Moolenaar071d4272004-06-13 20:20:40 +000083
Bram Moolenaarb26e6322010-05-22 21:34:09 +020084static int netbeans_connect __ARGS((char *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +000085static int getConnInfo __ARGS((char *file, char **host, char **port, char **password));
86
87static void nb_init_graphics __ARGS((void));
88static void coloncmd __ARGS((char *cmd, ...));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000089static void nb_set_curbuf __ARGS((buf_T *buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000090#ifdef FEAT_GUI_MOTIF
91static void messageFromNetbeans __ARGS((XtPointer, int *, XtInputId *));
92#endif
93#ifdef FEAT_GUI_GTK
94static void messageFromNetbeans __ARGS((gpointer, gint, GdkInputCondition));
95#endif
96static void nb_parse_cmd __ARGS((char_u *));
97static int nb_do_cmd __ARGS((int, char_u *, int, int, char_u *));
98static void nb_send __ARGS((char *buf, char *fun));
Bram Moolenaarb26e6322010-05-22 21:34:09 +020099static void nb_free __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100
Bram Moolenaar67c53842010-05-22 18:28:27 +0200101/* TRUE when netbeans is running with a GUI. */
102#ifdef FEAT_GUI
103# define NB_HAS_GUI (gui.in_use || gui.starting)
104#endif
105
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000106#ifdef WIN64
107typedef __int64 NBSOCK;
108#else
109typedef int NBSOCK;
110#endif
111
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200112static NBSOCK nbsock = -1; /* socket fd for Netbeans connection */
113#define NETBEANS_OPEN (nbsock != -1)
114
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115#ifdef FEAT_GUI_MOTIF
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200116static XtInputId inputHandler = (XtInputId)NULL; /* Cookie for input */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117#endif
118#ifdef FEAT_GUI_GTK
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200119static gint inputHandler = 0; /* Cookie for input */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120#endif
121#ifdef FEAT_GUI_W32
122static int inputHandler = -1; /* simply ret.value of WSAAsyncSelect() */
123extern HWND s_hwnd; /* Gvim's Window handle */
124#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +0000125static int r_cmdno; /* current command number for reply */
Bram Moolenaar009b2592004-10-24 19:18:58 +0000126static int dosetvisible = FALSE;
127
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128/*
129 * Include the debugging code if wanted.
130 */
131#ifdef NBDEBUG
132# include "nbdebug.c"
133#endif
134
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200135static int needupdate = 0;
136static int inAtomic = 0;
137
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138 static void
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200139netbeans_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140{
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200141 if (!NETBEANS_OPEN)
142 return;
143
144 netbeans_send_disconnect();
145
Bram Moolenaar67c53842010-05-22 18:28:27 +0200146#ifdef FEAT_GUI_MOTIF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147 if (inputHandler != (XtInputId)NULL)
148 {
149 XtRemoveInput(inputHandler);
150 inputHandler = (XtInputId)NULL;
151 }
Bram Moolenaar67c53842010-05-22 18:28:27 +0200152#else
153# ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154 if (inputHandler != 0)
155 {
156 gdk_input_remove(inputHandler);
157 inputHandler = 0;
158 }
Bram Moolenaar67c53842010-05-22 18:28:27 +0200159# else
160# ifdef FEAT_GUI_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 if (inputHandler == 0)
162 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200163 WSAAsyncSelect(nbsock, s_hwnd, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164 inputHandler = -1;
165 }
Bram Moolenaar67c53842010-05-22 18:28:27 +0200166# endif
167# endif
168#endif
169
Bram Moolenaar67c53842010-05-22 18:28:27 +0200170#ifdef FEAT_BEVAL
Bram Moolenaard62bec82005-03-07 22:56:57 +0000171 bevalServers &= ~BEVAL_NETBEANS;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200172#endif
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200173
174 sock_close(nbsock);
175 nbsock = -1;
176
177 needupdate = 0;
178 inAtomic = 0;
179 nb_free();
180
181 /* remove all signs and update the screen after gutter removal */
182 coloncmd(":sign unplace *");
183 changed_window_setting();
184 update_screen(CLEAR);
185 setcursor();
186 out_flush();
187#ifdef FEAT_GUI
188 gui_update_cursor(TRUE, FALSE);
189 gui_mch_flush();
190#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192
193#define NB_DEF_HOST "localhost"
194#define NB_DEF_ADDR "3219"
195#define NB_DEF_PASS "changeme"
196
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200197 static int
198netbeans_connect(char *params, int abort)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199{
200#ifdef INET_SOCKETS
201 struct sockaddr_in server;
202 struct hostent * host;
203# ifdef FEAT_GUI_W32
204 u_short port;
205# else
206 int port;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200207# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208#else
209 struct sockaddr_un server;
210#endif
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200211 int sd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212 char buf[32];
213 char *hostname = NULL;
214 char *address = NULL;
215 char *password = NULL;
216 char *fname;
217 char *arg = NULL;
218
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200219 if (*params == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200221 /* "=fname": Read info from specified file. */
222 if (getConnInfo(params + 1, &hostname, &address, &password)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223 == FAIL)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200224 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225 }
226 else
227 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200228 if (*params == ':')
229 /* ":<host>:<addr>:<password>": get info from argument */
230 arg = params + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231 if (arg == NULL && (fname = getenv("__NETBEANS_CONINFO")) != NULL)
232 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200233 /* "": get info from file specified in environment */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234 if (getConnInfo(fname, &hostname, &address, &password) == FAIL)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200235 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236 }
237 else
238 {
239 if (arg != NULL)
240 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200241 /* ":<host>:<addr>:<password>": get info from argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 hostname = arg;
243 address = strchr(hostname, ':');
244 if (address != NULL)
245 {
246 *address++ = '\0';
247 password = strchr(address, ':');
248 if (password != NULL)
249 *password++ = '\0';
250 }
251 }
252
253 /* Get the missing values from the environment. */
254 if (hostname == NULL || *hostname == '\0')
255 hostname = getenv("__NETBEANS_HOST");
256 if (address == NULL)
257 address = getenv("__NETBEANS_SOCKET");
258 if (password == NULL)
259 password = getenv("__NETBEANS_VIM_PASSWORD");
260
261 /* Move values to allocated memory. */
262 if (hostname != NULL)
263 hostname = (char *)vim_strsave((char_u *)hostname);
264 if (address != NULL)
265 address = (char *)vim_strsave((char_u *)address);
266 if (password != NULL)
267 password = (char *)vim_strsave((char_u *)password);
268 }
269 }
270
271 /* Use the default when a value is missing. */
272 if (hostname == NULL || *hostname == '\0')
273 {
274 vim_free(hostname);
275 hostname = (char *)vim_strsave((char_u *)NB_DEF_HOST);
276 }
277 if (address == NULL || *address == '\0')
278 {
279 vim_free(address);
280 address = (char *)vim_strsave((char_u *)NB_DEF_ADDR);
281 }
282 if (password == NULL || *password == '\0')
283 {
284 vim_free(password);
285 password = (char *)vim_strsave((char_u *)NB_DEF_PASS);
286 }
287 if (hostname == NULL || address == NULL || password == NULL)
288 goto theend; /* out of memory */
289
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200290#ifdef FEAT_GUI_W32
291 netbeans_init_winsock();
292#endif
293
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294#ifdef INET_SOCKETS
295 port = atoi(address);
296
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000297 if ((sd = (NBSOCK)socket(AF_INET, SOCK_STREAM, 0)) == (NBSOCK)-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000299 nbdebug(("error in socket() in netbeans_connect()\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300 PERROR("socket() in netbeans_connect()");
301 goto theend;
302 }
303
304 /* Get the server internet address and put into addr structure */
305 /* fill in the socket address structure and connect to server */
306 memset((char *)&server, '\0', sizeof(server));
307 server.sin_family = AF_INET;
308 server.sin_port = htons(port);
309 if ((host = gethostbyname(hostname)) == NULL)
310 {
311 if (mch_access(hostname, R_OK) >= 0)
312 {
313 /* DEBUG: input file */
314 sd = mch_open(hostname, O_RDONLY, 0);
315 goto theend;
316 }
Bram Moolenaarf2330482008-06-24 20:19:36 +0000317 nbdebug(("error in gethostbyname() in netbeans_connect()\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318 PERROR("gethostbyname() in netbeans_connect()");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319 goto theend;
320 }
321 memcpy((char *)&server.sin_addr, host->h_addr, host->h_length);
322#else
323 if ((sd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
324 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000325 nbdebug(("error in socket() in netbeans_connect()\n"));
326 PERROR("socket() in netbeans_connect()");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327 goto theend;
328 }
329
330 server.sun_family = AF_UNIX;
331 strcpy(server.sun_path, address);
332#endif
333 /* Connect to server */
334 if (connect(sd, (struct sockaddr *)&server, sizeof(server)))
335 {
336 nbdebug(("netbeans_connect: Connect failed with errno %d\n", sock_errno));
337 if (sock_errno == ECONNREFUSED)
338 {
339 sock_close(sd);
340#ifdef INET_SOCKETS
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000341 if ((sd = (NBSOCK)socket(AF_INET, SOCK_STREAM, 0)) == (NBSOCK)-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000343 nbdebug(("socket()#2 in netbeans_connect()\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344 PERROR("socket()#2 in netbeans_connect()");
345 goto theend;
346 }
347#else
348 if ((sd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
349 {
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#endif
355 if (connect(sd, (struct sockaddr *)&server, sizeof(server)))
356 {
357 int retries = 36;
358 int success = FALSE;
359 while (retries--
360 && ((sock_errno == ECONNREFUSED) || (sock_errno == EINTR)))
361 {
362 nbdebug(("retrying...\n"));
363 sleep(5);
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200364 if (!abort)
365 {
366 ui_breakcheck();
367 if (got_int)
368 {
369 sock_errno = EINTR;
370 break;
371 }
372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373 if (connect(sd, (struct sockaddr *)&server,
374 sizeof(server)) == 0)
375 {
376 success = TRUE;
377 break;
378 }
379 }
380 if (!success)
381 {
382 /* Get here when the server can't be found. */
Bram Moolenaarf2330482008-06-24 20:19:36 +0000383 nbdebug(("Cannot connect to Netbeans #2\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384 PERROR(_("Cannot connect to Netbeans #2"));
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200385 if (abort)
386 getout(1);
387 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 }
389 }
390
391 }
392 else
393 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000394 nbdebug(("Cannot connect to Netbeans\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 PERROR(_("Cannot connect to Netbeans"));
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200396 if (abort)
397 getout(1);
398 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 }
400 }
401
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200402 nbsock = sd;
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000403 vim_snprintf(buf, sizeof(buf), "AUTH %s\n", password);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404 nb_send(buf, "netbeans_connect");
405
406 sprintf(buf, "0:version=0 \"%s\"\n", ExtEdProtocolVersion);
407 nb_send(buf, "externaleditor_version");
408
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409theend:
410 vim_free(hostname);
411 vim_free(address);
412 vim_free(password);
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200413 return NETBEANS_OPEN ? OK : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414}
415
416/*
417 * Obtain the NetBeans hostname, port address and password from a file.
418 * Return the strings in allocated memory.
419 * Return FAIL if the file could not be read, OK otherwise (no matter what it
420 * contains).
421 */
422 static int
423getConnInfo(char *file, char **host, char **port, char **auth)
424{
425 FILE *fp;
426 char_u buf[BUFSIZ];
427 char_u *lp;
428 char_u *nl;
429#ifdef UNIX
430 struct stat st;
431
432 /*
433 * For Unix only accept the file when it's not accessible by others.
434 * The open will then fail if we don't own the file.
435 */
436 if (mch_stat(file, &st) == 0 && (st.st_mode & 0077) != 0)
437 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000438 nbdebug(("Wrong access mode for NetBeans connection info file: \"%s\"\n",
439 file));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 EMSG2(_("E668: Wrong access mode for NetBeans connection info file: \"%s\""),
441 file);
442 return FAIL;
443 }
444#endif
445
446 fp = mch_fopen(file, "r");
447 if (fp == NULL)
448 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000449 nbdebug(("Cannot open NetBeans connection info file\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 PERROR("E660: Cannot open NetBeans connection info file");
451 return FAIL;
452 }
453
454 /* Read the file. There should be one of each parameter */
455 while ((lp = (char_u *)fgets((char *)buf, BUFSIZ, fp)) != NULL)
456 {
457 if ((nl = vim_strchr(lp, '\n')) != NULL)
458 *nl = 0; /* strip off the trailing newline */
459
460 if (STRNCMP(lp, "host=", 5) == 0)
461 {
462 vim_free(*host);
463 *host = (char *)vim_strsave(&buf[5]);
464 }
465 else if (STRNCMP(lp, "port=", 5) == 0)
466 {
467 vim_free(*port);
468 *port = (char *)vim_strsave(&buf[5]);
469 }
470 else if (STRNCMP(lp, "auth=", 5) == 0)
471 {
472 vim_free(*auth);
473 *auth = (char *)vim_strsave(&buf[5]);
474 }
475 }
476 fclose(fp);
477
478 return OK;
479}
480
481
482struct keyqueue
483{
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100484 char_u *keystr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 struct keyqueue *next;
486 struct keyqueue *prev;
487};
488
489typedef struct keyqueue keyQ_T;
490
491static keyQ_T keyHead; /* dummy node, header for circular queue */
492
493
494/*
495 * Queue up key commands sent from netbeans.
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100496 * We store the string, because it may depend on the global mod_mask and
497 * :nbkey doesn't have a key number.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 */
499 static void
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100500postpone_keycommand(char_u *keystr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501{
502 keyQ_T *node;
503
504 node = (keyQ_T *)alloc(sizeof(keyQ_T));
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100505 if (node == NULL)
506 return; /* out of memory, drop the key */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507
508 if (keyHead.next == NULL) /* initialize circular queue */
509 {
510 keyHead.next = &keyHead;
511 keyHead.prev = &keyHead;
512 }
513
514 /* insert node at tail of queue */
515 node->next = &keyHead;
516 node->prev = keyHead.prev;
517 keyHead.prev->next = node;
518 keyHead.prev = node;
519
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100520 node->keystr = vim_strsave(keystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521}
522
523/*
524 * Handle any queued-up NetBeans keycommands to be send.
525 */
526 static void
527handle_key_queue(void)
528{
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100529 int postponed = FALSE;
530
531 while (!postponed && keyHead.next && keyHead.next != &keyHead)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 {
533 /* first, unlink the node */
534 keyQ_T *node = keyHead.next;
535 keyHead.next = node->next;
536 node->next->prev = node->prev;
537
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100538 /* Now, send the keycommand. This may cause it to be postponed again
539 * and change keyHead. */
540 if (node->keystr != NULL)
541 postponed = !netbeans_keystring(node->keystr);
542 vim_free(node->keystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543
544 /* Finally, dispose of the node */
545 vim_free(node);
546 }
547}
548
549
550struct cmdqueue
551{
552 char_u *buffer;
553 struct cmdqueue *next;
554 struct cmdqueue *prev;
555};
556
557typedef struct cmdqueue queue_T;
558
559static queue_T head; /* dummy node, header for circular queue */
560
561
562/*
563 * Put the buffer on the work queue; possibly save it to a file as well.
564 */
565 static void
566save(char_u *buf, int len)
567{
568 queue_T *node;
569
570 node = (queue_T *)alloc(sizeof(queue_T));
571 if (node == NULL)
572 return; /* out of memory */
573 node->buffer = alloc(len + 1);
574 if (node->buffer == NULL)
575 {
576 vim_free(node);
577 return; /* out of memory */
578 }
579 mch_memmove(node->buffer, buf, (size_t)len);
580 node->buffer[len] = NUL;
581
582 if (head.next == NULL) /* initialize circular queue */
583 {
584 head.next = &head;
585 head.prev = &head;
586 }
587
588 /* insert node at tail of queue */
589 node->next = &head;
590 node->prev = head.prev;
591 head.prev->next = node;
592 head.prev = node;
593
594#ifdef NBDEBUG
595 {
596 static int outfd = -2;
597
598 /* possibly write buffer out to a file */
599 if (outfd == -3)
600 return;
601
602 if (outfd == -2)
603 {
604 char *file = getenv("__NETBEANS_SAVE");
605 if (file == NULL)
606 outfd = -3;
607 else
608 outfd = mch_open(file, O_WRONLY|O_CREAT|O_TRUNC, 0666);
609 }
610
611 if (outfd >= 0)
612 write(outfd, buf, len);
613 }
614#endif
615}
616
617
618/*
619 * While there's still a command in the work queue, parse and execute it.
620 */
Bram Moolenaarf2330482008-06-24 20:19:36 +0000621 void
622netbeans_parse_messages(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623{
624 char_u *p;
625 queue_T *node;
626
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200627 if (!NETBEANS_OPEN)
628 return;
629
Bram Moolenaarf2330482008-06-24 20:19:36 +0000630 while (head.next != NULL && head.next != &head)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 {
632 node = head.next;
633
634 /* Locate the first line in the first buffer. */
635 p = vim_strchr(node->buffer, '\n');
636 if (p == NULL)
637 {
638 /* Command isn't complete. If there is no following buffer,
639 * return (wait for more). If there is another buffer following,
640 * prepend the text to that buffer and delete this one. */
641 if (node->next == &head)
642 return;
Bram Moolenaarf2330482008-06-24 20:19:36 +0000643 p = alloc((unsigned)(STRLEN(node->buffer)
644 + STRLEN(node->next->buffer) + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 if (p == NULL)
646 return; /* out of memory */
647 STRCPY(p, node->buffer);
648 STRCAT(p, node->next->buffer);
649 vim_free(node->next->buffer);
650 node->next->buffer = p;
651
652 /* dispose of the node and buffer */
653 head.next = node->next;
654 node->next->prev = node->prev;
655 vim_free(node->buffer);
656 vim_free(node);
657 }
658 else
659 {
660 /* There is a complete command at the start of the buffer.
661 * Terminate it with a NUL. When no more text is following unlink
662 * the buffer. Do this before executing, because new buffers can
663 * be added while busy handling the command. */
664 *p++ = NUL;
665 if (*p == NUL)
666 {
667 head.next = node->next;
668 node->next->prev = node->prev;
669 }
670
671 /* now, parse and execute the commands */
672 nb_parse_cmd(node->buffer);
673
674 if (*p == NUL)
675 {
676 /* buffer finished, dispose of the node and buffer */
677 vim_free(node->buffer);
678 vim_free(node);
679 }
680 else
681 {
682 /* more follows, move to the start */
Bram Moolenaarf2330482008-06-24 20:19:36 +0000683 STRMOVE(node->buffer, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 }
685 }
686 }
687}
688
689/* Buffer size for reading incoming messages. */
690#define MAXMSGSIZE 4096
691
692/*
Bram Moolenaar67c53842010-05-22 18:28:27 +0200693 * Read a command from netbeans.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 */
Bram Moolenaar67c53842010-05-22 18:28:27 +0200695#ifdef FEAT_GUI_MOTIF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 static void
Bram Moolenaara9d45512009-05-17 21:25:42 +0000697messageFromNetbeans(XtPointer clientData UNUSED,
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000698 int *unused1 UNUSED,
699 XtInputId *unused2 UNUSED)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200700{
701 netbeans_read();
702}
703#endif
704
705#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000707messageFromNetbeans(gpointer clientData UNUSED,
708 gint unused1 UNUSED,
709 GdkInputCondition unused2 UNUSED)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200710{
711 netbeans_read();
712}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713#endif
Bram Moolenaar67c53842010-05-22 18:28:27 +0200714
715 void
716netbeans_read()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717{
718 static char_u *buf = NULL;
Bram Moolenaar0b65f892010-05-15 14:49:02 +0200719 int len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 int readlen = 0;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200721#if defined(NB_HAS_GUI) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_W32)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 static int level = 0;
Bram Moolenaarf2330482008-06-24 20:19:36 +0000723#endif
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100724#ifdef HAVE_SELECT
725 struct timeval tval;
726 fd_set rfds;
727#else
728# ifdef HAVE_POLL
729 struct pollfd fds;
730# endif
731#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200733 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 {
735 nbdebug(("messageFromNetbeans() called without a socket\n"));
736 return;
737 }
738
Bram Moolenaar67c53842010-05-22 18:28:27 +0200739#if defined(NB_HAS_GUI) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_W32)
740 /* recursion guard; this will be called from the X event loop at unknown
741 * moments */
742 if (NB_HAS_GUI)
743 ++level;
Bram Moolenaarf2330482008-06-24 20:19:36 +0000744#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745
746 /* Allocate a buffer to read into. */
747 if (buf == NULL)
748 {
749 buf = alloc(MAXMSGSIZE);
750 if (buf == NULL)
751 return; /* out of memory! */
752 }
753
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100754 /* Keep on reading for as long as there is something to read.
755 * Use select() or poll() to avoid blocking on a message that is exactly
756 * MAXMSGSIZE long. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 for (;;)
758 {
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100759#ifdef HAVE_SELECT
760 FD_ZERO(&rfds);
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200761 FD_SET(nbsock, &rfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +0200762 tval.tv_sec = 0;
763 tval.tv_usec = 0;
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200764 if (select(nbsock + 1, &rfds, NULL, NULL, &tval) <= 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200765 break;
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100766#else
767# ifdef HAVE_POLL
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200768 fds.fd = nbsock;
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100769 fds.events = POLLIN;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200770 if (poll(&fds, 1, 0) <= 0)
771 break;
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100772# endif
773#endif
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200774 len = sock_read(nbsock, buf, MAXMSGSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 if (len <= 0)
776 break; /* error or nothing more to read */
777
778 /* Store the read message in the queue. */
779 save(buf, len);
780 readlen += len;
781 if (len < MAXMSGSIZE)
782 break; /* did read everything that's available */
783 }
784
785 if (readlen <= 0)
786 {
787 /* read error or didn't read anything */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200788 netbeans_close();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 nbdebug(("messageFromNetbeans: Error in read() from socket\n"));
790 if (len < 0)
Bram Moolenaarf2330482008-06-24 20:19:36 +0000791 {
792 nbdebug(("read from Netbeans socket\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 PERROR(_("read from Netbeans socket"));
Bram Moolenaarf2330482008-06-24 20:19:36 +0000794 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000795 return; /* don't try to parse it */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 }
797
Bram Moolenaar67c53842010-05-22 18:28:27 +0200798#if defined(NB_HAS_GUI) && !defined(FEAT_GUI_W32)
Bram Moolenaar61665aa2008-12-24 11:20:53 +0000799 /* Let the main loop handle messages. */
Bram Moolenaar67c53842010-05-22 18:28:27 +0200800 if (NB_HAS_GUI)
801 {
Bram Moolenaar61665aa2008-12-24 11:20:53 +0000802# ifdef FEAT_GUI_GTK
Bram Moolenaar67c53842010-05-22 18:28:27 +0200803 if (gtk_main_level() > 0)
804 gtk_main_quit();
805# else
806 /* Parse the messages now, but avoid recursion. */
807 if (level == 1)
808 netbeans_parse_messages();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809
Bram Moolenaar67c53842010-05-22 18:28:27 +0200810 --level;
811# endif
812 }
Bram Moolenaarf2330482008-06-24 20:19:36 +0000813#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814}
815
816/*
817 * Handle one NUL terminated command.
818 *
819 * format of a command from netbeans:
820 *
821 * 6:setTitle!84 "a.c"
822 *
823 * bufno
824 * colon
825 * cmd
826 * !
827 * cmdno
828 * args
829 *
830 * for function calls, the ! is replaced by a /
831 */
832 static void
833nb_parse_cmd(char_u *cmd)
834{
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000835 char *verb;
836 char *q;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 int bufno;
838 int isfunc = -1;
839
840 if (STRCMP(cmd, "DISCONNECT") == 0)
841 {
842 /* We assume the server knows that we can safely exit! */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 /* Disconnect before exiting, Motif hangs in a Select error
844 * message otherwise. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200845 netbeans_close();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 getout(0);
847 /* NOTREACHED */
848 }
849
850 if (STRCMP(cmd, "DETACH") == 0)
851 {
852 /* The IDE is breaking the connection. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200853 netbeans_close();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 return;
855 }
856
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000857 bufno = strtol((char *)cmd, &verb, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858
859 if (*verb != ':')
860 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000861 nbdebug((" missing colon: %s\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 EMSG2("E627: missing colon: %s", cmd);
863 return;
864 }
865 ++verb; /* skip colon */
866
867 for (q = verb; *q; q++)
868 {
869 if (*q == '!')
870 {
871 *q++ = NUL;
872 isfunc = 0;
873 break;
874 }
875 else if (*q == '/')
876 {
877 *q++ = NUL;
878 isfunc = 1;
879 break;
880 }
881 }
882
883 if (isfunc < 0)
884 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000885 nbdebug((" missing ! or / in: %s\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 EMSG2("E628: missing ! or / in: %s", cmd);
887 return;
888 }
889
Bram Moolenaar89d40322006-08-29 15:30:07 +0000890 r_cmdno = strtol(q, &q, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000892 q = (char *)skipwhite((char_u *)q);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893
Bram Moolenaar89d40322006-08-29 15:30:07 +0000894 if (nb_do_cmd(bufno, (char_u *)verb, isfunc, r_cmdno, (char_u *)q) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 {
Bram Moolenaar009b2592004-10-24 19:18:58 +0000896#ifdef NBDEBUG
897 /*
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100898 * This happens because the ExtEd can send a command or 2 after
Bram Moolenaar009b2592004-10-24 19:18:58 +0000899 * doing a stopDocumentListen command. It doesn't harm anything
900 * so I'm disabling it except for debugging.
901 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 nbdebug(("nb_parse_cmd: Command error for \"%s\"\n", cmd));
903 EMSG("E629: bad return from nb_do_cmd");
Bram Moolenaar009b2592004-10-24 19:18:58 +0000904#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 }
906}
907
908struct nbbuf_struct
909{
910 buf_T *bufp;
911 unsigned int fireChanges:1;
912 unsigned int initDone:1;
Bram Moolenaar009b2592004-10-24 19:18:58 +0000913 unsigned int insertDone:1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 unsigned int modified:1;
Bram Moolenaar009b2592004-10-24 19:18:58 +0000915 int nbbuf_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 char *displayname;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 int *signmap;
918 short_u signmaplen;
919 short_u signmapused;
920};
921
922typedef struct nbbuf_struct nbbuf_T;
923
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200924static nbbuf_T *buf_list = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000925static int buf_list_size = 0; /* size of buf_list */
926static int buf_list_used = 0; /* nr of entries in buf_list actually in use */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200928static char **globalsignmap = NULL;
929static int globalsignmaplen = 0;
930static int globalsignmapused = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931
932static int mapsigntype __ARGS((nbbuf_T *, int localsigntype));
933static void addsigntype __ARGS((nbbuf_T *, int localsigntype, char_u *typeName,
934 char_u *tooltip, char_u *glyphfile,
Bram Moolenaar67c53842010-05-22 18:28:27 +0200935 char_u *fg, char_u *bg));
Bram Moolenaar009b2592004-10-24 19:18:58 +0000936static void print_read_msg __ARGS((nbbuf_T *buf));
937static void print_save_msg __ARGS((nbbuf_T *buf, long nchars));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938
939static int curPCtype = -1;
940
941/*
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200942 * Free netbeans resources.
943 */
944 static void
945nb_free()
946{
947 keyQ_T *key_node = keyHead.next;
948 queue_T *cmd_node = head.next;
949 nbbuf_T buf;
950 buf_T *bufp;
951 int i;
952
953 /* free the netbeans buffer list */
954 for (i = 0; i < buf_list_used; i++)
955 {
956 buf = buf_list[i];
957 vim_free(buf.displayname);
958 vim_free(buf.signmap);
959 if ((bufp=buf.bufp) != NULL)
960 {
961 buf.bufp->b_netbeans_file = FALSE;
962 buf.bufp->b_was_netbeans_file = FALSE;
963 }
964 }
965 vim_free(buf_list);
966 buf_list = NULL;
967 buf_list_size = 0;
968 buf_list_used = 0;
969
970 /* free the queued key commands */
971 while(key_node != NULL && key_node != &keyHead)
972 {
973 keyQ_T *next = key_node->next;
974 vim_free(key_node->keystr);
975 vim_free(key_node);
976 if (next == &keyHead)
977 {
978 keyHead.next = &keyHead;
979 keyHead.prev = &keyHead;
980 break;
981 }
982 key_node = next;
983 }
984
985 /* free the queued netbeans commands */
986 while(cmd_node != NULL && cmd_node != &head)
987 {
988 queue_T *next = cmd_node->next;
989 vim_free(cmd_node->buffer);
990 vim_free(cmd_node);
991 if (next == &head)
992 {
993 head.next = &head;
994 head.prev = &head;
995 break;
996 }
997 cmd_node = next;
998 }
999}
1000
1001/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 * Get the Netbeans buffer number for the specified buffer.
1003 */
1004 static int
1005nb_getbufno(buf_T *bufp)
1006{
1007 int i;
1008
1009 for (i = 0; i < buf_list_used; i++)
1010 if (buf_list[i].bufp == bufp)
1011 return i;
1012 return -1;
1013}
1014
1015/*
1016 * Is this a NetBeans-owned buffer?
1017 */
1018 int
1019isNetbeansBuffer(buf_T *bufp)
1020{
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001021 return NETBEANS_OPEN && bufp->b_netbeans_file;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022}
1023
1024/*
1025 * NetBeans and Vim have different undo models. In Vim, the file isn't
1026 * changed if changes are undone via the undo command. In NetBeans, once
1027 * a change has been made the file is marked as modified until saved. It
1028 * doesn't matter if the change was undone.
1029 *
1030 * So this function is for the corner case where Vim thinks a buffer is
1031 * unmodified but NetBeans thinks it IS modified.
1032 */
1033 int
1034isNetbeansModified(buf_T *bufp)
1035{
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001036 if (isNetbeansBuffer(bufp))
Bram Moolenaar009b2592004-10-24 19:18:58 +00001037 {
1038 int bufno = nb_getbufno(bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039
Bram Moolenaar009b2592004-10-24 19:18:58 +00001040 if (bufno > 0)
1041 return buf_list[bufno].modified;
1042 else
1043 return FALSE;
1044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 else
1046 return FALSE;
1047}
1048
1049/*
1050 * Given a Netbeans buffer number, return the netbeans buffer.
1051 * Returns NULL for 0 or a negative number. A 0 bufno means a
1052 * non-buffer related command has been sent.
1053 */
1054 static nbbuf_T *
1055nb_get_buf(int bufno)
1056{
1057 /* find or create a buffer with the given number */
1058 int incr;
1059
1060 if (bufno <= 0)
1061 return NULL;
1062
1063 if (!buf_list)
1064 {
1065 /* initialize */
1066 buf_list = (nbbuf_T *)alloc_clear(100 * sizeof(nbbuf_T));
1067 buf_list_size = 100;
1068 }
1069 if (bufno >= buf_list_used) /* new */
1070 {
1071 if (bufno >= buf_list_size) /* grow list */
1072 {
1073 incr = bufno - buf_list_size + 90;
1074 buf_list_size += incr;
1075 buf_list = (nbbuf_T *)vim_realloc(
1076 buf_list, buf_list_size * sizeof(nbbuf_T));
1077 memset(buf_list + buf_list_size - incr, 0, incr * sizeof(nbbuf_T));
1078 }
1079
1080 while (buf_list_used <= bufno)
1081 {
1082 /* Default is to fire text changes. */
1083 buf_list[buf_list_used].fireChanges = 1;
1084 ++buf_list_used;
1085 }
1086 }
1087
1088 return buf_list + bufno;
1089}
1090
1091/*
1092 * Return the number of buffers that are modified.
1093 */
1094 static int
1095count_changed_buffers(void)
1096{
1097 buf_T *bufp;
1098 int n;
1099
1100 n = 0;
1101 for (bufp = firstbuf; bufp != NULL; bufp = bufp->b_next)
1102 if (bufp->b_changed)
1103 ++n;
1104 return n;
1105}
1106
1107/*
1108 * End the netbeans session.
1109 */
1110 void
1111netbeans_end(void)
1112{
1113 int i;
1114 static char buf[128];
1115
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001116 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 return;
1118
1119 for (i = 0; i < buf_list_used; i++)
1120 {
1121 if (!buf_list[i].bufp)
1122 continue;
1123 if (netbeansForcedQuit)
1124 {
1125 /* mark as unmodified so NetBeans won't put up dialog on "killed" */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001126 sprintf(buf, "%d:unmodified=%d\n", i, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 nbdebug(("EVT: %s", buf));
1128 nb_send(buf, "netbeans_end");
1129 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001130 sprintf(buf, "%d:killed=%d\n", i, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 nbdebug(("EVT: %s", buf));
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001132 /* nb_send(buf, "netbeans_end"); avoid "write failed" messages */
1133 ignored = sock_write(nbsock, buf, (int)STRLEN(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135}
1136
1137/*
1138 * Send a message to netbeans.
1139 */
1140 static void
1141nb_send(char *buf, char *fun)
1142{
1143 /* Avoid giving pages full of error messages when the other side has
1144 * exited, only mention the first error until the connection works again. */
1145 static int did_error = FALSE;
1146
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001147 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 {
1149 if (!did_error)
Bram Moolenaarf2330482008-06-24 20:19:36 +00001150 {
1151 nbdebug((" %s(): write while not connected\n", fun));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 EMSG2("E630: %s(): write while not connected", fun);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 did_error = TRUE;
1155 }
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001156 else if (sock_write(nbsock, buf, (int)STRLEN(buf)) != (int)STRLEN(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 {
1158 if (!did_error)
Bram Moolenaarf2330482008-06-24 20:19:36 +00001159 {
1160 nbdebug((" %s(): write failed\n", fun));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 EMSG2("E631: %s(): write failed", fun);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 did_error = TRUE;
1164 }
1165 else
1166 did_error = FALSE;
1167}
1168
1169/*
1170 * Some input received from netbeans requires a response. This function
1171 * handles a response with no information (except the command number).
1172 */
1173 static void
1174nb_reply_nil(int cmdno)
1175{
1176 char reply[32];
1177
Bram Moolenaar009b2592004-10-24 19:18:58 +00001178 nbdebug(("REP %d: <none>\n", cmdno));
1179
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 sprintf(reply, "%d\n", cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 nb_send(reply, "nb_reply_nil");
1182}
1183
1184
1185/*
1186 * Send a response with text.
1187 * "result" must have been quoted already (using nb_quote()).
1188 */
1189 static void
1190nb_reply_text(int cmdno, char_u *result)
1191{
1192 char_u *reply;
1193
Bram Moolenaar009b2592004-10-24 19:18:58 +00001194 nbdebug(("REP %d: %s\n", cmdno, (char *)result));
1195
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001196 reply = alloc((unsigned)STRLEN(result) + 32);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 sprintf((char *)reply, "%d %s\n", cmdno, (char *)result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 nb_send((char *)reply, "nb_reply_text");
1199
1200 vim_free(reply);
1201}
1202
1203
1204/*
1205 * Send a response with a number result code.
1206 */
1207 static void
1208nb_reply_nr(int cmdno, long result)
1209{
1210 char reply[32];
1211
Bram Moolenaar009b2592004-10-24 19:18:58 +00001212 nbdebug(("REP %d: %ld\n", cmdno, result));
1213
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 sprintf(reply, "%d %ld\n", cmdno, result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 nb_send(reply, "nb_reply_nr");
1216}
1217
1218
1219/*
1220 * Encode newline, ret, backslash, double quote for transmission to NetBeans.
1221 */
1222 static char_u *
1223nb_quote(char_u *txt)
1224{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001225 char_u *buf = alloc((unsigned)(2 * STRLEN(txt) + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 char_u *p = txt;
1227 char_u *q = buf;
1228
1229 if (buf == NULL)
1230 return NULL;
1231 for (; *p; p++)
1232 {
1233 switch (*p)
1234 {
1235 case '\"':
1236 case '\\':
1237 *q++ = '\\'; *q++ = *p; break;
1238 /* case '\t': */
1239 /* *q++ = '\\'; *q++ = 't'; break; */
1240 case '\n':
1241 *q++ = '\\'; *q++ = 'n'; break;
1242 case '\r':
1243 *q++ = '\\'; *q++ = 'r'; break;
1244 default:
1245 *q++ = *p;
1246 break;
1247 }
1248 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01001249 *q = '\0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250
1251 return buf;
1252}
1253
1254
1255/*
1256 * Remove top level double quotes; convert backslashed chars.
1257 * Returns an allocated string (NULL for failure).
1258 * If "endp" is not NULL it is set to the character after the terminating
1259 * quote.
1260 */
1261 static char *
1262nb_unquote(char_u *p, char_u **endp)
1263{
1264 char *result = 0;
1265 char *q;
1266 int done = 0;
1267
1268 /* result is never longer than input */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001269 result = (char *)alloc_clear((unsigned)STRLEN(p) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 if (result == NULL)
1271 return NULL;
1272
1273 if (*p++ != '"')
1274 {
1275 nbdebug(("nb_unquote called with string that doesn't start with a quote!: %s\n",
1276 p));
1277 result[0] = NUL;
1278 return result;
1279 }
1280
1281 for (q = result; !done && *p != NUL;)
1282 {
1283 switch (*p)
1284 {
1285 case '"':
1286 /*
1287 * Unbackslashed dquote marks the end, if first char was dquote.
1288 */
1289 done = 1;
1290 break;
1291
1292 case '\\':
1293 ++p;
1294 switch (*p)
1295 {
1296 case '\\': *q++ = '\\'; break;
1297 case 'n': *q++ = '\n'; break;
1298 case 't': *q++ = '\t'; break;
1299 case 'r': *q++ = '\r'; break;
1300 case '"': *q++ = '"'; break;
1301 case NUL: --p; break;
1302 /* default: skip over illegal chars */
1303 }
1304 ++p;
1305 break;
1306
1307 default:
1308 *q++ = *p++;
1309 }
1310 }
1311
1312 if (endp != NULL)
1313 *endp = p;
1314
1315 return result;
1316}
1317
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001318/*
1319 * Remove from "first" byte to "last" byte (inclusive), at line "lnum" of the
1320 * current buffer. Remove to end of line when "last" is MAXCOL.
1321 */
1322 static void
1323nb_partialremove(linenr_T lnum, colnr_T first, colnr_T last)
1324{
1325 char_u *oldtext, *newtext;
1326 int oldlen;
1327 int lastbyte = last;
1328
1329 oldtext = ml_get(lnum);
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001330 oldlen = (int)STRLEN(oldtext);
Bram Moolenaarb3c70982008-01-18 10:40:55 +00001331 if (first >= (colnr_T)oldlen || oldlen == 0) /* just in case */
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001332 return;
1333 if (lastbyte >= oldlen)
1334 lastbyte = oldlen - 1;
1335 newtext = alloc(oldlen - (int)(lastbyte - first));
1336 if (newtext != NULL)
1337 {
1338 mch_memmove(newtext, oldtext, first);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001339 STRMOVE(newtext + first, oldtext + lastbyte + 1);
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001340 nbdebug((" NEW LINE %d: %s\n", lnum, newtext));
1341 ml_replace(lnum, newtext, FALSE);
1342 }
1343}
1344
1345/*
1346 * Replace the "first" line with the concatenation of the "first" and
1347 * the "other" line. The "other" line is not removed.
1348 */
1349 static void
1350nb_joinlines(linenr_T first, linenr_T other)
1351{
1352 int len_first, len_other;
1353 char_u *p;
1354
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001355 len_first = (int)STRLEN(ml_get(first));
1356 len_other = (int)STRLEN(ml_get(other));
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001357 p = alloc((unsigned)(len_first + len_other + 1));
1358 if (p != NULL)
1359 {
1360 mch_memmove(p, ml_get(first), len_first);
1361 mch_memmove(p + len_first, ml_get(other), len_other + 1);
1362 ml_replace(first, p, FALSE);
1363 }
1364}
1365
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366#define SKIP_STOP 2
1367#define streq(a,b) (strcmp(a,b) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368
1369/*
1370 * Do the actual processing of a single netbeans command or function.
Bram Moolenaar143c38c2007-05-10 16:41:10 +00001371 * The difference between a command and function is that a function
1372 * gets a response (it's required) but a command does not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 * For arguments see comment for nb_parse_cmd().
1374 */
1375 static int
1376nb_do_cmd(
1377 int bufno,
1378 char_u *cmd,
1379 int func,
1380 int cmdno,
1381 char_u *args) /* points to space before arguments or NUL */
1382{
1383 int doupdate = 0;
1384 long off = 0;
1385 nbbuf_T *buf = nb_get_buf(bufno);
1386 static int skip = 0;
1387 int retval = OK;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001388 char *cp; /* for when a char pointer is needed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389
1390 nbdebug(("%s %d: (%d) %s %s\n", (func) ? "FUN" : "CMD", cmdno, bufno, cmd,
1391 STRCMP(cmd, "insert") == 0 ? "<text>" : (char *)args));
1392
1393 if (func)
1394 {
1395/* =====================================================================*/
1396 if (streq((char *)cmd, "getModified"))
1397 {
1398 if (buf == NULL || buf->bufp == NULL)
1399 /* Return the number of buffers that are modified. */
1400 nb_reply_nr(cmdno, (long)count_changed_buffers());
1401 else
1402 /* Return whether the buffer is modified. */
1403 nb_reply_nr(cmdno, (long)(buf->bufp->b_changed
1404 || isNetbeansModified(buf->bufp)));
1405/* =====================================================================*/
1406 }
1407 else if (streq((char *)cmd, "saveAndExit"))
1408 {
1409 /* Note: this will exit Vim if successful. */
1410 coloncmd(":confirm qall");
1411
1412 /* We didn't exit: return the number of changed buffers. */
1413 nb_reply_nr(cmdno, (long)count_changed_buffers());
1414/* =====================================================================*/
1415 }
1416 else if (streq((char *)cmd, "getCursor"))
1417 {
1418 char_u text[200];
1419
1420 /* Note: nb_getbufno() may return -1. This indicates the IDE
1421 * didn't assign a number to the current buffer in response to a
1422 * fileOpened event. */
1423 sprintf((char *)text, "%d %ld %d %ld",
1424 nb_getbufno(curbuf),
1425 (long)curwin->w_cursor.lnum,
1426 (int)curwin->w_cursor.col,
1427 pos2off(curbuf, &curwin->w_cursor));
1428 nb_reply_text(cmdno, text);
1429/* =====================================================================*/
1430 }
Bram Moolenaarc65c4912006-11-14 17:29:46 +00001431 else if (streq((char *)cmd, "getAnno"))
1432 {
1433 long linenum = 0;
1434#ifdef FEAT_SIGNS
1435 if (buf == NULL || buf->bufp == NULL)
1436 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001437 nbdebug((" Invalid buffer identifier in getAnno\n"));
1438 EMSG("E652: Invalid buffer identifier in getAnno");
Bram Moolenaarc65c4912006-11-14 17:29:46 +00001439 retval = FAIL;
1440 }
1441 else
1442 {
1443 int serNum;
1444
1445 cp = (char *)args;
1446 serNum = strtol(cp, &cp, 10);
1447 /* If the sign isn't found linenum will be zero. */
1448 linenum = (long)buf_findsign(buf->bufp, serNum);
1449 }
1450#endif
1451 nb_reply_nr(cmdno, linenum);
1452/* =====================================================================*/
1453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 else if (streq((char *)cmd, "getLength"))
1455 {
1456 long len = 0;
1457
1458 if (buf == NULL || buf->bufp == NULL)
1459 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001460 nbdebug((" invalid buffer identifier in getLength\n"));
1461 EMSG("E632: invalid buffer identifier in getLength");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 retval = FAIL;
1463 }
1464 else
1465 {
1466 len = get_buf_size(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 }
1468 nb_reply_nr(cmdno, len);
1469/* =====================================================================*/
1470 }
1471 else if (streq((char *)cmd, "getText"))
1472 {
1473 long len;
1474 linenr_T nlines;
1475 char_u *text = NULL;
1476 linenr_T lno = 1;
1477 char_u *p;
1478 char_u *line;
1479
1480 if (buf == NULL || buf->bufp == NULL)
1481 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001482 nbdebug((" invalid buffer identifier in getText\n"));
1483 EMSG("E633: invalid buffer identifier in getText");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 retval = FAIL;
1485 }
1486 else
1487 {
1488 len = get_buf_size(buf->bufp);
1489 nlines = buf->bufp->b_ml.ml_line_count;
1490 text = alloc((unsigned)((len > 0)
1491 ? ((len + nlines) * 2) : 4));
1492 if (text == NULL)
1493 {
1494 nbdebug((" nb_do_cmd: getText has null text field\n"));
1495 retval = FAIL;
1496 }
1497 else
1498 {
1499 p = text;
1500 *p++ = '\"';
1501 for (; lno <= nlines ; lno++)
1502 {
1503 line = nb_quote(ml_get_buf(buf->bufp, lno, FALSE));
1504 if (line != NULL)
1505 {
1506 STRCPY(p, line);
1507 p += STRLEN(line);
1508 *p++ = '\\';
1509 *p++ = 'n';
Bram Moolenaar009b2592004-10-24 19:18:58 +00001510 vim_free(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 }
1513 *p++ = '\"';
1514 *p = '\0';
1515 }
1516 }
1517 if (text == NULL)
1518 nb_reply_text(cmdno, (char_u *)"");
1519 else
1520 {
1521 nb_reply_text(cmdno, text);
1522 vim_free(text);
1523 }
1524/* =====================================================================*/
1525 }
1526 else if (streq((char *)cmd, "remove"))
1527 {
1528 long count;
1529 pos_T first, last;
1530 pos_T *pos;
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001531 pos_T *next;
1532 linenr_T del_from_lnum, del_to_lnum; /* lines to be deleted as a whole */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 int oldFire = netbeansFireChanges;
1534 int oldSuppress = netbeansSuppressNoLines;
1535 int wasChanged;
1536
1537 if (skip >= SKIP_STOP)
1538 {
1539 nbdebug((" Skipping %s command\n", (char *) cmd));
1540 nb_reply_nil(cmdno);
1541 return OK;
1542 }
1543
1544 if (buf == NULL || buf->bufp == NULL)
1545 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001546 nbdebug((" invalid buffer identifier in remove\n"));
1547 EMSG("E634: invalid buffer identifier in remove");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 retval = FAIL;
1549 }
1550 else
1551 {
1552 netbeansFireChanges = FALSE;
1553 netbeansSuppressNoLines = TRUE;
1554
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001555 nb_set_curbuf(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 wasChanged = buf->bufp->b_changed;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001557 cp = (char *)args;
1558 off = strtol(cp, &cp, 10);
1559 count = strtol(cp, &cp, 10);
1560 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 /* delete "count" chars, starting at "off" */
1562 pos = off2pos(buf->bufp, off);
1563 if (!pos)
1564 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001565 nbdebug((" !bad position\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 nb_reply_text(cmdno, (char_u *)"!bad position");
1567 netbeansFireChanges = oldFire;
1568 netbeansSuppressNoLines = oldSuppress;
1569 return FAIL;
1570 }
1571 first = *pos;
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001572 nbdebug((" FIRST POS: line %d, col %d\n",
1573 first.lnum, first.col));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 pos = off2pos(buf->bufp, off+count-1);
1575 if (!pos)
1576 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001577 nbdebug((" !bad count\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 nb_reply_text(cmdno, (char_u *)"!bad count");
1579 netbeansFireChanges = oldFire;
1580 netbeansSuppressNoLines = oldSuppress;
1581 return FAIL;
1582 }
1583 last = *pos;
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001584 nbdebug((" LAST POS: line %d, col %d\n",
1585 last.lnum, last.col));
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001586 del_from_lnum = first.lnum;
1587 del_to_lnum = last.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 doupdate = 1;
1589
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001590 /* Get the position of the first byte after the deleted
1591 * section. "next" is NULL when deleting to the end of the
1592 * file. */
1593 next = off2pos(buf->bufp, off + count);
1594
1595 /* Remove part of the first line. */
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001596 if (first.col != 0
1597 || (next != NULL && first.lnum == next->lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 {
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001599 if (first.lnum != last.lnum
1600 || (next != NULL && first.lnum != next->lnum))
1601 {
1602 /* remove to the end of the first line */
1603 nb_partialremove(first.lnum, first.col,
1604 (colnr_T)MAXCOL);
1605 if (first.lnum == last.lnum)
1606 {
1607 /* Partial line to remove includes the end of
1608 * line. Join the line with the next one, have
1609 * the next line deleted below. */
1610 nb_joinlines(first.lnum, next->lnum);
1611 del_to_lnum = next->lnum;
1612 }
1613 }
1614 else
1615 {
1616 /* remove within one line */
1617 nb_partialremove(first.lnum, first.col, last.col);
1618 }
1619 ++del_from_lnum; /* don't delete the first line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 }
1621
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001622 /* Remove part of the last line. */
1623 if (first.lnum != last.lnum && next != NULL
1624 && next->col != 0 && last.lnum == next->lnum)
1625 {
1626 nb_partialremove(last.lnum, 0, last.col);
1627 if (del_from_lnum > first.lnum)
1628 {
1629 /* Join end of last line to start of first line; last
1630 * line is deleted below. */
1631 nb_joinlines(first.lnum, last.lnum);
1632 }
1633 else
1634 /* First line is deleted as a whole, keep the last
1635 * line. */
1636 --del_to_lnum;
1637 }
1638
1639 /* First is partial line; last line to remove includes
1640 * the end of line; join first line to line following last
1641 * line; line following last line is deleted below. */
1642 if (first.lnum != last.lnum && del_from_lnum > first.lnum
1643 && next != NULL && last.lnum != next->lnum)
1644 {
1645 nb_joinlines(first.lnum, next->lnum);
1646 del_to_lnum = next->lnum;
1647 }
1648
1649 /* Delete whole lines if there are any. */
1650 if (del_to_lnum >= del_from_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 {
1652 int i;
1653
1654 /* delete signs from the lines being deleted */
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001655 for (i = del_from_lnum; i <= del_to_lnum; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 {
1657 int id = buf_findsign_id(buf->bufp, (linenr_T)i);
1658 if (id > 0)
1659 {
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001660 nbdebug((" Deleting sign %d on line %d\n",
1661 id, i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 buf_delsign(buf->bufp, id);
1663 }
1664 else
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001665 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 nbdebug((" No sign on line %d\n", i));
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 }
1669
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001670 nbdebug((" Deleting lines %d through %d\n",
1671 del_from_lnum, del_to_lnum));
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001672 curwin->w_cursor.lnum = del_from_lnum;
1673 curwin->w_cursor.col = 0;
1674 del_lines(del_to_lnum - del_from_lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 }
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001676
1677 /* Leave cursor at first deleted byte. */
1678 curwin->w_cursor = first;
1679 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 buf->bufp->b_changed = wasChanged; /* logically unchanged */
1681 netbeansFireChanges = oldFire;
1682 netbeansSuppressNoLines = oldSuppress;
1683
1684 u_blockfree(buf->bufp);
1685 u_clearall(buf->bufp);
1686 }
1687 nb_reply_nil(cmdno);
1688/* =====================================================================*/
1689 }
1690 else if (streq((char *)cmd, "insert"))
1691 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 char_u *to_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693
1694 if (skip >= SKIP_STOP)
1695 {
1696 nbdebug((" Skipping %s command\n", (char *) cmd));
1697 nb_reply_nil(cmdno);
1698 return OK;
1699 }
1700
1701 /* get offset */
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001702 cp = (char *)args;
1703 off = strtol(cp, &cp, 10);
1704 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705
1706 /* get text to be inserted */
1707 args = skipwhite(args);
1708 args = to_free = (char_u *)nb_unquote(args, NULL);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001709 /*
1710 nbdebug((" CHUNK[%d]: %d bytes at offset %d\n",
1711 buf->bufp->b_ml.ml_line_count, STRLEN(args), off));
1712 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713
1714 if (buf == NULL || buf->bufp == NULL)
1715 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001716 nbdebug((" invalid buffer identifier in insert\n"));
1717 EMSG("E635: invalid buffer identifier in insert");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 retval = FAIL;
1719 }
1720 else if (args != NULL)
1721 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001722 int ff_detected = EOL_UNKNOWN;
1723 int buf_was_empty = (buf->bufp->b_ml.ml_flags & ML_EMPTY);
1724 size_t len = 0;
1725 int added = 0;
1726 int oldFire = netbeansFireChanges;
1727 int old_b_changed;
1728 char_u *nl;
1729 linenr_T lnum;
1730 linenr_T lnum_start;
1731 pos_T *pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 netbeansFireChanges = 0;
1734
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001735 /* Jump to the buffer where we insert. After this "curbuf"
1736 * can be used. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001737 nb_set_curbuf(buf->bufp);
Bram Moolenaara3227e22006-03-08 21:32:40 +00001738 old_b_changed = curbuf->b_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001740 /* Convert the specified character offset into a lnum/col
1741 * position. */
Bram Moolenaara3227e22006-03-08 21:32:40 +00001742 pos = off2pos(curbuf, off);
1743 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001745 if (pos->lnum <= 0)
1746 lnum_start = 1;
1747 else
1748 lnum_start = pos->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 }
1750 else
1751 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001752 /* If the given position is not found, assume we want
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 * the end of the file. See setLocAndSize HACK. */
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001754 if (buf_was_empty)
1755 lnum_start = 1; /* above empty line */
1756 else
1757 lnum_start = curbuf->b_ml.ml_line_count + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001758 }
1759
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001760 /* "lnum" is the line where we insert: either append to it or
1761 * insert a new line above it. */
1762 lnum = lnum_start;
1763
1764 /* Loop over the "\n" separated lines of the argument. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 doupdate = 1;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001766 while (*args != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001768 nl = vim_strchr(args, '\n');
1769 if (nl == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001771 /* Incomplete line, probably truncated. Next "insert"
1772 * command should append to this one. */
1773 len = STRLEN(args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001775 else
1776 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001777 len = nl - args;
1778
1779 /*
1780 * We need to detect EOL style, because the commands
1781 * use a character offset.
1782 */
1783 if (nl > args && nl[-1] == '\r')
1784 {
1785 ff_detected = EOL_DOS;
1786 --len;
1787 }
1788 else
1789 ff_detected = EOL_UNIX;
1790 }
1791 args[len] = NUL;
1792
1793 if (lnum == lnum_start
1794 && ((pos != NULL && pos->col > 0)
1795 || (lnum == 1 && buf_was_empty)))
1796 {
1797 char_u *oldline = ml_get(lnum);
1798 char_u *newline;
1799
1800 /* Insert halfway a line. For simplicity we assume we
1801 * need to append to the line. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001802 newline = alloc_check((unsigned)(STRLEN(oldline) + len + 1));
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001803 if (newline != NULL)
1804 {
1805 STRCPY(newline, oldline);
1806 STRCAT(newline, args);
1807 ml_replace(lnum, newline, FALSE);
1808 }
1809 }
1810 else
1811 {
1812 /* Append a new line. Not that we always do this,
1813 * also when the text doesn't end in a "\n". */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001814 ml_append((linenr_T)(lnum - 1), args, (colnr_T)(len + 1), FALSE);
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001815 ++added;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001816 }
1817
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001818 if (nl == NULL)
1819 break;
1820 ++lnum;
1821 args = nl + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001822 }
1823
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001824 /* Adjust the marks below the inserted lines. */
1825 appended_lines_mark(lnum_start - 1, (long)added);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001827 /*
1828 * When starting with an empty buffer set the fileformat.
1829 * This is just guessing...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 */
1831 if (buf_was_empty)
1832 {
1833 if (ff_detected == EOL_UNKNOWN)
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001834#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 ff_detected = EOL_DOS;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001836#else
1837 ff_detected = EOL_UNIX;
1838#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 set_fileformat(ff_detected, OPT_LOCAL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00001840 curbuf->b_start_ffc = *curbuf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 }
1842
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 /*
1844 * XXX - GRP - Is the next line right? If I've inserted
1845 * text the buffer has been updated but not written. Will
1846 * netbeans guarantee to write it? Even if I do a :q! ?
1847 */
Bram Moolenaara3227e22006-03-08 21:32:40 +00001848 curbuf->b_changed = old_b_changed; /* logically unchanged */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 netbeansFireChanges = oldFire;
1850
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001851 /* Undo info is invalid now... */
Bram Moolenaara3227e22006-03-08 21:32:40 +00001852 u_blockfree(curbuf);
1853 u_clearall(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 }
1855 vim_free(to_free);
1856 nb_reply_nil(cmdno); /* or !error */
1857 }
1858 else
1859 {
1860 nbdebug(("UNIMPLEMENTED FUNCTION: %s\n", cmd));
1861 nb_reply_nil(cmdno);
1862 retval = FAIL;
1863 }
1864 }
1865 else /* Not a function; no reply required. */
1866 {
1867/* =====================================================================*/
1868 if (streq((char *)cmd, "create"))
1869 {
1870 /* Create a buffer without a name. */
1871 if (buf == NULL)
1872 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001873 nbdebug((" invalid buffer identifier in create\n"));
1874 EMSG("E636: invalid buffer identifier in create");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 return FAIL;
1876 }
1877 vim_free(buf->displayname);
1878 buf->displayname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879
1880 netbeansReadFile = 0; /* don't try to open disk file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001881 do_ecmd(0, NULL, 0, 0, ECMD_ONE, ECMD_HIDE + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 netbeansReadFile = 1;
1883 buf->bufp = curbuf;
1884 maketitle();
Bram Moolenaar009b2592004-10-24 19:18:58 +00001885 buf->insertDone = FALSE;
Bram Moolenaar67c53842010-05-22 18:28:27 +02001886#if defined(FEAT_MENU) && defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 gui_update_menus(0);
Bram Moolenaar67c53842010-05-22 18:28:27 +02001888#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889/* =====================================================================*/
1890 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001891 else if (streq((char *)cmd, "insertDone"))
1892 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001893 if (buf == NULL || buf->bufp == NULL)
1894 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001895 nbdebug((" invalid buffer identifier in insertDone\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001896 }
1897 else
1898 {
1899 buf->bufp->b_start_eol = *args == 'T';
1900 buf->insertDone = TRUE;
1901 args += 2;
1902 buf->bufp->b_p_ro = *args == 'T';
1903 print_read_msg(buf);
1904 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001905/* =====================================================================*/
1906 }
1907 else if (streq((char *)cmd, "saveDone"))
1908 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001909 long savedChars = atol((char *)args);
1910
1911 if (buf == NULL || buf->bufp == NULL)
1912 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001913 nbdebug((" invalid buffer identifier in saveDone\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001914 }
1915 else
1916 print_save_msg(buf, savedChars);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001917/* =====================================================================*/
1918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 else if (streq((char *)cmd, "startDocumentListen"))
1920 {
1921 if (buf == NULL)
1922 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001923 nbdebug((" invalid buffer identifier in startDocumentListen\n"));
1924 EMSG("E637: invalid buffer identifier in startDocumentListen");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 return FAIL;
1926 }
1927 buf->fireChanges = 1;
1928/* =====================================================================*/
1929 }
1930 else if (streq((char *)cmd, "stopDocumentListen"))
1931 {
1932 if (buf == NULL)
1933 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001934 nbdebug((" invalid buffer identifier in stopDocumentListen\n"));
1935 EMSG("E638: invalid buffer identifier in stopDocumentListen");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 return FAIL;
1937 }
1938 buf->fireChanges = 0;
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001939 if (buf->bufp != NULL && buf->bufp->b_was_netbeans_file)
Bram Moolenaar009b2592004-10-24 19:18:58 +00001940 {
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001941 if (!buf->bufp->b_netbeans_file)
Bram Moolenaarf2330482008-06-24 20:19:36 +00001942 {
1943 nbdebug(("E658: NetBeans connection lost for buffer %ld\n", buf->bufp->b_fnum));
Bram Moolenaar009b2592004-10-24 19:18:58 +00001944 EMSGN(_("E658: NetBeans connection lost for buffer %ld"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 buf->bufp->b_fnum);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001946 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001947 else
1948 {
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001949 /* NetBeans uses stopDocumentListen when it stops editing
1950 * a file. It then expects the buffer in Vim to
1951 * disappear. */
1952 do_bufdel(DOBUF_DEL, (char_u *)"", 1,
1953 buf->bufp->b_fnum, buf->bufp->b_fnum, TRUE);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001954 vim_memset(buf, 0, sizeof(nbbuf_T));
1955 }
1956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957/* =====================================================================*/
1958 }
1959 else if (streq((char *)cmd, "setTitle"))
1960 {
1961 if (buf == NULL)
1962 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001963 nbdebug((" invalid buffer identifier in setTitle\n"));
1964 EMSG("E639: invalid buffer identifier in setTitle");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 return FAIL;
1966 }
1967 vim_free(buf->displayname);
1968 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969/* =====================================================================*/
1970 }
1971 else if (streq((char *)cmd, "initDone"))
1972 {
1973 if (buf == NULL || buf->bufp == NULL)
1974 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001975 nbdebug((" invalid buffer identifier in initDone\n"));
1976 EMSG("E640: invalid buffer identifier in initDone");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 return FAIL;
1978 }
1979 doupdate = 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001980 buf->initDone = TRUE;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001981 nb_set_curbuf(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982#if defined(FEAT_AUTOCMD)
1983 apply_autocmds(EVENT_BUFREADPOST, 0, 0, FALSE, buf->bufp);
1984#endif
1985
1986 /* handle any postponed key commands */
1987 handle_key_queue();
1988/* =====================================================================*/
1989 }
1990 else if (streq((char *)cmd, "setBufferNumber")
1991 || streq((char *)cmd, "putBufferNumber"))
1992 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00001993 char_u *path;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 buf_T *bufp;
1995
1996 if (buf == NULL)
1997 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001998 nbdebug((" invalid buffer identifier in setBufferNumber\n"));
1999 EMSG("E641: invalid buffer identifier in setBufferNumber");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 return FAIL;
2001 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002002 path = (char_u *)nb_unquote(args, NULL);
2003 if (path == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 return FAIL;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002005 bufp = buflist_findname(path);
2006 vim_free(path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 if (bufp == NULL)
2008 {
Bram Moolenaarec906222009-02-21 21:14:00 +00002009 nbdebug((" File %s not found in setBufferNumber\n", args));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 EMSG2("E642: File %s not found in setBufferNumber", args);
2011 return FAIL;
2012 }
2013 buf->bufp = bufp;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002014 buf->nbbuf_number = bufp->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015
2016 /* "setBufferNumber" has the side effect of jumping to the buffer
2017 * (don't know why!). Don't do that for "putBufferNumber". */
2018 if (*cmd != 'p')
2019 coloncmd(":buffer %d", bufp->b_fnum);
2020 else
2021 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002022 buf->initDone = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023
2024 /* handle any postponed key commands */
2025 handle_key_queue();
2026 }
2027
2028#if 0 /* never used */
2029 buf->internalname = (char *)alloc_clear(8);
2030 sprintf(buf->internalname, "<%d>", bufno);
2031 buf->netbeansOwns = 0;
2032#endif
2033/* =====================================================================*/
2034 }
2035 else if (streq((char *)cmd, "setFullName"))
2036 {
2037 if (buf == NULL)
2038 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002039 nbdebug((" invalid buffer identifier in setFullName\n"));
2040 EMSG("E643: invalid buffer identifier in setFullName");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 return FAIL;
2042 }
2043 vim_free(buf->displayname);
2044 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045
2046 netbeansReadFile = 0; /* don't try to open disk file */
2047 do_ecmd(0, (char_u *)buf->displayname, 0, 0, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002048 ECMD_HIDE + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 netbeansReadFile = 1;
2050 buf->bufp = curbuf;
2051 maketitle();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002052#if defined(FEAT_MENU) && defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 gui_update_menus(0);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002054#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055/* =====================================================================*/
2056 }
2057 else if (streq((char *)cmd, "editFile"))
2058 {
2059 if (buf == NULL)
2060 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002061 nbdebug((" invalid buffer identifier in editFile\n"));
2062 EMSG("E644: invalid buffer identifier in editFile");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 return FAIL;
2064 }
2065 /* Edit a file: like create + setFullName + read the file. */
2066 vim_free(buf->displayname);
2067 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 do_ecmd(0, (char_u *)buf->displayname, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002069 ECMD_HIDE + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 buf->bufp = curbuf;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002071 buf->initDone = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 doupdate = 1;
2073#if defined(FEAT_TITLE)
2074 maketitle();
2075#endif
Bram Moolenaar67c53842010-05-22 18:28:27 +02002076#if defined(FEAT_MENU) && defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 gui_update_menus(0);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002078#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079/* =====================================================================*/
2080 }
2081 else if (streq((char *)cmd, "setVisible"))
2082 {
2083 if (buf == NULL || buf->bufp == NULL)
2084 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002085 nbdebug((" invalid buffer identifier in setVisible\n"));
2086 /* This message was commented out, probably because it can
2087 * happen when shutting down. */
2088 if (p_verbose > 0)
2089 EMSG("E645: invalid buffer identifier in setVisible");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 return FAIL;
2091 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002092 if (streq((char *)args, "T") && buf->bufp != curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 {
2094 exarg_T exarg;
2095 exarg.cmd = (char_u *)"goto";
2096 exarg.forceit = FALSE;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002097 dosetvisible = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 goto_buffer(&exarg, DOBUF_FIRST, FORWARD, buf->bufp->b_fnum);
2099 doupdate = 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002100 dosetvisible = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101
Bram Moolenaar67c53842010-05-22 18:28:27 +02002102#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 /* Side effect!!!. */
2104 if (!gui.starting)
2105 gui_mch_set_foreground();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002106#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108/* =====================================================================*/
2109 }
2110 else if (streq((char *)cmd, "raise"))
2111 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002112#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 /* Bring gvim to the foreground. */
2114 if (!gui.starting)
2115 gui_mch_set_foreground();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002116#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117/* =====================================================================*/
2118 }
2119 else if (streq((char *)cmd, "setModified"))
2120 {
Bram Moolenaarff064e12008-06-09 13:10:45 +00002121 int prev_b_changed;
2122
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 if (buf == NULL || buf->bufp == NULL)
2124 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002125 nbdebug((" invalid buffer identifier in setModified\n"));
2126 /* This message was commented out, probably because it can
2127 * happen when shutting down. */
2128 if (p_verbose > 0)
2129 EMSG("E646: invalid buffer identifier in setModified");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 return FAIL;
2131 }
Bram Moolenaarff064e12008-06-09 13:10:45 +00002132 prev_b_changed = buf->bufp->b_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 if (streq((char *)args, "T"))
Bram Moolenaarff064e12008-06-09 13:10:45 +00002134 buf->bufp->b_changed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 else
2136 {
2137 struct stat st;
2138
2139 /* Assume NetBeans stored the file. Reset the timestamp to
2140 * avoid "file changed" warnings. */
2141 if (buf->bufp->b_ffname != NULL
2142 && mch_stat((char *)buf->bufp->b_ffname, &st) >= 0)
2143 buf_store_time(buf->bufp, &st, buf->bufp->b_ffname);
Bram Moolenaarff064e12008-06-09 13:10:45 +00002144 buf->bufp->b_changed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 }
2146 buf->modified = buf->bufp->b_changed;
Bram Moolenaarff064e12008-06-09 13:10:45 +00002147 if (prev_b_changed != buf->bufp->b_changed)
2148 {
2149#ifdef FEAT_WINDOWS
2150 check_status(buf->bufp);
2151 redraw_tabline = TRUE;
2152#endif
2153#ifdef FEAT_TITLE
2154 maketitle();
2155#endif
2156 update_screen(0);
2157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158/* =====================================================================*/
2159 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002160 else if (streq((char *)cmd, "setModtime"))
2161 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002162 if (buf == NULL || buf->bufp == NULL)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002163 nbdebug((" invalid buffer identifier in setModtime\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002164 else
2165 buf->bufp->b_mtime = atoi((char *)args);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002166/* =====================================================================*/
2167 }
2168 else if (streq((char *)cmd, "setReadOnly"))
2169 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002170 if (buf == NULL || buf->bufp == NULL)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002171 nbdebug((" invalid buffer identifier in setReadOnly\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002172 else if (streq((char *)args, "T"))
Bram Moolenaar009b2592004-10-24 19:18:58 +00002173 buf->bufp->b_p_ro = TRUE;
2174 else
2175 buf->bufp->b_p_ro = FALSE;
2176/* =====================================================================*/
2177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 else if (streq((char *)cmd, "setMark"))
2179 {
2180 /* not yet */
2181/* =====================================================================*/
2182 }
2183 else if (streq((char *)cmd, "showBalloon"))
2184 {
2185#if defined(FEAT_BEVAL)
2186 static char *text = NULL;
2187
2188 /*
2189 * Set up the Balloon Expression Evaluation area.
2190 * Ignore 'ballooneval' here.
2191 * The text pointer must remain valid for a while.
2192 */
2193 if (balloonEval != NULL)
2194 {
2195 vim_free(text);
2196 text = nb_unquote(args, NULL);
2197 if (text != NULL)
2198 gui_mch_post_balloon(balloonEval, (char_u *)text);
2199 }
2200#endif
2201/* =====================================================================*/
2202 }
2203 else if (streq((char *)cmd, "setDot"))
2204 {
2205 pos_T *pos;
2206#ifdef NBDEBUG
2207 char_u *s;
2208#endif
2209
2210 if (buf == NULL || buf->bufp == NULL)
2211 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002212 nbdebug((" invalid buffer identifier in setDot\n"));
2213 EMSG("E647: invalid buffer identifier in setDot");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 return FAIL;
2215 }
2216
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002217 nb_set_curbuf(buf->bufp);
2218
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219#ifdef FEAT_VISUAL
2220 /* Don't want Visual mode now. */
2221 if (VIsual_active)
2222 end_visual_mode();
2223#endif
2224#ifdef NBDEBUG
2225 s = args;
2226#endif
2227 pos = get_off_or_lnum(buf->bufp, &args);
2228 if (pos)
2229 {
2230 curwin->w_cursor = *pos;
2231 check_cursor();
2232#ifdef FEAT_FOLDING
2233 foldOpenCursor();
2234#endif
2235 }
2236 else
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002237 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 nbdebug((" BAD POSITION in setDot: %s\n", s));
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240
2241 /* gui_update_cursor(TRUE, FALSE); */
2242 /* update_curbuf(NOT_VALID); */
2243 update_topline(); /* scroll to show the line */
2244 update_screen(VALID);
2245 setcursor();
2246 out_flush();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002247#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 gui_update_cursor(TRUE, FALSE);
2249 gui_mch_flush();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002250#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 /* Quit a hit-return or more prompt. */
2252 if (State == HITRETURN || State == ASKMORE)
2253 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254#ifdef FEAT_GUI_GTK
2255 if (gtk_main_level() > 0)
2256 gtk_main_quit();
2257#endif
2258 }
2259/* =====================================================================*/
2260 }
2261 else if (streq((char *)cmd, "close"))
2262 {
2263#ifdef NBDEBUG
2264 char *name = "<NONE>";
2265#endif
2266
2267 if (buf == NULL)
2268 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002269 nbdebug((" invalid buffer identifier in close\n"));
2270 EMSG("E648: invalid buffer identifier in close");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 return FAIL;
2272 }
2273
2274#ifdef NBDEBUG
2275 if (buf->displayname != NULL)
2276 name = buf->displayname;
2277#endif
Bram Moolenaarf2330482008-06-24 20:19:36 +00002278 if (buf->bufp == NULL)
2279 {
2280 nbdebug((" invalid buffer identifier in close\n"));
2281 /* This message was commented out, probably because it can
2282 * happen when shutting down. */
2283 if (p_verbose > 0)
2284 EMSG("E649: invalid buffer identifier in close");
2285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 nbdebug((" CLOSE %d: %s\n", bufno, name));
Bram Moolenaar67c53842010-05-22 18:28:27 +02002287#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 need_mouse_correct = TRUE;
Bram Moolenaar67c53842010-05-22 18:28:27 +02002289#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 if (buf->bufp != NULL)
2291 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD,
2292 buf->bufp->b_fnum, TRUE);
Bram Moolenaar8d602722006-08-08 19:34:19 +00002293 buf->bufp = NULL;
2294 buf->initDone = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 doupdate = 1;
2296/* =====================================================================*/
2297 }
2298 else if (streq((char *)cmd, "setStyle")) /* obsolete... */
2299 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002300 nbdebug((" setStyle is obsolete!\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301/* =====================================================================*/
2302 }
2303 else if (streq((char *)cmd, "setExitDelay"))
2304 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002305 /* Only used in version 2.1. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306/* =====================================================================*/
2307 }
2308 else if (streq((char *)cmd, "defineAnnoType"))
2309 {
2310#ifdef FEAT_SIGNS
2311 int typeNum;
2312 char_u *typeName;
2313 char_u *tooltip;
2314 char_u *p;
2315 char_u *glyphFile;
Bram Moolenaar67c53842010-05-22 18:28:27 +02002316 int parse_error = FALSE;
2317 char_u *fg;
2318 char_u *bg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319
2320 if (buf == NULL)
2321 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002322 nbdebug((" invalid buffer identifier in defineAnnoType\n"));
2323 EMSG("E650: invalid buffer identifier in defineAnnoType");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 return FAIL;
2325 }
2326
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002327 cp = (char *)args;
2328 typeNum = strtol(cp, &cp, 10);
2329 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 args = skipwhite(args);
2331 typeName = (char_u *)nb_unquote(args, &args);
2332 args = skipwhite(args + 1);
2333 tooltip = (char_u *)nb_unquote(args, &args);
2334 args = skipwhite(args + 1);
2335
2336 p = (char_u *)nb_unquote(args, &args);
2337 glyphFile = vim_strsave_escaped(p, escape_chars);
2338 vim_free(p);
2339
2340 args = skipwhite(args + 1);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002341 p = skiptowhite(args);
2342 if (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002344 *p = NUL;
2345 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02002347 fg = vim_strsave(args);
2348 bg = vim_strsave(p);
2349 if (STRLEN(fg) > MAX_COLOR_LENGTH || STRLEN(bg) > MAX_COLOR_LENGTH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002351 EMSG("E532: highlighting color name too long in defineAnnoType");
2352 vim_free(typeName);
2353 parse_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02002355 else if (typeName != NULL && tooltip != NULL && glyphFile != NULL)
2356 addsigntype(buf, typeNum, typeName, tooltip, glyphFile, fg, bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 else
2358 vim_free(typeName);
2359
2360 /* don't free typeName; it's used directly in addsigntype() */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002361 vim_free(fg);
2362 vim_free(bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 vim_free(tooltip);
2364 vim_free(glyphFile);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002365 if (parse_error)
2366 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367
2368#endif
2369/* =====================================================================*/
2370 }
2371 else if (streq((char *)cmd, "addAnno"))
2372 {
2373#ifdef FEAT_SIGNS
2374 int serNum;
2375 int localTypeNum;
2376 int typeNum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 pos_T *pos;
2378
2379 if (buf == NULL || buf->bufp == NULL)
2380 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002381 nbdebug((" invalid buffer identifier in addAnno\n"));
2382 EMSG("E651: invalid buffer identifier in addAnno");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 return FAIL;
2384 }
2385
2386 doupdate = 1;
2387
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002388 cp = (char *)args;
2389 serNum = strtol(cp, &cp, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390
2391 /* Get the typenr specific for this buffer and convert it to
2392 * the global typenumber, as used for the sign name. */
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002393 localTypeNum = strtol(cp, &cp, 10);
2394 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 typeNum = mapsigntype(buf, localTypeNum);
2396
2397 pos = get_off_or_lnum(buf->bufp, &args);
2398
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002399 cp = (char *)args;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002400 ignored = (int)strtol(cp, &cp, 10);
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002401 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402# ifdef NBDEBUG
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002403 if (ignored != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002405 nbdebug((" partial line annotation -- Not Yet Implemented!\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 }
2407# endif
2408 if (serNum >= GUARDEDOFFSET)
2409 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002410 nbdebug((" too many annotations! ignoring...\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 return FAIL;
2412 }
2413 if (pos)
2414 {
Bram Moolenaarec906222009-02-21 21:14:00 +00002415 coloncmd(":sign place %d line=%ld name=%d buffer=%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 serNum, pos->lnum, typeNum, buf->bufp->b_fnum);
2417 if (typeNum == curPCtype)
2418 coloncmd(":sign jump %d buffer=%d", serNum,
2419 buf->bufp->b_fnum);
2420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421#endif
2422/* =====================================================================*/
2423 }
2424 else if (streq((char *)cmd, "removeAnno"))
2425 {
2426#ifdef FEAT_SIGNS
2427 int serNum;
2428
2429 if (buf == NULL || buf->bufp == NULL)
2430 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002431 nbdebug((" invalid buffer identifier in removeAnno\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 return FAIL;
2433 }
2434 doupdate = 1;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002435 cp = (char *)args;
2436 serNum = strtol(cp, &cp, 10);
2437 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 coloncmd(":sign unplace %d buffer=%d",
2439 serNum, buf->bufp->b_fnum);
2440 redraw_buf_later(buf->bufp, NOT_VALID);
2441#endif
2442/* =====================================================================*/
2443 }
2444 else if (streq((char *)cmd, "moveAnnoToFront"))
2445 {
2446#ifdef FEAT_SIGNS
Bram Moolenaarf2330482008-06-24 20:19:36 +00002447 nbdebug((" moveAnnoToFront: Not Yet Implemented!\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448#endif
2449/* =====================================================================*/
2450 }
2451 else if (streq((char *)cmd, "guard") || streq((char *)cmd, "unguard"))
2452 {
2453 int len;
2454 pos_T first;
2455 pos_T last;
2456 pos_T *pos;
2457 int un = (cmd[0] == 'u');
2458 static int guardId = GUARDEDOFFSET;
2459
2460 if (skip >= SKIP_STOP)
2461 {
2462 nbdebug((" Skipping %s command\n", (char *) cmd));
2463 return OK;
2464 }
2465
2466 nb_init_graphics();
2467
2468 if (buf == NULL || buf->bufp == NULL)
2469 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002470 nbdebug((" invalid buffer identifier in %s command\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 return FAIL;
2472 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002473 nb_set_curbuf(buf->bufp);
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002474 cp = (char *)args;
2475 off = strtol(cp, &cp, 10);
2476 len = strtol(cp, NULL, 10);
2477 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 pos = off2pos(buf->bufp, off);
2479 doupdate = 1;
2480 if (!pos)
2481 nbdebug((" no such start pos in %s, %ld\n", cmd, off));
2482 else
2483 {
2484 first = *pos;
2485 pos = off2pos(buf->bufp, off + len - 1);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002486 if (pos != NULL && pos->col == 0)
2487 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 /*
2489 * In Java Swing the offset is a position between 2
2490 * characters. If col == 0 then we really want the
2491 * previous line as the end.
2492 */
2493 pos = off2pos(buf->bufp, off + len - 2);
2494 }
2495 if (!pos)
2496 nbdebug((" no such end pos in %s, %ld\n",
2497 cmd, off + len - 1));
2498 else
2499 {
2500 long lnum;
2501 last = *pos;
2502 /* set highlight for region */
2503 nbdebug((" %sGUARD %ld,%d to %ld,%d\n", (un) ? "UN" : "",
2504 first.lnum, first.col,
2505 last.lnum, last.col));
2506#ifdef FEAT_SIGNS
2507 for (lnum = first.lnum; lnum <= last.lnum; lnum++)
2508 {
2509 if (un)
2510 {
2511 /* never used */
2512 }
2513 else
2514 {
2515 if (buf_findsigntype_id(buf->bufp, lnum,
2516 GUARDED) == 0)
2517 {
2518 coloncmd(
Bram Moolenaarec906222009-02-21 21:14:00 +00002519 ":sign place %d line=%ld name=%d buffer=%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 guardId++, lnum, GUARDED,
2521 buf->bufp->b_fnum);
2522 }
2523 }
2524 }
2525#endif
2526 redraw_buf_later(buf->bufp, NOT_VALID);
2527 }
2528 }
2529/* =====================================================================*/
2530 }
2531 else if (streq((char *)cmd, "startAtomic"))
2532 {
2533 inAtomic = 1;
2534/* =====================================================================*/
2535 }
2536 else if (streq((char *)cmd, "endAtomic"))
2537 {
2538 inAtomic = 0;
2539 if (needupdate)
2540 {
2541 doupdate = 1;
2542 needupdate = 0;
2543 }
2544/* =====================================================================*/
2545 }
2546 else if (streq((char *)cmd, "save"))
2547 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002548 /*
2549 * NOTE - This command is obsolete wrt NetBeans. Its left in
2550 * only for historical reasons.
2551 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 if (buf == NULL || buf->bufp == NULL)
2553 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002554 nbdebug((" invalid buffer identifier in %s command\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 return FAIL;
2556 }
2557
2558 /* the following is taken from ex_cmds.c (do_wqall function) */
2559 if (bufIsChanged(buf->bufp))
2560 {
2561 /* Only write if the buffer can be written. */
2562 if (p_write
2563 && !buf->bufp->b_p_ro
2564 && buf->bufp->b_ffname != NULL
2565#ifdef FEAT_QUICKFIX
2566 && !bt_dontwrite(buf->bufp)
2567#endif
2568 )
2569 {
2570 buf_write_all(buf->bufp, FALSE);
2571#ifdef FEAT_AUTOCMD
2572 /* an autocommand may have deleted the buffer */
2573 if (!buf_valid(buf->bufp))
2574 buf->bufp = NULL;
2575#endif
2576 }
2577 }
Bram Moolenaarf2330482008-06-24 20:19:36 +00002578 else
2579 {
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002580 nbdebug((" Buffer has no changes!\n"));
Bram Moolenaarf2330482008-06-24 20:19:36 +00002581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582/* =====================================================================*/
2583 }
2584 else if (streq((char *)cmd, "netbeansBuffer"))
2585 {
2586 if (buf == NULL || buf->bufp == NULL)
2587 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002588 nbdebug((" invalid buffer identifier in %s command\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 return FAIL;
2590 }
2591 if (*args == 'T')
2592 {
2593 buf->bufp->b_netbeans_file = TRUE;
2594 buf->bufp->b_was_netbeans_file = TRUE;
2595 }
2596 else
2597 buf->bufp->b_netbeans_file = FALSE;
2598/* =====================================================================*/
2599 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002600 else if (streq((char *)cmd, "specialKeys"))
2601 {
2602 special_keys(args);
2603/* =====================================================================*/
2604 }
2605 else if (streq((char *)cmd, "actionMenuItem"))
2606 {
2607 /* not used yet */
2608/* =====================================================================*/
2609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 else if (streq((char *)cmd, "version"))
2611 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002612 /* not used yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 }
Bram Moolenaarf2330482008-06-24 20:19:36 +00002614 else
2615 {
2616 nbdebug(("Unrecognised command: %s\n", cmd));
2617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 /*
2619 * Unrecognized command is ignored.
2620 */
2621 }
2622 if (inAtomic && doupdate)
2623 {
2624 needupdate = 1;
2625 doupdate = 0;
2626 }
2627
Bram Moolenaar009b2592004-10-24 19:18:58 +00002628 /*
2629 * Is this needed? I moved the netbeans_Xt_connect() later during startup
2630 * and it may no longer be necessary. If its not needed then needupdate
2631 * and doupdate can also be removed.
2632 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 if (buf != NULL && buf->initDone && doupdate)
2634 {
2635 update_screen(NOT_VALID);
2636 setcursor();
2637 out_flush();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002638#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 gui_update_cursor(TRUE, FALSE);
2640 gui_mch_flush();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002641#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 /* Quit a hit-return or more prompt. */
2643 if (State == HITRETURN || State == ASKMORE)
2644 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645#ifdef FEAT_GUI_GTK
2646 if (gtk_main_level() > 0)
2647 gtk_main_quit();
2648#endif
2649 }
2650 }
2651
2652 return retval;
2653}
2654
2655
2656/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002657 * If "buf" is not the current buffer try changing to a window that edits this
2658 * buffer. If there is no such window then close the current buffer and set
2659 * the current buffer as "buf".
2660 */
2661 static void
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00002662nb_set_curbuf(buf_T *buf)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002663{
2664 if (curbuf != buf && buf_jump_open_win(buf) == NULL)
2665 set_curbuf(buf, DOBUF_GOTO);
2666}
2667
2668/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 * Process a vim colon command.
2670 */
2671 static void
2672coloncmd(char *cmd, ...)
2673{
2674 char buf[1024];
2675 va_list ap;
2676
2677 va_start(ap, cmd);
Bram Moolenaar0dc79e82009-06-24 14:50:12 +00002678 vim_vsnprintf(buf, sizeof(buf), cmd, ap, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 va_end(ap);
2680
2681 nbdebug((" COLONCMD %s\n", buf));
2682
2683/* ALT_INPUT_LOCK_ON; */
2684 do_cmdline((char_u *)buf, NULL, NULL, DOCMD_NOWAIT | DOCMD_KEYTYPED);
2685/* ALT_INPUT_LOCK_OFF; */
2686
2687 setcursor(); /* restore the cursor position */
2688 out_flush(); /* make sure output has been written */
2689
Bram Moolenaar67c53842010-05-22 18:28:27 +02002690#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 gui_update_cursor(TRUE, FALSE);
2692 gui_mch_flush();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002693#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694}
2695
2696
2697/*
Bram Moolenaar009b2592004-10-24 19:18:58 +00002698 * Parse the specialKeys argument and issue the appropriate map commands.
2699 */
2700 static void
2701special_keys(char_u *args)
2702{
2703 char *save_str = nb_unquote(args, NULL);
2704 char *tok = strtok(save_str, " ");
2705 char *sep;
2706 char keybuf[64];
2707 char cmdbuf[256];
2708
2709 while (tok != NULL)
2710 {
2711 int i = 0;
2712
2713 if ((sep = strchr(tok, '-')) != NULL)
2714 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002715 *sep = NUL;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002716 while (*tok)
2717 {
2718 switch (*tok)
2719 {
2720 case 'A':
2721 case 'M':
2722 case 'C':
2723 case 'S':
2724 keybuf[i++] = *tok;
2725 keybuf[i++] = '-';
2726 break;
2727 }
2728 tok++;
2729 }
2730 tok++;
2731 }
2732
2733 strcpy(&keybuf[i], tok);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002734 vim_snprintf(cmdbuf, sizeof(cmdbuf),
2735 "<silent><%s> :nbkey %s<CR>", keybuf, keybuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002736 do_map(0, (char_u *)cmdbuf, NORMAL, FALSE);
2737 tok = strtok(NULL, " ");
2738 }
2739 vim_free(save_str);
2740}
2741
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002742 void
2743ex_nbclose(eap)
2744 exarg_T *eap UNUSED;
2745{
2746 netbeans_close();
2747}
Bram Moolenaar009b2592004-10-24 19:18:58 +00002748
2749 void
2750ex_nbkey(eap)
2751 exarg_T *eap;
2752{
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002753 (void)netbeans_keystring(eap->arg);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002754}
2755
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002756 void
2757ex_nbstart(eap)
2758 exarg_T *eap;
2759{
2760 netbeans_open((char *)eap->arg, FALSE);
2761}
Bram Moolenaar009b2592004-10-24 19:18:58 +00002762
2763/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 * Initialize highlights and signs for use by netbeans (mostly obsolete)
2765 */
2766 static void
2767nb_init_graphics(void)
2768{
2769 static int did_init = FALSE;
2770
2771 if (!did_init)
2772 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002773 coloncmd(":highlight NBGuarded guibg=Cyan guifg=Black"
2774 " ctermbg=LightCyan ctermfg=Black");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 coloncmd(":sign define %d linehl=NBGuarded", GUARDED);
2776
2777 did_init = TRUE;
2778 }
2779}
2780
2781/*
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002782 * Convert key to netbeans name. This uses the global "mod_mask".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 */
2784 static void
2785netbeans_keyname(int key, char *buf)
2786{
2787 char *name = 0;
2788 char namebuf[2];
2789 int ctrl = 0;
2790 int shift = 0;
2791 int alt = 0;
2792
2793 if (mod_mask & MOD_MASK_CTRL)
2794 ctrl = 1;
2795 if (mod_mask & MOD_MASK_SHIFT)
2796 shift = 1;
2797 if (mod_mask & MOD_MASK_ALT)
2798 alt = 1;
2799
2800
2801 switch (key)
2802 {
2803 case K_F1: name = "F1"; break;
2804 case K_S_F1: name = "F1"; shift = 1; break;
2805 case K_F2: name = "F2"; break;
2806 case K_S_F2: name = "F2"; shift = 1; break;
2807 case K_F3: name = "F3"; break;
2808 case K_S_F3: name = "F3"; shift = 1; break;
2809 case K_F4: name = "F4"; break;
2810 case K_S_F4: name = "F4"; shift = 1; break;
2811 case K_F5: name = "F5"; break;
2812 case K_S_F5: name = "F5"; shift = 1; break;
2813 case K_F6: name = "F6"; break;
2814 case K_S_F6: name = "F6"; shift = 1; break;
2815 case K_F7: name = "F7"; break;
2816 case K_S_F7: name = "F7"; shift = 1; break;
2817 case K_F8: name = "F8"; break;
2818 case K_S_F8: name = "F8"; shift = 1; break;
2819 case K_F9: name = "F9"; break;
2820 case K_S_F9: name = "F9"; shift = 1; break;
2821 case K_F10: name = "F10"; break;
2822 case K_S_F10: name = "F10"; shift = 1; break;
2823 case K_F11: name = "F11"; break;
2824 case K_S_F11: name = "F11"; shift = 1; break;
2825 case K_F12: name = "F12"; break;
2826 case K_S_F12: name = "F12"; shift = 1; break;
2827 default:
2828 if (key >= ' ' && key <= '~')
2829 {
2830 /* Allow ASCII characters. */
2831 name = namebuf;
2832 namebuf[0] = key;
2833 namebuf[1] = NUL;
2834 }
2835 else
2836 name = "X";
2837 break;
2838 }
2839
2840 buf[0] = '\0';
2841 if (ctrl)
2842 strcat(buf, "C");
2843 if (shift)
2844 strcat(buf, "S");
2845 if (alt)
2846 strcat(buf, "M"); /* META */
2847 if (ctrl || shift || alt)
2848 strcat(buf, "-");
2849 strcat(buf, name);
2850}
2851
Bram Moolenaar67c53842010-05-22 18:28:27 +02002852#if defined(FEAT_BEVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853/*
2854 * Function to be called for balloon evaluation. Grabs the text under the
2855 * cursor and sends it to the debugger for evaluation. The debugger should
2856 * respond with a showBalloon command when there is a useful result.
2857 */
Bram Moolenaard62bec82005-03-07 22:56:57 +00002858 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859netbeans_beval_cb(
2860 BalloonEval *beval,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002861 int state UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862{
Bram Moolenaard62bec82005-03-07 22:56:57 +00002863 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 char_u *text;
Bram Moolenaard62bec82005-03-07 22:56:57 +00002865 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 int col;
2867 char buf[MAXPATHL * 2 + 25];
2868 char_u *p;
2869
2870 /* Don't do anything when 'ballooneval' is off, messages scrolled the
2871 * windows up or we have no connection. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002872 if (!p_beval || msg_scrolled > 0 || !NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 return;
2874
Bram Moolenaard62bec82005-03-07 22:56:57 +00002875 if (get_beval_info(beval, TRUE, &wp, &lnum, &text, &col) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 {
2877 /* Send debugger request. Only when the text is of reasonable
2878 * length. */
2879 if (text != NULL && text[0] != NUL && STRLEN(text) < MAXPATHL)
2880 {
2881 p = nb_quote(text);
2882 if (p != NULL)
Bram Moolenaar009b2592004-10-24 19:18:58 +00002883 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002884 vim_snprintf(buf, sizeof(buf),
Bram Moolenaar89d40322006-08-29 15:30:07 +00002885 "0:balloonText=%d \"%s\"\n", r_cmdno, p);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002886 vim_free(p);
2887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 nbdebug(("EVT: %s", buf));
2889 nb_send(buf, "netbeans_beval_cb");
2890 }
2891 vim_free(text);
2892 }
2893}
2894#endif
2895
2896/*
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002897 * Return TRUE when the netbeans connection is closed.
2898 */
2899 int
2900netbeans_active(void)
2901{
2902 return NETBEANS_OPEN;
2903}
2904
2905/*
Bram Moolenaar67c53842010-05-22 18:28:27 +02002906 * Return netbeans file descriptor.
2907 */
2908 int
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002909netbeans_filedesc(void)
Bram Moolenaar67c53842010-05-22 18:28:27 +02002910{
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002911 return nbsock;
Bram Moolenaar67c53842010-05-22 18:28:27 +02002912}
2913
2914#if defined(FEAT_GUI) || defined(PROTO)
2915/*
2916 * Register our file descriptor with the gui event handling system.
2917 */
2918 void
2919netbeans_gui_register(void)
2920{
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002921 if (!NB_HAS_GUI || !NETBEANS_OPEN)
Bram Moolenaar67c53842010-05-22 18:28:27 +02002922 return;
2923
Bram Moolenaar67c53842010-05-22 18:28:27 +02002924# ifdef FEAT_GUI_MOTIF
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002925 /* tell notifier we are interested in being called
2926 * when there is input on the editor connection socket
2927 */
2928 if (inputHandler == (XtInputId)NULL)
2929 inputHandler = XtAppAddInput((XtAppContext)app_context, nbsock,
2930 (XtPointer)(XtInputReadMask + XtInputExceptMask),
2931 messageFromNetbeans, NULL);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002932# else
2933# ifdef FEAT_GUI_GTK
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002934 /*
2935 * Tell gdk we are interested in being called when there
2936 * is input on the editor connection socket
2937 */
2938 if (inputHandler == 0)
2939 inputHandler = gdk_input_add((gint)nbsock, (GdkInputCondition)
2940 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
2941 messageFromNetbeans, NULL);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002942# else
2943# ifdef FEAT_GUI_W32
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002944 /*
2945 * Tell Windows we are interested in receiving message when there
2946 * is input on the editor connection socket
2947 */
2948 if (inputHandler == -1)
2949 inputHandler = WSAAsyncSelect(nbsock, s_hwnd, WM_NETBEANS, FD_READ);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002950# endif
2951# endif
2952# endif
Bram Moolenaar67c53842010-05-22 18:28:27 +02002953
2954# ifdef FEAT_BEVAL
2955 bevalServers |= BEVAL_NETBEANS;
2956# endif
2957}
2958#endif
2959
2960/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 * Tell netbeans that the window was opened, ready for commands.
2962 */
2963 void
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002964netbeans_open(char *params, int abort)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965{
2966 char *cmd = "0:startupDone=0\n";
2967
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002968 if (NETBEANS_OPEN)
2969 {
2970 EMSG(_("E511: netbeans already connected"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002974 if (netbeans_connect(params, abort) != OK)
Bram Moolenaar67c53842010-05-22 18:28:27 +02002975 return;
2976#ifdef FEAT_GUI
2977 netbeans_gui_register();
Bram Moolenaard62bec82005-03-07 22:56:57 +00002978#endif
2979
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 nbdebug(("EVT: %s", cmd));
2981 nb_send(cmd, "netbeans_startup_done");
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002982
2983 /* update the screen after having added the gutter */
2984 changed_window_setting();
2985 update_screen(CLEAR);
2986 setcursor();
2987 out_flush();
2988#ifdef FEAT_GUI
2989 gui_update_cursor(TRUE, FALSE);
2990 gui_mch_flush();
2991#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992}
2993
Bram Moolenaar009b2592004-10-24 19:18:58 +00002994/*
2995 * Tell netbeans that we're exiting. This should be called right
2996 * before calling exit.
2997 */
2998 void
2999netbeans_send_disconnect()
3000{
3001 char buf[128];
3002
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003003 if (NETBEANS_OPEN)
Bram Moolenaar009b2592004-10-24 19:18:58 +00003004 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00003005 sprintf(buf, "0:disconnect=%d\n", r_cmdno);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003006 nbdebug(("EVT: %s", buf));
3007 nb_send(buf, "netbeans_disconnect");
3008 }
3009}
3010
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_W32) || defined(PROTO)
3012/*
3013 * Tell netbeans that the window was moved or resized.
3014 */
3015 void
3016netbeans_frame_moved(int new_x, int new_y)
3017{
3018 char buf[128];
3019
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003020 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 return;
3022
3023 sprintf(buf, "0:geometry=%d %d %d %d %d\n",
Bram Moolenaar89d40322006-08-29 15:30:07 +00003024 r_cmdno, (int)Columns, (int)Rows, new_x, new_y);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003025 /*nbdebug(("EVT: %s", buf)); happens too many times during a move */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 nb_send(buf, "netbeans_frame_moved");
3027}
3028#endif
3029
3030/*
Bram Moolenaar009b2592004-10-24 19:18:58 +00003031 * Tell netbeans the user opened or activated a file.
3032 */
3033 void
3034netbeans_file_activated(buf_T *bufp)
3035{
3036 int bufno = nb_getbufno(bufp);
3037 nbbuf_T *bp = nb_get_buf(bufno);
3038 char buffer[2*MAXPATHL];
3039 char_u *q;
3040
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003041 if (!NETBEANS_OPEN || !bufp->b_netbeans_file || dosetvisible)
Bram Moolenaar009b2592004-10-24 19:18:58 +00003042 return;
3043
3044 q = nb_quote(bufp->b_ffname);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003045 if (q == NULL || bp == NULL)
Bram Moolenaar009b2592004-10-24 19:18:58 +00003046 return;
3047
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003048 vim_snprintf(buffer, sizeof(buffer), "%d:fileOpened=%d \"%s\" %s %s\n",
Bram Moolenaar009b2592004-10-24 19:18:58 +00003049 bufno,
3050 bufno,
3051 (char *)q,
3052 "T", /* open in NetBeans */
3053 "F"); /* modified */
3054
3055 vim_free(q);
3056 nbdebug(("EVT: %s", buffer));
3057
3058 nb_send(buffer, "netbeans_file_opened");
3059}
3060
3061/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 * Tell netbeans the user opened a file.
3063 */
3064 void
Bram Moolenaar009b2592004-10-24 19:18:58 +00003065netbeans_file_opened(buf_T *bufp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066{
Bram Moolenaar009b2592004-10-24 19:18:58 +00003067 int bufno = nb_getbufno(bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 char buffer[2*MAXPATHL];
3069 char_u *q;
Bram Moolenaar009b2592004-10-24 19:18:58 +00003070 nbbuf_T *bp = nb_get_buf(nb_getbufno(bufp));
3071 int bnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003073 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 return;
3075
Bram Moolenaar009b2592004-10-24 19:18:58 +00003076 q = nb_quote(bufp->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 if (q == NULL)
3078 return;
Bram Moolenaar009b2592004-10-24 19:18:58 +00003079 if (bp != NULL)
3080 bnum = bufno;
3081 else
3082 bnum = 0;
3083
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003084 vim_snprintf(buffer, sizeof(buffer), "%d:fileOpened=%d \"%s\" %s %s\n",
Bram Moolenaar009b2592004-10-24 19:18:58 +00003085 bnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 0,
3087 (char *)q,
3088 "T", /* open in NetBeans */
3089 "F"); /* modified */
3090
3091 vim_free(q);
3092 nbdebug(("EVT: %s", buffer));
3093
3094 nb_send(buffer, "netbeans_file_opened");
Bram Moolenaar009b2592004-10-24 19:18:58 +00003095 if (p_acd && vim_chdirfile(bufp->b_ffname) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 shorten_fnames(TRUE);
3097}
3098
3099/*
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00003100 * Tell netbeans that a file was deleted or wiped out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 */
3102 void
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00003103netbeans_file_killed(buf_T *bufp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104{
3105 int bufno = nb_getbufno(bufp);
3106 nbbuf_T *nbbuf = nb_get_buf(bufno);
3107 char buffer[2*MAXPATHL];
3108
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003109 if (!NETBEANS_OPEN || bufno == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 return;
3111
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00003112 nbdebug(("netbeans_file_killed:\n"));
3113 nbdebug((" Killing bufno: %d", bufno));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114
Bram Moolenaar89d40322006-08-29 15:30:07 +00003115 sprintf(buffer, "%d:killed=%d\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116
3117 nbdebug(("EVT: %s", buffer));
3118
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00003119 nb_send(buffer, "netbeans_file_killed");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120
3121 if (nbbuf != NULL)
3122 nbbuf->bufp = NULL;
3123}
3124
3125/*
3126 * Get a pointer to the Netbeans buffer for Vim buffer "bufp".
3127 * Return NULL if there is no such buffer or changes are not to be reported.
3128 * Otherwise store the buffer number in "*bufnop".
3129 */
3130 static nbbuf_T *
3131nb_bufp2nbbuf_fire(buf_T *bufp, int *bufnop)
3132{
3133 int bufno;
3134 nbbuf_T *nbbuf;
3135
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003136 if (!NETBEANS_OPEN || !netbeansFireChanges)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 return NULL; /* changes are not reported at all */
3138
3139 bufno = nb_getbufno(bufp);
3140 if (bufno <= 0)
3141 return NULL; /* file is not known to NetBeans */
3142
3143 nbbuf = nb_get_buf(bufno);
3144 if (nbbuf != NULL && !nbbuf->fireChanges)
3145 return NULL; /* changes in this buffer are not reported */
3146
3147 *bufnop = bufno;
3148 return nbbuf;
3149}
3150
3151/*
3152 * Tell netbeans the user inserted some text.
3153 */
3154 void
3155netbeans_inserted(
3156 buf_T *bufp,
3157 linenr_T linenr,
3158 colnr_T col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 char_u *txt,
3160 int newlen)
3161{
3162 char_u *buf;
3163 int bufno;
3164 nbbuf_T *nbbuf;
3165 pos_T pos;
3166 long off;
3167 char_u *p;
3168 char_u *newtxt;
3169
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003170 if (!NETBEANS_OPEN)
3171 return;
3172
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3174 if (nbbuf == NULL)
3175 return;
3176
Bram Moolenaar009b2592004-10-24 19:18:58 +00003177 /* Don't mark as modified for initial read */
3178 if (nbbuf->insertDone)
3179 nbbuf->modified = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180
3181 pos.lnum = linenr;
3182 pos.col = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 off = pos2off(bufp, &pos);
3184
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 /* send the "insert" EVT */
3186 newtxt = alloc(newlen + 1);
Bram Moolenaarb6356332005-07-18 21:40:44 +00003187 vim_strncpy(newtxt, txt, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 p = nb_quote(newtxt);
3189 if (p != NULL)
3190 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00003191 buf = alloc(128 + 2*newlen);
Bram Moolenaar89d40322006-08-29 15:30:07 +00003192 sprintf((char *)buf, "%d:insert=%d %ld \"%s\"\n",
3193 bufno, r_cmdno, off, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 nbdebug(("EVT: %s", buf));
3195 nb_send((char *)buf, "netbeans_inserted");
Bram Moolenaar009b2592004-10-24 19:18:58 +00003196 vim_free(p);
3197 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 vim_free(newtxt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200}
3201
3202/*
3203 * Tell netbeans some bytes have been removed.
3204 */
3205 void
3206netbeans_removed(
3207 buf_T *bufp,
3208 linenr_T linenr,
3209 colnr_T col,
3210 long len)
3211{
3212 char_u buf[128];
3213 int bufno;
3214 nbbuf_T *nbbuf;
3215 pos_T pos;
3216 long off;
3217
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003218 if (!NETBEANS_OPEN)
3219 return;
3220
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3222 if (nbbuf == NULL)
3223 return;
3224
3225 if (len < 0)
3226 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00003227 nbdebug(("Negative len %ld in netbeans_removed()!\n", len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 return;
3229 }
3230
3231 nbbuf->modified = 1;
3232
3233 pos.lnum = linenr;
3234 pos.col = col;
3235
3236 off = pos2off(bufp, &pos);
3237
Bram Moolenaar89d40322006-08-29 15:30:07 +00003238 sprintf((char *)buf, "%d:remove=%d %ld %ld\n", bufno, r_cmdno, off, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 nbdebug(("EVT: %s", buf));
3240 nb_send((char *)buf, "netbeans_removed");
3241}
3242
3243/*
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003244 * Send netbeans an unmodified command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003247netbeans_unmodified(buf_T *bufp UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248{
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003249 if (!NETBEANS_OPEN)
3250 return;
3251
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252#if 0
3253 char_u buf[128];
3254 int bufno;
3255 nbbuf_T *nbbuf;
3256
3257 /* This has been disabled, because NetBeans considers a buffer modified
3258 * even when all changes have been undone. */
3259 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3260 if (nbbuf == NULL)
3261 return;
3262
3263 nbbuf->modified = 0;
3264
Bram Moolenaar89d40322006-08-29 15:30:07 +00003265 sprintf((char *)buf, "%d:unmodified=%d\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 nbdebug(("EVT: %s", buf));
3267 nb_send((char *)buf, "netbeans_unmodified");
3268#endif
3269}
3270
3271/*
3272 * Send a button release event back to netbeans. Its up to netbeans
3273 * to decide what to do (if anything) with this event.
3274 */
3275 void
3276netbeans_button_release(int button)
3277{
3278 char buf[128];
3279 int bufno;
3280
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003281 if (!NETBEANS_OPEN)
3282 return;
3283
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 bufno = nb_getbufno(curbuf);
3285
3286 if (bufno >= 0 && curwin != NULL && curwin->w_buffer == curbuf)
3287 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003288 int col = mouse_col - W_WINCOL(curwin)
Bram Moolenaar67c53842010-05-22 18:28:27 +02003289 - ((curwin->w_p_nu || curwin->w_p_rnu) ? 9 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 long off = pos2off(curbuf, &curwin->w_cursor);
3291
3292 /* sync the cursor position */
Bram Moolenaar89d40322006-08-29 15:30:07 +00003293 sprintf(buf, "%d:newDotAndMark=%d %ld %ld\n", bufno, r_cmdno, off, off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 nbdebug(("EVT: %s", buf));
3295 nb_send(buf, "netbeans_button_release[newDotAndMark]");
3296
Bram Moolenaar89d40322006-08-29 15:30:07 +00003297 sprintf(buf, "%d:buttonRelease=%d %d %ld %d\n", bufno, r_cmdno,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 button, (long)curwin->w_cursor.lnum, col);
3299 nbdebug(("EVT: %s", buf));
3300 nb_send(buf, "netbeans_button_release");
3301 }
3302}
3303
3304
3305/*
Bram Moolenaar143c38c2007-05-10 16:41:10 +00003306 * Send a keypress event back to netbeans. This usually simulates some
Bram Moolenaar009b2592004-10-24 19:18:58 +00003307 * kind of function key press. This function operates on a key code.
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003308 * Return TRUE when the key was sent, FALSE when the command has been
3309 * postponed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 */
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003311 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312netbeans_keycommand(int key)
3313{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 char keyName[60];
Bram Moolenaar009b2592004-10-24 19:18:58 +00003315
3316 netbeans_keyname(key, keyName);
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003317 return netbeans_keystring((char_u *)keyName);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003318}
3319
3320
3321/*
Bram Moolenaar143c38c2007-05-10 16:41:10 +00003322 * Send a keypress event back to netbeans. This usually simulates some
Bram Moolenaar009b2592004-10-24 19:18:58 +00003323 * kind of function key press. This function operates on a key string.
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003324 * Return TRUE when the key was sent, FALSE when the command has been
3325 * postponed.
Bram Moolenaar009b2592004-10-24 19:18:58 +00003326 */
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003327 static int
3328netbeans_keystring(char_u *keyName)
Bram Moolenaar009b2592004-10-24 19:18:58 +00003329{
3330 char buf[2*MAXPATHL];
3331 int bufno = nb_getbufno(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 long off;
3333 char_u *q;
3334
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003335 if (!NETBEANS_OPEN)
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003336 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 if (bufno == -1)
3339 {
3340 nbdebug(("got keycommand for non-NetBeans buffer, opening...\n"));
3341 q = curbuf->b_ffname == NULL ? (char_u *)""
3342 : nb_quote(curbuf->b_ffname);
3343 if (q == NULL)
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003344 return TRUE;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003345 vim_snprintf(buf, sizeof(buf), "0:fileOpened=%d \"%s\" %s %s\n", 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 q,
3347 "T", /* open in NetBeans */
3348 "F"); /* modified */
3349 if (curbuf->b_ffname != NULL)
3350 vim_free(q);
3351 nbdebug(("EVT: %s", buf));
3352 nb_send(buf, "netbeans_keycommand");
3353
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003354 postpone_keycommand(keyName);
3355 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 }
3357
3358 /* sync the cursor position */
3359 off = pos2off(curbuf, &curwin->w_cursor);
Bram Moolenaar89d40322006-08-29 15:30:07 +00003360 sprintf(buf, "%d:newDotAndMark=%d %ld %ld\n", bufno, r_cmdno, off, off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 nbdebug(("EVT: %s", buf));
3362 nb_send(buf, "netbeans_keycommand");
3363
3364 /* To work on Win32 you must apply patch to ExtEditor module
3365 * from ExtEdCaret.java.diff - make EVT_newDotAndMark handler
3366 * more synchronous
3367 */
3368
3369 /* now send keyCommand event */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003370 vim_snprintf(buf, sizeof(buf), "%d:keyCommand=%d \"%s\"\n",
Bram Moolenaar89d40322006-08-29 15:30:07 +00003371 bufno, r_cmdno, keyName);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 nbdebug(("EVT: %s", buf));
3373 nb_send(buf, "netbeans_keycommand");
3374
3375 /* New: do both at once and include the lnum/col. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003376 vim_snprintf(buf, sizeof(buf), "%d:keyAtPos=%d \"%s\" %ld %ld/%ld\n",
Bram Moolenaar89d40322006-08-29 15:30:07 +00003377 bufno, r_cmdno, keyName,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 off, (long)curwin->w_cursor.lnum, (long)curwin->w_cursor.col);
3379 nbdebug(("EVT: %s", buf));
3380 nb_send(buf, "netbeans_keycommand");
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003381 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382}
3383
3384
3385/*
3386 * Send a save event to netbeans.
3387 */
3388 void
3389netbeans_save_buffer(buf_T *bufp)
3390{
3391 char_u buf[64];
3392 int bufno;
3393 nbbuf_T *nbbuf;
3394
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003395 if (!NETBEANS_OPEN)
3396 return;
3397
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3399 if (nbbuf == NULL)
3400 return;
3401
3402 nbbuf->modified = 0;
3403
Bram Moolenaar89d40322006-08-29 15:30:07 +00003404 sprintf((char *)buf, "%d:save=%d\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 nbdebug(("EVT: %s", buf));
3406 nb_send((char *)buf, "netbeans_save_buffer");
3407}
3408
3409
3410/*
3411 * Send remove command to netbeans (this command has been turned off).
3412 */
3413 void
3414netbeans_deleted_all_lines(buf_T *bufp)
3415{
3416 char_u buf[64];
3417 int bufno;
3418 nbbuf_T *nbbuf;
3419
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003420 if (!NETBEANS_OPEN)
3421 return;
3422
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3424 if (nbbuf == NULL)
3425 return;
3426
Bram Moolenaar009b2592004-10-24 19:18:58 +00003427 /* Don't mark as modified for initial read */
3428 if (nbbuf->insertDone)
3429 nbbuf->modified = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430
Bram Moolenaar89d40322006-08-29 15:30:07 +00003431 sprintf((char *)buf, "%d:remove=%d 0 -1\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 nbdebug(("EVT(suppressed): %s", buf));
3433/* nb_send(buf, "netbeans_deleted_all_lines"); */
3434}
3435
3436
3437/*
3438 * See if the lines are guarded. The top and bot parameters are from
3439 * u_savecommon(), these are the line above the change and the line below the
3440 * change.
3441 */
3442 int
3443netbeans_is_guarded(linenr_T top, linenr_T bot)
3444{
3445 signlist_T *p;
3446 int lnum;
3447
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003448 if (!NETBEANS_OPEN)
3449 return FALSE;
3450
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 for (p = curbuf->b_signlist; p != NULL; p = p->next)
3452 if (p->id >= GUARDEDOFFSET)
3453 for (lnum = top + 1; lnum < bot; lnum++)
3454 if (lnum == p->lnum)
3455 return TRUE;
3456
3457 return FALSE;
3458}
3459
3460#if defined(FEAT_GUI_MOTIF) || defined(PROTO)
3461/*
3462 * We have multiple signs to draw at the same location. Draw the
3463 * multi-sign indicator instead. This is the Motif version.
3464 */
3465 void
3466netbeans_draw_multisign_indicator(int row)
3467{
3468 int i;
3469 int y;
3470 int x;
3471
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003472 if (!NETBEANS_OPEN)
3473 return;
3474
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 x = 0;
3476 y = row * gui.char_height + 2;
3477
3478 for (i = 0; i < gui.char_height - 3; i++)
3479 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y++);
3480
3481 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+0, y);
3482 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3483 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+4, y++);
3484 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+1, y);
3485 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3486 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+3, y++);
3487 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3488}
3489#endif /* FEAT_GUI_MOTIF */
3490
3491#ifdef FEAT_GUI_GTK
3492/*
3493 * We have multiple signs to draw at the same location. Draw the
3494 * multi-sign indicator instead. This is the GTK/Gnome version.
3495 */
3496 void
3497netbeans_draw_multisign_indicator(int row)
3498{
3499 int i;
3500 int y;
3501 int x;
3502 GdkDrawable *drawable = gui.drawarea->window;
3503
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003504 if (!NETBEANS_OPEN)
3505 return;
3506
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 x = 0;
3508 y = row * gui.char_height + 2;
3509
3510 for (i = 0; i < gui.char_height - 3; i++)
3511 gdk_draw_point(drawable, gui.text_gc, x+2, y++);
3512
3513 gdk_draw_point(drawable, gui.text_gc, x+0, y);
3514 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3515 gdk_draw_point(drawable, gui.text_gc, x+4, y++);
3516 gdk_draw_point(drawable, gui.text_gc, x+1, y);
3517 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3518 gdk_draw_point(drawable, gui.text_gc, x+3, y++);
3519 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3520}
3521#endif /* FEAT_GUI_GTK */
3522
3523/*
3524 * If the mouse is clicked in the gutter of a line with multiple
3525 * annotations, cycle through the set of signs.
3526 */
3527 void
3528netbeans_gutter_click(linenr_T lnum)
3529{
3530 signlist_T *p;
3531
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003532 if (!NETBEANS_OPEN)
3533 return;
3534
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 for (p = curbuf->b_signlist; p != NULL; p = p->next)
3536 {
3537 if (p->lnum == lnum && p->next && p->next->lnum == lnum)
3538 {
3539 signlist_T *tail;
3540
3541 /* remove "p" from list, reinsert it at the tail of the sublist */
3542 if (p->prev)
3543 p->prev->next = p->next;
3544 else
3545 curbuf->b_signlist = p->next;
3546 p->next->prev = p->prev;
3547 /* now find end of sublist and insert p */
3548 for (tail = p->next;
3549 tail->next && tail->next->lnum == lnum
3550 && tail->next->id < GUARDEDOFFSET;
3551 tail = tail->next)
3552 ;
3553 /* tail now points to last entry with same lnum (except
3554 * that "guarded" annotations are always last) */
3555 p->next = tail->next;
3556 if (tail->next)
3557 tail->next->prev = p;
3558 p->prev = tail;
3559 tail->next = p;
3560 update_debug_sign(curbuf, lnum);
3561 break;
3562 }
3563 }
3564}
3565
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566/*
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003567 * Add a sign of the requested type at the requested location.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 *
3569 * Reverse engineering:
3570 * Apparently an annotation is defined the first time it is used in a buffer.
3571 * When the same annotation is used in two buffers, the second time we do not
3572 * need to define a new sign name but reuse the existing one. But since the
3573 * ID number used in the second buffer starts counting at one again, a mapping
3574 * is made from the ID specifically for the buffer to the global sign name
3575 * (which is a number).
3576 *
3577 * globalsignmap[] stores the signs that have been defined globally.
3578 * buf->signmapused[] maps buffer-local annotation IDs to an index in
3579 * globalsignmap[].
3580 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 static void
3582addsigntype(
3583 nbbuf_T *buf,
3584 int typeNum,
3585 char_u *typeName,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003586 char_u *tooltip UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 char_u *glyphFile,
Bram Moolenaar67c53842010-05-22 18:28:27 +02003588 char_u *fg,
3589 char_u *bg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 int i, j;
Bram Moolenaar67c53842010-05-22 18:28:27 +02003592 int use_fg = (*fg && STRCMP(fg, "none") != 0);
3593 int use_bg = (*bg && STRCMP(bg, "none") != 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594
3595 for (i = 0; i < globalsignmapused; i++)
3596 if (STRCMP(typeName, globalsignmap[i]) == 0)
3597 break;
3598
3599 if (i == globalsignmapused) /* not found; add it to global map */
3600 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003601 nbdebug(("DEFINEANNOTYPE(%d,%s,%s,%s,%s,%s)\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 typeNum, typeName, tooltip, glyphFile, fg, bg));
3603 if (use_fg || use_bg)
3604 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003605 char fgbuf[2 * (8 + MAX_COLOR_LENGTH) + 1];
3606 char bgbuf[2 * (8 + MAX_COLOR_LENGTH) + 1];
3607 char *ptr;
3608 int value;
3609
3610 value = strtol((char *)fg, &ptr, 10);
3611 if (ptr != (char *)fg)
3612 sprintf(fgbuf, "guifg=#%06x", value & 0xFFFFFF);
3613 else
3614 sprintf(fgbuf, "guifg=%s ctermfg=%s", fg, fg);
3615
3616 value = strtol((char *)bg, &ptr, 10);
3617 if (ptr != (char *)bg)
3618 sprintf(bgbuf, "guibg=#%06x", value & 0xFFFFFF);
3619 else
3620 sprintf(bgbuf, "guibg=%s ctermbg=%s", bg, bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621
3622 coloncmd(":highlight NB_%s %s %s", typeName, (use_fg) ? fgbuf : "",
3623 (use_bg) ? bgbuf : "");
3624 if (*glyphFile == NUL)
3625 /* no glyph, line highlighting only */
3626 coloncmd(":sign define %d linehl=NB_%s", i + 1, typeName);
3627 else if (vim_strsize(glyphFile) <= 2)
3628 /* one- or two-character glyph name, use as text glyph with
3629 * texthl */
3630 coloncmd(":sign define %d text=%s texthl=NB_%s", i + 1,
3631 glyphFile, typeName);
3632 else
3633 /* glyph, line highlighting */
3634 coloncmd(":sign define %d icon=%s linehl=NB_%s", i + 1,
3635 glyphFile, typeName);
3636 }
3637 else
3638 /* glyph, no line highlighting */
3639 coloncmd(":sign define %d icon=%s", i + 1, glyphFile);
3640
3641 if (STRCMP(typeName,"CurrentPC") == 0)
3642 curPCtype = typeNum;
3643
3644 if (globalsignmapused == globalsignmaplen)
3645 {
3646 if (globalsignmaplen == 0) /* first allocation */
3647 {
3648 globalsignmaplen = 20;
3649 globalsignmap = (char **)alloc_clear(globalsignmaplen*sizeof(char *));
3650 }
3651 else /* grow it */
3652 {
3653 int incr;
3654 int oldlen = globalsignmaplen;
3655
3656 globalsignmaplen *= 2;
3657 incr = globalsignmaplen - oldlen;
3658 globalsignmap = (char **)vim_realloc(globalsignmap,
3659 globalsignmaplen * sizeof(char *));
3660 memset(globalsignmap + oldlen, 0, incr * sizeof(char *));
3661 }
3662 }
3663
3664 globalsignmap[i] = (char *)typeName;
3665 globalsignmapused = i + 1;
3666 }
3667
3668 /* check local map; should *not* be found! */
3669 for (j = 0; j < buf->signmapused; j++)
3670 if (buf->signmap[j] == i + 1)
3671 return;
3672
3673 /* add to local map */
3674 if (buf->signmapused == buf->signmaplen)
3675 {
3676 if (buf->signmaplen == 0) /* first allocation */
3677 {
3678 buf->signmaplen = 5;
3679 buf->signmap = (int *)alloc_clear(buf->signmaplen * sizeof(int *));
3680 }
3681 else /* grow it */
3682 {
3683 int incr;
3684 int oldlen = buf->signmaplen;
3685 buf->signmaplen *= 2;
3686 incr = buf->signmaplen - oldlen;
3687 buf->signmap = (int *)vim_realloc(buf->signmap,
3688 buf->signmaplen*sizeof(int *));
3689 memset(buf->signmap + oldlen, 0, incr * sizeof(int *));
3690 }
3691 }
3692
3693 buf->signmap[buf->signmapused++] = i + 1;
3694
3695}
3696
3697
3698/*
3699 * See if we have the requested sign type in the buffer.
3700 */
3701 static int
3702mapsigntype(nbbuf_T *buf, int localsigntype)
3703{
3704 if (--localsigntype >= 0 && localsigntype < buf->signmapused)
3705 return buf->signmap[localsigntype];
3706
3707 return 0;
3708}
3709
3710
3711/*
3712 * Compute length of buffer, don't print anything.
3713 */
3714 static long
3715get_buf_size(buf_T *bufp)
3716{
3717 linenr_T lnum;
3718 long char_count = 0;
3719 int eol_size;
3720 long last_check = 100000L;
3721
3722 if (bufp->b_ml.ml_flags & ML_EMPTY)
3723 return 0;
3724 else
3725 {
3726 if (get_fileformat(bufp) == EOL_DOS)
3727 eol_size = 2;
3728 else
3729 eol_size = 1;
3730 for (lnum = 1; lnum <= bufp->b_ml.ml_line_count; ++lnum)
3731 {
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00003732 char_count += (long)STRLEN(ml_get_buf(bufp, lnum, FALSE))
3733 + eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 /* Check for a CTRL-C every 100000 characters */
3735 if (char_count > last_check)
3736 {
3737 ui_breakcheck();
3738 if (got_int)
3739 return char_count;
3740 last_check = char_count + 100000L;
3741 }
3742 }
3743 /* Correction for when last line doesn't have an EOL. */
3744 if (!bufp->b_p_eol && bufp->b_p_bin)
3745 char_count -= eol_size;
3746 }
3747
3748 return char_count;
3749}
3750
3751/*
3752 * Convert character offset to lnum,col
3753 */
3754 static pos_T *
3755off2pos(buf_T *buf, long offset)
3756{
3757 linenr_T lnum;
3758 static pos_T pos;
3759
3760 pos.lnum = 0;
3761 pos.col = 0;
3762#ifdef FEAT_VIRTUALEDIT
3763 pos.coladd = 0;
3764#endif
3765
3766 if (!(buf->b_ml.ml_flags & ML_EMPTY))
3767 {
3768 if ((lnum = ml_find_line_or_offset(buf, (linenr_T)0, &offset)) < 0)
3769 return NULL;
3770 pos.lnum = lnum;
3771 pos.col = offset;
3772 }
3773
3774 return &pos;
3775}
3776
3777/*
3778 * Convert an argument in the form "1234" to an offset and compute the
3779 * lnum/col from it. Convert an argument in the form "123/12" directly to a
3780 * lnum/col.
3781 * "argp" is advanced to after the argument.
3782 * Return a pointer to the position, NULL if something is wrong.
3783 */
3784 static pos_T *
3785get_off_or_lnum(buf_T *buf, char_u **argp)
3786{
3787 static pos_T mypos;
3788 long off;
3789
3790 off = strtol((char *)*argp, (char **)argp, 10);
3791 if (**argp == '/')
3792 {
3793 mypos.lnum = (linenr_T)off;
3794 ++*argp;
3795 mypos.col = strtol((char *)*argp, (char **)argp, 10);
3796#ifdef FEAT_VIRTUALEDIT
3797 mypos.coladd = 0;
3798#endif
3799 return &mypos;
3800 }
3801 return off2pos(buf, off);
3802}
3803
3804
3805/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003806 * Convert (lnum,col) to byte offset in the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 */
3808 static long
3809pos2off(buf_T *buf, pos_T *pos)
3810{
3811 long offset = 0;
3812
3813 if (!(buf->b_ml.ml_flags & ML_EMPTY))
3814 {
3815 if ((offset = ml_find_line_or_offset(buf, pos->lnum, 0)) < 0)
3816 return 0;
3817 offset += pos->col;
3818 }
3819
3820 return offset;
3821}
3822
3823
Bram Moolenaar009b2592004-10-24 19:18:58 +00003824/*
3825 * This message is printed after NetBeans opens a new file. Its
3826 * similar to the message readfile() uses, but since NetBeans
3827 * doesn't normally call readfile, we do our own.
3828 */
3829 static void
3830print_read_msg(buf)
3831 nbbuf_T *buf;
3832{
3833 int lnum = buf->bufp->b_ml.ml_line_count;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003834 long nchars = (long)buf->bufp->b_orig_size;
Bram Moolenaar009b2592004-10-24 19:18:58 +00003835 char_u c;
3836
3837 msg_add_fname(buf->bufp, buf->bufp->b_ffname);
3838 c = FALSE;
3839
3840 if (buf->bufp->b_p_ro)
3841 {
3842 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]"));
3843 c = TRUE;
3844 }
3845 if (!buf->bufp->b_start_eol)
3846 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003847 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]")
3848 : _("[Incomplete last line]"));
Bram Moolenaar009b2592004-10-24 19:18:58 +00003849 c = TRUE;
3850 }
3851 msg_add_lines(c, (long)lnum, nchars);
3852
3853 /* Now display it */
3854 vim_free(keep_msg);
3855 keep_msg = NULL;
3856 msg_scrolled_ign = TRUE;
3857 msg_trunc_attr(IObuff, FALSE, 0);
3858 msg_scrolled_ign = FALSE;
3859}
3860
3861
3862/*
Bram Moolenaar67c53842010-05-22 18:28:27 +02003863 * Print a message after NetBeans writes the file. This message should be
3864 * identical to the standard message a non-netbeans user would see when
3865 * writing a file.
Bram Moolenaar009b2592004-10-24 19:18:58 +00003866 */
3867 static void
3868print_save_msg(buf, nchars)
3869 nbbuf_T *buf;
3870 long nchars;
3871{
3872 char_u c;
3873 char_u *p;
3874
3875 if (nchars >= 0)
3876 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003877 /* put fname in IObuff with quotes */
3878 msg_add_fname(buf->bufp, buf->bufp->b_ffname);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003879 c = FALSE;
3880
3881 msg_add_lines(c, buf->bufp->b_ml.ml_line_count,
3882 (long)buf->bufp->b_orig_size);
3883
3884 vim_free(keep_msg);
3885 keep_msg = NULL;
3886 msg_scrolled_ign = TRUE;
3887 p = msg_trunc_attr(IObuff, FALSE, 0);
3888 if ((msg_scrolled && !need_wait_return) || !buf->initDone)
3889 {
3890 /* Need to repeat the message after redrawing when:
3891 * - When reading from stdin (the screen will be cleared next).
3892 * - When restart_edit is set (otherwise there will be a delay
3893 * before redrawing).
3894 * - When the screen was scrolled but there is no wait-return
3895 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003896 set_keep_msg(p, 0);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003897 }
3898 msg_scrolled_ign = FALSE;
3899 /* add_to_input_buf((char_u *)"\f", 1); */
3900 }
3901 else
3902 {
3903 char_u ebuf[BUFSIZ];
3904
3905 STRCPY(ebuf, (char_u *)_("E505: "));
3906 STRCAT(ebuf, IObuff);
3907 STRCAT(ebuf, (char_u *)_("is read-only (add ! to override)"));
3908 STRCPY(IObuff, ebuf);
Bram Moolenaarf2330482008-06-24 20:19:36 +00003909 nbdebug((" %s\n", ebuf ));
Bram Moolenaar009b2592004-10-24 19:18:58 +00003910 emsg(IObuff);
3911 }
3912}
3913
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914#endif /* defined(FEAT_NETBEANS_INTG) */