diff options
author | Petter Rodhelind <petter.rodhelind@gmail.com> | 2018-03-16 12:33:26 +0100 |
---|---|---|
committer | Petter Rodhelind <petter.rodhelind@gmail.com> | 2018-03-16 12:33:26 +0100 |
commit | fea1a1adbb03d657724be9b9994ba6b11dfe625f (patch) | |
tree | 59632674ec570a8f1db6d3154ce49b255ac63b6e /editor | |
parent | 2395485d075f80117fe3ce25ef339bb1ffecf160 (diff) | |
download | poe-fea1a1adbb03d657724be9b9994ba6b11dfe625f.tar.gz poe-fea1a1adbb03d657724be9b9994ba6b11dfe625f.tar.bz2 poe-fea1a1adbb03d657724be9b9994ba6b11dfe625f.zip |
Redesign.
Diffstat (limited to 'editor')
-rw-r--r-- | editor/buffer.go (renamed from editor/text.go) | 365 | ||||
-rw-r--r-- | editor/commands.go | 100 | ||||
-rw-r--r-- | editor/editor.go | 79 | ||||
-rw-r--r-- | editor/file.go | 4 |
4 files changed, 248 insertions, 300 deletions
diff --git a/editor/text.go b/editor/buffer.go index c9a62ce..189b57b 100644 --- a/editor/text.go +++ b/editor/buffer.go @@ -15,8 +15,7 @@ import ( ) const ( - BufferScratch uint8 = iota - BufferFile + BufferFile uint8 = iota BufferDir ) @@ -25,7 +24,7 @@ const ( // Although the underlying buffer is a pure byte slice, Buffer only works with runes and UTF-8. type Buffer struct { buf *gapbuffer.Buffer - file *File + file *file what uint8 dirty bool q0, q1 int // dot/cursor @@ -35,24 +34,27 @@ type Buffer struct { history History // undo/redo stack } -func (t *Buffer) initBuffer() { - if t.buf == nil { - t.buf = &gapbuffer.Buffer{} +// initBuffer initialized a nil buffer into the zero value of buffer. +func (b *Buffer) initBuffer() { + if b.buf == nil { + b.buf = &gapbuffer.Buffer{} } } -func (t *Buffer) NewFile(fn string) { - t.file = &File{name: fn} +// NewFile sets a filename for the buffer. +func (b *Buffer) NewFile(fn string) { + b.file = &file{name: fn} } -func (t *Buffer) ReadFile() error { - t.initBuffer() +// ReadFile reads content of the buffer's filename into the buffer. +func (b *Buffer) ReadFile() error { + b.initBuffer() - if t.file == nil || t.file.read { + if b.file == nil || b.file.read { return nil // silent } - info, err := os.Stat(t.file.name) + info, err := os.Stat(b.file.name) if err != nil { // if the file exists, print why we could not open it // otherwise just close silently @@ -64,12 +66,12 @@ func (t *Buffer) ReadFile() error { // name is a directory; list it's content into the buffer if info.IsDir() { - files, err := ioutil.ReadDir(t.file.name) + files, err := ioutil.ReadDir(b.file.name) if err != nil { return fmt.Errorf("%s", err) } - t.what = BufferDir + b.what = BufferDir // list files in dir for _, f := range files { @@ -77,19 +79,19 @@ func (t *Buffer) ReadFile() error { if f.IsDir() { dirchar = string(filepath.Separator) } - fmt.Fprintf(t.buf, "%s%s\n", f.Name(), dirchar) + fmt.Fprintf(b.buf, "%s%s\n", f.Name(), dirchar) } return nil } // name is a file - fh, err := os.OpenFile(t.file.name, os.O_RDWR|os.O_CREATE, 0644) + fh, err := os.OpenFile(b.file.name, os.O_RDWR|os.O_CREATE, 0644) if err != nil { return fmt.Errorf("%s", err) } defer fh.Close() - if _, err := io.Copy(t.buf, fh); err != nil { + if _, err := io.Copy(b.buf, fh); err != nil { return fmt.Errorf("%s", err) } fh.Seek(0, 0) @@ -98,24 +100,25 @@ func (t *Buffer) ReadFile() error { if _, err := io.Copy(h, fh); err != nil { return fmt.Errorf("%s", err) } - t.file.sha256 = fmt.Sprintf("%x", h.Sum(nil)) + b.file.sha256 = fmt.Sprintf("%x", h.Sum(nil)) - t.file.mtime = info.ModTime() - t.file.read = true + b.file.mtime = info.ModTime() + b.file.read = true - t.what = BufferFile + b.what = BufferFile return nil } -func (t *Buffer) SaveFile() (int, error) { - t.initBuffer() +// SaveFile writes content of buffer to its filename. +func (b *Buffer) SaveFile() (int, error) { + b.initBuffer() - if t.file == nil || t.file.name == "" { + if b.file == nil || b.file.name == "" { return 0, errors.New("no filename") } - if t.what != BufferFile { // can only save file buffers + if b.what != BufferFile { // can only save file buffers return 0, nil } @@ -127,10 +130,10 @@ func (t *Buffer) SaveFile() (int, error) { // namechange = true // to skip sha256 checksum // } - f, err := os.OpenFile(t.file.name, os.O_RDWR|os.O_CREATE, 0644) + f, err := os.OpenFile(b.file.name, os.O_RDWR|os.O_CREATE, 0644) if err != nil { if os.IsExist(err) { - return 0, fmt.Errorf("%s already exists", t.file.name) + return 0, fmt.Errorf("%s already exists", b.file.name) } return 0, err } @@ -147,41 +150,42 @@ func (t *Buffer) SaveFile() (int, error) { // return 0, errors.Errorf("file has been modified outside of poe") // } - n, err := f.WriteAt(t.buf.Bytes(), 0) + n, err := f.WriteAt(b.buf.Bytes(), 0) if err != nil { return 0, err } f.Truncate(int64(n)) f.Sync() - t.file.sha256 = fmt.Sprintf("%x", sha256.Sum256(t.buf.Bytes())) + b.file.sha256 = fmt.Sprintf("%x", sha256.Sum256(b.buf.Bytes())) info, err := f.Stat() if err != nil { return n, err } - t.file.mtime = info.ModTime() + b.file.mtime = info.ModTime() - t.dirty = false + b.dirty = false return n, nil } // Name returns either the file from disk name or empty string if the buffer has no disk counterpart. -func (t *Buffer) Name() string { - if t.file == nil || t.file.name == "" { +func (b *Buffer) Name() string { + if b.file == nil || b.file.name == "" { return "" } - s, _ := filepath.Abs(t.file.name) + s, _ := filepath.Abs(b.file.name) return s } -func (t *Buffer) WorkDir() string { - switch t.what { - case BufferFile, BufferScratch: - return filepath.Dir(t.Name()) +// WorkDir returns the working directory of the underlying file, ie the absolute path to the file with the last part stripped. If file is a directory, its name is returned as is. +func (b *Buffer) WorkDir() string { + switch b.what { + case BufferFile: + return filepath.Dir(b.Name()) case BufferDir: - return t.Name() + return b.Name() default: return "" } @@ -190,97 +194,108 @@ func (t *Buffer) WorkDir() string { // Write implements io.Writer, with the side effect of storing written data into a history stack for undo/redo. // // If dot has content, it will be replaced by an initial deletion before inserting the bytes. -func (t *Buffer) Write(p []byte) (int, error) { - t.initBuffer() +func (b *Buffer) Write(p []byte) (int, error) { + b.initBuffer() // handle replace - if len(t.ReadDot()) > 0 { - t.Delete() + if len(b.ReadDot()) > 0 { + b.Delete() } // do the actual insertion - c := Change{t.q0, HInsert, p} - n, err := t.commit(c) + c := Change{b.q0, HInsert, p} + n, err := b.commit(c) if err != nil { return n, err } - t.history.Do(c) - t.SeekDot(n, 1) // move dot + b.history.Do(c) + b.SeekDot(n, 1) // move dot + if b.what == BufferFile { + b.dirty = true + } return n, nil } // Delete removes current selection in dot. If dot is empty, it selects the previous rune and deletes that. -func (t *Buffer) Delete() (int, error) { - t.initBuffer() +func (b *Buffer) Delete() (int, error) { + b.initBuffer() - if len(t.ReadDot()) == 0 { - t.q0-- - c, _ := t.buf.ByteAt(t.q0) + if len(b.ReadDot()) == 0 { + b.q0-- + c, _ := b.buf.ByteAt(b.q0) for !utf8.RuneStart(c) { - t.q0-- - c, _ = t.buf.ByteAt(t.q0) + b.q0-- + c, _ = b.buf.ByteAt(b.q0) } - if t.q0 < 0 { + if b.q0 < 0 { return 0, nil } } - c := Change{t.q0, HDelete, []byte(t.ReadDot())} - n, err := t.commit(c) + c := Change{b.q0, HDelete, []byte(b.ReadDot())} + n, err := b.commit(c) if err != nil { return n, err } - t.history.Do(c) + b.history.Do(c) + if b.what == BufferFile { + b.dirty = true + } return n, nil } // Len returns the number of bytes in buffer. -func (t *Buffer) Len() int { - t.initBuffer() +func (b *Buffer) Len() int { + b.initBuffer() - return t.buf.Len() + return b.buf.Len() } // String returns the entire text buffer as a string. -func (t *Buffer) String() string { - t.initBuffer() +func (b *Buffer) String() string { + b.initBuffer() + + return string(b.buf.Bytes()) +} - return string(t.buf.Bytes()) +// Dirty returns true if the buffer has changed since last save. +func (b *Buffer) Dirty() bool { + return b.dirty } // ReadRune reads a rune from buffer and advances the internal offset. This could be called in sequence to get all runes from buffer. This populates LastRune(). -func (t *Buffer) ReadRune() (r rune, size int, err error) { - r, size, err = t.ReadRuneAt(t.off) - t.off += size - t.lastRune = r +func (b *Buffer) ReadRune() (r rune, size int, err error) { + r, size, err = b.ReadRuneAt(b.off) + b.off += size + b.lastRune = r return } // UnreadRune returns the rune before the current Seek offset and moves the offset to point to that. This could be called in sequence to scan backwards. -func (t *Buffer) UnreadRune() (r rune, size int, err error) { - t.off-- - r, size, err = t.ReadRuneAt(t.off) - t.off++ +func (b *Buffer) UnreadRune() (r rune, size int, err error) { + b.off-- + r, size, err = b.ReadRuneAt(b.off) + b.off++ if err != nil { return } - t.off -= size + b.off -= size return } // ReadRuneAt returns the rune and its size at offset. If the given offset (in byte count) is not a valid rune, it will try to back up until it finds a valid starting point for a rune and return that one. // // This is basically a Seek(offset) followed by a ReadRune(), but does not affect the internal offset for future reads. -func (t *Buffer) ReadRuneAt(offset int) (r rune, size int, err error) { - t.initBuffer() +func (b *Buffer) ReadRuneAt(offset int) (r rune, size int, err error) { + b.initBuffer() var c byte - c, err = t.buf.ByteAt(offset) + c, err = b.buf.ByteAt(offset) if err != nil { return 0, 0, err } for !utf8.RuneStart(c) { offset-- - c, err = t.buf.ByteAt(offset) + c, err = b.buf.ByteAt(offset) if err != nil { return 0, 0, err } @@ -290,32 +305,32 @@ func (t *Buffer) ReadRuneAt(offset int) (r rune, size int, err error) { return rune(c), 1, nil } - if cap(t.runeBuf) < 4 { - t.runeBuf = make([]byte, 4) // max length of a rune + if cap(b.runeBuf) < 4 { + b.runeBuf = make([]byte, 4) // max length of a rune } - _, err = t.buf.ReadAt(t.runeBuf, offset) + _, err = b.buf.ReadAt(b.runeBuf, offset) if err != nil { return 0, 0, err } - r, n := utf8.DecodeRune(t.runeBuf) + r, n := utf8.DecodeRune(b.runeBuf) return r, n, nil } // LastRune returns the last rune read by ReadRune(). -func (t *Buffer) LastRune() rune { - return t.lastRune +func (b *Buffer) LastRune() rune { + return b.lastRune } // ReadDot returns content of current dot. -func (t *Buffer) ReadDot() string { - t.initBuffer() +func (b *Buffer) ReadDot() string { + b.initBuffer() - if t.q0 == t.q1 { + if b.q0 == b.q1 { return "" } - buf := make([]byte, t.q1-t.q0) - _, err := t.buf.ReadAt(buf, t.q0) + buf := make([]byte, b.q1-b.q0) + _, err := b.buf.ReadAt(buf, b.q0) if err != nil { return "" } @@ -323,47 +338,47 @@ func (t *Buffer) ReadDot() string { } // Dot returns current offsets for dot. -func (t *Buffer) Dot() (int, int) { - return t.q0, t.q1 +func (b *Buffer) Dot() (int, int) { + return b.q0, b.q1 } // Seek implements io.Seeker and sets the internal offset for next ReadRune() or UnreadRune(). If the offset is not a valid rune start, it will backup until it finds one. -func (t *Buffer) Seek(offset, whence int) (int, error) { - t.initBuffer() +func (b *Buffer) Seek(offset, whence int) (int, error) { + b.initBuffer() - t.off = offset + b.off = offset switch whence { case io.SeekStart: - t.off = offset + b.off = offset case io.SeekCurrent: - t.off += offset + b.off += offset case io.SeekEnd: - t.off = t.Len() + offset + b.off = b.Len() + offset default: return 0, errors.New("invalid whence") } - c, _ := t.buf.ByteAt(t.off) + c, _ := b.buf.ByteAt(b.off) for !utf8.RuneStart(c) { - t.off-- - c, _ = t.buf.ByteAt(t.off) + b.off-- + c, _ = b.buf.ByteAt(b.off) } - return t.off, nil + return b.off, nil } // SeekDot sets the dot to a single offset in the text buffer. -func (t *Buffer) SeekDot(offset, whence int) (int, error) { +func (b *Buffer) SeekDot(offset, whence int) (int, error) { switch whence { case io.SeekStart: - q0, _, err := t.SetDot(offset, offset) + q0, _, err := b.SetDot(offset, offset) return q0, err case io.SeekCurrent: - q0, _, err := t.SetDot(t.q0+offset, t.q0+offset) + q0, _, err := b.SetDot(b.q0+offset, b.q0+offset) return q0, err case io.SeekEnd: - q0, _, err := t.SetDot(t.Len()+offset, t.Len()+offset) + q0, _, err := b.SetDot(b.Len()+offset, b.Len()+offset) return q0, err default: return 0, errors.New("invalid whence") @@ -371,59 +386,59 @@ func (t *Buffer) SeekDot(offset, whence int) (int, error) { } // SetDot sets both ends of the dot into an absolute position. It will check the given offsets and adjust them accordingly, so they are not out of bounds or on an invalid rune start. It returns the final offsets. Error is always nil. -func (t *Buffer) SetDot(q0, q1 int) (int, int, error) { - t.initBuffer() +func (b *Buffer) SetDot(q0, q1 int) (int, int, error) { + b.initBuffer() - t.q0, t.q1 = q0, q1 + b.q0, b.q1 = q0, q1 // check out of bounds - if t.q0 < 0 { - t.q0 = 0 + if b.q0 < 0 { + b.q0 = 0 } - if t.q1 < 0 { - t.q1 = 0 + if b.q1 < 0 { + b.q1 = 0 } - if t.q0 >= t.buf.Len() { - t.q0 = t.buf.Len() + if b.q0 >= b.buf.Len() { + b.q0 = b.buf.Len() } - if t.q1 >= t.buf.Len() { - t.q1 = t.buf.Len() + if b.q1 >= b.buf.Len() { + b.q1 = b.buf.Len() } // q0 must never be greater than q1 - if t.q0 > t.q1 { - t.q0 = t.q1 + if b.q0 > b.q1 { + b.q0 = b.q1 } - if t.q1 < t.q0 { - t.q1 = t.q0 + if b.q1 < b.q0 { + b.q1 = b.q0 } // set only to valid rune start var c byte - c, _ = t.buf.ByteAt(t.q0) + c, _ = b.buf.ByteAt(b.q0) for !utf8.RuneStart(c) { - t.q0-- - c, _ = t.buf.ByteAt(t.q0) + b.q0-- + c, _ = b.buf.ByteAt(b.q0) } - c, _ = t.buf.ByteAt(t.q1) + c, _ = b.buf.ByteAt(b.q1) for !utf8.RuneStart(c) { - t.q1-- - c, _ = t.buf.ByteAt(t.q1) + b.q1-- + c, _ = b.buf.ByteAt(b.q1) } - return t.q0, t.q1, nil + return b.q0, b.q1, nil } // ExpandDot expands the current selection in positive or negative offset. A positive offset expands forwards and a negative expands backwards. Q is 0 or 1, either the left or the right end of the dot. -func (t *Buffer) ExpandDot(q, offset int) { +func (b *Buffer) ExpandDot(q, offset int) { if q < 0 || q > 1 { return } if q == 0 { - t.SetDot(t.q0+offset, t.q1) + b.SetDot(b.q0+offset, b.q1) } else { - t.SetDot(t.q0, t.q1+offset) + b.SetDot(b.q0, b.q1+offset) } } @@ -434,8 +449,8 @@ func (t *Buffer) ExpandDot(q, offset int) { // If on newline, select the whole line. // // Otherwise, select word (longest alphanumeric sequence). -func (t *Buffer) Select(offset int) { - offset, _ = t.Seek(offset, io.SeekStart) +func (b *Buffer) Select(offset int) { + offset, _ = b.Seek(offset, io.SeekStart) start, end := offset, offset // space @@ -443,30 +458,30 @@ func (t *Buffer) Select(offset int) { //end += t.NextSpace(end) // word - start -= t.PrevWord(start) - end += t.NextWord(end) + start -= b.PrevWord(start) + end += b.NextWord(end) // return a single char selection if no word was found if start == end { - t.Seek(offset, io.SeekStart) - _, size, _ := t.ReadRune() + b.Seek(offset, io.SeekStart) + _, size, _ := b.ReadRune() end += size } // Set dot - t.SetDot(start, end) + b.SetDot(start, end) } -func (t *Buffer) NextSpace(offset int) (n int) { - offset, _ = t.Seek(offset, io.SeekStart) +func (b *Buffer) NextSpace(offset int) (n int) { + offset, _ = b.Seek(offset, io.SeekStart) - r, size, err := t.ReadRune() + r, size, err := b.ReadRune() if err != nil { return 0 } for !unicode.IsSpace(r) { n += size - r, size, err = t.ReadRune() + r, size, err = b.ReadRune() if err != nil { if err == io.EOF { return n @@ -478,15 +493,15 @@ func (t *Buffer) NextSpace(offset int) (n int) { return n } -func (t *Buffer) PrevSpace(offset int) (n int) { - offset, _ = t.Seek(offset, io.SeekStart) +func (b *Buffer) PrevSpace(offset int) (n int) { + offset, _ = b.Seek(offset, io.SeekStart) - r, size, err := t.ReadRuneAt(offset) + r, size, err := b.ReadRuneAt(offset) if err != nil { return 0 } for !unicode.IsSpace(r) { - r, size, err = t.UnreadRune() + r, size, err = b.UnreadRune() if err != nil { if err == gapbuffer.ErrOutOfRange { return n @@ -502,16 +517,16 @@ func (t *Buffer) PrevSpace(offset int) (n int) { return n } -func (t *Buffer) NextWord(offset int) (n int) { - offset, _ = t.Seek(offset, io.SeekStart) +func (b *Buffer) NextWord(offset int) (n int) { + offset, _ = b.Seek(offset, io.SeekStart) - r, size, err := t.ReadRune() + r, size, err := b.ReadRune() if err != nil { return 0 } for unicode.IsLetter(r) || unicode.IsDigit(r) { n += size - r, size, err = t.ReadRune() + r, size, err = b.ReadRune() if err != nil { if err == io.EOF { return n @@ -523,12 +538,12 @@ func (t *Buffer) NextWord(offset int) (n int) { return n } -func (t *Buffer) PrevWord(offset int) (n int) { - offset, _ = t.Seek(offset, io.SeekStart) +func (b *Buffer) PrevWord(offset int) (n int) { + offset, _ = b.Seek(offset, io.SeekStart) - r, size, _ := t.ReadRuneAt(offset) + r, size, _ := b.ReadRuneAt(offset) for unicode.IsLetter(r) || unicode.IsDigit(r) { - r, size, _ = t.UnreadRune() + r, size, _ = b.UnreadRune() n += size } @@ -540,17 +555,17 @@ func (t *Buffer) PrevWord(offset int) (n int) { } // NextDelim returns number of bytes from given offset up until next delimiter. -func (t *Buffer) NextDelim(delim rune, offset int) (n int) { - t.Seek(offset, io.SeekStart) +func (b *Buffer) NextDelim(delim rune, offset int) (n int) { + b.Seek(offset, io.SeekStart) - r, size, err := t.ReadRune() + r, size, err := b.ReadRune() if err != nil { return 0 } for r != delim { n += size - r, size, err = t.ReadRune() + r, size, err = b.ReadRune() if err != nil { if err == io.EOF { return n @@ -563,16 +578,16 @@ func (t *Buffer) NextDelim(delim rune, offset int) (n int) { } // PrevDelim returns number of bytes from given offset up until next delimiter. -func (t *Buffer) PrevDelim(delim rune, offset int) (n int) { - t.Seek(offset, io.SeekStart) - r, size, err := t.UnreadRune() +func (b *Buffer) PrevDelim(delim rune, offset int) (n int) { + b.Seek(offset, io.SeekStart) + r, size, err := b.UnreadRune() if err != nil { return 0 } n += size for r != delim { - r, size, err = t.UnreadRune() + r, size, err = b.UnreadRune() n += size if err != nil { if err == gapbuffer.ErrOutOfRange { @@ -585,50 +600,50 @@ func (t *Buffer) PrevDelim(delim rune, offset int) (n int) { return n } -func (t *Buffer) Undo() error { - c, err := t.history.Undo() +func (b *Buffer) Undo() error { + c, err := b.history.Undo() if err != nil { return errors.Wrap(err, "undo") } - t.commit(c) + b.commit(c) // highlight text - t.SetDot(c.offset, c.offset+len(c.content)) + b.SetDot(c.offset, c.offset+len(c.content)) return nil } -func (t *Buffer) Redo() error { - c, err := t.history.Redo() +func (b *Buffer) Redo() error { + c, err := b.history.Redo() if err != nil { return errors.Wrap(err, "redo") } - t.commit(c) + b.commit(c) if c.action == HDelete { - t.SetDot(c.offset, c.offset) + b.SetDot(c.offset, c.offset) } else { - t.SetDot(c.offset+len(c.content), c.offset+len(c.content)) + b.SetDot(c.offset+len(c.content), c.offset+len(c.content)) } return nil } -func (t *Buffer) commit(c Change) (int, error) { - t.initBuffer() +func (b *Buffer) commit(c Change) (int, error) { + b.initBuffer() switch c.action { case HInsert: - t.buf.Seek(c.offset) // sync gap buffer - n, err := t.buf.Write([]byte(c.content)) + b.buf.Seek(c.offset) // sync gap buffer + n, err := b.buf.Write([]byte(c.content)) if err != nil { return 0, err } return n, err case HDelete: n := len(c.content) - t.buf.Seek(c.offset + n) // sync gap buffer + b.buf.Seek(c.offset + n) // sync gap buffer for i := n; i > 0; i-- { - t.buf.Delete() // gap buffer deletes one byte at a time + b.buf.Delete() // gap buffer deletes one byte at a time } return n, nil default: diff --git a/editor/commands.go b/editor/commands.go deleted file mode 100644 index e3a77b9..0000000 --- a/editor/commands.go +++ /dev/null @@ -1,100 +0,0 @@ -package editor - -import ( - "fmt" - "strings" -) - -type CommandFunc func(args string) string - -var poecmds map[string]CommandFunc - -func (e *editor) initCommands() { - poecmds = map[string]CommandFunc{ - // "Exit": CmdExit, - // "New": CmdNew, - // "Del": CmdDel, - "Edit": e.CmdEdit, - // "Newcol": CmdNewcol, - } -} - -func (e *editor) Run(input string) { - if input == "" { - return - } - - input = strings.Trim(input, "\t\n ") - - // check poe default commands - cmd := strings.Split(string(input), " ") - if fn, ok := poecmds[cmd[0]]; ok { - fn(strings.TrimPrefix(input, cmd[0])) - return - } - - // Edit shortcuts for external commands and piping - switch input[0] { - case '!', '<', '>', '|': - e.CmdEdit(input) - } - - e.CmdEdit("!" + input) -} - -//func CmdExit(args string) { -// ok := true -// for _, win := range AllWindows() { -// if !win.CanClose() { -// ok = false -// } -// } -// if ok { -// quit <- true -// } -//} -// -//func CmdNewcol(args string) { -// screen.Clear() -// screen.Sync() -// workspace.AddCol() -// CmdNew("") -//} -// -// -// - -func (e *editor) CmdEdit(args string) string { - if len(args) < 2 { - return "" - } - - switch args[0] { - case 'f': - var names []string - for _, buf := range e.buffers { - names = append(names, fmt.Sprintf("%s", buf.Name())) - } - return fmt.Sprintf("buffers:\n%s\n", strings.Join(names, "\n")) - // case '!': - // os.Chdir(CurWin.Dir()) - // cmd := strings.Split(string(args[1:]), " ") - // path, err := exec.LookPath(cmd[0]) - // if err != nil { // path not found, break with silence - // //printMsg("path not found: %s\n", cmd[0]) - // break - // } - // out, err := exec.Command(path, cmd[1:]...).Output() - // if err != nil { - // printMsg("error: %s\n", err) - // break - // } - // // if command produced output, print it - // outstr := string(out) - // if outstr != "" { - // printMsg("%s", outstr) - // } - default: - return "?" - } -} diff --git a/editor/editor.go b/editor/editor.go index 67f0ab3..87b3eff 100644 --- a/editor/editor.go +++ b/editor/editor.go @@ -1,7 +1,11 @@ package editor import ( + "fmt" + "os" + "os/exec" "path/filepath" + "strings" "time" "github.com/prodhe/poe/gapbuffer" @@ -12,49 +16,45 @@ type Editor interface { NewBuffer() (id int64, buf *Buffer) Buffer(id int64) *Buffer Buffers() ([]int64, []*Buffer) - Current() *Buffer LoadBuffers(filenames []string) CloseBuffer(id int64) WorkDir() string Len() int - Run(cmd string) + Edit(bufid int64, cmd string) string } // New returns an empty editor with no buffers loaded. func New() Editor { - e := &editor{} + e := &ed{} e.buffers = map[int64]*Buffer{} e.workdir, _ = filepath.Abs(".") - e.initCommands() return e } -// editor implements Editor. -type editor struct { +// ed implements Editor. +type ed struct { buffers map[int64]*Buffer - current int64 // id ref to current buffer workdir string } // NewBuffer creates an empty buffer and appends it to the editor. Returns the new id and the new buffer. -func (e *editor) NewBuffer() (id int64, buf *Buffer) { +func (e *ed) NewBuffer() (id int64, buf *Buffer) { buf = &Buffer{buf: &gapbuffer.Buffer{}} id = e.genBufferID() e.buffers[id] = buf - e.current = id return id, buf } // Buffer returns the buffer with given index. Nil if id not found. -func (e *editor) Buffer(id int64) *Buffer { +func (e *ed) Buffer(id int64) *Buffer { if _, ok := e.buffers[id]; ok { - e.current = id + return e.buffers[id] } - return e.buffers[id] + return nil } // Buffers returns a slice of IDs and a slice of buffers. -func (e *editor) Buffers() ([]int64, []*Buffer) { +func (e *ed) Buffers() ([]int64, []*Buffer) { ids := make([]int64, 0, len(e.buffers)) bs := make([]*Buffer, 0, len(e.buffers)) for i, b := range e.buffers { @@ -64,23 +64,18 @@ func (e *editor) Buffers() ([]int64, []*Buffer) { return ids, bs } -// Current returns the current buffer. -func (e *editor) Current() *Buffer { - return e.buffers[e.current] -} - // CloseBuffer deletes the given buffer from memory. No warnings. Here be dragons. -func (e *editor) CloseBuffer(id int64) { +func (e *ed) CloseBuffer(id int64) { delete(e.buffers, id) } // Len returns number of buffers currently in the editor. -func (e *editor) Len() int { +func (e *ed) Len() int { return len(e.buffers) } // WorkDir returns the base working directory of the editor. -func (e *editor) WorkDir() string { +func (e *ed) WorkDir() string { if e.workdir == "" { d, _ := filepath.Abs(".") return d @@ -89,7 +84,7 @@ func (e *editor) WorkDir() string { } // LoadBuffers reads files from disk and loads them into windows. Screen need to be initialized. -func (e *editor) LoadBuffers(fns []string) { +func (e *ed) LoadBuffers(fns []string) { // load given filenames and append to buffer list for _, fn := range fns { _, buf := e.NewBuffer() @@ -102,6 +97,44 @@ func (e *editor) LoadBuffers(fns []string) { } } -func (e *editor) genBufferID() int64 { +func (e *ed) genBufferID() int64 { return time.Now().UnixNano() } + +func (e *ed) Edit(bufid int64, args string) string { + if len(args) < 1 { + return "" + } + + if _, ok := e.buffers[bufid]; !ok { + // no such bufid + return "" + } + + switch args[0] { + case 'f': + var names []string + for _, buf := range e.buffers { + names = append(names, fmt.Sprintf("%s", buf.Name())) + } + return fmt.Sprintf("buffers:\n%s", strings.Join(names, "\n")) + case '!': + os.Chdir(e.buffers[bufid].WorkDir()) + cmd := strings.Split(string(args[1:]), " ") + path, err := exec.LookPath(cmd[0]) + if err != nil { // path not found or not executable + //return fmt.Sprintf("cannot execute: %s", cmd[0]) + return "" + } + out, err := exec.Command(path, cmd[1:]...).Output() + if err != nil { + return fmt.Sprintf("error: %s", err) + break + } + outstr := string(out) + return outstr + } + + // no match + return "?" +} diff --git a/editor/file.go b/editor/file.go index e22c279..41657e1 100644 --- a/editor/file.go +++ b/editor/file.go @@ -4,8 +4,8 @@ import ( "time" ) -// File holds information about a file on disk. -type File struct { +// file holds information about a file on disk. +type file struct { name string read bool // true if file has been read mtime time.Time // of file when last read/written |