replace std::string references with String

This commit is contained in:
Maxime Coste 2012-04-14 01:17:09 +00:00
parent af5c528f04
commit 9337938403
50 changed files with 341 additions and 303 deletions

View File

@ -3,12 +3,12 @@
namespace Kakoune
{
assert_failed::assert_failed(const std::string& message)
assert_failed::assert_failed(const String& message)
{
m_message = message;
}
std::string assert_failed::description() const
String assert_failed::description() const
{
return m_message;
}

View File

@ -8,11 +8,11 @@ namespace Kakoune
struct assert_failed : logic_error
{
assert_failed(const std::string& message);
std::string description() const;
assert_failed(const String& message);
String description() const;
private:
std::string m_message;
String m_message;
};
}

View File

@ -22,7 +22,7 @@ T clamp(T min, T max, T val)
return val;
}
Buffer::Buffer(const std::string& name, Type type,
Buffer::Buffer(const String& name, Type type,
const String& initial_content)
: m_name(name), m_type(type),
m_history(1), m_history_cursor(m_history.begin()),
@ -301,10 +301,10 @@ void Buffer::apply_modification(const Modification& modification)
}
case Modification::Erase:
{
size_t size = modification.content.size();
assert(string(modification.position, modification.position + size)
size_t count = modification.content.length();
assert(string(modification.position, modification.position + count)
== modification.content);
erase(modification.position, size);
erase(modification.position, count);
break;
}
default:

View File

@ -1,13 +1,13 @@
#ifndef buffer_hh_INCLUDED
#define buffer_hh_INCLUDED
#include <string>
#include <vector>
#include <list>
#include <memory>
#include "line_and_column.hh"
#include "option_manager.hh"
#include "string.hh"
namespace Kakoune
{
@ -18,8 +18,6 @@ class Window;
typedef int BufferPos;
typedef int BufferSize;
typedef char BufferChar;
typedef std::basic_string<BufferChar> String;
struct BufferCoord : LineAndColumn<BufferCoord>
{
@ -35,7 +33,7 @@ struct BufferCoord : LineAndColumn<BufferCoord>
class BufferIterator
{
public:
typedef BufferChar value_type;
typedef Character value_type;
typedef BufferSize difference_type;
typedef const value_type* pointer;
typedef const value_type& reference;
@ -52,7 +50,7 @@ public:
bool operator> (const BufferIterator& iterator) const;
bool operator>= (const BufferIterator& iterator) const;
BufferChar operator* () const;
Character operator* () const;
BufferSize operator- (const BufferIterator& iterator) const;
BufferIterator operator+ (BufferSize size) const;
@ -117,7 +115,7 @@ public:
Scratch
};
Buffer(const std::string& name, Type type,
Buffer(const String& name, Type type,
const String& initial_content = "\n");
Buffer(const Buffer&) = delete;
Buffer(Buffer&&) = delete;
@ -146,7 +144,7 @@ public:
// returns nearest valid coordinates from given ones
BufferCoord clamp(const BufferCoord& line_and_column) const;
const std::string& name() const { return m_name; }
const String& name() const { return m_name; }
Window* get_or_create_window();
void delete_window(Window* window);
@ -191,7 +189,7 @@ private:
BufferPos line_at(const BufferIterator& iterator) const;
BufferSize line_length(BufferPos line) const;
std::string m_name;
String m_name;
const Type m_type;
typedef std::vector<Modification> UndoGroup;

View File

@ -110,7 +110,7 @@ inline void BufferIterator::on_erase(const BufferCoord& begin,
}
inline BufferChar BufferIterator::operator*() const
inline Character BufferIterator::operator*() const
{
assert(m_buffer);
return m_buffer->m_lines[line()].content[column()];

View File

@ -12,7 +12,7 @@ struct name_not_unique : logic_error {};
void BufferManager::register_buffer(Buffer* buffer)
{
assert(buffer);
const std::string& name = buffer->name();
const String& name = buffer->name();
if (m_buffers.find(name) != m_buffers.end())
throw name_not_unique();
@ -30,7 +30,7 @@ void BufferManager::unregister_buffer(Buffer* buffer)
}
}
Buffer* BufferManager::get_buffer(const std::string& name)
Buffer* BufferManager::get_buffer(const String& name)
{
if (m_buffers.find(name) == m_buffers.end())
return nullptr;
@ -38,10 +38,10 @@ Buffer* BufferManager::get_buffer(const std::string& name)
return m_buffers[name];
}
CandidateList BufferManager::complete_buffername(const std::string& prefix,
CandidateList BufferManager::complete_buffername(const String& prefix,
size_t cursor_pos)
{
std::string real_prefix = prefix.substr(0, cursor_pos);
String real_prefix = prefix.substr(0, cursor_pos);
CandidateList result;
for (auto& buffer : m_buffers)
{

View File

@ -14,7 +14,7 @@ class Buffer;
class BufferManager : public Singleton<BufferManager>
{
public:
typedef std::unordered_map<std::string, Buffer*> BufferMap;
typedef std::unordered_map<String, Buffer*> BufferMap;
struct iterator : public BufferMap::const_iterator
{
@ -32,10 +32,10 @@ public:
iterator begin() const { return iterator(m_buffers.begin()); }
iterator end() const { return iterator(m_buffers.end()); }
Buffer* get_buffer(const std::string& name);
Buffer* get_buffer(const String& name);
CandidateList complete_buffername(const std::string& prefix,
size_t cursor_pos = std::string::npos);
CandidateList complete_buffername(const String& prefix,
size_t cursor_pos = String::npos);
private:
BufferMap m_buffers;

View File

@ -12,14 +12,14 @@
namespace Kakoune
{
void CommandManager::register_command(const std::string& command_name, Command command,
void CommandManager::register_command(const String& command_name, Command command,
unsigned flags,
const CommandCompleter& completer)
{
m_commands[command_name] = CommandDescriptor { command, flags, completer };
}
void CommandManager::register_commands(const memoryview<std::string>& command_names, Command command,
void CommandManager::register_commands(const memoryview<String>& command_names, Command command,
unsigned flags,
const CommandCompleter& completer)
{
@ -33,7 +33,7 @@ static bool is_blank(char c)
}
typedef std::vector<std::pair<size_t, size_t>> TokenList;
static TokenList split(const std::string& line)
static TokenList split(const String& line)
{
TokenList result;
@ -76,18 +76,18 @@ static TokenList split(const std::string& line)
struct command_not_found : runtime_error
{
command_not_found(const std::string& command)
command_not_found(const String& command)
: runtime_error(command + " : no such command") {}
};
void CommandManager::execute(const std::string& command_line,
void CommandManager::execute(const String& command_line,
const Context& context)
{
TokenList tokens = split(command_line);
if (tokens.empty())
return;
std::vector<std::string> params;
std::vector<String> params;
for (auto it = tokens.begin(); it != tokens.end(); ++it)
{
params.push_back(command_line.substr(it->first,
@ -97,8 +97,8 @@ void CommandManager::execute(const std::string& command_line,
execute(params, context);
}
static void shell_eval(std::vector<std::string>& params,
const std::string& cmdline,
static void shell_eval(std::vector<String>& params,
const String& cmdline,
const Context& context)
{
int write_pipe[2];
@ -113,13 +113,13 @@ static void shell_eval(std::vector<std::string>& params,
close(read_pipe[1]);
close(write_pipe[1]);
std::string output;
String output;
char buffer[1024];
while (size_t size = read(read_pipe[0], buffer, 1024))
{
if (size == -1)
break;
output += std::string(buffer, buffer+size);
output += String(buffer, buffer+size);
}
close(read_pipe[0]);
waitpid(pid, NULL, 0);
@ -161,7 +161,7 @@ void CommandManager::execute(const CommandParameters& params,
if (end != begin)
{
std::vector<std::string> expanded_params;
std::vector<String> expanded_params;
auto command_it = m_commands.find(*begin);
if (command_it == m_commands.end() and
@ -209,7 +209,7 @@ void CommandManager::execute(const CommandParameters& params,
}
}
Completions CommandManager::complete(const std::string& command_line, size_t cursor_pos)
Completions CommandManager::complete(const String& command_line, size_t cursor_pos)
{
TokenList tokens = split(command_line);
@ -227,7 +227,7 @@ Completions CommandManager::complete(const std::string& command_line, size_t cur
{
size_t cmd_start = tokens.empty() ? 0 : tokens[0].first;
Completions result(cmd_start, cursor_pos);
std::string prefix = command_line.substr(cmd_start,
String prefix = command_line.substr(cmd_start,
cursor_pos - cmd_start);
for (auto& command : m_commands)
@ -240,7 +240,7 @@ Completions CommandManager::complete(const std::string& command_line, size_t cur
}
assert(not tokens.empty());
std::string command_name =
String command_name =
command_line.substr(tokens[0].first,
tokens[0].second - tokens[0].first);
@ -248,7 +248,7 @@ Completions CommandManager::complete(const std::string& command_line, size_t cur
if (command_it == m_commands.end() or not command_it->second.completer)
return Completions();
std::vector<std::string> params;
std::vector<String> params;
for (auto it = tokens.begin() + 1; it != tokens.end(); ++it)
{
params.push_back(command_line.substr(it->first,
@ -276,8 +276,8 @@ CandidateList PerArgumentCommandCompleter::operator()(const CommandParameters& p
// it is possible to try to complete a new argument
assert(token_to_complete <= params.size());
const std::string& argument = token_to_complete < params.size() ?
params[token_to_complete] : std::string();
const String& argument = token_to_complete < params.size() ?
params[token_to_complete] : String();
return m_completers[token_to_complete](argument, pos_in_token);
}

View File

@ -1,11 +1,11 @@
#ifndef command_manager_hh_INCLUDED
#define command_manager_hh_INCLUDED
#include <string>
#include <unordered_map>
#include <functional>
#include <initializer_list>
#include "string.hh"
#include "utils.hh"
#include "completion.hh"
#include "memoryview.hh"
@ -20,7 +20,7 @@ struct wrong_argument_count : runtime_error
wrong_argument_count() : runtime_error("wrong argument count") {}
};
typedef memoryview<std::string> CommandParameters;
typedef memoryview<String> CommandParameters;
typedef std::function<void (const CommandParameters&,
const Context& context)> Command;
@ -30,7 +30,7 @@ typedef std::function<CandidateList (const CommandParameters&,
class PerArgumentCommandCompleter
{
public:
typedef std::function<CandidateList (const std::string&, size_t)> ArgumentCompleter;
typedef std::function<CandidateList (const String&, size_t)> ArgumentCompleter;
typedef memoryview<ArgumentCompleter> ArgumentCompleterList;
PerArgumentCommandCompleter(const ArgumentCompleterList& completers)
@ -54,17 +54,17 @@ public:
DeferredShellEval = 2,
};
void execute(const std::string& command_line, const Context& context);
void execute(const String& command_line, const Context& context);
void execute(const CommandParameters& params, const Context& context);
Completions complete(const std::string& command_line, size_t cursor_pos);
Completions complete(const String& command_line, size_t cursor_pos);
void register_command(const std::string& command_name,
void register_command(const String& command_name,
Command command,
unsigned flags = None,
const CommandCompleter& completer = CommandCompleter());
void register_commands(const memoryview<std::string>& command_names,
void register_commands(const memoryview<String>& command_names,
Command command,
unsigned flags = None,
const CommandCompleter& completer = CommandCompleter());
@ -76,7 +76,7 @@ private:
unsigned flags;
CommandCompleter completer;
};
std::unordered_map<std::string, CommandDescriptor> m_commands;
std::unordered_map<String, CommandDescriptor> m_commands;
};
}

View File

@ -9,20 +9,20 @@
namespace Kakoune
{
CandidateList complete_filename(const std::string& prefix,
CandidateList complete_filename(const String& prefix,
size_t cursor_pos)
{
std::string real_prefix = prefix.substr(0, cursor_pos);
size_t dir_end = real_prefix.find_last_of('/');
std::string dirname = "./";
std::string dirprefix;
std::string fileprefix = real_prefix;
String real_prefix = prefix.substr(0, cursor_pos);
auto dir_end = std::find(real_prefix.begin(), real_prefix.end(), '/');
String dirname = "./";
String dirprefix;
String fileprefix = real_prefix;
if (dir_end != std::string::npos)
if (dir_end != real_prefix.end())
{
dirname = real_prefix.substr(0, dir_end + 1);
dirname = String(real_prefix.begin(), dir_end + 1);
dirprefix = dirname;
fileprefix = real_prefix.substr(dir_end + 1, std::string::npos);
fileprefix = String(dir_end + 1, real_prefix.end());
}
auto dir = auto_raii(opendir(dirname.c_str()), closedir);
@ -33,13 +33,13 @@ CandidateList complete_filename(const std::string& prefix,
while (dirent* entry = readdir(dir))
{
std::string filename = entry->d_name;
String filename = entry->d_name;
if (filename.empty())
continue;
if (filename.substr(0, fileprefix.length()) == fileprefix)
{
std::string name = dirprefix + filename;
String name = dirprefix + filename;
if (entry->d_type == DT_DIR)
name += '/';
if (fileprefix.length() or filename[0] != '.')

View File

@ -1,14 +1,15 @@
#ifndef completion_hh_INCLUDED
#define completion_hh_INCLUDED
#include <string>
#include <vector>
#include <functional>
#include "string.hh"
namespace Kakoune
{
typedef std::vector<std::string> CandidateList;
typedef std::vector<String> CandidateList;
struct Completions
{
@ -23,12 +24,12 @@ struct Completions
: start(start), end(end) {}
};
CandidateList complete_filename(const std::string& prefix,
size_t cursor_pos = std::string::npos);
CandidateList complete_filename(const String& prefix,
size_t cursor_pos = -1);
typedef std::function<Completions (const std::string&, size_t)> Completer;
typedef std::function<Completions (const String&, size_t)> Completer;
inline Completions complete_nothing(const std::string&, size_t cursor_pos)
inline Completions complete_nothing(const String&, size_t cursor_pos)
{
return Completions(cursor_pos, cursor_pos);
}

View File

@ -9,7 +9,7 @@ namespace Kakoune
static Buffer& get_or_create_debug_buffer()
{
static const std::string debug_buffer_name("*debug*");
static const String debug_buffer_name("*debug*");
Buffer* buffer = BufferManager::instance().get_buffer(debug_buffer_name);
if (not buffer)
@ -19,7 +19,7 @@ static Buffer& get_or_create_debug_buffer()
return *buffer;
}
void write_debug(const std::string& str)
void write_debug(const String& str)
{
Buffer& debug_buffer = get_or_create_debug_buffer();
Editor editor(debug_buffer);

View File

@ -1,12 +1,12 @@
#ifndef debug_hh_INCLUDED
#define debug_hh_INCLUDED
#include <string>
#include "string.hh"
namespace Kakoune
{
void write_debug(const std::string& str);
void write_debug(const String& str);
}

View File

@ -1,11 +1,10 @@
#ifndef display_buffer_hh_INCLUDED
#define display_buffer_hh_INCLUDED
#include <string>
#include <vector>
#include "string.hh"
#include "line_and_column.hh"
#include "buffer.hh"
namespace Kakoune

View File

@ -72,7 +72,7 @@ void Editor::append(const memoryview<String>& strings)
do_insert<true>(*this, strings);
}
void Editor::replace(const std::string& string)
void Editor::replace(const String& string)
{
scoped_edition edition(*this);
erase();
@ -228,7 +228,7 @@ void Editor::check_invariant() const
struct id_not_unique : public runtime_error
{
id_not_unique(const std::string& id)
id_not_unique(const String& id)
: runtime_error("id not unique: " + id) {}
};
@ -239,12 +239,12 @@ void Editor::add_filter(FilterAndId&& filter)
m_filters.append(std::forward<FilterAndId>(filter));
}
void Editor::remove_filter(const std::string& id)
void Editor::remove_filter(const String& id)
{
m_filters.remove(id);
}
CandidateList Editor::complete_filterid(const std::string& prefix,
CandidateList Editor::complete_filterid(const String& prefix,
size_t cursor_pos)
{
return m_filters.complete_id<str_to_str>(prefix, cursor_pos);

View File

@ -54,10 +54,10 @@ public:
bool redo();
void add_filter(FilterAndId&& filter);
void remove_filter(const std::string& id);
void remove_filter(const String& id);
CandidateList complete_filterid(const std::string& prefix,
size_t cursor_pos = std::string::npos);
CandidateList complete_filterid(const String& prefix,
size_t cursor_pos = String::npos);
bool is_editing() const { return m_edition_level!= 0; }
@ -76,7 +76,7 @@ private:
Buffer& m_buffer;
std::vector<SelectionList> m_selections;
idvaluemap<std::string, FilterFunc> m_filters;
idvaluemap<String, FilterFunc> m_filters;
};
struct scoped_edition

View File

@ -1,12 +1,12 @@
#include "exception.hh"
#include <string>
#include "string.hh"
#include <typeinfo>
namespace Kakoune
{
std::string exception::description() const
String exception::description() const
{
return typeid(*this).name();
}

View File

@ -1,7 +1,7 @@
#ifndef exception_hh_INCLUDED
#define exception_hh_INCLUDED
#include <string>
#include "string.hh"
namespace Kakoune
{
@ -9,18 +9,18 @@ namespace Kakoune
struct exception
{
virtual ~exception() {}
virtual std::string description() const;
virtual String description() const;
};
struct runtime_error : exception
{
runtime_error(const std::string& description)
runtime_error(const String& description)
: m_description(description) {}
std::string description() const { return m_description; }
String description() const { return m_description; }
private:
std::string m_description;
String m_description;
};
struct logic_error : exception

View File

@ -18,13 +18,13 @@ bool isidentifier(char c)
return std::isalnum(c) or c == '_';
}
std::string parse_filename(const std::string& filename)
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;
std::string result;
String result;
for (size_t i = 0; i < filename.length(); ++i)
{
if (filename[i] == '$' and (i == 0 or filename[i-1] != '\\'))
@ -33,7 +33,7 @@ std::string parse_filename(const std::string& filename)
size_t end = i+1;
while (end != filename.length() and isidentifier(filename[end]))
++end;
std::string var_name = filename.substr(i+1, end - i - 1);
String var_name = filename.substr(i+1, end - i - 1);
const char* var_value = getenv(var_name.c_str());
if (var_value)
result += var_value;
@ -47,7 +47,7 @@ std::string parse_filename(const std::string& filename)
return result;
}
std::string read_file(const std::string& filename)
String read_file(const String& filename)
{
int fd = open(filename.c_str(), O_RDONLY);
if (fd == -1)
@ -58,7 +58,7 @@ std::string read_file(const std::string& filename)
throw file_access_error(filename, strerror(errno));
}
std::string content;
String content;
char buf[256];
while (true)
{
@ -66,15 +66,15 @@ std::string read_file(const std::string& filename)
if (size == -1 or size == 0)
break;
content += std::string(buf, size);
content += String(buf, buf + size);
}
close(fd);
return content;
}
Buffer* create_buffer_from_file(const std::string& filename)
Buffer* create_buffer_from_file(const String& filename)
{
std::string content = read_file(filename);
String content = read_file(filename);
if (Buffer* buffer = BufferManager::instance().get_buffer(filename))
delete buffer;
@ -82,7 +82,7 @@ Buffer* create_buffer_from_file(const std::string& filename)
return new Buffer(filename, Buffer::Type::File, content);
}
void write_buffer_to_file(const Buffer& buffer, const std::string& filename)
void write_buffer_to_file(const Buffer& buffer, const String& filename)
{
int fd = open(filename.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0644);
if (fd == -1)
@ -91,8 +91,8 @@ void write_buffer_to_file(const Buffer& buffer, const std::string& filename)
for (size_t i = 0; i < buffer.line_count(); ++i)
{
const String& content = buffer.line_content(i);
ssize_t count = content.length() * sizeof(BufferChar);
const char* ptr = content.c_str();
ssize_t count = content.size();
while (count)
{

View File

@ -1,7 +1,7 @@
#ifndef file_hh_INCLUDED
#define file_hh_INCLUDED
#include <string>
#include "string.hh"
#include "exception.hh"
@ -11,25 +11,25 @@ namespace Kakoune
struct file_access_error : runtime_error
{
public:
file_access_error(const std::string& filename,
const std::string& error_desc)
file_access_error(const String& filename,
const String& error_desc)
: runtime_error(filename + ": " + error_desc) {}
};
struct file_not_found : file_access_error
{
file_not_found(const std::string& filename)
file_not_found(const String& filename)
: file_access_error(filename, "file not found") {}
};
class Buffer;
// parse ~/ and $env values in filename and returns the translated filename
std::string parse_filename(const std::string& filename);
String parse_filename(const String& filename);
std::string read_file(const std::string& filename);
Buffer* create_buffer_from_file(const std::string& filename);
void write_buffer_to_file(const Buffer& buffer, const std::string& filename);
String read_file(const String& filename);
Buffer* create_buffer_from_file(const String& filename);
void write_buffer_to_file(const Buffer& buffer, const String& filename);
}

View File

@ -1,7 +1,7 @@
#ifndef filter_hh_INCLUDED
#define filter_hh_INCLUDED
#include <string>
#include "string.hh"
#include <functional>
namespace Kakoune
@ -15,7 +15,7 @@ class Modification;
// prior to it's application.
typedef std::function<void (Buffer& buffer, Modification& modification)> FilterFunc;
typedef std::pair<std::string, FilterFunc> FilterAndId;
typedef std::pair<String, FilterFunc> FilterAndId;
}

View File

@ -8,11 +8,11 @@ namespace Kakoune
struct factory_not_found : public runtime_error
{
factory_not_found(const std::string& name)
factory_not_found(const String& name)
: runtime_error("filter factory not found '" + name + "'") {}
};
void FilterRegistry::register_factory(const std::string& name,
void FilterRegistry::register_factory(const String& name,
const FilterFactory& factory)
{
assert(not m_factories.contains(name));
@ -20,7 +20,7 @@ void FilterRegistry::register_factory(const std::string& name,
}
void FilterRegistry::add_filter_to_window(Window& window,
const std::string& name,
const String& name,
const FilterParameters& parameters)
{
auto it = m_factories.find(name);
@ -30,7 +30,7 @@ void FilterRegistry::add_filter_to_window(Window& window,
window.add_filter(it->second(window, parameters));
}
CandidateList FilterRegistry::complete_filter(const std::string& prefix,
CandidateList FilterRegistry::complete_filter(const String& prefix,
size_t cursor_pos)
{
return m_factories.complete_id<str_to_str>(prefix, cursor_pos);

View File

@ -1,9 +1,9 @@
#ifndef filter_registry_h_INCLUDED
#define filter_registry_h_INCLUDED
#include <string>
#include <unordered_map>
#include "string.hh"
#include "filter.hh"
#include "utils.hh"
#include "completion.hh"
@ -15,7 +15,7 @@ namespace Kakoune
class Window;
typedef memoryview<std::string> FilterParameters;
typedef memoryview<String> FilterParameters;
typedef std::function<FilterAndId (Window& window,
const FilterParameters& params)> FilterFactory;
@ -23,18 +23,18 @@ typedef std::function<FilterAndId (Window& window,
class FilterRegistry : public Singleton<FilterRegistry>
{
public:
void register_factory(const std::string& name,
void register_factory(const String& name,
const FilterFactory& factory);
void add_filter_to_window(Window& window,
const std::string& factory_name,
const String& factory_name,
const FilterParameters& parameters);
CandidateList complete_filter(const std::string& prefix,
CandidateList complete_filter(const String& prefix,
size_t cursor_pos);
private:
idvaluemap<std::string, FilterFactory> m_factories;
idvaluemap<String, FilterFactory> m_factories;
};
}

View File

@ -56,7 +56,9 @@ void expand_tabulations(Buffer& buffer, Modification& modification)
}
int count = tabstop - (column % tabstop);
modification.content = std::string(count, ' ');
modification.content.clear();
for (int i = 0; i < count; ++i)
modification.content += ' ';
}
}
@ -64,7 +66,7 @@ template<void (*filter_func)(Buffer&, Modification&)>
class SimpleFilterFactory
{
public:
SimpleFilterFactory(const std::string& id) : m_id(id) {}
SimpleFilterFactory(const String& id) : m_id(id) {}
FilterAndId operator()(Window& window,
const FilterParameters& params) const
@ -72,7 +74,7 @@ public:
return FilterAndId(m_id, FilterFunc(filter_func));
}
private:
std::string m_id;
String m_id;
};
void register_filters()

View File

@ -1,7 +1,7 @@
#ifndef highlighter_hh_INCLUDED
#define highlighter_hh_INCLUDED
#include <string>
#include "string.hh"
#include <functional>
#include "memoryview.hh"
@ -16,8 +16,8 @@ class DisplayBuffer;
// buffer content (folding for example)
typedef std::function<void (DisplayBuffer& display_buffer)> HighlighterFunc;
typedef std::pair<std::string, HighlighterFunc> HighlighterAndId;
typedef memoryview<std::string> HighlighterParameters;
typedef std::pair<String, HighlighterFunc> HighlighterAndId;
typedef memoryview<String> HighlighterParameters;
}

View File

@ -20,12 +20,12 @@ void HighlighterGroup::append(HighlighterAndId&& highlighter)
m_highlighters.append(std::forward<HighlighterAndId>(highlighter));
}
void HighlighterGroup::remove(const std::string& id)
void HighlighterGroup::remove(const String& id)
{
m_highlighters.remove(id);
}
HighlighterGroup& HighlighterGroup::get_group(const std::string& id)
HighlighterGroup& HighlighterGroup::get_group(const String& id)
{
auto it = m_highlighters.find(id);
if (it == m_highlighters.end())
@ -38,18 +38,18 @@ HighlighterGroup& HighlighterGroup::get_group(const std::string& id)
}
CandidateList HighlighterGroup::complete_id(const std::string& prefix,
CandidateList HighlighterGroup::complete_id(const String& prefix,
size_t cursor_pos)
{
return m_highlighters.complete_id<str_to_str>(prefix, cursor_pos);
}
CandidateList HighlighterGroup::complete_group_id(const std::string& prefix,
CandidateList HighlighterGroup::complete_group_id(const String& prefix,
size_t cursor_pos)
{
return m_highlighters.complete_id_if<str_to_str>(
prefix, cursor_pos,
[](std::pair<std::string, HighlighterFunc>& func)
[](std::pair<String, HighlighterFunc>& func)
{ return func.second.target<HighlighterGroup>() != nullptr; });
}

View File

@ -16,15 +16,15 @@ public:
void operator()(DisplayBuffer& display_buffer);
void append(HighlighterAndId&& highlighter);
void remove(const std::string& id);
void remove(const String& id);
HighlighterGroup& get_group(const std::string& id);
HighlighterGroup& get_group(const String& id);
CandidateList complete_id(const std::string& prefix, size_t cursor_pos);
CandidateList complete_group_id(const std::string& prefix, size_t cursor_pos);
CandidateList complete_id(const String& prefix, size_t cursor_pos);
CandidateList complete_group_id(const String& prefix, size_t cursor_pos);
private:
idvaluemap<std::string, HighlighterFunc> m_highlighters;
idvaluemap<String, HighlighterFunc> m_highlighters;
};
}

View File

@ -9,11 +9,11 @@ namespace Kakoune
struct factory_not_found : public runtime_error
{
factory_not_found(const std::string& name)
factory_not_found(const String& name)
: runtime_error("highlighter factory not found '" + name + "'") {}
};
void HighlighterRegistry::register_factory(const std::string& name,
void HighlighterRegistry::register_factory(const String& name,
const HighlighterFactory& factory)
{
assert(not m_factories.contains(name));
@ -21,7 +21,7 @@ void HighlighterRegistry::register_factory(const std::string& name,
}
void HighlighterRegistry::add_highlighter_to_window(Window& window,
const std::string& name,
const String& name,
const HighlighterParameters& parameters)
{
auto it = m_factories.find(name);
@ -33,7 +33,7 @@ void HighlighterRegistry::add_highlighter_to_window(Window& window,
void HighlighterRegistry::add_highlighter_to_group(Window& window,
HighlighterGroup& group,
const std::string& name,
const String& name,
const HighlighterParameters& parameters)
{
auto it = m_factories.find(name);
@ -43,7 +43,7 @@ void HighlighterRegistry::add_highlighter_to_group(Window& window,
group.append(it->second(window, parameters));
}
CandidateList HighlighterRegistry::complete_highlighter(const std::string& prefix,
CandidateList HighlighterRegistry::complete_highlighter(const String& prefix,
size_t cursor_pos)
{
return m_factories.complete_id<str_to_str>(prefix, cursor_pos);

View File

@ -1,7 +1,7 @@
#ifndef highlighter_registry_h_INCLUDED
#define highlighter_registry_h_INCLUDED
#include <string>
#include "string.hh"
#include <unordered_map>
#include "highlighter.hh"
@ -21,23 +21,23 @@ typedef std::function<HighlighterAndId (Window& window,
class HighlighterRegistry : public Singleton<HighlighterRegistry>
{
public:
void register_factory(const std::string& name,
void register_factory(const String& name,
const HighlighterFactory& factory);
void add_highlighter_to_window(Window& window,
const std::string& factory_name,
const String& factory_name,
const HighlighterParameters& parameters);
void add_highlighter_to_group(Window& window,
HighlighterGroup& group,
const std::string& factory_name,
const String& factory_name,
const HighlighterParameters& parameters);
CandidateList complete_highlighter(const std::string& prefix,
CandidateList complete_highlighter(const String& prefix,
size_t cursor_pos);
private:
idvaluemap<std::string, HighlighterFactory> m_factories;
idvaluemap<String, HighlighterFactory> m_factories;
};
}

View File

@ -5,7 +5,7 @@
#include "display_buffer.hh"
#include "highlighter_registry.hh"
#include "highlighter_group.hh"
#include <boost/regex.hpp>
#include "regex.hh"
namespace Kakoune
{
@ -15,7 +15,7 @@ using namespace std::placeholders;
void colorize_regex_range(DisplayBuffer& display_buffer,
const BufferIterator& range_begin,
const BufferIterator& range_end,
const boost::regex& ex,
const Regex& ex,
Color fg_color, Color bg_color = Color::Default)
{
assert(range_begin <= range_end);
@ -29,9 +29,8 @@ void colorize_regex_range(DisplayBuffer& display_buffer,
BufferIterator display_end = std::min(range_end,
display_buffer.back().end());
boost::regex_iterator<BufferIterator> re_it(display_begin, display_end,
ex, boost::match_nosubs);
boost::regex_iterator<BufferIterator> re_end;
RegexIterator re_it(display_begin, display_end, ex, boost::match_nosubs);
RegexIterator re_end;
DisplayBuffer::iterator atom_it = display_buffer.begin();
for (; re_it != re_end; ++re_it)
{
@ -65,14 +64,14 @@ void colorize_regex_range(DisplayBuffer& display_buffer,
}
void colorize_regex(DisplayBuffer& display_buffer,
const boost::regex& ex,
const Regex& ex,
Color fg_color, Color bg_color = Color::Default)
{
colorize_regex_range(display_buffer, display_buffer.front().begin(),
display_buffer.back().end(), ex, fg_color, bg_color);
}
Color parse_color(const std::string& color)
Color parse_color(const String& color)
{
if (color == "default") return Color::Default;
if (color == "black") return Color::Black;
@ -92,12 +91,12 @@ HighlighterAndId colorize_regex_factory(Window& window,
if (params.size() != 3)
throw runtime_error("wrong parameter count");
boost::regex ex(params[0]);
Regex ex(params[0].begin(), params[0].end());
Color fg_color = parse_color(params[1]);
Color bg_color = parse_color(params[2]);
std::string id = "colre'" + params[0] + "'";
String id = "colre'" + params[0] + "'";
return HighlighterAndId(id, std::bind(colorize_regex, _1,
ex, fg_color, bg_color));
@ -133,8 +132,10 @@ void expand_tabulations(Window& window, DisplayBuffer& display_buffer)
}
int count = tabstop - (column % tabstop);
display_buffer.replace_atom_content(atom_it,
std::string(count, ' '));
String padding;
for (int i = 0; i < count; ++i)
padding += ' ';
display_buffer.replace_atom_content(atom_it, padding);
}
}
}
@ -271,7 +272,7 @@ template<void (*highlighter_func)(DisplayBuffer&)>
class SimpleHighlighterFactory
{
public:
SimpleHighlighterFactory(const std::string& id) : m_id(id) {}
SimpleHighlighterFactory(const String& id) : m_id(id) {}
HighlighterAndId operator()(Window& window,
const HighlighterParameters& params) const
@ -279,14 +280,14 @@ public:
return HighlighterAndId(m_id, HighlighterFunc(highlighter_func));
}
private:
std::string m_id;
String m_id;
};
template<void (*highlighter_func)(Window&, DisplayBuffer&)>
class WindowHighlighterFactory
{
public:
WindowHighlighterFactory(const std::string& id) : m_id(id) {}
WindowHighlighterFactory(const String& id) : m_id(id) {}
HighlighterAndId operator()(Window& window,
const HighlighterParameters& params) const
@ -294,7 +295,7 @@ public:
return HighlighterAndId(m_id, std::bind(highlighter_func, std::ref(window), _1));
}
private:
std::string m_id;
String m_id;
};
HighlighterAndId highlighter_group_factory(Window& window,

View File

@ -3,13 +3,13 @@
namespace Kakoune
{
void HookManager::add_hook(const std::string& hook_name, HookFunc hook)
void HookManager::add_hook(const String& hook_name, HookFunc hook)
{
m_hook[hook_name].push_back(hook);
}
void HookManager::run_hook(const std::string& hook_name,
const std::string& param,
void HookManager::run_hook(const String& hook_name,
const String& param,
const Context& context) const
{
auto hook_list_it = m_hook.find(hook_name);

View File

@ -9,17 +9,17 @@ namespace Kakoune
{
class Context;
typedef std::function<void (const std::string&, const Context&)> HookFunc;
typedef std::function<void (const String&, const Context&)> HookFunc;
class HookManager
{
public:
void add_hook(const std::string& hook_name, HookFunc hook);
void run_hook(const std::string& hook_name, const std::string& param,
void add_hook(const String& hook_name, HookFunc hook);
void run_hook(const String& hook_name, const String& param,
const Context& context) const;
private:
std::unordered_map<std::string, std::vector<HookFunc>> m_hook;
std::unordered_map<String, std::vector<HookFunc>> m_hook;
};
class GlobalHookManager : public HookManager,

View File

@ -63,28 +63,28 @@ public:
}
}
template<std::string (*id_to_string)(const _Id&),
template<String (*id_to_string)(const _Id&),
typename _Condition>
CandidateList complete_id_if(const std::string& prefix,
CandidateList complete_id_if(const String& prefix,
size_t cursor_pos,
_Condition condition)
{
std::string real_prefix = prefix.substr(0, cursor_pos);
String real_prefix = prefix.substr(0, cursor_pos);
CandidateList result;
for (auto& value : m_content)
{
if (not condition(value))
continue;
std::string id_str = id_to_string(value.first);
String id_str = id_to_string(value.first);
if (id_str.substr(0, real_prefix.length()) == real_prefix)
result.push_back(std::move(id_str));
}
return result;
}
template<std::string (*id_to_string)(const _Id&)>
CandidateList complete_id(const std::string& prefix,
template<String (*id_to_string)(const _Id&)>
CandidateList complete_id(const String& prefix,
size_t cursor_pos)
{
return complete_id_if<id_to_string>(

View File

@ -5,13 +5,13 @@
namespace Kakoune
{
static std::unordered_map<std::string, char> keynamemap = {
static std::unordered_map<String, Character> keynamemap = {
{ "ret", '\n' },
{ "space", ' ' },
{ "esc", 27 }
};
KeyList parse_keys(const std::string& str)
KeyList parse_keys(const String& str)
{
KeyList result;
for (size_t pos = 0; pos < str.length(); ++pos)
@ -26,7 +26,7 @@ KeyList parse_keys(const std::string& str)
{
Key::Modifiers modifier = Key::Modifiers::None;
std::string keyname = str.substr(pos+1, end_pos - pos - 1);
String keyname = str.substr(pos+1, end_pos - pos - 1);
if (keyname.length() > 2)
{
if (tolower(keyname[0]) == 'c' and keyname[1] == '-')

View File

@ -2,7 +2,7 @@
#define keys_hh_INCLUDED
#include <vector>
#include <string>
#include "string.hh"
namespace Kakoune
{
@ -18,9 +18,9 @@ struct Key
};
Modifiers modifiers;
char key;
Character key;
Key(Modifiers modifiers, char key)
Key(Modifiers modifiers, Character key)
: modifiers(modifiers), key(key) {}
bool operator==(const Key& other) const
@ -29,7 +29,7 @@ struct Key
typedef std::vector<Key> KeyList;
KeyList parse_keys(const std::string& str);
KeyList parse_keys(const String& str);
}

View File

@ -15,9 +15,9 @@
#include "option_manager.hh"
#include "context.hh"
#include "ncurses.hh"
#include "regex.hh"
#include <unordered_map>
#include <boost/regex.hpp>
#include <sys/types.h>
#include <sys/wait.h>
@ -36,7 +36,7 @@ void draw_editor_ifn(Editor& editor)
}
PromptFunc prompt_func;
std::string prompt(const std::string& text, Completer completer = complete_nothing)
String prompt(const String& text, Completer completer = complete_nothing)
{
return prompt_func(text, completer);
}
@ -67,7 +67,7 @@ bool insert_char(IncrementalInserter& inserter, const Key& key)
case 27:
return false;
default:
inserter.insert(std::string() + key.key);
inserter.insert(String() + key.key);
}
break;
case Key::Modifiers::Control:
@ -91,10 +91,10 @@ bool insert_char(IncrementalInserter& inserter, const Key& key)
break;
}
case 'm':
inserter.insert(std::string() + '\n');
inserter.insert(String() + '\n');
break;
case 'i':
inserter.insert(std::string() + '\t');
inserter.insert(String() + '\t');
break;
case 'd':
inserter.move_cursors({0, -1});
@ -187,7 +187,7 @@ void do_go(Editor& editor, int count)
Context main_context;
Buffer* open_or_create(const std::string& filename)
Buffer* open_or_create(const String& filename)
{
Buffer* buffer = NULL;
try
@ -208,7 +208,7 @@ void edit(const CommandParameters& params, const Context& context)
if (params.size() == 0 or params.size() > 3)
throw wrong_argument_count();
std::string filename = params[0];
String filename = params[0];
Buffer* buffer = nullptr;
if (not force_reload)
@ -236,7 +236,7 @@ void write_buffer(const CommandParameters& params, const Context& context)
throw wrong_argument_count();
Buffer& buffer = context.window().buffer();
std::string filename = params.empty() ? buffer.name()
String filename = params.empty() ? buffer.name()
: parse_filename(params[0]);
write_buffer_to_file(buffer, filename);
@ -253,7 +253,7 @@ void quit(const CommandParameters& params, const Context& context)
if (not force)
{
std::vector<std::string> names;
std::vector<String> names;
for (auto& buffer : BufferManager::instance())
{
if (buffer.type() != Buffer::Type::Scratch and buffer.is_modified())
@ -261,7 +261,7 @@ void quit(const CommandParameters& params, const Context& context)
}
if (not names.empty())
{
std::string message = "modified buffers remaining: [";
String message = "modified buffers remaining: [";
for (auto it = names.begin(); it != names.end(); ++it)
{
if (it != names.begin())
@ -387,11 +387,12 @@ void add_hook(const CommandParameters& params, const Context& context)
if (params.size() < 4)
throw wrong_argument_count();
std::string regex = params[2];
std::vector<std::string> hook_params(params.begin()+3, params.end());
String regex = params[2];
std::vector<String> hook_params(params.begin()+3, params.end());
auto hook_func = [=](const std::string& param, const Context& context) {
if (boost::regex_match(param, boost::regex(regex)))
auto hook_func = [=](const String& param, const Context& context) {
if (boost::regex_match(param.begin(), param.end(),
Regex(regex.begin(), regex.end())))
CommandManager::instance().execute(hook_params, context);
};
@ -410,7 +411,7 @@ void define_command(const CommandParameters& params, const Context& context)
if (params[0] == "-env-params")
{
std::vector<std::string> cmd_params(params.begin() + 2, params.end());
std::vector<String> cmd_params(params.begin() + 2, params.end());
CommandManager::instance().register_command(params[1],
[=](const CommandParameters& params, const Context& context) {
char param_name[] = "kak_param0";
@ -427,10 +428,10 @@ void define_command(const CommandParameters& params, const Context& context)
}
else if (params[0] == "-append-params")
{
std::vector<std::string> cmd_params(params.begin() + 2, params.end());
std::vector<String> cmd_params(params.begin() + 2, params.end());
CommandManager::instance().register_command(params[1],
[=](const CommandParameters& params, const Context& context) {
std::vector<std::string> merged_params = cmd_params;
std::vector<String> merged_params = cmd_params;
for (auto& param : params)
merged_params.push_back(param);
CommandManager::instance().execute(merged_params, context);
@ -438,7 +439,7 @@ void define_command(const CommandParameters& params, const Context& context)
}
else
{
std::vector<std::string> cmd_params(params.begin() + 1, params.end());
std::vector<String> cmd_params(params.begin() + 1, params.end());
CommandManager::instance().register_command(params[0],
[=](const CommandParameters& params, const Context& context) {
if (not params.empty())
@ -450,7 +451,7 @@ void define_command(const CommandParameters& params, const Context& context)
void echo_message(const CommandParameters& params, const Context& context)
{
std::string message;
String message;
for (auto& param : params)
message += param + " ";
NCurses::print_status(message);
@ -462,13 +463,13 @@ void exec_commands_in_file(const CommandParameters& params,
if (params.size() != 1)
throw wrong_argument_count();
std::string file_content = read_file(parse_filename(params[0]));
String file_content = read_file(parse_filename(params[0]));
CommandManager& cmd_manager = CommandManager::instance();
size_t pos = 0;
size_t length = file_content.length();
bool cat_with_previous = false;
std::string command_line;
String command_line;
while (true)
{
if (not cat_with_previous)
@ -489,7 +490,7 @@ void exec_commands_in_file(const CommandParameters& params,
if (end_pos == length)
{
NCurses::print_status(std::string("unterminated '") + delimiter + "' string");
NCurses::print_status(String("unterminated '") + delimiter + "' string");
return;
}
@ -528,7 +529,7 @@ void exec_commands_in_runtime_file(const CommandParameters& params,
if (params.size() != 1)
throw wrong_argument_count();
const std::string& filename = params[0];
const String& filename = params[0];
char buffer[2048];
#if defined(__linux__)
ssize_t res = readlink("/proc/self/exe", buffer, 2048 - filename.length());
@ -589,15 +590,15 @@ void do_pipe(Editor& editor, int count)
close(write_pipe[0]);
close(read_pipe[1]);
std::string content = editor.buffer().string(sel.begin(), sel.end());
String content = editor.buffer().string(sel.begin(), sel.end());
write(write_pipe[1], content.c_str(), content.size());
close(write_pipe[1]);
std::string new_content;
String new_content;
char buffer[1024];
while (size_t size = read(read_pipe[0], buffer, 1024))
{
new_content += std::string(buffer, buffer+size);
new_content += String(buffer, buffer+size);
}
close(read_pipe[0]);
waitpid(pid, NULL, 0);
@ -626,7 +627,7 @@ void do_search(Editor& editor)
{
try
{
std::string ex = prompt("/");
String ex = prompt("/");
if (ex.empty())
ex = RegisterManager::instance()['/'].get();
else
@ -640,7 +641,7 @@ void do_search(Editor& editor)
template<bool append>
void do_search_next(Editor& editor)
{
const std::string& ex = RegisterManager::instance()['/'].get();
const String& ex = RegisterManager::instance()['/'].get();
if (not ex.empty())
editor.select(std::bind(select_next_match, _1, ex), append);
else
@ -688,7 +689,7 @@ void do_select_regex(Editor& editor, int count)
{
try
{
std::string ex = prompt("select: ");
String ex = prompt("select: ");
editor.multi_select(std::bind(select_all_matches, _1, ex));
}
catch (prompt_aborted&) {}
@ -698,7 +699,7 @@ void do_split_regex(Editor& editor, int count)
{
try
{
std::string ex = prompt("split: ");
String ex = prompt("split: ");
editor.multi_select(std::bind(split_selection, _1, ex));
}
catch (prompt_aborted&) {}
@ -842,7 +843,7 @@ public:
{ RegisterManager::instance()[m_name] = m_save; }
private:
std::vector<std::string> m_save;
std::vector<String> m_save;
char m_name;
};
@ -859,14 +860,14 @@ void exec_keys(const KeyList& keys,
size_t pos = 0;
prompt_func = [&](const std::string&, Completer) {
prompt_func = [&](const String&, Completer) {
size_t begin = pos;
while (pos < keys.size() and keys[pos].key != '\n')
++pos;
std::string result;
String result;
for (size_t i = begin; i < pos; ++i)
result += keys[i].key;
result += String() + keys[i].key;
++pos;
return result;
@ -957,14 +958,14 @@ int main(int argc, char* argv[])
command_manager.register_commands({ "agh", "addgrouphl" }, add_group_highlighter,
CommandManager::None,
PerArgumentCommandCompleter({
[&](const std::string& prefix, size_t cursor_pos)
[&](const String& prefix, size_t cursor_pos)
{ return main_context.window().highlighters().complete_group_id(prefix, cursor_pos); },
std::bind(&HighlighterRegistry::complete_highlighter, &highlighter_registry, _1, _2)
}));
command_manager.register_commands({ "rh", "rmhl" }, rm_highlighter,
CommandManager::None,
PerArgumentCommandCompleter({
[&](const std::string& prefix, size_t cursor_pos)
[&](const String& prefix, size_t cursor_pos)
{ return main_context.window().highlighters().complete_group_id(prefix, cursor_pos); }
}));
command_manager.register_commands({ "rgh", "rmgrouphl" }, rm_group_highlighter,
@ -972,8 +973,8 @@ int main(int argc, char* argv[])
[&](const CommandParameters& params, size_t token_to_complete, size_t pos_in_token)
{
Window& w = main_context.window();
const std::string& arg = token_to_complete < params.size() ?
params[token_to_complete] : std::string();
const String& arg = token_to_complete < params.size() ?
params[token_to_complete] : String();
if (token_to_complete == 0)
return w.highlighters().complete_group_id(arg, pos_in_token);
else if (token_to_complete == 1)
@ -987,7 +988,7 @@ int main(int argc, char* argv[])
command_manager.register_commands({ "rf", "rmfilter" }, rm_filter,
CommandManager::None,
PerArgumentCommandCompleter({
[&](const std::string& prefix, size_t cursor_pos)
[&](const String& prefix, size_t cursor_pos)
{ return main_context.window().complete_filterid(prefix, cursor_pos); }
}));
command_manager.register_command("hook", add_hook, CommandManager::IgnoreSemiColons | CommandManager::DeferredShellEval);
@ -1006,7 +1007,7 @@ int main(int argc, char* argv[])
[&](const CommandParameters& params, const Context&) { set_option(option_manager, params); },
CommandManager::None,
PerArgumentCommandCompleter({
[&](const std::string& prefix, size_t cursor_pos)
[&](const String& prefix, size_t cursor_pos)
{ return option_manager.complete_option_name(prefix, cursor_pos); }
}));
command_manager.register_commands({ "setb", "setbuffer" },
@ -1014,7 +1015,7 @@ int main(int argc, char* argv[])
{ set_option(context.buffer().option_manager(), params); },
CommandManager::None,
PerArgumentCommandCompleter({
[&](const std::string& prefix, size_t cursor_pos)
[&](const String& prefix, size_t cursor_pos)
{ return main_context.buffer().option_manager().complete_option_name(prefix, cursor_pos); }
}));
command_manager.register_commands({ "setw", "setwindow" },
@ -1022,7 +1023,7 @@ int main(int argc, char* argv[])
{ set_option(context.window().option_manager(), params); },
CommandManager::None,
PerArgumentCommandCompleter({
[&](const std::string& prefix, size_t cursor_pos)
[&](const String& prefix, size_t cursor_pos)
{ return main_context.window().option_manager().complete_option_name(prefix, cursor_pos); }
}));
@ -1041,6 +1042,7 @@ int main(int argc, char* argv[])
try
{
write_debug("*** This is the debug buffer, where debug info will be written ***\n");
write_debug("utf-8 test: é á ï");
auto buffer = (argc > 1) ? open_or_create(argv[1]) : new Buffer("*scratch*", Buffer::Type::Scratch);
main_context = Context(*buffer->get_or_create_window());

View File

@ -35,6 +35,7 @@ public:
memoryview(const std::initializer_list<T>& v)
: m_pointer(v.begin()), m_size(v.size()) {}
const T* pointer() const { return m_pointer; }
size_t size() const { return m_size; }
const T& operator[](size_t n) const { return *(m_pointer + n); }

View File

@ -82,7 +82,7 @@ void draw_window(Window& window)
for (const DisplayAtom& atom : window.display_buffer())
{
assert(position == atom.coord());
const std::string content = atom.content();
const String content = atom.content();
set_attribute(A_UNDERLINE, atom.attribute() & Underline);
set_attribute(A_REVERSE, atom.attribute() & Reverse);
@ -91,17 +91,16 @@ void draw_window(Window& window)
set_color(atom.fg_color(), atom.bg_color());
size_t pos = 0;
size_t end;
auto pos = content.begin();
while (true)
{
move(position.line, position.column);
clrtoeol();
end = content.find_first_of('\n', pos);
std::string line = content.substr(pos, end - pos);
auto end = std::find(pos, content.end(), '\n');
String line(pos, end);
addstr(line.c_str());
if (end != std::string::npos)
if (end != content.end())
{
addch(' ');
position.line = position.line + 1;
@ -134,7 +133,7 @@ void draw_window(Window& window)
}
set_color(Color::Cyan, Color::Black);
std::string status_line = window.status_line();
String status_line = window.status_line();
static int last_status_length = 0;
move(max_y, max_x - last_status_length);
clrtoeol();
@ -167,7 +166,7 @@ static Key get_key()
return Key(modifiers, c);
}
static std::string prompt(const std::string& text, Completer completer)
static String prompt(const String& text, Completer completer)
{
curs_set(2);
auto restore_cursor = on_scope_end([]() { curs_set(0); });
@ -182,13 +181,13 @@ static std::string prompt(const std::string& text, Completer completer)
Completions completions;
int current_completion = -1;
std::string text_before_completion;
String text_before_completion;
std::string result;
std::string saved_result;
String result;
String saved_result;
static std::unordered_map<std::string, std::vector<std::string>> history_per_prompt;
std::vector<std::string>& history = history_per_prompt[text];
static std::unordered_map<String, std::vector<String>> history_per_prompt;
std::vector<String>& history = history_per_prompt[text];
auto history_it = history.end();
while (true)
@ -198,7 +197,7 @@ static std::string prompt(const std::string& text, Completer completer)
{
case '\r':
{
std::vector<std::string>::iterator it;
std::vector<String>::iterator it;
while ((it = find(history, result)) != history.end())
history.erase(it);
@ -238,7 +237,7 @@ static std::string prompt(const std::string& text, Completer completer)
if (cursor_pos != 0)
{
result = result.substr(0, cursor_pos - 1)
+ result.substr(cursor_pos, std::string::npos);
+ result.substr(cursor_pos, String::npos);
--cursor_pos;
}
@ -248,9 +247,9 @@ static std::string prompt(const std::string& text, Completer completer)
case CTRL('r'):
{
c = getch();
std::string reg = RegisterManager::instance()[c].get();
String reg = RegisterManager::instance()[c].get();
current_completion = -1;
result = result.substr(0, cursor_pos) + reg + result.substr(cursor_pos, std::string::npos);
result = result.substr(0, cursor_pos) + reg + result.substr(cursor_pos, String::npos);
cursor_pos += reg.length();
}
break;
@ -269,7 +268,7 @@ static std::string prompt(const std::string& text, Completer completer)
}
++current_completion;
std::string completion;
String completion;
if (current_completion >= completions.candidates.size())
{
if (current_completion == completions.candidates.size() and
@ -291,7 +290,7 @@ static std::string prompt(const std::string& text, Completer completer)
}
default:
current_completion = -1;
result = result.substr(0, cursor_pos) + (char)c + result.substr(cursor_pos, std::string::npos);
result = result.substr(0, cursor_pos) + (char)c + result.substr(cursor_pos, String::npos);
++cursor_pos;
}
@ -304,7 +303,7 @@ static std::string prompt(const std::string& text, Completer completer)
return result;
}
void print_status(const std::string& status)
void print_status(const String& status)
{
int x,y;
getmaxyx(stdscr, y, x);
@ -315,6 +314,7 @@ void print_status(const std::string& status)
void init(PromptFunc& prompt_func, GetKeyFunc& get_key_func)
{
// setlocale(LC_ALL, "");
initscr();
cbreak();
noecho();

View File

@ -11,7 +11,7 @@ namespace Kakoune
class Window;
typedef std::function<std::string (const std::string&, Completer)> PromptFunc;
typedef std::function<String (const String&, Completer)> PromptFunc;
typedef std::function<Key ()> GetKeyFunc;
struct prompt_aborted {};
@ -22,7 +22,7 @@ namespace NCurses
void init(PromptFunc& prompt_func, GetKeyFunc& get_key_func);
void deinit();
void draw_window(Window& window);
void print_status(const std::string& status);
void print_status(const String& status);
}

View File

@ -5,14 +5,14 @@
namespace Kakoune
{
std::string int_to_str(int value)
String int_to_str(int value)
{
std::ostringstream oss;
oss << value;
return oss.str();
}
Option& OptionManager::operator[] (const std::string& name)
Option& OptionManager::operator[] (const String& name)
{
auto it = m_options.find(name);
if (it != m_options.end())
@ -23,7 +23,7 @@ Option& OptionManager::operator[] (const std::string& name)
return m_options[name];
}
const Option& OptionManager::operator[] (const std::string& name) const
const Option& OptionManager::operator[] (const String& name) const
{
auto it = m_options.find(name);
if (it != m_options.end())
@ -34,10 +34,10 @@ const Option& OptionManager::operator[] (const std::string& name) const
throw option_not_found(name);
}
CandidateList OptionManager::complete_option_name(const std::string& prefix,
CandidateList OptionManager::complete_option_name(const String& prefix,
size_t cursor_pos)
{
std::string real_prefix = prefix.substr(0, cursor_pos);
String real_prefix = prefix.substr(0, cursor_pos);
CandidateList result;
if (m_parent)
result = m_parent->complete_option_name(prefix, cursor_pos);

View File

@ -12,26 +12,26 @@ namespace Kakoune
struct option_not_found : public runtime_error
{
option_not_found(const std::string& name)
option_not_found(const String& name)
: runtime_error("option not found: " + name) {}
};
std::string int_to_str(int value);
String int_to_str(int value);
class Option
{
public:
Option() {}
explicit Option(int value) : m_value(int_to_str(value)) {}
explicit Option(const std::string& value) : m_value(value) {}
explicit Option(const String& value) : m_value(value) {}
Option& operator=(int value) { m_value = int_to_str(value); return *this; }
Option& operator=(const std::string& value) { m_value = value; return *this; }
Option& operator=(const String& value) { m_value = value; return *this; }
operator int() const { return atoi(m_value.c_str()); }
operator std::string() const { return m_value; }
operator String() const { return m_value; }
private:
std::string m_value;
String m_value;
};
class OptionManager
@ -40,10 +40,10 @@ public:
OptionManager(OptionManager& parent)
: m_parent(&parent) {}
Option& operator[] (const std::string& name);
const Option& operator[] (const std::string& name) const;
Option& operator[] (const String& name);
const Option& operator[] (const String& name) const;
CandidateList complete_option_name(const std::string& prefix,
CandidateList complete_option_name(const String& prefix,
size_t cursor_pos);
private:
@ -52,7 +52,7 @@ private:
// the only one allowed to construct a root option manager
friend class GlobalOptionManager;
std::unordered_map<std::string, Option> m_options;
std::unordered_map<String, Option> m_options;
OptionManager* m_parent;
};

17
src/regex.hh Normal file
View File

@ -0,0 +1,17 @@
#ifndef regex_hh_INCLUDED
#define regex_hh_INCLUDED
#include "string.hh"
#include <boost/regex.hpp>
namespace Kakoune
{
typedef boost::regex_iterator<BufferIterator> RegexIterator;
typedef boost::basic_regex<Character> Regex;
}
#endif // regex_hh_INCLUDED

View File

@ -3,22 +3,22 @@
namespace Kakoune
{
const std::string Register::ms_empty;
const String Register::ms_empty;
Register& Register::operator=(const std::string& value)
Register& Register::operator=(const String& value)
{
m_content.clear();
m_content.push_back(value);
return *this;
}
Register& Register::operator=(const memoryview<std::string>& values)
Register& Register::operator=(const memoryview<String>& values)
{
m_content = std::vector<std::string>(values.begin(), values.end());
m_content = std::vector<String>(values.begin(), values.end());
return *this;
}
const std::string& Register::get() const
const String& Register::get() const
{
if (m_content.size() != 0)
return m_content.front();
@ -26,7 +26,7 @@ const std::string& Register::get() const
return ms_empty;
}
const std::string& Register::get(size_t index) const
const String& Register::get(size_t index) const
{
if (m_content.size() > index)
return m_content[index];

View File

@ -1,9 +1,9 @@
#ifndef register_hh_INCLUDED
#define register_hh_INCLUDED
#include <string>
#include <vector>
#include "string.hh"
#include "memoryview.hh"
namespace Kakoune
@ -12,20 +12,20 @@ namespace Kakoune
class Register
{
public:
Register& operator=(const std::string& value);
Register& operator=(const memoryview<std::string>& values);
Register& operator=(const String& value);
Register& operator=(const memoryview<String>& values);
const std::string& get() const;
const std::string& get(size_t index) const;
const String& get() const;
const String& get(size_t index) const;
operator memoryview<std::string>() const
{ return memoryview<std::string>(m_content); }
operator memoryview<String>() const
{ return memoryview<String>(m_content); }
const std::vector<std::string>& content() const { return m_content; }
const std::vector<String>& content() const { return m_content; }
private:
std::vector<std::string> m_content;
std::vector<String> m_content;
static const std::string ms_empty;
static const String ms_empty;
};
}

View File

@ -1,6 +1,7 @@
#include "selectors.hh"
#include <boost/regex.hpp>
#include "regex.hh"
#include <algorithm>
namespace Kakoune
@ -371,7 +372,7 @@ SelectionAndCaptures select_whole_buffer(const Selection& selection)
}
SelectionAndCaptures select_next_match(const Selection& selection,
const std::string& regex)
const String& regex)
{
try
{
@ -379,7 +380,7 @@ SelectionAndCaptures select_next_match(const Selection& selection,
BufferIterator end = begin;
CaptureList captures;
boost::regex ex(regex);
Regex ex(regex.begin(), regex.end());
boost::match_results<BufferIterator> matches;
if (boost::regex_search(begin+1, begin.buffer().end(),
@ -387,16 +388,16 @@ SelectionAndCaptures select_next_match(const Selection& selection,
{
begin = matches[0].first;
end = matches[0].second;
std::copy(matches.begin(), matches.end(),
std::back_inserter(captures));
for (auto& match : matches)
captures.push_back(String(match.first, match.second));
}
else if (boost::regex_search(begin.buffer().begin(), begin+1,
matches, ex))
{
begin = matches[0].first;
end = matches[0].second;
std::copy(matches.begin(), matches.end(),
std::back_inserter(captures));
for (auto& match : matches)
captures.push_back(String(match.first, match.second));
}
if (begin == end)
++end;
@ -405,18 +406,16 @@ SelectionAndCaptures select_next_match(const Selection& selection,
}
catch (boost::regex_error& err)
{
throw runtime_error(std::string("regex error: ") + err.what());
throw runtime_error(String("regex error: ") + err.what());
}
}
typedef boost::regex_iterator<BufferIterator> RegexIterator;
SelectionAndCapturesList select_all_matches(const Selection& selection,
const std::string& regex)
const String& regex)
{
try
{
boost::regex ex(regex);
Regex ex(regex.begin(), regex.end());
RegexIterator re_it(selection.begin(), selection.end(), ex);
RegexIterator re_end;
@ -426,7 +425,9 @@ SelectionAndCapturesList select_all_matches(const Selection& selection,
BufferIterator begin = (*re_it)[0].first;
BufferIterator end = (*re_it)[0].second;
CaptureList captures(re_it->begin(), re_it->end());
CaptureList captures;
for (auto& match : *re_it)
captures.push_back(String(match.first, match.second));
result.push_back(SelectionAndCaptures(begin,
begin == end ? end : end-1,
@ -436,16 +437,16 @@ SelectionAndCapturesList select_all_matches(const Selection& selection,
}
catch (boost::regex_error& err)
{
throw runtime_error(std::string("regex error: ") + err.what());
throw runtime_error(String("regex error: ") + err.what());
}
}
SelectionAndCapturesList split_selection(const Selection& selection,
const std::string& separator_regex)
const String& separator_regex)
{
try
{
boost::regex ex(separator_regex);
Regex ex(separator_regex.begin(), separator_regex.end());
RegexIterator re_it(selection.begin(), selection.end(), ex,
boost::regex_constants::match_nosubs);
RegexIterator re_end;
@ -464,7 +465,7 @@ SelectionAndCapturesList split_selection(const Selection& selection,
}
catch (boost::regex_error& err)
{
throw runtime_error(std::string("regex error: ") + err.what());
throw runtime_error(String("regex error: ") + err.what());
}
}

View File

@ -33,13 +33,13 @@ SelectionAndCaptures select_whole_lines(const Selection& selection);
SelectionAndCaptures select_whole_buffer(const Selection& selection);
SelectionAndCaptures select_next_match(const Selection& selection,
const std::string& regex);
const String& regex);
SelectionAndCapturesList select_all_matches(const Selection& selection,
const std::string& regex);
const String& regex);
SelectionAndCapturesList split_selection(const Selection& selection,
const std::string& separator_regex);
const String& separator_regex);
}

16
src/string.hh Normal file
View File

@ -0,0 +1,16 @@
#ifndef string_hh_INCLUDED
#define string_hh_INCLUDED
#include <string>
namespace Kakoune
{
typedef char Character;
typedef std::string String;
}
#endif // string_hh_INCLUDED

View File

@ -112,7 +112,7 @@ bool contains(const Container& container, const T& value)
return find(container, value) != container.end();
}
inline std::string str_to_str(const std::string& str)
inline String str_to_str(const String& str)
{
return str;
}

View File

@ -113,7 +113,7 @@ void Window::scroll_to_keep_cursor_visible_ifn()
}
}
std::string Window::status_line() const
String Window::status_line() const
{
BufferCoord cursor = buffer().line_and_column_at(selections().back().last());
std::ostringstream oss;

View File

@ -35,7 +35,7 @@ public:
void update_display_buffer();
std::string status_line() const;
String status_line() const;
HighlighterGroup& highlighters() { return m_highlighters; }