Class: GtkApp::TextBuffer
- Inherits:
-
Gtk::TextBuffer
- Object
- Gtk::TextBuffer
- GtkApp::TextBuffer
- Defined in:
- lib/gtk_app/text_buffer.rb
Constant Summary collapse
- DEFAULT_LANG =
"en_US"
- DEFAULT_TAGS =
%w[bold italic strikethrough underline error spell_error]
Instance Attribute Summary collapse
-
#redo_stack ⇒ Object
readonly
Returns the value of attribute redo_stack.
-
#spell_check ⇒ Object
readonly
Returns the value of attribute spell_check.
-
#undo_stack ⇒ Object
readonly
Returns the value of attribute undo_stack.
Instance Method Summary collapse
- #check_spelling(word = nil, s_iter = nil, e_iter = nil) ⇒ Object
-
#clear(tag_name, s_iter, e_iter) ⇒ Object
Remove all tags of a given name from from one Gtk::TextIter to another.
-
#clear_all(s_iter, e_iter) ⇒ Object
Remove all Gtk::TextTag’s from one Gtk::TextIter to another.
-
#clear_selection(*tag_names) ⇒ Object
Remove all occurrences of a Gtk::TextTag in the given selection range.
-
#find(string) ⇒ Object
Locate text in selection or the entire buffer.
-
#format(tag_name, s_iter, e_iter) ⇒ Object
This is a small wrapper around the Gtk::TextBuffer#apply_tag method.
-
#format_selection(tag_name) ⇒ Object
Format text in the current selection range with a Gtk::TextTag identified by the given name.
-
#initialize(tag_table = nil, options = {}) ⇒ TextBuffer
constructor
A new instance of TextBuffer.
-
#redo ⇒ Object
Pop the last action off the redo stack and apply the changes.
- #replace(string, s_iter, e_iter) ⇒ Object
-
#spelling_errors? ⇒ Boolean
Does the Gtk::TextTagTable contain any ‘spell_error’ tags?.
-
#undo ⇒ Object
Pop the last action off the undo stack and rewind changes.
- #word_at_cursor ⇒ Object
- #word_bounds ⇒ Object
Constructor Details
#initialize(tag_table = nil, options = {}) ⇒ TextBuffer
Returns a new instance of TextBuffer.
11 12 13 14 15 16 17 |
# File 'lib/gtk_app/text_buffer.rb', line 11 def initialize(tag_table=nil, ={}) super(tag_table) @undo_stack, @redo_stack = [], [] @spell_check = Aspell.new([:lang] || DEFAULT_LANG) setup_signals end |
Instance Attribute Details
#redo_stack ⇒ Object (readonly)
Returns the value of attribute redo_stack.
6 7 8 |
# File 'lib/gtk_app/text_buffer.rb', line 6 def redo_stack @redo_stack end |
#spell_check ⇒ Object (readonly)
Returns the value of attribute spell_check.
5 6 7 |
# File 'lib/gtk_app/text_buffer.rb', line 5 def spell_check @spell_check end |
#undo_stack ⇒ Object (readonly)
Returns the value of attribute undo_stack.
6 7 8 |
# File 'lib/gtk_app/text_buffer.rb', line 6 def undo_stack @undo_stack end |
Instance Method Details
#check_spelling(word = nil, s_iter = nil, e_iter = nil) ⇒ Object
69 70 71 72 73 74 75 76 |
# File 'lib/gtk_app/text_buffer.rb', line 69 def check_spelling(word=nil, s_iter=nil, e_iter=nil) if word.nil? text.gsub(/[\w\']+/) do |w| check_spelling(w); end elsif !@spell_check.check(word) s, e = start_iter.forward_search(word, Gtk::TextIter::SEARCH_TEXT_ONLY, nil) format(:spell_error, s, e) end end |
#clear(tag_name, s_iter, e_iter) ⇒ Object
Remove all tags of a given name from from one Gtk::TextIter to another.
135 136 137 |
# File 'lib/gtk_app/text_buffer.rb', line 135 def clear(tag_name, s_iter, e_iter) remove_tag(tag_name.to_s, s_iter, e_iter) end |
#clear_all(s_iter, e_iter) ⇒ Object
Remove all Gtk::TextTag’s from one Gtk::TextIter to another.
142 143 144 |
# File 'lib/gtk_app/text_buffer.rb', line 142 def clear_all(s_iter, e_iter) (s_iter, e_iter) end |
#clear_selection(*tag_names) ⇒ Object
Remove all occurrences of a Gtk::TextTag in the given selection range.
121 122 123 124 125 126 127 128 129 130 |
# File 'lib/gtk_app/text_buffer.rb', line 121 def clear_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| clear(tag_name, s_iter, e_iter) } end end end |
#find(string) ⇒ Object
Locate text in selection or the entire buffer. If found, a Gtk::TextMark is returned. Else, nil.
88 89 90 91 92 93 |
# File 'lib/gtk_app/text_buffer.rb', line 88 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.
114 115 116 |
# File 'lib/gtk_app/text_buffer.rb', line 114 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.
106 107 108 109 |
# File 'lib/gtk_app/text_buffer.rb', line 106 def format_selection(tag_name) s_iter, e_iter, text_selected = selection_bounds format(tag_name, s_iter, e_iter) if text_selected end |
#redo ⇒ Object
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.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/gtk_app/text_buffer.rb', line 40 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
95 96 97 98 99 100 101 |
# File 'lib/gtk_app/text_buffer.rb', line 95 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?
81 82 83 |
# File 'lib/gtk_app/text_buffer.rb', line 81 def spelling_errors? !tag_table.lookup('spell_error').nil? end |
#undo ⇒ Object
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.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/gtk_app/text_buffer.rb', line 22 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 |
#word_at_cursor ⇒ Object
56 57 58 |
# File 'lib/gtk_app/text_buffer.rb', line 56 def word_at_cursor get_text(*word_bounds).strip end |
#word_bounds ⇒ Object
60 61 62 63 64 65 66 67 |
# File 'lib/gtk_app/text_buffer.rb', line 60 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 |