blob: 89f55efc7a236fd4cce9d39f5a6700ce485fef6c [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * Visual Workshop integration by Gordon Prieur
5 *
6 * Do ":help uganda" in Vim to read copying and usage conditions.
7 * Do ":help credits" in Vim to see a list of people who contributed.
8 * See README.txt for an overview of the Vim source code.
9 */
10
11#ifdef HAVE_CONFIG_H
12# include "auto/config.h"
13#endif
14#include <stdio.h>
15#include <stdlib.h>
Bram Moolenaardfccaf02004-12-31 20:56:11 +000016#include <sys/types.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000017#include <netdb.h>
18#include <netinet/in.h>
19#include <errno.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000020#include <sys/socket.h>
21#ifdef HAVE_LIBGEN_H
22# include <libgen.h>
23#endif
24#include <unistd.h>
25#include <string.h>
26#include <stdlib.h>
27#include <ctype.h>
28
29#include <X11/Intrinsic.h>
30#include <Xm/Xm.h>
31#include <Xm/PushB.h>
32
33#include "integration.h" /* <EditPlugin/integration.h> */
34
35#include "vim.h"
36#include "version.h"
37#include "gui_beval.h"
38#include "workshop.h"
39
40void workshop_hotkeys(Boolean);
41
42static Boolean isShowing(int);
43static win_T *get_window(buf_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +000044static void updatePriority(Boolean);
45static char *addUniqueMnemonic(char *, char *);
46static char *fixup(char *);
47static char *get_selection(buf_T *);
48static char *append_selection(int, char *, int *, int *);
49static void load_buffer_by_name(char *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000050static void load_window(char *, int lnum);
51static void warp_to_pc(int);
52#ifdef FEAT_BEVAL
Bram Moolenaarba07ce32010-01-06 18:25:34 +010053void workshop_beval_cb(BalloonEval *, int);
54static int computeIndex(int, char_u *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000055#endif
56static char *fixAccelText(char *);
57static void addMenu(char *, char *, char *);
58static char *lookupVerb(char *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000059static void coloncmd(char *, Boolean);
60
61extern Widget vimShell;
62extern Widget textArea;
63extern XtAppContext app_context;
64
65static int tbpri; /* ToolBar priority */
66int usingSunWorkShop = 0; /* set if -ws flag is used */
67char curMenuName[BUFSIZ];
68char curMenuPriority[BUFSIZ];
Bram Moolenaar071d4272004-06-13 20:20:40 +000069
70static Boolean workshopInitDone = False;
71static Boolean workshopHotKeysEnabled = False;
72
73/*
74 * The following enum is from <gp_dbx/gp_dbx_common.h>. We can't include it
75 * here because its C++.
76 */
77enum
78{
79 GPLineEval_EVALUATE, /* evaluate expression */
80 GPLineEval_INDIRECT, /* evaluate *<expression> */
81 GPLineEval_TYPE /* type of expression */
82};
83
84/*
85 * Store each verb in the MenuMap. This lets us map from a verb to a menu.
86 * There may be multiple matches for a single verb in this table.
87 */
88#define MENU_INC 50 /* menuMap incremental size increases */
89typedef struct
90{
91 char *name; /* name of the menu */
92 char *accel; /* optional accelerator key */
93 char *verb; /* menu verb */
94} MenuMap;
95static MenuMap *menuMap; /* list of verb/menu mappings */
96static int menuMapSize; /* current size of menuMap */
97static int menuMapMax; /* allocated size of menuMap */
98static char *initialFileCmd; /* save command but defer doing it */
99
100
101 void
102workshop_init()
103{
104 char_u buf[64];
105 int is_dirty = FALSE;
106 int width, height;
107 XtInputMask mask;
108
109 /*
110 * Turn on MenuBar, ToolBar, and Footer.
111 */
112 STRCPY(buf, p_go);
113 if (vim_strchr(p_go, GO_MENUS) == NULL)
114 {
115 STRCAT(buf, "m");
116 is_dirty = TRUE;
117 }
118 if (vim_strchr(p_go, GO_TOOLBAR) == NULL)
119 {
120 STRCAT(buf, "T");
121 is_dirty = TRUE;
122 }
123 if (vim_strchr(p_go, GO_FOOTER) == NULL)
124 {
125 STRCAT(buf, "F");
126 is_dirty = TRUE;
127 }
128 if (is_dirty)
129 set_option_value((char_u *)"go", 0L, buf, 0);
130
131 /*
132 * Set size from workshop_get_width_height().
133 */
134 width = height = 0;
135 if (workshop_get_width_height(&width, &height))
136 {
137 XtVaSetValues(vimShell,
138 XmNwidth, width,
139 XmNheight, height,
140 NULL);
141 }
142
143 /*
144 * Now read in the initial messages from eserve.
145 */
146 while ((mask = XtAppPending(app_context))
147 && (mask & XtIMAlternateInput) && !workshopInitDone)
148 XtAppProcessEvent(app_context, (XtInputMask)XtIMAlternateInput);
149}
150
151 void
152workshop_postinit()
153{
154 do_cmdline_cmd((char_u *)initialFileCmd);
155 ALT_INPUT_LOCK_OFF;
156 free(initialFileCmd);
157 initialFileCmd = NULL;
158}
159
160 void
161ex_wsverb(exarg_T *eap)
162{
163 msg_clr_cmdline();
164 workshop_perform_verb((char *) eap->arg, NULL);
165}
166
167/*
168 * Editor name
169 * This string is recognized by eserve and should be all lower case.
170 * This is how the editor detects that it is talking to gvim instead
171 * of NEdit, for example, when the connection is initiated from the editor.
172 */
173 char *
174workshop_get_editor_name()
175{
176 return "gvim";
177}
178
179/*
180 * Version number of the editor.
181 * This number is communicated along with the protocol
182 * version to the application.
183 */
184 char *
185workshop_get_editor_version()
186{
187 return Version;
188}
189
190/*
191 * Answer functions: called by eserve
192 */
193
194/*
195 * Name:
196 * workshop_load_file
197 *
198 * Function:
199 * Load a given file into the WorkShop buffer.
200 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201 void
202workshop_load_file(
203 char *filename, /* the file to load */
204 int line, /* an optional line number (or 0) */
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +0000205 char *frameid UNUSED) /* used for multi-frame support */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206{
207#ifdef WSDEBUG_TRACE
208 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
209 wstrace("workshop_load_file(%s, %d)\n", filename, line);
210#endif
211
212#ifdef FEAT_BEVAL
Bram Moolenaare4efc3b2005-03-07 23:16:51 +0000213 bevalServers |= BEVAL_WORKSHOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214#endif
215
216 load_window(filename, line);
217}
218
219/*
220 * Reload the WorkShop buffer
221 */
222 void
223workshop_reload_file(
224 char *filename,
225 int line)
226{
227#ifdef WSDEBUG_TRACE
228 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
229 wstrace("workshop_reload_file(%s, %d)\n", filename, line);
230#endif
231 load_window(filename, line);
232}
233
234 void
235workshop_show_file(
236 char *filename)
237{
238#ifdef WSDEBUG_TRACE
239 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
240 wstrace("workshop_show_file(%s)\n", filename);
241#endif
242
243 load_window(filename, 0);
244}
245
246 void
247workshop_goto_line(
248 char *filename,
249 int lineno)
250{
251#ifdef WSDEBUG_TRACE
252 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
253 wstrace("workshop_goto_line(%s, %d)\n", filename, lineno);
254#endif
255
256 load_window(filename, lineno);
257}
258
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 void
260workshop_front_file(
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +0000261 char *filename UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262{
263#ifdef WSDEBUG_TRACE
264 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
265 wstrace("workshop_front_file()\n");
266#endif
267 /*
268 * Assumption: This function will always be called after a call to
269 * workshop_show_file(), so the file is always showing.
270 */
271 if (vimShell != NULL)
272 XRaiseWindow(gui.dpy, XtWindow(vimShell));
273}
274
275 void
276workshop_save_file(
277 char *filename)
278{
279 char cbuf[BUFSIZ]; /* build vim command here */
280
281#ifdef WSDEBUG_TRACE
282 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
283 wstrace("workshop_save_file(%s)\n", filename);
284#endif
285
286 /* Save the given file */
Bram Moolenaar051b7822005-05-19 21:00:46 +0000287 vim_snprintf(cbuf, sizeof(cbuf), "w %s", filename);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288 coloncmd(cbuf, TRUE);
289}
290
291 void
292workshop_save_files()
293{
294 /* Save the given file */
295#ifdef WSDEBUG_TRACE
296 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
297 wstrace("workshop_save_files()\n");
298#endif
299
300 add_to_input_buf((char_u *) ":wall\n", 6);
301}
302
303 void
304workshop_quit()
305{
306#ifdef WSDEBUG_TRACE
307 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
308 wstrace("workshop_quit()\n");
309#endif
310
311 add_to_input_buf((char_u *) ":qall\n", 6);
312}
313
314 void
315workshop_minimize()
316{
317#ifdef WSDEBUG_TRACE
318 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
319 wstrace("workshop_minimize()\n");
320#endif
321 workshop_minimize_shell(vimShell);
322}
323 void
324workshop_maximize()
325{
326#ifdef WSDEBUG_TRACE
327 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
328 wstrace("workshop_maximize()\n");
329#endif
330
331 workshop_maximize_shell(vimShell);
332}
333
334 void
335workshop_add_mark_type(
336 int idx,
337 char *colorspec,
338 char *sign)
339{
340 char gbuf[BUFSIZ]; /* buffer for sign name */
341 char cibuf[BUFSIZ]; /* color information */
342 char cbuf[BUFSIZ]; /* command buffer */
343 char *bp;
344
345#ifdef WSDEBUG_TRACE
346 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
347 {
348 char *cp;
349
350 cp = strrchr(sign, '/');
351 if (cp == NULL)
352 cp = sign;
353 else
354 cp++; /* skip '/' character */
355 wstrace("workshop_add_mark_type(%d, \"%s\", \"%s\")\n", idx,
356 colorspec && *colorspec ? colorspec : "<None>", cp);
357 }
358#endif
359
360 /*
361 * Isolate the basename of sign in gbuf. We will use this for the
362 * GroupName in the highlight command sent to vim.
363 */
364 STRCPY(gbuf, gettail((char_u *)sign));
365 bp = strrchr(gbuf, '.');
366 if (bp != NULL)
367 *bp = NUL;
368
369 if (gbuf[0] != '-' && gbuf[1] != NUL)
370 {
371 if (colorspec != NULL && *colorspec)
372 {
Bram Moolenaar051b7822005-05-19 21:00:46 +0000373 vim_snprintf(cbuf, sizeof(cbuf),
374 "highlight WS%s guibg=%s", gbuf, colorspec);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 coloncmd(cbuf, FALSE);
Bram Moolenaar051b7822005-05-19 21:00:46 +0000376 vim_snprintf(cibuf, sizeof(cibuf), "linehl=WS%s", gbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377 }
378 else
379 cibuf[0] = NUL;
380
Bram Moolenaar051b7822005-05-19 21:00:46 +0000381 vim_snprintf(cbuf, sizeof(cbuf),
382 "sign define %d %s icon=%s", idx, cibuf, sign);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 coloncmd(cbuf, TRUE);
384 }
385}
386
387 void
388workshop_set_mark(
389 char *filename, /* filename which gets the mark */
390 int lineno, /* line number which gets the mark */
391 int markId, /* unique mark identifier */
392 int idx) /* which mark to use */
393{
394 char cbuf[BUFSIZ]; /* command buffer */
395
396 /* Set mark in a given file */
397#ifdef WSDEBUG_TRACE
398 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
399 wstrace("workshop_set_mark(%s, %d (ln), %d (id), %d (idx))\n",
400 filename, lineno, markId, idx);
401#endif
402
Bram Moolenaar051b7822005-05-19 21:00:46 +0000403 vim_snprintf(cbuf, sizeof(cbuf), "sign place %d line=%d name=%d file=%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404 markId, lineno, idx, filename);
405 coloncmd(cbuf, TRUE);
406}
407
408 void
409workshop_change_mark_type(
410 char *filename, /* filename which gets the mark */
411 int markId, /* unique mark identifier */
412 int idx) /* which mark to use */
413{
414 char cbuf[BUFSIZ]; /* command buffer */
415
416 /* Change mark type */
417#ifdef WSDEBUG_TRACE
418 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
419 wstrace("workshop_change_mark_type(%s, %d, %d)\n",
420 filename, markId, idx);
421#endif
422
Bram Moolenaar051b7822005-05-19 21:00:46 +0000423 vim_snprintf(cbuf, sizeof(cbuf),
424 "sign place %d name=%d file=%s", markId, idx, filename);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425 coloncmd(cbuf, TRUE);
426}
427
428/*
429 * Goto the given mark in a file (e.g. show it).
430 * If message is not null, display it in the footer.
431 */
432 void
433workshop_goto_mark(
434 char *filename,
435 int markId,
436 char *message)
437{
438 char cbuf[BUFSIZ]; /* command buffer */
439
440 /* Goto mark */
441#ifdef WSDEBUG_TRACE
442 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
443 wstrace("workshop_goto_mark(%s, %d (id), %s)\n",
444 filename, markId, message && *message &&
445 !(*message == ' ' && message[1] == NULL) ?
446 message : "<None>");
447#endif
448
Bram Moolenaar051b7822005-05-19 21:00:46 +0000449 vim_snprintf(cbuf, sizeof(cbuf), "sign jump %d file=%s", markId, filename);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 coloncmd(cbuf, TRUE);
451 if (message != NULL && *message != NUL)
452 gui_mch_set_footer((char_u *)message);
453}
454
455 void
456workshop_delete_mark(
457 char *filename,
458 int markId)
459{
460 char cbuf[BUFSIZ]; /* command buffer */
461
462 /* Delete mark */
463#ifdef WSDEBUG_TRACE
464 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
465 wstrace("workshop_delete_mark(%s, %d (id))\n",
466 filename, markId);
467#endif
468
Bram Moolenaar051b7822005-05-19 21:00:46 +0000469 vim_snprintf(cbuf, sizeof(cbuf),
470 "sign unplace %d file=%s", markId, filename);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471 coloncmd(cbuf, TRUE);
472}
473
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 int
475workshop_get_mark_lineno(
476 char *filename,
477 int markId)
478{
479 buf_T *buf; /* buffer containing filename */
480 int lineno; /* line number of filename in buf */
481
482 /* Get mark line number */
483#ifdef WSDEBUG_TRACE
484 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
485 wstrace("workshop_get_mark_lineno(%s, %d)\n",
486 filename, markId);
487#endif
488
489 lineno = 0;
490 buf = buflist_findname((char_u *)filename);
491 if (buf != NULL)
492 lineno = buf_findsign(buf, markId);
493
494 return lineno;
495}
496
497
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498/*
499 * Are there any moved marks? If so, call workshop_move_mark on
500 * each of them now. This is how eserve can find out if for example
501 * breakpoints have moved when a program has been recompiled and
502 * reloaded into dbx.
503 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 void
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +0000505workshop_moved_marks(char *filename UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506{
507#ifdef WSDEBUG_TRACE
508 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
509 wstrace("XXXworkshop_moved_marks(%s)\n", filename);
510#endif
511}
512
513 int
514workshop_get_font_height()
515{
516 XmFontList fontList; /* fontList made from gui.norm_font */
517 XmString str;
518 Dimension w;
519 Dimension h;
520
521#ifdef WSDEBUG_TRACE
522 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
523 wstrace("workshop_get_font_height()\n");
524#endif
525
526 /* Pick the proper signs for this font size */
527 fontList = gui_motif_create_fontlist((XFontStruct *)gui.norm_font);
528 h = 0;
529 if (fontList != NULL)
530 {
531 str = XmStringCreateLocalized("A");
532 XmStringExtent(fontList, str, &w, &h);
533 XmStringFree(str);
534 XmFontListFree(fontList);
535 }
536
537 return (int)h;
538}
539
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 void
541workshop_footer_message(
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +0000542 char *message,
543 int severity UNUSED) /* severity is currently unused */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544{
545#ifdef WSDEBUG_TRACE
546 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
547 wstrace("workshop_footer_message(%s, %d)\n", message, severity);
548#endif
549
550 gui_mch_set_footer((char_u *) message);
551}
552
553/*
554 * workshop_menu_begin() is passed the menu name. We determine its mnemonic
555 * here and store its name and priority.
556 */
557 void
558workshop_menu_begin(
559 char *label)
560{
561 vimmenu_T *menu; /* pointer to last menu */
562 int menuPriority = 0; /* priority of new menu */
563 char mnembuf[64]; /* store menubar mnemonics here */
564 char *name; /* label with a mnemonic */
565 char *p; /* used to find mnemonics */
566 int idx; /* index into mnembuf */
567
568#ifdef WSDEBUG_TRACE
569 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
570 wstrace("workshop_menu_begin()\n");
571#endif
572
573 /*
574 * Look through all existing (non-PopUp and non-Toolbar) menus
575 * and gather their mnemonics. Use this list to decide what
576 * mnemonic should be used for label.
577 */
578
579 idx = 0;
580 mnembuf[idx++] = 'H'; /* H is mnemonic for Help */
581 for (menu = root_menu; menu != NULL; menu = menu->next)
582 {
583 if (menu_is_menubar(menu->name))
584 {
585 p = strchr((char *)menu->name, '&');
586 if (p != NULL)
587 mnembuf[idx++] = *++p;
588 }
589 if (menu->next != NULL
590 && strcmp((char *) menu->next->dname, "Help") == 0)
591 {
592 menuPriority = menu->priority + 10;
593 break;
594 }
595 }
596 mnembuf[idx++] = NUL;
597 name = addUniqueMnemonic(mnembuf, label);
598
Bram Moolenaar051b7822005-05-19 21:00:46 +0000599 vim_snprintf(curMenuName, sizeof(curMenuName), "%s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 sprintf(curMenuPriority, "%d.0", menuPriority);
601}
602
603/*
604 * Append the name and priority to strings to be used in vim menu commands.
605 */
606 void
607workshop_submenu_begin(
608 char *label)
609{
610#ifdef WSDEBUG_TRACE
611 if (ws_debug && ws_dlevel & WS_TRACE
612 && strncmp(curMenuName, "ToolBar", 7) != 0)
613 wstrace("workshop_submenu_begin(%s)\n", label);
614#endif
615
616 strcat(curMenuName, ".");
617 strcat(curMenuName, fixup(label));
618
619 updatePriority(True);
620}
621
622/*
623 * Remove the submenu name and priority from curMenu*.
624 */
625
626 void
627workshop_submenu_end()
628{
629 char *p;
630
631#ifdef WSDEBUG_TRACE
632 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE)
633 && strncmp(curMenuName, "ToolBar", 7) != 0)
634 wstrace("workshop_submenu_end()\n");
635#endif
636
637 p = strrchr(curMenuPriority, '.');
638 ASSERT(p != NULL);
639 *p = NUL;
640
641 p = strrchr(curMenuName, '.');
642 ASSERT(p != NULL);
643 *p = NUL;
644}
645
646/*
647 * This is where menus are really made. Each item will generate an amenu vim
648 * command. The globals curMenuName and curMenuPriority contain the name and
649 * priority of the parent menu tree.
650 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 void
652workshop_menu_item(
653 char *label,
654 char *verb,
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +0000655 char *accelerator UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 char *acceleratorText,
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +0000657 char *name UNUSED,
658 char *filepos UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 char *sensitive)
660{
661 char cbuf[BUFSIZ];
662 char namebuf[BUFSIZ];
663 char accText[BUFSIZ];
664
665#ifdef WSDEBUG_TRACE
666 if (WSDLEVEL(WS_TRACE_VERBOSE)
667 && strncmp(curMenuName, "ToolBar", 7) != 0)
668 {
669 if (ws_dlevel & WS_TRACE_VERBOSE)
670 wsdebug("workshop_menu_item(\n"
671 "\tlabel = \"%s\",\n"
672 "\tverb = %s,\n"
673 "\taccelerator = %s,\n"
674 "\tacceleratorText = \"%s\",\n"
675 "\tname = %s,\n"
676 "\tfilepos = %s,\n"
677 "\tsensitive = %s)\n",
678 label && *label ? label : "<None>",
679 verb && *verb ? verb : "<None>",
680 accelerator && *accelerator ?
681 accelerator : "<None>",
682 acceleratorText && *acceleratorText ?
683 acceleratorText : "<None>",
684 name && *name ? name : "<None>",
685 filepos && *filepos ? filepos : "<None>",
686 sensitive);
687 else if (ws_dlevel & WS_TRACE)
688 wstrace("workshop_menu_item(\"%s\", %s)\n",
689 label && *label ? label : "<None>",
690 verb && *verb ? verb : "<None>", sensitive);
691 }
692#endif
693#ifdef WSDEBUG_SENSE
694 if (ws_debug)
695 wstrace("menu: %-21.20s%-21.20s(%s)\n", label, verb,
696 *sensitive == '1' ? "Sensitive" : "Insensitive");
697#endif
698
699 if (acceleratorText != NULL)
Bram Moolenaar051b7822005-05-19 21:00:46 +0000700 vim_snprintf(accText, sizeof(accText), "<Tab>%s", acceleratorText);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 else
702 accText[0] = NUL;
703 updatePriority(False);
Bram Moolenaar051b7822005-05-19 21:00:46 +0000704 vim_snprintf(namebuf, sizeof(namebuf), "%s.%s", curMenuName, fixup(label));
705 vim_snprintf(cbuf, sizeof(cbuf), "amenu %s %s%s\t:wsverb %s<CR>",
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 curMenuPriority, namebuf, accText, verb);
707
708 coloncmd(cbuf, TRUE);
709 addMenu(namebuf, fixAccelText(acceleratorText), verb);
710
711 if (*sensitive == '0')
712 {
Bram Moolenaar051b7822005-05-19 21:00:46 +0000713 vim_snprintf(cbuf, sizeof(cbuf), "amenu disable %s", namebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 coloncmd(cbuf, TRUE);
715 }
716}
717
718/*
719 * This function is called when a complete WorkShop menu description has been
720 * sent over from eserve. We do some menu cleanup.
721 */
722
723 void
724workshop_menu_end()
725{
726 Boolean using_tearoff; /* set per current option setting */
727
728#ifdef WSDEBUG_TRACE
729 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
730 wstrace("workshop_menu_end()\n");
731#endif
732
733 using_tearoff = vim_strchr(p_go, GO_TEAROFF) != NULL;
734 gui_mch_toggle_tearoffs(using_tearoff);
735}
736
737 void
738workshop_toolbar_begin()
739{
740#ifdef WSDEBUG_TRACE
741 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
742 wstrace("workshop_toolbar_begin()\n");
743#endif
744
745 coloncmd("aunmenu ToolBar", True);
746 tbpri = 10;
747}
748
749 void
750workshop_toolbar_end()
751{
752 char_u buf[64];
753
754#ifdef WSDEBUG_TRACE
755 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
756 {
757 wstrace("workshop_toolbar_end()\n");
758 }
759#endif
760
761 /*
762 * Turn on ToolBar.
763 */
764 STRCPY(buf, p_go);
765 if (vim_strchr(p_go, 'T') == NULL)
766 {
767 STRCAT(buf, "T");
768 set_option_value((char_u *)"go", 0L, buf, 0);
769 }
770 workshopInitDone = True;
771}
772
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 void
774workshop_toolbar_button(
775 char *label,
776 char *verb,
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +0000777 char *senseVerb UNUSED,
778 char *filepos UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 char *help,
780 char *sense,
781 char *file,
782 char *left)
783{
784 char cbuf[BUFSIZ + MAXPATHLEN];
785 char namebuf[BUFSIZ];
786 static int tbid = 1;
787 char_u *p;
788
789#ifdef WSDEBUG_TRACE
790 if (WSDLEVEL(WS_TRACE_VERBOSE))
791 wsdebug("workshop_toolbar_button(\"%s\", %s, %s,\n"
792 "\t%s, \"%s\", %s,\n\t\"%s\",\n\t<%s>)\n",
793 label && *label ? label : "<None>",
794 verb && *verb ? verb : "<None>",
795 senseVerb && *senseVerb ? senseVerb : "<None>",
796 filepos && *filepos ? filepos : "<None>",
797 help && *help ? help : "<None>",
798 sense && *sense ? sense : "<None>",
799 file && *file ? file : "<None>",
800 left && *left ? left : "<None>");
801 else if (WSDLEVEL(WS_TRACE))
802 wstrace("workshop_toolbar_button(\"%s\", %s)\n",
803 label && *label ? label : "<None>",
804 verb && *verb ? verb : "<None>");
805#endif
806#ifdef WSDEBUG_SENSE
807 if (ws_debug)
808 wsdebug("button: %-21.20s%-21.20s(%s)\n", label, verb,
809 *sense == '1' ? "Sensitive" : "Insensitive");
810#endif
811
812 if (left && *left && atoi(left) > 0)
813 {
814 /* Add a separator (but pass the width passed after the ':') */
815 sprintf(cbuf, "amenu 1.%d ToolBar.-sep%d:%s- <nul>",
816 tbpri - 5, tbid++, left);
817
818 coloncmd(cbuf, True);
819 }
820
821 p = vim_strsave_escaped((char_u *)label, (char_u *)"\\. ");
Bram Moolenaar051b7822005-05-19 21:00:46 +0000822 vim_snprintf(namebuf, sizeof(namebuf), "ToolBar.%s", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 vim_free(p);
824 STRCPY(cbuf, "amenu <silent> ");
825 if (file != NULL && *file != NUL)
826 {
827 p = vim_strsave_escaped((char_u *)file, (char_u *)" ");
Bram Moolenaara800b422010-06-27 01:15:55 +0200828 vim_snprintf_add(cbuf, sizeof(cbuf), "icon=%s ", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 vim_free(p);
830 }
Bram Moolenaara800b422010-06-27 01:15:55 +0200831 vim_snprintf_add(cbuf, sizeof(cbuf),"1.%d %s :wsverb %s<CR>",
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 tbpri, namebuf, verb);
833
834 /* Define the menu item */
835 coloncmd(cbuf, True);
836
837 if (*sense == '0')
838 {
839 /* If menu isn't sensitive at startup... */
Bram Moolenaar051b7822005-05-19 21:00:46 +0000840 vim_snprintf(cbuf, sizeof(cbuf), "amenu disable %s", namebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 coloncmd(cbuf, True);
842 }
843
844 if (help && *help)
845 {
846 /* Do the tooltip */
Bram Moolenaar051b7822005-05-19 21:00:46 +0000847 vim_snprintf(cbuf, sizeof(cbuf), "tmenu %s %s", namebuf, help);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 coloncmd(cbuf, True);
849 }
850
851 addMenu(namebuf, NULL, verb);
852 tbpri += 10;
853}
854
855 void
856workshop_frame_sensitivities(
857 VerbSense *vs) /* list of verbs to (de)sensitize */
858{
859 VerbSense *vp; /* iterate through vs */
860 char *menu_name; /* used in menu lookup */
861 int cnt; /* count of verbs to skip */
862 int len; /* length of nonvariant part of command */
863 char cbuf[4096];
864
865#ifdef WSDEBUG_TRACE
866 if (WSDLEVEL(WS_TRACE_VERBOSE) || WSDLEVEL(4))
867 {
868 wsdebug("workshop_frame_sensitivities(\n");
869 for (vp = vs; vp->verb != NULL; vp++)
870 wsdebug("\t%-25s%d\n", vp->verb, vp->sense);
871 wsdebug(")\n");
872 }
873 else if (WSDLEVEL(WS_TRACE))
874 wstrace("workshop_frame_sensitivities()\n");
875#endif
876#ifdef WSDEBUG_SENSE
877 if (ws_debug)
878 for (vp = vs; vp->verb != NULL; vp++)
879 wsdebug("change: %-21.20s%-21.20s(%s)\n",
880 "", vp->verb, vp->sense == 1 ?
881 "Sensitive" : "Insensitive");
882#endif
883
884 /*
885 * Look for all matching menu entries for the verb. There may be more
886 * than one if the verb has both a menu and toolbar entry.
887 */
888 for (vp = vs; vp->verb != NULL; vp++)
889 {
890 cnt = 0;
891 strcpy(cbuf, "amenu");
892 strcat(cbuf, " ");
893 strcat(cbuf, vp->sense ? "enable" : "disable");
894 strcat(cbuf, " ");
895 len = strlen(cbuf);
896 while ((menu_name = lookupVerb(vp->verb, cnt++)) != NULL)
897 {
898 strcpy(&cbuf[len], menu_name);
899 coloncmd(cbuf, FALSE);
900 }
901 }
902 gui_update_menus(0);
903 gui_mch_flush();
904}
905
906 void
907workshop_set_option(
908 char *option, /* name of a supported option */
909 char *value) /* value to set option to */
910{
911 char cbuf[BUFSIZ]; /* command buffer */
912
913#ifdef WSDEBUG_TRACE
914 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
915 {
916 wstrace("workshop_set_option(%s, %s)\n", option, value);
917 }
918#endif
919
920 cbuf[0] = NUL;
921 switch (*option) /* switch on 1st letter */
922 {
923 case 's':
924 if (strcmp(option, "syntax") == 0)
Bram Moolenaar051b7822005-05-19 21:00:46 +0000925 vim_snprintf(cbuf, sizeof(cbuf), "syntax %s", value);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 else if (strcmp(option, "savefiles") == 0)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +0000927 {
928 /* XXX - Not yet implemented */
929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 break;
931
932 case 'l':
933 if (strcmp(option, "lineno") == 0)
934 sprintf(cbuf, "set %snu",
935 (strcmp(value, "on") == 0) ? "" : "no");
936 break;
937
938 case 'p':
939 if (strcmp(option, "parentheses") == 0)
940 sprintf(cbuf, "set %ssm",
941 (strcmp(value, "on") == 0) ? "" : "no");
942 break;
943
944 case 'w':
945 /* this option is set by a direct call */
946#ifdef WSDEBUG
947 wsdebug("workshop_set_option: "
948 "Got unexpected workshopkeys option");
949#endif
950 break;
951
952 case 'b': /* these options are set from direct calls */
953 if (option[7] == NUL && strcmp(option, "balloon") == 0)
954 {
955#ifdef WSDEBUG
956 /* set by direct call to workshop_balloon_mode */
957 wsdebug("workshop_set_option: "
958 "Got unexpected ballooneval option");
959#endif
960 }
961 else if (strcmp(option, "balloondelay") == 0)
962 {
963#ifdef WSDEBUG
964 /* set by direct call to workshop_balloon_delay */
965 wsdebug("workshop_set_option: "
966 "Got unexpected balloondelay option");
967#endif
968 }
969 break;
970 }
971 if (cbuf[0] != NUL)
972 coloncmd(cbuf, TRUE);
973}
974
975
976 void
977workshop_balloon_mode(
978 Boolean on)
979{
980 char cbuf[BUFSIZ]; /* command buffer */
981
982#ifdef WSDEBUG_TRACE
983 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
984 wstrace("workshop_balloon_mode(%s)\n", on ? "True" : "False");
985#endif
986
987 sprintf(cbuf, "set %sbeval", on ? "" : "no");
988 coloncmd(cbuf, TRUE);
989}
990
991
992 void
993workshop_balloon_delay(
994 int delay)
995{
996 char cbuf[BUFSIZ]; /* command buffer */
997
998#ifdef WSDEBUG_TRACE
999 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
1000 wstrace("workshop_balloon_delay(%d)\n", delay);
1001#endif
1002
1003 sprintf(cbuf, "set bdlay=%d", delay);
1004 coloncmd(cbuf, TRUE);
1005}
1006
1007
1008 void
1009workshop_show_balloon_tip(
1010 char *tip)
1011{
1012#ifdef WSDEBUG_TRACE
1013 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
1014 wstrace("workshop_show_balloon_tip(%s)\n", tip);
1015#endif
1016
1017 if (balloonEval != NULL)
1018 gui_mch_post_balloon(balloonEval, (char_u *)tip);
1019}
1020
1021
1022 void
1023workshop_hotkeys(
1024 Boolean on)
1025{
1026 char cbuf[BUFSIZ]; /* command buffer */
1027 MenuMap *mp; /* iterate over menuMap entries */
1028
1029#ifdef WSDEBUG_TRACE
1030 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
1031 wstrace("workshop_hotkeys(%s)\n", on ? "True" : "False");
1032#endif
1033
1034 workshopHotKeysEnabled = on;
1035 if (workshopHotKeysEnabled)
1036 for (mp = menuMap; mp < &menuMap[menuMapSize]; mp++)
1037 {
1038 if (mp->accel != NULL)
1039 {
Bram Moolenaar051b7822005-05-19 21:00:46 +00001040 vim_snprintf(cbuf, sizeof(cbuf),
1041 "map %s :wsverb %s<CR>", mp->accel, mp->verb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 coloncmd(cbuf, TRUE);
1043 }
1044 }
1045 else
1046 for (mp = menuMap; mp < &menuMap[menuMapSize]; mp++)
1047 {
1048 if (mp->accel != NULL)
1049 {
Bram Moolenaar051b7822005-05-19 21:00:46 +00001050 vim_snprintf(cbuf, sizeof(cbuf), "unmap %s", mp->accel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 coloncmd(cbuf, TRUE);
1052 }
1053 }
1054}
1055
1056/*
1057 * A button in the toolbar has been pushed.
1058 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 int
1060workshop_get_positions(
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001061 void *clientData UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 char **filename, /* output data */
1063 int *curLine, /* output data */
1064 int *curCol, /* output data */
1065 int *selStartLine, /* output data */
1066 int *selStartCol, /* output data */
1067 int *selEndLine, /* output data */
1068 int *selEndCol, /* output data */
1069 int *selLength, /* output data */
1070 char **selection) /* output data */
1071{
1072 static char ffname[MAXPATHLEN];
1073
1074#ifdef WSDEBUG_TRACE
1075 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
1076 wstrace("workshop_get_positions(%#x, \"%s\", ...)\n",
1077 clientData, (curbuf && curbuf->b_sfname != NULL)
1078 ? (char *)curbuf->b_sfname : "<None>");
1079#endif
1080
Bram Moolenaara40b4662008-11-28 10:47:47 +00001081 if (curbuf->b_ffname == NULL)
1082 ffname[0] = NUL;
1083 else
1084 /* copy so nobody can change b_ffname */
1085 strcpy(ffname, (char *) curbuf->b_ffname);
1086 *filename = ffname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 *curLine = curwin->w_cursor.lnum;
1088 *curCol = curwin->w_cursor.col;
1089
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001090 if (curbuf->b_visual.vi_mode == 'v' &&
1091 equalpos(curwin->w_cursor, curbuf->b_visual.vi_end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001093 *selStartLine = curbuf->b_visual.vi_start.lnum;
1094 *selStartCol = curbuf->b_visual.vi_start.col;
1095 *selEndLine = curbuf->b_visual.vi_end.lnum;
1096 *selEndCol = curbuf->b_visual.vi_end.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 *selection = get_selection(curbuf);
1098 if (*selection)
1099 *selLength = strlen(*selection);
1100 else
1101 *selLength = 0;
1102 }
1103 else
1104 {
1105 *selStartLine = *selEndLine = -1;
1106 *selStartCol = *selEndCol = -1;
1107 *selLength = 0;
1108 *selection = "";
1109 }
1110
1111 return True;
1112}
1113
1114
1115
1116/************************************************************************
1117 * Utility functions
1118 ************************************************************************/
1119
1120 static char *
1121get_selection(
1122 buf_T *buf) /* buffer whose selection we want */
1123{
1124 pos_T *start; /* start of the selection */
1125 pos_T *end; /* end of the selection */
1126 char *lp; /* pointer to actual line data */
1127 int llen; /* length of actual line data */
1128 char *sp; /* pointer to selection buffer */
1129 int slen; /* string length in selection buffer */
1130 int size; /* size of selection buffer */
1131 char *new_sp; /* temp pointer to new sp */
1132 int lnum; /* line number we are appending */
1133
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001134 if (buf->b_visual.vi_mode == 'v')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001136 start = &buf->b_visual.vi_start;
1137 end = &buf->b_visual.vi_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 if (start->lnum == end->lnum)
1139 {
1140 /* selection is all on one line */
1141 lp = (char *) ml_get_pos(start);
1142 llen = end->col - start->col + 1;
1143 sp = (char *) malloc(llen + 1);
1144 if (sp != NULL)
1145 {
1146 strncpy(sp, lp, llen);
1147 sp[llen] = NUL;
1148 }
1149 }
1150 else
1151 {
1152 /* multi-line selection */
1153 lp = (char *) ml_get_pos(start);
1154 llen = strlen(lp);
1155 sp = (char *) malloc(BUFSIZ + llen);
1156 if (sp != NULL)
1157 {
1158 size = BUFSIZ + llen;
1159 strcpy(sp, lp);
1160 sp[llen] = '\n';
1161 slen = llen + 1;
1162
1163 lnum = start->lnum + 1;
1164 while (lnum < end->lnum)
1165 sp = append_selection(lnum++, sp, &size, &slen);
1166
1167 lp = (char *) ml_get(end->lnum);
1168 llen = end->col + 1;
1169 if ((slen + llen) >= size)
1170 {
1171 new_sp = (char *)
1172 realloc(sp, slen + llen + 1);
1173 if (new_sp != NULL)
1174 {
1175 size += llen + 1;
1176 sp = new_sp;
1177 }
1178 }
1179 if ((slen + llen) < size)
1180 {
1181 strncpy(&sp[slen], lp, llen);
1182 sp[slen + llen] = NUL;
1183 }
1184
1185 }
1186 }
1187 }
1188 else
1189 sp = NULL;
1190
1191 return sp;
1192}
1193
1194 static char *
1195append_selection(
1196 int lnum, /* line number to append */
1197 char *sp, /* pointer to selection buffer */
1198 int *size, /* ptr to size of sp */
1199 int *slen) /* ptr to length of selection string */
1200{
1201 char *lp; /* line of data from buffer */
1202 int llen; /* strlen of lp */
1203 char *new_sp; /* temp pointer to new sp */
1204
1205 lp = (char *)ml_get((linenr_T)lnum);
1206 llen = strlen(lp);
1207
1208 if ((*slen + llen) <= *size)
1209 {
1210 new_sp = (char *) realloc((void *) sp, BUFSIZ + *slen + llen);
1211 if (*new_sp != NUL)
1212 {
1213 *size = BUFSIZ + *slen + llen;
1214 sp = new_sp;
1215 }
1216 }
1217 if ((*slen + llen) > *size)
1218 {
1219 strcat(&sp[*slen], lp);
1220 *slen += llen;
1221 sp[*slen++] = '\n';
1222 }
1223
1224 return sp;
1225}
1226
1227
1228
1229 static void
1230load_buffer_by_name(
1231 char *filename, /* the file to load */
1232 int lnum) /* an optional line number (or 0) */
1233{
1234 char lnumbuf[16]; /* make line number option for :e */
1235 char cbuf[BUFSIZ]; /* command buffer */
1236
1237 if (lnum > 0)
1238 sprintf(lnumbuf, "+%d", lnum);
1239 else
1240 lnumbuf[0] = NUL;
1241
Bram Moolenaar051b7822005-05-19 21:00:46 +00001242 vim_snprintf(cbuf, sizeof(cbuf), "e %s %s", lnumbuf, filename);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 coloncmd(cbuf, False);
1244}
1245
1246
1247 static void
1248load_window(
1249 char *filename, /* filename to load */
1250 int lnum) /* linenumber to go to */
1251{
1252 buf_T *buf; /* buffer filename is stored in */
1253 win_T *win; /* window filenme is displayed in */
1254
1255 /*
1256 * Make sure filename is displayed and is the current window.
1257 */
1258
1259 buf = buflist_findname((char_u *)filename);
1260 if (buf == NULL || (win = get_window(buf)) == NULL)
1261 {
1262 /* No buffer or buffer is not in current window */
1263 /* wsdebug("load_window: load_buffer_by_name(\"%s\", %d)\n",
1264 filename, lnum); */
1265 load_buffer_by_name(filename, lnum);
1266 }
1267 else
1268 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001269#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 /* buf is in a window */
1271 if (win != curwin)
1272 {
1273 win_enter(win, False);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001274 /* wsdebug("load_window: window enter %s\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 win->w_buffer->b_sfname); */
1276 }
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001277#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 if (lnum > 0 && win->w_cursor.lnum != lnum)
1279 {
1280 warp_to_pc(lnum);
1281 /* wsdebug("load_window: warp to %s[%d]\n",
1282 win->w_buffer->b_sfname, lnum); */
1283 }
1284 }
1285 out_flush();
1286}
1287
1288
1289
1290 static void
1291warp_to_pc(
1292 int lnum) /* line number to warp to */
1293{
Bram Moolenaar720c7102007-05-10 18:07:50 +00001294 char lbuf[256]; /* build line command here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295
1296 if (lnum > 0)
1297 {
1298 if (State & INSERT)
1299 add_to_input_buf((char_u *) "\033", 1);
1300 if (isShowing(lnum))
1301 sprintf(lbuf, "%dG", lnum);
1302 else
1303 sprintf(lbuf, "%dz.", lnum);
1304 add_to_input_buf((char_u *) lbuf, strlen(lbuf));
1305 }
1306}
1307
1308 static Boolean
1309isShowing(
1310 int lnum) /* tell if line number is showing */
1311{
1312 return lnum >= curwin->w_topline && lnum < curwin->w_botline;
1313}
1314
1315
1316
1317 static win_T *
1318get_window(
1319 buf_T *buf) /* buffer to find window for */
1320{
1321 win_T *wp = NULL; /* window filename is in */
1322
1323 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
1324 if (buf == wp->w_buffer)
1325 break;
1326 return wp;
1327}
1328
1329
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 static void
1331updatePriority(
1332 Boolean subMenu) /* if True then start new submenu pri */
1333{
1334 int pri; /* priority of this menu/item */
1335 char *p;
1336
1337 p = strrchr(curMenuPriority, '.');
1338 ASSERT(p != NULL);
1339 *p++ = NUL;
1340
1341 pri = atoi(p) + 10; /* our new priority */
1342
1343 if (subMenu)
Bram Moolenaar051b7822005-05-19 21:00:46 +00001344 vim_snprintf(curMenuPriority, sizeof(curMenuPriority),
1345 "%s.%d.0", curMenuPriority, pri);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 else
Bram Moolenaar051b7822005-05-19 21:00:46 +00001347 vim_snprintf(curMenuPriority, sizeof(curMenuPriority),
1348 "%s.%d", curMenuPriority, pri);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349}
1350
1351 static char *
1352addUniqueMnemonic(
1353 char *mnemonics, /* currently used mnemonics */
1354 char *label) /* label of menu needing mnemonic */
1355{
1356 static char name[BUFSIZ]; /* buffer for the updated name */
1357 char *p; /* pointer into label */
1358 char *found; /* pointer to possible mnemonic */
1359
1360 found = NULL;
1361 for (p = label; *p != NUL; p++)
1362 if (strchr(mnemonics, *p) == 0)
1363 if (found == NULL || (isupper((int)*p) && islower((int)*found)))
1364 found = p;
1365
1366 if (found != NULL)
1367 {
1368 strncpy(name, label, (found - label));
1369 strcat(name, "&");
1370 strcat(name, found);
1371 }
1372 else
1373 strcpy(name, label);
1374
1375 return name;
1376}
1377
1378/*
1379 * Some characters in a menu name must be escaped in vim. Since this is vim
1380 * specific, it must be done on this side.
1381 */
1382 static char *
1383fixup(
1384 char *label)
1385{
1386 static char buf[BUFSIZ];
1387 char *bp; /* pointer into buf */
1388 char *lp; /* pointer into label */
1389
1390 lp = label;
1391 bp = buf;
1392 while (*lp != NUL)
1393 {
1394 if (*lp == ' ' || *lp == '.')
1395 *bp++ = '\\';
1396 *bp++ = *lp++;
1397 }
1398 *bp = NUL;
1399
1400 return buf;
1401}
1402
1403
1404#ifdef NOHANDS_SUPPORT_FUNCTIONS
1405
1406/* For the NoHands test suite */
1407
1408 char *
1409workshop_test_getcurrentfile()
1410{
1411 char *filename, *selection;
1412 int curLine, curCol, selStartLine, selStartCol, selEndLine;
1413 int selEndCol, selLength;
1414
1415 if (workshop_get_positions(
1416 NULL, &filename, &curLine, &curCol, &selStartLine,
1417 &selStartCol, &selEndLine, &selEndCol, &selLength,
1418 &selection))
1419 return filename;
1420 else
1421 return NULL;
1422}
1423
1424 int
1425workshop_test_getcursorrow()
1426{
1427 return 0;
1428}
1429
1430 int
1431workshop_test_getcursorcol()
1432{
1433 char *filename, *selection;
1434 int curLine, curCol, selStartLine, selStartCol, selEndLine;
1435 int selEndCol, selLength;
1436
1437 if (workshop_get_positions(
1438 NULL, &filename, &curLine, &curCol, &selStartLine,
1439 &selStartCol, &selEndLine, &selEndCol, &selLength,
1440 &selection))
1441 return curCol;
1442 else
1443 return -1;
1444}
1445
1446 char *
1447workshop_test_getcursorrowtext()
1448{
1449 return NULL;
1450}
1451
1452 char *
1453workshop_test_getselectedtext()
1454{
1455 char *filename, *selection;
1456 int curLine, curCol, selStartLine, selStartCol, selEndLine;
1457 int selEndCol, selLength;
1458
1459 if (workshop_get_positions(
1460 NULL, &filename, &curLine, &curCol, &selStartLine,
1461 &selStartCol, &selEndLine, &selEndCol, &selLength,
1462 &selection))
1463 return selection;
1464 else
1465 return NULL;
1466}
1467
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 void
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001469workshop_save_sensitivity(char *filename UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470{
1471}
1472
1473#endif
1474
1475 static char *
1476fixAccelText(
1477 char *ap) /* original acceleratorText */
1478{
1479 char buf[256]; /* build in temp buffer */
1480 char *shift; /* shift string of "" */
1481
1482 if (ap == NULL)
1483 return NULL;
1484
1485 /* If the accelerator is shifted use the vim form */
1486 if (strncmp("Shift+", ap, 6) == 0)
1487 {
1488 shift = "S-";
1489 ap += 6;
1490 }
1491 else
1492 shift = "";
1493
1494 if (*ap == 'F' && atoi(&ap[1]) > 0)
1495 {
Bram Moolenaar051b7822005-05-19 21:00:46 +00001496 vim_snprintf(buf, sizeof(buf), "<%s%s>", shift, ap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 return strdup(buf);
1498 }
1499 else
1500 return NULL;
1501}
1502
1503#ifdef FEAT_BEVAL
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00001504 void
1505workshop_beval_cb(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 BalloonEval *beval,
1507 int state)
1508{
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00001509 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 char_u *text;
1511 int type;
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00001512 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 int col;
1514 int idx;
1515 char buf[MAXPATHLEN * 2];
1516 static int serialNo = -1;
1517
1518 if (!p_beval)
1519 return;
1520
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00001521 if (get_beval_info(beval, FALSE, &wp, &lnum, &text, &col) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 {
1523 if (text && text[0])
1524 {
1525 /* Send debugger request */
1526 if (strlen((char *) text) > (MAXPATHLEN/2))
1527 {
1528 /*
1529 * The user has probably selected the entire
1530 * buffer or something like that - don't attempt
1531 * to evaluate it
1532 */
1533 return;
1534 }
1535
1536 /*
1537 * WorkShop expects the col to be a character index, not
1538 * a column number. Compute the index from col. Also set
1539 * line to 0 because thats what dbx expects.
1540 */
1541 idx = computeIndex(col, text, beval->ts);
1542 if (idx > 0)
1543 {
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00001544 lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545
1546 /*
1547 * If successful, it will respond with a balloon cmd.
1548 */
1549 if (state & ControlMask)
1550 /* Evaluate *(expression) */
1551 type = (int)GPLineEval_INDIRECT;
1552 else if (state & ShiftMask)
1553 /* Evaluate type(expression) */
1554 type = (int)GPLineEval_TYPE;
1555 else
1556 /* Evaluate value(expression) */
1557 type = (int)GPLineEval_EVALUATE;
1558
1559 /* Send request to dbx */
Bram Moolenaar051b7822005-05-19 21:00:46 +00001560 vim_snprintf(buf, sizeof(buf), "toolVerb debug.balloonEval "
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00001561 "%s %ld,0 %d,0 %d,%d %ld %s\n",
1562 (char *)wp->w_buffer->b_ffname,
1563 (long)lnum, idx, type, serialNo++,
1564 (long)strlen((char *)text), (char *)text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 balloonEval = beval;
1566 workshop_send_message(buf);
1567 }
1568 }
1569 }
1570}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571
1572 static int
1573computeIndex(
1574 int wantedCol,
1575 char_u *line,
1576 int ts)
1577{
1578 int col = 0;
1579 int idx = 0;
1580
1581 while (line[idx])
1582 {
1583 if (line[idx] == '\t')
1584 col += ts - (col % ts);
1585 else
1586 col++;
1587 idx++;
1588 if (col >= wantedCol)
1589 return idx;
1590 }
1591
1592 return -1;
1593}
Bram Moolenaarba07ce32010-01-06 18:25:34 +01001594#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595
1596 static void
1597addMenu(
1598 char *menu, /* menu name */
1599 char *accel, /* accelerator text (optional) */
1600 char *verb) /* WorkShop action-verb */
1601{
1602 MenuMap *newMap;
1603 char cbuf[BUFSIZ];
1604
1605 if (menuMapSize >= menuMapMax)
1606 {
1607 newMap = realloc(menuMap,
1608 sizeof(MenuMap) * (menuMapMax + MENU_INC));
1609 if (newMap != NULL)
1610 {
1611 menuMap = newMap;
1612 menuMapMax += MENU_INC;
1613 }
1614 }
1615 if (menuMapSize < menuMapMax)
1616 {
1617 menuMap[menuMapSize].name = strdup(menu);
1618 menuMap[menuMapSize].accel = accel && *accel ? strdup(accel) : NULL;
1619 menuMap[menuMapSize++].verb = strdup(verb);
1620 if (accel && workshopHotKeysEnabled)
1621 {
Bram Moolenaar051b7822005-05-19 21:00:46 +00001622 vim_snprintf(cbuf, sizeof(cbuf),
1623 "map %s :wsverb %s<CR>", accel, verb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 coloncmd(cbuf, TRUE);
1625 }
1626 }
1627}
1628
1629 static char *
1630nameStrip(
1631 char *raw) /* menu name, possibly with & chars */
1632{
1633 static char buf[BUFSIZ]; /* build stripped name here */
1634 char *bp = buf;
1635
1636 while (*raw)
1637 {
1638 if (*raw != '&')
1639 *bp++ = *raw;
1640 raw++;
1641 }
1642 *bp = NUL;
1643 return buf;
1644}
1645
1646
1647 static char *
1648lookupVerb(
1649 char *verb,
1650 int skip) /* number of matches to skip */
1651{
1652 int i; /* loop iterator */
1653
1654 for (i = 0; i < menuMapSize; i++)
1655 if (strcmp(menuMap[i].verb, verb) == 0 && skip-- == 0)
1656 return nameStrip(menuMap[i].name);
1657
1658 return NULL;
1659}
1660
1661
1662 static void
1663coloncmd(
1664 char *cmd, /* the command to print */
1665 Boolean force) /* force cursor update */
1666{
1667 char_u *cpo_save = p_cpo;
1668
1669#ifdef WSDEBUG
1670 if (WSDLEVEL(WS_TRACE_COLONCMD))
1671 wsdebug("Cmd: %s\n", cmd);
1672#endif
1673
1674 p_cpo = empty_option;
1675
1676 ALT_INPUT_LOCK_ON;
1677 do_cmdline_cmd((char_u *)cmd);
1678 ALT_INPUT_LOCK_OFF;
1679
1680 p_cpo = cpo_save;
1681
1682 if (force)
1683 gui_update_screen();
1684}
1685
1686/*
1687 * setDollarVim - Given the run directory, search for the vim install
1688 * directory and set $VIM.
1689 *
1690 * We can be running out of SUNWspro/bin or out of
1691 * SUNWspro/contrib/contrib6/vim5.6/bin so we check
1692 * relative to both of these directories.
1693 */
1694 static void
1695setDollarVim(
1696 char *rundir)
1697{
1698 char buf[MAXPATHLEN];
1699 char *cp;
1700
1701 /*
1702 * First case: Running from <install-dir>/SUNWspro/bin
1703 */
1704 strcpy(buf, rundir);
1705 strcat(buf, "/../contrib/contrib6/vim" VIM_VERSION_SHORT "/share/vim/"
1706 VIM_VERSION_NODOT "/syntax/syntax.vim");
1707 if (access(buf, R_OK) == 0)
1708 {
1709 strcpy(buf, "SPRO_WSDIR=");
1710 strcat(buf, rundir);
1711 cp = strrchr(buf, '/');
1712 if (cp != NULL)
1713 strcpy(cp, "/WS6U2");
1714 putenv(strdup(buf));
1715
1716 strcpy(buf, "VIM=");
1717 strcat(buf, rundir);
1718 strcat(buf, "/../contrib/contrib6/vim" VIM_VERSION_SHORT "/share/vim/"
1719 VIM_VERSION_NODOT);
1720 putenv(strdup(buf));
1721 return;
1722 }
1723
1724 /*
1725 * Second case: Probably running from
1726 * <install-dir>/SUNWspro/contrib/contrib6/vim5.6/bin
1727 */
1728 strcpy(buf, rundir);
1729 strcat(buf, "/../../../contrib/contrib6/vim" VIM_VERSION_SHORT
1730 "/share/vim/" VIM_VERSION_NODOT "/syntax/syntax.vim");
1731 if (access(buf, R_OK) == 0)
1732 {
1733 strcpy(buf, "SPRO_WSDIR=");
1734 strcat(buf, rundir);
1735 cp = strrchr(buf, '/');
1736 if (cp != NULL)
1737 strcpy(cp, "../../../../WS6U2");
1738 putenv(strdup(buf));
1739
1740 strcpy(buf, "VIM=");
1741 strcat(buf, rundir);
1742 strcat(buf, "/../../../contrib/contrib6/vim" VIM_VERSION_SHORT
1743 "/share/vim/" VIM_VERSION_NODOT);
1744 putenv(strdup(buf));
1745 return;
1746 }
1747}
1748
1749/*
1750 * findYourself - Find the directory we are running from. This is used to
1751 * set $VIM. We need to set this because users can install
1752 * the package in a different directory than the compiled
1753 * directory. This is a Sun Visual WorkShop requirement!
1754 *
1755 * Note: We override a user's $VIM because it won't have the
1756 * WorkShop specific files. S/he may not like this but its
1757 * better than getting the wrong files (especially as the
1758 * user is likely to have $VIM set to 5.4 or later).
1759 */
1760 void
1761findYourself(
1762 char *argv0)
1763{
1764 char *runpath = NULL;
1765 char *path;
1766 char *pathbuf;
1767
1768 if (*argv0 == '/')
1769 runpath = strdup(argv0);
1770 else if (*argv0 == '.' || strchr(argv0, '/'))
1771 {
1772 runpath = (char *) malloc(MAXPATHLEN);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001773 if (getcwd(runpath, MAXPATHLEN) == NULL)
1774 runpath[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 strcat(runpath, "/");
1776 strcat(runpath, argv0);
1777 }
1778 else
1779 {
1780 path = getenv("PATH");
1781 if (path != NULL)
1782 {
1783 runpath = (char *) malloc(MAXPATHLEN);
1784 pathbuf = strdup(path);
1785 path = strtok(pathbuf, ":");
1786 do
1787 {
1788 strcpy(runpath, path);
1789 strcat(runpath, "/");
1790 strcat(runpath, argv0);
1791 if (access(runpath, X_OK) == 0)
1792 break;
1793 } while ((path = strtok(NULL, ":")) != NULL);
1794 free(pathbuf);
1795 }
1796 }
1797
1798 if (runpath != NULL)
1799 {
1800 char runbuf[MAXPATHLEN];
1801
1802 /*
1803 * We found the run directory. Now find the install dir.
1804 */
1805 (void)vim_FullName((char_u *)runpath, (char_u *)runbuf, MAXPATHLEN, 1);
1806 path = strrchr(runbuf, '/');
1807 if (path != NULL)
1808 *path = NUL; /* remove the vim/gvim name */
1809 path = strrchr(runbuf, '/');
1810 if (path != NULL)
1811 {
1812 if (strncmp(path, "/bin", 4) == 0)
1813 setDollarVim(runbuf);
1814 else if (strncmp(path, "/src", 4) == 0)
1815 {
1816 *path = NUL; /* development tree */
1817 setDollarVim(runbuf);
1818 }
1819 }
1820 free(runpath);
1821 }
1822}