From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Daniel Mendler Newsgroups: gmane.emacs.bugs Subject: bug#47712: 27.1; Provide `string-display-width` function, which takes properties into account, `substring-width` Date: Sun, 11 Apr 2021 23:16:13 +0200 Message-ID: <642c8a37-31c7-2723-12af-06cd7f120c2f@daniel-mendler.de> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="31997"; mail-complaints-to="usenet@ciao.gmane.io" To: 47712@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sun Apr 11 23:18:15 2021 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 1lVhTC-0008Bt-N3 for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 11 Apr 2021 23:18:14 +0200 Original-Received: from localhost ([::1]:36986 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lVhTB-0007Np-NY for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 11 Apr 2021 17:18:13 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:42522) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lVhS3-0006rn-Gk for bug-gnu-emacs@gnu.org; Sun, 11 Apr 2021 17:17:03 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:44402) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lVhS2-0007ap-MI for bug-gnu-emacs@gnu.org; Sun, 11 Apr 2021 17:17:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1lVhS2-0001OR-I4 for bug-gnu-emacs@gnu.org; Sun, 11 Apr 2021 17:17:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Daniel Mendler Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 11 Apr 2021 21:17:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 47712 X-GNU-PR-Package: emacs X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.16181757955315 (code B ref -1); Sun, 11 Apr 2021 21:17:02 +0000 Original-Received: (at submit) by debbugs.gnu.org; 11 Apr 2021 21:16:35 +0000 Original-Received: from localhost ([127.0.0.1]:55948 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1lVhRb-0001Nd-6V for submit@debbugs.gnu.org; Sun, 11 Apr 2021 17:16:35 -0400 Original-Received: from lists.gnu.org ([209.51.188.17]:36724) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1lVhRZ-0001NV-28 for submit@debbugs.gnu.org; Sun, 11 Apr 2021 17:16:33 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:42472) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lVhRT-0006Vr-Ce for bug-gnu-emacs@gnu.org; Sun, 11 Apr 2021 17:16:32 -0400 Original-Received: from server.qxqx.de ([2a01:4f8:121:346::180]:52291 helo=mail.qxqx.de) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lVhRP-0007JX-5Q for bug-gnu-emacs@gnu.org; Sun, 11 Apr 2021 17:16:26 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=qxqx.de; s=mail1392553390; h=Content-Transfer-Encoding:Content-Type:MIME-Version:Date: Message-ID:Subject:From:To:Sender:Reply-To:Cc:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: In-Reply-To:References:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=RCU83ztfaG9K95VfGgkpI6x3M0cQ3yT54yt6RlVZPv0=; b=uculD7Jx+kyPamAaUGOFcmPiXF MfrZ6MyTE94nRmFJcMBkFiDS412qxh1tVN9CJT1r7lLPLnB37PZTkH4ksExhvk2oOU6AgF4LctGpK qULVzDK4kPtWTghhg7DJGZbIMzDO6p9bUYVO/n2kD7O/uF7Wcl5gWDZr2BKW5Z/umvEU=; Content-Language: en-US Received-SPF: pass client-ip=2a01:4f8:121:346::180; envelope-from=mail@daniel-mendler.de; helo=mail.qxqx.de X-Spam_score_int: -41 X-Spam_score: -4.2 X-Spam_bar: ---- X-Spam_report: (-4.2 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 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" Xref: news.gmane.io gmane.emacs.bugs:203865 Archived-At: Provide a `string-display-width' function, which computes the `string-width', but takes invisible display properties into account. This function is often needed by packages. For example my Consult package (https://github.com/minad/consult) has a crude (non-recursive) version of that function. Then the Embark package has (https://github.com/oantolin/embark) has a similar function. Last but not least, the function already exists in Org under the name `org-string-width'. It is reasonable to implement this function in Elisp. But all the implementations have to use `string-width' for measuring the parts which make up the actual string. Unfortunately this requires allocations for the substrings. A possible solution to this problem is to implement a primitive function `substring-width' and implement `string-width' in terms of that function. (defun consult--display-width (string) "Compute width of STRING taking display and invisible properties into account." (let ((pos 0) (width 0) (end (length string))) (while (< pos end) (let ((nextd (next-single-property-change pos 'display string end)) (display (get-text-property pos 'display string))) (if (stringp display) (setq width (+ width (string-width display)) pos nextd) (while (< pos nextd) (let ((nexti (next-single-property-change pos 'invisible string nextd))) (unless (get-text-property pos 'invisible string) (setq width (+ width (string-width ;; Avoid allocation for the full string. ;; There should be a `substring-width' ;; provided by Emacs. TODO: Propose ;; upstream? Alternatively propose this ;; whole `display-width' function to ;; upstream. (if (and (= pos 0) (= nexti end)) string (substring-no-properties string pos nexti)))))) (setq pos nexti)))))) width))