From eadf8930fb18b8c9742631af01de61dac44a63d7 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Thu, 8 Jun 2017 07:05:44 +0100 Subject: [PATCH] Add `-width ` support in the wrap highlighter Will always wrap at the minimum between max_width and actual window width. Fixes #1424 --- README.asciidoc | 3 ++- doc/manpages/highlighters.asciidoc | 3 +++ src/highlighters.cc | 21 ++++++++++++++------- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/README.asciidoc b/README.asciidoc index fc5e3eab7..5cbdc0343 100644 --- a/README.asciidoc +++ b/README.asciidoc @@ -1251,7 +1251,8 @@ General highlighters are: -hlcursor switch will highlight the cursor line with a separate face. With the -separator switch one can specify a string to separate the line numbers column with the rest of the buffer, default is `|`. - * `wrap \<-word>`: Soft wrap buffer content to window width, wrap at word boundaries if `-word` is specified. + * `wrap \<-word> \<-width `: Soft wrap buffer content to the smallest of window width and + max_width. Wrap at word boundaries if `-word` is specified. * `fill `: fill using given face, mostly useful with <> * `ranges `: use the data in the range-specs option of the given name to highlight the buffer. The string part of the is interpretted as a face to apply to the range. diff --git a/doc/manpages/highlighters.asciidoc b/doc/manpages/highlighters.asciidoc index b826efee4..e421813c6 100644 --- a/doc/manpages/highlighters.asciidoc +++ b/doc/manpages/highlighters.asciidoc @@ -90,6 +90,9 @@ General highlighters *-word*::: wrap at word boundaries instead of codepoint boundaries. + *-width *::: + wrap text at *max_width* if the window is wider. + *ranges* :: use the data in the range-specs option of the given name to highlight the buffer. The string part of the is interpretted as a face to apply diff --git a/src/highlighters.cc b/src/highlighters.cc index 18302b282..9ab0ba807 100644 --- a/src/highlighters.cc +++ b/src/highlighters.cc @@ -704,12 +704,13 @@ HighlighterAndId create_column_highlighter(HighlighterParameters params) struct WrapHighlighter : Highlighter { - WrapHighlighter(bool word_wrap) : Highlighter{HighlightPass::Wrap}, m_word_wrap{word_wrap} {} + WrapHighlighter(ColumnCount max_width, bool word_wrap) + : Highlighter{HighlightPass::Wrap}, m__max_width{max_width}, m_word_wrap{word_wrap} {} void do_highlight(const Context& context, HighlightPass pass, DisplayBuffer& display_buffer, BufferRange) override { - const ColumnCount wrap_column = context.window().range().column; + const ColumnCount wrap_column = std::min(m__max_width, context.window().range().column); if (wrap_column <= 0) return; @@ -759,7 +760,7 @@ struct WrapHighlighter : Highlighter void do_compute_display_setup(const Context& context, HighlightPass, DisplaySetup& setup) override { - const ColumnCount wrap_column = setup.window_range.column; + const ColumnCount wrap_column = std::min(setup.window_range.column, m__max_width); if (wrap_column <= 0) return; @@ -862,15 +863,21 @@ struct WrapHighlighter : Highlighter static HighlighterAndId create(HighlighterParameters params) { static const ParameterDesc param_desc{ - { { "word", { false, "" } } }, + { { "word", { false, "" } }, + { "width", { true, "" } } }, ParameterDesc::Flags::None, 0, 0 }; ParametersParser parser(params, param_desc); - return {"wrap", make_unique((bool)parser.get_switch("word"))}; + ColumnCount max_width{std::numeric_limits::max()}; + if (auto width = parser.get_switch("width")) + max_width = str_to_int(*width); + + return {"wrap", make_unique(max_width, (bool)parser.get_switch("word"))}; } const bool m_word_wrap; + const ColumnCount m__max_width; }; void expand_tabulations(const Context& context, HighlightPass, DisplayBuffer& display_buffer, BufferRange) @@ -2002,8 +2009,8 @@ void register_highlighters() registry.insert({ "wrap", { WrapHighlighter::create, - "Parameters: [-word]\n" - "Wrap lines to given column,\n" + "Parameters: [-word] [-width ]\n" + "Wrap lines to window width, or max_width if given and window is wider,\n" "wrap at word boundaries instead of codepoint boundaries if -word is given" } }); registry.insert({ "ref",