<commit_before>{ config, lib, pkgs, ... }:

with builtins;
with lib;

let
  cfg = config.custom.emacsConfig;

  emacsEdit = if cfg.enableUtils then
    "emacseditor"
  else
    (if enableServer then "emacsclient" else "emacs");

  extraPackages = import ./packages.nix;
  overrides = pkgs.callPackage ./overrides.nix { };

  mkEmacsConfigFiles = path:
    foldl' (acc: file: acc // { "emacs/${file}".source = "${path}/${file}"; })
    { } (attrNames (readDir path));
in {
  options.custom.emacsConfig = {
    enable = mkEnableOption "custom emacs configuration";

    package = mkOption {
      type = types.package;
      default = pkgs.emacsGit;
      defaultText = literalExample "pkgs.emacsGit";
      description = "The default Emacs derivation to use.";
    };

    enableOverrides = mkOption {
      default = true;
      type = types.bool;
      description = "Whether to enable Emacs package overrides.";
    };

    enablePackages = mkOption {
      default = true;
      type = types.bool;
      description = "Whether to enable extra Emacs packages.";
    };

    enableUserDirectory = mkOption {
      default = true;
      type = types.bool;
      description = "Whether to enable user Emacs directory files.";
    };

    enableGitDiff = mkOption {
      default = true;
      type = types.bool;
      description = "Whether to enable ediff as default git diff tool.";
    };

    enableServer = mkOption {
      default = true;
      type = types.bool;
      description = "Whether to enable user Emacs server.";
    };

    defaultEditor = mkOption {
      default = true;
      type = types.bool;
      description = "Whether to use Emacs as default editor.";
    };

    defaultEmailApplication = mkOption {
      default = false;
      type = types.bool;
      description = "Whether to use Emacs as default e-mail application.";
    };

    defaultPdfApplication = mkOption {
      default = false;
      type = types.bool;
      description = "Whether to use Emacs as default PDF application.";
    };

    enableUtils = mkOption {
      default = true;
      type = types.bool;
      description = "Whether to enable Emacs utils.";
    };
  };

  config = mkIf cfg.enable {
    programs.emacs = {
      enable = true;
      package = cfg.package;
      extraPackages = mkIf cfg.enablePackages extraPackages;
      overrides = mkIf cfg.enableOverrides overrides;
    };

    services.emacs.enable = cfg.enableServer;

    xdg.configFile =
      mkIf cfg.enableUserDirectory (mkEmacsConfigFiles pkgs.emacsConfig);

    home.sessionVariables = mkIf cfg.defaultEditor { EDITOR = emacsEdit; };

    programs.git.extraConfig = {
      diff.tool = mkIf cfg.enableGitDiff "ediff";

      "difftool \"ediff\"".cmd = ''
        ${emacsEdit} --eval '(ediff-files "'$LOCAL'" "'$REMOTE'")'
      '';

      "mergetool \"ediff\"".cmd = ''
        ${emacsEdit} --eval '(ediff-merge-files-with-ancestor "'$LOCAL'" "'$REMOTE'" "'$BASE'" nil "'$MERGED'")'
      '';
    };

    programs.qutebrowser.settings.editor.command =
      mkIf cfg.defaultEditor [ emacsEdit "{}" ];

    home.packages = [ ]
      ++ optionals cfg.enableUserDirectory pkgs.emacsConfig.buildInputs
      ++ optional cfg.enableUtils pkgs.emacsUtils;

    xdg.mimeApps.defaultApplications = {
      "x-scheme-handler/mailto" =
        mkIf (cfg.enableUtils && cfg.defaultEmailApplication)
        "emacsmail.desktop";
      "application/pdf" = mkIf (cfg.enableUtils && cfg.defaultPdfApplication)
        "emacseditor.desktop";
    };
  };
}<commit_msg>home-manager: fix optional config<commit_after>{ config, lib, pkgs, ... }:

with builtins;
with lib;

