alker0 / chezmoi.vim

Highlight dotfiles you manage with chezmoi.
Other
128 stars 4 forks source link

Can this plugin surpport for Windows platform? #82

Open Zim-Inn opened 2 weeks ago

Zim-Inn commented 2 weeks ago

image As you see, I found that env variable always be nil on windows ,so I change the line to make it has a right value. local home = (os.getenv("HOME") ~= nil and {os.getenv("HOME")} or {os.getenv("Homepath")})[1] But finally, it still process failed at the below line: image

My home dir is zimiq. image So I think the Unknown operator may be related to /zimiq's /zi I have no way to detect it futher. Hope you can give me some help.

alker0 commented 2 weeks ago

Thank you for your explaining the situation in detail.

This issue likely stems from the pattern rule of autocmd that's applied in the following line of your init.lua:

pattern = { home .. '/.local/share/chezmoi/*' }

The pattern field is set up to include the value of home, which value should contain backslashes (\) as the path separator. However, in the pattern of autocmd, \ is a special character that affects path matching behavior. Therefore, you need to replace every \ with a forward slash (/) to use it as a simple path separator.

Please try modifying your init.lua as follows and let me know if it works.

pattern = { home:gsub('\\', '/') .. '/.local/share/chezmoi/*' }

-- for more strictly:
pattern = { vim.fn.escape(home:gsub('\\', '/') .. '/.local/share/chezmoi/*', '.~$[ ') }

I hope this helps you. Plus, thank you for informing me about the HOMEPATH environment variable, which can be used to get the home directory path in Windows.

Zim-Inn commented 2 weeks ago
{ vim.fn.escape(home:gsub('\\', '/') .. '/.local/share/chezmoi/*', '.~$[ ') }

it works but ... Snipaste_2024-10-07_23-36-55 image

let g:chezmoi#source_dir_path = fnamemodify(g:chezmoi#source_dir_path, ':p')
Zim-Inn commented 2 weeks ago

I don't know whether the new error affect most function of chezmoi.

alker0 commented 2 weeks ago

Hmm... Would removing the brackets ({}) from around the pattern work?

-- pattern = { vim.fn.escape(home:gsub('\\', '/') .. '/.local/share/chezmoi/*', '.~$[ ') },
pattern = vim.fn.escape(home:gsub('\\', '/') .. '/.local/share/chezmoi/*', '.~$[ '),

Surrounding a value with brackets creates a list in Lua, but not valid as pattern value, which should be of string type.

Oh, create_autocmd() accepts even a list as its pattern value. I didn't know that. It wouldn't need fix.

alker0 commented 1 week ago

The chezmoi#source_dir_path option seems to be set to a list somewhere, as indicated by the error message. However, I haven't been able to reproduce this issue in my Windows environment yet. What output would you get if you modified your init function as follows?

init = function()
  vim.g["chezmoi#use_tmp_dir"] = 1
  vim.g["chezmoi#source_dir_path"] = home .. '/.local/share/chezmoi'
  print(vim.g["chezmoi#source_dir_path"])
end,
Zim-Inn commented 1 week ago

Surrounding a value with brackets creates a list in

ok, I got it. I have fixed it in my local. Could I give a PR to your repo?

Zim-Inn commented 1 week ago

Excuse me but ... I am wondering that the chezmoi.lua is at ~\AppData\Local\nvim-data\lazy\LazyVim\lua\lazyvim\plugins\extras\util\chezmoi.lua so that I think the file is in this repo. But in fact there is no file named chezmoi.lua. I should push commit to which repo?

alker0 commented 1 week ago

You shouldn't need to submit a PR. Thank you.

You should not need to modify chezmoi.lua, so revert it to its original state. Instead, set the HOME environment variable to a fixed value before LazyVim runs lazy.nvim. For example, you can add the following lines to ~/AppData/Local/nvim/lua/config/options.lua:

if os.getenv("HOME") == nil then
  -- with Vim API
  vim.fn.setenv("HOME", vim.fn.expand("~"))
  -- or Neovim API
  vim.fn.setenv("HOME", vim.env.HOMEDRIVE .. vim.env.HOMEPATH)
  -- or pure Lua
  vim.fn.setenv("HOME", os.getenv("HOMEDRIVE") .. os.getenv("HOMEPATH"))
end
alker0 commented 1 week ago

You may need to replace backslashes (\) with forward slashes (/) in the HOME environment variable if you are still encountering an error. Backslashes (\) in file paths can sometimes cause issues. This plugin automatically replaces backslashes for file path processing, but other plugins might not do so.

if os.getenv("HOME") == nil then
  -- with Vim API
  local home = vim.fn.expand("~")
  -- or Neovim API
  local home = vim.env.HOMEDRIVE .. vim.env.HOMEPATH
  -- or pure Lua
  local home = os.getenv("HOMEDRIVE") .. os.getenv("HOMEPATH")

  vim.fn.setenv("HOME", home:gsub("\\", "/"))
end