Macro get_word()?
Tony Balinski
ajbj at free.fr
Mon Jan 22 16:26:29 CET 2007
Quoting YT Lim <ytlim23 at yahoo.com.au>:
> > > The following looks for word ends rather than
> > delimiting spaces.
> > ...
> > Oops, sent too soon!
> > ...
> > > define get_word_at_pos
> > > {
> > > pos = $cursor
> > > if ($n_args > 0)
> > > pos = $1
> > > string = ""
> > > use_string = 0
> > > if ($n_args > 1)
> > > {
> > > string = $1
> > > use_string = 1
> > > }
> > ...
> > string = $1
> > should read
> > string = $2
>
> Thanks for the response.
>
> You are too fast for me. Could you please explain a
> little for me? I can't understand the purpose of
> use_string. Is the "word" stored in string?
The idea in the function is to be able to extract a word either from
a string (if supplied) or from the document's text. If you supply
no parameters or just a position (pos), use_string will be set to zero
and the document's text will be used; if you supply a position (pos)
and a string, use_text will be set to one and the attempt to find the
word will use the position pos in the string instead. I had only ever
used the function with the document's text, so I never noticed that it
wouldn't work when supplying a string instead... So use_string is just
an internal boolean, and string is ignored if use_string is zero
(false).
The calls are as follows:
get_word_at_pos(): returns the word found under the cursor in the
editor window (current document)
get_word_at_pos(pos): returns the word found at position pos in the
editor window (current document)
get_word_at_pos(pos, str): returns the word found at position pos
in the string str
So these two calls have the same result:
word = get_word_at_pos()
word = get_word_at_pos($cursor, get_range(0, $text_length))
except that the second copies your document into a string (returned
from get_range()), and looks inside the string - so it's less efficient.
HTH
Tony
More information about the Discuss
mailing list