How to write a function where whenever a variable is found, it returns t (in order to allow a loop):
(setq x 1)
(while ("backward search for regexp "%x" equals true") ;where x is variable
(setq x (+ x 1))
(insert (concat "%" (int-to-string x)))
)
Example: If %1 (x=1) is found, it will add 1 to x. If %2 (x=2) is found, it will add 1 to x.
Let’s say %3 is not found in a backward search, the while loop stops and “%” + “3” is inserted (%3).
I just don’t understand the how to return true on a backward-search.
search-backwardtakes an optional third argument which, when non-nil, tells it to return nil in case the search was unsuccessful:Now, if I try to understand what you really want to do (something like inserting at point the first
%dstring which doesn’t appear before), then you might want to wrap the search inside asave-excursionform to avoid moving the point: