From 7190791927e0e1bc20e2c301ae4e0ba7169e990d Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Mon, 28 Apr 2014 19:48:23 +0100 Subject: [PATCH] Move some buffer related utility functions to buffer_utils.{cc,hh} --- src/buffer_utils.cc | 23 ++++++++++++++++++++++ src/buffer_utils.hh | 47 +++++++++++++++++++++++++++++++++++++++++++++ src/main.cc | 1 + src/normal.cc | 17 ---------------- src/selection.hh | 32 ------------------------------ src/selectors.hh | 1 + 6 files changed, 72 insertions(+), 49 deletions(-) create mode 100644 src/buffer_utils.cc create mode 100644 src/buffer_utils.hh diff --git a/src/buffer_utils.cc b/src/buffer_utils.cc new file mode 100644 index 00000000..af61cb19 --- /dev/null +++ b/src/buffer_utils.cc @@ -0,0 +1,23 @@ +#include "buffer_utils.hh" + +namespace Kakoune +{ + +CharCount get_column(const Buffer& buffer, + CharCount tabstop, BufferCoord coord) +{ + auto& line = buffer[coord.line]; + auto col = 0_char; + for (auto it = line.begin(); + it != line.end() and coord.column > (int)(it - line.begin()); + it = utf8::next(it)) + { + if (*it == '\t') + col = (col / tabstop + 1) * tabstop; + else + ++col; + } + return col; +} + +} diff --git a/src/buffer_utils.hh b/src/buffer_utils.hh new file mode 100644 index 00000000..16e2c764 --- /dev/null +++ b/src/buffer_utils.hh @@ -0,0 +1,47 @@ +#ifndef buffer_utils_hh_INCLUDED +#define buffer_utils_hh_INCLUDED + +#include "buffer.hh" +#include "selection.hh" + +namespace Kakoune +{ + +inline String content(const Buffer& buffer, const Selection& range) +{ + return buffer.string(range.min(), buffer.char_next(range.max())); +} + +inline BufferIterator erase(Buffer& buffer, const Selection& range) +{ + return buffer.erase(buffer.iterator_at(range.min()), + utf8::next(buffer.iterator_at(range.max()))); +} + +inline CharCount char_length(const Buffer& buffer, const Selection& range) +{ + return utf8::distance(buffer.iterator_at(range.min()), + utf8::next(buffer.iterator_at(range.max()))); +} + +inline void avoid_eol(const Buffer& buffer, BufferCoord& coord) +{ + const auto column = coord.column; + const auto& line = buffer[coord.line]; + if (column != 0 and column == line.length() - 1) + coord.column = line.byte_count_to(line.char_length() - 2); +} + +inline void avoid_eol(const Buffer& buffer, Selection& sel) +{ + avoid_eol(buffer, sel.anchor()); + avoid_eol(buffer, sel.cursor()); +} + +CharCount get_column(const Buffer& buffer, + CharCount tabstop, BufferCoord coord); + +} + +#endif // buffer_utils_hh_INCLUDED + diff --git a/src/main.cc b/src/main.cc index 3cdbf997..d23f8091 100644 --- a/src/main.cc +++ b/src/main.cc @@ -1,6 +1,7 @@ #include "assert.hh" #include "buffer.hh" #include "buffer_manager.hh" +#include "buffer_utils.hh" #include "client_manager.hh" #include "color_registry.hh" #include "command_manager.hh" diff --git a/src/normal.cc b/src/normal.cc index 1e2edd82..3750f4cb 100644 --- a/src/normal.cc +++ b/src/normal.cc @@ -1040,23 +1040,6 @@ void save_selections(Context& context, int) " selections", get_color("Information") }); } -static CharCount get_column(const Buffer& buffer, - CharCount tabstop, BufferCoord coord) -{ - auto& line = buffer[coord.line]; - auto col = 0_char; - for (auto it = line.begin(); - it != line.end() and coord.column > (int)(it - line.begin()); - it = utf8::next(it)) - { - if (*it == '\t') - col = (col / tabstop + 1) * tabstop; - else - ++col; - } - return col; -} - void align(Context& context, int) { auto& selections = context.selections(); diff --git a/src/selection.hh b/src/selection.hh index 0f30c937..9c75765b 100644 --- a/src/selection.hh +++ b/src/selection.hh @@ -50,38 +50,6 @@ inline bool overlaps(const Selection& lhs, const Selection& rhs) : lhs.min() <= rhs.max(); } -inline String content(const Buffer& buffer, const Selection& range) -{ - return buffer.string(range.min(), buffer.char_next(range.max())); -} - -inline BufferIterator erase(Buffer& buffer, const Selection& range) -{ - return buffer.erase(buffer.iterator_at(range.min()), - utf8::next(buffer.iterator_at(range.max()))); -} - -inline CharCount char_length(const Buffer& buffer, const Selection& range) -{ - return utf8::distance(buffer.iterator_at(range.min()), - utf8::next(buffer.iterator_at(range.max()))); -} - - -inline void avoid_eol(const Buffer& buffer, BufferCoord& coord) -{ - const auto column = coord.column; - const auto& line = buffer[coord.line]; - if (column != 0 and column == line.length() - 1) - coord.column = line.byte_count_to(line.char_length() - 2); -} - -inline void avoid_eol(const Buffer& buffer, Selection& sel) -{ - avoid_eol(buffer, sel.anchor()); - avoid_eol(buffer, sel.cursor()); -} - static bool compare_selections(const Selection& lhs, const Selection& rhs) { return lhs.min() < rhs.min(); diff --git a/src/selectors.hh b/src/selectors.hh index 51a61291..b28febef 100644 --- a/src/selectors.hh +++ b/src/selectors.hh @@ -2,6 +2,7 @@ #define selectors_hh_INCLUDED #include "selection.hh" +#include "buffer_utils.hh" #include "unicode.hh" #include "utf8_iterator.hh"