From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: James N V Cash Newsgroups: gmane.emacs.devel Subject: [PATCH] Add option COUNT argument to text-property-search functions Date: Fri, 30 Jul 2021 15:20:59 -0400 Message-ID: <87im0rlias.fsf@gmail.com> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="35524"; mail-complaints-to="usenet@ciao.gmane.io" To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Fri Jul 30 21:21:53 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 1m9Y4v-00098F-DX for ged-emacs-devel@m.gmane-mx.org; Fri, 30 Jul 2021 21:21:53 +0200 Original-Received: from localhost ([::1]:57158 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1m9Y4u-0007g2-89 for ged-emacs-devel@m.gmane-mx.org; Fri, 30 Jul 2021 15:21:52 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:40068) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m9Y4B-0006yb-8R for emacs-devel@gnu.org; Fri, 30 Jul 2021 15:21:07 -0400 Original-Received: from mail-qt1-x831.google.com ([2607:f8b0:4864:20::831]:40456) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1m9Y47-0003r6-UX for emacs-devel@gnu.org; Fri, 30 Jul 2021 15:21:06 -0400 Original-Received: by mail-qt1-x831.google.com with SMTP id m11so7185448qtx.7 for ; Fri, 30 Jul 2021 12:21:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:mime-version; bh=LNDF9QfXfjnTKm54rrmX31U3DrxjQQ3gdq/gfRo6RMs=; b=jDjEKqLsCIDe/RhtwPEiNS8vH1QZ6Oxd7/soWsDLzNkVShwbnkWjOVr+a6FtFH0h+p kpn186NzykokUYMU8LpO/vwJa7b/Gj4duKpfNubiwCyAFylrk3pfEeA6146vyfjVZtCJ lwrB7kZ2ozJdVfZRSVfIrtBk/OPB9ihgCxbqF+2gbuDZHLL6X8ceJ/nFIgOYGJm6zvE9 grti3mQ9zIHN/RxpG48hxK3v/62T8aZFaAFb3D6nsBlBvk5DgOX6Lz2ngjjtCCEsEy66 xey24E5MVqq9VhecWE+U5BioygEebHknFRzmpspJvQxGJU8hAgu0ik35JmAP25gW74TC Hv3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :mime-version; bh=LNDF9QfXfjnTKm54rrmX31U3DrxjQQ3gdq/gfRo6RMs=; b=Uogs2phh1QbhiXzsTChqFKo7pttdSDeZoJzrq8KmBbQ/l7C4d/Ov2fNv/KnvFctG6Z 5ty8rxRhxv2bCmOy0L1fX5ntG60xUutPwXf5gyPHaPOpORE1eK9Mv69stwPZ01JmLGUf zQAlFIgLR/T7hW/0EooFt7bjnFdaSsNeCTh6IJMIu7WRk1zxLQKPPHOXuRtprSaDJ1Uy qlJ4CXQFvDdSnoRk7ytWFuzDHEJr5EPSGoJN/BY/foZicfuzq6cMA5RnsEaTcMn1QM6R 9tlnK1ECMNkafQRapCM6lFN0wetu3XfeH3tt05yOKrA7vfq5mX9vPu/9ct9owyA6EfQU JBgA== X-Gm-Message-State: AOAM531pUcOzMdAyr2ddkAXHXfH3LseiFxxqbXHD5c3VkOa9qGeBSI4T 9TxUVCVurZHsqy+BKP6lVpHt7eIUidgbVg== X-Google-Smtp-Source: ABdhPJzM119L3R8T/fCWT7rHb/epfG95jtVEuBdyERT+6WukmQP8J2vh0vmmV833V+nqMAYb6If9Uw== X-Received: by 2002:ac8:d4a:: with SMTP id r10mr3653135qti.245.1627672860188; Fri, 30 Jul 2021 12:21:00 -0700 (PDT) Original-Received: from gonk ([2607:fea8:10c1:8b00:1439:7588:85ce:969f]) by smtp.gmail.com with ESMTPSA id l14sm1022319qtj.26.2021.07.30.12.20.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Jul 2021 12:20:59 -0700 (PDT) Received-SPF: pass client-ip=2607:f8b0:4864:20::831; envelope-from=james.nvc@gmail.com; helo=mail-qt1-x831.google.com X-Spam_score_int: -14 X-Spam_score: -1.5 X-Spam_bar: - X-Spam_report: (-1.5 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FORGED_FROMDOMAIN=0.249, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.248, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no 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:271868 Archived-At: This allows searching forward for the Nth occurance of the text property, analogously to re-search-forward/backward. --- lisp/emacs-lisp/text-property-search.el | 59 +++++++++++++++++++------ 1 file changed, 45 insertions(+), 14 deletions(-) diff --git a/lisp/emacs-lisp/text-property-search.el b/lisp/emacs-lisp/text-property-search.el index 7da02a9cb2..fa51488135 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 @@ -74,6 +77,9 @@ text-property-search-forward ;; No matches at the end of the buffer. ((eobp) nil) + ;; Negative count means search backwards + ((and (numberp count) (< count 0)) + (text-property-search-backward property value predicate not-current (- count))) ;; We're standing in the property we're looking for, so find the ;; end. ((and (text-property--match-p value (get-text-property (point) property) @@ -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, @@ -147,6 +164,9 @@ text-property-search-backward ;; We're at the start of the buffer; no previous matches. ((bobp) nil) + ;; Negative count means search forwards + ((and (numberp count) (< count 0)) + (text-property-search-forward property value predicate not-current (- count))) ;; We're standing in the property we're looking for, so find the ;; end. ((text-property--match-p @@ -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))))) -- 2.25.1