feat(highlight): implement CurSearch highlight

Adds a `CurSearch` highlight group to highlight the current search result under the cursor.
This commit is contained in:
Famiu Haque
2022-04-11 22:20:24 +06:00
parent 3f2e9298bd
commit b16afe4d55
9 changed files with 87 additions and 12 deletions

View File

@@ -63,6 +63,7 @@ typedef enum {
HLF_E, // error messages
HLF_I, // incremental search
HLF_L, // last search string
HLF_LC, // current search match
HLF_M, // "--More--" message
HLF_CM, // Mode (e.g., "-- INSERT --")
HLF_N, // line number for ":number" and ":#" commands
@@ -123,6 +124,7 @@ EXTERN const char *hlf_names[] INIT(= {
[HLF_E] = "ErrorMsg",
[HLF_I] = "IncSearch",
[HLF_L] = "Search",
[HLF_LC] = "CurSearch",
[HLF_M] = "MoreMsg",
[HLF_CM] = "ModeMsg",
[HLF_N] = "LineNr",

View File

@@ -1927,7 +1927,7 @@ void highlight_changed(void)
}
highlight_attr[hlf] = hl_get_ui_attr(hlf, final_id,
hlf == HLF_INACTIVE);
(hlf == HLF_INACTIVE || hlf == HLF_LC));
if (highlight_attr[hlf] != highlight_attr_last[hlf]) {
if (hlf == HLF_MSG) {

View File

@@ -4,6 +4,7 @@
// match.c: functions for highlighting matches
#include <stdbool.h>
#include "nvim/buffer_defs.h"
#include "nvim/charset.h"
#include "nvim/fold.h"
#include "nvim/highlight_group.h"
@@ -667,7 +668,16 @@ int update_search_hl(win_T *wp, linenr_T lnum, colnr_T col, char_u **line, match
if (shl->endcol < next_col) {
shl->endcol = next_col;
}
shl->attr_cur = shl->attr;
// Use "CurSearch" highlight for current search match
if (shl == search_hl
&& (HL_ATTR(HLF_LC) || wp->w_hl_ids[HLF_LC])
&& wp->w_cursor.lnum == lnum
&& wp->w_cursor.col >= shl->startcol
&& wp->w_cursor.col < shl->endcol) {
shl->attr_cur = win_hl_attr(wp, HLF_LC) ? win_hl_attr(wp, HLF_LC) : HL_ATTR(HLF_LC);
} else {
shl->attr_cur = shl->attr;
}
// Match with the "Conceal" group results in hiding
// the match.
if (cur != NULL

View File

@@ -95,27 +95,31 @@ static void comp_botline(win_T *wp)
win_check_anchored_floats(wp);
}
/// Redraw when w_cline_row changes and 'relativenumber' or 'cursorline' is set.
/// Redraw when w_cline_row changes and 'relativenumber' or 'cursorline' is set
/// or if the 'CurSearch' highlight is defined.
/// Also when concealing is on and 'concealcursor' is not active.
void redraw_for_cursorline(win_T *wp)
FUNC_ATTR_NONNULL_ALL
{
if ((wp->w_valid & VALID_CROW) == 0 && !pum_visible()
&& (wp->w_p_rnu || win_cursorline_standout(wp))) {
// win_line() will redraw the number column and cursorline only.
&& (wp->w_p_rnu || win_cursorline_standout(wp)
|| HL_ATTR(HLF_LC) || wp->w_hl_ids[HLF_LC])) {
// win_line() will redraw the number column and cursorline only
// and also update the CurSearch highlight (if needed).
redraw_later(wp, VALID);
}
}
/// Redraw when w_virtcol changes and 'cursorcolumn' is set or 'cursorlineopt'
/// contains "screenline".
/// contains "screenline" or when the 'CurSearch' highlight is defined.
/// Also when concealing is on and 'concealcursor' is active.
static void redraw_for_cursorcolumn(win_T *wp)
FUNC_ATTR_NONNULL_ALL
{
if ((wp->w_valid & VALID_VIRTCOL) == 0 && !pum_visible()) {
if (wp->w_p_cuc) {
// When 'cursorcolumn' is set need to redraw with SOME_VALID.
if (wp->w_p_cuc || HL_ATTR(HLF_LC) || wp->w_hl_ids[HLF_LC]) {
// When 'cursorcolumn' is set or 'CurSearch' is defined
// need to redraw with SOME_VALID.
redraw_later(wp, SOME_VALID);
} else if (wp->w_p_cul && (wp->w_p_culopt_flags & CULOPT_SCRLINE)) {
// When 'cursorlineopt' contains "screenline" need to redraw with VALID.