ruby-gnome2 : problem with signal "insert-text"

O

oxman

Hello,

i have a problem with the signal "insert-text", the signal was
call before the text is insert, so i can't color it, how i can
have a signal just after the text have been insert ?

thanks
oxman.
 
M

Michael C. Libby

Hello,

i have a problem with the signal "insert-text", the signal was
call before the text is insert, so i can't color it, how i can
have a signal just after the text have been insert ?

Are you sure? insert-text should be getting called for every text
insertion--and it can't really be called until an insertion occurs.
If you are typing, every letter typed would cause this signal to emit.

Perhaps a code sample would help illustrate the issue.

You might try connecting to "changed" signal and then test the
buffer for words you want to color and then apply the coloring
at that time.

You could do the same with "insert-text" signal, and keep track
of letters typed that *could* be a word you'd want to color, and
then when the word is complete go back and color the whole word.
thanks
oxman.

- Michael
 
P

Peter Stuifzand

Hello,

i have a problem with the signal "insert-text", the signal was
call before the text is insert, so i can't color it, how i can
have a signal just after the text have been insert ?

thanks
oxman.

Hello oxman,

You should use:

widget.signal_connect_after("nsert-text") do
# code ....
end
 
O

oxman

Thanks.
But the time-out to call 'insert-text' is "very long".

buffer.signal_connect_after("insert-text") do |a,first_char,text,length|
if (text == "#")
first_char.offset = first_char.offset - 1
last_char = buffer.get_iter_at_offset(first_char.offset)
last_char.forward_to_line_end

buffer.apply_tag(buffer.tag_table.lookup("blue"),first_char, last_char)
end
end

The text is colored one or two seconds after i write the '#' character.

How i can have a signal immediately called after "insert-text" ?
 
O

oxman

Humm, after many test, I see my code is executed immediately.
But the effect of my code isn't immediat.
I think 'buffer' or 'textview' have a refresh period.
How i can force the refresh for 'buffer' or 'textview' ?
 
M

Michael C. Libby

--tKW2IUtsqtDRztdT
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

Humm, after many test, I see my code is executed immediately.
But the effect of my code isn't immediat.
I think 'buffer' or 'textview' have a refresh period.
How i can force the refresh for 'buffer' or 'textview' ?

I've never noticed any sort refresh issues with the text
widgets, but...

You can always make your application do all pending Gtk events
with a function like:

def update_window
while Gtk.event_pending? do
Gtk.main_iteration
end
end

I should note that while this works well for me on Linux,
it does cause the program to hang on Windows.

- Michael

--tKW2IUtsqtDRztdT
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFB6QUG4ClW9KMwqnMRApVRAJkBg54I4jPoWSjhM6/yoGZXGmqzpQCgj9uN
hzhno/F2DsB4MTU9FfgSb8w=
=CsFe
-----END PGP SIGNATURE-----

--tKW2IUtsqtDRztdT--
 
M

Michael C. Libby

--jq0ap7NbKX2Kqbes
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

It don't work.
The text is colored after one or two second :(

Do you have a short working example that demonstrates
the issue? Hard to debug without code. :)

-Michael
--jq0ap7NbKX2Kqbes
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFB6SFA4ClW9KMwqnMRAuanAJ9d5NUUD30EyaBQLdddpd3Uvmu0wACcDRmj
3zxzQR3cQrx8xNe4VKUyB9k=
=tRUL
-----END PGP SIGNATURE-----

--jq0ap7NbKX2Kqbes--
 
O

oxman

The code :

#!/usr/bin/ruby1.8
require 'gtk2'
Gtk.init
window = Gtk::Window.new
window.signal_connect("destroy") {
puts "destroy"
Gtk.main_quit
}
textviewer = Gtk::TextView.new
window.add(textviewer)
window.set_default_size(600,400)
window.show_all
buffer = textviewer.buffer
buffer.create_tag("blue", {"foreground" => "blue"})
buffer.signal_connect_after('insert-text') do |a,first_char,text,length|
if (text == '#')
first_char.offset -= 1
last_char = buffer.get_iter_at_offset(first_char.offset)
last_char.forward_to_line_end
buffer.apply_tag(buffer.tag_table.lookup("blue"),
first_char, last_char)
update_window
end
end

def update_window
while Gtk.events_pending? do
Gtk.main_iteration
end
end
Gtk.main
 
M

Michael C. Libby

--Qxx1br4bt0+wmkIi
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

