summary refs log tree commit diff stats
path: root/compiler/lambdalifting.nim
blob: cd2ccfe53c994f19a364db60585e3a3cce28f736 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
#
#
#           The Nim Compiler
#        (c) Copyright 2015 Andreas Rumpf
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

# This include file implements lambda lifting for the transformator.

import
  intsets, strutils, options, ast, astalgo, trees, treetab, msgs, os,
  idents, renderer, types, magicsys, rodread, lowerings, tables

discard """
  The basic approach is that captured vars need to be put on the heap and
  that the calling chain needs to be explicitly modelled. Things to consider:

  proc a =
    var v = 0
    proc b =
      var w = 2

      for x in 0..3:
        proc c = capture v, w, x
        c()
    b()

    for x in 0..4:
      proc d = capture x
      d()

  Needs to be translated into:

  proc a =
    var cl: *
    new cl
    cl.v = 0

    proc b(cl) =
      var bcl: *
      new bcl
      bcl.w = 2
      bcl.up = cl

      for x in 0..3:
        var bcl2: *
        new bcl2
        bcl2.up = bcl
        bcl2.up2 = cl
        bcl2.x = x

        proc c(cl) = capture cl.up2.v, cl.up.w, cl.x
        c(bcl2)

      c(bcl)

    b(cl)

    for x in 0..4:
      var acl2: *
      new acl2
      acl2.x = x
      proc d(cl) = capture cl.x
      d(acl2)

  Closures as interfaces:

  proc outer: T =
    var captureMe: TObject # value type required for efficiency
    proc getter(): int = result = captureMe.x
    proc setter(x: int) = captureMe.x = x

    result = (getter, setter)

  Is translated to:

  proc outer: T =
    var cl: *
    new cl

    proc getter(cl): int = result = cl.captureMe.x
    proc setter(cl: *, x: int) = cl.captureMe.x = x

    result = ((cl, getter), (cl, setter))


  For 'byref' capture, the outer proc needs to access the captured var through
  the indirection too. For 'bycopy' capture, the outer proc accesses the var
  not through the indirection.

  Possible optimizations:

  1) If the closure contains a single 'ref' and this
  reference is not re-assigned (check ``sfAddrTaken`` flag) make this the
  closure. This is an important optimization if closures are used as
  interfaces.
  2) If the closure does not escape, put it onto the stack, not on the heap.
  3) Dataflow analysis would help to eliminate the 'up' indirections.
  4) If the captured var is not actually used in the outer proc (common?),
  put it into an inner proc.

"""

# Important things to keep in mind:
# * Don't base the analysis on nkProcDef et al. This doesn't work for
#   instantiated (formerly generic) procs. The analysis has to look at nkSym.
#   This also means we need to prevent the same proc is processed multiple
#   times via the 'processed' set.
# * Keep in mind that the owner of some temporaries used to be unreliable.
# * For closure iterators we merge the "real" potential closure with the
#   local storage requirements for efficiency. This means closure iterators
#   have slightly different semantics from ordinary closures.

# ---------------- essential helpers -------------------------------------

const
  upName* = ":up" # field name for the 'up' reference
  paramName* = ":envP"
  envName* = ":env"

proc newCall(a: PSym, b: PNode): PNode =
  result = newNodeI(nkCall, a.info)
  result.add newSymNode(a)
  result.add b

proc createStateType(iter: PSym): PType =
  var n = newNodeI(nkRange, iter.info)
  addSon(n, newIntNode(nkIntLit, -1))
  addSon(n, newIntNode(nkIntLit, 0))
  result = newType(tyRange, iter)
  result.n = n
  var intType = nilOrSysInt()
  if intType.isNil: intType = newType(tyInt, iter)
  rawAddSon(result, intType)

proc createStateField(iter: PSym): PSym =
  result = newSym(skField, getIdent(":state"), iter, iter.info)
  result.typ = createStateType(iter)

proc createEnvObj(owner: PSym; info: TLineInfo): PType =
  # YYY meh, just add the state field for every closure for now, it's too
  # hard to figure out if it comes from a closure iterator:
  result = createObj(owner, info)
  rawAddField(result, createStateField(owner))

proc getIterResult(iter: PSym): PSym =
  if resultPos < iter.ast.len:
    result = iter.ast.sons[resultPos].sym
  else:
    # XXX a bit hacky:
    result = newSym(skResult, getIdent":result", iter, iter.info)
    result.typ = iter.typ.sons[0]
    incl(result.flags, sfUsed)
    iter.ast.add newSymNode(result)

proc addHiddenParam(routine: PSym, param: PSym) =
  assert param.kind == skParam
  var params = routine.ast.sons[paramsPos]
  # -1 is correct here as param.position is 0 based but we have at position 0
  # some nkEffect node:
  param.position = routine.typ.n.len-1
  addSon(params, newSymNode(param))
  #incl(routine.typ.flags, tfCapturesEnv)
  assert sfFromGeneric in param.flags
  #echo "produced environment: ", param.id, " for ", routine.id

proc getHiddenParam(routine: PSym): PSym =
  let params = routine.ast.sons[paramsPos]
  let hidden = lastSon(params)
  if hidden.kind == nkSym and hidden.sym.kind == skParam and hidden.sym.name.s == paramName:
    result = hidden.sym
    assert sfFromGeneric in result.flags
  else:
    # writeStackTrace()
    localError(routine.info, "internal error: could not find env param for " & routine.name.s)
    result = routine

proc getEnvParam*(routine: PSym): PSym =
  let params = routine.ast.sons[paramsPos]
  let hidden = lastSon(params)
  if hidden.kind == nkSym and hidden.sym.name.s == paramName:
    result = hidden.sym
    assert sfFromGeneric in result.flags

