[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

Full code:

-- CamelCase to snake_case
keys.cy = function()
  if buffer.selection_empty then
    local pos = buffer.current_pos
    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
        t = t .. '_' .. c2
    if t:sub(1,1) == '_' then
      t = t:sub(2,#t)
    local text = buffer.get_text()
    text = text:gsub(s,t)
    buffer.empty_undo_buffer() -- after set_text undo becomes buggy[
    buffer.set_selection(pos, pos) -- cancel selection
    ui.statusbar_text = t

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