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