Skip to content

Commit

Permalink
bad juju moving forward in buffer is sucky
Browse files Browse the repository at this point in the history
  • Loading branch information
xt0fer committed Nov 29, 2018
1 parent 21fb748 commit e7d8609
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 41 deletions.
13 changes: 12 additions & 1 deletion buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package kg
import (
"errors"
"fmt"
"log"
)

/*
Expand Down Expand Up @@ -104,11 +105,16 @@ func (bp *Buffer) SetPoint(np int) {
//bp.MoveGap(np - bp.Point)
// move gap <-(left) by np chars
gs := bp.gapStart()
log.Printf("gap start %d len %d new pt %d dist %d\n", gs, bp.gapLen(), np, gs-np)
f := 0
for i := gs - np; i > 0; i-- {
bp.data[bp.postStart()-1] = bp.data[bp.Point-1]
bp.Point--
bp.postLen++
f++
}
log.Printf("shuffled %d\n", f)

if bp.PageEnd < bp.Point {
bp.Reframe = true
}
Expand All @@ -133,7 +139,7 @@ func (bp *Buffer) postStart() int {
return len(bp.data) - bp.postLen
}

// CollapseGap moves the gap to the end of the buffer for replacement
// CollapseGap moves the gap to the end of the buffer
func (bp *Buffer) CollapseGap() {
//for i := bp.Point; bp.postLen > 0; i++ {
for bp.postLen > 0 {
Expand Down Expand Up @@ -507,6 +513,11 @@ func (bp *Buffer) GetLineStats() (curline int, lastline int) {
return curline, lastline
}

func (bp *Buffer) gotoLine(ln int) {
pt := bp.PointForLine(ln)
bp.SetPoint(pt)
}

// DebugPrint prints out a view of the buffer and the gap and so on.
func (bp *Buffer) DebugPrint() {
fmt.Printf("*********(gap)\n")
Expand Down
38 changes: 38 additions & 0 deletions buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,44 @@ func TestSetPoint(t *testing.T) {
assert.Equal(t, 12, gb.gapStart())
assert.Equal(t, 24, gb.gapLen())
gb.DebugPrint()
gb.SetPoint(0)
assert.Equal(t, 0, gb.gapStart())
assert.Equal(t, 24, gb.gapLen())
gb.SetPoint(8)
assert.Equal(t, 8, gb.gapStart())
assert.Equal(t, 24, gb.gapLen())
gb.DebugPrint()
gb.SetPoint(15)
assert.Equal(t, 15, gb.gapStart())
assert.Equal(t, 24, gb.gapLen())
gb.SetPoint(10)
assert.Equal(t, 10, gb.gapStart())
assert.Equal(t, 24, gb.gapLen())

gb.SetPoint(36)
assert.Equal(t, 36, gb.gapStart())
assert.Equal(t, 24, gb.gapLen())
gb.SetPoint(23)
assert.Equal(t, 23, gb.gapStart())
assert.Equal(t, 24, gb.gapLen())
gb.Backspace()
gb.Backspace()
gb.SetPoint(23)
assert.Equal(t, 23, gb.gapStart())
assert.Equal(t, 26, gb.gapLen())
gb.SetPoint(23)
assert.Equal(t, 23, gb.gapStart())
assert.Equal(t, 26, gb.gapLen())
gb.Insert("01234567890123456789")
assert.Equal(t, 43, gb.gapStart())
assert.Equal(t, 6, gb.gapLen())
gb.SetPoint(gb.TextSize - 1)
assert.Equal(t, 58, gb.gapStart())
assert.Equal(t, 6, gb.gapLen())
gb.SetPoint(0)
assert.Equal(t, 0, gb.gapStart())
assert.Equal(t, 6, gb.gapLen())

}

func TestRuneAt(t *testing.T) {
Expand Down
19 changes: 14 additions & 5 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package kg

import (
"io/ioutil"
"log"
"strconv"
"strings"
"unicode"
Expand Down Expand Up @@ -75,7 +76,6 @@ func (e *Editor) yesno(flag bool, prompt string) bool {
}

func (e *Editor) redraw() {

termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
e.CurrentWindow.Updated = true
e.CurrentBuffer.Reframe = true
Expand Down Expand Up @@ -104,11 +104,21 @@ func (e *Editor) wright() {
}

func (e *Editor) pgdown() {

pt := e.CurrentBuffer.Point
l1 := e.CurrentBuffer.LineForPoint(e.CurrentBuffer.PageEnd)
l2 := l1 + e.CurrentWindow.Rows - 2
npt := e.CurrentBuffer.PointForLine(l2)
log.Printf("start %d last line %d next %d new pt %d\n", pt, l1, l2, npt)
e.CurrentBuffer.SetPoint(npt)
e.CurrentWindow.Updated = true
}

func (e *Editor) pgup() {

l1 := e.CurrentBuffer.LineForPoint(e.CurrentBuffer.PageStart)
l2 := l1 - e.CurrentWindow.Rows - 2
npt := e.CurrentBuffer.PointForLine(l2)
log.Printf("last line %d next %d new pt %d\n", l1, l2, npt)
e.CurrentBuffer.SetPoint(npt)
}

func (e *Editor) backsp() {
Expand All @@ -127,8 +137,7 @@ func (e *Editor) gotoline() {
if err != nil {
e.msg("Invalid Line.")
}
pt := e.CurrentBuffer.PointForLine(ln)
e.CurrentBuffer.SetPoint(pt)
e.CurrentBuffer.gotoLine(ln)
}

func (e *Editor) insertfile() {
Expand Down
Binary file added docs/nfoo
Binary file not shown.
76 changes: 48 additions & 28 deletions editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package kg

import (
"fmt"
"log"
"os"
"strings"
"unicode"

Expand Down Expand Up @@ -55,19 +57,19 @@ type Editor struct {
// StartEditor is the old C main function
func (e *Editor) StartEditor(argv []string, argc int) {
// log setup....
// f, err := os.OpenFile("logfile", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
// if err != nil {
// log.Fatalf("error opening file: %v", err)
// }
// defer f.Close()
f, err := os.OpenFile("logfile", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("error opening file: %v", err)
}
defer f.Close()

// log.SetOutput(f)
// f.Truncate(0)
// // log.Println("Start of Log...")
log.SetOutput(f)
f.Truncate(0)
log.Println("Start of Log...")
//
e.FGColor = termbox.ColorDefault
e.BGColor = termbox.ColorWhite
err := termbox.Init()
err = termbox.Init()
checkErr(err)
defer termbox.Close()
e.Cols, e.Lines = termbox.Size()
Expand All @@ -83,16 +85,6 @@ func (e *Editor) StartEditor(argv []string, argc int) {
e.msg("NO file to open, creating scratch buffer")
e.CurrentBuffer = e.FindBuffer("*scratch*", true)
e.CurrentBuffer.Buffername = "*scratch*"
//_ = e.CurrentBuffer.GrowGap(gapchunk)
//s := "Lorem ipsum dolor sit amet,\nconsectetur adipiscing elit,\nsed do eiusmod tempor incididunt ut\nlabore et dolore magna aliqua. \n"

// e.CurrentBuffer.SetText("\n")
// e.CurrentBuffer.Insert(s)
// e.CurrentBuffer.Insert(" foo")
// e.CurrentBuffer.Insert(s)
// e.CurrentBuffer.Insert(" baz 2\n")
// e.CurrentBuffer.Insert(s)
// e.CurrentBuffer.Insert(" baz 2\n")
e.top()
}
e.CurrentWindow = NewWindow(e)
Expand Down Expand Up @@ -387,22 +379,40 @@ func (e *Editor) updateDisplay() {

// SetPointForMouse xxx
func (e *Editor) SetPointForMouse(mc, mr int) {
if mr > e.CurrentWindow.Rows {
mr = e.CurrentWindow.Rows
}
c, r := e.setWindowForMouse(mc, mr)
bp := e.CurrentBuffer
sl := bp.LineForPoint(bp.PageStart) // sl is startline of buffer frame
ml := sl + mr
ml := sl + r
mlpt := bp.PointForLine(ml)
mll := bp.LineLenAtPoint(mlpt) // how wide is line?
nc := mc + 1
if mll < mc {
nc := c + 1
if mll < c {
nc = mll
}
npt := bp.PointForXY(nc, ml)
bp.SetPoint(npt)
}

func (e *Editor) setWindowForMouse(mc, mr int) (c, r int) {
log.Printf("col %d row %d ", mc, mr)

wp := e.RootWindow
for wp != nil {
if (mr <= wp.Rows+wp.TopPt) && (mr >= wp.TopPt) {
log.Printf("set win rows %d top %d\n", wp.Rows, wp.TopPt)
e.setWindow(wp)
r = mr - wp.TopPt
// if mr == wp.Rows+wp.TopPt {
// r--
// }
c = mc
return
}
wp = wp.Next
}
return 0, e.Lines - 1
}

// ModeLine draw modeline for window
func (e *Editor) ModeLine(wp *Window) {
var lch, mch, och rune
Expand Down Expand Up @@ -603,9 +613,7 @@ func (e *Editor) splitWindow() {
e.CurrentWindow.Rows = ntru
nwp.TopPt = e.CurrentWindow.TopPt + ntru + 2
nwp.Rows = ntrl - 1
// log.Printf("New win %d %d\n", nwp.TopPt, nwp.Rows)
// log.Printf("Old win %d %d\n", e.CurrentWindow.TopPt, e.CurrentWindow.Rows)


/* insert it in the list */
wp2 := e.CurrentWindow.Next
e.CurrentWindow.Next = nwp
Expand All @@ -631,6 +639,18 @@ func (e *Editor) nextWindow() {
}
}

