Class: Utils::Editor

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

Defined Under Namespace

Modules: SourceLocationExtension

Constant Summary collapse

FILE_LINENUMBER_REGEXP =
/\A\s*([^:]+):(\d+)/
CLASS_METHOD_REGEXP =
/\A([A-Z][\w:]+)([#.])(\S+)/

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



50
51
52
53
54
55
56
57
58
# File 'lib/utils/editor.rb', line 50

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.



75
76
77
# File 'lib/utils/editor.rb', line 75

def config
  @config
end

#mkdirObject

Returns the value of attribute mkdir.



73
74
75
# File 'lib/utils/editor.rb', line 73

def mkdir
  @mkdir
end

#pause_durationObject

Returns the value of attribute pause_duration.



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

def pause_duration
  @pause_duration
end

#servernameObject

Returns the value of attribute servername.



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

def servername
  @servername
end

#waitObject Also known as: wait?

Returns the value of attribute wait.



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

def wait
  @wait
end

Instance Method Details

#activateObject



168
169
170
171
172
173
174
175
176
# File 'lib/utils/editor.rb', line 168

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
    # TODO use tmux to switch to editor pane?
  end
end

#cmd(*parts) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/utils/editor.rb', line 83

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



117
118
119
120
121
122
123
124
125
126
# File 'lib/utils/editor.rb', line 117

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
end

#edit_file(*filenames) ⇒ Object



137
138
139
140
# File 'lib/utils/editor.rb', line 137

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

#edit_file_linenumber(filename, linenumber) ⇒ Object



142
143
144
145
146
147
148
149
# File 'lib/utils/editor.rb', line 142

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



186
187
188
# File 'lib/utils/editor.rb', line 186

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

#edit_remote_file(*filenames) ⇒ Object



198
199
200
201
202
203
204
# File 'lib/utils/editor.rb', line 198

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

#edit_remote_send(*args) ⇒ Object



194
195
196
# File 'lib/utils/editor.rb', line 194

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

#edit_remote_wait(*args) ⇒ Object



190
191
192
# File 'lib/utils/editor.rb', line 190

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

#edit_source_location(source_location) ⇒ Object



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

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

#ensure_runningObject



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

def ensure_running
  started? ? activate : start
  self
end

#expand_globs(filenames) ⇒ Object



113
114
115
# File 'lib/utils/editor.rb', line 113

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

#file_linenumber?(filename) ⇒ Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/utils/editor.rb', line 109

def file_linenumber?(filename)
  filename.match(FILE_LINENUMBER_REGEXP)
end

#fullscreen=(enabled) ⇒ Object



98
99
100
101
102
103
104
105
106
107
# File 'lib/utils/editor.rb', line 98

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



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

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

#startObject



160
161
162
# File 'lib/utils/editor.rb', line 160

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

#started?(name = servername) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#stopObject



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

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

#vimObject



79
80
81
# File 'lib/utils/editor.rb', line 79

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