blob: 7ba8d8dc214d6b2b08a1892bfc72f10661287bd1 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * Netbeans integration by David Weatherford
5 * Adopted for Win32 by Sergey Khorev
6 *
7 * Do ":help uganda" in Vim to read copying and usage conditions.
8 * Do ":help credits" in Vim to see a list of people who contributed.
9 */
10
11/*
12 * Implements client side of org.netbeans.modules.emacs editor
13 * integration protocol. Be careful! The protocol uses offsets
14 * which are *between* characters, whereas vim uses line number
15 * and column number which are *on* characters.
16 * See ":help netbeans-protocol" for explanation.
17 */
18
Bram Moolenaarc236c162008-07-13 17:41:49 +000019#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
Bram Moolenaarff064e12008-06-09 13:10:45 +000020# include "vimio.h" /* for mch_open(), must be before vim.h */
21#endif
22
Bram Moolenaar071d4272004-06-13 20:20:40 +000023#include "vim.h"
24
25#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
26
Bram Moolenaarb26e6322010-05-22 21:34:09 +020027/* TODO: when should this not be defined? */
28#define INET_SOCKETS
29
Bram Moolenaar071d4272004-06-13 20:20:40 +000030/* Note: when making changes here also adjust configure.in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000031#ifdef WIN32
32# ifdef DEBUG
33# include <tchar.h> /* for _T definition for TRACEn macros */
34# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000035/* WinSock API is separated from C API, thus we can't use read(), write(),
36 * errno... */
Bram Moolenaarc39125d2010-05-23 12:06:58 +020037# define SOCK_ERRNO errno = WSAGetLastError()
Bram Moolenaarbd42a0f2009-06-16 14:57:26 +000038# undef ECONNREFUSED
Bram Moolenaar071d4272004-06-13 20:20:40 +000039# define ECONNREFUSED WSAECONNREFUSED
40# ifdef EINTR
41# undef EINTR
42# endif
43# define EINTR WSAEINTR
44# define sock_write(sd, buf, len) send(sd, buf, len, 0)
45# define sock_read(sd, buf, len) recv(sd, buf, len, 0)
46# define sock_close(sd) closesocket(sd)
47# define sleep(t) Sleep(t*1000) /* WinAPI Sleep() accepts milliseconds */
48#else
Bram Moolenaarb26e6322010-05-22 21:34:09 +020049# ifdef INET_SOCKETS
50# include <netdb.h>
51# include <netinet/in.h>
52# else
53# include <sys/un.h>
54# endif
55
Bram Moolenaar071d4272004-06-13 20:20:40 +000056# include <sys/socket.h>
57# ifdef HAVE_LIBGEN_H
58# include <libgen.h>
59# endif
Bram Moolenaarc39125d2010-05-23 12:06:58 +020060# define SOCK_ERRNO
Bram Moolenaar071d4272004-06-13 20:20:40 +000061# define sock_write(sd, buf, len) write(sd, buf, len)
62# define sock_read(sd, buf, len) read(sd, buf, len)
63# define sock_close(sd) close(sd)
64#endif
65
66#include "version.h"
67
Bram Moolenaar071d4272004-06-13 20:20:40 +000068#define GUARDED 10000 /* typenr for "guarded" annotation */
69#define GUARDEDOFFSET 1000000 /* base for "guarded" sign id's */
Bram Moolenaar67c53842010-05-22 18:28:27 +020070#define MAX_COLOR_LENGTH 32 /* max length of color name in defineAnnoType */
Bram Moolenaar071d4272004-06-13 20:20:40 +000071
72/* The first implementation (working only with Netbeans) returned "1.1". The
73 * protocol implemented here also supports A-A-P. */
Bram Moolenaar67c53842010-05-22 18:28:27 +020074static char *ExtEdProtocolVersion = "2.5";
Bram Moolenaar071d4272004-06-13 20:20:40 +000075
76static long pos2off __ARGS((buf_T *, pos_T *));
77static pos_T *off2pos __ARGS((buf_T *, long));
78static pos_T *get_off_or_lnum __ARGS((buf_T *buf, char_u **argp));
79static long get_buf_size __ARGS((buf_T *));
Bram Moolenaar8065d7f2010-01-19 15:13:14 +010080static int netbeans_keystring __ARGS((char_u *keystr));
81static void postpone_keycommand __ARGS((char_u *keystr));
Bram Moolenaar009b2592004-10-24 19:18:58 +000082static void special_keys __ARGS((char_u *args));
Bram Moolenaar071d4272004-06-13 20:20:40 +000083
Bram Moolenaarb26e6322010-05-22 21:34:09 +020084static int netbeans_connect __ARGS((char *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +000085static int getConnInfo __ARGS((char *file, char **host, char **port, char **password));
86
87static void nb_init_graphics __ARGS((void));
88static void coloncmd __ARGS((char *cmd, ...));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000089static void nb_set_curbuf __ARGS((buf_T *buf));
Bram Moolenaar173c9852010-09-29 17:27:01 +020090#ifdef FEAT_GUI_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +000091static void messageFromNetbeans __ARGS((XtPointer, int *, XtInputId *));
92#endif
93#ifdef FEAT_GUI_GTK
94static void messageFromNetbeans __ARGS((gpointer, gint, GdkInputCondition));
95#endif
96static void nb_parse_cmd __ARGS((char_u *));
97static int nb_do_cmd __ARGS((int, char_u *, int, int, char_u *));
98static void nb_send __ARGS((char *buf, char *fun));
Bram Moolenaarb26e6322010-05-22 21:34:09 +020099static void nb_free __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100
Bram Moolenaar67c53842010-05-22 18:28:27 +0200101/* TRUE when netbeans is running with a GUI. */
102#ifdef FEAT_GUI
103# define NB_HAS_GUI (gui.in_use || gui.starting)
104#endif
105
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000106#ifdef WIN64
107typedef __int64 NBSOCK;
108#else
109typedef int NBSOCK;
110#endif
111
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200112static NBSOCK nbsock = -1; /* socket fd for Netbeans connection */
113#define NETBEANS_OPEN (nbsock != -1)
114
Bram Moolenaar173c9852010-09-29 17:27:01 +0200115#ifdef FEAT_GUI_X11
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200116static XtInputId inputHandler = (XtInputId)NULL; /* Cookie for input */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117#endif
118#ifdef FEAT_GUI_GTK
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200119static gint inputHandler = 0; /* Cookie for input */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120#endif
121#ifdef FEAT_GUI_W32
122static int inputHandler = -1; /* simply ret.value of WSAAsyncSelect() */
123extern HWND s_hwnd; /* Gvim's Window handle */
124#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +0000125static int r_cmdno; /* current command number for reply */
Bram Moolenaar009b2592004-10-24 19:18:58 +0000126static int dosetvisible = FALSE;
127
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128/*
129 * Include the debugging code if wanted.
130 */
131#ifdef NBDEBUG
132# include "nbdebug.c"
133#endif
134
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200135static int needupdate = 0;
136static int inAtomic = 0;
137
Bram Moolenaar7ad7d012010-11-16 15:49:02 +0100138/*
139 * Close the socket and remove the input handlers.
140 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141 static void
Bram Moolenaar7ad7d012010-11-16 15:49:02 +0100142nb_close_socket(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143{
Bram Moolenaar173c9852010-09-29 17:27:01 +0200144#ifdef FEAT_GUI_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145 if (inputHandler != (XtInputId)NULL)
146 {
147 XtRemoveInput(inputHandler);
148 inputHandler = (XtInputId)NULL;
149 }
Bram Moolenaar67c53842010-05-22 18:28:27 +0200150#else
151# ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152 if (inputHandler != 0)
153 {
154 gdk_input_remove(inputHandler);
155 inputHandler = 0;
156 }
Bram Moolenaar67c53842010-05-22 18:28:27 +0200157# else
158# ifdef FEAT_GUI_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159 if (inputHandler == 0)
160 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200161 WSAAsyncSelect(nbsock, s_hwnd, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162 inputHandler = -1;
163 }
Bram Moolenaar67c53842010-05-22 18:28:27 +0200164# endif
165# endif
166#endif
167
Bram Moolenaar7ad7d012010-11-16 15:49:02 +0100168 sock_close(nbsock);
169 nbsock = -1;
170}
171
172/*
173 * Close the connection and cleanup.
174 * May be called when nb_close_socket() was called earlier.
175 */
176 static void
177netbeans_close(void)
178{
179 if (NETBEANS_OPEN)
180 {
181 netbeans_send_disconnect();
182 nb_close_socket();
183 }
184
Bram Moolenaar67c53842010-05-22 18:28:27 +0200185#ifdef FEAT_BEVAL
Bram Moolenaard62bec82005-03-07 22:56:57 +0000186 bevalServers &= ~BEVAL_NETBEANS;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200187#endif
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200188
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200189 needupdate = 0;
190 inAtomic = 0;
191 nb_free();
192
193 /* remove all signs and update the screen after gutter removal */
194 coloncmd(":sign unplace *");
195 changed_window_setting();
196 update_screen(CLEAR);
197 setcursor();
198 out_flush();
199#ifdef FEAT_GUI
Bram Moolenaard54a6882010-08-09 22:49:00 +0200200 if (gui.in_use)
201 {
202 gui_update_cursor(TRUE, FALSE);
203 gui_mch_flush();
204 }
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200205#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207
208#define NB_DEF_HOST "localhost"
209#define NB_DEF_ADDR "3219"
210#define NB_DEF_PASS "changeme"
211
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200212 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200213netbeans_connect(char *params, int doabort)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214{
215#ifdef INET_SOCKETS
216 struct sockaddr_in server;
217 struct hostent * host;
218# ifdef FEAT_GUI_W32
219 u_short port;
220# else
221 int port;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200222# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223#else
224 struct sockaddr_un server;
225#endif
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200226 int sd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 char buf[32];
228 char *hostname = NULL;
229 char *address = NULL;
230 char *password = NULL;
231 char *fname;
232 char *arg = NULL;
233
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200234 if (*params == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200236 /* "=fname": Read info from specified file. */
237 if (getConnInfo(params + 1, &hostname, &address, &password)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 == FAIL)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200239 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240 }
241 else
242 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200243 if (*params == ':')
244 /* ":<host>:<addr>:<password>": get info from argument */
245 arg = params + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246 if (arg == NULL && (fname = getenv("__NETBEANS_CONINFO")) != NULL)
247 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200248 /* "": get info from file specified in environment */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 if (getConnInfo(fname, &hostname, &address, &password) == FAIL)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200250 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 }
252 else
253 {
254 if (arg != NULL)
255 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200256 /* ":<host>:<addr>:<password>": get info from argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 hostname = arg;
258 address = strchr(hostname, ':');
259 if (address != NULL)
260 {
261 *address++ = '\0';
262 password = strchr(address, ':');
263 if (password != NULL)
264 *password++ = '\0';
265 }
266 }
267
268 /* Get the missing values from the environment. */
269 if (hostname == NULL || *hostname == '\0')
270 hostname = getenv("__NETBEANS_HOST");
271 if (address == NULL)
272 address = getenv("__NETBEANS_SOCKET");
273 if (password == NULL)
274 password = getenv("__NETBEANS_VIM_PASSWORD");
275
276 /* Move values to allocated memory. */
277 if (hostname != NULL)
278 hostname = (char *)vim_strsave((char_u *)hostname);
279 if (address != NULL)
280 address = (char *)vim_strsave((char_u *)address);
281 if (password != NULL)
282 password = (char *)vim_strsave((char_u *)password);
283 }
284 }
285
286 /* Use the default when a value is missing. */
287 if (hostname == NULL || *hostname == '\0')
288 {
289 vim_free(hostname);
290 hostname = (char *)vim_strsave((char_u *)NB_DEF_HOST);
291 }
292 if (address == NULL || *address == '\0')
293 {
294 vim_free(address);
295 address = (char *)vim_strsave((char_u *)NB_DEF_ADDR);
296 }
297 if (password == NULL || *password == '\0')
298 {
299 vim_free(password);
300 password = (char *)vim_strsave((char_u *)NB_DEF_PASS);
301 }
302 if (hostname == NULL || address == NULL || password == NULL)
303 goto theend; /* out of memory */
304
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200305#ifdef FEAT_GUI_W32
306 netbeans_init_winsock();
307#endif
308
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309#ifdef INET_SOCKETS
310 port = atoi(address);
311
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000312 if ((sd = (NBSOCK)socket(AF_INET, SOCK_STREAM, 0)) == (NBSOCK)-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000314 nbdebug(("error in socket() in netbeans_connect()\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315 PERROR("socket() in netbeans_connect()");
316 goto theend;
317 }
318
319 /* Get the server internet address and put into addr structure */
320 /* fill in the socket address structure and connect to server */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200321 vim_memset((char *)&server, '\0', sizeof(server));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322 server.sin_family = AF_INET;
323 server.sin_port = htons(port);
324 if ((host = gethostbyname(hostname)) == NULL)
325 {
326 if (mch_access(hostname, R_OK) >= 0)
327 {
328 /* DEBUG: input file */
329 sd = mch_open(hostname, O_RDONLY, 0);
330 goto theend;
331 }
Bram Moolenaarf2330482008-06-24 20:19:36 +0000332 nbdebug(("error in gethostbyname() in netbeans_connect()\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 PERROR("gethostbyname() in netbeans_connect()");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334 goto theend;
335 }
336 memcpy((char *)&server.sin_addr, host->h_addr, host->h_length);
337#else
338 if ((sd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
339 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000340 nbdebug(("error in socket() in netbeans_connect()\n"));
341 PERROR("socket() in netbeans_connect()");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342 goto theend;
343 }
344
345 server.sun_family = AF_UNIX;
346 strcpy(server.sun_path, address);
347#endif
348 /* Connect to server */
349 if (connect(sd, (struct sockaddr *)&server, sizeof(server)))
350 {
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200351 SOCK_ERRNO;
352 nbdebug(("netbeans_connect: Connect failed with errno %d\n", errno));
353 if (errno == ECONNREFUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354 {
355 sock_close(sd);
356#ifdef INET_SOCKETS
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000357 if ((sd = (NBSOCK)socket(AF_INET, SOCK_STREAM, 0)) == (NBSOCK)-1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 {
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200359 SOCK_ERRNO;
Bram Moolenaarf2330482008-06-24 20:19:36 +0000360 nbdebug(("socket()#2 in netbeans_connect()\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 PERROR("socket()#2 in netbeans_connect()");
362 goto theend;
363 }
364#else
365 if ((sd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
366 {
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200367 SOCK_ERRNO;
Bram Moolenaarf2330482008-06-24 20:19:36 +0000368 nbdebug(("socket()#2 in netbeans_connect()\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 PERROR("socket()#2 in netbeans_connect()");
370 goto theend;
371 }
372#endif
373 if (connect(sd, (struct sockaddr *)&server, sizeof(server)))
374 {
375 int retries = 36;
376 int success = FALSE;
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200377
378 SOCK_ERRNO;
379 while (retries-- && ((errno == ECONNREFUSED)
380 || (errno == EINTR)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 {
382 nbdebug(("retrying...\n"));
383 sleep(5);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200384 if (!doabort)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200385 {
386 ui_breakcheck();
387 if (got_int)
388 {
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200389 errno = EINTR;
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200390 break;
391 }
392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 if (connect(sd, (struct sockaddr *)&server,
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200394 sizeof(server)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 {
396 success = TRUE;
397 break;
398 }
Bram Moolenaarc39125d2010-05-23 12:06:58 +0200399 SOCK_ERRNO;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 }
401 if (!success)
402 {
403 /* Get here when the server can't be found. */
Bram Moolenaarf2330482008-06-24 20:19:36 +0000404 nbdebug(("Cannot connect to Netbeans #2\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405 PERROR(_("Cannot connect to Netbeans #2"));
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200406 if (doabort)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200407 getout(1);
408 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 }
410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411 }
412 else
413 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000414 nbdebug(("Cannot connect to Netbeans\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415 PERROR(_("Cannot connect to Netbeans"));
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200416 if (doabort)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200417 getout(1);
418 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 }
420 }
421
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200422 nbsock = sd;
Bram Moolenaar9c13b352005-05-19 20:53:52 +0000423 vim_snprintf(buf, sizeof(buf), "AUTH %s\n", password);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 nb_send(buf, "netbeans_connect");
425
426 sprintf(buf, "0:version=0 \"%s\"\n", ExtEdProtocolVersion);
427 nb_send(buf, "externaleditor_version");
428
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429theend:
430 vim_free(hostname);
431 vim_free(address);
432 vim_free(password);
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200433 return NETBEANS_OPEN ? OK : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434}
435
436/*
437 * Obtain the NetBeans hostname, port address and password from a file.
438 * Return the strings in allocated memory.
439 * Return FAIL if the file could not be read, OK otherwise (no matter what it
440 * contains).
441 */
442 static int
443getConnInfo(char *file, char **host, char **port, char **auth)
444{
445 FILE *fp;
446 char_u buf[BUFSIZ];
447 char_u *lp;
448 char_u *nl;
449#ifdef UNIX
450 struct stat st;
451
452 /*
453 * For Unix only accept the file when it's not accessible by others.
454 * The open will then fail if we don't own the file.
455 */
456 if (mch_stat(file, &st) == 0 && (st.st_mode & 0077) != 0)
457 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000458 nbdebug(("Wrong access mode for NetBeans connection info file: \"%s\"\n",
459 file));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460 EMSG2(_("E668: Wrong access mode for NetBeans connection info file: \"%s\""),
461 file);
462 return FAIL;
463 }
464#endif
465
466 fp = mch_fopen(file, "r");
467 if (fp == NULL)
468 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000469 nbdebug(("Cannot open NetBeans connection info file\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470 PERROR("E660: Cannot open NetBeans connection info file");
471 return FAIL;
472 }
473
474 /* Read the file. There should be one of each parameter */
475 while ((lp = (char_u *)fgets((char *)buf, BUFSIZ, fp)) != NULL)
476 {
477 if ((nl = vim_strchr(lp, '\n')) != NULL)
478 *nl = 0; /* strip off the trailing newline */
479
480 if (STRNCMP(lp, "host=", 5) == 0)
481 {
482 vim_free(*host);
483 *host = (char *)vim_strsave(&buf[5]);
484 }
485 else if (STRNCMP(lp, "port=", 5) == 0)
486 {
487 vim_free(*port);
488 *port = (char *)vim_strsave(&buf[5]);
489 }
490 else if (STRNCMP(lp, "auth=", 5) == 0)
491 {
492 vim_free(*auth);
493 *auth = (char *)vim_strsave(&buf[5]);
494 }
495 }
496 fclose(fp);
497
498 return OK;
499}
500
501
502struct keyqueue
503{
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100504 char_u *keystr;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 struct keyqueue *next;
506 struct keyqueue *prev;
507};
508
509typedef struct keyqueue keyQ_T;
510
511static keyQ_T keyHead; /* dummy node, header for circular queue */
512
513
514/*
515 * Queue up key commands sent from netbeans.
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100516 * We store the string, because it may depend on the global mod_mask and
517 * :nbkey doesn't have a key number.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 */
519 static void
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100520postpone_keycommand(char_u *keystr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521{
522 keyQ_T *node;
523
524 node = (keyQ_T *)alloc(sizeof(keyQ_T));
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100525 if (node == NULL)
526 return; /* out of memory, drop the key */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527
528 if (keyHead.next == NULL) /* initialize circular queue */
529 {
530 keyHead.next = &keyHead;
531 keyHead.prev = &keyHead;
532 }
533
534 /* insert node at tail of queue */
535 node->next = &keyHead;
536 node->prev = keyHead.prev;
537 keyHead.prev->next = node;
538 keyHead.prev = node;
539
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100540 node->keystr = vim_strsave(keystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541}
542
543/*
544 * Handle any queued-up NetBeans keycommands to be send.
545 */
546 static void
547handle_key_queue(void)
548{
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100549 int postponed = FALSE;
550
551 while (!postponed && keyHead.next && keyHead.next != &keyHead)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 {
553 /* first, unlink the node */
554 keyQ_T *node = keyHead.next;
555 keyHead.next = node->next;
556 node->next->prev = node->prev;
557
Bram Moolenaar8065d7f2010-01-19 15:13:14 +0100558 /* Now, send the keycommand. This may cause it to be postponed again
559 * and change keyHead. */
560 if (node->keystr != NULL)
561 postponed = !netbeans_keystring(node->keystr);
562 vim_free(node->keystr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563
564 /* Finally, dispose of the node */
565 vim_free(node);
566 }
567}
568
569
570struct cmdqueue
571{
572 char_u *buffer;
573 struct cmdqueue *next;
574 struct cmdqueue *prev;
575};
576
577typedef struct cmdqueue queue_T;
578
579static queue_T head; /* dummy node, header for circular queue */
580
581
582/*
583 * Put the buffer on the work queue; possibly save it to a file as well.
584 */
585 static void
586save(char_u *buf, int len)
587{
588 queue_T *node;
589
590 node = (queue_T *)alloc(sizeof(queue_T));
591 if (node == NULL)
592 return; /* out of memory */
593 node->buffer = alloc(len + 1);
594 if (node->buffer == NULL)
595 {
596 vim_free(node);
597 return; /* out of memory */
598 }
599 mch_memmove(node->buffer, buf, (size_t)len);
600 node->buffer[len] = NUL;
601
602 if (head.next == NULL) /* initialize circular queue */
603 {
604 head.next = &head;
605 head.prev = &head;
606 }
607
608 /* insert node at tail of queue */
609 node->next = &head;
610 node->prev = head.prev;
611 head.prev->next = node;
612 head.prev = node;
613
614#ifdef NBDEBUG
615 {
616 static int outfd = -2;
617
618 /* possibly write buffer out to a file */
619 if (outfd == -3)
620 return;
621
622 if (outfd == -2)
623 {
624 char *file = getenv("__NETBEANS_SAVE");
625 if (file == NULL)
626 outfd = -3;
627 else
628 outfd = mch_open(file, O_WRONLY|O_CREAT|O_TRUNC, 0666);
629 }
630
631 if (outfd >= 0)
632 write(outfd, buf, len);
633 }
634#endif
635}
636
637
638/*
639 * While there's still a command in the work queue, parse and execute it.
640 */
Bram Moolenaarf2330482008-06-24 20:19:36 +0000641 void
642netbeans_parse_messages(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643{
644 char_u *p;
645 queue_T *node;
646
Bram Moolenaarf2330482008-06-24 20:19:36 +0000647 while (head.next != NULL && head.next != &head)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 {
649 node = head.next;
650
651 /* Locate the first line in the first buffer. */
652 p = vim_strchr(node->buffer, '\n');
653 if (p == NULL)
654 {
655 /* Command isn't complete. If there is no following buffer,
656 * return (wait for more). If there is another buffer following,
657 * prepend the text to that buffer and delete this one. */
658 if (node->next == &head)
659 return;
Bram Moolenaarf2330482008-06-24 20:19:36 +0000660 p = alloc((unsigned)(STRLEN(node->buffer)
661 + STRLEN(node->next->buffer) + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 if (p == NULL)
663 return; /* out of memory */
664 STRCPY(p, node->buffer);
665 STRCAT(p, node->next->buffer);
666 vim_free(node->next->buffer);
667 node->next->buffer = p;
668
669 /* dispose of the node and buffer */
670 head.next = node->next;
671 node->next->prev = node->prev;
672 vim_free(node->buffer);
673 vim_free(node);
674 }
675 else
676 {
677 /* There is a complete command at the start of the buffer.
678 * Terminate it with a NUL. When no more text is following unlink
679 * the buffer. Do this before executing, because new buffers can
680 * be added while busy handling the command. */
681 *p++ = NUL;
682 if (*p == NUL)
683 {
684 head.next = node->next;
685 node->next->prev = node->prev;
686 }
687
688 /* now, parse and execute the commands */
689 nb_parse_cmd(node->buffer);
690
691 if (*p == NUL)
692 {
693 /* buffer finished, dispose of the node and buffer */
694 vim_free(node->buffer);
695 vim_free(node);
696 }
697 else
698 {
699 /* more follows, move to the start */
Bram Moolenaarf2330482008-06-24 20:19:36 +0000700 STRMOVE(node->buffer, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 }
702 }
703 }
704}
705
706/* Buffer size for reading incoming messages. */
707#define MAXMSGSIZE 4096
708
709/*
Bram Moolenaar67c53842010-05-22 18:28:27 +0200710 * Read a command from netbeans.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 */
Bram Moolenaar173c9852010-09-29 17:27:01 +0200712#ifdef FEAT_GUI_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 static void
Bram Moolenaara9d45512009-05-17 21:25:42 +0000714messageFromNetbeans(XtPointer clientData UNUSED,
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000715 int *unused1 UNUSED,
716 XtInputId *unused2 UNUSED)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200717{
718 netbeans_read();
719}
720#endif
721
722#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000724messageFromNetbeans(gpointer clientData UNUSED,
725 gint unused1 UNUSED,
726 GdkInputCondition unused2 UNUSED)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200727{
728 netbeans_read();
729}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730#endif
Bram Moolenaar67c53842010-05-22 18:28:27 +0200731
Bram Moolenaar7ad7d012010-11-16 15:49:02 +0100732#define DETACH_MSG "DETACH\n"
733
Bram Moolenaar67c53842010-05-22 18:28:27 +0200734 void
735netbeans_read()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736{
737 static char_u *buf = NULL;
Bram Moolenaar0b65f892010-05-15 14:49:02 +0200738 int len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 int readlen = 0;
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100740#ifdef HAVE_SELECT
741 struct timeval tval;
742 fd_set rfds;
743#else
744# ifdef HAVE_POLL
745 struct pollfd fds;
746# endif
747#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200749 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 {
751 nbdebug(("messageFromNetbeans() called without a socket\n"));
752 return;
753 }
754
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 /* Allocate a buffer to read into. */
756 if (buf == NULL)
757 {
758 buf = alloc(MAXMSGSIZE);
759 if (buf == NULL)
760 return; /* out of memory! */
761 }
762
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100763 /* Keep on reading for as long as there is something to read.
764 * Use select() or poll() to avoid blocking on a message that is exactly
765 * MAXMSGSIZE long. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 for (;;)
767 {
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100768#ifdef HAVE_SELECT
769 FD_ZERO(&rfds);
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200770 FD_SET(nbsock, &rfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +0200771 tval.tv_sec = 0;
772 tval.tv_usec = 0;
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200773 if (select(nbsock + 1, &rfds, NULL, NULL, &tval) <= 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200774 break;
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100775#else
776# ifdef HAVE_POLL
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200777 fds.fd = nbsock;
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100778 fds.events = POLLIN;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200779 if (poll(&fds, 1, 0) <= 0)
780 break;
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100781# endif
782#endif
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200783 len = sock_read(nbsock, buf, MAXMSGSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 if (len <= 0)
785 break; /* error or nothing more to read */
786
787 /* Store the read message in the queue. */
788 save(buf, len);
789 readlen += len;
790 if (len < MAXMSGSIZE)
791 break; /* did read everything that's available */
792 }
793
Bram Moolenaar7ad7d012010-11-16 15:49:02 +0100794 /* Reading a socket disconnection (readlen == 0), or a socket error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 if (readlen <= 0)
796 {
Bram Moolenaar7ad7d012010-11-16 15:49:02 +0100797 /* Queue a "DETACH" netbeans message in the command queue in order to
798 * terminate the netbeans session later. Do not end the session here
799 * directly as we may be running in the context of a call to
800 * netbeans_parse_messages():
801 * netbeans_parse_messages
802 * -> autocmd triggered while processing the netbeans cmd
803 * -> ui_breakcheck
804 * -> gui event loop or select loop
805 * -> netbeans_read()
806 */
807 save((char_u *)DETACH_MSG, strlen(DETACH_MSG));
808 nb_close_socket();
809
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 if (len < 0)
Bram Moolenaarf2330482008-06-24 20:19:36 +0000811 {
812 nbdebug(("read from Netbeans socket\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 PERROR(_("read from Netbeans socket"));
Bram Moolenaarf2330482008-06-24 20:19:36 +0000814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 }
816
Bram Moolenaar03531f72010-11-16 15:04:57 +0100817#if defined(NB_HAS_GUI) && defined(FEAT_GUI_GTK)
818 if (NB_HAS_GUI && gtk_main_level() > 0)
Bram Moolenaar7ad7d012010-11-16 15:49:02 +0100819 gtk_main_quit();
Bram Moolenaarf2330482008-06-24 20:19:36 +0000820#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821}
822
823/*
824 * Handle one NUL terminated command.
825 *
826 * format of a command from netbeans:
827 *
828 * 6:setTitle!84 "a.c"
829 *
830 * bufno
831 * colon
832 * cmd
833 * !
834 * cmdno
835 * args
836 *
837 * for function calls, the ! is replaced by a /
838 */
839 static void
840nb_parse_cmd(char_u *cmd)
841{
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000842 char *verb;
843 char *q;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 int bufno;
845 int isfunc = -1;
846
847 if (STRCMP(cmd, "DISCONNECT") == 0)
848 {
849 /* We assume the server knows that we can safely exit! */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 /* Disconnect before exiting, Motif hangs in a Select error
851 * message otherwise. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200852 netbeans_close();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 getout(0);
854 /* NOTREACHED */
855 }
856
857 if (STRCMP(cmd, "DETACH") == 0)
858 {
859 /* The IDE is breaking the connection. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200860 netbeans_close();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 return;
862 }
863
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000864 bufno = strtol((char *)cmd, &verb, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865
866 if (*verb != ':')
867 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000868 nbdebug((" missing colon: %s\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 EMSG2("E627: missing colon: %s", cmd);
870 return;
871 }
872 ++verb; /* skip colon */
873
874 for (q = verb; *q; q++)
875 {
876 if (*q == '!')
877 {
878 *q++ = NUL;
879 isfunc = 0;
880 break;
881 }
882 else if (*q == '/')
883 {
884 *q++ = NUL;
885 isfunc = 1;
886 break;
887 }
888 }
889
890 if (isfunc < 0)
891 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000892 nbdebug((" missing ! or / in: %s\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 EMSG2("E628: missing ! or / in: %s", cmd);
894 return;
895 }
896
Bram Moolenaar89d40322006-08-29 15:30:07 +0000897 r_cmdno = strtol(q, &q, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000899 q = (char *)skipwhite((char_u *)q);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900
Bram Moolenaar89d40322006-08-29 15:30:07 +0000901 if (nb_do_cmd(bufno, (char_u *)verb, isfunc, r_cmdno, (char_u *)q) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 {
Bram Moolenaar009b2592004-10-24 19:18:58 +0000903#ifdef NBDEBUG
904 /*
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100905 * This happens because the ExtEd can send a command or 2 after
Bram Moolenaar009b2592004-10-24 19:18:58 +0000906 * doing a stopDocumentListen command. It doesn't harm anything
907 * so I'm disabling it except for debugging.
908 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 nbdebug(("nb_parse_cmd: Command error for \"%s\"\n", cmd));
910 EMSG("E629: bad return from nb_do_cmd");
Bram Moolenaar009b2592004-10-24 19:18:58 +0000911#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 }
913}
914
915struct nbbuf_struct
916{
917 buf_T *bufp;
918 unsigned int fireChanges:1;
919 unsigned int initDone:1;
Bram Moolenaar009b2592004-10-24 19:18:58 +0000920 unsigned int insertDone:1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 unsigned int modified:1;
Bram Moolenaar009b2592004-10-24 19:18:58 +0000922 int nbbuf_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 char *displayname;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 int *signmap;
925 short_u signmaplen;
926 short_u signmapused;
927};
928
929typedef struct nbbuf_struct nbbuf_T;
930
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200931static nbbuf_T *buf_list = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000932static int buf_list_size = 0; /* size of buf_list */
933static int buf_list_used = 0; /* nr of entries in buf_list actually in use */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200935static char **globalsignmap = NULL;
936static int globalsignmaplen = 0;
937static int globalsignmapused = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938
939static int mapsigntype __ARGS((nbbuf_T *, int localsigntype));
940static void addsigntype __ARGS((nbbuf_T *, int localsigntype, char_u *typeName,
941 char_u *tooltip, char_u *glyphfile,
Bram Moolenaar67c53842010-05-22 18:28:27 +0200942 char_u *fg, char_u *bg));
Bram Moolenaar009b2592004-10-24 19:18:58 +0000943static void print_read_msg __ARGS((nbbuf_T *buf));
Bram Moolenaar914703b2010-05-31 21:59:46 +0200944static void print_save_msg __ARGS((nbbuf_T *buf, off_t nchars));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945
946static int curPCtype = -1;
947
948/*
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200949 * Free netbeans resources.
950 */
951 static void
952nb_free()
953{
954 keyQ_T *key_node = keyHead.next;
955 queue_T *cmd_node = head.next;
956 nbbuf_T buf;
957 buf_T *bufp;
958 int i;
959
960 /* free the netbeans buffer list */
961 for (i = 0; i < buf_list_used; i++)
962 {
963 buf = buf_list[i];
964 vim_free(buf.displayname);
965 vim_free(buf.signmap);
966 if ((bufp=buf.bufp) != NULL)
967 {
968 buf.bufp->b_netbeans_file = FALSE;
969 buf.bufp->b_was_netbeans_file = FALSE;
970 }
971 }
972 vim_free(buf_list);
973 buf_list = NULL;
974 buf_list_size = 0;
975 buf_list_used = 0;
976
977 /* free the queued key commands */
978 while(key_node != NULL && key_node != &keyHead)
979 {
980 keyQ_T *next = key_node->next;
981 vim_free(key_node->keystr);
982 vim_free(key_node);
983 if (next == &keyHead)
984 {
985 keyHead.next = &keyHead;
986 keyHead.prev = &keyHead;
987 break;
988 }
989 key_node = next;
990 }
991
992 /* free the queued netbeans commands */
993 while(cmd_node != NULL && cmd_node != &head)
994 {
995 queue_T *next = cmd_node->next;
996 vim_free(cmd_node->buffer);
997 vim_free(cmd_node);
998 if (next == &head)
999 {
1000 head.next = &head;
1001 head.prev = &head;
1002 break;
1003 }
1004 cmd_node = next;
1005 }
1006}
1007
1008/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 * Get the Netbeans buffer number for the specified buffer.
1010 */
1011 static int
1012nb_getbufno(buf_T *bufp)
1013{
1014 int i;
1015
1016 for (i = 0; i < buf_list_used; i++)
1017 if (buf_list[i].bufp == bufp)
1018 return i;
1019 return -1;
1020}
1021
1022/*
1023 * Is this a NetBeans-owned buffer?
1024 */
1025 int
1026isNetbeansBuffer(buf_T *bufp)
1027{
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001028 return NETBEANS_OPEN && bufp->b_netbeans_file;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029}
1030
1031/*
1032 * NetBeans and Vim have different undo models. In Vim, the file isn't
1033 * changed if changes are undone via the undo command. In NetBeans, once
1034 * a change has been made the file is marked as modified until saved. It
1035 * doesn't matter if the change was undone.
1036 *
1037 * So this function is for the corner case where Vim thinks a buffer is
1038 * unmodified but NetBeans thinks it IS modified.
1039 */
1040 int
1041isNetbeansModified(buf_T *bufp)
1042{
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001043 if (isNetbeansBuffer(bufp))
Bram Moolenaar009b2592004-10-24 19:18:58 +00001044 {
1045 int bufno = nb_getbufno(bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046
Bram Moolenaar009b2592004-10-24 19:18:58 +00001047 if (bufno > 0)
1048 return buf_list[bufno].modified;
1049 else
1050 return FALSE;
1051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 else
1053 return FALSE;
1054}
1055
1056/*
1057 * Given a Netbeans buffer number, return the netbeans buffer.
1058 * Returns NULL for 0 or a negative number. A 0 bufno means a
1059 * non-buffer related command has been sent.
1060 */
1061 static nbbuf_T *
1062nb_get_buf(int bufno)
1063{
1064 /* find or create a buffer with the given number */
1065 int incr;
1066
1067 if (bufno <= 0)
1068 return NULL;
1069
1070 if (!buf_list)
1071 {
1072 /* initialize */
1073 buf_list = (nbbuf_T *)alloc_clear(100 * sizeof(nbbuf_T));
1074 buf_list_size = 100;
1075 }
1076 if (bufno >= buf_list_used) /* new */
1077 {
1078 if (bufno >= buf_list_size) /* grow list */
1079 {
1080 incr = bufno - buf_list_size + 90;
1081 buf_list_size += incr;
1082 buf_list = (nbbuf_T *)vim_realloc(
1083 buf_list, buf_list_size * sizeof(nbbuf_T));
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02001084 vim_memset(buf_list + buf_list_size - incr, 0,
1085 incr * sizeof(nbbuf_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 }
1087
1088 while (buf_list_used <= bufno)
1089 {
1090 /* Default is to fire text changes. */
1091 buf_list[buf_list_used].fireChanges = 1;
1092 ++buf_list_used;
1093 }
1094 }
1095
1096 return buf_list + bufno;
1097}
1098
1099/*
1100 * Return the number of buffers that are modified.
1101 */
1102 static int
1103count_changed_buffers(void)
1104{
1105 buf_T *bufp;
1106 int n;
1107
1108 n = 0;
1109 for (bufp = firstbuf; bufp != NULL; bufp = bufp->b_next)
1110 if (bufp->b_changed)
1111 ++n;
1112 return n;
1113}
1114
1115/*
1116 * End the netbeans session.
1117 */
1118 void
1119netbeans_end(void)
1120{
1121 int i;
1122 static char buf[128];
1123
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001124 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 return;
1126
1127 for (i = 0; i < buf_list_used; i++)
1128 {
1129 if (!buf_list[i].bufp)
1130 continue;
1131 if (netbeansForcedQuit)
1132 {
1133 /* mark as unmodified so NetBeans won't put up dialog on "killed" */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001134 sprintf(buf, "%d:unmodified=%d\n", i, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 nbdebug(("EVT: %s", buf));
1136 nb_send(buf, "netbeans_end");
1137 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001138 sprintf(buf, "%d:killed=%d\n", i, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 nbdebug(("EVT: %s", buf));
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001140 /* nb_send(buf, "netbeans_end"); avoid "write failed" messages */
1141 ignored = sock_write(nbsock, buf, (int)STRLEN(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143}
1144
1145/*
1146 * Send a message to netbeans.
1147 */
1148 static void
1149nb_send(char *buf, char *fun)
1150{
1151 /* Avoid giving pages full of error messages when the other side has
1152 * exited, only mention the first error until the connection works again. */
1153 static int did_error = FALSE;
1154
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001155 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 {
1157 if (!did_error)
Bram Moolenaarf2330482008-06-24 20:19:36 +00001158 {
1159 nbdebug((" %s(): write while not connected\n", fun));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 EMSG2("E630: %s(): write while not connected", fun);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 did_error = TRUE;
1163 }
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001164 else if (sock_write(nbsock, buf, (int)STRLEN(buf)) != (int)STRLEN(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 {
1166 if (!did_error)
Bram Moolenaarf2330482008-06-24 20:19:36 +00001167 {
1168 nbdebug((" %s(): write failed\n", fun));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 EMSG2("E631: %s(): write failed", fun);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 did_error = TRUE;
1172 }
1173 else
1174 did_error = FALSE;
1175}
1176
1177/*
1178 * Some input received from netbeans requires a response. This function
1179 * handles a response with no information (except the command number).
1180 */
1181 static void
1182nb_reply_nil(int cmdno)
1183{
1184 char reply[32];
1185
Bram Moolenaar009b2592004-10-24 19:18:58 +00001186 nbdebug(("REP %d: <none>\n", cmdno));
1187
Bram Moolenaar7ad7d012010-11-16 15:49:02 +01001188 /* Avoid printing an annoying error message. */
1189 if (!NETBEANS_OPEN)
1190 return;
1191
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 sprintf(reply, "%d\n", cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 nb_send(reply, "nb_reply_nil");
1194}
1195
1196
1197/*
1198 * Send a response with text.
1199 * "result" must have been quoted already (using nb_quote()).
1200 */
1201 static void
1202nb_reply_text(int cmdno, char_u *result)
1203{
1204 char_u *reply;
1205
Bram Moolenaar009b2592004-10-24 19:18:58 +00001206 nbdebug(("REP %d: %s\n", cmdno, (char *)result));
1207
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001208 reply = alloc((unsigned)STRLEN(result) + 32);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 sprintf((char *)reply, "%d %s\n", cmdno, (char *)result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 nb_send((char *)reply, "nb_reply_text");
1211
1212 vim_free(reply);
1213}
1214
1215
1216/*
1217 * Send a response with a number result code.
1218 */
1219 static void
1220nb_reply_nr(int cmdno, long result)
1221{
1222 char reply[32];
1223
Bram Moolenaar009b2592004-10-24 19:18:58 +00001224 nbdebug(("REP %d: %ld\n", cmdno, result));
1225
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 sprintf(reply, "%d %ld\n", cmdno, result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 nb_send(reply, "nb_reply_nr");
1228}
1229
1230
1231/*
1232 * Encode newline, ret, backslash, double quote for transmission to NetBeans.
1233 */
1234 static char_u *
1235nb_quote(char_u *txt)
1236{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001237 char_u *buf = alloc((unsigned)(2 * STRLEN(txt) + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 char_u *p = txt;
1239 char_u *q = buf;
1240
1241 if (buf == NULL)
1242 return NULL;
1243 for (; *p; p++)
1244 {
1245 switch (*p)
1246 {
1247 case '\"':
1248 case '\\':
1249 *q++ = '\\'; *q++ = *p; break;
1250 /* case '\t': */
1251 /* *q++ = '\\'; *q++ = 't'; break; */
1252 case '\n':
1253 *q++ = '\\'; *q++ = 'n'; break;
1254 case '\r':
1255 *q++ = '\\'; *q++ = 'r'; break;
1256 default:
1257 *q++ = *p;
1258 break;
1259 }
1260 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01001261 *q = '\0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262
1263 return buf;
1264}
1265
1266
1267/*
1268 * Remove top level double quotes; convert backslashed chars.
1269 * Returns an allocated string (NULL for failure).
1270 * If "endp" is not NULL it is set to the character after the terminating
1271 * quote.
1272 */
1273 static char *
1274nb_unquote(char_u *p, char_u **endp)
1275{
1276 char *result = 0;
1277 char *q;
1278 int done = 0;
1279
1280 /* result is never longer than input */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001281 result = (char *)alloc_clear((unsigned)STRLEN(p) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 if (result == NULL)
1283 return NULL;
1284
1285 if (*p++ != '"')
1286 {
1287 nbdebug(("nb_unquote called with string that doesn't start with a quote!: %s\n",
1288 p));
1289 result[0] = NUL;
1290 return result;
1291 }
1292
1293 for (q = result; !done && *p != NUL;)
1294 {
1295 switch (*p)
1296 {
1297 case '"':
1298 /*
1299 * Unbackslashed dquote marks the end, if first char was dquote.
1300 */
1301 done = 1;
1302 break;
1303
1304 case '\\':
1305 ++p;
1306 switch (*p)
1307 {
1308 case '\\': *q++ = '\\'; break;
1309 case 'n': *q++ = '\n'; break;
1310 case 't': *q++ = '\t'; break;
1311 case 'r': *q++ = '\r'; break;
1312 case '"': *q++ = '"'; break;
1313 case NUL: --p; break;
1314 /* default: skip over illegal chars */
1315 }
1316 ++p;
1317 break;
1318
1319 default:
1320 *q++ = *p++;
1321 }
1322 }
1323
1324 if (endp != NULL)
1325 *endp = p;
1326
1327 return result;
1328}
1329
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001330/*
1331 * Remove from "first" byte to "last" byte (inclusive), at line "lnum" of the
1332 * current buffer. Remove to end of line when "last" is MAXCOL.
1333 */
1334 static void
1335nb_partialremove(linenr_T lnum, colnr_T first, colnr_T last)
1336{
1337 char_u *oldtext, *newtext;
1338 int oldlen;
1339 int lastbyte = last;
1340
1341 oldtext = ml_get(lnum);
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001342 oldlen = (int)STRLEN(oldtext);
Bram Moolenaarb3c70982008-01-18 10:40:55 +00001343 if (first >= (colnr_T)oldlen || oldlen == 0) /* just in case */
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001344 return;
1345 if (lastbyte >= oldlen)
1346 lastbyte = oldlen - 1;
1347 newtext = alloc(oldlen - (int)(lastbyte - first));
1348 if (newtext != NULL)
1349 {
1350 mch_memmove(newtext, oldtext, first);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001351 STRMOVE(newtext + first, oldtext + lastbyte + 1);
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001352 nbdebug((" NEW LINE %d: %s\n", lnum, newtext));
1353 ml_replace(lnum, newtext, FALSE);
1354 }
1355}
1356
1357/*
1358 * Replace the "first" line with the concatenation of the "first" and
1359 * the "other" line. The "other" line is not removed.
1360 */
1361 static void
1362nb_joinlines(linenr_T first, linenr_T other)
1363{
1364 int len_first, len_other;
1365 char_u *p;
1366
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001367 len_first = (int)STRLEN(ml_get(first));
1368 len_other = (int)STRLEN(ml_get(other));
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001369 p = alloc((unsigned)(len_first + len_other + 1));
1370 if (p != NULL)
1371 {
1372 mch_memmove(p, ml_get(first), len_first);
1373 mch_memmove(p + len_first, ml_get(other), len_other + 1);
1374 ml_replace(first, p, FALSE);
1375 }
1376}
1377
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378#define SKIP_STOP 2
1379#define streq(a,b) (strcmp(a,b) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380
1381/*
1382 * Do the actual processing of a single netbeans command or function.
Bram Moolenaar143c38c2007-05-10 16:41:10 +00001383 * The difference between a command and function is that a function
1384 * gets a response (it's required) but a command does not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 * For arguments see comment for nb_parse_cmd().
1386 */
1387 static int
1388nb_do_cmd(
1389 int bufno,
1390 char_u *cmd,
1391 int func,
1392 int cmdno,
1393 char_u *args) /* points to space before arguments or NUL */
1394{
1395 int doupdate = 0;
1396 long off = 0;
1397 nbbuf_T *buf = nb_get_buf(bufno);
1398 static int skip = 0;
1399 int retval = OK;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001400 char *cp; /* for when a char pointer is needed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401
1402 nbdebug(("%s %d: (%d) %s %s\n", (func) ? "FUN" : "CMD", cmdno, bufno, cmd,
1403 STRCMP(cmd, "insert") == 0 ? "<text>" : (char *)args));
1404
1405 if (func)
1406 {
1407/* =====================================================================*/
1408 if (streq((char *)cmd, "getModified"))
1409 {
1410 if (buf == NULL || buf->bufp == NULL)
1411 /* Return the number of buffers that are modified. */
1412 nb_reply_nr(cmdno, (long)count_changed_buffers());
1413 else
1414 /* Return whether the buffer is modified. */
1415 nb_reply_nr(cmdno, (long)(buf->bufp->b_changed
1416 || isNetbeansModified(buf->bufp)));
1417/* =====================================================================*/
1418 }
1419 else if (streq((char *)cmd, "saveAndExit"))
1420 {
1421 /* Note: this will exit Vim if successful. */
1422 coloncmd(":confirm qall");
1423
1424 /* We didn't exit: return the number of changed buffers. */
1425 nb_reply_nr(cmdno, (long)count_changed_buffers());
1426/* =====================================================================*/
1427 }
1428 else if (streq((char *)cmd, "getCursor"))
1429 {
1430 char_u text[200];
1431
1432 /* Note: nb_getbufno() may return -1. This indicates the IDE
1433 * didn't assign a number to the current buffer in response to a
1434 * fileOpened event. */
1435 sprintf((char *)text, "%d %ld %d %ld",
1436 nb_getbufno(curbuf),
1437 (long)curwin->w_cursor.lnum,
1438 (int)curwin->w_cursor.col,
1439 pos2off(curbuf, &curwin->w_cursor));
1440 nb_reply_text(cmdno, text);
1441/* =====================================================================*/
1442 }
Bram Moolenaarc65c4912006-11-14 17:29:46 +00001443 else if (streq((char *)cmd, "getAnno"))
1444 {
1445 long linenum = 0;
1446#ifdef FEAT_SIGNS
1447 if (buf == NULL || buf->bufp == NULL)
1448 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001449 nbdebug((" Invalid buffer identifier in getAnno\n"));
1450 EMSG("E652: Invalid buffer identifier in getAnno");
Bram Moolenaarc65c4912006-11-14 17:29:46 +00001451 retval = FAIL;
1452 }
1453 else
1454 {
1455 int serNum;
1456
1457 cp = (char *)args;
1458 serNum = strtol(cp, &cp, 10);
1459 /* If the sign isn't found linenum will be zero. */
1460 linenum = (long)buf_findsign(buf->bufp, serNum);
1461 }
1462#endif
1463 nb_reply_nr(cmdno, linenum);
1464/* =====================================================================*/
1465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 else if (streq((char *)cmd, "getLength"))
1467 {
1468 long len = 0;
1469
1470 if (buf == NULL || buf->bufp == NULL)
1471 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001472 nbdebug((" invalid buffer identifier in getLength\n"));
1473 EMSG("E632: invalid buffer identifier in getLength");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 retval = FAIL;
1475 }
1476 else
1477 {
1478 len = get_buf_size(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 }
1480 nb_reply_nr(cmdno, len);
1481/* =====================================================================*/
1482 }
1483 else if (streq((char *)cmd, "getText"))
1484 {
1485 long len;
1486 linenr_T nlines;
1487 char_u *text = NULL;
1488 linenr_T lno = 1;
1489 char_u *p;
1490 char_u *line;
1491
1492 if (buf == NULL || buf->bufp == NULL)
1493 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001494 nbdebug((" invalid buffer identifier in getText\n"));
1495 EMSG("E633: invalid buffer identifier in getText");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 retval = FAIL;
1497 }
1498 else
1499 {
1500 len = get_buf_size(buf->bufp);
1501 nlines = buf->bufp->b_ml.ml_line_count;
1502 text = alloc((unsigned)((len > 0)
1503 ? ((len + nlines) * 2) : 4));
1504 if (text == NULL)
1505 {
1506 nbdebug((" nb_do_cmd: getText has null text field\n"));
1507 retval = FAIL;
1508 }
1509 else
1510 {
1511 p = text;
1512 *p++ = '\"';
1513 for (; lno <= nlines ; lno++)
1514 {
1515 line = nb_quote(ml_get_buf(buf->bufp, lno, FALSE));
1516 if (line != NULL)
1517 {
1518 STRCPY(p, line);
1519 p += STRLEN(line);
1520 *p++ = '\\';
1521 *p++ = 'n';
Bram Moolenaar009b2592004-10-24 19:18:58 +00001522 vim_free(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 }
1525 *p++ = '\"';
1526 *p = '\0';
1527 }
1528 }
1529 if (text == NULL)
1530 nb_reply_text(cmdno, (char_u *)"");
1531 else
1532 {
1533 nb_reply_text(cmdno, text);
1534 vim_free(text);
1535 }
1536/* =====================================================================*/
1537 }
1538 else if (streq((char *)cmd, "remove"))
1539 {
1540 long count;
1541 pos_T first, last;
1542 pos_T *pos;
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001543 pos_T *next;
1544 linenr_T del_from_lnum, del_to_lnum; /* lines to be deleted as a whole */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 int oldFire = netbeansFireChanges;
1546 int oldSuppress = netbeansSuppressNoLines;
1547 int wasChanged;
1548
1549 if (skip >= SKIP_STOP)
1550 {
1551 nbdebug((" Skipping %s command\n", (char *) cmd));
1552 nb_reply_nil(cmdno);
1553 return OK;
1554 }
1555
1556 if (buf == NULL || buf->bufp == NULL)
1557 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001558 nbdebug((" invalid buffer identifier in remove\n"));
1559 EMSG("E634: invalid buffer identifier in remove");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 retval = FAIL;
1561 }
1562 else
1563 {
1564 netbeansFireChanges = FALSE;
1565 netbeansSuppressNoLines = TRUE;
1566
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001567 nb_set_curbuf(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 wasChanged = buf->bufp->b_changed;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001569 cp = (char *)args;
1570 off = strtol(cp, &cp, 10);
1571 count = strtol(cp, &cp, 10);
1572 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 /* delete "count" chars, starting at "off" */
1574 pos = off2pos(buf->bufp, off);
1575 if (!pos)
1576 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001577 nbdebug((" !bad position\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 nb_reply_text(cmdno, (char_u *)"!bad position");
1579 netbeansFireChanges = oldFire;
1580 netbeansSuppressNoLines = oldSuppress;
1581 return FAIL;
1582 }
1583 first = *pos;
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001584 nbdebug((" FIRST POS: line %d, col %d\n",
1585 first.lnum, first.col));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 pos = off2pos(buf->bufp, off+count-1);
1587 if (!pos)
1588 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001589 nbdebug((" !bad count\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 nb_reply_text(cmdno, (char_u *)"!bad count");
1591 netbeansFireChanges = oldFire;
1592 netbeansSuppressNoLines = oldSuppress;
1593 return FAIL;
1594 }
1595 last = *pos;
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001596 nbdebug((" LAST POS: line %d, col %d\n",
1597 last.lnum, last.col));
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001598 del_from_lnum = first.lnum;
1599 del_to_lnum = last.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 doupdate = 1;
1601
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001602 /* Get the position of the first byte after the deleted
1603 * section. "next" is NULL when deleting to the end of the
1604 * file. */
1605 next = off2pos(buf->bufp, off + count);
1606
1607 /* Remove part of the first line. */
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001608 if (first.col != 0
1609 || (next != NULL && first.lnum == next->lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 {
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001611 if (first.lnum != last.lnum
1612 || (next != NULL && first.lnum != next->lnum))
1613 {
1614 /* remove to the end of the first line */
1615 nb_partialremove(first.lnum, first.col,
1616 (colnr_T)MAXCOL);
1617 if (first.lnum == last.lnum)
1618 {
1619 /* Partial line to remove includes the end of
1620 * line. Join the line with the next one, have
1621 * the next line deleted below. */
1622 nb_joinlines(first.lnum, next->lnum);
1623 del_to_lnum = next->lnum;
1624 }
1625 }
1626 else
1627 {
1628 /* remove within one line */
1629 nb_partialremove(first.lnum, first.col, last.col);
1630 }
1631 ++del_from_lnum; /* don't delete the first line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 }
1633
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001634 /* Remove part of the last line. */
1635 if (first.lnum != last.lnum && next != NULL
1636 && next->col != 0 && last.lnum == next->lnum)
1637 {
1638 nb_partialremove(last.lnum, 0, last.col);
1639 if (del_from_lnum > first.lnum)
1640 {
1641 /* Join end of last line to start of first line; last
1642 * line is deleted below. */
1643 nb_joinlines(first.lnum, last.lnum);
1644 }
1645 else
1646 /* First line is deleted as a whole, keep the last
1647 * line. */
1648 --del_to_lnum;
1649 }
1650
1651 /* First is partial line; last line to remove includes
1652 * the end of line; join first line to line following last
1653 * line; line following last line is deleted below. */
1654 if (first.lnum != last.lnum && del_from_lnum > first.lnum
1655 && next != NULL && last.lnum != next->lnum)
1656 {
1657 nb_joinlines(first.lnum, next->lnum);
1658 del_to_lnum = next->lnum;
1659 }
1660
1661 /* Delete whole lines if there are any. */
1662 if (del_to_lnum >= del_from_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 {
1664 int i;
1665
1666 /* delete signs from the lines being deleted */
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001667 for (i = del_from_lnum; i <= del_to_lnum; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 {
1669 int id = buf_findsign_id(buf->bufp, (linenr_T)i);
1670 if (id > 0)
1671 {
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001672 nbdebug((" Deleting sign %d on line %d\n",
1673 id, i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 buf_delsign(buf->bufp, id);
1675 }
1676 else
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001677 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 nbdebug((" No sign on line %d\n", i));
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 }
1681
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001682 nbdebug((" Deleting lines %d through %d\n",
1683 del_from_lnum, del_to_lnum));
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001684 curwin->w_cursor.lnum = del_from_lnum;
1685 curwin->w_cursor.col = 0;
1686 del_lines(del_to_lnum - del_from_lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 }
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001688
1689 /* Leave cursor at first deleted byte. */
1690 curwin->w_cursor = first;
1691 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 buf->bufp->b_changed = wasChanged; /* logically unchanged */
1693 netbeansFireChanges = oldFire;
1694 netbeansSuppressNoLines = oldSuppress;
1695
1696 u_blockfree(buf->bufp);
1697 u_clearall(buf->bufp);
1698 }
1699 nb_reply_nil(cmdno);
1700/* =====================================================================*/
1701 }
1702 else if (streq((char *)cmd, "insert"))
1703 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 char_u *to_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705
1706 if (skip >= SKIP_STOP)
1707 {
1708 nbdebug((" Skipping %s command\n", (char *) cmd));
1709 nb_reply_nil(cmdno);
1710 return OK;
1711 }
1712
1713 /* get offset */
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001714 cp = (char *)args;
1715 off = strtol(cp, &cp, 10);
1716 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717
1718 /* get text to be inserted */
1719 args = skipwhite(args);
1720 args = to_free = (char_u *)nb_unquote(args, NULL);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001721 /*
1722 nbdebug((" CHUNK[%d]: %d bytes at offset %d\n",
1723 buf->bufp->b_ml.ml_line_count, STRLEN(args), off));
1724 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725
1726 if (buf == NULL || buf->bufp == NULL)
1727 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001728 nbdebug((" invalid buffer identifier in insert\n"));
1729 EMSG("E635: invalid buffer identifier in insert");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 retval = FAIL;
1731 }
1732 else if (args != NULL)
1733 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001734 int ff_detected = EOL_UNKNOWN;
1735 int buf_was_empty = (buf->bufp->b_ml.ml_flags & ML_EMPTY);
1736 size_t len = 0;
1737 int added = 0;
1738 int oldFire = netbeansFireChanges;
1739 int old_b_changed;
1740 char_u *nl;
1741 linenr_T lnum;
1742 linenr_T lnum_start;
1743 pos_T *pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 netbeansFireChanges = 0;
1746
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001747 /* Jump to the buffer where we insert. After this "curbuf"
1748 * can be used. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001749 nb_set_curbuf(buf->bufp);
Bram Moolenaara3227e22006-03-08 21:32:40 +00001750 old_b_changed = curbuf->b_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001752 /* Convert the specified character offset into a lnum/col
1753 * position. */
Bram Moolenaara3227e22006-03-08 21:32:40 +00001754 pos = off2pos(curbuf, off);
1755 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001757 if (pos->lnum <= 0)
1758 lnum_start = 1;
1759 else
1760 lnum_start = pos->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 }
1762 else
1763 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001764 /* If the given position is not found, assume we want
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 * the end of the file. See setLocAndSize HACK. */
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001766 if (buf_was_empty)
1767 lnum_start = 1; /* above empty line */
1768 else
1769 lnum_start = curbuf->b_ml.ml_line_count + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001770 }
1771
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001772 /* "lnum" is the line where we insert: either append to it or
1773 * insert a new line above it. */
1774 lnum = lnum_start;
1775
1776 /* Loop over the "\n" separated lines of the argument. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 doupdate = 1;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001778 while (*args != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001780 nl = vim_strchr(args, '\n');
1781 if (nl == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001783 /* Incomplete line, probably truncated. Next "insert"
1784 * command should append to this one. */
1785 len = STRLEN(args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001787 else
1788 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001789 len = nl - args;
1790
1791 /*
1792 * We need to detect EOL style, because the commands
1793 * use a character offset.
1794 */
1795 if (nl > args && nl[-1] == '\r')
1796 {
1797 ff_detected = EOL_DOS;
1798 --len;
1799 }
1800 else
1801 ff_detected = EOL_UNIX;
1802 }
1803 args[len] = NUL;
1804
1805 if (lnum == lnum_start
1806 && ((pos != NULL && pos->col > 0)
1807 || (lnum == 1 && buf_was_empty)))
1808 {
1809 char_u *oldline = ml_get(lnum);
1810 char_u *newline;
1811
1812 /* Insert halfway a line. For simplicity we assume we
1813 * need to append to the line. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001814 newline = alloc_check((unsigned)(STRLEN(oldline) + len + 1));
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001815 if (newline != NULL)
1816 {
1817 STRCPY(newline, oldline);
1818 STRCAT(newline, args);
1819 ml_replace(lnum, newline, FALSE);
1820 }
1821 }
1822 else
1823 {
1824 /* Append a new line. Not that we always do this,
1825 * also when the text doesn't end in a "\n". */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001826 ml_append((linenr_T)(lnum - 1), args, (colnr_T)(len + 1), FALSE);
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001827 ++added;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001828 }
1829
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001830 if (nl == NULL)
1831 break;
1832 ++lnum;
1833 args = nl + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001834 }
1835
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001836 /* Adjust the marks below the inserted lines. */
1837 appended_lines_mark(lnum_start - 1, (long)added);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001839 /*
1840 * When starting with an empty buffer set the fileformat.
1841 * This is just guessing...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 */
1843 if (buf_was_empty)
1844 {
1845 if (ff_detected == EOL_UNKNOWN)
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001846#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 ff_detected = EOL_DOS;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001848#else
1849 ff_detected = EOL_UNIX;
1850#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 set_fileformat(ff_detected, OPT_LOCAL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00001852 curbuf->b_start_ffc = *curbuf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 }
1854
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 /*
1856 * XXX - GRP - Is the next line right? If I've inserted
1857 * text the buffer has been updated but not written. Will
1858 * netbeans guarantee to write it? Even if I do a :q! ?
1859 */
Bram Moolenaara3227e22006-03-08 21:32:40 +00001860 curbuf->b_changed = old_b_changed; /* logically unchanged */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 netbeansFireChanges = oldFire;
1862
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001863 /* Undo info is invalid now... */
Bram Moolenaara3227e22006-03-08 21:32:40 +00001864 u_blockfree(curbuf);
1865 u_clearall(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 }
1867 vim_free(to_free);
1868 nb_reply_nil(cmdno); /* or !error */
1869 }
1870 else
1871 {
1872 nbdebug(("UNIMPLEMENTED FUNCTION: %s\n", cmd));
1873 nb_reply_nil(cmdno);
1874 retval = FAIL;
1875 }
1876 }
1877 else /* Not a function; no reply required. */
1878 {
1879/* =====================================================================*/
1880 if (streq((char *)cmd, "create"))
1881 {
1882 /* Create a buffer without a name. */
1883 if (buf == NULL)
1884 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001885 nbdebug((" invalid buffer identifier in create\n"));
1886 EMSG("E636: invalid buffer identifier in create");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 return FAIL;
1888 }
1889 vim_free(buf->displayname);
1890 buf->displayname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891
1892 netbeansReadFile = 0; /* don't try to open disk file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001893 do_ecmd(0, NULL, 0, 0, ECMD_ONE, ECMD_HIDE + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 netbeansReadFile = 1;
1895 buf->bufp = curbuf;
1896 maketitle();
Bram Moolenaar009b2592004-10-24 19:18:58 +00001897 buf->insertDone = FALSE;
Bram Moolenaar67c53842010-05-22 18:28:27 +02001898#if defined(FEAT_MENU) && defined(FEAT_GUI)
Bram Moolenaard54a6882010-08-09 22:49:00 +02001899 if (gui.in_use)
1900 gui_update_menus(0);
Bram Moolenaar67c53842010-05-22 18:28:27 +02001901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902/* =====================================================================*/
1903 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001904 else if (streq((char *)cmd, "insertDone"))
1905 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001906 if (buf == NULL || buf->bufp == NULL)
1907 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001908 nbdebug((" invalid buffer identifier in insertDone\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001909 }
1910 else
1911 {
1912 buf->bufp->b_start_eol = *args == 'T';
1913 buf->insertDone = TRUE;
1914 args += 2;
1915 buf->bufp->b_p_ro = *args == 'T';
1916 print_read_msg(buf);
1917 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001918/* =====================================================================*/
1919 }
1920 else if (streq((char *)cmd, "saveDone"))
1921 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001922 long savedChars = atol((char *)args);
1923
1924 if (buf == NULL || buf->bufp == NULL)
1925 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001926 nbdebug((" invalid buffer identifier in saveDone\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001927 }
1928 else
1929 print_save_msg(buf, savedChars);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001930/* =====================================================================*/
1931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 else if (streq((char *)cmd, "startDocumentListen"))
1933 {
1934 if (buf == NULL)
1935 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001936 nbdebug((" invalid buffer identifier in startDocumentListen\n"));
1937 EMSG("E637: invalid buffer identifier in startDocumentListen");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 return FAIL;
1939 }
1940 buf->fireChanges = 1;
1941/* =====================================================================*/
1942 }
1943 else if (streq((char *)cmd, "stopDocumentListen"))
1944 {
1945 if (buf == NULL)
1946 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001947 nbdebug((" invalid buffer identifier in stopDocumentListen\n"));
1948 EMSG("E638: invalid buffer identifier in stopDocumentListen");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 return FAIL;
1950 }
1951 buf->fireChanges = 0;
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001952 if (buf->bufp != NULL && buf->bufp->b_was_netbeans_file)
Bram Moolenaar009b2592004-10-24 19:18:58 +00001953 {
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001954 if (!buf->bufp->b_netbeans_file)
Bram Moolenaarf2330482008-06-24 20:19:36 +00001955 {
1956 nbdebug(("E658: NetBeans connection lost for buffer %ld\n", buf->bufp->b_fnum));
Bram Moolenaar009b2592004-10-24 19:18:58 +00001957 EMSGN(_("E658: NetBeans connection lost for buffer %ld"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 buf->bufp->b_fnum);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001959 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001960 else
1961 {
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001962 /* NetBeans uses stopDocumentListen when it stops editing
1963 * a file. It then expects the buffer in Vim to
1964 * disappear. */
1965 do_bufdel(DOBUF_DEL, (char_u *)"", 1,
1966 buf->bufp->b_fnum, buf->bufp->b_fnum, TRUE);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001967 vim_memset(buf, 0, sizeof(nbbuf_T));
1968 }
1969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970/* =====================================================================*/
1971 }
1972 else if (streq((char *)cmd, "setTitle"))
1973 {
1974 if (buf == NULL)
1975 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001976 nbdebug((" invalid buffer identifier in setTitle\n"));
1977 EMSG("E639: invalid buffer identifier in setTitle");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 return FAIL;
1979 }
1980 vim_free(buf->displayname);
1981 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982/* =====================================================================*/
1983 }
1984 else if (streq((char *)cmd, "initDone"))
1985 {
1986 if (buf == NULL || buf->bufp == NULL)
1987 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001988 nbdebug((" invalid buffer identifier in initDone\n"));
1989 EMSG("E640: invalid buffer identifier in initDone");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 return FAIL;
1991 }
1992 doupdate = 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001993 buf->initDone = TRUE;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001994 nb_set_curbuf(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995#if defined(FEAT_AUTOCMD)
1996 apply_autocmds(EVENT_BUFREADPOST, 0, 0, FALSE, buf->bufp);
1997#endif
1998
1999 /* handle any postponed key commands */
2000 handle_key_queue();
2001/* =====================================================================*/
2002 }
2003 else if (streq((char *)cmd, "setBufferNumber")
2004 || streq((char *)cmd, "putBufferNumber"))
2005 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002006 char_u *path;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 buf_T *bufp;
2008
2009 if (buf == NULL)
2010 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002011 nbdebug((" invalid buffer identifier in setBufferNumber\n"));
2012 EMSG("E641: invalid buffer identifier in setBufferNumber");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 return FAIL;
2014 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002015 path = (char_u *)nb_unquote(args, NULL);
2016 if (path == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 return FAIL;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002018 bufp = buflist_findname(path);
2019 vim_free(path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 if (bufp == NULL)
2021 {
Bram Moolenaarec906222009-02-21 21:14:00 +00002022 nbdebug((" File %s not found in setBufferNumber\n", args));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 EMSG2("E642: File %s not found in setBufferNumber", args);
2024 return FAIL;
2025 }
2026 buf->bufp = bufp;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002027 buf->nbbuf_number = bufp->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028
2029 /* "setBufferNumber" has the side effect of jumping to the buffer
2030 * (don't know why!). Don't do that for "putBufferNumber". */
2031 if (*cmd != 'p')
2032 coloncmd(":buffer %d", bufp->b_fnum);
2033 else
2034 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002035 buf->initDone = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036
2037 /* handle any postponed key commands */
2038 handle_key_queue();
2039 }
2040
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041/* =====================================================================*/
2042 }
2043 else if (streq((char *)cmd, "setFullName"))
2044 {
2045 if (buf == NULL)
2046 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002047 nbdebug((" invalid buffer identifier in setFullName\n"));
2048 EMSG("E643: invalid buffer identifier in setFullName");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 return FAIL;
2050 }
2051 vim_free(buf->displayname);
2052 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053
2054 netbeansReadFile = 0; /* don't try to open disk file */
2055 do_ecmd(0, (char_u *)buf->displayname, 0, 0, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002056 ECMD_HIDE + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 netbeansReadFile = 1;
2058 buf->bufp = curbuf;
2059 maketitle();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002060#if defined(FEAT_MENU) && defined(FEAT_GUI)
Bram Moolenaard54a6882010-08-09 22:49:00 +02002061 if (gui.in_use)
2062 gui_update_menus(0);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002063#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064/* =====================================================================*/
2065 }
2066 else if (streq((char *)cmd, "editFile"))
2067 {
2068 if (buf == NULL)
2069 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002070 nbdebug((" invalid buffer identifier in editFile\n"));
2071 EMSG("E644: invalid buffer identifier in editFile");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 return FAIL;
2073 }
2074 /* Edit a file: like create + setFullName + read the file. */
2075 vim_free(buf->displayname);
2076 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 do_ecmd(0, (char_u *)buf->displayname, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002078 ECMD_HIDE + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 buf->bufp = curbuf;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002080 buf->initDone = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 doupdate = 1;
2082#if defined(FEAT_TITLE)
2083 maketitle();
2084#endif
Bram Moolenaar67c53842010-05-22 18:28:27 +02002085#if defined(FEAT_MENU) && defined(FEAT_GUI)
Bram Moolenaard54a6882010-08-09 22:49:00 +02002086 if (gui.in_use)
2087 gui_update_menus(0);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002088#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089/* =====================================================================*/
2090 }
2091 else if (streq((char *)cmd, "setVisible"))
2092 {
2093 if (buf == NULL || buf->bufp == NULL)
2094 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002095 nbdebug((" invalid buffer identifier in setVisible\n"));
2096 /* This message was commented out, probably because it can
2097 * happen when shutting down. */
2098 if (p_verbose > 0)
2099 EMSG("E645: invalid buffer identifier in setVisible");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 return FAIL;
2101 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002102 if (streq((char *)args, "T") && buf->bufp != curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 {
2104 exarg_T exarg;
2105 exarg.cmd = (char_u *)"goto";
2106 exarg.forceit = FALSE;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002107 dosetvisible = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 goto_buffer(&exarg, DOBUF_FIRST, FORWARD, buf->bufp->b_fnum);
2109 doupdate = 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002110 dosetvisible = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111
Bram Moolenaar67c53842010-05-22 18:28:27 +02002112#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 /* Side effect!!!. */
Bram Moolenaard54a6882010-08-09 22:49:00 +02002114 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 gui_mch_set_foreground();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002116#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118/* =====================================================================*/
2119 }
2120 else if (streq((char *)cmd, "raise"))
2121 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002122#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 /* Bring gvim to the foreground. */
Bram Moolenaard54a6882010-08-09 22:49:00 +02002124 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 gui_mch_set_foreground();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002126#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127/* =====================================================================*/
2128 }
2129 else if (streq((char *)cmd, "setModified"))
2130 {
Bram Moolenaarff064e12008-06-09 13:10:45 +00002131 int prev_b_changed;
2132
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 if (buf == NULL || buf->bufp == NULL)
2134 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002135 nbdebug((" invalid buffer identifier in setModified\n"));
2136 /* This message was commented out, probably because it can
2137 * happen when shutting down. */
2138 if (p_verbose > 0)
2139 EMSG("E646: invalid buffer identifier in setModified");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 return FAIL;
2141 }
Bram Moolenaarff064e12008-06-09 13:10:45 +00002142 prev_b_changed = buf->bufp->b_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 if (streq((char *)args, "T"))
Bram Moolenaarff064e12008-06-09 13:10:45 +00002144 buf->bufp->b_changed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 else
2146 {
2147 struct stat st;
2148
2149 /* Assume NetBeans stored the file. Reset the timestamp to
2150 * avoid "file changed" warnings. */
2151 if (buf->bufp->b_ffname != NULL
2152 && mch_stat((char *)buf->bufp->b_ffname, &st) >= 0)
2153 buf_store_time(buf->bufp, &st, buf->bufp->b_ffname);
Bram Moolenaarff064e12008-06-09 13:10:45 +00002154 buf->bufp->b_changed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 }
2156 buf->modified = buf->bufp->b_changed;
Bram Moolenaarff064e12008-06-09 13:10:45 +00002157 if (prev_b_changed != buf->bufp->b_changed)
2158 {
2159#ifdef FEAT_WINDOWS
2160 check_status(buf->bufp);
2161 redraw_tabline = TRUE;
2162#endif
2163#ifdef FEAT_TITLE
2164 maketitle();
2165#endif
2166 update_screen(0);
2167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168/* =====================================================================*/
2169 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002170 else if (streq((char *)cmd, "setModtime"))
2171 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002172 if (buf == NULL || buf->bufp == NULL)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002173 nbdebug((" invalid buffer identifier in setModtime\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002174 else
2175 buf->bufp->b_mtime = atoi((char *)args);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002176/* =====================================================================*/
2177 }
2178 else if (streq((char *)cmd, "setReadOnly"))
2179 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002180 if (buf == NULL || buf->bufp == NULL)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002181 nbdebug((" invalid buffer identifier in setReadOnly\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002182 else if (streq((char *)args, "T"))
Bram Moolenaar009b2592004-10-24 19:18:58 +00002183 buf->bufp->b_p_ro = TRUE;
2184 else
2185 buf->bufp->b_p_ro = FALSE;
2186/* =====================================================================*/
2187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 else if (streq((char *)cmd, "setMark"))
2189 {
2190 /* not yet */
2191/* =====================================================================*/
2192 }
2193 else if (streq((char *)cmd, "showBalloon"))
2194 {
2195#if defined(FEAT_BEVAL)
2196 static char *text = NULL;
2197
2198 /*
2199 * Set up the Balloon Expression Evaluation area.
2200 * Ignore 'ballooneval' here.
2201 * The text pointer must remain valid for a while.
2202 */
2203 if (balloonEval != NULL)
2204 {
2205 vim_free(text);
2206 text = nb_unquote(args, NULL);
2207 if (text != NULL)
2208 gui_mch_post_balloon(balloonEval, (char_u *)text);
2209 }
2210#endif
2211/* =====================================================================*/
2212 }
2213 else if (streq((char *)cmd, "setDot"))
2214 {
2215 pos_T *pos;
2216#ifdef NBDEBUG
2217 char_u *s;
2218#endif
2219
2220 if (buf == NULL || buf->bufp == NULL)
2221 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002222 nbdebug((" invalid buffer identifier in setDot\n"));
2223 EMSG("E647: invalid buffer identifier in setDot");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 return FAIL;
2225 }
2226
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002227 nb_set_curbuf(buf->bufp);
2228
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229#ifdef FEAT_VISUAL
2230 /* Don't want Visual mode now. */
2231 if (VIsual_active)
2232 end_visual_mode();
2233#endif
2234#ifdef NBDEBUG
2235 s = args;
2236#endif
2237 pos = get_off_or_lnum(buf->bufp, &args);
2238 if (pos)
2239 {
2240 curwin->w_cursor = *pos;
2241 check_cursor();
2242#ifdef FEAT_FOLDING
2243 foldOpenCursor();
2244#endif
2245 }
2246 else
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002247 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 nbdebug((" BAD POSITION in setDot: %s\n", s));
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250
2251 /* gui_update_cursor(TRUE, FALSE); */
2252 /* update_curbuf(NOT_VALID); */
2253 update_topline(); /* scroll to show the line */
2254 update_screen(VALID);
2255 setcursor();
2256 out_flush();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002257#ifdef FEAT_GUI
Bram Moolenaard54a6882010-08-09 22:49:00 +02002258 if (gui.in_use)
2259 {
2260 gui_update_cursor(TRUE, FALSE);
2261 gui_mch_flush();
2262 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02002263#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 /* Quit a hit-return or more prompt. */
2265 if (State == HITRETURN || State == ASKMORE)
2266 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267#ifdef FEAT_GUI_GTK
Bram Moolenaard54a6882010-08-09 22:49:00 +02002268 if (gui.in_use && gtk_main_level() > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 gtk_main_quit();
2270#endif
2271 }
2272/* =====================================================================*/
2273 }
2274 else if (streq((char *)cmd, "close"))
2275 {
2276#ifdef NBDEBUG
2277 char *name = "<NONE>";
2278#endif
2279
2280 if (buf == NULL)
2281 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002282 nbdebug((" invalid buffer identifier in close\n"));
2283 EMSG("E648: invalid buffer identifier in close");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 return FAIL;
2285 }
2286
2287#ifdef NBDEBUG
2288 if (buf->displayname != NULL)
2289 name = buf->displayname;
2290#endif
Bram Moolenaarf2330482008-06-24 20:19:36 +00002291 if (buf->bufp == NULL)
2292 {
2293 nbdebug((" invalid buffer identifier in close\n"));
2294 /* This message was commented out, probably because it can
2295 * happen when shutting down. */
2296 if (p_verbose > 0)
2297 EMSG("E649: invalid buffer identifier in close");
2298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 nbdebug((" CLOSE %d: %s\n", bufno, name));
Bram Moolenaar67c53842010-05-22 18:28:27 +02002300#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 need_mouse_correct = TRUE;
Bram Moolenaar67c53842010-05-22 18:28:27 +02002302#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 if (buf->bufp != NULL)
2304 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD,
2305 buf->bufp->b_fnum, TRUE);
Bram Moolenaar8d602722006-08-08 19:34:19 +00002306 buf->bufp = NULL;
2307 buf->initDone = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 doupdate = 1;
2309/* =====================================================================*/
2310 }
2311 else if (streq((char *)cmd, "setStyle")) /* obsolete... */
2312 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002313 nbdebug((" setStyle is obsolete!\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314/* =====================================================================*/
2315 }
2316 else if (streq((char *)cmd, "setExitDelay"))
2317 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002318 /* Only used in version 2.1. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319/* =====================================================================*/
2320 }
2321 else if (streq((char *)cmd, "defineAnnoType"))
2322 {
2323#ifdef FEAT_SIGNS
2324 int typeNum;
2325 char_u *typeName;
2326 char_u *tooltip;
2327 char_u *p;
2328 char_u *glyphFile;
Bram Moolenaar67c53842010-05-22 18:28:27 +02002329 int parse_error = FALSE;
2330 char_u *fg;
2331 char_u *bg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332
2333 if (buf == NULL)
2334 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002335 nbdebug((" invalid buffer identifier in defineAnnoType\n"));
2336 EMSG("E650: invalid buffer identifier in defineAnnoType");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 return FAIL;
2338 }
2339
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002340 cp = (char *)args;
2341 typeNum = strtol(cp, &cp, 10);
2342 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 args = skipwhite(args);
2344 typeName = (char_u *)nb_unquote(args, &args);
2345 args = skipwhite(args + 1);
2346 tooltip = (char_u *)nb_unquote(args, &args);
2347 args = skipwhite(args + 1);
2348
2349 p = (char_u *)nb_unquote(args, &args);
2350 glyphFile = vim_strsave_escaped(p, escape_chars);
2351 vim_free(p);
2352
2353 args = skipwhite(args + 1);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002354 p = skiptowhite(args);
2355 if (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002357 *p = NUL;
2358 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02002360 fg = vim_strsave(args);
2361 bg = vim_strsave(p);
2362 if (STRLEN(fg) > MAX_COLOR_LENGTH || STRLEN(bg) > MAX_COLOR_LENGTH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002364 EMSG("E532: highlighting color name too long in defineAnnoType");
2365 vim_free(typeName);
2366 parse_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02002368 else if (typeName != NULL && tooltip != NULL && glyphFile != NULL)
2369 addsigntype(buf, typeNum, typeName, tooltip, glyphFile, fg, bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 else
2371 vim_free(typeName);
2372
2373 /* don't free typeName; it's used directly in addsigntype() */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002374 vim_free(fg);
2375 vim_free(bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 vim_free(tooltip);
2377 vim_free(glyphFile);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002378 if (parse_error)
2379 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380
2381#endif
2382/* =====================================================================*/
2383 }
2384 else if (streq((char *)cmd, "addAnno"))
2385 {
2386#ifdef FEAT_SIGNS
2387 int serNum;
2388 int localTypeNum;
2389 int typeNum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 pos_T *pos;
2391
2392 if (buf == NULL || buf->bufp == NULL)
2393 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002394 nbdebug((" invalid buffer identifier in addAnno\n"));
2395 EMSG("E651: invalid buffer identifier in addAnno");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 return FAIL;
2397 }
2398
2399 doupdate = 1;
2400
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002401 cp = (char *)args;
2402 serNum = strtol(cp, &cp, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403
2404 /* Get the typenr specific for this buffer and convert it to
2405 * the global typenumber, as used for the sign name. */
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002406 localTypeNum = strtol(cp, &cp, 10);
2407 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 typeNum = mapsigntype(buf, localTypeNum);
2409
2410 pos = get_off_or_lnum(buf->bufp, &args);
2411
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002412 cp = (char *)args;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002413 ignored = (int)strtol(cp, &cp, 10);
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002414 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415# ifdef NBDEBUG
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002416 if (ignored != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002418 nbdebug((" partial line annotation -- Not Yet Implemented!\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 }
2420# endif
2421 if (serNum >= GUARDEDOFFSET)
2422 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002423 nbdebug((" too many annotations! ignoring...\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 return FAIL;
2425 }
2426 if (pos)
2427 {
Bram Moolenaarec906222009-02-21 21:14:00 +00002428 coloncmd(":sign place %d line=%ld name=%d buffer=%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 serNum, pos->lnum, typeNum, buf->bufp->b_fnum);
2430 if (typeNum == curPCtype)
2431 coloncmd(":sign jump %d buffer=%d", serNum,
2432 buf->bufp->b_fnum);
2433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434#endif
2435/* =====================================================================*/
2436 }
2437 else if (streq((char *)cmd, "removeAnno"))
2438 {
2439#ifdef FEAT_SIGNS
2440 int serNum;
2441
2442 if (buf == NULL || buf->bufp == NULL)
2443 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002444 nbdebug((" invalid buffer identifier in removeAnno\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 return FAIL;
2446 }
2447 doupdate = 1;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002448 cp = (char *)args;
2449 serNum = strtol(cp, &cp, 10);
2450 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 coloncmd(":sign unplace %d buffer=%d",
2452 serNum, buf->bufp->b_fnum);
2453 redraw_buf_later(buf->bufp, NOT_VALID);
2454#endif
2455/* =====================================================================*/
2456 }
2457 else if (streq((char *)cmd, "moveAnnoToFront"))
2458 {
2459#ifdef FEAT_SIGNS
Bram Moolenaarf2330482008-06-24 20:19:36 +00002460 nbdebug((" moveAnnoToFront: Not Yet Implemented!\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461#endif
2462/* =====================================================================*/
2463 }
2464 else if (streq((char *)cmd, "guard") || streq((char *)cmd, "unguard"))
2465 {
2466 int len;
2467 pos_T first;
2468 pos_T last;
2469 pos_T *pos;
2470 int un = (cmd[0] == 'u');
2471 static int guardId = GUARDEDOFFSET;
2472
2473 if (skip >= SKIP_STOP)
2474 {
2475 nbdebug((" Skipping %s command\n", (char *) cmd));
2476 return OK;
2477 }
2478
2479 nb_init_graphics();
2480
2481 if (buf == NULL || buf->bufp == NULL)
2482 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002483 nbdebug((" invalid buffer identifier in %s command\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 return FAIL;
2485 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002486 nb_set_curbuf(buf->bufp);
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002487 cp = (char *)args;
2488 off = strtol(cp, &cp, 10);
2489 len = strtol(cp, NULL, 10);
2490 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 pos = off2pos(buf->bufp, off);
2492 doupdate = 1;
2493 if (!pos)
2494 nbdebug((" no such start pos in %s, %ld\n", cmd, off));
2495 else
2496 {
2497 first = *pos;
2498 pos = off2pos(buf->bufp, off + len - 1);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002499 if (pos != NULL && pos->col == 0)
2500 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 /*
2502 * In Java Swing the offset is a position between 2
2503 * characters. If col == 0 then we really want the
2504 * previous line as the end.
2505 */
2506 pos = off2pos(buf->bufp, off + len - 2);
2507 }
2508 if (!pos)
2509 nbdebug((" no such end pos in %s, %ld\n",
2510 cmd, off + len - 1));
2511 else
2512 {
2513 long lnum;
2514 last = *pos;
2515 /* set highlight for region */
2516 nbdebug((" %sGUARD %ld,%d to %ld,%d\n", (un) ? "UN" : "",
2517 first.lnum, first.col,
2518 last.lnum, last.col));
2519#ifdef FEAT_SIGNS
2520 for (lnum = first.lnum; lnum <= last.lnum; lnum++)
2521 {
2522 if (un)
2523 {
2524 /* never used */
2525 }
2526 else
2527 {
2528 if (buf_findsigntype_id(buf->bufp, lnum,
2529 GUARDED) == 0)
2530 {
2531 coloncmd(
Bram Moolenaarec906222009-02-21 21:14:00 +00002532 ":sign place %d line=%ld name=%d buffer=%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 guardId++, lnum, GUARDED,
2534 buf->bufp->b_fnum);
2535 }
2536 }
2537 }
2538#endif
2539 redraw_buf_later(buf->bufp, NOT_VALID);
2540 }
2541 }
2542/* =====================================================================*/
2543 }
2544 else if (streq((char *)cmd, "startAtomic"))
2545 {
2546 inAtomic = 1;
2547/* =====================================================================*/
2548 }
2549 else if (streq((char *)cmd, "endAtomic"))
2550 {
2551 inAtomic = 0;
2552 if (needupdate)
2553 {
2554 doupdate = 1;
2555 needupdate = 0;
2556 }
2557/* =====================================================================*/
2558 }
2559 else if (streq((char *)cmd, "save"))
2560 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002561 /*
2562 * NOTE - This command is obsolete wrt NetBeans. Its left in
2563 * only for historical reasons.
2564 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 if (buf == NULL || buf->bufp == NULL)
2566 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002567 nbdebug((" invalid buffer identifier in %s command\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 return FAIL;
2569 }
2570
2571 /* the following is taken from ex_cmds.c (do_wqall function) */
2572 if (bufIsChanged(buf->bufp))
2573 {
2574 /* Only write if the buffer can be written. */
2575 if (p_write
2576 && !buf->bufp->b_p_ro
2577 && buf->bufp->b_ffname != NULL
2578#ifdef FEAT_QUICKFIX
2579 && !bt_dontwrite(buf->bufp)
2580#endif
2581 )
2582 {
2583 buf_write_all(buf->bufp, FALSE);
2584#ifdef FEAT_AUTOCMD
2585 /* an autocommand may have deleted the buffer */
2586 if (!buf_valid(buf->bufp))
2587 buf->bufp = NULL;
2588#endif
2589 }
2590 }
Bram Moolenaarf2330482008-06-24 20:19:36 +00002591 else
2592 {
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002593 nbdebug((" Buffer has no changes!\n"));
Bram Moolenaarf2330482008-06-24 20:19:36 +00002594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595/* =====================================================================*/
2596 }
2597 else if (streq((char *)cmd, "netbeansBuffer"))
2598 {
2599 if (buf == NULL || buf->bufp == NULL)
2600 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002601 nbdebug((" invalid buffer identifier in %s command\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 return FAIL;
2603 }
2604 if (*args == 'T')
2605 {
2606 buf->bufp->b_netbeans_file = TRUE;
2607 buf->bufp->b_was_netbeans_file = TRUE;
2608 }
2609 else
2610 buf->bufp->b_netbeans_file = FALSE;
2611/* =====================================================================*/
2612 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002613 else if (streq((char *)cmd, "specialKeys"))
2614 {
2615 special_keys(args);
2616/* =====================================================================*/
2617 }
2618 else if (streq((char *)cmd, "actionMenuItem"))
2619 {
2620 /* not used yet */
2621/* =====================================================================*/
2622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 else if (streq((char *)cmd, "version"))
2624 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002625 /* not used yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 }
Bram Moolenaarf2330482008-06-24 20:19:36 +00002627 else
2628 {
2629 nbdebug(("Unrecognised command: %s\n", cmd));
2630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 /*
2632 * Unrecognized command is ignored.
2633 */
2634 }
2635 if (inAtomic && doupdate)
2636 {
2637 needupdate = 1;
2638 doupdate = 0;
2639 }
2640
Bram Moolenaar009b2592004-10-24 19:18:58 +00002641 /*
2642 * Is this needed? I moved the netbeans_Xt_connect() later during startup
2643 * and it may no longer be necessary. If its not needed then needupdate
2644 * and doupdate can also be removed.
2645 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 if (buf != NULL && buf->initDone && doupdate)
2647 {
2648 update_screen(NOT_VALID);
2649 setcursor();
2650 out_flush();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002651#ifdef FEAT_GUI
Bram Moolenaard54a6882010-08-09 22:49:00 +02002652 if (gui.in_use)
2653 {
2654 gui_update_cursor(TRUE, FALSE);
2655 gui_mch_flush();
2656 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02002657#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 /* Quit a hit-return or more prompt. */
2659 if (State == HITRETURN || State == ASKMORE)
2660 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661#ifdef FEAT_GUI_GTK
Bram Moolenaard54a6882010-08-09 22:49:00 +02002662 if (gui.in_use && gtk_main_level() > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 gtk_main_quit();
2664#endif
2665 }
2666 }
2667
2668 return retval;
2669}
2670
2671
2672/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002673 * If "buf" is not the current buffer try changing to a window that edits this
2674 * buffer. If there is no such window then close the current buffer and set
2675 * the current buffer as "buf".
2676 */
2677 static void
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00002678nb_set_curbuf(buf_T *buf)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002679{
2680 if (curbuf != buf && buf_jump_open_win(buf) == NULL)
2681 set_curbuf(buf, DOBUF_GOTO);
2682}
2683
2684/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 * Process a vim colon command.
2686 */
2687 static void
2688coloncmd(char *cmd, ...)
2689{
2690 char buf[1024];
2691 va_list ap;
2692
2693 va_start(ap, cmd);
Bram Moolenaar0dc79e82009-06-24 14:50:12 +00002694 vim_vsnprintf(buf, sizeof(buf), cmd, ap, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 va_end(ap);
2696
2697 nbdebug((" COLONCMD %s\n", buf));
2698
2699/* ALT_INPUT_LOCK_ON; */
2700 do_cmdline((char_u *)buf, NULL, NULL, DOCMD_NOWAIT | DOCMD_KEYTYPED);
2701/* ALT_INPUT_LOCK_OFF; */
2702
2703 setcursor(); /* restore the cursor position */
2704 out_flush(); /* make sure output has been written */
2705
Bram Moolenaar67c53842010-05-22 18:28:27 +02002706#ifdef FEAT_GUI
Bram Moolenaard54a6882010-08-09 22:49:00 +02002707 if (gui.in_use)
2708 {
2709 gui_update_cursor(TRUE, FALSE);
2710 gui_mch_flush();
2711 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02002712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713}
2714
2715
2716/*
Bram Moolenaar009b2592004-10-24 19:18:58 +00002717 * Parse the specialKeys argument and issue the appropriate map commands.
2718 */
2719 static void
2720special_keys(char_u *args)
2721{
2722 char *save_str = nb_unquote(args, NULL);
2723 char *tok = strtok(save_str, " ");
2724 char *sep;
2725 char keybuf[64];
2726 char cmdbuf[256];
2727
2728 while (tok != NULL)
2729 {
2730 int i = 0;
2731
2732 if ((sep = strchr(tok, '-')) != NULL)
2733 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002734 *sep = NUL;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002735 while (*tok)
2736 {
2737 switch (*tok)
2738 {
2739 case 'A':
2740 case 'M':
2741 case 'C':
2742 case 'S':
2743 keybuf[i++] = *tok;
2744 keybuf[i++] = '-';
2745 break;
2746 }
2747 tok++;
2748 }
2749 tok++;
2750 }
2751
2752 strcpy(&keybuf[i], tok);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002753 vim_snprintf(cmdbuf, sizeof(cmdbuf),
2754 "<silent><%s> :nbkey %s<CR>", keybuf, keybuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002755 do_map(0, (char_u *)cmdbuf, NORMAL, FALSE);
2756 tok = strtok(NULL, " ");
2757 }
2758 vim_free(save_str);
2759}
2760
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002761 void
2762ex_nbclose(eap)
2763 exarg_T *eap UNUSED;
2764{
2765 netbeans_close();
2766}
Bram Moolenaar009b2592004-10-24 19:18:58 +00002767
2768 void
2769ex_nbkey(eap)
2770 exarg_T *eap;
2771{
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002772 (void)netbeans_keystring(eap->arg);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002773}
2774
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002775 void
2776ex_nbstart(eap)
2777 exarg_T *eap;
2778{
Bram Moolenaarc2a406b2010-09-30 21:03:26 +02002779#ifdef FEAT_GUI
2780# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar7ad7d012010-11-16 15:49:02 +01002781 && !defined(FEAT_GUI_W32)
Bram Moolenaarc2a406b2010-09-30 21:03:26 +02002782 if (gui.in_use)
2783 {
Bram Moolenaar7ad7d012010-11-16 15:49:02 +01002784 EMSG(_("E838: netbeans is not supported with this GUI"));
2785 return;
Bram Moolenaarc2a406b2010-09-30 21:03:26 +02002786 }
2787# endif
2788#endif
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002789 netbeans_open((char *)eap->arg, FALSE);
2790}
Bram Moolenaar009b2592004-10-24 19:18:58 +00002791
2792/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 * Initialize highlights and signs for use by netbeans (mostly obsolete)
2794 */
2795 static void
2796nb_init_graphics(void)
2797{
2798 static int did_init = FALSE;
2799
2800 if (!did_init)
2801 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002802 coloncmd(":highlight NBGuarded guibg=Cyan guifg=Black"
2803 " ctermbg=LightCyan ctermfg=Black");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 coloncmd(":sign define %d linehl=NBGuarded", GUARDED);
2805
2806 did_init = TRUE;
2807 }
2808}
2809
2810/*
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002811 * Convert key to netbeans name. This uses the global "mod_mask".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 */
2813 static void
2814netbeans_keyname(int key, char *buf)
2815{
2816 char *name = 0;
2817 char namebuf[2];
2818 int ctrl = 0;
2819 int shift = 0;
2820 int alt = 0;
2821
2822 if (mod_mask & MOD_MASK_CTRL)
2823 ctrl = 1;
2824 if (mod_mask & MOD_MASK_SHIFT)
2825 shift = 1;
2826 if (mod_mask & MOD_MASK_ALT)
2827 alt = 1;
2828
2829
2830 switch (key)
2831 {
2832 case K_F1: name = "F1"; break;
2833 case K_S_F1: name = "F1"; shift = 1; break;
2834 case K_F2: name = "F2"; break;
2835 case K_S_F2: name = "F2"; shift = 1; break;
2836 case K_F3: name = "F3"; break;
2837 case K_S_F3: name = "F3"; shift = 1; break;
2838 case K_F4: name = "F4"; break;
2839 case K_S_F4: name = "F4"; shift = 1; break;
2840 case K_F5: name = "F5"; break;
2841 case K_S_F5: name = "F5"; shift = 1; break;
2842 case K_F6: name = "F6"; break;
2843 case K_S_F6: name = "F6"; shift = 1; break;
2844 case K_F7: name = "F7"; break;
2845 case K_S_F7: name = "F7"; shift = 1; break;
2846 case K_F8: name = "F8"; break;
2847 case K_S_F8: name = "F8"; shift = 1; break;
2848 case K_F9: name = "F9"; break;
2849 case K_S_F9: name = "F9"; shift = 1; break;
2850 case K_F10: name = "F10"; break;
2851 case K_S_F10: name = "F10"; shift = 1; break;
2852 case K_F11: name = "F11"; break;
2853 case K_S_F11: name = "F11"; shift = 1; break;
2854 case K_F12: name = "F12"; break;
2855 case K_S_F12: name = "F12"; shift = 1; break;
2856 default:
2857 if (key >= ' ' && key <= '~')
2858 {
2859 /* Allow ASCII characters. */
2860 name = namebuf;
2861 namebuf[0] = key;
2862 namebuf[1] = NUL;
2863 }
2864 else
2865 name = "X";
2866 break;
2867 }
2868
2869 buf[0] = '\0';
2870 if (ctrl)
2871 strcat(buf, "C");
2872 if (shift)
2873 strcat(buf, "S");
2874 if (alt)
2875 strcat(buf, "M"); /* META */
2876 if (ctrl || shift || alt)
2877 strcat(buf, "-");
2878 strcat(buf, name);
2879}
2880
Bram Moolenaar67c53842010-05-22 18:28:27 +02002881#if defined(FEAT_BEVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882/*
2883 * Function to be called for balloon evaluation. Grabs the text under the
2884 * cursor and sends it to the debugger for evaluation. The debugger should
2885 * respond with a showBalloon command when there is a useful result.
2886 */
Bram Moolenaard62bec82005-03-07 22:56:57 +00002887 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888netbeans_beval_cb(
2889 BalloonEval *beval,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002890 int state UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891{
Bram Moolenaard62bec82005-03-07 22:56:57 +00002892 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 char_u *text;
Bram Moolenaard62bec82005-03-07 22:56:57 +00002894 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 int col;
2896 char buf[MAXPATHL * 2 + 25];
2897 char_u *p;
2898
2899 /* Don't do anything when 'ballooneval' is off, messages scrolled the
2900 * windows up or we have no connection. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002901 if (!p_beval || msg_scrolled > 0 || !NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 return;
2903
Bram Moolenaard62bec82005-03-07 22:56:57 +00002904 if (get_beval_info(beval, TRUE, &wp, &lnum, &text, &col) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 {
2906 /* Send debugger request. Only when the text is of reasonable
2907 * length. */
2908 if (text != NULL && text[0] != NUL && STRLEN(text) < MAXPATHL)
2909 {
2910 p = nb_quote(text);
2911 if (p != NULL)
Bram Moolenaar009b2592004-10-24 19:18:58 +00002912 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002913 vim_snprintf(buf, sizeof(buf),
Bram Moolenaar89d40322006-08-29 15:30:07 +00002914 "0:balloonText=%d \"%s\"\n", r_cmdno, p);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002915 vim_free(p);
2916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 nbdebug(("EVT: %s", buf));
2918 nb_send(buf, "netbeans_beval_cb");
2919 }
2920 vim_free(text);
2921 }
2922}
2923#endif
2924
2925/*
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002926 * Return TRUE when the netbeans connection is closed.
2927 */
2928 int
2929netbeans_active(void)
2930{
2931 return NETBEANS_OPEN;
2932}
2933
2934/*
Bram Moolenaar67c53842010-05-22 18:28:27 +02002935 * Return netbeans file descriptor.
2936 */
2937 int
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002938netbeans_filedesc(void)
Bram Moolenaar67c53842010-05-22 18:28:27 +02002939{
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002940 return nbsock;
Bram Moolenaar67c53842010-05-22 18:28:27 +02002941}
2942
2943#if defined(FEAT_GUI) || defined(PROTO)
2944/*
2945 * Register our file descriptor with the gui event handling system.
2946 */
2947 void
2948netbeans_gui_register(void)
2949{
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002950 if (!NB_HAS_GUI || !NETBEANS_OPEN)
Bram Moolenaar67c53842010-05-22 18:28:27 +02002951 return;
2952
Bram Moolenaar173c9852010-09-29 17:27:01 +02002953# ifdef FEAT_GUI_X11
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002954 /* tell notifier we are interested in being called
2955 * when there is input on the editor connection socket
2956 */
2957 if (inputHandler == (XtInputId)NULL)
2958 inputHandler = XtAppAddInput((XtAppContext)app_context, nbsock,
2959 (XtPointer)(XtInputReadMask + XtInputExceptMask),
2960 messageFromNetbeans, NULL);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002961# else
2962# ifdef FEAT_GUI_GTK
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002963 /*
2964 * Tell gdk we are interested in being called when there
2965 * is input on the editor connection socket
2966 */
2967 if (inputHandler == 0)
2968 inputHandler = gdk_input_add((gint)nbsock, (GdkInputCondition)
2969 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
2970 messageFromNetbeans, NULL);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002971# else
2972# ifdef FEAT_GUI_W32
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002973 /*
2974 * Tell Windows we are interested in receiving message when there
2975 * is input on the editor connection socket
2976 */
2977 if (inputHandler == -1)
2978 inputHandler = WSAAsyncSelect(nbsock, s_hwnd, WM_NETBEANS, FD_READ);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002979# endif
2980# endif
2981# endif
Bram Moolenaar67c53842010-05-22 18:28:27 +02002982
2983# ifdef FEAT_BEVAL
2984 bevalServers |= BEVAL_NETBEANS;
2985# endif
2986}
2987#endif
2988
2989/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 * Tell netbeans that the window was opened, ready for commands.
2991 */
2992 void
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02002993netbeans_open(char *params, int doabort)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994{
2995 char *cmd = "0:startupDone=0\n";
2996
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002997 if (NETBEANS_OPEN)
2998 {
2999 EMSG(_("E511: netbeans already connected"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003003 if (netbeans_connect(params, doabort) != OK)
Bram Moolenaar67c53842010-05-22 18:28:27 +02003004 return;
3005#ifdef FEAT_GUI
3006 netbeans_gui_register();
Bram Moolenaard62bec82005-03-07 22:56:57 +00003007#endif
3008
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 nbdebug(("EVT: %s", cmd));
3010 nb_send(cmd, "netbeans_startup_done");
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003011
3012 /* update the screen after having added the gutter */
3013 changed_window_setting();
3014 update_screen(CLEAR);
3015 setcursor();
3016 out_flush();
3017#ifdef FEAT_GUI
Bram Moolenaard54a6882010-08-09 22:49:00 +02003018 if (gui.in_use)
3019 {
3020 gui_update_cursor(TRUE, FALSE);
3021 gui_mch_flush();
3022 }
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003023#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024}
3025
Bram Moolenaar009b2592004-10-24 19:18:58 +00003026/*
3027 * Tell netbeans that we're exiting. This should be called right
3028 * before calling exit.
3029 */
3030 void
3031netbeans_send_disconnect()
3032{
3033 char buf[128];
3034
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003035 if (NETBEANS_OPEN)
Bram Moolenaar009b2592004-10-24 19:18:58 +00003036 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00003037 sprintf(buf, "0:disconnect=%d\n", r_cmdno);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003038 nbdebug(("EVT: %s", buf));
3039 nb_send(buf, "netbeans_disconnect");
3040 }
3041}
3042
Bram Moolenaar173c9852010-09-29 17:27:01 +02003043#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_W32) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044/*
3045 * Tell netbeans that the window was moved or resized.
3046 */
3047 void
3048netbeans_frame_moved(int new_x, int new_y)
3049{
3050 char buf[128];
3051
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003052 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 return;
3054
3055 sprintf(buf, "0:geometry=%d %d %d %d %d\n",
Bram Moolenaar89d40322006-08-29 15:30:07 +00003056 r_cmdno, (int)Columns, (int)Rows, new_x, new_y);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003057 /*nbdebug(("EVT: %s", buf)); happens too many times during a move */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 nb_send(buf, "netbeans_frame_moved");
3059}
3060#endif
3061
3062/*
Bram Moolenaar009b2592004-10-24 19:18:58 +00003063 * Tell netbeans the user opened or activated a file.
3064 */
3065 void
3066netbeans_file_activated(buf_T *bufp)
3067{
3068 int bufno = nb_getbufno(bufp);
3069 nbbuf_T *bp = nb_get_buf(bufno);
3070 char buffer[2*MAXPATHL];
3071 char_u *q;
3072
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003073 if (!NETBEANS_OPEN || !bufp->b_netbeans_file || dosetvisible)
Bram Moolenaar009b2592004-10-24 19:18:58 +00003074 return;
3075
3076 q = nb_quote(bufp->b_ffname);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003077 if (q == NULL || bp == NULL)
Bram Moolenaar009b2592004-10-24 19:18:58 +00003078 return;
3079
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003080 vim_snprintf(buffer, sizeof(buffer), "%d:fileOpened=%d \"%s\" %s %s\n",
Bram Moolenaar009b2592004-10-24 19:18:58 +00003081 bufno,
3082 bufno,
3083 (char *)q,
3084 "T", /* open in NetBeans */
3085 "F"); /* modified */
3086
3087 vim_free(q);
3088 nbdebug(("EVT: %s", buffer));
3089
3090 nb_send(buffer, "netbeans_file_opened");
3091}
3092
3093/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 * Tell netbeans the user opened a file.
3095 */
3096 void
Bram Moolenaar009b2592004-10-24 19:18:58 +00003097netbeans_file_opened(buf_T *bufp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098{
Bram Moolenaar009b2592004-10-24 19:18:58 +00003099 int bufno = nb_getbufno(bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 char buffer[2*MAXPATHL];
3101 char_u *q;
Bram Moolenaar009b2592004-10-24 19:18:58 +00003102 nbbuf_T *bp = nb_get_buf(nb_getbufno(bufp));
3103 int bnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003105 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 return;
3107
Bram Moolenaar009b2592004-10-24 19:18:58 +00003108 q = nb_quote(bufp->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 if (q == NULL)
3110 return;
Bram Moolenaar009b2592004-10-24 19:18:58 +00003111 if (bp != NULL)
3112 bnum = bufno;
3113 else
3114 bnum = 0;
3115
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003116 vim_snprintf(buffer, sizeof(buffer), "%d:fileOpened=%d \"%s\" %s %s\n",
Bram Moolenaar009b2592004-10-24 19:18:58 +00003117 bnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 0,
3119 (char *)q,
3120 "T", /* open in NetBeans */
3121 "F"); /* modified */
3122
3123 vim_free(q);
3124 nbdebug(("EVT: %s", buffer));
3125
3126 nb_send(buffer, "netbeans_file_opened");
Bram Moolenaar009b2592004-10-24 19:18:58 +00003127 if (p_acd && vim_chdirfile(bufp->b_ffname) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 shorten_fnames(TRUE);
3129}
3130
3131/*
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00003132 * Tell netbeans that a file was deleted or wiped out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 */
3134 void
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00003135netbeans_file_killed(buf_T *bufp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136{
3137 int bufno = nb_getbufno(bufp);
3138 nbbuf_T *nbbuf = nb_get_buf(bufno);
3139 char buffer[2*MAXPATHL];
3140
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003141 if (!NETBEANS_OPEN || bufno == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 return;
3143
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00003144 nbdebug(("netbeans_file_killed:\n"));
3145 nbdebug((" Killing bufno: %d", bufno));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146
Bram Moolenaar89d40322006-08-29 15:30:07 +00003147 sprintf(buffer, "%d:killed=%d\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148
3149 nbdebug(("EVT: %s", buffer));
3150
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00003151 nb_send(buffer, "netbeans_file_killed");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152
3153 if (nbbuf != NULL)
3154 nbbuf->bufp = NULL;
3155}
3156
3157/*
3158 * Get a pointer to the Netbeans buffer for Vim buffer "bufp".
3159 * Return NULL if there is no such buffer or changes are not to be reported.
3160 * Otherwise store the buffer number in "*bufnop".
3161 */
3162 static nbbuf_T *
3163nb_bufp2nbbuf_fire(buf_T *bufp, int *bufnop)
3164{
3165 int bufno;
3166 nbbuf_T *nbbuf;
3167
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003168 if (!NETBEANS_OPEN || !netbeansFireChanges)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 return NULL; /* changes are not reported at all */
3170
3171 bufno = nb_getbufno(bufp);
3172 if (bufno <= 0)
3173 return NULL; /* file is not known to NetBeans */
3174
3175 nbbuf = nb_get_buf(bufno);
3176 if (nbbuf != NULL && !nbbuf->fireChanges)
3177 return NULL; /* changes in this buffer are not reported */
3178
3179 *bufnop = bufno;
3180 return nbbuf;
3181}
3182
3183/*
3184 * Tell netbeans the user inserted some text.
3185 */
3186 void
3187netbeans_inserted(
3188 buf_T *bufp,
3189 linenr_T linenr,
3190 colnr_T col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 char_u *txt,
3192 int newlen)
3193{
3194 char_u *buf;
3195 int bufno;
3196 nbbuf_T *nbbuf;
3197 pos_T pos;
3198 long off;
3199 char_u *p;
3200 char_u *newtxt;
3201
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003202 if (!NETBEANS_OPEN)
3203 return;
3204
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3206 if (nbbuf == NULL)
3207 return;
3208
Bram Moolenaar009b2592004-10-24 19:18:58 +00003209 /* Don't mark as modified for initial read */
3210 if (nbbuf->insertDone)
3211 nbbuf->modified = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212
3213 pos.lnum = linenr;
3214 pos.col = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 off = pos2off(bufp, &pos);
3216
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 /* send the "insert" EVT */
3218 newtxt = alloc(newlen + 1);
Bram Moolenaarb6356332005-07-18 21:40:44 +00003219 vim_strncpy(newtxt, txt, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 p = nb_quote(newtxt);
3221 if (p != NULL)
3222 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00003223 buf = alloc(128 + 2*newlen);
Bram Moolenaar89d40322006-08-29 15:30:07 +00003224 sprintf((char *)buf, "%d:insert=%d %ld \"%s\"\n",
3225 bufno, r_cmdno, off, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 nbdebug(("EVT: %s", buf));
3227 nb_send((char *)buf, "netbeans_inserted");
Bram Moolenaar009b2592004-10-24 19:18:58 +00003228 vim_free(p);
3229 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 vim_free(newtxt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232}
3233
3234/*
3235 * Tell netbeans some bytes have been removed.
3236 */
3237 void
3238netbeans_removed(
3239 buf_T *bufp,
3240 linenr_T linenr,
3241 colnr_T col,
3242 long len)
3243{
3244 char_u buf[128];
3245 int bufno;
3246 nbbuf_T *nbbuf;
3247 pos_T pos;
3248 long off;
3249
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003250 if (!NETBEANS_OPEN)
3251 return;
3252
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3254 if (nbbuf == NULL)
3255 return;
3256
3257 if (len < 0)
3258 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00003259 nbdebug(("Negative len %ld in netbeans_removed()!\n", len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 return;
3261 }
3262
3263 nbbuf->modified = 1;
3264
3265 pos.lnum = linenr;
3266 pos.col = col;
3267
3268 off = pos2off(bufp, &pos);
3269
Bram Moolenaar89d40322006-08-29 15:30:07 +00003270 sprintf((char *)buf, "%d:remove=%d %ld %ld\n", bufno, r_cmdno, off, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 nbdebug(("EVT: %s", buf));
3272 nb_send((char *)buf, "netbeans_removed");
3273}
3274
3275/*
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003276 * Send netbeans an unmodified command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003279netbeans_unmodified(buf_T *bufp UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280{
Bram Moolenaar09092152010-08-08 16:38:42 +02003281 /* This is a no-op, because NetBeans considers a buffer modified
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 * even when all changes have been undone. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283}
3284
3285/*
3286 * Send a button release event back to netbeans. Its up to netbeans
3287 * to decide what to do (if anything) with this event.
3288 */
3289 void
3290netbeans_button_release(int button)
3291{
3292 char buf[128];
3293 int bufno;
3294
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003295 if (!NETBEANS_OPEN)
3296 return;
3297
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 bufno = nb_getbufno(curbuf);
3299
3300 if (bufno >= 0 && curwin != NULL && curwin->w_buffer == curbuf)
3301 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003302 int col = mouse_col - W_WINCOL(curwin)
Bram Moolenaar67c53842010-05-22 18:28:27 +02003303 - ((curwin->w_p_nu || curwin->w_p_rnu) ? 9 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 long off = pos2off(curbuf, &curwin->w_cursor);
3305
3306 /* sync the cursor position */
Bram Moolenaar89d40322006-08-29 15:30:07 +00003307 sprintf(buf, "%d:newDotAndMark=%d %ld %ld\n", bufno, r_cmdno, off, off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 nbdebug(("EVT: %s", buf));
3309 nb_send(buf, "netbeans_button_release[newDotAndMark]");
3310
Bram Moolenaar89d40322006-08-29 15:30:07 +00003311 sprintf(buf, "%d:buttonRelease=%d %d %ld %d\n", bufno, r_cmdno,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 button, (long)curwin->w_cursor.lnum, col);
3313 nbdebug(("EVT: %s", buf));
3314 nb_send(buf, "netbeans_button_release");
3315 }
3316}
3317
3318
3319/*
Bram Moolenaar143c38c2007-05-10 16:41:10 +00003320 * Send a keypress event back to netbeans. This usually simulates some
Bram Moolenaar009b2592004-10-24 19:18:58 +00003321 * kind of function key press. This function operates on a key code.
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003322 * Return TRUE when the key was sent, FALSE when the command has been
3323 * postponed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 */
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003325 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326netbeans_keycommand(int key)
3327{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 char keyName[60];
Bram Moolenaar009b2592004-10-24 19:18:58 +00003329
3330 netbeans_keyname(key, keyName);
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003331 return netbeans_keystring((char_u *)keyName);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003332}
3333
3334
3335/*
Bram Moolenaar143c38c2007-05-10 16:41:10 +00003336 * Send a keypress event back to netbeans. This usually simulates some
Bram Moolenaar009b2592004-10-24 19:18:58 +00003337 * kind of function key press. This function operates on a key string.
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003338 * Return TRUE when the key was sent, FALSE when the command has been
3339 * postponed.
Bram Moolenaar009b2592004-10-24 19:18:58 +00003340 */
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003341 static int
3342netbeans_keystring(char_u *keyName)
Bram Moolenaar009b2592004-10-24 19:18:58 +00003343{
3344 char buf[2*MAXPATHL];
3345 int bufno = nb_getbufno(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 long off;
3347 char_u *q;
3348
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003349 if (!NETBEANS_OPEN)
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003350 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 if (bufno == -1)
3353 {
3354 nbdebug(("got keycommand for non-NetBeans buffer, opening...\n"));
3355 q = curbuf->b_ffname == NULL ? (char_u *)""
3356 : nb_quote(curbuf->b_ffname);
3357 if (q == NULL)
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003358 return TRUE;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003359 vim_snprintf(buf, sizeof(buf), "0:fileOpened=%d \"%s\" %s %s\n", 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 q,
3361 "T", /* open in NetBeans */
3362 "F"); /* modified */
3363 if (curbuf->b_ffname != NULL)
3364 vim_free(q);
3365 nbdebug(("EVT: %s", buf));
3366 nb_send(buf, "netbeans_keycommand");
3367
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003368 postpone_keycommand(keyName);
3369 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 }
3371
3372 /* sync the cursor position */
3373 off = pos2off(curbuf, &curwin->w_cursor);
Bram Moolenaar89d40322006-08-29 15:30:07 +00003374 sprintf(buf, "%d:newDotAndMark=%d %ld %ld\n", bufno, r_cmdno, off, off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 nbdebug(("EVT: %s", buf));
3376 nb_send(buf, "netbeans_keycommand");
3377
3378 /* To work on Win32 you must apply patch to ExtEditor module
3379 * from ExtEdCaret.java.diff - make EVT_newDotAndMark handler
3380 * more synchronous
3381 */
3382
3383 /* now send keyCommand event */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003384 vim_snprintf(buf, sizeof(buf), "%d:keyCommand=%d \"%s\"\n",
Bram Moolenaar89d40322006-08-29 15:30:07 +00003385 bufno, r_cmdno, keyName);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 nbdebug(("EVT: %s", buf));
3387 nb_send(buf, "netbeans_keycommand");
3388
3389 /* New: do both at once and include the lnum/col. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003390 vim_snprintf(buf, sizeof(buf), "%d:keyAtPos=%d \"%s\" %ld %ld/%ld\n",
Bram Moolenaar89d40322006-08-29 15:30:07 +00003391 bufno, r_cmdno, keyName,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 off, (long)curwin->w_cursor.lnum, (long)curwin->w_cursor.col);
3393 nbdebug(("EVT: %s", buf));
3394 nb_send(buf, "netbeans_keycommand");
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003395 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396}
3397
3398
3399/*
3400 * Send a save event to netbeans.
3401 */
3402 void
3403netbeans_save_buffer(buf_T *bufp)
3404{
3405 char_u buf[64];
3406 int bufno;
3407 nbbuf_T *nbbuf;
3408
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003409 if (!NETBEANS_OPEN)
3410 return;
3411
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3413 if (nbbuf == NULL)
3414 return;
3415
3416 nbbuf->modified = 0;
3417
Bram Moolenaar89d40322006-08-29 15:30:07 +00003418 sprintf((char *)buf, "%d:save=%d\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 nbdebug(("EVT: %s", buf));
3420 nb_send((char *)buf, "netbeans_save_buffer");
3421}
3422
3423
3424/*
3425 * Send remove command to netbeans (this command has been turned off).
3426 */
3427 void
3428netbeans_deleted_all_lines(buf_T *bufp)
3429{
3430 char_u buf[64];
3431 int bufno;
3432 nbbuf_T *nbbuf;
3433
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003434 if (!NETBEANS_OPEN)
3435 return;
3436
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3438 if (nbbuf == NULL)
3439 return;
3440
Bram Moolenaar009b2592004-10-24 19:18:58 +00003441 /* Don't mark as modified for initial read */
3442 if (nbbuf->insertDone)
3443 nbbuf->modified = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444
Bram Moolenaar89d40322006-08-29 15:30:07 +00003445 sprintf((char *)buf, "%d:remove=%d 0 -1\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 nbdebug(("EVT(suppressed): %s", buf));
3447/* nb_send(buf, "netbeans_deleted_all_lines"); */
3448}
3449
3450
3451/*
3452 * See if the lines are guarded. The top and bot parameters are from
3453 * u_savecommon(), these are the line above the change and the line below the
3454 * change.
3455 */
3456 int
3457netbeans_is_guarded(linenr_T top, linenr_T bot)
3458{
3459 signlist_T *p;
3460 int lnum;
3461
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003462 if (!NETBEANS_OPEN)
3463 return FALSE;
3464
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 for (p = curbuf->b_signlist; p != NULL; p = p->next)
3466 if (p->id >= GUARDEDOFFSET)
3467 for (lnum = top + 1; lnum < bot; lnum++)
3468 if (lnum == p->lnum)
3469 return TRUE;
3470
3471 return FALSE;
3472}
3473
Bram Moolenaar173c9852010-09-29 17:27:01 +02003474#if defined(FEAT_GUI_X11) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475/*
3476 * We have multiple signs to draw at the same location. Draw the
3477 * multi-sign indicator instead. This is the Motif version.
3478 */
3479 void
3480netbeans_draw_multisign_indicator(int row)
3481{
3482 int i;
3483 int y;
3484 int x;
3485
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003486 if (!NETBEANS_OPEN)
3487 return;
3488
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 x = 0;
3490 y = row * gui.char_height + 2;
3491
3492 for (i = 0; i < gui.char_height - 3; i++)
3493 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y++);
3494
3495 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+0, y);
3496 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3497 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+4, y++);
3498 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+1, y);
3499 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3500 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+3, y++);
3501 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3502}
Bram Moolenaar173c9852010-09-29 17:27:01 +02003503#endif /* FEAT_GUI_X11 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504
Bram Moolenaar64404472010-06-26 06:24:45 +02003505#if defined(FEAT_GUI_GTK) && !defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506/*
3507 * We have multiple signs to draw at the same location. Draw the
3508 * multi-sign indicator instead. This is the GTK/Gnome version.
3509 */
3510 void
3511netbeans_draw_multisign_indicator(int row)
3512{
3513 int i;
3514 int y;
3515 int x;
3516 GdkDrawable *drawable = gui.drawarea->window;
3517
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003518 if (!NETBEANS_OPEN)
3519 return;
3520
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 x = 0;
3522 y = row * gui.char_height + 2;
3523
3524 for (i = 0; i < gui.char_height - 3; i++)
3525 gdk_draw_point(drawable, gui.text_gc, x+2, y++);
3526
3527 gdk_draw_point(drawable, gui.text_gc, x+0, y);
3528 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3529 gdk_draw_point(drawable, gui.text_gc, x+4, y++);
3530 gdk_draw_point(drawable, gui.text_gc, x+1, y);
3531 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3532 gdk_draw_point(drawable, gui.text_gc, x+3, y++);
3533 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3534}
3535#endif /* FEAT_GUI_GTK */
3536
3537/*
3538 * If the mouse is clicked in the gutter of a line with multiple
3539 * annotations, cycle through the set of signs.
3540 */
3541 void
3542netbeans_gutter_click(linenr_T lnum)
3543{
3544 signlist_T *p;
3545
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003546 if (!NETBEANS_OPEN)
3547 return;
3548
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 for (p = curbuf->b_signlist; p != NULL; p = p->next)
3550 {
3551 if (p->lnum == lnum && p->next && p->next->lnum == lnum)
3552 {
3553 signlist_T *tail;
3554
3555 /* remove "p" from list, reinsert it at the tail of the sublist */
3556 if (p->prev)
3557 p->prev->next = p->next;
3558 else
3559 curbuf->b_signlist = p->next;
3560 p->next->prev = p->prev;
3561 /* now find end of sublist and insert p */
3562 for (tail = p->next;
3563 tail->next && tail->next->lnum == lnum
3564 && tail->next->id < GUARDEDOFFSET;
3565 tail = tail->next)
3566 ;
3567 /* tail now points to last entry with same lnum (except
3568 * that "guarded" annotations are always last) */
3569 p->next = tail->next;
3570 if (tail->next)
3571 tail->next->prev = p;
3572 p->prev = tail;
3573 tail->next = p;
3574 update_debug_sign(curbuf, lnum);
3575 break;
3576 }
3577 }
3578}
3579
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580/*
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003581 * Add a sign of the requested type at the requested location.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 *
3583 * Reverse engineering:
3584 * Apparently an annotation is defined the first time it is used in a buffer.
3585 * When the same annotation is used in two buffers, the second time we do not
3586 * need to define a new sign name but reuse the existing one. But since the
3587 * ID number used in the second buffer starts counting at one again, a mapping
3588 * is made from the ID specifically for the buffer to the global sign name
3589 * (which is a number).
3590 *
3591 * globalsignmap[] stores the signs that have been defined globally.
3592 * buf->signmapused[] maps buffer-local annotation IDs to an index in
3593 * globalsignmap[].
3594 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 static void
3596addsigntype(
3597 nbbuf_T *buf,
3598 int typeNum,
3599 char_u *typeName,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003600 char_u *tooltip UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 char_u *glyphFile,
Bram Moolenaar67c53842010-05-22 18:28:27 +02003602 char_u *fg,
3603 char_u *bg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 int i, j;
Bram Moolenaar67c53842010-05-22 18:28:27 +02003606 int use_fg = (*fg && STRCMP(fg, "none") != 0);
3607 int use_bg = (*bg && STRCMP(bg, "none") != 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608
3609 for (i = 0; i < globalsignmapused; i++)
3610 if (STRCMP(typeName, globalsignmap[i]) == 0)
3611 break;
3612
3613 if (i == globalsignmapused) /* not found; add it to global map */
3614 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003615 nbdebug(("DEFINEANNOTYPE(%d,%s,%s,%s,%s,%s)\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 typeNum, typeName, tooltip, glyphFile, fg, bg));
3617 if (use_fg || use_bg)
3618 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003619 char fgbuf[2 * (8 + MAX_COLOR_LENGTH) + 1];
3620 char bgbuf[2 * (8 + MAX_COLOR_LENGTH) + 1];
3621 char *ptr;
3622 int value;
3623
3624 value = strtol((char *)fg, &ptr, 10);
3625 if (ptr != (char *)fg)
3626 sprintf(fgbuf, "guifg=#%06x", value & 0xFFFFFF);
3627 else
3628 sprintf(fgbuf, "guifg=%s ctermfg=%s", fg, fg);
3629
3630 value = strtol((char *)bg, &ptr, 10);
3631 if (ptr != (char *)bg)
3632 sprintf(bgbuf, "guibg=#%06x", value & 0xFFFFFF);
3633 else
3634 sprintf(bgbuf, "guibg=%s ctermbg=%s", bg, bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635
3636 coloncmd(":highlight NB_%s %s %s", typeName, (use_fg) ? fgbuf : "",
3637 (use_bg) ? bgbuf : "");
3638 if (*glyphFile == NUL)
3639 /* no glyph, line highlighting only */
3640 coloncmd(":sign define %d linehl=NB_%s", i + 1, typeName);
3641 else if (vim_strsize(glyphFile) <= 2)
3642 /* one- or two-character glyph name, use as text glyph with
3643 * texthl */
3644 coloncmd(":sign define %d text=%s texthl=NB_%s", i + 1,
3645 glyphFile, typeName);
3646 else
3647 /* glyph, line highlighting */
3648 coloncmd(":sign define %d icon=%s linehl=NB_%s", i + 1,
3649 glyphFile, typeName);
3650 }
3651 else
3652 /* glyph, no line highlighting */
3653 coloncmd(":sign define %d icon=%s", i + 1, glyphFile);
3654
3655 if (STRCMP(typeName,"CurrentPC") == 0)
3656 curPCtype = typeNum;
3657
3658 if (globalsignmapused == globalsignmaplen)
3659 {
3660 if (globalsignmaplen == 0) /* first allocation */
3661 {
3662 globalsignmaplen = 20;
3663 globalsignmap = (char **)alloc_clear(globalsignmaplen*sizeof(char *));
3664 }
3665 else /* grow it */
3666 {
3667 int incr;
3668 int oldlen = globalsignmaplen;
3669
3670 globalsignmaplen *= 2;
3671 incr = globalsignmaplen - oldlen;
3672 globalsignmap = (char **)vim_realloc(globalsignmap,
3673 globalsignmaplen * sizeof(char *));
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003674 vim_memset(globalsignmap + oldlen, 0, incr * sizeof(char *));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 }
3676 }
3677
3678 globalsignmap[i] = (char *)typeName;
3679 globalsignmapused = i + 1;
3680 }
3681
3682 /* check local map; should *not* be found! */
3683 for (j = 0; j < buf->signmapused; j++)
3684 if (buf->signmap[j] == i + 1)
3685 return;
3686
3687 /* add to local map */
3688 if (buf->signmapused == buf->signmaplen)
3689 {
3690 if (buf->signmaplen == 0) /* first allocation */
3691 {
3692 buf->signmaplen = 5;
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003693 buf->signmap = (int *)alloc_clear(buf->signmaplen * sizeof(int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 }
3695 else /* grow it */
3696 {
3697 int incr;
3698 int oldlen = buf->signmaplen;
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003699
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 buf->signmaplen *= 2;
3701 incr = buf->signmaplen - oldlen;
3702 buf->signmap = (int *)vim_realloc(buf->signmap,
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003703 buf->signmaplen * sizeof(int));
3704 vim_memset(buf->signmap + oldlen, 0, incr * sizeof(int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 }
3706 }
3707
3708 buf->signmap[buf->signmapused++] = i + 1;
3709
3710}
3711
3712
3713/*
3714 * See if we have the requested sign type in the buffer.
3715 */
3716 static int
3717mapsigntype(nbbuf_T *buf, int localsigntype)
3718{
3719 if (--localsigntype >= 0 && localsigntype < buf->signmapused)
3720 return buf->signmap[localsigntype];
3721
3722 return 0;
3723}
3724
3725
3726/*
3727 * Compute length of buffer, don't print anything.
3728 */
3729 static long
3730get_buf_size(buf_T *bufp)
3731{
3732 linenr_T lnum;
3733 long char_count = 0;
3734 int eol_size;
3735 long last_check = 100000L;
3736
3737 if (bufp->b_ml.ml_flags & ML_EMPTY)
3738 return 0;
3739 else
3740 {
3741 if (get_fileformat(bufp) == EOL_DOS)
3742 eol_size = 2;
3743 else
3744 eol_size = 1;
3745 for (lnum = 1; lnum <= bufp->b_ml.ml_line_count; ++lnum)
3746 {
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00003747 char_count += (long)STRLEN(ml_get_buf(bufp, lnum, FALSE))
3748 + eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 /* Check for a CTRL-C every 100000 characters */
3750 if (char_count > last_check)
3751 {
3752 ui_breakcheck();
3753 if (got_int)
3754 return char_count;
3755 last_check = char_count + 100000L;
3756 }
3757 }
3758 /* Correction for when last line doesn't have an EOL. */
3759 if (!bufp->b_p_eol && bufp->b_p_bin)
3760 char_count -= eol_size;
3761 }
3762
3763 return char_count;
3764}
3765
3766/*
3767 * Convert character offset to lnum,col
3768 */
3769 static pos_T *
3770off2pos(buf_T *buf, long offset)
3771{
3772 linenr_T lnum;
3773 static pos_T pos;
3774
3775 pos.lnum = 0;
3776 pos.col = 0;
3777#ifdef FEAT_VIRTUALEDIT
3778 pos.coladd = 0;
3779#endif
3780
3781 if (!(buf->b_ml.ml_flags & ML_EMPTY))
3782 {
3783 if ((lnum = ml_find_line_or_offset(buf, (linenr_T)0, &offset)) < 0)
3784 return NULL;
3785 pos.lnum = lnum;
3786 pos.col = offset;
3787 }
3788
3789 return &pos;
3790}
3791
3792/*
3793 * Convert an argument in the form "1234" to an offset and compute the
3794 * lnum/col from it. Convert an argument in the form "123/12" directly to a
3795 * lnum/col.
3796 * "argp" is advanced to after the argument.
3797 * Return a pointer to the position, NULL if something is wrong.
3798 */
3799 static pos_T *
3800get_off_or_lnum(buf_T *buf, char_u **argp)
3801{
3802 static pos_T mypos;
3803 long off;
3804
3805 off = strtol((char *)*argp, (char **)argp, 10);
3806 if (**argp == '/')
3807 {
3808 mypos.lnum = (linenr_T)off;
3809 ++*argp;
3810 mypos.col = strtol((char *)*argp, (char **)argp, 10);
3811#ifdef FEAT_VIRTUALEDIT
3812 mypos.coladd = 0;
3813#endif
3814 return &mypos;
3815 }
3816 return off2pos(buf, off);
3817}
3818
3819
3820/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003821 * Convert (lnum,col) to byte offset in the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 */
3823 static long
3824pos2off(buf_T *buf, pos_T *pos)
3825{
3826 long offset = 0;
3827
3828 if (!(buf->b_ml.ml_flags & ML_EMPTY))
3829 {
3830 if ((offset = ml_find_line_or_offset(buf, pos->lnum, 0)) < 0)
3831 return 0;
3832 offset += pos->col;
3833 }
3834
3835 return offset;
3836}
3837
3838
Bram Moolenaar009b2592004-10-24 19:18:58 +00003839/*
3840 * This message is printed after NetBeans opens a new file. Its
3841 * similar to the message readfile() uses, but since NetBeans
3842 * doesn't normally call readfile, we do our own.
3843 */
3844 static void
3845print_read_msg(buf)
3846 nbbuf_T *buf;
3847{
3848 int lnum = buf->bufp->b_ml.ml_line_count;
Bram Moolenaar914703b2010-05-31 21:59:46 +02003849 off_t nchars = buf->bufp->b_orig_size;
Bram Moolenaar009b2592004-10-24 19:18:58 +00003850 char_u c;
3851
3852 msg_add_fname(buf->bufp, buf->bufp->b_ffname);
3853 c = FALSE;
3854
3855 if (buf->bufp->b_p_ro)
3856 {
3857 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]"));
3858 c = TRUE;
3859 }
3860 if (!buf->bufp->b_start_eol)
3861 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003862 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]")
3863 : _("[Incomplete last line]"));
Bram Moolenaar009b2592004-10-24 19:18:58 +00003864 c = TRUE;
3865 }
3866 msg_add_lines(c, (long)lnum, nchars);
3867
3868 /* Now display it */
3869 vim_free(keep_msg);
3870 keep_msg = NULL;
3871 msg_scrolled_ign = TRUE;
3872 msg_trunc_attr(IObuff, FALSE, 0);
3873 msg_scrolled_ign = FALSE;
3874}
3875
3876
3877/*
Bram Moolenaar67c53842010-05-22 18:28:27 +02003878 * Print a message after NetBeans writes the file. This message should be
3879 * identical to the standard message a non-netbeans user would see when
3880 * writing a file.
Bram Moolenaar009b2592004-10-24 19:18:58 +00003881 */
3882 static void
3883print_save_msg(buf, nchars)
3884 nbbuf_T *buf;
Bram Moolenaar914703b2010-05-31 21:59:46 +02003885 off_t nchars;
Bram Moolenaar009b2592004-10-24 19:18:58 +00003886{
3887 char_u c;
3888 char_u *p;
3889
3890 if (nchars >= 0)
3891 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003892 /* put fname in IObuff with quotes */
3893 msg_add_fname(buf->bufp, buf->bufp->b_ffname);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003894 c = FALSE;
3895
3896 msg_add_lines(c, buf->bufp->b_ml.ml_line_count,
Bram Moolenaar914703b2010-05-31 21:59:46 +02003897 buf->bufp->b_orig_size);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003898
3899 vim_free(keep_msg);
3900 keep_msg = NULL;
3901 msg_scrolled_ign = TRUE;
3902 p = msg_trunc_attr(IObuff, FALSE, 0);
3903 if ((msg_scrolled && !need_wait_return) || !buf->initDone)
3904 {
3905 /* Need to repeat the message after redrawing when:
3906 * - When reading from stdin (the screen will be cleared next).
3907 * - When restart_edit is set (otherwise there will be a delay
3908 * before redrawing).
3909 * - When the screen was scrolled but there is no wait-return
3910 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003911 set_keep_msg(p, 0);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003912 }
3913 msg_scrolled_ign = FALSE;
3914 /* add_to_input_buf((char_u *)"\f", 1); */
3915 }
3916 else
3917 {
3918 char_u ebuf[BUFSIZ];
3919
3920 STRCPY(ebuf, (char_u *)_("E505: "));
3921 STRCAT(ebuf, IObuff);
3922 STRCAT(ebuf, (char_u *)_("is read-only (add ! to override)"));
3923 STRCPY(IObuff, ebuf);
Bram Moolenaarf2330482008-06-24 20:19:36 +00003924 nbdebug((" %s\n", ebuf ));
Bram Moolenaar009b2592004-10-24 19:18:58 +00003925 emsg(IObuff);
3926 }
3927}
3928
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929#endif /* defined(FEAT_NETBEANS_INTG) */