help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: search forward at point question


From: Xah Lee
Subject: Re: search forward at point question
Date: Fri, 6 Mar 2009 09:21:34 -0800 (PST)
User-agent: G2/1.0

On Mar 4, 10:42 pm, Maindoor <address@hidden> wrote:
> Hi,
>      I have the following code to search forward from point.
>      But If I have a string like SOMETHING_HERE=$(HI)
>
>      then this code yanks the whole "SOMETHING_HERE=$"
>      and searches for it forward. How do I limit it to
>      "SOMETHING_HERE". I want it to stop if it encounters
>      any special characters except "_" and "-".
>
>      Here is the code:
>
> (defun my-viper-search-yank-word (arg forward)
>   "Search forward for ARG occurance of word under point.
> If FORWARD is nil, searches backward instead."
>   (let ((viper-re-search t))
>     (viper-search
>      (concat "\\<" (regexp-quote (current-word)) "\\>") forward arg)))
>
> (defun my-viper-search-forward-yank-word (arg)
>   "Search forward for ARG occurance of word under point.
> Like the Vim command \"*\" (but not exactly)."
>   (interactive "P")
>   (my-viper-search-yank-word arg t))

As Barry Margolin suggested, you might try to use the optional
paramter in the function current-word.

However, as Barry also suggested, current-word relies on emacs's
syntax table. Which means, whether “_” or “-” or any other such as “=”
is considered part of the word depends on the current major mode.

You could, write your own so that your function always works on
alphanumerics plus “_” and “-” regardless what syntax table there is.
(This problem also bugged me a few times. Emacs's concept syntax table
seems to me questionable. It is not really powerful enough to model
most coding problems dealing with syntax, yet it does gets in the way
sometimes, and is quite complex to use.)

To code your own current-word, just use search-backward-regexp with a
regex something like [-_A-Za-z], then get the cursor position. Do same
by search forward. Then, use buffer-substring-no-properties to grab
the string.

  Xah
∑ http://xahlee.org/

reply via email to

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