proc interestingVar(s: PSym): bool {.inline.} =
  result = s.kind in {skVar, skLet, skTemp, skForVar, skParam, skResult} and
    sfGlobal notin s.flags

proc illegalCapture(s: PSym): bool {.inline.} =
  result = skipTypes(s.typ, abstractInst).kind in
                   {tyVar, tyOpenArray, tyVarargs} or
      s.kind == skResult

proc isInnerProc(s: PSym): bool =
  if s.kind in {skProc, skMethod, skConverter, skIterator} and s.magic == mNone:
    result = s.skipGenericOwner.kind in routineKinds

proc newAsgnStmt(le, ri: PNode, info: TLineInfo): PNode =
  # Bugfix: unfortunately we cannot use 'nkFastAsgn' here as that would
  # mean to be able to capture string literals which have no GC header.
  # However this can only happen if the capture happens through a parameter,
  # which is however the only case when we generate an assignment in the first
  # place.
  result = newNodeI(nkAsgn, info, 2)
  result.sons[0] = le
  result.sons[1] = ri

proc makeClosure*(prc: PSym; env: PNode; info: TLineInfo): PNode =
  result = newNodeIT(nkClosure, info, prc.typ)
  result.add(newSymNode(prc))
  if env == nil:
    result.add(newNodeIT(nkNilLit, info, getSysType(tyNil)))
  else:
    if env.skipConv.kind == nkClosure:
      localError(info, "internal error: taking closure of closure")
    result.add(env)

proc interestingIterVar(s: PSym): bool {.inline.} =
  # XXX optimization: Only lift the variable if it lives across
  # yield/return boundaries! This can potentially speed up
  # closure iterators quite a bit.
  result = s.kind in {skVar, skLet, skTemp, skForVar} and sfGlobal notin s.flags

template isIterator*(owner: PSym): bool =
  owner.kind == skIterator and owner.typ.callConv == ccClosure

proc liftingHarmful(owner: PSym): bool {.inline.} =
  ## lambda lifting can be harmful for JS-like code generators.
  let isCompileTime = sfCompileTime in owner.flags or owner.kind == skMacro
  result = gCmd in {cmdCompileToPHP, cmdCompileToJS} and not isCompileTime

proc liftIterSym*(n: PNode; owner: PSym): PNode =
  # transforms  (iter)  to  (let env = newClosure[iter](); (iter, env))
  if liftingHarmful(owner): return n
  let iter = n.sym
  assert iter.isIterator

  result = newNodeIT(nkStmtListExpr, n.info, n.typ)

  let hp = getHiddenParam(iter)
  var env: PNode
  if owner.isIterator:
    let it = getHiddenParam(owner)
    addUniqueField(it.typ.sons[0], hp)
    env = indirectAccess(newSymNode(it), hp, hp.info)
  else:
    let e = newSym(skLet, iter.name, owner, n.info)
    e.typ = hp.typ
    e.flags = hp.flags
    env lass="n">g_key_file_free(theme);
        }
        theme = g_key_file_new();

    // load theme from file
    } else {
        GString *new_theme_file = _theme_find(theme_name);
        if (new_theme_file == NULL) {
            log_info("Theme does not exist \"%s\"", theme_name);
            return FALSE;
        }

        if (theme_loc) {
            g_string_free(theme_loc, TRUE);
        }
        theme_loc = new_theme_file;
        log_info("Loading theme \"%s\"", theme_name);
        if (theme) {
            g_key_file_free(theme);
        }
        theme = g_key_file_new();
        g_key_file_load_from_file(theme, theme_loc->str, G_KEY_FILE_KEEP_COMMENTS,
            NULL);
    }

    return TRUE;
}

GSList*
theme_list(void)
{
    GSList *result = NULL;
    char *themes_dir = _get_themes_dir();
    _theme_list_dir(themes_dir, &result);
    free(themes_dir);
#ifdef THEMES_PATH
    _theme_list_dir(THEMES_PATH, &result);
#endif
    return result;
}

void
theme_close(void)
{
    if (theme) {
        g_key_file_free(theme);
        theme = NULL;
    }
    if (theme_loc) {
        g_string_free(theme_loc, TRUE);
        theme_loc = NULL;
    }
    if (bold_items) {
        g_hash_table_destroy(bold_items);
        bold_items = NULL;
    }
    if (str_to_pair) {
        g_hash_table_destroy(str_to_pair);
        str_to_pair = NULL;
    }
    if (defaults) {
        g_hash_table_destroy(defaults);
        defaults = NULL;
    }
}

static void
_theme_init_pair(short pair, short fgnd, short bgnd, char *pair_str)
{
    init_pair(pair, fgnd, bgnd);
    g_hash_table_insert(str_to_pair, strdup(pair_str), GINT_TO_POINTER((int)pair));
}

