@@ -62,26 +62,25 @@ func getLineType(parts []string, c context) lineType {
6262 return skipLine
6363}
6464
65- /* TODO:
66- See i3 documentation for group1,group2,etc.
67- type Group string
68- */
69-
70- //ParseFromRunning loads config from the running i3 instance
71- func ParseFromRunning (wm string ) ([]Mode , []Binding , error ) {
65+ // ParseFromRunning loads config from the running i3 instance
66+ func ParseFromRunning (wm string , logError bool ) ([]Mode , []Variable , error ) {
7267 switch wm {
7368 case "i3" :
74- return parse (getConfigFromRunningi3 ())
69+ r , err := getConfigFromRunningi3 ()
70+ return parse (r , logError , err )
7571 case "sway" :
76- return parse (getConfigFromRunningSway ())
72+ r , err := getConfigFromRunningSway ()
73+ return parse (r , logError , err )
7774 default :
78- return parse (getAutoWM ())
75+ r , err := getAutoWM ()
76+ return parse (r , logError , err )
7977 }
8078}
8179
82- //ParseFromFile loads config from path
83- func ParseFromFile (path string ) ([]Mode , []Binding , error ) {
84- return parse (getConfigFromFile (path ))
80+ // ParseFromFile loads config from path
81+ func ParseFromFile (path string , logError bool ) ([]Mode , []Variable , error ) {
82+ r , err := getConfigFromFile (path )
83+ return parse (r , logError , err )
8584}
8685
8786func readLine (reader * bufio.Reader , c context , variables []Variable ) (string , []string , lineType , error ) {
@@ -136,15 +135,15 @@ func readLine(reader *bufio.Reader, c context, variables []Variable) (string, []
136135 return line , lineParts , lineType , err
137136}
138137
139- func parseConfig (confReader io.Reader , confPath string , variables []Variable , err error ) ([]Mode , [] Binding , []Variable , []string , error ) {
138+ func parseConfig (confReader io.Reader , confPath string , variables []Variable , logError bool , err error ) ([]Mode , []Variable , []string , error ) {
140139 if err != nil {
141- return []Mode {}, []Binding {}, [] Variable {}, []string {}, errors .New ("couldn't get the config file" )
140+ return []Mode {}, []Variable {}, []string {}, errors .New ("couldn't get the config file" )
142141 }
143142
144143 reader := bufio .NewReader (confReader )
145144
146- var modes []Mode
147- var bindings [] Binding
145+ modes := []Mode {{}}
146+
148147 var includes []helpers.Include
149148
150149 context := mainContext
@@ -156,7 +155,7 @@ func parseConfig(confReader io.Reader, confPath string, variables []Variable, er
156155 _ , lineParts , lineType , readErr = readLine (reader , context , variables )
157156
158157 if readErr != nil && readErr != io .EOF {
159- return []Mode {}, []Binding {}, [] Variable {}, []string {}, readErr
158+ return []Mode {}, []Variable {}, []string {}, readErr
160159 }
161160
162161 switch lineType {
@@ -208,42 +207,41 @@ func parseConfig(confReader io.Reader, confPath string, variables []Variable, er
208207 bindingLine := lineType == bindSymLine || lineType == bindCodeLine
209208
210209 binding , err := parseBinding (lineParts )
211- if err != nil {
210+ if err != nil && logError {
212211 log .Println (err )
213212 continue
214213 }
215214
216215 isMainContext := context == mainContext || context == bindCodeMainContext || context == bindSymMainContext
217216 if isMainContext && bindingLine {
218- bindings = append (bindings , binding )
217+ modes [ 0 ]. Bindings = append (modes [ 0 ]. Bindings , binding )
219218 }
220219
221220 isModeContext := context == modeContext || context == bindCodeModeContext || context == bindSymModeContext
222221 if isModeContext && bindingLine {
223- modes [len (modes )- 1 ].Bindings = append (modes [len (modes )- 1 ].Bindings ,
224- binding ,
225- )
222+ l := len (modes ) - 1
223+ modes [l ].Bindings = append (modes [l ].Bindings , binding )
226224 }
227225 }
228226
229227 var includePaths []string
230228 for _ , incl := range includes {
231229 matches , err := helpers .GetPaths (incl )
232- if err != nil {
230+ if err != nil && logError {
233231 log .Printf ("couldn't parse the following include \" %s\" got error %v" , incl , err )
234232 continue
235233 }
236234 includePaths = append (includePaths , matches ... )
237235 }
238236
239- return modes , bindings , variables , includePaths , nil
237+ return modes , variables , includePaths , nil
240238}
241239
242- func parse (confReader io.Reader , err error ) ([]Mode , []Binding , error ) {
240+ func parse (confReader io.Reader , logError bool , err error ) ([]Mode , []Variable , error ) {
243241 configPath , _ := helpers .GetSwayDefaultConfig ()
244- modes , bindings , variables , includes , err := parseConfig (confReader , configPath , []Variable {}, err )
242+ modes , variables , includes , err := parseConfig (confReader , configPath , []Variable {}, logError , err )
245243 if err != nil {
246- return []Mode {}, []Binding {}, errors .New ("couldn't get the config file" )
244+ return []Mode {}, []Variable {}, errors .New ("couldn't get the config file" )
247245 }
248246 var parsedIncludes []string
249247 for j := 0 ; j < len (includes ); j ++ {
@@ -258,26 +256,37 @@ func parse(confReader io.Reader, err error) ([]Mode, []Binding, error) {
258256 continue
259257 }
260258 f , ferr := os .Open (incl )
261- if err != nil {
259+ if err != nil && logError {
262260 log .Printf ("couldn't open the included file %s, got err: %v\n " , incl , ferr )
263261 }
264- m , b , v , i , perr := parseConfig (f , incl , variables , err )
265- if err != nil {
262+ m , v , i , perr := parseConfig (f , incl , variables , logError , err )
263+ if err != nil && logError {
266264 log .Printf ("couldn't parse the included file %s, got err: %v\n " , incl , perr )
267265 }
268- modes = append (modes , m ... )
269- bindings = append (bindings , b ... )
270- variables = v
266+ // add modes merging existing bindings
267+ for iNew := range m {
268+ found := false
269+ for iOld := range modes {
270+ if m [iNew ].Name == modes [iOld ].Name {
271+ found = true
272+ modes [iOld ].Bindings = append (modes [iOld ].Bindings , m [iNew ].Bindings ... ) // duplicates?
273+ break
274+ }
275+ }
276+ if ! found {
277+ modes = append (modes , m [iNew ])
278+ }
279+ }
280+ variables = v // NOTE: variables are updated in parseConfig
271281 includes = append (includes , i ... )
272282 parsedIncludes = append (parsedIncludes , incl )
273283 }
274284
275285 for key := range modes {
276286 modes [key ].Bindings = sortModifiers (modes [key ].Bindings )
277287 }
278- bindings = sortModifiers (bindings )
279288
280- return modes , bindings , nil
289+ return modes , variables , nil
281290}
282291
283292func parseMode (line string ) string {
0 commit comments