summaryrefslogtreecommitdiff
path: root/src/debug.cc
diff options
context:
space:
mode:
authorEnrico Borba <enricozb@users.noreply.github.com>2024-12-23 09:23:58 +0100
committerGitHub <noreply@github.com>2024-12-23 09:23:58 +0100
commit52125e6336d596aebdd4da91080b3178ddca7449 (patch)
tree27d3e5c01660d567f22fee621c97753f294256b0 /src/debug.cc
parent14cb35f62b36b2f1aa530adb5e31c05ff1347bfc (diff)
parent9c458c50661446fc6e7295787b06422137af099d (diff)
Merge branch 'master' into enricozb/daemon-stdin
Diffstat (limited to 'src/debug.cc')
-rw-r--r--src/debug.cc39
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);
+ }
+}
+
+}