utils.vim 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. " Private Functions {{{1
  2. " Public Functions {{{1
  3. function! tablemode#utils#throw(string) abort "{{{2
  4. let v:errmsg = 'table-mode: ' . a:string
  5. throw v:errmsg
  6. endfunction
  7. function! tablemode#utils#line(row) "{{{2
  8. if type(a:row) == type('')
  9. return line(a:row)
  10. else
  11. return a:row
  12. endif
  13. endfunction
  14. function! tablemode#utils#strip(string) "{{{2
  15. return matchstr(a:string, '^\s*\zs.\{-}\ze\s*$')
  16. endfunction
  17. " function! tablemode#utils#strlen {{{2
  18. " To count multibyte characters accurately
  19. function! tablemode#utils#strlen(text)
  20. return strlen(substitute(a:text, '.', 'x', 'g'))
  21. endfunction
  22. function! tablemode#utils#StrDisplayWidth(string) "{{{2
  23. if exists('*strdisplaywidth')
  24. return strdisplaywidth(a:string)
  25. else
  26. " Implement the tab handling part of strdisplaywidth for vim 7.2 and
  27. " earlier - not much that can be done about handling doublewidth
  28. " characters.
  29. let rv = 0
  30. let i = 0
  31. for char in split(a:string, '\zs')
  32. if char == "\t"
  33. let rv += &ts - i
  34. let i = 0
  35. else
  36. let rv += 1
  37. let i = (i + 1) % &ts
  38. endif
  39. endfor
  40. return rv
  41. endif
  42. endfunction
  43. function! tablemode#utils#get_buffer_or_global_option(table_option) "{{{2
  44. return get(b:, a:table_option, get(g:, a:table_option))
  45. endf
  46. function tablemode#utils#MoveToLine(line) "{{{2
  47. let offset = tablemode#utils#line(a:line) - line('.')
  48. if offset > 0
  49. execute "normal! ".offset."j"
  50. elseif offset < 0
  51. execute "normal! ".(-offset)."k"
  52. endif
  53. endfunction
  54. function! tablemode#utils#SeparatorCount(str)
  55. return tablemode#utils#strlen(substitute(a:str, '\V\C\(\\' . escape(g:table_mode_separator, '\') . '\|\[^' . escape(g:table_mode_separator, ']^-\') . ']\)', '', 'g'))
  56. endfunction