kill_ring_manager.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #include "lumacs/kill_ring_manager.hpp"
  2. #include <iostream> // For debug output, consider replacing with logging
  3. namespace lumacs {
  4. KillRingManager::KillRingManager(size_t max_size) : max_size_(max_size) {
  5. if (max_size_ == 0) {
  6. max_size_ = 1; // Ensure minimum size
  7. }
  8. }
  9. void KillRingManager::push(const std::string& text) {
  10. if (text.empty()) {
  11. return;
  12. }
  13. if (!ring_.empty() && last_action_was_kill_) {
  14. // If the last action was a kill, append to the last entry
  15. ring_.front().append(text);
  16. } else {
  17. // Otherwise, create a new entry
  18. ring_.push_front(text);
  19. if (ring_.size() > max_size_) {
  20. ring_.pop_back(); // Remove oldest entry if max size exceeded
  21. }
  22. }
  23. last_action_was_kill_ = true;
  24. }
  25. std::string KillRingManager::current() const {
  26. if (ring_.empty()) {
  27. return "";
  28. }
  29. return ring_.front();
  30. }
  31. std::string KillRingManager::previous() {
  32. if (ring_.empty()) {
  33. return "";
  34. }
  35. // Rotate the ring: move back to front (conceptual 'previous')
  36. std::string back_item = ring_.back();
  37. ring_.pop_back();
  38. ring_.push_front(back_item);
  39. last_action_was_kill_ = false; // Reset state for push logic
  40. return ring_.front();
  41. }
  42. void KillRingManager::set_yank_range(Position start, Position end) {
  43. last_yank_start_ = start;
  44. last_yank_end_ = end;
  45. }
  46. } // namespace lumacs