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



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/vimamsa/editor.rb', line 9

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 = {}
  @errors
  @register = Hash.new("")
  @cur_register = "a"
  @clipboard = Clipboard.new
end

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.



5
6
7
# File 'lib/vimamsa/editor.rb', line 5

def actions
  @actions
end

#clipboardObject

Returns the value of attribute clipboard.



6
7
8
# File 'lib/vimamsa/editor.rb', line 6

def clipboard
  @clipboard
end

#convertersObject

Returns the value of attribute converters.



6
7
8
# File 'lib/vimamsa/editor.rb', line 6

def converters
  @converters
end

#cur_registerObject

Returns the value of attribute cur_register.



6
7
8
# File 'lib/vimamsa/editor.rb', line 6

def cur_register
  @cur_register
end

#fhObject

Returns the value of attribute fh.



6
7
8
# File 'lib/vimamsa/editor.rb', line 6

def fh
  @fh
end

#file_content_search_pathsObject (readonly)

Returns the value of attribute file_content_search_paths.



5
6
7
# File 'lib/vimamsa/editor.rb', line 5

def file_content_search_paths
  @file_content_search_paths
end

#file_name_search_pathsObject (readonly)

Returns the value of attribute file_name_search_paths.



5
6
7
# File 'lib/vimamsa/editor.rb', line 5

def file_name_search_paths
  @file_name_search_paths
end

#guiObject (readonly)

Returns the value of attribute gui.



5
6
7
# File 'lib/vimamsa/editor.rb', line 5

def gui
  @gui
end

#hookObject (readonly)

Returns the value of attribute hook.



5
6
7
# File 'lib/vimamsa/editor.rb', line 5

def hook
  @hook
end

#kbdObject

Returns the value of attribute kbd.



6
7
8
# File 'lib/vimamsa/editor.rb', line 6

def kbd
  @kbd
end

#langsrvObject

Returns the value of attribute langsrv.



6
7
8
# File 'lib/vimamsa/editor.rb', line 6

def langsrv
  @langsrv
end

#macroObject (readonly)

Returns the value of attribute macro.



5
6
7
# File 'lib/vimamsa/editor.rb', line 5

def macro
  @macro
end

#paint_stackObject

Returns the value of attribute paint_stack.



6
7
8
# File 'lib/vimamsa/editor.rb', line 6

def paint_stack
  @paint_stack
end

#registerObject

Returns the value of attribute register.



6
7
8
# File 'lib/vimamsa/editor.rb', line 6

def register
  @register
end

Instance Method Details

#add_content_search_path(pathstr) ⇒ Object



303
304
305
306
307
308
# File 'lib/vimamsa/editor.rb', line 303

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



316
317
318
# File 'lib/vimamsa/editor.rb', line 316

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

#bufObject



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

def buf()
  return $buffer
end

#buf=(aa) ⇒ Object



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

def buf=(aa)
  $buffer = aa
end

#buffersObject



213
214
215
# File 'lib/vimamsa/editor.rb', line 213

def buffers()
  return $buffers
end

#can_open_extension?(filepath) ⇒ Boolean

Returns:

  • (Boolean)


338
339
340
341
342
343
344
# File 'lib/vimamsa/editor.rb', line 338

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

#check_session_restoreObject



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/vimamsa/editor.rb', line 274

def check_session_restore
  session_path = get_dot_path("session.txt")
  return unless File.exist?(session_path)

  fnames = File.read(session_path).lines.map(&:strip).reject(&:empty?)
  fnames.select! { |f| File.exist?(f) }
  fnames.reject! { |f| vma.buffers.get_buffer_by_filename(f) }
  return if fnames.empty?

  n = fnames.size
  label = n == 1 ? "1 file" : "#{n} files"
  params = {
    "title" => "Restore previous session? (#{label})",
    "inputs" => {
      "yes_btn" => { :label => "Restore", :type => :button, :default_focus => true },
      "no_btn"  => { :label => "No", :type => :button },
    },
    :callback => proc { |x|
      if x["yes_btn"] == "submit"
        initial = vma.buffers.list.find { |b| b.fname.nil? }
        fnames.each { |f| load_buffer(f) }
        initial&.close
        message("Session restored: #{label}")
      end
    },
  }
  PopupFormGenerator.new(params).run
end

#error(message) ⇒ Object



346
347
348
349
# File 'lib/vimamsa/editor.rb', line 346

def error(message)
  debug "ERORR #{caller[0]} #{str}", 2
  @errors << [message, caller]
end

#get_content_search_pathsObject

Used only by ack module at the moment



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/vimamsa/editor.rb', line 321

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
  
  # Ensure that paths are in correct format
  r = r.map{|x|File.expand_path(x)}

  return r
end

#load_var_from_file(varname) ⇒ Object



238
239
240
241
242
243
244
245
246
247
248
# File 'lib/vimamsa/editor.rb', line 238

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



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

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



