clean up lst.vim, move functions around

no actual change in behavior or so
This commit is contained in:
EinfachToll 2014-01-06 13:03:07 +01:00
parent 9821aaf528
commit 373ad1cee5

View File

@ -83,14 +83,34 @@ endfunction "}}}
" incrementation functions for the various kinds of numbers }}} " incrementation functions for the various kinds of numbers }}}
" utility function {{{
function! s:substitute_rx_in_line(lnum, pattern, new_string) "{{{ function! s:substitute_rx_in_line(lnum, pattern, new_string) "{{{
call setline(a:lnum, substitute(getline(a:lnum), a:pattern, a:new_string, '')) call setline(a:lnum, substitute(getline(a:lnum), a:pattern, a:new_string,
\ ''))
endfunction "}}} endfunction "}}}
function! s:substitute_string_in_line(lnum, pattern, new_string) "{{{ function! s:substitute_string_in_line(lnum, old_string, new_string) "{{{
call s:substitute_rx_in_line(a:lnum, vimwiki#u#escape(a:pattern), a:new_string) call s:substitute_rx_in_line(a:lnum, vimwiki#u#escape(a:old_string),
\ a:new_string)
endfunction "}}} endfunction "}}}
function! s:first_char(string) "{{{
return matchstr(a:string, '^.')
endfunction "}}}
if exists("*strdisplaywidth") "{{{
function! s:string_length(str)
return strdisplaywidth(a:str)
endfunction
else
function! s:string_length(str)
return strlen(substitute(a:str, '.', 'x', 'g'))
endfunction
endif "}}}
"utility functions }}}
"Returns: the mainly used data structure in this file "Returns: the mainly used data structure in this file
"An item represents a single list item and is a dictionary with the keys "An item represents a single list item and is a dictionary with the keys
"lnum - the line number of the list item "lnum - the line number of the list item
@ -103,13 +123,15 @@ function! s:get_item(lnum) "{{{
let item.type = 0 let item.type = 0
return item return item
endif endif
let matches = matchlist(getline(a:lnum), g:vimwiki_rxListItem) let matches = matchlist(getline(a:lnum), g:vimwiki_rxListItem)
if matches == [] || (matches[1] == '' && matches[2] == '') || (matches[1] != '' && matches[2] != '') if matches == [] ||
\ (matches[1] == '' && matches[2] == '') ||
\ (matches[1] != '' && matches[2] != '')
let item.type = 0 let item.type = 0
return item return item
endif endif
let item.cb = matches[3] let item.cb = matches[3]
if matches[1] != '' if matches[1] != ''
@ -123,6 +145,10 @@ function! s:get_item(lnum) "{{{
return item return item
endfunction "}}} endfunction "}}}
function! s:empty_item() "{{{
return {'type': 0}
endfunction "}}}
"Returns: level of the line "Returns: level of the line
"0 is the 'highest' level "0 is the 'highest' level
function! s:get_level(lnum) "{{{ function! s:get_level(lnum) "{{{
@ -155,15 +181,11 @@ function! s:regexp_of_marker(item) "{{{
endif endif
endfunction "}}} endfunction "}}}
function! s:empty_item() "{{{ "Returns: the list item after a:item or an empty item
return {'type': 0} "If a:ignore_kind is 1, the markers can differ
endfunction "}}} function! s:get_next_list_item(item, ignore_kind) "{{{
"Returns: the list item after a:item in its list or empty item
"If a:all is 1, the markers can differ
function! s:get_next_list_item(item, all) "{{{
let org_lvl = s:get_level(a:item.lnum) let org_lvl = s:get_level(a:item.lnum)
if !a:all if !a:ignore_kind
let org_regex = s:regexp_of_marker(a:item) let org_regex = s:regexp_of_marker(a:item)
endif endif
@ -171,7 +193,7 @@ function! s:get_next_list_item(item, all) "{{{
while cur_ln <= line('$') while cur_ln <= line('$')
let cur_lvl = s:get_level(cur_ln) let cur_lvl = s:get_level(cur_ln)
if cur_lvl <= org_lvl if cur_lvl <= org_lvl
if a:all if a:ignore_kind
return s:get_any_item_of_level(cur_ln, cur_lvl, org_lvl) return s:get_any_item_of_level(cur_ln, cur_lvl, org_lvl)
else else
return s:get_item_of_level(cur_ln, cur_lvl, org_lvl, org_regex) return s:get_item_of_level(cur_ln, cur_lvl, org_lvl, org_regex)
@ -182,11 +204,11 @@ function! s:get_next_list_item(item, all) "{{{
return s:empty_item() return s:empty_item()
endfunction "}}} endfunction "}}}
"Returns: the list item before a:item in its list or empty item "Returns: the list item before a:item or an empty item
"If a:all is 1, the markers can differ "If a:ignore_kind is 1, the markers can differ
function! s:get_prev_list_item(item, all) "{{{ function! s:get_prev_list_item(item, ignore_kind) "{{{
let org_lvl = s:get_level(a:item.lnum) let org_lvl = s:get_level(a:item.lnum)
if !a:all if !a:ignore_kind
let org_regex = s:regexp_of_marker(a:item) let org_regex = s:regexp_of_marker(a:item)
endif endif
@ -194,7 +216,7 @@ function! s:get_prev_list_item(item, all) "{{{
while cur_ln >= 1 while cur_ln >= 1
let cur_lvl = s:get_level(cur_ln) let cur_lvl = s:get_level(cur_ln)
if cur_lvl <= org_lvl if cur_lvl <= org_lvl
if a:all if a:ignore_kind
return s:get_any_item_of_level(cur_ln, cur_lvl, org_lvl) return s:get_any_item_of_level(cur_ln, cur_lvl, org_lvl)
else else
return s:get_item_of_level(cur_ln, cur_lvl, org_lvl, org_regex) return s:get_item_of_level(cur_ln, cur_lvl, org_lvl, org_regex)
@ -226,10 +248,6 @@ function! s:get_any_item_of_level(cur_ln, cur_lvl, org_lvl) "{{{
endif endif
endfunction "}}} endfunction "}}}
function! s:first_char(string) "{{{
return matchstr(a:string, '^.')
endfunction "}}}
"Returns: 1, a, i, A, I or '' "Returns: 1, a, i, A, I or ''
"If in doubt if alphanumeric character or romanian "If in doubt if alphanumeric character or romanian
"numeral, peek in the previous line "numeral, peek in the previous line
@ -291,11 +309,10 @@ function! s:guess_kind_of_numbered_item(item) "{{{
endif endif
endfunction "}}} endfunction "}}}
function! s:get_first_item_in_list(item, ignore_kind) "{{{
function! s:get_first_item_in_list(item, all) "{{{
let cur_item = a:item let cur_item = a:item
while 1 while 1
let prev_item = s:get_prev_list_item(cur_item, a:all) let prev_item = s:get_prev_list_item(cur_item, a:ignore_kind)
if prev_item.type == 0 if prev_item.type == 0
break break
else else
@ -305,10 +322,10 @@ function! s:get_first_item_in_list(item, all) "{{{
return cur_item return cur_item
endfunction "}}} endfunction "}}}
function! s:get_last_item_in_list(item, all) "{{{ function! s:get_last_item_in_list(item, ignore_kind) "{{{
let cur_item = a:item let cur_item = a:item
while 1 while 1
let next_item = s:get_next_list_item(cur_item, a:all) let next_item = s:get_next_list_item(cur_item, a:ignore_kind)
if next_item.type == 0 if next_item.type == 0
break break
else else
@ -325,7 +342,8 @@ endfunction "}}}
function! s:get_next_line(lnum, ...) "{{{ function! s:get_next_line(lnum, ...) "{{{
if getline(a:lnum) =~# '^\s*'.g:vimwiki_rxPreStart if getline(a:lnum) =~# '^\s*'.g:vimwiki_rxPreStart
let cur_ln = a:lnum + 1 let cur_ln = a:lnum + 1
while cur_ln <= line('$') && getline(cur_ln) !~# '^\s*'.g:vimwiki_rxPreEnd.'\s*$' while cur_ln <= line('$') &&
\ getline(cur_ln) !~# '^\s*'.g:vimwiki_rxPreEnd.'\s*$'
let cur_ln += 1 let cur_ln += 1
endwhile endwhile
let next_line = nextnonblank(cur_ln+1) let next_line = nextnonblank(cur_ln+1)
@ -337,7 +355,8 @@ function! s:get_next_line(lnum, ...) "{{{
let next_line = s:get_next_line(next_line, 1) let next_line = s:get_next_line(next_line, 1)
endif endif
if next_line < 0 || next_line > line('$') || (getline(next_line) =~# g:vimwiki_rxHeader && a:0 == 0) if next_line < 0 || next_line > line('$') ||
\ (getline(next_line) =~# g:vimwiki_rxHeader && a:0 == 0)
return 0 return 0
endif endif
@ -360,7 +379,8 @@ function! s:get_prev_line(lnum) "{{{
let prev_line = cur_ln let prev_line = cur_ln
endif endif
if prev_line < 0 || prev_line > line('$') || getline(prev_line) =~# g:vimwiki_rxHeader if prev_line < 0 || prev_line > line('$') ||
\ getline(prev_line) =~# g:vimwiki_rxHeader
return 0 return 0
endif endif
@ -377,7 +397,8 @@ function! s:get_first_child(item) "{{{
if cur_item.type != 0 && s:get_level(cur_item.lnum) > org_lvl if cur_item.type != 0 && s:get_level(cur_item.lnum) > org_lvl
return cur_item return cur_item
endif endif
if cur_item.lnum > line('$') || cur_item.lnum <= 0 || s:get_level(cur_item.lnum) <= org_lvl if cur_item.lnum > line('$') || cur_item.lnum <= 0 ||
\ s:get_level(cur_item.lnum) <= org_lvl
return s:empty_item() return s:empty_item()
endif endif
let cur_item = s:get_item(s:get_next_line(cur_item.lnum)) let cur_item = s:get_item(s:get_next_line(cur_item.lnum))
@ -406,6 +427,32 @@ function! s:get_next_child_item(parent, child) "{{{
return s:empty_item() return s:empty_item()
endfunction "}}} endfunction "}}}
function! s:get_parent(item) "{{{
let parent_line = 0
let cur_ln = prevnonblank(a:item.lnum)
let child_lvl = s:get_level(cur_ln)
if child_lvl == 0
return s:empty_item()
endif
while 1
let cur_ln = s:get_prev_line(cur_ln)
if cur_ln == 0 | break | endif
let cur_lvl = s:get_level(cur_ln)
if cur_lvl < child_lvl
let cur_item = s:get_item(cur_ln)
if cur_item.type == 0
let child_lvl = cur_lvl
continue
endif
let parent_line = cur_ln
break
endif
endwhile
return s:get_item(parent_line)
endfunction "}}}
"Renumbers the current list from a:item on downwards "Renumbers the current list from a:item on downwards
"Returns: the last item that was adjusted "Returns: the last item that was adjusted
function! s:adjust_numbered_list_below(item, recursive) "{{{ function! s:adjust_numbered_list_below(item, recursive) "{{{
@ -457,34 +504,38 @@ function! s:adjust_items_recursively(parent) "{{{
endfunction "}}} endfunction "}}}
"Renumbers the list a:item is in. "Renumbers the list a:item is in.
"If a:all == 0, only the items which have the same kind of marker as a:item are "If a:ignore_kind == 0, only the items which have the same kind of marker as
"considered, otherwise all items. "a:item are considered, otherwise all items.
"Returns: the last item that was adjusted "Returns: the last item that was adjusted
function! s:adjust_numbered_list(item, all, recursive) "{{{ function! s:adjust_numbered_list(item, ignore_kind, recursive) "{{{
if !(a:item.type == 2 || (a:item.type == 1 && (a:all || a:recursive))) if !(a:item.type == 2 || (a:item.type == 1 && (a:ignore_kind || a:recursive)))
return s:empty_item() return s:empty_item()
end end
let first_item = s:get_first_item_in_list(a:item, a:all) let first_item = s:get_first_item_in_list(a:item, a:ignore_kind)
while 1 while 1
if first_item.type == 2 if first_item.type == 2
let new_mrkr = s:guess_kind_of_numbered_item(first_item) . first_item.mrkr[-1:] let new_mrkr = s:guess_kind_of_numbered_item(first_item) .
call s:substitute_string_in_line(first_item.lnum, first_item.mrkr, new_mrkr) \ first_item.mrkr[-1:]
call s:substitute_string_in_line(first_item.lnum, first_item.mrkr,
\ new_mrkr)
let first_item.mrkr = new_mrkr let first_item.mrkr = new_mrkr
endif endif
let last_item = s:adjust_numbered_list_below(first_item, a:recursive) let last_item = s:adjust_numbered_list_below(first_item, a:recursive)
let next_first_item = s:get_next_list_item(last_item, 1) let next_first_item = s:get_next_list_item(last_item, 1)
if a:all == 0 || next_first_item.type == 0 if a:ignore_kind == 0 || next_first_item.type == 0
return last_item return last_item
endif endif
let first_item = next_first_item let first_item = next_first_item
endwhile endwhile
endfunction "}}} endfunction "}}}
"Returns: the (rounded) rate of checkboxed list item in percent "checkbox stuff {{{
"Returns: the rate of checkboxed list item in percent
function! s:get_rate(item) "{{{ function! s:get_rate(item) "{{{
if a:item.type == 0 || a:item.cb == '' if a:item.type == 0 || a:item.cb == ''
return -1 return -1
@ -523,6 +574,7 @@ function! s:set_state_plus_children(item, new_rate) "{{{
endwhile endwhile
endfunction "}}} endfunction "}}}
"Returns: the appropriate symbol for a given percent rate
function! s:rate_to_state(rate) "{{{ function! s:rate_to_state(rate) "{{{
let state = '' let state = ''
if a:rate == 100 if a:rate == 100
@ -539,32 +591,8 @@ function! s:rate_to_state(rate) "{{{
return state return state
endfunction "}}} endfunction "}}}
function! s:get_parent(item) "{{{ "updates the symbol of a checkboxed item according to the symbols of its
let parent_line = 0 "children
let cur_ln = prevnonblank(a:item.lnum)
let child_lvl = s:get_level(cur_ln)
if child_lvl == 0
return s:empty_item()
endif
while 1
let cur_ln = s:get_prev_line(cur_ln)
if cur_ln == 0 | break | endif
let cur_lvl = s:get_level(cur_ln)
if cur_lvl < child_lvl
let cur_item = s:get_item(cur_ln)
if cur_item.type == 0
let child_lvl = cur_lvl
continue
endif
let parent_line = cur_ln
break
endif
endwhile
return s:get_item(parent_line)
endfunction "}}}
function! s:update_state(item) "{{{ function! s:update_state(item) "{{{
if a:item.type == 0 || a:item.cb == '' if a:item.type == 0 || a:item.cb == ''
return return
@ -613,12 +641,24 @@ function! s:create_cb(item) "{{{
let new_item = a:item let new_item = a:item
let new_item.cb = g:vimwiki_listsyms[0] let new_item.cb = g:vimwiki_listsyms[0]
call s:substitute_rx_in_line(new_item.lnum, vimwiki#u#escape(new_item.mrkr) . '\zs\ze', ' [' . new_item.cb . ']') call s:substitute_rx_in_line(new_item.lnum,
\ vimwiki#u#escape(new_item.mrkr) . '\zs\ze', ' [' . new_item.cb . ']')
call s:update_state(new_item) call s:update_state(new_item)
return 1 return 1
endfunction "}}} endfunction "}}}
function! s:remove_cb(item) "{{{
let item = a:item
if item.type != 0 && item.cb != ''
let item.cb = ''
call s:substitute_rx_in_line(item.lnum, '\s\+\[.\]', '')
endif
return item
endfunction "}}}
"checkbox stuff }}}
"Returns: the item if there is one in a:lnum "Returns: the item if there is one in a:lnum
"else the multiline item a:lnum belongs to "else the multiline item a:lnum belongs to
function! s:get_corresponding_item(lnum) "{{{ function! s:get_corresponding_item(lnum) "{{{
@ -656,7 +696,7 @@ function! s:get_last_line_of_item_incl_children(item) "{{{
endfunction "}}} endfunction "}}}
"Returns: the last line of a (possibly multiline) item "Returns: the last line of a (possibly multiline) item
"Note: there can be other list items inbetween these lines "Note: there can be other list items inbetween the first and last lines
function! s:get_last_line_of_item(item) "{{{ function! s:get_last_line_of_item(item) "{{{
if a:item.type == 0 | return 0 | endif if a:item.type == 0 | return 0 | endif
let org_lvl = s:get_level(a:item.lnum) let org_lvl = s:get_level(a:item.lnum)
@ -672,7 +712,8 @@ function! s:get_last_line_of_item(item) "{{{
let last_corresponding_line = cur_ln let last_corresponding_line = cur_ln
let cur_ln = s:get_next_line(cur_ln) let cur_ln = s:get_next_line(cur_ln)
else else
let cur_ln = s:get_next_line(s:get_last_line_of_item_incl_children(cur_item)) let cur_ln = s:get_next_line(
\ s:get_last_line_of_item_incl_children(cur_item))
endif endif
endwhile endwhile
@ -685,16 +726,6 @@ function! s:text_begin(lnum) "{{{
return s:string_length(matchstr(getline(a:lnum), g:vimwiki_rxListItem)) return s:string_length(matchstr(getline(a:lnum), g:vimwiki_rxListItem))
endfunction "}}} endfunction "}}}
if exists("*strdisplaywidth") "{{{
function! s:string_length(str)
return strdisplaywidth(a:str)
endfunction
else
function! s:string_length(str)
return strlen(substitute(a:str, '.', 'x', 'g'))
endfunction
endif "}}}
"Returns: 2 if there is a marker and text "Returns: 2 if there is a marker and text
" 1 for a marker and no text " 1 for a marker and no text
" 0 for no marker at all (empty line or only text) " 0 for no marker at all (empty line or only text)
@ -709,7 +740,7 @@ function! s:line_has_marker(lnum) "{{{
endfunction "}}} endfunction "}}}
"Renumbers the list the cursor is in "Renumbers the list the cursor is in
"also update its parents state "also update its parents checkbox state
function! vimwiki#lst#adjust_numbered_list() "{{{ function! vimwiki#lst#adjust_numbered_list() "{{{
let cur_item = s:get_corresponding_item(line('.')) let cur_item = s:get_corresponding_item(line('.'))
if cur_item.type == 0 | return | endif if cur_item.type == 0 | return | endif
@ -733,7 +764,8 @@ function! vimwiki#lst#adjust_whole_buffer() "{{{
endwhile endwhile
endfunction "}}} endfunction "}}}
"Toggle checkbox between [ ] and [X] or creates one "Toggles checkbox between [ ] and [X] or creates one
"in the lines of the given range
function! vimwiki#lst#toggle_cb(line1, line2) "{{{ function! vimwiki#lst#toggle_cb(line1, line2) "{{{
let from_item = s:get_corresponding_item(a:line1) let from_item = s:get_corresponding_item(a:line1)
let to_item = s:get_corresponding_item(a:line2) let to_item = s:get_corresponding_item(a:line2)
@ -784,6 +816,7 @@ function! vimwiki#lst#toggle_cb(line1, line2) "{{{
endfunction "}}} endfunction "}}}
"Returns: the position of a marker in g:vimwiki_list_markers
function! s:get_idx_list_markers(item) "{{{ function! s:get_idx_list_markers(item) "{{{
if a:item.type == 1 if a:item.type == 1
let m = s:first_char(a:item.mrkr) let m = s:first_char(a:item.mrkr)
@ -793,6 +826,7 @@ function! s:get_idx_list_markers(item) "{{{
return index(g:vimwiki_list_markers, m) return index(g:vimwiki_list_markers, m)
endfunction "}}} endfunction "}}}
"changes the marker of the given item to the next in g:vimwiki_list_markers
function! s:get_next_mrkr(item) "{{{ function! s:get_next_mrkr(item) "{{{
if a:item.type == 0 if a:item.type == 0
let new_mrkr = g:vimwiki_list_markers[0] let new_mrkr = g:vimwiki_list_markers[0]
@ -803,6 +837,7 @@ function! s:get_next_mrkr(item) "{{{
return new_mrkr return new_mrkr
endfunction "}}} endfunction "}}}
"changes the marker of the given item to the previous in g:vimwiki_list_markers
function! s:get_prev_mrkr(item) "{{{ function! s:get_prev_mrkr(item) "{{{
if a:item.type == 0 if a:item.type == 0
return g:vimwiki_list_markers[-1] return g:vimwiki_list_markers[-1]
@ -811,7 +846,8 @@ function! s:get_prev_mrkr(item) "{{{
if idx == -1 if idx == -1
return g:vimwiki_list_markers[-1] return g:vimwiki_list_markers[-1]
else else
return g:vimwiki_list_markers[(idx - 1 + len(g:vimwiki_list_markers)) % len(g:vimwiki_list_markers)] return g:vimwiki_list_markers[(idx - 1 + len(g:vimwiki_list_markers)) %
\ len(g:vimwiki_list_markers)]
endif endif
endfunction "}}} endfunction "}}}
@ -843,22 +879,27 @@ function! vimwiki#lst#change_marker(line1, line2, new_mrkr, mode) "{{{
if index(s:multiple_bullet_chars, s:first_char(new_mrkr)) > -1 if index(s:multiple_bullet_chars, s:first_char(new_mrkr)) > -1
"use *** if the item above has *** too "use *** if the item above has *** too
let item_above = s:get_prev_list_item(cur_item, 1) let item_above = s:get_prev_list_item(cur_item, 1)
if item_above.type == 1 && s:first_char(item_above.mrkr) == s:first_char(new_mrkr) if item_above.type == 1 &&
\ s:first_char(item_above.mrkr) ==s:first_char(new_mrkr)
let new_mrkr = item_above.mrkr let new_mrkr = item_above.mrkr
else else
"use *** if the item below has *** too "use *** if the item below has *** too
let item_below = s:get_next_list_item(cur_item, 1) let item_below = s:get_next_list_item(cur_item, 1)
if item_below.type == 1 && s:first_char(item_below.mrkr) == s:first_char(new_mrkr) if item_below.type == 1 &&
\ s:first_char(item_below.mrkr) == s:first_char(new_mrkr)
let new_mrkr = item_below.mrkr let new_mrkr = item_below.mrkr
else else
"if the old is ### and the new is * use *** "if the old is ### and the new is * use ***
if cur_item.type == 1 && index(s:multiple_bullet_chars, s:first_char(cur_item.mrkr)) > -1 if cur_item.type == 1 &&
\ index(s:multiple_bullet_chars,s:first_char(cur_item.mrkr))>-1
let new_mrkr = repeat(new_mrkr, s:string_length(cur_item.mrkr)) let new_mrkr = repeat(new_mrkr, s:string_length(cur_item.mrkr))
else else
"use *** if the parent item has ** "use *** if the parent item has **
let parent_item = s:get_parent(cur_item) let parent_item = s:get_parent(cur_item)
if parent_item.type == 1 && s:first_char(parent_item.mrkr) == s:first_char(new_mrkr) if parent_item.type == 1 &&
let new_mrkr = repeat(s:first_char(parent_item.mrkr), s:string_length(parent_item.mrkr)+1) \ s:first_char(parent_item.mrkr) == s:first_char(new_mrkr)
let new_mrkr = repeat(s:first_char(parent_item.mrkr),
\ s:string_length(parent_item.mrkr)+1)
endif endif
endif endif
endif endif
@ -876,15 +917,6 @@ function! vimwiki#lst#change_marker(line1, line2, new_mrkr, mode) "{{{
call cursor('.', col('$') - cur_col_from_eol) call cursor('.', col('$') - cur_col_from_eol)
endfunction "}}} endfunction "}}}
function! s:remove_cb(item) "{{{
let item = a:item
if item.type != 0 && item.cb != ''
let item.cb = ''
call s:substitute_rx_in_line(item.lnum, '\s\+\[.\]', '')
endif
return item
endfunction "}}}
function! vimwiki#lst#remove_cb(first_line, last_line) "{{{ function! vimwiki#lst#remove_cb(first_line, last_line) "{{{
let first_item = s:get_corresponding_item(a:first_line) let first_item = s:get_corresponding_item(a:first_line)
let last_item = s:get_corresponding_item(a:last_line) let last_item = s:get_corresponding_item(a:last_line)
@ -913,7 +945,8 @@ function! vimwiki#lst#remove_cb(first_line, last_line) "{{{
endfunction "}}} endfunction "}}}
function! vimwiki#lst#remove_cb_in_list() "{{{ function! vimwiki#lst#remove_cb_in_list() "{{{
let first_item = s:get_first_item_in_list(s:get_corresponding_item(line('.')), 0) let first_item = s:get_first_item_in_list(
\ s:get_corresponding_item(line('.')), 0)
let cur_item = first_item let cur_item = first_item
while 1 while 1
@ -949,7 +982,8 @@ function! s:set_indent(lnum, new_indent) "{{{
if &expandtab if &expandtab
let indentstring = repeat(' ', a:new_indent) let indentstring = repeat(' ', a:new_indent)
else else
let indentstring = repeat('\t', a:new_indent / &tabstop) . repeat(' ', a:new_indent % &tabstop) let indentstring = repeat('\t', a:new_indent / &tabstop) .
\ repeat(' ', a:new_indent % &tabstop)
endif endif
call s:substitute_rx_in_line(a:lnum, '^\s*', indentstring) call s:substitute_rx_in_line(a:lnum, '^\s*', indentstring)
endfunction "}}} endfunction "}}}
@ -959,7 +993,8 @@ function! s:decrease_level(item) "{{{
if VimwikiGet('syntax') == 'media' && a:item.type == 1 && if VimwikiGet('syntax') == 'media' && a:item.type == 1 &&
\ index(s:multiple_bullet_chars, s:first_char(a:item.mrkr)) > -1 \ index(s:multiple_bullet_chars, s:first_char(a:item.mrkr)) > -1
if s:string_length(a:item.mrkr) >= 2 if s:string_length(a:item.mrkr) >= 2
call s:substitute_string_in_line(a:item.lnum, s:first_char(a:item.mrkr), '') call s:substitute_string_in_line(a:item.lnum,
\ s:first_char(a:item.mrkr), '')
let removed_indent = -1 let removed_indent = -1
endif endif
else else
@ -979,7 +1014,8 @@ function! s:increase_level(item) "{{{
let additional_space = 0 let additional_space = 0
if VimwikiGet('syntax') == 'media' && a:item.type == 1 && if VimwikiGet('syntax') == 'media' && a:item.type == 1 &&
\ index(s:multiple_bullet_chars, s:first_char(a:item.mrkr)) > -1 \ index(s:multiple_bullet_chars, s:first_char(a:item.mrkr)) > -1
call s:substitute_string_in_line(a:item.lnum, a:item.mrkr, a:item.mrkr . s:first_char(a:item.mrkr)) call s:substitute_string_in_line(a:item.lnum, a:item.mrkr, a:item.mrkr .
\ s:first_char(a:item.mrkr))
let additional_indent = 1 let additional_indent = 1
else else
let old_indent = indent(a:item.lnum) let old_indent = indent(a:item.lnum)
@ -1001,7 +1037,8 @@ function! s:indent_line_by(lnum, indent_by) "{{{
if VimwikiGet('syntax') == 'media' && item.type == 1 && if VimwikiGet('syntax') == 'media' && item.type == 1 &&
\ index(s:multiple_bullet_chars, s:first_char(item.mrkr)) > -1 \ index(s:multiple_bullet_chars, s:first_char(item.mrkr)) > -1
if a:indent_by > 0 if a:indent_by > 0
call s:substitute_string_in_line(a:lnum, item.mrkr, item.mrkr . s:first_char(item.mrkr)) call s:substitute_string_in_line(a:lnum, item.mrkr,
\ item.mrkr . s:first_char(item.mrkr))
elseif a:indent_by < 0 elseif a:indent_by < 0
call s:substitute_string_in_line(a:lnum, s:first_char(item.mrkr), '') call s:substitute_string_in_line(a:lnum, s:first_char(item.mrkr), '')
endif endif
@ -1040,8 +1077,10 @@ function! s:adjust_mrkr(item) "{{{
if neighbor_item.type == 0 && a:item.type == 1 && if neighbor_item.type == 0 && a:item.type == 1 &&
\ index(s:multiple_bullet_chars, s:first_char(a:item.mrkr)) > -1 \ index(s:multiple_bullet_chars, s:first_char(a:item.mrkr)) > -1
let parent_item = s:get_parent(a:item) let parent_item = s:get_parent(a:item)
if parent_item.type == 1 && s:first_char(parent_item.mrkr) == s:first_char(a:item.mrkr) if parent_item.type == 1 &&
let new_mrkr = repeat(s:first_char(parent_item.mrkr), s:string_length(parent_item.mrkr)+1) \ s:first_char(parent_item.mrkr) == s:first_char(a:item.mrkr)
let new_mrkr = repeat(s:first_char(parent_item.mrkr),
\ s:string_length(parent_item.mrkr)+1)
endif endif
endif endif
@ -1053,7 +1092,8 @@ endfunction "}}}
function! s:change_level(from_line, to_line, direction, plus_children) "{{{ function! s:change_level(from_line, to_line, direction, plus_children) "{{{
let from_item = s:get_corresponding_item(a:from_line) let from_item = s:get_corresponding_item(a:from_line)
if from_item.type == 0 if from_item.type == 0
if a:direction == 'increase' && a:from_line == a:to_line && empty(getline(a:from_line)) if a:direction == 'increase' && a:from_line == a:to_line &&
\ empty(getline(a:from_line))
"that's because :> doesn't work on an empty line "that's because :> doesn't work on an empty line
normal! gi normal! gi
else else
@ -1080,11 +1120,15 @@ function! s:change_level(from_line, to_line, direction, plus_children) "{{{
let first_line_level = s:get_level(from_item.lnum) let first_line_level = s:get_level(from_item.lnum)
let more_than_one_level_concerned = 0 let more_than_one_level_concerned = 0
let first_line_indented_by = (a:direction == 'increase') ? s:increase_level(from_item) : s:decrease_level(from_item) let first_line_indented_by =
\ (a:direction == 'increase') ?
\ s:increase_level(from_item) : s:decrease_level(from_item)
let cur_ln = s:get_next_line(from_item.lnum) let cur_ln = s:get_next_line(from_item.lnum)
while cur_ln > 0 && cur_ln <= to_line while cur_ln > 0 && cur_ln <= to_line
if !more_than_one_level_concerned && s:get_level(cur_ln) != first_line_level && s:get_item(cur_ln).type != 0 if !more_than_one_level_concerned &&
\ s:get_level(cur_ln) != first_line_level &&
\ s:get_item(cur_ln).type != 0
let more_than_one_level_concerned = 1 let more_than_one_level_concerned = 1
endif endif
call s:indent_line_by(cur_ln, first_line_indented_by) call s:indent_line_by(cur_ln, first_line_indented_by)
@ -1342,7 +1386,8 @@ function! vimwiki#lst#toggle_list_item() "{{{
let prev_item = s:get_corresponding_item(s:get_prev_line(cur_item.lnum)) let prev_item = s:get_corresponding_item(s:get_prev_line(cur_item.lnum))
endif endif
let cur_item = s:remove_mrkr(cur_item) let cur_item = s:remove_mrkr(cur_item)
let adjust_prev_item = (prev_item.type == 2 && s:get_level(cur_item.lnum) <= s:get_level(prev_item.lnum)) ? 1 : 0 let adjust_prev_item = (prev_item.type == 2 &&
\ s:get_level(cur_item.lnum) <= s:get_level(prev_item.lnum)) ? 1 : 0
call s:indent_multiline(prev_item, cur_item.lnum) call s:indent_multiline(prev_item, cur_item.lnum)
if adjust_prev_item if adjust_prev_item
call s:adjust_numbered_list_below(prev_item, 0) call s:adjust_numbered_list_below(prev_item, 0)
@ -1389,17 +1434,20 @@ function! vimwiki#lst#setup_marker_infos() "{{{
let s:number_divisors .= vimwiki#u#escape(i[1]) let s:number_divisors .= vimwiki#u#escape(i[1])
endfor endfor
let s:char_to_rx = {'1': '\d\+', 'i': '[ivxlcdm]\+', 'I': '[IVXLCDM]\+', 'a': '\l\{1,2}', 'A': '\u\{1,2}'} let s:char_to_rx = {'1': '\d\+', 'i': '[ivxlcdm]\+', 'I': '[IVXLCDM]\+',
\ 'a': '\l\{1,2}', 'A': '\u\{1,2}'}
"create regexp for bulleted list items "create regexp for bulleted list items
let g:vimwiki_rxListBullet = join( map(keys(g:vimwiki_bullet_types), let g:vimwiki_rxListBullet = join( map(keys(g:vimwiki_bullet_types),
\ 'vimwiki#u#escape(v:val) . repeat("\\+", g:vimwiki_bullet_types[v:val])') , '\|') \'vimwiki#u#escape(v:val).repeat("\\+", g:vimwiki_bullet_types[v:val])'
\ ) , '\|')
"create regex for numbered list items "create regex for numbered list items
if !empty(g:vimwiki_number_types) if !empty(g:vimwiki_number_types)
let g:vimwiki_rxListNumber = '\C\%(' let g:vimwiki_rxListNumber = '\C\%('
for type in g:vimwiki_number_types[:-2] for type in g:vimwiki_number_types[:-2]
let g:vimwiki_rxListNumber .= s:char_to_rx[type[0]] . vimwiki#u#escape(type[1]) . '\|' let g:vimwiki_rxListNumber .= s:char_to_rx[type[0]] .
\ vimwiki#u#escape(type[1]) . '\|'
endfor endfor
let g:vimwiki_rxListNumber .= s:char_to_rx[g:vimwiki_number_types[-1][0]]. let g:vimwiki_rxListNumber .= s:char_to_rx[g:vimwiki_number_types[-1][0]].
\ vimwiki#u#escape(g:vimwiki_number_types[-1][1]) . '\)' \ vimwiki#u#escape(g:vimwiki_number_types[-1][1]) . '\)'
@ -1440,6 +1488,9 @@ fun! vimwiki#lst#fold_level(lnum) "{{{
return 's1' return 's1'
endif endif
endif endif
return '='
endfu
return '=' return '='
endf "}}} endf "}}}