Category Archives: Developers

Kate in 4.11

Another release cycle gone, and the KDE Software Compilation 4.11 is out in the open (well, for quite some time already), and with that it is time to talk about what changed in Kate the last half year since the 4.10 release. Besides the usual bug fixing (~50 bugs since 4.10), the following sections present some major improvements and features of Kate in 4.11.

Python Plugins

Since KDE 4.10, Kate features the Python »Pâté« plugin. This plugin basically wraps the API of all Kate application interfaces. Therewith, together with the Python KDE (PyKDE) bindings you can develop full-featured Kate plugins in Python. Being relatively new, there are quite some Python plugins available already now (see this blog, this blog, or this blog).

@Python community: You can interpret this as an invitation for writing lots of cool addons for the Kate text editor in python. All contributions are usually very much welcome!

Vi Input Mode

Enabling the vi input mode turns Kate into a full-fleged vim compatible editor: modal editing exactly like in vim. This mode is especially suited for vim users who want a fully KDE integrated text editor with all the beloved vim features. Started as GSoC project in 2010, the vi input mode evolved over the years to become a very mature alternative to vim itself. However, there is always room for improvements. The good news is, that Kate Part’s vi input mode gained a lot of attention thanks to Simon, mostly in the form of small “papercuts” that fixed small bugs and annoyances and made behaviour more compatible with Vim, including:

  • Yank highlighting, which helps you to see what text you just yanked;
  • “Yank to clipboard” (courtesy of Fabian Kosmale!);
  • The “last edit” markers, “.”, “[" and "]“;
  • Recursive vs non-recursive mappings, for if you want to e.g. map “j” to “gj” …
  • … plus plenty of fixes to gj and gk;
  • and countless other small tweaks to things like the “inner block” text object; cursor position after paste; fixes to ctrl-a/x (add to/ subtract from number under cursor); making more motions available in Visual Mode and more motions counted, etc!

