diff options
Diffstat (limited to 'src/debug.cc')
| -rw-r--r-- | src/debug.cc | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/src/debug.cc b/src/debug.cc new file mode 100644 index 00000000..55f7e4d3 --- /dev/null +++ b/src/debug.cc @@ -0,0 +1,39 @@ +#include "debug.hh" + +#include "buffer_manager.hh" +#include "buffer_utils.hh" +#include "utils.hh" + +namespace Kakoune +{ + +void write_to_debug_buffer(StringView str) +{ + if (not BufferManager::has_instance()) + { + write(2, str); + write(2, "\n"); + return; + } + + constexpr StringView debug_buffer_name = "*debug*"; + // Try to ensure we keep an empty line at the end of the debug buffer + // where the user can put its cursor to scroll with new messages + const bool eol_back = not str.empty() and str.back() == '\n'; + if (Buffer* buffer = BufferManager::instance().get_buffer_ifp(debug_buffer_name)) + { + buffer->flags() &= ~Buffer::Flags::ReadOnly; + auto restore = on_scope_end([buffer] { buffer->flags() |= Buffer::Flags::ReadOnly; }); + + buffer->insert(buffer->back_coord(), eol_back ? str : str + "\n"); + } + else + { + String line = str + (eol_back ? "\n" : "\n\n"); + create_buffer_from_string( + debug_buffer_name.str(), Buffer::Flags::NoUndo | Buffer::Flags::Debug | Buffer::Flags::ReadOnly, + line); + } +} + +} |
