{ pkgs, lib, ... }:

{
  # TODO: upstream this
  imports         = [ ../../modules/micro.nix ];
  disabledModules = [ "programs/micro.nix" ];

  home.shellAliases.ed = "$EDITOR"; # ed is the standard editor
  home.shellAliases.de = "$EDITOR"; # de is the standard typo
  home.sessionVariables.EDITOR = "micro";

  home.packages = [
    pkgs.micro
    pkgs.aspell
    pkgs.aspellDicts.en
    pkgs.aspellDicts.nb
  ];

  programs.bash.initExtra = ''
    if [ "$COLORTERM" = "truecolor" ] || [ "$TERM" == "xterm" ] || [ "$TERM" == "xterm-256color" ]; then
	    # export TERM=xterm-256color
	    export MICRO_TRUECOLOR=1
    fi
  '';

  xdg.configFile."micro/settings.json".force = true;
  xdg.configFile."micro/bindings.json".force = true;

  programs.micro.enable = true;
  programs.micro.trueColor = true;
  programs.micro.settings.colorscheme = "railscast";
  programs.micro.settings.savecursor = true;
  programs.micro.settings.rmtrailingws = true;
  programs.micro.settings.tabstospaces = true;
  programs.micro.settings.autoclose = false;
  #xdg.configFile."micro/bindings.json".source = (pkgs.formats.json {}).generate "micro-bindings" {
  programs.micro.bindings = {
    "Alt-/"          = "lua:comment.comment";
    "Alt-d"          = "SpawnMultiCursor";
    "Alt-j"          = "lua:joinLines.joinLines";
    "Alt-l"          = "command:lower";
    "Alt-u"          = "command:upper";
    "AltLeft"        = "PreviousTab";
    "AltRight"       = "NextTab";
    "Ctrl-j"         = "EndOfLine,CursorRight,OutdentLine,OutdentLine,OutdentLine,OutdentLine,OutdentLine,OutdentLine,OutdentLine,OutdentLine,OutdentLine,OutdentLine,OutdentLine,OutdentLine,OutdentLine,OutdentLine,Backspace";
    "CtrlDown"       = "None";
    "CtrlUnderscore" = "lua:comment.comment";
    "CtrlUp"         = "None";
    "Escape"         = "RemoveAllMultiCursors";
    "Shift-PageDown" = "SelectPageDown";
    "Shift-PageUp"   = "SelectPageUp";
    # TODO: test on ghostty
    "Ctrl-'"         = "lua:comment.comment"; # doesn't work on lxterminal, `> raw` shows `EventKey: ': "'"`
    "Alt-'"          = "lua:comment.comment"; # workaround
  };
  programs.micro.ensurePlugins = [
    "aspell"
    "detectindent"
    "editorconfig"
    "joinLines"
    "manipulator"
    "quoter"
  ];
}
