Skip to content

Commit

Permalink
cut/copy/paste working
Browse files Browse the repository at this point in the history
  • Loading branch information
kristofer committed Nov 24, 2018
1 parent d88d832 commit 28e8a45
Show file tree
Hide file tree
Showing 6 changed files with 239 additions and 76 deletions.
8 changes: 8 additions & 0 deletions tkg/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,14 @@ func (bp *Buffer) MoveGap(offset int) int {
return offset
}

// Remove extent runes starting at from point
func (bp *Buffer) Remove(from int, extent int) {
bp.SetPoint(from)
for k := 0; k < extent; k++ {
bp.Delete()
}
}

//LineStart xxx
func (bp *Buffer) LineStart(point int) int {
if point > len(bp.data)-bp.gapLen() {
Expand Down
129 changes: 62 additions & 67 deletions tkg/command.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tkg

import (
"io/ioutil"
"log"
"strconv"
"unicode"
Expand Down Expand Up @@ -46,10 +47,10 @@ func (e *Editor) block() {
e.CurrentBuffer.Mark = e.CurrentBuffer.Point()
}
func (e *Editor) copy() {
//copyCut(false)
e.copyCut(false)
}
func (e *Editor) cut() {
//copyCut(true)
e.copyCut(true)
}
func (e *Editor) resizeTerminal() {
e.CurrentWindow.OneWindow()
Expand Down Expand Up @@ -152,27 +153,24 @@ func (e *Editor) insertfile() {
}

func (e *Editor) readfile() {
// buffer_t *bp;

// temp[0] = '\0';
// int result = getfilename("Find file: ", (char*)temp, NAME_MAX);
// /* int result = getinput("Find file: ", (char*)temp, NAME_MAX, F_CLEAR); */

// if (result) {
// bp = find_buffer(temp, TRUE);
// disassociate_b(curwp); /* we are leaving the old buffer for a new one */
// curbp = bp;
// associate_b2w(curbp, curwp);

// /* load the file if not already loaded */
// if (bp != NULL && bp->b_fname[0] == '\0') {
// if (!load_file(temp)) {
// msg("New file %s", temp);
// }
// strncpy(curbp->b_fname, temp, NAME_MAX);
// curbp->b_fname[NAME_MAX] = '\0'; /* truncate if required */
// }
// }
fname := e.getFilename("Find file: ")
if fname == "" {
e.msg("Nope")
return
}
dat, err := ioutil.ReadFile(fname)
if err != nil {
e.msg("Failed to find file \"%s\".", fname)
return
}

bp := e.FindBuffer(fname, true)
e.CurrentWindow.DisassociateBuffer()
e.CurrentBuffer = bp
e.CurrentWindow.AssociateBuffer(bp)
e.CurrentBuffer.Filename = fname
bp.SetText(string(dat))
}

func (e *Editor) savebuffer() {
Expand Down Expand Up @@ -235,54 +233,51 @@ func (e *Editor) killtoeol() {
}
}

func (e *Editor) copyCut(cut int) {
// char_t *p;
// /* if no mark or point == marker, nothing doing */
// if (curbp->b_mark == NOMARK || curbp->b_point == curbp->b_mark)
// return;
// if (scrap != NULL) {
// free(scrap);
// scrap = NULL;
// }
// if (curbp->b_point < curbp->b_mark) {
// /* point above marker: move gap under point, region = marker - point */
// (void) movegap(curbp, curbp->b_point);
// p = ptr(curbp, curbp->b_point);
// nscrap = curbp->b_mark - curbp->b_point;
// } else {
// /* if point below marker: move gap under marker, region = point - marker */
// (void) movegap(curbp, curbp->b_mark);
// p = ptr(curbp, curbp->b_mark);
// nscrap = curbp->b_point - curbp->b_mark;
// }
// if ((scrap = (char_t*) malloc(nscrap)) == NULL) {
// msg("No more memory available.");
// } else {
// (void) memcpy(scrap, p, nscrap * sizeof (char_t));
// if (cut) {
// curbp->b_egap += nscrap; /* if cut expand gap down */
// curbp->b_point = pos(curbp, curbp->b_egap); /* set point to after region */
// curbp->b_flags |= B_MODIFIED;
// msg("%ld bytes cut.", nscrap);
// } else {
// msg("%ld bytes copied.", nscrap);
// }
// curbp->b_mark = NOMARK; /* unmark */
// }
func (e *Editor) copyCut(cut bool) {
/* if no mark or point == marker, nothing doing */
bp := e.CurrentBuffer
pt := bp.Point()
if bp.Mark == nomark || pt == bp.Mark {
return
}
extent := 0
start := 0
if pt < bp.Mark {
extent = bp.Mark - pt
start = pt
} else { // bp.Point() > bp.Mark
extent = pt - bp.Mark
start = bp.Mark
}
scrap := make([]rune, extent)
l := start
for k := 0; k < extent; k++ {
rch, err := bp.RuneAt(l)
if err != nil {
e.msg("Copy/Cut failed. %s", err)
log.Println("Copy/Cut failed.", err)
}
scrap[k] = rch
log.Println("rune", rch)
l++
}
log.Printf("CopyCut start %d len %d, %#v", start, extent, scrap)
e.PasteBuffer = string(scrap)
if cut == true {
bp.Remove(start, extent)
e.msg("%d characters cut.", extent)
} else {
e.msg("%d bytes copied.", extent)
}
bp.Mark = nomark
}

func (e *Editor) paste() {
// if(curbp->b_flags & B_OVERWRITE)
// return;
// if (nscrap <= 0) {
// msg("Scrap is empty. Nothing to paste.");
// } else if (nscrap < curbp->b_egap - curbp->b_gap || growgap(curbp, nscrap)) {
// curbp->b_point = movegap(curbp, curbp->b_point);
// memcpy(curbp->b_gap, scrap, nscrap * sizeof (char_t));
// curbp->b_gap += nscrap;
// curbp->b_point = pos(curbp, curbp->b_egap);
// curbp->b_flags |= B_MODIFIED;
// }
if len(e.PasteBuffer) <= 0 {
e.msg("PasterBuffer is empty. Nothing to paste.")
} else {
e.CurrentBuffer.Insert(e.PasteBuffer)
}
}

func (e *Editor) showpos() {
Expand Down
7 changes: 1 addition & 6 deletions tkg/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ import (
termbox "github.com/nsf/termbox-go"
)

// var LINES = 1
// var COLS = 10
// var MSGLINE = (LINES - 1)
func checkErr(e error) {
if e != nil {
panic(e)
Expand Down Expand Up @@ -43,10 +40,8 @@ type Editor struct {
// status vars
Done bool /* Quit flag. */
Msgflag bool /* True if msgline should be displayed. */
Nscrap int /* Length of scrap buffer. */
Scrap string /* Allocated scrap buffer. */
PasteBuffer string /* Allocated scrap buffer. */
Msgline string /* Message line input/output buffer. */
Temp string /* Temporary buffer. */
Searchtext string
Replace string
Keymap []keymapt
Expand Down
2 changes: 1 addition & 1 deletion tkg/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (e *Editor) Save(fname string) bool {

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

// InsertFile reads file into buffer at point
Expand Down
Binary file modified tkg/foo.txt
Binary file not shown.
169 changes: 167 additions & 2 deletions tkg/logfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,167 @@
2018/11/23 12:15:41 Start of Log...
2018/11/23 12:15:41 OneWindow rows 21 line 24
2018/11/24 09:46:25 Start of Log...
2018/11/24 09:46:25 OneWindow rows 27 line 30
2018/11/24 09:46:44 rune 32
2018/11/24 09:46:44 rune 102
2018/11/24 09:46:44 rune 111
2018/11/24 09:46:44 rune 111
2018/11/24 09:46:44 rune 76
2018/11/24 09:46:44 rune 111
2018/11/24 09:46:44 rune 114
2018/11/24 09:46:44 rune 101
2018/11/24 09:46:44 rune 109
2018/11/24 09:46:44 rune 32
2018/11/24 09:46:44 rune 105
2018/11/24 09:46:44 rune 112
2018/11/24 09:46:44 rune 115
2018/11/24 09:46:44 rune 117
2018/11/24 09:46:44 rune 109
2018/11/24 09:46:44 rune 32
2018/11/24 09:46:44 rune 100
2018/11/24 09:46:44 rune 111
2018/11/24 09:46:44 rune 108
2018/11/24 09:46:44 rune 111
2018/11/24 09:46:44 rune 114
2018/11/24 09:46:44 rune 32
2018/11/24 09:46:44 rune 115
2018/11/24 09:46:44 rune 105
2018/11/24 09:46:44 rune 116
2018/11/24 09:46:44 rune 32
2018/11/24 09:46:44 rune 97
2018/11/24 09:46:44 rune 109
2018/11/24 09:46:44 rune 101
2018/11/24 09:46:44 rune 116
2018/11/24 09:46:44 rune 44
2018/11/24 09:46:44 rune 10
2018/11/24 09:46:44 rune 99
2018/11/24 09:46:44 rune 111
2018/11/24 09:46:44 rune 110
2018/11/24 09:46:44 rune 115
2018/11/24 09:46:44 rune 101
2018/11/24 09:46:44 rune 99
2018/11/24 09:46:44 rune 116
2018/11/24 09:46:44 rune 101
2018/11/24 09:46:44 rune 116
2018/11/24 09:46:44 rune 117
2018/11/24 09:46:44 rune 114
2018/11/24 09:46:44 rune 32
2018/11/24 09:46:44 rune 97
2018/11/24 09:46:44 rune 100
2018/11/24 09:46:44 rune 105
2018/11/24 09:46:44 rune 112
2018/11/24 09:46:44 rune 105
2018/11/24 09:46:44 rune 115
2018/11/24 09:46:44 rune 99
2018/11/24 09:46:44 rune 105
2018/11/24 09:46:44 rune 110
2018/11/24 09:46:44 rune 103
2018/11/24 09:46:44 rune 32
2018/11/24 09:46:44 rune 101
2018/11/24 09:46:44 rune 108
2018/11/24 09:46:44 rune 105
2018/11/24 09:46:44 rune 116
2018/11/24 09:46:44 rune 44
2018/11/24 09:46:44 rune 10
2018/11/24 09:46:44 rune 115
2018/11/24 09:46:44 rune 101
2018/11/24 09:46:44 rune 100
2018/11/24 09:46:44 rune 32
2018/11/24 09:46:44 rune 100
2018/11/24 09:46:44 rune 111
2018/11/24 09:46:44 rune 32
2018/11/24 09:46:44 rune 101
2018/11/24 09:46:44 rune 105
2018/11/24 09:46:44 rune 117
2018/11/24 09:46:44 rune 115
2018/11/24 09:46:44 rune 109
2018/11/24 09:46:44 rune 111
2018/11/24 09:46:44 rune 100
2018/11/24 09:46:44 rune 32
2018/11/24 09:46:44 rune 116
2018/11/24 09:46:44 rune 101
2018/11/24 09:46:44 rune 109
2018/11/24 09:46:44 rune 112
2018/11/24 09:46:44 rune 111
2018/11/24 09:46:44 rune 114
2018/11/24 09:46:44 rune 32
2018/11/24 09:46:44 rune 105
2018/11/24 09:46:44 rune 110
2018/11/24 09:46:44 rune 99
2018/11/24 09:46:44 rune 105
2018/11/24 09:46:44 rune 100
2018/11/24 09:46:44 rune 105
2018/11/24 09:46:44 rune 100
2018/11/24 09:46:44 rune 117
2018/11/24 09:46:44 rune 110
2018/11/24 09:46:44 rune 116
2018/11/24 09:46:44 rune 32
2018/11/24 09:46:44 rune 117
2018/11/24 09:46:44 rune 116
2018/11/24 09:46:44 rune 10
2018/11/24 09:46:44 rune 108
2018/11/24 09:46:44 rune 97
2018/11/24 09:46:44 rune 98
2018/11/24 09:46:44 rune 111
2018/11/24 09:46:44 rune 114
2018/11/24 09:46:44 rune 101
2018/11/24 09:46:44 rune 32
2018/11/24 09:46:44 rune 101
2018/11/24 09:46:44 rune 116
2018/11/24 09:46:44 rune 32
2018/11/24 09:46:44 rune 100
2018/11/24 09:46:44 rune 111
2018/11/24 09:46:44 rune 108
2018/11/24 09:46:44 rune 111
2018/11/24 09:46:44 rune 114
2018/11/24 09:46:44 rune 101
2018/11/24 09:46:44 rune 32
2018/11/24 09:46:44 rune 109
2018/11/24 09:46:44 rune 97
2018/11/24 09:46:44 rune 103
2018/11/24 09:46:44 rune 110
2018/11/24 09:46:44 rune 97
2018/11/24 09:46:44 rune 32
2018/11/24 09:46:44 rune 97
2018/11/24 09:46:44 rune 108
2018/11/24 09:46:44 rune 105
2018/11/24 09:46:44 rune 113
2018/11/24 09:46:44 rune 117
2018/11/24 09:46:44 rune 97
2018/11/24 09:46:44 rune 46
2018/11/24 09:46:44 rune 10
2018/11/24 09:46:44 rune 111
2018/11/24 09:46:44 rune 114
2018/11/24 09:46:44 rune 101
2018/11/24 09:46:44 rune 32
2018/11/24 09:46:44 rune 109
2018/11/24 09:46:44 rune 97
2018/11/24 09:46:44 rune 103
2018/11/24 09:46:44 rune 110
2018/11/24 09:46:44 rune 97
2018/11/24 09:46:44 rune 32
2018/11/24 09:46:44 rune 97
2018/11/24 09:46:44 rune 108
2018/11/24 09:46:44 rune 105
2018/11/24 09:46:44 rune 113
2018/11/24 09:46:44 rune 117
2018/11/24 09:46:44 rune 97
2018/11/24 09:46:44 rune 46
2018/11/24 09:46:44 rune 32
2018/11/24 09:46:44 rune 10
2018/11/24 09:46:44 CopyCut start 125 len 147, []int32{32, 102, 111, 111, 76, 111, 114, 101, 109, 32, 105, 112, 115, 117, 109, 32, 100, 111, 108, 111, 114, 32, 115, 105, 116, 32, 97, 109, 101, 116, 44, 10, 99, 111, 110, 115, 101, 99, 116, 101, 116, 117, 114, 32, 97, 100, 105, 112, 105, 115, 99, 105, 110, 103, 32, 101, 108, 105, 116, 44, 10, 115, 101, 100, 32, 100, 111, 32, 101, 105, 117, 115, 109, 111, 100, 32, 116, 101, 109, 112, 111, 114, 32, 105, 110, 99, 105, 100, 105, 100, 117, 110, 116, 32, 117, 116, 10, 108, 97, 98, 111, 114, 101, 32, 101, 116, 32, 100, 111, 108, 111, 114, 101, 32, 109, 97, 103, 110, 97, 32, 97, 108, 105, 113, 117, 97, 46, 10, 111, 114, 101, 32, 109, 97, 103, 110, 97, 32, 97, 108, 105, 113, 117, 97, 46, 32, 10}
2018/11/24 09:46:48 reframing!
2018/11/24 09:46:52 >>Called from tioga.co/kristofer/kg/tkg.(*Buffer).SetPoint
>> /Volumes/DeVere/kristofer/Projects/go/src/tioga.co/kristofer/kg/tkg/buffer.go Ln# 132
2018/11/24 09:46:52 >>Setting Point to EOB 1363 1363
2018/11/24 09:46:52 reframing!
2018/11/24 09:46:52 >>Called from tioga.co/kristofer/kg/tkg.(*Buffer).SetPoint
>> /Volumes/DeVere/kristofer/Projects/go/src/tioga.co/kristofer/kg/tkg/buffer.go Ln# 132
2018/11/24 09:46:52 >>Setting Point to EOB 1363 1363
2018/11/24 09:46:52 reframing!
2018/11/24 09:46:52 >>Called from tioga.co/kristofer/kg/tkg.(*Buffer).SetPoint
>> /Volumes/DeVere/kristofer/Projects/go/src/tioga.co/kristofer/kg/tkg/buffer.go Ln# 132
2018/11/24 09:46:52 >>Setting Point to EOB 1363 1363
2018/11/24 09:46:52 reframing!
2018/11/24 09:46:53 >>Called from tioga.co/kristofer/kg/tkg.(*Buffer).SetPoint
>> /Volumes/DeVere/kristofer/Projects/go/src/tioga.co/kristofer/kg/tkg/buffer.go Ln# 132
2018/11/24 09:46:53 >>Setting Point to EOB 1363 1363
2018/11/24 09:46:53 reframing!

0 comments on commit 28e8a45

Please sign in to comment.