tags:

views:

144

answers:

3

I'm writing a simple little Lua commandline app that will build a static website. I'm storing my fragments in a sqlite database. Retrieving the data from the db is straightforward as is saving it; my question comes from editing the data.

Is there an elegant way to pipe the data from Lua to vim? Can vim edit a memory buffer and return it? I was planning on launching the editor via os.execute('vim') but only after grabbing a temporary file handle and dumping the database output into that. I would like to have to avoid touching the filesystem that way but that is my contingency plan.

+2  A: 

The only way I know of to achieve what you want is to allocate a temporary file and edit it. I actually wouldn't worry about touching the filesystem: If your data is small, you can hope that the operating system keeps it in memory cache, and will write the bits out to disk only during otherwise idle cycles.

Some systems offer parts of a filesystem that are guaranteed to be kept in RAM, but it's very unportable.

Norman Ramsey
+1 I wouldn't worry about accessing the filesystem unless it becomes a problem, premature optimization and all that jazz.
ponzao
+1  A: 

You can pipe text into vim using stdin (e.g. echo 'Hello, world!' | vim -), but I'm not sure how to feed the edited results to stdout. So, the first part of the solution in lua would be:

local vim = io.popen('vim -', 'w')
vim:write('Hello, world!')

Perhaps you can achieve the results you want using a memory-mapped file?

Judge Maygarden
A: 

io.tmpfile; os.getenv (to grab EDITOR, not everyone likes vi(m) ); and an io.popen are what you need...

daurnimator