lib/soywiki.vim in soywiki-0.1.3 vs lib/soywiki.vim in soywiki-0.1.4

- old
+ new

@@ -1,12 +1,13 @@ " Vim script that turns Vim into a personal wiki " Maintainer: Daniel Choi <dhchoi@gmail.com> " License: MIT License (c) 2011 Daniel Choi " This regex matches namedspaced WikiWords and unqualified WikiWords -let s:wiki_link_pattern = '\C\<\([a-z][[:alnum:]_]\+\.\)\?[A-Z][a-z]\+[A-Z]\w*\>' +let s:wiki_link_pattern = '\C\m\<\([a-z][[:alnum:]_]\+\.\)\?[A-Z][a-z]\+[A-Z]\w*\>' + let s:http_link_pattern = 'https\?:[^ >)\]]\+' let s:rename_links_command = 'soywiki-rename ' let s:find_pages_linking_in_command = 'soywiki-pages-linking-in ' let s:expand_command = 'soywiki-expand ' let s:ls_command = 'soywiki-ls-t ' @@ -38,10 +39,11 @@ " page must have namespace if len(segments) == 2 return get(segments, 0) else call s:display_missing_namespace_error(len(segments)) + return "" endif endfunc func! s:page_namespace() return s:namespace_of_title(s:page_title()) @@ -90,15 +92,19 @@ func! s:list_pages() let s:search_for_link = "" call s:page_list_window(s:get_page_list(), "Select page: ") endfunc +func! s:trim_link(link) + let link = matchstr(a:link, s:wiki_link_pattern) + return link +endfunc + " returns a fully namespaced link func! s:link_under_cursor() - let link = expand("<cWORD>") - " strip off non-letters at the end (e.g., a comma) - let link = substitute(link, '[^[:alnum:]]*$', '', '') + let link = s:trim_link(expand("<cWORD>")) + " strip off non-letters at the end and beginning (e.g., a comma) if ! s:has_namespace(link) let link = s:infer_namespace(link) endif if match(link, s:wiki_link_pattern) == -1 if match(link, s:http_link_pattern) != -1 @@ -149,10 +155,13 @@ let file = s:pagetitle2file(a:page) let title = s:filename2pagetitle(a:page) if (!filereadable(file)) " create the file let namespace = s:namespace_of_title(a:page) + if namespace == "" + return + end call system("mkdir -p " . namespace) call writefile([title, '', ''], file) endif if (a:split == 2) exec "botright vsplit ". file @@ -255,12 +264,11 @@ func! s:pages_in_this_namespace(pages) let namespace = s:page_namespace() let pages = filter( a:pages, 'v:val =~ "^' . namespace . '\."') " strip leading namespace - let pages = map( pages, "substitute(v:val, '^" . namespace . "\.', '', '') " ) - return pages + return map(pages, "substitute(v:val, '^" . namespace . "\.', '', '') ") endfunc " When user press TAB after typing a few characters in the page selection " window, if the user started typing a namespace (which starts with a " lowercase letter), try to complete it. Otherwise take no action. @@ -302,48 +310,41 @@ normal $ call feedkeys("a\<c-x>\<c-u>\<c-p>", 't') " call feedkeys("a", 't') endfunction -" This function assumes s:matching_pages has been set by the calling function function! CompletePageTitle(findstart, base) - let fragment = expand("<cWORD>") - if !exists("s:matching_pages") - let s:matching_pages = s:get_page_list() - endif - if match(fragment, '^[A-Z]') == 0 - " we have a WikiWord without a namespace; filter down to pages in pages in this - " namespace - let s:matching_pages = s:pages_in_this_namespace(s:matching_pages) - endif if a:findstart " locate the start of the word - " Assume we're in a page select window if there is a ': ' in the line. - " Admittedly, this is not work well in all cases - if bufname('') == 'page-list-buffer' - " by starting after prompt ': ' - let start = match(getline('.'), ': ') + 2 - else - " locate the start of the word - let line = getline('.') - let start = col('.') - 1 - while start > 0 && line[start - 1] =~ '\a' - let start -= 1 - endwhile - end + let line = getline('.') + let start = col('.') - 1 + while start > 0 && line[start - 1] =~ '\m[[:alnum:]\.]' + let start -= 1 + endwhile return start else let base = s:trimString(a:base) if (base == '') - return s:matching_pages + return s:get_page_list() else let res = [] - for m in s:matching_pages - if m =~ '\c' . base - call add(res, m) - endif - endfor + if bufname('') == 'page-list-buffer' + let pages = s:get_page_list() + for m in pages + if m =~ '\c' . base + call add(res, m) + endif + endfor + else + " autocomplete inline + let pages = base =~ '\C^[a-z]' ? s:get_page_list() : s:pages_in_this_namespace(s:get_page_list()) + for m in pages + if m =~ '^\c' . base + call add(res, m) + endif + endfor + endif return res endif endif endfun @@ -448,14 +449,17 @@ silent put! =divider silent put='' endfunc "------------------------------------------------------------------------ " SEARCH -func! s:wiki_search(pattern) - +func! s:wiki_search(pattern, in_this_namespace) let pattern = (empty(a:pattern) ? @/ : a:pattern) - execute printf('vimgrep/%s/ %s', pattern, "**/*") + if a:in_this_namespace + execute printf('vimgrep/\c%s/ %s', pattern, s:page_namespace()."/*") + else + execute printf('vimgrep/\c%s/ %s', pattern, "*/*") + endif endfunc "------------------------------------------------------------------------ " This opens a new buffer with all the lines with just WikiLinks on them " expanded (recursively). This is not a wiki buffer but a text buffer @@ -515,11 +519,11 @@ command! -bar -nargs=1 -range -complete=file SWAppend :<line1>,<line2>call s:extract(<f-args>, 'append', 0) command! -bar -nargs=1 -range -complete=file SWInsert :<line1>,<line2>call s:extract(<f-args>, 'insert', 0) command! -bar -nargs=1 -range -complete=file SWLinkAppend :<line1>,<line2>call s:extract(<f-args>, 'append', 1) command! -bar -nargs=1 -range -complete=file SWLinkInsert :<line1>,<line2>call s:extract(<f-args>, 'insert', 1) - command! -bar -nargs=1 SWSearch :call s:wiki_search(<f-args>) - " TODO a search confined to current namespace + command! -bar -nargs=1 SWSearch :call s:wiki_search(<f-args>, 0) + command! -bar -nargs=1 SWNamespaceSearch :call s:wiki_search(<f-args>, 1) autocmd BufReadPost,BufNewFile,WinEnter,BufEnter,BufNew,BufAdd * call s:highlight_wikiwords() autocmd BufEnter * call s:prep_buffer() endfunc