From 680cfeb074fe8e7962d69e047e11b98f2b4c2933 Mon Sep 17 00:00:00 2001 From: Olivier Roques Date: Fri, 21 May 2021 11:31:23 +0200 Subject: [PATCH] Fix #14: better active window detection --- lua/hardline.lua | 56 ++++++++++++++++++----------------------- lua/hardline/common.lua | 7 +----- plugin/hardline.vim | 14 +++++++++++ 3 files changed, 40 insertions(+), 37 deletions(-) diff --git a/lua/hardline.lua b/lua/hardline.lua index 740763f..c5a6323 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) +local function get_section_state(section, active) if section.class == 'mode' then - if common.is_active() then + if active then local mode = common.modes[fn.mode()] or common.modes['?'] return mode.state end @@ -115,40 +115,35 @@ local function get_section_state(section) end return state end - return common.is_active() and 'active' or 'inactive' + return active and 'active' or 'inactive' end -local function highlight_section(section) - if type(section) ~= 'table' then - return section +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) 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() - 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)) +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)) end -------------------- BUFFERLINE ---------------------------- @@ -191,8 +186,7 @@ end local function set_statusline() o.showmode = false - o.statusline = [[%!luaeval('require("hardline").update_statusline()')]] - wo.statusline = o.statusline + o.statusline = [[%!luaeval('require("hardline").update_statusline(true)')]] end local function set_bufferline() diff --git a/lua/hardline/common.lua b/lua/hardline/common.lua index 446e377..44ace8a 100644 --- a/lua/hardline/common.lua +++ b/lua/hardline/common.lua @@ -1,5 +1,4 @@ -local cmd, fn = vim.cmd, vim.fn -local g = vim.g +local cmd = vim.cmd local fmt = string.format local M = {} @@ -24,10 +23,6 @@ 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 c24cddc..6883541 100644 --- a/plugin/hardline.vim +++ b/plugin/hardline.vim @@ -7,3 +7,17 @@ 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