vimamsa 0.1.26 → 0.1.28

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (229) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/appimage.yml +93 -0
  3. data/.gitignore +4 -1
  4. data/Dockerfile +7 -4
  5. data/README.md +195 -99
  6. data/custom_example.rb +59 -17
  7. data/doc/TODO.md +678 -0
  8. data/doc/action_names_plan.md +442 -0
  9. data/doc/bundling_plan.md +570 -0
  10. data/doc/delta_syntax_plan.md +293 -0
  11. data/doc/demo_recording_plan.md +288 -0
  12. data/doc/design_goals.md +27 -0
  13. data/doc/intro_page_plan.md +266 -0
  14. data/doc/key_bindings_report_plan.md +213 -0
  15. data/doc/line_tree_plan.md +89 -0
  16. data/doc/missing_vim_bindings.md +61 -0
  17. data/doc/modules_settings_plan.md +433 -0
  18. data/doc/refactor_dedup_plan.md +117 -0
  19. data/doc/registries_plan.md +202 -0
  20. data/doc/startup_speed_plan.md +97 -0
  21. data/doc/treap_visualizer.html +585 -0
  22. data/doc/unused_code_plan.md +120 -0
  23. data/doc/vim_compatibility_plan.md +451 -0
  24. data/doc/vimlike_vim_conflicts.md +262 -0
  25. data/doc/vm_snapshots_plan.md +146 -0
  26. data/doc/wysiwyg_plan.md +231 -0
  27. data/exe/run_tests.rb +124 -3
  28. data/exe/vimamsa +68 -5
  29. data/fonts/OFL-Oxanium.txt +93 -0
  30. data/fonts/Oxanium[wght].ttf +0 -0
  31. data/help/intro.txt +30 -0
  32. data/help/vim_intro.txt +31 -0
  33. data/icon.png +0 -0
  34. data/img/demo.gif +0 -0
  35. data/img/demo.mp4 +0 -0
  36. data/lang/hyperplaintext.lang +7 -1
  37. data/lib/vimamsa/ack.rb +13 -1
  38. data/lib/vimamsa/actions.rb +385 -37
  39. data/lib/vimamsa/appimage_env.rb +35 -0
  40. data/lib/vimamsa/audio.rb +2 -5
  41. data/lib/vimamsa/bookmarks.rb +302 -0
  42. data/lib/vimamsa/buffer.rb +675 -118
  43. data/lib/vimamsa/buffer_changetext.rb +150 -68
  44. data/lib/vimamsa/buffer_cursor.rb +7 -0
  45. data/lib/vimamsa/buffer_list.rb +6 -2
  46. data/lib/vimamsa/buffer_manager.rb +10 -7
  47. data/lib/vimamsa/bundled_fonts.rb +32 -0
  48. data/lib/vimamsa/cli_help.rb +75 -0
  49. data/lib/vimamsa/clipboard.rb +9 -3
  50. data/lib/vimamsa/conf.rb +68 -3
  51. data/lib/vimamsa/constants.rb +3 -0
  52. data/lib/vimamsa/crash_handler.rb +149 -0
  53. data/lib/vimamsa/debug.rb +6 -3
  54. data/lib/vimamsa/demo_player.rb +458 -0
  55. data/lib/vimamsa/demo_recorder.rb +277 -0
  56. data/lib/vimamsa/desktop_install.rb +96 -0
  57. data/lib/vimamsa/diff_buffer.rb +40 -11
  58. data/lib/vimamsa/dot_repeat.rb +104 -0
  59. data/lib/vimamsa/easy_jump.rb +57 -3
  60. data/lib/vimamsa/editor.rb +327 -78
  61. data/lib/vimamsa/encrypt.rb +78 -29
  62. data/lib/vimamsa/file_finder.rb +4 -19
  63. data/lib/vimamsa/file_history.rb +4 -1
  64. data/lib/vimamsa/file_manager.rb +46 -31
  65. data/lib/vimamsa/fold.rb +226 -0
  66. data/lib/vimamsa/form_input.rb +388 -0
  67. data/lib/vimamsa/gui.rb +603 -119
  68. data/lib/vimamsa/gui_command_line.rb +247 -0
  69. data/lib/vimamsa/gui_dialog.rb +94 -42
  70. data/lib/vimamsa/gui_file_panel.rb +356 -25
  71. data/lib/vimamsa/gui_form_generator.rb +75 -40
  72. data/lib/vimamsa/gui_func_panel.rb +67 -22
  73. data/lib/vimamsa/gui_image.rb +3 -0
  74. data/lib/vimamsa/gui_key_hint.rb +165 -0
  75. data/lib/vimamsa/gui_keylog_panel.rb +408 -0
  76. data/lib/vimamsa/gui_menu.rb +84 -53
  77. data/lib/vimamsa/gui_select_window.rb +80 -75
  78. data/lib/vimamsa/gui_settings.rb +86 -41
  79. data/lib/vimamsa/gui_sourceview.rb +280 -92
  80. data/lib/vimamsa/gui_sourceview_autocomplete.rb +319 -90
  81. data/lib/vimamsa/gui_text.rb +63 -2
  82. data/lib/vimamsa/gui_theme.rb +615 -0
  83. data/lib/vimamsa/hook.rb +5 -0
  84. data/lib/vimamsa/hyper_plain_text.rb +3 -0
  85. data/lib/vimamsa/intro_page.rb +368 -0
  86. data/lib/vimamsa/key_actions.rb +382 -193
  87. data/lib/vimamsa/key_binding_tree.rb +467 -273
  88. data/lib/vimamsa/key_bindings_notepad.rb +45 -0
  89. data/lib/vimamsa/key_bindings_report.rb +484 -0
  90. data/lib/vimamsa/key_bindings_vim.rb +104 -0
  91. data/lib/vimamsa/key_bindings_vimlike.rb +353 -289
  92. data/lib/vimamsa/key_names.rb +80 -0
  93. data/lib/vimamsa/langservp.rb +271 -54
  94. data/lib/vimamsa/line_tree.rb +367 -0
  95. data/lib/vimamsa/macro.rb +23 -3
  96. data/lib/vimamsa/main.rb +14 -8
  97. data/lib/vimamsa/rbvma.rb +41 -1
  98. data/lib/vimamsa/registers.rb +125 -0
  99. data/lib/vimamsa/run_ruby.rb +64 -0
  100. data/lib/vimamsa/search.rb +3 -0
  101. data/lib/vimamsa/search_replace.rb +15 -14
  102. data/lib/vimamsa/string_util.rb +134 -40
  103. data/lib/vimamsa/terminal.rb +3 -0
  104. data/lib/vimamsa/test_framework.rb +28 -2
  105. data/lib/vimamsa/tests.rb +11 -8
  106. data/lib/vimamsa/text_objects.rb +310 -0
  107. data/lib/vimamsa/text_transforms.rb +18 -0
  108. data/lib/vimamsa/tmp_user_dir.rb +39 -0
  109. data/lib/vimamsa/user_dirs.rb +83 -0
  110. data/lib/vimamsa/util.rb +46 -7
  111. data/lib/vimamsa/version.rb +1 -1
  112. data/lib/vimamsa/vim_core.rb +355 -0
  113. data/lib/vimamsa/vim_operators.rb +365 -0
  114. data/lib/vimamsa.rb +0 -1
  115. data/modules/calculator/calculator.rb +5 -5
  116. data/modules/color_highlight/color_highlight.rb +118 -0
  117. data/modules/color_highlight/color_highlight_info.rb +3 -0
  118. data/modules/dictation/dictation.rb +1054 -0
  119. data/modules/dictation/dictation_info.rb +3 -0
  120. data/modules/dictation/whisper_worker.py +273 -0
  121. data/modules/ollama/ollama.rb +107 -0
  122. data/modules/ollama/ollama_info.rb +3 -0
  123. data/modules/terminal/terminal.rb +5 -5
  124. data/modules/tts/tts.rb +119 -0
  125. data/modules/tts/tts_info.rb +3 -0
  126. data/run_e2e_vm.sh +28 -0
  127. data/run_tests_vm.rb +138 -0
  128. data/scripts/appimage/AppRun +54 -0
  129. data/scripts/appimage/Dockerfile +18 -0
  130. data/scripts/appimage/build.sh +19 -0
  131. data/scripts/appimage/build_in_container.sh +192 -0
  132. data/scripts/appimage/smoke_test.sh +27 -0
  133. data/scripts/demo/demo_easyjump_steps.txt +60 -0
  134. data/scripts/demo/demo_files_steps.txt +52 -0
  135. data/scripts/demo/demo_steps.txt +67 -0
  136. data/scripts/demo/notes.txt +6 -0
  137. data/scripts/demo/record_demo.rb +156 -0
  138. data/scripts/demo/report.rb +7 -0
  139. data/scripts/demo/shapes.rb +29 -0
  140. data/scripts/demo/square/shapes.rb +29 -0
  141. data/scripts/demo/square/steps.txt +185 -0
  142. data/scripts/lib/perf_startup.rb +138 -0
  143. data/scripts/lib/script_helpers.rb +53 -0
  144. data/scripts/perf_compare.rb +34 -0
  145. data/scripts/profile_startup.rb +72 -0
  146. data/styles/134272-molokai.xml +3 -0
  147. data/styles/molokai_cyber.xml +48 -0
  148. data/styles/molokai_edit.xml +1 -0
  149. data/tests/support/virtual_keyboard.rb +254 -0
  150. data/tests/support/virtual_mic.rb +67 -0
  151. data/tests/test_ack.rb +22 -0
  152. data/tests/test_action_ids.rb +232 -0
  153. data/tests/test_auto_format.rb +122 -0
  154. data/tests/test_autocomplete.rb +211 -0
  155. data/tests/test_autosave.rb +145 -0
  156. data/tests/test_basic_editing.rb +46 -16
  157. data/tests/test_bookmarks.rb +206 -0
  158. data/tests/test_bracket_match.rb +88 -0
  159. data/tests/test_bundling.rb +92 -0
  160. data/tests/test_cli_help.rb +64 -0
  161. data/tests/test_color_highlight.rb +92 -0
  162. data/tests/test_command_line.rb +79 -0
  163. data/tests/test_command_line_file.rb +27 -0
  164. data/tests/test_context_menu_file.rb +147 -0
  165. data/tests/test_copy_paste.rb +106 -26
  166. data/tests/test_crash_handler.rb +39 -0
  167. data/tests/test_custom_two_phase.rb +92 -0
  168. data/tests/test_demo_player.rb +301 -0
  169. data/tests/test_demo_recorder.rb +202 -0
  170. data/tests/test_dialog_callback_errors.rb +44 -0
  171. data/tests/test_dictation.rb +294 -0
  172. data/tests/test_diff_buffer_name.rb +51 -0
  173. data/tests/test_dot_repeat.rb +88 -0
  174. data/tests/test_double_click.rb +41 -0
  175. data/tests/test_drag_selection.rb +125 -0
  176. data/tests/test_e2e_cmdline.rb +116 -0
  177. data/tests/test_e2e_dictation.rb +98 -0
  178. data/tests/test_e2e_replace_char.rb +58 -0
  179. data/tests/test_easy_jump.rb +93 -0
  180. data/tests/test_extract_file_links.rb +55 -0
  181. data/tests/test_file_format.rb +217 -0
  182. data/tests/test_file_manager.rb +72 -0
  183. data/tests/test_file_panel.rb +269 -0
  184. data/tests/test_flash.rb +78 -0
  185. data/tests/test_fold.rb +111 -0
  186. data/tests/test_font_refresh.rb +56 -0
  187. data/tests/test_form_input.rb +203 -0
  188. data/tests/test_func_panel.rb +68 -0
  189. data/tests/test_grep_apply.rb +46 -0
  190. data/tests/test_gtk_sync.rb +126 -0
  191. data/tests/test_hpt_highlight_visible.rb +75 -0
  192. data/tests/test_hpt_link_highlight.rb +82 -0
  193. data/tests/test_intro_page.rb +437 -0
  194. data/tests/test_jump_to_file.rb +41 -0
  195. data/tests/test_key_bindings.rb +208 -0
  196. data/tests/test_key_categories.rb +163 -0
  197. data/tests/test_keylog_panel.rb +261 -0
  198. data/tests/test_line_ends.rb +51 -0
  199. data/tests/test_line_tree.rb +86 -0
  200. data/tests/test_lsp_clangd.rb +281 -0
  201. data/tests/test_lsp_jump.rb +87 -0
  202. data/tests/test_lsp_position_encoding.rb +49 -0
  203. data/tests/test_lsp_symbols.rb +68 -0
  204. data/tests/test_macro.rb +137 -0
  205. data/tests/test_markdown_highlight.rb +57 -0
  206. data/tests/test_mode_badge.rb +41 -0
  207. data/tests/test_module_interface.rb +9 -9
  208. data/tests/test_notepad_bindings.rb +67 -0
  209. data/tests/test_perf_startup.rb +28 -0
  210. data/tests/test_popup_queue.rb +51 -0
  211. data/tests/test_random_edit.rb +109 -0
  212. data/tests/test_registers.rb +122 -0
  213. data/tests/test_revert.rb +127 -0
  214. data/tests/test_run_ruby.rb +77 -0
  215. data/tests/test_startup_file.rb +58 -0
  216. data/tests/test_status_area.rb +50 -0
  217. data/tests/test_text_objects.rb +163 -0
  218. data/tests/test_text_transforms.rb +341 -0
  219. data/tests/test_tmp_user_dir.rb +79 -0
  220. data/tests/test_tts.rb +68 -0
  221. data/tests/test_undo.rb +34 -34
  222. data/tests/test_user_dirs.rb +122 -0
  223. data/tests/test_vim_bindings.rb +353 -0
  224. data/tests/test_vim_conflicts.rb +100 -0
  225. data/tests/test_vim_operators.rb +116 -0
  226. data/tests/test_window_layout.rb +302 -0
  227. data/vimamsa.gemspec +3 -1
  228. data/vm_guest_run.sh +129 -0
  229. metadata +167 -10
