aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/match.c
diff options
context:
space:
mode:
authorJosh Rahm <joshuarahm@gmail.com>2023-11-30 20:35:25 +0000
committerJosh Rahm <joshuarahm@gmail.com>2023-11-30 20:35:25 +0000
commit1b7b916b7631ddf73c38e3a0070d64e4636cb2f3 (patch)
treecd08258054db80bb9a11b1061bb091c70b76926a /src/nvim/match.c
parenteaa89c11d0f8aefbb512de769c6c82f61a8baca3 (diff)
parent4a8bf24ac690004aedf5540fa440e788459e5e34 (diff)
downloadrneovim-1b7b916b7631ddf73c38e3a0070d64e4636cb2f3.tar.gz
rneovim-1b7b916b7631ddf73c38e3a0070d64e4636cb2f3.tar.bz2
rneovim-1b7b916b7631ddf73c38e3a0070d64e4636cb2f3.zip
Merge remote-tracking branch 'upstream/master' into aucmd_textputpostaucmd_textputpost
Diffstat (limited to 'src/nvim/match.c')
-rw-r--r--src/nvim/match.c125
1 files changed, 52 insertions, 73 deletions
diff --git a/src/nvim/match.c b/src/nvim/match.c
index 6663dfd7ec..0a7c264d4f 100644
--- a/src/nvim/match.c
+++ b/src/nvim/match.c
@@ -1,6 +1,3 @@
-// This is an open source non-commercial project. Dear PVS-Studio, please check
-// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
-
// match.c: functions for highlighting matches
#include <assert.h>
@@ -9,40 +6,40 @@
#include <stdio.h>
#include <string.h>
-#include "nvim/ascii.h"
+#include "nvim/ascii_defs.h"
#include "nvim/buffer_defs.h"
#include "nvim/charset.h"
#include "nvim/drawscreen.h"
#include "nvim/eval/funcs.h"
#include "nvim/eval/typval.h"
-#include "nvim/eval/typval_defs.h"
#include "nvim/eval/window.h"
#include "nvim/ex_cmds_defs.h"
#include "nvim/ex_docmd.h"
#include "nvim/fold.h"
+#include "nvim/func_attr.h"
#include "nvim/gettext.h"
#include "nvim/globals.h"
#include "nvim/highlight.h"
#include "nvim/highlight_group.h"
-#include "nvim/macros.h"
+#include "nvim/macros_defs.h"
#include "nvim/match.h"
#include "nvim/mbyte.h"
#include "nvim/memline.h"
#include "nvim/memory.h"
#include "nvim/message.h"
-#include "nvim/option_defs.h"
-#include "nvim/pos.h"
+#include "nvim/option_vars.h"
+#include "nvim/pos_defs.h"
#include "nvim/profile.h"
#include "nvim/regexp.h"
#include "nvim/strings.h"
-#include "nvim/types.h"
-#include "nvim/vim.h"
+#include "nvim/types_defs.h"
+#include "nvim/vim_defs.h"
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "match.c.generated.h"
#endif
-static char *e_invalwindow = N_("E957: Invalid window number");
+static const char *e_invalwindow = N_("E957: Invalid window number");
#define SEARCH_HL_PRIORITY 0
@@ -59,9 +56,6 @@ static int match_add(win_T *wp, const char *const grp, const char *const pat, in
list_T *pos_list, const char *const conceal_char)
FUNC_ATTR_NONNULL_ARG(1, 2)
{
- matchitem_T *cur;
- matchitem_T *prev;
- matchitem_T *m;
int hlg_id;
regprog_T *regprog = NULL;
int rtype = UPD_SOME_VALID;
@@ -80,7 +74,7 @@ static int match_add(win_T *wp, const char *const grp, const char *const pat, in
id = wp->w_next_match_id++;
} else {
// check the given ID is not already in use
- for (cur = wp->w_match_head; cur != NULL; cur = cur->mit_next) {
+ for (matchitem_T *cur = wp->w_match_head; cur != NULL; cur = cur->mit_next) {
if (cur->mit_id == id) {
semsg(_("E801: ID already taken: %" PRId64), (int64_t)id);
return -1;
@@ -98,20 +92,20 @@ static int match_add(win_T *wp, const char *const grp, const char *const pat, in
if ((hlg_id = syn_check_group(grp, strlen(grp))) == 0) {
return -1;
}
- if (pat != NULL && (regprog = vim_regcomp((char *)pat, RE_MAGIC)) == NULL) {
+ if (pat != NULL && (regprog = vim_regcomp(pat, RE_MAGIC)) == NULL) {
semsg(_(e_invarg2), pat);
return -1;
}
// Build new match.
- m = xcalloc(1, sizeof(matchitem_T));
+ matchitem_T *m = xcalloc(1, sizeof(matchitem_T));
if (pos_list != NULL) {
m->mit_pos_array = xcalloc((size_t)tv_list_len(pos_list), sizeof(llpos_T));
m->mit_pos_count = tv_list_len(pos_list);
}
m->mit_id = id;
m->mit_priority = prio;
- m->mit_pattern = pat == NULL ? NULL: xstrdup(pat);
+ m->mit_pattern = pat == NULL ? NULL : xstrdup(pat);
m->mit_hlg_id = hlg_id;
m->mit_match.regprog = regprog;
m->mit_match.rmm_ic = false;
@@ -215,8 +209,8 @@ static int match_add(win_T *wp, const char *const grp, const char *const pat, in
// Insert new match. The match list is in ascending order with regard to
// the match priorities.
- cur = wp->w_match_head;
- prev = cur;
+ matchitem_T *cur = wp->w_match_head;
+ matchitem_T *prev = cur;
while (cur != NULL && prio >= cur->mit_priority) {
prev = cur;
cur = cur->mit_next;
@@ -296,10 +290,8 @@ static int match_delete(win_T *wp, int id, bool perr)
/// Delete all matches in the match list of window 'wp'.
void clear_matches(win_T *wp)
{
- matchitem_T *m;
-
while (wp->w_match_head != NULL) {
- m = wp->w_match_head->mit_next;
+ matchitem_T *m = wp->w_match_head->mit_next;
vim_regfree(wp->w_match_head->mit_match.regprog);
xfree(wp->w_match_head->mit_pattern);
xfree(wp->w_match_head->mit_pos_array);
@@ -358,11 +350,10 @@ void init_search_hl(win_T *wp, match_T *search_hl)
static int next_search_hl_pos(match_T *shl, linenr_T lnum, matchitem_T *match, colnr_T mincol)
FUNC_ATTR_NONNULL_ALL
{
- int i;
int found = -1;
shl->lnum = 0;
- for (i = match->mit_pos_cur; i < match->mit_pos_count; i++) {
+ for (int i = match->mit_pos_cur; i < match->mit_pos_count; i++) {
llpos_T *pos = &match->mit_pos_array[i];
if (pos->lnum == 0) {
@@ -388,7 +379,7 @@ static int next_search_hl_pos(match_T *shl, linenr_T lnum, matchitem_T *match, c
match->mit_pos_cur = 0;
if (found >= 0) {
colnr_T start = match->mit_pos_array[found].col == 0
- ? 0: match->mit_pos_array[found].col - 1;
+ ? 0 : match->mit_pos_array[found].col - 1;
colnr_T end = match->mit_pos_array[found].col == 0
? MAXCOL : start + match->mit_pos_array[found].len;
@@ -419,9 +410,8 @@ static void next_search_hl(win_T *win, match_T *search_hl, match_T *shl, linenr_
colnr_T mincol, matchitem_T *cur)
FUNC_ATTR_NONNULL_ARG(2)
{
- linenr_T l;
colnr_T matchcol;
- long nmatched = 0;
+ int nmatched = 0;
const int called_emsg_before = called_emsg;
// for :{range}s/pat only highlight inside the range
@@ -435,7 +425,7 @@ static void next_search_hl(win_T *win, match_T *search_hl, match_T *shl, linenr_
// 1. If the "lnum" is below a previous match, start a new search.
// 2. If the previous match includes "mincol", use it.
// 3. Continue after the previous match.
- l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
+ linenr_T l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
if (lnum > l) {
shl->lnum = 0;
} else if (lnum < l || shl->rm.endpos[0].col > mincol) {
@@ -445,7 +435,7 @@ static void next_search_hl(win_T *win, match_T *search_hl, match_T *shl, linenr_
// Repeat searching for a match until one is found that includes "mincol"
// or none is found in this line.
- for (;;) {
+ while (true) {
// Stop searching after passing the time limit.
if (profile_passed_limit(shl->tm)) {
shl->lnum = 0; // no match found in time
@@ -464,7 +454,7 @@ static void next_search_hl(win_T *win, match_T *search_hl, match_T *shl, linenr_
char *ml;
matchcol = shl->rm.startpos[0].col;
- ml = ml_get_buf(shl->buf, lnum, false) + matchcol;
+ ml = ml_get_buf(shl->buf, lnum) + matchcol;
if (*ml == NUL) {
matchcol++;
shl->lnum = 0;
@@ -523,22 +513,19 @@ static void next_search_hl(win_T *win, match_T *search_hl, match_T *shl, linenr_
void prepare_search_hl(win_T *wp, match_T *search_hl, linenr_T lnum)
FUNC_ATTR_NONNULL_ALL
{
- matchitem_T *cur; // points to the match list
+ matchitem_T *cur = wp->w_match_head; // points to the match list
match_T *shl; // points to search_hl or a match
- bool shl_flag; // flag to indicate whether search_hl
- // has been processed or not
+ bool shl_flag = false; // flag to indicate whether search_hl has been processed or not
// When using a multi-line pattern, start searching at the top
// of the window or just after a closed fold.
// Do this both for search_hl and the match list.
- cur = wp->w_match_head;
- shl_flag = false;
while (cur != NULL || shl_flag == false) {
if (shl_flag == false) {
shl = search_hl;
shl_flag = true;
} else {
- shl = &cur->mit_hl; // -V595
+ shl = &cur->mit_hl;
}
if (shl->rm.regprog != NULL
&& shl->lnum == 0
@@ -616,7 +603,7 @@ bool prepare_search_hl_line(win_T *wp, linenr_T lnum, colnr_T mincol, char **lin
shl = search_hl;
shl_flag = true;
} else {
- shl = &cur->mit_hl; // -V595
+ shl = &cur->mit_hl;
}
shl->startcol = MAXCOL;
shl->endcol = MAXCOL;
@@ -631,7 +618,7 @@ bool prepare_search_hl_line(win_T *wp, linenr_T lnum, colnr_T mincol, char **lin
// Need to get the line again, a multi-line regexp may have made it
// invalid.
- *line = ml_get_buf(wp->w_buffer, lnum, false);
+ *line = ml_get_buf(wp->w_buffer, lnum);
if (shl->lnum != 0 && shl->lnum <= lnum) {
if (shl->lnum == lnum) {
@@ -659,7 +646,7 @@ bool prepare_search_hl_line(win_T *wp, linenr_T lnum, colnr_T mincol, char **lin
shl->endcol++;
}
}
- if ((long)shl->startcol < mincol) { // match at leftcol
+ if (shl->startcol < mincol) { // match at leftcol
shl->attr_cur = shl->attr;
*search_attr = shl->attr;
*search_attr_from_match = shl != search_hl;
@@ -717,8 +704,8 @@ int update_search_hl(win_T *wp, linenr_T lnum, colnr_T col, char **line, match_T
}
// Highlight the match were the cursor is using the CurSearch
// group.
- if (shl == search_hl && shl->has_cursor && (HL_ATTR(HLF_LC) || win_hl_attr(wp, HLF_LC))) {
- shl->attr_cur = win_hl_attr(wp, HLF_LC) ? win_hl_attr(wp, HLF_LC) : HL_ATTR(HLF_LC);
+ if (shl == search_hl && shl->has_cursor) {
+ shl->attr_cur = win_hl_attr(wp, HLF_LC);
} else {
shl->attr_cur = shl->attr;
}
@@ -741,7 +728,7 @@ int update_search_hl(win_T *wp, linenr_T lnum, colnr_T col, char **line, match_T
// Need to get the line again, a multi-line regexp
// may have made it invalid.
- *line = ml_get_buf(wp->w_buffer, lnum, false);
+ *line = ml_get_buf(wp->w_buffer, lnum);
if (shl->lnum == lnum) {
shl->startcol = shl->rm.startpos[0].col;
@@ -809,28 +796,27 @@ int update_search_hl(win_T *wp, linenr_T lnum, colnr_T col, char **line, match_T
return search_attr;
}
-bool get_prevcol_hl_flag(win_T *wp, match_T *search_hl, long curcol)
+bool get_prevcol_hl_flag(win_T *wp, match_T *search_hl, colnr_T curcol)
{
- long prevcol = curcol;
- matchitem_T *cur; // points to the match list
+ colnr_T prevcol = curcol;
// we're not really at that column when skipping some text
- if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol) {
+ if ((wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol) {
prevcol++;
}
// Highlight a character after the end of the line if the match started
// at the end of the line or when the match continues in the next line
// (match includes the line break).
- if (!search_hl->is_addpos && (prevcol == (long)search_hl->startcol
- || (prevcol > (long)search_hl->startcol
+ if (!search_hl->is_addpos && (prevcol == search_hl->startcol
+ || (prevcol > search_hl->startcol
&& search_hl->endcol == MAXCOL))) {
return true;
}
- cur = wp->w_match_head;
+ matchitem_T *cur = wp->w_match_head; // points to the match list
while (cur != NULL) {
- if (!cur->mit_hl.is_addpos && (prevcol == (long)cur->mit_hl.startcol
- || (prevcol > (long)cur->mit_hl.startcol
+ if (!cur->mit_hl.is_addpos && (prevcol == cur->mit_hl.startcol
+ || (prevcol > cur->mit_hl.startcol
&& cur->mit_hl.endcol == MAXCOL))) {
return true;
}
@@ -842,7 +828,7 @@ bool get_prevcol_hl_flag(win_T *wp, match_T *search_hl, long curcol)
/// Get highlighting for the char after the text in "char_attr" from 'hlsearch'
/// or match highlighting.
-void get_search_match_hl(win_T *wp, match_T *search_hl, long col, int *char_attr)
+void get_search_match_hl(win_T *wp, match_T *search_hl, colnr_T col, int *char_attr)
{
matchitem_T *cur = wp->w_match_head; // points to the match list
match_T *shl; // points to search_hl or a match
@@ -857,7 +843,7 @@ void get_search_match_hl(win_T *wp, match_T *search_hl, long col, int *char_attr
} else {
shl = &cur->mit_hl;
}
- if (col - 1 == (long)shl->startcol
+ if (col - 1 == shl->startcol
&& (shl == search_hl || !shl->is_addpos)) {
*char_attr = shl->attr;
}
@@ -906,8 +892,6 @@ void f_clearmatches(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
/// "getmatches()" function
void f_getmatches(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
{
- matchitem_T *cur;
- int i;
win_T *win = get_optional_window(argvars, 0);
tv_list_alloc_ret(rettv, kListLenMayKnow);
@@ -915,12 +899,12 @@ void f_getmatches(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
return;
}
- cur = win->w_match_head;
+ matchitem_T *cur = win->w_match_head;
while (cur != NULL) {
dict_T *dict = tv_dict_alloc();
if (cur->mit_match.regprog == NULL) {
// match added with matchaddpos()
- for (i = 0; i < cur->mit_pos_count; i++) {
+ for (int i = 0; i < cur->mit_pos_count; i++) {
llpos_T *llpos;
char buf[30]; // use 30 to avoid compiler warning
@@ -939,15 +923,14 @@ void f_getmatches(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
tv_dict_add_list(dict, buf, (size_t)len, l);
}
} else {
- tv_dict_add_str(dict, S_LEN("pattern"), (const char *)cur->mit_pattern);
+ tv_dict_add_str(dict, S_LEN("pattern"), cur->mit_pattern);
}
- tv_dict_add_str(dict, S_LEN("group"),
- (const char *)syn_id2name(cur->mit_hlg_id));
+ tv_dict_add_str(dict, S_LEN("group"), syn_id2name(cur->mit_hlg_id));
tv_dict_add_nr(dict, S_LEN("priority"), (varnumber_T)cur->mit_priority);
tv_dict_add_nr(dict, S_LEN("id"), (varnumber_T)cur->mit_id);
if (cur->mit_conceal_char) {
- char buf[MB_MAXBYTES + 1];
+ char buf[MB_MAXCHAR + 1];
buf[utf_char2bytes(cur->mit_conceal_char, buf)] = NUL;
tv_dict_add_str(dict, S_LEN("conceal"), buf);
@@ -1167,9 +1150,8 @@ void f_matcharg(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
matchitem_T *const m = get_match(curwin, id);
if (m != NULL) {
- tv_list_append_string(rettv->vval.v_list,
- (const char *)syn_id2name(m->mit_hlg_id), -1);
- tv_list_append_string(rettv->vval.v_list, (const char *)m->mit_pattern, -1);
+ tv_list_append_string(rettv->vval.v_list, syn_id2name(m->mit_hlg_id), -1);
+ tv_list_append_string(rettv->vval.v_list, m->mit_pattern, -1);
} else {
tv_list_append_string(rettv->vval.v_list, NULL, 0);
tv_list_append_string(rettv->vval.v_list, NULL, 0);
@@ -1194,10 +1176,8 @@ void f_matchdelete(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
/// skipping commands to find the next command.
void ex_match(exarg_T *eap)
{
- char *p;
char *g = NULL;
char *end;
- int c;
int id;
if (eap->line2 <= 3) {
@@ -1218,9 +1198,9 @@ void ex_match(exarg_T *eap)
&& (ascii_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4])))) {
end = eap->arg + 4;
} else {
- p = skiptowhite(eap->arg);
+ char *p = skiptowhite(eap->arg);
if (!eap->skip) {
- g = xstrnsave(eap->arg, (size_t)(p - eap->arg));
+ g = xmemdupz(eap->arg, (size_t)(p - eap->arg));
}
p = skipwhite(p);
if (*p == NUL) {
@@ -1233,7 +1213,7 @@ void ex_match(exarg_T *eap)
if (!eap->skip) {
if (*end != NUL && !ends_excmd(*skipwhite(end + 1))) {
xfree(g);
- eap->errmsg = ex_errmsg(e_trailing_arg, (const char *)end);
+ eap->errmsg = ex_errmsg(e_trailing_arg, end);
return;
}
if (*end != *p) {
@@ -1242,10 +1222,9 @@ void ex_match(exarg_T *eap)
return;
}
- c = (uint8_t)(*end);
+ int c = (uint8_t)(*end);
*end = NUL;
- match_add(curwin, (const char *)g, (const char *)p + 1, 10, id,
- NULL, NULL);
+ match_add(curwin, g, p + 1, 10, id, NULL, NULL);
xfree(g);
*end = (char)c;
}