void
theme_init_colours(void)
{
    assume_default_colors(-1, -1);
    g_hash_table_insert(str_to_pair, strdup("default_default"), 0);

    _theme_init_pair(1, -1, COLOR_BLACK,                "default_black");
    _theme_init_pair(2, -1, COLOR_BLUE,                 "default_blue");
    _theme_init_pair(3, -1, COLOR_GREEN,                "default_green");
    _theme_init_pair(4, -1, COLOR_RED,                  "default_red");
    _theme_init_pair(5, -1, COLOR_CYAN,                 "default_cyan");
    _theme_init_pair(6, -1, COLOR_MAGENTA,              "default_magenta");
    _theme_init_pair(7, -1, COLOR_WHITE,                "default_white");
    _theme_init_pair(8, -1, COLOR_YELLOW,               "default_yellow");

    _theme_init_pair(9,  COLOR_BLACK, -1,               "black_default");
    _theme_init_pair(10, COLOR_BLACK, COLOR_BLACK,      "black_black");
    _theme_init_pair(11, COLOR_BLACK, COLOR_BLUE,       "black_blue");
    _theme_init_pair(12, COLOR_BLACK, COLOR_GREEN,      "black_green");
    _theme_init_pair(13, COLOR_BLACK, COLOR_RED,        "black_red");
    _theme_init_pair(14, COLOR_BLACK, COLOR_CYAN,       "black_cyan");
    _theme_init_pair(15, COLOR_BLACK, COLOR_MAGENTA,    "black_magenta");
    _theme_init_pair(16, COLOR_BLACK, COLOR_WHITE,      "black_white");
    _theme_init_pair(17, COLOR_BLACK, COLOR_YELLOW,     "black_yellow");

    _theme_init_pair(18, COLOR_BLUE, -1,                "blue_default");
    _theme_init_pair(19, COLOR_BLUE, COLOR_BLACK,       "blue_black");
    _theme_init_pair(20, COLOR_BLUE, COLOR_BLUE,        "blue_blue");
    _theme_init_pair(21, COLOR_BLUE, COLOR_GREEN,       "blue_green");
    _theme_init_pair(22, COLOR_BLUE, COLOR_RED,         "blue_red");
    _theme_init_pair(23, COLOR_BLUE, COLOR_CYAN,        "blue_cyan");
    _theme_init_pair(24, COLOR_BLUE, COLOR_MAGENTA,     "blue_magenta");
    _theme_init_pair(25, COLOR_BLUE, COLOR_WHITE,       "blue_white");
    _theme_init_pair(26, COLOR_BLUE, COLOR_YELLOW,      "blue_yellow");

    _theme_init_pair(27, COLOR_GREEN, -1,               "green_default");
    _theme_init_pair(28, COLOR_GREEN, COLOR_BLACK,      "green_black");
    _theme_init_pair(29, COLOR_GREEN, COLOR_BLUE,       "green_blue");
    _theme_init_pair(30, COLOR_GREEN, COLOR_GREEN,      "green_green");
    _theme_init_pair(31, COLOR_GREEN, COLOR_RED,        "green_red");
    _theme_init_pair(32, COLOR_GREEN, COLOR_CYAN,       "green_cyan");
    _theme_init_pair(33, COLOR_GREEN, COLOR_MAGENTA,    "green_magenta");
    _theme_init_pair(34, COLOR_GREEN, COLOR_WHITE,      "green_white");
    _theme_init_pair(35, COLOR_GREEN, COLOR_YELLOW,     "green_yellow");

    _theme_init_pair(36, COLOR_RED, -1,                 "red_default");
    _theme_init_pair(37, COLOR_RED, COLOR_BLACK,        "red_black");
    _theme_init_pair(38, COLOR_RED, COLOR_BLUE,         "red_blue");
    _theme_init_pair(39, COLOR_RED, COLOR_GREEN,        "red_green");
    _theme_init_pair(40, COLOR_RED, COLOR_RED,          "red_red");
    _theme_init_pair(41, COLOR_RED, COLOR_CYAN,         "red_cyan");
    _theme_init_pair(42, COLOR_RED, COLOR_MAGENTA,      "red_magenta");
    _theme_init_pair(43, COLOR_RED, COLOR_WHITE,        "red_white");
    _theme_init_pair(44, COLOR_RED, COLOR_YELLOW,       "red_yellow");

    _theme_init_pair(45, COLOR_CYAN, -1,                "cyan_default");
    _theme_init_pair(46, COLOR_CYAN, COLOR_BLACK,       "cyan_black");
    _theme_init_pair(47, COLOR_CYAN, COLOR_BLUE,        "cyan_blue");
    _theme_init_pair(48, COLOR_CYAN, COLOR_GREEN,       "cyan_green");
    _theme_init_pair(49, COLOR_CYAN, COLOR_RED,         "cyan_red");
    _theme_init_pair(50, COLOR_CYAN, COLOR_CYAN,        "cyan_cyan");
    _theme_init_pair(51, COLOR_CYAN, COLOR_MAGENTA,     "cyan_magenta");
    _theme_init_pair(52, COLOR_CYAN, COLOR_WHITE,       "cyan_white");
    _theme_init_pair(53, COLOR_CYAN, COLOR_YELLOW,      "cyan_yellow");

    _theme_init_pair(54, COLOR_MAGENTA, -1,             "magenta_default");
    _theme_init_pair(55, COLOR_MAGENTA, COLOR_BLACK,    "magenta_black");
    _theme_init_pair(56, COLOR_MAGENTA, COLOR_BLUE,     "magenta_blue");
    _theme_init_pair(57, COLOR_MAGENTA, COLOR_GREEN,    "magenta_green");
    _theme_init_pair(58, COLOR_MAGENTA, COLOR_RED,      "magenta_red");
    _theme_init_pair(59, COLOR_MAGENTA, COLOR_CYAN,     "magenta_cyan");
    _theme_init_pair(60, COLOR_MAGENTA, COLOR_MAGENTA,  "magenta_magenta");
    _theme_init_pair(61, COLOR_MAGENTA, COLOR_WHITE,    "magenta_white");
    _theme_init_pair(62, COLOR_MAGENTA, COLOR_YELLOW,   "magenta_yellow");

    _theme_init_pair(63, COLOR_WHITE, -1,               "white_default");
    _theme_init_pair(64, COLOR_WHITE, COLOR_BLACK,      "white_black");
    _theme_init_pair(65, COLOR_WHITE, COLOR_BLUE,       "white_blue");
    _theme_init_pair(66, COLOR_WHITE, COLOR_GREEN,      "white_green");
    _theme_init_pair(67, COLOR_WHITE, COLOR_RED,        "white_red");
    _theme_init_pair(68, COLOR_WHITE, COLOR_CYAN,       "white_cyan");
    _theme_init_pair(69, COLOR_WHITE, COLOR_MAGENTA,    "white_magenta");
    _theme_init_pair(70, COLOR_WHITE, COLOR_WHITE,      "white_white");
    _theme_init_pair(71, COLOR_WHITE, COLOR_YELLOW,     "white_yellow");

    _theme_init_pair(72, COLOR_YELLOW, -1,              "yellow_default");
    _theme_init_pair(73, COLOR_YELLOW, COLOR_BLACK,     "yellow_black");
    _theme_init_pair(74, COLOR_YELLOW, COLOR_BLUE,      "yellow_blue");
    _theme_init_pair(75, COLOR_YELLOW, COLOR_GREEN,     "yellow_green");
    _theme_init_pair(76, COLOR_YELLOW, COLOR_RED,       "yellow_red");
    _theme_init_pair(77, COLOR_YELLOW, COLOR_CYAN,      "yellow_cyan");
    _theme_init_pair(78, COLOR_YELLOW, COLOR_MAGENTA,   "yellow_magenta");
    _theme_init_pair(79, COLOR_YELLOW, COLOR_WHITE,     "yellow_white");
    _theme_init_pair(80, COLOR_YELLOW, COLOR_YELLOW,    "yellow_yellow");
}

