[code][textadept] Dirty buffers

From: Richard Philips <richard.philips.att.gmail.com>
Date: Fri, 08 Nov 2013 16:23:45 +0100

Hi,

I find it is very easy for a buffer in Textadept to become dirty :-)

e.g.:

- executing `io.set_buffer_encoding('UTF-8')` with CTRL+E in a buffer
which uses already UTF-8 makes the buffer dirty.
    (same thing with the other encodings)

- changing the buffer with search/replace where the search and replace
string is the same (do not ask :-) makes the buffer dirty.

Especially the first example is cumbersome: if you create a new language
module which should be in a specific encoding, say 'ISO-8859-1'.
and you put in your module: `io.set_buffer_encoding('ISO-8859-1')` then
every time you open a file of that type in textadept, its buffer becomes
dirty.

Looking at the code in file_io.lua:

function io.set_buffer_encoding(encoding)
   if not buffer.encoding then
     error(_L['Cannot change binary file encoding'])
   end
   local pos, first_visible_line = buffer.current_pos,
buffer.first_visible_line
   local text = buffer:get_text()
   text = text:iconv(buffer.encoding, 'UTF-8')
   text = text:iconv(encoding, buffer.encoding)
   text = text:iconv('UTF-8', encoding)
   buffer:clear_all()
   buffer:add_text(text, #text)
   buffer:line_scroll(0, first_visible_line)
   buffer:goto_pos(pos)
   buffer.encoding, buffer.encoding_bom = encoding, io.boms[encoding]
end

can be replaced with:

function io.set_buffer_encoding(encoding)
   if not buffer.encoding then
     error(_L['Cannot change binary file encoding'])
   end
   local pos, first_visible_line = buffer.current_pos,
buffer.first_visible_line
   local text, text2 = buffer:get_text()
   text2 = text:iconv(buffer.encoding, 'UTF-8')
   text2 = text2:iconv(encoding, buffer.encoding)
   text2 = text2:iconv('UTF-8', encoding)
   if text2 ~= text then
       buffer:clear_all()
       buffer:add_text(text, #text)
       buffer:line_scroll(0, first_visible_line)
       buffer:goto_pos(pos)
   end
   buffer.encoding, buffer.encoding_bom = encoding, io.boms[encoding]
end

Richard

-- 
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 Fri 08 Nov 2013 - 10:23:45 EST

This archive was generated by hypermail 2.2.0 : Sat 09 Nov 2013 - 06:42:47 EST