diff options
Diffstat (limited to 'modules')
-rw-r--r-- | modules/textadept/editing.lua | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/modules/textadept/editing.lua b/modules/textadept/editing.lua index 972b52d8..2d10b56e 100644 --- a/modules/textadept/editing.lua +++ b/modules/textadept/editing.lua @@ -75,6 +75,33 @@ textadept.events.add_handler('char_added', if char_matches[c] then buffer:insert_text(-1, char_matches[c]) end end) +textadept.events.add_handler('char_added', + function(char) -- auto-indent on return + if char ~= '\n' then return end + local buffer = buffer + local anchor, caret = buffer.anchor, buffer.current_pos + local curr_line = buffer:line_from_position(caret) + local last_line = curr_line - 1 + while last_line >= 0 and #buffer:get_line(last_line) == 1 do + last_line = last_line - 1 + end + if last_line >= 0 then + local indentation = buffer.line_indentation[last_line] + local s = buffer.line_indent_position[curr_line] + buffer.line_indentation[curr_line] = indentation + local e = buffer.line_indent_position[curr_line] + local diff = e - s + if e > s then -- move selection on + if anchor >= s then anchor = anchor + diff end + if caret >= s then caret = caret + diff end + elseif e < s then -- move selection back + if anchor >= e then anchor = anchor >= s and anchor + diff or e end + if caret >= e then caret = caret >= s and caret + diff or e end + end + buffer:set_sel(anchor, caret) + end + end) + -- local functions local insert_into_kill_ring, scroll_kill_ring local get_preceding_number, get_sel_or_line |