static void
_set_string_preference(char *prefstr, preference_t pref)
{
    if (g_key_file_has_key(theme, "ui", prefstr, NULL)) {
        gchar *val = g_key_file_get_string(theme, "ui", prefstr, NULL);
        prefs_set_string(pref, val);
        g_free(val);
    }
}

static void
_set_boolean_preference(char *prefstr, preference_t pref)
{
    if (g_key_file_has_key(theme, "ui", prefstr, NULL)) {
        gboolean val = g_key_file_get_boolean(theme, "ui", prefstr, NULL);
        prefs_set_boolean(pref, val);
    }
}

static void
_load_preferences(void)
{
    _set_boolean_preference("beep", PREF_BEEP);
    _set_boolean_preference("flash", PREF_FLASH);
    _set_boolean_preference("splash", PREF_SPLASH);
    _set_boolean_preference("wrap", PREF_WRAP);
    _set_boolean_preference("wins.autotidy", PREF_WINS_AUTO_TIDY);
    _set_boolean_preference("resource.title", PREF_RESOURCE_TITLE);
    _set_boolean_preference("resource.message", PREF_RESOURCE_MESSAGE);
    _set_boolean_preference("occupants", PREF_OCCUPANTS);
    _set_boolean_preference("occupants.jid", PREF_OCCUPANTS_JID);
    _set_boolean_preference("roster", PREF_ROSTER);
    _set_boolean_preference("roster.offline", PREF_ROSTER_OFFLINE);
    _set_boolean_preference("roster.resource", PREF_ROSTER_RESOURCE);
    _set_boolean_preference("roster.resource.join", PREF_ROSTER_RESOURCE_JOIN);
    _set_boolean_preference("roster.presence", PREF_ROSTER_PRESENCE);
    _set_boolean_preference("roster.status", PREF_ROSTER_STATUS);
    _set_boolean_preference("roster.empty", PREF_ROSTER_EMPTY);
    _set_boolean_preference("roster.wrap", PREF_ROSTER_WRAP);
    _set_boolean_preference("roster.count.zero", PREF_ROSTER_COUNT_ZERO);
    _set_boolean_preference("roster.priority", PREF_ROSTER_PRIORITY);
    _set_boolean_preference("roster.contacts", PREF_ROSTER_CONTACTS);
    _set_boolean_preference("roster.rooms", PREF_ROSTER_ROOMS);
    _set_boolean_preference("privileges", PREF_MUC_PRIVILEGES);
    _set_boolean_preference("presence", PREF_PRESENCE);
    _set_boolean_preference("intype", PREF_INTYPE);
    _set_boolean_preference("enc.warn", PREF_ENC_WARN);
    _set_boolean_preference("tls.show", PREF_TLS_SHOW);

    _set_string_preference("time.console", PREF_TIME_CONSOLE);
    _set_string_preference("time.chat", PREF_TIME_CHAT);
    _set_string_preference("time.muc", PREF_TIME_MUC);
    _set_string_preference("time.mucconfig", PREF_TIME_MUCCONFIG);
    _set_string_preference("time.private", PREF_TIME_PRIVATE);
    _set_string_preference("time.xmlconsole", PREF_TIME_XMLCONSOLE);
    _set_string_preference("time.statusbar", PREF_TIME_STATUSBAR);
    _set_string_preference("time.lastactivity", PREF_TIME_LASTACTIVITY);
    _set_string_preference("statuses.console", PREF_STATUSES_CONSOLE);
    _set_string_preference("statuses.chat", PREF_STATUSES_CHAT);
    _set_string_preference("statuses.muc", PREF_STATUSES_MUC);
    _set_string_preference("console.muc", PREF_CONSOLE_MUC);
    _set_string_preference("roster.by", PREF_ROSTER_BY);
    _set_string_preference("roster.order", PREF_ROSTER_ORDER);
    _set_string_preference("roster.unread", PREF_ROSTER_UNREAD);
    _set_string_preference("roster.rooms.order", PREF_ROSTER_ROOMS_ORDER);
    _set_string_preference("roster.rooms.unread", PREF_ROSTER_ROOMS_UNREAD);
    _set_string_preference("roster.rooms.pos", PREF_ROSTER_ROOMS_POS);
    _set_string_preference("roster.private", PREF_ROSTER_PRIVATE);
    _set_string_preference("roster.count", PREF_ROSTER_COUNT);


    if (g_key_file_has_key(theme, "ui", "occupants.size", NULL)) {
        gint occupants_size = g_key_file_get_integer(theme, "ui", "occupants.size", NULL);
        prefs_set_occupants_size(occupants_size);
    }

    if (g_key_file_has_key(theme, "ui", "roster.size", NULL)) {
        gint roster_size = g_key_file_get_integer(theme, "ui", "roster.size", NULL);
        prefs_set_roster_size(roster_size);
    }

    if (g_key_file_has_key(theme, "ui", "roster.header.char", NULL)) {
        gchar *ch = g_key_file_get_string(theme, "ui", "roster.header.char", NULL);
        if (ch && strlen(ch) > 0) {
            prefs_set_roster_header_char(ch[0]);
            g_free(ch);
        }
    } else {
        prefs_clear_roster_header_char();
    }

    if (g_key_file_has_key(theme, "ui", "roster.contact.char", NULL)) {
        gchar *ch = g_key_file_get_string(theme, "ui", "roster.contact.char", NULL);
        if (ch && strlen(ch) > 0) {
            prefs_set_roster_contact_char(ch[0]);
            g_free(ch);
        }
    } else {
        prefs_clear_roster_contact_char();
    }

    if (g_key_file_has_key(theme, "ui", "roster.resource.char", NULL)) {
        gchar *ch = g_key_file_get_string(theme, "ui", "roster.resource.char", NULL);
        if (ch && strlen(ch) > 0) {
            prefs_set_roster_resource_char(ch[0]);
            g_free(ch);
        }
    } else {
        prefs_clear_roster_resource_char();
    }

    if (g_key_file_has_key(theme, "ui", "roster.rooms.char", NULL)) {
        gchar *ch = g_key_file_get_string(theme, "ui", "roster.rooms.char", NULL);
        if (ch && strlen(ch) > 0) {
            prefs_set_roster_room_char(ch[0]);
            g_free(ch);
        }
    } else {
        prefs_clear_roster_room_char();
    }

    if (g_key_file_has_key(theme, "ui", "roster.rooms.private.char", NULL)) {
        gchar *ch = g_key_file_get_string(theme, "ui", "roster.rooms.private.char", NULL);
        if (ch && strlen(ch) > 0) {
            prefs_set_roster_room_private_char(ch[0]);
            g_free(ch);
        }
    } else {
        prefs_clear_roster_room_private_char();
    }

    if (g_key_file_has_key(theme, "ui", "roster.private.char", NULL)) {
        gchar *ch = g_key_file_get_string(theme, "ui", "roster.private.char", NULL);
        if (ch && strlen(ch) > 0) {
            prefs_set_roster_private_char(ch[0]);
            g_free(ch);
        }
    } else {
        prefs_clear_roster_private_char();
    }

    if (g_key_file_has_key(theme, "ui", "roster.contact.indent", NULL)) {
        gint contact_indent = g_key_file_get_integer(theme, "ui", "roster.contact.indent", NULL);
        prefs_set_roster_contact_indent(contact_indent);
    }

    if (g_key_file_has_key(theme, "ui", "roster.resource.indent", NULL)) {
        gint resource_indent = g_key_file_get_integer(theme, "ui", "roster.resource.indent", NULL);
        prefs_set_roster_resource_indent(resource_indent);
    }

    if (g_key_file_has_key(theme, "ui", "roster.presence.indent", NULL)) {
        gint presence_indent = g_key_file_get_integer(theme, "ui", "roster.presence.indent", NULL);
        prefs_set_roster_presence_indent(presence_indent);
    }

    if (g_key_file_has_key(theme, "ui", "otr.char", NULL)) {
        gchar *ch = g_key_file_get_string(theme, "ui", "otr.char", NULL);
        if (ch && strlen(ch) > 0) {
            prefs_set_otr_char(ch[0]);
            g_free(ch);
        }
    }

    if (g_key_file_has_key(theme, "ui", "pgp.char", NULL)) {
        gchar *ch = g_key_file_get_string(theme, "ui", "pgp.char", NULL);
        if (ch && strlen(ch) > 0) {
            prefs_set_pgp_char(ch[0]);
            g_free(ch);
        }
    }
}

