From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: =?UTF-8?Q?Jo=C3=A3o_?= =?UTF-8?Q?T=C3=A1vora?= Newsgroups: gmane.emacs.bugs Subject: bug#67390: 28; shorthands-font-lock-shorthands assumes shorthand uses same separator Date: Sat, 25 Nov 2023 00:03:10 +0000 Message-ID: References: <87a5r5ph3p.fsf@bernoul.li> <87msv2vmzf.fsf@bernoul.li> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="13263"; mail-complaints-to="usenet@ciao.gmane.io" Cc: joseph@ushin.org, adam@alphapapa.net, 67390@debbugs.gnu.org To: Jonas Bernoulli Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sat Nov 25 01:04:18 2023 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1r6g9i-0003AY-7e for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 25 Nov 2023 01:04:18 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r6g9P-000856-J8; Fri, 24 Nov 2023 19:03:59 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1r6g9O-00084q-5V for bug-gnu-emacs@gnu.org; Fri, 24 Nov 2023 19:03:58 -0500 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1r6g9N-0002s2-Sf for bug-gnu-emacs@gnu.org; Fri, 24 Nov 2023 19:03:57 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1r6g9S-0002V8-8K for bug-gnu-emacs@gnu.org; Fri, 24 Nov 2023 19:04:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: =?UTF-8?Q?Jo=C3=A3o_?= =?UTF-8?Q?T=C3=A1vora?= Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sat, 25 Nov 2023 00:04:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 67390 X-GNU-PR-Package: emacs X-Debbugs-Original-Cc: Joseph Turner , Adam Porter , bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.17008706229589 (code B ref -1); Sat, 25 Nov 2023 00:04:02 +0000 Original-Received: (at submit) by debbugs.gnu.org; 25 Nov 2023 00:03:42 +0000 Original-Received: from localhost ([127.0.0.1]:37501 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1r6g98-0002Ua-9H for submit@debbugs.gnu.org; Fri, 24 Nov 2023 19:03:42 -0500 Original-Received: from lists.gnu.org ([2001:470:142::17]:35436) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1r6g94-0002UL-0k for submit@debbugs.gnu.org; Fri, 24 Nov 2023 19:03:40 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1r6g8t-00081M-QM for bug-gnu-emacs@gnu.org; Fri, 24 Nov 2023 19:03:27 -0500 Original-Received: from mail-lf1-x12b.google.com ([2a00:1450:4864:20::12b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1r6g8r-0002oc-Te for bug-gnu-emacs@gnu.org; Fri, 24 Nov 2023 19:03:27 -0500 Original-Received: by mail-lf1-x12b.google.com with SMTP id 2adb3069b0e04-5079f6efd64so3196518e87.2 for ; Fri, 24 Nov 2023 16:03:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1700870603; x=1701475403; darn=gnu.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=Ol+AjJlLPdeK8Wk9mRyBSoMDt8M7c64Ya7RZ95ZZWa8=; b=Uh7zWHp3T7vNrCMEa9W1zntC/nic4uEku0WDOUH7wGqZxMldT+baZdPeKsuPHb7D1b 8PeCd6i6CwccVDge0MkPxOLnF8HdAehYiFO7/vlOlSdi841iD3yhjZDRf0XLr+ZSWlO8 xcHbMu8Jtmv8DaucUa8nZJqtVsoMaTsBed2qGv+/04IY27Kz2SPMDdvQl+bQ/1icatBk KvNVmKnQtgaVmVpJaS4e+vOHPcmBzG6411+AIbYfXVWxBT8oX1D6CHkWubD673mWjC9B R/FNLprKhwYTfgVR7ycLWnHxZiy17N7Ng1/NyJM7gZKY4VK81BZMoGpvL2bMvR5h2k4m 6J2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700870603; x=1701475403; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Ol+AjJlLPdeK8Wk9mRyBSoMDt8M7c64Ya7RZ95ZZWa8=; b=eqvmVTT9yjnCRS9ieutFsQ5u7OV8CxITqAgYh8Y7nX+joLs4A2n2tY47nELhZdZ9OS cvTEDeudEXjsZXm7rizm7idD47zoioAY1ZeJ0vIAe3SCDqYKIPNMtS88WVYU7a0r62gp OLG6AfhYYOgAXTtPxJHZwrhuI9dYYIi4u1InYQPvGbI8wJHYyqwRzWkltmM2idgxgLcp 0EtOnVHJVz3erAvXRHSo9nI7RGOMRy7PsTcBeA2pSp0NWCTIk3+xytTRDN0WS91nkixI yYhQCroKY5U9QXGwp8RKKaatXOnkKOfXXF6c7u911ufy+Ku2yxpKIHloZ+2iGIEeKjL+ gIeA== X-Gm-Message-State: AOJu0YzlB6h1wbaeJPhAULM2QEXrtFS8zIAWVPIhpDlSCcWxnJv0wUQW UEsbQUUjq/BH0U4uQ6ho74MK0WamXKeYmLiswv5p2M6E1Lk= X-Google-Smtp-Source: AGHT+IFJxK14DZtOHQEBIztyrxM5G8nuvPReVCkJaRky9ObII92i+3xglJiMFj1qrYoivzuFKau9/oHMotv3SYaKljM= X-Received: by 2002:ac2:5e66:0:b0:509:115c:51de with SMTP id a6-20020ac25e66000000b00509115c51demr2464144lfr.25.1700870603019; Fri, 24 Nov 2023 16:03:23 -0800 (PST) In-Reply-To: <87msv2vmzf.fsf@bernoul.li> Received-SPF: pass client-ip=2a00:1450:4864:20::12b; envelope-from=joaotavora@gmail.com; helo=mail-lf1-x12b.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:274900 Archived-At: On Fri, Nov 24, 2023 at 9:51=E2=80=AFPM Jonas Bernoulli = wrote: > I haven't found any issues beside this off-by-one font-lock issue. Good. > So far I have used this beauty: > > diff --git a/lisp/emacs-lisp/shorthands.el b/lisp/emacs-lisp/shorthands.e= l > @@ -57,7 +57,7 @@ shorthands--mismatch-from-end > for i from 1 > for i1 =3D (- l1 i) for i2 =3D (- l2 i) > while (and (>=3D i1 0) (>=3D i2 0) (eq (aref str1 i1) (aref s= tr2 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. A bit dodgy, no :-) Maybe a docstring would shed some light on what this function is supposed to do, so I'll propose one below. > 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*. Probably I wanted to emulate what CL's MISMATCH does to some degree. cl-mismatch exists in Emacs, but it's not available in shorthands.el, and it seems to be buggy anyway. You can rename the function shorthands--common-suffix-length if you want. I probably meant it to be separator-agnostic function, and be replaceable by a cl-mismatch some time around 2084, Now to your problem: I think what you want is to customize element comparison (CL's MISMATCH allows that, btw). Here's one way. diff --git a/lisp/emacs-lisp/shorthands.el b/lisp/emacs-lisp/shorthands.el index 82200ab88e9..36a862bc037 100644 --- a/lisp/emacs-lisp/shorthands.el +++ b/lisp/emacs-lisp/shorthands.el @@ -52,11 +52,18 @@ elisp-shorthand-font-lock-face :version "28.1" :group 'font-lock-faces) -(defun shorthands--mismatch-from-end (str1 str2) - (cl-loop with l1 =3D (length str1) with l2 =3D (length str2) +(defun shorthands--mismatch-from-end (str1 str2 &optional test) + "Compute position of first mismatching element of STR1 and STR2, from en= d. +The return value is the reverse index of that element. If 0, the +last characters of STR1 and STR2 differ, if 1, the penultimate +characters differ, and so on. If optional TEST is passed, +compare elements using that function, else compare with `eq'." + (cl-loop with test =3D (or test #'eq) + with l1 =3D (length str1) with l2 =3D (length str2) for i from 1 for i1 =3D (- l1 i) for i2 =3D (- l2 i) - while (and (>=3D i1 0) (>=3D i2 0) (eq (aref str1 i1) (aref str= 2 i2))) + while (and (>=3D i1 0) (>=3D i2 0) + (funcall test (aref str1 i1) (aref str2 i2))) finally (return (1- i)))) (defun shorthands-font-lock-shorthands (limit) And now using the following lambda for TEST yields what you want: (shorthands--mismatch-from-end "s-tail" "long-tail") ;; =3D> 5 (shorthands--mismatch-from-end "s/tail" "long-tail" (lambda (c1 c2) (or (and (eq c1 ?/) (eq c2 ?-)) (eq c1 c2)))) ;; =3D> also 5 Of course, you can hardcode this test inside the function, no need for a parameter, and rename the function to whatever you find more appropriate. This allows us to keep control over what things are considered acceptable separators from a font-locking perspective > It might make more sense to return the length of the shorthand prefix. I've been away from this code for a couple of years, so feel free to propose more extensive changes. As long as they don't increase the complexity and are suitably tested, I have nothing against. > 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? Where and how this be thrown in? How would you know what to highlight in the shorthand printed form? There can be many shorthand prefixes in a given file. But do show some code. > 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. Maybe. Not sure this is more work (string-equal must still compare every character right?), but, in summary, I'm not married to this implementation. I somewhat appreciate that I could still read it after not having looked at it for a couple years, but feel free to change it. Jo=C3=A3o