forked from rgieseke/ta-common
-
Notifications
You must be signed in to change notification settings - Fork 0
/
save_strips_ws.lua
37 lines (34 loc) · 1.23 KB
/
save_strips_ws.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
-- By default Textadept's strips off trailing whitespace from all lines.
-- This is a good thing, especially if you are using version control.
-- To avoid moving the caret when you have just carefully found your
-- position in some deeply nested code, this extension saves your
-- position and by enabling virtual space keeps the cursor's
-- position. If another key is pressed the necessary spaces
-- are inserted again. To see what is going on enable
-- view whitespace.
module('_m.common.save_strips_ws', package.seeall)
-- Variable to save the current column.
local saved_col
-- Save position before saving.
events.connect('file_before_save',
function()
local buffer = buffer
saved_col = buffer.column[buffer.current_pos]
end, 1)
-- Go in virtual space to position the cursor was at before saving.
events.connect('file_after_save',
function()
local buffer = buffer
if saved_col > 0 then
virtual_space = buffer.virtual_space_options
buffer.virtual_space_options = 2
local col = buffer.column[buffer.current_pos]
local diff = saved_col - col
if diff > 0 then
for i=1, diff do
buffer:char_right()
end
end
buffer.virtual_space_options = virtual_space
end
end)