Re: [code] [textadept] Checking for changed files on disk

From: Robert Gieseke <rob.g.att.web.de>
Date: Wed, 06 Nov 2013 11:15:36 +0100

Am 11/6/13, 10:48 AM, schrieb Chris Emerson:
> On Tue, Nov 05, 2013 at 11:07:52PM -0500, Mitchell wrote:

>> On Tue, 5 Nov 2013, Chris Emerson wrote:
>>> I could duplicate core/file_io.lua:update_modified_file(), or do some
>>> combination of switching buffers/views and manually emiting
>>> BUFFER/VIEW_AFTER_SWITCH events, but it would be nice to have a standard API
>>> for it.
>>
>> Since this is a pretty special case (I cannot think of anything
>> similar on other platforms), I don't feel like exposing a low-level
>> `io` function is justified; I would copy the functionality for your
>> own use. Jumping through hoops to manually emit the proper events
>> seems too hackish.
>
> I agree that the suspend/resume is unusual, but what about other things that
> may deliberately cause files on disk to change - eg VCS integration (after
> an update/pull, you'd want to check for changes), or if you've got a buffer
> with the output of a "compile" (for example a log file)?
>

Couldn't an event 'FILE_CHANGED' be emitted after switching to a buffer
and the modification checks in update_modified_file?
Then we could hook into this or trigger it manually. Currently I have
hacks like

-- Always reload '.log' or '.blg' files.
events.connect(events.BUFFER_AFTER_SWITCH, function()
   if not buffer.filename then return end
   if buffer.filename:match('%.log$') or buffer.filename:match('%.blg$')
then
     io.reload_file()
   end
end, 1)

to always reload LaTeX/BibTeX logfiles.

I'd like to be able to use this and for example check if the file
contents actually changed after switching branches and if not, silently
reload the file.

Basically, make update_modified_file look something like

local function update_modified_file()
   if not buffer.filename then return end
   local mod_time = lfs.attributes(buffer.filename, 'modification')
   if not mod_time or not buffer.mod_time then return end
   if buffer.mod_time < mod_time then
     buffer.mod_time = mod_time
     events.emit(events.FILE_CHANGED)
   end
end

Best regards,
Robert

-- 
You are subscribed to code.att.foicica.com.
To change subscription settings, send an e-mail to code+help.att.foicica.com.
To unsubscribe, send an e-mail to code+unsubscribe.att.foicica.com.
Received on Wed 06 Nov 2013 - 05:15:36 EST

This archive was generated by hypermail 2.2.0 : Wed 06 Nov 2013 - 06:32:24 EST