From c2f2d54cee7ceb9b3bcf72ae906677a302bd3249 Mon Sep 17 00:00:00 2001 From: Gokcehan Date: Fri, 6 Nov 2020 15:48:28 +0300 Subject: [PATCH] apply 'ignorecase' and 'ignoredia' to sorting Related #320 --- doc.go | 4 ++-- docstring.go | 12 ++++++------ lf.1 | 18 ++++++++++++++++-- nav.go | 35 ++++++++++++++++++++++++----------- 4 files changed, 48 insertions(+), 21 deletions(-) diff --git a/doc.go b/doc.go index 6bbe836..cebd302 100644 --- a/doc.go +++ b/doc.go @@ -552,11 +552,11 @@ This option does not have any effect on windows. ignorecase bool (default on) -Ignore case in search patterns. +Ignore case in sorting and search patterns. ignoredia bool (default off) -Ignore diacritics in search patterns. +Ignore diacritics in sorting and search patterns. incsearch bool (default off) diff --git a/docstring.go b/docstring.go index f457aef..55cf437 100644 --- a/docstring.go +++ b/docstring.go @@ -428,13 +428,13 @@ Autocomplete the current word. cmd-menu-complete -Autocomplete the current word, then you can press the binded key/s -again to cycle completition options. +Autocomplete the current word, then you can press the binded key/s again to +cycle completition options. cmd-menu-complete-back -Autocomplete the current word, then you can press the binded key/s -again to cycle completition options backwards. +Autocomplete the current word, then you can press the binded key/s again to +cycle completition options backwards. cmd-enter (default '' and '') @@ -582,11 +582,11 @@ have any effect on windows. ignorecase bool (default on) -Ignore case in search patterns. +Ignore case in sorting and search patterns. ignoredia bool (default off) -Ignore diacritics in search patterns. +Ignore diacritics in sorting and search patterns. incsearch bool (default off) diff --git a/lf.1 b/lf.1 index 62554f5..2703e3e 100644 --- a/lf.1 +++ b/lf.1 @@ -83,6 +83,8 @@ The following command line commands are provided by lf: .EX cmd-escape (default '') cmd-complete (default '') + cmd-menu-complete + cmd-menu-complete-back cmd-enter (default '' and '') cmd-interrupt (default '') cmd-history-next (default '') @@ -491,6 +493,18 @@ Quit command line mode and return to normal mode. .PP Autocomplete the current word. .PP +.EX + cmd-menu-complete +.EE +.PP +Autocomplete the current word, then you can press the binded key/s again to cycle completition options. +.PP +.EX + cmd-menu-complete-back +.EE +.PP +Autocomplete the current word, then you can press the binded key/s again to cycle completition options backwards. +.PP .EX cmd-enter (default '' and '') .EE @@ -656,13 +670,13 @@ Sets 'IFS' variable in shell commands. It works by adding the assignment to the ignorecase bool (default on) .EE .PP -Ignore case in search patterns. +Ignore case in sorting and search patterns. .PP .EX ignoredia bool (default off) .EE .PP -Ignore diacritics in search patterns. +Ignore diacritics in sorting and search patterns. .PP .EX incsearch bool (default off) diff --git a/nav.go b/nav.go index fdf1d00..aff2657 100644 --- a/nav.go +++ b/nav.go @@ -134,6 +134,18 @@ func newDir(path string) *dir { } } +func normalize(s1, s2 string) (string, string) { + if gOpts.ignorecase { + s1 = strings.ToLower(s1) + s2 = strings.ToLower(s2) + } + if gOpts.ignoredia { + s1 = removeDiacritics(s1) + s2 = removeDiacritics(s2) + } + return s1, s2 +} + func (dir *dir) sort() { dir.sortType = gOpts.sortType dir.hiddenfiles = gOpts.hiddenfiles @@ -143,11 +155,13 @@ func (dir *dir) sort() { switch gOpts.sortType.method { case naturalSort: sort.SliceStable(dir.files, func(i, j int) bool { - return naturalLess(strings.ToLower(dir.files[i].Name()), strings.ToLower(dir.files[j].Name())) + s1, s2 := normalize(dir.files[i].Name(), dir.files[j].Name()) + return naturalLess(s1, s2) }) case nameSort: sort.SliceStable(dir.files, func(i, j int) bool { - return strings.ToLower(dir.files[i].Name()) < strings.ToLower(dir.files[j].Name()) + s1, s2 := normalize(dir.files[i].Name(), dir.files[j].Name()) + return s1 < s2 }) case sizeSort: sort.SliceStable(dir.files, func(i, j int) bool { @@ -167,23 +181,22 @@ func (dir *dir) sort() { }) case extSort: sort.SliceStable(dir.files, func(i, j int) bool { - leftExt := strings.ToLower(dir.files[i].ext) - rightExt := strings.ToLower(dir.files[j].ext) + ext1, ext2 := normalize(dir.files[i].ext, dir.files[j].ext) // if the extension could not be determined (directories, files without) // use a zero byte so that these files can be ranked higher - if leftExt == "" { - leftExt = "\x00" + if ext1 == "" { + ext1 = "\x00" } - if rightExt == "" { - rightExt = "\x00" + if ext2 == "" { + ext2 = "\x00" } + name1, name2 := normalize(dir.files[i].Name(), dir.files[j].Name()) + // in order to also have natural sorting with the filenames // combine the name with the ext but have the ext at the front - left := leftExt + strings.ToLower(dir.files[i].Name()) - right := rightExt + strings.ToLower(dir.files[j].Name()) - return left < right + return (ext1 + name1) < (ext2 + name2) }) }