Skip to content

Commit

Permalink
Merge pull request #13 from kristofer/updatego
Browse files Browse the repository at this point in the history
Updatego
  • Loading branch information
xt0fer authored Nov 19, 2021
2 parents 31c896f + 044e176 commit ef55b52
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 46 deletions.
22 changes: 22 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Attach to Process",
"type": "go",
"request": "attach",
"mode": "local",
"processId": 0
},
{
"name": "Launch file",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${file}"
}
]
}
21 changes: 11 additions & 10 deletions buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ package kg
import (
"errors"
"fmt"
"log"
)

/*
* Buffer is where all the opertions on the main rune array are implemented.
* Buffer is where all the operations on the main rune array are implemented.
* Because of the Gap, all the indexing around it should be done by these routines.
*/

Expand All @@ -19,7 +18,7 @@ type Buffer struct {

Next *Buffer
Mark int
OrigPoint int /* b_cpoint the original current point, used for mutliple window displaying */
OrigPoint int /* b_cpoint the original current point, used for multiple window displaying */
PageStart int /* start of page */
PageEnd int /* end of page */
Reframe bool /* force a reframe of the display */
Expand Down Expand Up @@ -64,18 +63,20 @@ func (bp *Buffer) getText() string {
return string(ret)
}

// RuneAt finally reliable!!
// RuneAt finally reliable!! (well, maybe not)
func (bp *Buffer) RuneAt(pt int) (rune, error) {
//log.Println("RuneAt pt = ", pt)
if pt >= len(bp.data) {
return 0, errors.New("Beyond data buffer in RuneAt")
return 0, errors.New("beyond data buffer in RuneAt")
}
if pt < 0 {
return '\u0000', errors.New("negative buffer pointer in RuneAt")
//return '\u0000', errors.New("negative buffer pointer in RuneAt")
pt = 0
}
if npt := bp.dataPointForBufferPoint(pt); npt < len(bp.data) {
return bp.data[npt], nil
}
return 0, errors.New("Ran over end of data buffer in RuneAt")
return 0, errors.New("ran over end of data buffer in RuneAt")
}

func (bp *Buffer) dataPointForBufferPoint(pt int) int {
Expand Down Expand Up @@ -105,15 +106,15 @@ 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)
//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)
//log.Printf("shuffled %d\n", f)

if bp.PageEnd < bp.Point {
bp.Reframe = true
Expand Down Expand Up @@ -227,6 +228,7 @@ func (bp *Buffer) LineStart(point int) int {
}
sp := point - 1
p, err := bp.RuneAt(sp)
checkErr(err)
if p == '\n' {
sp++
return sp
Expand Down Expand Up @@ -324,7 +326,6 @@ func (bp *Buffer) ColumnForPoint(point int) (column int) {
point = bp.TextSize - 1
}
return point - bp.LineStart(point) + 1

}

// XYForPoint returns the cursor location for a pt in the buffer
Expand Down
5 changes: 2 additions & 3 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ func (e *Editor) up() {
}
func (e *Editor) down() {
e.CurrentBuffer.PointDown()

}
func (e *Editor) lnbegin() {
e.CurrentBuffer.SetPoint(e.CurrentBuffer.LineStart(e.CurrentBuffer.Point))
Expand Down Expand Up @@ -143,7 +142,7 @@ func (e *Editor) gotoline() {
func (e *Editor) insertfile() {
fname := e.getInput("Insert file: ")
if fname != "" {
res := e.InsertFile(fname, false)
res := e.InsertFile(fname, true)
if res {
e.msg("Loaded file %s", fname)
}
Expand Down Expand Up @@ -262,7 +261,7 @@ func (e *Editor) copyCut(cut bool) {

func (e *Editor) paste() {
if len(e.PasteBuffer) <= 0 {
e.msg("PasterBuffer is empty. Nothing to paste.")
e.msg("PasteBuffer is empty. Nothing to paste.")
} else {
e.CurrentBuffer.Insert(e.PasteBuffer)
}
Expand Down
Binary file modified docs/foo
Binary file not shown.
54 changes: 27 additions & 27 deletions editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func checkErr(e error) {
}

const (
version = "kg 1.0, Public Domain, November 2018, Kristofer Younger, No warranty."
version = "kg 1.1, Public Domain, November 2021, Kristofer Younger, No warranty."
nomark = -1
gapchunk = 16 //= 8096
idDefault = 1
Expand Down Expand Up @@ -107,23 +107,19 @@ func (e *Editor) StartEditor(argv []string, argc int) {
e.EventChan <- termbox.PollEvent()
}
}()
for {
select {
case ev := <-e.EventChan:
//// log.Printf("%#v\n", ev)
//// log.Println(">>\n ", time.Now().Unix(), "\n>>")

ok := e.handleEvent(&ev)
if !ok {
return
}
//e.ConsumeMoreEvents()
e.updateDisplay()
termbox.Flush()
// }

// Instead of using for {
// select {
// case ev := <-e.EventChan:

for ev := range e.EventChan {
ok := e.handleEvent(&ev)
if !ok {
return
}
e.updateDisplay()
termbox.Flush()
}
//return
}

// handleEvent
Expand Down Expand Up @@ -173,7 +169,7 @@ func (e *Editor) handleEvent(ev *termbox.Event) bool {
e.updateDisplay()
case termbox.EventMouse:
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
e.msg("Mouse: c %d, r %d ", ev.MouseX, ev.MouseY)
e.msg("Mouse: r %d, c %d ", ev.MouseY, ev.MouseX)
e.SetPointForMouse(ev.MouseX, ev.MouseY)
e.updateDisplay()
case termbox.EventError:
Expand Down Expand Up @@ -245,8 +241,8 @@ func (e *Editor) OnAltKey(ev *termbox.Event) bool {
func (e *Editor) msg(fm string, args ...interface{}) {
e.Msgline = fmt.Sprintf(fm, args...)
e.Msgflag = true
return
}

func (e *Editor) drawString(x, y int, fg, bg termbox.Attribute, msg string) {
for _, c := range msg {
termbox.SetCell(x, y, c, fg, bg)
Expand All @@ -270,12 +266,12 @@ func (e *Editor) Display(wp *Window, shouldDrawCursor bool) {
bp.PageStart = bp.SegStart(bp.LineStart(pt), pt, e.Cols)
}

if bp.Reframe == true || (pt > bp.PageEnd && pt != bp.PageEnd && !(pt >= bp.TextSize)) {
if bp.Reframe || (pt > bp.PageEnd && pt != bp.PageEnd && !(pt >= bp.TextSize)) {
bp.Reframe = false
i := 0
/* Find end of screen plus one. */
bp.PageStart = bp.DownDown(pt, e.Cols)
/* if we scoll to EOF we show 1 blank line at bottom of screen */
/* if we scroll to EOF we show 1 blank line at bottom of screen */
if bp.PageEnd <= bp.PageStart {
bp.PageStart = bp.PageEnd
i = wp.Rows - 1 // 1
Expand All @@ -301,7 +297,7 @@ func (e *Editor) Display(wp *Window, shouldDrawCursor bool) {
}
rch, err := bp.RuneAt(k)
if err != nil {
// log.Println("Error on RuneAt", err)
e.msg("Error on RuneAt", err)
}
if rch != '\r' {
if unicode.IsPrint(rch) || rch == '\t' || rch == '\n' {
Expand Down Expand Up @@ -394,9 +390,13 @@ func (e *Editor) SetPointForMouse(mc, mr int) {
}

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

wp := e.RootWindow
// if mr is modeline or modeline+1, reduce to last wp.Rows
if mr > wp.Rows {
mr = wp.Rows
}
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)
Expand Down Expand Up @@ -428,9 +428,9 @@ func (e *Editor) ModeLine(wp *Window) {
mch = '*'
}
och = lch
temp := fmt.Sprintf("%c%c%c kg: %c%c %s wp(%d,%d)", lch, och, mch, lch, lch,
temp := fmt.Sprintf("%c%c%c kg: %c%c %s L%d wp(%d,%d)", lch, och, mch, lch, lch,
e.GetBufferName(wp.Buffer),
wp.Col, wp.Row)
wp.Buffer.PointRow, wp.Row, wp.Col)
x := 0
y := wp.TopPt + wp.Rows + 1
for _, c := range temp {
Expand Down Expand Up @@ -555,7 +555,7 @@ func (e *Editor) ModifiedBuffers() bool {
var bp *Buffer

for bp = e.RootBuffer; bp != nil; bp = bp.Next {
if bp.modified == true {
if bp.modified {
return true
}
}
Expand All @@ -571,7 +571,7 @@ func (e *Editor) FindBuffer(fname string, cflag bool) *Buffer {
}
bp = bp.Next
}
if cflag != false {
if cflag {
bp = NewBuffer()
/* find the place in the list to insert this buffer */
if e.RootBuffer == nil {
Expand Down Expand Up @@ -613,7 +613,7 @@ func (e *Editor) splitWindow() {
e.CurrentWindow.Rows = ntru
nwp.TopPt = e.CurrentWindow.TopPt + ntru + 2
nwp.Rows = ntrl - 1

/* insert it in the list */
wp2 := e.CurrentWindow.Next
e.CurrentWindow.Next = nwp
Expand Down
13 changes: 7 additions & 6 deletions files.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ func (e *Editor) Save(fname string) bool {
}

// LoadFile foo
func (e *Editor) LoadFile(fname string) bool {
return false
}
// func (e *Editor) LoadFile(fname string) bool {
// return false
// }

// InsertFile reads file into buffer at point
func (e *Editor) InsertFile(fname string, modflag bool) bool {
Expand All @@ -58,12 +58,13 @@ func (e *Editor) InsertFile(fname string, modflag bool) bool {
if err != nil {
e.msg("Failed to read and insert file \"%s\".", fname)
}
if modflag == true {
if !modflag { // just do a load into buffer with no modification
bp.setText(string(dat))
} else {
bp.modified = false
} else { // insert into buffer and mark as modified.
bp.Insert(string(dat))
bp.modified = true
}
bp.modified = true
e.msg("File \"%s\" %d bytes read.", fname, len(dat))
return true
}
15 changes: 15 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module github.com/kristofer/kg

go 1.17

require (
github.com/nsf/termbox-go v1.1.1
github.com/stretchr/testify v1.7.0
)

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)
15 changes: 15 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/nsf/termbox-go v1.1.1 h1:nksUPLCb73Q++DwbYUBEglYBRPZyoXJdrj5L+TkjyZY=
github.com/nsf/termbox-go v1.1.1/go.mod h1:T0cTdVuOwf7pHQNtfhnEbzHbcNyCEcVU4YPpouCbVxo=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 comments on commit ef55b52

Please sign in to comment.