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,93 @@
1
+ Copyright 2019 The Oxanium Project Authors (https://github.com/sevmeyer/oxanium)
2
+
3
+ This Font Software is licensed under the SIL Open Font License, Version 1.1.
4
+ This license is copied below, and is also available with a FAQ at:
5
+ https://scripts.sil.org/OFL
6
+
7
+
8
+ -----------------------------------------------------------
9
+ SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
10
+ -----------------------------------------------------------
11
+
12
+ PREAMBLE
13
+ The goals of the Open Font License (OFL) are to stimulate worldwide
14
+ development of collaborative font projects, to support the font creation
15
+ efforts of academic and linguistic communities, and to provide a free and
16
+ open framework in which fonts may be shared and improved in partnership
17
+ with others.
18
+
19
+ The OFL allows the licensed fonts to be used, studied, modified and
20
+ redistributed freely as long as they are not sold by themselves. The
21
+ fonts, including any derivative works, can be bundled, embedded,
22
+ redistributed and/or sold with any software provided that any reserved
23
+ names are not used by derivative works. The fonts and derivatives,
24
+ however, cannot be released under any other type of license. The
25
+ requirement for fonts to remain under this license does not apply
26
+ to any document created using the fonts or their derivatives.
27
+
28
+ DEFINITIONS
29
+ "Font Software" refers to the set of files released by the Copyright
30
+ Holder(s) under this license and clearly marked as such. This may
31
+ include source files, build scripts and documentation.
32
+
33
+ "Reserved Font Name" refers to any names specified as such after the
34
+ copyright statement(s).
35
+
36
+ "Original Version" refers to the collection of Font Software components as
37
+ distributed by the Copyright Holder(s).
38
+
39
+ "Modified Version" refers to any derivative made by adding to, deleting,
40
+ or substituting -- in part or in whole -- any of the components of the
41
+ Original Version, by changing formats or by porting the Font Software to a
42
+ new environment.
43
+
44
+ "Author" refers to any designer, engineer, programmer, technical
45
+ writer or other person who contributed to the Font Software.
46
+
47
+ PERMISSION & CONDITIONS
48
+ Permission is hereby granted, free of charge, to any person obtaining
49
+ a copy of the Font Software, to use, study, copy, merge, embed, modify,
50
+ redistribute, and sell modified and unmodified copies of the Font
51
+ Software, subject to the following conditions:
52
+
53
+ 1) Neither the Font Software nor any of its individual components,
54
+ in Original or Modified Versions, may be sold by itself.
55
+
56
+ 2) Original or Modified Versions of the Font Software may be bundled,
57
+ redistributed and/or sold with any software, provided that each copy
58
+ contains the above copyright notice and this license. These can be
59
+ included either as stand-alone text files, human-readable headers or
60
+ in the appropriate machine-readable metadata fields within text or
61
+ binary files as long as those fields can be easily viewed by the user.
62
+
63
+ 3) No Modified Version of the Font Software may use the Reserved Font
64
+ Name(s) unless explicit written permission is granted by the corresponding
65
+ Copyright Holder. This restriction only applies to the primary font name as
66
+ presented to the users.
67
+
68
+ 4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
69
+ Software shall not be used to promote, endorse or advertise any
70
+ Modified Version, except to acknowledge the contribution(s) of the
71
+ Copyright Holder(s) and the Author(s) or with their explicit written
72
+ permission.
73
+
74
+ 5) The Font Software, modified or unmodified, in part or in whole,
75
+ must be distributed entirely under this license, and must not be
76
+ distributed under any other license. The requirement for fonts to
77
+ remain under this license does not apply to any document created
78
+ using the Font Software.
79
+
80
+ TERMINATION
81
+ This license becomes null and void if any of the above conditions are
82
+ not met.
83
+
84
+ DISCLAIMER
85
+ THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
86
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
87
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
88
+ OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
89
+ COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
90
+ INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
91
+ DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
92
+ FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
93
+ OTHER DEALINGS IN THE FONT SOFTWARE.
Binary file
data/icon.png ADDED
Binary file
data/img/demo.gif ADDED
Binary file
@@ -14,6 +14,7 @@
14
14
  <style id="heading3" name="heading4" map-to="def:heading3"/>
