vimamsa 0.1.27 → 0.1.29

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 (167) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/appimage.yml +17 -10
  3. data/Dockerfile +2 -2
  4. data/README.md +162 -90
  5. data/custom_example.rb +33 -15
  6. data/doc/TODO.md +334 -110
  7. data/doc/action_names_plan.md +442 -0
  8. data/doc/bundling_plan.md +2 -1
  9. data/doc/delta_syntax_plan.md +293 -0
  10. data/doc/demo_recording_plan.md +288 -0
  11. data/doc/design_goals.md +27 -0
  12. data/doc/intro_page_plan.md +266 -0
  13. data/doc/key_bindings_report_plan.md +45 -0
  14. data/doc/line_tree_plan.md +89 -0
  15. data/doc/missing_vim_bindings.md +34 -44
  16. data/doc/modules_settings_plan.md +1 -1
  17. data/doc/registries_plan.md +202 -0
  18. data/doc/startup_speed_plan.md +97 -0
  19. data/doc/test_commands.md +232 -0
  20. data/doc/treap_visualizer.html +585 -0
  21. data/doc/vim_compatibility_plan.md +186 -31
  22. data/doc/vimlike_vim_conflicts.md +236 -228
  23. data/doc/vm_snapshots_plan.md +4 -4
  24. data/doc/wysiwyg_plan.md +231 -0
  25. data/docker_cmd.sh +1 -1
  26. data/exe/run_tests.rb +85 -3
  27. data/exe/vimamsa +49 -6
  28. data/help/intro.txt +30 -0
  29. data/help/vim_intro.txt +31 -0
  30. data/img/demo.gif +0 -0
  31. data/img/demo.mp4 +0 -0
  32. data/lang/hyperplaintext.lang +1 -1
  33. data/lib/vimamsa/ack.rb +2 -0
  34. data/lib/vimamsa/actions.rb +381 -36
  35. data/lib/vimamsa/bookmarks.rb +8 -8
  36. data/lib/vimamsa/buffer.rb +84 -38
  37. data/lib/vimamsa/buffer_changetext.rb +58 -36
  38. data/lib/vimamsa/buffer_list.rb +2 -2
  39. data/lib/vimamsa/buffer_manager.rb +6 -6
  40. data/lib/vimamsa/cli_help.rb +75 -0
  41. data/lib/vimamsa/clipboard.rb +6 -3
  42. data/lib/vimamsa/conf.rb +9 -0
  43. data/lib/vimamsa/crash_handler.rb +2 -2
  44. data/lib/vimamsa/debug.rb +2 -2
  45. data/lib/vimamsa/demo_player.rb +494 -0
  46. data/lib/vimamsa/demo_recorder.rb +277 -0
  47. data/lib/vimamsa/diff_buffer.rb +1 -1
  48. data/lib/vimamsa/dot_repeat.rb +104 -0
  49. data/lib/vimamsa/easy_jump.rb +54 -3
  50. data/lib/vimamsa/editor.rb +136 -65
  51. data/lib/vimamsa/file_history.rb +1 -1
  52. data/lib/vimamsa/file_manager.rb +23 -23
  53. data/lib/vimamsa/form_input.rb +388 -0
  54. data/lib/vimamsa/gui.rb +89 -13
  55. data/lib/vimamsa/gui_command_line.rb +44 -5
  56. data/lib/vimamsa/gui_form_generator.rb +17 -6
  57. data/lib/vimamsa/gui_func_panel.rb +1 -1
  58. data/lib/vimamsa/gui_key_hint.rb +165 -0
  59. data/lib/vimamsa/gui_keylog_panel.rb +164 -2
  60. data/lib/vimamsa/gui_menu.rb +69 -50
  61. data/lib/vimamsa/gui_select_window.rb +77 -43
  62. data/lib/vimamsa/gui_settings.rb +9 -5
  63. data/lib/vimamsa/gui_sourceview.rb +28 -81
  64. data/lib/vimamsa/gui_theme.rb +11 -2
  65. data/lib/vimamsa/hook.rb +2 -0
  66. data/lib/vimamsa/intro_page.rb +367 -0
  67. data/lib/vimamsa/key_actions.rb +315 -225
  68. data/lib/vimamsa/key_binding_tree.rb +228 -61
  69. data/lib/vimamsa/key_bindings_notepad.rb +18 -16
  70. data/lib/vimamsa/key_bindings_report.rb +290 -67
  71. data/lib/vimamsa/key_bindings_vim.rb +43 -297
  72. data/lib/vimamsa/key_bindings_vim_core.rb +356 -0
  73. data/lib/vimamsa/key_bindings_vimlike.rb +347 -339
  74. data/lib/vimamsa/key_names.rb +80 -0
  75. data/lib/vimamsa/langservp.rb +62 -7
  76. data/lib/vimamsa/line_tree.rb +367 -0
  77. data/lib/vimamsa/macro.rb +20 -3
  78. data/lib/vimamsa/rbvma.rb +15 -0
  79. data/lib/vimamsa/registers.rb +125 -0
  80. data/lib/vimamsa/run_ruby.rb +64 -0
  81. data/lib/vimamsa/search_replace.rb +4 -4
  82. data/lib/vimamsa/string_util.rb +17 -3
  83. data/lib/vimamsa/tests.rb +8 -8
  84. data/lib/vimamsa/text_objects.rb +11 -54
  85. data/lib/vimamsa/text_transforms.rb +4 -0
  86. data/lib/vimamsa/tmp_user_dir.rb +39 -0
  87. data/lib/vimamsa/user_dirs.rb +83 -0
  88. data/lib/vimamsa/version.rb +1 -1
  89. data/lib/vimamsa/vim_operators.rb +365 -0
  90. data/modules/calculator/calculator.rb +5 -5
  91. data/modules/color_highlight/color_highlight.rb +3 -3
  92. data/modules/dictation/dictation.rb +32 -32
  93. data/modules/ollama/ollama.rb +5 -5
  94. data/modules/terminal/terminal.rb +5 -5
  95. data/modules/tts/tts.rb +10 -10
  96. data/run_e2e_vm.sh +3 -3
  97. data/run_tests_vm.rb +163 -0
  98. data/scripts/appimage/AppRun +7 -0
  99. data/scripts/appimage/build_in_container.sh +11 -1
  100. data/scripts/appimage/smoke_test.sh +1 -1
  101. data/scripts/demo/demo_easyjump_steps.txt +60 -0
  102. data/scripts/demo/demo_files_steps.txt +52 -0
  103. data/scripts/demo/demo_steps.txt +9 -1
  104. data/scripts/demo/notes.txt +6 -0
  105. data/scripts/demo/record_demo.rb +60 -81
  106. data/scripts/demo/report.rb +7 -0
  107. data/scripts/demo/shapes.rb +1 -0
  108. data/scripts/demo/square/shapes.rb +29 -0
  109. data/scripts/demo/square/steps.txt +185 -0
  110. data/scripts/lib/perf_startup.rb +138 -0
  111. data/scripts/lib/script_helpers.rb +53 -0
  112. data/scripts/perf_compare.rb +34 -0
  113. data/scripts/profile_startup.rb +72 -0
  114. data/tests/test_action_ids.rb +232 -0
  115. data/tests/test_auto_format.rb +2 -2
  116. data/tests/test_autocomplete.rb +1 -1
  117. data/tests/test_basic_editing.rb +18 -18
  118. data/tests/test_bookmarks.rb +6 -6
  119. data/tests/test_bracket_match.rb +3 -0
  120. data/tests/test_cli_help.rb +64 -0
  121. data/tests/test_color_highlight.rb +2 -2
  122. data/tests/test_command_line.rb +1 -1
  123. data/tests/test_context_menu_file.rb +18 -0
  124. data/tests/test_copy_paste.rb +33 -33
  125. data/tests/test_custom_two_phase.rb +1 -1
  126. data/tests/test_demo_player.rb +355 -0
  127. data/tests/test_demo_recorder.rb +202 -0
  128. data/tests/test_dictation.rb +16 -16
  129. data/tests/test_dot_repeat.rb +88 -0
  130. data/tests/test_easy_jump.rb +93 -0
  131. data/tests/test_extract_file_links.rb +3 -3
  132. data/tests/test_file_panel.rb +2 -2
  133. data/tests/test_flash.rb +1 -1
  134. data/tests/test_fold.rb +1 -1
  135. data/tests/test_form_input.rb +203 -0
  136. data/tests/test_func_panel.rb +2 -2
  137. data/tests/test_gtk_sync.rb +7 -7
  138. data/tests/test_intro_page.rb +437 -0
  139. data/tests/test_key_categories.rb +163 -0
  140. data/tests/test_keylog_panel.rb +163 -3
  141. data/tests/test_line_ends.rb +51 -0
  142. data/tests/test_line_tree.rb +86 -0
  143. data/tests/test_lsp_clangd.rb +2 -2
  144. data/tests/test_lsp_root.rb +43 -0
  145. data/tests/test_macro.rb +2 -2
  146. data/tests/test_module_interface.rb +9 -9
  147. data/tests/test_notepad_bindings.rb +5 -5
  148. data/tests/test_perf_startup.rb +28 -0
  149. data/tests/test_random_edit.rb +27 -27
  150. data/tests/test_registers.rb +131 -0
  151. data/tests/test_run_ruby.rb +77 -0
  152. data/tests/test_startup_file.rb +45 -4
  153. data/tests/test_text_objects.rb +1 -1
  154. data/tests/test_text_transforms.rb +12 -0
  155. data/tests/test_tmp_user_dir.rb +79 -0
  156. data/tests/test_tts.rb +10 -10
  157. data/tests/test_undo.rb +34 -34
  158. data/tests/test_user_dirs.rb +122 -0
  159. data/tests/test_vim_bindings.rb +107 -6
  160. data/tests/test_vim_conflicts.rb +41 -4
  161. data/tests/test_vim_operators.rb +116 -0
  162. data/tests/test_window_layout.rb +83 -0
  163. data/vimamsa.gemspec +1 -1
  164. data/vm_guest_run.sh +75 -28
  165. metadata +60 -4
  166. data/run_tests.rb +0 -89
  167. data/run_tests_vm.sh +0 -130
