From 9e80d9bbfad057cd8c056523ac18571083461ef0 Mon Sep 17 00:00:00 2001 From: Olliver Schinagl Date: Wed, 28 Dec 2022 07:19:06 +0100 Subject: [PATCH] feat:(XDG): add XDG support * fonts: Ensure mkfontscale is installed When installing fonts, we have a silent igored error about missing `mkfontscale` (and `mkfontdir`). Add a requirements check for it, and 'need' it when actually attempting to install fonts. * fetch_repo: Never change a directory It is bad practice to change the currents shell working directory. E.g. never use 'cd' in a script. For example, if `git pull` crashes, it would leave the user in the git directory 'somewhere' on his filesystem, potentially causing confusion, as the following `cd -` command is never executed. There are of course always exceptions, such as when tooling does not (easily) support out of tree invocation. To solve this, run the command in a subshell, which won't touch the current working directory as the exit of the subshell kept the previous working directory. A more difficult alternative would be to pass `--git-dir` and `--work-tree` along to git, but this would make it far harder to read. * fetch_repo: Do not duplicate code all over There is no reason to put `need 'git'` all over the place, when the only time we truly need it, is if we pull the repo. Make the code a little less cluttered and a bit easier to read. * install: Support XDG_CONFIG_HOME Add support for the XDG specification for XDG_CONFIG_HOME. NeoVIM already follows this, regular vim needs a bit of help there. Closes: #3517 * SpaceVim.d: Support XDG paths Signed-off-by: Olliver Schinagl Signed-off-by: Olliver Schinagl --- autoload/SpaceVim/custom.vim | 6 ++- docs/install.sh | 100 +++++++++++++++++++---------------- vimrc | 20 +++++++ 3 files changed, 78 insertions(+), 48 deletions(-) diff --git a/autoload/SpaceVim/custom.vim b/autoload/SpaceVim/custom.vim index f793b7b6b..76074c739 100644 --- a/autoload/SpaceVim/custom.vim +++ b/autoload/SpaceVim/custom.vim @@ -62,7 +62,11 @@ endfunction function! s:global_dir() abort if empty($SPACEVIMDIR) - return s:FILE.unify_path('~/.SpaceVim.d/') + if !empty($XDG_CONFIG_HOME) + return s:FILE.unify_path($XDG_CONFIG_HOME.'/SpaceVim.d/') + else + return s:FILE.unify_path($HOME.'/.SpaceVim.d/') + endif else return s:FILE.unify_path($SPACEVIMDIR) endif diff --git a/docs/install.sh b/docs/install.sh index 0859a1bf4..5cde422ec 100755 --- a/docs/install.sh +++ b/docs/install.sh @@ -89,6 +89,10 @@ Version='2.1.0-dev' System="$(uname -s)" # }}} +XDGSpaceDir="${XDG_CONFIG_HOME:-${HOME}/.}${XDG_CONFIG_HOME:+/}SpaceVim" +XDGvimDir="${XDG_CONFIG_HOME:-${HOME}/.}${XDG_CONFIG_HOME:+/}vim" +XDGnvimDir="${XDG_CONFIG_HOME:-${HOME}/.}${XDG_CONFIG_HOME:+/}nvim" + # need_cmd {{{ need_cmd () { if ! hash "$1" &>/dev/null; then @@ -129,15 +133,17 @@ echo_with_color () { # fetch_repo {{{ fetch_repo () { - if [[ -d "$HOME/.SpaceVim" ]]; then + need_cmd 'git' + if [[ -d "${XDGSpaceDir:-}" ]]; then info "Trying to update SpaceVim" - cd "$HOME/.SpaceVim" - git pull - cd - > /dev/null 2>&1 + ( + cd "${XDGSpaceDir:?}" + git pull + ) success "Successfully update SpaceVim" else info "Trying to clone SpaceVim" - git clone https://github.com/SpaceVim/SpaceVim.git "$HOME/.SpaceVim" + git clone https://github.com/SpaceVim/SpaceVim.git "${XDGSpaceDir:-}" if [ $? -eq 0 ]; then success "Successfully clone SpaceVim" else @@ -155,51 +161,47 @@ install_vim () { success "Backup $HOME/.vimrc to $HOME/.vimrc_back" fi - if [[ -d "$HOME/.vim" ]]; then - if [[ "$(readlink $HOME/.vim)" =~ \.SpaceVim$ ]]; then - success "Installed SpaceVim for vim" - else - mv "$HOME/.vim" "$HOME/.vim_back" - success "BackUp $HOME/.vim to $HOME/.vim_back" - ln -s "$HOME/.SpaceVim" "$HOME/.vim" - success "Installed SpaceVim for vim" + if [[ -d "${XDGvimDir:-}" ]]; then + if [[ "$(readlink "${XDGvimDir:-}")" =~ \.?SpaceVim$ ]]; then + success "SpaceVim already installed in '${XDGvimDir:-}'" + return fi - else - ln -s "$HOME/.SpaceVim" "$HOME/.vim" - success "Installed SpaceVim for vim" + + mv "${XDGvimDir:?}" "${XDGvimDir:-}_back" + success "BackUp '${XDGvimDir}' to '${XDGvimDir}_back'" fi + + ln -s "${XDGSpaceDir:?}" "${XDGvimDir:?}" + success "Installed SpaceVim for vim in '${XDGvimDir}'" } # }}} # install_neovim {{{ install_neovim () { - if [[ -d "$HOME/.config/nvim" ]]; then - if [[ "$(readlink $HOME/.config/nvim)" =~ \.SpaceVim$ ]]; then - success "Installed SpaceVim for neovim" - else - mv "$HOME/.config/nvim" "$HOME/.config/nvim_back" - success "BackUp $HOME/.config/nvim to $HOME/.config/nvim_back" - ln -s "$HOME/.SpaceVim" "$HOME/.config/nvim" - success "Installed SpaceVim for neovim" + if [[ -d "${XDGnvimDir:-}" ]]; then + if [[ "$(readlink "${XDGnvimDir:-}")" =~ \.?SpaceVim$ ]]; then + success "SpaceVim already installed in '${XDGnvimDir:-}'" + return fi - else - mkdir -p "$HOME/.config" - ln -s "$HOME/.SpaceVim" "$HOME/.config/nvim" - success "Installed SpaceVim for neovim" + + mv "${XDGnvimDir:?}" "${XDGnvimDir:-}_back" + success "BackUp '${XDGnvimDir}' to '${XDGnvimDir}_back'" fi + + ln -s "${XDGSpaceDir:?}" "${XDGnvimDir:?}" + success "Installed SpaceVim for nvim in '${XDGnvimDir}'" } # }}} # uninstall_vim {{{ uninstall_vim () { - if [[ -d "$HOME/.vim" ]]; then - if [[ "$(readlink $HOME/.vim)" =~ \.SpaceVim$ ]]; then - rm "$HOME/.vim" - success "Uninstall SpaceVim for vim" - if [[ -d "$HOME/.vim_back" ]]; then - mv "$HOME/.vim_back" "$HOME/.vim" - success "Recover from $HOME/.vim_back" - fi + if [[ -d "${XDGvimDir:-}" ]] && + [[ "$(readlink "${XDGvimDir:?}")" =~ \.?SpaceVim$ ]]; then + rm "${XDGvimDir:?}" + success "Uninstall SpaceVim for vim" + if [[ -d "${XDGvimDir}_back" ]]; then + mv "${XDGvimDir}_back" "${XDGvimDir}" + success "Recover from ${XDGvimDir}_back" fi fi if [[ -f "$HOME/.vimrc_back" ]]; then @@ -211,14 +213,13 @@ uninstall_vim () { # uninstall_neovim {{{ uninstall_neovim () { - if [[ -d "$HOME/.config/nvim" ]]; then - if [[ "$(readlink $HOME/.config/nvim)" =~ \.SpaceVim$ ]]; then - rm "$HOME/.config/nvim" - success "Uninstall SpaceVim for neovim" - if [[ -d "$HOME/.config/nvim_back" ]]; then - mv "$HOME/.config/nvim_back" "$HOME/.config/nvim" - success "Recover from $HOME/.config/nvim_back" - fi + if [[ -d "${XDGnvimDir:-}" ]] && + [[ "$(readlink "${XDGnvimDir:?}")" =~ \.?SpaceVim$ ]]; then + rm "${XDGnvimDir:?}" + success "Uninstall SpaceVim for neovim" + if [[ -d "${XDGnvimDir}_back" ]]; then + mv "${XDGnvimDir}_back" "${XDGnvimDir}" + success "Recover from ${XDGnvimDir}_back" fi fi } @@ -233,6 +234,12 @@ check_requirements () { else warn "Check Requirements : git" fi + if hash "mkfontscale" &>/dev/null; then + mkfontscale_version=$(mkfontscale -v) + success "Check Requirements: ${mkfontscale_version}" + else + warn "Check Requirements : mkfontscale" + fi if hash "vim" &>/dev/null; then is_vim8=$(vim --version | grep "Vi IMproved 8") is_vim74=$(vim --version | grep "Vi IMproved 7.4") @@ -346,6 +353,8 @@ download_font () { # install_fonts {{{ install_fonts () { + need_cmd 'mkfontdir' + need_cmd 'mkfontscale' if [[ ! -d "$HOME/.local/share/fonts" ]]; then mkdir -p $HOME/.local/share/fonts fi @@ -383,7 +392,6 @@ main () { ;; --install|-i) welcome - need_cmd 'git' fetch_repo if [ $# -eq 2 ] then @@ -413,7 +421,6 @@ main () { ;; --no-fonts) welcome - need_cmd 'git' fetch_repo install_vim install_neovim @@ -426,7 +433,6 @@ main () { esac else welcome - need_cmd 'git' fetch_repo install_vim install_neovim diff --git a/vimrc b/vimrc index 692c4f0e7..a87b37d9b 100644 --- a/vimrc +++ b/vimrc @@ -6,6 +6,26 @@ " License: GPLv3 "============================================================================= +" Use XDG paths if available +if !empty($XDG_CONFIG_HOME) + set runtimepath^=$XDG_CONFIG_HOME/vim + set runtimepath+=$XDG_DATA_HOME/vim + set runtimepath+=$XDG_CONFIG_HOME/vim/after + + set packpath^=$XDG_DATA_HOME/vim,$XDG_CONFIG_HOME/vim + set packpath+=$XDG_CONFIG_HOME/vim/after,$XDG_DATA_HOME/vim/after + + let g:netrw_home = $XDG_DATA_HOME."/vim" + call mkdir($XDG_DATA_HOME."/vim/spell", 'p') + + set backupdir=$XDG_STATE_HOME/vim/backup | call mkdir(&backupdir, 'p') + set directory=$XDG_STATE_HOME/vim/swap | call mkdir(&directory, 'p') + set undodir=$XDG_STATE_HOME/vim/undo | call mkdir(&undodir, 'p') + set viewdir=$XDG_STATE_HOME/vim/view | call mkdir(&viewdir, 'p') + + if !has('nvim') | set viminfofile=$XDG_STATE_HOME/vim/viminfo | endif +endif + " Note: Skip initialization for vim-tiny or vim-small. if 1 let g:_spacevim_if_lua = 0