15
15
  <style id="heading4" name="heading4" map-to="def:heading4"/>
16
16
  <style id="bold" name="bold" map-to="def:bold"/>
17
+ <style id="italic" name="italic" map-to="def:emphasis"/>
17
18
  <style id="string" name="String" map-to="def:string"/>
18
19
  <style id="floating-point" name="Floating point number" map-to="def:floating-point"/>
19
20
  </styles>
@@ -69,6 +70,10 @@
69
70
  <context id="bold" style-ref="bold">
70
71
  <match>(⦁.*⦁)</match>
71
72
  </context>
73
+
74
+ <context id="italic" style-ref="italic">
75
+ <match>(╱[^╱\n]*╱)</match>
76
+ </context>
72
77
 
73
78
  <context id="hyperlink" style-ref="hyperlink">
74
79
  <match>(⟦.*⟧)</match>
@@ -105,6 +110,7 @@
105
110
  <context ref="heading3"/>
106
111
  <context ref="heading4"/>
107
112
  <context ref="bold"/>
113
+ <context ref="italic"/>
108
114
  <context ref="float"/>
109
115
  </include>
110
116
  </context>
data/lib/vimamsa/ack.rb CHANGED
@@ -1,3 +1,5 @@
1
+
2
+ module Vimamsa
1
3
  # Interface for ack! https://beyondgrep.com/
2
4
 
3
5
  class FileContentSearch
@@ -145,8 +147,14 @@ class Ack
145
147
  instr = Shellwords.escape(_instr)
146
148
  bufstr = ""
147
149
  for path in vma.get_content_search_paths
148
- bufstr += run_cmd("ack -Q --type-add=gd=.gd -ki --nohtml --nojs --nojson #{instr} #{path}")
150
+ # Skip editor autosave files (.<name>_vma_autosave, see Buffer#autosave_path)
151
+ # stdin from /dev/null: ack 3 reads a piped stdin instead of the given
152
+ # path, which hangs forever if the editor was started with stdin a pipe.
153
+ bufstr += run_cmd("ack -Q --type-add=gd=.gd -ki --nohtml --nojs --nojson --ignore-file=match:/_vma_autosave$/ #{instr} #{Shellwords.escape(path)} < /dev/null")
149
154
  end
155
+ # Matches from non-UTF-8 files (e.g. latin-1) would make the regexp below
156
+ # raise "invalid byte sequence", which kills the app from a GTK callback.
157
+ bufstr = bufstr.scrub
150
158
 
151
159
  b = ""
152
160
  list = []
@@ -155,6 +163,7 @@ class Ack
155
163
  fn = $1
156
164
  lineno = $2
157
165
  matchpart = $3
166
+ next if fn.end_with?("_vma_autosave")
158
167
  pp [fn, lineno, matchpart]
159
168
  b << "[#{fn},#{lineno},#{matchpart}]\n"
160
169
  list << { fpath: fn, lineno: lineno, matchpart: matchpart }
@@ -192,3 +201,4 @@ class Ack
192
201
  jump_to_file(nfo[:fpath], nfo[:lineno].to_i)
193
202
  end
194
203
  end
204
+ end # module Vimamsa
@@ -1,3 +1,5 @@
1
+
2
+ module Vimamsa
1
3
  class Action
2
4
  attr_accessor :id, :method_name, :method, :opt
3
5
 
@@ -6,7 +8,6 @@ class Action
6
8
  @id = id
7
9
  @method = method
8
10
  @opt = opt
9
- vma.actions.register(id, self) # TODO: handle this in Editor class
10
11
  end
11
12
  end
12
13
 
@@ -25,6 +26,7 @@ def reg_act(id, callfunc, name = "", opt = {})
25
26
  end
