Class: Gtk::TextBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/gtk_app/ext/text_buffer.rb

Direct Known Subclasses

GtkApp::TextBuffer

Constant Summary collapse

DEFAULT_LANG =
"en_US"
DEFAULT_TAGS =
%w[bold italic strikethrough underline error spell_error]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag_table = nil, options = {}) ⇒ TextBuffer

Returns a new instance of TextBuffer.



20
21
22
23
24
25
26
27
# File 'lib/gtk_app/ext/text_buffer.rb', line 20

def initialize(tag_table=nil, options={})
  super(tag_table)
  @undo_stack, @redo_stack = [], []
  @spell_check = Aspell.new(options[:lang] || DEFAULT_LANG)
  setup_default_tags
  setup_spell_check_marks
  setup_signals
end

Instance Attribute Details

#insert_endObject

Returns the value of attribute insert_end.



15
16
17
# File 'lib/gtk_app/ext/text_buffer.rb', line 15

def insert_end
  @insert_end
end

#insert_startObject

Returns the value of attribute insert_start.



13
14
15
# File 'lib/gtk_app/ext/text_buffer.rb', line 13

def insert_start
  @insert_start
end

#redo_stackObject

Returns the value of attribute redo_stack.



11
12
13
# File 'lib/gtk_app/ext/text_buffer.rb', line 11

def redo_stack
  @redo_stack
end

#spell_checkObject

Returns the value of attribute spell_check.



7
8
9
# File 'lib/gtk_app/ext/text_buffer.rb', line 7

def spell_check
  @spell_check
end

#undo_stackObject

Returns the value of attribute undo_stack.



9
10
11
# File 'lib/gtk_app/ext/text_buffer.rb', line 9

def undo_stack
  @undo_stack
end

Instance Method Details

#check_spelling(s_iter = nil, e_iter = nil) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/gtk_app/ext/text_buffer.rb', line 83

def check_spelling(s_iter=nil, e_iter=nil)
  s_iter = bounds.first if s_iter.nil?
  e_iter = bounds.last  if e_iter.nil?

  e_iter.forward_word_end if e_iter.inside_word?

  unless s_iter.starts_word?
    if s_iter.inside_word? || s_iter.ends_word?
      s_iter.backward_word_start
    elsif s_iter.forward_word_end
      s_iter.backward_word_start
    end
  end

  cursor = get_iter_at_offset(cursor_position)
  precursor = cursor.clone
  precursor.backward_char
  tag = tag_table.lookup('spell_error')
  has_error = cursor.has_tag?(tag) || precursor.has_tag?(tag)

  unformat(:spell_error, s_iter, e_iter)

  word_start = s_iter.clone
  while (word_start <=> e_iter) < 0 do
    word_end = word_start.clone
    word_end.forward_word_end

    word = get_text(word_start, word_end)
    if !has_error && word =~ /[A-Za-z]/ && !@spell_check.check(word)
      # puts "[#{word}]"
      format(:spell_error, word_start, word_end)
    end

    # => meow point to the beginning of the next word.
    word_end.forward_word_end
    word_end.backward_word_start

    break if word_start == word_end

    word_start = word_end.clone
  end
end

#find(string) ⇒ Object

Locate text in selection or the entire buffer. If found, a Gtk::TextMark is returned. Else, nil.

Parameters:

  • string (String)

    Text to search.



137
138
139
140
141
142
# File 'lib/gtk_app/ext/text_buffer.rb', line 137

def find(string)
  s_iter, e_iter, text_selected = selection_bounds
  s_iter = start_iter unless text_selected
  s_iter, e_iter = s_iter.forward_search(string, Gtk::TextIter::SEARCH_TEXT_ONLY, e_iter)
  s_iter ? create_mark(nil, s_iter, false) : nil
end

#format(tag_name, s_iter, e_iter) ⇒ Object

This is a small wrapper around the Gtk::TextBuffer#apply_tag method. It allows the Gtk::TextTag name to be passed as a symbol.

Parameters:

  • []

    tag_name

  • s_iter (Gtk::TextIter)
  • e_iter (Gtk::TextIter)


170
171
172
# File 'lib/gtk_app/ext/text_buffer.rb', line 170

def format(tag_name, s_iter, e_iter)
  apply_tag(tag_name.to_s, s_iter, e_iter)
end

#format_selection(tag_name) ⇒ Object

Format text in the current selection range with a Gtk::TextTag identified by the given name.

Parameters:

  • tag_name (String)