static gchar*
_get_themes_dir(void)
{
    gchar *xdg_config = xdg_get_config_home();
    GString *themes_dir = g_string_new(xdg_config);
    g_free(xdg_config);
    g_string_append(themes_dir, "/profanity/themes");
    return g_string_free(themes_dir, FALSE);
}

void
_theme_list_dir(const gchar *const dir, GSList **result)
{
    GDir *themes = g_dir_open(dir, 0, NULL);
    if (themes) {
        const gchar *theme = g_dir_read_name(themes);
        while (theme) {
            *result = g_slist_append(*result, strdup(theme));
            theme = g_dir_read_name(themes);
        }
        g_dir_close(themes);
    }
}

static GString*
_theme_find(const char *const theme_name)
{
    GString *path = NULL;
    gchar *themes_dir = _get_themes_dir();

    if (themes_dir) {
        path = g_string_new(themes_dir);
        g_free(themes_dir);
        g_string_append(path, "/");
        g_string_append(path, theme_name);
        if (!g_file_test(path->str, G_FILE_TEST_EXISTS)) {
            g_string_free(path, true);
            path = NULL;
        }
    }

#ifdef THEMES_PATH
    if (path == NULL) {
        path = g_string_new(THEMES_PATH);
        g_string_append(path, "/");
        g_string_append(path, theme_name);
        if (!g_file_test(path->str, G_FILE_TEST_EXISTS)) {
            g_string_free(path, true);
            path = NULL;
        }
    }
#endif /* THEMES_PATH */

    return path;
}

