refactor: move statusline code from buffer.c and [draw]screen.c to new file

problem: code for drawing statusline is arbitrarily spreadout between drawscreen.c, screen.c and buffer.c
solution: move it to a new file statusline.c

- rename archaic internal name "status match" to public name "wildmenu"
- showruler() does not show the ruler. it show anything which displays
  info about the cursor. Rename it accordingy.
This commit is contained in:
bfredl
2022-08-19 12:37:58 +02:00
parent b2f979b30b
commit 6adc2ae7e0
14 changed files with 1873 additions and 1825 deletions

View File

@@ -57,6 +57,7 @@
#include "nvim/popupmenu.h" #include "nvim/popupmenu.h"
#include "nvim/runtime.h" #include "nvim/runtime.h"
#include "nvim/state.h" #include "nvim/state.h"
#include "nvim/statusline.h"
#include "nvim/types.h" #include "nvim/types.h"
#include "nvim/ui.h" #include "nvim/ui.h"
#include "nvim/vim.h" #include "nvim/vim.h"

File diff suppressed because it is too large Load Diff

View File

@@ -62,6 +62,9 @@ enum bfa_values {
BFA_IGNORE_ABORT = 8, // do not abort for aborting() BFA_IGNORE_ABORT = 8, // do not abort for aborting()
}; };
EXTERN char *msg_loclist INIT(= N_("[Location List]"));
EXTERN char *msg_qflist INIT(= N_("[Quickfix List]"));
#ifdef INCLUDE_GENERATED_DECLARATIONS #ifdef INCLUDE_GENERATED_DECLARATIONS
# include "buffer.h.generated.h" # include "buffer.h.generated.h"
#endif #endif

View File

