aboutsummaryrefslogtreecommitdiffhomepage
path: root/modules/textadept
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 /modules/textadept
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 'modules/textadept')
-rw-r--r--modules/textadept/bookmarks.lua7
-rw-r--r--modules/textadept/history.lua33
2 files changed, 10 insertions, 30 deletions
diff --git a/modules/textadept/bookmarks.lua b/modules/textadept/bookmarks.lua
index 532d1427..5f919e3d 100644
--- a/modules/textadept/bookmarks.lua
+++ b/modules/textadept/bookmarks.lua
@@ -80,10 +80,11 @@ function M.goto_mark(next)
end
local lines = {}
--- Save and restore bookmarks on buffer:reload().
-events.connect(events.FILE_BEFORE_RELOAD,
+-- Save and restore bookmarks when replacing buffer text (e.g. buffer:reload(),
+-- textadept.editing.filter_through()).
+events.connect(events.BUFFER_BEFORE_REPLACE_TEXT,
function() for line in bookmarks(buffer) do lines[#lines + 1] = line end end)
-events.connect(events.FILE_AFTER_RELOAD, function()
+events.connect(events.BUFFER_AFTER_REPLACE_TEXT, function()
for _, line in ipairs(lines) do buffer:marker_add(line, M.MARK_BOOKMARK) end
lines = {} -- clear
end)
diff --git a/modules/textadept/history.lua b/modules/textadept/history.lua
index 8c784c17..f6238a0d 100644
--- a/modules/textadept/history.lua
+++ b/modules/textadept/history.lua
@@ -33,36 +33,15 @@ local view_history = setmetatable({}, {
end
})
-local restore_position, pos, first_visible_line = false, nil, nil
--- Restore position after a full-buffer undo/redo operation, e.g. after replacing buffer contents
--- with a formatting command and then performing an undo.
-events.connect(events.UPDATE_UI, function(updated)
- if not restore_position or updated & buffer.UPDATE_SELECTION == 0 then return end
- restore_position = false
- buffer:goto_pos(pos)
- view.first_visible_line, pos, first_visible_line = first_visible_line, nil, nil
-end)
-
+local INSERT, DELETE = buffer.MOD_INSERTTEXT, buffer.MOD_DELETETEXT
+local UNDO, REDO = buffer.PERFORMED_UNDO, buffer.PERFORMED_REDO
-- Listens for text insertion and deletion events and records their locations.
events.connect(events.MODIFIED, function(position, mod, text, length)
- local buffer = buffer
- -- Only interested in text insertion or deletion.
- if mod & buffer.MOD_INSERTTEXT > 0 then
- if length == buffer.length then
- if mod & buffer.MULTILINEUNDOREDO > 0 then restore_position = true end
- return -- ignore file loading or replacing buffer contents
- end
- position = position + length
- elseif mod & buffer.MOD_DELETETEXT > 0 then
- if buffer.length == 0 then return end -- ignore replacing buffer contents
- elseif mod & (buffer.PERFORMED_UNDO | buffer.PERFORMED_REDO) > 0 and
- (mod & buffer.MOD_BEFOREDELETE > 0) and length == buffer.length then
- -- Save view state for potential undo before it's lost.
- pos, first_visible_line = buffer.current_pos, view.first_visible_line
- else
- return
+ if mod & (INSERT | DELETE) == 0 or buffer.length == (mod & INSERT > 0 and length or 0) then
+ return -- ignore non-insertion/deletion, file loading, and replacing buffer contents
end
- if mod & (buffer.PERFORMED_UNDO | buffer.PERFORMED_REDO) > 0 then return end -- ignore undo/redo
+ if mod & INSERT > 0 then position = position + length end
+ if mod & (UNDO | REDO) > 0 then return end -- ignore undo/redo
M.record(nil, buffer:line_from_position(position), buffer.column[position])
end)