[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [h-e-w] Find sexp at cursor
From: |
Christopher J. White |
Subject: |
Re: [h-e-w] Find sexp at cursor |
Date: |
19 Nov 2001 16:07:42 -0500 |
User-agent: |
Gnus/5.0808 (Gnus v5.8.8) Emacs/20.7 |
>>>>> "david" == Gallucci, David <address@hidden> writes:
david> This is a piece of lisp I wrote to find current sexp at cursor. The
problem
david> is with substrings. For instance, if the current sexp under the cursor is
david> foo, running this code will advance to the next instance of foo as
expected.
david> If I execute again, it advances to foo-bar, since the foo substring is
david> actually part of foo-bar. But now all subsequent searches are looking for
david> foo-bar, and not the originally intended string "foo". It seems that I
need
david> to somehow persist the orignal search string between calls.
Use context to determine if this is a repeat. This requires the
a variable to keep track of the last search which I'll all dg-f-search-word.
The variable is only changed if it's values is not a substring
of the current-word. For example, first search sets var to "foo"
and moves forward to "foobar". Second search sees that var is "foo"
and is a substring of "foobar" so leaves var alone and searchs for
next occurence of "foo".
You can force a new search by passing a non-nil prefix arg.
(defvar dg-f-search-word nil)
(defun dg-f-search-sexp-at-cursor (force-new)
"Searches forward for word under cursor. If the last word searched
for is a substring of the current word, leaves the search
word as is (the substring not the current-word). Pass a non-nil
prefix arg to force a new search word."
(interactive "P")
(let ((word dg-f-search-word) (cword (current-word)))
(cond
((or force-new
(null word)
(not (eq 0 (string-match (regexp-quote word) cword))))
(setq dg-f-search-word cword
word cword)
(message "Searching for word \"%s\"" word)))
(if (looking-at (regexp-quote word))
(forward-char))
(if (not (search-forward word nil t))
(message (format "\"%s\" not found." word)))))
Minor comment, I imagined that you wanted to use this to move to the
next occurence of the word currently at the cursor. As you had it
defined, this required calling the function twice if you were
currently positioned at the beginning of the word. I added
the (looking-at) line above to detect this case.
...cj
--
------------------------------------------------------------------------------
Christopher J. White address@hidden
------------------------------------------------------------------------------