Skip to content
Myr

Allow escaping meta characters with backslashes

Before adoption: this commit came in with the history the project adopted, so no landing records it.

One can escape meta characters in extended-search mode with backslashes. Prefixes: \' \! \^ Suffix: \$ Term separator: \<SPACE> To keep things simple, we are not going to support escaping of escaped sequences (e.g. \\') for matching them literally. Since this is a breaking change, we will bump the minor version. Close #444

authored by Junegunn Choi · committed by Junegunn Choi · parent dc55e68 · Browse files · 4 files+63 added−22 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+63 added−22 removed

  • tests:+22 added−0 removed
  • other:+41 added−22 removed

4 files+63 added−22 removed

modified:src/pattern_test.go+6 added-6 removed
163 unmodified lines
164 if pat.CacheKey() != expected {165 t.Errorf("Expected: %s, actual: %s", expected, pat.CacheKey())166 }167 if pat.cacheable != cacheable {168 t.Errorf("Expected: %s, actual: %s (%s)", cacheable, pat.cacheable, patStr)168 t.Errorf("Expected: %t, actual: %t (%s)", cacheable, pat.cacheable, patStr)169 }170 clearPatternCache()171 }172 test(false, "foo !bar", "foo !bar", true)173 test(false, "foo | bar !baz", "foo | bar !baz", true)174 test(true, "foo bar baz", "foo bar baz", true)174 test(true, "foo bar baz", "foo\tbar\tbaz", true)175 test(true, "foo !bar", "foo", false)176 test(true, "foo !bar baz", "foo baz", false)176 test(true, "foo !bar baz", "foo\tbaz", false)177 test(true, "foo | bar baz", "baz", false)178 test(true, "foo | bar | baz", "", false)179 test(true, "foo | bar !baz", "", false)180 test(true, "| | | foo", "foo", true)
10 unmodified lines
191 t.Errorf("Invalid Pattern.cacheable for \"%s\": %v (expected: %v)", str, pat.cacheable, cacheable)192 }193 clearPatternCache()194 }195 test(true, "foo bar", "foo bar", true)196 test(true, "foo 'bar", "foo bar", false)195 test(true, "foo bar", "foo\tbar", true)196 test(true, "foo 'bar", "foo\tbar", false)197 test(true, "foo !bar", "foo", false)198199 test(false, "foo bar", "foo bar", true)199 test(false, "foo bar", "foo\tbar", true)200 test(false, "foo 'bar", "foo", false)201 test(false, "foo '", "foo", true)202 test(false, "foo 'bar", "foo", false)203 test(false, "foo !bar", "foo", false)
1 unmodified line
modified:src/pattern.go+28 added-13 removed
51 unmodified lines
52 procFun map[termType]algo.Algo53}5455var (56 _patternCache map[string]*Pattern57 _splitRegex *regexp.Regexp58 _cache ChunkCache56 _patternCache map[string]*Pattern57 _splitRegex *regexp.Regexp58 _escapedPrefixRegex *regexp.Regexp59 _cache ChunkCache60)6162func init() {62 _splitRegex = regexp.MustCompile("\\s+")63 _splitRegex = regexp.MustCompile(" +")64 _escapedPrefixRegex = regexp.MustCompile("^\\\\['!^]")65 clearPatternCache()66 clearChunkCache()67}68
12 unmodified lines
81 cacheable bool, nth []Range, delimiter Delimiter, runes []rune) *Pattern {8283 var asString string84 if extended {83 asString = strings.Trim(string(runes), " ")85 asString = strings.TrimLeft(string(runes), " ")86 for strings.HasSuffix(asString, " ") && !strings.HasSuffix(asString, "\\ ") {87 asString = asString[:len(asString)-1]88 }89 } else {90 asString = string(runes)91 }92
51 unmodified lines
144 return ptr145}146147func parseTerms(fuzzy bool, caseMode Case, normalize bool, str string) []termSet {148 str = strings.Replace(str, "\\ ", "\t", -1)149 tokens := _splitRegex.Split(str, -1)150 sets := []termSet{}151 set := termSet{}152 switchSet := false153 for _, token := range tokens {148 typ, inv, text := termFuzzy, false, token154 typ, inv, text := termFuzzy, false, strings.Replace(token, "\t", " ", -1)155 lowerText := strings.ToLower(text)156 caseSensitive := caseMode == CaseRespect ||157 caseMode == CaseSmart && text != lowerText158 if !caseSensitive {
13 unmodified lines
172 typ = termExact173 text = text[1:]174 }175176 if strings.HasSuffix(text, "$") {177 if strings.HasSuffix(text, "\\$") {178 text = text[:len(text)-2] + "$"179 } else {180 typ = termSuffix181 text = text[:len(text)-1]182 }183 }184185 if strings.HasPrefix(text, "'") {186 // Flip exactness187 if fuzzy && !inv {188 typ = termExact
2 unmodified lines
191 typ = termFuzzy192 text = text[1:]193 }194 } else if strings.HasPrefix(text, "^") {180 if strings.HasSuffix(text, "$") {195 if typ == termSuffix {196 typ = termEqual182 text = text[1 : len(text)-1]197 } else {198 typ = termPrefix185 text = text[1:]199 }187 } else if strings.HasSuffix(text, "$") {188 typ = termSuffix189 text = text[:len(text)-1]200 text = text[1:]201 }202203 if _escapedPrefixRegex.MatchString(text) {204 text = text[1:]205 }206207 if len(text) > 0 {208 if switchSet {209 sets = append(sets, set)210 set = termSet{}
39 unmodified lines
250 if len(termSet) == 1 && !termSet[0].inv && (p.fuzzy || termSet[0].typ == termExact) {251 cacheableTerms = append(cacheableTerms, string(termSet[0].text))252 }253 }239 return strings.Join(cacheableTerms, " ")254 return strings.Join(cacheableTerms, "\t")255}256257// Match returns the list of matches Items in the given Chunk258func (p *Pattern) Match(chunk *Chunk, slab *util.Slab) []Result {
145 unmodified lines
modified:src/terminal.go+7 added-3 removed
279 unmodified lines
280 keymap[tui.DoubleClick] = toActions(actAccept)281 return keymap282}283284func trimQuery(query string) []rune {285 return []rune(strings.Replace(query, "\t", " ", -1))286}287288// NewTerminal returns new Terminal object289func NewTerminal(opts *Options, eventBox *util.EventBox) *Terminal {286 input := []rune(opts.Query)290 input := trimQuery(opts.Query)291 var header []string292 if opts.Reverse {293 header = opts.Header294 } else {
1402 unmodified lines
1697 t.cx++1698 case actPreviousHistory:1699 if t.history != nil {1700 t.history.override(string(t.input))1697 t.input = []rune(t.history.previous())1701 t.input = trimQuery(t.history.previous())1702 t.cx = len(t.input)1703 }1704 case actNextHistory:1705 if t.history != nil {1706 t.history.override(string(t.input))1703 t.input = []rune(t.history.next())1707 t.input = trimQuery(t.history.next())1708 t.cx = len(t.input)1709 }1710 case actSigStop:1711 p, err := os.FindProcess(os.Getpid())
145 unmodified lines
modified:test/test_go.rb+22 added
1376 unmodified lines
1377 tmux.until { |lines| lines.any? { |line| line.include? '1 2 3 4 5' } }1378 tmux.send_keys 'a'1379 tmux.until { |lines| lines.none? { |line| line.include? '1 2 3 4 5' } }1380 end13811382 def test_escaped_meta_characters1383 input = <<~EOF1384 foo^bar1385 foo$bar1386 foo!bar1387 foo'bar1388 foo bar1389 bar foo1390 EOF1391 writelines tempname, input.lines.map(&:chomp)13921393 assert_equal input.lines.count, `#{FZF} -f'foo bar' < #{tempname}`.lines.count1394 assert_equal ['foo bar'], `#{FZF} -f'foo\\ bar' < #{tempname}`.lines.map(&:chomp)1395 assert_equal ['bar foo'], `#{FZF} -f'foo$' < #{tempname}`.lines.map(&:chomp)1396 assert_equal ['foo$bar'], `#{FZF} -f'foo\\$' < #{tempname}`.lines.map(&:chomp)1397 assert_equal [], `#{FZF} -f'!bar' < #{tempname}`.lines.map(&:chomp)1398 assert_equal ['foo!bar'], `#{FZF} -f'\\!bar' < #{tempname}`.lines.map(&:chomp)1399 assert_equal ['foo bar'], `#{FZF} -f'^foo\\ bar$' < #{tempname}`.lines.map(&:chomp)1400 assert_equal [], `#{FZF} -f"'br" < #{tempname}`.lines.map(&:chomp)1401 assert_equal ["foo'bar"], `#{FZF} -f"\\'br" < #{tempname}`.lines.map(&:chomp)1402 end1403end14041405module TestShell1406 def setup
353 unmodified lines