-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathautocomplete.rb
122 lines (103 loc) · 2.96 KB
/
autocomplete.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
class Autocomplete < Plugin
class << self
attr_accessor :enabled
attr_accessor :words
attr_accessor :original_clipboard
attr_accessor :map
attr_accessor :breaks
attr_accessor :app_list
def toggle
@enabled = !@enabled
system (@enabled ? '/usr/local/bin/growlnotify -m "" -a Keymando Autocomplete enabled.' : '/usr/local/bin/growlnotify -m "" -a Keymando Autocomplete disabled.')
end
def stash_clipboard
s = IO.popen('pbpaste', 'r+').read
@original_clipboard = s
end
def get_words
s = IO.popen('pbpaste', 'r+').read
@words = s.scan(/\w+/).uniq
end
def filter_words(filter)
s = Regexp.escape(filter)
@words = @words.select do |w|
w.match(/^#{s}\w*/i)
end
@words.delete filter
@words.sort!
@words.push filter
end
def next_word
@words.shift if @words
end
def enabled?
@enabled
end
end
def before
Autocomplete.map = "<Ctrl- >"
Autocomplete.breaks = [" ","<Return>",".",",","?","-","<Delete>"]
Autocomplete.app_list = [/Mail/]
end
def after
Autocomplete.app_list.each do |app|
only app do
map Autocomplete.map do
Autocomplete.toggle
return if ! Autocomplete.enabled?
Autocomplete.breaks.each do |b|
map b do
reset if Autocomplete.enabled?
send(b)
end
end
Autocomplete.stash_clipboard
send('<Alt-Left>')
send('<Shift-Home>')
send('<Cmd-c>')
send('<Right>')
send('<Alt-Right>')
send('<Alt-Shift-Left>')
sleep(0.5)
Autocomplete.get_words
send('<Cmd-c>')
sleep(0.5)
m = IO.popen('pbpaste', 'r+').read
Autocomplete.filter_words(m)
map '<Tab>' do
fill
end
map "<Right>" do
reset
end
begin
fill
rescue
reset
alert('Autocomplete failed')
ensure
IO.popen('pbcopy', 'w').puts Autocomplete.original_clipboard
end
end
end
end
end
def reset
Autocomplete.toggle if Autocomplete.enabled?
Autocomplete.words = nil
Autocomplete.original_clipboard = nil
send('<Right>')
map('<Tab>', '<Tab>')
map("<Right>", "<Right>")
Autocomplete.breaks.each { |b| map b, b}
end
def fill
@next_word = Autocomplete.next_word
if (Autocomplete.enabled? && @next_word)
send(@next_word)
send('<Alt-Shift-Left>')
else
reset
end
end
end