vimamsa 0.1.26 → 0.1.27

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 (170) 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 +62 -16
  6. data/custom_example.rb +43 -13
  7. data/doc/TODO.md +461 -0
  8. data/doc/bundling_plan.md +569 -0
  9. data/doc/key_bindings_report_plan.md +168 -0
  10. data/doc/missing_vim_bindings.md +71 -0
  11. data/doc/modules_settings_plan.md +433 -0
  12. data/doc/refactor_dedup_plan.md +117 -0
  13. data/doc/unused_code_plan.md +120 -0
  14. data/doc/vim_compatibility_plan.md +296 -0
  15. data/doc/vimlike_vim_conflicts.md +254 -0
  16. data/doc/vm_snapshots_plan.md +146 -0
  17. data/exe/run_tests.rb +117 -3
  18. data/exe/vimamsa +24 -4
  19. data/fonts/OFL-Oxanium.txt +93 -0
  20. data/fonts/Oxanium[wght].ttf +0 -0
  21. data/icon.png +0 -0
  22. data/img/demo.gif +0 -0
  23. data/lang/hyperplaintext.lang +6 -0
  24. data/lib/vimamsa/ack.rb +11 -1
  25. data/lib/vimamsa/actions.rb +4 -1
  26. data/lib/vimamsa/appimage_env.rb +35 -0
  27. data/lib/vimamsa/audio.rb +2 -5
  28. data/lib/vimamsa/bookmarks.rb +302 -0
  29. data/lib/vimamsa/buffer.rb +593 -82
  30. data/lib/vimamsa/buffer_changetext.rb +102 -42
  31. data/lib/vimamsa/buffer_cursor.rb +7 -0
  32. data/lib/vimamsa/buffer_list.rb +4 -0
  33. data/lib/vimamsa/buffer_manager.rb +4 -1
  34. data/lib/vimamsa/bundled_fonts.rb +32 -0
  35. data/lib/vimamsa/clipboard.rb +3 -0
  36. data/lib/vimamsa/conf.rb +59 -3
  37. data/lib/vimamsa/constants.rb +3 -0
  38. data/lib/vimamsa/crash_handler.rb +149 -0
  39. data/lib/vimamsa/debug.rb +4 -1
  40. data/lib/vimamsa/desktop_install.rb +96 -0
  41. data/lib/vimamsa/diff_buffer.rb +39 -10
  42. data/lib/vimamsa/easy_jump.rb +3 -0
  43. data/lib/vimamsa/editor.rb +263 -57
  44. data/lib/vimamsa/encrypt.rb +78 -29
  45. data/lib/vimamsa/file_finder.rb +4 -19
  46. data/lib/vimamsa/file_history.rb +3 -0
  47. data/lib/vimamsa/file_manager.rb +30 -15
  48. data/lib/vimamsa/fold.rb +226 -0
  49. data/lib/vimamsa/gui.rb +520 -106
  50. data/lib/vimamsa/gui_command_line.rb +208 -0
  51. data/lib/vimamsa/gui_dialog.rb +94 -42
  52. data/lib/vimamsa/gui_file_panel.rb +356 -25
  53. data/lib/vimamsa/gui_form_generator.rb +59 -35
  54. data/lib/vimamsa/gui_func_panel.rb +67 -22
  55. data/lib/vimamsa/gui_image.rb +3 -0
  56. data/lib/vimamsa/gui_keylog_panel.rb +246 -0
  57. data/lib/vimamsa/gui_menu.rb +23 -11
  58. data/lib/vimamsa/gui_select_window.rb +19 -48
  59. data/lib/vimamsa/gui_settings.rb +78 -37
  60. data/lib/vimamsa/gui_sourceview.rb +273 -19
  61. data/lib/vimamsa/gui_sourceview_autocomplete.rb +319 -90
  62. data/lib/vimamsa/gui_text.rb +63 -2
  63. data/lib/vimamsa/gui_theme.rb +606 -0
  64. data/lib/vimamsa/hook.rb +3 -0
  65. data/lib/vimamsa/hyper_plain_text.rb +3 -0
  66. data/lib/vimamsa/key_actions.rb +112 -13
  67. data/lib/vimamsa/key_binding_tree.rb +242 -215
  68. data/lib/vimamsa/key_bindings_notepad.rb +43 -0
  69. data/lib/vimamsa/key_bindings_report.rb +261 -0
  70. data/lib/vimamsa/key_bindings_vim.rb +354 -0
  71. data/lib/vimamsa/key_bindings_vimlike.rb +65 -9
  72. data/lib/vimamsa/langservp.rb +271 -54
  73. data/lib/vimamsa/macro.rb +3 -0
  74. data/lib/vimamsa/main.rb +14 -8
  75. data/lib/vimamsa/rbvma.rb +26 -1
  76. data/lib/vimamsa/search.rb +3 -0
  77. data/lib/vimamsa/search_replace.rb +11 -10
  78. data/lib/vimamsa/string_util.rb +120 -40
  79. data/lib/vimamsa/terminal.rb +3 -0
  80. data/lib/vimamsa/test_framework.rb +28 -2
  81. data/lib/vimamsa/tests.rb +3 -0
  82. data/lib/vimamsa/text_objects.rb +353 -0
  83. data/lib/vimamsa/text_transforms.rb +14 -0
  84. data/lib/vimamsa/util.rb +46 -7
  85. data/lib/vimamsa/version.rb +1 -1
  86. data/lib/vimamsa.rb +0 -1
  87. data/modules/color_highlight/color_highlight.rb +118 -0
  88. data/modules/color_highlight/color_highlight_info.rb +3 -0
  89. data/modules/dictation/dictation.rb +1054 -0
  90. data/modules/dictation/dictation_info.rb +3 -0
  91. data/modules/dictation/whisper_worker.py +273 -0
  92. data/modules/ollama/ollama.rb +107 -0
  93. data/modules/ollama/ollama_info.rb +3 -0
  94. data/modules/tts/tts.rb +119 -0
  95. data/modules/tts/tts_info.rb +3 -0
  96. data/run_e2e_vm.sh +28 -0
  97. data/run_tests_vm.sh +130 -0
  98. data/scripts/appimage/AppRun +54 -0
  99. data/scripts/appimage/Dockerfile +18 -0
  100. data/scripts/appimage/build.sh +19 -0
  101. data/scripts/appimage/build_in_container.sh +192 -0
  102. data/scripts/appimage/smoke_test.sh +27 -0
  103. data/scripts/demo/demo_steps.txt +59 -0
  104. data/scripts/demo/record_demo.rb +177 -0
  105. data/scripts/demo/shapes.rb +28 -0
  106. data/styles/134272-molokai.xml +3 -0
  107. data/styles/molokai_cyber.xml +48 -0
  108. data/styles/molokai_edit.xml +1 -0
  109. data/tests/support/virtual_keyboard.rb +254 -0
  110. data/tests/support/virtual_mic.rb +67 -0
  111. data/tests/test_ack.rb +22 -0
  112. data/tests/test_auto_format.rb +122 -0
  113. data/tests/test_autocomplete.rb +211 -0
  114. data/tests/test_autosave.rb +145 -0
  115. data/tests/test_basic_editing.rb +33 -3
  116. data/tests/test_bookmarks.rb +206 -0
  117. data/tests/test_bracket_match.rb +85 -0
  118. data/tests/test_bundling.rb +92 -0
  119. data/tests/test_color_highlight.rb +92 -0
  120. data/tests/test_command_line.rb +79 -0
  121. data/tests/test_command_line_file.rb +27 -0
  122. data/tests/test_context_menu_file.rb +129 -0
  123. data/tests/test_copy_paste.rb +80 -0
  124. data/tests/test_crash_handler.rb +39 -0
  125. data/tests/test_custom_two_phase.rb +92 -0
  126. data/tests/test_dialog_callback_errors.rb +44 -0
  127. data/tests/test_dictation.rb +294 -0
  128. data/tests/test_diff_buffer_name.rb +51 -0
  129. data/tests/test_double_click.rb +41 -0
  130. data/tests/test_drag_selection.rb +125 -0
  131. data/tests/test_e2e_cmdline.rb +116 -0
  132. data/tests/test_e2e_dictation.rb +98 -0
  133. data/tests/test_e2e_replace_char.rb +58 -0
  134. data/tests/test_extract_file_links.rb +55 -0
  135. data/tests/test_file_format.rb +217 -0
  136. data/tests/test_file_manager.rb +72 -0
  137. data/tests/test_file_panel.rb +269 -0
  138. data/tests/test_flash.rb +78 -0
  139. data/tests/test_fold.rb +111 -0
  140. data/tests/test_font_refresh.rb +56 -0
  141. data/tests/test_func_panel.rb +68 -0
  142. data/tests/test_grep_apply.rb +46 -0
  143. data/tests/test_gtk_sync.rb +126 -0
  144. data/tests/test_hpt_highlight_visible.rb +75 -0
  145. data/tests/test_hpt_link_highlight.rb +82 -0
  146. data/tests/test_jump_to_file.rb +41 -0
  147. data/tests/test_key_bindings.rb +208 -0
  148. data/tests/test_keylog_panel.rb +101 -0
  149. data/tests/test_lsp_clangd.rb +281 -0
  150. data/tests/test_lsp_jump.rb +87 -0
  151. data/tests/test_lsp_position_encoding.rb +49 -0
  152. data/tests/test_lsp_symbols.rb +68 -0
  153. data/tests/test_macro.rb +137 -0
  154. data/tests/test_markdown_highlight.rb +57 -0
  155. data/tests/test_mode_badge.rb +41 -0
  156. data/tests/test_notepad_bindings.rb +67 -0
  157. data/tests/test_popup_queue.rb +51 -0
  158. data/tests/test_random_edit.rb +109 -0
  159. data/tests/test_revert.rb +127 -0
  160. data/tests/test_startup_file.rb +58 -0
  161. data/tests/test_status_area.rb +50 -0
  162. data/tests/test_text_objects.rb +163 -0
  163. data/tests/test_text_transforms.rb +329 -0
  164. data/tests/test_tts.rb +68 -0
  165. data/tests/test_vim_bindings.rb +252 -0
  166. data/tests/test_vim_conflicts.rb +63 -0
  167. data/tests/test_window_layout.rb +219 -0
  168. data/vimamsa.gemspec +3 -1
  169. data/vm_guest_run.sh +109 -0
  170. metadata +112 -10