159
160
161
162
# File 'lib/gtk_app/ext/text_buffer.rb', line 159

def format_selection(tag_name)
  s_iter, e_iter, text_selected = selection_bounds
  format(tag_name, s_iter, e_iter) if text_selected
end

#redoObject

Pop the last action off the redo stack and apply the changes. If and action was performed, the cursor is placed at the actions starting Gtk::TextIter.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/gtk_app/ext/text_buffer.rb', line 50

def redo
  if @redo_stack.empty?
    Gdk.beep
  else
    action = @redo_stack.pop
    s_iter = get_iter_at_offset(action[1])
    e_iter = get_iter_at_offset(action[2])
    case action[0]
    when :insert then insert(s_iter, action[3])
    when :delete then delete(s_iter, e_iter)
    end
    @undo_stack.push(action)
    place_cursor(s_iter)
  end
end

#replace(string, s_iter, e_iter) ⇒ Object

Parameters:

  • string (String)

    Text to replace the selection with.

  • s_iter (Gtk::TextIter)
  • e_iter (Gtk::TextIter)


147
148
149
150
151
152
153
# File 'lib/gtk_app/ext/text_buffer.rb', line 147

def replace(string, s_iter, e_iter)
  begin_user_action
  delete(s_iter, e_iter)
  insert(s_iter, string)
  end_user_action
  place_cursor(s_iter)
end

#spelling_errors?Boolean

Does the Gtk::TextTagTable contain any ‘spell_error’ tags?

Returns:

  • (Boolean)


129
130
131
# File 'lib/gtk_app/ext/text_buffer.rb', line 129

def spelling_errors?
  !tag_table.lookup('spell_error').nil?
end

#undoObject

Pop the last action off the undo stack and rewind changes. If an action was performed, the cursor is placed at the actions starting Gtk::TextIter.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/gtk_app/ext/text_buffer.rb', line 32

def undo
  if @undo_stack.empty?
    Gdk.beep
  else
    action = @undo_stack.pop
    s_iter = get_iter_at_offset(action[1])
    case action[0]
    when :insert then delete(s_iter, get_iter_at_offset(action[2]))
    when :delete then insert(s_iter, action[3])
    end
    @redo_stack.push(action)
    place_cursor(s_iter)
  end
end

#unformat(tag_name, s_iter, e_iter) ⇒ Object

Remove all tags of a given name from from one Gtk::TextIter to another.

Parameters:

  • []

    tag_name

  • s_iter (Gtk::TextIter)
  • e_iter (Gtk::TextIter)


180
181
182
# File 'lib/gtk_app/ext/text_buffer.rb', line 180

def unformat(tag_name, s_iter, e_iter)
  remove_tag(tag_name.to_s, s_iter, e_iter)
end

#unformat_all(s_iter, e_iter) ⇒ Object

Remove all Gtk::TextTag’s from one Gtk::TextIter to another.

Parameters:

  • s_iter (Gtk::TextIter)
  • e_iter (Gtk::TextIter)


203
204
205
# File 'lib/gtk_app/ext/text_buffer.rb', line 203

def unformat_all(s_iter, e_iter)
  remove_all_tags(s_iter, e_iter)
end

#unformat_selection(*tag_names) ⇒ Object

Remove all occurrences of a Gtk::TextTag in the given selection range.



187
188
189
190
191
192
193
194
195
196
# File 'lib/gtk_app/ext/text_buffer.rb', line 187

def unformat_selection(*tag_names)
  s_iter, e_iter, text_selected = selection_bounds
  if text_selected
    if tag_names.empty?
      clear_all(s_iter, e_iter)
    else
      tag_names.each { |tag_name| unformat(tag_name, s_iter, e_iter) }
    end
  end
end

#word_at_cursorString

Retrieve the word at the current cursor position

Returns:

  • (String)

    The word.



68
69
70
# File 'lib/gtk_app/ext/text_buffer.rb', line 68

def word_at_cursor
  get_text(*word_bounds).strip
end

#word_boundsArray

Determine the boudaries of the word at the current cursor position.

Returns:

  • (Array)

    The start and end iter.



74
75
76
77
78
79
80
81
# File 'lib/gtk_app/ext/text_buffer.rb', line 74

def word_bounds
  iter = get_iter_at_offset(cursor_position)
  s_iter, e_iter = iter.clone, iter.clone
  s_iter.backward_word_start unless s_iter.starts_word?
  e_iter.forward_word_end unless e_iter.ends_word?

  [s_iter, e_iter]
end