Module: Textbringer::Commands

Includes:
Utils
Included in:
Mode, Mode
Defined in:
lib/textbringer/commands.rb

Constant Summary collapse

RE_SEARCH_STATUS =
{
  last_regexp: nil
}
UNIVERSAL_ARGUMENT_MAP =
Keymap.new
ISEARCH_MODE_MAP =
Keymap.new
ISEARCH_STATUS =
{
  forward: true,
  string: "",
  last_string: "",
  start: 0,
  last_pos: 0
}
@@command_list =
[]

Constants included from Utils

Utils::HOOKS, Utils::Y_OR_N_MAP

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils

#add_hook, #complete, #handle_exception, #message, #read_buffer, #read_char, #read_command_name, #read_file_name, #read_from_minibuffer, #read_single_char, #received_keyboard_quit?, #remove_hook, #run_hooks, #self_insert_and_exit_minibuffer, #set_transient_map, #sit_for, #sleep_for, #y_or_n?, #yes_or_no?

Class Method Details

.define_command(name, &block) ⇒ Object



16
17
18
19
# File 'lib/textbringer/commands.rb', line 16

def define_command(name, &block)
  Commands.send(:define_method, name, &block)
  @@command_list << name if !@@command_list.include?(name)
end

.listObject



12
13
14
# File 'lib/textbringer/commands.rb', line 12

def self.list
  @@command_list
end

.undefine_command(name) ⇒ Object



22
23
24
25
26
27
# File 'lib/textbringer/commands.rb', line 22

def undefine_command(name)
  if @@command_list.include?(name)
    Commands.send(:undef_method, name)
    @@command_list.delete(name)
  end
end

Instance Method Details

#current_prefix_argObject



421
422
423
# File 'lib/textbringer/commands.rb', line 421

def current_prefix_arg
  Controller.current.current_prefix_arg
end

#isearch_doneObject



553
554
555
556
557
558
# File 'lib/textbringer/commands.rb', line 553

def isearch_done
  Buffer.current.delete_visible_mark
  Controller.current.overriding_map = nil
  remove_hook(:pre_command_hook, :isearch_pre_command_hook)
  ISEARCH_STATUS[:last_string] = ISEARCH_STATUS[:string]
end

#isearch_mode(forward) ⇒ Object



527
528
529
530
531
532
533
534
535
536
537
# File 'lib/textbringer/commands.rb', line 527

def isearch_mode(forward)
  ISEARCH_STATUS[:forward] = forward
  ISEARCH_STATUS[:string] = String.new
  Controller.current.overriding_map = ISEARCH_MODE_MAP
  run_hooks(:isearch_mode_hook)
  add_hook(:pre_command_hook, :isearch_pre_command_hook)
  ISEARCH_STATUS[:start] = ISEARCH_STATUS[:last_pos] = Buffer.current.point
  if Buffer.current != Buffer.minibuffer
    message(isearch_prompt, log: false)
  end
end

#isearch_pre_command_hookObject



547
548
549
550
551
# File 'lib/textbringer/commands.rb', line 547

def isearch_pre_command_hook
  if /\Aisearch_/ !~ Controller.current.this_command
    isearch_done
  end
end

#isearch_promptObject



539
540
541
542
543
544
545
# File 'lib/textbringer/commands.rb', line 539

def isearch_prompt
  if ISEARCH_STATUS[:forward]
    "I-search: "
  else
    "I-search backward: "
  end
end

#isearch_repeat(forward) ⇒ Object



614
615
616
617
618
619
620
621
# File 'lib/textbringer/commands.rb', line 614

def isearch_repeat(forward)
  ISEARCH_STATUS[:forward] = forward
  ISEARCH_STATUS[:last_pos] = Buffer.current.point
  if ISEARCH_STATUS[:string].empty?
    ISEARCH_STATUS[:string] = ISEARCH_STATUS[:last_string]
  end
  isearch_search
end

#isearch_repeat_backwardObject



610
611
612
# File 'lib/textbringer/commands.rb', line 610

def isearch_repeat_backward
  isearch_repeat(false)
end

#isearch_repeat_forwardObject



606
607
608
# File 'lib/textbringer/commands.rb', line 606

def isearch_repeat_forward
  isearch_repeat(true)
end

#isearch_searchObject



581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
# File 'lib/textbringer/commands.rb', line 581

def isearch_search
  forward = ISEARCH_STATUS[:forward]
  options = if /\A[A-Z]/ =~ ISEARCH_STATUS[:string]
              nil
            else
              Regexp::IGNORECASE
            end
  re = Regexp.new(Regexp.quote(ISEARCH_STATUS[:string]), options)
  last_pos = ISEARCH_STATUS[:last_pos]
  offset = forward ? last_pos : last_pos - ISEARCH_STATUS[:string].bytesize
  if Buffer.current.byteindex(forward, re, offset)
    if Buffer.current != Buffer.minibuffer
      message(isearch_prompt + ISEARCH_STATUS[:string], log: false)
    end
    Buffer.current.set_visible_mark(forward ? match_beginning(0) :
                                    match_end(0))
    goto_char(forward ? match_end(0) : match_beginning(0))
  else
    if Buffer.current != Buffer.minibuffer
      message("Falling " + isearch_prompt + ISEARCH_STATUS[:string],
              log: false)
    end
  end
end

#match_beginning(n) ⇒ Object



128
129
130
# File 'lib/textbringer/commands.rb', line 128

def match_beginning(n)
  Buffer.current.match_beginning(n)
end

#match_end(n) ⇒ Object



132
133
134
# File 'lib/textbringer/commands.rb', line 132

def match_end(n)
  Buffer.current.match_end(n)
end

#match_string(n) ⇒ Object



136
137
138
# File 'lib/textbringer/commands.rb', line 136

def match_string(n)
  Buffer.current.match_string(n)
end

#number_prefix_argObject



425
426
427
428
429
430
431
432
433
434
435
436
437
# File 'lib/textbringer/commands.rb', line 425

def number_prefix_arg
  arg = current_prefix_arg
  case arg
  when Integer
    arg
  when Array
    arg.first
  when :-
    -1
  else
    1
  end
end

#replace_match(s) ⇒ Object



140
141
142
# File 'lib/textbringer/commands.rb', line 140

def replace_match(s)
  Buffer.current.replace_match(s)
end

#universal_argument_modeObject



412
413
414
# File 'lib/textbringer/commands.rb', line 412

def universal_argument_mode
  set_transient_map(UNIVERSAL_ARGUMENT_MAP)
end