Skip to content

Commit 6906818

Browse files
committed
Merge remote-tracking branch 'enricozb/enricozb/column-ruler'
2 parents 9c94891 + 8cd1a7f commit 6906818

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

doc/pages/highlighters.asciidoc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,13 @@ highlighter is replaced with the new one.
111111
*fill* <face>::
112112
fill using the given *face*, mostly useful with regions highlighters
113113

114-
*column* <number> <face>::
115-
highlight column *number* with face *face*
114+
*column* <number> <face> [options]::
115+
highlight column *number* with face *face*, with the following *options*:
116+
117+
*-ruler* <character>:::
118+
a single character to display instead of empty or whitespace cells at
119+
column *number*. If this option is provided, *face* will also not
120+
apply to non-empty cells, and will only apply to *character*.
116121

117122
*line* <number> <face>::
118123
highlight line *number* with face *face*

src/highlighters.cc

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "utf8.hh"
2020
#include "utf8_iterator.hh"
2121
#include "window.hh"
22+
#include "unicode.hh"
2223

2324
#include <cstdio>
2425
#include <limits>
@@ -519,16 +520,26 @@ UniquePtr<Highlighter> create_line_highlighter(HighlighterParameters params, Hig
519520
}
520521

521522
const HighlighterDesc column_desc = {
522-
"Parameters: <value string> <face>\n"
523-
"Highlight the column given by evaluating <value string> with <face>",
524-
{}
523+
"Parameters: [-ruler <character>] <column> <face>\n"
524+
"Highlight the column <column> with <face>",
525+
{
526+
{ { "ruler", { ArgCompleter{}, "replace empty or whitespace cells with the given character. When provided, <face> is not applied to non-empty cells" } } },
527+
ParameterDesc::Flags::None, 2, 2
528+
},
525529
};
526530
UniquePtr<Highlighter> create_column_highlighter(HighlighterParameters params, Highlighter*)
527531
{
528-
if (params.size() != 2)
529-
throw runtime_error("wrong parameter count");
532+
ParametersParser parser{params, column_desc.params};
533+
534+
auto positionals = parser.positionals_from(0);
530535

531-
auto func = [col_expr=params[0], facespec=parse_face(params[1])]
536+
auto ruler = parser.get_switch("ruler");
537+
bool highlight_non_blank = not (bool)ruler;
538+
auto col_char = ruler.value_or(" ").str();
539+
if (col_char.char_length() > 1)
540+
throw runtime_error("-ruler expects a single character");
541+
542+
auto func = [col_expr=positionals[0], facespec=parse_face(positionals[1]), highlight_non_blank, col_char]
532543
(HighlightContext context, DisplayBuffer& display_buffer, BufferRange)
533544
{
534545
ColumnCount column = -1;
@@ -563,7 +574,13 @@ UniquePtr<Highlighter> create_column_highlighter(HighlighterParameters params, H
563574
atom_it = ++line.split(atom_it, remaining_col);
564575
if (atom_it->length() > 1)
565576
atom_it = line.split(atom_it, 1_col);
566-
atom_it->face = merge_faces(atom_it->face, face);
577+
if (highlight_non_blank)
578+
atom_it->face = merge_faces(atom_it->face, face);
579+
else if (is_blank(atom_it->content()[0]))
580+
{
581+
atom_it->replace(col_char);
582+
atom_it->face = merge_faces(atom_it->face, face);
583+
}
567584
found = true;
568585
break;
569586
}
@@ -574,7 +591,7 @@ UniquePtr<Highlighter> create_column_highlighter(HighlighterParameters params, H
574591

575592
if (remaining_col > 0)
576593
line.push_back({String{' ', remaining_col}, Face{}});
577-
line.push_back({" ", face});
594+
line.push_back({col_char, face});
578595
}
579596
};
580597

0 commit comments

Comments
 (0)