blob: 02e92371d6236ea107af6a62a20200ea516c147f [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * Netbeans integration by David Weatherford
5 * Adopted for Win32 by Sergey Khorev
6 *
7 * Do ":help uganda" in Vim to read copying and usage conditions.
8 * Do ":help credits" in Vim to see a list of people who contributed.
9 */
10
11/*
12 * Implements client side of org.netbeans.modules.emacs editor
13 * integration protocol. Be careful! The protocol uses offsets
14 * which are *between* characters, whereas vim uses line number
15 * and column number which are *on* characters.
16 * See ":help netbeans-protocol" for explanation.
17 */
18
Bram Moolenaarc236c162008-07-13 17:41:49 +000019#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
Bram Moolenaarff064e12008-06-09 13:10:45 +000020# include "vimio.h" /* for mch_open(), must be before vim.h */
21#endif
22
Bram Moolenaar071d4272004-06-13 20:20:40 +000023#include "vim.h"
24
25#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
26
Bram Moolenaarb26e6322010-05-22 21:34:09 +020027/* TODO: when should this not be defined? */
28#define INET_SOCKETS
29
Bram Moolenaar071d4272004-06-13 20:20:40 +000030/* Note: when making changes here also adjust configure.in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000031#ifdef WIN32
32# ifdef DEBUG
33# include <tchar.h> /* for _T definition for TRACEn macros */
34# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000035/* WinSock API is separated from C API, thus we can't use read(), write(),
36 * errno... */
Bram Moolenaarc39125d2010-05-23 12:06:58 +020037# define SOCK_ERRNO errno = WSAGetLastError()
Bram Moolenaarbd42a0f2009-06-16 14:57:26 +000038# undef ECONNREFUSED
Bram Moolenaar071d4272004-06-13 20:20:40 +000039# define ECONNREFUSED WSAECONNREFUSED
40# ifdef EINTR
41# undef EINTR
42# endif
43# define EINTR WSAEINTR
44# define sock_write(sd, buf, len) send(sd, buf, len, 0)
45# define sock_read(sd, buf, len) recv(sd, buf, len, 0)
46# define sock_close(sd) closesocket(sd)
47# define sleep(t) Sleep(t*1000) /* WinAPI Sleep() accepts milliseconds */
48#else
Bram Moolenaarb26e6322010-05-22 21:34:09 +020049# ifdef INET_SOCKETS
50# include <netdb.h>
51# include <netinet/in.h>
52# else
53# include <sys/un.h>
54# endif
55
Bram Moolenaar071d4272004-06-13 20:20:40 +000056# include <sys/socket.h>
57# ifdef HAVE_LIBGEN_H
58# include <libgen.h>
59# endif
Bram Moolenaarc39125d2010-05-23 12:06:58 +020060# define SOCK_ERRNO
Bram Moolenaar071d4272004-06-13 20:20:40 +000061# define sock_write(sd, buf, len) write(sd, buf, len)
62# define sock_read(sd, buf, len) read(sd, buf, len)
63# define sock_close(sd) close(sd)
64#endif
65
66#include "version.h"
67
Bram Moolenaar071d4272004-06-13 20:20:40 +000068#define GUARDED 10000 /* typenr for "guarded" annotation */
69#define GUARDEDOFFSET 1000000 /* base for "guarded" sign id's */
Bram Moolenaar67c53842010-05-22 18:28:27 +020070#define MAX_COLOR_LENGTH 32 /* max length of color name in defineAnnoType */
Bram Moolenaar071d4272004-06-13 20:20:40 +000071
72/* The first implementation (working only with Netbeans) returned "1.1". The
73 * protocol implemented here also supports A-A-P. */
Bram Moolenaar67c53842010-05-22 18:28:27 +020074static char *ExtEdProtocolVersion = "2.5";
Bram Moolenaar071d4272004-06-13 20:20:40 +000075
76static long pos2off __ARGS((buf_T *, pos_T *));
77static pos_T *off2pos __ARGS((buf_T *, long));
78static pos_T *get_off_or_lnum __ARGS((buf_T *buf, char_u **argp));
79static long get_buf_size __ARGS((buf_T *));
Bram Moolenaar8065d7f2010-01-19 15:13:14 +010080static int netbeans_keystring __ARGS((char_u *keystr));
81static void postpone_keycommand __ARGS((char_u *keystr));
Bram Moolenaar009b2592004-10-24 19:18:58 +000082static void special_keys __ARGS((char_u *args));
Bram Moolenaar071d4272004-06-13 20:20:40 +000083
Bram Moolenaarb26e6322010-05-22 21:34:09 +020084static int netbeans_connect __ARGS((char *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +000085static int getConnInfo __ARGS((char *file, char **host, char **port, char **password));
86
87static void nb_init_graphics __ARGS((void));
88static void coloncmd __ARGS((char *cmd, ...));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000089static void nb_set_curbuf __ARGS((buf_T *buf));
Bram Moolenaar173c9852010-09-29 17:27:01 +020090#ifdef FEAT_GUI_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +000091static void messageFromNetbeans __ARGS((XtPointer, int *, XtInputId *));
92#endif
93#ifdef FEAT_GUI_GTK
94static void messageFromNetbeans __ARGS((gpointer, gint, GdkInputCondition));
95#endif
96static void nb_parse_cmd __ARGS((char_u *));
97static int nb_do_cmd __ARGS((int, char_u *, int, int, char_u *));
98static void nb_send __ARGS((char *buf, char *fun));
Bram Moolenaarb26e6322010-05-22 21:34:09 +020099static void nb_free __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100
Bram Moolenaar67c53842010-05-22 18:28:27 +0200101/* TRUE when netbeans is running with a GUI. */
102#ifdef FEAT_GUI
103# define NB_HAS_GUI (gui.in_use || gui.starting)
104#endif
105
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000106#ifdef WIN64
107typedef __int64 NBSOCK;
108#else
109typedef int NBSOCK;
110#endif
111
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200112static NBSOCK nbsock = -1; /* socket fd for Netbeans connection */
113#define NETBEANS_OPEN (nbsock != -1)
114
Bram Moolenaar173c9852010-09-29 17:27:01 +0200115#ifdef FEAT_GUI_X11
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200116static XtInputId inputHandler = (XtInputId)NULL; /* Cookie for input */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117#endif
118#ifdef FEAT_GUI_GTK
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200119static gint inputHandler = 0; /* Cookie for input */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120#endif
121#ifdef FEAT_GUI_W32
122static int inputHandler = -1; /* simply ret.value of WSAAsyncSelect() */
123extern HWND s_hwnd; /* Gvim's Window handle */
124#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +0000125static int r_cmdno; /* current command number for reply */
Bram Moolenaar009b2592004-10-24 19:18:58 +0000126static int dosetvisible = FALSE;
127
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128/*
129 * Include the debugging code if wanted.
130 */
131#ifdef NBDEBUG
132# include "nbdebug.c"
133#endif
134
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200135static int needupdate = 0;
136static int inAtomic = 0;
137
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138 static void
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200139netbeans_close(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140{
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200141 if (!NETBEANS_OPEN)
142 return;
143
144 netbeans_send_disconnect();
145
Bram Moolenaar173c9852010-09-29 17:27:01 +0200146#ifdef FEAT_GUI_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147 if (inputHandler != (XtInputId)NULL)
148 {
149 XtRemoveInput(inputHandler);
150 inputHandler = (XtInputId)NULL;
151 }
Bram Moolenaar67c53842010-05-22 18:28:27 +0200152#else
153# ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154 if (inputHandler != 0)
155 {
156 gdk_input_remove(inputHandler);
157 inputHandler = 0;
158 }
Bram Moolenaar67c53842010-05-22 18:28:27 +0200159# else
160# ifdef FEAT_GUI_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 if (inputHandler == 0)
162 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200163 WSAAsyncSelect(nbsock, s_hwnd, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164 inputHandler = -1;
165 }
Bram Moolenaar67c53842010-05-22 18:28:27 +0200166# endif
167# endif
168#endif
169
Bram Moolenaar67c53842010-05-22 18:28:27 +0200170#ifdef FEAT_BEVAL
Bram Moolenaard62bec82005-03-07 22:56:57 +0000171 bevalServers &= ~BEVAL_NETBEANS;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200172#endif
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200173
174 sock_close(nbsock);
175 nbsock = -1;
176
177 needupdate = 0;
178 inAtomic = 0;
179 nb_free();
180
181 /* remove all signs and update the screen after gutter removal */
182 coloncmd(":sign unplace *");
183 changed_window_setting();
184 update_screen(CLEAR);
185 setcursor();
186 out_flush();
187#ifdef FEAT_GUI
Bram Moolenaard54a6882010-08-09 22:49:00 +0200188 if (gui.in_use)
189 {
190 gui_update_cursor(TRUE, FALSE);
191 gui_mch_flush();
192 }
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200193#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195
196#define NB_DEF_HOST "localhost"
197#define NB_DEF_ADDR "3219"
198#define NB_DEF_PASS "changeme"
199
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200200 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200201netbeans_connect(char *params, int doabort)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202{
203#ifdef INET_SOCKETS
204 struct sockaddr_in server;
205 struct hostent * host;
206# ifdef FEAT_GUI_W32
207 u_short port;
208# else
209 int port;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200210# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211#else
212 struct sockaddr_un server;
213#endif
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200214 int sd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215 char buf[32];
216 char *hostname = NULL;
217 char *address = NULL;
218 char *password = NULL;
219 char *fname;
220 char *arg = NULL;
221
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200222 if (*params == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200224 /* "=fname": Read info from specified file. */
225 if (getConnInfo(params + 1, &hostname, &address, &password)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226 == FAIL)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200227 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228 }
229 else
230 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200231 if (*params == ':')
232 /* ":<host>:<addr>:<password>": get info from argument */
233 arg = params + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234 if (arg == NULL && (fname = getenv("__NETBEANS_CONINFO")) != NULL)
235 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200236 /* "": get info from file specified in environment */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 if (getConnInfo(fname, &hostname, &address, &password) == FAIL)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200238 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 }
240 else
241 {
242 if (arg != NULL)
243 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200244 /* ":<host>:<addr>:<password>": get info from argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 hostname = arg;
246 address = strchr(hostname, ':');
247 if (address != NULL)
248 {
249 *address++ = '\0';
250 password = strchr(address, ':');
251 if (password != NULL)
252 *password++ = '\0';
253 }
254 }
255
256 /* Get the missing values from the environment. */
257 if (hostname == NULL || *hostname == '\0')
258 hostname = getenv("__NETBEANS_HOST");
259 if (address == NULL)
260 address = getenv("__NETBEANS_SOCKET");
261 if (password == NULL)
262 password = getenv("__NETBEANS_VIM_PASSWORD");
263
264 /* Move values to allocated memory. */
265 if (hostname != NULL)
266 hostname = (char *)vim_strsave((char_u *)hostname);
267 if (address != NULL)
268 address = (char *)vim_strsave((char_u *)address);
269 if (password != NULL)
270 password = (char *)vim_strsave((char_u *)password);
271 }
272 }
273
274 /* Use the default when a value is missing. */
275 if (hostname == NULL || *hostname == '\0')
276 {
277 vim_free(hostname);
278 hostname = (char *)vim_strsave((char_u *)NB_DEF_HOST);
279 }
280 if (address == NULL || *address == '\0')
281 {
282 vim_free(address);
283 address = (char *)vim_strsave((char_u *)NB_DEF_ADDR);
284 }
285 if (password == NULL || *password == '\0')
286 {
287 vim_free(password);
288 password = (char *)vim_strsave((char_u *)NB_DEF_PASS);
289 }
290 if (hostname == NULL || address == NULL || password == NULL)
291 goto theend; /* out of memory */
292
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200293#ifdef FEAT_GUI_W32
294 netbeans_init_winsock();
295#endif
296
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297#ifdef INET_SOCKETS
298 port = atoi(address);
299
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000300 if ((sd = (NBSOCK)socket(AF_INET, SOCK_STREAM, 0)) == (NBSOCK)-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000302 nbdebug(("error in socket() in netbeans_connect()\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303 PERROR("socket() in netbeans_connect()");
304 goto theend;
305 }
306
307 /* Get the server internet address and put into addr structure */
308 /* fill in the socket address structure and connect to server */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200309 vim_memset((char *)&server, '\0', sizeof(server));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310 server.sin_family = AF_INET;
311 server.sin_port = htons(port);
312 if ((host = gethostbyname(hostname)) == NULL)
313 {
314 if (mch_access(hostname, R_OK) >= 0)
315 {
316 /* DEBUG: input file */
317 sd = mch_open(hostname, O_RDONLY, 0);
318 goto theend;
319 }
Bram Moolenaarf2330482008-06-24 20:19:36 +0000320 nbdebug(("error in gethostbyname() in netbeans_connect()\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321 PERROR("gethostbyname() in netbeans_connect()");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322 goto theend;
323 }
324 memcpy((char *)&server.sin_addr, host->h_addr, host->h_length);
325#else
326 if ((sd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
327 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000328 nbdebug(("error in socket() in netbeans_connect()\n"));
329 PERROR("socket() in netbeans_connect()");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330 goto theend;
331 }
332
333 server.sun_family = AF_UNIX;
334 strcpy(server.sun_path, address);
335#endif
336 /* Connect to server */
337 if (connect(sd, (struct sockaddr *)&server, sizeof(server)))
338 {
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200339 SOCK_ERRNO;
340 nbdebug(("netbeans_connect: Connect failed with errno %d\n", errno));
341 if (errno == ECONNREFUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342 {
343 sock_close(sd);
344#ifdef INET_SOCKETS
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000345 if ((sd = (NBSOCK)socket(AF_INET, SOCK_STREAM, 0)) == (NBSOCK)-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 {
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200347 SOCK_ERRNO;
Bram Moolenaarf2330482008-06-24 20:19:36 +0000348 nbdebug(("socket()#2 in netbeans_connect()\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 PERROR("socket()#2 in netbeans_connect()");
350 goto theend;
351 }
352#else
353 if ((sd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
354 {
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200355 SOCK_ERRNO;
Bram Moolenaarf2330482008-06-24 20:19:36 +0000356 nbdebug(("socket()#2 in netbeans_connect()\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 PERROR("socket()#2 in netbeans_connect()");
358 goto theend;
359 }
360#endif
361 if (connect(sd, (struct sockaddr *)&server, sizeof(server)))
362 {
363 int retries = 36;
364 int success = FALSE;
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200365
366 SOCK_ERRNO;
367 while (retries-- && ((errno == ECONNREFUSED)
368 || (errno == EINTR)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 {
370 nbdebug(("retrying...\n"));
371 sleep(5);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200372 if (!doabort)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200373 {
374 ui_breakcheck();
375 if (got_int)
376 {
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200377 errno = EINTR;
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200378 break;
379 }
380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 if (connect(sd, (struct sockaddr *)&server,
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200382 sizeof(server)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 {
384 success = TRUE;
385 break;
386 }
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200387 SOCK_ERRNO;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 }
389 if (!success)
390 {
391 /* Get here when the server can't be found. */
Bram Moolenaarf2330482008-06-24 20:19:36 +0000392 nbdebug(("Cannot connect to Netbeans #2\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 PERROR(_("Cannot connect to Netbeans #2"));
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200394 if (doabort)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200395 getout(1);
396 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397 }
398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 }
400 else
401 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000402 nbdebug(("Cannot connect to Netbeans\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 PERROR(_("Cannot connect to Netbeans"));
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200404 if (doabort)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200405 getout(1);
406 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 }
408 }
409
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200410 nbsock = sd;
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000411 vim_snprintf(buf, sizeof(buf), "AUTH %s\n", password);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412 nb_send(buf, "netbeans_connect");
413
414 sprintf(buf, "0:version=0 \"%s\"\n", ExtEdProtocolVersion);
415 nb_send(buf, "externaleditor_version");
416
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417theend:
418 vim_free(hostname);
419 vim_free(address);
420 vim_free(password);
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200421 return NETBEANS_OPEN ? OK : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422}
423
424/*
425 * Obtain the NetBeans hostname, port address and password from a file.
426 * Return the strings in allocated memory.
427 * Return FAIL if the file could not be read, OK otherwise (no matter what it
428 * contains).
429 */
430 static int
431getConnInfo(char *file, char **host, char **port, char **auth)
432{
433 FILE *fp;
434 char_u buf[BUFSIZ];
435 char_u *lp;
436 char_u *nl;
437#ifdef UNIX
438 struct stat st;
439
440 /*
441 * For Unix only accept the file when it's not accessible by others.
442 * The open will then fail if we don't own the file.
443 */
444 if (mch_stat(file, &st) == 0 && (st.st_mode & 0077) != 0)
445 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000446 nbdebug(("Wrong access mode for NetBeans connection info file: \"%s\"\n",
447 file));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 EMSG2(_("E668: Wrong access mode for NetBeans connection info file: \"%s\""),
449 file);
450 return FAIL;
451 }
452#endif
453
454 fp = mch_fopen(file, "r");
455 if (fp == NULL)
456 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000457 nbdebug(("Cannot open NetBeans connection info file\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 PERROR("E660: Cannot open NetBeans connection info file");
459 return FAIL;
460 }
461
462 /* Read the file. There should be one of each parameter */
463 while ((lp = (char_u *)fgets((char *)buf, BUFSIZ, fp)) != NULL)
464 {
465 if ((nl = vim_strchr(lp, '\n')) != NULL)
466 *nl = 0; /* strip off the trailing newline */
467
468 if (STRNCMP(lp, "host=", 5) == 0)
469 {
470 vim_free(*host);
471 *host = (char *)vim_strsave(&buf[5]);
472 }
473 else if (STRNCMP(lp, "port=", 5) == 0)
474 {
475 vim_free(*port);
476 *port = (char *)vim_strsave(&buf[5]);
477 }
478 else if (STRNCMP(lp, "auth=", 5) == 0)
479 {
480 vim_free(*auth);
481 *auth = (char *)vim_strsave(&buf[5]);
482 }
483 }
484 fclose(fp);
485
486 return OK;
487}
488
489
490struct keyqueue
491{
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100492 char_u *keystr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493 struct keyqueue *next;
494 struct keyqueue *prev;
495};
496
497typedef struct keyqueue keyQ_T;
498
499static keyQ_T keyHead; /* dummy node, header for circular queue */
500
501
502/*
503 * Queue up key commands sent from netbeans.
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100504 * We store the string, because it may depend on the global mod_mask and
505 * :nbkey doesn't have a key number.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 */
507 static void
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100508postpone_keycommand(char_u *keystr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509{
510 keyQ_T *node;
511
512 node = (keyQ_T *)alloc(sizeof(keyQ_T));
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100513 if (node == NULL)
514 return; /* out of memory, drop the key */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515
516 if (keyHead.next == NULL) /* initialize circular queue */
517 {
518 keyHead.next = &keyHead;
519 keyHead.prev = &keyHead;
520 }
521
522 /* insert node at tail of queue */
523 node->next = &keyHead;
524 node->prev = keyHead.prev;
525 keyHead.prev->next = node;
526 keyHead.prev = node;
527
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100528 node->keystr = vim_strsave(keystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529}
530
531/*
532 * Handle any queued-up NetBeans keycommands to be send.
533 */
534 static void
535handle_key_queue(void)
536{
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100537 int postponed = FALSE;
538
539 while (!postponed && keyHead.next && keyHead.next != &keyHead)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 {
541 /* first, unlink the node */
542 keyQ_T *node = keyHead.next;
543 keyHead.next = node->next;
544 node->next->prev = node->prev;
545
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100546 /* Now, send the keycommand. This may cause it to be postponed again
547 * and change keyHead. */
548 if (node->keystr != NULL)
549 postponed = !netbeans_keystring(node->keystr);
550 vim_free(node->keystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551
552 /* Finally, dispose of the node */
553 vim_free(node);
554 }
555}
556
557
558struct cmdqueue
559{
560 char_u *buffer;
561 struct cmdqueue *next;
562 struct cmdqueue *prev;
563};
564
565typedef struct cmdqueue queue_T;
566
567static queue_T head; /* dummy node, header for circular queue */
568
569
570/*
571 * Put the buffer on the work queue; possibly save it to a file as well.
572 */
573 static void
574save(char_u *buf, int len)
575{
576 queue_T *node;
577
578 node = (queue_T *)alloc(sizeof(queue_T));
579 if (node == NULL)
580 return; /* out of memory */
581 node->buffer = alloc(len + 1);
582 if (node->buffer == NULL)
583 {
584 vim_free(node);
585 return; /* out of memory */
586 }
587 mch_memmove(node->buffer, buf, (size_t)len);
588 node->buffer[len] = NUL;
589
590 if (head.next == NULL) /* initialize circular queue */
591 {
592 head.next = &head;
593 head.prev = &head;
594 }
595
596 /* insert node at tail of queue */
597 node->next = &head;
598 node->prev = head.prev;
599 head.prev->next = node;
600 head.prev = node;
601
602#ifdef NBDEBUG
603 {
604 static int outfd = -2;
605
606 /* possibly write buffer out to a file */
607 if (outfd == -3)
608 return;
609
610 if (outfd == -2)
611 {
612 char *file = getenv("__NETBEANS_SAVE");
613 if (file == NULL)
614 outfd = -3;
615 else
616 outfd = mch_open(file, O_WRONLY|O_CREAT|O_TRUNC, 0666);
617 }
618
619 if (outfd >= 0)
620 write(outfd, buf, len);
621 }
622#endif
623}
624
625
626/*
627 * While there's still a command in the work queue, parse and execute it.
628 */
Bram Moolenaarf2330482008-06-24 20:19:36 +0000629 void
630netbeans_parse_messages(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631{
632 char_u *p;
633 queue_T *node;
634
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200635 if (!NETBEANS_OPEN)
636 return;
637
Bram Moolenaarf2330482008-06-24 20:19:36 +0000638 while (head.next != NULL && head.next != &head)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 {
640 node = head.next;
641
642 /* Locate the first line in the first buffer. */
643 p = vim_strchr(node->buffer, '\n');
644 if (p == NULL)
645 {
646 /* Command isn't complete. If there is no following buffer,
647 * return (wait for more). If there is another buffer following,
648 * prepend the text to that buffer and delete this one. */
649 if (node->next == &head)
650 return;
Bram Moolenaarf2330482008-06-24 20:19:36 +0000651 p = alloc((unsigned)(STRLEN(node->buffer)
652 + STRLEN(node->next->buffer) + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 if (p == NULL)
654 return; /* out of memory */
655 STRCPY(p, node->buffer);
656 STRCAT(p, node->next->buffer);
657 vim_free(node->next->buffer);
658 node->next->buffer = p;
659
660 /* dispose of the node and buffer */
661 head.next = node->next;
662 node->next->prev = node->prev;
663 vim_free(node->buffer);
664 vim_free(node);
665 }
666 else
667 {
668 /* There is a complete command at the start of the buffer.
669 * Terminate it with a NUL. When no more text is following unlink
670 * the buffer. Do this before executing, because new buffers can
671 * be added while busy handling the command. */
672 *p++ = NUL;
673 if (*p == NUL)
674 {
675 head.next = node->next;
676 node->next->prev = node->prev;
677 }
678
679 /* now, parse and execute the commands */
680 nb_parse_cmd(node->buffer);
681
682 if (*p == NUL)
683 {
684 /* buffer finished, dispose of the node and buffer */
685 vim_free(node->buffer);
686 vim_free(node);
687 }
688 else
689 {
690 /* more follows, move to the start */
Bram Moolenaarf2330482008-06-24 20:19:36 +0000691 STRMOVE(node->buffer, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 }
693 }
694 }
695}
696
697/* Buffer size for reading incoming messages. */
698#define MAXMSGSIZE 4096
699
700/*
Bram Moolenaar67c53842010-05-22 18:28:27 +0200701 * Read a command from netbeans.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 */
Bram Moolenaar173c9852010-09-29 17:27:01 +0200703#ifdef FEAT_GUI_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 static void
Bram Moolenaara9d45512009-05-17 21:25:42 +0000705messageFromNetbeans(XtPointer clientData UNUSED,
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000706 int *unused1 UNUSED,
707 XtInputId *unused2 UNUSED)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200708{
709 netbeans_read();
710}
711#endif
712
713#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000715messageFromNetbeans(gpointer clientData UNUSED,
716 gint unused1 UNUSED,
717 GdkInputCondition unused2 UNUSED)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200718{
719 netbeans_read();
720}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721#endif
Bram Moolenaar67c53842010-05-22 18:28:27 +0200722
723 void
724netbeans_read()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725{
726 static char_u *buf = NULL;
Bram Moolenaar0b65f892010-05-15 14:49:02 +0200727 int len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 int readlen = 0;
Bram 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 Moolenaar071d4272004-06-13 20:20:40 +0000744 /* Allocate a buffer to read into. */
745 if (buf == NULL)
746 {
747 buf = alloc(MAXMSGSIZE);
748 if (buf == NULL)
749 return; /* out of memory! */
750 }
751
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100752 /* Keep on reading for as long as there is something to read.
753 * Use select() or poll() to avoid blocking on a message that is exactly
754 * MAXMSGSIZE long. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 for (;;)
756 {
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100757#ifdef HAVE_SELECT
758 FD_ZERO(&rfds);
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200759 FD_SET(nbsock, &rfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +0200760 tval.tv_sec = 0;
761 tval.tv_usec = 0;
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200762 if (select(nbsock + 1, &rfds, NULL, NULL, &tval) <= 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200763 break;
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100764#else
765# ifdef HAVE_POLL
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200766 fds.fd = nbsock;
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100767 fds.events = POLLIN;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200768 if (poll(&fds, 1, 0) <= 0)
769 break;
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100770# endif
771#endif
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200772 len = sock_read(nbsock, buf, MAXMSGSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 if (len <= 0)
774 break; /* error or nothing more to read */
775
776 /* Store the read message in the queue. */
777 save(buf, len);
778 readlen += len;
779 if (len < MAXMSGSIZE)
780 break; /* did read everything that's available */
781 }
782
783 if (readlen <= 0)
784 {
785 /* read error or didn't read anything */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200786 netbeans_close();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 nbdebug(("messageFromNetbeans: Error in read() from socket\n"));
788 if (len < 0)
Bram Moolenaarf2330482008-06-24 20:19:36 +0000789 {
790 nbdebug(("read from Netbeans socket\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 PERROR(_("read from Netbeans socket"));
Bram Moolenaarf2330482008-06-24 20:19:36 +0000792 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000793 return; /* don't try to parse it */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 }
795
Bram Moolenaar03531f72010-11-16 15:04:57 +0100796#if defined(NB_HAS_GUI) && defined(FEAT_GUI_GTK)
797 if (NB_HAS_GUI && gtk_main_level() > 0)
798 gtk_main_quit();
Bram Moolenaarf2330482008-06-24 20:19:36 +0000799#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800}
801
802/*
803 * Handle one NUL terminated command.
804 *
805 * format of a command from netbeans:
806 *
807 * 6:setTitle!84 "a.c"
808 *
809 * bufno
810 * colon
811 * cmd
812 * !
813 * cmdno
814 * args
815 *
816 * for function calls, the ! is replaced by a /
817 */
818 static void
819nb_parse_cmd(char_u *cmd)
820{
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000821 char *verb;
822 char *q;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 int bufno;
824 int isfunc = -1;
825
826 if (STRCMP(cmd, "DISCONNECT") == 0)
827 {
828 /* We assume the server knows that we can safely exit! */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 /* Disconnect before exiting, Motif hangs in a Select error
830 * message otherwise. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200831 netbeans_close();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 getout(0);
833 /* NOTREACHED */
834 }
835
836 if (STRCMP(cmd, "DETACH") == 0)
837 {
838 /* The IDE is breaking the connection. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200839 netbeans_close();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 return;
841 }
842
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000843 bufno = strtol((char *)cmd, &verb, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844
845 if (*verb != ':')
846 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000847 nbdebug((" missing colon: %s\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 EMSG2("E627: missing colon: %s", cmd);
849 return;
850 }
851 ++verb; /* skip colon */
852
853 for (q = verb; *q; q++)
854 {
855 if (*q == '!')
856 {
857 *q++ = NUL;
858 isfunc = 0;
859 break;
860 }
861 else if (*q == '/')
862 {
863 *q++ = NUL;
864 isfunc = 1;
865 break;
866 }
867 }
868
869 if (isfunc < 0)
870 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000871 nbdebug((" missing ! or / in: %s\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 EMSG2("E628: missing ! or / in: %s", cmd);
873 return;
874 }
875
Bram Moolenaar89d40322006-08-29 15:30:07 +0000876 r_cmdno = strtol(q, &q, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000878 q = (char *)skipwhite((char_u *)q);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879
Bram Moolenaar89d40322006-08-29 15:30:07 +0000880 if (nb_do_cmd(bufno, (char_u *)verb, isfunc, r_cmdno, (char_u *)q) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 {
Bram Moolenaar009b2592004-10-24 19:18:58 +0000882#ifdef NBDEBUG
883 /*
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100884 * This happens because the ExtEd can send a command or 2 after
Bram Moolenaar009b2592004-10-24 19:18:58 +0000885 * doing a stopDocumentListen command. It doesn't harm anything
886 * so I'm disabling it except for debugging.
887 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 nbdebug(("nb_parse_cmd: Command error for \"%s\"\n", cmd));
889 EMSG("E629: bad return from nb_do_cmd");
Bram Moolenaar009b2592004-10-24 19:18:58 +0000890#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 }
892}
893
894struct nbbuf_struct
895{
896 buf_T *bufp;
897 unsigned int fireChanges:1;
898 unsigned int initDone:1;
Bram Moolenaar009b2592004-10-24 19:18:58 +0000899 unsigned int insertDone:1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 unsigned int modified:1;
Bram Moolenaar009b2592004-10-24 19:18:58 +0000901 int nbbuf_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 char *displayname;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 int *signmap;
904 short_u signmaplen;
905 short_u signmapused;
906};
907
908typedef struct nbbuf_struct nbbuf_T;
909
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200910static nbbuf_T *buf_list = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000911static int buf_list_size = 0; /* size of buf_list */
912static int buf_list_used = 0; /* nr of entries in buf_list actually in use */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200914static char **globalsignmap = NULL;
915static int globalsignmaplen = 0;
916static int globalsignmapused = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917
918static int mapsigntype __ARGS((nbbuf_T *, int localsigntype));
919static void addsigntype __ARGS((nbbuf_T *, int localsigntype, char_u *typeName,
920 char_u *tooltip, char_u *glyphfile,
Bram Moolenaar67c53842010-05-22 18:28:27 +0200921 char_u *fg, char_u *bg));
Bram Moolenaar009b2592004-10-24 19:18:58 +0000922static void print_read_msg __ARGS((nbbuf_T *buf));
Bram Moolenaar914703b2010-05-31 21:59:46 +0200923static void print_save_msg __ARGS((nbbuf_T *buf, off_t nchars));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924
925static int curPCtype = -1;
926
927/*
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200928 * Free netbeans resources.
929 */
930 static void
931nb_free()
932{
933 keyQ_T *key_node = keyHead.next;
934 queue_T *cmd_node = head.next;
935 nbbuf_T buf;
936 buf_T *bufp;
937 int i;
938
939 /* free the netbeans buffer list */
940 for (i = 0; i < buf_list_used; i++)
941 {
942 buf = buf_list[i];
943 vim_free(buf.displayname);
944 vim_free(buf.signmap);
945 if ((bufp=buf.bufp) != NULL)
946 {
947 buf.bufp->b_netbeans_file = FALSE;
948 buf.bufp->b_was_netbeans_file = FALSE;
949 }
950 }
951 vim_free(buf_list);
952 buf_list = NULL;
953 buf_list_size = 0;
954 buf_list_used = 0;
955
956 /* free the queued key commands */
957 while(key_node != NULL && key_node != &keyHead)
958 {
959 keyQ_T *next = key_node->next;
960 vim_free(key_node->keystr);
961 vim_free(key_node);
962 if (next == &keyHead)
963 {
964 keyHead.next = &keyHead;
965 keyHead.prev = &keyHead;
966 break;
967 }
968 key_node = next;
969 }
970
971 /* free the queued netbeans commands */
972 while(cmd_node != NULL && cmd_node != &head)
973 {
974 queue_T *next = cmd_node->next;
975 vim_free(cmd_node->buffer);
976 vim_free(cmd_node);
977 if (next == &head)
978 {
979 head.next = &head;
980 head.prev = &head;
981 break;
982 }
983 cmd_node = next;
984 }
985}
986
987/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 * Get the Netbeans buffer number for the specified buffer.
989 */
990 static int
991nb_getbufno(buf_T *bufp)
992{
993 int i;
994
995 for (i = 0; i < buf_list_used; i++)
996 if (buf_list[i].bufp == bufp)
997 return i;
998 return -1;
999}
1000
1001/*
1002 * Is this a NetBeans-owned buffer?
1003 */
1004 int
1005isNetbeansBuffer(buf_T *bufp)
1006{
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001007 return NETBEANS_OPEN && bufp->b_netbeans_file;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008}
1009
1010/*
1011 * NetBeans and Vim have different undo models. In Vim, the file isn't
1012 * changed if changes are undone via the undo command. In NetBeans, once
1013 * a change has been made the file is marked as modified until saved. It
1014 * doesn't matter if the change was undone.
1015 *
1016 * So this function is for the corner case where Vim thinks a buffer is
1017 * unmodified but NetBeans thinks it IS modified.
1018 */
1019 int
1020isNetbeansModified(buf_T *bufp)
1021{
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001022 if (isNetbeansBuffer(bufp))
Bram Moolenaar009b2592004-10-24 19:18:58 +00001023 {
1024 int bufno = nb_getbufno(bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025
Bram Moolenaar009b2592004-10-24 19:18:58 +00001026 if (bufno > 0)
1027 return buf_list[bufno].modified;
1028 else
1029 return FALSE;
1030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 else
1032 return FALSE;
1033}
1034
1035/*
1036 * Given a Netbeans buffer number, return the netbeans buffer.
1037 * Returns NULL for 0 or a negative number. A 0 bufno means a
1038 * non-buffer related command has been sent.
1039 */
1040 static nbbuf_T *
1041nb_get_buf(int bufno)
1042{
1043 /* find or create a buffer with the given number */
1044 int incr;
1045
1046 if (bufno <= 0)
1047 return NULL;
1048
1049 if (!buf_list)
1050 {
1051 /* initialize */
1052 buf_list = (nbbuf_T *)alloc_clear(100 * sizeof(nbbuf_T));
1053 buf_list_size = 100;
1054 }
1055 if (bufno >= buf_list_used) /* new */
1056 {
1057 if (bufno >= buf_list_size) /* grow list */
1058 {
1059 incr = bufno - buf_list_size + 90;
1060 buf_list_size += incr;
1061 buf_list = (nbbuf_T *)vim_realloc(
1062 buf_list, buf_list_size * sizeof(nbbuf_T));
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02001063 vim_memset(buf_list + buf_list_size - incr, 0,
1064 incr * sizeof(nbbuf_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 }
1066
1067 while (buf_list_used <= bufno)
1068 {
1069 /* Default is to fire text changes. */
1070 buf_list[buf_list_used].fireChanges = 1;
1071 ++buf_list_used;
1072 }
1073 }
1074
1075 return buf_list + bufno;
1076}
1077
1078/*
1079 * Return the number of buffers that are modified.
1080 */
1081 static int
1082count_changed_buffers(void)
1083{
1084 buf_T *bufp;
1085 int n;
1086
1087 n = 0;
1088 for (bufp = firstbuf; bufp != NULL; bufp = bufp->b_next)
1089 if (bufp->b_changed)
1090 ++n;
1091 return n;
1092}
1093
1094/*
1095 * End the netbeans session.
1096 */
1097 void
1098netbeans_end(void)
1099{
1100 int i;
1101 static char buf[128];
1102
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001103 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 return;
1105
1106 for (i = 0; i < buf_list_used; i++)
1107 {
1108 if (!buf_list[i].bufp)
1109 continue;
1110 if (netbeansForcedQuit)
1111 {
1112 /* mark as unmodified so NetBeans won't put up dialog on "killed" */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001113 sprintf(buf, "%d:unmodified=%d\n", i, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 nbdebug(("EVT: %s", buf));
1115 nb_send(buf, "netbeans_end");
1116 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001117 sprintf(buf, "%d:killed=%d\n", i, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 nbdebug(("EVT: %s", buf));
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001119 /* nb_send(buf, "netbeans_end"); avoid "write failed" messages */
1120 ignored = sock_write(nbsock, buf, (int)STRLEN(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122}
1123
1124/*
1125 * Send a message to netbeans.
1126 */
1127 static void
1128nb_send(char *buf, char *fun)
1129{
1130 /* Avoid giving pages full of error messages when the other side has
1131 * exited, only mention the first error until the connection works again. */
1132 static int did_error = FALSE;
1133
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001134 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 {
1136 if (!did_error)
Bram Moolenaarf2330482008-06-24 20:19:36 +00001137 {
1138 nbdebug((" %s(): write while not connected\n", fun));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 EMSG2("E630: %s(): write while not connected", fun);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 did_error = TRUE;
1142 }
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001143 else if (sock_write(nbsock, buf, (int)STRLEN(buf)) != (int)STRLEN(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 {
1145 if (!did_error)
Bram Moolenaarf2330482008-06-24 20:19:36 +00001146 {
1147 nbdebug((" %s(): write failed\n", fun));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 EMSG2("E631: %s(): write failed", fun);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 did_error = TRUE;
1151 }
1152 else
1153 did_error = FALSE;
1154}
1155
1156/*
1157 * Some input received from netbeans requires a response. This function
1158 * handles a response with no information (except the command number).
1159 */
1160 static void
1161nb_reply_nil(int cmdno)
1162{
1163 char reply[32];
1164
Bram Moolenaar009b2592004-10-24 19:18:58 +00001165 nbdebug(("REP %d: <none>\n", cmdno));
1166
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 sprintf(reply, "%d\n", cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 nb_send(reply, "nb_reply_nil");
1169}
1170
1171
1172/*
1173 * Send a response with text.
1174 * "result" must have been quoted already (using nb_quote()).
1175 */
1176 static void
1177nb_reply_text(int cmdno, char_u *result)
1178{
1179 char_u *reply;
1180
Bram Moolenaar009b2592004-10-24 19:18:58 +00001181 nbdebug(("REP %d: %s\n", cmdno, (char *)result));
1182
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001183 reply = alloc((unsigned)STRLEN(result) + 32);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 sprintf((char *)reply, "%d %s\n", cmdno, (char *)result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 nb_send((char *)reply, "nb_reply_text");
1186
1187 vim_free(reply);
1188}
1189
1190
1191/*
1192 * Send a response with a number result code.
1193 */
1194 static void
1195nb_reply_nr(int cmdno, long result)
1196{
1197 char reply[32];
1198
Bram Moolenaar009b2592004-10-24 19:18:58 +00001199 nbdebug(("REP %d: %ld\n", cmdno, result));
1200
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 sprintf(reply, "%d %ld\n", cmdno, result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 nb_send(reply, "nb_reply_nr");
1203}
1204
1205
1206/*
1207 * Encode newline, ret, backslash, double quote for transmission to NetBeans.
1208 */
1209 static char_u *
1210nb_quote(char_u *txt)
1211{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001212 char_u *buf = alloc((unsigned)(2 * STRLEN(txt) + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 char_u *p = txt;
1214 char_u *q = buf;
1215
1216 if (buf == NULL)
1217 return NULL;
1218 for (; *p; p++)
1219 {
1220 switch (*p)
1221 {
1222 case '\"':
1223 case '\\':
1224 *q++ = '\\'; *q++ = *p; break;
1225 /* case '\t': */
1226 /* *q++ = '\\'; *q++ = 't'; break; */
1227 case '\n':
1228 *q++ = '\\'; *q++ = 'n'; break;
1229 case '\r':
1230 *q++ = '\\'; *q++ = 'r'; break;
1231 default:
1232 *q++ = *p;
1233 break;
1234 }
1235 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01001236 *q = '\0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237
1238 return buf;
1239}
1240
1241
1242/*
1243 * Remove top level double quotes; convert backslashed chars.
1244 * Returns an allocated string (NULL for failure).
1245 * If "endp" is not NULL it is set to the character after the terminating
1246 * quote.
1247 */
1248 static char *
1249nb_unquote(char_u *p, char_u **endp)
1250{
1251 char *result = 0;
1252 char *q;
1253 int done = 0;
1254
1255 /* result is never longer than input */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001256 result = (char *)alloc_clear((unsigned)STRLEN(p) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 if (result == NULL)
1258 return NULL;
1259
1260 if (*p++ != '"')
1261 {
1262 nbdebug(("nb_unquote called with string that doesn't start with a quote!: %s\n",
1263 p));
1264 result[0] = NUL;
1265 return result;
1266 }
1267
1268 for (q = result; !done && *p != NUL;)
1269 {
1270 switch (*p)
1271 {
1272 case '"':
1273 /*
1274 * Unbackslashed dquote marks the end, if first char was dquote.
1275 */
1276 done = 1;
1277 break;
1278
1279 case '\\':
1280 ++p;
1281 switch (*p)
1282 {
1283 case '\\': *q++ = '\\'; break;
1284 case 'n': *q++ = '\n'; break;
1285 case 't': *q++ = '\t'; break;
1286 case 'r': *q++ = '\r'; break;
1287 case '"': *q++ = '"'; break;
1288 case NUL: --p; break;
1289 /* default: skip over illegal chars */
1290 }
1291 ++p;
1292 break;
1293
1294 default:
1295 *q++ = *p++;
1296 }
1297 }
1298
1299 if (endp != NULL)
1300 *endp = p;
1301
1302 return result;
1303}
1304
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001305/*
1306 * Remove from "first" byte to "last" byte (inclusive), at line "lnum" of the
1307 * current buffer. Remove to end of line when "last" is MAXCOL.
1308 */
1309 static void
1310nb_partialremove(linenr_T lnum, colnr_T first, colnr_T last)
1311{
1312 char_u *oldtext, *newtext;
1313 int oldlen;
1314 int lastbyte = last;
1315
1316 oldtext = ml_get(lnum);
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001317 oldlen = (int)STRLEN(oldtext);
Bram Moolenaarb3c70982008-01-18 10:40:55 +00001318 if (first >= (colnr_T)oldlen || oldlen == 0) /* just in case */
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001319 return;
1320 if (lastbyte >= oldlen)
1321 lastbyte = oldlen - 1;
1322 newtext = alloc(oldlen - (int)(lastbyte - first));
1323 if (newtext != NULL)
1324 {
1325 mch_memmove(newtext, oldtext, first);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001326 STRMOVE(newtext + first, oldtext + lastbyte + 1);
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001327 nbdebug((" NEW LINE %d: %s\n", lnum, newtext));
1328 ml_replace(lnum, newtext, FALSE);
1329 }
1330}
1331
1332/*
1333 * Replace the "first" line with the concatenation of the "first" and
1334 * the "other" line. The "other" line is not removed.
1335 */
1336 static void
1337nb_joinlines(linenr_T first, linenr_T other)
1338{
1339 int len_first, len_other;
1340 char_u *p;
1341
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001342 len_first = (int)STRLEN(ml_get(first));
1343 len_other = (int)STRLEN(ml_get(other));
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001344 p = alloc((unsigned)(len_first + len_other + 1));
1345 if (p != NULL)
1346 {
1347 mch_memmove(p, ml_get(first), len_first);
1348 mch_memmove(p + len_first, ml_get(other), len_other + 1);
1349 ml_replace(first, p, FALSE);
1350 }
1351}
1352
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353#define SKIP_STOP 2
1354#define streq(a,b) (strcmp(a,b) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355
1356/*
1357 * Do the actual processing of a single netbeans command or function.
Bram Moolenaar143c38c2007-05-10 16:41:10 +00001358 * The difference between a command and function is that a function
1359 * gets a response (it's required) but a command does not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 * For arguments see comment for nb_parse_cmd().
1361 */
1362 static int
1363nb_do_cmd(
1364 int bufno,
1365 char_u *cmd,
1366 int func,
1367 int cmdno,
1368 char_u *args) /* points to space before arguments or NUL */
1369{
1370 int doupdate = 0;
1371 long off = 0;
1372 nbbuf_T *buf = nb_get_buf(bufno);
1373 static int skip = 0;
1374 int retval = OK;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001375 char *cp; /* for when a char pointer is needed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376
1377 nbdebug(("%s %d: (%d) %s %s\n", (func) ? "FUN" : "CMD", cmdno, bufno, cmd,
1378 STRCMP(cmd, "insert") == 0 ? "<text>" : (char *)args));
1379
1380 if (func)
1381 {
1382/* =====================================================================*/
1383 if (streq((char *)cmd, "getModified"))
1384 {
1385 if (buf == NULL || buf->bufp == NULL)
1386 /* Return the number of buffers that are modified. */
1387 nb_reply_nr(cmdno, (long)count_changed_buffers());
1388 else
1389 /* Return whether the buffer is modified. */
1390 nb_reply_nr(cmdno, (long)(buf->bufp->b_changed
1391 || isNetbeansModified(buf->bufp)));
1392/* =====================================================================*/
1393 }
1394 else if (streq((char *)cmd, "saveAndExit"))
1395 {
1396 /* Note: this will exit Vim if successful. */
1397 coloncmd(":confirm qall");
1398
1399 /* We didn't exit: return the number of changed buffers. */
1400 nb_reply_nr(cmdno, (long)count_changed_buffers());
1401/* =====================================================================*/
1402 }
1403 else if (streq((char *)cmd, "getCursor"))
1404 {
1405 char_u text[200];
1406
1407 /* Note: nb_getbufno() may return -1. This indicates the IDE
1408 * didn't assign a number to the current buffer in response to a
1409 * fileOpened event. */
1410 sprintf((char *)text, "%d %ld %d %ld",
1411 nb_getbufno(curbuf),
1412 (long)curwin->w_cursor.lnum,
1413 (int)curwin->w_cursor.col,
1414 pos2off(curbuf, &curwin->w_cursor));
1415 nb_reply_text(cmdno, text);
1416/* =====================================================================*/
1417 }
Bram Moolenaarc65c4912006-11-14 17:29:46 +00001418 else if (streq((char *)cmd, "getAnno"))
1419 {
1420 long linenum = 0;
1421#ifdef FEAT_SIGNS
1422 if (buf == NULL || buf->bufp == NULL)
1423 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001424 nbdebug((" Invalid buffer identifier in getAnno\n"));
1425 EMSG("E652: Invalid buffer identifier in getAnno");
Bram Moolenaarc65c4912006-11-14 17:29:46 +00001426 retval = FAIL;
1427 }
1428 else
1429 {
1430 int serNum;
1431
1432 cp = (char *)args;
1433 serNum = strtol(cp, &cp, 10);
1434 /* If the sign isn't found linenum will be zero. */
1435 linenum = (long)buf_findsign(buf->bufp, serNum);
1436 }
1437#endif
1438 nb_reply_nr(cmdno, linenum);
1439/* =====================================================================*/
1440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 else if (streq((char *)cmd, "getLength"))
1442 {
1443 long len = 0;
1444
1445 if (buf == NULL || buf->bufp == NULL)
1446 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001447 nbdebug((" invalid buffer identifier in getLength\n"));
1448 EMSG("E632: invalid buffer identifier in getLength");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 retval = FAIL;
1450 }
1451 else
1452 {
1453 len = get_buf_size(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 }
1455 nb_reply_nr(cmdno, len);
1456/* =====================================================================*/
1457 }
1458 else if (streq((char *)cmd, "getText"))
1459 {
1460 long len;
1461 linenr_T nlines;
1462 char_u *text = NULL;
1463 linenr_T lno = 1;
1464 char_u *p;
1465 char_u *line;
1466
1467 if (buf == NULL || buf->bufp == NULL)
1468 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001469 nbdebug((" invalid buffer identifier in getText\n"));
1470 EMSG("E633: invalid buffer identifier in getText");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 retval = FAIL;
1472 }
1473 else
1474 {
1475 len = get_buf_size(buf->bufp);
1476 nlines = buf->bufp->b_ml.ml_line_count;
1477 text = alloc((unsigned)((len > 0)
1478 ? ((len + nlines) * 2) : 4));
1479 if (text == NULL)
1480 {
1481 nbdebug((" nb_do_cmd: getText has null text field\n"));
1482 retval = FAIL;
1483 }
1484 else
1485 {
1486 p = text;
1487 *p++ = '\"';
1488 for (; lno <= nlines ; lno++)
1489 {
1490 line = nb_quote(ml_get_buf(buf->bufp, lno, FALSE));
1491 if (line != NULL)
1492 {
1493 STRCPY(p, line);
1494 p += STRLEN(line);
1495 *p++ = '\\';
1496 *p++ = 'n';
Bram Moolenaar009b2592004-10-24 19:18:58 +00001497 vim_free(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 }
1500 *p++ = '\"';
1501 *p = '\0';
1502 }
1503 }
1504 if (text == NULL)
1505 nb_reply_text(cmdno, (char_u *)"");
1506 else
1507 {
1508 nb_reply_text(cmdno, text);
1509 vim_free(text);
1510 }
1511/* =====================================================================*/
1512 }
1513 else if (streq((char *)cmd, "remove"))
1514 {
1515 long count;
1516 pos_T first, last;
1517 pos_T *pos;
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001518 pos_T *next;
1519 linenr_T del_from_lnum, del_to_lnum; /* lines to be deleted as a whole */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 int oldFire = netbeansFireChanges;
1521 int oldSuppress = netbeansSuppressNoLines;
1522 int wasChanged;
1523
1524 if (skip >= SKIP_STOP)
1525 {
1526 nbdebug((" Skipping %s command\n", (char *) cmd));
1527 nb_reply_nil(cmdno);
1528 return OK;
1529 }
1530
1531 if (buf == NULL || buf->bufp == NULL)
1532 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001533 nbdebug((" invalid buffer identifier in remove\n"));
1534 EMSG("E634: invalid buffer identifier in remove");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 retval = FAIL;
1536 }
1537 else
1538 {
1539 netbeansFireChanges = FALSE;
1540 netbeansSuppressNoLines = TRUE;
1541
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001542 nb_set_curbuf(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 wasChanged = buf->bufp->b_changed;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001544 cp = (char *)args;
1545 off = strtol(cp, &cp, 10);
1546 count = strtol(cp, &cp, 10);
1547 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 /* delete "count" chars, starting at "off" */
1549 pos = off2pos(buf->bufp, off);
1550 if (!pos)
1551 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001552 nbdebug((" !bad position\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 nb_reply_text(cmdno, (char_u *)"!bad position");
1554 netbeansFireChanges = oldFire;
1555 netbeansSuppressNoLines = oldSuppress;
1556 return FAIL;
1557 }
1558 first = *pos;
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001559 nbdebug((" FIRST POS: line %d, col %d\n",
1560 first.lnum, first.col));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 pos = off2pos(buf->bufp, off+count-1);
1562 if (!pos)
1563 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001564 nbdebug((" !bad count\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 nb_reply_text(cmdno, (char_u *)"!bad count");
1566 netbeansFireChanges = oldFire;
1567 netbeansSuppressNoLines = oldSuppress;
1568 return FAIL;
1569 }
1570 last = *pos;
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001571 nbdebug((" LAST POS: line %d, col %d\n",
1572 last.lnum, last.col));
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001573 del_from_lnum = first.lnum;
1574 del_to_lnum = last.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 doupdate = 1;
1576
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001577 /* Get the position of the first byte after the deleted
1578 * section. "next" is NULL when deleting to the end of the
1579 * file. */
1580 next = off2pos(buf->bufp, off + count);
1581
1582 /* Remove part of the first line. */
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001583 if (first.col != 0
1584 || (next != NULL && first.lnum == next->lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 {
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001586 if (first.lnum != last.lnum
1587 || (next != NULL && first.lnum != next->lnum))
1588 {
1589 /* remove to the end of the first line */
1590 nb_partialremove(first.lnum, first.col,
1591 (colnr_T)MAXCOL);
1592 if (first.lnum == last.lnum)
1593 {
1594 /* Partial line to remove includes the end of
1595 * line. Join the line with the next one, have
1596 * the next line deleted below. */
1597 nb_joinlines(first.lnum, next->lnum);
1598 del_to_lnum = next->lnum;
1599 }
1600 }
1601 else
1602 {
1603 /* remove within one line */
1604 nb_partialremove(first.lnum, first.col, last.col);
1605 }
1606 ++del_from_lnum; /* don't delete the first line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 }
1608
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001609 /* Remove part of the last line. */
1610 if (first.lnum != last.lnum && next != NULL
1611 && next->col != 0 && last.lnum == next->lnum)
1612 {
1613 nb_partialremove(last.lnum, 0, last.col);
1614 if (del_from_lnum > first.lnum)
1615 {
1616 /* Join end of last line to start of first line; last
1617 * line is deleted below. */
1618 nb_joinlines(first.lnum, last.lnum);
1619 }
1620 else
1621 /* First line is deleted as a whole, keep the last
1622 * line. */
1623 --del_to_lnum;
1624 }
1625
1626 /* First is partial line; last line to remove includes
1627 * the end of line; join first line to line following last
1628 * line; line following last line is deleted below. */
1629 if (first.lnum != last.lnum && del_from_lnum > first.lnum
1630 && next != NULL && last.lnum != next->lnum)
1631 {
1632 nb_joinlines(first.lnum, next->lnum);
1633 del_to_lnum = next->lnum;
1634 }
1635
1636 /* Delete whole lines if there are any. */
1637 if (del_to_lnum >= del_from_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 {
1639 int i;
1640
1641 /* delete signs from the lines being deleted */
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001642 for (i = del_from_lnum; i <= del_to_lnum; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 {
1644 int id = buf_findsign_id(buf->bufp, (linenr_T)i);
1645 if (id > 0)
1646 {
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001647 nbdebug((" Deleting sign %d on line %d\n",
1648 id, i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 buf_delsign(buf->bufp, id);
1650 }
1651 else
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001652 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 nbdebug((" No sign on line %d\n", i));
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 }
1656
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001657 nbdebug((" Deleting lines %d through %d\n",
1658 del_from_lnum, del_to_lnum));
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001659 curwin->w_cursor.lnum = del_from_lnum;
1660 curwin->w_cursor.col = 0;
1661 del_lines(del_to_lnum - del_from_lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 }
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001663
1664 /* Leave cursor at first deleted byte. */
1665 curwin->w_cursor = first;
1666 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 buf->bufp->b_changed = wasChanged; /* logically unchanged */
1668 netbeansFireChanges = oldFire;
1669 netbeansSuppressNoLines = oldSuppress;
1670
1671 u_blockfree(buf->bufp);
1672 u_clearall(buf->bufp);
1673 }
1674 nb_reply_nil(cmdno);
1675/* =====================================================================*/
1676 }
1677 else if (streq((char *)cmd, "insert"))
1678 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 char_u *to_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680
1681 if (skip >= SKIP_STOP)
1682 {
1683 nbdebug((" Skipping %s command\n", (char *) cmd));
1684 nb_reply_nil(cmdno);
1685 return OK;
1686 }
1687
1688 /* get offset */
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001689 cp = (char *)args;
1690 off = strtol(cp, &cp, 10);
1691 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692
1693 /* get text to be inserted */
1694 args = skipwhite(args);
1695 args = to_free = (char_u *)nb_unquote(args, NULL);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001696 /*
1697 nbdebug((" CHUNK[%d]: %d bytes at offset %d\n",
1698 buf->bufp->b_ml.ml_line_count, STRLEN(args), off));
1699 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700
1701 if (buf == NULL || buf->bufp == NULL)
1702 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001703 nbdebug((" invalid buffer identifier in insert\n"));
1704 EMSG("E635: invalid buffer identifier in insert");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 retval = FAIL;
1706 }
1707 else if (args != NULL)
1708 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001709 int ff_detected = EOL_UNKNOWN;
1710 int buf_was_empty = (buf->bufp->b_ml.ml_flags & ML_EMPTY);
1711 size_t len = 0;
1712 int added = 0;
1713 int oldFire = netbeansFireChanges;
1714 int old_b_changed;
1715 char_u *nl;
1716 linenr_T lnum;
1717 linenr_T lnum_start;
1718 pos_T *pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 netbeansFireChanges = 0;
1721
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001722 /* Jump to the buffer where we insert. After this "curbuf"
1723 * can be used. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001724 nb_set_curbuf(buf->bufp);
Bram Moolenaara3227e22006-03-08 21:32:40 +00001725 old_b_changed = curbuf->b_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001727 /* Convert the specified character offset into a lnum/col
1728 * position. */
Bram Moolenaara3227e22006-03-08 21:32:40 +00001729 pos = off2pos(curbuf, off);
1730 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001732 if (pos->lnum <= 0)
1733 lnum_start = 1;
1734 else
1735 lnum_start = pos->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 }
1737 else
1738 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001739 /* If the given position is not found, assume we want
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 * the end of the file. See setLocAndSize HACK. */
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001741 if (buf_was_empty)
1742 lnum_start = 1; /* above empty line */
1743 else
1744 lnum_start = curbuf->b_ml.ml_line_count + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001745 }
1746
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001747 /* "lnum" is the line where we insert: either append to it or
1748 * insert a new line above it. */
1749 lnum = lnum_start;
1750
1751 /* Loop over the "\n" separated lines of the argument. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 doupdate = 1;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001753 while (*args != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001755 nl = vim_strchr(args, '\n');
1756 if (nl == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001758 /* Incomplete line, probably truncated. Next "insert"
1759 * command should append to this one. */
1760 len = STRLEN(args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001762 else
1763 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001764 len = nl - args;
1765
1766 /*
1767 * We need to detect EOL style, because the commands
1768 * use a character offset.
1769 */
1770 if (nl > args && nl[-1] == '\r')
1771 {
1772 ff_detected = EOL_DOS;
1773 --len;
1774 }
1775 else
1776 ff_detected = EOL_UNIX;
1777 }
1778 args[len] = NUL;
1779
1780 if (lnum == lnum_start
1781 && ((pos != NULL && pos->col > 0)
1782 || (lnum == 1 && buf_was_empty)))
1783 {
1784 char_u *oldline = ml_get(lnum);
1785 char_u *newline;
1786
1787 /* Insert halfway a line. For simplicity we assume we
1788 * need to append to the line. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001789 newline = alloc_check((unsigned)(STRLEN(oldline) + len + 1));
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001790 if (newline != NULL)
1791 {
1792 STRCPY(newline, oldline);
1793 STRCAT(newline, args);
1794 ml_replace(lnum, newline, FALSE);
1795 }
1796 }
1797 else
1798 {
1799 /* Append a new line. Not that we always do this,
1800 * also when the text doesn't end in a "\n". */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001801 ml_append((linenr_T)(lnum - 1), args, (colnr_T)(len + 1), FALSE);
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001802 ++added;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001803 }
1804
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001805 if (nl == NULL)
1806 break;
1807 ++lnum;
1808 args = nl + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001809 }
1810
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001811 /* Adjust the marks below the inserted lines. */
1812 appended_lines_mark(lnum_start - 1, (long)added);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001814 /*
1815 * When starting with an empty buffer set the fileformat.
1816 * This is just guessing...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 */
1818 if (buf_was_empty)
1819 {
1820 if (ff_detected == EOL_UNKNOWN)
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001821#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 ff_detected = EOL_DOS;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001823#else
1824 ff_detected = EOL_UNIX;
1825#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 set_fileformat(ff_detected, OPT_LOCAL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00001827 curbuf->b_start_ffc = *curbuf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 }
1829
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 /*
1831 * XXX - GRP - Is the next line right? If I've inserted
1832 * text the buffer has been updated but not written. Will
1833 * netbeans guarantee to write it? Even if I do a :q! ?
1834 */
Bram Moolenaara3227e22006-03-08 21:32:40 +00001835 curbuf->b_changed = old_b_changed; /* logically unchanged */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 netbeansFireChanges = oldFire;
1837
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001838 /* Undo info is invalid now... */
Bram Moolenaara3227e22006-03-08 21:32:40 +00001839 u_blockfree(curbuf);
1840 u_clearall(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 }
1842 vim_free(to_free);
1843 nb_reply_nil(cmdno); /* or !error */
1844 }
1845 else
1846 {
1847 nbdebug(("UNIMPLEMENTED FUNCTION: %s\n", cmd));
1848 nb_reply_nil(cmdno);
1849 retval = FAIL;
1850 }
1851 }
1852 else /* Not a function; no reply required. */
1853 {
1854/* =====================================================================*/
1855 if (streq((char *)cmd, "create"))
1856 {
1857 /* Create a buffer without a name. */
1858 if (buf == NULL)
1859 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001860 nbdebug((" invalid buffer identifier in create\n"));
1861 EMSG("E636: invalid buffer identifier in create");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 return FAIL;
1863 }
1864 vim_free(buf->displayname);
1865 buf->displayname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866
1867 netbeansReadFile = 0; /* don't try to open disk file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001868 do_ecmd(0, NULL, 0, 0, ECMD_ONE, ECMD_HIDE + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 netbeansReadFile = 1;
1870 buf->bufp = curbuf;
1871 maketitle();
Bram Moolenaar009b2592004-10-24 19:18:58 +00001872 buf->insertDone = FALSE;
Bram Moolenaar67c53842010-05-22 18:28:27 +02001873#if defined(FEAT_MENU) && defined(FEAT_GUI)
Bram Moolenaard54a6882010-08-09 22:49:00 +02001874 if (gui.in_use)
1875 gui_update_menus(0);
Bram Moolenaar67c53842010-05-22 18:28:27 +02001876#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877/* =====================================================================*/
1878 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001879 else if (streq((char *)cmd, "insertDone"))
1880 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001881 if (buf == NULL || buf->bufp == NULL)
1882 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001883 nbdebug((" invalid buffer identifier in insertDone\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001884 }
1885 else
1886 {
1887 buf->bufp->b_start_eol = *args == 'T';
1888 buf->insertDone = TRUE;
1889 args += 2;
1890 buf->bufp->b_p_ro = *args == 'T';
1891 print_read_msg(buf);
1892 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001893/* =====================================================================*/
1894 }
1895 else if (streq((char *)cmd, "saveDone"))
1896 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001897 long savedChars = atol((char *)args);
1898
1899 if (buf == NULL || buf->bufp == NULL)
1900 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001901 nbdebug((" invalid buffer identifier in saveDone\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001902 }
1903 else
1904 print_save_msg(buf, savedChars);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001905/* =====================================================================*/
1906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 else if (streq((char *)cmd, "startDocumentListen"))
1908 {
1909 if (buf == NULL)
1910 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001911 nbdebug((" invalid buffer identifier in startDocumentListen\n"));
1912 EMSG("E637: invalid buffer identifier in startDocumentListen");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 return FAIL;
1914 }
1915 buf->fireChanges = 1;
1916/* =====================================================================*/
1917 }
1918 else if (streq((char *)cmd, "stopDocumentListen"))
1919 {
1920 if (buf == NULL)
1921 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001922 nbdebug((" invalid buffer identifier in stopDocumentListen\n"));
1923 EMSG("E638: invalid buffer identifier in stopDocumentListen");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 return FAIL;
1925 }
1926 buf->fireChanges = 0;
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001927 if (buf->bufp != NULL && buf->bufp->b_was_netbeans_file)
Bram Moolenaar009b2592004-10-24 19:18:58 +00001928 {
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001929 if (!buf->bufp->b_netbeans_file)
Bram Moolenaarf2330482008-06-24 20:19:36 +00001930 {
1931 nbdebug(("E658: NetBeans connection lost for buffer %ld\n", buf->bufp->b_fnum));
Bram Moolenaar009b2592004-10-24 19:18:58 +00001932 EMSGN(_("E658: NetBeans connection lost for buffer %ld"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 buf->bufp->b_fnum);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001934 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001935 else
1936 {
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001937 /* NetBeans uses stopDocumentListen when it stops editing
1938 * a file. It then expects the buffer in Vim to
1939 * disappear. */
1940 do_bufdel(DOBUF_DEL, (char_u *)"", 1,
1941 buf->bufp->b_fnum, buf->bufp->b_fnum, TRUE);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001942 vim_memset(buf, 0, sizeof(nbbuf_T));
1943 }
1944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945/* =====================================================================*/
1946 }
1947 else if (streq((char *)cmd, "setTitle"))
1948 {
1949 if (buf == NULL)
1950 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001951 nbdebug((" invalid buffer identifier in setTitle\n"));
1952 EMSG("E639: invalid buffer identifier in setTitle");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 return FAIL;
1954 }
1955 vim_free(buf->displayname);
1956 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957/* =====================================================================*/
1958 }
1959 else if (streq((char *)cmd, "initDone"))
1960 {
1961 if (buf == NULL || buf->bufp == NULL)
1962 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001963 nbdebug((" invalid buffer identifier in initDone\n"));
1964 EMSG("E640: invalid buffer identifier in initDone");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 return FAIL;
1966 }
1967 doupdate = 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001968 buf->initDone = TRUE;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001969 nb_set_curbuf(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970#if defined(FEAT_AUTOCMD)
1971 apply_autocmds(EVENT_BUFREADPOST, 0, 0, FALSE, buf->bufp);
1972#endif
1973
1974 /* handle any postponed key commands */
1975 handle_key_queue();
1976/* =====================================================================*/
1977 }
1978 else if (streq((char *)cmd, "setBufferNumber")
1979 || streq((char *)cmd, "putBufferNumber"))
1980 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00001981 char_u *path;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 buf_T *bufp;
1983
1984 if (buf == NULL)
1985 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001986 nbdebug((" invalid buffer identifier in setBufferNumber\n"));
1987 EMSG("E641: invalid buffer identifier in setBufferNumber");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 return FAIL;
1989 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001990 path = (char_u *)nb_unquote(args, NULL);
1991 if (path == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 return FAIL;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001993 bufp = buflist_findname(path);
1994 vim_free(path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 if (bufp == NULL)
1996 {
Bram Moolenaarec906222009-02-21 21:14:00 +00001997 nbdebug((" File %s not found in setBufferNumber\n", args));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 EMSG2("E642: File %s not found in setBufferNumber", args);
1999 return FAIL;
2000 }
2001 buf->bufp = bufp;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002002 buf->nbbuf_number = bufp->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003
2004 /* "setBufferNumber" has the side effect of jumping to the buffer
2005 * (don't know why!). Don't do that for "putBufferNumber". */
2006 if (*cmd != 'p')
2007 coloncmd(":buffer %d", bufp->b_fnum);
2008 else
2009 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002010 buf->initDone = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011
2012 /* handle any postponed key commands */
2013 handle_key_queue();
2014 }
2015
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016/* =====================================================================*/
2017 }
2018 else if (streq((char *)cmd, "setFullName"))
2019 {
2020 if (buf == NULL)
2021 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002022 nbdebug((" invalid buffer identifier in setFullName\n"));
2023 EMSG("E643: invalid buffer identifier in setFullName");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 return FAIL;
2025 }
2026 vim_free(buf->displayname);
2027 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028
2029 netbeansReadFile = 0; /* don't try to open disk file */
2030 do_ecmd(0, (char_u *)buf->displayname, 0, 0, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002031 ECMD_HIDE + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 netbeansReadFile = 1;
2033 buf->bufp = curbuf;
2034 maketitle();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002035#if defined(FEAT_MENU) && defined(FEAT_GUI)
Bram Moolenaard54a6882010-08-09 22:49:00 +02002036 if (gui.in_use)
2037 gui_update_menus(0);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039/* =====================================================================*/
2040 }
2041 else if (streq((char *)cmd, "editFile"))
2042 {
2043 if (buf == NULL)
2044 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002045 nbdebug((" invalid buffer identifier in editFile\n"));
2046 EMSG("E644: invalid buffer identifier in editFile");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 return FAIL;
2048 }
2049 /* Edit a file: like create + setFullName + read the file. */
2050 vim_free(buf->displayname);
2051 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 do_ecmd(0, (char_u *)buf->displayname, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002053 ECMD_HIDE + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 buf->bufp = curbuf;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002055 buf->initDone = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 doupdate = 1;
2057#if defined(FEAT_TITLE)
2058 maketitle();
2059#endif
Bram Moolenaar67c53842010-05-22 18:28:27 +02002060#if defined(FEAT_MENU) && defined(FEAT_GUI)
Bram Moolenaard54a6882010-08-09 22:49:00 +02002061 if (gui.in_use)
2062 gui_update_menus(0);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002063#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064/* =====================================================================*/
2065 }
2066 else if (streq((char *)cmd, "setVisible"))
2067 {
2068 if (buf == NULL || buf->bufp == NULL)
2069 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002070 nbdebug((" invalid buffer identifier in setVisible\n"));
2071 /* This message was commented out, probably because it can
2072 * happen when shutting down. */
2073 if (p_verbose > 0)
2074 EMSG("E645: invalid buffer identifier in setVisible");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 return FAIL;
2076 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002077 if (streq((char *)args, "T") && buf->bufp != curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 {
2079 exarg_T exarg;
2080 exarg.cmd = (char_u *)"goto";
2081 exarg.forceit = FALSE;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002082 dosetvisible = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 goto_buffer(&exarg, DOBUF_FIRST, FORWARD, buf->bufp->b_fnum);
2084 doupdate = 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002085 dosetvisible = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086
Bram Moolenaar67c53842010-05-22 18:28:27 +02002087#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 /* Side effect!!!. */
Bram Moolenaard54a6882010-08-09 22:49:00 +02002089 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 gui_mch_set_foreground();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002091#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093/* =====================================================================*/
2094 }
2095 else if (streq((char *)cmd, "raise"))
2096 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002097#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 /* Bring gvim to the foreground. */
Bram Moolenaard54a6882010-08-09 22:49:00 +02002099 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 gui_mch_set_foreground();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002101#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102/* =====================================================================*/
2103 }
2104 else if (streq((char *)cmd, "setModified"))
2105 {
Bram Moolenaarff064e12008-06-09 13:10:45 +00002106 int prev_b_changed;
2107
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 if (buf == NULL || buf->bufp == NULL)
2109 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002110 nbdebug((" invalid buffer identifier in setModified\n"));
2111 /* This message was commented out, probably because it can
2112 * happen when shutting down. */
2113 if (p_verbose > 0)
2114 EMSG("E646: invalid buffer identifier in setModified");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 return FAIL;
2116 }
Bram Moolenaarff064e12008-06-09 13:10:45 +00002117 prev_b_changed = buf->bufp->b_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 if (streq((char *)args, "T"))
Bram Moolenaarff064e12008-06-09 13:10:45 +00002119 buf->bufp->b_changed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 else
2121 {
2122 struct stat st;
2123
2124 /* Assume NetBeans stored the file. Reset the timestamp to
2125 * avoid "file changed" warnings. */
2126 if (buf->bufp->b_ffname != NULL
2127 && mch_stat((char *)buf->bufp->b_ffname, &st) >= 0)
2128 buf_store_time(buf->bufp, &st, buf->bufp->b_ffname);
Bram Moolenaarff064e12008-06-09 13:10:45 +00002129 buf->bufp->b_changed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 }
2131 buf->modified = buf->bufp->b_changed;
Bram Moolenaarff064e12008-06-09 13:10:45 +00002132 if (prev_b_changed != buf->bufp->b_changed)
2133 {
2134#ifdef FEAT_WINDOWS
2135 check_status(buf->bufp);
2136 redraw_tabline = TRUE;
2137#endif
2138#ifdef FEAT_TITLE
2139 maketitle();
2140#endif
2141 update_screen(0);
2142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143/* =====================================================================*/
2144 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002145 else if (streq((char *)cmd, "setModtime"))
2146 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002147 if (buf == NULL || buf->bufp == NULL)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002148 nbdebug((" invalid buffer identifier in setModtime\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002149 else
2150 buf->bufp->b_mtime = atoi((char *)args);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002151/* =====================================================================*/
2152 }
2153 else if (streq((char *)cmd, "setReadOnly"))
2154 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002155 if (buf == NULL || buf->bufp == NULL)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002156 nbdebug((" invalid buffer identifier in setReadOnly\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002157 else if (streq((char *)args, "T"))
Bram Moolenaar009b2592004-10-24 19:18:58 +00002158 buf->bufp->b_p_ro = TRUE;
2159 else
2160 buf->bufp->b_p_ro = FALSE;
2161/* =====================================================================*/
2162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 else if (streq((char *)cmd, "setMark"))
2164 {
2165 /* not yet */
2166/* =====================================================================*/
2167 }
2168 else if (streq((char *)cmd, "showBalloon"))
2169 {
2170#if defined(FEAT_BEVAL)
2171 static char *text = NULL;
2172
2173 /*
2174 * Set up the Balloon Expression Evaluation area.
2175 * Ignore 'ballooneval' here.
2176 * The text pointer must remain valid for a while.
2177 */
2178 if (balloonEval != NULL)
2179 {
2180 vim_free(text);
2181 text = nb_unquote(args, NULL);
2182 if (text != NULL)
2183 gui_mch_post_balloon(balloonEval, (char_u *)text);
2184 }
2185#endif
2186/* =====================================================================*/
2187 }
2188 else if (streq((char *)cmd, "setDot"))
2189 {
2190 pos_T *pos;
2191#ifdef NBDEBUG
2192 char_u *s;
2193#endif
2194
2195 if (buf == NULL || buf->bufp == NULL)
2196 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002197 nbdebug((" invalid buffer identifier in setDot\n"));
2198 EMSG("E647: invalid buffer identifier in setDot");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 return FAIL;
2200 }
2201
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002202 nb_set_curbuf(buf->bufp);
2203
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204#ifdef FEAT_VISUAL
2205 /* Don't want Visual mode now. */
2206 if (VIsual_active)
2207 end_visual_mode();
2208#endif
2209#ifdef NBDEBUG
2210 s = args;
2211#endif
2212 pos = get_off_or_lnum(buf->bufp, &args);
2213 if (pos)
2214 {
2215 curwin->w_cursor = *pos;
2216 check_cursor();
2217#ifdef FEAT_FOLDING
2218 foldOpenCursor();
2219#endif
2220 }
2221 else
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002222 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 nbdebug((" BAD POSITION in setDot: %s\n", s));
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225
2226 /* gui_update_cursor(TRUE, FALSE); */
2227 /* update_curbuf(NOT_VALID); */
2228 update_topline(); /* scroll to show the line */
2229 update_screen(VALID);
2230 setcursor();
2231 out_flush();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002232#ifdef FEAT_GUI
Bram Moolenaard54a6882010-08-09 22:49:00 +02002233 if (gui.in_use)
2234 {
2235 gui_update_cursor(TRUE, FALSE);
2236 gui_mch_flush();
2237 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02002238#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 /* Quit a hit-return or more prompt. */
2240 if (State == HITRETURN || State == ASKMORE)
2241 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242#ifdef FEAT_GUI_GTK
Bram Moolenaard54a6882010-08-09 22:49:00 +02002243 if (gui.in_use && gtk_main_level() > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 gtk_main_quit();
2245#endif
2246 }
2247/* =====================================================================*/
2248 }
2249 else if (streq((char *)cmd, "close"))
2250 {
2251#ifdef NBDEBUG
2252 char *name = "<NONE>";
2253#endif
2254
2255 if (buf == NULL)
2256 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002257 nbdebug((" invalid buffer identifier in close\n"));
2258 EMSG("E648: invalid buffer identifier in close");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 return FAIL;
2260 }
2261
2262#ifdef NBDEBUG
2263 if (buf->displayname != NULL)
2264 name = buf->displayname;
2265#endif
Bram Moolenaarf2330482008-06-24 20:19:36 +00002266 if (buf->bufp == NULL)
2267 {
2268 nbdebug((" invalid buffer identifier in close\n"));
2269 /* This message was commented out, probably because it can
2270 * happen when shutting down. */
2271 if (p_verbose > 0)
2272 EMSG("E649: invalid buffer identifier in close");
2273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 nbdebug((" CLOSE %d: %s\n", bufno, name));
Bram Moolenaar67c53842010-05-22 18:28:27 +02002275#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 need_mouse_correct = TRUE;
Bram Moolenaar67c53842010-05-22 18:28:27 +02002277#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 if (buf->bufp != NULL)
2279 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD,
2280 buf->bufp->b_fnum, TRUE);
Bram Moolenaar8d602722006-08-08 19:34:19 +00002281 buf->bufp = NULL;
2282 buf->initDone = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 doupdate = 1;
2284/* =====================================================================*/
2285 }
2286 else if (streq((char *)cmd, "setStyle")) /* obsolete... */
2287 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002288 nbdebug((" setStyle is obsolete!\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289/* =====================================================================*/
2290 }
2291 else if (streq((char *)cmd, "setExitDelay"))
2292 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002293 /* Only used in version 2.1. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294/* =====================================================================*/
2295 }
2296 else if (streq((char *)cmd, "defineAnnoType"))
2297 {
2298#ifdef FEAT_SIGNS
2299 int typeNum;
2300 char_u *typeName;
2301 char_u *tooltip;
2302 char_u *p;
2303 char_u *glyphFile;
Bram Moolenaar67c53842010-05-22 18:28:27 +02002304 int parse_error = FALSE;
2305 char_u *fg;
2306 char_u *bg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307
2308 if (buf == NULL)
2309 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002310 nbdebug((" invalid buffer identifier in defineAnnoType\n"));
2311 EMSG("E650: invalid buffer identifier in defineAnnoType");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 return FAIL;
2313 }
2314
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002315 cp = (char *)args;
2316 typeNum = strtol(cp, &cp, 10);
2317 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 args = skipwhite(args);
2319 typeName = (char_u *)nb_unquote(args, &args);
2320 args = skipwhite(args + 1);
2321 tooltip = (char_u *)nb_unquote(args, &args);
2322 args = skipwhite(args + 1);
2323
2324 p = (char_u *)nb_unquote(args, &args);
2325 glyphFile = vim_strsave_escaped(p, escape_chars);
2326 vim_free(p);
2327
2328 args = skipwhite(args + 1);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002329 p = skiptowhite(args);
2330 if (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002332 *p = NUL;
2333 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02002335 fg = vim_strsave(args);
2336 bg = vim_strsave(p);
2337 if (STRLEN(fg) > MAX_COLOR_LENGTH || STRLEN(bg) > MAX_COLOR_LENGTH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002339 EMSG("E532: highlighting color name too long in defineAnnoType");
2340 vim_free(typeName);
2341 parse_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02002343 else if (typeName != NULL && tooltip != NULL && glyphFile != NULL)
2344 addsigntype(buf, typeNum, typeName, tooltip, glyphFile, fg, bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 else
2346 vim_free(typeName);
2347
2348 /* don't free typeName; it's used directly in addsigntype() */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002349 vim_free(fg);
2350 vim_free(bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 vim_free(tooltip);
2352 vim_free(glyphFile);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002353 if (parse_error)
2354 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355
2356#endif
2357/* =====================================================================*/
2358 }
2359 else if (streq((char *)cmd, "addAnno"))
2360 {
2361#ifdef FEAT_SIGNS
2362 int serNum;
2363 int localTypeNum;
2364 int typeNum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 pos_T *pos;
2366
2367 if (buf == NULL || buf->bufp == NULL)
2368 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002369 nbdebug((" invalid buffer identifier in addAnno\n"));
2370 EMSG("E651: invalid buffer identifier in addAnno");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 return FAIL;
2372 }
2373
2374 doupdate = 1;
2375
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002376 cp = (char *)args;
2377 serNum = strtol(cp, &cp, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378
2379 /* Get the typenr specific for this buffer and convert it to
2380 * the global typenumber, as used for the sign name. */
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002381 localTypeNum = strtol(cp, &cp, 10);
2382 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 typeNum = mapsigntype(buf, localTypeNum);
2384
2385 pos = get_off_or_lnum(buf->bufp, &args);
2386
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002387 cp = (char *)args;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002388 ignored = (int)strtol(cp, &cp, 10);
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002389 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390# ifdef NBDEBUG
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002391 if (ignored != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002393 nbdebug((" partial line annotation -- Not Yet Implemented!\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 }
2395# endif
2396 if (serNum >= GUARDEDOFFSET)
2397 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002398 nbdebug((" too many annotations! ignoring...\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 return FAIL;
2400 }
2401 if (pos)
2402 {
Bram Moolenaarec906222009-02-21 21:14:00 +00002403 coloncmd(":sign place %d line=%ld name=%d buffer=%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 serNum, pos->lnum, typeNum, buf->bufp->b_fnum);
2405 if (typeNum == curPCtype)
2406 coloncmd(":sign jump %d buffer=%d", serNum,
2407 buf->bufp->b_fnum);
2408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409#endif
2410/* =====================================================================*/
2411 }
2412 else if (streq((char *)cmd, "removeAnno"))
2413 {
2414#ifdef FEAT_SIGNS
2415 int serNum;
2416
2417 if (buf == NULL || buf->bufp == NULL)
2418 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002419 nbdebug((" invalid buffer identifier in removeAnno\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 return FAIL;
2421 }
2422 doupdate = 1;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002423 cp = (char *)args;
2424 serNum = strtol(cp, &cp, 10);
2425 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 coloncmd(":sign unplace %d buffer=%d",
2427 serNum, buf->bufp->b_fnum);
2428 redraw_buf_later(buf->bufp, NOT_VALID);
2429#endif
2430/* =====================================================================*/
2431 }
2432 else if (streq((char *)cmd, "moveAnnoToFront"))
2433 {
2434#ifdef FEAT_SIGNS
Bram Moolenaarf2330482008-06-24 20:19:36 +00002435 nbdebug((" moveAnnoToFront: Not Yet Implemented!\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436#endif
2437/* =====================================================================*/
2438 }
2439 else if (streq((char *)cmd, "guard") || streq((char *)cmd, "unguard"))
2440 {
2441 int len;
2442 pos_T first;
2443 pos_T last;
2444 pos_T *pos;
2445 int un = (cmd[0] == 'u');
2446 static int guardId = GUARDEDOFFSET;
2447
2448 if (skip >= SKIP_STOP)
2449 {
2450 nbdebug((" Skipping %s command\n", (char *) cmd));
2451 return OK;
2452 }
2453
2454 nb_init_graphics();
2455
2456 if (buf == NULL || buf->bufp == NULL)
2457 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002458 nbdebug((" invalid buffer identifier in %s command\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 return FAIL;
2460 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002461 nb_set_curbuf(buf->bufp);
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002462 cp = (char *)args;
2463 off = strtol(cp, &cp, 10);
2464 len = strtol(cp, NULL, 10);
2465 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 pos = off2pos(buf->bufp, off);
2467 doupdate = 1;
2468 if (!pos)
2469 nbdebug((" no such start pos in %s, %ld\n", cmd, off));
2470 else
2471 {
2472 first = *pos;
2473 pos = off2pos(buf->bufp, off + len - 1);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002474 if (pos != NULL && pos->col == 0)
2475 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 /*
2477 * In Java Swing the offset is a position between 2
2478 * characters. If col == 0 then we really want the
2479 * previous line as the end.
2480 */
2481 pos = off2pos(buf->bufp, off + len - 2);
2482 }
2483 if (!pos)
2484 nbdebug((" no such end pos in %s, %ld\n",
2485 cmd, off + len - 1));
2486 else
2487 {
2488 long lnum;
2489 last = *pos;
2490 /* set highlight for region */
2491 nbdebug((" %sGUARD %ld,%d to %ld,%d\n", (un) ? "UN" : "",
2492 first.lnum, first.col,
2493 last.lnum, last.col));
2494#ifdef FEAT_SIGNS
2495 for (lnum = first.lnum; lnum <= last.lnum; lnum++)
2496 {
2497 if (un)
2498 {
2499 /* never used */
2500 }
2501 else
2502 {
2503 if (buf_findsigntype_id(buf->bufp, lnum,
2504 GUARDED) == 0)
2505 {
2506 coloncmd(
Bram Moolenaarec906222009-02-21 21:14:00 +00002507 ":sign place %d line=%ld name=%d buffer=%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 guardId++, lnum, GUARDED,
2509 buf->bufp->b_fnum);
2510 }
2511 }
2512 }
2513#endif
2514 redraw_buf_later(buf->bufp, NOT_VALID);
2515 }
2516 }
2517/* =====================================================================*/
2518 }
2519 else if (streq((char *)cmd, "startAtomic"))
2520 {
2521 inAtomic = 1;
2522/* =====================================================================*/
2523 }
2524 else if (streq((char *)cmd, "endAtomic"))
2525 {
2526 inAtomic = 0;
2527 if (needupdate)
2528 {
2529 doupdate = 1;
2530 needupdate = 0;
2531 }
2532/* =====================================================================*/
2533 }
2534 else if (streq((char *)cmd, "save"))
2535 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002536 /*
2537 * NOTE - This command is obsolete wrt NetBeans. Its left in
2538 * only for historical reasons.
2539 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 if (buf == NULL || buf->bufp == NULL)
2541 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002542 nbdebug((" invalid buffer identifier in %s command\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 return FAIL;
2544 }
2545
2546 /* the following is taken from ex_cmds.c (do_wqall function) */
2547 if (bufIsChanged(buf->bufp))
2548 {
2549 /* Only write if the buffer can be written. */
2550 if (p_write
2551 && !buf->bufp->b_p_ro
2552 && buf->bufp->b_ffname != NULL
2553#ifdef FEAT_QUICKFIX
2554 && !bt_dontwrite(buf->bufp)
2555#endif
2556 )
2557 {
2558 buf_write_all(buf->bufp, FALSE);
2559#ifdef FEAT_AUTOCMD
2560 /* an autocommand may have deleted the buffer */
2561 if (!buf_valid(buf->bufp))
2562 buf->bufp = NULL;
2563#endif
2564 }
2565 }
Bram Moolenaarf2330482008-06-24 20:19:36 +00002566 else
2567 {
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002568 nbdebug((" Buffer has no changes!\n"));
Bram Moolenaarf2330482008-06-24 20:19:36 +00002569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570/* =====================================================================*/
2571 }
2572 else if (streq((char *)cmd, "netbeansBuffer"))
2573 {
2574 if (buf == NULL || buf->bufp == NULL)
2575 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002576 nbdebug((" invalid buffer identifier in %s command\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 return FAIL;
2578 }
2579 if (*args == 'T')
2580 {
2581 buf->bufp->b_netbeans_file = TRUE;
2582 buf->bufp->b_was_netbeans_file = TRUE;
2583 }
2584 else
2585 buf->bufp->b_netbeans_file = FALSE;
2586/* =====================================================================*/
2587 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002588 else if (streq((char *)cmd, "specialKeys"))
2589 {
2590 special_keys(args);
2591/* =====================================================================*/
2592 }
2593 else if (streq((char *)cmd, "actionMenuItem"))
2594 {
2595 /* not used yet */
2596/* =====================================================================*/
2597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 else if (streq((char *)cmd, "version"))
2599 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002600 /* not used yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 }
Bram Moolenaarf2330482008-06-24 20:19:36 +00002602 else
2603 {
2604 nbdebug(("Unrecognised command: %s\n", cmd));
2605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 /*
2607 * Unrecognized command is ignored.
2608 */
2609 }
2610 if (inAtomic && doupdate)
2611 {
2612 needupdate = 1;
2613 doupdate = 0;
2614 }
2615
Bram Moolenaar009b2592004-10-24 19:18:58 +00002616 /*
2617 * Is this needed? I moved the netbeans_Xt_connect() later during startup
2618 * and it may no longer be necessary. If its not needed then needupdate
2619 * and doupdate can also be removed.
2620 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 if (buf != NULL && buf->initDone && doupdate)
2622 {
2623 update_screen(NOT_VALID);
2624 setcursor();
2625 out_flush();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002626#ifdef FEAT_GUI
Bram Moolenaard54a6882010-08-09 22:49:00 +02002627 if (gui.in_use)
2628 {
2629 gui_update_cursor(TRUE, FALSE);
2630 gui_mch_flush();
2631 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02002632#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 /* Quit a hit-return or more prompt. */
2634 if (State == HITRETURN || State == ASKMORE)
2635 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636#ifdef FEAT_GUI_GTK
Bram Moolenaard54a6882010-08-09 22:49:00 +02002637 if (gui.in_use && gtk_main_level() > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 gtk_main_quit();
2639#endif
2640 }
2641 }
2642
2643 return retval;
2644}
2645
2646
2647/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002648 * If "buf" is not the current buffer try changing to a window that edits this
2649 * buffer. If there is no such window then close the current buffer and set
2650 * the current buffer as "buf".
2651 */
2652 static void
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00002653nb_set_curbuf(buf_T *buf)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002654{
2655 if (curbuf != buf && buf_jump_open_win(buf) == NULL)
2656 set_curbuf(buf, DOBUF_GOTO);
2657}
2658
2659/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 * Process a vim colon command.
2661 */
2662 static void
2663coloncmd(char *cmd, ...)
2664{
2665 char buf[1024];
2666 va_list ap;
2667
2668 va_start(ap, cmd);
Bram Moolenaar0dc79e82009-06-24 14:50:12 +00002669 vim_vsnprintf(buf, sizeof(buf), cmd, ap, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 va_end(ap);
2671
2672 nbdebug((" COLONCMD %s\n", buf));
2673
2674/* ALT_INPUT_LOCK_ON; */
2675 do_cmdline((char_u *)buf, NULL, NULL, DOCMD_NOWAIT | DOCMD_KEYTYPED);
2676/* ALT_INPUT_LOCK_OFF; */
2677
2678 setcursor(); /* restore the cursor position */
2679 out_flush(); /* make sure output has been written */
2680
Bram Moolenaar67c53842010-05-22 18:28:27 +02002681#ifdef FEAT_GUI
Bram Moolenaard54a6882010-08-09 22:49:00 +02002682 if (gui.in_use)
2683 {
2684 gui_update_cursor(TRUE, FALSE);
2685 gui_mch_flush();
2686 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02002687#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688}
2689
2690
2691/*
Bram Moolenaar009b2592004-10-24 19:18:58 +00002692 * Parse the specialKeys argument and issue the appropriate map commands.
2693 */
2694 static void
2695special_keys(char_u *args)
2696{
2697 char *save_str = nb_unquote(args, NULL);
2698 char *tok = strtok(save_str, " ");
2699 char *sep;
2700 char keybuf[64];
2701 char cmdbuf[256];
2702
2703 while (tok != NULL)
2704 {
2705 int i = 0;
2706
2707 if ((sep = strchr(tok, '-')) != NULL)
2708 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002709 *sep = NUL;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002710 while (*tok)
2711 {
2712 switch (*tok)
2713 {
2714 case 'A':
2715 case 'M':
2716 case 'C':
2717 case 'S':
2718 keybuf[i++] = *tok;
2719 keybuf[i++] = '-';
2720 break;
2721 }
2722 tok++;
2723 }
2724 tok++;
2725 }
2726
2727 strcpy(&keybuf[i], tok);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002728 vim_snprintf(cmdbuf, sizeof(cmdbuf),
2729 "<silent><%s> :nbkey %s<CR>", keybuf, keybuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002730 do_map(0, (char_u *)cmdbuf, NORMAL, FALSE);
2731 tok = strtok(NULL, " ");
2732 }
2733 vim_free(save_str);
2734}
2735
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002736 void
2737ex_nbclose(eap)
2738 exarg_T *eap UNUSED;
2739{
2740 netbeans_close();
2741}
Bram Moolenaar009b2592004-10-24 19:18:58 +00002742
2743 void
2744ex_nbkey(eap)
2745 exarg_T *eap;
2746{
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002747 (void)netbeans_keystring(eap->arg);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002748}
2749
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002750 void
2751ex_nbstart(eap)
2752 exarg_T *eap;
2753{
Bram Moolenaarc2a406b2010-09-30 21:03:26 +02002754#ifdef FEAT_GUI
2755# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
2756 && !defined(FEAT_GUI_W32)
2757 if (gui.in_use)
2758 {
2759 EMSG(_("E838: netbeans is not supported with this GUI"));
2760 return;
2761 }
2762# endif
2763#endif
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002764 netbeans_open((char *)eap->arg, FALSE);
2765}
Bram Moolenaar009b2592004-10-24 19:18:58 +00002766
2767/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 * Initialize highlights and signs for use by netbeans (mostly obsolete)
2769 */
2770 static void
2771nb_init_graphics(void)
2772{
2773 static int did_init = FALSE;
2774
2775 if (!did_init)
2776 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002777 coloncmd(":highlight NBGuarded guibg=Cyan guifg=Black"
2778 " ctermbg=LightCyan ctermfg=Black");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 coloncmd(":sign define %d linehl=NBGuarded", GUARDED);
2780
2781 did_init = TRUE;
2782 }
2783}
2784
2785/*
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002786 * Convert key to netbeans name. This uses the global "mod_mask".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 */
2788 static void
2789netbeans_keyname(int key, char *buf)
2790{
2791 char *name = 0;
2792 char namebuf[2];
2793 int ctrl = 0;
2794 int shift = 0;
2795 int alt = 0;
2796
2797 if (mod_mask & MOD_MASK_CTRL)
2798 ctrl = 1;
2799 if (mod_mask & MOD_MASK_SHIFT)
2800 shift = 1;
2801 if (mod_mask & MOD_MASK_ALT)
2802 alt = 1;
2803
2804
2805 switch (key)
2806 {
2807 case K_F1: name = "F1"; break;
2808 case K_S_F1: name = "F1"; shift = 1; break;
2809 case K_F2: name = "F2"; break;
2810 case K_S_F2: name = "F2"; shift = 1; break;
2811 case K_F3: name = "F3"; break;
2812 case K_S_F3: name = "F3"; shift = 1; break;
2813 case K_F4: name = "F4"; break;
2814 case K_S_F4: name = "F4"; shift = 1; break;
2815 case K_F5: name = "F5"; break;
2816 case K_S_F5: name = "F5"; shift = 1; break;
2817 case K_F6: name = "F6"; break;
2818 case K_S_F6: name = "F6"; shift = 1; break;
2819 case K_F7: name = "F7"; break;
2820 case K_S_F7: name = "F7"; shift = 1; break;
2821 case K_F8: name = "F8"; break;
2822 case K_S_F8: name = "F8"; shift = 1; break;
2823 case K_F9: name = "F9"; break;
2824 case K_S_F9: name = "F9"; shift = 1; break;
2825 case K_F10: name = "F10"; break;
2826 case K_S_F10: name = "F10"; shift = 1; break;
2827 case K_F11: name = "F11"; break;
2828 case K_S_F11: name = "F11"; shift = 1; break;
2829 case K_F12: name = "F12"; break;
2830 case K_S_F12: name = "F12"; shift = 1; break;
2831 default:
2832 if (key >= ' ' && key <= '~')
2833 {
2834 /* Allow ASCII characters. */
2835 name = namebuf;
2836 namebuf[0] = key;
2837 namebuf[1] = NUL;
2838 }
2839 else
2840 name = "X";
2841 break;
2842 }
2843
2844 buf[0] = '\0';
2845 if (ctrl)
2846 strcat(buf, "C");
2847 if (shift)
2848 strcat(buf, "S");
2849 if (alt)
2850 strcat(buf, "M"); /* META */
2851 if (ctrl || shift || alt)
2852 strcat(buf, "-");
2853 strcat(buf, name);
2854}
2855
Bram Moolenaar67c53842010-05-22 18:28:27 +02002856#if defined(FEAT_BEVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857/*
2858 * Function to be called for balloon evaluation. Grabs the text under the
2859 * cursor and sends it to the debugger for evaluation. The debugger should
2860 * respond with a showBalloon command when there is a useful result.
2861 */
Bram Moolenaard62bec82005-03-07 22:56:57 +00002862 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863netbeans_beval_cb(
2864 BalloonEval *beval,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002865 int state UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866{
Bram Moolenaard62bec82005-03-07 22:56:57 +00002867 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 char_u *text;
Bram Moolenaard62bec82005-03-07 22:56:57 +00002869 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 int col;
2871 char buf[MAXPATHL * 2 + 25];
2872 char_u *p;
2873
2874 /* Don't do anything when 'ballooneval' is off, messages scrolled the
2875 * windows up or we have no connection. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002876 if (!p_beval || msg_scrolled > 0 || !NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 return;
2878
Bram Moolenaard62bec82005-03-07 22:56:57 +00002879 if (get_beval_info(beval, TRUE, &wp, &lnum, &text, &col) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 {
2881 /* Send debugger request. Only when the text is of reasonable
2882 * length. */
2883 if (text != NULL && text[0] != NUL && STRLEN(text) < MAXPATHL)
2884 {
2885 p = nb_quote(text);
2886 if (p != NULL)
Bram Moolenaar009b2592004-10-24 19:18:58 +00002887 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002888 vim_snprintf(buf, sizeof(buf),
Bram Moolenaar89d40322006-08-29 15:30:07 +00002889 "0:balloonText=%d \"%s\"\n", r_cmdno, p);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002890 vim_free(p);
2891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 nbdebug(("EVT: %s", buf));
2893 nb_send(buf, "netbeans_beval_cb");
2894 }
2895 vim_free(text);
2896 }
2897}
2898#endif
2899
2900/*
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002901 * Return TRUE when the netbeans connection is closed.
2902 */
2903 int
2904netbeans_active(void)
2905{
2906 return NETBEANS_OPEN;
2907}
2908
2909/*
Bram Moolenaar67c53842010-05-22 18:28:27 +02002910 * Return netbeans file descriptor.
2911 */
2912 int
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002913netbeans_filedesc(void)
Bram Moolenaar67c53842010-05-22 18:28:27 +02002914{
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002915 return nbsock;
Bram Moolenaar67c53842010-05-22 18:28:27 +02002916}
2917
2918#if defined(FEAT_GUI) || defined(PROTO)
2919/*
2920 * Register our file descriptor with the gui event handling system.
2921 */
2922 void
2923netbeans_gui_register(void)
2924{
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002925 if (!NB_HAS_GUI || !NETBEANS_OPEN)
Bram Moolenaar67c53842010-05-22 18:28:27 +02002926 return;
2927
Bram Moolenaar173c9852010-09-29 17:27:01 +02002928# ifdef FEAT_GUI_X11
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002929 /* tell notifier we are interested in being called
2930 * when there is input on the editor connection socket
2931 */
2932 if (inputHandler == (XtInputId)NULL)
2933 inputHandler = XtAppAddInput((XtAppContext)app_context, nbsock,
2934 (XtPointer)(XtInputReadMask + XtInputExceptMask),
2935 messageFromNetbeans, NULL);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002936# else
2937# ifdef FEAT_GUI_GTK
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002938 /*
2939 * Tell gdk we are interested in being called when there
2940 * is input on the editor connection socket
2941 */
2942 if (inputHandler == 0)
2943 inputHandler = gdk_input_add((gint)nbsock, (GdkInputCondition)
2944 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
2945 messageFromNetbeans, NULL);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002946# else
2947# ifdef FEAT_GUI_W32
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002948 /*
2949 * Tell Windows we are interested in receiving message when there
2950 * is input on the editor connection socket
2951 */
2952 if (inputHandler == -1)
2953 inputHandler = WSAAsyncSelect(nbsock, s_hwnd, WM_NETBEANS, FD_READ);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002954# endif
2955# endif
2956# endif
Bram Moolenaar67c53842010-05-22 18:28:27 +02002957
2958# ifdef FEAT_BEVAL
2959 bevalServers |= BEVAL_NETBEANS;
2960# endif
2961}
2962#endif
2963
2964/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 * Tell netbeans that the window was opened, ready for commands.
2966 */
2967 void
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02002968netbeans_open(char *params, int doabort)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969{
2970 char *cmd = "0:startupDone=0\n";
2971
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002972 if (NETBEANS_OPEN)
2973 {
2974 EMSG(_("E511: netbeans already connected"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02002978 if (netbeans_connect(params, doabort) != OK)
Bram Moolenaar67c53842010-05-22 18:28:27 +02002979 return;
2980#ifdef FEAT_GUI
2981 netbeans_gui_register();
Bram Moolenaard62bec82005-03-07 22:56:57 +00002982#endif
2983
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 nbdebug(("EVT: %s", cmd));
2985 nb_send(cmd, "netbeans_startup_done");
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002986
2987 /* update the screen after having added the gutter */
2988 changed_window_setting();
2989 update_screen(CLEAR);
2990 setcursor();
2991 out_flush();
2992#ifdef FEAT_GUI
Bram Moolenaard54a6882010-08-09 22:49:00 +02002993 if (gui.in_use)
2994 {
2995 gui_update_cursor(TRUE, FALSE);
2996 gui_mch_flush();
2997 }
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002998#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999}
3000
Bram Moolenaar009b2592004-10-24 19:18:58 +00003001/*
3002 * Tell netbeans that we're exiting. This should be called right
3003 * before calling exit.
3004 */
3005 void
3006netbeans_send_disconnect()
3007{
3008 char buf[128];
3009
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003010 if (NETBEANS_OPEN)
Bram Moolenaar009b2592004-10-24 19:18:58 +00003011 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00003012 sprintf(buf, "0:disconnect=%d\n", r_cmdno);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003013 nbdebug(("EVT: %s", buf));
3014 nb_send(buf, "netbeans_disconnect");
3015 }
3016}
3017
Bram Moolenaar173c9852010-09-29 17:27:01 +02003018#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_W32) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019/*
3020 * Tell netbeans that the window was moved or resized.
3021 */
3022 void
3023netbeans_frame_moved(int new_x, int new_y)
3024{
3025 char buf[128];
3026
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003027 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 return;
3029
3030 sprintf(buf, "0:geometry=%d %d %d %d %d\n",
Bram Moolenaar89d40322006-08-29 15:30:07 +00003031 r_cmdno, (int)Columns, (int)Rows, new_x, new_y);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003032 /*nbdebug(("EVT: %s", buf)); happens too many times during a move */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 nb_send(buf, "netbeans_frame_moved");
3034}
3035#endif
3036
3037/*
Bram Moolenaar009b2592004-10-24 19:18:58 +00003038 * Tell netbeans the user opened or activated a file.
3039 */
3040 void
3041netbeans_file_activated(buf_T *bufp)
3042{
3043 int bufno = nb_getbufno(bufp);
3044 nbbuf_T *bp = nb_get_buf(bufno);
3045 char buffer[2*MAXPATHL];
3046 char_u *q;
3047
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003048 if (!NETBEANS_OPEN || !bufp->b_netbeans_file || dosetvisible)
Bram Moolenaar009b2592004-10-24 19:18:58 +00003049 return;
3050
3051 q = nb_quote(bufp->b_ffname);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003052 if (q == NULL || bp == NULL)
Bram Moolenaar009b2592004-10-24 19:18:58 +00003053 return;
3054
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003055 vim_snprintf(buffer, sizeof(buffer), "%d:fileOpened=%d \"%s\" %s %s\n",
Bram Moolenaar009b2592004-10-24 19:18:58 +00003056 bufno,
3057 bufno,
3058 (char *)q,
3059 "T", /* open in NetBeans */
3060 "F"); /* modified */
3061
3062 vim_free(q);
3063 nbdebug(("EVT: %s", buffer));
3064
3065 nb_send(buffer, "netbeans_file_opened");
3066}
3067
3068/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 * Tell netbeans the user opened a file.
3070 */
3071 void
Bram Moolenaar009b2592004-10-24 19:18:58 +00003072netbeans_file_opened(buf_T *bufp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073{
Bram Moolenaar009b2592004-10-24 19:18:58 +00003074 int bufno = nb_getbufno(bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 char buffer[2*MAXPATHL];
3076 char_u *q;
Bram Moolenaar009b2592004-10-24 19:18:58 +00003077 nbbuf_T *bp = nb_get_buf(nb_getbufno(bufp));
3078 int bnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003080 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 return;
3082
Bram Moolenaar009b2592004-10-24 19:18:58 +00003083 q = nb_quote(bufp->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 if (q == NULL)
3085 return;
Bram Moolenaar009b2592004-10-24 19:18:58 +00003086 if (bp != NULL)
3087 bnum = bufno;
3088 else
3089 bnum = 0;
3090
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003091 vim_snprintf(buffer, sizeof(buffer), "%d:fileOpened=%d \"%s\" %s %s\n",
Bram Moolenaar009b2592004-10-24 19:18:58 +00003092 bnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 0,
3094 (char *)q,
3095 "T", /* open in NetBeans */
3096 "F"); /* modified */
3097
3098 vim_free(q);
3099 nbdebug(("EVT: %s", buffer));
3100
3101 nb_send(buffer, "netbeans_file_opened");
Bram Moolenaar009b2592004-10-24 19:18:58 +00003102 if (p_acd && vim_chdirfile(bufp->b_ffname) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 shorten_fnames(TRUE);
3104}
3105
3106/*
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00003107 * Tell netbeans that a file was deleted or wiped out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 */
3109 void
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00003110netbeans_file_killed(buf_T *bufp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111{
3112 int bufno = nb_getbufno(bufp);
3113 nbbuf_T *nbbuf = nb_get_buf(bufno);
3114 char buffer[2*MAXPATHL];
3115
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003116 if (!NETBEANS_OPEN || bufno == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 return;
3118
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00003119 nbdebug(("netbeans_file_killed:\n"));
3120 nbdebug((" Killing bufno: %d", bufno));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121
Bram Moolenaar89d40322006-08-29 15:30:07 +00003122 sprintf(buffer, "%d:killed=%d\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123
3124 nbdebug(("EVT: %s", buffer));
3125
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00003126 nb_send(buffer, "netbeans_file_killed");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127
3128 if (nbbuf != NULL)
3129 nbbuf->bufp = NULL;
3130}
3131
3132/*
3133 * Get a pointer to the Netbeans buffer for Vim buffer "bufp".
3134 * Return NULL if there is no such buffer or changes are not to be reported.
3135 * Otherwise store the buffer number in "*bufnop".
3136 */
3137 static nbbuf_T *
3138nb_bufp2nbbuf_fire(buf_T *bufp, int *bufnop)
3139{
3140 int bufno;
3141 nbbuf_T *nbbuf;
3142
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003143 if (!NETBEANS_OPEN || !netbeansFireChanges)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 return NULL; /* changes are not reported at all */
3145
3146 bufno = nb_getbufno(bufp);
3147 if (bufno <= 0)
3148 return NULL; /* file is not known to NetBeans */
3149
3150 nbbuf = nb_get_buf(bufno);
3151 if (nbbuf != NULL && !nbbuf->fireChanges)
3152 return NULL; /* changes in this buffer are not reported */
3153
3154 *bufnop = bufno;
3155 return nbbuf;
3156}
3157
3158/*
3159 * Tell netbeans the user inserted some text.
3160 */
3161 void
3162netbeans_inserted(
3163 buf_T *bufp,
3164 linenr_T linenr,
3165 colnr_T col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 char_u *txt,
3167 int newlen)
3168{
3169 char_u *buf;
3170 int bufno;
3171 nbbuf_T *nbbuf;
3172 pos_T pos;
3173 long off;
3174 char_u *p;
3175 char_u *newtxt;
3176
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003177 if (!NETBEANS_OPEN)
3178 return;
3179
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3181 if (nbbuf == NULL)
3182 return;
3183
Bram Moolenaar009b2592004-10-24 19:18:58 +00003184 /* Don't mark as modified for initial read */
3185 if (nbbuf->insertDone)
3186 nbbuf->modified = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187
3188 pos.lnum = linenr;
3189 pos.col = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 off = pos2off(bufp, &pos);
3191
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 /* send the "insert" EVT */
3193 newtxt = alloc(newlen + 1);
Bram Moolenaarb6356332005-07-18 21:40:44 +00003194 vim_strncpy(newtxt, txt, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 p = nb_quote(newtxt);
3196 if (p != NULL)
3197 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00003198 buf = alloc(128 + 2*newlen);
Bram Moolenaar89d40322006-08-29 15:30:07 +00003199 sprintf((char *)buf, "%d:insert=%d %ld \"%s\"\n",
3200 bufno, r_cmdno, off, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 nbdebug(("EVT: %s", buf));
3202 nb_send((char *)buf, "netbeans_inserted");
Bram Moolenaar009b2592004-10-24 19:18:58 +00003203 vim_free(p);
3204 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 vim_free(newtxt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207}
3208
3209/*
3210 * Tell netbeans some bytes have been removed.
3211 */
3212 void
3213netbeans_removed(
3214 buf_T *bufp,
3215 linenr_T linenr,
3216 colnr_T col,
3217 long len)
3218{
3219 char_u buf[128];
3220 int bufno;
3221 nbbuf_T *nbbuf;
3222 pos_T pos;
3223 long off;
3224
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003225 if (!NETBEANS_OPEN)
3226 return;
3227
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3229 if (nbbuf == NULL)
3230 return;
3231
3232 if (len < 0)
3233 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00003234 nbdebug(("Negative len %ld in netbeans_removed()!\n", len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 return;
3236 }
3237
3238 nbbuf->modified = 1;
3239
3240 pos.lnum = linenr;
3241 pos.col = col;
3242
3243 off = pos2off(bufp, &pos);
3244
Bram Moolenaar89d40322006-08-29 15:30:07 +00003245 sprintf((char *)buf, "%d:remove=%d %ld %ld\n", bufno, r_cmdno, off, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 nbdebug(("EVT: %s", buf));
3247 nb_send((char *)buf, "netbeans_removed");
3248}
3249
3250/*
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003251 * Send netbeans an unmodified command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003254netbeans_unmodified(buf_T *bufp UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255{
Bram Moolenaar09092152010-08-08 16:38:42 +02003256 /* This is a no-op, because NetBeans considers a buffer modified
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 * even when all changes have been undone. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258}
3259
3260/*
3261 * Send a button release event back to netbeans. Its up to netbeans
3262 * to decide what to do (if anything) with this event.
3263 */
3264 void
3265netbeans_button_release(int button)
3266{
3267 char buf[128];
3268 int bufno;
3269
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003270 if (!NETBEANS_OPEN)
3271 return;
3272
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 bufno = nb_getbufno(curbuf);
3274
3275 if (bufno >= 0 && curwin != NULL && curwin->w_buffer == curbuf)
3276 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003277 int col = mouse_col - W_WINCOL(curwin)
Bram Moolenaar67c53842010-05-22 18:28:27 +02003278 - ((curwin->w_p_nu || curwin->w_p_rnu) ? 9 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 long off = pos2off(curbuf, &curwin->w_cursor);
3280
3281 /* sync the cursor position */
Bram Moolenaar89d40322006-08-29 15:30:07 +00003282 sprintf(buf, "%d:newDotAndMark=%d %ld %ld\n", bufno, r_cmdno, off, off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 nbdebug(("EVT: %s", buf));
3284 nb_send(buf, "netbeans_button_release[newDotAndMark]");
3285
Bram Moolenaar89d40322006-08-29 15:30:07 +00003286 sprintf(buf, "%d:buttonRelease=%d %d %ld %d\n", bufno, r_cmdno,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 button, (long)curwin->w_cursor.lnum, col);
3288 nbdebug(("EVT: %s", buf));
3289 nb_send(buf, "netbeans_button_release");
3290 }
3291}
3292
3293
3294/*
Bram Moolenaar143c38c2007-05-10 16:41:10 +00003295 * Send a keypress event back to netbeans. This usually simulates some
Bram Moolenaar009b2592004-10-24 19:18:58 +00003296 * kind of function key press. This function operates on a key code.
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003297 * Return TRUE when the key was sent, FALSE when the command has been
3298 * postponed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 */
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003300 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301netbeans_keycommand(int key)
3302{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 char keyName[60];
Bram Moolenaar009b2592004-10-24 19:18:58 +00003304
3305 netbeans_keyname(key, keyName);
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003306 return netbeans_keystring((char_u *)keyName);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003307}
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 string.
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003313 * Return TRUE when the key was sent, FALSE when the command has been
3314 * postponed.
Bram Moolenaar009b2592004-10-24 19:18:58 +00003315 */
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003316 static int
3317netbeans_keystring(char_u *keyName)
Bram Moolenaar009b2592004-10-24 19:18:58 +00003318{
3319 char buf[2*MAXPATHL];
3320 int bufno = nb_getbufno(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 long off;
3322 char_u *q;
3323
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003324 if (!NETBEANS_OPEN)
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003325 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 if (bufno == -1)
3328 {
3329 nbdebug(("got keycommand for non-NetBeans buffer, opening...\n"));
3330 q = curbuf->b_ffname == NULL ? (char_u *)""
3331 : nb_quote(curbuf->b_ffname);
3332 if (q == NULL)
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003333 return TRUE;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003334 vim_snprintf(buf, sizeof(buf), "0:fileOpened=%d \"%s\" %s %s\n", 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 q,
3336 "T", /* open in NetBeans */
3337 "F"); /* modified */
3338 if (curbuf->b_ffname != NULL)
3339 vim_free(q);
3340 nbdebug(("EVT: %s", buf));
3341 nb_send(buf, "netbeans_keycommand");
3342
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003343 postpone_keycommand(keyName);
3344 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 }
3346
3347 /* sync the cursor position */
3348 off = pos2off(curbuf, &curwin->w_cursor);
Bram Moolenaar89d40322006-08-29 15:30:07 +00003349 sprintf(buf, "%d:newDotAndMark=%d %ld %ld\n", bufno, r_cmdno, off, off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 nbdebug(("EVT: %s", buf));
3351 nb_send(buf, "netbeans_keycommand");
3352
3353 /* To work on Win32 you must apply patch to ExtEditor module
3354 * from ExtEdCaret.java.diff - make EVT_newDotAndMark handler
3355 * more synchronous
3356 */
3357
3358 /* now send keyCommand event */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003359 vim_snprintf(buf, sizeof(buf), "%d:keyCommand=%d \"%s\"\n",
Bram Moolenaar89d40322006-08-29 15:30:07 +00003360 bufno, r_cmdno, keyName);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 nbdebug(("EVT: %s", buf));
3362 nb_send(buf, "netbeans_keycommand");
3363
3364 /* New: do both at once and include the lnum/col. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003365 vim_snprintf(buf, sizeof(buf), "%d:keyAtPos=%d \"%s\" %ld %ld/%ld\n",
Bram Moolenaar89d40322006-08-29 15:30:07 +00003366 bufno, r_cmdno, keyName,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 off, (long)curwin->w_cursor.lnum, (long)curwin->w_cursor.col);
3368 nbdebug(("EVT: %s", buf));
3369 nb_send(buf, "netbeans_keycommand");
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003370 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371}
3372
3373
3374/*
3375 * Send a save event to netbeans.
3376 */
3377 void
3378netbeans_save_buffer(buf_T *bufp)
3379{
3380 char_u buf[64];
3381 int bufno;
3382 nbbuf_T *nbbuf;
3383
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003384 if (!NETBEANS_OPEN)
3385 return;
3386
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3388 if (nbbuf == NULL)
3389 return;
3390
3391 nbbuf->modified = 0;
3392
Bram Moolenaar89d40322006-08-29 15:30:07 +00003393 sprintf((char *)buf, "%d:save=%d\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 nbdebug(("EVT: %s", buf));
3395 nb_send((char *)buf, "netbeans_save_buffer");
3396}
3397
3398
3399/*
3400 * Send remove command to netbeans (this command has been turned off).
3401 */
3402 void
3403netbeans_deleted_all_lines(buf_T *bufp)
3404{
3405 char_u buf[64];
3406 int bufno;
3407 nbbuf_T *nbbuf;
3408
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003409 if (!NETBEANS_OPEN)
3410 return;
3411
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3413 if (nbbuf == NULL)
3414 return;
3415
Bram Moolenaar009b2592004-10-24 19:18:58 +00003416 /* Don't mark as modified for initial read */
3417 if (nbbuf->insertDone)
3418 nbbuf->modified = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419
Bram Moolenaar89d40322006-08-29 15:30:07 +00003420 sprintf((char *)buf, "%d:remove=%d 0 -1\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 nbdebug(("EVT(suppressed): %s", buf));
3422/* nb_send(buf, "netbeans_deleted_all_lines"); */
3423}
3424
3425
3426/*
3427 * See if the lines are guarded. The top and bot parameters are from
3428 * u_savecommon(), these are the line above the change and the line below the
3429 * change.
3430 */
3431 int
3432netbeans_is_guarded(linenr_T top, linenr_T bot)
3433{
3434 signlist_T *p;
3435 int lnum;
3436
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003437 if (!NETBEANS_OPEN)
3438 return FALSE;
3439
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 for (p = curbuf->b_signlist; p != NULL; p = p->next)
3441 if (p->id >= GUARDEDOFFSET)
3442 for (lnum = top + 1; lnum < bot; lnum++)
3443 if (lnum == p->lnum)
3444 return TRUE;
3445
3446 return FALSE;
3447}
3448
Bram Moolenaar173c9852010-09-29 17:27:01 +02003449#if defined(FEAT_GUI_X11) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450/*
3451 * We have multiple signs to draw at the same location. Draw the
3452 * multi-sign indicator instead. This is the Motif version.
3453 */
3454 void
3455netbeans_draw_multisign_indicator(int row)
3456{
3457 int i;
3458 int y;
3459 int x;
3460
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003461 if (!NETBEANS_OPEN)
3462 return;
3463
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 x = 0;
3465 y = row * gui.char_height + 2;
3466
3467 for (i = 0; i < gui.char_height - 3; i++)
3468 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y++);
3469
3470 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+0, y);
3471 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3472 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+4, y++);
3473 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+1, y);
3474 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3475 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+3, y++);
3476 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3477}
Bram Moolenaar173c9852010-09-29 17:27:01 +02003478#endif /* FEAT_GUI_X11 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479
Bram Moolenaar64404472010-06-26 06:24:45 +02003480#if defined(FEAT_GUI_GTK) && !defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481/*
3482 * We have multiple signs to draw at the same location. Draw the
3483 * multi-sign indicator instead. This is the GTK/Gnome version.
3484 */
3485 void
3486netbeans_draw_multisign_indicator(int row)
3487{
3488 int i;
3489 int y;
3490 int x;
3491 GdkDrawable *drawable = gui.drawarea->window;
3492
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003493 if (!NETBEANS_OPEN)
3494 return;
3495
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 x = 0;
3497 y = row * gui.char_height + 2;
3498
3499 for (i = 0; i < gui.char_height - 3; i++)
3500 gdk_draw_point(drawable, gui.text_gc, x+2, y++);
3501
3502 gdk_draw_point(drawable, gui.text_gc, x+0, y);
3503 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3504 gdk_draw_point(drawable, gui.text_gc, x+4, y++);
3505 gdk_draw_point(drawable, gui.text_gc, x+1, y);
3506 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3507 gdk_draw_point(drawable, gui.text_gc, x+3, y++);
3508 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3509}
3510#endif /* FEAT_GUI_GTK */
3511
3512/*
3513 * If the mouse is clicked in the gutter of a line with multiple
3514 * annotations, cycle through the set of signs.
3515 */
3516 void
3517netbeans_gutter_click(linenr_T lnum)
3518{
3519 signlist_T *p;
3520
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003521 if (!NETBEANS_OPEN)
3522 return;
3523
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 for (p = curbuf->b_signlist; p != NULL; p = p->next)
3525 {
3526 if (p->lnum == lnum && p->next && p->next->lnum == lnum)
3527 {
3528 signlist_T *tail;
3529
3530 /* remove "p" from list, reinsert it at the tail of the sublist */
3531 if (p->prev)
3532 p->prev->next = p->next;
3533 else
3534 curbuf->b_signlist = p->next;
3535 p->next->prev = p->prev;
3536 /* now find end of sublist and insert p */
3537 for (tail = p->next;
3538 tail->next && tail->next->lnum == lnum
3539 && tail->next->id < GUARDEDOFFSET;
3540 tail = tail->next)
3541 ;
3542 /* tail now points to last entry with same lnum (except
3543 * that "guarded" annotations are always last) */
3544 p->next = tail->next;
3545 if (tail->next)
3546 tail->next->prev = p;
3547 p->prev = tail;
3548 tail->next = p;
3549 update_debug_sign(curbuf, lnum);
3550 break;
3551 }
3552 }
3553}
3554
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555/*
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003556 * Add a sign of the requested type at the requested location.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 *
3558 * Reverse engineering:
3559 * Apparently an annotation is defined the first time it is used in a buffer.
3560 * When the same annotation is used in two buffers, the second time we do not
3561 * need to define a new sign name but reuse the existing one. But since the
3562 * ID number used in the second buffer starts counting at one again, a mapping
3563 * is made from the ID specifically for the buffer to the global sign name
3564 * (which is a number).
3565 *
3566 * globalsignmap[] stores the signs that have been defined globally.
3567 * buf->signmapused[] maps buffer-local annotation IDs to an index in
3568 * globalsignmap[].
3569 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 static void
3571addsigntype(
3572 nbbuf_T *buf,
3573 int typeNum,
3574 char_u *typeName,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003575 char_u *tooltip UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 char_u *glyphFile,
Bram Moolenaar67c53842010-05-22 18:28:27 +02003577 char_u *fg,
3578 char_u *bg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 int i, j;
Bram Moolenaar67c53842010-05-22 18:28:27 +02003581 int use_fg = (*fg && STRCMP(fg, "none") != 0);
3582 int use_bg = (*bg && STRCMP(bg, "none") != 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583
3584 for (i = 0; i < globalsignmapused; i++)
3585 if (STRCMP(typeName, globalsignmap[i]) == 0)
3586 break;
3587
3588 if (i == globalsignmapused) /* not found; add it to global map */
3589 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003590 nbdebug(("DEFINEANNOTYPE(%d,%s,%s,%s,%s,%s)\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 typeNum, typeName, tooltip, glyphFile, fg, bg));
3592 if (use_fg || use_bg)
3593 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003594 char fgbuf[2 * (8 + MAX_COLOR_LENGTH) + 1];
3595 char bgbuf[2 * (8 + MAX_COLOR_LENGTH) + 1];
3596 char *ptr;
3597 int value;
3598
3599 value = strtol((char *)fg, &ptr, 10);
3600 if (ptr != (char *)fg)
3601 sprintf(fgbuf, "guifg=#%06x", value & 0xFFFFFF);
3602 else
3603 sprintf(fgbuf, "guifg=%s ctermfg=%s", fg, fg);
3604
3605 value = strtol((char *)bg, &ptr, 10);
3606 if (ptr != (char *)bg)
3607 sprintf(bgbuf, "guibg=#%06x", value & 0xFFFFFF);
3608 else
3609 sprintf(bgbuf, "guibg=%s ctermbg=%s", bg, bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610
3611 coloncmd(":highlight NB_%s %s %s", typeName, (use_fg) ? fgbuf : "",
3612 (use_bg) ? bgbuf : "");
3613 if (*glyphFile == NUL)
3614 /* no glyph, line highlighting only */
3615 coloncmd(":sign define %d linehl=NB_%s", i + 1, typeName);
3616 else if (vim_strsize(glyphFile) <= 2)
3617 /* one- or two-character glyph name, use as text glyph with
3618 * texthl */
3619 coloncmd(":sign define %d text=%s texthl=NB_%s", i + 1,
3620 glyphFile, typeName);
3621 else
3622 /* glyph, line highlighting */
3623 coloncmd(":sign define %d icon=%s linehl=NB_%s", i + 1,
3624 glyphFile, typeName);
3625 }
3626 else
3627 /* glyph, no line highlighting */
3628 coloncmd(":sign define %d icon=%s", i + 1, glyphFile);
3629
3630 if (STRCMP(typeName,"CurrentPC") == 0)
3631 curPCtype = typeNum;
3632
3633 if (globalsignmapused == globalsignmaplen)
3634 {
3635 if (globalsignmaplen == 0) /* first allocation */
3636 {
3637 globalsignmaplen = 20;
3638 globalsignmap = (char **)alloc_clear(globalsignmaplen*sizeof(char *));
3639 }
3640 else /* grow it */
3641 {
3642 int incr;
3643 int oldlen = globalsignmaplen;
3644
3645 globalsignmaplen *= 2;
3646 incr = globalsignmaplen - oldlen;
3647 globalsignmap = (char **)vim_realloc(globalsignmap,
3648 globalsignmaplen * sizeof(char *));
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003649 vim_memset(globalsignmap + oldlen, 0, incr * sizeof(char *));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 }
3651 }
3652
3653 globalsignmap[i] = (char *)typeName;
3654 globalsignmapused = i + 1;
3655 }
3656
3657 /* check local map; should *not* be found! */
3658 for (j = 0; j < buf->signmapused; j++)
3659 if (buf->signmap[j] == i + 1)
3660 return;
3661
3662 /* add to local map */
3663 if (buf->signmapused == buf->signmaplen)
3664 {
3665 if (buf->signmaplen == 0) /* first allocation */
3666 {
3667 buf->signmaplen = 5;
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003668 buf->signmap = (int *)alloc_clear(buf->signmaplen * sizeof(int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 }
3670 else /* grow it */
3671 {
3672 int incr;
3673 int oldlen = buf->signmaplen;
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003674
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 buf->signmaplen *= 2;
3676 incr = buf->signmaplen - oldlen;
3677 buf->signmap = (int *)vim_realloc(buf->signmap,
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003678 buf->signmaplen * sizeof(int));
3679 vim_memset(buf->signmap + oldlen, 0, incr * sizeof(int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 }
3681 }
3682
3683 buf->signmap[buf->signmapused++] = i + 1;
3684
3685}
3686
3687
3688/*
3689 * See if we have the requested sign type in the buffer.
3690 */
3691 static int
3692mapsigntype(nbbuf_T *buf, int localsigntype)
3693{
3694 if (--localsigntype >= 0 && localsigntype < buf->signmapused)
3695 return buf->signmap[localsigntype];
3696
3697 return 0;
3698}
3699
3700
3701/*
3702 * Compute length of buffer, don't print anything.
3703 */
3704 static long
3705get_buf_size(buf_T *bufp)
3706{
3707 linenr_T lnum;
3708 long char_count = 0;
3709 int eol_size;
3710 long last_check = 100000L;
3711
3712 if (bufp->b_ml.ml_flags & ML_EMPTY)
3713 return 0;
3714 else
3715 {
3716 if (get_fileformat(bufp) == EOL_DOS)
3717 eol_size = 2;
3718 else
3719 eol_size = 1;
3720 for (lnum = 1; lnum <= bufp->b_ml.ml_line_count; ++lnum)
3721 {
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00003722 char_count += (long)STRLEN(ml_get_buf(bufp, lnum, FALSE))
3723 + eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 /* Check for a CTRL-C every 100000 characters */
3725 if (char_count > last_check)
3726 {
3727 ui_breakcheck();
3728 if (got_int)
3729 return char_count;
3730 last_check = char_count + 100000L;
3731 }
3732 }
3733 /* Correction for when last line doesn't have an EOL. */
3734 if (!bufp->b_p_eol && bufp->b_p_bin)
3735 char_count -= eol_size;
3736 }
3737
3738 return char_count;
3739}
3740
3741/*
3742 * Convert character offset to lnum,col
3743 */
3744 static pos_T *
3745off2pos(buf_T *buf, long offset)
3746{
3747 linenr_T lnum;
3748 static pos_T pos;
3749
3750 pos.lnum = 0;
3751 pos.col = 0;
3752#ifdef FEAT_VIRTUALEDIT
3753 pos.coladd = 0;
3754#endif
3755
3756 if (!(buf->b_ml.ml_flags & ML_EMPTY))
3757 {
3758 if ((lnum = ml_find_line_or_offset(buf, (linenr_T)0, &offset)) < 0)
3759 return NULL;
3760 pos.lnum = lnum;
3761 pos.col = offset;
3762 }
3763
3764 return &pos;
3765}
3766
3767/*
3768 * Convert an argument in the form "1234" to an offset and compute the
3769 * lnum/col from it. Convert an argument in the form "123/12" directly to a
3770 * lnum/col.
3771 * "argp" is advanced to after the argument.
3772 * Return a pointer to the position, NULL if something is wrong.
3773 */
3774 static pos_T *
3775get_off_or_lnum(buf_T *buf, char_u **argp)
3776{
3777 static pos_T mypos;
3778 long off;
3779
3780 off = strtol((char *)*argp, (char **)argp, 10);
3781 if (**argp == '/')
3782 {
3783 mypos.lnum = (linenr_T)off;
3784 ++*argp;
3785 mypos.col = strtol((char *)*argp, (char **)argp, 10);
3786#ifdef FEAT_VIRTUALEDIT
3787 mypos.coladd = 0;
3788#endif
3789 return &mypos;
3790 }
3791 return off2pos(buf, off);
3792}
3793
3794
3795/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003796 * Convert (lnum,col) to byte offset in the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 */
3798 static long
3799pos2off(buf_T *buf, pos_T *pos)
3800{
3801 long offset = 0;
3802
3803 if (!(buf->b_ml.ml_flags & ML_EMPTY))
3804 {
3805 if ((offset = ml_find_line_or_offset(buf, pos->lnum, 0)) < 0)
3806 return 0;
3807 offset += pos->col;
3808 }
3809
3810 return offset;
3811}
3812
3813
Bram Moolenaar009b2592004-10-24 19:18:58 +00003814/*
3815 * This message is printed after NetBeans opens a new file. Its
3816 * similar to the message readfile() uses, but since NetBeans
3817 * doesn't normally call readfile, we do our own.
3818 */
3819 static void
3820print_read_msg(buf)
3821 nbbuf_T *buf;
3822{
3823 int lnum = buf->bufp->b_ml.ml_line_count;
Bram Moolenaar914703b2010-05-31 21:59:46 +02003824 off_t nchars = buf->bufp->b_orig_size;
Bram Moolenaar009b2592004-10-24 19:18:58 +00003825 char_u c;
3826
3827 msg_add_fname(buf->bufp, buf->bufp->b_ffname);
3828 c = FALSE;
3829
3830 if (buf->bufp->b_p_ro)
3831 {
3832 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]"));
3833 c = TRUE;
3834 }
3835 if (!buf->bufp->b_start_eol)
3836 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003837 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]")
3838 : _("[Incomplete last line]"));
Bram Moolenaar009b2592004-10-24 19:18:58 +00003839 c = TRUE;
3840 }
3841 msg_add_lines(c, (long)lnum, nchars);
3842
3843 /* Now display it */
3844 vim_free(keep_msg);
3845 keep_msg = NULL;
3846 msg_scrolled_ign = TRUE;
3847 msg_trunc_attr(IObuff, FALSE, 0);
3848 msg_scrolled_ign = FALSE;
3849}
3850
3851
3852/*
Bram Moolenaar67c53842010-05-22 18:28:27 +02003853 * Print a message after NetBeans writes the file. This message should be
3854 * identical to the standard message a non-netbeans user would see when
3855 * writing a file.
Bram Moolenaar009b2592004-10-24 19:18:58 +00003856 */
3857 static void
3858print_save_msg(buf, nchars)
3859 nbbuf_T *buf;
Bram Moolenaar914703b2010-05-31 21:59:46 +02003860 off_t nchars;
Bram Moolenaar009b2592004-10-24 19:18:58 +00003861{
3862 char_u c;
3863 char_u *p;
3864
3865 if (nchars >= 0)
3866 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003867 /* put fname in IObuff with quotes */
3868 msg_add_fname(buf->bufp, buf->bufp->b_ffname);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003869 c = FALSE;
3870
3871 msg_add_lines(c, buf->bufp->b_ml.ml_line_count,
Bram Moolenaar914703b2010-05-31 21:59:46 +02003872 buf->bufp->b_orig_size);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003873
3874 vim_free(keep_msg);
3875 keep_msg = NULL;
3876 msg_scrolled_ign = TRUE;
3877 p = msg_trunc_attr(IObuff, FALSE, 0);
3878 if ((msg_scrolled && !need_wait_return) || !buf->initDone)
3879 {
3880 /* Need to repeat the message after redrawing when:
3881 * - When reading from stdin (the screen will be cleared next).
3882 * - When restart_edit is set (otherwise there will be a delay
3883 * before redrawing).
3884 * - When the screen was scrolled but there is no wait-return
3885 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003886 set_keep_msg(p, 0);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003887 }
3888 msg_scrolled_ign = FALSE;
3889 /* add_to_input_buf((char_u *)"\f", 1); */
3890 }
3891 else
3892 {
3893 char_u ebuf[BUFSIZ];
3894
3895 STRCPY(ebuf, (char_u *)_("E505: "));
3896 STRCAT(ebuf, IObuff);
3897 STRCAT(ebuf, (char_u *)_("is read-only (add ! to override)"));
3898 STRCPY(IObuff, ebuf);
Bram Moolenaarf2330482008-06-24 20:19:36 +00003899 nbdebug((" %s\n", ebuf ));
Bram Moolenaar009b2592004-10-24 19:18:58 +00003900 emsg(IObuff);
3901 }
3902}
3903
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904#endif /* defined(FEAT_NETBEANS_INTG) */