Archive for the ‘GtkSourceView’ Category

File loading and saving in GtkSourceView, finally

Wednesday, July 9th, 2014

Last year GtkSourceView saw a new search and replace API. This year it’s the file loading and saving that has just landed! It is in the continuity of making the gedit source code more reusable for other text editors.

In short, the back-end part of the file loading and saving in gedit has been moved to GtkSourceView, with a new and more flexible API to better wrap the feature. The code has been modified to follow GIO conventions for asynchronous operations, to use GTask internally, and replace the uses of GSettings by properties, among other things.

The file loader auto-detects encodings, newline types and compression type (yes gedit can open gzip files). Invalid characters can also be displayed with their corresponding hexadecimal value, because GtkTextBuffer accepts only valid UTF-8 text. And progress information is reported during the operation.

The API is stateful: the file parameters (encoding etc) are remembered between a file loading and a file saving in a GtkSourceFile object. Unfortunately the new API is still quite low-level. The application must configure the file loader and the file saver, and some errors are returned to the application.

So the front-end and errors handling is still implemented in gedit. Errors and progress information are displayed in an info bar, above the GtkSourceView widget. For some errors, gedit proposes different actions to the user, like choosing another encoding.

API design

If you are a text editor developer, don’t hesitate to have a look at the GtkSourceFile API. The API can still change during this 3.13 development cycle. The notes about the API design are available on bugzilla (there has been four iterations). Here is a summary.

The API is a bit heavy, with the following classes: GtkSourceEncoding, GtkSourceFile, GtkSourceFileLoader and GtkSourceFileSaver. There are good reasons for having two class’ names derived from a verb (“to load” and “to save”), which is generally a hint that those classes should be replaced by a method (i.e. an action). The reasons are partly similar with the new GSubprocessLauncher in GIO: the FileLoader and FileSaver classes are there not only for launching the operation, but also for configuring it with a nicer and more extensible API than one function with a dozen parameters. With the FileLoader and FileSaver, we can add properties, functions and even signals. And the FileLoader and FileSaver classes are quite useful in gedit: when an error occurs and the user clicks on a button in the info bar, the loader or saver object is reconfigured and the operation is relaunched, without the need to keep the initial configuration around. Also, the FileLoader or FileSaver properties are applied to the GtkSourceFile object only on a successful operation, so the GtkSourceFile state is (normally) consistent with the underlying file on the disk, at least at the time of the last load or save done with GtkSourceView.

Possible follow-ups work

A possible follow-up is to make the front-end code more reusable, either by creating higher-level classes in GtkSourceView, or by creating a git submodule, like the libgd (without API stability guarantees in the latter case).

Another possible follow-up is to analyze and improve the performances, which are currently quite bad. For short files it is not visible of course, but opening a very big log file takes more time than with other popular text editors.

There are other possible improvements, like unescaping invalid characters when saving, or avoid blocking the GtkTextBuffer when (auto-)saving a short remote file on a slow network connection.

Changes for gedit plugins

Unfortunately there are some changes in the gedit API for plugins, mainly in GeditDocument. Fortunately the changes are well documented, and most plugins should still work without any modification.

Conclusion

The file loading and saving can quickly become a fairly complex beast. Now this code is available for other GTK+ text editors.

Thanks to Paolo Borelli for the reviews!

TL;DR:

  • In GtkSourceView: 29 files changed, 7909 insertions(+), 3 deletions(-)
  • In gedit: 49 files changed, 1888 insertions(+), 8819 deletions(-)
  • In gedit-plugins: 11 files changed, 22 insertions(+), 31 deletions(-)

This is a 12% code increase in GtkSourceView, and a 6% code decrease in gedit.

PS: As usual, some tests would be more than welcome before the stable version in September.

Making the gedit source code more reusable

Tuesday, May 20th, 2014

On IRC I see some developers working on other text editors based on GtkTextView/GtkSourceView. It is a good thing in my opinion to have specialized text editors. They are easier to use and configure. A specialized application has a potentially better usability. A general-purpose text editor like gedit is of course still needed for languages not covered by a specialized text editor or IDE.

The problem is that creating a new text editor based on GtkSourceView is still quite a lot of work. Instead of reinventing the wheel, the idea is to make the gedit source code more reusable by moving it to GtkSourceView or to a git submodule. This wiki page has all the details, it keeps track of the progress made (see also the RoadMap), more details on the “why?”, and a paragraph on Anjuta.

This blog post was to make developers aware of this initiative. If you are interested, join us on the #gedit channel or the mailing list!

Search and replace behavior for a text editor

