|
|
Subscribe / Log in / New account

Wayland support (and more) for Emacs

By Jonathan Corbet
January 28, 2021

LCA
Jeffrey Walsh started off his 2021 linux.conf.au presentation with a statement that, while 2020 was not the greatest year ever, there were still some good things that happened; one of those was the Emacs 27.1 release. This major update brought a number of welcome new features, but also led to yet another discussion on the future of Emacs. With that starting point, Walsh launched into a fast-moving look at the history of Emacs, why users still care about it, what changes are coming, and (especially) what was involved in moving Emacs away from the X window system and making it work with the Wayland compositor.

There were a number of good things to be found in the 27.1 release, which was a "huge jump" in functionality. Perhaps at the top of the list is support for the HarfBuzz library, which brings improved text-rendering support in multiple languages — and the support for color emoji that no self-respecting 2020s application can be without. Portable dumping was finally added, leading to faster startup and less system-dependent code. Emacs also now supports a tab-based interface, something that "had been asked-for forever", Walsh said.

After that release, though, "the navel-gazing set in" with lots of questions about how Emacs could be restored to its one-time popularity. This is something that happens every year, he said, but the discussion was perhaps deeper this time around. The problem with this kind of discussion [Jeffrey Walsh] in the Emacs community is that any resulting action can take years to come to fruition; there tends to be a lot of resistance to change from "old hands" (in whose company Walsh counted himself). But if people present changes in the form of code, they can eventually gain traction.

Some ideas that were discussed are not going to happen; these include replacing elisp with JavaScript or some other "more modern" language. Similarly, porting Emacs's C code to Rust is not something to hold one's breath for. Other desired changes are more "socially easy", but are hard technically; these include making elisp faster so that more of Emacs could be written in Lisp rather than C. Another is rewriting the Unix graphical interface, which is fragile; small changes can break obscure platforms in surprising ways.

Taking a step back, Walsh asked why people bother to use Emacs in the first place. In his own case, it's simply too late to change — and he has tried a couple of times. It is his toolbox at this point. In any case, whenever some other editor demonstrates an interesting feature, it gets ported to Emacs eventually. He also likes the fact that, unlike other editors, Emacs treats C code as C, rather than as some poor-developer's C++.

It is one thing to ask why a developer like Walsh would use Emacs, but it is another to ask why he would want Emacs to work with Wayland, and why he would want to work on that project. The answer was that Wayland works well on his box; he finds it to be "less clunky" than X at this point. Emacs was the only application he had that still used X; fixing that was his opportunity to get the X libraries off his system entirely.

The Wayland port

Emacs, he said, is not really a text editor; it's a Lisp environment that has been ported to Unix, which just happens to have a useful editor built into it. Without Lisp, he said, there is no Emacs. Everything that happens in Emacs goes through Lisp; for example, if one types the letter "x", Emacs will (usually) run:

    (self-insert-command "x")

to insert that letter into the current buffer. Everything can be reconfigured at almost all levels. Among other things, that means that Emacs has no concept of a "plugin" because none is needed; add-on features are equivalent to the core of Emacs. If one looks in the source repository, 60% of what's there is Lisp, while only 15% is C. Amusingly, 16% is changelog containing 35 years of development history. The C code includes the Lisp interpreter, the Athena toolkit, and the core "redisplay" code that manages the screen.

That redisplay code, he said, is C code that was written by Lisp developers while many of the core C standards were still being developed. It is characterized by "heavy macro use" that is trying to hide a lot of history. It is efficient code, but also complex; it's one of the trickiest parts of the entire code base. But this is where changes need to be made to bring Emacs to Wayland.

The problem is complicated by the fact that Emacs was never designed to be a graphical user interface; it has always been a terminal application. To work with the X window system, Emacs pretends that an X window is, in fact, a terminal. As is its usual practice, Emacs deals with X at the lowest level, processing as much X interaction through Lisp code as possible.

At some point, the X redisplay code was ported to the GTK toolkit, but that port is a sort of false front; windows are created with GTK, but then all events in that window are forwarded directly to the Lisp engine. Even with GTK3, Emacs is still using much of its old code, handling drawing and events at the X level. Later, Cairo support was added to help with printing; it has been "periodically maintained" since.

There has been interest in Wayland support for some time; after all, Emacs now uses GTK and Cairo, both of which support Wayland, so how hard can it be? A rudimentary port was done in 2014 using Cairo for drawing; it was missing a number of features but was a useful learning experience. That exercise did lead to the adoption of Cairo as the core rendering library in 2015, though Cairo did not become the default until the 27.1 release in January 2020. Nonetheless, it was one of the first signs that Emacs may be moving away from X.

In 2019, Stefan Monnier pointed out an in-progress Wayland port and asked if anybody had looked at it. That was, it turns out, a pure GTK port that migrates all low-level X code to its GTK3 equivalents. All drawing is done through Cairo and GTK, which should be wonderful for Emacs going forward, in that the maintenance burden for that code rests with the GTK developers.

It works by creating a new terminal type that uses GTK to get a window up on the screen and handles GTK events. There is a new Emacs widget that serves as the endpoint for those events and implements the Emacs display. In the future, this widget could conceivably be used in any program that wants to be able to put up an Emacs window. This widget can hold others, things like GTK scrollbars seem to work well within it, for example.

Getting all of this working has required dealing with a number of challenges, many of which are based in the fact that Emacs is both huge and old. The redisplay code is about 20,000 lines, full of #ifdefs and macros; it's hard to work with. There are a lot old workarounds in this code that may not be needed anymore, but it can be hard to tell.

The X code in Emacs "wants to know everything" and handle everything itself; this approach is wired deeply into Emacs as a whole. GTK, instead, wants to abstract things and deal with as much as possible itself. So, for example, Emacs wants to see and respond to raw X events, but that's not what GTK provides. Emacs is designed around having all events be handled through Lisp code, but that may not be a model that can be supported going forward.

In Wayland, all windows are independent, even when they are nested inside each other. Windows in X, instead, are interrelated and applications can use global coordinates. That leads to confusion in the Wayland switch because it's not always clear where the origin should be; that leads to annoying problems where popups appear in the wrong place. Walsh lost three weeks of his life figuring this out, he said.

The pure-GTK work, in the end, came down to about 100 commits touching 58 files and adding about 16,000 lines of code. Does it work? Yes, it does. It was developed on Arch Linux and Fedora, but has been tested on FreeBSD as well; he has been using it daily for the last year. He does not see a lot of differences between the X and Wayland ports, but prefers the Wayland version. It is "bug-compatible" with X Emacs, though there are a few rendering differences that one might notice. He thinks it renders more quickly, but had no measurements to prove that.

There are a few things that will not be supported in the Wayland version, though. Xsettings has been replaced with GSettings, for example. Wayland doesn't support self-positioning of windows — that is left to the compositor — so Emacs is unable to support options that fix the position of windows.

This code currently lives in a feature branch in the Emacs Savannah repository; he would like to see more people testing it. He also said that, if there are developers who could provide GTK help, that would be appreciated.

Looking forward

Now that there is a pure GTK3 port of Emacs, GTK4 has naturally been released. There is some experimental work going on to port Emacs to GTK4, he said, but there may not be a lot of benefits to be had from that port. GTK4 is an even further departure from X, making the port harder yet. There has also been interest in using features like OpenGL, Vulkan, and DirectX, but he advised patience; it took twelve years to get the Cairo port in, for example. Beyond that, text rendering is relatively hard on a GPU, so it's not clear what Emacs would gain from this work.

What else can be expected from Emacs in general? It probably will not break away from its "ugly look", he predicted. The GTK port was not meant to change the editor's appearance. The default key bindings will stay the same; there are too many old hands who would resist changing them. Those old hands are the core Emacs user base, so there is no desire to make them unhappy. Elisp will not be replaced anytime soon either — except, maybe, with a different flavor of Lisp. Whenever that conversation comes up, though, people start arguing about which flavor, with lots of bikeshedding going on.

What is changing can be seen in the ongoing push toward common libraries rather than doing everything by hand within Emacs itself. The adoption of libraries like HarfBuzz, Cairo, and Jansson are examples; he thinks this trend could continue.

A big thing to look forward to is native compilation of Lisp code; that will have "a huge ripple effect" in the Emacs community. Andrea Corallo first asked about this capability in 2019, and delivered a prototype implementation one month later. It uses libgccjit as the back-end for the byte compiler and supports a wide range of platforms. That is important: by using GNU tools for the compilation, it meets the "GNU political requirements" and can actually be included in Emacs. This work will allow writing more of Emacs in Lisp, migrating some code back from C. That will allow for more customization; in the future, customized Emacs "distributions" may take on a bigger role as a result.

He finished by crediting Yuuki Harano as the main developer behind the pure GTK work in Emacs.

Your editor asked afterward about how interested users could try out this port. One way is to check out the branch from Savannah and build it, but there are also a number of repositories out there with builds for various distributions now. Arch Linux packages it as part of the distribution "because it's Arch". This article was written using a build from the Fedora Copr repository. The xeyes test shows that it is indeed running natively on Wayland, and it seems to work well, with the one strange exception that mouse-wheel scrolling happens at half speed.

Index entries for this article
Conferencelinux.conf.au/2021


to post comments

Wayland support (and more) for Emacs

