summaryrefslogtreecommitdiff
path: root/config/kak/palette.kak
blob: 66272de2146abd881b833d86b989c6377089557e (plain)
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
define-command palette-status -docstring 'show main selection color in status bar' %{
  evaluate-commands %sh{
    awk_script='{
      if ((x=index($1,"#")) > 0)
        $1 = substr($1, x+1)
      if (length($1) == 8)
        $1 = substr($1, 1, 6)
      if (length($1) == 4)
        $1 = substr($1, 1, 3)
      if (length($1) == 3) {
        r = substr($1, 1, 1)
        g = substr($1, 2, 1)
        b = substr($1, 3, 1)
        $1 = r r g g b b
      }
      print "try %{ evaluate-commands -client " client " echo -markup {rgb:" $1 "} ██████ }"
    }'
    printf %s\\n "$kak_selection" | awk -v client="$kak_client" "$awk_script" | kak -p "$kak_session"
  }
}

# use this palette command in a colorscheme kak file or the output of 'debug faces'
# it will add a color preview in a new column on the left for each line with a face declaration

define-command palette-gutter -docstring 'show faces in gutter' %{
  set-face global Palette default
  # palette_flags is undefined for now, but will be populated by the set command generated by awk
  # it must follow this format: 1486635122:1|Foo:3|{red,yellow+b}Bar
  # which is a timestamp, then a list of line number|text tuples separated by colons
  declare-option line-specs palette_flags
  # from previous call
  remove-highlighter window/palette
  add-highlighter window/palette flag-lines Palette palette_flags

  # populate $kak_selection to feed the whole buffer content to awk
  execute-keys '%'

  # awk quick survival guide:
  # - NR is line number
  # - substr starts at 1
  # - the v flag is used to assign argument
  # - gsub() returns 0 or 1, but mutates 3rd arg
  evaluate-commands %sh{
    case "$kak_opt_filetype" in

    'kak')
      awk_script='
        /face / {
          flags = flags NR "|{" $3 "}" $3 " "
        }
      '
      ;;

    'css'|'less'|'sass'|'scss'|'stylus')
      awk_script='
        function toKakColor(hex) {
          if (length(hex) == 7) {
            return "rgb:" substr(hex, 2, 6)
          } else if (length(hex) == 4) {
            r = substr(hex, 2, 1)
            g = substr(hex, 3, 1)
            b = substr(hex, 4, 1)
            return "rgb:" r r g g b b
          } else {
            return "rgb:000000"
          }
        }
        /color: / {
          flags = flags NR "|{" toKakColor($2) "," toKakColor($2) "}██████ "
        }
      '
      ;;

    *)
      # parse the output of 'debug faces' command
      if [ "$kak_bufname" = '*debug*' ]; then
        awk_script='
          / \* / {
            gsub(":", " ", $2)
            flags = flags NR "|{" $3 "}" $2
          }
        '
      else
        echo 'echo -markup "{Error}filetype not supported by palette"'
      fi
      ;;

    esac

    if [ -n "$awk_script" ]; then
      awk_script=$awk_script'
        END {
          print "set-option \"buffer=" file "\" palette_flags " stamp " " substr(flags, 1, length(flags)-1)
        }
      '
      printf %s\\n "$kak_selection" | awk -v file="$kak_buffile" -v stamp="$kak_timestamp" "$awk_script" | kak -p "$kak_session"
    fi
  }

  # back to normal after '%'
  execute-keys ';'
}