Sunday, December 8th, 2013

The search and replace is one of those features that have a wide variety of different implementations and behaviors across applications. The nice thing with my work I did this summer on GtkSourceView, is that you can build whatever behavior you want by using the search and replace framework. And that’s exactly what I did for LaTeXila, which has a new behavior for the replace button.

Search and replace in LaTeXila

Search and replace in LaTeXila

When we replace an occurrence, we generally have the time and we don’t have a train to take (or we are already in the train). And it’s better to see how the content looks like with the replacement text, to see if everything is fine and make adjustments if required. As a consequence, when you click on the replace button, it just replaces the search match without moving to the next occurrence (which can be far away, so we would not be able to see the replaced text).

When you have just replaced an occurrence, no text is selected. If you click a second time on the replace button, it goes to the next occurrence, and selects it, to repeat the process. Nice, isn’t it?

Voilà, that’s all, I just wanted to share this information. It can be interesting for other applications.

The GSoC on GtkSourceView/gedit comes to an end

Saturday, September 21st, 2013

A small update on my GSoC.

The first part took a little longer than expected. But I’m happy with the results, the search and replace API is simple and flexible, there were not a lot of bugs to fix, and gedit users will be happy with the improvements (including the regex search).

I’ve changed a little what was initially planned for the second part of the GSoC. Ideally, it was to rework the color schemes to use CSS, and investigate GtkTextTag to modernize it. For the color schemes, it was not really a high-priority issue for GtkSourceView, writing them in XML works fine. And the GtkTextTag investigation would have been the logical follow-up. But with the remaining time, I have found other interesting and smaller tasks instead.

The past few weeks I worked on various things in other parts of GtkSourceView and gedit: better document the API, fix some bugs, improve the code here and there, and investigate two longer projects: the code folding and making the gedit source code more reusable.

I’ve written a utility class to navigate quickly through a set of GtkTextMarks. It is a cleaner implementation of GtkSourceMarks, and it will be useful for the code folding.

Making the gedit source code more reusable

For making the gedit source code more reusable, I’ve created a git submodule called libgedit. Like the libgd, the API/ABI stability is not guaranteed. The purpose is to try to move as most code as possible in the libgedit, iterate the API if needed until it is good enough to be included in GtkSourceView. So the libgedit would be a staging area between gedit and GtkSourceView. There is a problem though: gedit has an API for its plugins. And if possible, it’s better to not break the plugins… If the code moves from gedit to the libgedit, and then from the libgedit to GtkSourceView, there can be several API breaks. On the other hand, if the code goes directly in GtkSourceView, there is at most one API break (that can be avoided if the gedit functions are simply deprecated, not removed). With the second solution, the risk is to discover problems too late.

Maintaining the gedit plugins API in the libgedit is one problem. There are other problems: it would be nice to share the translations; have an API documentation only for the libgedit (with GTK-Doc, I mean); working with a git submodule is less convenient; having the same namespace (“Gedit”) for gedit and libgedit is a problem when trying to make the code more reusable; a headers nightmare, some headers are private, others are public, and others are split between gedit and libgedit; and so on.

So I think it will be simpler to move the code directly to GtkSourceView. The API must be well thought. The developers must have enough time during the 6-months development cycle to iterate the API if needed. And ideally the API must be tested on other applications than gedit. If it is done slowly but surely, by small steps every cycle, it should be doable.

That’s all folks. Thanks to Ignacio Casal Quinteiro (nacho) and Paolo Borelli (pbor) for their reviews, advices and (sometimes late ;-) ) testing. Thanks to Garrett Regier (gregier) for having well tested the new search and replace in gedit. And thanks to Google and GNOME, for accepting me as a GSoC student.

Regex search in gedit and GtkSourceView, finally

Monday, August 19th, 2013
Regex search in gedit

Regex search in gedit

Although regular expression searches were possible with plugins, it is now available by default in gedit. The search and replace works too, and you can use backreferences (\0, \1, …) in the replacement text.

All this work has been merged upstream, just in time before the freeze.

Reworked search and replace API in GtkSourceView

Behind the scene, the regex search is implemented in GtkSourceView. For most cases, it scales well for big text buffers, since multi-segment matching is done: when a partial match is returned, we take a bigger segment. Read the lengthy implementation overview at the top of gtksourcesearchcontext.c for more details.

Besides the regex search, the API is now more flexible. Roughly speaking, it was initially a one-to-one relationship: only one search was possible at a time in a text buffer. It is now a many-to-many relationship: several searches can execute at the same time in the same text buffer (it can be useful for non-highlighted, background searches). And the search settings can be shared between several buffers.

