diff --git a/README.asciidoc b/README.asciidoc index fc5e3eab..5cbdc034 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 b826efee..e421813c 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 18302b28..9ab0ba80 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",