Class: Ruco::Editor

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

Defined Under Namespace

Modules: Colors, History, LineNumbers

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, options) ⇒ Editor

Returns a new instance of Editor.



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

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.binary_read(@file) : '')
  @options[:language] ||= LanguageSniffer.detect(@file, :content => content).language
  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)
  @history = @text_area.history
  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

#historyObject (readonly)

Returns the value of attribute history.



5
6
7
# File 'lib/ruco/editor.rb', line 5

def history
  @history
end

Instance Method Details

#contentObject



73
74
75
# File 'lib/ruco/editor.rb', line 73

def content
  text_area.content.freeze # no modifications allowed
end

#find(text) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ruco/editor.rb', line 38

def find(text)
  move(:relative, 0,0) # reset selection
  start = text_area.content.index(text, text_area.index_for_position+1)
  return unless start

  # select the found word
  finish = start + text.size
  move(:to_index, finish)
  selecting{ move(:to_index, start) }

  true
end

#modified?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/ruco/editor.rb', line 51

def modified?
  @saved_content != text_area.content
end

#saveObject



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ruco/editor.rb', line 55

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,'wb'){|f| f.write(content) }
  @saved_content = content.gsub(/\r?\n/, "\n")

  true
rescue Object => e
  e.message
end

#store_sessionObject



69
70
71
# File 'lib/ruco/editor.rb', line 69

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