fork/home-manager

git clone https://git.julia.blue/fork/home-manager

Robert HelgessonMerge PR #421575790441

master
19.3 KiB669 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7  cfg = config.programs.kakoune;
8
9  hook = types.submodule {
10    options = {
11      name = mkOption {
12        type = types.enum [
13          "NormalIdle"
14          "NormalKey"
15          "InsertIdle"
16          "InsertKey"
17          "InsertChar"
18          "InsertDelete"
19          "InsertMove"
20          "WinCreate"
21          "WinClose"
22          "WinResize"
23          "WinDisplay"
24          "WinSetOption"
25          "BufSetOption"
26          "BufNewFile"
27          "BufOpenFile"
28          "BufCreate"
29          "BufWritePre"
30          "BufWritePost"
31          "BufReload"
32          "BufClose"
33          "BufOpenFifo"
34          "BufReadFifo"
35          "BufCloseFifo"
36          "RuntimeError"
37          "ModeChange"
38          "PromptIdle"
39          "GlobalSetOption"
40          "KakBegin"
41          "KakEnd"
42          "FocusIn"
43          "FocusOut"
44          "RawKey"
45          "InsertCompletionShow"
46          "InsertCompletionHide"
47          "ModuleLoaded"
48          "ClientCreate"
49          "ClientClose"
50          "RegisterModified"
51          "User"
52        ];
53        example = "SetOption";
54        description = ''
55          The name of the hook. For a description, see
56          <https://github.com/mawww/kakoune/blob/master/doc/pages/hooks.asciidoc#default-hooks>.
57        '';
58      };
59
60      once = mkOption {
61        type = types.bool;
62        default = false;
63        description = ''
64          Remove the hook after running it once.
65        '';
66      };
67
68      group = mkOption {
69        type = types.nullOr types.str;
70        default = null;
71        description = ''
72          Add the hook to the named group.
73        '';
74      };
75
76      option = mkOption {
77        type = types.nullOr types.str;
78        default = null;
79        example = "filetype=latex";
80        description = ''
81          Additional option to pass to the hook.
82        '';
83      };
84
85      commands = mkOption {
86        type = types.lines;
87        default = "";
88        example = "set-option window indentwidth 2";
89        description = ''
90          Commands to run when the hook is activated.
91        '';
92      };
93    };
94  };
95
96  keyMapping = types.submodule {
97    options = {
98      mode = mkOption {
99        type = types.str;
100        example = "user";
101        description = ''
102          The mode in which the mapping takes effect.
103        '';
104      };
105
106      docstring = mkOption {
107        type = types.nullOr types.str;
108        default = null;
109        description = ''
110          Optional documentation text to display in info boxes.
111        '';
112      };
113
114      key = mkOption {
115        type = types.str;
116        example = "<a-x>";
117        description = ''
118          The key to be mapped. See
119          <https://github.com/mawww/kakoune/blob/master/doc/pages/mapping.asciidoc#mappable-keys>
120          for possible values.
121        '';
122      };
123
124      effect = mkOption {
125        type = types.str;
126        example = ":wq<ret>";
127        description = ''
128          The sequence of keys to be mapped.
129        '';
130      };
131    };
132  };
133
134  configModule = types.submodule {
135    options = {
136      colorScheme = mkOption {
137        type = types.nullOr types.str;
138        default = null;
139        description = ''
140          Set the color scheme. To see available schemes, enter
141          {command}`colorscheme` at the kakoune prompt.
142        '';
143      };
144
145      tabStop = mkOption {
146        type = types.nullOr types.ints.unsigned;
147        default = null;
148        description = ''
149          The width of a tab in spaces. The kakoune default is
150          `6`.
151        '';
152      };
153
154      indentWidth = mkOption {
155        type = types.nullOr types.ints.unsigned;
156        default = null;
157        description = ''
158          The width of an indentation in spaces.
159          The kakoune default is `4`.
160          If `0`, a tab will be used instead.
161        '';
162      };
163
164      incrementalSearch = mkOption {
165        type = types.bool;
166        default = true;
167        description = ''
168          Execute a search as it is being typed.
169        '';
170      };
171
172      alignWithTabs = mkOption {
173        type = types.bool;
174        default = false;
175        description = ''
176          Use tabs for the align command.
177        '';
178      };
179
180      autoInfo = mkOption {
181        type = types.nullOr
182          (types.listOf (types.enum [ "command" "onkey" "normal" ]));
183        default = null;
184        example = [ "command" "normal" ];
185        description = ''
186          Contexts in which to display automatic information box.
187          The kakoune default is `[ "command" "onkey" ]`.
188        '';
189      };
190
191      autoComplete = mkOption {
192        type = types.nullOr (types.listOf (types.enum [ "insert" "prompt" ]));
193        default = null;
194        description = ''
195          Modes in which to display possible completions.
196          The kakoune default is `[ "insert" "prompt" ]`.
197        '';
198      };
199
200      autoReload = mkOption {
201        type = types.nullOr (types.enum [ "yes" "no" "ask" ]);
202        default = null;
203        description = ''
204          Reload buffers when an external modification is detected.
205          The kakoune default is `"ask"`.
206        '';
207      };
208
209      scrollOff = mkOption {
210        type = types.nullOr (types.submodule {
211          options = {
212            lines = mkOption {
213              type = types.ints.unsigned;
214              default = 0;
215              description = ''
216                The number of lines to keep visible around the cursor.
217              '';
218            };
219
220            columns = mkOption {
221              type = types.ints.unsigned;
222              default = 0;
223              description = ''
224                The number of columns to keep visible around the cursor.
225              '';
226            };
227          };
228        });
229        default = null;
230        description = ''
231          How many lines and columns to keep visible around the cursor.
232        '';
233      };
234
235      ui = mkOption {
236        type = types.nullOr (types.submodule {
237          options = {
238            setTitle = mkOption {
239              type = types.bool;
240              default = false;
241              description = ''
242                Change the title of the terminal emulator.
243              '';
244            };
245
246            statusLine = mkOption {
247              type = types.enum [ "top" "bottom" ];
248              default = "bottom";
249              description = ''
250                Where to display the status line.
251              '';
252            };
253
254            assistant = mkOption {
255              type = types.enum [ "clippy" "cat" "dilbert" "none" ];
256              default = "clippy";
257              description = ''
258                The assistant displayed in info boxes.
259              '';
260            };
261
262            enableMouse = mkOption {
263              type = types.bool;
264              default = false;
265              description = ''
266                Whether to enable mouse support.
267              '';
268            };
269
270            changeColors = mkOption {
271              type = types.bool;
272              default = true;
273              description = ''
274                Change color palette.
275              '';
276            };
277
278            wheelDownButton = mkOption {
279              type = types.nullOr types.str;
280              default = null;
281              description = ''
282                Button to send for wheel down events.
283              '';
284            };
285
286            wheelUpButton = mkOption {
287              type = types.nullOr types.str;
288              default = null;
289              description = ''
290                Button to send for wheel up events.
291              '';
292            };
293
294            shiftFunctionKeys = mkOption {
295              type = types.nullOr types.ints.unsigned;
296              default = null;
297              description = ''
298                Amount by which shifted function keys are offset. That
299                is, if the terminal sends F13 for Shift-F1, this
300                should be `12`.
301              '';
302            };
303
304            useBuiltinKeyParser = mkOption {
305              type = types.bool;
306              default = false;
307              description = ''
308                Bypass ncurses key parser and use an internal one.
309              '';
310            };
311          };
312        });
313        default = null;
314        description = ''
315          Settings for the ncurses interface.
316        '';
317      };
318
319      showMatching = mkOption {
320        type = types.bool;
321        default = false;
322        description = ''
323          Highlight the matching char of the character under the
324          selections' cursor using the `MatchingChar`
325          face.
326        '';
327      };
328
329      wrapLines = mkOption {
330        type = types.nullOr (types.submodule {
331          options = {
332            enable = mkEnableOption "the wrap lines highlighter";
333
334            word = mkOption {
335              type = types.bool;
336              default = false;
337              description = ''
338                Wrap at word boundaries instead of codepoint boundaries.
339              '';
340            };
341
342            indent = mkOption {
343              type = types.bool;
344              default = false;
345              description = ''
346                Preserve line indentation when wrapping.
347              '';
348            };
349
350            maxWidth = mkOption {
351              type = types.nullOr types.ints.unsigned;
352              default = null;
353              description = ''
354                Wrap text at maxWidth, even if the window is wider.
355              '';
356            };
357
358            marker = mkOption {
359              type = types.nullOr types.str;
360              default = null;
361              example = "⏎";
362              description = ''
363                Prefix wrapped lines with marker text.
364                If not `null`,
365                the marker text will be displayed in the indentation if possible.
366              '';
367            };
368          };
369        });
370        default = null;
371        description = ''
372          Settings for the wrap lines highlighter.
373        '';
374      };
375
376      numberLines = mkOption {
377        type = types.nullOr (types.submodule {
378          options = {
379            enable = mkEnableOption "the number lines highlighter";
380
381            relative = mkOption {
382              type = types.bool;
383              default = false;
384              description = ''
385                Show line numbers relative to the main cursor line.
386              '';
387            };
388
389            highlightCursor = mkOption {
390              type = types.bool;
391              default = false;
392              description = ''
393                Highlight the cursor line with a separate face.
394              '';
395            };
396
397            separator = mkOption {
398              type = types.nullOr types.str;
399              default = null;
400              description = ''
401                String that separates the line number column from the
402                buffer contents. The kakoune default is
403                `"|"`.
404              '';
405            };
406          };
407        });
408        default = null;
409        description = ''
410          Settings for the number lines highlighter.
411        '';
412      };
413
414      showWhitespace = mkOption {
415        type = types.nullOr (types.submodule {
416          options = {
417            enable = mkEnableOption "the show whitespace highlighter";
418
419            lineFeed = mkOption {
420              type = types.nullOr types.str;
421              default = null;
422              description = ''
423                The character to display for line feeds.
424                The kakoune default is `"¬"`.
425              '';
426            };
427
428            space = mkOption {
429              type = types.nullOr types.str;
430              default = null;
431              description = ''
432                The character to display for spaces.
433                The kakoune default is `"·"`.
434              '';
435            };
436
437            nonBreakingSpace = mkOption {
438              type = types.nullOr types.str;
439              default = null;
440              description = ''
441                The character to display for non-breaking spaces.
442                The kakoune default is `"⍽"`.
443              '';
444            };
445
446            tab = mkOption {
447              type = types.nullOr types.str;
448              default = null;
449              description = ''
450                The character to display for tabs.
451                The kakoune default is `"→"`.
452              '';
453            };
454
455            tabStop = mkOption {
456              type = types.nullOr types.str;
457              default = null;
458              description = ''
459                The character to append to tabs to reach the width of a tabstop.
460                The kakoune default is `" "`.
461              '';
462            };
463          };
464        });
465        default = null;
466        description = ''
467          Settings for the show whitespaces highlighter.
468        '';
469      };
470
471      keyMappings = mkOption {
472        type = types.listOf keyMapping;
473        default = [ ];
474        description = ''
475          User-defined key mappings. For documentation, see
476          <https://github.com/mawww/kakoune/blob/master/doc/pages/mapping.asciidoc>.
477        '';
478      };
479
480      hooks = mkOption {
481        type = types.listOf hook;
482        default = [ ];
483        description = ''
484          Global hooks. For documentation, see
485          <https://github.com/mawww/kakoune/blob/master/doc/pages/hooks.asciidoc>.
486        '';
487      };
488    };
489  };
490
491  kakouneWithPlugins =
492    pkgs.wrapKakoune cfg.package { configure = { plugins = cfg.plugins; }; };
493
494  configFile = let
495    wrapOptions = with cfg.config.wrapLines;
496      concatStrings [
497        "${optionalString word " -word"}"
498        "${optionalString indent " -indent"}"
499        "${optionalString (marker != null) " -marker ${marker}"}"
500        "${optionalString (maxWidth != null) " -width ${toString maxWidth}"}"
501      ];
502
503    numberLinesOptions = with cfg.config.numberLines;
504      concatStrings [
505        "${optionalString relative " -relative "}"
506        "${optionalString highlightCursor " -hlcursor"}"
507        "${optionalString (separator != null) " -separator ${separator}"}"
508      ];
509
510    showWhitespaceOptions = with cfg.config.showWhitespace;
511      let
512        quoteSep = sep:
513          if sep == "'" then
514            ''"'"''
515          else if lib.strings.stringLength sep == 1 then
516            "'${sep}'"
517          else
518            sep; # backwards compat, in case sep == "' '", etc.
519
520      in concatStrings [
521        (optionalString (tab != null) " -tab ${quoteSep tab}")
522        (optionalString (tabStop != null) " -tabpad ${quoteSep tabStop}")
523        (optionalString (space != null) " -spc ${quoteSep space}")
524        (optionalString (nonBreakingSpace != null)
525          " -nbsp ${quoteSep nonBreakingSpace}")
526        (optionalString (lineFeed != null) " -lf ${quoteSep lineFeed}")
527      ];
528
529    uiOptions = with cfg.config.ui;
530      concatStringsSep " " [
531        "terminal_set_title=${if setTitle then "true" else "false"}"
532        "terminal_status_on_top=${
533          if (statusLine == "top") then "true" else "false"
534        }"
535        "terminal_assistant=${assistant}"
536        "terminal_enable_mouse=${if enableMouse then "true" else "false"}"
537        "terminal_change_colors=${if changeColors then "true" else "false"}"
538        "${optionalString (wheelDownButton != null)
539        "terminal_wheel_down_button=${wheelDownButton}"}"
540        "${optionalString (wheelUpButton != null)
541        "terminal_wheel_up_button=${wheelUpButton}"}"
542        "${optionalString (shiftFunctionKeys != null)
543        "terminal_shift_function_key=${toString shiftFunctionKeys}"}"
544        "terminal_builtin_key_parser=${
545          if useBuiltinKeyParser then "true" else "false"
546        }"
547      ];
548
549    userModeString = mode:
550      optionalString (!builtins.elem mode [
551        "insert"
552        "normal"
553        "prompt"
554        "menu"
555        "user"
556        "goto"
557        "view"
558        "object"
559      ]) "try %{declare-user-mode ${mode}}";
560
561    userModeStrings = map userModeString
562      (lists.unique (map (km: km.mode) cfg.config.keyMappings));
563
564    keyMappingString = km:
565      concatStringsSep " " [
566        "map global"
567        "${km.mode} ${km.key} '${km.effect}'"
568        "${optionalString (km.docstring != null)
569        "-docstring '${km.docstring}'"}"
570      ];
571
572    hookString = h:
573      concatStringsSep " " [
574        "hook"
575        "${optionalString (h.group != null) "-group ${h.group}"}"
576        "${optionalString (h.once) "-once"}"
577        "global"
578        "${h.name}"
579        "${optionalString (h.option != null) h.option}"
580        "%{ ${h.commands} }"
581      ];
582
583    cfgStr = with cfg.config;
584      concatStringsSep "\n" ([ "# Generated by home-manager" ]
585        ++ optional (colorScheme != null) "colorscheme ${colorScheme}"
586        ++ optional (tabStop != null)
587        "set-option global tabstop ${toString tabStop}"
588        ++ optional (indentWidth != null)
589        "set-option global indentwidth ${toString indentWidth}"
590        ++ optional (!incrementalSearch) "set-option global incsearch false"
591        ++ optional (alignWithTabs) "set-option global aligntab true"
592        ++ optional (autoInfo != null)
593        "set-option global autoinfo ${concatStringsSep "|" autoInfo}"
594        ++ optional (autoComplete != null)
595        "set-option global autocomplete ${concatStringsSep "|" autoComplete}"
596        ++ optional (autoReload != null)
597        "set-option global autoreload ${autoReload}"
598        ++ optional (wrapLines != null && wrapLines.enable)
599        "add-highlighter global/ wrap${wrapOptions}"
600        ++ optional (numberLines != null && numberLines.enable)
601        "add-highlighter global/ number-lines${numberLinesOptions}"
602        ++ optional showMatching "add-highlighter global/ show-matching"
603        ++ optional (showWhitespace != null && showWhitespace.enable)
604        "add-highlighter global/ show-whitespaces${showWhitespaceOptions}"
605        ++ optional (scrollOff != null)
606        "set-option global scrolloff ${toString scrollOff.lines},${
607          toString scrollOff.columns
608        }"
609
610        ++ [ "# UI options" ]
611        ++ optional (ui != null) "set-option global ui_options ${uiOptions}"
612
613        ++ [ "# User modes" ] ++ userModeStrings ++ [ "# Key mappings" ]
614        ++ map keyMappingString keyMappings
615
616        ++ [ "# Hooks" ] ++ map hookString hooks);
617  in pkgs.writeText "kakrc"
618  (optionalString (cfg.config != null) cfgStr + "\n" + cfg.extraConfig);
619
620in {
621  options = {
622    programs.kakoune = {
623      enable = mkEnableOption "the kakoune text editor";
624
625      package = mkPackageOption pkgs "kakoune-unwrapped" { };
626
627      config = mkOption {
628        type = types.nullOr configModule;
629        default = { };
630        description = "kakoune configuration options.";
631      };
632
633      defaultEditor = mkOption {
634        type = types.bool;
635        default = false;
636        description = ''
637          Whether to configure {command}`kak` as the default
638          editor using the {env}`EDITOR` environment variable.
639        '';
640      };
641
642      extraConfig = mkOption {
643        type = types.lines;
644        default = "";
645        description = ''
646          Extra configuration lines to add to
647          {file}`$XDG_CONFIG_HOME/kak/kakrc`.
648        '';
649      };
650
651      plugins = mkOption {
652        type = with types; listOf package;
653        default = [ ];
654        example = literalExpression "[ pkgs.kakounePlugins.kak-fzf ]";
655        description = ''
656          List of kakoune plugins to install. To get a list of
657          supported plugins run:
658          {command}`nix-env -f '<nixpkgs>' -qaP -A kakounePlugins`.
659        '';
660      };
661    };
662  };
663
664  config = mkIf cfg.enable {
665    home.packages = [ kakouneWithPlugins ];
666    home.sessionVariables = mkIf cfg.defaultEditor { EDITOR = "kak"; };
667    xdg.configFile."kak/kakrc".source = configFile;
668  };
669}