From 12856066b1065adc99aa1ee0319e91f8eaf522cb Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Mon, 28 Nov 2016 23:53:50 +0000 Subject: [PATCH] Cleanup include dependencies a bit --- src/buffer_utils.cc | 8 +++++--- src/commands.cc | 1 + src/completion.cc | 1 + src/file.cc | 28 ++++++++++++++++------------ src/file.hh | 27 ++++++--------------------- src/json_ui.cc | 7 ++++--- src/main.cc | 4 ++++ src/ranked_match.cc | 5 ++++- src/ranked_match.hh | 3 --- src/shell_manager.cc | 9 +++++---- src/string.hh | 30 +++++++++++++++--------------- src/unicode.hh | 4 +++- 12 files changed, 64 insertions(+), 63 deletions(-) diff --git a/src/buffer_utils.cc b/src/buffer_utils.cc index 3f0ec837..735657e0 100644 --- a/src/buffer_utils.cc +++ b/src/buffer_utils.cc @@ -4,6 +4,8 @@ #include "event_manager.hh" #include "file.hh" +#include + #if defined(__APPLE__) #define st_mtim st_mtimespec #endif @@ -128,7 +130,7 @@ Buffer* create_fifo_buffer(String name, int fd, bool scroll) ssize_t count = 0; do { - count = read(fifo, data, buffer_size); + count = ::read(fifo, data, buffer_size); auto pos = buffer->back_coord(); const bool prevent_scrolling = pos == BufferCoord{0,0} and not scroll; @@ -165,8 +167,8 @@ void write_to_debug_buffer(StringView str) { if (not BufferManager::has_instance()) { - write_stderr(str); - write_stderr("\n"); + write(2, str); + write(2, "\n"); return; } diff --git a/src/commands.cc b/src/commands.cc index 32d54996..6b1025d6 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -21,6 +21,7 @@ #include "register_manager.hh" #include "insert_completer.hh" #include "remote.hh" +#include "regex.hh" #include "shell_manager.hh" #include "string.hh" #include "window.hh" diff --git a/src/completion.cc b/src/completion.cc index 77b5d6b8..e85d5fe1 100644 --- a/src/completion.cc +++ b/src/completion.cc @@ -1,6 +1,7 @@ #include "completion.hh" #include "file.hh" #include "context.hh" +#include "regex.hh" namespace Kakoune { diff --git a/src/file.cc b/src/file.cc index 92ddcf13..1455624b 100644 --- a/src/file.cc +++ b/src/file.cc @@ -2,10 +2,11 @@ #include "assert.hh" #include "buffer.hh" -#include "unicode.hh" +#include "exception.hh" #include "ranked_match.hh" #include "regex.hh" #include "string.hh" +#include "unicode.hh" #include #include @@ -33,6 +34,14 @@ namespace Kakoune { +struct file_access_error : runtime_error +{ +public: + file_access_error(StringView filename, + StringView error_desc) + : runtime_error(format("{}: {}", filename, error_desc)) {} +}; + String parse_filename(StringView filename) { if (filename.length() >= 1 and filename[0_byte] == '~' and @@ -168,14 +177,9 @@ String read_file(StringView filename, bool text) { int fd = open(parse_filename(filename).c_str(), O_RDONLY); if (fd == -1) - { - if (errno == ENOENT) - throw file_not_found(filename); - throw file_access_error(filename, strerror(errno)); - } - auto close_fd = on_scope_end([fd]{ close(fd); }); + auto close_fd = on_scope_end([fd]{ close(fd); }); return read_fd(fd, text); } @@ -185,12 +189,7 @@ MappedFile::MappedFile(StringView filename) fd = open(real_filename.c_str(), O_RDONLY | O_NONBLOCK); if (fd == -1) - { - if (errno == ENOENT) - throw file_not_found{real_filename}; - throw file_access_error(real_filename, strerror(errno)); - } fstat(fd, &st); if (S_ISDIR(st.st_mode)) @@ -208,6 +207,11 @@ MappedFile::~MappedFile() } } +MappedFile::operator StringView() const +{ + return { data, (int)st.st_size }; +} + bool file_exists(StringView filename) { String real_filename = real_path(parse_filename(filename)); diff --git a/src/file.hh b/src/file.hh index 2c73ed7b..ae1d9db2 100644 --- a/src/file.hh +++ b/src/file.hh @@ -2,10 +2,9 @@ #define file_hh_INCLUDED #include "array_view.hh" -#include "completion.hh" -#include "exception.hh" -#include "regex.hh" #include "flags.hh" +#include "units.hh" +#include "vector.hh" #include #include @@ -13,23 +12,12 @@ namespace Kakoune { -struct file_access_error : runtime_error -{ -public: - file_access_error(StringView filename, - StringView error_desc) - : runtime_error(format("{}: {}", filename, error_desc)) {} -}; - -struct file_not_found : file_access_error -{ - file_not_found(StringView filename) - : file_access_error(filename, "file not found") {} -}; - class Buffer; class String; class StringView; +class Regex; + +using CandidateList = Vector; // parse ~/ and $env values in filename and returns the translated filename String parse_filename(StringView filename); @@ -45,16 +33,13 @@ bool fd_readable(int fd); String read_fd(int fd, bool text = false); String read_file(StringView filename, bool text = false); void write(int fd, StringView data); -inline void write_stdout(StringView str) { write(1, str); } -inline void write_stderr(StringView str) { write(2, str); } - struct MappedFile { MappedFile(StringView filename); ~MappedFile(); - operator StringView() const { return { data, (int)st.st_size }; } + operator StringView() const; int fd; const char* data; diff --git a/src/json_ui.cc b/src/json_ui.cc index b74464a0..f24b78c5 100644 --- a/src/json_ui.cc +++ b/src/json_ui.cc @@ -2,6 +2,7 @@ #include "containers.hh" #include "display_buffer.hh" +#include "exception.hh" #include "keys.hh" #include "file.hh" #include "event_manager.hh" @@ -159,7 +160,7 @@ void rpc_call(StringView method, Args&&... args) auto q = format(R"(\{ "jsonrpc": "2.0", "method": "{}", "params": [{}] }{})", method, concat(std::forward(args)...), "\n"); - write_stdout(q); + write(1, q); } JsonUI::JsonUI() @@ -424,8 +425,8 @@ void JsonUI::parse_requests(EventMode mode) } catch (runtime_error& error) { - write_stderr(format("error while handling requests '{}': '{}'", - m_requests, error.what())); + write(2, format("error while handling requests '{}': '{}'", + m_requests, error.what())); // try to salvage request by dropping its first line pos = std::min(m_requests.end(), find(m_requests, '\n')+1); } diff --git a/src/main.cc b/src/main.cc index dc25efc8..f522c088 100644 --- a/src/main.cc +++ b/src/main.cc @@ -18,6 +18,7 @@ #include "parameters_parser.hh" #include "register_manager.hh" #include "remote.hh" +#include "regex.hh" #include "scope.hh" #include "shell_manager.hh" #include "string.hh" @@ -38,6 +39,9 @@ struct startup_error : Kakoune::runtime_error using Kakoune::runtime_error::runtime_error; }; +inline void write_stdout(StringView str) { write(1, str); } +inline void write_stderr(StringView str) { write(2, str); } + String runtime_directory() { char relpath[PATH_MAX+1]; diff --git a/src/ranked_match.cc b/src/ranked_match.cc index 1f3e36fd..719fb08b 100644 --- a/src/ranked_match.cc +++ b/src/ranked_match.cc @@ -1,13 +1,16 @@ #include "ranked_match.hh" -#include "utf8_iterator.hh" +#include "flags.hh" #include "unit_tests.hh" +#include "utf8_iterator.hh" #include namespace Kakoune { +template<> struct WithBitOps : std::true_type {}; + UsedLetters used_letters(StringView str) { UsedLetters res = 0; diff --git a/src/ranked_match.hh b/src/ranked_match.hh index 91169202..4a56528a 100644 --- a/src/ranked_match.hh +++ b/src/ranked_match.hh @@ -2,7 +2,6 @@ #define ranked_match_hh_INCLUDED #include "string.hh" -#include "flags.hh" namespace Kakoune { @@ -51,8 +50,6 @@ private: int m_max_index = 0; }; -template<> struct WithBitOps : std::true_type {}; - } #endif // ranked_match_hh_INCLUDED diff --git a/src/shell_manager.cc b/src/shell_manager.cc index 2cee4cbe..1ee1c280 100644 --- a/src/shell_manager.cc +++ b/src/shell_manager.cc @@ -1,12 +1,13 @@ #include "shell_manager.hh" +#include "buffer_utils.hh" #include "clock.hh" #include "context.hh" -#include "buffer_utils.hh" -#include "event_manager.hh" -#include "file.hh" -#include "face_registry.hh" #include "display_buffer.hh" +#include "event_manager.hh" +#include "face_registry.hh" +#include "file.hh" +#include "regex.hh" #include #include diff --git a/src/string.hh b/src/string.hh index 5721dc6a..d7e39ff9 100644 --- a/src/string.hh +++ b/src/string.hh @@ -114,7 +114,7 @@ public: explicit String(Codepoint cp, ColumnCount count) { kak_assert(count % codepoint_width(cp) == 0); - int cp_count = (int)count / codepoint_width(cp); + int cp_count = (int)(count / codepoint_width(cp)); reserve(utf8::codepoint_size(cp) * cp_count); while (cp_count-- > 0) utf8::dump(std::back_inserter(*this), cp); @@ -289,7 +289,7 @@ inline String& operator+=(String& lhs, StringView rhs) inline String operator+(StringView lhs, StringView rhs) { String res; - res.reserve((int)(lhs.length() + rhs.length())); + res.reserve(lhs.length() + rhs.length()); res.append(lhs.data(), lhs.length()); res.append(rhs.data(), rhs.length()); return res; @@ -312,6 +312,11 @@ inline bool operator<(const StringView& lhs, const StringView& rhs) rhs.begin(), rhs.end()); } +inline String operator"" _str(const char* str, size_t) +{ + return String(str); +} + Vector split(StringView str, char separator, char escape); Vector split(StringView str, char separator); @@ -335,11 +340,17 @@ String join(const Container& container, char joiner, bool esc_joiner = true) return res; } -inline String operator"" _str(const char* str, size_t) +inline bool prefix_match(StringView str, StringView prefix) { - return String(str); + return str.substr(0_byte, prefix.length()) == prefix; } +bool subsequence_match(StringView str, StringView subseq); + +String expand_tabs(StringView line, ColumnCount tabstop, ColumnCount col = 0); + +Vector wrap_lines(StringView text, ColumnCount max_width); + int str_to_int(StringView str); // throws on error Optional str_to_int_ifp(StringView str); @@ -377,17 +388,6 @@ to_string(const StronglyTypedNumber& val) return to_string((ValueType)val); } -inline bool prefix_match(StringView str, StringView prefix) -{ - return str.substr(0_byte, prefix.length()) == prefix; -} - -bool subsequence_match(StringView str, StringView subseq); - -String expand_tabs(StringView line, ColumnCount tabstop, ColumnCount col = 0); - -Vector wrap_lines(StringView text, ColumnCount max_width); - namespace detail { diff --git a/src/unicode.hh b/src/unicode.hh index 7a33b1fe..1c96e1cd 100644 --- a/src/unicode.hh +++ b/src/unicode.hh @@ -5,6 +5,8 @@ #include #include +#include "units.hh" + namespace Kakoune { @@ -49,7 +51,7 @@ inline bool is_basic_alpha(Codepoint c) return (c >= 'a' and c <= 'z') or (c >= 'A' and c <= 'Z'); } -inline size_t codepoint_width(Codepoint c) +inline ColumnCount codepoint_width(Codepoint c) { return c == '\n' ? 1 : wcwidth((wchar_t)c); }