emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Isearch: add variable to control search match group


From: Alexander Shukaev
Subject: Re: Isearch: add variable to control search match group
Date: Tue, 25 Aug 2015 17:06:37 +0200

>> 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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]