Class: RuVim::GlobalCommands

Inherits:
Object
  • Object
show all
Includes:
RuVim::Git::Handler, Singleton
Defined in:
lib/ruvim/global_commands.rb

Constant Summary

Constants included from RuVim::Git::Handler

RuVim::Git::Handler::GH_SUBCOMMANDS, RuVim::Git::Handler::GIT_SUBCOMMANDS

Instance Method Summary collapse

Methods included from RuVim::Git::Handler

#enter_git_command_mode, #ex_gh, #ex_git, #git_close_buffer

Methods included from RuVim::Gh::Link::HandlerMethods

#gh_browse, #gh_link, #gh_pr

Methods included from RuVim::Git::Grep::HandlerMethods

#git_grep, #git_grep_open_file

Methods included from RuVim::Git::Commit::HandlerMethods

#git_commit, #git_commit_execute

Methods included from RuVim::Git::Branch::HandlerMethods

#git_branch, #git_branch_checkout, #git_branch_execute_checkout

Methods included from RuVim::Git::Log::HandlerMethods

#git_log

Methods included from RuVim::Git::Diff::HandlerMethods

#git_diff, #git_diff_open_file

Methods included from RuVim::Git::Status::HandlerMethods

#git_status, #git_status_open_file

Methods included from RuVim::Git::Blame::HandlerMethods

#git_blame, #git_blame_back, #git_blame_commit, #git_blame_prev

Instance Method Details

#app_quit(ctx, bang:) ⇒ Object



817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
# File 'lib/ruvim/global_commands.rb', line 817

def app_quit(ctx, bang:, **)
  if ctx.buffer.kind == :filter
    saved_y = ctx.buffer.options["filter_source_cursor_y"]
    saved_x = ctx.buffer.options["filter_source_cursor_x"]
    saved_row_offset = ctx.buffer.options["filter_source_row_offset"]
    saved_col_offset = ctx.buffer.options["filter_source_col_offset"]
    ctx.editor.delete_buffer(ctx.buffer.id)
    if saved_y
      win = ctx.editor.current_window
      win.cursor_y = saved_y
      win.cursor_x = saved_x || 0
      win.row_offset = saved_row_offset || 0
      win.col_offset = saved_col_offset || 0
    end
    return
  end

  if ctx.editor.window_count > 1
    ctx.editor.close_current_window
    ctx.editor.echo("closed window")
    return
  end

  if ctx.editor.tabpage_count > 1
    ctx.editor.close_current_tabpage
    ctx.editor.echo("closed tab")
    return
  end

  if ctx.buffer.modified? && !bang
    ctx.editor.echo_error("No write since last change (add ! to override)")
    return
  end

  ctx.editor.request_quit!
end

#app_quit_all(ctx, bang:) ⇒ Object



854
855
856
857
858
859
860
861
862
863
# File 'lib/ruvim/global_commands.rb', line 854

def app_quit_all(ctx, bang:, **)
  unless bang
    modified = ctx.editor.buffers.values.select { |b| b.file_buffer? && b.modified? }
    unless modified.empty?
      ctx.editor.echo_error("#{modified.size} buffer(s) have unsaved changes (add ! to override)")
      return
    end
  end
  ctx.editor.request_quit!
end

#append_line_end_mode(ctx) ⇒ Object



182
183
184
185
# File 'lib/ruvim/global_commands.rb', line 182

def append_line_end_mode(ctx, **)
  ctx.window.cursor_x = ctx.buffer.line_length(ctx.window.cursor_y)
  enter_insert_mode(ctx)
end

#append_mode(ctx) ⇒ Object



175
176
177
178
179
180
# File 'lib/ruvim/global_commands.rb', line 175

def append_mode(ctx, **)
  x = ctx.window.cursor_x
  len = ctx.buffer.line_length(ctx.window.cursor_y)
  ctx.window.cursor_x = [x + 1, len].min
  enter_insert_mode(ctx)
end

#arglist_first(ctx) ⇒ Object



3405
3406
3407
3408
3409
3410
# File 'lib/ruvim/global_commands.rb', line 3405

def arglist_first(ctx, **)
  path = ctx.editor.arglist_first
  return ctx.editor.error("No arguments") unless path
  switch_to_file(ctx, path)
  ctx.editor.echo("Argument 1 of #{ctx.editor.arglist.length}: #{path}")
end

#arglist_last(ctx) ⇒ Object



3412
3413
3414
3415
3416
3417
# File 'lib/ruvim/global_commands.rb', line 3412

def arglist_last(ctx, **)
  path = ctx.editor.arglist_last
  return ctx.editor.error("No arguments") unless path
  switch_to_file(ctx, path)
  ctx.editor.echo("Argument #{ctx.editor.arglist.length} of #{ctx.editor.arglist.length}: #{path}")
end

#arglist_next(ctx, count:) ⇒ Object



3391
3392
3393
3394
3395
3396
# File 'lib/ruvim/global_commands.rb', line 3391

def arglist_next(ctx, count:, **)
  count = normalized_count(count)
  path = ctx.editor.arglist_next(count)
  switch_to_file(ctx, path)
  ctx.editor.echo("Argument #{ctx.editor.arglist_index + 1} of #{ctx.editor.arglist.length}: #{path}")
end

#arglist_prev(ctx, count:) ⇒ Object



3398
3399
3400
3401
3402
3403
# File 'lib/ruvim/global_commands.rb', line 3398

def arglist_prev(ctx, count:, **)
  count = normalized_count(count)
  path = ctx.editor.arglist_prev(count)
  switch_to_file(ctx, path)
  ctx.editor.echo("Argument #{ctx.editor.arglist_index + 1} of #{ctx.editor.arglist.length}: #{path}")
end

#arglist_show(ctx) ⇒ Object



3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
# File 'lib/ruvim/global_commands.rb', line 3373

def arglist_show(ctx, **)
  arglist = ctx.editor.arglist
  if arglist.empty?
    ctx.editor.echo("No arguments")
    return
  end

  current_index = ctx.editor.arglist_index
  items = arglist.map.with_index do |path, i|
    if i == current_index
      "[#{path}]"
    else
      " #{path}"
    end
  end
  ctx.editor.echo_multiline(items)
end

#buffer_delete(ctx, argv:, bang:) ⇒ Object



983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
# File 'lib/ruvim/global_commands.rb', line 983

def buffer_delete(ctx, argv:, bang:, **)
  arg = argv[0]
  target_id =
    if arg.nil? || arg.empty?
      ctx.buffer.id
    elsif arg == "#"
      ctx.editor.alternate_buffer_id || raise(RuVim::CommandError, "No alternate buffer")
    elsif arg.match?(/\A\d+\z/)
      arg.to_i
    else
      find_buffer_by_name(ctx.editor, arg)&.id || raise(RuVim::CommandError, "No such buffer: #{arg}")
    end

  target = ctx.editor.buffers[target_id] || raise(RuVim::CommandError, "No such buffer: #{target_id}")
  if target.modified? && !bang
    raise RuVim::CommandError, "No write since last change (use :bdelete! to discard)"
  end

  ctx.editor.delete_buffer(target_id)
  ctx.editor.echo("buffer #{target_id} deleted")
end

#buffer_list(ctx) ⇒ Object



935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
# File 'lib/ruvim/global_commands.rb', line 935

def buffer_list(ctx, **)
  current_id = ctx.buffer.id
  alt_id = ctx.editor.alternate_buffer_id
  items = ctx.editor.buffer_ids.map do |id|
    b = ctx.editor.buffers.fetch(id)
    indicator = id == current_id ? "%a" : "  "
    indicator = "# " if id == alt_id && id != current_id
    mod = b.modified? ? "+" : " "
    name = b.path ? "\"#{b.path}\"" : "[No Name]"
    line_info = "line #{b.respond_to?(:cursor_line) ? b.cursor_line : 0}"
    # Find the window showing this buffer to get cursor line
    win = ctx.editor.windows.values.find { |w| w.buffer_id == id }
    line_info = "line #{win ? win.cursor_y + 1 : 0}"
    "%3d %s %s %-30s %s" % [id, indicator, mod, name, line_info]
  end
  ctx.editor.echo_multiline(items)
end

#buffer_next(ctx, count:, bang:) ⇒ Object



953
954
955
956
957
958
# File 'lib/ruvim/global_commands.rb', line 953

def buffer_next(ctx, count:, bang:, **)
  count = normalized_count(count)
  target = ctx.editor.current_buffer.id
  count.times { target = ctx.editor.next_buffer_id_from(target, 1) }
  switch_buffer_id(ctx, target, bang:)
end

#buffer_prev(ctx, count:, bang:) ⇒ Object



960
961
962
963
964
965
# File 'lib/ruvim/global_commands.rb', line 960

def buffer_prev(ctx, count:, bang:, **)
  count = normalized_count(count)
  target = ctx.editor.current_buffer.id
  count.times { target = ctx.editor.next_buffer_id_from(target, -1) }
  switch_buffer_id(ctx, target, bang:)
end

#buffer_redo(ctx) ⇒ Object



517
518
519
520
521
522
523
524
# File 'lib/ruvim/global_commands.rb', line 517

def buffer_redo(ctx, **)
  if ctx.buffer.redo!
    ctx.window.clamp_to_buffer(ctx.buffer)
    ctx.editor.echo("Redo")
  else
    ctx.editor.echo("Already at newest change")
  end
end

#buffer_switch(ctx, argv:, bang:) ⇒ Object



967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
# File 'lib/ruvim/global_commands.rb', line 967

