Class: Utils::Editor
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.
51
52
53
54
55
56
57
58
59
|
# File 'lib/utils/editor.rb', line 51
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
Returns the value of attribute config.
76
77
78
|
# File 'lib/utils/editor.rb', line 76
def config
@config
end
|
Returns the value of attribute mkdir.
74
75
76
|
# File 'lib/utils/editor.rb', line 74
def mkdir
@mkdir
end
|
#pause_duration ⇒ Object
Returns the value of attribute pause_duration.
68
69
70
|
# File 'lib/utils/editor.rb', line 68
def pause_duration
@pause_duration
end
|
#servername ⇒ Object
Returns the value of attribute servername.
72
73
74
|
# File 'lib/utils/editor.rb', line 72
def servername
@servername
end
|
#wait ⇒ Object
Also known as:
wait?
Returns the value of attribute wait.
70
71
72
|
# File 'lib/utils/editor.rb', line 70
def wait
@wait
end
|
Instance Method Details
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
# File 'lib/utils/editor.rb', line 166
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/utils/editor.rb', line 84
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
118
119
120
121
122
123
124
125
126
127
128
129
|
# File 'lib/utils/editor.rb', line 118
def edit(*filenames)
if filenames.size == 1 and
source_location = filenames.first.source_location
then
edit_source_location(source_location) 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
140
141
142
143
|
# File 'lib/utils/editor.rb', line 140
def edit_file(*filenames)
make_dirs(*filenames)
edit_remote_file(*filenames)
end
|
#edit_file_linenumber(filename, linenumber) ⇒ Object
145
146
147
148
149
150
151
152
|
# File 'lib/utils/editor.rb', line 145
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
193
194
195
|
# File 'lib/utils/editor.rb', line 193
def edit_remote(*args)
cmd(*vim, '--servername', servername, '--remote', *args)
end
|
#edit_remote_file(*filenames) ⇒ Object
205
206
207
208
209
210
211
|
# File 'lib/utils/editor.rb', line 205
def edit_remote_file(*filenames)
if wait?
edit_remote_wait(*filenames)
else
edit_remote(*filenames)
end
end
|
#edit_remote_send(*args) ⇒ Object
201
202
203
|
# File 'lib/utils/editor.rb', line 201
def edit_remote_send(*args)
cmd(*vim, '--servername', servername, '--remote-send', *args)
end
|
#edit_remote_wait(*args) ⇒ Object
197
198
199
|
# File 'lib/utils/editor.rb', line 197
def edit_remote_wait(*args)
cmd(*vim, '--servername', servername, '--remote-wait', *args)
end
|
#edit_source_location(source_location) ⇒ Object
154
155
156
|
# File 'lib/utils/editor.rb', line 154
def edit_source_location(source_location)
edit_file_linenumber(source_location[0], source_location[1])
end
|
#expand_globs(filenames) ⇒ Object
114
115
116
|
# File 'lib/utils/editor.rb', line 114
def expand_globs(filenames)
filenames.map { |f| Dir[f] }.flatten.uniq.sort.full? || filenames
end
|
#file_linenumber?(filename) ⇒ Boolean
110
111
112
|
# File 'lib/utils/editor.rb', line 110
def file_linenumber?(filename)
filename.match(FILE_LINENUMBER_REGEXP)
end
|
#fullscreen=(enabled) ⇒ Object
99
100
101
102
103
104
105
106
107
108
|
# File 'lib/utils/editor.rb', line 99
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
|
#serverlist ⇒ Object
185
186
187
|
# File 'lib/utils/editor.rb', line 185
def serverlist
@serverlist ||= `#{vim.map(&:inspect) * ' '} --serverlist`.split
end
|
158
159
160
|
# File 'lib/utils/editor.rb', line 158
def start
started? or cmd(*vim, '--servername', servername)
end
|
#started?(name = servername) ⇒ Boolean
189
190
191
|
# File 'lib/utils/editor.rb', line 189
def started?(name = servername)
serverlist.member?(name)
end
|
162
163
164
|
# File 'lib/utils/editor.rb', line 162
def stop
started? and edit_remote_send('<ESC>:qa<CR>')
end
|
80
81
82
|
# File 'lib/utils/editor.rb', line 80
def vim
([ config.vim_path ] + Array(config.vim_default_args))
end
|