Re: [code] TA loading big C file

From: Mitchell <m.att.foicica.com>
Date: Tue, 6 Nov 2012 09:11:10 -0500 (Eastern Standard Time)

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 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 Tue 06 Nov 2012 - 09:11:10 EST

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