def buffer_switch(ctx, argv:, bang:, **)
  arg = argv[0]
  raise RuVim::CommandError, "Usage: :buffer <id|#>" if arg.nil? || arg.empty?

  target_id =
    if arg == "#"
      ctx.editor.alternate_buffer_id || raise(RuVim::CommandError, "No alternate buffer")
    elsif arg.match?(/\A\d+\z/)
      arg.to_i
    else
      find_buffer_by_name(ctx.editor, arg)&.id || raise(RuVim::CommandError, "No such buffer: #{arg}")
    end

  switch_buffer_id(ctx, target_id, bang:)
end

#buffer_undo(ctx) ⇒ Object



508
509
510
511
512
513
514
515
# File 'lib/ruvim/global_commands.rb', line 508

def buffer_undo(ctx, **)
  if ctx.buffer.undo!
    ctx.window.clamp_to_buffer(ctx.buffer)
    ctx.editor.echo("Undo")
  else
    ctx.editor.echo("Already at oldest change")
  end
end

#call(spec_call, ctx, argv: [], kwargs: {}, bang: false, count: nil) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/ruvim/global_commands.rb', line 10

def call(spec_call, ctx, argv: [], kwargs: {}, bang: false, count: nil)
  case spec_call
  when Symbol, String
    public_send(spec_call.to_sym, ctx, argv: argv, kwargs: kwargs, bang: bang, count: count)
  else
    spec_call.call(ctx, argv: argv, kwargs: kwargs, bang: bang, count: count)
  end
end

#change_line(ctx, count:) ⇒ Object



502
503
504
505
506
# File 'lib/ruvim/global_commands.rb', line 502

def change_line(ctx, count:, **)
  materialize_intro_buffer_if_needed(ctx)
  delete_line(ctx, count:)
  enter_insert_mode(ctx)
end

#change_motion(ctx, count:, kwargs:) ⇒ Object



481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
# File 'lib/ruvim/global_commands.rb', line 481

def change_motion(ctx, count:, kwargs:, **)
  materialize_intro_buffer_if_needed(ctx)
  motion = (kwargs[:motion] || kwargs["motion"]).to_s
  result = delete_motion(ctx, count:, kwargs:)
  return unless result

  if result == :linewise
    case motion
    when "G"
      y = ctx.buffer.lines.length
      ctx.buffer.insert_lines_at(y, [""])
      ctx.window.cursor_y = y
    when "gg"
      ctx.buffer.insert_lines_at(0, [""])
      ctx.window.cursor_y = 0
    end
    ctx.window.cursor_x = 0
  end
  enter_insert_mode(ctx)
end

#clear_message(ctx) ⇒ Object



773
774
775
# File 'lib/ruvim/global_commands.rb', line 773

def clear_message(ctx, **)
  ctx.editor.clear_message
end

#cursor_buffer_end(ctx, count:) ⇒ Object



114
115
116
117
118
119
# File 'lib/ruvim/global_commands.rb', line 114

def cursor_buffer_end(ctx, count:, **)
  record_jump(ctx)
  target_row = count.nil? ? (ctx.buffer.line_count - 1) : [normalized_count(count) - 1, ctx.buffer.line_count - 1].min
  ctx.window.cursor_y = target_row
  cursor_first_nonblank(ctx)
end

#cursor_buffer_start(ctx, count:) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/ruvim/global_commands.rb', line 106

def cursor_buffer_start(ctx, count:, **)
  record_jump(ctx)
  target_row = [normalized_count(count).to_i - 1, 0].max
  target_row = [target_row, ctx.buffer.line_count - 1].min
  ctx.window.cursor_y = target_row
  cursor_first_nonblank(ctx)
end

#cursor_down(ctx, count:) ⇒ Object



31
32
33
# File 'lib/ruvim/global_commands.rb', line 31

def cursor_down(ctx, count:, **)
  ctx.window.move_down(ctx.buffer, normalized_count(count))
end

#cursor_first_nonblank(ctx) ⇒ Object



99
100
101
102
103
104
# File 'lib/ruvim/global_commands.rb', line 99

def cursor_first_nonblank(ctx, **)
  line = ctx.buffer.line_at(ctx.window.cursor_y)
  idx = line.index(/\S/) || 0
  ctx.window.cursor_x = idx
  ctx.window.clamp_to_buffer(ctx.buffer)
end

#cursor_left(ctx, count:) ⇒ Object



19
20
21
# File 'lib/ruvim/global_commands.rb', line 19

def cursor_left(ctx, count:, **)
  move_cursor_horizontally(ctx, direction: :left, count:)
end

#cursor_line_end(ctx) ⇒ Object



94
95
96
97
# File 'lib/ruvim/global_commands.rb', line 94

def cursor_line_end(ctx, **)
  ctx.window.cursor_x = ctx.buffer.line_length(ctx.window.cursor_y)
  ctx.window.clamp_to_buffer(ctx.buffer)
end

#cursor_line_start(ctx) ⇒ Object



89
90
91
92
# File 'lib/ruvim/global_commands.rb', line 89

def cursor_line_start(ctx, **)
  ctx.window.cursor_x = 0
  ctx.window.clamp_to_buffer(ctx.buffer)
end

#cursor_match_bracket(ctx) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/ruvim/global_commands.rb', line 133

def cursor_match_bracket(ctx, **)
  line = ctx.buffer.line_at(ctx.window.cursor_y)
  ch = line[ctx.window.cursor_x]
  unless ch
    ctx.editor.echo_error("No bracket under cursor")
    return
  end

  pair_map = {
    "(" => [")", :forward],
    "[" => ["]", :forward],
    "{" => ["}", :forward],
    ")" => ["(", :backward],
    "]" => ["[", :backward],
    "}" => ["{", :backward]
  }
  pair = pair_map[ch]
  unless pair
    ctx.editor.echo_error("No bracket under cursor")
    return
  end

  target_char, direction = pair
  record_jump(ctx)
  loc = find_matching_bracket(ctx.buffer, ctx.window.cursor_y, ctx.window.cursor_x, ch, target_char, direction)
  if loc
    ctx.window.cursor_y = loc[:row]
    ctx.window.cursor_x = loc[:col]
    ctx.window.clamp_to_buffer(ctx.buffer)
  else
    ctx.editor.echo_error("Match not found")
  end
end

#cursor_page_down(ctx, kwargs:, count:) ⇒ Object



40
41
42
43
# File 'lib/ruvim/global_commands.rb', line 40

def cursor_page_down(ctx, kwargs:, count:, **)
  page_lines = [(kwargs[:page_lines] || kwargs["page_lines"] || 1).to_i, 1].max
  ctx.window.move_down(ctx.buffer, page_lines * [count.to_i, 1].max)
end

#cursor_page_down_default(ctx, count:, bang:) ⇒ Object



49
50
51
# File 'lib/ruvim/global_commands.rb', line 49

def cursor_page_down_default(ctx, count:, bang:, **)
  call(:cursor_page_down, ctx, count:, bang:, kwargs: { page_lines: current_page_step_lines(ctx) })
end

#cursor_page_down_half(ctx, count:, bang:) ⇒ Object



57
58
59
# File 'lib/ruvim/global_commands.rb', line 57

def cursor_page_down_half(ctx, count:, bang:, **)
  call(:cursor_page_down, ctx, count:, bang:, kwargs: { page_lines: current_half_page_step_lines(ctx) })
end

#cursor_page_up(ctx, kwargs:, count:) ⇒ Object



35
36
37
38
# File 'lib/ruvim/global_commands.rb', line 35

def cursor_page_up(ctx, kwargs:, count:, **)
  page_lines = [(kwargs[:page_lines] || kwargs["page_lines"] || 1).to_i, 1].max
  ctx.window.move_up(ctx.buffer, page_lines * [count.to_i, 1].max)
end

#cursor_page_up_default(ctx, count:, bang:) ⇒ Object



45
46
47
# File 'lib/ruvim/global_commands.rb', line 45

def cursor_page_up_default(ctx, count:, bang:, **)
  call(:cursor_page_up, ctx, count:, bang:, kwargs: { page_lines: current_page_step_lines(ctx) })
end

#cursor_page_up_half(ctx, count:, bang:) ⇒ Object



53
54
55
# File 'lib/ruvim/global_commands.rb', line 53

def cursor_page_up_half(ctx, count:, bang:, **)
  call(:cursor_page_up, ctx, count:, bang:, kwargs: { page_lines: current_half_page_step_lines(ctx) })
end

#cursor_right(ctx, count:) ⇒ Object



23
24
25
# File 'lib/ruvim/global_commands.rb', line 23

def cursor_right(ctx, count:, **)
  move_cursor_horizontally(ctx, direction: :right, count:)
end

#cursor_up(ctx, count:) ⇒ Object



27
28
29
# File 'lib/ruvim/global_commands.rb', line 27

def cursor_up(ctx, count:, **)
  ctx.window.move_up(ctx.buffer, normalized_count(count))
end

#cursor_word_backward(ctx, count:) ⇒ Object



125
126
127
# File 'lib/ruvim/global_commands.rb', line 125

def cursor_word_backward(ctx, count:, **)
  move_cursor_word(ctx, count:, kind: :backward_start)
end

#cursor_word_end(ctx, count:) ⇒ Object



129
130
131
# File 'lib/ruvim/global_commands.rb', line 129

def cursor_word_end(ctx, count:, **)
  move_cursor_word(ctx, count:, kind: :forward_end)
end

#cursor_word_forward(ctx, count:) ⇒ Object



121
122
123
# File 'lib/ruvim/global_commands.rb', line 121

def cursor_word_forward(ctx, count:, **)
  move_cursor_word(ctx, count:, kind: :forward_start)
end

#delete_char(ctx, count:) ⇒ Object



360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'lib/ruvim/global_commands.rb', line 360

