diff --git a/src/lua/addons/statusbar_pagecount.lua b/src/lua/addons/statusbar_pagecount.lua index 81488cfd..8ff88df8 100644 --- a/src/lua/addons/statusbar_pagecount.lua +++ b/src/lua/addons/statusbar_pagecount.lua @@ -8,8 +8,14 @@ do local function cb(event, token, terms) local settings = DocumentSet.addons.pagecount or {} + local pages if settings.enabled then - local pages = math.floor((Document.wordcount or 0) / settings.wordsperpage) + if (settings.pagesbylines) then + pages = math.ceil((Document.linecount or 0) / settings.linesperpage) + else + pages = math.ceil((Document.wordcount or 0) / settings.wordsperpage) + end + terms[#terms+1] = { priority=80, value=string.format("%d %s", pages, @@ -28,7 +34,9 @@ do local function cb() DocumentSet.addons.pagecount = DocumentSet.addons.pagecount or { enabled = false, + pagesbylines = true, wordsperpage = 250, + linesperpage = 22, } end @@ -44,23 +52,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 +89,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 +115,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..32d4ca36 100644 --- a/src/lua/fileio.lua +++ b/src/lua/fileio.lua @@ -726,4 +726,18 @@ 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 + + --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 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