Re: [code] Re: Change to Line Number Margin Width calculation in init.lua

From: Ryan McQuen <ryanpcmcquen.att.member.fsf.org>
Date: Tue, 26 Mar 2019 00:28:02 +0100 (CET)

March 26, 2019 12:12:20 AM CET Ryan <ryanpcmcquen.att.member.fsf.org> wrote:

 Given that a file could jump to the next group of digits, while being edited, perhaps adding one to `lineCountLength` would be safer.

--
On Monday, Mar 25, 2019 at 1:59 PM, Ryan McQuen <ryanpcmcquen.att.member.fsf.org> wrote:
Textadept currently has some strange handling for the Line Number Margin Width, which results in numbers running over once files reach a certain number (depending on font, this can happen as soon as a file hits 100 lines). I propose the following change to `init.lua` which calculates the width based on the file when it is opened: 
--- init-old.lua 2019-03-25 13:48:32.000000000 -0700 
+++ init-new.lua 2019-03-25 13:49:30.000000000 -0700 
@@ -131,8 +131,17 @@ 
 --buffer.margin_right = 
 -- Line Number Margin. 
 buffer.margin_type_n[0] = buffer.MARGIN_NUMBER 
-local width = 4 * buffer:text_width(buffer.STYLE_LINENUMBER, '9') 
-buffer.margin_width_n[0] = width + (not CURSES and 4 or 0) 
+-- Increase the line number margin width, relatively: 
+events.connect( 
+ events.FILE_OPENED, 
+ function() 
+ if type(buffer.line_count) == 'number' then 
+ local lineCountLength = tostring(buffer.line_count):len() 
+ local width = lineCountLength * 12 
+ buffer.margin_width_n[0] = width + (not CURSES and 4 or 0) 
+ end 
+ end 
+) 
 -- Marker Margin. 
 buffer.margin_width_n[1] = not CURSES and 4 or 1 
 -- Fold Margin. 
You can see this in action in the attached screenshots, which allow the width to be set programmatically, dependent on the file. 
---- 
From: ryanpcmcquen.att.member.fsf.org 
To: code.att.foicica.com 
Mar 25, 2019, 1:58:17 PM
Sorry for the top post earlier (was on mobile).
Here's the new proposed diff:
--- init-old.lua    2019-03-25 16:23:30.000000000 -0700
+++ init-new.lua    2019-03-25 16:24:26.000000000 -0700
@@ -131,8 +131,17 @@
 --buffer.margin_right =
 -- Line Number Margin.
 buffer.margin_type_n[0] = buffer.MARGIN_NUMBER
-local width = 4 * buffer:text_width(buffer.STYLE_LINENUMBER, '9')
-buffer.margin_width_n[0] = width + (not CURSES and 4 or 0)
+-- Increase the line number margin width, relatively:
+events.connect(
+    events.FILE_OPENED,
+    function()
+        if type(buffer.line_count) == 'number' then
+            local lineCountLength = tostring(buffer.line_count):len()
+            local width = (lineCountLength + 1) * 12
+            buffer.margin_width_n[0] = width + (not CURSES and 4 or 0)
+        end
+    end
+)
 -- Marker Margin.
 buffer.margin_width_n[1] = not CURSES and 4 or 1
 -- Fold Margin.
For reference, I also attached a screenshot of the current behavior.
----
From: ryanpcmcquen.att.member.fsf.org
To: code.att.foicica.com
Mar 25, 2019, 4:26:59 PM


-- 
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.
File Attachment: Screen Shot 2019-03-25 at 4.26.22 PM.png
Received on Mon 25 Mar 2019 - 19:28:02 EDT

This archive was generated by hypermail 2.2.0 : Tue 26 Mar 2019 - 06:27:05 EDT