aboutsummaryrefslogtreecommitdiffstats
path: root/src/layout/engine.nim
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-10-10 17:22:15 +0200
committerbptato <nincsnevem662@gmail.com>2024-10-10 17:31:18 +0200
commit8c64d7cb4e54c289a6c18f6c144125196d888296 (patch)
tree4cc8de480912beebe62ccf9cf9ba826dc160fe2c /src/layout/engine.nim
parentcf31d252ffe0aa195909a03efa714c6fc6e5fddf (diff)
downloadchawan-8c64d7cb4e54c289a6c18f6c144125196d888296.tar.gz
layout, pager: preserve tabs on display, selection & output
Substitute tabs with one of eight PUA characters based on their width, and convert them back in the pager: * TUI mode always prints spaces, but now handles tabs appropriately on cursor movement * dump mode tries to preserve hard tabs, but uses soft tabs when that is not possible (e.g. tabs after a margin, tab with background color, etc) * selection mode always outputs hard tabs.
Diffstat (limited to 'src/layout/engine.nim')
-rw-r--r--src/layout/engine.nim19
1 files changed, 13 insertions, 6 deletions
diff --git a/src/layout/engine.nim b/src/layout/engine.nim
index e0567d2a..a3c58341 100644
--- a/src/layout/engine.nim
+++ b/src/layout/engine.nim
@@ -767,17 +767,21 @@ proc processWhitespace(ictx: var InlineContext; state: var InlineState;
ictx.whitespacenum = 1
ictx.whitespaceFragment = state.fragment
of WhitespacePre, WhitespacePreWrap:
- #TODO whitespace type should be preserved here. (it isn't, because
- # it would break tabs in the current buffer model.)
ictx.whitespaceIsLF = false
if c == '\n':
ictx.flushLine(state)
elif c == '\t':
let realWidth = ictx.lbstate.charwidth + ictx.whitespacenum
- let targetTabStops = realWidth div 8 + 1
- let targetWidth = targetTabStops * 8
- ictx.whitespacenum += targetWidth - realWidth
- ictx.whitespaceFragment = state.fragment
+ # We must flush first, because addWord would otherwise try to wrap the
+ # line. (I think.)
+ ictx.flushWhitespace(state)
+ let w = ((realWidth + 8) and not 7) - realWidth
+ ictx.word.str.addUTF8(tabPUAPoint(w))
+ ictx.word.size.w += w * ictx.cellWidth
+ ictx.lbstate.charwidth += w
+ # Ditto here - we don't want the tab stop to get merged into the next
+ # word's atom.
+ discard ictx.addWord(state)
else:
inc ictx.whitespacenum
ictx.whitespaceFragment = state.fragment
@@ -824,6 +828,9 @@ proc layoutTextLoop(ictx: var InlineContext; state: var InlineState;
if u == 0xAD: # soft hyphen
ictx.wrappos = ictx.word.str.len
ictx.hasshy = true
+ elif u in TabPUARange: # filter out chars placed in our PUA range
+ ictx.word.str &= "\uFFFD"
+ ictx.word.size.w += 0xFFFD.width() * ictx.cellWidth
else:
for j in pi ..< i:
ictx.word.str &= str[j]