buffer_utils.cc (15840B)
1 #include "buffer_utils.hh" 2 3 #include "buffer_manager.hh" 4 #include "hook_manager.hh" 5 #include "coord.hh" 6 #include "event_manager.hh" 7 #include "option_manager.hh" 8 #include "file.hh" 9 #include "selection.hh" 10 #include "changes.hh" 11 12 #include <unistd.h> 13 14 #if defined(__APPLE__) 15 #define st_mtim st_mtimespec 16 #endif 17 18 19 namespace Kakoune 20 { 21 22 void replace(Buffer& buffer, ArrayView<BufferRange> ranges, ConstArrayView<String> strings) 23 { 24 ForwardChangesTracker changes_tracker; 25 size_t timestamp = buffer.timestamp(); 26 for (size_t index = 0; index < ranges.size(); ++index) 27 { 28 auto& range = ranges[index]; 29 range.begin = changes_tracker.get_new_coord_tolerant(range.begin); 30 range.end = changes_tracker.get_new_coord_tolerant(range.end); 31 kak_assert(buffer.is_valid(range.begin) and buffer.is_valid(range.end)); 32 33 range = buffer.replace(range.begin, range.end, strings.empty() ? StringView{} : strings[std::min(index, strings.size()-1)]); 34 kak_assert(buffer.is_valid(range.begin) and buffer.is_valid(range.end)); 35 changes_tracker.update(buffer, timestamp); 36 } 37 38 buffer.check_invariant(); 39 } 40 41 ColumnCount get_column(const Buffer& buffer, 42 ColumnCount tabstop, BufferCoord coord) 43 { 44 auto line = buffer[coord.line]; 45 auto col = 0_col; 46 for (auto it = line.begin(); 47 it != line.end() and coord.column > (int)(it - line.begin()); ) 48 { 49 if (*it == '\t') 50 { 51 col = (col / tabstop + 1) * tabstop; 52 ++it; 53 } 54 else 55 col += codepoint_width(utf8::read_codepoint(it, line.end())); 56 } 57 return col; 58 } 59 60 ColumnCount column_length(const Buffer& buffer, ColumnCount tabstop, LineCount line) 61 { 62 return get_column(buffer, tabstop, BufferCoord{line, ByteCount{INT_MAX}}); 63 } 64 65 ByteCount get_byte_to_column(const Buffer& buffer, ColumnCount tabstop, DisplayCoord coord) 66 { 67 auto line = buffer[coord.line]; 68 auto col = 0_col; 69 auto it = line.begin(); 70 while (it != line.end() and coord.column > col) 71 { 72 if (*it == '\t') 73 { 74 col = (col / tabstop + 1) * tabstop; 75 if (col > coord.column) // the target column was in the tab 76 break; 77 ++it; 78 } 79 else 80 { 81 auto next = it; 82 col += codepoint_width(utf8::read_codepoint(next, line.end())); 83 if (col > coord.column) // the target column was in the char 84 break; 85 it = next; 86 } 87 } 88 return (int)(it - line.begin()); 89 } 90 91 static BufferLines parse_lines(const char* pos, const char* end, EolFormat eolformat, size_t expected_line_count = 0) 92 { 93 BufferLines lines; 94 lines.reserve(expected_line_count); 95 while (pos < end) 96 { 97 if (lines.size() >= std::numeric_limits<int>::max()) 98 throw runtime_error("too many lines"); 99 100 const char* eol = std::find(pos, end, '\n'); 101 if ((eol - pos) >= std::numeric_limits<int>::max()) 102 throw runtime_error("line is too long"); 103 104 lines.emplace_back(StringData::create(StringView{pos, eol - (eolformat == EolFormat::Crlf and eol != end ? 1 : 0)}, "\n")); 105 pos = eol + 1; 106 } 107 108 if (lines.empty()) 109 lines.emplace_back(StringData::create("\n")); 110 111 return lines; 112 } 113 114 Buffer* create_buffer_from_string(String name, Buffer::Flags flags, StringView data) 115 { 116 return BufferManager::instance().create_buffer( 117 std::move(name), flags, 118 parse_lines(data.begin(), data.end(), EolFormat::Lf), 119 ByteOrderMark::None, EolFormat::Lf, FinalEol::Present, 120 FsStatus{InvalidTime, {}, {}}); 121 } 122 123 template<typename Func> 124 decltype(auto) parse_file(StringView filename, Func&& func) 125 { 126 MappedFile file{parse_filename(filename)}; 127 128 const char* pos = file.data; 129 const char* end = pos + file.st.st_size; 130 131 auto bom = ByteOrderMark::None; 132 if (file.st.st_size >= 3 and StringView{pos, 3_byte} == "\xEF\xBB\xBF") 133 { 134 bom = ByteOrderMark::Utf8; 135 pos += 3; 136 } 137 138 size_t line_count = 1; 139 bool has_crlf = false, has_lf = false; 140 for (auto it = std::find(pos, end, '\n'); it != end; it = std::find(it+1, end, '\n'), ++line_count) 141 ((it != pos and *(it-1) == '\r') ? has_crlf : has_lf) = true; 142 auto eolformat = (has_crlf and not has_lf) ? EolFormat::Crlf : EolFormat::Lf; 143 auto finaleol = pos == end ? FinalEol::IfNotEmpty 144 : *(end-1) == '\n' ? FinalEol::Present : FinalEol::Missing; 145 FsStatus fs_status{file.st.st_mtim, file.st.st_size, murmur3(file.data, file.st.st_size)}; 146 return func(parse_lines(pos, end, eolformat, line_count), bom, eolformat, finaleol, fs_status); 147 } 148 149 Buffer* open_file_buffer(StringView filename, Buffer::Flags flags) 150 { 151 return parse_file(filename, [&](BufferLines&& lines, ByteOrderMark bom, EolFormat eolformat, FinalEol finaleol, FsStatus fs_status) { 152 return BufferManager::instance().create_buffer(filename.str(), Buffer::Flags::File | flags, 153 std::move(lines), bom, eolformat, finaleol, fs_status); 154 }); 155 } 156 157 Buffer* open_or_create_file_buffer(StringView filename, Buffer::Flags flags) 158 { 159 auto path = parse_filename(filename); 160 if (file_exists(path)) 161 return open_file_buffer(filename.str(), Buffer::Flags::File | flags); 162 return create_buffer_from_string(filename.str(), Buffer::Flags::File | Buffer::Flags::New, StringView{}); 163 } 164 165 void reload_file_buffer(Buffer& buffer) 166 { 167 kak_assert(buffer.flags() & Buffer::Flags::File); 168 parse_file(buffer.filename(), [&](auto&&... params) { 169 buffer.reload(std::forward<decltype(params)>(params)...); 170 }); 171 buffer.flags() &= ~Buffer::Flags::New; 172 } 173 174 void write_buffer_to_fd(Buffer& buffer, int fd, Optional<FinalEol> finaleol) 175 { 176 if (not finaleol) 177 finaleol = buffer.options()["finaleol"].get<FinalEol>(); 178 auto eolformat = buffer.options()["eolformat"].get<EolFormat>(); 179 StringView eoldata; 180 if (eolformat == EolFormat::Crlf) 181 eoldata = "\r\n"; 182 else 183 eoldata = "\n"; 184 bool write_eol_at_eof = *finaleol == FinalEol::Present or 185 (*finaleol == FinalEol::IfNotEmpty and (buffer.line_count() != 1 or buffer[0] != "\n")); 186 187 BufferedWriter<false> writer{fd}; 188 if (buffer.options()["BOM"].get<ByteOrderMark>() == ByteOrderMark::Utf8) 189 writer.write("\xEF\xBB\xBF"); 190 191 for (LineCount i = 0; i < buffer.line_count(); ++i) 192 { 193 // end of lines are written according to eolformat but always 194 // stored as \n 195 StringView linedata = buffer[i]; 196 writer.write(linedata.substr(0, linedata.length()-1)); 197 if (write_eol_at_eof or i != buffer.line_count()-1) 198 writer.write(eoldata); 199 } 200 } 201 202 void write_buffer_to_file(Buffer& buffer, StringView filename, 203 WriteMethod method, WriteFlags flags, 204 Optional<FinalEol> finaleol) 205 { 206 auto zfilename = filename.zstr(); 207 struct stat st; 208 209 bool replace = method == WriteMethod::Replace; 210 bool force = flags & WriteFlags::Force; 211 212 if ((replace or force) and (::stat(zfilename, &st) != 0 or 213 (st.st_mode & S_IFMT) != S_IFREG)) 214 { 215 force = false; 216 replace = false; 217 } 218 219 if (force and ::chmod(zfilename, st.st_mode | S_IWUSR) < 0) 220 throw runtime_error(format("unable to change file permissions: {}", strerror(errno))); 221 222 char temp_filename[PATH_MAX]; 223 const int fd = replace ? open_temp_file(filename, temp_filename) 224 : create_file(zfilename); 225 if (fd == -1) 226 { 227 auto saved_errno = errno; 228 if (force) 229 ::chmod(zfilename, st.st_mode); 230 throw file_access_error(filename, strerror(saved_errno)); 231 } 232 233 { 234 auto close_fd = OnScopeEnd([fd]{ close(fd); }); 235 write_buffer_to_fd(buffer, fd, finaleol); 236 if (flags & WriteFlags::Sync) 237 ::fsync(fd); 238 } 239 240 if (replace and geteuid() == 0 and ::chown(temp_filename, st.st_uid, st.st_gid) < 0) 241 throw runtime_error(format("unable to set replacement file ownership: {}", strerror(errno))); 242 if (replace and ::chmod(temp_filename, st.st_mode) < 0) 243 throw runtime_error(format("unable to set replacement file permissions: {}", strerror(errno))); 244 if (force and not replace and ::chmod(zfilename, st.st_mode) < 0) 245 throw runtime_error(format("unable to restore file permissions: {}", strerror(errno))); 246 247 if (replace and rename(temp_filename, zfilename) != 0) 248 { 249 if (force) 250 ::chmod(zfilename, st.st_mode); 251 throw runtime_error("replacing file failed"); 252 } 253 254 if ((buffer.flags() & Buffer::Flags::File) and 255 real_path(filename) == real_path(buffer.filename())) 256 buffer.notify_saved(get_fs_status(real_path(filename))); 257 } 258 259 void write_buffer_to_backup_file(Buffer& buffer) 260 { 261 const int fd = open_temp_file(buffer.filename()); 262 if (fd >= 0) 263 { 264 write_buffer_to_fd(buffer, fd); 265 close(fd); 266 } 267 } 268 269 Buffer* create_fifo_buffer(String name, int fd, Buffer::Flags flags, AutoScroll scroll) 270 { 271 static ValueId fifo_watcher_id = get_free_value_id(); 272 273 auto& buffer_manager = BufferManager::instance(); 274 Buffer* buffer = buffer_manager.get_buffer_ifp(name); 275 if (buffer) 276 { 277 buffer->flags() |= Buffer::Flags::NoUndo | flags; 278 buffer->values().clear(); 279 buffer->reload({StringData::create("\n")}, ByteOrderMark::None, EolFormat::Lf, FinalEol::Present, {InvalidTime, {}, {}}); 280 buffer_manager.make_latest(*buffer); 281 } 282 else 283 buffer = buffer_manager.create_buffer( 284 std::move(name), flags | Buffer::Flags::Fifo | Buffer::Flags::NoUndo, 285 {StringData::create("\n")}, ByteOrderMark::None, EolFormat::Lf, FinalEol::Present, {InvalidTime, {}, {}}); 286 287 struct FifoWatcher : FDWatcher 288 { 289 FifoWatcher(int fd, Buffer& buffer, AutoScroll scroll) 290 : FDWatcher(fd, FdEvents::Read, EventMode::Normal, 291 [](FDWatcher& watcher, FdEvents, EventMode mode) { 292 if (mode == EventMode::Normal) 293 static_cast<FifoWatcher&>(watcher).read_fifo(); 294 }), 295 m_buffer(buffer), m_scroll(scroll) 296 {} 297 298 ~FifoWatcher() 299 { 300 kak_assert(m_buffer.flags() & Buffer::Flags::Fifo); 301 close_fd(); 302 m_buffer.run_hook_in_own_context(Hook::BufCloseFifo, ""); 303 if (not m_buffer.values().contains(fifo_watcher_id)) 304 m_buffer.flags() &= ~(Buffer::Flags::Fifo | Buffer::Flags::NoUndo); 305 } 306 307 void read_fifo() 308 { 309 kak_assert(m_buffer.flags() & Buffer::Flags::Fifo); 310 311 constexpr size_t buffer_size = 2048; 312 // if we read data slower than it arrives in the fifo, limiting the 313 // iteration number allows us to go back go back to the event loop and 314 // handle other events sources (such as input) 315 constexpr size_t max_loop = 1024; 316 bool closed = false; 317 size_t loop = 0; 318 char data[buffer_size]; 319 Optional<BufferCoord> insert_begin; 320 const int fifo = fd(); 321 322 { 323 auto restore_flags = OnScopeEnd([this, flags=m_buffer.flags()] { m_buffer.flags() = flags; }); 324 m_buffer.flags() &= ~Buffer::Flags::ReadOnly; 325 do 326 { 327 328 const ssize_t count = ::read(fifo, data, buffer_size); 329 if (count <= 0) 330 { 331 closed = true; 332 break; 333 } 334 335 auto pos = m_buffer.back_coord(); 336 const bool is_first = pos == BufferCoord{0,0}; 337 if ((m_scroll == AutoScroll::No and (is_first or m_had_trailing_newline)) 338 or (m_scroll == AutoScroll::NotInitially and is_first)) 339 pos = m_buffer.next(pos); 340 341 auto inserted_range = m_buffer.insert(pos, StringView(data, data+count)); 342 if (not insert_begin) 343 insert_begin = inserted_range.begin; 344 pos = inserted_range.end; 345 346 bool have_trailing_newline = (data[count-1] == '\n'); 347 if (m_scroll != AutoScroll::Yes) 348 { 349 if (is_first) 350 { 351 m_buffer.erase({0,0}, m_buffer.next({0,0})); 352 --insert_begin->line; 353 if (m_scroll == AutoScroll::NotInitially and have_trailing_newline) 354 m_buffer.insert(m_buffer.end_coord(), "\n"); 355 } 356 else if (m_scroll == AutoScroll::No and 357 not m_had_trailing_newline and have_trailing_newline) 358 m_buffer.erase(m_buffer.prev(pos), pos); 359 } 360 m_had_trailing_newline = have_trailing_newline; 361 } 362 while (++loop < max_loop and fd_readable(fifo)); 363 } 364 365 if (insert_begin) 366 { 367 auto insert_back = (m_had_trailing_newline and m_scroll == AutoScroll::No) 368 ? m_buffer.back_coord() : m_buffer.prev(m_buffer.back_coord()); 369 m_buffer.run_hook_in_own_context( 370 Hook::BufReadFifo, 371 selection_to_string(ColumnType::Byte, m_buffer, {*insert_begin, insert_back})); 372 } 373 374 if (closed) 375 m_buffer.values().erase(fifo_watcher_id); // will delete this 376 } 377 378 Buffer& m_buffer; 379 AutoScroll m_scroll; 380 bool m_had_trailing_newline = false; 381 }; 382 383 buffer->values()[fifo_watcher_id] = Value(Meta::Type<FifoWatcher>{}, fd, *buffer, scroll); 384 buffer->flags() = flags | Buffer::Flags::Fifo | Buffer::Flags::NoUndo; 385 buffer->run_hook_in_own_context(Hook::BufOpenFifo, buffer->name()); 386 387 return buffer; 388 } 389 390 auto to_string(Buffer::HistoryId id) 391 { 392 using Result = decltype(to_string(size_t{})); 393 if (id == Buffer::HistoryId::Invalid) 394 return Result{1, "-"}; 395 return to_string(static_cast<size_t>(id)); 396 } 397 398 static String modification_as_string(const Buffer::Modification& modification) 399 { 400 return format("{}{}.{}|{}", 401 modification.type == Buffer::Modification::Type::Insert ? '+' : '-', 402 modification.coord.line, modification.coord.column, 403 modification.content->strview()); 404 } 405 406 Vector<String> history_as_strings(ConstArrayView<Buffer::HistoryNode> history) 407 { 408 Vector<String> res; 409 for (auto& node : history) 410 { 411 auto seconds = std::chrono::duration_cast<std::chrono::seconds>(node.committed.time_since_epoch()); 412 res.push_back(to_string(node.parent)); 413 res.push_back(to_string(seconds.count())); 414 res.push_back(to_string(node.redo_child)); 415 for (auto& modification : node.undo_group) 416 res.push_back(modification_as_string(modification)); 417 }; 418 return res; 419 } 420 421 Vector<String> undo_group_as_strings(const Buffer::UndoGroup& undo_group) 422 { 423 Vector<String> res; 424 for (auto& modification : undo_group) 425 res.push_back(modification_as_string(modification)); 426 return res; 427 } 428 429 String generate_buffer_name(StringView pattern) 430 { 431 auto& buffer_manager = BufferManager::instance(); 432 for (int i = 0; true; ++i) 433 { 434 String name = format(pattern, i); 435 if (buffer_manager.get_buffer_ifp(name) == nullptr) 436 return name; 437 } 438 } 439 440 }