Loading ...
Sorry, an error occurred while loading the content.

Re: Wrong characters count when using utf-8

Expand Messages
  • Bram Moolenaar
    ... Hmm, there are several methods, but I suppose the good old :s method ... This ignores composing characters. You can also use virtcol(), although it gets
    Message 1 of 6 , May 28, 2003
    • 0 Attachment
      Valery Kondakoff wrote:

      > Tuesday, May 27, 2003, you wrote to me:
      >
      > >> > As far as I understand the character count is _not_ changed when
      > >> > converting, but wrong character count is displayed.
      >
      > BM> The byte count changes. Using "chars" in the file message is an old Vi
      > BM> habit. Look at this as the C language "char", which is actually a byte.
      >
      > Aha. Now I understand this. Thank you for explanation. But I still
      > don't understand how can I count _characters_, not bytes when using
      > 'utf-8'?

      Hmm, there are several methods, but I suppose the good old ":s" method
      works:

      :s/./&/g

      This ignores composing characters.

      You can also use virtcol(), although it gets confused by Tabs and wide
      characters.

      If you want a function you could use something like:

      strlen(substitute(getline(".")), ".", "x", "g")

      Not tested!

      --
      Often you're less important than your furniture. If you think about it, you
      can get fired but your furniture stays behind, gainfully employed at the
      company that didn't need _you_ anymore.
      (Scott Adams - The Dilbert principle)

      /// Bram Moolenaar -- Bram@... -- http://www.Moolenaar.net \\\
      /// Creator of Vim - Vi IMproved -- http://www.Vim.org \\\
      \\\ Project leader for A-A-P -- http://www.A-A-P.org ///
      \\\ Help AIDS victims, buy at Amazon -- http://ICCF.nl/click1.html ///
    • Valery Kondakoff
      Hello, Bram! Thursday, May 29, 2003, you wrote to me: BM If you want a function you could use something like: BM strlen(substitute(getline( . )),
      Message 2 of 6 , May 29, 2003
      • 0 Attachment
        Hello, Bram!

        Thursday, May 29, 2003, you wrote to me:


        BM> If you want a function you could use something like:
        BM> strlen(substitute(getline(".")), ".", "x", "g")
        BM> Not tested!

        Thank you. This example does exactly what I wanted:

        strlen(substitute(getline("."), ".", "x", "g"))

        Here is a modified function, which counts selected characters in
        Visual mode:

        " If the argument is 0, newlines are not counted in blockwise visual mode
        fun! VisualCount(newlines)
        let mode = mode()
        let ret = ""
        if mode ==? 'v'
        let opt_report = &report
        let &report = 2147483647 " 2^31 - 1
        norm "-ygv
        let &report = opt_report
        let len = strlen(substitute(@-, ".", "x", "g"))
        if mode ==# 'V' && a:newlines == 0
        let len = len - (line("'>") - line("'<")) - 1
        elseif mode ==# 'v' && a:newlines == 0
        let len = len - (line("'>") - line("'<"))
        endif
        else
        let len = 0
        endif
        if len > 0
        let ret = '[' . len . ']'
        endif
        return ret
        endfun


        --
        Best regards,
        Valery Kondakoff
        http://www.nbk.orc.ru (Ne Bey Kopytom)
        http://www.nbk.orc.ru/mtb (MTB riding in Moscow)

        PGP key: mailto:pgp-public-keys@...?subject=GET%20strauss@...

        np: Anthony Rother - Red Light District (Unknown) [stopped]
      Your message has been successfully submitted and would be delivered to recipients shortly.