diff --git a/buffer.go b/buffer.go index de3df40..189db6f 100644 --- a/buffer.go +++ b/buffer.go @@ -3,6 +3,7 @@ package kg import ( "errors" "fmt" + "log" ) /* @@ -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 } @@ -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 { @@ -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") diff --git a/buffer_test.go b/buffer_test.go index b8f48bd..07c6e46 100644 --- a/buffer_test.go +++ b/buffer_test.go @@ -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) { diff --git a/command.go b/command.go index 9ac5fd0..b92b560 100644 --- a/command.go +++ b/command.go @@ -2,6 +2,7 @@ package kg import ( "io/ioutil" + "log" "strconv" "strings" "unicode" @@ -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 @@ -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() { @@ -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() { diff --git a/docs/nfoo b/docs/nfoo new file mode 100644 index 0000000..d3770e5 Binary files /dev/null and b/docs/nfoo differ diff --git a/editor.go b/editor.go index 07dc380..bd882da 100644 --- a/editor.go +++ b/editor.go @@ -2,6 +2,8 @@ package kg import ( "fmt" + "log" + "os" "strings" "unicode" @@ -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() @@ -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) @@ -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 @@ -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 @@ -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 diff --git a/window.go b/window.go index eebc173..9c40f51 100644 --- a/window.go +++ b/window.go @@ -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