blob: f39111dc9b77eef55c12bd606bbd6c194f8986d7 [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 Moolenaar173c9852010-09-29 17:27:01 +020090#ifdef FEAT_GUI_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +000091static 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 Moolenaar173c9852010-09-29 17:27:01 +0200115#ifdef FEAT_GUI_X11
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 Moolenaar173c9852010-09-29 17:27:01 +0200146#ifdef FEAT_GUI_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147 if (inputHandler != (XtInputId)NULL)
148 {
149 XtRemoveInput(inputHandler);
150 inputHandler = (XtInputId)NULL;
151 }
Bram Moolenaar67c53842010-05-22 18:28:27 +0200152#else
153# ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154 if (inputHandler != 0)
155 {
156 gdk_input_remove(inputHandler);
157 inputHandler = 0;
158 }
Bram Moolenaar67c53842010-05-22 18:28:27 +0200159# else
160# ifdef FEAT_GUI_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 if (inputHandler == 0)
162 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200163 WSAAsyncSelect(nbsock, s_hwnd, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164 inputHandler = -1;
165 }
Bram Moolenaar67c53842010-05-22 18:28:27 +0200166# endif
167# endif
168#endif
169
Bram 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
Bram Moolenaard54a6882010-08-09 22:49:00 +0200188 if (gui.in_use)
189 {
190 gui_update_cursor(TRUE, FALSE);
191 gui_mch_flush();
192 }
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200193#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195
196#define NB_DEF_HOST "localhost"
197#define NB_DEF_ADDR "3219"
198#define NB_DEF_PASS "changeme"
199
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200200 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200201netbeans_connect(char *params, int doabort)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202{
203#ifdef INET_SOCKETS
204 struct sockaddr_in server;
205 struct hostent * host;
206# ifdef FEAT_GUI_W32
207 u_short port;
208# else
209 int port;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200210# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211#else
212 struct sockaddr_un server;
213#endif
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200214 int sd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215 char buf[32];
216 char *hostname = NULL;
217 char *address = NULL;
218 char *password = NULL;
219 char *fname;
220 char *arg = NULL;
221
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200222 if (*params == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200224 /* "=fname": Read info from specified file. */
225 if (getConnInfo(params + 1, &hostname, &address, &password)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226 == FAIL)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200227 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228 }
229 else
230 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200231 if (*params == ':')
232 /* ":<host>:<addr>:<password>": get info from argument */
233 arg = params + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234 if (arg == NULL && (fname = getenv("__NETBEANS_CONINFO")) != NULL)
235 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200236 /* "": get info from file specified in environment */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 if (getConnInfo(fname, &hostname, &address, &password) == FAIL)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200238 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 }
240 else
241 {
242 if (arg != NULL)
243 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200244 /* ":<host>:<addr>:<password>": get info from argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 hostname = arg;
246 address = strchr(hostname, ':');
247 if (address != NULL)
248 {
249 *address++ = '\0';
250 password = strchr(address, ':');
251 if (password != NULL)
252 *password++ = '\0';
253 }
254 }
255
256 /* Get the missing values from the environment. */
257 if (hostname == NULL || *hostname == '\0')
258 hostname = getenv("__NETBEANS_HOST");
259 if (address == NULL)
260 address = getenv("__NETBEANS_SOCKET");
261 if (password == NULL)
262 password = getenv("__NETBEANS_VIM_PASSWORD");
263
264 /* Move values to allocated memory. */
265 if (hostname != NULL)
266 hostname = (char *)vim_strsave((char_u *)hostname);
267 if (address != NULL)
268 address = (char *)vim_strsave((char_u *)address);
269 if (password != NULL)
270 password = (char *)vim_strsave((char_u *)password);
271 }
272 }
273
274 /* Use the default when a value is missing. */
275 if (hostname == NULL || *hostname == '\0')
276 {
277 vim_free(hostname);
278 hostname = (char *)vim_strsave((char_u *)NB_DEF_HOST);
279 }
280 if (address == NULL || *address == '\0')
281 {
282 vim_free(address);
283 address = (char *)vim_strsave((char_u *)NB_DEF_ADDR);
284 }
285 if (password == NULL || *password == '\0')
286 {
287 vim_free(password);
288 password = (char *)vim_strsave((char_u *)NB_DEF_PASS);
289 }
290 if (hostname == NULL || address == NULL || password == NULL)
291 goto theend; /* out of memory */
292
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200293#ifdef FEAT_GUI_W32
294 netbeans_init_winsock();
295#endif
296
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297#ifdef INET_SOCKETS
298 port = atoi(address);
299
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000300 if ((sd = (NBSOCK)socket(AF_INET, SOCK_STREAM, 0)) == (NBSOCK)-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000302 nbdebug(("error in socket() in netbeans_connect()\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303 PERROR("socket() in netbeans_connect()");
304 goto theend;
305 }
306
307 /* Get the server internet address and put into addr structure */
308 /* fill in the socket address structure and connect to server */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200309 vim_memset((char *)&server, '\0', sizeof(server));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310 server.sin_family = AF_INET;
311 server.sin_port = htons(port);
312 if ((host = gethostbyname(hostname)) == NULL)
313 {
314 if (mch_access(hostname, R_OK) >= 0)
315 {
316 /* DEBUG: input file */
317 sd = mch_open(hostname, O_RDONLY, 0);
318 goto theend;
319 }
Bram Moolenaarf2330482008-06-24 20:19:36 +0000320 nbdebug(("error in gethostbyname() in netbeans_connect()\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321 PERROR("gethostbyname() in netbeans_connect()");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322 goto theend;
323 }
324 memcpy((char *)&server.sin_addr, host->h_addr, host->h_length);
325#else
326 if ((sd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
327 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000328 nbdebug(("error in socket() in netbeans_connect()\n"));
329 PERROR("socket() in netbeans_connect()");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330 goto theend;
331 }
332
333 server.sun_family = AF_UNIX;
334 strcpy(server.sun_path, address);
335#endif
336 /* Connect to server */
337 if (connect(sd, (struct sockaddr *)&server, sizeof(server)))
338 {
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200339 SOCK_ERRNO;
340 nbdebug(("netbeans_connect: Connect failed with errno %d\n", errno));
341 if (errno == ECONNREFUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342 {
343 sock_close(sd);
344#ifdef INET_SOCKETS
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000345 if ((sd = (NBSOCK)socket(AF_INET, SOCK_STREAM, 0)) == (NBSOCK)-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 {
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200347 SOCK_ERRNO;
Bram Moolenaarf2330482008-06-24 20:19:36 +0000348 nbdebug(("socket()#2 in netbeans_connect()\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 PERROR("socket()#2 in netbeans_connect()");
350 goto theend;
351 }
352#else
353 if ((sd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
354 {
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200355 SOCK_ERRNO;
Bram Moolenaarf2330482008-06-24 20:19:36 +0000356 nbdebug(("socket()#2 in netbeans_connect()\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 PERROR("socket()#2 in netbeans_connect()");
358 goto theend;
359 }
360#endif
361 if (connect(sd, (struct sockaddr *)&server, sizeof(server)))
362 {
363 int retries = 36;
364 int success = FALSE;
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200365
366 SOCK_ERRNO;
367 while (retries-- && ((errno == ECONNREFUSED)
368 || (errno == EINTR)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 {
370 nbdebug(("retrying...\n"));
371 sleep(5);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200372 if (!doabort)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200373 {
374 ui_breakcheck();
375 if (got_int)
376 {
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200377 errno = EINTR;
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200378 break;
379 }
380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 if (connect(sd, (struct sockaddr *)&server,
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200382 sizeof(server)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 {
384 success = TRUE;
385 break;
386 }
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200387 SOCK_ERRNO;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 }
389 if (!success)
390 {
391 /* Get here when the server can't be found. */
Bram Moolenaarf2330482008-06-24 20:19:36 +0000392 nbdebug(("Cannot connect to Netbeans #2\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 PERROR(_("Cannot connect to Netbeans #2"));
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200394 if (doabort)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200395 getout(1);
396 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397 }
398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 }
400 else
401 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000402 nbdebug(("Cannot connect to Netbeans\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 PERROR(_("Cannot connect to Netbeans"));
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200404 if (doabort)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200405 getout(1);
406 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 }
408 }
409
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200410 nbsock = sd;
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000411 vim_snprintf(buf, sizeof(buf), "AUTH %s\n", password);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412 nb_send(buf, "netbeans_connect");
413
414 sprintf(buf, "0:version=0 \"%s\"\n", ExtEdProtocolVersion);
415 nb_send(buf, "externaleditor_version");
416
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417theend:
418 vim_free(hostname);
419 vim_free(address);
420 vim_free(password);
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200421 return NETBEANS_OPEN ? OK : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422}
423
424/*
425 * Obtain the NetBeans hostname, port address and password from a file.
426 * Return the strings in allocated memory.
427 * Return FAIL if the file could not be read, OK otherwise (no matter what it
428 * contains).
429 */
430 static int
431getConnInfo(char *file, char **host, char **port, char **auth)
432{
433 FILE *fp;
434 char_u buf[BUFSIZ];
435 char_u *lp;
436 char_u *nl;
437#ifdef UNIX
438 struct stat st;
439
440 /*
441 * For Unix only accept the file when it's not accessible by others.
442 * The open will then fail if we don't own the file.
443 */
444 if (mch_stat(file, &st) == 0 && (st.st_mode & 0077) != 0)
445 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000446 nbdebug(("Wrong access mode for NetBeans connection info file: \"%s\"\n",
447 file));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 EMSG2(_("E668: Wrong access mode for NetBeans connection info file: \"%s\""),
449 file);
450 return FAIL;
451 }
452#endif
453
454 fp = mch_fopen(file, "r");
455 if (fp == NULL)
456 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000457 nbdebug(("Cannot open NetBeans connection info file\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 PERROR("E660: Cannot open NetBeans connection info file");
459 return FAIL;
460 }
461
462 /* Read the file. There should be one of each parameter */
463 while ((lp = (char_u *)fgets((char *)buf, BUFSIZ, fp)) != NULL)
464 {
465 if ((nl = vim_strchr(lp, '\n')) != NULL)
466 *nl = 0; /* strip off the trailing newline */
467
468 if (STRNCMP(lp, "host=", 5) == 0)
469 {
470 vim_free(*host);
471 *host = (char *)vim_strsave(&buf[5]);
472 }
473 else if (STRNCMP(lp, "port=", 5) == 0)
474 {
475 vim_free(*port);
476 *port = (char *)vim_strsave(&buf[5]);
477 }
478 else if (STRNCMP(lp, "auth=", 5) == 0)
479 {
480 vim_free(*auth);
481 *auth = (char *)vim_strsave(&buf[5]);
482 }
483 }
484 fclose(fp);
485
486 return OK;
487}
488
489
490struct keyqueue
491{
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100492 char_u *keystr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493 struct keyqueue *next;
494 struct keyqueue *prev;
495};
496
497typedef struct keyqueue keyQ_T;
498
499static keyQ_T keyHead; /* dummy node, header for circular queue */
500
501
502/*
503 * Queue up key commands sent from netbeans.
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100504 * We store the string, because it may depend on the global mod_mask and
505 * :nbkey doesn't have a key number.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 */
507 static void
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100508postpone_keycommand(char_u *keystr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509{
510 keyQ_T *node;
511
512 node = (keyQ_T *)alloc(sizeof(keyQ_T));
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100513 if (node == NULL)
514 return; /* out of memory, drop the key */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515
516 if (keyHead.next == NULL) /* initialize circular queue */
517 {
518 keyHead.next = &keyHead;
519 keyHead.prev = &keyHead;
520 }
521
522 /* insert node at tail of queue */
523 node->next = &keyHead;
524 node->prev = keyHead.prev;
525 keyHead.prev->next = node;
526 keyHead.prev = node;
527
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100528 node->keystr = vim_strsave(keystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529}
530
531/*
532 * Handle any queued-up NetBeans keycommands to be send.
533 */
534 static void
535handle_key_queue(void)
536{
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100537 int postponed = FALSE;
538
539 while (!postponed && keyHead.next && keyHead.next != &keyHead)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 {
541 /* first, unlink the node */
542 keyQ_T *node = keyHead.next;
543 keyHead.next = node->next;
544 node->next->prev = node->prev;
545
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100546 /* Now, send the keycommand. This may cause it to be postponed again
547 * and change keyHead. */
548 if (node->keystr != NULL)
549 postponed = !netbeans_keystring(node->keystr);
550 vim_free(node->keystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551
552 /* Finally, dispose of the node */
553 vim_free(node);
554 }
555}
556
557
558struct cmdqueue
559{
560 char_u *buffer;
561 struct cmdqueue *next;
562 struct cmdqueue *prev;
563};
564
565typedef struct cmdqueue queue_T;
566
567static queue_T head; /* dummy node, header for circular queue */
568
569
570/*
571 * Put the buffer on the work queue; possibly save it to a file as well.
572 */
573 static void
574save(char_u *buf, int len)
575{
576 queue_T *node;
577
578 node = (queue_T *)alloc(sizeof(queue_T));
579 if (node == NULL)
580 return; /* out of memory */
581 node->buffer = alloc(len + 1);
582 if (node->buffer == NULL)
583 {
584 vim_free(node);
585 return; /* out of memory */
586 }
587 mch_memmove(node->buffer, buf, (size_t)len);
588 node->buffer[len] = NUL;
589
590 if (head.next == NULL) /* initialize circular queue */
591 {
592 head.next = &head;
593 head.prev = &head;
594 }
595
596 /* insert node at tail of queue */
597 node->next = &head;
598 node->prev = head.prev;
599 head.prev->next = node;
600 head.prev = node;
601
602#ifdef NBDEBUG
603 {
604 static int outfd = -2;
605
606 /* possibly write buffer out to a file */
607 if (outfd == -3)
608 return;
609
610 if (outfd == -2)
611 {
612 char *file = getenv("__NETBEANS_SAVE");
613 if (file == NULL)
614 outfd = -3;
615 else
616 outfd = mch_open(file, O_WRONLY|O_CREAT|O_TRUNC, 0666);
617 }
618
619 if (outfd >= 0)
620 write(outfd, buf, len);
621 }
622#endif
623}
624
625
626/*
627 * While there's still a command in the work queue, parse and execute it.
628 */
Bram Moolenaarf2330482008-06-24 20:19:36 +0000629 void
630netbeans_parse_messages(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631{
632 char_u *p;
633 queue_T *node;
634
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200635 if (!NETBEANS_OPEN)
636 return;
637
Bram Moolenaarf2330482008-06-24 20:19:36 +0000638 while (head.next != NULL && head.next != &head)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 {
640 node = head.next;
641
642 /* Locate the first line in the first buffer. */
643 p = vim_strchr(node->buffer, '\n');
644 if (p == NULL)
645 {
646 /* Command isn't complete. If there is no following buffer,
647 * return (wait for more). If there is another buffer following,
648 * prepend the text to that buffer and delete this one. */
649 if (node->next == &head)
650 return;
Bram Moolenaarf2330482008-06-24 20:19:36 +0000651 p = alloc((unsigned)(STRLEN(node->buffer)
652 + STRLEN(node->next->buffer) + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 if (p == NULL)
654 return; /* out of memory */
655 STRCPY(p, node->buffer);
656 STRCAT(p, node->next->buffer);
657 vim_free(node->next->buffer);
658 node->next->buffer = p;
659
660 /* dispose of the node and buffer */
661 head.next = node->next;
662 node->next->prev = node->prev;
663 vim_free(node->buffer);
664 vim_free(node);
665 }
666 else
667 {
668 /* There is a complete command at the start of the buffer.
669 * Terminate it with a NUL. When no more text is following unlink
670 * the buffer. Do this before executing, because new buffers can
671 * be added while busy handling the command. */
672 *p++ = NUL;
673 if (*p == NUL)
674 {
675 head.next = node->next;
676 node->next->prev = node->prev;
677 }
678
679 /* now, parse and execute the commands */
680 nb_parse_cmd(node->buffer);
681
682 if (*p == NUL)
683 {
684 /* buffer finished, dispose of the node and buffer */
685 vim_free(node->buffer);
686 vim_free(node);
687 }
688 else
689 {
690 /* more follows, move to the start */
Bram Moolenaarf2330482008-06-24 20:19:36 +0000691 STRMOVE(node->buffer, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 }
693 }
694 }
695}
696
697/* Buffer size for reading incoming messages. */
698#define MAXMSGSIZE 4096
699
700/*
Bram Moolenaar67c53842010-05-22 18:28:27 +0200701 * Read a command from netbeans.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 */
Bram Moolenaar173c9852010-09-29 17:27:01 +0200703#ifdef FEAT_GUI_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 static void
Bram Moolenaara9d45512009-05-17 21:25:42 +0000705messageFromNetbeans(XtPointer clientData UNUSED,
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000706 int *unused1 UNUSED,
707 XtInputId *unused2 UNUSED)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200708{
709 netbeans_read();
710}
711#endif
712
713#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000715messageFromNetbeans(gpointer clientData UNUSED,
716 gint unused1 UNUSED,
717 GdkInputCondition unused2 UNUSED)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200718{
719 netbeans_read();
720}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721#endif
Bram Moolenaar67c53842010-05-22 18:28:27 +0200722
723 void
724netbeans_read()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725{
726 static char_u *buf = NULL;
Bram Moolenaar0b65f892010-05-15 14:49:02 +0200727 int len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 int readlen = 0;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200729#if defined(NB_HAS_GUI) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_W32)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 static int level = 0;
Bram Moolenaarf2330482008-06-24 20:19:36 +0000731#endif
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100732#ifdef HAVE_SELECT
733 struct timeval tval;
734 fd_set rfds;
735#else
736# ifdef HAVE_POLL
737 struct pollfd fds;
738# endif
739#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200741 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 {
743 nbdebug(("messageFromNetbeans() called without a socket\n"));
744 return;
745 }
746
Bram Moolenaar67c53842010-05-22 18:28:27 +0200747#if defined(NB_HAS_GUI) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_W32)
748 /* recursion guard; this will be called from the X event loop at unknown
749 * moments */
750 if (NB_HAS_GUI)
751 ++level;
Bram Moolenaarf2330482008-06-24 20:19:36 +0000752#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753
754 /* Allocate a buffer to read into. */
755 if (buf == NULL)
756 {
757 buf = alloc(MAXMSGSIZE);
758 if (buf == NULL)
759 return; /* out of memory! */
760 }
761
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100762 /* Keep on reading for as long as there is something to read.
763 * Use select() or poll() to avoid blocking on a message that is exactly
764 * MAXMSGSIZE long. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 for (;;)
766 {
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100767#ifdef HAVE_SELECT
768 FD_ZERO(&rfds);
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200769 FD_SET(nbsock, &rfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +0200770 tval.tv_sec = 0;
771 tval.tv_usec = 0;
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200772 if (select(nbsock + 1, &rfds, NULL, NULL, &tval) <= 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200773 break;
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100774#else
775# ifdef HAVE_POLL
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200776 fds.fd = nbsock;
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100777 fds.events = POLLIN;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200778 if (poll(&fds, 1, 0) <= 0)
779 break;
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100780# endif
781#endif
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200782 len = sock_read(nbsock, buf, MAXMSGSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 if (len <= 0)
784 break; /* error or nothing more to read */
785
786 /* Store the read message in the queue. */
787 save(buf, len);
788 readlen += len;
789 if (len < MAXMSGSIZE)
790 break; /* did read everything that's available */
791 }
792
793 if (readlen <= 0)
794 {
795 /* read error or didn't read anything */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200796 netbeans_close();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 nbdebug(("messageFromNetbeans: Error in read() from socket\n"));
798 if (len < 0)
Bram Moolenaarf2330482008-06-24 20:19:36 +0000799 {
800 nbdebug(("read from Netbeans socket\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 PERROR(_("read from Netbeans socket"));
Bram Moolenaarf2330482008-06-24 20:19:36 +0000802 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000803 return; /* don't try to parse it */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 }
805
Bram Moolenaar67c53842010-05-22 18:28:27 +0200806#if defined(NB_HAS_GUI) && !defined(FEAT_GUI_W32)
Bram Moolenaar61665aa2008-12-24 11:20:53 +0000807 /* Let the main loop handle messages. */
Bram Moolenaar67c53842010-05-22 18:28:27 +0200808 if (NB_HAS_GUI)
809 {
Bram Moolenaar61665aa2008-12-24 11:20:53 +0000810# ifdef FEAT_GUI_GTK
Bram Moolenaar67c53842010-05-22 18:28:27 +0200811 if (gtk_main_level() > 0)
812 gtk_main_quit();
813# else
814 /* Parse the messages now, but avoid recursion. */
815 if (level == 1)
816 netbeans_parse_messages();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817
Bram Moolenaar67c53842010-05-22 18:28:27 +0200818 --level;
819# endif
820 }
Bram Moolenaarf2330482008-06-24 20:19:36 +0000821#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822}
823
824/*
825 * Handle one NUL terminated command.
826 *
827 * format of a command from netbeans:
828 *
829 * 6:setTitle!84 "a.c"
830 *
831 * bufno
832 * colon
833 * cmd
834 * !
835 * cmdno
836 * args
837 *
838 * for function calls, the ! is replaced by a /
839 */
840 static void
841nb_parse_cmd(char_u *cmd)
842{
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000843 char *verb;
844 char *q;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 int bufno;
846 int isfunc = -1;
847
848 if (STRCMP(cmd, "DISCONNECT") == 0)
849 {
850 /* We assume the server knows that we can safely exit! */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 /* Disconnect before exiting, Motif hangs in a Select error
852 * message otherwise. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200853 netbeans_close();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 getout(0);
855 /* NOTREACHED */
856 }
857
858 if (STRCMP(cmd, "DETACH") == 0)
859 {
860 /* The IDE is breaking the connection. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200861 netbeans_close();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 return;
863 }
864
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000865 bufno = strtol((char *)cmd, &verb, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866
867 if (*verb != ':')
868 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000869 nbdebug((" missing colon: %s\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 EMSG2("E627: missing colon: %s", cmd);
871 return;
872 }
873 ++verb; /* skip colon */
874
875 for (q = verb; *q; q++)
876 {
877 if (*q == '!')
878 {
879 *q++ = NUL;
880 isfunc = 0;
881 break;
882 }
883 else if (*q == '/')
884 {
885 *q++ = NUL;
886 isfunc = 1;
887 break;
888 }
889 }
890
891 if (isfunc < 0)
892 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000893 nbdebug((" missing ! or / in: %s\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 EMSG2("E628: missing ! or / in: %s", cmd);
895 return;
896 }
897
Bram Moolenaar89d40322006-08-29 15:30:07 +0000898 r_cmdno = strtol(q, &q, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000900 q = (char *)skipwhite((char_u *)q);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901
Bram Moolenaar89d40322006-08-29 15:30:07 +0000902 if (nb_do_cmd(bufno, (char_u *)verb, isfunc, r_cmdno, (char_u *)q) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 {
Bram Moolenaar009b2592004-10-24 19:18:58 +0000904#ifdef NBDEBUG
905 /*
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100906 * This happens because the ExtEd can send a command or 2 after
Bram Moolenaar009b2592004-10-24 19:18:58 +0000907 * doing a stopDocumentListen command. It doesn't harm anything
908 * so I'm disabling it except for debugging.
909 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 nbdebug(("nb_parse_cmd: Command error for \"%s\"\n", cmd));
911 EMSG("E629: bad return from nb_do_cmd");
Bram Moolenaar009b2592004-10-24 19:18:58 +0000912#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 }
914}
915
916struct nbbuf_struct
917{
918 buf_T *bufp;
919 unsigned int fireChanges:1;
920 unsigned int initDone:1;
Bram Moolenaar009b2592004-10-24 19:18:58 +0000921 unsigned int insertDone:1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 unsigned int modified:1;
Bram Moolenaar009b2592004-10-24 19:18:58 +0000923 int nbbuf_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 char *displayname;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 int *signmap;
926 short_u signmaplen;
927 short_u signmapused;
928};
929
930typedef struct nbbuf_struct nbbuf_T;
931
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200932static nbbuf_T *buf_list = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000933static int buf_list_size = 0; /* size of buf_list */
934static int buf_list_used = 0; /* nr of entries in buf_list actually in use */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200936static char **globalsignmap = NULL;
937static int globalsignmaplen = 0;
938static int globalsignmapused = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939
940static int mapsigntype __ARGS((nbbuf_T *, int localsigntype));
941static void addsigntype __ARGS((nbbuf_T *, int localsigntype, char_u *typeName,
942 char_u *tooltip, char_u *glyphfile,
Bram Moolenaar67c53842010-05-22 18:28:27 +0200943 char_u *fg, char_u *bg));
Bram Moolenaar009b2592004-10-24 19:18:58 +0000944static void print_read_msg __ARGS((nbbuf_T *buf));
Bram Moolenaar914703b2010-05-31 21:59:46 +0200945static void print_save_msg __ARGS((nbbuf_T *buf, off_t nchars));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946
947static int curPCtype = -1;
948
949/*
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200950 * Free netbeans resources.
951 */
952 static void
953nb_free()
954{
955 keyQ_T *key_node = keyHead.next;
956 queue_T *cmd_node = head.next;
957 nbbuf_T buf;
958 buf_T *bufp;
959 int i;
960
961 /* free the netbeans buffer list */
962 for (i = 0; i < buf_list_used; i++)
963 {
964 buf = buf_list[i];
965 vim_free(buf.displayname);
966 vim_free(buf.signmap);
967 if ((bufp=buf.bufp) != NULL)
968 {
969 buf.bufp->b_netbeans_file = FALSE;
970 buf.bufp->b_was_netbeans_file = FALSE;
971 }
972 }
973 vim_free(buf_list);
974 buf_list = NULL;
975 buf_list_size = 0;
976 buf_list_used = 0;
977
978 /* free the queued key commands */
979 while(key_node != NULL && key_node != &keyHead)
980 {
981 keyQ_T *next = key_node->next;
982 vim_free(key_node->keystr);
983 vim_free(key_node);
984 if (next == &keyHead)
985 {
986 keyHead.next = &keyHead;
987 keyHead.prev = &keyHead;
988 break;
989 }
990 key_node = next;
991 }
992
993 /* free the queued netbeans commands */
994 while(cmd_node != NULL && cmd_node != &head)
995 {
996 queue_T *next = cmd_node->next;
997 vim_free(cmd_node->buffer);
998 vim_free(cmd_node);
999 if (next == &head)
1000 {
1001 head.next = &head;
1002 head.prev = &head;
1003 break;
1004 }
1005 cmd_node = next;
1006 }
1007}
1008
1009/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 * Get the Netbeans buffer number for the specified buffer.
1011 */
1012 static int
1013nb_getbufno(buf_T *bufp)
1014{
1015 int i;
1016
1017 for (i = 0; i < buf_list_used; i++)
1018 if (buf_list[i].bufp == bufp)
1019 return i;
1020 return -1;
1021}
1022
1023/*
1024 * Is this a NetBeans-owned buffer?
1025 */
1026 int
1027isNetbeansBuffer(buf_T *bufp)
1028{
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001029 return NETBEANS_OPEN && bufp->b_netbeans_file;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030}
1031
1032/*
1033 * NetBeans and Vim have different undo models. In Vim, the file isn't
1034 * changed if changes are undone via the undo command. In NetBeans, once
1035 * a change has been made the file is marked as modified until saved. It
1036 * doesn't matter if the change was undone.
1037 *
1038 * So this function is for the corner case where Vim thinks a buffer is
1039 * unmodified but NetBeans thinks it IS modified.
1040 */
1041 int
1042isNetbeansModified(buf_T *bufp)
1043{
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001044 if (isNetbeansBuffer(bufp))
Bram Moolenaar009b2592004-10-24 19:18:58 +00001045 {
1046 int bufno = nb_getbufno(bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047
Bram Moolenaar009b2592004-10-24 19:18:58 +00001048 if (bufno > 0)
1049 return buf_list[bufno].modified;
1050 else
1051 return FALSE;
1052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 else
1054 return FALSE;
1055}
1056
1057/*
1058 * Given a Netbeans buffer number, return the netbeans buffer.
1059 * Returns NULL for 0 or a negative number. A 0 bufno means a
1060 * non-buffer related command has been sent.
1061 */
1062 static nbbuf_T *
1063nb_get_buf(int bufno)
1064{
1065 /* find or create a buffer with the given number */
1066 int incr;
1067
1068 if (bufno <= 0)
1069 return NULL;
1070
1071 if (!buf_list)
1072 {
1073 /* initialize */
1074 buf_list = (nbbuf_T *)alloc_clear(100 * sizeof(nbbuf_T));
1075 buf_list_size = 100;
1076 }
1077 if (bufno >= buf_list_used) /* new */
1078 {
1079 if (bufno >= buf_list_size) /* grow list */
1080 {
1081 incr = bufno - buf_list_size + 90;
1082 buf_list_size += incr;
1083 buf_list = (nbbuf_T *)vim_realloc(
1084 buf_list, buf_list_size * sizeof(nbbuf_T));
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02001085 vim_memset(buf_list + buf_list_size - incr, 0,
1086 incr * sizeof(nbbuf_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 }
1088
1089 while (buf_list_used <= bufno)
1090 {
1091 /* Default is to fire text changes. */
1092 buf_list[buf_list_used].fireChanges = 1;
1093 ++buf_list_used;
1094 }
1095 }
1096
1097 return buf_list + bufno;
1098}
1099
1100/*
1101 * Return the number of buffers that are modified.
1102 */
1103 static int
1104count_changed_buffers(void)
1105{
1106 buf_T *bufp;
1107 int n;
1108
1109 n = 0;
1110 for (bufp = firstbuf; bufp != NULL; bufp = bufp->b_next)
1111 if (bufp->b_changed)
1112 ++n;
1113 return n;
1114}
1115
1116/*
1117 * End the netbeans session.
1118 */
1119 void
1120netbeans_end(void)
1121{
1122 int i;
1123 static char buf[128];
1124
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001125 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 return;
1127
1128 for (i = 0; i < buf_list_used; i++)
1129 {
1130 if (!buf_list[i].bufp)
1131 continue;
1132 if (netbeansForcedQuit)
1133 {
1134 /* mark as unmodified so NetBeans won't put up dialog on "killed" */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001135 sprintf(buf, "%d:unmodified=%d\n", i, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 nbdebug(("EVT: %s", buf));
1137 nb_send(buf, "netbeans_end");
1138 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001139 sprintf(buf, "%d:killed=%d\n", i, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 nbdebug(("EVT: %s", buf));
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001141 /* nb_send(buf, "netbeans_end"); avoid "write failed" messages */
1142 ignored = sock_write(nbsock, buf, (int)STRLEN(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144}
1145
1146/*
1147 * Send a message to netbeans.
1148 */
1149 static void
1150nb_send(char *buf, char *fun)
1151{
1152 /* Avoid giving pages full of error messages when the other side has
1153 * exited, only mention the first error until the connection works again. */
1154 static int did_error = FALSE;
1155
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001156 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 {
1158 if (!did_error)
Bram Moolenaarf2330482008-06-24 20:19:36 +00001159 {
1160 nbdebug((" %s(): write while not connected\n", fun));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 EMSG2("E630: %s(): write while not connected", fun);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 did_error = TRUE;
1164 }
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001165 else if (sock_write(nbsock, buf, (int)STRLEN(buf)) != (int)STRLEN(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 {
1167 if (!did_error)
Bram Moolenaarf2330482008-06-24 20:19:36 +00001168 {
1169 nbdebug((" %s(): write failed\n", fun));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 EMSG2("E631: %s(): write failed", fun);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 did_error = TRUE;
1173 }
1174 else
1175 did_error = FALSE;
1176}
1177
1178/*
1179 * Some input received from netbeans requires a response. This function
1180 * handles a response with no information (except the command number).
1181 */
1182 static void
1183nb_reply_nil(int cmdno)
1184{
1185 char reply[32];
1186
Bram Moolenaar009b2592004-10-24 19:18:58 +00001187 nbdebug(("REP %d: <none>\n", cmdno));
1188
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 sprintf(reply, "%d\n", cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 nb_send(reply, "nb_reply_nil");
1191}
1192
1193
1194/*
1195 * Send a response with text.
1196 * "result" must have been quoted already (using nb_quote()).
1197 */
1198 static void
1199nb_reply_text(int cmdno, char_u *result)
1200{
1201 char_u *reply;
1202
Bram Moolenaar009b2592004-10-24 19:18:58 +00001203 nbdebug(("REP %d: %s\n", cmdno, (char *)result));
1204
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001205 reply = alloc((unsigned)STRLEN(result) + 32);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 sprintf((char *)reply, "%d %s\n", cmdno, (char *)result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 nb_send((char *)reply, "nb_reply_text");
1208
1209 vim_free(reply);
1210}
1211
1212
1213/*
1214 * Send a response with a number result code.
1215 */
1216 static void
1217nb_reply_nr(int cmdno, long result)
1218{
1219 char reply[32];
1220
Bram Moolenaar009b2592004-10-24 19:18:58 +00001221 nbdebug(("REP %d: %ld\n", cmdno, result));
1222
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 sprintf(reply, "%d %ld\n", cmdno, result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 nb_send(reply, "nb_reply_nr");
1225}
1226
1227
1228/*
1229 * Encode newline, ret, backslash, double quote for transmission to NetBeans.
1230 */
1231 static char_u *
1232nb_quote(char_u *txt)
1233{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001234 char_u *buf = alloc((unsigned)(2 * STRLEN(txt) + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 char_u *p = txt;
1236 char_u *q = buf;
1237
1238 if (buf == NULL)
1239 return NULL;
1240 for (; *p; p++)
1241 {
1242 switch (*p)
1243 {
1244 case '\"':
1245 case '\\':
1246 *q++ = '\\'; *q++ = *p; break;
1247 /* case '\t': */
1248 /* *q++ = '\\'; *q++ = 't'; break; */
1249 case '\n':
1250 *q++ = '\\'; *q++ = 'n'; break;
1251 case '\r':
1252 *q++ = '\\'; *q++ = 'r'; break;
1253 default:
1254 *q++ = *p;
1255 break;
1256 }
1257 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01001258 *q = '\0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259
1260 return buf;
1261}
1262
1263
1264/*
1265 * Remove top level double quotes; convert backslashed chars.
1266 * Returns an allocated string (NULL for failure).
1267 * If "endp" is not NULL it is set to the character after the terminating
1268 * quote.
1269 */
1270 static char *
1271nb_unquote(char_u *p, char_u **endp)
1272{
1273 char *result = 0;
1274 char *q;
1275 int done = 0;
1276
1277 /* result is never longer than input */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001278 result = (char *)alloc_clear((unsigned)STRLEN(p) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 if (result == NULL)
1280 return NULL;
1281
1282 if (*p++ != '"')
1283 {
1284 nbdebug(("nb_unquote called with string that doesn't start with a quote!: %s\n",
1285 p));
1286 result[0] = NUL;
1287 return result;
1288 }
1289
1290 for (q = result; !done && *p != NUL;)
1291 {
1292 switch (*p)
1293 {
1294 case '"':
1295 /*
1296 * Unbackslashed dquote marks the end, if first char was dquote.
1297 */
1298 done = 1;
1299 break;
1300
1301 case '\\':
1302 ++p;
1303 switch (*p)
1304 {
1305 case '\\': *q++ = '\\'; break;
1306 case 'n': *q++ = '\n'; break;
1307 case 't': *q++ = '\t'; break;
1308 case 'r': *q++ = '\r'; break;
1309 case '"': *q++ = '"'; break;
1310 case NUL: --p; break;
1311 /* default: skip over illegal chars */
1312 }
1313 ++p;
1314 break;
1315
1316 default:
1317 *q++ = *p++;
1318 }
1319 }
1320
1321 if (endp != NULL)
1322 *endp = p;
1323
1324 return result;
1325}
1326
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001327/*
1328 * Remove from "first" byte to "last" byte (inclusive), at line "lnum" of the
1329 * current buffer. Remove to end of line when "last" is MAXCOL.
1330 */
1331 static void
1332nb_partialremove(linenr_T lnum, colnr_T first, colnr_T last)
1333{
1334 char_u *oldtext, *newtext;
1335 int oldlen;
1336 int lastbyte = last;
1337
1338 oldtext = ml_get(lnum);
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001339 oldlen = (int)STRLEN(oldtext);
Bram Moolenaarb3c70982008-01-18 10:40:55 +00001340 if (first >= (colnr_T)oldlen || oldlen == 0) /* just in case */
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001341 return;
1342 if (lastbyte >= oldlen)
1343 lastbyte = oldlen - 1;
1344 newtext = alloc(oldlen - (int)(lastbyte - first));
1345 if (newtext != NULL)
1346 {
1347 mch_memmove(newtext, oldtext, first);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001348 STRMOVE(newtext + first, oldtext + lastbyte + 1);
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001349 nbdebug((" NEW LINE %d: %s\n", lnum, newtext));
1350 ml_replace(lnum, newtext, FALSE);
1351 }
1352}
1353
1354/*
1355 * Replace the "first" line with the concatenation of the "first" and
1356 * the "other" line. The "other" line is not removed.
1357 */
1358 static void
1359nb_joinlines(linenr_T first, linenr_T other)
1360{
1361 int len_first, len_other;
1362 char_u *p;
1363
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001364 len_first = (int)STRLEN(ml_get(first));
1365 len_other = (int)STRLEN(ml_get(other));
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001366 p = alloc((unsigned)(len_first + len_other + 1));
1367 if (p != NULL)
1368 {
1369 mch_memmove(p, ml_get(first), len_first);
1370 mch_memmove(p + len_first, ml_get(other), len_other + 1);
1371 ml_replace(first, p, FALSE);
1372 }
1373}
1374
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375#define SKIP_STOP 2
1376#define streq(a,b) (strcmp(a,b) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377
1378/*
1379 * Do the actual processing of a single netbeans command or function.
Bram Moolenaar143c38c2007-05-10 16:41:10 +00001380 * The difference between a command and function is that a function
1381 * gets a response (it's required) but a command does not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 * For arguments see comment for nb_parse_cmd().
1383 */
1384 static int
1385nb_do_cmd(
1386 int bufno,
1387 char_u *cmd,
1388 int func,
1389 int cmdno,
1390 char_u *args) /* points to space before arguments or NUL */
1391{
1392 int doupdate = 0;
1393 long off = 0;
1394 nbbuf_T *buf = nb_get_buf(bufno);
1395 static int skip = 0;
1396 int retval = OK;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001397 char *cp; /* for when a char pointer is needed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398
1399 nbdebug(("%s %d: (%d) %s %s\n", (func) ? "FUN" : "CMD", cmdno, bufno, cmd,
1400 STRCMP(cmd, "insert") == 0 ? "<text>" : (char *)args));
1401
1402 if (func)
1403 {
1404/* =====================================================================*/
1405 if (streq((char *)cmd, "getModified"))
1406 {
1407 if (buf == NULL || buf->bufp == NULL)
1408 /* Return the number of buffers that are modified. */
1409 nb_reply_nr(cmdno, (long)count_changed_buffers());
1410 else
1411 /* Return whether the buffer is modified. */
1412 nb_reply_nr(cmdno, (long)(buf->bufp->b_changed
1413 || isNetbeansModified(buf->bufp)));
1414/* =====================================================================*/
1415 }
1416 else if (streq((char *)cmd, "saveAndExit"))
1417 {
1418 /* Note: this will exit Vim if successful. */
1419 coloncmd(":confirm qall");
1420
1421 /* We didn't exit: return the number of changed buffers. */
1422 nb_reply_nr(cmdno, (long)count_changed_buffers());
1423/* =====================================================================*/
1424 }
1425 else if (streq((char *)cmd, "getCursor"))
1426 {
1427 char_u text[200];
1428
1429 /* Note: nb_getbufno() may return -1. This indicates the IDE
1430 * didn't assign a number to the current buffer in response to a
1431 * fileOpened event. */
1432 sprintf((char *)text, "%d %ld %d %ld",
1433 nb_getbufno(curbuf),
1434 (long)curwin->w_cursor.lnum,
1435 (int)curwin->w_cursor.col,
1436 pos2off(curbuf, &curwin->w_cursor));
1437 nb_reply_text(cmdno, text);
1438/* =====================================================================*/
1439 }
Bram Moolenaarc65c4912006-11-14 17:29:46 +00001440 else if (streq((char *)cmd, "getAnno"))
1441 {
1442 long linenum = 0;
1443#ifdef FEAT_SIGNS
1444 if (buf == NULL || buf->bufp == NULL)
1445 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001446 nbdebug((" Invalid buffer identifier in getAnno\n"));
1447 EMSG("E652: Invalid buffer identifier in getAnno");
Bram Moolenaarc65c4912006-11-14 17:29:46 +00001448 retval = FAIL;
1449 }
1450 else
1451 {
1452 int serNum;
1453
1454 cp = (char *)args;
1455 serNum = strtol(cp, &cp, 10);
1456 /* If the sign isn't found linenum will be zero. */
1457 linenum = (long)buf_findsign(buf->bufp, serNum);
1458 }
1459#endif
1460 nb_reply_nr(cmdno, linenum);
1461/* =====================================================================*/
1462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 else if (streq((char *)cmd, "getLength"))
1464 {
1465 long len = 0;
1466
1467 if (buf == NULL || buf->bufp == NULL)
1468 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001469 nbdebug((" invalid buffer identifier in getLength\n"));
1470 EMSG("E632: invalid buffer identifier in getLength");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 retval = FAIL;
1472 }
1473 else
1474 {
1475 len = get_buf_size(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 }
1477 nb_reply_nr(cmdno, len);
1478/* =====================================================================*/
1479 }
1480 else if (streq((char *)cmd, "getText"))
1481 {
1482 long len;
1483 linenr_T nlines;
1484 char_u *text = NULL;
1485 linenr_T lno = 1;
1486 char_u *p;
1487 char_u *line;
1488
1489 if (buf == NULL || buf->bufp == NULL)
1490 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001491 nbdebug((" invalid buffer identifier in getText\n"));
1492 EMSG("E633: invalid buffer identifier in getText");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 retval = FAIL;
1494 }
1495 else
1496 {
1497 len = get_buf_size(buf->bufp);
1498 nlines = buf->bufp->b_ml.ml_line_count;
1499 text = alloc((unsigned)((len > 0)
1500 ? ((len + nlines) * 2) : 4));
1501 if (text == NULL)
1502 {
1503 nbdebug((" nb_do_cmd: getText has null text field\n"));
1504 retval = FAIL;
1505 }
1506 else
1507 {
1508 p = text;
1509 *p++ = '\"';
1510 for (; lno <= nlines ; lno++)
1511 {
1512 line = nb_quote(ml_get_buf(buf->bufp, lno, FALSE));
1513 if (line != NULL)
1514 {
1515 STRCPY(p, line);
1516 p += STRLEN(line);
1517 *p++ = '\\';
1518 *p++ = 'n';
Bram Moolenaar009b2592004-10-24 19:18:58 +00001519 vim_free(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 }
1522 *p++ = '\"';
1523 *p = '\0';
1524 }
1525 }
1526 if (text == NULL)
1527 nb_reply_text(cmdno, (char_u *)"");
1528 else
1529 {
1530 nb_reply_text(cmdno, text);
1531 vim_free(text);
1532 }
1533/* =====================================================================*/
1534 }
1535 else if (streq((char *)cmd, "remove"))
1536 {
1537 long count;
1538 pos_T first, last;
1539 pos_T *pos;
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001540 pos_T *next;
1541 linenr_T del_from_lnum, del_to_lnum; /* lines to be deleted as a whole */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 int oldFire = netbeansFireChanges;
1543 int oldSuppress = netbeansSuppressNoLines;
1544 int wasChanged;
1545
1546 if (skip >= SKIP_STOP)
1547 {
1548 nbdebug((" Skipping %s command\n", (char *) cmd));
1549 nb_reply_nil(cmdno);
1550 return OK;
1551 }
1552
1553 if (buf == NULL || buf->bufp == NULL)
1554 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001555 nbdebug((" invalid buffer identifier in remove\n"));
1556 EMSG("E634: invalid buffer identifier in remove");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 retval = FAIL;
1558 }
1559 else
1560 {
1561 netbeansFireChanges = FALSE;
1562 netbeansSuppressNoLines = TRUE;
1563
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001564 nb_set_curbuf(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 wasChanged = buf->bufp->b_changed;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001566 cp = (char *)args;
1567 off = strtol(cp, &cp, 10);
1568 count = strtol(cp, &cp, 10);
1569 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 /* delete "count" chars, starting at "off" */
1571 pos = off2pos(buf->bufp, off);
1572 if (!pos)
1573 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001574 nbdebug((" !bad position\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 nb_reply_text(cmdno, (char_u *)"!bad position");
1576 netbeansFireChanges = oldFire;
1577 netbeansSuppressNoLines = oldSuppress;
1578 return FAIL;
1579 }
1580 first = *pos;
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001581 nbdebug((" FIRST POS: line %d, col %d\n",
1582 first.lnum, first.col));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 pos = off2pos(buf->bufp, off+count-1);
1584 if (!pos)
1585 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001586 nbdebug((" !bad count\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 nb_reply_text(cmdno, (char_u *)"!bad count");
1588 netbeansFireChanges = oldFire;
1589 netbeansSuppressNoLines = oldSuppress;
1590 return FAIL;
1591 }
1592 last = *pos;
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001593 nbdebug((" LAST POS: line %d, col %d\n",
1594 last.lnum, last.col));
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001595 del_from_lnum = first.lnum;
1596 del_to_lnum = last.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 doupdate = 1;
1598
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001599 /* Get the position of the first byte after the deleted
1600 * section. "next" is NULL when deleting to the end of the
1601 * file. */
1602 next = off2pos(buf->bufp, off + count);
1603
1604 /* Remove part of the first line. */
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001605 if (first.col != 0
1606 || (next != NULL && first.lnum == next->lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 {
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001608 if (first.lnum != last.lnum
1609 || (next != NULL && first.lnum != next->lnum))
1610 {
1611 /* remove to the end of the first line */
1612 nb_partialremove(first.lnum, first.col,
1613 (colnr_T)MAXCOL);
1614 if (first.lnum == last.lnum)
1615 {
1616 /* Partial line to remove includes the end of
1617 * line. Join the line with the next one, have
1618 * the next line deleted below. */
1619 nb_joinlines(first.lnum, next->lnum);
1620 del_to_lnum = next->lnum;
1621 }
1622 }
1623 else
1624 {
1625 /* remove within one line */
1626 nb_partialremove(first.lnum, first.col, last.col);
1627 }
1628 ++del_from_lnum; /* don't delete the first line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 }
1630
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001631 /* Remove part of the last line. */
1632 if (first.lnum != last.lnum && next != NULL
1633 && next->col != 0 && last.lnum == next->lnum)
1634 {
1635 nb_partialremove(last.lnum, 0, last.col);
1636 if (del_from_lnum > first.lnum)
1637 {
1638 /* Join end of last line to start of first line; last
1639 * line is deleted below. */
1640 nb_joinlines(first.lnum, last.lnum);
1641 }
1642 else
1643 /* First line is deleted as a whole, keep the last
1644 * line. */
1645 --del_to_lnum;
1646 }
1647
1648 /* First is partial line; last line to remove includes
1649 * the end of line; join first line to line following last
1650 * line; line following last line is deleted below. */
1651 if (first.lnum != last.lnum && del_from_lnum > first.lnum
1652 && next != NULL && last.lnum != next->lnum)
1653 {
1654 nb_joinlines(first.lnum, next->lnum);
1655 del_to_lnum = next->lnum;
1656 }
1657
1658 /* Delete whole lines if there are any. */
1659 if (del_to_lnum >= del_from_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 {
1661 int i;
1662
1663 /* delete signs from the lines being deleted */
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001664 for (i = del_from_lnum; i <= del_to_lnum; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 {
1666 int id = buf_findsign_id(buf->bufp, (linenr_T)i);
1667 if (id > 0)
1668 {
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001669 nbdebug((" Deleting sign %d on line %d\n",
1670 id, i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 buf_delsign(buf->bufp, id);
1672 }
1673 else
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001674 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 nbdebug((" No sign on line %d\n", i));
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 }
1678
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001679 nbdebug((" Deleting lines %d through %d\n",
1680 del_from_lnum, del_to_lnum));
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001681 curwin->w_cursor.lnum = del_from_lnum;
1682 curwin->w_cursor.col = 0;
1683 del_lines(del_to_lnum - del_from_lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 }
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001685
1686 /* Leave cursor at first deleted byte. */
1687 curwin->w_cursor = first;
1688 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 buf->bufp->b_changed = wasChanged; /* logically unchanged */
1690 netbeansFireChanges = oldFire;
1691 netbeansSuppressNoLines = oldSuppress;
1692
1693 u_blockfree(buf->bufp);
1694 u_clearall(buf->bufp);
1695 }
1696 nb_reply_nil(cmdno);
1697/* =====================================================================*/
1698 }
1699 else if (streq((char *)cmd, "insert"))
1700 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 char_u *to_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702
1703 if (skip >= SKIP_STOP)
1704 {
1705 nbdebug((" Skipping %s command\n", (char *) cmd));
1706 nb_reply_nil(cmdno);
1707 return OK;
1708 }
1709
1710 /* get offset */
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001711 cp = (char *)args;
1712 off = strtol(cp, &cp, 10);
1713 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714
1715 /* get text to be inserted */
1716 args = skipwhite(args);
1717 args = to_free = (char_u *)nb_unquote(args, NULL);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001718 /*
1719 nbdebug((" CHUNK[%d]: %d bytes at offset %d\n",
1720 buf->bufp->b_ml.ml_line_count, STRLEN(args), off));
1721 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722
1723 if (buf == NULL || buf->bufp == NULL)
1724 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001725 nbdebug((" invalid buffer identifier in insert\n"));
1726 EMSG("E635: invalid buffer identifier in insert");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 retval = FAIL;
1728 }
1729 else if (args != NULL)
1730 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001731 int ff_detected = EOL_UNKNOWN;
1732 int buf_was_empty = (buf->bufp->b_ml.ml_flags & ML_EMPTY);
1733 size_t len = 0;
1734 int added = 0;
1735 int oldFire = netbeansFireChanges;
1736 int old_b_changed;
1737 char_u *nl;
1738 linenr_T lnum;
1739 linenr_T lnum_start;
1740 pos_T *pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 netbeansFireChanges = 0;
1743
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001744 /* Jump to the buffer where we insert. After this "curbuf"
1745 * can be used. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001746 nb_set_curbuf(buf->bufp);
Bram Moolenaara3227e22006-03-08 21:32:40 +00001747 old_b_changed = curbuf->b_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001749 /* Convert the specified character offset into a lnum/col
1750 * position. */
Bram Moolenaara3227e22006-03-08 21:32:40 +00001751 pos = off2pos(curbuf, off);
1752 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001754 if (pos->lnum <= 0)
1755 lnum_start = 1;
1756 else
1757 lnum_start = pos->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 }
1759 else
1760 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001761 /* If the given position is not found, assume we want
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 * the end of the file. See setLocAndSize HACK. */
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001763 if (buf_was_empty)
1764 lnum_start = 1; /* above empty line */
1765 else
1766 lnum_start = curbuf->b_ml.ml_line_count + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001767 }
1768
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001769 /* "lnum" is the line where we insert: either append to it or
1770 * insert a new line above it. */
1771 lnum = lnum_start;
1772
1773 /* Loop over the "\n" separated lines of the argument. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 doupdate = 1;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001775 while (*args != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001777 nl = vim_strchr(args, '\n');
1778 if (nl == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001780 /* Incomplete line, probably truncated. Next "insert"
1781 * command should append to this one. */
1782 len = STRLEN(args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001784 else
1785 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001786 len = nl - args;
1787
1788 /*
1789 * We need to detect EOL style, because the commands
1790 * use a character offset.
1791 */
1792 if (nl > args && nl[-1] == '\r')
1793 {
1794 ff_detected = EOL_DOS;
1795 --len;
1796 }
1797 else
1798 ff_detected = EOL_UNIX;
1799 }
1800 args[len] = NUL;
1801
1802 if (lnum == lnum_start
1803 && ((pos != NULL && pos->col > 0)
1804 || (lnum == 1 && buf_was_empty)))
1805 {
1806 char_u *oldline = ml_get(lnum);
1807 char_u *newline;
1808
1809 /* Insert halfway a line. For simplicity we assume we
1810 * need to append to the line. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001811 newline = alloc_check((unsigned)(STRLEN(oldline) + len + 1));
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001812 if (newline != NULL)
1813 {
1814 STRCPY(newline, oldline);
1815 STRCAT(newline, args);
1816 ml_replace(lnum, newline, FALSE);
1817 }
1818 }
1819 else
1820 {
1821 /* Append a new line. Not that we always do this,
1822 * also when the text doesn't end in a "\n". */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001823 ml_append((linenr_T)(lnum - 1), args, (colnr_T)(len + 1), FALSE);
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001824 ++added;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001825 }
1826
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001827 if (nl == NULL)
1828 break;
1829 ++lnum;
1830 args = nl + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001831 }
1832
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001833 /* Adjust the marks below the inserted lines. */
1834 appended_lines_mark(lnum_start - 1, (long)added);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001836 /*
1837 * When starting with an empty buffer set the fileformat.
1838 * This is just guessing...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 */
1840 if (buf_was_empty)
1841 {
1842 if (ff_detected == EOL_UNKNOWN)
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001843#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 ff_detected = EOL_DOS;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001845#else
1846 ff_detected = EOL_UNIX;
1847#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 set_fileformat(ff_detected, OPT_LOCAL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00001849 curbuf->b_start_ffc = *curbuf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 }
1851
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 /*
1853 * XXX - GRP - Is the next line right? If I've inserted
1854 * text the buffer has been updated but not written. Will
1855 * netbeans guarantee to write it? Even if I do a :q! ?
1856 */
Bram Moolenaara3227e22006-03-08 21:32:40 +00001857 curbuf->b_changed = old_b_changed; /* logically unchanged */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 netbeansFireChanges = oldFire;
1859
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001860 /* Undo info is invalid now... */
Bram Moolenaara3227e22006-03-08 21:32:40 +00001861 u_blockfree(curbuf);
1862 u_clearall(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 }
1864 vim_free(to_free);
1865 nb_reply_nil(cmdno); /* or !error */
1866 }
1867 else
1868 {
1869 nbdebug(("UNIMPLEMENTED FUNCTION: %s\n", cmd));
1870 nb_reply_nil(cmdno);
1871 retval = FAIL;
1872 }
1873 }
1874 else /* Not a function; no reply required. */
1875 {
1876/* =====================================================================*/
1877 if (streq((char *)cmd, "create"))
1878 {
1879 /* Create a buffer without a name. */
1880 if (buf == NULL)
1881 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001882 nbdebug((" invalid buffer identifier in create\n"));
1883 EMSG("E636: invalid buffer identifier in create");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 return FAIL;
1885 }
1886 vim_free(buf->displayname);
1887 buf->displayname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888
1889 netbeansReadFile = 0; /* don't try to open disk file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001890 do_ecmd(0, NULL, 0, 0, ECMD_ONE, ECMD_HIDE + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 netbeansReadFile = 1;
1892 buf->bufp = curbuf;
1893 maketitle();
Bram Moolenaar009b2592004-10-24 19:18:58 +00001894 buf->insertDone = FALSE;
Bram Moolenaar67c53842010-05-22 18:28:27 +02001895#if defined(FEAT_MENU) && defined(FEAT_GUI)
Bram Moolenaard54a6882010-08-09 22:49:00 +02001896 if (gui.in_use)
1897 gui_update_menus(0);
Bram Moolenaar67c53842010-05-22 18:28:27 +02001898#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899/* =====================================================================*/
1900 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001901 else if (streq((char *)cmd, "insertDone"))
1902 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001903 if (buf == NULL || buf->bufp == NULL)
1904 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001905 nbdebug((" invalid buffer identifier in insertDone\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001906 }
1907 else
1908 {
1909 buf->bufp->b_start_eol = *args == 'T';
1910 buf->insertDone = TRUE;
1911 args += 2;
1912 buf->bufp->b_p_ro = *args == 'T';
1913 print_read_msg(buf);
1914 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001915/* =====================================================================*/
1916 }
1917 else if (streq((char *)cmd, "saveDone"))
1918 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001919 long savedChars = atol((char *)args);
1920
1921 if (buf == NULL || buf->bufp == NULL)
1922 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001923 nbdebug((" invalid buffer identifier in saveDone\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001924 }
1925 else
1926 print_save_msg(buf, savedChars);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001927/* =====================================================================*/
1928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 else if (streq((char *)cmd, "startDocumentListen"))
1930 {
1931 if (buf == NULL)
1932 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001933 nbdebug((" invalid buffer identifier in startDocumentListen\n"));
1934 EMSG("E637: invalid buffer identifier in startDocumentListen");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 return FAIL;
1936 }
1937 buf->fireChanges = 1;
1938/* =====================================================================*/
1939 }
1940 else if (streq((char *)cmd, "stopDocumentListen"))
1941 {
1942 if (buf == NULL)
1943 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001944 nbdebug((" invalid buffer identifier in stopDocumentListen\n"));
1945 EMSG("E638: invalid buffer identifier in stopDocumentListen");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 return FAIL;
1947 }
1948 buf->fireChanges = 0;
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001949 if (buf->bufp != NULL && buf->bufp->b_was_netbeans_file)
Bram Moolenaar009b2592004-10-24 19:18:58 +00001950 {
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001951 if (!buf->bufp->b_netbeans_file)
Bram Moolenaarf2330482008-06-24 20:19:36 +00001952 {
1953 nbdebug(("E658: NetBeans connection lost for buffer %ld\n", buf->bufp->b_fnum));
Bram Moolenaar009b2592004-10-24 19:18:58 +00001954 EMSGN(_("E658: NetBeans connection lost for buffer %ld"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 buf->bufp->b_fnum);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001956 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001957 else
1958 {
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001959 /* NetBeans uses stopDocumentListen when it stops editing
1960 * a file. It then expects the buffer in Vim to
1961 * disappear. */
1962 do_bufdel(DOBUF_DEL, (char_u *)"", 1,
1963 buf->bufp->b_fnum, buf->bufp->b_fnum, TRUE);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001964 vim_memset(buf, 0, sizeof(nbbuf_T));
1965 }
1966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967/* =====================================================================*/
1968 }
1969 else if (streq((char *)cmd, "setTitle"))
1970 {
1971 if (buf == NULL)
1972 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001973 nbdebug((" invalid buffer identifier in setTitle\n"));
1974 EMSG("E639: invalid buffer identifier in setTitle");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 return FAIL;
1976 }
1977 vim_free(buf->displayname);
1978 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979/* =====================================================================*/
1980 }
1981 else if (streq((char *)cmd, "initDone"))
1982 {
1983 if (buf == NULL || buf->bufp == NULL)
1984 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001985 nbdebug((" invalid buffer identifier in initDone\n"));
1986 EMSG("E640: invalid buffer identifier in initDone");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 return FAIL;
1988 }
1989 doupdate = 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001990 buf->initDone = TRUE;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001991 nb_set_curbuf(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992#if defined(FEAT_AUTOCMD)
1993 apply_autocmds(EVENT_BUFREADPOST, 0, 0, FALSE, buf->bufp);
1994#endif
1995
1996 /* handle any postponed key commands */
1997 handle_key_queue();
1998/* =====================================================================*/
1999 }
2000 else if (streq((char *)cmd, "setBufferNumber")
2001 || streq((char *)cmd, "putBufferNumber"))
2002 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002003 char_u *path;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 buf_T *bufp;
2005
2006 if (buf == NULL)
2007 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002008 nbdebug((" invalid buffer identifier in setBufferNumber\n"));
2009 EMSG("E641: invalid buffer identifier in setBufferNumber");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 return FAIL;
2011 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002012 path = (char_u *)nb_unquote(args, NULL);
2013 if (path == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 return FAIL;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002015 bufp = buflist_findname(path);
2016 vim_free(path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 if (bufp == NULL)
2018 {
Bram Moolenaarec906222009-02-21 21:14:00 +00002019 nbdebug((" File %s not found in setBufferNumber\n", args));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 EMSG2("E642: File %s not found in setBufferNumber", args);
2021 return FAIL;
2022 }
2023 buf->bufp = bufp;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002024 buf->nbbuf_number = bufp->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025
2026 /* "setBufferNumber" has the side effect of jumping to the buffer
2027 * (don't know why!). Don't do that for "putBufferNumber". */
2028 if (*cmd != 'p')
2029 coloncmd(":buffer %d", bufp->b_fnum);
2030 else
2031 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002032 buf->initDone = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033
2034 /* handle any postponed key commands */
2035 handle_key_queue();
2036 }
2037
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038/* =====================================================================*/
2039 }
2040 else if (streq((char *)cmd, "setFullName"))
2041 {
2042 if (buf == NULL)
2043 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002044 nbdebug((" invalid buffer identifier in setFullName\n"));
2045 EMSG("E643: invalid buffer identifier in setFullName");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 return FAIL;
2047 }
2048 vim_free(buf->displayname);
2049 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050
2051 netbeansReadFile = 0; /* don't try to open disk file */
2052 do_ecmd(0, (char_u *)buf->displayname, 0, 0, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002053 ECMD_HIDE + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 netbeansReadFile = 1;
2055 buf->bufp = curbuf;
2056 maketitle();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002057#if defined(FEAT_MENU) && defined(FEAT_GUI)
Bram Moolenaard54a6882010-08-09 22:49:00 +02002058 if (gui.in_use)
2059 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 Moolenaard54a6882010-08-09 22:49:00 +02002083 if (gui.in_use)
2084 gui_update_menus(0);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002085#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086/* =====================================================================*/
2087 }
2088 else if (streq((char *)cmd, "setVisible"))
2089 {
2090 if (buf == NULL || buf->bufp == NULL)
2091 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002092 nbdebug((" invalid buffer identifier in setVisible\n"));
2093 /* This message was commented out, probably because it can
2094 * happen when shutting down. */
2095 if (p_verbose > 0)
2096 EMSG("E645: invalid buffer identifier in setVisible");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 return FAIL;
2098 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002099 if (streq((char *)args, "T") && buf->bufp != curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 {
2101 exarg_T exarg;
2102 exarg.cmd = (char_u *)"goto";
2103 exarg.forceit = FALSE;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002104 dosetvisible = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 goto_buffer(&exarg, DOBUF_FIRST, FORWARD, buf->bufp->b_fnum);
2106 doupdate = 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002107 dosetvisible = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108
Bram Moolenaar67c53842010-05-22 18:28:27 +02002109#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 /* Side effect!!!. */
Bram Moolenaard54a6882010-08-09 22:49:00 +02002111 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 gui_mch_set_foreground();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115/* =====================================================================*/
2116 }
2117 else if (streq((char *)cmd, "raise"))
2118 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002119#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 /* Bring gvim to the foreground. */
Bram Moolenaard54a6882010-08-09 22:49:00 +02002121 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 gui_mch_set_foreground();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002123#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124/* =====================================================================*/
2125 }
2126 else if (streq((char *)cmd, "setModified"))
2127 {
Bram Moolenaarff064e12008-06-09 13:10:45 +00002128 int prev_b_changed;
2129
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 if (buf == NULL || buf->bufp == NULL)
2131 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002132 nbdebug((" invalid buffer identifier in setModified\n"));
2133 /* This message was commented out, probably because it can
2134 * happen when shutting down. */
2135 if (p_verbose > 0)
2136 EMSG("E646: invalid buffer identifier in setModified");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 return FAIL;
2138 }
Bram Moolenaarff064e12008-06-09 13:10:45 +00002139 prev_b_changed = buf->bufp->b_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 if (streq((char *)args, "T"))
Bram Moolenaarff064e12008-06-09 13:10:45 +00002141 buf->bufp->b_changed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 else
2143 {
2144 struct stat st;
2145
2146 /* Assume NetBeans stored the file. Reset the timestamp to
2147 * avoid "file changed" warnings. */
2148 if (buf->bufp->b_ffname != NULL
2149 && mch_stat((char *)buf->bufp->b_ffname, &st) >= 0)
2150 buf_store_time(buf->bufp, &st, buf->bufp->b_ffname);
Bram Moolenaarff064e12008-06-09 13:10:45 +00002151 buf->bufp->b_changed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 }
2153 buf->modified = buf->bufp->b_changed;
Bram Moolenaarff064e12008-06-09 13:10:45 +00002154 if (prev_b_changed != buf->bufp->b_changed)
2155 {
2156#ifdef FEAT_WINDOWS
2157 check_status(buf->bufp);
2158 redraw_tabline = TRUE;
2159#endif
2160#ifdef FEAT_TITLE
2161 maketitle();
2162#endif
2163 update_screen(0);
2164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165/* =====================================================================*/
2166 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002167 else if (streq((char *)cmd, "setModtime"))
2168 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002169 if (buf == NULL || buf->bufp == NULL)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002170 nbdebug((" invalid buffer identifier in setModtime\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002171 else
2172 buf->bufp->b_mtime = atoi((char *)args);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002173/* =====================================================================*/
2174 }
2175 else if (streq((char *)cmd, "setReadOnly"))
2176 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002177 if (buf == NULL || buf->bufp == NULL)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002178 nbdebug((" invalid buffer identifier in setReadOnly\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002179 else if (streq((char *)args, "T"))
Bram Moolenaar009b2592004-10-24 19:18:58 +00002180 buf->bufp->b_p_ro = TRUE;
2181 else
2182 buf->bufp->b_p_ro = FALSE;
2183/* =====================================================================*/
2184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 else if (streq((char *)cmd, "setMark"))
2186 {
2187 /* not yet */
2188/* =====================================================================*/
2189 }
2190 else if (streq((char *)cmd, "showBalloon"))
2191 {
2192#if defined(FEAT_BEVAL)
2193 static char *text = NULL;
2194
2195 /*
2196 * Set up the Balloon Expression Evaluation area.
2197 * Ignore 'ballooneval' here.
2198 * The text pointer must remain valid for a while.
2199 */
2200 if (balloonEval != NULL)
2201 {
2202 vim_free(text);
2203 text = nb_unquote(args, NULL);
2204 if (text != NULL)
2205 gui_mch_post_balloon(balloonEval, (char_u *)text);
2206 }
2207#endif
2208/* =====================================================================*/
2209 }
2210 else if (streq((char *)cmd, "setDot"))
2211 {
2212 pos_T *pos;
2213#ifdef NBDEBUG
2214 char_u *s;
2215#endif
2216
2217 if (buf == NULL || buf->bufp == NULL)
2218 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002219 nbdebug((" invalid buffer identifier in setDot\n"));
2220 EMSG("E647: invalid buffer identifier in setDot");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 return FAIL;
2222 }
2223
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002224 nb_set_curbuf(buf->bufp);
2225
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226#ifdef FEAT_VISUAL
2227 /* Don't want Visual mode now. */
2228 if (VIsual_active)
2229 end_visual_mode();
2230#endif
2231#ifdef NBDEBUG
2232 s = args;
2233#endif
2234 pos = get_off_or_lnum(buf->bufp, &args);
2235 if (pos)
2236 {
2237 curwin->w_cursor = *pos;
2238 check_cursor();
2239#ifdef FEAT_FOLDING
2240 foldOpenCursor();
2241#endif
2242 }
2243 else
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002244 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 nbdebug((" BAD POSITION in setDot: %s\n", s));
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247
2248 /* gui_update_cursor(TRUE, FALSE); */
2249 /* update_curbuf(NOT_VALID); */
2250 update_topline(); /* scroll to show the line */
2251 update_screen(VALID);
2252 setcursor();
2253 out_flush();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002254#ifdef FEAT_GUI
Bram Moolenaard54a6882010-08-09 22:49:00 +02002255 if (gui.in_use)
2256 {
2257 gui_update_cursor(TRUE, FALSE);
2258 gui_mch_flush();
2259 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02002260#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 /* Quit a hit-return or more prompt. */
2262 if (State == HITRETURN || State == ASKMORE)
2263 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264#ifdef FEAT_GUI_GTK
Bram Moolenaard54a6882010-08-09 22:49:00 +02002265 if (gui.in_use && gtk_main_level() > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 gtk_main_quit();
2267#endif
2268 }
2269/* =====================================================================*/
2270 }
2271 else if (streq((char *)cmd, "close"))
2272 {
2273#ifdef NBDEBUG
2274 char *name = "<NONE>";
2275#endif
2276
2277 if (buf == NULL)
2278 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002279 nbdebug((" invalid buffer identifier in close\n"));
2280 EMSG("E648: invalid buffer identifier in close");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 return FAIL;
2282 }
2283
2284#ifdef NBDEBUG
2285 if (buf->displayname != NULL)
2286 name = buf->displayname;
2287#endif
Bram Moolenaarf2330482008-06-24 20:19:36 +00002288 if (buf->bufp == NULL)
2289 {
2290 nbdebug((" invalid buffer identifier in close\n"));
2291 /* This message was commented out, probably because it can
2292 * happen when shutting down. */
2293 if (p_verbose > 0)
2294 EMSG("E649: invalid buffer identifier in close");
2295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 nbdebug((" CLOSE %d: %s\n", bufno, name));
Bram Moolenaar67c53842010-05-22 18:28:27 +02002297#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 need_mouse_correct = TRUE;
Bram Moolenaar67c53842010-05-22 18:28:27 +02002299#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 if (buf->bufp != NULL)
2301 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD,
2302 buf->bufp->b_fnum, TRUE);
Bram Moolenaar8d602722006-08-08 19:34:19 +00002303 buf->bufp = NULL;
2304 buf->initDone = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 doupdate = 1;
2306/* =====================================================================*/
2307 }
2308 else if (streq((char *)cmd, "setStyle")) /* obsolete... */
2309 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002310 nbdebug((" setStyle is obsolete!\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311/* =====================================================================*/
2312 }
2313 else if (streq((char *)cmd, "setExitDelay"))
2314 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002315 /* Only used in version 2.1. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316/* =====================================================================*/
2317 }
2318 else if (streq((char *)cmd, "defineAnnoType"))
2319 {
2320#ifdef FEAT_SIGNS
2321 int typeNum;
2322 char_u *typeName;
2323 char_u *tooltip;
2324 char_u *p;
2325 char_u *glyphFile;
Bram Moolenaar67c53842010-05-22 18:28:27 +02002326 int parse_error = FALSE;
2327 char_u *fg;
2328 char_u *bg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329
2330 if (buf == NULL)
2331 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002332 nbdebug((" invalid buffer identifier in defineAnnoType\n"));
2333 EMSG("E650: invalid buffer identifier in defineAnnoType");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 return FAIL;
2335 }
2336
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002337 cp = (char *)args;
2338 typeNum = strtol(cp, &cp, 10);
2339 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 args = skipwhite(args);
2341 typeName = (char_u *)nb_unquote(args, &args);
2342 args = skipwhite(args + 1);
2343 tooltip = (char_u *)nb_unquote(args, &args);
2344 args = skipwhite(args + 1);
2345
2346 p = (char_u *)nb_unquote(args, &args);
2347 glyphFile = vim_strsave_escaped(p, escape_chars);
2348 vim_free(p);
2349
2350 args = skipwhite(args + 1);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002351 p = skiptowhite(args);
2352 if (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002354 *p = NUL;
2355 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02002357 fg = vim_strsave(args);
2358 bg = vim_strsave(p);
2359 if (STRLEN(fg) > MAX_COLOR_LENGTH || STRLEN(bg) > MAX_COLOR_LENGTH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002361 EMSG("E532: highlighting color name too long in defineAnnoType");
2362 vim_free(typeName);
2363 parse_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02002365 else if (typeName != NULL && tooltip != NULL && glyphFile != NULL)
2366 addsigntype(buf, typeNum, typeName, tooltip, glyphFile, fg, bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 else
2368 vim_free(typeName);
2369
2370 /* don't free typeName; it's used directly in addsigntype() */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002371 vim_free(fg);
2372 vim_free(bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 vim_free(tooltip);
2374 vim_free(glyphFile);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002375 if (parse_error)
2376 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377
2378#endif
2379/* =====================================================================*/
2380 }
2381 else if (streq((char *)cmd, "addAnno"))
2382 {
2383#ifdef FEAT_SIGNS
2384 int serNum;
2385 int localTypeNum;
2386 int typeNum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 pos_T *pos;
2388
2389 if (buf == NULL || buf->bufp == NULL)
2390 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002391 nbdebug((" invalid buffer identifier in addAnno\n"));
2392 EMSG("E651: invalid buffer identifier in addAnno");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 return FAIL;
2394 }
2395
2396 doupdate = 1;
2397
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002398 cp = (char *)args;
2399 serNum = strtol(cp, &cp, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400
2401 /* Get the typenr specific for this buffer and convert it to
2402 * the global typenumber, as used for the sign name. */
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002403 localTypeNum = strtol(cp, &cp, 10);
2404 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 typeNum = mapsigntype(buf, localTypeNum);
2406
2407 pos = get_off_or_lnum(buf->bufp, &args);
2408
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002409 cp = (char *)args;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002410 ignored = (int)strtol(cp, &cp, 10);
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002411 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412# ifdef NBDEBUG
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002413 if (ignored != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002415 nbdebug((" partial line annotation -- Not Yet Implemented!\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 }
2417# endif
2418 if (serNum >= GUARDEDOFFSET)
2419 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002420 nbdebug((" too many annotations! ignoring...\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 return FAIL;
2422 }
2423 if (pos)
2424 {
Bram Moolenaarec906222009-02-21 21:14:00 +00002425 coloncmd(":sign place %d line=%ld name=%d buffer=%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 serNum, pos->lnum, typeNum, buf->bufp->b_fnum);
2427 if (typeNum == curPCtype)
2428 coloncmd(":sign jump %d buffer=%d", serNum,
2429 buf->bufp->b_fnum);
2430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431#endif
2432/* =====================================================================*/
2433 }
2434 else if (streq((char *)cmd, "removeAnno"))
2435 {
2436#ifdef FEAT_SIGNS
2437 int serNum;
2438
2439 if (buf == NULL || buf->bufp == NULL)
2440 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002441 nbdebug((" invalid buffer identifier in removeAnno\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 return FAIL;
2443 }
2444 doupdate = 1;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002445 cp = (char *)args;
2446 serNum = strtol(cp, &cp, 10);
2447 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 coloncmd(":sign unplace %d buffer=%d",
2449 serNum, buf->bufp->b_fnum);
2450 redraw_buf_later(buf->bufp, NOT_VALID);
2451#endif
2452/* =====================================================================*/
2453 }
2454 else if (streq((char *)cmd, "moveAnnoToFront"))
2455 {
2456#ifdef FEAT_SIGNS
Bram Moolenaarf2330482008-06-24 20:19:36 +00002457 nbdebug((" moveAnnoToFront: Not Yet Implemented!\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458#endif
2459/* =====================================================================*/
2460 }
2461 else if (streq((char *)cmd, "guard") || streq((char *)cmd, "unguard"))
2462 {
2463 int len;
2464 pos_T first;
2465 pos_T last;
2466 pos_T *pos;
2467 int un = (cmd[0] == 'u');
2468 static int guardId = GUARDEDOFFSET;
2469
2470 if (skip >= SKIP_STOP)
2471 {
2472 nbdebug((" Skipping %s command\n", (char *) cmd));
2473 return OK;
2474 }
2475
2476 nb_init_graphics();
2477
2478 if (buf == NULL || buf->bufp == NULL)
2479 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002480 nbdebug((" invalid buffer identifier in %s command\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 return FAIL;
2482 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002483 nb_set_curbuf(buf->bufp);
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002484 cp = (char *)args;
2485 off = strtol(cp, &cp, 10);
2486 len = strtol(cp, NULL, 10);
2487 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 pos = off2pos(buf->bufp, off);
2489 doupdate = 1;
2490 if (!pos)
2491 nbdebug((" no such start pos in %s, %ld\n", cmd, off));
2492 else
2493 {
2494 first = *pos;
2495 pos = off2pos(buf->bufp, off + len - 1);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002496 if (pos != NULL && pos->col == 0)
2497 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 /*
2499 * In Java Swing the offset is a position between 2
2500 * characters. If col == 0 then we really want the
2501 * previous line as the end.
2502 */
2503 pos = off2pos(buf->bufp, off + len - 2);
2504 }
2505 if (!pos)
2506 nbdebug((" no such end pos in %s, %ld\n",
2507 cmd, off + len - 1));
2508 else
2509 {
2510 long lnum;
2511 last = *pos;
2512 /* set highlight for region */
2513 nbdebug((" %sGUARD %ld,%d to %ld,%d\n", (un) ? "UN" : "",
2514 first.lnum, first.col,
2515 last.lnum, last.col));
2516#ifdef FEAT_SIGNS
2517 for (lnum = first.lnum; lnum <= last.lnum; lnum++)
2518 {
2519 if (un)
2520 {
2521 /* never used */
2522 }
2523 else
2524 {
2525 if (buf_findsigntype_id(buf->bufp, lnum,
2526 GUARDED) == 0)
2527 {
2528 coloncmd(
Bram Moolenaarec906222009-02-21 21:14:00 +00002529 ":sign place %d line=%ld name=%d buffer=%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 guardId++, lnum, GUARDED,
2531 buf->bufp->b_fnum);
2532 }
2533 }
2534 }
2535#endif
2536 redraw_buf_later(buf->bufp, NOT_VALID);
2537 }
2538 }
2539/* =====================================================================*/
2540 }
2541 else if (streq((char *)cmd, "startAtomic"))
2542 {
2543 inAtomic = 1;
2544/* =====================================================================*/
2545 }
2546 else if (streq((char *)cmd, "endAtomic"))
2547 {
2548 inAtomic = 0;
2549 if (needupdate)
2550 {
2551 doupdate = 1;
2552 needupdate = 0;
2553 }
2554/* =====================================================================*/
2555 }
2556 else if (streq((char *)cmd, "save"))
2557 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002558 /*
2559 * NOTE - This command is obsolete wrt NetBeans. Its left in
2560 * only for historical reasons.
2561 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 if (buf == NULL || buf->bufp == NULL)
2563 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002564 nbdebug((" invalid buffer identifier in %s command\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 return FAIL;
2566 }
2567
2568 /* the following is taken from ex_cmds.c (do_wqall function) */
2569 if (bufIsChanged(buf->bufp))
2570 {
2571 /* Only write if the buffer can be written. */
2572 if (p_write
2573 && !buf->bufp->b_p_ro
2574 && buf->bufp->b_ffname != NULL
2575#ifdef FEAT_QUICKFIX
2576 && !bt_dontwrite(buf->bufp)
2577#endif
2578 )
2579 {
2580 buf_write_all(buf->bufp, FALSE);
2581#ifdef FEAT_AUTOCMD
2582 /* an autocommand may have deleted the buffer */
2583 if (!buf_valid(buf->bufp))
2584 buf->bufp = NULL;
2585#endif
2586 }
2587 }
Bram Moolenaarf2330482008-06-24 20:19:36 +00002588 else
2589 {
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002590 nbdebug((" Buffer has no changes!\n"));
Bram Moolenaarf2330482008-06-24 20:19:36 +00002591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592/* =====================================================================*/
2593 }
2594 else if (streq((char *)cmd, "netbeansBuffer"))
2595 {
2596 if (buf == NULL || buf->bufp == NULL)
2597 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002598 nbdebug((" invalid buffer identifier in %s command\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 return FAIL;
2600 }
2601 if (*args == 'T')
2602 {
2603 buf->bufp->b_netbeans_file = TRUE;
2604 buf->bufp->b_was_netbeans_file = TRUE;
2605 }
2606 else
2607 buf->bufp->b_netbeans_file = FALSE;
2608/* =====================================================================*/
2609 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002610 else if (streq((char *)cmd, "specialKeys"))
2611 {
2612 special_keys(args);
2613/* =====================================================================*/
2614 }
2615 else if (streq((char *)cmd, "actionMenuItem"))
2616 {
2617 /* not used yet */
2618/* =====================================================================*/
2619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 else if (streq((char *)cmd, "version"))
2621 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002622 /* not used yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 }
Bram Moolenaarf2330482008-06-24 20:19:36 +00002624 else
2625 {
2626 nbdebug(("Unrecognised command: %s\n", cmd));
2627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 /*
2629 * Unrecognized command is ignored.
2630 */
2631 }
2632 if (inAtomic && doupdate)
2633 {
2634 needupdate = 1;
2635 doupdate = 0;
2636 }
2637
Bram Moolenaar009b2592004-10-24 19:18:58 +00002638 /*
2639 * Is this needed? I moved the netbeans_Xt_connect() later during startup
2640 * and it may no longer be necessary. If its not needed then needupdate
2641 * and doupdate can also be removed.
2642 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 if (buf != NULL && buf->initDone && doupdate)
2644 {
2645 update_screen(NOT_VALID);
2646 setcursor();
2647 out_flush();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002648#ifdef FEAT_GUI
Bram Moolenaard54a6882010-08-09 22:49:00 +02002649 if (gui.in_use)
2650 {
2651 gui_update_cursor(TRUE, FALSE);
2652 gui_mch_flush();
2653 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02002654#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 /* Quit a hit-return or more prompt. */
2656 if (State == HITRETURN || State == ASKMORE)
2657 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658#ifdef FEAT_GUI_GTK
Bram Moolenaard54a6882010-08-09 22:49:00 +02002659 if (gui.in_use && gtk_main_level() > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 gtk_main_quit();
2661#endif
2662 }
2663 }
2664
2665 return retval;
2666}
2667
2668
2669/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002670 * If "buf" is not the current buffer try changing to a window that edits this
2671 * buffer. If there is no such window then close the current buffer and set
2672 * the current buffer as "buf".
2673 */
2674 static void
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00002675nb_set_curbuf(buf_T *buf)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002676{
2677 if (curbuf != buf && buf_jump_open_win(buf) == NULL)
2678 set_curbuf(buf, DOBUF_GOTO);
2679}
2680
2681/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 * Process a vim colon command.
2683 */
2684 static void
2685coloncmd(char *cmd, ...)
2686{
2687 char buf[1024];
2688 va_list ap;
2689
2690 va_start(ap, cmd);
Bram Moolenaar0dc79e82009-06-24 14:50:12 +00002691 vim_vsnprintf(buf, sizeof(buf), cmd, ap, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 va_end(ap);
2693
2694 nbdebug((" COLONCMD %s\n", buf));
2695
2696/* ALT_INPUT_LOCK_ON; */
2697 do_cmdline((char_u *)buf, NULL, NULL, DOCMD_NOWAIT | DOCMD_KEYTYPED);
2698/* ALT_INPUT_LOCK_OFF; */
2699
2700 setcursor(); /* restore the cursor position */
2701 out_flush(); /* make sure output has been written */
2702
Bram Moolenaar67c53842010-05-22 18:28:27 +02002703#ifdef FEAT_GUI
Bram Moolenaard54a6882010-08-09 22:49:00 +02002704 if (gui.in_use)
2705 {
2706 gui_update_cursor(TRUE, FALSE);
2707 gui_mch_flush();
2708 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02002709#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710}
2711
2712
2713/*
Bram Moolenaar009b2592004-10-24 19:18:58 +00002714 * Parse the specialKeys argument and issue the appropriate map commands.
2715 */
2716 static void
2717special_keys(char_u *args)
2718{
2719 char *save_str = nb_unquote(args, NULL);
2720 char *tok = strtok(save_str, " ");
2721 char *sep;
2722 char keybuf[64];
2723 char cmdbuf[256];
2724
2725 while (tok != NULL)
2726 {
2727 int i = 0;
2728
2729 if ((sep = strchr(tok, '-')) != NULL)
2730 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002731 *sep = NUL;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002732 while (*tok)
2733 {
2734 switch (*tok)
2735 {
2736 case 'A':
2737 case 'M':
2738 case 'C':
2739 case 'S':
2740 keybuf[i++] = *tok;
2741 keybuf[i++] = '-';
2742 break;
2743 }
2744 tok++;
2745 }
2746 tok++;
2747 }
2748
2749 strcpy(&keybuf[i], tok);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002750 vim_snprintf(cmdbuf, sizeof(cmdbuf),
2751 "<silent><%s> :nbkey %s<CR>", keybuf, keybuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002752 do_map(0, (char_u *)cmdbuf, NORMAL, FALSE);
2753 tok = strtok(NULL, " ");
2754 }
2755 vim_free(save_str);
2756}
2757
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002758 void
2759ex_nbclose(eap)
2760 exarg_T *eap UNUSED;
2761{
2762 netbeans_close();
2763}
Bram Moolenaar009b2592004-10-24 19:18:58 +00002764
2765 void
2766ex_nbkey(eap)
2767 exarg_T *eap;
2768{
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002769 (void)netbeans_keystring(eap->arg);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002770}
2771
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002772 void
2773ex_nbstart(eap)
2774 exarg_T *eap;
2775{
2776 netbeans_open((char *)eap->arg, FALSE);
2777}
Bram Moolenaar009b2592004-10-24 19:18:58 +00002778
2779/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 * Initialize highlights and signs for use by netbeans (mostly obsolete)
2781 */
2782 static void
2783nb_init_graphics(void)
2784{
2785 static int did_init = FALSE;
2786
2787 if (!did_init)
2788 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002789 coloncmd(":highlight NBGuarded guibg=Cyan guifg=Black"
2790 " ctermbg=LightCyan ctermfg=Black");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 coloncmd(":sign define %d linehl=NBGuarded", GUARDED);
2792
2793 did_init = TRUE;
2794 }
2795}
2796
2797/*
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002798 * Convert key to netbeans name. This uses the global "mod_mask".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 */
2800 static void
2801netbeans_keyname(int key, char *buf)
2802{
2803 char *name = 0;
2804 char namebuf[2];
2805 int ctrl = 0;
2806 int shift = 0;
2807 int alt = 0;
2808
2809 if (mod_mask & MOD_MASK_CTRL)
2810 ctrl = 1;
2811 if (mod_mask & MOD_MASK_SHIFT)
2812 shift = 1;
2813 if (mod_mask & MOD_MASK_ALT)
2814 alt = 1;
2815
2816
2817 switch (key)
2818 {
2819 case K_F1: name = "F1"; break;
2820 case K_S_F1: name = "F1"; shift = 1; break;
2821 case K_F2: name = "F2"; break;
2822 case K_S_F2: name = "F2"; shift = 1; break;
2823 case K_F3: name = "F3"; break;
2824 case K_S_F3: name = "F3"; shift = 1; break;
2825 case K_F4: name = "F4"; break;
2826 case K_S_F4: name = "F4"; shift = 1; break;
2827 case K_F5: name = "F5"; break;
2828 case K_S_F5: name = "F5"; shift = 1; break;
2829 case K_F6: name = "F6"; break;
2830 case K_S_F6: name = "F6"; shift = 1; break;
2831 case K_F7: name = "F7"; break;
2832 case K_S_F7: name = "F7"; shift = 1; break;
2833 case K_F8: name = "F8"; break;
2834 case K_S_F8: name = "F8"; shift = 1; break;
2835 case K_F9: name = "F9"; break;
2836 case K_S_F9: name = "F9"; shift = 1; break;
2837 case K_F10: name = "F10"; break;
2838 case K_S_F10: name = "F10"; shift = 1; break;
2839 case K_F11: name = "F11"; break;
2840 case K_S_F11: name = "F11"; shift = 1; break;
2841 case K_F12: name = "F12"; break;
2842 case K_S_F12: name = "F12"; shift = 1; break;
2843 default:
2844 if (key >= ' ' && key <= '~')
2845 {
2846 /* Allow ASCII characters. */
2847 name = namebuf;
2848 namebuf[0] = key;
2849 namebuf[1] = NUL;
2850 }
2851 else
2852 name = "X";
2853 break;
2854 }
2855
2856 buf[0] = '\0';
2857 if (ctrl)
2858 strcat(buf, "C");
2859 if (shift)
2860 strcat(buf, "S");
2861 if (alt)
2862 strcat(buf, "M"); /* META */
2863 if (ctrl || shift || alt)
2864 strcat(buf, "-");
2865 strcat(buf, name);
2866}
2867
Bram Moolenaar67c53842010-05-22 18:28:27 +02002868#if defined(FEAT_BEVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869/*
2870 * Function to be called for balloon evaluation. Grabs the text under the
2871 * cursor and sends it to the debugger for evaluation. The debugger should
2872 * respond with a showBalloon command when there is a useful result.
2873 */
Bram Moolenaard62bec82005-03-07 22:56:57 +00002874 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875netbeans_beval_cb(
2876 BalloonEval *beval,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002877 int state UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878{
Bram Moolenaard62bec82005-03-07 22:56:57 +00002879 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 char_u *text;
Bram Moolenaard62bec82005-03-07 22:56:57 +00002881 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 int col;
2883 char buf[MAXPATHL * 2 + 25];
2884 char_u *p;
2885
2886 /* Don't do anything when 'ballooneval' is off, messages scrolled the
2887 * windows up or we have no connection. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002888 if (!p_beval || msg_scrolled > 0 || !NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 return;
2890
Bram Moolenaard62bec82005-03-07 22:56:57 +00002891 if (get_beval_info(beval, TRUE, &wp, &lnum, &text, &col) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 {
2893 /* Send debugger request. Only when the text is of reasonable
2894 * length. */
2895 if (text != NULL && text[0] != NUL && STRLEN(text) < MAXPATHL)
2896 {
2897 p = nb_quote(text);
2898 if (p != NULL)
Bram Moolenaar009b2592004-10-24 19:18:58 +00002899 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002900 vim_snprintf(buf, sizeof(buf),
Bram Moolenaar89d40322006-08-29 15:30:07 +00002901 "0:balloonText=%d \"%s\"\n", r_cmdno, p);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002902 vim_free(p);
2903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 nbdebug(("EVT: %s", buf));
2905 nb_send(buf, "netbeans_beval_cb");
2906 }
2907 vim_free(text);
2908 }
2909}
2910#endif
2911
2912/*
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002913 * Return TRUE when the netbeans connection is closed.
2914 */
2915 int
2916netbeans_active(void)
2917{
2918 return NETBEANS_OPEN;
2919}
2920
2921/*
Bram Moolenaar67c53842010-05-22 18:28:27 +02002922 * Return netbeans file descriptor.
2923 */
2924 int
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002925netbeans_filedesc(void)
Bram Moolenaar67c53842010-05-22 18:28:27 +02002926{
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002927 return nbsock;
Bram Moolenaar67c53842010-05-22 18:28:27 +02002928}
2929
2930#if defined(FEAT_GUI) || defined(PROTO)
2931/*
2932 * Register our file descriptor with the gui event handling system.
2933 */
2934 void
2935netbeans_gui_register(void)
2936{
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002937 if (!NB_HAS_GUI || !NETBEANS_OPEN)
Bram Moolenaar67c53842010-05-22 18:28:27 +02002938 return;
2939
Bram Moolenaar173c9852010-09-29 17:27:01 +02002940# ifdef FEAT_GUI_X11
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002941 /* tell notifier we are interested in being called
2942 * when there is input on the editor connection socket
2943 */
2944 if (inputHandler == (XtInputId)NULL)
2945 inputHandler = XtAppAddInput((XtAppContext)app_context, nbsock,
2946 (XtPointer)(XtInputReadMask + XtInputExceptMask),
2947 messageFromNetbeans, NULL);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002948# else
2949# ifdef FEAT_GUI_GTK
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002950 /*
2951 * Tell gdk we are interested in being called when there
2952 * is input on the editor connection socket
2953 */
2954 if (inputHandler == 0)
2955 inputHandler = gdk_input_add((gint)nbsock, (GdkInputCondition)
2956 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
2957 messageFromNetbeans, NULL);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002958# else
2959# ifdef FEAT_GUI_W32
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002960 /*
2961 * Tell Windows we are interested in receiving message when there
2962 * is input on the editor connection socket
2963 */
2964 if (inputHandler == -1)
2965 inputHandler = WSAAsyncSelect(nbsock, s_hwnd, WM_NETBEANS, FD_READ);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002966# endif
2967# endif
2968# endif
Bram Moolenaar67c53842010-05-22 18:28:27 +02002969
2970# ifdef FEAT_BEVAL
2971 bevalServers |= BEVAL_NETBEANS;
2972# endif
2973}
2974#endif
2975
2976/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 * Tell netbeans that the window was opened, ready for commands.
2978 */
2979 void
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02002980netbeans_open(char *params, int doabort)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981{
2982 char *cmd = "0:startupDone=0\n";
2983
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002984 if (NETBEANS_OPEN)
2985 {
2986 EMSG(_("E511: netbeans already connected"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02002990 if (netbeans_connect(params, doabort) != OK)
Bram Moolenaar67c53842010-05-22 18:28:27 +02002991 return;
2992#ifdef FEAT_GUI
2993 netbeans_gui_register();
Bram Moolenaard62bec82005-03-07 22:56:57 +00002994#endif
2995
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 nbdebug(("EVT: %s", cmd));
2997 nb_send(cmd, "netbeans_startup_done");
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002998
2999 /* update the screen after having added the gutter */
3000 changed_window_setting();
3001 update_screen(CLEAR);
3002 setcursor();
3003 out_flush();
3004#ifdef FEAT_GUI
Bram Moolenaard54a6882010-08-09 22:49:00 +02003005 if (gui.in_use)
3006 {
3007 gui_update_cursor(TRUE, FALSE);
3008 gui_mch_flush();
3009 }
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003010#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011}
3012
Bram Moolenaar009b2592004-10-24 19:18:58 +00003013/*
3014 * Tell netbeans that we're exiting. This should be called right
3015 * before calling exit.
3016 */
3017 void
3018netbeans_send_disconnect()
3019{
3020 char buf[128];
3021
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003022 if (NETBEANS_OPEN)
Bram Moolenaar009b2592004-10-24 19:18:58 +00003023 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00003024 sprintf(buf, "0:disconnect=%d\n", r_cmdno);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003025 nbdebug(("EVT: %s", buf));
3026 nb_send(buf, "netbeans_disconnect");
3027 }
3028}
3029
Bram Moolenaar173c9852010-09-29 17:27:01 +02003030#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_W32) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031/*
3032 * Tell netbeans that the window was moved or resized.
3033 */
3034 void
3035netbeans_frame_moved(int new_x, int new_y)
3036{
3037 char buf[128];
3038
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003039 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 return;
3041
3042 sprintf(buf, "0:geometry=%d %d %d %d %d\n",
Bram Moolenaar89d40322006-08-29 15:30:07 +00003043 r_cmdno, (int)Columns, (int)Rows, new_x, new_y);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003044 /*nbdebug(("EVT: %s", buf)); happens too many times during a move */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 nb_send(buf, "netbeans_frame_moved");
3046}
3047#endif
3048
3049/*
Bram Moolenaar009b2592004-10-24 19:18:58 +00003050 * Tell netbeans the user opened or activated a file.
3051 */
3052 void
3053netbeans_file_activated(buf_T *bufp)
3054{
3055 int bufno = nb_getbufno(bufp);
3056 nbbuf_T *bp = nb_get_buf(bufno);
3057 char buffer[2*MAXPATHL];
3058 char_u *q;
3059
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003060 if (!NETBEANS_OPEN || !bufp->b_netbeans_file || dosetvisible)
Bram Moolenaar009b2592004-10-24 19:18:58 +00003061 return;
3062
3063 q = nb_quote(bufp->b_ffname);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003064 if (q == NULL || bp == NULL)
Bram Moolenaar009b2592004-10-24 19:18:58 +00003065 return;
3066
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003067 vim_snprintf(buffer, sizeof(buffer), "%d:fileOpened=%d \"%s\" %s %s\n",
Bram Moolenaar009b2592004-10-24 19:18:58 +00003068 bufno,
3069 bufno,
3070 (char *)q,
3071 "T", /* open in NetBeans */
3072 "F"); /* modified */
3073
3074 vim_free(q);
3075 nbdebug(("EVT: %s", buffer));
3076
3077 nb_send(buffer, "netbeans_file_opened");
3078}
3079
3080/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 * Tell netbeans the user opened a file.
3082 */
3083 void
Bram Moolenaar009b2592004-10-24 19:18:58 +00003084netbeans_file_opened(buf_T *bufp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085{
Bram Moolenaar009b2592004-10-24 19:18:58 +00003086 int bufno = nb_getbufno(bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 char buffer[2*MAXPATHL];
3088 char_u *q;
Bram Moolenaar009b2592004-10-24 19:18:58 +00003089 nbbuf_T *bp = nb_get_buf(nb_getbufno(bufp));
3090 int bnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003092 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 return;
3094
Bram Moolenaar009b2592004-10-24 19:18:58 +00003095 q = nb_quote(bufp->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 if (q == NULL)
3097 return;
Bram Moolenaar009b2592004-10-24 19:18:58 +00003098 if (bp != NULL)
3099 bnum = bufno;
3100 else
3101 bnum = 0;
3102
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003103 vim_snprintf(buffer, sizeof(buffer), "%d:fileOpened=%d \"%s\" %s %s\n",
Bram Moolenaar009b2592004-10-24 19:18:58 +00003104 bnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 0,
3106 (char *)q,
3107 "T", /* open in NetBeans */
3108 "F"); /* modified */
3109
3110 vim_free(q);
3111 nbdebug(("EVT: %s", buffer));
3112
3113 nb_send(buffer, "netbeans_file_opened");
Bram Moolenaar009b2592004-10-24 19:18:58 +00003114 if (p_acd && vim_chdirfile(bufp->b_ffname) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 shorten_fnames(TRUE);
3116}
3117
3118/*
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00003119 * Tell netbeans that a file was deleted or wiped out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 */
3121 void
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00003122netbeans_file_killed(buf_T *bufp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123{
3124 int bufno = nb_getbufno(bufp);
3125 nbbuf_T *nbbuf = nb_get_buf(bufno);
3126 char buffer[2*MAXPATHL];
3127
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003128 if (!NETBEANS_OPEN || bufno == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 return;
3130
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00003131 nbdebug(("netbeans_file_killed:\n"));
3132 nbdebug((" Killing bufno: %d", bufno));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133
Bram Moolenaar89d40322006-08-29 15:30:07 +00003134 sprintf(buffer, "%d:killed=%d\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135
3136 nbdebug(("EVT: %s", buffer));
3137
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00003138 nb_send(buffer, "netbeans_file_killed");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139
3140 if (nbbuf != NULL)
3141 nbbuf->bufp = NULL;
3142}
3143
3144/*
3145 * Get a pointer to the Netbeans buffer for Vim buffer "bufp".
3146 * Return NULL if there is no such buffer or changes are not to be reported.
3147 * Otherwise store the buffer number in "*bufnop".
3148 */
3149 static nbbuf_T *
3150nb_bufp2nbbuf_fire(buf_T *bufp, int *bufnop)
3151{
3152 int bufno;
3153 nbbuf_T *nbbuf;
3154
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003155 if (!NETBEANS_OPEN || !netbeansFireChanges)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 return NULL; /* changes are not reported at all */
3157
3158 bufno = nb_getbufno(bufp);
3159 if (bufno <= 0)
3160 return NULL; /* file is not known to NetBeans */
3161
3162 nbbuf = nb_get_buf(bufno);
3163 if (nbbuf != NULL && !nbbuf->fireChanges)
3164 return NULL; /* changes in this buffer are not reported */
3165
3166 *bufnop = bufno;
3167 return nbbuf;
3168}
3169
3170/*
3171 * Tell netbeans the user inserted some text.
3172 */
3173 void
3174netbeans_inserted(
3175 buf_T *bufp,
3176 linenr_T linenr,
3177 colnr_T col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 char_u *txt,
3179 int newlen)
3180{
3181 char_u *buf;
3182 int bufno;
3183 nbbuf_T *nbbuf;
3184 pos_T pos;
3185 long off;
3186 char_u *p;
3187 char_u *newtxt;
3188
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003189 if (!NETBEANS_OPEN)
3190 return;
3191
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3193 if (nbbuf == NULL)
3194 return;
3195
Bram Moolenaar009b2592004-10-24 19:18:58 +00003196 /* Don't mark as modified for initial read */
3197 if (nbbuf->insertDone)
3198 nbbuf->modified = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199
3200 pos.lnum = linenr;
3201 pos.col = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 off = pos2off(bufp, &pos);
3203
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 /* send the "insert" EVT */
3205 newtxt = alloc(newlen + 1);
Bram Moolenaarb6356332005-07-18 21:40:44 +00003206 vim_strncpy(newtxt, txt, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 p = nb_quote(newtxt);
3208 if (p != NULL)
3209 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00003210 buf = alloc(128 + 2*newlen);
Bram Moolenaar89d40322006-08-29 15:30:07 +00003211 sprintf((char *)buf, "%d:insert=%d %ld \"%s\"\n",
3212 bufno, r_cmdno, off, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 nbdebug(("EVT: %s", buf));
3214 nb_send((char *)buf, "netbeans_inserted");
Bram Moolenaar009b2592004-10-24 19:18:58 +00003215 vim_free(p);
3216 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 vim_free(newtxt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219}
3220
3221/*
3222 * Tell netbeans some bytes have been removed.
3223 */
3224 void
3225netbeans_removed(
3226 buf_T *bufp,
3227 linenr_T linenr,
3228 colnr_T col,
3229 long len)
3230{
3231 char_u buf[128];
3232 int bufno;
3233 nbbuf_T *nbbuf;
3234 pos_T pos;
3235 long off;
3236
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003237 if (!NETBEANS_OPEN)
3238 return;
3239
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3241 if (nbbuf == NULL)
3242 return;
3243
3244 if (len < 0)
3245 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00003246 nbdebug(("Negative len %ld in netbeans_removed()!\n", len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 return;
3248 }
3249
3250 nbbuf->modified = 1;
3251
3252 pos.lnum = linenr;
3253 pos.col = col;
3254
3255 off = pos2off(bufp, &pos);
3256
Bram Moolenaar89d40322006-08-29 15:30:07 +00003257 sprintf((char *)buf, "%d:remove=%d %ld %ld\n", bufno, r_cmdno, off, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 nbdebug(("EVT: %s", buf));
3259 nb_send((char *)buf, "netbeans_removed");
3260}
3261
3262/*
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003263 * Send netbeans an unmodified command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003266netbeans_unmodified(buf_T *bufp UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267{
Bram Moolenaar09092152010-08-08 16:38:42 +02003268 /* This is a no-op, because NetBeans considers a buffer modified
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 * even when all changes have been undone. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270}
3271
3272/*
3273 * Send a button release event back to netbeans. Its up to netbeans
3274 * to decide what to do (if anything) with this event.
3275 */
3276 void
3277netbeans_button_release(int button)
3278{
3279 char buf[128];
3280 int bufno;
3281
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003282 if (!NETBEANS_OPEN)
3283 return;
3284
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 bufno = nb_getbufno(curbuf);
3286
3287 if (bufno >= 0 && curwin != NULL && curwin->w_buffer == curbuf)
3288 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003289 int col = mouse_col - W_WINCOL(curwin)
Bram Moolenaar67c53842010-05-22 18:28:27 +02003290 - ((curwin->w_p_nu || curwin->w_p_rnu) ? 9 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 long off = pos2off(curbuf, &curwin->w_cursor);
3292
3293 /* sync the cursor position */
Bram Moolenaar89d40322006-08-29 15:30:07 +00003294 sprintf(buf, "%d:newDotAndMark=%d %ld %ld\n", bufno, r_cmdno, off, off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 nbdebug(("EVT: %s", buf));
3296 nb_send(buf, "netbeans_button_release[newDotAndMark]");
3297
Bram Moolenaar89d40322006-08-29 15:30:07 +00003298 sprintf(buf, "%d:buttonRelease=%d %d %ld %d\n", bufno, r_cmdno,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 button, (long)curwin->w_cursor.lnum, col);
3300 nbdebug(("EVT: %s", buf));
3301 nb_send(buf, "netbeans_button_release");
3302 }
3303}
3304
3305
3306/*
Bram Moolenaar143c38c2007-05-10 16:41:10 +00003307 * Send a keypress event back to netbeans. This usually simulates some
Bram Moolenaar009b2592004-10-24 19:18:58 +00003308 * kind of function key press. This function operates on a key code.
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003309 * Return TRUE when the key was sent, FALSE when the command has been
3310 * postponed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 */
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003312 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313netbeans_keycommand(int key)
3314{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 char keyName[60];
Bram Moolenaar009b2592004-10-24 19:18:58 +00003316
3317 netbeans_keyname(key, keyName);
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003318 return netbeans_keystring((char_u *)keyName);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003319}
3320
3321
3322/*
Bram Moolenaar143c38c2007-05-10 16:41:10 +00003323 * Send a keypress event back to netbeans. This usually simulates some
Bram Moolenaar009b2592004-10-24 19:18:58 +00003324 * kind of function key press. This function operates on a key string.
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003325 * Return TRUE when the key was sent, FALSE when the command has been
3326 * postponed.
Bram Moolenaar009b2592004-10-24 19:18:58 +00003327 */
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003328 static int
3329netbeans_keystring(char_u *keyName)
Bram Moolenaar009b2592004-10-24 19:18:58 +00003330{
3331 char buf[2*MAXPATHL];
3332 int bufno = nb_getbufno(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 long off;
3334 char_u *q;
3335
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003336 if (!NETBEANS_OPEN)
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003337 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 if (bufno == -1)
3340 {
3341 nbdebug(("got keycommand for non-NetBeans buffer, opening...\n"));
3342 q = curbuf->b_ffname == NULL ? (char_u *)""
3343 : nb_quote(curbuf->b_ffname);
3344 if (q == NULL)
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003345 return TRUE;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003346 vim_snprintf(buf, sizeof(buf), "0:fileOpened=%d \"%s\" %s %s\n", 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 q,
3348 "T", /* open in NetBeans */
3349 "F"); /* modified */
3350 if (curbuf->b_ffname != NULL)
3351 vim_free(q);
3352 nbdebug(("EVT: %s", buf));
3353 nb_send(buf, "netbeans_keycommand");
3354
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003355 postpone_keycommand(keyName);
3356 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 }
3358
3359 /* sync the cursor position */
3360 off = pos2off(curbuf, &curwin->w_cursor);
Bram Moolenaar89d40322006-08-29 15:30:07 +00003361 sprintf(buf, "%d:newDotAndMark=%d %ld %ld\n", bufno, r_cmdno, off, off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 nbdebug(("EVT: %s", buf));
3363 nb_send(buf, "netbeans_keycommand");
3364
3365 /* To work on Win32 you must apply patch to ExtEditor module
3366 * from ExtEdCaret.java.diff - make EVT_newDotAndMark handler
3367 * more synchronous
3368 */
3369
3370 /* now send keyCommand event */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003371 vim_snprintf(buf, sizeof(buf), "%d:keyCommand=%d \"%s\"\n",
Bram Moolenaar89d40322006-08-29 15:30:07 +00003372 bufno, r_cmdno, keyName);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 nbdebug(("EVT: %s", buf));
3374 nb_send(buf, "netbeans_keycommand");
3375
3376 /* New: do both at once and include the lnum/col. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003377 vim_snprintf(buf, sizeof(buf), "%d:keyAtPos=%d \"%s\" %ld %ld/%ld\n",
Bram Moolenaar89d40322006-08-29 15:30:07 +00003378 bufno, r_cmdno, keyName,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 off, (long)curwin->w_cursor.lnum, (long)curwin->w_cursor.col);
3380 nbdebug(("EVT: %s", buf));
3381 nb_send(buf, "netbeans_keycommand");
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003382 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383}
3384
3385
3386/*
3387 * Send a save event to netbeans.
3388 */
3389 void
3390netbeans_save_buffer(buf_T *bufp)
3391{
3392 char_u buf[64];
3393 int bufno;
3394 nbbuf_T *nbbuf;
3395
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003396 if (!NETBEANS_OPEN)
3397 return;
3398
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3400 if (nbbuf == NULL)
3401 return;
3402
3403 nbbuf->modified = 0;
3404
Bram Moolenaar89d40322006-08-29 15:30:07 +00003405 sprintf((char *)buf, "%d:save=%d\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 nbdebug(("EVT: %s", buf));
3407 nb_send((char *)buf, "netbeans_save_buffer");
3408}
3409
3410
3411/*
3412 * Send remove command to netbeans (this command has been turned off).
3413 */
3414 void
3415netbeans_deleted_all_lines(buf_T *bufp)
3416{
3417 char_u buf[64];
3418 int bufno;
3419 nbbuf_T *nbbuf;
3420
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003421 if (!NETBEANS_OPEN)
3422 return;
3423
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3425 if (nbbuf == NULL)
3426 return;
3427
Bram Moolenaar009b2592004-10-24 19:18:58 +00003428 /* Don't mark as modified for initial read */
3429 if (nbbuf->insertDone)
3430 nbbuf->modified = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431
Bram Moolenaar89d40322006-08-29 15:30:07 +00003432 sprintf((char *)buf, "%d:remove=%d 0 -1\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 nbdebug(("EVT(suppressed): %s", buf));
3434/* nb_send(buf, "netbeans_deleted_all_lines"); */
3435}
3436
3437
3438/*
3439 * See if the lines are guarded. The top and bot parameters are from
3440 * u_savecommon(), these are the line above the change and the line below the
3441 * change.
3442 */
3443 int
3444netbeans_is_guarded(linenr_T top, linenr_T bot)
3445{
3446 signlist_T *p;
3447 int lnum;
3448
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003449 if (!NETBEANS_OPEN)
3450 return FALSE;
3451
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 for (p = curbuf->b_signlist; p != NULL; p = p->next)
3453 if (p->id >= GUARDEDOFFSET)
3454 for (lnum = top + 1; lnum < bot; lnum++)
3455 if (lnum == p->lnum)
3456 return TRUE;
3457
3458 return FALSE;
3459}
3460
Bram Moolenaar173c9852010-09-29 17:27:01 +02003461#if defined(FEAT_GUI_X11) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462/*
3463 * We have multiple signs to draw at the same location. Draw the
3464 * multi-sign indicator instead. This is the Motif version.
3465 */
3466 void
3467netbeans_draw_multisign_indicator(int row)
3468{
3469 int i;
3470 int y;
3471 int x;
3472
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003473 if (!NETBEANS_OPEN)
3474 return;
3475
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 x = 0;
3477 y = row * gui.char_height + 2;
3478
3479 for (i = 0; i < gui.char_height - 3; i++)
3480 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y++);
3481
3482 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+0, y);
3483 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3484 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+4, y++);
3485 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+1, y);
3486 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3487 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+3, y++);
3488 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3489}
Bram Moolenaar173c9852010-09-29 17:27:01 +02003490#endif /* FEAT_GUI_X11 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491
Bram Moolenaar64404472010-06-26 06:24:45 +02003492#if defined(FEAT_GUI_GTK) && !defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493/*
3494 * We have multiple signs to draw at the same location. Draw the
3495 * multi-sign indicator instead. This is the GTK/Gnome version.
3496 */
3497 void
3498netbeans_draw_multisign_indicator(int row)
3499{
3500 int i;
3501 int y;
3502 int x;
3503 GdkDrawable *drawable = gui.drawarea->window;
3504
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003505 if (!NETBEANS_OPEN)
3506 return;
3507
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 x = 0;
3509 y = row * gui.char_height + 2;
3510
3511 for (i = 0; i < gui.char_height - 3; i++)
3512 gdk_draw_point(drawable, gui.text_gc, x+2, y++);
3513
3514 gdk_draw_point(drawable, gui.text_gc, x+0, y);
3515 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3516 gdk_draw_point(drawable, gui.text_gc, x+4, y++);
3517 gdk_draw_point(drawable, gui.text_gc, x+1, y);
3518 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3519 gdk_draw_point(drawable, gui.text_gc, x+3, y++);
3520 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3521}
3522#endif /* FEAT_GUI_GTK */
3523
3524/*
3525 * If the mouse is clicked in the gutter of a line with multiple
3526 * annotations, cycle through the set of signs.
3527 */
3528 void
3529netbeans_gutter_click(linenr_T lnum)
3530{
3531 signlist_T *p;
3532
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003533 if (!NETBEANS_OPEN)
3534 return;
3535
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 for (p = curbuf->b_signlist; p != NULL; p = p->next)
3537 {
3538 if (p->lnum == lnum && p->next && p->next->lnum == lnum)
3539 {
3540 signlist_T *tail;
3541
3542 /* remove "p" from list, reinsert it at the tail of the sublist */
3543 if (p->prev)
3544 p->prev->next = p->next;
3545 else
3546 curbuf->b_signlist = p->next;
3547 p->next->prev = p->prev;
3548 /* now find end of sublist and insert p */
3549 for (tail = p->next;
3550 tail->next && tail->next->lnum == lnum
3551 && tail->next->id < GUARDEDOFFSET;
3552 tail = tail->next)
3553 ;
3554 /* tail now points to last entry with same lnum (except
3555 * that "guarded" annotations are always last) */
3556 p->next = tail->next;
3557 if (tail->next)
3558 tail->next->prev = p;
3559 p->prev = tail;
3560 tail->next = p;
3561 update_debug_sign(curbuf, lnum);
3562 break;
3563 }
3564 }
3565}
3566
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567/*
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003568 * Add a sign of the requested type at the requested location.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 *
3570 * Reverse engineering:
3571 * Apparently an annotation is defined the first time it is used in a buffer.
3572 * When the same annotation is used in two buffers, the second time we do not
3573 * need to define a new sign name but reuse the existing one. But since the
3574 * ID number used in the second buffer starts counting at one again, a mapping
3575 * is made from the ID specifically for the buffer to the global sign name
3576 * (which is a number).
3577 *
3578 * globalsignmap[] stores the signs that have been defined globally.
3579 * buf->signmapused[] maps buffer-local annotation IDs to an index in
3580 * globalsignmap[].
3581 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 static void
3583addsigntype(
3584 nbbuf_T *buf,
3585 int typeNum,
3586 char_u *typeName,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003587 char_u *tooltip UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 char_u *glyphFile,
Bram Moolenaar67c53842010-05-22 18:28:27 +02003589 char_u *fg,
3590 char_u *bg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 int i, j;
Bram Moolenaar67c53842010-05-22 18:28:27 +02003593 int use_fg = (*fg && STRCMP(fg, "none") != 0);
3594 int use_bg = (*bg && STRCMP(bg, "none") != 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595
3596 for (i = 0; i < globalsignmapused; i++)
3597 if (STRCMP(typeName, globalsignmap[i]) == 0)
3598 break;
3599
3600 if (i == globalsignmapused) /* not found; add it to global map */
3601 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003602 nbdebug(("DEFINEANNOTYPE(%d,%s,%s,%s,%s,%s)\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 typeNum, typeName, tooltip, glyphFile, fg, bg));
3604 if (use_fg || use_bg)
3605 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003606 char fgbuf[2 * (8 + MAX_COLOR_LENGTH) + 1];
3607 char bgbuf[2 * (8 + MAX_COLOR_LENGTH) + 1];
3608 char *ptr;
3609 int value;
3610
3611 value = strtol((char *)fg, &ptr, 10);
3612 if (ptr != (char *)fg)
3613 sprintf(fgbuf, "guifg=#%06x", value & 0xFFFFFF);
3614 else
3615 sprintf(fgbuf, "guifg=%s ctermfg=%s", fg, fg);
3616
3617 value = strtol((char *)bg, &ptr, 10);
3618 if (ptr != (char *)bg)
3619 sprintf(bgbuf, "guibg=#%06x", value & 0xFFFFFF);
3620 else
3621 sprintf(bgbuf, "guibg=%s ctermbg=%s", bg, bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622
3623 coloncmd(":highlight NB_%s %s %s", typeName, (use_fg) ? fgbuf : "",
3624 (use_bg) ? bgbuf : "");
3625 if (*glyphFile == NUL)
3626 /* no glyph, line highlighting only */
3627 coloncmd(":sign define %d linehl=NB_%s", i + 1, typeName);
3628 else if (vim_strsize(glyphFile) <= 2)
3629 /* one- or two-character glyph name, use as text glyph with
3630 * texthl */
3631 coloncmd(":sign define %d text=%s texthl=NB_%s", i + 1,
3632 glyphFile, typeName);
3633 else
3634 /* glyph, line highlighting */
3635 coloncmd(":sign define %d icon=%s linehl=NB_%s", i + 1,
3636 glyphFile, typeName);
3637 }
3638 else
3639 /* glyph, no line highlighting */
3640 coloncmd(":sign define %d icon=%s", i + 1, glyphFile);
3641
3642 if (STRCMP(typeName,"CurrentPC") == 0)
3643 curPCtype = typeNum;
3644
3645 if (globalsignmapused == globalsignmaplen)
3646 {
3647 if (globalsignmaplen == 0) /* first allocation */
3648 {
3649 globalsignmaplen = 20;
3650 globalsignmap = (char **)alloc_clear(globalsignmaplen*sizeof(char *));
3651 }
3652 else /* grow it */
3653 {
3654 int incr;
3655 int oldlen = globalsignmaplen;
3656
3657 globalsignmaplen *= 2;
3658 incr = globalsignmaplen - oldlen;
3659 globalsignmap = (char **)vim_realloc(globalsignmap,
3660 globalsignmaplen * sizeof(char *));
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003661 vim_memset(globalsignmap + oldlen, 0, incr * sizeof(char *));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 }
3663 }
3664
3665 globalsignmap[i] = (char *)typeName;
3666 globalsignmapused = i + 1;
3667 }
3668
3669 /* check local map; should *not* be found! */
3670 for (j = 0; j < buf->signmapused; j++)
3671 if (buf->signmap[j] == i + 1)
3672 return;
3673
3674 /* add to local map */
3675 if (buf->signmapused == buf->signmaplen)
3676 {
3677 if (buf->signmaplen == 0) /* first allocation */
3678 {
3679 buf->signmaplen = 5;
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003680 buf->signmap = (int *)alloc_clear(buf->signmaplen * sizeof(int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 }
3682 else /* grow it */
3683 {
3684 int incr;
3685 int oldlen = buf->signmaplen;
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003686
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 buf->signmaplen *= 2;
3688 incr = buf->signmaplen - oldlen;
3689 buf->signmap = (int *)vim_realloc(buf->signmap,
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003690 buf->signmaplen * sizeof(int));
3691 vim_memset(buf->signmap + oldlen, 0, incr * sizeof(int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 }
3693 }
3694
3695 buf->signmap[buf->signmapused++] = i + 1;
3696
3697}
3698
3699
3700/*
3701 * See if we have the requested sign type in the buffer.
3702 */
3703 static int
3704mapsigntype(nbbuf_T *buf, int localsigntype)
3705{
3706 if (--localsigntype >= 0 && localsigntype < buf->signmapused)
3707 return buf->signmap[localsigntype];
3708
3709 return 0;
3710}
3711
3712
3713/*
3714 * Compute length of buffer, don't print anything.
3715 */
3716 static long
3717get_buf_size(buf_T *bufp)
3718{
3719 linenr_T lnum;
3720 long char_count = 0;
3721 int eol_size;
3722 long last_check = 100000L;
3723
3724 if (bufp->b_ml.ml_flags & ML_EMPTY)
3725 return 0;
3726 else
3727 {
3728 if (get_fileformat(bufp) == EOL_DOS)
3729 eol_size = 2;
3730 else
3731 eol_size = 1;
3732 for (lnum = 1; lnum <= bufp->b_ml.ml_line_count; ++lnum)
3733 {
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00003734 char_count += (long)STRLEN(ml_get_buf(bufp, lnum, FALSE))
3735 + eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 /* Check for a CTRL-C every 100000 characters */
3737 if (char_count > last_check)
3738 {
3739 ui_breakcheck();
3740 if (got_int)
3741 return char_count;
3742 last_check = char_count + 100000L;
3743 }
3744 }
3745 /* Correction for when last line doesn't have an EOL. */
3746 if (!bufp->b_p_eol && bufp->b_p_bin)
3747 char_count -= eol_size;
3748 }
3749
3750 return char_count;
3751}
3752
3753/*
3754 * Convert character offset to lnum,col
3755 */
3756 static pos_T *
3757off2pos(buf_T *buf, long offset)
3758{
3759 linenr_T lnum;
3760 static pos_T pos;
3761
3762 pos.lnum = 0;
3763 pos.col = 0;
3764#ifdef FEAT_VIRTUALEDIT
3765 pos.coladd = 0;
3766#endif
3767
3768 if (!(buf->b_ml.ml_flags & ML_EMPTY))
3769 {
3770 if ((lnum = ml_find_line_or_offset(buf, (linenr_T)0, &offset)) < 0)
3771 return NULL;
3772 pos.lnum = lnum;
3773 pos.col = offset;
3774 }
3775
3776 return &pos;
3777}
3778
3779/*
3780 * Convert an argument in the form "1234" to an offset and compute the
3781 * lnum/col from it. Convert an argument in the form "123/12" directly to a
3782 * lnum/col.
3783 * "argp" is advanced to after the argument.
3784 * Return a pointer to the position, NULL if something is wrong.
3785 */
3786 static pos_T *
3787get_off_or_lnum(buf_T *buf, char_u **argp)
3788{
3789 static pos_T mypos;
3790 long off;
3791
3792 off = strtol((char *)*argp, (char **)argp, 10);
3793 if (**argp == '/')
3794 {
3795 mypos.lnum = (linenr_T)off;
3796 ++*argp;
3797 mypos.col = strtol((char *)*argp, (char **)argp, 10);
3798#ifdef FEAT_VIRTUALEDIT
3799 mypos.coladd = 0;
3800#endif
3801 return &mypos;
3802 }
3803 return off2pos(buf, off);
3804}
3805
3806
3807/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003808 * Convert (lnum,col) to byte offset in the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 */
3810 static long
3811pos2off(buf_T *buf, pos_T *pos)
3812{
3813 long offset = 0;
3814
3815 if (!(buf->b_ml.ml_flags & ML_EMPTY))
3816 {
3817 if ((offset = ml_find_line_or_offset(buf, pos->lnum, 0)) < 0)
3818 return 0;
3819 offset += pos->col;
3820 }
3821
3822 return offset;
3823}
3824
3825
Bram Moolenaar009b2592004-10-24 19:18:58 +00003826/*
3827 * This message is printed after NetBeans opens a new file. Its
3828 * similar to the message readfile() uses, but since NetBeans
3829 * doesn't normally call readfile, we do our own.
3830 */
3831 static void
3832print_read_msg(buf)
3833 nbbuf_T *buf;
3834{
3835 int lnum = buf->bufp->b_ml.ml_line_count;
Bram Moolenaar914703b2010-05-31 21:59:46 +02003836 off_t nchars = buf->bufp->b_orig_size;
Bram Moolenaar009b2592004-10-24 19:18:58 +00003837 char_u c;
3838
3839 msg_add_fname(buf->bufp, buf->bufp->b_ffname);
3840 c = FALSE;
3841
3842 if (buf->bufp->b_p_ro)
3843 {
3844 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]"));
3845 c = TRUE;
3846 }
3847 if (!buf->bufp->b_start_eol)
3848 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003849 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]")
3850 : _("[Incomplete last line]"));
Bram Moolenaar009b2592004-10-24 19:18:58 +00003851 c = TRUE;
3852 }
3853 msg_add_lines(c, (long)lnum, nchars);
3854
3855 /* Now display it */
3856 vim_free(keep_msg);
3857 keep_msg = NULL;
3858 msg_scrolled_ign = TRUE;
3859 msg_trunc_attr(IObuff, FALSE, 0);
3860 msg_scrolled_ign = FALSE;
3861}
3862
3863
3864/*
Bram Moolenaar67c53842010-05-22 18:28:27 +02003865 * Print a message after NetBeans writes the file. This message should be
3866 * identical to the standard message a non-netbeans user would see when
3867 * writing a file.
Bram Moolenaar009b2592004-10-24 19:18:58 +00003868 */
3869 static void
3870print_save_msg(buf, nchars)
3871 nbbuf_T *buf;
Bram Moolenaar914703b2010-05-31 21:59:46 +02003872 off_t nchars;
Bram Moolenaar009b2592004-10-24 19:18:58 +00003873{
3874 char_u c;
3875 char_u *p;
3876
3877 if (nchars >= 0)
3878 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003879 /* put fname in IObuff with quotes */
3880 msg_add_fname(buf->bufp, buf->bufp->b_ffname);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003881 c = FALSE;
3882
3883 msg_add_lines(c, buf->bufp->b_ml.ml_line_count,
Bram Moolenaar914703b2010-05-31 21:59:46 +02003884 buf->bufp->b_orig_size);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003885
3886 vim_free(keep_msg);
3887 keep_msg = NULL;
3888 msg_scrolled_ign = TRUE;
3889 p = msg_trunc_attr(IObuff, FALSE, 0);
3890 if ((msg_scrolled && !need_wait_return) || !buf->initDone)
3891 {
3892 /* Need to repeat the message after redrawing when:
3893 * - When reading from stdin (the screen will be cleared next).
3894 * - When restart_edit is set (otherwise there will be a delay
3895 * before redrawing).
3896 * - When the screen was scrolled but there is no wait-return
3897 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003898 set_keep_msg(p, 0);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003899 }
3900 msg_scrolled_ign = FALSE;
3901 /* add_to_input_buf((char_u *)"\f", 1); */
3902 }
3903 else
3904 {
3905 char_u ebuf[BUFSIZ];
3906
3907 STRCPY(ebuf, (char_u *)_("E505: "));
3908 STRCAT(ebuf, IObuff);
3909 STRCAT(ebuf, (char_u *)_("is read-only (add ! to override)"));
3910 STRCPY(IObuff, ebuf);
Bram Moolenaarf2330482008-06-24 20:19:36 +00003911 nbdebug((" %s\n", ebuf ));
Bram Moolenaar009b2592004-10-24 19:18:58 +00003912 emsg(IObuff);
3913 }
3914}
3915
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916#endif /* defined(FEAT_NETBEANS_INTG) */