blob: cb6ba3e59801ea711300eb7522f221572b97cbcb [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
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>
Bram Moolenaar071d4272004-06-13 20:20:40 +000019#include <sys/socket.h>
20#ifdef HAVE_LIBGEN_H
21# include <libgen.h>
22#endif
23#include <unistd.h>
24#include <string.h>
25#include <stdlib.h>
26#include <ctype.h>
27
28#include <X11/Intrinsic.h>
29#include <Xm/Xm.h>
30#include <Xm/PushB.h>
31
32#include "integration.h" /* <EditPlugin/integration.h> */
33
34#include "vim.h"
35#include "version.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +000036#include "workshop.h"
37
38void workshop_hotkeys(Boolean);
39
40static Boolean isShowing(int);
41static win_T *get_window(buf_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +000042static void updatePriority(Boolean);
43static char *addUniqueMnemonic(char *, char *);
44static char *fixup(char *);
45static char *get_selection(buf_T *);
46static char *append_selection(int, char *, int *, int *);
47static void load_buffer_by_name(char *, 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
1230
1231 static void
1232load_buffer_by_name(
1233 char *filename, /* the file to load */
1234 int lnum) /* an optional line number (or 0) */
1235{
1236 char lnumbuf[16]; /* make line number option for :e */
1237 char cbuf[BUFSIZ]; /* command buffer */
1238
1239 if (lnum > 0)
1240 sprintf(lnumbuf, "+%d", lnum);
1241 else
1242 lnumbuf[0] = NUL;
1243
Bram Moolenaar051b7822005-05-19 21:00:46 +00001244 vim_snprintf(cbuf, sizeof(cbuf), "e %s %s", lnumbuf, filename);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 coloncmd(cbuf, False);
1246}
1247
1248
1249 static void
1250load_window(
1251 char *filename, /* filename to load */
1252 int lnum) /* linenumber to go to */
1253{
1254 buf_T *buf; /* buffer filename is stored in */
1255 win_T *win; /* window filenme is displayed in */
1256
1257 /*
1258 * Make sure filename is displayed and is the current window.
1259 */
1260
1261 buf = buflist_findname((char_u *)filename);
1262 if (buf == NULL || (win = get_window(buf)) == NULL)
1263 {
1264 /* No buffer or buffer is not in current window */
1265 /* wsdebug("load_window: load_buffer_by_name(\"%s\", %d)\n",
1266 filename, lnum); */
1267 load_buffer_by_name(filename, lnum);
1268 }
1269 else
1270 {
1271 /* buf is in a window */
1272 if (win != curwin)
1273 {
1274 win_enter(win, False);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001275 /* wsdebug("load_window: window enter %s\n",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 win->w_buffer->b_sfname); */
1277 }
1278 if (lnum > 0 && win->w_cursor.lnum != lnum)
1279 {
1280 warp_to_pc(lnum);
1281 /* wsdebug("load_window: warp to %s[%d]\n",
1282 win->w_buffer->b_sfname, lnum); */
1283 }
1284 }
1285 out_flush();
1286}
1287
1288
1289
1290 static void
1291warp_to_pc(
1292 int lnum) /* line number to warp to */
1293{
Bram Moolenaar720c7102007-05-10 18:07:50 +00001294 char lbuf[256]; /* build line command here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295
1296 if (lnum > 0)
1297 {
1298 if (State & INSERT)
1299 add_to_input_buf((char_u *) "\033", 1);
1300 if (isShowing(lnum))
1301 sprintf(lbuf, "%dG", lnum);
1302 else
1303 sprintf(lbuf, "%dz.", lnum);
1304 add_to_input_buf((char_u *) lbuf, strlen(lbuf));
1305 }
1306}
1307
1308 static Boolean
1309isShowing(
1310 int lnum) /* tell if line number is showing */
1311{
1312 return lnum >= curwin->w_topline && lnum < curwin->w_botline;
1313}
1314
1315
1316
1317 static win_T *
1318get_window(
1319 buf_T *buf) /* buffer to find window for */
1320{
1321 win_T *wp = NULL; /* window filename is in */
1322
Bram Moolenaar29323592016-07-24 22:04:11 +02001323 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 if (buf == wp->w_buffer)
1325 break;
1326 return wp;
1327}
1328
1329
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 static void
1331updatePriority(
1332 Boolean subMenu) /* if True then start new submenu pri */
1333{
1334 int pri; /* priority of this menu/item */
1335 char *p;
1336
1337 p = strrchr(curMenuPriority, '.');
1338 ASSERT(p != NULL);
1339 *p++ = NUL;
1340
1341 pri = atoi(p) + 10; /* our new priority */
1342
1343 if (subMenu)
Bram Moolenaar051b7822005-05-19 21:00:46 +00001344 vim_snprintf(curMenuPriority, sizeof(curMenuPriority),
1345 "%s.%d.0", curMenuPriority, pri);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 else
Bram Moolenaar051b7822005-05-19 21:00:46 +00001347 vim_snprintf(curMenuPriority, sizeof(curMenuPriority),
1348 "%s.%d", curMenuPriority, pri);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349}
1350
1351 static char *
1352addUniqueMnemonic(
1353 char *mnemonics, /* currently used mnemonics */
1354 char *label) /* label of menu needing mnemonic */
1355{
1356 static char name[BUFSIZ]; /* buffer for the updated name */
1357 char *p; /* pointer into label */
1358 char *found; /* pointer to possible mnemonic */
1359
1360 found = NULL;
1361 for (p = label; *p != NUL; p++)
1362 if (strchr(mnemonics, *p) == 0)
1363 if (found == NULL || (isupper((int)*p) && islower((int)*found)))
1364 found = p;
1365
1366 if (found != NULL)
1367 {
1368 strncpy(name, label, (found - label));
1369 strcat(name, "&");
1370 strcat(name, found);
1371 }
1372 else
1373 strcpy(name, label);
1374
1375 return name;
1376}
1377
1378/*
1379 * Some characters in a menu name must be escaped in vim. Since this is vim
1380 * specific, it must be done on this side.
1381 */
1382 static char *
1383fixup(
1384 char *label)
1385{
1386 static char buf[BUFSIZ];
1387 char *bp; /* pointer into buf */
1388 char *lp; /* pointer into label */
1389
1390 lp = label;
1391 bp = buf;
1392 while (*lp != NUL)
1393 {
1394 if (*lp == ' ' || *lp == '.')
1395 *bp++ = '\\';
1396 *bp++ = *lp++;
1397 }
1398 *bp = NUL;
1399
1400 return buf;
1401}
1402
1403
1404#ifdef NOHANDS_SUPPORT_FUNCTIONS
1405
1406/* For the NoHands test suite */
1407
1408 char *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001409workshop_test_getcurrentfile(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410{
1411 char *filename, *selection;
1412 int curLine, curCol, selStartLine, selStartCol, selEndLine;
1413 int selEndCol, selLength;
1414
1415 if (workshop_get_positions(
1416 NULL, &filename, &curLine, &curCol, &selStartLine,
1417 &selStartCol, &selEndLine, &selEndCol, &selLength,
1418 &selection))
1419 return filename;
1420 else
1421 return NULL;
1422}
1423
1424 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001425workshop_test_getcursorrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426{
1427 return 0;
1428}
1429
1430 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001431workshop_test_getcursorcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432{
1433 char *filename, *selection;
1434 int curLine, curCol, selStartLine, selStartCol, selEndLine;
1435 int selEndCol, selLength;
1436
1437 if (workshop_get_positions(
1438 NULL, &filename, &curLine, &curCol, &selStartLine,
1439 &selStartCol, &selEndLine, &selEndCol, &selLength,
1440 &selection))
1441 return curCol;
1442 else
1443 return -1;
1444}
1445
1446 char *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001447workshop_test_getcursorrowtext(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448{
1449 return NULL;
1450}
1451
1452 char *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001453workshop_test_getselectedtext(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454{
1455 char *filename, *selection;
1456 int curLine, curCol, selStartLine, selStartCol, selEndLine;
1457 int selEndCol, selLength;
1458
1459 if (workshop_get_positions(
1460 NULL, &filename, &curLine, &curCol, &selStartLine,
1461 &selStartCol, &selEndLine, &selEndCol, &selLength,
1462 &selection))
1463 return selection;
1464 else
1465 return NULL;
1466}
1467
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 void
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001469workshop_save_sensitivity(char *filename UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470{
1471}
1472
1473#endif
1474
1475 static char *
1476fixAccelText(
1477 char *ap) /* original acceleratorText */
1478{
1479 char buf[256]; /* build in temp buffer */
1480 char *shift; /* shift string of "" */
1481
1482 if (ap == NULL)
1483 return NULL;
1484
1485 /* If the accelerator is shifted use the vim form */
1486 if (strncmp("Shift+", ap, 6) == 0)
1487 {
1488 shift = "S-";
1489 ap += 6;
1490 }
1491 else
1492 shift = "";
1493
1494 if (*ap == 'F' && atoi(&ap[1]) > 0)
1495 {
Bram Moolenaar051b7822005-05-19 21:00:46 +00001496 vim_snprintf(buf, sizeof(buf), "<%s%s>", shift, ap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 return strdup(buf);
1498 }
1499 else
1500 return NULL;
1501}
1502
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001503#ifdef FEAT_BEVAL_GUI
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00001504 void
1505workshop_beval_cb(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 BalloonEval *beval,
1507 int state)
1508{
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00001509 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 char_u *text;
1511 int type;
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00001512 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 int col;
1514 int idx;
1515 char buf[MAXPATHLEN * 2];
1516 static int serialNo = -1;
1517
1518 if (!p_beval)
1519 return;
1520
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00001521 if (get_beval_info(beval, FALSE, &wp, &lnum, &text, &col) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 {
1523 if (text && text[0])
1524 {
1525 /* Send debugger request */
1526 if (strlen((char *) text) > (MAXPATHLEN/2))
1527 {
1528 /*
1529 * The user has probably selected the entire
1530 * buffer or something like that - don't attempt
1531 * to evaluate it
1532 */
1533 return;
1534 }
1535
1536 /*
1537 * WorkShop expects the col to be a character index, not
1538 * a column number. Compute the index from col. Also set
1539 * line to 0 because thats what dbx expects.
1540 */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001541#ifdef FEAT_VARTABS
1542 idx = computeIndex(col, text, beval->ts, beval->vts);
1543#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 idx = computeIndex(col, text, beval->ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001545#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 if (idx > 0)
1547 {
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00001548 lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549
1550 /*
1551 * If successful, it will respond with a balloon cmd.
1552 */
1553 if (state & ControlMask)
1554 /* Evaluate *(expression) */
1555 type = (int)GPLineEval_INDIRECT;
1556 else if (state & ShiftMask)
1557 /* Evaluate type(expression) */
1558 type = (int)GPLineEval_TYPE;
1559 else
1560 /* Evaluate value(expression) */
1561 type = (int)GPLineEval_EVALUATE;
1562
1563 /* Send request to dbx */
Bram Moolenaar051b7822005-05-19 21:00:46 +00001564 vim_snprintf(buf, sizeof(buf), "toolVerb debug.balloonEval "
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00001565 "%s %ld,0 %d,0 %d,%d %ld %s\n",
1566 (char *)wp->w_buffer->b_ffname,
1567 (long)lnum, idx, type, serialNo++,
1568 (long)strlen((char *)text), (char *)text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 balloonEval = beval;
1570 workshop_send_message(buf);
1571 }
1572 }
1573 }
1574}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575
1576 static int
1577computeIndex(
1578 int wantedCol,
1579 char_u *line,
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001580 int ts
1581#ifdef FEAT_VARTABS
1582 int *vts
1583#else
1584 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585{
1586 int col = 0;
1587 int idx = 0;
1588
1589 while (line[idx])
1590 {
1591 if (line[idx] == '\t')
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001592#ifdef FEAT_VARTABS
1593 col += tabstop_padding(col, ts, vts);
1594#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 col += ts - (col % ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001596#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 else
1598 col++;
1599 idx++;
1600 if (col >= wantedCol)
1601 return idx;
1602 }
1603
1604 return -1;
1605}
Bram Moolenaarba07ce32010-01-06 18:25:34 +01001606#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607
1608 static void
1609addMenu(
1610 char *menu, /* menu name */
1611 char *accel, /* accelerator text (optional) */
1612 char *verb) /* WorkShop action-verb */
1613{
1614 MenuMap *newMap;
1615 char cbuf[BUFSIZ];
1616
1617 if (menuMapSize >= menuMapMax)
1618 {
1619 newMap = realloc(menuMap,
1620 sizeof(MenuMap) * (menuMapMax + MENU_INC));
1621 if (newMap != NULL)
1622 {
1623 menuMap = newMap;
1624 menuMapMax += MENU_INC;
1625 }
1626 }
1627 if (menuMapSize < menuMapMax)
1628 {
1629 menuMap[menuMapSize].name = strdup(menu);
1630 menuMap[menuMapSize].accel = accel && *accel ? strdup(accel) : NULL;
1631 menuMap[menuMapSize++].verb = strdup(verb);
1632 if (accel && workshopHotKeysEnabled)
1633 {
Bram Moolenaar051b7822005-05-19 21:00:46 +00001634 vim_snprintf(cbuf, sizeof(cbuf),
1635 "map %s :wsverb %s<CR>", accel, verb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 coloncmd(cbuf, TRUE);
1637 }
1638 }
1639}
1640
1641 static char *
1642nameStrip(
1643 char *raw) /* menu name, possibly with & chars */
1644{
1645 static char buf[BUFSIZ]; /* build stripped name here */
1646 char *bp = buf;
1647
1648 while (*raw)
1649 {
1650 if (*raw != '&')
1651 *bp++ = *raw;
1652 raw++;
1653 }
1654 *bp = NUL;
1655 return buf;
1656}
1657
1658
1659 static char *
1660lookupVerb(
1661 char *verb,
1662 int skip) /* number of matches to skip */
1663{
1664 int i; /* loop iterator */
1665
1666 for (i = 0; i < menuMapSize; i++)
1667 if (strcmp(menuMap[i].verb, verb) == 0 && skip-- == 0)
1668 return nameStrip(menuMap[i].name);
1669
1670 return NULL;
1671}
1672
1673
1674 static void
1675coloncmd(
1676 char *cmd, /* the command to print */
1677 Boolean force) /* force cursor update */
1678{
1679 char_u *cpo_save = p_cpo;
1680
1681#ifdef WSDEBUG
1682 if (WSDLEVEL(WS_TRACE_COLONCMD))
1683 wsdebug("Cmd: %s\n", cmd);
1684#endif
1685
1686 p_cpo = empty_option;
1687
1688 ALT_INPUT_LOCK_ON;
1689 do_cmdline_cmd((char_u *)cmd);
1690 ALT_INPUT_LOCK_OFF;
1691
1692 p_cpo = cpo_save;
1693
1694 if (force)
1695 gui_update_screen();
1696}
1697
1698/*
1699 * setDollarVim - Given the run directory, search for the vim install
1700 * directory and set $VIM.
1701 *
1702 * We can be running out of SUNWspro/bin or out of
1703 * SUNWspro/contrib/contrib6/vim5.6/bin so we check
1704 * relative to both of these directories.
1705 */
1706 static void
1707setDollarVim(
1708 char *rundir)
1709{
1710 char buf[MAXPATHLEN];
1711 char *cp;
1712
1713 /*
1714 * First case: Running from <install-dir>/SUNWspro/bin
1715 */
1716 strcpy(buf, rundir);
1717 strcat(buf, "/../contrib/contrib6/vim" VIM_VERSION_SHORT "/share/vim/"
1718 VIM_VERSION_NODOT "/syntax/syntax.vim");
1719 if (access(buf, R_OK) == 0)
1720 {
1721 strcpy(buf, "SPRO_WSDIR=");
1722 strcat(buf, rundir);
1723 cp = strrchr(buf, '/');
1724 if (cp != NULL)
1725 strcpy(cp, "/WS6U2");
1726 putenv(strdup(buf));
1727
1728 strcpy(buf, "VIM=");
1729 strcat(buf, rundir);
1730 strcat(buf, "/../contrib/contrib6/vim" VIM_VERSION_SHORT "/share/vim/"
1731 VIM_VERSION_NODOT);
1732 putenv(strdup(buf));
1733 return;
1734 }
1735
1736 /*
1737 * Second case: Probably running from
1738 * <install-dir>/SUNWspro/contrib/contrib6/vim5.6/bin
1739 */
1740 strcpy(buf, rundir);
1741 strcat(buf, "/../../../contrib/contrib6/vim" VIM_VERSION_SHORT
1742 "/share/vim/" VIM_VERSION_NODOT "/syntax/syntax.vim");
1743 if (access(buf, R_OK) == 0)
1744 {
1745 strcpy(buf, "SPRO_WSDIR=");
1746 strcat(buf, rundir);
1747 cp = strrchr(buf, '/');
1748 if (cp != NULL)
1749 strcpy(cp, "../../../../WS6U2");
1750 putenv(strdup(buf));
1751
1752 strcpy(buf, "VIM=");
1753 strcat(buf, rundir);
1754 strcat(buf, "/../../../contrib/contrib6/vim" VIM_VERSION_SHORT
1755 "/share/vim/" VIM_VERSION_NODOT);
1756 putenv(strdup(buf));
1757 return;
1758 }
1759}
1760
1761/*
1762 * findYourself - Find the directory we are running from. This is used to
1763 * set $VIM. We need to set this because users can install
1764 * the package in a different directory than the compiled
1765 * directory. This is a Sun Visual WorkShop requirement!
1766 *
1767 * Note: We override a user's $VIM because it won't have the
Bram Moolenaar9af41842016-09-25 21:45:05 +02001768 * WorkShop specific files. S/he may not like this but it's
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 * better than getting the wrong files (especially as the
1770 * user is likely to have $VIM set to 5.4 or later).
1771 */
1772 void
1773findYourself(
1774 char *argv0)
1775{
1776 char *runpath = NULL;
1777 char *path;
1778 char *pathbuf;
1779
1780 if (*argv0 == '/')
1781 runpath = strdup(argv0);
1782 else if (*argv0 == '.' || strchr(argv0, '/'))
1783 {
1784 runpath = (char *) malloc(MAXPATHLEN);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001785 if (getcwd(runpath, MAXPATHLEN) == NULL)
1786 runpath[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 strcat(runpath, "/");
1788 strcat(runpath, argv0);
1789 }
1790 else
1791 {
1792 path = getenv("PATH");
1793 if (path != NULL)
1794 {
1795 runpath = (char *) malloc(MAXPATHLEN);
1796 pathbuf = strdup(path);
1797 path = strtok(pathbuf, ":");
1798 do
1799 {
1800 strcpy(runpath, path);
1801 strcat(runpath, "/");
1802 strcat(runpath, argv0);
1803 if (access(runpath, X_OK) == 0)
1804 break;
1805 } while ((path = strtok(NULL, ":")) != NULL);
1806 free(pathbuf);
1807 }
1808 }
1809
1810 if (runpath != NULL)
1811 {
1812 char runbuf[MAXPATHLEN];
1813
1814 /*
1815 * We found the run directory. Now find the install dir.
1816 */
1817 (void)vim_FullName((char_u *)runpath, (char_u *)runbuf, MAXPATHLEN, 1);
1818 path = strrchr(runbuf, '/');
1819 if (path != NULL)
1820 *path = NUL; /* remove the vim/gvim name */
1821 path = strrchr(runbuf, '/');
1822 if (path != NULL)
1823 {
1824 if (strncmp(path, "/bin", 4) == 0)
1825 setDollarVim(runbuf);
1826 else if (strncmp(path, "/src", 4) == 0)
1827 {
1828 *path = NUL; /* development tree */
1829 setDollarVim(runbuf);
1830 }
1831 }
1832 free(runpath);
1833 }
1834}