Re: [code] TA loading big C file

From: John Ferrier <>
Date: Tue, 6 Nov 2012 07:45:52 -0800 (PST)

Thank you so much , Mitchell. Basically I would like to deal two kinds of situations:   1) If a file has more than 10000 lines, I would like to have TA use a larger width to correctly display the line numbers when it is loaded;   2) A file may be less than 10000 lines when it is loaded, but after some editing it may have more than that, and TA need to dynamically adjust the width.   So I was asking if I could code like:   events.connect(events.FILE_OPENED or events.CHAR_ADDED, function(c) .....       Thanks again,   JF ----- Original Message ----- From: Mitchell <> To: "" <> Cc: Sent: Tuesday, November 6, 2012 8:11 AM Subject: Re: [code] TA loading big C file Hi John, On Mon, 5 Nov 2012, John Ferrier wrote: > > > Thank you, Mitchell. I found a piece of code on Textadept Wiki ( I am not sure if this is the code snippet you mentioned), and modified it as follows: > > ---------------------------------------------------------------- > > events.connect(events.KEYPRESS, function() >   local buffer = buffer >   local c = _SCINTILLA.constants >   local width = #(buffer.line_count..'') >   width = width > 4 and width or 4 >   buffer.margin_width_n[0] = 4 + width * buffer:text_width(c.STYLE_LINENUMBER, '9') > end) > ---------------------------------------------------------------- > > "events.KEYPRESS" was changed from "events.FILE_OPENED" because I would like to dynamically adjust the width for line numbers. > > Above works for simple cases but I have some concerns: > > > 1) What's the best event to connect with? I think maybe "events.KEYPRESS" is not necessary. Maybe something line "events.NEWLINE" is the best. FILE_OPENED captures big files when they are loaded without you having to press any keys or insert a newline. Using an unfiltered KEYPRESS event will run your code on each and every keypress and is unnecessary. Here is the code for a newline-equivalent event: events.connect(events.CHAR_ADDED, function(c)   if c ~= 10 then return end   -- do stuff here on newline character end) > 2) Can I connect the function with either of two events, such as "events.FILE_OPENED" or "events.KEYPRESS"? I'm not sure what you mean here. Literally, yes you can. As mentioned earlier, KEYPRESS is probably overkill while FILE_OPENED does not account for text added later to an opened buffer. > 3) Will this code snippet cause any potential problem in some complicated cases (e.g. multiple buffers and/or multiple views? It will not cause any problems, but keep in mind that any multiple views that show the same buffer you are working on will not auto-update when the margin of the current view changes. Mitchell

You are subscribed to
To change subscription settings, send an e-mail to
To unsubscribe, send an e-mail to
Received on Tue 06 Nov 2012 - 10:45:52 EST

This archive was generated by hypermail 2.2.0 : Wed 07 Nov 2012 - 06:29:54 EST