theme_item_t
theme_roster_unread_presence_attrs(const char *const presence)
{
    if (g_strcmp0(presence, "online") == 0) {
        return THEME_ROSTER_ONLINE_UNREAD;
    } else if (g_strcmp0(presence, "away") == 0) {
        return THEME_ROSTER_AWAY_UNREAD;
    } else if (g_strcmp0(presence, "chat") == 0) {
        return THEME_ROSTER_CHAT_UNREAD;
    } else if (g_strcmp0(presence, "dnd") == 0) {
        return THEME_ROSTER_DND_UNREAD;
    } else if (g_strcmp0(presence, "xa") == 0) {
        return THEME_ROSTER_XA_UNREAD;
    } else {
        return THEME_ROSTER_OFFLINE_UNREAD;
    }
}

theme_item_t
theme_roster_active_presence_attrs(const char *const presence)
{
    if (g_strcmp0(presence, "online") == 0) {
        return THEME_ROSTER_ONLINE_ACTIVE;
    } else if (g_strcmp0(presence, "away") == 0) {
        return THEME_ROSTER_AWAY_ACTIVE;
    } else if (g_strcmp0(presence, "chat") == 0) {
        return THEME_ROSTER_CHAT_ACTIVE;
    } else if (g_strcmp0(presence, "dnd") == 0) {
        return THEME_ROSTER_DND_ACTIVE;
    } else if (g_strcmp0(presence, "xa") == 0) {
        return THEME_ROSTER_XA_ACTIVE;
    } else {
        return THEME_ROSTER_OFFLINE_ACTIVE;
    }
}

theme_item_t
theme_roster_presence_attrs(const char *const presence)
{
    if (g_strcmp0(presence, "online") == 0) {
        return THEME_ROSTER_ONLINE;
    } else if (g_strcmp0(presence, "away") == 0) {
        return THEME_ROSTER_AWAY;
    } else if (g_strcmp0(presence, "chat") == 0) {
        return THEME_ROSTER_CHAT;
    } else if (g_strcmp0(presence, "dnd") == 0) {
        return THEME_ROSTER_DND;
    } else if (g_strcmp0(presence, "xa") == 0) {
        return THEME_ROSTER_XA;
    } else {
        return THEME_ROSTER_OFFLINE;
    }
}

theme_item_t
theme_main_presence_attrs(const char *const presence)
{
    if (g_strcmp0(presence, "online") == 0) {
        return THEME_ONLINE;
    } else if (g_strcmp0(presence, "away") == 0) {
        return THEME_AWAY;
    } else if (g_strcmp0(presence, "chat") == 0) {
        return THEME_CHAT;
    } else if (g_strcmp0(presence, "dnd") == 0) {
        return THEME_DND;
    } else if (g_strcmp0(presence, "xa") == 0) {
        return THEME_XA;
    } else {
        return THEME_OFFLINE;
    }
}

static void
_theme_prep_bgnd(char *setting, char *def, GString *lookup_str)
{
    gchar *val = g_key_file_get_string(theme, "colours", setting, NULL);
    if (!val) {
        g_string_append(lookup_str, def);
    } else {
        if (g_str_has_prefix(val, "bold_")) {
            g_string_append(lookup_str, &val[5]);
        } else {
            g_string_append(lookup_str, val);
        }
    }
    g_free(val);
}

static void
_theme_prep_fgnd(char *setting, GString *lookup_str, gboolean *bold)
{
    gchar *val = g_key_file_get_string(theme, "colours", setting, NULL);
    if (!val) {
        char *def = g_hash_table_lookup(defaults, setting);
        g_string_append(lookup_str, def);
    } else {
        if (g_str_has_prefix(val, "bold_")) {
            g_string_append(lookup_str, &val[5]);
            *bold = TRUE;
        } else {
            g_string_append(lookup_str, val);
            *bold = FALSE;
        }
    }
    g_free(val);
}

char*
theme_get_string(char *str)
{
    char *res = g_key_file_get_string(theme, "colours", str, NULL);
    if (!res) {
        return strdup(g_hash_table_lookup(defaults, str));
    } else {
        return res;
    }
}

void
theme_free_string(char *str)
{
    if (str) {
        g_free(str);
    }
}