@@ -0,0 +1,353 @@
1
+ module Vimamsa
2
+ # Vim text objects. After an operator (d c y gU gu g~) or in Visual mode,
3
+ # "i <x>" / "a <x>" acts on a range around the cursor instead of moving it:
4
+ # w W word / WORD (WORD = any run of non-blank characters)
5
+ # s sentence
6
+ # p paragraph (linewise)
7
+ # " ' ` quoted string on the current line
8
+ # ( ) b ( ) block, may span lines; also [ ], { } B, < >
9
+ # t XML/HTML tag
10
+ # "i" = inner (contents only). "a" = also the delimiters, or the white space
11
+ # around, following vim's rules (vim :help text-objects). Checked against
12
+ # vim 9.1, including "not inside a ( ) block => use the next one on the line".
13
+ class Buffer < String
14
+ TEXT_OBJECT_BRACKETS = {
15
+ "(" => "()", ")" => "()", "b" => "()",
16
+ "[" => "[]", "]" => "[]",
17
+ "{" => "{}", "}" => "{}", "B" => "{}",
18
+ "<" => "<>", ">" => "<>",
19
+ }
20
+
21
+ # Range of text object obj ("w", "(", ...) around pos, as
22
+ # [start, end, linewise] with end exclusive (start == end for an empty
23
+ # inner, e.g. i( on "()"), or nil if there is no such object here.
24
+ # count = nesting level for brackets and tags (vim d2i().
25
+ def text_object_range(obj, inner, pos = @pos, count = 1)
26
+ case obj
27
+ when "w" then tobj_word(pos, inner, false)
28
+ when "W" then tobj_word(pos, inner, true)
29
+ when "s" then tobj_sentence(pos, inner)
30
+ when "p" then tobj_paragraph(pos, inner)
31
+ when '"', "'", "`" then tobj_quote(pos, inner, obj)
32
+ when "t" then tobj_tag(pos, inner, count)
33
+ else
34
+ br = TEXT_OBJECT_BRACKETS[obj]
35
+ br && tobj_bracket(pos, inner, br[0], br[1], count)
36
+ end
37
+ end
38
+
39
+ # Apply op to a text object, e.g. text_object_op(:delete, "w", :i) = vim diw.
40
+ # op: :delete, :change, :yank, :upcase, :downcase, :swapcase, or :select
41
+ # (Visual mode: select the object). which: :i (inner) or :a.
42
+ # Returns true if there was an object at the cursor.
43
+ def text_object_op(op, obj, which)
44
+ count = get_repeat_num
45
+ r = text_object_range(obj, which == :i, @pos, count)
46
+ return false if r.nil?
47
+ s, e, linewise = r
48
+ case op
49
+ when :select
50
+ return false if e <= s
51
+ @selection_start = s
52
+ set_pos(e - 1)
53
+ when :yank
54
+ vma.clipboard.set(self[s...e]) if e > s
55
+ @paste_lines = linewise
56
+ set_pos(s)
57
+ when :delete
58
+ return false if e <= s
59
+ txt = self[s...e]
60
+ ds, de = s, e - 1
61
+ # The buffer's final "\n" can't be deleted: for the last lines, take
62
+ # the newline before them instead (vim dap on the last paragraph).
63
+ ds, de = s - 1, e - 2 if linewise && e >= size && s > 0
64
+ Gui.flash_range(self, ds..de) do
65
+ delete_range(ds, de)
66
+ vma.clipboard.set(txt)
67
+ @paste_lines = linewise
68
+ linewise ? tobj_set_line_pos(ds) : tobj_set_normal_pos(s)
69
+ end
70
+ when :change
71
+ # Linewise (cip, multi-line ci{): leave an empty line to type on, as vim
72
+ if linewise && e >= size && e - 1 > s
73
+ delete_range(s, e - 2) # the final "\n" stays as that line
74
+ else
75
+ delete_range(s, e - 1) if e > s
76
+ insert_txt_at("\n", s) if linewise
77
+ end
78
+ @paste_lines = linewise
79
+ set_pos(s)
80
+ vma.kbd.set_mode(:insert)
81
+ when :upcase, :downcase, :swapcase
82
+ return false if e <= s
83
+ old = self[s...e]
84
+ txt = transform_text(old.dup, op)
85
+ replace_range(s..(e - 1), txt) if txt != old
86
+ set_pos(s)
87
+ end
88
+ return true
89
+ end
90
+
91
+ private
92
+
93
+ # Like vim Normal mode: the cursor can't stay on a line's newline unless
94
+ # the line is empty.
95
+ def tobj_set_normal_pos(p)
96
+ p = [p, size - 1].min
97
+ p -= 1 if p > 0 and self[p] == "\n" and self[p - 1] != "\n"
98
+ set_pos(p)
99
+ end
100
+
101
+ # After a linewise delete: first non-blank of the line holding p (vim)
102
+ def tobj_set_line_pos(p)
103
+ p = [p, size - 1].min
104
+ ls, le = tobj_line_bounds(p)
105
+ p = ls
106
+ p += 1 while p < le && " \t".include?(self[p])
107
+ tobj_set_normal_pos(p)
108
+ end
109
+
110
+ # [start of line, position of its "\n"] for the line holding pos
111
+ def tobj_line_bounds(pos)
112
+ ls = pos == 0 ? 0 : (rindex("\n", pos - 1) || -1) + 1
113
+ [ls, index("\n", pos) || size]
114
+ end
115
+
116
+ def tobj_word(pos, inner, big)
117
+ ls, le = tobj_line_bounds(pos)
118
+ return nil if pos >= le
119
+ # 0 = blank, 1 = word chars (WORD: any non-blank), 2 = other non-blank
120
+ cls = ->(c) { c == " " || c == "\t" ? 0 : (big || c.match?(/\p{Word}/) ? 1 : 2) }
121
+ run = ->(p) {
122
+ k = cls.(self[p])
123
+ s = p
124
+ s -= 1 while s > ls && cls.(self[s - 1]) == k
125
+ e = p + 1
126
+ e += 1 while e < le && cls.(self[e]) == k
127
+ [s, e]
128
+ }
129
+ s, e = run.(pos)
130
+ return [s, e, false] if inner
131
+ if cls.(self[pos]) == 0 # on blanks: the blanks and the word after them
132
+ return nil if e >= le
133
+ return [s, run.(e)[1], false]
134
+ end
135
+ return [s, run.(e)[1], false] if e < le && cls.(self[e]) == 0 # trailing blanks
136
+ # No trailing blanks: take the leading ones, unless they are the indent
137
+ if s > ls && cls.(self[s - 1]) == 0
138
+ bs = run.(s - 1)[0]
139
+ s = bs if bs > ls
140
+ end
141
+ [s, e, false]
142
+ end
143
+
144
+ def tobj_sentence(pos, inner)
145
+ ls, le = tobj_line_bounds(pos)
146
+ return nil if self[ls...le].match?(/\A[ \t]*\z/)
147
+ para = tobj_paragraph(pos, true)
148
+ ps, pe = para[0], para[1] - 1 # pe: the paragraph's last newline
149
+ sents = [] # [start, end of sentence, end of the white space after it]
150
+ i = ps
151
+ i += 1 while i < pe && self[i].match?(/\s/)
152
+ while i < pe
153
+ m = /[.!?][)\]"']*(?=\s|\z)/.match(self[i...pe])
154
+ e = m ? i + m.end(0) : pe
155
+ w = e
156
+ w += 1 while w < pe && self[w].match?(/\s/)
157
+ sents << [i, e, w]
158
+ i = w
159
+ end
160
+ return nil if sents.empty?
161
+ k = sents.rindex { |s, _e, _w| s <= pos } || 0
162
+ s, e, w = sents[k]
163
+ if pos >= e # in the white space after a sentence
164
+ return [e, w, false] if inner
165
+ nxt = sents[k + 1]
166
+ return [e, nxt ? nxt[1] : w, false]
167
+ end
168
+ return [s, e, false] if inner
169
+ return [s, w, false] if w > e
170
+ # Last sentence of the paragraph: take the white space before it
171
+ [k > 0 ? sents[k - 1][1] : s, e, false]
172
+ end
173
+
174
+ def tobj_paragraph(pos, inner)
175
+ line_start = ->(p) { p == 0 ? 0 : (rindex("\n", p - 1) || -1) + 1 }
176
+ line_end = ->(l) { (index("\n", l) || size - 1) + 1 } # after its "\n"
177
+ blank = ->(l) { self[l...line_end.(l)].match?(/\A[ \t]*\n?\z/) }
178
+ next_line = ->(l) { n = line_end.(l); n < size ? n : nil }
179
+ prev_line = ->(l) { l == 0 ? nil : line_start.(l - 1) }
180
+
181
+ ls = line_start.(pos)
182
+ b = blank.(ls)
183
+ s = ls
184
+ while (pl = prev_line.(s)) && blank.(pl) == b
185
+ s = pl
186
+ end
187
+ e = ls
188
+ while (nl = next_line.(e)) && blank.(nl) == b
189
+ e = nl
190
+ end
191
+ return [s, line_end.(e), true] if inner
192
+ # "a": add the run after it (blank lines after text, text after blanks)
193
+ if (nl = next_line.(e))
194
+ e = nl
195
+ while (n2 = next_line.(e)) && blank.(n2) != b
196
+ e = n2
197
+ end
198
+ return [s, line_end.(e), true]
199
+ end
200
+ # Last paragraph: take the blank lines before it instead
201
+ if !b
202
+ while (pl = prev_line.(s)) && blank.(pl)
203
+ s = pl
204
+ end
205
+ end
206
+ [s, line_end.(e), true]
207
+ end
208
+
209
+ def tobj_quote(pos, inner, q)
210
+ ls, le = tobj_line_bounds(pos)
211
+ qs = [] # unescaped quote chars on the line
212
+ i = ls
213
+ while i < le
214
+ if self[i] == "\\"
215
+ i += 2
216
+ next
217
+ end
218
+ qs << i if self[i] == q
219
+ i += 1
220
+ end
221
+ if (k = qs.index(pos))
222
+ # On a quote: which pair it belongs to is counted from the line start
223
+ o, c = k.even? ? [pos, qs[k + 1]] : [qs[k - 1], pos]
224
+ elsif (k = qs.rindex { |x| x < pos })
225
+ o, c = qs[k], qs[k + 1]
226
+ else # no quote before the cursor: the first string after it
227
+ o, c = qs[0], qs[1]
228
+ end
229
+ return nil if o.nil? || c.nil?
230
+ return [o + 1, c, false] if inner
231
+ s, e = o, c + 1
232
+ w = e
233
+ w += 1 while w < le && " \t".include?(self[w])
234
+ if w > e
235
+ e = w
236
+ else
237
+ s -= 1 while s > ls && " \t".include?(self[s - 1])
238
+ end
239
+ [s, e, false]
240
+ end
241
+
242
+ def tobj_bracket(pos, inner, op, cl, count)
243
+ o = tobj_find_open(pos, op, cl, count)
244
+ if o.nil? && count == 1
245
+ # Not inside a block: use the next one on the line (as vim 9.1)
246
+ _ls, le = tobj_line_bounds(pos)
247
+ nx = index(op, pos)
248
+ o = nx if nx && nx < le
249
+ end
250
+ return nil if o.nil?
251
+ c = tobj_match_close(o, op, cl)
252
+ return nil if c.nil?
253
+ return [o, c + 1, false] if !inner
254
+ s, e = o + 1, c
255
+ # "(" ending its line / ")" first on its line: those lines aren't part
256
+ # of the inner text, so di{ on a multi-line block keeps "{\n}".
257
+ starts_line = self[s] == "\n"
258
+ s += 1 if starts_line
259
+ nl = rindex("\n", c - 1)
260
+ ends_line = nl && nl >= o + 1 && self[(nl + 1)...c].match?(/\A[ \t]*\z/)
261
+ e = nl + 1 if ends_line
262
+ e = s if e < s
263
+ [s, e, starts_line && ends_line && e > s]
264
+ end
265
+
266
+ # Position of the count'th unmatched op enclosing pos (pos itself if on op)
267
+ def tobj_find_open(pos, op, cl, count)
268
+ o = nil
269
+ i = pos - 1
270
+ if self[pos] == op
271
+ o = pos
272
+ count -= 1
273
+ end
274
+ while count > 0
275
+ depth = 0
276
+ found = nil
277
+ while i >= 0
278
+ ch = self[i]
279
+ if ch == cl
280
+ depth += 1
281
+ elsif ch == op
282
+ if depth == 0
283
+ found = i
284
+ break
285
+ end
286
+ depth -= 1
287
+ end
288
+ i -= 1
289
+ end
290
+ return nil if found.nil?
291
+ o = found
292
+ count -= 1
293
+ i = found - 1
294
+ end
295
+ o
296
+ end
297
+
298
+ def tobj_match_close(o, op, cl)
299
+ depth = 0
300
+ i = o + 1
301
+ while i < size
302
+ ch = self[i]
303
+ if ch == op
304
+ depth += 1
305
+ elsif ch == cl
306
+ return i if depth == 0
307
+ depth -= 1
308
+ end
309
+ i += 1
310
+ end
311
+ nil
312
+ end
313
+
314
+ def tobj_tag(pos, inner, count)
315
+ pairs = [] # [outer start, inner start, inner end, outer end]
316
+ stack = []
317
+ to_s.scan(/<(\/?)([A-Za-z][\w:.-]*)[^<>]*?(\/?)>/) {
318
+ m = $~
319
+ next if m[3] == "/" # <br/>
320
+ if m[1] == ""
321
+ stack << [m[2], m.begin(0), m.end(0)]
322
+ elsif (k = stack.rindex { |t| t[0] == m[2] })
323
+ t = stack[k]
324
+ stack = stack[0...k] # drops unclosed tags inside, e.g. <br>
325
+ pairs << [t[1], t[2], m.begin(0), m.end(0)]
326
+ end
327
+ }
328
+ enc = pairs.select { |os, _is, _ie, oe| os <= pos && pos < oe }.sort_by { |os, _is, _ie, oe| oe - os }
329
+ t = enc[count - 1]
330
+ return nil if t.nil?
331
+ inner ? [t[1], t[2], false] : [t[0], t[3], false]
332
+ end
333
+
334
+ public
335
+ end
336
+
337
+ # Bind the text objects for operators d c y gU gu g~ and Visual mode.
338
+ # Called by the vim and vimlike keymaps; none of these keys are used by
339
+ # vimlike otherwise (Visual "a y" / "a d" keep working: exact keys win over
340
+ # <char>, and vim has no "ay"/"ad" objects).
341
+ def bind_vim_text_objects
342
+ ops = { "d" => :delete, "c" => :change, "y" => :yank,
343
+ "g U" => :upcase, "g u" => :downcase, "g ~" => :swapcase }
344
+ h = {}
345
+ ops.each { |k, op|
346
+ %w[i a].each { |w|
347
+ h["C #{k} #{w} <char>"] = "buf.text_object_op(:#{op}, <char>, :#{w})"
348
+ }
349
+ }
350
+ %w[i a].each { |w| h["V #{w} <char>"] = "buf.text_object_op(:select, <char>, :#{w})" }
351
+ add_keys "vim text objects", h
352
+ end
353
+ end
@@ -1,3 +1,5 @@
1
+
2
+ module Vimamsa
1
3
  class Converter
