Unify path handling -- part 3
This commit is contained in:
parent
12d6265193
commit
f76e75d117
@ -49,8 +49,8 @@ function! s:is_img_link(lnk) "{{{
|
|||||||
return 0
|
return 0
|
||||||
endfunction "}}}
|
endfunction "}}}
|
||||||
|
|
||||||
function! s:default_CSS_full_name(target_dir) " {{{
|
function! s:default_CSS_full_name(path) " {{{
|
||||||
return vimwiki#path#join(path, vimwiki#vars#get_wikilocal('css_name'))
|
return vimwiki#path#join(a:path, vimwiki#vars#get_wikilocal('css_name'))
|
||||||
endfunction "}}}
|
endfunction "}}}
|
||||||
|
|
||||||
" Returns: 1 if it was created, 0 if it already existed
|
" Returns: 1 if it was created, 0 if it already existed
|
||||||
@ -72,7 +72,7 @@ function! s:template_full_name(name) "{{{
|
|||||||
let name = a:name
|
let name = a:name
|
||||||
endif
|
endif
|
||||||
|
|
||||||
let filename = vimwiki#path#from_segment_file(name . vimwiki#vars#get_wikilocal('template_ext'))
|
let filename = vimwiki#path#file_segment(name . vimwiki#vars#get_wikilocal('template_ext'))
|
||||||
|
|
||||||
let template_file = vimwiki#path#to_string(vimwiki#path#join(vimwiki#vars#get_wikilocal('template_path'), filename))
|
let template_file = vimwiki#path#to_string(vimwiki#path#join(vimwiki#vars#get_wikilocal('template_path'), filename))
|
||||||
|
|
||||||
|
@ -169,19 +169,28 @@ endif
|
|||||||
|
|
||||||
|
|
||||||
"----------------------------------------------------------
|
"----------------------------------------------------------
|
||||||
" Path manipulation, i.e. do stuff with the paths of (not necessarily existing) files
|
" Path manipulation, i.e. functions which do stuff with the paths of (not necessarily existing) files
|
||||||
"----------------------------------------------------------
|
"----------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
" The used data types are (with their internal representation)
|
" The used data types are
|
||||||
" - directory object: a dictionary with the following entries:
|
"
|
||||||
" - 'protocoll' -- how to access the file. Supported are 'scp' or 'file'
|
" - directory object:
|
||||||
" - 'is_unix' -- 1 if it's supposed to be a unix-like path
|
" - used for an absolute path to a directory
|
||||||
" - 'path' -- a list containing the directory names starting at the root
|
" - internally, it is a dictionary with the following entries:
|
||||||
" - file object: a list [dir_obj, file name]
|
" - 'protocoll' -- how to access the file. Supported are 'scp' or 'file'
|
||||||
" - file segment: representing e.g. a relative path. Is a list where the first
|
" - 'is_unix' -- 1 if it's supposed to be a unix-like path
|
||||||
" element is a list of directory names and the second the file name
|
" - 'path' -- a list containing the directory names starting at the root
|
||||||
" - directory segment: a list of directory names
|
" - file object:
|
||||||
|
" - for an absolute path to a file
|
||||||
|
" - internally a list [dir_obj, file name]
|
||||||
|
" - file segment:
|
||||||
|
" - for a relative path to a file or a part of an absolute path
|
||||||
|
" - internally a list where the first element is a list of directory names and the second the
|
||||||
|
" file name
|
||||||
|
" - directory segment:
|
||||||
|
" - for a relative path to a directory or a part of an absolute path
|
||||||
|
" - internally a list of directory names
|
||||||
|
|
||||||
|
|
||||||
" create and return a file object from a string. It is assumed that the given
|
" create and return a file object from a string. It is assumed that the given
|
||||||
@ -214,6 +223,21 @@ function! vimwiki#path#dir_obj(dirpath)
|
|||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
|
" Assume it is not an absolute path
|
||||||
|
function! vimwiki#path#file_segment(path_segment)
|
||||||
|
let filename = fnamemodify(a:path_segment, ':t')
|
||||||
|
let path = fnamemodify(a:path_segment, ':h')
|
||||||
|
let path_list = (path ==# '.' ? [] : split(path, '\m[/\\]', 1))
|
||||||
|
return [path_list, filename]
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
|
" Assume it is not an absolute path
|
||||||
|
function! vimwiki#path#dir_segment(path_segment)
|
||||||
|
return split(a:path_segment, '\m[/\\]', 1)
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
function! vimwiki#path#extension(file_object)
|
function! vimwiki#path#extension(file_object)
|
||||||
return fnamemodify(a:file_object[1], ':e')
|
return fnamemodify(a:file_object[1], ':e')
|
||||||
endfunction
|
endfunction
|
||||||
@ -231,16 +255,26 @@ function! vimwiki#path#filename(file_object)
|
|||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
" Returns: the dir_obj or file_obj as string, ready to be used with the regular
|
" Returns: the dir_obj, file_obj, file segment or dir segment as string, ready
|
||||||
" path handling functions in Vim
|
" to be used with the regular path handling functions in Vim
|
||||||
function! vimwiki#path#to_string(obj)
|
function! vimwiki#path#to_string(obj)
|
||||||
let dir_obj = type(a:obj) == 4 ? a:obj : a:obj[0]
|
if type(a:obj) == 4 " dir object
|
||||||
let separator = dir_obj.is_unix ? '/' : '\'
|
let separator = a:obj.is_unix ? '/' : '\'
|
||||||
let address = join(dir_obj.path, separator) . separator
|
let address = join(dir_obj.path, separator) . separator
|
||||||
if type(a:obj) == 3
|
return address
|
||||||
let address .= a:path[1]
|
elseif type(a:obj[0]) == 4 " file object
|
||||||
|
let dir_obj = type(a:obj) == 4 ? a:obj : a:obj[0]
|
||||||
|
let separator = a:obj[0].is_unix ? '/' : '\'
|
||||||
|
let address = join(a:obj[0].path, separator) . separator . a:obj[1]
|
||||||
|
return address
|
||||||
|
elseif type(a:obj[0]) == 3 " file segment
|
||||||
|
" XXX: what about the separator?
|
||||||
|
return join(a:obj[0], '/') . '/' . a:obj[1]
|
||||||
|
elseif type(a:obj[0]) == 1 " directory segment
|
||||||
|
return join(a:obj, '/') . '/'
|
||||||
|
else
|
||||||
|
call vimwiki#u#error('Invalid argument ' . string(a:obj))
|
||||||
endif
|
endif
|
||||||
return address
|
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
@ -251,38 +285,30 @@ function! vimwiki#path#append_to_dirname(dir_obj, str)
|
|||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
" Assume it is not an absolute path
|
" Returns a file object made from a dir object plus a file semgent
|
||||||
function! vimwiki#path#from_segment_file(path_segment)
|
function! vimwiki#path#join(dir_obje, file_segment)
|
||||||
let filename = fnamemodify(a:path_segment, ':t')
|
let new_dir_object = copy(a:dir_obj)
|
||||||
let path = fnamemodify(a:path_segment, ':h')
|
|
||||||
let path_list = (path ==# '.' ? [] : split(path, '\m[/\\]', 1))
|
|
||||||
return [path_list, filename]
|
|
||||||
endfunction
|
|
||||||
|
|
||||||
" Assume it is not an absolute path
|
|
||||||
function! vimwiki#path#from_segment_dir(path_segment)
|
|
||||||
return split(a:path_segment, '\m[/\\]', 1)
|
|
||||||
endfunction
|
|
||||||
|
|
||||||
function! vimwiki#path#join(dir_path, file_segment)
|
|
||||||
let new_dir_object = copy(a:dir_path)
|
|
||||||
let new_dir_object.path += a:file_segment[0]
|
let new_dir_object.path += a:file_segment[0]
|
||||||
return [new_dir_object, a:file_segment[1]]
|
return [new_dir_object, a:file_segment[1]]
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
|
" Returns a dir object made from a dir object plus a dir semgent
|
||||||
function! vimwiki#path#join_dir(dir_path, dir_segment)
|
function! vimwiki#path#join_dir(dir_path, dir_segment)
|
||||||
let new_dir_object = copy(a:dir_path)
|
let new_dir_object = copy(a:dir_path)
|
||||||
let new_dir_object.path += a:dir_segment
|
let new_dir_object.path += a:dir_segment
|
||||||
return new_dir_object
|
return new_dir_object
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
" returns the segment s, so that join(dir, s) == file
|
|
||||||
|
" returns the file segment fs, so that join(dir, fs) == file
|
||||||
" we just assume the file is somewhere in dir
|
" we just assume the file is somewhere in dir
|
||||||
function! vimwiki#path#subtract(dir_object, file_object)
|
function! vimwiki#path#subtract(dir_object, file_object)
|
||||||
let path_rest = a:file_object[0].path[len(a:dir_object.path):]
|
let path_rest = a:file_object[0].path[len(a:dir_object.path):]
|
||||||
return [path_rest, file_object[1]]
|
return [path_rest, file_object[1]]
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
" Returns: the relative path from a:dir to a:file
|
" Returns: the relative path from a:dir to a:file
|
||||||
function! vimwiki#path#relpath(dir1_object, dir2_object)
|
function! vimwiki#path#relpath(dir1_object, dir2_object)
|
||||||
let dir1_path = copy(a:dir1_object.path)
|
let dir1_path = copy(a:dir1_object.path)
|
||||||
@ -331,7 +357,7 @@ let s:vimwiki_autoload_dir = expand('<sfile>:p:h')
|
|||||||
|
|
||||||
function! vimwiki#path#find_autoload_file(filename)
|
function! vimwiki#path#find_autoload_file(filename)
|
||||||
let autoload_dir = vimwiki#path#dir_obj(s:vimwiki_autoload_dir)
|
let autoload_dir = vimwiki#path#dir_obj(s:vimwiki_autoload_dir)
|
||||||
let filename_obj = vimwiki#path#from_segment_file(a:filename)
|
let filename_obj = vimwiki#path#file_segment(a:filename)
|
||||||
let file = vimwiki#path#join(autoload_dir, filename_obj)
|
let file = vimwiki#path#join(autoload_dir, filename_obj)
|
||||||
if !vimwiki#path#exists(file)
|
if !vimwiki#path#exists(file)
|
||||||
echom 'Vimwiki Error: ' . vimwiki#path#to_string(file) . ' not found'
|
echom 'Vimwiki Error: ' . vimwiki#path#to_string(file) . ' not found'
|
||||||
|
@ -145,7 +145,7 @@ endfunction " }}}
|
|||||||
" vimwiki#tags#metadata_file_path
|
" vimwiki#tags#metadata_file_path
|
||||||
" Returns tags metadata file path object
|
" Returns tags metadata file path object
|
||||||
function! vimwiki#tags#metadata_file_path() abort "{{{
|
function! vimwiki#tags#metadata_file_path() abort "{{{
|
||||||
return vimwiki#path#join(vimwiki#vars#get_wikilocal('path'), vimwiki#path#from_segment_file(s:TAGS_METADATA_FILE_NAME))
|
return vimwiki#path#join(vimwiki#vars#get_wikilocal('path'), vimwiki#path#file_segment(s:TAGS_METADATA_FILE_NAME))
|
||||||
endfunction " }}}
|
endfunction " }}}
|
||||||
|
|
||||||
" s:load_tags_metadata
|
" s:load_tags_metadata
|
||||||
|
@ -48,6 +48,8 @@ function! s:populate_global_variables()
|
|||||||
endif
|
endif
|
||||||
endfor
|
endfor
|
||||||
|
|
||||||
|
call s:validate_global_settings()
|
||||||
|
|
||||||
" non-configurable global variables
|
" non-configurable global variables
|
||||||
|
|
||||||
" Scheme regexes should be defined even if syntax file is not loaded yet cause users should be
|
" Scheme regexes should be defined even if syntax file is not loaded yet cause users should be
|
||||||
@ -152,6 +154,27 @@ function! s:populate_global_variables()
|
|||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
|
function! s:validate_global_settings()
|
||||||
|
let g:vimwiki_global_vars.dir_link =
|
||||||
|
\ vimwiki#path#file_segment(g:vimwiki_global_vars.dir_link)
|
||||||
|
|
||||||
|
for extension in g:vimwiki_global_vars.ext2syntax
|
||||||
|
if extension[0] != '.'
|
||||||
|
let g:vimwiki_global_vars.ext2syntax['.'.extension] =
|
||||||
|
g:vimwiki_global_vars.ext2syntax[extension]
|
||||||
|
call remove(g:vimwiki_global_vars.ext2syntax, extension)
|
||||||
|
endif
|
||||||
|
endfor
|
||||||
|
|
||||||
|
let new_user_htmls = []
|
||||||
|
for file_name in split(g:vimwiki_global_vars.user_htmls, ',')
|
||||||
|
let trimmed = vimwiki#u#trim(file_name)
|
||||||
|
call add(new_user_htmls, vimwiki#path#file_segment(trimmed))
|
||||||
|
endfor
|
||||||
|
let g:vimwiki_global_vars.user_htmls = new_user_htmls
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
" g:vimwiki_wikilocal_vars is a list of dictionaries: one dict for every registered wiki plus one
|
" g:vimwiki_wikilocal_vars is a list of dictionaries: one dict for every registered wiki plus one
|
||||||
" (the last in the list) which contains the default values (used for temporary wikis).
|
" (the last in the list) which contains the default values (used for temporary wikis).
|
||||||
function! s:populate_wikilocal_options()
|
function! s:populate_wikilocal_options()
|
||||||
@ -213,12 +236,16 @@ function! s:populate_wikilocal_options()
|
|||||||
let temporary_options_dict.temp = 1
|
let temporary_options_dict.temp = 1
|
||||||
call add(g:vimwiki_wikilocal_vars, temporary_options_dict)
|
call add(g:vimwiki_wikilocal_vars, temporary_options_dict)
|
||||||
|
|
||||||
call s:validate_settings()
|
call s:validate_wikilocal_settings()
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
function! s:validate_settings()
|
function! s:validate_wikilocal_settings()
|
||||||
for wiki_settings in g:vimwiki_wikilocal_vars
|
for wiki_settings in g:vimwiki_wikilocal_vars
|
||||||
|
let wiki_settings.css_name = vimwiki#path#file_segment(wiki_settings.css_name)
|
||||||
|
|
||||||
|
let wiki_settings.custom_wiki2html = vimwiki#path#file_object(wiki_settings.custom_wiki2html)
|
||||||
|
|
||||||
let wiki_settings['path'] = vimwiki#path#dir_obj(wiki_settings['path'])
|
let wiki_settings['path'] = vimwiki#path#dir_obj(wiki_settings['path'])
|
||||||
|
|
||||||
let path_html = wiki_settings['path_html']
|
let path_html = wiki_settings['path_html']
|
||||||
@ -231,7 +258,7 @@ function! s:validate_settings()
|
|||||||
|
|
||||||
let wiki_settings['template_path'] = vimwiki#path#dir_obj(wiki_settings['template_path'])
|
let wiki_settings['template_path'] = vimwiki#path#dir_obj(wiki_settings['template_path'])
|
||||||
let wiki_settings['diary_path'] = vimwiki#path#join_dir(wiki_settings['path'],
|
let wiki_settings['diary_path'] = vimwiki#path#join_dir(wiki_settings['path'],
|
||||||
\ vimwiki#path#from_segment_dir(wiki_settings['diary_rel_path']))
|
\ vimwiki#path#dir_segment(wiki_settings['diary_rel_path']))
|
||||||
endfor
|
endfor
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
@ -606,7 +633,7 @@ function! vimwiki#vars#add_temporary_wiki(settings)
|
|||||||
let new_temp_wiki_settings[key] = value
|
let new_temp_wiki_settings[key] = value
|
||||||
endfor
|
endfor
|
||||||
call insert(g:vimwiki_wikilocal_vars, new_temp_wiki_settings, -1)
|
call insert(g:vimwiki_wikilocal_vars, new_temp_wiki_settings, -1)
|
||||||
call s:validate_settings()
|
call s:validate_wikilocal_settings()
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
" number of registered wikis + temporary
|
" number of registered wikis + temporary
|
||||||
|
Loading…
Reference in New Issue
Block a user