From aa974c0dc3a27c2d68a9777a6554f67be5a80fdd Mon Sep 17 00:00:00 2001 From: Robert Helgesson Date: Thu, 21 Sep 2017 00:15:08 +0200 Subject: [PATCH] vim: add option `programs.vim.settings` This option gathers basic Vim options into a single place. The idea is to allow many options without making the Home Manager documentation too verbose. This also deprecates the options `programs.vim.lineNumbers` and `programs.vim.tabSize`. Fixes #69. --- modules/misc/news.nix | 19 +++++++ modules/programs/vim.nix | 115 +++++++++++++++++++++++++++++++++++---- 2 files changed, 122 insertions(+), 12 deletions(-) diff --git a/modules/misc/news.nix b/modules/misc/news.nix index 025f69c0e..3c3058701 100644 --- a/modules/misc/news.nix +++ b/modules/misc/news.nix @@ -255,6 +255,25 @@ in 'xsession.windowManager.i3'. ''; } + + { + time = "2017-09-30T09:44:18+00:00"; + condition = with config.programs.vim; + enable && (tabSize != null || lineNumbers != null); + message = '' + The options 'programs.vim.tabSize' and 'programs.vim.lineNumbers' have + been deprecated and will be removed in the near future. + + The new and preferred way to configure tab size and line numbers is to + use the more general 'programs.vim.settings' option. Specifically, + instead of + + - 'programs.vim.lineNumbers' use 'programs.vim.settings.number', and + + - 'programs.vim.tabSize' use 'programs.vim.settings.tabstop' and + 'programs.vim.settings.shiftwidth'. + ''; + } ]; }; } diff --git a/modules/programs/vim.nix b/modules/programs/vim.nix index b96adeacb..8f9430160 100644 --- a/modules/programs/vim.nix +++ b/modules/programs/vim.nix @@ -7,6 +7,36 @@ let cfg = config.programs.vim; defaultPlugins = [ "sensible" ]; + knownSettings = { + background = types.enum [ "dark" "light" ]; + expandtab = types.bool; + history = types.int; + number = types.bool; + relativenumber = types.bool; + shiftwidth = types.int; + tabstop = types.int; + }; + + vimSettingsType = types.submodule { + options = + let + opt = name: type: mkOption { + type = types.nullOr type; + default = null; + visible = false; + }; + in + mapAttrs opt knownSettings; + }; + + setExpr = name: value: + let + v = + if isBool value then (if value then "" else "no") + name + else name + "=" + toString value; + in + optionalString (value != null) ("set " + v); + in { @@ -17,14 +47,22 @@ in lineNumbers = mkOption { type = types.nullOr types.bool; default = null; - description = "Whether to show line numbers."; + description = '' + Whether to show line numbers. DEPRECATED: Use + programs.vim.settings.number. + ''; }; tabSize = mkOption { type = types.nullOr types.int; - default = null; + default = null; example = 4; - description = "Set tab size and shift width to a specified number of spaces."; + description = '' + Set tab size and shift width to a specified number of + spaces. DEPRECATED: Use + programs.vim.settings.tabstop and + programs.vim.settings.shiftwidth. + ''; }; plugins = mkOption { @@ -37,6 +75,38 @@ in ''; }; + settings = mkOption { + type = vimSettingsType; + default = {}; + example = literalExample '' + { + expandtab = true; + history = 1000; + background = "dark"; + } + ''; + description = '' + At attribute set of Vim settings. The attribute names and + corresponding values must be among the following supported + options. + + + ${concatStringsSep "\n" ( + mapAttrsToList (n: v: '' + + ${n} + ${v.description} + + '') knownSettings + )} + + + See the Vim documentation for detailed descriptions of these + options. Note, use extraConfig to + manually set any options not listed above. + ''; + }; + extraConfig = mkOption { type = types.lines; default = ""; @@ -57,12 +127,11 @@ in config = ( let - optionalBoolean = name: val: optionalString (val != null) (if val then "set ${name}" else "unset ${name}"); - optionalInteger = name: val: optionalString (val != null) "set ${name}=${toString val}"; customRC = '' - ${optionalBoolean "number" cfg.lineNumbers} - ${optionalInteger "tabstop" cfg.tabSize} - ${optionalInteger "shiftwidth" cfg.tabSize} + ${concatStringsSep "\n" ( + filter (v: v != "") ( + mapAttrsToList setExpr ( + builtins.intersectAttrs knownSettings cfg.settings)))} ${cfg.extraConfig} ''; @@ -76,9 +145,31 @@ in ]; }; - in mkIf cfg.enable { - programs.vim.package = vim; - home.packages = [ cfg.package ]; - } + in mkIf cfg.enable (mkMerge [ + { + programs.vim.package = vim; + home.packages = [ cfg.package ]; + } + + (mkIf (cfg.lineNumbers != null) { + warnings = [ + ("'programs.vim.lineNumbers' is deprecated, " + + "use 'programs.vim.settings.number'") + ]; + + programs.vim.settings.number = cfg.lineNumbers; + }) + + (mkIf (cfg.tabSize != null) { + warnings = [ + ("'programs.vim.tabSize' is deprecated, use " + + "'programs.vim.settings.tabstop' and " + + "'programs.vim.settings.shiftwidth'") + ]; + + programs.vim.settings.tabstop = cfg.tabSize; + programs.vim.settings.shiftwidth = cfg.tabSize; + }) + ]) ); }