diff --git a/src/buffer.cc b/src/buffer.cc index 398f7460..52621f3d 100644 --- a/src/buffer.cc +++ b/src/buffer.cc @@ -66,10 +66,10 @@ BufferCoord Buffer::line_and_column_at(const BufferIterator& iterator) const return iterator.m_coord; } -BufferSize Buffer::line_length(LineCount line) const +CharCount Buffer::line_length(LineCount line) const { assert(line < line_count()); - BufferPos end = (line < line_count() - 1) ? + CharCount end = (line < line_count() - 1) ? m_lines[line + 1].start : character_count(); return end - m_lines[line].start; } @@ -82,8 +82,8 @@ BufferCoord Buffer::clamp(const BufferCoord& line_and_column, BufferCoord result(line_and_column.line, line_and_column.column); result.line = Kakoune::clamp(0_line, line_count() - 1, result.line); - int max_col = std::max(0, line_length(result.line) - (avoid_eol ? 2 : 1)); - result.column = Kakoune::clamp(0, max_col, result.column); + CharCount max_col = std::max(0_char, line_length(result.line) - (avoid_eol ? 2 : 1)); + result.column = Kakoune::clamp(0_char, max_col, result.column); return result; } @@ -120,10 +120,10 @@ BufferIterator Buffer::end() const { if (m_lines.empty()) return BufferIterator(*this, { 0_line, 0 }); - return BufferIterator(*this, { line_count()-1, (int)m_lines.back().length() }); + return BufferIterator(*this, { line_count()-1, m_lines.back().length() }); } -BufferSize Buffer::character_count() const +CharCount Buffer::character_count() const { if (m_lines.empty()) return 0; @@ -140,10 +140,10 @@ String Buffer::string(const BufferIterator& begin, const BufferIterator& end) co String res; for (LineCount line = begin.line(); line <= end.line(); ++line) { - size_t start = 0; + CharCount start = 0; if (line == begin.line()) start = begin.column(); - size_t count = -1; + CharCount count = -1; if (line == end.line()) count = end.column() - start; res += m_lines[line].content.substr(start, count); @@ -231,7 +231,7 @@ void Buffer::reset_undo_data() void Buffer::check_invariant() const { - BufferSize start = 0; + CharCount start = 0; assert(not m_lines.empty()); for (auto& line : m_lines) { @@ -245,7 +245,7 @@ void Buffer::check_invariant() const void Buffer::do_insert(const BufferIterator& pos, const String& content) { ++m_timestamp; - BufferSize offset = pos.offset(); + CharCount offset = pos.offset(); // all following lines advanced by length for (LineCount i = pos.line()+1; i < line_count(); ++i) @@ -257,8 +257,8 @@ void Buffer::do_insert(const BufferIterator& pos, const String& content) // line without inserting a '\n' if (pos == end() and (pos == begin() or *(pos-1) == '\n')) { - int start = 0; - for (int i = 0; i < content.length(); ++i) + CharCount start = 0; + for (CharCount i = 0; i < content.length(); ++i) { if (content[i] == '\n') { @@ -280,8 +280,8 @@ void Buffer::do_insert(const BufferIterator& pos, const String& content) auto line_it = m_lines.begin() + (int)pos.line(); line_it = m_lines.erase(line_it); - int start = 0; - for (int i = 0; i < content.length(); ++i) + CharCount start = 0; + for (CharCount i = 0; i < content.length(); ++i) { if (content[i] == '\n') { @@ -289,7 +289,7 @@ void Buffer::do_insert(const BufferIterator& pos, const String& content) if (start == 0) { line_content = prefix + line_content; - line_it = m_lines.insert(line_it, { offset + start - (int)prefix.length(), + line_it = m_lines.insert(line_it, { offset + start - prefix.length(), std::move(line_content) }); } else @@ -301,7 +301,7 @@ void Buffer::do_insert(const BufferIterator& pos, const String& content) } } if (start == 0) - line_it = m_lines.insert(line_it, { offset + start - (int)prefix.length(), prefix + content + suffix }); + line_it = m_lines.insert(line_it, { offset + start - prefix.length(), prefix + content + suffix }); else if (start != content.length() or not suffix.empty()) line_it = m_lines.insert(line_it, { offset + start, content.substr(start) + suffix }); else @@ -309,7 +309,7 @@ void Buffer::do_insert(const BufferIterator& pos, const String& content) begin_it = pos; end_it = BufferIterator(*this, { LineCount(line_it - m_lines.begin()), - int(line_it->length() - suffix.length()) }); + line_it->length() - suffix.length() }); } check_invariant(); @@ -318,7 +318,7 @@ void Buffer::do_insert(const BufferIterator& pos, const String& content) listener->on_insert(begin_it, end_it); } -void Buffer::do_erase(const BufferIterator& pos, BufferSize length) +void Buffer::do_erase(const BufferIterator& pos, CharCount length) { ++m_timestamp; BufferIterator end = pos + length; @@ -328,7 +328,7 @@ void Buffer::do_erase(const BufferIterator& pos, BufferSize length) Line new_line = { m_lines[pos.line()].start, prefix + suffix }; m_lines.erase(m_lines.begin() + (int)pos.line(), m_lines.begin() + (int)end.line() + 1); - if (new_line.length()) + if (new_line.length() != 0) m_lines.insert(m_lines.begin() + (int)pos.line(), std::move(new_line)); for (LineCount i = pos.line()+1; i < line_count(); ++i) @@ -356,7 +356,7 @@ void Buffer::apply_modification(const Modification& modification) } case Modification::Erase: { - size_t count = modification.content.length(); + CharCount count = modification.content.length(); assert(string(modification.position, modification.position + count) == modification.content); do_erase(modification.position, count); diff --git a/src/buffer.hh b/src/buffer.hh index 971c731f..7f3f95ac 100644 --- a/src/buffer.hh +++ b/src/buffer.hh @@ -17,12 +17,9 @@ namespace Kakoune class Buffer; class Window; -typedef int BufferPos; -typedef int BufferSize; - struct BufferCoord : LineAndColumn { - BufferCoord(LineCount line = 0, int column = 0) + BufferCoord(LineCount line = 0, CharCount column = 0) : LineAndColumn(line, column) {} template @@ -35,7 +32,7 @@ class BufferIterator { public: typedef Character value_type; - typedef BufferSize difference_type; + typedef size_t difference_type; typedef const value_type* pointer; typedef const value_type& reference; typedef std::bidirectional_iterator_tag iterator_category; @@ -51,13 +48,13 @@ public: bool operator>= (const BufferIterator& iterator) const; Character operator* () const; - BufferSize operator- (const BufferIterator& iterator) const; + size_t operator- (const BufferIterator& iterator) const; - BufferIterator operator+ (BufferSize size) const; - BufferIterator operator- (BufferSize size) const; + BufferIterator operator+ (CharCount size) const; + BufferIterator operator- (CharCount size) const; - BufferIterator& operator+= (BufferSize size); - BufferIterator& operator-= (BufferSize size); + BufferIterator& operator+= (CharCount size); + BufferIterator& operator-= (CharCount size); BufferIterator& operator++ (); BufferIterator& operator-- (); @@ -74,10 +71,10 @@ public: const Buffer& buffer() const; const BufferCoord& coord() const { return m_coord; } LineCount line() const { return m_coord.line; } - BufferSize column() const { return m_coord.column; } + CharCount column() const { return m_coord.column; } private: - BufferSize offset() const; + CharCount offset() const; const Buffer* m_buffer; BufferCoord m_coord; @@ -130,7 +127,7 @@ public: BufferIterator begin() const; BufferIterator end() const; - BufferSize character_count() const; + CharCount character_count() const; LineCount line_count() const; // returns an iterator at given coordinates. line_and_column is @@ -187,10 +184,10 @@ private: struct Line { - BufferPos start; + CharCount start; String content; - size_t length() const { return content.length(); } + CharCount length() const { return content.length(); } }; struct LineList : std::vector { @@ -204,9 +201,9 @@ private: LineList m_lines; void do_insert(const BufferIterator& pos, const String& content); - void do_erase(const BufferIterator& pos, BufferSize length); + void do_erase(const BufferIterator& pos, CharCount length); - BufferSize line_length(LineCount line) const; + CharCount line_length(LineCount line) const; String m_name; const Type m_type; diff --git a/src/buffer_iterator.inl.hh b/src/buffer_iterator.inl.hh index b91734dc..1b346527 100644 --- a/src/buffer_iterator.inl.hh +++ b/src/buffer_iterator.inl.hh @@ -114,25 +114,25 @@ inline Character BufferIterator::operator*() const return m_buffer->m_lines[line()].content[column()]; } -inline BufferSize BufferIterator::offset() const +inline CharCount BufferIterator::offset() const { assert(m_buffer); return line() == 0 ? column() - : m_buffer->m_lines[line()].start + column(); + : m_buffer->m_lines[line()].start + column(); } -inline BufferSize BufferIterator::operator-(const BufferIterator& iterator) const +inline size_t BufferIterator::operator-(const BufferIterator& iterator) const { assert(m_buffer == iterator.m_buffer); - return offset() - iterator.offset(); + return (size_t)(int)(offset() - iterator.offset()); } -inline BufferIterator BufferIterator::operator+(BufferSize size) const +inline BufferIterator BufferIterator::operator+(CharCount size) const { assert(m_buffer); if (size >= 0) { - BufferSize o = std::min(m_buffer->character_count(), offset() + size); + CharCount o = std::min(m_buffer->character_count(), offset() + size); for (LineCount i = line() + 1; i < m_buffer->line_count(); ++i) { if (m_buffer->m_lines[i].start > o) @@ -144,12 +144,12 @@ inline BufferIterator BufferIterator::operator+(BufferSize size) const return operator-(-size); } -inline BufferIterator BufferIterator::operator-(BufferSize size) const +inline BufferIterator BufferIterator::operator-(CharCount size) const { assert(m_buffer); if (size >= 0) { - BufferSize o = std::max(0, offset() - size); + CharCount o = std::max(0_char, offset() - size); for (LineCount i = line(); i >= 0; --i) { if (m_buffer->m_lines[i].start <= o) @@ -160,12 +160,12 @@ inline BufferIterator BufferIterator::operator-(BufferSize size) const return operator+(-size); } -inline BufferIterator& BufferIterator::operator+=(BufferSize size) +inline BufferIterator& BufferIterator::operator+=(CharCount size) { return *this = (*this + size); } -inline BufferIterator& BufferIterator::operator-=(BufferSize size) +inline BufferIterator& BufferIterator::operator-=(CharCount size) { return *this = (*this - size); } diff --git a/src/buffer_manager.cc b/src/buffer_manager.cc index c5235cb1..5a1f63da 100644 --- a/src/buffer_manager.cc +++ b/src/buffer_manager.cc @@ -53,7 +53,7 @@ Buffer* BufferManager::get_buffer(const String& name) } CandidateList BufferManager::complete_buffername(const String& prefix, - size_t cursor_pos) + CharCount cursor_pos) { String real_prefix = prefix.substr(0, cursor_pos); CandidateList result; diff --git a/src/buffer_manager.hh b/src/buffer_manager.hh index 36373a69..fd6ca23f 100644 --- a/src/buffer_manager.hh +++ b/src/buffer_manager.hh @@ -29,7 +29,7 @@ public: Buffer* get_buffer(const String& name); CandidateList complete_buffername(const String& prefix, - size_t cursor_pos = String::npos); + CharCount cursor_pos = -1); private: BufferList m_buffers; diff --git a/src/command_manager.cc b/src/command_manager.cc index 4e982663..947031c4 100644 --- a/src/command_manager.cc +++ b/src/command_manager.cc @@ -61,7 +61,7 @@ private: using TokenList = std::vector; -using TokenPosList = std::vector>; +using TokenPosList = std::vector>; bool is_command_separator(Character c) { @@ -78,8 +78,8 @@ TokenList parse(const String& line, { TokenList result; - size_t length = line.length(); - size_t pos = 0; + CharCount length = line.length(); + CharCount pos = 0; while (pos < length) { while (pos != length) @@ -92,7 +92,7 @@ TokenList parse(const String& line, break; } - size_t token_start = pos; + CharCount token_start = pos; Token::Type type = Token::Type::Raw; if (line[pos] == '"' or line[pos] == '\'') @@ -107,7 +107,7 @@ TokenList parse(const String& line, } else if (line[pos] == '%') { - size_t type_start = ++pos; + CharCount type_start = ++pos; while (isalpha(line[pos])) ++pos; String type_name = line.substr(type_start, pos - type_start); @@ -250,7 +250,7 @@ void CommandManager::execute(const String& command_line, } Completions CommandManager::complete(const Context& context, - const String& command_line, size_t cursor_pos) + const String& command_line, CharCount cursor_pos) { TokenPosList pos_info; TokenList tokens = parse(command_line, &pos_info); @@ -267,7 +267,7 @@ Completions CommandManager::complete(const Context& context, if (token_to_complete == 0 or tokens.empty()) // command name completion { - size_t cmd_start = tokens.empty() ? 0 : pos_info[0].first; + CharCount cmd_start = tokens.empty() ? 0 : pos_info[0].first; Completions result(cmd_start, cursor_pos); String prefix = command_line.substr(cmd_start, cursor_pos - cmd_start); @@ -292,10 +292,10 @@ Completions CommandManager::complete(const Context& context, if (command_it == m_commands.end() or not command_it->second.completer) return Completions(); - size_t start = token_to_complete < tokens.size() ? + CharCount start = token_to_complete < tokens.size() ? pos_info[token_to_complete].first : cursor_pos; Completions result(start , cursor_pos); - size_t cursor_pos_in_token = cursor_pos - start; + CharCount cursor_pos_in_token = cursor_pos - start; std::vector params; for (auto token_it = tokens.begin()+1; token_it != tokens.end(); ++token_it) @@ -309,7 +309,7 @@ Completions CommandManager::complete(const Context& context, CandidateList PerArgumentCommandCompleter::operator()(const Context& context, const CommandParameters& params, size_t token_to_complete, - size_t pos_in_token) const + CharCount pos_in_token) const { if (token_to_complete >= m_completers.size()) return CandidateList(); diff --git a/src/command_manager.hh b/src/command_manager.hh index 113bac3e..cbc6a2c7 100644 --- a/src/command_manager.hh +++ b/src/command_manager.hh @@ -28,13 +28,13 @@ typedef std::function CommandCompleter; + size_t, CharCount)> CommandCompleter; class PerArgumentCommandCompleter { public: typedef std::function ArgumentCompleter; + const String&, CharCount)> ArgumentCompleter; typedef memoryview ArgumentCompleterList; PerArgumentCommandCompleter(const ArgumentCompleterList& completers) @@ -43,7 +43,7 @@ public: CandidateList operator()(const Context& context, const CommandParameters& params, size_t token_to_complete, - size_t pos_in_token) const; + CharCount pos_in_token) const; private: std::vector m_completers; @@ -56,7 +56,7 @@ public: const EnvVarMap& env_vars = EnvVarMap()); Completions complete(const Context& context, - const String& command_line, size_t cursor_pos); + const String& command_line, CharCount cursor_pos); bool command_defined(const String& command_name) const; diff --git a/src/commands.cc b/src/commands.cc index 0b73b164..6c04fbc0 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -259,7 +259,7 @@ void edit(const CommandParameters& params, Context& context) int column = param_count > 2 ? std::max(0, str_to_int(parser[2]) - 1) : 0; - window.select(window.buffer().iterator_at({LineCount(line), column})); + window.select(window.buffer().iterator_at({ line, column })); window.center_selection(); } @@ -536,11 +536,11 @@ void define_command(const CommandParameters& params, Context& context) { String shell_cmd = parser.option_value("shell-completion"); auto completer = [=](const Context& context, const CommandParameters& params, - size_t token_to_complete, size_t pos_in_token) + size_t token_to_complete, CharCount pos_in_token) { EnvVarMap vars = params_to_env_var_map(params); vars["token_to_complete"] = int_to_str(token_to_complete); - vars["pos_in_token"] = int_to_str(pos_in_token); + vars["pos_in_token"] = int_to_str((int)pos_in_token); String output = ShellManager::instance().eval(shell_cmd, context, vars); return split(output, '\n'); }; @@ -577,14 +577,14 @@ void exec_commands_in_runtime_file(const CommandParameters& params, const String& filename = params[0]; char buffer[2048]; #if defined(__linux__) - ssize_t res = readlink("/proc/self/exe", buffer, 2048 - filename.length()); + ssize_t res = readlink("/proc/self/exe", buffer, 2048 - (int)filename.length()); assert(res != -1); buffer[res] = '\0'; #elif defined(__APPLE__) uint32_t bufsize = 2048 - filename.length(); _NSGetExecutablePath(buffer, &bufsize); char* canonical_path = realpath(buffer, NULL); - strncpy(buffer, canonical_path, 2048 - filename.length()); + strncpy(buffer, canonical_path, 2048 - (int)filename.length()); free(canonical_path); #else # error "finding executable path is not implemented on this platform" @@ -778,7 +778,7 @@ void register_commands() cm.register_command("wq!", write_and_quit); PerArgumentCommandCompleter buffer_completer({ - [](const Context& context, const String& prefix, size_t cursor_pos) + [](const Context& context, const String& prefix, CharCount cursor_pos) { return BufferManager::instance().complete_buffername(prefix, cursor_pos); } }); cm.register_commands({ "b", "buffer" }, show_buffer, buffer_completer); @@ -786,7 +786,7 @@ void register_commands() cm.register_commands({ "ah", "addhl" }, add_highlighter, [](const Context& context, const CommandParameters& params, - size_t token_to_complete, size_t pos_in_token) + size_t token_to_complete, CharCount pos_in_token) { Window& w = context.window(); const String& arg = token_to_complete < params.size() ? @@ -800,7 +800,7 @@ void register_commands() }); cm.register_commands({ "rh", "rmhl" }, rm_highlighter, [](const Context& context, const CommandParameters& params, - size_t token_to_complete, size_t pos_in_token) + size_t token_to_complete, CharCount pos_in_token) { Window& w = context.window(); const String& arg = token_to_complete < params.size() ? @@ -814,7 +814,7 @@ void register_commands() }); cm.register_commands({ "af", "addfilter" }, add_filter, [](const Context& context, const CommandParameters& params, - size_t token_to_complete, size_t pos_in_token) + size_t token_to_complete, CharCount pos_in_token) { Window& w = context.window(); const String& arg = token_to_complete < params.size() ? @@ -828,7 +828,7 @@ void register_commands() }); cm.register_commands({ "rf", "rmfilter" }, rm_filter, [](const Context& context, const CommandParameters& params, - size_t token_to_complete, size_t pos_in_token) + size_t token_to_complete, CharCount pos_in_token) { Window& w = context.window(); const String& arg = token_to_complete < params.size() ? @@ -857,21 +857,21 @@ void register_commands() [](const CommandParameters& params, Context& context) { set_option(GlobalOptionManager::instance(), params, context); }, PerArgumentCommandCompleter({ - [](const Context& context, const String& prefix, size_t cursor_pos) + [](const Context& context, const String& prefix, CharCount cursor_pos) { return GlobalOptionManager::instance().complete_option_name(prefix, cursor_pos); } })); cm.register_commands({ "setb", "setbuffer" }, [](const CommandParameters& params, Context& context) { set_option(context.buffer().option_manager(), params, context); }, PerArgumentCommandCompleter({ - [](const Context& context, const String& prefix, size_t cursor_pos) + [](const Context& context, const String& prefix, CharCount cursor_pos) { return context.buffer().option_manager().complete_option_name(prefix, cursor_pos); } })); cm.register_commands({ "setw", "setwindow" }, [](const CommandParameters& params, Context& context) { set_option(context.window().option_manager(), params, context); }, PerArgumentCommandCompleter({ - [](const Context& context, const String& prefix, size_t cursor_pos) + [](const Context& context, const String& prefix, CharCount cursor_pos) { return context.window().option_manager().complete_option_name(prefix, cursor_pos); } })); } diff --git a/src/completion.cc b/src/completion.cc index 1d2a5e86..319da0f7 100644 --- a/src/completion.cc +++ b/src/completion.cc @@ -11,15 +11,15 @@ namespace Kakoune CandidateList complete_filename(const Context& context, const String& prefix, - size_t cursor_pos) + CharCount cursor_pos) { String real_prefix = prefix.substr(0, cursor_pos); String dirname = "./"; String dirprefix; String fileprefix = real_prefix; - size_t dir_end = -1; - for (size_t i = 0; i < real_prefix.length(); ++i) + CharCount dir_end = -1; + for (CharCount i = 0; i < real_prefix.length(); ++i) { if (real_prefix[i] == '/') dir_end = i; @@ -49,7 +49,7 @@ CandidateList complete_filename(const Context& context, String name = dirprefix + filename; if (entry->d_type == DT_DIR) name += '/'; - if (fileprefix.length() or filename[0] != '.') + if (fileprefix.length() != 0 and filename[0] != '.') result.push_back(name); } } diff --git a/src/completion.hh b/src/completion.hh index 775d558e..60462975 100644 --- a/src/completion.hh +++ b/src/completion.hh @@ -16,25 +16,25 @@ typedef std::vector CandidateList; struct Completions { CandidateList candidates; - size_t start; - size_t end; + CharCount start; + CharCount end; Completions() : start(0), end(0) {} - Completions(size_t start, size_t end) + Completions(CharCount start, CharCount end) : start(start), end(end) {} }; CandidateList complete_filename(const Context& context, const String& prefix, - size_t cursor_pos = -1); + CharCount cursor_pos = -1); typedef std::function Completer; + const String&, CharCount)> Completer; inline Completions complete_nothing(const Context& context, - const String&, size_t cursor_pos) + const String&, CharCount cursor_pos) { return Completions(cursor_pos, cursor_pos); } diff --git a/src/display_buffer.hh b/src/display_buffer.hh index f48807c4..110b6be2 100644 --- a/src/display_buffer.hh +++ b/src/display_buffer.hh @@ -12,7 +12,7 @@ namespace Kakoune struct DisplayCoord : LineAndColumn { - DisplayCoord(LineCount line = 0, int column = 0) + DisplayCoord(LineCount line = 0, CharCount column = 0) : LineAndColumn(line, column) {} template diff --git a/src/file.cc b/src/file.cc index ec6a4f67..936b6fb3 100644 --- a/src/file.cc +++ b/src/file.cc @@ -24,14 +24,14 @@ String parse_filename(const String& filename) if (filename.length() > 2 and filename[0] == '~' and filename[1] == '/') return parse_filename("$HOME/" + filename.substr(2)); - size_t pos = 0; + CharCount pos = 0; String result; - for (size_t i = 0; i < filename.length(); ++i) + for (CharCount i = 0; i < filename.length(); ++i) { if (filename[i] == '$' and (i == 0 or filename[i-1] != '\\')) { result += filename.substr(pos, i - pos); - size_t end = i+1; + CharCount end = i+1; while (end != filename.length() and isidentifier(filename[end])) ++end; String var_name = filename.substr(i+1, end - i - 1); diff --git a/src/filter_group.cc b/src/filter_group.cc index eabe0427..ea44bffa 100644 --- a/src/filter_group.cc +++ b/src/filter_group.cc @@ -40,13 +40,13 @@ FilterGroup& FilterGroup::get_group(const String& id) CandidateList FilterGroup::complete_id(const String& prefix, - size_t cursor_pos) + CharCount cursor_pos) { return m_filters.complete_id(prefix, cursor_pos); } CandidateList FilterGroup::complete_group_id(const String& prefix, - size_t cursor_pos) + CharCount cursor_pos) { return m_filters.complete_id_if( prefix, cursor_pos, diff --git a/src/filter_group.hh b/src/filter_group.hh index 7d1aa60f..8c93936b 100644 --- a/src/filter_group.hh +++ b/src/filter_group.hh @@ -19,8 +19,8 @@ public: FilterGroup& get_group(const String& id); - CandidateList complete_id(const String& prefix, size_t cursor_pos); - CandidateList complete_group_id(const String& prefix, size_t cursor_pos); + CandidateList complete_id(const String& prefix, CharCount cursor_pos); + CandidateList complete_group_id(const String& prefix, CharCount cursor_pos); private: idvaluemap m_filters; diff --git a/src/filter_registry.cc b/src/filter_registry.cc index a2a08266..65070bdf 100644 --- a/src/filter_registry.cc +++ b/src/filter_registry.cc @@ -31,7 +31,7 @@ void FilterRegistry::add_filter_to_group(FilterGroup& group, } CandidateList FilterRegistry::complete_filter(const String& prefix, - size_t cursor_pos) + CharCount cursor_pos) { return m_factories.complete_id(prefix, cursor_pos); } diff --git a/src/filter_registry.hh b/src/filter_registry.hh index cdae62ab..77763782 100644 --- a/src/filter_registry.hh +++ b/src/filter_registry.hh @@ -30,7 +30,7 @@ public: const FilterParameters& parameters); CandidateList complete_filter(const String& prefix, - size_t cursor_pos); + CharCount cursor_pos); private: idvaluemap m_factories; diff --git a/src/highlighter_group.cc b/src/highlighter_group.cc index 5203f2a6..57b21a5c 100644 --- a/src/highlighter_group.cc +++ b/src/highlighter_group.cc @@ -39,13 +39,13 @@ HighlighterGroup& HighlighterGroup::get_group(const String& id) CandidateList HighlighterGroup::complete_id(const String& prefix, - size_t cursor_pos) + CharCount cursor_pos) { return m_highlighters.complete_id(prefix, cursor_pos); } CandidateList HighlighterGroup::complete_group_id(const String& prefix, - size_t cursor_pos) + CharCount cursor_pos) { return m_highlighters.complete_id_if( prefix, cursor_pos, diff --git a/src/highlighter_group.hh b/src/highlighter_group.hh index 79128ac7..9b4f1bf0 100644 --- a/src/highlighter_group.hh +++ b/src/highlighter_group.hh @@ -22,8 +22,8 @@ public: HighlighterGroup& get_group(const String& id); - CandidateList complete_id(const String& prefix, size_t cursor_pos); - CandidateList complete_group_id(const String& prefix, size_t cursor_pos); + CandidateList complete_id(const String& prefix, CharCount cursor_pos); + CandidateList complete_group_id(const String& prefix, CharCount cursor_pos); private: idvaluemap m_highlighters; diff --git a/src/highlighter_registry.cc b/src/highlighter_registry.cc index b2ca4e97..46038aa3 100644 --- a/src/highlighter_registry.cc +++ b/src/highlighter_registry.cc @@ -33,7 +33,7 @@ void HighlighterRegistry::add_highlighter_to_group(Window& window, } CandidateList HighlighterRegistry::complete_highlighter(const String& prefix, - size_t cursor_pos) + CharCount cursor_pos) { return m_factories.complete_id(prefix, cursor_pos); } diff --git a/src/highlighter_registry.hh b/src/highlighter_registry.hh index 92da5c66..2a3f740d 100644 --- a/src/highlighter_registry.hh +++ b/src/highlighter_registry.hh @@ -30,7 +30,7 @@ public: const HighlighterParameters& parameters); CandidateList complete_highlighter(const String& prefix, - size_t cursor_pos); + CharCount cursor_pos); private: idvaluemap m_factories; diff --git a/src/idvaluemap.hh b/src/idvaluemap.hh index f6627872..c65f35fc 100644 --- a/src/idvaluemap.hh +++ b/src/idvaluemap.hh @@ -65,7 +65,7 @@ public: template CandidateList complete_id_if(const String& prefix, - size_t cursor_pos, + CharCount cursor_pos, _Condition condition) { String real_prefix = prefix.substr(0, cursor_pos); @@ -83,7 +83,7 @@ public: } CandidateList complete_id(const String& prefix, - size_t cursor_pos) + CharCount cursor_pos) { return complete_id_if( prefix, cursor_pos, [](const value_type&) { return true; }); diff --git a/src/keys.cc b/src/keys.cc index d231cfac..5acfdaed 100644 --- a/src/keys.cc +++ b/src/keys.cc @@ -14,11 +14,11 @@ static std::unordered_map keynamemap = { KeyList parse_keys(const String& str) { KeyList result; - for (size_t pos = 0; pos < str.length(); ++pos) + for (CharCount pos = 0; pos < str.length(); ++pos) { if (str[pos] == '<') { - size_t end_pos = pos; + CharCount end_pos = pos; while (end_pos < str.length() and str[end_pos] != '>') ++end_pos; diff --git a/src/line_and_column.hh b/src/line_and_column.hh index 3e03d1b9..cceaa459 100644 --- a/src/line_and_column.hh +++ b/src/line_and_column.hh @@ -10,9 +10,9 @@ template struct LineAndColumn { LineCount line; - int column; + CharCount column; - LineAndColumn(LineCount line = 0, int column = 0) + LineAndColumn(LineCount line = 0, CharCount column = 0) : line(line), column(column) {} EffectiveType operator+(const EffectiveType& other) const diff --git a/src/ncurses.cc b/src/ncurses.cc index b18e048e..c9dd6bde 100644 --- a/src/ncurses.cc +++ b/src/ncurses.cc @@ -113,7 +113,7 @@ void NCursesClient::draw_window(Window& window) String content = atom.content.content(); if (content[content.length()-1] == '\n') { - addnstr(content.c_str(), content.length() - 1); + addnstr(content.c_str(), (int)content.length() - 1); addch(' '); } else @@ -139,9 +139,9 @@ void NCursesClient::draw_window(Window& window) static int last_status_length = 0; move(max_y, max_x - last_status_length); clrtoeol(); - move(max_y, max_x - status_line.length()); + move(max_y, max_x - (int)status_line.length()); addstr(status_line.c_str()); - last_status_length = status_line.length(); + last_status_length = (int)status_line.length(); } Key NCursesClient::get_key() @@ -179,7 +179,7 @@ String NCursesClient::prompt(const String& text, const Context& context, Complet addstr(text.c_str()); clrtoeol(); - size_t cursor_pos = 0; + CharCount cursor_pos = 0; Completions completions; int current_completion = -1; @@ -285,7 +285,7 @@ String NCursesClient::prompt(const String& text, const Context& context, Complet else completion = completions.candidates[current_completion]; - move(max_y-1, text.length()); + move(max_y-1, (int)text.length()); result = result.substr(0, completions.start) + completion; cursor_pos = completions.start + completion.length(); break; @@ -296,10 +296,10 @@ String NCursesClient::prompt(const String& text, const Context& context, Complet ++cursor_pos; } - move(max_y - 1, text.length()); + move(max_y - 1, (int)text.length()); clrtoeol(); addstr(result.c_str()); - move(max_y - 1, text.length() + cursor_pos); + move(max_y - 1, (int)(text.length() + cursor_pos)); refresh(); } return result; diff --git a/src/option_manager.cc b/src/option_manager.cc index 487fca6b..1a09361d 100644 --- a/src/option_manager.cc +++ b/src/option_manager.cc @@ -57,7 +57,7 @@ const Option& OptionManager::operator[](const String& name) const } CandidateList OptionManager::complete_option_name(const String& prefix, - size_t cursor_pos) + CharCount cursor_pos) { String real_prefix = prefix.substr(0, cursor_pos); CandidateList result; diff --git a/src/option_manager.hh b/src/option_manager.hh index f6d21bd2..221239fd 100644 --- a/src/option_manager.hh +++ b/src/option_manager.hh @@ -57,7 +57,7 @@ public: void set_option(const String& name, const Option& value); CandidateList complete_option_name(const String& prefix, - size_t cursor_pos); + CharCount cursor_pos); typedef std::unordered_map OptionMap; OptionMap flatten_options() const; diff --git a/src/string.hh b/src/string.hh index cd5cfe1d..58dd79b7 100644 --- a/src/string.hh +++ b/src/string.hh @@ -5,6 +5,7 @@ #include #include "memoryview.hh" +#include "units.hh" namespace Kakoune { @@ -23,8 +24,8 @@ public: template String(Iterator begin, Iterator end) : m_content(begin, end) {} - Character operator[](size_t pos) const { return static_cast(m_content[pos]); } - size_t length() const { return m_content.length(); } + Character operator[](CharCount pos) const { return static_cast(m_content[(int)pos]); } + CharCount length() const { return m_content.length(); } bool empty() const { return m_content.empty(); } bool operator== (const String& other) const { return m_content == other.m_content; } @@ -43,7 +44,7 @@ public: memoryview data() const { return memoryview(m_content.data(), m_content.size()); } const char* c_str() const { return m_content.c_str(); } - String substr(size_t pos, size_t length = -1) const { return String(m_content.substr(pos, length)); } + String substr(CharCount pos, CharCount length = -1) const { return String(m_content.substr((int)pos, (int)length)); } class iterator { diff --git a/src/units.hh b/src/units.hh index 8a1180de..ca9eb39a 100644 --- a/src/units.hh +++ b/src/units.hh @@ -67,6 +67,9 @@ public: bool operator>=(const RealType& other) const { return m_value >= other.m_value; } + bool operator!() const + { return !m_value; } + explicit operator ValueType() const { return m_value; } private: ValueType m_value; @@ -82,6 +85,16 @@ inline LineCount operator"" _line(unsigned long long int value) return LineCount(value); } +struct CharCount : public StronglyTypedInteger +{ + CharCount(int value) : StronglyTypedInteger(value) {} +}; + +inline CharCount operator"" _char(unsigned long long int value) +{ + return CharCount(value); +} + } #endif // units_hh_INCLUDED diff --git a/src/window.cc b/src/window.cc index cc60d1f1..c330a7ec 100644 --- a/src/window.cc +++ b/src/window.cc @@ -56,7 +56,7 @@ void Window::update_display_buffer() BufferIterator line_end = buffer().iterator_at_line_end(pos); BufferIterator end; - if (line_end - pos > m_dimensions.column) + if (CharCount(line_end - pos) > m_dimensions.column) end = pos + m_dimensions.column; else end = line_end; @@ -100,7 +100,7 @@ void Window::scroll_to_keep_cursor_visible_ifn() // (this is only valid if highlighting one line and multiple lines put // the cursor in the same position, however I do not find any sane example // of highlighters not doing that) - int column = 0; + CharCount column = 0; for (auto& atom : lines.back()) { if (atom.content.has_buffer_range() and @@ -136,7 +136,7 @@ String Window::status_line() const oss << buffer().name(); if (buffer().is_modified()) oss << " [+]"; - oss << " -- " << (int)cursor.line+1 << "," << cursor.column+1 + oss << " -- " << (int)cursor.line+1 << "," << (int)cursor.column+1 << " -- " << selections().size() << " sel -- "; if (is_editing()) oss << "[Insert]";