News for hackers. Stuff that matters.
Re:Bringing GNU Emacs to native code [video] by fmakunbound on Monday May 04, 2020 @10:01AM (#23068151)
This seems convoluted compared to say moving the Lisp implementation from Emacs Lisp to Common Lisp, of which several native code compiling implementations exists.
Re:Bringing GNU Emacs to native code [video] by widdershins on Monday May 04, 2020 @10:07AM (#23068226)
Rewriting thousands of packages (some of which have had man-decades of work poured into them) would also be rather convoluted.
Re:Bringing GNU Emacs to native code [video] by junke on Monday May 04, 2020 @10:27AM (#23068453)
You don't need to rewrite packages if you have a layer where Emacs Lisp is compiled/interpreted as Common Lisp.

You can easily hack the readtable in Lisp, and rewrite Emacs Lisp sexps as Common Lisp sexps.

Some interesting things to consider are file-local variables, buffer-local variables, dynamic-scope/lexical-scope, how to handle floating point (-0.0e+NaN in Emacs Lisp, custom floating point rouding modes/traps in SBCL for example), but this looks like a reasonable approach overall.

For example (just a draft, this might be incorrect):

    USER> (defun make-local-variable (symbol)
            (eval `(define-symbol-macro ,symbol (bvar (quote ,symbol)))))
    MAKE-LOCAL-VARIABLE

    USER> (make-local-variable 'foo)
    FOO

    USER> (macroexpand-all '(list foo))
    (LIST (BVAR 'FOO))
    T
    T
The BVAR forms would then be able to access a buffer variable, with the current buffer. This works with SETF too (but Emacs Lisp SETQ should be replaced by SETF during the transform).

BVAR could expand into code that calls FFI functions, for compatibility. Using an FFI approach would allow to progressively rewrite some parts of the runtime into CL.

Just to clarify, I know this is a huge work to undertake, but this comes from the assumption that we want to keep all existing Elisp files running identically.

Re:Bringing GNU Emacs to native code [video] by toohotatopic on Monday May 04, 2020 @12:38PM (#23070147)
>You can easily hack the readtable in Lisp

The funny thing about lisp is that all things are easy, but yet, other eco systems offer more finished libraries.

Re:Bringing GNU Emacs to native code [video] by reikonomusha on Monday May 04, 2020 @01:18PM (#23070710)
Other ecosystems also become obsolete or dead within years counted on one hand. That’s perfect for today’s culture of disposable code though. Lisp isn’t good for that.
Re:Bringing GNU Emacs to native code [video] by TeMPOraL on Monday May 04, 2020 @05:05PM (#23073238)
That's the curse right here. Anyone can easily make a half-baked solution to their problem, so those solutions end up as half-baked libraries, if they get released at all.
Re:Bringing GNU Emacs to native code [video] by ufo on Monday May 04, 2020 @11:02AM (#23068861)
I would expect that an ELisp interpreter written in Common Lisp would likely be slower than the existing ELisp interpreter written in C.

As for compilation to Common Lisp, I don't know how feasible it would be. Apparently they tried something similar with Guile and it ran into problems.

Re:Bringing GNU Emacs to native code [video] by bjoli on Monday May 04, 2020 @11:38AM (#23069291)
Guile (the VM) runs elisp just fine. There has been zero optimization work done though so it is quite slow. The reason they did not go with guile was more political than anything else. It was also understandable from their point of view (and I say this as a guile weenie)
Re:Bringing GNU Emacs to native code [video] by hvis on Monday May 04, 2020 @02:50PM (#23071801)
It was somewhat political, but it was more of an issue of manpower. Also, Guile is basically maintained by one developer (as far as I've heard). It's pretty dangerous for Emacs to rely on a project like that.
Re:Bringing GNU Emacs to native code [video] by rurban on Monday May 04, 2020 @02:06PM (#23071286)
No, the reason is still slow string buffers. Politically everybody wants emacs to switch to guile.
Re:Bringing GNU Emacs to native code [video] by bjoli on Tuesday May 05, 2020 @06:53AM (#23078599)
Iirc Guile-emacs relies on Emacs for all string related functionality. The overhead comes for dynamic bindings that are currently slower in guiles elisp implementation. I haven't looked into it since talking to Robin about it in 2016.

You maybe have some more inside info, but back in 2016 I remember a lot of complaints and that guile was a liability for the much bigger Emacs project. Maybe even people threatening to retire as maintainers

Re:Bringing GNU Emacs to native code [video] by kazinator on Monday May 04, 2020 @10:31AM (#23068496)
Emacs contains a quarter million lines of C. What do you do with all the elisp that calls into it? Maybe that could be turned into a "libemacs" and used from FFI.
Re:Bringing GNU Emacs to native code [video] by koral on Monday May 04, 2020 @11:12AM (#23068987)
The real trouble of using Emacs C core from another language is that you have to use all the internal datastructures used by C core, and this exactly equivalent to all elisp datastructures.

This implies you have either to convert back and forward everything or you just can't use the native datastructures of the new programming language (making the whole operation often quite pointless).

I must confess that most of the comments in this thread seems to start from the assumption that people working on this in the last two+ decades are probably dumb, and this is sad.

Re:Bringing GNU Emacs to native code [video] by wtetzner on Monday May 04, 2020 @10:54AM (#23068755)
Or maybe you rewrite it in Common Lisp. If you're going to take the Common Lisp route, I think it might be worth going all in and just re-implementing Emacs in Common Lisp.
Re:Bringing GNU Emacs to native code [video] by mikelevins on Monday May 04, 2020 @11:54AM (#23069500)
That's already been done. More than once, in fact. The problem is that, although there are perfectly serviceable Emacsen written in Common Lisp, none of them are GNU Emacs.

For example, there's Hemlock from CMUCL, and its descendants built into Lispworks and Clozure Common Lisp.

Those implementations aren't likely to work well as a substitute for GNU Emacs. For one thing, there's a substantial ecosystem of software that depends on specific APIs and other characteristics of GNU Emacs. Writing code to bridge GNU Emacs APIs with those available in the Hemlock descendants would be a lot of work.

There are some other obstacles as well. CCL's implementation of Hemlock uses the Cocoa text architecture, so it's not portable to platforms other than macOS.

The Lispworks implementation is portable across Windows and numerous UNIXEN, but the Lispworks license will not allow delivery of a proper substitute for GNU Emacs (it forbids building an application that can be construed as a Lisp development system, and when I pressed them about exactly what limits that policy implies, they explicitly used Emacs as an example of an application that would be forbidden).

The original CMUCL Hemlock and its portable version are designed to work with CLX. It could probably be made to work in a modern X environment, but making it fit well into modern GUI environments and porting it to all the platforms GNU Emacs works on would be a huge amount of work.

I'm probably overlooking some other Emacsen, but I don't know of any off the top of my head for which the situation is any easier.

Re:Bringing GNU Emacs to native code [video] by fiddlerwoaroof on Monday May 04, 2020 @03:16PM (#23072116)
Robert Strandh is working on an Common Lisp emacs based on McClim but, McClim basically forces you to use linux.
Re:Bringing GNU Emacs to native code [video] by fiddlerwoaroof on Monday May 04, 2020 @03:22PM (#23072190)
There's also lem, which is more portable: https://github.com/cxxxr/lem
Re:Bringing GNU Emacs to native code [video] by metroholografix on Monday May 04, 2020 @12:16PM (#23069850)
It hasn't already been done, no. The Emacs variants you mentioned (that IMV should not be referred to as "Emacs") are from-scratch new implementations of just the concept behind Emacs, but not GNU Emacs itself. They are not compatible with GNU Emacs, they don't even support a small fraction of GNU Emacs features, and are thus doomed to obsolescence / non-existent market share.

So far, nobody -that I know of- has tried to move GNU Emacs to Common Lisp. GNU Emacs is a well-known, well-used platform and moving to Common Lisp could very well be worth the upfront costs.

Wishful thinking aside, the GNU Emacs development community has hunkered down behind Emacs Lisp, so improving it will bring immediate and tangible benefits to every GNU Emacs user. I love Common Lisp and do use SBCL for a lot of personal projects, but I also love GNU Emacs and I will take any improvements there that I can get. Watching Emacs Lisp become a more viable general purpose language (improved performance means an expanded set of problems that it can now address) is a great development. Stefan Monnier on emacs-devel:

  The main benefit of such a compiler is not to run existing Elisp
  code faster (99% of existing Elisp code runs fast enough that the
  user won't notice if it runs faster) but to make it practical to
  write other Elisp code which would otherwise be too slow. But for
  that to work well, you want the new compiler to be
  available "everywhere", rather than just on some platforms. So it
  will only start being useful when it works on GNU/Linux, macOS,
  Windows, ARM, RISC-V, x86, amd64, MIPS, younameit (AFAIK
  lbgccjit's CPU coverage is already good enough for that, so the
  main barrier here is the OS support), and when it's not just an
  option at compile-time but when it's included in all builds.
Re:Bringing GNU Emacs to native code [video] by mikelevins on Monday May 04, 2020 @01:03PM (#23070518)
You appear to consider "Emacs" and "GNU Emacs" synonymous. I explicitly do not.

Hemlock is not a reimplementation of GNU Emacs; it's derived from earlier Emacsen, just as GNU Emacs is. Hemlock in particular is based on ZWEI, and was in version 0.99 around the time that work started on the GNU Emacs project. It's a slightly older sibling of GNU Emacs, rather than a descendant of it.

GNU Emacs has undoubtedly become the de facto standard Emacs, but I don't see why that disqualifies other branches of the Emacs family tree from using the name that all of them inherit from the venerable TECO implementation.

My point was that Emacsen have already been built in Common Lisp, but that they do not serve as substitutes for GNU Emacs--a point with which you appear to agree.

Maybe we even agree on the reason why: because no Emacs implementation can substitute for GNU Emacs without supporting its ecosystem. The amount of work needed to do that would be enormous, whether the proposed substitute is written in Common Lisp or in something else.

Re:Bringing GNU Emacs to native code [video] by metroholografix on Monday May 04, 2020 @01:41PM (#23070986)
Sure, I just wanted to clarify and make it very clear that GNU Emacs in CL hasn't even been attempted since that was what the poster you originally replied to, equated "Emacs" with.

As you said, the value lies primarily in the ecosystem. Common Lisp is by far the better language, but GNU Emacs is by far the most empowering/practical environment. I don't think enormous work would be needed, there are plenty of powerful shortcuts one could take (some ideas are in this thread) and Emacs Lisp can be made to run on top of Common Lisp but it's safe to say that for whatever reasons the will/intent is simply not there.

Emacs Lisp is what we have to go on with and it's nice to see that it is not stagnating.

Re:Bringing GNU Emacs to native code [video] by junke on Monday May 04, 2020 @11:01AM (#23068846)
Primitive Emacs Lisp functions are defined in C, yes, using helper C macros. There might be a way to extract the symbol names from those macros, and maybe enough information to generate the FFI bindings.

Any kind of rewrite of Emacs is going to be a lot of work, I am not denying that. But if we assume there is a layer where everything works as it currently does (the C primitives), the Emacs Lisp defined on top of that needs not be defined in C, as far as I know.

Also, the last time I checked, there were a ton of legacy stuff in the configure script for different platforms.

Re:Bringing GNU Emacs to native code [video] by slightwinder on Monday May 04, 2020 @11:44AM (#23069363)
I wonder whether a modern modularized architecture might be the better way to handle this. Similar to neovim where you have a backend which communicates with a detatched frontend and other backends over some rpc-mechanism.

Refactor the existing emacs to work headless and become the legacy-core, while also establishing channels to attatch any other core and frontend.

Re:Bringing GNU Emacs to native code [video] by Myrmornis on Monday May 04, 2020 @11:41PM (#23076254)
https://github.com/xi-editor is an example of the architecture you're referring to I believe. Xi's README makes it clear that very high performance is one of the main goals: in particular it mentions the latency between keypresses and screen painting. To what extent does this RPC-based architecture limit performance compared to a single-process architecture? I know that the original author of Xi very much knows what he is doing, so I guess the answer is "it doesn't" -- I was just hoping to be educated on this point.
Re:Bringing GNU Emacs to native code [video] by gpderetta on Monday May 04, 2020 @12:35PM (#23070102)
An important optimization discussed in the video is to optimize through call into C code (i.e elisp->C->elisp). adding RPC at the boundaries would go in the opposite direction.
Re:Bringing GNU Emacs to native code [video] by slightwinder on Monday May 04, 2020 @04:06PM (#23072663)
It's optimization for a single process vs multiple processes.

Additionally, when you free up your architecture and loose up the internal bindings between different areas, you sometimes can open chances for significant improvments which prior where not possible, thus still be faster with a "worse" solution.

Considering how old GNU Emacs is, how much historical problems it has and how often you hear that this and that can't be optimized for this and that reason, this seems like such a situation where loosing up would open faster roads long term.

Re:Bringing GNU Emacs to native code [video] by WhatIsDukkha on Monday May 04, 2020 @02:25PM (#23071491)
Well this is not to my taste but I take it as a proof of existence that an rpc at the boundary could make for some amazing things -

https://github.com/rhysd/NyaoVim

Re:Bringing GNU Emacs to native code [video] by kazinator on Monday May 04, 2020 @10:44AM (#23068656)
It brings GCC into the address space via libgccjit. GCC is not robust enough to be integrated into applications that stay running, and be repeatedly invoked.
Re:Bringing GNU Emacs to native code [video] by tgbugs on Monday May 04, 2020 @03:19PM (#23072145)
I don't think that is how libgccjit is being used in this project. Emacs is not being linked with libgccjit. libgccjit is only being use for AOT compilation.
Re:Bringing GNU Emacs to native code [video] by gumby on Monday May 04, 2020 @10:15AM (#23068311)
I assume you mean writing an Emacs lisp interpreter in Common Lisp — which could be done with a small lexer, a lot of macros, and some library support. That would probably be a win.
Re:Bringing GNU Emacs to native code [video] by wtetzner on Monday May 04, 2020 @10:19AM (#23068352)
Depends on how difficult it is to integrate Common Lisp into Emacs. Or would you just be rewriting Emacs in Common Lisp?
Re:Bringing GNU Emacs to native code [video] by gumby on Monday May 04, 2020 @10:35AM (#23068551)
The issue is the importance of all the existing packages — invalidate them and nobody will use your implementation.

There are small syntactic differences between Emacs lisp and Common Lisp so a lexer that handled those would suffice.

There are some small semantic differences but they could be handled by porting code. And the Emacs c code could be rewritten in lisp or ported into the Common Lisp implementation, depending on your taste.

Re:Bringing GNU Emacs to native code [video] by wtetzner on Monday May 04, 2020 @10:48AM (#23068707)
The existing package ecosystem is one of the main value propositions of Emacs. Otherwise a re-implementation with a different extension language could have replaced it a long time ago.

> There are some small semantic differences but they could be handled by porting code. I suspect there are more than just small differences. And someone would have to do the work to port the code. I suspect that if packages that people use don't work, they will just avoid using the new version of Emacs, as opposed to abandoning the package.

It's not an impossible project, but it's not clear to me it's easier/better than the proposed solution.

Re:Bringing GNU Emacs to native code [video] by gumby on Monday May 04, 2020 @11:01AM (#23068841)
The point of my suggestion was to not have to rewrite any existing .el files.
Re:Bringing GNU Emacs to native code [video] by wtetzner on Monday May 04, 2020 @10:07AM (#23068219)
You don't think it would be difficult to keep compatibility with all of the existing Emacs Lisp packages that exist now?