Skip to content

Commit 1f136f5

Browse files
committed
refactor(autoload): generate syntax files in after/syntax
1 parent ad781f6 commit 1f136f5

File tree

5 files changed

+180
-179
lines changed

5 files changed

+180
-179
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
doc/tags
2-
after/ftplugin
2+
after/syntax

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ All notable changes to this project will be documented in this file.
88

99
- Add `g:sonokai_spell_foreground`.
1010
- Optimize `junegunn/limelight.vim`.
11+
- Generate `/after/syntax`.
1112

1213
### Changed
1314

autoload/sonokai.vim

+44-44
Original file line numberDiff line numberDiff line change
@@ -202,62 +202,62 @@ function! sonokai#highlight(group, fg, bg, ...) "{{{
202202
\ a:2[0] :
203203
\ 'NONE')
204204
endfunction "}}}
205-
function! sonokai#ft_gen(path, last_modified, msg) "{{{
206-
" Generate the `after/ftplugin` directory.
205+
function! sonokai#syn_gen(path, last_modified, msg) "{{{
206+
" Generate the `after/syntax` directory.
207207
let full_content = join(readfile(a:path), "\n") " Get the content of `colors/sonokai.vim`
208-
let ft_content = []
209-
let rootpath = sonokai#ft_rootpath(a:path) " Get the path to place the `after/ftplugin` directory.
210-
call substitute(full_content, '" ft_begin.\{-}ft_end', '\=add(ft_content, submatch(0))', 'g') " Search for 'ft_begin.\{-}ft_end' (non-greedy) and put all the search results into a list.
211-
for content in ft_content
212-
let ft_list = []
213-
call substitute(matchstr(matchstr(content, 'ft_begin:.\{-}{{{'), ':.\{-}{{{'), '\(\w\|-\)\+', '\=add(ft_list, submatch(0))', 'g') " Get the file types. }}}}}}
214-
for ft in ft_list
215-
call sonokai#ft_write(rootpath, ft, content) " Write the content.
208+
let syn_conent = []
209+
let rootpath = sonokai#syn_rootpath(a:path) " Get the path to place the `after/syntax` directory.
210+
call substitute(full_content, '" syn_begin.\{-}syn_end', '\=add(syn_conent, submatch(0))', 'g') " Search for 'syn_begin.\{-}syn_end' (non-greedy) and put all the search results into a list.
211+
for content in syn_conent
212+
let syn_list = []
213+
call substitute(matchstr(matchstr(content, 'syn_begin:.\{-}{{{'), ':.\{-}{{{'), '\(\w\|-\)\+', '\=add(syn_list, submatch(0))', 'g') " Get the file types. }}}}}}
214+
for syn in syn_list
215+
call sonokai#syn_write(rootpath, syn, content) " Write the content.
216216
endfor
217217
endfor
218-
call sonokai#ft_write(rootpath, 'text', "let g:sonokai_last_modified = '" . a:last_modified . "'") " Write the last modified time to `after/ftplugin/text/sonokai.vim`
219-
let ftplugin_relative_path = has('win32') ? '\after\ftplugin' : '/after/ftplugin'
218+
call sonokai#syn_write(rootpath, 'text', "let g:sonokai_last_modified = '" . a:last_modified . "'") " Write the last modified time to `after/syntax/text/sonokai.vim`
219+
let syntax_relative_path = has('win32') ? '\after\syntax' : '/after/syntax'
220220
if a:msg ==# 'update'
221-
echohl WarningMsg | echom '[sonokai] Updated ' . rootpath . ftplugin_relative_path | echohl None
221+
echohl WarningMsg | echom '[sonokai] Updated ' . rootpath . syntax_relative_path | echohl None
222222
else
223-
echohl WarningMsg | echom '[sonokai] Generated ' . rootpath . ftplugin_relative_path | echohl None
223+
echohl WarningMsg | echom '[sonokai] Generated ' . rootpath . syntax_relative_path | echohl None
224224
endif
225225
endfunction "}}}
226-
function! sonokai#ft_write(rootpath, ft, content) "{{{
226+
function! sonokai#syn_write(rootpath, syn, content) "{{{
227227
" Write the content.
228-
let ft_path = a:rootpath . '/after/ftplugin/' . a:ft . '/sonokai.vim' " The path of a ftplugin file.
228+
let syn_path = a:rootpath . '/after/syntax/' . a:syn . '/sonokai.vim' " The path of a syntax file.
229229
" create a new file if it doesn't exist
230-
if !filereadable(ft_path)
231-
call mkdir(a:rootpath . '/after/ftplugin/' . a:ft, 'p')
230+
if !filereadable(syn_path)
231+
call mkdir(a:rootpath . '/after/syntax/' . a:syn, 'p')
232232
call writefile([
233233
\ "if !exists('g:colors_name') || g:colors_name !=# 'sonokai'",
234234
\ ' finish',
235235
\ 'endif'
236-
\ ], ft_path, 'a') " Abort if the current color scheme is not sonokai.
236+
\ ], syn_path, 'a') " Abort if the current color scheme is not sonokai.
237237
call writefile([
238-
\ "if index(g:sonokai_loaded_file_types, '" . a:ft . "') ==# -1",
239-
\ " call add(g:sonokai_loaded_file_types, '" . a:ft . "')",
238+
\ "if index(g:sonokai_loaded_file_types, '" . a:syn . "') ==# -1",
239+
\ " call add(g:sonokai_loaded_file_types, '" . a:syn . "')",
240240
\ 'else',
241241
\ ' finish',
242242
\ 'endif'
243-
\ ], ft_path, 'a') " Abort if this file type has already been loaded.
243+
\ ], syn_path, 'a') " Abort if this file type has already been loaded.
244244
endif
245245
" If there is something like `call sonokai#highlight()`, then add
246246
" code to initialize the palette and configuration.
247247
if matchstr(a:content, 'sonokai#highlight') !=# ''
248248
call writefile([
249249
\ 'let s:configuration = sonokai#get_configuration()',
250250
\ 'let s:palette = sonokai#get_palette(s:configuration.style)'
251-
\ ], ft_path, 'a')
251+
\ ], syn_path, 'a')
252252
endif
253253
" Append the content.
254-
call writefile(split(a:content, "\n"), ft_path, 'a')
254+
call writefile(split(a:content, "\n"), syn_path, 'a')
255255
" Add modeline.
256-
call writefile(['" vim: set sw=2 ts=2 sts=2 et tw=80 ft=vim fdm=marker fmr={{{,}}}:'], ft_path, 'a')
256+
call writefile(['" vim: set sw=2 ts=2 sts=2 et tw=80 ft=vim fdm=marker fmr={{{,}}}:'], syn_path, 'a')
257257
endfunction "}}}
258-
function! sonokai#ft_rootpath(path) "{{{
259-
" Get the directory where `after/ftplugin` is generated.
260-
if (matchstr(a:path, '^/usr/share') ==# '') " Return the plugin directory. The `after/ftplugin` directory should never be generated in `/usr/share`, even if you are a root user.
258+
function! sonokai#syn_rootpath(path) "{{{
259+
" Get the directory where `after/syntax` is generated.
260+
if (matchstr(a:path, '^/usr/share') ==# '') " Return the plugin directory. The `after/syntax` directory should never be generated in `/usr/share`, even if you are a root user.
261261
return fnamemodify(a:path, ':p:h:h')
262262
else " Use vim home directory.
263263
if has('nvim')
@@ -267,40 +267,40 @@ function! sonokai#ft_rootpath(path) "{{{
267267
endif
268268
endif
269269
endfunction "}}}
270-
function! sonokai#ft_newest(path, last_modified) "{{{
271-
" Determine whether the current ftplugin files are up to date by comparing the last modified time in `colors/sonokai.vim` and `after/ftplugin/text/sonokai.vim`.
272-
let rootpath = sonokai#ft_rootpath(a:path)
273-
execute 'source ' . rootpath . '/after/ftplugin/text/sonokai.vim'
270+
function! sonokai#syn_newest(path, last_modified) "{{{
271+
" Determine whether the current syntax files are up to date by comparing the last modified time in `colors/sonokai.vim` and `after/syntax/text/sonokai.vim`.
272+
let rootpath = sonokai#syn_rootpath(a:path)
273+
execute 'source ' . rootpath . '/after/syntax/text/sonokai.vim'
274274
return a:last_modified ==# g:sonokai_last_modified ? 1 : 0
275275
endfunction "}}}
276-
function! sonokai#ft_clean(path, msg) "{{{
277-
" Clean the `after/ftplugin` directory.
278-
let rootpath = sonokai#ft_rootpath(a:path)
279-
" Remove `after/ftplugin/**/sonokai.vim`.
280-
let file_list = split(globpath(rootpath, 'after/ftplugin/**/sonokai.vim'), "\n")
276+
function! sonokai#syn_clean(path, msg) "{{{
277+
" Clean the `after/syntax` directory.
278+
let rootpath = sonokai#syn_rootpath(a:path)
279+
" Remove `after/syntax/**/sonokai.vim`.
280+
let file_list = split(globpath(rootpath, 'after/syntax/**/sonokai.vim'), "\n")
281281
for file in file_list
282282
call delete(file)
283283
endfor
284284
" Remove empty directories.
285-
let dir_list = split(globpath(rootpath, 'after/ftplugin/*'), "\n")
285+
let dir_list = split(globpath(rootpath, 'after/syntax/*'), "\n")
286286
for dir in dir_list
287287
if globpath(dir, '*') ==# ''
288288
call delete(dir, 'd')
289289
endif
290290
endfor
291-
if globpath(rootpath . '/after/ftplugin', '*') ==# ''
292-
call delete(rootpath . '/after/ftplugin', 'd')
291+
if globpath(rootpath . '/after/syntax', '*') ==# ''
292+
call delete(rootpath . '/after/syntax', 'd')
293293
endif
294294
if globpath(rootpath . '/after', '*') ==# ''
295295
call delete(rootpath . '/after', 'd')
296296
endif
297297
if a:msg
298-
let ftplugin_relative_path = has('win32') ? '\after\ftplugin' : '/after/ftplugin'
299-
echohl WarningMsg | echom '[sonokai] Cleaned ' . rootpath . ftplugin_relative_path | echohl None
298+
let syntax_relative_path = has('win32') ? '\after\syntax' : '/after/syntax'
299+
echohl WarningMsg | echom '[sonokai] Cleaned ' . rootpath . syntax_relative_path | echohl None
300300
endif
301301
endfunction "}}}
302-
function! sonokai#ft_exists(path) "{{{
303-
return filereadable(sonokai#ft_rootpath(a:path) . '/after/ftplugin/text/sonokai.vim')
302+
function! sonokai#syn_exists(path) "{{{
303+
return filereadable(sonokai#syn_rootpath(a:path) . '/after/syntax/text/sonokai.vim')
304304
endfunction "}}}
305305

306306
" vim: set sw=2 ts=2 sts=2 et tw=80 ft=vim fdm=marker fmr={{{,}}}:

0 commit comments

Comments
 (0)