217
218
219
# File 'lib/vimamsa/editor.rb', line 217

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

#open_file_listener(added) ⇒ Object



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

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

#paste_register(char) ⇒ Object



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

def paste_register(char)
  $c = @cur_register #TODO:??
  message("Paste: #{$c}")
end

#plugObject



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

def plug()
  return @_plugins
end

#reg_conv(converter, converter_id) ⇒ Object

Register converter



311
312
313
314
# File 'lib/vimamsa/editor.rb', line 311

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



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

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

#save_sessionObject



264
265
266
267
268
269
270
271
272
# File 'lib/vimamsa/editor.rb', line 264

def save_session
  fnames = vma.buffers.list
    .map { |b| b.fname }
    .compact
    .select { |f| File.exist?(f) }
  IO.write(get_dot_path("session.txt"), fnames.join("\n") + "\n")
rescue => ex
  debug "save_session failed: #{ex}"
end

#save_stateObject



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

def save_state
end

#save_var_to_file(varname, vardata) ⇒ Object



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

def save_var_to_file(varname, vardata)
  fn = get_dot_path(varname)
  #TODO: check that save path is safe
  f = File.open(fn, "w")
  File.binwrite(f, vardata)
  f.close
end

#set_register(char) ⇒ Object



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

def set_register(char)
  @cur_register = char
  message("Set register #{char}")
end

#shutdownObject



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

def shutdown()
  save_session
  @hook.call(:shutdown)
  save_state
  @gui.quit
end

#startObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
190
191
192
193
194
195
196
197
# File 'lib/vimamsa/editor.rb', line 56

def start
  @gui = $vmag #TODO

  $hook = Hook.new
  @hook = $hook
  require "vimamsa/actions"
  @actions = ActionList.new
  require "vimamsa/key_actions"

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

  # build_key_bindings_tree
  @kbd = KeyBindingTree.new()
  $kbd = @kbd #TODO: remove global
  require "vimamsa/key_bindings_vimlike"

  $buffers = BufferList.new
  $minibuffer = Buffer.new(">", "")
  @langsrv = {}

  require "vimamsa/text_transforms"

  debug "ARGV: " + ARGV.inspect

  sleep(0.03)

  BufferManager.init

  @gui.init_menu

  mkdir_if_not_exists(get_dot_path(""))
  mkdir_if_not_exists(get_dot_path("backup"))
  mkdir_if_not_exists(get_dot_path("testfoobar")) #TODO
  listen_dir = get_dot_path("listen")
  mkdir_if_not_exists(listen_dir)
  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 = get_dot_path("custom.rb")
  if !File.exist?(custom_fn)
    example_custom = IO.read(ppath("custom_example.rb"))
    IO.write(custom_fn, example_custom)
  end

  settings_path = get_dot_path("settings.rb")
  if File.exist?(settings_path)
    eval(IO.read(settings_path))
  end

  custom_script = read_file("", custom_fn)
  eval(custom_script) if custom_script

  Grep.init
  FileManager.init
  Autocomplete.init

  if cnf.audio.enabled?
    require "vimamsa/audio"
  end

  if cnf.lsp.enabled?
    require "vimamsa/langservp"
    @langsrv["ruby"] = LangSrv.new("ruby")
    @langsrv["cpp"] = LangSrv.new("cpp")
    #TODO: if language enabled in config?
  end

  # Load each module that is enabled in settings.
  # Each module lives in modules/<name>/<name>.rb and may optionally define
  # <name>_init to register actions, key bindings, etc.
  Dir.glob(ppath("modules/*/")).sort.each do |mod_dir|
    mod_name = File.basename(mod_dir)
    if cnf.modules.public_send(mod_name).enabled?
      main_file = File.join(mod_dir, "#{mod_name}.rb")
      if File.exist?(main_file)
        load main_file
        # Call <name>_init if the module defines it.
        init_fn = "#{mod_name}_init"
        send(init_fn) if respond_to?(init_fn, true)
      end
    end
  end

  fname = nil
  if cnf.startup_file?
    fname_ = File.expand_path(cnf.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

  argv_has_files = ARGV.any? { |a| File.file?(File.expand_path(a)) }

  if fname
    open_new_file(fname)
  else
    create_new_buffer(file_contents = "\n")
  end

  #Load plugins
  require "vimamsa/file_history.rb"
  @fh = FileHistory.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] }

  check_session_restore unless argv_has_files

  @hook.call(:after_init)

  if ARGV.include?("--test")
    test_files = ARGV.select { |a| a.end_with?(".rb") && File.file?(a) }
    run_as_idle proc {
      test_files.each { |f| load f }
      success = run_vma_tests
      # Defer shutdown so GTK can drain all pending idle callbacks from test
      # teardown before widget destruction begins (avoids heap corruption).
      GLib::Timeout.add(300) { shutdown(); false }
    }
  end
end