VimEditingEngine.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright (c) 2021, the SerenityOS developers.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <LibGUI/EditingEngine.h>
  28. namespace GUI {
  29. class VimEditingEngine final : public EditingEngine {
  30. public:
  31. virtual CursorWidth cursor_width() const override;
  32. virtual bool on_key(const KeyEvent& event) override;
  33. private:
  34. enum VimMode {
  35. Normal,
  36. Insert,
  37. Visual
  38. };
  39. enum YankType {
  40. Line,
  41. Selection
  42. };
  43. VimMode m_vim_mode { VimMode::Normal };
  44. YankType m_yank_type {};
  45. String m_yank_buffer {};
  46. void yank(YankType);
  47. void yank(TextRange);
  48. void put(const GUI::KeyEvent&);
  49. TextPosition m_selection_start_position {};
  50. void update_selection_on_cursor_move();
  51. void clear_visual_mode_data();
  52. KeyCode m_previous_key {};
  53. void switch_to_normal_mode();
  54. void switch_to_insert_mode();
  55. void switch_to_visual_mode();
  56. void move_half_page_up(const KeyEvent& event);
  57. void move_half_page_down(const KeyEvent& event);
  58. bool on_key_in_insert_mode(const KeyEvent& event);
  59. bool on_key_in_normal_mode(const KeyEvent& event);
  60. bool on_key_in_visual_mode(const KeyEvent& event);
  61. };
  62. }