all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Andreas Politz <politza@fh-trier.de>
To: help-gnu-emacs@gnu.org
Subject: Re: Custom searches, like interactively searching palindromes
Date: Fri, 01 Oct 2010 15:58:24 +0200	[thread overview]
Message-ID: <87mxqyf20v.fsf@fh-trier.de> (raw)
In-Reply-To: mailman.1.1285926611.30610.help-gnu-emacs@gnu.org

Karan Bathla <karan_goku@yahoo.com> writes:

>
> I would like to know how one can interactively search for patterns
> that are not captured by regexes, like palindromes.
>

I had a similar desire and wrote this 2 functions, which can be
used to do what you want, though not for replacing.

(defun isearch-with-predicate (predicate indicator &optional backward regexp)
  (lexical-let ((predicate predicate))
    (let ((isearch-search-fun-function
           (lambda nil
             (lambda (string &optional bound no-error)
               (let (isearch-search-fun-function)
                 (search-with-predicate
                  string (isearch-search-fun)
                  predicate bound no-error)))))
          (isearch-message-prefix-add indicator)
          isearch-with-predicate-success
          (isearch-mode-end-hook
           (lambda nil
             (setq isearch-with-predicate-success
                   (not isearch-mode-end-hook-quit)))))
      (funcall (if backward
                   (if regexp
                       'isearch-backward-regexp
                     'isearch-backward)
                 (if regexp
                     'isearch-forward-regexp
                   'isearch-forward)))
      (isearch-clean-overlays)
      isearch-with-predicate-success)))

(defun search-with-predicate (string
                              search-fn
                              predicate
                              &optional
                              bound noerror count)
  (let (found
        limit
        (count (or count 1)))
    (save-excursion
      (while (and (setq found
                        (funcall search-fn string bound noerror))
                  (or (not (setq found
                                 (and (funcall predicate)
                                      found)))
                      (> (decf count) 0))))
      (setq limit (point)))
    (if found
        (goto-char found)
      (unless (eq noerror t)
        (goto-char limit)
        nil))))

(defun isearch-palindrome (&optional regexp)
  (interactive "P")
  (isearch-with-predicate
   (lambda ()
     (let* ((word (buffer-substring (match-beginning 0) (match-end 0)))
            (prefix (append (substring word 0 (/ (length word) 2)) nil))
            (suffix (nreverse (append (substring word (/ (length word) 2)) nil))))
       (every '= prefix suffix)))
   "(Palindrome)" nil regexp))

-ap


       reply	other threads:[~2010-10-01 13:58 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <mailman.1.1285926611.30610.help-gnu-emacs@gnu.org>
2010-10-01 13:58 ` Andreas Politz [this message]
2010-10-01  9:50 Custom searches, like interactively searching palindromes Karan Bathla
2010-10-01 13:36 ` Andreas Röhler

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87mxqyf20v.fsf@fh-trier.de \
    --to=politza@fh-trier.de \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.