2
4
  def initialize(obj, type, id = nil)
3
5
  @obj = obj
@@ -22,6 +24,10 @@ Converter.new(lambda { |x| x.split("\n").sort.join("\n") }, :lambda, :sortlines)
22
24
  Converter.new(lambda { |x| x.split("\n").reverse.join("\n") }, :lambda, :reverse_lines)
23
25
 
24
26
  Converter.new(lambda { |x| x.split(/\s+/).sort.join(" ") }, :lambda, :sortwords)
27
+
28
+ # Collapse runs of spaces/tabs to one space and strip trailing whitespace on
29
+ # each line. Leading indentation is kept.
30
+ Converter.new(lambda { |x| x.gsub(/^([ \t]*)(.*?)[ \t]*$/) { $1 + $2.gsub(/[ \t]+/, " ") } }, :lambda, :remove_extra_spaces)
25
31
  Converter.new(lambda { |x| x.split("\n").collect { |b| b.scan(/(\d+(\.\d+)?)/).collect { |c| c[0] }.join(" ") }.join("\n") }, :lambda, :getnums_on_lines)
26
32
 
27
33
  Converter.new(lambda { |x| x + "\n" + x.split("\n").collect { |b| b.scan(/(\d+(\.\d+)?)/).collect { |c| c[0] }.join(" ") }.join("\n") }, :lambda, :getnums_on_lines)
