[code] [textadept] buffer.set_text and undo

From: Heck Fy <shitpoet.att.gmail.com>
Date: Wed, 22 Apr 2015 15:49:04 +0300

Hi! I've written a function for my code, that converts CamelStyle
identifier to snake_case, replacing all occurencies with buffer.get_text,
gsub and buffer.set_text.

It works great, but I noticied that if I undo after several such replaces
then the buffer resets to the original text before any replaces and even
any other manual modificatons!

As some workaround I commented begin/end_undo_action() commands and added
buffer.empty_undo_buffer() to my code but that's of course isnt
convinient...

Full code:

-- CamelCase to snake_case
keys.cy = function()
  if buffer.selection_empty then
    --buffer:begin_undo_action()
    local pos = buffer.current_pos
    textadept.editing.select_word()
    --buffer.copy()
    local s = buffer.get_sel_text()
    local l = s:lower()
    local t = ''
    for i = 1, #s do
      local c1 = s:sub(i,i)
      local c2 = l:sub(i,i)
      if c1 == c2 then
        t = t .. c1
      else
        t = t .. '_' .. c2
      end
    end
    if t:sub(1,1) == '_' then
      t = t:sub(2,#t)
    end
    local text = buffer.get_text()
    text = text:gsub(s,t)
    buffer.set_text(text)
    buffer.empty_undo_buffer() -- after set_text undo becomes buggy[
    buffer.set_selection(pos, pos) -- cancel selection
    buffer.goto_pos(pos)
    --buffer:end_undo_action()
    ui.statusbar_text = t
  end
end

-- 
You are subscribed to code.att.foicica.com.
To change subscription settings, send an e-mail to code+help.att.foicica.com.
To unsubscribe, send an e-mail to code+unsubscribe.att.foicica.com.
Received on Wed 22 Apr 2015 - 08:49:04 EDT

This archive was generated by hypermail 2.2.0 : Thu 23 Apr 2015 - 06:26:09 EDT