26
27
  a = Action.new(id, name, m, opt)
27
28
  end
29
+ vma.actions.register(id, a)
28
30
  return a
29
31
  end
30
32
 
@@ -159,3 +161,4 @@ class ActionList
159
161
  return scores
160
162
  end
161
163
  end
164
+ end # module Vimamsa
@@ -0,0 +1,35 @@
1
+ module Vimamsa
2
+
3
+ # From the AppImage, AppRun points GI_TYPELIB_PATH, GIO_MODULE_DIR, GST_*, ...
4
+ # at the bundle and unsets GEM_HOME, RUBYOPT, ... (scripts/appimage/AppRun). It
5
+ # lists those names in VMA_APPIMAGE_ENV and keeps each one's previous value in
6
+ # VMA_HOST_<name>. GLib, GIO, GObject Introspection and GStreamer read them once
7
+ # and keep what they read, so after startup the host's values go back into ENV.
8
+ # Programs the editor starts (git, a terminal, the user's own Ruby or Python
9
+ # GTK programs) then get the user's environment, not the bundle's typelibs,
10
+ # modules and gem settings. PATH keeps the bundle's ruby-lsp/rufo wrappers.
11
+ def restore_host_env
12
+ names = ENV["VMA_APPIMAGE_ENV"]
13
+ return if names.nil?
14
+ # Read on first use, so use them before the bundle paths go away
15
+ GdkPixbuf::Pixbuf.formats if defined?(GdkPixbuf::Pixbuf)
16
+ Gio::SettingsSchemaSource.default
17
+ Gio::Vfs.default
18
+ # GStreamer may only be loaded later (cnf.audio, the dictation module), but
19
+ # finds the bundled plugins only through GST_* (the plugin registry is
20
+ # cached in ~/.cache/vimamsa, so this is quick after the first start)
21
+ require "gstreamer"
22
+ Gst.init if Gst.respond_to?(:init)
23
+ names.split.each { |n|
24
+ host = "VMA_HOST_#{n}"
25
+ if ENV.key?(host)
26
+ ENV[n] = ENV.delete(host)
27
+ else
28
+ ENV.delete(n)
29
+ end
30
+ }
31
+ ENV.delete("VMA_APPIMAGE_ENV")
32
+ debug "Restored the host environment for child processes: #{names}"
33
+ end
34
+
35
+ end # module Vimamsa
data/lib/vimamsa/audio.rb CHANGED
@@ -1,10 +1,6 @@
1
1
  require "gstreamer"
2
2
 
3
- class File
4
- def self.exists?(fn)
5
- File.exist?(fn)
6
- end
7
- end
3
+ module Vimamsa
8
4
 
9
5
  # following the example gstreamer-4.2.0/sample/helloworld_e.rb
10
6
  class Audio
@@ -80,3 +76,4 @@ class Audio
80
76
  end
81
77
  end
82
78
 