One fairly major change is the introduction of an experimental Emulated Vim Command Bar. This, as you will probably have guessed, is a replacement for the current Search dialog that behaves more like Vim’s, allowing one to e.g. insert the contents of registers via ctrl-r; dismiss via ctrl-c/ ctrl-[ as well as ESC; work properly with both forward (“/”) and backward (“?”) incremental searches; use smart case for case-sensitivity; allow the use of Vim-style regex’s etc. As an extension to Vim, I’ve also added the ability to auto-complete words from the document via ctrl-space: not that useful for searching, but very useful for when you want to do a search+replace command (command mode (“:”) did not make it into 4.11, alas).

In 4.11, it is disabled by default as it is not yet ready for primetime, but may be enabled by setting the hidden config option “Vi Input Mode Emulate Command Bar” to “true”. It is much more powerful and featureful in current master (see the blog here!) and will likely be the default in 4.12.

New Text Folding

Back in KDE SC 4.8, Kate got a new code folding implementation as part of the yearly GSoC project. Compared to the previous implementation, the amount of bugs indeed reduced, but during the 4.9 and 4.10 release it became apparent, that the code folding was still not where it should be. Maybe writing a good code folding implementation was also a bit too much in terms of a newby developer in a GSoC project.

So Christoph sat down and wrote it completely new from scratch, this time very clean and simple. And as a new feature, the text folding is now per view, so the folded parts can be different in each view of a document. And as another feature for those who do not use text folding at all: Not using text folding means you have zero overhead, no matter how large the file. Besides that, code folding in Python works now better than ever before, in fact, it should be bug-free.

Passive Notifications & KTextEditor Interfaces

Passive notifications are meant to provide a non-intrusive way showing notifications (replacing KPassivePopup). Used e.g. by the data recovery, these notifications were also introduced for the search & replace feature (most of this is already available in KDE SC 4.10.3). In KDE SC 4.11, the interface for showing these passive notifications got officially included into the KTextEditor interfaces, meaning that applications like Kile or KDevelop can show notifications as well. (Issues in KDE 4.11.0 should be fixed for 4.11.1).

Next to the KTextEditor::MessageInterface, Kate has a so-called TextHintInterface since year 2003, but it seems its implementation never got finished. For 4.11, Sven Brauch stepped up and finally fixed the text hint interface. So showing tool tips for text under the mouse (section Text tooltips) is as easy as it was never before:

Saving Files

When saving files, Kate uses the class KSaveFile (in Qt5 available as QSaveFile thanks to David Faure). In short, to avoid data loss, KSaveFile saves the file to a temporary file in the same directory as the target file, and on successful write finally moves the temporary file to the target filename. If the folder of the file does not allow creating new files, KSaveFile automatically falls back to writing the file directly. In this case, data loss could happen if e.g. the system crashes in this moment, but saving the file would otherwise be not possible at all.

What Comes Next?

Announced just a few days ago, the KDE community prepares for KDE Frameworks 5 and much more: The KDE libraries and the KDE Plasma Workspace are now in long term support mode, meaning that bugs will get fixed for another two years. That means that the KTextEditor interfaces are frozen as well, there will not be any feature updates at all in the KTextEditor interfaces itself. Since Kate Part and the Kate Application are developed outside of kdelibs, Kate Part and Kate will most certainly see another feature release. Especially, since there are already a lot of improvements for the vi input mode in the pipe for Kate in KDE SC 4.12.

After that, we plan to port Kate to Qt5 and the new frameworks 5 libraries. We already added a lot of “KDE 5″ todo-markers to the Kate source code, meaning that we will probably work on a 5 port rather sooner than later. We will address this schedule in a separate post later this year, so stay tuned!

Intel Threading Building Blocks Scalable Allocator & Valgrind

Hi,

if you ever use the TBB (Intel Threading Building Blocks) allocator to overwrite malloc/free/* and want to use Valgrind for leak checking and fail, here is the simple trick to get it working:

valgrind --soname-synonyms=somalloc=\*tbbmalloc\* <your-application-here>

I missed that hint in the Valgrind documentation for my first tries ;)

Btw., the scalable allocator from TBB is a really BIG improvement over the normal system allocator on current Linux & Windows machines if you allocate mostly fixed size small object, like what happens if you heavily use STL data structures like std::map/set that are implemented as trees and you have other stuff like DOM/AST like data structures (even in the single threaded case, for which it just saves a LOT of memory).

Ramblings about compilers…

In my job I work on binary and source level analysis software running on Linux and Windows. One of my tasks is to maintain the build farm and compile environment, therefore I am responsible for keeping care of the compilers and libraries we use (like the beloved Qt, congratulations for the nice 5.1 release, btw.).

For Linux, we normally use the GNU C/C++ compiler. That works out very well since years. We have hit compiler bugs only a few times and there was always already some patch-release out in the wild that fixed our issues. In addition the GCC developers brought a constant stream of improvements in the area of C and C++ standard compliance. I am more than happy with the GCC quality.

I did some brief experiments with LLVM/Clang two years ago and was not that impressed in the stability of the C++ support (especially of the fresh libc++ STL implementation) but even for a such young project, it was fast as light to get issues fixed, like a crash in the unordered containers (see Bug 10278, only some hours from report to fix, even if it was only a trivial size variable used uninitialized in the libc++ implementation). I guess today LLVM would perform ok to compile our software (including Qt), still I will stick to GCC, given we use some libraries that normally only get love for that compiler on Linux (at least at the moment).

But now, after my experiences with open source compilers, lets take a look at the wonderful world of Windows.

I know for Windows GCC variants are available (like the 32 bit MinGW-builds shipped with some Qt library variant) and some experimental work on some LLVM able to compile applications using Windows SDK headers is ongoing. Still, the most common way on Windows is to use the Visual Studio compiler and given we always have some libraries of 3rd-parties around that we need to use and that are Visual Studio compiled the Microsoft compiler is more or less unavoidable.

Now, lets take a look of this fabulous compiler you get there. At the moment two major Version are of interest, the 2010 and 2012 variant of Visual Studio.

As I started to evaluate Visual Studio 2010 in 2010, I was immediately hit by its nice 64 bit optimizer bug. Not a single Qt application would start up without immediate segfault… More about this here and even in the Qt bugtracker, especially the answers from the support are interesting, it seems to be forbidden to return a object per value from a function ;)

From report to “hotfix” it has taken some months, that means with the release version of 2010, you couldn’t build any optimized x64 application using Qt (and I guess most other evil libraries using object on the stack that got misaligned by the optimizer) for some months.

Afterwards I was that adventurous to install the SP1 to get the included further compiler fixes. Unfortunately that meant I got all my x64 compilers deleted, but no problem, just close to a month later (until which you need to reinstall your Visual Studio/SDK without SP1 and reapply you hotfix) you got again some fix that reinstalls the compilers that SP1 will remove. Really, you will need to install the 2010 + SP1 + then a fix that restores the compilers SP1 removes if you want to have a working VS 2010 Express for x64 (or stay without SP1 and use the above hotfix for the x64 compiler).

For Visual Studio 2012, which I started to eval months ago, you will get the same story, but reversed. Now we have some nice and nasty x86 optimizer bug. That bug is reported since long see here, it leads to problems for software that use libicu, see the matching icu bug. But given the simple code pattern that can make it occur, it may hit other parts, too. Until now, no fix, only some “will be fixed in next version”…

For me that means, I need to stick with 2010 for x86 and with 2012 for x64, given I want to use the pre-build Qt binaries and not to dive into the “compile qt on windows” fun once more. I would like 2012 for x86, too, to benefit from the C++11 feature improvements and to not have to support two different compiler variants, but I guess that will have to wait, seems its not that easy to build some working x86 compiler that can do optimizations.

Therefore, really, +1 to the compiler developers of the GCC and LLVM project. Its amazing what they have archived!
And +1 for the improvements in the C99 and C++ area in Visual Studio, too. Still I really would be more than happy to get a fix for the x86 optimizer issue in VS 2012. Even no report that it is really fixed in 2013 until now, but I am not sure that was really tried, I didn’t try it myself because I am lazy and must blog instead ;=)

QUndoStack versus Kate’s Undo System

I’m currently using QUndoStack in a project of mine. A QUndoStack contains a list of QUndoCommands which then can be executed with QUndoStack::undo() and QUndoStack:.redo().

Now I thought when deleting an instance of the class Node, I can just hock the creation of the respective UndoDeleteNode class (derived from QUndoCommand) into the destructor, like this:

class Node {
public:
  // ...
  ~Node() {
    undoStack()->push(new UndoDeleteNode(this));
  }
};

class UndoDeleteNode : public QUndoCommand {
public:
  // ...
  void undo() {
    // create node again
  }
  void redo() {
    // delete node again
  }
};

Now the problem with this code is, that executing UndoDeleteNode::redo() will delete the node through the undo/redo system. In this case, the destructor ~Node() will create another UndoDeleteNode item. So what I was looking for a way to only create the undo item when the undo/redo system is not active, i.e.:

class Node {
public:
  // ...
  ~Node() {
    if (!undoStack()->undoOrRedoRunning()) {
      undoStack()->push(new UndoDeleteNode(this));
    } 
  }
};

I found QUndoStack::isActive(), but isActive() does something completely different. In fact, it looks as if there is no way to tell that the QUndoStack is currently operating. This finally also gets clear by reading the API docs of QUndoStack::push(QUndoCommand* cmd):

[...] Executes cmd by calling its redo() function. [...]

In other words, each QUndoCommand you push on the stack is executed immediately. This obviously is a design decision: Following this paradigm, you should not just call “delete node;” Instead, you should simply create a UndoDeleteNode() instance and push it to the stack. The undo command is then immediately executed by calling redo().

This design has the advantage that developers are forced to go this route. Following this paradigm, you very easily get macro support, since all actions are undo/redo items. This is cool.

However, what I dislike about this approach is the fact that it seems you loose the nice API to simply delete the node. You cannot write “delete node;”. Instead, you have to have some helper class such as a Document, that provides a Document::deleteNode(Node* node) function. This function then just creates a new UndoDeleteNode and pushes it to the stack.

Is this how the QUndoStack is supposed to be used? Does it imply that the destructor should be protected, and UndoDeleteNode must be a friend class of Node, to avoid other classes from just calling delete Node?

In Kate, we indeed go the other way: We have a KateUndoManager::isActive(), which indicates whether the edit actions should be tracked by the undo manager or not…

I’m not yet convinced that the approach of QUndoStack is ultimately cool. To me it looks like I’m forced into some design decision I didn’t want to take. Maybe I’m missing something?