aboutsummaryrefslogtreecommitdiff
path: root/modules/textadept/editing.lua
diff options
context:
space:
mode:
authormitchell <70453897+667e-11@users.noreply.github.com>2009-01-25 22:02:15 -0500
committermitchell <70453897+667e-11@users.noreply.github.com>2009-01-25 22:02:15 -0500
commitff35e0ae339ce65589d8808514a694aab2c54bb3 (patch)
tree9f7aeb9106254ecb799351727d435f5d92de4c05 /modules/textadept/editing.lua
parentf0c18b47d2d4524c5975481a40bd36b1813417cb (diff)
downloadtextadept-ff35e0ae339ce65589d8808514a694aab2c54bb3.tar.gz
textadept-ff35e0ae339ce65589d8808514a694aab2c54bb3.zip
Moved auto-indent from core/events.lua to modules/textadept/editing.lua.
Diffstat (limited to 'modules/textadept/editing.lua')
-rw-r--r--modules/textadept/editing.lua27
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