@@ -75,6 +75,7 @@
#include "nvim/popupmenu.h" #include "nvim/popupmenu.h"
#include "nvim/profile.h" #include "nvim/profile.h"
#include "nvim/regexp.h" #include "nvim/regexp.h"
#include "nvim/statusline.h"
#include "nvim/syntax.h" #include "nvim/syntax.h"
#include "nvim/ui_compositor.h" #include "nvim/ui_compositor.h"
#include "nvim/undo.h" #include "nvim/undo.h"
@@ -736,190 +737,10 @@ static void win_redr_border(win_T *wp)
} }
} }
/// Redraw the status line of window `wp`. /// Show current cursor info in ruler and various other places
///
/// If inversion is possible we use it. Else '=' characters are used.
static void win_redr_status(win_T *wp)
{
int row;
int col;
char_u *p;
int len;
int fillchar;
int attr;
int width;
int this_ru_col;
bool is_stl_global = global_stl_height() > 0;
static bool busy = false;
// May get here recursively when 'statusline' (indirectly)
// invokes ":redrawstatus". Simply ignore the call then.
if (busy
// Also ignore if wildmenu is showing.
|| (wild_menu_showing != 0 && !ui_has(kUIWildmenu))) {
return;
}
busy = true;
wp->w_redr_status = false;
if (wp->w_status_height == 0 && !(is_stl_global && wp == curwin)) {
// no status line, either global statusline is enabled or the window is a last window
redraw_cmdline = true;
} else if (!redrawing()) {
// Don't redraw right now, do it later. Don't update status line when
// popup menu is visible and may be drawn over it
wp->w_redr_status = true;
} else if (*p_stl != NUL || *wp->w_p_stl != NUL) {
// redraw custom status line
redraw_custom_statusline(wp);
} else {
fillchar = fillchar_status(&attr, wp);
width = is_stl_global ? Columns : wp->w_width;
get_trans_bufname(wp->w_buffer);
p = NameBuff;
len = (int)STRLEN(p);
if (bt_help(wp->w_buffer)
|| wp->w_p_pvw
|| bufIsChanged(wp->w_buffer)
|| wp->w_buffer->b_p_ro) {
*(p + len++) = ' ';
}
if (bt_help(wp->w_buffer)) {
snprintf((char *)p + len, MAXPATHL - (size_t)len, "%s", _("[Help]"));
len += (int)STRLEN(p + len);
}
if (wp->w_p_pvw) {
snprintf((char *)p + len, MAXPATHL - (size_t)len, "%s", _("[Preview]"));
len += (int)STRLEN(p + len);
}
if (bufIsChanged(wp->w_buffer)) {
snprintf((char *)p + len, MAXPATHL - (size_t)len, "%s", "[+]");
len += (int)STRLEN(p + len);
}
if (wp->w_buffer->b_p_ro) {
snprintf((char *)p + len, MAXPATHL - (size_t)len, "%s", _("[RO]"));
// len += (int)STRLEN(p + len); // dead assignment
}
this_ru_col = ru_col - (Columns - width);
if (this_ru_col < (width + 1) / 2) {
this_ru_col = (width + 1) / 2;
}
if (this_ru_col <= 1) {
p = (char_u *)"<"; // No room for file name!
len = 1;
} else {
int clen = 0, i;
// Count total number of display cells.
clen = (int)mb_string2cells((char *)p);
// Find first character that will fit.
// Going from start to end is much faster for DBCS.
for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
i += utfc_ptr2len((char *)p + i)) {
clen -= utf_ptr2cells((char *)p + i);
}
len = clen;
if (i > 0) {
p = p + i - 1;
*p = '<';
len++;
}
}
row = is_stl_global ? (Rows - (int)p_ch - 1) : W_ENDROW(wp);
col = is_stl_global ? 0 : wp->w_wincol;
grid_puts(&default_grid, p, row, col, attr);
grid_fill(&default_grid, row, row + 1, len + col,
this_ru_col + col, fillchar, fillchar, attr);
if (get_keymap_str(wp, "<%s>", (char *)NameBuff, MAXPATHL)
&& this_ru_col - len > (int)(STRLEN(NameBuff) + 1)) {
grid_puts(&default_grid, NameBuff, row,
(int)((size_t)this_ru_col - STRLEN(NameBuff) - 1), attr);
}
win_redr_ruler(wp, true);
}
// May need to draw the character below the vertical separator.
if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing()) {
if (stl_connected(wp)) {
fillchar = fillchar_status(&attr, wp);
} else {
fillchar = fillchar_vsep(wp, &attr);
}
grid_putchar(&default_grid, fillchar, W_ENDROW(wp), W_ENDCOL(wp), attr);
}
busy = false;
}
/// Redraw the status line according to 'statusline' and take care of any
/// errors encountered.
static void redraw_custom_statusline(win_T *wp)
{
static bool entered = false;
int saved_did_emsg = did_emsg;
// When called recursively return. This can happen when the statusline
// contains an expression that triggers a redraw.
if (entered) {
return;
}
entered = true;
did_emsg = false;
win_redr_custom(wp, false, false);
if (did_emsg) {
// When there is an error disable the statusline, otherwise the
// display is messed up with errors and a redraw triggers the problem
// again and again.
set_string_option_direct("statusline", -1, "",
OPT_FREE | (*wp->w_p_stl != NUL
? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
}
did_emsg |= saved_did_emsg;
entered = false;
}
static void win_redr_winbar(win_T *wp)
{
static bool entered = false;
// Return when called recursively. This can happen when the winbar contains an expression
// that triggers a redraw.
if (entered) {
return;
}
entered = true;
if (wp->w_winbar_height == 0 || !redrawing()) {
// Do nothing.
} else if (*p_wbr != NUL || *wp->w_p_wbr != NUL) {
int saved_did_emsg = did_emsg;
did_emsg = false;
win_redr_custom(wp, true, false);
if (did_emsg) {
// When there is an error disable the winbar, otherwise the
// display is messed up with errors and a redraw triggers the problem
// again and again.
set_string_option_direct("winbar", -1, "",
OPT_FREE | (*wp->w_p_stl != NUL
? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
}
did_emsg |= saved_did_emsg;
}
entered = false;
}
/// Show current status info in ruler and various other places
/// ///
/// @param always if false, only show ruler if position has changed. /// @param always if false, only show ruler if position has changed.
void showruler(bool always) void show_cursor_info(bool always)
{ {
if (!always && !redrawing()) { if (!always && !redrawing()) {
return; return;
@@ -2303,6 +2124,26 @@ void redraw_statuslines(void)
} }
} }
/// Redraw all status lines at the bottom of frame "frp".
void win_redraw_last_status(const frame_T *frp)
FUNC_ATTR_NONNULL_ARG(1)
{
if (frp->fr_layout == FR_LEAF) {
frp->fr_win->w_redr_status = true;
} else if (frp->fr_layout == FR_ROW) {
FOR_ALL_FRAMES(frp, frp->fr_child) {
win_redraw_last_status(frp);
}
} else {
assert(frp->fr_layout == FR_COL);
frp = frp->fr_child;
while (frp->fr_next != NULL) {
frp = frp->fr_next;
}
win_redraw_last_status(frp);
}
}
/// Changed something in the current window, at buffer line "lnum", that /// Changed something in the current window, at buffer line "lnum", that
/// requires that line and possibly other lines to be redrawn. /// requires that line and possibly other lines to be redrawn.
/// Used when entering/leaving Insert mode with the cursor on a folded line. /// Used when entering/leaving Insert mode with the cursor on a folded line.

View File

@@ -1349,7 +1349,7 @@ void ins_redraw(bool ready)
} else if (clear_cmdline || redraw_cmdline) { } else if (clear_cmdline || redraw_cmdline) {
showmode(); // clear cmdline and show mode showmode(); // clear cmdline and show mode
} }
showruler(false); show_cursor_info(false);
setcursor(); setcursor();
emsg_on_display = false; // may remove error message now emsg_on_display = false; // may remove error message now
} }

View File

@@ -3976,7 +3976,7 @@ static int do_sub(exarg_T *eap, proftime_T timeout, long cmdpreview_ns, handle_T
_("replace with %s (y/n/a/q/l/^E/^Y)?"), sub); _("replace with %s (y/n/a/q/l/^E/^Y)?"), sub);
msg_no_more = false; msg_no_more = false;
msg_scroll = (int)i; msg_scroll = (int)i;
showruler(true); show_cursor_info(true);
ui_cursor_goto(msg_row, msg_col); ui_cursor_goto(msg_row, msg_col);
RedrawingDisabled = temp; RedrawingDisabled = temp;

View File

@@ -4264,7 +4264,7 @@ char_u *ExpandOne(expand_T *xp, char_u *str, char_u *orig, int options, int mode
compl_selected = findex; compl_selected = findex;
cmdline_pum_display(false); cmdline_pum_display(false);
} else if (p_wmnu) { } else if (p_wmnu) {
win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files, findex, cmd_showtail); redraw_wildmenu(xp, xp->xp_numfiles, xp->xp_files, findex, cmd_showtail);
} }
if (findex == -1) { if (findex == -1) {
return vim_strsave(orig_save); return vim_strsave(orig_save);
@@ -4679,7 +4679,7 @@ static int showmatches(expand_T *xp, int wildmenu)
if (got_int) { if (got_int) {
got_int = false; // only int. the completion, not the cmd line got_int = false; // only int. the completion, not the cmd line
} else if (wildmenu) { } else if (wildmenu) {
win_redr_status_matches(xp, num_files, files_found, -1, showtail); redraw_wildmenu(xp, num_files, files_found, -1, showtail);
} else { } else {
// find the length of the longest file name // find the length of the longest file name
maxlen = 0; maxlen = 0;
@@ -4791,7 +4791,7 @@ static int showmatches(expand_T *xp, int wildmenu)
return EXPAND_OK; return EXPAND_OK;
} }
/// Private path_tail for showmatches() (and win_redr_status_matches()): /// Private path_tail for showmatches() (and redraw_wildmenu()):
/// Find tail of file name path, but ignore trailing "/". /// Find tail of file name path, but ignore trailing "/".
char *sm_gettail(char *s, bool eager) char *sm_gettail(char *s, bool eager)
{ {

View File

@@ -33,6 +33,7 @@
#include "nvim/os/os.h" #include "nvim/os/os.h"
#include "nvim/path.h" #include "nvim/path.h"
#include "nvim/runtime.h" #include "nvim/runtime.h"
#include "nvim/statusline.h"
#include "nvim/strings.h" #include "nvim/strings.h"
#include "nvim/syntax.h" #include "nvim/syntax.h"
#include "nvim/ui.h" #include "nvim/ui.h"

View File

@@ -1322,7 +1322,7 @@ static void normal_redraw(NormalState *s)
did_emsg = false; did_emsg = false;
msg_didany = false; // reset lines_left in msg_start() msg_didany = false; // reset lines_left in msg_start()
may_clear_sb_text(); // clear scroll-back text on next msg may_clear_sb_text(); // clear scroll-back text on next msg
showruler(false); show_cursor_info(false);
setcursor(); setcursor();
} }

View File

@@ -16,6 +16,7 @@
#include "nvim/buffer.h" #include "nvim/buffer.h"
#include "nvim/charset.h" #include "nvim/charset.h"
#include "nvim/cursor.h" #include "nvim/cursor.h"
#include "nvim/drawscreen.h"
#include "nvim/eval.h" #include "nvim/eval.h"
#include "nvim/ex_getln.h" #include "nvim/ex_getln.h"
#include "nvim/extmark.h" #include "nvim/extmark.h"
@@ -34,6 +35,7 @@
#include "nvim/screen.h" #include "nvim/screen.h"
#include "nvim/search.h" #include "nvim/search.h"
#include "nvim/state.h" #include "nvim/state.h"
#include "nvim/statusline.h"
#include "nvim/ui_compositor.h" #include "nvim/ui_compositor.h"
#include "nvim/undo.h" #include "nvim/undo.h"
#include "nvim/window.h" #include "nvim/window.h"
@@ -249,7 +251,7 @@ void rl_mirror(char_u *str)
} }
/// Get the length of an item as it will be shown in the status line. /// Get the length of an item as it will be shown in the status line.
static int status_match_len(expand_T *xp, char_u *s) static int wildmenu_match_len(expand_T *xp, char_u *s)
{ {
int len = 0; int len = 0;
@@ -262,7 +264,7 @@ static int status_match_len(expand_T *xp, char_u *s)
} }
while (*s != NUL) { while (*s != NUL) {
s += skip_status_match_char(xp, s); s += skip_wildmenu_char(xp, s);
len += ptr2cells((char *)s); len += ptr2cells((char *)s);
MB_PTR_ADV(s); MB_PTR_ADV(s);
} }
@@ -270,29 +272,9 @@ static int status_match_len(expand_T *xp, char_u *s)
return len; return len;
} }
/// Redraw all status lines at the bottom of frame "frp". /// Return the number of characters that should be skipped in the wildmenu
void win_redraw_last_status(const frame_T *frp)
FUNC_ATTR_NONNULL_ARG(1)
{
if (frp->fr_layout == FR_LEAF) {
frp->fr_win->w_redr_status = true;
} else if (frp->fr_layout == FR_ROW) {
FOR_ALL_FRAMES(frp, frp->fr_child) {
win_redraw_last_status(frp);
}
} else {
assert(frp->fr_layout == FR_COL);
frp = frp->fr_child;
while (frp->fr_next != NULL) {
frp = frp->fr_next;
}
win_redraw_last_status(frp);
}
}
/// Return the number of characters that should be skipped in a status match.
/// These are backslashes used for escaping. Do show backslashes in help tags. /// These are backslashes used for escaping. Do show backslashes in help tags.
static int skip_status_match_char(expand_T *xp, char_u *s) static int skip_wildmenu_char(expand_T *xp, char_u *s)
{ {
if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP) if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
|| ((xp->xp_context == EXPAND_MENUS || ((xp->xp_context == EXPAND_MENUS
@@ -300,6 +282,10 @@ static int skip_status_match_char(expand_T *xp, char_u *s)
&& (s[0] == '\t' && (s[0] == '\t'
|| (s[0] == '\\' && s[1] != NUL)))) { || (s[0] == '\\' && s[1] != NUL)))) {
#ifndef BACKSLASH_IN_FILENAME #ifndef BACKSLASH_IN_FILENAME
// TODO(bfredl): Why in the actual fuck are we special casing the
// shell variety deep in the redraw logic? Shell special snowflakiness
// should already be eliminated multiple layers before reaching the
// screen infracstructure.
if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!') { if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!') {
return 2; return 2;
} }
@@ -316,7 +302,7 @@ static int skip_status_match_char(expand_T *xp, char_u *s)
/// If inversion is possible we use it. Else '=' characters are used. /// If inversion is possible we use it. Else '=' characters are used.
/// ///
/// @param matches list of matches /// @param matches list of matches
void win_redr_status_matches(expand_T *xp, int num_matches, char **matches, int match, int showtail) void redraw_wildmenu(expand_T *xp, int num_matches, char **matches, int match, int showtail)
{ {
#define L_MATCH(m) (showtail ? sm_gettail(matches[m], false) : matches[m]) #define L_MATCH(m) (showtail ? sm_gettail(matches[m], false) : matches[m])
int row; int row;
@@ -347,7 +333,7 @@ void win_redr_status_matches(expand_T *xp, int num_matches, char **matches, int
highlight = false; highlight = false;
} }
// count 1 for the ending ">" // count 1 for the ending ">"
clen = status_match_len(xp, (char_u *)L_MATCH(match)) + 3; clen = wildmenu_match_len(xp, (char_u *)L_MATCH(match)) + 3;
if (match == 0) { if (match == 0) {
first_match = 0; first_match = 0;
} else if (match < first_match) { } else if (match < first_match) {
@@ -357,7 +343,7 @@ void win_redr_status_matches(expand_T *xp, int num_matches, char **matches, int
} else { } else {
// check if match fits on the screen // check if match fits on the screen
for (i = first_match; i < match; i++) { for (i = first_match; i < match; i++) {
clen += status_match_len(xp, (char_u *)L_MATCH(i)) + 2; clen += wildmenu_match_len(xp, (char_u *)L_MATCH(i)) + 2;
} }
if (first_match > 0) { if (first_match > 0) {
clen += 2; clen += 2;
@@ -368,7 +354,7 @@ void win_redr_status_matches(expand_T *xp, int num_matches, char **matches, int
// if showing the last match, we can add some on the left // if showing the last match, we can add some on the left
clen = 2; clen = 2;
for (i = match; i < num_matches; i++) { for (i = match; i < num_matches; i++) {
clen += status_match_len(xp, (char_u *)L_MATCH(i)) + 2; clen += wildmenu_match_len(xp, (char_u *)L_MATCH(i)) + 2;
if ((long)clen >= Columns) { if ((long)clen >= Columns) {
break; break;
} }
@@ -380,7 +366,7 @@ void win_redr_status_matches(expand_T *xp, int num_matches, char **matches, int
} }
if (add_left) { if (add_left) {
while (first_match > 0) { while (first_match > 0) {
clen += status_match_len(xp, (char_u *)L_MATCH(first_match - 1)) + 2; clen += wildmenu_match_len(xp, (char_u *)L_MATCH(first_match - 1)) + 2;
if ((long)clen >= Columns) { if ((long)clen >= Columns) {
break; break;
} }
@@ -400,7 +386,7 @@ void win_redr_status_matches(expand_T *xp, int num_matches, char **matches, int
clen = len; clen = len;
i = first_match; i = first_match;
while (clen + status_match_len(xp, (char_u *)L_MATCH(i)) + 2 < Columns) { while (clen + wildmenu_match_len(xp, (char_u *)L_MATCH(i)) + 2 < Columns) {
if (i == match) { if (i == match) {
selstart = buf + len; selstart = buf + len;
selstart_col = clen; selstart_col = clen;
@@ -417,7 +403,7 @@ void win_redr_status_matches(expand_T *xp, int num_matches, char **matches, int
clen += l; clen += l;
} else { } else {
for (; *s != NUL; s++) { for (; *s != NUL; s++) {
s += skip_status_match_char(xp, s); s += skip_wildmenu_char(xp, s);
clen += ptr2cells((char *)s); clen += ptr2cells((char *)s);
if ((l = utfc_ptr2len((char *)s)) > 1) { if ((l = utfc_ptr2len((char *)s)) > 1) {
STRNCPY(buf + len, s, l); // NOLINT(runtime/printf) STRNCPY(buf + len, s, l); // NOLINT(runtime/printf)
@@ -561,223 +547,6 @@ bool get_keymap_str(win_T *wp, char *fmt, char *buf, int len)
return buf[0] != NUL; return buf[0] != NUL;
} }
/// Redraw the status line, window bar or ruler of window "wp".
/// When "wp" is NULL redraw the tab pages line from 'tabline'.
void win_redr_custom(win_T *wp, bool draw_winbar, bool draw_ruler)
{
static bool entered = false;
int attr;
int curattr;
int row;
int col = 0;
int maxwidth;
int width;
int n;
int len;
int fillchar;
char buf[MAXPATHL];
char_u *stl;
char *p;
stl_hlrec_t *hltab;
StlClickRecord *tabtab;
int use_sandbox = false;
win_T *ewp;
int p_crb_save;
bool is_stl_global = global_stl_height() > 0;
ScreenGrid *grid = &default_grid;
// There is a tiny chance that this gets called recursively: When
// redrawing a status line triggers redrawing the ruler or tabline.
// Avoid trouble by not allowing recursion.
if (entered) {
return;
}
entered = true;
// setup environment for the task at hand
if (wp == NULL) {
// Use 'tabline'. Always at the first line of the screen.
stl = p_tal;
row = 0;
fillchar = ' ';
attr = HL_ATTR(HLF_TPF);
maxwidth = Columns;
use_sandbox = was_set_insecurely(wp, "tabline", 0);
} else if (draw_winbar) {
stl = (char_u *)((*wp->w_p_wbr != NUL) ? wp->w_p_wbr : p_wbr);
row = -1; // row zero is first row of text
col = 0;
grid = &wp->w_grid;
grid_adjust(&grid, &row, &col);
if (row < 0) {
return;
}
fillchar = wp->w_p_fcs_chars.wbr;
attr = (wp == curwin) ? win_hl_attr(wp, HLF_WBR) : win_hl_attr(wp, HLF_WBRNC);
maxwidth = wp->w_width_inner;
use_sandbox = was_set_insecurely(wp, "winbar", 0);
stl_clear_click_defs(wp->w_winbar_click_defs, (long)wp->w_winbar_click_defs_size);
// Allocate / resize the click definitions array for winbar if needed.
if (wp->w_winbar_height && wp->w_winbar_click_defs_size < (size_t)maxwidth) {
xfree(wp->w_winbar_click_defs);
wp->w_winbar_click_defs_size = (size_t)maxwidth;
wp->w_winbar_click_defs = xcalloc(wp->w_winbar_click_defs_size, sizeof(StlClickRecord));
}
} else {
row = is_stl_global ? (Rows - (int)p_ch - 1) : W_ENDROW(wp);
fillchar = fillchar_status(&attr, wp);
maxwidth = is_stl_global ? Columns : wp->w_width;
stl_clear_click_defs(wp->w_status_click_defs, (long)wp->w_status_click_defs_size);
// Allocate / resize the click definitions array for statusline if needed.
if (wp->w_status_click_defs_size < (size_t)maxwidth) {
xfree(wp->w_status_click_defs);
wp->w_status_click_defs_size = (size_t)maxwidth;
wp->w_status_click_defs = xcalloc(wp->w_status_click_defs_size, sizeof(StlClickRecord));
}
if (draw_ruler) {
stl = p_ruf;
// advance past any leading group spec - implicit in ru_col
if (*stl == '%') {
if (*++stl == '-') {
stl++;
}
if (atoi((char *)stl)) {
while (ascii_isdigit(*stl)) {
stl++;
}
}
if (*stl++ != '(') {
stl = p_ruf;
}
}
col = ru_col - (Columns - maxwidth);
if (col < (maxwidth + 1) / 2) {
col = (maxwidth + 1) / 2;
}
maxwidth = maxwidth - col;
if (!wp->w_status_height && !is_stl_global) {
grid = &msg_grid_adj;
row = Rows - 1;
maxwidth--; // writing in last column may cause scrolling
fillchar = ' ';
attr = HL_ATTR(HLF_MSG);
}
use_sandbox = was_set_insecurely(wp, "rulerformat", 0);
} else {
if (*wp->w_p_stl != NUL) {
stl = wp->w_p_stl;
} else {
stl = p_stl;
}
use_sandbox = was_set_insecurely(wp, "statusline", *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
}
col += is_stl_global ? 0 : wp->w_wincol;
}
if (maxwidth <= 0) {
goto theend;
}
// Temporarily reset 'cursorbind', we don't want a side effect from moving
// the cursor away and back.
ewp = wp == NULL ? curwin : wp;
p_crb_save = ewp->w_p_crb;
ewp->w_p_crb = false;
// Make a copy, because the statusline may include a function call that
// might change the option value and free the memory.
stl = vim_strsave(stl);
width =
build_stl_str_hl(ewp, buf, sizeof(buf), (char *)stl, use_sandbox,
fillchar, maxwidth, &hltab, &tabtab);
xfree(stl);
ewp->w_p_crb = p_crb_save;
// Make all characters printable.
p = transstr(buf, true);
len = (int)STRLCPY(buf, p, sizeof(buf));
len = (size_t)len < sizeof(buf) ? len : (int)sizeof(buf) - 1;
xfree(p);
// fill up with "fillchar"
while (width < maxwidth && len < (int)sizeof(buf) - 1) {
len += utf_char2bytes(fillchar, buf + len);
width++;
}
buf[len] = NUL;
// Draw each snippet with the specified highlighting.
grid_puts_line_start(grid, row);
curattr = attr;
p = buf;
for (n = 0; hltab[n].start != NULL; n++) {
int textlen = (int)(hltab[n].start - p);
grid_puts_len(grid, (char_u *)p, textlen, row, col, curattr);
col += vim_strnsize((char_u *)p, textlen);
p = hltab[n].start;
if (hltab[n].userhl == 0) {
curattr = attr;
} else if (hltab[n].userhl < 0) {
curattr = syn_id2attr(-hltab[n].userhl);
} else if (wp != NULL && wp != curwin && wp->w_status_height != 0) {
curattr = highlight_stlnc[hltab[n].userhl - 1];
} else {
curattr = highlight_user[hltab[n].userhl - 1];
}
}
// Make sure to use an empty string instead of p, if p is beyond buf + len.
grid_puts(grid, p >= buf + len ? (char_u *)"" : (char_u *)p, row, col,
curattr);
grid_puts_line_flush(false);
// Fill the tab_page_click_defs, w_status_click_defs or w_winbar_click_defs array for clicking
// in the tab page line, status line or window bar
StlClickDefinition *click_defs = (wp == NULL) ? tab_page_click_defs
: draw_winbar ? wp->w_winbar_click_defs
: wp->w_status_click_defs;
if (click_defs == NULL) {
goto theend;
}
col = 0;
len = 0;
p = buf;
StlClickDefinition cur_click_def = {
.type = kStlClickDisabled,
};
for (n = 0; tabtab[n].start != NULL; n++) {
len += vim_strnsize((char_u *)p, (int)(tabtab[n].start - p));
while (col < len) {
click_defs[col++] = cur_click_def;
}
p = (char *)tabtab[n].start;
cur_click_def = tabtab[n].def;
if ((wp != NULL) && !(cur_click_def.type == kStlClickDisabled
|| cur_click_def.type == kStlClickFuncRun)) {
// window bar and status line only support click functions
cur_click_def.type = kStlClickDisabled;
}
}
while (col < maxwidth) {
click_defs[col++] = cur_click_def;
}
theend:
entered = false;
}
/// Prepare for 'hlsearch' highlighting. /// Prepare for 'hlsearch' highlighting.
void start_search_hl(void) void start_search_hl(void)
{ {
@@ -1378,32 +1147,6 @@ void get_trans_bufname(buf_T *buf)
trans_characters((char *)NameBuff, MAXPATHL); trans_characters((char *)NameBuff, MAXPATHL);
} }
/// Get the character to use in a status line. Get its attributes in "*attr".
int fillchar_status(int *attr, win_T *wp)
{
int fill;
bool is_curwin = (wp == curwin);
if (is_curwin) {
*attr = win_hl_attr(wp, HLF_S);
fill = wp->w_p_fcs_chars.stl;
} else {
*attr = win_hl_attr(wp, HLF_SNC);
fill = wp->w_p_fcs_chars.stlnc;
}
// Use fill when there is highlighting, and highlighting of current
// window differs, or the fillchars differ, or this is not the
// current window
if (*attr != 0 && ((win_hl_attr(wp, HLF_S) != win_hl_attr(wp, HLF_SNC)
|| !is_curwin || ONE_WINDOW)
|| (wp->w_p_fcs_chars.stl != wp->w_p_fcs_chars.stlnc))) {
return fill;
}
if (is_curwin) {
return '^';
}
return '=';
}
/// Get the character to use in a separator between vertically split windows. /// Get the character to use in a separator between vertically split windows.
/// Get its attributes in "*attr". /// Get its attributes in "*attr".
int fillchar_vsep(win_T *wp, int *attr) int fillchar_vsep(win_T *wp, int *attr)
@@ -1433,175 +1176,6 @@ bool messaging(void)
return !(p_lz && char_avail() && !KeyTyped) && ui_has_messages(); return !(p_lz && char_avail() && !KeyTyped) && ui_has_messages();
} }
void win_redr_ruler(win_T *wp, bool always)
{
bool is_stl_global = global_stl_height() > 0;
static bool did_show_ext_ruler = false;
// If 'ruler' off, don't do anything
if (!p_ru) {
return;
}
// Check if cursor.lnum is valid, since win_redr_ruler() may be called
// after deleting lines, before cursor.lnum is corrected.
if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count) {
return;
}
// Don't draw the ruler while doing insert-completion, it might overwrite
// the (long) mode message.
if (wp == lastwin && lastwin->w_status_height == 0 && !is_stl_global) {
if (edit_submode != NULL) {
return;
}
}
if (*p_ruf && p_ch > 0 && !ui_has(kUIMessages)) {
const int called_emsg_before = called_emsg;
win_redr_custom(wp, false, true);
if (called_emsg > called_emsg_before) {
set_string_option_direct("rulerformat", -1, "", OPT_FREE, SID_ERROR);
}
return;
}
// Check if not in Insert mode and the line is empty (will show "0-1").
int empty_line = false;
if ((State & MODE_INSERT) == 0 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, false) == NUL) {
empty_line = true;
}
// Only draw the ruler when something changed.
validate_virtcol_win(wp);
if (redraw_cmdline
|| always
|| wp->w_cursor.lnum != wp->w_ru_cursor.lnum
|| wp->w_cursor.col != wp->w_ru_cursor.col
|| wp->w_virtcol != wp->w_ru_virtcol
|| wp->w_cursor.coladd != wp->w_ru_cursor.coladd
|| wp->w_topline != wp->w_ru_topline
|| wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
|| wp->w_topfill != wp->w_ru_topfill
|| empty_line != wp->w_ru_empty) {
int width;
int row;
int fillchar;
int attr;
int off;
bool part_of_status = false;
if (wp->w_status_height) {
row = W_ENDROW(wp);
fillchar = fillchar_status(&attr, wp);
off = wp->w_wincol;
width = wp->w_width;
part_of_status = true;
} else if (is_stl_global) {
row = Rows - (int)p_ch - 1;
fillchar = fillchar_status(&attr, wp);
off = 0;
width = Columns;
part_of_status = true;
} else {
row = Rows - 1;
fillchar = ' ';
attr = HL_ATTR(HLF_MSG);
width = Columns;
off = 0;
}
if (!part_of_status && !ui_has_messages()) {
return;
}
// In list mode virtcol needs to be recomputed
colnr_T virtcol = wp->w_virtcol;
if (wp->w_p_list && wp->w_p_lcs_chars.tab1 == NUL) {
wp->w_p_list = false;
getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
wp->w_p_list = true;
}
#define RULER_BUF_LEN 70
char buffer[RULER_BUF_LEN];
// Some sprintfs return the length, some return a pointer.
// To avoid portability problems we use strlen() here.
vim_snprintf(buffer, RULER_BUF_LEN, "%" PRId64 ",",
(wp->w_buffer->b_ml.ml_flags &
ML_EMPTY) ? (int64_t)0L : (int64_t)wp->w_cursor.lnum);
size_t len = STRLEN(buffer);
col_print(buffer + len, RULER_BUF_LEN - len,
empty_line ? 0 : (int)wp->w_cursor.col + 1,
(int)virtcol + 1);
// Add a "50%" if there is room for it.
// On the last line, don't print in the last column (scrolls the
// screen up on some terminals).
int i = (int)STRLEN(buffer);
get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
int o = i + vim_strsize(buffer + i + 1);
if (wp->w_status_height == 0 && !is_stl_global) { // can't use last char of screen
o++;
}
int this_ru_col = ru_col - (Columns - width);
if (this_ru_col < 0) {
this_ru_col = 0;
}
// Never use more than half the window/screen width, leave the other half
// for the filename.
if (this_ru_col < (width + 1) / 2) {
this_ru_col = (width + 1) / 2;
}
if (this_ru_col + o < width) {
// Need at least 3 chars left for get_rel_pos() + NUL.
while (this_ru_col + o < width && RULER_BUF_LEN > i + 4) {
i += utf_char2bytes(fillchar, buffer + i);
o++;
}
get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
}
if (ui_has(kUIMessages) && !part_of_status) {
MAXSIZE_TEMP_ARRAY(content, 1);
MAXSIZE_TEMP_ARRAY(chunk, 2);
ADD_C(chunk, INTEGER_OBJ(attr));
ADD_C(chunk, STRING_OBJ(cstr_as_string((char *)buffer)));
ADD_C(content, ARRAY_OBJ(chunk));
ui_call_msg_ruler(content);
did_show_ext_ruler = true;
} else {
if (did_show_ext_ruler) {
ui_call_msg_ruler((Array)ARRAY_DICT_INIT);
did_show_ext_ruler = false;
}
// Truncate at window boundary.
o = 0;
for (i = 0; buffer[i] != NUL; i += utfc_ptr2len(buffer + i)) {
o += utf_ptr2cells(buffer + i);
if (this_ru_col + o > width) {
buffer[i] = NUL;
break;
}
}
ScreenGrid *grid = part_of_status ? &default_grid : &msg_grid_adj;
grid_puts(grid, (char_u *)buffer, row, this_ru_col + off, attr);
grid_fill(grid, row, row + 1,
this_ru_col + off + (int)STRLEN(buffer), off + width, fillchar,
fillchar, attr);
}
wp->w_ru_cursor = wp->w_cursor;
wp->w_ru_virtcol = wp->w_virtcol;
wp->w_ru_empty = (char)empty_line;
wp->w_ru_topline = wp->w_topline;
wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
wp->w_ru_topfill = wp->w_topfill;
}
}
#define COL_RULER 17 // columns needed by standard ruler #define COL_RULER 17 // columns needed by standard ruler
/// Compute columns for ruler and shown command. 'sc_col' is also used to /// Compute columns for ruler and shown command. 'sc_col' is also used to

View File

@@ -2423,7 +2423,7 @@ void showmatch(int c)
curwin->w_cursor = mpos; // move to matching char curwin->w_cursor = mpos; // move to matching char
*so = 0; // don't use 'scrolloff' here *so = 0; // don't use 'scrolloff' here
*siso = 0; // don't use 'sidescrolloff' here *siso = 0; // don't use 'sidescrolloff' here
showruler(false); show_cursor_info(false);
setcursor(); setcursor();
ui_flush(); ui_flush();
// Restore dollar_vcol(), because setcursor() may call curs_rows() // Restore dollar_vcol(), because setcursor() may call curs_rows()

1807
src/nvim/statusline.c Normal file

File diff suppressed because it is too large Load Diff

10
src/nvim/statusline.h Normal file
View File

@@ -0,0 +1,10 @@
#ifndef NVIM_STATUSLINE_H
#define NVIM_STATUSLINE_H
#include "nvim/buffer_defs.h"
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "statusline.h.generated.h"
#endif
#endif // NVIM_STATUSLINE_H

View File

@@ -9,6 +9,7 @@ local NULL = helpers.NULL
local globals = helpers.cimport("./src/nvim/globals.h") local globals = helpers.cimport("./src/nvim/globals.h")
local buffer = helpers.cimport("./src/nvim/buffer.h") local buffer = helpers.cimport("./src/nvim/buffer.h")
local stl = helpers.cimport("./src/nvim/statusline.h")
describe('buffer functions', function() describe('buffer functions', function()
@@ -228,7 +229,7 @@ describe('buffer functions', function()
local fillchar = arg.fillchar or (' '):byte() local fillchar = arg.fillchar or (' '):byte()
local maximum_cell_count = arg.maximum_cell_count or buffer_byte_size local maximum_cell_count = arg.maximum_cell_count or buffer_byte_size
return buffer.build_stl_str_hl(globals.curwin, return stl.build_stl_str_hl(globals.curwin,
output_buffer, output_buffer,
buffer_byte_size, buffer_byte_size,
to_cstr(pat), to_cstr(pat),