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,310 @@
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: an operator of VIM_OPERATORS (:delete, :change, :yank, :upcase, ...)
41
+ # or :select (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
+ if op == :select
49
+ return false if e <= s
50
+ @selection_start = s
51
+ set_pos(e - 1)
52
+ return true
53
+ end
54
+ # yank / change of an empty inner (yi( on "()") still sets the cursor
55
+ return false if e <= s && ![:yank, :change].include?(op)
56
+ if op == :yank
57
+ vma.clipboard.set(self[s...e], linewise: linewise) if e > s
58
+ set_pos(s)
59
+ return true
60
+ end
61
+ vim_apply_operator(op, s, e, linewise)
62
+ end
63
+
64
+ private
65
+
66
+ # Like vim Normal mode: the cursor can't stay on a line's newline unless
67
+ # the line is empty.
68
+ def tobj_set_normal_pos(p)
69
+ p = [p, size - 1].min
70
+ p -= 1 if p > 0 and self[p] == "\n" and self[p - 1] != "\n"
71
+ set_pos(p)
72
+ end
73
+
74
+ # After a linewise delete: first non-blank of the line holding p (vim)
75
+ def tobj_set_line_pos(p)
76
+ p = [p, size - 1].min
77
+ ls, le = tobj_line_bounds(p)
78
+ p = ls
79
+ p += 1 while p < le && " \t".include?(self[p])
80
+ tobj_set_normal_pos(p)
81
+ end
82
+
83
+ # [start of line, position of its "\n"] for the line holding pos
84
+ def tobj_line_bounds(pos)
85
+ ls = pos == 0 ? 0 : (rindex("\n", pos - 1) || -1) + 1
86
+ [ls, index("\n", pos) || size]
87
+ end
88
+
89
+ def tobj_word(pos, inner, big)
90
+ ls, le = tobj_line_bounds(pos)
91
+ return nil if pos >= le
92
+ # 0 = blank, 1 = word chars (WORD: any non-blank), 2 = other non-blank
93
+ cls = ->(c) { c == " " || c == "\t" ? 0 : (big || c.match?(/\p{Word}/) ? 1 : 2) }
94
+ run = ->(p) {
95
+ k = cls.(self[p])
96
+ s = p
97
+ s -= 1 while s > ls && cls.(self[s - 1]) == k
98
+ e = p + 1
99
+ e += 1 while e < le && cls.(self[e]) == k
100
+ [s, e]
101
+ }
102
+ s, e = run.(pos)
103
+ return [s, e, false] if inner
104
+ if cls.(self[pos]) == 0 # on blanks: the blanks and the word after them
105
+ return nil if e >= le
106
+ return [s, run.(e)[1], false]
107
+ end
108
+ return [s, run.(e)[1], false] if e < le && cls.(self[e]) == 0 # trailing blanks
109
+ # No trailing blanks: take the leading ones, unless they are the indent
110
+ if s > ls && cls.(self[s - 1]) == 0
111
+ bs = run.(s - 1)[0]
112
+ s = bs if bs > ls
113
+ end
114
+ [s, e, false]
115
+ end
116
+
117
+ def tobj_sentence(pos, inner)
118
+ ls, le = tobj_line_bounds(pos)
119
+ return nil if self[ls...le].match?(/\A[ \t]*\z/)
120
+ para = tobj_paragraph(pos, true)
121
+ ps, pe = para[0], para[1] - 1 # pe: the paragraph's last newline
122
+ sents = [] # [start, end of sentence, end of the white space after it]
123
+ i = ps
124
+ i += 1 while i < pe && self[i].match?(/\s/)
125
+ while i < pe
126
+ m = /[.!?][)\]"']*(?=\s|\z)/.match(self[i...pe])
127
+ e = m ? i + m.end(0) : pe
128
+ w = e
129
+ w += 1 while w < pe && self[w].match?(/\s/)
130
+ sents << [i, e, w]
131
+ i = w
132
+ end
133
+ return nil if sents.empty?
134
+ k = sents.rindex { |s, _e, _w| s <= pos } || 0
135
+ s, e, w = sents[k]
136
+ if pos >= e # in the white space after a sentence
137
+ return [e, w, false] if inner
138
+ nxt = sents[k + 1]
139
+ return [e, nxt ? nxt[1] : w, false]
140
+ end
141
+ return [s, e, false] if inner
142
+ return [s, w, false] if w > e
143
+ # Last sentence of the paragraph: take the white space before it
144
+ [k > 0 ? sents[k - 1][1] : s, e, false]
145
+ end
146
+
147
+ def tobj_paragraph(pos, inner)
148
+ line_start = ->(p) { p == 0 ? 0 : (rindex("\n", p - 1) || -1) + 1 }
149
+ line_end = ->(l) { (index("\n", l) || size - 1) + 1 } # after its "\n"
150
+ blank = ->(l) { self[l...line_end.(l)].match?(/\A[ \t]*\n?\z/) }
151
+ next_line = ->(l) { n = line_end.(l); n < size ? n : nil }
152
+ prev_line = ->(l) { l == 0 ? nil : line_start.(l - 1) }
153
+
154
+ ls = line_start.(pos)
155
+ b = blank.(ls)
156
+ s = ls
157
+ while (pl = prev_line.(s)) && blank.(pl) == b
158
+ s = pl
159
+ end
160
+ e = ls
161
+ while (nl = next_line.(e)) && blank.(nl) == b
162
+ e = nl
163
+ end
164
+ return [s, line_end.(e), true] if inner
165
+ # "a": add the run after it (blank lines after text, text after blanks)
166
+ if (nl = next_line.(e))
167
+ e = nl
168
+ while (n2 = next_line.(e)) && blank.(n2) != b
169
+ e = n2
170
+ end
171
+ return [s, line_end.(e), true]
172
+ end
173
+ # Last paragraph: take the blank lines before it instead
174
+ if !b
175
+ while (pl = prev_line.(s)) && blank.(pl)
176
+ s = pl
177
+ end
178
+ end
179
+ [s, line_end.(e), true]
180
+ end
181
+
182
+ def tobj_quote(pos, inner, q)
183
+ ls, le = tobj_line_bounds(pos)
184
+ qs = [] # unescaped quote chars on the line
185
+ i = ls
186
+ while i < le
187
+ if self[i] == "\\"
188
+ i += 2
189
+ next
190
+ end
191
+ qs << i if self[i] == q
192
+ i += 1
193
+ end
194
+ if (k = qs.index(pos))
195
+ # On a quote: which pair it belongs to is counted from the line start
196
+ o, c = k.even? ? [pos, qs[k + 1]] : [qs[k - 1], pos]
197
+ elsif (k = qs.rindex { |x| x < pos })
198
+ o, c = qs[k], qs[k + 1]
199
+ else # no quote before the cursor: the first string after it
200
+ o, c = qs[0], qs[1]
201
+ end
202
+ return nil if o.nil? || c.nil?
203
+ return [o + 1, c, false] if inner
204
+ s, e = o, c + 1
205
+ w = e
206
+ w += 1 while w < le && " \t".include?(self[w])
207
+ if w > e
208
+ e = w
209
+ else
210
+ s -= 1 while s > ls && " \t".include?(self[s - 1])
211
+ end
212
+ [s, e, false]
213
+ end
214
+
215
+ def tobj_bracket(pos, inner, op, cl, count)
216
+ o = tobj_find_open(pos, op, cl, count)
217
+ if o.nil? && count == 1
218
+ # Not inside a block: use the next one on the line (as vim 9.1)
219
+ _ls, le = tobj_line_bounds(pos)
220
+ nx = index(op, pos)
221
+ o = nx if nx && nx < le
222
+ end
223
+ return nil if o.nil?
224
+ c = tobj_match_close(o, op, cl)
225
+ return nil if c.nil?
226
+ return [o, c + 1, false] if !inner
227
+ s, e = o + 1, c
228
+ # "(" ending its line / ")" first on its line: those lines aren't part
229
+ # of the inner text, so di{ on a multi-line block keeps "{\n}".
230
+ starts_line = self[s] == "\n"
231
+ s += 1 if starts_line
232
+ nl = rindex("\n", c - 1)
233
+ ends_line = nl && nl >= o + 1 && self[(nl + 1)...c].match?(/\A[ \t]*\z/)
234
+ e = nl + 1 if ends_line
235
+ e = s if e < s
236
+ [s, e, starts_line && ends_line && e > s]
237
+ end
238
+
239
+ # Position of the count'th unmatched op enclosing pos (pos itself if on op)
240
+ def tobj_find_open(pos, op, cl, count)
241
+ o = nil
242
+ i = pos - 1
243
+ if self[pos] == op
244
+ o = pos
245
+ count -= 1
246
+ end
247
+ while count > 0
248
+ depth = 0
249
+ found = nil
250
+ while i >= 0
251
+ ch = self[i]
252
+ if ch == cl
253
+ depth += 1
254
+ elsif ch == op
255
+ if depth == 0
256
+ found = i
257
+ break
258
+ end
259
+ depth -= 1
260
+ end
261
+ i -= 1
262
+ end
263
+ return nil if found.nil?
264
+ o = found
265
+ count -= 1
266
+ i = found - 1
267
+ end
268
+ o
269
+ end
270
+
271
+ def tobj_match_close(o, op, cl)
272
+ depth = 0
273
+ i = o + 1
274
+ while i < size
275
+ ch = self[i]
276
+ if ch == op
277
+ depth += 1
278
+ elsif ch == cl
279
+ return i if depth == 0
280
+ depth -= 1
281
+ end
282
+ i += 1
283
+ end
284
+ nil
285
+ end
286
+
287
+ def tobj_tag(pos, inner, count)
288
+ pairs = [] # [outer start, inner start, inner end, outer end]
289
+ stack = []
290
+ to_s.scan(/<(\/?)([A-Za-z][\w:.-]*)[^<>]*?(\/?)>/) {
291
+ m = $~
292
+ next if m[3] == "/" # <br/>
293
+ if m[1] == ""
294
+ stack << [m[2], m.begin(0), m.end(0)]
295
+ elsif (k = stack.rindex { |t| t[0] == m[2] })
296
+ t = stack[k]
297
+ stack = stack[0...k] # drops unclosed tags inside, e.g. <br>
298
+ pairs << [t[1], t[2], m.begin(0), m.end(0)]
299
+ end
300
+ }
301
+ enc = pairs.select { |os, _is, _ie, oe| os <= pos && pos < oe }.sort_by { |os, _is, _ie, oe| oe - os }
302
+ t = enc[count - 1]
303
+ return nil if t.nil?
304
+ inner ? [t[1], t[2], false] : [t[0], t[3], false]
305
+ end
306
+
307
+ public
308
+ end
309
+
310
+ 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
@@ -19,9 +21,17 @@ end
19
21
  Converter.new(lambda { |x| x.split("\n").collect { |x| r = x.strip }.select { |y| !y.empty? }.join(" ") + "\n" }, :lambda, :joinlines)
