blob: 66a60d3ba8b2f21e0039a8cb0df6b5f380f54a8d [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
1124 strcpy(ffname, (char *) curbuf->b_ffname);
1125 *filename = ffname; /* copy so nobody can change b_ffname */
1126 *curLine = curwin->w_cursor.lnum;
1127 *curCol = curwin->w_cursor.col;
1128
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001129 if (curbuf->b_visual.vi_mode == 'v' &&
1130 equalpos(curwin->w_cursor, curbuf->b_visual.vi_end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001132 *selStartLine = curbuf->b_visual.vi_start.lnum;
1133 *selStartCol = curbuf->b_visual.vi_start.col;
1134 *selEndLine = curbuf->b_visual.vi_end.lnum;
1135 *selEndCol = curbuf->b_visual.vi_end.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 *selection = get_selection(curbuf);
1137 if (*selection)
1138 *selLength = strlen(*selection);
1139 else
1140 *selLength = 0;
1141 }
1142 else
1143 {
1144 *selStartLine = *selEndLine = -1;
1145 *selStartCol = *selEndCol = -1;
1146 *selLength = 0;
1147 *selection = "";
1148 }
1149
1150 return True;
1151}
1152
1153
1154
1155/************************************************************************
1156 * Utility functions
1157 ************************************************************************/
1158
1159 static char *
1160get_selection(
1161 buf_T *buf) /* buffer whose selection we want */
1162{
1163 pos_T *start; /* start of the selection */
1164 pos_T *end; /* end of the selection */
1165 char *lp; /* pointer to actual line data */
1166 int llen; /* length of actual line data */
1167 char *sp; /* pointer to selection buffer */
1168 int slen; /* string length in selection buffer */
1169 int size; /* size of selection buffer */
1170 char *new_sp; /* temp pointer to new sp */
1171 int lnum; /* line number we are appending */
1172
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001173 if (buf->b_visual.vi_mode == 'v')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001175 start = &buf->b_visual.vi_start;
1176 end = &buf->b_visual.vi_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 if (start->lnum == end->lnum)
1178 {
1179 /* selection is all on one line */
1180 lp = (char *) ml_get_pos(start);
1181 llen = end->col - start->col + 1;
1182 sp = (char *) malloc(llen + 1);
1183 if (sp != NULL)
1184 {
1185 strncpy(sp, lp, llen);
1186 sp[llen] = NUL;
1187 }
1188 }
1189 else
1190 {
1191 /* multi-line selection */
1192 lp = (char *) ml_get_pos(start);
1193 llen = strlen(lp);
1194 sp = (char *) malloc(BUFSIZ + llen);
1195 if (sp != NULL)
1196 {
1197 size = BUFSIZ + llen;
1198 strcpy(sp, lp);
1199 sp[llen] = '\n';
1200 slen = llen + 1;
1201
1202 lnum = start->lnum + 1;
1203 while (lnum < end->lnum)
1204 sp = append_selection(lnum++, sp, &size, &slen);
1205
1206 lp = (char *) ml_get(end->lnum);
1207 llen = end->col + 1;
1208 if ((slen + llen) >= size)
1209 {
1210 new_sp = (char *)
1211 realloc(sp, slen + llen + 1);
1212 if (new_sp != NULL)
1213 {
1214 size += llen + 1;
1215 sp = new_sp;
1216 }
1217 }
1218 if ((slen + llen) < size)
1219 {
1220 strncpy(&sp[slen], lp, llen);
1221 sp[slen + llen] = NUL;
1222 }
1223
1224 }
1225 }
1226 }
1227 else
1228 sp = NULL;
1229
1230 return sp;
1231}
1232
1233 static char *
1234append_selection(
1235 int lnum, /* line number to append */
1236 char *sp, /* pointer to selection buffer */
1237 int *size, /* ptr to size of sp */
1238 int *slen) /* ptr to length of selection string */
1239{
1240 char *lp; /* line of data from buffer */
1241 int llen; /* strlen of lp */
1242 char *new_sp; /* temp pointer to new sp */
1243
1244 lp = (char *)ml_get((linenr_T)lnum);
1245 llen = strlen(lp);
1246
1247 if ((*slen + llen) <= *size)
1248 {
1249 new_sp = (char *) realloc((void *) sp, BUFSIZ + *slen + llen);
1250 if (*new_sp != NUL)
1251 {
1252 *size = BUFSIZ + *slen + llen;
1253 sp = new_sp;
1254 }
1255 }
1256 if ((*slen + llen) > *size)
1257 {
1258 strcat(&sp[*slen], lp);
1259 *slen += llen;
1260 sp[*slen++] = '\n';
1261 }
1262
1263 return sp;
1264}
1265
1266
1267
1268 static void
1269load_buffer_by_name(
1270 char *filename, /* the file to load */
1271 int lnum) /* an optional line number (or 0) */
1272{
1273 char lnumbuf[16]; /* make line number option for :e */
1274 char cbuf[BUFSIZ]; /* command buffer */
1275
1276 if (lnum > 0)
1277 sprintf(lnumbuf, "+%d", lnum);
1278 else
1279 lnumbuf[0] = NUL;
1280
Bram Moolenaar051b7822005-05-19 21:00:46 +00001281 vim_snprintf(cbuf, sizeof(cbuf), "e %s %s", lnumbuf, filename);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 coloncmd(cbuf, False);
1283}
1284
1285
1286 static void
1287load_window(
1288 char *filename, /* filename to load */
1289 int lnum) /* linenumber to go to */
1290{
1291 buf_T *buf; /* buffer filename is stored in */
1292 win_T *win; /* window filenme is displayed in */
1293
1294 /*
1295 * Make sure filename is displayed and is the current window.
1296 */
1297
1298 buf = buflist_findname((char_u *)filename);
1299 if (buf == NULL || (win = get_window(buf)) == NULL)
1300 {
1301 /* No buffer or buffer is not in current window */
1302 /* wsdebug("load_window: load_buffer_by_name(\"%s\", %d)\n",
1303 filename, lnum); */
1304 load_buffer_by_name(filename, lnum);
1305 }
1306 else
1307 {
1308 /* buf is in a window */
1309 if (win != curwin)
1310 {
1311 win_enter(win, False);
1312 /* wsdebug("load_window: window endter %s\n",
1313 win->w_buffer->b_sfname); */
1314 }
1315 if (lnum > 0 && win->w_cursor.lnum != lnum)
1316 {
1317 warp_to_pc(lnum);
1318 /* wsdebug("load_window: warp to %s[%d]\n",
1319 win->w_buffer->b_sfname, lnum); */
1320 }
1321 }
1322 out_flush();
1323}
1324
1325
1326
1327 static void
1328warp_to_pc(
1329 int lnum) /* line number to warp to */
1330{
Bram Moolenaar720c7102007-05-10 18:07:50 +00001331 char lbuf[256]; /* build line command here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332
1333 if (lnum > 0)
1334 {
1335 if (State & INSERT)
1336 add_to_input_buf((char_u *) "\033", 1);
1337 if (isShowing(lnum))
1338 sprintf(lbuf, "%dG", lnum);
1339 else
1340 sprintf(lbuf, "%dz.", lnum);
1341 add_to_input_buf((char_u *) lbuf, strlen(lbuf));
1342 }
1343}
1344
1345 static Boolean
1346isShowing(
1347 int lnum) /* tell if line number is showing */
1348{
1349 return lnum >= curwin->w_topline && lnum < curwin->w_botline;
1350}
1351
1352
1353
1354 static win_T *
1355get_window(
1356 buf_T *buf) /* buffer to find window for */
1357{
1358 win_T *wp = NULL; /* window filename is in */
1359
1360 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
1361 if (buf == wp->w_buffer)
1362 break;
1363 return wp;
1364}
1365
1366
1367#if 0 /* not used */
1368 static int
1369get_buffer_number(
1370 buf_T *buf) /* buffer to get position of */
1371{
1372 buf_T *bp; /* iterate over buffer list */
1373 int pos; /* the position in the buffer list */
1374
1375 pos = 1;
1376 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
1377 {
1378 if (bp == buf)
1379 return pos;
1380 pos++;
1381 }
1382
1383 return 1;
1384}
1385#endif
1386
1387 static void
1388updatePriority(
1389 Boolean subMenu) /* if True then start new submenu pri */
1390{
1391 int pri; /* priority of this menu/item */
1392 char *p;
1393
1394 p = strrchr(curMenuPriority, '.');
1395 ASSERT(p != NULL);
1396 *p++ = NUL;
1397
1398 pri = atoi(p) + 10; /* our new priority */
1399
1400 if (subMenu)
Bram Moolenaar051b7822005-05-19 21:00:46 +00001401 vim_snprintf(curMenuPriority, sizeof(curMenuPriority),
1402 "%s.%d.0", curMenuPriority, pri);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 else
Bram Moolenaar051b7822005-05-19 21:00:46 +00001404 vim_snprintf(curMenuPriority, sizeof(curMenuPriority),
1405 "%s.%d", curMenuPriority, pri);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406}
1407
1408 static char *
1409addUniqueMnemonic(
1410 char *mnemonics, /* currently used mnemonics */
1411 char *label) /* label of menu needing mnemonic */
1412{
1413 static char name[BUFSIZ]; /* buffer for the updated name */
1414 char *p; /* pointer into label */
1415 char *found; /* pointer to possible mnemonic */
1416
1417 found = NULL;
1418 for (p = label; *p != NUL; p++)
1419 if (strchr(mnemonics, *p) == 0)
1420 if (found == NULL || (isupper((int)*p) && islower((int)*found)))
1421 found = p;
1422
1423 if (found != NULL)
1424 {
1425 strncpy(name, label, (found - label));
1426 strcat(name, "&");
1427 strcat(name, found);
1428 }
1429 else
1430 strcpy(name, label);
1431
1432 return name;
1433}
1434
1435/*
1436 * Some characters in a menu name must be escaped in vim. Since this is vim
1437 * specific, it must be done on this side.
1438 */
1439 static char *
1440fixup(
1441 char *label)
1442{
1443 static char buf[BUFSIZ];
1444 char *bp; /* pointer into buf */
1445 char *lp; /* pointer into label */
1446
1447 lp = label;
1448 bp = buf;
1449 while (*lp != NUL)
1450 {
1451 if (*lp == ' ' || *lp == '.')
1452 *bp++ = '\\';
1453 *bp++ = *lp++;
1454 }
1455 *bp = NUL;
1456
1457 return buf;
1458}
1459
1460
1461#ifdef NOHANDS_SUPPORT_FUNCTIONS
1462
1463/* For the NoHands test suite */
1464
1465 char *
1466workshop_test_getcurrentfile()
1467{
1468 char *filename, *selection;
1469 int curLine, curCol, selStartLine, selStartCol, selEndLine;
1470 int selEndCol, selLength;
1471
1472 if (workshop_get_positions(
1473 NULL, &filename, &curLine, &curCol, &selStartLine,
1474 &selStartCol, &selEndLine, &selEndCol, &selLength,
1475 &selection))
1476 return filename;
1477 else
1478 return NULL;
1479}
1480
1481 int
1482workshop_test_getcursorrow()
1483{
1484 return 0;
1485}
1486
1487 int
1488workshop_test_getcursorcol()
1489{
1490 char *filename, *selection;
1491 int curLine, curCol, selStartLine, selStartCol, selEndLine;
1492 int selEndCol, selLength;
1493
1494 if (workshop_get_positions(
1495 NULL, &filename, &curLine, &curCol, &selStartLine,
1496 &selStartCol, &selEndLine, &selEndCol, &selLength,
1497 &selection))
1498 return curCol;
1499 else
1500 return -1;
1501}
1502
1503 char *
1504workshop_test_getcursorrowtext()
1505{
1506 return NULL;
1507}
1508
1509 char *
1510workshop_test_getselectedtext()
1511{
1512 char *filename, *selection;
1513 int curLine, curCol, selStartLine, selStartCol, selEndLine;
1514 int selEndCol, selLength;
1515
1516 if (workshop_get_positions(
1517 NULL, &filename, &curLine, &curCol, &selStartLine,
1518 &selStartCol, &selEndLine, &selEndCol, &selLength,
1519 &selection))
1520 return selection;
1521 else
1522 return NULL;
1523}
1524
1525/*ARGSUSED*/
1526 void
1527workshop_save_sensitivity(char *filename)
1528{
1529}
1530
1531#endif
1532
1533 static char *
1534fixAccelText(
1535 char *ap) /* original acceleratorText */
1536{
1537 char buf[256]; /* build in temp buffer */
1538 char *shift; /* shift string of "" */
1539
1540 if (ap == NULL)
1541 return NULL;
1542
1543 /* If the accelerator is shifted use the vim form */
1544 if (strncmp("Shift+", ap, 6) == 0)
1545 {
1546 shift = "S-";
1547 ap += 6;
1548 }
1549 else
1550 shift = "";
1551
1552 if (*ap == 'F' && atoi(&ap[1]) > 0)
1553 {
Bram Moolenaar051b7822005-05-19 21:00:46 +00001554 vim_snprintf(buf, sizeof(buf), "<%s%s>", shift, ap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 return strdup(buf);
1556 }
1557 else
1558 return NULL;
1559}
1560
1561#ifdef FEAT_BEVAL
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00001562 void
1563workshop_beval_cb(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 BalloonEval *beval,
1565 int state)
1566{
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00001567 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 char_u *text;
1569 int type;
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00001570 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 int col;
1572 int idx;
1573 char buf[MAXPATHLEN * 2];
1574 static int serialNo = -1;
1575
1576 if (!p_beval)
1577 return;
1578
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00001579 if (get_beval_info(beval, FALSE, &wp, &lnum, &text, &col) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 {
1581 if (text && text[0])
1582 {
1583 /* Send debugger request */
1584 if (strlen((char *) text) > (MAXPATHLEN/2))
1585 {
1586 /*
1587 * The user has probably selected the entire
1588 * buffer or something like that - don't attempt
1589 * to evaluate it
1590 */
1591 return;
1592 }
1593
1594 /*
1595 * WorkShop expects the col to be a character index, not
1596 * a column number. Compute the index from col. Also set
1597 * line to 0 because thats what dbx expects.
1598 */
1599 idx = computeIndex(col, text, beval->ts);
1600 if (idx > 0)
1601 {
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00001602 lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603
1604 /*
1605 * If successful, it will respond with a balloon cmd.
1606 */
1607 if (state & ControlMask)
1608 /* Evaluate *(expression) */
1609 type = (int)GPLineEval_INDIRECT;
1610 else if (state & ShiftMask)
1611 /* Evaluate type(expression) */
1612 type = (int)GPLineEval_TYPE;
1613 else
1614 /* Evaluate value(expression) */
1615 type = (int)GPLineEval_EVALUATE;
1616
1617 /* Send request to dbx */
Bram Moolenaar051b7822005-05-19 21:00:46 +00001618 vim_snprintf(buf, sizeof(buf), "toolVerb debug.balloonEval "
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00001619 "%s %ld,0 %d,0 %d,%d %ld %s\n",
1620 (char *)wp->w_buffer->b_ffname,
1621 (long)lnum, idx, type, serialNo++,
1622 (long)strlen((char *)text), (char *)text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 balloonEval = beval;
1624 workshop_send_message(buf);
1625 }
1626 }
1627 }
1628}
1629#endif
1630
1631
1632 static int
1633computeIndex(
1634 int wantedCol,
1635 char_u *line,
1636 int ts)
1637{
1638 int col = 0;
1639 int idx = 0;
1640
1641 while (line[idx])
1642 {
1643 if (line[idx] == '\t')
1644 col += ts - (col % ts);
1645 else
1646 col++;
1647 idx++;
1648 if (col >= wantedCol)
1649 return idx;
1650 }
1651
1652 return -1;
1653}
1654
1655 static void
1656addMenu(
1657 char *menu, /* menu name */
1658 char *accel, /* accelerator text (optional) */
1659 char *verb) /* WorkShop action-verb */
1660{
1661 MenuMap *newMap;
1662 char cbuf[BUFSIZ];
1663
1664 if (menuMapSize >= menuMapMax)
1665 {
1666 newMap = realloc(menuMap,
1667 sizeof(MenuMap) * (menuMapMax + MENU_INC));
1668 if (newMap != NULL)
1669 {
1670 menuMap = newMap;
1671 menuMapMax += MENU_INC;
1672 }
1673 }
1674 if (menuMapSize < menuMapMax)
1675 {
1676 menuMap[menuMapSize].name = strdup(menu);
1677 menuMap[menuMapSize].accel = accel && *accel ? strdup(accel) : NULL;
1678 menuMap[menuMapSize++].verb = strdup(verb);
1679 if (accel && workshopHotKeysEnabled)
1680 {
Bram Moolenaar051b7822005-05-19 21:00:46 +00001681 vim_snprintf(cbuf, sizeof(cbuf),
1682 "map %s :wsverb %s<CR>", accel, verb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 coloncmd(cbuf, TRUE);
1684 }
1685 }
1686}
1687
1688 static char *
1689nameStrip(
1690 char *raw) /* menu name, possibly with & chars */
1691{
1692 static char buf[BUFSIZ]; /* build stripped name here */
1693 char *bp = buf;
1694
1695 while (*raw)
1696 {
1697 if (*raw != '&')
1698 *bp++ = *raw;
1699 raw++;
1700 }
1701 *bp = NUL;
1702 return buf;
1703}
1704
1705
1706 static char *
1707lookupVerb(
1708 char *verb,
1709 int skip) /* number of matches to skip */
1710{
1711 int i; /* loop iterator */
1712
1713 for (i = 0; i < menuMapSize; i++)
1714 if (strcmp(menuMap[i].verb, verb) == 0 && skip-- == 0)
1715 return nameStrip(menuMap[i].name);
1716
1717 return NULL;
1718}
1719
1720
1721 static void
1722coloncmd(
1723 char *cmd, /* the command to print */
1724 Boolean force) /* force cursor update */
1725{
1726 char_u *cpo_save = p_cpo;
1727
1728#ifdef WSDEBUG
1729 if (WSDLEVEL(WS_TRACE_COLONCMD))
1730 wsdebug("Cmd: %s\n", cmd);
1731#endif
1732
1733 p_cpo = empty_option;
1734
1735 ALT_INPUT_LOCK_ON;
1736 do_cmdline_cmd((char_u *)cmd);
1737 ALT_INPUT_LOCK_OFF;
1738
1739 p_cpo = cpo_save;
1740
1741 if (force)
1742 gui_update_screen();
1743}
1744
1745/*
1746 * setDollarVim - Given the run directory, search for the vim install
1747 * directory and set $VIM.
1748 *
1749 * We can be running out of SUNWspro/bin or out of
1750 * SUNWspro/contrib/contrib6/vim5.6/bin so we check
1751 * relative to both of these directories.
1752 */
1753 static void
1754setDollarVim(
1755 char *rundir)
1756{
1757 char buf[MAXPATHLEN];
1758 char *cp;
1759
1760 /*
1761 * First case: Running from <install-dir>/SUNWspro/bin
1762 */
1763 strcpy(buf, rundir);
1764 strcat(buf, "/../contrib/contrib6/vim" VIM_VERSION_SHORT "/share/vim/"
1765 VIM_VERSION_NODOT "/syntax/syntax.vim");
1766 if (access(buf, R_OK) == 0)
1767 {
1768 strcpy(buf, "SPRO_WSDIR=");
1769 strcat(buf, rundir);
1770 cp = strrchr(buf, '/');
1771 if (cp != NULL)
1772 strcpy(cp, "/WS6U2");
1773 putenv(strdup(buf));
1774
1775 strcpy(buf, "VIM=");
1776 strcat(buf, rundir);
1777 strcat(buf, "/../contrib/contrib6/vim" VIM_VERSION_SHORT "/share/vim/"
1778 VIM_VERSION_NODOT);
1779 putenv(strdup(buf));
1780 return;
1781 }
1782
1783 /*
1784 * Second case: Probably running from
1785 * <install-dir>/SUNWspro/contrib/contrib6/vim5.6/bin
1786 */
1787 strcpy(buf, rundir);
1788 strcat(buf, "/../../../contrib/contrib6/vim" VIM_VERSION_SHORT
1789 "/share/vim/" VIM_VERSION_NODOT "/syntax/syntax.vim");
1790 if (access(buf, R_OK) == 0)
1791 {
1792 strcpy(buf, "SPRO_WSDIR=");
1793 strcat(buf, rundir);
1794 cp = strrchr(buf, '/');
1795 if (cp != NULL)
1796 strcpy(cp, "../../../../WS6U2");
1797 putenv(strdup(buf));
1798
1799 strcpy(buf, "VIM=");
1800 strcat(buf, rundir);
1801 strcat(buf, "/../../../contrib/contrib6/vim" VIM_VERSION_SHORT
1802 "/share/vim/" VIM_VERSION_NODOT);
1803 putenv(strdup(buf));
1804 return;
1805 }
1806}
1807
1808/*
1809 * findYourself - Find the directory we are running from. This is used to
1810 * set $VIM. We need to set this because users can install
1811 * the package in a different directory than the compiled
1812 * directory. This is a Sun Visual WorkShop requirement!
1813 *
1814 * Note: We override a user's $VIM because it won't have the
1815 * WorkShop specific files. S/he may not like this but its
1816 * better than getting the wrong files (especially as the
1817 * user is likely to have $VIM set to 5.4 or later).
1818 */
1819 void
1820findYourself(
1821 char *argv0)
1822{
1823 char *runpath = NULL;
1824 char *path;
1825 char *pathbuf;
1826
1827 if (*argv0 == '/')
1828 runpath = strdup(argv0);
1829 else if (*argv0 == '.' || strchr(argv0, '/'))
1830 {
1831 runpath = (char *) malloc(MAXPATHLEN);
1832 getcwd(runpath, MAXPATHLEN);
1833 strcat(runpath, "/");
1834 strcat(runpath, argv0);
1835 }
1836 else
1837 {
1838 path = getenv("PATH");
1839 if (path != NULL)
1840 {
1841 runpath = (char *) malloc(MAXPATHLEN);
1842 pathbuf = strdup(path);
1843 path = strtok(pathbuf, ":");
1844 do
1845 {
1846 strcpy(runpath, path);
1847 strcat(runpath, "/");
1848 strcat(runpath, argv0);
1849 if (access(runpath, X_OK) == 0)
1850 break;
1851 } while ((path = strtok(NULL, ":")) != NULL);
1852 free(pathbuf);
1853 }
1854 }
1855
1856 if (runpath != NULL)
1857 {
1858 char runbuf[MAXPATHLEN];
1859
1860 /*
1861 * We found the run directory. Now find the install dir.
1862 */
1863 (void)vim_FullName((char_u *)runpath, (char_u *)runbuf, MAXPATHLEN, 1);
1864 path = strrchr(runbuf, '/');
1865 if (path != NULL)
1866 *path = NUL; /* remove the vim/gvim name */
1867 path = strrchr(runbuf, '/');
1868 if (path != NULL)
1869 {
1870 if (strncmp(path, "/bin", 4) == 0)
1871 setDollarVim(runbuf);
1872 else if (strncmp(path, "/src", 4) == 0)
1873 {
1874 *path = NUL; /* development tree */
1875 setDollarVim(runbuf);
1876 }
1877 }
1878 free(runpath);
1879 }
1880}