add ignoredia and smartdia option for search and find without diacritics (#118)
This commit is contained in:
1 parent
4ee6488a09
commit
c693b7eae4
7 files changed
+174
No files matched your search
@@ -81,6 +81,9 @@ var (
|
|||||||
"ignorecase",
|
"ignorecase",
|
||||||
"noignorecase",
|
"noignorecase",
|
||||||
"ignorecase!",
|
"ignorecase!",
|
||||||
|
"ignoredia",
|
||||||
|
"noignoredia",
|
||||||
|
"ignoredia!",
|
||||||
"incsearch",
|
"incsearch",
|
||||||
"noincsearch",
|
"noincsearch",
|
||||||
"incsearch!",
|
"incsearch!",
|
||||||
@@ -93,6 +96,9 @@ var (
|
|||||||
"smartcase",
|
"smartcase",
|
||||||
"nosmartcase",
|
"nosmartcase",
|
||||||
"smartcase!",
|
"smartcase!",
|
||||||
|
"smartdia",
|
||||||
|
"nosmartdia",
|
||||||
|
"smartdia!",
|
||||||
"wrapscan",
|
"wrapscan",
|
||||||
"nowrapscan",
|
"nowrapscan",
|
||||||
"wrapscan!",
|
"wrapscan!",
|
||||||
|
|||||||
@@ -80,6 +80,12 @@ func (e *setExpr) eval(app *app, args []string) {
|
|||||||
gOpts.ignorecase = false
|
gOpts.ignorecase = false
|
||||||
case "ignorecase!":
|
case "ignorecase!":
|
||||||
gOpts.ignorecase = !gOpts.ignorecase
|
gOpts.ignorecase = !gOpts.ignorecase
|
||||||
|
case "ignoredia":
|
||||||
|
gOpts.ignoredia = true
|
||||||
|
case "noignoredia":
|
||||||
|
gOpts.ignoredia = false
|
||||||
|
case "ignoredia!":
|
||||||
|
gOpts.ignoredia = !gOpts.ignoredia
|
||||||
case "incsearch":
|
case "incsearch":
|
||||||
gOpts.incsearch = true
|
gOpts.incsearch = true
|
||||||
case "noincsearch":
|
case "noincsearch":
|
||||||
@@ -110,6 +116,12 @@ func (e *setExpr) eval(app *app, args []string) {
|
|||||||
gOpts.smartcase = false
|
gOpts.smartcase = false
|
||||||
case "smartcase!":
|
case "smartcase!":
|
||||||
gOpts.smartcase = !gOpts.smartcase
|
gOpts.smartcase = !gOpts.smartcase
|
||||||
|
case "smartdia":
|
||||||
|
gOpts.smartdia = true
|
||||||
|
case "nosmartdia":
|
||||||
|
gOpts.smartdia = false
|
||||||
|
case "smartdia!":
|
||||||
|
gOpts.smartdia = !gOpts.smartdia
|
||||||
case "wrapscan":
|
case "wrapscan":
|
||||||
gOpts.wrapscan = true
|
gOpts.wrapscan = true
|
||||||
case "nowrapscan":
|
case "nowrapscan":
|
||||||
|
|||||||
@@ -115,10 +115,12 @@ The following options can be used to customize the behavior of lf:
|
|||||||
globsearch boolean (default off)
|
globsearch boolean (default off)
|
||||||
hidden boolean (default off)
|
hidden boolean (default off)
|
||||||
ignorecase boolean (default on)
|
ignorecase boolean (default on)
|
||||||
|
ignoredia boolean (default off) (ignore diacritics in latin letters for search and find)
|
||||||
incsearch boolean (default off)
|
incsearch boolean (default off)
|
||||||
preview boolean (default on)
|
preview boolean (default on)
|
||||||
reverse boolean (default off)
|
reverse boolean (default off)
|
||||||
smartcase boolean (default on)
|
smartcase boolean (default on)
|
||||||
|
smartdia boolean (default off)
|
||||||
wrapscan boolean (default on)
|
wrapscan boolean (default on)
|
||||||
findlen integer (default 1) (zero to prompt until single match)
|
findlen integer (default 1) (zero to prompt until single match)
|
||||||
period integer (default 0) (zero to disable periodic loading)
|
period integer (default 0) (zero to disable periodic loading)
|
||||||
|
|||||||
@@ -667,6 +667,13 @@ func findMatch(name, pattern string) bool {
|
|||||||
name = strings.ToLower(name)
|
name = strings.ToLower(name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if gOpts.ignoredia {
|
||||||
|
lpattern := removeDiacritics(pattern)
|
||||||
|
if !gOpts.smartdia || lpattern == pattern {
|
||||||
|
pattern = lpattern
|
||||||
|
name = removeDiacritics(name)
|
||||||
|
}
|
||||||
|
}
|
||||||
if gOpts.anchorfind {
|
if gOpts.anchorfind {
|
||||||
return strings.HasPrefix(name, pattern)
|
return strings.HasPrefix(name, pattern)
|
||||||
}
|
}
|
||||||
@@ -742,6 +749,13 @@ func searchMatch(name, pattern string) (matched bool, err error) {
|
|||||||
name = strings.ToLower(name)
|
name = strings.ToLower(name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if gOpts.ignoredia {
|
||||||
|
lpattern := removeDiacritics(pattern)
|
||||||
|
if !gOpts.smartdia || lpattern == pattern {
|
||||||
|
pattern = lpattern
|
||||||
|
name = removeDiacritics(name)
|
||||||
|
}
|
||||||
|
}
|
||||||
if gOpts.globsearch {
|
if gOpts.globsearch {
|
||||||
return filepath.Match(pattern, name)
|
return filepath.Match(pattern, name)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,6 +33,8 @@ var gOpts struct {
|
|||||||
incsearch bool
|
incsearch bool
|
||||||
preview bool
|
preview bool
|
||||||
smartcase bool
|
smartcase bool
|
||||||
|
ignoredia bool
|
||||||
|
smartdia bool
|
||||||
wrapscan bool
|
wrapscan bool
|
||||||
findlen int
|
findlen int
|
||||||
period int
|
period int
|
||||||
@@ -62,6 +64,8 @@ func init() {
|
|||||||
gOpts.incsearch = false
|
gOpts.incsearch = false
|
||||||
gOpts.preview = true
|
gOpts.preview = true
|
||||||
gOpts.smartcase = true
|
gOpts.smartcase = true
|
||||||
|
gOpts.ignoredia = false
|
||||||
|
gOpts.smartdia = false
|
||||||
gOpts.wrapscan = true
|
gOpts.wrapscan = true
|
||||||
gOpts.findlen = 1
|
gOpts.findlen = 1
|
||||||
gOpts.period = 0
|
gOpts.period = 0
|
||||||
|
|||||||
@@ -0,0 +1,64 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
var normMap map[rune]rune
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
normMap = make(map[rune]rune)
|
||||||
|
|
||||||
|
// (not only) european
|
||||||
|
appendTransliterate(
|
||||||
|
"ěřůøĉĝĥĵŝŭèùÿėįųāēīūļķņģőűëïąćęłńśźżõșțčďĺľňŕšťýžéíñóúüåäöçîşûğăâđêôơưáàãảạ",
|
||||||
|
"eruocghjsueuyeiuaeiulkngoueiacelnszzostcdllnrstyzeinouuaaocisugaadeoouaaaaa",
|
||||||
|
true,
|
||||||
|
)
|
||||||
|
|
||||||
|
// Vietnamese
|
||||||
|
appendTransliterate(
|
||||||
|
"áạàảãăắặằẳẵâấậầẩẫéẹèẻẽêếệềểễiíịìỉĩoóọòỏõôốộồổỗơớợờởỡúụùủũưứựừửữyýỵỳỷỹđ",
|
||||||
|
"aaaaaaaaaaaaaaaaaeeeeeeeeeeeiiiiiioooooooooooooooooouuuuuuuuuuuyyyyyyd",
|
||||||
|
true,
|
||||||
|
)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func appendTransliterate(base, norm string, uppercase bool) {
|
||||||
|
normRunes := []rune(norm)
|
||||||
|
baseRunes := []rune(base)
|
||||||
|
|
||||||
|
lenNorm := len(normRunes)
|
||||||
|
lenBase := len(baseRunes)
|
||||||
|
if lenNorm != lenBase {
|
||||||
|
panic("Base and normalized strings have differend lenght: base=" + strconv.Itoa(lenBase) + ", norm=" + strconv.Itoa(lenNorm)) // programmer error in constant lenght
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, baseRune := range baseRunes {
|
||||||
|
normMap[baseRune] = normRunes[i]
|
||||||
|
}
|
||||||
|
|
||||||
|
if uppercase {
|
||||||
|
upperBase := strings.ToUpper(base)
|
||||||
|
upperNorm := strings.ToUpper(norm)
|
||||||
|
normRune := []rune(upperNorm)
|
||||||
|
for i, baseRune := range []rune(upperBase) {
|
||||||
|
normMap[baseRune] = normRune[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove diacritics and make lowercase.
|
||||||
|
func removeDiacritics(baseString string) string {
|
||||||
|
var normalizedRunes []rune
|
||||||
|
for _, baseRune := range []rune(baseString) {
|
||||||
|
if normRune, ok := normMap[baseRune]; ok {
|
||||||
|
normalizedRunes = append(normalizedRunes, normRune)
|
||||||
|
} else {
|
||||||
|
normalizedRunes = append(normalizedRunes, baseRune)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return string(normalizedRunes)
|
||||||
|
}
|
||||||
@@ -0,0 +1,72 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strconv"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
// typical czech test sentence ;-)
|
||||||
|
const baseTestString = "Příliš žluťoučký kůň příšerně úpěl ďábelské ódy"
|
||||||
|
|
||||||
|
func TestRemoveDiacritics(t *testing.T) {
|
||||||
|
testStr := baseTestString
|
||||||
|
expStr := "Prilis zlutoucky kun priserne upel dabelske ody"
|
||||||
|
checkRemoveDiacritics(testStr, expStr, t)
|
||||||
|
|
||||||
|
// other accents (non comlete, but all I founded)
|
||||||
|
testStr = "áéíóúýčďěňřšťžůåøĉĝĥĵŝŭšžõäöüàâçéèêëîïôùûüÿžščćđáéíóúąęėįųūčšžāēīūčšžļķņģáéíóúöüőűäöüëïąćęłńóśźżáàãâçéêíóõôăâîșțáäčďéíĺľňóôŕšťúýžáéíñóúüåäöâçîşûğăâđêôơưáàãảạ"
|
||||||
|
expStr = "aeiouycdenrstzuaocghjsuszoaouaaceeeeiiouuuyzsccdaeiouaeeiuucszaeiucszlkngaeiouououaoueiacelnoszzaaaaceeioooaaistaacdeillnoorstuyzaeinouuaaoacisugaadeoouaaaaa"
|
||||||
|
checkRemoveDiacritics(testStr, expStr, t)
|
||||||
|
|
||||||
|
testStr = "ÁÉÍÓÚÝČĎĚŇŘŠŤŽŮÅØĈĜĤĴŜŬŠŽÕÄÖÜÀÂÇÉÈÊËÎÏÔÙÛÜŸŽŠČĆĐÁÉÍÓÚĄĘĖĮŲŪČŠŽĀĒĪŪČŠŽĻĶŅĢÁÉÍÓÚÖÜŐŰÄÖÜËÏĄĆĘŁŃÓŚŹŻÁÀÃÂÇÉÊÍÓÕÔĂÂÎȘȚÁÄČĎÉÍĹĽŇÓÔŔŠŤÚÝŽÁÉÍÑÓÚÜÅÄÖÂÇÎŞÛĞĂÂĐÊÔƠƯÁÀÃẢẠ"
|
||||||
|
expStr = "AEIOUYCDENRSTZUAOCGHJSUSZOAOUAACEEEEIIOUUUYZSCCDAEIOUAEEIUUCSZAEIUCSZLKNGAEIOUOUOUAOUEIACELNOSZZAAAACEEIOOOAAISTAACDEILLNOORSTUYZAEINOUUAAOACISUGAADEOOUAAAAA"
|
||||||
|
checkRemoveDiacritics(testStr, expStr, t)
|
||||||
|
|
||||||
|
testStr = "áạàảãăắặằẳẵâấậầẩẫéẹèẻẽêếệềểễiíịìỉĩoóọòỏõôốộồổỗơớợờởỡúụùủũưứựừửữyýỵỳỷỹđ"
|
||||||
|
expStr = "aaaaaaaaaaaaaaaaaeeeeeeeeeeeiiiiiioooooooooooooooooouuuuuuuuuuuyyyyyyd"
|
||||||
|
checkRemoveDiacritics(testStr, expStr, t)
|
||||||
|
|
||||||
|
testStr = "ÁẠÀẢÃĂẮẶẰẲẴÂẤẬẦẨẪÉẸÈẺẼÊẾỆỀỂỄÍỊÌỈĨÓỌÒỎÕÔỐỘỒỔỖƠỚỢỜỞỠÚỤÙỦŨƯỨỰỪỬỮÝỴỲỶỸĐ"
|
||||||
|
expStr = "AAAAAAAAAAAAAAAAAEEEEEEEEEEEIIIIIOOOOOOOOOOOOOOOOOUUUUUUUUUUUYYYYYD"
|
||||||
|
checkRemoveDiacritics(testStr, expStr, t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkRemoveDiacritics(testStr string, expStr string, t *testing.T) {
|
||||||
|
resultStr := removeDiacritics(testStr)
|
||||||
|
if resultStr != expStr {
|
||||||
|
t.Errorf("at input '%v' expected '%v' but got '%v'", testStr, expStr, resultStr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSearchSettings(t *testing.T) {
|
||||||
|
runSearch(t, true, false, true, true, "Veřejný", "vere", true)
|
||||||
|
|
||||||
|
runSearch(t, true, false, true, false, baseTestString, "Zlutoucky", true)
|
||||||
|
runSearch(t, true, false, true, false, baseTestString, "zlutoucky", true)
|
||||||
|
runSearch(t, true, true, true, false, baseTestString, "Zlutoucky", false)
|
||||||
|
runSearch(t, true, true, true, true, baseTestString, "zlutoucky", true)
|
||||||
|
|
||||||
|
runSearch(t, false, false, true, false, baseTestString, "žlutoucky", true)
|
||||||
|
runSearch(t, false, false, true, false, baseTestString, "Žlutoucky", false)
|
||||||
|
|
||||||
|
runSearch(t, false, false, true, true, baseTestString, "žluťoučký", true)
|
||||||
|
runSearch(t, false, false, true, false, baseTestString, "žluťoučký", true)
|
||||||
|
runSearch(t, false, false, false, false, baseTestString, "žluťoučký", true)
|
||||||
|
runSearch(t, false, false, false, false, baseTestString, "zlutoucky", false)
|
||||||
|
runSearch(t, false, false, true, true, baseTestString, "zlutoucky", true)
|
||||||
|
}
|
||||||
|
|
||||||
|
func runSearch(t *testing.T, ignorecase, smartcase, ignorediacritics, smartdiacritics bool, base, pattern string, expected bool) {
|
||||||
|
gOpts.ignorecase = ignorecase
|
||||||
|
gOpts.smartcase = smartcase
|
||||||
|
gOpts.ignoredia = ignorediacritics
|
||||||
|
gOpts.smartdia = smartdiacritics
|
||||||
|
matched, _ := searchMatch(base, pattern)
|
||||||
|
if matched != expected {
|
||||||
|
t.Errorf("False search for" +
|
||||||
|
" ignorecase = " + strconv.FormatBool(gOpts.ignorecase) + ", " +
|
||||||
|
" smartcase = " + strconv.FormatBool(gOpts.smartcase) + ", " +
|
||||||
|
" ignoredia = " + strconv.FormatBool(gOpts.ignoredia) + ", " +
|
||||||
|
" smartdia = " + strconv.FormatBool(gOpts.smartdia) + ", ")
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user