blob: ff191d7d2a414d3461ca270311f465a49666bc16 [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... */
Bram Moolenaarc39125d2010-05-23 12:06:58 +020037# define SOCK_ERRNO 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
Bram Moolenaarc39125d2010-05-23 12:06:58 +020060# define SOCK_ERRNO
Bram Moolenaar071d4272004-06-13 20:20:40 +000061# 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 */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200306 vim_memset((char *)&server, '\0', sizeof(server));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307 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 {
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200336 SOCK_ERRNO;
337 nbdebug(("netbeans_connect: Connect failed with errno %d\n", errno));
338 if (errno == ECONNREFUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 {
340 sock_close(sd);
341#ifdef INET_SOCKETS
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000342 if ((sd = (NBSOCK)socket(AF_INET, SOCK_STREAM, 0)) == (NBSOCK)-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 {
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200344 SOCK_ERRNO;
Bram Moolenaarf2330482008-06-24 20:19:36 +0000345 nbdebug(("socket()#2 in netbeans_connect()\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 PERROR("socket()#2 in netbeans_connect()");
347 goto theend;
348 }
349#else
350 if ((sd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
351 {
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200352 SOCK_ERRNO;
Bram Moolenaarf2330482008-06-24 20:19:36 +0000353 nbdebug(("socket()#2 in netbeans_connect()\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354 PERROR("socket()#2 in netbeans_connect()");
355 goto theend;
356 }
357#endif
358 if (connect(sd, (struct sockaddr *)&server, sizeof(server)))
359 {
360 int retries = 36;
361 int success = FALSE;
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200362
363 SOCK_ERRNO;
364 while (retries-- && ((errno == ECONNREFUSED)
365 || (errno == EINTR)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366 {
367 nbdebug(("retrying...\n"));
368 sleep(5);
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200369 if (!abort)
370 {
371 ui_breakcheck();
372 if (got_int)
373 {
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200374 errno = EINTR;
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200375 break;
376 }
377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 if (connect(sd, (struct sockaddr *)&server,
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200379 sizeof(server)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380 {
381 success = TRUE;
382 break;
383 }
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200384 SOCK_ERRNO;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385 }
386 if (!success)
387 {
388 /* Get here when the server can't be found. */
Bram Moolenaarf2330482008-06-24 20:19:36 +0000389 nbdebug(("Cannot connect to Netbeans #2\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390 PERROR(_("Cannot connect to Netbeans #2"));
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200391 if (abort)
392 getout(1);
393 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 }
395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396 }
397 else
398 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000399 nbdebug(("Cannot connect to Netbeans\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 PERROR(_("Cannot connect to Netbeans"));
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200401 if (abort)
402 getout(1);
403 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404 }
405 }
406
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200407 nbsock = sd;
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000408 vim_snprintf(buf, sizeof(buf), "AUTH %s\n", password);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 nb_send(buf, "netbeans_connect");
410
411 sprintf(buf, "0:version=0 \"%s\"\n", ExtEdProtocolVersion);
412 nb_send(buf, "externaleditor_version");
413
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414theend:
415 vim_free(hostname);
416 vim_free(address);
417 vim_free(password);
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200418 return NETBEANS_OPEN ? OK : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419}
420
421/*
422 * Obtain the NetBeans hostname, port address and password from a file.
423 * Return the strings in allocated memory.
424 * Return FAIL if the file could not be read, OK otherwise (no matter what it
425 * contains).
426 */
427 static int
428getConnInfo(char *file, char **host, char **port, char **auth)
429{
430 FILE *fp;
431 char_u buf[BUFSIZ];
432 char_u *lp;
433 char_u *nl;
434#ifdef UNIX
435 struct stat st;
436
437 /*
438 * For Unix only accept the file when it's not accessible by others.
439 * The open will then fail if we don't own the file.
440 */
441 if (mch_stat(file, &st) == 0 && (st.st_mode & 0077) != 0)
442 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000443 nbdebug(("Wrong access mode for NetBeans connection info file: \"%s\"\n",
444 file));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 EMSG2(_("E668: Wrong access mode for NetBeans connection info file: \"%s\""),
446 file);
447 return FAIL;
448 }
449#endif
450
451 fp = mch_fopen(file, "r");
452 if (fp == NULL)
453 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000454 nbdebug(("Cannot open NetBeans connection info file\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455 PERROR("E660: Cannot open NetBeans connection info file");
456 return FAIL;
457 }
458
459 /* Read the file. There should be one of each parameter */
460 while ((lp = (char_u *)fgets((char *)buf, BUFSIZ, fp)) != NULL)
461 {
462 if ((nl = vim_strchr(lp, '\n')) != NULL)
463 *nl = 0; /* strip off the trailing newline */
464
465 if (STRNCMP(lp, "host=", 5) == 0)
466 {
467 vim_free(*host);
468 *host = (char *)vim_strsave(&buf[5]);
469 }
470 else if (STRNCMP(lp, "port=", 5) == 0)
471 {
472 vim_free(*port);
473 *port = (char *)vim_strsave(&buf[5]);
474 }
475 else if (STRNCMP(lp, "auth=", 5) == 0)
476 {
477 vim_free(*auth);
478 *auth = (char *)vim_strsave(&buf[5]);
479 }
480 }
481 fclose(fp);
482
483 return OK;
484}
485
486
487struct keyqueue
488{
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100489 char_u *keystr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 struct keyqueue *next;
491 struct keyqueue *prev;
492};
493
494typedef struct keyqueue keyQ_T;
495
496static keyQ_T keyHead; /* dummy node, header for circular queue */
497
498
499/*
500 * Queue up key commands sent from netbeans.
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100501 * We store the string, because it may depend on the global mod_mask and
502 * :nbkey doesn't have a key number.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 */
504 static void
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100505postpone_keycommand(char_u *keystr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506{
507 keyQ_T *node;
508
509 node = (keyQ_T *)alloc(sizeof(keyQ_T));
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100510 if (node == NULL)
511 return; /* out of memory, drop the key */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512
513 if (keyHead.next == NULL) /* initialize circular queue */
514 {
515 keyHead.next = &keyHead;
516 keyHead.prev = &keyHead;
517 }
518
519 /* insert node at tail of queue */
520 node->next = &keyHead;
521 node->prev = keyHead.prev;
522 keyHead.prev->next = node;
523 keyHead.prev = node;
524
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100525 node->keystr = vim_strsave(keystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526}
527
528/*
529 * Handle any queued-up NetBeans keycommands to be send.
530 */
531 static void
532handle_key_queue(void)
533{
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100534 int postponed = FALSE;
535
536 while (!postponed && keyHead.next && keyHead.next != &keyHead)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 {
538 /* first, unlink the node */
539 keyQ_T *node = keyHead.next;
540 keyHead.next = node->next;
541 node->next->prev = node->prev;
542
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100543 /* Now, send the keycommand. This may cause it to be postponed again
544 * and change keyHead. */
545 if (node->keystr != NULL)
546 postponed = !netbeans_keystring(node->keystr);
547 vim_free(node->keystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548
549 /* Finally, dispose of the node */
550 vim_free(node);
551 }
552}
553
554
555struct cmdqueue
556{
557 char_u *buffer;
558 struct cmdqueue *next;
559 struct cmdqueue *prev;
560};
561
562typedef struct cmdqueue queue_T;
563
564static queue_T head; /* dummy node, header for circular queue */
565
566
567/*
568 * Put the buffer on the work queue; possibly save it to a file as well.
569 */
570 static void
571save(char_u *buf, int len)
572{
573 queue_T *node;
574
575 node = (queue_T *)alloc(sizeof(queue_T));
576 if (node == NULL)
577 return; /* out of memory */
578 node->buffer = alloc(len + 1);
579 if (node->buffer == NULL)
580 {
581 vim_free(node);
582 return; /* out of memory */
583 }
584 mch_memmove(node->buffer, buf, (size_t)len);
585 node->buffer[len] = NUL;
586
587 if (head.next == NULL) /* initialize circular queue */
588 {
589 head.next = &head;
590 head.prev = &head;
591 }
592
593 /* insert node at tail of queue */
594 node->next = &head;
595 node->prev = head.prev;
596 head.prev->next = node;
597 head.prev = node;
598
599#ifdef NBDEBUG
600 {
601 static int outfd = -2;
602
603 /* possibly write buffer out to a file */
604 if (outfd == -3)
605 return;
606
607 if (outfd == -2)
608 {
609 char *file = getenv("__NETBEANS_SAVE");
610 if (file == NULL)
611 outfd = -3;
612 else
613 outfd = mch_open(file, O_WRONLY|O_CREAT|O_TRUNC, 0666);
614 }
615
616 if (outfd >= 0)
617 write(outfd, buf, len);
618 }
619#endif
620}
621
622
623/*
624 * While there's still a command in the work queue, parse and execute it.
625 */
Bram Moolenaarf2330482008-06-24 20:19:36 +0000626 void
627netbeans_parse_messages(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628{
629 char_u *p;
630 queue_T *node;
631
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200632 if (!NETBEANS_OPEN)
633 return;
634
Bram Moolenaarf2330482008-06-24 20:19:36 +0000635 while (head.next != NULL && head.next != &head)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 {
637 node = head.next;
638
639 /* Locate the first line in the first buffer. */
640 p = vim_strchr(node->buffer, '\n');
641 if (p == NULL)
642 {
643 /* Command isn't complete. If there is no following buffer,
644 * return (wait for more). If there is another buffer following,
645 * prepend the text to that buffer and delete this one. */
646 if (node->next == &head)
647 return;
Bram Moolenaarf2330482008-06-24 20:19:36 +0000648 p = alloc((unsigned)(STRLEN(node->buffer)
649 + STRLEN(node->next->buffer) + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 if (p == NULL)
651 return; /* out of memory */
652 STRCPY(p, node->buffer);
653 STRCAT(p, node->next->buffer);
654 vim_free(node->next->buffer);
655 node->next->buffer = p;
656
657 /* dispose of the node and buffer */
658 head.next = node->next;
659 node->next->prev = node->prev;
660 vim_free(node->buffer);
661 vim_free(node);
662 }
663 else
664 {
665 /* There is a complete command at the start of the buffer.
666 * Terminate it with a NUL. When no more text is following unlink
667 * the buffer. Do this before executing, because new buffers can
668 * be added while busy handling the command. */
669 *p++ = NUL;
670 if (*p == NUL)
671 {
672 head.next = node->next;
673 node->next->prev = node->prev;
674 }
675
676 /* now, parse and execute the commands */
677 nb_parse_cmd(node->buffer);
678
679 if (*p == NUL)
680 {
681 /* buffer finished, dispose of the node and buffer */
682 vim_free(node->buffer);
683 vim_free(node);
684 }
685 else
686 {
687 /* more follows, move to the start */
Bram Moolenaarf2330482008-06-24 20:19:36 +0000688 STRMOVE(node->buffer, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 }
690 }
691 }
692}
693
694/* Buffer size for reading incoming messages. */
695#define MAXMSGSIZE 4096
696
697/*
Bram Moolenaar67c53842010-05-22 18:28:27 +0200698 * Read a command from netbeans.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 */
Bram Moolenaar67c53842010-05-22 18:28:27 +0200700#ifdef FEAT_GUI_MOTIF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 static void
Bram Moolenaara9d45512009-05-17 21:25:42 +0000702messageFromNetbeans(XtPointer clientData UNUSED,
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000703 int *unused1 UNUSED,
704 XtInputId *unused2 UNUSED)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200705{
706 netbeans_read();
707}
708#endif
709
710#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000712messageFromNetbeans(gpointer clientData UNUSED,
713 gint unused1 UNUSED,
714 GdkInputCondition unused2 UNUSED)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200715{
716 netbeans_read();
717}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718#endif
Bram Moolenaar67c53842010-05-22 18:28:27 +0200719
720 void
721netbeans_read()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722{
723 static char_u *buf = NULL;
Bram Moolenaar0b65f892010-05-15 14:49:02 +0200724 int len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 int readlen = 0;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200726#if defined(NB_HAS_GUI) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_W32)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 static int level = 0;
Bram Moolenaarf2330482008-06-24 20:19:36 +0000728#endif
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100729#ifdef HAVE_SELECT
730 struct timeval tval;
731 fd_set rfds;
732#else
733# ifdef HAVE_POLL
734 struct pollfd fds;
735# endif
736#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200738 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 {
740 nbdebug(("messageFromNetbeans() called without a socket\n"));
741 return;
742 }
743
Bram Moolenaar67c53842010-05-22 18:28:27 +0200744#if defined(NB_HAS_GUI) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_W32)
745 /* recursion guard; this will be called from the X event loop at unknown
746 * moments */
747 if (NB_HAS_GUI)
748 ++level;
Bram Moolenaarf2330482008-06-24 20:19:36 +0000749#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750
751 /* Allocate a buffer to read into. */
752 if (buf == NULL)
753 {
754 buf = alloc(MAXMSGSIZE);
755 if (buf == NULL)
756 return; /* out of memory! */
757 }
758
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100759 /* Keep on reading for as long as there is something to read.
760 * Use select() or poll() to avoid blocking on a message that is exactly
761 * MAXMSGSIZE long. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 for (;;)
763 {
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100764#ifdef HAVE_SELECT
765 FD_ZERO(&rfds);
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200766 FD_SET(nbsock, &rfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +0200767 tval.tv_sec = 0;
768 tval.tv_usec = 0;
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200769 if (select(nbsock + 1, &rfds, NULL, NULL, &tval) <= 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200770 break;
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100771#else
772# ifdef HAVE_POLL
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200773 fds.fd = nbsock;
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100774 fds.events = POLLIN;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200775 if (poll(&fds, 1, 0) <= 0)
776 break;
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100777# endif
778#endif
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200779 len = sock_read(nbsock, buf, MAXMSGSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 if (len <= 0)
781 break; /* error or nothing more to read */
782
783 /* Store the read message in the queue. */
784 save(buf, len);
785 readlen += len;
786 if (len < MAXMSGSIZE)
787 break; /* did read everything that's available */
788 }
789
790 if (readlen <= 0)
791 {
792 /* read error or didn't read anything */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200793 netbeans_close();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 nbdebug(("messageFromNetbeans: Error in read() from socket\n"));
795 if (len < 0)
Bram Moolenaarf2330482008-06-24 20:19:36 +0000796 {
797 nbdebug(("read from Netbeans socket\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 PERROR(_("read from Netbeans socket"));
Bram Moolenaarf2330482008-06-24 20:19:36 +0000799 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000800 return; /* don't try to parse it */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 }
802
Bram Moolenaar67c53842010-05-22 18:28:27 +0200803#if defined(NB_HAS_GUI) && !defined(FEAT_GUI_W32)
Bram Moolenaar61665aa2008-12-24 11:20:53 +0000804 /* Let the main loop handle messages. */
Bram Moolenaar67c53842010-05-22 18:28:27 +0200805 if (NB_HAS_GUI)
806 {
Bram Moolenaar61665aa2008-12-24 11:20:53 +0000807# ifdef FEAT_GUI_GTK
Bram Moolenaar67c53842010-05-22 18:28:27 +0200808 if (gtk_main_level() > 0)
809 gtk_main_quit();
810# else
811 /* Parse the messages now, but avoid recursion. */
812 if (level == 1)
813 netbeans_parse_messages();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814
Bram Moolenaar67c53842010-05-22 18:28:27 +0200815 --level;
816# endif
817 }
Bram Moolenaarf2330482008-06-24 20:19:36 +0000818#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819}
820
821/*
822 * Handle one NUL terminated command.
823 *
824 * format of a command from netbeans:
825 *
826 * 6:setTitle!84 "a.c"
827 *
828 * bufno
829 * colon
830 * cmd
831 * !
832 * cmdno
833 * args
834 *
835 * for function calls, the ! is replaced by a /
836 */
837 static void
838nb_parse_cmd(char_u *cmd)
839{
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000840 char *verb;
841 char *q;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 int bufno;
843 int isfunc = -1;
844
845 if (STRCMP(cmd, "DISCONNECT") == 0)
846 {
847 /* We assume the server knows that we can safely exit! */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 /* Disconnect before exiting, Motif hangs in a Select error
849 * message otherwise. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200850 netbeans_close();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 getout(0);
852 /* NOTREACHED */
853 }
854
855 if (STRCMP(cmd, "DETACH") == 0)
856 {
857 /* The IDE is breaking the connection. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200858 netbeans_close();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 return;
860 }
861
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000862 bufno = strtol((char *)cmd, &verb, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863
864 if (*verb != ':')
865 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000866 nbdebug((" missing colon: %s\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 EMSG2("E627: missing colon: %s", cmd);
868 return;
869 }
870 ++verb; /* skip colon */
871
872 for (q = verb; *q; q++)
873 {
874 if (*q == '!')
875 {
876 *q++ = NUL;
877 isfunc = 0;
878 break;
879 }
880 else if (*q == '/')
881 {
882 *q++ = NUL;
883 isfunc = 1;
884 break;
885 }
886 }
887
888 if (isfunc < 0)
889 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000890 nbdebug((" missing ! or / in: %s\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 EMSG2("E628: missing ! or / in: %s", cmd);
892 return;
893 }
894
Bram Moolenaar89d40322006-08-29 15:30:07 +0000895 r_cmdno = strtol(q, &q, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000897 q = (char *)skipwhite((char_u *)q);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898
Bram Moolenaar89d40322006-08-29 15:30:07 +0000899 if (nb_do_cmd(bufno, (char_u *)verb, isfunc, r_cmdno, (char_u *)q) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 {
Bram Moolenaar009b2592004-10-24 19:18:58 +0000901#ifdef NBDEBUG
902 /*
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100903 * This happens because the ExtEd can send a command or 2 after
Bram Moolenaar009b2592004-10-24 19:18:58 +0000904 * doing a stopDocumentListen command. It doesn't harm anything
905 * so I'm disabling it except for debugging.
906 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 nbdebug(("nb_parse_cmd: Command error for \"%s\"\n", cmd));
908 EMSG("E629: bad return from nb_do_cmd");
Bram Moolenaar009b2592004-10-24 19:18:58 +0000909#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 }
911}
912
913struct nbbuf_struct
914{
915 buf_T *bufp;
916 unsigned int fireChanges:1;
917 unsigned int initDone:1;
Bram Moolenaar009b2592004-10-24 19:18:58 +0000918 unsigned int insertDone:1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 unsigned int modified:1;
Bram Moolenaar009b2592004-10-24 19:18:58 +0000920 int nbbuf_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 char *displayname;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 int *signmap;
923 short_u signmaplen;
924 short_u signmapused;
925};
926
927typedef struct nbbuf_struct nbbuf_T;
928
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200929static nbbuf_T *buf_list = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000930static int buf_list_size = 0; /* size of buf_list */
931static int buf_list_used = 0; /* nr of entries in buf_list actually in use */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200933static char **globalsignmap = NULL;
934static int globalsignmaplen = 0;
935static int globalsignmapused = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936
937static int mapsigntype __ARGS((nbbuf_T *, int localsigntype));
938static void addsigntype __ARGS((nbbuf_T *, int localsigntype, char_u *typeName,
939 char_u *tooltip, char_u *glyphfile,
Bram Moolenaar67c53842010-05-22 18:28:27 +0200940 char_u *fg, char_u *bg));
Bram Moolenaar009b2592004-10-24 19:18:58 +0000941static void print_read_msg __ARGS((nbbuf_T *buf));
Bram Moolenaar914703b2010-05-31 21:59:46 +0200942static void print_save_msg __ARGS((nbbuf_T *buf, off_t nchars));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943
944static int curPCtype = -1;
945
946/*
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200947 * Free netbeans resources.
948 */
949 static void
950nb_free()
951{
952 keyQ_T *key_node = keyHead.next;
953 queue_T *cmd_node = head.next;
954 nbbuf_T buf;
955 buf_T *bufp;
956 int i;
957
958 /* free the netbeans buffer list */
959 for (i = 0; i < buf_list_used; i++)
960 {
961 buf = buf_list[i];
962 vim_free(buf.displayname);
963 vim_free(buf.signmap);
964 if ((bufp=buf.bufp) != NULL)
965 {
966 buf.bufp->b_netbeans_file = FALSE;
967 buf.bufp->b_was_netbeans_file = FALSE;
968 }
969 }
970 vim_free(buf_list);
971 buf_list = NULL;
972 buf_list_size = 0;
973 buf_list_used = 0;
974
975 /* free the queued key commands */
976 while(key_node != NULL && key_node != &keyHead)
977 {
978 keyQ_T *next = key_node->next;
979 vim_free(key_node->keystr);
980 vim_free(key_node);
981 if (next == &keyHead)
982 {
983 keyHead.next = &keyHead;
984 keyHead.prev = &keyHead;
985 break;
986 }
987 key_node = next;
988 }
989
990 /* free the queued netbeans commands */
991 while(cmd_node != NULL && cmd_node != &head)
992 {
993 queue_T *next = cmd_node->next;
994 vim_free(cmd_node->buffer);
995 vim_free(cmd_node);
996 if (next == &head)
997 {
998 head.next = &head;
999 head.prev = &head;
1000 break;
1001 }
1002 cmd_node = next;
1003 }
1004}
1005
1006/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 * Get the Netbeans buffer number for the specified buffer.
1008 */
1009 static int
1010nb_getbufno(buf_T *bufp)
1011{
1012 int i;
1013
1014 for (i = 0; i < buf_list_used; i++)
1015 if (buf_list[i].bufp == bufp)
1016 return i;
1017 return -1;
1018}
1019
1020/*
1021 * Is this a NetBeans-owned buffer?
1022 */
1023 int
1024isNetbeansBuffer(buf_T *bufp)
1025{
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001026 return NETBEANS_OPEN && bufp->b_netbeans_file;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027}
1028
1029/*
1030 * NetBeans and Vim have different undo models. In Vim, the file isn't
1031 * changed if changes are undone via the undo command. In NetBeans, once
1032 * a change has been made the file is marked as modified until saved. It
1033 * doesn't matter if the change was undone.
1034 *
1035 * So this function is for the corner case where Vim thinks a buffer is
1036 * unmodified but NetBeans thinks it IS modified.
1037 */
1038 int
1039isNetbeansModified(buf_T *bufp)
1040{
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001041 if (isNetbeansBuffer(bufp))
Bram Moolenaar009b2592004-10-24 19:18:58 +00001042 {
1043 int bufno = nb_getbufno(bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044
Bram Moolenaar009b2592004-10-24 19:18:58 +00001045 if (bufno > 0)
1046 return buf_list[bufno].modified;
1047 else
1048 return FALSE;
1049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 else
1051 return FALSE;
1052}
1053
1054/*
1055 * Given a Netbeans buffer number, return the netbeans buffer.
1056 * Returns NULL for 0 or a negative number. A 0 bufno means a
1057 * non-buffer related command has been sent.
1058 */
1059 static nbbuf_T *
1060nb_get_buf(int bufno)
1061{
1062 /* find or create a buffer with the given number */
1063 int incr;
1064
1065 if (bufno <= 0)
1066 return NULL;
1067
1068 if (!buf_list)
1069 {
1070 /* initialize */
1071 buf_list = (nbbuf_T *)alloc_clear(100 * sizeof(nbbuf_T));
1072 buf_list_size = 100;
1073 }
1074 if (bufno >= buf_list_used) /* new */
1075 {
1076 if (bufno >= buf_list_size) /* grow list */
1077 {
1078 incr = bufno - buf_list_size + 90;
1079 buf_list_size += incr;
1080 buf_list = (nbbuf_T *)vim_realloc(
1081 buf_list, buf_list_size * sizeof(nbbuf_T));
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02001082 vim_memset(buf_list + buf_list_size - incr, 0,
1083 incr * sizeof(nbbuf_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 }
1085
1086 while (buf_list_used <= bufno)
1087 {
1088 /* Default is to fire text changes. */
1089 buf_list[buf_list_used].fireChanges = 1;
1090 ++buf_list_used;
1091 }
1092 }
1093
1094 return buf_list + bufno;
1095}
1096
1097/*
1098 * Return the number of buffers that are modified.
1099 */
1100 static int
1101count_changed_buffers(void)
1102{
1103 buf_T *bufp;
1104 int n;
1105
1106 n = 0;
1107 for (bufp = firstbuf; bufp != NULL; bufp = bufp->b_next)
1108 if (bufp->b_changed)
1109 ++n;
1110 return n;
1111}
1112
1113/*
1114 * End the netbeans session.
1115 */
1116 void
1117netbeans_end(void)
1118{
1119 int i;
1120 static char buf[128];
1121
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001122 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 return;
1124
1125 for (i = 0; i < buf_list_used; i++)
1126 {
1127 if (!buf_list[i].bufp)
1128 continue;
1129 if (netbeansForcedQuit)
1130 {
1131 /* mark as unmodified so NetBeans won't put up dialog on "killed" */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001132 sprintf(buf, "%d:unmodified=%d\n", i, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 nbdebug(("EVT: %s", buf));
1134 nb_send(buf, "netbeans_end");
1135 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001136 sprintf(buf, "%d:killed=%d\n", i, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 nbdebug(("EVT: %s", buf));
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001138 /* nb_send(buf, "netbeans_end"); avoid "write failed" messages */
1139 ignored = sock_write(nbsock, buf, (int)STRLEN(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141}
1142
1143/*
1144 * Send a message to netbeans.
1145 */
1146 static void
1147nb_send(char *buf, char *fun)
1148{
1149 /* Avoid giving pages full of error messages when the other side has
1150 * exited, only mention the first error until the connection works again. */
1151 static int did_error = FALSE;
1152
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001153 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 {
1155 if (!did_error)
Bram Moolenaarf2330482008-06-24 20:19:36 +00001156 {
1157 nbdebug((" %s(): write while not connected\n", fun));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 EMSG2("E630: %s(): write while not connected", fun);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 did_error = TRUE;
1161 }
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001162 else if (sock_write(nbsock, buf, (int)STRLEN(buf)) != (int)STRLEN(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 {
1164 if (!did_error)
Bram Moolenaarf2330482008-06-24 20:19:36 +00001165 {
1166 nbdebug((" %s(): write failed\n", fun));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 EMSG2("E631: %s(): write failed", fun);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 did_error = TRUE;
1170 }
1171 else
1172 did_error = FALSE;
1173}
1174
1175/*
1176 * Some input received from netbeans requires a response. This function
1177 * handles a response with no information (except the command number).
1178 */
1179 static void
1180nb_reply_nil(int cmdno)
1181{
1182 char reply[32];
1183
Bram Moolenaar009b2592004-10-24 19:18:58 +00001184 nbdebug(("REP %d: <none>\n", cmdno));
1185
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 sprintf(reply, "%d\n", cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 nb_send(reply, "nb_reply_nil");
1188}
1189
1190
1191/*
1192 * Send a response with text.
1193 * "result" must have been quoted already (using nb_quote()).
1194 */
1195 static void
1196nb_reply_text(int cmdno, char_u *result)
1197{
1198 char_u *reply;
1199
Bram Moolenaar009b2592004-10-24 19:18:58 +00001200 nbdebug(("REP %d: %s\n", cmdno, (char *)result));
1201
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001202 reply = alloc((unsigned)STRLEN(result) + 32);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 sprintf((char *)reply, "%d %s\n", cmdno, (char *)result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 nb_send((char *)reply, "nb_reply_text");
1205
1206 vim_free(reply);
1207}
1208
1209
1210/*
1211 * Send a response with a number result code.
1212 */
1213 static void
1214nb_reply_nr(int cmdno, long result)
1215{
1216 char reply[32];
1217
Bram Moolenaar009b2592004-10-24 19:18:58 +00001218 nbdebug(("REP %d: %ld\n", cmdno, result));
1219
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 sprintf(reply, "%d %ld\n", cmdno, result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 nb_send(reply, "nb_reply_nr");
1222}
1223
1224
1225/*
1226 * Encode newline, ret, backslash, double quote for transmission to NetBeans.
1227 */
1228 static char_u *
1229nb_quote(char_u *txt)
1230{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001231 char_u *buf = alloc((unsigned)(2 * STRLEN(txt) + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 char_u *p = txt;
1233 char_u *q = buf;
1234
1235 if (buf == NULL)
1236 return NULL;
1237 for (; *p; p++)
1238 {
1239 switch (*p)
1240 {
1241 case '\"':
1242 case '\\':
1243 *q++ = '\\'; *q++ = *p; break;
1244 /* case '\t': */
1245 /* *q++ = '\\'; *q++ = 't'; break; */
1246 case '\n':
1247 *q++ = '\\'; *q++ = 'n'; break;
1248 case '\r':
1249 *q++ = '\\'; *q++ = 'r'; break;
1250 default:
1251 *q++ = *p;
1252 break;
1253 }
1254 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01001255 *q = '\0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256
1257 return buf;
1258}
1259
1260
1261/*
1262 * Remove top level double quotes; convert backslashed chars.
1263 * Returns an allocated string (NULL for failure).
1264 * If "endp" is not NULL it is set to the character after the terminating
1265 * quote.
1266 */
1267 static char *
1268nb_unquote(char_u *p, char_u **endp)
1269{
1270 char *result = 0;
1271 char *q;
1272 int done = 0;
1273
1274 /* result is never longer than input */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001275 result = (char *)alloc_clear((unsigned)STRLEN(p) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 if (result == NULL)
1277 return NULL;
1278
1279 if (*p++ != '"')
1280 {
1281 nbdebug(("nb_unquote called with string that doesn't start with a quote!: %s\n",
1282 p));
1283 result[0] = NUL;
1284 return result;
1285 }
1286
1287 for (q = result; !done && *p != NUL;)
1288 {
1289 switch (*p)
1290 {
1291 case '"':
1292 /*
1293 * Unbackslashed dquote marks the end, if first char was dquote.
1294 */
1295 done = 1;
1296 break;
1297
1298 case '\\':
1299 ++p;
1300 switch (*p)
1301 {
1302 case '\\': *q++ = '\\'; break;
1303 case 'n': *q++ = '\n'; break;
1304 case 't': *q++ = '\t'; break;
1305 case 'r': *q++ = '\r'; break;
1306 case '"': *q++ = '"'; break;
1307 case NUL: --p; break;
1308 /* default: skip over illegal chars */
1309 }
1310 ++p;
1311 break;
1312
1313 default:
1314 *q++ = *p++;
1315 }
1316 }
1317
1318 if (endp != NULL)
1319 *endp = p;
1320
1321 return result;
1322}
1323
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001324/*
1325 * Remove from "first" byte to "last" byte (inclusive), at line "lnum" of the
1326 * current buffer. Remove to end of line when "last" is MAXCOL.
1327 */
1328 static void
1329nb_partialremove(linenr_T lnum, colnr_T first, colnr_T last)
1330{
1331 char_u *oldtext, *newtext;
1332 int oldlen;
1333 int lastbyte = last;
1334
1335 oldtext = ml_get(lnum);
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001336 oldlen = (int)STRLEN(oldtext);
Bram Moolenaarb3c70982008-01-18 10:40:55 +00001337 if (first >= (colnr_T)oldlen || oldlen == 0) /* just in case */
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001338 return;
1339 if (lastbyte >= oldlen)
1340 lastbyte = oldlen - 1;
1341 newtext = alloc(oldlen - (int)(lastbyte - first));
1342 if (newtext != NULL)
1343 {
1344 mch_memmove(newtext, oldtext, first);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001345 STRMOVE(newtext + first, oldtext + lastbyte + 1);
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001346 nbdebug((" NEW LINE %d: %s\n", lnum, newtext));
1347 ml_replace(lnum, newtext, FALSE);
1348 }
1349}
1350
1351/*
1352 * Replace the "first" line with the concatenation of the "first" and
1353 * the "other" line. The "other" line is not removed.
1354 */
1355 static void
1356nb_joinlines(linenr_T first, linenr_T other)
1357{
1358 int len_first, len_other;
1359 char_u *p;
1360
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001361 len_first = (int)STRLEN(ml_get(first));
1362 len_other = (int)STRLEN(ml_get(other));
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001363 p = alloc((unsigned)(len_first + len_other + 1));
1364 if (p != NULL)
1365 {
1366 mch_memmove(p, ml_get(first), len_first);
1367 mch_memmove(p + len_first, ml_get(other), len_other + 1);
1368 ml_replace(first, p, FALSE);
1369 }
1370}
1371
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372#define SKIP_STOP 2
1373#define streq(a,b) (strcmp(a,b) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374
1375/*
1376 * Do the actual processing of a single netbeans command or function.
Bram Moolenaar143c38c2007-05-10 16:41:10 +00001377 * The difference between a command and function is that a function
1378 * gets a response (it's required) but a command does not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 * For arguments see comment for nb_parse_cmd().
1380 */
1381 static int
1382nb_do_cmd(
1383 int bufno,
1384 char_u *cmd,
1385 int func,
1386 int cmdno,
1387 char_u *args) /* points to space before arguments or NUL */
1388{
1389 int doupdate = 0;
1390 long off = 0;
1391 nbbuf_T *buf = nb_get_buf(bufno);
1392 static int skip = 0;
1393 int retval = OK;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001394 char *cp; /* for when a char pointer is needed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395
1396 nbdebug(("%s %d: (%d) %s %s\n", (func) ? "FUN" : "CMD", cmdno, bufno, cmd,
1397 STRCMP(cmd, "insert") == 0 ? "<text>" : (char *)args));
1398
1399 if (func)
1400 {
1401/* =====================================================================*/
1402 if (streq((char *)cmd, "getModified"))
1403 {
1404 if (buf == NULL || buf->bufp == NULL)
1405 /* Return the number of buffers that are modified. */
1406 nb_reply_nr(cmdno, (long)count_changed_buffers());
1407 else
1408 /* Return whether the buffer is modified. */
1409 nb_reply_nr(cmdno, (long)(buf->bufp->b_changed
1410 || isNetbeansModified(buf->bufp)));
1411/* =====================================================================*/
1412 }
1413 else if (streq((char *)cmd, "saveAndExit"))
1414 {
1415 /* Note: this will exit Vim if successful. */
1416 coloncmd(":confirm qall");
1417
1418 /* We didn't exit: return the number of changed buffers. */
1419 nb_reply_nr(cmdno, (long)count_changed_buffers());
1420/* =====================================================================*/
1421 }
1422 else if (streq((char *)cmd, "getCursor"))
1423 {
1424 char_u text[200];
1425
1426 /* Note: nb_getbufno() may return -1. This indicates the IDE
1427 * didn't assign a number to the current buffer in response to a
1428 * fileOpened event. */
1429 sprintf((char *)text, "%d %ld %d %ld",
1430 nb_getbufno(curbuf),
1431 (long)curwin->w_cursor.lnum,
1432 (int)curwin->w_cursor.col,
1433 pos2off(curbuf, &curwin->w_cursor));
1434 nb_reply_text(cmdno, text);
1435/* =====================================================================*/
1436 }
Bram Moolenaarc65c4912006-11-14 17:29:46 +00001437 else if (streq((char *)cmd, "getAnno"))
1438 {
1439 long linenum = 0;
1440#ifdef FEAT_SIGNS
1441 if (buf == NULL || buf->bufp == NULL)
1442 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001443 nbdebug((" Invalid buffer identifier in getAnno\n"));
1444 EMSG("E652: Invalid buffer identifier in getAnno");
Bram Moolenaarc65c4912006-11-14 17:29:46 +00001445 retval = FAIL;
1446 }
1447 else
1448 {
1449 int serNum;
1450
1451 cp = (char *)args;
1452 serNum = strtol(cp, &cp, 10);
1453 /* If the sign isn't found linenum will be zero. */
1454 linenum = (long)buf_findsign(buf->bufp, serNum);
1455 }
1456#endif
1457 nb_reply_nr(cmdno, linenum);
1458/* =====================================================================*/
1459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 else if (streq((char *)cmd, "getLength"))
1461 {
1462 long len = 0;
1463
1464 if (buf == NULL || buf->bufp == NULL)
1465 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001466 nbdebug((" invalid buffer identifier in getLength\n"));
1467 EMSG("E632: invalid buffer identifier in getLength");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 retval = FAIL;
1469 }
1470 else
1471 {
1472 len = get_buf_size(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 }
1474 nb_reply_nr(cmdno, len);
1475/* =====================================================================*/
1476 }
1477 else if (streq((char *)cmd, "getText"))
1478 {
1479 long len;
1480 linenr_T nlines;
1481 char_u *text = NULL;
1482 linenr_T lno = 1;
1483 char_u *p;
1484 char_u *line;
1485
1486 if (buf == NULL || buf->bufp == NULL)
1487 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001488 nbdebug((" invalid buffer identifier in getText\n"));
1489 EMSG("E633: invalid buffer identifier in getText");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 retval = FAIL;
1491 }
1492 else
1493 {
1494 len = get_buf_size(buf->bufp);
1495 nlines = buf->bufp->b_ml.ml_line_count;
1496 text = alloc((unsigned)((len > 0)
1497 ? ((len + nlines) * 2) : 4));
1498 if (text == NULL)
1499 {
1500 nbdebug((" nb_do_cmd: getText has null text field\n"));
1501 retval = FAIL;
1502 }
1503 else
1504 {
1505 p = text;
1506 *p++ = '\"';
1507 for (; lno <= nlines ; lno++)
1508 {
1509 line = nb_quote(ml_get_buf(buf->bufp, lno, FALSE));
1510 if (line != NULL)
1511 {
1512 STRCPY(p, line);
1513 p += STRLEN(line);
1514 *p++ = '\\';
1515 *p++ = 'n';
Bram Moolenaar009b2592004-10-24 19:18:58 +00001516 vim_free(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 }
1519 *p++ = '\"';
1520 *p = '\0';
1521 }
1522 }
1523 if (text == NULL)
1524 nb_reply_text(cmdno, (char_u *)"");
1525 else
1526 {
1527 nb_reply_text(cmdno, text);
1528 vim_free(text);
1529 }
1530/* =====================================================================*/
1531 }
1532 else if (streq((char *)cmd, "remove"))
1533 {
1534 long count;
1535 pos_T first, last;
1536 pos_T *pos;
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001537 pos_T *next;
1538 linenr_T del_from_lnum, del_to_lnum; /* lines to be deleted as a whole */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 int oldFire = netbeansFireChanges;
1540 int oldSuppress = netbeansSuppressNoLines;
1541 int wasChanged;
1542
1543 if (skip >= SKIP_STOP)
1544 {
1545 nbdebug((" Skipping %s command\n", (char *) cmd));
1546 nb_reply_nil(cmdno);
1547 return OK;
1548 }
1549
1550 if (buf == NULL || buf->bufp == NULL)
1551 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001552 nbdebug((" invalid buffer identifier in remove\n"));
1553 EMSG("E634: invalid buffer identifier in remove");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 retval = FAIL;
1555 }
1556 else
1557 {
1558 netbeansFireChanges = FALSE;
1559 netbeansSuppressNoLines = TRUE;
1560
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001561 nb_set_curbuf(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 wasChanged = buf->bufp->b_changed;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001563 cp = (char *)args;
1564 off = strtol(cp, &cp, 10);
1565 count = strtol(cp, &cp, 10);
1566 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 /* delete "count" chars, starting at "off" */
1568 pos = off2pos(buf->bufp, off);
1569 if (!pos)
1570 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001571 nbdebug((" !bad position\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 nb_reply_text(cmdno, (char_u *)"!bad position");
1573 netbeansFireChanges = oldFire;
1574 netbeansSuppressNoLines = oldSuppress;
1575 return FAIL;
1576 }
1577 first = *pos;
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001578 nbdebug((" FIRST POS: line %d, col %d\n",
1579 first.lnum, first.col));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 pos = off2pos(buf->bufp, off+count-1);
1581 if (!pos)
1582 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001583 nbdebug((" !bad count\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 nb_reply_text(cmdno, (char_u *)"!bad count");
1585 netbeansFireChanges = oldFire;
1586 netbeansSuppressNoLines = oldSuppress;
1587 return FAIL;
1588 }
1589 last = *pos;
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001590 nbdebug((" LAST POS: line %d, col %d\n",
1591 last.lnum, last.col));
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001592 del_from_lnum = first.lnum;
1593 del_to_lnum = last.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 doupdate = 1;
1595
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001596 /* Get the position of the first byte after the deleted
1597 * section. "next" is NULL when deleting to the end of the
1598 * file. */
1599 next = off2pos(buf->bufp, off + count);
1600
1601 /* Remove part of the first line. */
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001602 if (first.col != 0
1603 || (next != NULL && first.lnum == next->lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 {
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001605 if (first.lnum != last.lnum
1606 || (next != NULL && first.lnum != next->lnum))
1607 {
1608 /* remove to the end of the first line */
1609 nb_partialremove(first.lnum, first.col,
1610 (colnr_T)MAXCOL);
1611 if (first.lnum == last.lnum)
1612 {
1613 /* Partial line to remove includes the end of
1614 * line. Join the line with the next one, have
1615 * the next line deleted below. */
1616 nb_joinlines(first.lnum, next->lnum);
1617 del_to_lnum = next->lnum;
1618 }
1619 }
1620 else
1621 {
1622 /* remove within one line */
1623 nb_partialremove(first.lnum, first.col, last.col);
1624 }
1625 ++del_from_lnum; /* don't delete the first line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 }
1627
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001628 /* Remove part of the last line. */
1629 if (first.lnum != last.lnum && next != NULL
1630 && next->col != 0 && last.lnum == next->lnum)
1631 {
1632 nb_partialremove(last.lnum, 0, last.col);
1633 if (del_from_lnum > first.lnum)
1634 {
1635 /* Join end of last line to start of first line; last
1636 * line is deleted below. */
1637 nb_joinlines(first.lnum, last.lnum);
1638 }
1639 else
1640 /* First line is deleted as a whole, keep the last
1641 * line. */
1642 --del_to_lnum;
1643 }
1644
1645 /* First is partial line; last line to remove includes
1646 * the end of line; join first line to line following last
1647 * line; line following last line is deleted below. */
1648 if (first.lnum != last.lnum && del_from_lnum > first.lnum
1649 && next != NULL && last.lnum != next->lnum)
1650 {
1651 nb_joinlines(first.lnum, next->lnum);
1652 del_to_lnum = next->lnum;
1653 }
1654
1655 /* Delete whole lines if there are any. */
1656 if (del_to_lnum >= del_from_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 {
1658 int i;
1659
1660 /* delete signs from the lines being deleted */
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001661 for (i = del_from_lnum; i <= del_to_lnum; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 {
1663 int id = buf_findsign_id(buf->bufp, (linenr_T)i);
1664 if (id > 0)
1665 {
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001666 nbdebug((" Deleting sign %d on line %d\n",
1667 id, i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 buf_delsign(buf->bufp, id);
1669 }
1670 else
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001671 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 nbdebug((" No sign on line %d\n", i));
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 }
1675
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001676 nbdebug((" Deleting lines %d through %d\n",
1677 del_from_lnum, del_to_lnum));
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001678 curwin->w_cursor.lnum = del_from_lnum;
1679 curwin->w_cursor.col = 0;
1680 del_lines(del_to_lnum - del_from_lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 }
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001682
1683 /* Leave cursor at first deleted byte. */
1684 curwin->w_cursor = first;
1685 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 buf->bufp->b_changed = wasChanged; /* logically unchanged */
1687 netbeansFireChanges = oldFire;
1688 netbeansSuppressNoLines = oldSuppress;
1689
1690 u_blockfree(buf->bufp);
1691 u_clearall(buf->bufp);
1692 }
1693 nb_reply_nil(cmdno);
1694/* =====================================================================*/
1695 }
1696 else if (streq((char *)cmd, "insert"))
1697 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 char_u *to_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699
1700 if (skip >= SKIP_STOP)
1701 {
1702 nbdebug((" Skipping %s command\n", (char *) cmd));
1703 nb_reply_nil(cmdno);
1704 return OK;
1705 }
1706
1707 /* get offset */
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001708 cp = (char *)args;
1709 off = strtol(cp, &cp, 10);
1710 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711
1712 /* get text to be inserted */
1713 args = skipwhite(args);
1714 args = to_free = (char_u *)nb_unquote(args, NULL);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001715 /*
1716 nbdebug((" CHUNK[%d]: %d bytes at offset %d\n",
1717 buf->bufp->b_ml.ml_line_count, STRLEN(args), off));
1718 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719
1720 if (buf == NULL || buf->bufp == NULL)
1721 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001722 nbdebug((" invalid buffer identifier in insert\n"));
1723 EMSG("E635: invalid buffer identifier in insert");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 retval = FAIL;
1725 }
1726 else if (args != NULL)
1727 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001728 int ff_detected = EOL_UNKNOWN;
1729 int buf_was_empty = (buf->bufp->b_ml.ml_flags & ML_EMPTY);
1730 size_t len = 0;
1731 int added = 0;
1732 int oldFire = netbeansFireChanges;
1733 int old_b_changed;
1734 char_u *nl;
1735 linenr_T lnum;
1736 linenr_T lnum_start;
1737 pos_T *pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 netbeansFireChanges = 0;
1740
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001741 /* Jump to the buffer where we insert. After this "curbuf"
1742 * can be used. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001743 nb_set_curbuf(buf->bufp);
Bram Moolenaara3227e22006-03-08 21:32:40 +00001744 old_b_changed = curbuf->b_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001746 /* Convert the specified character offset into a lnum/col
1747 * position. */
Bram Moolenaara3227e22006-03-08 21:32:40 +00001748 pos = off2pos(curbuf, off);
1749 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001751 if (pos->lnum <= 0)
1752 lnum_start = 1;
1753 else
1754 lnum_start = pos->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 }
1756 else
1757 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001758 /* If the given position is not found, assume we want
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 * the end of the file. See setLocAndSize HACK. */
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001760 if (buf_was_empty)
1761 lnum_start = 1; /* above empty line */
1762 else
1763 lnum_start = curbuf->b_ml.ml_line_count + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001764 }
1765
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001766 /* "lnum" is the line where we insert: either append to it or
1767 * insert a new line above it. */
1768 lnum = lnum_start;
1769
1770 /* Loop over the "\n" separated lines of the argument. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 doupdate = 1;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001772 while (*args != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001774 nl = vim_strchr(args, '\n');
1775 if (nl == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001777 /* Incomplete line, probably truncated. Next "insert"
1778 * command should append to this one. */
1779 len = STRLEN(args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001781 else
1782 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001783 len = nl - args;
1784
1785 /*
1786 * We need to detect EOL style, because the commands
1787 * use a character offset.
1788 */
1789 if (nl > args && nl[-1] == '\r')
1790 {
1791 ff_detected = EOL_DOS;
1792 --len;
1793 }
1794 else
1795 ff_detected = EOL_UNIX;
1796 }
1797 args[len] = NUL;
1798
1799 if (lnum == lnum_start
1800 && ((pos != NULL && pos->col > 0)
1801 || (lnum == 1 && buf_was_empty)))
1802 {
1803 char_u *oldline = ml_get(lnum);
1804 char_u *newline;
1805
1806 /* Insert halfway a line. For simplicity we assume we
1807 * need to append to the line. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001808 newline = alloc_check((unsigned)(STRLEN(oldline) + len + 1));
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001809 if (newline != NULL)
1810 {
1811 STRCPY(newline, oldline);
1812 STRCAT(newline, args);
1813 ml_replace(lnum, newline, FALSE);
1814 }
1815 }
1816 else
1817 {
1818 /* Append a new line. Not that we always do this,
1819 * also when the text doesn't end in a "\n". */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001820 ml_append((linenr_T)(lnum - 1), args, (colnr_T)(len + 1), FALSE);
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001821 ++added;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001822 }
1823
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001824 if (nl == NULL)
1825 break;
1826 ++lnum;
1827 args = nl + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001828 }
1829
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001830 /* Adjust the marks below the inserted lines. */
1831 appended_lines_mark(lnum_start - 1, (long)added);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001833 /*
1834 * When starting with an empty buffer set the fileformat.
1835 * This is just guessing...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 */
1837 if (buf_was_empty)
1838 {
1839 if (ff_detected == EOL_UNKNOWN)
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001840#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 ff_detected = EOL_DOS;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001842#else
1843 ff_detected = EOL_UNIX;
1844#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 set_fileformat(ff_detected, OPT_LOCAL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00001846 curbuf->b_start_ffc = *curbuf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 }
1848
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 /*
1850 * XXX - GRP - Is the next line right? If I've inserted
1851 * text the buffer has been updated but not written. Will
1852 * netbeans guarantee to write it? Even if I do a :q! ?
1853 */
Bram Moolenaara3227e22006-03-08 21:32:40 +00001854 curbuf->b_changed = old_b_changed; /* logically unchanged */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 netbeansFireChanges = oldFire;
1856
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001857 /* Undo info is invalid now... */
Bram Moolenaara3227e22006-03-08 21:32:40 +00001858 u_blockfree(curbuf);
1859 u_clearall(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 }
1861 vim_free(to_free);
1862 nb_reply_nil(cmdno); /* or !error */
1863 }
1864 else
1865 {
1866 nbdebug(("UNIMPLEMENTED FUNCTION: %s\n", cmd));
1867 nb_reply_nil(cmdno);
1868 retval = FAIL;
1869 }
1870 }
1871 else /* Not a function; no reply required. */
1872 {
1873/* =====================================================================*/
1874 if (streq((char *)cmd, "create"))
1875 {
1876 /* Create a buffer without a name. */
1877 if (buf == NULL)
1878 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001879 nbdebug((" invalid buffer identifier in create\n"));
1880 EMSG("E636: invalid buffer identifier in create");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 return FAIL;
1882 }
1883 vim_free(buf->displayname);
1884 buf->displayname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885
1886 netbeansReadFile = 0; /* don't try to open disk file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001887 do_ecmd(0, NULL, 0, 0, ECMD_ONE, ECMD_HIDE + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 netbeansReadFile = 1;
1889 buf->bufp = curbuf;
1890 maketitle();
Bram Moolenaar009b2592004-10-24 19:18:58 +00001891 buf->insertDone = FALSE;
Bram Moolenaar67c53842010-05-22 18:28:27 +02001892#if defined(FEAT_MENU) && defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 gui_update_menus(0);
Bram Moolenaar67c53842010-05-22 18:28:27 +02001894#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895/* =====================================================================*/
1896 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001897 else if (streq((char *)cmd, "insertDone"))
1898 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001899 if (buf == NULL || buf->bufp == NULL)
1900 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001901 nbdebug((" invalid buffer identifier in insertDone\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001902 }
1903 else
1904 {
1905 buf->bufp->b_start_eol = *args == 'T';
1906 buf->insertDone = TRUE;
1907 args += 2;
1908 buf->bufp->b_p_ro = *args == 'T';
1909 print_read_msg(buf);
1910 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001911/* =====================================================================*/
1912 }
1913 else if (streq((char *)cmd, "saveDone"))
1914 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001915 long savedChars = atol((char *)args);
1916
1917 if (buf == NULL || buf->bufp == NULL)
1918 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001919 nbdebug((" invalid buffer identifier in saveDone\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001920 }
1921 else
1922 print_save_msg(buf, savedChars);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001923/* =====================================================================*/
1924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 else if (streq((char *)cmd, "startDocumentListen"))
1926 {
1927 if (buf == NULL)
1928 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001929 nbdebug((" invalid buffer identifier in startDocumentListen\n"));
1930 EMSG("E637: invalid buffer identifier in startDocumentListen");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 return FAIL;
1932 }
1933 buf->fireChanges = 1;
1934/* =====================================================================*/
1935 }
1936 else if (streq((char *)cmd, "stopDocumentListen"))
1937 {
1938 if (buf == NULL)
1939 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001940 nbdebug((" invalid buffer identifier in stopDocumentListen\n"));
1941 EMSG("E638: invalid buffer identifier in stopDocumentListen");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 return FAIL;
1943 }
1944 buf->fireChanges = 0;
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001945 if (buf->bufp != NULL && buf->bufp->b_was_netbeans_file)
Bram Moolenaar009b2592004-10-24 19:18:58 +00001946 {
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001947 if (!buf->bufp->b_netbeans_file)
Bram Moolenaarf2330482008-06-24 20:19:36 +00001948 {
1949 nbdebug(("E658: NetBeans connection lost for buffer %ld\n", buf->bufp->b_fnum));
Bram Moolenaar009b2592004-10-24 19:18:58 +00001950 EMSGN(_("E658: NetBeans connection lost for buffer %ld"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 buf->bufp->b_fnum);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001952 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001953 else
1954 {
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001955 /* NetBeans uses stopDocumentListen when it stops editing
1956 * a file. It then expects the buffer in Vim to
1957 * disappear. */
1958 do_bufdel(DOBUF_DEL, (char_u *)"", 1,
1959 buf->bufp->b_fnum, buf->bufp->b_fnum, TRUE);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001960 vim_memset(buf, 0, sizeof(nbbuf_T));
1961 }
1962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963/* =====================================================================*/
1964 }
1965 else if (streq((char *)cmd, "setTitle"))
1966 {
1967 if (buf == NULL)
1968 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001969 nbdebug((" invalid buffer identifier in setTitle\n"));
1970 EMSG("E639: invalid buffer identifier in setTitle");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 return FAIL;
1972 }
1973 vim_free(buf->displayname);
1974 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975/* =====================================================================*/
1976 }
1977 else if (streq((char *)cmd, "initDone"))
1978 {
1979 if (buf == NULL || buf->bufp == NULL)
1980 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001981 nbdebug((" invalid buffer identifier in initDone\n"));
1982 EMSG("E640: invalid buffer identifier in initDone");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 return FAIL;
1984 }
1985 doupdate = 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001986 buf->initDone = TRUE;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001987 nb_set_curbuf(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988#if defined(FEAT_AUTOCMD)
1989 apply_autocmds(EVENT_BUFREADPOST, 0, 0, FALSE, buf->bufp);
1990#endif
1991
1992 /* handle any postponed key commands */
1993 handle_key_queue();
1994/* =====================================================================*/
1995 }
1996 else if (streq((char *)cmd, "setBufferNumber")
1997 || streq((char *)cmd, "putBufferNumber"))
1998 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00001999 char_u *path;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 buf_T *bufp;
2001
2002 if (buf == NULL)
2003 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002004 nbdebug((" invalid buffer identifier in setBufferNumber\n"));
2005 EMSG("E641: invalid buffer identifier in setBufferNumber");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 return FAIL;
2007 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002008 path = (char_u *)nb_unquote(args, NULL);
2009 if (path == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 return FAIL;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002011 bufp = buflist_findname(path);
2012 vim_free(path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 if (bufp == NULL)
2014 {
Bram Moolenaarec906222009-02-21 21:14:00 +00002015 nbdebug((" File %s not found in setBufferNumber\n", args));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 EMSG2("E642: File %s not found in setBufferNumber", args);
2017 return FAIL;
2018 }
2019 buf->bufp = bufp;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002020 buf->nbbuf_number = bufp->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021
2022 /* "setBufferNumber" has the side effect of jumping to the buffer
2023 * (don't know why!). Don't do that for "putBufferNumber". */
2024 if (*cmd != 'p')
2025 coloncmd(":buffer %d", bufp->b_fnum);
2026 else
2027 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002028 buf->initDone = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029
2030 /* handle any postponed key commands */
2031 handle_key_queue();
2032 }
2033
2034#if 0 /* never used */
2035 buf->internalname = (char *)alloc_clear(8);
2036 sprintf(buf->internalname, "<%d>", bufno);
2037 buf->netbeansOwns = 0;
2038#endif
2039/* =====================================================================*/
2040 }
2041 else if (streq((char *)cmd, "setFullName"))
2042 {
2043 if (buf == NULL)
2044 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002045 nbdebug((" invalid buffer identifier in setFullName\n"));
2046 EMSG("E643: invalid buffer identifier in setFullName");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 return FAIL;
2048 }
2049 vim_free(buf->displayname);
2050 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051
2052 netbeansReadFile = 0; /* don't try to open disk file */
2053 do_ecmd(0, (char_u *)buf->displayname, 0, 0, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002054 ECMD_HIDE + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 netbeansReadFile = 1;
2056 buf->bufp = curbuf;
2057 maketitle();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002058#if defined(FEAT_MENU) && defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 gui_update_menus(0);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002060#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061/* =====================================================================*/
2062 }
2063 else if (streq((char *)cmd, "editFile"))
2064 {
2065 if (buf == NULL)
2066 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002067 nbdebug((" invalid buffer identifier in editFile\n"));
2068 EMSG("E644: invalid buffer identifier in editFile");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 return FAIL;
2070 }
2071 /* Edit a file: like create + setFullName + read the file. */
2072 vim_free(buf->displayname);
2073 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 do_ecmd(0, (char_u *)buf->displayname, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002075 ECMD_HIDE + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 buf->bufp = curbuf;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002077 buf->initDone = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 doupdate = 1;
2079#if defined(FEAT_TITLE)
2080 maketitle();
2081#endif
Bram Moolenaar67c53842010-05-22 18:28:27 +02002082#if defined(FEAT_MENU) && defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 gui_update_menus(0);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002084#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085/* =====================================================================*/
2086 }
2087 else if (streq((char *)cmd, "setVisible"))
2088 {
2089 if (buf == NULL || buf->bufp == NULL)
2090 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002091 nbdebug((" invalid buffer identifier in setVisible\n"));
2092 /* This message was commented out, probably because it can
2093 * happen when shutting down. */
2094 if (p_verbose > 0)
2095 EMSG("E645: invalid buffer identifier in setVisible");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 return FAIL;
2097 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002098 if (streq((char *)args, "T") && buf->bufp != curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 {
2100 exarg_T exarg;
2101 exarg.cmd = (char_u *)"goto";
2102 exarg.forceit = FALSE;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002103 dosetvisible = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 goto_buffer(&exarg, DOBUF_FIRST, FORWARD, buf->bufp->b_fnum);
2105 doupdate = 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002106 dosetvisible = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107
Bram Moolenaar67c53842010-05-22 18:28:27 +02002108#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 /* Side effect!!!. */
2110 if (!gui.starting)
2111 gui_mch_set_foreground();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002112#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114/* =====================================================================*/
2115 }
2116 else if (streq((char *)cmd, "raise"))
2117 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002118#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 /* Bring gvim to the foreground. */
2120 if (!gui.starting)
2121 gui_mch_set_foreground();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002122#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123/* =====================================================================*/
2124 }
2125 else if (streq((char *)cmd, "setModified"))
2126 {
Bram Moolenaarff064e12008-06-09 13:10:45 +00002127 int prev_b_changed;
2128
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 if (buf == NULL || buf->bufp == NULL)
2130 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002131 nbdebug((" invalid buffer identifier in setModified\n"));
2132 /* This message was commented out, probably because it can
2133 * happen when shutting down. */
2134 if (p_verbose > 0)
2135 EMSG("E646: invalid buffer identifier in setModified");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 return FAIL;
2137 }
Bram Moolenaarff064e12008-06-09 13:10:45 +00002138 prev_b_changed = buf->bufp->b_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 if (streq((char *)args, "T"))
Bram Moolenaarff064e12008-06-09 13:10:45 +00002140 buf->bufp->b_changed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 else
2142 {
2143 struct stat st;
2144
2145 /* Assume NetBeans stored the file. Reset the timestamp to
2146 * avoid "file changed" warnings. */
2147 if (buf->bufp->b_ffname != NULL
2148 && mch_stat((char *)buf->bufp->b_ffname, &st) >= 0)
2149 buf_store_time(buf->bufp, &st, buf->bufp->b_ffname);
Bram Moolenaarff064e12008-06-09 13:10:45 +00002150 buf->bufp->b_changed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 }
2152 buf->modified = buf->bufp->b_changed;
Bram Moolenaarff064e12008-06-09 13:10:45 +00002153 if (prev_b_changed != buf->bufp->b_changed)
2154 {
2155#ifdef FEAT_WINDOWS
2156 check_status(buf->bufp);
2157 redraw_tabline = TRUE;
2158#endif
2159#ifdef FEAT_TITLE
2160 maketitle();
2161#endif
2162 update_screen(0);
2163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164/* =====================================================================*/
2165 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002166 else if (streq((char *)cmd, "setModtime"))
2167 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002168 if (buf == NULL || buf->bufp == NULL)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002169 nbdebug((" invalid buffer identifier in setModtime\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002170 else
2171 buf->bufp->b_mtime = atoi((char *)args);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002172/* =====================================================================*/
2173 }
2174 else if (streq((char *)cmd, "setReadOnly"))
2175 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002176 if (buf == NULL || buf->bufp == NULL)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002177 nbdebug((" invalid buffer identifier in setReadOnly\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002178 else if (streq((char *)args, "T"))
Bram Moolenaar009b2592004-10-24 19:18:58 +00002179 buf->bufp->b_p_ro = TRUE;
2180 else
2181 buf->bufp->b_p_ro = FALSE;
2182/* =====================================================================*/
2183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 else if (streq((char *)cmd, "setMark"))
2185 {
2186 /* not yet */
2187/* =====================================================================*/
2188 }
2189 else if (streq((char *)cmd, "showBalloon"))
2190 {
2191#if defined(FEAT_BEVAL)
2192 static char *text = NULL;
2193
2194 /*
2195 * Set up the Balloon Expression Evaluation area.
2196 * Ignore 'ballooneval' here.
2197 * The text pointer must remain valid for a while.
2198 */
2199 if (balloonEval != NULL)
2200 {
2201 vim_free(text);
2202 text = nb_unquote(args, NULL);
2203 if (text != NULL)
2204 gui_mch_post_balloon(balloonEval, (char_u *)text);
2205 }
2206#endif
2207/* =====================================================================*/
2208 }
2209 else if (streq((char *)cmd, "setDot"))
2210 {
2211 pos_T *pos;
2212#ifdef NBDEBUG
2213 char_u *s;
2214#endif
2215
2216 if (buf == NULL || buf->bufp == NULL)
2217 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002218 nbdebug((" invalid buffer identifier in setDot\n"));
2219 EMSG("E647: invalid buffer identifier in setDot");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 return FAIL;
2221 }
2222
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002223 nb_set_curbuf(buf->bufp);
2224
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225#ifdef FEAT_VISUAL
2226 /* Don't want Visual mode now. */
2227 if (VIsual_active)
2228 end_visual_mode();
2229#endif
2230#ifdef NBDEBUG
2231 s = args;
2232#endif
2233 pos = get_off_or_lnum(buf->bufp, &args);
2234 if (pos)
2235 {
2236 curwin->w_cursor = *pos;
2237 check_cursor();
2238#ifdef FEAT_FOLDING
2239 foldOpenCursor();
2240#endif
2241 }
2242 else
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002243 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 nbdebug((" BAD POSITION in setDot: %s\n", s));
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246
2247 /* gui_update_cursor(TRUE, FALSE); */
2248 /* update_curbuf(NOT_VALID); */
2249 update_topline(); /* scroll to show the line */
2250 update_screen(VALID);
2251 setcursor();
2252 out_flush();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002253#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 gui_update_cursor(TRUE, FALSE);
2255 gui_mch_flush();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002256#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 /* Quit a hit-return or more prompt. */
2258 if (State == HITRETURN || State == ASKMORE)
2259 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260#ifdef FEAT_GUI_GTK
2261 if (gtk_main_level() > 0)
2262 gtk_main_quit();
2263#endif
2264 }
2265/* =====================================================================*/
2266 }
2267 else if (streq((char *)cmd, "close"))
2268 {
2269#ifdef NBDEBUG
2270 char *name = "<NONE>";
2271#endif
2272
2273 if (buf == NULL)
2274 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002275 nbdebug((" invalid buffer identifier in close\n"));
2276 EMSG("E648: invalid buffer identifier in close");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 return FAIL;
2278 }
2279
2280#ifdef NBDEBUG
2281 if (buf->displayname != NULL)
2282 name = buf->displayname;
2283#endif
Bram Moolenaarf2330482008-06-24 20:19:36 +00002284 if (buf->bufp == NULL)
2285 {
2286 nbdebug((" invalid buffer identifier in close\n"));
2287 /* This message was commented out, probably because it can
2288 * happen when shutting down. */
2289 if (p_verbose > 0)
2290 EMSG("E649: invalid buffer identifier in close");
2291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 nbdebug((" CLOSE %d: %s\n", bufno, name));
Bram Moolenaar67c53842010-05-22 18:28:27 +02002293#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 need_mouse_correct = TRUE;
Bram Moolenaar67c53842010-05-22 18:28:27 +02002295#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 if (buf->bufp != NULL)
2297 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD,
2298 buf->bufp->b_fnum, TRUE);
Bram Moolenaar8d602722006-08-08 19:34:19 +00002299 buf->bufp = NULL;
2300 buf->initDone = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 doupdate = 1;
2302/* =====================================================================*/
2303 }
2304 else if (streq((char *)cmd, "setStyle")) /* obsolete... */
2305 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002306 nbdebug((" setStyle is obsolete!\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307/* =====================================================================*/
2308 }
2309 else if (streq((char *)cmd, "setExitDelay"))
2310 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002311 /* Only used in version 2.1. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312/* =====================================================================*/
2313 }
2314 else if (streq((char *)cmd, "defineAnnoType"))
2315 {
2316#ifdef FEAT_SIGNS
2317 int typeNum;
2318 char_u *typeName;
2319 char_u *tooltip;
2320 char_u *p;
2321 char_u *glyphFile;
Bram Moolenaar67c53842010-05-22 18:28:27 +02002322 int parse_error = FALSE;
2323 char_u *fg;
2324 char_u *bg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325
2326 if (buf == NULL)
2327 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002328 nbdebug((" invalid buffer identifier in defineAnnoType\n"));
2329 EMSG("E650: invalid buffer identifier in defineAnnoType");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 return FAIL;
2331 }
2332
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002333 cp = (char *)args;
2334 typeNum = strtol(cp, &cp, 10);
2335 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 args = skipwhite(args);
2337 typeName = (char_u *)nb_unquote(args, &args);
2338 args = skipwhite(args + 1);
2339 tooltip = (char_u *)nb_unquote(args, &args);
2340 args = skipwhite(args + 1);
2341
2342 p = (char_u *)nb_unquote(args, &args);
2343 glyphFile = vim_strsave_escaped(p, escape_chars);
2344 vim_free(p);
2345
2346 args = skipwhite(args + 1);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002347 p = skiptowhite(args);
2348 if (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002350 *p = NUL;
2351 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02002353 fg = vim_strsave(args);
2354 bg = vim_strsave(p);
2355 if (STRLEN(fg) > MAX_COLOR_LENGTH || STRLEN(bg) > MAX_COLOR_LENGTH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002357 EMSG("E532: highlighting color name too long in defineAnnoType");
2358 vim_free(typeName);
2359 parse_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02002361 else if (typeName != NULL && tooltip != NULL && glyphFile != NULL)
2362 addsigntype(buf, typeNum, typeName, tooltip, glyphFile, fg, bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 else
2364 vim_free(typeName);
2365
2366 /* don't free typeName; it's used directly in addsigntype() */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002367 vim_free(fg);
2368 vim_free(bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 vim_free(tooltip);
2370 vim_free(glyphFile);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002371 if (parse_error)
2372 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373
2374#endif
2375/* =====================================================================*/
2376 }
2377 else if (streq((char *)cmd, "addAnno"))
2378 {
2379#ifdef FEAT_SIGNS
2380 int serNum;
2381 int localTypeNum;
2382 int typeNum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 pos_T *pos;
2384
2385 if (buf == NULL || buf->bufp == NULL)
2386 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002387 nbdebug((" invalid buffer identifier in addAnno\n"));
2388 EMSG("E651: invalid buffer identifier in addAnno");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 return FAIL;
2390 }
2391
2392 doupdate = 1;
2393
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002394 cp = (char *)args;
2395 serNum = strtol(cp, &cp, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396
2397 /* Get the typenr specific for this buffer and convert it to
2398 * the global typenumber, as used for the sign name. */
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002399 localTypeNum = strtol(cp, &cp, 10);
2400 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 typeNum = mapsigntype(buf, localTypeNum);
2402
2403 pos = get_off_or_lnum(buf->bufp, &args);
2404
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002405 cp = (char *)args;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002406 ignored = (int)strtol(cp, &cp, 10);
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002407 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408# ifdef NBDEBUG
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002409 if (ignored != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002411 nbdebug((" partial line annotation -- Not Yet Implemented!\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 }
2413# endif
2414 if (serNum >= GUARDEDOFFSET)
2415 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002416 nbdebug((" too many annotations! ignoring...\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 return FAIL;
2418 }
2419 if (pos)
2420 {
Bram Moolenaarec906222009-02-21 21:14:00 +00002421 coloncmd(":sign place %d line=%ld name=%d buffer=%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 serNum, pos->lnum, typeNum, buf->bufp->b_fnum);
2423 if (typeNum == curPCtype)
2424 coloncmd(":sign jump %d buffer=%d", serNum,
2425 buf->bufp->b_fnum);
2426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427#endif
2428/* =====================================================================*/
2429 }
2430 else if (streq((char *)cmd, "removeAnno"))
2431 {
2432#ifdef FEAT_SIGNS
2433 int serNum;
2434
2435 if (buf == NULL || buf->bufp == NULL)
2436 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002437 nbdebug((" invalid buffer identifier in removeAnno\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 return FAIL;
2439 }
2440 doupdate = 1;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002441 cp = (char *)args;
2442 serNum = strtol(cp, &cp, 10);
2443 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 coloncmd(":sign unplace %d buffer=%d",
2445 serNum, buf->bufp->b_fnum);
2446 redraw_buf_later(buf->bufp, NOT_VALID);
2447#endif
2448/* =====================================================================*/
2449 }
2450 else if (streq((char *)cmd, "moveAnnoToFront"))
2451 {
2452#ifdef FEAT_SIGNS
Bram Moolenaarf2330482008-06-24 20:19:36 +00002453 nbdebug((" moveAnnoToFront: Not Yet Implemented!\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454#endif
2455/* =====================================================================*/
2456 }
2457 else if (streq((char *)cmd, "guard") || streq((char *)cmd, "unguard"))
2458 {
2459 int len;
2460 pos_T first;
2461 pos_T last;
2462 pos_T *pos;
2463 int un = (cmd[0] == 'u');
2464 static int guardId = GUARDEDOFFSET;
2465
2466 if (skip >= SKIP_STOP)
2467 {
2468 nbdebug((" Skipping %s command\n", (char *) cmd));
2469 return OK;
2470 }
2471
2472 nb_init_graphics();
2473
2474 if (buf == NULL || buf->bufp == NULL)
2475 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002476 nbdebug((" invalid buffer identifier in %s command\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 return FAIL;
2478 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002479 nb_set_curbuf(buf->bufp);
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002480 cp = (char *)args;
2481 off = strtol(cp, &cp, 10);
2482 len = strtol(cp, NULL, 10);
2483 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 pos = off2pos(buf->bufp, off);
2485 doupdate = 1;
2486 if (!pos)
2487 nbdebug((" no such start pos in %s, %ld\n", cmd, off));
2488 else
2489 {
2490 first = *pos;
2491 pos = off2pos(buf->bufp, off + len - 1);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002492 if (pos != NULL && pos->col == 0)
2493 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 /*
2495 * In Java Swing the offset is a position between 2
2496 * characters. If col == 0 then we really want the
2497 * previous line as the end.
2498 */
2499 pos = off2pos(buf->bufp, off + len - 2);
2500 }
2501 if (!pos)
2502 nbdebug((" no such end pos in %s, %ld\n",
2503 cmd, off + len - 1));
2504 else
2505 {
2506 long lnum;
2507 last = *pos;
2508 /* set highlight for region */
2509 nbdebug((" %sGUARD %ld,%d to %ld,%d\n", (un) ? "UN" : "",
2510 first.lnum, first.col,
2511 last.lnum, last.col));
2512#ifdef FEAT_SIGNS
2513 for (lnum = first.lnum; lnum <= last.lnum; lnum++)
2514 {
2515 if (un)
2516 {
2517 /* never used */
2518 }
2519 else
2520 {
2521 if (buf_findsigntype_id(buf->bufp, lnum,
2522 GUARDED) == 0)
2523 {
2524 coloncmd(
Bram Moolenaarec906222009-02-21 21:14:00 +00002525 ":sign place %d line=%ld name=%d buffer=%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 guardId++, lnum, GUARDED,
2527 buf->bufp->b_fnum);
2528 }
2529 }
2530 }
2531#endif
2532 redraw_buf_later(buf->bufp, NOT_VALID);
2533 }
2534 }
2535/* =====================================================================*/
2536 }
2537 else if (streq((char *)cmd, "startAtomic"))
2538 {
2539 inAtomic = 1;
2540/* =====================================================================*/
2541 }
2542 else if (streq((char *)cmd, "endAtomic"))
2543 {
2544 inAtomic = 0;
2545 if (needupdate)
2546 {
2547 doupdate = 1;
2548 needupdate = 0;
2549 }
2550/* =====================================================================*/
2551 }
2552 else if (streq((char *)cmd, "save"))
2553 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002554 /*
2555 * NOTE - This command is obsolete wrt NetBeans. Its left in
2556 * only for historical reasons.
2557 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 if (buf == NULL || buf->bufp == NULL)
2559 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002560 nbdebug((" invalid buffer identifier in %s command\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 return FAIL;
2562 }
2563
2564 /* the following is taken from ex_cmds.c (do_wqall function) */
2565 if (bufIsChanged(buf->bufp))
2566 {
2567 /* Only write if the buffer can be written. */
2568 if (p_write
2569 && !buf->bufp->b_p_ro
2570 && buf->bufp->b_ffname != NULL
2571#ifdef FEAT_QUICKFIX
2572 && !bt_dontwrite(buf->bufp)
2573#endif
2574 )
2575 {
2576 buf_write_all(buf->bufp, FALSE);
2577#ifdef FEAT_AUTOCMD
2578 /* an autocommand may have deleted the buffer */
2579 if (!buf_valid(buf->bufp))
2580 buf->bufp = NULL;
2581#endif
2582 }
2583 }
Bram Moolenaarf2330482008-06-24 20:19:36 +00002584 else
2585 {
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002586 nbdebug((" Buffer has no changes!\n"));
Bram Moolenaarf2330482008-06-24 20:19:36 +00002587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588/* =====================================================================*/
2589 }
2590 else if (streq((char *)cmd, "netbeansBuffer"))
2591 {
2592 if (buf == NULL || buf->bufp == NULL)
2593 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002594 nbdebug((" invalid buffer identifier in %s command\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 return FAIL;
2596 }
2597 if (*args == 'T')
2598 {
2599 buf->bufp->b_netbeans_file = TRUE;
2600 buf->bufp->b_was_netbeans_file = TRUE;
2601 }
2602 else
2603 buf->bufp->b_netbeans_file = FALSE;
2604/* =====================================================================*/
2605 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002606 else if (streq((char *)cmd, "specialKeys"))
2607 {
2608 special_keys(args);
2609/* =====================================================================*/
2610 }
2611 else if (streq((char *)cmd, "actionMenuItem"))
2612 {
2613 /* not used yet */
2614/* =====================================================================*/
2615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 else if (streq((char *)cmd, "version"))
2617 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002618 /* not used yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 }
Bram Moolenaarf2330482008-06-24 20:19:36 +00002620 else
2621 {
2622 nbdebug(("Unrecognised command: %s\n", cmd));
2623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 /*
2625 * Unrecognized command is ignored.
2626 */
2627 }
2628 if (inAtomic && doupdate)
2629 {
2630 needupdate = 1;
2631 doupdate = 0;
2632 }
2633
Bram Moolenaar009b2592004-10-24 19:18:58 +00002634 /*
2635 * Is this needed? I moved the netbeans_Xt_connect() later during startup
2636 * and it may no longer be necessary. If its not needed then needupdate
2637 * and doupdate can also be removed.
2638 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 if (buf != NULL && buf->initDone && doupdate)
2640 {
2641 update_screen(NOT_VALID);
2642 setcursor();
2643 out_flush();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002644#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 gui_update_cursor(TRUE, FALSE);
2646 gui_mch_flush();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002647#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 /* Quit a hit-return or more prompt. */
2649 if (State == HITRETURN || State == ASKMORE)
2650 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651#ifdef FEAT_GUI_GTK
2652 if (gtk_main_level() > 0)
2653 gtk_main_quit();
2654#endif
2655 }
2656 }
2657
2658 return retval;
2659}
2660
2661
2662/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002663 * If "buf" is not the current buffer try changing to a window that edits this
2664 * buffer. If there is no such window then close the current buffer and set
2665 * the current buffer as "buf".
2666 */
2667 static void
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00002668nb_set_curbuf(buf_T *buf)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002669{
2670 if (curbuf != buf && buf_jump_open_win(buf) == NULL)
2671 set_curbuf(buf, DOBUF_GOTO);
2672}
2673
2674/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 * Process a vim colon command.
2676 */
2677 static void
2678coloncmd(char *cmd, ...)
2679{
2680 char buf[1024];
2681 va_list ap;
2682
2683 va_start(ap, cmd);
Bram Moolenaar0dc79e82009-06-24 14:50:12 +00002684 vim_vsnprintf(buf, sizeof(buf), cmd, ap, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 va_end(ap);
2686
2687 nbdebug((" COLONCMD %s\n", buf));
2688
2689/* ALT_INPUT_LOCK_ON; */
2690 do_cmdline((char_u *)buf, NULL, NULL, DOCMD_NOWAIT | DOCMD_KEYTYPED);
2691/* ALT_INPUT_LOCK_OFF; */
2692
2693 setcursor(); /* restore the cursor position */
2694 out_flush(); /* make sure output has been written */
2695
Bram Moolenaar67c53842010-05-22 18:28:27 +02002696#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 gui_update_cursor(TRUE, FALSE);
2698 gui_mch_flush();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002699#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700}
2701
2702
2703/*
Bram Moolenaar009b2592004-10-24 19:18:58 +00002704 * Parse the specialKeys argument and issue the appropriate map commands.
2705 */
2706 static void
2707special_keys(char_u *args)
2708{
2709 char *save_str = nb_unquote(args, NULL);
2710 char *tok = strtok(save_str, " ");
2711 char *sep;
2712 char keybuf[64];
2713 char cmdbuf[256];
2714
2715 while (tok != NULL)
2716 {
2717 int i = 0;
2718
2719 if ((sep = strchr(tok, '-')) != NULL)
2720 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002721 *sep = NUL;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002722 while (*tok)
2723 {
2724 switch (*tok)
2725 {
2726 case 'A':
2727 case 'M':
2728 case 'C':
2729 case 'S':
2730 keybuf[i++] = *tok;
2731 keybuf[i++] = '-';
2732 break;
2733 }
2734 tok++;
2735 }
2736 tok++;
2737 }
2738
2739 strcpy(&keybuf[i], tok);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002740 vim_snprintf(cmdbuf, sizeof(cmdbuf),
2741 "<silent><%s> :nbkey %s<CR>", keybuf, keybuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002742 do_map(0, (char_u *)cmdbuf, NORMAL, FALSE);
2743 tok = strtok(NULL, " ");
2744 }
2745 vim_free(save_str);
2746}
2747
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002748 void
2749ex_nbclose(eap)
2750 exarg_T *eap UNUSED;
2751{
2752 netbeans_close();
2753}
Bram Moolenaar009b2592004-10-24 19:18:58 +00002754
2755 void
2756ex_nbkey(eap)
2757 exarg_T *eap;
2758{
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002759 (void)netbeans_keystring(eap->arg);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002760}
2761
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002762 void
2763ex_nbstart(eap)
2764 exarg_T *eap;
2765{
2766 netbeans_open((char *)eap->arg, FALSE);
2767}
Bram Moolenaar009b2592004-10-24 19:18:58 +00002768
2769/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 * Initialize highlights and signs for use by netbeans (mostly obsolete)
2771 */
2772 static void
2773nb_init_graphics(void)
2774{
2775 static int did_init = FALSE;
2776
2777 if (!did_init)
2778 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002779 coloncmd(":highlight NBGuarded guibg=Cyan guifg=Black"
2780 " ctermbg=LightCyan ctermfg=Black");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 coloncmd(":sign define %d linehl=NBGuarded", GUARDED);
2782
2783 did_init = TRUE;
2784 }
2785}
2786
2787/*
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002788 * Convert key to netbeans name. This uses the global "mod_mask".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 */
2790 static void
2791netbeans_keyname(int key, char *buf)
2792{
2793 char *name = 0;
2794 char namebuf[2];
2795 int ctrl = 0;
2796 int shift = 0;
2797 int alt = 0;
2798
2799 if (mod_mask & MOD_MASK_CTRL)
2800 ctrl = 1;
2801 if (mod_mask & MOD_MASK_SHIFT)
2802 shift = 1;
2803 if (mod_mask & MOD_MASK_ALT)
2804 alt = 1;
2805
2806
2807 switch (key)
2808 {
2809 case K_F1: name = "F1"; break;
2810 case K_S_F1: name = "F1"; shift = 1; break;
2811 case K_F2: name = "F2"; break;
2812 case K_S_F2: name = "F2"; shift = 1; break;
2813 case K_F3: name = "F3"; break;
2814 case K_S_F3: name = "F3"; shift = 1; break;
2815 case K_F4: name = "F4"; break;
2816 case K_S_F4: name = "F4"; shift = 1; break;
2817 case K_F5: name = "F5"; break;
2818 case K_S_F5: name = "F5"; shift = 1; break;
2819 case K_F6: name = "F6"; break;
2820 case K_S_F6: name = "F6"; shift = 1; break;
2821 case K_F7: name = "F7"; break;
2822 case K_S_F7: name = "F7"; shift = 1; break;
2823 case K_F8: name = "F8"; break;
2824 case K_S_F8: name = "F8"; shift = 1; break;
2825 case K_F9: name = "F9"; break;
2826 case K_S_F9: name = "F9"; shift = 1; break;
2827 case K_F10: name = "F10"; break;
2828 case K_S_F10: name = "F10"; shift = 1; break;
2829 case K_F11: name = "F11"; break;
2830 case K_S_F11: name = "F11"; shift = 1; break;
2831 case K_F12: name = "F12"; break;
2832 case K_S_F12: name = "F12"; shift = 1; break;
2833 default:
2834 if (key >= ' ' && key <= '~')
2835 {
2836 /* Allow ASCII characters. */
2837 name = namebuf;
2838 namebuf[0] = key;
2839 namebuf[1] = NUL;
2840 }
2841 else
2842 name = "X";
2843 break;
2844 }
2845
2846 buf[0] = '\0';
2847 if (ctrl)
2848 strcat(buf, "C");
2849 if (shift)
2850 strcat(buf, "S");
2851 if (alt)
2852 strcat(buf, "M"); /* META */
2853 if (ctrl || shift || alt)
2854 strcat(buf, "-");
2855 strcat(buf, name);
2856}
2857
Bram Moolenaar67c53842010-05-22 18:28:27 +02002858#if defined(FEAT_BEVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859/*
2860 * Function to be called for balloon evaluation. Grabs the text under the
2861 * cursor and sends it to the debugger for evaluation. The debugger should
2862 * respond with a showBalloon command when there is a useful result.
2863 */
Bram Moolenaard62bec82005-03-07 22:56:57 +00002864 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865netbeans_beval_cb(
2866 BalloonEval *beval,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002867 int state UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868{
Bram Moolenaard62bec82005-03-07 22:56:57 +00002869 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 char_u *text;
Bram Moolenaard62bec82005-03-07 22:56:57 +00002871 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 int col;
2873 char buf[MAXPATHL * 2 + 25];
2874 char_u *p;
2875
2876 /* Don't do anything when 'ballooneval' is off, messages scrolled the
2877 * windows up or we have no connection. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002878 if (!p_beval || msg_scrolled > 0 || !NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 return;
2880
Bram Moolenaard62bec82005-03-07 22:56:57 +00002881 if (get_beval_info(beval, TRUE, &wp, &lnum, &text, &col) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 {
2883 /* Send debugger request. Only when the text is of reasonable
2884 * length. */
2885 if (text != NULL && text[0] != NUL && STRLEN(text) < MAXPATHL)
2886 {
2887 p = nb_quote(text);
2888 if (p != NULL)
Bram Moolenaar009b2592004-10-24 19:18:58 +00002889 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002890 vim_snprintf(buf, sizeof(buf),
Bram Moolenaar89d40322006-08-29 15:30:07 +00002891 "0:balloonText=%d \"%s\"\n", r_cmdno, p);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002892 vim_free(p);
2893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 nbdebug(("EVT: %s", buf));
2895 nb_send(buf, "netbeans_beval_cb");
2896 }
2897 vim_free(text);
2898 }
2899}
2900#endif
2901
2902/*
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002903 * Return TRUE when the netbeans connection is closed.
2904 */
2905 int
2906netbeans_active(void)
2907{
2908 return NETBEANS_OPEN;
2909}
2910
2911/*
Bram Moolenaar67c53842010-05-22 18:28:27 +02002912 * Return netbeans file descriptor.
2913 */
2914 int
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002915netbeans_filedesc(void)
Bram Moolenaar67c53842010-05-22 18:28:27 +02002916{
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002917 return nbsock;
Bram Moolenaar67c53842010-05-22 18:28:27 +02002918}
2919
2920#if defined(FEAT_GUI) || defined(PROTO)
2921/*
2922 * Register our file descriptor with the gui event handling system.
2923 */
2924 void
2925netbeans_gui_register(void)
2926{
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002927 if (!NB_HAS_GUI || !NETBEANS_OPEN)
Bram Moolenaar67c53842010-05-22 18:28:27 +02002928 return;
2929
Bram Moolenaar67c53842010-05-22 18:28:27 +02002930# ifdef FEAT_GUI_MOTIF
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002931 /* tell notifier we are interested in being called
2932 * when there is input on the editor connection socket
2933 */
2934 if (inputHandler == (XtInputId)NULL)
2935 inputHandler = XtAppAddInput((XtAppContext)app_context, nbsock,
2936 (XtPointer)(XtInputReadMask + XtInputExceptMask),
2937 messageFromNetbeans, NULL);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002938# else
2939# ifdef FEAT_GUI_GTK
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002940 /*
2941 * Tell gdk we are interested in being called when there
2942 * is input on the editor connection socket
2943 */
2944 if (inputHandler == 0)
2945 inputHandler = gdk_input_add((gint)nbsock, (GdkInputCondition)
2946 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
2947 messageFromNetbeans, NULL);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002948# else
2949# ifdef FEAT_GUI_W32
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002950 /*
2951 * Tell Windows we are interested in receiving message when there
2952 * is input on the editor connection socket
2953 */
2954 if (inputHandler == -1)
2955 inputHandler = WSAAsyncSelect(nbsock, s_hwnd, WM_NETBEANS, FD_READ);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002956# endif
2957# endif
2958# endif
Bram Moolenaar67c53842010-05-22 18:28:27 +02002959
2960# ifdef FEAT_BEVAL
2961 bevalServers |= BEVAL_NETBEANS;
2962# endif
2963}
2964#endif
2965
2966/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 * Tell netbeans that the window was opened, ready for commands.
2968 */
2969 void
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002970netbeans_open(char *params, int abort)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971{
2972 char *cmd = "0:startupDone=0\n";
2973
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002974 if (NETBEANS_OPEN)
2975 {
2976 EMSG(_("E511: netbeans already connected"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002980 if (netbeans_connect(params, abort) != OK)
Bram Moolenaar67c53842010-05-22 18:28:27 +02002981 return;
2982#ifdef FEAT_GUI
2983 netbeans_gui_register();
Bram Moolenaard62bec82005-03-07 22:56:57 +00002984#endif
2985
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 nbdebug(("EVT: %s", cmd));
2987 nb_send(cmd, "netbeans_startup_done");
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002988
2989 /* update the screen after having added the gutter */
2990 changed_window_setting();
2991 update_screen(CLEAR);
2992 setcursor();
2993 out_flush();
2994#ifdef FEAT_GUI
2995 gui_update_cursor(TRUE, FALSE);
2996 gui_mch_flush();
2997#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998}
2999
Bram Moolenaar009b2592004-10-24 19:18:58 +00003000/*
3001 * Tell netbeans that we're exiting. This should be called right
3002 * before calling exit.
3003 */
3004 void
3005netbeans_send_disconnect()
3006{
3007 char buf[128];
3008
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003009 if (NETBEANS_OPEN)
Bram Moolenaar009b2592004-10-24 19:18:58 +00003010 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00003011 sprintf(buf, "0:disconnect=%d\n", r_cmdno);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003012 nbdebug(("EVT: %s", buf));
3013 nb_send(buf, "netbeans_disconnect");
3014 }
3015}
3016
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_W32) || defined(PROTO)
3018/*
3019 * Tell netbeans that the window was moved or resized.
3020 */
3021 void
3022netbeans_frame_moved(int new_x, int new_y)
3023{
3024 char buf[128];
3025
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003026 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 return;
3028
3029 sprintf(buf, "0:geometry=%d %d %d %d %d\n",
Bram Moolenaar89d40322006-08-29 15:30:07 +00003030 r_cmdno, (int)Columns, (int)Rows, new_x, new_y);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003031 /*nbdebug(("EVT: %s", buf)); happens too many times during a move */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 nb_send(buf, "netbeans_frame_moved");
3033}
3034#endif
3035
3036/*
Bram Moolenaar009b2592004-10-24 19:18:58 +00003037 * Tell netbeans the user opened or activated a file.
3038 */
3039 void
3040netbeans_file_activated(buf_T *bufp)
3041{
3042 int bufno = nb_getbufno(bufp);
3043 nbbuf_T *bp = nb_get_buf(bufno);
3044 char buffer[2*MAXPATHL];
3045 char_u *q;
3046
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003047 if (!NETBEANS_OPEN || !bufp->b_netbeans_file || dosetvisible)
Bram Moolenaar009b2592004-10-24 19:18:58 +00003048 return;
3049
3050 q = nb_quote(bufp->b_ffname);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003051 if (q == NULL || bp == NULL)
Bram Moolenaar009b2592004-10-24 19:18:58 +00003052 return;
3053
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003054 vim_snprintf(buffer, sizeof(buffer), "%d:fileOpened=%d \"%s\" %s %s\n",
Bram Moolenaar009b2592004-10-24 19:18:58 +00003055 bufno,
3056 bufno,
3057 (char *)q,
3058 "T", /* open in NetBeans */
3059 "F"); /* modified */
3060
3061 vim_free(q);
3062 nbdebug(("EVT: %s", buffer));
3063
3064 nb_send(buffer, "netbeans_file_opened");
3065}
3066
3067/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 * Tell netbeans the user opened a file.
3069 */
3070 void
Bram Moolenaar009b2592004-10-24 19:18:58 +00003071netbeans_file_opened(buf_T *bufp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072{
Bram Moolenaar009b2592004-10-24 19:18:58 +00003073 int bufno = nb_getbufno(bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 char buffer[2*MAXPATHL];
3075 char_u *q;
Bram Moolenaar009b2592004-10-24 19:18:58 +00003076 nbbuf_T *bp = nb_get_buf(nb_getbufno(bufp));
3077 int bnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003079 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 return;
3081
Bram Moolenaar009b2592004-10-24 19:18:58 +00003082 q = nb_quote(bufp->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 if (q == NULL)
3084 return;
Bram Moolenaar009b2592004-10-24 19:18:58 +00003085 if (bp != NULL)
3086 bnum = bufno;
3087 else
3088 bnum = 0;
3089
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003090 vim_snprintf(buffer, sizeof(buffer), "%d:fileOpened=%d \"%s\" %s %s\n",
Bram Moolenaar009b2592004-10-24 19:18:58 +00003091 bnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 0,
3093 (char *)q,
3094 "T", /* open in NetBeans */
3095 "F"); /* modified */
3096
3097 vim_free(q);
3098 nbdebug(("EVT: %s", buffer));
3099
3100 nb_send(buffer, "netbeans_file_opened");
Bram Moolenaar009b2592004-10-24 19:18:58 +00003101 if (p_acd && vim_chdirfile(bufp->b_ffname) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 shorten_fnames(TRUE);
3103}
3104
3105/*
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00003106 * Tell netbeans that a file was deleted or wiped out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 */
3108 void
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00003109netbeans_file_killed(buf_T *bufp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110{
3111 int bufno = nb_getbufno(bufp);
3112 nbbuf_T *nbbuf = nb_get_buf(bufno);
3113 char buffer[2*MAXPATHL];
3114
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003115 if (!NETBEANS_OPEN || bufno == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 return;
3117
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00003118 nbdebug(("netbeans_file_killed:\n"));
3119 nbdebug((" Killing bufno: %d", bufno));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120
Bram Moolenaar89d40322006-08-29 15:30:07 +00003121 sprintf(buffer, "%d:killed=%d\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122
3123 nbdebug(("EVT: %s", buffer));
3124
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00003125 nb_send(buffer, "netbeans_file_killed");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126
3127 if (nbbuf != NULL)
3128 nbbuf->bufp = NULL;
3129}
3130
3131/*
3132 * Get a pointer to the Netbeans buffer for Vim buffer "bufp".
3133 * Return NULL if there is no such buffer or changes are not to be reported.
3134 * Otherwise store the buffer number in "*bufnop".
3135 */
3136 static nbbuf_T *
3137nb_bufp2nbbuf_fire(buf_T *bufp, int *bufnop)
3138{
3139 int bufno;
3140 nbbuf_T *nbbuf;
3141
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003142 if (!NETBEANS_OPEN || !netbeansFireChanges)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 return NULL; /* changes are not reported at all */
3144
3145 bufno = nb_getbufno(bufp);
3146 if (bufno <= 0)
3147 return NULL; /* file is not known to NetBeans */
3148
3149 nbbuf = nb_get_buf(bufno);
3150 if (nbbuf != NULL && !nbbuf->fireChanges)
3151 return NULL; /* changes in this buffer are not reported */
3152
3153 *bufnop = bufno;
3154 return nbbuf;
3155}
3156
3157/*
3158 * Tell netbeans the user inserted some text.
3159 */
3160 void
3161netbeans_inserted(
3162 buf_T *bufp,
3163 linenr_T linenr,
3164 colnr_T col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 char_u *txt,
3166 int newlen)
3167{
3168 char_u *buf;
3169 int bufno;
3170 nbbuf_T *nbbuf;
3171 pos_T pos;
3172 long off;
3173 char_u *p;
3174 char_u *newtxt;
3175
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003176 if (!NETBEANS_OPEN)
3177 return;
3178
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3180 if (nbbuf == NULL)
3181 return;
3182
Bram Moolenaar009b2592004-10-24 19:18:58 +00003183 /* Don't mark as modified for initial read */
3184 if (nbbuf->insertDone)
3185 nbbuf->modified = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186
3187 pos.lnum = linenr;
3188 pos.col = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 off = pos2off(bufp, &pos);
3190
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 /* send the "insert" EVT */
3192 newtxt = alloc(newlen + 1);
Bram Moolenaarb6356332005-07-18 21:40:44 +00003193 vim_strncpy(newtxt, txt, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 p = nb_quote(newtxt);
3195 if (p != NULL)
3196 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00003197 buf = alloc(128 + 2*newlen);
Bram Moolenaar89d40322006-08-29 15:30:07 +00003198 sprintf((char *)buf, "%d:insert=%d %ld \"%s\"\n",
3199 bufno, r_cmdno, off, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 nbdebug(("EVT: %s", buf));
3201 nb_send((char *)buf, "netbeans_inserted");
Bram Moolenaar009b2592004-10-24 19:18:58 +00003202 vim_free(p);
3203 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 vim_free(newtxt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206}
3207
3208/*
3209 * Tell netbeans some bytes have been removed.
3210 */
3211 void
3212netbeans_removed(
3213 buf_T *bufp,
3214 linenr_T linenr,
3215 colnr_T col,
3216 long len)
3217{
3218 char_u buf[128];
3219 int bufno;
3220 nbbuf_T *nbbuf;
3221 pos_T pos;
3222 long off;
3223
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003224 if (!NETBEANS_OPEN)
3225 return;
3226
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3228 if (nbbuf == NULL)
3229 return;
3230
3231 if (len < 0)
3232 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00003233 nbdebug(("Negative len %ld in netbeans_removed()!\n", len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 return;
3235 }
3236
3237 nbbuf->modified = 1;
3238
3239 pos.lnum = linenr;
3240 pos.col = col;
3241
3242 off = pos2off(bufp, &pos);
3243
Bram Moolenaar89d40322006-08-29 15:30:07 +00003244 sprintf((char *)buf, "%d:remove=%d %ld %ld\n", bufno, r_cmdno, off, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 nbdebug(("EVT: %s", buf));
3246 nb_send((char *)buf, "netbeans_removed");
3247}
3248
3249/*
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003250 * Send netbeans an unmodified command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003253netbeans_unmodified(buf_T *bufp UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254{
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003255 if (!NETBEANS_OPEN)
3256 return;
3257
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258#if 0
3259 char_u buf[128];
3260 int bufno;
3261 nbbuf_T *nbbuf;
3262
3263 /* This has been disabled, because NetBeans considers a buffer modified
3264 * even when all changes have been undone. */
3265 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3266 if (nbbuf == NULL)
3267 return;
3268
3269 nbbuf->modified = 0;
3270
Bram Moolenaar89d40322006-08-29 15:30:07 +00003271 sprintf((char *)buf, "%d:unmodified=%d\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 nbdebug(("EVT: %s", buf));
3273 nb_send((char *)buf, "netbeans_unmodified");
3274#endif
3275}
3276
3277/*
3278 * Send a button release event back to netbeans. Its up to netbeans
3279 * to decide what to do (if anything) with this event.
3280 */
3281 void
3282netbeans_button_release(int button)
3283{
3284 char buf[128];
3285 int bufno;
3286
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003287 if (!NETBEANS_OPEN)
3288 return;
3289
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 bufno = nb_getbufno(curbuf);
3291
3292 if (bufno >= 0 && curwin != NULL && curwin->w_buffer == curbuf)
3293 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003294 int col = mouse_col - W_WINCOL(curwin)
Bram Moolenaar67c53842010-05-22 18:28:27 +02003295 - ((curwin->w_p_nu || curwin->w_p_rnu) ? 9 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 long off = pos2off(curbuf, &curwin->w_cursor);
3297
3298 /* sync the cursor position */
Bram Moolenaar89d40322006-08-29 15:30:07 +00003299 sprintf(buf, "%d:newDotAndMark=%d %ld %ld\n", bufno, r_cmdno, off, off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 nbdebug(("EVT: %s", buf));
3301 nb_send(buf, "netbeans_button_release[newDotAndMark]");
3302
Bram Moolenaar89d40322006-08-29 15:30:07 +00003303 sprintf(buf, "%d:buttonRelease=%d %d %ld %d\n", bufno, r_cmdno,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 button, (long)curwin->w_cursor.lnum, col);
3305 nbdebug(("EVT: %s", buf));
3306 nb_send(buf, "netbeans_button_release");
3307 }
3308}
3309
3310
3311/*
Bram Moolenaar143c38c2007-05-10 16:41:10 +00003312 * Send a keypress event back to netbeans. This usually simulates some
Bram Moolenaar009b2592004-10-24 19:18:58 +00003313 * kind of function key press. This function operates on a key code.
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003314 * Return TRUE when the key was sent, FALSE when the command has been
3315 * postponed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 */
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003317 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318netbeans_keycommand(int key)
3319{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 char keyName[60];
Bram Moolenaar009b2592004-10-24 19:18:58 +00003321
3322 netbeans_keyname(key, keyName);
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003323 return netbeans_keystring((char_u *)keyName);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003324}
3325
3326
3327/*
Bram Moolenaar143c38c2007-05-10 16:41:10 +00003328 * Send a keypress event back to netbeans. This usually simulates some
Bram Moolenaar009b2592004-10-24 19:18:58 +00003329 * kind of function key press. This function operates on a key string.
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003330 * Return TRUE when the key was sent, FALSE when the command has been
3331 * postponed.
Bram Moolenaar009b2592004-10-24 19:18:58 +00003332 */
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003333 static int
3334netbeans_keystring(char_u *keyName)
Bram Moolenaar009b2592004-10-24 19:18:58 +00003335{
3336 char buf[2*MAXPATHL];
3337 int bufno = nb_getbufno(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 long off;
3339 char_u *q;
3340
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003341 if (!NETBEANS_OPEN)
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003342 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 if (bufno == -1)
3345 {
3346 nbdebug(("got keycommand for non-NetBeans buffer, opening...\n"));
3347 q = curbuf->b_ffname == NULL ? (char_u *)""
3348 : nb_quote(curbuf->b_ffname);
3349 if (q == NULL)
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003350 return TRUE;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003351 vim_snprintf(buf, sizeof(buf), "0:fileOpened=%d \"%s\" %s %s\n", 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 q,
3353 "T", /* open in NetBeans */
3354 "F"); /* modified */
3355 if (curbuf->b_ffname != NULL)
3356 vim_free(q);
3357 nbdebug(("EVT: %s", buf));
3358 nb_send(buf, "netbeans_keycommand");
3359
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003360 postpone_keycommand(keyName);
3361 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 }
3363
3364 /* sync the cursor position */
3365 off = pos2off(curbuf, &curwin->w_cursor);
Bram Moolenaar89d40322006-08-29 15:30:07 +00003366 sprintf(buf, "%d:newDotAndMark=%d %ld %ld\n", bufno, r_cmdno, off, off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 nbdebug(("EVT: %s", buf));
3368 nb_send(buf, "netbeans_keycommand");
3369
3370 /* To work on Win32 you must apply patch to ExtEditor module
3371 * from ExtEdCaret.java.diff - make EVT_newDotAndMark handler
3372 * more synchronous
3373 */
3374
3375 /* now send keyCommand event */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003376 vim_snprintf(buf, sizeof(buf), "%d:keyCommand=%d \"%s\"\n",
Bram Moolenaar89d40322006-08-29 15:30:07 +00003377 bufno, r_cmdno, keyName);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 nbdebug(("EVT: %s", buf));
3379 nb_send(buf, "netbeans_keycommand");
3380
3381 /* New: do both at once and include the lnum/col. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003382 vim_snprintf(buf, sizeof(buf), "%d:keyAtPos=%d \"%s\" %ld %ld/%ld\n",
Bram Moolenaar89d40322006-08-29 15:30:07 +00003383 bufno, r_cmdno, keyName,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 off, (long)curwin->w_cursor.lnum, (long)curwin->w_cursor.col);
3385 nbdebug(("EVT: %s", buf));
3386 nb_send(buf, "netbeans_keycommand");
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003387 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388}
3389
3390
3391/*
3392 * Send a save event to netbeans.
3393 */
3394 void
3395netbeans_save_buffer(buf_T *bufp)
3396{
3397 char_u buf[64];
3398 int bufno;
3399 nbbuf_T *nbbuf;
3400
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003401 if (!NETBEANS_OPEN)
3402 return;
3403
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3405 if (nbbuf == NULL)
3406 return;
3407
3408 nbbuf->modified = 0;
3409
Bram Moolenaar89d40322006-08-29 15:30:07 +00003410 sprintf((char *)buf, "%d:save=%d\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 nbdebug(("EVT: %s", buf));
3412 nb_send((char *)buf, "netbeans_save_buffer");
3413}
3414
3415
3416/*
3417 * Send remove command to netbeans (this command has been turned off).
3418 */
3419 void
3420netbeans_deleted_all_lines(buf_T *bufp)
3421{
3422 char_u buf[64];
3423 int bufno;
3424 nbbuf_T *nbbuf;
3425
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003426 if (!NETBEANS_OPEN)
3427 return;
3428
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3430 if (nbbuf == NULL)
3431 return;
3432
Bram Moolenaar009b2592004-10-24 19:18:58 +00003433 /* Don't mark as modified for initial read */
3434 if (nbbuf->insertDone)
3435 nbbuf->modified = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436
Bram Moolenaar89d40322006-08-29 15:30:07 +00003437 sprintf((char *)buf, "%d:remove=%d 0 -1\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 nbdebug(("EVT(suppressed): %s", buf));
3439/* nb_send(buf, "netbeans_deleted_all_lines"); */
3440}
3441
3442
3443/*
3444 * See if the lines are guarded. The top and bot parameters are from
3445 * u_savecommon(), these are the line above the change and the line below the
3446 * change.
3447 */
3448 int
3449netbeans_is_guarded(linenr_T top, linenr_T bot)
3450{
3451 signlist_T *p;
3452 int lnum;
3453
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003454 if (!NETBEANS_OPEN)
3455 return FALSE;
3456
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 for (p = curbuf->b_signlist; p != NULL; p = p->next)
3458 if (p->id >= GUARDEDOFFSET)
3459 for (lnum = top + 1; lnum < bot; lnum++)
3460 if (lnum == p->lnum)
3461 return TRUE;
3462
3463 return FALSE;
3464}
3465
3466#if defined(FEAT_GUI_MOTIF) || defined(PROTO)
3467/*
3468 * We have multiple signs to draw at the same location. Draw the
3469 * multi-sign indicator instead. This is the Motif version.
3470 */
3471 void
3472netbeans_draw_multisign_indicator(int row)
3473{
3474 int i;
3475 int y;
3476 int x;
3477
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003478 if (!NETBEANS_OPEN)
3479 return;
3480
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 x = 0;
3482 y = row * gui.char_height + 2;
3483
3484 for (i = 0; i < gui.char_height - 3; i++)
3485 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y++);
3486
3487 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+0, y);
3488 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3489 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+4, y++);
3490 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+1, y);
3491 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3492 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+3, y++);
3493 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3494}
3495#endif /* FEAT_GUI_MOTIF */
3496
3497#ifdef FEAT_GUI_GTK
3498/*
3499 * We have multiple signs to draw at the same location. Draw the
3500 * multi-sign indicator instead. This is the GTK/Gnome version.
3501 */
3502 void
3503netbeans_draw_multisign_indicator(int row)
3504{
3505 int i;
3506 int y;
3507 int x;
3508 GdkDrawable *drawable = gui.drawarea->window;
3509
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003510 if (!NETBEANS_OPEN)
3511 return;
3512
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 x = 0;
3514 y = row * gui.char_height + 2;
3515
3516 for (i = 0; i < gui.char_height - 3; i++)
3517 gdk_draw_point(drawable, gui.text_gc, x+2, y++);
3518
3519 gdk_draw_point(drawable, gui.text_gc, x+0, y);
3520 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3521 gdk_draw_point(drawable, gui.text_gc, x+4, y++);
3522 gdk_draw_point(drawable, gui.text_gc, x+1, y);
3523 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3524 gdk_draw_point(drawable, gui.text_gc, x+3, y++);
3525 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3526}
3527#endif /* FEAT_GUI_GTK */
3528
3529/*
3530 * If the mouse is clicked in the gutter of a line with multiple
3531 * annotations, cycle through the set of signs.
3532 */
3533 void
3534netbeans_gutter_click(linenr_T lnum)
3535{
3536 signlist_T *p;
3537
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003538 if (!NETBEANS_OPEN)
3539 return;
3540
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 for (p = curbuf->b_signlist; p != NULL; p = p->next)
3542 {
3543 if (p->lnum == lnum && p->next && p->next->lnum == lnum)
3544 {
3545 signlist_T *tail;
3546
3547 /* remove "p" from list, reinsert it at the tail of the sublist */
3548 if (p->prev)
3549 p->prev->next = p->next;
3550 else
3551 curbuf->b_signlist = p->next;
3552 p->next->prev = p->prev;
3553 /* now find end of sublist and insert p */
3554 for (tail = p->next;
3555 tail->next && tail->next->lnum == lnum
3556 && tail->next->id < GUARDEDOFFSET;
3557 tail = tail->next)
3558 ;
3559 /* tail now points to last entry with same lnum (except
3560 * that "guarded" annotations are always last) */
3561 p->next = tail->next;
3562 if (tail->next)
3563 tail->next->prev = p;
3564 p->prev = tail;
3565 tail->next = p;
3566 update_debug_sign(curbuf, lnum);
3567 break;
3568 }
3569 }
3570}
3571
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572/*
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003573 * Add a sign of the requested type at the requested location.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 *
3575 * Reverse engineering:
3576 * Apparently an annotation is defined the first time it is used in a buffer.
3577 * When the same annotation is used in two buffers, the second time we do not
3578 * need to define a new sign name but reuse the existing one. But since the
3579 * ID number used in the second buffer starts counting at one again, a mapping
3580 * is made from the ID specifically for the buffer to the global sign name
3581 * (which is a number).
3582 *
3583 * globalsignmap[] stores the signs that have been defined globally.
3584 * buf->signmapused[] maps buffer-local annotation IDs to an index in
3585 * globalsignmap[].
3586 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 static void
3588addsigntype(
3589 nbbuf_T *buf,
3590 int typeNum,
3591 char_u *typeName,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003592 char_u *tooltip UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 char_u *glyphFile,
Bram Moolenaar67c53842010-05-22 18:28:27 +02003594 char_u *fg,
3595 char_u *bg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 int i, j;
Bram Moolenaar67c53842010-05-22 18:28:27 +02003598 int use_fg = (*fg && STRCMP(fg, "none") != 0);
3599 int use_bg = (*bg && STRCMP(bg, "none") != 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600
3601 for (i = 0; i < globalsignmapused; i++)
3602 if (STRCMP(typeName, globalsignmap[i]) == 0)
3603 break;
3604
3605 if (i == globalsignmapused) /* not found; add it to global map */
3606 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003607 nbdebug(("DEFINEANNOTYPE(%d,%s,%s,%s,%s,%s)\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 typeNum, typeName, tooltip, glyphFile, fg, bg));
3609 if (use_fg || use_bg)
3610 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003611 char fgbuf[2 * (8 + MAX_COLOR_LENGTH) + 1];
3612 char bgbuf[2 * (8 + MAX_COLOR_LENGTH) + 1];
3613 char *ptr;
3614 int value;
3615
3616 value = strtol((char *)fg, &ptr, 10);
3617 if (ptr != (char *)fg)
3618 sprintf(fgbuf, "guifg=#%06x", value & 0xFFFFFF);
3619 else
3620 sprintf(fgbuf, "guifg=%s ctermfg=%s", fg, fg);
3621
3622 value = strtol((char *)bg, &ptr, 10);
3623 if (ptr != (char *)bg)
3624 sprintf(bgbuf, "guibg=#%06x", value & 0xFFFFFF);
3625 else
3626 sprintf(bgbuf, "guibg=%s ctermbg=%s", bg, bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627
3628 coloncmd(":highlight NB_%s %s %s", typeName, (use_fg) ? fgbuf : "",
3629 (use_bg) ? bgbuf : "");
3630 if (*glyphFile == NUL)
3631 /* no glyph, line highlighting only */
3632 coloncmd(":sign define %d linehl=NB_%s", i + 1, typeName);
3633 else if (vim_strsize(glyphFile) <= 2)
3634 /* one- or two-character glyph name, use as text glyph with
3635 * texthl */
3636 coloncmd(":sign define %d text=%s texthl=NB_%s", i + 1,
3637 glyphFile, typeName);
3638 else
3639 /* glyph, line highlighting */
3640 coloncmd(":sign define %d icon=%s linehl=NB_%s", i + 1,
3641 glyphFile, typeName);
3642 }
3643 else
3644 /* glyph, no line highlighting */
3645 coloncmd(":sign define %d icon=%s", i + 1, glyphFile);
3646
3647 if (STRCMP(typeName,"CurrentPC") == 0)
3648 curPCtype = typeNum;
3649
3650 if (globalsignmapused == globalsignmaplen)
3651 {
3652 if (globalsignmaplen == 0) /* first allocation */
3653 {
3654 globalsignmaplen = 20;
3655 globalsignmap = (char **)alloc_clear(globalsignmaplen*sizeof(char *));
3656 }
3657 else /* grow it */
3658 {
3659 int incr;
3660 int oldlen = globalsignmaplen;
3661
3662 globalsignmaplen *= 2;
3663 incr = globalsignmaplen - oldlen;
3664 globalsignmap = (char **)vim_realloc(globalsignmap,
3665 globalsignmaplen * sizeof(char *));
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003666 vim_memset(globalsignmap + oldlen, 0, incr * sizeof(char *));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 }
3668 }
3669
3670 globalsignmap[i] = (char *)typeName;
3671 globalsignmapused = i + 1;
3672 }
3673
3674 /* check local map; should *not* be found! */
3675 for (j = 0; j < buf->signmapused; j++)
3676 if (buf->signmap[j] == i + 1)
3677 return;
3678
3679 /* add to local map */
3680 if (buf->signmapused == buf->signmaplen)
3681 {
3682 if (buf->signmaplen == 0) /* first allocation */
3683 {
3684 buf->signmaplen = 5;
3685 buf->signmap = (int *)alloc_clear(buf->signmaplen * sizeof(int *));
3686 }
3687 else /* grow it */
3688 {
3689 int incr;
3690 int oldlen = buf->signmaplen;
3691 buf->signmaplen *= 2;
3692 incr = buf->signmaplen - oldlen;
3693 buf->signmap = (int *)vim_realloc(buf->signmap,
3694 buf->signmaplen*sizeof(int *));
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003695 vim_memset(buf->signmap + oldlen, 0, incr * sizeof(int *));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 }
3697 }
3698
3699 buf->signmap[buf->signmapused++] = i + 1;
3700
3701}
3702
3703
3704/*
3705 * See if we have the requested sign type in the buffer.
3706 */
3707 static int
3708mapsigntype(nbbuf_T *buf, int localsigntype)
3709{
3710 if (--localsigntype >= 0 && localsigntype < buf->signmapused)
3711 return buf->signmap[localsigntype];
3712
3713 return 0;
3714}
3715
3716
3717/*
3718 * Compute length of buffer, don't print anything.
3719 */
3720 static long
3721get_buf_size(buf_T *bufp)
3722{
3723 linenr_T lnum;
3724 long char_count = 0;
3725 int eol_size;
3726 long last_check = 100000L;
3727
3728 if (bufp->b_ml.ml_flags & ML_EMPTY)
3729 return 0;
3730 else
3731 {
3732 if (get_fileformat(bufp) == EOL_DOS)
3733 eol_size = 2;
3734 else
3735 eol_size = 1;
3736 for (lnum = 1; lnum <= bufp->b_ml.ml_line_count; ++lnum)
3737 {
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00003738 char_count += (long)STRLEN(ml_get_buf(bufp, lnum, FALSE))
3739 + eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 /* Check for a CTRL-C every 100000 characters */
3741 if (char_count > last_check)
3742 {
3743 ui_breakcheck();
3744 if (got_int)
3745 return char_count;
3746 last_check = char_count + 100000L;
3747 }
3748 }
3749 /* Correction for when last line doesn't have an EOL. */
3750 if (!bufp->b_p_eol && bufp->b_p_bin)
3751 char_count -= eol_size;
3752 }
3753
3754 return char_count;
3755}
3756
3757/*
3758 * Convert character offset to lnum,col
3759 */
3760 static pos_T *
3761off2pos(buf_T *buf, long offset)
3762{
3763 linenr_T lnum;
3764 static pos_T pos;
3765
3766 pos.lnum = 0;
3767 pos.col = 0;
3768#ifdef FEAT_VIRTUALEDIT
3769 pos.coladd = 0;
3770#endif
3771
3772 if (!(buf->b_ml.ml_flags & ML_EMPTY))
3773 {
3774 if ((lnum = ml_find_line_or_offset(buf, (linenr_T)0, &offset)) < 0)
3775 return NULL;
3776 pos.lnum = lnum;
3777 pos.col = offset;
3778 }
3779
3780 return &pos;
3781}
3782
3783/*
3784 * Convert an argument in the form "1234" to an offset and compute the
3785 * lnum/col from it. Convert an argument in the form "123/12" directly to a
3786 * lnum/col.
3787 * "argp" is advanced to after the argument.
3788 * Return a pointer to the position, NULL if something is wrong.
3789 */
3790 static pos_T *
3791get_off_or_lnum(buf_T *buf, char_u **argp)
3792{
3793 static pos_T mypos;
3794 long off;
3795
3796 off = strtol((char *)*argp, (char **)argp, 10);
3797 if (**argp == '/')
3798 {
3799 mypos.lnum = (linenr_T)off;
3800 ++*argp;
3801 mypos.col = strtol((char *)*argp, (char **)argp, 10);
3802#ifdef FEAT_VIRTUALEDIT
3803 mypos.coladd = 0;
3804#endif
3805 return &mypos;
3806 }
3807 return off2pos(buf, off);
3808}
3809
3810
3811/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003812 * Convert (lnum,col) to byte offset in the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 */
3814 static long
3815pos2off(buf_T *buf, pos_T *pos)
3816{
3817 long offset = 0;
3818
3819 if (!(buf->b_ml.ml_flags & ML_EMPTY))
3820 {
3821 if ((offset = ml_find_line_or_offset(buf, pos->lnum, 0)) < 0)
3822 return 0;
3823 offset += pos->col;
3824 }
3825
3826 return offset;
3827}
3828
3829
Bram Moolenaar009b2592004-10-24 19:18:58 +00003830/*
3831 * This message is printed after NetBeans opens a new file. Its
3832 * similar to the message readfile() uses, but since NetBeans
3833 * doesn't normally call readfile, we do our own.
3834 */
3835 static void
3836print_read_msg(buf)
3837 nbbuf_T *buf;
3838{
3839 int lnum = buf->bufp->b_ml.ml_line_count;
Bram Moolenaar914703b2010-05-31 21:59:46 +02003840 off_t nchars = buf->bufp->b_orig_size;
Bram Moolenaar009b2592004-10-24 19:18:58 +00003841 char_u c;
3842
3843 msg_add_fname(buf->bufp, buf->bufp->b_ffname);
3844 c = FALSE;
3845
3846 if (buf->bufp->b_p_ro)
3847 {
3848 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]"));
3849 c = TRUE;
3850 }
3851 if (!buf->bufp->b_start_eol)
3852 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003853 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]")
3854 : _("[Incomplete last line]"));
Bram Moolenaar009b2592004-10-24 19:18:58 +00003855 c = TRUE;
3856 }
3857 msg_add_lines(c, (long)lnum, nchars);
3858
3859 /* Now display it */
3860 vim_free(keep_msg);
3861 keep_msg = NULL;
3862 msg_scrolled_ign = TRUE;
3863 msg_trunc_attr(IObuff, FALSE, 0);
3864 msg_scrolled_ign = FALSE;
3865}
3866
3867
3868/*
Bram Moolenaar67c53842010-05-22 18:28:27 +02003869 * Print a message after NetBeans writes the file. This message should be
3870 * identical to the standard message a non-netbeans user would see when
3871 * writing a file.
Bram Moolenaar009b2592004-10-24 19:18:58 +00003872 */
3873 static void
3874print_save_msg(buf, nchars)
3875 nbbuf_T *buf;
Bram Moolenaar914703b2010-05-31 21:59:46 +02003876 off_t nchars;
Bram Moolenaar009b2592004-10-24 19:18:58 +00003877{
3878 char_u c;
3879 char_u *p;
3880
3881 if (nchars >= 0)
3882 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003883 /* put fname in IObuff with quotes */
3884 msg_add_fname(buf->bufp, buf->bufp->b_ffname);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003885 c = FALSE;
3886
3887 msg_add_lines(c, buf->bufp->b_ml.ml_line_count,
Bram Moolenaar914703b2010-05-31 21:59:46 +02003888 buf->bufp->b_orig_size);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003889
3890 vim_free(keep_msg);
3891 keep_msg = NULL;
3892 msg_scrolled_ign = TRUE;
3893 p = msg_trunc_attr(IObuff, FALSE, 0);
3894 if ((msg_scrolled && !need_wait_return) || !buf->initDone)
3895 {
3896 /* Need to repeat the message after redrawing when:
3897 * - When reading from stdin (the screen will be cleared next).
3898 * - When restart_edit is set (otherwise there will be a delay
3899 * before redrawing).
3900 * - When the screen was scrolled but there is no wait-return
3901 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003902 set_keep_msg(p, 0);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003903 }
3904 msg_scrolled_ign = FALSE;
3905 /* add_to_input_buf((char_u *)"\f", 1); */
3906 }
3907 else
3908 {
3909 char_u ebuf[BUFSIZ];
3910
3911 STRCPY(ebuf, (char_u *)_("E505: "));
3912 STRCAT(ebuf, IObuff);
3913 STRCAT(ebuf, (char_u *)_("is read-only (add ! to override)"));
3914 STRCPY(IObuff, ebuf);
Bram Moolenaarf2330482008-06-24 20:19:36 +00003915 nbdebug((" %s\n", ebuf ));
Bram Moolenaar009b2592004-10-24 19:18:58 +00003916 emsg(IObuff);
3917 }
3918}
3919
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920#endif /* defined(FEAT_NETBEANS_INTG) */