Posted Jan 28, 2021 17:33 UTC (Thu) by JoeBuck (subscriber, #2330) [Link] (2 responses)

I remember getting the tape (probably made by RMS himself) in the mail at work in '85 or '86 (yes, I'm, shall we say, experienced). I also remember the skull drawing in the comment block above the display code.

Wayland support (and more) for Emacs

Posted Jan 28, 2021 18:28 UTC (Thu) by atai (subscriber, #10977) [Link]

May you still get code from/to RMS, in whatever medium appropriate for that time, at 2085.

Wayland support (and more) for Emacs

Posted Jan 29, 2021 5:02 UTC (Fri) by felixfix (subscriber, #242) [Link]

I got a tape in 1989 or 1990 to port to a "Regulus" real-time Unix whose file names had two fewer characters. That was quite a chore but I got it working.

Wayland support (and more) for Emacs

Posted Jan 28, 2021 18:39 UTC (Thu) by jem (subscriber, #24231) [Link] (7 responses)

Other desired changes are more "socially easy", but are hard technically; these include making elisp faster so that more of Emacs could be written in Lisp rather than C.

We have GNU Guile 3.0, which features a JIT native code generator, and provides a "complete Emacs-compatible Elisp implementation"1. Work on Guile-emacs was started maybe a decade ago, and "as of October 2014, the implementation had reached a stage where Guile Emacs is able to reliably run most Emacs Lisp code." And yet the reaction has been: "meh".

I really doubt a faster Elisp isn't a social problem.

Wayland support (and more) for Emacs

Posted Jan 28, 2021 19:11 UTC (Thu) by IanKelling (subscriber, #89418) [Link] (2 responses)

> able to reliably run most Emacs Lisp code." And yet the reaction has been: "meh".

Afaik, it ran significantly slower. Also "most" needs to be more specific. Before any real consideration it would need to be really close, like 99%, "most" could mean 80%.

Wayland support (and more) for Emacs

Posted Jan 28, 2021 19:13 UTC (Thu) by IanKelling (subscriber, #89418) [Link]

> real consideration

I should have said real preparation or momentum to switch. But, I do agree there is a social problem as well.

Wayland support (and more) for Emacs

Posted Jan 28, 2021 19:41 UTC (Thu) by jem (subscriber, #24231) [Link]

Guile Emacs's startup speed was/is slower, not the speed in general. Also, the ability to run "most" Emacs Lisp was the state of things in 2014. In 2017 version 2.2.0 was released, which "provided a complete Emacs-compatible Elisp implementation, implementing all of Elisp's features and quirks in the same way as Emacs."

Wayland support (and more) for Emacs

Posted Jan 28, 2021 22:29 UTC (Thu) by mmaug (subscriber, #61003) [Link] (3 responses)

I had spoken with one of the Emacs/Guile developers at LibrePlanet in '17 or '18 (I think). Performance was definitely an issue, but a lot of that problem was rooted in the fact that Emacs and Guile represented text chunks in very different ways and the need to convert back and forth between the two. I do not remember the details, but there was some discussion of the issue in the repo along side of the code. Unlike the GUI, Lisp is fundemental to the Emacs engine itself and the extra boxing/unboxing on every Lisp call is neither trivial nor efficient regardless how much faster the Lisp engine itself is. From what I recall of the conversation, this was seen as a significant problem and neither interest nor resources were available to address it.

Wayland support (and more) for Emacs

Posted Jan 28, 2021 23:30 UTC (Thu) by iabervon (subscriber, #722) [Link]

It's definitely the case that Emacs has some special complicated data types, and one of these is what Emacs considers an ordinary string.

Wayland support (and more) for Emacs

Posted Jan 29, 2021 1:03 UTC (Fri) by atai (subscriber, #10977) [Link] (1 responses)

not knowing the details, but it would seem strange that scheme and Emacs Lisp have semantic gap in boxing and unboxing...

Wayland support (and more) for Emacs

Posted Feb 1, 2021 22:41 UTC (Mon) by tgerdin (guest, #112550) [Link]

I think the commenter was referring to the required string conversion when moving between Emacs Lisp and Guile.

Wayland support (and more) for Emacs

Posted Jan 29, 2021 9:43 UTC (Fri) by dottedmag (subscriber, #18590) [Link]

Oh, wonders. I was thinking of doing the port myself, and... it is already done!

Wayland support (and more) for Emacs

Posted Jan 29, 2021 13:41 UTC (Fri) by daenzer (subscriber, #7050) [Link]

GTK4 uses OpenGL for its drawing by default, so that one stone should get an extra bird.

Wayland support (and more) for Emacs

Posted Jan 29, 2021 14:26 UTC (Fri) by zuki (subscriber, #41808) [Link] (1 responses)

I installed the version from Fedora copr, and I'm very pleasantly surprised. The font rendering is, wow, much crispier. This is most likely because of a hidpi screen. Compared to the wayland version, the Xwayland version is simply blurry.

Wayland support (and more) for Emacs

Posted Jul 20, 2021 16:56 UTC (Tue) by ocrete (subscriber, #107180) [Link]

It's really saved Emacs for me too using fractional scaling on my nice 4K display.

A huge thanks to the developers who did all the hard work.

Wayland support (and more) for Emacs

Posted Jan 29, 2021 21:09 UTC (Fri) by swilmet (subscriber, #98424) [Link]

> if there are developers who could provide GTK help

While I cannot help for the Emacs's GTK code, I have written this guide:
https://people.gnome.org/~swilmet/glib-gtk-book/

To understand the GTK API, a basic knowledge about GLib and GObject is required. That's what the guide describes (bottom-up approach).

You may also be interested by the Emacs "plugin" for Devhelp, the API browser:
https://wiki.gnome.org/Apps/Devhelp
https://gitlab.gnome.org/GNOME/devhelp/-/blob/master/plug...

Wayland support (and more) for Emacs

Posted Feb 2, 2021 17:40 UTC (Tue) by nix (subscriber, #2304) [Link] (3 responses)

> Nonetheless, it was one of the first signs that Emacs may be moving away from X.

This is unlikely given the understandable but still notable lack of interest from the Gtk developers in bugs, some very old, which completely break important Emacs workflows: there are a bunch of obscure ones involving fonts (most of which seem to have been fixed now, probably as a result of the harfbuzz push): the big remaining one has to be https://gitlab.gnome.org/GNOME/gtk/issues/221... and note that it's unclear if this is *actually* a bug any more but in over a year nobody has checked. (I'm planning to check when I eventually bite the bullet and upgrade to 27.1, but my Emacs is so crucial and breaking it is so disastrous that I hardly ever upgrade it!)

One problem here is that Emacs does things with the display that more or less no other program does, so of course toolkit developers aren't going to care much about fixing them since fixing them only affects one program. The big one is to open multiple sorts of display connections at once, and outlive them. This is usually done via emacs --daemon, but can be done directly without that being involved at all: you can open terminal connections on Emacsen started connected to X, you can open X connections on Emacsen started connected to the console or to nothing at all, then closing them and having Emacs keep running, then reopening them later and everything still works... I think one variant that doesn't work is having all three of Win32 *and* X *and* console connections at once in an Emacs started on the console, because of limitations in the Win32 API, but every other combo I'm aware of works -- except Gtk. Close a Gtk connection and reopen it later and -- a decade ago, anyway -- Emacs crashed horribly inside Gtk. Whether it still crashes is, ah, unclear, but this sort of thing is a very common use case for those Emacs users who run our editors on beefy machines that aren't our desktop: we want the Emacs and all its state to persist even when the desktop is rebooted. So I doubt the crusty old core developers will allow the old Lucid X toolkit to be torn out until that problem is actually confirmed fixed.

Wayland support (and more) for Emacs

Posted Feb 4, 2021 11:00 UTC (Thu) by yaap (subscriber, #71398) [Link] (1 responses)

Sometimes I use Emacs in daemon mode with a Gtk build: "GNU Emacs 27.1 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.5, cairo version 1.16.0)". Connecting with gnuclient, closing the unique frame, and reconnecting later with a new frame has never been a problem. From what I understand this is the sort of client/server use that triggered the bug.

In the Gtk bug you pointed to a maintainer comment at the end says the ticket was closed as now irrelevant, being related to a Gtk2 issue (an I use Gtk3). I don't use the daemon mode much in the end, so don't really stress it, but so far it worked for me.

Wayland support (and more) for Emacs

Posted Feb 4, 2021 17:36 UTC (Thu) by nix (subscriber, #2304) [Link]

Try disconnecting and connecting again on a *new X server*. Gtk used to get very confused in this case (but of course with an emacs --daemon on a remote host it is routine).

This was definitely still happening with Gtk 3 as of two or three years ago.

Wayland support (and more) for Emacs

Posted Feb 12, 2021 3:44 UTC (Fri) by pclouds (guest, #76590) [Link]

The problem with closing display is still there, but tracked elsewhere in https://gitlab.gnome.org/GNOME/gtk/-/issues/2315

Wayland support (and more) for Emacs

Posted Feb 4, 2021 13:43 UTC (Thu) by mtk (subscriber, #804) [Link] (1 responses)

as an emacs user since 1979(!) and a developer, i've always thought the primary challenge faced by emacs came from IDE's that parsed & analyzed edited source and made that semantic info available in useful ways. LSP is a great start and has generated lots of related tool development. but i'm lazy and don't want to spend months figuring out which language-specific back end (how many are there for python now?) or completion library from MELPA to use with it. i want a *curated* collection of LSP tools incorporated into the core.

Wayland support (and more) for Emacs

Posted Feb 20, 2021 21:39 UTC (Sat) by Hi-Angel (guest, #110915) [Link]

I'm kinda confused… Isn't there such list in lsp-mode's website https://emacs-lsp.github.io/lsp-mode/page/languages/

Wayland support (and more) for Emacs

Posted Feb 5, 2021 2:49 UTC (Fri) by flussence (guest, #85566) [Link] (1 responses)

All things considered, Emacs deserves some congratulations for getting out a release with GTK3 support so soon. I would've expected the G in GTK to get there first.

Wayland support (and more) for Emacs

Posted Feb 11, 2021 14:55 UTC (Thu) by ejr (subscriber, #51652) [Link]

Gimp? That was the G in GTK.


Copyright © 2021, Eklektix, Inc.
This article may be redistributed under the terms of the Creative Commons CC BY-SA 4.0 license
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds