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::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


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

def config
  @config
end

#mkdirObject

Returns the value of attribute mkdir


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

def mkdir
  @mkdir
end

#pause_durationObject

Returns the value of attribute pause_duration


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

def pause_duration
  @pause_duration
end

#servernameObject

Returns the value of attribute servername


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

def servername
  @servername
end

#waitObject Also known as: wait?

Returns the value of attribute wait


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

def wait
  @wait
end

Instance Method Details

#activateObject


144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/utils/editor.rb', line 144

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


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

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


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

def edit(*filenames)
  source_location = nil
  if filenames.size == 1 and
    source_location = filenames.first.source_location
  then
    if source_location.respond_to?(:filename) and source_location.respond_to?(:linenumber)
      edit_source_location(source_location)
    else
      edit_file_linenumber(*source_location)
    end
  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


100
101
102
103
# File 'lib/utils/editor.rb', line 100

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

#edit_file_linenumber(filename, linenumber, rangeend = nil) ⇒ Object


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/utils/editor.rb', line 105

def edit_file_linenumber(filename, linenumber, rangeend = nil)
  make_dirs filename
  if rangeend
    Thread.new do
      while !started?
        sleep 1
      end
      edit_remote_send("<ESC>:normal #{linenumber}GV#{rangeend}G<CR>")
    end
  end
  if wait?
    edit_remote_wait("+#{linenumber}", filename)
  else
    edit_remote("+#{linenumber}", filename)
  end
end

#edit_remote(*args) ⇒ Object


171
172
173
174
# File 'lib/utils/editor.rb', line 171

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

#edit_remote_file(*filenames) ⇒ Object


186
187
188
189
190
191
192
# File 'lib/utils/editor.rb', line 186

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

#edit_remote_send(*args) ⇒ Object


181
182
183
184
# File 'lib/utils/editor.rb', line 181

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

#edit_remote_wait(*args) ⇒ Object


176
177
178
179
# File 'lib/utils/editor.rb', line 176

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

#edit_source_location(source_location) ⇒ Object


122
123
124
125
126
127
128
# File 'lib/utils/editor.rb', line 122

def edit_source_location(source_location)
  edit_file_linenumber(
    source_location.filename,
    source_location.linenumber,
    source_location.rangeend
  )
end

#expand_globs(filenames) ⇒ Object


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

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

#file_linenumber?(filename) ⇒ Boolean

Returns:

  • (Boolean)

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

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

#fullscreen=(enabled) ⇒ Object


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

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


163
164
165
# File 'lib/utils/editor.rb', line 163

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

#startObject


135
136
137
138
# File 'lib/utils/editor.rb', line 135

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

#started?(name = servername) ⇒ Boolean

Returns:

  • (Boolean)

167
168
169
# File 'lib/utils/editor.rb', line 167

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

#stopObject


140
141
142
# File 'lib/utils/editor.rb', line 140

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

#vimObject


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

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