def delete_char(ctx, count:, **)
  materialize_intro_buffer_if_needed(ctx)
  count = normalized_count(count)
  ctx.buffer.begin_change_group
  deleted = +""
  count.times do
    chunk = char_at_cursor_for_delete(ctx.buffer, ctx.window.cursor_y, ctx.window.cursor_x)
    ok = ctx.buffer.delete_char(ctx.window.cursor_y, ctx.window.cursor_x)
    break unless ok
    deleted << chunk
  end
  ctx.buffer.end_change_group
  store_delete_register(ctx, text: deleted, type: :charwise) unless deleted.empty?
  ctx.window.clamp_to_buffer(ctx.buffer)
end

#delete_line(ctx, count:) ⇒ Object



447
448
449
450
451
452
453
454
455
456
# File 'lib/ruvim/global_commands.rb', line 447

def delete_line(ctx, count:, **)
  materialize_intro_buffer_if_needed(ctx)
  count = normalized_count(count)
  ctx.buffer.begin_change_group
  deleted_lines = []
  count.times { deleted_lines << ctx.buffer.delete_line(ctx.window.cursor_y) }
  ctx.buffer.end_change_group
  store_delete_register(ctx, text: deleted_lines.join("\n") + "\n", type: :linewise)
  ctx.window.clamp_to_buffer(ctx.buffer)
end

#delete_motion(ctx, count:, kwargs:) ⇒ Object



458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
# File 'lib/ruvim/global_commands.rb', line 458

def delete_motion(ctx, count:, kwargs:, **)
  materialize_intro_buffer_if_needed(ctx)
  motion = (kwargs[:motion] || kwargs["motion"]).to_s
  ncount = normalized_count(count)
  handled =
    case motion
    when "h" then delete_chars_left(ctx, ncount)
    when "l" then delete_chars_right(ctx, ncount)
    when "j" then delete_lines_down(ctx, ncount)
    when "k" then delete_lines_up(ctx, ncount)
    when "$" then delete_to_end_of_line(ctx)
    when "w" then delete_word_forward(ctx, ncount)
    when "G" then delete_lines_to_end(ctx)
    when "gg" then delete_lines_to_start(ctx)
    when "iw" then delete_text_object_word(ctx, around: false)
    when "aw" then delete_text_object_word(ctx, around: true)
    else
      text_object_motion?(motion) ? delete_text_object(ctx, motion) : false
    end
  ctx.editor.echo("Unsupported motion for d: #{motion}") unless handled
  handled
end

#enter_command_line_mode(ctx) ⇒ Object



345
346
347
348
# File 'lib/ruvim/global_commands.rb', line 345

def enter_command_line_mode(ctx, **)
  ctx.editor.enter_command_line_mode(":")
  ctx.editor.clear_message
end

#enter_insert_mode(ctx) ⇒ Object



167
168
169
170
171
172
173
# File 'lib/ruvim/global_commands.rb', line 167

def enter_insert_mode(ctx, **)
  materialize_intro_buffer_if_needed(ctx)
  ensure_modifiable_for_insert!(ctx)
  ctx.buffer.begin_change_group
  ctx.editor.enter_insert_mode
  ctx.editor.echo("-- INSERT --")
end

#enter_search_backward_mode(ctx) ⇒ Object



355
356
357
358
# File 'lib/ruvim/global_commands.rb', line 355

def enter_search_backward_mode(ctx, **)
  ctx.editor.enter_command_line_mode("?")
  ctx.editor.clear_message
end

#enter_search_forward_mode(ctx) ⇒ Object



350
351
352
353
# File 'lib/ruvim/global_commands.rb', line 350

def enter_search_forward_mode(ctx, **)
  ctx.editor.enter_command_line_mode("/")
  ctx.editor.clear_message
end

#enter_visual_block_mode(ctx) ⇒ Object



229
230
231
232
# File 'lib/ruvim/global_commands.rb', line 229

def enter_visual_block_mode(ctx, **)
  ctx.editor.enter_visual(:visual_block)
  ctx.editor.echo("-- VISUAL BLOCK --")
end

#enter_visual_char_mode(ctx) ⇒ Object



219
220
221
222
# File 'lib/ruvim/global_commands.rb', line 219

def enter_visual_char_mode(ctx, **)
  ctx.editor.enter_visual(:visual_char)
  ctx.editor.echo("-- VISUAL --")
end

#enter_visual_line_mode(ctx) ⇒ Object



224
225
226
227
# File 'lib/ruvim/global_commands.rb', line 224

def enter_visual_line_mode(ctx, **)
  ctx.editor.enter_visual(:visual_line)
  ctx.editor.echo("-- VISUAL LINE --")
end

#ex_bindings(ctx, argv: []) ⇒ Object



1322
1323
1324
1325
1326
1327
1328
1329
1330
# File 'lib/ruvim/global_commands.rb', line 1322

def ex_bindings(ctx, argv: [], **)
  keymaps = ctx.editor.keymap_manager
  raise RuVim::CommandError, "Keymap manager is unavailable" unless keymaps

  mode_filter, sort = parse_bindings_args(argv)
  entries = keymaps.binding_entries_for_context(ctx.editor, mode: mode_filter)
  lines = bindings_buffer_lines(ctx.editor, entries, mode_filter:, sort:)
  ctx.editor.show_help_buffer!(title: "[Bindings]", lines:)
end

#ex_cclose(ctx) ⇒ Object



1378
1379
1380
# File 'lib/ruvim/global_commands.rb', line 1378

def ex_cclose(ctx, **)
  close_list_windows(ctx.editor, :quickfix)
end

#ex_cnext(ctx) ⇒ Object



1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
# File 'lib/ruvim/global_commands.rb', line 1382

def ex_cnext(ctx, **)
  item = ctx.editor.move_quickfix(+1)
  unless item
    ctx.editor.echo_error("quickfix list is empty")
    return
  end
  ctx.editor.jump_to_location(item)
  refresh_list_window(ctx.editor, :quickfix)
  ctx.editor.echo(quickfix_item_echo(ctx.editor))
end

#ex_commands(ctx) ⇒ Object



1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
# File 'lib/ruvim/global_commands.rb', line 1304

def ex_commands(ctx, **)
  rows = RuVim::ExCommandRegistry.instance.all.map do |spec|
    alias_text = spec.aliases.empty? ? "" : " (#{spec.aliases.join(', ')})"
    source = spec.source == :user ? " [user]" : ""
    name = "#{spec.name}#{alias_text}#{source}"
    desc = spec.desc
    keys = ex_command_binding_labels(ctx.editor, spec)
    [name, desc, keys]
  end
  name_width = rows.map { |name, _desc, _keys| name.length }.max || 0
  items = rows.map do |name, desc, keys|
    line = "#{name.ljust(name_width)}  #{desc}"
    line += "  keys: #{keys.join(', ')}" unless keys.empty?
    line
  end
  ctx.editor.show_help_buffer!(title: "[Commands]", lines: ["Ex commands", "", *items])
end

#ex_copen(ctx) ⇒ Object



1374
1375
1376
# File 'lib/ruvim/global_commands.rb', line 1374

def ex_copen(ctx, **)
  open_list_window(ctx, kind: :quickfix, title: "[Quickfix]", lines: quickfix_buffer_lines(ctx.editor), source_window_id: ctx.window.id)
end

#ex_cprev(ctx) ⇒ Object



1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
# File 'lib/ruvim/global_commands.rb', line 1393

def ex_cprev(ctx, **)
  item = ctx.editor.move_quickfix(-1)
  unless item
    ctx.editor.echo_error("quickfix list is empty")
    return
  end
  ctx.editor.jump_to_location(item)
  refresh_list_window(ctx.editor, :quickfix)
  ctx.editor.echo(quickfix_item_echo(ctx.editor))
end

#ex_define_command(ctx, argv:, bang:) ⇒ Object



1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
# File 'lib/ruvim/global_commands.rb', line 1054

def ex_define_command(ctx, argv:, bang:, **)
  registry = RuVim::ExCommandRegistry.instance
  if argv.empty?
    user_cmds = registry.all.select { |spec| spec.source == :user }
    if user_cmds.empty?
      ctx.editor.echo("No user commands")
    else
      header = "    Name        Definition"
      items = [header] + user_cmds.map { |spec|
        body = spec.respond_to?(:body) ? spec.body.to_s : spec.name.to_s
        "    %-12s%s" % [spec.name, body]
      }
      ctx.editor.echo_multiline(items)
    end
    return
  end

  name = argv[0].to_s
  body_tokens = argv[1..] || []
  raise RuVim::CommandError, "Usage: :command Name ex_body" if body_tokens.empty?

  if registry.registered?(name)
    unless bang
      raise RuVim::CommandError, "Command exists: #{name} (use :command! to replace)"
    end
    registry.unregister(name)
  end

  body = body_tokens.join(" ")
  handler = lambda do |inner_ctx, argv:, **_k|
    expanded = [body, *argv].join(" ").strip
    Dispatcher.new.dispatch_ex(inner_ctx.editor, expanded)
  end

  registry.register(name, call: handler, desc: "user-defined", nargs: :any, bang: true, source: :user)
  ctx.editor.echo("Defined :#{name}")
end

#ex_delete_lines(ctx, kwargs: {}) ⇒ Object



1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
# File 'lib/ruvim/global_commands.rb', line 1470

def ex_delete_lines(ctx, kwargs: {}, **)
  materialize_intro_buffer_if_needed(ctx)
  r_start = kwargs[:range_start]
  r_end = kwargs[:range_end]
  unless r_start && r_end
    # Default to current line
    r_start = r_end = ctx.window.cursor_y
  end

  count = r_end - r_start + 1
  deleted_text = ctx.buffer.line_block_text(r_start, count)
  ctx.buffer.begin_change_group
  count.times { ctx.buffer.delete_line(r_start) }
  ctx.buffer.end_change_group
  store_delete_register(ctx, text: deleted_text, type: :linewise)
  ctx.window.cursor_y = [r_start, ctx.buffer.line_count - 1].min
  ctx.window.cursor_x = 0
  ctx.window.clamp_to_buffer(ctx.buffer)
  ctx.editor.echo("#{count} line(s) deleted")
