aboutsummaryrefslogtreecommitdiff
path: root/vim/autoload/xdg.vim
blob: 67a9c5b28ad2b3d14f11ea1ff83aee93b64c6c3e (plain) (blame)
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
" <https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html#variables>
let s:defaults = {
      \ 'XDG_CACHE_HOME': $HOME.'/.cache',
      \ 'XDG_CONFIG_HOME': $HOME.'/.config',
      \ 'XDG_CONFIG_DIRS': '/etc/xdg',
      \ 'XDG_DATA_HOME': $HOME.'/.local/share',
      \ 'XDG_DATA_DIRS': '/usr/local/share:/usr/share',
      \}

function! s:Get(name) abort
  let name = a:name
  if !has_key(s:defaults, name)
    throw 'Illegal XDG basedirs env var name'
  endif
  let value = s:defaults[name]
  if exists('$'.a:name)
    execute 'let value = $'.a:name
  endif
  return value
endfunction

function! s:Absolute(path) abort
  return a:path =~# '^/'
        \ || a:path =~# '^\~/'
        \ || a:path ==# '~'
endfunction

function! s:Home(name) abort
  let home = s:Get(a:name)
  if !s:Absolute(home)
    return ''
  endif
  return home.'/vim'
endfunction

function! s:Dirs(name) abort
  let dirs = split(s:Get(a:name), ':')
  return map(
        \ filter(copy(dirs), 's:Absolute(v:val)')
        \,'v:val.''/vim'''
        \)
endfunction

function! xdg#CacheHome() abort
  return s:Home('XDG_CACHE_HOME')
endfunction

function! xdg#ConfigHome() abort
  return s:Home('XDG_CONFIG_HOME')
endfunction

function! xdg#DataHome() abort
  return s:Home('XDG_DATA_HOME')
endfunction

function! xdg#ConfigDirs() abort
  return s:Dirs('XDG_CONFIG_DIRS')
endfunction

function! xdg#DataDirs() abort
  return s:Dirs('XDG_DATA_DIRS')
endfunction