55 "errors"
66 "io"
77 "log"
8+ "os"
89 "sort"
910 "strings"
1011
@@ -38,6 +39,10 @@ func getLineType(parts []string, c context) lineType {
3839 if validateVariable (parts ) {
3940 return variableLine
4041 }
42+ case "include" :
43+ if validateInclude (parts ) {
44+ return includeLine
45+ }
4146 case "bindsym" :
4247 if validateBinding (parts ) && parts [len (parts )- 1 ] != "{" {
4348 return bindSymLine
@@ -103,16 +108,17 @@ func readLine(reader *bufio.Reader, c context) (string, []string, lineType, erro
103108 return line , lineParts , lineType , err
104109}
105110
106- func parse (confReader io.Reader , err error ) ([]Mode , []Binding , error ) {
111+ func parseConfig (confReader io.Reader , err error ) ([]Mode , []Binding , [] Variable , [] string , error ) {
107112 if err != nil {
108- return []Mode {}, []Binding {}, errors .New ("Couldn't get the config file" )
113+ return []Mode {}, []Binding {}, [] Variable {}, [] string {}, errors .New ("Couldn't get the config file" )
109114 }
110115
111116 reader := bufio .NewReader (confReader )
112117
113118 var modes []Mode
114119 var bindings []Binding
115120 var variables []Variable
121+ var includes []string
116122
117123 context := mainContext
118124 var readErr error
@@ -124,18 +130,23 @@ func parse(confReader io.Reader, err error) ([]Mode, []Binding, error) {
124130 line , lineParts , lineType , readErr = readLine (reader , context )
125131
126132 if readErr != nil && readErr != io .EOF {
127- return []Mode {}, []Binding {}, readErr
133+ return []Mode {}, []Binding {}, [] Variable {}, [] string {}, readErr
128134 }
129135
130136 switch lineType {
131137 case skipLine :
132138 continue
133139 case variableLine :
134140 variables = append (variables , parseVariable (lineParts ))
141+ continue
135142 case modeLine :
136143 context = modeContext
137144 name := parseMode (line )
138145 modes = append (modes , Mode {Name : name })
146+ continue
147+ case includeLine :
148+ includes = append (includes , strings .Join (lineParts [1 :], " " ))
149+ continue
139150 case bindCodeBracket :
140151 if context == mainContext {
141152 context = bindCodeMainContext
@@ -185,6 +196,50 @@ func parse(confReader io.Reader, err error) ([]Mode, []Binding, error) {
185196 }
186197 }
187198
199+ var includePaths []string
200+ for _ , incl := range includes {
201+ matches , err := helpers .GetPaths (incl )
202+ if err != nil {
203+ log .Printf ("couldn't parse the following include \" %s\" got error %v" , incl , err )
204+ continue
205+ }
206+ includePaths = append (includePaths , matches ... )
207+ }
208+
209+ return modes , bindings , variables , includePaths , nil
210+ }
211+
212+ func parse (confReader io.Reader , err error ) ([]Mode , []Binding , error ) {
213+ modes , bindings , variables , includes , err := parseConfig (confReader , err )
214+ if err != nil {
215+ return []Mode {}, []Binding {}, errors .New ("Couldn't get the config file" )
216+ }
217+ var parsedIncludes []string
218+ for _ , incl := range includes {
219+ done := false
220+ for _ , ap := range parsedIncludes {
221+ if ap == incl {
222+ done = true
223+ }
224+ }
225+ if done {
226+ continue
227+ }
228+ f , ferr := os .Open (incl )
229+ if err != nil {
230+ log .Printf ("couldn't open the included file %s, got err: %v\n " , incl , ferr )
231+ }
232+ m , b , v , i , perr := parseConfig (f , err )
233+ if err != nil {
234+ log .Printf ("couldn't parse the included file %s, got err: %v\n " , incl , perr )
235+ }
236+ modes = append (modes , m ... )
237+ bindings = append (bindings , b ... )
238+ variables = append (variables , v ... )
239+ includes = append (includes , i ... )
240+ parsedIncludes = append (parsedIncludes , incl )
241+ }
242+
188243 bindings , modes = replaceVariables (variables , bindings , modes )
189244
190245 for key := range modes {
0 commit comments