From 07a39517f7361fa772023bc891c6c47ed1bda3cd Mon Sep 17 00:00:00 2001 From: bkepps Date: Tue, 4 Aug 2020 12:34:40 -0500 Subject: [PATCH 1/3] merge test --- src/lua/addons/statusbar_pagecount.lua | 72 ++++++++++++++++++++------ src/lua/document.lua | 1 + src/lua/fileio.lua | 9 ++++ src/lua/redraw.lua | 19 +++++++ 4 files changed, 85 insertions(+), 16 deletions(-) diff --git a/src/lua/addons/statusbar_pagecount.lua b/src/lua/addons/statusbar_pagecount.lua index 81488cfd..02f2ebe5 100644 --- a/src/lua/addons/statusbar_pagecount.lua +++ b/src/lua/addons/statusbar_pagecount.lua @@ -9,12 +9,21 @@ do local function cb(event, token, terms) local settings = DocumentSet.addons.pagecount or {} if settings.enabled then - local pages = math.floor((Document.wordcount or 0) / settings.wordsperpage) - terms[#terms+1] = { - priority=80, - value=string.format("%d %s", pages, - Pluralise(pages, "page", "pages")) - } + if settings.pagesbylines then + local pages = math.ceil((Document.linecount or 0) / settings.linesperpage) + terms[#terms+1] = { + priority=80, + value=string.format("%d %s", pages, + Pluralise(pages, "page", "pages")) + } + else + local pages = math.ceil((Document.linecount or 0) / settings.wordsperpage) + terms[#terms+1] = { + priority=80, + value=string.format("%d %s", pages, + Pluralise(pages, "page", "pages")) + } + end end end @@ -28,7 +37,9 @@ do local function cb() DocumentSet.addons.pagecount = DocumentSet.addons.pagecount or { enabled = false, - wordsperpage = 250, + pagesbylines = true, + wordsperpage = 251, + linesperpage = 22, } end @@ -44,23 +55,36 @@ function Cmd.ConfigurePageCount() local enabled_checkbox = Form.Checkbox { x1 = 1, y1 = 1, - x2 = 33, y2 = 1, + x2 = 40, y2 = 1, label = "Show approximate page count", value = settings.enabled } - + local mode_checkbox = + Form.Checkbox { + x1 = 1, y1 = 3, + x2 = 40, y2 = 3, + label = "estimate pagecount by lines, not words", + value = settings.pagesbylines + } local count_textfield = Form.TextField { - x1 = 33, y1 = 3, - x2 = 43, y2 = 3, + x1 = 40, y1 = 5, + x2 = 50, y2 = 5, value = tostring(settings.wordsperpage) } + + local count_lpptextfield = + Form.TextField { + x1 = 40, y1 = 7, + x2 = 50, y2 = 7, + value = tostring(settings.linesperpage) + } local dialogue = { title = "Configure Page Count", width = Form.Large, - height = 5, + height = 11, stretchy = false, ["KEY_^C"] = "cancel", @@ -68,16 +92,24 @@ function Cmd.ConfigurePageCount() ["KEY_ENTER"] = "confirm", enabled_checkbox, + mode_checkbox, Form.Label { - x1 = 1, y1 = 3, - x2 = 32, y2 = 3, + x1 = 1, y1 = 5, + x2 = 39, y2 = 5, align = Form.Left, value = "Number of words per page:" }, count_textfield, + + Form.Label { + x1 = 1, y1 = 7, + x2 = 39, y2 = 7, + align = Form.Left, + value = "Number of lines per page:" + }, + count_lpptextfield } - while true do local result = Form.Run(dialogue, RedrawScreen, "SPACE to toggle, RETURN to confirm, CTRL+C to cancel") @@ -86,13 +118,21 @@ function Cmd.ConfigurePageCount() end local enabled = enabled_checkbox.value + local pagesbylines = mode_checkbox.value local wordsperpage = tonumber(count_textfield.value) - + local linesperpage = tonumber(count_lpptextfield.value) + if not wordsperpage then ModalMessage("Parameter error", "The number of words per page must be a valid number.") + + elseif not linesperpage then + ModalMessage("Parameter error", "The number of lines per page must be a valid number.") + else settings.enabled = enabled + settings.pagesbylines = pagesbylines settings.wordsperpage = wordsperpage + settings.linesperpage = linesperpage DocumentSet:touch() return true diff --git a/src/lua/document.lua b/src/lua/document.lua index 4ff93e18..a2358128 100644 --- a/src/lua/document.lua +++ b/src/lua/document.lua @@ -228,6 +228,7 @@ DocumentClass = -- These should no longer exist; this dates from a previous attempt -- at undo with file version 6. We're not storing the undo buffer -- in files any more. + -- lol XD this is the content of so many programming memes self.undostack = nil self.redostack = nil end, diff --git a/src/lua/fileio.lua b/src/lua/fileio.lua index e3816100..c3b8f084 100644 --- a/src/lua/fileio.lua +++ b/src/lua/fileio.lua @@ -726,4 +726,13 @@ function UpgradeDocument(oldversion) end DocumentSet.styles = nil end + + -- Upgrade version 7 to 8! + + if (oldversion < 8) then + --this version is where the number of lines is a tracked value, and two fields were + --added to the pagecount addon + + Document.linecount = nil + end end diff --git a/src/lua/redraw.lua b/src/lua/redraw.lua index f68ab485..7beb05d3 100644 --- a/src/lua/redraw.lua +++ b/src/lua/redraw.lua @@ -301,3 +301,22 @@ do AddEventListener(Event.Changed, cb) end + +----------------------------------------------------------------------------- +--Maintains the line count field in the current document. + +do + local function cb(event, token) + local lc = 0 + local nl = 0 + + for _, p in ipairs(Document) do + lc = lc + #p:wrap() + + end + + Document.linecount = lc + end + + AddEventListener(Event.Changed, cb) +end From 2e318bb63643a61e0602fb23f8035a4b1096092c Mon Sep 17 00:00:00 2001 From: bkepps Date: Wed, 5 Aug 2020 19:44:13 -0500 Subject: [PATCH 2/3] cleanup --- src/lua/addons/statusbar_pagecount.lua | 2 +- src/lua/fileio.lua | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/lua/addons/statusbar_pagecount.lua b/src/lua/addons/statusbar_pagecount.lua index 02f2ebe5..995d5038 100644 --- a/src/lua/addons/statusbar_pagecount.lua +++ b/src/lua/addons/statusbar_pagecount.lua @@ -17,7 +17,7 @@ do Pluralise(pages, "page", "pages")) } else - local pages = math.ceil((Document.linecount or 0) / settings.wordsperpage) + local pages = math.ceil((Document.wordcount or 0) / settings.wordsperpage) terms[#terms+1] = { priority=80, value=string.format("%d %s", pages, diff --git a/src/lua/fileio.lua b/src/lua/fileio.lua index c3b8f084..32d4ca36 100644 --- a/src/lua/fileio.lua +++ b/src/lua/fileio.lua @@ -732,6 +732,11 @@ function UpgradeDocument(oldversion) if (oldversion < 8) then --this version is where the number of lines is a tracked value, and two fields were --added to the pagecount addon + + --not sure of how you're setting FILEVERSION or how you've been doing file version updates + -- + --I've added a few more variables to the file, so it seems like + --there should be a new fileversion Document.linecount = nil end From 088cd6552d08ff29956f9fb53a4d28e77af7b66b Mon Sep 17 00:00:00 2001 From: bkepps Date: Wed, 5 Aug 2020 20:44:05 -0500 Subject: [PATCH 3/3] took way to long to realize you need make install to actually output an executable --- src/lua/addons/statusbar_pagecount.lua | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/src/lua/addons/statusbar_pagecount.lua b/src/lua/addons/statusbar_pagecount.lua index 995d5038..8ff88df8 100644 --- a/src/lua/addons/statusbar_pagecount.lua +++ b/src/lua/addons/statusbar_pagecount.lua @@ -8,22 +8,19 @@ do local function cb(event, token, terms) local settings = DocumentSet.addons.pagecount or {} + local pages if settings.enabled then - if settings.pagesbylines then - local pages = math.ceil((Document.linecount or 0) / settings.linesperpage) - terms[#terms+1] = { - priority=80, - value=string.format("%d %s", pages, - Pluralise(pages, "page", "pages")) - } + if (settings.pagesbylines) then + pages = math.ceil((Document.linecount or 0) / settings.linesperpage) else - local pages = math.ceil((Document.wordcount or 0) / settings.wordsperpage) - terms[#terms+1] = { - priority=80, - value=string.format("%d %s", pages, - Pluralise(pages, "page", "pages")) - } + pages = math.ceil((Document.wordcount or 0) / settings.wordsperpage) end + + terms[#terms+1] = { + priority=80, + value=string.format("%d %s", pages, + Pluralise(pages, "page", "pages")) + } end end @@ -38,7 +35,7 @@ do DocumentSet.addons.pagecount = DocumentSet.addons.pagecount or { enabled = false, pagesbylines = true, - wordsperpage = 251, + wordsperpage = 250, linesperpage = 22, } end