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.
48
49
50
51
52
53
|
# File 'lib/utils/editor.rb', line 48
def initialize
self.wait = false
self.pause_duration = 1
self.servername = ENV['VIM_SERVER'] || "G#{ENV['USER'].upcase}"
yield self if block_given?
end
|
Instance Attribute Details
#pause_duration ⇒ Object
Returns the value of attribute pause_duration.
55
56
57
|
# File 'lib/utils/editor.rb', line 55
def pause_duration
@pause_duration
end
|
#servername ⇒ Object
Returns the value of attribute servername.
59
60
61
|
# File 'lib/utils/editor.rb', line 59
def servername
@servername
end
|
#wait ⇒ Object
Also known as:
wait?
Returns the value of attribute wait.
57
58
59
|
# File 'lib/utils/editor.rb', line 57
def wait
@wait
end
|
Instance Method Details
168
169
170
171
172
|
# File 'lib/utils/editor.rb', line 168
def activate
edit_remote("stupid_trick#{rand}")
sleep pause_duration
edit_remote_send('<ESC>:bw<CR>')
end
|
#cmd(*parts) ⇒ Object
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
# File 'lib/utils/editor.rb', line 80
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
114
115
116
117
118
119
120
121
122
123
124
|
# File 'lib/utils/editor.rb', line 114
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
126
127
128
129
130
131
132
|
# File 'lib/utils/editor.rb', line 126
def edit_file(*filenames)
if gui
edit_remote_file(*filenames)
else
cmd(vim, *filenames)
end
end
|
#edit_file_linenumber(filename, linenumber) ⇒ Object
134
135
136
137
138
139
140
141
142
143
144
145
|
# File 'lib/utils/editor.rb', line 134
def edit_file_linenumber(filename, linenumber)
if wait?
edit_remote(filename)
sleep pause_duration
edit_remote_send("<ESC>:#{linenumber}<CR>")
edit_remote_wait(filename)
else
edit_remote(filename)
sleep pause_duration
edit_remote_send("<ESC>:#{linenumber}<CR>")
end
end
|
#edit_remote(*args) ⇒ Object
182
183
184
|
# File 'lib/utils/editor.rb', line 182
def edit_remote(*args)
gui and cmd(vim, gui, '--servername', servername, '--remote', *args)
end
|
#edit_remote_file(*filenames) ⇒ Object
194
195
196
197
198
199
200
|
# File 'lib/utils/editor.rb', line 194
def edit_remote_file(*filenames)
if gui && wait?
edit_remote_wait(*filenames)
else
edit_remote(*filenames)
end
end
|
#edit_remote_send(*args) ⇒ Object
190
191
192
|
# File 'lib/utils/editor.rb', line 190
def edit_remote_send(*args)
gui and cmd(vim, gui, '--servername', servername, '--remote-send', *args)
end
|
#edit_remote_wait(*args) ⇒ Object
186
187
188
|
# File 'lib/utils/editor.rb', line 186
def edit_remote_wait(*args)
gui and cmd(vim, gui, '--servername', servername, '--remote-wait', *args)
end
|
#edit_source_location(source_location) ⇒ Object
147
148
149
|
# File 'lib/utils/editor.rb', line 147
def edit_source_location(source_location)
edit_file_linenumber(source_location[0], source_location[1])
end
|
#ensure_running ⇒ Object
151
152
153
154
|
# File 'lib/utils/editor.rb', line 151
def ensure_running
started? ? activate : start
self
end
|
#expand_globs(filenames) ⇒ Object
110
111
112
|
# File 'lib/utils/editor.rb', line 110
def expand_globs(filenames)
filenames.map { |f| Dir[f] }.flatten.uniq.sort.full? || filenames
end
|
#file_linenumber?(filename) ⇒ Boolean
106
107
108
|
# File 'lib/utils/editor.rb', line 106
def file_linenumber?(filename)
filename.match(FILE_LINENUMBER_REGEXP)
end
|
#fullscreen=(enabled) ⇒ Object
95
96
97
98
99
100
101
102
103
104
|
# File 'lib/utils/editor.rb', line 95
def fullscreen=(enabled)
started? or start
sleep pause_duration
if enabled
edit_remote_send '<ESC>:set fullscreen<CR>'
else
edit_remote_send '<ESC>:set nofullscreen<CR>'
end
activate
end
|
156
157
158
|
# File 'lib/utils/editor.rb', line 156
def gui
ENV['TERM'] =~ /xterm/ ? '-g' : nil
end
|
#serverlist ⇒ Object
174
175
176
|
# File 'lib/utils/editor.rb', line 174
def serverlist
@serverlist ||= `#{vim} #{gui} --serverlist`.split
end
|
160
161
162
|
# File 'lib/utils/editor.rb', line 160
def start
cmd(vim, gui, '--servername', servername)
end
|
#started?(name = servername) ⇒ Boolean
178
179
180
|
# File 'lib/utils/editor.rb', line 178
def started?(name = servername)
serverlist.member?(name)
end
|
164
165
166
|
# File 'lib/utils/editor.rb', line 164
def stop
started? and edit_remote_send('<ESC>:qa<CR>')
end
|
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/utils/editor.rb', line 63
def vim
vim_in_path = [`which gvim`, `which vim`].map(&:chomp).find(&:full?)
@vim ||=
case `uname -s`
when /\Adarwin/i
if File.directory?(path = File.expand_path('~/Applications/MacVim.app')) or
File.directory?(path = File.expand_path('/Applications/MacVim.app'))
then
File.join(path, 'Contents/MacOS/Vim')
else
vim_in_path
end
else
vim_in_path
end
end
|