" vim:tabstop=2:shiftwidth=2:expandtab:foldmethod=marker:textwidth=79 " Vimwiki filetype plugin file " Author: Maxim Kim " Home: http://code.google.com/p/vimwiki/ if exists("b:did_ftplugin") finish endif let b:did_ftplugin = 1 " Don't load another plugin for this buffer call vimwiki#u#reload_regexes() " UNDO list {{{ " Reset the following options to undo this plugin. let b:undo_ftplugin = "setlocal ". \ "suffixesadd< isfname< formatlistpat< ". \ "formatoptions< foldtext< ". \ "foldmethod< foldexpr< commentstring< " " UNDO }}} " MISC STUFF {{{ setlocal commentstring=%%%s if g:vimwiki_conceallevel && exists("+conceallevel") let &l:conceallevel = g:vimwiki_conceallevel endif " GOTO FILE: gf {{{ execute 'setlocal suffixesadd='.VimwikiGet('ext') setlocal isfname-=[,] " gf}}} " omnicomplete function for wiki files and anchors {{{ let g:vimwiki_default_header_search = '^\s*\(=\{1,6}\)\([^=].*[^=]\)\1\s*$' let g:vimwiki_default_header_match = '^\s*\(=\{1,6}\)=\@!\s*__Header__\s*\1=\@!\s*$' let g:vimwiki_markdown_header_search = '^\s*\(#\{1,6}\)\([^#].*\)$' let g:vimwiki_markdown_header_match = '^\s*\(#\{1,6}\)#\@!\s*__Header__\s*$' let g:vimwiki_media_header_search = '^\s*\(=\{1,6}\)\([^=].*[^=]\)\1\s*$' let g:vimwiki_media_header_match = '^\s*\(=\{1,6}\)=\@!\s*__Header__\s*\1=\@!\s*$' let g:vimwiki_default_bold_search = '\%(^\|\s\|[[:punct:]]\)\@<=\*\zs\%([^*`[:space:]][^*`]*[^*`[:space:]]\|[^*`[:space:]]\)\ze\*\%([[:punct:]]\|\s\|$\)\@=' let g:vimwiki_default_bold_match = '\%(^\|\s\|[[:punct:]]\)\@<=\*__Text__\*\%([[:punct:]]\|\s\|$\)\@=' let g:vimwiki_markdown_bold_search = '\%(^\|\s\|[[:punct:]]\)\@<=\*\zs\%([^*`[:space:]][^*`]*[^*`[:space:]]\|[^*`[:space:]]\)\ze\*\%([[:punct:]]\|\s\|$\)\@=' let g:vimwiki_markdown_bold_match = '\%(^\|\s\|[[:punct:]]\)\@<=\*__Text__\*\%([[:punct:]]\|\s\|$\)\@=' let g:vimwiki_media_bold_search = "'''\\zs[^']\\+\\ze'''" let g:vimwiki_media_bold_match = '''''''__Text__''''''' " ^- looks strange, but is equivalent to "'''__Text__'''" but since we later " want to call escape() on this string, we must keep it in single quotes function! g:complete_wikifiles(findstart, base) if a:findstart == 1 let column = col('.')-1 let line = getline('.')[:column] let startoflink = match(line, '\[\[\zs[^\\[]*$') if startoflink != -1 return startoflink endif if VimwikiGet('syntax') == 'markdown' let startofinlinelink = match(line, '\[.*\](\zs.*$') if startofinlinelink != -1 return startofinlinelink endif endif return -1 else if a:base !~ '#' " we look for wiki files if a:base =~# '^wiki\d:' let wikinumber = eval(matchstr(a:base, '^wiki\zs\d')) let directory = VimwikiGet('path', wikinumber) let ext = VimwikiGet('ext', wikinumber) let prefix = matchstr(a:base, '^wiki\d:\zs.*') let scheme = matchstr(a:base, '^wiki\d:\ze') elseif a:base =~# '^diary:' let directory = VimwikiGet('path').'/'.VimwikiGet('diary_rel_path') let ext = VimwikiGet('ext') let prefix = matchstr(a:base, '^diary:\zs.*') let scheme = matchstr(a:base, '^diary:\ze') else let directory = VimwikiGet('path') let ext = VimwikiGet('ext') let prefix = a:base let scheme = '' endif let result = [] for wikifile in split(globpath(directory, '**/*'.ext), '\n') " get the filename relative to the wiki path: let subdir_filename = substitute(fnamemodify(wikifile, ':p:r'), \ '\V'.fnamemodify(directory, ':p'), '', '') if subdir_filename =~ '^'.prefix call add(result, scheme . subdir_filename) endif endfor return result else " we look for anchors in the given wikifile let segments = split(a:base, '#', 1) let link_infos = vimwiki#base#resolve_scheme(segments[0].'#', 0) let wikifile = link_infos[6] let syntax = VimwikiGet('syntax', link_infos[0]) let rxheader = g:vimwiki_{syntax}_header_search let rxbold = g:vimwiki_{syntax}_bold_search if !filereadable(wikifile) return [] endif let filecontent = readfile(wikifile) let anchor_level = ['', '', '', '', '', '', ''] let anchors = [] for line in filecontent " collect headers let h_match = matchlist(line, rxheader) if !empty(h_match) let header = vimwiki#u#trim(h_match[2]) let level = len(h_match[1]) let anchor_level[level-1] = header for l in range(level, 6) let anchor_level[l] = '' endfor call add(anchors, header) let complete_anchor = '' for l in range(level-1) if anchor_level[l] != '' let complete_anchor .= anchor_level[l].'#' endif endfor let complete_anchor .= header call add(anchors, complete_anchor) endif " collect bold text (there can be several in one line) let bold_count = 0 let bold_end = 0 while 1 let bold_text = matchstr(line, rxbold, bold_end, bold_count) let bold_end = matchend(line, rxbold, bold_end, bold_count) + 1 if bold_text == "" break endif let anchor_level[6] = bold_text call add(anchors, bold_text) let complete_anchor = '' for l in range(6) if anchor_level[l] != '' let complete_anchor .= anchor_level[l].'#' endif endfor let complete_anchor .= bold_text call add(anchors, complete_anchor) let bold_count += 1 endwhile endfor let filtered_anchors = [] let given_anchor = join(segments[1:], '#') for anchor in anchors if anchor =~# '^'.given_anchor call add(filtered_anchors, segments[0].'#'.anchor) endif endfor return filtered_anchors endif endif endfunction setlocal omnifunc=g:complete_wikifiles " omnicomplete }}} " MISC }}} " LIST STUFF {{{ " settings necessary for the automatic formatting of lists setlocal autoindent setlocal nosmartindent setlocal nocindent setlocal comments="" setlocal formatoptions-=c setlocal formatoptions-=r setlocal formatoptions-=o setlocal formatoptions-=2 setlocal formatoptions+=n "Create 'formatlistpat' let &formatlistpat = g:vimwiki_rxListItem if !empty(&langmap) " Valid only if langmap is a comma separated pairs of chars let s:l_o = matchstr(&langmap, '\C,\zs.\zeo,') if s:l_o exe 'nnoremap '.s:l_o.' :call vimwiki#lst#kbd_o()a' endif let s:l_O = matchstr(&langmap, '\C,\zs.\zeO,') if s:l_O exe 'nnoremap '.s:l_O.' :call vimwiki#lst#kbd_O()a' endif endif " LIST STUFF }}} " FOLDING {{{ " Folding list items {{{ function! VimwikiFoldListLevel(lnum) "{{{ return vimwiki#lst#fold_level(a:lnum) endfunction "}}} " Folding list items }}} " Folding sections and code blocks {{{ function! VimwikiFoldLevel(lnum) "{{{ let line = getline(a:lnum) " Header/section folding... if line =~ g:vimwiki_rxHeader return '>'.vimwiki#u#count_first_sym(line) " Code block folding... elseif line =~ '^\s*'.g:vimwiki_rxPreStart return 'a1' elseif line =~ '^\s*'.g:vimwiki_rxPreEnd.'\s*$' return 's1' else return "=" endif endfunction "}}} " Constants used by VimwikiFoldText {{{ " use \u2026 and \u21b2 (or \u2424) if enc=utf-8 to save screen space let s:ellipsis = (&enc ==? 'utf-8') ? "\u2026" : "..." let s:ell_len = strlen(s:ellipsis) let s:newline = (&enc ==? 'utf-8') ? "\u21b2 " : " " let s:tolerance = 5 " }}} function! s:shorten_text_simple(text, len) "{{{ unused let spare_len = a:len - len(a:text) return (spare_len>=0) ? [a:text,spare_len] : [a:text[0:a:len].s:ellipsis, -1] endfunction "}}} " s:shorten_text(text, len) = [string, spare] with "spare" = len-strlen(string) " for long enough "text", the string's length is within s:tolerance of "len" " (so that -s:tolerance <= spare <= s:tolerance, "string" ends with s:ellipsis) function! s:shorten_text(text, len) "{{{ returns [string, spare] let spare_len = a:len - strlen(a:text) if (spare_len + s:tolerance >= 0) return [a:text, spare_len] endif " try to break on a space; assumes a:len-s:ell_len >= s:tolerance let newlen = a:len - s:ell_len let idx = strridx(a:text, ' ', newlen + s:tolerance) let break_idx = (idx + s:tolerance >= newlen) ? idx : newlen return [a:text[0:break_idx].s:ellipsis, newlen - break_idx] endfunction "}}} function! VimwikiFoldText() "{{{ let line = getline(v:foldstart) let main_text = substitute(line, '^\s*', repeat(' ',indent(v:foldstart)), '') let fold_len = v:foldend - v:foldstart + 1 let len_text = ' ['.fold_len.'] ' if line !~ '^\s*'.g:vimwiki_rxPreStart let [main_text, spare_len] = s:shorten_text(main_text, 50) return main_text.len_text else " fold-text for code blocks: use one or two of the starting lines let [main_text, spare_len] = s:shorten_text(main_text, 24) let line1 = substitute(getline(v:foldstart+1), '^\s*', ' ', '') let [content_text, spare_len] = s:shorten_text(line1, spare_len+20) if spare_len > s:tolerance && fold_len > 3 let line2 = substitute(getline(v:foldstart+2), '^\s*', s:newline, '') let [more_text, spare_len] = s:shorten_text(line2, spare_len+12) let content_text .= more_text endif return main_text.len_text.content_text endif endfunction "}}} " Folding sections and code blocks }}} " FOLDING }}} " COMMANDS {{{ command! -buffer Vimwiki2HTML \ silent noautocmd w \ let res = vimwiki#html#Wiki2HTML(expand(VimwikiGet('path_html')), \ expand('%')) \ \ if res != '' | echo 'Vimwiki: HTML conversion is done.' | endif command! -buffer Vimwiki2HTMLBrowse \ silent noautocmd w \ call vimwiki#base#system_open_link(vimwiki#html#Wiki2HTML( \ expand(VimwikiGet('path_html')), \ expand('%'))) command! -buffer VimwikiAll2HTML \ call vimwiki#html#WikiAll2HTML(expand(VimwikiGet('path_html'))) command! -buffer VimwikiTOC call vimwiki#base#table_of_contents(1) command! -buffer VimwikiNextLink call vimwiki#base#find_next_link() command! -buffer VimwikiPrevLink call vimwiki#base#find_prev_link() command! -buffer VimwikiDeleteLink call vimwiki#base#delete_link() command! -buffer VimwikiRenameLink call vimwiki#base#rename_link() command! -buffer VimwikiFollowLink call vimwiki#base#follow_link('nosplit') command! -buffer VimwikiGoBackLink call vimwiki#base#go_back_link() command! -buffer VimwikiSplitLink call vimwiki#base#follow_link('split') command! -buffer VimwikiVSplitLink call vimwiki#base#follow_link('vsplit') command! -buffer -nargs=? VimwikiNormalizeLink call vimwiki#base#normalize_link() command! -buffer VimwikiTabnewLink call vimwiki#base#follow_link('tabnew') command! -buffer VimwikiGenerateLinks call vimwiki#base#generate_links() command! -buffer -nargs=0 VimwikiBacklinks call vimwiki#base#backlinks() command! -buffer -nargs=0 VWB call vimwiki#base#backlinks() exe 'command! -buffer -nargs=* VimwikiSearch lvimgrep '. \ escape(VimwikiGet('path').'**/*'.VimwikiGet('ext'), ' ') exe 'command! -buffer -nargs=* VWS lvimgrep '. \ escape(VimwikiGet('path').'**/*'.VimwikiGet('ext'), ' ') command! -buffer -nargs=+ VimwikiGoto call vimwiki#base#goto() " list commands command! -buffer -nargs=+ VimwikiReturn call CR() command! -buffer -range -nargs=1 VimwikiChangeSymbolTo call vimwiki#lst#change_marker(, , , 'n') command! -buffer -range -nargs=1 VimwikiListChangeSymbolI call vimwiki#lst#change_marker(, , , 'i') command! -buffer -nargs=1 VimwikiChangeSymbolInListTo call vimwiki#lst#change_marker_in_list() command! -buffer -range VimwikiToggleListItem call vimwiki#lst#toggle_cb(, ) command! -buffer -range -nargs=+ VimwikiListChangeLvl call vimwiki#lst#change_level(, , ) command! -buffer -range VimwikiRemoveSingleCB call vimwiki#lst#remove_cb(, ) command! -buffer VimwikiRemoveCBInList call vimwiki#lst#remove_cb_in_list() command! -buffer VimwikiRenumberList call vimwiki#lst#adjust_numbered_list() command! -buffer VimwikiRenumberAllLists call vimwiki#lst#adjust_whole_buffer() command! -buffer VimwikiListToggle call vimwiki#lst#toggle_list_item() " table commands command! -buffer -nargs=* VimwikiTable call vimwiki#tbl#create() command! -buffer VimwikiTableAlignQ call vimwiki#tbl#align_or_cmd('gqq') command! -buffer VimwikiTableAlignW call vimwiki#tbl#align_or_cmd('gww') command! -buffer VimwikiTableMoveColumnLeft call vimwiki#tbl#move_column_left() command! -buffer VimwikiTableMoveColumnRight call vimwiki#tbl#move_column_right() " diary commands command! -buffer VimwikiDiaryNextDay call vimwiki#diary#goto_next_day() command! -buffer VimwikiDiaryPrevDay call vimwiki#diary#goto_prev_day() " COMMANDS }}} " KEYBINDINGS {{{ if g:vimwiki_use_mouse nmap nmap nnoremap <2-LeftMouse> :call vimwiki#base#follow_link("nosplit", "\2-LeftMouse>") nnoremap :VimwikiSplitLink nnoremap :VimwikiVSplitLink nnoremap :VimwikiGoBackLink endif if !hasmapto('Vimwiki2HTML') nmap wh Vimwiki2HTML endif nnoremap