In my .emacs file I have the following function:
; Search for token forward and backward
(defun search-token-forward()
"Search forward for the ucode token at the point."
(interactive) ; Makes the function available as a command
(let (target-string)
(setq target-string (buffer-substring
(+ (re-search-backward "[^A-Za-z0-9/_.@#\$]") 1)
(- (re-search-forward "[^A-Za-z0-9/_.@#\$]" nil t 2) 1)))
; post a message saying what we're looking for
(message "Search for \`%s\`" target-string)
; (setq case-fold-search nil)
(search-forward target-string)))
(global-set-key [f5] 'search-token-forward)
(defun search-token-backward()
"Search backward for the ucode token at the point."
(interactive)
(point)
(let (target-string)
(setq target-string (buffer-substring
(+ (re-search-backward "[^A-Za-z0-9/_.@#\$]") 1)
(- (re-search-forward "[^A-Za-z0-9/_.@#\$]" nil t 2) 1)))
(message "backward search for \`%s\`" target-string)
; (setq case-fold-search nil)
;
; here the search for the target string is done twice. Once
; for the string that the cursor is on, and once more to go
; backwards for the next occurance.
;
(search-backward target-string nil nil 2)))
(global-set-key [C-f5] 'search-token-backward)
After the first search the cursor moves till after the last character of the expression, which is fine since I can still do "search-token-backward" using another function that I have. Unfortunately, when searching after the last occurrence of the expression, the cursor moves to the end of line and so I can't smoothly try to search backwards since the cursor is not on the expression "position". What is causing this behavior and how is it possible to fix it?
Basically I would like the cursor to stay at the space after the last character of the expression even when there are no forward occurrences of it.
Here is an example: In the next code while the cursor is on the function foo_func I am clicking f5 which is tied to search-token-forward. The X marks where the cursor is after each click:
int foo_func(int w, int y);
x cursor here to begin with.
some code...
foo_func(1, 2);
x After first click
some code...
foo_func(3,4);
x after second click
some code...
return foo_func(5,6);
x after third click
There are now no more occurences of foo_func in the file, so hitting f5 one more time moves the cursor one character to the right, like so:
return foo_func(5,6);
x after forth click, this is the same line above.
So I can't simply hit ctrl-f5 to search backward for foo_func because the cursor is no more adjacent to the function.