lib/soywiki.vim in soywiki-0.1.5 vs lib/soywiki.vim in soywiki-0.1.6
- old
+ new
@@ -89,11 +89,16 @@
return substitute(a:page, '/', '.', 'g')
endfunc
func! s:list_pages()
let s:search_for_link = ""
- call s:page_list_window(s:get_page_list(), 'select-page', "Select page: ")
+ let pages = s:get_page_list()
+ if len(pages) == 0
+ echom "There are no wiki pages yet but this one."
+ else
+ call s:page_list_window(pages, 'select-page', "Select page: ")
+ end
endfunc
func! s:trim_link(link)
let link = matchstr(a:link, s:wiki_link_pattern)
return link
@@ -131,10 +136,17 @@
func! s:follow_link_under_cursor(split)
let link = s:link_under_cursor()
if link == ""
echom link . " is not a wiki link"
return ""
+ elseif line('.') == 1
+ " SPECIAL CASE
+ " close window
+ if winnr('$') > 1
+ close
+ endif
+ return
else
call s:load_page(link, a:split)
endif
endfunc
@@ -146,37 +158,44 @@
return ""
end
return s:link_under_cursor()
endfunc
+" --------------------------------------------------------------------------------
+" LOAD PAGE
+
func! s:load_page(page, split)
if (s:is_wiki_page())
write
endif
let file = s:pagetitle2file(a:page)
let title = s:filename2pagetitle(a:page)
+ if bufwinnr(file) != -1
+ exec bufwinnr(file)."wincmd w"
+ return
+ endif
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
- else
- exec "botright split ". file
+ exec "rightbelow vsplit ". file
+ elseif (a:split == 1)
+ exec "rightbelow split ". file
+ elseif (a:split == 0)
+ exec "e ".file
endif
- if (a:split == 0)
- wincmd p
- close
- endif
- if len(s:search_for_link) > 0
+ if s:search_for_link != ''
let res = search(s:search_for_link, 'cw')
let s:search_for_link = ''
+ else
+ normal gg
endif
endfunc
func! s:load_most_recently_modified_page(index)
let pages = split(system(s:ls_command), "\n")
@@ -250,23 +269,33 @@
endfunc
" -------------------------------------------------------------------------------
" select Page
+func! s:omit_this_page(page_list)
+ if exists("s:return_to_bufname")
+ let page_list = filter( a:page_list, 'v:val != "'.s:return_to_bufname.'"')
+ return page_list
+ else
+ return a:page_list
+ endif
+endfunc
+
" This function both sets a script variable and returns the value.
func! s:get_page_list()
" no file current in buffer
- if len(bufname('%')) == 0
+ if len(bufname('')) == 0
return split(system(s:ls_command), "\n")
elseif bufname('') == 'pages-linking-in'
" this needs refactoring to rely less on state
return s:pages_linking_in
else
- return split(system(s:ls_command . " | grep -vF '" . s:page_title() . "'" ), "\n")
+ return s:omit_this_page(split(system(s:ls_command), "\n"))
endif
endfunction
+
func! s:pages_in_this_namespace(pages)
let namespace = s:page_namespace()
let pages = filter( a:pages, 'v:val =~ "^' . namespace . '\."')
" strip leading namespace
return map(pages, "substitute(v:val, '^" . namespace . "\.', '', '') ")
@@ -295,10 +324,11 @@
endfunc
function! s:page_list_window(page_match_list, buffer_name, prompt)
" remember the original window
let s:return_to_winnr = winnr()
+ let s:return_to_bufname = s:filename2pagetitle(bufname(''))
let s:matching_pages = a:page_match_list
exec "topleft split ".a:buffer_name
setlocal completefunc=CompletePageTitle
setlocal buftype=nofile
setlocal noswapfile
@@ -424,11 +454,11 @@
if len(namespace) > 0
call system("mkdir -p " . namespace)
endif
call writefile([page_title, '', ''], file)
endif
- exec "split ".file
+ exec "rightbelow vsplit ".file
else
let targetWindow = bufwinnr(bufnr(file))
exe targetWindow."wincmd w"
end
if mode == 'append'
@@ -476,11 +506,11 @@
else " seamless
echom "Expanding seamlessly. Please wait."
let res = system(s:expand_command . " seamful " . bufname('%'))
endif
if a:vertical
- botright vnew
+ rightbelow vnew
else
new
endif
setlocal buftype=nofile "scratch buffer for viewing; user can write
silent! put =res
@@ -509,11 +539,10 @@
func! s:global_mappings()
noremap <leader>m :call <SID>list_pages()<CR>
noremap <leader>M :call <SID>list_pages_linking_in()<CR>
noremap <silent> <leader>o :call <SID>open_href()<cr>
nnoremap <silent> q :close<cr>
- nnoremap <silent> <C-h> :close<cr>
" reflow text
nnoremap \ gwap
" insert a line
nmap <Leader>- o<Esc>k72i-<Esc><CR>
@@ -537,10 +566,10 @@
func! s:prep_buffer()
if (s:is_wiki_page())
set textwidth=72
nnoremap <buffer> <cr> :call <SID>follow_link_under_cursor(0)<cr>
nnoremap <buffer> <c-l> :call <SID>follow_link_under_cursor(2)<cr>
- nnoremap <buffer> <c-n> :call <SID>follow_link_under_cursor(1)<cr>
+ nnoremap <buffer> <c-h> :call <SID>follow_link_under_cursor(1)<cr>
noremap <buffer> <leader>f :call <SID>follow_link(0)<CR>
noremap <buffer> <c-j> :call <SID>find_next_wiki_link(0)<CR>
noremap <buffer> <c-k> :call <SID>find_next_wiki_link(1)<CR>
command! -bar -nargs=1 -range -complete=file SWCreate :call <SID>create_page(<f-args>)