From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Gabriele Nicolardi Newsgroups: gmane.emacs.bugs Subject: bug#69542: Feature request: making occur obey isearch-filter-predicate Date: Tue, 5 Mar 2024 18:11:37 +0100 Message-ID: References: <6071eb9a-fa15-45fb-9917-7a35c7556680@medialab.sissa.it> <86msrdnjdn.fsf@mail.linkov.net> Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="------------iVWKVnOq03f7hFV9tkDybpPV" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="2248"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Mozilla Thunderbird Cc: 69542@debbugs.gnu.org To: Juri Linkov Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Tue Mar 05 18:13:01 2024 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 1rhYLc-0000Nd-Vi for geb-bug-gnu-emacs@m.gmane-mx.org; Tue, 05 Mar 2024 18:13:01 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rhYLD-0002Iu-Vh; Tue, 05 Mar 2024 12:12:36 -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 1rhYLA-0002IQ-Rf for bug-gnu-emacs@gnu.org; Tue, 05 Mar 2024 12:12:32 -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 1rhYLA-0006sy-Jk for bug-gnu-emacs@gnu.org; Tue, 05 Mar 2024 12:12:32 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1rhYLe-0004ao-7D for bug-gnu-emacs@gnu.org; Tue, 05 Mar 2024 12:13:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Gabriele Nicolardi Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Tue, 05 Mar 2024 17:13:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 69542 X-GNU-PR-Package: emacs Original-Received: via spool by 69542-submit@debbugs.gnu.org id=B69542.170965874617605 (code B ref 69542); Tue, 05 Mar 2024 17:13:02 +0000 Original-Received: (at 69542) by debbugs.gnu.org; 5 Mar 2024 17:12:26 +0000 Original-Received: from localhost ([127.0.0.1]:48869 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rhYL0-0004Zn-Gz for submit@debbugs.gnu.org; Tue, 05 Mar 2024 12:12:26 -0500 Original-Received: from smtp05.cbsolt.net ([185.97.217.44]:44380) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rhYKv-0004ZX-Gx for 69542@debbugs.gnu.org; Tue, 05 Mar 2024 12:12:21 -0500 Original-Received: from [10.0.2.15] (host-79-16-242-199.retail.telecomitalia.it [79.16.242.199]) by smtp05.cbsolt.net (Postfix) with ESMTPSA id 4Tq2Cv5sQtz3wfg; Tue, 5 Mar 2024 18:11:39 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cbsolt.net; s=201504-di4k2w; t=1709658701; bh=eIlXfByBNEMl3WxVbnT7BYgNoeK1dlLfUQU19b9TJhs=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=J0v9hwApmT9KjzAfjBryO08/j9Voi6MHot6n8Wt3ETNunCfP5m4DAz+U17Gm4MTBR TPcNgGKPuu+FdDSz9MqBEJu/YPYCGQmALa1ysvDLV4qRzOOHGkpbuHu8reXqryNEeQ quu5bhSk+nXb7bk/+L4/DQkOGea9MPosxv5O4o8A= Content-Language: en-US In-Reply-To: <86msrdnjdn.fsf@mail.linkov.net> 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:281070 Archived-At: This is a multi-part message in MIME format. --------------iVWKVnOq03f7hFV9tkDybpPV Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit > I think duplicating the whole body of functions is not better > than using advice. Until yesterday, I had no idea how to advise a function. Now I'm experimenting with this code to obtain modified versions of |occur| and |how-many| that are sensitive to |isearch-filter-predicate|. I'm not a programmer, so my code may not be very rigorous: ;; I need a copy of `re-search-forward' because it will be temporary adviced in ;; `how-many-ifp' and `occur-ifp' functions: (defalias 're-search-forward-copy   (symbol-function 're-search-forward)   "Copy of `re-search-forward'") (defun re-search-forward-ifp (REGEXP &optional BOUND NOERROR COUNT)   "Modified version of `search-forward-regexp' that filters (skips) matches according to `isearch-filter-predicate'."   (let ((POINT (point)))     (catch 'filtered       (while (re-search-forward-copy REGEXP BOUND NOERROR COUNT)         (let ((B (match-beginning 0))               (E (match-end 0)))           (when (funcall isearch-filter-predicate B E)             (throw 'filtered (point)))))       (goto-char POINT)       nil))) (defalias 'search-forward-regexp-ifp 're-search-forward-ifp) (defun override-re-search-forward (orig-fun &rest args)   "Temporary advice the `re-search-forward' function, overriding it with `re-search-forward-ifp' to make it sensitive to `isearch-filter-predicate'"   (unwind-protect       (progn         (advice-add 're-search-forward :override #'re-search-forward-ifp)         (apply orig-fun args))     (advice-remove 're-search-forward #'re-search-forward-ifp))) (defalias 'how-many-ifp   (symbol-function 'how-many)   "Copy of the `how-many' function (to be) adviced to obey to `isearch-filter-predicate'") (defalias 'count-matches-ifp 'how-many-ifp) (advice-add 'how-many-ifp :around #'override-re-search-forward) (defalias 'occur-ifp   (symbol-function 'occur)   "Copy of the `occur' function (to be) adviced to obey to  `isearch-filter-predicate'") (advice-add 'occur-ifp :around #'override-re-search-forward) The key is the `re-search-forward` function used in `how-many` and `occur` definition. If you make it sensible to `isearch-filter-predicate` also `how-many` and `occur` will be. I don't know if advicing a function to advice a function inside it is a good idea but it seem to work (I need more tests). Now I discovered how to build these functions I could be fine with my modified ones, but making this feature avaible (maybe not default) in the original functions could be a better idea. > An alternative would be to add a new variable > 're-search-forward-function' and then to use it like > '(funcall re-search-forward-function)'. The idea of a new function seems good to me. --------------iVWKVnOq03f7hFV9tkDybpPV Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit
I think duplicating the whole body of functions is not better
than using advice. 