end

#ex_filter(ctx, argv:) ⇒ Object



1568
1569
1570
1571
1572
1573
1574
1575
# File 'lib/ruvim/global_commands.rb', line 1568

def ex_filter(ctx, argv:, **)
  if argv.any?
    pattern = parse_vimgrep_pattern(argv.join(" "))
    editor = ctx.editor
    editor.set_last_search(pattern: pattern, direction: :forward)
  end
  search_filter(ctx)
end

#ex_grep(ctx, argv:, kwargs: {}) ⇒ Object



1462
1463
1464
# File 'lib/ruvim/global_commands.rb', line 1462

def ex_grep(ctx, argv:, kwargs: {}, **)
  run_external_grep(ctx, argv:, target: :quickfix)
end

#ex_help(ctx, argv: []) ⇒ Object



1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
# File 'lib/ruvim/global_commands.rb', line 1005

def ex_help(ctx, argv: [], **)
  topic = argv.first.to_s
  registry = RuVim::ExCommandRegistry.instance

  if topic.empty?
    lines = [
      "RuVim help",
      "",
      "Topics:",
      "  :help commands",
      "  :help regex",
      "  :help options",
      "  :help config",
      "  :help bindings",
      "",
      "Ex command help:",
      "  :help w",
      "  :help set",
      "  :help buffer"
    ]
    ctx.editor.show_help_buffer!(title: "[Help] help", lines:)
    return
  end

  key = topic.downcase
  text =
  case key
  when "commands", "command"
    "Ex commands: use :commands (list), :help <name> (detail)"
  when "regex", "search"
    "Regex uses Ruby Regexp (not Vim regex). :%s/pat/rep/g is minimal parser + Ruby regex."
  when "options", "set"
    "Options: use :set/:setlocal/:setglobal. See :help number, :help relativenumber, :help ignorecase, :help smartcase, :help hlsearch"
  when "config"
    "Config: XDG Ruby DSL at ~/.config/ruvim/init.rb and ftplugin/<filetype>.rb"
  when "bindings", "keys", "keymap"
    "Bindings: use :bindings (current effective key bindings by layer). Docs: docs/binding.md"
  when "number", "relativenumber", "ignorecase", "smartcase", "hlsearch", "tabstop", "filetype"
    option_help_line(key)
  else
    if (spec = registry.resolve(topic))
      ex_command_help_line(spec)
    else
      "No help for #{topic}. Try :help or :help commands"
    end
  end
  ctx.editor.show_help_buffer!(title: "[Help] #{topic}", lines: help_text_to_lines(topic, text))
end

#ex_lclose(ctx) ⇒ Object



1409
1410
1411
# File 'lib/ruvim/global_commands.rb', line 1409

def ex_lclose(ctx, **)
  close_list_windows(ctx.editor, :location_list)
end

#ex_lgrep(ctx, argv:, kwargs: {}) ⇒ Object



1466
1467
1468
# File 'lib/ruvim/global_commands.rb', line 1466

def ex_lgrep(ctx, argv:, kwargs: {}, **)
  run_external_grep(ctx, argv:, target: :location_list)
end

#ex_lnext(ctx) ⇒ Object



1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
# File 'lib/ruvim/global_commands.rb', line 1413

def ex_lnext(ctx, **)
  item = ctx.editor.move_location_list(+1, window_id: ctx.window.id)
  unless item
    ctx.editor.echo_error("location list is empty")
    return
  end
  ctx.editor.jump_to_location(item)
  refresh_list_window(ctx.editor, :location_list)
  ctx.editor.echo(location_item_echo(ctx.editor, ctx.window.id))
end

#ex_lopen(ctx) ⇒ Object



1404
1405
1406
1407
# File 'lib/ruvim/global_commands.rb', line 1404

def ex_lopen(ctx, **)
  open_list_window(ctx, kind: :location_list, title: "[Location List]", lines: location_list_buffer_lines(ctx.editor, ctx.window.id),
                   source_window_id: ctx.window.id)
end

#ex_lprev(ctx) ⇒ Object



1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
# File 'lib/ruvim/global_commands.rb', line 1424

def ex_lprev(ctx, **)
  item = ctx.editor.move_location_list(-1, window_id: ctx.window.id)
  unless item
    ctx.editor.echo_error("location list is empty")
    return
  end
  ctx.editor.jump_to_location(item)
  refresh_list_window(ctx.editor, :location_list)
  ctx.editor.echo(location_item_echo(ctx.editor, ctx.window.id))
end

#ex_lvimgrep(ctx, argv:) ⇒ Object



1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
# File 'lib/ruvim/global_commands.rb', line 1359

def ex_lvimgrep(ctx, argv:, **)
  pattern = parse_vimgrep_pattern(argv)
  regex = compile_search_regex(pattern, editor: ctx.editor, window: ctx.window, buffer: ctx.buffer)
  items = grep_items_for_buffers([ctx.buffer], regex)
  if items.empty?
    ctx.editor.echo_error("Pattern not found: #{pattern}")
    return
  end

  ctx.editor.set_location_list(items, window_id: ctx.window.id)
  ctx.editor.select_location_list(0, window_id: ctx.window.id)
  ctx.editor.jump_to_location(ctx.editor.current_location_list_item(ctx.window.id))
  ctx.editor.echo("location list: #{items.length} item(s)")
end

#ex_read(ctx, argv:, kwargs:) ⇒ Object



1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
# File 'lib/ruvim/global_commands.rb', line 1274

def ex_read(ctx, argv:, kwargs:, **)
  arg = argv.join(" ")
  raise RuVim::CommandError, "Usage: :r[ead] [file] or :r[ead] !command" if arg.strip.empty?

  insert_line = kwargs[:range_start] || ctx.window.cursor_y
  new_lines = if arg.start_with?("!")
                command = arg[1..].strip
                raise RuVim::CommandError, "Usage: :r !<command>" if command.empty?

                shell = ENV["SHELL"].to_s
                shell = "/bin/sh" if shell.empty?
                stdout_text, stderr_text, _status = Open3.capture3(shell, "-c", command)
                unless stderr_text.empty?
                  ctx.editor.echo_error(stderr_text.lines(chomp: true).first)
                end
                stdout_text.lines(chomp: true)
              else
                path = File.expand_path(arg.strip)
                raise RuVim::CommandError, "File not found: #{arg.strip}" unless File.exist?(path)

                File.read(path).lines(chomp: true)
              end

  return if new_lines.empty?

  ctx.buffer.insert_lines_at(insert_line + 1, new_lines)
  ctx.window.cursor_y = insert_line + new_lines.length
  ctx.editor.echo("#{new_lines.length} line(s) inserted")
end

#ex_rich(ctx, argv: []) ⇒ Object



1505
1506
1507
1508
# File 'lib/ruvim/global_commands.rb', line 1505

def ex_rich(ctx, argv: [], **)
  format = argv.first
  RuVim::RichView.toggle!(ctx.editor, format: format)
end

#ex_ruby(ctx, argv:) ⇒ Object



1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
# File 'lib/ruvim/global_commands.rb', line 1092

def ex_ruby(ctx, argv:, **)
  raise RuVim::CommandError, "Restricted mode: :ruby is disabled" if ctx.editor.respond_to?(:restricted_mode?) && ctx.editor.restricted_mode?

  code = argv.join(" ")
  raise RuVim::CommandError, "Usage: :ruby <code>" if code.strip.empty?

  b = binding
  # Use local_variable_set for eval locals to avoid "assigned but unused variable"
  # warnings while still exposing editor/buffer/window in :ruby.
  b.local_variable_set(:editor, ctx.editor)
  b.local_variable_set(:buffer, ctx.buffer)
  b.local_variable_set(:window, ctx.window)
  saved_stdout = STDOUT.dup
  saved_stderr = STDERR.dup
  original_g_stdout = $stdout
  original_g_stderr = $stderr
  result = nil
  stdout_text = ""
  stderr_text = ""
  Tempfile.create("ruvim-ruby-stdout") do |outf|
    Tempfile.create("ruvim-ruby-stderr") do |errf|
      STDOUT.reopen(outf)
      STDERR.reopen(errf)
      $stdout = STDOUT
      $stderr = STDERR
      result = eval(code, b) # rubocop:disable Security/Eval
      STDOUT.flush
      STDERR.flush
      outf.flush
      errf.flush
      outf.rewind
      errf.rewind
      stdout_text = outf.read
      stderr_text = errf.read
    end
  end
  if !stdout_text.empty? || !stderr_text.empty?
    lines = ["Ruby output", ""]
    unless stdout_text.empty?
      lines << "[stdout]"
      lines.concat(stdout_text.lines(chomp: true))
      lines << ""
    end
    unless stderr_text.empty?
      lines << "[stderr]"
      lines.concat(stderr_text.lines(chomp: true))
      lines << ""
    end
    lines << "[result]"
    lines << (result.nil? ? "nil" : result.inspect)
    ctx.editor.show_help_buffer!(title: "[Ruby Output]", lines:, filetype: "ruby")
  else
    ctx.editor.echo(result.nil? ? "ruby: nil" : "ruby: #{result.inspect}")
  end
rescue StandardError => e
  raise RuVim::CommandError, "Ruby error: #{e.class}: #{e.message}"
ensure
  if defined?(saved_stdout) && saved_stdout
    STDOUT.reopen(saved_stdout)
    saved_stdout.close unless saved_stdout.closed?
  end
  if defined?(saved_stderr) && saved_stderr
    STDERR.reopen(saved_stderr)
    saved_stderr.close unless saved_stderr.closed?
  end
  $stdout = (defined?(original_g_stdout) && original_g_stdout) ? original_g_stdout : STDOUT
  $stderr = (defined?(original_g_stderr) && original_g_stderr) ? original_g_stderr : STDERR