The code :
=20
#!/usr/bin/ruby1.8
require 'gtk2'
Gtk.init
window =3D Gtk::Window.new
window.signal_connect("destroy") {
puts "destroy"
Gtk.main_quit
}
textviewer =3D Gtk::TextView.new
window.add(textviewer)
window.set_default_size(600,400)
window.show_all
buffer =3D textviewer.buffer
buffer.create_tag("blue", {"foreground" =3D> "blue"})
buffer.signal_connect_after('insert-text') do |a,first_char,text,length|
if (text =3D=3D '#')
first_char.offset -=3D 1
last_char =3D buffer.get_iter_at_offset(first_char.offset)
last_char.forward_to_line_end
buffer.apply_tag(buffer.tag_table.lookup("blue"),=20
first_char, last_char)
update_window
end
end
=20
def update_window
while Gtk.events_pending? do
Gtk.main_iteration
end
end
Gtk.main

I guess in this case update_window isn't going to have an effect because
no Gtk events are actually pending.

I can only see this problem when I type # and then wait. If I continue to
type or quickly switch lines using the arrow keys then it changes the color
right away. So the problem is that it's not detecting the need for an=20
update very quickly. I think this has to do with the priority of redraws.

I did find information about redraw priority here:
http://ruby-gnome2.sourceforge.jp/hiki.cgi?Gtk::TextView#PRIORITY_VALID=
ATE

I have not found any way to adjust the priority of redraws in the Ruby-
GNOME2 docs or in the regular GNOME API docs. But I thought I'd mention it
in case it help you find more info.

You might look at GtkSourceView as well... if not for using it, to see=20
how it handles this problem.

-Michael

--Qxx1br4bt0+wmkIi
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFB6US44ClW9KMwqnMRAsK0AJ0Wh85zdwkhhu/sd3pi7Xw8vmMpZgCcDs1q
HwhAGIp9BACYp12KX/ZJyBw=
=UeW2
-----END PGP SIGNATURE-----

--Qxx1br4bt0+wmkIi--
 
J

Joao Pedrosa

Hi,

The code :

Do you get any improvement or idea with this code:

require 'gtk2'
Gtk.init
window = Gtk::Window.new
window.signal_connect("destroy") { Gtk.main_quit }
textviewer = Gtk::TextView.new
window.add(textviewer)
window.set_default_size(600,400)
window.show_all
buffer = textviewer.buffer
buffer.create_tag("blue", {"foreground" => "blue"})
buffer.signal_connect('insert-text') do |a,iter,text,length|
Gtk.timeout_add(10){
iter = buffer.start_iter
while a = iter.forward_search('#', Gtk::TextIter::SEARCH_TEXT_ONLY) do
iter, = a
iter2 = buffer.get_iter_at_offset(iter.offset)
iter2.forward_to_line_end
buffer.apply_tag(buffer.tag_table.lookup("blue"), iter, iter2)
iter.forward_line
p a
end
false
}
end
Gtk.main

It seems that after the first # is colored I get some improvement, but
I haven't come up with a best approach yet, though I'm interested in
this as well. :)

Cheers,
Joao
 
J

Joao Pedrosa

Hi,


I think I have found a hack. Check this out:

require 'gtk2'
Gtk.init
window = Gtk::Window.new
window.signal_connect("destroy") { Gtk.main_quit }
textviewer = Gtk::TextView.new
window.add(textviewer)
window.set_default_size(600,400)
window.show_all
buffer = textviewer.buffer
buffer.create_tag("blue", {"foreground" => "blue"})
buffer.signal_connect('insert-text') do |a,iter,text,length|
offs = iter.offset
Gtk.timeout_add(10){
iter = buffer.get_iter_at_offset(offs)
iter.forward_to_line_end
limit_iter = buffer.get_iter_at_offset(iter.offset)
iter.line_offset = 0
if a = iter.forward_search('#', Gtk::TextIter::SEARCH_TEXT_ONLY,
limit_iter) or text == '#'
iter, = a if a
2.times{
buffer.apply_tag(buffer.tag_table.lookup("blue"), iter, limit_iter)
}
end
false
}
end
Gtk.main

Cheers,
Joao
 
O

oxman

Thanks.
But i don't think it's a good way.
Because i don't want color just #.
I want a full syntax color for ruby language.
(and other language in futur...)

So full color each x ms i don't think it's a good way.

I can't believe I'm the first man with this problem.
 
O

oxman

