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