-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathredraw.lua
More file actions
322 lines (256 loc) · 6.67 KB
/
Copy pathredraw.lua
File metadata and controls
322 lines (256 loc) · 6.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
-- © 2008 David Given.
-- WordGrinder is licensed under the MIT open source license. See the COPYING
-- file in this distribution for the full text.
local int = math.floor
local Write = wg.write
local GotoXY = wg.gotoxy
local ClearArea = wg.cleararea
local SetNormal = wg.setnormal
local SetBold = wg.setbold
local SetBright = wg.setbright
local SetUnderline = wg.setunderline
local SetReverse = wg.setreverse
local SetDim = wg.setdim
local GetStringWidth = wg.getstringwidth
local messages = {}
local leftpadding = 0
function NonmodalMessage(s)
messages[#messages+1] = s
QueueRedraw()
end
function ResetNonmodalMessages()
messages = {}
end
function ResizeScreen()
ScreenWidth, ScreenHeight = wg.getscreensize()
local w = GetMaximumAllowedWidth(ScreenWidth)
local rw = w - Document.margin - 1
leftpadding = math.floor(ScreenWidth/2 - rw/2)
Document:wrap(w - Document.margin - 1)
end
local function drawmargin(y, pn, p)
local controller = MarginControllers[Document.viewmode]
if controller.getcontent then
local s = controller:getcontent(pn, p)
if s then
SetDim()
RAlignInField(leftpadding, y, Document.margin - 1, s)
SetNormal()
end
end
local bullet = DocumentStyles[p.style].bullet
if bullet then
local w = GetStringWidth(bullet) + 1
local i = DocumentStyles[p.style].indent
if (i >= w) then
Write(leftpadding + Document.margin + i - w, y, bullet)
end
end
end
local changed_tab =
{
[true] = "CHANGED"
}
local function redrawstatus()
local y = ScreenHeight - 1
if DocumentSet.statusbar then
local s = {
Leafname(DocumentSet.name or "(unnamed)"),
"[",
Document.name or "",
"] ",
changed_tab[DocumentSet.changed] or "",
}
SetReverse()
ClearArea(0, ScreenHeight-1, ScreenWidth-1, ScreenHeight-1)
LAlignInField(0, ScreenHeight-1, ScreenWidth, table.concat(s, ""))
local ss = {}
FireEvent(Event.BuildStatusBar, ss)
table.sort(ss, function(x, y) return x.priority < y.priority end)
local s = {" "}
for _, v in ipairs(ss) do
s[#s+1] = v.value
end
s = table.concat(s, " │ ")
if (string.sub(s, #s) == " ") then
s = string.sub(s, 1, #s-1)
end
RAlignInField(0, ScreenHeight-1, ScreenWidth, s)
SetNormal()
y = y - 1
end
if (#messages > 0) then
SetReverse()
for i = #messages, 1, -1 do
ClearArea(0, y, ScreenWidth-1, y)
Write(0, y, messages[i])
y = y - 1
end
SetNormal()
end
end
local topmarker = {
" ▲ ▲ ▲ ▲ ▲ ",
"───────────────────────────────────────────────────────"
}
local topmarkerwidth = GetStringWidth(topmarker[1])
local function drawtopmarker(y)
local x = int((ScreenWidth - topmarkerwidth)/2)
SetBright()
for i = #topmarker, 1, -1 do
if (y >= 0) then
Write(x, y, topmarker[i])
end
y = y - 1
end
SetNormal()
end
local bottommarker = {
"───────────────────────────────────────────────────────",
" ▼ ▼ ▼ ▼ ▼ ",
}
local bottommarkerwidth = GetStringWidth(bottommarker[1])
local function drawbottommarker(y)
local x = int((ScreenWidth - bottommarkerwidth)/2)
SetBright()
for i = 1, #bottommarker do
if (y <= ScreenHeight) then
Write(x, y, bottommarker[i])
end
y = y + 1
end
SetNormal()
end
function RedrawScreen()
wg.clearscreen()
local cp, cw, co = Document.cp, Document.cw, Document.co
local cy = int(ScreenHeight / 2)
local margin = Document.margin
-- Find out the offset of the current paragraph.
local paragraph = Document[cp]
local ocw = cw
local cl
cl, cw = paragraph:getLineOfWord(cw)
if not cl then
error("word "..ocw.." not in para "..cp.." of len "..#paragraph)
end
-- Position the cursor.
do
local cw = Document.cw
local word = paragraph[cw]
GotoXY(leftpadding + margin + paragraph.xs[cw] +
GetWidthFromOffset(word, Document.co) + paragraph:getIndentOfLine(cl),
cy - 1)
end
-- Cache values for mark drawing.
local mp = Document.mp
local mw = Document.mw
local mo = Document.mo
-- Draw backwards.
local pn = cp - 1
local y = cy - cl - 1 - Document:spaceAbove(cp)
Document.topp = nil
Document.topw = nil
while (y >= 0) do
local paragraph = Document[pn]
if not paragraph then
break
end
local lines = paragraph:wrap()
for ln = #lines, 1, -1 do
local x = paragraph:getIndentOfLine(ln)
local l = lines[ln]
if not mp then
paragraph:renderLine(l,
leftpadding + margin + x, y)
else
paragraph:renderMarkedLine(l,
leftpadding + margin + x, y, nil, pn)
end
if (ln == 1) then
drawmargin(y, pn, paragraph)
end
Document.topp = pn
Document.topw = l.wn
y = y - 1
if (y < 0) then
break
end
end
y = y - Document:spaceAbove(pn)
pn = pn - 1
end
if (y >= 0) and WantTerminators() then
drawtopmarker(y)
end
-- Draw forwards.
y = cy - cl
pn = cp
while (y < ScreenHeight) do
local paragraph = Document[pn]
if not paragraph then
break
end
drawmargin(y, pn, paragraph)
for ln, l in ipairs(paragraph:wrap()) do
local x = paragraph:getIndentOfLine(ln)
if not mp then
paragraph:renderLine(l,
leftpadding + margin + x, y)
else
paragraph:renderMarkedLine(l,
leftpadding + margin + x, y, nil, pn)
end
-- If the top of the page hasn't already been set, then the
-- current paragraph extends off the top of the screen.
if not Document.topp and (y == 0) then
Document.topp = pn
Document.topw = l.wn
end
Document.botp = pn
Document.botw = l.wn
y = y + 1
if (y > ScreenHeight) then
break
end
end
y = y + Document:spaceBelow(pn)
pn = pn + 1
end
-- If the top of the page *still* hasn't been set, then we're on the
-- first paragraph of the document.
if not Document.topp then
Document.topp = 1
Document.topw = 1
end
if (y <= ScreenHeight) and WantTerminators() then
drawbottommarker(y)
end
redrawstatus()
FireEvent(Event.Redraw)
end
-----------------------------------------------------------------------------
-- Maintains the word count field in the current document.
do
local function cb(event, token)
local wc = 0
for _, p in ipairs(Document) do
wc = wc + #p
end
Document.wordcount = wc
end
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