end

#ex_run(ctx, argv:) ⇒ Object



1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
# File 'lib/ruvim/global_commands.rb', line 1161

def ex_run(ctx, argv:, **)
  editor = ctx.editor
  source_buffer = ctx.buffer

  if argv.empty?
    # No args: use last command for this buffer, or runprg
    command = editor.run_history[source_buffer.id]
    if command.nil?
      runprg = editor.get_option("runprg", buffer: source_buffer)
      raise RuVim::CommandError, "No runprg set and no previous :run command" unless runprg
      command = runprg
    end
  else
    command = argv.first
  end

  # Auto-save modified buffer before running
  if source_buffer.modified? && source_buffer.path
    source_buffer.write_to
  end

  expanded = expand_run_command(command, source_buffer)
  editor.run_history[source_buffer.id] = command

  # Find or create [Shell Output] buffer
  output_buf = if editor.run_output_buffer_id
                 editor.buffers[editor.run_output_buffer_id]
               end

  if output_buf
    # Reuse: clear content
    output_buf.replace_all_lines!([""])
  else
    output_buf = editor.add_virtual_buffer(
      kind: :run_output,
      name: "[Shell Output]",
      lines: [""],
      readonly: true,
      modifiable: false
    )
    editor.run_output_buffer_id = output_buf.id
  end
  # Open output buffer in a split (reuse existing window if present)
  existing_win = editor.windows.values.find { |w| w.buffer_id == output_buf.id }
  if existing_win
    editor.current_window_id = existing_win.id
  else
    win = editor.split_current_window(layout: :horizontal, place: :after)
    win.buffer_id = output_buf.id
    win.cursor_x = 0
    win.cursor_y = 0
    win.row_offset = 0
  end

  # Start streaming (handler creates Stream::Run and sets buf.stream)
  handler = editor.run_stream_handler
  if handler
    handler.call(output_buf, expanded)
  else
    # Fallback for tests without stream handler: synchronous execution
    shell = ENV["SHELL"].to_s
    shell = "/bin/sh" if shell.empty?
    output, _status = Open3.capture2e(shell, "-c", expanded)
    output_buf.replace_all_lines!(output.lines(chomp: true))
  end
end

#ex_set(ctx, argv:) ⇒ Object



1332
1333
1334
# File 'lib/ruvim/global_commands.rb', line 1332

def ex_set(ctx, argv:, **)
  ex_set_common(ctx, argv, scope: :auto)
end

#ex_setglobal(ctx, argv:) ⇒ Object



1340
1341
1342
# File 'lib/ruvim/global_commands.rb', line 1340

def ex_setglobal(ctx, argv:, **)
  ex_set_common(ctx, argv, scope: :global)
end

#ex_setlocal(ctx, argv:) ⇒ Object



1336
1337
1338
# File 'lib/ruvim/global_commands.rb', line 1336

def ex_setlocal(ctx, argv:, **)
  ex_set_common(ctx, argv, scope: :local)
end

#ex_shell(ctx, command:) ⇒ Object



1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
# File 'lib/ruvim/global_commands.rb', line 1237

def ex_shell(ctx, command:, **)
  raise RuVim::CommandError, "Restricted mode: :! is disabled" if ctx.editor.respond_to?(:restricted_mode?) && ctx.editor.restricted_mode?

  raise RuVim::CommandError, "Usage: :!<command>" if command.strip.empty?

  executor = ctx.editor.shell_executor
  if executor
    status = executor.call(command)
    ctx.editor.echo("shell exit #{status.exitstatus}")
  else
    shell = ENV["SHELL"].to_s
    shell = "/bin/sh" if shell.empty?
    stdout_text, stderr_text, status = Open3.capture3(shell, "-c", command)

    if !stdout_text.empty? || !stderr_text.empty?
      lines = ["Shell output", "", "[command]", command, ""]
      unless stdout_text.empty?
        lines << "[stdout]"
        lines.concat(stdout_text.lines(chomp: true))
        lines << ""
      end
      unless stderr_text.empty?
        lines << "[stderr]"
        lines.concat(stderr_text.lines(chomp: true))
        lines << ""
      end
      lines << "[status]"
      lines << "exit #{status.exitstatus}"
      ctx.editor.show_help_buffer!(title: "[Shell Output]", lines:, filetype: "sh")
    else
      ctx.editor.echo("shell exit #{status.exitstatus}")
    end
  end
rescue Errno::ENOENT => e
  raise RuVim::CommandError, "Shell error: #{e.message}"
end

#ex_substitute(ctx, pattern:, replacement:, flags_str: nil, range_start: nil, range_end: nil, global: false) ⇒ Object



1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
# File 'lib/ruvim/global_commands.rb', line 1435

def ex_substitute(ctx, pattern:, replacement:, flags_str: nil, range_start: nil, range_end: nil, global: false, **)
  materialize_intro_buffer_if_needed(ctx)
  flags = parse_substitute_flags(flags_str, default_global: global)
  raise RuVim::CommandError, "Confirm flag (:s///c) is not yet supported" if flags[:confirm]

  regex = build_substitute_regex(pattern, flags, ctx)

  r_start = range_start || 0
  r_end = range_end || (ctx.buffer.line_count - 1)

  if flags[:count_only]
    total = count_matches_in_range(ctx.buffer, regex, r_start, r_end, flags[:global])
    ctx.editor.echo("#{total} match(es)")
    return
  end

  changed = substitute_range(ctx, regex, replacement, r_start, r_end, flags)

  if changed.positive?
    ctx.editor.echo("#{changed} substitution(s)")
  elsif flags[:no_error]
    ctx.editor.echo("Pattern not found: #{pattern}")
  else
    ctx.editor.echo("Pattern not found: #{pattern}")
  end
end

#ex_vimgrep(ctx, argv:) ⇒ Object



1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
# File 'lib/ruvim/global_commands.rb', line 1344

def ex_vimgrep(ctx, argv:, **)
  pattern = parse_vimgrep_pattern(argv)
  regex = compile_search_regex(pattern, editor: ctx.editor, window: ctx.window, buffer: ctx.buffer)
  items = grep_items_for_buffers(ctx.editor.buffers.values.select(&:file_buffer?), regex)
  if items.empty?
    ctx.editor.echo_error("Pattern not found: #{pattern}")
    return
  end

  ctx.editor.set_quickfix_list(items)
  ctx.editor.select_quickfix(0)
  ctx.editor.jump_to_location(ctx.editor.current_quickfix_item)
  ctx.editor.echo("quickfix: #{items.length} item(s)")
end

#ex_yank_lines(ctx, kwargs: {}) ⇒ Object



1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
# File 'lib/ruvim/global_commands.rb', line 1491

def ex_yank_lines(ctx, kwargs: {}, **)
  materialize_intro_buffer_if_needed(ctx)
  r_start = kwargs[:range_start]
  r_end = kwargs[:range_end]
  unless r_start && r_end
    r_start = r_end = ctx.window.cursor_y
  end

  count = r_end - r_start + 1
  text = ctx.buffer.line_block_text(r_start, count)
  store_yank_register(ctx, text:, type: :linewise)
  ctx.editor.echo("#{count} line(s) yanked")
end

#expand_run_command(command, buffer) ⇒ Object



1228
1229
1230
1231
1232
1233
1234
1235
# File 'lib/ruvim/global_commands.rb', line 1228

def expand_run_command(command, buffer)
  return command unless command.include?("%")

  path = buffer.path
  raise RuVim::CommandError, "No file name (use % in :run requires a file)" unless path

  command.gsub("%", path)
end

#file_edit(ctx, argv:, bang:) ⇒ Object



879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
# File 'lib/ruvim/global_commands.rb', line 879

def file_edit(ctx, argv:, bang:, **)
  path = argv[0]
  if path.nil? || path.empty?
    current_path = ctx.buffer.path
    raise RuVim::CommandError, "Argument required" if current_path.nil? || current_path.empty?

    if ctx.buffer.modified? && !bang
      ctx.editor.echo_error("Unsaved changes (use :e! to discard)")
      return
    end

    target = ctx.buffer.reload_from_file!(current_path)
    ctx.window.clamp_to_buffer(ctx.buffer)
    ctx.editor.echo("\"#{target}\" reloaded")
    return
  end

  if ctx.buffer.modified? && !bang
    if ctx.editor.effective_option("hidden", window: ctx.window, buffer: ctx.buffer)
      # hidden permits abandoning a modified buffer without forcing write.
    elsif maybe_autowrite_before_switch(ctx)
      # autowrite handled
    else
      ctx.editor.echo_error("Unsaved changes (use :e! to discard and open)")
      return
    end
  end

  ctx.editor.open_path(path)
end

#file_goto_under_cursor(ctx) ⇒ Object



910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
# File 'lib/ruvim/global_commands.rb', line 910

def file_goto_under_cursor(ctx, **)
  token = file_token_under_cursor(ctx.buffer, ctx.window)
  if token.nil? || token.empty?
    ctx.editor.echo_error("No file under cursor")
    return
  end

  target = parse_gf_target(token)
  path = resolve_gf_path(ctx, target[:path])
  unless path
    ctx.editor.echo_error("File not found: #{target[:path]}")
    return
  end

  if ctx.buffer.modified? && !ctx.editor.effective_option("hidden", window: ctx.window, buffer: ctx.buffer)
    unless maybe_autowrite_before_switch(ctx)
      ctx.editor.echo_error("Unsaved changes (set hidden or :w)")
      return
    end
  end

  ctx.editor.open_path(path)
  move_cursor_to_gf_line(ctx, target[:line]) if target[:line]
end

#file_write(ctx, argv:, bang:, kwargs: {}) ⇒ Object



