I’m not exactly a lisp expert, so please forgive a fairly newbie question.
I’m writing a fairly simple elisp function, trying to find a short string on the same line as the current cursor position. The relevant part of the code as written now is:
(let ((matchpos (search-forward myword (line-end-position) t)))
(if matchpos
(goto-char (- matchpos (length myword)))
(setq matchpos (search-backward myword (line-beginning-position) t)))
...
This mostly works without trouble, but it fails to find a string containing the current cursor position. Apparently, search-backward only looks for strings that end before point. In truth, I can solve this particular problem by going to the end of the line before doing the search-backward, but I’m rather curious whether this behavior can be modified.
Is there any variant on search-backward that matches any string that starts before point?
Edit: Perhaps an example is in order, since those answering don’t seem to quite understand the question. Suppose I am searching for the string “search this”, and my line looks like this (point noted by the *):
I want to search this string, I have code to search t*his string
Unfortunately, the code fragment shown above moves point to the first occurrence of “search this” in the line above. I want point to end up at the beginning of the second “search this” string, the one currently containing point, since it’s the nearest one.
I would suggest that you move the cursor to the right the number of characters that corresponds to the length of the string you search for (possibly minus one, depending on if you would like to match something immediately to the right of the point).
Something like:
This will work, as you have moved the point out of the way for
search-backword.Another solution would be to go to the beginning of the line and repeatedly search forward until you have passed the point.