@@ -22,6 +22,12 @@ module Vimamsa
22
22
 
23
23
  class State
24
24
  attr_accessor :key_name, :eval_rule, :children, :action, :label, :major_modes, :level, :cursor_type, :keywords, :root, :parent
25
+ # Order in which the action was bound (KeyBindingTree#bindings lists by it)
26
+ attr_accessor :seq
27
+ # For a binding expanded from a template ("C <op> <motion>"): the values it
28
+ # was expanded from ({ op: :delete, motion: :word_next }), passed to the
29
+ # action, and the template keys
30
+ attr_accessor :bind_args, :template
25
31
  attr_reader :cur_mode, :scope
26
32
 
27
33
  def initialize(key_name, eval_rule = "", ctype = :command, scope: :buffer)
@@ -47,6 +53,12 @@ class KeyBindingTree
47
53
  attr_accessor :C, :I, :cur_state, :root, :match_state, :last_action, :cur_action, :modifiers, :next_command_count, :method_handles_repeat, :default_mode
48
54
  attr_accessor :action_handler, :on_action_error, :logger, :fatal_handler, :error_handler
49
55
  attr_reader :mode_root_state, :state_trail, :act_bindings, :default_mode_stack, :actions
56
+ # [mode, keys] of the shared vim bindings bound by bind_vim_bindings (key_bindings_vim_core.rb)
57
+ attr_accessor :vim_added
58
+ # Keywords added to every binding made inside with_keywords
59
+ attr_reader :extra_keywords
60
+ # Key sets ({ name => { keys => value } }) and the templates bound with them
61
+ attr_reader :key_sets, :templates
50
62
 
51
63
  # This class is host-agnostic: it never touches the editor or GUI directly.
52
64
  # The host injects collaborators (actions registry, action_handler, logger,
@@ -59,8 +71,13 @@ class KeyBindingTree
59
71
  # :key_action(trail, action) - action about to be executed
60
72
  # :action_executed(action, ret) - one execution of an action finished
61
73
  # :action_handled(trail, action) - key handling for an action completed
74
+ # :key_input(key, event_type) - a key came in (match_key_conf), first
62
75
  def initialize(actions: nil)
63
76
  @actions = actions
77
+ @extra_keywords = []
78
+ @bind_seq = 0
79
+ @key_sets = {}
80
+ @templates = []
64
81
  @listeners = {}
65
82
  @action_handler = nil
66
83
  @logger = nil
@@ -205,6 +222,11 @@ class KeyBindingTree
205
222
  @override_keyhandling_callback = _callback
206
223
  end
207
224
 
225
+ # True while something reads the keys itself (easy jump's labels)
226
+ def keyhandling_override?
227
+ !@override_keyhandling_callback.nil?
228
+ end
229
+
208
230
  def remove_keyhandling_override()
209
231
  @override_keyhandling_callback = nil
210
232
  end
@@ -371,55 +393,56 @@ class KeyBindingTree
371
393
  }.join("\n")
372
394
  end
373
395
 
374
- def get_by_keywords(modes: [], keywords: [])
375
- s = ""
376
- stack = [[@root, ""]]
377
- lines = []
378
-
379
- # Traverse the tree (class State objects) using a stack
380
- while stack.any?
381
- t, p = *stack.pop # t = current state, p = current path
382
- if t.children.any?
383
- t.children.reverse.each { |c|
384
-
385
- # Restrict by modes if specified
386
- if c.level == 1 and !modes.empty?
387
- next if !modes.include?(c.key_name)
388
- end
389
-
390
- if c.eval_rule.size > 0
391
- new_p = "#{p} #{c.key_name}(#{c.eval_rule})"
392
- else
393
- if c.level == 1
394
- new_p = "#{p} [#{c.key_name}]"
395
- else
396
- new_p = "#{p} #{c.key_name}"
397
- end
398
- end
399
- stack << [c, new_p]
400
- }
401
- # stack.concat[t.children]
396
+ # Run the block with kws added to the keywords of every binding it makes,
397
+ # e.g. with_keywords(["module:tts"]) { tts_init }
398
+ def with_keywords(kws)
399
+ old = @extra_keywords
400
+ @extra_keywords = old + kws
401
+ yield
402
+ ensure
403
+ @extra_keywords = old
404
+ end
402
405
 
403
- else
404
- method_desc = t.action
405
- if t.action.class == Symbol
406
- if @actions&.include?(t.action)
407
- a = @actions[t.action].method_name
408
- if !a.nil? and !a.empty?
409
- method_desc = a
410
- end
411
- end
406
+ # Every binding as a record, in definition order:
407
+ # { mode: "C", keys: ["d", "i", "<char>"], action:, keywords: [...], desc:, seq: }
408
+ # seq counts bindkey calls; rebinding keys moves them to the end.
409
+ # A key with a condition is "name(rule)", as in the binding syntax. desc is
410
+ # the action's description, or the action itself (Ruby code, an unknown id).
411
+ def bindings
412
+ out = []
413
+ walk = lambda { |st, mode, path|
414
+ st.children.each { |c|
415
+ p = path + [c.eval_rule.empty? ? c.key_name : "#{c.key_name}(#{c.eval_rule})"]
416
+ if !c.action.nil?
417
+ out << { mode: mode, keys: p, action: c.action, keywords: c.keywords || [], desc: action_desc(c.action), seq: c.seq || 0,
418
+ args: c.bind_args, template: c.template }
412
419
  end
420
+ walk.(c, mode, p) if c.children.any?
421
+ }
422
+ }
423
+ @root.children.each { |m| walk.(m, m.key_name, []) }
424
+ out.each_with_index.sort_by { |b, i| [b[:seq], i] }.map(&:first)
425
+ end
413
426
 
414
- if (keywords - t.keywords).empty? # All keywords are present
415
- kw = ""
416
- # kw = ", [#{t.keywords.join(",")}]" if !t.keywords.empty?
417
- lines << p + " : #{method_desc}#{kw}"
418
- end
419
- end
427
+ # Description of a bound action, or the action as text
428
+ def action_desc(action)
429
+ if action.class == Proc
430
+ f, l = action.source_location
431
+ return "proc (#{File.basename(f.to_s)}:#{l})"
420
432
  end
421
- s = lines.sort.join("\n")
422
- return s
433
+ a = (@actions[action] rescue nil) if action.class == Symbol || action.class == String
434
+ if a && !a.opt&.[](:code) && !a.method_name.to_s.empty? && a.method_name.to_s != action.to_s
435
+ return a.method_name.to_s
436
+ end
437
+ action.to_s
438
+ end
439
+
440
+ # Lines "[C] keys : description" of the bindings in modes (all if empty)
441
+ # that have all of keywords, sorted
442
+ def get_by_keywords(modes: [], keywords: [])
443
+ bindings.select { |b|
444
+ (modes.empty? || modes.include?(b[:mode])) && (keywords - b[:keywords]).empty?
445
+ }.map { |b| " [#{b[:mode]}] #{b[:keys].join(" ")} : #{b[:desc]}" }.sort.join("\n")
423
446
  end
424
447
 
425
448
  def to_s()
@@ -528,6 +551,8 @@ class KeyBindingTree
528
551
  # if no, go back to root
529
552
  def match_key_conf(c, translated_c, event_type)
530
553
  log "MATCH KEY CONF: #{[c, translated_c]}"
554
+ # Every key as it comes in, before anything handles it (demo recorder)
555
+ emit(:key_input, c, event_type)
531
556
  # A delete still flashing from the previous key must land before this
