From ab662e6f33de76fd5bd4174a54ae3cd2006e22b9 Mon Sep 17 00:00:00 2001 From: CatsDeservePets <145048791+CatsDeservePets@users.noreply.github.com> Date: Sat, 26 Jul 2025 13:25:45 +0200 Subject: [PATCH] Add `filtermethod` and `searchmethod` options (#2058) * Add `filtermethod` and `searchmethod` options * Rename `re` to `regex` * Remove `globfilter` and `globsearch` options * Add completions for `filtermethod` and `searchmethod` * Update documentation * Remove unnecessary if statement * Fix unrelated typo * Put options in alphabetical order * Fix typo in documentation * Rename variable for consistency --------- Co-authored-by: Joe Lim <50560759+joelim-work@users.noreply.github.com> --- complete.go | 2 ++ diacritics_test.go | 2 +- doc.md | 32 ++++++++++++++++++-------------- doc.txt | 34 ++++++++++++++++++---------------- eval.go | 32 +++++++++++++++++++++++--------- lf.1 | 40 +++++++++++++++++++++++++--------------- nav.go | 23 +++++++++++++++-------- opts.go | 16 ++++++++++++---- 8 files changed, 114 insertions(+), 67 deletions(-) diff --git a/complete.go b/complete.go index 435f79a9..a60aea5e 100644 --- a/complete.go +++ b/complete.go @@ -343,6 +343,8 @@ func completeCmd(acc []rune) (matches []string, longestAcc []rune) { break } switch f[1] { + case "filtermethod", "searchmethod": + matches, longest = matchWord(f[2], []string{"glob", "regex", "text"}) case "selmode": matches, longest = matchWord(f[2], []string{"all", "dir"}) case "sortby": diff --git a/diacritics_test.go b/diacritics_test.go index f2b67091..13a298d5 100644 --- a/diacritics_test.go +++ b/diacritics_test.go @@ -60,7 +60,7 @@ func runSearch(t *testing.T, ignorecase, smartcase, ignorediacritics, smartdiacr gOpts.smartcase = smartcase gOpts.ignoredia = ignorediacritics gOpts.smartdia = smartdiacritics - matched, _ := searchMatch(base, pattern, false) + matched, _ := searchMatch(base, pattern, textSearch) if matched != expected { t.Errorf("False search for ignorecase = %t, smartcase = %t, ignoredia = %t, smartdia = %t", gOpts.ignorecase, diff --git a/doc.md b/doc.md index 19a81722..8bbc7300 100644 --- a/doc.md +++ b/doc.md @@ -163,9 +163,8 @@ The following options can be used to customize the behavior of lf: dupfilefmt string (default '%f.~%n~') errorfmt string (default "\033[7;31;47m") filesep string (default "\n") + filtermethod string (default 'text') findlen int (default 1) - globfilter bool (default false) - globsearch bool (default false) hidden bool (default false) hiddenfiles []string (default '.*' for Unix and '' for Windows) history bool (default true) @@ -193,6 +192,7 @@ The following options can be used to customize the behavior of lf: roundbox bool (default false) rulerfmt string (default " %a| %p| \033[7;31m %m \033[0m| \033[7;33m %c \033[0m| \033[7;35m %s \033[0m| \033[7;34m %f \033[0m| %i/%t") scrolloff int (default 0) + searchmethod string (default 'text') selectfmt string (default "\033[7;35m") selmode string (default 'all') shell string (default 'sh' for Unix and 'cmd' for Windows) @@ -789,21 +789,17 @@ For example, `\033[4m%s\033[0m` has the same effect as `\033[4m`. File separator used in environment variables `fs`, `fv` and `fx`. +## filtermethod (string) (default `text`) + +How filter command patterns are treated. +Currently supported methods are `text` (i.e. string literals), `glob` (i.e shell globs) and `regex` (i.e. regular expressions). +See `SEARCHING FILES` for more details. + ## findlen (int) (default 1) Number of characters prompted for the find command. When this value is set to 0, find command prompts until there is only a single match left. -## globfilter (bool) (default false) - -Patterns are treated as globs for the filter command, see `globsearch` for more details. - -## globsearch (bool) (default false) - -When this option is enabled, search command patterns are considered as globs, otherwise, they are literals. -With globbing, `*` matches any sequence, `?` matches any character, and `[...]` or `[^...]` matches character sets or ranges. -Otherwise, these characters are interpreted as they are. - ## hidden (bool) (default false) Show hidden files. @@ -895,7 +891,7 @@ Periodic checks are disabled when the value of this option is set to zero. ## preserve ([]string) (default `mode`) List of attributes that are preserved when copying files. -Currently supported attributes are `mode` (i.a. access mode) and `timestamps` (i.e. modification time and access time). +Currently supported attributes are `mode` (i.e. access mode) and `timestamps` (i.e. modification time and access time). Note that preserving other attributes like ownership of change/birth timestamp is desirable, but not portably supported in Go. ## preview (bool) (default true) @@ -964,6 +960,12 @@ Minimum number of offset lines shown at all times at the top and bottom of the s The current line is kept in the middle when this option is set to a large value that is bigger than the half of number of lines. A smaller offset can be used when the current file is close to the beginning or end of the list to show the maximum number of items. +## searchmethod (string) default `text`) + +How search command patterns are treated. +Currently supported methods are `text` (i.e. string literals), `glob` (i.e shell globs) and `regex` (i.e. regular expressions). +See `SEARCHING FILES` for more details. + ## shell (string) (default `sh` for Unix and `cmd` for Windows) Shell executable to use for shell commands. @@ -1632,8 +1634,10 @@ Searching is the traditional method to move the selection to a file matching a g Finding is an alternative way to search for a pattern possibly using fewer keystrokes. The searching mechanism is implemented with commands `search` (default `/`), `search-back` (default `?`), `search-next` (default `n`), and `search-prev` (default `N`). -You can enable `globsearch` option to match with a glob pattern. +You can set `searchmethod` to `glob` to match using a glob pattern. Globbing supports `*` to match any sequence, `?` to match any character, and `[...]` or `[^...]` to match character sets or ranges. +You can set `searchmethod` to `regex` to match using a regex pattern. +For a full overview of Go's RE2 syntax, see https://pkg.go.dev/regexp/syntax. You can enable `incsearch` option to jump to the current match at each keystroke while typing. In this mode, you can either use `cmd-enter` to accept the search or use `cmd-escape` to cancel the search. You can also map some other commands with `cmap` to accept the search and execute the command immediately afterwards. diff --git a/doc.txt b/doc.txt index 4c242396..a4166d98 100644 --- a/doc.txt +++ b/doc.txt @@ -153,9 +153,8 @@ The following options can be used to customize the behavior of lf: dupfilefmt string (default '%f.~%n~') errorfmt string (default "\033[7;31;47m") filesep string (default "\n") + filtermethod string (default 'text') findlen int (default 1) - globfilter bool (default false) - globsearch bool (default false) hidden bool (default false) hiddenfiles []string (default '.*' for Unix and '' for Windows) history bool (default true) @@ -183,6 +182,7 @@ The following options can be used to customize the behavior of lf: roundbox bool (default false) rulerfmt string (default " %a| %p| \033[7;31m %m \033[0m| \033[7;33m %c \033[0m| \033[7;35m %s \033[0m| \033[7;34m %f \033[0m| %i/%t") scrolloff int (default 0) + searchmethod string (default 'text') selectfmt string (default "\033[7;35m") selmode string (default 'all') shell string (default 'sh' for Unix and 'cmd' for Windows) @@ -839,23 +839,17 @@ filesep (string) (default \n) File separator used in environment variables fs, fv and fx. +filtermethod (string) (default text) + +How filter command patterns are treated. Currently supported methods are +text (i.e. string literals), glob (i.e shell globs) and regex (i.e. +regular expressions). See SEARCHING FILES for more details. + findlen (int) (default 1) Number of characters prompted for the find command. When this value is set to 0, find command prompts until there is only a single match left. -globfilter (bool) (default false) - -Patterns are treated as globs for the filter command, see globsearch for -more details. - -globsearch (bool) (default false) - -When this option is enabled, search command patterns are considered as -globs, otherwise, they are literals. With globbing, * matches any -sequence, ? matches any character, and [...] or [^...] matches character -sets or ranges. Otherwise, these characters are interpreted as they are. - hidden (bool) (default false) Show hidden files. On Unix systems, hidden files are determined by the @@ -958,7 +952,7 @@ disabled when the value of this option is set to zero. preserve ([]string) (default mode) List of attributes that are preserved when copying files. Currently -supported attributes are mode (i.a. access mode) and timestamps (i.e. +supported attributes are mode (i.e. access mode) and timestamps (i.e. modification time and access time). Note that preserving other attributes like ownership of change/birth timestamp is desirable, but not portably supported in Go. @@ -1052,6 +1046,12 @@ number of lines. A smaller offset can be used when the current file is close to the beginning or end of the list to show the maximum number of items. +searchmethod (string) default text) + +How search command patterns are treated. Currently supported methods are +text (i.e. string literals), glob (i.e shell globs) and regex (i.e. +regular expressions). See SEARCHING FILES for more details. + shell (string) (default sh for Unix and cmd for Windows) Shell executable to use for shell commands. Shell commands are executed @@ -1820,9 +1820,11 @@ way to search for a pattern possibly using fewer keystrokes. The searching mechanism is implemented with commands search (default /), search-back (default ?), search-next (default n), and search-prev -(default N). You can enable globsearch option to match with a glob +(default N). You can set searchmethod to glob to match using a glob pattern. Globbing supports * to match any sequence, ? to match any character, and [...] or [^...] to match character sets or ranges. You +can set searchmethod to regex to match using a regex pattern. For a full +overview of Go's RE2 syntax, see https://pkg.go.dev/regexp/syntax. You can enable incsearch option to jump to the current match at each keystroke while typing. In this mode, you can either use cmd-enter to accept the search or use cmd-escape to cancel the search. You can also diff --git a/eval.go b/eval.go index cefbcbe1..ce802f00 100644 --- a/eval.go +++ b/eval.go @@ -98,16 +98,22 @@ func (e *setExpr) eval(app *app, args []string) { } app.ui.loadFile(app, true) } - case "globfilter", "noglobfilter", "globfilter!": - err = applyBoolOpt(&gOpts.globfilter, e) - if err == nil { - app.nav.sort() - app.nav.position() - app.ui.sort() - app.ui.loadFile(app, true) + case "filtermethod": + switch e.val { + case "text", "glob", "regex": + gOpts.filtermethod = searchMethod(e.val) + default: + app.ui.echoerr("filtermethod: value should either be 'text', 'glob' or 'regex") + return } + app.nav.sort() + app.nav.position() + app.ui.sort() + app.ui.loadFile(app, true) + case "globfilter", "noglobfilter", "globfilter!": + app.ui.echoerr("option 'globfilter' is deprecated, use 'filtermethod' instead") case "globsearch", "noglobsearch", "globsearch!": - err = applyBoolOpt(&gOpts.globsearch, e) + app.ui.echoerr("option 'globsearch' is deprecated, use 'searchmethod' instead") case "hidden", "nohidden", "hidden!": err = applyBoolOpt(&gOpts.hidden, e) if err == nil { @@ -302,7 +308,7 @@ func (e *setExpr) eval(app *app, args []string) { switch s { case "mode", "timestamps": default: - app.ui.echoerr("preserve: should consist of 'mode' or 'timestamps separated with colon") + app.ui.echoerr("preserve: should consist of 'mode' or 'timestamps' separated with colon") return } } @@ -370,6 +376,14 @@ func (e *setExpr) eval(app *app, args []string) { return } gOpts.scrolloff = n + case "searchmethod": + switch e.val { + case "text", "glob", "regex": + gOpts.searchmethod = searchMethod(e.val) + default: + app.ui.echoerr("searchmethod: value should either be 'text', 'glob' or 'regex'") + return + } case "selectfmt": gOpts.selectfmt = e.val case "selmode": diff --git a/lf.1 b/lf.1 index 4853b30e..99cf694e 100644 --- a/lf.1 +++ b/lf.1 @@ -168,9 +168,8 @@ drawbox bool (default false) dupfilefmt string (default \(aq%f.\(ti%n\(ti\(aq) errorfmt string (default \(dq\(rs033[7;31;47m\(dq) filesep string (default \(dq\(rsn\(dq) +filtermethod string (default \(aqtext\(aq) findlen int (default 1) -globfilter bool (default false) -globsearch bool (default false) hidden bool (default false) hiddenfiles []string (default \(aq.*\(aq for Unix and \(aq\(aq for Windows) history bool (default true) @@ -198,6 +197,7 @@ reverse bool (default false) roundbox bool (default false) rulerfmt string (default \(dq %a| %p| \(rs033[7;31m %m \(rs033[0m| \(rs033[7;33m %c \(rs033[0m| \(rs033[7;35m %s \(rs033[0m| \(rs033[7;34m %f \(rs033[0m| %i/%t\(dq) scrolloff int (default 0) +searchmethod string (default \(aqtext\(aq) selectfmt string (default \(dq\(rs033[7;35m\(dq) selmode string (default \(aqall\(aq) shell string (default \(aqsh\(aq for Unix and \(aqcmd\(aq for Windows) @@ -736,20 +736,17 @@ For example, \f[CR]\(rs033[4m%s\(rs033[0m\f[R] has the same effect as .SS filesep (string) (default \f[CR]\(rsn\f[R]) File separator used in environment variables \f[CR]fs\f[R], \f[CR]fv\f[R] and \f[CR]fx\f[R]. +.SS filtermethod (string) (default \f[CR]text\f[R]) +How filter command patterns are treated. +Currently supported methods are \f[CR]text\f[R] (i.e. +string literals), \f[CR]glob\f[R] (i.e shell globs) and \f[CR]regex\f[R] +(i.e. +regular expressions). +See \f[CR]SEARCHING FILES\f[R] for more details. .SS findlen (int) (default 1) Number of characters prompted for the find command. When this value is set to 0, find command prompts until there is only a single match left. -.SS globfilter (bool) (default false) -Patterns are treated as globs for the filter command, see -\f[CR]globsearch\f[R] for more details. -.SS globsearch (bool) (default false) -When this option is enabled, search command patterns are considered as -globs, otherwise, they are literals. -With globbing, \f[CR]*\f[R] matches any sequence, \f[CR]?\f[R] matches -any character, and \f[CR][...]\f[R] or \f[CR][\(ha...]\f[R] matches -character sets or ranges. -Otherwise, these characters are interpreted as they are. .SS hidden (bool) (default false) Show hidden files. On Unix systems, hidden files are determined by the value of @@ -833,7 +830,7 @@ Periodic checks are disabled when the value of this option is set to zero. .SS preserve ([]string) (default \f[CR]mode\f[R]) List of attributes that are preserved when copying files. -Currently supported attributes are \f[CR]mode\f[R] (i.a. +Currently supported attributes are \f[CR]mode\f[R] (i.e. access mode) and \f[CR]timestamps\f[R] (i.e. modification time and access time). Note that preserving other attributes like ownership of change/birth @@ -922,6 +919,13 @@ The current line is kept in the middle when this option is set to a large value that is bigger than the half of number of lines. A smaller offset can be used when the current file is close to the beginning or end of the list to show the maximum number of items. +.SS searchmethod (string) default \f[CR]text\f[R]) +How search command patterns are treated. +Currently supported methods are \f[CR]text\f[R] (i.e. +string literals), \f[CR]glob\f[R] (i.e shell globs) and \f[CR]regex\f[R] +(i.e. +regular expressions). +See \f[CR]SEARCHING FILES\f[R] for more details. .SS shell (string) (default \f[CR]sh\f[R] for Unix and \f[CR]cmd\f[R] for Windows) Shell executable to use for shell commands. Shell commands are executed as @@ -1735,11 +1739,17 @@ The searching mechanism is implemented with commands \f[CR]search\f[R] (default \f[CR]/\f[R]), \f[CR]search\-back\f[R] (default \f[CR]?\f[R]), \f[CR]search\-next\f[R] (default \f[CR]n\f[R]), and \f[CR]search\-prev\f[R] (default \f[CR]N\f[R]). -You can enable \f[CR]globsearch\f[R] option to match with a glob -pattern. +You can set \f[CR]searchmethod\f[R] to \f[CR]glob\f[R] to match using a +glob pattern. Globbing supports \f[CR]*\f[R] to match any sequence, \f[CR]?\f[R] to match any character, and \f[CR][...]\f[R] or \f[CR][\(ha...]\f[R] to match character sets or ranges. +You can set \f[CR]searchmethod\f[R] to \f[CR]regex\f[R] to match using a +regex pattern. +For a full overview of Go\(aqs RE2 syntax, see \c +.UR https://pkg.go.dev/regexp/syntax +.UE \c +\&. You can enable \f[CR]incsearch\f[R] option to jump to the current match at each keystroke while typing. In this mode, you can either use \f[CR]cmd\-enter\f[R] to accept the diff --git a/nav.go b/nav.go index 7a15535c..49a80ecf 100644 --- a/nav.go +++ b/nav.go @@ -11,6 +11,7 @@ import ( "os/exec" "path/filepath" "reflect" + "regexp" "slices" "sort" "strconv" @@ -1788,7 +1789,7 @@ func (nav *nav) findPrev() (bool, bool) { return false, false } -func searchMatch(name, pattern string, glob bool) (matched bool, err error) { +func searchMatch(name, pattern string, method searchMethod) (matched bool, err error) { if gOpts.ignorecase { lpattern := strings.ToLower(pattern) if !gOpts.smartcase || lpattern == pattern { @@ -1803,16 +1804,22 @@ func searchMatch(name, pattern string, glob bool) (matched bool, err error) { name = removeDiacritics(name) } } - if glob { + switch method { + case textSearch: + return strings.Contains(name, pattern), nil + case globSearch: return filepath.Match(pattern, name) + case regexSearch: + return regexp.MatchString(pattern, name) + default: + return false, fmt.Errorf("searchMatch: invalid searchMethod") } - return strings.Contains(name, pattern), nil } func (nav *nav) searchNext() (bool, error) { dir := nav.currDir() for i := dir.ind + 1; i < len(dir.files); i++ { - if matched, err := searchMatch(dir.files[i].Name(), nav.search, gOpts.globsearch); err != nil { + if matched, err := searchMatch(dir.files[i].Name(), nav.search, gOpts.searchmethod); err != nil { return false, err } else if matched { return nav.down(i - dir.ind), nil @@ -1820,7 +1827,7 @@ func (nav *nav) searchNext() (bool, error) { } if gOpts.wrapscan { for i := range dir.ind { - if matched, err := searchMatch(dir.files[i].Name(), nav.search, gOpts.globsearch); err != nil { + if matched, err := searchMatch(dir.files[i].Name(), nav.search, gOpts.searchmethod); err != nil { return false, err } else if matched { dir.visualWrap += 1 @@ -1834,7 +1841,7 @@ func (nav *nav) searchNext() (bool, error) { func (nav *nav) searchPrev() (bool, error) { dir := nav.currDir() for i := dir.ind - 1; i >= 0; i-- { - if matched, err := searchMatch(dir.files[i].Name(), nav.search, gOpts.globsearch); err != nil { + if matched, err := searchMatch(dir.files[i].Name(), nav.search, gOpts.searchmethod); err != nil { return false, err } else if matched { return nav.up(dir.ind - i), nil @@ -1842,7 +1849,7 @@ func (nav *nav) searchPrev() (bool, error) { } if gOpts.wrapscan { for i := len(dir.files) - 1; i > dir.ind; i-- { - if matched, err := searchMatch(dir.files[i].Name(), nav.search, gOpts.globsearch); err != nil { + if matched, err := searchMatch(dir.files[i].Name(), nav.search, gOpts.searchmethod); err != nil { return false, err } else if matched { dir.visualWrap -= 1 @@ -1855,7 +1862,7 @@ func (nav *nav) searchPrev() (bool, error) { func isFiltered(f os.FileInfo, filter []string) bool { for _, pattern := range filter { - matched, err := searchMatch(f.Name(), strings.TrimPrefix(pattern, "!"), gOpts.globfilter) + matched, err := searchMatch(f.Name(), strings.TrimPrefix(pattern, "!"), gOpts.filtermethod) if err != nil { log.Printf("Filter Error: %s", err) return false diff --git a/opts.go b/opts.go index a40f731c..954c1ebd 100644 --- a/opts.go +++ b/opts.go @@ -35,6 +35,14 @@ func isValidSortMethod(method sortMethod) bool { const invalidSortErrorMessage = `sortby: value should either be 'natural', 'name', 'size', 'time', 'atime', 'btime', 'ctime', 'ext' or 'custom'` +type searchMethod string + +const ( + textSearch searchMethod = "text" + globSearch searchMethod = "glob" + regexSearch searchMethod = "regex" +) + var gOpts struct { anchorfind bool autoquit bool @@ -51,8 +59,8 @@ var gOpts struct { dirpreviews bool drawbox bool dupfilefmt string - globfilter bool - globsearch bool + searchmethod searchMethod + filtermethod searchMethod hidden bool icons bool ignorecase bool @@ -220,8 +228,8 @@ func init() { gOpts.cursorparentfmt = "\033[7m" gOpts.cursorpreviewfmt = "\033[4m" gOpts.cutfmt = "\033[7;31m" - gOpts.globfilter = false - gOpts.globsearch = false + gOpts.filtermethod = textSearch + gOpts.searchmethod = textSearch gOpts.hidden = false gOpts.icons = false gOpts.ignorecase = true