Until yesterday, I had no idea how to advise a function.

Now I'm experimenting with this code to obtain modified versions of occur and how-many that are sensitive to isearch-filter-predicate.

I'm not a programmer, so my code may not be very rigorous:

;; I need a copy of `re-search-forward' because it will be temporary adviced in
;; `how-many-ifp' and `occur-ifp' functions:
(defalias 're-search-forward-copy
  (symbol-function 're-search-forward)
  "Copy of `re-search-forward'")

(defun re-search-forward-ifp (REGEXP &optional BOUND NOERROR COUNT)
  "Modified version of `search-forward-regexp' that filters (skips) matches
according to `isearch-filter-predicate'."
  (let ((POINT (point)))
    (catch 'filtered
      (while (re-search-forward-copy REGEXP BOUND NOERROR COUNT)
        (let ((B (match-beginning 0))
              (E (match-end 0)))
          (when (funcall isearch-filter-predicate B E)
            (throw 'filtered (point)))))
      (goto-char POINT)
      nil)))
(defalias 'search-forward-regexp-ifp 're-search-forward-ifp)

(defun override-re-search-forward (orig-fun &rest args)
  "Temporary advice the `re-search-forward' function, overriding it with
`re-search-forward-ifp' to make it sensitive to `isearch-filter-predicate'"
  (unwind-protect
      (progn
        (advice-add 're-search-forward :override #'re-search-forward-ifp)
        (apply orig-fun args))
    (advice-remove 're-search-forward #'re-search-forward-ifp)))

(defalias 'how-many-ifp
  (symbol-function 'how-many)
  "Copy of the `how-many' function (to be) adviced to obey to
`isearch-filter-predicate'")
(defalias 'count-matches-ifp 'how-many-ifp)

(advice-add 'how-many-ifp :around #'override-re-search-forward)

(defalias 'occur-ifp
  (symbol-function 'occur)
  "Copy of the `occur' function (to be) adviced to obey to
 `isearch-filter-predicate'")

(advice-add 'occur-ifp :around #'override-re-search-forward)

The key is the `re-search-forward` function used in `how-many` and `occur` definition. If you make it sensible to `isearch-filter-predicate` also `how-many` and `occur` will be.

I don't know if advicing a function to advice a function inside it is a good idea but it seem to work (I need more tests).

Now I discovered how to build these functions I could be fine with my modified ones, but making this feature avaible (maybe not default) in the original functions could be a better idea.

 An alternative would be to add a new variable
're-search-forward-function' and then to use it like
'(funcall re-search-forward-function)'.

The idea of a new function seems good to me.


--------------iVWKVnOq03f7hFV9tkDybpPV--