On 20-Dec-2011 01:07, mattn wrote:
> On Tuesday, December 20, 2011 5:53:00 AM UTC+9, Дмитрий Франк wrote:
>
> Your suggestion
>
> len(iconv(getline('.'), &encoding, 'char'))
>
> returns length of current line, but i need to get length of all lines from
> buffer start to current line. line2byte('.') does this.
> To workaround with this, i have to write cycle for each line. Imagine how
> long this cycle on Vimscript will work on large buffers.
>
>
> len(iconv(join(getline(1, line('$')), "\n"), &encoding, "char"))
>
> Or if you want to treat large file.
>
> let s = 0
> for i in range(1, line('$'))
> let s += iconv(getline(i)."\n", &encoding, "char")
> endfor
>
> Note that you may get different result if you want to treat DOS format.
mattn, by now you have to acknowledge that the workaround isn't actually so
simple than you initially thought, is processing-intensive in Vimscript, and has
major pitfalls.
I'd like to direct the attention also to the related "go" / :goto commands. I
have rarely used them, but I imagine that the main use case is that some
external tool is pointing me to a byte offset N in a file, and I want to use Vim
to quickly check that position. For that to work (when &fileencoding differs
from &encoding, i.e. what this entire discussion is about), Vim would have to
use fileencoding-based counts. Currently, I would need to use a tool like xxd to
achieve that goal (assuming I cannot just temporarily switch &encoding, like
when &fileencoding is ucs-2 or ucs-4).
One could "overload" :goto (with [!]) in the same way as I've suggested for
line2byte(), but I really wonder whether using &encoding-based counts is ever
wanted (and that could indeed be easily worked around by temporarily setting
&fenc to &enc).
BTW, I don't see a problem with unpersisted buffers; they have a &fileencoding,
too. And this doesn't look so much like an API change to me, more a design
clarification of a rather obscure edge case which wasn't fully thought through
when the encoding-related stuff was introduced. (Can some of the core devs
comment on this?)
-- regards, ingo
--
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php