From 267ecc7cd13c674f8c7286898334d30a07e31919 Mon Sep 17 00:00:00 2001 From: Olivier Roques Date: Fri, 21 May 2021 20:19:33 +0200 Subject: [PATCH] Revert "Fix #14: better active window detection" This reverts commit 680cfeb074fe8e7962d69e047e11b98f2b4c2933. --- lua/hardline.lua | 56 +++++++++++++++++++++++------------------ lua/hardline/common.lua | 7 +++++- plugin/hardline.vim | 14 ----------- 3 files changed, 37 insertions(+), 40 deletions(-) diff --git a/lua/hardline.lua b/lua/hardline.lua index c5a6323..740763f 100644 --- a/lua/hardline.lua +++ b/lua/hardline.lua @@ -98,9 +98,9 @@ local function remove_hidden_sections(sections) end -------------------- SECTION HIGHLIGHTING ------------------ -local function get_section_state(section, active) +local function get_section_state(section) if section.class == 'mode' then - if active then + if common.is_active() then local mode = common.modes[fn.mode()] or common.modes['?'] return mode.state end @@ -115,35 +115,40 @@ local function get_section_state(section, active) end return state end - return active and 'active' or 'inactive' + return common.is_active() and 'active' or 'inactive' end -local function highlight_sections(sections, active) - local highlight_section = function(section) - if type(section) ~= 'table' then - return section - end - if section.class == 'none' then - return section.item - end - local state = get_section_state(section, active) - local hlgroup = fmt('Hardline_%s_%s', section.class, state) - if fn.hlexists(hlgroup) == 0 then - return section.item - end - return fmt('%%#%s#%s%%*', hlgroup, section.item) +local function highlight_section(section) + if type(section) ~= 'table' then + return section end + if section.class == 'none' then + return section.item + end + local state = get_section_state(section) + local hlgroup = fmt('Hardline_%s_%s', section.class, state) + if fn.hlexists(hlgroup) == 0 then + return section.item + end + return fmt('%%#%s#%s%%*', hlgroup, section.item) +end + +local function highlight_sections(sections) return vim.tbl_map(highlight_section, sections) end -------------------- STATUSLINE ---------------------------- -function M.update_statusline(active) - sections = M.options.sections - sections = remove_hidden_sections(sections) - sections = load_sections(sections) - sections = remove_empty_sections(sections) - sections = aggregate_sections(sections) - return table.concat(highlight_sections(sections, active)) +function M.update_statusline() + local sections = cache.previous + if common.is_active() or not sections then + sections = M.options.sections + sections = remove_hidden_sections(sections) + sections = load_sections(sections) + sections = remove_empty_sections(sections) + sections = aggregate_sections(sections) + cache.previous, cache.current = cache.current, sections + end + return table.concat(highlight_sections(sections)) end -------------------- BUFFERLINE ---------------------------- @@ -186,7 +191,8 @@ end local function set_statusline() o.showmode = false - o.statusline = [[%!luaeval('require("hardline").update_statusline(true)')]] + o.statusline = [[%!luaeval('require("hardline").update_statusline()')]] + wo.statusline = o.statusline end local function set_bufferline() diff --git a/lua/hardline/common.lua b/lua/hardline/common.lua index 44ace8a..446e377 100644 --- a/lua/hardline/common.lua +++ b/lua/hardline/common.lua @@ -1,4 +1,5 @@ -local cmd = vim.cmd +local cmd, fn = vim.cmd, vim.fn +local g = vim.g local fmt = string.format local M = {} @@ -23,6 +24,10 @@ function M.echo(hlgroup, msg) cmd('echohl None') end +function M.is_active() + return g.statusline_winid == fn.win_getid() +end + function M.set_cache_autocmds(augroup) cmd(fmt('augroup %s', augroup)) cmd('autocmd!') diff --git a/plugin/hardline.vim b/plugin/hardline.vim index 6883541..c24cddc 100644 --- a/plugin/hardline.vim +++ b/plugin/hardline.vim @@ -7,17 +7,3 @@ if exists('g:loaded_hardline') endif let g:loaded_hardline = 1 - -function! ActiveLine() - return luaeval('require("hardline").update_statusline(true)') -endfunction - -function! InactiveLine() - return luaeval('require("hardline").update_statusline(false)') -endfunction - -augroup hardline - autocmd! - autocmd WinEnter,BufEnter * setlocal statusline=%!ActiveLine() - autocmd WinLeave,BufLeave * setlocal statusline=%!InactiveLine() -augroup END