532
557
  # key's action runs.
533
558
  Gui.flush_flash
@@ -645,6 +670,7 @@ class KeyBindingTree
645
670
  end
646
671
 
647
672
  if s_act.any? and !state_with_children.any?
673
+ wildcard = !eval_s.nil? # matched a "<char>" or /regexp/ key
648
674
  eval_s = s_act.first.action if eval_s == nil
649
675
  # if eval_s.to_s.match(/end_recording/)
650
676
  # require "pry"; binding.pry
@@ -654,6 +680,17 @@ class KeyBindingTree
654
680
  c.gsub!("\\", %q{\\\\} * 4) # Escape \ -chars
655
681
  c.gsub!("'", "#{'\\' * 4}'") # Escape ' -chars
656
682
 
683
+ node = s_act.first
684
+ if node.bind_args && eval_s.class == String
685
+ # A template expansion gets its values (and the typed character)
686
+ vals = "{" + node.bind_args.map { |k, v| "#{k}: #{v.inspect}" }.join(", ") + "}"
687
+ eval_s = wildcard ? "a.#{eval_s}!(#{vals}, <char>)" : "a.#{eval_s}!(#{vals})"
688
+ elsif wildcard && eval_s.class == String && (r = @actions&.[](eval_s)) && !r.opt&.[](:code)
689
+ # A named action gets the typed character as its argument. As a
690
+ # string, so macros and the key log can replay it like Ruby code.
691
+ # (the gsub! below fills in the escaped character)
692
+ eval_s = "a.#{eval_s}!(<char>)"
693
+ end
657
694
  eval_s.gsub!("<char>", "'#{c}'") if eval_s.class == String
658
695
  log eval_s
659
696
  log c
@@ -670,7 +707,10 @@ class KeyBindingTree
670
707
  return true
671
708
  end
672
709
 
673
- def bindkey(key, action, keywords: "")
710
+ # A key using a key set ("C <op> <motion>") is a template, see
711
+ # bind_template. filter: for templates, a proc (mode, keys, action) that
712
+ # says whether to bind one expansion.
713
+ def bindkey(key, action, keywords: "", args: nil, template: nil, filter: nil)
674
714
  # puts "keywords #{keywords}" if !keywords.empty?
675
715
  if keywords.class == String
676
716
  keywords = keywords.split(" ")
@@ -681,16 +721,131 @@ class KeyBindingTree
681
721
  # "X esc || X ctrl!" => "vma.kbd.to_previous_mode",
682
722
  key = key.split("||")
683
723
  end
724
+ keywords = keywords + @extra_keywords
684
725
 
685
726
  a = action
686
727
  if action.class == Array
687
- label = a[0]
728
+ label = @actions.resolve(a[0])
688
729
  a = label
689
730
  proc = action[1]
690
731
  msg = action[2]
691
732
  @actions.register(label, Action.new(label, msg, proc))