int
theme_attrs(theme_item_t attrs)
{
    int result = 0;

    GString *lookup_str = g_string_new("");
    gboolean bold = FALSE;

    // get forground colour
    switch (attrs) {
    case THEME_TEXT:                    _theme_prep_fgnd("main.text",               lookup_str, &bold); break;
    case THEME_TEXT_ME:                 _theme_prep_fgnd("main.text.me",            lookup_str, &bold); break;
    case THEME_TEXT_THEM:               _theme_prep_fgnd("main.text.them",          lookup_str, &bold); break;
    case THEME_SPLASH:                  _theme_prep_fgnd("main.splash",             lookup_str, &bold); break;
    case THEME_ERROR:                   _theme_prep_fgnd("error",                   lookup_str, &bold); break;
    case THEME_INCOMING:                _theme_prep_fgnd("incoming",                lookup_str, &bold); break;
    case THEME_INPUT_TEXT:              _theme_prep_fgnd("input.text",              lookup_str, &bold); break;
    case THEME_TIME:                    _theme_prep_fgnd("main.time",               lookup_str, &bold); break;
    case THEME_TITLE_TEXT:              _theme_prep_fgnd("titlebar.text",           lookup_str, &bold); break;
    case THEME_TITLE_BRACKET:           _theme_prep_fgnd("titlebar.brackets",       lookup_str, &bold); break;
    case THEME_TITLE_UNENCRYPTED:       _theme_prep_fgnd("titlebar.unencrypted",    lookup_str, &bold); break;
    case THEME_TITLE_ENCRYPTED:         _theme_prep_fgnd("titlebar.encrypted",      lookup_str, &bold); break;
    case THEME_TITLE_UNTRUSTED:         _theme_prep_fgnd("titlebar.untrusted",      lookup_str, &bold); break;
    case THEME_TITLE_TRUSTED:           _theme_prep_fgnd("titlebar.trusted",        lookup_str, &bold); break;
    case THEME_TITLE_ONLINE:            _theme_prep_fgnd("titlebar.online",         lookup_str, &bold); break;
    case THEME_TITLE_OFFLINE:           _theme_prep_fgnd("titlebar.offline",        lookup_str, &bold); break;
    case THEME_TITLE_AWAY:              _theme_prep_fgnd("titlebar.away",           lookup_str, &bold); break;
    case THEME_TITLE_CHAT:              _theme_prep_fgnd("titlebar.chat",           lookup_str, &bold); break;
    case THEME_TITLE_DND:               _theme_prep_fgnd("titlebar.dnd",            lookup_str, &bold); break;
    case THEME_TITLE_XA:                _theme_prep_fgnd("titlebar.xa",             lookup_str, &bold); break;
    case THEME_STATUS_TEXT:             _theme_prep_fgnd("statusbar.text",          lookup_str, &bold); break;
    case THEME_STATUS_BRACKET:          _theme_prep_fgnd("statusbar.brackets",      lookup_str, &bold); break;
    case THEME_STATUS_ACTIVE:           _theme_prep_fgnd("statusbar.active",        lookup_str, &bold); break;
    case THEME_STATUS_NEW:              _theme_prep_fgnd("statusbar.new",           lookup_str, &bold); break;
    case THEME_ME:                      _theme_prep_fgnd("me",                      lookup_str, &bold); break;
    case THEME_THEM:                    _theme_prep_fgnd("them",                    lookup_str, &bold); break;
    case THEME_RECEIPT_SENT:            _theme_prep_fgnd("receipt.sent",            lookup_str, &bold); break;
    case THEME_ROOMINFO:                _theme_prep_fgnd("roominfo",                lookup_str, &bold); break;
    case THEME_ROOMMENTION:             _theme_prep_fgnd("roommention",             lookup_str, &bold); break;
    case THEME_ROOMTRIGGER:             _theme_prep_fgnd("roomtrigger",             lookup_str, &bold); break;
    case THEME_ONLINE:                  _theme_prep_fgnd("online",                  lookup_str, &bold); break;
    case THEME_OFFLINE:                 _theme_prep_fgnd("offline",                 lookup_str, &bold); break;
    case THEME_AWAY:                    _theme_prep_fgnd("away",                    lookup_str, &bold); break;
    case THEME_CHAT:                    _theme_prep_fgnd("chat",                    lookup_str, &bold); break;
    case THEME_DND:                     _theme_prep_fgnd("dnd",                     lookup_str, &bold); break;
    case THEME_XA:                      _theme_prep_fgnd("xa",                      lookup_str, &bold); break;
    case THEME_TYPING:                  _theme_prep_fgnd("typing",                  lookup_str, &bold); break;
    case THEME_GONE:                    _theme_prep_fgnd("gone",                    lookup_str, &bold); break;
    case THEME_SUBSCRIBED:              _theme_prep_fgnd("subscribed",              lookup_str, &bold); break;
    case THEME_UNSUBSCRIBED:            _theme_prep_fgnd("unsubscribed",            lookup_str, &bold); break;
    case THEME_OTR_STARTED_TRUSTED:     _theme_prep_fgnd("otr.started.trusted",     lookup_str, &bold); break;
    case THEME_OTR_STARTED_UNTRUSTED:   _theme_prep_fgnd("otr.started.untrusted",   lookup_str, &bold); break;
    case THEME_OTR_ENDED:               _theme_prep_fgnd("otr.ended",               lookup_str, &bold); break;
    case THEME_OTR_TRUSTED:             _theme_prep_fgnd("otr.trusted",             lookup_str, &bold); break;
    case THEME_OTR_UNTRUSTED:           _theme_prep_fgnd("otr.untrusted",           lookup_str, &bold); break;
    case THEME_ROSTER_HEADER:           _theme_prep_fgnd("roster.header",           lookup_str, &bold); break;
    case THEME_ROSTER_ONLINE:           _theme_prep_fgnd("roster.online",           lookup_str, &bold); break;
    case THEME_ROSTER_OFFLINE:          _theme_prep_fgnd("roster.offline",          lookup_str, &bold); break;
    case THEME_ROSTER_CHAT:             _theme_prep_fgnd("roster.chat",             lookup_str, &bold); break;
    case THEME_ROSTER_AWAY:             _theme_prep_fgnd("roster.away",             lookup_str, &bold); break;
    case THEME_ROSTER_DND:              _theme_prep_fgnd("roster.dnd",              lookup_str, &bold); break;
    case THEME_ROSTER_XA:               _theme_prep_fgnd("roster.xa",               lookup_str, &bold); break;
    case THEME_ROSTER_ONLINE_ACTIVE:    _theme_prep_fgnd("roster.online.active",    lookup_str, &bold); break;
    case THEME_ROSTER_OFFLINE_ACTIVE:   _theme_prep_fgnd("roster.offline.active",   lookup_str, &bold); break;
    case THEME_ROSTER_CHAT_ACTIVE:      _theme_prep_fgnd("roster.chat.active",      lookup_str, &bold); break;
    case THEME_ROSTER_AWAY_ACTIVE:      _theme_prep_fgnd("roster.away.active",      lookup_str, &bold); break;
    case THEME_ROSTER_DND_ACTIVE:       _theme_prep_fgnd("roster.dnd.active",       lookup_str, &bold); break;
    case THEME_ROSTER_XA_ACTIVE:        _theme_prep_fgnd("roster.xa.active",        lookup_str, &bold); break;
    case THEME_ROSTER_ONLINE_UNREAD:    _theme_prep_fgnd("roster.online.unread",    lookup_str, &bold); break;
    case THEME_ROSTER_OFFLINE_UNREAD:   _theme_prep_fgnd("roster.offline.unread",   lookup_str, &bold); break;
    case THEME_ROSTER_CHAT_UNREAD:      _theme_prep_fgnd("roster.chat.unread",      lookup_str, &bold); break;
    case THEME_ROSTER_AWAY_UNREAD:      _theme_prep_fgnd("roster.away.unread",      lookup_str, &bold); break;
    case THEME_ROSTER_DND_UNREAD:       _theme_prep_fgnd("roster.dnd.unread",       lookup_str, &bold); break;
    case THEME_ROSTER_XA_UNREAD:        _theme_prep_fgnd("roster.xa.unread",        lookup_str, &bold); break;
    case THEME_ROSTER_ROOM:             _theme_prep_fgnd("roster.room",             lookup_str, &bold); break;
    case THEME_ROSTER_ROOM_UNREAD:      _theme_prep_fgnd("roster.room.unread",      lookup_str, &bold); break;
    case THEME_ROSTER_ROOM_TRIGGER:     _theme_prep_fgnd("roster.room.trigger",     lookup_str, &bold); break;
    case THEME_ROSTER_ROOM_MENTION:     _theme_prep_fgnd("roster.room.mention",     lookup_str, &bold); break;
    case THEME_OCCUPANTS_HEADER:        _theme_prep_fgnd("occupants.header",        lookup_str, &bold); break;
    case THEME_WHITE:                   g_string_append(lookup_str, "white");   bold = FALSE;   break;
    case THEME_WHITE_BOLD:              g_string_append(lookup_str, "white");   bold = TRUE;    break;
    case THEME_GREEN:                   g_string_append(lookup_str, "green");   bold = FALSE;   break;
    case THEME_GREEN_BOLD:              g_string_append(lookup_str, "green");   bold = TRUE;    break;
    case THEME_RED:                     g_string_append(lookup_str, "red");     bold = FALSE;   break;
    case THEME_RED_BOLD:                g_string_append(lookup_str, "red");     bold = TRUE;    break;
    case THEME_YELLOW:                  g_string_append(lookup_str, "yellow");  bold = FALSE;   break;
    case THEME_YELLOW_BOLD:             g_string_append(lookup_str, "yellow");  bold = TRUE;    break;
    case THEME_BLUE:                    g_string_append(lookup_str, "blue");    bold = FALSE;   break;
    case THEME_BLUE_BOLD:               g_string_append(lookup_str, "blue");    bold = TRUE;    break;
    case THEME_CYAN:                    g_string_append(lookup_str, "cyan");    bold = FALSE;   break;
    case THEME_CYAN_BOLD:               g_string_append(lookup_str, "cyan");    bold = TRUE;    break;
    case THEME_BLACK:                   g_string_append(lookup_str, "black");   bold = FALSE;   break;
    case THEME_BLACK_BOLD:              g_string_append(lookup_str, "black");   bold = TRUE;    break;
    case THEME_MAGENTA:                 g_string_append(lookup_str, "magenta"); bold = FALSE;   break;
    case THEME_MAGENTA_BOLD:            g_string_append(lookup_str, "magenta"); bold = TRUE;    break;
    default:                            g_string_append(lookup_str, "default"); bold = FALSE;   break;
    }

    g_string_append(lookup_str, "_");

    // append background str
    switch (attrs) {
    case THEME_TITLE_TEXT:
    case THEME_TITLE_BRACKET:
    case THEME_TITLE_UNENCRYPTED:
    case THEME_TITLE_ENCRYPTED:
    case THEME_TITLE_UNTRUSTED:
    case THEME_TITLE_TRUSTED:
    case THEME_TITLE_ONLINE:
    case THEME_TITLE_OFFLINE:
    case THEME_TITLE_AWAY:
    case THEME_TITLE_CHAT:
    case THEME_TITLE_DND:
    case THEME_TITLE_XA:
        _theme_prep_bgnd("titlebar", "blue", lookup_str);
        break;
    case THEME_STATUS_TEXT:
    case THEME_STATUS_BRACKET:
    case THEME_STATUS_ACTIVE:
    case THEME_STATUS_NEW:
        _theme_prep_bgnd("statusbar", "blue", lookup_str);
        break;
    default:
        _theme_prep_bgnd("bkgnd", "default", lookup_str);
        break;
    }

    // lookup colour pair
    result = GPOINTER_TO_INT(g_hash_table_lookup(str_to_pair, lookup_str->str));
    g_string_free(lookup_str, TRUE);
    if (bold) {
        return COLOR_PAIR(result) | A_BOLD;
    } else {
        return COLOR_PAIR(result);
    }
}