79
+ end # module Vimamsa
@@ -0,0 +1,302 @@
1
+
2
+ module Vimamsa
3
+ # Persistent bookmarks across files.
4
+ # , M add bookmark at cursor
5
+ # , ' list bookmarks (newest first): <enter> jump, <d> delete, <x> exit
6
+ #
7
+ # Saved to get_dot_path("bookmarks") so they survive restarts. Each bookmark
8
+ # stores the text of its line (and the nearest non-blank lines around it), so
9
+ # if the file has changed a jump finds the line again with a regexp instead of
10
+ # trusting the stored line number.
11
+ #
12
+ # While a bookmarked file is open, its position is also kept in the buffer's
13
+ # @marks hash (key "bm:<id>"), which Buffer#update_index shifts on edits. On
14
+ # save the stored line/text is refreshed from there.
15
+ class Bookmarks
16
+ attr_reader :list
17
+
18
+ def self.init()
19
+ vma.kbd.add_minor_mode("bmark", :bmark, :command)
20
+ reg_act(:bookmark_add, proc { vma.bookmarks.add }, "Add bookmark at cursor (persistent, across files)")
21
+ reg_act(:bookmark_list, proc { BookmarkList.new.run; vma.kbd.set_mode(:bmark) }, "List bookmarks")
22
+ reg_act(:bmark_select, proc { buf.module.select_line }, "")
23
+ reg_act(:bmark_delete, proc { buf.module.delete_selected }, "")
24
+
25
+ bindkey "bmark enter", :bmark_select
26
+ bindkey "bmark d", :bmark_delete
27
+ bindkey "bmark x", :close_current_buffer
28
+ end
29
+
30
+ def initialize()
31
+ load_list
32
+ vma.hook.register(:change_buffer, self.method("track_buffer"))
33
+ vma.hook.register(:file_saved, self.method("file_saved"))
34
+ vma.hook.register(:shutdown, self.method("save"))
35
+ # Buffers opened before this plugin was created (files from ARGV)
36
+ vma.buffers.list.each { |b| track_buffer(b) }
37
+ end
38
+
39
+ def load_list()
40
+ @list = vma.marshal_load("bookmarks", [])
41
+ @list = [] if !@list.is_a?(Array)
42
+ @next_id = (@list.collect { |bm| bm[:id].to_i }.max || 0) + 1
43
+ end
44
+
45
+ def save()
46
+ vma.marshal_save("bookmarks", @list)
47
+ rescue => ex
48
+ debug "Bookmarks.save failed: #{ex}"
49
+ end
50
+
51
+ # Newest first
52
+ def sorted_list()
53
+ @list.sort_by { |bm| bm[:created] || Time.at(0) }.reverse
54
+ end
55
+
56
+ def add(b = vma.buf)
57
+ if b.nil? or b.fname.nil?
58
+ message("Bookmark: buffer is not saved to a file")
59
+ return nil
60
+ end
61
+ lpos = b.lpos
62
+ # Re-marking the same line replaces the old bookmark
63
+ @list.select { |bm| bm[:fname] == b.fname && bm[:lpos] == lpos }.each { |bm| delete(bm, false) }
64
+
65
+ bm = { id: @next_id, fname: b.fname, created: Time.now }
66
+ @next_id += 1
67
+ lines = b.to_s.lines
68
+ bm.merge!(snapshot(lines, lpos, b.cpos))
69
+ @list << bm
70
+ b.marks[mark_key(bm)] = anchor_pos(b, lpos, lines)
71
+ save
72
+ message("Bookmark added: #{tilde_path(b.fname)}:#{lpos + 1}")
73
+ return bm
74
+ end
75
+
76
+ def delete(bm, do_save = true)
77
+ b = buffer_for(bm[:fname])
78
+ b.marks.delete(mark_key(bm)) if b
79
+ @list.delete(bm)
80
+ save if do_save
81
+ end
82
+
83
+ # Open the bookmarked file and put the cursor on the bookmarked line.
84
+ # Returns false if the file could not be opened.
85
+ def jump(bm)
86
+ if !File.exist?(bm[:fname])
87
+ message("Bookmark file not found: #{bm[:fname]}")
88
+ return false
89
+ end
90
+ open_new_file(bm[:fname])
91
+ b = vma.buf
92
+ return false if b.nil? or b.fname != bm[:fname] # e.g. large file confirm dialog
93
+
94
+ lines = b.to_s.lines
95
+ (lpos, found) = resolve_line(lines, bm)
96
+ message("Bookmark text not found, using line #{lpos + 1}") if !found
97
+ b.set_pos(line_start(b, lpos) + [bm[:cpos].to_i, [lines[lpos].to_s.chomp.size - 1, 0].max].min)
98
+ center_on_current_line
99
+
100
+ if found && lpos != bm[:lpos]
101
+ # Self-heal: remember where the line is now
102
+ bm[:lpos] = lpos
103
+ b.marks[mark_key(bm)] = anchor_pos(b, lpos, lines)
104
+ save
105
+ end
106
+ return true
107
+ end
108
+
109
+ # Find the bookmarked line in lines (Array of lines incl. "\n").
110
+ # Returns [lpos, found]. Among lines matching the stored text, the one whose
111
+ # surrounding context also matches wins; ties go to the one closest to the
112
+ # stored line number. If nothing matches, falls back to the stored line
113
+ # number (clamped) with found=false.
114
+ def resolve_line(lines, bm)
115
+ n = lines.size
116
+ return [0, false] if n == 0
117
+ hint = bm[:lpos].to_i
118
+ fallback = hint.clamp(0, n - 1)
119
+ re = text_regexp(bm[:text])
120
+ return [fallback, false] if re.nil?
121
+
122
+ best = nil
123
+ best_score = nil
124
+ lines.each_with_index do |l, i|
125
+ next if !l.match?(re)
126
+ score = (i - hint).abs
127
+ score -= n if bm[:before] && context_line(lines, i, -1) == bm[:before]
128
+ score -= n if bm[:after] && context_line(lines, i, 1) == bm[:after]
129
+ if best.nil? or score < best_score
130
+ best = i
131
+ best_score = score
132
+ end
133
+ end
134
+ return [best, true] if best
135
+ return [fallback, false]
136
+ end
137
+
138
+ # Whole-line match of text, ignoring indentation and amount of whitespace
139
+ def text_regexp(text)
140
+ t = text.to_s.strip
141
+ return nil if t.empty?
142
+ body = Regexp.escape(t).gsub(/(?:\\ |\\t)+/) { '\s+' }
143
+ return /\A\s*#{body}\s*\z/
144
+ end
145
+
146
+ # :change_buffer hook. Start tracking bookmarks of b's file in b.marks.
147
+ def track_buffer(b)
148
+ return if b.nil? or b.fname.nil?
149
+ lines = nil
150
+ for bm in @list
151
+ next if bm[:fname] != b.fname or b.marks.has_key?(mark_key(bm))
152
+ lines ||= b.to_s.lines
153
+ (lpos, found) = resolve_line(lines, bm)
154
+ # If the line is not found, leave it untracked so that a save does not
155
+ # overwrite the stored text with some unrelated line.
156
+ b.marks[mark_key(bm)] = anchor_pos(b, lpos, lines) if found
157
+ end
158
+ end
159
+
160
+ # :file_saved hook. Refresh stored line/text from the tracked positions.
161
+ def file_saved(b)
162
+ return if b.nil? or b.fname.nil?
163
+ lines = nil
164
+ changed = false
165
+ for bm in @list
166
+ next if bm[:fname] != b.fname
167
+ pos = b.marks[mark_key(bm)]
168
+ next if pos.nil?
169
+ lines ||= b.to_s.lines
170
+ next if lines.empty?
171
+ lpos = line_of_pos(b, pos)
172
+ bm.merge!(snapshot(lines, lpos, bm[:cpos]))
173
+ changed = true
174
+ end
175
+ save if changed
176
+ end
177
+
178
+ private
179
+
180
+ def mark_key(bm)
181
+ "bm:#{bm[:id]}"
182
+ end
183
+
184
+ def buffer_for(fname)
185
+ id = vma.buffers.get_buffer_by_filename(fname)
186
+ return nil if id.nil?
187
+ return vma.buffers.get_buffer_by_id(id)
188
+ end
189
+
190
+ def snapshot(lines, lpos, cpos)
191
+ { lpos: lpos, cpos: cpos,
192
+ text: lines[lpos].to_s.chomp,
193
+ before: context_line(lines, lpos, -1),
194
+ after: context_line(lines, lpos, 1) }
195
+ end
196
+
197
+ # Nearest non-blank line from lpos in direction dir (-1 or 1), stripped
198
+ def context_line(lines, lpos, dir)
199
+ i = lpos + dir
200
+ while i >= 0 && i < lines.size
201
+ t = lines[i].strip
202
+ return t if !t.empty?
203
+ i += dir
204
+ end
205
+ return nil
206
+ end
207
+
208
+ def line_start(b, lpos)
209
+ return 0 if lpos <= 0
210
+ return b.line_ends[lpos - 1] + 1
211
+ end
212
+
213
+ def line_of_pos(b, pos)
214
+ b.line_ends.bsearch_index { |e| e >= pos } || [b.line_ends.size - 1, 0].max
215
+ end
216
+
217
+ # Position stored in b.marks. Second char of the line (when there is one), so
218
+ # that inserting text at the line start (e.g. "O") shifts the mark along with
219
+ # the line (update_index only shifts marks after the insert position).
220
+ def anchor_pos(b, lpos, lines)
221
+ line_start(b, lpos) + (lines[lpos].to_s.chomp.empty? ? 0 : 1)
222
+ end
223
+ end
224
+
225
+ # List view for bookmarks, modelled on BufferManager
226
+ class BookmarkList
227
+ attr_reader :buf
228
+ @@cur = nil # Current object of class
229
+
230
+ def self.cur()
231
+ return @@cur
232
+ end
233
+
234
+ def initialize()
235
+ @buf = nil
236
+ @line_to_bm = {}
237
+ end
238
+
239
+ def bm_of_current_line()
240
+ return @line_to_bm[@buf.lpos]
241
+ end
242
+
243
+ def select_line()
244
+ bm = bm_of_current_line
245
+ return if bm.nil?
246
+ list_id = @buf.id
247
+ return if !vma.bookmarks.jump(bm) # Keep the list open if the jump failed
248
+ vma.buffers.close_other_buffer(list_id)
249
+ @@cur = nil
250
+ vma.kbd.set_mode(vma.kbd.default_mode)
251
+ end
252
+
253
+ def delete_selected()
254
+ bm = bm_of_current_line
255
+ return if bm.nil?
256
+ lpos = @buf.lpos
257
+ vma.bookmarks.delete(bm)
258
+ message("Bookmark deleted: #{tilde_path(bm[:fname])}:#{bm[:lpos] + 1}")
259
+ run
260
+ last = @header.size + [@line_to_bm.size - 1, 0].max
261
+ @buf.set_line_and_column_pos([lpos, last].min, 0)
262
+ end
263
+
264
+ def run()
265
+ if !@@cur.nil? && @@cur != self && !@@cur.buf.nil?
266
+ # One instance open already, close it
267
+ vma.buffers.close_buffer(@@cur.buf.id)
268
+ end
269
+ @@cur = self
270
+ @header = []
271
+ @header << "Bookmarks (newest first):"
272
+ @header << "keys: <enter> (or <double click>) to jump, <d> to delete, <x> exit"
273
+ @header << "=" * 40
274
+
275
+ s = ""
276
+ s << @header.join("\n")
277
+ s << "\n"
278
+ @line_to_bm = {}
279
+ list = vma.bookmarks.sorted_list
280
+ s << "(no bookmarks, add one with , M)\n" if list.empty?
281
+ list.each_with_index do |bm, i|
282
+ @line_to_bm[@header.size + i] = bm
283
+ s << row(bm) << "\n"
284
+ end
285
+
286
+ if @buf.nil?
287
+ @buf = create_new_buffer(s, "bookmarks")
288
+ @buf.default_mode = :bmark
289
+ @buf.module = self
290
+ @buf.active_kbd_mode = :bmark
291
+ else
292
+ @buf.set_content(s)
293
+ end
294
+ @buf.set_line_and_column_pos(@header.size, 0)
295
+ end
296
+
297
+ def row(bm)
298
+ t = bm[:created].respond_to?(:strftime) ? bm[:created].strftime("%Y-%m-%d %H:%M") : "?"
299
+ "#{t} #{tilde_path(bm[:fname])}:#{bm[:lpos] + 1} #{bm[:text].to_s.strip[0, 80]}"
300
+ end
301
+ end
302
+ end # module Vimamsa