class Macro

Attributes

last_macro[RW]

attr_reader :recorded_macros, :recording, :named_macros

named_macros[RW]

attr_reader :recorded_macros, :recording, :named_macros

recorded_macros[RW]

attr_reader :recorded_macros, :recording, :named_macros

recording[RW]

attr_reader :recorded_macros, :recording, :named_macros

Public Class Methods

new() click to toggle source
# File lib/vimamsa/macro.rb, line 30
def initialize()
  @recording = false
  # @recorded_macros = {}
  @current_recording = []
  @current_name = nil
  @last_macro = "a"

  #TODO:
  @recorded_macros = vma.marshal_load("macros", {})
  @named_macros = vma.marshal_load("named_macros", {})
  $hook.register(:shutdown, self.method("save"))
end

Public Instance Methods

end_recording() click to toggle source
# File lib/vimamsa/macro.rb, line 84
def end_recording()
  if @recording == true
    @recorded_macros[@current_name] = @current_recording
    @last_macro = @current_name
    @current_name = @current_recording = nil
    @recording = false
    message("Stop recording macro [#{@last_macro}]")
  else
    message("Not recording macro")
  end
end
find_macro_gui() click to toggle source
# File lib/vimamsa/macro.rb, line 54
def find_macro_gui()
  # Ripl.start :binding => binding

  l = $macro.named_macros.keys.sort.collect { |x| [x, 0] }
  $macro_search_list = l
  $select_keys = ["h", "l", "f", "d", "s", "a", "g", "z"]

  gui_select_update_window(l, $select_keys.collect { |x| x.upcase },
                          "gui_find_macro_select_callback",
                          "gui_find_macro_update_callback")
end
gui_name_macro() click to toggle source
# File lib/vimamsa/macro.rb, line 48
def gui_name_macro()
  callback = self.method("name_macro")
  # gui_one_input_action("Grep", "Search:", "grep", "grep_cur_buffer")
  gui_one_input_action("Name last macro", "Name:", "Set", callback)
end
is_recording() click to toggle source
# File lib/vimamsa/macro.rb, line 96
def is_recording
  return @recording
end
name_macro(name, id = nil) click to toggle source
# File lib/vimamsa/macro.rb, line 66
def name_macro(name, id = nil)
  puts "NAME MACRO #{name}"
  if id.nil?
    id = @last_macro
  end
  @named_macros[name] = @recorded_macros[id].clone
end
overwrite_current_action(eval_str) click to toggle source

Allow method to specify the macro action instead of recording from keyboard input

# File lib/vimamsa/macro.rb, line 111
def overwrite_current_action(eval_str)
  if @recording
    @current_recording[-1] = eval_str
  end
end
record_action(eval_str) click to toggle source
# File lib/vimamsa/macro.rb, line 100
def record_action(eval_str)
  if @recording
    if eval_str == "repeat_last_action"
      @current_recording << $command_history.last
    else
      @current_recording << eval_str
    end
  end
end
run_last_macro() click to toggle source
# File lib/vimamsa/macro.rb, line 117
def run_last_macro
  run_macro(@last_macro)
end
run_macro(name) click to toggle source
# File lib/vimamsa/macro.rb, line 121
def run_macro(name)
  if $macro.is_recording == true
    message("Can't run a macro that runs a macro (recursion risk)")
    return false
  end
  message("Start running macro [#{name}]")
  if @recorded_macros.has_key?(name)
    @last_macro = name
  end
  acts = @recorded_macros[name]
  if acts.kind_of?(Array) and acts.any?
    set_last_command({ method: $macro.method("run_macro"), params: [name] })
    #
    # Ripl.start :binding => binding
    for a in acts
      ret = exec_action(a)
      puts ret
      if ret == false
        message("Error while running macro")
        break
      end
    end
    # eval_str = m.join(";")
    # debug(eval_str)
    # eval(eval_str)
  end
  buf.set_pos(buf.pos)
end
save() click to toggle source
# File lib/vimamsa/macro.rb, line 43
def save()
  vma.marshal_save("macros", @recorded_macros)
  vma.marshal_save("named_macros", @named_macros)
end
save_macro(name) click to toggle source
# File lib/vimamsa/macro.rb, line 150
def save_macro(name)
  m = @recorded_macros[name]
  return if !(m.kind_of?(Array) and m.any?)
  contents = m.join(";")
  dot_dir = File.expand_path("~/.vimamsa")
  Dir.mkdir(dot_dir) unless File.exist?(dot_dir)
  save_fn = "#{dot_dir}/macro_#{name}.rb"

  Thread.new {
    File.open(save_fn, "w+") do |io|
      #io.set_encoding(self.encoding)

      begin
        io.write(contents)
      rescue Encoding::UndefinedConversionError => ex
        # this might happen when trying to save UTF-8 as US-ASCII
        # so just warn, try to save as UTF-8 instead.
        warn("Saving as UTF-8 because of: #{ex.class}: #{ex}")
        io.rewind

        io.set_encoding(Encoding::UTF_8)
        io.write(contents)
      end
    end
    sleep 3 #TODO:remove
  }
end
start_recording(name) click to toggle source
# File lib/vimamsa/macro.rb, line 74
def start_recording(name)
  @recording = true
  @current_name = name
  @current_recording = []
  message("Start recording macro [#{name}]")

  # Returning false prevents from putting start_recording to start of macro
  return false
end