]> git.r.bdr.sh - rbdr/dotfiles/blame - vim/autoload/Pl/Segment.vim
Add initial Alacritty config
[rbdr/dotfiles] / vim / autoload / Pl / Segment.vim
CommitLineData
321ecaca
BB
1let s:default_modes = ['n', 'N', 'v', 'i', 'r', 's']
2
3function! s:CheckConditions(params) " {{{
4 " Check conditions for a segment/group
5 " Integer parameters are always conditions
6 for param in a:params
7 if type(param) == type(0) && param == 0
8 " Break here if it's an integer parameter and it's false (0)
9 return 0
10 endif
11 unlet! param
12 endfor
13
14 return 1
15endfunction " }}}
16function! Pl#Segment#Create(name, ...) " {{{
17 " Check condition parameters
18 if ! s:CheckConditions(a:000)
19 return {}
20 endif
21
22 let name = a:name
23 let modes = s:default_modes
24 let segments = []
25
26 for param in a:000
27 " Lookup modes for this segment/group
28 if type(param) == type([]) && param[0] == 'modes'
29 let modes = param[1]
30 elseif type(a:1) == type([]) && a:1[0] == 'segment'
31 call add(segments, param[1])
32 endif
33
34 unlet! param
35 endfor
36
37 if type(a:1) == type([]) && a:1[0] == 'segment'
38 " This is a segment group
39 return ['segment_group', {
40 \ 'type': 'segment_group'
41 \ , 'name': name
42 \ , 'segments': segments
43 \ , 'modes': modes
44 \ }]
45 else
46 " This is a single segment
47 let text = a:1
48
49 " Search/replace symbols
50 for [key, symbol] in items(g:Pl#Parser#Symbols[g:Powerline_symbols].symbols)
51 let text = substitute(
52 \ text,
53 \ '\v\$('. key .')',
54 \ '\=Pl#Parser#ParseChars(g:Pl#Parser#Symbols[g:Powerline_symbols].symbols[submatch(1)])',
55 \ 'g')
56 endfor
57
58 return ['segment', {
59 \ 'type': 'segment'
60 \ , 'name': name
61 \ , 'text': text
62 \ , 'modes': modes
63 \ }]
64 endif
65
66endfunction " }}}
67function! Pl#Segment#Init(...) " {{{
68 " Check condition parameters
69 if ! s:CheckConditions(a:000)
70 return {}
71 endif
72
73 let segments = {}
74 let ns = ''
75
76 for param in a:000
77 if type(param) == type('')
78 " String parameters is the namespace
79 let ns = param
80 elseif type(param) == type([])
81 " The data dict is in param[1]
82 " By default we don't have a namespace for the segment
83 let segment = param[1]
84
85 if ! empty(ns)
86 " Update segment so that it includes the namespace
87 " Add the namespace to the segment dict key
88 let segment.ns = ns
89 let segment.name = join([segment.ns, segment.name], ':')
90 endif
91
92 let key = segment.name
93
94 let segments[key] = segment
95 endif
96
97 unlet! param
98 endfor
99
100 return segments
101endfunction " }}}
102function! Pl#Segment#Modes(modes) " {{{
103 " Handle modes for both segments and groups
104 let modes = split(a:modes, '\zs')
105
106 if modes[0] == '!'
107 " Filter modes (e.g. "!nr" will ignore the segment in normal and replace modes)
108 let modes = filter(deepcopy(s:default_modes), 'v:val !~# "['. join(modes[1:]) .']"')
109 endif
110
111 return ['modes', modes]
112endfunction " }}}
113function! Pl#Segment#Split(...) " {{{
114 return a:0 ? a:1 .':SPLIT' : 'SPLIT'
115endfunction " }}}
116function! Pl#Segment#Truncate() " {{{
117 return 'TRUNCATE'
118endfunction " }}}
119function! Pl#Segment#Get(name) " {{{
120 " Return a segment data dict
121 let args = []
122
123 " Check for printf segments (lists)
124 if type(a:name) == type([])
125 " We're dealing with a segment with printf arguments
126 let seg_orig_name = a:name[0]
127 let args = a:name[1:]
128 else
129 let seg_orig_name = a:name
130 endif
131
132 " Fetch namespace and variants for storing in the segment dict
133 let seg_ns = ''
134 let seg_variants = []
135
136 " Retrieve color scheme variants
137 let seg_name_split = split(seg_orig_name, '\v\.')
138 if len(seg_name_split) > 1
139 let seg_variants = seg_name_split[1:]
140 endif
141
142 " Retrieve segment name and namespace
143 " Use the first piece of the split string, we can't have variants in the final segment name
144 let seg_name_split = split(seg_name_split[0], '\v:')
145 let seg_name = seg_name_split[0]
146
147 if len(seg_name_split) > 1
148 let seg_ns = seg_name_split[0]
149 let seg_name = seg_name_split[-1]
150 endif
151
152 try
153 " If we have a namespace, try to use the namespaced segment first (i.e. search for the segment in the namespaced file first)
154 let return_segment = deepcopy(g:Powerline#Segments#{seg_ns}#segments[seg_ns .':'. seg_name])
155 catch
156 try
157 " We didn't find a namespaced segment, fall back to common segments
158 let return_segment = deepcopy(g:Powerline#Segments#segments[seg_name])
159 catch
160 " Didn't find the segment among the common segments either, just skip it
161 return {}
162 endtry
163 endtry
164
165 if len(args) && has_key(return_segment, 'text')
166 " Handle segment printf arguments
167 " printf doesn't accept lists as its second argument, so we have to work around that
168 let return_segment.text = call('printf', [ return_segment.text ] + args)
169 endif
170
171 " Assign namespace, name and variants
172 let return_segment.ns = seg_ns
173 let return_segment.name = seg_name
174 let return_segment.orig_name = seg_orig_name
175 let return_segment.variants = seg_variants
176
177 return return_segment
178endfunction " }}}