summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMaxime Coste <frrrwww@gmail.com>2014-05-09 13:50:12 +0100
committerMaxime Coste <frrrwww@gmail.com>2014-05-09 13:50:12 +0100
commitc7d24ac7db2921eb5e4d6b00d0c1340b64bf92aa (patch)
tree732f4b4ba60bd7aad3b2c68c7593d449007c2e70 /src
parente4e609a35c6a5e45d495c5d2e0f99dd4c6dc33fb (diff)
Add a show_whitespaces highlighter
Diffstat (limited to 'src')
-rw-r--r--src/highlighters.cc59
1 files changed, 47 insertions, 12 deletions
diff --git a/src/highlighters.cc b/src/highlighters.cc
index a0a2242a..cc484b88 100644
--- a/src/highlighters.cc
+++ b/src/highlighters.cc
@@ -1,15 +1,16 @@
#include "highlighters.hh"
#include "assert.hh"
+#include "buffer_utils.hh"
#include "color_registry.hh"
#include "context.hh"
#include "display_buffer.hh"
+#include "line_change_watcher.hh"
#include "option_types.hh"
#include "register_manager.hh"
#include "string.hh"
#include "utf8.hh"
#include "utf8_iterator.hh"
-#include "line_change_watcher.hh"
#include <sstream>
#include <locale>
@@ -351,17 +352,7 @@ void expand_tabulations(const Context& context, HighlightFlags flags, DisplayBuf
if (it+1 != end)
atom_it = line.split(atom_it, (it+1).coord());
- int column = 0;
- for (auto line_it = buffer.iterator_at(it.coord().line);
- line_it != it; ++line_it)
- {
- kak_assert(*line_it != '\n');
- if (*line_it == '\t')
- column += tabstop - (column % tabstop);
- else
- ++column;
- }
-
+ int column = (int)get_column(buffer, tabstop, it.coord());
int count = tabstop - (column % tabstop);
String padding;
for (int i = 0; i < count; ++i)
@@ -374,6 +365,49 @@ void expand_tabulations(const Context& context, HighlightFlags flags, DisplayBuf
}
}
+void show_whitespaces(const Context& context, HighlightFlags flags, DisplayBuffer& display_buffer)
+{
+ const int tabstop = context.options()["tabstop"].get<int>();
+ auto& buffer = context.buffer();
+ for (auto& line : display_buffer.lines())
+ {
+ for (auto atom_it = line.begin(); atom_it != line.end(); ++atom_it)
+ {
+ if (atom_it->type() != DisplayAtom::BufferRange)
+ continue;
+
+ auto begin = buffer.iterator_at(atom_it->begin());
+ auto end = buffer.iterator_at(atom_it->end());
+ for (BufferIterator it = begin; it != end; ++it)
+ {
+ auto c = *it;
+ if (c == '\t' or c == ' ' or c == '\n')
+ {
+ if (it != begin)
+ atom_it = ++line.split(atom_it, it.coord());
+ if (it+1 != end)
+ atom_it = line.split(atom_it, (it+1).coord());
+
+ if (c == '\t')
+ {
+ int column = (int)get_column(buffer, tabstop, it.coord());
+ int count = tabstop - (column % tabstop);
+ String padding = "→";
+ for (int i = 0; i < count-1; ++i)
+ padding += ' ';
+ atom_it->replace(padding);
+ }
+ else if (c == ' ')
+ atom_it->replace("·");
+ else if (c == '\n')
+ atom_it->replace("¬");
+ break;
+ }
+ }
+ }
+ }
+}
+
void show_line_numbers(const Context& context, HighlightFlags flags, DisplayBuffer& display_buffer)
{
LineCount last_line = context.buffer().line_count();
@@ -830,6 +864,7 @@ void register_highlighters()
registry.register_func("number_lines", SimpleHighlighterFactory<show_line_numbers>("number_lines"));
registry.register_func("show_matching", SimpleHighlighterFactory<show_matching_char>("show_matching"));
+ registry.register_func("show_whitespaces", SimpleHighlighterFactory<show_whitespaces>("show_whitespaces"));
registry.register_func("regex", colorize_regex_factory);
registry.register_func("regex_option", highlight_regex_option_factory);
registry.register_func("search", highlight_search_factory);