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

From: Ryan McQuen <ryanpcmcquen.att.member.fsf.org>
Date: Mon, 25 Mar 2019 21:59:00 +0100 (CET)

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



-- 
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 1.52.41 PM.png
File Attachment: Screen Shot 2019-03-25 at 1.52.13 PM.png
Received on Mon 25 Mar 2019 - 16:59:00 EDT

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