blob: 0b9ea134f3bcc5b2f0f8eb96a2a5d89e8f1b84f9 [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
30/* Note: when making changes here also adjust configure.in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000031#ifdef WIN32
32# ifdef DEBUG
33# include <tchar.h> /* for _T definition for TRACEn macros */
34# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000035/* WinSock API is separated from C API, thus we can't use read(), write(),
36 * errno... */
Bram Moolenaarc39125d2010-05-23 12:06:58 +020037# define SOCK_ERRNO errno = WSAGetLastError()
Bram Moolenaarbd42a0f2009-06-16 14:57:26 +000038# undef ECONNREFUSED
Bram Moolenaar071d4272004-06-13 20:20:40 +000039# define ECONNREFUSED WSAECONNREFUSED
40# ifdef EINTR
41# undef EINTR
42# endif
43# define EINTR WSAEINTR
44# define sock_write(sd, buf, len) send(sd, buf, len, 0)
45# define sock_read(sd, buf, len) recv(sd, buf, len, 0)
46# define sock_close(sd) closesocket(sd)
47# define sleep(t) Sleep(t*1000) /* WinAPI Sleep() accepts milliseconds */
48#else
Bram Moolenaar3e53c702016-01-24 22:17:03 +010049# include <netdb.h>
50# include <netinet/in.h>
Bram Moolenaarb26e6322010-05-22 21:34:09 +020051
Bram Moolenaar071d4272004-06-13 20:20:40 +000052# include <sys/socket.h>
53# ifdef HAVE_LIBGEN_H
54# include <libgen.h>
55# endif
Bram Moolenaarc39125d2010-05-23 12:06:58 +020056# define SOCK_ERRNO
Bram Moolenaar071d4272004-06-13 20:20:40 +000057# define sock_write(sd, buf, len) write(sd, buf, len)
58# define sock_read(sd, buf, len) read(sd, buf, len)
59# define sock_close(sd) close(sd)
60#endif
61
62#include "version.h"
63
Bram Moolenaar071d4272004-06-13 20:20:40 +000064#define GUARDED 10000 /* typenr for "guarded" annotation */
65#define GUARDEDOFFSET 1000000 /* base for "guarded" sign id's */
Bram Moolenaar67c53842010-05-22 18:28:27 +020066#define MAX_COLOR_LENGTH 32 /* max length of color name in defineAnnoType */
Bram Moolenaar071d4272004-06-13 20:20:40 +000067
68/* The first implementation (working only with Netbeans) returned "1.1". The
69 * protocol implemented here also supports A-A-P. */
Bram Moolenaar67c53842010-05-22 18:28:27 +020070static char *ExtEdProtocolVersion = "2.5";
Bram Moolenaar071d4272004-06-13 20:20:40 +000071
72static long pos2off __ARGS((buf_T *, pos_T *));
73static pos_T *off2pos __ARGS((buf_T *, long));
74static pos_T *get_off_or_lnum __ARGS((buf_T *buf, char_u **argp));
75static long get_buf_size __ARGS((buf_T *));
Bram Moolenaar8065d7f2010-01-19 15:13:14 +010076static int netbeans_keystring __ARGS((char_u *keystr));
77static void postpone_keycommand __ARGS((char_u *keystr));
Bram Moolenaar009b2592004-10-24 19:18:58 +000078static void special_keys __ARGS((char_u *args));
Bram Moolenaar071d4272004-06-13 20:20:40 +000079
Bram Moolenaarb26e6322010-05-22 21:34:09 +020080static int netbeans_connect __ARGS((char *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +000081static int getConnInfo __ARGS((char *file, char **host, char **port, char **password));
82
83static void nb_init_graphics __ARGS((void));
84static void coloncmd __ARGS((char *cmd, ...));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000085static void nb_set_curbuf __ARGS((buf_T *buf));
Bram Moolenaar173c9852010-09-29 17:27:01 +020086#ifdef FEAT_GUI_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +000087static void messageFromNetbeans __ARGS((XtPointer, int *, XtInputId *));
88#endif
89#ifdef FEAT_GUI_GTK
90static void messageFromNetbeans __ARGS((gpointer, gint, GdkInputCondition));
91#endif
92static void nb_parse_cmd __ARGS((char_u *));
93static int nb_do_cmd __ARGS((int, char_u *, int, int, char_u *));
94static void nb_send __ARGS((char *buf, char *fun));
Bram Moolenaarb26e6322010-05-22 21:34:09 +020095static void nb_free __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +000096
Bram Moolenaar67c53842010-05-22 18:28:27 +020097/* TRUE when netbeans is running with a GUI. */
98#ifdef FEAT_GUI
99# define NB_HAS_GUI (gui.in_use || gui.starting)
100#endif
101
Bram Moolenaare0874f82016-01-24 20:36:41 +0100102static sock_T nbsock = -1; /* socket fd for Netbeans connection */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200103#define NETBEANS_OPEN (nbsock != -1)
104
Bram Moolenaar173c9852010-09-29 17:27:01 +0200105#ifdef FEAT_GUI_X11
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200106static XtInputId inputHandler = (XtInputId)NULL; /* Cookie for input */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107#endif
108#ifdef FEAT_GUI_GTK
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200109static gint inputHandler = 0; /* Cookie for input */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110#endif
111#ifdef FEAT_GUI_W32
112static int inputHandler = -1; /* simply ret.value of WSAAsyncSelect() */
113extern HWND s_hwnd; /* Gvim's Window handle */
114#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +0000115static int r_cmdno; /* current command number for reply */
Bram Moolenaar009b2592004-10-24 19:18:58 +0000116static int dosetvisible = FALSE;
117
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118/*
119 * Include the debugging code if wanted.
120 */
121#ifdef NBDEBUG
122# include "nbdebug.c"
123#endif
124
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200125static int needupdate = 0;
126static int inAtomic = 0;
127
Bram Moolenaar7ad7d012010-11-16 15:49:02 +0100128/*
129 * Close the socket and remove the input handlers.
130 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131 static void
Bram Moolenaar7ad7d012010-11-16 15:49:02 +0100132nb_close_socket(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133{
Bram Moolenaar3b7b8362015-03-20 18:11:48 +0100134 buf_T *buf;
135
136 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
137 buf->b_has_sign_column = FALSE;
138
Bram Moolenaar173c9852010-09-29 17:27:01 +0200139#ifdef FEAT_GUI_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140 if (inputHandler != (XtInputId)NULL)
141 {
142 XtRemoveInput(inputHandler);
143 inputHandler = (XtInputId)NULL;
144 }
Bram Moolenaar67c53842010-05-22 18:28:27 +0200145#else
146# ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147 if (inputHandler != 0)
148 {
149 gdk_input_remove(inputHandler);
150 inputHandler = 0;
151 }
Bram Moolenaar67c53842010-05-22 18:28:27 +0200152# else
153# ifdef FEAT_GUI_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154 if (inputHandler == 0)
155 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200156 WSAAsyncSelect(nbsock, s_hwnd, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157 inputHandler = -1;
158 }
Bram Moolenaar67c53842010-05-22 18:28:27 +0200159# endif
160# endif
161#endif
162
Bram Moolenaar7ad7d012010-11-16 15:49:02 +0100163 sock_close(nbsock);
164 nbsock = -1;
Bram Moolenaare0874f82016-01-24 20:36:41 +0100165 channel_remove_netbeans();
Bram Moolenaar7ad7d012010-11-16 15:49:02 +0100166}
167
168/*
169 * Close the connection and cleanup.
170 * May be called when nb_close_socket() was called earlier.
171 */
172 static void
173netbeans_close(void)
174{
175 if (NETBEANS_OPEN)
176 {
177 netbeans_send_disconnect();
178 nb_close_socket();
179 }
180
Bram Moolenaar67c53842010-05-22 18:28:27 +0200181#ifdef FEAT_BEVAL
Bram Moolenaard62bec82005-03-07 22:56:57 +0000182 bevalServers &= ~BEVAL_NETBEANS;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200183#endif
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200184
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200185 needupdate = 0;
186 inAtomic = 0;
187 nb_free();
188
189 /* remove all signs and update the screen after gutter removal */
190 coloncmd(":sign unplace *");
191 changed_window_setting();
192 update_screen(CLEAR);
193 setcursor();
Bram Moolenaar96bcc5e2011-04-01 15:33:59 +0200194 cursor_on();
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200195 out_flush();
196#ifdef FEAT_GUI
Bram Moolenaard54a6882010-08-09 22:49:00 +0200197 if (gui.in_use)
198 {
199 gui_update_cursor(TRUE, FALSE);
200 gui_mch_flush();
201 }
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200202#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204
205#define NB_DEF_HOST "localhost"
206#define NB_DEF_ADDR "3219"
207#define NB_DEF_PASS "changeme"
208
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200209 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200210netbeans_connect(char *params, int doabort)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212 struct sockaddr_in server;
213 struct hostent * host;
Bram Moolenaar3e53c702016-01-24 22:17:03 +0100214#ifdef FEAT_GUI_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215 u_short port;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216#else
Bram Moolenaar3e53c702016-01-24 22:17:03 +0100217 int port;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218#endif
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200219 int sd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220 char buf[32];
221 char *hostname = NULL;
222 char *address = NULL;
223 char *password = NULL;
224 char *fname;
225 char *arg = NULL;
226
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200227 if (*params == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200229 /* "=fname": Read info from specified file. */
Bram Moolenaare0874f82016-01-24 20:36:41 +0100230 if (getConnInfo(params + 1, &hostname, &address, &password) == FAIL)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200231 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232 }
233 else
234 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200235 if (*params == ':')
236 /* ":<host>:<addr>:<password>": get info from argument */
237 arg = params + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 if (arg == NULL && (fname = getenv("__NETBEANS_CONINFO")) != NULL)
239 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200240 /* "": get info from file specified in environment */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 if (getConnInfo(fname, &hostname, &address, &password) == FAIL)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200242 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243 }
244 else
245 {
246 if (arg != NULL)
247 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200248 /* ":<host>:<addr>:<password>": get info from argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 hostname = arg;
250 address = strchr(hostname, ':');
251 if (address != NULL)
252 {
253 *address++ = '\0';
254 password = strchr(address, ':');
255 if (password != NULL)
256 *password++ = '\0';
257 }
258 }
259
260 /* Get the missing values from the environment. */
261 if (hostname == NULL || *hostname == '\0')
262 hostname = getenv("__NETBEANS_HOST");
263 if (address == NULL)
264 address = getenv("__NETBEANS_SOCKET");
265 if (password == NULL)
266 password = getenv("__NETBEANS_VIM_PASSWORD");
267
268 /* Move values to allocated memory. */
269 if (hostname != NULL)
270 hostname = (char *)vim_strsave((char_u *)hostname);
271 if (address != NULL)
272 address = (char *)vim_strsave((char_u *)address);
273 if (password != NULL)
274 password = (char *)vim_strsave((char_u *)password);
275 }
276 }
277
278 /* Use the default when a value is missing. */
279 if (hostname == NULL || *hostname == '\0')
280 {
281 vim_free(hostname);
282 hostname = (char *)vim_strsave((char_u *)NB_DEF_HOST);
283 }
284 if (address == NULL || *address == '\0')
285 {
286 vim_free(address);
287 address = (char *)vim_strsave((char_u *)NB_DEF_ADDR);
288 }
289 if (password == NULL || *password == '\0')
290 {
291 vim_free(password);
292 password = (char *)vim_strsave((char_u *)NB_DEF_PASS);
293 }
294 if (hostname == NULL || address == NULL || password == NULL)
295 goto theend; /* out of memory */
296
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200297#ifdef FEAT_GUI_W32
Bram Moolenaare0874f82016-01-24 20:36:41 +0100298 channel_init_winsock();
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200299#endif
300
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301 port = atoi(address);
302
Bram Moolenaare0874f82016-01-24 20:36:41 +0100303 if ((sd = (sock_T)socket(AF_INET, SOCK_STREAM, 0)) == (sock_T)-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000305 nbdebug(("error in socket() in netbeans_connect()\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306 PERROR("socket() in netbeans_connect()");
307 goto theend;
308 }
309
310 /* Get the server internet address and put into addr structure */
311 /* fill in the socket address structure and connect to server */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200312 vim_memset((char *)&server, '\0', sizeof(server));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313 server.sin_family = AF_INET;
314 server.sin_port = htons(port);
315 if ((host = gethostbyname(hostname)) == NULL)
316 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000317 nbdebug(("error in gethostbyname() in netbeans_connect()\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318 PERROR("gethostbyname() in netbeans_connect()");
Bram Moolenaar870b05c2011-01-04 18:11:43 +0100319 sock_close(sd);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320 goto theend;
321 }
322 memcpy((char *)&server.sin_addr, host->h_addr, host->h_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323 /* Connect to server */
324 if (connect(sd, (struct sockaddr *)&server, sizeof(server)))
325 {
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200326 SOCK_ERRNO;
327 nbdebug(("netbeans_connect: Connect failed with errno %d\n", errno));
328 if (errno == ECONNREFUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329 {
330 sock_close(sd);
Bram Moolenaare0874f82016-01-24 20:36:41 +0100331 if ((sd = (sock_T)socket(AF_INET, SOCK_STREAM, 0)) == (sock_T)-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332 {
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200333 SOCK_ERRNO;
Bram Moolenaarf2330482008-06-24 20:19:36 +0000334 nbdebug(("socket()#2 in netbeans_connect()\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 PERROR("socket()#2 in netbeans_connect()");
336 goto theend;
337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338 if (connect(sd, (struct sockaddr *)&server, sizeof(server)))
339 {
340 int retries = 36;
341 int success = FALSE;
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200342
343 SOCK_ERRNO;
344 while (retries-- && ((errno == ECONNREFUSED)
345 || (errno == EINTR)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 {
347 nbdebug(("retrying...\n"));
Bram Moolenaar870b05c2011-01-04 18:11:43 +0100348 mch_delay(3000L, TRUE);
349 ui_breakcheck();
350 if (got_int)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200351 {
Bram Moolenaar870b05c2011-01-04 18:11:43 +0100352 errno = EINTR;
353 break;
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355 if (connect(sd, (struct sockaddr *)&server,
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200356 sizeof(server)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 {
358 success = TRUE;
359 break;
360 }
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200361 SOCK_ERRNO;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 }
363 if (!success)
364 {
365 /* Get here when the server can't be found. */
Bram Moolenaarf2330482008-06-24 20:19:36 +0000366 nbdebug(("Cannot connect to Netbeans #2\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367 PERROR(_("Cannot connect to Netbeans #2"));
Bram Moolenaar870b05c2011-01-04 18:11:43 +0100368 sock_close(sd);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200369 if (doabort)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200370 getout(1);
371 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372 }
373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 }
375 else
376 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000377 nbdebug(("Cannot connect to Netbeans\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 PERROR(_("Cannot connect to Netbeans"));
Bram Moolenaar870b05c2011-01-04 18:11:43 +0100379 sock_close(sd);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200380 if (doabort)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200381 getout(1);
382 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 }
384 }
385
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200386 nbsock = sd;
Bram Moolenaare0874f82016-01-24 20:36:41 +0100387 channel_add_netbeans(nbsock);
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000388 vim_snprintf(buf, sizeof(buf), "AUTH %s\n", password);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389 nb_send(buf, "netbeans_connect");
390
391 sprintf(buf, "0:version=0 \"%s\"\n", ExtEdProtocolVersion);
392 nb_send(buf, "externaleditor_version");
393
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394theend:
395 vim_free(hostname);
396 vim_free(address);
397 vim_free(password);
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200398 return NETBEANS_OPEN ? OK : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399}
400
401/*
402 * Obtain the NetBeans hostname, port address and password from a file.
403 * Return the strings in allocated memory.
404 * Return FAIL if the file could not be read, OK otherwise (no matter what it
405 * contains).
406 */
407 static int
408getConnInfo(char *file, char **host, char **port, char **auth)
409{
410 FILE *fp;
411 char_u buf[BUFSIZ];
412 char_u *lp;
Bram Moolenaar309cbc32012-01-10 22:31:31 +0100413 char_u *nlp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414#ifdef UNIX
415 struct stat st;
416
417 /*
418 * For Unix only accept the file when it's not accessible by others.
419 * The open will then fail if we don't own the file.
420 */
421 if (mch_stat(file, &st) == 0 && (st.st_mode & 0077) != 0)
422 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000423 nbdebug(("Wrong access mode for NetBeans connection info file: \"%s\"\n",
424 file));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425 EMSG2(_("E668: Wrong access mode for NetBeans connection info file: \"%s\""),
426 file);
427 return FAIL;
428 }
429#endif
430
431 fp = mch_fopen(file, "r");
432 if (fp == NULL)
433 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000434 nbdebug(("Cannot open NetBeans connection info file\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435 PERROR("E660: Cannot open NetBeans connection info file");
436 return FAIL;
437 }
438
439 /* Read the file. There should be one of each parameter */
440 while ((lp = (char_u *)fgets((char *)buf, BUFSIZ, fp)) != NULL)
441 {
Bram Moolenaar309cbc32012-01-10 22:31:31 +0100442 if ((nlp = vim_strchr(lp, '\n')) != NULL)
443 *nlp = 0; /* strip off the trailing newline */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444
445 if (STRNCMP(lp, "host=", 5) == 0)
446 {
447 vim_free(*host);
448 *host = (char *)vim_strsave(&buf[5]);
449 }
450 else if (STRNCMP(lp, "port=", 5) == 0)
451 {
452 vim_free(*port);
453 *port = (char *)vim_strsave(&buf[5]);
454 }
455 else if (STRNCMP(lp, "auth=", 5) == 0)
456 {
457 vim_free(*auth);
458 *auth = (char *)vim_strsave(&buf[5]);
459 }
460 }
461 fclose(fp);
462
463 return OK;
464}
465
466
467struct keyqueue
468{
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100469 char_u *keystr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470 struct keyqueue *next;
471 struct keyqueue *prev;
472};
473
474typedef struct keyqueue keyQ_T;
475
476static keyQ_T keyHead; /* dummy node, header for circular queue */
477
478
479/*
480 * Queue up key commands sent from netbeans.
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100481 * We store the string, because it may depend on the global mod_mask and
482 * :nbkey doesn't have a key number.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 */
484 static void
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100485postpone_keycommand(char_u *keystr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486{
487 keyQ_T *node;
488
489 node = (keyQ_T *)alloc(sizeof(keyQ_T));
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100490 if (node == NULL)
491 return; /* out of memory, drop the key */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492
493 if (keyHead.next == NULL) /* initialize circular queue */
494 {
495 keyHead.next = &keyHead;
496 keyHead.prev = &keyHead;
497 }
498
499 /* insert node at tail of queue */
500 node->next = &keyHead;
501 node->prev = keyHead.prev;
502 keyHead.prev->next = node;
503 keyHead.prev = node;
504
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100505 node->keystr = vim_strsave(keystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506}
507
508/*
509 * Handle any queued-up NetBeans keycommands to be send.
510 */
511 static void
512handle_key_queue(void)
513{
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100514 int postponed = FALSE;
515
516 while (!postponed && keyHead.next && keyHead.next != &keyHead)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 {
518 /* first, unlink the node */
519 keyQ_T *node = keyHead.next;
520 keyHead.next = node->next;
521 node->next->prev = node->prev;
522
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100523 /* Now, send the keycommand. This may cause it to be postponed again
524 * and change keyHead. */
525 if (node->keystr != NULL)
526 postponed = !netbeans_keystring(node->keystr);
527 vim_free(node->keystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528
529 /* Finally, dispose of the node */
530 vim_free(node);
531 }
532}
533
534
535struct cmdqueue
536{
537 char_u *buffer;
538 struct cmdqueue *next;
539 struct cmdqueue *prev;
540};
541
542typedef struct cmdqueue queue_T;
543
544static queue_T head; /* dummy node, header for circular queue */
545
546
547/*
548 * Put the buffer on the work queue; possibly save it to a file as well.
549 */
550 static void
551save(char_u *buf, int len)
552{
553 queue_T *node;
554
555 node = (queue_T *)alloc(sizeof(queue_T));
556 if (node == NULL)
557 return; /* out of memory */
558 node->buffer = alloc(len + 1);
559 if (node->buffer == NULL)
560 {
561 vim_free(node);
562 return; /* out of memory */
563 }
564 mch_memmove(node->buffer, buf, (size_t)len);
565 node->buffer[len] = NUL;
566
567 if (head.next == NULL) /* initialize circular queue */
568 {
569 head.next = &head;
570 head.prev = &head;
571 }
572
573 /* insert node at tail of queue */
574 node->next = &head;
575 node->prev = head.prev;
576 head.prev->next = node;
577 head.prev = node;
578
579#ifdef NBDEBUG
580 {
581 static int outfd = -2;
582
583 /* possibly write buffer out to a file */
584 if (outfd == -3)
585 return;
586
587 if (outfd == -2)
588 {
589 char *file = getenv("__NETBEANS_SAVE");
590 if (file == NULL)
591 outfd = -3;
592 else
593 outfd = mch_open(file, O_WRONLY|O_CREAT|O_TRUNC, 0666);
594 }
595
596 if (outfd >= 0)
597 write(outfd, buf, len);
598 }
599#endif
600}
601
602
603/*
604 * While there's still a command in the work queue, parse and execute it.
605 */
Bram Moolenaarf2330482008-06-24 20:19:36 +0000606 void
607netbeans_parse_messages(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608{
609 char_u *p;
610 queue_T *node;
Bram Moolenaar863053d2010-12-02 17:09:54 +0100611 int own_node;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612
Bram Moolenaarf2330482008-06-24 20:19:36 +0000613 while (head.next != NULL && head.next != &head)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 {
615 node = head.next;
616
617 /* Locate the first line in the first buffer. */
618 p = vim_strchr(node->buffer, '\n');
619 if (p == NULL)
620 {
621 /* Command isn't complete. If there is no following buffer,
622 * return (wait for more). If there is another buffer following,
623 * prepend the text to that buffer and delete this one. */
624 if (node->next == &head)
625 return;
Bram Moolenaarf2330482008-06-24 20:19:36 +0000626 p = alloc((unsigned)(STRLEN(node->buffer)
627 + STRLEN(node->next->buffer) + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 if (p == NULL)
629 return; /* out of memory */
630 STRCPY(p, node->buffer);
631 STRCAT(p, node->next->buffer);
632 vim_free(node->next->buffer);
633 node->next->buffer = p;
634
635 /* dispose of the node and buffer */
636 head.next = node->next;
637 node->next->prev = node->prev;
638 vim_free(node->buffer);
639 vim_free(node);
640 }
641 else
642 {
643 /* There is a complete command at the start of the buffer.
644 * Terminate it with a NUL. When no more text is following unlink
645 * the buffer. Do this before executing, because new buffers can
646 * be added while busy handling the command. */
647 *p++ = NUL;
648 if (*p == NUL)
649 {
Bram Moolenaar863053d2010-12-02 17:09:54 +0100650 own_node = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 head.next = node->next;
652 node->next->prev = node->prev;
653 }
Bram Moolenaar863053d2010-12-02 17:09:54 +0100654 else
655 own_node = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656
657 /* now, parse and execute the commands */
658 nb_parse_cmd(node->buffer);
659
Bram Moolenaar863053d2010-12-02 17:09:54 +0100660 if (own_node)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 {
662 /* buffer finished, dispose of the node and buffer */
663 vim_free(node->buffer);
664 vim_free(node);
665 }
Bram Moolenaar863053d2010-12-02 17:09:54 +0100666 /* Check that "head" wasn't changed under our fingers, e.g. when a
667 * DETACH command was handled. */
668 else if (head.next == node)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 {
670 /* more follows, move to the start */
Bram Moolenaarf2330482008-06-24 20:19:36 +0000671 STRMOVE(node->buffer, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 }
673 }
674 }
675}
676
677/* Buffer size for reading incoming messages. */
678#define MAXMSGSIZE 4096
679
680/*
Bram Moolenaar67c53842010-05-22 18:28:27 +0200681 * Read a command from netbeans.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 */
Bram Moolenaar173c9852010-09-29 17:27:01 +0200683#ifdef FEAT_GUI_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 static void
Bram Moolenaara9d45512009-05-17 21:25:42 +0000685messageFromNetbeans(XtPointer clientData UNUSED,
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000686 int *unused1 UNUSED,
687 XtInputId *unused2 UNUSED)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200688{
689 netbeans_read();
690}
691#endif
692
693#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000695messageFromNetbeans(gpointer clientData UNUSED,
696 gint unused1 UNUSED,
697 GdkInputCondition unused2 UNUSED)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200698{
699 netbeans_read();
700}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701#endif
Bram Moolenaar67c53842010-05-22 18:28:27 +0200702
Bram Moolenaar7ad7d012010-11-16 15:49:02 +0100703#define DETACH_MSG "DETACH\n"
704
Bram Moolenaar67c53842010-05-22 18:28:27 +0200705 void
706netbeans_read()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707{
708 static char_u *buf = NULL;
Bram Moolenaar0b65f892010-05-15 14:49:02 +0200709 int len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 int readlen = 0;
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100711#ifdef HAVE_SELECT
712 struct timeval tval;
713 fd_set rfds;
714#else
715# ifdef HAVE_POLL
716 struct pollfd fds;
717# endif
718#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200720 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 {
722 nbdebug(("messageFromNetbeans() called without a socket\n"));
723 return;
724 }
725
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 /* Allocate a buffer to read into. */
727 if (buf == NULL)
728 {
729 buf = alloc(MAXMSGSIZE);
730 if (buf == NULL)
731 return; /* out of memory! */
732 }
733
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100734 /* Keep on reading for as long as there is something to read.
735 * Use select() or poll() to avoid blocking on a message that is exactly
736 * MAXMSGSIZE long. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 for (;;)
738 {
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100739#ifdef HAVE_SELECT
740 FD_ZERO(&rfds);
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200741 FD_SET(nbsock, &rfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +0200742 tval.tv_sec = 0;
743 tval.tv_usec = 0;
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200744 if (select(nbsock + 1, &rfds, NULL, NULL, &tval) <= 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200745 break;
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100746#else
747# ifdef HAVE_POLL
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200748 fds.fd = nbsock;
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100749 fds.events = POLLIN;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200750 if (poll(&fds, 1, 0) <= 0)
751 break;
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100752# endif
753#endif
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200754 len = sock_read(nbsock, buf, MAXMSGSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 if (len <= 0)
756 break; /* error or nothing more to read */
757
758 /* Store the read message in the queue. */
759 save(buf, len);
760 readlen += len;
761 if (len < MAXMSGSIZE)
762 break; /* did read everything that's available */
763 }
764
Bram Moolenaar7ad7d012010-11-16 15:49:02 +0100765 /* Reading a socket disconnection (readlen == 0), or a socket error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 if (readlen <= 0)
767 {
Bram Moolenaar7ad7d012010-11-16 15:49:02 +0100768 /* Queue a "DETACH" netbeans message in the command queue in order to
769 * terminate the netbeans session later. Do not end the session here
770 * directly as we may be running in the context of a call to
771 * netbeans_parse_messages():
772 * netbeans_parse_messages
773 * -> autocmd triggered while processing the netbeans cmd
774 * -> ui_breakcheck
775 * -> gui event loop or select loop
776 * -> netbeans_read()
777 */
Bram Moolenaarde1b0922010-12-24 14:00:17 +0100778 save((char_u *)DETACH_MSG, (int)strlen(DETACH_MSG));
Bram Moolenaar7ad7d012010-11-16 15:49:02 +0100779 nb_close_socket();
780
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 if (len < 0)
Bram Moolenaarf2330482008-06-24 20:19:36 +0000782 {
783 nbdebug(("read from Netbeans socket\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 PERROR(_("read from Netbeans socket"));
Bram Moolenaarf2330482008-06-24 20:19:36 +0000785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 }
787
Bram Moolenaar03531f72010-11-16 15:04:57 +0100788#if defined(NB_HAS_GUI) && defined(FEAT_GUI_GTK)
789 if (NB_HAS_GUI && gtk_main_level() > 0)
Bram Moolenaar7ad7d012010-11-16 15:49:02 +0100790 gtk_main_quit();
Bram Moolenaarf2330482008-06-24 20:19:36 +0000791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792}
793
794/*
795 * Handle one NUL terminated command.
796 *
797 * format of a command from netbeans:
798 *
799 * 6:setTitle!84 "a.c"
800 *
801 * bufno
802 * colon
803 * cmd
804 * !
805 * cmdno
806 * args
807 *
808 * for function calls, the ! is replaced by a /
809 */
810 static void
811nb_parse_cmd(char_u *cmd)
812{
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000813 char *verb;
814 char *q;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 int bufno;
816 int isfunc = -1;
817
818 if (STRCMP(cmd, "DISCONNECT") == 0)
819 {
820 /* We assume the server knows that we can safely exit! */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 /* Disconnect before exiting, Motif hangs in a Select error
822 * message otherwise. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200823 netbeans_close();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 getout(0);
825 /* NOTREACHED */
826 }
827
828 if (STRCMP(cmd, "DETACH") == 0)
829 {
830 /* The IDE is breaking the connection. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200831 netbeans_close();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 return;
833 }
834
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000835 bufno = strtol((char *)cmd, &verb, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836
837 if (*verb != ':')
838 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000839 nbdebug((" missing colon: %s\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 EMSG2("E627: missing colon: %s", cmd);
841 return;
842 }
843 ++verb; /* skip colon */
844
845 for (q = verb; *q; q++)
846 {
847 if (*q == '!')
848 {
849 *q++ = NUL;
850 isfunc = 0;
851 break;
852 }
853 else if (*q == '/')
854 {
855 *q++ = NUL;
856 isfunc = 1;
857 break;
858 }
859 }
860
861 if (isfunc < 0)
862 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000863 nbdebug((" missing ! or / in: %s\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 EMSG2("E628: missing ! or / in: %s", cmd);
865 return;
866 }
867
Bram Moolenaar89d40322006-08-29 15:30:07 +0000868 r_cmdno = strtol(q, &q, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000870 q = (char *)skipwhite((char_u *)q);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871
Bram Moolenaar89d40322006-08-29 15:30:07 +0000872 if (nb_do_cmd(bufno, (char_u *)verb, isfunc, r_cmdno, (char_u *)q) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 {
Bram Moolenaar009b2592004-10-24 19:18:58 +0000874#ifdef NBDEBUG
875 /*
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100876 * This happens because the ExtEd can send a command or 2 after
Bram Moolenaar009b2592004-10-24 19:18:58 +0000877 * doing a stopDocumentListen command. It doesn't harm anything
878 * so I'm disabling it except for debugging.
879 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 nbdebug(("nb_parse_cmd: Command error for \"%s\"\n", cmd));
881 EMSG("E629: bad return from nb_do_cmd");
Bram Moolenaar009b2592004-10-24 19:18:58 +0000882#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 }
884}
885
886struct nbbuf_struct
887{
888 buf_T *bufp;
889 unsigned int fireChanges:1;
890 unsigned int initDone:1;
Bram Moolenaar009b2592004-10-24 19:18:58 +0000891 unsigned int insertDone:1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 unsigned int modified:1;
Bram Moolenaar009b2592004-10-24 19:18:58 +0000893 int nbbuf_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 char *displayname;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 int *signmap;
896 short_u signmaplen;
897 short_u signmapused;
898};
899
900typedef struct nbbuf_struct nbbuf_T;
901
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200902static nbbuf_T *buf_list = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000903static int buf_list_size = 0; /* size of buf_list */
904static int buf_list_used = 0; /* nr of entries in buf_list actually in use */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200906static char **globalsignmap = NULL;
907static int globalsignmaplen = 0;
908static int globalsignmapused = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909
910static int mapsigntype __ARGS((nbbuf_T *, int localsigntype));
911static void addsigntype __ARGS((nbbuf_T *, int localsigntype, char_u *typeName,
912 char_u *tooltip, char_u *glyphfile,
Bram Moolenaar67c53842010-05-22 18:28:27 +0200913 char_u *fg, char_u *bg));
Bram Moolenaar009b2592004-10-24 19:18:58 +0000914static void print_read_msg __ARGS((nbbuf_T *buf));
Bram Moolenaar914703b2010-05-31 21:59:46 +0200915static void print_save_msg __ARGS((nbbuf_T *buf, off_t nchars));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916
917static int curPCtype = -1;
918
919/*
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200920 * Free netbeans resources.
921 */
922 static void
923nb_free()
924{
925 keyQ_T *key_node = keyHead.next;
926 queue_T *cmd_node = head.next;
927 nbbuf_T buf;
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200928 int i;
929
930 /* free the netbeans buffer list */
931 for (i = 0; i < buf_list_used; i++)
932 {
933 buf = buf_list[i];
934 vim_free(buf.displayname);
935 vim_free(buf.signmap);
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100936 if (buf.bufp != NULL)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200937 {
938 buf.bufp->b_netbeans_file = FALSE;
939 buf.bufp->b_was_netbeans_file = FALSE;
940 }
941 }
942 vim_free(buf_list);
943 buf_list = NULL;
944 buf_list_size = 0;
945 buf_list_used = 0;
946
947 /* free the queued key commands */
Bram Moolenaar8d4eecc2012-11-20 17:19:01 +0100948 while (key_node != NULL && key_node != &keyHead)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200949 {
950 keyQ_T *next = key_node->next;
951 vim_free(key_node->keystr);
952 vim_free(key_node);
953 if (next == &keyHead)
954 {
955 keyHead.next = &keyHead;
956 keyHead.prev = &keyHead;
957 break;
958 }
959 key_node = next;
960 }
961
962 /* free the queued netbeans commands */
Bram Moolenaar8d4eecc2012-11-20 17:19:01 +0100963 while (cmd_node != NULL && cmd_node != &head)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200964 {
965 queue_T *next = cmd_node->next;
966 vim_free(cmd_node->buffer);
967 vim_free(cmd_node);
968 if (next == &head)
969 {
970 head.next = &head;
971 head.prev = &head;
972 break;
973 }
974 cmd_node = next;
975 }
976}
977
978/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 * Get the Netbeans buffer number for the specified buffer.
980 */
981 static int
982nb_getbufno(buf_T *bufp)
983{
984 int i;
985
986 for (i = 0; i < buf_list_used; i++)
987 if (buf_list[i].bufp == bufp)
988 return i;
989 return -1;
990}
991
992/*
993 * Is this a NetBeans-owned buffer?
994 */
995 int
996isNetbeansBuffer(buf_T *bufp)
997{
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200998 return NETBEANS_OPEN && bufp->b_netbeans_file;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999}
1000
1001/*
1002 * NetBeans and Vim have different undo models. In Vim, the file isn't
1003 * changed if changes are undone via the undo command. In NetBeans, once
1004 * a change has been made the file is marked as modified until saved. It
1005 * doesn't matter if the change was undone.
1006 *
1007 * So this function is for the corner case where Vim thinks a buffer is
1008 * unmodified but NetBeans thinks it IS modified.
1009 */
1010 int
1011isNetbeansModified(buf_T *bufp)
1012{
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001013 if (isNetbeansBuffer(bufp))
Bram Moolenaar009b2592004-10-24 19:18:58 +00001014 {
1015 int bufno = nb_getbufno(bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016
Bram Moolenaar009b2592004-10-24 19:18:58 +00001017 if (bufno > 0)
1018 return buf_list[bufno].modified;
1019 else
1020 return FALSE;
1021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 else
1023 return FALSE;
1024}
1025
1026/*
1027 * Given a Netbeans buffer number, return the netbeans buffer.
1028 * Returns NULL for 0 or a negative number. A 0 bufno means a
1029 * non-buffer related command has been sent.
1030 */
1031 static nbbuf_T *
1032nb_get_buf(int bufno)
1033{
1034 /* find or create a buffer with the given number */
1035 int incr;
1036
1037 if (bufno <= 0)
1038 return NULL;
1039
1040 if (!buf_list)
1041 {
1042 /* initialize */
1043 buf_list = (nbbuf_T *)alloc_clear(100 * sizeof(nbbuf_T));
1044 buf_list_size = 100;
1045 }
1046 if (bufno >= buf_list_used) /* new */
1047 {
1048 if (bufno >= buf_list_size) /* grow list */
1049 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001050 nbbuf_T *t_buf_list = buf_list;
1051
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 incr = bufno - buf_list_size + 90;
1053 buf_list_size += incr;
1054 buf_list = (nbbuf_T *)vim_realloc(
1055 buf_list, buf_list_size * sizeof(nbbuf_T));
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01001056 if (buf_list == NULL)
1057 {
1058 vim_free(t_buf_list);
1059 buf_list_size = 0;
1060 return NULL;
1061 }
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02001062 vim_memset(buf_list + buf_list_size - incr, 0,
1063 incr * sizeof(nbbuf_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 }
1065
1066 while (buf_list_used <= bufno)
1067 {
1068 /* Default is to fire text changes. */
1069 buf_list[buf_list_used].fireChanges = 1;
1070 ++buf_list_used;
1071 }
1072 }
1073
1074 return buf_list + bufno;
1075}
1076
1077/*
1078 * Return the number of buffers that are modified.
1079 */
1080 static int
1081count_changed_buffers(void)
1082{
1083 buf_T *bufp;
1084 int n;
1085
1086 n = 0;
1087 for (bufp = firstbuf; bufp != NULL; bufp = bufp->b_next)
1088 if (bufp->b_changed)
1089 ++n;
1090 return n;
1091}
1092
1093/*
1094 * End the netbeans session.
1095 */
1096 void
1097netbeans_end(void)
1098{
1099 int i;
1100 static char buf[128];
1101
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001102 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 return;
1104
1105 for (i = 0; i < buf_list_used; i++)
1106 {
1107 if (!buf_list[i].bufp)
1108 continue;
1109 if (netbeansForcedQuit)
1110 {
1111 /* mark as unmodified so NetBeans won't put up dialog on "killed" */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001112 sprintf(buf, "%d:unmodified=%d\n", i, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 nbdebug(("EVT: %s", buf));
1114 nb_send(buf, "netbeans_end");
1115 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001116 sprintf(buf, "%d:killed=%d\n", i, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 nbdebug(("EVT: %s", buf));
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001118 /* nb_send(buf, "netbeans_end"); avoid "write failed" messages */
1119 ignored = sock_write(nbsock, buf, (int)STRLEN(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121}
1122
1123/*
1124 * Send a message to netbeans.
1125 */
1126 static void
1127nb_send(char *buf, char *fun)
1128{
1129 /* Avoid giving pages full of error messages when the other side has
1130 * exited, only mention the first error until the connection works again. */
1131 static int did_error = FALSE;
1132
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001133 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 {
1135 if (!did_error)
Bram Moolenaarf2330482008-06-24 20:19:36 +00001136 {
1137 nbdebug((" %s(): write while not connected\n", fun));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 EMSG2("E630: %s(): write while not connected", fun);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 did_error = TRUE;
1141 }
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001142 else if (sock_write(nbsock, buf, (int)STRLEN(buf)) != (int)STRLEN(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 {
1144 if (!did_error)
Bram Moolenaarf2330482008-06-24 20:19:36 +00001145 {
1146 nbdebug((" %s(): write failed\n", fun));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 EMSG2("E631: %s(): write failed", fun);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 did_error = TRUE;
1150 }
1151 else
1152 did_error = FALSE;
1153}
1154
1155/*
1156 * Some input received from netbeans requires a response. This function
1157 * handles a response with no information (except the command number).
1158 */
1159 static void
1160nb_reply_nil(int cmdno)
1161{
1162 char reply[32];
1163
Bram Moolenaar009b2592004-10-24 19:18:58 +00001164 nbdebug(("REP %d: <none>\n", cmdno));
1165
Bram Moolenaar7ad7d012010-11-16 15:49:02 +01001166 /* Avoid printing an annoying error message. */
1167 if (!NETBEANS_OPEN)
1168 return;
1169
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 sprintf(reply, "%d\n", cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 nb_send(reply, "nb_reply_nil");
1172}
1173
1174
1175/*
1176 * Send a response with text.
1177 * "result" must have been quoted already (using nb_quote()).
1178 */
1179 static void
1180nb_reply_text(int cmdno, char_u *result)
1181{
1182 char_u *reply;
1183
Bram Moolenaar009b2592004-10-24 19:18:58 +00001184 nbdebug(("REP %d: %s\n", cmdno, (char *)result));
1185
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001186 reply = alloc((unsigned)STRLEN(result) + 32);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 sprintf((char *)reply, "%d %s\n", cmdno, (char *)result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 nb_send((char *)reply, "nb_reply_text");
1189
1190 vim_free(reply);
1191}
1192
1193
1194/*
1195 * Send a response with a number result code.
1196 */
1197 static void
1198nb_reply_nr(int cmdno, long result)
1199{
1200 char reply[32];
1201
Bram Moolenaar009b2592004-10-24 19:18:58 +00001202 nbdebug(("REP %d: %ld\n", cmdno, result));
1203
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 sprintf(reply, "%d %ld\n", cmdno, result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 nb_send(reply, "nb_reply_nr");
1206}
1207
1208
1209/*
1210 * Encode newline, ret, backslash, double quote for transmission to NetBeans.
1211 */
1212 static char_u *
1213nb_quote(char_u *txt)
1214{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001215 char_u *buf = alloc((unsigned)(2 * STRLEN(txt) + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 char_u *p = txt;
1217 char_u *q = buf;
1218
1219 if (buf == NULL)
1220 return NULL;
1221 for (; *p; p++)
1222 {
1223 switch (*p)
1224 {
1225 case '\"':
1226 case '\\':
1227 *q++ = '\\'; *q++ = *p; break;
1228 /* case '\t': */
1229 /* *q++ = '\\'; *q++ = 't'; break; */
1230 case '\n':
1231 *q++ = '\\'; *q++ = 'n'; break;
1232 case '\r':
1233 *q++ = '\\'; *q++ = 'r'; break;
1234 default:
1235 *q++ = *p;
1236 break;
1237 }
1238 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01001239 *q = '\0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240
1241 return buf;
1242}
1243
1244
1245/*
1246 * Remove top level double quotes; convert backslashed chars.
1247 * Returns an allocated string (NULL for failure).
1248 * If "endp" is not NULL it is set to the character after the terminating
1249 * quote.
1250 */
1251 static char *
1252nb_unquote(char_u *p, char_u **endp)
1253{
1254 char *result = 0;
1255 char *q;
1256 int done = 0;
1257
1258 /* result is never longer than input */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001259 result = (char *)alloc_clear((unsigned)STRLEN(p) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 if (result == NULL)
1261 return NULL;
1262
1263 if (*p++ != '"')
1264 {
1265 nbdebug(("nb_unquote called with string that doesn't start with a quote!: %s\n",
1266 p));
1267 result[0] = NUL;
1268 return result;
1269 }
1270
1271 for (q = result; !done && *p != NUL;)
1272 {
1273 switch (*p)
1274 {
1275 case '"':
1276 /*
1277 * Unbackslashed dquote marks the end, if first char was dquote.
1278 */
1279 done = 1;
1280 break;
1281
1282 case '\\':
1283 ++p;
1284 switch (*p)
1285 {
1286 case '\\': *q++ = '\\'; break;
1287 case 'n': *q++ = '\n'; break;
1288 case 't': *q++ = '\t'; break;
1289 case 'r': *q++ = '\r'; break;
1290 case '"': *q++ = '"'; break;
1291 case NUL: --p; break;
1292 /* default: skip over illegal chars */
1293 }
1294 ++p;
1295 break;
1296
1297 default:
1298 *q++ = *p++;
1299 }
1300 }
1301
1302 if (endp != NULL)
1303 *endp = p;
1304
1305 return result;
1306}
1307
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001308/*
1309 * Remove from "first" byte to "last" byte (inclusive), at line "lnum" of the
1310 * current buffer. Remove to end of line when "last" is MAXCOL.
1311 */
1312 static void
1313nb_partialremove(linenr_T lnum, colnr_T first, colnr_T last)
1314{
1315 char_u *oldtext, *newtext;
1316 int oldlen;
1317 int lastbyte = last;
1318
1319 oldtext = ml_get(lnum);
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001320 oldlen = (int)STRLEN(oldtext);
Bram Moolenaarb3c70982008-01-18 10:40:55 +00001321 if (first >= (colnr_T)oldlen || oldlen == 0) /* just in case */
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001322 return;
1323 if (lastbyte >= oldlen)
1324 lastbyte = oldlen - 1;
1325 newtext = alloc(oldlen - (int)(lastbyte - first));
1326 if (newtext != NULL)
1327 {
1328 mch_memmove(newtext, oldtext, first);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001329 STRMOVE(newtext + first, oldtext + lastbyte + 1);
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001330 nbdebug((" NEW LINE %d: %s\n", lnum, newtext));
1331 ml_replace(lnum, newtext, FALSE);
1332 }
1333}
1334
1335/*
1336 * Replace the "first" line with the concatenation of the "first" and
1337 * the "other" line. The "other" line is not removed.
1338 */
1339 static void
1340nb_joinlines(linenr_T first, linenr_T other)
1341{
1342 int len_first, len_other;
1343 char_u *p;
1344
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001345 len_first = (int)STRLEN(ml_get(first));
1346 len_other = (int)STRLEN(ml_get(other));
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001347 p = alloc((unsigned)(len_first + len_other + 1));
1348 if (p != NULL)
1349 {
1350 mch_memmove(p, ml_get(first), len_first);
1351 mch_memmove(p + len_first, ml_get(other), len_other + 1);
1352 ml_replace(first, p, FALSE);
1353 }
1354}
1355
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356#define SKIP_STOP 2
1357#define streq(a,b) (strcmp(a,b) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358
1359/*
1360 * Do the actual processing of a single netbeans command or function.
Bram Moolenaar143c38c2007-05-10 16:41:10 +00001361 * The difference between a command and function is that a function
1362 * gets a response (it's required) but a command does not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 * For arguments see comment for nb_parse_cmd().
1364 */
1365 static int
1366nb_do_cmd(
1367 int bufno,
1368 char_u *cmd,
1369 int func,
1370 int cmdno,
1371 char_u *args) /* points to space before arguments or NUL */
1372{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001373 int do_update = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 long off = 0;
1375 nbbuf_T *buf = nb_get_buf(bufno);
1376 static int skip = 0;
1377 int retval = OK;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001378 char *cp; /* for when a char pointer is needed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379
1380 nbdebug(("%s %d: (%d) %s %s\n", (func) ? "FUN" : "CMD", cmdno, bufno, cmd,
1381 STRCMP(cmd, "insert") == 0 ? "<text>" : (char *)args));
1382
1383 if (func)
1384 {
1385/* =====================================================================*/
1386 if (streq((char *)cmd, "getModified"))
1387 {
1388 if (buf == NULL || buf->bufp == NULL)
1389 /* Return the number of buffers that are modified. */
1390 nb_reply_nr(cmdno, (long)count_changed_buffers());
1391 else
1392 /* Return whether the buffer is modified. */
1393 nb_reply_nr(cmdno, (long)(buf->bufp->b_changed
1394 || isNetbeansModified(buf->bufp)));
1395/* =====================================================================*/
1396 }
1397 else if (streq((char *)cmd, "saveAndExit"))
1398 {
1399 /* Note: this will exit Vim if successful. */
1400 coloncmd(":confirm qall");
1401
1402 /* We didn't exit: return the number of changed buffers. */
1403 nb_reply_nr(cmdno, (long)count_changed_buffers());
1404/* =====================================================================*/
1405 }
1406 else if (streq((char *)cmd, "getCursor"))
1407 {
1408 char_u text[200];
1409
1410 /* Note: nb_getbufno() may return -1. This indicates the IDE
1411 * didn't assign a number to the current buffer in response to a
1412 * fileOpened event. */
1413 sprintf((char *)text, "%d %ld %d %ld",
1414 nb_getbufno(curbuf),
1415 (long)curwin->w_cursor.lnum,
1416 (int)curwin->w_cursor.col,
1417 pos2off(curbuf, &curwin->w_cursor));
1418 nb_reply_text(cmdno, text);
1419/* =====================================================================*/
1420 }
Bram Moolenaarc65c4912006-11-14 17:29:46 +00001421 else if (streq((char *)cmd, "getAnno"))
1422 {
1423 long linenum = 0;
1424#ifdef FEAT_SIGNS
1425 if (buf == NULL || buf->bufp == NULL)
1426 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001427 nbdebug((" Invalid buffer identifier in getAnno\n"));
1428 EMSG("E652: Invalid buffer identifier in getAnno");
Bram Moolenaarc65c4912006-11-14 17:29:46 +00001429 retval = FAIL;
1430 }
1431 else
1432 {
1433 int serNum;
1434
1435 cp = (char *)args;
1436 serNum = strtol(cp, &cp, 10);
1437 /* If the sign isn't found linenum will be zero. */
1438 linenum = (long)buf_findsign(buf->bufp, serNum);
1439 }
1440#endif
1441 nb_reply_nr(cmdno, linenum);
1442/* =====================================================================*/
1443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 else if (streq((char *)cmd, "getLength"))
1445 {
1446 long len = 0;
1447
1448 if (buf == NULL || buf->bufp == NULL)
1449 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001450 nbdebug((" invalid buffer identifier in getLength\n"));
1451 EMSG("E632: invalid buffer identifier in getLength");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 retval = FAIL;
1453 }
1454 else
1455 {
1456 len = get_buf_size(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 }
1458 nb_reply_nr(cmdno, len);
1459/* =====================================================================*/
1460 }
1461 else if (streq((char *)cmd, "getText"))
1462 {
1463 long len;
1464 linenr_T nlines;
1465 char_u *text = NULL;
1466 linenr_T lno = 1;
1467 char_u *p;
1468 char_u *line;
1469
1470 if (buf == NULL || buf->bufp == NULL)
1471 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001472 nbdebug((" invalid buffer identifier in getText\n"));
1473 EMSG("E633: invalid buffer identifier in getText");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 retval = FAIL;
1475 }
1476 else
1477 {
1478 len = get_buf_size(buf->bufp);
1479 nlines = buf->bufp->b_ml.ml_line_count;
1480 text = alloc((unsigned)((len > 0)
1481 ? ((len + nlines) * 2) : 4));
1482 if (text == NULL)
1483 {
1484 nbdebug((" nb_do_cmd: getText has null text field\n"));
1485 retval = FAIL;
1486 }
1487 else
1488 {
1489 p = text;
1490 *p++ = '\"';
1491 for (; lno <= nlines ; lno++)
1492 {
1493 line = nb_quote(ml_get_buf(buf->bufp, lno, FALSE));
1494 if (line != NULL)
1495 {
1496 STRCPY(p, line);
1497 p += STRLEN(line);
1498 *p++ = '\\';
1499 *p++ = 'n';
Bram Moolenaar009b2592004-10-24 19:18:58 +00001500 vim_free(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 }
1503 *p++ = '\"';
1504 *p = '\0';
1505 }
1506 }
1507 if (text == NULL)
1508 nb_reply_text(cmdno, (char_u *)"");
1509 else
1510 {
1511 nb_reply_text(cmdno, text);
1512 vim_free(text);
1513 }
1514/* =====================================================================*/
1515 }
1516 else if (streq((char *)cmd, "remove"))
1517 {
1518 long count;
1519 pos_T first, last;
1520 pos_T *pos;
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001521 pos_T *next;
1522 linenr_T del_from_lnum, del_to_lnum; /* lines to be deleted as a whole */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 int oldFire = netbeansFireChanges;
1524 int oldSuppress = netbeansSuppressNoLines;
1525 int wasChanged;
1526
1527 if (skip >= SKIP_STOP)
1528 {
1529 nbdebug((" Skipping %s command\n", (char *) cmd));
1530 nb_reply_nil(cmdno);
1531 return OK;
1532 }
1533
1534 if (buf == NULL || buf->bufp == NULL)
1535 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001536 nbdebug((" invalid buffer identifier in remove\n"));
1537 EMSG("E634: invalid buffer identifier in remove");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 retval = FAIL;
1539 }
1540 else
1541 {
1542 netbeansFireChanges = FALSE;
1543 netbeansSuppressNoLines = TRUE;
1544
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001545 nb_set_curbuf(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 wasChanged = buf->bufp->b_changed;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001547 cp = (char *)args;
1548 off = strtol(cp, &cp, 10);
1549 count = strtol(cp, &cp, 10);
1550 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 /* delete "count" chars, starting at "off" */
1552 pos = off2pos(buf->bufp, off);
1553 if (!pos)
1554 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001555 nbdebug((" !bad position\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 nb_reply_text(cmdno, (char_u *)"!bad position");
1557 netbeansFireChanges = oldFire;
1558 netbeansSuppressNoLines = oldSuppress;
1559 return FAIL;
1560 }
1561 first = *pos;
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001562 nbdebug((" FIRST POS: line %d, col %d\n",
1563 first.lnum, first.col));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 pos = off2pos(buf->bufp, off+count-1);
1565 if (!pos)
1566 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001567 nbdebug((" !bad count\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 nb_reply_text(cmdno, (char_u *)"!bad count");
1569 netbeansFireChanges = oldFire;
1570 netbeansSuppressNoLines = oldSuppress;
1571 return FAIL;
1572 }
1573 last = *pos;
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001574 nbdebug((" LAST POS: line %d, col %d\n",
1575 last.lnum, last.col));
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001576 del_from_lnum = first.lnum;
1577 del_to_lnum = last.lnum;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001578 do_update = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001580 /* Get the position of the first byte after the deleted
1581 * section. "next" is NULL when deleting to the end of the
1582 * file. */
1583 next = off2pos(buf->bufp, off + count);
1584
1585 /* Remove part of the first line. */
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001586 if (first.col != 0
1587 || (next != NULL && first.lnum == next->lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 {
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001589 if (first.lnum != last.lnum
1590 || (next != NULL && first.lnum != next->lnum))
1591 {
1592 /* remove to the end of the first line */
1593 nb_partialremove(first.lnum, first.col,
1594 (colnr_T)MAXCOL);
1595 if (first.lnum == last.lnum)
1596 {
1597 /* Partial line to remove includes the end of
1598 * line. Join the line with the next one, have
1599 * the next line deleted below. */
1600 nb_joinlines(first.lnum, next->lnum);
1601 del_to_lnum = next->lnum;
1602 }
1603 }
1604 else
1605 {
1606 /* remove within one line */
1607 nb_partialremove(first.lnum, first.col, last.col);
1608 }
1609 ++del_from_lnum; /* don't delete the first line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 }
1611
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001612 /* Remove part of the last line. */
1613 if (first.lnum != last.lnum && next != NULL
1614 && next->col != 0 && last.lnum == next->lnum)
1615 {
1616 nb_partialremove(last.lnum, 0, last.col);
1617 if (del_from_lnum > first.lnum)
1618 {
1619 /* Join end of last line to start of first line; last
1620 * line is deleted below. */
1621 nb_joinlines(first.lnum, last.lnum);
1622 }
1623 else
1624 /* First line is deleted as a whole, keep the last
1625 * line. */
1626 --del_to_lnum;
1627 }
1628
1629 /* First is partial line; last line to remove includes
1630 * the end of line; join first line to line following last
1631 * line; line following last line is deleted below. */
1632 if (first.lnum != last.lnum && del_from_lnum > first.lnum
1633 && next != NULL && last.lnum != next->lnum)
1634 {
1635 nb_joinlines(first.lnum, next->lnum);
1636 del_to_lnum = next->lnum;
1637 }
1638
1639 /* Delete whole lines if there are any. */
1640 if (del_to_lnum >= del_from_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 {
1642 int i;
1643
1644 /* delete signs from the lines being deleted */
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001645 for (i = del_from_lnum; i <= del_to_lnum; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 {
1647 int id = buf_findsign_id(buf->bufp, (linenr_T)i);
1648 if (id > 0)
1649 {
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001650 nbdebug((" Deleting sign %d on line %d\n",
1651 id, i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 buf_delsign(buf->bufp, id);
1653 }
1654 else
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001655 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 nbdebug((" No sign on line %d\n", i));
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 }
1659
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001660 nbdebug((" Deleting lines %d through %d\n",
1661 del_from_lnum, del_to_lnum));
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001662 curwin->w_cursor.lnum = del_from_lnum;
1663 curwin->w_cursor.col = 0;
1664 del_lines(del_to_lnum - del_from_lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 }
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001666
1667 /* Leave cursor at first deleted byte. */
1668 curwin->w_cursor = first;
1669 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 buf->bufp->b_changed = wasChanged; /* logically unchanged */
1671 netbeansFireChanges = oldFire;
1672 netbeansSuppressNoLines = oldSuppress;
1673
1674 u_blockfree(buf->bufp);
1675 u_clearall(buf->bufp);
1676 }
1677 nb_reply_nil(cmdno);
1678/* =====================================================================*/
1679 }
1680 else if (streq((char *)cmd, "insert"))
1681 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 char_u *to_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683
1684 if (skip >= SKIP_STOP)
1685 {
1686 nbdebug((" Skipping %s command\n", (char *) cmd));
1687 nb_reply_nil(cmdno);
1688 return OK;
1689 }
1690
1691 /* get offset */
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001692 cp = (char *)args;
1693 off = strtol(cp, &cp, 10);
1694 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695
1696 /* get text to be inserted */
1697 args = skipwhite(args);
1698 args = to_free = (char_u *)nb_unquote(args, NULL);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001699 /*
1700 nbdebug((" CHUNK[%d]: %d bytes at offset %d\n",
1701 buf->bufp->b_ml.ml_line_count, STRLEN(args), off));
1702 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703
1704 if (buf == NULL || buf->bufp == NULL)
1705 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001706 nbdebug((" invalid buffer identifier in insert\n"));
1707 EMSG("E635: invalid buffer identifier in insert");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 retval = FAIL;
1709 }
1710 else if (args != NULL)
1711 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001712 int ff_detected = EOL_UNKNOWN;
1713 int buf_was_empty = (buf->bufp->b_ml.ml_flags & ML_EMPTY);
1714 size_t len = 0;
1715 int added = 0;
1716 int oldFire = netbeansFireChanges;
1717 int old_b_changed;
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001718 char_u *nlp;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001719 linenr_T lnum;
1720 linenr_T lnum_start;
1721 pos_T *pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 netbeansFireChanges = 0;
1724
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001725 /* Jump to the buffer where we insert. After this "curbuf"
1726 * can be used. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001727 nb_set_curbuf(buf->bufp);
Bram Moolenaara3227e22006-03-08 21:32:40 +00001728 old_b_changed = curbuf->b_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001730 /* Convert the specified character offset into a lnum/col
1731 * position. */
Bram Moolenaara3227e22006-03-08 21:32:40 +00001732 pos = off2pos(curbuf, off);
1733 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001735 if (pos->lnum <= 0)
1736 lnum_start = 1;
1737 else
1738 lnum_start = pos->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 }
1740 else
1741 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001742 /* If the given position is not found, assume we want
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 * the end of the file. See setLocAndSize HACK. */
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001744 if (buf_was_empty)
1745 lnum_start = 1; /* above empty line */
1746 else
1747 lnum_start = curbuf->b_ml.ml_line_count + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001748 }
1749
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001750 /* "lnum" is the line where we insert: either append to it or
1751 * insert a new line above it. */
1752 lnum = lnum_start;
1753
1754 /* Loop over the "\n" separated lines of the argument. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001755 do_update = 1;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001756 while (*args != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 {
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001758 nlp = vim_strchr(args, '\n');
1759 if (nlp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001761 /* Incomplete line, probably truncated. Next "insert"
1762 * command should append to this one. */
1763 len = STRLEN(args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001765 else
1766 {
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001767 len = nlp - args;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001768
1769 /*
1770 * We need to detect EOL style, because the commands
1771 * use a character offset.
1772 */
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001773 if (nlp > args && nlp[-1] == '\r')
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001774 {
1775 ff_detected = EOL_DOS;
1776 --len;
1777 }
1778 else
1779 ff_detected = EOL_UNIX;
1780 }
1781 args[len] = NUL;
1782
1783 if (lnum == lnum_start
1784 && ((pos != NULL && pos->col > 0)
1785 || (lnum == 1 && buf_was_empty)))
1786 {
1787 char_u *oldline = ml_get(lnum);
1788 char_u *newline;
1789
Bram Moolenaare4365282012-04-20 19:47:05 +02001790 /* Insert halfway a line. */
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001791 newline = alloc_check(
1792 (unsigned)(STRLEN(oldline) + len + 1));
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001793 if (newline != NULL)
1794 {
Bram Moolenaare4365282012-04-20 19:47:05 +02001795 mch_memmove(newline, oldline, (size_t)pos->col);
1796 newline[pos->col] = NUL;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001797 STRCAT(newline, args);
Bram Moolenaare4365282012-04-20 19:47:05 +02001798 STRCAT(newline, oldline + pos->col);
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001799 ml_replace(lnum, newline, FALSE);
1800 }
1801 }
1802 else
1803 {
1804 /* Append a new line. Not that we always do this,
1805 * also when the text doesn't end in a "\n". */
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001806 ml_append((linenr_T)(lnum - 1), args,
1807 (colnr_T)(len + 1), FALSE);
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001808 ++added;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001809 }
1810
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001811 if (nlp == NULL)
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001812 break;
1813 ++lnum;
Bram Moolenaar309cbc32012-01-10 22:31:31 +01001814 args = nlp + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001815 }
1816
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001817 /* Adjust the marks below the inserted lines. */
1818 appended_lines_mark(lnum_start - 1, (long)added);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001820 /*
1821 * When starting with an empty buffer set the fileformat.
1822 * This is just guessing...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 */
1824 if (buf_was_empty)
1825 {
1826 if (ff_detected == EOL_UNKNOWN)
Bram Moolenaare7fedb62015-12-31 19:07:19 +01001827#if defined(MSDOS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 ff_detected = EOL_DOS;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001829#else
1830 ff_detected = EOL_UNIX;
1831#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 set_fileformat(ff_detected, OPT_LOCAL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00001833 curbuf->b_start_ffc = *curbuf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 }
1835
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 /*
1837 * XXX - GRP - Is the next line right? If I've inserted
1838 * text the buffer has been updated but not written. Will
1839 * netbeans guarantee to write it? Even if I do a :q! ?
1840 */
Bram Moolenaara3227e22006-03-08 21:32:40 +00001841 curbuf->b_changed = old_b_changed; /* logically unchanged */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 netbeansFireChanges = oldFire;
1843
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001844 /* Undo info is invalid now... */
Bram Moolenaara3227e22006-03-08 21:32:40 +00001845 u_blockfree(curbuf);
1846 u_clearall(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 }
1848 vim_free(to_free);
1849 nb_reply_nil(cmdno); /* or !error */
1850 }
1851 else
1852 {
1853 nbdebug(("UNIMPLEMENTED FUNCTION: %s\n", cmd));
1854 nb_reply_nil(cmdno);
1855 retval = FAIL;
1856 }
1857 }
1858 else /* Not a function; no reply required. */
1859 {
1860/* =====================================================================*/
1861 if (streq((char *)cmd, "create"))
1862 {
1863 /* Create a buffer without a name. */
1864 if (buf == NULL)
1865 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001866 nbdebug((" invalid buffer identifier in create\n"));
1867 EMSG("E636: invalid buffer identifier in create");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 return FAIL;
1869 }
1870 vim_free(buf->displayname);
1871 buf->displayname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872
1873 netbeansReadFile = 0; /* don't try to open disk file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001874 do_ecmd(0, NULL, 0, 0, ECMD_ONE, ECMD_HIDE + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 netbeansReadFile = 1;
1876 buf->bufp = curbuf;
1877 maketitle();
Bram Moolenaar009b2592004-10-24 19:18:58 +00001878 buf->insertDone = FALSE;
Bram Moolenaar67c53842010-05-22 18:28:27 +02001879#if defined(FEAT_MENU) && defined(FEAT_GUI)
Bram Moolenaard54a6882010-08-09 22:49:00 +02001880 if (gui.in_use)
1881 gui_update_menus(0);
Bram Moolenaar67c53842010-05-22 18:28:27 +02001882#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883/* =====================================================================*/
1884 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001885 else if (streq((char *)cmd, "insertDone"))
1886 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001887 if (buf == NULL || buf->bufp == NULL)
1888 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001889 nbdebug((" invalid buffer identifier in insertDone\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001890 }
1891 else
1892 {
1893 buf->bufp->b_start_eol = *args == 'T';
1894 buf->insertDone = TRUE;
1895 args += 2;
1896 buf->bufp->b_p_ro = *args == 'T';
1897 print_read_msg(buf);
1898 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001899/* =====================================================================*/
1900 }
1901 else if (streq((char *)cmd, "saveDone"))
1902 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001903 long savedChars = atol((char *)args);
1904
1905 if (buf == NULL || buf->bufp == NULL)
1906 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001907 nbdebug((" invalid buffer identifier in saveDone\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001908 }
1909 else
1910 print_save_msg(buf, savedChars);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001911/* =====================================================================*/
1912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 else if (streq((char *)cmd, "startDocumentListen"))
1914 {
1915 if (buf == NULL)
1916 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001917 nbdebug((" invalid buffer identifier in startDocumentListen\n"));
1918 EMSG("E637: invalid buffer identifier in startDocumentListen");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 return FAIL;
1920 }
1921 buf->fireChanges = 1;
1922/* =====================================================================*/
1923 }
1924 else if (streq((char *)cmd, "stopDocumentListen"))
1925 {
1926 if (buf == NULL)
1927 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001928 nbdebug((" invalid buffer identifier in stopDocumentListen\n"));
1929 EMSG("E638: invalid buffer identifier in stopDocumentListen");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 return FAIL;
1931 }
1932 buf->fireChanges = 0;
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001933 if (buf->bufp != NULL && buf->bufp->b_was_netbeans_file)
Bram Moolenaar009b2592004-10-24 19:18:58 +00001934 {
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001935 if (!buf->bufp->b_netbeans_file)
Bram Moolenaarf2330482008-06-24 20:19:36 +00001936 {
1937 nbdebug(("E658: NetBeans connection lost for buffer %ld\n", buf->bufp->b_fnum));
Bram Moolenaar009b2592004-10-24 19:18:58 +00001938 EMSGN(_("E658: NetBeans connection lost for buffer %ld"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 buf->bufp->b_fnum);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001940 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001941 else
1942 {
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001943 /* NetBeans uses stopDocumentListen when it stops editing
1944 * a file. It then expects the buffer in Vim to
1945 * disappear. */
1946 do_bufdel(DOBUF_DEL, (char_u *)"", 1,
1947 buf->bufp->b_fnum, buf->bufp->b_fnum, TRUE);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001948 vim_memset(buf, 0, sizeof(nbbuf_T));
1949 }
1950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951/* =====================================================================*/
1952 }
1953 else if (streq((char *)cmd, "setTitle"))
1954 {
1955 if (buf == NULL)
1956 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001957 nbdebug((" invalid buffer identifier in setTitle\n"));
1958 EMSG("E639: invalid buffer identifier in setTitle");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 return FAIL;
1960 }
1961 vim_free(buf->displayname);
1962 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963/* =====================================================================*/
1964 }
1965 else if (streq((char *)cmd, "initDone"))
1966 {
1967 if (buf == NULL || buf->bufp == NULL)
1968 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001969 nbdebug((" invalid buffer identifier in initDone\n"));
1970 EMSG("E640: invalid buffer identifier in initDone");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 return FAIL;
1972 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001973 do_update = 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001974 buf->initDone = TRUE;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001975 nb_set_curbuf(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976#if defined(FEAT_AUTOCMD)
1977 apply_autocmds(EVENT_BUFREADPOST, 0, 0, FALSE, buf->bufp);
1978#endif
1979
1980 /* handle any postponed key commands */
1981 handle_key_queue();
1982/* =====================================================================*/
1983 }
1984 else if (streq((char *)cmd, "setBufferNumber")
1985 || streq((char *)cmd, "putBufferNumber"))
1986 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00001987 char_u *path;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 buf_T *bufp;
1989
1990 if (buf == NULL)
1991 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001992 nbdebug((" invalid buffer identifier in setBufferNumber\n"));
1993 EMSG("E641: invalid buffer identifier in setBufferNumber");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 return FAIL;
1995 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001996 path = (char_u *)nb_unquote(args, NULL);
1997 if (path == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 return FAIL;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001999 bufp = buflist_findname(path);
2000 vim_free(path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 if (bufp == NULL)
2002 {
Bram Moolenaarec906222009-02-21 21:14:00 +00002003 nbdebug((" File %s not found in setBufferNumber\n", args));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 EMSG2("E642: File %s not found in setBufferNumber", args);
2005 return FAIL;
2006 }
2007 buf->bufp = bufp;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002008 buf->nbbuf_number = bufp->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009
2010 /* "setBufferNumber" has the side effect of jumping to the buffer
2011 * (don't know why!). Don't do that for "putBufferNumber". */
2012 if (*cmd != 'p')
2013 coloncmd(":buffer %d", bufp->b_fnum);
2014 else
2015 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002016 buf->initDone = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017
2018 /* handle any postponed key commands */
2019 handle_key_queue();
2020 }
2021
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022/* =====================================================================*/
2023 }
2024 else if (streq((char *)cmd, "setFullName"))
2025 {
2026 if (buf == NULL)
2027 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002028 nbdebug((" invalid buffer identifier in setFullName\n"));
2029 EMSG("E643: invalid buffer identifier in setFullName");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 return FAIL;
2031 }
2032 vim_free(buf->displayname);
2033 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034
2035 netbeansReadFile = 0; /* don't try to open disk file */
2036 do_ecmd(0, (char_u *)buf->displayname, 0, 0, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002037 ECMD_HIDE + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 netbeansReadFile = 1;
2039 buf->bufp = curbuf;
2040 maketitle();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002041#if defined(FEAT_MENU) && defined(FEAT_GUI)
Bram Moolenaard54a6882010-08-09 22:49:00 +02002042 if (gui.in_use)
2043 gui_update_menus(0);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002044#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045/* =====================================================================*/
2046 }
2047 else if (streq((char *)cmd, "editFile"))
2048 {
2049 if (buf == NULL)
2050 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002051 nbdebug((" invalid buffer identifier in editFile\n"));
2052 EMSG("E644: invalid buffer identifier in editFile");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 return FAIL;
2054 }
2055 /* Edit a file: like create + setFullName + read the file. */
2056 vim_free(buf->displayname);
2057 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 do_ecmd(0, (char_u *)buf->displayname, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002059 ECMD_HIDE + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 buf->bufp = curbuf;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002061 buf->initDone = TRUE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002062 do_update = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063#if defined(FEAT_TITLE)
2064 maketitle();
2065#endif
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, "setVisible"))
2073 {
2074 if (buf == NULL || buf->bufp == NULL)
2075 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002076 nbdebug((" invalid buffer identifier in setVisible\n"));
2077 /* This message was commented out, probably because it can
2078 * happen when shutting down. */
2079 if (p_verbose > 0)
2080 EMSG("E645: invalid buffer identifier in setVisible");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 return FAIL;
2082 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002083 if (streq((char *)args, "T") && buf->bufp != curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 {
2085 exarg_T exarg;
2086 exarg.cmd = (char_u *)"goto";
2087 exarg.forceit = FALSE;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002088 dosetvisible = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 goto_buffer(&exarg, DOBUF_FIRST, FORWARD, buf->bufp->b_fnum);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002090 do_update = 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002091 dosetvisible = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092
Bram Moolenaar67c53842010-05-22 18:28:27 +02002093#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 /* Side effect!!!. */
Bram Moolenaard54a6882010-08-09 22:49:00 +02002095 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 gui_mch_set_foreground();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002097#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099/* =====================================================================*/
2100 }
2101 else if (streq((char *)cmd, "raise"))
2102 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002103#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 /* Bring gvim to the foreground. */
Bram Moolenaard54a6882010-08-09 22:49:00 +02002105 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 gui_mch_set_foreground();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002107#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108/* =====================================================================*/
2109 }
2110 else if (streq((char *)cmd, "setModified"))
2111 {
Bram Moolenaarff064e12008-06-09 13:10:45 +00002112 int prev_b_changed;
2113
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 if (buf == NULL || buf->bufp == NULL)
2115 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002116 nbdebug((" invalid buffer identifier in setModified\n"));
2117 /* This message was commented out, probably because it can
2118 * happen when shutting down. */
2119 if (p_verbose > 0)
2120 EMSG("E646: invalid buffer identifier in setModified");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 return FAIL;
2122 }
Bram Moolenaarff064e12008-06-09 13:10:45 +00002123 prev_b_changed = buf->bufp->b_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 if (streq((char *)args, "T"))
Bram Moolenaarff064e12008-06-09 13:10:45 +00002125 buf->bufp->b_changed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 else
2127 {
2128 struct stat st;
2129
2130 /* Assume NetBeans stored the file. Reset the timestamp to
2131 * avoid "file changed" warnings. */
2132 if (buf->bufp->b_ffname != NULL
2133 && mch_stat((char *)buf->bufp->b_ffname, &st) >= 0)
2134 buf_store_time(buf->bufp, &st, buf->bufp->b_ffname);
Bram Moolenaarff064e12008-06-09 13:10:45 +00002135 buf->bufp->b_changed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 }
2137 buf->modified = buf->bufp->b_changed;
Bram Moolenaarff064e12008-06-09 13:10:45 +00002138 if (prev_b_changed != buf->bufp->b_changed)
2139 {
2140#ifdef FEAT_WINDOWS
2141 check_status(buf->bufp);
2142 redraw_tabline = TRUE;
2143#endif
2144#ifdef FEAT_TITLE
2145 maketitle();
2146#endif
2147 update_screen(0);
2148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149/* =====================================================================*/
2150 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002151 else if (streq((char *)cmd, "setModtime"))
2152 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002153 if (buf == NULL || buf->bufp == NULL)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002154 nbdebug((" invalid buffer identifier in setModtime\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002155 else
2156 buf->bufp->b_mtime = atoi((char *)args);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002157/* =====================================================================*/
2158 }
2159 else if (streq((char *)cmd, "setReadOnly"))
2160 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002161 if (buf == NULL || buf->bufp == NULL)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002162 nbdebug((" invalid buffer identifier in setReadOnly\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002163 else if (streq((char *)args, "T"))
Bram Moolenaar009b2592004-10-24 19:18:58 +00002164 buf->bufp->b_p_ro = TRUE;
2165 else
2166 buf->bufp->b_p_ro = FALSE;
2167/* =====================================================================*/
2168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 else if (streq((char *)cmd, "setMark"))
2170 {
2171 /* not yet */
2172/* =====================================================================*/
2173 }
2174 else if (streq((char *)cmd, "showBalloon"))
2175 {
2176#if defined(FEAT_BEVAL)
2177 static char *text = NULL;
2178
2179 /*
2180 * Set up the Balloon Expression Evaluation area.
2181 * Ignore 'ballooneval' here.
2182 * The text pointer must remain valid for a while.
2183 */
2184 if (balloonEval != NULL)
2185 {
2186 vim_free(text);
2187 text = nb_unquote(args, NULL);
2188 if (text != NULL)
2189 gui_mch_post_balloon(balloonEval, (char_u *)text);
2190 }
2191#endif
2192/* =====================================================================*/
2193 }
2194 else if (streq((char *)cmd, "setDot"))
2195 {
2196 pos_T *pos;
2197#ifdef NBDEBUG
2198 char_u *s;
2199#endif
2200
2201 if (buf == NULL || buf->bufp == NULL)
2202 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002203 nbdebug((" invalid buffer identifier in setDot\n"));
2204 EMSG("E647: invalid buffer identifier in setDot");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 return FAIL;
2206 }
2207
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002208 nb_set_curbuf(buf->bufp);
2209
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 /* Don't want Visual mode now. */
2211 if (VIsual_active)
2212 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213#ifdef NBDEBUG
2214 s = args;
2215#endif
2216 pos = get_off_or_lnum(buf->bufp, &args);
2217 if (pos)
2218 {
2219 curwin->w_cursor = *pos;
2220 check_cursor();
2221#ifdef FEAT_FOLDING
2222 foldOpenCursor();
2223#endif
2224 }
2225 else
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002226 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 nbdebug((" BAD POSITION in setDot: %s\n", s));
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229
2230 /* gui_update_cursor(TRUE, FALSE); */
2231 /* update_curbuf(NOT_VALID); */
2232 update_topline(); /* scroll to show the line */
2233 update_screen(VALID);
2234 setcursor();
Bram Moolenaar96bcc5e2011-04-01 15:33:59 +02002235 cursor_on();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 out_flush();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002237#ifdef FEAT_GUI
Bram Moolenaard54a6882010-08-09 22:49:00 +02002238 if (gui.in_use)
2239 {
2240 gui_update_cursor(TRUE, FALSE);
2241 gui_mch_flush();
2242 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02002243#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 /* Quit a hit-return or more prompt. */
2245 if (State == HITRETURN || State == ASKMORE)
2246 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247#ifdef FEAT_GUI_GTK
Bram Moolenaard54a6882010-08-09 22:49:00 +02002248 if (gui.in_use && gtk_main_level() > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 gtk_main_quit();
2250#endif
2251 }
2252/* =====================================================================*/
2253 }
2254 else if (streq((char *)cmd, "close"))
2255 {
2256#ifdef NBDEBUG
2257 char *name = "<NONE>";
2258#endif
2259
2260 if (buf == NULL)
2261 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002262 nbdebug((" invalid buffer identifier in close\n"));
2263 EMSG("E648: invalid buffer identifier in close");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 return FAIL;
2265 }
2266
2267#ifdef NBDEBUG
2268 if (buf->displayname != NULL)
2269 name = buf->displayname;
2270#endif
Bram Moolenaarf2330482008-06-24 20:19:36 +00002271 if (buf->bufp == NULL)
2272 {
2273 nbdebug((" invalid buffer identifier in close\n"));
2274 /* This message was commented out, probably because it can
2275 * happen when shutting down. */
2276 if (p_verbose > 0)
2277 EMSG("E649: invalid buffer identifier in close");
2278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 nbdebug((" CLOSE %d: %s\n", bufno, name));
Bram Moolenaar67c53842010-05-22 18:28:27 +02002280#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 need_mouse_correct = TRUE;
Bram Moolenaar67c53842010-05-22 18:28:27 +02002282#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 if (buf->bufp != NULL)
2284 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD,
2285 buf->bufp->b_fnum, TRUE);
Bram Moolenaar8d602722006-08-08 19:34:19 +00002286 buf->bufp = NULL;
2287 buf->initDone = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002288 do_update = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289/* =====================================================================*/
2290 }
2291 else if (streq((char *)cmd, "setStyle")) /* obsolete... */
2292 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002293 nbdebug((" setStyle is obsolete!\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294/* =====================================================================*/
2295 }
2296 else if (streq((char *)cmd, "setExitDelay"))
2297 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002298 /* Only used in version 2.1. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299/* =====================================================================*/
2300 }
2301 else if (streq((char *)cmd, "defineAnnoType"))
2302 {
2303#ifdef FEAT_SIGNS
2304 int typeNum;
2305 char_u *typeName;
2306 char_u *tooltip;
2307 char_u *p;
2308 char_u *glyphFile;
Bram Moolenaar67c53842010-05-22 18:28:27 +02002309 int parse_error = FALSE;
2310 char_u *fg;
2311 char_u *bg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312
2313 if (buf == NULL)
2314 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002315 nbdebug((" invalid buffer identifier in defineAnnoType\n"));
2316 EMSG("E650: invalid buffer identifier in defineAnnoType");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 return FAIL;
2318 }
2319
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002320 cp = (char *)args;
2321 typeNum = strtol(cp, &cp, 10);
2322 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 args = skipwhite(args);
2324 typeName = (char_u *)nb_unquote(args, &args);
2325 args = skipwhite(args + 1);
2326 tooltip = (char_u *)nb_unquote(args, &args);
2327 args = skipwhite(args + 1);
2328
2329 p = (char_u *)nb_unquote(args, &args);
2330 glyphFile = vim_strsave_escaped(p, escape_chars);
2331 vim_free(p);
2332
2333 args = skipwhite(args + 1);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002334 p = skiptowhite(args);
2335 if (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002337 *p = NUL;
2338 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02002340 fg = vim_strsave(args);
2341 bg = vim_strsave(p);
2342 if (STRLEN(fg) > MAX_COLOR_LENGTH || STRLEN(bg) > MAX_COLOR_LENGTH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002344 EMSG("E532: highlighting color name too long in defineAnnoType");
2345 vim_free(typeName);
2346 parse_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02002348 else if (typeName != NULL && tooltip != NULL && glyphFile != NULL)
2349 addsigntype(buf, typeNum, typeName, tooltip, glyphFile, fg, bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 else
2351 vim_free(typeName);
2352
2353 /* don't free typeName; it's used directly in addsigntype() */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002354 vim_free(fg);
2355 vim_free(bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 vim_free(tooltip);
2357 vim_free(glyphFile);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002358 if (parse_error)
2359 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360
2361#endif
2362/* =====================================================================*/
2363 }
2364 else if (streq((char *)cmd, "addAnno"))
2365 {
2366#ifdef FEAT_SIGNS
2367 int serNum;
2368 int localTypeNum;
2369 int typeNum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 pos_T *pos;
2371
2372 if (buf == NULL || buf->bufp == NULL)
2373 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002374 nbdebug((" invalid buffer identifier in addAnno\n"));
2375 EMSG("E651: invalid buffer identifier in addAnno");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 return FAIL;
2377 }
2378
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002379 do_update = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002381 cp = (char *)args;
2382 serNum = strtol(cp, &cp, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383
2384 /* Get the typenr specific for this buffer and convert it to
2385 * the global typenumber, as used for the sign name. */
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002386 localTypeNum = strtol(cp, &cp, 10);
2387 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 typeNum = mapsigntype(buf, localTypeNum);
2389
2390 pos = get_off_or_lnum(buf->bufp, &args);
2391
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002392 cp = (char *)args;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002393 ignored = (int)strtol(cp, &cp, 10);
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002394 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395# ifdef NBDEBUG
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002396 if (ignored != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002398 nbdebug((" partial line annotation -- Not Yet Implemented!\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 }
2400# endif
2401 if (serNum >= GUARDEDOFFSET)
2402 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002403 nbdebug((" too many annotations! ignoring...\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 return FAIL;
2405 }
2406 if (pos)
2407 {
Bram Moolenaarec906222009-02-21 21:14:00 +00002408 coloncmd(":sign place %d line=%ld name=%d buffer=%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 serNum, pos->lnum, typeNum, buf->bufp->b_fnum);
2410 if (typeNum == curPCtype)
2411 coloncmd(":sign jump %d buffer=%d", serNum,
2412 buf->bufp->b_fnum);
2413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414#endif
2415/* =====================================================================*/
2416 }
2417 else if (streq((char *)cmd, "removeAnno"))
2418 {
2419#ifdef FEAT_SIGNS
2420 int serNum;
2421
2422 if (buf == NULL || buf->bufp == NULL)
2423 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002424 nbdebug((" invalid buffer identifier in removeAnno\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 return FAIL;
2426 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002427 do_update = 1;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002428 cp = (char *)args;
2429 serNum = strtol(cp, &cp, 10);
2430 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 coloncmd(":sign unplace %d buffer=%d",
2432 serNum, buf->bufp->b_fnum);
2433 redraw_buf_later(buf->bufp, NOT_VALID);
2434#endif
2435/* =====================================================================*/
2436 }
2437 else if (streq((char *)cmd, "moveAnnoToFront"))
2438 {
2439#ifdef FEAT_SIGNS
Bram Moolenaarf2330482008-06-24 20:19:36 +00002440 nbdebug((" moveAnnoToFront: Not Yet Implemented!\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441#endif
2442/* =====================================================================*/
2443 }
2444 else if (streq((char *)cmd, "guard") || streq((char *)cmd, "unguard"))
2445 {
2446 int len;
2447 pos_T first;
2448 pos_T last;
2449 pos_T *pos;
2450 int un = (cmd[0] == 'u');
2451 static int guardId = GUARDEDOFFSET;
2452
2453 if (skip >= SKIP_STOP)
2454 {
2455 nbdebug((" Skipping %s command\n", (char *) cmd));
2456 return OK;
2457 }
2458
2459 nb_init_graphics();
2460
2461 if (buf == NULL || buf->bufp == NULL)
2462 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002463 nbdebug((" invalid buffer identifier in %s command\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 return FAIL;
2465 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002466 nb_set_curbuf(buf->bufp);
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002467 cp = (char *)args;
2468 off = strtol(cp, &cp, 10);
2469 len = strtol(cp, NULL, 10);
2470 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 pos = off2pos(buf->bufp, off);
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002472 do_update = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 if (!pos)
2474 nbdebug((" no such start pos in %s, %ld\n", cmd, off));
2475 else
2476 {
2477 first = *pos;
2478 pos = off2pos(buf->bufp, off + len - 1);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002479 if (pos != NULL && pos->col == 0)
2480 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 /*
2482 * In Java Swing the offset is a position between 2
2483 * characters. If col == 0 then we really want the
2484 * previous line as the end.
2485 */
2486 pos = off2pos(buf->bufp, off + len - 2);
2487 }
2488 if (!pos)
2489 nbdebug((" no such end pos in %s, %ld\n",
2490 cmd, off + len - 1));
2491 else
2492 {
2493 long lnum;
2494 last = *pos;
2495 /* set highlight for region */
2496 nbdebug((" %sGUARD %ld,%d to %ld,%d\n", (un) ? "UN" : "",
2497 first.lnum, first.col,
2498 last.lnum, last.col));
2499#ifdef FEAT_SIGNS
2500 for (lnum = first.lnum; lnum <= last.lnum; lnum++)
2501 {
2502 if (un)
2503 {
2504 /* never used */
2505 }
2506 else
2507 {
2508 if (buf_findsigntype_id(buf->bufp, lnum,
2509 GUARDED) == 0)
2510 {
2511 coloncmd(
Bram Moolenaarec906222009-02-21 21:14:00 +00002512 ":sign place %d line=%ld name=%d buffer=%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 guardId++, lnum, GUARDED,
2514 buf->bufp->b_fnum);
2515 }
2516 }
2517 }
2518#endif
2519 redraw_buf_later(buf->bufp, NOT_VALID);
2520 }
2521 }
2522/* =====================================================================*/
2523 }
2524 else if (streq((char *)cmd, "startAtomic"))
2525 {
2526 inAtomic = 1;
2527/* =====================================================================*/
2528 }
2529 else if (streq((char *)cmd, "endAtomic"))
2530 {
2531 inAtomic = 0;
2532 if (needupdate)
2533 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002534 do_update = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 needupdate = 0;
2536 }
2537/* =====================================================================*/
2538 }
2539 else if (streq((char *)cmd, "save"))
2540 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002541 /*
2542 * NOTE - This command is obsolete wrt NetBeans. Its left in
2543 * only for historical reasons.
2544 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 if (buf == NULL || buf->bufp == NULL)
2546 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002547 nbdebug((" invalid buffer identifier in %s command\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 return FAIL;
2549 }
2550
2551 /* the following is taken from ex_cmds.c (do_wqall function) */
2552 if (bufIsChanged(buf->bufp))
2553 {
2554 /* Only write if the buffer can be written. */
2555 if (p_write
2556 && !buf->bufp->b_p_ro
2557 && buf->bufp->b_ffname != NULL
2558#ifdef FEAT_QUICKFIX
2559 && !bt_dontwrite(buf->bufp)
2560#endif
2561 )
2562 {
2563 buf_write_all(buf->bufp, FALSE);
2564#ifdef FEAT_AUTOCMD
2565 /* an autocommand may have deleted the buffer */
2566 if (!buf_valid(buf->bufp))
2567 buf->bufp = NULL;
2568#endif
2569 }
2570 }
Bram Moolenaarf2330482008-06-24 20:19:36 +00002571 else
2572 {
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002573 nbdebug((" Buffer has no changes!\n"));
Bram Moolenaarf2330482008-06-24 20:19:36 +00002574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575/* =====================================================================*/
2576 }
2577 else if (streq((char *)cmd, "netbeansBuffer"))
2578 {
2579 if (buf == NULL || buf->bufp == NULL)
2580 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002581 nbdebug((" invalid buffer identifier in %s command\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 return FAIL;
2583 }
2584 if (*args == 'T')
2585 {
2586 buf->bufp->b_netbeans_file = TRUE;
2587 buf->bufp->b_was_netbeans_file = TRUE;
2588 }
2589 else
2590 buf->bufp->b_netbeans_file = FALSE;
2591/* =====================================================================*/
2592 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002593 else if (streq((char *)cmd, "specialKeys"))
2594 {
2595 special_keys(args);
2596/* =====================================================================*/
2597 }
2598 else if (streq((char *)cmd, "actionMenuItem"))
2599 {
2600 /* not used yet */
2601/* =====================================================================*/
2602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 else if (streq((char *)cmd, "version"))
2604 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002605 /* not used yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 }
Bram Moolenaarf2330482008-06-24 20:19:36 +00002607 else
2608 {
2609 nbdebug(("Unrecognised command: %s\n", cmd));
2610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 /*
2612 * Unrecognized command is ignored.
2613 */
2614 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002615 if (inAtomic && do_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 {
2617 needupdate = 1;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002618 do_update = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 }
2620
Bram Moolenaar009b2592004-10-24 19:18:58 +00002621 /*
2622 * Is this needed? I moved the netbeans_Xt_connect() later during startup
2623 * and it may no longer be necessary. If its not needed then needupdate
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002624 * and do_update can also be removed.
Bram Moolenaar009b2592004-10-24 19:18:58 +00002625 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002626 if (buf != NULL && buf->initDone && do_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 {
2628 update_screen(NOT_VALID);
2629 setcursor();
Bram Moolenaar96bcc5e2011-04-01 15:33:59 +02002630 cursor_on();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 out_flush();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002632#ifdef FEAT_GUI
Bram Moolenaard54a6882010-08-09 22:49:00 +02002633 if (gui.in_use)
2634 {
2635 gui_update_cursor(TRUE, FALSE);
2636 gui_mch_flush();
2637 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02002638#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 /* Quit a hit-return or more prompt. */
2640 if (State == HITRETURN || State == ASKMORE)
2641 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642#ifdef FEAT_GUI_GTK
Bram Moolenaard54a6882010-08-09 22:49:00 +02002643 if (gui.in_use && gtk_main_level() > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 gtk_main_quit();
2645#endif
2646 }
2647 }
2648
2649 return retval;
2650}
2651
2652
2653/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002654 * If "buf" is not the current buffer try changing to a window that edits this
2655 * buffer. If there is no such window then close the current buffer and set
2656 * the current buffer as "buf".
2657 */
2658 static void
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00002659nb_set_curbuf(buf_T *buf)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002660{
Bram Moolenaar404c9422015-03-14 15:35:52 +01002661 if (curbuf != buf) {
2662 if (buf_jump_open_win(buf) != NULL)
2663 return;
2664# ifdef FEAT_WINDOWS
2665 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf) != NULL)
2666 return;
2667# endif
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002668 set_curbuf(buf, DOBUF_GOTO);
Bram Moolenaar404c9422015-03-14 15:35:52 +01002669 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002670}
2671
2672/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 * Process a vim colon command.
2674 */
2675 static void
2676coloncmd(char *cmd, ...)
2677{
2678 char buf[1024];
2679 va_list ap;
2680
2681 va_start(ap, cmd);
Bram Moolenaar0dc79e82009-06-24 14:50:12 +00002682 vim_vsnprintf(buf, sizeof(buf), cmd, ap, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 va_end(ap);
2684
2685 nbdebug((" COLONCMD %s\n", buf));
2686
2687/* ALT_INPUT_LOCK_ON; */
2688 do_cmdline((char_u *)buf, NULL, NULL, DOCMD_NOWAIT | DOCMD_KEYTYPED);
2689/* ALT_INPUT_LOCK_OFF; */
2690
2691 setcursor(); /* restore the cursor position */
2692 out_flush(); /* make sure output has been written */
2693
Bram Moolenaar67c53842010-05-22 18:28:27 +02002694#ifdef FEAT_GUI
Bram Moolenaard54a6882010-08-09 22:49:00 +02002695 if (gui.in_use)
2696 {
2697 gui_update_cursor(TRUE, FALSE);
2698 gui_mch_flush();
2699 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02002700#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701}
2702
2703
2704/*
Bram Moolenaar009b2592004-10-24 19:18:58 +00002705 * Parse the specialKeys argument and issue the appropriate map commands.
2706 */
2707 static void
2708special_keys(char_u *args)
2709{
2710 char *save_str = nb_unquote(args, NULL);
2711 char *tok = strtok(save_str, " ");
2712 char *sep;
2713 char keybuf[64];
2714 char cmdbuf[256];
2715
2716 while (tok != NULL)
2717 {
2718 int i = 0;
2719
2720 if ((sep = strchr(tok, '-')) != NULL)
2721 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002722 *sep = NUL;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002723 while (*tok)
2724 {
2725 switch (*tok)
2726 {
2727 case 'A':
2728 case 'M':
2729 case 'C':
2730 case 'S':
2731 keybuf[i++] = *tok;
2732 keybuf[i++] = '-';
2733 break;
2734 }
2735 tok++;
2736 }
2737 tok++;
2738 }
2739
2740 strcpy(&keybuf[i], tok);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002741 vim_snprintf(cmdbuf, sizeof(cmdbuf),
2742 "<silent><%s> :nbkey %s<CR>", keybuf, keybuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002743 do_map(0, (char_u *)cmdbuf, NORMAL, FALSE);
2744 tok = strtok(NULL, " ");
2745 }
2746 vim_free(save_str);
2747}
2748
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002749 void
2750ex_nbclose(eap)
2751 exarg_T *eap UNUSED;
2752{
2753 netbeans_close();
2754}
Bram Moolenaar009b2592004-10-24 19:18:58 +00002755
2756 void
2757ex_nbkey(eap)
2758 exarg_T *eap;
2759{
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002760 (void)netbeans_keystring(eap->arg);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002761}
2762
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002763 void
2764ex_nbstart(eap)
2765 exarg_T *eap;
2766{
Bram Moolenaarc2a406b2010-09-30 21:03:26 +02002767#ifdef FEAT_GUI
2768# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar7ad7d012010-11-16 15:49:02 +01002769 && !defined(FEAT_GUI_W32)
Bram Moolenaarc2a406b2010-09-30 21:03:26 +02002770 if (gui.in_use)
2771 {
Bram Moolenaar7ad7d012010-11-16 15:49:02 +01002772 EMSG(_("E838: netbeans is not supported with this GUI"));
2773 return;
Bram Moolenaarc2a406b2010-09-30 21:03:26 +02002774 }
2775# endif
2776#endif
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002777 netbeans_open((char *)eap->arg, FALSE);
2778}
Bram Moolenaar009b2592004-10-24 19:18:58 +00002779
2780/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 * Initialize highlights and signs for use by netbeans (mostly obsolete)
2782 */
2783 static void
2784nb_init_graphics(void)
2785{
2786 static int did_init = FALSE;
2787
2788 if (!did_init)
2789 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002790 coloncmd(":highlight NBGuarded guibg=Cyan guifg=Black"
2791 " ctermbg=LightCyan ctermfg=Black");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 coloncmd(":sign define %d linehl=NBGuarded", GUARDED);
2793
2794 did_init = TRUE;
2795 }
2796}
2797
2798/*
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002799 * Convert key to netbeans name. This uses the global "mod_mask".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 */
2801 static void
2802netbeans_keyname(int key, char *buf)
2803{
2804 char *name = 0;
2805 char namebuf[2];
2806 int ctrl = 0;
2807 int shift = 0;
2808 int alt = 0;
2809
2810 if (mod_mask & MOD_MASK_CTRL)
2811 ctrl = 1;
2812 if (mod_mask & MOD_MASK_SHIFT)
2813 shift = 1;
2814 if (mod_mask & MOD_MASK_ALT)
2815 alt = 1;
2816
2817
2818 switch (key)
2819 {
2820 case K_F1: name = "F1"; break;
2821 case K_S_F1: name = "F1"; shift = 1; break;
2822 case K_F2: name = "F2"; break;
2823 case K_S_F2: name = "F2"; shift = 1; break;
2824 case K_F3: name = "F3"; break;
2825 case K_S_F3: name = "F3"; shift = 1; break;
2826 case K_F4: name = "F4"; break;
2827 case K_S_F4: name = "F4"; shift = 1; break;
2828 case K_F5: name = "F5"; break;
2829 case K_S_F5: name = "F5"; shift = 1; break;
2830 case K_F6: name = "F6"; break;
2831 case K_S_F6: name = "F6"; shift = 1; break;
2832 case K_F7: name = "F7"; break;
2833 case K_S_F7: name = "F7"; shift = 1; break;
2834 case K_F8: name = "F8"; break;
2835 case K_S_F8: name = "F8"; shift = 1; break;
2836 case K_F9: name = "F9"; break;
2837 case K_S_F9: name = "F9"; shift = 1; break;
2838 case K_F10: name = "F10"; break;
2839 case K_S_F10: name = "F10"; shift = 1; break;
2840 case K_F11: name = "F11"; break;
2841 case K_S_F11: name = "F11"; shift = 1; break;
2842 case K_F12: name = "F12"; break;
2843 case K_S_F12: name = "F12"; shift = 1; break;
2844 default:
2845 if (key >= ' ' && key <= '~')
2846 {
2847 /* Allow ASCII characters. */
2848 name = namebuf;
2849 namebuf[0] = key;
2850 namebuf[1] = NUL;
2851 }
2852 else
2853 name = "X";
2854 break;
2855 }
2856
2857 buf[0] = '\0';
2858 if (ctrl)
2859 strcat(buf, "C");
2860 if (shift)
2861 strcat(buf, "S");
2862 if (alt)
2863 strcat(buf, "M"); /* META */
2864 if (ctrl || shift || alt)
2865 strcat(buf, "-");
2866 strcat(buf, name);
2867}
2868
Bram Moolenaar67c53842010-05-22 18:28:27 +02002869#if defined(FEAT_BEVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870/*
2871 * Function to be called for balloon evaluation. Grabs the text under the
2872 * cursor and sends it to the debugger for evaluation. The debugger should
2873 * respond with a showBalloon command when there is a useful result.
2874 */
Bram Moolenaard62bec82005-03-07 22:56:57 +00002875 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876netbeans_beval_cb(
2877 BalloonEval *beval,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002878 int state UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879{
Bram Moolenaard62bec82005-03-07 22:56:57 +00002880 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 char_u *text;
Bram Moolenaard62bec82005-03-07 22:56:57 +00002882 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 int col;
Bram Moolenaard9462e32011-04-11 21:35:11 +02002884 char *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 char_u *p;
2886
2887 /* Don't do anything when 'ballooneval' is off, messages scrolled the
2888 * windows up or we have no connection. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002889 if (!p_beval || msg_scrolled > 0 || !NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 return;
2891
Bram Moolenaard62bec82005-03-07 22:56:57 +00002892 if (get_beval_info(beval, TRUE, &wp, &lnum, &text, &col) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 {
2894 /* Send debugger request. Only when the text is of reasonable
2895 * length. */
2896 if (text != NULL && text[0] != NUL && STRLEN(text) < MAXPATHL)
2897 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02002898 buf = (char *)alloc(MAXPATHL * 2 + 25);
2899 if (buf != NULL)
Bram Moolenaar009b2592004-10-24 19:18:58 +00002900 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02002901 p = nb_quote(text);
2902 if (p != NULL)
2903 {
2904 vim_snprintf(buf, MAXPATHL * 2 + 25,
2905 "0:balloonText=%d \"%s\"\n", r_cmdno, p);
2906 vim_free(p);
2907 }
2908 nbdebug(("EVT: %s", buf));
2909 nb_send(buf, "netbeans_beval_cb");
2910 vim_free(buf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 }
2913 vim_free(text);
2914 }
2915}
2916#endif
2917
2918/*
Bram Moolenaare0874f82016-01-24 20:36:41 +01002919 * Return TRUE when the netbeans connection is active.
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002920 */
2921 int
2922netbeans_active(void)
2923{
2924 return NETBEANS_OPEN;
2925}
2926
Bram Moolenaar67c53842010-05-22 18:28:27 +02002927#if defined(FEAT_GUI) || defined(PROTO)
2928/*
2929 * Register our file descriptor with the gui event handling system.
2930 */
2931 void
2932netbeans_gui_register(void)
2933{
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002934 if (!NB_HAS_GUI || !NETBEANS_OPEN)
Bram Moolenaar67c53842010-05-22 18:28:27 +02002935 return;
2936
Bram Moolenaar173c9852010-09-29 17:27:01 +02002937# ifdef FEAT_GUI_X11
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002938 /* tell notifier we are interested in being called
2939 * when there is input on the editor connection socket
2940 */
2941 if (inputHandler == (XtInputId)NULL)
2942 inputHandler = XtAppAddInput((XtAppContext)app_context, nbsock,
2943 (XtPointer)(XtInputReadMask + XtInputExceptMask),
2944 messageFromNetbeans, NULL);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002945# else
2946# ifdef FEAT_GUI_GTK
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002947 /*
2948 * Tell gdk we are interested in being called when there
2949 * is input on the editor connection socket
2950 */
2951 if (inputHandler == 0)
2952 inputHandler = gdk_input_add((gint)nbsock, (GdkInputCondition)
2953 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
2954 messageFromNetbeans, NULL);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002955# else
2956# ifdef FEAT_GUI_W32
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002957 /*
2958 * Tell Windows we are interested in receiving message when there
2959 * is input on the editor connection socket
2960 */
2961 if (inputHandler == -1)
2962 inputHandler = WSAAsyncSelect(nbsock, s_hwnd, WM_NETBEANS, FD_READ);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002963# endif
2964# endif
2965# endif
Bram Moolenaar67c53842010-05-22 18:28:27 +02002966
2967# ifdef FEAT_BEVAL
2968 bevalServers |= BEVAL_NETBEANS;
2969# endif
2970}
2971#endif
2972
2973/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 * Tell netbeans that the window was opened, ready for commands.
2975 */
2976 void
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02002977netbeans_open(char *params, int doabort)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978{
2979 char *cmd = "0:startupDone=0\n";
2980
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002981 if (NETBEANS_OPEN)
2982 {
2983 EMSG(_("E511: netbeans already connected"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02002987 if (netbeans_connect(params, doabort) != OK)
Bram Moolenaar67c53842010-05-22 18:28:27 +02002988 return;
2989#ifdef FEAT_GUI
2990 netbeans_gui_register();
Bram Moolenaard62bec82005-03-07 22:56:57 +00002991#endif
2992
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 nbdebug(("EVT: %s", cmd));
2994 nb_send(cmd, "netbeans_startup_done");
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002995
2996 /* update the screen after having added the gutter */
2997 changed_window_setting();
2998 update_screen(CLEAR);
2999 setcursor();
Bram Moolenaar96bcc5e2011-04-01 15:33:59 +02003000 cursor_on();
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003001 out_flush();
3002#ifdef FEAT_GUI
Bram Moolenaard54a6882010-08-09 22:49:00 +02003003 if (gui.in_use)
3004 {
3005 gui_update_cursor(TRUE, FALSE);
3006 gui_mch_flush();
3007 }
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003008#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009}
3010
Bram Moolenaar009b2592004-10-24 19:18:58 +00003011/*
3012 * Tell netbeans that we're exiting. This should be called right
3013 * before calling exit.
3014 */
3015 void
3016netbeans_send_disconnect()
3017{
3018 char buf[128];
3019
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003020 if (NETBEANS_OPEN)
Bram Moolenaar009b2592004-10-24 19:18:58 +00003021 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00003022 sprintf(buf, "0:disconnect=%d\n", r_cmdno);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003023 nbdebug(("EVT: %s", buf));
3024 nb_send(buf, "netbeans_disconnect");
3025 }
3026}
3027
Bram Moolenaar173c9852010-09-29 17:27:01 +02003028#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_W32) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029/*
3030 * Tell netbeans that the window was moved or resized.
3031 */
3032 void
3033netbeans_frame_moved(int new_x, int new_y)
3034{
3035 char buf[128];
3036
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003037 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 return;
3039
3040 sprintf(buf, "0:geometry=%d %d %d %d %d\n",
Bram Moolenaar89d40322006-08-29 15:30:07 +00003041 r_cmdno, (int)Columns, (int)Rows, new_x, new_y);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003042 /*nbdebug(("EVT: %s", buf)); happens too many times during a move */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 nb_send(buf, "netbeans_frame_moved");
3044}
3045#endif
3046
3047/*
Bram Moolenaar009b2592004-10-24 19:18:58 +00003048 * Tell netbeans the user opened or activated a file.
3049 */
3050 void
3051netbeans_file_activated(buf_T *bufp)
3052{
3053 int bufno = nb_getbufno(bufp);
3054 nbbuf_T *bp = nb_get_buf(bufno);
3055 char buffer[2*MAXPATHL];
3056 char_u *q;
3057
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003058 if (!NETBEANS_OPEN || !bufp->b_netbeans_file || dosetvisible)
Bram Moolenaar009b2592004-10-24 19:18:58 +00003059 return;
3060
3061 q = nb_quote(bufp->b_ffname);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003062 if (q == NULL || bp == NULL)
Bram Moolenaar009b2592004-10-24 19:18:58 +00003063 return;
3064
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003065 vim_snprintf(buffer, sizeof(buffer), "%d:fileOpened=%d \"%s\" %s %s\n",
Bram Moolenaar009b2592004-10-24 19:18:58 +00003066 bufno,
3067 bufno,
3068 (char *)q,
3069 "T", /* open in NetBeans */
3070 "F"); /* modified */
3071
3072 vim_free(q);
3073 nbdebug(("EVT: %s", buffer));
3074
3075 nb_send(buffer, "netbeans_file_opened");
3076}
3077
3078/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 * Tell netbeans the user opened a file.
3080 */
3081 void
Bram Moolenaar009b2592004-10-24 19:18:58 +00003082netbeans_file_opened(buf_T *bufp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083{
Bram Moolenaar009b2592004-10-24 19:18:58 +00003084 int bufno = nb_getbufno(bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 char buffer[2*MAXPATHL];
3086 char_u *q;
Bram Moolenaar009b2592004-10-24 19:18:58 +00003087 nbbuf_T *bp = nb_get_buf(nb_getbufno(bufp));
3088 int bnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003090 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 return;
3092
Bram Moolenaar009b2592004-10-24 19:18:58 +00003093 q = nb_quote(bufp->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 if (q == NULL)
3095 return;
Bram Moolenaar009b2592004-10-24 19:18:58 +00003096 if (bp != NULL)
3097 bnum = bufno;
3098 else
3099 bnum = 0;
3100
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003101 vim_snprintf(buffer, sizeof(buffer), "%d:fileOpened=%d \"%s\" %s %s\n",
Bram Moolenaar009b2592004-10-24 19:18:58 +00003102 bnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 0,
3104 (char *)q,
3105 "T", /* open in NetBeans */
3106 "F"); /* modified */
3107
3108 vim_free(q);
3109 nbdebug(("EVT: %s", buffer));
3110
3111 nb_send(buffer, "netbeans_file_opened");
Bram Moolenaar009b2592004-10-24 19:18:58 +00003112 if (p_acd && vim_chdirfile(bufp->b_ffname) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 shorten_fnames(TRUE);
3114}
3115
3116/*
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00003117 * Tell netbeans that a file was deleted or wiped out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 */
3119 void
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00003120netbeans_file_killed(buf_T *bufp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121{
3122 int bufno = nb_getbufno(bufp);
3123 nbbuf_T *nbbuf = nb_get_buf(bufno);
3124 char buffer[2*MAXPATHL];
3125
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003126 if (!NETBEANS_OPEN || bufno == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 return;
3128
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00003129 nbdebug(("netbeans_file_killed:\n"));
3130 nbdebug((" Killing bufno: %d", bufno));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131
Bram Moolenaar89d40322006-08-29 15:30:07 +00003132 sprintf(buffer, "%d:killed=%d\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133
3134 nbdebug(("EVT: %s", buffer));
3135
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00003136 nb_send(buffer, "netbeans_file_killed");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137
3138 if (nbbuf != NULL)
3139 nbbuf->bufp = NULL;
3140}
3141
3142/*
3143 * Get a pointer to the Netbeans buffer for Vim buffer "bufp".
3144 * Return NULL if there is no such buffer or changes are not to be reported.
3145 * Otherwise store the buffer number in "*bufnop".
3146 */
3147 static nbbuf_T *
3148nb_bufp2nbbuf_fire(buf_T *bufp, int *bufnop)
3149{
3150 int bufno;
3151 nbbuf_T *nbbuf;
3152
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003153 if (!NETBEANS_OPEN || !netbeansFireChanges)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 return NULL; /* changes are not reported at all */
3155
3156 bufno = nb_getbufno(bufp);
3157 if (bufno <= 0)
3158 return NULL; /* file is not known to NetBeans */
3159
3160 nbbuf = nb_get_buf(bufno);
3161 if (nbbuf != NULL && !nbbuf->fireChanges)
3162 return NULL; /* changes in this buffer are not reported */
3163
3164 *bufnop = bufno;
3165 return nbbuf;
3166}
3167
3168/*
3169 * Tell netbeans the user inserted some text.
3170 */
3171 void
3172netbeans_inserted(
3173 buf_T *bufp,
3174 linenr_T linenr,
3175 colnr_T col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 char_u *txt,
3177 int newlen)
3178{
3179 char_u *buf;
3180 int bufno;
3181 nbbuf_T *nbbuf;
3182 pos_T pos;
3183 long off;
3184 char_u *p;
3185 char_u *newtxt;
3186
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003187 if (!NETBEANS_OPEN)
3188 return;
3189
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3191 if (nbbuf == NULL)
3192 return;
3193
Bram Moolenaar009b2592004-10-24 19:18:58 +00003194 /* Don't mark as modified for initial read */
3195 if (nbbuf->insertDone)
3196 nbbuf->modified = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197
3198 pos.lnum = linenr;
3199 pos.col = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 off = pos2off(bufp, &pos);
3201
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 /* send the "insert" EVT */
3203 newtxt = alloc(newlen + 1);
Bram Moolenaarb6356332005-07-18 21:40:44 +00003204 vim_strncpy(newtxt, txt, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 p = nb_quote(newtxt);
3206 if (p != NULL)
3207 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00003208 buf = alloc(128 + 2*newlen);
Bram Moolenaar89d40322006-08-29 15:30:07 +00003209 sprintf((char *)buf, "%d:insert=%d %ld \"%s\"\n",
3210 bufno, r_cmdno, off, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 nbdebug(("EVT: %s", buf));
3212 nb_send((char *)buf, "netbeans_inserted");
Bram Moolenaar009b2592004-10-24 19:18:58 +00003213 vim_free(p);
3214 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 vim_free(newtxt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217}
3218
3219/*
3220 * Tell netbeans some bytes have been removed.
3221 */
3222 void
3223netbeans_removed(
3224 buf_T *bufp,
3225 linenr_T linenr,
3226 colnr_T col,
3227 long len)
3228{
3229 char_u buf[128];
3230 int bufno;
3231 nbbuf_T *nbbuf;
3232 pos_T pos;
3233 long off;
3234
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003235 if (!NETBEANS_OPEN)
3236 return;
3237
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3239 if (nbbuf == NULL)
3240 return;
3241
3242 if (len < 0)
3243 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00003244 nbdebug(("Negative len %ld in netbeans_removed()!\n", len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 return;
3246 }
3247
3248 nbbuf->modified = 1;
3249
3250 pos.lnum = linenr;
3251 pos.col = col;
3252
3253 off = pos2off(bufp, &pos);
3254
Bram Moolenaar89d40322006-08-29 15:30:07 +00003255 sprintf((char *)buf, "%d:remove=%d %ld %ld\n", bufno, r_cmdno, off, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 nbdebug(("EVT: %s", buf));
3257 nb_send((char *)buf, "netbeans_removed");
3258}
3259
3260/*
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003261 * Send netbeans an unmodified command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003264netbeans_unmodified(buf_T *bufp UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265{
Bram Moolenaar09092152010-08-08 16:38:42 +02003266 /* This is a no-op, because NetBeans considers a buffer modified
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 * even when all changes have been undone. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268}
3269
3270/*
3271 * Send a button release event back to netbeans. Its up to netbeans
3272 * to decide what to do (if anything) with this event.
3273 */
3274 void
3275netbeans_button_release(int button)
3276{
3277 char buf[128];
3278 int bufno;
3279
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003280 if (!NETBEANS_OPEN)
3281 return;
3282
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 bufno = nb_getbufno(curbuf);
3284
3285 if (bufno >= 0 && curwin != NULL && curwin->w_buffer == curbuf)
3286 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003287 int col = mouse_col - W_WINCOL(curwin)
Bram Moolenaar67c53842010-05-22 18:28:27 +02003288 - ((curwin->w_p_nu || curwin->w_p_rnu) ? 9 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 long off = pos2off(curbuf, &curwin->w_cursor);
3290
3291 /* sync the cursor position */
Bram Moolenaar89d40322006-08-29 15:30:07 +00003292 sprintf(buf, "%d:newDotAndMark=%d %ld %ld\n", bufno, r_cmdno, off, off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 nbdebug(("EVT: %s", buf));
3294 nb_send(buf, "netbeans_button_release[newDotAndMark]");
3295
Bram Moolenaar89d40322006-08-29 15:30:07 +00003296 sprintf(buf, "%d:buttonRelease=%d %d %ld %d\n", bufno, r_cmdno,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 button, (long)curwin->w_cursor.lnum, col);
3298 nbdebug(("EVT: %s", buf));
3299 nb_send(buf, "netbeans_button_release");
3300 }
3301}
3302
3303
3304/*
Bram Moolenaar143c38c2007-05-10 16:41:10 +00003305 * Send a keypress event back to netbeans. This usually simulates some
Bram Moolenaar009b2592004-10-24 19:18:58 +00003306 * kind of function key press. This function operates on a key code.
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003307 * Return TRUE when the key was sent, FALSE when the command has been
3308 * postponed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 */
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003310 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311netbeans_keycommand(int key)
3312{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 char keyName[60];
Bram Moolenaar009b2592004-10-24 19:18:58 +00003314
3315 netbeans_keyname(key, keyName);
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003316 return netbeans_keystring((char_u *)keyName);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003317}
3318
3319
3320/*
Bram Moolenaar143c38c2007-05-10 16:41:10 +00003321 * Send a keypress event back to netbeans. This usually simulates some
Bram Moolenaar009b2592004-10-24 19:18:58 +00003322 * kind of function key press. This function operates on a key string.
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003323 * Return TRUE when the key was sent, FALSE when the command has been
3324 * postponed.
Bram Moolenaar009b2592004-10-24 19:18:58 +00003325 */
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003326 static int
3327netbeans_keystring(char_u *keyName)
Bram Moolenaar009b2592004-10-24 19:18:58 +00003328{
3329 char buf[2*MAXPATHL];
3330 int bufno = nb_getbufno(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 long off;
3332 char_u *q;
3333
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003334 if (!NETBEANS_OPEN)
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003335 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 if (bufno == -1)
3338 {
3339 nbdebug(("got keycommand for non-NetBeans buffer, opening...\n"));
3340 q = curbuf->b_ffname == NULL ? (char_u *)""
3341 : nb_quote(curbuf->b_ffname);
3342 if (q == NULL)
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003343 return TRUE;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003344 vim_snprintf(buf, sizeof(buf), "0:fileOpened=%d \"%s\" %s %s\n", 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 q,
3346 "T", /* open in NetBeans */
3347 "F"); /* modified */
3348 if (curbuf->b_ffname != NULL)
3349 vim_free(q);
3350 nbdebug(("EVT: %s", buf));
3351 nb_send(buf, "netbeans_keycommand");
3352
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003353 postpone_keycommand(keyName);
3354 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 }
3356
3357 /* sync the cursor position */
3358 off = pos2off(curbuf, &curwin->w_cursor);
Bram Moolenaar89d40322006-08-29 15:30:07 +00003359 sprintf(buf, "%d:newDotAndMark=%d %ld %ld\n", bufno, r_cmdno, off, off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 nbdebug(("EVT: %s", buf));
3361 nb_send(buf, "netbeans_keycommand");
3362
3363 /* To work on Win32 you must apply patch to ExtEditor module
3364 * from ExtEdCaret.java.diff - make EVT_newDotAndMark handler
3365 * more synchronous
3366 */
3367
3368 /* now send keyCommand event */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003369 vim_snprintf(buf, sizeof(buf), "%d:keyCommand=%d \"%s\"\n",
Bram Moolenaar89d40322006-08-29 15:30:07 +00003370 bufno, r_cmdno, keyName);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 nbdebug(("EVT: %s", buf));
3372 nb_send(buf, "netbeans_keycommand");
3373
3374 /* New: do both at once and include the lnum/col. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003375 vim_snprintf(buf, sizeof(buf), "%d:keyAtPos=%d \"%s\" %ld %ld/%ld\n",
Bram Moolenaar89d40322006-08-29 15:30:07 +00003376 bufno, r_cmdno, keyName,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 off, (long)curwin->w_cursor.lnum, (long)curwin->w_cursor.col);
3378 nbdebug(("EVT: %s", buf));
3379 nb_send(buf, "netbeans_keycommand");
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003380 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381}
3382
3383
3384/*
3385 * Send a save event to netbeans.
3386 */
3387 void
3388netbeans_save_buffer(buf_T *bufp)
3389{
3390 char_u buf[64];
3391 int bufno;
3392 nbbuf_T *nbbuf;
3393
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003394 if (!NETBEANS_OPEN)
3395 return;
3396
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3398 if (nbbuf == NULL)
3399 return;
3400
3401 nbbuf->modified = 0;
3402
Bram Moolenaar89d40322006-08-29 15:30:07 +00003403 sprintf((char *)buf, "%d:save=%d\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 nbdebug(("EVT: %s", buf));
3405 nb_send((char *)buf, "netbeans_save_buffer");
3406}
3407
3408
3409/*
3410 * Send remove command to netbeans (this command has been turned off).
3411 */
3412 void
3413netbeans_deleted_all_lines(buf_T *bufp)
3414{
3415 char_u buf[64];
3416 int bufno;
3417 nbbuf_T *nbbuf;
3418
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003419 if (!NETBEANS_OPEN)
3420 return;
3421
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3423 if (nbbuf == NULL)
3424 return;
3425
Bram Moolenaar009b2592004-10-24 19:18:58 +00003426 /* Don't mark as modified for initial read */
3427 if (nbbuf->insertDone)
3428 nbbuf->modified = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429
Bram Moolenaar89d40322006-08-29 15:30:07 +00003430 sprintf((char *)buf, "%d:remove=%d 0 -1\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 nbdebug(("EVT(suppressed): %s", buf));
3432/* nb_send(buf, "netbeans_deleted_all_lines"); */
3433}
3434
3435
3436/*
3437 * See if the lines are guarded. The top and bot parameters are from
3438 * u_savecommon(), these are the line above the change and the line below the
3439 * change.
3440 */
3441 int
3442netbeans_is_guarded(linenr_T top, linenr_T bot)
3443{
3444 signlist_T *p;
3445 int lnum;
3446
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003447 if (!NETBEANS_OPEN)
3448 return FALSE;
3449
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 for (p = curbuf->b_signlist; p != NULL; p = p->next)
3451 if (p->id >= GUARDEDOFFSET)
3452 for (lnum = top + 1; lnum < bot; lnum++)
3453 if (lnum == p->lnum)
3454 return TRUE;
3455
3456 return FALSE;
3457}
3458
Bram Moolenaar173c9852010-09-29 17:27:01 +02003459#if defined(FEAT_GUI_X11) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460/*
3461 * We have multiple signs to draw at the same location. Draw the
3462 * multi-sign indicator instead. This is the Motif version.
3463 */
3464 void
3465netbeans_draw_multisign_indicator(int row)
3466{
3467 int i;
3468 int y;
3469 int x;
3470
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003471 if (!NETBEANS_OPEN)
3472 return;
3473
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 x = 0;
3475 y = row * gui.char_height + 2;
3476
3477 for (i = 0; i < gui.char_height - 3; i++)
3478 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y++);
3479
3480 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+0, y);
3481 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3482 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+4, y++);
3483 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+1, y);
3484 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3485 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+3, y++);
3486 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3487}
Bram Moolenaar173c9852010-09-29 17:27:01 +02003488#endif /* FEAT_GUI_X11 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489
Bram Moolenaar64404472010-06-26 06:24:45 +02003490#if defined(FEAT_GUI_GTK) && !defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491/*
3492 * We have multiple signs to draw at the same location. Draw the
3493 * multi-sign indicator instead. This is the GTK/Gnome version.
3494 */
3495 void
3496netbeans_draw_multisign_indicator(int row)
3497{
3498 int i;
3499 int y;
3500 int x;
3501 GdkDrawable *drawable = gui.drawarea->window;
3502
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003503 if (!NETBEANS_OPEN)
3504 return;
3505
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 x = 0;
3507 y = row * gui.char_height + 2;
3508
3509 for (i = 0; i < gui.char_height - 3; i++)
3510 gdk_draw_point(drawable, gui.text_gc, x+2, y++);
3511
3512 gdk_draw_point(drawable, gui.text_gc, x+0, y);
3513 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3514 gdk_draw_point(drawable, gui.text_gc, x+4, y++);
3515 gdk_draw_point(drawable, gui.text_gc, x+1, y);
3516 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3517 gdk_draw_point(drawable, gui.text_gc, x+3, y++);
3518 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3519}
3520#endif /* FEAT_GUI_GTK */
3521
3522/*
3523 * If the mouse is clicked in the gutter of a line with multiple
3524 * annotations, cycle through the set of signs.
3525 */
3526 void
3527netbeans_gutter_click(linenr_T lnum)
3528{
3529 signlist_T *p;
3530
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003531 if (!NETBEANS_OPEN)
3532 return;
3533
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 for (p = curbuf->b_signlist; p != NULL; p = p->next)
3535 {
3536 if (p->lnum == lnum && p->next && p->next->lnum == lnum)
3537 {
3538 signlist_T *tail;
3539
3540 /* remove "p" from list, reinsert it at the tail of the sublist */
3541 if (p->prev)
3542 p->prev->next = p->next;
3543 else
3544 curbuf->b_signlist = p->next;
3545 p->next->prev = p->prev;
3546 /* now find end of sublist and insert p */
3547 for (tail = p->next;
3548 tail->next && tail->next->lnum == lnum
3549 && tail->next->id < GUARDEDOFFSET;
3550 tail = tail->next)
3551 ;
3552 /* tail now points to last entry with same lnum (except
3553 * that "guarded" annotations are always last) */
3554 p->next = tail->next;
3555 if (tail->next)
3556 tail->next->prev = p;
3557 p->prev = tail;
3558 tail->next = p;
3559 update_debug_sign(curbuf, lnum);
3560 break;
3561 }
3562 }
3563}
3564
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565/*
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003566 * Add a sign of the requested type at the requested location.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 *
3568 * Reverse engineering:
3569 * Apparently an annotation is defined the first time it is used in a buffer.
3570 * When the same annotation is used in two buffers, the second time we do not
3571 * need to define a new sign name but reuse the existing one. But since the
3572 * ID number used in the second buffer starts counting at one again, a mapping
3573 * is made from the ID specifically for the buffer to the global sign name
3574 * (which is a number).
3575 *
3576 * globalsignmap[] stores the signs that have been defined globally.
3577 * buf->signmapused[] maps buffer-local annotation IDs to an index in
3578 * globalsignmap[].
3579 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 static void
3581addsigntype(
3582 nbbuf_T *buf,
3583 int typeNum,
3584 char_u *typeName,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003585 char_u *tooltip UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 char_u *glyphFile,
Bram Moolenaar67c53842010-05-22 18:28:27 +02003587 char_u *fg,
3588 char_u *bg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 int i, j;
Bram Moolenaar67c53842010-05-22 18:28:27 +02003591 int use_fg = (*fg && STRCMP(fg, "none") != 0);
3592 int use_bg = (*bg && STRCMP(bg, "none") != 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593
3594 for (i = 0; i < globalsignmapused; i++)
3595 if (STRCMP(typeName, globalsignmap[i]) == 0)
3596 break;
3597
3598 if (i == globalsignmapused) /* not found; add it to global map */
3599 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003600 nbdebug(("DEFINEANNOTYPE(%d,%s,%s,%s,%s,%s)\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 typeNum, typeName, tooltip, glyphFile, fg, bg));
3602 if (use_fg || use_bg)
3603 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003604 char fgbuf[2 * (8 + MAX_COLOR_LENGTH) + 1];
3605 char bgbuf[2 * (8 + MAX_COLOR_LENGTH) + 1];
3606 char *ptr;
3607 int value;
3608
3609 value = strtol((char *)fg, &ptr, 10);
3610 if (ptr != (char *)fg)
3611 sprintf(fgbuf, "guifg=#%06x", value & 0xFFFFFF);
3612 else
3613 sprintf(fgbuf, "guifg=%s ctermfg=%s", fg, fg);
3614
3615 value = strtol((char *)bg, &ptr, 10);
3616 if (ptr != (char *)bg)
3617 sprintf(bgbuf, "guibg=#%06x", value & 0xFFFFFF);
3618 else
3619 sprintf(bgbuf, "guibg=%s ctermbg=%s", bg, bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620
3621 coloncmd(":highlight NB_%s %s %s", typeName, (use_fg) ? fgbuf : "",
3622 (use_bg) ? bgbuf : "");
3623 if (*glyphFile == NUL)
3624 /* no glyph, line highlighting only */
3625 coloncmd(":sign define %d linehl=NB_%s", i + 1, typeName);
3626 else if (vim_strsize(glyphFile) <= 2)
3627 /* one- or two-character glyph name, use as text glyph with
3628 * texthl */
3629 coloncmd(":sign define %d text=%s texthl=NB_%s", i + 1,
3630 glyphFile, typeName);
3631 else
3632 /* glyph, line highlighting */
3633 coloncmd(":sign define %d icon=%s linehl=NB_%s", i + 1,
3634 glyphFile, typeName);
3635 }
3636 else
3637 /* glyph, no line highlighting */
3638 coloncmd(":sign define %d icon=%s", i + 1, glyphFile);
3639
3640 if (STRCMP(typeName,"CurrentPC") == 0)
3641 curPCtype = typeNum;
3642
3643 if (globalsignmapused == globalsignmaplen)
3644 {
3645 if (globalsignmaplen == 0) /* first allocation */
3646 {
3647 globalsignmaplen = 20;
3648 globalsignmap = (char **)alloc_clear(globalsignmaplen*sizeof(char *));
3649 }
3650 else /* grow it */
3651 {
3652 int incr;
3653 int oldlen = globalsignmaplen;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003654 char **t_globalsignmap = globalsignmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655
3656 globalsignmaplen *= 2;
3657 incr = globalsignmaplen - oldlen;
3658 globalsignmap = (char **)vim_realloc(globalsignmap,
3659 globalsignmaplen * sizeof(char *));
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003660 if (globalsignmap == NULL)
3661 {
3662 vim_free(t_globalsignmap);
3663 globalsignmaplen = 0;
3664 return;
3665 }
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003666 vim_memset(globalsignmap + oldlen, 0, incr * sizeof(char *));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 }
3668 }
3669
3670 globalsignmap[i] = (char *)typeName;
3671 globalsignmapused = i + 1;
3672 }
3673
3674 /* check local map; should *not* be found! */
3675 for (j = 0; j < buf->signmapused; j++)
3676 if (buf->signmap[j] == i + 1)
3677 return;
3678
3679 /* add to local map */
3680 if (buf->signmapused == buf->signmaplen)
3681 {
3682 if (buf->signmaplen == 0) /* first allocation */
3683 {
3684 buf->signmaplen = 5;
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003685 buf->signmap = (int *)alloc_clear(buf->signmaplen * sizeof(int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 }
3687 else /* grow it */
3688 {
3689 int incr;
3690 int oldlen = buf->signmaplen;
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003691 int *t_signmap = buf->signmap;
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003692
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 buf->signmaplen *= 2;
3694 incr = buf->signmaplen - oldlen;
3695 buf->signmap = (int *)vim_realloc(buf->signmap,
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003696 buf->signmaplen * sizeof(int));
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003697 if (buf->signmap == NULL)
3698 {
3699 vim_free(t_signmap);
3700 buf->signmaplen = 0;
3701 return;
3702 }
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003703 vim_memset(buf->signmap + oldlen, 0, incr * sizeof(int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 }
3705 }
3706
3707 buf->signmap[buf->signmapused++] = i + 1;
3708
3709}
3710
3711
3712/*
3713 * See if we have the requested sign type in the buffer.
3714 */
3715 static int
3716mapsigntype(nbbuf_T *buf, int localsigntype)
3717{
3718 if (--localsigntype >= 0 && localsigntype < buf->signmapused)
3719 return buf->signmap[localsigntype];
3720
3721 return 0;
3722}
3723
3724
3725/*
3726 * Compute length of buffer, don't print anything.
3727 */
3728 static long
3729get_buf_size(buf_T *bufp)
3730{
3731 linenr_T lnum;
3732 long char_count = 0;
3733 int eol_size;
3734 long last_check = 100000L;
3735
3736 if (bufp->b_ml.ml_flags & ML_EMPTY)
3737 return 0;
3738 else
3739 {
3740 if (get_fileformat(bufp) == EOL_DOS)
3741 eol_size = 2;
3742 else
3743 eol_size = 1;
3744 for (lnum = 1; lnum <= bufp->b_ml.ml_line_count; ++lnum)
3745 {
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00003746 char_count += (long)STRLEN(ml_get_buf(bufp, lnum, FALSE))
3747 + eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 /* Check for a CTRL-C every 100000 characters */
3749 if (char_count > last_check)
3750 {
3751 ui_breakcheck();
3752 if (got_int)
3753 return char_count;
3754 last_check = char_count + 100000L;
3755 }
3756 }
3757 /* Correction for when last line doesn't have an EOL. */
Bram Moolenaar34d72d42015-07-17 14:18:08 +02003758 if (!bufp->b_p_eol && (bufp->b_p_bin || !bufp->b_p_fixeol))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 char_count -= eol_size;
3760 }
3761
3762 return char_count;
3763}
3764
3765/*
3766 * Convert character offset to lnum,col
3767 */
3768 static pos_T *
3769off2pos(buf_T *buf, long offset)
3770{
3771 linenr_T lnum;
3772 static pos_T pos;
3773
3774 pos.lnum = 0;
3775 pos.col = 0;
3776#ifdef FEAT_VIRTUALEDIT
3777 pos.coladd = 0;
3778#endif
3779
3780 if (!(buf->b_ml.ml_flags & ML_EMPTY))
3781 {
3782 if ((lnum = ml_find_line_or_offset(buf, (linenr_T)0, &offset)) < 0)
3783 return NULL;
3784 pos.lnum = lnum;
3785 pos.col = offset;
3786 }
3787
3788 return &pos;
3789}
3790
3791/*
3792 * Convert an argument in the form "1234" to an offset and compute the
3793 * lnum/col from it. Convert an argument in the form "123/12" directly to a
3794 * lnum/col.
3795 * "argp" is advanced to after the argument.
3796 * Return a pointer to the position, NULL if something is wrong.
3797 */
3798 static pos_T *
3799get_off_or_lnum(buf_T *buf, char_u **argp)
3800{
3801 static pos_T mypos;
3802 long off;
3803
3804 off = strtol((char *)*argp, (char **)argp, 10);
3805 if (**argp == '/')
3806 {
3807 mypos.lnum = (linenr_T)off;
3808 ++*argp;
3809 mypos.col = strtol((char *)*argp, (char **)argp, 10);
3810#ifdef FEAT_VIRTUALEDIT
3811 mypos.coladd = 0;
3812#endif
3813 return &mypos;
3814 }
3815 return off2pos(buf, off);
3816}
3817
3818
3819/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003820 * Convert (lnum,col) to byte offset in the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 */
3822 static long
3823pos2off(buf_T *buf, pos_T *pos)
3824{
3825 long offset = 0;
3826
3827 if (!(buf->b_ml.ml_flags & ML_EMPTY))
3828 {
3829 if ((offset = ml_find_line_or_offset(buf, pos->lnum, 0)) < 0)
3830 return 0;
3831 offset += pos->col;
3832 }
3833
3834 return offset;
3835}
3836
3837
Bram Moolenaar009b2592004-10-24 19:18:58 +00003838/*
3839 * This message is printed after NetBeans opens a new file. Its
3840 * similar to the message readfile() uses, but since NetBeans
3841 * doesn't normally call readfile, we do our own.
3842 */
3843 static void
3844print_read_msg(buf)
3845 nbbuf_T *buf;
3846{
3847 int lnum = buf->bufp->b_ml.ml_line_count;
Bram Moolenaar914703b2010-05-31 21:59:46 +02003848 off_t nchars = buf->bufp->b_orig_size;
Bram Moolenaar009b2592004-10-24 19:18:58 +00003849 char_u c;
3850
3851 msg_add_fname(buf->bufp, buf->bufp->b_ffname);
3852 c = FALSE;
3853
3854 if (buf->bufp->b_p_ro)
3855 {
3856 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]"));
3857 c = TRUE;
3858 }
3859 if (!buf->bufp->b_start_eol)
3860 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003861 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]")
3862 : _("[Incomplete last line]"));
Bram Moolenaar009b2592004-10-24 19:18:58 +00003863 c = TRUE;
3864 }
3865 msg_add_lines(c, (long)lnum, nchars);
3866
3867 /* Now display it */
3868 vim_free(keep_msg);
3869 keep_msg = NULL;
3870 msg_scrolled_ign = TRUE;
3871 msg_trunc_attr(IObuff, FALSE, 0);
3872 msg_scrolled_ign = FALSE;
3873}
3874
3875
3876/*
Bram Moolenaar67c53842010-05-22 18:28:27 +02003877 * Print a message after NetBeans writes the file. This message should be
3878 * identical to the standard message a non-netbeans user would see when
3879 * writing a file.
Bram Moolenaar009b2592004-10-24 19:18:58 +00003880 */
3881 static void
3882print_save_msg(buf, nchars)
3883 nbbuf_T *buf;
Bram Moolenaar914703b2010-05-31 21:59:46 +02003884 off_t nchars;
Bram Moolenaar009b2592004-10-24 19:18:58 +00003885{
3886 char_u c;
3887 char_u *p;
3888
3889 if (nchars >= 0)
3890 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003891 /* put fname in IObuff with quotes */
3892 msg_add_fname(buf->bufp, buf->bufp->b_ffname);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003893 c = FALSE;
3894
3895 msg_add_lines(c, buf->bufp->b_ml.ml_line_count,
Bram Moolenaar914703b2010-05-31 21:59:46 +02003896 buf->bufp->b_orig_size);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003897
3898 vim_free(keep_msg);
3899 keep_msg = NULL;
3900 msg_scrolled_ign = TRUE;
3901 p = msg_trunc_attr(IObuff, FALSE, 0);
3902 if ((msg_scrolled && !need_wait_return) || !buf->initDone)
3903 {
3904 /* Need to repeat the message after redrawing when:
3905 * - When reading from stdin (the screen will be cleared next).
3906 * - When restart_edit is set (otherwise there will be a delay
3907 * before redrawing).
3908 * - When the screen was scrolled but there is no wait-return
3909 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003910 set_keep_msg(p, 0);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003911 }
3912 msg_scrolled_ign = FALSE;
3913 /* add_to_input_buf((char_u *)"\f", 1); */
3914 }
3915 else
3916 {
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003917 char_u msgbuf[IOSIZE];
Bram Moolenaar009b2592004-10-24 19:18:58 +00003918
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003919 vim_snprintf((char *)msgbuf, IOSIZE,
3920 _("E505: %s is read-only (add ! to override)"), IObuff);
3921 nbdebug((" %s\n", msgbuf));
3922 emsg(msgbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003923 }
3924}
3925
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926#endif /* defined(FEAT_NETBEANS_INTG) */