Class: Utils::Editor

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Editor

Returns a new instance of Editor.

Yields:

  • (_self)

Yield Parameters:

  • _self (Utils::Editor)

    the object that the method was called on



8
9
10
11
12
13
14
15
16
# File 'lib/utils/editor.rb', line 8

def initialize
  self.wait           = false
  self.pause_duration = 1
  self.servername     = derive_server_name
  config              = Utils::Config::ConfigFile.new
  config.configure_from_paths
  self.config = config.edit
  yield self if block_given?
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



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

def config
  @config
end

#mkdirObject

Returns the value of attribute mkdir.



31
32
33
# File 'lib/utils/editor.rb', line 31

def mkdir
  @mkdir
end

#pause_durationObject

Returns the value of attribute pause_duration.



25
26
27
# File 'lib/utils/editor.rb', line 25

def pause_duration
  @pause_duration
end

#servernameObject

Returns the value of attribute servername.



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

def servername
  @servername
end

#waitObject Also known as: wait?

Returns the value of attribute wait.



27
28
29
# File 'lib/utils/editor.rb', line 27

def wait
  @wait
end

Instance Method Details

#activateObject



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/utils/editor.rb', line 123

def activate
  if Array(config.vim_default_args).include?('-g')
    edit_remote("stupid_trick#{rand}")
    sleep pause_duration
    edit_remote_send('<ESC>:bw<CR>')
  else
    pstree = PSTree.new
    switch_to_index =
      `tmux list-panes -F '\#{pane_pid} \#{pane_index}'`.lines.find { |l|
        pid, index = l.split(' ')
        pid = pid.to_i
        if pstree.find { |ps| ps.pid != $$ && ps.ppid == pid && ps.cmd =~ %r(/edit( |$)) }
          break index.to_i
        end
      }
    switch_to_index and system "tmux select-pane -t #{switch_to_index}"
  end
end

#cmd(*parts) ⇒ Object



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

def cmd(*parts)
  command = parts.compact.inject([]) do |a, p|
    case
    when p == nil, p == []
      a
    when p.respond_to?(:to_ary)
      a.concat p.to_ary
    else
      a << p.to_s
    end
  end
  $DEBUG and warn command * ' '
  system(*command.map(&:to_s))
end

#edit(*filenames) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/utils/editor.rb', line 75

def edit(*filenames)
  if filenames.size == 1 and
    source_location = filenames.first.source_location
  then
    edit_source_location(source_location) # || edit_file(expand_globs(source_location[0, 1]))
  elsif source_locations = filenames.map(&:source_location).compact.full?
    filenames = expand_globs(source_locations.map(&:first))
    edit_file(*filenames)
  end.tap do
    activate
  end
end

#edit_file(*filenames) ⇒ Object



97
98
99
100
# File 'lib/utils/editor.rb', line 97

def edit_file(*filenames)
  make_dirs(*filenames)
  edit_remote_file(*filenames)
end

#edit_file_linenumber(filename, linenumber) ⇒ Object



102
103
104
105
106
107
108
109
# File 'lib/utils/editor.rb', line 102

def edit_file_linenumber(filename, linenumber)
  make_dirs filename
  if wait?
    edit_remote_wait("+#{linenumber}", filename)
  else
    edit_remote("+#{linenumber}", filename)
  end
end

#edit_remote(*args) ⇒ Object



150
151
152
# File 'lib/utils/editor.rb', line 150

def edit_remote(*args)
  cmd(*vim, '--servername', servername, '--remote', *args)
end

#edit_remote_file(*filenames) ⇒ Object



162
163
164
165
166
167
168
# File 'lib/utils/editor.rb', line 162

def edit_remote_file(*filenames)
  if wait?
    edit_remote_wait(*filenames)
  else
    edit_remote(*filenames)
  end
end

#edit_remote_send(*args) ⇒ Object



158
159
160
# File 'lib/utils/editor.rb', line 158

def edit_remote_send(*args)
  cmd(*vim, '--servername', servername, '--remote-send', *args)
end

#edit_remote_wait(*args) ⇒ Object



154
155
156
# File 'lib/utils/editor.rb', line 154

def edit_remote_wait(*args)
  cmd(*vim, '--servername', servername, '--remote-wait', *args)
end

#edit_source_location(source_location) ⇒ Object



111
112
113
# File 'lib/utils/editor.rb', line 111

def edit_source_location(source_location)
  edit_file_linenumber(source_location[0], source_location[1])
end

#expand_globs(filenames) ⇒ Object



71
72
73
# File 'lib/utils/editor.rb', line 71

def expand_globs(filenames)
  filenames.map { |f| Dir[f] }.flatten.uniq.sort.full? || filenames
end

#file_linenumber?(filename) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/utils/editor.rb', line 67

def file_linenumber?(filename)
  filename.match(Utils::Xt::SourceLocationExtension::FILE_LINENUMBER_REGEXP)
end

#fullscreen=(enabled) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/utils/editor.rb', line 56

def fullscreen=(enabled)
  start
  sleep pause_duration
  if enabled
    edit_remote_send '<ESC>:set fullscreen<CR>'
  else
    edit_remote_send '<ESC>:set nofullscreen<CR>'
  end
  activate
end

#serverlistObject



142
143
144
# File 'lib/utils/editor.rb', line 142

def serverlist
  @serverlist ||= `#{vim.map(&:inspect) * ' '} --serverlist`.split
end

#startObject



115
116
117
# File 'lib/utils/editor.rb', line 115

def start
  started? or cmd(*vim, '--servername', servername)
end

#started?(name = servername) ⇒ Boolean

Returns:

  • (Boolean)


146
147
148
# File 'lib/utils/editor.rb', line 146

def started?(name = servername)
  serverlist.member?(name)
end

#stopObject



119
120
121
# File 'lib/utils/editor.rb', line 119

def stop
  started? and edit_remote_send('<ESC>:qa<CR>')
end

#vimObject



37
38
39
# File 'lib/utils/editor.rb', line 37

def vim
  ([ config.vim_path ] + Array(config.vim_default_args))
end