Class: Macro

Inherits:
Object
  • Object
show all
Defined in:
lib/vimamsa/macro.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMacro

Returns a new instance of Macro.



30
31
32
33
34
35
36
37
38
39
40
41
# 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

Instance Attribute Details

#last_macroObject

attr_reader :recorded_macros, :recording, :named_macros



28
29
30
# File 'lib/vimamsa/macro.rb', line 28

def last_macro
  @last_macro
end

#named_macrosObject

attr_reader :recorded_macros, :recording, :named_macros



28
29
30
# File 'lib/vimamsa/macro.rb', line 28

def named_macros
  @named_macros
end

#recorded_macrosObject

attr_reader :recorded_macros, :recording, :named_macros



28
29
30
# File 'lib/vimamsa/macro.rb', line 28

def recorded_macros
  @recorded_macros
end

#recordingObject

attr_reader :recorded_macros, :recording, :named_macros



28
29
30
# File 'lib/vimamsa/macro.rb', line 28

def recording
  @recording
end

Instance Method Details

#end_recordingObject



84
85
86
87
88
89
90
91
92
93
94
# 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_guiObject



54
55
56
57
58
59
60
61
62
63
64
# 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_macroObject



48
49
50
51
52
# 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_recordingObject



96
97
98
# File 'lib/vimamsa/macro.rb', line 96

def is_recording
  return @recording
end

#name_macro(name, id = nil) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/vimamsa/macro.rb', line 66

def name_macro(name, id = nil)
  debug "NAME MACRO #{name}"
  if id.nil?
    id = @last_macro
  end
  @named_macros[name] = @recorded_macros[id].clone
end

#overwrite_current_action(eval_str) ⇒ Object

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



111
112
113
114
115
# 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) ⇒ Object



100
101
102
103
104
105
106
107
108
# 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_macroObject



117
118
119
# File 'lib/vimamsa/macro.rb', line 117

def run_last_macro
  run_macro(@last_macro)
end

#run_macro(name) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# 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)
      debug 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

#saveObject



43
44
45
46
# 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) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# 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) ⇒ Object



74
75
76
77
78
79
80
81
82
# 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