add 'setlocal' for directory specific options (#1381)

* add 'setlocal' for option 'sortby'

* save each local option separately

* add 'setlocal' for option 'reverse'

* add 'setlocal' for option 'dirfirst'

* add 'setlocal' for option 'hidden'

* add 'setlocal' for option 'dironly'

* add 'setlocal' for option 'info'

* add boolean toggling for 'setlocal' options

* check if the path is absolute for 'setlocal'

* add 'setlocal' options to completion

* add tests for 'setlocal'

* add 'setlocal' to the documentation

* use trailing '/' in 'setlocal' for subdir matching

* cleanup

* add 'setlocal' subdirs to the documentation
This commit is contained in:
Gökçehan Kara authored and GitHub committed 2023-09-02 22:22:56 +03:00
1 parent 990cea7e04
commit 8d013c9375
10 files changed
+486 -31

No files matched your search

+22 -1
View File
@@ -11,6 +11,7 @@ import (
var (
gCmdWords = []string{
"set",
"setlocal",
"map",
"maps",
"cmap",
@@ -224,6 +225,23 @@ var (
"truncatechar",
"truncatepct",
}
gLocalOptWords = []string{
"dirfirst",
"nodirfirst",
"dirfirst!",
"dironly",
"nodironly",
"dironly!",
"hidden",
"nohidden",
"hidden!",
"info",
"reverse",
"noreverse",
"reverse!",
"sortby",
}
)
func matchLongest(s1, s2 []rune) []rune {
@@ -410,6 +428,9 @@ func completeCmd(acc []rune) (matches []string, longestAcc []rune) {
}
case 3:
switch f[0] {
case "setlocal":
matches, longest = matchWord(f[2], gLocalOptWords)
longestAcc = append(acc[:len(acc)-len([]rune(f[len(f)-1]))], longest...)
case "map", "cmap":
matches, longest = matchCmd(f[2])
longestAcc = append(acc[:len(acc)-len([]rune(f[len(f)-1]))], longest...)
@@ -419,7 +440,7 @@ func completeCmd(acc []rune) (matches []string, longestAcc []rune) {
}
default:
switch f[0] {
case "set", "map", "cmap", "cmd":
case "set", "setlocal", "map", "cmap", "cmd":
longestAcc = acc
default:
matches, longest = matchFile(f[len(f)-1])
+16 -1
View File
@@ -1125,7 +1125,7 @@ Characters from '#' to newline are comments and ignored:
# comments start with '#'
There are four special commands ('set', 'map', 'cmap', and 'cmd') for configuration.
There are five special commands ('set', 'setlocal', 'map', 'cmap', and 'cmd') for configuration.
Command 'set' is used to set an option which can be boolean, integer, or string:
@@ -1139,6 +1139,21 @@ Command 'set' is used to set an option which can be boolean, integer, or string:
set sortby 'time' # string value with single quotes (whitespaces)
set sortby "time" # string value with double quotes (backslash escapes)
Command 'setlocal' is used to set a local option for a directory which can be boolean or string.
Currently supported local options are 'dirfirst', 'dironly', 'hidden', 'info', 'reverse', and 'sortby'.
Adding a trailing path separator (i.e. '/' for Unix and '\' for Windows) sets the option for the given directory along with its subdirectories:
setlocal /foo/bar hidden # boolean enable
setlocal /foo/bar hidden true # boolean enable
setlocal /foo/bar nohidden # boolean disable
setlocal /foo/bar hidden false # boolean disable
setlocal /foo/bar hidden! # boolean toggle
setlocal /foo/bar sortby time # string value w/o quotes
setlocal /foo/bar sortby 'time' # string value with single quotes (whitespaces)
setlocal /foo/bar sortby "time" # string value with double quotes (backslash escapes)
setlocal /foo/bar hidden # for only '/foo/bar' directory
setlocal /foo/bar/ hidden # for '/foo/bar' and its subdirectories (e.g. '/foo/bar/baz')
Command 'map' is used to bind a key to a command which can be builtin command, custom command, or shell command:
map gh cd ~ # builtin command
+19 -2
View File
@@ -1211,8 +1211,8 @@ Characters from '#' to newline are comments and ignored:
# comments start with '#'
There are four special commands ('set', 'map', 'cmap', and 'cmd') for
configuration.
There are five special commands ('set', 'setlocal', 'map', 'cmap', and 'cmd')
for configuration.
Command 'set' is used to set an option which can be boolean, integer, or string:
@@ -1226,6 +1226,23 @@ Command 'set' is used to set an option which can be boolean, integer, or string:
set sortby 'time' # string value with single quotes (whitespaces)
set sortby "time" # string value with double quotes (backslash escapes)
Command 'setlocal' is used to set a local option for a directory which can be
boolean or string. Currently supported local options are 'dirfirst', 'dironly',
'hidden', 'info', 'reverse', and 'sortby'. Adding a trailing path separator
(i.e. '/' for Unix and '\' for Windows) sets the option for the given directory
along with its subdirectories:
setlocal /foo/bar hidden # boolean enable
setlocal /foo/bar hidden true # boolean enable
setlocal /foo/bar nohidden # boolean disable
setlocal /foo/bar hidden false # boolean disable
setlocal /foo/bar hidden! # boolean toggle
setlocal /foo/bar sortby time # string value w/o quotes
setlocal /foo/bar sortby 'time' # string value with single quotes (whitespaces)
setlocal /foo/bar sortby "time" # string value with double quotes (backslash escapes)
setlocal /foo/bar hidden # for only '/foo/bar' directory
setlocal /foo/bar/ hidden # for '/foo/bar' and its subdirectories (e.g. '/foo/bar/baz')
Command 'map' is used to bind a key to a command which can be builtin command,
custom command, or shell command:
+179
View File
@@ -924,6 +924,185 @@ func (e *setExpr) eval(app *app, args []string) {
app.ui.loadFileInfo(app.nav)
}
func (e *setLocalExpr) eval(app *app, args []string) {
path := replaceTilde(e.path)
if !filepath.IsAbs(path) {
app.ui.echoerr("setlocal: path should be absolute")
return
}
switch e.opt {
case "dirfirst":
if e.val == "" || e.val == "true" {
gLocalOpts.dirfirsts[path] = true
} else if e.val == "false" {
gLocalOpts.dirfirsts[path] = false
} else {
app.ui.echoerr("dirfirst: value should be empty, 'true', or 'false'")
return
}
app.nav.sort()
app.ui.sort()
case "nodirfirst":
if e.val != "" {
app.ui.echoerrf("nodirfirst: unexpected value: %s", e.val)
return
}
gLocalOpts.dirfirsts[path] = false
app.nav.sort()
app.ui.sort()
case "dirfirst!":
if e.val != "" {
app.ui.echoerrf("dirfirst!: unexpected value: %s", e.val)
return
}
if val, ok := gLocalOpts.dirfirsts[path]; ok {
gLocalOpts.dirfirsts[path] = !val
} else {
val = gOpts.sortType.option&dirfirstSort != 0
gLocalOpts.dirfirsts[path] = !val
}
app.nav.sort()
app.ui.sort()
case "dironly":
if e.val == "" || e.val == "true" {
gLocalOpts.dironlys[path] = true
} else if e.val == "false" {
gLocalOpts.dironlys[path] = false
} else {
app.ui.echoerr("dironly: value should be empty, 'true', or 'false'")
return
}
app.nav.sort()
app.ui.sort()
case "nodironly":
if e.val != "" {
app.ui.echoerrf("nodironly: unexpected value: %s", e.val)
return
}
gLocalOpts.dironlys[path] = false
app.nav.sort()
app.ui.sort()
case "dironly!":
if e.val != "" {
app.ui.echoerrf("dironly!: unexpected value: %s", e.val)
return
}
if val, ok := gLocalOpts.dironlys[path]; ok {
gLocalOpts.dironlys[path] = !val
} else {
gLocalOpts.dironlys[path] = !gOpts.dironly
}
app.nav.sort()
app.ui.sort()
case "hidden":
if e.val == "" || e.val == "true" {
gLocalOpts.hiddens[path] = true
} else if e.val == "false" {
gLocalOpts.hiddens[path] = false
} else {
app.ui.echoerr("hidden: value should be empty, 'true', or 'false'")
return
}
app.nav.sort()
app.ui.sort()
case "nohidden":
if e.val != "" {
app.ui.echoerrf("nohidden: unexpected value: %s", e.val)
return
}
gLocalOpts.hiddens[path] = false
app.nav.sort()
app.ui.sort()
case "hidden!":
if e.val != "" {
app.ui.echoerrf("hidden!: unexpected value: %s", e.val)
return
}
if val, ok := gLocalOpts.hiddens[path]; ok {
gLocalOpts.hiddens[path] = !val
} else {
val = gOpts.sortType.option&hiddenSort != 0
gLocalOpts.hiddens[path] = !val
}
app.nav.sort()
app.ui.sort()
case "info":
if e.val == "" {
gLocalOpts.infos[path] = nil
return
}
toks := strings.Split(e.val, ":")
for _, s := range toks {
switch s {
case "size", "time", "atime", "ctime":
default:
app.ui.echoerr("info: should consist of 'size', 'time', 'atime' or 'ctime' separated with colon")
return
}
}
gLocalOpts.infos[path] = toks
case "reverse":
if e.val == "" || e.val == "true" {
gLocalOpts.reverses[path] = true
} else if e.val == "false" {
gLocalOpts.reverses[path] = false
} else {
app.ui.echoerr("reverse: value should be empty, 'true', or 'false'")
return
}
app.nav.sort()
app.ui.sort()
case "noreverse":
if e.val != "" {
app.ui.echoerrf("noreverse: unexpected value: %s", e.val)
return
}
gLocalOpts.reverses[path] = false
app.nav.sort()
app.ui.sort()
case "reverse!":
if e.val != "" {
app.ui.echoerrf("reverse!: unexpected value: %s", e.val)
return
}
if val, ok := gLocalOpts.reverses[path]; ok {
gLocalOpts.reverses[path] = !val
} else {
val = gOpts.sortType.option&reverseSort != 0
gLocalOpts.reverses[path] = !val
}
app.nav.sort()
app.ui.sort()
case "sortby":
switch e.val {
case "natural":
gLocalOpts.sortMethods[path] = naturalSort
case "name":
gLocalOpts.sortMethods[path] = nameSort
case "size":
gLocalOpts.sortMethods[path] = sizeSort
case "time":
gLocalOpts.sortMethods[path] = timeSort
case "ctime":
gLocalOpts.sortMethods[path] = ctimeSort
case "atime":
gLocalOpts.sortMethods[path] = atimeSort
case "ext":
gLocalOpts.sortMethods[path] = extSort
default:
app.ui.echoerr("sortby: value should either be 'natural', 'name', 'size', 'time', 'atime', 'ctime' or 'ext'")
return
}
app.nav.sort()
app.ui.sort()
default:
app.ui.echoerrf("unknown option: %s", e.opt)
return
}
app.ui.loadFileInfo(app.nav)
}
func (e *mapExpr) eval(app *app, args []string) {
if e.expr == nil {
delete(gOpts.keys, e.keys)
+78
View File
@@ -191,6 +191,84 @@ var gEvalTests = []struct {
[]expr{&setExpr{"ratios", "1:2:3"}, &setExpr{"hidden", ""}},
},
{
"setlocal /foo/bar hidden # trailing comments are allowed",
[]string{"setlocal", "/foo/bar", "hidden", "\n"},
[]expr{&setLocalExpr{"/foo/bar", "hidden", ""}},
},
{
"setlocal /foo/bar hidden; setlocal /foo/bar reverse",
[]string{"setlocal", "/foo/bar", "hidden", ";", "setlocal", "/foo/bar", "reverse", "\n"},
[]expr{&setLocalExpr{"/foo/bar", "hidden", ""}, &setLocalExpr{"/foo/bar", "reverse", ""}},
},
{
"setlocal /foo/bar hidden\nsetlocal /foo/bar reverse",
[]string{"setlocal", "/foo/bar", "hidden", "\n", "setlocal", "/foo/bar", "reverse", "\n"},
[]expr{&setLocalExpr{"/foo/bar", "hidden", ""}, &setLocalExpr{"/foo/bar", "reverse", ""}},
},
{
`setlocal /foo/bar info ""`,
[]string{"setlocal", "/foo/bar", "info", "", "\n"},
[]expr{&setLocalExpr{"/foo/bar", "info", ""}},
},
{
`setlocal /foo/bar info "size"`,
[]string{"setlocal", "/foo/bar", "info", "size", "\n"},
[]expr{&setLocalExpr{"/foo/bar", "info", "size"}},
},
{
"setlocal /foo/bar info size:time",
[]string{"setlocal", "/foo/bar", "info", "size:time", "\n"},
[]expr{&setLocalExpr{"/foo/bar", "info", "size:time"}},
},
{
"setlocal /foo/bar info size:time;",
[]string{"setlocal", "/foo/bar", "info", "size:time", ";"},
[]expr{&setLocalExpr{"/foo/bar", "info", "size:time"}},
},
{
":setlocal /foo/bar info size:time",
[]string{":", "setlocal", "/foo/bar", "info", "size:time", "\n", "\n"},
[]expr{&listExpr{[]expr{&setLocalExpr{"/foo/bar", "info", "size:time"}}, 1}},
},
{
":setlocal /foo/bar info size:time\nsetlocal /foo/bar hidden",
[]string{":", "setlocal", "/foo/bar", "info", "size:time", "\n", "\n", "setlocal", "/foo/bar", "hidden", "\n"},
[]expr{&listExpr{[]expr{&setLocalExpr{"/foo/bar", "info", "size:time"}}, 1}, &setLocalExpr{"/foo/bar", "hidden", ""}},
},
{
":setlocal /foo/bar info size:time;",
[]string{":", "setlocal", "/foo/bar", "info", "size:time", ";", "\n"},
[]expr{&listExpr{[]expr{&setLocalExpr{"/foo/bar", "info", "size:time"}}, 1}},
},
{
":setlocal /foo/bar info size:time;\nsetlocal /foo/bar hidden",
[]string{":", "setlocal", "/foo/bar", "info", "size:time", ";", "\n", "setlocal", "/foo/bar", "hidden", "\n"},
[]expr{&listExpr{[]expr{&setLocalExpr{"/foo/bar", "info", "size:time"}}, 1}, &setLocalExpr{"/foo/bar", "hidden", ""}},
},
{
"setlocal /foo/bar info size:time\n setlocal /foo/bar hidden",
[]string{"setlocal", "/foo/bar", "info", "size:time", "\n", "setlocal", "/foo/bar", "hidden", "\n"},
[]expr{&setLocalExpr{"/foo/bar", "info", "size:time"}, &setLocalExpr{"/foo/bar", "hidden", ""}},
},
{
"setlocal /foo/bar info size:time \nsetlocal /foo/bar hidden",
[]string{"setlocal", "/foo/bar", "info", "size:time", "\n", "setlocal", "/foo/bar", "hidden", "\n"},
[]expr{&setLocalExpr{"/foo/bar", "info", "size:time"}, &setLocalExpr{"/foo/bar", "hidden", ""}},
},
{
"map gh cd ~",
[]string{"map", "gh", "cd", "~", "\n"},
+16 -1
View File
@@ -1349,7 +1349,7 @@ Characters from '#' to newline are comments and ignored:
# comments start with '#'
.EE
.PP
There are four special commands ('set', 'map', 'cmap', and 'cmd') for configuration.
There are five special commands ('set', 'setlocal', 'map', 'cmap', and 'cmd') for configuration.
.PP
Command 'set' is used to set an option which can be boolean, integer, or string:
.PP
@@ -1365,6 +1365,21 @@ Command 'set' is used to set an option which can be boolean, integer, or string:
set sortby "time" # string value with double quotes (backslash escapes)
.EE
.PP
Command 'setlocal' is used to set a local option for a directory which can be boolean or string. Currently supported local options are 'dirfirst', 'dironly', 'hidden', 'info', 'reverse', and 'sortby'. Adding a trailing path separator (i.e. '/' for Unix and '\e' for Windows) sets the option for the given directory along with its subdirectories:
.PP
.EX
setlocal /foo/bar hidden # boolean enable
setlocal /foo/bar hidden true # boolean enable
setlocal /foo/bar nohidden # boolean disable
setlocal /foo/bar hidden false # boolean disable
setlocal /foo/bar hidden! # boolean toggle
setlocal /foo/bar sortby time # string value w/o quotes
setlocal /foo/bar sortby 'time' # string value with single quotes (whitespaces)
setlocal /foo/bar sortby "time" # string value with double quotes (backslash escapes)
setlocal /foo/bar hidden # for only '/foo/bar' directory
setlocal /foo/bar/ hidden # for '/foo/bar' and its subdirectories (e.g. '/foo/bar/baz')
.EE
.PP
Command 'map' is used to bind a key to a command which can be builtin command, custom command, or shell command:
.PP
.EX
+5 -5
View File
@@ -210,8 +210,8 @@ func normalize(s1, s2 string, ignorecase, ignoredia bool) (string, string) {
}
func (dir *dir) sort() {
dir.sortType = gOpts.sortType
dir.dironly = gOpts.dironly
dir.sortType = getSortType(dir.path)
dir.dironly = getDirOnly(dir.path)
dir.hiddenfiles = gOpts.hiddenfiles
dir.ignorecase = gOpts.ignorecase
dir.ignoredia = gOpts.ignoredia
@@ -472,7 +472,7 @@ func (nav *nav) loadDirInternal(path string) *dir {
loading: true,
loadTime: time.Now(),
path: path,
sortType: gOpts.sortType,
sortType: getSortType(path),
hiddenfiles: gOpts.hiddenfiles,
ignorecase: gOpts.ignorecase,
ignoredia: gOpts.ignoredia,
@@ -535,8 +535,8 @@ func (nav *nav) checkDir(dir *dir) {
}
nav.dirChan <- nd
}()
case dir.sortType != gOpts.sortType ||
dir.dironly != gOpts.dironly ||
case dir.sortType != getSortType(dir.path) ||
dir.dironly != getDirOnly(dir.path) ||
!reflect.DeepEqual(dir.hiddenfiles, gOpts.hiddenfiles) ||
dir.ignorecase != gOpts.ignorecase ||
dir.ignoredia != gOpts.ignoredia:
+96 -1
View File
@@ -1,6 +1,9 @@
package main
import "time"
import (
"path/filepath"
"time"
)
type sortMethod byte
@@ -92,6 +95,91 @@ var gOpts struct {
tagfmt string
}
var gLocalOpts struct {
sortMethods map[string]sortMethod
dirfirsts map[string]bool
dironlys map[string]bool
hiddens map[string]bool
reverses map[string]bool
infos map[string][]string
}
func localOptPaths(path string) []string {
var list []string
list = append(list, path)
for curr := path; !isRoot(curr); curr = filepath.Dir(curr) {
list = append(list, curr+string(filepath.Separator))
}
return list
}
func getSortMethod(path string) sortMethod {
for _, key := range localOptPaths(path) {
if val, ok := gLocalOpts.sortMethods[key]; ok {
return val
}
}
return gOpts.sortType.method
}
func getSortType(path string) sortType {
method := getSortMethod(path)
option := gOpts.sortType.option
for _, key := range localOptPaths(path) {
if val, ok := gLocalOpts.dirfirsts[key]; ok {
if val {
option |= dirfirstSort
} else {
option &= ^dirfirstSort
}
break
}
}
for _, key := range localOptPaths(path) {
if val, ok := gLocalOpts.hiddens[key]; ok {
if val {
option |= hiddenSort
} else {
option &= ^hiddenSort
}
break
}
}
for _, key := range localOptPaths(path) {
if val, ok := gLocalOpts.reverses[key]; ok {
if val {
option |= reverseSort
} else {
option &= ^reverseSort
}
break
}
}
val := sortType{
method: method,
option: option,
}
return val
}
func getDirOnly(path string) bool {
for _, key := range localOptPaths(path) {
if val, ok := gLocalOpts.dironlys[key]; ok {
return val
}
}
return gOpts.dironly
}
func getInfo(path string) []string {
for _, key := range localOptPaths(path) {
if val, ok := gLocalOpts.infos[key]; ok {
return val
}
}
return gOpts.info
}
func init() {
gOpts.anchorfind = true
gOpts.autoquit = false
@@ -268,5 +356,12 @@ func init() {
gOpts.cmds = make(map[string]expr)
gOpts.user = make(map[string]string)
gLocalOpts.sortMethods = make(map[string]sortMethod)
gLocalOpts.dirfirsts = make(map[string]bool)
gLocalOpts.dironlys = make(map[string]bool)
gLocalOpts.hiddens = make(map[string]bool)
gLocalOpts.reverses = make(map[string]bool)
gLocalOpts.infos = make(map[string][]string)
setDefaults()
}
+54 -19
View File
@@ -2,34 +2,37 @@ package main
// Grammar of the language used in the evaluator
//
// Expr = SetExpr
// | MapExpr
// | CMapExpr
// | CmdExpr
// | CallExpr
// | ExecExpr
// | ListExpr
// Expr = SetExpr
// | SetLocalExpr
// | MapExpr
// | CMapExpr
// | CmdExpr
// | CallExpr
// | ExecExpr
// | ListExpr
//
// SetExpr = 'set' <opt> <val> ';'
// SetExpr = 'set' <opt> <val> ';'
//
// MapExpr = 'map' <keys> Expr
// SetLocalExpr = 'setlocal' <dir> <opt> <val> ';'
//
// CMapExpr = 'cmap' <key> Expr
// MapExpr = 'map' <keys> Expr
//
// CmdExpr = 'cmd' <name> Expr
// CMapExpr = 'cmap' <key> Expr
//
// CallExpr = <name> <args> ';'
// CmdExpr = 'cmd' <name> Expr
//
// ExecExpr = Prefix <value> '\n'
// | Prefix '{{' <value> '}}' ';'
// CallExpr = <name> <args> ';'
//
// Prefix = '$' | '%' | '!' | '&'
// ExecExpr = Prefix <value> '\n'
// | Prefix '{{' <value> '}}' ';'
//
// ListExpr = ':' Expr ListRest '\n'
// | ':' '{{' Expr ListRest '}}' ';'
// Prefix = '$' | '%' | '!' | '&'
//
// ListRest = Nil
// | Expr ListExpr
// ListExpr = ':' Expr ListRest '\n'
// | ':' '{{' Expr ListRest '}}' ';'
//
// ListRest = Nil
// | Expr ListExpr
import (
"bytes"
@@ -50,6 +53,14 @@ type setExpr struct {
func (e *setExpr) String() string { return fmt.Sprintf("set %s %s", e.opt, e.val) }
type setLocalExpr struct {
path string
opt string
val string
}
func (e *setLocalExpr) String() string { return fmt.Sprintf("setlocal %s %s %s", e.path, e.opt, e.val) }
type mapExpr struct {
keys string
expr expr
@@ -176,6 +187,30 @@ func (p *parser) parseExpr() expr {
s.scan()
result = &setExpr{opt, val}
case "setlocal":
var val string
s.scan()
if s.typ != tokenIdent {
p.err = fmt.Errorf("expected directory: %s", s.tok)
}
dir := s.tok
s.scan()
if s.typ != tokenIdent {
p.err = fmt.Errorf("expected identifier: %s", s.tok)
}
opt := s.tok
s.scan()
if s.typ != tokenSemicolon {
val = s.tok
s.scan()
}
s.scan()
result = &setLocalExpr{dir, opt, val}
case "map":
var expr expr
+1 -1
View File
@@ -290,7 +290,7 @@ func infotimefmt(t time.Time) string {
func fileInfo(f *file, d *dir) string {
var info string
for _, s := range gOpts.info {
for _, s := range getInfo(d.path) {
switch s {
case "size":
if !(f.IsDir() && gOpts.dircounts) {