@@ -52,51 +52,87 @@ func InitColorscheme() error {
52
52
Colorscheme = make (map [string ]tcell.Style )
53
53
DefStyle = tcell .StyleDefault
54
54
55
- return LoadDefaultColorscheme ()
55
+ c , err := LoadDefaultColorscheme ()
56
+ if err == nil {
57
+ Colorscheme = c
58
+ }
59
+
60
+ return err
56
61
}
57
62
58
63
// LoadDefaultColorscheme loads the default colorscheme from $(ConfigDir)/colorschemes
59
- func LoadDefaultColorscheme () error {
60
- return LoadColorscheme (GlobalSettings ["colorscheme" ].(string ))
64
+ func LoadDefaultColorscheme () (map [string ]tcell.Style , error ) {
65
+ var parsedColorschemes []string
66
+ return LoadColorscheme (GlobalSettings ["colorscheme" ].(string ), & parsedColorschemes )
61
67
}
62
68
63
69
// LoadColorscheme loads the given colorscheme from a directory
64
- func LoadColorscheme (colorschemeName string ) error {
70
+ func LoadColorscheme (colorschemeName string , parsedColorschemes * []string ) (map [string ]tcell.Style , error ) {
71
+ c := make (map [string ]tcell.Style )
65
72
file := FindRuntimeFile (RTColorscheme , colorschemeName )
66
73
if file == nil {
67
- return errors .New (colorschemeName + " is not a valid colorscheme" )
74
+ return c , errors .New (colorschemeName + " is not a valid colorscheme" )
68
75
}
69
76
if data , err := file .Data (); err != nil {
70
- return errors .New ("Error loading colorscheme: " + err .Error ())
77
+ return c , errors .New ("Error loading colorscheme: " + err .Error ())
71
78
} else {
72
- Colorscheme , err = ParseColorscheme (string (data ))
79
+ var err error
80
+ c , err = ParseColorscheme (file .Name (), string (data ), parsedColorschemes )
73
81
if err != nil {
74
- return err
82
+ return c , err
75
83
}
76
84
}
77
- return nil
85
+ return c , nil
78
86
}
79
87
80
88
// ParseColorscheme parses the text definition for a colorscheme and returns the corresponding object
81
89
// Colorschemes are made up of color-link statements linking a color group to a list of colors
82
90
// For example, color-link keyword (blue,red) makes all keywords have a blue foreground and
83
91
// red background
84
- func ParseColorscheme (text string ) (map [string ]tcell.Style , error ) {
92
+ func ParseColorscheme (name string , text string , parsedColorschemes * [] string ) (map [string ]tcell.Style , error ) {
85
93
var err error
86
- parser := regexp .MustCompile (`color-link\s+(\S*)\s+"(.*)"` )
87
-
94
+ colorParser := regexp .MustCompile (`color-link\s+(\S*)\s+"(.*)"` )
95
+ includeParser := regexp . MustCompile ( `include\s+"(.*)"` )
88
96
lines := strings .Split (text , "\n " )
89
-
90
97
c := make (map [string ]tcell.Style )
91
98
99
+ if parsedColorschemes != nil {
100
+ * parsedColorschemes = append (* parsedColorschemes , name )
101
+ }
102
+
103
+ lineLoop:
92
104
for _ , line := range lines {
93
105
if strings .TrimSpace (line ) == "" ||
94
106
strings .TrimSpace (line )[0 ] == '#' {
95
107
// Ignore this line
96
108
continue
97
109
}
98
110
99
- matches := parser .FindSubmatch ([]byte (line ))
111
+ matches := includeParser .FindSubmatch ([]byte (line ))
112
+ if len (matches ) == 2 {
113
+ // support includes only in case parsedColorschemes are given
114
+ if parsedColorschemes != nil {
115
+ include := string (matches [1 ])
116
+ for _ , name := range * parsedColorschemes {
117
+ // check for circular includes...
118
+ if name == include {
119
+ // ...and prevent them
120
+ continue lineLoop
121
+ }
122
+ }
123
+ includeScheme , err := LoadColorscheme (include , parsedColorschemes )
124
+ if err != nil {
125
+ return c , err
126
+ } else {
127
+ for k , v := range includeScheme {
128
+ c [k ] = v
129
+ }
130
+ }
131
+ }
132
+ continue
133
+ }
134
+
135
+ matches = colorParser .FindSubmatch ([]byte (line ))
100
136
if len (matches ) == 3 {
101
137
link := string (matches [1 ])
102
138
colors := string (matches [2 ])
0 commit comments