diff --git a/src/normal.cc b/src/normal.cc index 16f1a4c2..033eef34 100644 --- a/src/normal.cc +++ b/src/normal.cc @@ -35,12 +35,11 @@ enum class SelectMode template void select(Context& context, T func) { - auto& buffer = context.buffer(); auto& selections = context.selections(); if (mode == SelectMode::Append) { auto& sel = selections.main(); - if (auto res = func(buffer, sel)) + if (auto res = func(context, sel)) { if (res->captures().empty()) res->captures() = sel.captures(); @@ -54,7 +53,7 @@ void select(Context& context, T func) for (int i = 0; i < (int)selections.size(); ++i) { auto& sel = selections[i]; - auto res = func(buffer, sel); + auto res = func(context, sel); if (not res) { to_remove.push_back(i); @@ -82,7 +81,7 @@ void select(Context& context, T func) selections.check_invariant(); } -template (*func)(const Buffer&, const Selection&)> +template (*func)(const Context&, const Selection&)> void select(Context& context, NormalParams) { select(context, func); @@ -727,15 +726,14 @@ void search(Context& context, NormalParams params) { int c = count; auto& selections = context.selections(); - auto& buffer = context.buffer(); do { bool wrapped = false; for (auto& sel : selections) { if (mode == SelectMode::Replace) - sel = keep_direction(find_next_match(buffer, sel, regex, wrapped), sel); + sel = keep_direction(find_next_match(context, sel, regex, wrapped), sel); if (mode == SelectMode::Extend) - sel.merge_with(find_next_match(buffer, sel, regex, wrapped)); + sel.merge_with(find_next_match(context, sel, regex, wrapped)); } selections.sort_and_merge_overlapping(); } while (--c > 0); @@ -752,19 +750,18 @@ void search_next(Context& context, NormalParams params) { Regex regex{str}; auto& selections = context.selections(); - auto& buffer = context.buffer(); bool main_wrapped = false; do { bool wrapped = false; if (mode == SelectMode::Replace) { auto& sel = selections.main(); - sel = keep_direction(find_next_match(buffer, sel, regex, wrapped), sel); + sel = keep_direction(find_next_match(context, sel, regex, wrapped), sel); } else if (mode == SelectMode::Append) { auto sel = keep_direction( - find_next_match(buffer, selections.main(), regex, wrapped), + find_next_match(context, selections.main(), regex, wrapped), selections.main()); selections.push_back(std::move(sel)); selections.set_main_index(selections.size() - 1); @@ -1077,7 +1074,7 @@ void select_object(Context& context, NormalParams params) static constexpr struct ObjectType { Codepoint key; - Optional (*func)(const Buffer&, const Selection&, int, ObjectFlags); + Optional (*func)(const Context&, const Selection&, int, ObjectFlags); } selectors[] = { { 'w', select_word }, { 'W', select_word }, diff --git a/src/selectors.cc b/src/selectors.cc index 962386cb..62ef8363 100644 --- a/src/selectors.cc +++ b/src/selectors.cc @@ -1,6 +1,7 @@ #include "selectors.hh" #include "buffer_utils.hh" +#include "context.hh" #include "flags.hh" #include "optional.hh" #include "regex.hh" @@ -39,8 +40,9 @@ Selection utf8_range(const Utf8Iterator& first, const Utf8Iterator& last) template Optional -select_to_next_word(const Buffer& buffer, const Selection& selection) +select_to_next_word(const Context& context, const Selection& selection) { + auto& buffer = context.buffer(); Utf8Iterator begin{buffer.iterator_at(selection.cursor()), buffer}; if (begin+1 == buffer.end()) return {}; @@ -61,13 +63,14 @@ select_to_next_word(const Buffer& buffer, const Selection& selection) return utf8_range(begin, end-1); } -template Optional select_to_next_word(const Buffer&, const Selection&); -template Optional select_to_next_word(const Buffer&, const Selection&); +template Optional select_to_next_word(const Context&, const Selection&); +template Optional select_to_next_word(const Context&, const Selection&); template Optional -select_to_next_word_end(const Buffer& buffer, const Selection& selection) +select_to_next_word_end(const Context& context, const Selection& selection) { + auto& buffer = context.buffer(); Utf8Iterator begin{buffer.iterator_at(selection.cursor()), buffer}; if (begin+1 == buffer.end()) return {}; @@ -87,13 +90,14 @@ select_to_next_word_end(const Buffer& buffer, const Selection& selection) return utf8_range(begin, end-1); } -template Optional select_to_next_word_end(const Buffer&, const Selection&); -template Optional select_to_next_word_end(const Buffer&, const Selection&); +template Optional select_to_next_word_end(const Context&, const Selection&); +template Optional select_to_next_word_end(const Context&, const Selection&); template Optional -select_to_previous_word(const Buffer& buffer, const Selection& selection) +select_to_previous_word(const Context& context, const Selection& selection) { + auto& buffer = context.buffer(); Utf8Iterator begin{buffer.iterator_at(selection.cursor()), buffer}; if (begin == buffer.begin()) return {}; @@ -112,14 +116,15 @@ select_to_previous_word(const Buffer& buffer, const Selection& selection) return utf8_range(begin, with_end ? end : end+1); } -template Optional select_to_previous_word(const Buffer&, const Selection&); -template Optional select_to_previous_word(const Buffer&, const Selection&); +template Optional select_to_previous_word(const Context&, const Selection&); +template Optional select_to_previous_word(const Context&, const Selection&); template Optional -select_word(const Buffer& buffer, const Selection& selection, +select_word(const Context& context, const Selection& selection, int count, ObjectFlags flags) { + auto& buffer = context.buffer(); Utf8Iterator first{buffer.iterator_at(selection.cursor()), buffer}; if (not is_word(*first)) return {}; @@ -141,12 +146,13 @@ select_word(const Buffer& buffer, const Selection& selection, return (flags & ObjectFlags::ToEnd) ? utf8_range(first, last) : utf8_range(last, first); } -template Optional select_word(const Buffer&, const Selection&, int, ObjectFlags); -template Optional select_word(const Buffer&, const Selection&, int, ObjectFlags); +template Optional select_word(const Context&, const Selection&, int, ObjectFlags); +template Optional select_word(const Context&, const Selection&, int, ObjectFlags); Optional -select_line(const Buffer& buffer, const Selection& selection) +select_line(const Context& context, const Selection& selection) { + auto& buffer = context.buffer(); Utf8Iterator first{buffer.iterator_at(selection.cursor()), buffer}; if (*first == '\n' and first + 1 != buffer.end()) ++first; @@ -162,33 +168,35 @@ select_line(const Buffer& buffer, const Selection& selection) template Optional -select_to_line_end(const Buffer& buffer, const Selection& selection) +select_to_line_end(const Context& context, const Selection& selection) { + auto& buffer = context.buffer(); BufferCoord begin = selection.cursor(); LineCount line = begin.line; BufferCoord end = utf8::previous(buffer.iterator_at({line, buffer[line].length() - 1}), - buffer.iterator_at(line)).coord(); + buffer.iterator_at(line)).coord(); if (end < begin) // Do not go backward when cursor is on eol end = begin; return target_eol({only_move ? end : begin, end}); } -template Optional select_to_line_end(const Buffer&, const Selection&); -template Optional select_to_line_end(const Buffer&, const Selection&); +template Optional select_to_line_end(const Context&, const Selection&); +template Optional select_to_line_end(const Context&, const Selection&); template Optional -select_to_line_begin(const Buffer& buffer, const Selection& selection) +select_to_line_begin(const Context&, const Selection& selection) { BufferCoord begin = selection.cursor(); BufferCoord end = begin.line; return Selection{only_move ? end : begin, end}; } -template Optional select_to_line_begin(const Buffer&, const Selection&); -template Optional select_to_line_begin(const Buffer&, const Selection&); +template Optional select_to_line_begin(const Context&, const Selection&); +template Optional select_to_line_begin(const Context&, const Selection&); Optional -select_to_first_non_blank(const Buffer& buffer, const Selection& selection) +select_to_first_non_blank(const Context& context, const Selection& selection) { + auto& buffer = context.buffer(); auto it = buffer.iterator_at(selection.cursor().line); skip_while(it, buffer.iterator_at(selection.cursor().line+1), is_horizontal_blank); @@ -196,8 +204,9 @@ select_to_first_non_blank(const Buffer& buffer, const Selection& selection) } Optional -select_matching(const Buffer& buffer, const Selection& selection) +select_matching(const Context& context, const Selection& selection) { + auto& buffer = context.buffer(); ConstArrayView matching_pairs = { '(', ')', '{', '}', '[', ']', '<', '>' }; Utf8Iterator it{buffer.iterator_at(selection.cursor()), buffer}; auto match = matching_pairs.end(); @@ -341,10 +350,11 @@ find_surrounding(const Container& container, Iterator pos, } Optional -select_surrounding(const Buffer& buffer, const Selection& selection, +select_surrounding(const Context& context, const Selection& selection, StringView opening, StringView closing, int level, ObjectFlags flags) { + auto& buffer = context.buffer(); const bool nestable = opening != closing; auto pos = selection.cursor(); if (not nestable or flags & ObjectFlags::Inner) @@ -378,9 +388,10 @@ select_surrounding(const Buffer& buffer, const Selection& selection, } Optional -select_to(const Buffer& buffer, const Selection& selection, +select_to(const Context& context, const Selection& selection, Codepoint c, int count, bool inclusive) { + auto& buffer = context.buffer(); Utf8Iterator begin{buffer.iterator_at(selection.cursor()), buffer}; Utf8Iterator end = begin; do @@ -396,9 +407,10 @@ select_to(const Buffer& buffer, const Selection& selection, } Optional -select_to_reverse(const Buffer& buffer, const Selection& selection, +select_to_reverse(const Context& context, const Selection& selection, Codepoint c, int count, bool inclusive) { + auto& buffer = context.buffer(); Utf8Iterator begin{buffer.iterator_at(selection.cursor()), buffer}; Utf8Iterator end = begin; do @@ -414,7 +426,7 @@ select_to_reverse(const Buffer& buffer, const Selection& selection, } Optional -select_number(const Buffer& buffer, const Selection& selection, +select_number(const Context& context, const Selection& selection, int count, ObjectFlags flags) { auto is_number = [&](char c) { @@ -422,6 +434,7 @@ select_number(const Buffer& buffer, const Selection& selection, (not (flags & ObjectFlags::Inner) and c == '.'); }; + auto& buffer = context.buffer(); BufferIterator first = buffer.iterator_at(selection.cursor()); BufferIterator last = first; @@ -450,13 +463,14 @@ select_number(const Buffer& buffer, const Selection& selection, } Optional -select_sentence(const Buffer& buffer, const Selection& selection, +select_sentence(const Context& context, const Selection& selection, int count, ObjectFlags flags) { auto is_end_of_sentence = [](char c) { return c == '.' or c == ';' or c == '!' or c == '?'; }; + auto& buffer = context.buffer(); BufferIterator first = buffer.iterator_at(selection.cursor()); if (not (flags & ObjectFlags::ToEnd)) @@ -517,9 +531,10 @@ select_sentence(const Buffer& buffer, const Selection& selection, } Optional -select_paragraph(const Buffer& buffer, const Selection& selection, +select_paragraph(const Context& context, const Selection& selection, int count, ObjectFlags flags) { + auto& buffer = context.buffer(); BufferIterator first = buffer.iterator_at(selection.cursor()); if (not (flags & ObjectFlags::ToEnd) and first.coord() > BufferCoord{0,1} and @@ -572,13 +587,14 @@ select_paragraph(const Buffer& buffer, const Selection& selection, } Optional -select_whitespaces(const Buffer& buffer, const Selection& selection, +select_whitespaces(const Context& context, const Selection& selection, int count, ObjectFlags flags) { auto is_whitespace = [&](char c) { return c == ' ' or c == '\t' or (not (flags & ObjectFlags::Inner) and c == '\n'); }; + auto& buffer = context.buffer(); BufferIterator first = buffer.iterator_at(selection.cursor()); BufferIterator last = first; @@ -607,7 +623,7 @@ select_whitespaces(const Buffer& buffer, const Selection& selection, } Optional -select_indent(const Buffer& buffer, const Selection& selection, +select_indent(const Context& context, const Selection& selection, int count, ObjectFlags flags) { auto get_indent = [](StringView str, int tabstop) { @@ -634,7 +650,8 @@ select_indent(const Buffer& buffer, const Selection& selection, const bool to_begin = flags & ObjectFlags::ToBegin; const bool to_end = flags & ObjectFlags::ToEnd; - int tabstop = buffer.options()["tabstop"].get(); + auto& buffer = context.buffer(); + int tabstop = context.options()["tabstop"].get(); auto pos = selection.cursor(); LineCount line = pos.line; auto indent = get_indent(buffer[line], tabstop); @@ -673,7 +690,7 @@ select_indent(const Buffer& buffer, const Selection& selection, } Optional -select_argument(const Buffer& buffer, const Selection& selection, +select_argument(const Context& context, const Selection& selection, int level, ObjectFlags flags) { enum Class { None, Opening, Closing, Delimiter }; @@ -687,6 +704,7 @@ select_argument(const Buffer& buffer, const Selection& selection, } }; + auto& buffer = context.buffer(); BufferIterator pos = buffer.iterator_at(selection.cursor()); switch (classify(*pos)) { @@ -762,8 +780,9 @@ select_argument(const Buffer& buffer, const Selection& selection, } Optional -select_lines(const Buffer& buffer, const Selection& selection) +select_lines(const Context& context, const Selection& selection) { + auto& buffer = context.buffer(); BufferCoord anchor = selection.anchor(); BufferCoord cursor = selection.cursor(); BufferCoord& to_line_start = anchor <= cursor ? anchor : cursor; @@ -776,8 +795,9 @@ select_lines(const Buffer& buffer, const Selection& selection) } Optional -trim_partial_lines(const Buffer& buffer, const Selection& selection) +trim_partial_lines(const Context& context, const Selection& selection) { + auto& buffer = context.buffer(); BufferCoord anchor = selection.anchor(); BufferCoord cursor = selection.cursor(); BufferCoord& to_line_start = anchor <= cursor ? anchor : cursor; @@ -853,8 +873,9 @@ static bool find_prev(const Buffer& buffer, const BufferIterator& pos, } template -Selection find_next_match(const Buffer& buffer, const Selection& sel, const Regex& regex, bool& wrapped) +Selection find_next_match(const Context& context, const Selection& sel, const Regex& regex, bool& wrapped) { + auto& buffer = context.buffer(); MatchResults matches; auto pos = buffer.iterator_at(direction == Backward ? sel.min() : sel.max()); wrapped = false; @@ -876,8 +897,8 @@ Selection find_next_match(const Buffer& buffer, const Selection& sel, const Rege return {begin.coord(), end.coord(), std::move(captures)}; } -template Selection find_next_match(const Buffer&, const Selection&, const Regex&, bool&); -template Selection find_next_match(const Buffer&, const Selection&, const Regex&, bool&); +template Selection find_next_match(const Context&, const Selection&, const Regex&, bool&); +template Selection find_next_match(const Context&, const Selection&, const Regex&, bool&); using RegexIt = RegexIterator; diff --git a/src/selectors.hh b/src/selectors.hh index c87f6321..5dd34068 100644 --- a/src/selectors.hh +++ b/src/selectors.hh @@ -7,6 +7,7 @@ namespace Kakoune { class Regex; +class Context; template struct MatchResults; inline Selection keep_direction(Selection res, const Selection& ref) @@ -18,39 +19,39 @@ inline Selection keep_direction(Selection res, const Selection& ref) template Optional -select_to_next_word(const Buffer& buffer, const Selection& selection); +select_to_next_word(const Context& context, const Selection& selection); template Optional -select_to_next_word_end(const Buffer& buffer, const Selection& selection); +select_to_next_word_end(const Context& context, const Selection& selection); template Optional -select_to_previous_word(const Buffer& buffer, const Selection& selection); +select_to_previous_word(const Context& context, const Selection& selection); Optional -select_line(const Buffer& buffer, const Selection& selection); +select_line(const Context& context, const Selection& selection); Optional -select_matching(const Buffer& buffer, const Selection& selection); +select_matching(const Context& context, const Selection& selection); Optional -select_to(const Buffer& buffer, const Selection& selection, +select_to(const Context& context, const Selection& selection, Codepoint c, int count, bool inclusive); Optional -select_to_reverse(const Buffer& buffer, const Selection& selection, +select_to_reverse(const Context& context, const Selection& selection, Codepoint c, int count, bool inclusive); template Optional -select_to_line_end(const Buffer& buffer, const Selection& selection); +select_to_line_end(const Context& context, const Selection& selection); template Optional -select_to_line_begin(const Buffer& buffer, const Selection& selection); +select_to_line_begin(const Context& context, const Selection& selection); Optional -select_to_first_non_blank(const Buffer& buffer, const Selection& selection); +select_to_first_non_blank(const Context& context, const Selection& selection); enum class ObjectFlags { @@ -63,52 +64,52 @@ constexpr bool with_bit_ops(Meta::Type) { return true; } template Optional -select_word(const Buffer& buffer, const Selection& selection, +select_word(const Context& context, const Selection& selection, int count, ObjectFlags flags); Optional -select_number(const Buffer& buffer, const Selection& selection, +select_number(const Context& context, const Selection& selection, int count, ObjectFlags flags); Optional -select_sentence(const Buffer& buffer, const Selection& selection, +select_sentence(const Context& context, const Selection& selection, int count, ObjectFlags flags); Optional -select_paragraph(const Buffer& buffer, const Selection& selection, +select_paragraph(const Context& context, const Selection& selection, int count, ObjectFlags flags); Optional -select_whitespaces(const Buffer& buffer, const Selection& selection, +select_whitespaces(const Context& context, const Selection& selection, int count, ObjectFlags flags); Optional -select_indent(const Buffer& buffer, const Selection& selection, +select_indent(const Context& context, const Selection& selection, int count, ObjectFlags flags); Optional -select_argument(const Buffer& buffer, const Selection& selection, +select_argument(const Context& context, const Selection& selection, int level, ObjectFlags flags); Optional -select_lines(const Buffer& buffer, const Selection& selection); +select_lines(const Context& context, const Selection& selection); Optional -trim_partial_lines(const Buffer& buffer, const Selection& selection); +trim_partial_lines(const Context& context, const Selection& selection); void select_buffer(SelectionList& selections); enum Direction { Forward, Backward }; template -Selection find_next_match(const Buffer& buffer, const Selection& sel, +Selection find_next_match(const Context& context, const Selection& sel, const Regex& regex, bool& wrapped); void select_all_matches(SelectionList& selections, const Regex& regex, int capture = 0); void split_selections(SelectionList& selections, const Regex& regex, int capture = 0); Optional -select_surrounding(const Buffer& buffer, const Selection& selection, +select_surrounding(const Context& context, const Selection& selection, StringView opening, StringView closing, int level, ObjectFlags flags);