Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1 | /* 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 | |
| 19 | #include "vim.h" |
| 20 | |
| 21 | #if defined(FEAT_NETBEANS_INTG) || defined(PROTO) |
| 22 | |
| 23 | /* Note: when making changes here also adjust configure.in. */ |
| 24 | # include <stdarg.h> |
| 25 | # include <fcntl.h> |
| 26 | #ifdef WIN32 |
| 27 | # ifdef DEBUG |
| 28 | # include <tchar.h> /* for _T definition for TRACEn macros */ |
| 29 | # endif |
| 30 | # include <io.h> |
| 31 | /* WinSock API is separated from C API, thus we can't use read(), write(), |
| 32 | * errno... */ |
| 33 | # define sock_errno WSAGetLastError() |
| 34 | # define ECONNREFUSED WSAECONNREFUSED |
| 35 | # ifdef EINTR |
| 36 | # undef EINTR |
| 37 | # endif |
| 38 | # define EINTR WSAEINTR |
| 39 | # define sock_write(sd, buf, len) send(sd, buf, len, 0) |
| 40 | # define sock_read(sd, buf, len) recv(sd, buf, len, 0) |
| 41 | # define sock_close(sd) closesocket(sd) |
| 42 | # define sleep(t) Sleep(t*1000) /* WinAPI Sleep() accepts milliseconds */ |
| 43 | #else |
| 44 | # include <netdb.h> |
| 45 | # include <netinet/in.h> |
| 46 | # include <sys/socket.h> |
| 47 | # ifdef HAVE_LIBGEN_H |
| 48 | # include <libgen.h> |
| 49 | # endif |
| 50 | # define sock_errno errno |
| 51 | # define sock_write(sd, buf, len) write(sd, buf, len) |
| 52 | # define sock_read(sd, buf, len) read(sd, buf, len) |
| 53 | # define sock_close(sd) close(sd) |
| 54 | #endif |
| 55 | |
| 56 | #include "version.h" |
| 57 | |
| 58 | #define INET_SOCKETS |
| 59 | |
| 60 | #define GUARDED 10000 /* typenr for "guarded" annotation */ |
| 61 | #define GUARDEDOFFSET 1000000 /* base for "guarded" sign id's */ |
| 62 | |
| 63 | /* The first implementation (working only with Netbeans) returned "1.1". The |
| 64 | * protocol implemented here also supports A-A-P. */ |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 65 | static char *ExtEdProtocolVersion = "2.3"; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 66 | |
| 67 | static long pos2off __ARGS((buf_T *, pos_T *)); |
| 68 | static pos_T *off2pos __ARGS((buf_T *, long)); |
| 69 | static pos_T *get_off_or_lnum __ARGS((buf_T *buf, char_u **argp)); |
| 70 | static long get_buf_size __ARGS((buf_T *)); |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 71 | static void netbeans_keystring __ARGS((int key, char *keystr)); |
| 72 | static void special_keys __ARGS((char_u *args)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 73 | |
| 74 | static void netbeans_connect __ARGS((void)); |
| 75 | static int getConnInfo __ARGS((char *file, char **host, char **port, char **password)); |
| 76 | |
| 77 | static void nb_init_graphics __ARGS((void)); |
| 78 | static void coloncmd __ARGS((char *cmd, ...)); |
| 79 | #ifdef FEAT_GUI_MOTIF |
| 80 | static void messageFromNetbeans __ARGS((XtPointer, int *, XtInputId *)); |
| 81 | #endif |
| 82 | #ifdef FEAT_GUI_GTK |
| 83 | static void messageFromNetbeans __ARGS((gpointer, gint, GdkInputCondition)); |
| 84 | #endif |
| 85 | static void nb_parse_cmd __ARGS((char_u *)); |
| 86 | static int nb_do_cmd __ARGS((int, char_u *, int, int, char_u *)); |
| 87 | static void nb_send __ARGS((char *buf, char *fun)); |
| 88 | #ifdef FEAT_BEVAL |
| 89 | static void netbeans_beval_cb __ARGS((BalloonEval *beval, int state)); |
| 90 | #endif |
| 91 | |
| 92 | static int sd = -1; /* socket fd for Netbeans connection */ |
| 93 | #ifdef FEAT_GUI_MOTIF |
| 94 | static XtInputId inputHandler; /* Cookie for input */ |
| 95 | #endif |
| 96 | #ifdef FEAT_GUI_GTK |
| 97 | static gint inputHandler; /* Cookie for input */ |
| 98 | #endif |
| 99 | #ifdef FEAT_GUI_W32 |
| 100 | static int inputHandler = -1; /* simply ret.value of WSAAsyncSelect() */ |
| 101 | extern HWND s_hwnd; /* Gvim's Window handle */ |
| 102 | #endif |
| 103 | static int cmdno; /* current command number for reply */ |
| 104 | static int haveConnection = FALSE; /* socket is connected and |
| 105 | initialization is done */ |
| 106 | static int oldFire = 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 107 | |
| 108 | #ifdef FEAT_BEVAL |
| 109 | # if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA) |
| 110 | extern Widget textArea; |
| 111 | # endif |
| 112 | BalloonEval *balloonEval = NULL; |
| 113 | #endif |
| 114 | |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 115 | #ifdef FEAT_GUI_MOTIF |
| 116 | static void netbeans_Xt_connect __ARGS((void *context)); |
| 117 | #else |
| 118 | # ifdef FEAT_GUI_GTK |
| 119 | static void netbeans_gtk_connect __ARGS((void)); |
| 120 | # else |
| 121 | # ifdef FEAT_GUI_W32 |
| 122 | static void netbeans_w32_connect __ARGS((void)); |
| 123 | # endif |
| 124 | # endif |
| 125 | #endif |
| 126 | |
| 127 | static int dosetvisible = FALSE; |
| 128 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 129 | /* |
| 130 | * Include the debugging code if wanted. |
| 131 | */ |
| 132 | #ifdef NBDEBUG |
| 133 | # include "nbdebug.c" |
| 134 | #endif |
| 135 | |
| 136 | /* Connect back to Netbeans process */ |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 137 | #ifdef FEAT_GUI_MOTIF |
| 138 | static void |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 139 | netbeans_Xt_connect(void *context) |
| 140 | { |
| 141 | netbeans_connect(); |
| 142 | if (sd > 0) |
| 143 | { |
| 144 | /* tell notifier we are interested in being called |
| 145 | * when there is input on the editor connection socket |
| 146 | */ |
| 147 | inputHandler = XtAppAddInput((XtAppContext)context, sd, |
| 148 | (XtPointer)(XtInputReadMask + XtInputExceptMask), |
| 149 | messageFromNetbeans, NULL); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | static void |
| 154 | netbeans_disconnect(void) |
| 155 | { |
| 156 | if (inputHandler != (XtInputId)NULL) |
| 157 | { |
| 158 | XtRemoveInput(inputHandler); |
| 159 | inputHandler = (XtInputId)NULL; |
| 160 | } |
| 161 | sd = -1; |
| 162 | haveConnection = FALSE; |
| 163 | } |
| 164 | #endif /* FEAT_MOTIF_GUI */ |
| 165 | |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 166 | #ifdef FEAT_GUI_GTK |
| 167 | static void |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 168 | netbeans_gtk_connect(void) |
| 169 | { |
| 170 | # ifdef FEAT_BEVAL |
| 171 | /* |
| 172 | * Set up the Balloon Expression Evaluation area. |
| 173 | * Always create it but disable it when 'ballooneval' isn't set. |
| 174 | */ |
| 175 | balloonEval = gui_mch_create_beval_area(gui.drawarea, NULL, |
| 176 | &netbeans_beval_cb, NULL); |
| 177 | if (!p_beval) |
| 178 | gui_mch_disable_beval_area(balloonEval); |
| 179 | # endif |
| 180 | |
| 181 | netbeans_connect(); |
| 182 | if (sd > 0) |
| 183 | { |
| 184 | /* |
| 185 | * Tell gdk we are interested in being called when there |
| 186 | * is input on the editor connection socket |
| 187 | */ |
| 188 | inputHandler = gdk_input_add(sd, (GdkInputCondition) |
| 189 | ((int)GDK_INPUT_READ + (int)GDK_INPUT_EXCEPTION), |
| 190 | messageFromNetbeans, NULL); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | static void |
| 195 | netbeans_disconnect(void) |
| 196 | { |
| 197 | if (inputHandler != 0) |
| 198 | { |
| 199 | gdk_input_remove(inputHandler); |
| 200 | inputHandler = 0; |
| 201 | } |
| 202 | sd = -1; |
| 203 | haveConnection = FALSE; |
| 204 | } |
| 205 | #endif /* FEAT_GUI_GTK */ |
| 206 | |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 207 | #ifdef FEAT_GUI_W32 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 208 | void |
| 209 | netbeans_w32_connect(void) |
| 210 | { |
| 211 | netbeans_connect(); |
| 212 | if (sd > 0) |
| 213 | { |
| 214 | /* |
| 215 | * Tell Windows we are interested in receiving message when there |
| 216 | * is input on the editor connection socket |
| 217 | */ |
| 218 | inputHandler = WSAAsyncSelect(sd, s_hwnd, WM_NETBEANS, FD_READ); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | static void |
| 223 | netbeans_disconnect(void) |
| 224 | { |
| 225 | if (inputHandler == 0) |
| 226 | { |
| 227 | WSAAsyncSelect(sd, s_hwnd, 0, 0); |
| 228 | inputHandler = -1; |
| 229 | } |
| 230 | sd = -1; |
| 231 | haveConnection = FALSE; |
| 232 | |
| 233 | /* It seems that Motif and GTK versions also need this: */ |
| 234 | gui_mch_destroy_beval_area(balloonEval); |
| 235 | balloonEval = NULL; |
| 236 | } |
| 237 | #endif /* FEAT_GUI_W32 */ |
| 238 | |
| 239 | #define NB_DEF_HOST "localhost" |
| 240 | #define NB_DEF_ADDR "3219" |
| 241 | #define NB_DEF_PASS "changeme" |
| 242 | |
| 243 | static void |
| 244 | netbeans_connect(void) |
| 245 | { |
| 246 | #ifdef INET_SOCKETS |
| 247 | struct sockaddr_in server; |
| 248 | struct hostent * host; |
| 249 | # ifdef FEAT_GUI_W32 |
| 250 | u_short port; |
| 251 | # else |
| 252 | int port; |
| 253 | #endif |
| 254 | #else |
| 255 | struct sockaddr_un server; |
| 256 | #endif |
| 257 | char buf[32]; |
| 258 | char *hostname = NULL; |
| 259 | char *address = NULL; |
| 260 | char *password = NULL; |
| 261 | char *fname; |
| 262 | char *arg = NULL; |
| 263 | |
| 264 | if (netbeansArg[3] == '=') |
| 265 | { |
| 266 | /* "-nb=fname": Read info from specified file. */ |
| 267 | if (getConnInfo(netbeansArg + 4, &hostname, &address, &password) |
| 268 | == FAIL) |
| 269 | return; |
| 270 | } |
| 271 | else |
| 272 | { |
| 273 | if (netbeansArg[3] == ':') |
| 274 | /* "-nb:<host>:<addr>:<password>": get info from argument */ |
| 275 | arg = netbeansArg + 4; |
| 276 | if (arg == NULL && (fname = getenv("__NETBEANS_CONINFO")) != NULL) |
| 277 | { |
| 278 | /* "-nb": get info from file specified in environment */ |
| 279 | if (getConnInfo(fname, &hostname, &address, &password) == FAIL) |
| 280 | return; |
| 281 | } |
| 282 | else |
| 283 | { |
| 284 | if (arg != NULL) |
| 285 | { |
| 286 | /* "-nb:<host>:<addr>:<password>": get info from argument */ |
| 287 | hostname = arg; |
| 288 | address = strchr(hostname, ':'); |
| 289 | if (address != NULL) |
| 290 | { |
| 291 | *address++ = '\0'; |
| 292 | password = strchr(address, ':'); |
| 293 | if (password != NULL) |
| 294 | *password++ = '\0'; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | /* Get the missing values from the environment. */ |
| 299 | if (hostname == NULL || *hostname == '\0') |
| 300 | hostname = getenv("__NETBEANS_HOST"); |
| 301 | if (address == NULL) |
| 302 | address = getenv("__NETBEANS_SOCKET"); |
| 303 | if (password == NULL) |
| 304 | password = getenv("__NETBEANS_VIM_PASSWORD"); |
| 305 | |
| 306 | /* Move values to allocated memory. */ |
| 307 | if (hostname != NULL) |
| 308 | hostname = (char *)vim_strsave((char_u *)hostname); |
| 309 | if (address != NULL) |
| 310 | address = (char *)vim_strsave((char_u *)address); |
| 311 | if (password != NULL) |
| 312 | password = (char *)vim_strsave((char_u *)password); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | /* Use the default when a value is missing. */ |
| 317 | if (hostname == NULL || *hostname == '\0') |
| 318 | { |
| 319 | vim_free(hostname); |
| 320 | hostname = (char *)vim_strsave((char_u *)NB_DEF_HOST); |
| 321 | } |
| 322 | if (address == NULL || *address == '\0') |
| 323 | { |
| 324 | vim_free(address); |
| 325 | address = (char *)vim_strsave((char_u *)NB_DEF_ADDR); |
| 326 | } |
| 327 | if (password == NULL || *password == '\0') |
| 328 | { |
| 329 | vim_free(password); |
| 330 | password = (char *)vim_strsave((char_u *)NB_DEF_PASS); |
| 331 | } |
| 332 | if (hostname == NULL || address == NULL || password == NULL) |
| 333 | goto theend; /* out of memory */ |
| 334 | |
| 335 | #ifdef INET_SOCKETS |
| 336 | port = atoi(address); |
| 337 | |
| 338 | if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) |
| 339 | { |
| 340 | PERROR("socket() in netbeans_connect()"); |
| 341 | goto theend; |
| 342 | } |
| 343 | |
| 344 | /* Get the server internet address and put into addr structure */ |
| 345 | /* fill in the socket address structure and connect to server */ |
| 346 | memset((char *)&server, '\0', sizeof(server)); |
| 347 | server.sin_family = AF_INET; |
| 348 | server.sin_port = htons(port); |
| 349 | if ((host = gethostbyname(hostname)) == NULL) |
| 350 | { |
| 351 | if (mch_access(hostname, R_OK) >= 0) |
| 352 | { |
| 353 | /* DEBUG: input file */ |
| 354 | sd = mch_open(hostname, O_RDONLY, 0); |
| 355 | goto theend; |
| 356 | } |
| 357 | PERROR("gethostbyname() in netbeans_connect()"); |
| 358 | sd = -1; |
| 359 | goto theend; |
| 360 | } |
| 361 | memcpy((char *)&server.sin_addr, host->h_addr, host->h_length); |
| 362 | #else |
| 363 | if ((sd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) |
| 364 | { |
| 365 | PERROR("socket()"); |
| 366 | goto theend; |
| 367 | } |
| 368 | |
| 369 | server.sun_family = AF_UNIX; |
| 370 | strcpy(server.sun_path, address); |
| 371 | #endif |
| 372 | /* Connect to server */ |
| 373 | if (connect(sd, (struct sockaddr *)&server, sizeof(server))) |
| 374 | { |
| 375 | nbdebug(("netbeans_connect: Connect failed with errno %d\n", sock_errno)); |
| 376 | if (sock_errno == ECONNREFUSED) |
| 377 | { |
| 378 | sock_close(sd); |
| 379 | #ifdef INET_SOCKETS |
| 380 | if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) |
| 381 | { |
| 382 | PERROR("socket()#2 in netbeans_connect()"); |
| 383 | goto theend; |
| 384 | } |
| 385 | #else |
| 386 | if ((sd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) |
| 387 | { |
| 388 | PERROR("socket()#2 in netbeans_connect()"); |
| 389 | goto theend; |
| 390 | } |
| 391 | #endif |
| 392 | if (connect(sd, (struct sockaddr *)&server, sizeof(server))) |
| 393 | { |
| 394 | int retries = 36; |
| 395 | int success = FALSE; |
| 396 | while (retries-- |
| 397 | && ((sock_errno == ECONNREFUSED) || (sock_errno == EINTR))) |
| 398 | { |
| 399 | nbdebug(("retrying...\n")); |
| 400 | sleep(5); |
| 401 | if (connect(sd, (struct sockaddr *)&server, |
| 402 | sizeof(server)) == 0) |
| 403 | { |
| 404 | success = TRUE; |
| 405 | break; |
| 406 | } |
| 407 | } |
| 408 | if (!success) |
| 409 | { |
| 410 | /* Get here when the server can't be found. */ |
| 411 | PERROR(_("Cannot connect to Netbeans #2")); |
| 412 | getout(1); |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | } |
| 417 | else |
| 418 | { |
| 419 | PERROR(_("Cannot connect to Netbeans")); |
| 420 | getout(1); |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | sprintf(buf, "AUTH %s\n", password); |
| 425 | nb_send(buf, "netbeans_connect"); |
| 426 | |
| 427 | sprintf(buf, "0:version=0 \"%s\"\n", ExtEdProtocolVersion); |
| 428 | nb_send(buf, "externaleditor_version"); |
| 429 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 430 | /* nb_init_graphics(); delay until needed */ |
| 431 | |
| 432 | haveConnection = TRUE; |
| 433 | |
| 434 | theend: |
| 435 | vim_free(hostname); |
| 436 | vim_free(address); |
| 437 | vim_free(password); |
| 438 | return; |
| 439 | } |
| 440 | |
| 441 | /* |
| 442 | * Obtain the NetBeans hostname, port address and password from a file. |
| 443 | * Return the strings in allocated memory. |
| 444 | * Return FAIL if the file could not be read, OK otherwise (no matter what it |
| 445 | * contains). |
| 446 | */ |
| 447 | static int |
| 448 | getConnInfo(char *file, char **host, char **port, char **auth) |
| 449 | { |
| 450 | FILE *fp; |
| 451 | char_u buf[BUFSIZ]; |
| 452 | char_u *lp; |
| 453 | char_u *nl; |
| 454 | #ifdef UNIX |
| 455 | struct stat st; |
| 456 | |
| 457 | /* |
| 458 | * For Unix only accept the file when it's not accessible by others. |
| 459 | * The open will then fail if we don't own the file. |
| 460 | */ |
| 461 | if (mch_stat(file, &st) == 0 && (st.st_mode & 0077) != 0) |
| 462 | { |
| 463 | EMSG2(_("E668: Wrong access mode for NetBeans connection info file: \"%s\""), |
| 464 | file); |
| 465 | return FAIL; |
| 466 | } |
| 467 | #endif |
| 468 | |
| 469 | fp = mch_fopen(file, "r"); |
| 470 | if (fp == NULL) |
| 471 | { |
| 472 | PERROR("E660: Cannot open NetBeans connection info file"); |
| 473 | return FAIL; |
| 474 | } |
| 475 | |
| 476 | /* Read the file. There should be one of each parameter */ |
| 477 | while ((lp = (char_u *)fgets((char *)buf, BUFSIZ, fp)) != NULL) |
| 478 | { |
| 479 | if ((nl = vim_strchr(lp, '\n')) != NULL) |
| 480 | *nl = 0; /* strip off the trailing newline */ |
| 481 | |
| 482 | if (STRNCMP(lp, "host=", 5) == 0) |
| 483 | { |
| 484 | vim_free(*host); |
| 485 | *host = (char *)vim_strsave(&buf[5]); |
| 486 | } |
| 487 | else if (STRNCMP(lp, "port=", 5) == 0) |
| 488 | { |
| 489 | vim_free(*port); |
| 490 | *port = (char *)vim_strsave(&buf[5]); |
| 491 | } |
| 492 | else if (STRNCMP(lp, "auth=", 5) == 0) |
| 493 | { |
| 494 | vim_free(*auth); |
| 495 | *auth = (char *)vim_strsave(&buf[5]); |
| 496 | } |
| 497 | } |
| 498 | fclose(fp); |
| 499 | |
| 500 | return OK; |
| 501 | } |
| 502 | |
| 503 | |
| 504 | struct keyqueue |
| 505 | { |
| 506 | int key; |
| 507 | struct keyqueue *next; |
| 508 | struct keyqueue *prev; |
| 509 | }; |
| 510 | |
| 511 | typedef struct keyqueue keyQ_T; |
| 512 | |
| 513 | static keyQ_T keyHead; /* dummy node, header for circular queue */ |
| 514 | |
| 515 | |
| 516 | /* |
| 517 | * Queue up key commands sent from netbeans. |
| 518 | */ |
| 519 | static void |
| 520 | postpone_keycommand(int key) |
| 521 | { |
| 522 | keyQ_T *node; |
| 523 | |
| 524 | node = (keyQ_T *)alloc(sizeof(keyQ_T)); |
| 525 | |
| 526 | if (keyHead.next == NULL) /* initialize circular queue */ |
| 527 | { |
| 528 | keyHead.next = &keyHead; |
| 529 | keyHead.prev = &keyHead; |
| 530 | } |
| 531 | |
| 532 | /* insert node at tail of queue */ |
| 533 | node->next = &keyHead; |
| 534 | node->prev = keyHead.prev; |
| 535 | keyHead.prev->next = node; |
| 536 | keyHead.prev = node; |
| 537 | |
| 538 | node->key = key; |
| 539 | } |
| 540 | |
| 541 | /* |
| 542 | * Handle any queued-up NetBeans keycommands to be send. |
| 543 | */ |
| 544 | static void |
| 545 | handle_key_queue(void) |
| 546 | { |
| 547 | while (keyHead.next && keyHead.next != &keyHead) |
| 548 | { |
| 549 | /* first, unlink the node */ |
| 550 | keyQ_T *node = keyHead.next; |
| 551 | keyHead.next = node->next; |
| 552 | node->next->prev = node->prev; |
| 553 | |
| 554 | /* now, send the keycommand */ |
| 555 | netbeans_keycommand(node->key); |
| 556 | |
| 557 | /* Finally, dispose of the node */ |
| 558 | vim_free(node); |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | |
| 563 | struct cmdqueue |
| 564 | { |
| 565 | char_u *buffer; |
| 566 | struct cmdqueue *next; |
| 567 | struct cmdqueue *prev; |
| 568 | }; |
| 569 | |
| 570 | typedef struct cmdqueue queue_T; |
| 571 | |
| 572 | static queue_T head; /* dummy node, header for circular queue */ |
| 573 | |
| 574 | |
| 575 | /* |
| 576 | * Put the buffer on the work queue; possibly save it to a file as well. |
| 577 | */ |
| 578 | static void |
| 579 | save(char_u *buf, int len) |
| 580 | { |
| 581 | queue_T *node; |
| 582 | |
| 583 | node = (queue_T *)alloc(sizeof(queue_T)); |
| 584 | if (node == NULL) |
| 585 | return; /* out of memory */ |
| 586 | node->buffer = alloc(len + 1); |
| 587 | if (node->buffer == NULL) |
| 588 | { |
| 589 | vim_free(node); |
| 590 | return; /* out of memory */ |
| 591 | } |
| 592 | mch_memmove(node->buffer, buf, (size_t)len); |
| 593 | node->buffer[len] = NUL; |
| 594 | |
| 595 | if (head.next == NULL) /* initialize circular queue */ |
| 596 | { |
| 597 | head.next = &head; |
| 598 | head.prev = &head; |
| 599 | } |
| 600 | |
| 601 | /* insert node at tail of queue */ |
| 602 | node->next = &head; |
| 603 | node->prev = head.prev; |
| 604 | head.prev->next = node; |
| 605 | head.prev = node; |
| 606 | |
| 607 | #ifdef NBDEBUG |
| 608 | { |
| 609 | static int outfd = -2; |
| 610 | |
| 611 | /* possibly write buffer out to a file */ |
| 612 | if (outfd == -3) |
| 613 | return; |
| 614 | |
| 615 | if (outfd == -2) |
| 616 | { |
| 617 | char *file = getenv("__NETBEANS_SAVE"); |
| 618 | if (file == NULL) |
| 619 | outfd = -3; |
| 620 | else |
| 621 | outfd = mch_open(file, O_WRONLY|O_CREAT|O_TRUNC, 0666); |
| 622 | } |
| 623 | |
| 624 | if (outfd >= 0) |
| 625 | write(outfd, buf, len); |
| 626 | } |
| 627 | #endif |
| 628 | } |
| 629 | |
| 630 | |
| 631 | /* |
| 632 | * While there's still a command in the work queue, parse and execute it. |
| 633 | */ |
| 634 | static void |
| 635 | nb_parse_messages(void) |
| 636 | { |
| 637 | char_u *p; |
| 638 | queue_T *node; |
| 639 | |
| 640 | while (head.next != &head) |
| 641 | { |
| 642 | node = head.next; |
| 643 | |
| 644 | /* Locate the first line in the first buffer. */ |
| 645 | p = vim_strchr(node->buffer, '\n'); |
| 646 | if (p == NULL) |
| 647 | { |
| 648 | /* Command isn't complete. If there is no following buffer, |
| 649 | * return (wait for more). If there is another buffer following, |
| 650 | * prepend the text to that buffer and delete this one. */ |
| 651 | if (node->next == &head) |
| 652 | return; |
| 653 | p = alloc(STRLEN(node->buffer) + STRLEN(node->next->buffer) + 1); |
| 654 | if (p == NULL) |
| 655 | return; /* out of memory */ |
| 656 | STRCPY(p, node->buffer); |
| 657 | STRCAT(p, node->next->buffer); |
| 658 | vim_free(node->next->buffer); |
| 659 | node->next->buffer = p; |
| 660 | |
| 661 | /* dispose of the node and buffer */ |
| 662 | head.next = node->next; |
| 663 | node->next->prev = node->prev; |
| 664 | vim_free(node->buffer); |
| 665 | vim_free(node); |
| 666 | } |
| 667 | else |
| 668 | { |
| 669 | /* There is a complete command at the start of the buffer. |
| 670 | * Terminate it with a NUL. When no more text is following unlink |
| 671 | * the buffer. Do this before executing, because new buffers can |
| 672 | * be added while busy handling the command. */ |
| 673 | *p++ = NUL; |
| 674 | if (*p == NUL) |
| 675 | { |
| 676 | head.next = node->next; |
| 677 | node->next->prev = node->prev; |
| 678 | } |
| 679 | |
| 680 | /* now, parse and execute the commands */ |
| 681 | nb_parse_cmd(node->buffer); |
| 682 | |
| 683 | if (*p == NUL) |
| 684 | { |
| 685 | /* buffer finished, dispose of the node and buffer */ |
| 686 | vim_free(node->buffer); |
| 687 | vim_free(node); |
| 688 | } |
| 689 | else |
| 690 | { |
| 691 | /* more follows, move to the start */ |
| 692 | mch_memmove(node->buffer, p, STRLEN(p) + 1); |
| 693 | } |
| 694 | } |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | /* Buffer size for reading incoming messages. */ |
| 699 | #define MAXMSGSIZE 4096 |
| 700 | |
| 701 | /* |
| 702 | * Read and process a command from netbeans. |
| 703 | */ |
| 704 | /*ARGSUSED*/ |
| 705 | #if defined(FEAT_GUI_W32) || defined(PROTO) |
| 706 | /* Use this one when generating prototypes, the others are static. */ |
| 707 | void |
| 708 | messageFromNetbeansW32() |
| 709 | #else |
| 710 | # ifdef FEAT_GUI_MOTIF |
| 711 | static void |
| 712 | messageFromNetbeans(XtPointer clientData, int *unused1, XtInputId *unused2) |
| 713 | # endif |
| 714 | # ifdef FEAT_GUI_GTK |
| 715 | static void |
| 716 | messageFromNetbeans(gpointer clientData, gint unused1, |
| 717 | GdkInputCondition unused2) |
| 718 | # endif |
| 719 | #endif |
| 720 | { |
| 721 | static char_u *buf = NULL; |
| 722 | int len; |
| 723 | int readlen = 0; |
| 724 | static int level = 0; |
| 725 | |
| 726 | if (sd < 0) |
| 727 | { |
| 728 | nbdebug(("messageFromNetbeans() called without a socket\n")); |
| 729 | return; |
| 730 | } |
| 731 | |
| 732 | ++level; /* recursion guard; this will be called from the X event loop */ |
| 733 | |
| 734 | /* Allocate a buffer to read into. */ |
| 735 | if (buf == NULL) |
| 736 | { |
| 737 | buf = alloc(MAXMSGSIZE); |
| 738 | if (buf == NULL) |
| 739 | return; /* out of memory! */ |
| 740 | } |
| 741 | |
| 742 | /* Keep on reading for as long as there is something to read. */ |
| 743 | for (;;) |
| 744 | { |
| 745 | len = sock_read(sd, buf, MAXMSGSIZE); |
| 746 | if (len <= 0) |
| 747 | break; /* error or nothing more to read */ |
| 748 | |
| 749 | /* Store the read message in the queue. */ |
| 750 | save(buf, len); |
| 751 | readlen += len; |
| 752 | if (len < MAXMSGSIZE) |
| 753 | break; /* did read everything that's available */ |
| 754 | } |
| 755 | |
| 756 | if (readlen <= 0) |
| 757 | { |
| 758 | /* read error or didn't read anything */ |
| 759 | netbeans_disconnect(); |
| 760 | nbdebug(("messageFromNetbeans: Error in read() from socket\n")); |
| 761 | if (len < 0) |
| 762 | PERROR(_("read from Netbeans socket")); |
| 763 | return; /* don't try to parse it */; |
| 764 | } |
| 765 | |
| 766 | /* Parse the messages, but avoid recursion. */ |
| 767 | if (level == 1) |
| 768 | nb_parse_messages(); |
| 769 | |
| 770 | --level; |
| 771 | } |
| 772 | |
| 773 | /* |
| 774 | * Handle one NUL terminated command. |
| 775 | * |
| 776 | * format of a command from netbeans: |
| 777 | * |
| 778 | * 6:setTitle!84 "a.c" |
| 779 | * |
| 780 | * bufno |
| 781 | * colon |
| 782 | * cmd |
| 783 | * ! |
| 784 | * cmdno |
| 785 | * args |
| 786 | * |
| 787 | * for function calls, the ! is replaced by a / |
| 788 | */ |
| 789 | static void |
| 790 | nb_parse_cmd(char_u *cmd) |
| 791 | { |
Bram Moolenaar | 349b2f6 | 2004-10-11 10:00:50 +0000 | [diff] [blame] | 792 | char *verb; |
| 793 | char *q; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 794 | int bufno; |
| 795 | int isfunc = -1; |
| 796 | |
| 797 | if (STRCMP(cmd, "DISCONNECT") == 0) |
| 798 | { |
| 799 | /* We assume the server knows that we can safely exit! */ |
| 800 | if (sd >= 0) |
| 801 | sock_close(sd); |
| 802 | /* Disconnect before exiting, Motif hangs in a Select error |
| 803 | * message otherwise. */ |
| 804 | netbeans_disconnect(); |
| 805 | getout(0); |
| 806 | /* NOTREACHED */ |
| 807 | } |
| 808 | |
| 809 | if (STRCMP(cmd, "DETACH") == 0) |
| 810 | { |
| 811 | /* The IDE is breaking the connection. */ |
| 812 | if (sd >= 0) |
| 813 | sock_close(sd); |
| 814 | netbeans_disconnect(); |
| 815 | return; |
| 816 | } |
| 817 | |
Bram Moolenaar | 349b2f6 | 2004-10-11 10:00:50 +0000 | [diff] [blame] | 818 | bufno = strtol((char *)cmd, &verb, 10); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 819 | |
| 820 | if (*verb != ':') |
| 821 | { |
| 822 | EMSG2("E627: missing colon: %s", cmd); |
| 823 | return; |
| 824 | } |
| 825 | ++verb; /* skip colon */ |
| 826 | |
| 827 | for (q = verb; *q; q++) |
| 828 | { |
| 829 | if (*q == '!') |
| 830 | { |
| 831 | *q++ = NUL; |
| 832 | isfunc = 0; |
| 833 | break; |
| 834 | } |
| 835 | else if (*q == '/') |
| 836 | { |
| 837 | *q++ = NUL; |
| 838 | isfunc = 1; |
| 839 | break; |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | if (isfunc < 0) |
| 844 | { |
| 845 | EMSG2("E628: missing ! or / in: %s", cmd); |
| 846 | return; |
| 847 | } |
| 848 | |
Bram Moolenaar | 349b2f6 | 2004-10-11 10:00:50 +0000 | [diff] [blame] | 849 | cmdno = strtol(q, &q, 10); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 850 | |
Bram Moolenaar | 349b2f6 | 2004-10-11 10:00:50 +0000 | [diff] [blame] | 851 | q = (char *)skipwhite((char_u *)q); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 852 | |
Bram Moolenaar | 349b2f6 | 2004-10-11 10:00:50 +0000 | [diff] [blame] | 853 | if (nb_do_cmd(bufno, (char_u *)verb, isfunc, cmdno, (char_u *)q) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 854 | { |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 855 | #ifdef NBDEBUG |
| 856 | /* |
| 857 | * This happens because the ExtEd can send a cammand or 2 after |
| 858 | * doing a stopDocumentListen command. It doesn't harm anything |
| 859 | * so I'm disabling it except for debugging. |
| 860 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 861 | nbdebug(("nb_parse_cmd: Command error for \"%s\"\n", cmd)); |
| 862 | EMSG("E629: bad return from nb_do_cmd"); |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 863 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 864 | } |
| 865 | } |
| 866 | |
| 867 | struct nbbuf_struct |
| 868 | { |
| 869 | buf_T *bufp; |
| 870 | unsigned int fireChanges:1; |
| 871 | unsigned int initDone:1; |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 872 | unsigned int insertDone:1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 873 | unsigned int modified:1; |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 874 | int nbbuf_number; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 875 | char *displayname; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 876 | int *signmap; |
| 877 | short_u signmaplen; |
| 878 | short_u signmapused; |
| 879 | }; |
| 880 | |
| 881 | typedef struct nbbuf_struct nbbuf_T; |
| 882 | |
| 883 | static nbbuf_T *buf_list = 0; |
| 884 | int buf_list_size = 0; /* size of buf_list */ |
| 885 | int buf_list_used = 0; /* nr of entries in buf_list actually in use */ |
| 886 | |
| 887 | static char **globalsignmap; |
| 888 | static int globalsignmaplen; |
| 889 | static int globalsignmapused; |
| 890 | |
| 891 | static int mapsigntype __ARGS((nbbuf_T *, int localsigntype)); |
| 892 | static void addsigntype __ARGS((nbbuf_T *, int localsigntype, char_u *typeName, |
| 893 | char_u *tooltip, char_u *glyphfile, |
| 894 | int usefg, int fg, int usebg, int bg)); |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 895 | static void print_read_msg __ARGS((nbbuf_T *buf)); |
| 896 | static void print_save_msg __ARGS((nbbuf_T *buf, long nchars)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 897 | |
| 898 | static int curPCtype = -1; |
| 899 | |
| 900 | /* |
| 901 | * Get the Netbeans buffer number for the specified buffer. |
| 902 | */ |
| 903 | static int |
| 904 | nb_getbufno(buf_T *bufp) |
| 905 | { |
| 906 | int i; |
| 907 | |
| 908 | for (i = 0; i < buf_list_used; i++) |
| 909 | if (buf_list[i].bufp == bufp) |
| 910 | return i; |
| 911 | return -1; |
| 912 | } |
| 913 | |
| 914 | /* |
| 915 | * Is this a NetBeans-owned buffer? |
| 916 | */ |
| 917 | int |
| 918 | isNetbeansBuffer(buf_T *bufp) |
| 919 | { |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 920 | return usingNetbeans && bufp->b_netbeans_file; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | /* |
| 924 | * NetBeans and Vim have different undo models. In Vim, the file isn't |
| 925 | * changed if changes are undone via the undo command. In NetBeans, once |
| 926 | * a change has been made the file is marked as modified until saved. It |
| 927 | * doesn't matter if the change was undone. |
| 928 | * |
| 929 | * So this function is for the corner case where Vim thinks a buffer is |
| 930 | * unmodified but NetBeans thinks it IS modified. |
| 931 | */ |
| 932 | int |
| 933 | isNetbeansModified(buf_T *bufp) |
| 934 | { |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 935 | if (usingNetbeans && bufp->b_netbeans_file) |
| 936 | { |
| 937 | int bufno = nb_getbufno(bufp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 938 | |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 939 | if (bufno > 0) |
| 940 | return buf_list[bufno].modified; |
| 941 | else |
| 942 | return FALSE; |
| 943 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 944 | else |
| 945 | return FALSE; |
| 946 | } |
| 947 | |
| 948 | /* |
| 949 | * Given a Netbeans buffer number, return the netbeans buffer. |
| 950 | * Returns NULL for 0 or a negative number. A 0 bufno means a |
| 951 | * non-buffer related command has been sent. |
| 952 | */ |
| 953 | static nbbuf_T * |
| 954 | nb_get_buf(int bufno) |
| 955 | { |
| 956 | /* find or create a buffer with the given number */ |
| 957 | int incr; |
| 958 | |
| 959 | if (bufno <= 0) |
| 960 | return NULL; |
| 961 | |
| 962 | if (!buf_list) |
| 963 | { |
| 964 | /* initialize */ |
| 965 | buf_list = (nbbuf_T *)alloc_clear(100 * sizeof(nbbuf_T)); |
| 966 | buf_list_size = 100; |
| 967 | } |
| 968 | if (bufno >= buf_list_used) /* new */ |
| 969 | { |
| 970 | if (bufno >= buf_list_size) /* grow list */ |
| 971 | { |
| 972 | incr = bufno - buf_list_size + 90; |
| 973 | buf_list_size += incr; |
| 974 | buf_list = (nbbuf_T *)vim_realloc( |
| 975 | buf_list, buf_list_size * sizeof(nbbuf_T)); |
| 976 | memset(buf_list + buf_list_size - incr, 0, incr * sizeof(nbbuf_T)); |
| 977 | } |
| 978 | |
| 979 | while (buf_list_used <= bufno) |
| 980 | { |
| 981 | /* Default is to fire text changes. */ |
| 982 | buf_list[buf_list_used].fireChanges = 1; |
| 983 | ++buf_list_used; |
| 984 | } |
| 985 | } |
| 986 | |
| 987 | return buf_list + bufno; |
| 988 | } |
| 989 | |
| 990 | /* |
| 991 | * Return the number of buffers that are modified. |
| 992 | */ |
| 993 | static int |
| 994 | count_changed_buffers(void) |
| 995 | { |
| 996 | buf_T *bufp; |
| 997 | int n; |
| 998 | |
| 999 | n = 0; |
| 1000 | for (bufp = firstbuf; bufp != NULL; bufp = bufp->b_next) |
| 1001 | if (bufp->b_changed) |
| 1002 | ++n; |
| 1003 | return n; |
| 1004 | } |
| 1005 | |
| 1006 | /* |
| 1007 | * End the netbeans session. |
| 1008 | */ |
| 1009 | void |
| 1010 | netbeans_end(void) |
| 1011 | { |
| 1012 | int i; |
| 1013 | static char buf[128]; |
| 1014 | |
| 1015 | if (!haveConnection) |
| 1016 | return; |
| 1017 | |
| 1018 | for (i = 0; i < buf_list_used; i++) |
| 1019 | { |
| 1020 | if (!buf_list[i].bufp) |
| 1021 | continue; |
| 1022 | if (netbeansForcedQuit) |
| 1023 | { |
| 1024 | /* mark as unmodified so NetBeans won't put up dialog on "killed" */ |
| 1025 | sprintf(buf, "%d:unmodified=%d\n", i, cmdno); |
| 1026 | nbdebug(("EVT: %s", buf)); |
| 1027 | nb_send(buf, "netbeans_end"); |
| 1028 | } |
| 1029 | sprintf(buf, "%d:killed=%d\n", i, cmdno); |
| 1030 | nbdebug(("EVT: %s", buf)); |
| 1031 | /* nb_send(buf, "netbeans_end"); avoid "write failed" messages */ |
| 1032 | if (sd >= 0) |
| 1033 | sock_write(sd, buf, STRLEN(buf)); /* ignore errors */ |
| 1034 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | /* |
| 1038 | * Send a message to netbeans. |
| 1039 | */ |
| 1040 | static void |
| 1041 | nb_send(char *buf, char *fun) |
| 1042 | { |
| 1043 | /* Avoid giving pages full of error messages when the other side has |
| 1044 | * exited, only mention the first error until the connection works again. */ |
| 1045 | static int did_error = FALSE; |
| 1046 | |
| 1047 | if (sd < 0) |
| 1048 | { |
| 1049 | if (!did_error) |
| 1050 | EMSG2("E630: %s(): write while not connected", fun); |
| 1051 | did_error = TRUE; |
| 1052 | } |
| 1053 | else if (sock_write(sd, buf, STRLEN(buf)) != (int)STRLEN(buf)) |
| 1054 | { |
| 1055 | if (!did_error) |
| 1056 | EMSG2("E631: %s(): write failed", fun); |
| 1057 | did_error = TRUE; |
| 1058 | } |
| 1059 | else |
| 1060 | did_error = FALSE; |
| 1061 | } |
| 1062 | |
| 1063 | /* |
| 1064 | * Some input received from netbeans requires a response. This function |
| 1065 | * handles a response with no information (except the command number). |
| 1066 | */ |
| 1067 | static void |
| 1068 | nb_reply_nil(int cmdno) |
| 1069 | { |
| 1070 | char reply[32]; |
| 1071 | |
| 1072 | if (!haveConnection) |
| 1073 | return; |
| 1074 | |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 1075 | nbdebug(("REP %d: <none>\n", cmdno)); |
| 1076 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1077 | sprintf(reply, "%d\n", cmdno); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1078 | nb_send(reply, "nb_reply_nil"); |
| 1079 | } |
| 1080 | |
| 1081 | |
| 1082 | /* |
| 1083 | * Send a response with text. |
| 1084 | * "result" must have been quoted already (using nb_quote()). |
| 1085 | */ |
| 1086 | static void |
| 1087 | nb_reply_text(int cmdno, char_u *result) |
| 1088 | { |
| 1089 | char_u *reply; |
| 1090 | |
| 1091 | if (!haveConnection) |
| 1092 | return; |
| 1093 | |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 1094 | nbdebug(("REP %d: %s\n", cmdno, (char *)result)); |
| 1095 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1096 | reply = alloc(STRLEN(result) + 32); |
| 1097 | sprintf((char *)reply, "%d %s\n", cmdno, (char *)result); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1098 | nb_send((char *)reply, "nb_reply_text"); |
| 1099 | |
| 1100 | vim_free(reply); |
| 1101 | } |
| 1102 | |
| 1103 | |
| 1104 | /* |
| 1105 | * Send a response with a number result code. |
| 1106 | */ |
| 1107 | static void |
| 1108 | nb_reply_nr(int cmdno, long result) |
| 1109 | { |
| 1110 | char reply[32]; |
| 1111 | |
| 1112 | if (!haveConnection) |
| 1113 | return; |
| 1114 | |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 1115 | nbdebug(("REP %d: %ld\n", cmdno, result)); |
| 1116 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1117 | sprintf(reply, "%d %ld\n", cmdno, result); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1118 | nb_send(reply, "nb_reply_nr"); |
| 1119 | } |
| 1120 | |
| 1121 | |
| 1122 | /* |
| 1123 | * Encode newline, ret, backslash, double quote for transmission to NetBeans. |
| 1124 | */ |
| 1125 | static char_u * |
| 1126 | nb_quote(char_u *txt) |
| 1127 | { |
| 1128 | char_u *buf = alloc(2 * STRLEN(txt) + 1); |
| 1129 | char_u *p = txt; |
| 1130 | char_u *q = buf; |
| 1131 | |
| 1132 | if (buf == NULL) |
| 1133 | return NULL; |
| 1134 | for (; *p; p++) |
| 1135 | { |
| 1136 | switch (*p) |
| 1137 | { |
| 1138 | case '\"': |
| 1139 | case '\\': |
| 1140 | *q++ = '\\'; *q++ = *p; break; |
| 1141 | /* case '\t': */ |
| 1142 | /* *q++ = '\\'; *q++ = 't'; break; */ |
| 1143 | case '\n': |
| 1144 | *q++ = '\\'; *q++ = 'n'; break; |
| 1145 | case '\r': |
| 1146 | *q++ = '\\'; *q++ = 'r'; break; |
| 1147 | default: |
| 1148 | *q++ = *p; |
| 1149 | break; |
| 1150 | } |
| 1151 | } |
| 1152 | *q++ = '\0'; |
| 1153 | |
| 1154 | return buf; |
| 1155 | } |
| 1156 | |
| 1157 | |
| 1158 | /* |
| 1159 | * Remove top level double quotes; convert backslashed chars. |
| 1160 | * Returns an allocated string (NULL for failure). |
| 1161 | * If "endp" is not NULL it is set to the character after the terminating |
| 1162 | * quote. |
| 1163 | */ |
| 1164 | static char * |
| 1165 | nb_unquote(char_u *p, char_u **endp) |
| 1166 | { |
| 1167 | char *result = 0; |
| 1168 | char *q; |
| 1169 | int done = 0; |
| 1170 | |
| 1171 | /* result is never longer than input */ |
| 1172 | result = (char *)alloc_clear(STRLEN(p) + 1); |
| 1173 | if (result == NULL) |
| 1174 | return NULL; |
| 1175 | |
| 1176 | if (*p++ != '"') |
| 1177 | { |
| 1178 | nbdebug(("nb_unquote called with string that doesn't start with a quote!: %s\n", |
| 1179 | p)); |
| 1180 | result[0] = NUL; |
| 1181 | return result; |
| 1182 | } |
| 1183 | |
| 1184 | for (q = result; !done && *p != NUL;) |
| 1185 | { |
| 1186 | switch (*p) |
| 1187 | { |
| 1188 | case '"': |
| 1189 | /* |
| 1190 | * Unbackslashed dquote marks the end, if first char was dquote. |
| 1191 | */ |
| 1192 | done = 1; |
| 1193 | break; |
| 1194 | |
| 1195 | case '\\': |
| 1196 | ++p; |
| 1197 | switch (*p) |
| 1198 | { |
| 1199 | case '\\': *q++ = '\\'; break; |
| 1200 | case 'n': *q++ = '\n'; break; |
| 1201 | case 't': *q++ = '\t'; break; |
| 1202 | case 'r': *q++ = '\r'; break; |
| 1203 | case '"': *q++ = '"'; break; |
| 1204 | case NUL: --p; break; |
| 1205 | /* default: skip over illegal chars */ |
| 1206 | } |
| 1207 | ++p; |
| 1208 | break; |
| 1209 | |
| 1210 | default: |
| 1211 | *q++ = *p++; |
| 1212 | } |
| 1213 | } |
| 1214 | |
| 1215 | if (endp != NULL) |
| 1216 | *endp = p; |
| 1217 | |
| 1218 | return result; |
| 1219 | } |
| 1220 | |
| 1221 | #define SKIP_STOP 2 |
| 1222 | #define streq(a,b) (strcmp(a,b) == 0) |
| 1223 | static int needupdate = 0; |
| 1224 | static int inAtomic = 0; |
| 1225 | |
| 1226 | /* |
| 1227 | * Do the actual processing of a single netbeans command or function. |
| 1228 | * The differance between a command and function is that a function |
| 1229 | * gets a response (its required) but a command does not. |
| 1230 | * For arguments see comment for nb_parse_cmd(). |
| 1231 | */ |
| 1232 | static int |
| 1233 | nb_do_cmd( |
| 1234 | int bufno, |
| 1235 | char_u *cmd, |
| 1236 | int func, |
| 1237 | int cmdno, |
| 1238 | char_u *args) /* points to space before arguments or NUL */ |
| 1239 | { |
| 1240 | int doupdate = 0; |
| 1241 | long off = 0; |
| 1242 | nbbuf_T *buf = nb_get_buf(bufno); |
| 1243 | static int skip = 0; |
| 1244 | int retval = OK; |
Bram Moolenaar | 349b2f6 | 2004-10-11 10:00:50 +0000 | [diff] [blame] | 1245 | char *cp; /* for when a char pointer is needed */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1246 | |
| 1247 | nbdebug(("%s %d: (%d) %s %s\n", (func) ? "FUN" : "CMD", cmdno, bufno, cmd, |
| 1248 | STRCMP(cmd, "insert") == 0 ? "<text>" : (char *)args)); |
| 1249 | |
| 1250 | if (func) |
| 1251 | { |
| 1252 | /* =====================================================================*/ |
| 1253 | if (streq((char *)cmd, "getModified")) |
| 1254 | { |
| 1255 | if (buf == NULL || buf->bufp == NULL) |
| 1256 | /* Return the number of buffers that are modified. */ |
| 1257 | nb_reply_nr(cmdno, (long)count_changed_buffers()); |
| 1258 | else |
| 1259 | /* Return whether the buffer is modified. */ |
| 1260 | nb_reply_nr(cmdno, (long)(buf->bufp->b_changed |
| 1261 | || isNetbeansModified(buf->bufp))); |
| 1262 | /* =====================================================================*/ |
| 1263 | } |
| 1264 | else if (streq((char *)cmd, "saveAndExit")) |
| 1265 | { |
| 1266 | /* Note: this will exit Vim if successful. */ |
| 1267 | coloncmd(":confirm qall"); |
| 1268 | |
| 1269 | /* We didn't exit: return the number of changed buffers. */ |
| 1270 | nb_reply_nr(cmdno, (long)count_changed_buffers()); |
| 1271 | /* =====================================================================*/ |
| 1272 | } |
| 1273 | else if (streq((char *)cmd, "getCursor")) |
| 1274 | { |
| 1275 | char_u text[200]; |
| 1276 | |
| 1277 | /* Note: nb_getbufno() may return -1. This indicates the IDE |
| 1278 | * didn't assign a number to the current buffer in response to a |
| 1279 | * fileOpened event. */ |
| 1280 | sprintf((char *)text, "%d %ld %d %ld", |
| 1281 | nb_getbufno(curbuf), |
| 1282 | (long)curwin->w_cursor.lnum, |
| 1283 | (int)curwin->w_cursor.col, |
| 1284 | pos2off(curbuf, &curwin->w_cursor)); |
| 1285 | nb_reply_text(cmdno, text); |
| 1286 | /* =====================================================================*/ |
| 1287 | } |
| 1288 | else if (streq((char *)cmd, "getLength")) |
| 1289 | { |
| 1290 | long len = 0; |
| 1291 | |
| 1292 | if (buf == NULL || buf->bufp == NULL) |
| 1293 | { |
| 1294 | nbdebug((" null bufp in getLength")); |
| 1295 | EMSG("E632: null bufp in getLength"); |
| 1296 | retval = FAIL; |
| 1297 | } |
| 1298 | else |
| 1299 | { |
| 1300 | len = get_buf_size(buf->bufp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1301 | } |
| 1302 | nb_reply_nr(cmdno, len); |
| 1303 | /* =====================================================================*/ |
| 1304 | } |
| 1305 | else if (streq((char *)cmd, "getText")) |
| 1306 | { |
| 1307 | long len; |
| 1308 | linenr_T nlines; |
| 1309 | char_u *text = NULL; |
| 1310 | linenr_T lno = 1; |
| 1311 | char_u *p; |
| 1312 | char_u *line; |
| 1313 | |
| 1314 | if (buf == NULL || buf->bufp == NULL) |
| 1315 | { |
| 1316 | nbdebug((" null bufp in getText")); |
| 1317 | EMSG("E633: null bufp in getText"); |
| 1318 | retval = FAIL; |
| 1319 | } |
| 1320 | else |
| 1321 | { |
| 1322 | len = get_buf_size(buf->bufp); |
| 1323 | nlines = buf->bufp->b_ml.ml_line_count; |
| 1324 | text = alloc((unsigned)((len > 0) |
| 1325 | ? ((len + nlines) * 2) : 4)); |
| 1326 | if (text == NULL) |
| 1327 | { |
| 1328 | nbdebug((" nb_do_cmd: getText has null text field\n")); |
| 1329 | retval = FAIL; |
| 1330 | } |
| 1331 | else |
| 1332 | { |
| 1333 | p = text; |
| 1334 | *p++ = '\"'; |
| 1335 | for (; lno <= nlines ; lno++) |
| 1336 | { |
| 1337 | line = nb_quote(ml_get_buf(buf->bufp, lno, FALSE)); |
| 1338 | if (line != NULL) |
| 1339 | { |
| 1340 | STRCPY(p, line); |
| 1341 | p += STRLEN(line); |
| 1342 | *p++ = '\\'; |
| 1343 | *p++ = 'n'; |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 1344 | vim_free(line); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1345 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1346 | } |
| 1347 | *p++ = '\"'; |
| 1348 | *p = '\0'; |
| 1349 | } |
| 1350 | } |
| 1351 | if (text == NULL) |
| 1352 | nb_reply_text(cmdno, (char_u *)""); |
| 1353 | else |
| 1354 | { |
| 1355 | nb_reply_text(cmdno, text); |
| 1356 | vim_free(text); |
| 1357 | } |
| 1358 | /* =====================================================================*/ |
| 1359 | } |
| 1360 | else if (streq((char *)cmd, "remove")) |
| 1361 | { |
| 1362 | long count; |
| 1363 | pos_T first, last; |
| 1364 | pos_T *pos; |
| 1365 | int oldFire = netbeansFireChanges; |
| 1366 | int oldSuppress = netbeansSuppressNoLines; |
| 1367 | int wasChanged; |
| 1368 | |
| 1369 | if (skip >= SKIP_STOP) |
| 1370 | { |
| 1371 | nbdebug((" Skipping %s command\n", (char *) cmd)); |
| 1372 | nb_reply_nil(cmdno); |
| 1373 | return OK; |
| 1374 | } |
| 1375 | |
| 1376 | if (buf == NULL || buf->bufp == NULL) |
| 1377 | { |
| 1378 | nbdebug((" null bufp in remove")); |
| 1379 | EMSG("E634: null bufp in remove"); |
| 1380 | retval = FAIL; |
| 1381 | } |
| 1382 | else |
| 1383 | { |
| 1384 | netbeansFireChanges = FALSE; |
| 1385 | netbeansSuppressNoLines = TRUE; |
| 1386 | |
| 1387 | if (curbuf != buf->bufp) |
| 1388 | set_curbuf(buf->bufp, DOBUF_GOTO); |
| 1389 | wasChanged = buf->bufp->b_changed; |
Bram Moolenaar | 349b2f6 | 2004-10-11 10:00:50 +0000 | [diff] [blame] | 1390 | cp = (char *)args; |
| 1391 | off = strtol(cp, &cp, 10); |
| 1392 | count = strtol(cp, &cp, 10); |
| 1393 | args = (char_u *)cp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1394 | /* delete "count" chars, starting at "off" */ |
| 1395 | pos = off2pos(buf->bufp, off); |
| 1396 | if (!pos) |
| 1397 | { |
| 1398 | nb_reply_text(cmdno, (char_u *)"!bad position"); |
| 1399 | netbeansFireChanges = oldFire; |
| 1400 | netbeansSuppressNoLines = oldSuppress; |
| 1401 | return FAIL; |
| 1402 | } |
| 1403 | first = *pos; |
| 1404 | nbdebug((" FIRST POS: line %d, col %d\n", first.lnum, first.col)); |
| 1405 | pos = off2pos(buf->bufp, off+count-1); |
| 1406 | if (!pos) |
| 1407 | { |
| 1408 | nb_reply_text(cmdno, (char_u *)"!bad count"); |
| 1409 | netbeansFireChanges = oldFire; |
| 1410 | netbeansSuppressNoLines = oldSuppress; |
| 1411 | return FAIL; |
| 1412 | } |
| 1413 | last = *pos; |
| 1414 | nbdebug((" LAST POS: line %d, col %d\n", last.lnum, last.col)); |
| 1415 | curwin->w_cursor = first; |
| 1416 | doupdate = 1; |
| 1417 | |
| 1418 | /* keep part of first line */ |
| 1419 | if (first.lnum == last.lnum && first.col != last.col) |
| 1420 | { |
| 1421 | /* deletion is within one line */ |
| 1422 | char_u *p = ml_get(first.lnum); |
| 1423 | mch_memmove(p + first.col, p + last.col + 1, STRLEN(p + last.col) + 1); |
| 1424 | nbdebug((" NEW LINE %d: %s\n", first.lnum, p)); |
| 1425 | ml_replace(first.lnum, p, TRUE); |
| 1426 | } |
| 1427 | |
| 1428 | if (first.lnum < last.lnum) |
| 1429 | { |
| 1430 | int i; |
| 1431 | |
| 1432 | /* delete signs from the lines being deleted */ |
| 1433 | for (i = first.lnum; i <= last.lnum; i++) |
| 1434 | { |
| 1435 | int id = buf_findsign_id(buf->bufp, (linenr_T)i); |
| 1436 | if (id > 0) |
| 1437 | { |
| 1438 | nbdebug((" Deleting sign %d on line %d\n", id, i)); |
| 1439 | buf_delsign(buf->bufp, id); |
| 1440 | } |
| 1441 | else |
| 1442 | nbdebug((" No sign on line %d\n", i)); |
| 1443 | } |
| 1444 | |
| 1445 | /* delete whole lines */ |
| 1446 | nbdebug((" Deleting lines %d through %d\n", first.lnum, last.lnum)); |
| 1447 | del_lines(last.lnum - first.lnum + 1, FALSE); |
| 1448 | } |
| 1449 | buf->bufp->b_changed = wasChanged; /* logically unchanged */ |
| 1450 | netbeansFireChanges = oldFire; |
| 1451 | netbeansSuppressNoLines = oldSuppress; |
| 1452 | |
| 1453 | u_blockfree(buf->bufp); |
| 1454 | u_clearall(buf->bufp); |
| 1455 | } |
| 1456 | nb_reply_nil(cmdno); |
| 1457 | /* =====================================================================*/ |
| 1458 | } |
| 1459 | else if (streq((char *)cmd, "insert")) |
| 1460 | { |
| 1461 | pos_T *pos; |
| 1462 | pos_T mypos; |
| 1463 | char_u *to_free; |
| 1464 | char_u *nl; |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 1465 | linenr_T lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1466 | pos_T old_w_cursor; |
| 1467 | int old_b_changed; |
| 1468 | |
| 1469 | if (skip >= SKIP_STOP) |
| 1470 | { |
| 1471 | nbdebug((" Skipping %s command\n", (char *) cmd)); |
| 1472 | nb_reply_nil(cmdno); |
| 1473 | return OK; |
| 1474 | } |
| 1475 | |
| 1476 | /* get offset */ |
Bram Moolenaar | 349b2f6 | 2004-10-11 10:00:50 +0000 | [diff] [blame] | 1477 | cp = (char *)args; |
| 1478 | off = strtol(cp, &cp, 10); |
| 1479 | args = (char_u *)cp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1480 | |
| 1481 | /* get text to be inserted */ |
| 1482 | args = skipwhite(args); |
| 1483 | args = to_free = (char_u *)nb_unquote(args, NULL); |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 1484 | /* |
| 1485 | nbdebug((" CHUNK[%d]: %d bytes at offset %d\n", |
| 1486 | buf->bufp->b_ml.ml_line_count, STRLEN(args), off)); |
| 1487 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1488 | |
| 1489 | if (buf == NULL || buf->bufp == NULL) |
| 1490 | { |
| 1491 | nbdebug((" null bufp in insert")); |
| 1492 | EMSG("E635: null bufp in insert"); |
| 1493 | retval = FAIL; |
| 1494 | } |
| 1495 | else if (args != NULL) |
| 1496 | { |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 1497 | /* |
| 1498 | * We need to detect EOL style |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1499 | * because addAnno passes char-offset |
| 1500 | */ |
| 1501 | int ff_detected = EOL_UNKNOWN; |
| 1502 | int buf_was_empty = (buf->bufp->b_ml.ml_flags & ML_EMPTY); |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 1503 | char_u lbuf[4096]; /* size of largest insert sent by exted */ |
| 1504 | int lbuf_len = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1505 | |
| 1506 | oldFire = netbeansFireChanges; |
| 1507 | netbeansFireChanges = 0; |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 1508 | lbuf[0] = '\0'; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1509 | |
| 1510 | if (curbuf != buf->bufp) |
| 1511 | set_curbuf(buf->bufp, DOBUF_GOTO); |
| 1512 | old_b_changed = buf->bufp->b_changed; |
| 1513 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1514 | pos = off2pos(buf->bufp, off); |
| 1515 | if (pos) |
| 1516 | { |
| 1517 | if (pos->lnum == 0) |
| 1518 | pos->lnum = 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1519 | } |
| 1520 | else |
| 1521 | { |
| 1522 | /* if the given position is not found, assume we want |
| 1523 | * the end of the file. See setLocAndSize HACK. */ |
| 1524 | pos = &mypos; |
| 1525 | pos->col = 0; |
| 1526 | #ifdef FEAT_VIRTUALEDIT |
| 1527 | pos->coladd = 0; |
| 1528 | #endif |
| 1529 | pos->lnum = buf->bufp->b_ml.ml_line_count; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1530 | } |
| 1531 | lnum = pos->lnum; |
| 1532 | old_w_cursor = curwin->w_cursor; |
| 1533 | curwin->w_cursor = *pos; |
| 1534 | |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 1535 | if (buf->bufp->b_start_eol == FALSE && lnum > 0) |
| 1536 | { |
| 1537 | /* Append to a partial line */ |
| 1538 | char_u *partial = ml_get(lnum); |
| 1539 | |
| 1540 | if (partial != IObuff) |
| 1541 | STRCPY(lbuf, partial); |
| 1542 | lbuf_len = STRLEN(partial); |
| 1543 | ml_delete(lnum, FALSE); |
| 1544 | } |
| 1545 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1546 | doupdate = 1; |
| 1547 | while (*args) |
| 1548 | { |
| 1549 | nl = (char_u *)strchr((char *)args, '\n'); |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 1550 | if (nl) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1551 | { |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 1552 | STRNCAT(lbuf, args, nl - args); |
| 1553 | lbuf[lbuf_len + nl - args] = '\0'; |
| 1554 | args += nl - args + 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1555 | } |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 1556 | else |
| 1557 | { |
| 1558 | STRCPY(lbuf, args); |
| 1559 | args += STRLEN(lbuf); |
| 1560 | } |
| 1561 | |
| 1562 | /* |
| 1563 | * EOL detecting. Not sure how to deal with '\n' on Mac. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1564 | */ |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 1565 | if (buf_was_empty && nl && *(nl - 1) != '\r') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1566 | ff_detected = EOL_UNIX; |
| 1567 | |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 1568 | /* nbdebug((" INSERT[%d]: %s\n", lnum, lbuf)); */ |
| 1569 | ml_append((linenr_T)(lnum++ - 1), lbuf, |
| 1570 | STRLEN(lbuf) + 1, FALSE); |
| 1571 | lbuf[0] = '\0'; /* empty buffer */ |
| 1572 | lbuf_len = 0; |
| 1573 | } |
| 1574 | |
| 1575 | if (*(args - 1) == '\n') |
| 1576 | { |
| 1577 | buf->bufp->b_p_eol = TRUE; |
| 1578 | buf->bufp->b_start_eol = TRUE; |
| 1579 | } |
| 1580 | else |
| 1581 | { |
| 1582 | buf->bufp->b_p_eol = FALSE; |
| 1583 | buf->bufp->b_start_eol = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1584 | } |
| 1585 | |
| 1586 | appended_lines_mark(pos->lnum - 1, lnum - pos->lnum); |
| 1587 | |
| 1588 | /* We can change initial ff without consequences |
| 1589 | * Isn't it a kind of hacking? |
| 1590 | */ |
| 1591 | if (buf_was_empty) |
| 1592 | { |
| 1593 | if (ff_detected == EOL_UNKNOWN) |
| 1594 | ff_detected = EOL_DOS; |
| 1595 | set_fileformat(ff_detected, OPT_LOCAL); |
| 1596 | buf->bufp->b_start_ffc = *buf->bufp->b_p_ff; |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 1597 | ml_delete(curbuf->b_ml.ml_line_count, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1598 | } |
| 1599 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1600 | curwin->w_cursor = old_w_cursor; |
| 1601 | |
| 1602 | /* |
| 1603 | * XXX - GRP - Is the next line right? If I've inserted |
| 1604 | * text the buffer has been updated but not written. Will |
| 1605 | * netbeans guarantee to write it? Even if I do a :q! ? |
| 1606 | */ |
| 1607 | buf->bufp->b_changed = old_b_changed; /* logically unchanged */ |
| 1608 | netbeansFireChanges = oldFire; |
| 1609 | |
| 1610 | u_blockfree(buf->bufp); |
| 1611 | u_clearall(buf->bufp); |
| 1612 | } |
| 1613 | vim_free(to_free); |
| 1614 | nb_reply_nil(cmdno); /* or !error */ |
| 1615 | } |
| 1616 | else |
| 1617 | { |
| 1618 | nbdebug(("UNIMPLEMENTED FUNCTION: %s\n", cmd)); |
| 1619 | nb_reply_nil(cmdno); |
| 1620 | retval = FAIL; |
| 1621 | } |
| 1622 | } |
| 1623 | else /* Not a function; no reply required. */ |
| 1624 | { |
| 1625 | /* =====================================================================*/ |
| 1626 | if (streq((char *)cmd, "create")) |
| 1627 | { |
| 1628 | /* Create a buffer without a name. */ |
| 1629 | if (buf == NULL) |
| 1630 | { |
| 1631 | EMSG("E636: null buf in create"); |
| 1632 | return FAIL; |
| 1633 | } |
| 1634 | vim_free(buf->displayname); |
| 1635 | buf->displayname = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1636 | |
| 1637 | netbeansReadFile = 0; /* don't try to open disk file */ |
| 1638 | do_ecmd(0, NULL, 0, 0, ECMD_ONE, ECMD_HIDE + ECMD_OLDBUF); |
| 1639 | netbeansReadFile = 1; |
| 1640 | buf->bufp = curbuf; |
| 1641 | maketitle(); |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 1642 | buf->insertDone = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1643 | gui_update_menus(0); |
| 1644 | /* =====================================================================*/ |
| 1645 | } |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 1646 | else if (streq((char *)cmd, "insertDone")) |
| 1647 | { |
| 1648 | buf->bufp->b_start_eol = *args == 'T'; |
| 1649 | buf->insertDone = TRUE; |
| 1650 | args += 2; |
| 1651 | buf->bufp->b_p_ro = *args == 'T'; |
| 1652 | print_read_msg(buf); |
| 1653 | /* =====================================================================*/ |
| 1654 | } |
| 1655 | else if (streq((char *)cmd, "saveDone")) |
| 1656 | { |
| 1657 | long savedChars = atol((char *) args); |
| 1658 | print_save_msg(buf, savedChars); |
| 1659 | /* =====================================================================*/ |
| 1660 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1661 | else if (streq((char *)cmd, "startDocumentListen")) |
| 1662 | { |
| 1663 | if (buf == NULL) |
| 1664 | { |
| 1665 | EMSG("E637: null buf in startDocumentListen"); |
| 1666 | return FAIL; |
| 1667 | } |
| 1668 | buf->fireChanges = 1; |
| 1669 | /* =====================================================================*/ |
| 1670 | } |
| 1671 | else if (streq((char *)cmd, "stopDocumentListen")) |
| 1672 | { |
| 1673 | if (buf == NULL) |
| 1674 | { |
| 1675 | EMSG("E638: null buf in stopDocumentListen"); |
| 1676 | return FAIL; |
| 1677 | } |
| 1678 | buf->fireChanges = 0; |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 1679 | if (buf->bufp != NULL) |
| 1680 | { |
| 1681 | if (buf->bufp->b_was_netbeans_file |
| 1682 | && !buf->bufp->b_netbeans_file) |
| 1683 | EMSGN(_("E658: NetBeans connection lost for buffer %ld"), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1684 | buf->bufp->b_fnum); |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 1685 | else |
| 1686 | { |
| 1687 | do_bufdel(DOBUF_DEL, (char_u *)"", 1, buf->bufp->b_fnum, |
| 1688 | buf->bufp->b_fnum, TRUE); |
| 1689 | /* add_to_input_buf((char_u *)"\f", 1); */ |
| 1690 | vim_memset(buf, 0, sizeof(nbbuf_T)); |
| 1691 | } |
| 1692 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1693 | /* =====================================================================*/ |
| 1694 | } |
| 1695 | else if (streq((char *)cmd, "setTitle")) |
| 1696 | { |
| 1697 | if (buf == NULL) |
| 1698 | { |
| 1699 | EMSG("E639: null buf in setTitle"); |
| 1700 | return FAIL; |
| 1701 | } |
| 1702 | vim_free(buf->displayname); |
| 1703 | buf->displayname = nb_unquote(args, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1704 | /* =====================================================================*/ |
| 1705 | } |
| 1706 | else if (streq((char *)cmd, "initDone")) |
| 1707 | { |
| 1708 | if (buf == NULL || buf->bufp == NULL) |
| 1709 | { |
| 1710 | EMSG("E640: null buf in initDone"); |
| 1711 | return FAIL; |
| 1712 | } |
| 1713 | doupdate = 1; |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 1714 | buf->initDone = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1715 | if (curbuf != buf->bufp) |
| 1716 | set_curbuf(buf->bufp, DOBUF_GOTO); |
| 1717 | #if defined(FEAT_AUTOCMD) |
| 1718 | apply_autocmds(EVENT_BUFREADPOST, 0, 0, FALSE, buf->bufp); |
| 1719 | #endif |
| 1720 | |
| 1721 | /* handle any postponed key commands */ |
| 1722 | handle_key_queue(); |
| 1723 | /* =====================================================================*/ |
| 1724 | } |
| 1725 | else if (streq((char *)cmd, "setBufferNumber") |
| 1726 | || streq((char *)cmd, "putBufferNumber")) |
| 1727 | { |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 1728 | char_u *path; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1729 | buf_T *bufp; |
| 1730 | |
| 1731 | if (buf == NULL) |
| 1732 | { |
| 1733 | EMSG("E641: null buf in setBufferNumber"); |
| 1734 | return FAIL; |
| 1735 | } |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 1736 | path = (char_u *)nb_unquote(args, NULL); |
| 1737 | if (path == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1738 | return FAIL; |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 1739 | bufp = buflist_findname(path); |
| 1740 | vim_free(path); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1741 | if (bufp == NULL) |
| 1742 | { |
| 1743 | EMSG2("E642: File %s not found in setBufferNumber", args); |
| 1744 | return FAIL; |
| 1745 | } |
| 1746 | buf->bufp = bufp; |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 1747 | buf->nbbuf_number = bufp->b_fnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1748 | |
| 1749 | /* "setBufferNumber" has the side effect of jumping to the buffer |
| 1750 | * (don't know why!). Don't do that for "putBufferNumber". */ |
| 1751 | if (*cmd != 'p') |
| 1752 | coloncmd(":buffer %d", bufp->b_fnum); |
| 1753 | else |
| 1754 | { |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 1755 | buf->initDone = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1756 | |
| 1757 | /* handle any postponed key commands */ |
| 1758 | handle_key_queue(); |
| 1759 | } |
| 1760 | |
| 1761 | #if 0 /* never used */ |
| 1762 | buf->internalname = (char *)alloc_clear(8); |
| 1763 | sprintf(buf->internalname, "<%d>", bufno); |
| 1764 | buf->netbeansOwns = 0; |
| 1765 | #endif |
| 1766 | /* =====================================================================*/ |
| 1767 | } |
| 1768 | else if (streq((char *)cmd, "setFullName")) |
| 1769 | { |
| 1770 | if (buf == NULL) |
| 1771 | { |
| 1772 | EMSG("E643: null buf in setFullName"); |
| 1773 | return FAIL; |
| 1774 | } |
| 1775 | vim_free(buf->displayname); |
| 1776 | buf->displayname = nb_unquote(args, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1777 | |
| 1778 | netbeansReadFile = 0; /* don't try to open disk file */ |
| 1779 | do_ecmd(0, (char_u *)buf->displayname, 0, 0, ECMD_ONE, |
| 1780 | ECMD_HIDE + ECMD_OLDBUF); |
| 1781 | netbeansReadFile = 1; |
| 1782 | buf->bufp = curbuf; |
| 1783 | maketitle(); |
| 1784 | gui_update_menus(0); |
| 1785 | /* =====================================================================*/ |
| 1786 | } |
| 1787 | else if (streq((char *)cmd, "editFile")) |
| 1788 | { |
| 1789 | if (buf == NULL) |
| 1790 | { |
| 1791 | EMSG("E644: null buf in editFile"); |
| 1792 | return FAIL; |
| 1793 | } |
| 1794 | /* Edit a file: like create + setFullName + read the file. */ |
| 1795 | vim_free(buf->displayname); |
| 1796 | buf->displayname = nb_unquote(args, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1797 | do_ecmd(0, (char_u *)buf->displayname, NULL, NULL, ECMD_ONE, |
| 1798 | ECMD_HIDE + ECMD_OLDBUF); |
| 1799 | buf->bufp = curbuf; |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 1800 | buf->initDone = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1801 | doupdate = 1; |
| 1802 | #if defined(FEAT_TITLE) |
| 1803 | maketitle(); |
| 1804 | #endif |
| 1805 | gui_update_menus(0); |
| 1806 | /* =====================================================================*/ |
| 1807 | } |
| 1808 | else if (streq((char *)cmd, "setVisible")) |
| 1809 | { |
| 1810 | if (buf == NULL || buf->bufp == NULL) |
| 1811 | { |
| 1812 | /* EMSG("E645: null bufp in setVisible"); */ |
| 1813 | return FAIL; |
| 1814 | } |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 1815 | if (streq((char *)args, "T") && buf->bufp != curbuf) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1816 | { |
| 1817 | exarg_T exarg; |
| 1818 | exarg.cmd = (char_u *)"goto"; |
| 1819 | exarg.forceit = FALSE; |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 1820 | dosetvisible = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1821 | goto_buffer(&exarg, DOBUF_FIRST, FORWARD, buf->bufp->b_fnum); |
| 1822 | doupdate = 1; |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 1823 | dosetvisible = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1824 | |
| 1825 | /* Side effect!!!. */ |
| 1826 | if (!gui.starting) |
| 1827 | gui_mch_set_foreground(); |
| 1828 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1829 | /* =====================================================================*/ |
| 1830 | } |
| 1831 | else if (streq((char *)cmd, "raise")) |
| 1832 | { |
| 1833 | /* Bring gvim to the foreground. */ |
| 1834 | if (!gui.starting) |
| 1835 | gui_mch_set_foreground(); |
| 1836 | /* =====================================================================*/ |
| 1837 | } |
| 1838 | else if (streq((char *)cmd, "setModified")) |
| 1839 | { |
| 1840 | if (buf == NULL || buf->bufp == NULL) |
| 1841 | { |
| 1842 | /* EMSG("E646: null bufp in setModified"); */ |
| 1843 | return FAIL; |
| 1844 | } |
| 1845 | if (streq((char *)args, "T")) |
| 1846 | buf->bufp->b_changed = 1; |
| 1847 | else |
| 1848 | { |
| 1849 | struct stat st; |
| 1850 | |
| 1851 | /* Assume NetBeans stored the file. Reset the timestamp to |
| 1852 | * avoid "file changed" warnings. */ |
| 1853 | if (buf->bufp->b_ffname != NULL |
| 1854 | && mch_stat((char *)buf->bufp->b_ffname, &st) >= 0) |
| 1855 | buf_store_time(buf->bufp, &st, buf->bufp->b_ffname); |
| 1856 | buf->bufp->b_changed = 0; |
| 1857 | } |
| 1858 | buf->modified = buf->bufp->b_changed; |
| 1859 | /* =====================================================================*/ |
| 1860 | } |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 1861 | else if (streq((char *)cmd, "setModtime")) |
| 1862 | { |
| 1863 | buf->bufp->b_mtime = atoi((char *) args); |
| 1864 | /* =====================================================================*/ |
| 1865 | } |
| 1866 | else if (streq((char *)cmd, "setReadOnly")) |
| 1867 | { |
| 1868 | if (streq((char *)args, "T")) |
| 1869 | buf->bufp->b_p_ro = TRUE; |
| 1870 | else |
| 1871 | buf->bufp->b_p_ro = FALSE; |
| 1872 | /* =====================================================================*/ |
| 1873 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1874 | else if (streq((char *)cmd, "setMark")) |
| 1875 | { |
| 1876 | /* not yet */ |
| 1877 | /* =====================================================================*/ |
| 1878 | } |
| 1879 | else if (streq((char *)cmd, "showBalloon")) |
| 1880 | { |
| 1881 | #if defined(FEAT_BEVAL) |
| 1882 | static char *text = NULL; |
| 1883 | |
| 1884 | /* |
| 1885 | * Set up the Balloon Expression Evaluation area. |
| 1886 | * Ignore 'ballooneval' here. |
| 1887 | * The text pointer must remain valid for a while. |
| 1888 | */ |
| 1889 | if (balloonEval != NULL) |
| 1890 | { |
| 1891 | vim_free(text); |
| 1892 | text = nb_unquote(args, NULL); |
| 1893 | if (text != NULL) |
| 1894 | gui_mch_post_balloon(balloonEval, (char_u *)text); |
| 1895 | } |
| 1896 | #endif |
| 1897 | /* =====================================================================*/ |
| 1898 | } |
| 1899 | else if (streq((char *)cmd, "setDot")) |
| 1900 | { |
| 1901 | pos_T *pos; |
| 1902 | #ifdef NBDEBUG |
| 1903 | char_u *s; |
| 1904 | #endif |
| 1905 | |
| 1906 | if (buf == NULL || buf->bufp == NULL) |
| 1907 | { |
| 1908 | EMSG("E647: null bufp in setDot"); |
| 1909 | return FAIL; |
| 1910 | } |
| 1911 | |
| 1912 | if (curbuf != buf->bufp) |
| 1913 | set_curbuf(buf->bufp, DOBUF_GOTO); |
| 1914 | #ifdef FEAT_VISUAL |
| 1915 | /* Don't want Visual mode now. */ |
| 1916 | if (VIsual_active) |
| 1917 | end_visual_mode(); |
| 1918 | #endif |
| 1919 | #ifdef NBDEBUG |
| 1920 | s = args; |
| 1921 | #endif |
| 1922 | pos = get_off_or_lnum(buf->bufp, &args); |
| 1923 | if (pos) |
| 1924 | { |
| 1925 | curwin->w_cursor = *pos; |
| 1926 | check_cursor(); |
| 1927 | #ifdef FEAT_FOLDING |
| 1928 | foldOpenCursor(); |
| 1929 | #endif |
| 1930 | } |
| 1931 | else |
| 1932 | nbdebug((" BAD POSITION in setDot: %s\n", s)); |
| 1933 | |
| 1934 | /* gui_update_cursor(TRUE, FALSE); */ |
| 1935 | /* update_curbuf(NOT_VALID); */ |
| 1936 | update_topline(); /* scroll to show the line */ |
| 1937 | update_screen(VALID); |
| 1938 | setcursor(); |
| 1939 | out_flush(); |
| 1940 | gui_update_cursor(TRUE, FALSE); |
| 1941 | gui_mch_flush(); |
| 1942 | /* Quit a hit-return or more prompt. */ |
| 1943 | if (State == HITRETURN || State == ASKMORE) |
| 1944 | { |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 1945 | /* add_to_input_buf((char_u *)"\003", 1); */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1946 | #ifdef FEAT_GUI_GTK |
| 1947 | if (gtk_main_level() > 0) |
| 1948 | gtk_main_quit(); |
| 1949 | #endif |
| 1950 | } |
| 1951 | /* =====================================================================*/ |
| 1952 | } |
| 1953 | else if (streq((char *)cmd, "close")) |
| 1954 | { |
| 1955 | #ifdef NBDEBUG |
| 1956 | char *name = "<NONE>"; |
| 1957 | #endif |
| 1958 | |
| 1959 | if (buf == NULL) |
| 1960 | { |
| 1961 | EMSG("E648: null buf in close"); |
| 1962 | return FAIL; |
| 1963 | } |
| 1964 | |
| 1965 | #ifdef NBDEBUG |
| 1966 | if (buf->displayname != NULL) |
| 1967 | name = buf->displayname; |
| 1968 | #endif |
| 1969 | /* if (buf->bufp == NULL) */ |
| 1970 | /* EMSG("E649: null bufp in close"); */ |
| 1971 | nbdebug((" CLOSE %d: %s\n", bufno, name)); |
| 1972 | need_mouse_correct = TRUE; |
| 1973 | if (buf->bufp != NULL) |
| 1974 | do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, |
| 1975 | buf->bufp->b_fnum, TRUE); |
| 1976 | doupdate = 1; |
| 1977 | /* =====================================================================*/ |
| 1978 | } |
| 1979 | else if (streq((char *)cmd, "setStyle")) /* obsolete... */ |
| 1980 | { |
| 1981 | nbdebug((" setStyle is obsolete!")); |
| 1982 | /* =====================================================================*/ |
| 1983 | } |
| 1984 | else if (streq((char *)cmd, "setExitDelay")) |
| 1985 | { |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 1986 | /* Only used in version 2.1. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1987 | /* =====================================================================*/ |
| 1988 | } |
| 1989 | else if (streq((char *)cmd, "defineAnnoType")) |
| 1990 | { |
| 1991 | #ifdef FEAT_SIGNS |
| 1992 | int typeNum; |
| 1993 | char_u *typeName; |
| 1994 | char_u *tooltip; |
| 1995 | char_u *p; |
| 1996 | char_u *glyphFile; |
| 1997 | int use_fg = 0; |
| 1998 | int use_bg = 0; |
| 1999 | int fg = -1; |
| 2000 | int bg = -1; |
| 2001 | |
| 2002 | if (buf == NULL) |
| 2003 | { |
| 2004 | EMSG("E650: null buf in defineAnnoType"); |
| 2005 | return FAIL; |
| 2006 | } |
| 2007 | |
Bram Moolenaar | 349b2f6 | 2004-10-11 10:00:50 +0000 | [diff] [blame] | 2008 | cp = (char *)args; |
| 2009 | typeNum = strtol(cp, &cp, 10); |
| 2010 | args = (char_u *)cp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2011 | args = skipwhite(args); |
| 2012 | typeName = (char_u *)nb_unquote(args, &args); |
| 2013 | args = skipwhite(args + 1); |
| 2014 | tooltip = (char_u *)nb_unquote(args, &args); |
| 2015 | args = skipwhite(args + 1); |
| 2016 | |
| 2017 | p = (char_u *)nb_unquote(args, &args); |
| 2018 | glyphFile = vim_strsave_escaped(p, escape_chars); |
| 2019 | vim_free(p); |
| 2020 | |
| 2021 | args = skipwhite(args + 1); |
| 2022 | if (STRNCMP(args, "none", 4) == 0) |
| 2023 | args += 5; |
| 2024 | else |
| 2025 | { |
| 2026 | use_fg = 1; |
Bram Moolenaar | 349b2f6 | 2004-10-11 10:00:50 +0000 | [diff] [blame] | 2027 | cp = (char *)args; |
| 2028 | fg = strtol(cp, &cp, 10); |
| 2029 | args = (char_u *)cp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2030 | } |
| 2031 | if (STRNCMP(args, "none", 4) == 0) |
| 2032 | args += 5; |
| 2033 | else |
| 2034 | { |
| 2035 | use_bg = 1; |
Bram Moolenaar | 349b2f6 | 2004-10-11 10:00:50 +0000 | [diff] [blame] | 2036 | cp = (char *)args; |
| 2037 | bg = strtol(cp, &cp, 10); |
| 2038 | args = (char_u *)cp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2039 | } |
| 2040 | if (typeName != NULL && tooltip != NULL && glyphFile != NULL) |
| 2041 | addsigntype(buf, typeNum, typeName, tooltip, glyphFile, |
| 2042 | use_fg, fg, use_bg, bg); |
| 2043 | else |
| 2044 | vim_free(typeName); |
| 2045 | |
| 2046 | /* don't free typeName; it's used directly in addsigntype() */ |
| 2047 | vim_free(tooltip); |
| 2048 | vim_free(glyphFile); |
| 2049 | |
| 2050 | #endif |
| 2051 | /* =====================================================================*/ |
| 2052 | } |
| 2053 | else if (streq((char *)cmd, "addAnno")) |
| 2054 | { |
| 2055 | #ifdef FEAT_SIGNS |
| 2056 | int serNum; |
| 2057 | int localTypeNum; |
| 2058 | int typeNum; |
| 2059 | # ifdef NBDEBUG |
| 2060 | int len; |
| 2061 | # endif |
| 2062 | pos_T *pos; |
| 2063 | |
| 2064 | if (buf == NULL || buf->bufp == NULL) |
| 2065 | { |
| 2066 | EMSG("E651: null bufp in addAnno"); |
| 2067 | return FAIL; |
| 2068 | } |
| 2069 | |
| 2070 | doupdate = 1; |
| 2071 | |
Bram Moolenaar | 349b2f6 | 2004-10-11 10:00:50 +0000 | [diff] [blame] | 2072 | cp = (char *)args; |
| 2073 | serNum = strtol(cp, &cp, 10); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2074 | |
| 2075 | /* Get the typenr specific for this buffer and convert it to |
| 2076 | * the global typenumber, as used for the sign name. */ |
Bram Moolenaar | 349b2f6 | 2004-10-11 10:00:50 +0000 | [diff] [blame] | 2077 | localTypeNum = strtol(cp, &cp, 10); |
| 2078 | args = (char_u *)cp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2079 | typeNum = mapsigntype(buf, localTypeNum); |
| 2080 | |
| 2081 | pos = get_off_or_lnum(buf->bufp, &args); |
| 2082 | |
Bram Moolenaar | 349b2f6 | 2004-10-11 10:00:50 +0000 | [diff] [blame] | 2083 | cp = (char *)args; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2084 | # ifdef NBDEBUG |
| 2085 | len = |
| 2086 | # endif |
Bram Moolenaar | 349b2f6 | 2004-10-11 10:00:50 +0000 | [diff] [blame] | 2087 | strtol(cp, &cp, 10); |
| 2088 | args = (char_u *)cp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2089 | # ifdef NBDEBUG |
| 2090 | if (len != -1) |
| 2091 | { |
| 2092 | nbdebug((" partial line annotation -- Not Yet Implemented!")); |
| 2093 | } |
| 2094 | # endif |
| 2095 | if (serNum >= GUARDEDOFFSET) |
| 2096 | { |
| 2097 | nbdebug((" too many annotations! ignoring...")); |
| 2098 | return FAIL; |
| 2099 | } |
| 2100 | if (pos) |
| 2101 | { |
| 2102 | coloncmd(":sign place %d line=%d name=%d buffer=%d", |
| 2103 | serNum, pos->lnum, typeNum, buf->bufp->b_fnum); |
| 2104 | if (typeNum == curPCtype) |
| 2105 | coloncmd(":sign jump %d buffer=%d", serNum, |
| 2106 | buf->bufp->b_fnum); |
| 2107 | } |
| 2108 | /* XXX only redraw what changed. */ |
| 2109 | redraw_later(CLEAR); |
| 2110 | #endif |
| 2111 | /* =====================================================================*/ |
| 2112 | } |
| 2113 | else if (streq((char *)cmd, "removeAnno")) |
| 2114 | { |
| 2115 | #ifdef FEAT_SIGNS |
| 2116 | int serNum; |
| 2117 | |
| 2118 | if (buf == NULL || buf->bufp == NULL) |
| 2119 | { |
| 2120 | nbdebug((" null bufp in removeAnno")); |
| 2121 | return FAIL; |
| 2122 | } |
| 2123 | doupdate = 1; |
Bram Moolenaar | 349b2f6 | 2004-10-11 10:00:50 +0000 | [diff] [blame] | 2124 | cp = (char *)args; |
| 2125 | serNum = strtol(cp, &cp, 10); |
| 2126 | args = (char_u *)cp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2127 | coloncmd(":sign unplace %d buffer=%d", |
| 2128 | serNum, buf->bufp->b_fnum); |
| 2129 | redraw_buf_later(buf->bufp, NOT_VALID); |
| 2130 | #endif |
| 2131 | /* =====================================================================*/ |
| 2132 | } |
| 2133 | else if (streq((char *)cmd, "moveAnnoToFront")) |
| 2134 | { |
| 2135 | #ifdef FEAT_SIGNS |
| 2136 | nbdebug((" moveAnnoToFront: Not Yet Implemented!")); |
| 2137 | #endif |
| 2138 | /* =====================================================================*/ |
| 2139 | } |
| 2140 | else if (streq((char *)cmd, "guard") || streq((char *)cmd, "unguard")) |
| 2141 | { |
| 2142 | int len; |
| 2143 | pos_T first; |
| 2144 | pos_T last; |
| 2145 | pos_T *pos; |
| 2146 | int un = (cmd[0] == 'u'); |
| 2147 | static int guardId = GUARDEDOFFSET; |
| 2148 | |
| 2149 | if (skip >= SKIP_STOP) |
| 2150 | { |
| 2151 | nbdebug((" Skipping %s command\n", (char *) cmd)); |
| 2152 | return OK; |
| 2153 | } |
| 2154 | |
| 2155 | nb_init_graphics(); |
| 2156 | |
| 2157 | if (buf == NULL || buf->bufp == NULL) |
| 2158 | { |
| 2159 | nbdebug((" null bufp in %s command", cmd)); |
| 2160 | return FAIL; |
| 2161 | } |
| 2162 | if (curbuf != buf->bufp) |
| 2163 | set_curbuf(buf->bufp, DOBUF_GOTO); |
Bram Moolenaar | 349b2f6 | 2004-10-11 10:00:50 +0000 | [diff] [blame] | 2164 | cp = (char *)args; |
| 2165 | off = strtol(cp, &cp, 10); |
| 2166 | len = strtol(cp, NULL, 10); |
| 2167 | args = (char_u *)cp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2168 | pos = off2pos(buf->bufp, off); |
| 2169 | doupdate = 1; |
| 2170 | if (!pos) |
| 2171 | nbdebug((" no such start pos in %s, %ld\n", cmd, off)); |
| 2172 | else |
| 2173 | { |
| 2174 | first = *pos; |
| 2175 | pos = off2pos(buf->bufp, off + len - 1); |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 2176 | if (pos != NULL && pos->col == 0) |
| 2177 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2178 | /* |
| 2179 | * In Java Swing the offset is a position between 2 |
| 2180 | * characters. If col == 0 then we really want the |
| 2181 | * previous line as the end. |
| 2182 | */ |
| 2183 | pos = off2pos(buf->bufp, off + len - 2); |
| 2184 | } |
| 2185 | if (!pos) |
| 2186 | nbdebug((" no such end pos in %s, %ld\n", |
| 2187 | cmd, off + len - 1)); |
| 2188 | else |
| 2189 | { |
| 2190 | long lnum; |
| 2191 | last = *pos; |
| 2192 | /* set highlight for region */ |
| 2193 | nbdebug((" %sGUARD %ld,%d to %ld,%d\n", (un) ? "UN" : "", |
| 2194 | first.lnum, first.col, |
| 2195 | last.lnum, last.col)); |
| 2196 | #ifdef FEAT_SIGNS |
| 2197 | for (lnum = first.lnum; lnum <= last.lnum; lnum++) |
| 2198 | { |
| 2199 | if (un) |
| 2200 | { |
| 2201 | /* never used */ |
| 2202 | } |
| 2203 | else |
| 2204 | { |
| 2205 | if (buf_findsigntype_id(buf->bufp, lnum, |
| 2206 | GUARDED) == 0) |
| 2207 | { |
| 2208 | coloncmd( |
| 2209 | ":sign place %d line=%d name=%d buffer=%d", |
| 2210 | guardId++, lnum, GUARDED, |
| 2211 | buf->bufp->b_fnum); |
| 2212 | } |
| 2213 | } |
| 2214 | } |
| 2215 | #endif |
| 2216 | redraw_buf_later(buf->bufp, NOT_VALID); |
| 2217 | } |
| 2218 | } |
| 2219 | /* =====================================================================*/ |
| 2220 | } |
| 2221 | else if (streq((char *)cmd, "startAtomic")) |
| 2222 | { |
| 2223 | inAtomic = 1; |
| 2224 | /* =====================================================================*/ |
| 2225 | } |
| 2226 | else if (streq((char *)cmd, "endAtomic")) |
| 2227 | { |
| 2228 | inAtomic = 0; |
| 2229 | if (needupdate) |
| 2230 | { |
| 2231 | doupdate = 1; |
| 2232 | needupdate = 0; |
| 2233 | } |
| 2234 | /* =====================================================================*/ |
| 2235 | } |
| 2236 | else if (streq((char *)cmd, "save")) |
| 2237 | { |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 2238 | /* |
| 2239 | * NOTE - This command is obsolete wrt NetBeans. Its left in |
| 2240 | * only for historical reasons. |
| 2241 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2242 | if (buf == NULL || buf->bufp == NULL) |
| 2243 | { |
| 2244 | nbdebug((" null bufp in %s command", cmd)); |
| 2245 | return FAIL; |
| 2246 | } |
| 2247 | |
| 2248 | /* the following is taken from ex_cmds.c (do_wqall function) */ |
| 2249 | if (bufIsChanged(buf->bufp)) |
| 2250 | { |
| 2251 | /* Only write if the buffer can be written. */ |
| 2252 | if (p_write |
| 2253 | && !buf->bufp->b_p_ro |
| 2254 | && buf->bufp->b_ffname != NULL |
| 2255 | #ifdef FEAT_QUICKFIX |
| 2256 | && !bt_dontwrite(buf->bufp) |
| 2257 | #endif |
| 2258 | ) |
| 2259 | { |
| 2260 | buf_write_all(buf->bufp, FALSE); |
| 2261 | #ifdef FEAT_AUTOCMD |
| 2262 | /* an autocommand may have deleted the buffer */ |
| 2263 | if (!buf_valid(buf->bufp)) |
| 2264 | buf->bufp = NULL; |
| 2265 | #endif |
| 2266 | } |
| 2267 | } |
| 2268 | /* =====================================================================*/ |
| 2269 | } |
| 2270 | else if (streq((char *)cmd, "netbeansBuffer")) |
| 2271 | { |
| 2272 | if (buf == NULL || buf->bufp == NULL) |
| 2273 | { |
| 2274 | nbdebug((" null bufp in %s command", cmd)); |
| 2275 | return FAIL; |
| 2276 | } |
| 2277 | if (*args == 'T') |
| 2278 | { |
| 2279 | buf->bufp->b_netbeans_file = TRUE; |
| 2280 | buf->bufp->b_was_netbeans_file = TRUE; |
| 2281 | } |
| 2282 | else |
| 2283 | buf->bufp->b_netbeans_file = FALSE; |
| 2284 | /* =====================================================================*/ |
| 2285 | } |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 2286 | else if (streq((char *)cmd, "specialKeys")) |
| 2287 | { |
| 2288 | special_keys(args); |
| 2289 | /* =====================================================================*/ |
| 2290 | } |
| 2291 | else if (streq((char *)cmd, "actionMenuItem")) |
| 2292 | { |
| 2293 | /* not used yet */ |
| 2294 | /* =====================================================================*/ |
| 2295 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2296 | else if (streq((char *)cmd, "version")) |
| 2297 | { |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 2298 | /* not used yet */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2299 | } |
| 2300 | /* |
| 2301 | * Unrecognized command is ignored. |
| 2302 | */ |
| 2303 | } |
| 2304 | if (inAtomic && doupdate) |
| 2305 | { |
| 2306 | needupdate = 1; |
| 2307 | doupdate = 0; |
| 2308 | } |
| 2309 | |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 2310 | /* |
| 2311 | * Is this needed? I moved the netbeans_Xt_connect() later during startup |
| 2312 | * and it may no longer be necessary. If its not needed then needupdate |
| 2313 | * and doupdate can also be removed. |
| 2314 | */ |
| 2315 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2316 | if (buf != NULL && buf->initDone && doupdate) |
| 2317 | { |
| 2318 | update_screen(NOT_VALID); |
| 2319 | setcursor(); |
| 2320 | out_flush(); |
| 2321 | gui_update_cursor(TRUE, FALSE); |
| 2322 | gui_mch_flush(); |
| 2323 | /* Quit a hit-return or more prompt. */ |
| 2324 | if (State == HITRETURN || State == ASKMORE) |
| 2325 | { |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 2326 | /* add_to_input_buf((char_u *)"\003", 1);*/ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2327 | #ifdef FEAT_GUI_GTK |
| 2328 | if (gtk_main_level() > 0) |
| 2329 | gtk_main_quit(); |
| 2330 | #endif |
| 2331 | } |
| 2332 | } |
| 2333 | |
| 2334 | return retval; |
| 2335 | } |
| 2336 | |
| 2337 | |
| 2338 | /* |
| 2339 | * Process a vim colon command. |
| 2340 | */ |
| 2341 | static void |
| 2342 | coloncmd(char *cmd, ...) |
| 2343 | { |
| 2344 | char buf[1024]; |
| 2345 | va_list ap; |
| 2346 | |
| 2347 | va_start(ap, cmd); |
| 2348 | vsprintf(buf, cmd, ap); |
| 2349 | va_end(ap); |
| 2350 | |
| 2351 | nbdebug((" COLONCMD %s\n", buf)); |
| 2352 | |
| 2353 | /* ALT_INPUT_LOCK_ON; */ |
| 2354 | do_cmdline((char_u *)buf, NULL, NULL, DOCMD_NOWAIT | DOCMD_KEYTYPED); |
| 2355 | /* ALT_INPUT_LOCK_OFF; */ |
| 2356 | |
| 2357 | setcursor(); /* restore the cursor position */ |
| 2358 | out_flush(); /* make sure output has been written */ |
| 2359 | |
| 2360 | gui_update_cursor(TRUE, FALSE); |
| 2361 | gui_mch_flush(); |
| 2362 | } |
| 2363 | |
| 2364 | |
| 2365 | /* |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 2366 | * Parse the specialKeys argument and issue the appropriate map commands. |
| 2367 | */ |
| 2368 | static void |
| 2369 | special_keys(char_u *args) |
| 2370 | { |
| 2371 | char *save_str = nb_unquote(args, NULL); |
| 2372 | char *tok = strtok(save_str, " "); |
| 2373 | char *sep; |
| 2374 | char keybuf[64]; |
| 2375 | char cmdbuf[256]; |
| 2376 | |
| 2377 | while (tok != NULL) |
| 2378 | { |
| 2379 | int i = 0; |
| 2380 | |
| 2381 | if ((sep = strchr(tok, '-')) != NULL) |
| 2382 | { |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 2383 | *sep = NUL; |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 2384 | while (*tok) |
| 2385 | { |
| 2386 | switch (*tok) |
| 2387 | { |
| 2388 | case 'A': |
| 2389 | case 'M': |
| 2390 | case 'C': |
| 2391 | case 'S': |
| 2392 | keybuf[i++] = *tok; |
| 2393 | keybuf[i++] = '-'; |
| 2394 | break; |
| 2395 | } |
| 2396 | tok++; |
| 2397 | } |
| 2398 | tok++; |
| 2399 | } |
| 2400 | |
| 2401 | strcpy(&keybuf[i], tok); |
| 2402 | sprintf(cmdbuf, "<silent><%s> :nbkey %s<CR>", keybuf, keybuf); |
| 2403 | do_map(0, (char_u *)cmdbuf, NORMAL, FALSE); |
| 2404 | tok = strtok(NULL, " "); |
| 2405 | } |
| 2406 | vim_free(save_str); |
| 2407 | } |
| 2408 | |
| 2409 | |
| 2410 | void |
| 2411 | ex_nbkey(eap) |
| 2412 | exarg_T *eap; |
| 2413 | { |
| 2414 | netbeans_keystring(0, (char *)eap->arg); |
| 2415 | } |
| 2416 | |
| 2417 | |
| 2418 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2419 | * Initialize highlights and signs for use by netbeans (mostly obsolete) |
| 2420 | */ |
| 2421 | static void |
| 2422 | nb_init_graphics(void) |
| 2423 | { |
| 2424 | static int did_init = FALSE; |
| 2425 | |
| 2426 | if (!did_init) |
| 2427 | { |
| 2428 | coloncmd(":highlight NBGuarded guibg=Cyan guifg=Black"); |
| 2429 | coloncmd(":sign define %d linehl=NBGuarded", GUARDED); |
| 2430 | |
| 2431 | did_init = TRUE; |
| 2432 | } |
| 2433 | } |
| 2434 | |
| 2435 | /* |
| 2436 | * Convert key to netbeans name. |
| 2437 | */ |
| 2438 | static void |
| 2439 | netbeans_keyname(int key, char *buf) |
| 2440 | { |
| 2441 | char *name = 0; |
| 2442 | char namebuf[2]; |
| 2443 | int ctrl = 0; |
| 2444 | int shift = 0; |
| 2445 | int alt = 0; |
| 2446 | |
| 2447 | if (mod_mask & MOD_MASK_CTRL) |
| 2448 | ctrl = 1; |
| 2449 | if (mod_mask & MOD_MASK_SHIFT) |
| 2450 | shift = 1; |
| 2451 | if (mod_mask & MOD_MASK_ALT) |
| 2452 | alt = 1; |
| 2453 | |
| 2454 | |
| 2455 | switch (key) |
| 2456 | { |
| 2457 | case K_F1: name = "F1"; break; |
| 2458 | case K_S_F1: name = "F1"; shift = 1; break; |
| 2459 | case K_F2: name = "F2"; break; |
| 2460 | case K_S_F2: name = "F2"; shift = 1; break; |
| 2461 | case K_F3: name = "F3"; break; |
| 2462 | case K_S_F3: name = "F3"; shift = 1; break; |
| 2463 | case K_F4: name = "F4"; break; |
| 2464 | case K_S_F4: name = "F4"; shift = 1; break; |
| 2465 | case K_F5: name = "F5"; break; |
| 2466 | case K_S_F5: name = "F5"; shift = 1; break; |
| 2467 | case K_F6: name = "F6"; break; |
| 2468 | case K_S_F6: name = "F6"; shift = 1; break; |
| 2469 | case K_F7: name = "F7"; break; |
| 2470 | case K_S_F7: name = "F7"; shift = 1; break; |
| 2471 | case K_F8: name = "F8"; break; |
| 2472 | case K_S_F8: name = "F8"; shift = 1; break; |
| 2473 | case K_F9: name = "F9"; break; |
| 2474 | case K_S_F9: name = "F9"; shift = 1; break; |
| 2475 | case K_F10: name = "F10"; break; |
| 2476 | case K_S_F10: name = "F10"; shift = 1; break; |
| 2477 | case K_F11: name = "F11"; break; |
| 2478 | case K_S_F11: name = "F11"; shift = 1; break; |
| 2479 | case K_F12: name = "F12"; break; |
| 2480 | case K_S_F12: name = "F12"; shift = 1; break; |
| 2481 | default: |
| 2482 | if (key >= ' ' && key <= '~') |
| 2483 | { |
| 2484 | /* Allow ASCII characters. */ |
| 2485 | name = namebuf; |
| 2486 | namebuf[0] = key; |
| 2487 | namebuf[1] = NUL; |
| 2488 | } |
| 2489 | else |
| 2490 | name = "X"; |
| 2491 | break; |
| 2492 | } |
| 2493 | |
| 2494 | buf[0] = '\0'; |
| 2495 | if (ctrl) |
| 2496 | strcat(buf, "C"); |
| 2497 | if (shift) |
| 2498 | strcat(buf, "S"); |
| 2499 | if (alt) |
| 2500 | strcat(buf, "M"); /* META */ |
| 2501 | if (ctrl || shift || alt) |
| 2502 | strcat(buf, "-"); |
| 2503 | strcat(buf, name); |
| 2504 | } |
| 2505 | |
| 2506 | #ifdef FEAT_BEVAL |
| 2507 | /* |
| 2508 | * Function to be called for balloon evaluation. Grabs the text under the |
| 2509 | * cursor and sends it to the debugger for evaluation. The debugger should |
| 2510 | * respond with a showBalloon command when there is a useful result. |
| 2511 | */ |
| 2512 | /*ARGSUSED*/ |
| 2513 | static void |
| 2514 | netbeans_beval_cb( |
| 2515 | BalloonEval *beval, |
| 2516 | int state) |
| 2517 | { |
| 2518 | char_u *filename; |
| 2519 | char_u *text; |
| 2520 | int line; |
| 2521 | int col; |
| 2522 | char buf[MAXPATHL * 2 + 25]; |
| 2523 | char_u *p; |
| 2524 | |
| 2525 | /* Don't do anything when 'ballooneval' is off, messages scrolled the |
| 2526 | * windows up or we have no connection. */ |
| 2527 | if (!p_beval || msg_scrolled > 0 || !haveConnection) |
| 2528 | return; |
| 2529 | |
| 2530 | if (gui_mch_get_beval_info(beval, &filename, &line, &text, &col) == OK) |
| 2531 | { |
| 2532 | /* Send debugger request. Only when the text is of reasonable |
| 2533 | * length. */ |
| 2534 | if (text != NULL && text[0] != NUL && STRLEN(text) < MAXPATHL) |
| 2535 | { |
| 2536 | p = nb_quote(text); |
| 2537 | if (p != NULL) |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 2538 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2539 | sprintf(buf, "0:balloonText=%d \"%s\"\n", cmdno, p); |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 2540 | vim_free(p); |
| 2541 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2542 | nbdebug(("EVT: %s", buf)); |
| 2543 | nb_send(buf, "netbeans_beval_cb"); |
| 2544 | } |
| 2545 | vim_free(text); |
| 2546 | } |
| 2547 | } |
| 2548 | #endif |
| 2549 | |
| 2550 | /* |
| 2551 | * Tell netbeans that the window was opened, ready for commands. |
| 2552 | */ |
| 2553 | void |
| 2554 | netbeans_startup_done(void) |
| 2555 | { |
| 2556 | char *cmd = "0:startupDone=0\n"; |
| 2557 | |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 2558 | if (usingNetbeans) |
| 2559 | #ifdef FEAT_GUI_MOTIF |
| 2560 | netbeans_Xt_connect(app_context); |
| 2561 | #else |
| 2562 | # ifdef FEAT_GUI_GTK |
| 2563 | netbeans_gtk_connect(); |
| 2564 | # endif |
| 2565 | #endif |
| 2566 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2567 | if (!haveConnection) |
| 2568 | return; |
| 2569 | |
| 2570 | nbdebug(("EVT: %s", cmd)); |
| 2571 | nb_send(cmd, "netbeans_startup_done"); |
| 2572 | |
| 2573 | #ifdef FEAT_BEVAL |
| 2574 | # ifdef FEAT_GUI_MOTIF |
| 2575 | if (gui.in_use) |
| 2576 | { |
| 2577 | /* |
| 2578 | * Set up the Balloon Expression Evaluation area for Motif. |
| 2579 | * GTK can do it earlier... |
| 2580 | * Always create it but disable it when 'ballooneval' isn't set. |
| 2581 | */ |
| 2582 | balloonEval = gui_mch_create_beval_area(textArea, NULL, |
| 2583 | &netbeans_beval_cb, NULL); |
| 2584 | if (!p_beval) |
| 2585 | gui_mch_disable_beval_area(balloonEval); |
| 2586 | } |
| 2587 | # else |
| 2588 | # if defined(FEAT_GUI_W32) && defined(FEAT_BEVAL) |
| 2589 | balloonEval = gui_mch_create_beval_area(NULL, NULL, |
| 2590 | &netbeans_beval_cb, NULL); |
| 2591 | if (!p_beval) |
| 2592 | gui_mch_disable_beval_area(balloonEval); |
| 2593 | # endif |
| 2594 | # endif |
| 2595 | #endif |
| 2596 | } |
| 2597 | |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 2598 | /* |
| 2599 | * Tell netbeans that we're exiting. This should be called right |
| 2600 | * before calling exit. |
| 2601 | */ |
| 2602 | void |
| 2603 | netbeans_send_disconnect() |
| 2604 | { |
| 2605 | char buf[128]; |
| 2606 | |
| 2607 | if (haveConnection) |
| 2608 | { |
| 2609 | sprintf(buf, "0:disconnect=%d\n", cmdno); |
| 2610 | nbdebug(("EVT: %s", buf)); |
| 2611 | nb_send(buf, "netbeans_disconnect"); |
| 2612 | } |
| 2613 | } |
| 2614 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2615 | #if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_W32) || defined(PROTO) |
| 2616 | /* |
| 2617 | * Tell netbeans that the window was moved or resized. |
| 2618 | */ |
| 2619 | void |
| 2620 | netbeans_frame_moved(int new_x, int new_y) |
| 2621 | { |
| 2622 | char buf[128]; |
| 2623 | |
| 2624 | if (!haveConnection) |
| 2625 | return; |
| 2626 | |
| 2627 | sprintf(buf, "0:geometry=%d %d %d %d %d\n", |
| 2628 | cmdno, (int)Columns, (int)Rows, new_x, new_y); |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 2629 | /*nbdebug(("EVT: %s", buf)); happens too many times during a move */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2630 | nb_send(buf, "netbeans_frame_moved"); |
| 2631 | } |
| 2632 | #endif |
| 2633 | |
| 2634 | /* |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 2635 | * Tell netbeans the user opened or activated a file. |
| 2636 | */ |
| 2637 | void |
| 2638 | netbeans_file_activated(buf_T *bufp) |
| 2639 | { |
| 2640 | int bufno = nb_getbufno(bufp); |
| 2641 | nbbuf_T *bp = nb_get_buf(bufno); |
| 2642 | char buffer[2*MAXPATHL]; |
| 2643 | char_u *q; |
| 2644 | |
| 2645 | if (!haveConnection || dosetvisible) |
| 2646 | return; |
| 2647 | |
| 2648 | q = nb_quote(bufp->b_ffname); |
| 2649 | if (q == NULL || bp == NULL || bufp == NULL) |
| 2650 | return; |
| 2651 | |
| 2652 | sprintf(buffer, "%d:fileOpened=%d \"%s\" %s %s\n", |
| 2653 | bufno, |
| 2654 | bufno, |
| 2655 | (char *)q, |
| 2656 | "T", /* open in NetBeans */ |
| 2657 | "F"); /* modified */ |
| 2658 | |
| 2659 | vim_free(q); |
| 2660 | nbdebug(("EVT: %s", buffer)); |
| 2661 | |
| 2662 | nb_send(buffer, "netbeans_file_opened"); |
| 2663 | } |
| 2664 | |
| 2665 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2666 | * Tell netbeans the user opened a file. |
| 2667 | */ |
| 2668 | void |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 2669 | netbeans_file_opened(buf_T *bufp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2670 | { |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 2671 | int bufno = nb_getbufno(bufp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2672 | char buffer[2*MAXPATHL]; |
| 2673 | char_u *q; |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 2674 | nbbuf_T *bp = nb_get_buf(nb_getbufno(bufp)); |
| 2675 | int bnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2676 | |
| 2677 | if (!haveConnection) |
| 2678 | return; |
| 2679 | |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 2680 | q = nb_quote(bufp->b_ffname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2681 | if (q == NULL) |
| 2682 | return; |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 2683 | if (bp != NULL) |
| 2684 | bnum = bufno; |
| 2685 | else |
| 2686 | bnum = 0; |
| 2687 | |
| 2688 | sprintf(buffer, "%d:fileOpened=%d \"%s\" %s %s\n", |
| 2689 | bnum, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2690 | 0, |
| 2691 | (char *)q, |
| 2692 | "T", /* open in NetBeans */ |
| 2693 | "F"); /* modified */ |
| 2694 | |
| 2695 | vim_free(q); |
| 2696 | nbdebug(("EVT: %s", buffer)); |
| 2697 | |
| 2698 | nb_send(buffer, "netbeans_file_opened"); |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 2699 | if (p_acd && vim_chdirfile(bufp->b_ffname) == OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2700 | shorten_fnames(TRUE); |
| 2701 | } |
| 2702 | |
| 2703 | /* |
| 2704 | * Tell netbeans a file was closed. |
| 2705 | */ |
| 2706 | void |
| 2707 | netbeans_file_closed(buf_T *bufp) |
| 2708 | { |
| 2709 | int bufno = nb_getbufno(bufp); |
| 2710 | nbbuf_T *nbbuf = nb_get_buf(bufno); |
| 2711 | char buffer[2*MAXPATHL]; |
| 2712 | |
| 2713 | if (!haveConnection || bufno < 0) |
| 2714 | return; |
| 2715 | |
| 2716 | if (!netbeansCloseFile) |
| 2717 | { |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 2718 | nbdebug(("Ignoring file_closed for %s. File was closed from IDE\n", |
| 2719 | bufp->b_ffname)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2720 | return; |
| 2721 | } |
| 2722 | |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 2723 | nbdebug(("netbeans_file_closed:\n")); |
| 2724 | nbdebug((" Closing bufno: %d", bufno)); |
| 2725 | if (curbuf != NULL && curbuf != bufp) |
| 2726 | { |
| 2727 | nbdebug((" Curbuf bufno: %d\n", nb_getbufno(curbuf))); |
| 2728 | } |
| 2729 | else if (curbuf == bufp) |
| 2730 | { |
| 2731 | nbdebug((" curbuf == bufp\n")); |
| 2732 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2733 | |
| 2734 | if (bufno <= 0) |
| 2735 | return; |
| 2736 | |
| 2737 | sprintf(buffer, "%d:killed=%d\n", bufno, cmdno); |
| 2738 | |
| 2739 | nbdebug(("EVT: %s", buffer)); |
| 2740 | |
| 2741 | nb_send(buffer, "netbeans_file_closed"); |
| 2742 | |
| 2743 | if (nbbuf != NULL) |
| 2744 | nbbuf->bufp = NULL; |
| 2745 | } |
| 2746 | |
| 2747 | /* |
| 2748 | * Get a pointer to the Netbeans buffer for Vim buffer "bufp". |
| 2749 | * Return NULL if there is no such buffer or changes are not to be reported. |
| 2750 | * Otherwise store the buffer number in "*bufnop". |
| 2751 | */ |
| 2752 | static nbbuf_T * |
| 2753 | nb_bufp2nbbuf_fire(buf_T *bufp, int *bufnop) |
| 2754 | { |
| 2755 | int bufno; |
| 2756 | nbbuf_T *nbbuf; |
| 2757 | |
| 2758 | if (!haveConnection || !netbeansFireChanges) |
| 2759 | return NULL; /* changes are not reported at all */ |
| 2760 | |
| 2761 | bufno = nb_getbufno(bufp); |
| 2762 | if (bufno <= 0) |
| 2763 | return NULL; /* file is not known to NetBeans */ |
| 2764 | |
| 2765 | nbbuf = nb_get_buf(bufno); |
| 2766 | if (nbbuf != NULL && !nbbuf->fireChanges) |
| 2767 | return NULL; /* changes in this buffer are not reported */ |
| 2768 | |
| 2769 | *bufnop = bufno; |
| 2770 | return nbbuf; |
| 2771 | } |
| 2772 | |
| 2773 | /* |
| 2774 | * Tell netbeans the user inserted some text. |
| 2775 | */ |
| 2776 | void |
| 2777 | netbeans_inserted( |
| 2778 | buf_T *bufp, |
| 2779 | linenr_T linenr, |
| 2780 | colnr_T col, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2781 | char_u *txt, |
| 2782 | int newlen) |
| 2783 | { |
| 2784 | char_u *buf; |
| 2785 | int bufno; |
| 2786 | nbbuf_T *nbbuf; |
| 2787 | pos_T pos; |
| 2788 | long off; |
| 2789 | char_u *p; |
| 2790 | char_u *newtxt; |
| 2791 | |
| 2792 | nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno); |
| 2793 | if (nbbuf == NULL) |
| 2794 | return; |
| 2795 | |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 2796 | /* Don't mark as modified for initial read */ |
| 2797 | if (nbbuf->insertDone) |
| 2798 | nbbuf->modified = 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2799 | |
| 2800 | pos.lnum = linenr; |
| 2801 | pos.col = col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2802 | off = pos2off(bufp, &pos); |
| 2803 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2804 | /* send the "insert" EVT */ |
| 2805 | newtxt = alloc(newlen + 1); |
| 2806 | STRNCPY(newtxt, txt, newlen); |
| 2807 | newtxt[newlen] = '\0'; |
| 2808 | p = nb_quote(newtxt); |
| 2809 | if (p != NULL) |
| 2810 | { |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 2811 | buf = alloc(128 + 2*newlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2812 | sprintf((char *)buf, "%d:insert=%d %ld \"%s\"\n", bufno, cmdno, off, p); |
| 2813 | nbdebug(("EVT: %s", buf)); |
| 2814 | nb_send((char *)buf, "netbeans_inserted"); |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 2815 | vim_free(p); |
| 2816 | vim_free(buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2817 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2818 | vim_free(newtxt); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2819 | } |
| 2820 | |
| 2821 | /* |
| 2822 | * Tell netbeans some bytes have been removed. |
| 2823 | */ |
| 2824 | void |
| 2825 | netbeans_removed( |
| 2826 | buf_T *bufp, |
| 2827 | linenr_T linenr, |
| 2828 | colnr_T col, |
| 2829 | long len) |
| 2830 | { |
| 2831 | char_u buf[128]; |
| 2832 | int bufno; |
| 2833 | nbbuf_T *nbbuf; |
| 2834 | pos_T pos; |
| 2835 | long off; |
| 2836 | |
| 2837 | nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno); |
| 2838 | if (nbbuf == NULL) |
| 2839 | return; |
| 2840 | |
| 2841 | if (len < 0) |
| 2842 | { |
| 2843 | nbdebug(("Negative len %ld in netbeans_removed()!", len)); |
| 2844 | return; |
| 2845 | } |
| 2846 | |
| 2847 | nbbuf->modified = 1; |
| 2848 | |
| 2849 | pos.lnum = linenr; |
| 2850 | pos.col = col; |
| 2851 | |
| 2852 | off = pos2off(bufp, &pos); |
| 2853 | |
| 2854 | sprintf((char *)buf, "%d:remove=%d %ld %ld\n", bufno, cmdno, off, len); |
| 2855 | nbdebug(("EVT: %s", buf)); |
| 2856 | nb_send((char *)buf, "netbeans_removed"); |
| 2857 | } |
| 2858 | |
| 2859 | /* |
| 2860 | * Send netbeans an unmodufied command. |
| 2861 | */ |
| 2862 | /*ARGSUSED*/ |
| 2863 | void |
| 2864 | netbeans_unmodified(buf_T *bufp) |
| 2865 | { |
| 2866 | #if 0 |
| 2867 | char_u buf[128]; |
| 2868 | int bufno; |
| 2869 | nbbuf_T *nbbuf; |
| 2870 | |
| 2871 | /* This has been disabled, because NetBeans considers a buffer modified |
| 2872 | * even when all changes have been undone. */ |
| 2873 | nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno); |
| 2874 | if (nbbuf == NULL) |
| 2875 | return; |
| 2876 | |
| 2877 | nbbuf->modified = 0; |
| 2878 | |
| 2879 | sprintf((char *)buf, "%d:unmodified=%d\n", bufno, cmdno); |
| 2880 | nbdebug(("EVT: %s", buf)); |
| 2881 | nb_send((char *)buf, "netbeans_unmodified"); |
| 2882 | #endif |
| 2883 | } |
| 2884 | |
| 2885 | /* |
| 2886 | * Send a button release event back to netbeans. Its up to netbeans |
| 2887 | * to decide what to do (if anything) with this event. |
| 2888 | */ |
| 2889 | void |
| 2890 | netbeans_button_release(int button) |
| 2891 | { |
| 2892 | char buf[128]; |
| 2893 | int bufno; |
| 2894 | |
| 2895 | bufno = nb_getbufno(curbuf); |
| 2896 | |
| 2897 | if (bufno >= 0 && curwin != NULL && curwin->w_buffer == curbuf) |
| 2898 | { |
| 2899 | int col = mouse_col - curwin->w_wincol - (curwin->w_p_nu ? 9 : 1); |
| 2900 | long off = pos2off(curbuf, &curwin->w_cursor); |
| 2901 | |
| 2902 | /* sync the cursor position */ |
| 2903 | sprintf(buf, "%d:newDotAndMark=%d %ld %ld\n", bufno, cmdno, off, off); |
| 2904 | nbdebug(("EVT: %s", buf)); |
| 2905 | nb_send(buf, "netbeans_button_release[newDotAndMark]"); |
| 2906 | |
| 2907 | sprintf(buf, "%d:buttonRelease=%d %d %ld %d\n", bufno, cmdno, |
| 2908 | button, (long)curwin->w_cursor.lnum, col); |
| 2909 | nbdebug(("EVT: %s", buf)); |
| 2910 | nb_send(buf, "netbeans_button_release"); |
| 2911 | } |
| 2912 | } |
| 2913 | |
| 2914 | |
| 2915 | /* |
| 2916 | * Send a keypress event back to netbeans. This usualy simulates some |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 2917 | * kind of function key press. This function operates on a key code. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2918 | */ |
| 2919 | void |
| 2920 | netbeans_keycommand(int key) |
| 2921 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2922 | char keyName[60]; |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 2923 | |
| 2924 | netbeans_keyname(key, keyName); |
| 2925 | netbeans_keystring(key, keyName); |
| 2926 | } |
| 2927 | |
| 2928 | |
| 2929 | /* |
| 2930 | * Send a keypress event back to netbeans. This usualy simulates some |
| 2931 | * kind of function key press. This function operates on a key string. |
| 2932 | */ |
| 2933 | static void |
| 2934 | netbeans_keystring(int key, char *keyName) |
| 2935 | { |
| 2936 | char buf[2*MAXPATHL]; |
| 2937 | int bufno = nb_getbufno(curbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2938 | long off; |
| 2939 | char_u *q; |
| 2940 | |
| 2941 | if (!haveConnection) |
| 2942 | return; |
| 2943 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2944 | |
| 2945 | if (bufno == -1) |
| 2946 | { |
| 2947 | nbdebug(("got keycommand for non-NetBeans buffer, opening...\n")); |
| 2948 | q = curbuf->b_ffname == NULL ? (char_u *)"" |
| 2949 | : nb_quote(curbuf->b_ffname); |
| 2950 | if (q == NULL) |
| 2951 | return; |
| 2952 | sprintf(buf, "0:fileOpened=%d \"%s\" %s %s\n", 0, |
| 2953 | q, |
| 2954 | "T", /* open in NetBeans */ |
| 2955 | "F"); /* modified */ |
| 2956 | if (curbuf->b_ffname != NULL) |
| 2957 | vim_free(q); |
| 2958 | nbdebug(("EVT: %s", buf)); |
| 2959 | nb_send(buf, "netbeans_keycommand"); |
| 2960 | |
| 2961 | postpone_keycommand(key); |
| 2962 | return; |
| 2963 | } |
| 2964 | |
| 2965 | /* sync the cursor position */ |
| 2966 | off = pos2off(curbuf, &curwin->w_cursor); |
| 2967 | sprintf(buf, "%d:newDotAndMark=%d %ld %ld\n", bufno, cmdno, off, off); |
| 2968 | nbdebug(("EVT: %s", buf)); |
| 2969 | nb_send(buf, "netbeans_keycommand"); |
| 2970 | |
| 2971 | /* To work on Win32 you must apply patch to ExtEditor module |
| 2972 | * from ExtEdCaret.java.diff - make EVT_newDotAndMark handler |
| 2973 | * more synchronous |
| 2974 | */ |
| 2975 | |
| 2976 | /* now send keyCommand event */ |
| 2977 | sprintf(buf, "%d:keyCommand=%d \"%s\"\n", bufno, cmdno, keyName); |
| 2978 | nbdebug(("EVT: %s", buf)); |
| 2979 | nb_send(buf, "netbeans_keycommand"); |
| 2980 | |
| 2981 | /* New: do both at once and include the lnum/col. */ |
| 2982 | sprintf(buf, "%d:keyAtPos=%d \"%s\" %ld %ld/%ld\n", bufno, cmdno, keyName, |
| 2983 | off, (long)curwin->w_cursor.lnum, (long)curwin->w_cursor.col); |
| 2984 | nbdebug(("EVT: %s", buf)); |
| 2985 | nb_send(buf, "netbeans_keycommand"); |
| 2986 | } |
| 2987 | |
| 2988 | |
| 2989 | /* |
| 2990 | * Send a save event to netbeans. |
| 2991 | */ |
| 2992 | void |
| 2993 | netbeans_save_buffer(buf_T *bufp) |
| 2994 | { |
| 2995 | char_u buf[64]; |
| 2996 | int bufno; |
| 2997 | nbbuf_T *nbbuf; |
| 2998 | |
| 2999 | nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno); |
| 3000 | if (nbbuf == NULL) |
| 3001 | return; |
| 3002 | |
| 3003 | nbbuf->modified = 0; |
| 3004 | |
| 3005 | sprintf((char *)buf, "%d:save=%d\n", bufno, cmdno); |
| 3006 | nbdebug(("EVT: %s", buf)); |
| 3007 | nb_send((char *)buf, "netbeans_save_buffer"); |
| 3008 | } |
| 3009 | |
| 3010 | |
| 3011 | /* |
| 3012 | * Send remove command to netbeans (this command has been turned off). |
| 3013 | */ |
| 3014 | void |
| 3015 | netbeans_deleted_all_lines(buf_T *bufp) |
| 3016 | { |
| 3017 | char_u buf[64]; |
| 3018 | int bufno; |
| 3019 | nbbuf_T *nbbuf; |
| 3020 | |
| 3021 | nbbuf = nb_bufp2nbbuf_fire(bufp, &bufno); |
| 3022 | if (nbbuf == NULL) |
| 3023 | return; |
| 3024 | |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 3025 | /* Don't mark as modified for initial read */ |
| 3026 | if (nbbuf->insertDone) |
| 3027 | nbbuf->modified = 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3028 | |
| 3029 | sprintf((char *)buf, "%d:remove=%d 0 -1\n", bufno, cmdno); |
| 3030 | nbdebug(("EVT(suppressed): %s", buf)); |
| 3031 | /* nb_send(buf, "netbeans_deleted_all_lines"); */ |
| 3032 | } |
| 3033 | |
| 3034 | |
| 3035 | /* |
| 3036 | * See if the lines are guarded. The top and bot parameters are from |
| 3037 | * u_savecommon(), these are the line above the change and the line below the |
| 3038 | * change. |
| 3039 | */ |
| 3040 | int |
| 3041 | netbeans_is_guarded(linenr_T top, linenr_T bot) |
| 3042 | { |
| 3043 | signlist_T *p; |
| 3044 | int lnum; |
| 3045 | |
| 3046 | for (p = curbuf->b_signlist; p != NULL; p = p->next) |
| 3047 | if (p->id >= GUARDEDOFFSET) |
| 3048 | for (lnum = top + 1; lnum < bot; lnum++) |
| 3049 | if (lnum == p->lnum) |
| 3050 | return TRUE; |
| 3051 | |
| 3052 | return FALSE; |
| 3053 | } |
| 3054 | |
| 3055 | #if defined(FEAT_GUI_MOTIF) || defined(PROTO) |
| 3056 | /* |
| 3057 | * We have multiple signs to draw at the same location. Draw the |
| 3058 | * multi-sign indicator instead. This is the Motif version. |
| 3059 | */ |
| 3060 | void |
| 3061 | netbeans_draw_multisign_indicator(int row) |
| 3062 | { |
| 3063 | int i; |
| 3064 | int y; |
| 3065 | int x; |
| 3066 | |
| 3067 | x = 0; |
| 3068 | y = row * gui.char_height + 2; |
| 3069 | |
| 3070 | for (i = 0; i < gui.char_height - 3; i++) |
| 3071 | XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y++); |
| 3072 | |
| 3073 | XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+0, y); |
| 3074 | XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y); |
| 3075 | XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+4, y++); |
| 3076 | XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+1, y); |
| 3077 | XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y); |
| 3078 | XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+3, y++); |
| 3079 | XDrawPoint(gui.dpy, gui.wid, gui.text_gc, x+2, y); |
| 3080 | } |
| 3081 | #endif /* FEAT_GUI_MOTIF */ |
| 3082 | |
| 3083 | #ifdef FEAT_GUI_GTK |
| 3084 | /* |
| 3085 | * We have multiple signs to draw at the same location. Draw the |
| 3086 | * multi-sign indicator instead. This is the GTK/Gnome version. |
| 3087 | */ |
| 3088 | void |
| 3089 | netbeans_draw_multisign_indicator(int row) |
| 3090 | { |
| 3091 | int i; |
| 3092 | int y; |
| 3093 | int x; |
| 3094 | GdkDrawable *drawable = gui.drawarea->window; |
| 3095 | |
| 3096 | x = 0; |
| 3097 | y = row * gui.char_height + 2; |
| 3098 | |
| 3099 | for (i = 0; i < gui.char_height - 3; i++) |
| 3100 | gdk_draw_point(drawable, gui.text_gc, x+2, y++); |
| 3101 | |
| 3102 | gdk_draw_point(drawable, gui.text_gc, x+0, y); |
| 3103 | gdk_draw_point(drawable, gui.text_gc, x+2, y); |
| 3104 | gdk_draw_point(drawable, gui.text_gc, x+4, y++); |
| 3105 | gdk_draw_point(drawable, gui.text_gc, x+1, y); |
| 3106 | gdk_draw_point(drawable, gui.text_gc, x+2, y); |
| 3107 | gdk_draw_point(drawable, gui.text_gc, x+3, y++); |
| 3108 | gdk_draw_point(drawable, gui.text_gc, x+2, y); |
| 3109 | } |
| 3110 | #endif /* FEAT_GUI_GTK */ |
| 3111 | |
| 3112 | /* |
| 3113 | * If the mouse is clicked in the gutter of a line with multiple |
| 3114 | * annotations, cycle through the set of signs. |
| 3115 | */ |
| 3116 | void |
| 3117 | netbeans_gutter_click(linenr_T lnum) |
| 3118 | { |
| 3119 | signlist_T *p; |
| 3120 | |
| 3121 | for (p = curbuf->b_signlist; p != NULL; p = p->next) |
| 3122 | { |
| 3123 | if (p->lnum == lnum && p->next && p->next->lnum == lnum) |
| 3124 | { |
| 3125 | signlist_T *tail; |
| 3126 | |
| 3127 | /* remove "p" from list, reinsert it at the tail of the sublist */ |
| 3128 | if (p->prev) |
| 3129 | p->prev->next = p->next; |
| 3130 | else |
| 3131 | curbuf->b_signlist = p->next; |
| 3132 | p->next->prev = p->prev; |
| 3133 | /* now find end of sublist and insert p */ |
| 3134 | for (tail = p->next; |
| 3135 | tail->next && tail->next->lnum == lnum |
| 3136 | && tail->next->id < GUARDEDOFFSET; |
| 3137 | tail = tail->next) |
| 3138 | ; |
| 3139 | /* tail now points to last entry with same lnum (except |
| 3140 | * that "guarded" annotations are always last) */ |
| 3141 | p->next = tail->next; |
| 3142 | if (tail->next) |
| 3143 | tail->next->prev = p; |
| 3144 | p->prev = tail; |
| 3145 | tail->next = p; |
| 3146 | update_debug_sign(curbuf, lnum); |
| 3147 | break; |
| 3148 | } |
| 3149 | } |
| 3150 | } |
| 3151 | |
| 3152 | |
| 3153 | /* |
| 3154 | * Add a sign of the reqested type at the requested location. |
| 3155 | * |
| 3156 | * Reverse engineering: |
| 3157 | * Apparently an annotation is defined the first time it is used in a buffer. |
| 3158 | * When the same annotation is used in two buffers, the second time we do not |
| 3159 | * need to define a new sign name but reuse the existing one. But since the |
| 3160 | * ID number used in the second buffer starts counting at one again, a mapping |
| 3161 | * is made from the ID specifically for the buffer to the global sign name |
| 3162 | * (which is a number). |
| 3163 | * |
| 3164 | * globalsignmap[] stores the signs that have been defined globally. |
| 3165 | * buf->signmapused[] maps buffer-local annotation IDs to an index in |
| 3166 | * globalsignmap[]. |
| 3167 | */ |
| 3168 | /*ARGSUSED*/ |
| 3169 | static void |
| 3170 | addsigntype( |
| 3171 | nbbuf_T *buf, |
| 3172 | int typeNum, |
| 3173 | char_u *typeName, |
| 3174 | char_u *tooltip, |
| 3175 | char_u *glyphFile, |
| 3176 | int use_fg, |
| 3177 | int fg, |
| 3178 | int use_bg, |
| 3179 | int bg) |
| 3180 | { |
| 3181 | char fgbuf[32]; |
| 3182 | char bgbuf[32]; |
| 3183 | int i, j; |
| 3184 | |
| 3185 | for (i = 0; i < globalsignmapused; i++) |
| 3186 | if (STRCMP(typeName, globalsignmap[i]) == 0) |
| 3187 | break; |
| 3188 | |
| 3189 | if (i == globalsignmapused) /* not found; add it to global map */ |
| 3190 | { |
| 3191 | nbdebug(("DEFINEANNOTYPE(%d,%s,%s,%s,%d,%d)\n", |
| 3192 | typeNum, typeName, tooltip, glyphFile, fg, bg)); |
| 3193 | if (use_fg || use_bg) |
| 3194 | { |
| 3195 | sprintf(fgbuf, "guifg=#%06x", fg & 0xFFFFFF); |
| 3196 | sprintf(bgbuf, "guibg=#%06x", bg & 0xFFFFFF); |
| 3197 | |
| 3198 | coloncmd(":highlight NB_%s %s %s", typeName, (use_fg) ? fgbuf : "", |
| 3199 | (use_bg) ? bgbuf : ""); |
| 3200 | if (*glyphFile == NUL) |
| 3201 | /* no glyph, line highlighting only */ |
| 3202 | coloncmd(":sign define %d linehl=NB_%s", i + 1, typeName); |
| 3203 | else if (vim_strsize(glyphFile) <= 2) |
| 3204 | /* one- or two-character glyph name, use as text glyph with |
| 3205 | * texthl */ |
| 3206 | coloncmd(":sign define %d text=%s texthl=NB_%s", i + 1, |
| 3207 | glyphFile, typeName); |
| 3208 | else |
| 3209 | /* glyph, line highlighting */ |
| 3210 | coloncmd(":sign define %d icon=%s linehl=NB_%s", i + 1, |
| 3211 | glyphFile, typeName); |
| 3212 | } |
| 3213 | else |
| 3214 | /* glyph, no line highlighting */ |
| 3215 | coloncmd(":sign define %d icon=%s", i + 1, glyphFile); |
| 3216 | |
| 3217 | if (STRCMP(typeName,"CurrentPC") == 0) |
| 3218 | curPCtype = typeNum; |
| 3219 | |
| 3220 | if (globalsignmapused == globalsignmaplen) |
| 3221 | { |
| 3222 | if (globalsignmaplen == 0) /* first allocation */ |
| 3223 | { |
| 3224 | globalsignmaplen = 20; |
| 3225 | globalsignmap = (char **)alloc_clear(globalsignmaplen*sizeof(char *)); |
| 3226 | } |
| 3227 | else /* grow it */ |
| 3228 | { |
| 3229 | int incr; |
| 3230 | int oldlen = globalsignmaplen; |
| 3231 | |
| 3232 | globalsignmaplen *= 2; |
| 3233 | incr = globalsignmaplen - oldlen; |
| 3234 | globalsignmap = (char **)vim_realloc(globalsignmap, |
| 3235 | globalsignmaplen * sizeof(char *)); |
| 3236 | memset(globalsignmap + oldlen, 0, incr * sizeof(char *)); |
| 3237 | } |
| 3238 | } |
| 3239 | |
| 3240 | globalsignmap[i] = (char *)typeName; |
| 3241 | globalsignmapused = i + 1; |
| 3242 | } |
| 3243 | |
| 3244 | /* check local map; should *not* be found! */ |
| 3245 | for (j = 0; j < buf->signmapused; j++) |
| 3246 | if (buf->signmap[j] == i + 1) |
| 3247 | return; |
| 3248 | |
| 3249 | /* add to local map */ |
| 3250 | if (buf->signmapused == buf->signmaplen) |
| 3251 | { |
| 3252 | if (buf->signmaplen == 0) /* first allocation */ |
| 3253 | { |
| 3254 | buf->signmaplen = 5; |
| 3255 | buf->signmap = (int *)alloc_clear(buf->signmaplen * sizeof(int *)); |
| 3256 | } |
| 3257 | else /* grow it */ |
| 3258 | { |
| 3259 | int incr; |
| 3260 | int oldlen = buf->signmaplen; |
| 3261 | buf->signmaplen *= 2; |
| 3262 | incr = buf->signmaplen - oldlen; |
| 3263 | buf->signmap = (int *)vim_realloc(buf->signmap, |
| 3264 | buf->signmaplen*sizeof(int *)); |
| 3265 | memset(buf->signmap + oldlen, 0, incr * sizeof(int *)); |
| 3266 | } |
| 3267 | } |
| 3268 | |
| 3269 | buf->signmap[buf->signmapused++] = i + 1; |
| 3270 | |
| 3271 | } |
| 3272 | |
| 3273 | |
| 3274 | /* |
| 3275 | * See if we have the requested sign type in the buffer. |
| 3276 | */ |
| 3277 | static int |
| 3278 | mapsigntype(nbbuf_T *buf, int localsigntype) |
| 3279 | { |
| 3280 | if (--localsigntype >= 0 && localsigntype < buf->signmapused) |
| 3281 | return buf->signmap[localsigntype]; |
| 3282 | |
| 3283 | return 0; |
| 3284 | } |
| 3285 | |
| 3286 | |
| 3287 | /* |
| 3288 | * Compute length of buffer, don't print anything. |
| 3289 | */ |
| 3290 | static long |
| 3291 | get_buf_size(buf_T *bufp) |
| 3292 | { |
| 3293 | linenr_T lnum; |
| 3294 | long char_count = 0; |
| 3295 | int eol_size; |
| 3296 | long last_check = 100000L; |
| 3297 | |
| 3298 | if (bufp->b_ml.ml_flags & ML_EMPTY) |
| 3299 | return 0; |
| 3300 | else |
| 3301 | { |
| 3302 | if (get_fileformat(bufp) == EOL_DOS) |
| 3303 | eol_size = 2; |
| 3304 | else |
| 3305 | eol_size = 1; |
| 3306 | for (lnum = 1; lnum <= bufp->b_ml.ml_line_count; ++lnum) |
| 3307 | { |
| 3308 | char_count += STRLEN(ml_get(lnum)) + eol_size; |
| 3309 | /* Check for a CTRL-C every 100000 characters */ |
| 3310 | if (char_count > last_check) |
| 3311 | { |
| 3312 | ui_breakcheck(); |
| 3313 | if (got_int) |
| 3314 | return char_count; |
| 3315 | last_check = char_count + 100000L; |
| 3316 | } |
| 3317 | } |
| 3318 | /* Correction for when last line doesn't have an EOL. */ |
| 3319 | if (!bufp->b_p_eol && bufp->b_p_bin) |
| 3320 | char_count -= eol_size; |
| 3321 | } |
| 3322 | |
| 3323 | return char_count; |
| 3324 | } |
| 3325 | |
| 3326 | /* |
| 3327 | * Convert character offset to lnum,col |
| 3328 | */ |
| 3329 | static pos_T * |
| 3330 | off2pos(buf_T *buf, long offset) |
| 3331 | { |
| 3332 | linenr_T lnum; |
| 3333 | static pos_T pos; |
| 3334 | |
| 3335 | pos.lnum = 0; |
| 3336 | pos.col = 0; |
| 3337 | #ifdef FEAT_VIRTUALEDIT |
| 3338 | pos.coladd = 0; |
| 3339 | #endif |
| 3340 | |
| 3341 | if (!(buf->b_ml.ml_flags & ML_EMPTY)) |
| 3342 | { |
| 3343 | if ((lnum = ml_find_line_or_offset(buf, (linenr_T)0, &offset)) < 0) |
| 3344 | return NULL; |
| 3345 | pos.lnum = lnum; |
| 3346 | pos.col = offset; |
| 3347 | } |
| 3348 | |
| 3349 | return &pos; |
| 3350 | } |
| 3351 | |
| 3352 | /* |
| 3353 | * Convert an argument in the form "1234" to an offset and compute the |
| 3354 | * lnum/col from it. Convert an argument in the form "123/12" directly to a |
| 3355 | * lnum/col. |
| 3356 | * "argp" is advanced to after the argument. |
| 3357 | * Return a pointer to the position, NULL if something is wrong. |
| 3358 | */ |
| 3359 | static pos_T * |
| 3360 | get_off_or_lnum(buf_T *buf, char_u **argp) |
| 3361 | { |
| 3362 | static pos_T mypos; |
| 3363 | long off; |
| 3364 | |
| 3365 | off = strtol((char *)*argp, (char **)argp, 10); |
| 3366 | if (**argp == '/') |
| 3367 | { |
| 3368 | mypos.lnum = (linenr_T)off; |
| 3369 | ++*argp; |
| 3370 | mypos.col = strtol((char *)*argp, (char **)argp, 10); |
| 3371 | #ifdef FEAT_VIRTUALEDIT |
| 3372 | mypos.coladd = 0; |
| 3373 | #endif |
| 3374 | return &mypos; |
| 3375 | } |
| 3376 | return off2pos(buf, off); |
| 3377 | } |
| 3378 | |
| 3379 | |
| 3380 | /* |
| 3381 | * Convert lnum,col to character offset |
| 3382 | */ |
| 3383 | static long |
| 3384 | pos2off(buf_T *buf, pos_T *pos) |
| 3385 | { |
| 3386 | long offset = 0; |
| 3387 | |
| 3388 | if (!(buf->b_ml.ml_flags & ML_EMPTY)) |
| 3389 | { |
| 3390 | if ((offset = ml_find_line_or_offset(buf, pos->lnum, 0)) < 0) |
| 3391 | return 0; |
| 3392 | offset += pos->col; |
| 3393 | } |
| 3394 | |
| 3395 | return offset; |
| 3396 | } |
| 3397 | |
| 3398 | |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 3399 | /* |
| 3400 | * This message is printed after NetBeans opens a new file. Its |
| 3401 | * similar to the message readfile() uses, but since NetBeans |
| 3402 | * doesn't normally call readfile, we do our own. |
| 3403 | */ |
| 3404 | static void |
| 3405 | print_read_msg(buf) |
| 3406 | nbbuf_T *buf; |
| 3407 | { |
| 3408 | int lnum = buf->bufp->b_ml.ml_line_count; |
| 3409 | long nchars = buf->bufp->b_orig_size; |
| 3410 | char_u c; |
| 3411 | |
| 3412 | msg_add_fname(buf->bufp, buf->bufp->b_ffname); |
| 3413 | c = FALSE; |
| 3414 | |
| 3415 | if (buf->bufp->b_p_ro) |
| 3416 | { |
| 3417 | STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]")); |
| 3418 | c = TRUE; |
| 3419 | } |
| 3420 | if (!buf->bufp->b_start_eol) |
| 3421 | { |
| 3422 | STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]") : _("[Incomplete last line]")); |
| 3423 | c = TRUE; |
| 3424 | } |
| 3425 | msg_add_lines(c, (long)lnum, nchars); |
| 3426 | |
| 3427 | /* Now display it */ |
| 3428 | vim_free(keep_msg); |
| 3429 | keep_msg = NULL; |
| 3430 | msg_scrolled_ign = TRUE; |
| 3431 | msg_trunc_attr(IObuff, FALSE, 0); |
| 3432 | msg_scrolled_ign = FALSE; |
| 3433 | } |
| 3434 | |
| 3435 | |
| 3436 | /* |
| 3437 | * Print a message after NetBeans writes the file. This message should be identical |
| 3438 | * to the standard message a non-netbeans user would see when writing a file. |
| 3439 | */ |
| 3440 | static void |
| 3441 | print_save_msg(buf, nchars) |
| 3442 | nbbuf_T *buf; |
| 3443 | long nchars; |
| 3444 | { |
| 3445 | char_u c; |
| 3446 | char_u *p; |
| 3447 | |
| 3448 | if (nchars >= 0) |
| 3449 | { |
| 3450 | msg_add_fname(buf->bufp, buf->bufp->b_ffname); /* fname in IObuff with quotes */ |
| 3451 | c = FALSE; |
| 3452 | |
| 3453 | msg_add_lines(c, buf->bufp->b_ml.ml_line_count, |
| 3454 | (long)buf->bufp->b_orig_size); |
| 3455 | |
| 3456 | vim_free(keep_msg); |
| 3457 | keep_msg = NULL; |
| 3458 | msg_scrolled_ign = TRUE; |
| 3459 | p = msg_trunc_attr(IObuff, FALSE, 0); |
| 3460 | if ((msg_scrolled && !need_wait_return) || !buf->initDone) |
| 3461 | { |
| 3462 | /* Need to repeat the message after redrawing when: |
| 3463 | * - When reading from stdin (the screen will be cleared next). |
| 3464 | * - When restart_edit is set (otherwise there will be a delay |
| 3465 | * before redrawing). |
| 3466 | * - When the screen was scrolled but there is no wait-return |
| 3467 | * prompt. */ |
| 3468 | set_keep_msg(p); |
| 3469 | keep_msg_attr = 0; |
| 3470 | } |
| 3471 | msg_scrolled_ign = FALSE; |
| 3472 | /* add_to_input_buf((char_u *)"\f", 1); */ |
| 3473 | } |
| 3474 | else |
| 3475 | { |
| 3476 | char_u ebuf[BUFSIZ]; |
| 3477 | |
| 3478 | STRCPY(ebuf, (char_u *)_("E505: ")); |
| 3479 | STRCAT(ebuf, IObuff); |
| 3480 | STRCAT(ebuf, (char_u *)_("is read-only (add ! to override)")); |
| 3481 | STRCPY(IObuff, ebuf); |
| 3482 | emsg(IObuff); |
| 3483 | } |
| 3484 | } |
| 3485 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3486 | #endif /* defined(FEAT_NETBEANS_INTG) */ |