summaryrefslogtreecommitdiff
path: root/config/kak/palette.kak
diff options
context:
space:
mode:
authorMarc Coquand <marc@mccd.space>2024-05-15 19:47:21 -0500
committerMarc Coquand <marc@mccd.space>2024-05-15 19:47:21 -0500
commit9536a4c2b46e6851db41bab2806c7bdeee8edbb8 (patch)
tree5a7be6c2376ed92884557d0257d0ed8c3dc1e796 /config/kak/palette.kak
parentd09be0d5ced1b31e40a1114581abfce0db06c540 (diff)
downloadrcm-9536a4c2b46e6851db41bab2806c7bdeee8edbb8.tar.gz
rcm-9536a4c2b46e6851db41bab2806c7bdeee8edbb8.tar.bz2
rcm-9536a4c2b46e6851db41bab2806c7bdeee8edbb8.zip
Add kak and more
Diffstat (limited to 'config/kak/palette.kak')
-rw-r--r--config/kak/palette.kak102
1 files changed, 102 insertions, 0 deletions
diff --git a/config/kak/palette.kak b/config/kak/palette.kak
new file mode 100644
index 0000000..66272de
--- /dev/null
+++ b/config/kak/palette.kak
@@ -0,0 +1,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 ';'
+}