From 67f1ff9c0e5440adc31d9a8f5ee0ff05a7696da6 Mon Sep 17 00:00:00 2001 From: Harrison McCullough Date: Fri, 17 Jan 2020 11:05:06 -0500 Subject: [PATCH] Add support for fixed strings and --less-search-pattern Previously, if you tried to use the `-F`/`--fixed strings` option for ripgrep in combination with the `--less-search-pattern` option, `less` would either a) match a different pattern than intended, since it would be interpreted as a regular expression, or b) refuse to start if it detected an invalid pattern. This change keeps track of whether or not the `--fixed-strings` option was passed in. If both it and the `--less-search-pattern` was passed in, a `^R` (Control-R) control character is prepended to the search pattern. This instructs less to *not* treat the pattern as a regular expression. --- src/batgrep.sh | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/batgrep.sh b/src/batgrep.sh index e49ddf7..d9c91c1 100755 --- a/src/batgrep.sh +++ b/src/batgrep.sh @@ -31,6 +31,7 @@ OPT_FOLLOW=true OPT_SNIP="" OPT_HIGHLIGHT=true OPT_SEARCH_PATTERN=false +OPT_FIXED_STRINGS=false BAT_STYLE="header,numbers" # Set options based on the bat version. @@ -51,7 +52,8 @@ while shiftopt; do -C|--context) shiftval; OPT_CONTEXT_BEFORE="$OPT_VAL"; OPT_CONTEXT_AFTER="$OPT_VAL";; - -F|--fixed-strings|\ + -F|--fixed-strings) OPT_FIXED_STRINGS=true; RG_ARGS+=("$OPT");; + -U|--multiline|\ -P|--pcre2|\ -z|--search-zip|\ @@ -138,7 +140,17 @@ if [[ "$OPT_CONTEXT_BEFORE" -eq 0 && "$OPT_CONTEXT_AFTER" -eq 0 ]]; then fi if [[ "$OPT_LESS_SEARCH_PATTERN" = true ]]; then - SCRIPT_PAGER_ARGS+=(-p "$PATTERN") + if [[ "$OPT_FIXED_STRINGS" = true ]]; then + # Depending on your editor, this may look identical. However, there is a + # special control character at the beginning of the pattern string, right + # after the first double quote. It is a a ^R, or Control-R, character. This + # instructs less to NOT use regular expressions, which is what the -F flag + # does for ripgrep. If we did not use this, then less would match a + # different pattern than ripgrep searched for. + SCRIPT_PAGER_ARGS+=(-p "$PATTERN") + else + SCRIPT_PAGER_ARGS+=(-p "$PATTERN") + fi fi # -----------------------------------------------------------------------------