diff options
author | 2013-11-11 09:46:34 -0500 | |
---|---|---|
committer | 2013-11-11 09:46:34 -0500 | |
commit | 2670ba1b74225fdf1d9447ca773af8dab8dcb6e8 (patch) | |
tree | 92471d5a91ffa2f174b29ad1fdf65c0bc71d125a | |
parent | e864cd85138bd7e7058f2d3ddf7bac84ab03f3f8 (diff) |
Added `events.FILE_CHANGED`; core/file_io.lua
Add an event handler at index 1 to override the default prompt to reload.
-rw-r--r-- | core/file_io.lua | 34 |
1 files changed, 23 insertions, 11 deletions
diff --git a/core/file_io.lua b/core/file_io.lua index 3353f962..e2c2e1ed 100644 --- a/core/file_io.lua +++ b/core/file_io.lua @@ -27,6 +27,11 @@ -- Arguments: -- -- * _`filename`_: The new filename. +-- @field _G.events.FILE_CHANGED (string) +-- Emitted when Textadept detects that a file was externally modified. +-- Arguments: +-- +-- * _`filename`_: The filename externally modified. -- @field SNAPOPEN_MAX (number) -- The maximum number of files to list in the snapopen dialog. -- The default value is `1000`. @@ -38,6 +43,7 @@ events.FILE_OPENED = 'file_opened' events.FILE_BEFORE_SAVE = 'file_before_save' events.FILE_AFTER_SAVE = 'file_after_save' events.FILE_SAVED_AS = 'file_saved_as' +events.FILE_CHANGED = 'file_changed' io.SNAPOPEN_MAX = 1000 @@ -294,28 +300,34 @@ function io.close_all_buffers() return io.close_buffer() -- the last one end --- Prompts the user to reload the current file if it has been modified outside --- of Textadept. +-- Detects if the current file has been externally modified and, if so, emits a +-- `FILE_CHANGED` event. 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 - local msg = string.format('"%s"\n%s', - buffer.filename:iconv('UTF-8', _CHARSET), - _L['has been modified. Reload it?']) - local button = ui.dialogs.msgbox{ - title = _L['Reload?'], text = _L['Reload modified file?'], - informative_text = msg, icon = 'gtk-dialog-question', - button1 = _L['_Yes'], button2 = _L['_No'] - } - if button == 1 then io.reload_file() end + events.emit(events.FILE_CHANGED) end end events_connect(events.BUFFER_AFTER_SWITCH, update_modified_file) events_connect(events.VIEW_AFTER_SWITCH, update_modified_file) +-- Prompts the user to reload the current file if it has been externally +-- modified. +events_connect(events.FILE_CHANGED, function(filename) + local msg = string.format('"%s"\n%s', + buffer.filename:iconv('UTF-8', _CHARSET), + _L['has been modified. Reload it?']) + local button = ui.dialogs.msgbox{ + title = _L['Reload?'], text = _L['Reload modified file?'], + informative_text = msg, icon = 'gtk-dialog-question', + button1 = _L['_Yes'], button2 = _L['_No'] + } + if button == 1 then io.reload_file() end +end) + -- Closes the initial "Untitled" buffer. events_connect(events.FILE_OPENED, function(filename) local buf = _BUFFERS[1] |