Class: Editor

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEditor

attr_writer :call_func, :update_highlight



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/vimamsa/editor.rb', line 39

def initialize()
  # Thread.new{10000.times{|x|sleep(3);10000.times{|y|y+2};debug "FOOTHREAD #{x}"}}

  # Search for content inside files (e.g. using ack/grep) in:
  @file_content_search_paths = []

  # Search for files based on filenames in:
  @file_name_search_paths = []

  #Regexp gsubs or other small modifiers of text
  @converters = {}
  @paint_stack = []
  @_plugins = {}
end

Instance Attribute Details

#convertersObject

Returns the value of attribute converters.



36
37
38
# File 'lib/vimamsa/editor.rb', line 36

def converters
  @converters
end

#fhObject

Returns the value of attribute fh.



36
37
38
# File 'lib/vimamsa/editor.rb', line 36

def fh
  @fh
end

#file_content_search_pathsObject (readonly)

Returns the value of attribute file_content_search_paths.



35
36
37
# File 'lib/vimamsa/editor.rb', line 35

def file_content_search_paths
  @file_content_search_paths
end

#file_name_search_pathsObject (readonly)

Returns the value of attribute file_name_search_paths.



35
36
37
# File 'lib/vimamsa/editor.rb', line 35

def file_name_search_paths
  @file_name_search_paths
end

#guiObject (readonly)

Returns the value of attribute gui.



35
36
37
# File 'lib/vimamsa/editor.rb', line 35

def gui
  @gui
end

#kbdObject

Returns the value of attribute kbd.



36
37
38
# File 'lib/vimamsa/editor.rb', line 36

def kbd
  @kbd
end

#paint_stackObject

Returns the value of attribute paint_stack.



36
37
38
# File 'lib/vimamsa/editor.rb', line 36

def paint_stack
  @paint_stack
end

Instance Method Details

#add_content_search_path(pathstr) ⇒ Object



253
254
255
256
257
258
# File 'lib/vimamsa/editor.rb', line 253

def add_content_search_path(pathstr)
  p = File.expand_path(pathstr)
  if !@file_content_search_paths.include?(p)
    @file_content_search_paths << p
  end
end

#apply_conv(converter_id, txt) ⇒ Object



266
267
268
# File 'lib/vimamsa/editor.rb', line 266

def apply_conv(converter_id, txt)
  @converters[converter_id].apply(txt)
end

#bufObject



197
198
199
# File 'lib/vimamsa/editor.rb', line 197

def buf()
  return $buffer
end

#buf=(aa) ⇒ Object



200
201
202
# File 'lib/vimamsa/editor.rb', line 200

def buf=(aa)
  $buffer=aa
end

#buffersObject



205
206
207
# File 'lib/vimamsa/editor.rb', line 205

def buffers()
  return $buffers
end

#can_open_extension?(filepath) ⇒ Boolean

Returns:

  • (Boolean)


285
286
287
288
289
290
291
# File 'lib/vimamsa/editor.rb', line 285

def can_open_extension?(filepath)
  exts = $cnf[:extensions_to_open]
  extname = Pathname.new(filepath).extname.downcase
  can_open = exts.include?(extname)
  debug "CAN OPEN?: #{can_open}"
  return can_open
end

#get_content_search_pathsObject

Used only by ack module at the moment



271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/vimamsa/editor.rb', line 271

def get_content_search_paths()
  r = @file_content_search_paths.clone
  p = find_project_dir_of_cur_buffer()
  if p.nil?
    p = vma.buffers.last_dir 
  end

  if p and !@file_content_search_paths.include?(p)
    r.insert(0, p)
  end

  return r
end

#load_var_from_file(varname) ⇒ Object



229
230
231
232
233
234
235
236
237
238
239
# File 'lib/vimamsa/editor.rb', line 229

def load_var_from_file(varname)
  fn = get_dot_path(varname)
  if File.exist?(fn)
    vardata = IO.binread(fn)
    if vardata
      debug("Successfully red #{varname} from file #{fn}")
      return vardata
    end
  end
  return nil
end

#marshal_load(varname, default_data = nil) ⇒ Object



213
214
215
216
217
218
219
220
# File 'lib/vimamsa/editor.rb', line 213

def marshal_load(varname, default_data = nil)
  mdata = load_var_from_file(varname)
  if mdata
    return Marshal.load(mdata)
  else
    return default_data
  end
end

#marshal_save(varname, vardata) ⇒ Object



209
210
211
# File 'lib/vimamsa/editor.rb', line 209

def marshal_save(varname, vardata)
  save_var_to_file(varname, Marshal.dump(vardata))
end

#open_file_listener(added) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/vimamsa/editor.rb', line 54

def open_file_listener(added)
  if !added.empty?
    for fp in added
      sleep 0.1
      x = IO.read(fp)
      File.delete(fp)
      for f in x.lines
        f.gsub!("\n", "")
        if File.exist?(f)
          if file_is_text_file(f)
            jump_to_file(f)
          end
        end
      end
    end
  end
end

#plugObject



241
242
243
# File 'lib/vimamsa/editor.rb', line 241

