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