func (e *Editor) setWindow(wp *Window) {
e.CurrentWindow.Updated = true /* make sure modeline gets updated */
e.CurrentWindow = wp
e.CurrentWindow.Updated = true /* make sure modeline gets updated */
e.CurrentBuffer = e.CurrentWindow.Buffer
// if e.CurrentBuffer.WinCount > 1 {
// /* push win vars to buffer */
// window2Buffer(e.CurrentWindow)
// }
e.updateDisplay()
}

// DeleteOtherWindows
func (e *Editor) deleteOtherWindows() {
wp := e.RootWindow
Expand Down
14 changes: 7 additions & 7 deletions window.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,13 @@ func window2Buffer(w *Window) {
b.PointCol = w.Col
// this should be figured out.
/* fixup Pointers in other windows of the same buffer, if size of edit text changed */
// if b.Point > b.OrigPoint {
// sizeDelta := b.TextSize - b.PrevSize
// //b.MoveGap(sizeDelta)
// b.SetPoint(b.Point + sizeDelta)
// b.PageStart += sizeDelta
// b.PageEnd += sizeDelta
// }
if b.Point > b.OrigPoint {
sizeDelta := b.TextSize - b.PrevSize
b.MoveGap(sizeDelta)
b.SetPoint(b.Point + sizeDelta)
b.PageStart += sizeDelta
b.PageEnd += sizeDelta
}
}

// PushBuffer2Window xxx
Expand Down

0 comments on commit e7d8609

Please sign in to comment.