blob: 010e20105197d9b0bbabf3e2cb12923844ddfcba [file] [log] [blame]
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001/* vi:set ts=8 sts=4 sw=4 noet:
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#include "vim.h"
12
Bram Moolenaar3f3e9542019-07-07 20:30:48 +020013#if defined(FEAT_BEVAL) || defined(FEAT_TEXT_PROP) || defined(PROT)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +020014/*
15 * Find text under the mouse position "row" / "col".
16 * If "getword" is TRUE the returned text in "*textp" is not the whole line but
17 * the relevant word in allocated memory.
18 * Return OK if found.
19 * Return FAIL if not found, no text at the mouse position.
20 */
21 int
22find_word_under_cursor(
23 int mouserow,
24 int mousecol,
25 int getword,
26 int flags, // flags for find_ident_at_pos()
27 win_T **winp, // can be NULL
28 linenr_T *lnump, // can be NULL
29 char_u **textp,
30 int *colp)
31{
32 int row = mouserow;
33 int col = mousecol;
34 win_T *wp;
35 char_u *lbuf;
36 linenr_T lnum;
37
38 *textp = NULL;
Bram Moolenaar451d4b52019-06-12 20:22:27 +020039 wp = mouse_find_win(&row, &col, FAIL_POPUP);
Bram Moolenaar1ad022a2017-12-03 18:20:32 +010040 if (wp != NULL && row >= 0 && row < wp->w_height && col < wp->w_width)
Bram Moolenaarc3719bd2017-11-18 22:13:31 +010041 {
Bram Moolenaarb3d17a22019-07-07 18:28:14 +020042 // Found a window and the cursor is in the text. Now find the line
43 // number.
Bram Moolenaarc3719bd2017-11-18 22:13:31 +010044 if (!mouse_comp_pos(wp, &row, &col, &lnum))
45 {
Bram Moolenaarb3d17a22019-07-07 18:28:14 +020046 // Not past end of the file.
Bram Moolenaarc3719bd2017-11-18 22:13:31 +010047 lbuf = ml_get_buf(wp->w_buffer, lnum, FALSE);
48 if (col <= win_linetabsize(wp, lbuf, (colnr_T)MAXCOL))
49 {
Bram Moolenaarb3d17a22019-07-07 18:28:14 +020050 // Not past end of line.
Bram Moolenaarc3719bd2017-11-18 22:13:31 +010051 if (getword)
52 {
Bram Moolenaarb3d17a22019-07-07 18:28:14 +020053 // For Netbeans we get the relevant part of the line
54 // instead of the whole line.
Bram Moolenaarc3719bd2017-11-18 22:13:31 +010055 int len;
56 pos_T *spos = NULL, *epos = NULL;
57
58 if (VIsual_active)
59 {
60 if (LT_POS(VIsual, curwin->w_cursor))
61 {
62 spos = &VIsual;
63 epos = &curwin->w_cursor;
64 }
65 else
66 {
67 spos = &curwin->w_cursor;
68 epos = &VIsual;
69 }
70 }
71
72 col = vcol2col(wp, lnum, col);
73
74 if (VIsual_active
75 && wp->w_buffer == curwin->w_buffer
76 && (lnum == spos->lnum
77 ? col >= (int)spos->col
78 : lnum > spos->lnum)
79 && (lnum == epos->lnum
80 ? col <= (int)epos->col
81 : lnum < epos->lnum))
82 {
Bram Moolenaarb3d17a22019-07-07 18:28:14 +020083 // Visual mode and pointing to the line with the
84 // Visual selection: return selected text, with a
85 // maximum of one line.
Bram Moolenaarc3719bd2017-11-18 22:13:31 +010086 if (spos->lnum != epos->lnum || spos->col == epos->col)
87 return FAIL;
88
89 lbuf = ml_get_buf(curwin->w_buffer, VIsual.lnum, FALSE);
90 len = epos->col - spos->col;
91 if (*p_sel != 'e')
92 len += MB_PTR2LEN(lbuf + epos->col);
93 lbuf = vim_strnsave(lbuf + spos->col, len);
94 lnum = spos->lnum;
95 col = spos->col;
96 }
97 else
98 {
Bram Moolenaarb3d17a22019-07-07 18:28:14 +020099 // Find the word under the cursor.
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100100 ++emsg_off;
101 len = find_ident_at_pos(wp, lnum, (colnr_T)col, &lbuf,
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200102 flags);
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100103 --emsg_off;
104 if (len == 0)
105 return FAIL;
106 lbuf = vim_strnsave(lbuf, len);
107 }
108 }
109
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200110 if (winp != NULL)
111 *winp = wp;
112 if (lnump != NULL)
113 *lnump = lnum;
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100114 *textp = lbuf;
115 *colp = col;
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100116 return OK;
117 }
118 }
119 }
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100120 return FAIL;
121}
Bram Moolenaar3f3e9542019-07-07 20:30:48 +0200122#endif
123
124#if defined(FEAT_BEVAL) || defined(PROTO)
125
126/*
127 * Get the text and position to be evaluated for "beval".
128 * If "getword" is TRUE the returned text is not the whole line but the
129 * relevant word in allocated memory.
130 * Returns OK or FAIL.
131 */
132 int
133get_beval_info(
134 BalloonEval *beval,
135 int getword,
136 win_T **winp,
137 linenr_T *lnump,
138 char_u **textp,
139 int *colp)
140{
141 int row, col;
142
143# ifdef FEAT_BEVAL_TERM
144# ifdef FEAT_GUI
145 if (!gui.in_use)
146# endif
147 {
148 row = mouse_row;
149 col = mouse_col;
150 }
151# endif
152# ifdef FEAT_GUI
153 if (gui.in_use)
154 {
155 row = Y_2_ROW(beval->y);
156 col = X_2_COL(beval->x);
157 }
158#endif
159 if (find_word_under_cursor(row, col, getword,
160 FIND_IDENT + FIND_STRING + FIND_EVAL,
161 winp, lnump, textp, colp) == OK)
162 {
163#ifdef FEAT_VARTABS
164 vim_free(beval->vts);
165 beval->vts = tabstop_copy((*winp)->w_buffer->b_p_vts_array);
166 if ((*winp)->w_buffer->b_p_vts_array != NULL && beval->vts == NULL)
167 {
168 if (getword)
169 vim_free(*textp);
170 return FAIL;
171 }
172#endif
173 beval->ts = (*winp)->w_buffer->b_p_ts;
174 return OK;
175 }
176
177 return FAIL;
178}
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100179
180/*
Bram Moolenaar246fe032017-11-19 19:56:27 +0100181 * Show a balloon with "mesg" or "list".
Bram Moolenaar451d4b52019-06-12 20:22:27 +0200182 * Hide the balloon when both are NULL.
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100183 */
184 void
Bram Moolenaar792f0e32018-02-27 17:27:13 +0100185post_balloon(BalloonEval *beval UNUSED, char_u *mesg, list_T *list UNUSED)
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100186{
187# ifdef FEAT_BEVAL_TERM
188# ifdef FEAT_GUI
189 if (!gui.in_use)
190# endif
Bram Moolenaar246fe032017-11-19 19:56:27 +0100191 ui_post_balloon(mesg, list);
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100192# endif
193# ifdef FEAT_BEVAL_GUI
194 if (gui.in_use)
Bram Moolenaar451d4b52019-06-12 20:22:27 +0200195 // GUI can't handle a list
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100196 gui_mch_post_balloon(beval, mesg);
197# endif
198}
199
200/*
201 * Returns TRUE if the balloon eval has been enabled:
202 * 'ballooneval' for the GUI and 'balloonevalterm' for the terminal.
203 * Also checks if the screen isn't scrolled up.
204 */
205 int
206can_use_beval(void)
207{
208 return (0
209#ifdef FEAT_BEVAL_GUI
210 || (gui.in_use && p_beval)
211#endif
212#ifdef FEAT_BEVAL_TERM
213 || (
214# ifdef FEAT_GUI
215 !gui.in_use &&
216# endif
217 p_bevalterm)
218#endif
219 ) && msg_scrolled == 0;
220}
221
222/*
223 * Common code, invoked when the mouse is resting for a moment.
224 */
225 void
226general_beval_cb(BalloonEval *beval, int state UNUSED)
227{
228#ifdef FEAT_EVAL
229 win_T *wp;
230 int col;
231 int use_sandbox;
232 linenr_T lnum;
233 char_u *text;
234 static char_u *result = NULL;
235 long winnr = 0;
236 char_u *bexpr;
237 buf_T *save_curbuf;
238 size_t len;
239 win_T *cw;
240#endif
241 static int recursive = FALSE;
242
243 /* Don't do anything when 'ballooneval' is off, messages scrolled the
244 * windows up or we have no beval area. */
245 if (!can_use_beval() || beval == NULL)
246 return;
247
248 /* Don't do this recursively. Happens when the expression evaluation
249 * takes a long time and invokes something that checks for CTRL-C typed. */
250 if (recursive)
251 return;
252 recursive = TRUE;
253
254#ifdef FEAT_EVAL
255 if (get_beval_info(beval, TRUE, &wp, &lnum, &text, &col) == OK)
256 {
257 bexpr = (*wp->w_buffer->b_p_bexpr == NUL) ? p_bexpr
258 : wp->w_buffer->b_p_bexpr;
259 if (*bexpr != NUL)
260 {
261 /* Convert window pointer to number. */
262 for (cw = firstwin; cw != wp; cw = cw->w_next)
263 ++winnr;
264
265 set_vim_var_nr(VV_BEVAL_BUFNR, (long)wp->w_buffer->b_fnum);
266 set_vim_var_nr(VV_BEVAL_WINNR, winnr);
267 set_vim_var_nr(VV_BEVAL_WINID, wp->w_id);
268 set_vim_var_nr(VV_BEVAL_LNUM, (long)lnum);
269 set_vim_var_nr(VV_BEVAL_COL, (long)(col + 1));
270 set_vim_var_string(VV_BEVAL_TEXT, text, -1);
271 vim_free(text);
272
273 /*
274 * Temporarily change the curbuf, so that we can determine whether
275 * the buffer-local balloonexpr option was set insecurely.
276 */
277 save_curbuf = curbuf;
278 curbuf = wp->w_buffer;
279 use_sandbox = was_set_insecurely((char_u *)"balloonexpr",
280 *curbuf->b_p_bexpr == NUL ? 0 : OPT_LOCAL);
281 curbuf = save_curbuf;
282 if (use_sandbox)
283 ++sandbox;
284 ++textlock;
285
286 vim_free(result);
287 result = eval_to_string(bexpr, NULL, TRUE);
288
289 /* Remove one trailing newline, it is added when the result was a
290 * list and it's hardly ever useful. If the user really wants a
291 * trailing newline he can add two and one remains. */
292 if (result != NULL)
293 {
294 len = STRLEN(result);
295 if (len > 0 && result[len - 1] == NL)
296 result[len - 1] = NUL;
297 }
298
299 if (use_sandbox)
300 --sandbox;
301 --textlock;
302
303 set_vim_var_string(VV_BEVAL_TEXT, NULL, -1);
304 if (result != NULL && result[0] != NUL)
305 {
Bram Moolenaar246fe032017-11-19 19:56:27 +0100306 post_balloon(beval, result, NULL);
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100307 recursive = FALSE;
308 return;
309 }
310 }
311 }
312#endif
313#ifdef FEAT_NETBEANS_INTG
314 if (bevalServers & BEVAL_NETBEANS)
315 netbeans_beval_cb(beval, state);
316#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100317
318 recursive = FALSE;
319}
320
321#endif
322