Post

Get/Set Configuration Script

The lazyman menus include the capability to get and set configuration values for the Lazyman, LazyIde, and Webdev Neovim configurations. This is done using a new Neovim feature introduced in version 0.9, the ability to invoke Neovim with a Lua script:

1
nvim -l /path/to/some_script.lua

The specified Lua script is executed in the Neovim runtime environment and then Neovim exits.

Lazyman uses the Lua script ~/.config/nvim-Lazyman/scripts/get_conf.lua to manage Neovim configuration settings. This script can retrieve the current value of a setting, set the value of a supported setting, list all supported setting names, or display the location of the Neovim configuration.

Lazyman configuration script

The ~/.config/nvim-Lazyman/scripts/lazyman_config.sh script is used to display the Lazyman Neovim Configuration menus and invoke the get_conf.lua script to get and set configuration values. The usage message of lazyman_config.sh:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Usage: lazyman_config [-a] [-d] [-i] [-m menu] [-s name value] [-u]
Where:
    -a lists all configuration names and exits
    -d specifies debug mode
    -i indicates initialize conditional plugin configurations and exit
    -m 'menu' specifies the menu to display (conf, form, lsp, plugins)
    -s 'name value' indicates set the value of configuration 'name' to 'value'
      if 'name' is 'get' then 'value' is the configuration name to get
      if 'name' is a table then 'value' is the table entry to set
      follow 'value' with 'enable' or 'disable'
    -u displays this usage message and exits
Examples:
  Display the 'Formatters' menu
    lazyman_config -m form
  Set the theme to 'kanagawa'
    lazyman_config -s theme kanagawa
  Get the theme setting
    lazyman_config -s get theme
  Disable 'gopls' language server
    lazyman_config -s lsp_servers gopls disable

Get/Set configuration script source

The scripts/get_conf.lua script is used to get or set configuration values for those Lazyman Neovim configurations with a lazyman menu configuration interface.

The scripts/get_conf.lua source code:

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
-- Invoke with 'nvim -l get_conf.lua conf_name'
-- Where 'conf_name' is:
--   - one of the entries in lua/configuration.lua
--   - the keyword 'config_home' to get configuration location info
--   - the keyword 'list_names' to lists all configuration entry names
--   - an option/variable name to retrieve its value
--
-- For example, to retrieve the Lazyman configuration 'namespace' setting:
--
-- #!/bin/bash
-- NVIM_APPNAME="nvim-Lazyman" \
--   nvim -l ~/.config/nvim-Lazyman/scripts/get_conf.lua namespace
--
-- or, to retrieve the value of the 'mouse' option in the Webdev config:
--
-- #!/bin/bash
-- NVIM_APPNAME="nvim-Webdev" \
--   nvim -l ~/.config/nvim-Lazyman/scripts/get_conf.lua mouse

local config = vim.inspect(_G.arg[1])
local arg = string.gsub(config, '"', "")
local app_name = os.getenv("NVIM_APPNAME") or ""

local function get_var(var_name, default_value)
  local s, v = pcall(function()
    return vim.api.nvim_get_option(var_name)
  end)
  if s then
    return v
  else
    s, v = pcall(function()
      return vim.api.nvim_get_var(var_name)
    end)
    if s then
      return v
    else
      return default_value
    end
  end
end

local function print_var(entry)
  if type(entry) == "string" then
    io.write(entry .. "\n")
  elseif type(entry) == "table" then
    table.sort(entry)
    for _, val in ipairs(entry) do
      io.write(val .. "\n")
    end
  else
    io.write(tostring(entry) .. "\n")
  end
end

if arg == "config_home" then
  local config_home = vim.fn.stdpath("config")
  io.write("Neovim configuration location = " .. vim.fn.expand(config_home) .. "\n")
  io.write("NVIM_APPNAME = " .. app_name .. "\n")
else
  local var_val = ""
  if app_name == "nvim-Lazyman" then
    local settings = require("configuration")
    if arg == "list_names" then
      for k,_ in pairs(settings) do
        io.write(k .. "\n")
      end
    else
      local entry = settings[arg]
      if entry ~= nil then
        print_var(entry)
      else
        var_val = get_var(arg, "")
        print_var(var_val)
      end
    end
  else
    var_val = get_var(arg, "")
    print_var(var_val)
  end
end