My dotfiles. Period.

encoding.vim 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. "=============================================================================
  2. " $Id: encoding.vim 520 2012-03-19 18:09:15Z luc.hermitte $
  3. " File: autoload/lh/encoding.vim {{{1
  4. " Author: Luc Hermitte <EMAIL:hermitte {at} free {dot} fr>
  5. " <URL:http://code.google.com/p/lh-vim/>
  6. " License: GPLv3 with exceptions
  7. " <URL:http://code.google.com/p/lh-vim/wiki/License>
  8. " Version: 3.0.0
  9. " Created: 21st Feb 2008
  10. " Last Update: $Date: 2012-03-19 19:09:15 +0100 (Mon, 19 Mar 2012) $
  11. "------------------------------------------------------------------------
  12. " Description:
  13. " Defines functions that help managing various encodings
  14. "
  15. "------------------------------------------------------------------------
  16. " Installation:
  17. " Drop it into {rtp}/autoload/lh/
  18. " Vim 7+ required.
  19. " History:
  20. " v3.0.0:
  21. " (*) GPLv3
  22. " v2.2.2:
  23. " (*) new mb_strings functions: strlen, strpart, at
  24. " v2.0.7:
  25. " (*) lh#encoding#Iconv() copied from map-tools
  26. " TODO: «missing features»
  27. " }}}1
  28. "=============================================================================
  29. let s:cpo_save=&cpo
  30. set cpo&vim
  31. "------------------------------------------------------------------------
  32. " Exported functions {{{2
  33. " Function: lh#encoding#iconv(expr, from, to) " {{{3
  34. " Unlike |iconv()|, this wrapper returns {expr} when we know no convertion can
  35. " be acheived.
  36. function! lh#encoding#iconv(expr, from, to)
  37. " call Dfunc("s:ICONV(".a:expr.','.a:from.','.a:to.')')
  38. if has('multi_byte') &&
  39. \ ( has('iconv') || has('iconv/dyn') ||
  40. \ ((a:from=~'latin1\|utf-8') && (a:to=~'latin1\|utf-8')))
  41. " call confirm('encoding: '.&enc."\nto:".a:to, "&Ok", 1)
  42. " call Dret("s:ICONV convert=".iconv(a:expr, a:from, a:to))
  43. return iconv(a:expr,a:from,a:to)
  44. else
  45. " Cannot convert
  46. " call Dret("s:ICONV no convert=".a:expr)
  47. return a:expr
  48. endif
  49. endfunction
  50. " Function: lh#encoding#at(mb_string, i) " {{{3
  51. " @return i-th character in a mb_string
  52. " @parem mb_string multi-bytes string
  53. " @param i 0-indexed position
  54. function! lh#encoding#at(mb_string, i)
  55. return matchstr(a:mb_string, '.', 0, a:i+1)
  56. endfunction
  57. " Function: lh#encoding#strpart(mb_string, pos, length) " {{{3
  58. " @return {length} extracted characters from {position} in multi-bytes string.
  59. " @parem mb_string multi-bytes string
  60. " @param p 0-indexed position
  61. " @param l length of the string to extract
  62. function! lh#encoding#strpart(mb_string, p, l)
  63. return matchstr(a:mb_string, '.\{'.a:l.'}', 0, a:p+1)
  64. endfunction
  65. " Function: lh#encoding#strlen(mb_string) " {{{3
  66. " @return the length of the multi-bytes string.
  67. function! lh#encoding#strlen(mb_string)
  68. return strlen(substitute(a:mb_string, '.', 'a', 'g'))
  69. endfunction
  70. "------------------------------------------------------------------------
  71. let &cpo=s:cpo_save
  72. "=============================================================================
  73. " vim600: set fdm=marker: