1
1
mirror of https://github.com/mawww/kakoune.git synced 2024-12-26 04:52:53 +03:00

use a strongly typed int CharCount for character counts

This commit is contained in:
Maxime Coste 2012-08-23 23:56:35 +02:00
parent 0d8cce2728
commit e4fd839dda
29 changed files with 134 additions and 123 deletions

View File

@ -66,10 +66,10 @@ BufferCoord Buffer::line_and_column_at(const BufferIterator& iterator) const
return iterator.m_coord; return iterator.m_coord;
} }
BufferSize Buffer::line_length(LineCount line) const CharCount Buffer::line_length(LineCount line) const
{ {
assert(line < line_count()); assert(line < line_count());
BufferPos end = (line < line_count() - 1) ? CharCount end = (line < line_count() - 1) ?
m_lines[line + 1].start : character_count(); m_lines[line + 1].start : character_count();
return end - m_lines[line].start; 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); BufferCoord result(line_and_column.line, line_and_column.column);
result.line = Kakoune::clamp(0_line, line_count() - 1, result.line); 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)); CharCount max_col = std::max(0_char, line_length(result.line) - (avoid_eol ? 2 : 1));
result.column = Kakoune::clamp<int>(0, max_col, result.column); result.column = Kakoune::clamp(0_char, max_col, result.column);
return result; return result;
} }
@ -120,10 +120,10 @@ BufferIterator Buffer::end() const
{ {
if (m_lines.empty()) if (m_lines.empty())
return BufferIterator(*this, { 0_line, 0 }); 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()) if (m_lines.empty())
return 0; return 0;
@ -140,10 +140,10 @@ String Buffer::string(const BufferIterator& begin, const BufferIterator& end) co
String res; String res;
for (LineCount line = begin.line(); line <= end.line(); ++line) for (LineCount line = begin.line(); line <= end.line(); ++line)
{ {
size_t start = 0; CharCount start = 0;
if (line == begin.line()) if (line == begin.line())
start = begin.column(); start = begin.column();
size_t count = -1; CharCount count = -1;
if (line == end.line()) if (line == end.line())
count = end.column() - start; count = end.column() - start;
res += m_lines[line].content.substr(start, count); res += m_lines[line].content.substr(start, count);
@ -231,7 +231,7 @@ void Buffer::reset_undo_data()
void Buffer::check_invariant() const void Buffer::check_invariant() const
{ {
BufferSize start = 0; CharCount start = 0;
assert(not m_lines.empty()); assert(not m_lines.empty());
for (auto& line : m_lines) for (auto& line : m_lines)
{ {
@ -245,7 +245,7 @@ void Buffer::check_invariant() const
void Buffer::do_insert(const BufferIterator& pos, const String& content) void Buffer::do_insert(const BufferIterator& pos, const String& content)
{ {
++m_timestamp; ++m_timestamp;
BufferSize offset = pos.offset(); CharCount offset = pos.offset();
// all following lines advanced by length // all following lines advanced by length
for (LineCount i = pos.line()+1; i < line_count(); ++i) 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' // line without inserting a '\n'
if (pos == end() and (pos == begin() or *(pos-1) == '\n')) if (pos == end() and (pos == begin() or *(pos-1) == '\n'))
{ {
int start = 0; CharCount start = 0;
for (int i = 0; i < content.length(); ++i) for (CharCount i = 0; i < content.length(); ++i)
{ {
if (content[i] == '\n') 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(); auto line_it = m_lines.begin() + (int)pos.line();
line_it = m_lines.erase(line_it); line_it = m_lines.erase(line_it);
int start = 0; CharCount start = 0;
for (int i = 0; i < content.length(); ++i) for (CharCount i = 0; i < content.length(); ++i)
{ {
if (content[i] == '\n') if (content[i] == '\n')
{ {
@ -289,7 +289,7 @@ void Buffer::do_insert(const BufferIterator& pos, const String& content)
if (start == 0) if (start == 0)
{ {
line_content = prefix + line_content; 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) }); std::move(line_content) });
} }
else else
@ -301,7 +301,7 @@ void Buffer::do_insert(const BufferIterator& pos, const String& content)
} }
} }
if (start == 0) 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()) else if (start != content.length() or not suffix.empty())
line_it = m_lines.insert(line_it, { offset + start, content.substr(start) + suffix }); line_it = m_lines.insert(line_it, { offset + start, content.substr(start) + suffix });
else else
@ -309,7 +309,7 @@ void Buffer::do_insert(const BufferIterator& pos, const String& content)
begin_it = pos; begin_it = pos;
end_it = BufferIterator(*this, { LineCount(line_it - m_lines.begin()), end_it = BufferIterator(*this, { LineCount(line_it - m_lines.begin()),
int(line_it->length() - suffix.length()) }); line_it->length() - suffix.length() });
} }
check_invariant(); check_invariant();
@ -318,7 +318,7 @@ void Buffer::do_insert(const BufferIterator& pos, const String& content)
listener->on_insert(begin_it, end_it); 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; ++m_timestamp;
BufferIterator end = pos + length; 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 }; 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); 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)); m_lines.insert(m_lines.begin() + (int)pos.line(), std::move(new_line));
for (LineCount i = pos.line()+1; i < line_count(); ++i) for (LineCount i = pos.line()+1; i < line_count(); ++i)
@ -356,7 +356,7 @@ void Buffer::apply_modification(const Modification& modification)
} }
case Modification::Erase: case Modification::Erase:
{ {
size_t count = modification.content.length(); CharCount count = modification.content.length();
assert(string(modification.position, modification.position + count) assert(string(modification.position, modification.position + count)
== modification.content); == modification.content);
do_erase(modification.position, count); do_erase(modification.position, count);

View File

@ -17,12 +17,9 @@ namespace Kakoune
class Buffer; class Buffer;
class Window; class Window;
typedef int BufferPos;
typedef int BufferSize;
struct BufferCoord : LineAndColumn<BufferCoord> struct BufferCoord : LineAndColumn<BufferCoord>
{ {
BufferCoord(LineCount line = 0, int column = 0) BufferCoord(LineCount line = 0, CharCount column = 0)
: LineAndColumn(line, column) {} : LineAndColumn(line, column) {}
template<typename T> template<typename T>
@ -35,7 +32,7 @@ class BufferIterator
{ {
public: public:
typedef Character value_type; typedef Character value_type;
typedef BufferSize difference_type; typedef size_t difference_type;
typedef const value_type* pointer; typedef const value_type* pointer;
typedef const value_type& reference; typedef const value_type& reference;
typedef std::bidirectional_iterator_tag iterator_category; typedef std::bidirectional_iterator_tag iterator_category;
@ -51,13 +48,13 @@ public:
bool operator>= (const BufferIterator& iterator) const; bool operator>= (const BufferIterator& iterator) const;
Character operator* () const; Character operator* () const;
BufferSize operator- (const BufferIterator& iterator) const; size_t operator- (const BufferIterator& iterator) const;
BufferIterator operator+ (BufferSize size) const; BufferIterator operator+ (CharCount size) const;
BufferIterator operator- (BufferSize size) const; BufferIterator operator- (CharCount size) const;
BufferIterator& operator+= (BufferSize size); BufferIterator& operator+= (CharCount size);
BufferIterator& operator-= (BufferSize size); BufferIterator& operator-= (CharCount size);
BufferIterator& operator++ (); BufferIterator& operator++ ();
BufferIterator& operator-- (); BufferIterator& operator-- ();
@ -74,10 +71,10 @@ public:
const Buffer& buffer() const; const Buffer& buffer() const;
const BufferCoord& coord() const { return m_coord; } const BufferCoord& coord() const { return m_coord; }
LineCount line() const { return m_coord.line; } LineCount line() const { return m_coord.line; }
BufferSize column() const { return m_coord.column; } CharCount column() const { return m_coord.column; }
private: private:
BufferSize offset() const; CharCount offset() const;
const Buffer* m_buffer; const Buffer* m_buffer;
BufferCoord m_coord; BufferCoord m_coord;
@ -130,7 +127,7 @@ public:
BufferIterator begin() const; BufferIterator begin() const;
BufferIterator end() const; BufferIterator end() const;
BufferSize character_count() const; CharCount character_count() const;
LineCount line_count() const; LineCount line_count() const;
// returns an iterator at given coordinates. line_and_column is // returns an iterator at given coordinates. line_and_column is
@ -187,10 +184,10 @@ private:
struct Line struct Line
{ {
BufferPos start; CharCount start;
String content; String content;
size_t length() const { return content.length(); } CharCount length() const { return content.length(); }
}; };
struct LineList : std::vector<Line> struct LineList : std::vector<Line>
{ {
@ -204,9 +201,9 @@ private:
LineList m_lines; LineList m_lines;
void do_insert(const BufferIterator& pos, const String& content); 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; String m_name;
const Type m_type; const Type m_type;

View File

@ -114,25 +114,25 @@ inline Character BufferIterator::operator*() const
return m_buffer->m_lines[line()].content[column()]; return m_buffer->m_lines[line()].content[column()];
} }
inline BufferSize BufferIterator::offset() const inline CharCount BufferIterator::offset() const
{ {
assert(m_buffer); assert(m_buffer);
return line() == 0 ? column() 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); 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); assert(m_buffer);
if (size >= 0) 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) for (LineCount i = line() + 1; i < m_buffer->line_count(); ++i)
{ {
if (m_buffer->m_lines[i].start > o) if (m_buffer->m_lines[i].start > o)
@ -144,12 +144,12 @@ inline BufferIterator BufferIterator::operator+(BufferSize size) const
return operator-(-size); return operator-(-size);
} }
inline BufferIterator BufferIterator::operator-(BufferSize size) const inline BufferIterator BufferIterator::operator-(CharCount size) const
{ {
assert(m_buffer); assert(m_buffer);
if (size >= 0) 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) for (LineCount i = line(); i >= 0; --i)
{ {
if (m_buffer->m_lines[i].start <= o) if (m_buffer->m_lines[i].start <= o)
@ -160,12 +160,12 @@ inline BufferIterator BufferIterator::operator-(BufferSize size) const
return operator+(-size); return operator+(-size);
} }
inline BufferIterator& BufferIterator::operator+=(BufferSize size) inline BufferIterator& BufferIterator::operator+=(CharCount size)
{ {
return *this = (*this + size); return *this = (*this + size);
} }
inline BufferIterator& BufferIterator::operator-=(BufferSize size) inline BufferIterator& BufferIterator::operator-=(CharCount size)
{ {
return *this = (*this - size); return *this = (*this - size);
} }

View File

@ -53,7 +53,7 @@ Buffer* BufferManager::get_buffer(const String& name)
} }
CandidateList BufferManager::complete_buffername(const String& prefix, CandidateList BufferManager::complete_buffername(const String& prefix,
size_t cursor_pos) CharCount cursor_pos)
{ {
String real_prefix = prefix.substr(0, cursor_pos); String real_prefix = prefix.substr(0, cursor_pos);
CandidateList result; CandidateList result;

View File

@ -29,7 +29,7 @@ public:
Buffer* get_buffer(const String& name); Buffer* get_buffer(const String& name);
CandidateList complete_buffername(const String& prefix, CandidateList complete_buffername(const String& prefix,
size_t cursor_pos = String::npos); CharCount cursor_pos = -1);
private: private:
BufferList m_buffers; BufferList m_buffers;

View File

@ -61,7 +61,7 @@ private:
using TokenList = std::vector<Token>; using TokenList = std::vector<Token>;
using TokenPosList = std::vector<std::pair<size_t, size_t>>; using TokenPosList = std::vector<std::pair<CharCount, CharCount>>;
bool is_command_separator(Character c) bool is_command_separator(Character c)
{ {
@ -78,8 +78,8 @@ TokenList parse(const String& line,
{ {
TokenList result; TokenList result;
size_t length = line.length(); CharCount length = line.length();
size_t pos = 0; CharCount pos = 0;
while (pos < length) while (pos < length)
{ {
while (pos != length) while (pos != length)
@ -92,7 +92,7 @@ TokenList parse(const String& line,
break; break;
} }
size_t token_start = pos; CharCount token_start = pos;
Token::Type type = Token::Type::Raw; Token::Type type = Token::Type::Raw;
if (line[pos] == '"' or line[pos] == '\'') if (line[pos] == '"' or line[pos] == '\'')
@ -107,7 +107,7 @@ TokenList parse(const String& line,
} }
else if (line[pos] == '%') else if (line[pos] == '%')
{ {
size_t type_start = ++pos; CharCount type_start = ++pos;
while (isalpha(line[pos])) while (isalpha(line[pos]))
++pos; ++pos;
String type_name = line.substr(type_start, pos - type_start); 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, Completions CommandManager::complete(const Context& context,
const String& command_line, size_t cursor_pos) const String& command_line, CharCount cursor_pos)
{ {
TokenPosList pos_info; TokenPosList pos_info;
TokenList tokens = parse(command_line, &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 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); Completions result(cmd_start, cursor_pos);
String prefix = command_line.substr(cmd_start, String prefix = command_line.substr(cmd_start,
cursor_pos - 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) if (command_it == m_commands.end() or not command_it->second.completer)
return Completions(); 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; pos_info[token_to_complete].first : cursor_pos;
Completions result(start , 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<String> params; std::vector<String> params;
for (auto token_it = tokens.begin()+1; token_it != tokens.end(); ++token_it) 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, CandidateList PerArgumentCommandCompleter::operator()(const Context& context,
const CommandParameters& params, const CommandParameters& params,
size_t token_to_complete, size_t token_to_complete,
size_t pos_in_token) const CharCount pos_in_token) const
{ {
if (token_to_complete >= m_completers.size()) if (token_to_complete >= m_completers.size())
return CandidateList(); return CandidateList();

View File

@ -28,13 +28,13 @@ typedef std::function<void (const CommandParameters&,
typedef std::function<CandidateList (const Context& context, typedef std::function<CandidateList (const Context& context,
const CommandParameters&, const CommandParameters&,
size_t, size_t)> CommandCompleter; size_t, CharCount)> CommandCompleter;
class PerArgumentCommandCompleter class PerArgumentCommandCompleter
{ {
public: public:
typedef std::function<CandidateList (const Context&, typedef std::function<CandidateList (const Context&,
const String&, size_t)> ArgumentCompleter; const String&, CharCount)> ArgumentCompleter;
typedef memoryview<ArgumentCompleter> ArgumentCompleterList; typedef memoryview<ArgumentCompleter> ArgumentCompleterList;
PerArgumentCommandCompleter(const ArgumentCompleterList& completers) PerArgumentCommandCompleter(const ArgumentCompleterList& completers)
@ -43,7 +43,7 @@ public:
CandidateList operator()(const Context& context, CandidateList operator()(const Context& context,
const CommandParameters& params, const CommandParameters& params,
size_t token_to_complete, size_t token_to_complete,
size_t pos_in_token) const; CharCount pos_in_token) const;
private: private:
std::vector<ArgumentCompleter> m_completers; std::vector<ArgumentCompleter> m_completers;
@ -56,7 +56,7 @@ public:
const EnvVarMap& env_vars = EnvVarMap()); const EnvVarMap& env_vars = EnvVarMap());
Completions complete(const Context& context, 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; bool command_defined(const String& command_name) const;

View File

@ -259,7 +259,7 @@ void edit(const CommandParameters& params, Context& context)
int column = param_count > 2 ? int column = param_count > 2 ?
std::max(0, str_to_int(parser[2]) - 1) : 0; 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(); window.center_selection();
} }
@ -536,11 +536,11 @@ void define_command(const CommandParameters& params, Context& context)
{ {
String shell_cmd = parser.option_value("shell-completion"); String shell_cmd = parser.option_value("shell-completion");
auto completer = [=](const Context& context, const CommandParameters& params, 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); EnvVarMap vars = params_to_env_var_map(params);
vars["token_to_complete"] = int_to_str(token_to_complete); 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); String output = ShellManager::instance().eval(shell_cmd, context, vars);
return split(output, '\n'); return split(output, '\n');
}; };
@ -577,14 +577,14 @@ void exec_commands_in_runtime_file(const CommandParameters& params,
const String& filename = params[0]; const String& filename = params[0];
char buffer[2048]; char buffer[2048];
#if defined(__linux__) #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); assert(res != -1);
buffer[res] = '\0'; buffer[res] = '\0';
#elif defined(__APPLE__) #elif defined(__APPLE__)
uint32_t bufsize = 2048 - filename.length(); uint32_t bufsize = 2048 - filename.length();
_NSGetExecutablePath(buffer, &bufsize); _NSGetExecutablePath(buffer, &bufsize);
char* canonical_path = realpath(buffer, NULL); char* canonical_path = realpath(buffer, NULL);
strncpy(buffer, canonical_path, 2048 - filename.length()); strncpy(buffer, canonical_path, 2048 - (int)filename.length());
free(canonical_path); free(canonical_path);
#else #else
# error "finding executable path is not implemented on this platform" # error "finding executable path is not implemented on this platform"
@ -778,7 +778,7 @@ void register_commands()
cm.register_command("wq!", write_and_quit<true>); cm.register_command("wq!", write_and_quit<true>);
PerArgumentCommandCompleter buffer_completer({ 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); } { return BufferManager::instance().complete_buffername(prefix, cursor_pos); }
}); });
cm.register_commands({ "b", "buffer" }, show_buffer, buffer_completer); cm.register_commands({ "b", "buffer" }, show_buffer, buffer_completer);
@ -786,7 +786,7 @@ void register_commands()
cm.register_commands({ "ah", "addhl" }, add_highlighter, cm.register_commands({ "ah", "addhl" }, add_highlighter,
[](const Context& context, const CommandParameters& params, [](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(); Window& w = context.window();
const String& arg = token_to_complete < params.size() ? const String& arg = token_to_complete < params.size() ?
@ -800,7 +800,7 @@ void register_commands()
}); });
cm.register_commands({ "rh", "rmhl" }, rm_highlighter, cm.register_commands({ "rh", "rmhl" }, rm_highlighter,
[](const Context& context, const CommandParameters& params, [](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(); Window& w = context.window();
const String& arg = token_to_complete < params.size() ? const String& arg = token_to_complete < params.size() ?
@ -814,7 +814,7 @@ void register_commands()
}); });
cm.register_commands({ "af", "addfilter" }, add_filter, cm.register_commands({ "af", "addfilter" }, add_filter,
[](const Context& context, const CommandParameters& params, [](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(); Window& w = context.window();
const String& arg = token_to_complete < params.size() ? const String& arg = token_to_complete < params.size() ?
@ -828,7 +828,7 @@ void register_commands()
}); });
cm.register_commands({ "rf", "rmfilter" }, rm_filter, cm.register_commands({ "rf", "rmfilter" }, rm_filter,
[](const Context& context, const CommandParameters& params, [](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(); Window& w = context.window();
const String& arg = token_to_complete < params.size() ? const String& arg = token_to_complete < params.size() ?
@ -857,21 +857,21 @@ void register_commands()
[](const CommandParameters& params, Context& context) [](const CommandParameters& params, Context& context)
{ set_option(GlobalOptionManager::instance(), params, context); }, { set_option(GlobalOptionManager::instance(), params, context); },
PerArgumentCommandCompleter({ 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); } { return GlobalOptionManager::instance().complete_option_name(prefix, cursor_pos); }
})); }));
cm.register_commands({ "setb", "setbuffer" }, cm.register_commands({ "setb", "setbuffer" },
[](const CommandParameters& params, Context& context) [](const CommandParameters& params, Context& context)
{ set_option(context.buffer().option_manager(), params, context); }, { set_option(context.buffer().option_manager(), params, context); },
PerArgumentCommandCompleter({ 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); } { return context.buffer().option_manager().complete_option_name(prefix, cursor_pos); }
})); }));
cm.register_commands({ "setw", "setwindow" }, cm.register_commands({ "setw", "setwindow" },
[](const CommandParameters& params, Context& context) [](const CommandParameters& params, Context& context)
{ set_option(context.window().option_manager(), params, context); }, { set_option(context.window().option_manager(), params, context); },
PerArgumentCommandCompleter({ 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); } { return context.window().option_manager().complete_option_name(prefix, cursor_pos); }
})); }));
} }

View File

@ -11,15 +11,15 @@ namespace Kakoune
CandidateList complete_filename(const Context& context, CandidateList complete_filename(const Context& context,
const String& prefix, const String& prefix,
size_t cursor_pos) CharCount cursor_pos)
{ {
String real_prefix = prefix.substr(0, cursor_pos); String real_prefix = prefix.substr(0, cursor_pos);
String dirname = "./"; String dirname = "./";
String dirprefix; String dirprefix;
String fileprefix = real_prefix; String fileprefix = real_prefix;
size_t dir_end = -1; CharCount dir_end = -1;
for (size_t i = 0; i < real_prefix.length(); ++i) for (CharCount i = 0; i < real_prefix.length(); ++i)
{ {
if (real_prefix[i] == '/') if (real_prefix[i] == '/')
dir_end = i; dir_end = i;
@ -49,7 +49,7 @@ CandidateList complete_filename(const Context& context,
String name = dirprefix + filename; String name = dirprefix + filename;
if (entry->d_type == DT_DIR) if (entry->d_type == DT_DIR)
name += '/'; name += '/';
if (fileprefix.length() or filename[0] != '.') if (fileprefix.length() != 0 and filename[0] != '.')
result.push_back(name); result.push_back(name);
} }
} }

View File

@ -16,25 +16,25 @@ typedef std::vector<String> CandidateList;
struct Completions struct Completions
{ {
CandidateList candidates; CandidateList candidates;
size_t start; CharCount start;
size_t end; CharCount end;
Completions() Completions()
: start(0), end(0) {} : start(0), end(0) {}
Completions(size_t start, size_t end) Completions(CharCount start, CharCount end)
: start(start), end(end) {} : start(start), end(end) {}
}; };
CandidateList complete_filename(const Context& context, CandidateList complete_filename(const Context& context,
const String& prefix, const String& prefix,
size_t cursor_pos = -1); CharCount cursor_pos = -1);
typedef std::function<Completions (const Context&, typedef std::function<Completions (const Context&,
const String&, size_t)> Completer; const String&, CharCount)> Completer;
inline Completions complete_nothing(const Context& context, inline Completions complete_nothing(const Context& context,
const String&, size_t cursor_pos) const String&, CharCount cursor_pos)
{ {
return Completions(cursor_pos, cursor_pos); return Completions(cursor_pos, cursor_pos);
} }

View File

@ -12,7 +12,7 @@ namespace Kakoune
struct DisplayCoord : LineAndColumn<DisplayCoord> struct DisplayCoord : LineAndColumn<DisplayCoord>
{ {
DisplayCoord(LineCount line = 0, int column = 0) DisplayCoord(LineCount line = 0, CharCount column = 0)
: LineAndColumn(line, column) {} : LineAndColumn(line, column) {}
template<typename T> template<typename T>

View File

@ -24,14 +24,14 @@ String parse_filename(const String& filename)
if (filename.length() > 2 and filename[0] == '~' and filename[1] == '/') if (filename.length() > 2 and filename[0] == '~' and filename[1] == '/')
return parse_filename("$HOME/" + filename.substr(2)); return parse_filename("$HOME/" + filename.substr(2));
size_t pos = 0; CharCount pos = 0;
String result; 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] != '\\')) if (filename[i] == '$' and (i == 0 or filename[i-1] != '\\'))
{ {
result += filename.substr(pos, i - pos); result += filename.substr(pos, i - pos);
size_t end = i+1; CharCount end = i+1;
while (end != filename.length() and isidentifier(filename[end])) while (end != filename.length() and isidentifier(filename[end]))
++end; ++end;
String var_name = filename.substr(i+1, end - i - 1); String var_name = filename.substr(i+1, end - i - 1);

View File

@ -40,13 +40,13 @@ FilterGroup& FilterGroup::get_group(const String& id)
CandidateList FilterGroup::complete_id(const String& prefix, CandidateList FilterGroup::complete_id(const String& prefix,
size_t cursor_pos) CharCount cursor_pos)
{ {
return m_filters.complete_id(prefix, cursor_pos); return m_filters.complete_id(prefix, cursor_pos);
} }
CandidateList FilterGroup::complete_group_id(const String& prefix, CandidateList FilterGroup::complete_group_id(const String& prefix,
size_t cursor_pos) CharCount cursor_pos)
{ {
return m_filters.complete_id_if( return m_filters.complete_id_if(
prefix, cursor_pos, prefix, cursor_pos,

View File

@ -19,8 +19,8 @@ public:
FilterGroup& get_group(const String& id); FilterGroup& get_group(const String& id);
CandidateList complete_id(const String& prefix, size_t cursor_pos); CandidateList complete_id(const String& prefix, CharCount cursor_pos);
CandidateList complete_group_id(const String& prefix, size_t cursor_pos); CandidateList complete_group_id(const String& prefix, CharCount cursor_pos);
private: private:
idvaluemap<String, FilterFunc> m_filters; idvaluemap<String, FilterFunc> m_filters;

View File

@ -31,7 +31,7 @@ void FilterRegistry::add_filter_to_group(FilterGroup& group,
} }
CandidateList FilterRegistry::complete_filter(const String& prefix, CandidateList FilterRegistry::complete_filter(const String& prefix,
size_t cursor_pos) CharCount cursor_pos)
{ {
return m_factories.complete_id(prefix, cursor_pos); return m_factories.complete_id(prefix, cursor_pos);
} }

View File

@ -30,7 +30,7 @@ public:
const FilterParameters& parameters); const FilterParameters& parameters);
CandidateList complete_filter(const String& prefix, CandidateList complete_filter(const String& prefix,
size_t cursor_pos); CharCount cursor_pos);
private: private:
idvaluemap<String, FilterFactory> m_factories; idvaluemap<String, FilterFactory> m_factories;

View File

@ -39,13 +39,13 @@ HighlighterGroup& HighlighterGroup::get_group(const String& id)
CandidateList HighlighterGroup::complete_id(const String& prefix, CandidateList HighlighterGroup::complete_id(const String& prefix,
size_t cursor_pos) CharCount cursor_pos)
{ {
return m_highlighters.complete_id(prefix, cursor_pos); return m_highlighters.complete_id(prefix, cursor_pos);
} }
CandidateList HighlighterGroup::complete_group_id(const String& prefix, CandidateList HighlighterGroup::complete_group_id(const String& prefix,
size_t cursor_pos) CharCount cursor_pos)
{ {
return m_highlighters.complete_id_if( return m_highlighters.complete_id_if(
prefix, cursor_pos, prefix, cursor_pos,

View File

@ -22,8 +22,8 @@ public:
HighlighterGroup& get_group(const String& id); HighlighterGroup& get_group(const String& id);
CandidateList complete_id(const String& prefix, size_t cursor_pos); CandidateList complete_id(const String& prefix, CharCount cursor_pos);
CandidateList complete_group_id(const String& prefix, size_t cursor_pos); CandidateList complete_group_id(const String& prefix, CharCount cursor_pos);
private: private:
idvaluemap<String, HighlighterFunc> m_highlighters; idvaluemap<String, HighlighterFunc> m_highlighters;

View File

@ -33,7 +33,7 @@ void HighlighterRegistry::add_highlighter_to_group(Window& window,
} }
CandidateList HighlighterRegistry::complete_highlighter(const String& prefix, CandidateList HighlighterRegistry::complete_highlighter(const String& prefix,
size_t cursor_pos) CharCount cursor_pos)
{ {
return m_factories.complete_id(prefix, cursor_pos); return m_factories.complete_id(prefix, cursor_pos);
} }

View File

@ -30,7 +30,7 @@ public:
const HighlighterParameters& parameters); const HighlighterParameters& parameters);
CandidateList complete_highlighter(const String& prefix, CandidateList complete_highlighter(const String& prefix,
size_t cursor_pos); CharCount cursor_pos);
private: private:
idvaluemap<String, HighlighterFactory> m_factories; idvaluemap<String, HighlighterFactory> m_factories;

View File

@ -65,7 +65,7 @@ public:
template<typename _Condition> template<typename _Condition>
CandidateList complete_id_if(const String& prefix, CandidateList complete_id_if(const String& prefix,
size_t cursor_pos, CharCount cursor_pos,
_Condition condition) _Condition condition)
{ {
String real_prefix = prefix.substr(0, cursor_pos); String real_prefix = prefix.substr(0, cursor_pos);
@ -83,7 +83,7 @@ public:
} }
CandidateList complete_id(const String& prefix, CandidateList complete_id(const String& prefix,
size_t cursor_pos) CharCount cursor_pos)
{ {
return complete_id_if( return complete_id_if(
prefix, cursor_pos, [](const value_type&) { return true; }); prefix, cursor_pos, [](const value_type&) { return true; });

View File

@ -14,11 +14,11 @@ static std::unordered_map<String, Character> keynamemap = {
KeyList parse_keys(const String& str) KeyList parse_keys(const String& str)
{ {
KeyList result; KeyList result;
for (size_t pos = 0; pos < str.length(); ++pos) for (CharCount pos = 0; pos < str.length(); ++pos)
{ {
if (str[pos] == '<') if (str[pos] == '<')
{ {
size_t end_pos = pos; CharCount end_pos = pos;
while (end_pos < str.length() and str[end_pos] != '>') while (end_pos < str.length() and str[end_pos] != '>')
++end_pos; ++end_pos;

View File

@ -10,9 +10,9 @@ template<typename EffectiveType>
struct LineAndColumn struct LineAndColumn
{ {
LineCount line; LineCount line;
int column; CharCount column;
LineAndColumn(LineCount line = 0, int column = 0) LineAndColumn(LineCount line = 0, CharCount column = 0)
: line(line), column(column) {} : line(line), column(column) {}
EffectiveType operator+(const EffectiveType& other) const EffectiveType operator+(const EffectiveType& other) const

View File

@ -113,7 +113,7 @@ void NCursesClient::draw_window(Window& window)
String content = atom.content.content(); String content = atom.content.content();
if (content[content.length()-1] == '\n') if (content[content.length()-1] == '\n')
{ {
addnstr(content.c_str(), content.length() - 1); addnstr(content.c_str(), (int)content.length() - 1);
addch(' '); addch(' ');
} }
else else
@ -139,9 +139,9 @@ void NCursesClient::draw_window(Window& window)
static int last_status_length = 0; static int last_status_length = 0;
move(max_y, max_x - last_status_length); move(max_y, max_x - last_status_length);
clrtoeol(); clrtoeol();
move(max_y, max_x - status_line.length()); move(max_y, max_x - (int)status_line.length());
addstr(status_line.c_str()); addstr(status_line.c_str());
last_status_length = status_line.length(); last_status_length = (int)status_line.length();
} }
Key NCursesClient::get_key() Key NCursesClient::get_key()
@ -179,7 +179,7 @@ String NCursesClient::prompt(const String& text, const Context& context, Complet
addstr(text.c_str()); addstr(text.c_str());
clrtoeol(); clrtoeol();
size_t cursor_pos = 0; CharCount cursor_pos = 0;
Completions completions; Completions completions;
int current_completion = -1; int current_completion = -1;
@ -285,7 +285,7 @@ String NCursesClient::prompt(const String& text, const Context& context, Complet
else else
completion = completions.candidates[current_completion]; 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; result = result.substr(0, completions.start) + completion;
cursor_pos = completions.start + completion.length(); cursor_pos = completions.start + completion.length();
break; break;
@ -296,10 +296,10 @@ String NCursesClient::prompt(const String& text, const Context& context, Complet
++cursor_pos; ++cursor_pos;
} }
move(max_y - 1, text.length()); move(max_y - 1, (int)text.length());
clrtoeol(); clrtoeol();
addstr(result.c_str()); addstr(result.c_str());
move(max_y - 1, text.length() + cursor_pos); move(max_y - 1, (int)(text.length() + cursor_pos));
refresh(); refresh();
} }
return result; return result;

View File

@ -57,7 +57,7 @@ const Option& OptionManager::operator[](const String& name) const
} }
CandidateList OptionManager::complete_option_name(const String& prefix, CandidateList OptionManager::complete_option_name(const String& prefix,
size_t cursor_pos) CharCount cursor_pos)
{ {
String real_prefix = prefix.substr(0, cursor_pos); String real_prefix = prefix.substr(0, cursor_pos);
CandidateList result; CandidateList result;

View File

@ -57,7 +57,7 @@ public:
void set_option(const String& name, const Option& value); void set_option(const String& name, const Option& value);
CandidateList complete_option_name(const String& prefix, CandidateList complete_option_name(const String& prefix,
size_t cursor_pos); CharCount cursor_pos);
typedef std::unordered_map<String, Option> OptionMap; typedef std::unordered_map<String, Option> OptionMap;
OptionMap flatten_options() const; OptionMap flatten_options() const;

View File

@ -5,6 +5,7 @@
#include <iosfwd> #include <iosfwd>
#include "memoryview.hh" #include "memoryview.hh"
#include "units.hh"
namespace Kakoune namespace Kakoune
{ {
@ -23,8 +24,8 @@ public:
template<typename Iterator> template<typename Iterator>
String(Iterator begin, Iterator end) : m_content(begin, end) {} String(Iterator begin, Iterator end) : m_content(begin, end) {}
Character operator[](size_t pos) const { return static_cast<Character>(m_content[pos]); } Character operator[](CharCount pos) const { return static_cast<Character>(m_content[(int)pos]); }
size_t length() const { return m_content.length(); } CharCount length() const { return m_content.length(); }
bool empty() const { return m_content.empty(); } bool empty() const { return m_content.empty(); }
bool operator== (const String& other) const { return m_content == other.m_content; } bool operator== (const String& other) const { return m_content == other.m_content; }
@ -43,7 +44,7 @@ public:
memoryview<char> data() const { return memoryview<char>(m_content.data(), m_content.size()); } memoryview<char> data() const { return memoryview<char>(m_content.data(), m_content.size()); }
const char* c_str() const { return m_content.c_str(); } 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 class iterator
{ {

View File

@ -67,6 +67,9 @@ public:
bool operator>=(const RealType& other) const bool operator>=(const RealType& other) const
{ return m_value >= other.m_value; } { return m_value >= other.m_value; }
bool operator!() const
{ return !m_value; }
explicit operator ValueType() const { return m_value; } explicit operator ValueType() const { return m_value; }
private: private:
ValueType m_value; ValueType m_value;
@ -82,6 +85,16 @@ inline LineCount operator"" _line(unsigned long long int value)
return LineCount(value); return LineCount(value);
} }
struct CharCount : public StronglyTypedInteger<CharCount, int>
{
CharCount(int value) : StronglyTypedInteger<CharCount>(value) {}
};
inline CharCount operator"" _char(unsigned long long int value)
{
return CharCount(value);
}
} }
#endif // units_hh_INCLUDED #endif // units_hh_INCLUDED

View File

@ -56,7 +56,7 @@ void Window::update_display_buffer()
BufferIterator line_end = buffer().iterator_at_line_end(pos); BufferIterator line_end = buffer().iterator_at_line_end(pos);
BufferIterator end; BufferIterator end;
if (line_end - pos > m_dimensions.column) if (CharCount(line_end - pos) > m_dimensions.column)
end = pos + m_dimensions.column; end = pos + m_dimensions.column;
else else
end = line_end; 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 // (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 // the cursor in the same position, however I do not find any sane example
// of highlighters not doing that) // of highlighters not doing that)
int column = 0; CharCount column = 0;
for (auto& atom : lines.back()) for (auto& atom : lines.back())
{ {
if (atom.content.has_buffer_range() and if (atom.content.has_buffer_range() and
@ -136,7 +136,7 @@ String Window::status_line() const
oss << buffer().name(); oss << buffer().name();
if (buffer().is_modified()) if (buffer().is_modified())
oss << " [+]"; oss << " [+]";
oss << " -- " << (int)cursor.line+1 << "," << cursor.column+1 oss << " -- " << (int)cursor.line+1 << "," << (int)cursor.column+1
<< " -- " << selections().size() << " sel -- "; << " -- " << selections().size() << " sel -- ";
if (is_editing()) if (is_editing())
oss << "[Insert]"; oss << "[Insert]";