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