From 2b9d2cc305af9eaadf7f7a394fa33c4931b7f6b1 Mon Sep 17 00:00:00 2001 From: Robert Kroeger Date: Sun, 15 Jul 2018 13:10:54 -0400 Subject: [PATCH] Relocate typing code More code motion for #5. --- text.go | 88 +++++++--------------------------- typing.go | 138 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 151 insertions(+), 75 deletions(-) diff --git a/text.go b/text.go index 799f18f9..1c77281e 100644 --- a/text.go +++ b/text.go @@ -743,108 +743,52 @@ func (t *Text) Type(r rune) { switch r { case draw.KeyLeft: - t.TypeCommit() - if t.q0 > 0 { - if t.q0 != t.q1 { - t.Show(t.q0, t.q0, true) - } else { - t.Show(t.q0-1, t.q0-1, true) - } - } + t.KeyLeft() return case draw.KeyRight: - t.TypeCommit() - if t.q1 < t.file.b.Nc() { - // This is a departure from the plan9/plan9port acme - // Instead of always going right one char from q1, it - // collapses multi-character selections first, behaving - // like every other selection on modern systems. -flux - if t.q0 != t.q1 { - t.Show(t.q1, t.q1, true) - } else { - t.Show(t.q1+1, t.q1+1, true) - } - } + t.KeyRight() return case draw.KeyDown: - t.tagdownalways(func() { - t.keydownhelper(t.fr.GetFrameFillStatus().Maxlines / 3) - }) + t.KeyDownTagExpanding() return case Kscrollonedown: - t.tagdownalways(func() { - t.keydownhelper(max(1, mousescrollsize(t.fr.GetFrameFillStatus().Maxlines))) - }) + t.KeyScrollOneDown() return case draw.KeyPageDown: - t.keydownhelper(2 * t.fr.GetFrameFillStatus().Maxlines / 3) + t.KeyPageDown() return case draw.KeyUp: - t.tagupalways(func() { - t.keyuphelper( t.fr.GetFrameFillStatus().Maxlines / 3) - }) + t.KeyUpTagExpanding() return case Kscrolloneup: - t.tagupalways(func() { - t.keyuphelper(mousescrollsize(t.fr.GetFrameFillStatus().Maxlines)) - }) + t.KeyScrollOneUp() return case draw.KeyPageUp: - t.keyuphelper(2 * t.fr.GetFrameFillStatus().Maxlines / 3) + t.KeyPageUp() return case draw.KeyHome: - t.TypeCommit() - if t.org > t.iq1 { - q0 = t.Backnl(t.iq1, 1) - t.SetOrigin(q0, true) - } else { - t.Show(0, 0, false) - } + t.KeyHome() return case draw.KeyEnd: - t.TypeCommit() - if t.iq1 > t.org+t.fr.GetFrameFillStatus().Nchars { - if t.iq1 > t.file.b.Nc() { - // should not happen, but does. and it will crash textbacknl. - t.iq1 = t.file.b.Nc() - } - q0 = t.Backnl(t.iq1, 1) - t.SetOrigin(q0, true) - } else { - t.Show(t.file.b.Nc(), t.file.b.Nc(), false) - } + t.KeyEnd() return case 0x01: /* ^A: beginning of line */ - t.TypeCommit() - /* go to where ^U would erase, if not already at BOL */ - nnb = 0 - if t.q0 > 0 && t.ReadC(t.q0-1) != '\n' { - nnb = t.BsWidth(0x15) - } - t.Show(t.q0-nnb, t.q0-nnb, true) + t.KeyLineBeginning() return case 0x05: /* ^E: end of line */ - t.TypeCommit() - q0 = t.q0 - for q0 < t.file.b.Nc() && t.ReadC(q0) != '\n' { - q0++ - } - t.Show(q0, q0, true) + t.KeyLineEnding() return case draw.KeyCmd + 'c': /* %C: copy */ - t.TypeCommit() - cut(t, t, nil, true, false, "") + t.KeyCmdC() return case draw.KeyCmd + 'z': /* %Z: undo */ - t.TypeCommit() - undo(t, nil, nil, true, false, "") + t.KeyCmdZ() return case draw.KeyCmd + 'Z': /* %-shift-Z: redo */ - t.TypeCommit() - undo(t, nil, nil, false, false, "") + t.KeyShiftCmdZ() return - } + if t.what == Body { seq++ t.file.Mark() diff --git a/typing.go b/typing.go index c75a51de..cfadf6a9 100644 --- a/typing.go +++ b/typing.go @@ -6,10 +6,10 @@ import ( // All the sub-functions needed to implement typing are in this file. -// tagdown expands the tag to show all of the text. +// tagdown wraps the nta key handling function to always open the +// tag. func (t *Text) tagdownalways(nta func()) { if t.what == Tag { - if !t.w.tagexpand { t.w.tagexpand = true t.w.Resize(t.w.r, false, true) @@ -21,7 +21,8 @@ func (t *Text) tagdownalways(nta func()) { } -// tagup shrinks the tag to a single line +// tagup wraps the nta key handling function to always collapse +// the tag. func (t *Text) tagupalways(nta func()) { if t.what == Tag { if t.w.tagexpand { @@ -49,3 +50,134 @@ func (t *Text) keyuphelper(n int) { q0 := t.Backnl(t.org, n) t.SetOrigin(q0, true) } + +// KeyLeft handles left-arrow. +func (t *Text) KeyLeft() { + t.TypeCommit() + if t.q0 > 0 { + if t.q0 != t.q1 { + t.Show(t.q0, t.q0, true) + } else { + t.Show(t.q0-1, t.q0-1, true) + } + } +} + +// KeyRight handles right-arrow. +func (t *Text) KeyRight() { + t.TypeCommit() + if t.q1 < t.file.b.Nc() { + // This is a departure from the plan9/plan9port acme + // Instead of always going right one char from q1, it + // collapses multi-character selections first, behaving + // like every other selection on modern systems. -flux + if t.q0 != t.q1 { + t.Show(t.q1, t.q1, true) + } else { + t.Show(t.q1+1, t.q1+1, true) + } + } +} + +// KeyDown handles down with tag expansion. +func (t *Text) KeyDownTagExpanding() { + t.tagdownalways(func() { + t.keydownhelper(t.fr.GetFrameFillStatus().Maxlines / 3) + }) +} + +// KeyScrollOneDown handles scroll down with tag expansion. +func (t *Text) KeyScrollOneDown() { + t.tagdownalways(func() { + t.keydownhelper(max(1, mousescrollsize(t.fr.GetFrameFillStatus().Maxlines))) + }) +} + +// KeyPageDown handles page down +func (t *Text) KeyPageDown() { + t.keydownhelper(2 * t.fr.GetFrameFillStatus().Maxlines / 3) +} + +// KeyUp handles arrow up. +func (t *Text) KeyUpTagExpanding() { + t.tagupalways(func() { + t.keyuphelper(t.fr.GetFrameFillStatus().Maxlines / 3) + }) +} + +// KeyScrollOneUp handles scroll up with tag collapsing. +func (t *Text) KeyScrollOneUp() { + t.tagupalways(func() { + t.keyuphelper(mousescrollsize(t.fr.GetFrameFillStatus().Maxlines)) + }) +} + +// KeyPageUp handles page up. +func (t *Text) KeyPageUp() { + t.keyuphelper(2 * t.fr.GetFrameFillStatus().Maxlines / 3) +} + +// KeyHome handles pressing the home key. +func (t *Text) KeyHome() { + t.TypeCommit() + if t.org > t.iq1 { + q0 := t.Backnl(t.iq1, 1) + t.SetOrigin(q0, true) + } else { + t.Show(0, 0, false) + } +} + +// KeyEnd handles pressing the end key. +func (t *Text) KeyEnd() { + t.TypeCommit() + if t.iq1 > t.org+t.fr.GetFrameFillStatus().Nchars { + if t.iq1 > t.file.b.Nc() { + // should not happen, but does. and it will crash textbacknl. + t.iq1 = t.file.b.Nc() + } + q0 := t.Backnl(t.iq1, 1) + t.SetOrigin(q0, true) + } else { + t.Show(t.file.b.Nc(), t.file.b.Nc(), false) + } +} + +// KeyLineBeginning handles pressing a key to move to the beginning of the line. +func (t *Text) KeyLineBeginning() { + t.TypeCommit() + /* go to where ^U would erase, if not already at BOL */ + nnb := 0 + if t.q0 > 0 && t.ReadC(t.q0-1) != '\n' { + nnb = t.BsWidth(0x15) + } + t.Show(t.q0-nnb, t.q0-nnb, true) +} + +// KeyLineEnding handles pressing a key to move to the end of the line. +func (t *Text) KeyLineEnding() { + t.TypeCommit() + q0 := t.q0 + for q0 < t.file.b.Nc() && t.ReadC(q0) != '\n' { + q0++ + } + t.Show(q0, q0, true) +} + +// KeyCmdC handles ⌘-C +func (t *Text) KeyCmdC() { + t.TypeCommit() + cut(t, t, nil, true, false, "") +} + +// KeyCmdZ handles⌘-Z +func (t *Text) KeyCmdZ() { + t.TypeCommit() + undo(t, nil, nil, true, false, "") +} + +// KeyShiftCmdZ handles ⌘-Shift-C +func (t *Text) KeyShiftCmdZ() { + t.TypeCommit() + undo(t, nil, nil, false, false, "") +}