Skip to content

Commit

Permalink
add kate editor theme config
Browse files Browse the repository at this point in the history
  • Loading branch information
blitter committed Mar 25, 2024
1 parent 8a032af commit 2818d4b
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 3 deletions.
47 changes: 47 additions & 0 deletions modules/apps/check-theme-name-free.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# there could be a bash shebang to ${pkgs.bash}/bin/bash here

# https://stackoverflow.com/questions/5947742/how-to-change-the-output-color-of-echo-in-linux/5947802#5947802
RED='\033[0;31m'

# https://stackoverflow.com/questions/2924697/how-does-one-output-bold-text-in-bash/2924755#2924755
BOLD=$(tput bold)
NORMAL=$(tput sgr0)


# # =====================================
# # CHECK THE NUMBER OF ARGS
# #
# # https://www.baeldung.com/linux/bash-check-script-arguments

if [[ "$#" -ne 2 ]]; then
# https://stackoverflow.com/questions/3005963/how-can-i-have-a-newline-in-a-string-in-sh/3182519#3182519
>&2 printf "${RED}${BOLD}Incorrect number of arguments.${NORMAL}${RED} Expected three:\n * Name of the theme that should not already be in use\n * the path to the jq executable"
exit 1
fi

THEMENAME=$1
jqexec=$2


# =====================================
# GO THROUGH THE THEMES
#
# reference the XDG dir as proposed in https://github.com/nix-community/home-manager/pull/4594#issuecomment-1774024207

THEME_DIR="${XDG_DATA_HOME:-$HOME/.local/share}/org.kde.syntax-highlighting/themes/"

ls "${THEME_DIR}" | while read -r themefile; do
FULL_PATH="${THEME_DIR}/${themefile}"
THIS_THEMENAME=$(${jqexec} -r .metadata.name "${FULL_PATH}")
# TODO skip if file is a symlink to /nix/store"

if [[ "${THIS_THEMENAME}" == "${THEMENAME}" ]]; then
# make sure to not look at symbolic links to the nix store
# https://stackoverflow.com/questions/17918367/linux-shell-verify-whether-a-file-exists-and-is-a-soft-link/17918442#17918442
# https://stackoverflow.com/questions/2172352/in-bash-how-can-i-check-if-a-string-begins-with-some-value/2172367#2172367
if [[ ! ( -L "${FULL_PATH}" && $(readlink -f "${FULL_PATH}") == /nix/store/* ) ]]; then
>&2 printf "${RED}${BOLD}In ${THEME_DIR} there is already a theme with the name ${THEMENAME} (${themefile}).${NORMAL}${RED} You could rename the theme given in config.programs.kate.editor.theme.src by changing the value for metadta.name inside the theme."
exit 1 # even on dryrun
fi
fi
done
57 changes: 54 additions & 3 deletions modules/apps/kate.nix
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
{ config, lib, ... }:


{ config, lib, pkgs, ... }:

let
cfg = config.programs.kate;

# compute kate's magic TabHandlingMode
# 0 is not tab & not undoByShiftTab
# 1 is tab & undoByShiftTab
Expand All @@ -20,6 +19,9 @@ in
enable = lib.mkEnableOption ''
Enable configuration management for kate.
'';

# ==================================
# INDENTATION
editor = {
tabWidth = lib.mkOption {
description = "The width of a single tab (''\t) sign (in number of spaces).";
Expand Down Expand Up @@ -82,6 +84,10 @@ in
assertion = cfg.editor.indent.undoByShiftTab || (!cfg.editor.indent.tabFromEverywhere);
message = "Kate does not support both 'undoByShiftTab' to be disabled and 'tabFromEverywhere' to be enabled at the same time.";
}
{
assertion = (! cfg.enable) || (builtins.elem pkgs.libsForQt5.kate config.home.packages);
message = "Kate cannot be managed with plasma-manager if it is not installed. Either install kate or disable `config.programs.kate.enabled";
}
];

config.programs.plasma.configFile."katerc" = lib.mkIf cfg.enable {
Expand All @@ -96,6 +102,51 @@ in

"KTextEditor Renderer" = {
"Show Indentation Lines" = cfg.editor.indent.showLines;


# COLORTHEME (cannot define this below)
# Do pick the theme if the user chose one,
# Do not touch the theme settings otherwise
"Auto Color Theme Selection" = lib.mkIf (cfg.editor.theme.name != "") false;
"Color Theme" = lib.mkIf (cfg.editor.theme.name != "") cfg.editor.theme.name;
};
};


# ==================================
# COLORTHEME

options.programs.kate.editor.theme = {
src = lib.mkOption {
description = "The path of a theme file for the KDE editor (not the window color scheme). Obtain a custom one by using the GUI settings in kate. If you want to use a system-wide editor color scheme set this path to null. If you set the metadata.name entry in the file to a value that matches the name of a system-wide color scheme undesired behaviour may occur. The activation will fail if a theme with the filename `<name of your theme>.theme` already exists.";
type = lib.types.nullOr lib.types.path;
default = null;
};

name = lib.mkOption {
description = "The name of the theme in use. May be a system theme. If a theme file was submitted this setting will be set automatically.";
type = lib.types.str;
default = "";
};
};

config.programs.kate.editor.theme = {
name = lib.mkIf (null != cfg.editor.theme.src) (lib.mkForce (builtins.fromJSON (builtins.readFile cfg.editor.theme.src))."metadata"."name");
# kate's naming scheme is ${themename}.theme
# which is why we use the same naming scheme here
};

# This won't override existing files since the home-manager activation fails in that case
config.xdg.dataFile."${cfg.editor.theme.name}.theme" = lib.mkIf (null != cfg.editor.theme.src)
{
source = cfg.editor.theme.src;
target = "org.kde.syntax-highlighting/themes/${cfg.editor.theme.name}.theme";
};

config = {
home.activation.checkKateTheme = lib.mkIf (cfg.enable && config.programs.kate.editor.theme.src != null) (lib.hm.dag.entryBefore [ "writeBoundary" ]
''
$DRY_RUN_CMD ${./check-theme-name-free.sh} ${cfg.editor.theme.name} ${pkgs.jq}/bin/jq
'');
};
}

0 comments on commit 2818d4b

Please sign in to comment.