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