Next step for my GSoC: rework the GtkSourceView style schemes to use CSS.

Search and replace in gedit and GtkSourceView

Monday, July 15th, 2013

My GSoC on GtkSourceView/gedit/… is coming along nicely. Here is a screenshot of the new search in gedit:

New gedit search

New gedit search

The visible new thing is the “298 of 703″ in the search entry, that shows the position of the current search match, and the total number of occurrences. This is just the tip of the iceberg, as much of the work has been done under the hood. Ignacio (nacho), my mentor, has improved GdTaggedEntry, and I worked on the new API in GtkSourceView, and adapted gedit to use the new API.

Note: almost all my work has already been merged upstream, but not the new search entry.

Search and replace API in GtkSourceView

The new API in GtkSourceView is available. One thing is missing though: regex search.

Why you should use this API:

  • It is asynchronous, i.e. the user interface is normally not blocked when the GtkTextBuffer is scanned. And the asynchronous overhead is small;
  • The search matches are highlighted;
  • You can get the total number of occurrences and the position of a certain occurrence easily;
  • For each search, the buffer is scanned at most once. Once the search occurrences are found, navigating through them doesn’t require to re-scan the buffer, thanks to gtk_text_iter_forward_to_tag_toggle(). As a result, most operations are really fast;
  • The regex search (still to come) will normally do multi-segment matching, also called incremental matching: when a partial match is returned, we take a bigger segment. No need to waste memory space to retrieve the entire buffer contents.

Nice, isn’t it?

Hi, planet GNOME! GSoC on (mainly) GtkSourceView

Sunday, June 23rd, 2013

My blog has just been added to the planet GNOME!

I’m now a GSoC student, working mainly on GtkSourceView (my proposal).

I’m implementing a higher-level asynchronous API for the search and replace. I hope to finish this task in two weeks.
Then: regex search, adapt gedit, modernize the color schemes and ideally GtkTextTag to use CSS.

Search API, some progress

The highlighting of search matches works fine, there was a flickering issue that I fixed.

A property contains the total number of search occurrences. So while the user is typing the text to search, the buffer is scanned to highlight and count the number of matches. What will be really nice is that, once the buffer is entirely scanned, going to the previous/next occurrence will be a O(log n) operation, thanks to gtk_text_iter_forward_to_tag_toggle() (that’s why the matches must be highlighted even if they are not visible on the screen). It was more difficult to implement than always using gtk_text_iter_forward_search() and backward_search() to navigate through the occurrences, but I think it was worth the effort.

If you want to read more details, I’ve written a quite long implementation overview, with the problems to avoid, at the top of gtksourcesearch.c (it will also be useful to future maintainers).

I’ll continue to blog to explain my progress, so stay tuned!

My work on GtkSourceView so far

Tuesday, March 19th, 2013

During the GNOME 3.7 development cycle I’ve been busy working on GtkSourceView, a library used by gedit, Anjuta DevStudio, LaTeXila, and other applications.

The main change is that the completion system has been revamped, mainly under the hood.

Completion: user-visible changes

The only user-visible changes are the bug fixes and the new calltip window sizing, also used for displaying extra pieces of information for proposals. In GtkSourceView 2 there was a function for tuning the calltip sizing, but this function has been removed when porting GtkSourceView to GTK+ 3. Now the sizing works correctly without any tuning function, the window simply fits the natural size of its child widget.

Now the user-comprehensible explanation:

Before

Before

After

After

You see that the main completion window is still quite big when there are only a few proposals. For now it’s not possible to change that, but a more compact completion window should be possible in the future.

Completion: developer-visible changes

The completion system is now better documented. A few functions have been deprecated. The code has been simplified (two classes have been almost completely rewritten). There are also some performance improvements, but it was not really an issue previously. Some statistics on the number of lines:

Before:

$ wc -l gtksourcecompletion*.{c,h} | tail -1
8012 total

After:

$ wc -l gtksourcecompletion*.{c,h} | tail -1
6728 total

Perfection is achieved not when there is nothing more to add, but rather when there is nothing more to take away. (Antoine de Saint-Exupéry)

Other enhancements to GtkSourceView

Paolo and I wrote more unit tests, and code coverage support has been added, to have statistics with nice colors.

Lots of compilation warnings have been fixed, especially for the API documentation. For example, the links to GLib or GTK+ symbols work now in the documentation. Compilation warnings are generally easy to fix, it’s a good way to get involved in a project I think, if there are no easy bugs to fix.

That’s all folks. Thanks to Paolo, Ignacio and Jesse for their advices and reviews!