733
+ elsif ActionRef === a
734
+ # a.move.word.next: the action must exist (catches typos)
735
+ if !@actions.include?(a)
736
+ error("bindkey: unknown action #{a.to_s}#{@actions.did_you_mean(a.to_s)} for #{key.inspect}. Binding skipped.")
737
+ return
738
+ end
739
+ a = @actions.resolve(a)
740
+ elsif a.class == Symbol
741
+ a = @actions.resolve(a) # old id => new id (ACTION_ALIASES)
742
+ end
743
+ key.each { |k|
744
+ if template_key?(k)
745
+ bind_template(k.strip, a, keywords, filter)
746
+ else
747
+ _bindkey(k, a, keywords: keywords, args: args, template: template)
748
+ end
749
+ }
750
+ end
751
+
752
+ # ── Key sets and templates (doc/vim_compatibility_plan.md, step 1) ──
753
+ #
754
+ # A key set maps keys to values, e.g. the Vim operators and motions:
755
+ # define_key_set :op, { "d" => :delete, "g U" => :upcase }
756
+ # define_key_set :motion, { "w" => :word_next, "f <char>" => :find_char,
757
+ # "f space" => { id: :find_char, char: " " } }
758
+ # A binding whose keys name a key set is a template:
759
+ # bindkey "C <op> <motion>", "op.motion"
760
+ # It is expanded into one binding per combination (C d w, C g U f <char>,
761
+ # ...), each passing its values to the action: a.op.motion!({ op: :delete,
762
+ # motion: :word_next }). A Hash value adds its other fields to the values.
763
+ # "<same>" repeats the last key of the placeholder before it (d d, g U U).
764
+ # A count may be typed before every placeholder but the first (d 3 w): the
765
+ # expansion also gets a "<num>" variant there.
766
+
767
+ def define_key_set(name, entries)
768
+ @key_sets[name.to_sym] = entries.dup
769
+ end
770
+
771
+ # Add entries to a key set; the templates already bound with it get the
772
+ # new combinations too
773
+ def key_set_add(name, entries)
774
+ name = name.to_sym
775
+ (@key_sets[name] ||= {}).merge!(entries)
776
+ @templates.each { |t|
777
+ next if !t[:key].split.include?("<#{name}>")
778
+ bind_template(t[:key], t[:action], t[:keywords], t[:filter], only: { name => entries.keys }, record: false)
779
+ }
780
+ end
781
+
782
+ def key_set(name)
783
+ @key_sets[name.to_sym]
784
+ end
785
+
786
+ def template_key?(key)
787
+ key.split.any? { |t|
788
+ t == "<same>" || ((m = t.match(/\A<([a-z_]+)>\z/)) && @key_sets.key?(m[1].to_sym))
789
+ }
790
+ end
791
+
792
+ # The expansions of the key part of a template ("<op> <motion>") as
793
+ # [[keys, values], ...]. only: { set name => [keys] } limits that set to
794
+ # those entries.
795
+ def expand_template(keydef, only: nil)
796
+ out = [[[], {}, nil]] # keys, values, last key of the previous placeholder
797
+ first = true
798
+ keydef.split.each { |tok|
799
+ m = tok.match(/\A<([a-z_]+)>\z/)
800
+ name = m && m[1].to_sym
801
+ if tok == "<same>" || (name && @key_sets.key?(name))
802
+ nums = first ? [false] : [false, true]
803
+ out = out.flat_map { |ks, vals, last|
804
+ if tok == "<same>"
805
+ next [] if last.nil?
806
+ entries = { last => nil }
807
+ else
808
+ entries = @key_sets[name]
809
+ entries = entries.select { |k, _| only[name].include?(k) } if only && only[name]
810
+ end
811
+ nums.flat_map { |num|
812
+ entries.map { |ek, ev|
813
+ v = vals.dup
814
+ if ev.is_a?(Hash)
815
+ v[name] = ev[:id]
816
+ v.merge!(ev.reject { |k, _| k == :id })
817
+ elsif tok != "<same>"
818
+ v[name] = ev
819
+ end
820
+ eks = ek.split
821
+ [ks + (num ? ["<num>"] : []) + eks, v, eks.last]
822
+ }
823
+ }
824
+ }
825
+ first = false
826
+ else
827
+ out = out.map { |ks, vals, last| [ks + [tok], vals, last] }
828
+ end
829
+ }
830
+ out.map { |ks, vals, _| [ks, vals] }
831
+ end
832
+
833
+ # Bind every expansion of a template, e.g. "C <op> <motion>"
834
+ def bind_template(key, action, keywords, filter = nil, only: nil, record: true)
835
+ m = key.match(/\A(\S+)\s(\S.*)\z/)
836
+ if m.nil?
837
+ error("bindkey: invalid template #{key.inspect}. Binding skipped.")
838
+ return
692
839
  end
693
- key.each { |k| _bindkey(k, a, keywords: keywords) }
840
+ mstr, keydef = m[1], m[2]
841
+ modes = mstr.match?(/\A\p{Lu}+\z/) ? mstr.chars : [mstr]
842
+ @templates << { key: key, action: action, keywords: keywords, filter: filter } if record
843
+ modes.each { |mode|
844
+ expand_template(keydef, only: only).each { |keys, vals|
845
+ next if filter && !filter.call(mode, keys, action)
846
+ _bindkey("#{mode} #{keys.join(" ")}", action, keywords: keywords, args: vals, template: "#{mode} #{keydef}")
847
+ }
848
+ }
694
849
  end
695
850
 
696
851
  # Unbind one or more key definitions (split on "||").
@@ -747,7 +902,7 @@ class KeyBindingTree
747
902
  node
748
903
  end
749
904
 
750
- def _bindkey(key, action, keywords: [])
905
+ def _bindkey(key, action, keywords: [], args: nil, template: nil)
751
906
  key.strip!
752
907
  key.gsub!(/\s+/, " ")
753
908
 
