Class: Seto::Editor

Inherits:
Object
  • Object
show all
Defined in:
lib/seto/editor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(enumerator) ⇒ Editor

Returns a new instance of Editor.



4
5
6
7
8
9
# File 'lib/seto/editor.rb', line 4

def initialize(enumerator)
  @enumerator = enumerator
  @patterns = {}
  @result = []
  @hold_space = []
end

Instance Attribute Details

#current_lineObject (readonly)

Returns the value of attribute current_line.



3
4
5
# File 'lib/seto/editor.rb', line 3

def current_line
  @current_line
end

#line_numberObject (readonly)

Returns the value of attribute line_number.



3
4
5
# File 'lib/seto/editor.rb', line 3

def line_number
  @line_number
end

#resultObject (readonly)

Returns the value of attribute result.



3
4
5
# File 'lib/seto/editor.rb', line 3

def result
  @result
end

Instance Method Details

#append(text) ⇒ Object



15
16
17
# File 'lib/seto/editor.rb', line 15

def append(text)
  @current_line += text
end

#change(text) ⇒ Object



19
20
21
# File 'lib/seto/editor.rb', line 19

def change(text)
  @current_line = text
end

#copyObject



41
42
43
# File 'lib/seto/editor.rb', line 41

def copy
  @result << @current_line.dup
end

#cover?(first, last) ⇒ Boolean

Returns:

  • (Boolean)


100
101
102
103
104
105
# File 'lib/seto/editor.rb', line 100

def cover?(first, last)
  case [first.class, last.class]
  when [Fixnum, Fixnum] then cover1 first, last
  else cover2 first, last
  end
end

#deleteObject



23
24
25
# File 'lib/seto/editor.rb', line 23

def delete
  @current_line = ''
end

#exchangeObject



53
54
55
56
# File 'lib/seto/editor.rb', line 53

def exchange
  @current_line, hs = @hold_space.pop, @current_line
  @hold_space << hs
end

#getObject



45
46
47
# File 'lib/seto/editor.rb', line 45

def get
  @current_line = @hold_space.pop
end

#holdObject



49
50
51
# File 'lib/seto/editor.rb', line 49

def hold
  @hold_space << @current_line.dup
end

#insert(text) ⇒ Object



27
28
29
# File 'lib/seto/editor.rb', line 27

def insert(text)
  @current_line = "#{text}#{@current_line}"
end

#linenoObject



58
59
60
# File 'lib/seto/editor.rb', line 58

def lineno
  Kernel.print @line_number
end

#loadObject



11
12
13
# File 'lib/seto/editor.rb', line 11

def load
  @current_line, @line_number = @enumerator.next
end

#match?(condition) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
94
95
96
97
98
# File 'lib/seto/editor.rb', line 91

def match?(condition)
  case condition
  when Fixnum then condition == @line_number
  when Regexp then condition =~ @current_line
  else
    condition
  end
end

#nextObject



62
63
64
65
# File 'lib/seto/editor.rb', line 62

def next
  copy
  load
end


67
68
69
# File 'lib/seto/editor.rb', line 67

def print
  Kernel.print @current_line
end

#quitObject

Raises:

  • (StopIteration)


71
72
73
74
# File 'lib/seto/editor.rb', line 71

def quit
  copy
  raise StopIteration
end

#read(filename) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/seto/editor.rb', line 76

def read(filename)
  begin
    text = open(filename).read
  rescue
    # ignore
  end
  append text if text
end

#substitute(pattern, replace, flag = nil) ⇒ Object



31
32
33
34
35
# File 'lib/seto/editor.rb', line 31

def substitute(pattern, replace, flag=nil)
  method = :sub!
  method = :gsub! if flag && flag == :g
  @current_line.send method, pattern, replace
end

#transform(pattern, replace) ⇒ Object



37
38
39
# File 'lib/seto/editor.rb', line 37

def transform(pattern, replace)
  @current_line.tr! pattern, replace
end

#write(filename) ⇒ Object



85
86
87
88
89
# File 'lib/seto/editor.rb', line 85

def write(filename)
  open(filename, 'a') do |f|
    f.write @current_line
  end
end