def plug()
  return @_plugins
end

#reg_conv(converter, converter_id) ⇒ Object

Register converter



261
262
263
264
# File 'lib/vimamsa/editor.rb', line 261

def reg_conv(converter, converter_id)
  @converters[converter_id] = converter
  reg_act(converter_id, proc { vma.buf.convert_selected_text(converter_id) }, "Converter #{converter_id}", { :scope => [:selection] })
end

#register_plugin(name, obj) ⇒ Object



191
192
193
194
195
# File 'lib/vimamsa/editor.rb', line 191

def register_plugin(name, obj)
  @_plugins[name] = obj
  # To access via e.g. vma.FileFinder
  self.define_singleton_method(name) { obj }
end

#save_stateObject



250
251
# File 'lib/vimamsa/editor.rb', line 250

def save_state
end

#save_var_to_file(varname, vardata) ⇒ Object



222
223
224
225
226
227
# File 'lib/vimamsa/editor.rb', line 222

def save_var_to_file(varname, vardata)
  fn = get_dot_path(varname)
  f = File.open(fn, "w")
  File.binwrite(f, vardata)
  f.close
end

#shutdownObject



245
246
247
248
# File 'lib/vimamsa/editor.rb', line 245

def shutdown()
  $hook.call(:shutdown)
  save_state
end

#startObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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
149
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
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/vimamsa/editor.rb', line 72

def start
  @gui = $vmag #TODO

  $hook = Hook.new
  register_plugin(:Hook, $hook)
  $macro = Macro.new
  register_plugin(:Macro, $macro)
  $search = Search.new
  register_plugin(:Search, $search)

  $buffers = BufferList.new
  $minibuffer = Buffer.new(">", "")

  require "vimamsa/text_transforms"

  debug "ARGV: " + ARGV.inspect
  # build_key_bindings_tree
  @kbd = KeyBindingTree.new()
  $kbd = @kbd
  @kbd.add_mode("C", :command)
  @kbd.add_mode("I", :insert)
  @kbd.add_mode("V", :visual)
  @kbd.add_mode("M", :minibuffer)
  @kbd.add_mode("R", :readchar)
  @kbd.add_mode("B", :browse)
  @kbd.set_default_mode(:command)
  @kbd.set_mode(:command)
  @kbd.show_state_trail
  require "vimamsa/key_bindings_vimlike"
  sleep(0.03)

  FileManager.init
  BufferManager.init

  mkdir_if_not_exists("~/.vimamsa")
  mkdir_if_not_exists("~/.vimamsa/backup")
  mkdir_if_not_exists("~/.vimamsa/listen")
  listen_dir = File.expand_path "~/.vimamsa/listen"
  listener = Listen.to(listen_dir) do |modified, added, removed|
    debug([modified: modified, added: added, removed: removed])
    open_file_listener(added)
  end
  listener.start

  custom_fn = File.expand_path("~/.vimamsa/custom.rb")
  if !File.exist?(custom_fn)
    example_custom = IO.read(ppath("custom_example.rb"))
    IO.write(custom_fn, example_custom)
  end

  mkdir_if_not_exists("~/.vimamsa/custom.rb")

  $cnf[:theme] = "Twilight_edit"
  $cnf[:syntax_highlight] = true
  settings_path = get_dot_path("settings.rb")
  if File.exist?(settings_path)
    $cnf = eval(IO.read(settings_path))
  end

  # set_gui_style(1)

  # Limit file search to these extensions:
  $find_extensions = [".txt", ".h", ".c", ".cpp", ".hpp", ".rb"]

  dotfile = read_file("", "~/.vimamsarc")
  eval(dotfile) if dotfile
  
  custom_script = read_file("", custom_fn)
  eval(custom_script) if custom_script

  # build_options

  fname = nil
  if conf(:startup_file)
    fname_ = File.expand_path(conf(:startup_file))
    if File.exist?(fname_)
      fname = fname_
    end
  else
    fname = ppath("demo.txt")
  end
  fname = ARGV[0] if ARGV.size >= 1 and File.file?(File.expand_path(ARGV[0]))
  # vma.add_content_search_path(Dir.pwd)
  for fn in ARGV
    fn = File.expand_path(fn)
    if File.directory?(fn)
      vma.add_content_search_path(fn)
      $search_dirs << fn
    end
  end

  if fname
    buffer = Buffer.new(read_file("", fname), fname)
  else
    buffer = Buffer.new(" \n")
  end
  vma.buffers << buffer

  # load_theme($cnf[:theme])

  # render_buffer(vma.buf, 1) #TODO

  # gui_select_buffer_init #TODO
  # gui_file_finder_init #TODO

  #Load plugins
  require "vimamsa/file_history.rb"
  @fh = FileHistory.new
  # @_plugins[:FileFinder] = FileFinder.new
  @_plugins[:FileHistory] = @fh

  register_plugin(:FileHistory, @fh)
  register_plugin(:FileFinder, FileFinder.new)
  # To access via vma.FileFinder
  # self.define_singleton_method(:FileFinder) { @_plugins[:FileFinder] }

  $hook.call(:after_init)
end