Re: [gedit-list] Plugins: signal handler when a new line is inserted



Spaces can end up on the ends of lines in all sorts of ways, e.g. copy-and-pasting text or erasing the newline of a line with indentation.  I think an easier, safer, and more reliable approach would be to strip all trailing spaces and tabs when the file is saved.  I think there might even be some plugin that already does this.

Derek

Mildred Ki'Lya said the following on 08/13/2010 05:34 AM:
Hi,


First, if this is a duplicate, sorry, but I couldn't find the message on
the archives.


I want to write a plugin for Gedit that removes trailing spaces while
writing. But that won't remove trailing spaces where the file was not
modified. This is useful for example to avoid too many merge conflicts
when other people leave trailing spaces everywhere. It can also be
meaningful sometimes. In PHP, "?>\n" is not the same as "?> \n". In the
first form, the new line is removed.

This function exists in KWrite, but i'm trying to change to gedit...


How does this function works?
When I press <Return> and a new line is inserted, the previous line is
stripped from its trailing spaces.

How did I implement it?
I connected the signal key-press-event to get the <Return> key event,
and when this happens, I remove all the spaces that are before the
cursor. The signal continues its propagation and the default handler in
GtkSourceView inserts the new line.


Now, with this implementation, I have the following problems:

- I'm not sure I catch all the new line creations as i'm only listening
for the <Return> key. Indeed, while reading the source code of the
default handler in GtkSourceView, I noticed that <KP_Enter> had the same
effects

- There are other things i'm not duplicating. Again, while reading code,
I noticed that sometimes the event was discarded because it was only
relevant to the input method (call to
gtk_text_view_im_context_filter_keypress)

- It messes horribly with indentation. Basically, if I'm on an empty
line with only indentation spaces. The plugin foirst removes the
indentation before GtkSourceView creates the new line. Therefore,
GtkSourceView things there is no indentation. Instead, I'd like to keep
the same indentation level

- My plugin messes with other indentation plugins. For example, it seems
that the python_indentation plugin replaces completely the default
handler instead of adding to it. So, depending of the signal processing
order, the plugin might get executed or not


I wanted to know if there was a way to fix those issues. I was thinking
that GtkSourceView could be modified so it sends an event when a new
line is inserted. Both my plugin and the indentation plugin in effect
could then connect to this signal and modify the document as needed.
This method also keeps all the complicated stuff (key binding and input
method) in GtkSourceView, hidden away from plugin developers (so they
could not remove functionality by messing with it).

Am I imagining things, is there a better solution ?

Thanks.

Mildred
  
_______________________________________________ gedit-list mailing list gedit-list gnome org http://mail.gnome.org/mailman/listinfo/gedit-list


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]