class Vice::Buffer

Attributes

buffer[R]
cursor[RW]
filename[R]
modified[R]
v_scroll[RW]

Public Class Methods

new(vice, filename) click to toggle source
# File lib/vice/buffer.rb, line 8
def initialize(vice, filename)
        @buffer = []
        @marks = {}

        @cursor = Vice::Cursor.new
        @modified = false
        @v_scroll = 0

        if filename
                @filename = filename

                if File.file? filename
                        File.open(filename, 'r') do |f| # TODO: don't assume file exists
                                f.each_line { |line| @buffer.push line.chomp }
                        end
                else
                        vice.alert 'new file'
                        @buffer.push ''
                end
        else
                @buffer.push ''
        end
end

Public Instance Methods

addmark(mark) click to toggle source
# File lib/vice/buffer.rb, line 153
def addmark(mark)
        @marks[mark] = @cursor.clone
end
cols() click to toggle source
# File lib/vice/buffer.rb, line 149
def cols
        @buffer[@cursor.line].length
end
currentline() click to toggle source
# File lib/vice/buffer.rb, line 141
def currentline
        getline @cursor.line
end
cursor_down() click to toggle source
# File lib/vice/buffer.rb, line 57
def cursor_down
        @cursor.line += 1 if @cursor.line < @buffer.length - 1
        cursor_end_of_line
end
cursor_end_of_line() click to toggle source
# File lib/vice/buffer.rb, line 46
def cursor_end_of_line
        # if we're out of bounds, move the cursor to the end of the line
        @cursor.col = @buffer[@cursor.line].length - 1 if @cursor.col >= @buffer[@cursor.line].length
        @cursor.col = 0 if @cursor.col.negative?
end
cursor_left() click to toggle source
# File lib/vice/buffer.rb, line 62
def cursor_left
        @cursor.col -= 1 if @cursor.col.positive?
end
cursor_right() click to toggle source
# File lib/vice/buffer.rb, line 66
def cursor_right
        @cursor.col += 1 if @cursor.col < @buffer[@cursor.line].length - 1
end
cursor_up() click to toggle source
# File lib/vice/buffer.rb, line 52
def cursor_up
        @cursor.line -= 1 if @cursor.line.positive?
        cursor_end_of_line
end
getline(index) click to toggle source
# File lib/vice/buffer.rb, line 134
def getline(index)
        raise 'negative line index' unless index >= 0
        raise 'line index out of bounds' unless index < @buffer.length

        @buffer[index]
end
gotomark(mark) click to toggle source
# File lib/vice/buffer.rb, line 157
def gotomark(mark)
        return false unless @marks[mark]
        @cursor = @marks[mark].clone
        @cursor.line = @buffer.length - 1 if @cursor.line >= @buffer.length
        cursor_end_of_line
        true
end
insert(text) click to toggle source
# File lib/vice/buffer.rb, line 105
def insert(text)
        insertf @cursor.line, @cursor.col, text
end
insertf(index, column, text) click to toggle source
# File lib/vice/buffer.rb, line 94
def insertf(index, column, text)
        raise 'negative line index' unless index >= 0
        raise 'line index out of bounds' unless index < @buffer.length
        raise 'negative column index' unless column >= 0
        raise 'column index out of bounds' unless column <= @buffer[index].length

        @modified = true

        @buffer[index].insert column, text
end
lines() click to toggle source
# File lib/vice/buffer.rb, line 145
def lines
        @buffer.length
end
newline(index) click to toggle source
# File lib/vice/buffer.rb, line 70
def newline(index)
        raise 'negative line index' unless index >= 0

        @modified = true

        # silently append to the end when index out of bounds
        index = @buffer.length if index > @buffer.length

        @buffer.insert index, ''
end
rmchar() click to toggle source
# File lib/vice/buffer.rb, line 120
def rmchar
        rmcharf @cursor.line, @cursor.col
end
rmcharf(index, column) click to toggle source
# File lib/vice/buffer.rb, line 109
def rmcharf(index, column)
        raise 'negative line index' unless index >= 0
        raise 'line index out of bounds' unless index < @buffer.length
        raise 'negative column index' unless column >= 0
        raise 'column index out of bounds' unless column <= @buffer[index].length

        @modified = true

        @buffer[index].slice! column
end
rmline() click to toggle source
# File lib/vice/buffer.rb, line 90
def rmline
        rmlinef @cursor.line
end
rmlinef(index) click to toggle source
# File lib/vice/buffer.rb, line 81
def rmlinef(index)
        raise 'negative line index' unless index >= 0
        raise 'line index out of bounds' unless index < @buffer.length

        @modified = true

        @buffer.delete_at index
end
setline(index, text) click to toggle source
# File lib/vice/buffer.rb, line 124
def setline(index, text)
        raise 'negative line index' unless index >= 0
        raise 'line index out of bounds' unless index < @buffer.length

        @modified = true

        @buffer[index] = text
        cursor_end_of_line
end
write() click to toggle source
# File lib/vice/buffer.rb, line 42
def write
        writef @filename
end
writef(filename) click to toggle source
# File lib/vice/buffer.rb, line 32
def writef(filename)
        @modified = false

        File.open(filename, 'w') do |f|
                f.write @buffer.join "\n"
        end

        @filename = filename
end