blob: 853df364affc393a6afc9d51c208b2bc969c5b0c [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;
Bram Moolenaar863053d2010-12-02 17:09:54 +0100646 int own_node;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647
Bram Moolenaarf2330482008-06-24 20:19:36 +0000648 while (head.next != NULL && head.next != &head)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 {
650 node = head.next;
651
652 /* Locate the first line in the first buffer. */
653 p = vim_strchr(node->buffer, '\n');
654 if (p == NULL)
655 {
656 /* Command isn't complete. If there is no following buffer,
657 * return (wait for more). If there is another buffer following,
658 * prepend the text to that buffer and delete this one. */
659 if (node->next == &head)
660 return;
Bram Moolenaarf2330482008-06-24 20:19:36 +0000661 p = alloc((unsigned)(STRLEN(node->buffer)
662 + STRLEN(node->next->buffer) + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 if (p == NULL)
664 return; /* out of memory */
665 STRCPY(p, node->buffer);
666 STRCAT(p, node->next->buffer);
667 vim_free(node->next->buffer);
668 node->next->buffer = p;
669
670 /* dispose of the node and buffer */
671 head.next = node->next;
672 node->next->prev = node->prev;
673 vim_free(node->buffer);
674 vim_free(node);
675 }
676 else
677 {
678 /* There is a complete command at the start of the buffer.
679 * Terminate it with a NUL. When no more text is following unlink
680 * the buffer. Do this before executing, because new buffers can
681 * be added while busy handling the command. */
682 *p++ = NUL;
683 if (*p == NUL)
684 {
Bram Moolenaar863053d2010-12-02 17:09:54 +0100685 own_node = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 head.next = node->next;
687 node->next->prev = node->prev;
688 }
Bram Moolenaar863053d2010-12-02 17:09:54 +0100689 else
690 own_node = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691
692 /* now, parse and execute the commands */
693 nb_parse_cmd(node->buffer);
694
Bram Moolenaar863053d2010-12-02 17:09:54 +0100695 if (own_node)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 {
697 /* buffer finished, dispose of the node and buffer */
698 vim_free(node->buffer);
699 vim_free(node);
700 }
Bram Moolenaar863053d2010-12-02 17:09:54 +0100701 /* Check that "head" wasn't changed under our fingers, e.g. when a
702 * DETACH command was handled. */
703 else if (head.next == node)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 {
705 /* more follows, move to the start */
Bram Moolenaarf2330482008-06-24 20:19:36 +0000706 STRMOVE(node->buffer, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 }
708 }
709 }
710}
711
712/* Buffer size for reading incoming messages. */
713#define MAXMSGSIZE 4096
714
715/*
Bram Moolenaar67c53842010-05-22 18:28:27 +0200716 * Read a command from netbeans.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 */
Bram Moolenaar173c9852010-09-29 17:27:01 +0200718#ifdef FEAT_GUI_X11
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 static void
Bram Moolenaara9d45512009-05-17 21:25:42 +0000720messageFromNetbeans(XtPointer clientData UNUSED,
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000721 int *unused1 UNUSED,
722 XtInputId *unused2 UNUSED)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200723{
724 netbeans_read();
725}
726#endif
727
728#ifdef FEAT_GUI_GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000730messageFromNetbeans(gpointer clientData UNUSED,
731 gint unused1 UNUSED,
732 GdkInputCondition unused2 UNUSED)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200733{
734 netbeans_read();
735}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736#endif
Bram Moolenaar67c53842010-05-22 18:28:27 +0200737
Bram Moolenaar7ad7d012010-11-16 15:49:02 +0100738#define DETACH_MSG "DETACH\n"
739
Bram Moolenaar67c53842010-05-22 18:28:27 +0200740 void
741netbeans_read()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742{
743 static char_u *buf = NULL;
Bram Moolenaar0b65f892010-05-15 14:49:02 +0200744 int len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 int readlen = 0;
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100746#ifdef HAVE_SELECT
747 struct timeval tval;
748 fd_set rfds;
749#else
750# ifdef HAVE_POLL
751 struct pollfd fds;
752# endif
753#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200755 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 {
757 nbdebug(("messageFromNetbeans() called without a socket\n"));
758 return;
759 }
760
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 /* Allocate a buffer to read into. */
762 if (buf == NULL)
763 {
764 buf = alloc(MAXMSGSIZE);
765 if (buf == NULL)
766 return; /* out of memory! */
767 }
768
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100769 /* Keep on reading for as long as there is something to read.
770 * Use select() or poll() to avoid blocking on a message that is exactly
771 * MAXMSGSIZE long. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 for (;;)
773 {
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100774#ifdef HAVE_SELECT
775 FD_ZERO(&rfds);
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200776 FD_SET(nbsock, &rfds);
Bram Moolenaar67c53842010-05-22 18:28:27 +0200777 tval.tv_sec = 0;
778 tval.tv_usec = 0;
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200779 if (select(nbsock + 1, &rfds, NULL, NULL, &tval) <= 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200780 break;
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100781#else
782# ifdef HAVE_POLL
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200783 fds.fd = nbsock;
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100784 fds.events = POLLIN;
Bram Moolenaar67c53842010-05-22 18:28:27 +0200785 if (poll(&fds, 1, 0) <= 0)
786 break;
Bram Moolenaar581f6dc2010-03-10 16:12:48 +0100787# endif
788#endif
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200789 len = sock_read(nbsock, buf, MAXMSGSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 if (len <= 0)
791 break; /* error or nothing more to read */
792
793 /* Store the read message in the queue. */
794 save(buf, len);
795 readlen += len;
796 if (len < MAXMSGSIZE)
797 break; /* did read everything that's available */
798 }
799
Bram Moolenaar7ad7d012010-11-16 15:49:02 +0100800 /* Reading a socket disconnection (readlen == 0), or a socket error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 if (readlen <= 0)
802 {
Bram Moolenaar7ad7d012010-11-16 15:49:02 +0100803 /* Queue a "DETACH" netbeans message in the command queue in order to
804 * terminate the netbeans session later. Do not end the session here
805 * directly as we may be running in the context of a call to
806 * netbeans_parse_messages():
807 * netbeans_parse_messages
808 * -> autocmd triggered while processing the netbeans cmd
809 * -> ui_breakcheck
810 * -> gui event loop or select loop
811 * -> netbeans_read()
812 */
813 save((char_u *)DETACH_MSG, strlen(DETACH_MSG));
814 nb_close_socket();
815
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 if (len < 0)
Bram Moolenaarf2330482008-06-24 20:19:36 +0000817 {
818 nbdebug(("read from Netbeans socket\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 PERROR(_("read from Netbeans socket"));
Bram Moolenaarf2330482008-06-24 20:19:36 +0000820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 }
822
Bram Moolenaar03531f72010-11-16 15:04:57 +0100823#if defined(NB_HAS_GUI) && defined(FEAT_GUI_GTK)
824 if (NB_HAS_GUI && gtk_main_level() > 0)
Bram Moolenaar7ad7d012010-11-16 15:49:02 +0100825 gtk_main_quit();
Bram Moolenaarf2330482008-06-24 20:19:36 +0000826#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827}
828
829/*
830 * Handle one NUL terminated command.
831 *
832 * format of a command from netbeans:
833 *
834 * 6:setTitle!84 "a.c"
835 *
836 * bufno
837 * colon
838 * cmd
839 * !
840 * cmdno
841 * args
842 *
843 * for function calls, the ! is replaced by a /
844 */
845 static void
846nb_parse_cmd(char_u *cmd)
847{
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000848 char *verb;
849 char *q;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 int bufno;
851 int isfunc = -1;
852
853 if (STRCMP(cmd, "DISCONNECT") == 0)
854 {
855 /* We assume the server knows that we can safely exit! */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 /* Disconnect before exiting, Motif hangs in a Select error
857 * message otherwise. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200858 netbeans_close();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 getout(0);
860 /* NOTREACHED */
861 }
862
863 if (STRCMP(cmd, "DETACH") == 0)
864 {
865 /* The IDE is breaking the connection. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200866 netbeans_close();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 return;
868 }
869
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000870 bufno = strtol((char *)cmd, &verb, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871
872 if (*verb != ':')
873 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000874 nbdebug((" missing colon: %s\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 EMSG2("E627: missing colon: %s", cmd);
876 return;
877 }
878 ++verb; /* skip colon */
879
880 for (q = verb; *q; q++)
881 {
882 if (*q == '!')
883 {
884 *q++ = NUL;
885 isfunc = 0;
886 break;
887 }
888 else if (*q == '/')
889 {
890 *q++ = NUL;
891 isfunc = 1;
892 break;
893 }
894 }
895
896 if (isfunc < 0)
897 {
Bram Moolenaarf2330482008-06-24 20:19:36 +0000898 nbdebug((" missing ! or / in: %s\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 EMSG2("E628: missing ! or / in: %s", cmd);
900 return;
901 }
902
Bram Moolenaar89d40322006-08-29 15:30:07 +0000903 r_cmdno = strtol(q, &q, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904
Bram Moolenaar349b2f62004-10-11 10:00:50 +0000905 q = (char *)skipwhite((char_u *)q);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906
Bram Moolenaar89d40322006-08-29 15:30:07 +0000907 if (nb_do_cmd(bufno, (char_u *)verb, isfunc, r_cmdno, (char_u *)q) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 {
Bram Moolenaar009b2592004-10-24 19:18:58 +0000909#ifdef NBDEBUG
910 /*
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100911 * This happens because the ExtEd can send a command or 2 after
Bram Moolenaar009b2592004-10-24 19:18:58 +0000912 * doing a stopDocumentListen command. It doesn't harm anything
913 * so I'm disabling it except for debugging.
914 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 nbdebug(("nb_parse_cmd: Command error for \"%s\"\n", cmd));
916 EMSG("E629: bad return from nb_do_cmd");
Bram Moolenaar009b2592004-10-24 19:18:58 +0000917#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 }
919}
920
921struct nbbuf_struct
922{
923 buf_T *bufp;
924 unsigned int fireChanges:1;
925 unsigned int initDone:1;
Bram Moolenaar009b2592004-10-24 19:18:58 +0000926 unsigned int insertDone:1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 unsigned int modified:1;
Bram Moolenaar009b2592004-10-24 19:18:58 +0000928 int nbbuf_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 char *displayname;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 int *signmap;
931 short_u signmaplen;
932 short_u signmapused;
933};
934
935typedef struct nbbuf_struct nbbuf_T;
936
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200937static nbbuf_T *buf_list = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000938static int buf_list_size = 0; /* size of buf_list */
939static int buf_list_used = 0; /* nr of entries in buf_list actually in use */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200941static char **globalsignmap = NULL;
942static int globalsignmaplen = 0;
943static int globalsignmapused = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944
945static int mapsigntype __ARGS((nbbuf_T *, int localsigntype));
946static void addsigntype __ARGS((nbbuf_T *, int localsigntype, char_u *typeName,
947 char_u *tooltip, char_u *glyphfile,
Bram Moolenaar67c53842010-05-22 18:28:27 +0200948 char_u *fg, char_u *bg));
Bram Moolenaar009b2592004-10-24 19:18:58 +0000949static void print_read_msg __ARGS((nbbuf_T *buf));
Bram Moolenaar914703b2010-05-31 21:59:46 +0200950static void print_save_msg __ARGS((nbbuf_T *buf, off_t nchars));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951
952static int curPCtype = -1;
953
954/*
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200955 * Free netbeans resources.
956 */
957 static void
958nb_free()
959{
960 keyQ_T *key_node = keyHead.next;
961 queue_T *cmd_node = head.next;
962 nbbuf_T buf;
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200963 int i;
964
965 /* free the netbeans buffer list */
966 for (i = 0; i < buf_list_used; i++)
967 {
968 buf = buf_list[i];
969 vim_free(buf.displayname);
970 vim_free(buf.signmap);
Bram Moolenaare980d8a2010-12-08 13:11:21 +0100971 if (buf.bufp != NULL)
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200972 {
973 buf.bufp->b_netbeans_file = FALSE;
974 buf.bufp->b_was_netbeans_file = FALSE;
975 }
976 }
977 vim_free(buf_list);
978 buf_list = NULL;
979 buf_list_size = 0;
980 buf_list_used = 0;
981
982 /* free the queued key commands */
983 while(key_node != NULL && key_node != &keyHead)
984 {
985 keyQ_T *next = key_node->next;
986 vim_free(key_node->keystr);
987 vim_free(key_node);
988 if (next == &keyHead)
989 {
990 keyHead.next = &keyHead;
991 keyHead.prev = &keyHead;
992 break;
993 }
994 key_node = next;
995 }
996
997 /* free the queued netbeans commands */
998 while(cmd_node != NULL && cmd_node != &head)
999 {
1000 queue_T *next = cmd_node->next;
1001 vim_free(cmd_node->buffer);
1002 vim_free(cmd_node);
1003 if (next == &head)
1004 {
1005 head.next = &head;
1006 head.prev = &head;
1007 break;
1008 }
1009 cmd_node = next;
1010 }
1011}
1012
1013/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 * Get the Netbeans buffer number for the specified buffer.
1015 */
1016 static int
1017nb_getbufno(buf_T *bufp)
1018{
1019 int i;
1020
1021 for (i = 0; i < buf_list_used; i++)
1022 if (buf_list[i].bufp == bufp)
1023 return i;
1024 return -1;
1025}
1026
1027/*
1028 * Is this a NetBeans-owned buffer?
1029 */
1030 int
1031isNetbeansBuffer(buf_T *bufp)
1032{
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001033 return NETBEANS_OPEN && bufp->b_netbeans_file;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034}
1035
1036/*
1037 * NetBeans and Vim have different undo models. In Vim, the file isn't
1038 * changed if changes are undone via the undo command. In NetBeans, once
1039 * a change has been made the file is marked as modified until saved. It
1040 * doesn't matter if the change was undone.
1041 *
1042 * So this function is for the corner case where Vim thinks a buffer is
1043 * unmodified but NetBeans thinks it IS modified.
1044 */
1045 int
1046isNetbeansModified(buf_T *bufp)
1047{
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001048 if (isNetbeansBuffer(bufp))
Bram Moolenaar009b2592004-10-24 19:18:58 +00001049 {
1050 int bufno = nb_getbufno(bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051
Bram Moolenaar009b2592004-10-24 19:18:58 +00001052 if (bufno > 0)
1053 return buf_list[bufno].modified;
1054 else
1055 return FALSE;
1056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 else
1058 return FALSE;
1059}
1060
1061/*
1062 * Given a Netbeans buffer number, return the netbeans buffer.
1063 * Returns NULL for 0 or a negative number. A 0 bufno means a
1064 * non-buffer related command has been sent.
1065 */
1066 static nbbuf_T *
1067nb_get_buf(int bufno)
1068{
1069 /* find or create a buffer with the given number */
1070 int incr;
1071
1072 if (bufno <= 0)
1073 return NULL;
1074
1075 if (!buf_list)
1076 {
1077 /* initialize */
1078 buf_list = (nbbuf_T *)alloc_clear(100 * sizeof(nbbuf_T));
1079 buf_list_size = 100;
1080 }
1081 if (bufno >= buf_list_used) /* new */
1082 {
1083 if (bufno >= buf_list_size) /* grow list */
1084 {
1085 incr = bufno - buf_list_size + 90;
1086 buf_list_size += incr;
1087 buf_list = (nbbuf_T *)vim_realloc(
1088 buf_list, buf_list_size * sizeof(nbbuf_T));
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02001089 vim_memset(buf_list + buf_list_size - incr, 0,
1090 incr * sizeof(nbbuf_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 }
1092
1093 while (buf_list_used <= bufno)
1094 {
1095 /* Default is to fire text changes. */
1096 buf_list[buf_list_used].fireChanges = 1;
1097 ++buf_list_used;
1098 }
1099 }
1100
1101 return buf_list + bufno;
1102}
1103
1104/*
1105 * Return the number of buffers that are modified.
1106 */
1107 static int
1108count_changed_buffers(void)
1109{
1110 buf_T *bufp;
1111 int n;
1112
1113 n = 0;
1114 for (bufp = firstbuf; bufp != NULL; bufp = bufp->b_next)
1115 if (bufp->b_changed)
1116 ++n;
1117 return n;
1118}
1119
1120/*
1121 * End the netbeans session.
1122 */
1123 void
1124netbeans_end(void)
1125{
1126 int i;
1127 static char buf[128];
1128
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001129 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 return;
1131
1132 for (i = 0; i < buf_list_used; i++)
1133 {
1134 if (!buf_list[i].bufp)
1135 continue;
1136 if (netbeansForcedQuit)
1137 {
1138 /* mark as unmodified so NetBeans won't put up dialog on "killed" */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001139 sprintf(buf, "%d:unmodified=%d\n", i, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 nbdebug(("EVT: %s", buf));
1141 nb_send(buf, "netbeans_end");
1142 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001143 sprintf(buf, "%d:killed=%d\n", i, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 nbdebug(("EVT: %s", buf));
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001145 /* nb_send(buf, "netbeans_end"); avoid "write failed" messages */
1146 ignored = sock_write(nbsock, buf, (int)STRLEN(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148}
1149
1150/*
1151 * Send a message to netbeans.
1152 */
1153 static void
1154nb_send(char *buf, char *fun)
1155{
1156 /* Avoid giving pages full of error messages when the other side has
1157 * exited, only mention the first error until the connection works again. */
1158 static int did_error = FALSE;
1159
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001160 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 {
1162 if (!did_error)
Bram Moolenaarf2330482008-06-24 20:19:36 +00001163 {
1164 nbdebug((" %s(): write while not connected\n", fun));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 EMSG2("E630: %s(): write while not connected", fun);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 did_error = TRUE;
1168 }
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001169 else if (sock_write(nbsock, buf, (int)STRLEN(buf)) != (int)STRLEN(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 {
1171 if (!did_error)
Bram Moolenaarf2330482008-06-24 20:19:36 +00001172 {
1173 nbdebug((" %s(): write failed\n", fun));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 EMSG2("E631: %s(): write failed", fun);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 did_error = TRUE;
1177 }
1178 else
1179 did_error = FALSE;
1180}
1181
1182/*
1183 * Some input received from netbeans requires a response. This function
1184 * handles a response with no information (except the command number).
1185 */
1186 static void
1187nb_reply_nil(int cmdno)
1188{
1189 char reply[32];
1190
Bram Moolenaar009b2592004-10-24 19:18:58 +00001191 nbdebug(("REP %d: <none>\n", cmdno));
1192
Bram Moolenaar7ad7d012010-11-16 15:49:02 +01001193 /* Avoid printing an annoying error message. */
1194 if (!NETBEANS_OPEN)
1195 return;
1196
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 sprintf(reply, "%d\n", cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 nb_send(reply, "nb_reply_nil");
1199}
1200
1201
1202/*
1203 * Send a response with text.
1204 * "result" must have been quoted already (using nb_quote()).
1205 */
1206 static void
1207nb_reply_text(int cmdno, char_u *result)
1208{
1209 char_u *reply;
1210
Bram Moolenaar009b2592004-10-24 19:18:58 +00001211 nbdebug(("REP %d: %s\n", cmdno, (char *)result));
1212
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001213 reply = alloc((unsigned)STRLEN(result) + 32);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 sprintf((char *)reply, "%d %s\n", cmdno, (char *)result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 nb_send((char *)reply, "nb_reply_text");
1216
1217 vim_free(reply);
1218}
1219
1220
1221/*
1222 * Send a response with a number result code.
1223 */
1224 static void
1225nb_reply_nr(int cmdno, long result)
1226{
1227 char reply[32];
1228
Bram Moolenaar009b2592004-10-24 19:18:58 +00001229 nbdebug(("REP %d: %ld\n", cmdno, result));
1230
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 sprintf(reply, "%d %ld\n", cmdno, result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 nb_send(reply, "nb_reply_nr");
1233}
1234
1235
1236/*
1237 * Encode newline, ret, backslash, double quote for transmission to NetBeans.
1238 */
1239 static char_u *
1240nb_quote(char_u *txt)
1241{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001242 char_u *buf = alloc((unsigned)(2 * STRLEN(txt) + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 char_u *p = txt;
1244 char_u *q = buf;
1245
1246 if (buf == NULL)
1247 return NULL;
1248 for (; *p; p++)
1249 {
1250 switch (*p)
1251 {
1252 case '\"':
1253 case '\\':
1254 *q++ = '\\'; *q++ = *p; break;
1255 /* case '\t': */
1256 /* *q++ = '\\'; *q++ = 't'; break; */
1257 case '\n':
1258 *q++ = '\\'; *q++ = 'n'; break;
1259 case '\r':
1260 *q++ = '\\'; *q++ = 'r'; break;
1261 default:
1262 *q++ = *p;
1263 break;
1264 }
1265 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01001266 *q = '\0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267
1268 return buf;
1269}
1270
1271
1272/*
1273 * Remove top level double quotes; convert backslashed chars.
1274 * Returns an allocated string (NULL for failure).
1275 * If "endp" is not NULL it is set to the character after the terminating
1276 * quote.
1277 */
1278 static char *
1279nb_unquote(char_u *p, char_u **endp)
1280{
1281 char *result = 0;
1282 char *q;
1283 int done = 0;
1284
1285 /* result is never longer than input */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001286 result = (char *)alloc_clear((unsigned)STRLEN(p) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 if (result == NULL)
1288 return NULL;
1289
1290 if (*p++ != '"')
1291 {
1292 nbdebug(("nb_unquote called with string that doesn't start with a quote!: %s\n",
1293 p));
1294 result[0] = NUL;
1295 return result;
1296 }
1297
1298 for (q = result; !done && *p != NUL;)
1299 {
1300 switch (*p)
1301 {
1302 case '"':
1303 /*
1304 * Unbackslashed dquote marks the end, if first char was dquote.
1305 */
1306 done = 1;
1307 break;
1308
1309 case '\\':
1310 ++p;
1311 switch (*p)
1312 {
1313 case '\\': *q++ = '\\'; break;
1314 case 'n': *q++ = '\n'; break;
1315 case 't': *q++ = '\t'; break;
1316 case 'r': *q++ = '\r'; break;
1317 case '"': *q++ = '"'; break;
1318 case NUL: --p; break;
1319 /* default: skip over illegal chars */
1320 }
1321 ++p;
1322 break;
1323
1324 default:
1325 *q++ = *p++;
1326 }
1327 }
1328
1329 if (endp != NULL)
1330 *endp = p;
1331
1332 return result;
1333}
1334
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001335/*
1336 * Remove from "first" byte to "last" byte (inclusive), at line "lnum" of the
1337 * current buffer. Remove to end of line when "last" is MAXCOL.
1338 */
1339 static void
1340nb_partialremove(linenr_T lnum, colnr_T first, colnr_T last)
1341{
1342 char_u *oldtext, *newtext;
1343 int oldlen;
1344 int lastbyte = last;
1345
1346 oldtext = ml_get(lnum);
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001347 oldlen = (int)STRLEN(oldtext);
Bram Moolenaarb3c70982008-01-18 10:40:55 +00001348 if (first >= (colnr_T)oldlen || oldlen == 0) /* just in case */
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001349 return;
1350 if (lastbyte >= oldlen)
1351 lastbyte = oldlen - 1;
1352 newtext = alloc(oldlen - (int)(lastbyte - first));
1353 if (newtext != NULL)
1354 {
1355 mch_memmove(newtext, oldtext, first);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001356 STRMOVE(newtext + first, oldtext + lastbyte + 1);
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001357 nbdebug((" NEW LINE %d: %s\n", lnum, newtext));
1358 ml_replace(lnum, newtext, FALSE);
1359 }
1360}
1361
1362/*
1363 * Replace the "first" line with the concatenation of the "first" and
1364 * the "other" line. The "other" line is not removed.
1365 */
1366 static void
1367nb_joinlines(linenr_T first, linenr_T other)
1368{
1369 int len_first, len_other;
1370 char_u *p;
1371
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001372 len_first = (int)STRLEN(ml_get(first));
1373 len_other = (int)STRLEN(ml_get(other));
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001374 p = alloc((unsigned)(len_first + len_other + 1));
1375 if (p != NULL)
1376 {
1377 mch_memmove(p, ml_get(first), len_first);
1378 mch_memmove(p + len_first, ml_get(other), len_other + 1);
1379 ml_replace(first, p, FALSE);
1380 }
1381}
1382
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383#define SKIP_STOP 2
1384#define streq(a,b) (strcmp(a,b) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385
1386/*
1387 * Do the actual processing of a single netbeans command or function.
Bram Moolenaar143c38c2007-05-10 16:41:10 +00001388 * The difference between a command and function is that a function
1389 * gets a response (it's required) but a command does not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 * For arguments see comment for nb_parse_cmd().
1391 */
1392 static int
1393nb_do_cmd(
1394 int bufno,
1395 char_u *cmd,
1396 int func,
1397 int cmdno,
1398 char_u *args) /* points to space before arguments or NUL */
1399{
1400 int doupdate = 0;
1401 long off = 0;
1402 nbbuf_T *buf = nb_get_buf(bufno);
1403 static int skip = 0;
1404 int retval = OK;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001405 char *cp; /* for when a char pointer is needed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406
1407 nbdebug(("%s %d: (%d) %s %s\n", (func) ? "FUN" : "CMD", cmdno, bufno, cmd,
1408 STRCMP(cmd, "insert") == 0 ? "<text>" : (char *)args));
1409
1410 if (func)
1411 {
1412/* =====================================================================*/
1413 if (streq((char *)cmd, "getModified"))
1414 {
1415 if (buf == NULL || buf->bufp == NULL)
1416 /* Return the number of buffers that are modified. */
1417 nb_reply_nr(cmdno, (long)count_changed_buffers());
1418 else
1419 /* Return whether the buffer is modified. */
1420 nb_reply_nr(cmdno, (long)(buf->bufp->b_changed
1421 || isNetbeansModified(buf->bufp)));
1422/* =====================================================================*/
1423 }
1424 else if (streq((char *)cmd, "saveAndExit"))
1425 {
1426 /* Note: this will exit Vim if successful. */
1427 coloncmd(":confirm qall");
1428
1429 /* We didn't exit: return the number of changed buffers. */
1430 nb_reply_nr(cmdno, (long)count_changed_buffers());
1431/* =====================================================================*/
1432 }
1433 else if (streq((char *)cmd, "getCursor"))
1434 {
1435 char_u text[200];
1436
1437 /* Note: nb_getbufno() may return -1. This indicates the IDE
1438 * didn't assign a number to the current buffer in response to a
1439 * fileOpened event. */
1440 sprintf((char *)text, "%d %ld %d %ld",
1441 nb_getbufno(curbuf),
1442 (long)curwin->w_cursor.lnum,
1443 (int)curwin->w_cursor.col,
1444 pos2off(curbuf, &curwin->w_cursor));
1445 nb_reply_text(cmdno, text);
1446/* =====================================================================*/
1447 }
Bram Moolenaarc65c4912006-11-14 17:29:46 +00001448 else if (streq((char *)cmd, "getAnno"))
1449 {
1450 long linenum = 0;
1451#ifdef FEAT_SIGNS
1452 if (buf == NULL || buf->bufp == NULL)
1453 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001454 nbdebug((" Invalid buffer identifier in getAnno\n"));
1455 EMSG("E652: Invalid buffer identifier in getAnno");
Bram Moolenaarc65c4912006-11-14 17:29:46 +00001456 retval = FAIL;
1457 }
1458 else
1459 {
1460 int serNum;
1461
1462 cp = (char *)args;
1463 serNum = strtol(cp, &cp, 10);
1464 /* If the sign isn't found linenum will be zero. */
1465 linenum = (long)buf_findsign(buf->bufp, serNum);
1466 }
1467#endif
1468 nb_reply_nr(cmdno, linenum);
1469/* =====================================================================*/
1470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 else if (streq((char *)cmd, "getLength"))
1472 {
1473 long len = 0;
1474
1475 if (buf == NULL || buf->bufp == NULL)
1476 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001477 nbdebug((" invalid buffer identifier in getLength\n"));
1478 EMSG("E632: invalid buffer identifier in getLength");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 retval = FAIL;
1480 }
1481 else
1482 {
1483 len = get_buf_size(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 }
1485 nb_reply_nr(cmdno, len);
1486/* =====================================================================*/
1487 }
1488 else if (streq((char *)cmd, "getText"))
1489 {
1490 long len;
1491 linenr_T nlines;
1492 char_u *text = NULL;
1493 linenr_T lno = 1;
1494 char_u *p;
1495 char_u *line;
1496
1497 if (buf == NULL || buf->bufp == NULL)
1498 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001499 nbdebug((" invalid buffer identifier in getText\n"));
1500 EMSG("E633: invalid buffer identifier in getText");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 retval = FAIL;
1502 }
1503 else
1504 {
1505 len = get_buf_size(buf->bufp);
1506 nlines = buf->bufp->b_ml.ml_line_count;
1507 text = alloc((unsigned)((len > 0)
1508 ? ((len + nlines) * 2) : 4));
1509 if (text == NULL)
1510 {
1511 nbdebug((" nb_do_cmd: getText has null text field\n"));
1512 retval = FAIL;
1513 }
1514 else
1515 {
1516 p = text;
1517 *p++ = '\"';
1518 for (; lno <= nlines ; lno++)
1519 {
1520 line = nb_quote(ml_get_buf(buf->bufp, lno, FALSE));
1521 if (line != NULL)
1522 {
1523 STRCPY(p, line);
1524 p += STRLEN(line);
1525 *p++ = '\\';
1526 *p++ = 'n';
Bram Moolenaar009b2592004-10-24 19:18:58 +00001527 vim_free(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 }
1530 *p++ = '\"';
1531 *p = '\0';
1532 }
1533 }
1534 if (text == NULL)
1535 nb_reply_text(cmdno, (char_u *)"");
1536 else
1537 {
1538 nb_reply_text(cmdno, text);
1539 vim_free(text);
1540 }
1541/* =====================================================================*/
1542 }
1543 else if (streq((char *)cmd, "remove"))
1544 {
1545 long count;
1546 pos_T first, last;
1547 pos_T *pos;
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001548 pos_T *next;
1549 linenr_T del_from_lnum, del_to_lnum; /* lines to be deleted as a whole */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 int oldFire = netbeansFireChanges;
1551 int oldSuppress = netbeansSuppressNoLines;
1552 int wasChanged;
1553
1554 if (skip >= SKIP_STOP)
1555 {
1556 nbdebug((" Skipping %s command\n", (char *) cmd));
1557 nb_reply_nil(cmdno);
1558 return OK;
1559 }
1560
1561 if (buf == NULL || buf->bufp == NULL)
1562 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001563 nbdebug((" invalid buffer identifier in remove\n"));
1564 EMSG("E634: invalid buffer identifier in remove");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 retval = FAIL;
1566 }
1567 else
1568 {
1569 netbeansFireChanges = FALSE;
1570 netbeansSuppressNoLines = TRUE;
1571
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001572 nb_set_curbuf(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 wasChanged = buf->bufp->b_changed;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001574 cp = (char *)args;
1575 off = strtol(cp, &cp, 10);
1576 count = strtol(cp, &cp, 10);
1577 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 /* delete "count" chars, starting at "off" */
1579 pos = off2pos(buf->bufp, off);
1580 if (!pos)
1581 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001582 nbdebug((" !bad position\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 nb_reply_text(cmdno, (char_u *)"!bad position");
1584 netbeansFireChanges = oldFire;
1585 netbeansSuppressNoLines = oldSuppress;
1586 return FAIL;
1587 }
1588 first = *pos;
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001589 nbdebug((" FIRST POS: line %d, col %d\n",
1590 first.lnum, first.col));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 pos = off2pos(buf->bufp, off+count-1);
1592 if (!pos)
1593 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001594 nbdebug((" !bad count\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 nb_reply_text(cmdno, (char_u *)"!bad count");
1596 netbeansFireChanges = oldFire;
1597 netbeansSuppressNoLines = oldSuppress;
1598 return FAIL;
1599 }
1600 last = *pos;
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001601 nbdebug((" LAST POS: line %d, col %d\n",
1602 last.lnum, last.col));
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001603 del_from_lnum = first.lnum;
1604 del_to_lnum = last.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 doupdate = 1;
1606
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001607 /* Get the position of the first byte after the deleted
1608 * section. "next" is NULL when deleting to the end of the
1609 * file. */
1610 next = off2pos(buf->bufp, off + count);
1611
1612 /* Remove part of the first line. */
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001613 if (first.col != 0
1614 || (next != NULL && first.lnum == next->lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 {
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001616 if (first.lnum != last.lnum
1617 || (next != NULL && first.lnum != next->lnum))
1618 {
1619 /* remove to the end of the first line */
1620 nb_partialremove(first.lnum, first.col,
1621 (colnr_T)MAXCOL);
1622 if (first.lnum == last.lnum)
1623 {
1624 /* Partial line to remove includes the end of
1625 * line. Join the line with the next one, have
1626 * the next line deleted below. */
1627 nb_joinlines(first.lnum, next->lnum);
1628 del_to_lnum = next->lnum;
1629 }
1630 }
1631 else
1632 {
1633 /* remove within one line */
1634 nb_partialremove(first.lnum, first.col, last.col);
1635 }
1636 ++del_from_lnum; /* don't delete the first line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 }
1638
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001639 /* Remove part of the last line. */
1640 if (first.lnum != last.lnum && next != NULL
1641 && next->col != 0 && last.lnum == next->lnum)
1642 {
1643 nb_partialremove(last.lnum, 0, last.col);
1644 if (del_from_lnum > first.lnum)
1645 {
1646 /* Join end of last line to start of first line; last
1647 * line is deleted below. */
1648 nb_joinlines(first.lnum, last.lnum);
1649 }
1650 else
1651 /* First line is deleted as a whole, keep the last
1652 * line. */
1653 --del_to_lnum;
1654 }
1655
1656 /* First is partial line; last line to remove includes
1657 * the end of line; join first line to line following last
1658 * line; line following last line is deleted below. */
1659 if (first.lnum != last.lnum && del_from_lnum > first.lnum
1660 && next != NULL && last.lnum != next->lnum)
1661 {
1662 nb_joinlines(first.lnum, next->lnum);
1663 del_to_lnum = next->lnum;
1664 }
1665
1666 /* Delete whole lines if there are any. */
1667 if (del_to_lnum >= del_from_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 {
1669 int i;
1670
1671 /* delete signs from the lines being deleted */
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001672 for (i = del_from_lnum; i <= del_to_lnum; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 {
1674 int id = buf_findsign_id(buf->bufp, (linenr_T)i);
1675 if (id > 0)
1676 {
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001677 nbdebug((" Deleting sign %d on line %d\n",
1678 id, i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 buf_delsign(buf->bufp, id);
1680 }
1681 else
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001682 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 nbdebug((" No sign on line %d\n", i));
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 }
1686
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00001687 nbdebug((" Deleting lines %d through %d\n",
1688 del_from_lnum, del_to_lnum));
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001689 curwin->w_cursor.lnum = del_from_lnum;
1690 curwin->w_cursor.col = 0;
1691 del_lines(del_to_lnum - del_from_lnum + 1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 }
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00001693
1694 /* Leave cursor at first deleted byte. */
1695 curwin->w_cursor = first;
1696 check_cursor_lnum();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 buf->bufp->b_changed = wasChanged; /* logically unchanged */
1698 netbeansFireChanges = oldFire;
1699 netbeansSuppressNoLines = oldSuppress;
1700
1701 u_blockfree(buf->bufp);
1702 u_clearall(buf->bufp);
1703 }
1704 nb_reply_nil(cmdno);
1705/* =====================================================================*/
1706 }
1707 else if (streq((char *)cmd, "insert"))
1708 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 char_u *to_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710
1711 if (skip >= SKIP_STOP)
1712 {
1713 nbdebug((" Skipping %s command\n", (char *) cmd));
1714 nb_reply_nil(cmdno);
1715 return OK;
1716 }
1717
1718 /* get offset */
Bram Moolenaar349b2f62004-10-11 10:00:50 +00001719 cp = (char *)args;
1720 off = strtol(cp, &cp, 10);
1721 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722
1723 /* get text to be inserted */
1724 args = skipwhite(args);
1725 args = to_free = (char_u *)nb_unquote(args, NULL);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001726 /*
1727 nbdebug((" CHUNK[%d]: %d bytes at offset %d\n",
1728 buf->bufp->b_ml.ml_line_count, STRLEN(args), off));
1729 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730
1731 if (buf == NULL || buf->bufp == NULL)
1732 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001733 nbdebug((" invalid buffer identifier in insert\n"));
1734 EMSG("E635: invalid buffer identifier in insert");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 retval = FAIL;
1736 }
1737 else if (args != NULL)
1738 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001739 int ff_detected = EOL_UNKNOWN;
1740 int buf_was_empty = (buf->bufp->b_ml.ml_flags & ML_EMPTY);
1741 size_t len = 0;
1742 int added = 0;
1743 int oldFire = netbeansFireChanges;
1744 int old_b_changed;
1745 char_u *nl;
1746 linenr_T lnum;
1747 linenr_T lnum_start;
1748 pos_T *pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 netbeansFireChanges = 0;
1751
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001752 /* Jump to the buffer where we insert. After this "curbuf"
1753 * can be used. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001754 nb_set_curbuf(buf->bufp);
Bram Moolenaara3227e22006-03-08 21:32:40 +00001755 old_b_changed = curbuf->b_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001757 /* Convert the specified character offset into a lnum/col
1758 * position. */
Bram Moolenaara3227e22006-03-08 21:32:40 +00001759 pos = off2pos(curbuf, off);
1760 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001762 if (pos->lnum <= 0)
1763 lnum_start = 1;
1764 else
1765 lnum_start = pos->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 }
1767 else
1768 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001769 /* If the given position is not found, assume we want
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 * the end of the file. See setLocAndSize HACK. */
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001771 if (buf_was_empty)
1772 lnum_start = 1; /* above empty line */
1773 else
1774 lnum_start = curbuf->b_ml.ml_line_count + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001775 }
1776
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001777 /* "lnum" is the line where we insert: either append to it or
1778 * insert a new line above it. */
1779 lnum = lnum_start;
1780
1781 /* Loop over the "\n" separated lines of the argument. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 doupdate = 1;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001783 while (*args != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001785 nl = vim_strchr(args, '\n');
1786 if (nl == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001788 /* Incomplete line, probably truncated. Next "insert"
1789 * command should append to this one. */
1790 len = STRLEN(args);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001792 else
1793 {
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001794 len = nl - args;
1795
1796 /*
1797 * We need to detect EOL style, because the commands
1798 * use a character offset.
1799 */
1800 if (nl > args && nl[-1] == '\r')
1801 {
1802 ff_detected = EOL_DOS;
1803 --len;
1804 }
1805 else
1806 ff_detected = EOL_UNIX;
1807 }
1808 args[len] = NUL;
1809
1810 if (lnum == lnum_start
1811 && ((pos != NULL && pos->col > 0)
1812 || (lnum == 1 && buf_was_empty)))
1813 {
1814 char_u *oldline = ml_get(lnum);
1815 char_u *newline;
1816
1817 /* Insert halfway a line. For simplicity we assume we
1818 * need to append to the line. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001819 newline = alloc_check((unsigned)(STRLEN(oldline) + len + 1));
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001820 if (newline != NULL)
1821 {
1822 STRCPY(newline, oldline);
1823 STRCAT(newline, args);
1824 ml_replace(lnum, newline, FALSE);
1825 }
1826 }
1827 else
1828 {
1829 /* Append a new line. Not that we always do this,
1830 * also when the text doesn't end in a "\n". */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001831 ml_append((linenr_T)(lnum - 1), args, (colnr_T)(len + 1), FALSE);
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001832 ++added;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001833 }
1834
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001835 if (nl == NULL)
1836 break;
1837 ++lnum;
1838 args = nl + 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001839 }
1840
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001841 /* Adjust the marks below the inserted lines. */
1842 appended_lines_mark(lnum_start - 1, (long)added);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001844 /*
1845 * When starting with an empty buffer set the fileformat.
1846 * This is just guessing...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 */
1848 if (buf_was_empty)
1849 {
1850 if (ff_detected == EOL_UNKNOWN)
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001851#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 ff_detected = EOL_DOS;
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001853#else
1854 ff_detected = EOL_UNIX;
1855#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 set_fileformat(ff_detected, OPT_LOCAL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00001857 curbuf->b_start_ffc = *curbuf->b_p_ff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 }
1859
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 /*
1861 * XXX - GRP - Is the next line right? If I've inserted
1862 * text the buffer has been updated but not written. Will
1863 * netbeans guarantee to write it? Even if I do a :q! ?
1864 */
Bram Moolenaara3227e22006-03-08 21:32:40 +00001865 curbuf->b_changed = old_b_changed; /* logically unchanged */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 netbeansFireChanges = oldFire;
1867
Bram Moolenaar0fd92892006-03-09 22:27:48 +00001868 /* Undo info is invalid now... */
Bram Moolenaara3227e22006-03-08 21:32:40 +00001869 u_blockfree(curbuf);
1870 u_clearall(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 }
1872 vim_free(to_free);
1873 nb_reply_nil(cmdno); /* or !error */
1874 }
1875 else
1876 {
1877 nbdebug(("UNIMPLEMENTED FUNCTION: %s\n", cmd));
1878 nb_reply_nil(cmdno);
1879 retval = FAIL;
1880 }
1881 }
1882 else /* Not a function; no reply required. */
1883 {
1884/* =====================================================================*/
1885 if (streq((char *)cmd, "create"))
1886 {
1887 /* Create a buffer without a name. */
1888 if (buf == NULL)
1889 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001890 nbdebug((" invalid buffer identifier in create\n"));
1891 EMSG("E636: invalid buffer identifier in create");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 return FAIL;
1893 }
1894 vim_free(buf->displayname);
1895 buf->displayname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896
1897 netbeansReadFile = 0; /* don't try to open disk file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001898 do_ecmd(0, NULL, 0, 0, ECMD_ONE, ECMD_HIDE + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 netbeansReadFile = 1;
1900 buf->bufp = curbuf;
1901 maketitle();
Bram Moolenaar009b2592004-10-24 19:18:58 +00001902 buf->insertDone = FALSE;
Bram Moolenaar67c53842010-05-22 18:28:27 +02001903#if defined(FEAT_MENU) && defined(FEAT_GUI)
Bram Moolenaard54a6882010-08-09 22:49:00 +02001904 if (gui.in_use)
1905 gui_update_menus(0);
Bram Moolenaar67c53842010-05-22 18:28:27 +02001906#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907/* =====================================================================*/
1908 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001909 else if (streq((char *)cmd, "insertDone"))
1910 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001911 if (buf == NULL || buf->bufp == NULL)
1912 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001913 nbdebug((" invalid buffer identifier in insertDone\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001914 }
1915 else
1916 {
1917 buf->bufp->b_start_eol = *args == 'T';
1918 buf->insertDone = TRUE;
1919 args += 2;
1920 buf->bufp->b_p_ro = *args == 'T';
1921 print_read_msg(buf);
1922 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001923/* =====================================================================*/
1924 }
1925 else if (streq((char *)cmd, "saveDone"))
1926 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001927 long savedChars = atol((char *)args);
1928
1929 if (buf == NULL || buf->bufp == NULL)
1930 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001931 nbdebug((" invalid buffer identifier in saveDone\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001932 }
1933 else
1934 print_save_msg(buf, savedChars);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001935/* =====================================================================*/
1936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 else if (streq((char *)cmd, "startDocumentListen"))
1938 {
1939 if (buf == NULL)
1940 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001941 nbdebug((" invalid buffer identifier in startDocumentListen\n"));
1942 EMSG("E637: invalid buffer identifier in startDocumentListen");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 return FAIL;
1944 }
1945 buf->fireChanges = 1;
1946/* =====================================================================*/
1947 }
1948 else if (streq((char *)cmd, "stopDocumentListen"))
1949 {
1950 if (buf == NULL)
1951 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001952 nbdebug((" invalid buffer identifier in stopDocumentListen\n"));
1953 EMSG("E638: invalid buffer identifier in stopDocumentListen");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 return FAIL;
1955 }
1956 buf->fireChanges = 0;
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001957 if (buf->bufp != NULL && buf->bufp->b_was_netbeans_file)
Bram Moolenaar009b2592004-10-24 19:18:58 +00001958 {
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001959 if (!buf->bufp->b_netbeans_file)
Bram Moolenaarf2330482008-06-24 20:19:36 +00001960 {
1961 nbdebug(("E658: NetBeans connection lost for buffer %ld\n", buf->bufp->b_fnum));
Bram Moolenaar009b2592004-10-24 19:18:58 +00001962 EMSGN(_("E658: NetBeans connection lost for buffer %ld"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 buf->bufp->b_fnum);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001964 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00001965 else
1966 {
Bram Moolenaar24c088a2005-02-02 22:55:47 +00001967 /* NetBeans uses stopDocumentListen when it stops editing
1968 * a file. It then expects the buffer in Vim to
1969 * disappear. */
1970 do_bufdel(DOBUF_DEL, (char_u *)"", 1,
1971 buf->bufp->b_fnum, buf->bufp->b_fnum, TRUE);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001972 vim_memset(buf, 0, sizeof(nbbuf_T));
1973 }
1974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975/* =====================================================================*/
1976 }
1977 else if (streq((char *)cmd, "setTitle"))
1978 {
1979 if (buf == NULL)
1980 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001981 nbdebug((" invalid buffer identifier in setTitle\n"));
1982 EMSG("E639: invalid buffer identifier in setTitle");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 return FAIL;
1984 }
1985 vim_free(buf->displayname);
1986 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987/* =====================================================================*/
1988 }
1989 else if (streq((char *)cmd, "initDone"))
1990 {
1991 if (buf == NULL || buf->bufp == NULL)
1992 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001993 nbdebug((" invalid buffer identifier in initDone\n"));
1994 EMSG("E640: invalid buffer identifier in initDone");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 return FAIL;
1996 }
1997 doupdate = 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00001998 buf->initDone = TRUE;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00001999 nb_set_curbuf(buf->bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000#if defined(FEAT_AUTOCMD)
2001 apply_autocmds(EVENT_BUFREADPOST, 0, 0, FALSE, buf->bufp);
2002#endif
2003
2004 /* handle any postponed key commands */
2005 handle_key_queue();
2006/* =====================================================================*/
2007 }
2008 else if (streq((char *)cmd, "setBufferNumber")
2009 || streq((char *)cmd, "putBufferNumber"))
2010 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002011 char_u *path;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 buf_T *bufp;
2013
2014 if (buf == NULL)
2015 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002016 nbdebug((" invalid buffer identifier in setBufferNumber\n"));
2017 EMSG("E641: invalid buffer identifier in setBufferNumber");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 return FAIL;
2019 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002020 path = (char_u *)nb_unquote(args, NULL);
2021 if (path == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 return FAIL;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002023 bufp = buflist_findname(path);
2024 vim_free(path);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 if (bufp == NULL)
2026 {
Bram Moolenaarec906222009-02-21 21:14:00 +00002027 nbdebug((" File %s not found in setBufferNumber\n", args));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 EMSG2("E642: File %s not found in setBufferNumber", args);
2029 return FAIL;
2030 }
2031 buf->bufp = bufp;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002032 buf->nbbuf_number = bufp->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033
2034 /* "setBufferNumber" has the side effect of jumping to the buffer
2035 * (don't know why!). Don't do that for "putBufferNumber". */
2036 if (*cmd != 'p')
2037 coloncmd(":buffer %d", bufp->b_fnum);
2038 else
2039 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002040 buf->initDone = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041
2042 /* handle any postponed key commands */
2043 handle_key_queue();
2044 }
2045
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046/* =====================================================================*/
2047 }
2048 else if (streq((char *)cmd, "setFullName"))
2049 {
2050 if (buf == NULL)
2051 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002052 nbdebug((" invalid buffer identifier in setFullName\n"));
2053 EMSG("E643: invalid buffer identifier in setFullName");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 return FAIL;
2055 }
2056 vim_free(buf->displayname);
2057 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058
2059 netbeansReadFile = 0; /* don't try to open disk file */
2060 do_ecmd(0, (char_u *)buf->displayname, 0, 0, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002061 ECMD_HIDE + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 netbeansReadFile = 1;
2063 buf->bufp = curbuf;
2064 maketitle();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002065#if defined(FEAT_MENU) && defined(FEAT_GUI)
Bram Moolenaard54a6882010-08-09 22:49:00 +02002066 if (gui.in_use)
2067 gui_update_menus(0);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002068#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069/* =====================================================================*/
2070 }
2071 else if (streq((char *)cmd, "editFile"))
2072 {
2073 if (buf == NULL)
2074 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002075 nbdebug((" invalid buffer identifier in editFile\n"));
2076 EMSG("E644: invalid buffer identifier in editFile");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 return FAIL;
2078 }
2079 /* Edit a file: like create + setFullName + read the file. */
2080 vim_free(buf->displayname);
2081 buf->displayname = nb_unquote(args, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 do_ecmd(0, (char_u *)buf->displayname, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002083 ECMD_HIDE + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 buf->bufp = curbuf;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002085 buf->initDone = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 doupdate = 1;
2087#if defined(FEAT_TITLE)
2088 maketitle();
2089#endif
Bram Moolenaar67c53842010-05-22 18:28:27 +02002090#if defined(FEAT_MENU) && defined(FEAT_GUI)
Bram Moolenaard54a6882010-08-09 22:49:00 +02002091 if (gui.in_use)
2092 gui_update_menus(0);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002093#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094/* =====================================================================*/
2095 }
2096 else if (streq((char *)cmd, "setVisible"))
2097 {
2098 if (buf == NULL || buf->bufp == NULL)
2099 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002100 nbdebug((" invalid buffer identifier in setVisible\n"));
2101 /* This message was commented out, probably because it can
2102 * happen when shutting down. */
2103 if (p_verbose > 0)
2104 EMSG("E645: invalid buffer identifier in setVisible");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 return FAIL;
2106 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002107 if (streq((char *)args, "T") && buf->bufp != curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 {
2109 exarg_T exarg;
2110 exarg.cmd = (char_u *)"goto";
2111 exarg.forceit = FALSE;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002112 dosetvisible = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 goto_buffer(&exarg, DOBUF_FIRST, FORWARD, buf->bufp->b_fnum);
2114 doupdate = 1;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002115 dosetvisible = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116
Bram Moolenaar67c53842010-05-22 18:28:27 +02002117#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 /* Side effect!!!. */
Bram Moolenaard54a6882010-08-09 22:49:00 +02002119 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 gui_mch_set_foreground();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002121#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123/* =====================================================================*/
2124 }
2125 else if (streq((char *)cmd, "raise"))
2126 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002127#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 /* Bring gvim to the foreground. */
Bram Moolenaard54a6882010-08-09 22:49:00 +02002129 if (gui.in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 gui_mch_set_foreground();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132/* =====================================================================*/
2133 }
2134 else if (streq((char *)cmd, "setModified"))
2135 {
Bram Moolenaarff064e12008-06-09 13:10:45 +00002136 int prev_b_changed;
2137
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 if (buf == NULL || buf->bufp == NULL)
2139 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002140 nbdebug((" invalid buffer identifier in setModified\n"));
2141 /* This message was commented out, probably because it can
2142 * happen when shutting down. */
2143 if (p_verbose > 0)
2144 EMSG("E646: invalid buffer identifier in setModified");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 return FAIL;
2146 }
Bram Moolenaarff064e12008-06-09 13:10:45 +00002147 prev_b_changed = buf->bufp->b_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 if (streq((char *)args, "T"))
Bram Moolenaarff064e12008-06-09 13:10:45 +00002149 buf->bufp->b_changed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 else
2151 {
2152 struct stat st;
2153
2154 /* Assume NetBeans stored the file. Reset the timestamp to
2155 * avoid "file changed" warnings. */
2156 if (buf->bufp->b_ffname != NULL
2157 && mch_stat((char *)buf->bufp->b_ffname, &st) >= 0)
2158 buf_store_time(buf->bufp, &st, buf->bufp->b_ffname);
Bram Moolenaarff064e12008-06-09 13:10:45 +00002159 buf->bufp->b_changed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 }
2161 buf->modified = buf->bufp->b_changed;
Bram Moolenaarff064e12008-06-09 13:10:45 +00002162 if (prev_b_changed != buf->bufp->b_changed)
2163 {
2164#ifdef FEAT_WINDOWS
2165 check_status(buf->bufp);
2166 redraw_tabline = TRUE;
2167#endif
2168#ifdef FEAT_TITLE
2169 maketitle();
2170#endif
2171 update_screen(0);
2172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173/* =====================================================================*/
2174 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002175 else if (streq((char *)cmd, "setModtime"))
2176 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002177 if (buf == NULL || buf->bufp == NULL)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002178 nbdebug((" invalid buffer identifier in setModtime\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002179 else
2180 buf->bufp->b_mtime = atoi((char *)args);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002181/* =====================================================================*/
2182 }
2183 else if (streq((char *)cmd, "setReadOnly"))
2184 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002185 if (buf == NULL || buf->bufp == NULL)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002186 nbdebug((" invalid buffer identifier in setReadOnly\n"));
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002187 else if (streq((char *)args, "T"))
Bram Moolenaar009b2592004-10-24 19:18:58 +00002188 buf->bufp->b_p_ro = TRUE;
2189 else
2190 buf->bufp->b_p_ro = FALSE;
2191/* =====================================================================*/
2192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 else if (streq((char *)cmd, "setMark"))
2194 {
2195 /* not yet */
2196/* =====================================================================*/
2197 }
2198 else if (streq((char *)cmd, "showBalloon"))
2199 {
2200#if defined(FEAT_BEVAL)
2201 static char *text = NULL;
2202
2203 /*
2204 * Set up the Balloon Expression Evaluation area.
2205 * Ignore 'ballooneval' here.
2206 * The text pointer must remain valid for a while.
2207 */
2208 if (balloonEval != NULL)
2209 {
2210 vim_free(text);
2211 text = nb_unquote(args, NULL);
2212 if (text != NULL)
2213 gui_mch_post_balloon(balloonEval, (char_u *)text);
2214 }
2215#endif
2216/* =====================================================================*/
2217 }
2218 else if (streq((char *)cmd, "setDot"))
2219 {
2220 pos_T *pos;
2221#ifdef NBDEBUG
2222 char_u *s;
2223#endif
2224
2225 if (buf == NULL || buf->bufp == NULL)
2226 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002227 nbdebug((" invalid buffer identifier in setDot\n"));
2228 EMSG("E647: invalid buffer identifier in setDot");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 return FAIL;
2230 }
2231
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002232 nb_set_curbuf(buf->bufp);
2233
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234#ifdef FEAT_VISUAL
2235 /* Don't want Visual mode now. */
2236 if (VIsual_active)
2237 end_visual_mode();
2238#endif
2239#ifdef NBDEBUG
2240 s = args;
2241#endif
2242 pos = get_off_or_lnum(buf->bufp, &args);
2243 if (pos)
2244 {
2245 curwin->w_cursor = *pos;
2246 check_cursor();
2247#ifdef FEAT_FOLDING
2248 foldOpenCursor();
2249#endif
2250 }
2251 else
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002252 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 nbdebug((" BAD POSITION in setDot: %s\n", s));
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255
2256 /* gui_update_cursor(TRUE, FALSE); */
2257 /* update_curbuf(NOT_VALID); */
2258 update_topline(); /* scroll to show the line */
2259 update_screen(VALID);
2260 setcursor();
2261 out_flush();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002262#ifdef FEAT_GUI
Bram Moolenaard54a6882010-08-09 22:49:00 +02002263 if (gui.in_use)
2264 {
2265 gui_update_cursor(TRUE, FALSE);
2266 gui_mch_flush();
2267 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02002268#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 /* Quit a hit-return or more prompt. */
2270 if (State == HITRETURN || State == ASKMORE)
2271 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272#ifdef FEAT_GUI_GTK
Bram Moolenaard54a6882010-08-09 22:49:00 +02002273 if (gui.in_use && gtk_main_level() > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 gtk_main_quit();
2275#endif
2276 }
2277/* =====================================================================*/
2278 }
2279 else if (streq((char *)cmd, "close"))
2280 {
2281#ifdef NBDEBUG
2282 char *name = "<NONE>";
2283#endif
2284
2285 if (buf == NULL)
2286 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002287 nbdebug((" invalid buffer identifier in close\n"));
2288 EMSG("E648: invalid buffer identifier in close");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 return FAIL;
2290 }
2291
2292#ifdef NBDEBUG
2293 if (buf->displayname != NULL)
2294 name = buf->displayname;
2295#endif
Bram Moolenaarf2330482008-06-24 20:19:36 +00002296 if (buf->bufp == NULL)
2297 {
2298 nbdebug((" invalid buffer identifier in close\n"));
2299 /* This message was commented out, probably because it can
2300 * happen when shutting down. */
2301 if (p_verbose > 0)
2302 EMSG("E649: invalid buffer identifier in close");
2303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 nbdebug((" CLOSE %d: %s\n", bufno, name));
Bram Moolenaar67c53842010-05-22 18:28:27 +02002305#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 need_mouse_correct = TRUE;
Bram Moolenaar67c53842010-05-22 18:28:27 +02002307#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 if (buf->bufp != NULL)
2309 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD,
2310 buf->bufp->b_fnum, TRUE);
Bram Moolenaar8d602722006-08-08 19:34:19 +00002311 buf->bufp = NULL;
2312 buf->initDone = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 doupdate = 1;
2314/* =====================================================================*/
2315 }
2316 else if (streq((char *)cmd, "setStyle")) /* obsolete... */
2317 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002318 nbdebug((" setStyle is obsolete!\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319/* =====================================================================*/
2320 }
2321 else if (streq((char *)cmd, "setExitDelay"))
2322 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002323 /* Only used in version 2.1. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324/* =====================================================================*/
2325 }
2326 else if (streq((char *)cmd, "defineAnnoType"))
2327 {
2328#ifdef FEAT_SIGNS
2329 int typeNum;
2330 char_u *typeName;
2331 char_u *tooltip;
2332 char_u *p;
2333 char_u *glyphFile;
Bram Moolenaar67c53842010-05-22 18:28:27 +02002334 int parse_error = FALSE;
2335 char_u *fg;
2336 char_u *bg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337
2338 if (buf == NULL)
2339 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002340 nbdebug((" invalid buffer identifier in defineAnnoType\n"));
2341 EMSG("E650: invalid buffer identifier in defineAnnoType");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 return FAIL;
2343 }
2344
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002345 cp = (char *)args;
2346 typeNum = strtol(cp, &cp, 10);
2347 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 args = skipwhite(args);
2349 typeName = (char_u *)nb_unquote(args, &args);
2350 args = skipwhite(args + 1);
2351 tooltip = (char_u *)nb_unquote(args, &args);
2352 args = skipwhite(args + 1);
2353
2354 p = (char_u *)nb_unquote(args, &args);
2355 glyphFile = vim_strsave_escaped(p, escape_chars);
2356 vim_free(p);
2357
2358 args = skipwhite(args + 1);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002359 p = skiptowhite(args);
2360 if (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002362 *p = NUL;
2363 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02002365 fg = vim_strsave(args);
2366 bg = vim_strsave(p);
2367 if (STRLEN(fg) > MAX_COLOR_LENGTH || STRLEN(bg) > MAX_COLOR_LENGTH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002369 EMSG("E532: highlighting color name too long in defineAnnoType");
2370 vim_free(typeName);
2371 parse_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02002373 else if (typeName != NULL && tooltip != NULL && glyphFile != NULL)
2374 addsigntype(buf, typeNum, typeName, tooltip, glyphFile, fg, bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 else
2376 vim_free(typeName);
2377
2378 /* don't free typeName; it's used directly in addsigntype() */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002379 vim_free(fg);
2380 vim_free(bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 vim_free(tooltip);
2382 vim_free(glyphFile);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002383 if (parse_error)
2384 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385
2386#endif
2387/* =====================================================================*/
2388 }
2389 else if (streq((char *)cmd, "addAnno"))
2390 {
2391#ifdef FEAT_SIGNS
2392 int serNum;
2393 int localTypeNum;
2394 int typeNum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 pos_T *pos;
2396
2397 if (buf == NULL || buf->bufp == NULL)
2398 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002399 nbdebug((" invalid buffer identifier in addAnno\n"));
2400 EMSG("E651: invalid buffer identifier in addAnno");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 return FAIL;
2402 }
2403
2404 doupdate = 1;
2405
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002406 cp = (char *)args;
2407 serNum = strtol(cp, &cp, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408
2409 /* Get the typenr specific for this buffer and convert it to
2410 * the global typenumber, as used for the sign name. */
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002411 localTypeNum = strtol(cp, &cp, 10);
2412 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 typeNum = mapsigntype(buf, localTypeNum);
2414
2415 pos = get_off_or_lnum(buf->bufp, &args);
2416
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002417 cp = (char *)args;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002418 ignored = (int)strtol(cp, &cp, 10);
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002419 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420# ifdef NBDEBUG
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002421 if (ignored != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002423 nbdebug((" partial line annotation -- Not Yet Implemented!\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 }
2425# endif
2426 if (serNum >= GUARDEDOFFSET)
2427 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002428 nbdebug((" too many annotations! ignoring...\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 return FAIL;
2430 }
2431 if (pos)
2432 {
Bram Moolenaarec906222009-02-21 21:14:00 +00002433 coloncmd(":sign place %d line=%ld name=%d buffer=%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 serNum, pos->lnum, typeNum, buf->bufp->b_fnum);
2435 if (typeNum == curPCtype)
2436 coloncmd(":sign jump %d buffer=%d", serNum,
2437 buf->bufp->b_fnum);
2438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439#endif
2440/* =====================================================================*/
2441 }
2442 else if (streq((char *)cmd, "removeAnno"))
2443 {
2444#ifdef FEAT_SIGNS
2445 int serNum;
2446
2447 if (buf == NULL || buf->bufp == NULL)
2448 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002449 nbdebug((" invalid buffer identifier in removeAnno\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 return FAIL;
2451 }
2452 doupdate = 1;
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002453 cp = (char *)args;
2454 serNum = strtol(cp, &cp, 10);
2455 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 coloncmd(":sign unplace %d buffer=%d",
2457 serNum, buf->bufp->b_fnum);
2458 redraw_buf_later(buf->bufp, NOT_VALID);
2459#endif
2460/* =====================================================================*/
2461 }
2462 else if (streq((char *)cmd, "moveAnnoToFront"))
2463 {
2464#ifdef FEAT_SIGNS
Bram Moolenaarf2330482008-06-24 20:19:36 +00002465 nbdebug((" moveAnnoToFront: Not Yet Implemented!\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466#endif
2467/* =====================================================================*/
2468 }
2469 else if (streq((char *)cmd, "guard") || streq((char *)cmd, "unguard"))
2470 {
2471 int len;
2472 pos_T first;
2473 pos_T last;
2474 pos_T *pos;
2475 int un = (cmd[0] == 'u');
2476 static int guardId = GUARDEDOFFSET;
2477
2478 if (skip >= SKIP_STOP)
2479 {
2480 nbdebug((" Skipping %s command\n", (char *) cmd));
2481 return OK;
2482 }
2483
2484 nb_init_graphics();
2485
2486 if (buf == NULL || buf->bufp == NULL)
2487 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002488 nbdebug((" invalid buffer identifier in %s command\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 return FAIL;
2490 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002491 nb_set_curbuf(buf->bufp);
Bram Moolenaar349b2f62004-10-11 10:00:50 +00002492 cp = (char *)args;
2493 off = strtol(cp, &cp, 10);
2494 len = strtol(cp, NULL, 10);
2495 args = (char_u *)cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 pos = off2pos(buf->bufp, off);
2497 doupdate = 1;
2498 if (!pos)
2499 nbdebug((" no such start pos in %s, %ld\n", cmd, off));
2500 else
2501 {
2502 first = *pos;
2503 pos = off2pos(buf->bufp, off + len - 1);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002504 if (pos != NULL && pos->col == 0)
2505 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 /*
2507 * In Java Swing the offset is a position between 2
2508 * characters. If col == 0 then we really want the
2509 * previous line as the end.
2510 */
2511 pos = off2pos(buf->bufp, off + len - 2);
2512 }
2513 if (!pos)
2514 nbdebug((" no such end pos in %s, %ld\n",
2515 cmd, off + len - 1));
2516 else
2517 {
2518 long lnum;
2519 last = *pos;
2520 /* set highlight for region */
2521 nbdebug((" %sGUARD %ld,%d to %ld,%d\n", (un) ? "UN" : "",
2522 first.lnum, first.col,
2523 last.lnum, last.col));
2524#ifdef FEAT_SIGNS
2525 for (lnum = first.lnum; lnum <= last.lnum; lnum++)
2526 {
2527 if (un)
2528 {
2529 /* never used */
2530 }
2531 else
2532 {
2533 if (buf_findsigntype_id(buf->bufp, lnum,
2534 GUARDED) == 0)
2535 {
2536 coloncmd(
Bram Moolenaarec906222009-02-21 21:14:00 +00002537 ":sign place %d line=%ld name=%d buffer=%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 guardId++, lnum, GUARDED,
2539 buf->bufp->b_fnum);
2540 }
2541 }
2542 }
2543#endif
2544 redraw_buf_later(buf->bufp, NOT_VALID);
2545 }
2546 }
2547/* =====================================================================*/
2548 }
2549 else if (streq((char *)cmd, "startAtomic"))
2550 {
2551 inAtomic = 1;
2552/* =====================================================================*/
2553 }
2554 else if (streq((char *)cmd, "endAtomic"))
2555 {
2556 inAtomic = 0;
2557 if (needupdate)
2558 {
2559 doupdate = 1;
2560 needupdate = 0;
2561 }
2562/* =====================================================================*/
2563 }
2564 else if (streq((char *)cmd, "save"))
2565 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002566 /*
2567 * NOTE - This command is obsolete wrt NetBeans. Its left in
2568 * only for historical reasons.
2569 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 if (buf == NULL || buf->bufp == NULL)
2571 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002572 nbdebug((" invalid buffer identifier in %s command\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 return FAIL;
2574 }
2575
2576 /* the following is taken from ex_cmds.c (do_wqall function) */
2577 if (bufIsChanged(buf->bufp))
2578 {
2579 /* Only write if the buffer can be written. */
2580 if (p_write
2581 && !buf->bufp->b_p_ro
2582 && buf->bufp->b_ffname != NULL
2583#ifdef FEAT_QUICKFIX
2584 && !bt_dontwrite(buf->bufp)
2585#endif
2586 )
2587 {
2588 buf_write_all(buf->bufp, FALSE);
2589#ifdef FEAT_AUTOCMD
2590 /* an autocommand may have deleted the buffer */
2591 if (!buf_valid(buf->bufp))
2592 buf->bufp = NULL;
2593#endif
2594 }
2595 }
Bram Moolenaarf2330482008-06-24 20:19:36 +00002596 else
2597 {
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002598 nbdebug((" Buffer has no changes!\n"));
Bram Moolenaarf2330482008-06-24 20:19:36 +00002599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600/* =====================================================================*/
2601 }
2602 else if (streq((char *)cmd, "netbeansBuffer"))
2603 {
2604 if (buf == NULL || buf->bufp == NULL)
2605 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002606 nbdebug((" invalid buffer identifier in %s command\n", cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 return FAIL;
2608 }
2609 if (*args == 'T')
2610 {
2611 buf->bufp->b_netbeans_file = TRUE;
2612 buf->bufp->b_was_netbeans_file = TRUE;
2613 }
2614 else
2615 buf->bufp->b_netbeans_file = FALSE;
2616/* =====================================================================*/
2617 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00002618 else if (streq((char *)cmd, "specialKeys"))
2619 {
2620 special_keys(args);
2621/* =====================================================================*/
2622 }
2623 else if (streq((char *)cmd, "actionMenuItem"))
2624 {
2625 /* not used yet */
2626/* =====================================================================*/
2627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 else if (streq((char *)cmd, "version"))
2629 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00002630 /* not used yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 }
Bram Moolenaarf2330482008-06-24 20:19:36 +00002632 else
2633 {
2634 nbdebug(("Unrecognised command: %s\n", cmd));
2635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 /*
2637 * Unrecognized command is ignored.
2638 */
2639 }
2640 if (inAtomic && doupdate)
2641 {
2642 needupdate = 1;
2643 doupdate = 0;
2644 }
2645
Bram Moolenaar009b2592004-10-24 19:18:58 +00002646 /*
2647 * Is this needed? I moved the netbeans_Xt_connect() later during startup
2648 * and it may no longer be necessary. If its not needed then needupdate
2649 * and doupdate can also be removed.
2650 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 if (buf != NULL && buf->initDone && doupdate)
2652 {
2653 update_screen(NOT_VALID);
2654 setcursor();
2655 out_flush();
Bram Moolenaar67c53842010-05-22 18:28:27 +02002656#ifdef FEAT_GUI
Bram Moolenaard54a6882010-08-09 22:49:00 +02002657 if (gui.in_use)
2658 {
2659 gui_update_cursor(TRUE, FALSE);
2660 gui_mch_flush();
2661 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02002662#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 /* Quit a hit-return or more prompt. */
2664 if (State == HITRETURN || State == ASKMORE)
2665 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666#ifdef FEAT_GUI_GTK
Bram Moolenaard54a6882010-08-09 22:49:00 +02002667 if (gui.in_use && gtk_main_level() > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 gtk_main_quit();
2669#endif
2670 }
2671 }
2672
2673 return retval;
2674}
2675
2676
2677/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002678 * If "buf" is not the current buffer try changing to a window that edits this
2679 * buffer. If there is no such window then close the current buffer and set
2680 * the current buffer as "buf".
2681 */
2682 static void
Bram Moolenaar5eaf8722008-01-05 17:07:13 +00002683nb_set_curbuf(buf_T *buf)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002684{
2685 if (curbuf != buf && buf_jump_open_win(buf) == NULL)
2686 set_curbuf(buf, DOBUF_GOTO);
2687}
2688
2689/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 * Process a vim colon command.
2691 */
2692 static void
2693coloncmd(char *cmd, ...)
2694{
2695 char buf[1024];
2696 va_list ap;
2697
2698 va_start(ap, cmd);
Bram Moolenaar0dc79e82009-06-24 14:50:12 +00002699 vim_vsnprintf(buf, sizeof(buf), cmd, ap, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 va_end(ap);
2701
2702 nbdebug((" COLONCMD %s\n", buf));
2703
2704/* ALT_INPUT_LOCK_ON; */
2705 do_cmdline((char_u *)buf, NULL, NULL, DOCMD_NOWAIT | DOCMD_KEYTYPED);
2706/* ALT_INPUT_LOCK_OFF; */
2707
2708 setcursor(); /* restore the cursor position */
2709 out_flush(); /* make sure output has been written */
2710
Bram Moolenaar67c53842010-05-22 18:28:27 +02002711#ifdef FEAT_GUI
Bram Moolenaard54a6882010-08-09 22:49:00 +02002712 if (gui.in_use)
2713 {
2714 gui_update_cursor(TRUE, FALSE);
2715 gui_mch_flush();
2716 }
Bram Moolenaar67c53842010-05-22 18:28:27 +02002717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718}
2719
2720
2721/*
Bram Moolenaar009b2592004-10-24 19:18:58 +00002722 * Parse the specialKeys argument and issue the appropriate map commands.
2723 */
2724 static void
2725special_keys(char_u *args)
2726{
2727 char *save_str = nb_unquote(args, NULL);
2728 char *tok = strtok(save_str, " ");
2729 char *sep;
2730 char keybuf[64];
2731 char cmdbuf[256];
2732
2733 while (tok != NULL)
2734 {
2735 int i = 0;
2736
2737 if ((sep = strchr(tok, '-')) != NULL)
2738 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002739 *sep = NUL;
Bram Moolenaar009b2592004-10-24 19:18:58 +00002740 while (*tok)
2741 {
2742 switch (*tok)
2743 {
2744 case 'A':
2745 case 'M':
2746 case 'C':
2747 case 'S':
2748 keybuf[i++] = *tok;
2749 keybuf[i++] = '-';
2750 break;
2751 }
2752 tok++;
2753 }
2754 tok++;
2755 }
2756
2757 strcpy(&keybuf[i], tok);
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002758 vim_snprintf(cmdbuf, sizeof(cmdbuf),
2759 "<silent><%s> :nbkey %s<CR>", keybuf, keybuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002760 do_map(0, (char_u *)cmdbuf, NORMAL, FALSE);
2761 tok = strtok(NULL, " ");
2762 }
2763 vim_free(save_str);
2764}
2765
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002766 void
2767ex_nbclose(eap)
2768 exarg_T *eap UNUSED;
2769{
2770 netbeans_close();
2771}
Bram Moolenaar009b2592004-10-24 19:18:58 +00002772
2773 void
2774ex_nbkey(eap)
2775 exarg_T *eap;
2776{
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002777 (void)netbeans_keystring(eap->arg);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002778}
2779
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002780 void
2781ex_nbstart(eap)
2782 exarg_T *eap;
2783{
Bram Moolenaarc2a406b2010-09-30 21:03:26 +02002784#ifdef FEAT_GUI
2785# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar7ad7d012010-11-16 15:49:02 +01002786 && !defined(FEAT_GUI_W32)
Bram Moolenaarc2a406b2010-09-30 21:03:26 +02002787 if (gui.in_use)
2788 {
Bram Moolenaar7ad7d012010-11-16 15:49:02 +01002789 EMSG(_("E838: netbeans is not supported with this GUI"));
2790 return;
Bram Moolenaarc2a406b2010-09-30 21:03:26 +02002791 }
2792# endif
2793#endif
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002794 netbeans_open((char *)eap->arg, FALSE);
2795}
Bram Moolenaar009b2592004-10-24 19:18:58 +00002796
2797/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 * Initialize highlights and signs for use by netbeans (mostly obsolete)
2799 */
2800 static void
2801nb_init_graphics(void)
2802{
2803 static int did_init = FALSE;
2804
2805 if (!did_init)
2806 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002807 coloncmd(":highlight NBGuarded guibg=Cyan guifg=Black"
2808 " ctermbg=LightCyan ctermfg=Black");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 coloncmd(":sign define %d linehl=NBGuarded", GUARDED);
2810
2811 did_init = TRUE;
2812 }
2813}
2814
2815/*
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01002816 * Convert key to netbeans name. This uses the global "mod_mask".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 */
2818 static void
2819netbeans_keyname(int key, char *buf)
2820{
2821 char *name = 0;
2822 char namebuf[2];
2823 int ctrl = 0;
2824 int shift = 0;
2825 int alt = 0;
2826
2827 if (mod_mask & MOD_MASK_CTRL)
2828 ctrl = 1;
2829 if (mod_mask & MOD_MASK_SHIFT)
2830 shift = 1;
2831 if (mod_mask & MOD_MASK_ALT)
2832 alt = 1;
2833
2834
2835 switch (key)
2836 {
2837 case K_F1: name = "F1"; break;
2838 case K_S_F1: name = "F1"; shift = 1; break;
2839 case K_F2: name = "F2"; break;
2840 case K_S_F2: name = "F2"; shift = 1; break;
2841 case K_F3: name = "F3"; break;
2842 case K_S_F3: name = "F3"; shift = 1; break;
2843 case K_F4: name = "F4"; break;
2844 case K_S_F4: name = "F4"; shift = 1; break;
2845 case K_F5: name = "F5"; break;
2846 case K_S_F5: name = "F5"; shift = 1; break;
2847 case K_F6: name = "F6"; break;
2848 case K_S_F6: name = "F6"; shift = 1; break;
2849 case K_F7: name = "F7"; break;
2850 case K_S_F7: name = "F7"; shift = 1; break;
2851 case K_F8: name = "F8"; break;
2852 case K_S_F8: name = "F8"; shift = 1; break;
2853 case K_F9: name = "F9"; break;
2854 case K_S_F9: name = "F9"; shift = 1; break;
2855 case K_F10: name = "F10"; break;
2856 case K_S_F10: name = "F10"; shift = 1; break;
2857 case K_F11: name = "F11"; break;
2858 case K_S_F11: name = "F11"; shift = 1; break;
2859 case K_F12: name = "F12"; break;
2860 case K_S_F12: name = "F12"; shift = 1; break;
2861 default:
2862 if (key >= ' ' && key <= '~')
2863 {
2864 /* Allow ASCII characters. */
2865 name = namebuf;
2866 namebuf[0] = key;
2867 namebuf[1] = NUL;
2868 }
2869 else
2870 name = "X";
2871 break;
2872 }
2873
2874 buf[0] = '\0';
2875 if (ctrl)
2876 strcat(buf, "C");
2877 if (shift)
2878 strcat(buf, "S");
2879 if (alt)
2880 strcat(buf, "M"); /* META */
2881 if (ctrl || shift || alt)
2882 strcat(buf, "-");
2883 strcat(buf, name);
2884}
2885
Bram Moolenaar67c53842010-05-22 18:28:27 +02002886#if defined(FEAT_BEVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887/*
2888 * Function to be called for balloon evaluation. Grabs the text under the
2889 * cursor and sends it to the debugger for evaluation. The debugger should
2890 * respond with a showBalloon command when there is a useful result.
2891 */
Bram Moolenaard62bec82005-03-07 22:56:57 +00002892 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893netbeans_beval_cb(
2894 BalloonEval *beval,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002895 int state UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896{
Bram Moolenaard62bec82005-03-07 22:56:57 +00002897 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 char_u *text;
Bram Moolenaard62bec82005-03-07 22:56:57 +00002899 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 int col;
2901 char buf[MAXPATHL * 2 + 25];
2902 char_u *p;
2903
2904 /* Don't do anything when 'ballooneval' is off, messages scrolled the
2905 * windows up or we have no connection. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002906 if (!p_beval || msg_scrolled > 0 || !NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 return;
2908
Bram Moolenaard62bec82005-03-07 22:56:57 +00002909 if (get_beval_info(beval, TRUE, &wp, &lnum, &text, &col) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 {
2911 /* Send debugger request. Only when the text is of reasonable
2912 * length. */
2913 if (text != NULL && text[0] != NUL && STRLEN(text) < MAXPATHL)
2914 {
2915 p = nb_quote(text);
2916 if (p != NULL)
Bram Moolenaar009b2592004-10-24 19:18:58 +00002917 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002918 vim_snprintf(buf, sizeof(buf),
Bram Moolenaar89d40322006-08-29 15:30:07 +00002919 "0:balloonText=%d \"%s\"\n", r_cmdno, p);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002920 vim_free(p);
2921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 nbdebug(("EVT: %s", buf));
2923 nb_send(buf, "netbeans_beval_cb");
2924 }
2925 vim_free(text);
2926 }
2927}
2928#endif
2929
2930/*
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002931 * Return TRUE when the netbeans connection is closed.
2932 */
2933 int
2934netbeans_active(void)
2935{
2936 return NETBEANS_OPEN;
2937}
2938
2939/*
Bram Moolenaar67c53842010-05-22 18:28:27 +02002940 * Return netbeans file descriptor.
2941 */
2942 int
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002943netbeans_filedesc(void)
Bram Moolenaar67c53842010-05-22 18:28:27 +02002944{
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002945 return nbsock;
Bram Moolenaar67c53842010-05-22 18:28:27 +02002946}
2947
2948#if defined(FEAT_GUI) || defined(PROTO)
2949/*
2950 * Register our file descriptor with the gui event handling system.
2951 */
2952 void
2953netbeans_gui_register(void)
2954{
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002955 if (!NB_HAS_GUI || !NETBEANS_OPEN)
Bram Moolenaar67c53842010-05-22 18:28:27 +02002956 return;
2957
Bram Moolenaar173c9852010-09-29 17:27:01 +02002958# ifdef FEAT_GUI_X11
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002959 /* tell notifier we are interested in being called
2960 * when there is input on the editor connection socket
2961 */
2962 if (inputHandler == (XtInputId)NULL)
2963 inputHandler = XtAppAddInput((XtAppContext)app_context, nbsock,
2964 (XtPointer)(XtInputReadMask + XtInputExceptMask),
2965 messageFromNetbeans, NULL);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002966# else
2967# ifdef FEAT_GUI_GTK
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002968 /*
2969 * Tell gdk we are interested in being called when there
2970 * is input on the editor connection socket
2971 */
2972 if (inputHandler == 0)
2973 inputHandler = gdk_input_add((gint)nbsock, (GdkInputCondition)
2974 ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION),
2975 messageFromNetbeans, NULL);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002976# else
2977# ifdef FEAT_GUI_W32
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002978 /*
2979 * Tell Windows we are interested in receiving message when there
2980 * is input on the editor connection socket
2981 */
2982 if (inputHandler == -1)
2983 inputHandler = WSAAsyncSelect(nbsock, s_hwnd, WM_NETBEANS, FD_READ);
Bram Moolenaar67c53842010-05-22 18:28:27 +02002984# endif
2985# endif
2986# endif
Bram Moolenaar67c53842010-05-22 18:28:27 +02002987
2988# ifdef FEAT_BEVAL
2989 bevalServers |= BEVAL_NETBEANS;
2990# endif
2991}
2992#endif
2993
2994/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 * Tell netbeans that the window was opened, ready for commands.
2996 */
2997 void
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02002998netbeans_open(char *params, int doabort)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999{
3000 char *cmd = "0:startupDone=0\n";
3001
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003002 if (NETBEANS_OPEN)
3003 {
3004 EMSG(_("E511: netbeans already connected"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003008 if (netbeans_connect(params, doabort) != OK)
Bram Moolenaar67c53842010-05-22 18:28:27 +02003009 return;
3010#ifdef FEAT_GUI
3011 netbeans_gui_register();
Bram Moolenaard62bec82005-03-07 22:56:57 +00003012#endif
3013
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 nbdebug(("EVT: %s", cmd));
3015 nb_send(cmd, "netbeans_startup_done");
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003016
3017 /* update the screen after having added the gutter */
3018 changed_window_setting();
3019 update_screen(CLEAR);
3020 setcursor();
3021 out_flush();
3022#ifdef FEAT_GUI
Bram Moolenaard54a6882010-08-09 22:49:00 +02003023 if (gui.in_use)
3024 {
3025 gui_update_cursor(TRUE, FALSE);
3026 gui_mch_flush();
3027 }
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003028#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029}
3030
Bram Moolenaar009b2592004-10-24 19:18:58 +00003031/*
3032 * Tell netbeans that we're exiting. This should be called right
3033 * before calling exit.
3034 */
3035 void
3036netbeans_send_disconnect()
3037{
3038 char buf[128];
3039
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003040 if (NETBEANS_OPEN)
Bram Moolenaar009b2592004-10-24 19:18:58 +00003041 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00003042 sprintf(buf, "0:disconnect=%d\n", r_cmdno);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003043 nbdebug(("EVT: %s", buf));
3044 nb_send(buf, "netbeans_disconnect");
3045 }
3046}
3047
Bram Moolenaar173c9852010-09-29 17:27:01 +02003048#if defined(FEAT_GUI_X11) || defined(FEAT_GUI_W32) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049/*
3050 * Tell netbeans that the window was moved or resized.
3051 */
3052 void
3053netbeans_frame_moved(int new_x, int new_y)
3054{
3055 char buf[128];
3056
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003057 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 return;
3059
3060 sprintf(buf, "0:geometry=%d %d %d %d %d\n",
Bram Moolenaar89d40322006-08-29 15:30:07 +00003061 r_cmdno, (int)Columns, (int)Rows, new_x, new_y);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003062 /*nbdebug(("EVT: %s", buf)); happens too many times during a move */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 nb_send(buf, "netbeans_frame_moved");
3064}
3065#endif
3066
3067/*
Bram Moolenaar009b2592004-10-24 19:18:58 +00003068 * Tell netbeans the user opened or activated a file.
3069 */
3070 void
3071netbeans_file_activated(buf_T *bufp)
3072{
3073 int bufno = nb_getbufno(bufp);
3074 nbbuf_T *bp = nb_get_buf(bufno);
3075 char buffer[2*MAXPATHL];
3076 char_u *q;
3077
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003078 if (!NETBEANS_OPEN || !bufp->b_netbeans_file || dosetvisible)
Bram Moolenaar009b2592004-10-24 19:18:58 +00003079 return;
3080
3081 q = nb_quote(bufp->b_ffname);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003082 if (q == NULL || bp == NULL)
Bram Moolenaar009b2592004-10-24 19:18:58 +00003083 return;
3084
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003085 vim_snprintf(buffer, sizeof(buffer), "%d:fileOpened=%d \"%s\" %s %s\n",
Bram Moolenaar009b2592004-10-24 19:18:58 +00003086 bufno,
3087 bufno,
3088 (char *)q,
3089 "T", /* open in NetBeans */
3090 "F"); /* modified */
3091
3092 vim_free(q);
3093 nbdebug(("EVT: %s", buffer));
3094
3095 nb_send(buffer, "netbeans_file_opened");
3096}
3097
3098/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 * Tell netbeans the user opened a file.
3100 */
3101 void
Bram Moolenaar009b2592004-10-24 19:18:58 +00003102netbeans_file_opened(buf_T *bufp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103{
Bram Moolenaar009b2592004-10-24 19:18:58 +00003104 int bufno = nb_getbufno(bufp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 char buffer[2*MAXPATHL];
3106 char_u *q;
Bram Moolenaar009b2592004-10-24 19:18:58 +00003107 nbbuf_T *bp = nb_get_buf(nb_getbufno(bufp));
3108 int bnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003110 if (!NETBEANS_OPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 return;
3112
Bram Moolenaar009b2592004-10-24 19:18:58 +00003113 q = nb_quote(bufp->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 if (q == NULL)
3115 return;
Bram Moolenaar009b2592004-10-24 19:18:58 +00003116 if (bp != NULL)
3117 bnum = bufno;
3118 else
3119 bnum = 0;
3120
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003121 vim_snprintf(buffer, sizeof(buffer), "%d:fileOpened=%d \"%s\" %s %s\n",
Bram Moolenaar009b2592004-10-24 19:18:58 +00003122 bnum,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 0,
3124 (char *)q,
3125 "T", /* open in NetBeans */
3126 "F"); /* modified */
3127
3128 vim_free(q);
3129 nbdebug(("EVT: %s", buffer));
3130
3131 nb_send(buffer, "netbeans_file_opened");
Bram Moolenaar009b2592004-10-24 19:18:58 +00003132 if (p_acd && vim_chdirfile(bufp->b_ffname) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 shorten_fnames(TRUE);
3134}
3135
3136/*
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00003137 * Tell netbeans that a file was deleted or wiped out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 */
3139 void
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00003140netbeans_file_killed(buf_T *bufp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141{
3142 int bufno = nb_getbufno(bufp);
3143 nbbuf_T *nbbuf = nb_get_buf(bufno);
3144 char buffer[2*MAXPATHL];
3145
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003146 if (!NETBEANS_OPEN || bufno == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 return;
3148
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00003149 nbdebug(("netbeans_file_killed:\n"));
3150 nbdebug((" Killing bufno: %d", bufno));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151
Bram Moolenaar89d40322006-08-29 15:30:07 +00003152 sprintf(buffer, "%d:killed=%d\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153
3154 nbdebug(("EVT: %s", buffer));
3155
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00003156 nb_send(buffer, "netbeans_file_killed");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157
3158 if (nbbuf != NULL)
3159 nbbuf->bufp = NULL;
3160}
3161
3162/*
3163 * Get a pointer to the Netbeans buffer for Vim buffer "bufp".
3164 * Return NULL if there is no such buffer or changes are not to be reported.
3165 * Otherwise store the buffer number in "*bufnop".
3166 */
3167 static nbbuf_T *
3168nb_bufp2nbbuf_fire(buf_T *bufp, int *bufnop)
3169{
3170 int bufno;
3171 nbbuf_T *nbbuf;
3172
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003173 if (!NETBEANS_OPEN || !netbeansFireChanges)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 return NULL; /* changes are not reported at all */
3175
3176 bufno = nb_getbufno(bufp);
3177 if (bufno <= 0)
3178 return NULL; /* file is not known to NetBeans */
3179
3180 nbbuf = nb_get_buf(bufno);
3181 if (nbbuf != NULL && !nbbuf->fireChanges)
3182 return NULL; /* changes in this buffer are not reported */
3183
3184 *bufnop = bufno;
3185 return nbbuf;
3186}
3187
3188/*
3189 * Tell netbeans the user inserted some text.
3190 */
3191 void
3192netbeans_inserted(
3193 buf_T *bufp,
3194 linenr_T linenr,
3195 colnr_T col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 char_u *txt,
3197 int newlen)
3198{
3199 char_u *buf;
3200 int bufno;
3201 nbbuf_T *nbbuf;
3202 pos_T pos;
3203 long off;
3204 char_u *p;
3205 char_u *newtxt;
3206
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003207 if (!NETBEANS_OPEN)
3208 return;
3209
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3211 if (nbbuf == NULL)
3212 return;
3213
Bram Moolenaar009b2592004-10-24 19:18:58 +00003214 /* Don't mark as modified for initial read */
3215 if (nbbuf->insertDone)
3216 nbbuf->modified = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217
3218 pos.lnum = linenr;
3219 pos.col = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 off = pos2off(bufp, &pos);
3221
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 /* send the "insert" EVT */
3223 newtxt = alloc(newlen + 1);
Bram Moolenaarb6356332005-07-18 21:40:44 +00003224 vim_strncpy(newtxt, txt, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 p = nb_quote(newtxt);
3226 if (p != NULL)
3227 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00003228 buf = alloc(128 + 2*newlen);
Bram Moolenaar89d40322006-08-29 15:30:07 +00003229 sprintf((char *)buf, "%d:insert=%d %ld \"%s\"\n",
3230 bufno, r_cmdno, off, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 nbdebug(("EVT: %s", buf));
3232 nb_send((char *)buf, "netbeans_inserted");
Bram Moolenaar009b2592004-10-24 19:18:58 +00003233 vim_free(p);
3234 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 vim_free(newtxt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237}
3238
3239/*
3240 * Tell netbeans some bytes have been removed.
3241 */
3242 void
3243netbeans_removed(
3244 buf_T *bufp,
3245 linenr_T linenr,
3246 colnr_T col,
3247 long len)
3248{
3249 char_u buf[128];
3250 int bufno;
3251 nbbuf_T *nbbuf;
3252 pos_T pos;
3253 long off;
3254
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003255 if (!NETBEANS_OPEN)
3256 return;
3257
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3259 if (nbbuf == NULL)
3260 return;
3261
3262 if (len < 0)
3263 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00003264 nbdebug(("Negative len %ld in netbeans_removed()!\n", len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 return;
3266 }
3267
3268 nbbuf->modified = 1;
3269
3270 pos.lnum = linenr;
3271 pos.col = col;
3272
3273 off = pos2off(bufp, &pos);
3274
Bram Moolenaar89d40322006-08-29 15:30:07 +00003275 sprintf((char *)buf, "%d:remove=%d %ld %ld\n", bufno, r_cmdno, off, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 nbdebug(("EVT: %s", buf));
3277 nb_send((char *)buf, "netbeans_removed");
3278}
3279
3280/*
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003281 * Send netbeans an unmodified command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003284netbeans_unmodified(buf_T *bufp UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285{
Bram Moolenaar09092152010-08-08 16:38:42 +02003286 /* This is a no-op, because NetBeans considers a buffer modified
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 * even when all changes have been undone. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288}
3289
3290/*
3291 * Send a button release event back to netbeans. Its up to netbeans
3292 * to decide what to do (if anything) with this event.
3293 */
3294 void
3295netbeans_button_release(int button)
3296{
3297 char buf[128];
3298 int bufno;
3299
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003300 if (!NETBEANS_OPEN)
3301 return;
3302
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 bufno = nb_getbufno(curbuf);
3304
3305 if (bufno >= 0 && curwin != NULL && curwin->w_buffer == curbuf)
3306 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003307 int col = mouse_col - W_WINCOL(curwin)
Bram Moolenaar67c53842010-05-22 18:28:27 +02003308 - ((curwin->w_p_nu || curwin->w_p_rnu) ? 9 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 long off = pos2off(curbuf, &curwin->w_cursor);
3310
3311 /* sync the cursor position */
Bram Moolenaar89d40322006-08-29 15:30:07 +00003312 sprintf(buf, "%d:newDotAndMark=%d %ld %ld\n", bufno, r_cmdno, off, off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 nbdebug(("EVT: %s", buf));
3314 nb_send(buf, "netbeans_button_release[newDotAndMark]");
3315
Bram Moolenaar89d40322006-08-29 15:30:07 +00003316 sprintf(buf, "%d:buttonRelease=%d %d %ld %d\n", bufno, r_cmdno,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 button, (long)curwin->w_cursor.lnum, col);
3318 nbdebug(("EVT: %s", buf));
3319 nb_send(buf, "netbeans_button_release");
3320 }
3321}
3322
3323
3324/*
Bram Moolenaar143c38c2007-05-10 16:41:10 +00003325 * Send a keypress event back to netbeans. This usually simulates some
Bram Moolenaar009b2592004-10-24 19:18:58 +00003326 * kind of function key press. This function operates on a key code.
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003327 * Return TRUE when the key was sent, FALSE when the command has been
3328 * postponed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 */
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003330 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331netbeans_keycommand(int key)
3332{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 char keyName[60];
Bram Moolenaar009b2592004-10-24 19:18:58 +00003334
3335 netbeans_keyname(key, keyName);
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003336 return netbeans_keystring((char_u *)keyName);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003337}
3338
3339
3340/*
Bram Moolenaar143c38c2007-05-10 16:41:10 +00003341 * Send a keypress event back to netbeans. This usually simulates some
Bram Moolenaar009b2592004-10-24 19:18:58 +00003342 * kind of function key press. This function operates on a key string.
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003343 * Return TRUE when the key was sent, FALSE when the command has been
3344 * postponed.
Bram Moolenaar009b2592004-10-24 19:18:58 +00003345 */
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003346 static int
3347netbeans_keystring(char_u *keyName)
Bram Moolenaar009b2592004-10-24 19:18:58 +00003348{
3349 char buf[2*MAXPATHL];
3350 int bufno = nb_getbufno(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 long off;
3352 char_u *q;
3353
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003354 if (!NETBEANS_OPEN)
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003355 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 if (bufno == -1)
3358 {
3359 nbdebug(("got keycommand for non-NetBeans buffer, opening...\n"));
3360 q = curbuf->b_ffname == NULL ? (char_u *)""
3361 : nb_quote(curbuf->b_ffname);
3362 if (q == NULL)
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003363 return TRUE;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003364 vim_snprintf(buf, sizeof(buf), "0:fileOpened=%d \"%s\" %s %s\n", 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 q,
3366 "T", /* open in NetBeans */
3367 "F"); /* modified */
3368 if (curbuf->b_ffname != NULL)
3369 vim_free(q);
3370 nbdebug(("EVT: %s", buf));
3371 nb_send(buf, "netbeans_keycommand");
3372
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003373 postpone_keycommand(keyName);
3374 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 }
3376
3377 /* sync the cursor position */
3378 off = pos2off(curbuf, &curwin->w_cursor);
Bram Moolenaar89d40322006-08-29 15:30:07 +00003379 sprintf(buf, "%d:newDotAndMark=%d %ld %ld\n", bufno, r_cmdno, off, off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 nbdebug(("EVT: %s", buf));
3381 nb_send(buf, "netbeans_keycommand");
3382
3383 /* To work on Win32 you must apply patch to ExtEditor module
3384 * from ExtEdCaret.java.diff - make EVT_newDotAndMark handler
3385 * more synchronous
3386 */
3387
3388 /* now send keyCommand event */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003389 vim_snprintf(buf, sizeof(buf), "%d:keyCommand=%d \"%s\"\n",
Bram Moolenaar89d40322006-08-29 15:30:07 +00003390 bufno, r_cmdno, keyName);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 nbdebug(("EVT: %s", buf));
3392 nb_send(buf, "netbeans_keycommand");
3393
3394 /* New: do both at once and include the lnum/col. */
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003395 vim_snprintf(buf, sizeof(buf), "%d:keyAtPos=%d \"%s\" %ld %ld/%ld\n",
Bram Moolenaar89d40322006-08-29 15:30:07 +00003396 bufno, r_cmdno, keyName,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 off, (long)curwin->w_cursor.lnum, (long)curwin->w_cursor.col);
3398 nbdebug(("EVT: %s", buf));
3399 nb_send(buf, "netbeans_keycommand");
Bram Moolenaar8065d7f2010-01-19 15:13:14 +01003400 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401}
3402
3403
3404/*
3405 * Send a save event to netbeans.
3406 */
3407 void
3408netbeans_save_buffer(buf_T *bufp)
3409{
3410 char_u buf[64];
3411 int bufno;
3412 nbbuf_T *nbbuf;
3413
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003414 if (!NETBEANS_OPEN)
3415 return;
3416
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3418 if (nbbuf == NULL)
3419 return;
3420
3421 nbbuf->modified = 0;
3422
Bram Moolenaar89d40322006-08-29 15:30:07 +00003423 sprintf((char *)buf, "%d:save=%d\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 nbdebug(("EVT: %s", buf));
3425 nb_send((char *)buf, "netbeans_save_buffer");
3426}
3427
3428
3429/*
3430 * Send remove command to netbeans (this command has been turned off).
3431 */
3432 void
3433netbeans_deleted_all_lines(buf_T *bufp)
3434{
3435 char_u buf[64];
3436 int bufno;
3437 nbbuf_T *nbbuf;
3438
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003439 if (!NETBEANS_OPEN)
3440 return;
3441
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno);
3443 if (nbbuf == NULL)
3444 return;
3445
Bram Moolenaar009b2592004-10-24 19:18:58 +00003446 /* Don't mark as modified for initial read */
3447 if (nbbuf->insertDone)
3448 nbbuf->modified = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449
Bram Moolenaar89d40322006-08-29 15:30:07 +00003450 sprintf((char *)buf, "%d:remove=%d 0 -1\n", bufno, r_cmdno);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 nbdebug(("EVT(suppressed): %s", buf));
3452/* nb_send(buf, "netbeans_deleted_all_lines"); */
3453}
3454
3455
3456/*
3457 * See if the lines are guarded. The top and bot parameters are from
3458 * u_savecommon(), these are the line above the change and the line below the
3459 * change.
3460 */
3461 int
3462netbeans_is_guarded(linenr_T top, linenr_T bot)
3463{
3464 signlist_T *p;
3465 int lnum;
3466
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003467 if (!NETBEANS_OPEN)
3468 return FALSE;
3469
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 for (p = curbuf->b_signlist; p != NULL; p = p->next)
3471 if (p->id >= GUARDEDOFFSET)
3472 for (lnum = top + 1; lnum < bot; lnum++)
3473 if (lnum == p->lnum)
3474 return TRUE;
3475
3476 return FALSE;
3477}
3478
Bram Moolenaar173c9852010-09-29 17:27:01 +02003479#if defined(FEAT_GUI_X11) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480/*
3481 * We have multiple signs to draw at the same location. Draw the
3482 * multi-sign indicator instead. This is the Motif version.
3483 */
3484 void
3485netbeans_draw_multisign_indicator(int row)
3486{
3487 int i;
3488 int y;
3489 int x;
3490
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003491 if (!NETBEANS_OPEN)
3492 return;
3493
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 x = 0;
3495 y = row * gui.char_height + 2;
3496
3497 for (i = 0; i < gui.char_height - 3; i++)
3498 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y++);
3499
3500 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+0, y);
3501 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3502 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+4, y++);
3503 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+1, y);
3504 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3505 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+3, y++);
3506 XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y);
3507}
Bram Moolenaar173c9852010-09-29 17:27:01 +02003508#endif /* FEAT_GUI_X11 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509
Bram Moolenaar64404472010-06-26 06:24:45 +02003510#if defined(FEAT_GUI_GTK) && !defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511/*
3512 * We have multiple signs to draw at the same location. Draw the
3513 * multi-sign indicator instead. This is the GTK/Gnome version.
3514 */
3515 void
3516netbeans_draw_multisign_indicator(int row)
3517{
3518 int i;
3519 int y;
3520 int x;
3521 GdkDrawable *drawable = gui.drawarea->window;
3522
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003523 if (!NETBEANS_OPEN)
3524 return;
3525
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 x = 0;
3527 y = row * gui.char_height + 2;
3528
3529 for (i = 0; i < gui.char_height - 3; i++)
3530 gdk_draw_point(drawable, gui.text_gc, x+2, y++);
3531
3532 gdk_draw_point(drawable, gui.text_gc, x+0, y);
3533 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3534 gdk_draw_point(drawable, gui.text_gc, x+4, y++);
3535 gdk_draw_point(drawable, gui.text_gc, x+1, y);
3536 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3537 gdk_draw_point(drawable, gui.text_gc, x+3, y++);
3538 gdk_draw_point(drawable, gui.text_gc, x+2, y);
3539}
3540#endif /* FEAT_GUI_GTK */
3541
3542/*
3543 * If the mouse is clicked in the gutter of a line with multiple
3544 * annotations, cycle through the set of signs.
3545 */
3546 void
3547netbeans_gutter_click(linenr_T lnum)
3548{
3549 signlist_T *p;
3550
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003551 if (!NETBEANS_OPEN)
3552 return;
3553
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 for (p = curbuf->b_signlist; p != NULL; p = p->next)
3555 {
3556 if (p->lnum == lnum && p->next && p->next->lnum == lnum)
3557 {
3558 signlist_T *tail;
3559
3560 /* remove "p" from list, reinsert it at the tail of the sublist */
3561 if (p->prev)
3562 p->prev->next = p->next;
3563 else
3564 curbuf->b_signlist = p->next;
3565 p->next->prev = p->prev;
3566 /* now find end of sublist and insert p */
3567 for (tail = p->next;
3568 tail->next && tail->next->lnum == lnum
3569 && tail->next->id < GUARDEDOFFSET;
3570 tail = tail->next)
3571 ;
3572 /* tail now points to last entry with same lnum (except
3573 * that "guarded" annotations are always last) */
3574 p->next = tail->next;
3575 if (tail->next)
3576 tail->next->prev = p;
3577 p->prev = tail;
3578 tail->next = p;
3579 update_debug_sign(curbuf, lnum);
3580 break;
3581 }
3582 }
3583}
3584
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585/*
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003586 * Add a sign of the requested type at the requested location.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 *
3588 * Reverse engineering:
3589 * Apparently an annotation is defined the first time it is used in a buffer.
3590 * When the same annotation is used in two buffers, the second time we do not
3591 * need to define a new sign name but reuse the existing one. But since the
3592 * ID number used in the second buffer starts counting at one again, a mapping
3593 * is made from the ID specifically for the buffer to the global sign name
3594 * (which is a number).
3595 *
3596 * globalsignmap[] stores the signs that have been defined globally.
3597 * buf->signmapused[] maps buffer-local annotation IDs to an index in
3598 * globalsignmap[].
3599 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 static void
3601addsigntype(
3602 nbbuf_T *buf,
3603 int typeNum,
3604 char_u *typeName,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003605 char_u *tooltip UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 char_u *glyphFile,
Bram Moolenaar67c53842010-05-22 18:28:27 +02003607 char_u *fg,
3608 char_u *bg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 int i, j;
Bram Moolenaar67c53842010-05-22 18:28:27 +02003611 int use_fg = (*fg && STRCMP(fg, "none") != 0);
3612 int use_bg = (*bg && STRCMP(bg, "none") != 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613
3614 for (i = 0; i < globalsignmapused; i++)
3615 if (STRCMP(typeName, globalsignmap[i]) == 0)
3616 break;
3617
3618 if (i == globalsignmapused) /* not found; add it to global map */
3619 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003620 nbdebug(("DEFINEANNOTYPE(%d,%s,%s,%s,%s,%s)\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 typeNum, typeName, tooltip, glyphFile, fg, bg));
3622 if (use_fg || use_bg)
3623 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003624 char fgbuf[2 * (8 + MAX_COLOR_LENGTH) + 1];
3625 char bgbuf[2 * (8 + MAX_COLOR_LENGTH) + 1];
3626 char *ptr;
3627 int value;
3628
3629 value = strtol((char *)fg, &ptr, 10);
3630 if (ptr != (char *)fg)
3631 sprintf(fgbuf, "guifg=#%06x", value & 0xFFFFFF);
3632 else
3633 sprintf(fgbuf, "guifg=%s ctermfg=%s", fg, fg);
3634
3635 value = strtol((char *)bg, &ptr, 10);
3636 if (ptr != (char *)bg)
3637 sprintf(bgbuf, "guibg=#%06x", value & 0xFFFFFF);
3638 else
3639 sprintf(bgbuf, "guibg=%s ctermbg=%s", bg, bg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640
3641 coloncmd(":highlight NB_%s %s %s", typeName, (use_fg) ? fgbuf : "",
3642 (use_bg) ? bgbuf : "");
3643 if (*glyphFile == NUL)
3644 /* no glyph, line highlighting only */
3645 coloncmd(":sign define %d linehl=NB_%s", i + 1, typeName);
3646 else if (vim_strsize(glyphFile) <= 2)
3647 /* one- or two-character glyph name, use as text glyph with
3648 * texthl */
3649 coloncmd(":sign define %d text=%s texthl=NB_%s", i + 1,
3650 glyphFile, typeName);
3651 else
3652 /* glyph, line highlighting */
3653 coloncmd(":sign define %d icon=%s linehl=NB_%s", i + 1,
3654 glyphFile, typeName);
3655 }
3656 else
3657 /* glyph, no line highlighting */
3658 coloncmd(":sign define %d icon=%s", i + 1, glyphFile);
3659
3660 if (STRCMP(typeName,"CurrentPC") == 0)
3661 curPCtype = typeNum;
3662
3663 if (globalsignmapused == globalsignmaplen)
3664 {
3665 if (globalsignmaplen == 0) /* first allocation */
3666 {
3667 globalsignmaplen = 20;
3668 globalsignmap = (char **)alloc_clear(globalsignmaplen*sizeof(char *));
3669 }
3670 else /* grow it */
3671 {
3672 int incr;
3673 int oldlen = globalsignmaplen;
3674
3675 globalsignmaplen *= 2;
3676 incr = globalsignmaplen - oldlen;
3677 globalsignmap = (char **)vim_realloc(globalsignmap,
3678 globalsignmaplen * sizeof(char *));
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003679 vim_memset(globalsignmap + oldlen, 0, incr * sizeof(char *));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 }
3681 }
3682
3683 globalsignmap[i] = (char *)typeName;
3684 globalsignmapused = i + 1;
3685 }
3686
3687 /* check local map; should *not* be found! */
3688 for (j = 0; j < buf->signmapused; j++)
3689 if (buf->signmap[j] == i + 1)
3690 return;
3691
3692 /* add to local map */
3693 if (buf->signmapused == buf->signmaplen)
3694 {
3695 if (buf->signmaplen == 0) /* first allocation */
3696 {
3697 buf->signmaplen = 5;
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003698 buf->signmap = (int *)alloc_clear(buf->signmaplen * sizeof(int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 }
3700 else /* grow it */
3701 {
3702 int incr;
3703 int oldlen = buf->signmaplen;
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003704
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 buf->signmaplen *= 2;
3706 incr = buf->signmaplen - oldlen;
3707 buf->signmap = (int *)vim_realloc(buf->signmap,
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003708 buf->signmaplen * sizeof(int));
3709 vim_memset(buf->signmap + oldlen, 0, incr * sizeof(int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 }
3711 }
3712
3713 buf->signmap[buf->signmapused++] = i + 1;
3714
3715}
3716
3717
3718/*
3719 * See if we have the requested sign type in the buffer.
3720 */
3721 static int
3722mapsigntype(nbbuf_T *buf, int localsigntype)
3723{
3724 if (--localsigntype >= 0 && localsigntype < buf->signmapused)
3725 return buf->signmap[localsigntype];
3726
3727 return 0;
3728}
3729
3730
3731/*
3732 * Compute length of buffer, don't print anything.
3733 */
3734 static long
3735get_buf_size(buf_T *bufp)
3736{
3737 linenr_T lnum;
3738 long char_count = 0;
3739 int eol_size;
3740 long last_check = 100000L;
3741
3742 if (bufp->b_ml.ml_flags & ML_EMPTY)
3743 return 0;
3744 else
3745 {
3746 if (get_fileformat(bufp) == EOL_DOS)
3747 eol_size = 2;
3748 else
3749 eol_size = 1;
3750 for (lnum = 1; lnum <= bufp->b_ml.ml_line_count; ++lnum)
3751 {
Bram Moolenaarfa68b0f2009-09-11 12:19:51 +00003752 char_count += (long)STRLEN(ml_get_buf(bufp, lnum, FALSE))
3753 + eol_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 /* Check for a CTRL-C every 100000 characters */
3755 if (char_count > last_check)
3756 {
3757 ui_breakcheck();
3758 if (got_int)
3759 return char_count;
3760 last_check = char_count + 100000L;
3761 }
3762 }
3763 /* Correction for when last line doesn't have an EOL. */
3764 if (!bufp->b_p_eol && bufp->b_p_bin)
3765 char_count -= eol_size;
3766 }
3767
3768 return char_count;
3769}
3770
3771/*
3772 * Convert character offset to lnum,col
3773 */
3774 static pos_T *
3775off2pos(buf_T *buf, long offset)
3776{
3777 linenr_T lnum;
3778 static pos_T pos;
3779
3780 pos.lnum = 0;
3781 pos.col = 0;
3782#ifdef FEAT_VIRTUALEDIT
3783 pos.coladd = 0;
3784#endif
3785
3786 if (!(buf->b_ml.ml_flags & ML_EMPTY))
3787 {
3788 if ((lnum = ml_find_line_or_offset(buf, (linenr_T)0, &offset)) < 0)
3789 return NULL;
3790 pos.lnum = lnum;
3791 pos.col = offset;
3792 }
3793
3794 return &pos;
3795}
3796
3797/*
3798 * Convert an argument in the form "1234" to an offset and compute the
3799 * lnum/col from it. Convert an argument in the form "123/12" directly to a
3800 * lnum/col.
3801 * "argp" is advanced to after the argument.
3802 * Return a pointer to the position, NULL if something is wrong.
3803 */
3804 static pos_T *
3805get_off_or_lnum(buf_T *buf, char_u **argp)
3806{
3807 static pos_T mypos;
3808 long off;
3809
3810 off = strtol((char *)*argp, (char **)argp, 10);
3811 if (**argp == '/')
3812 {
3813 mypos.lnum = (linenr_T)off;
3814 ++*argp;
3815 mypos.col = strtol((char *)*argp, (char **)argp, 10);
3816#ifdef FEAT_VIRTUALEDIT
3817 mypos.coladd = 0;
3818#endif
3819 return &mypos;
3820 }
3821 return off2pos(buf, off);
3822}
3823
3824
3825/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003826 * Convert (lnum,col) to byte offset in the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 */
3828 static long
3829pos2off(buf_T *buf, pos_T *pos)
3830{
3831 long offset = 0;
3832
3833 if (!(buf->b_ml.ml_flags & ML_EMPTY))
3834 {
3835 if ((offset = ml_find_line_or_offset(buf, pos->lnum, 0)) < 0)
3836 return 0;
3837 offset += pos->col;
3838 }
3839
3840 return offset;
3841}
3842
3843
Bram Moolenaar009b2592004-10-24 19:18:58 +00003844/*
3845 * This message is printed after NetBeans opens a new file. Its
3846 * similar to the message readfile() uses, but since NetBeans
3847 * doesn't normally call readfile, we do our own.
3848 */
3849 static void
3850print_read_msg(buf)
3851 nbbuf_T *buf;
3852{
3853 int lnum = buf->bufp->b_ml.ml_line_count;
Bram Moolenaar914703b2010-05-31 21:59:46 +02003854 off_t nchars = buf->bufp->b_orig_size;
Bram Moolenaar009b2592004-10-24 19:18:58 +00003855 char_u c;
3856
3857 msg_add_fname(buf->bufp, buf->bufp->b_ffname);
3858 c = FALSE;
3859
3860 if (buf->bufp->b_p_ro)
3861 {
3862 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]"));
3863 c = TRUE;
3864 }
3865 if (!buf->bufp->b_start_eol)
3866 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003867 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]")
3868 : _("[Incomplete last line]"));
Bram Moolenaar009b2592004-10-24 19:18:58 +00003869 c = TRUE;
3870 }
3871 msg_add_lines(c, (long)lnum, nchars);
3872
3873 /* Now display it */
3874 vim_free(keep_msg);
3875 keep_msg = NULL;
3876 msg_scrolled_ign = TRUE;
3877 msg_trunc_attr(IObuff, FALSE, 0);
3878 msg_scrolled_ign = FALSE;
3879}
3880
3881
3882/*
Bram Moolenaar67c53842010-05-22 18:28:27 +02003883 * Print a message after NetBeans writes the file. This message should be
3884 * identical to the standard message a non-netbeans user would see when
3885 * writing a file.
Bram Moolenaar009b2592004-10-24 19:18:58 +00003886 */
3887 static void
3888print_save_msg(buf, nchars)
3889 nbbuf_T *buf;
Bram Moolenaar914703b2010-05-31 21:59:46 +02003890 off_t nchars;
Bram Moolenaar009b2592004-10-24 19:18:58 +00003891{
3892 char_u c;
3893 char_u *p;
3894
3895 if (nchars >= 0)
3896 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02003897 /* put fname in IObuff with quotes */
3898 msg_add_fname(buf->bufp, buf->bufp->b_ffname);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003899 c = FALSE;
3900
3901 msg_add_lines(c, buf->bufp->b_ml.ml_line_count,
Bram Moolenaar914703b2010-05-31 21:59:46 +02003902 buf->bufp->b_orig_size);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003903
3904 vim_free(keep_msg);
3905 keep_msg = NULL;
3906 msg_scrolled_ign = TRUE;
3907 p = msg_trunc_attr(IObuff, FALSE, 0);
3908 if ((msg_scrolled && !need_wait_return) || !buf->initDone)
3909 {
3910 /* Need to repeat the message after redrawing when:
3911 * - When reading from stdin (the screen will be cleared next).
3912 * - When restart_edit is set (otherwise there will be a delay
3913 * before redrawing).
3914 * - When the screen was scrolled but there is no wait-return
3915 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003916 set_keep_msg(p, 0);
Bram Moolenaar009b2592004-10-24 19:18:58 +00003917 }
3918 msg_scrolled_ign = FALSE;
3919 /* add_to_input_buf((char_u *)"\f", 1); */
3920 }
3921 else
3922 {
3923 char_u ebuf[BUFSIZ];
3924
3925 STRCPY(ebuf, (char_u *)_("E505: "));
3926 STRCAT(ebuf, IObuff);
3927 STRCAT(ebuf, (char_u *)_("is read-only (add ! to override)"));
3928 STRCPY(IObuff, ebuf);
Bram Moolenaarf2330482008-06-24 20:19:36 +00003929 nbdebug((" %s\n", ebuf ));
Bram Moolenaar009b2592004-10-24 19:18:58 +00003930 emsg(IObuff);
3931 }
3932}
3933
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934#endif /* defined(FEAT_NETBEANS_INTG) */