777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
# File 'lib/ruvim/global_commands.rb', line 777

def file_write(ctx, argv:, bang:, kwargs: {}, **)
  if ctx.buffer.kind == :git_commit
    git_commit_execute(ctx)
    return
  end

  arg = argv.join(" ")
  if arg.start_with?("!")
    file_write_to_shell(ctx, command: arg[1..].strip, kwargs: kwargs)
    return
  end

  path = argv[0]
  target = ctx.buffer.write_to(path)
  size = File.exist?(target) ? File.size(target) : 0
  suffix = bang ? " (force accepted)" : ""
  ctx.editor.echo("\"#{target}\" #{ctx.buffer.line_count}L, #{size}B written#{suffix}")
  if ctx.editor.get_option("onsavehook")
    ctx.buffer.lang_module.on_save(ctx, target)
  end
end

#file_write_quit(ctx, argv:, bang:) ⇒ Object



873
874
875
876
877
# File 'lib/ruvim/global_commands.rb', line 873

def file_write_quit(ctx, argv:, bang:, **)
  file_write(ctx, argv:, bang:)
  return unless ctx.editor.running?
  app_quit(ctx, bang: true)
end

#file_write_quit_all(ctx, bang:) ⇒ Object



865
866
867
868
869
870
871
# File 'lib/ruvim/global_commands.rb', line 865

def file_write_quit_all(ctx, bang:, **)
  ctx.editor.buffers.each_value do |buf|
    next unless buf.file_buffer? && buf.modified? && buf.path
    buf.write_to(buf.path)
  end
  app_quit_all(ctx, bang: true)
end

#file_write_to_shell(ctx, command:, kwargs: {}) ⇒ Object



799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
# File 'lib/ruvim/global_commands.rb', line 799

def file_write_to_shell(ctx, command:, kwargs: {})
  raise RuVim::CommandError, "Usage: :w !<command>" if command.empty?

  r_start = kwargs[:range_start] || 0
  r_end = kwargs[:range_end] || (ctx.buffer.line_count - 1)
  lines = (r_start..r_end).map { |i| ctx.buffer.lines[i] }
  input = lines.join("\n") + "\n"

  shell = ENV["SHELL"].to_s
  shell = "/bin/sh" if shell.empty?
  _stdout, stderr_text, status = Open3.capture3(shell, "-c", command, stdin_data: input)
  unless stderr_text.empty?
    ctx.editor.echo_error(stderr_text.lines(chomp: true).first)
    return
  end
  ctx.editor.echo("#{lines.length} line(s) written to !#{command}, exit #{status.exitstatus}")
end

#indent_lines(ctx, count:) ⇒ Object



719
720
721
722
723
724
# File 'lib/ruvim/global_commands.rb', line 719

def indent_lines(ctx, count:, **)
  count = normalized_count(count)
  start_row = ctx.window.cursor_y
  end_row = [start_row + count - 1, ctx.buffer.line_count - 1].min
  reindent_range(ctx, start_row, end_row)
end

#indent_motion(ctx, count:, kwargs:) ⇒ Object



726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
# File 'lib/ruvim/global_commands.rb', line 726

def indent_motion(ctx, count:, kwargs:, **)
  motion = (kwargs[:motion] || kwargs["motion"]).to_s
  ncount = normalized_count(count)
  start_row = ctx.window.cursor_y
  case motion
  when "j"
    end_row = [start_row + ncount, ctx.buffer.line_count - 1].min
  when "k"
    end_row = start_row
    start_row = [start_row - ncount, 0].max
  when "G"
    end_row = ctx.buffer.line_count - 1
  when "gg"
    end_row = start_row
    start_row = 0
  else
    ctx.editor.echo("Unsupported motion for =: #{motion}")
    return
  end
  reindent_range(ctx, start_row, end_row)
end

#insert_line_start_nonblank_mode(ctx) ⇒ Object



187
188
189
190
# File 'lib/ruvim/global_commands.rb', line 187

def insert_line_start_nonblank_mode(ctx, **)
  cursor_first_nonblank(ctx)
  enter_insert_mode(ctx)
end

#join_lines(ctx, count:) ⇒ Object



409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
# File 'lib/ruvim/global_commands.rb', line 409

def join_lines(ctx, count:, **)
  materialize_intro_buffer_if_needed(ctx)
  joins = [normalized_count(count) - 1, 1].max
  y = ctx.window.cursor_y
  x = ctx.window.cursor_x
  changed = false

  ctx.buffer.begin_change_group
  joins.times do
    break if y >= ctx.buffer.line_count - 1

    left = ctx.buffer.line_at(y)
    right = ctx.buffer.line_at(y + 1)
    join_col = left.length

    # Join raw lines first.
    break unless ctx.buffer.delete_char(y, join_col)

    right_trimmed = right.sub(/\A\s+/, "")
    trimmed_count = right.length - right_trimmed.length
    if trimmed_count.positive?
      ctx.buffer.delete_span(y, join_col, y, join_col + trimmed_count)
    end

    need_space = !left.empty? && !left.match?(/\s\z/) && !right_trimmed.empty? && !right_trimmed.match?(/\A\s/)
    ctx.buffer.insert_char(y, join_col, " ") if need_space

    x = join_col
    changed = true
  end
  ctx.buffer.end_change_group

  ctx.window.cursor_y = y
  ctx.window.cursor_x = x
  ctx.window.clamp_to_buffer(ctx.buffer)
  ctx.editor.echo("joined") if changed
end

#jump_newer(ctx, kwargs: {}) ⇒ Object



581
582
583
584
585
# File 'lib/ruvim/global_commands.rb', line 581

def jump_newer(ctx, kwargs: {}, **)
  linewise = !!(kwargs[:linewise] || kwargs["linewise"])
  loc = ctx.editor.jump_newer(linewise:)
  ctx.editor.echo(loc ? "<C-i>" : "At newest jump")
end

#jump_older(ctx, kwargs: {}) ⇒ Object



575
576
577
578
579
# File 'lib/ruvim/global_commands.rb', line 575

def jump_older(ctx, kwargs: {}, **)
  linewise = !!(kwargs[:linewise] || kwargs["linewise"])
  loc = ctx.editor.jump_older(linewise:)
  ctx.editor.echo(loc ? (linewise ? "''" : "``") : "Jump list empty")
end

#mark_jump(ctx, kwargs:) ⇒ Object



563
564
565
566
567
568
569
570
571
572
573
# File 'lib/ruvim/global_commands.rb', line 563

