diff --git a/editor/editor.hpp b/editor/editor.hpp index 385dc0b..d5922cd 100644 --- a/editor/editor.hpp +++ b/editor/editor.hpp @@ -101,6 +101,7 @@ struct buffer { inline constexpr void cursor_scan_word_backward(); inline constexpr bool enter(); + inline constexpr void line_kill(); inline constexpr void mark_set(); inline constexpr selection mark_get(); inline constexpr void delete_from_line(line *& l, @@ -180,7 +181,8 @@ inline constexpr void buffer::deallocate(line *& l) template inline constexpr void buffer::decref(line *& l) { - if (l->refcount == 1) + if (l == nullptr) return; + else if (l->refcount == 1) buffer::deallocate(l); else { l->refcount--; @@ -585,6 +587,27 @@ inline constexpr bool buffer::enter() return true; } +template +inline constexpr void buffer::line_kill() +{ + editor::cursor& cur = this->cursor; + + if (line_length(this->lines[cur.row]) == 0) { + decref(this->lines[cur.row]); + + // shift all lines up by one + int32_t n_lines = this->length - (cur.row + 1); + move(&this->lines[cur.row], + &this->lines[cur.row+1], + (sizeof (line*)) * n_lines); + this->length--; + this->lines[this->length] = nullptr; + } else { + line * l = mutref(this->lines[cur.row]); + l->length = cur.col; + } +} + template inline constexpr void buffer::mark_set() { @@ -719,8 +742,7 @@ template inline constexpr void buffer::shadow_clear() { for (int32_t i = 0; i < this->shadow.length; i++) - if (this->shadow.lines[i] != nullptr) - decref(this->shadow.lines[i]); + decref(this->shadow.lines[i]); this->shadow.length = 1; } @@ -815,7 +837,7 @@ inline constexpr void buffer::overwrite_line(line *& dst, //else do nothing } else if (dst_col == 0 && src_col == 0 && line_length(src) >= line_length(dst)) { - if (dst != nullptr) decref(dst); + decref(dst); dst = incref(src); } else { line * dstmut = mutref(dst); diff --git a/editor/main_saturn.cpp b/editor/main_saturn.cpp index 12cf647..aae7f71 100644 --- a/editor/main_saturn.cpp +++ b/editor/main_saturn.cpp @@ -139,6 +139,7 @@ inline void keyboard_regular_key(const enum keysym k) case keysym::E : buffer.cursor_end(); break; case keysym::Y : buffer.shadow_paste(); break; case keysym::W : buffer.shadow_cut(); break; + case keysym::K : buffer.line_kill(); break; default: break; } break;