unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Custom searches, like interactively searching palindromes
@ 2010-10-01  9:50 Karan Bathla
  2010-10-01 13:36 ` Andreas Röhler
  0 siblings, 1 reply; 3+ messages in thread
From: Karan Bathla @ 2010-10-01  9:50 UTC (permalink / raw)
  To: help-gnu-emacs


Hi

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

One approach would be to use \, to embed a lisp function, like it's availabe in regex-replace. Is it easy to modify existing functions (or make new ones using the ones available) to do this?

Or maybe something like (check-for-pattern palindrome), where palindrome is a function that takes a string and returns true if that string is palindrome. The function check-for-pattern does the search interactively feeding new string in the buffer to palindrome and highlighting them if they match. The problem I am facing here is writing the check-for-pattern function using the existing search functions.

Any other ways to do it ?

Thanks 
Karan


      



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

* Re: Custom searches, like interactively searching palindromes
  2010-10-01  9:50 Karan Bathla
@ 2010-10-01 13:36 ` Andreas Röhler
  0 siblings, 0 replies; 3+ messages in thread
From: Andreas Röhler @ 2010-10-01 13:36 UTC (permalink / raw)
  To: help-gnu-emacs

Am 01.10.2010 11:50, schrieb Karan Bathla:
>
> Hi
>
> I would like to know how one can interactively search for patterns that are not captured by regexes, like palindromes.
>
> One approach would be to use \, to embed a lisp function, like it's availabe in regex-replace. Is it easy to modify existing functions

  (or make new ones using the ones available) to do this?
>

With Emacs Lisp knowledge you can do this at a reasonable effort.
It's just for tasks like this, Emacs is the most perfect general tool IMHO.

The knowledge given, you may do the basics within minutes.
Finishing it will be some kind of tricky, as written language commonly 
is not so regular.

It depends how reliable your results must be, which language, the kind 
of texts etc.


> Or maybe something like (check-for-pattern palindrome), where palindrome is a function that takes a string and returns true if that

string is palindrome. The function check-for-pattern does the search 
interactively feeding new string in the buffer to palindrome

and highlighting them if they match. The problem I am facing here is 
writing the check-for-pattern function using the existing

search functions.

Should you encounter that kind of tasks repeatedly, read the Emacs Lisp 
Doku, resp. the Intro first, if you are not a programmer.

Maybe some person here on this list don't know what to do some evening 
and writes it for you...

Good luck


Andreas

--
https://code.launchpad.net/~a-roehler/python-mode/python-mode-components
https://code.launchpad.net/s-x-emacs-werkstatt/




>
> Any other ways to do it ?
>
> Thanks
> Karan
>
>
>
>
>




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

* Re: Custom searches, like interactively searching palindromes
       [not found] <mailman.1.1285926611.30610.help-gnu-emacs@gnu.org>
@ 2010-10-01 13:58 ` Andreas Politz
  0 siblings, 0 replies; 3+ messages in thread
From: Andreas Politz @ 2010-10-01 13:58 UTC (permalink / raw)
  To: help-gnu-emacs

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


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

end of thread, other threads:[~2010-10-01 13:58 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <mailman.1.1285926611.30610.help-gnu-emacs@gnu.org>
2010-10-01 13:58 ` Custom searches, like interactively searching palindromes Andreas Politz
2010-10-01  9:50 Karan Bathla
2010-10-01 13:36 ` Andreas Röhler

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