def mark_jump(ctx, kwargs:, **)
  mark = (kwargs[:mark] || kwargs["mark"]).to_s
  linewise = !!(kwargs[:linewise] || kwargs["linewise"])
  record_jump(ctx)
  loc = ctx.editor.jump_to_mark(mark, linewise:)
  if loc
    ctx.editor.echo("#{linewise ? "'" : '`'}#{mark}")
  else
    ctx.editor.echo("Mark not set: #{mark}")
  end
end

#mark_set(ctx, kwargs:) ⇒ Object



556
557
558
559
560
561
# File 'lib/ruvim/global_commands.rb', line 556

def mark_set(ctx, kwargs:, **)
  mark = (kwargs[:mark] || kwargs["mark"]).to_s
  raise RuVim::CommandError, "Invalid mark" unless ctx.editor.set_mark(mark)

  ctx.editor.echo("mark #{mark}")
end

#open_line_above(ctx) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/ruvim/global_commands.rb', line 206

def open_line_above(ctx, **)
  materialize_intro_buffer_if_needed(ctx)
  ensure_modifiable_for_insert!(ctx)
  y = ctx.window.cursor_y
  ctx.buffer.begin_change_group
  _new_y, new_x = ctx.buffer.insert_newline(y, 0)
  new_x = apply_autoindent_to_newline(ctx, row: y, previous_row: y + 1, start_col: 0)
  ctx.window.cursor_y = y
  ctx.window.cursor_x = new_x
  ctx.editor.enter_insert_mode
  ctx.editor.echo("-- INSERT --")
end

#open_line_below(ctx) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/ruvim/global_commands.rb', line 192

def open_line_below(ctx, **)
  materialize_intro_buffer_if_needed(ctx)
  ensure_modifiable_for_insert!(ctx)
  y = ctx.window.cursor_y
  x = ctx.buffer.line_length(y)
  ctx.buffer.begin_change_group
  new_y, new_x = ctx.buffer.insert_newline(y, x)
  new_x = apply_autoindent_to_newline(ctx, row: new_y, previous_row: y, start_col: new_x)
  ctx.window.cursor_y = new_y
  ctx.window.cursor_x = new_x
  ctx.editor.enter_insert_mode
  ctx.editor.echo("-- INSERT --")
end

#paste_after(ctx, count:) ⇒ Object



647
648
649
650
# File 'lib/ruvim/global_commands.rb', line 647

def paste_after(ctx, count:, **)
  materialize_intro_buffer_if_needed(ctx)
  paste_register(ctx, before: false, count:)
end

#paste_before(ctx, count:) ⇒ Object



652
653
654
655
# File 'lib/ruvim/global_commands.rb', line 652

def paste_before(ctx, count:, **)
  materialize_intro_buffer_if_needed(ctx)
  paste_register(ctx, before: true, count:)
end

#replace_char(ctx, argv:, count:) ⇒ Object



587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
# File 'lib/ruvim/global_commands.rb', line 587

def replace_char(ctx, argv:, count:, **)
  materialize_intro_buffer_if_needed(ctx)
  count = normalized_count(count)
  ch = argv[0].to_s
  raise RuVim::CommandError, "replace requires a character" if ch.empty?

  y = ctx.window.cursor_y
  x = ctx.window.cursor_x
  line = ctx.buffer.line_at(y)
  return if x >= line.length

  ctx.buffer.begin_change_group
  count.times do |i|
    cx = x + i
    break if cx >= ctx.buffer.line_length(y)
    ctx.buffer.delete_span(y, cx, y, cx + 1)
    ctx.buffer.insert_char(y, cx, ch[0])
  end
  ctx.buffer.end_change_group
  ctx.window.clamp_to_buffer(ctx.buffer)
end

#rich_toggle(ctx) ⇒ Object



1510
1511
1512
# File 'lib/ruvim/global_commands.rb', line 1510

def rich_toggle(ctx, **)
  RuVim::RichView.toggle!(ctx.editor)
end

#rich_view_close_buffer(ctx) ⇒ Object



1514
1515
1516
# File 'lib/ruvim/global_commands.rb', line 1514

def rich_view_close_buffer(ctx, **)
  ctx.editor.delete_buffer(ctx.buffer.id)
end

#search_filter(ctx) ⇒ Object



1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
# File 'lib/ruvim/global_commands.rb', line 1518

def search_filter(ctx, **)
  editor = ctx.editor
  search = editor.last_search
  unless search
    editor.echo_error("No search pattern")
    return
  end

  regex = compile_search_regex(search[:pattern], editor: editor, window: ctx.window, buffer: ctx.buffer)
  source_buffer = ctx.buffer

  # Collect matching lines with origin mapping
  origins = []
  matching_lines = []
  source_buffer.lines.each_with_index do |line, row|
    if regex.match?(line)
      # If source is a filter buffer, chain back to the original
      if source_buffer.kind == :filter && source_buffer.options["filter_origins"]
        origins << source_buffer.options["filter_origins"][row]
      else
        origins << { buffer_id: source_buffer.id, row: row }
      end
      matching_lines << line
    end
  end

  if matching_lines.empty?
    editor.echo_error("Pattern not found: #{search[:pattern]}")
    return
  end

  filetype = source_buffer.options["filetype"]
  filter_buf = editor.add_virtual_buffer(
    kind: :filter,
    name: "[Filter: /#{search[:pattern]}/]",
    lines: matching_lines,
    filetype: filetype,
    readonly: false,
    modifiable: false
  )
  filter_buf.options["filter_origins"] = origins
  filter_buf.options["filter_source_buffer_id"] = source_buffer.id
  filter_buf.options["filter_source_cursor_y"] = ctx.window.cursor_y
  filter_buf.options["filter_source_cursor_x"] = ctx.window.cursor_x
  filter_buf.options["filter_source_row_offset"] = ctx.window.row_offset
  filter_buf.options["filter_source_col_offset"] = ctx.window.col_offset
  editor.switch_to_buffer(filter_buf.id)
  editor.echo("filter: #{matching_lines.length} line(s)")
end

#search_next(ctx, count:) ⇒ Object



526
527
528
529
# File 'lib/ruvim/global_commands.rb', line 526

def search_next(ctx, count:, **)
  record_jump(ctx)
  repeat_search(ctx, forward: true, count:)
end

#search_prev(ctx, count:) ⇒ Object



531
532
533
534
# File 'lib/ruvim/global_commands.rb', line 531

def search_prev(ctx, count:, **)
  record_jump(ctx)
  repeat_search(ctx, forward: false, count:)
end

#search_word_backward(ctx) ⇒ Object



541
542
543
544
# File 'lib/ruvim/global_commands.rb', line 541

def search_word_backward(ctx, **)
  record_jump(ctx)
  search_current_word(ctx, exact: true, direction: :backward)
end

#search_word_backward_partial(ctx) ⇒ Object



551
552
553
554
# File 'lib/ruvim/global_commands.rb', line 551

def search_word_backward_partial(ctx, **)
  record_jump(ctx)
  search_current_word(ctx, exact: false, direction: :backward)
end

#search_word_forward(ctx) ⇒ Object



536
537
538
539
# File 'lib/ruvim/global_commands.rb', line 536

def search_word_forward(ctx, **)
  record_jump(ctx)
  search_current_word(ctx, exact: true, direction: :forward)
end

#search_word_forward_partial(ctx) ⇒ Object



546
547
548
549
# File 'lib/ruvim/global_commands.rb', line 546

def search_word_forward_partial(ctx, **)
  record_jump(ctx)
  search_current_word(ctx, exact: false, direction: :forward)
end

#submit_search(ctx, pattern:, direction:) ⇒ Object



1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
# File 'lib/ruvim/global_commands.rb', line 1577

def submit_search(ctx, pattern:, direction:)
  text = pattern.to_s
  if text.empty?
    prev = ctx.editor.last_search
    raise RuVim::CommandError, "No previous search" unless prev
    text = prev[:pattern]
  end
  compile_search_regex(text, editor: ctx.editor, window: ctx.window, buffer: ctx.buffer)
  ctx.editor.set_last_search(pattern: text, direction:)
  record_jump(ctx)
  move_to_search(ctx, pattern: text, direction:, count: 1)
end

#substitute_char(ctx, count:, bang:) ⇒ Object



376
377
378
# File 'lib/ruvim/global_commands.rb', line 376

def substitute_char(ctx, count:, bang:, **)
  call(:change_motion, ctx, count:, bang:, kwargs: { motion: "l" })
end

#swapcase_char(ctx, count:) ⇒ Object



380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# File 'lib/ruvim/global_commands.rb', line 380

def swapcase_char(ctx, count:, **)
  materialize_intro_buffer_if_needed(ctx)
  count = normalized_count(count)

  y = ctx.window.cursor_y
  x = ctx.window.cursor_x
  processed = false

  ctx.buffer.begin_change_group
  count.times do
    line = ctx.buffer.line_at(y)
    break if x >= line.length

    ch = line[x]
    swapped = ch.swapcase
    if !swapped.empty? && swapped != ch
      ctx.buffer.delete_span(y, x, y, x + 1)
      ctx.buffer.insert_char(y, x, swapped[0])
    end
    processed = true
    x += 1
  end
  ctx.buffer.end_change_group

  ctx.window.cursor_y = y
  ctx.window.cursor_x = processed ? x : ctx.window.cursor_x
  ctx.window.clamp_to_buffer(ctx.buffer)
end

#tab_list(ctx) ⇒ Object



323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/ruvim/global_commands.rb', line 323

def tab_list(ctx, **)
  editor = ctx.editor
  items = []
  # For current tab, use live window_order; for others, use saved snapshot
  editor.tabpages.each_with_index do |tab, i|
    is_current = (i == editor.current_tabpage_index)
    current_marker = is_current ? ">" : " "
    items << "#{current_marker}Tab page #{i + 1}"
    win_ids = is_current ? editor.window_order : editor.tabpage_windows(tab)
    win_ids.each do |wid|
      win = editor.windows[wid]
      next unless win
      buf = editor.buffers[win.buffer_id]
      next unless buf
      active = (is_current && wid == editor.current_window_id) ? ">" : " "
      name = buf.display_name
      items << "  #{active} #{name}"
    end
  end
  editor.echo_multiline(items)
end

#tab_new(ctx, argv:) ⇒ Object



302
303
304
305
306
307
308
309
310
311
# File 'lib/ruvim/global_commands.rb', line 302

def tab_new(ctx, argv:, **)
  path = argv[0]
  if ctx.buffer.modified? && !ctx.editor.effective_option("hidden", window: ctx.window, buffer: ctx.buffer)
    unless maybe_autowrite_before_switch(ctx)
      ctx.editor.echo_error("Unsaved changes (use :w or :q!)")
      return
    end
  end
  ctx.editor.tabnew(path: path)
end

#tab_next(ctx, count:) ⇒ Object



313
314
315
316
# File 'lib/ruvim/global_commands.rb', line 313

def tab_next(ctx, count:, **)
  count = normalized_count(count)
  ctx.editor.tabnext(count)
end

#tab_prev(ctx, count:) ⇒ Object



318
319
320
321
# File 'lib/ruvim/global_commands.rb', line 318

def tab_prev(ctx, count:, **)
  count = normalized_count(count)
  ctx.editor.tabprev(count)
end

#visual_delete(ctx) ⇒ Object



677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
# File 'lib/ruvim/global_commands.rb', line 677

def visual_delete(ctx, **)
  materialize_intro_buffer_if_needed(ctx)
  sel = ctx.editor.visual_selection
  return unless sel

  if sel[:mode] == :linewise
    count = sel[:end_row] - sel[:start_row] + 1
    text = ctx.buffer.line_block_text(sel[:start_row], count)
    ctx.buffer.begin_change_group
    count.times { ctx.buffer.delete_line(sel[:start_row]) }
    ctx.buffer.end_change_group
    store_delete_register(ctx, text:, type: :linewise)
    ctx.window.cursor_y = [sel[:start_row], ctx.buffer.line_count - 1].min
    ctx.window.cursor_x = 0
  elsif sel[:mode] == :blockwise
    text = blockwise_selection_text(ctx.buffer, sel)
    ctx.buffer.begin_change_group
    (sel[:start_row]..sel[:end_row]).each do |row|
      line = ctx.buffer.line_at(row)
      s_col = [sel[:start_col], line.length].min
      e_col = [sel[:end_col], line.length].min
      next if e_col <= s_col

      ctx.buffer.delete_span(row, s_col, row, e_col)
    end
    ctx.buffer.end_change_group
    store_delete_register(ctx, text:, type: :charwise)
    ctx.window.cursor_y = sel[:start_row]
    ctx.window.cursor_x = sel[:start_col]
  else
    text = ctx.buffer.span_text(sel[:start_row], sel[:start_col], sel[:end_row], sel[:end_col])
    ctx.buffer.begin_change_group
    ctx.buffer.delete_span(sel[:start_row], sel[:start_col], sel[:end_row], sel[:end_col])
    ctx.buffer.end_change_group
    store_delete_register(ctx, text:, type: :charwise)
    ctx.window.cursor_y = sel[:start_row]
    ctx.window.cursor_x = sel[:start_col]
  end
  ctx.window.clamp_to_buffer(ctx.buffer)
  ctx.editor.enter_normal_mode
end

#visual_indent(ctx) ⇒ Object



748
749
750
751
752
753
754
755
756
# File 'lib/ruvim/global_commands.rb', line 748

def visual_indent(ctx, **)
  sel = ctx.editor.visual_selection
  return unless sel

  start_row = sel[:start_row]
  end_row = sel[:end_row]
  reindent_range(ctx, start_row, end_row)
  ctx.editor.enter_normal_mode
end

#visual_select_text_object(ctx, kwargs:) ⇒ Object



758
759
760
761
762
763
764
765
766
767
768
769
770
771
# File 'lib/ruvim/global_commands.rb', line 758

def visual_select_text_object(ctx, kwargs:, **)
  motion = (kwargs[:motion] || kwargs["motion"]).to_s
  span = text_object_span(ctx.buffer, ctx.window, motion)
  return false unless span

  ctx.editor.enter_visual(:visual_char) unless ctx.editor.mode == :visual_char
  v = ctx.editor.visual_state
  v[:anchor_y] = span[:start_row]
  v[:anchor_x] = span[:start_col]
  ctx.window.cursor_y = span[:end_row]
  ctx.window.cursor_x = [span[:end_col] - 1, 0].max
  ctx.window.clamp_to_buffer(ctx.buffer)
  true
end

#visual_yank(ctx) ⇒ Object



657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
# File 'lib/ruvim/global_commands.rb', line 657

def visual_yank(ctx, **)
  sel = ctx.editor.visual_selection
  return unless sel

  if sel[:mode] == :linewise
    count = sel[:end_row] - sel[:start_row] + 1
    text = ctx.buffer.line_block_text(sel[:start_row], count)
    store_yank_register(ctx, text:, type: :linewise)
  elsif sel[:mode] == :blockwise
    text = blockwise_selection_text(ctx.buffer, sel)
    # Blockwise register/paste semantics are not implemented yet; store text payload.
    store_yank_register(ctx, text:, type: :charwise)
  else
    text = ctx.buffer.span_text(sel[:start_row], sel[:start_col], sel[:end_row], sel[:end_col])
    store_yank_register(ctx, text:, type: :charwise)
  end
  ctx.editor.enter_normal_mode
  ctx.editor.echo("yanked")
end

#window_cursor_line_bottom(ctx, count:) ⇒ Object



85
86
87
# File 'lib/ruvim/global_commands.rb', line 85

def window_cursor_line_bottom(ctx, count:, **)
  place_cursor_line_in_window(ctx, where: :bottom, count:)
end

#window_cursor_line_center(ctx, count:) ⇒ Object



81
82
83
# File 'lib/ruvim/global_commands.rb', line 81

def window_cursor_line_center(ctx, count:, **)
  place_cursor_line_in_window(ctx, where: :center, count:)
end

#window_cursor_line_top(ctx, count:) ⇒ Object



77
78
79
# File 'lib/ruvim/global_commands.rb', line 77

def window_cursor_line_top(ctx, count:, **)
  place_cursor_line_in_window(ctx, where: :top, count:)
end

#window_focus_down(ctx) ⇒ Object



262
263
264
# File 'lib/ruvim/global_commands.rb', line 262

def window_focus_down(ctx, **)
  ctx.editor.focus_window_direction(:down)
end

#window_focus_left(ctx) ⇒ Object



250
251
252
# File 'lib/ruvim/global_commands.rb', line 250

def window_focus_left(ctx, **)
  ctx.editor.focus_window_direction(:left)
end

#window_focus_next(ctx) ⇒ Object



246
247
248
# File 'lib/ruvim/global_commands.rb', line 246

def window_focus_next(ctx, **)
  ctx.editor.focus_next_window
end

#window_focus_or_split_down(ctx) ⇒ Object



293
294
295
296
297
298
299
300
# File 'lib/ruvim/global_commands.rb', line 293

def window_focus_or_split_down(ctx, **)
  ed = ctx.editor
  if ed.has_split_ancestor_on_axis?(:down)
    ed.focus_window_direction(:down)
  else
    ed.split_current_window(layout: :horizontal, place: :after)
  end
end

#window_focus_or_split_left(ctx) ⇒ Object



266
267
268
269
270
271
272
273
# File 'lib/ruvim/global_commands.rb', line 266

def window_focus_or_split_left(ctx, **)
  ed = ctx.editor
  if ed.has_split_ancestor_on_axis?(:left)
    ed.focus_window_direction(:left)
  else
    ed.split_current_window(layout: :vertical, place: :before)
  end
end

#window_focus_or_split_right(ctx) ⇒ Object



275
276
277
278
279
280
281
282
# File 'lib/ruvim/global_commands.rb', line 275

def window_focus_or_split_right(ctx, **)
  ed = ctx.editor
  if ed.has_split_ancestor_on_axis?(:right)
    ed.focus_window_direction(:right)
  else
    ed.split_current_window(layout: :vertical, place: :after)
  end
end

#window_focus_or_split_up(ctx) ⇒ Object



284
285
286
287
288
289
290
291
# File 'lib/ruvim/global_commands.rb', line 284

def window_focus_or_split_up(ctx, **)
  ed = ctx.editor
  if ed.has_split_ancestor_on_axis?(:up)
    ed.focus_window_direction(:up)
  else
    ed.split_current_window(layout: :horizontal, place: :before)
  end
end

#window_focus_right(ctx) ⇒ Object



254
255
256
# File 'lib/ruvim/global_commands.rb', line 254

def window_focus_right(ctx, **)
  ctx.editor.focus_window_direction(:right)
end

#window_focus_up(ctx) ⇒ Object



258
259
260
# File 'lib/ruvim/global_commands.rb', line 258

def window_focus_up(ctx, **)
  ctx.editor.focus_window_direction(:up)
end

#window_scroll_down(ctx, kwargs:, count:) ⇒ Object



65
66
67
# File 'lib/ruvim/global_commands.rb', line 65

def window_scroll_down(ctx, kwargs:, count:, **)
  scroll_window_vertically(ctx, direction: :down, lines: kwargs[:lines] || kwargs["lines"], view_height: kwargs[:view_height] || kwargs["view_height"], count:)
end

#window_scroll_down_line(ctx, count:, bang:) ⇒ Object



73
74
75
# File 'lib/ruvim/global_commands.rb', line 73

def window_scroll_down_line(ctx, count:, bang:, **)
  call(:window_scroll_down, ctx, count:, bang:, kwargs: { lines: 1, view_height: current_view_height(ctx) + 1 })
end

#window_scroll_up(ctx, kwargs:, count:) ⇒ Object



61
62
63
# File 'lib/ruvim/global_commands.rb', line 61

def window_scroll_up(ctx, kwargs:, count:, **)
  scroll_window_vertically(ctx, direction: :up, lines: kwargs[:lines] || kwargs["lines"], view_height: kwargs[:view_height] || kwargs["view_height"], count:)
end

#window_scroll_up_line(ctx, count:, bang:) ⇒ Object



69
70
71
# File 'lib/ruvim/global_commands.rb', line 69

def window_scroll_up_line(ctx, count:, bang:, **)
  call(:window_scroll_up, ctx, count:, bang:, kwargs: { lines: 1, view_height: current_view_height(ctx) + 1 })
end

#window_split(ctx) ⇒ Object



234
235
236
237
238
# File 'lib/ruvim/global_commands.rb', line 234

def window_split(ctx, **)
  place = ctx.editor.effective_option("splitbelow", window: ctx.window, buffer: ctx.buffer) ? :after : :before
  ctx.editor.split_current_window(layout: :horizontal, place:)
  ctx.editor.echo("split")
end

#window_vsplit(ctx) ⇒ Object



240
241
242
243
244
# File 'lib/ruvim/global_commands.rb', line 240

def window_vsplit(ctx, **)
  place = ctx.editor.effective_option("splitright", window: ctx.window, buffer: ctx.buffer) ? :after : :before
  ctx.editor.split_current_window(layout: :vertical, place:)
  ctx.editor.echo("vsplit")
end

#yank_line(ctx, count:) ⇒ Object



609
610
611
612
613
614
615
# File 'lib/ruvim/global_commands.rb', line 609

def yank_line(ctx, count:, **)
  count = normalized_count(count)
  start = ctx.window.cursor_y
  text = ctx.buffer.line_block_text(start, count)
  store_yank_register(ctx, text:, type: :linewise)
  ctx.editor.echo("#{count} line(s) yanked")
end

#yank_motion(ctx, count:, kwargs:) ⇒ Object



617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
# File 'lib/ruvim/global_commands.rb', line 617

def yank_motion(ctx, count:, kwargs:, **)
  motion = (kwargs[:motion] || kwargs["motion"]).to_s
  case motion
  when "w"
    y = ctx.window.cursor_y
    x = ctx.window.cursor_x
    target = advance_word_forward(ctx.buffer, y, x, count, editor: ctx.editor, window: ctx.window)
    target ||= { row: y, col: x }
    text = ctx.buffer.span_text(y, x, target[:row], target[:col])
    store_yank_register(ctx, text:, type: :charwise)
    ctx.editor.echo("yanked")
  when "G"
    yank_lines_to_end(ctx)
  when "gg"
    yank_lines_to_start(ctx)
  when "iw"
    yank_text_object_word(ctx, around: false)
  when "aw"
    yank_text_object_word(ctx, around: true)
  when "y"
    yank_line(ctx, count:)
  else
    if text_object_motion?(motion)
      yank_text_object(ctx, motion)
    else
      ctx.editor.echo("Unsupported motion for y: #{motion}")
    end
  end
end