20
22
 
21
23
  Converter.new(lambda { |x| x.split("\n").sort.join("\n") }, :lambda, :sortlines)
24
+
25
+ # Remove empty lines (also those with only spaces or tabs); the line ends of
26
+ # the others stay as they are
27
+ Converter.new(lambda { |x| x.lines.reject { |l| l.strip.empty? }.join }, :lambda, :remove_empty_lines)
22
28
  Converter.new(lambda { |x| x.split("\n").reverse.join("\n") }, :lambda, :reverse_lines)
23
29
 
24
30
  Converter.new(lambda { |x| x.split(/\s+/).sort.join(" ") }, :lambda, :sortwords)
31
+
32
+ # Collapse runs of spaces/tabs to one space and strip trailing whitespace on
33
+ # each line. Leading indentation is kept.
34
+ Converter.new(lambda { |x| x.gsub(/^([ \t]*)(.*?)[ \t]*$/) { $1 + $2.gsub(/[ \t]+/, " ") } }, :lambda, :remove_extra_spaces)
25
35
  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
36
 
27
37
  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 +44,14 @@ Converter.new(lambda { |x|
34
44
 
35
45
  Converter.new(lambda { |x| x.scan(/[\w\.]+@[\w\.]+/).join("\n") }, :lambda, :get_emails)
36
46
 
47
+ Converter.new(lambda { |x| x.scan(/https?:\/\/[^\s\])"'>]+/).join("\n") }, :lambda, :get_urls)
48
+
37
49
  # Eval selection as ruby code
38
50
  Converter.new(lambda { |x| b = "eval failed"; begin; b = eval(x, TOPLEVEL_BINDING); rescue; end; "#{x}\n#{b}\n" }, :lambda, :eval)
39
51
 
52
+ Converter.new(lambda { |x|
53
+ items = x.split("\n").map(&:strip).reject(&:empty?)
54
+ "\\begin{itemize}\n" + items.map { |line| " \\item #{line}" }.join("\n") + "\n\\end{itemize}\n"
55
+ }, :lambda, :latex_itemize)
56
+
57
+ end # module Vimamsa
@@ -0,0 +1,39 @@
1
+ require "tmpdir"
2
+ require "fileutils"
3
+
4
+ module Vimamsa
5
+ # vimamsa --tmp-user-dir: a new, empty config/data dir in the system temp
6
+ # dir, deleted when the editor quits (exe/vimamsa). An editor restart
7
+ # (restart_editor) keeps it: the new process gets it in ENV_KEY and deletes
8
+ # it in its turn. Only dirs made here are ever deleted (ours?).
9
+ module TmpUserDir
10
+ PREFIX = "vimamsa-user-"
11
+ ENV_KEY = "VIMAMSA_TMP_USER_DIR"
12
+
13
+ module_function
14
+
15
+ def create
16
+ Dir.mktmpdir(PREFIX)
17
+ end
18
+
19
+ # Made by create: directly in the system temp dir, named PREFIX...
20
+ def ours?(dir)
21
+ return false if dir.nil? || dir.to_s.empty?
22
+ d = File.expand_path(dir)
23
+ File.dirname(d) == File.expand_path(Dir.tmpdir) && File.basename(d).start_with?(PREFIX)
24
+ end
25
+
26
+ # Deletes dir, unless it holds a crash report (crash_handler.rb writes
27
+ # them into the user dir): then it stays, and is named on stderr
28
+ def delete(dir)
29
+ return false if !ours?(dir) || !File.directory?(dir)
30
+ reports = Dir[File.join(dir, "crash", "crash_*")]
31
+ if reports.any?
32
+ warn "vimamsa: kept the temporary user dir #{dir}: it has a crash report (#{File.basename(reports.max)})"
33
+ return false
34
+ end
35
+ FileUtils.rm_rf(dir)
36
+ true
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,83 @@
1
+ require "fileutils"
2
+
3
+ module Vimamsa
4
+ # Where vimamsa keeps its files:
5
+ # config dir files the user edits: settings.rb, custom.rb, styles/,
6
+ # bookmarks, named macros. $XDG_CONFIG_HOME/vimamsa, by
7
+ # default ~/.config/vimamsa (vma_user_dir, get_dot_path).
8
+ # data dir files the program generates: session, buffer list, histories,
9
+ # file index, recorded macros, backups, crash reports, the
10
+ # listen dir. $XDG_DATA_HOME/vimamsa, by default
11
+ # ~/.local/share/vimamsa (vma_data_dir, get_data_path).
12
+ # With --user-dir DIR (and in tests, and with --tmp-user-dir) both are DIR, so
13
+ # those runs touch nothing else. Loaded by exe/vimamsa before the rest, as its
14
+ # check for a running editor uses the listen dir.
15
+ module UserDirs
16
+ # Files and dirs that moved from the config dir to the data dir, by name
17
+ MOVED_TO_DATA = %w[
18
+ session.txt buffers.txt cmd_history.txt dictation_prompts.json
19
+ file_history file_index file_panel_shown macros backup crash
20
+ ]
21
+
22
+ module_function
23
+
24
+ def default_config_dir
25
+ base = ENV["XDG_CONFIG_HOME"].to_s
26
+ base = File.expand_path("~/.config") if base.empty?
27
+ File.join(File.expand_path(base), "vimamsa")
28
+ end
29
+
30
+ def default_data_dir
31
+ base = ENV["XDG_DATA_HOME"].to_s
32
+ base = File.expand_path("~/.local/share") if base.empty?
33
+ File.join(File.expand_path(base), "vimamsa")
34
+ end
35
+
36
+ # The default config dir, or one given by --user-dir?
37
+ def default_config_dir?(dir)
38
+ File.expand_path(dir) == default_config_dir
39
+ end
40
+
41
+ # The data dir that goes with config dir dir
42
+ def data_dir_for(dir)
43
+ default_config_dir?(dir) ? default_data_dir : File.expand_path(dir)
44
+ end
45
+
46
+ # One-time move of the files of older versions to their new places, when
47
+ # the new place has nothing yet: the generated files from the config dir to
48
+ # the data dir, and from the even older ~/.vimamsa the command history and
49
+ # backups (its other files are older copies of those in the config dir);
50
+ # macros saved with save_macro from ~/.config/.vimamsa to the config dir.
51
+ # A dir whose new place exists already is merged file by file, a file that
52
+ # is in both places is left where it was. Nothing is deleted, except the
53
+ # old listen dir if empty. Returns [[from, to], ...] of what was moved.
54
+ def migrate(config_dir: default_config_dir, data_dir: default_data_dir,
55
+ legacy_dir: File.expand_path("~/.vimamsa"),
56
+ legacy_macro_dir: File.expand_path("~/.config/.vimamsa"))
57
+ return [] if File.expand_path(config_dir) == File.expand_path(data_dir)
58
+ moved = []
59
+ MOVED_TO_DATA.each { |n| move(File.join(config_dir, n), File.join(data_dir, n), moved) }
60
+ %w[cmd_history.txt backup].each { |n| move(File.join(legacy_dir, n), File.join(data_dir, n), moved) }
61
+ Dir[File.join(legacy_macro_dir, "macro_*.rb")].each { |f| move(f, File.join(config_dir, File.basename(f)), moved) }
62
+ old_listen = File.join(config_dir, "listen")
63
+ Dir.rmdir(old_listen) if File.directory?(old_listen) && Dir.empty?(old_listen)
64
+ moved
65
+ rescue SystemCallError => e
66
+ warn "vimamsa: moving old files to #{data_dir} stopped: #{e.message}"
67
+ moved
68
+ end
69
+
70
+ # Moves from to to: a file only if to doesn't exist; a dir whole, or its
71
+ # entries one by one into an existing dir
72
+ def move(from, to, moved)
73
+ return if !File.exist?(from)
74
+ if !File.exist?(to)
75
+ FileUtils.mkdir_p(File.dirname(to))
76
+ FileUtils.mv(from, to)
77
+ moved << [from, to]
78
+ elsif File.directory?(from) && File.directory?(to)
79
+ Dir.children(from).each { |c| move(File.join(from, c), File.join(to, c), moved) }
80
+ end
81
+ end
82
+ end
83
+ end
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.28"
3
3
  end