@@ -758,7 +913,9 @@ class KeyBindingTree
758
913
  # $action_list << { :action => action, :key => key }
759
914
  if !@actions.include?(action)
760
915
  if action.class == String
761
- @actions.register(action, Action.new(action, action, proc { eval(action) }))
916
+ # Ruby code, run by exec_action (code: true keeps it apart from
917
+ # dotted action ids, which are strings too)
918
+ @actions.register(action, Action.new(action, action, proc { eval(action) }, { code: true }))
762
919
  end
763
920
  end
764
921
 
@@ -775,7 +932,7 @@ class KeyBindingTree
775
932
  modes = modetmp.split("") if modetmp.match(/^\p{Lu}+$/) # Uppercase
776
933
 
777
934
  # If all of first word is down case, like in:
778
- # bindkey "audio space", :audio_stop
935
+ # bindkey "audio space", "audio.stop"
779
936
  # interpret as whole word representing a mode.
780
937
  modes = [modetmp] if modetmp.match(/^\p{Ll}+$/) # Lowercase
781
938
  keydef = m[2]
@@ -793,22 +950,23 @@ class KeyBindingTree
793
950
 
794
951
  # puts "keywords #{keywords}"
795
952
  modes.each { |mode_id|
796
- next if !mode_bind_key(mode_id, keydef, action, keywords: keywords)
953
+ next if !mode_bind_key(mode_id, keydef, action, keywords: keywords, args: args, template: template)
797
954
 
798
- # Map froma actions to keybindings (e.g. to show bindings in menu)
799
- @act_bindings[mode_id][action] = keydef
955
+ # Map froma actions to keybindings (e.g. to show bindings in menu).
956
+ # Not for template expansions: the action has hundreds of them.
957
+ @act_bindings[mode_id][action] = keydef if template.nil?
800
958
  }
801
959
  end
802
960
 
803
961
  # Binds a keyboard key combination to an action,
804
962
  # for a given keyboard mode like insert ("I") or command ("C")
805
963
  # Returns true if the binding was created, false if the mode is unknown.
806
- def mode_bind_key(mode_id, keydef, action, keywords: [])
964
+ def mode_bind_key(mode_id, keydef, action, keywords: [], args: nil, template: nil)
807
965
  # debug "mode_bind_key #{mode_id.inspect}, #{keydef.inspect}, #{action.inspect}", 2
808
966
  # Example:
809
- # bindkey "C , f", :gui_file_finder
967
+ # bindkey "C , f", "file.finder"
810
968
  # mode_id = "C", keydef = ", f"
811
- # and action = :gui_file_finder
969
+ # and action = "file.finder"
812
970
 
813
971
  # Bind only to a mode that exists. Without this check the binding would
814
972
  # silently end up in the current default mode (see set_state), e.g. a
@@ -866,6 +1024,9 @@ class KeyBindingTree
866
1024
  prev_state.children.delete(cur_state)
867
1025
  else
868
1026
  @cur_state.action = action
1027
+ @cur_state.seq = (@bind_seq += 1)
1028
+ @cur_state.bind_args = args
1029
+ @cur_state.template = template
869
1030
  end
870
1031
  @cur_state.keywords = keywords
871
1032
  @cur_state = @root
@@ -879,7 +1040,8 @@ class KeyBindingTree
879
1040
  # $acth << action #TODO:needed here?
880
1041
  @method_handles_repeat = false #TODO:??
881
1042
  n = 1
882
- if @next_command_count and !(action.class == String and action.include?("set_next_command_count"))
1043
+ counting = action.class == String && (action.include?("set_next_command_count") || action.include?("kbd.count.digit"))
1044
+ if @next_command_count and !counting
883
1045
  n = @next_command_count
884
1046
  log("COUNT command #{n} times")
885
1047
  end
@@ -901,7 +1063,7 @@ class KeyBindingTree
901
1063
  @on_action_error.call(action, e)
902
1064
  end
903
1065
 
904
- if !(action.class == String and action.include?("set_next_command_count"))
1066
+ if !counting
905
1067
  @next_command_count = nil
906
1068
  end
907
1069
 
@@ -909,8 +1071,8 @@ class KeyBindingTree
909
1071
  end
910
1072
  end
911
1073
 
912
- def bindkey(key, action, keywords: "")
913
- vma.kbd.bindkey(key, action, keywords: keywords)
1074
+ def bindkey(key, action, keywords: "", filter: nil)
1075
+ vma.kbd.bindkey(key, action, keywords: keywords, filter: filter)
914
1076
  end
