blob: 9e31fba0affb8635034dbf31d455cfaa2ecc4097 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * Netbeans integration by David Weatherford
5 * Adopted for Win32 by Sergey Khorev
6 *
7 * Do ":help uganda" in Vim to read copying and usage conditions.
8 * Do ":help credits" in Vim to see a list of people who contributed.
9 */
10
11/*
12 * Implements client side of org.netbeans.modules.emacs editor
13 * integration protocol. Be careful! The protocol uses offsets
14 * which are *between* characters, whereas vim uses line number
15 * and column number which are *on* characters.
16 * See ":help netbeans-protocol" for explanation.
17 */
18
Bram Moolenaarc236c162008-07-13 17:41:49 +000019#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
Bram Moolenaarff064e12008-06-09 13:10:45 +000020# include "vimio.h" /* for mch_open(), must be before vim.h */
21#endif
22
Bram Moolenaar071d4272004-06-13 20:20:40 +000023#include "vim.h"
24
25#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
26
Bram Moolenaarb26e6322010-05-22 21:34:09 +020027/* TODO: when should this not be defined? */
28#define INET_SOCKETS
29
Bram Moolenaar071d4272004-06-13 20:20:40 +000030/* Note: when making changes here also adjust configure.in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000031#ifdef WIN32
32# ifdef DEBUG
33# include <tchar.h> /* for _T definition for TRACEn macros */
34# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000035/* WinSock API is separated from C API, thus we can't use read(), write(),
36 * errno... */
Bram Moolenaarc39125d2010-05-23 12:06:58 +020037# define SOCK_ERRNO errno = WSAGetLastError()
Bram Moolenaarbd42a0f2009-06-16 14:57:26 +000038# undef ECONNREFUSED
Bram Moolenaar071d4272004-06-13 20:20:40 +000039# define ECONNREFUSED WSAECONNREFUSED
40# ifdef EINTR
41# undef EINTR
42# endif
43# define EINTR WSAEINTR
44# define sock_write(sd, buf, len) send(sd, buf, len, 0)
45# define sock_read(sd, buf, len) recv(sd, buf, len, 0)
46# define sock_close(sd) closesocket(sd)
47# define sleep(t) Sleep(t*1000) /* WinAPI Sleep() accepts milliseconds */
48#else
Bram Moolenaarb26e6322010-05-22 21:34:09 +020049# ifdef INET_SOCKETS
50# include <netdb.h>
51# include <netinet/in.h>
52# else
53# include <sys/un.h>
54# endif
55
Bram Moolenaar071d4272004-06-13 20:20:40 +000056# include <sys/socket.h>
57# ifdef HAVE_LIBGEN_H
58# include <libgen.h>
59# endif
Bram Moolenaarc39125d2010-05-23 12:06:58 +020060# define SOCK_ERRNO
Bram Moolenaar071d4272004-06-13 20:20:40 +000061# define sock_write(sd, buf, len) write(sd, buf, len)
62# define sock_read(sd, buf, len) read(sd, buf, len)
63# define sock_close(sd) close(sd)
64#endif
65
66#include "version.h"
67
Bram Moolenaar071d4272004-06-13 20:20:40 +000068#define GUARDED 10000 /* typenr for "guarded" annotation */
69#define GUARDEDOFFSET 1000000 /* base for "guarded" sign id's */
Bram Moolenaar67c53842010-05-22 18:28:27 +020070#define MAX_COLOR_LENGTH 32 /* max length of color name in defineAnnoType */
Bram Moolenaar071d4272004-06-13 20:20:40 +000071
72/* The first implementation (working only with Netbeans) returned "1.1". The
73 * protocol implemented here also supports A-A-P. */
Bram Moolenaar67c53842010-05-22 18:28:27 +020074static char *ExtEdProtocolVersion = "2.5";
Bram Moolenaar071d4272004-06-13 20:20:40 +000075
76static long pos2off __ARGS((buf_T *, pos_T *));
77static pos_T *off2pos __ARGS((buf_T *, long));
78static pos_T *get_off_or_lnum __ARGS((buf_T *buf, char_u **argp));
79static long get_buf_size __ARGS((buf_T *));
Bram Moolenaar8065d7f2010-01-19 15:13:14 +010080static int netbeans_keystring __ARGS((char_u *keystr));
81static void postpone_keycommand __ARGS((char_u *keystr));
Bram Moolenaar009b2592004-10-24 19:18:58 +000082static void special_keys __ARGS((char_u *args));
Bram Moolenaar071d4272004-06-13 20:20:40 +000083
Bram Moolenaarb26e6322010-05-22 21:34:09 +020084static int netbeans_connect __ARGS((char *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +000085static int getConnInfo __ARGS((char *file, char **host, char **port, char **password));
86
87static void nb_init_graphics __ARGS((void));
88static void coloncmd __ARGS((char *cmd, ...));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000089static void nb_set_curbuf __ARGS((buf_T *buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000090#ifdef FEAT_GUI_MOTIF
91static void messageFromNetbeans __ARGS((XtPointer, int *, XtInputId *));
92#endif
93#ifdef FEAT_GUI_GTK
94static void messageFromNetbeans __ARGS((gpointer, gint, GdkInputCondition));
95#endif
96static void nb_parse_cmd __ARGS((char_u *));
97static int nb_do_cmd __ARGS((int, char_u *, int, int, char_u *));
98static void nb_send __ARGS((char *buf, char *fun));
Bram Moolenaarb26e6322010-05-22 21:34:09 +020099static void nb_free __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100
Bram Moolenaar67c53842010-05-22 18:28:27 +0200101/* TRUE when netbeans is running with a GUI. */
102#ifdef FEAT_GUI
103# define NB_HAS_GUI (gui.in_use || gui.starting)
104#endif
105
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000106#ifdef WIN64
107typedef __int64 NBSOCK;
108#else
109typedef int NBSOCK;
110#endif
111
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200112static NBSOCK nbsock = -1; /* socket fd for Netbeans connection */
113#define NETBEANS_OPEN (nbsock != -1)
114
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115#ifdef FEAT_GUI_MOTIF
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200116static XtInputId inputHandler = (XtInputId)NULL; /* Cookie for input */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117#endif
118#ifdef FEAT_GUI_GTK
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200119static gint inputHandler = 0; /* Cookie for input */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120#endif
121#ifdef FEAT_GUI_W32
122static int inputHandler = -1; /* simply ret.value of WSAAsyncSelect() */
123extern HWND s_hwnd; /* Gvim's Window handle */
124#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +0000125static int r_cmdno; /* current command number for reply */
Bram Moolenaar009b2592004-10-24 19:18:58 +0000126static int dosetvisible = FALSE;
127
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128/*
129 * Include the debugging code if wanted.
130 */
131#ifdef NBDEBUG
132# include "nbdebug.c"
133#endif
134
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200135static int needupdate = 0;
136static int inAtomic = 0;
137
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138 static void
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200139netbeans_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140{
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200141 if (!NETBEANS_OPEN)
142 return;
143
144 netbeans_send_disconnect();
145
Bram Moolenaar67c53842010-05-22 18:28:27 +0200146#ifdef FEAT_GUI_MOTIF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147 if (inputHandler != (XtInputId)NULL)
148 {
149 XtRemoveInput(inputHandler);
150 inputHandler = (XtInputId)NULL;
151 }
Bram Moolenaar67c53842010-05-22 18:28:27 +0200152#else
153# ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154 if (inputHandler != 0)
155 {
156 gdk_input_remove(inputHandler);
157 inputHandler = 0;
158 }
Bram Moolenaar67c53842010-05-22 18:28:27 +0200159# else
160# ifdef FEAT_GUI_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 if (inputHandler == 0)
162 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200163 WSAAsyncSelect(nbsock, s_hwnd, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164 inputHandler = -1;
165 }
Bram Moolenaar67c53842010-05-22 18:28:27 +0200166# endif
167# endif
168#endif
169
Bram Moolenaar67c53842010-05-22 18:28:27 +0200170#ifdef FEAT_BEVAL
Bram Moolenaard62bec82005-03-07 22:56:57 +0000171 bevalServers &= ~BEVAL_NETBEANS;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200172#endif
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200173
174 sock_close(nbsock);
175 nbsock = -1;
176
177 needupdate = 0;
178 inAtomic = 0;
179 nb_free();
180
181 /* remove all signs and update the screen after gutter removal */
182 coloncmd(":sign unplace *");
183 changed_window_setting();
184 update_screen(CLEAR);
185 setcursor();
186 out_flush();
187#ifdef FEAT_GUI
188 gui_update_cursor(TRUE, FALSE);
189 gui_mch_flush();
190#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192
193#define NB_DEF_HOST "localhost"
194#define NB_DEF_ADDR "3219"
195#define NB_DEF_PASS "changeme"
196
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200197 static int
198netbeans_connect(char *params, int abort)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199{
200#ifdef INET_SOCKETS
201 struct sockaddr_in server;
202 struct hostent * host;
203# ifdef FEAT_GUI_W32
204 u_short port;
205# else
206 int port;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200207# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208#else
209 struct sockaddr_un server;
210#endif
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200211 int sd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212 char buf[32];
213 char *hostname = NULL;
214 char *address = NULL;
215 char *password = NULL;
216 char *fname;
217 char *arg = NULL;
218
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200219 if (*params == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200221 /* "=fname": Read info from specified file. */
222 if (getConnInfo(params + 1, &hostname, &address, &password)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223 == FAIL)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200224 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225 }
226 else
227 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200228 if (*params == ':')
229 /* ":<host>:<addr>:<password>": get info from argument */
230 arg = params + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231 if (arg == NULL && (fname = getenv("__NETBEANS_CONINFO")) != NULL)
232 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200233 /* "": get info from file specified in environment */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234 if (getConnInfo(fname, &hostname, &address, &password) == FAIL)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200235 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236 }
237 else
238 {
239 if (arg != NULL)
240 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200241 /* ":<host>:<addr>:<password>": get info from argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 hostname = arg;
243 address = strchr(hostname, ':');
244 if (address != NULL)
245 {
246 *address++ = '\0';
247 password = strchr(address, ':');
248 if (password != NULL)
249 *password++ = '\0';
250 }
251 }
252
253 /* Get the missing values from the environment. */
254 if (hostname == NULL || *hostname == '\0')
255 hostname = getenv("__NETBEANS_HOST");
256 if (address == NULL)
257 address = getenv("__NETBEANS_SOCKET");
258 if (password == NULL)
259 password = getenv("__NETBEANS_VIM_PASSWORD");
260
261 /* Move values to allocated memory. */
262 if (hostname != NULL)
263 hostname = (char *)vim_strsave((char_u *)hostname);
264 if (address != NULL)
265 address = (char *)vim_strsave((char_u *)address);
266 if (password != NULL)
267 password = (char *)vim_strsave((char_u *)password);
268 }
269 }
270
271 /* Use the default when a value is missing. */
272 if (hostname == NULL || *hostname == '\0')
273 {
274 vim_free(hostname);
275 hostname = (char *)vim_strsave((char_u *)NB_DEF_HOST);
276 }
277 if (address == NULL || *address == '\0')
278 {
279 vim_free(address);
280 address = (char *)vim_strsave((char_u *)NB_DEF_ADDR);
281 }
282 if (password == NULL || *password == '\0')
283 {
284 vim_free(password);
285 password = (char *)vim_strsave((char_u *)NB_DEF_PASS);
286 }
287 if (hostname == NULL || address == NULL || password == NULL)
288 goto theend; /* out of memory */
289
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200290#ifdef FEAT_GUI_W32
291 netbeans_init_winsock();
292#endif
293
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294#ifdef INET_SOCKETS
295 port = atoi(address);
296
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000297 if ((sd = (NBSOCK)socket(AF_INET, SOCK_STREAM, 0)) == (NBSOCK)-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000299 nbdebug(("error in socket() in netbeans_connect()\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300 PERROR("socket() in netbeans_connect()");
301 goto theend;
302 }
303
304 /* Get the server internet address and put into addr structure */
305 /* fill in the socket address structure and connect to server */
306 memset((char *)&server, '\0', sizeof(server));
307 server.sin_family = AF_INET;
308 server.sin_port = htons(port);
309 if ((host = gethostbyname(hostname)) == NULL)
310 {
311 if (mch_access(hostname, R_OK) >= 0)
312 {
313 /* DEBUG: input file */
314 sd = mch_open(hostname, O_RDONLY, 0);
315 goto theend;
316 }
Bram Moolenaarf2330482008-06-24 20:19:36 +0000317 nbdebug(("error in gethostbyname() in netbeans_connect()\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318 PERROR("gethostbyname() in netbeans_connect()");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319 goto theend;
320 }
321 memcpy((char *)&server.sin_addr, host->h_addr, host->h_length);
322#else
323 if ((sd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
324 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000325 nbdebug(("error in socket() in netbeans_connect()\n"));
326 PERROR("socket() in netbeans_connect()");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327 goto theend;
328 }
329
330 server.sun_family = AF_UNIX;
331 strcpy(server.sun_path, address);
332#endif
333 /* Connect to server */
334 if (connect(sd, (struct sockaddr *)&server, sizeof(server)))
335 {
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200336 SOCK_ERRNO;
337 nbdebug(("netbeans_connect: Connect failed with errno %d\n", errno));
338 if (errno == ECONNREFUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 {
340 sock_close(sd);
341#ifdef INET_SOCKETS
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000342 if ((sd = (NBSOCK)socket(AF_INET, SOCK_STREAM, 0)) == (NBSOCK)-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 {
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200344 SOCK_ERRNO;
Bram Moolenaarf2330482008-06-24 20:19:36 +0000345 nbdebug(("socket()#2 in netbeans_connect()\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 PERROR("socket()#2 in netbeans_connect()");
347 goto theend;
348 }
349#else
350 if ((sd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
351 {
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200352 SOCK_ERRNO;
Bram Moolenaarf2330482008-06-24 20:19:36 +0000353 nbdebug(("socket()#2 in netbeans_connect()\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354 PERROR("socket()#2 in netbeans_connect()");
355 goto theend;
356 }
357#endif
358 if (connect(sd, (struct sockaddr *)&server, sizeof(server)))
359 {
360 int retries = 36;
361 int success = FALSE;
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200362
363 SOCK_ERRNO;
364 while (retries-- && ((errno == ECONNREFUSED)
365 || (errno == EINTR)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366 {
367 nbdebug(("retrying...\n"));
368 sleep(5);
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200369 if (!abort)
370 {
371 ui_breakcheck();
372 if (got_int)
373 {
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200374 errno = EINTR;
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200375 break;
376 }
377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 if (connect(sd, (struct sockaddr *)&server,
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200379 sizeof(server)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380 {
381 success = TRUE;
382 break;
383 }
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200384 SOCK_ERRNO;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385 }
386 if (!success)
387 {
388 /* Get here when the server can't be found. */
Bram Moolenaarf2330482008-06-24 20:19:36 +0000389 nbdebug(("Cannot connect to Netbeans #2\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390 PERROR(_("Cannot connect to Netbeans #2"));
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200391 if (abort)
392 getout(1);
393 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 }
395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396 }
397 else
398 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000399 nbdebug(("Cannot connect to Netbeans\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 PERROR(_("Cannot connect to Netbeans"));
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200401 if (abort)
402 getout(1);
403 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404 }
405 }
406
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200407 nbsock = sd;
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000408 vim_snprintf(buf, sizeof(buf), "AUTH %s\n", password);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 nb_send(buf, "netbeans_connect");
410
411 sprintf(buf, "0:version=0 \"%s\"\n", ExtEdProtocolVersion);
412 nb_send(buf, "externaleditor_version");
413
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414theend:
415 vim_free(hostname);
416 vim_free(address);
417 vim_free(password);
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200418 return NETBEANS_OPEN ? OK : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419}
420
421/*
422 * Obtain the NetBeans hostname, port address and password from a file.
423 * Return the strings in allocated memory.
424 * Return FAIL if the file could not be read, OK otherwise (no matter what it
425 * contains).
426 */
427 static int
428getConnInfo(char *file, char **host, char **port, char **auth)
429{
430 FILE *fp;
431 char_u buf[BUFSIZ];
432 char_u *lp;
433 char_u *nl;
434#ifdef UNIX
435 struct stat st;
436
437 /*
438 * For Unix only accept the file when it's not accessible by others.
439 * The open will then fail if we don't own the file.
440 */
441 if (mch_stat(file, &st) == 0 && (st.st_mode & 0077) != 0)
442 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000443 nbdebug(("Wrong access mode for NetBeans connection info file: \"%s\"\n",
444 file));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 EMSG2(_("E668: Wrong access mode for NetBeans connection info file: \"%s\""),
446 file);
447 return FAIL;
448 }
449#endif
450
451 fp = mch_fopen(file, "r");
452 if (fp == NULL)
453 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000454 nbdebug(("Cannot open NetBeans connection info file\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455 PERROR("E660: Cannot open NetBeans connection info file");
456 return FAIL;
457 }
458
459 /* Read the file. There should be one of each parameter */
460 while ((lp = (char_u *)fgets((char *)buf, BUFSIZ, fp)) != NULL)
461 {
462 if ((nl = vim_strchr(lp, '\n')) != NULL)
463 *nl = 0; /* strip off the trailing newline */
464
465 if (STRNCMP(lp, "host=", 5) == 0)
466 {
467 vim_free(*host);
468 *host = (char *)vim_strsave(&buf[5]);
469 }
470 else if (STRNCMP(lp, "port=", 5) == 0)
471 {
472 vim_free(*port);
473 *port = (char *)vim_strsave(&buf[5]);
474 }
475 else if (STRNCMP(lp, "auth=", 5) == 0)
476 {
477 vim_free(*auth);
478 *auth = (char *)vim_strsave(&buf[5]);
479 }
480 }
481 fclose(fp);
482
483 return OK;
484}
485
486
487struct keyqueue
488{
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100489 char_u *keystr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 struct keyqueue *next;
491 struct keyqueue *prev;
492};
493
494typedef struct keyqueue keyQ_T;
495
496static keyQ_T keyHead; /* dummy node, header for circular queue */
497
498
499/*
500 * Queue up key commands sent from netbeans.
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100501 * We store the string, because it may depend on the global mod_mask and
502 * :nbkey doesn't have a key number.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 */
504 static void
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100505postpone_keycommand(char_u *keystr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506{
507 keyQ_T *node;
508
509 node = (keyQ_T *)alloc(sizeof(keyQ_T));
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100510 if (node == NULL)
511 return; /* out of memory, drop the key */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512
513 if (keyHead.next == NULL) /* initialize circular queue */
514 {
515 keyHead.next = &keyHead;
516 keyHead.prev = &keyHead;
517 }
518
519 /* insert node at tail of queue */
520 node->next = &keyHead;
521 node->prev = keyHead.prev;
522 keyHead.prev->next = node;
523 keyHead.prev = node;
524
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100525 node->keystr = vim_strsave(keystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526}
527
528/*
529 * Handle any queued-up NetBeans keycommands to be send.
530 */
531 static void
532handle_key_queue(void)
533{
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100534 int postponed = FALSE;
535
536 while (!postponed && keyHead.next && keyHead.next != &keyHead)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 {
538 /* first, unlink the node */
539 keyQ_T *node = keyHead.next;
540 keyHead.next = node->next;
541 node->next->prev = node->prev;
542
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100543 /* Now, send the keycommand. This may cause it to be postponed again
544 * and change keyHead. */
545 if (node->keystr != NULL)
546 postponed = !netbeans_keystring(node->keystr);
547 vim_free(node->keystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548
549 /* Finally, dispose of the node */
550 vim_free(node);
551 }
552}
553
554
555struct cmdqueue
556{
557 char_u *buffer;
558 struct cmdqueue *next;
559 struct cmdqueue *prev;
560};
561
562typedef struct cmdqueue queue_T;
563
564static queue_T head; /* dummy node, header for circular queue */
565
566
567/*
568 * Put the buffer on the work queue; possibly save it to a file as well.
569 */
570 static void
571save(char_u *buf, int len)
572{
573 queue_T *node;
574
575 node = (queue_T *)alloc(sizeof(queue_T));
576 if (node == NULL)
577 return; /* out of memory */
578 node->buffer = alloc(len + 1);
579 if (node->buffer == NULL)
580 {
581 vim_free(node);
582 return; /* out of memory */
583 }
584 mch_memmove(node->buffer, buf, (size_t)len);
585 node->buffer[len] = NUL;
586
587 if (head.next == NULL) /* initialize circular queue */
588 {
589 head.next = &head;
590 head.prev = &head;
591 }
592
593 /* insert node at tail of queue */
594 node->next = &head;
595 node->prev = head.prev;
596 head.prev->next = node;
597 head.prev = node;
598
599#ifdef NBDEBUG
600 {
601 static int outfd = -2;
602
603 /* possibly write buffer out to a file */
604 if (outfd == -3)
605 return;
606
607 if (outfd == -2)
608 {
609 char *file = getenv("__NETBEANS_SAVE");
610 if (file == NULL)
611 outfd = -3;
612 else
613 outfd = mch_open(file, O_WRONLY|O_CREAT|O_TRUNC, 0666);
614 }
615
616 if (outfd >= 0)
617 write(outfd, buf, len);
618 }
619#endif
620}
621
622
623/*
624 * While there's still a command in the work queue, parse and execute it.
625 */
Bram Moolenaarf2330482008-06-24 20:19:36 +0000626 void
627netbeans_parse_messages(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628{
629 char_u *p;
630 queue_T *node;
631
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200632 if (!NETBEANS_OPEN)
633 return;
634
Bram Moolenaarf2330482008-06-24 20:19:36 +0000635 while (head.next != NULL && head.next != &head)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 {
637 node = head.next;
638
639 /* Locate the first line in the first buffer. */
640 p = vim_strchr(node->buffer, '\n');
641 if (p == NULL)
642 {
643 /* Command isn't complete. If there is no following buffer,
644 * return (wait for more). If there is another buffer following,
645 * prepend the text to that buffer and delete this one. */
646 if (node->next == &head)
647 return;
Bram Moolenaarf2330482008-06-24 20:19:36 +0000648 p = alloc((unsigned)(STRLEN(node->buffer)
649 + STRLEN(node->next->buffer) + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 if (p == NULL)
651 return; /* out of memory */
652 STRCPY(p, node->buffer);
653 STRCAT(p, node->next->buffer);
654 vim_free(node->next->buffer);
655 node->next->buffer = p;
656
657 /* dispose of the node and buffer */
658 head.next = node->next;
659 node->next->prev = node->prev;
660 vim_free(node->buffer);
661 vim_free(node);
662 }
663 else
664 {
665 /* There is a complete command at the start of the buffer.
666 * Terminate it with a NUL. When no more text is following unlink
667 * the buffer. Do this before executing, because new buffers can
668 * be added while busy handling the command. */
669 *p++ = NUL;
670 if (*p == NUL)
671 {
672 head.next = node->next;
673 node->next->prev = node->prev;
674 }
675
676 /* now, parse and execute the commands */
677 nb_parse_cmd(node->buffer);
678
679 if (*p == NUL)
680 {
681 /* buffer finished, dispose of the node and buffer */
682 vim_free(node->buffer);
683 vim_free(node);
684 }
685 else
686 {
687 /* more follows, move to the start */
Bram Moolenaarf2330482008-06-24 20:19:36 +0000688 STRMOVE(node->buffer, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 }
690 }
691 }
692}
693
694/* Buffer size for reading incoming messages. */
695#define MAXMSGSIZE 4096
696
697/*
Bram Moolenaar67c53842010-05-22 18:28:27 +0200698 * Read a command from netbeans.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 */
Bram Moolenaar67c53842010-05-22 18:28:27 +0200700#ifdef FEAT_GUI_MOTIF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 static void
Bram Moolenaara9d45512009-05-17 21:25:42 +0000702messageFromNetbeans(XtPointer clientData UNUSED,
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000703 int *unused1 UNUSED,
704 XtInputId *unused2 UNUSED)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200705{
706 netbeans_read();
707}
708#endif
709
710#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000712messageFromNetbeans(gpointer clientData UNUSED,
713 gint unused1 UNUSED,
714 GdkInputCondition unused2 UNUSED)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200715{
716 netbeans_read();
717}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718#endif
Bram Moolenaar67c53842010-05-22 18:28:27 +0200719
720 void
721netbeans_read()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722{
723 static char_u *buf = NULL;
Bram Moolenaar0b65f892010-05-15 14:49:02 +0200724 int len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 int readlen = 0;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200726#if defined(NB_HAS_GUI) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_W32)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 static int level = 0;
Bram Moolenaarf2330482008-06-24 20:19:36 +0000728#endif
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100729#ifdef HAVE_SELECT
730 struct timeval tval;
731 fd_set rfds;
732#else
733# ifdef HAVE_POLL
734 struct pollfd fds;
735# endif
736#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200738 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 {
740 nbdebug(("messageFromNetbeans() called without a socket\n"));
741 return;
742 }
743
Bram Moolenaar67c53842010-05-22 18:28:27 +0200744#if defined(NB_HAS_GUI) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_W32)
745 /* recursion guard; this will be called from the X event loop at unknown
746 * moments */
747 if (NB_HAS_GUI)
748 ++level;
Bram Moolenaarf2330482008-06-24 20:19:36 +0000749#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750
751 /* Allocate a buffer to read into. */
752 if (buf == NULL)
753 {
754 buf = alloc(MAXMSGSIZE);
755 if (buf == NULL)
756 return; /* out of memory! */
757 }
758
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100759 /* Keep on reading for as long as there is something to read.
760 * Use select() or poll() to avoid blocking on a message that is exactly
761 * MAXMSGSIZE long. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 for (;;)
763 {
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100764#ifdef HAVE_SELECT
765 FD_ZERO(&rfds);
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200766 FD_SET(nbsock, &rfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +0200767 tval.tv_sec = 0;
768 tval.tv_usec = 0;
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200769 if (select(nbsock + 1, &rfds, NULL, NULL, &tval) <= 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200770 break;
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100771#else
772# ifdef HAVE_POLL
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200773 fds.fd = nbsock;
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100774 fds.events = POLLIN;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200775 if (poll(&fds, 1, 0) <= 0)
776 break;
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100777# endif
778#endif
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200779 len = sock_read(nbsock, buf, MAXMSGSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 if (len <= 0)
781 break; /* error or nothing more to read */
782
783 /* Store the read message in the queue. */
784 save(buf, len);
785 readlen += len;
786 if (len < MAXMSGSIZE)
787 break; /* did read everything that's available */
788 }
789
790 if (readlen <= 0)
791 {
792 /* read error or didn't read anything */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200793 netbeans_close();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 nbdebug(("messageFromNetbeans: Error in read() from socket\n"));
795 if (len < 0)
Bram Moolenaarf2330482008-06-24 20:19:36 +0000796 {
797 nbdebug(("read from Netbeans socket\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 PERROR(_("read from Netbeans socket"));
Bram Moolenaarf2330482008-06-24 20:19:36 +0000799 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000800 return; /* don't try to parse it */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 }
802
Bram Moolenaar67c53842010-05-22 18:28:27 +0200803#if defined(NB_HAS_GUI) && !defined(FEAT_GUI_W32)
Bram Moolenaar61665aa2008-12-24 11:20:53 +0000804 /* Let the main loop handle messages. */
Bram Moolenaar67c53842010-05-22 18:28:27 +0200805 if (NB_HAS_GUI)
806 {
Bram Moolenaar61665aa2008-12-24 11:20:53 +0000807# ifdef FEAT_GUI_GTK
Bram Moolenaar67c53842010-05-22 18:28:27 +0200808 if (gtk_main_level() > 0)
809 gtk_main_quit();
810# else
811 /* Parse the messages now, but avoid recursion. */
812 if (level == 1)
813 netbeans_parse_messages();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814
Bram Moolenaar67c53842010-05-22 18:28:27 +0200815 --level;
816# endif
817 }
Bram Moolenaarf2330482008-06-24 20:19:36 +0000818#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819}
820
821/*
822 * Handle one NUL terminated command.
823 *
824 * format of a command from netbeans:
825 *
826 * 6:setTitle!84 "a.c"
827 *
828 * bufno
829 * colon
830 * cmd
831 * !
832 * cmdno
833 * args
834 *
835 * for function calls, the ! is replaced by a /
836 */
837 static void
838nb_parse_cmd(char_u *cmd)
839{
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000840 char *verb;
841 char *q;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 int bufno;
843 int isfunc = -1;
844
845 if (STRCMP(cmd, "DISCONNECT") == 0)
846 {
847 /* We assume the server knows that we can safely exit! */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 /* Disconnect before exiting, Motif hangs in a Select error
849 * message otherwise. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200850 netbeans_close();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 getout(0);
852 /* NOTREACHED */
853 }
854
855 if (STRCMP(cmd, "DETACH") == 0)
856 {
857 /* The IDE is breaking the connection. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200858 netbeans_close();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 return;
860 }
861
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000862 bufno = strtol((char *)cmd, &verb, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863
864 if (*verb != ':')
865 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000866 nbdebug((" missing colon: %s\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 EMSG2("E627: missing colon: %s", cmd);
868 return;
869 }
870 ++verb; /* skip colon */
871
872 for (q = verb; *q; q++)
873 {
874 if (*q == '!')
875 {
876 *q++ = NUL;
877 isfunc = 0;
878 break;
879 }
880 else if (*q == '/')
881 {
882 *q++ = NUL;
883 isfunc = 1;
884 break;
885 }
886 }
887
888 if (isfunc < 0)
889 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000890 nbdebug((" missing ! or / in: %s\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 EMSG2("E628: missing ! or / in: %s", cmd);
892 return;
893 }
894
Bram Moolenaar89d40322006-08-29 15:30:07 +0000895 r_cmdno = strtol(q, &q, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000897 q = (char *)skipwhite((char_u *)q);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898
Bram Moolenaar89d40322006-08-29 15:30:07 +0000899 if (nb_do_cmd(bufno, (char_u *)verb, isfunc, r_cmdno, (char_u *)q) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 {
Bram Moolenaar009b2592004-10-24 19:18:58 +0000901#ifdef NBDEBUG
902 /*
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100903 * This happens because the ExtEd can send a command or 2 after
Bram Moolenaar009b2592004-10-24 19:18:58 +0000904 * doing a stopDocumentListen command. It doesn't harm anything
905 * so I'm disabling it except for debugging.
906 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 nbdebug(("nb_parse_cmd: Command error for \"%s\"\n", cmd));
908 EMSG("E629: bad return from nb_do_cmd");
Bram Moolenaar009b2592004-10-24 19:18:58 +0000909#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 }
911}
912
913struct nbbuf_struct
914{
915 buf_T *bufp;
916 unsigned int fireChanges:1;
917 unsigned int initDone:1;
Bram Moolenaar009b2592004-10-24 19:18:58 +0000918 unsigned int insertDone:1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 unsigned int modified:1;
Bram Moolenaar009b2592004-10-24 19:18:58 +0000920 int nbbuf_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 char *displayname;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 int *signmap;
923 short_u signmaplen;
924 short_u signmapused;
925};
926
927typedef struct nbbuf_struct nbbuf_T;
928
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200929static nbbuf_T *buf_list = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000930static int buf_list_size = 0; /* size of buf_list */
931static int buf_list_used = 0; /* nr of entries in buf_list actually in use */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200933static char **globalsignmap = NULL;
934static int globalsignmaplen = 0;
935static int globalsignmapused = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936
937static int mapsigntype __ARGS((nbbuf_T *, int localsigntype));
938static void addsigntype __ARGS((nbbuf_T *, int localsigntype, char_u *typeName,
939 char_u *tooltip, char_u *glyphfile,
Bram Moolenaar67c53842010-05-22 18:28:27 +0200940 char_u *fg, char_u *bg));
Bram Moolenaar009b2592004-10-24 19:18:58 +0000941static void print_read_msg __ARGS((nbbuf_T *buf));
942static void print_save_msg __ARGS((nbbuf_T *buf, long nchars));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943
944static int curPCtype = -1;
945
946/*
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200947 * Free netbeans resources.
948 */
949 static void
950nb_free()
951{
952 keyQ_T *key_node = keyHead.next;
953 queue_T *cmd_node = head.next;
954 nbbuf_T buf;
955 buf_T *bufp;
956 int i;
957
958 /* free the netbeans buffer list */
959 for (i = 0; i < buf_list_used; i++)
960 {
961 buf = buf_list[i];
962 vim_free(buf.displayname);
963 vim_free(buf.signmap);
964 if ((bufp=buf.bufp) != NULL)
965 {
966 buf.bufp->b_netbeans_file = FALSE;
967 buf.bufp->b_was_netbeans_file = FALSE;
968 }
969 }
970 vim_free(buf_list);
971 buf_list = NULL;
972 buf_list_size = 0;
973 buf_list_used = 0;
974
975 /* free the queued key commands */
976 while(key_node != NULL && key_node != &keyHead)
977 {
978 keyQ_T *next = key_node->next;
979 vim_free(key_node->keystr);
980 vim_free(key_node);
981 if (next == &keyHead)
982 {
983 keyHead.next = &keyHead;
984 keyHead.prev = &keyHead;
985 break;
986 }
987 key_node = next;
988 }
989
990 /* free the queued netbeans commands */
991 while(cmd_node != NULL && cmd_node != &head)
992 {
993 queue_T *next = cmd_node->next;
994 vim_free(cmd_node->buffer);
995 vim_free(cmd_node);
996 if (next == &head)
997 {
998 head.next = &head;
999 head.prev = &head;
1000 break;
1001 }
1002 cmd_node = next;
1003 }
1004}
1005
1006/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 * Get the Netbeans buffer number for the specified buffer.
1008 */
1009 static int
1010nb_getbufno(buf_T *bufp)
1011{
1012 int i;
1013
1014 for (i = 0; i < buf_list_used; i++)
1015 if (buf_list[i].bufp == bufp)
1016 return i;
1017 return -1;
1018}
1019
1020/*
1021 * Is this a NetBeans-owned buffer?
1022 */
1023 int
1024isNetbeansBuffer(buf_T *bufp)
1025{
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001026 return NETBEANS_OPEN && bufp->b_netbeans_file;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027}
1028
1029/*
1030 * NetBeans and Vim have different undo models. In Vim, the file isn't
1031 * changed if changes are undone via the undo command. In NetBeans, once
1032 * a change has been made the file is marked as modified until saved. It
1033 * doesn't matter if the change was undone.
1034 *
1035 * So this function is for the corner case where Vim thinks a buffer is
1036 * unmodified but NetBeans thinks it IS modified.
1037 */
1038 int
1039isNetbeansModified(buf_T *bufp)
1040{
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001041 if (isNetbeansBuffer(bufp))
Bram Moolenaar009b2592004-10-24 19:18:58 +00001042 {
1043 int bufno = nb_getbufno(bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044
Bram Moolenaar009b2592004-10-24 19:18:58 +00001045 if (bufno > 0)
1046 return buf_list[bufno].modified;
1047 else
1048 return FALSE;
1049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 else
1051 return FALSE;
1052}
1053
1054/*
1055 * Given a Netbeans buffer number, return the netbeans buffer.
1056 * Returns NULL for 0 or a negative number. A 0 bufno means a
1057 * non-buffer related command has been sent.
1058 */
1059 static nbbuf_T *
1060nb_get_buf(int bufno)
1061{
1062 /* find or create a buffer with the given number */
1063 int incr;
1064
1065 if (bufno <= 0)
1066 return NULL;
1067
1068 if (!buf_list)
1069 {
1070 /* initialize */
1071 buf_list = (nbbuf_T *)alloc_clear(100 * sizeof(nbbuf_T));
1072 buf_list_size = 100;
1073 }
1074 if (bufno >= buf_list_used) /* new */
1075 {
1076 if (bufno >= buf_list_size) /* grow list */
1077 {
1078 incr = bufno - buf_list_size + 90;
1079 buf_list_size += incr;
1080 buf_list = (nbbuf_T *)vim_realloc(
1081 buf_list, buf_list_size * sizeof(nbbuf_T));
1082 memset(buf_list + buf_list_size - incr, 0, incr * sizeof(nbbuf_T));
1083 }
1084
1085 while (buf_list_used <= bufno)
1086 {
1087 /* Default is to fire text changes. */
1088 buf_list[buf_list_used].fireChanges = 1;
1089 ++buf_list_used;
1090 }
1091 }
1092
1093 return buf_list + bufno;
1094}
1095
1096/*
1097 * Return the number of buffers that are modified.
1098 */
1099 static int
1100count_changed_buffers(void)
1101{
1102 buf_T *bufp;
1103 int n;
1104
1105 n = 0;
1106 for (bufp = firstbuf; bufp != NULL; bufp = bufp->b_next)
1107 if (bufp->b_changed)
1108 ++n;
1109 return n;
1110}
1111
1112/*
1113 * End the netbeans session.
1114 */
1115 void
1116netbeans_end(void)
1117{
1118 int i;
1119 static char buf[128];
1120
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001121 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 return;
1123
1124 for (i = 0; i < buf_list_used; i++)
1125 {
1126 if (!buf_list[i].bufp)
1127 continue;
1128 if (netbeansForcedQuit)
1129 {
1130 /* mark as unmodified so NetBeans won't put up dialog on "killed" */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001131 sprintf(buf, "%d:unmodified=%d\n", i, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 nbdebug(("EVT: %s", buf));
1133 nb_send(buf, "netbeans_end");
1134 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001135 sprintf(buf, "%d:killed=%d\n", i, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 nbdebug(("EVT: %s", buf));
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001137 /* nb_send(buf, "netbeans_end"); avoid "write failed" messages */
1138 ignored = sock_write(nbsock, buf, (int)STRLEN(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140}
1141
1142/*
1143 * Send a message to netbeans.
1144 */
1145 static void
1146nb_send(char *buf, char *fun)
1147{
1148 /* Avoid giving pages full of error messages when the other side has
1149 * exited, only mention the first error until the connection works again. */
1150 static int did_error = FALSE;
1151
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001152 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 {
1154 if (!did_error)
Bram Moolenaarf2330482008-06-24 20:19:36 +00001155 {
1156 nbdebug((" %s(): write while not connected\n", fun));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 EMSG2("E630: %s(): write while not connected", fun);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 did_error = TRUE;
1160 }
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001161 else if (sock_write(nbsock, buf, (int)STRLEN(buf)) != (int)STRLEN(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 {
1163 if (!did_error)
Bram Moolenaarf2330482008-06-24 20:19:36 +00001164 {
1165 nbdebug((" %s(): write failed\n", fun));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 EMSG2("E631: %s(): write failed", fun);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 did_error = TRUE;
1169 }
1170 else
1171 did_error = FALSE;
1172}
1173
1174/*
1175 * Some input received from netbeans requires a response. This function
1176 * handles a response with no information (except the command number).
1177 */
1178 static void
1179nb_reply_nil(int cmdno)
1180{
1181 char reply[32];
1182
Bram Moolenaar009b2592004-10-24 19:18:58 +00001183 nbdebug(("REP %d: <none>\n", cmdno));
1184
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 sprintf(reply, "%d\n", cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 nb_send(reply, "nb_reply_nil");
1187}
1188
1189
1190/*
1191 * Send a response with text.
1192 * "result" must have been quoted already (using nb_quote()).
1193 */
1194 static void
1195nb_reply_text(int cmdno, char_u *result)
1196{
1197 char_u *reply;
1198
Bram Moolenaar009b2592004-10-24 19:18:58 +00001199 nbdebug(("REP %d: %s\n", cmdno, (char *)result));
1200
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001201 reply = alloc((unsigned)STRLEN(result) + 32);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 sprintf((char *)reply, "%d %s\n", cmdno, (char *)result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 nb_send((char *)reply, "nb_reply_text");
1204
1205 vim_free(reply);
1206}
1207
1208
1209/*
1210 * Send a response with a number result code.
1211 */
1212 static void
1213nb_reply_nr(int cmdno, long result)
1214{
1215 char reply[32];
1216
Bram Moolenaar009b2592004-10-24 19:18:58 +00001217 nbdebug(("REP %d: %ld\n", cmdno, result));
1218
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 sprintf(reply, "%d %ld\n", cmdno, result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 nb_send(reply, "nb_reply_nr");
1221}
1222
1223
1224/*
1225 * Encode newline, ret, backslash, double quote for transmission to NetBeans.
1226 */
1227 static char_u *
1228nb_quote(char_u *txt)
1229{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001230 char_u *buf = alloc((unsigned)(2 * STRLEN(txt) + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 char_u *p = txt;
1232 char_u *q = buf;
1233
1234 if (buf == NULL)
1235 return NULL;
1236 for (; *p; p++)
1237 {
1238 switch (*p)
1239 {
1240 case '\"':
1241 case '\\':
1242 *q++ = '\\'; *q++ = *p; break;
1243 /* case '\t': */
1244 /* *q++ = '\\'; *q++ = 't'; break; */
1245 case '\n':
1246 *q++ = '\\'; *q++ = 'n'; break;
1247 case '\r':
1248 *q++ = '\\'; *q++ = 'r'; break;
1249 default:
1250 *q++ = *p;
1251 break;
1252 }
1253 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01001254 *q = '\0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255
1256 return buf;
1257}
1258
1259
1260/*
1261 * Remove top level double quotes; convert backslashed chars.
1262 * Returns an allocated string (NULL for failure).
1263 * If "endp" is not NULL it is set to the character after the terminating
1264 * quote.
1265 */
1266 static char *
1267nb_unquote(char_u *p, char_u **endp)
1268{
1269 char *result = 0;
1270 char *q;
1271 int done = 0;
1272
1273 /* result is never longer than input */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001274 result = (char *)alloc_clear((unsigned)STRLEN(p) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 if (result == NULL)
1276 return NULL;
1277
1278 if (*p++ != '"')
1279 {
1280 nbdebug(("nb_unquote called with string that doesn't start with a quote!: %s\n",
1281 p));
1282 result[0] = NUL;
1283 return result;
1284 }
1285
1286 for (q = result; !done && *p != NUL;)
1287 {
1288 switch (*p)
1289 {
1290 case '"':
1291 /*
1292 * Unbackslashed dquote marks the end, if first char was dquote.
1293 */
1294 done = 1;
1295 break;
1296
1297 case '\\':
1298 ++p;
1299 switch (*p)
1300 {
1301 case '\\': *q++ = '\\'; break;
1302 case 'n': *q++ = '\n'; break;
1303 case 't': *q++ = '\t'; break;
1304 case 'r': *q++ = '\r'; break;
1305 case '"': *q++ = '"'; break;
1306 case NUL: --p; break;
1307 /* default: skip over illegal chars */
1308 }
1309 ++p;
1310 break;
1311
1312 default:
1313 *q++ = *p++;
1314 }
1315 }
1316
1317 if (endp != NULL)
1318 *endp = p;
1319
1320 return result;
1321}
1322
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001323/*
1324 * Remove from "first" byte to "last" byte (inclusive), at line "lnum" of the
1325 * current buffer. Remove to end of line when "last" is MAXCOL.
1326 */
1327 static void
1328nb_partialremove(linenr_T lnum, colnr_T first, colnr_T last)
1329{
1330 char_u *oldtext, *newtext;
1331 int oldlen;
1332 int lastbyte = last;
1333
1334 oldtext = ml_get(lnum);
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001335 oldlen = (int)STRLEN(oldtext);
Bram Moolenaarb3c70982008-01-18 10:40:55 +00001336 if (first >= (colnr_T)oldlen || oldlen == 0) /* just in case */
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001337 return;
1338 if (lastbyte >= oldlen)
1339 lastbyte = oldlen - 1;
1340 newtext = alloc(oldlen - (int)(lastbyte - first));
1341 if (newtext != NULL)
1342 {
1343 mch_memmove(newtext, oldtext, first);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001344 STRMOVE(newtext + first, oldtext + lastbyte + 1);
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001345 nbdebug((" NEW LINE %d: %s\n", lnum, newtext));
1346 ml_replace(lnum, newtext, FALSE);
1347 }
1348}
1349
1350/*
1351 * Replace the "first" line with the concatenation of the "first" and
1352 * the "other" line. The "other" line is not removed.
1353 */
1354 static void
1355nb_joinlines(linenr_T first, linenr_T other)
1356{
1357 int len_first, len_other;
1358 char_u *p;
1359
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001360 len_first = (int)STRLEN(ml_get(first));
1361 len_other = (int)STRLEN(ml_get(other));
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001362 p = alloc((unsigned)(len_first + len_other + 1));
1363 if (p != NULL)
1364 {
1365 mch_memmove(p, ml_get(first), len_first);
1366 mch_memmove(p + len_first, ml_get(other), len_other + 1);
1367 ml_replace(first, p, FALSE);
1368 }
1369}
1370
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371#define SKIP_STOP 2
1372#define streq(a,b) (strcmp(a,b) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373
1374/*
1375 * Do the actual processing of a single netbeans command or function.
Bram Moolenaar143c38c2007-05-10 16:41:10 +00001376 * The difference between a command and function is that a function
1377 * gets a response (it's required) but a command does not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 * For arguments see comment for nb_parse_cmd().
1379 */
1380 static int
1381nb_do_cmd(
1382 int bufno,
1383 char_u *cmd,
1384 int func,
1385 int cmdno,
1386 char_u *args) /* points to space before arguments or NUL */
1387{
1388 int doupdate = 0;
1389 long off = 0;
1390 nbbuf_T *buf = nb_get_buf(bufno);
1391 static int skip = 0;
1392 int retval = OK;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001393 char *cp; /* for when a char pointer is needed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394
1395 nbdebug(("%s %d: (%d) %s %s\n", (func) ? "FUN" : "CMD", cmdno, bufno, cmd,
1396 STRCMP(cmd, "insert") == 0 ? "<text>" : (char *)args));
1397
1398 if (func)
1399 {
1400/* =====================================================================*/
1401 if (streq((char *)cmd, "getModified"))
1402 {
1403 if (buf == NULL || buf->bufp == NULL)
1404 /* Return the number of buffers that are modified. */
1405 nb_reply_nr(cmdno, (long)count_changed_buffers());
1406 else
1407 /* Return whether the buffer is modified. */
1408 nb_reply_nr(cmdno, (long)(buf->bufp->b_changed
1409 || isNetbeansModified(buf->bufp)));
1410/* =====================================================================*/
1411 }
1412 else if (streq((char *)cmd, "saveAndExit"))
1413 {
1414 /* Note: this will exit Vim if successful. */
1415 coloncmd(":confirm qall");
1416
1417 /* We didn't exit: return the number of changed buffers. */
1418 nb_reply_nr(cmdno, (long)count_changed_buffers());
1419/* =====================================================================*/
1420 }
1421 else if (streq((char *)cmd, "getCursor"))
1422 {
1423 char_u text[200];
1424
1425 /* Note: nb_getbufno() may return -1. This indicates the IDE
1426 * didn't assign a number to the current buffer in response to a
1427 * fileOpened event. */
1428 sprintf((char *)text, "%d %ld %d %ld",
1429 nb_getbufno(curbuf),
1430 (long)curwin->w_cursor.lnum,
1431 (int)curwin->w_cursor.col,
1432 pos2off(curbuf, &curwin->w_cursor));
1433 nb_reply_text(cmdno, text);
1434/* =====================================================================*/
1435 }
Bram Moolenaarc65c4912006-11-14 17:29:46 +00001436 else if (streq((char *)cmd, "getAnno"))
1437 {
1438 long linenum = 0;
1439#ifdef FEAT_SIGNS
1440 if (buf == NULL || buf->bufp == NULL)
1441 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001442 nbdebug((" Invalid buffer identifier in getAnno\n"));
1443 EMSG("E652: Invalid buffer identifier in getAnno");
Bram Moolenaarc65c4912006-11-14 17:29:46 +00001444 retval = FAIL;
1445 }
1446 else
1447 {
1448 int serNum;
1449
1450 cp = (char *)args;
1451 serNum = strtol(cp, &cp, 10);
1452 /* If the sign isn't found linenum will be zero. */
1453 linenum = (long)buf_findsign(buf->bufp, serNum);
1454 }
1455#endif
1456 nb_reply_nr(cmdno, linenum);
1457/* =====================================================================*/
1458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 else if (streq((char *)cmd, "getLength"))
1460 {
1461 long len = 0;
1462
1463 if (buf == NULL || buf->bufp == NULL)
1464 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001465 nbdebug((" invalid buffer identifier in getLength\n"));
1466 EMSG("E632: invalid buffer identifier in getLength");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 retval = FAIL;
1468 }
1469 else
1470 {
1471 len = get_buf_size(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 }
1473 nb_reply_nr(cmdno, len);
1474/* =====================================================================*/
1475 }
1476 else if (streq((char *)cmd, "getText"))
1477 {
1478 long len;
1479 linenr_T nlines;
1480 char_u *text = NULL;
1481 linenr_T lno = 1;
1482 char_u *p;
1483 char_u *line;
1484
1485 if (buf == NULL || buf->bufp == NULL)
1486 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001487 nbdebug((" invalid buffer identifier in getText\n"));
1488 EMSG("E633: invalid buffer identifier in getText");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 retval = FAIL;
1490 }
1491 else
1492 {
1493 len = get_buf_size(buf->bufp);
1494 nlines = buf->bufp->b_ml.ml_line_count;
1495 text = alloc((unsigned)((len > 0)
1496 ? ((len + nlines) * 2) : 4));
1497 if (text == NULL)
1498 {
1499 nbdebug((" nb_do_cmd: getText has null text field\n"));
1500 retval = FAIL;
1501 }
1502 else
1503 {
1504 p = text;
1505 *p++ = '\"';
1506 for (; lno <= nlines ; lno++)
1507 {
1508 line = nb_quote(ml_get_buf(buf->bufp, lno, FALSE));
1509 if (line != NULL)
1510 {
1511 STRCPY(p, line);
1512 p += STRLEN(line);
1513 *p++ = '\\';
1514 *p++ = 'n';
Bram Moolenaar009b2592004-10-24 19:18:58 +00001515 vim_free(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 }
1518 *p++ = '\"';
1519 *p = '\0';
1520 }
1521 }
1522 if (text == NULL)
1523 nb_reply_text(cmdno, (char_u *)"");
1524 else
1525 {
1526 nb_reply_text(cmdno, text);
1527 vim_free(text);
1528 }
1529/* =====================================================================*/
1530 }
1531 else if (streq((char *)cmd, "remove"))
1532 {
1533 long count;
1534 pos_T first, last;
1535 pos_T *pos;
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001536 pos_T *next;
1537 linenr_T del_from_lnum, del_to_lnum; /* lines to be deleted as a whole */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 int oldFire = netbeansFireChanges;
1539 int oldSuppress = netbeansSuppressNoLines;
1540 int wasChanged;
1541
1542 if (skip >= SKIP_STOP)
1543 {
1544 nbdebug((" Skipping %s command\n", (char *) cmd));
1545 nb_reply_nil(cmdno);
1546 return OK;
1547 }
1548
1549 if (buf == NULL || buf->bufp == NULL)
1550 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001551 nbdebug((" invalid buffer identifier in remove\n"));
1552 EMSG("E634: invalid buffer identifier in remove");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 retval = FAIL;
1554 }
1555 else
1556 {
1557 netbeansFireChanges = FALSE;
1558 netbeansSuppressNoLines = TRUE;
1559
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001560 nb_set_curbuf(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 wasChanged = buf->bufp->b_changed;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001562 cp = (char *)args;
1563 off = strtol(cp, &cp, 10);
1564 count = strtol(cp, &cp, 10);
1565 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 /* delete "count" chars, starting at "off" */
1567 pos = off2pos(buf->bufp, off);
1568 if (!pos)
1569 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001570 nbdebug((" !bad position\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 nb_reply_text(cmdno, (char_u *)"!bad position");
1572 netbeansFireChanges = oldFire;
1573 netbeansSuppressNoLines = oldSuppress;
1574 return FAIL;
1575 }
1576 first = *pos;
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001577 nbdebug((" FIRST POS: line %d, col %d\n",
1578 first.lnum, first.col));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 pos = off2pos(buf->bufp, off+count-1);
1580 if (!pos)
1581 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001582 nbdebug((" !bad count\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 nb_reply_text(cmdno, (char_u *)"!bad count");
1584 netbeansFireChanges = oldFire;
1585 netbeansSuppressNoLines = oldSuppress;
1586 return FAIL;
1587 }
1588 last = *pos;
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001589 nbdebug((" LAST POS: line %d, col %d\n",
1590 last.lnum, last.col));
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001591 del_from_lnum = first.lnum;
1592 del_to_lnum = last.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 doupdate = 1;
1594
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001595 /* Get the position of the first byte after the deleted
1596 * section. "next" is NULL when deleting to the end of the
1597 * file. */
1598 next = off2pos(buf->bufp, off + count);
1599
1600 /* Remove part of the first line. */
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001601 if (first.col != 0
1602 || (next != NULL && first.lnum == next->lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 {
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001604 if (first.lnum != last.lnum
1605 || (next != NULL && first.lnum != next->lnum))
1606 {
1607 /* remove to the end of the first line */
1608 nb_partialremove(first.lnum, first.col,
1609 (colnr_T)MAXCOL);
1610 if (first.lnum == last.lnum)
1611 {
1612 /* Partial line to remove includes the end of
1613 * line. Join the line with the next one, have
1614 * the next line deleted below. */
1615 nb_joinlines(first.lnum, next->lnum);
1616 del_to_lnum = next->lnum;
1617 }
1618 }
1619 else
1620 {
1621 /* remove within one line */
1622 nb_partialremove(first.lnum, first.col, last.col);
1623 }
1624 ++del_from_lnum; /* don't delete the first line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 }
1626
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001627 /* Remove part of the last line. */
1628 if (first.lnum != last.lnum && next != NULL
1629 && next->col != 0 && last.lnum == next->lnum)
1630 {
1631 nb_partialremove(last.lnum, 0, last.col);
1632 if (del_from_lnum > first.lnum)
1633 {
1634 /* Join end of last line to start of first line; last
1635 * line is deleted below. */
1636 nb_joinlines(first.lnum, last.lnum);
1637 }
1638 else
1639 /* First line is deleted as a whole, keep the last
1640 * line. */
1641 --del_to_lnum;
1642 }
1643
1644 /* First is partial line; last line to remove includes
1645 * the end of line; join first line to line following last
1646 * line; line following last line is deleted below. */
1647 if (first.lnum != last.lnum && del_from_lnum > first.lnum
1648 && next != NULL && last.lnum != next->lnum)
1649 {
1650 nb_joinlines(first.lnum, next->lnum);
1651 del_to_lnum = next->lnum;
1652 }
1653
1654 /* Delete whole lines if there are any. */
1655 if (del_to_lnum >= del_from_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 {
1657 int i;
1658
1659 /* delete signs from the lines being deleted */
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001660 for (i = del_from_lnum; i <= del_to_lnum; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 {
1662 int id = buf_findsign_id(buf->bufp, (linenr_T)i);
1663 if (id > 0)
1664 {
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001665 nbdebug((" Deleting sign %d on line %d\n",
1666 id, i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 buf_delsign(buf->bufp, id);
1668 }
1669 else
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001670 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 nbdebug((" No sign on line %d\n", i));
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 }
1674
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001675 nbdebug((" Deleting lines %d through %d\n",
1676 del_from_lnum, del_to_lnum));
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001677 curwin->w_cursor.lnum = del_from_lnum;
1678 curwin->w_cursor.col = 0;
1679 del_lines(del_to_lnum - del_from_lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 }
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001681
1682 /* Leave cursor at first deleted byte. */
1683 curwin->w_cursor = first;
1684 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 buf->bufp->b_changed = wasChanged; /* logically unchanged */
1686 netbeansFireChanges = oldFire;
1687 netbeansSuppressNoLines = oldSuppress;
1688
1689 u_blockfree(buf->bufp);
1690 u_clearall(buf->bufp);
1691 }
1692 nb_reply_nil(cmdno);
1693/* =====================================================================*/
1694 }
1695 else if (streq((char *)cmd, "insert"))
1696 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 char_u *to_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698
1699 if (skip >= SKIP_STOP)
1700 {
1701 nbdebug((" Skipping %s command\n", (char *) cmd));
1702 nb_reply_nil(cmdno);
1703 return OK;
1704 }
1705
1706 /* get offset */
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001707 cp = (char *)args;
1708 off = strtol(cp, &cp, 10);
1709 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710
1711 /* get text to be inserted */
1712 args = skipwhite(args);
1713 args = to_free = (char_u *)nb_unquote(args, NULL);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001714 /*
1715 nbdebug((" CHUNK[%d]: %d bytes at offset %d\n",
1716 buf->bufp->b_ml.ml_line_count, STRLEN(args), off));
1717 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718
1719 if (buf == NULL || buf->bufp == NULL)
1720 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001721 nbdebug((" invalid buffer identifier in insert\n"));
1722 EMSG("E635: invalid buffer identifier in insert");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 retval = FAIL;
1724 }
1725 else if (args != NULL)
1726 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001727 int ff_detected = EOL_UNKNOWN;
1728 int buf_was_empty = (buf->bufp->b_ml.ml_flags & ML_EMPTY);
1729 size_t len = 0;
1730 int added = 0;
1731 int oldFire = netbeansFireChanges;
1732 int old_b_changed;
1733 char_u *nl;
1734 linenr_T lnum;
1735 linenr_T lnum_start;
1736 pos_T *pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 netbeansFireChanges = 0;
1739
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001740 /* Jump to the buffer where we insert. After this "curbuf"
1741 * can be used. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001742 nb_set_curbuf(buf->bufp);
Bram Moolenaara3227e22006-03-08 21:32:40 +00001743 old_b_changed = curbuf->b_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001745 /* Convert the specified character offset into a lnum/col
1746 * position. */
Bram Moolenaara3227e22006-03-08 21:32:40 +00001747 pos = off2pos(curbuf, off);
1748 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001750 if (pos->lnum <= 0)
1751 lnum_start = 1;
1752 else
1753 lnum_start = pos->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 }
1755 else
1756 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001757 /* If the given position is not found, assume we want
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 * the end of the file. See setLocAndSize HACK. */
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001759 if (buf_was_empty)
1760 lnum_start = 1; /* above empty line */
1761 else
1762 lnum_start = curbuf->b_ml.ml_line_count + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001763 }
1764
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001765 /* "lnum" is the line where we insert: either append to it or
1766 * insert a new line above it. */
1767 lnum = lnum_start;
1768
1769 /* Loop over the "\n" separated lines of the argument. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 doupdate = 1;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001771 while (*args != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001773 nl = vim_strchr(args, '\n');
1774 if (nl == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001776 /* Incomplete line, probably truncated. Next "insert"
1777 * command should append to this one. */
1778 len = STRLEN(args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001780 else
1781 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001782 len = nl - args;
1783
1784 /*
1785 * We need to detect EOL style, because the commands
1786 * use a character offset.
1787 */
1788 if (nl > args && nl[-1] == '\r')
1789 {
1790 ff_detected = EOL_DOS;
1791 --len;
1792 }
1793 else
1794 ff_detected = EOL_UNIX;
1795 }
1796 args[len] = NUL;
1797
1798 if (lnum == lnum_start
1799 && ((pos != NULL && pos->col > 0)
1800 || (lnum == 1 && buf_was_empty)))
1801 {
1802 char_u *oldline = ml_get(lnum);
1803 char_u *newline;
1804
1805 /* Insert halfway a line. For simplicity we assume we
1806 * need to append to the line. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001807 newline = alloc_check((unsigned)(STRLEN(oldline) + len + 1));
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001808 if (newline != NULL)
1809 {
1810 STRCPY(newline, oldline);
1811 STRCAT(newline, args);
1812 ml_replace(lnum, newline, FALSE);
1813 }
1814 }
1815 else
1816 {
1817 /* Append a new line. Not that we always do this,
1818 * also when the text doesn't end in a "\n". */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001819 ml_append((linenr_T)(lnum - 1), args, (colnr_T)(len + 1), FALSE);
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001820 ++added;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001821 }
1822
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001823 if (nl == NULL)
1824 break;
1825 ++lnum;
1826 args = nl + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001827 }
1828
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001829 /* Adjust the marks below the inserted lines. */
1830 appended_lines_mark(lnum_start - 1, (long)added);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001832 /*
1833 * When starting with an empty buffer set the fileformat.
1834 * This is just guessing...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 */
1836 if (buf_was_empty)
1837 {
1838 if (ff_detected == EOL_UNKNOWN)
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001839#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 ff_detected = EOL_DOS;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001841#else
1842 ff_detected = EOL_UNIX;
1843#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 set_fileformat(ff_detected, OPT_LOCAL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00001845 curbuf->b_start_ffc = *curbuf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 }
1847
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 /*
1849 * XXX - GRP - Is the next line right? If I've inserted
1850 * text the buffer has been updated but not written. Will
1851 * netbeans guarantee to write it? Even if I do a :q! ?
1852 */
Bram Moolenaara3227e22006-03-08 21:32:40 +00001853 curbuf->b_changed = old_b_changed; /* logically unchanged */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 netbeansFireChanges = oldFire;
1855
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001856 /* Undo info is invalid now... */
Bram Moolenaara3227e22006-03-08 21:32:40 +00001857 u_blockfree(curbuf);
1858 u_clearall(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 }
1860 vim_free(to_free);
1861 nb_reply_nil(cmdno); /* or !error */
1862 }
1863 else
1864 {
1865 nbdebug(("UNIMPLEMENTED FUNCTION: %s\n", cmd));
1866 nb_reply_nil(cmdno);
1867 retval = FAIL;
1868 }
1869 }
1870 else /* Not a function; no reply required. */
1871 {
1872/* =====================================================================*/
1873 if (streq((char *)cmd, "create"))
1874 {
1875 /* Create a buffer without a name. */
1876 if (buf == NULL)
1877 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001878 nbdebug((" invalid buffer identifier in create\n"));
1879 EMSG("E636: invalid buffer identifier in create");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 return FAIL;
1881 }
1882 vim_free(buf->displayname);
1883 buf->displayname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884
1885 netbeansReadFile = 0; /* don't try to open disk file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001886 do_ecmd(0, NULL, 0, 0, ECMD_ONE, ECMD_HIDE + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 netbeansReadFile = 1;
1888 buf->bufp = curbuf;
1889 maketitle();
Bram Moolenaar009b2592004-10-24 19:18:58 +00001890 buf->insertDone = FALSE;
Bram Moolenaar67c53842010-05-22 18:28:27 +02001891#if defined(FEAT_MENU) && defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 gui_update_menus(0);
Bram Moolenaar67c53842010-05-22 18:28:27 +02001893#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894/* =====================================================================*/
1895 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001896 else if (streq((char *)cmd, "insertDone"))
1897 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001898 if (buf == NULL || buf->bufp == NULL)
1899 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001900 nbdebug((" invalid buffer identifier in insertDone\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001901 }
1902 else
1903 {
1904 buf->bufp->b_start_eol = *args == 'T';
1905 buf->insertDone = TRUE;
1906 args += 2;
1907 buf->bufp->b_p_ro = *args == 'T';
1908 print_read_msg(buf);
1909 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001910/* =====================================================================*/
1911 }
1912 else if (streq((char *)cmd, "saveDone"))
1913 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001914 long savedChars = atol((char *)args);
1915
1916 if (buf == NULL || buf->bufp == NULL)
1917 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001918 nbdebug((" invalid buffer identifier in saveDone\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001919 }
1920 else
1921 print_save_msg(buf, savedChars);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001922/* =====================================================================*/
1923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 else if (streq((char *)cmd, "startDocumentListen"))
1925 {
1926 if (buf == NULL)
1927 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001928 nbdebug((" invalid buffer identifier in startDocumentListen\n"));
1929 EMSG("E637: invalid buffer identifier in startDocumentListen");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 return FAIL;
1931 }
1932 buf->fireChanges = 1;
1933/* =====================================================================*/
1934 }
1935 else if (streq((char *)cmd, "stopDocumentListen"))
1936 {
1937 if (buf == NULL)
1938 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001939 nbdebug((" invalid buffer identifier in stopDocumentListen\n"));
1940 EMSG("E638: invalid buffer identifier in stopDocumentListen");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 return FAIL;
1942 }
1943 buf->fireChanges = 0;
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001944 if (buf->bufp != NULL && buf->bufp->b_was_netbeans_file)
Bram Moolenaar009b2592004-10-24 19:18:58 +00001945 {
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001946 if (!buf->bufp->b_netbeans_file)
Bram Moolenaarf2330482008-06-24 20:19:36 +00001947 {
1948 nbdebug(("E658: NetBeans connection lost for buffer %ld\n", buf->bufp->b_fnum));
Bram Moolenaar009b2592004-10-24 19:18:58 +00001949 EMSGN(_("E658: NetBeans connection lost for buffer %ld"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 buf->bufp->b_fnum);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001951 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001952 else
1953 {
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001954 /* NetBeans uses stopDocumentListen when it stops editing
1955 * a file. It then expects the buffer in Vim to
1956 * disappear. */
1957 do_bufdel(DOBUF_DEL, (char_u *)"", 1,
1958 buf->bufp->b_fnum, buf->bufp->b_fnum, TRUE);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001959 vim_memset(buf, 0, sizeof(nbbuf_T));
1960 }
1961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962/* =====================================================================*/
1963 }
1964 else if (streq((char *)cmd, "setTitle"))
1965 {
1966 if (buf == NULL)
1967 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001968 nbdebug((" invalid buffer identifier in setTitle\n"));
1969 EMSG("E639: invalid buffer identifier in setTitle");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 return FAIL;
1971 }
1972 vim_free(buf->displayname);
1973 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974/* =====================================================================*/
1975 }
1976 else if (streq((char *)cmd, "initDone"))
1977 {
1978 if (buf == NULL || buf->bufp == NULL)
1979 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001980 nbdebug((" invalid buffer identifier in initDone\n"));
1981 EMSG("E640: invalid buffer identifier in initDone");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 return FAIL;
1983 }
1984 doupdate = 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001985 buf->initDone = TRUE;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001986 nb_set_curbuf(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987#if defined(FEAT_AUTOCMD)
1988 apply_autocmds(EVENT_BUFREADPOST, 0, 0, FALSE, buf->bufp);
1989#endif
1990
1991 /* handle any postponed key commands */
1992 handle_key_queue();
1993/* =====================================================================*/
1994 }
1995 else if (streq((char *)cmd, "setBufferNumber")
1996 || streq((char *)cmd, "putBufferNumber"))
1997 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00001998 char_u *path;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 buf_T *bufp;
2000
2001 if (buf == NULL)
2002 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002003 nbdebug((" invalid buffer identifier in setBufferNumber\n"));
2004 EMSG("E641: invalid buffer identifier in setBufferNumber");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 return FAIL;
2006 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002007 path = (char_u *)nb_unquote(args, NULL);
2008 if (path == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 return FAIL;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002010 bufp = buflist_findname(path);
2011 vim_free(path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 if (bufp == NULL)
2013 {
Bram Moolenaarec906222009-02-21 21:14:00 +00002014 nbdebug((" File %s not found in setBufferNumber\n", args));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 EMSG2("E642: File %s not found in setBufferNumber", args);
2016 return FAIL;
2017 }
2018 buf->bufp = bufp;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002019 buf->nbbuf_number = bufp->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020
2021 /* "setBufferNumber" has the side effect of jumping to the buffer
2022 * (don't know why!). Don't do that for "putBufferNumber". */
2023 if (*cmd != 'p')
2024 coloncmd(":buffer %d", bufp->b_fnum);
2025 else
2026 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002027 buf->initDone = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028
2029 /* handle any postponed key commands */
2030 handle_key_queue();
2031 }
2032
2033#if 0 /* never used */
2034 buf->internalname = (char *)alloc_clear(8);
2035 sprintf(buf->internalname, "<%d>", bufno);
2036 buf->netbeansOwns = 0;
2037#endif
2038/* =====================================================================*/
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 Moolenaar071d4272004-06-13 20:20:40 +00002058 gui_update_menus(0);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002059#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060/* =====================================================================*/
2061 }
2062 else if (streq((char *)cmd, "editFile"))
2063 {
2064 if (buf == NULL)
2065 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002066 nbdebug((" invalid buffer identifier in editFile\n"));
2067 EMSG("E644: invalid buffer identifier in editFile");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 return FAIL;
2069 }
2070 /* Edit a file: like create + setFullName + read the file. */
2071 vim_free(buf->displayname);
2072 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 do_ecmd(0, (char_u *)buf->displayname, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002074 ECMD_HIDE + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 buf->bufp = curbuf;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002076 buf->initDone = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 doupdate = 1;
2078#if defined(FEAT_TITLE)
2079 maketitle();
2080#endif
Bram Moolenaar67c53842010-05-22 18:28:27 +02002081#if defined(FEAT_MENU) && defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 gui_update_menus(0);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002083#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084/* =====================================================================*/
2085 }
2086 else if (streq((char *)cmd, "setVisible"))
2087 {
2088 if (buf == NULL || buf->bufp == NULL)
2089 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002090 nbdebug((" invalid buffer identifier in setVisible\n"));
2091 /* This message was commented out, probably because it can
2092 * happen when shutting down. */
2093 if (p_verbose > 0)
2094 EMSG("E645: invalid buffer identifier in setVisible");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 return FAIL;
2096 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002097 if (streq((char *)args, "T") && buf->bufp != curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 {
2099 exarg_T exarg;
2100 exarg.cmd = (char_u *)"goto";
2101 exarg.forceit = FALSE;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002102 dosetvisible = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 goto_buffer(&exarg, DOBUF_FIRST, FORWARD, buf->bufp->b_fnum);
2104 doupdate = 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002105 dosetvisible = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106
Bram Moolenaar67c53842010-05-22 18:28:27 +02002107#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 /* Side effect!!!. */
2109 if (!gui.starting)
2110 gui_mch_set_foreground();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002111#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113/* =====================================================================*/
2114 }
2115 else if (streq((char *)cmd, "raise"))
2116 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002117#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 /* Bring gvim to the foreground. */
2119 if (!gui.starting)
2120 gui_mch_set_foreground();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002121#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122/* =====================================================================*/
2123 }
2124 else if (streq((char *)cmd, "setModified"))
2125 {
Bram Moolenaarff064e12008-06-09 13:10:45 +00002126 int prev_b_changed;
2127
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 if (buf == NULL || buf->bufp == NULL)
2129 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002130 nbdebug((" invalid buffer identifier in setModified\n"));
2131 /* This message was commented out, probably because it can
2132 * happen when shutting down. */
2133 if (p_verbose > 0)
2134 EMSG("E646: invalid buffer identifier in setModified");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 return FAIL;
2136 }
Bram Moolenaarff064e12008-06-09 13:10:45 +00002137 prev_b_changed = buf->bufp->b_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 if (streq((char *)args, "T"))
Bram Moolenaarff064e12008-06-09 13:10:45 +00002139 buf->bufp->b_changed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 else
2141 {
2142 struct stat st;
2143
2144 /* Assume NetBeans stored the file. Reset the timestamp to
2145 * avoid "file changed" warnings. */
2146 if (buf->bufp->b_ffname != NULL
2147 && mch_stat((char *)buf->bufp->b_ffname, &st) >= 0)
2148 buf_store_time(buf->bufp, &st, buf->bufp->b_ffname);
Bram Moolenaarff064e12008-06-09 13:10:45 +00002149 buf->bufp->b_changed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 }
2151 buf->modified = buf->bufp->b_changed;
Bram Moolenaarff064e12008-06-09 13:10:45 +00002152 if (prev_b_changed != buf->bufp->b_changed)
2153 {
2154#ifdef FEAT_WINDOWS
2155 check_status(buf->bufp);
2156 redraw_tabline = TRUE;
2157#endif
2158#ifdef FEAT_TITLE
2159 maketitle();
2160#endif
2161 update_screen(0);
2162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163/* =====================================================================*/
2164 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002165 else if (streq((char *)cmd, "setModtime"))
2166 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002167 if (buf == NULL || buf->bufp == NULL)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002168 nbdebug((" invalid buffer identifier in setModtime\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002169 else
2170 buf->bufp->b_mtime = atoi((char *)args);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002171/* =====================================================================*/
2172 }
2173 else if (streq((char *)cmd, "setReadOnly"))
2174 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002175 if (buf == NULL || buf->bufp == NULL)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002176 nbdebug((" invalid buffer identifier in setReadOnly\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002177 else if (streq((char *)args, "T"))
Bram Moolenaar009b2592004-10-24 19:18:58 +00002178 buf->bufp->b_p_ro = TRUE;
2179 else
2180 buf->bufp->b_p_ro = FALSE;
2181/* =====================================================================*/
2182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 else if (streq((char *)cmd, "setMark"))
2184 {
2185 /* not yet */
2186/* =====================================================================*/
2187 }
2188 else if (streq((char *)cmd, "showBalloon"))
2189 {
2190#if defined(FEAT_BEVAL)
2191 static char *text = NULL;
2192
2193 /*
2194 * Set up the Balloon Expression Evaluation area.
2195 * Ignore 'ballooneval' here.
2196 * The text pointer must remain valid for a while.
2197 */
2198 if (balloonEval != NULL)
2199 {
2200 vim_free(text);
2201 text = nb_unquote(args, NULL);
2202 if (text != NULL)
2203 gui_mch_post_balloon(balloonEval, (char_u *)text);
2204 }
2205#endif
2206/* =====================================================================*/
2207 }
2208 else if (streq((char *)cmd, "setDot"))
2209 {
2210 pos_T *pos;
2211#ifdef NBDEBUG
2212 char_u *s;
2213#endif
2214
2215 if (buf == NULL || buf->bufp == NULL)
2216 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002217 nbdebug((" invalid buffer identifier in setDot\n"));
2218 EMSG("E647: invalid buffer identifier in setDot");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 return FAIL;
2220 }
2221
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002222 nb_set_curbuf(buf->bufp);
2223
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224#ifdef FEAT_VISUAL
2225 /* Don't want Visual mode now. */
2226 if (VIsual_active)
2227 end_visual_mode();
2228#endif
2229#ifdef NBDEBUG
2230 s = args;
2231#endif
2232 pos = get_off_or_lnum(buf->bufp, &args);
2233 if (pos)
2234 {
2235 curwin->w_cursor = *pos;
2236 check_cursor();
2237#ifdef FEAT_FOLDING
2238 foldOpenCursor();
2239#endif
2240 }
2241 else
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002242 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 nbdebug((" BAD POSITION in setDot: %s\n", s));
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245
2246 /* gui_update_cursor(TRUE, FALSE); */
2247 /* update_curbuf(NOT_VALID); */
2248 update_topline(); /* scroll to show the line */
2249 update_screen(VALID);
2250 setcursor();
2251 out_flush();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002252#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 gui_update_cursor(TRUE, FALSE);
2254 gui_mch_flush();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002255#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 /* Quit a hit-return or more prompt. */
2257 if (State == HITRETURN || State == ASKMORE)
2258 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259#ifdef FEAT_GUI_GTK
2260 if (gtk_main_level() > 0)
2261 gtk_main_quit();
2262#endif
2263 }
2264/* =====================================================================*/
2265 }
2266 else if (streq((char *)cmd, "close"))
2267 {
2268#ifdef NBDEBUG
2269 char *name = "<NONE>";
2270#endif
2271
2272 if (buf == NULL)
2273 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002274 nbdebug((" invalid buffer identifier in close\n"));
2275 EMSG("E648: invalid buffer identifier in close");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 return FAIL;
2277 }
2278
2279#ifdef NBDEBUG
2280 if (buf->displayname != NULL)
2281 name = buf->displayname;
2282#endif
Bram Moolenaarf2330482008-06-24 20:19:36 +00002283 if (buf->bufp == NULL)
2284 {
2285 nbdebug((" invalid buffer identifier in close\n"));
2286 /* This message was commented out, probably because it can
2287 * happen when shutting down. */
2288 if (p_verbose > 0)
2289 EMSG("E649: invalid buffer identifier in close");
2290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 nbdebug((" CLOSE %d: %s\n", bufno, name));
Bram Moolenaar67c53842010-05-22 18:28:27 +02002292#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 need_mouse_correct = TRUE;
Bram Moolenaar67c53842010-05-22 18:28:27 +02002294#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 if (buf->bufp != NULL)
2296 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD,
2297 buf->bufp->b_fnum, TRUE);
Bram Moolenaar8d602722006-08-08 19:34:19 +00002298 buf->bufp = NULL;
2299 buf->initDone = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 doupdate = 1;
2301/* =====================================================================*/
2302 }
2303 else if (streq((char *)cmd, "setStyle")) /* obsolete... */
2304 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002305 nbdebug((" setStyle is obsolete!\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306/* =====================================================================*/
2307 }
2308 else if (streq((char *)cmd, "setExitDelay"))
2309 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002310 /* Only used in version 2.1. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311/* =====================================================================*/
2312 }
2313 else if (streq((char *)cmd, "defineAnnoType"))
2314 {
2315#ifdef FEAT_SIGNS
2316 int typeNum;
2317 char_u *typeName;
2318 char_u *tooltip;
2319 char_u *p;
2320 char_u *glyphFile;
Bram Moolenaar67c53842010-05-22 18:28:27 +02002321 int parse_error = FALSE;
2322 char_u *fg;
2323 char_u *bg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324
2325 if (buf == NULL)
2326 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002327 nbdebug((" invalid buffer identifier in defineAnnoType\n"));
2328 EMSG("E650: invalid buffer identifier in defineAnnoType");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 return FAIL;
2330 }
2331
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002332 cp = (char *)args;
2333 typeNum = strtol(cp, &cp, 10);
2334 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 args = skipwhite(args);
2336 typeName = (char_u *)nb_unquote(args, &args);
2337 args = skipwhite(args + 1);
2338 tooltip = (char_u *)nb_unquote(args, &args);
2339 args = skipwhite(args + 1);
2340
2341 p = (char_u *)nb_unquote(args, &args);
2342 glyphFile = vim_strsave_escaped(p, escape_chars);
2343 vim_free(p);
2344
2345 args = skipwhite(args + 1);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002346 p = skiptowhite(args);
2347 if (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002349 *p = NUL;
2350 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02002352 fg = vim_strsave(args);
2353 bg = vim_strsave(p);
2354 if (STRLEN(fg) > MAX_COLOR_LENGTH || STRLEN(bg) > MAX_COLOR_LENGTH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002356 EMSG("E532: highlighting color name too long in defineAnnoType");
2357 vim_free(typeName);
2358 parse_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02002360 else if (typeName != NULL && tooltip != NULL && glyphFile != NULL)
2361 addsigntype(buf, typeNum, typeName, tooltip, glyphFile, fg, bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 else
2363 vim_free(typeName);
2364
2365 /* don't free typeName; it's used directly in addsigntype() */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002366 vim_free(fg);
2367 vim_free(bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 vim_free(tooltip);
2369 vim_free(glyphFile);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002370 if (parse_error)
2371 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372
2373#endif
2374/* =====================================================================*/
2375 }
2376 else if (streq((char *)cmd, "addAnno"))
2377 {
2378#ifdef FEAT_SIGNS
2379 int serNum;
2380 int localTypeNum;
2381 int typeNum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 pos_T *pos;
2383
2384 if (buf == NULL || buf->bufp == NULL)
2385 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002386 nbdebug((" invalid buffer identifier in addAnno\n"));
2387 EMSG("E651: invalid buffer identifier in addAnno");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 return FAIL;
2389 }
2390
2391 doupdate = 1;
2392
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002393 cp = (char *)args;
2394 serNum = strtol(cp, &cp, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395
2396 /* Get the typenr specific for this buffer and convert it to
2397 * the global typenumber, as used for the sign name. */
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002398 localTypeNum = strtol(cp, &cp, 10);
2399 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 typeNum = mapsigntype(buf, localTypeNum);
2401
2402 pos = get_off_or_lnum(buf->bufp, &args);
2403
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002404 cp = (char *)args;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002405 ignored = (int)strtol(cp, &cp, 10);
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002406 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407# ifdef NBDEBUG
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002408 if (ignored != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002410 nbdebug((" partial line annotation -- Not Yet Implemented!\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 }
2412# endif
2413 if (serNum >= GUARDEDOFFSET)
2414 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002415 nbdebug((" too many annotations! ignoring...\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 return FAIL;
2417 }
2418 if (pos)
2419 {
Bram Moolenaarec906222009-02-21 21:14:00 +00002420 coloncmd(":sign place %d line=%ld name=%d buffer=%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 serNum, pos->lnum, typeNum, buf->bufp->b_fnum);
2422 if (typeNum == curPCtype)
2423 coloncmd(":sign jump %d buffer=%d", serNum,
2424 buf->bufp->b_fnum);
2425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426#endif
2427/* =====================================================================*/
2428 }
2429 else if (streq((char *)cmd, "removeAnno"))
2430 {
2431#ifdef FEAT_SIGNS
2432 int serNum;
2433
2434 if (buf == NULL || buf->bufp == NULL)
2435 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002436 nbdebug((" invalid buffer identifier in removeAnno\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 return FAIL;
2438 }
2439 doupdate = 1;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002440 cp = (char *)args;
2441 serNum = strtol(cp, &cp, 10);
2442 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 coloncmd(":sign unplace %d buffer=%d",
2444 serNum, buf->bufp->b_fnum);
2445 redraw_buf_later(buf->bufp, NOT_VALID);
2446#endif
2447/* =====================================================================*/
2448 }
2449 else if (streq((char *)cmd, "moveAnnoToFront"))
2450 {
2451#ifdef FEAT_SIGNS
Bram Moolenaarf2330482008-06-24 20:19:36 +00002452 nbdebug((" moveAnnoToFront: Not Yet Implemented!\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453#endif
2454/* =====================================================================*/
2455 }
2456 else if (streq((char *)cmd, "guard") || streq((char *)cmd, "unguard"))
2457 {
2458 int len;
2459 pos_T first;
2460 pos_T last;
2461 pos_T *pos;
2462 int un = (cmd[0] == 'u');
2463 static int guardId = GUARDEDOFFSET;
2464
2465 if (skip >= SKIP_STOP)
2466 {
2467 nbdebug((" Skipping %s command\n", (char *) cmd));
2468 return OK;
2469 }
2470
2471 nb_init_graphics();
2472
2473 if (buf == NULL || buf->bufp == NULL)
2474 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002475 nbdebug((" invalid buffer identifier in %s command\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 return FAIL;
2477 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002478 nb_set_curbuf(buf->bufp);
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002479 cp = (char *)args;
2480 off = strtol(cp, &cp, 10);
2481 len = strtol(cp, NULL, 10);
2482 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 pos = off2pos(buf->bufp, off);
2484 doupdate = 1;
2485 if (!pos)
2486 nbdebug((" no such start pos in %s, %ld\n", cmd, off));
2487 else
2488 {
2489 first = *pos;
2490 pos = off2pos(buf->bufp, off + len - 1);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002491 if (pos != NULL && pos->col == 0)
2492 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 /*
2494 * In Java Swing the offset is a position between 2
2495 * characters. If col == 0 then we really want the
2496 * previous line as the end.
2497 */
2498 pos = off2pos(buf->bufp, off + len - 2);
2499 }
2500 if (!pos)
2501 nbdebug((" no such end pos in %s, %ld\n",
2502 cmd, off + len - 1));
2503 else
2504 {
2505 long lnum;
2506 last = *pos;
2507 /* set highlight for region */
2508 nbdebug((" %sGUARD %ld,%d to %ld,%d\n", (un) ? "UN" : "",
2509 first.lnum, first.col,
2510 last.lnum, last.col));
2511#ifdef FEAT_SIGNS
2512 for (lnum = first.lnum; lnum <= last.lnum; lnum++)
2513 {
2514 if (un)
2515 {
2516 /* never used */
2517 }
2518 else
2519 {
2520 if (buf_findsigntype_id(buf->bufp, lnum,
2521 GUARDED) == 0)
2522 {
2523 coloncmd(
Bram Moolenaarec906222009-02-21 21:14:00 +00002524 ":sign place %d line=%ld name=%d buffer=%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 guardId++, lnum, GUARDED,
2526 buf->bufp->b_fnum);
2527 }
2528 }
2529 }
2530#endif
2531 redraw_buf_later(buf->bufp, NOT_VALID);
2532 }
2533 }
2534/* =====================================================================*/
2535 }
2536 else if (streq((char *)cmd, "startAtomic"))
2537 {
2538 inAtomic = 1;
2539/* =====================================================================*/
2540 }
2541 else if (streq((char *)cmd, "endAtomic"))
2542 {
2543 inAtomic = 0;
2544 if (needupdate)
2545 {
2546 doupdate = 1;
2547 needupdate = 0;
2548 }
2549/* =====================================================================*/
2550 }
2551 else if (streq((char *)cmd, "save"))
2552 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002553 /*
2554 * NOTE - This command is obsolete wrt NetBeans. Its left in
2555 * only for historical reasons.
2556 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 if (buf == NULL || buf->bufp == NULL)
2558 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002559 nbdebug((" invalid buffer identifier in %s command\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 return FAIL;
2561 }
2562
2563 /* the following is taken from ex_cmds.c (do_wqall function) */
2564 if (bufIsChanged(buf->bufp))
2565 {
2566 /* Only write if the buffer can be written. */
2567 if (p_write
2568 && !buf->bufp->b_p_ro
2569 && buf->bufp->b_ffname != NULL
2570#ifdef FEAT_QUICKFIX
2571 && !bt_dontwrite(buf->bufp)
2572#endif
2573 )
2574 {
2575 buf_write_all(buf->bufp, FALSE);
2576#ifdef FEAT_AUTOCMD
2577 /* an autocommand may have deleted the buffer */
2578 if (!buf_valid(buf->bufp))
2579 buf->bufp = NULL;
2580#endif
2581 }
2582 }
Bram Moolenaarf2330482008-06-24 20:19:36 +00002583 else
2584 {
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002585 nbdebug((" Buffer has no changes!\n"));
Bram Moolenaarf2330482008-06-24 20:19:36 +00002586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587/* =====================================================================*/
2588 }
2589 else if (streq((char *)cmd, "netbeansBuffer"))
2590 {
2591 if (buf == NULL || buf->bufp == NULL)
2592 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002593 nbdebug((" invalid buffer identifier in %s command\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 return FAIL;
2595 }
2596 if (*args == 'T')
2597 {
2598 buf->bufp->b_netbeans_file = TRUE;
2599 buf->bufp->b_was_netbeans_file = TRUE;
2600 }
2601 else
2602 buf->bufp->b_netbeans_file = FALSE;
2603/* =====================================================================*/
2604 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002605 else if (streq((char *)cmd, "specialKeys"))
2606 {
2607 special_keys(args);
2608/* =====================================================================*/
2609 }
2610 else if (streq((char *)cmd, "actionMenuItem"))
2611 {
2612 /* not used yet */
2613/* =====================================================================*/
2614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 else if (streq((char *)cmd, "version"))
2616 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002617 /* not used yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 }
Bram Moolenaarf2330482008-06-24 20:19:36 +00002619 else
2620 {
2621 nbdebug(("Unrecognised command: %s\n", cmd));
2622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 /*
2624 * Unrecognized command is ignored.
2625 */
2626 }
2627 if (inAtomic && doupdate)
2628 {
2629 needupdate = 1;
2630 doupdate = 0;
2631 }
2632
Bram Moolenaar009b2592004-10-24 19:18:58 +00002633 /*
2634 * Is this needed? I moved the netbeans_Xt_connect() later during startup
2635 * and it may no longer be necessary. If its not needed then needupdate
2636 * and doupdate can also be removed.
2637 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 if (buf != NULL && buf->initDone && doupdate)
2639 {
2640 update_screen(NOT_VALID);
2641 setcursor();
2642 out_flush();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002643#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 gui_update_cursor(TRUE, FALSE);
2645 gui_mch_flush();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002646#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 /* Quit a hit-return or more prompt. */
2648 if (State == HITRETURN || State == ASKMORE)
2649 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650#ifdef FEAT_GUI_GTK
2651 if (gtk_main_level() > 0)
2652 gtk_main_quit();
2653#endif
2654 }
2655 }
2656
2657 return retval;
2658}
2659
2660
2661/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002662 * If "buf" is not the current buffer try changing to a window that edits this
2663 * buffer. If there is no such window then close the current buffer and set
2664 * the current buffer as "buf".
2665 */
2666 static void
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00002667nb_set_curbuf(buf_T *buf)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002668{
2669 if (curbuf != buf && buf_jump_open_win(buf) == NULL)
2670 set_curbuf(buf, DOBUF_GOTO);
2671}
2672
2673/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 * Process a vim colon command.
2675 */
2676 static void
2677coloncmd(char *cmd, ...)
2678{
2679 char buf[1024];
2680 va_list ap;
2681
2682 va_start(ap, cmd);
Bram Moolenaar0dc79e82009-06-24 14:50:12 +00002683 vim_vsnprintf(buf, sizeof(buf), cmd, ap, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 va_end(ap);
2685
2686 nbdebug((" COLONCMD %s\n", buf));
2687
2688/* ALT_INPUT_LOCK_ON; */
2689 do_cmdline((char_u *)buf, NULL, NULL, DOCMD_NOWAIT | DOCMD_KEYTYPED);
2690/* ALT_INPUT_LOCK_OFF; */
2691
2692 setcursor(); /* restore the cursor position */
2693 out_flush(); /* make sure output has been written */
2694
Bram Moolenaar67c53842010-05-22 18:28:27 +02002695#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 gui_update_cursor(TRUE, FALSE);
2697 gui_mch_flush();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002698#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699}
2700
2701
2702/*
Bram Moolenaar009b2592004-10-24 19:18:58 +00002703 * Parse the specialKeys argument and issue the appropriate map commands.
2704 */
2705 static void
2706special_keys(char_u *args)
2707{
2708 char *save_str = nb_unquote(args, NULL);
2709 char *tok = strtok(save_str, " ");
2710 char *sep;
2711 char keybuf[64];
2712 char cmdbuf[256];
2713
2714 while (tok != NULL)
2715 {
2716 int i = 0;
2717
2718 if ((sep = strchr(tok, '-')) != NULL)
2719 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002720 *sep = NUL;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002721 while (*tok)
2722 {
2723 switch (*tok)
2724 {
2725 case 'A':
2726 case 'M':
2727 case 'C':
2728 case 'S':
2729 keybuf[i++] = *tok;
2730 keybuf[i++] = '-';
2731 break;
2732 }
2733 tok++;
2734 }
2735 tok++;
2736 }
2737
2738 strcpy(&keybuf[i], tok);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002739 vim_snprintf(cmdbuf, sizeof(cmdbuf),
2740 "<silent><%s> :nbkey %s<CR>", keybuf, keybuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002741 do_map(0, (char_u *)cmdbuf, NORMAL, FALSE);
2742 tok = strtok(NULL, " ");
2743 }
2744 vim_free(save_str);
2745}
2746
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002747 void
2748ex_nbclose(eap)
2749 exarg_T *eap UNUSED;
2750{
2751 netbeans_close();
2752}
Bram Moolenaar009b2592004-10-24 19:18:58 +00002753
2754 void
2755ex_nbkey(eap)
2756 exarg_T *eap;
2757{
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002758 (void)netbeans_keystring(eap->arg);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002759}
2760
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002761 void
2762ex_nbstart(eap)
2763 exarg_T *eap;
2764{
2765 netbeans_open((char *)eap->arg, FALSE);
2766}
Bram Moolenaar009b2592004-10-24 19:18:58 +00002767
2768/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 * Initialize highlights and signs for use by netbeans (mostly obsolete)
2770 */
2771 static void
2772nb_init_graphics(void)
2773{
2774 static int did_init = FALSE;
2775
2776 if (!did_init)
2777 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002778 coloncmd(":highlight NBGuarded guibg=Cyan guifg=Black"
2779 " ctermbg=LightCyan ctermfg=Black");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 coloncmd(":sign define %d linehl=NBGuarded", GUARDED);
2781
2782 did_init = TRUE;
2783 }
2784}
2785
2786/*
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002787 * Convert key to netbeans name. This uses the global "mod_mask".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 */
2789 static void
2790netbeans_keyname(int key, char *buf)
2791{
2792 char *name = 0;
2793 char namebuf[2];
2794 int ctrl = 0;
2795 int shift = 0;
2796 int alt = 0;
2797
2798 if (mod_mask & MOD_MASK_CTRL)
2799 ctrl = 1;
2800 if (mod_mask & MOD_MASK_SHIFT)
2801 shift = 1;
2802 if (mod_mask & MOD_MASK_ALT)
2803 alt = 1;
2804
2805
2806 switch (key)
2807 {
2808 case K_F1: name = "F1"; break;
2809 case K_S_F1: name = "F1"; shift = 1; break;
2810 case K_F2: name = "F2"; break;
2811 case K_S_F2: name = "F2"; shift = 1; break;
2812 case K_F3: name = "F3"; break;
2813 case K_S_F3: name = "F3"; shift = 1; break;
2814 case K_F4: name = "F4"; break;
2815 case K_S_F4: name = "F4"; shift = 1; break;
2816 case K_F5: name = "F5"; break;
2817 case K_S_F5: name = "F5"; shift = 1; break;
2818 case K_F6: name = "F6"; break;
2819 case K_S_F6: name = "F6"; shift = 1; break;
2820 case K_F7: name = "F7"; break;
2821 case K_S_F7: name = "F7"; shift = 1; break;
2822 case K_F8: name = "F8"; break;
2823 case K_S_F8: name = "F8"; shift = 1; break;
2824 case K_F9: name = "F9"; break;
2825 case K_S_F9: name = "F9"; shift = 1; break;
2826 case K_F10: name = "F10"; break;
2827 case K_S_F10: name = "F10"; shift = 1; break;
2828 case K_F11: name = "F11"; break;
2829 case K_S_F11: name = "F11"; shift = 1; break;
2830 case K_F12: name = "F12"; break;
2831 case K_S_F12: name = "F12"; shift = 1; break;
2832 default:
2833 if (key >= ' ' && key <= '~')
2834 {
2835 /* Allow ASCII characters. */
2836 name = namebuf;
2837 namebuf[0] = key;
2838 namebuf[1] = NUL;
2839 }
2840 else
2841 name = "X";
2842 break;
2843 }
2844
2845 buf[0] = '\0';
2846 if (ctrl)
2847 strcat(buf, "C");
2848 if (shift)
2849 strcat(buf, "S");
2850 if (alt)
2851 strcat(buf, "M"); /* META */
2852 if (ctrl || shift || alt)
2853 strcat(buf, "-");
2854 strcat(buf, name);
2855}
2856
Bram Moolenaar67c53842010-05-22 18:28:27 +02002857#if defined(FEAT_BEVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858/*
2859 * Function to be called for balloon evaluation. Grabs the text under the
2860 * cursor and sends it to the debugger for evaluation. The debugger should
2861 * respond with a showBalloon command when there is a useful result.
2862 */
Bram Moolenaard62bec82005-03-07 22:56:57 +00002863 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864netbeans_beval_cb(
2865 BalloonEval *beval,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002866 int state UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867{
Bram Moolenaard62bec82005-03-07 22:56:57 +00002868 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 char_u *text;
Bram Moolenaard62bec82005-03-07 22:56:57 +00002870 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 int col;
2872 char buf[MAXPATHL * 2 + 25];
2873 char_u *p;
2874
2875 /* Don't do anything when 'ballooneval' is off, messages scrolled the
2876 * windows up or we have no connection. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002877 if (!p_beval || msg_scrolled > 0 || !NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 return;
2879
Bram Moolenaard62bec82005-03-07 22:56:57 +00002880 if (get_beval_info(beval, TRUE, &wp, &lnum, &text, &col) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 {
2882 /* Send debugger request. Only when the text is of reasonable
2883 * length. */
2884 if (text != NULL && text[0] != NUL && STRLEN(text) < MAXPATHL)
2885 {
2886 p = nb_quote(text);
2887 if (p != NULL)
Bram Moolenaar009b2592004-10-24 19:18:58 +00002888 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002889 vim_snprintf(buf, sizeof(buf),
Bram Moolenaar89d40322006-08-29 15:30:07 +00002890 "0:balloonText=%d \"%s\"\n", r_cmdno, p);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002891 vim_free(p);
2892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 nbdebug(("EVT: %s", buf));
2894 nb_send(buf, "netbeans_beval_cb");
2895 }
2896 vim_free(text);
2897 }
2898}
2899#endif
2900
2901/*
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002902 * Return TRUE when the netbeans connection is closed.
2903 */
2904 int
2905netbeans_active(void)
2906{
2907 return NETBEANS_OPEN;
2908}
2909
2910/*
Bram Moolenaar67c53842010-05-22 18:28:27 +02002911 * Return netbeans file descriptor.
2912 */
2913 int
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002914netbeans_filedesc(void)
Bram Moolenaar67c53842010-05-22 18:28:27 +02002915{
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002916 return nbsock;
Bram Moolenaar67c53842010-05-22 18:28:27 +02002917}
2918
2919#if defined(FEAT_GUI) || defined(PROTO)
2920/*
2921 * Register our file descriptor with the gui event handling system.
2922 */
2923 void
2924netbeans_gui_register(void)
2925{
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002926 if (!NB_HAS_GUI || !NETBEANS_OPEN)
Bram Moolenaar67c53842010-05-22 18:28:27 +02002927 return;
2928
Bram Moolenaar67c53842010-05-22 18:28:27 +02002929# ifdef FEAT_GUI_MOTIF
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002930 /* tell notifier we are interested in being called
2931 * when there is input on the editor connection socket
2932 */
2933 if (inputHandler == (XtInputId)NULL)
2934 inputHandler = XtAppAddInput((XtAppContext)app_context, nbsock,
2935 (XtPointer)(XtInputReadMask + XtInputExceptMask),
2936 messageFromNetbeans, NULL);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002937# else
2938# ifdef FEAT_GUI_GTK
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002939 /*
2940 * Tell gdk we are interested in being called when there
2941 * is input on the editor connection socket
2942 */
2943 if (inputHandler == 0)
2944 inputHandler = gdk_input_add((gint)nbsock, (GdkInputCondition)
2945 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
2946 messageFromNetbeans, NULL);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002947# else
2948# ifdef FEAT_GUI_W32
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002949 /*
2950 * Tell Windows we are interested in receiving message when there
2951 * is input on the editor connection socket
2952 */
2953 if (inputHandler == -1)
2954 inputHandler = WSAAsyncSelect(nbsock, s_hwnd, WM_NETBEANS, FD_READ);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002955# endif
2956# endif
2957# endif
Bram Moolenaar67c53842010-05-22 18:28:27 +02002958
2959# ifdef FEAT_BEVAL
2960 bevalServers |= BEVAL_NETBEANS;
2961# endif
2962}
2963#endif
2964
2965/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 * Tell netbeans that the window was opened, ready for commands.
2967 */
2968 void
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002969netbeans_open(char *params, int abort)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970{
2971 char *cmd = "0:startupDone=0\n";
2972
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002973 if (NETBEANS_OPEN)
2974 {
2975 EMSG(_("E511: netbeans already connected"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002979 if (netbeans_connect(params, abort) != OK)
Bram Moolenaar67c53842010-05-22 18:28:27 +02002980 return;
2981#ifdef FEAT_GUI
2982 netbeans_gui_register();
Bram Moolenaard62bec82005-03-07 22:56:57 +00002983#endif
2984
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 nbdebug(("EVT: %s", cmd));
2986 nb_send(cmd, "netbeans_startup_done");
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002987
2988 /* update the screen after having added the gutter */
2989 changed_window_setting();
2990 update_screen(CLEAR);
2991 setcursor();
2992 out_flush();
2993#ifdef FEAT_GUI
2994 gui_update_cursor(TRUE, FALSE);
2995 gui_mch_flush();
2996#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997}
2998
Bram Moolenaar009b2592004-10-24 19:18:58 +00002999/*
3000 * Tell netbeans that we're exiting. This should be called right
3001 * before calling exit.
3002 */
3003 void
3004netbeans_send_disconnect()
3005{
3006 char buf[128];
3007
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003008 if (NETBEANS_OPEN)
Bram Moolenaar009b2592004-10-24 19:18:58 +00003009 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00003010 sprintf(buf, "0:disconnect=%d\n", r_cmdno);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003011 nbdebug(("EVT: %s", buf));
3012 nb_send(buf, "netbeans_disconnect");
3013 }
3014}
3015
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_W32) || defined(PROTO)
3017/*
3018 * Tell netbeans that the window was moved or resized.
3019 */
3020 void
3021netbeans_frame_moved(int new_x, int new_y)
3022{
3023 char buf[128];
3024
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003025 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 return;
3027
3028 sprintf(buf, "0:geometry=%d %d %d %d %d\n",
Bram Moolenaar89d40322006-08-29 15:30:07 +00003029 r_cmdno, (int)Columns, (int)Rows, new_x, new_y);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003030 /*nbdebug(("EVT: %s", buf)); happens too many times during a move */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 nb_send(buf, "netbeans_frame_moved");
3032}
3033#endif
3034
3035/*
Bram Moolenaar009b2592004-10-24 19:18:58 +00003036 * Tell netbeans the user opened or activated a file.
3037 */
3038 void
3039netbeans_file_activated(buf_T *bufp)
3040{
3041 int bufno = nb_getbufno(bufp);
3042 nbbuf_T *bp = nb_get_buf(bufno);
3043 char buffer[2*MAXPATHL];
3044 char_u *q;
3045
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003046 if (!NETBEANS_OPEN || !bufp->b_netbeans_file || dosetvisible)
Bram Moolenaar009b2592004-10-24 19:18:58 +00003047 return;
3048
3049 q = nb_quote(bufp->b_ffname);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003050 if (q == NULL || bp == NULL)
Bram Moolenaar009b2592004-10-24 19:18:58 +00003051 return;
3052
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003053 vim_snprintf(buffer, sizeof(buffer), "%d:fileOpened=%d \"%s\" %s %s\n",
Bram Moolenaar009b2592004-10-24 19:18:58 +00003054 bufno,
3055 bufno,
3056 (char *)q,
3057 "T", /* open in NetBeans */
3058 "F"); /* modified */
3059
3060 vim_free(q);
3061 nbdebug(("EVT: %s", buffer));
3062
3063 nb_send(buffer, "netbeans_file_opened");
3064}
3065
3066/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 * Tell netbeans the user opened a file.
3068 */
3069 void
Bram Moolenaar009b2592004-10-24 19:18:58 +00003070netbeans_file_opened(buf_T *bufp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071{
Bram Moolenaar009b2592004-10-24 19:18:58 +00003072 int bufno = nb_getbufno(bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 char buffer[2*MAXPATHL];
3074 char_u *q;
Bram Moolenaar009b2592004-10-24 19:18:58 +00003075 nbbuf_T *bp = nb_get_buf(nb_getbufno(bufp));
3076 int bnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003078 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 return;
3080
Bram Moolenaar009b2592004-10-24 19:18:58 +00003081 q = nb_quote(bufp->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 if (q == NULL)
3083 return;
Bram Moolenaar009b2592004-10-24 19:18:58 +00003084 if (bp != NULL)
3085 bnum = bufno;
3086 else
3087 bnum = 0;
3088
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003089 vim_snprintf(buffer, sizeof(buffer), "%d:fileOpened=%d \"%s\" %s %s\n",
Bram Moolenaar009b2592004-10-24 19:18:58 +00003090 bnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 0,
3092 (char *)q,
3093 "T", /* open in NetBeans */
3094 "F"); /* modified */
3095
3096 vim_free(q);
3097 nbdebug(("EVT: %s", buffer));
3098
3099 nb_send(buffer, "netbeans_file_opened");
Bram Moolenaar009b2592004-10-24 19:18:58 +00003100 if (p_acd && vim_chdirfile(bufp->b_ffname) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 shorten_fnames(TRUE);
3102}
3103
3104/*
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00003105 * Tell netbeans that a file was deleted or wiped out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 */
3107 void
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00003108netbeans_file_killed(buf_T *bufp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109{
3110 int bufno = nb_getbufno(bufp);
3111 nbbuf_T *nbbuf = nb_get_buf(bufno);
3112 char buffer[2*MAXPATHL];
3113
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003114 if (!NETBEANS_OPEN || bufno == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 return;
3116
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00003117 nbdebug(("netbeans_file_killed:\n"));
3118 nbdebug((" Killing bufno: %d", bufno));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119
Bram Moolenaar89d40322006-08-29 15:30:07 +00003120 sprintf(buffer, "%d:killed=%d\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121
3122 nbdebug(("EVT: %s", buffer));
3123
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00003124 nb_send(buffer, "netbeans_file_killed");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125
3126 if (nbbuf != NULL)
3127 nbbuf->bufp = NULL;
3128}
3129
3130/*
3131 * Get a pointer to the Netbeans buffer for Vim buffer "bufp".
3132 * Return NULL if there is no such buffer or changes are not to be reported.
3133 * Otherwise store the buffer number in "*bufnop".
3134 */
3135 static nbbuf_T *
3136nb_bufp2nbbuf_fire(buf_T *bufp, int *bufnop)
3137{
3138 int bufno;
3139 nbbuf_T *nbbuf;
3140
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003141 if (!NETBEANS_OPEN || !netbeansFireChanges)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 return NULL; /* changes are not reported at all */
3143
3144 bufno = nb_getbufno(bufp);
3145 if (bufno <= 0)
3146 return NULL; /* file is not known to NetBeans */
3147
3148 nbbuf = nb_get_buf(bufno);
3149 if (nbbuf != NULL && !nbbuf->fireChanges)
3150 return NULL; /* changes in this buffer are not reported */
3151
3152 *bufnop = bufno;
3153 return nbbuf;
3154}
3155
3156/*
3157 * Tell netbeans the user inserted some text.
3158 */
3159 void
3160netbeans_inserted(
3161 buf_T *bufp,
3162 linenr_T linenr,
3163 colnr_T col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 char_u *txt,
3165 int newlen)
3166{
3167 char_u *buf;
3168 int bufno;
3169 nbbuf_T *nbbuf;
3170 pos_T pos;
3171 long off;
3172 char_u *p;
3173 char_u *newtxt;
3174
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003175 if (!NETBEANS_OPEN)
3176 return;
3177
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3179 if (nbbuf == NULL)
3180 return;
3181
Bram Moolenaar009b2592004-10-24 19:18:58 +00003182 /* Don't mark as modified for initial read */
3183 if (nbbuf->insertDone)
3184 nbbuf->modified = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185
3186 pos.lnum = linenr;
3187 pos.col = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 off = pos2off(bufp, &pos);
3189
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 /* send the "insert" EVT */
3191 newtxt = alloc(newlen + 1);
Bram Moolenaarb6356332005-07-18 21:40:44 +00003192 vim_strncpy(newtxt, txt, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 p = nb_quote(newtxt);
3194 if (p != NULL)
3195 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00003196 buf = alloc(128 + 2*newlen);
Bram Moolenaar89d40322006-08-29 15:30:07 +00003197 sprintf((char *)buf, "%d:insert=%d %ld \"%s\"\n",
3198 bufno, r_cmdno, off, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 nbdebug(("EVT: %s", buf));
3200 nb_send((char *)buf, "netbeans_inserted");
Bram Moolenaar009b2592004-10-24 19:18:58 +00003201 vim_free(p);
3202 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 vim_free(newtxt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205}
3206
3207/*
3208 * Tell netbeans some bytes have been removed.
3209 */
3210 void
3211netbeans_removed(
3212 buf_T *bufp,
3213 linenr_T linenr,
3214 colnr_T col,
3215 long len)
3216{
3217 char_u buf[128];
3218 int bufno;
3219 nbbuf_T *nbbuf;
3220 pos_T pos;
3221 long off;
3222
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003223 if (!NETBEANS_OPEN)
3224 return;
3225
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3227 if (nbbuf == NULL)
3228 return;
3229
3230 if (len < 0)
3231 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00003232 nbdebug(("Negative len %ld in netbeans_removed()!\n", len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 return;
3234 }
3235
3236 nbbuf->modified = 1;
3237
3238 pos.lnum = linenr;
3239 pos.col = col;
3240
3241 off = pos2off(bufp, &pos);
3242
Bram Moolenaar89d40322006-08-29 15:30:07 +00003243 sprintf((char *)buf, "%d:remove=%d %ld %ld\n", bufno, r_cmdno, off, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 nbdebug(("EVT: %s", buf));
3245 nb_send((char *)buf, "netbeans_removed");
3246}
3247
3248/*
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003249 * Send netbeans an unmodified command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003252netbeans_unmodified(buf_T *bufp UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253{
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003254 if (!NETBEANS_OPEN)
3255 return;
3256
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257#if 0
3258 char_u buf[128];
3259 int bufno;
3260 nbbuf_T *nbbuf;
3261
3262 /* This has been disabled, because NetBeans considers a buffer modified
3263 * even when all changes have been undone. */
3264 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3265 if (nbbuf == NULL)
3266 return;
3267
3268 nbbuf->modified = 0;
3269
Bram Moolenaar89d40322006-08-29 15:30:07 +00003270 sprintf((char *)buf, "%d:unmodified=%d\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 nbdebug(("EVT: %s", buf));
3272 nb_send((char *)buf, "netbeans_unmodified");
3273#endif
3274}
3275
3276/*
3277 * Send a button release event back to netbeans. Its up to netbeans
3278 * to decide what to do (if anything) with this event.
3279 */
3280 void
3281netbeans_button_release(int button)
3282{
3283 char buf[128];
3284 int bufno;
3285
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003286 if (!NETBEANS_OPEN)
3287 return;
3288
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 bufno = nb_getbufno(curbuf);
3290
3291 if (bufno >= 0 && curwin != NULL && curwin->w_buffer == curbuf)
3292 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003293 int col = mouse_col - W_WINCOL(curwin)
Bram Moolenaar67c53842010-05-22 18:28:27 +02003294 - ((curwin->w_p_nu || curwin->w_p_rnu) ? 9 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 long off = pos2off(curbuf, &curwin->w_cursor);
3296
3297 /* sync the cursor position */
Bram Moolenaar89d40322006-08-29 15:30:07 +00003298 sprintf(buf, "%d:newDotAndMark=%d %ld %ld\n", bufno, r_cmdno, off, off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 nbdebug(("EVT: %s", buf));
3300 nb_send(buf, "netbeans_button_release[newDotAndMark]");
3301
Bram Moolenaar89d40322006-08-29 15:30:07 +00003302 sprintf(buf, "%d:buttonRelease=%d %d %ld %d\n", bufno, r_cmdno,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 button, (long)curwin->w_cursor.lnum, col);
3304 nbdebug(("EVT: %s", buf));
3305 nb_send(buf, "netbeans_button_release");
3306 }
3307}
3308
3309
3310/*
Bram Moolenaar143c38c2007-05-10 16:41:10 +00003311 * Send a keypress event back to netbeans. This usually simulates some
Bram Moolenaar009b2592004-10-24 19:18:58 +00003312 * kind of function key press. This function operates on a key code.
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003313 * Return TRUE when the key was sent, FALSE when the command has been
3314 * postponed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 */
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003316 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317netbeans_keycommand(int key)
3318{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 char keyName[60];
Bram Moolenaar009b2592004-10-24 19:18:58 +00003320
3321 netbeans_keyname(key, keyName);
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003322 return netbeans_keystring((char_u *)keyName);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003323}
3324
3325
3326/*
Bram Moolenaar143c38c2007-05-10 16:41:10 +00003327 * Send a keypress event back to netbeans. This usually simulates some
Bram Moolenaar009b2592004-10-24 19:18:58 +00003328 * kind of function key press. This function operates on a key string.
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003329 * Return TRUE when the key was sent, FALSE when the command has been
3330 * postponed.
Bram Moolenaar009b2592004-10-24 19:18:58 +00003331 */
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003332 static int
3333netbeans_keystring(char_u *keyName)
Bram Moolenaar009b2592004-10-24 19:18:58 +00003334{
3335 char buf[2*MAXPATHL];
3336 int bufno = nb_getbufno(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 long off;
3338 char_u *q;
3339
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003340 if (!NETBEANS_OPEN)
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003341 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 if (bufno == -1)
3344 {
3345 nbdebug(("got keycommand for non-NetBeans buffer, opening...\n"));
3346 q = curbuf->b_ffname == NULL ? (char_u *)""
3347 : nb_quote(curbuf->b_ffname);
3348 if (q == NULL)
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003349 return TRUE;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003350 vim_snprintf(buf, sizeof(buf), "0:fileOpened=%d \"%s\" %s %s\n", 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 q,
3352 "T", /* open in NetBeans */
3353 "F"); /* modified */
3354 if (curbuf->b_ffname != NULL)
3355 vim_free(q);
3356 nbdebug(("EVT: %s", buf));
3357 nb_send(buf, "netbeans_keycommand");
3358
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003359 postpone_keycommand(keyName);
3360 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 }
3362
3363 /* sync the cursor position */
3364 off = pos2off(curbuf, &curwin->w_cursor);
Bram Moolenaar89d40322006-08-29 15:30:07 +00003365 sprintf(buf, "%d:newDotAndMark=%d %ld %ld\n", bufno, r_cmdno, off, off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 nbdebug(("EVT: %s", buf));
3367 nb_send(buf, "netbeans_keycommand");
3368
3369 /* To work on Win32 you must apply patch to ExtEditor module
3370 * from ExtEdCaret.java.diff - make EVT_newDotAndMark handler
3371 * more synchronous
3372 */
3373
3374 /* now send keyCommand event */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003375 vim_snprintf(buf, sizeof(buf), "%d:keyCommand=%d \"%s\"\n",
Bram Moolenaar89d40322006-08-29 15:30:07 +00003376 bufno, r_cmdno, keyName);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 nbdebug(("EVT: %s", buf));
3378 nb_send(buf, "netbeans_keycommand");
3379
3380 /* New: do both at once and include the lnum/col. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003381 vim_snprintf(buf, sizeof(buf), "%d:keyAtPos=%d \"%s\" %ld %ld/%ld\n",
Bram Moolenaar89d40322006-08-29 15:30:07 +00003382 bufno, r_cmdno, keyName,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 off, (long)curwin->w_cursor.lnum, (long)curwin->w_cursor.col);
3384 nbdebug(("EVT: %s", buf));
3385 nb_send(buf, "netbeans_keycommand");
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003386 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387}
3388
3389
3390/*
3391 * Send a save event to netbeans.
3392 */
3393 void
3394netbeans_save_buffer(buf_T *bufp)
3395{
3396 char_u buf[64];
3397 int bufno;
3398 nbbuf_T *nbbuf;
3399
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003400 if (!NETBEANS_OPEN)
3401 return;
3402
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3404 if (nbbuf == NULL)
3405 return;
3406
3407 nbbuf->modified = 0;
3408
Bram Moolenaar89d40322006-08-29 15:30:07 +00003409 sprintf((char *)buf, "%d:save=%d\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 nbdebug(("EVT: %s", buf));
3411 nb_send((char *)buf, "netbeans_save_buffer");
3412}
3413
3414
3415/*
3416 * Send remove command to netbeans (this command has been turned off).
3417 */
3418 void
3419netbeans_deleted_all_lines(buf_T *bufp)
3420{
3421 char_u buf[64];
3422 int bufno;
3423 nbbuf_T *nbbuf;
3424
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003425 if (!NETBEANS_OPEN)
3426 return;
3427
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3429 if (nbbuf == NULL)
3430 return;
3431
Bram Moolenaar009b2592004-10-24 19:18:58 +00003432 /* Don't mark as modified for initial read */
3433 if (nbbuf->insertDone)
3434 nbbuf->modified = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435
Bram Moolenaar89d40322006-08-29 15:30:07 +00003436 sprintf((char *)buf, "%d:remove=%d 0 -1\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 nbdebug(("EVT(suppressed): %s", buf));
3438/* nb_send(buf, "netbeans_deleted_all_lines"); */
3439}
3440
3441
3442/*
3443 * See if the lines are guarded. The top and bot parameters are from
3444 * u_savecommon(), these are the line above the change and the line below the
3445 * change.
3446 */
3447 int
3448netbeans_is_guarded(linenr_T top, linenr_T bot)
3449{
3450 signlist_T *p;
3451 int lnum;
3452
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003453 if (!NETBEANS_OPEN)
3454 return FALSE;
3455
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 for (p = curbuf->b_signlist; p != NULL; p = p->next)
3457 if (p->id >= GUARDEDOFFSET)
3458 for (lnum = top + 1; lnum < bot; lnum++)
3459 if (lnum == p->lnum)
3460 return TRUE;
3461
3462 return FALSE;
3463}
3464
3465#if defined(FEAT_GUI_MOTIF) || defined(PROTO)
3466/*
3467 * We have multiple signs to draw at the same location. Draw the
3468 * multi-sign indicator instead. This is the Motif version.
3469 */
3470 void
3471netbeans_draw_multisign_indicator(int row)
3472{
3473 int i;
3474 int y;
3475 int x;
3476
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003477 if (!NETBEANS_OPEN)
3478 return;
3479
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 x = 0;
3481 y = row * gui.char_height + 2;
3482
3483 for (i = 0; i < gui.char_height - 3; i++)
3484 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y++);
3485
3486 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+0, y);
3487 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3488 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+4, y++);
3489 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+1, y);
3490 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3491 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+3, y++);
3492 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3493}
3494#endif /* FEAT_GUI_MOTIF */
3495
3496#ifdef FEAT_GUI_GTK
3497/*
3498 * We have multiple signs to draw at the same location. Draw the
3499 * multi-sign indicator instead. This is the GTK/Gnome version.
3500 */
3501 void
3502netbeans_draw_multisign_indicator(int row)
3503{
3504 int i;
3505 int y;
3506 int x;
3507 GdkDrawable *drawable = gui.drawarea->window;
3508
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003509 if (!NETBEANS_OPEN)
3510 return;
3511
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 x = 0;
3513 y = row * gui.char_height + 2;
3514
3515 for (i = 0; i < gui.char_height - 3; i++)
3516 gdk_draw_point(drawable, gui.text_gc, x+2, y++);
3517
3518 gdk_draw_point(drawable, gui.text_gc, x+0, y);
3519 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3520 gdk_draw_point(drawable, gui.text_gc, x+4, y++);
3521 gdk_draw_point(drawable, gui.text_gc, x+1, y);
3522 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3523 gdk_draw_point(drawable, gui.text_gc, x+3, y++);
3524 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3525}
3526#endif /* FEAT_GUI_GTK */
3527
3528/*
3529 * If the mouse is clicked in the gutter of a line with multiple
3530 * annotations, cycle through the set of signs.
3531 */
3532 void
3533netbeans_gutter_click(linenr_T lnum)
3534{
3535 signlist_T *p;
3536
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003537 if (!NETBEANS_OPEN)
3538 return;
3539
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 for (p = curbuf->b_signlist; p != NULL; p = p->next)
3541 {
3542 if (p->lnum == lnum && p->next && p->next->lnum == lnum)
3543 {
3544 signlist_T *tail;
3545
3546 /* remove "p" from list, reinsert it at the tail of the sublist */
3547 if (p->prev)
3548 p->prev->next = p->next;
3549 else
3550 curbuf->b_signlist = p->next;
3551 p->next->prev = p->prev;
3552 /* now find end of sublist and insert p */
3553 for (tail = p->next;
3554 tail->next && tail->next->lnum == lnum
3555 && tail->next->id < GUARDEDOFFSET;
3556 tail = tail->next)
3557 ;
3558 /* tail now points to last entry with same lnum (except
3559 * that "guarded" annotations are always last) */
3560 p->next = tail->next;
3561 if (tail->next)
3562 tail->next->prev = p;
3563 p->prev = tail;
3564 tail->next = p;
3565 update_debug_sign(curbuf, lnum);
3566 break;
3567 }
3568 }
3569}
3570
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571/*
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003572 * Add a sign of the requested type at the requested location.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 *
3574 * Reverse engineering:
3575 * Apparently an annotation is defined the first time it is used in a buffer.
3576 * When the same annotation is used in two buffers, the second time we do not
3577 * need to define a new sign name but reuse the existing one. But since the
3578 * ID number used in the second buffer starts counting at one again, a mapping
3579 * is made from the ID specifically for the buffer to the global sign name
3580 * (which is a number).
3581 *
3582 * globalsignmap[] stores the signs that have been defined globally.
3583 * buf->signmapused[] maps buffer-local annotation IDs to an index in
3584 * globalsignmap[].
3585 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 static void
3587addsigntype(
3588 nbbuf_T *buf,
3589 int typeNum,
3590 char_u *typeName,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003591 char_u *tooltip UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 char_u *glyphFile,
Bram Moolenaar67c53842010-05-22 18:28:27 +02003593 char_u *fg,
3594 char_u *bg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 int i, j;
Bram Moolenaar67c53842010-05-22 18:28:27 +02003597 int use_fg = (*fg && STRCMP(fg, "none") != 0);
3598 int use_bg = (*bg && STRCMP(bg, "none") != 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599
3600 for (i = 0; i < globalsignmapused; i++)
3601 if (STRCMP(typeName, globalsignmap[i]) == 0)
3602 break;
3603
3604 if (i == globalsignmapused) /* not found; add it to global map */
3605 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003606 nbdebug(("DEFINEANNOTYPE(%d,%s,%s,%s,%s,%s)\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 typeNum, typeName, tooltip, glyphFile, fg, bg));
3608 if (use_fg || use_bg)
3609 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003610 char fgbuf[2 * (8 + MAX_COLOR_LENGTH) + 1];
3611 char bgbuf[2 * (8 + MAX_COLOR_LENGTH) + 1];
3612 char *ptr;
3613 int value;
3614
3615 value = strtol((char *)fg, &ptr, 10);
3616 if (ptr != (char *)fg)
3617 sprintf(fgbuf, "guifg=#%06x", value & 0xFFFFFF);
3618 else
3619 sprintf(fgbuf, "guifg=%s ctermfg=%s", fg, fg);
3620
3621 value = strtol((char *)bg, &ptr, 10);
3622 if (ptr != (char *)bg)
3623 sprintf(bgbuf, "guibg=#%06x", value & 0xFFFFFF);
3624 else
3625 sprintf(bgbuf, "guibg=%s ctermbg=%s", bg, bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626
3627 coloncmd(":highlight NB_%s %s %s", typeName, (use_fg) ? fgbuf : "",
3628 (use_bg) ? bgbuf : "");
3629 if (*glyphFile == NUL)
3630 /* no glyph, line highlighting only */
3631 coloncmd(":sign define %d linehl=NB_%s", i + 1, typeName);
3632 else if (vim_strsize(glyphFile) <= 2)
3633 /* one- or two-character glyph name, use as text glyph with
3634 * texthl */
3635 coloncmd(":sign define %d text=%s texthl=NB_%s", i + 1,
3636 glyphFile, typeName);
3637 else
3638 /* glyph, line highlighting */
3639 coloncmd(":sign define %d icon=%s linehl=NB_%s", i + 1,
3640 glyphFile, typeName);
3641 }
3642 else
3643 /* glyph, no line highlighting */
3644 coloncmd(":sign define %d icon=%s", i + 1, glyphFile);
3645
3646 if (STRCMP(typeName,"CurrentPC") == 0)
3647 curPCtype = typeNum;
3648
3649 if (globalsignmapused == globalsignmaplen)
3650 {
3651 if (globalsignmaplen == 0) /* first allocation */
3652 {
3653 globalsignmaplen = 20;
3654 globalsignmap = (char **)alloc_clear(globalsignmaplen*sizeof(char *));
3655 }
3656 else /* grow it */
3657 {
3658 int incr;
3659 int oldlen = globalsignmaplen;
3660
3661 globalsignmaplen *= 2;
3662 incr = globalsignmaplen - oldlen;
3663 globalsignmap = (char **)vim_realloc(globalsignmap,
3664 globalsignmaplen * sizeof(char *));
3665 memset(globalsignmap + oldlen, 0, incr * sizeof(char *));
3666 }
3667 }
3668
3669 globalsignmap[i] = (char *)typeName;
3670 globalsignmapused = i + 1;
3671 }
3672
3673 /* check local map; should *not* be found! */
3674 for (j = 0; j < buf->signmapused; j++)
3675 if (buf->signmap[j] == i + 1)
3676 return;
3677
3678 /* add to local map */
3679 if (buf->signmapused == buf->signmaplen)
3680 {
3681 if (buf->signmaplen == 0) /* first allocation */
3682 {
3683 buf->signmaplen = 5;
3684 buf->signmap = (int *)alloc_clear(buf->signmaplen * sizeof(int *));
3685 }
3686 else /* grow it */
3687 {
3688 int incr;
3689 int oldlen = buf->signmaplen;
3690 buf->signmaplen *= 2;
3691 incr = buf->signmaplen - oldlen;
3692 buf->signmap = (int *)vim_realloc(buf->signmap,
3693 buf->signmaplen*sizeof(int *));
3694 memset(buf->signmap + oldlen, 0, incr * sizeof(int *));
3695 }
3696 }
3697
3698 buf->signmap[buf->signmapused++] = i + 1;
3699
3700}
3701
3702
3703/*
3704 * See if we have the requested sign type in the buffer.
3705 */
3706 static int
3707mapsigntype(nbbuf_T *buf, int localsigntype)
3708{
3709 if (--localsigntype >= 0 && localsigntype < buf->signmapused)
3710 return buf->signmap[localsigntype];
3711
3712 return 0;
3713}
3714
3715
3716/*
3717 * Compute length of buffer, don't print anything.
3718 */
3719 static long
3720get_buf_size(buf_T *bufp)
3721{
3722 linenr_T lnum;
3723 long char_count = 0;
3724 int eol_size;
3725 long last_check = 100000L;
3726
3727 if (bufp->b_ml.ml_flags & ML_EMPTY)
3728 return 0;
3729 else
3730 {
3731 if (get_fileformat(bufp) == EOL_DOS)
3732 eol_size = 2;
3733 else
3734 eol_size = 1;
3735 for (lnum = 1; lnum <= bufp->b_ml.ml_line_count; ++lnum)
3736 {
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00003737 char_count += (long)STRLEN(ml_get_buf(bufp, lnum, FALSE))
3738 + eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 /* Check for a CTRL-C every 100000 characters */
3740 if (char_count > last_check)
3741 {
3742 ui_breakcheck();
3743 if (got_int)
3744 return char_count;
3745 last_check = char_count + 100000L;
3746 }
3747 }
3748 /* Correction for when last line doesn't have an EOL. */
3749 if (!bufp->b_p_eol && bufp->b_p_bin)
3750 char_count -= eol_size;
3751 }
3752
3753 return char_count;
3754}
3755
3756/*
3757 * Convert character offset to lnum,col
3758 */
3759 static pos_T *
3760off2pos(buf_T *buf, long offset)
3761{
3762 linenr_T lnum;
3763 static pos_T pos;
3764
3765 pos.lnum = 0;
3766 pos.col = 0;
3767#ifdef FEAT_VIRTUALEDIT
3768 pos.coladd = 0;
3769#endif
3770
3771 if (!(buf->b_ml.ml_flags & ML_EMPTY))
3772 {
3773 if ((lnum = ml_find_line_or_offset(buf, (linenr_T)0, &offset)) < 0)
3774 return NULL;
3775 pos.lnum = lnum;
3776 pos.col = offset;
3777 }
3778
3779 return &pos;
3780}
3781
3782/*
3783 * Convert an argument in the form "1234" to an offset and compute the
3784 * lnum/col from it. Convert an argument in the form "123/12" directly to a
3785 * lnum/col.
3786 * "argp" is advanced to after the argument.
3787 * Return a pointer to the position, NULL if something is wrong.
3788 */
3789 static pos_T *
3790get_off_or_lnum(buf_T *buf, char_u **argp)
3791{
3792 static pos_T mypos;
3793 long off;
3794
3795 off = strtol((char *)*argp, (char **)argp, 10);
3796 if (**argp == '/')
3797 {
3798 mypos.lnum = (linenr_T)off;
3799 ++*argp;
3800 mypos.col = strtol((char *)*argp, (char **)argp, 10);
3801#ifdef FEAT_VIRTUALEDIT
3802 mypos.coladd = 0;
3803#endif
3804 return &mypos;
3805 }
3806 return off2pos(buf, off);
3807}
3808
3809
3810/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003811 * Convert (lnum,col) to byte offset in the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 */
3813 static long
3814pos2off(buf_T *buf, pos_T *pos)
3815{
3816 long offset = 0;
3817
3818 if (!(buf->b_ml.ml_flags & ML_EMPTY))
3819 {
3820 if ((offset = ml_find_line_or_offset(buf, pos->lnum, 0)) < 0)
3821 return 0;
3822 offset += pos->col;
3823 }
3824
3825 return offset;
3826}
3827
3828
Bram Moolenaar009b2592004-10-24 19:18:58 +00003829/*
3830 * This message is printed after NetBeans opens a new file. Its
3831 * similar to the message readfile() uses, but since NetBeans
3832 * doesn't normally call readfile, we do our own.
3833 */
3834 static void
3835print_read_msg(buf)
3836 nbbuf_T *buf;
3837{
3838 int lnum = buf->bufp->b_ml.ml_line_count;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003839 long nchars = (long)buf->bufp->b_orig_size;
Bram Moolenaar009b2592004-10-24 19:18:58 +00003840 char_u c;
3841
3842 msg_add_fname(buf->bufp, buf->bufp->b_ffname);
3843 c = FALSE;
3844
3845 if (buf->bufp->b_p_ro)
3846 {
3847 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]"));
3848 c = TRUE;
3849 }
3850 if (!buf->bufp->b_start_eol)
3851 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003852 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]")
3853 : _("[Incomplete last line]"));
Bram Moolenaar009b2592004-10-24 19:18:58 +00003854 c = TRUE;
3855 }
3856 msg_add_lines(c, (long)lnum, nchars);
3857
3858 /* Now display it */
3859 vim_free(keep_msg);
3860 keep_msg = NULL;
3861 msg_scrolled_ign = TRUE;
3862 msg_trunc_attr(IObuff, FALSE, 0);
3863 msg_scrolled_ign = FALSE;
3864}
3865
3866
3867/*
Bram Moolenaar67c53842010-05-22 18:28:27 +02003868 * Print a message after NetBeans writes the file. This message should be
3869 * identical to the standard message a non-netbeans user would see when
3870 * writing a file.
Bram Moolenaar009b2592004-10-24 19:18:58 +00003871 */
3872 static void
3873print_save_msg(buf, nchars)
3874 nbbuf_T *buf;
3875 long nchars;
3876{
3877 char_u c;
3878 char_u *p;
3879
3880 if (nchars >= 0)
3881 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003882 /* put fname in IObuff with quotes */
3883 msg_add_fname(buf->bufp, buf->bufp->b_ffname);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003884 c = FALSE;
3885
3886 msg_add_lines(c, buf->bufp->b_ml.ml_line_count,
3887 (long)buf->bufp->b_orig_size);
3888
3889 vim_free(keep_msg);
3890 keep_msg = NULL;
3891 msg_scrolled_ign = TRUE;
3892 p = msg_trunc_attr(IObuff, FALSE, 0);
3893 if ((msg_scrolled && !need_wait_return) || !buf->initDone)
3894 {
3895 /* Need to repeat the message after redrawing when:
3896 * - When reading from stdin (the screen will be cleared next).
3897 * - When restart_edit is set (otherwise there will be a delay
3898 * before redrawing).
3899 * - When the screen was scrolled but there is no wait-return
3900 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003901 set_keep_msg(p, 0);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003902 }
3903 msg_scrolled_ign = FALSE;
3904 /* add_to_input_buf((char_u *)"\f", 1); */
3905 }
3906 else
3907 {
3908 char_u ebuf[BUFSIZ];
3909
3910 STRCPY(ebuf, (char_u *)_("E505: "));
3911 STRCAT(ebuf, IObuff);
3912 STRCAT(ebuf, (char_u *)_("is read-only (add ! to override)"));
3913 STRCPY(IObuff, ebuf);
Bram Moolenaarf2330482008-06-24 20:19:36 +00003914 nbdebug((" %s\n", ebuf ));
Bram Moolenaar009b2592004-10-24 19:18:58 +00003915 emsg(IObuff);
3916 }
3917}
3918
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919#endif /* defined(FEAT_NETBEANS_INTG) */