Ejemplo n.º 1
0
func (a *Action) insert(buf *Buffer) {
	var data_chunk []byte
	nline := 0
	offset := a.Cursor.Boffset
	line := a.Cursor.Line
	utils.IterLines(a.Data, func(data []byte) {
		if data[0] == '\n' {
			buf.numBytes++
			if offset < line.Len() {
				// a case where we insert at the middle of the
				// line, need to save that chunk for later
				// insertion at the end of the operation
				data_chunk = line.Data[offset:]
				line.Data = line.Data[:offset]
			}
			// insert a line
			buf.InsertLine(a.Lines[nline], line)
			line = a.Lines[nline]
			nline++
			offset = 0
		} else {
			buf.numBytes += len(data)
			// insert a chunk of data
			line.Data = utils.InsertBytes(line.Data, offset, data)
			offset += len(data)
		}
	})
	if data_chunk != nil {
		line.Data = append(line.Data, data_chunk...)
	}
	buf.Emit(BufferEvent{Type: BufferEventInsert, Action: a})
}
Ejemplo n.º 2
0
func (a *Action) delete(buf *Buffer) {
	nline := 0
	offset := a.Cursor.Boffset
	line := a.Cursor.Line
	utils.IterLines(a.Data, func(data []byte) {
		if data[0] == '\n' {
			buf.numBytes--
			// append the contents of the deleted line the current line
			line.Data = append(line.Data, a.Lines[nline].Data...)
			// delete a line
			buf.DeleteLine(a.Lines[nline])
			nline++
		} else {
			buf.numBytes -= len(data)
			// delete a chunk of data
			copy(line.Data[offset:], line.Data[offset+len(data):])
			line.Data = line.Data[:line.Len()-len(data)]
		}
	})
	buf.Emit(BufferEvent{Type: BufferEventDelete, Action: a})
}