github.com/markusbkk/elvish@v0.0.0-20231204143114-91dc52438621/pkg/edit/ns_helper.go (about) 1 package edit 2 3 import ( 4 "os" 5 "strings" 6 7 "github.com/markusbkk/elvish/pkg/eval" 8 "github.com/markusbkk/elvish/pkg/fsutil" 9 ) 10 11 // Calls the passed function for each variable name in namespace ns that can be 12 // found from the top context. 13 func eachVariableInTop(builtin, global *eval.Ns, ns string, f func(s string)) { 14 switch ns { 15 case "", ":": 16 global.IterateKeysString(f) 17 builtin.IterateKeysString(f) 18 case "e:": 19 fsutil.EachExternal(func(cmd string) { 20 f(cmd + eval.FnSuffix) 21 }) 22 case "E:": 23 for _, s := range os.Environ() { 24 if i := strings.IndexByte(s, '='); i > 0 { 25 f(s[:i]) 26 } 27 } 28 default: 29 segs := eval.SplitQNameSegs(ns) 30 mod := global.IndexString(segs[0]) 31 if mod == nil { 32 mod = builtin.IndexString(segs[0]) 33 } 34 for _, seg := range segs[1:] { 35 if mod == nil { 36 return 37 } 38 mod = mod.Get().(*eval.Ns).IndexString(seg) 39 } 40 if mod != nil { 41 mod.Get().(*eval.Ns).IterateKeysString(f) 42 } 43 } 44 } 45 46 // Calls the passed function for each namespace that can be used from the top 47 // context. 48 func eachNsInTop(builtin, global *eval.Ns, f func(s string)) { 49 f("e:") 50 f("E:") 51 52 global.IterateKeysString(func(name string) { 53 if strings.HasSuffix(name, eval.NsSuffix) { 54 f(name) 55 } 56 }) 57 58 builtin.IterateKeysString(func(name string) { 59 if strings.HasSuffix(name, eval.NsSuffix) { 60 f(name) 61 } 62 }) 63 }