From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Joseph Turner via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Newsgroups: gmane.emacs.bugs Subject: bug#67390: 28; shorthands-font-lock-shorthands assumes shorthand uses same separator Date: Fri, 24 Nov 2023 19:26:54 -0800 Message-ID: <878r6mzezo.fsf@ushin.org> References: <87a5r5ph3p.fsf@bernoul.li> <87msv2vmzf.fsf@bernoul.li> Reply-To: Joseph Turner Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="38542"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 67390@debbugs.gnu.org, Adam Porter , Jonas Bernoulli To: =?UTF-8?Q?Jo=C3=A3o_?= =?UTF-8?Q?T=C3=A1vora?= Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sat Nov 25 04:31:23 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 1r6jO5-0009ts-0k for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 25 Nov 2023 04:31:22 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r6jNk-0004dH-8p; Fri, 24 Nov 2023 22:31:00 -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 1r6jNh-0004d7-Ce for bug-gnu-emacs@gnu.org; Fri, 24 Nov 2023 22:30:57 -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 1r6jNh-0004GL-4w for bug-gnu-emacs@gnu.org; Fri, 24 Nov 2023 22:30:57 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1r6jNl-000879-PN for bug-gnu-emacs@gnu.org; Fri, 24 Nov 2023 22:31:01 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Joseph Turner Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sat, 25 Nov 2023 03:31:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 67390 X-GNU-PR-Package: emacs Original-Received: via spool by 67390-submit@debbugs.gnu.org id=B67390.170088305031173 (code B ref 67390); Sat, 25 Nov 2023 03:31:01 +0000 Original-Received: (at 67390) by debbugs.gnu.org; 25 Nov 2023 03:30:50 +0000 Original-Received: from localhost ([127.0.0.1]:37562 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1r6jNZ-00086j-MB for submit@debbugs.gnu.org; Fri, 24 Nov 2023 22:30:50 -0500 Original-Received: from out-172.mta1.migadu.com ([2001:41d0:203:375::ac]:34047) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1r6jNV-00086U-Jz for 67390@debbugs.gnu.org; Fri, 24 Nov 2023 22:30:48 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ushin.org; s=key1; t=1700883039; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=k5AensBvfw1yi2Gi5b4zMJje9rdtvv1hqk+rzhqdwq4=; b=R/iMRz46HFFSEDWyTBB5qyvjlic8CJLIuW2ZzSd+9OftOImZrdlp3LU8mQo4AEJdSpy0HY ZmdfAr7Lf9C53eZolfEst5d8jZ6lBVmkudXQpRsXBfE7TZqD8ra+ExRT6cJVAPQqvDEH3s TAe4t6kwY22pVYaIevqzt4iSPvWvfmBsE2Ix8KBBSCAvcEYtXLipTgEH8xASfoTecKMAvt MFHMdh8ll4LDUCM3lU3qK6uYSaMr2CLBWDmMHm7VNSmL+ff3ojJfIM+wpmXW3CwdbJxxjZ nsR2c5+CKj6jbkhjiQiyWyKyoJhuvYiClrKpMlfrGUkEuz6WAoL5mtboJD7uyw== X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. In-reply-to: X-Migadu-Flow: FLOW_OUT 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:274905 Archived-At: --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Jo=C3=A3o T=C3=A1vora writes: > On Fri, Nov 24, 2023 at 9:51=E2=80=AFPM Jonas Bernoulli wrote: > >> 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. Please see the attached patch, inspired by Jonas's comment. >> 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. I haven't done any benchmarking - I'm curious to learn how to benchmark font lock though! Thanks!! Joseph --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0001-Support-shorthand-prefixes-besides.patch >From e469c9d621116857bd1d84720eb2eb43d4560074 Mon Sep 17 00:00:00 2001 From: Joseph Turner Date: Fri, 24 Nov 2023 19:17:13 -0800 Subject: [PATCH] Support shorthand prefixes besides "-" Previously, shorthands-font-lock-shorthands added font locking to the shorthand prefix by checking for a mismatch between the shorthand and longhand symbols. This broke font-locking when the shorthand prefix separator was not "-" (bug#67390). Now, shorthands-font-lock-shorthands adds font locking to the shorthand prefix by checking if any of the shorthand prefixes in read-symbol-shorthands are a prefix for the current symbol name. Thanks to Jonas Bernoulli for the idea to use (not (string-equal (match-string 1) sname)) --- lisp/emacs-lisp/shorthands.el | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/lisp/emacs-lisp/shorthands.el b/lisp/emacs-lisp/shorthands.el index 82200ab88e9..141b6115a3d 100644 --- a/lisp/emacs-lisp/shorthands.el +++ b/lisp/emacs-lisp/shorthands.el @@ -52,12 +52,14 @@ :version "28.1" :group 'font-lock-faces) -(defun shorthands--mismatch-from-end (str1 str2) - (cl-loop with l1 = (length str1) with l2 = (length str2) - 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)))) +(defun shorthands--find-if (predicate seq &optional key) + "Find the first item satisfying PREDICATE in SEQ. +Return the matching item, or nil if not found. Optional argument +KEY is used to filter SEQ, as in `cl-find-if'." + (catch 'found + (dolist (el seq) + (when (funcall predicate (funcall (or key #'identity) el)) + (throw 'found el))))) (defun shorthands-font-lock-shorthands (limit) (when read-symbol-shorthands @@ -69,10 +71,15 @@ font-lock-string-face))) (intern-soft (match-string 1)))) (sname (and probe (symbol-name probe))) - (mm (and sname (shorthands--mismatch-from-end - (match-string 1) sname)))) - (unless (or (null mm) (= mm (length sname))) - (add-face-text-property (match-beginning 1) (1+ (- (match-end 1) mm)) + (prefix (and sname + (not (string-equal (match-string 1) sname)) + (car (shorthands--find-if + (lambda (short) + (string-prefix-p short (match-string 1))) + read-symbol-shorthands #'car))))) + (when prefix + (add-face-text-property (match-beginning 1) + (+ (match-beginning 1) (length prefix)) 'elisp-shorthand-font-lock-face)))))) (font-lock-add-keywords 'emacs-lisp-mode '((shorthands-font-lock-shorthands)) t) -- 2.41.0 --=-=-=--