1379 lines · 34.2 KB · C
RawDownload
/* vi:set ts=8 sts=4 sw=4: * * VIM - Vi IMproved	by Bram Moolenaar *			Visual Workshop integration by Gordon Prieur * * Do ":help uganda"  in Vim to read copying and usage conditions. * Do ":help credits" in Vim to see a list of people who contributed. * See README.txt for an overview of the Vim source code. */ #include "vim.h" #if defined(FEAT_BEVAL) || defined(PROTO) /* * Common code, invoked when the mouse is resting for a moment. *//*ARGSUSED*/    voidgeneral_beval_cb(beval, state)    BalloonEval *beval;    int state;{    win_T	*wp;    int		col;    int		use_sandbox;    linenr_T	lnum;    char_u	*text;    static char_u  *result = NULL;    long	winnr = 0;    char_u	*bexpr;    buf_T	*save_curbuf;#ifdef FEAT_WINDOWS    win_T	*cw;#endif    static int	recursive = FALSE;     /* Don't do anything when 'ballooneval' is off, messages scrolled the     * windows up or we have no beval area. */    if (!p_beval || balloonEval == NULL || msg_scrolled > 0)	return;     /* Don't do this recursively.  Happens when the expression evaluation     * takes a long time and invokes something that checks for CTRL-C typed. */    if (recursive)	return;    recursive = TRUE; #ifdef FEAT_EVAL    if (get_beval_info(balloonEval, TRUE, &wp, &lnum, &text, &col) == OK)    {	bexpr = (*wp->w_buffer->b_p_bexpr == NUL) ? p_bexpr						    : wp->w_buffer->b_p_bexpr;	if (*bexpr != NUL)	{# ifdef FEAT_WINDOWS	    /* Convert window pointer to number. */	    for (cw = firstwin; cw != wp; cw = cw->w_next)		++winnr;# endif 	    set_vim_var_nr(VV_BEVAL_BUFNR, (long)wp->w_buffer->b_fnum);	    set_vim_var_nr(VV_BEVAL_WINNR, winnr);	    set_vim_var_nr(VV_BEVAL_LNUM, (long)lnum);	    set_vim_var_nr(VV_BEVAL_COL, (long)(col + 1));	    set_vim_var_string(VV_BEVAL_TEXT, text, -1);	    vim_free(text); 	    /*	     * Temporarily change the curbuf, so that we can determine whether	     * the buffer-local balloonexpr option was set insecurely.	     */	    save_curbuf = curbuf;	    curbuf = wp->w_buffer;	    use_sandbox = was_set_insecurely((char_u *)"balloonexpr",				 *curbuf->b_p_bexpr == NUL ? 0 : OPT_LOCAL);	    curbuf = save_curbuf;	    if (use_sandbox)		++sandbox;	    ++textlock; 	    vim_free(result);	    result = eval_to_string(bexpr, NULL, TRUE); 	    if (use_sandbox)		--sandbox;	    --textlock; 	    set_vim_var_string(VV_BEVAL_TEXT, NULL, -1);	    if (result != NULL && result[0] != NUL)	    {		gui_mch_post_balloon(beval, result);		recursive = FALSE;		return;	    }	}    }#endif#ifdef FEAT_NETBEANS_INTG    if (bevalServers & BEVAL_NETBEANS)	netbeans_beval_cb(beval, state);#endif#ifdef FEAT_SUN_WORKSHOP    if (bevalServers & BEVAL_WORKSHOP)	workshop_beval_cb(beval, state);#endif     recursive = FALSE;} /* on Win32 only get_beval_info() is required */#if !defined(FEAT_GUI_W32) || defined(PROTO) #ifdef FEAT_GUI_GTK# include <gdk/gdkkeysyms.h># include <gtk/gtk.h>#else# include <X11/keysym.h># ifdef FEAT_GUI_MOTIF#  include <Xm/PushB.h>#  include <Xm/Separator.h>#  include <Xm/List.h>#  include <Xm/Label.h>#  include <Xm/AtomMgr.h>#  include <Xm/Protocols.h># else   /* Assume Athena */#  include <X11/Shell.h>#  ifdef FEAT_GUI_NEXTAW#   include <X11/neXtaw/Label.h>#  else#   include <X11/Xaw/Label.h>#  endif# endif#endif #include "gui_beval.h" #ifndef FEAT_GUI_GTKextern Widget vimShell; /* * Currently, we assume that there can be only one BalloonEval showing * on-screen at any given moment.  This variable will hold the currently * showing BalloonEval or NULL if none is showing. */static BalloonEval *current_beval = NULL;#endif #ifdef FEAT_GUI_GTKstatic void addEventHandler __ARGS((GtkWidget *, BalloonEval *));static void removeEventHandler __ARGS((BalloonEval *));static gint target_event_cb __ARGS((GtkWidget *, GdkEvent *, gpointer));static gint mainwin_event_cb __ARGS((GtkWidget *, GdkEvent *, gpointer));static void pointer_event __ARGS((BalloonEval *, int, int, unsigned));static void key_event __ARGS((BalloonEval *, unsigned, int));static gint timeout_cb __ARGS((gpointer));static gint balloon_expose_event_cb __ARGS((GtkWidget *, GdkEventExpose *, gpointer));# ifndef HAVE_GTK2static void balloon_draw_cb __ARGS((GtkWidget *, GdkRectangle *, gpointer));# endif#elsestatic void addEventHandler __ARGS((Widget, BalloonEval *));static void removeEventHandler __ARGS((BalloonEval *));static void pointerEventEH __ARGS((Widget, XtPointer, XEvent *, Boolean *));static void pointerEvent __ARGS((BalloonEval *, XEvent *));static void timerRoutine __ARGS((XtPointer, XtIntervalId *));#endifstatic void cancelBalloon __ARGS((BalloonEval *));static void requestBalloon __ARGS((BalloonEval *));static void drawBalloon __ARGS((BalloonEval *));static void undrawBalloon __ARGS((BalloonEval *beval));static void createBalloonEvalWindow __ARGS((BalloonEval *));   /* * Create a balloon-evaluation area for a Widget. * There can be either a "mesg" for a fixed string or "mesgCB" to generate a * message by calling this callback function. * When "mesg" is not NULL it must remain valid for as long as the balloon is * used.  It is not freed here. * Returns a pointer to the resulting object (NULL when out of memory). */    BalloonEval *gui_mch_create_beval_area(target, mesg, mesgCB, clientData)    void	*target;    char_u	*mesg;    void	(*mesgCB)__ARGS((BalloonEval *, int));    void	*clientData;{#ifndef FEAT_GUI_GTK    char	*display_name;	    /* get from gui.dpy */    int		screen_num;    char	*p;#endif    BalloonEval	*beval;     if (mesg != NULL && mesgCB != NULL)    {	EMSG(_("E232: Cannot create BalloonEval with both message and callback"));	return NULL;    }     beval = (BalloonEval *)alloc(sizeof(BalloonEval));    if (beval != NULL)    {#ifdef FEAT_GUI_GTK	beval->target = GTK_WIDGET(target);	beval->balloonShell = NULL;	beval->timerID = 0;#else	beval->target = (Widget)target;	beval->balloonShell = NULL;	beval->timerID = (XtIntervalId)NULL;	beval->appContext = XtWidgetToApplicationContext((Widget)target);#endif	beval->showState = ShS_NEUTRAL;	beval->x = 0;	beval->y = 0;	beval->msg = mesg;	beval->msgCB = mesgCB;	beval->clientData = clientData; 	/*	 * Set up event handler which will keep its eyes on the pointer,	 * and when the pointer rests in a certain spot for a given time	 * interval, show the beval.	 */	addEventHandler(beval->target, beval);	createBalloonEvalWindow(beval); #ifndef FEAT_GUI_GTK	/*	 * Now create and save the screen width and height. Used in drawing.	 */	display_name = DisplayString(gui.dpy);	p = strrchr(display_name, '.');	if (p != NULL)	    screen_num = atoi(++p);	else	    screen_num = 0;	beval->screen_width = DisplayWidth(gui.dpy, screen_num);	beval->screen_height = DisplayHeight(gui.dpy, screen_num);#endif    }     return beval;} #if defined(FEAT_BEVAL_TIP) || defined(PROTO)/* * Destroy a balloon-eval and free its associated memory. */    voidgui_mch_destroy_beval_area(beval)    BalloonEval	*beval;{    cancelBalloon(beval);    removeEventHandler(beval);    /* Children will automatically be destroyed */# ifdef FEAT_GUI_GTK    gtk_widget_destroy(beval->balloonShell);# else    XtDestroyWidget(beval->balloonShell);# endif    vim_free(beval);}#endif     voidgui_mch_enable_beval_area(beval)    BalloonEval	*beval;{    if (beval != NULL)	addEventHandler(beval->target, beval);}     voidgui_mch_disable_beval_area(beval)    BalloonEval	*beval;{    if (beval != NULL)	removeEventHandler(beval);} #if defined(FEAT_BEVAL_TIP) || defined(PROTO)/* * This function returns the BalloonEval * associated with the currently * displayed tooltip.  Returns NULL if there is no tooltip currently showing. * * Assumption: Only one tooltip can be shown at a time. */    BalloonEval *gui_mch_currently_showing_beval(){    return current_beval;}#endif#endif /* !FEAT_GUI_W32 */ #if defined(FEAT_SUN_WORKSHOP) || defined(FEAT_NETBEANS_INTG) \    || defined(FEAT_EVAL) || defined(PROTO)/* * Get the text and position to be evaluated for "beval". * If "getword" is true the returned text is not the whole line but the * relevant word in allocated memory. * Returns OK or FAIL. */    intget_beval_info(beval, getword, winp, lnump, textp, colp)    BalloonEval	*beval;    int		getword;    win_T	**winp;    linenr_T	*lnump;    char_u	**textp;    int		*colp;{    win_T	*wp;    int		row, col;    char_u	*lbuf;    linenr_T	lnum;     *textp = NULL;    row = Y_2_ROW(beval->y);    col = X_2_COL(beval->x);#ifdef FEAT_WINDOWS    wp = mouse_find_win(&row, &col);#else    wp = firstwin;#endif    if (wp != NULL && row < wp->w_height && col < W_WIDTH(wp))    {	/* Found a window and the cursor is in the text.  Now find the line	 * number. */	if (!mouse_comp_pos(wp, &row, &col, &lnum))	{	    /* Not past end of the file. */	    lbuf = ml_get_buf(wp->w_buffer, lnum, FALSE);	    if (col <= win_linetabsize(wp, lbuf, (colnr_T)MAXCOL))	    {		/* Not past end of line. */		if (getword)		{		    /* For Netbeans we get the relevant part of the line		     * instead of the whole line. */		    int		len;		    pos_T	*spos = NULL, *epos = NULL; 		    if (VIsual_active)		    {			if (lt(VIsual, curwin->w_cursor))			{			    spos = &VIsual;			    epos = &curwin->w_cursor;			}			else			{			    spos = &curwin->w_cursor;			    epos = &VIsual;			}		    } 		    col = vcol2col(wp, lnum, col) - 1; 		    if (VIsual_active			    && wp->w_buffer == curwin->w_buffer			    && (lnum == spos->lnum				? col >= (int)spos->col				: lnum > spos->lnum)			    && (lnum == epos->lnum				? col <= (int)epos->col				: lnum < epos->lnum))		    {			/* Visual mode and pointing to the line with the			 * Visual selection: return selected text, with a			 * maximum of one line. */			if (spos->lnum != epos->lnum || spos->col == epos->col)			    return FAIL; 			lbuf = ml_get_buf(curwin->w_buffer, VIsual.lnum, FALSE);			lbuf = vim_strnsave(lbuf + spos->col,				     epos->col - spos->col + (*p_sel != 'e'));			lnum = spos->lnum;			col = spos->col;		    }		    else		    {			/* Find the word under the cursor. */			++emsg_off;			len = find_ident_at_pos(wp, lnum, (colnr_T)col, &lbuf,					FIND_IDENT + FIND_STRING + FIND_EVAL);			--emsg_off;			if (len == 0)			    return FAIL;			lbuf = vim_strnsave(lbuf, len);		    }		} 		*winp = wp;		*lnump = lnum;		*textp = lbuf;		*colp = col;		beval->ts = wp->w_buffer->b_p_ts;		return OK;	    }	}    }     return FAIL;} # if !defined(FEAT_GUI_W32) || defined(PROTO) /* * Show a balloon with "mesg". */    voidgui_mch_post_balloon(beval, mesg)    BalloonEval	*beval;    char_u	*mesg;{    beval->msg = mesg;    if (mesg != NULL)	drawBalloon(beval);    else	undrawBalloon(beval);}# endif /* FEAT_GUI_W32 */#endif /* FEAT_SUN_WORKSHOP || FEAT_NETBEANS_INTG || PROTO */ #if !defined(FEAT_GUI_W32) || defined(PROTO)#if defined(FEAT_BEVAL_TIP) || defined(PROTO)/* * Hide the given balloon. */    voidgui_mch_unpost_balloon(beval)    BalloonEval	*beval;{    undrawBalloon(beval);}#endif #ifdef FEAT_GUI_GTK/* * We can unconditionally use ANSI-style prototypes here since * GTK+ requires an ANSI C compiler anyway. */    static voidaddEventHandler(GtkWidget *target, BalloonEval *beval){    /*     * Connect to the generic "event" signal instead of the individual     * signals for each event type, because the former is emitted earlier.     * This allows us to catch events independently of the signal handlers     * in gui_gtk_x11.c.     */    /* Should use GTK_OBJECT() here, but that causes a lint warning... */    gtk_signal_connect((GtkObject*)(target), "event",		       GTK_SIGNAL_FUNC(target_event_cb),		       beval);    /*     * Nasty:  Key press events go to the main window thus the drawing area     * will never see them.  This means we have to connect to the main window     * as well in order to catch those events.     */    if (gtk_socket_id == 0 && gui.mainwin != NULL	    && gtk_widget_is_ancestor(target, gui.mainwin))    {	gtk_signal_connect((GtkObject*)(gui.mainwin), "event",			   GTK_SIGNAL_FUNC(mainwin_event_cb),			   beval);    }}     static voidremoveEventHandler(BalloonEval *beval){    /* LINTED: avoid warning: dubious operation on enum */    gtk_signal_disconnect_by_func((GtkObject*)(beval->target),				  GTK_SIGNAL_FUNC(target_event_cb),				  beval);     if (gtk_socket_id == 0 && gui.mainwin != NULL	    && gtk_widget_is_ancestor(beval->target, gui.mainwin))    {	/* LINTED: avoid warning: dubious operation on enum */	gtk_signal_disconnect_by_func((GtkObject*)(gui.mainwin),				      GTK_SIGNAL_FUNC(mainwin_event_cb),				      beval);    }}     static ginttarget_event_cb(GtkWidget *widget, GdkEvent *event, gpointer data){    BalloonEval *beval = (BalloonEval *)data;     switch (event->type)

Lines 1–500 of 1379.