blob: c3345447aadaacfb85aeb1950ac1e27e3a9ade87 [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.
Bram Moolenaare3cc6d42011-10-20 21:58:34 +020017 *
18 * The Netbeans messages are received and queued in the gui event loop, or in
19 * the select loop when Vim runs in a terminal. These messages are processed
20 * by netbeans_parse_messages() which is invoked in the idle loop when Vim is
21 * waiting for user input. The function netbeans_parse_messages() is also
22 * called from the ":sleep" command, to allow the execution of test cases that
23 * may not invoke the idle loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024 */
25
26#include "vim.h"
27
28#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
29
Bram Moolenaarb26e6322010-05-22 21:34:09 +020030/* TODO: when should this not be defined? */
31#define INET_SOCKETS
32
Bram Moolenaar071d4272004-06-13 20:20:40 +000033/* Note: when making changes here also adjust configure.in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000034#ifdef WIN32
35# ifdef DEBUG
36# include <tchar.h> /* for _T definition for TRACEn macros */
37# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000038/* WinSock API is separated from C API, thus we can't use read(), write(),
39 * errno... */
Bram Moolenaarc39125d2010-05-23 12:06:58 +020040# define SOCK_ERRNO errno = WSAGetLastError()
Bram Moolenaarbd42a0f2009-06-16 14:57:26 +000041# undef ECONNREFUSED
Bram Moolenaar071d4272004-06-13 20:20:40 +000042# define ECONNREFUSED WSAECONNREFUSED
43# ifdef EINTR
44# undef EINTR
45# endif
46# define EINTR WSAEINTR
47# define sock_write(sd, buf, len) send(sd, buf, len, 0)
48# define sock_read(sd, buf, len) recv(sd, buf, len, 0)
49# define sock_close(sd) closesocket(sd)
50# define sleep(t) Sleep(t*1000) /* WinAPI Sleep() accepts milliseconds */
51#else
Bram Moolenaarb26e6322010-05-22 21:34:09 +020052# ifdef INET_SOCKETS
53# include <netdb.h>
54# include <netinet/in.h>
55# else
56# include <sys/un.h>
57# endif
58
Bram Moolenaar071d4272004-06-13 20:20:40 +000059# include <sys/socket.h>
60# ifdef HAVE_LIBGEN_H
61# include <libgen.h>
62# endif
Bram Moolenaarc39125d2010-05-23 12:06:58 +020063# define SOCK_ERRNO
Bram Moolenaar071d4272004-06-13 20:20:40 +000064# define sock_write(sd, buf, len) write(sd, buf, len)
65# define sock_read(sd, buf, len) read(sd, buf, len)
66# define sock_close(sd) close(sd)
67#endif
68
69#include "version.h"
70
Bram Moolenaar071d4272004-06-13 20:20:40 +000071#define GUARDED 10000 /* typenr for "guarded" annotation */
72#define GUARDEDOFFSET 1000000 /* base for "guarded" sign id's */
Bram Moolenaar67c53842010-05-22 18:28:27 +020073#define MAX_COLOR_LENGTH 32 /* max length of color name in defineAnnoType */
Bram Moolenaar071d4272004-06-13 20:20:40 +000074
75/* The first implementation (working only with Netbeans) returned "1.1". The
76 * protocol implemented here also supports A-A-P. */
Bram Moolenaar67c53842010-05-22 18:28:27 +020077static char *ExtEdProtocolVersion = "2.5";
Bram Moolenaar071d4272004-06-13 20:20:40 +000078
79static long pos2off __ARGS((buf_T *, pos_T *));
80static pos_T *off2pos __ARGS((buf_T *, long));
81static pos_T *get_off_or_lnum __ARGS((buf_T *buf, char_u **argp));
82static long get_buf_size __ARGS((buf_T *));
Bram Moolenaar8065d7f2010-01-19 15:13:14 +010083static int netbeans_keystring __ARGS((char_u *keystr));
84static void postpone_keycommand __ARGS((char_u *keystr));
Bram Moolenaar009b2592004-10-24 19:18:58 +000085static void special_keys __ARGS((char_u *args));
Bram Moolenaar071d4272004-06-13 20:20:40 +000086
Bram Moolenaarb26e6322010-05-22 21:34:09 +020087static int netbeans_connect __ARGS((char *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +000088static int getConnInfo __ARGS((char *file, char **host, char **port, char **password));
89
90static void nb_init_graphics __ARGS((void));
91static void coloncmd __ARGS((char *cmd, ...));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000092static void nb_set_curbuf __ARGS((buf_T *buf));
Bram Moolenaar173c9852010-09-29 17:27:01 +020093#ifdef FEAT_GUI_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +000094static void messageFromNetbeans __ARGS((XtPointer, int *, XtInputId *));
95#endif
96#ifdef FEAT_GUI_GTK
97static void messageFromNetbeans __ARGS((gpointer, gint, GdkInputCondition));
98#endif
99static void nb_parse_cmd __ARGS((char_u *));
100static int nb_do_cmd __ARGS((int, char_u *, int, int, char_u *));
101static void nb_send __ARGS((char *buf, char *fun));
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200102static void nb_free __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103
Bram Moolenaar67c53842010-05-22 18:28:27 +0200104/* TRUE when netbeans is running with a GUI. */
105#ifdef FEAT_GUI
106# define NB_HAS_GUI (gui.in_use || gui.starting)
107#endif
108
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000109#ifdef WIN64
110typedef __int64 NBSOCK;
111#else
112typedef int NBSOCK;
113#endif
114
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200115static NBSOCK nbsock = -1; /* socket fd for Netbeans connection */
116#define NETBEANS_OPEN (nbsock != -1)
117
Bram Moolenaar173c9852010-09-29 17:27:01 +0200118#ifdef FEAT_GUI_X11
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200119static XtInputId inputHandler = (XtInputId)NULL; /* Cookie for input */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120#endif
121#ifdef FEAT_GUI_GTK
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200122static gint inputHandler = 0; /* Cookie for input */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123#endif
124#ifdef FEAT_GUI_W32
125static int inputHandler = -1; /* simply ret.value of WSAAsyncSelect() */
126extern HWND s_hwnd; /* Gvim's Window handle */
127#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +0000128static int r_cmdno; /* current command number for reply */
Bram Moolenaar009b2592004-10-24 19:18:58 +0000129static int dosetvisible = FALSE;
130
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131/*
132 * Include the debugging code if wanted.
133 */
134#ifdef NBDEBUG
135# include "nbdebug.c"
136#endif
137
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200138static int needupdate = 0;
139static int inAtomic = 0;
140
Bram Moolenaar7ad7d012010-11-16 15:49:02 +0100141/*
142 * Close the socket and remove the input handlers.
143 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144 static void
Bram Moolenaar7ad7d012010-11-16 15:49:02 +0100145nb_close_socket(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146{
Bram Moolenaar173c9852010-09-29 17:27:01 +0200147#ifdef FEAT_GUI_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148 if (inputHandler != (XtInputId)NULL)
149 {
150 XtRemoveInput(inputHandler);
151 inputHandler = (XtInputId)NULL;
152 }
Bram Moolenaar67c53842010-05-22 18:28:27 +0200153#else
154# ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155 if (inputHandler != 0)
156 {
157 gdk_input_remove(inputHandler);
158 inputHandler = 0;
159 }
Bram Moolenaar67c53842010-05-22 18:28:27 +0200160# else
161# ifdef FEAT_GUI_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162 if (inputHandler == 0)
163 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200164 WSAAsyncSelect(nbsock, s_hwnd, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165 inputHandler = -1;
166 }
Bram Moolenaar67c53842010-05-22 18:28:27 +0200167# endif
168# endif
169#endif
170
Bram Moolenaar7ad7d012010-11-16 15:49:02 +0100171 sock_close(nbsock);
172 nbsock = -1;
173}
174
175/*
176 * Close the connection and cleanup.
177 * May be called when nb_close_socket() was called earlier.
178 */
179 static void
180netbeans_close(void)
181{
182 if (NETBEANS_OPEN)
183 {
184 netbeans_send_disconnect();
185 nb_close_socket();
186 }
187
Bram Moolenaar67c53842010-05-22 18:28:27 +0200188#ifdef FEAT_BEVAL
Bram Moolenaard62bec82005-03-07 22:56:57 +0000189 bevalServers &= ~BEVAL_NETBEANS;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200190#endif
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200191
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200192 needupdate = 0;
193 inAtomic = 0;
194 nb_free();
195
196 /* remove all signs and update the screen after gutter removal */
197 coloncmd(":sign unplace *");
198 changed_window_setting();
199 update_screen(CLEAR);
200 setcursor();
Bram Moolenaar96bcc5e2011-04-01 15:33:59 +0200201 cursor_on();
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200202 out_flush();
203#ifdef FEAT_GUI
Bram Moolenaard54a6882010-08-09 22:49:00 +0200204 if (gui.in_use)
205 {
206 gui_update_cursor(TRUE, FALSE);
207 gui_mch_flush();
208 }
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200209#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211
212#define NB_DEF_HOST "localhost"
213#define NB_DEF_ADDR "3219"
214#define NB_DEF_PASS "changeme"
215
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200216 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200217netbeans_connect(char *params, int doabort)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218{
219#ifdef INET_SOCKETS
220 struct sockaddr_in server;
221 struct hostent * host;
222# ifdef FEAT_GUI_W32
223 u_short port;
224# else
225 int port;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200226# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227#else
228 struct sockaddr_un server;
229#endif
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200230 int sd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231 char buf[32];
232 char *hostname = NULL;
233 char *address = NULL;
234 char *password = NULL;
235 char *fname;
236 char *arg = NULL;
237
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200238 if (*params == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200240 /* "=fname": Read info from specified file. */
241 if (getConnInfo(params + 1, &hostname, &address, &password)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 == FAIL)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200243 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 }
245 else
246 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200247 if (*params == ':')
248 /* ":<host>:<addr>:<password>": get info from argument */
249 arg = params + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250 if (arg == NULL && (fname = getenv("__NETBEANS_CONINFO")) != NULL)
251 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200252 /* "": get info from file specified in environment */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 if (getConnInfo(fname, &hostname, &address, &password) == FAIL)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200254 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255 }
256 else
257 {
258 if (arg != NULL)
259 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200260 /* ":<host>:<addr>:<password>": get info from argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 hostname = arg;
262 address = strchr(hostname, ':');
263 if (address != NULL)
264 {
265 *address++ = '\0';
266 password = strchr(address, ':');
267 if (password != NULL)
268 *password++ = '\0';
269 }
270 }
271
272 /* Get the missing values from the environment. */
273 if (hostname == NULL || *hostname == '\0')
274 hostname = getenv("__NETBEANS_HOST");
275 if (address == NULL)
276 address = getenv("__NETBEANS_SOCKET");
277 if (password == NULL)
278 password = getenv("__NETBEANS_VIM_PASSWORD");
279
280 /* Move values to allocated memory. */
281 if (hostname != NULL)
282 hostname = (char *)vim_strsave((char_u *)hostname);
283 if (address != NULL)
284 address = (char *)vim_strsave((char_u *)address);
285 if (password != NULL)
286 password = (char *)vim_strsave((char_u *)password);
287 }
288 }
289
290 /* Use the default when a value is missing. */
291 if (hostname == NULL || *hostname == '\0')
292 {
293 vim_free(hostname);
294 hostname = (char *)vim_strsave((char_u *)NB_DEF_HOST);
295 }
296 if (address == NULL || *address == '\0')
297 {
298 vim_free(address);
299 address = (char *)vim_strsave((char_u *)NB_DEF_ADDR);
300 }
301 if (password == NULL || *password == '\0')
302 {
303 vim_free(password);
304 password = (char *)vim_strsave((char_u *)NB_DEF_PASS);
305 }
306 if (hostname == NULL || address == NULL || password == NULL)
307 goto theend; /* out of memory */
308
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200309#ifdef FEAT_GUI_W32
310 netbeans_init_winsock();
311#endif
312
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313#ifdef INET_SOCKETS
314 port = atoi(address);
315
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000316 if ((sd = (NBSOCK)socket(AF_INET, SOCK_STREAM, 0)) == (NBSOCK)-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000318 nbdebug(("error in socket() in netbeans_connect()\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319 PERROR("socket() in netbeans_connect()");
320 goto theend;
321 }
322
323 /* Get the server internet address and put into addr structure */
324 /* fill in the socket address structure and connect to server */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200325 vim_memset((char *)&server, '\0', sizeof(server));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326 server.sin_family = AF_INET;
327 server.sin_port = htons(port);
328 if ((host = gethostbyname(hostname)) == NULL)
329 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000330 nbdebug(("error in gethostbyname() in netbeans_connect()\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331 PERROR("gethostbyname() in netbeans_connect()");
Bram Moolenaar870b05c2011-01-04 18:11:43 +0100332 sock_close(sd);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 goto theend;
334 }
335 memcpy((char *)&server.sin_addr, host->h_addr, host->h_length);
336#else
337 if ((sd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
338 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000339 nbdebug(("error in socket() in netbeans_connect()\n"));
340 PERROR("socket() in netbeans_connect()");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 goto theend;
342 }
343
344 server.sun_family = AF_UNIX;
345 strcpy(server.sun_path, address);
346#endif
347 /* Connect to server */
348 if (connect(sd, (struct sockaddr *)&server, sizeof(server)))
349 {
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200350 SOCK_ERRNO;
351 nbdebug(("netbeans_connect: Connect failed with errno %d\n", errno));
352 if (errno == ECONNREFUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 {
354 sock_close(sd);
355#ifdef INET_SOCKETS
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000356 if ((sd = (NBSOCK)socket(AF_INET, SOCK_STREAM, 0)) == (NBSOCK)-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 {
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200358 SOCK_ERRNO;
Bram Moolenaarf2330482008-06-24 20:19:36 +0000359 nbdebug(("socket()#2 in netbeans_connect()\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 PERROR("socket()#2 in netbeans_connect()");
361 goto theend;
362 }
363#else
364 if ((sd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
365 {
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200366 SOCK_ERRNO;
Bram Moolenaarf2330482008-06-24 20:19:36 +0000367 nbdebug(("socket()#2 in netbeans_connect()\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 PERROR("socket()#2 in netbeans_connect()");
369 goto theend;
370 }
371#endif
372 if (connect(sd, (struct sockaddr *)&server, sizeof(server)))
373 {
374 int retries = 36;
375 int success = FALSE;
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200376
377 SOCK_ERRNO;
378 while (retries-- && ((errno == ECONNREFUSED)
379 || (errno == EINTR)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380 {
381 nbdebug(("retrying...\n"));
Bram Moolenaar870b05c2011-01-04 18:11:43 +0100382 mch_delay(3000L, TRUE);
383 ui_breakcheck();
384 if (got_int)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200385 {
Bram Moolenaar870b05c2011-01-04 18:11:43 +0100386 errno = EINTR;
387 break;
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389 if (connect(sd, (struct sockaddr *)&server,
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200390 sizeof(server)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391 {
392 success = TRUE;
393 break;
394 }
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200395 SOCK_ERRNO;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396 }
397 if (!success)
398 {
399 /* Get here when the server can't be found. */
Bram Moolenaarf2330482008-06-24 20:19:36 +0000400 nbdebug(("Cannot connect to Netbeans #2\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 PERROR(_("Cannot connect to Netbeans #2"));
Bram Moolenaar870b05c2011-01-04 18:11:43 +0100402 sock_close(sd);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200403 if (doabort)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200404 getout(1);
405 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 }
407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 }
409 else
410 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000411 nbdebug(("Cannot connect to Netbeans\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412 PERROR(_("Cannot connect to Netbeans"));
Bram Moolenaar870b05c2011-01-04 18:11:43 +0100413 sock_close(sd);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200414 if (doabort)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200415 getout(1);
416 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417 }
418 }
419
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200420 nbsock = sd;
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000421 vim_snprintf(buf, sizeof(buf), "AUTH %s\n", password);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422 nb_send(buf, "netbeans_connect");
423
424 sprintf(buf, "0:version=0 \"%s\"\n", ExtEdProtocolVersion);
425 nb_send(buf, "externaleditor_version");
426
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427theend:
428 vim_free(hostname);
429 vim_free(address);
430 vim_free(password);
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200431 return NETBEANS_OPEN ? OK : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432}
433
434/*
435 * Obtain the NetBeans hostname, port address and password from a file.
436 * Return the strings in allocated memory.
437 * Return FAIL if the file could not be read, OK otherwise (no matter what it
438 * contains).
439 */
440 static int
441getConnInfo(char *file, char **host, char **port, char **auth)
442{
443 FILE *fp;
444 char_u buf[BUFSIZ];
445 char_u *lp;
Bram Moolenaar309cbc32012-01-10 22:31:31 +0100446 char_u *nlp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447#ifdef UNIX
448 struct stat st;
449
450 /*
451 * For Unix only accept the file when it's not accessible by others.
452 * The open will then fail if we don't own the file.
453 */
454 if (mch_stat(file, &st) == 0 && (st.st_mode & 0077) != 0)
455 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000456 nbdebug(("Wrong access mode for NetBeans connection info file: \"%s\"\n",
457 file));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 EMSG2(_("E668: Wrong access mode for NetBeans connection info file: \"%s\""),
459 file);
460 return FAIL;
461 }
462#endif
463
464 fp = mch_fopen(file, "r");
465 if (fp == NULL)
466 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000467 nbdebug(("Cannot open NetBeans connection info file\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468 PERROR("E660: Cannot open NetBeans connection info file");
469 return FAIL;
470 }
471
472 /* Read the file. There should be one of each parameter */
473 while ((lp = (char_u *)fgets((char *)buf, BUFSIZ, fp)) != NULL)
474 {
Bram Moolenaar309cbc32012-01-10 22:31:31 +0100475 if ((nlp = vim_strchr(lp, '\n')) != NULL)
476 *nlp = 0; /* strip off the trailing newline */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477
478 if (STRNCMP(lp, "host=", 5) == 0)
479 {
480 vim_free(*host);
481 *host = (char *)vim_strsave(&buf[5]);
482 }
483 else if (STRNCMP(lp, "port=", 5) == 0)
484 {
485 vim_free(*port);
486 *port = (char *)vim_strsave(&buf[5]);
487 }
488 else if (STRNCMP(lp, "auth=", 5) == 0)
489 {
490 vim_free(*auth);
491 *auth = (char *)vim_strsave(&buf[5]);
492 }
493 }
494 fclose(fp);
495
496 return OK;
497}
498
499
500struct keyqueue
501{
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100502 char_u *keystr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 struct keyqueue *next;
504 struct keyqueue *prev;
505};
506
507typedef struct keyqueue keyQ_T;
508
509static keyQ_T keyHead; /* dummy node, header for circular queue */
510
511
512/*
513 * Queue up key commands sent from netbeans.
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100514 * We store the string, because it may depend on the global mod_mask and
515 * :nbkey doesn't have a key number.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 */
517 static void
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100518postpone_keycommand(char_u *keystr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519{
520 keyQ_T *node;
521
522 node = (keyQ_T *)alloc(sizeof(keyQ_T));
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100523 if (node == NULL)
524 return; /* out of memory, drop the key */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525
526 if (keyHead.next == NULL) /* initialize circular queue */
527 {
528 keyHead.next = &keyHead;
529 keyHead.prev = &keyHead;
530 }
531
532 /* insert node at tail of queue */
533 node->next = &keyHead;
534 node->prev = keyHead.prev;
535 keyHead.prev->next = node;
536 keyHead.prev = node;
537
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100538 node->keystr = vim_strsave(keystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539}
540
541/*
542 * Handle any queued-up NetBeans keycommands to be send.
543 */
544 static void
545handle_key_queue(void)
546{
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100547 int postponed = FALSE;
548
549 while (!postponed && keyHead.next && keyHead.next != &keyHead)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 {
551 /* first, unlink the node */
552 keyQ_T *node = keyHead.next;
553 keyHead.next = node->next;
554 node->next->prev = node->prev;
555
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100556 /* Now, send the keycommand. This may cause it to be postponed again
557 * and change keyHead. */
558 if (node->keystr != NULL)
559 postponed = !netbeans_keystring(node->keystr);
560 vim_free(node->keystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561
562 /* Finally, dispose of the node */
563 vim_free(node);
564 }
565}
566
567
568struct cmdqueue
569{
570 char_u *buffer;
571 struct cmdqueue *next;
572 struct cmdqueue *prev;
573};
574
575typedef struct cmdqueue queue_T;
576
577static queue_T head; /* dummy node, header for circular queue */
578
579
580/*
581 * Put the buffer on the work queue; possibly save it to a file as well.
582 */
583 static void
584save(char_u *buf, int len)
585{
586 queue_T *node;
587
588 node = (queue_T *)alloc(sizeof(queue_T));
589 if (node == NULL)
590 return; /* out of memory */
591 node->buffer = alloc(len + 1);
592 if (node->buffer == NULL)
593 {
594 vim_free(node);
595 return; /* out of memory */
596 }
597 mch_memmove(node->buffer, buf, (size_t)len);
598 node->buffer[len] = NUL;
599
600 if (head.next == NULL) /* initialize circular queue */
601 {
602 head.next = &head;
603 head.prev = &head;
604 }
605
606 /* insert node at tail of queue */
607 node->next = &head;
608 node->prev = head.prev;
609 head.prev->next = node;
610 head.prev = node;
611
612#ifdef NBDEBUG
613 {
614 static int outfd = -2;
615
616 /* possibly write buffer out to a file */
617 if (outfd == -3)
618 return;
619
620 if (outfd == -2)
621 {
622 char *file = getenv("__NETBEANS_SAVE");
623 if (file == NULL)
624 outfd = -3;
625 else
626 outfd = mch_open(file, O_WRONLY|O_CREAT|O_TRUNC, 0666);
627 }
628
629 if (outfd >= 0)
630 write(outfd, buf, len);
631 }
632#endif
633}
634
635
636/*
637 * While there's still a command in the work queue, parse and execute it.
638 */
Bram Moolenaarf2330482008-06-24 20:19:36 +0000639 void
640netbeans_parse_messages(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641{
642 char_u *p;
643 queue_T *node;
Bram Moolenaar863053d2010-12-02 17:09:54 +0100644 int own_node;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645
Bram Moolenaarf2330482008-06-24 20:19:36 +0000646 while (head.next != NULL && head.next != &head)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 {
648 node = head.next;
649
650 /* Locate the first line in the first buffer. */
651 p = vim_strchr(node->buffer, '\n');
652 if (p == NULL)
653 {
654 /* Command isn't complete. If there is no following buffer,
655 * return (wait for more). If there is another buffer following,
656 * prepend the text to that buffer and delete this one. */
657 if (node->next == &head)
658 return;
Bram Moolenaarf2330482008-06-24 20:19:36 +0000659 p = alloc((unsigned)(STRLEN(node->buffer)
660 + STRLEN(node->next->buffer) + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 if (p == NULL)
662 return; /* out of memory */
663 STRCPY(p, node->buffer);
664 STRCAT(p, node->next->buffer);
665 vim_free(node->next->buffer);
666 node->next->buffer = p;
667
668 /* dispose of the node and buffer */
669 head.next = node->next;
670 node->next->prev = node->prev;
671 vim_free(node->buffer);
672 vim_free(node);
673 }
674 else
675 {
676 /* There is a complete command at the start of the buffer.
677 * Terminate it with a NUL. When no more text is following unlink
678 * the buffer. Do this before executing, because new buffers can
679 * be added while busy handling the command. */
680 *p++ = NUL;
681 if (*p == NUL)
682 {
Bram Moolenaar863053d2010-12-02 17:09:54 +0100683 own_node = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 head.next = node->next;
685 node->next->prev = node->prev;
686 }
Bram Moolenaar863053d2010-12-02 17:09:54 +0100687 else
688 own_node = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689
690 /* now, parse and execute the commands */
691 nb_parse_cmd(node->buffer);
692
Bram Moolenaar863053d2010-12-02 17:09:54 +0100693 if (own_node)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 {
695 /* buffer finished, dispose of the node and buffer */
696 vim_free(node->buffer);
697 vim_free(node);
698 }
Bram Moolenaar863053d2010-12-02 17:09:54 +0100699 /* Check that "head" wasn't changed under our fingers, e.g. when a
700 * DETACH command was handled. */
701 else if (head.next == node)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 {
703 /* more follows, move to the start */
Bram Moolenaarf2330482008-06-24 20:19:36 +0000704 STRMOVE(node->buffer, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 }
706 }
707 }
708}
709
710/* Buffer size for reading incoming messages. */
711#define MAXMSGSIZE 4096
712
713/*
Bram Moolenaar67c53842010-05-22 18:28:27 +0200714 * Read a command from netbeans.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 */
Bram Moolenaar173c9852010-09-29 17:27:01 +0200716#ifdef FEAT_GUI_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 static void
Bram Moolenaara9d45512009-05-17 21:25:42 +0000718messageFromNetbeans(XtPointer clientData UNUSED,
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000719 int *unused1 UNUSED,
720 XtInputId *unused2 UNUSED)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200721{
722 netbeans_read();
723}
724#endif
725
726#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000728messageFromNetbeans(gpointer clientData UNUSED,
729 gint unused1 UNUSED,
730 GdkInputCondition unused2 UNUSED)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200731{
732 netbeans_read();
733}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734#endif
Bram Moolenaar67c53842010-05-22 18:28:27 +0200735
Bram Moolenaar7ad7d012010-11-16 15:49:02 +0100736#define DETACH_MSG "DETACH\n"
737
Bram Moolenaar67c53842010-05-22 18:28:27 +0200738 void
739netbeans_read()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740{
741 static char_u *buf = NULL;
Bram Moolenaar0b65f892010-05-15 14:49:02 +0200742 int len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 int readlen = 0;
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100744#ifdef HAVE_SELECT
745 struct timeval tval;
746 fd_set rfds;
747#else
748# ifdef HAVE_POLL
749 struct pollfd fds;
750# endif
751#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200753 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 {
755 nbdebug(("messageFromNetbeans() called without a socket\n"));
756 return;
757 }
758
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 /* Allocate a buffer to read into. */
760 if (buf == NULL)
761 {
762 buf = alloc(MAXMSGSIZE);
763 if (buf == NULL)
764 return; /* out of memory! */
765 }
766
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100767 /* Keep on reading for as long as there is something to read.
768 * Use select() or poll() to avoid blocking on a message that is exactly
769 * MAXMSGSIZE long. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 for (;;)
771 {
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100772#ifdef HAVE_SELECT
773 FD_ZERO(&rfds);
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200774 FD_SET(nbsock, &rfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +0200775 tval.tv_sec = 0;
776 tval.tv_usec = 0;
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200777 if (select(nbsock + 1, &rfds, NULL, NULL, &tval) <= 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200778 break;
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100779#else
780# ifdef HAVE_POLL
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200781 fds.fd = nbsock;
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100782 fds.events = POLLIN;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200783 if (poll(&fds, 1, 0) <= 0)
784 break;
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100785# endif
786#endif
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200787 len = sock_read(nbsock, buf, MAXMSGSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 if (len <= 0)
789 break; /* error or nothing more to read */
790
791 /* Store the read message in the queue. */
792 save(buf, len);
793 readlen += len;
794 if (len < MAXMSGSIZE)
795 break; /* did read everything that's available */
796 }
797
Bram Moolenaar7ad7d012010-11-16 15:49:02 +0100798 /* Reading a socket disconnection (readlen == 0), or a socket error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 if (readlen <= 0)
800 {
Bram Moolenaar7ad7d012010-11-16 15:49:02 +0100801 /* Queue a "DETACH" netbeans message in the command queue in order to
802 * terminate the netbeans session later. Do not end the session here
803 * directly as we may be running in the context of a call to
804 * netbeans_parse_messages():
805 * netbeans_parse_messages
806 * -> autocmd triggered while processing the netbeans cmd
807 * -> ui_breakcheck
808 * -> gui event loop or select loop
809 * -> netbeans_read()
810 */
Bram Moolenaarde1b0922010-12-24 14:00:17 +0100811 save((char_u *)DETACH_MSG, (int)strlen(DETACH_MSG));
Bram Moolenaar7ad7d012010-11-16 15:49:02 +0100812 nb_close_socket();
813
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 if (len < 0)
Bram Moolenaarf2330482008-06-24 20:19:36 +0000815 {
816 nbdebug(("read from Netbeans socket\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 PERROR(_("read from Netbeans socket"));
Bram Moolenaarf2330482008-06-24 20:19:36 +0000818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 }
820
Bram Moolenaar03531f72010-11-16 15:04:57 +0100821#if defined(NB_HAS_GUI) && defined(FEAT_GUI_GTK)
822 if (NB_HAS_GUI && gtk_main_level() > 0)
Bram Moolenaar7ad7d012010-11-16 15:49:02 +0100823 gtk_main_quit();
Bram Moolenaarf2330482008-06-24 20:19:36 +0000824#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825}
826
827/*
828 * Handle one NUL terminated command.
829 *
830 * format of a command from netbeans:
831 *
832 * 6:setTitle!84 "a.c"
833 *
834 * bufno
835 * colon
836 * cmd
837 * !
838 * cmdno
839 * args
840 *
841 * for function calls, the ! is replaced by a /
842 */
843 static void
844nb_parse_cmd(char_u *cmd)
845{
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000846 char *verb;
847 char *q;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 int bufno;
849 int isfunc = -1;
850
851 if (STRCMP(cmd, "DISCONNECT") == 0)
852 {
853 /* We assume the server knows that we can safely exit! */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 /* Disconnect before exiting, Motif hangs in a Select error
855 * message otherwise. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200856 netbeans_close();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 getout(0);
858 /* NOTREACHED */
859 }
860
861 if (STRCMP(cmd, "DETACH") == 0)
862 {
863 /* The IDE is breaking the connection. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200864 netbeans_close();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 return;
866 }
867
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000868 bufno = strtol((char *)cmd, &verb, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869
870 if (*verb != ':')
871 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000872 nbdebug((" missing colon: %s\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 EMSG2("E627: missing colon: %s", cmd);
874 return;
875 }
876 ++verb; /* skip colon */
877
878 for (q = verb; *q; q++)
879 {
880 if (*q == '!')
881 {
882 *q++ = NUL;
883 isfunc = 0;
884 break;
885 }
886 else if (*q == '/')
887 {
888 *q++ = NUL;
889 isfunc = 1;
890 break;
891 }
892 }
893
894 if (isfunc < 0)
895 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000896 nbdebug((" missing ! or / in: %s\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 EMSG2("E628: missing ! or / in: %s", cmd);
898 return;
899 }
900
Bram Moolenaar89d40322006-08-29 15:30:07 +0000901 r_cmdno = strtol(q, &q, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000903 q = (char *)skipwhite((char_u *)q);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904
Bram Moolenaar89d40322006-08-29 15:30:07 +0000905 if (nb_do_cmd(bufno, (char_u *)verb, isfunc, r_cmdno, (char_u *)q) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 {
Bram Moolenaar009b2592004-10-24 19:18:58 +0000907#ifdef NBDEBUG
908 /*
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100909 * This happens because the ExtEd can send a command or 2 after
Bram Moolenaar009b2592004-10-24 19:18:58 +0000910 * doing a stopDocumentListen command. It doesn't harm anything
911 * so I'm disabling it except for debugging.
912 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 nbdebug(("nb_parse_cmd: Command error for \"%s\"\n", cmd));
914 EMSG("E629: bad return from nb_do_cmd");
Bram Moolenaar009b2592004-10-24 19:18:58 +0000915#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 }
917}
918
919struct nbbuf_struct
920{
921 buf_T *bufp;
922 unsigned int fireChanges:1;
923 unsigned int initDone:1;
Bram Moolenaar009b2592004-10-24 19:18:58 +0000924 unsigned int insertDone:1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 unsigned int modified:1;
Bram Moolenaar009b2592004-10-24 19:18:58 +0000926 int nbbuf_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 char *displayname;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 int *signmap;
929 short_u signmaplen;
930 short_u signmapused;
931};
932
933typedef struct nbbuf_struct nbbuf_T;
934
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200935static nbbuf_T *buf_list = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000936static int buf_list_size = 0; /* size of buf_list */
937static int buf_list_used = 0; /* nr of entries in buf_list actually in use */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200939static char **globalsignmap = NULL;
940static int globalsignmaplen = 0;
941static int globalsignmapused = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942
943static int mapsigntype __ARGS((nbbuf_T *, int localsigntype));
944static void addsigntype __ARGS((nbbuf_T *, int localsigntype, char_u *typeName,
945 char_u *tooltip, char_u *glyphfile,
Bram Moolenaar67c53842010-05-22 18:28:27 +0200946 char_u *fg, char_u *bg));
Bram Moolenaar009b2592004-10-24 19:18:58 +0000947static void print_read_msg __ARGS((nbbuf_T *buf));
Bram Moolenaar914703b2010-05-31 21:59:46 +0200948static void print_save_msg __ARGS((nbbuf_T *buf, off_t nchars));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949
950static int curPCtype = -1;
951
952/*
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200953 * Free netbeans resources.
954 */
955 static void
956nb_free()
957{
958 keyQ_T *key_node = keyHead.next;
959 queue_T *cmd_node = head.next;
960 nbbuf_T buf;
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200961 int i;
962
963 /* free the netbeans buffer list */
964 for (i = 0; i < buf_list_used; i++)
965 {
966 buf = buf_list[i];
967 vim_free(buf.displayname);
968 vim_free(buf.signmap);
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100969 if (buf.bufp != NULL)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200970 {
971 buf.bufp->b_netbeans_file = FALSE;
972 buf.bufp->b_was_netbeans_file = FALSE;
973 }
974 }
975 vim_free(buf_list);
976 buf_list = NULL;
977 buf_list_size = 0;
978 buf_list_used = 0;
979
980 /* free the queued key commands */
Bram Moolenaar8d4eecc2012-11-20 17:19:01 +0100981 while (key_node != NULL && key_node != &keyHead)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200982 {
983 keyQ_T *next = key_node->next;
984 vim_free(key_node->keystr);
985 vim_free(key_node);
986 if (next == &keyHead)
987 {
988 keyHead.next = &keyHead;
989 keyHead.prev = &keyHead;
990 break;
991 }
992 key_node = next;
993 }
994
995 /* free the queued netbeans commands */
Bram Moolenaar8d4eecc2012-11-20 17:19:01 +0100996 while (cmd_node != NULL && cmd_node != &head)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200997 {
998 queue_T *next = cmd_node->next;
999 vim_free(cmd_node->buffer);
1000 vim_free(cmd_node);
1001 if (next == &head)
1002 {
1003 head.next = &head;
1004 head.prev = &head;
1005 break;
1006 }
1007 cmd_node = next;
1008 }
1009}
1010
1011/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 * Get the Netbeans buffer number for the specified buffer.
1013 */
1014 static int
1015nb_getbufno(buf_T *bufp)
1016{
1017 int i;
1018
1019 for (i = 0; i < buf_list_used; i++)
1020 if (buf_list[i].bufp == bufp)
1021 return i;
1022 return -1;
1023}
1024
1025/*
1026 * Is this a NetBeans-owned buffer?
1027 */
1028 int
1029isNetbeansBuffer(buf_T *bufp)
1030{
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001031 return NETBEANS_OPEN && bufp->b_netbeans_file;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032}
1033
1034/*
1035 * NetBeans and Vim have different undo models. In Vim, the file isn't
1036 * changed if changes are undone via the undo command. In NetBeans, once
1037 * a change has been made the file is marked as modified until saved. It
1038 * doesn't matter if the change was undone.
1039 *
1040 * So this function is for the corner case where Vim thinks a buffer is
1041 * unmodified but NetBeans thinks it IS modified.
1042 */
1043 int
1044isNetbeansModified(buf_T *bufp)
1045{
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001046 if (isNetbeansBuffer(bufp))
Bram Moolenaar009b2592004-10-24 19:18:58 +00001047 {
1048 int bufno = nb_getbufno(bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049
Bram Moolenaar009b2592004-10-24 19:18:58 +00001050 if (bufno > 0)
1051 return buf_list[bufno].modified;
1052 else
1053 return FALSE;
1054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 else
1056 return FALSE;
1057}
1058
1059/*
1060 * Given a Netbeans buffer number, return the netbeans buffer.
1061 * Returns NULL for 0 or a negative number. A 0 bufno means a
1062 * non-buffer related command has been sent.
1063 */
1064 static nbbuf_T *
1065nb_get_buf(int bufno)
1066{
1067 /* find or create a buffer with the given number */
1068 int incr;
1069
1070 if (bufno <= 0)
1071 return NULL;
1072
1073 if (!buf_list)
1074 {
1075 /* initialize */
1076 buf_list = (nbbuf_T *)alloc_clear(100 * sizeof(nbbuf_T));
1077 buf_list_size = 100;
1078 }
1079 if (bufno >= buf_list_used) /* new */
1080 {
1081 if (bufno >= buf_list_size) /* grow list */
1082 {
1083 incr = bufno - buf_list_size + 90;
1084 buf_list_size += incr;
1085 buf_list = (nbbuf_T *)vim_realloc(
1086 buf_list, buf_list_size * sizeof(nbbuf_T));
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02001087 vim_memset(buf_list + buf_list_size - incr, 0,
1088 incr * sizeof(nbbuf_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 }
1090
1091 while (buf_list_used <= bufno)
1092 {
1093 /* Default is to fire text changes. */
1094 buf_list[buf_list_used].fireChanges = 1;
1095 ++buf_list_used;
1096 }
1097 }
1098
1099 return buf_list + bufno;
1100}
1101
1102/*
1103 * Return the number of buffers that are modified.
1104 */
1105 static int
1106count_changed_buffers(void)
1107{
1108 buf_T *bufp;
1109 int n;
1110
1111 n = 0;
1112 for (bufp = firstbuf; bufp != NULL; bufp = bufp->b_next)
1113 if (bufp->b_changed)
1114 ++n;
1115 return n;
1116}
1117
1118/*
1119 * End the netbeans session.
1120 */
1121 void
1122netbeans_end(void)
1123{
1124 int i;
1125 static char buf[128];
1126
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001127 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 return;
1129
1130 for (i = 0; i < buf_list_used; i++)
1131 {
1132 if (!buf_list[i].bufp)
1133 continue;
1134 if (netbeansForcedQuit)
1135 {
1136 /* mark as unmodified so NetBeans won't put up dialog on "killed" */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001137 sprintf(buf, "%d:unmodified=%d\n", i, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 nbdebug(("EVT: %s", buf));
1139 nb_send(buf, "netbeans_end");
1140 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001141 sprintf(buf, "%d:killed=%d\n", i, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 nbdebug(("EVT: %s", buf));
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001143 /* nb_send(buf, "netbeans_end"); avoid "write failed" messages */
1144 ignored = sock_write(nbsock, buf, (int)STRLEN(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146}
1147
1148/*
1149 * Send a message to netbeans.
1150 */
1151 static void
1152nb_send(char *buf, char *fun)
1153{
1154 /* Avoid giving pages full of error messages when the other side has
1155 * exited, only mention the first error until the connection works again. */
1156 static int did_error = FALSE;
1157
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001158 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 {
1160 if (!did_error)
Bram Moolenaarf2330482008-06-24 20:19:36 +00001161 {
1162 nbdebug((" %s(): write while not connected\n", fun));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 EMSG2("E630: %s(): write while not connected", fun);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 did_error = TRUE;
1166 }
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001167 else if (sock_write(nbsock, buf, (int)STRLEN(buf)) != (int)STRLEN(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 {
1169 if (!did_error)
Bram Moolenaarf2330482008-06-24 20:19:36 +00001170 {
1171 nbdebug((" %s(): write failed\n", fun));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 EMSG2("E631: %s(): write failed", fun);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 did_error = TRUE;
1175 }
1176 else
1177 did_error = FALSE;
1178}
1179
1180/*
1181 * Some input received from netbeans requires a response. This function
1182 * handles a response with no information (except the command number).
1183 */
1184 static void
1185nb_reply_nil(int cmdno)
1186{
1187 char reply[32];
1188
Bram Moolenaar009b2592004-10-24 19:18:58 +00001189 nbdebug(("REP %d: <none>\n", cmdno));
1190
Bram Moolenaar7ad7d012010-11-16 15:49:02 +01001191 /* Avoid printing an annoying error message. */
1192 if (!NETBEANS_OPEN)
1193 return;
1194
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 sprintf(reply, "%d\n", cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 nb_send(reply, "nb_reply_nil");
1197}
1198
1199
1200/*
1201 * Send a response with text.
1202 * "result" must have been quoted already (using nb_quote()).
1203 */
1204 static void
1205nb_reply_text(int cmdno, char_u *result)
1206{
1207 char_u *reply;
1208
Bram Moolenaar009b2592004-10-24 19:18:58 +00001209 nbdebug(("REP %d: %s\n", cmdno, (char *)result));
1210
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001211 reply = alloc((unsigned)STRLEN(result) + 32);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 sprintf((char *)reply, "%d %s\n", cmdno, (char *)result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 nb_send((char *)reply, "nb_reply_text");
1214
1215 vim_free(reply);
1216}
1217
1218
1219/*
1220 * Send a response with a number result code.
1221 */
1222 static void
1223nb_reply_nr(int cmdno, long result)
1224{
1225 char reply[32];
1226
Bram Moolenaar009b2592004-10-24 19:18:58 +00001227 nbdebug(("REP %d: %ld\n", cmdno, result));
1228
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 sprintf(reply, "%d %ld\n", cmdno, result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 nb_send(reply, "nb_reply_nr");
1231}
1232
1233
1234/*
1235 * Encode newline, ret, backslash, double quote for transmission to NetBeans.
1236 */
1237 static char_u *
1238nb_quote(char_u *txt)
1239{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001240 char_u *buf = alloc((unsigned)(2 * STRLEN(txt) + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 char_u *p = txt;
1242 char_u *q = buf;
1243
1244 if (buf == NULL)
1245 return NULL;
1246 for (; *p; p++)
1247 {
1248 switch (*p)
1249 {
1250 case '\"':
1251 case '\\':
1252 *q++ = '\\'; *q++ = *p; break;
1253 /* case '\t': */
1254 /* *q++ = '\\'; *q++ = 't'; break; */
1255 case '\n':
1256 *q++ = '\\'; *q++ = 'n'; break;
1257 case '\r':
1258 *q++ = '\\'; *q++ = 'r'; break;
1259 default:
1260 *q++ = *p;
1261 break;
1262 }
1263 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01001264 *q = '\0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265
1266 return buf;
1267}
1268
1269
1270/*
1271 * Remove top level double quotes; convert backslashed chars.
1272 * Returns an allocated string (NULL for failure).
1273 * If "endp" is not NULL it is set to the character after the terminating
1274 * quote.
1275 */
1276 static char *
1277nb_unquote(char_u *p, char_u **endp)
1278{
1279 char *result = 0;
1280 char *q;
1281 int done = 0;
1282
1283 /* result is never longer than input */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001284 result = (char *)alloc_clear((unsigned)STRLEN(p) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 if (result == NULL)
1286 return NULL;
1287
1288 if (*p++ != '"')
1289 {
1290 nbdebug(("nb_unquote called with string that doesn't start with a quote!: %s\n",
1291 p));
1292 result[0] = NUL;
1293 return result;
1294 }
1295
1296 for (q = result; !done && *p != NUL;)
1297 {
1298 switch (*p)
1299 {
1300 case '"':
1301 /*
1302 * Unbackslashed dquote marks the end, if first char was dquote.
1303 */
1304 done = 1;
1305 break;
1306
1307 case '\\':
1308 ++p;
1309 switch (*p)
1310 {
1311 case '\\': *q++ = '\\'; break;
1312 case 'n': *q++ = '\n'; break;
1313 case 't': *q++ = '\t'; break;
1314 case 'r': *q++ = '\r'; break;
1315 case '"': *q++ = '"'; break;
1316 case NUL: --p; break;
1317 /* default: skip over illegal chars */
1318 }
1319 ++p;
1320 break;
1321
1322 default:
1323 *q++ = *p++;
1324 }
1325 }
1326
1327 if (endp != NULL)
1328 *endp = p;
1329
1330 return result;
1331}
1332
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001333/*
1334 * Remove from "first" byte to "last" byte (inclusive), at line "lnum" of the
1335 * current buffer. Remove to end of line when "last" is MAXCOL.
1336 */
1337 static void
1338nb_partialremove(linenr_T lnum, colnr_T first, colnr_T last)
1339{
1340 char_u *oldtext, *newtext;
1341 int oldlen;
1342 int lastbyte = last;
1343
1344 oldtext = ml_get(lnum);
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001345 oldlen = (int)STRLEN(oldtext);
Bram Moolenaarb3c70982008-01-18 10:40:55 +00001346 if (first >= (colnr_T)oldlen || oldlen == 0) /* just in case */
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001347 return;
1348 if (lastbyte >= oldlen)
1349 lastbyte = oldlen - 1;
1350 newtext = alloc(oldlen - (int)(lastbyte - first));
1351 if (newtext != NULL)
1352 {
1353 mch_memmove(newtext, oldtext, first);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001354 STRMOVE(newtext + first, oldtext + lastbyte + 1);
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001355 nbdebug((" NEW LINE %d: %s\n", lnum, newtext));
1356 ml_replace(lnum, newtext, FALSE);
1357 }
1358}
1359
1360/*
1361 * Replace the "first" line with the concatenation of the "first" and
1362 * the "other" line. The "other" line is not removed.
1363 */
1364 static void
1365nb_joinlines(linenr_T first, linenr_T other)
1366{
1367 int len_first, len_other;
1368 char_u *p;
1369
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001370 len_first = (int)STRLEN(ml_get(first));
1371 len_other = (int)STRLEN(ml_get(other));
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001372 p = alloc((unsigned)(len_first + len_other + 1));
1373 if (p != NULL)
1374 {
1375 mch_memmove(p, ml_get(first), len_first);
1376 mch_memmove(p + len_first, ml_get(other), len_other + 1);
1377 ml_replace(first, p, FALSE);
1378 }
1379}
1380
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381#define SKIP_STOP 2
1382#define streq(a,b) (strcmp(a,b) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383
1384/*
1385 * Do the actual processing of a single netbeans command or function.
Bram Moolenaar143c38c2007-05-10 16:41:10 +00001386 * The difference between a command and function is that a function
1387 * gets a response (it's required) but a command does not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 * For arguments see comment for nb_parse_cmd().
1389 */
1390 static int
1391nb_do_cmd(
1392 int bufno,
1393 char_u *cmd,
1394 int func,
1395 int cmdno,
1396 char_u *args) /* points to space before arguments or NUL */
1397{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001398 int do_update = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 long off = 0;
1400 nbbuf_T *buf = nb_get_buf(bufno);
1401 static int skip = 0;
1402 int retval = OK;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001403 char *cp; /* for when a char pointer is needed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404
1405 nbdebug(("%s %d: (%d) %s %s\n", (func) ? "FUN" : "CMD", cmdno, bufno, cmd,
1406 STRCMP(cmd, "insert") == 0 ? "<text>" : (char *)args));
1407
1408 if (func)
1409 {
1410/* =====================================================================*/
1411 if (streq((char *)cmd, "getModified"))
1412 {
1413 if (buf == NULL || buf->bufp == NULL)
1414 /* Return the number of buffers that are modified. */
1415 nb_reply_nr(cmdno, (long)count_changed_buffers());
1416 else
1417 /* Return whether the buffer is modified. */
1418 nb_reply_nr(cmdno, (long)(buf->bufp->b_changed
1419 || isNetbeansModified(buf->bufp)));
1420/* =====================================================================*/
1421 }
1422 else if (streq((char *)cmd, "saveAndExit"))
1423 {
1424 /* Note: this will exit Vim if successful. */
1425 coloncmd(":confirm qall");
1426
1427 /* We didn't exit: return the number of changed buffers. */
1428 nb_reply_nr(cmdno, (long)count_changed_buffers());
1429/* =====================================================================*/
1430 }
1431 else if (streq((char *)cmd, "getCursor"))
1432 {
1433 char_u text[200];
1434
1435 /* Note: nb_getbufno() may return -1. This indicates the IDE
1436 * didn't assign a number to the current buffer in response to a
1437 * fileOpened event. */
1438 sprintf((char *)text, "%d %ld %d %ld",
1439 nb_getbufno(curbuf),
1440 (long)curwin->w_cursor.lnum,
1441 (int)curwin->w_cursor.col,
1442 pos2off(curbuf, &curwin->w_cursor));
1443 nb_reply_text(cmdno, text);
1444/* =====================================================================*/
1445 }
Bram Moolenaarc65c4912006-11-14 17:29:46 +00001446 else if (streq((char *)cmd, "getAnno"))
1447 {
1448 long linenum = 0;
1449#ifdef FEAT_SIGNS
1450 if (buf == NULL || buf->bufp == NULL)
1451 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001452 nbdebug((" Invalid buffer identifier in getAnno\n"));
1453 EMSG("E652: Invalid buffer identifier in getAnno");
Bram Moolenaarc65c4912006-11-14 17:29:46 +00001454 retval = FAIL;
1455 }
1456 else
1457 {
1458 int serNum;
1459
1460 cp = (char *)args;
1461 serNum = strtol(cp, &cp, 10);
1462 /* If the sign isn't found linenum will be zero. */
1463 linenum = (long)buf_findsign(buf->bufp, serNum);
1464 }
1465#endif
1466 nb_reply_nr(cmdno, linenum);
1467/* =====================================================================*/
1468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 else if (streq((char *)cmd, "getLength"))
1470 {
1471 long len = 0;
1472
1473 if (buf == NULL || buf->bufp == NULL)
1474 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001475 nbdebug((" invalid buffer identifier in getLength\n"));
1476 EMSG("E632: invalid buffer identifier in getLength");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 retval = FAIL;
1478 }
1479 else
1480 {
1481 len = get_buf_size(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 }
1483 nb_reply_nr(cmdno, len);
1484/* =====================================================================*/
1485 }
1486 else if (streq((char *)cmd, "getText"))
1487 {
1488 long len;
1489 linenr_T nlines;
1490 char_u *text = NULL;
1491 linenr_T lno = 1;
1492 char_u *p;
1493 char_u *line;
1494
1495 if (buf == NULL || buf->bufp == NULL)
1496 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001497 nbdebug((" invalid buffer identifier in getText\n"));
1498 EMSG("E633: invalid buffer identifier in getText");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 retval = FAIL;
1500 }
1501 else
1502 {
1503 len = get_buf_size(buf->bufp);
1504 nlines = buf->bufp->b_ml.ml_line_count;
1505 text = alloc((unsigned)((len > 0)
1506 ? ((len + nlines) * 2) : 4));
1507 if (text == NULL)
1508 {
1509 nbdebug((" nb_do_cmd: getText has null text field\n"));
1510 retval = FAIL;
1511 }
1512 else
1513 {
1514 p = text;
1515 *p++ = '\"';
1516 for (; lno <= nlines ; lno++)
1517 {
1518 line = nb_quote(ml_get_buf(buf->bufp, lno, FALSE));
1519 if (line != NULL)
1520 {
1521 STRCPY(p, line);
1522 p += STRLEN(line);
1523 *p++ = '\\';
1524 *p++ = 'n';
Bram Moolenaar009b2592004-10-24 19:18:58 +00001525 vim_free(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 }
1528 *p++ = '\"';
1529 *p = '\0';
1530 }
1531 }
1532 if (text == NULL)
1533 nb_reply_text(cmdno, (char_u *)"");
1534 else
1535 {
1536 nb_reply_text(cmdno, text);
1537 vim_free(text);
1538 }
1539/* =====================================================================*/
1540 }
1541 else if (streq((char *)cmd, "remove"))
1542 {
1543 long count;
1544 pos_T first, last;
1545 pos_T *pos;
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001546 pos_T *next;
1547 linenr_T del_from_lnum, del_to_lnum; /* lines to be deleted as a whole */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 int oldFire = netbeansFireChanges;
1549 int oldSuppress = netbeansSuppressNoLines;
1550 int wasChanged;
1551
1552 if (skip >= SKIP_STOP)
1553 {
1554 nbdebug((" Skipping %s command\n", (char *) cmd));
1555 nb_reply_nil(cmdno);
1556 return OK;
1557 }
1558
1559 if (buf == NULL || buf->bufp == NULL)
1560 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001561 nbdebug((" invalid buffer identifier in remove\n"));
1562 EMSG("E634: invalid buffer identifier in remove");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 retval = FAIL;
1564 }
1565 else
1566 {
1567 netbeansFireChanges = FALSE;
1568 netbeansSuppressNoLines = TRUE;
1569
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001570 nb_set_curbuf(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 wasChanged = buf->bufp->b_changed;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001572 cp = (char *)args;
1573 off = strtol(cp, &cp, 10);
1574 count = strtol(cp, &cp, 10);
1575 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 /* delete "count" chars, starting at "off" */
1577 pos = off2pos(buf->bufp, off);
1578 if (!pos)
1579 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001580 nbdebug((" !bad position\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 nb_reply_text(cmdno, (char_u *)"!bad position");
1582 netbeansFireChanges = oldFire;
1583 netbeansSuppressNoLines = oldSuppress;
1584 return FAIL;
1585 }
1586 first = *pos;
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001587 nbdebug((" FIRST POS: line %d, col %d\n",
1588 first.lnum, first.col));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 pos = off2pos(buf->bufp, off+count-1);
1590 if (!pos)
1591 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001592 nbdebug((" !bad count\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 nb_reply_text(cmdno, (char_u *)"!bad count");
1594 netbeansFireChanges = oldFire;
1595 netbeansSuppressNoLines = oldSuppress;
1596 return FAIL;
1597 }
1598 last = *pos;
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001599 nbdebug((" LAST POS: line %d, col %d\n",
1600 last.lnum, last.col));
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001601 del_from_lnum = first.lnum;
1602 del_to_lnum = last.lnum;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001603 do_update = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001605 /* Get the position of the first byte after the deleted
1606 * section. "next" is NULL when deleting to the end of the
1607 * file. */
1608 next = off2pos(buf->bufp, off + count);
1609
1610 /* Remove part of the first line. */
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001611 if (first.col != 0
1612 || (next != NULL && first.lnum == next->lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 {
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001614 if (first.lnum != last.lnum
1615 || (next != NULL && first.lnum != next->lnum))
1616 {
1617 /* remove to the end of the first line */
1618 nb_partialremove(first.lnum, first.col,
1619 (colnr_T)MAXCOL);
1620 if (first.lnum == last.lnum)
1621 {
1622 /* Partial line to remove includes the end of
1623 * line. Join the line with the next one, have
1624 * the next line deleted below. */
1625 nb_joinlines(first.lnum, next->lnum);
1626 del_to_lnum = next->lnum;
1627 }
1628 }
1629 else
1630 {
1631 /* remove within one line */
1632 nb_partialremove(first.lnum, first.col, last.col);
1633 }
1634 ++del_from_lnum; /* don't delete the first line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 }
1636
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001637 /* Remove part of the last line. */
1638 if (first.lnum != last.lnum && next != NULL
1639 && next->col != 0 && last.lnum == next->lnum)
1640 {
1641 nb_partialremove(last.lnum, 0, last.col);
1642 if (del_from_lnum > first.lnum)
1643 {
1644 /* Join end of last line to start of first line; last
1645 * line is deleted below. */
1646 nb_joinlines(first.lnum, last.lnum);
1647 }
1648 else
1649 /* First line is deleted as a whole, keep the last
1650 * line. */
1651 --del_to_lnum;
1652 }
1653
1654 /* First is partial line; last line to remove includes
1655 * the end of line; join first line to line following last
1656 * line; line following last line is deleted below. */
1657 if (first.lnum != last.lnum && del_from_lnum > first.lnum
1658 && next != NULL && last.lnum != next->lnum)
1659 {
1660 nb_joinlines(first.lnum, next->lnum);
1661 del_to_lnum = next->lnum;
1662 }
1663
1664 /* Delete whole lines if there are any. */
1665 if (del_to_lnum >= del_from_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 {
1667 int i;
1668
1669 /* delete signs from the lines being deleted */
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001670 for (i = del_from_lnum; i <= del_to_lnum; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 {
1672 int id = buf_findsign_id(buf->bufp, (linenr_T)i);
1673 if (id > 0)
1674 {
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001675 nbdebug((" Deleting sign %d on line %d\n",
1676 id, i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 buf_delsign(buf->bufp, id);
1678 }
1679 else
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001680 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 nbdebug((" No sign on line %d\n", i));
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 }
1684
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001685 nbdebug((" Deleting lines %d through %d\n",
1686 del_from_lnum, del_to_lnum));
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001687 curwin->w_cursor.lnum = del_from_lnum;
1688 curwin->w_cursor.col = 0;
1689 del_lines(del_to_lnum - del_from_lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 }
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001691
1692 /* Leave cursor at first deleted byte. */
1693 curwin->w_cursor = first;
1694 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 buf->bufp->b_changed = wasChanged; /* logically unchanged */
1696 netbeansFireChanges = oldFire;
1697 netbeansSuppressNoLines = oldSuppress;
1698
1699 u_blockfree(buf->bufp);
1700 u_clearall(buf->bufp);
1701 }
1702 nb_reply_nil(cmdno);
1703/* =====================================================================*/
1704 }
1705 else if (streq((char *)cmd, "insert"))
1706 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 char_u *to_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708
1709 if (skip >= SKIP_STOP)
1710 {
1711 nbdebug((" Skipping %s command\n", (char *) cmd));
1712 nb_reply_nil(cmdno);
1713 return OK;
1714 }
1715
1716 /* get offset */
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001717 cp = (char *)args;
1718 off = strtol(cp, &cp, 10);
1719 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720
1721 /* get text to be inserted */
1722 args = skipwhite(args);
1723 args = to_free = (char_u *)nb_unquote(args, NULL);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001724 /*
1725 nbdebug((" CHUNK[%d]: %d bytes at offset %d\n",
1726 buf->bufp->b_ml.ml_line_count, STRLEN(args), off));
1727 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728
1729 if (buf == NULL || buf->bufp == NULL)
1730 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001731 nbdebug((" invalid buffer identifier in insert\n"));
1732 EMSG("E635: invalid buffer identifier in insert");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 retval = FAIL;
1734 }
1735 else if (args != NULL)
1736 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001737 int ff_detected = EOL_UNKNOWN;
1738 int buf_was_empty = (buf->bufp->b_ml.ml_flags & ML_EMPTY);
1739 size_t len = 0;
1740 int added = 0;
1741 int oldFire = netbeansFireChanges;
1742 int old_b_changed;
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001743 char_u *nlp;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001744 linenr_T lnum;
1745 linenr_T lnum_start;
1746 pos_T *pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 netbeansFireChanges = 0;
1749
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001750 /* Jump to the buffer where we insert. After this "curbuf"
1751 * can be used. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001752 nb_set_curbuf(buf->bufp);
Bram Moolenaara3227e22006-03-08 21:32:40 +00001753 old_b_changed = curbuf->b_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001755 /* Convert the specified character offset into a lnum/col
1756 * position. */
Bram Moolenaara3227e22006-03-08 21:32:40 +00001757 pos = off2pos(curbuf, off);
1758 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001760 if (pos->lnum <= 0)
1761 lnum_start = 1;
1762 else
1763 lnum_start = pos->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 }
1765 else
1766 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001767 /* If the given position is not found, assume we want
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 * the end of the file. See setLocAndSize HACK. */
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001769 if (buf_was_empty)
1770 lnum_start = 1; /* above empty line */
1771 else
1772 lnum_start = curbuf->b_ml.ml_line_count + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001773 }
1774
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001775 /* "lnum" is the line where we insert: either append to it or
1776 * insert a new line above it. */
1777 lnum = lnum_start;
1778
1779 /* Loop over the "\n" separated lines of the argument. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001780 do_update = 1;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001781 while (*args != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 {
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001783 nlp = vim_strchr(args, '\n');
1784 if (nlp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001786 /* Incomplete line, probably truncated. Next "insert"
1787 * command should append to this one. */
1788 len = STRLEN(args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001790 else
1791 {
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001792 len = nlp - args;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001793
1794 /*
1795 * We need to detect EOL style, because the commands
1796 * use a character offset.
1797 */
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001798 if (nlp > args && nlp[-1] == '\r')
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001799 {
1800 ff_detected = EOL_DOS;
1801 --len;
1802 }
1803 else
1804 ff_detected = EOL_UNIX;
1805 }
1806 args[len] = NUL;
1807
1808 if (lnum == lnum_start
1809 && ((pos != NULL && pos->col > 0)
1810 || (lnum == 1 && buf_was_empty)))
1811 {
1812 char_u *oldline = ml_get(lnum);
1813 char_u *newline;
1814
Bram Moolenaare4365282012-04-20 19:47:05 +02001815 /* Insert halfway a line. */
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001816 newline = alloc_check(
1817 (unsigned)(STRLEN(oldline) + len + 1));
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001818 if (newline != NULL)
1819 {
Bram Moolenaare4365282012-04-20 19:47:05 +02001820 mch_memmove(newline, oldline, (size_t)pos->col);
1821 newline[pos->col] = NUL;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001822 STRCAT(newline, args);
Bram Moolenaare4365282012-04-20 19:47:05 +02001823 STRCAT(newline, oldline + pos->col);
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001824 ml_replace(lnum, newline, FALSE);
1825 }
1826 }
1827 else
1828 {
1829 /* Append a new line. Not that we always do this,
1830 * also when the text doesn't end in a "\n". */
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001831 ml_append((linenr_T)(lnum - 1), args,
1832 (colnr_T)(len + 1), FALSE);
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001833 ++added;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001834 }
1835
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001836 if (nlp == NULL)
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001837 break;
1838 ++lnum;
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001839 args = nlp + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001840 }
1841
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001842 /* Adjust the marks below the inserted lines. */
1843 appended_lines_mark(lnum_start - 1, (long)added);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001845 /*
1846 * When starting with an empty buffer set the fileformat.
1847 * This is just guessing...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 */
1849 if (buf_was_empty)
1850 {
1851 if (ff_detected == EOL_UNKNOWN)
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001852#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 ff_detected = EOL_DOS;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001854#else
1855 ff_detected = EOL_UNIX;
1856#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 set_fileformat(ff_detected, OPT_LOCAL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00001858 curbuf->b_start_ffc = *curbuf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 }
1860
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 /*
1862 * XXX - GRP - Is the next line right? If I've inserted
1863 * text the buffer has been updated but not written. Will
1864 * netbeans guarantee to write it? Even if I do a :q! ?
1865 */
Bram Moolenaara3227e22006-03-08 21:32:40 +00001866 curbuf->b_changed = old_b_changed; /* logically unchanged */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 netbeansFireChanges = oldFire;
1868
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001869 /* Undo info is invalid now... */
Bram Moolenaara3227e22006-03-08 21:32:40 +00001870 u_blockfree(curbuf);
1871 u_clearall(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 }
1873 vim_free(to_free);
1874 nb_reply_nil(cmdno); /* or !error */
1875 }
1876 else
1877 {
1878 nbdebug(("UNIMPLEMENTED FUNCTION: %s\n", cmd));
1879 nb_reply_nil(cmdno);
1880 retval = FAIL;
1881 }
1882 }
1883 else /* Not a function; no reply required. */
1884 {
1885/* =====================================================================*/
1886 if (streq((char *)cmd, "create"))
1887 {
1888 /* Create a buffer without a name. */
1889 if (buf == NULL)
1890 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001891 nbdebug((" invalid buffer identifier in create\n"));
1892 EMSG("E636: invalid buffer identifier in create");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 return FAIL;
1894 }
1895 vim_free(buf->displayname);
1896 buf->displayname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897
1898 netbeansReadFile = 0; /* don't try to open disk file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001899 do_ecmd(0, NULL, 0, 0, ECMD_ONE, ECMD_HIDE + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 netbeansReadFile = 1;
1901 buf->bufp = curbuf;
1902 maketitle();
Bram Moolenaar009b2592004-10-24 19:18:58 +00001903 buf->insertDone = FALSE;
Bram Moolenaar67c53842010-05-22 18:28:27 +02001904#if defined(FEAT_MENU) && defined(FEAT_GUI)
Bram Moolenaard54a6882010-08-09 22:49:00 +02001905 if (gui.in_use)
1906 gui_update_menus(0);
Bram Moolenaar67c53842010-05-22 18:28:27 +02001907#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908/* =====================================================================*/
1909 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001910 else if (streq((char *)cmd, "insertDone"))
1911 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001912 if (buf == NULL || buf->bufp == NULL)
1913 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001914 nbdebug((" invalid buffer identifier in insertDone\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001915 }
1916 else
1917 {
1918 buf->bufp->b_start_eol = *args == 'T';
1919 buf->insertDone = TRUE;
1920 args += 2;
1921 buf->bufp->b_p_ro = *args == 'T';
1922 print_read_msg(buf);
1923 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001924/* =====================================================================*/
1925 }
1926 else if (streq((char *)cmd, "saveDone"))
1927 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001928 long savedChars = atol((char *)args);
1929
1930 if (buf == NULL || buf->bufp == NULL)
1931 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001932 nbdebug((" invalid buffer identifier in saveDone\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001933 }
1934 else
1935 print_save_msg(buf, savedChars);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001936/* =====================================================================*/
1937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 else if (streq((char *)cmd, "startDocumentListen"))
1939 {
1940 if (buf == NULL)
1941 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001942 nbdebug((" invalid buffer identifier in startDocumentListen\n"));
1943 EMSG("E637: invalid buffer identifier in startDocumentListen");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 return FAIL;
1945 }
1946 buf->fireChanges = 1;
1947/* =====================================================================*/
1948 }
1949 else if (streq((char *)cmd, "stopDocumentListen"))
1950 {
1951 if (buf == NULL)
1952 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001953 nbdebug((" invalid buffer identifier in stopDocumentListen\n"));
1954 EMSG("E638: invalid buffer identifier in stopDocumentListen");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 return FAIL;
1956 }
1957 buf->fireChanges = 0;
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001958 if (buf->bufp != NULL && buf->bufp->b_was_netbeans_file)
Bram Moolenaar009b2592004-10-24 19:18:58 +00001959 {
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001960 if (!buf->bufp->b_netbeans_file)
Bram Moolenaarf2330482008-06-24 20:19:36 +00001961 {
1962 nbdebug(("E658: NetBeans connection lost for buffer %ld\n", buf->bufp->b_fnum));
Bram Moolenaar009b2592004-10-24 19:18:58 +00001963 EMSGN(_("E658: NetBeans connection lost for buffer %ld"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 buf->bufp->b_fnum);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001965 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001966 else
1967 {
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001968 /* NetBeans uses stopDocumentListen when it stops editing
1969 * a file. It then expects the buffer in Vim to
1970 * disappear. */
1971 do_bufdel(DOBUF_DEL, (char_u *)"", 1,
1972 buf->bufp->b_fnum, buf->bufp->b_fnum, TRUE);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001973 vim_memset(buf, 0, sizeof(nbbuf_T));
1974 }
1975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976/* =====================================================================*/
1977 }
1978 else if (streq((char *)cmd, "setTitle"))
1979 {
1980 if (buf == NULL)
1981 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001982 nbdebug((" invalid buffer identifier in setTitle\n"));
1983 EMSG("E639: invalid buffer identifier in setTitle");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 return FAIL;
1985 }
1986 vim_free(buf->displayname);
1987 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988/* =====================================================================*/
1989 }
1990 else if (streq((char *)cmd, "initDone"))
1991 {
1992 if (buf == NULL || buf->bufp == NULL)
1993 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001994 nbdebug((" invalid buffer identifier in initDone\n"));
1995 EMSG("E640: invalid buffer identifier in initDone");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 return FAIL;
1997 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001998 do_update = 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001999 buf->initDone = TRUE;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002000 nb_set_curbuf(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001#if defined(FEAT_AUTOCMD)
2002 apply_autocmds(EVENT_BUFREADPOST, 0, 0, FALSE, buf->bufp);
2003#endif
2004
2005 /* handle any postponed key commands */
2006 handle_key_queue();
2007/* =====================================================================*/
2008 }
2009 else if (streq((char *)cmd, "setBufferNumber")
2010 || streq((char *)cmd, "putBufferNumber"))
2011 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002012 char_u *path;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 buf_T *bufp;
2014
2015 if (buf == NULL)
2016 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002017 nbdebug((" invalid buffer identifier in setBufferNumber\n"));
2018 EMSG("E641: invalid buffer identifier in setBufferNumber");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 return FAIL;
2020 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002021 path = (char_u *)nb_unquote(args, NULL);
2022 if (path == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 return FAIL;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002024 bufp = buflist_findname(path);
2025 vim_free(path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 if (bufp == NULL)
2027 {
Bram Moolenaarec906222009-02-21 21:14:00 +00002028 nbdebug((" File %s not found in setBufferNumber\n", args));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 EMSG2("E642: File %s not found in setBufferNumber", args);
2030 return FAIL;
2031 }
2032 buf->bufp = bufp;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002033 buf->nbbuf_number = bufp->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034
2035 /* "setBufferNumber" has the side effect of jumping to the buffer
2036 * (don't know why!). Don't do that for "putBufferNumber". */
2037 if (*cmd != 'p')
2038 coloncmd(":buffer %d", bufp->b_fnum);
2039 else
2040 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002041 buf->initDone = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042
2043 /* handle any postponed key commands */
2044 handle_key_queue();
2045 }
2046
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047/* =====================================================================*/
2048 }
2049 else if (streq((char *)cmd, "setFullName"))
2050 {
2051 if (buf == NULL)
2052 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002053 nbdebug((" invalid buffer identifier in setFullName\n"));
2054 EMSG("E643: invalid buffer identifier in setFullName");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 return FAIL;
2056 }
2057 vim_free(buf->displayname);
2058 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059
2060 netbeansReadFile = 0; /* don't try to open disk file */
2061 do_ecmd(0, (char_u *)buf->displayname, 0, 0, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002062 ECMD_HIDE + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 netbeansReadFile = 1;
2064 buf->bufp = curbuf;
2065 maketitle();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002066#if defined(FEAT_MENU) && defined(FEAT_GUI)
Bram Moolenaard54a6882010-08-09 22:49:00 +02002067 if (gui.in_use)
2068 gui_update_menus(0);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002069#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070/* =====================================================================*/
2071 }
2072 else if (streq((char *)cmd, "editFile"))
2073 {
2074 if (buf == NULL)
2075 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002076 nbdebug((" invalid buffer identifier in editFile\n"));
2077 EMSG("E644: invalid buffer identifier in editFile");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 return FAIL;
2079 }
2080 /* Edit a file: like create + setFullName + read the file. */
2081 vim_free(buf->displayname);
2082 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 do_ecmd(0, (char_u *)buf->displayname, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002084 ECMD_HIDE + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 buf->bufp = curbuf;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002086 buf->initDone = TRUE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002087 do_update = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088#if defined(FEAT_TITLE)
2089 maketitle();
2090#endif
Bram Moolenaar67c53842010-05-22 18:28:27 +02002091#if defined(FEAT_MENU) && defined(FEAT_GUI)
Bram Moolenaard54a6882010-08-09 22:49:00 +02002092 if (gui.in_use)
2093 gui_update_menus(0);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002094#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095/* =====================================================================*/
2096 }
2097 else if (streq((char *)cmd, "setVisible"))
2098 {
2099 if (buf == NULL || buf->bufp == NULL)
2100 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002101 nbdebug((" invalid buffer identifier in setVisible\n"));
2102 /* This message was commented out, probably because it can
2103 * happen when shutting down. */
2104 if (p_verbose > 0)
2105 EMSG("E645: invalid buffer identifier in setVisible");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 return FAIL;
2107 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002108 if (streq((char *)args, "T") && buf->bufp != curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 {
2110 exarg_T exarg;
2111 exarg.cmd = (char_u *)"goto";
2112 exarg.forceit = FALSE;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002113 dosetvisible = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 goto_buffer(&exarg, DOBUF_FIRST, FORWARD, buf->bufp->b_fnum);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002115 do_update = 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002116 dosetvisible = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117
Bram Moolenaar67c53842010-05-22 18:28:27 +02002118#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 /* Side effect!!!. */
Bram Moolenaard54a6882010-08-09 22:49:00 +02002120 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 gui_mch_set_foreground();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002122#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124/* =====================================================================*/
2125 }
2126 else if (streq((char *)cmd, "raise"))
2127 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002128#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 /* Bring gvim to the foreground. */
Bram Moolenaard54a6882010-08-09 22:49:00 +02002130 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 gui_mch_set_foreground();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002132#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133/* =====================================================================*/
2134 }
2135 else if (streq((char *)cmd, "setModified"))
2136 {
Bram Moolenaarff064e12008-06-09 13:10:45 +00002137 int prev_b_changed;
2138
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 if (buf == NULL || buf->bufp == NULL)
2140 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002141 nbdebug((" invalid buffer identifier in setModified\n"));
2142 /* This message was commented out, probably because it can
2143 * happen when shutting down. */
2144 if (p_verbose > 0)
2145 EMSG("E646: invalid buffer identifier in setModified");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 return FAIL;
2147 }
Bram Moolenaarff064e12008-06-09 13:10:45 +00002148 prev_b_changed = buf->bufp->b_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 if (streq((char *)args, "T"))
Bram Moolenaarff064e12008-06-09 13:10:45 +00002150 buf->bufp->b_changed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 else
2152 {
2153 struct stat st;
2154
2155 /* Assume NetBeans stored the file. Reset the timestamp to
2156 * avoid "file changed" warnings. */
2157 if (buf->bufp->b_ffname != NULL
2158 && mch_stat((char *)buf->bufp->b_ffname, &st) >= 0)
2159 buf_store_time(buf->bufp, &st, buf->bufp->b_ffname);
Bram Moolenaarff064e12008-06-09 13:10:45 +00002160 buf->bufp->b_changed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 }
2162 buf->modified = buf->bufp->b_changed;
Bram Moolenaarff064e12008-06-09 13:10:45 +00002163 if (prev_b_changed != buf->bufp->b_changed)
2164 {
2165#ifdef FEAT_WINDOWS
2166 check_status(buf->bufp);
2167 redraw_tabline = TRUE;
2168#endif
2169#ifdef FEAT_TITLE
2170 maketitle();
2171#endif
2172 update_screen(0);
2173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174/* =====================================================================*/
2175 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002176 else if (streq((char *)cmd, "setModtime"))
2177 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002178 if (buf == NULL || buf->bufp == NULL)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002179 nbdebug((" invalid buffer identifier in setModtime\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002180 else
2181 buf->bufp->b_mtime = atoi((char *)args);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002182/* =====================================================================*/
2183 }
2184 else if (streq((char *)cmd, "setReadOnly"))
2185 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002186 if (buf == NULL || buf->bufp == NULL)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002187 nbdebug((" invalid buffer identifier in setReadOnly\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002188 else if (streq((char *)args, "T"))
Bram Moolenaar009b2592004-10-24 19:18:58 +00002189 buf->bufp->b_p_ro = TRUE;
2190 else
2191 buf->bufp->b_p_ro = FALSE;
2192/* =====================================================================*/
2193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 else if (streq((char *)cmd, "setMark"))
2195 {
2196 /* not yet */
2197/* =====================================================================*/
2198 }
2199 else if (streq((char *)cmd, "showBalloon"))
2200 {
2201#if defined(FEAT_BEVAL)
2202 static char *text = NULL;
2203
2204 /*
2205 * Set up the Balloon Expression Evaluation area.
2206 * Ignore 'ballooneval' here.
2207 * The text pointer must remain valid for a while.
2208 */
2209 if (balloonEval != NULL)
2210 {
2211 vim_free(text);
2212 text = nb_unquote(args, NULL);
2213 if (text != NULL)
2214 gui_mch_post_balloon(balloonEval, (char_u *)text);
2215 }
2216#endif
2217/* =====================================================================*/
2218 }
2219 else if (streq((char *)cmd, "setDot"))
2220 {
2221 pos_T *pos;
2222#ifdef NBDEBUG
2223 char_u *s;
2224#endif
2225
2226 if (buf == NULL || buf->bufp == NULL)
2227 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002228 nbdebug((" invalid buffer identifier in setDot\n"));
2229 EMSG("E647: invalid buffer identifier in setDot");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 return FAIL;
2231 }
2232
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002233 nb_set_curbuf(buf->bufp);
2234
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 /* Don't want Visual mode now. */
2236 if (VIsual_active)
2237 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238#ifdef NBDEBUG
2239 s = args;
2240#endif
2241 pos = get_off_or_lnum(buf->bufp, &args);
2242 if (pos)
2243 {
2244 curwin->w_cursor = *pos;
2245 check_cursor();
2246#ifdef FEAT_FOLDING
2247 foldOpenCursor();
2248#endif
2249 }
2250 else
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002251 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 nbdebug((" BAD POSITION in setDot: %s\n", s));
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254
2255 /* gui_update_cursor(TRUE, FALSE); */
2256 /* update_curbuf(NOT_VALID); */
2257 update_topline(); /* scroll to show the line */
2258 update_screen(VALID);
2259 setcursor();
Bram Moolenaar96bcc5e2011-04-01 15:33:59 +02002260 cursor_on();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 out_flush();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002262#ifdef FEAT_GUI
Bram Moolenaard54a6882010-08-09 22:49:00 +02002263 if (gui.in_use)
2264 {
2265 gui_update_cursor(TRUE, FALSE);
2266 gui_mch_flush();
2267 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02002268#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 /* Quit a hit-return or more prompt. */
2270 if (State == HITRETURN || State == ASKMORE)
2271 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272#ifdef FEAT_GUI_GTK
Bram Moolenaard54a6882010-08-09 22:49:00 +02002273 if (gui.in_use && gtk_main_level() > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 gtk_main_quit();
2275#endif
2276 }
2277/* =====================================================================*/
2278 }
2279 else if (streq((char *)cmd, "close"))
2280 {
2281#ifdef NBDEBUG
2282 char *name = "<NONE>";
2283#endif
2284
2285 if (buf == NULL)
2286 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002287 nbdebug((" invalid buffer identifier in close\n"));
2288 EMSG("E648: invalid buffer identifier in close");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 return FAIL;
2290 }
2291
2292#ifdef NBDEBUG
2293 if (buf->displayname != NULL)
2294 name = buf->displayname;
2295#endif
Bram Moolenaarf2330482008-06-24 20:19:36 +00002296 if (buf->bufp == NULL)
2297 {
2298 nbdebug((" invalid buffer identifier in close\n"));
2299 /* This message was commented out, probably because it can
2300 * happen when shutting down. */
2301 if (p_verbose > 0)
2302 EMSG("E649: invalid buffer identifier in close");
2303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 nbdebug((" CLOSE %d: %s\n", bufno, name));
Bram Moolenaar67c53842010-05-22 18:28:27 +02002305#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 need_mouse_correct = TRUE;
Bram Moolenaar67c53842010-05-22 18:28:27 +02002307#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 if (buf->bufp != NULL)
2309 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD,
2310 buf->bufp->b_fnum, TRUE);
Bram Moolenaar8d602722006-08-08 19:34:19 +00002311 buf->bufp = NULL;
2312 buf->initDone = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002313 do_update = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314/* =====================================================================*/
2315 }
2316 else if (streq((char *)cmd, "setStyle")) /* obsolete... */
2317 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002318 nbdebug((" setStyle is obsolete!\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319/* =====================================================================*/
2320 }
2321 else if (streq((char *)cmd, "setExitDelay"))
2322 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002323 /* Only used in version 2.1. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324/* =====================================================================*/
2325 }
2326 else if (streq((char *)cmd, "defineAnnoType"))
2327 {
2328#ifdef FEAT_SIGNS
2329 int typeNum;
2330 char_u *typeName;
2331 char_u *tooltip;
2332 char_u *p;
2333 char_u *glyphFile;
Bram Moolenaar67c53842010-05-22 18:28:27 +02002334 int parse_error = FALSE;
2335 char_u *fg;
2336 char_u *bg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337
2338 if (buf == NULL)
2339 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002340 nbdebug((" invalid buffer identifier in defineAnnoType\n"));
2341 EMSG("E650: invalid buffer identifier in defineAnnoType");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 return FAIL;
2343 }
2344
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002345 cp = (char *)args;
2346 typeNum = strtol(cp, &cp, 10);
2347 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 args = skipwhite(args);
2349 typeName = (char_u *)nb_unquote(args, &args);
2350 args = skipwhite(args + 1);
2351 tooltip = (char_u *)nb_unquote(args, &args);
2352 args = skipwhite(args + 1);
2353
2354 p = (char_u *)nb_unquote(args, &args);
2355 glyphFile = vim_strsave_escaped(p, escape_chars);
2356 vim_free(p);
2357
2358 args = skipwhite(args + 1);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002359 p = skiptowhite(args);
2360 if (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002362 *p = NUL;
2363 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02002365 fg = vim_strsave(args);
2366 bg = vim_strsave(p);
2367 if (STRLEN(fg) > MAX_COLOR_LENGTH || STRLEN(bg) > MAX_COLOR_LENGTH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002369 EMSG("E532: highlighting color name too long in defineAnnoType");
2370 vim_free(typeName);
2371 parse_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02002373 else if (typeName != NULL && tooltip != NULL && glyphFile != NULL)
2374 addsigntype(buf, typeNum, typeName, tooltip, glyphFile, fg, bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 else
2376 vim_free(typeName);
2377
2378 /* don't free typeName; it's used directly in addsigntype() */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002379 vim_free(fg);
2380 vim_free(bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 vim_free(tooltip);
2382 vim_free(glyphFile);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002383 if (parse_error)
2384 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385
2386#endif
2387/* =====================================================================*/
2388 }
2389 else if (streq((char *)cmd, "addAnno"))
2390 {
2391#ifdef FEAT_SIGNS
2392 int serNum;
2393 int localTypeNum;
2394 int typeNum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 pos_T *pos;
2396
2397 if (buf == NULL || buf->bufp == NULL)
2398 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002399 nbdebug((" invalid buffer identifier in addAnno\n"));
2400 EMSG("E651: invalid buffer identifier in addAnno");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 return FAIL;
2402 }
2403
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002404 do_update = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002406 cp = (char *)args;
2407 serNum = strtol(cp, &cp, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408
2409 /* Get the typenr specific for this buffer and convert it to
2410 * the global typenumber, as used for the sign name. */
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002411 localTypeNum = strtol(cp, &cp, 10);
2412 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 typeNum = mapsigntype(buf, localTypeNum);
2414
2415 pos = get_off_or_lnum(buf->bufp, &args);
2416
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002417 cp = (char *)args;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002418 ignored = (int)strtol(cp, &cp, 10);
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002419 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420# ifdef NBDEBUG
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002421 if (ignored != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002423 nbdebug((" partial line annotation -- Not Yet Implemented!\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 }
2425# endif
2426 if (serNum >= GUARDEDOFFSET)
2427 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002428 nbdebug((" too many annotations! ignoring...\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 return FAIL;
2430 }
2431 if (pos)
2432 {
Bram Moolenaarec906222009-02-21 21:14:00 +00002433 coloncmd(":sign place %d line=%ld name=%d buffer=%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 serNum, pos->lnum, typeNum, buf->bufp->b_fnum);
2435 if (typeNum == curPCtype)
2436 coloncmd(":sign jump %d buffer=%d", serNum,
2437 buf->bufp->b_fnum);
2438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439#endif
2440/* =====================================================================*/
2441 }
2442 else if (streq((char *)cmd, "removeAnno"))
2443 {
2444#ifdef FEAT_SIGNS
2445 int serNum;
2446
2447 if (buf == NULL || buf->bufp == NULL)
2448 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002449 nbdebug((" invalid buffer identifier in removeAnno\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 return FAIL;
2451 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002452 do_update = 1;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002453 cp = (char *)args;
2454 serNum = strtol(cp, &cp, 10);
2455 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 coloncmd(":sign unplace %d buffer=%d",
2457 serNum, buf->bufp->b_fnum);
2458 redraw_buf_later(buf->bufp, NOT_VALID);
2459#endif
2460/* =====================================================================*/
2461 }
2462 else if (streq((char *)cmd, "moveAnnoToFront"))
2463 {
2464#ifdef FEAT_SIGNS
Bram Moolenaarf2330482008-06-24 20:19:36 +00002465 nbdebug((" moveAnnoToFront: Not Yet Implemented!\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466#endif
2467/* =====================================================================*/
2468 }
2469 else if (streq((char *)cmd, "guard") || streq((char *)cmd, "unguard"))
2470 {
2471 int len;
2472 pos_T first;
2473 pos_T last;
2474 pos_T *pos;
2475 int un = (cmd[0] == 'u');
2476 static int guardId = GUARDEDOFFSET;
2477
2478 if (skip >= SKIP_STOP)
2479 {
2480 nbdebug((" Skipping %s command\n", (char *) cmd));
2481 return OK;
2482 }
2483
2484 nb_init_graphics();
2485
2486 if (buf == NULL || buf->bufp == NULL)
2487 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002488 nbdebug((" invalid buffer identifier in %s command\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 return FAIL;
2490 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002491 nb_set_curbuf(buf->bufp);
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002492 cp = (char *)args;
2493 off = strtol(cp, &cp, 10);
2494 len = strtol(cp, NULL, 10);
2495 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 pos = off2pos(buf->bufp, off);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002497 do_update = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 if (!pos)
2499 nbdebug((" no such start pos in %s, %ld\n", cmd, off));
2500 else
2501 {
2502 first = *pos;
2503 pos = off2pos(buf->bufp, off + len - 1);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002504 if (pos != NULL && pos->col == 0)
2505 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 /*
2507 * In Java Swing the offset is a position between 2
2508 * characters. If col == 0 then we really want the
2509 * previous line as the end.
2510 */
2511 pos = off2pos(buf->bufp, off + len - 2);
2512 }
2513 if (!pos)
2514 nbdebug((" no such end pos in %s, %ld\n",
2515 cmd, off + len - 1));
2516 else
2517 {
2518 long lnum;
2519 last = *pos;
2520 /* set highlight for region */
2521 nbdebug((" %sGUARD %ld,%d to %ld,%d\n", (un) ? "UN" : "",
2522 first.lnum, first.col,
2523 last.lnum, last.col));
2524#ifdef FEAT_SIGNS
2525 for (lnum = first.lnum; lnum <= last.lnum; lnum++)
2526 {
2527 if (un)
2528 {
2529 /* never used */
2530 }
2531 else
2532 {
2533 if (buf_findsigntype_id(buf->bufp, lnum,
2534 GUARDED) == 0)
2535 {
2536 coloncmd(
Bram Moolenaarec906222009-02-21 21:14:00 +00002537 ":sign place %d line=%ld name=%d buffer=%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 guardId++, lnum, GUARDED,
2539 buf->bufp->b_fnum);
2540 }
2541 }
2542 }
2543#endif
2544 redraw_buf_later(buf->bufp, NOT_VALID);
2545 }
2546 }
2547/* =====================================================================*/
2548 }
2549 else if (streq((char *)cmd, "startAtomic"))
2550 {
2551 inAtomic = 1;
2552/* =====================================================================*/
2553 }
2554 else if (streq((char *)cmd, "endAtomic"))
2555 {
2556 inAtomic = 0;
2557 if (needupdate)
2558 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002559 do_update = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 needupdate = 0;
2561 }
2562/* =====================================================================*/
2563 }
2564 else if (streq((char *)cmd, "save"))
2565 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002566 /*
2567 * NOTE - This command is obsolete wrt NetBeans. Its left in
2568 * only for historical reasons.
2569 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 if (buf == NULL || buf->bufp == NULL)
2571 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002572 nbdebug((" invalid buffer identifier in %s command\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 return FAIL;
2574 }
2575
2576 /* the following is taken from ex_cmds.c (do_wqall function) */
2577 if (bufIsChanged(buf->bufp))
2578 {
2579 /* Only write if the buffer can be written. */
2580 if (p_write
2581 && !buf->bufp->b_p_ro
2582 && buf->bufp->b_ffname != NULL
2583#ifdef FEAT_QUICKFIX
2584 && !bt_dontwrite(buf->bufp)
2585#endif
2586 )
2587 {
2588 buf_write_all(buf->bufp, FALSE);
2589#ifdef FEAT_AUTOCMD
2590 /* an autocommand may have deleted the buffer */
2591 if (!buf_valid(buf->bufp))
2592 buf->bufp = NULL;
2593#endif
2594 }
2595 }
Bram Moolenaarf2330482008-06-24 20:19:36 +00002596 else
2597 {
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002598 nbdebug((" Buffer has no changes!\n"));
Bram Moolenaarf2330482008-06-24 20:19:36 +00002599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600/* =====================================================================*/
2601 }
2602 else if (streq((char *)cmd, "netbeansBuffer"))
2603 {
2604 if (buf == NULL || buf->bufp == NULL)
2605 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002606 nbdebug((" invalid buffer identifier in %s command\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 return FAIL;
2608 }
2609 if (*args == 'T')
2610 {
2611 buf->bufp->b_netbeans_file = TRUE;
2612 buf->bufp->b_was_netbeans_file = TRUE;
2613 }
2614 else
2615 buf->bufp->b_netbeans_file = FALSE;
2616/* =====================================================================*/
2617 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002618 else if (streq((char *)cmd, "specialKeys"))
2619 {
2620 special_keys(args);
2621/* =====================================================================*/
2622 }
2623 else if (streq((char *)cmd, "actionMenuItem"))
2624 {
2625 /* not used yet */
2626/* =====================================================================*/
2627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 else if (streq((char *)cmd, "version"))
2629 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002630 /* not used yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 }
Bram Moolenaarf2330482008-06-24 20:19:36 +00002632 else
2633 {
2634 nbdebug(("Unrecognised command: %s\n", cmd));
2635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 /*
2637 * Unrecognized command is ignored.
2638 */
2639 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002640 if (inAtomic && do_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 {
2642 needupdate = 1;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002643 do_update = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 }
2645
Bram Moolenaar009b2592004-10-24 19:18:58 +00002646 /*
2647 * Is this needed? I moved the netbeans_Xt_connect() later during startup
2648 * and it may no longer be necessary. If its not needed then needupdate
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002649 * and do_update can also be removed.
Bram Moolenaar009b2592004-10-24 19:18:58 +00002650 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002651 if (buf != NULL && buf->initDone && do_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 {
2653 update_screen(NOT_VALID);
2654 setcursor();
Bram Moolenaar96bcc5e2011-04-01 15:33:59 +02002655 cursor_on();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 out_flush();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002657#ifdef FEAT_GUI
Bram Moolenaard54a6882010-08-09 22:49:00 +02002658 if (gui.in_use)
2659 {
2660 gui_update_cursor(TRUE, FALSE);
2661 gui_mch_flush();
2662 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02002663#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 /* Quit a hit-return or more prompt. */
2665 if (State == HITRETURN || State == ASKMORE)
2666 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667#ifdef FEAT_GUI_GTK
Bram Moolenaard54a6882010-08-09 22:49:00 +02002668 if (gui.in_use && gtk_main_level() > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 gtk_main_quit();
2670#endif
2671 }
2672 }
2673
2674 return retval;
2675}
2676
2677
2678/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002679 * If "buf" is not the current buffer try changing to a window that edits this
2680 * buffer. If there is no such window then close the current buffer and set
2681 * the current buffer as "buf".
2682 */
2683 static void
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00002684nb_set_curbuf(buf_T *buf)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002685{
2686 if (curbuf != buf && buf_jump_open_win(buf) == NULL)
2687 set_curbuf(buf, DOBUF_GOTO);
2688}
2689
2690/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 * Process a vim colon command.
2692 */
2693 static void
2694coloncmd(char *cmd, ...)
2695{
2696 char buf[1024];
2697 va_list ap;
2698
2699 va_start(ap, cmd);
Bram Moolenaar0dc79e82009-06-24 14:50:12 +00002700 vim_vsnprintf(buf, sizeof(buf), cmd, ap, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 va_end(ap);
2702
2703 nbdebug((" COLONCMD %s\n", buf));
2704
2705/* ALT_INPUT_LOCK_ON; */
2706 do_cmdline((char_u *)buf, NULL, NULL, DOCMD_NOWAIT | DOCMD_KEYTYPED);
2707/* ALT_INPUT_LOCK_OFF; */
2708
2709 setcursor(); /* restore the cursor position */
2710 out_flush(); /* make sure output has been written */
2711
Bram Moolenaar67c53842010-05-22 18:28:27 +02002712#ifdef FEAT_GUI
Bram Moolenaard54a6882010-08-09 22:49:00 +02002713 if (gui.in_use)
2714 {
2715 gui_update_cursor(TRUE, FALSE);
2716 gui_mch_flush();
2717 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02002718#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719}
2720
2721
2722/*
Bram Moolenaar009b2592004-10-24 19:18:58 +00002723 * Parse the specialKeys argument and issue the appropriate map commands.
2724 */
2725 static void
2726special_keys(char_u *args)
2727{
2728 char *save_str = nb_unquote(args, NULL);
2729 char *tok = strtok(save_str, " ");
2730 char *sep;
2731 char keybuf[64];
2732 char cmdbuf[256];
2733
2734 while (tok != NULL)
2735 {
2736 int i = 0;
2737
2738 if ((sep = strchr(tok, '-')) != NULL)
2739 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002740 *sep = NUL;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002741 while (*tok)
2742 {
2743 switch (*tok)
2744 {
2745 case 'A':
2746 case 'M':
2747 case 'C':
2748 case 'S':
2749 keybuf[i++] = *tok;
2750 keybuf[i++] = '-';
2751 break;
2752 }
2753 tok++;
2754 }
2755 tok++;
2756 }
2757
2758 strcpy(&keybuf[i], tok);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002759 vim_snprintf(cmdbuf, sizeof(cmdbuf),
2760 "<silent><%s> :nbkey %s<CR>", keybuf, keybuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002761 do_map(0, (char_u *)cmdbuf, NORMAL, FALSE);
2762 tok = strtok(NULL, " ");
2763 }
2764 vim_free(save_str);
2765}
2766
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002767 void
2768ex_nbclose(eap)
2769 exarg_T *eap UNUSED;
2770{
2771 netbeans_close();
2772}
Bram Moolenaar009b2592004-10-24 19:18:58 +00002773
2774 void
2775ex_nbkey(eap)
2776 exarg_T *eap;
2777{
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002778 (void)netbeans_keystring(eap->arg);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002779}
2780
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002781 void
2782ex_nbstart(eap)
2783 exarg_T *eap;
2784{
Bram Moolenaarc2a406b2010-09-30 21:03:26 +02002785#ifdef FEAT_GUI
2786# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar7ad7d012010-11-16 15:49:02 +01002787 && !defined(FEAT_GUI_W32)
Bram Moolenaarc2a406b2010-09-30 21:03:26 +02002788 if (gui.in_use)
2789 {
Bram Moolenaar7ad7d012010-11-16 15:49:02 +01002790 EMSG(_("E838: netbeans is not supported with this GUI"));
2791 return;
Bram Moolenaarc2a406b2010-09-30 21:03:26 +02002792 }
2793# endif
2794#endif
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002795 netbeans_open((char *)eap->arg, FALSE);
2796}
Bram Moolenaar009b2592004-10-24 19:18:58 +00002797
2798/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 * Initialize highlights and signs for use by netbeans (mostly obsolete)
2800 */
2801 static void
2802nb_init_graphics(void)
2803{
2804 static int did_init = FALSE;
2805
2806 if (!did_init)
2807 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002808 coloncmd(":highlight NBGuarded guibg=Cyan guifg=Black"
2809 " ctermbg=LightCyan ctermfg=Black");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 coloncmd(":sign define %d linehl=NBGuarded", GUARDED);
2811
2812 did_init = TRUE;
2813 }
2814}
2815
2816/*
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002817 * Convert key to netbeans name. This uses the global "mod_mask".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 */
2819 static void
2820netbeans_keyname(int key, char *buf)
2821{
2822 char *name = 0;
2823 char namebuf[2];
2824 int ctrl = 0;
2825 int shift = 0;
2826 int alt = 0;
2827
2828 if (mod_mask & MOD_MASK_CTRL)
2829 ctrl = 1;
2830 if (mod_mask & MOD_MASK_SHIFT)
2831 shift = 1;
2832 if (mod_mask & MOD_MASK_ALT)
2833 alt = 1;
2834
2835
2836 switch (key)
2837 {
2838 case K_F1: name = "F1"; break;
2839 case K_S_F1: name = "F1"; shift = 1; break;
2840 case K_F2: name = "F2"; break;
2841 case K_S_F2: name = "F2"; shift = 1; break;
2842 case K_F3: name = "F3"; break;
2843 case K_S_F3: name = "F3"; shift = 1; break;
2844 case K_F4: name = "F4"; break;
2845 case K_S_F4: name = "F4"; shift = 1; break;
2846 case K_F5: name = "F5"; break;
2847 case K_S_F5: name = "F5"; shift = 1; break;
2848 case K_F6: name = "F6"; break;
2849 case K_S_F6: name = "F6"; shift = 1; break;
2850 case K_F7: name = "F7"; break;
2851 case K_S_F7: name = "F7"; shift = 1; break;
2852 case K_F8: name = "F8"; break;
2853 case K_S_F8: name = "F8"; shift = 1; break;
2854 case K_F9: name = "F9"; break;
2855 case K_S_F9: name = "F9"; shift = 1; break;
2856 case K_F10: name = "F10"; break;
2857 case K_S_F10: name = "F10"; shift = 1; break;
2858 case K_F11: name = "F11"; break;
2859 case K_S_F11: name = "F11"; shift = 1; break;
2860 case K_F12: name = "F12"; break;
2861 case K_S_F12: name = "F12"; shift = 1; break;
2862 default:
2863 if (key >= ' ' && key <= '~')
2864 {
2865 /* Allow ASCII characters. */
2866 name = namebuf;
2867 namebuf[0] = key;
2868 namebuf[1] = NUL;
2869 }
2870 else
2871 name = "X";
2872 break;
2873 }
2874
2875 buf[0] = '\0';
2876 if (ctrl)
2877 strcat(buf, "C");
2878 if (shift)
2879 strcat(buf, "S");
2880 if (alt)
2881 strcat(buf, "M"); /* META */
2882 if (ctrl || shift || alt)
2883 strcat(buf, "-");
2884 strcat(buf, name);
2885}
2886
Bram Moolenaar67c53842010-05-22 18:28:27 +02002887#if defined(FEAT_BEVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888/*
2889 * Function to be called for balloon evaluation. Grabs the text under the
2890 * cursor and sends it to the debugger for evaluation. The debugger should
2891 * respond with a showBalloon command when there is a useful result.
2892 */
Bram Moolenaard62bec82005-03-07 22:56:57 +00002893 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894netbeans_beval_cb(
2895 BalloonEval *beval,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002896 int state UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897{
Bram Moolenaard62bec82005-03-07 22:56:57 +00002898 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 char_u *text;
Bram Moolenaard62bec82005-03-07 22:56:57 +00002900 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 int col;
Bram Moolenaard9462e32011-04-11 21:35:11 +02002902 char *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 char_u *p;
2904
2905 /* Don't do anything when 'ballooneval' is off, messages scrolled the
2906 * windows up or we have no connection. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002907 if (!p_beval || msg_scrolled > 0 || !NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 return;
2909
Bram Moolenaard62bec82005-03-07 22:56:57 +00002910 if (get_beval_info(beval, TRUE, &wp, &lnum, &text, &col) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 {
2912 /* Send debugger request. Only when the text is of reasonable
2913 * length. */
2914 if (text != NULL && text[0] != NUL && STRLEN(text) < MAXPATHL)
2915 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02002916 buf = (char *)alloc(MAXPATHL * 2 + 25);
2917 if (buf != NULL)
Bram Moolenaar009b2592004-10-24 19:18:58 +00002918 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02002919 p = nb_quote(text);
2920 if (p != NULL)
2921 {
2922 vim_snprintf(buf, MAXPATHL * 2 + 25,
2923 "0:balloonText=%d \"%s\"\n", r_cmdno, p);
2924 vim_free(p);
2925 }
2926 nbdebug(("EVT: %s", buf));
2927 nb_send(buf, "netbeans_beval_cb");
2928 vim_free(buf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 }
2931 vim_free(text);
2932 }
2933}
2934#endif
2935
2936/*
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002937 * Return TRUE when the netbeans connection is closed.
2938 */
2939 int
2940netbeans_active(void)
2941{
2942 return NETBEANS_OPEN;
2943}
2944
2945/*
Bram Moolenaar67c53842010-05-22 18:28:27 +02002946 * Return netbeans file descriptor.
2947 */
2948 int
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002949netbeans_filedesc(void)
Bram Moolenaar67c53842010-05-22 18:28:27 +02002950{
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002951 return nbsock;
Bram Moolenaar67c53842010-05-22 18:28:27 +02002952}
2953
2954#if defined(FEAT_GUI) || defined(PROTO)
2955/*
2956 * Register our file descriptor with the gui event handling system.
2957 */
2958 void
2959netbeans_gui_register(void)
2960{
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002961 if (!NB_HAS_GUI || !NETBEANS_OPEN)
Bram Moolenaar67c53842010-05-22 18:28:27 +02002962 return;
2963
Bram Moolenaar173c9852010-09-29 17:27:01 +02002964# ifdef FEAT_GUI_X11
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002965 /* tell notifier we are interested in being called
2966 * when there is input on the editor connection socket
2967 */
2968 if (inputHandler == (XtInputId)NULL)
2969 inputHandler = XtAppAddInput((XtAppContext)app_context, nbsock,
2970 (XtPointer)(XtInputReadMask + XtInputExceptMask),
2971 messageFromNetbeans, NULL);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002972# else
2973# ifdef FEAT_GUI_GTK
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002974 /*
2975 * Tell gdk we are interested in being called when there
2976 * is input on the editor connection socket
2977 */
2978 if (inputHandler == 0)
2979 inputHandler = gdk_input_add((gint)nbsock, (GdkInputCondition)
2980 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
2981 messageFromNetbeans, NULL);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002982# else
2983# ifdef FEAT_GUI_W32
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002984 /*
2985 * Tell Windows we are interested in receiving message when there
2986 * is input on the editor connection socket
2987 */
2988 if (inputHandler == -1)
2989 inputHandler = WSAAsyncSelect(nbsock, s_hwnd, WM_NETBEANS, FD_READ);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002990# endif
2991# endif
2992# endif
Bram Moolenaar67c53842010-05-22 18:28:27 +02002993
2994# ifdef FEAT_BEVAL
2995 bevalServers |= BEVAL_NETBEANS;
2996# endif
2997}
2998#endif
2999
3000/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 * Tell netbeans that the window was opened, ready for commands.
3002 */
3003 void
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003004netbeans_open(char *params, int doabort)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005{
3006 char *cmd = "0:startupDone=0\n";
3007
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003008 if (NETBEANS_OPEN)
3009 {
3010 EMSG(_("E511: netbeans already connected"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003014 if (netbeans_connect(params, doabort) != OK)
Bram Moolenaar67c53842010-05-22 18:28:27 +02003015 return;
3016#ifdef FEAT_GUI
3017 netbeans_gui_register();
Bram Moolenaard62bec82005-03-07 22:56:57 +00003018#endif
3019
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 nbdebug(("EVT: %s", cmd));
3021 nb_send(cmd, "netbeans_startup_done");
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003022
3023 /* update the screen after having added the gutter */
3024 changed_window_setting();
3025 update_screen(CLEAR);
3026 setcursor();
Bram Moolenaar96bcc5e2011-04-01 15:33:59 +02003027 cursor_on();
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003028 out_flush();
3029#ifdef FEAT_GUI
Bram Moolenaard54a6882010-08-09 22:49:00 +02003030 if (gui.in_use)
3031 {
3032 gui_update_cursor(TRUE, FALSE);
3033 gui_mch_flush();
3034 }
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003035#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036}
3037
Bram Moolenaar009b2592004-10-24 19:18:58 +00003038/*
3039 * Tell netbeans that we're exiting. This should be called right
3040 * before calling exit.
3041 */
3042 void
3043netbeans_send_disconnect()
3044{
3045 char buf[128];
3046
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003047 if (NETBEANS_OPEN)
Bram Moolenaar009b2592004-10-24 19:18:58 +00003048 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00003049 sprintf(buf, "0:disconnect=%d\n", r_cmdno);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003050 nbdebug(("EVT: %s", buf));
3051 nb_send(buf, "netbeans_disconnect");
3052 }
3053}
3054
Bram Moolenaar173c9852010-09-29 17:27:01 +02003055#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_W32) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056/*
3057 * Tell netbeans that the window was moved or resized.
3058 */
3059 void
3060netbeans_frame_moved(int new_x, int new_y)
3061{
3062 char buf[128];
3063
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003064 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 return;
3066
3067 sprintf(buf, "0:geometry=%d %d %d %d %d\n",
Bram Moolenaar89d40322006-08-29 15:30:07 +00003068 r_cmdno, (int)Columns, (int)Rows, new_x, new_y);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003069 /*nbdebug(("EVT: %s", buf)); happens too many times during a move */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 nb_send(buf, "netbeans_frame_moved");
3071}
3072#endif
3073
3074/*
Bram Moolenaar009b2592004-10-24 19:18:58 +00003075 * Tell netbeans the user opened or activated a file.
3076 */
3077 void
3078netbeans_file_activated(buf_T *bufp)
3079{
3080 int bufno = nb_getbufno(bufp);
3081 nbbuf_T *bp = nb_get_buf(bufno);
3082 char buffer[2*MAXPATHL];
3083 char_u *q;
3084
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003085 if (!NETBEANS_OPEN || !bufp->b_netbeans_file || dosetvisible)
Bram Moolenaar009b2592004-10-24 19:18:58 +00003086 return;
3087
3088 q = nb_quote(bufp->b_ffname);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003089 if (q == NULL || bp == NULL)
Bram Moolenaar009b2592004-10-24 19:18:58 +00003090 return;
3091
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003092 vim_snprintf(buffer, sizeof(buffer), "%d:fileOpened=%d \"%s\" %s %s\n",
Bram Moolenaar009b2592004-10-24 19:18:58 +00003093 bufno,
3094 bufno,
3095 (char *)q,
3096 "T", /* open in NetBeans */
3097 "F"); /* modified */
3098
3099 vim_free(q);
3100 nbdebug(("EVT: %s", buffer));
3101
3102 nb_send(buffer, "netbeans_file_opened");
3103}
3104
3105/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 * Tell netbeans the user opened a file.
3107 */
3108 void
Bram Moolenaar009b2592004-10-24 19:18:58 +00003109netbeans_file_opened(buf_T *bufp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110{
Bram Moolenaar009b2592004-10-24 19:18:58 +00003111 int bufno = nb_getbufno(bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 char buffer[2*MAXPATHL];
3113 char_u *q;
Bram Moolenaar009b2592004-10-24 19:18:58 +00003114 nbbuf_T *bp = nb_get_buf(nb_getbufno(bufp));
3115 int bnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003117 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 return;
3119
Bram Moolenaar009b2592004-10-24 19:18:58 +00003120 q = nb_quote(bufp->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 if (q == NULL)
3122 return;
Bram Moolenaar009b2592004-10-24 19:18:58 +00003123 if (bp != NULL)
3124 bnum = bufno;
3125 else
3126 bnum = 0;
3127
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003128 vim_snprintf(buffer, sizeof(buffer), "%d:fileOpened=%d \"%s\" %s %s\n",
Bram Moolenaar009b2592004-10-24 19:18:58 +00003129 bnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 0,
3131 (char *)q,
3132 "T", /* open in NetBeans */
3133 "F"); /* modified */
3134
3135 vim_free(q);
3136 nbdebug(("EVT: %s", buffer));
3137
3138 nb_send(buffer, "netbeans_file_opened");
Bram Moolenaar009b2592004-10-24 19:18:58 +00003139 if (p_acd && vim_chdirfile(bufp->b_ffname) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 shorten_fnames(TRUE);
3141}
3142
3143/*
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00003144 * Tell netbeans that a file was deleted or wiped out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 */
3146 void
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00003147netbeans_file_killed(buf_T *bufp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148{
3149 int bufno = nb_getbufno(bufp);
3150 nbbuf_T *nbbuf = nb_get_buf(bufno);
3151 char buffer[2*MAXPATHL];
3152
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003153 if (!NETBEANS_OPEN || bufno == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 return;
3155
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00003156 nbdebug(("netbeans_file_killed:\n"));
3157 nbdebug((" Killing bufno: %d", bufno));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158
Bram Moolenaar89d40322006-08-29 15:30:07 +00003159 sprintf(buffer, "%d:killed=%d\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160
3161 nbdebug(("EVT: %s", buffer));
3162
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00003163 nb_send(buffer, "netbeans_file_killed");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164
3165 if (nbbuf != NULL)
3166 nbbuf->bufp = NULL;
3167}
3168
3169/*
3170 * Get a pointer to the Netbeans buffer for Vim buffer "bufp".
3171 * Return NULL if there is no such buffer or changes are not to be reported.
3172 * Otherwise store the buffer number in "*bufnop".
3173 */
3174 static nbbuf_T *
3175nb_bufp2nbbuf_fire(buf_T *bufp, int *bufnop)
3176{
3177 int bufno;
3178 nbbuf_T *nbbuf;
3179
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003180 if (!NETBEANS_OPEN || !netbeansFireChanges)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 return NULL; /* changes are not reported at all */
3182
3183 bufno = nb_getbufno(bufp);
3184 if (bufno <= 0)
3185 return NULL; /* file is not known to NetBeans */
3186
3187 nbbuf = nb_get_buf(bufno);
3188 if (nbbuf != NULL && !nbbuf->fireChanges)
3189 return NULL; /* changes in this buffer are not reported */
3190
3191 *bufnop = bufno;
3192 return nbbuf;
3193}
3194
3195/*
3196 * Tell netbeans the user inserted some text.
3197 */
3198 void
3199netbeans_inserted(
3200 buf_T *bufp,
3201 linenr_T linenr,
3202 colnr_T col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 char_u *txt,
3204 int newlen)
3205{
3206 char_u *buf;
3207 int bufno;
3208 nbbuf_T *nbbuf;
3209 pos_T pos;
3210 long off;
3211 char_u *p;
3212 char_u *newtxt;
3213
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003214 if (!NETBEANS_OPEN)
3215 return;
3216
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3218 if (nbbuf == NULL)
3219 return;
3220
Bram Moolenaar009b2592004-10-24 19:18:58 +00003221 /* Don't mark as modified for initial read */
3222 if (nbbuf->insertDone)
3223 nbbuf->modified = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224
3225 pos.lnum = linenr;
3226 pos.col = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 off = pos2off(bufp, &pos);
3228
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 /* send the "insert" EVT */
3230 newtxt = alloc(newlen + 1);
Bram Moolenaarb6356332005-07-18 21:40:44 +00003231 vim_strncpy(newtxt, txt, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 p = nb_quote(newtxt);
3233 if (p != NULL)
3234 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00003235 buf = alloc(128 + 2*newlen);
Bram Moolenaar89d40322006-08-29 15:30:07 +00003236 sprintf((char *)buf, "%d:insert=%d %ld \"%s\"\n",
3237 bufno, r_cmdno, off, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 nbdebug(("EVT: %s", buf));
3239 nb_send((char *)buf, "netbeans_inserted");
Bram Moolenaar009b2592004-10-24 19:18:58 +00003240 vim_free(p);
3241 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 vim_free(newtxt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244}
3245
3246/*
3247 * Tell netbeans some bytes have been removed.
3248 */
3249 void
3250netbeans_removed(
3251 buf_T *bufp,
3252 linenr_T linenr,
3253 colnr_T col,
3254 long len)
3255{
3256 char_u buf[128];
3257 int bufno;
3258 nbbuf_T *nbbuf;
3259 pos_T pos;
3260 long off;
3261
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003262 if (!NETBEANS_OPEN)
3263 return;
3264
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3266 if (nbbuf == NULL)
3267 return;
3268
3269 if (len < 0)
3270 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00003271 nbdebug(("Negative len %ld in netbeans_removed()!\n", len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 return;
3273 }
3274
3275 nbbuf->modified = 1;
3276
3277 pos.lnum = linenr;
3278 pos.col = col;
3279
3280 off = pos2off(bufp, &pos);
3281
Bram Moolenaar89d40322006-08-29 15:30:07 +00003282 sprintf((char *)buf, "%d:remove=%d %ld %ld\n", bufno, r_cmdno, off, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 nbdebug(("EVT: %s", buf));
3284 nb_send((char *)buf, "netbeans_removed");
3285}
3286
3287/*
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003288 * Send netbeans an unmodified command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003291netbeans_unmodified(buf_T *bufp UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292{
Bram Moolenaar09092152010-08-08 16:38:42 +02003293 /* This is a no-op, because NetBeans considers a buffer modified
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 * even when all changes have been undone. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295}
3296
3297/*
3298 * Send a button release event back to netbeans. Its up to netbeans
3299 * to decide what to do (if anything) with this event.
3300 */
3301 void
3302netbeans_button_release(int button)
3303{
3304 char buf[128];
3305 int bufno;
3306
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003307 if (!NETBEANS_OPEN)
3308 return;
3309
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 bufno = nb_getbufno(curbuf);
3311
3312 if (bufno >= 0 && curwin != NULL && curwin->w_buffer == curbuf)
3313 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003314 int col = mouse_col - W_WINCOL(curwin)
Bram Moolenaar67c53842010-05-22 18:28:27 +02003315 - ((curwin->w_p_nu || curwin->w_p_rnu) ? 9 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 long off = pos2off(curbuf, &curwin->w_cursor);
3317
3318 /* sync the cursor position */
Bram Moolenaar89d40322006-08-29 15:30:07 +00003319 sprintf(buf, "%d:newDotAndMark=%d %ld %ld\n", bufno, r_cmdno, off, off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 nbdebug(("EVT: %s", buf));
3321 nb_send(buf, "netbeans_button_release[newDotAndMark]");
3322
Bram Moolenaar89d40322006-08-29 15:30:07 +00003323 sprintf(buf, "%d:buttonRelease=%d %d %ld %d\n", bufno, r_cmdno,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 button, (long)curwin->w_cursor.lnum, col);
3325 nbdebug(("EVT: %s", buf));
3326 nb_send(buf, "netbeans_button_release");
3327 }
3328}
3329
3330
3331/*
Bram Moolenaar143c38c2007-05-10 16:41:10 +00003332 * Send a keypress event back to netbeans. This usually simulates some
Bram Moolenaar009b2592004-10-24 19:18:58 +00003333 * kind of function key press. This function operates on a key code.
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003334 * Return TRUE when the key was sent, FALSE when the command has been
3335 * postponed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 */
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003337 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338netbeans_keycommand(int key)
3339{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 char keyName[60];
Bram Moolenaar009b2592004-10-24 19:18:58 +00003341
3342 netbeans_keyname(key, keyName);
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003343 return netbeans_keystring((char_u *)keyName);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003344}
3345
3346
3347/*
Bram Moolenaar143c38c2007-05-10 16:41:10 +00003348 * Send a keypress event back to netbeans. This usually simulates some
Bram Moolenaar009b2592004-10-24 19:18:58 +00003349 * kind of function key press. This function operates on a key string.
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003350 * Return TRUE when the key was sent, FALSE when the command has been
3351 * postponed.
Bram Moolenaar009b2592004-10-24 19:18:58 +00003352 */
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003353 static int
3354netbeans_keystring(char_u *keyName)
Bram Moolenaar009b2592004-10-24 19:18:58 +00003355{
3356 char buf[2*MAXPATHL];
3357 int bufno = nb_getbufno(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 long off;
3359 char_u *q;
3360
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003361 if (!NETBEANS_OPEN)
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003362 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 if (bufno == -1)
3365 {
3366 nbdebug(("got keycommand for non-NetBeans buffer, opening...\n"));
3367 q = curbuf->b_ffname == NULL ? (char_u *)""
3368 : nb_quote(curbuf->b_ffname);
3369 if (q == NULL)
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003370 return TRUE;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003371 vim_snprintf(buf, sizeof(buf), "0:fileOpened=%d \"%s\" %s %s\n", 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 q,
3373 "T", /* open in NetBeans */
3374 "F"); /* modified */
3375 if (curbuf->b_ffname != NULL)
3376 vim_free(q);
3377 nbdebug(("EVT: %s", buf));
3378 nb_send(buf, "netbeans_keycommand");
3379
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003380 postpone_keycommand(keyName);
3381 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 }
3383
3384 /* sync the cursor position */
3385 off = pos2off(curbuf, &curwin->w_cursor);
Bram Moolenaar89d40322006-08-29 15:30:07 +00003386 sprintf(buf, "%d:newDotAndMark=%d %ld %ld\n", bufno, r_cmdno, off, off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 nbdebug(("EVT: %s", buf));
3388 nb_send(buf, "netbeans_keycommand");
3389
3390 /* To work on Win32 you must apply patch to ExtEditor module
3391 * from ExtEdCaret.java.diff - make EVT_newDotAndMark handler
3392 * more synchronous
3393 */
3394
3395 /* now send keyCommand event */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003396 vim_snprintf(buf, sizeof(buf), "%d:keyCommand=%d \"%s\"\n",
Bram Moolenaar89d40322006-08-29 15:30:07 +00003397 bufno, r_cmdno, keyName);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 nbdebug(("EVT: %s", buf));
3399 nb_send(buf, "netbeans_keycommand");
3400
3401 /* New: do both at once and include the lnum/col. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003402 vim_snprintf(buf, sizeof(buf), "%d:keyAtPos=%d \"%s\" %ld %ld/%ld\n",
Bram Moolenaar89d40322006-08-29 15:30:07 +00003403 bufno, r_cmdno, keyName,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 off, (long)curwin->w_cursor.lnum, (long)curwin->w_cursor.col);
3405 nbdebug(("EVT: %s", buf));
3406 nb_send(buf, "netbeans_keycommand");
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003407 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408}
3409
3410
3411/*
3412 * Send a save event to netbeans.
3413 */
3414 void
3415netbeans_save_buffer(buf_T *bufp)
3416{
3417 char_u buf[64];
3418 int bufno;
3419 nbbuf_T *nbbuf;
3420
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003421 if (!NETBEANS_OPEN)
3422 return;
3423
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3425 if (nbbuf == NULL)
3426 return;
3427
3428 nbbuf->modified = 0;
3429
Bram Moolenaar89d40322006-08-29 15:30:07 +00003430 sprintf((char *)buf, "%d:save=%d\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 nbdebug(("EVT: %s", buf));
3432 nb_send((char *)buf, "netbeans_save_buffer");
3433}
3434
3435
3436/*
3437 * Send remove command to netbeans (this command has been turned off).
3438 */
3439 void
3440netbeans_deleted_all_lines(buf_T *bufp)
3441{
3442 char_u buf[64];
3443 int bufno;
3444 nbbuf_T *nbbuf;
3445
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003446 if (!NETBEANS_OPEN)
3447 return;
3448
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3450 if (nbbuf == NULL)
3451 return;
3452
Bram Moolenaar009b2592004-10-24 19:18:58 +00003453 /* Don't mark as modified for initial read */
3454 if (nbbuf->insertDone)
3455 nbbuf->modified = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456
Bram Moolenaar89d40322006-08-29 15:30:07 +00003457 sprintf((char *)buf, "%d:remove=%d 0 -1\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 nbdebug(("EVT(suppressed): %s", buf));
3459/* nb_send(buf, "netbeans_deleted_all_lines"); */
3460}
3461
3462
3463/*
3464 * See if the lines are guarded. The top and bot parameters are from
3465 * u_savecommon(), these are the line above the change and the line below the
3466 * change.
3467 */
3468 int
3469netbeans_is_guarded(linenr_T top, linenr_T bot)
3470{
3471 signlist_T *p;
3472 int lnum;
3473
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003474 if (!NETBEANS_OPEN)
3475 return FALSE;
3476
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 for (p = curbuf->b_signlist; p != NULL; p = p->next)
3478 if (p->id >= GUARDEDOFFSET)
3479 for (lnum = top + 1; lnum < bot; lnum++)
3480 if (lnum == p->lnum)
3481 return TRUE;
3482
3483 return FALSE;
3484}
3485
Bram Moolenaar173c9852010-09-29 17:27:01 +02003486#if defined(FEAT_GUI_X11) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487/*
3488 * We have multiple signs to draw at the same location. Draw the
3489 * multi-sign indicator instead. This is the Motif version.
3490 */
3491 void
3492netbeans_draw_multisign_indicator(int row)
3493{
3494 int i;
3495 int y;
3496 int x;
3497
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003498 if (!NETBEANS_OPEN)
3499 return;
3500
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 x = 0;
3502 y = row * gui.char_height + 2;
3503
3504 for (i = 0; i < gui.char_height - 3; i++)
3505 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y++);
3506
3507 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+0, y);
3508 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3509 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+4, y++);
3510 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+1, y);
3511 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3512 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+3, y++);
3513 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3514}
Bram Moolenaar173c9852010-09-29 17:27:01 +02003515#endif /* FEAT_GUI_X11 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516
Bram Moolenaar64404472010-06-26 06:24:45 +02003517#if defined(FEAT_GUI_GTK) && !defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518/*
3519 * We have multiple signs to draw at the same location. Draw the
3520 * multi-sign indicator instead. This is the GTK/Gnome version.
3521 */
3522 void
3523netbeans_draw_multisign_indicator(int row)
3524{
3525 int i;
3526 int y;
3527 int x;
3528 GdkDrawable *drawable = gui.drawarea->window;
3529
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003530 if (!NETBEANS_OPEN)
3531 return;
3532
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 x = 0;
3534 y = row * gui.char_height + 2;
3535
3536 for (i = 0; i < gui.char_height - 3; i++)
3537 gdk_draw_point(drawable, gui.text_gc, x+2, y++);
3538
3539 gdk_draw_point(drawable, gui.text_gc, x+0, y);
3540 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3541 gdk_draw_point(drawable, gui.text_gc, x+4, y++);
3542 gdk_draw_point(drawable, gui.text_gc, x+1, y);
3543 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3544 gdk_draw_point(drawable, gui.text_gc, x+3, y++);
3545 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3546}
3547#endif /* FEAT_GUI_GTK */
3548
3549/*
3550 * If the mouse is clicked in the gutter of a line with multiple
3551 * annotations, cycle through the set of signs.
3552 */
3553 void
3554netbeans_gutter_click(linenr_T lnum)
3555{
3556 signlist_T *p;
3557
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003558 if (!NETBEANS_OPEN)
3559 return;
3560
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 for (p = curbuf->b_signlist; p != NULL; p = p->next)
3562 {
3563 if (p->lnum == lnum && p->next && p->next->lnum == lnum)
3564 {
3565 signlist_T *tail;
3566
3567 /* remove "p" from list, reinsert it at the tail of the sublist */
3568 if (p->prev)
3569 p->prev->next = p->next;
3570 else
3571 curbuf->b_signlist = p->next;
3572 p->next->prev = p->prev;
3573 /* now find end of sublist and insert p */
3574 for (tail = p->next;
3575 tail->next && tail->next->lnum == lnum
3576 && tail->next->id < GUARDEDOFFSET;
3577 tail = tail->next)
3578 ;
3579 /* tail now points to last entry with same lnum (except
3580 * that "guarded" annotations are always last) */
3581 p->next = tail->next;
3582 if (tail->next)
3583 tail->next->prev = p;
3584 p->prev = tail;
3585 tail->next = p;
3586 update_debug_sign(curbuf, lnum);
3587 break;
3588 }
3589 }
3590}
3591
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592/*
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003593 * Add a sign of the requested type at the requested location.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 *
3595 * Reverse engineering:
3596 * Apparently an annotation is defined the first time it is used in a buffer.
3597 * When the same annotation is used in two buffers, the second time we do not
3598 * need to define a new sign name but reuse the existing one. But since the
3599 * ID number used in the second buffer starts counting at one again, a mapping
3600 * is made from the ID specifically for the buffer to the global sign name
3601 * (which is a number).
3602 *
3603 * globalsignmap[] stores the signs that have been defined globally.
3604 * buf->signmapused[] maps buffer-local annotation IDs to an index in
3605 * globalsignmap[].
3606 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 static void
3608addsigntype(
3609 nbbuf_T *buf,
3610 int typeNum,
3611 char_u *typeName,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003612 char_u *tooltip UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 char_u *glyphFile,
Bram Moolenaar67c53842010-05-22 18:28:27 +02003614 char_u *fg,
3615 char_u *bg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 int i, j;
Bram Moolenaar67c53842010-05-22 18:28:27 +02003618 int use_fg = (*fg && STRCMP(fg, "none") != 0);
3619 int use_bg = (*bg && STRCMP(bg, "none") != 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620
3621 for (i = 0; i < globalsignmapused; i++)
3622 if (STRCMP(typeName, globalsignmap[i]) == 0)
3623 break;
3624
3625 if (i == globalsignmapused) /* not found; add it to global map */
3626 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003627 nbdebug(("DEFINEANNOTYPE(%d,%s,%s,%s,%s,%s)\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 typeNum, typeName, tooltip, glyphFile, fg, bg));
3629 if (use_fg || use_bg)
3630 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003631 char fgbuf[2 * (8 + MAX_COLOR_LENGTH) + 1];
3632 char bgbuf[2 * (8 + MAX_COLOR_LENGTH) + 1];
3633 char *ptr;
3634 int value;
3635
3636 value = strtol((char *)fg, &ptr, 10);
3637 if (ptr != (char *)fg)
3638 sprintf(fgbuf, "guifg=#%06x", value & 0xFFFFFF);
3639 else
3640 sprintf(fgbuf, "guifg=%s ctermfg=%s", fg, fg);
3641
3642 value = strtol((char *)bg, &ptr, 10);
3643 if (ptr != (char *)bg)
3644 sprintf(bgbuf, "guibg=#%06x", value & 0xFFFFFF);
3645 else
3646 sprintf(bgbuf, "guibg=%s ctermbg=%s", bg, bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647
3648 coloncmd(":highlight NB_%s %s %s", typeName, (use_fg) ? fgbuf : "",
3649 (use_bg) ? bgbuf : "");
3650 if (*glyphFile == NUL)
3651 /* no glyph, line highlighting only */
3652 coloncmd(":sign define %d linehl=NB_%s", i + 1, typeName);
3653 else if (vim_strsize(glyphFile) <= 2)
3654 /* one- or two-character glyph name, use as text glyph with
3655 * texthl */
3656 coloncmd(":sign define %d text=%s texthl=NB_%s", i + 1,
3657 glyphFile, typeName);
3658 else
3659 /* glyph, line highlighting */
3660 coloncmd(":sign define %d icon=%s linehl=NB_%s", i + 1,
3661 glyphFile, typeName);
3662 }
3663 else
3664 /* glyph, no line highlighting */
3665 coloncmd(":sign define %d icon=%s", i + 1, glyphFile);
3666
3667 if (STRCMP(typeName,"CurrentPC") == 0)
3668 curPCtype = typeNum;
3669
3670 if (globalsignmapused == globalsignmaplen)
3671 {
3672 if (globalsignmaplen == 0) /* first allocation */
3673 {
3674 globalsignmaplen = 20;
3675 globalsignmap = (char **)alloc_clear(globalsignmaplen*sizeof(char *));
3676 }
3677 else /* grow it */
3678 {
3679 int incr;
3680 int oldlen = globalsignmaplen;
3681
3682 globalsignmaplen *= 2;
3683 incr = globalsignmaplen - oldlen;
3684 globalsignmap = (char **)vim_realloc(globalsignmap,
3685 globalsignmaplen * sizeof(char *));
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003686 vim_memset(globalsignmap + oldlen, 0, incr * sizeof(char *));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 }
3688 }
3689
3690 globalsignmap[i] = (char *)typeName;
3691 globalsignmapused = i + 1;
3692 }
3693
3694 /* check local map; should *not* be found! */
3695 for (j = 0; j < buf->signmapused; j++)
3696 if (buf->signmap[j] == i + 1)
3697 return;
3698
3699 /* add to local map */
3700 if (buf->signmapused == buf->signmaplen)
3701 {
3702 if (buf->signmaplen == 0) /* first allocation */
3703 {
3704 buf->signmaplen = 5;
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003705 buf->signmap = (int *)alloc_clear(buf->signmaplen * sizeof(int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 }
3707 else /* grow it */
3708 {
3709 int incr;
3710 int oldlen = buf->signmaplen;
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003711
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 buf->signmaplen *= 2;
3713 incr = buf->signmaplen - oldlen;
3714 buf->signmap = (int *)vim_realloc(buf->signmap,
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003715 buf->signmaplen * sizeof(int));
3716 vim_memset(buf->signmap + oldlen, 0, incr * sizeof(int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 }
3718 }
3719
3720 buf->signmap[buf->signmapused++] = i + 1;
3721
3722}
3723
3724
3725/*
3726 * See if we have the requested sign type in the buffer.
3727 */
3728 static int
3729mapsigntype(nbbuf_T *buf, int localsigntype)
3730{
3731 if (--localsigntype >= 0 && localsigntype < buf->signmapused)
3732 return buf->signmap[localsigntype];
3733
3734 return 0;
3735}
3736
3737
3738/*
3739 * Compute length of buffer, don't print anything.
3740 */
3741 static long
3742get_buf_size(buf_T *bufp)
3743{
3744 linenr_T lnum;
3745 long char_count = 0;
3746 int eol_size;
3747 long last_check = 100000L;
3748
3749 if (bufp->b_ml.ml_flags & ML_EMPTY)
3750 return 0;
3751 else
3752 {
3753 if (get_fileformat(bufp) == EOL_DOS)
3754 eol_size = 2;
3755 else
3756 eol_size = 1;
3757 for (lnum = 1; lnum <= bufp->b_ml.ml_line_count; ++lnum)
3758 {
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00003759 char_count += (long)STRLEN(ml_get_buf(bufp, lnum, FALSE))
3760 + eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 /* Check for a CTRL-C every 100000 characters */
3762 if (char_count > last_check)
3763 {
3764 ui_breakcheck();
3765 if (got_int)
3766 return char_count;
3767 last_check = char_count + 100000L;
3768 }
3769 }
3770 /* Correction for when last line doesn't have an EOL. */
3771 if (!bufp->b_p_eol && bufp->b_p_bin)
3772 char_count -= eol_size;
3773 }
3774
3775 return char_count;
3776}
3777
3778/*
3779 * Convert character offset to lnum,col
3780 */
3781 static pos_T *
3782off2pos(buf_T *buf, long offset)
3783{
3784 linenr_T lnum;
3785 static pos_T pos;
3786
3787 pos.lnum = 0;
3788 pos.col = 0;
3789#ifdef FEAT_VIRTUALEDIT
3790 pos.coladd = 0;
3791#endif
3792
3793 if (!(buf->b_ml.ml_flags & ML_EMPTY))
3794 {
3795 if ((lnum = ml_find_line_or_offset(buf, (linenr_T)0, &offset)) < 0)
3796 return NULL;
3797 pos.lnum = lnum;
3798 pos.col = offset;
3799 }
3800
3801 return &pos;
3802}
3803
3804/*
3805 * Convert an argument in the form "1234" to an offset and compute the
3806 * lnum/col from it. Convert an argument in the form "123/12" directly to a
3807 * lnum/col.
3808 * "argp" is advanced to after the argument.
3809 * Return a pointer to the position, NULL if something is wrong.
3810 */
3811 static pos_T *
3812get_off_or_lnum(buf_T *buf, char_u **argp)
3813{
3814 static pos_T mypos;
3815 long off;
3816
3817 off = strtol((char *)*argp, (char **)argp, 10);
3818 if (**argp == '/')
3819 {
3820 mypos.lnum = (linenr_T)off;
3821 ++*argp;
3822 mypos.col = strtol((char *)*argp, (char **)argp, 10);
3823#ifdef FEAT_VIRTUALEDIT
3824 mypos.coladd = 0;
3825#endif
3826 return &mypos;
3827 }
3828 return off2pos(buf, off);
3829}
3830
3831
3832/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003833 * Convert (lnum,col) to byte offset in the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 */
3835 static long
3836pos2off(buf_T *buf, pos_T *pos)
3837{
3838 long offset = 0;
3839
3840 if (!(buf->b_ml.ml_flags & ML_EMPTY))
3841 {
3842 if ((offset = ml_find_line_or_offset(buf, pos->lnum, 0)) < 0)
3843 return 0;
3844 offset += pos->col;
3845 }
3846
3847 return offset;
3848}
3849
3850
Bram Moolenaar009b2592004-10-24 19:18:58 +00003851/*
3852 * This message is printed after NetBeans opens a new file. Its
3853 * similar to the message readfile() uses, but since NetBeans
3854 * doesn't normally call readfile, we do our own.
3855 */
3856 static void
3857print_read_msg(buf)
3858 nbbuf_T *buf;
3859{
3860 int lnum = buf->bufp->b_ml.ml_line_count;
Bram Moolenaar914703b2010-05-31 21:59:46 +02003861 off_t nchars = buf->bufp->b_orig_size;
Bram Moolenaar009b2592004-10-24 19:18:58 +00003862 char_u c;
3863
3864 msg_add_fname(buf->bufp, buf->bufp->b_ffname);
3865 c = FALSE;
3866
3867 if (buf->bufp->b_p_ro)
3868 {
3869 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]"));
3870 c = TRUE;
3871 }
3872 if (!buf->bufp->b_start_eol)
3873 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003874 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]")
3875 : _("[Incomplete last line]"));
Bram Moolenaar009b2592004-10-24 19:18:58 +00003876 c = TRUE;
3877 }
3878 msg_add_lines(c, (long)lnum, nchars);
3879
3880 /* Now display it */
3881 vim_free(keep_msg);
3882 keep_msg = NULL;
3883 msg_scrolled_ign = TRUE;
3884 msg_trunc_attr(IObuff, FALSE, 0);
3885 msg_scrolled_ign = FALSE;
3886}
3887
3888
3889/*
Bram Moolenaar67c53842010-05-22 18:28:27 +02003890 * Print a message after NetBeans writes the file. This message should be
3891 * identical to the standard message a non-netbeans user would see when
3892 * writing a file.
Bram Moolenaar009b2592004-10-24 19:18:58 +00003893 */
3894 static void
3895print_save_msg(buf, nchars)
3896 nbbuf_T *buf;
Bram Moolenaar914703b2010-05-31 21:59:46 +02003897 off_t nchars;
Bram Moolenaar009b2592004-10-24 19:18:58 +00003898{
3899 char_u c;
3900 char_u *p;
3901
3902 if (nchars >= 0)
3903 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003904 /* put fname in IObuff with quotes */
3905 msg_add_fname(buf->bufp, buf->bufp->b_ffname);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003906 c = FALSE;
3907
3908 msg_add_lines(c, buf->bufp->b_ml.ml_line_count,
Bram Moolenaar914703b2010-05-31 21:59:46 +02003909 buf->bufp->b_orig_size);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003910
3911 vim_free(keep_msg);
3912 keep_msg = NULL;
3913 msg_scrolled_ign = TRUE;
3914 p = msg_trunc_attr(IObuff, FALSE, 0);
3915 if ((msg_scrolled && !need_wait_return) || !buf->initDone)
3916 {
3917 /* Need to repeat the message after redrawing when:
3918 * - When reading from stdin (the screen will be cleared next).
3919 * - When restart_edit is set (otherwise there will be a delay
3920 * before redrawing).
3921 * - When the screen was scrolled but there is no wait-return
3922 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003923 set_keep_msg(p, 0);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003924 }
3925 msg_scrolled_ign = FALSE;
3926 /* add_to_input_buf((char_u *)"\f", 1); */
3927 }
3928 else
3929 {
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003930 char_u msgbuf[IOSIZE];
Bram Moolenaar009b2592004-10-24 19:18:58 +00003931
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003932 vim_snprintf((char *)msgbuf, IOSIZE,
3933 _("E505: %s is read-only (add ! to override)"), IObuff);
3934 nbdebug((" %s\n", msgbuf));
3935 emsg(msgbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003936 }
3937}
3938
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939#endif /* defined(FEAT_NETBEANS_INTG) */