Example #1
0
func (e *Editor) NewBufferFromFile(filename string) (*buffer.Buffer, error) {
	fullpath, err := filepath.Abs(filename)
	if err != nil {
		return nil, fmt.Errorf("couldn't determine absolute path: %s", err)
	}
	buf := e.findBufferByFullPath(fullpath)
	if buf != nil {
		return buf, nil
	}

	f, err := os.Open(fullpath)
	if err == os.ErrNotExist {
		// Assume a new file
		e.SetStatus("(New file)")
		buf = buffer.NewEmptyBuffer()
	} else if err != nil {
		e.SetStatus(err.Error())
		return nil, err
	}
	defer f.Close()
	buf, err = buffer.NewBuffer(f)
	if err != nil {
		e.SetStatus(err.Error())
		return nil, err
	}
	buf.Path = fullpath

	buf.Name = e.bufferName(filename)
	e.buffers = append(e.buffers, buf)
	return buf, nil
}
Example #2
0
func NewEditor(filenames []string) *Editor {
	e := new(Editor)
	e.buffers = make([]*buffer.Buffer, 0, 20)
	e.cutBuffers = newCutBuffers()

	for _, filename := range filenames {
		//TODO: Check errors here
		e.NewBufferFromFile(filename)
	}
	if len(e.buffers) == 0 {
		buf := buffer.NewEmptyBuffer()
		buf.Name = e.bufferName("unnamed")
		e.buffers = append(e.buffers, buf)
	}
	e.redraw = make(chan struct{})
	e.views = view.NewTree(view.NewView(e.viewContext(), e.buffers[0], e.redraw))
	e.active = e.views
	e.UIEvents = make(chan termbox.Event, 20)
	e.Commands = make(chan Command, 20)
	return e
}