summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYegappan Lakshmanan <yegappan@yahoo.com>2022-09-27 11:46:48 +0100
committerBram Moolenaar <Bram@vim.org>2022-09-27 11:46:48 +0100
commit368aa6908862874fdb901c534ee99033ac977e4a (patch)
tree4a1bc5cb3a9be47356efaa73d1554208b95c8a3e
parentd32474229213276c64cb293885a975dcb406fbc9 (diff)
downloadvim-git-368aa6908862874fdb901c534ee99033ac977e4a.tar.gz
patch 9.0.0601: too much indentv9.0.0601
Problem: Too much indent. Solution: Return out early from a funtion. (Yegappan Lakshmanan, close #11238)
-rw-r--r--src/beval.c321
-rw-r--r--src/blob.c178
-rw-r--r--src/version.c2
3 files changed, 275 insertions, 226 deletions
diff --git a/src/beval.c b/src/beval.c
index 79ffff5f4..97fc354f1 100644
--- a/src/beval.c
+++ b/src/beval.c
@@ -39,94 +39,94 @@ find_word_under_cursor(
*textp = NULL;
wp = mouse_find_win(&row, &col, FAIL_POPUP);
- if (wp != NULL && row >= 0 && row < wp->w_height && col < wp->w_width)
+ if (wp == NULL || row < 0 || row >= wp->w_height || col >= wp->w_width)
+ return FAIL;
+
+ // Found a window and the cursor is in the text. Now find the line
+ // number.
+ if (mouse_comp_pos(wp, &row, &col, &lnum, NULL))
+ return FAIL; // position is below the last line
+
+ // Not past end of the file.
+ lbuf = ml_get_buf(wp->w_buffer, lnum, FALSE);
+ if (col > win_linetabsize(wp, lnum, lbuf, (colnr_T)MAXCOL))
+ return FAIL; // past end of line
+
+ // Not past end of line.
+ if (getword)
{
- // Found a window and the cursor is in the text. Now find the line
- // number.
- if (!mouse_comp_pos(wp, &row, &col, &lnum, NULL))
+ // 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)
{
- // Not past end of the file.
- lbuf = ml_get_buf(wp->w_buffer, lnum, FALSE);
- if (col <= win_linetabsize(wp, lnum, lbuf, (colnr_T)MAXCOL))
+ if (LT_POS(VIsual, curwin->w_cursor))
{
- // 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_POS(VIsual, curwin->w_cursor))
- {
- spos = &VIsual;
- epos = &curwin->w_cursor;
- }
- else
- {
- spos = &curwin->w_cursor;
- epos = &VIsual;
- }
- }
-
- col = vcol2col(wp, lnum, col);
- scol = col;
-
- 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);
- len = epos->col - spos->col;
- if (*p_sel != 'e')
- len += mb_ptr2len(lbuf + epos->col);
- lbuf = vim_strnsave(lbuf + spos->col, len);
- lnum = spos->lnum;
- col = spos->col;
- scol = col;
- }
- else
- {
- // Find the word under the cursor.
- ++emsg_off;
- len = find_ident_at_pos(wp, lnum, (colnr_T)col,
- &lbuf, &scol, flags);
- --emsg_off;
- if (len == 0)
- return FAIL;
- lbuf = vim_strnsave(lbuf, len);
- }
- }
- else
- scol = col;
-
- if (winp != NULL)
- *winp = wp;
- if (lnump != NULL)
- *lnump = lnum;
- *textp = lbuf;
- if (colp != NULL)
- *colp = col;
- if (startcolp != NULL)
- *startcolp = scol;
- return OK;
+ spos = &VIsual;
+ epos = &curwin->w_cursor;
}
+ else
+ {
+ spos = &curwin->w_cursor;
+ epos = &VIsual;
+ }
+ }
+
+ col = vcol2col(wp, lnum, col);
+ scol = col;
+
+ 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);
+ len = epos->col - spos->col;
+ if (*p_sel != 'e')
+ len += mb_ptr2len(lbuf + epos->col);
+ lbuf = vim_strnsave(lbuf + spos->col, len);
+ lnum = spos->lnum;
+ col = spos->col;
+ scol = col;
+ }
+ else
+ {
+ // Find the word under the cursor.
+ ++emsg_off;
+ len = find_ident_at_pos(wp, lnum, (colnr_T)col,
+ &lbuf, &scol, flags);
+ --emsg_off;
+ if (len == 0)
+ return FAIL;
+ lbuf = vim_strnsave(lbuf, len);
}
}
- return FAIL;
+ else
+ scol = col;
+
+ if (winp != NULL)
+ *winp = wp;
+ if (lnump != NULL)
+ *lnump = lnum;
+ *textp = lbuf;
+ if (colp != NULL)
+ *colp = col;
+ if (startcolp != NULL)
+ *startcolp = scol;
+
+ return OK;
}
#endif
@@ -220,6 +220,92 @@ can_use_beval(void)
) && msg_scrolled == 0;
}
+# ifdef FEAT_EVAL
+/*
+ * Evaluate the expression 'bexpr' and set the text in the balloon 'beval'.
+ */
+ static void
+bexpr_eval(
+ BalloonEval *beval,
+ char_u *bexpr,
+ win_T *wp,
+ linenr_T lnum,
+ int col,
+ char_u *text)
+{
+ win_T *cw;
+ long winnr = 0;
+ buf_T *save_curbuf;
+ int use_sandbox;
+ static char_u *result = NULL;
+ size_t len;
+
+ sctx_T save_sctx = current_sctx;
+
+ // Convert window pointer to number.
+ for (cw = firstwin; cw != wp; cw = cw->w_next)
+ ++winnr;
+
+ 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_WINID, wp->w_id);
+ 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;
+
+ if (bexpr == p_bexpr)
+ {
+ sctx_T *sp = get_option_sctx("balloonexpr");
+
+ if (sp != NULL)
+ current_sctx = *sp;
+ }
+ else
+ current_sctx = curbuf->b_p_script_ctx[BV_BEXPR];
+
+ vim_free(result);
+ result = eval_to_string(bexpr, TRUE);
+
+ // Remove one trailing newline, it is added when the result was a
+ // list and it's hardly ever useful. If the user really wants a
+ // trailing newline he can add two and one remains.
+ if (result != NULL)
+ {
+ len = STRLEN(result);
+ if (len > 0 && result[len - 1] == NL)
+ result[len - 1] = NUL;
+ }
+
+ if (use_sandbox)
+ --sandbox;
+ --textlock;
+ current_sctx = save_sctx;
+
+ set_vim_var_string(VV_BEVAL_TEXT, NULL, -1);
+ if (result != NULL && result[0] != NUL)
+ post_balloon(beval, result, NULL);
+
+ // The 'balloonexpr' evaluation may show something on the screen
+ // that requires a screen update.
+ if (must_redraw)
+ redraw_after_callback(FALSE, FALSE);
+}
+# endif
+
/*
* Common code, invoked when the mouse is resting for a moment.
*/
@@ -229,15 +315,9 @@ general_beval_cb(BalloonEval *beval, int state UNUSED)
#ifdef FEAT_EVAL
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;
- size_t len;
- win_T *cw;
#endif
static int recursive = FALSE;
@@ -259,70 +339,7 @@ general_beval_cb(BalloonEval *beval, int state UNUSED)
: wp->w_buffer->b_p_bexpr;
if (*bexpr != NUL)
{
- sctx_T save_sctx = current_sctx;
-
- // Convert window pointer to number.
- for (cw = firstwin; cw != wp; cw = cw->w_next)
- ++winnr;
-
- 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_WINID, wp->w_id);
- 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;
-
- if (bexpr == p_bexpr)
- {
- sctx_T *sp = get_option_sctx("balloonexpr");
-
- if (sp != NULL)
- current_sctx = *sp;
- }
- else
- current_sctx = curbuf->b_p_script_ctx[BV_BEXPR];
-
- vim_free(result);
- result = eval_to_string(bexpr, TRUE);
-
- // Remove one trailing newline, it is added when the result was a
- // list and it's hardly ever useful. If the user really wants a
- // trailing newline he can add two and one remains.
- if (result != NULL)
- {
- len = STRLEN(result);
- if (len > 0 && result[len - 1] == NL)
- result[len - 1] = NUL;
- }
-
- if (use_sandbox)
- --sandbox;
- --textlock;
- current_sctx = save_sctx;
-
- set_vim_var_string(VV_BEVAL_TEXT, NULL, -1);
- if (result != NULL && result[0] != NUL)
- post_balloon(beval, result, NULL);
-
- // The 'balloonexpr' evaluation may show something on the screen
- // that requires a screen update.
- if (must_redraw)
- redraw_after_callback(FALSE, FALSE);
-
+ bexpr_eval(beval, bexpr, wp, lnum, col, text);
recursive = FALSE;
return;
}
diff --git a/src/blob.c b/src/blob.c
index a6ab3959b..e4d23d260 100644
--- a/src/blob.c
+++ b/src/blob.c
@@ -60,29 +60,31 @@ rettv_blob_set(typval_T *rettv, blob_T *b)
int
blob_copy(blob_T *from, typval_T *to)
{
- int ret = OK;
+ int len;
to->v_type = VAR_BLOB;
to->v_lock = 0;
if (from == NULL)
- to->vval.v_blob = NULL;
- else if (rettv_blob_alloc(to) == FAIL)
- ret = FAIL;
- else
{
- int len = from->bv_ga.ga_len;
+ to->vval.v_blob = NULL;
+ return OK;
+ }
- if (len > 0)
- {
- to->vval.v_blob->bv_ga.ga_data =
- vim_memsave(from->bv_ga.ga_data, len);
- if (to->vval.v_blob->bv_ga.ga_data == NULL)
- len = 0;
- }
- to->vval.v_blob->bv_ga.ga_len = len;
- to->vval.v_blob->bv_ga.ga_maxlen = len;
+ if (rettv_blob_alloc(to) == FAIL)
+ return FAIL;
+
+ len = from->bv_ga.ga_len;
+ if (len > 0)
+ {
+ to->vval.v_blob->bv_ga.ga_data =
+ vim_memsave(from->bv_ga.ga_data, len);
+ if (to->vval.v_blob->bv_ga.ga_data == NULL)
+ len = 0;
}
- return ret;
+ to->vval.v_blob->bv_ga.ga_len = len;
+ to->vval.v_blob->bv_ga.ga_maxlen = len;
+
+ return OK;
}
void
@@ -280,80 +282,108 @@ failed:
return NULL;
}
- int
-blob_slice_or_index(
+/*
+ * Returns a slice of 'blob' from index 'n1' to 'n2' in 'rettv'. The length of
+ * the blob is 'len'. Returns an empty blob if the indexes are out of range.
+ */
+ static int
+blob_slice(
blob_T *blob,
- int is_range,
+ long len,
varnumber_T n1,
varnumber_T n2,
int exclusive,
typval_T *rettv)
{
- long len = blob_len(blob);
-
- if (is_range)
+ if (n1 < 0)
{
- // The resulting variable is a sub-blob. If the indexes
- // are out of range the result is empty.
+ n1 = len + n1;
if (n1 < 0)
- {
- n1 = len + n1;
- if (n1 < 0)
- n1 = 0;
- }
- if (n2 < 0)
- n2 = len + n2;
- else if (n2 >= len)
- n2 = len - (exclusive ? 0 : 1);
- if (exclusive)
- --n2;
- if (n1 >= len || n2 < 0 || n1 > n2)
- {
- clear_tv(rettv);
- rettv->v_type = VAR_BLOB;
- rettv->vval.v_blob = NULL;
- }
- else
- {
- blob_T *new_blob = blob_alloc();
- long i;
-
- if (new_blob != NULL)
- {
- if (ga_grow(&new_blob->bv_ga, n2 - n1 + 1) == FAIL)
- {
- blob_free(new_blob);
- return FAIL;
- }
- new_blob->bv_ga.ga_len = n2 - n1 + 1;
- for (i = n1; i <= n2; i++)
- blob_set(new_blob, i - n1, blob_get(blob, i));
-
- clear_tv(rettv);
- rettv_blob_set(rettv, new_blob);
- }
- }
+ n1 = 0;
+ }
+ if (n2 < 0)
+ n2 = len + n2;
+ else if (n2 >= len)
+ n2 = len - (exclusive ? 0 : 1);
+ if (exclusive)
+ --n2;
+ if (n1 >= len || n2 < 0 || n1 > n2)
+ {
+ clear_tv(rettv);
+ rettv->v_type = VAR_BLOB;
+ rettv->vval.v_blob = NULL;
}
else
{
- // The resulting variable is a byte value.
- // If the index is too big or negative that is an error.
- if (n1 < 0)
- n1 = len + n1;
- if (n1 < len && n1 >= 0)
+ blob_T *new_blob = blob_alloc();
+ long i;
+
+ if (new_blob != NULL)
{
- int v = blob_get(blob, n1);
+ if (ga_grow(&new_blob->bv_ga, n2 - n1 + 1) == FAIL)
+ {
+ blob_free(new_blob);
+ return FAIL;
+ }
+ new_blob->bv_ga.ga_len = n2 - n1 + 1;
+ for (i = n1; i <= n2; i++)
+ blob_set(new_blob, i - n1, blob_get(blob, i));
clear_tv(rettv);
- rettv->v_type = VAR_NUMBER;
- rettv->vval.v_number = v;
- }
- else
- {
- semsg(_(e_blob_index_out_of_range_nr), n1);
- return FAIL;
+ rettv_blob_set(rettv, new_blob);
}
}
+
+ return OK;
+}
+
+/*
+ * Return the byte value in 'blob' at index 'idx' in 'rettv'. If the index is
+ * too big or negative that is an error. The length of the blob is 'len'.
+ */
+ static int
+blob_index(
+ blob_T *blob,
+ int len,
+ varnumber_T idx,
+ typval_T *rettv)
+{
+ // The resulting variable is a byte value.
+ // If the index is too big or negative that is an error.
+ if (idx < 0)
+ idx = len + idx;
+ if (idx < len && idx >= 0)
+ {
+ int v = blob_get(blob, idx);
+
+ clear_tv(rettv);
+ rettv->v_type = VAR_NUMBER;
+ rettv->vval.v_number = v;
+ }
+ else
+ {
+ semsg(_(e_blob_index_out_of_range_nr), idx);
+ return FAIL;
+ }
+
+ return OK;
+}
+
+ int
+blob_slice_or_index(
+ blob_T *blob,
+ int is_range,
+ varnumber_T n1,
+ varnumber_T n2,
+ int exclusive,
+ typval_T *rettv)
+{
+ long len = blob_len(blob);
+
+ if (is_range)
+ return blob_slice(blob, len, n1, n2, exclusive, rettv);
+ else
+ return blob_index(blob, len, n1, rettv);
return OK;
}
diff --git a/src/version.c b/src/version.c
index d0e14a203..095252689 100644
--- a/src/version.c
+++ b/src/version.c
@@ -700,6 +700,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 601,
+/**/
600,
/**/
599,