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