915
1077
 
916
1078
  def unbindkey(key, include_child_nodes: false)
@@ -926,10 +1088,15 @@ end
926
1088
  def exec_action(action)
927
1089
  vma.kbd.last_action = vma.kbd.cur_action
928
1090
  vma.kbd.cur_action = action
1091
+ action = action.to_s if ActionRef === action
929
1092
  if action.class == Symbol
930
1093
  return call_action(action)
931
1094
  elsif action.class == Proc
932
1095
  return action.call
1096
+ elsif action.class == String && (r = vma.actions[action]) && !r.opt&.[](:code)
1097
+ # A dotted action id ("move.word.next"). Registered actions win over
1098
+ # running the string as Ruby, even where the id is valid Ruby too.
1099
+ return call_action(action)
933
1100
  elsif action.class == String && vma.actions.include?(action.to_sym)
934
1101
  # Symbols serialised through JSON become plain strings; dispatch them as actions.
935
1102
  return call_action(action.to_sym)
@@ -9,14 +9,14 @@ module Vimamsa
9
9
  vma.kbd.__set_mode(:insert)
10
10
 
11
11
  # esc never leaves insert mode: clear selection, dismiss autocomplete popup
12
- reg_act(:notepad_escape, proc {
12
+ reg_act("mode.notepad.escape", proc {
13
13
  buf.end_selection
14
14
  v = vma.buf.view
15
15
  v.hide_completions if v.respond_to?(:hide_completions)
16
16
  }, "Clear selection and dismiss popups")
17
17
 
18
- # Replaces the "I esc" leaf of vimlike's "I esc || I ctrl!" => :prev_mode
19
- bindkey "I esc", :notepad_escape
18
+ # Replaces the "I esc" leaf of vimlike's "I esc || I ctrl!" => "mode.prev"
19
+ bindkey "I esc", "mode.notepad.escape"
20
20
  # In vimlike, releasing bare ctrl in insert mode returns to command mode.
21
21
  # Harmless here (mode stack is [:insert], to_previous_mode won't pop) but
22
22
  # remove it so ctrl-release doesn't dispatch a pointless action.
@@ -25,19 +25,21 @@ module Vimamsa
25
25
  add_keys "notepad", {
26
26
  # Rebinds over conflicting vimlike insert-mode bindings
27
27
  # (bindkey on an existing chord overwrites the leaf)
28
- "I ctrl-f" => :find_in_buffer, # was move FORWARD_CHAR
29
- "I ctrl-h" => :gui_search_replace, # was autocp trigger (ctrl-space remains)
30
- "I ctrl-n" => :buf_new, # was move_next_line
31
- "I ctrl-a" => :select_all, # was jump_beginning_of_line
32
- "I ctrl-q" => :quit,
33
- "I ctrl-shift-z || I ctrl-shift-Z" => :redo, # shift+z may arrive uppercase
34
- "I delete" => :insert_delete,
28
+ "I ctrl-f" => "search.buf", # was move FORWARD_CHAR
29
+ "I ctrl-h" => "search.replace", # was autocp trigger (ctrl-space remains)
30
+ "I ctrl-n" => "buf.new", # was move_next_line
31
+ "I ctrl-a" => "sel.all", # was jump_beginning_of_line
32
+ "I ctrl-q" => "app.quit",
33
+ "I ctrl-shift-z || I ctrl-shift-Z" => "undo.redo", # shift+z may arrive uppercase
34
+ "I delete" => "edit.delete.char_or_sel",
35
+ "I ctrl-home" => "move.buf.start",
36
+ "I ctrl-end" => "move.buf.end",
37
+ }
35
38
 
36
- "I home" => "buf.end_selection; buf.jump(BEGINNING_OF_LINE)",
37
- "I end" => "buf.end_selection; buf.jump(END_OF_LINE)",
38
- "I shift-home" => "buf.continue_selection; buf.jump(BEGINNING_OF_LINE)",
39
- "I shift-end" => "buf.continue_selection; buf.jump(END_OF_LINE)",
40
- "I ctrl-home" => :jump_to_start_of_buffer,
41
- "I ctrl-end" => :jump_to_end_of_buffer,
39
+ add_keys "notepad cat:motion", {
40
+ "I home" => "move.line.start.unselect",
41
+ "I end" => "move.line.end.unselect",
42
+ "I shift-home" => "sel.line.start",
43
+ "I shift-end" => "sel.line.end",
42
44
  }
43
45
  end # module Vimamsa