kill_ring_manager.hpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #pragma once
  2. #include <string>
  3. #include <deque>
  4. #include <optional>
  5. #include "lumacs/buffer.hpp" // For Position and Range
  6. namespace lumacs {
  7. /// @brief Implements an Emacs-like kill ring (clipboard history).
  8. /// The kill ring stores killed (cut) text in a circular buffer.
  9. class KillRingManager {
  10. public:
  11. explicit KillRingManager(size_t max_size = 60);
  12. /// @brief Pushes text onto the kill ring. If the last entry was also a kill
  13. /// (not a yank), it appends to that entry. Otherwise, it creates a new entry.
  14. /// @param text The text to push.
  15. void push(const std::string& text);
  16. /// @brief Retrieves the current (most recently pushed) entry in the kill ring.
  17. /// @return The current entry. Returns empty string if ring is empty.
  18. [[nodiscard]] std::string current() const;
  19. /// @brief Rotates the kill ring and retrieves the previous entry.
  20. /// Used for `yank-pop`.
  21. /// @return The previous entry. Returns empty string if ring is empty.
  22. [[nodiscard]] std::string previous();
  23. /// @brief Checks if the kill ring is empty.
  24. [[nodiscard]] bool empty() const { return ring_.empty(); }
  25. /// @brief Clears the kill ring.
  26. void clear() { ring_.clear(); }
  27. private:
  28. std::deque<std::string> ring_;
  29. size_t max_size_;
  30. bool last_action_was_kill_ = false; // Tracks if the last push was a kill or append
  31. };
  32. } // namespace lumacs