Class: Utils::Editor

Inherits:
Object
  • 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



3
4
5
6
7
8
# File 'lib/utils/editor.rb', line 3

def initialize
  self.wait           = false
  self.pause_duration = 1
  self.servername     = "G#{ENV['USER'].upcase}"
  yield self if block_given?
end

Instance Attribute Details

#pause_durationObject

Returns the value of attribute pause_duration.



10
11
12
# File 'lib/utils/editor.rb', line 10

def pause_duration
  @pause_duration
end

#servernameObject

Returns the value of attribute servername.



14
15
16
# File 'lib/utils/editor.rb', line 14

def servername
  @servername
end

#waitObject Also known as: wait?

Returns the value of attribute wait.



12
13
14
# File 'lib/utils/editor.rb', line 12

def wait
  @wait
end

Instance Method Details

#activateObject



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

def activate
  edit_remote("stupid_trick#{rand}")
  sleep pause_duration
  edit_remote_send('<ESC>:bw<CR>')
end

#cmd(*parts) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/utils/editor.rb', line 31

def cmd(*parts)
  command = parts.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)
end

#edit(*filenames) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/utils/editor.rb', line 61

def edit(*filenames)
  if filenames.size == 1
    filename = filenames.first
    if m = file_linenumber?(filename)
      edit_file_linenumber(*m.captures)
    else
      edit_file(filename)
    end
  elsif !filenames.empty?
    edit_file(*filenames)
  end
end

#edit_file(*filenames) ⇒ Object



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

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

#edit_file_linenumber(filename, linenumber) ⇒ Object



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

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



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

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

#edit_remote_file(*filenames) ⇒ Object



130
131
132
133
134
135
136
# File 'lib/utils/editor.rb', line 130

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

#edit_remote_send(*args) ⇒ Object



126
127
128
# File 'lib/utils/editor.rb', line 126

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

#edit_remote_wait(*args) ⇒ Object



122
123
124
# File 'lib/utils/editor.rb', line 122

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

#ensure_runningObject



91
92
93
94
# File 'lib/utils/editor.rb', line 91

def ensure_running
  started? ? activate : start
  self
end

#file_linenumber?(filename) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/utils/editor.rb', line 57

def file_linenumber?(filename)
  filename.match(/^\s*([^:]+):(\d+)/)
end

#fullscreen=(enabled) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/utils/editor.rb', line 46

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

#serverlistObject



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

def serverlist
  @serverlist ||= `#{vim} -g --serverlist`.split
end

#startObject



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

def start
  cmd(vim, '-g', '--servername', servername)
end

#started?(name = servername) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#stopObject



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

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

#vimObject



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/utils/editor.rb', line 18

def vim
  @vim ||= case `uname -s`
  when /\Adarwin/i
    if File.directory?('/Applications')
      '/Applications/MacVim.app/Contents/MacOS/Vim'
    else
      'gvim'
    end
  else
    'gvim'
  end
end