forked from reeflective/readline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisearch.go
190 lines (157 loc) · 4.36 KB
/
isearch.go
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package readline
import (
"bytes"
"regexp"
"unicode"
"github.com/reiver/go-caret"
)
// The isearch keymap is empty by default: the widgets that can
// be used while in incremental search mode will be found in the
// main keymap, so that the same keybinds can be used.
var isearchKeys = map[string]string{}
// those widgets, generally found in the main keymap, are the only
// valid widgets to be used in the incremental search minibuffer.
var validIsearchWidgets = []string{
"accept-and-infer-next-history",
"accept-line",
"accept-line-and-down-history",
"accept-search",
"backward-delete-char",
"vi-backward-delete-char",
"backward-kill-word",
"backward-delete-word",
"vi-backward-kill-word",
"clear-screen",
"history-incremental-search-forward", // Not sure history- needed
"history-incremental-search-backward", // same
"space",
"quoted-insert",
"vi-quoted-insert",
"vi-cmd-mode",
"self-insert",
}
func (rl *Instance) enterIsearchMode() {
rl.local = isearch
rl.hint = []rune(seqBold + seqFgCyan + "isearch: " + seqReset)
rl.hint = append(rl.hint, rl.tfLine...)
}
// useIsearchLine replaces the input line with our current
// isearch buffer, the time for the widget to work on it.
func (rl *Instance) useIsearchLine() {
rl.lineBuf = string(rl.line)
rl.line = append([]rune{}, rl.tfLine...)
cpos := rl.pos
rl.pos = rl.tfPos
rl.tfPos = cpos
}
// exitIsearchLine resets the input line to its original once
// the widget used in isearch mode has done its work.
func (rl *Instance) exitIsearchLine() {
rl.tfLine = append([]rune{}, rl.line...)
rl.line = []rune(rl.lineBuf)
rl.lineBuf = ""
cpos := rl.tfPos
rl.tfPos = rl.pos
rl.pos = cpos
}
// updateIsearch recompiles the isearch as a regex and
// filters matching candidates in the available completions.
func (rl *Instance) updateIsearch() {
// First compile the search as regular expression
var regexStr string
if hasUpper(rl.tfLine) {
regexStr = string(rl.tfLine)
} else {
regexStr = "(?i)" + string(rl.tfLine)
}
var err error
rl.isearch, err = regexp.Compile(regexStr)
if err != nil {
rl.hint = append(rl.hint, []rune(seqFgRed+"Failed to compile search regexp")...)
}
if rl.completer != nil {
rl.completer()
}
// And filter out the completions.
for _, g := range rl.tcGroups {
g.updateIsearch(rl)
}
// In history isearch, insert the first matching candidate.
// This candidate will be cleared/replaced as soon as another
// key/change is operated on the isearch buffer.
if len(rl.histHint) > 0 && len(rl.tcGroups) > 0 && len(rl.tcGroups[0].values) > 0 {
rl.resetVirtualComp(true)
cur := rl.currentGroup()
cur.tcPosY = 0
cur.tcPosX = 0
rl.updateVirtualComp()
cur.tcPosY = -1
cur.tcPosX = -1
}
}
func (rl *Instance) isearchHint() {
// Append to the history hint when completing it.
currentMode := ""
if len(rl.histHint) > 0 {
currentMode = string(rl.histHint) + seqFgCyan + " (isearch): "
} else {
currentMode = "isearch: "
}
rl.hint = []rune(seqBold + seqFgCyan + currentMode + seqReset + seqBgDarkGray)
rl.hint = append(rl.hint, rl.tfLine...)
if rl.isearch == nil && len(rl.tfLine) > 0 {
rl.hint = append(rl.hint, []rune(seqFgRed+" ! failed to compile search regexp")...)
} else if rl.noCompletions() && len(rl.tfLine) > 0 {
rl.hint = append(rl.hint, []rune(seqFgRed+" ! no matches")...)
}
rl.hint = append(rl.hint, []rune(seqReset)...)
}
func (rl *Instance) resetIsearch() {
if rl.local != isearch {
return
}
rl.local = ""
rl.tfLine = []rune{}
rl.tfPos = 0
rl.isearch = nil
}
func (rl *Instance) isIsearchMode(mode keymapMode) bool {
if mode != emacs && mode != viins && mode != vicmd {
return false
}
if rl.local != isearch {
return false
}
return true
}
func (rl *Instance) filterIsearchWidgets(mode keymapMode) (isearch widgets) {
km := rl.config.Keymaps[mode]
isearch = make(widgets)
b := new(bytes.Buffer)
decoder := caret.Decoder{Writer: b}
for key, widget := range km {
// Widget must be a valid isearch widget
if !isValidIsearchWidget(widget) {
continue
}
// Or bind to our temporary isearch keymap
rl.bindWidget(key, widget, &isearch, decoder, b)
}
return
}
func isValidIsearchWidget(widget string) bool {
for _, isw := range validIsearchWidgets {
if isw == widget {
return true
}
}
return false
}
func hasUpper(line []rune) bool {
for _, r := range line {
if unicode.IsUpper(r) {
return true
}
}
return false
}