blob: 334e6afff8d6e2ea739a6df9040c8845258e0bd9 [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 Moolenaarba07ce32010-01-06 18:25:34 +010059void workshop_beval_cb(BalloonEval *, int);
60static int computeIndex(int, char_u *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000061#endif
62static char *fixAccelText(char *);
63static void addMenu(char *, char *, char *);
64static char *lookupVerb(char *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000065static 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 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207 void
208workshop_load_file(
209 char *filename, /* the file to load */
210 int line, /* an optional line number (or 0) */
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +0000211 char *frameid UNUSED) /* used for multi-frame support */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212{
213#ifdef WSDEBUG_TRACE
214 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
215 wstrace("workshop_load_file(%s, %d)\n", filename, line);
216#endif
217
218#ifdef FEAT_BEVAL
Bram Moolenaare4efc3b2005-03-07 23:16:51 +0000219 bevalServers |= BEVAL_WORKSHOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220#endif
221
222 load_window(filename, line);
223}
224
225/*
226 * Reload the WorkShop buffer
227 */
228 void
229workshop_reload_file(
230 char *filename,
231 int line)
232{
233#ifdef WSDEBUG_TRACE
234 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
235 wstrace("workshop_reload_file(%s, %d)\n", filename, line);
236#endif
237 load_window(filename, line);
238}
239
240 void
241workshop_show_file(
242 char *filename)
243{
244#ifdef WSDEBUG_TRACE
245 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
246 wstrace("workshop_show_file(%s)\n", filename);
247#endif
248
249 load_window(filename, 0);
250}
251
252 void
253workshop_goto_line(
254 char *filename,
255 int lineno)
256{
257#ifdef WSDEBUG_TRACE
258 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
259 wstrace("workshop_goto_line(%s, %d)\n", filename, lineno);
260#endif
261
262 load_window(filename, lineno);
263}
264
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265 void
266workshop_front_file(
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +0000267 char *filename UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268{
269#ifdef WSDEBUG_TRACE
270 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
271 wstrace("workshop_front_file()\n");
272#endif
273 /*
274 * Assumption: This function will always be called after a call to
275 * workshop_show_file(), so the file is always showing.
276 */
277 if (vimShell != NULL)
278 XRaiseWindow(gui.dpy, XtWindow(vimShell));
279}
280
281 void
282workshop_save_file(
283 char *filename)
284{
285 char cbuf[BUFSIZ]; /* build vim command here */
286
287#ifdef WSDEBUG_TRACE
288 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
289 wstrace("workshop_save_file(%s)\n", filename);
290#endif
291
292 /* Save the given file */
Bram Moolenaar051b7822005-05-19 21:00:46 +0000293 vim_snprintf(cbuf, sizeof(cbuf), "w %s", filename);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294 coloncmd(cbuf, TRUE);
295}
296
297 void
298workshop_save_files()
299{
300 /* Save the given file */
301#ifdef WSDEBUG_TRACE
302 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
303 wstrace("workshop_save_files()\n");
304#endif
305
306 add_to_input_buf((char_u *) ":wall\n", 6);
307}
308
309 void
310workshop_quit()
311{
312#ifdef WSDEBUG_TRACE
313 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
314 wstrace("workshop_quit()\n");
315#endif
316
317 add_to_input_buf((char_u *) ":qall\n", 6);
318}
319
320 void
321workshop_minimize()
322{
323#ifdef WSDEBUG_TRACE
324 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
325 wstrace("workshop_minimize()\n");
326#endif
327 workshop_minimize_shell(vimShell);
328}
329 void
330workshop_maximize()
331{
332#ifdef WSDEBUG_TRACE
333 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
334 wstrace("workshop_maximize()\n");
335#endif
336
337 workshop_maximize_shell(vimShell);
338}
339
340 void
341workshop_add_mark_type(
342 int idx,
343 char *colorspec,
344 char *sign)
345{
346 char gbuf[BUFSIZ]; /* buffer for sign name */
347 char cibuf[BUFSIZ]; /* color information */
348 char cbuf[BUFSIZ]; /* command buffer */
349 char *bp;
350
351#ifdef WSDEBUG_TRACE
352 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
353 {
354 char *cp;
355
356 cp = strrchr(sign, '/');
357 if (cp == NULL)
358 cp = sign;
359 else
360 cp++; /* skip '/' character */
361 wstrace("workshop_add_mark_type(%d, \"%s\", \"%s\")\n", idx,
362 colorspec && *colorspec ? colorspec : "<None>", cp);
363 }
364#endif
365
366 /*
367 * Isolate the basename of sign in gbuf. We will use this for the
368 * GroupName in the highlight command sent to vim.
369 */
370 STRCPY(gbuf, gettail((char_u *)sign));
371 bp = strrchr(gbuf, '.');
372 if (bp != NULL)
373 *bp = NUL;
374
375 if (gbuf[0] != '-' && gbuf[1] != NUL)
376 {
377 if (colorspec != NULL && *colorspec)
378 {
Bram Moolenaar051b7822005-05-19 21:00:46 +0000379 vim_snprintf(cbuf, sizeof(cbuf),
380 "highlight WS%s guibg=%s", gbuf, colorspec);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 coloncmd(cbuf, FALSE);
Bram Moolenaar051b7822005-05-19 21:00:46 +0000382 vim_snprintf(cibuf, sizeof(cibuf), "linehl=WS%s", gbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 }
384 else
385 cibuf[0] = NUL;
386
Bram Moolenaar051b7822005-05-19 21:00:46 +0000387 vim_snprintf(cbuf, sizeof(cbuf),
388 "sign define %d %s icon=%s", idx, cibuf, sign);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389 coloncmd(cbuf, TRUE);
390 }
391}
392
393 void
394workshop_set_mark(
395 char *filename, /* filename which gets the mark */
396 int lineno, /* line number which gets the mark */
397 int markId, /* unique mark identifier */
398 int idx) /* which mark to use */
399{
400 char cbuf[BUFSIZ]; /* command buffer */
401
402 /* Set mark in a given file */
403#ifdef WSDEBUG_TRACE
404 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
405 wstrace("workshop_set_mark(%s, %d (ln), %d (id), %d (idx))\n",
406 filename, lineno, markId, idx);
407#endif
408
Bram Moolenaar051b7822005-05-19 21:00:46 +0000409 vim_snprintf(cbuf, sizeof(cbuf), "sign place %d line=%d name=%d file=%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410 markId, lineno, idx, filename);
411 coloncmd(cbuf, TRUE);
412}
413
414 void
415workshop_change_mark_type(
416 char *filename, /* filename which gets the mark */
417 int markId, /* unique mark identifier */
418 int idx) /* which mark to use */
419{
420 char cbuf[BUFSIZ]; /* command buffer */
421
422 /* Change mark type */
423#ifdef WSDEBUG_TRACE
424 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
425 wstrace("workshop_change_mark_type(%s, %d, %d)\n",
426 filename, markId, idx);
427#endif
428
Bram Moolenaar051b7822005-05-19 21:00:46 +0000429 vim_snprintf(cbuf, sizeof(cbuf),
430 "sign place %d name=%d file=%s", markId, idx, filename);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431 coloncmd(cbuf, TRUE);
432}
433
434/*
435 * Goto the given mark in a file (e.g. show it).
436 * If message is not null, display it in the footer.
437 */
438 void
439workshop_goto_mark(
440 char *filename,
441 int markId,
442 char *message)
443{
444 char cbuf[BUFSIZ]; /* command buffer */
445
446 /* Goto mark */
447#ifdef WSDEBUG_TRACE
448 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
449 wstrace("workshop_goto_mark(%s, %d (id), %s)\n",
450 filename, markId, message && *message &&
451 !(*message == ' ' && message[1] == NULL) ?
452 message : "<None>");
453#endif
454
Bram Moolenaar051b7822005-05-19 21:00:46 +0000455 vim_snprintf(cbuf, sizeof(cbuf), "sign jump %d file=%s", markId, filename);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456 coloncmd(cbuf, TRUE);
457 if (message != NULL && *message != NUL)
458 gui_mch_set_footer((char_u *)message);
459}
460
461 void
462workshop_delete_mark(
463 char *filename,
464 int markId)
465{
466 char cbuf[BUFSIZ]; /* command buffer */
467
468 /* Delete mark */
469#ifdef WSDEBUG_TRACE
470 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
471 wstrace("workshop_delete_mark(%s, %d (id))\n",
472 filename, markId);
473#endif
474
Bram Moolenaar051b7822005-05-19 21:00:46 +0000475 vim_snprintf(cbuf, sizeof(cbuf),
476 "sign unplace %d file=%s", markId, filename);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 coloncmd(cbuf, TRUE);
478}
479
480#if 0 /* not used */
481 void
482workshop_delete_all_marks(
483 void *window,
484 Boolean doRefresh)
485{
486#ifdef WSDEBUG_TRACE
487 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
488 wstrace("workshop_delete_all_marks(%#x, %s)\n",
489 window, doRefresh ? "True" : "False");
490#endif
491
492 coloncmd("sign unplace *", TRUE);
493}
494#endif
495
496 int
497workshop_get_mark_lineno(
498 char *filename,
499 int markId)
500{
501 buf_T *buf; /* buffer containing filename */
502 int lineno; /* line number of filename in buf */
503
504 /* Get mark line number */
505#ifdef WSDEBUG_TRACE
506 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
507 wstrace("workshop_get_mark_lineno(%s, %d)\n",
508 filename, markId);
509#endif
510
511 lineno = 0;
512 buf = buflist_findname((char_u *)filename);
513 if (buf != NULL)
514 lineno = buf_findsign(buf, markId);
515
516 return lineno;
517}
518
519
520#if 0 /* not used */
521 void
522workshop_adjust_marks(Widget *window, int pos,
523 int inserted, int deleted)
524{
525#ifdef WSDEBUG_TRACE
526 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
527 wstrace("XXXworkshop_adjust_marks(%s, %d, %d, %d)\n",
528 window ? XtName(window) : "<None>", pos, inserted, deleted);
529#endif
530}
531#endif
532
533/*
534 * Are there any moved marks? If so, call workshop_move_mark on
535 * each of them now. This is how eserve can find out if for example
536 * breakpoints have moved when a program has been recompiled and
537 * reloaded into dbx.
538 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 void
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +0000540workshop_moved_marks(char *filename UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541{
542#ifdef WSDEBUG_TRACE
543 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
544 wstrace("XXXworkshop_moved_marks(%s)\n", filename);
545#endif
546}
547
548 int
549workshop_get_font_height()
550{
551 XmFontList fontList; /* fontList made from gui.norm_font */
552 XmString str;
553 Dimension w;
554 Dimension h;
555
556#ifdef WSDEBUG_TRACE
557 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
558 wstrace("workshop_get_font_height()\n");
559#endif
560
561 /* Pick the proper signs for this font size */
562 fontList = gui_motif_create_fontlist((XFontStruct *)gui.norm_font);
563 h = 0;
564 if (fontList != NULL)
565 {
566 str = XmStringCreateLocalized("A");
567 XmStringExtent(fontList, str, &w, &h);
568 XmStringFree(str);
569 XmFontListFree(fontList);
570 }
571
572 return (int)h;
573}
574
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 void
576workshop_footer_message(
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +0000577 char *message,
578 int severity UNUSED) /* severity is currently unused */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579{
580#ifdef WSDEBUG_TRACE
581 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
582 wstrace("workshop_footer_message(%s, %d)\n", message, severity);
583#endif
584
585 gui_mch_set_footer((char_u *) message);
586}
587
588/*
589 * workshop_menu_begin() is passed the menu name. We determine its mnemonic
590 * here and store its name and priority.
591 */
592 void
593workshop_menu_begin(
594 char *label)
595{
596 vimmenu_T *menu; /* pointer to last menu */
597 int menuPriority = 0; /* priority of new menu */
598 char mnembuf[64]; /* store menubar mnemonics here */
599 char *name; /* label with a mnemonic */
600 char *p; /* used to find mnemonics */
601 int idx; /* index into mnembuf */
602
603#ifdef WSDEBUG_TRACE
604 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
605 wstrace("workshop_menu_begin()\n");
606#endif
607
608 /*
609 * Look through all existing (non-PopUp and non-Toolbar) menus
610 * and gather their mnemonics. Use this list to decide what
611 * mnemonic should be used for label.
612 */
613
614 idx = 0;
615 mnembuf[idx++] = 'H'; /* H is mnemonic for Help */
616 for (menu = root_menu; menu != NULL; menu = menu->next)
617 {
618 if (menu_is_menubar(menu->name))
619 {
620 p = strchr((char *)menu->name, '&');
621 if (p != NULL)
622 mnembuf[idx++] = *++p;
623 }
624 if (menu->next != NULL
625 && strcmp((char *) menu->next->dname, "Help") == 0)
626 {
627 menuPriority = menu->priority + 10;
628 break;
629 }
630 }
631 mnembuf[idx++] = NUL;
632 name = addUniqueMnemonic(mnembuf, label);
633
Bram Moolenaar051b7822005-05-19 21:00:46 +0000634 vim_snprintf(curMenuName, sizeof(curMenuName), "%s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 sprintf(curMenuPriority, "%d.0", menuPriority);
636}
637
638/*
639 * Append the name and priority to strings to be used in vim menu commands.
640 */
641 void
642workshop_submenu_begin(
643 char *label)
644{
645#ifdef WSDEBUG_TRACE
646 if (ws_debug && ws_dlevel & WS_TRACE
647 && strncmp(curMenuName, "ToolBar", 7) != 0)
648 wstrace("workshop_submenu_begin(%s)\n", label);
649#endif
650
651 strcat(curMenuName, ".");
652 strcat(curMenuName, fixup(label));
653
654 updatePriority(True);
655}
656
657/*
658 * Remove the submenu name and priority from curMenu*.
659 */
660
661 void
662workshop_submenu_end()
663{
664 char *p;
665
666#ifdef WSDEBUG_TRACE
667 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE)
668 && strncmp(curMenuName, "ToolBar", 7) != 0)
669 wstrace("workshop_submenu_end()\n");
670#endif
671
672 p = strrchr(curMenuPriority, '.');
673 ASSERT(p != NULL);
674 *p = NUL;
675
676 p = strrchr(curMenuName, '.');
677 ASSERT(p != NULL);
678 *p = NUL;
679}
680
681/*
682 * This is where menus are really made. Each item will generate an amenu vim
683 * command. The globals curMenuName and curMenuPriority contain the name and
684 * priority of the parent menu tree.
685 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 void
687workshop_menu_item(
688 char *label,
689 char *verb,
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +0000690 char *accelerator UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 char *acceleratorText,
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +0000692 char *name UNUSED,
693 char *filepos UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 char *sensitive)
695{
696 char cbuf[BUFSIZ];
697 char namebuf[BUFSIZ];
698 char accText[BUFSIZ];
699
700#ifdef WSDEBUG_TRACE
701 if (WSDLEVEL(WS_TRACE_VERBOSE)
702 && strncmp(curMenuName, "ToolBar", 7) != 0)
703 {
704 if (ws_dlevel & WS_TRACE_VERBOSE)
705 wsdebug("workshop_menu_item(\n"
706 "\tlabel = \"%s\",\n"
707 "\tverb = %s,\n"
708 "\taccelerator = %s,\n"
709 "\tacceleratorText = \"%s\",\n"
710 "\tname = %s,\n"
711 "\tfilepos = %s,\n"
712 "\tsensitive = %s)\n",
713 label && *label ? label : "<None>",
714 verb && *verb ? verb : "<None>",
715 accelerator && *accelerator ?
716 accelerator : "<None>",
717 acceleratorText && *acceleratorText ?
718 acceleratorText : "<None>",
719 name && *name ? name : "<None>",
720 filepos && *filepos ? filepos : "<None>",
721 sensitive);
722 else if (ws_dlevel & WS_TRACE)
723 wstrace("workshop_menu_item(\"%s\", %s)\n",
724 label && *label ? label : "<None>",
725 verb && *verb ? verb : "<None>", sensitive);
726 }
727#endif
728#ifdef WSDEBUG_SENSE
729 if (ws_debug)
730 wstrace("menu: %-21.20s%-21.20s(%s)\n", label, verb,
731 *sensitive == '1' ? "Sensitive" : "Insensitive");
732#endif
733
734 if (acceleratorText != NULL)
Bram Moolenaar051b7822005-05-19 21:00:46 +0000735 vim_snprintf(accText, sizeof(accText), "<Tab>%s", acceleratorText);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 else
737 accText[0] = NUL;
738 updatePriority(False);
Bram Moolenaar051b7822005-05-19 21:00:46 +0000739 vim_snprintf(namebuf, sizeof(namebuf), "%s.%s", curMenuName, fixup(label));
740 vim_snprintf(cbuf, sizeof(cbuf), "amenu %s %s%s\t:wsverb %s<CR>",
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 curMenuPriority, namebuf, accText, verb);
742
743 coloncmd(cbuf, TRUE);
744 addMenu(namebuf, fixAccelText(acceleratorText), verb);
745
746 if (*sensitive == '0')
747 {
Bram Moolenaar051b7822005-05-19 21:00:46 +0000748 vim_snprintf(cbuf, sizeof(cbuf), "amenu disable %s", namebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 coloncmd(cbuf, TRUE);
750 }
751}
752
753/*
754 * This function is called when a complete WorkShop menu description has been
755 * sent over from eserve. We do some menu cleanup.
756 */
757
758 void
759workshop_menu_end()
760{
761 Boolean using_tearoff; /* set per current option setting */
762
763#ifdef WSDEBUG_TRACE
764 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
765 wstrace("workshop_menu_end()\n");
766#endif
767
768 using_tearoff = vim_strchr(p_go, GO_TEAROFF) != NULL;
769 gui_mch_toggle_tearoffs(using_tearoff);
770}
771
772 void
773workshop_toolbar_begin()
774{
775#ifdef WSDEBUG_TRACE
776 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
777 wstrace("workshop_toolbar_begin()\n");
778#endif
779
780 coloncmd("aunmenu ToolBar", True);
781 tbpri = 10;
782}
783
784 void
785workshop_toolbar_end()
786{
787 char_u buf[64];
788
789#ifdef WSDEBUG_TRACE
790 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
791 {
792 wstrace("workshop_toolbar_end()\n");
793 }
794#endif
795
796 /*
797 * Turn on ToolBar.
798 */
799 STRCPY(buf, p_go);
800 if (vim_strchr(p_go, 'T') == NULL)
801 {
802 STRCAT(buf, "T");
803 set_option_value((char_u *)"go", 0L, buf, 0);
804 }
805 workshopInitDone = True;
806}
807
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 void
809workshop_toolbar_button(
810 char *label,
811 char *verb,
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +0000812 char *senseVerb UNUSED,
813 char *filepos UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 char *help,
815 char *sense,
816 char *file,
817 char *left)
818{
819 char cbuf[BUFSIZ + MAXPATHLEN];
820 char namebuf[BUFSIZ];
821 static int tbid = 1;
822 char_u *p;
Bram Moolenaar051b7822005-05-19 21:00:46 +0000823 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824
825#ifdef WSDEBUG_TRACE
826 if (WSDLEVEL(WS_TRACE_VERBOSE))
827 wsdebug("workshop_toolbar_button(\"%s\", %s, %s,\n"
828 "\t%s, \"%s\", %s,\n\t\"%s\",\n\t<%s>)\n",
829 label && *label ? label : "<None>",
830 verb && *verb ? verb : "<None>",
831 senseVerb && *senseVerb ? senseVerb : "<None>",
832 filepos && *filepos ? filepos : "<None>",
833 help && *help ? help : "<None>",
834 sense && *sense ? sense : "<None>",
835 file && *file ? file : "<None>",
836 left && *left ? left : "<None>");
837 else if (WSDLEVEL(WS_TRACE))
838 wstrace("workshop_toolbar_button(\"%s\", %s)\n",
839 label && *label ? label : "<None>",
840 verb && *verb ? verb : "<None>");
841#endif
842#ifdef WSDEBUG_SENSE
843 if (ws_debug)
844 wsdebug("button: %-21.20s%-21.20s(%s)\n", label, verb,
845 *sense == '1' ? "Sensitive" : "Insensitive");
846#endif
847
848 if (left && *left && atoi(left) > 0)
849 {
850 /* Add a separator (but pass the width passed after the ':') */
851 sprintf(cbuf, "amenu 1.%d ToolBar.-sep%d:%s- <nul>",
852 tbpri - 5, tbid++, left);
853
854 coloncmd(cbuf, True);
855 }
856
857 p = vim_strsave_escaped((char_u *)label, (char_u *)"\\. ");
Bram Moolenaar051b7822005-05-19 21:00:46 +0000858 vim_snprintf(namebuf, sizeof(namebuf), "ToolBar.%s", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 vim_free(p);
860 STRCPY(cbuf, "amenu <silent> ");
861 if (file != NULL && *file != NUL)
862 {
863 p = vim_strsave_escaped((char_u *)file, (char_u *)" ");
Bram Moolenaar051b7822005-05-19 21:00:46 +0000864 len = STRLEN(cbuf);
865 vim_snprintf(cbuf + len, sizeof(cbuf) - len, "icon=%s ", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 vim_free(p);
867 }
Bram Moolenaar051b7822005-05-19 21:00:46 +0000868 len = STRLEN(cbuf);
869 vim_snprintf(cbuf + len, sizeof(cbuf) - len,"1.%d %s :wsverb %s<CR>",
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 tbpri, namebuf, verb);
871
872 /* Define the menu item */
873 coloncmd(cbuf, True);
874
875 if (*sense == '0')
876 {
877 /* If menu isn't sensitive at startup... */
Bram Moolenaar051b7822005-05-19 21:00:46 +0000878 vim_snprintf(cbuf, sizeof(cbuf), "amenu disable %s", namebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 coloncmd(cbuf, True);
880 }
881
882 if (help && *help)
883 {
884 /* Do the tooltip */
Bram Moolenaar051b7822005-05-19 21:00:46 +0000885 vim_snprintf(cbuf, sizeof(cbuf), "tmenu %s %s", namebuf, help);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 coloncmd(cbuf, True);
887 }
888
889 addMenu(namebuf, NULL, verb);
890 tbpri += 10;
891}
892
893 void
894workshop_frame_sensitivities(
895 VerbSense *vs) /* list of verbs to (de)sensitize */
896{
897 VerbSense *vp; /* iterate through vs */
898 char *menu_name; /* used in menu lookup */
899 int cnt; /* count of verbs to skip */
900 int len; /* length of nonvariant part of command */
901 char cbuf[4096];
902
903#ifdef WSDEBUG_TRACE
904 if (WSDLEVEL(WS_TRACE_VERBOSE) || WSDLEVEL(4))
905 {
906 wsdebug("workshop_frame_sensitivities(\n");
907 for (vp = vs; vp->verb != NULL; vp++)
908 wsdebug("\t%-25s%d\n", vp->verb, vp->sense);
909 wsdebug(")\n");
910 }
911 else if (WSDLEVEL(WS_TRACE))
912 wstrace("workshop_frame_sensitivities()\n");
913#endif
914#ifdef WSDEBUG_SENSE
915 if (ws_debug)
916 for (vp = vs; vp->verb != NULL; vp++)
917 wsdebug("change: %-21.20s%-21.20s(%s)\n",
918 "", vp->verb, vp->sense == 1 ?
919 "Sensitive" : "Insensitive");
920#endif
921
922 /*
923 * Look for all matching menu entries for the verb. There may be more
924 * than one if the verb has both a menu and toolbar entry.
925 */
926 for (vp = vs; vp->verb != NULL; vp++)
927 {
928 cnt = 0;
929 strcpy(cbuf, "amenu");
930 strcat(cbuf, " ");
931 strcat(cbuf, vp->sense ? "enable" : "disable");
932 strcat(cbuf, " ");
933 len = strlen(cbuf);
934 while ((menu_name = lookupVerb(vp->verb, cnt++)) != NULL)
935 {
936 strcpy(&cbuf[len], menu_name);
937 coloncmd(cbuf, FALSE);
938 }
939 }
940 gui_update_menus(0);
941 gui_mch_flush();
942}
943
944 void
945workshop_set_option(
946 char *option, /* name of a supported option */
947 char *value) /* value to set option to */
948{
949 char cbuf[BUFSIZ]; /* command buffer */
950
951#ifdef WSDEBUG_TRACE
952 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
953 {
954 wstrace("workshop_set_option(%s, %s)\n", option, value);
955 }
956#endif
957
958 cbuf[0] = NUL;
959 switch (*option) /* switch on 1st letter */
960 {
961 case 's':
962 if (strcmp(option, "syntax") == 0)
Bram Moolenaar051b7822005-05-19 21:00:46 +0000963 vim_snprintf(cbuf, sizeof(cbuf), "syntax %s", value);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 else if (strcmp(option, "savefiles") == 0)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +0000965 {
966 /* XXX - Not yet implemented */
967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 break;
969
970 case 'l':
971 if (strcmp(option, "lineno") == 0)
972 sprintf(cbuf, "set %snu",
973 (strcmp(value, "on") == 0) ? "" : "no");
974 break;
975
976 case 'p':
977 if (strcmp(option, "parentheses") == 0)
978 sprintf(cbuf, "set %ssm",
979 (strcmp(value, "on") == 0) ? "" : "no");
980 break;
981
982 case 'w':
983 /* this option is set by a direct call */
984#ifdef WSDEBUG
985 wsdebug("workshop_set_option: "
986 "Got unexpected workshopkeys option");
987#endif
988 break;
989
990 case 'b': /* these options are set from direct calls */
991 if (option[7] == NUL && strcmp(option, "balloon") == 0)
992 {
993#ifdef WSDEBUG
994 /* set by direct call to workshop_balloon_mode */
995 wsdebug("workshop_set_option: "
996 "Got unexpected ballooneval option");
997#endif
998 }
999 else if (strcmp(option, "balloondelay") == 0)
1000 {
1001#ifdef WSDEBUG
1002 /* set by direct call to workshop_balloon_delay */
1003 wsdebug("workshop_set_option: "
1004 "Got unexpected balloondelay option");
1005#endif
1006 }
1007 break;
1008 }
1009 if (cbuf[0] != NUL)
1010 coloncmd(cbuf, TRUE);
1011}
1012
1013
1014 void
1015workshop_balloon_mode(
1016 Boolean on)
1017{
1018 char cbuf[BUFSIZ]; /* command buffer */
1019
1020#ifdef WSDEBUG_TRACE
1021 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
1022 wstrace("workshop_balloon_mode(%s)\n", on ? "True" : "False");
1023#endif
1024
1025 sprintf(cbuf, "set %sbeval", on ? "" : "no");
1026 coloncmd(cbuf, TRUE);
1027}
1028
1029
1030 void
1031workshop_balloon_delay(
1032 int delay)
1033{
1034 char cbuf[BUFSIZ]; /* command buffer */
1035
1036#ifdef WSDEBUG_TRACE
1037 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
1038 wstrace("workshop_balloon_delay(%d)\n", delay);
1039#endif
1040
1041 sprintf(cbuf, "set bdlay=%d", delay);
1042 coloncmd(cbuf, TRUE);
1043}
1044
1045
1046 void
1047workshop_show_balloon_tip(
1048 char *tip)
1049{
1050#ifdef WSDEBUG_TRACE
1051 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
1052 wstrace("workshop_show_balloon_tip(%s)\n", tip);
1053#endif
1054
1055 if (balloonEval != NULL)
1056 gui_mch_post_balloon(balloonEval, (char_u *)tip);
1057}
1058
1059
1060 void
1061workshop_hotkeys(
1062 Boolean on)
1063{
1064 char cbuf[BUFSIZ]; /* command buffer */
1065 MenuMap *mp; /* iterate over menuMap entries */
1066
1067#ifdef WSDEBUG_TRACE
1068 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
1069 wstrace("workshop_hotkeys(%s)\n", on ? "True" : "False");
1070#endif
1071
1072 workshopHotKeysEnabled = on;
1073 if (workshopHotKeysEnabled)
1074 for (mp = menuMap; mp < &menuMap[menuMapSize]; mp++)
1075 {
1076 if (mp->accel != NULL)
1077 {
Bram Moolenaar051b7822005-05-19 21:00:46 +00001078 vim_snprintf(cbuf, sizeof(cbuf),
1079 "map %s :wsverb %s<CR>", mp->accel, mp->verb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 coloncmd(cbuf, TRUE);
1081 }
1082 }
1083 else
1084 for (mp = menuMap; mp < &menuMap[menuMapSize]; mp++)
1085 {
1086 if (mp->accel != NULL)
1087 {
Bram Moolenaar051b7822005-05-19 21:00:46 +00001088 vim_snprintf(cbuf, sizeof(cbuf), "unmap %s", mp->accel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 coloncmd(cbuf, TRUE);
1090 }
1091 }
1092}
1093
1094/*
1095 * A button in the toolbar has been pushed.
1096 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 int
1098workshop_get_positions(
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001099 void *clientData UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 char **filename, /* output data */
1101 int *curLine, /* output data */
1102 int *curCol, /* output data */
1103 int *selStartLine, /* output data */
1104 int *selStartCol, /* output data */
1105 int *selEndLine, /* output data */
1106 int *selEndCol, /* output data */
1107 int *selLength, /* output data */
1108 char **selection) /* output data */
1109{
1110 static char ffname[MAXPATHLEN];
1111
1112#ifdef WSDEBUG_TRACE
1113 if (WSDLEVEL(WS_TRACE_VERBOSE | WS_TRACE))
1114 wstrace("workshop_get_positions(%#x, \"%s\", ...)\n",
1115 clientData, (curbuf && curbuf->b_sfname != NULL)
1116 ? (char *)curbuf->b_sfname : "<None>");
1117#endif
1118
Bram Moolenaara40b4662008-11-28 10:47:47 +00001119 if (curbuf->b_ffname == NULL)
1120 ffname[0] = NUL;
1121 else
1122 /* copy so nobody can change b_ffname */
1123 strcpy(ffname, (char *) curbuf->b_ffname);
1124 *filename = ffname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 *curLine = curwin->w_cursor.lnum;
1126 *curCol = curwin->w_cursor.col;
1127
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001128 if (curbuf->b_visual.vi_mode == 'v' &&
1129 equalpos(curwin->w_cursor, curbuf->b_visual.vi_end))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001131 *selStartLine = curbuf->b_visual.vi_start.lnum;
1132 *selStartCol = curbuf->b_visual.vi_start.col;
1133 *selEndLine = curbuf->b_visual.vi_end.lnum;
1134 *selEndCol = curbuf->b_visual.vi_end.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 *selection = get_selection(curbuf);
1136 if (*selection)
1137 *selLength = strlen(*selection);
1138 else
1139 *selLength = 0;
1140 }
1141 else
1142 {
1143 *selStartLine = *selEndLine = -1;
1144 *selStartCol = *selEndCol = -1;
1145 *selLength = 0;
1146 *selection = "";
1147 }
1148
1149 return True;
1150}
1151
1152
1153
1154/************************************************************************
1155 * Utility functions
1156 ************************************************************************/
1157
1158 static char *
1159get_selection(
1160 buf_T *buf) /* buffer whose selection we want */
1161{
1162 pos_T *start; /* start of the selection */
1163 pos_T *end; /* end of the selection */
1164 char *lp; /* pointer to actual line data */
1165 int llen; /* length of actual line data */
1166 char *sp; /* pointer to selection buffer */
1167 int slen; /* string length in selection buffer */
1168 int size; /* size of selection buffer */
1169 char *new_sp; /* temp pointer to new sp */
1170 int lnum; /* line number we are appending */
1171
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001172 if (buf->b_visual.vi_mode == 'v')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001174 start = &buf->b_visual.vi_start;
1175 end = &buf->b_visual.vi_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 if (start->lnum == end->lnum)
1177 {
1178 /* selection is all on one line */
1179 lp = (char *) ml_get_pos(start);
1180 llen = end->col - start->col + 1;
1181 sp = (char *) malloc(llen + 1);
1182 if (sp != NULL)
1183 {
1184 strncpy(sp, lp, llen);
1185 sp[llen] = NUL;
1186 }
1187 }
1188 else
1189 {
1190 /* multi-line selection */
1191 lp = (char *) ml_get_pos(start);
1192 llen = strlen(lp);
1193 sp = (char *) malloc(BUFSIZ + llen);
1194 if (sp != NULL)
1195 {
1196 size = BUFSIZ + llen;
1197 strcpy(sp, lp);
1198 sp[llen] = '\n';
1199 slen = llen + 1;
1200
1201 lnum = start->lnum + 1;
1202 while (lnum < end->lnum)
1203 sp = append_selection(lnum++, sp, &size, &slen);
1204
1205 lp = (char *) ml_get(end->lnum);
1206 llen = end->col + 1;
1207 if ((slen + llen) >= size)
1208 {
1209 new_sp = (char *)
1210 realloc(sp, slen + llen + 1);
1211 if (new_sp != NULL)
1212 {
1213 size += llen + 1;
1214 sp = new_sp;
1215 }
1216 }
1217 if ((slen + llen) < size)
1218 {
1219 strncpy(&sp[slen], lp, llen);
1220 sp[slen + llen] = NUL;
1221 }
1222
1223 }
1224 }
1225 }
1226 else
1227 sp = NULL;
1228
1229 return sp;
1230}
1231
1232 static char *
1233append_selection(
1234 int lnum, /* line number to append */
1235 char *sp, /* pointer to selection buffer */
1236 int *size, /* ptr to size of sp */
1237 int *slen) /* ptr to length of selection string */
1238{
1239 char *lp; /* line of data from buffer */
1240 int llen; /* strlen of lp */
1241 char *new_sp; /* temp pointer to new sp */
1242
1243 lp = (char *)ml_get((linenr_T)lnum);
1244 llen = strlen(lp);
1245
1246 if ((*slen + llen) <= *size)
1247 {
1248 new_sp = (char *) realloc((void *) sp, BUFSIZ + *slen + llen);
1249 if (*new_sp != NUL)
1250 {
1251 *size = BUFSIZ + *slen + llen;
1252 sp = new_sp;
1253 }
1254 }
1255 if ((*slen + llen) > *size)
1256 {
1257 strcat(&sp[*slen], lp);
1258 *slen += llen;
1259 sp[*slen++] = '\n';
1260 }
1261
1262 return sp;
1263}
1264
1265
1266
1267 static void
1268load_buffer_by_name(
1269 char *filename, /* the file to load */
1270 int lnum) /* an optional line number (or 0) */
1271{
1272 char lnumbuf[16]; /* make line number option for :e */
1273 char cbuf[BUFSIZ]; /* command buffer */
1274
1275 if (lnum > 0)
1276 sprintf(lnumbuf, "+%d", lnum);
1277 else
1278 lnumbuf[0] = NUL;
1279
Bram Moolenaar051b7822005-05-19 21:00:46 +00001280 vim_snprintf(cbuf, sizeof(cbuf), "e %s %s", lnumbuf, filename);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 coloncmd(cbuf, False);
1282}
1283
1284
1285 static void
1286load_window(
1287 char *filename, /* filename to load */
1288 int lnum) /* linenumber to go to */
1289{
1290 buf_T *buf; /* buffer filename is stored in */
1291 win_T *win; /* window filenme is displayed in */
1292
1293 /*
1294 * Make sure filename is displayed and is the current window.
1295 */
1296
1297 buf = buflist_findname((char_u *)filename);
1298 if (buf == NULL || (win = get_window(buf)) == NULL)
1299 {
1300 /* No buffer or buffer is not in current window */
1301 /* wsdebug("load_window: load_buffer_by_name(\"%s\", %d)\n",
1302 filename, lnum); */
1303 load_buffer_by_name(filename, lnum);
1304 }
1305 else
1306 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001307#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 /* buf is in a window */
1309 if (win != curwin)
1310 {
1311 win_enter(win, False);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001312 /* wsdebug("load_window: window enter %s\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 win->w_buffer->b_sfname); */
1314 }
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001315#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 if (lnum > 0 && win->w_cursor.lnum != lnum)
1317 {
1318 warp_to_pc(lnum);
1319 /* wsdebug("load_window: warp to %s[%d]\n",
1320 win->w_buffer->b_sfname, lnum); */
1321 }
1322 }
1323 out_flush();
1324}
1325
1326
1327
1328 static void
1329warp_to_pc(
1330 int lnum) /* line number to warp to */
1331{
Bram Moolenaar720c7102007-05-10 18:07:50 +00001332 char lbuf[256]; /* build line command here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333
1334 if (lnum > 0)
1335 {
1336 if (State & INSERT)
1337 add_to_input_buf((char_u *) "\033", 1);
1338 if (isShowing(lnum))
1339 sprintf(lbuf, "%dG", lnum);
1340 else
1341 sprintf(lbuf, "%dz.", lnum);
1342 add_to_input_buf((char_u *) lbuf, strlen(lbuf));
1343 }
1344}
1345
1346 static Boolean
1347isShowing(
1348 int lnum) /* tell if line number is showing */
1349{
1350 return lnum >= curwin->w_topline && lnum < curwin->w_botline;
1351}
1352
1353
1354
1355 static win_T *
1356get_window(
1357 buf_T *buf) /* buffer to find window for */
1358{
1359 win_T *wp = NULL; /* window filename is in */
1360
1361 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp))
1362 if (buf == wp->w_buffer)
1363 break;
1364 return wp;
1365}
1366
1367
1368#if 0 /* not used */
1369 static int
1370get_buffer_number(
1371 buf_T *buf) /* buffer to get position of */
1372{
1373 buf_T *bp; /* iterate over buffer list */
1374 int pos; /* the position in the buffer list */
1375
1376 pos = 1;
1377 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
1378 {
1379 if (bp == buf)
1380 return pos;
1381 pos++;
1382 }
1383
1384 return 1;
1385}
1386#endif
1387
1388 static void
1389updatePriority(
1390 Boolean subMenu) /* if True then start new submenu pri */
1391{
1392 int pri; /* priority of this menu/item */
1393 char *p;
1394
1395 p = strrchr(curMenuPriority, '.');
1396 ASSERT(p != NULL);
1397 *p++ = NUL;
1398
1399 pri = atoi(p) + 10; /* our new priority */
1400
1401 if (subMenu)
Bram Moolenaar051b7822005-05-19 21:00:46 +00001402 vim_snprintf(curMenuPriority, sizeof(curMenuPriority),
1403 "%s.%d.0", curMenuPriority, pri);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 else
Bram Moolenaar051b7822005-05-19 21:00:46 +00001405 vim_snprintf(curMenuPriority, sizeof(curMenuPriority),
1406 "%s.%d", curMenuPriority, pri);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407}
1408
1409 static char *
1410addUniqueMnemonic(
1411 char *mnemonics, /* currently used mnemonics */
1412 char *label) /* label of menu needing mnemonic */
1413{
1414 static char name[BUFSIZ]; /* buffer for the updated name */
1415 char *p; /* pointer into label */
1416 char *found; /* pointer to possible mnemonic */
1417
1418 found = NULL;
1419 for (p = label; *p != NUL; p++)
1420 if (strchr(mnemonics, *p) == 0)
1421 if (found == NULL || (isupper((int)*p) && islower((int)*found)))
1422 found = p;
1423
1424 if (found != NULL)
1425 {
1426 strncpy(name, label, (found - label));
1427 strcat(name, "&");
1428 strcat(name, found);
1429 }
1430 else
1431 strcpy(name, label);
1432
1433 return name;
1434}
1435
1436/*
1437 * Some characters in a menu name must be escaped in vim. Since this is vim
1438 * specific, it must be done on this side.
1439 */
1440 static char *
1441fixup(
1442 char *label)
1443{
1444 static char buf[BUFSIZ];
1445 char *bp; /* pointer into buf */
1446 char *lp; /* pointer into label */
1447
1448 lp = label;
1449 bp = buf;
1450 while (*lp != NUL)
1451 {
1452 if (*lp == ' ' || *lp == '.')
1453 *bp++ = '\\';
1454 *bp++ = *lp++;
1455 }
1456 *bp = NUL;
1457
1458 return buf;
1459}
1460
1461
1462#ifdef NOHANDS_SUPPORT_FUNCTIONS
1463
1464/* For the NoHands test suite */
1465
1466 char *
1467workshop_test_getcurrentfile()
1468{
1469 char *filename, *selection;
1470 int curLine, curCol, selStartLine, selStartCol, selEndLine;
1471 int selEndCol, selLength;
1472
1473 if (workshop_get_positions(
1474 NULL, &filename, &curLine, &curCol, &selStartLine,
1475 &selStartCol, &selEndLine, &selEndCol, &selLength,
1476 &selection))
1477 return filename;
1478 else
1479 return NULL;
1480}
1481
1482 int
1483workshop_test_getcursorrow()
1484{
1485 return 0;
1486}
1487
1488 int
1489workshop_test_getcursorcol()
1490{
1491 char *filename, *selection;
1492 int curLine, curCol, selStartLine, selStartCol, selEndLine;
1493 int selEndCol, selLength;
1494
1495 if (workshop_get_positions(
1496 NULL, &filename, &curLine, &curCol, &selStartLine,
1497 &selStartCol, &selEndLine, &selEndCol, &selLength,
1498 &selection))
1499 return curCol;
1500 else
1501 return -1;
1502}
1503
1504 char *
1505workshop_test_getcursorrowtext()
1506{
1507 return NULL;
1508}
1509
1510 char *
1511workshop_test_getselectedtext()
1512{
1513 char *filename, *selection;
1514 int curLine, curCol, selStartLine, selStartCol, selEndLine;
1515 int selEndCol, selLength;
1516
1517 if (workshop_get_positions(
1518 NULL, &filename, &curLine, &curCol, &selStartLine,
1519 &selStartCol, &selEndLine, &selEndCol, &selLength,
1520 &selection))
1521 return selection;
1522 else
1523 return NULL;
1524}
1525
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 void
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001527workshop_save_sensitivity(char *filename UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528{
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}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629
1630 static int
1631computeIndex(
1632 int wantedCol,
1633 char_u *line,
1634 int ts)
1635{
1636 int col = 0;
1637 int idx = 0;
1638
1639 while (line[idx])
1640 {
1641 if (line[idx] == '\t')
1642 col += ts - (col % ts);
1643 else
1644 col++;
1645 idx++;
1646 if (col >= wantedCol)
1647 return idx;
1648 }
1649
1650 return -1;
1651}
Bram Moolenaarba07ce32010-01-06 18:25:34 +01001652#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653
1654 static void
1655addMenu(
1656 char *menu, /* menu name */
1657 char *accel, /* accelerator text (optional) */
1658 char *verb) /* WorkShop action-verb */
1659{
1660 MenuMap *newMap;
1661 char cbuf[BUFSIZ];
1662
1663 if (menuMapSize >= menuMapMax)
1664 {
1665 newMap = realloc(menuMap,
1666 sizeof(MenuMap) * (menuMapMax + MENU_INC));
1667 if (newMap != NULL)
1668 {
1669 menuMap = newMap;
1670 menuMapMax += MENU_INC;
1671 }
1672 }
1673 if (menuMapSize < menuMapMax)
1674 {
1675 menuMap[menuMapSize].name = strdup(menu);
1676 menuMap[menuMapSize].accel = accel && *accel ? strdup(accel) : NULL;
1677 menuMap[menuMapSize++].verb = strdup(verb);
1678 if (accel && workshopHotKeysEnabled)
1679 {
Bram Moolenaar051b7822005-05-19 21:00:46 +00001680 vim_snprintf(cbuf, sizeof(cbuf),
1681 "map %s :wsverb %s<CR>", accel, verb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 coloncmd(cbuf, TRUE);
1683 }
1684 }
1685}
1686
1687 static char *
1688nameStrip(
1689 char *raw) /* menu name, possibly with & chars */
1690{
1691 static char buf[BUFSIZ]; /* build stripped name here */
1692 char *bp = buf;
1693
1694 while (*raw)
1695 {
1696 if (*raw != '&')
1697 *bp++ = *raw;
1698 raw++;
1699 }
1700 *bp = NUL;
1701 return buf;
1702}
1703
1704
1705 static char *
1706lookupVerb(
1707 char *verb,
1708 int skip) /* number of matches to skip */
1709{
1710 int i; /* loop iterator */
1711
1712 for (i = 0; i < menuMapSize; i++)
1713 if (strcmp(menuMap[i].verb, verb) == 0 && skip-- == 0)
1714 return nameStrip(menuMap[i].name);
1715
1716 return NULL;
1717}
1718
1719
1720 static void
1721coloncmd(
1722 char *cmd, /* the command to print */
1723 Boolean force) /* force cursor update */
1724{
1725 char_u *cpo_save = p_cpo;
1726
1727#ifdef WSDEBUG
1728 if (WSDLEVEL(WS_TRACE_COLONCMD))
1729 wsdebug("Cmd: %s\n", cmd);
1730#endif
1731
1732 p_cpo = empty_option;
1733
1734 ALT_INPUT_LOCK_ON;
1735 do_cmdline_cmd((char_u *)cmd);
1736 ALT_INPUT_LOCK_OFF;
1737
1738 p_cpo = cpo_save;
1739
1740 if (force)
1741 gui_update_screen();
1742}
1743
1744/*
1745 * setDollarVim - Given the run directory, search for the vim install
1746 * directory and set $VIM.
1747 *
1748 * We can be running out of SUNWspro/bin or out of
1749 * SUNWspro/contrib/contrib6/vim5.6/bin so we check
1750 * relative to both of these directories.
1751 */
1752 static void
1753setDollarVim(
1754 char *rundir)
1755{
1756 char buf[MAXPATHLEN];
1757 char *cp;
1758
1759 /*
1760 * First case: Running from <install-dir>/SUNWspro/bin
1761 */
1762 strcpy(buf, rundir);
1763 strcat(buf, "/../contrib/contrib6/vim" VIM_VERSION_SHORT "/share/vim/"
1764 VIM_VERSION_NODOT "/syntax/syntax.vim");
1765 if (access(buf, R_OK) == 0)
1766 {
1767 strcpy(buf, "SPRO_WSDIR=");
1768 strcat(buf, rundir);
1769 cp = strrchr(buf, '/');
1770 if (cp != NULL)
1771 strcpy(cp, "/WS6U2");
1772 putenv(strdup(buf));
1773
1774 strcpy(buf, "VIM=");
1775 strcat(buf, rundir);
1776 strcat(buf, "/../contrib/contrib6/vim" VIM_VERSION_SHORT "/share/vim/"
1777 VIM_VERSION_NODOT);
1778 putenv(strdup(buf));
1779 return;
1780 }
1781
1782 /*
1783 * Second case: Probably running from
1784 * <install-dir>/SUNWspro/contrib/contrib6/vim5.6/bin
1785 */
1786 strcpy(buf, rundir);
1787 strcat(buf, "/../../../contrib/contrib6/vim" VIM_VERSION_SHORT
1788 "/share/vim/" VIM_VERSION_NODOT "/syntax/syntax.vim");
1789 if (access(buf, R_OK) == 0)
1790 {
1791 strcpy(buf, "SPRO_WSDIR=");
1792 strcat(buf, rundir);
1793 cp = strrchr(buf, '/');
1794 if (cp != NULL)
1795 strcpy(cp, "../../../../WS6U2");
1796 putenv(strdup(buf));
1797
1798 strcpy(buf, "VIM=");
1799 strcat(buf, rundir);
1800 strcat(buf, "/../../../contrib/contrib6/vim" VIM_VERSION_SHORT
1801 "/share/vim/" VIM_VERSION_NODOT);
1802 putenv(strdup(buf));
1803 return;
1804 }
1805}
1806
1807/*
1808 * findYourself - Find the directory we are running from. This is used to
1809 * set $VIM. We need to set this because users can install
1810 * the package in a different directory than the compiled
1811 * directory. This is a Sun Visual WorkShop requirement!
1812 *
1813 * Note: We override a user's $VIM because it won't have the
1814 * WorkShop specific files. S/he may not like this but its
1815 * better than getting the wrong files (especially as the
1816 * user is likely to have $VIM set to 5.4 or later).
1817 */
1818 void
1819findYourself(
1820 char *argv0)
1821{
1822 char *runpath = NULL;
1823 char *path;
1824 char *pathbuf;
1825
1826 if (*argv0 == '/')
1827 runpath = strdup(argv0);
1828 else if (*argv0 == '.' || strchr(argv0, '/'))
1829 {
1830 runpath = (char *) malloc(MAXPATHLEN);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001831 if (getcwd(runpath, MAXPATHLEN) == NULL)
1832 runpath[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 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}