@@ -0,0 +1,365 @@
1
+ module Vimamsa
2
+ # Vim operators and motions (doc/vim_compatibility_plan.md, step 1).
3
+ #
4
+ # The keys are key sets (KeyBindingTree#define_key_set), combined by templates
5
+ # in VIM_BINDINGS ("C <op> <motion>", ...), so every operator works with every
6
+ # motion and text object. This file has the data (VIM_KEY_SETS), what each
7
+ # motion and operator means (VIM_MOTIONS, VIM_OPERATORS), and the Buffer
8
+ # methods that compute motion targets and apply operators to a range.
9
+ #
10
+ # Vim rules followed (vim :help exclusive, :help cw, :help dw):
11
+ # - A motion is charwise or linewise, and a charwise motion is inclusive (its
12
+ # target character is included: e, f, t, $) or exclusive (w, b, h, l, 0, ^).
13
+ # - An exclusive motion that ends in column 0 of a later line ends at the end
14
+ # of the line before instead. If it started at or before the first non-blank
15
+ # of its line, it becomes linewise.
16
+ # - With an operator, w stops at the end of the line of the last word moved
17
+ # over (dw on the last word of a line doesn't join lines).
18
+ # - cw on a non-blank works like ce.
19
+ # - Counts before the operator and before the motion multiply (2d3w).
20
+
21
+ # Motion id => properties. desc is used on the key bindings page.
22
+ VIM_MOTIONS = {
23
+ char_left: { desc: "character left" },
24
+ char_right: { desc: "character right" },
25
+ line_down: { desc: "line down", linewise: true },
26
+ line_up: { desc: "line up", linewise: true },
27
+ word_next: { desc: "next word start" },
28
+ word_prev: { desc: "previous word start" },
29
+ word_end: { desc: "word end", inclusive: true },
30
+ bigword_next: { desc: "next WORD start" },
31
+ bigword_prev: { desc: "previous WORD start" },
32
+ bigword_end: { desc: "WORD end", inclusive: true },
33
+ line_start: { desc: "line start" },
34
+ line_first_char: { desc: "first non-blank" },
35
+ line_end: { desc: "line end", inclusive: true },
36
+ buf_start: { desc: "first line (<count>: that line)", linewise: true },
37
+ buf_end: { desc: "last line (<count>: that line)", linewise: true },
38
+ find_char: { desc: "to <char>", inclusive: true },
39
+ till_char: { desc: "till <char>", inclusive: true },
40
+ find_char_back: { desc: "back to <char>" },
41
+ till_char_back: { desc: "back till <char>" },
42
+ mark_line: { desc: "line of mark <char>", linewise: true },
43
+ mark: { desc: "mark <char>" },
44
+ }
45
+
46
+ # Operator id => description
47
+ VIM_OPERATORS = {
48
+ delete: "delete", change: "change", yank: "copy",
49
+ upcase: "uppercase", downcase: "lowercase", swapcase: "swap case",
50
+ indent: "indent", unindent: "unindent",
51
+ }
52
+
53
+ VIM_TEXTOBJ_DESC = { inner: "inside", around: "around" }
54
+
55
+ # The key sets of the vim keymap. vimlike uses the same sets, plus its own
56
+ # additions (key_set_add before bind_vim_bindings), and gets each
57
+ # combination whose keys it doesn't use for something else.
58
+ VIM_KEY_SETS = {
59
+ op: {
60
+ "d" => :delete, "c" => :change, "y" => :yank,
61
+ "g U" => :upcase, "g u" => :downcase, "g ~" => :swapcase,
62
+ ">" => :indent, "<" => :unindent,
63
+ },
64
+ motion: {
65
+ "h" => :char_left, "l" => :char_right, "j" => :line_down, "k" => :line_up,
66
+ "w" => :word_next, "b" => :word_prev, "e" => :word_end,
67
+ "W" => :bigword_next, "B" => :bigword_prev, "E" => :bigword_end,
68
+ "0" => :line_start, "^" => :line_first_char, "$" => :line_end,
69
+ "g g" => :buf_start, "G" => :buf_end,
70
+ "f <char>" => :find_char, "t <char>" => :till_char,
71
+ "F <char>" => :find_char_back, "T <char>" => :till_char_back,
72
+ "f space" => { id: :find_char, char: " " }, "t space" => { id: :till_char, char: " " },
73
+ "F space" => { id: :find_char_back, char: " " }, "T space" => { id: :till_char_back, char: " " },
74
+ "' <char>" => :mark_line, "` <char>" => :mark,
75
+ },
76
+ # The object is the typed <char>: w W s p " ' ` ( ) b [ ] { } B < > t
77
+ textobj: { "i <char>" => :inner, "a <char>" => :around },
78
+ }
79
+
80
+ # Text for a key set value on the key bindings page
81
+ def key_set_value_desc(set, value)
82
+ case set
83
+ when :op then VIM_OPERATORS[value]
84
+ when :motion then VIM_MOTIONS.dig(value, :desc)
85
+ when :textobj then VIM_TEXTOBJ_DESC[value]
86
+ end || value.to_s
87
+ end
88
+
89
+ class Buffer < String
90
+ # The position motion moves to from pos, or nil if it can't move.
91
+ # count: the typed count, nil if none.
92
+ def vim_motion_target(motion, count, char, pos = @pos)
93
+ n = count || 1
94
+ ls, le = tobj_line_bounds(pos)
95
+ case motion
96
+ when :char_left
97
+ return nil if pos <= ls
98
+ [pos - n, ls].max
99
+ when :char_right
100
+ return nil if pos >= le
101
+ [pos + n, le].min
102
+ when :line_down, :line_up
103
+ l = get_line_pos(pos) + (motion == :line_down ? n : -n)
104
+ return nil if l < 0 || l >= @line_ends.size
105
+ r = line_range(l, 1)
106
+ [r.begin + (pos - ls), r.end].min
107
+ when :word_next, :bigword_next
108
+ big = motion == :bigword_next
109
+ t = pos
110
+ n.times { t = vm_word_next(t, big) }
111
+ t == pos ? nil : t
112
+ when :word_end, :bigword_end
113
+ t = pos
114
+ n.times { t = vm_word_end(t, motion == :bigword_end) || (return nil) }
115
+ t
116
+ when :word_prev, :bigword_prev
117
+ t = pos
118
+ n.times { t = vm_word_prev(t, motion == :bigword_prev) || (return nil) }
119
+ t
120
+ when :line_start then ls
121
+ when :line_first_char then vm_first_non_blank(ls)
122
+ when :line_end
123
+ l = [get_line_pos(pos) + n - 1, @line_ends.size - 1].min
124
+ r = line_range(l, 1)
125
+ r.end > r.begin ? r.end - 1 : r.begin
126
+ when :buf_start, :buf_end
127
+ l = count ? count - 1 : (motion == :buf_start ? 0 : @line_ends.size - 1)
128
+ l = l.clamp(0, @line_ends.size - 1)
129
+ vm_first_non_blank(line_range(l, 1).begin)
130
+ when :find_char, :till_char
131
+ return nil if char.nil?
132
+ t = pos
133
+ n.times { t = index(char, t + 1); return nil if t.nil? || t >= le }
134
+ motion == :till_char ? t - 1 : t
135
+ when :find_char_back, :till_char_back
136
+ return nil if char.nil? || pos == 0
137
+ t = pos
138
+ n.times { t = t > 0 ? rindex(char, t - 1) : nil; return nil if t.nil? || t < ls }
139
+ motion == :till_char_back ? t + 1 : t
140
+ when :mark_line, :mark
141
+ t = @marks[char]
142
+ return nil if t.nil?
143
+ t = [t, size - 1].min
144
+ motion == :mark_line ? vm_first_non_blank(tobj_line_bounds(t)[0]) : t
145
+ end
146
+ end
147
+
148
+ # Plain cursor movement and Visual mode extension (the selection follows
149
+ # the cursor)
150
+ def vim_motion_move(motion, count, char)
151
+ t = vim_motion_target(motion, count, char)
152
+ return false if t.nil?
153
+ tobj_set_normal_pos(t)
154
+ true
155
+ end
156
+
157
+ # d w, c e, y $, g U f x, > j, ...
158
+ def vim_operator_motion(op, motion, count, char)
159
+ m = VIM_MOTIONS[motion]
160
+ return false if m.nil?
161
+ p = @pos
162
+ linewise = m[:linewise] || false
163
+ inclusive = m[:inclusive] || false
164
+ word = [:word_next, :bigword_next].include?(motion)
165
+ if op == :change && word && !" \t\n".include?(self[p].to_s)
166
+ # cw = ce: change to the end of the word, not including the blanks after
167
+ big = motion == :bigword_next
168
+ t = p
169
+ k = count || 1
170
+ k -= 1 if vm_word_end_at?(p, big)
171
+ k.times { t = vm_word_end(t, big) || t }
172
+ inclusive = true
173
+ else
174
+ t = vim_motion_target(motion, count, char, p)
175
+ end
176
+ return false if t.nil?
177
+
178
+ s, e = [p, t].minmax
179
+ if !linewise
180
+ e += 1 if inclusive && self[e] != "\n"
181
+ if word && !inclusive && t > p && get_line_pos(t) > get_line_pos(p) &&
182
+ self[tobj_line_bounds(t)[0]...t].match?(/\A[ \t]*\z/)
183
+ # dw on the last word of a line: stop at the end of that line
184
+ e = [tobj_line_bounds(t)[0] - 1, s].max
185
+ elsif !inclusive && e > s && e < size && (e == 0 || self[e - 1] == "\n") &&
186
+ get_line_pos(e) > get_line_pos(s)
187
+ e -= 1 # end of the line before (its "\n" is not included)
188
+ # (e is now that line's "\n", so the linewise range below ends there)
189
+ linewise = true if s <= vm_first_non_blank(tobj_line_bounds(s)[0])
190
+ end
191
+ end
192
+ # A charwise delete over several lines that starts in the indent and ends
193
+ # at the end of a line deletes whole lines (vim op_delete): d2$ from col 0
194
+ if !linewise && op == :delete && e > s && get_line_pos([e - 1, s].max) > get_line_pos(s)
195
+ le = tobj_line_bounds(e - 1)[1]
196
+ if self[e...le].to_s.match?(/\A[ \t]*\z/) && s <= vm_first_non_blank(tobj_line_bounds(s)[0])
197
+ linewise = true
198
+ end
199
+ end
200
+ # > and < always work on whole lines
201
+ linewise = true if [:indent, :unindent].include?(op)
202
+ if linewise
203
+ s = tobj_line_bounds(s)[0]
204
+ e = tobj_line_bounds(e)[1] + 1
205
+ end
206
+ return false if e <= s
207
+ vim_apply_operator(op, s, e, linewise, from: p)
208
+ end
209
+
210
+ # d d, c c, y y, > >, g U U: count lines from the current one. Like vim,
211
+ # nothing happens when there are fewer lines left than the count.
212
+ def vim_operator_lines(op, count)
213
+ l = get_line_pos(@pos)
214
+ l2 = l + (count || 1) - 1
215
+ return false if l2 >= @line_ends.size
216
+ s = line_range(l, 1).begin
217
+ e = line_range(l2, 1).end + 1
218
+ vim_apply_operator(op, s, e, true, from: @pos)
219
+ end
220
+
221
+ # Apply op to [s, e) (e exclusive). from: the cursor before the command.
222
+ # Shared with the text objects (text_object_op).
223
+ def vim_apply_operator(op, s, e, linewise, from: @pos)
224
+ e = [e, size].min
225
+ case op
226
+ when :yank
227
+ vma.clipboard.set(self[s...e], linewise: linewise) if e > s
228
+ # The cursor goes to the start of the text, but a linewise yank that
229
+ # starts on the cursor line doesn't move it (y j, y y)
230
+ if !(linewise && tobj_line_bounds(from)[0] == s)
231
+ linewise ? tobj_set_line_pos(s) : set_pos(s)
232
+ end
233
+ when :delete
234
+ return false if e <= s
235
+ txt = self[s...e]
236
+ ds, de = s, e - 1
237
+ # The buffer's final "\n" can't be deleted: for the last lines, take
238
+ # the newline before them instead (vim dap / dd on the last line).
239
+ ds, de = s - 1, e - 2 if linewise && e >= size && s > 0
240
+ Gui.flash_range(self, ds..de) do
241
+ delete_range(ds, de, register: false)
242
+ vma.clipboard.set(txt, linewise: linewise, kind: :delete)
243
+ linewise ? tobj_set_line_pos(ds) : tobj_set_normal_pos(s)
244
+ end
245
+ when :change
246
+ vma.clipboard.set(self[s...e], linewise: linewise, kind: :delete) if e > s
247
+ # Linewise (cc, cip, multi-line ci{): leave an empty line to type on
248
+ if linewise && e >= size && e - 1 > s
249
+ delete_range(s, e - 2, register: false) # the final "\n" stays as that line
250
+ else
251
+ delete_range(s, e - 1, register: false) if e > s
252
+ insert_txt_at("\n", s) if linewise
253
+ end
254
+ set_pos(s)
255
+ vma.kbd.set_mode(:insert)
256
+ when :upcase, :downcase, :swapcase
257
+ return false if e <= s
258
+ old = self[s...e]
259
+ txt = transform_text(old.dup, op)
260
+ replace_range(s..(e - 1), txt) if txt != old
261
+ set_pos(s)
262
+ when :indent, :unindent
263
+ ls = tobj_line_bounds(s)[0]
264
+ le = tobj_line_bounds([e - 1, s].max)[1]
265
+ old = self[ls..le]
266
+ tw = cnf.tab.width!
267
+ new = old.lines.map { |line|
268
+ if op == :indent
269
+ line == "\n" ? line : vm_indent_pad + line
270
+ else
271
+ line.sub(/\A(\t| {1,#{tw}})/, "")
272
+ end
273
+ }.join
274
+ replace_range(ls..le, new) if new != old
275
+ tobj_set_line_pos(ls)
276
+ else
277
+ return false
278
+ end
279
+ true
280
+ end
281
+
282
+ private
283
+
284
+ def vm_indent_pad
285
+ convert = cnf.tab.to_spaces_default?
286
+ convert = true if cnf.tab.to_spaces_languages?.include?(@lang)
287
+ convert = false if cnf.tab.to_spaces_not_languages?.include?(@lang)
288
+ convert ? " " * cnf.tab.width! : "\t"
289
+ end
290
+
291
+ def vm_first_non_blank(ls)
292
+ p = ls
293
+ p += 1 while p < size - 1 && " \t".include?(self[p])
294
+ p
295
+ end
296
+
297
+ # Character class for word motions: 0 blank or newline, 1 punctuation,
298
+ # 2 word character. For WORDs (big) every non-blank is 1.
299
+ def vm_cls(p, big)
300
+ ch = self[p]
301
+ return 0 if ch.nil? || ch == " " || ch == "\t" || ch == "\n"
302
+ return 1 if big
303
+ ch.match?(/[\p{L}\p{N}_]/) ? 2 : 1
304
+ end
305
+
306
+ # w: start of the next word. An empty line counts as a word; at the end of
307
+ # the buffer, the final "\n".
308
+ def vm_word_next(p, big)
309
+ last = size - 1
310
+ if self[p] != "\n"
311
+ c = vm_cls(p, big)
312
+ p += 1 while p < last && c != 0 && vm_cls(p, big) == c
313
+ p += 1 while p < last && " \t".include?(self[p])
314
+ return p if self[p] != "\n" || p >= last
315
+ end
316
+ loop {
317
+ p += 1
318
+ return last if p >= last
319
+ return p if self[p] == "\n" # empty line
320
+ p += 1 while p < last && " \t".include?(self[p])
321
+ return p if self[p] != "\n"
322
+ }
323
+ end
324
+
325
+ # e: end of the word (the next one when already at an end), or nil
326
+ def vm_word_end(p, big)
327
+ last = size - 1
328
+ p += 1
329
+ p += 1 while p < last && vm_cls(p, big) == 0
330
+ return nil if p >= last
331
+ c = vm_cls(p, big)
332
+ p += 1 while p + 1 < last && vm_cls(p + 1, big) == c
333
+ p
334
+ end
335
+
336
+ def vm_word_end_at?(p, big)
337
+ c = vm_cls(p, big)
338
+ c != 0 && vm_cls(p + 1, big) != c
339
+ end
340
+
341
+ # b: start of the previous word (an empty line counts), or nil at the start
342
+ def vm_word_prev(p, big)
343
+ return nil if p == 0
344
+ p -= 1
345
+ loop {
346
+ ch = self[p]
347
+ break if p == 0
348
+ if ch == " " || ch == "\t"
349
+ p -= 1
350
+ elsif ch == "\n"
351
+ return p if self[p - 1] == "\n" # empty line
352
+ p -= 1
353
+ else
354
+ break
355
+ end
356
+ }
357
+ c = vm_cls(p, big)
358
+ return p if c == 0
359
+ p -= 1 while p > 0 && vm_cls(p - 1, big) == c
360
+ p
361
+ end
362
+
363
+ public
364
+ end
365
+ end # module Vimamsa
data/lib/vimamsa.rb CHANGED
@@ -2,7 +2,6 @@ require "vimamsa/version"
2
2
 
3
3
  require "vmaext"
4
4
 
5
- $:.unshift File.expand_path(File.dirname(__FILE__)+"/../ext/stridx")
6
5
  require "stridx"
7
6
 
8
7
  require "vimamsa/rbvma"
@@ -3,7 +3,7 @@
3
3
  # Enable this module in Preferences → Modules → "Scientific Calculator".
4
4
  #
5
5
  # Usage (once enabled):
6
- # :insert_calculator action — inserts the widget at the cursor position
6
+ # "calculator.insert" action — inserts the widget at the cursor position
7
7
  #
8
8
  # The widget occupies a single U+FFFC (object replacement) character in the
9
9
  # Ruby buffer, keeping GTK/Ruby buffer offsets aligned.
@@ -308,11 +308,11 @@ def insert_calculator_in_buffer
308
308
  end
309
309
 
310
310
  def calculator_init
311
- reg_act(:insert_calculator, proc { insert_calculator_in_buffer }, "Insert scientific calculator widget at cursor")
312
- vma.gui.menu.add_module_action(:insert_calculator, "Insert Calculator")
311
+ reg_act("calculator.insert", proc { insert_calculator_in_buffer }, "Insert scientific calculator widget at cursor")
312
+ vma.gui.menu.add_module_action("calculator.insert", "Insert Calculator")
313
313
  end
314
314
 
315
315
  def calculator_disable
316
- unreg_act(:insert_calculator)
317
- vma.gui.menu.remove_module_action(:insert_calculator)
316
+ unreg_act("calculator.insert")
317
+ vma.gui.menu.remove_module_action("calculator.insert")
318
318
  end
@@ -0,0 +1,118 @@
1
+ # Hex color highlighting module
2
+ #
3
+ # Paints every hex color code (#RGB / #RGBA / #RRGGBB / #RRGGBBAA) in the buffer
4
+ # with that color as its background (e.g. "#3DCBB5" shows up teal), in any file
5
+ # type — source and .txt (hyper plaintext) alike.
6
+ #
7
+ # Enable/disable from Settings ▸ Modules ▸ "Hex Color Highlight", the `C , ; h c`
8
+ # key binding, or `cnf.modules.color_highlight.enabled`. Highlighting runs on a
9
+ # debounced idle after edits, driven by the :view_content_set / :view_text_changed
10
+ # hooks that VSourceView fires (so no core code hard-depends on this module).
11
+
12
+ module Vimamsa
13
+
14
+ # Matches hex color codes: #RGB, #RGBA, #RRGGBB, #RRGGBBAA.
15
+ # The trailing lookahead prevents matching a prefix of a longer hex run.
16
+ COLOR_HEX_RE = /#(?:[0-9a-fA-F]{8}|[0-9a-fA-F]{6}|[0-9a-fA-F]{4}|[0-9a-fA-F]{3})(?![0-9a-fA-F])/ unless defined?(COLOR_HEX_RE)
17
+
18
+ module ColorHighlight
19
+ # Map a matched hex string to [background "#rrggbb", foreground "#000000"/"#ffffff"].
20
+ # Shorthand (#rgb / #rgba) is expanded; any alpha byte is dropped for the swatch.
21
+ def self.display_colors(hex)
22
+ h = hex.delete("#")
23
+ h = h.chars.map { |c| c + c }.join if h.size == 3 || h.size == 4
24
+ r = h[0, 2].to_i(16)
25
+ g = h[2, 2].to_i(16)
26
+ b = h[4, 2].to_i(16)
27
+ bg = format("#%02x%02x%02x", r, g, b)
28
+ lum = 0.299 * r + 0.587 * g + 0.114 * b # perceived brightness
29
+ fg = lum > 140 ? "#000000" : "#ffffff"
30
+ [bg, fg]
31
+ end
32
+ end
33
+
34
+ class VSourceView < GtkSource::View
35
+ # Paint every hex color code in the buffer with its own color as the
36
+ # background. Cheap enough to re-run on a debounced idle after edits; tags are
37
+ # reused across runs. Wipes prior swatches first, so calling it while the
38
+ # module is disabled just clears them.
39
+ def highlight_colors
40
+ return if @bufo.nil?
41
+ vbuf = buffer
42
+ @color_tags ||= {} # bg hex string => Gtk::TextTag
43
+
44
+ unless @color_tags.empty?
45
+ s = vbuf.start_iter
46
+ e = vbuf.end_iter
47
+ @color_tags.each_value { |t| vbuf.remove_tag(t, s, e) }
48
+ end
49
+ return unless cnf.modules.color_highlight.enabled?
50
+
51
+ @bufo.to_s.scan(COLOR_HEX_RE) do
52
+ m = $~
53
+ (bg, fg) = ColorHighlight.display_colors(m[0])
54
+ tag = @color_tags[bg]
55
+ if tag.nil?
56
+ tag = vbuf.create_tag("vma_color_#{bg}")
57
+ tag.background = bg
58
+ tag.foreground = fg
59
+ @color_tags[bg] = tag
60
+ end
61
+ si = vbuf.get_iter_at(:offset => m.begin(0))
62
+ ei = vbuf.get_iter_at(:offset => m.end(0))
63
+ vbuf.apply_tag(tag, si, ei)
64
+ end
65
+ end
66
+ end
67
+
68
+ # Re-highlight the current view's color codes, debounced so a burst of
69
+ # keystrokes only triggers one scan.
70
+ def schedule_color_highlight
71
+ return unless cnf.modules.color_highlight.enabled?
72
+ DelayExecutioner.exec(id: :highlight_colors, wait: 0.4,
73
+ callable: proc { vma.gui.view&.highlight_colors; false })
74
+ end
75
+
76
+ def color_highlight_rehighlight_all
77
+ vma.gui.buffers.each_value { |v| v.highlight_colors }
78
+ end
79
+
80
+ # Hook handlers (registered once by color_highlight_init).
81
+ def color_highlight_on_content_set(view)
82
+ run_as_idle(proc { view.highlight_colors }) if view
83
+ end
84
+
85
+ def color_highlight_on_text_changed(_view = nil)
86
+ schedule_color_highlight
87
+ end
88
+
89
+ def color_highlight_init
90
+ reg_act("color_highlight.toggle", proc {
91
+ en = cnf.modules.color_highlight.enabled? == false
92
+ cnf.modules.color_highlight.enabled = en
93
+ color_highlight_rehighlight_all
94
+ message("Color code highlighting: #{en ? "ON" : "OFF"}")
95
+ }, "Toggle hex color code highlighting on/off")
96
+ add_keys "color_highlight", { "C , ; h c" => "color_highlight.toggle" }
97
+
98
+ # Register the view hooks once. The guard survives module reloads (a global
99
+ # set only here persists across `load`), so a no_restart re-enable never
100
+ # double-registers.
101
+ unless $color_highlight_hooks_registered
102
+ vma.hook.register(:view_content_set, method(:color_highlight_on_content_set))
103
+ vma.hook.register(:view_text_changed, method(:color_highlight_on_text_changed))
104
+ $color_highlight_hooks_registered = true
105
+ end
106
+
107
+ color_highlight_rehighlight_all
108
+ end
109
+
110
+ def color_highlight_disable
111
+ unbindkey "C , ; h c"
112
+ unreg_act("color_highlight.toggle")
113
+ # cnf.modules.color_highlight.enabled is already false here, so this wipes the
114
+ # swatches. The registered hooks stay but no-op via that gate.
115
+ color_highlight_rehighlight_all
116
+ end
117
+
118
+ end # module Vimamsa
@@ -0,0 +1,3 @@
1
+ def color_highlight_info
2
+ { name: "Hex Color Highlight", no_restart: true }
3
+ end