@@ -34,6 +40,14 @@ Converter.new(lambda { |x|
34
40
 
35
41
  Converter.new(lambda { |x| x.scan(/[\w\.]+@[\w\.]+/).join("\n") }, :lambda, :get_emails)
36
42
 
43
+ Converter.new(lambda { |x| x.scan(/https?:\/\/[^\s\])"'>]+/).join("\n") }, :lambda, :get_urls)
44
+
37
45
  # Eval selection as ruby code
38
46
  Converter.new(lambda { |x| b = "eval failed"; begin; b = eval(x, TOPLEVEL_BINDING); rescue; end; "#{x}\n#{b}\n" }, :lambda, :eval)
39
47
 
48
+ Converter.new(lambda { |x|
49
+ items = x.split("\n").map(&:strip).reject(&:empty?)
50
+ "\\begin{itemize}\n" + items.map { |line| " \\item #{line}" }.join("\n") + "\n\\end{itemize}\n"
51
+ }, :lambda, :latex_itemize)
52
+
53
+ end # module Vimamsa
data/lib/vimamsa/util.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  require "open3"
2
2
 
3
+ module Vimamsa
4
+
3
5
  VOWELS = %w(a e i o u)
4
6
  CONSONANTS = %w(b c d f g h j k l m n p q r s t v w x y z)
5
7
 
@@ -21,6 +23,15 @@ def running_wayland?
21
23
  end
22
24
  end
23
25
 
26
+ # True when the editor was started by exe/run_tests.rb (--test). Used to skip
27
+ # dialogs the user did not ask for (session restore, autosave found, reload
28
+ # from disk): a modal popup grabs the keyboard, so it would swallow the keys an
29
+ # e2e test types and leave the run stuck. Tests that cover those dialogs call
30
+ # the prompting methods directly, so only the unsolicited call sites check this.
31
+ def vma_test_mode?
32
+ ARGV.include?("--test")
33
+ end
34
+
24
35
  def tilde_path(abspath)
25
36
  userhome = File.expand_path("~/")
26
37
  abspath.sub(/^#{Regexp.escape(userhome)}\//, "~/")
@@ -76,9 +87,10 @@ def file_is_text_file(fpath)
76
87
  end
77
88
 
78
89
  #TODO: not sure if needed
79
- r = exec_cmd("file", fpath)
80
- return true if r.match(/UTF-8.*text/)
81
- return true if r.match(/ASCII.*text/)
90
+ # Any text encoding (e.g. "ISO-8859 text"): read_file converts it to UTF-8.
91
+ # -b: without the file name, which could itself contain "text".
92
+ r = exec_cmd("file", "-b", fpath)
93
+ return true if r.match(/\btext\b/)
82
94
  return false
83
95
  end
84
96
 
@@ -240,7 +252,7 @@ class DelayExecutioner
240
252
  while true
241
253
  sleep 0.1
242
254
  if Time.now - @lastt > @wait_time
243
- @proc.call
255
+ GLib::Idle.add { @proc.call; false }
244
256
  @last_run = Time.now
245
257
  @thread_running = false
246
258
  break
@@ -278,7 +290,31 @@ def assert_binary_exists(bin)
278
290
  end
279
291
  end
280
292
 
293
+ # Command output (e.g. a diff) as buffer text: UTF-8 with \n line endings.
294
+ # Lines that are not valid UTF-8 (from files in other encodings, e.g.
295
+ # Latin-1) are decoded with the same guess as read_file_with_format.
296
+ def text_to_utf8(str)
297
+ str = str.dup.force_encoding(Encoding::UTF_8)
298
+ unless str.valid_encoding?
299
+ str = str.b.lines.map { |l|
300
+ u = l.dup.force_encoding(Encoding::UTF_8)
301
+ next u if u.valid_encoding?
302
+ enc = GUESS_ENCODING_ORDER.find { |e| l.dup.force_encoding(e).valid_encoding? }
303
+ l.force_encoding(enc).encode(Encoding::UTF_8, invalid: :replace, undef: :replace)
304
+ }.join
305
+ end
306
+ str.gsub("\r\n", "\n")
307
+ end
308
+
281
309
  def read_file(text, path)
310
+ read_file_with_format(path, text.encoding)&.first
311
+ end
312
+
313
+ # Read a text file as UTF-8 with \n line endings. Returns [content, format]:
314
+ # format = { encoding: the file's encoding (guessed if not valid UTF-8),
315
+ # crlf: true if every line ends in \r\n }
316
+ # so that saving can restore the original format (see Buffer#save).
317
+ def read_file_with_format(path, encoding = Encoding::UTF_8)
282
318
  path = Pathname(path.to_s).expand_path
283
319
  FileUtils.touch(path) unless File.exist?(path)
284
320
  if !File.exist?(path)
@@ -286,7 +322,6 @@ def read_file(text, path)
286
322
  return
287
323
  end
288
324
 
289
- encoding = text.encoding
290
325
  content = path.open("r:#{encoding.name}") { |io| io.read }
291
326
 
292
327
  debug("GUESS ENCODING")
@@ -295,9 +330,12 @@ def read_file(text, path)
295
330
  content.force_encoding(enc)
296
331
  content.valid_encoding?
297
332
  }
298
- content.encode!(Encoding::UTF_8)
299
333
  end
334
+ file_encoding = content.encoding
335
+ content.encode!(Encoding::UTF_8) if file_encoding != Encoding::UTF_8
300
336
  debug("END GUESS ENCODING")
337
+ nl = content.count("\n")
338
+ crlf = nl > 0 && content.scan("\r\n").size == nl
301
339
 
302
340
  #TODO: Should put these as option:
303
341
  content.gsub!(/\r\n/, "\n")
@@ -306,7 +344,7 @@ def read_file(text, path)
306
344
 
307
345
  # content = filter_buffer(content)
308
346
  debug("END FILTER")
309
- return content
347
+ return [content, { encoding: file_encoding, crlf: crlf }]
310
348
  end
311
349
 
312
350
  def expand_if_existing(fpath)
@@ -361,3 +399,4 @@ end
361
399
  # #TODO: check contents of file
362
400
  # return true
363
401
  # end
402
+ end # module Vimamsa
@@ -1,3 +1,3 @@
1
1
  module Vimamsa
2
- VERSION = "0.1.26"
2
+ VERSION = "0.1.27"
3
3
  end
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"
@@ -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(:toggle_highlight_colors, 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" => :toggle_highlight_colors }
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(:toggle_highlight_colors)
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