This :
2.times{
buffer.apply_tag(buffer.tag_table.lookup("blue"), iter,
limit_iter)
}

Perfect.
Thanks :)
 
J

Joao Pedrosa

Hi,

This :
2.times{
buffer.apply_tag(buffer.tag_table.lookup("blue"), iter,
limit_iter)
}

Perfect.
Thanks :)

No problem. :)

I just created another one, a little better:

require 'gtk2'
Gtk.init
window = Gtk::Window.new
window.signal_connect("destroy") { Gtk.main_quit }
textviewer = Gtk::TextView.new
window.add(textviewer)
window.set_default_size(600,400)
window.show_all
buffer = textviewer.buffer
buffer.create_tag("blue", {"foreground" => "blue"})
buffer.signal_connect_after('insert-text') do |a,iter,text,length|
orig_iter = buffer.get_iter_at_offset(iter.offset - 1)
iter.forward_to_line_end
limit_iter = buffer.get_iter_at_offset(iter.offset)
iter.line_offset = 0
iter.offset = iter.offset - 1
if a = iter.forward_search('#', Gtk::TextIter::SEARCH_TEXT_ONLY,
limit_iter) or text == '#'
if a
iter, = a
else
iter = orig_iter
end
2.times{
buffer.apply_tag(buffer.tag_table.lookup("blue"), iter, limit_iter)
}
end
end
Gtk.main

Cheers,
Joao
 
O

oxman

I prefer a more generic way, such as :
buffer.signal_connect_after('insert-text') do |a,iter,text,length|
color(buffer, iter)
end

def color (buffer, iter)
first_char = iter
first_char.offset -= iter.line_offset
last_char = buffer.get_iter_at_offset(first_char.offset)
last_char.forward_to_line_end
line_text = first_char.get_visible_text(last_char)
f = File.open("ruby.color")
f.each_line do |pattern|
pattern.strip!
if (match = Regexp.new(pattern).match(line_text))
length = match.pre_match.length
start_iter =
buffer.get_iter_at_offset(first_char.offset + length)
length = match.post_match.length
end_iter =
buffer.get_iter_at_offset(last_char.offset - length)
2.times
{buffer.apply_tag(buffer.tag_table.lookup("blue"), start_iter, end_iter)}
end
end
end


sentinel% cat ruby.color
#.+$
if
else
 
J

Joao Pedrosa

Hi,

I prefer a more generic way, such as :
buffer.signal_connect_after('insert-text') do |a,iter,text,length|
color(buffer, iter)
end

def color (buffer, iter)
first_char = iter
first_char.offset -= iter.line_offset
last_char = buffer.get_iter_at_offset(first_char.offset)
last_char.forward_to_line_end
line_text = first_char.get_visible_text(last_char)
f = File.open("ruby.color")
f.each_line do |pattern|
pattern.strip!
if (match = Regexp.new(pattern).match(line_text))
length = match.pre_match.length
start_iter =
buffer.get_iter_at_offset(first_char.offset + length)
length = match.post_match.length
end_iter =
buffer.get_iter_at_offset(last_char.offset - length)
2.times
{buffer.apply_tag(buffer.tag_table.lookup("blue"), start_iter, end_iter)}
end
end
end

sentinel% cat ruby.color
#.+$
if
else

Cool. :)

I don't know how I'm going to use it yet. I like the syntax highlight
provided by AEditor. I'm using it already, but only for the entire
buffer when loading or on demand. Your Regexes might be great as well.

It was cool to think about it in your example. I'm not in a hurry to
get this working, though. What I have is good enough for me (a simple
working editor)... But, I still need to provide full syntax
highlighting... hehe.

If GtkSourceText was available on Windows, then I would use it... But
it's not. :)

Cheers,
Joao
 
J

Joao Pedrosa

Hi,

So you're the developper of aeditor ?
Or developper of an other text editor ?

I develop my own editor. See a screenshot of it:
http://www.geocities.com/joaopedrosa/ruby_editor.html

It's not professional and I do not want to release it to the public in
the near future, so I'm free to keep it broken until I want. hehehe
:)

I use just the Ruby Lexer of AEditor (Simon's) to give some color and
life to mine. :)

Cheers,
Joao
 
O

oxman

Hum good :)
I will add you in my friend list :p
I develop my own editor two.
I want a textmate-like (textmate from mac).

My editor will be release.. in... two years ;-)

I have start since 3 days :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,440
Latest member
YoungBorel

Latest Threads

Top