let
  cfg = config.custom.emacsConfig;

  emacsEdit = if cfg.enableUtils then
    "emacseditor"
  else
    (if enableServer then "emacsclient" else "emacs");

  extraPackages = import ./packages.nix;
  overrides = pkgs.callPackage ./overrides.nix { };

  mkEmacsConfigFiles = path:
    foldl' (acc: file: acc // { "emacs/${file}".source = "${path}/${file}"; })
    { } (attrNames (readDir path));
in {
  options.custom.emacsConfig = {
    enable = mkEnableOption "custom emacs configuration";

    package = mkOption {
      type = types.package;
      default = pkgs.emacsGit;
      defaultText = literalExample "pkgs.emacsGit";
      description = "The default Emacs derivation to use.";
    };

    enableOverrides = mkOption {
      default = true;
      type = types.bool;
      description = "Whether to enable Emacs package overrides.";
    };

    enablePackages = mkOption {
      default = true;
      type = types.bool;
      description = "Whether to enable extra Emacs packages.";
    };

    enableUserDirectory = mkOption {
      default = true;
      type = types.bool;
      description = "Whether to enable user Emacs directory files.";
    };

    enableGitDiff = mkOption {
      default = true;
      type = types.bool;
      description = "Whether to enable ediff as default git diff tool.";
    };

    enableServer = mkOption {
      default = true;
      type = types.bool;
      description = "Whether to enable user Emacs server.";
    };

    defaultEditor = mkOption {
      default = true;
      type = types.bool;
      description = "Whether to use Emacs as default editor.";
    };

    defaultEmailApplication = mkOption {
      default = false;
      type = types.bool;
      description = "Whether to use Emacs as default e-mail application.";
    };

    defaultPdfApplication = mkOption {
      default = false;
      type = types.bool;
      description = "Whether to use Emacs as default PDF application.";
    };

    enableUtils = mkOption {
      default = true;
      type = types.bool;
      description = "Whether to enable Emacs utils.";
    };
  };

  config = mkIf cfg.enable (mkMerge [
    {
      programs.emacs = mkMerge [
        {
          enable = true;
          package = cfg.package;
        }

        (mkIf cfg.enablePackages { inherit extraPackages; })
        (mkIf cfg.enableOverrides { inherit overrides; })
      ];

      services.emacs.enable = cfg.enableServer;

      programs.git.extraConfig = {
        "difftool \"ediff\"".cmd = ''
          ${emacsEdit} --eval '(ediff-files "'$LOCAL'" "'$REMOTE'")'
        '';

        "mergetool \"ediff\"".cmd = ''
          ${emacsEdit} --eval '(ediff-merge-files-with-ancestor "'$LOCAL'" "'$REMOTE'" "'$BASE'" nil "'$MERGED'")'
        '';
      };

      home.packages = [ ]
        ++ optionals cfg.enableUserDirectory pkgs.emacsConfig.buildInputs
        ++ optional cfg.enableUtils pkgs.emacsUtils;
    }
    (mkIf cfg.enableUserDirectory {
      xdg.configFile = mkEmacsConfigFiles pkgs.emacsConfig;
    })
    (mkIf cfg.defaultEditor { home.sessionVariables.EDITOR = emacsEdit; })
    (mkIf cfg.enableGitDiff { programs.git.extraConfig.diff.tool = "ediff"; })
    (mkIf cfg.defaultEditor {
      programs.qutebrowser.settings.editor.command = [ emacsEdit "{}" ];
    })
    (mkIf (cfg.enableUtils && cfg.defaultEmailApplication) {
      xdg.mimeApps.defaultApplications."x-scheme-handler/mailto" =
        "emacsmail.desktop";
    })
    (mkIf (cfg.enableUtils && cfg.defaultPdfApplication) {
      xdg.mimeApps.defaultApplications."application/pdf" =
        "emacseditor.desktop";
    })
  ]);
}
{'fraction_non_alphanumeric': 0.12420645873585426, 'fraction_numerical': 0.0, 'mean_word_length': 2.5128453708191953, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3464152', 'n_tokens_mistral': 2262, 'n_tokens_neox': 2170, 'n_words': 523}