Wednesday, October 8, 2014

Re: readfile() and writefile() in practice

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

On October 8, 2014 8:45:03 PM GMT+03:00, Christian Brabandt <cblists@256bit.org> wrote:
>Hi Ben!
>
>On Mi, 08 Okt 2014, Ben Klein wrote:
>
>> Thanks all!
>>
>> Sorry -- I normally would include an example but I sent that email
>from my
>> phone and have unreliable access to my computer (where I've been
>playing
>> with this). I am on a Mac, for what it's worth (not yet using Vim on
>the
>> iPhone :D ).
>>
>> What I've tried to do is to combine a file with the contents of a
>buffer
>> and writefile() that. So here is the file test.txt:
>>
>> ---
>> Tis is
>> text this is
>> ---
>>
>> And this code:
>>
>> ---
>> let dir = "test"
>>
>> function! Append()
>> let contents = readfile(g:dir . "test.txt", "b")
>
>contents is a list here.
>
>> call add(contents, join(getline(1, '$'), "\n"))
>
>To that list you are adding the current buffer content as a single
>string.
>
>> call writefile(contents, g:dir . "does-this-work.txt", "b")
>
>writefile writes each listitem and because binary mode is set, it will
>replace "\n" by an ASCII NUL.

It is standard Vim NUL "escaping" used in various places (including the output of getline() and internal representation of buffer contents). I have never heard this has absolutely anything to do with binary mode or that using non-binary mode changes how Vim handles it.

>
>> endfunction
>> ---
>>
>> when Append() is called on this buffer content:
>>
>> ---
>> This is one line of text.
>> This is another line.
>> ---
>>
>> gives me the file does-this-work.txt:
>>
>> ---
>> Tis is
>> text this is
>>
>> This is one line of text.^@This is another line.
>> ---
>>
>> In other words it looks like readfile() and writefile() are indeed
>behaving
>> correctly (so that was a mistake on my part -- sorry). Where I'm
>having
>> trouble is with the getline() that gets the buffer contents. If I
>join()
>> it, I don't know how to represent the newlines (obviously '\n'
>doesn't
>> work). If I don't join() it, I get E730 ('using List as a String').
>
>See my comments above.
>
>Best,
>Christian

-----BEGIN PGP SIGNATURE-----
Version: APG v1.1.1

iQI1BAEBCgAfBQJUNZ4JGBxaeVggPHp5eC52aW1AZ21haWwuY29tPgAKCRCf3UKj
HhHSvgIKD/9Yornekh5QwQ05GwbcKknZKsbvzgOKP3DfasWE3MHCJsRs9TyMpmn1
inc5LuLTwNhZoWTkaj8wA7VMxXco/6huNW0+V4ezMpOl2TBWsm5kdy1EAeXhTbOq
3RQcWo0+Ev22hL8LkpkodUi8SQWNJneTw8dN+4XjUsPfxSpPTouy6u15NyqwJVeQ
HNc40t8yxFKfnCT13tABzI6VF9D8CP9asGldamiQEJkufN5xY/LVSB5YtwAB+s/c
ZPZwz5R+IfcTgkCOb9Zvu4BfC3AYZrKh6zpofpA6J9bLrKCK6dhD5Qq3lyr4/LYb
0K9efVT1MEvS+j5l4DSICLZFWfyc1Gg84TRIoRTGhWXUx2EgbWkPze9Qz9x0Mvoe
HnICdOWGmwWQj3JfWTRedEM9SbqWTK4scEJgDpmX8zDmxT6UWH8A3JcqTyjOw2dQ
deZBXWE8jiP4iYC74OPZcXLzkaQlxEDo16dd+ANR04akX0bOlqr1Br29mlMlS2H5
GnzNvZgXnf+GVqSPihGXPorZ3YtJsshcADEUmHIAUgogQKZShGJdE69JaVPrXY/K
uyO5RVU+C334gdKhPVkE+1+iWn6JT34tryXttwyNsUYgfdmprCZfsPeu/a9aTWWj
ObmTYGITo9NE7astuVKfhnxsiDCHrlYPnwVzoFmmkSvMnXDwiVYlTw==
=UyoQ
-----END PGP SIGNATURE-----

--
--
You received this message from the "vim_use" 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

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments: