Subject: bug#67390: 28; shorthands-font-lock-shorthands assumes shorthand uses same separator

From: Jonas Bernoulli <[email-masked]>
Date: Fri, 24 Nov 2023 22:51:32 +0100

João Távora <[email-masked]> writes:

> On Wed, Nov 22, 2023 at 10:18 PM Jonas Bernoulli <[email-masked]> wrote:
>
>> Could we add support for using an alternative separator in shorthands?
>
> I think so, if you can find a patch for it.  This only affects the font-locking
> bits of shorthands right?  IOW all other shorthand-aware functionality like
> eldoc, M-., etc, already works with different separators, right?

I haven't found any issues beside this off-by-one font-lock issue.

So far I have used this beauty:

diff --git a/lisp/emacs-lisp/shorthands.el b/lisp/emacs-lisp/shorthands.el
@@ -57,7 +57,7 @@ shorthands--mismatch-from-end
            for i from 1
            for i1 = (- l1 i) for i2 = (- l2 i)
            while (and (>= i1 0) (>= i2 0) (eq (aref str1 i1) (aref str2 i2)))
-           finally (return (1- i))))
+           finally (return (if (eq (aref str2 (1+ i2)) ?-) (1- i) i))))

Is that good enough?  Depending on how you look at it, this changes what
is being returned, but IMO this function is a bit murky to begin with.

The function name is `shorthands--mismatch-from-end', but it returns the
length of the common suffix, minus one, to account for the separator.
This change ensures that the separator is accounted for, even if it
differs between the shorthand and real symbol.

Since this function returns the length of the *matching* suffix after
the prefixes (including separator), I find it weird that its name
contains *MISmatch*.

It might make more sense to return the length of the shorthand prefix.

Also, have you considered throwing in a
  (not (string-equal (match-string 1) sname))

to avoid having to call `shorthands--mismatch-from-end' at all?
Maybe you have, but concluded it is cheaper to do a bit too much work
for non-shorthands, than to effectively repeat some work for shorthands.

     Jonas