diff options
author | mitchell <70453897+667e-11@users.noreply.github.com> | 2020-02-28 17:52:44 -0500 |
---|---|---|
committer | mitchell <70453897+667e-11@users.noreply.github.com> | 2020-02-28 17:52:44 -0500 |
commit | 28b4a90aa7609d7a37cd2c32eef97ba4be112084 (patch) | |
tree | a494990f6dd80237b1422af930e134e595c00411 | |
parent | 2e8301161d5b0ef1e7a5762c090b3776da53dc60 (diff) |
Autopair, typeover, and backspace delete match works with multiple selection.
-rw-r--r-- | core/ui.lua | 2 | ||||
-rw-r--r-- | modules/textadept/editing.lua | 61 |
2 files changed, 35 insertions, 28 deletions
diff --git a/core/ui.lua b/core/ui.lua index 88dd83c6..b5c46114 100644 --- a/core/ui.lua +++ b/core/ui.lua @@ -324,7 +324,7 @@ local GETLEXERLANGUAGE = _SCINTILLA.properties.lexer_language[1] -- Sets buffer statusbar text. events_connect(events.UPDATE_UI, function(updated) if updated and updated & 3 == 0 then return end -- ignore scrolling - local pos = buffer.current_pos + local pos = buffer.selection_n_caret[buffer.main_selection] local line, max = buffer:line_from_position(pos) + 1, buffer.line_count local col = buffer.column[pos] + 1 local lexer = buffer:private_lexer_call(GETLEXERLANGUAGE):match('^[^/]+') diff --git a/modules/textadept/editing.lua b/modules/textadept/editing.lua index f97e5618..b27d9fea 100644 --- a/modules/textadept/editing.lua +++ b/modules/textadept/editing.lua @@ -120,54 +120,61 @@ M.autocompleters = {} -- @class table -- @name api_files -- @see show_documentation -M.api_files = {} -setmetatable(M.api_files, {__index = function(t, k) +M.api_files = setmetatable({}, {__index = function(t, k) t[k] = {} return t[k] end}) -- Matches characters specified in auto_pairs. events.connect(events.CHAR_ADDED, function(code) - if M.auto_pairs and M.auto_pairs[code] and buffer.selections == 1 then - buffer:insert_text(-1, M.auto_pairs[code]) + if M.auto_pairs and M.auto_pairs[code] then + buffer:begin_undo_action() + for i = 0, buffer.selections - 1 do + local pos = buffer.selection_n_caret[i] + buffer:set_target_range(pos, pos) + buffer:replace_target(M.auto_pairs[code]) + end + buffer:end_undo_action() end end) -- Removes matched chars on backspace. events.connect(events.KEYPRESS, function(code) - if not M.auto_pairs or keys.KEYSYMS[code] ~= '\b' or - buffer.selections ~= 1 then - return - end - local byte = buffer.char_at[buffer.current_pos - 1] - if M.auto_pairs[byte] and - buffer.char_at[buffer.current_pos] == string.byte(M.auto_pairs[byte]) then - buffer:clear() + if not M.auto_pairs or keys.KEYSYMS[code] ~= '\b' then return end + buffer:begin_undo_action() + for i = 0, buffer.selections - 1 do + local pos = buffer.selection_n_caret[i] + local complement = M.auto_pairs[buffer.char_at[pos - 1]] + if complement and buffer.char_at[pos] == string.byte(complement) then + buffer:set_target_range(pos, pos + 1) + buffer:replace_target('') + end end + buffer:end_undo_action() end) -- Highlights matching braces. events.connect(events.UPDATE_UI, function(updated) if updated and updated & 3 == 0 then return end -- ignore scrolling - if M.brace_matches[buffer.char_at[buffer.current_pos]] then - local match = buffer:brace_match(buffer.current_pos, 0) - if match ~= -1 then - buffer:brace_highlight(buffer.current_pos, match) - else - buffer:brace_bad_light(buffer.current_pos) - end - else - buffer:brace_bad_light(-1) - end + local pos = buffer.selection_n_caret[buffer.main_selection] + local match = M.brace_matches[buffer.char_at[pos]] and + buffer:brace_match(pos, 0) or -1 + local f = buffer[match ~= -1 and 'brace_highlight' or 'brace_bad_light'] + f(pos, match) end) -- Moves over typeover characters when typed. events.connect(events.KEYPRESS, function(code) - if M.typeover_chars and M.typeover_chars[code] and - buffer.selection_start == buffer.selection_end and - buffer.char_at[buffer.current_pos] == code then - buffer:char_right() - return true + if M.typeover_chars and M.typeover_chars[code] then + local handled = false + for i = 0, buffer.selections - 1 do + local s, e = buffer.selection_n_start[i], buffer.selection_n_end[i] + if s == e and buffer.char_at[s] == code then + buffer.selection_n_start[i], buffer.selection_n_end[i] = s + 1, s + 1 + handled = true + end + end + if handled then return true end end end) |