🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

4 total messages Started by Lawrence D’Oliveiro Thu, 15 Oct 2026 21:33
Re: GNU Emacs native compiled functions
#32
Author: Lawrence D’Oliveiro
Date: Thu, 15 Oct 2026 21:33
90 lines
6556 bytes
On Tue, 6 Oct 2026 20:51:02 -0000 (UTC), I wrote:

> On Tue, 06 Oct 2026 08:34:15 -0400, Stefan Monnier wrote:
>
>> The move to lexical scoping eliminated that problem and brought the
>> joy of closures with it (the lack of closures was mostly not
>> perceived as a problem: the coding style just "naturally" avoided
>> them, and for those rare cases where they were really handy you
>> could use hacks like backquoted lambdas or `lexical-let`).
>
> I can’t imagine how you could have functions as first-class objects
> without closures. At the very least, you would end up writing a lot
> more repetitive code, which I hate.

Well, the discussion got derailed by some irrelevancies that somehow
crept in to responses over the first example I posted -- which wasn’t
really a good illustration of closures anyway; I realized it would
probably work just as well with dynamic binding.

So, to start again, here’s a much more elaborate example, which you
can see in all its horrible glory at
<https://gitlab.com/ldo/emacs-prefs/-/blob/master/alt-alphanumerics.el>.
The idea is to create a function factory that defines a set of
functions for converting ordinary letters and numbers in selected text
into the various sets of those weird alternative alphanumerics
available in Unicode. Instead of writing out each function in full,
with lots of repetitive code, I define a set of translation tables,
and use a closure to construct the appropriate mapping command for
each one.

Having defined a generic “to-alt-alphanumerics” function that, given
the name of one of these alternate ranges of alphanumerics, converts
the text to those characters, the following code generates a
customized version for each range name:

    (dolist
        ; define the commands for converting regular text to each alternate alphanumeric range
        (convdef
            '( ; funcname rangename
                ; "double-struck italic" -- only partial
                (to-circled-alt-alpha "circled")
                (to-parenthesized-alt-alpha "parenthesized")
                (to-full-stop-alt-alpha "full stop")
                (to-comma-alt-alpha "comma")
                (to-double-circled-alt-alpha "double circled")
                (to-negative-circled-alt-alpha "negative circled")
                (to-dingbat-circled-sans-serif-alt-alpha "dingbat circled sans-serif")
                (to-dingbat-negative-circled-sans-serif-alt-alpha "dingbat negative circled sans-serif")
                (to-fullwidth-alt-alpha "fullwidth")
                (to-squared-alt-alpha "squared")
                (to-negative-squared-alt-alpha "negative squared")
                (to-double-struck-alt-alpha "double-struck")
                (to-mathematical-bold-alt-alpha "mathematical bold")
                (to-mathematical-italic-alt-alpha "mathematical italic")
                (to-mathematical-bold-italic-alt-alpha "mathematical bold italic")
                (to-mathematical-script-alt-alpha "mathematical script")
                (to-mathematical-bold-script-alt-alpha "mathematical bold script")
                (to-mathematical-bold-fraktur-alt-alpha "mathematical bold fraktur")
                (to-mathematical-sans-serif-alt-alpha "mathematical sans-serif")
                (to-mathematical-sans-serif-bold-alt-alpha "mathematical sans-serif bold")
                (to-mathematical-sans-serif-italic-alt-alpha "mathematical sans-serif italic")
                (to-mathematical-sans-serif-bold-italic-alt-alpha "mathematical sans-serif bold italic")
                (to-mathematical-monospace-alt-alpha "mathematical monospace")
                (to-turned "turned")
            )
        )
        (let
            (
                (funcname (car convdef))
                (rangename (cadr convdef))
            )
            (setf (symbol-function funcname)
                (lambda (beg end)
                    (interactive "*r")
                    (to-alt-alphanumerics beg end rangename)
                ) ; lambda
            )
        ) ; let
    ) ; dolist

I still find some quirks in the way Emacs does lexical binding; do you
need a “;;; -*- lexical-binding: t; -*-” header comment line, or can
it be done with regular lisp code as “(setq lexical-binding t)”? Also,
the above code is followed by this line

    (setq lexical-binding nil)

because otherwise I found that subsequent use of “(deactivate-mark)”
was liable to misbehave otherwise. (This might be fixed in newer Emacs
versions.)
Re: GNU Emacs native compiled functions
#33
Author: Stefan Monnier
Date: Fri, 16 Oct 2026 13:27
39 lines
3422 bytes
> I still find some quirks in the way Emacs does lexical binding; do you
> need a “;;; -*- lexical-binding: t; -*-” header comment line, or can
> it be done with regular lisp code as “(setq lexical-binding t)”? Also,
> the above code is followed by this line
>
>     (setq lexical-binding nil)
>
> because otherwise I found that subsequent use of “(deactivate-mark)”
> was liable to misbehave otherwise. (This might be fixed in newer Emacs
> versions.)

Static-vs-dynamic binding is not just a boolean in the state of the
"Lisp machine" (something which you could change with a `setq`) but is
better thought of as an attribute of source code.  At any given time,
you can have several functions using the static dialect of ELisp and
several other functions using the dynamic dialect, and they can all call
each other.

IOW, the static-vs-dynamic property is a static rather than
a dynamic property.  🙂

Imagine compiling a file like:

    (setq lexical-binding nil)

    (defun my-fun1 (..)
      ...)

    (setq lexical-binding t)

    (defun my-fun2 (..)
      ...)

and take into account that we're only *compiling* the file, so the
`setq`s are not executed: they're just turned into code which will set
the variable when/if the code is later executed.


=== Stefan
Re: GNU Emacs native compiled functions
#34
Author: Lawrence D’Oliveiro
Date: Fri, 16 Oct 2026 21:45
11 lines
2454 bytes
On Fri, 16 Oct 2026 09:27:43 -0400, Stefan Monnier wrote:

> Static-vs-dynamic binding is not just a boolean in the state of the
> "Lisp machine" (something which you could change with a `setq`) but
> is better thought of as an attribute of source code.

No, it is best thought of as a standard feature of the language, not
something you turn off or on.

Yet Emacs does have this “lexical-binding” variable, that you can use
to turn it on or off -- very much part of the state of the machine.
Re: GNU Emacs native compiled functions
#35
Author: Kaz Kylheku
Date: Fri, 16 Oct 2026 21:48
11 lines
2443 bytes
On 2026-10-16, Lawrence D’Oliveiro <ldo@nz.invalid> wrote:
> On Fri, 16 Oct 2026 09:27:43 -0400, Stefan Monnier wrote:
>
>> Static-vs-dynamic binding is not just a boolean in the state of the
>> "Lisp machine" (something which you could change with a `setq`) but
>> is better thought of as an attribute of source code.
>
> No, it is best thought of as a standard feature of the language, not
> something you turn off or on.

Poster boy for Dunning-Kruger.
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads