"""Install the Vim 9 configuration on Windows.""" from pathlib import Path from shutil import copyfile, which import subprocess import tempfile import time HOME = Path.home() APP_PATH = Path(__file__).resolve().parent VIM_PATH = HOME / "vimfiles" VIMRC = HOME / "_vimrc" def run(command, cwd=None): subprocess.run(command, cwd=cwd, check=True) def require(program): if which(program) is None: raise SystemExit(f"Error: {program} must be installed.") def check_vim_version(): result = subprocess.run( ["vim", "--clean", "-Nu", "NONE", "-n", "-es", "+if v:version < 900 | cquit | endif", "+qall"] ) if result.returncode: raise SystemExit("Error: Vim 9 or newer is required.") def install_vim_plug(): target = VIM_PATH / "autoload" / "plug.vim" target.parent.mkdir(parents=True, exist_ok=True) with tempfile.TemporaryDirectory() as temp: run(["git", "clone", "--depth", "1", "--quiet", "https://github.com/junegunn/vim-plug", temp]) copyfile(Path(temp) / "plug.vim", target) def install_vimrc(): config_path = (APP_PATH / ".vimrc").as_posix().replace("'", "''") source_line = f"execute 'source ' . fnameescape('{config_path}')" if VIMRC.exists() and VIMRC.read_text(encoding="utf-8", errors="ignore").strip() != source_line: backup = VIMRC.with_name(f"{VIMRC.name}.{int(time.time())}.backup") VIMRC.replace(backup) print(f"Backed up existing Vim config to {backup}") VIMRC.write_text(source_line + "\n", encoding="utf-8") def main(): require("git") require("vim") check_vim_version() install_vim_plug() install_vimrc() run(["vim", "-Nu", str(APP_PATH / ".vimrc"), "-n", "-es", "--cmd", "set nomore", "+PlugInstall --sync", "+qall"]) print("Vim configuration installed.") if __name__ == "__main__": main()