summary refs log tree commit diff
path: root/nixos/modules/programs/less.nix
blob: 9f2d5d915815985275df2016065a7a5b710e2d3b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
{ config, lib, pkgs, ... }:

with lib;

let

  cfg = config.programs.less;

  configText = if (cfg.configFile != null) then (builtins.readFile cfg.configFile) else ''
    #command
    ${concatStringsSep "\n"
      (mapAttrsToList (command: action: "${command} ${action}") cfg.commands)
    }
    ${if cfg.clearDefaultCommands then "#stop" else ""}

    #line-edit
    ${concatStringsSep "\n"
      (mapAttrsToList (command: action: "${command} ${action}") cfg.lineEditingKeys)
    }

    #env
    ${concatStringsSep "\n"
      (mapAttrsToList (variable: values: "${variable}=${values}") cfg.envVariables)
    }
  '';

  lessKey = pkgs.writeText "lessconfig" configText;

in

{
  options = {

    programs.less = {

      # note that environment.nix sets PAGER=less, and
      # therefore also enables this module
      enable = mkEnableOption "less";

      configFile = mkOption {
        type = types.nullOr types.path;
        default = null;
        example = literalExpression ''"''${pkgs.my-configs}/lesskey"'';
        description = lib.mdDoc ''
          Path to lesskey configuration file.

          {option}`configFile` takes precedence over {option}`commands`,
          {option}`clearDefaultCommands`, {option}`lineEditingKeys`, and
          {option}`envVariables`.
        '';
      };

      commands = mkOption {
        type = types.attrsOf types.str;
        default = {};
        example = {
          h = "noaction 5\\e(";
          l = "noaction 5\\e)";
        };
        description = lib.mdDoc "Defines new command keys.";
      };

      clearDefaultCommands = mkOption {
        type = types.bool;
        default = false;
        description = lib.mdDoc ''
          Clear all default commands.
          You should remember to set the quit key.
          Otherwise you will not be able to leave less without killing it.
        '';
      };

      lineEditingKeys = mkOption {
        type = types.attrsOf types.str;
        default = {};
        example = {
          e = "abort";
        };
        description = lib.mdDoc "Defines new line-editing keys.";
      };

      envVariables = mkOption {
        type = types.attrsOf types.str;
        default = {
          LESS = "-R";
        };
        example = {
          LESS = "--quit-if-one-screen";
        };
        description = lib.mdDoc "Defines environment variables.";
      };

      lessopen = mkOption {
        type = types.nullOr types.str;
        default = "|${pkgs.lesspipe}/bin/lesspipe.sh %s";
        defaultText = literalExpression ''"|''${pkgs.lesspipe}/bin/lesspipe.sh %s"'';
        description = lib.mdDoc ''
          Before less opens a file, it first gives your input preprocessor a chance to modify the way the contents of the file are displayed.
        '';
      };

      lessclose = mkOption {
        type = types.nullOr types.str;
        default = null;
        description = lib.mdDoc ''
          When less closes a file opened in such a way, it will call another program, called the input postprocessor, which may  perform  any  desired  clean-up  action (such  as deleting the replacement file created by LESSOPEN).
        '';
      };
    };
  };

  config = mkIf cfg.enable {

    environment.systemPackages = [ pkgs.less ];

    environment.variables = {
      LESSKEYIN_SYSTEM = toString lessKey;
    } // optionalAttrs (cfg.lessopen != null) {
      LESSOPEN = cfg.lessopen;
    } // optionalAttrs (cfg.lessclose != null) {
      LESSCLOSE = cfg.lessclose;
    };

    warnings = optional (
      cfg.clearDefaultCommands && (all (x: x != "quit") (attrValues cfg.commands))
    ) ''
      config.programs.less.clearDefaultCommands clears all default commands of less but there is no alternative binding for exiting.
      Consider adding a binding for 'quit'.
    '';
  };

  meta.maintainers = with maintainers; [ johnazoidberg ];

}