From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: James Cash Newsgroups: gmane.emacs.devel Subject: Re: [PATCH] Add option COUNT argument to text-property-search functions Date: Fri, 30 Jul 2021 16:49:17 -0400 Message-ID: <87czqzle7m.fsf@gmail.com> References: <87im0rlias.fsf@gmail.com> <83y29npo8n.fsf@gnu.org> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="24023"; mail-complaints-to="usenet@ciao.gmane.io" Cc: , emacs-devel@gnu.org To: Eli Zaretskii Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Fri Jul 30 22:51:29 2021 Return-path: Envelope-to: ged-emacs-devel@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 1m9ZTd-00069G-8i for ged-emacs-devel@m.gmane-mx.org; Fri, 30 Jul 2021 22:51:29 +0200 Original-Received: from localhost ([::1]:52334 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1m9ZTc-0001pE-5C for ged-emacs-devel@m.gmane-mx.org; Fri, 30 Jul 2021 16:51:28 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:55604) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m9ZRb-0007Wb-1o for emacs-devel@gnu.org; Fri, 30 Jul 2021 16:49:23 -0400 Original-Received: from mail-qt1-x835.google.com ([2607:f8b0:4864:20::835]:34439) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1m9ZRY-00046L-UF; Fri, 30 Jul 2021 16:49:22 -0400 Original-Received: by mail-qt1-x835.google.com with SMTP id a19so7382951qtx.1; Fri, 30 Jul 2021 13:49:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:cc:subject:in-reply-to:references:date:message-id :mime-version; bh=G4oN6M3lEbvc48EsMXlgJCFVANR1VYg+lgqe+huq7vw=; b=lhOX8bJQF5GJFxEaHdcyO7oDX/luvyfnMPC8u0txTKh396c29EYjLW10Sm2eSHtn/J TtSjT8LOs+vTEx9jgIIoKktK5qp0VkE9KIotn2yqJJpu++r4g9RQY1jayYil1QC8zeXC kwIRn5InWlqdqrJCqAwBApBW8LAZ0iUneNJhFGIqLm/wtTsNyrrDUBLrN7DgYbgKBEqd QLK8tqRMtUkQBQsH6ctWgPygJfCxmdrjN9IcQHWIFQx3Y6IhG6fCPgygtzKE1EjKxjGs 2nZ2Q3BcfZ6lpjgUkwsu+CA1bVdP18WNiHAHwsxlyKcCLeG19kALcJmxn02q7lEAXRHM dCxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:cc:subject:in-reply-to:references :date:message-id:mime-version; bh=G4oN6M3lEbvc48EsMXlgJCFVANR1VYg+lgqe+huq7vw=; b=gSXemAfQzllJSAk5t8qd2ac5w8Me83Psw5t+zfry7GpgLR/ewre2vmZnlzAmGDPuD5 zTH0Y4JoezZ89SbMw+LPX9iP0k2AXIH88Zb993lhhl4NXIUuRwxwLyFUIv46pAuEIiXk uu2Hd9r8DiyZT7Hk2jdJyf6XFmrW/SCrtlHmkHdOl1ygpw5cY4ohfju6Iil96/LfcCB3 hpPWolzzFbHvn3DQxwEA+ltSVrrrSde1LYsYFuoE1SiMsaX8XIz7YUfF7cCDz0bQQqG9 kO4TX3kz3ve1wqm6pzVXJPA+eINffT3h/PXWU0uWcfp9A3iD3LahZi1bwYmzllZFAGa4 pP+g== X-Gm-Message-State: AOAM532VFvzQu6avT2NLgIL/rhZrWeMAI4DG/jNrNfuIKUqEjRMkEk/9 Gvy7uT6fFyk9XIlfHbgAAvsaHDI1tmyKVg== X-Google-Smtp-Source: ABdhPJzWes7EYe1K6LWmn7tMZHITfUcQG1du00RixKonjRMtoyvJZW2vE2Zj5xT28JsMe5b3I3j2dw== X-Received: by 2002:a05:622a:118a:: with SMTP id m10mr4034429qtk.263.1627678159350; Fri, 30 Jul 2021 13:49:19 -0700 (PDT) Original-Received: from gonk ([2607:fea8:10c1:8b00:1439:7588:85ce:969f]) by smtp.gmail.com with ESMTPSA id w71sm1527541qkb.67.2021.07.30.13.49.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Jul 2021 13:49:18 -0700 (PDT) In-Reply-To: <83y29npo8n.fsf@gnu.org> Received-SPF: pass client-ip=2607:f8b0:4864:20::835; envelope-from=james.nvc@gmail.com; helo=mail-qt1-x835.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 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.io gmane.emacs.devel:271873 Archived-At: Eli Zaretskii writes: >> +If COUNT is a positive number, it will search forward COUNT times. If > ^^ > Two spaces between sentences, please, per the US English conventions. Fixed. > Also, could you please add tests for this new feature? Added! > P.S. Would it be possible for you to use the same email address in > your contributions as the one you used in the copyright assignment? > It will help us verify that your assignment is on file. Ah, sorry about that; replying from the email address from which I did the copyright assignment (which isn't subscribed to the mailing list though, so hopefully this gets through; I suppose I should switch which address is subscribed). --- lisp/emacs-lisp/text-property-search.el | 59 ++++++++++++++----- .../emacs-lisp/text-property-search-tests.el | 25 ++++++++ 2 files changed, 70 insertions(+), 14 deletions(-) diff --git a/lisp/emacs-lisp/text-property-search.el b/lisp/emacs-lisp/text-property-search.el index 7da02a9cb2..e4fcbf2ed1 100644 --- a/lisp/emacs-lisp/text-property-search.el +++ b/lisp/emacs-lisp/text-property-search.el @@ -30,7 +30,7 @@ beginning end value) (defun text-property-search-forward (property &optional value predicate - not-current) + not-current count) "Search for the next region of text where PREDICATE is true. PREDICATE is used to decide whether a value of PROPERTY should be considered as matching VALUE. @@ -59,6 +59,9 @@ text-property-search-forward region that doesn't include point and has a value of PROPERTY that matches VALUE. +If COUNT is a positive number, it will search forward COUNT times. If +negative, it will perform text-property-search-backwards -COUNT times. + If no matches can be found, return nil and don't move point. If found, move point to the end of the region and return a `prop-match' object describing the match. To access the details @@ -71,6 +74,9 @@ text-property-search-forward (when (> (length string) 0) (intern string obarray))))) (cond + ;; Negative count means search backwards + ((and (numberp count) (< count 0)) + (text-property-search-backward property value predicate not-current (- count))) ;; No matches at the end of the buffer. ((eobp) nil) @@ -83,25 +89,36 @@ text-property-search-forward (t (let ((origin (point)) (ended nil) + (count (or count 1)) + (match t) pos) - ;; Find the next candidate. + ;; Find the COUNT-th next candidate. (while (not ended) (setq pos (next-single-property-change (point) property)) (if (not pos) (progn (goto-char origin) - (setq ended t)) + (setq ended match)) (goto-char pos) (if (text-property--match-p value (get-text-property (point) property) predicate) - (setq ended - (text-property--find-end-forward - (point) property value predicate)) + (progn + (setq match (text-property--find-end-forward + (point) property value predicate)) + (setq origin (point)) + (cl-decf count) + (if (zerop count) + (setq ended match) + (setq pos (next-single-property-change (point) property)) + (if pos + (goto-char pos) + (goto-char origin) + (setq ended match)))) ;; Skip past this section of non-matches. (setq pos (next-single-property-change (point) property)) (unless pos (goto-char origin) - (setq ended t))))) + (setq ended match))))) (and (not (eq ended t)) ended))))) @@ -133,7 +150,7 @@ text-property--find-end-forward (defun text-property-search-backward (property &optional value predicate - not-current) + not-current count) "Search for the previous region of text whose PROPERTY matches VALUE. Like `text-property-search-forward', which see, but searches backward, @@ -144,6 +161,9 @@ text-property-search-backward (when (> (length string) 0) (intern string obarray))))) (cond + ;; Negative count means search forwards + ((and (numberp count) (< count 0)) + (text-property-search-forward property value predicate not-current (- count))) ;; We're at the start of the buffer; no previous matches. ((bobp) nil) @@ -165,26 +185,37 @@ text-property-search-backward (t (let ((origin (point)) (ended nil) + (count (or count 1)) + (match t) pos) (forward-char -1) - ;; Find the previous candidate. + ;; Find the COUNT-th previous candidate. (while (not ended) (setq pos (previous-single-property-change (point) property)) (if (not pos) (progn (goto-char origin) - (setq ended t)) + (setq ended match)) (goto-char (1- pos)) (if (text-property--match-p value (get-text-property (point) property) predicate) - (setq ended - (text-property--find-end-backward - (point) property value predicate)) + (progn + (setq match (text-property--find-end-backward + (point) property value predicate)) + (setq origin (point)) + (cl-decf count) + (if (zerop count) + (setq ended match) + (setq pos (previous-single-property-change (point) property)) + (if pos + (goto-char pos) + (goto-char origin) + (setq ended match)))) ;; Skip past this section of non-matches. (setq pos (previous-single-property-change (point) property)) (unless pos (goto-char origin) - (setq ended t))))) + (setq ended match))))) (and (not (eq ended t)) ended))))) diff --git a/test/lisp/emacs-lisp/text-property-search-tests.el b/test/lisp/emacs-lisp/text-property-search-tests.el index 90f06c3c4c..16ff6e87fd 100644 --- a/test/lisp/emacs-lisp/text-property-search-tests.el +++ b/test/lisp/emacs-lisp/text-property-search-tests.el @@ -79,12 +79,32 @@ text-property-search-forward-partial-non-current-bold-t '("bold2") 10)) +(ert-deftest text-property-search-forward-non-current-bold-t-count-1 () + (with-test (text-property-search-forward 'face 'bold t t 1) + '("bold1" "bold2"))) + +(ert-deftest text-property-search-forward-non-current-bold-t-count-2 () + (with-test (text-property-search-forward 'face 'bold t t 2) + '("bold2"))) + +(ert-deftest text-property-search-backwards-non-current-bold-t-count--negative-2 () + (with-test (text-property-search-backward 'face 'bold t t -2) + '("bold2"))) + +(ert-deftest text-property-search-forward-non-current-bold-t-count-many () + (with-test (text-property-search-forward 'face 'bold t t 100) + '("bold2"))) (ert-deftest text-property-search-backward-bold-t () (with-test (text-property-search-backward 'face 'bold t) '("bold2" "bold1") (point-max))) +(ert-deftest text-property-search-forward-bold-t-negative-count () + (with-test (text-property-search-forward 'face 'bold t nil -1) + '("bold2" "bold1") + (point-max))) + (ert-deftest text-property-search-backward-bold-nil () (with-test (text-property-search-backward 'face 'bold nil) '( "italic2 at the end" " and this is italic1" "This is ") @@ -110,6 +130,11 @@ text-property-search-backward-partial-non-current-bold-t '("bold1") 35)) +(ert-deftest text-property-search-backward-non-current-bold-t-count-2 () + (with-test (text-property-search-backward 'face 'bold t t 2) + '("bold1") + (point-max))) + (defmacro with-match-test (form beginning end value &optional point) `(with-temp-buffer (text-property-setup) -- 2.25.1