Class: Ruco::Editor

Inherits:
Object show all
Defined in:
lib/ruco/editor.rb,
lib/ruco/editor/history.rb,
lib/ruco/editor/line_numbers.rb

Defined Under Namespace

Modules: History, LineNumbers

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, options) ⇒ Editor

Returns a new instance of Editor.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ruco/editor.rb', line 13

def initialize(file, options)
  @file = file
  @options = options

  # check for size (10000 lines * 100 chars should be enough for everybody !?)
  if File.exist?(@file) and File.size(@file) > (1024 * 1024)
    raise "#{@file} is larger than 1MB, did you really want to open that with Ruco?"
  end

  content = (File.exist?(@file) ? File.read(@file) : '')
  content.tabs_to_spaces! if @options[:convert_tabs]

  # cleanup newline formats
  @newline = content.match("\r\n|\r|\n")
  @newline = (@newline ? @newline[0] : "\n")
  content.gsub!(/\r\n?/,"\n")

  @saved_content = content
  @text_area = EditorArea.new(content, @options)
  restore_session
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



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

def file
  @file
end

Instance Method Details

#find(text) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/ruco/editor.rb', line 35

def find(text)
  move(:relative, 0,0) # reset selection
  return unless start = text_area.content.index(text, text_area.index_for_position+1)
  finish = start + text.size
  move(:to_index, finish)
  selecting{ move(:to_index, start) }
  true
end

#modified?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/ruco/editor.rb', line 44

def modified?
  @saved_content != text_area.content
end

#saveObject



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ruco/editor.rb', line 48

def save
  lines = text_area.send(:lines)
  lines.each(&:rstrip!) if @options[:remove_trailing_whitespace_on_save]
  lines << '' if @options[:blank_line_before_eof_on_save] and lines.last.to_s !~ /^\s*$/
  content = lines * @newline

  File.open(@file,'w'){|f| f.write(content) }
  @saved_content = content.gsub(/\r?\n/, "\n")

  true
rescue Object => e
  e.message
end

#store_sessionObject



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

def store_session
  session_store.set(@file, text_area.state.slice(:position, :screen_position))
end