aboutsummaryrefslogtreecommitdiffhomepage
path: root/core
diff options
context:
space:
mode:
authorGravatar mitchell <70453897+orbitalquark@users.noreply.github.com>2021-04-30 18:40:06 -0400
committerGravatar mitchell <70453897+orbitalquark@users.noreply.github.com>2021-04-30 18:40:06 -0400
commit715901363a02634f5336c0d3f18cbd9a9c080b4a (patch)
tree315146ace595595cefe4e6b4eae01c475a21a85b /core
parenta775e9fb4188f2638111c15623ea9bd5c804b3e2 (diff)
Replaced `events.FILE_{BEFORE,AFTER}_RELOAD` with `events.BUFFER_{BEFORE,AFTER}_REPLACE_TEXT`.
This allows more features to save/restore state when buffer contents are replaced (e.g. file reload, filter through, etc.)
Diffstat (limited to 'core')
-rw-r--r--core/.buffer.luadoc1
-rw-r--r--core/events.lua34
-rw-r--r--core/file_io.lua13
-rw-r--r--core/ui.lua4
4 files changed, 37 insertions, 15 deletions
diff --git a/core/.buffer.luadoc b/core/.buffer.luadoc
index 33b02bd5..2463a4bc 100644
--- a/core/.buffer.luadoc
+++ b/core/.buffer.luadoc
@@ -1671,7 +1671,6 @@ function style_of_name(buffer, style_name) end
---
-- Reloads the buffer's file contents, discarding any changes.
--- Emits `FILE_BEFORE_RELOAD` and `FILE_AFTER_RELOAD` events if the buffer is the current one.
-- @param buffer A buffer.
-- @name reload
function reload(buffer) end
diff --git a/core/events.lua b/core/events.lua
index 07829919..d9b263d4 100644
--- a/core/events.lua
+++ b/core/events.lua
@@ -59,10 +59,20 @@ local M = {}
-- Emitted right after switching to another buffer.
-- The buffer being switched to is `buffer`.
-- Emitted by [`view.goto_buffer()`]().
+-- @field BUFFER_BEFORE_REPLACE_TEXT (string)
+-- Emitted before replacing the contents of the current buffer.
+-- Note that it is not guaranteed that [`events.BUFFER_AFTER_REPLACE_TEXT`]() will be emitted
+-- shortly after this event.
+-- The buffer **must not** be modified during this event.
-- @field BUFFER_BEFORE_SWITCH (string)
-- Emitted right before switching to another buffer.
-- The buffer being switched from is `buffer`.
-- Emitted by [`view.goto_buffer()`]().
+-- @field BUFFER_AFTER_REPLACE_TEXT (string)
+-- Emitted after replacing the contents of the current buffer.
+-- Note that it is not guaranteed that [`events.BUFFER_BEFORE_REPLACE_TEXT`]() was emitted
+-- previously.
+-- The buffer **must not** be modified during this event.
-- @field BUFFER_DELETED (string)
-- Emitted after deleting a buffer.
-- Emitted by [`buffer.delete()`]().
@@ -383,8 +393,30 @@ end)
-- Set event constants.
for _, v in pairs(_SCINTILLA.events) do M[v[1]:upper()] = v[1] end
-- LuaFormatter off
-local textadept_events = {'appleevent_odoc','buffer_after_switch','buffer_before_switch','buffer_deleted','buffer_new','csi','command_text_changed','error','find','find_text_changed','focus','initialized','keypress','menu_clicked','mouse','quit','replace','replace_all','reset_after','reset_before','resume','suspend', 'tab_clicked','unfocus','view_after_switch','view_before_switch','view_new'}
+local textadept_events = {'appleevent_odoc','buffer_after_replace_text','buffer_after_switch','buffer_before_replace_text','buffer_before_switch','buffer_deleted','buffer_new','csi','command_text_changed','error','find','find_text_changed','focus','initialized','keypress','menu_clicked','mouse','quit','replace','replace_all','reset_after','reset_before','resume','suspend', 'tab_clicked','unfocus','view_after_switch','view_before_switch','view_new'}
-- LuaFormatter on
for _, v in pairs(textadept_events) do M[v:upper()] = v end
+-- Implement `events.BUFFER_{BEFORE,AFTER}_REPLACE_TEXT` as a convenience in lieu of the
+-- undocumented `events.MODIFIED`.
+local DELETE, INSERT, UNDOREDO = _SCINTILLA.constants.MOD_BEFOREDELETE,
+ _SCINTILLA.constants.MOD_INSERTTEXT, _SCINTILLA.constants.MULTILINEUNDOREDO
+-- Helper function for emitting `events.BUFFER_AFTER_REPLACE_TEXT` after a full-buffer undo/redo
+-- operation, e.g. after reloading buffer contents and then performing an undo.
+local function emit_after_replace_text()
+ events.disconnect(events.UPDATE_UI, emit_after_replace_text)
+ events.emit(events.BUFFER_AFTER_REPLACE_TEXT)
+end
+-- Emits events prior to and after replacing buffer text.
+M.connect(M.MODIFIED, function(position, mod, text, length)
+ if mod & (DELETE | INSERT) == 0 or length ~= buffer.length then return end
+ if mod & (INSERT | UNDOREDO) > 0 then
+ -- Cannot emit BUFFER_AFTER_REPLACE_TEXT here because Scintilla will do things like update
+ -- the selection afterwards, which could undo what event handlers do.
+ events.connect(events.UPDATE_UI, emit_after_replace_text)
+ return
+ end
+ M.emit(mod & DELETE > 0 and M.BUFFER_BEFORE_REPLACE_TEXT or M.BUFFER_AFTER_REPLACE_TEXT)
+end)
+
return M
diff --git a/core/file_io.lua b/core/file_io.lua
index 9dde8550..7f0c9999 100644
--- a/core/file_io.lua
+++ b/core/file_io.lua
@@ -9,12 +9,6 @@
-- Arguments:
--
-- * _`filename`_: The opened file's filename.
--- @field _G.events.FILE_BEFORE_RELOAD (string)
--- Emitted before reloading the current file.
--- Emitted by [`buffer:reload()`]().
--- @field _G.events.FILE_AFTER_RELOAD (string)
--- Emitted after reloading the current file.
--- Emitted by [`buffer:reload()`]().
-- @field _G.events.FILE_BEFORE_SAVE (string)
-- Emitted right before saving a file to disk.
-- Emitted by [`buffer:save()`]().
@@ -42,7 +36,7 @@ module('io')]]
-- Events.
-- LuaFormatter off
-local file_io_events = {'file_opened','file_before_reload','file_after_reload','file_before_save','file_after_save','file_changed'}
+local file_io_events = {'file_opened','file_before_save','file_after_save','file_changed'}
-- LuaFormatter on
for _, v in ipairs(file_io_events) do events[v:upper()] = v end
@@ -157,16 +151,13 @@ end
local function reload(buffer)
if not buffer then buffer = _G.buffer end
if not buffer.filename then return end
- if buffer == _G.buffer then events.emit(events.FILE_BEFORE_RELOAD) end
local f = assert(io.open(buffer.filename, 'rb'))
local text = f:read('a')
f:close()
if buffer.encoding then text = text:iconv('UTF-8', buffer.encoding) end
- buffer:clear_all()
- buffer:append_text(text)
+ buffer:set_text(text)
buffer:set_save_point()
buffer.mod_time = lfs.attributes(buffer.filename, 'modification')
- if buffer == _G.buffer then events.emit(events.FILE_AFTER_RELOAD) end
end
-- LuaDoc is in core/.buffer.luadoc.
diff --git a/core/ui.lua b/core/ui.lua
index 2e0635fa..c8266775 100644
--- a/core/ui.lua
+++ b/core/ui.lua
@@ -358,7 +358,7 @@ local function save_buffer_state()
buffer._folds = folds
end
events.connect(events.BUFFER_BEFORE_SWITCH, save_buffer_state)
-events.connect(events.FILE_BEFORE_RELOAD, save_buffer_state)
+events.connect(events.BUFFER_BEFORE_REPLACE_TEXT, save_buffer_state)
-- Restore buffer properties.
local function restore_buffer_state()
@@ -375,7 +375,7 @@ local function restore_buffer_state()
view.x_offset = buffer._x_offset or 0
end
events.connect(events.BUFFER_AFTER_SWITCH, restore_buffer_state)
-events.connect(events.FILE_AFTER_RELOAD, restore_buffer_state)
+events.connect(events.BUFFER_AFTER_REPLACE_TEXT, restore_buffer_state)
-- Updates titlebar and statusbar.
local function update_bars()