unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Isearch: add variable to control search match group
@ 2015-08-24 23:55 Alexander Shukaev
  2015-08-25 14:45 ` Stefan Monnier
  0 siblings, 1 reply; 4+ messages in thread
From: Alexander Shukaev @ 2015-08-24 23:55 UTC (permalink / raw)
  To: emacs-devel

Hello,

would it be reasonable to add

(defcustom isearch-regexp-group 0
  "Number used to match concrete group in search pattern."
  :group 'isearch
  :type 'number)

and substitute all the calls to `(match-beginning 0)' and `(match-end
0)' with `(match-beginning (or isearch-regexp-group 0))' and
`(match-end (or isearch-regexp-group 0))' accordingly in 'isearch.el'?

For instance, I develop my own search module on top of Isearch
currently, and I really have problem with the fact that the choice of
group 0 is hard-coded in Isearch.  Let me give you a simple example,
where this makes things stiff.  Note, though, that the following
example (and the corresponding change above) are meant for
programmatic use of Isearch in the first place (rather than
interactive one).  Imagine that my search module wants to search for

\(^\|\s-+?\)(;;)\($\|\s-+?\)

and I am actually only interested in the "(;;)" part since I also want
to highlight it (with both, current match highlighting and all matches
lazy highlighting).  Then, clearly, "\(^\|\s-+?\)" and "\($\|\s-+?\)"
are just anchors.  Also, note that I deliberately chosen non-word
constituent to prevent any speculations on the usage of "\<" and "\>"
for this example.  Now, since Emacs does not have Vim-like "\zs" and
"\ze" to control beginning and end of match respectively on the one
hand, and does not have something like (hypothetical, but very welcome
too) "\sb", which would essentially stand for "space boundary" (i.e.
do for spaces what "\<" and "\>" do for words) on the other hand (in
which case I would simply transform the original search RE into

\(^\|\sb\)(;;)\($\|\sb\)

and be done with it), I have no choice, but to use something like this

\(^\|\s-+?\)\(?1:(;;)\)\($\|\s-+?\)

and then extract exactly the 1st group from it.  Unfortunately,
Isearch internally does not support that at all.  Will it be possible
to follow my suggestion and/or maybe somebody has better ideas?

Regards,
Alexander



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Isearch: add variable to control search match group
  2015-08-24 23:55 Isearch: add variable to control search match group Alexander Shukaev
@ 2015-08-25 14:45 ` Stefan Monnier
  2015-08-25 15:06   ` Alexander Shukaev
  0 siblings, 1 reply; 4+ messages in thread
From: Stefan Monnier @ 2015-08-25 14:45 UTC (permalink / raw)
  To: Alexander Shukaev; +Cc: emacs-devel

> interactive one).  Imagine that my search module wants to search for

> \(^\|\s-+?\)(;;)\($\|\s-+?\)

> and I am actually only interested in the "(;;)" part since I also want
> to highlight it (with both, current match highlighting and all matches
> lazy highlighting).  Then, clearly, "\(^\|\s-+?\)" and "\($\|\s-+?\)"
> are just anchors.

How do you do that currently?  I'd expect that to do the above, you'd
set isearch-search-fun-function, and that function can take care of
shuffling the match-data so that the interesting part is
match-subgroup 0.


        Stefan



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Isearch: add variable to control search match group
  2015-08-25 14:45 ` Stefan Monnier
@ 2015-08-25 15:06   ` Alexander Shukaev
  2015-08-25 22:08     ` Stefan Monnier
  0 siblings, 1 reply; 4+ messages in thread
From: Alexander Shukaev @ 2015-08-25 15:06 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: emacs-devel

>> interactive one).  Imagine that my search module wants to search for
>
>> \(^\|\s-+?\)(;;)\($\|\s-+?\)
>
>> and I am actually only interested in the "(;;)" part since I also want
>> to highlight it (with both, current match highlighting and all matches
>> lazy highlighting).  Then, clearly, "\(^\|\s-+?\)" and "\($\|\s-+?\)"
>> are just anchors.
>
> How do you do that currently?  I'd expect that to do the above, you'd
> set isearch-search-fun-function, and that function can take care of
> shuffling the match-data so that the interesting part is
> match-subgroup 0.

(defun my-search-function
    (&optional forward regexp-p wrap-p)
  "Return search function.
If FORWARD is non-nil, search forward, otherwise backward.
If REGEXP-P is non-nil, input is treated as regular expression.
If WRAP-P is non-nil, search can wrap around top or bottom of buffer."
  `(lambda
       (string &optional bound noerror count)
     (let* ((point    (point))
            (function ',(if regexp-p
                            (if forward
                                #'re-search-forward
                              #'re-search-backward)
                          (if forward
                              #'search-forward
                            #'search-backward)))
            (result   (funcall function
                               string
                               bound
                               ,(if wrap-p t 'noerror)
                               count)))
       (when (and (null result) ,wrap-p)
         (goto-char ,(if forward '(point-min) '(point-max)))
         (unwind-protect
             (setq result (funcall function string bound noerror count))
           (unless result
             (goto-char point))))
       result)))

(defun devil-isearch-search-fun ()
  "Return Isearch-compatible search function.
Based on `isearch-forward' and `isearch-regexp'."
  (my-search-function isearch-forward
                      isearch-regexp
                      t))

(defun my-isearch-input
    (&optional string forward regexp-p history-p)
  "Search for (user-entered) input in specified direction."
  ...
  (let (...
        (isearch-search-fun-function #'my-isearch-search-fun)
        ...)
    ...
    (if forward (isearch-forward regexp-p) (isearch-backward regexp-p))
    ...))

Do you mean that I can modify `my-search-function' in such a way that
the calls to `match-beginning' and `match-end' with group 0 happening
inside and after calls to `isearch-forward' and `isearch-backward'
will return the positions of the middle group excluding anchors?  If
yes, then maybe you could show me how to achieve that?  Thanks.

Regards,
Alexander



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Isearch: add variable to control search match group
  2015-08-25 15:06   ` Alexander Shukaev
@ 2015-08-25 22:08     ` Stefan Monnier
  0 siblings, 0 replies; 4+ messages in thread
From: Stefan Monnier @ 2015-08-25 22:08 UTC (permalink / raw)
  To: Alexander Shukaev; +Cc: emacs-devel

> Do you mean that I can modify `my-search-function' in such a way that
> the calls to `match-beginning' and `match-end' with group 0 happening
> inside and after calls to `isearch-forward' and `isearch-backward'
> will return the positions of the middle group excluding anchors?

That's right.  E.g. (set-match-data (list (match-beginning 1) (match-end
1))) or (set-match-data (cddr (match-data))) should get you started.


        Stefan



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2015-08-25 22:08 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-08-24 23:55 Isearch: add variable to control search match group Alexander Shukaev
2015-08-25 14:45 ` Stefan Monnier
2015-08-25 15:06   ` Alexander Shukaev
2015-08-25 22:08     ` Stefan Monnier

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).