mawww / kakoune

mawww's experiment for a better code editor
http://kakoune.org
The Unlicense
9.91k stars 714 forks source link

`declare-colorscheme` command #4342

Open raiguard opened 3 years ago

raiguard commented 3 years ago

Feature

I would like to have a declare-colorscheme command that will add a colorscheme to the available colorscheme options, but not source the file. It would simply take a path argument like source.

Usecase

I am extending cork.kak to support colorschemes, and currently the only ways to do it "properly" are to either copy the colorscheme files to the ~/.config/kak/colors directory, or symlink them. Neither of these are preferred, because they both involve messing with the user's kak config directory, which cork.kak currently avoids entirely. Being able to add colorschemes to the colorscheme command with a different command would allow the files to remain outside the default kak directories.

declare-colorscheme "~/.local/share/kak/cork/plugins/one.kak/repo/colors/one-darker.kak"

raiguard commented 3 years ago

After looking at the source, I discovered that colorscheme isn't actually implemented in the C++ at all - it's just a kakscript! It seems the best solution is going to be defining more directories for the command to search through. I'll make a PR if I manage to get it done.

krobelus commented 3 years ago

Being able to install colorschemes with a plugin manager seems reasonable. Currently you could create a~/.config/kak/colors/cork If we added %opt{colorscheme_path} it could be ~/.config/kak/cork/colors

raiguard commented 3 years ago

I've been working on a script, but my lack of bash skills has been biting me. I added a colorscheme_sources option that lists all of the directories to pull colorschemes from. I modified the colorscheme command to pull from all of those directories. I modified cork to call set -add on this option for any plugins that have a colors directory.

The actual functionality is there, the only thing missing is autocomplete. I haven't managed to wrap my head around how to get that to work well.

Here's the current code:

decl -docstring "A list of directories for the `colorscheme` command to search through" \
    str-list colorscheme_sources "%val{runtime}/colors" "%val{config}/colors"

def -override -params 1 -docstring "colorscheme <name>: enable named colorscheme" \
    -shell-script-candidates %{
    # TODO: This needs to pull from all directories in $kak_quoted_opt_colorscheme_sources
    find -L "${kak_runtime}/colors" "${kak_config}/colors" -type f -name '*\.kak' \
        | while read -r filename; do
            basename="${filename##*/}"
            printf %s\\n "${basename%.*}"
        done | sort -u
  } \
  colorscheme %{ evaluate-commands %sh{
    find_colorscheme() {
        find -L "${1}" -type f -name "${2}".kak | head -n 1
    }

    scheme_name=$1
    filename=""

    eval set -- "$kak_quoted_opt_colorscheme_sources"
    for source; do
        if [ -d "${source}" ]; then
            filename=$(find_colorscheme "${source}" "${scheme_name}" || ${filename})
        fi
        shift
    done

    if [ -n "${filename}" ]; then
        printf 'source %%{%s}' "${filename}"
    else
        echo "fail 'No such colorscheme ${scheme_name}.kak'"
    fi
}}
krobelus commented 3 years ago

I think you can do

eval set -- "$kak_quoted_opt_colorscheme_sources"
find -L "$@" "${kak_runtime}/colors" "${kak_config}/colors" -type f -name '*\.kak' \
raiguard commented 3 years ago

Wow, you're a genius! I was unaware of $@ and it solves the problem perfectly. That's a lot nicer than anything I would have come up with.

image

lenormf commented 3 years ago

Dupe of #2402

alexherbo2 commented 3 years ago

How about revisiting the color scheme implementation?

We could explore #4152 and implement color schemes as:

Dracula theme

Declaring theme

declare-scope dracula-theme
set-face dracula-theme ...

Activating theme

link-scope window dracula-theme
raiguard commented 3 years ago

I'm pretty OK with the way it works now, assuming the PR gets merged. As long as you're not changing colorschemes every ten minutes, it does the job. There are plenty of other things (i.e. scrollable info boxes) that I would much rather have energy spent on.

caksoylar commented 3 years ago

Note that the speed of the colorscheme command also affects Kakoune startup time for most people, so it is worth keeping it light if possible.