From 715901363a02634f5336c0d3f18cbd9a9c080b4a Mon Sep 17 00:00:00 2001 From: mitchell <70453897+orbitalquark@users.noreply.github.com> Date: Fri, 30 Apr 2021 18:40:06 -0400 Subject: 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.) --- modules/lua/ta_api | 6 +++--- modules/lua/ta_tags | 4 ++-- modules/textadept/bookmarks.lua | 7 ++++--- modules/textadept/history.lua | 33 ++++++--------------------------- 4 files changed, 15 insertions(+), 35 deletions(-) (limited to 'modules') diff --git a/modules/lua/ta_api b/modules/lua/ta_api index d1c83c0e..6e400e97 100644 --- a/modules/lua/ta_api +++ b/modules/lua/ta_api @@ -13,7 +13,9 @@ AUTO_C_COMPLETED events.AUTO_C_COMPLETED (string)\nEmitted after inserting an it AUTO_C_SELECTION events.AUTO_C_SELECTION (string)\nEmitted after selecting an item from an autocompletion list, but before inserting that\nitem into the buffer.\nAutomatic insertion can be canceled by calling `buffer:auto_c_cancel()` before returning\nfrom the event handler.\nArguments:\n\n* _`text`_: The selection's text.\n* _`position`_: The autocompleted word's beginning position. AUTO_C_SELECTION_CHANGE events.AUTO_C_SELECTION_CHANGE (string)\nEmitted as items are highlighted in an autocompletion or user list.\nArguments:\n\n* _`id`_: Either the *id* from `buffer.user_list_show()` or `0` for an autocompletion list.\n* _`text`_: The current selection's text.\n* _`position`_: The position the list was displayed at. BSD _G.BSD (bool)\nWhether or not Textadept is running on BSD. +BUFFER_AFTER_REPLACE_TEXT events.BUFFER_AFTER_REPLACE_TEXT (string)\nEmitted after replacing the contents of the current buffer.\nNote that it is not guaranteed that `events.BUFFER_BEFORE_REPLACE_TEXT` was emitted\npreviously.\nThe buffer **must not** be modified during this event. BUFFER_AFTER_SWITCH events.BUFFER_AFTER_SWITCH (string)\nEmitted right after switching to another buffer.\nThe buffer being switched to is `buffer`.\nEmitted by `view.goto_buffer()`. +BUFFER_BEFORE_REPLACE_TEXT events.BUFFER_BEFORE_REPLACE_TEXT (string)\nEmitted before replacing the contents of the current buffer.\nNote that it is not guaranteed that `events.BUFFER_AFTER_REPLACE_TEXT` will be emitted\nshortly after this event.\nThe buffer **must not** be modified during this event. BUFFER_BEFORE_SWITCH events.BUFFER_BEFORE_SWITCH (string)\nEmitted right before switching to another buffer.\nThe buffer being switched from is `buffer`.\nEmitted by `view.goto_buffer()`. BUFFER_DELETED events.BUFFER_DELETED (string)\nEmitted after deleting a buffer.\nEmitted by `buffer.delete()`. BUFFER_NEW events.BUFFER_NEW (string)\nEmitted after creating a new buffer.\nThe new buffer is `buffer`.\nEmitted on startup and by `buffer.new()`. @@ -62,9 +64,7 @@ EOL_CRLF buffer.EOL_CRLF (number, Read-only)\n EOL_LF buffer.EOL_LF (number, Read-only)\n ERROR events.ERROR (string)\nEmitted when an error occurs.\nArguments:\n\n* _`text`_: The error message text. ERROR lexer.ERROR (string)\nThe token name for error tokens. -FILE_AFTER_RELOAD events.FILE_AFTER_RELOAD (string)\nEmitted after reloading the current file.\nEmitted by `buffer:reload()`. FILE_AFTER_SAVE events.FILE_AFTER_SAVE (string)\nEmitted right after saving a file to disk.\nEmitted by `buffer:save()` and `buffer:save_as()`.\nArguments:\n\n* _`filename`_: The filename of the file being saved.\n* _`saved_as`_: Whether or not the file was saved under a different filename. -FILE_BEFORE_RELOAD events.FILE_BEFORE_RELOAD (string)\nEmitted before reloading the current file.\nEmitted by `buffer:reload()`. FILE_BEFORE_SAVE events.FILE_BEFORE_SAVE (string)\nEmitted right before saving a file to disk.\nEmitted by `buffer:save()`.\nArguments:\n\n* _`filename`_: The filename of the file being saved. FILE_CHANGED events.FILE_CHANGED (string)\nEmitted when Textadept detects that an open file was modified externally.\nWhen connecting to this event, connect with an index of 1 in order to override the default\nprompt to reload the file.\nArguments:\n\n* _`filename`_: The filename externally modified. FILE_OPENED events.FILE_OPENED (string)\nEmitted after opening a file in a new buffer.\nEmitted by `io.open_file()`.\nArguments:\n\n* _`filename`_: The opened file's filename. @@ -796,7 +796,7 @@ regex_label_text ui.find.regex_label_text (string, Write-only)\nThe text of the register args.register(short, long, narg, f, description)\nRegisters a command line option with short and long versions *short* and *long*, respectively.\n*narg* is the number of arguments the option accepts, *f* is the function called when the\noption is set, and *description* is the option's description when displaying help.\n@param short The string short version of the option.\n@param long The string long version of the option.\n@param narg The number of expected parameters for the option.\n@param f The Lua function to run when the option is set. It is passed *narg* string arguments.\n@param description The string description of the option for command line help. register_image view.register_image(view, type, xpm_data)\nRegisters XPM image *xpm_data* to type number *type* for use in autocompletion and user lists.\n@param view A view.\n@param type Integer type to register the image with.\n@param xpm_data The XPM data as described in `view.marker_define_pixmap()`. register_rgba_image view.register_rgba_image(view, type, pixels)\nRegisters RGBA image *pixels* to type number *type* for use in autocompletion and user lists.\nThe dimensions for *pixels* (`view.rgba_image_width` and `view.rgba_image_height`) must\nhave already been defined. *pixels* is a sequence of 4 byte pixel values (red, blue, green,\nand alpha) defining the image line by line starting at the top-left pixel.\n@param view A view.\n@param type Integer type to register the image with.\n@param pixels The RGBA data as described in `view.marker_define_rgba_image()`. -reload buffer.reload(buffer)\nReloads the buffer's file contents, discarding any changes.\nEmits `FILE_BEFORE_RELOAD` and `FILE_AFTER_RELOAD` events if the buffer is the current one.\n@param buffer A buffer. +reload buffer.reload(buffer)\nReloads the buffer's file contents, discarding any changes.\n@param buffer A buffer. replace ui.find.replace()\nMimics pressing the "Replace" button. replace_all ui.find.replace_all()\nMimics pressing the "Replace All" button. replace_all_button_text ui.find.replace_all_button_text (string, Write-only)\nThe text of the "Replace All" button.\nThis is primarily used for localization. diff --git a/modules/lua/ta_tags b/modules/lua/ta_tags index 5471b09f..453bd320 100644 --- a/modules/lua/ta_tags +++ b/modules/lua/ta_tags @@ -13,7 +13,9 @@ AUTO_C_COMPLETED _HOME/core/events.lua /^module('events')]]$/;" F class:events AUTO_C_SELECTION _HOME/core/events.lua /^module('events')]]$/;" F class:events AUTO_C_SELECTION_CHANGE _HOME/core/events.lua /^module('events')]]$/;" F class:events BSD _HOME/core/init.lua /^module('_G')]]$/;" F +BUFFER_AFTER_REPLACE_TEXT _HOME/core/events.lua /^module('events')]]$/;" F class:events BUFFER_AFTER_SWITCH _HOME/core/events.lua /^module('events')]]$/;" F class:events +BUFFER_BEFORE_REPLACE_TEXT _HOME/core/events.lua /^module('events')]]$/;" F class:events BUFFER_BEFORE_SWITCH _HOME/core/events.lua /^module('events')]]$/;" F class:events BUFFER_DELETED _HOME/core/events.lua /^module('events')]]$/;" F class:events BUFFER_NEW _HOME/core/events.lua /^module('events')]]$/;" F class:events @@ -62,9 +64,7 @@ EOL_CRLF _HOME/core/.buffer.luadoc /^module('buffer')$/;" F class:buffer EOL_LF _HOME/core/.buffer.luadoc /^module('buffer')$/;" F class:buffer ERROR _HOME/core/events.lua /^module('events')]]$/;" F class:events ERROR _HOME/lexers/lexer.lua /^module('lexer')]=]$/;" F class:lexer -FILE_AFTER_RELOAD _HOME/core/file_io.lua /^module('io')]]$/;" F class:events FILE_AFTER_SAVE _HOME/core/file_io.lua /^module('io')]]$/;" F class:events -FILE_BEFORE_RELOAD _HOME/core/file_io.lua /^module('io')]]$/;" F class:events FILE_BEFORE_SAVE _HOME/core/file_io.lua /^module('io')]]$/;" F class:events FILE_CHANGED _HOME/core/file_io.lua /^module('io')]]$/;" F class:events FILE_OPENED _HOME/core/file_io.lua /^module('io')]]$/;" F class:events 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) -- cgit v1.2.3