comparison src/beval.c @ 30531:2650a01b8bbc v9.0.0601

patch 9.0.0601: too much indent Commit: https://github.com/vim/vim/commit/368aa6908862874fdb901c534ee99033ac977e4a Author: Yegappan Lakshmanan <yegappan@yahoo.com> Date: Tue Sep 27 11:46:48 2022 +0100 patch 9.0.0601: too much indent Problem: Too much indent. Solution: Return out early from a funtion. (Yegappan Lakshmanan, close #11238)
author Bram Moolenaar <Bram@vim.org>
date Tue, 27 Sep 2022 13:00:04 +0200
parents 057c26b5c33a
children 37aa9fd2ed72
comparison
equal deleted inserted replaced
30530:f501627bf532 30531:2650a01b8bbc
37 char_u *lbuf; 37 char_u *lbuf;
38 linenr_T lnum; 38 linenr_T lnum;
39 39
40 *textp = NULL; 40 *textp = NULL;
41 wp = mouse_find_win(&row, &col, FAIL_POPUP); 41 wp = mouse_find_win(&row, &col, FAIL_POPUP);
42 if (wp != NULL && row >= 0 && row < wp->w_height && col < wp->w_width) 42 if (wp == NULL || row < 0 || row >= wp->w_height || col >= wp->w_width)
43 { 43 return FAIL;
44 // Found a window and the cursor is in the text. Now find the line 44
45 // number. 45 // Found a window and the cursor is in the text. Now find the line
46 if (!mouse_comp_pos(wp, &row, &col, &lnum, NULL)) 46 // number.
47 { 47 if (mouse_comp_pos(wp, &row, &col, &lnum, NULL))
48 // Not past end of the file. 48 return FAIL; // position is below the last line
49 lbuf = ml_get_buf(wp->w_buffer, lnum, FALSE); 49
50 if (col <= win_linetabsize(wp, lnum, lbuf, (colnr_T)MAXCOL)) 50 // Not past end of the file.
51 lbuf = ml_get_buf(wp->w_buffer, lnum, FALSE);
52 if (col > win_linetabsize(wp, lnum, lbuf, (colnr_T)MAXCOL))
53 return FAIL; // past end of line
54
55 // Not past end of line.
56 if (getword)
57 {
58 // For Netbeans we get the relevant part of the line
59 // instead of the whole line.
60 int len;
61 pos_T *spos = NULL, *epos = NULL;
62
63 if (VIsual_active)
64 {
65 if (LT_POS(VIsual, curwin->w_cursor))
51 { 66 {
52 // Not past end of line. 67 spos = &VIsual;
53 if (getword) 68 epos = &curwin->w_cursor;
54 {
55 // For Netbeans we get the relevant part of the line
56 // instead of the whole line.
57 int len;
58 pos_T *spos = NULL, *epos = NULL;
59
60 if (VIsual_active)
61 {
62 if (LT_POS(VIsual, curwin->w_cursor))
63 {
64 spos = &VIsual;
65 epos = &curwin->w_cursor;
66 }
67 else
68 {
69 spos = &curwin->w_cursor;
70 epos = &VIsual;
71 }
72 }
73
74 col = vcol2col(wp, lnum, col);
75 scol = col;
76
77 if (VIsual_active
78 && wp->w_buffer == curwin->w_buffer
79 && (lnum == spos->lnum
80 ? col >= (int)spos->col
81 : lnum > spos->lnum)
82 && (lnum == epos->lnum
83 ? col <= (int)epos->col
84 : lnum < epos->lnum))
85 {
86 // Visual mode and pointing to the line with the
87 // Visual selection: return selected text, with a
88 // maximum of one line.
89 if (spos->lnum != epos->lnum || spos->col == epos->col)
90 return FAIL;
91
92 lbuf = ml_get_buf(curwin->w_buffer, VIsual.lnum, FALSE);
93 len = epos->col - spos->col;
94 if (*p_sel != 'e')
95 len += mb_ptr2len(lbuf + epos->col);
96 lbuf = vim_strnsave(lbuf + spos->col, len);
97 lnum = spos->lnum;
98 col = spos->col;
99 scol = col;
100 }
101 else
102 {
103 // Find the word under the cursor.
104 ++emsg_off;
105 len = find_ident_at_pos(wp, lnum, (colnr_T)col,
106 &lbuf, &scol, flags);
107 --emsg_off;
108 if (len == 0)
109 return FAIL;
110 lbuf = vim_strnsave(lbuf, len);
111 }
112 }
113 else
114 scol = col;
115
116 if (winp != NULL)
117 *winp = wp;
118 if (lnump != NULL)
119 *lnump = lnum;
120 *textp = lbuf;
121 if (colp != NULL)
122 *colp = col;
123 if (startcolp != NULL)
124 *startcolp = scol;
125 return OK;
126 } 69 }
127 } 70 else
128 } 71 {
129 return FAIL; 72 spos = &curwin->w_cursor;
73 epos = &VIsual;
74 }
75 }
76
77 col = vcol2col(wp, lnum, col);
78 scol = col;
79
80 if (VIsual_active
81 && wp->w_buffer == curwin->w_buffer
82 && (lnum == spos->lnum
83 ? col >= (int)spos->col
84 : lnum > spos->lnum)
85 && (lnum == epos->lnum
86 ? col <= (int)epos->col
87 : lnum < epos->lnum))
88 {
89 // Visual mode and pointing to the line with the
90 // Visual selection: return selected text, with a
91 // maximum of one line.
92 if (spos->lnum != epos->lnum || spos->col == epos->col)
93 return FAIL;
94
95 lbuf = ml_get_buf(curwin->w_buffer, VIsual.lnum, FALSE);
96 len = epos->col - spos->col;
97 if (*p_sel != 'e')
98 len += mb_ptr2len(lbuf + epos->col);
99 lbuf = vim_strnsave(lbuf + spos->col, len);
100 lnum = spos->lnum;
101 col = spos->col;
102 scol = col;
103 }
104 else
105 {
106 // Find the word under the cursor.
107 ++emsg_off;
108 len = find_ident_at_pos(wp, lnum, (colnr_T)col,
109 &lbuf, &scol, flags);
110 --emsg_off;
111 if (len == 0)
112 return FAIL;
113 lbuf = vim_strnsave(lbuf, len);
114 }
115 }
116 else
117 scol = col;
118
119 if (winp != NULL)
120 *winp = wp;
121 if (lnump != NULL)
122 *lnump = lnum;
123 *textp = lbuf;
124 if (colp != NULL)
125 *colp = col;
126 if (startcolp != NULL)
127 *startcolp = scol;
128
129 return OK;
130 } 130 }
131 #endif 131 #endif
132 132
133 #if defined(FEAT_BEVAL) || defined(PROTO) 133 #if defined(FEAT_BEVAL) || defined(PROTO)
134 134
218 p_bevalterm) 218 p_bevalterm)
219 #endif 219 #endif
220 ) && msg_scrolled == 0; 220 ) && msg_scrolled == 0;
221 } 221 }
222 222
223 # ifdef FEAT_EVAL
224 /*
225 * Evaluate the expression 'bexpr' and set the text in the balloon 'beval'.
226 */
227 static void
228 bexpr_eval(
229 BalloonEval *beval,
230 char_u *bexpr,
231 win_T *wp,
232 linenr_T lnum,
233 int col,
234 char_u *text)
235 {
236 win_T *cw;
237 long winnr = 0;
238 buf_T *save_curbuf;
239 int use_sandbox;
240 static char_u *result = NULL;
241 size_t len;
242
243 sctx_T save_sctx = current_sctx;
244
245 // Convert window pointer to number.
246 for (cw = firstwin; cw != wp; cw = cw->w_next)
247 ++winnr;
248
249 set_vim_var_nr(VV_BEVAL_BUFNR, (long)wp->w_buffer->b_fnum);
250 set_vim_var_nr(VV_BEVAL_WINNR, winnr);
251 set_vim_var_nr(VV_BEVAL_WINID, wp->w_id);
252 set_vim_var_nr(VV_BEVAL_LNUM, (long)lnum);
253 set_vim_var_nr(VV_BEVAL_COL, (long)(col + 1));
254 set_vim_var_string(VV_BEVAL_TEXT, text, -1);
255 vim_free(text);
256
257 /*
258 * Temporarily change the curbuf, so that we can determine whether
259 * the buffer-local balloonexpr option was set insecurely.
260 */
261 save_curbuf = curbuf;
262 curbuf = wp->w_buffer;
263 use_sandbox = was_set_insecurely((char_u *)"balloonexpr",
264 *curbuf->b_p_bexpr == NUL ? 0 : OPT_LOCAL);
265 curbuf = save_curbuf;
266 if (use_sandbox)
267 ++sandbox;
268 ++textlock;
269
270 if (bexpr == p_bexpr)
271 {
272 sctx_T *sp = get_option_sctx("balloonexpr");
273
274 if (sp != NULL)
275 current_sctx = *sp;
276 }
277 else
278 current_sctx = curbuf->b_p_script_ctx[BV_BEXPR];
279
280 vim_free(result);
281 result = eval_to_string(bexpr, TRUE);
282
283 // Remove one trailing newline, it is added when the result was a
284 // list and it's hardly ever useful. If the user really wants a
285 // trailing newline he can add two and one remains.
286 if (result != NULL)
287 {
288 len = STRLEN(result);
289 if (len > 0 && result[len - 1] == NL)
290 result[len - 1] = NUL;
291 }
292
293 if (use_sandbox)
294 --sandbox;
295 --textlock;
296 current_sctx = save_sctx;
297
298 set_vim_var_string(VV_BEVAL_TEXT, NULL, -1);
299 if (result != NULL && result[0] != NUL)
300 post_balloon(beval, result, NULL);
301
302 // The 'balloonexpr' evaluation may show something on the screen
303 // that requires a screen update.
304 if (must_redraw)
305 redraw_after_callback(FALSE, FALSE);
306 }
307 # endif
308
223 /* 309 /*
224 * Common code, invoked when the mouse is resting for a moment. 310 * Common code, invoked when the mouse is resting for a moment.
225 */ 311 */
226 void 312 void
227 general_beval_cb(BalloonEval *beval, int state UNUSED) 313 general_beval_cb(BalloonEval *beval, int state UNUSED)
228 { 314 {
229 #ifdef FEAT_EVAL 315 #ifdef FEAT_EVAL
230 win_T *wp; 316 win_T *wp;
231 int col; 317 int col;
232 int use_sandbox;
233 linenr_T lnum; 318 linenr_T lnum;
234 char_u *text; 319 char_u *text;
235 static char_u *result = NULL;
236 long winnr = 0;
237 char_u *bexpr; 320 char_u *bexpr;
238 buf_T *save_curbuf;
239 size_t len;
240 win_T *cw;
241 #endif 321 #endif
242 static int recursive = FALSE; 322 static int recursive = FALSE;
243 323
244 // Don't do anything when 'ballooneval' is off, messages scrolled the 324 // Don't do anything when 'ballooneval' is off, messages scrolled the
245 // windows up or we have no beval area. 325 // windows up or we have no beval area.
257 { 337 {
258 bexpr = (*wp->w_buffer->b_p_bexpr == NUL) ? p_bexpr 338 bexpr = (*wp->w_buffer->b_p_bexpr == NUL) ? p_bexpr
259 : wp->w_buffer->b_p_bexpr; 339 : wp->w_buffer->b_p_bexpr;
260 if (*bexpr != NUL) 340 if (*bexpr != NUL)
261 { 341 {
262 sctx_T save_sctx = current_sctx; 342 bexpr_eval(beval, bexpr, wp, lnum, col, text);
263
264 // Convert window pointer to number.
265 for (cw = firstwin; cw != wp; cw = cw->w_next)
266 ++winnr;
267
268 set_vim_var_nr(VV_BEVAL_BUFNR, (long)wp->w_buffer->b_fnum);
269 set_vim_var_nr(VV_BEVAL_WINNR, winnr);
270 set_vim_var_nr(VV_BEVAL_WINID, wp->w_id);
271 set_vim_var_nr(VV_BEVAL_LNUM, (long)lnum);
272 set_vim_var_nr(VV_BEVAL_COL, (long)(col + 1));
273 set_vim_var_string(VV_BEVAL_TEXT, text, -1);
274 vim_free(text);
275
276 /*
277 * Temporarily change the curbuf, so that we can determine whether
278 * the buffer-local balloonexpr option was set insecurely.
279 */
280 save_curbuf = curbuf;
281 curbuf = wp->w_buffer;
282 use_sandbox = was_set_insecurely((char_u *)"balloonexpr",
283 *curbuf->b_p_bexpr == NUL ? 0 : OPT_LOCAL);
284 curbuf = save_curbuf;
285 if (use_sandbox)
286 ++sandbox;
287 ++textlock;
288
289 if (bexpr == p_bexpr)
290 {
291 sctx_T *sp = get_option_sctx("balloonexpr");
292
293 if (sp != NULL)
294 current_sctx = *sp;
295 }
296 else
297 current_sctx = curbuf->b_p_script_ctx[BV_BEXPR];
298
299 vim_free(result);
300 result = eval_to_string(bexpr, TRUE);
301
302 // Remove one trailing newline, it is added when the result was a
303 // list and it's hardly ever useful. If the user really wants a
304 // trailing newline he can add two and one remains.
305 if (result != NULL)
306 {
307 len = STRLEN(result);
308 if (len > 0 && result[len - 1] == NL)
309 result[len - 1] = NUL;
310 }
311
312 if (use_sandbox)
313 --sandbox;
314 --textlock;
315 current_sctx = save_sctx;
316
317 set_vim_var_string(VV_BEVAL_TEXT, NULL, -1);
318 if (result != NULL && result[0] != NUL)
319 post_balloon(beval, result, NULL);
320
321 // The 'balloonexpr' evaluation may show something on the screen
322 // that requires a screen update.
323 if (must_redraw)
324 redraw_after_callback(FALSE, FALSE);
325
326 recursive = FALSE; 343 recursive = FALSE;
327 return; 344 return;
328 } 345 }
329 } 346 }
330 #endif 347 #endif