1
1
module Vimrunner
2
-
3
- # A Client is simply a proxy to a Vim server. It's initialized with a Server
4
- # instance and sends commands, keys and signals to it.
5
2
class Client
6
- attr_reader :server , :driver
3
+ attr_reader :server
7
4
8
5
def initialize ( server )
9
6
@server = server
10
- @driver = server . driver
11
7
end
12
8
13
- # Adds a plugin to Vim's runtime. Initially, Vim is started without
14
- # sourcing any plugins to ensure a clean state. This method can be used to
15
- # populate the instance's environment.
9
+ # Public: Adds a plugin to Vim's runtime. Initially, Vim is started
10
+ # without sourcing any plugins to ensure a clean state. This method can be
11
+ # used to populate the instance's environment.
16
12
#
17
13
# dir - The base directory of the plugin, the one that contains
18
14
# its autoload, plugin, ftplugin, etc. directories.
19
- # entry_script - The Vim script that's runtime'd to initialize the plugin.
20
- # Optional .
15
+ # entry_script - The Vim script that's runtime'd to initialize the plugin
16
+ # (optional) .
21
17
#
22
18
# Example:
23
19
#
24
20
# vim.add_plugin 'rails', 'plugin/rails.vim'
25
21
#
22
+ # Returns nothing.
26
23
def add_plugin ( dir , entry_script = nil )
27
24
command ( "set runtimepath+=#{ dir } " )
28
25
command ( "runtime #{ entry_script } " ) if entry_script
29
26
end
30
27
31
- # Invokes one of the basic actions the Vim server supports, sending a key
32
- # sequence. The keys are sent as-is, so it'd probably be better to use the
33
- # wrapper methods, #normal, #insert and so on.
28
+ def normal ( keys = "" )
29
+ server . remote_send ( "<C-\\ ><C-n>#{ keys } " )
30
+ end
31
+
32
+ # Public: Invokes one of the basic actions the Vim server supports,
33
+ # sending a key sequence. The keys are sent as-is, so it'd probably be
34
+ # better to use the wrapper methods, #normal, #insert and so on.
35
+ #
36
+ # Returns nothing.
34
37
def type ( keys )
35
- invoke_vim '--remote-send' , keys
38
+ server . remote_send ( keys )
36
39
end
37
40
38
- # Executes the given command in the Vim instance and returns its output,
39
- # stripping all surrounding whitespace.
40
- def command ( vim_command )
41
+ # Public: Starts a search in Vim for the given text. The result is that
42
+ # the cursor is positioned on its first occurrence.
43
+ #
44
+ # Returns nothing.
45
+ def search ( text )
41
46
normal
47
+ type "/#{ text } <CR>"
48
+ end
42
49
43
- escaped_command = vim_command . to_s . gsub ( "'" , "''" )
44
- expression = "VimrunnerEvaluateCommandOutput('#{ escaped_command } ')"
50
+ # Public: Switches Vim to insert mode and types in the given text.
51
+ #
52
+ # Returns nothing.
53
+ def insert ( text )
54
+ normal "i#{ text } "
55
+ end
45
56
46
- invoke_vim ( '--remote-expr' , expression ) . tap do |output |
47
- raise InvalidCommandError if output =~ /^Vim:E\d +:/
48
- end
57
+ # Public: Writes the file being edited to disk. Note that you probably
58
+ # want to set the file's name first by using Runner#edit.
59
+ def write
60
+ command :write
49
61
end
50
62
51
- # Starts a search in Vim for the given text. The result is that the cursor
52
- # is positioned on its first occurrence.
53
- def search ( text )
54
- normal
55
- type "/ #{ text } <cr> "
63
+ # Public: Echo each expression with a space in between.
64
+ #
65
+ # Returns the String output.
66
+ def echo ( * expressions )
67
+ command "echo #{ expressions . join ( ' ' ) } "
56
68
end
57
69
58
- # Sets a setting in Vim. If +value+ is nil, the setting is considered to be
59
- # a boolean.
70
+ # Public: Sets a setting in Vim. If +value+ is nil, the setting is
71
+ # considered to be a boolean.
60
72
#
61
73
# Examples:
62
74
#
@@ -71,34 +83,26 @@ def set(setting, value = nil)
71
83
end
72
84
end
73
85
74
- # Edits the file +filename+ with Vim.
86
+ # Public: Edits the file +filename+ with Vim.
75
87
#
76
88
# Note that this doesn't use the '--remote' Vim flag, it simply types in
77
- # the command manually. This is necessary to avoid the Vim instance getting
78
- # focus.
89
+ # the command manually. This is necessary to avoid the Vim instance
90
+ # getting focus.
79
91
def edit ( filename )
80
92
command "edit #{ filename } "
81
93
end
82
94
83
- # Writes the file being edited to disk. Note that you probably want to set
84
- # the file's name first by using Runner#edit.
85
- def write
86
- command :write
87
- end
88
-
89
- # Echo each expression with a space in between.
90
- def echo ( *expressions )
91
- command "echo #{ expressions . join ( ' ' ) } "
92
- end
93
-
94
- # Switches Vim to insert mode and types in the given text.
95
- def insert ( text = '' )
96
- normal "i#{ text } "
97
- end
95
+ # Public: Executes the given command in the Vim instance and returns its
96
+ # output, stripping all surrounding whitespace.
97
+ #
98
+ # Returns the string output.
99
+ # Raises InvalidCommandError if the command is not recognised by vim.
100
+ def command ( commands )
101
+ expression = "VimrunnerEvaluateCommandOutput('#{ escape ( commands ) } ')"
98
102
99
- # Switches Vim to normal mode and types in the given keys.
100
- def normal ( keys = '' )
101
- type "<c- \\ ><c-n> #{ keys } "
103
+ server . remote_expr ( expression ) . tap do | output |
104
+ raise InvalidCommandError if output =~ /^Vim:E \d +:/
105
+ end
102
106
end
103
107
104
108
# Kills the server it's connected to.
@@ -108,8 +112,8 @@ def kill
108
112
109
113
private
110
114
111
- def invoke_vim ( * args )
112
- driver . run ( '--servername' , server . name , * args )
115
+ def escape ( string )
116
+ string . to_s . gsub ( "'" , "''" )
113
117
end
114
118
end
115
119
end
0 commit comments