That's also a good way, braunbaer! The two methods seem to work equally well. Thank you!
As I continue to refine and experiment, I'm (again) reaching the limits of my ability to sort things out with RegExMatch.
My immediate goal is to move the cursor left to the start of the current sentence. I assume all sentences end in a period, exclamation mark, or a question mark. The sentence might appear at the start of a paragraph, in the middle of a paragraph, or at the end of a paragraph. For example, here are two paragraphs I'm using for testing.
Paragraph one. Hello! Are you there? I am wherever.
Paragraph two. Goodbye! Were you there? I am whatever.
If the cursor is within the sentence "Are you there?" the cursor ends up to the left of the "A" in "Are". If the cursor is within the sentence "Goodbye!" the cursor ends up to the left of "G" in "Goodbye".
My approach is to hunt for text that precedes the start of the sentence, which is a period, question mark, or exclamation mark, followed by a whitespace character.
Here's the logic I'm trying to encode.
0. Manually place the cursor in the middle of a sentence.
1. Select left to the start of the document or field: Shift + Ctrl + Home. (I would prefer to select to the start of the paragraph with Shift + Ctrl + Up, but this hotkey is not supported in Notepad or in some text entry fields.)
2. Copy to clipboard, and place in a variable.
3. Deselect by pressing Right arrow. The cursor is in the same location as in Step 0.
4. Get the length of the variable.
5. Get the position of the end of the previous sentence.
6. Subtract one from the other.
7. Move the cursor left that amount.
The problem: The script works with sentences in the first paragraph, but not for sentences in the second paragraph (or in subsequent paragraphs, if any). Instead, the cursor ends up somewhere near the end of the first paragraph.
My guess is that carriage returns are messing with RegexMatch's ability to identify the correct match, although I don't understand why this would happen.
Is there a way to adjust the statement (or my script!) so that the end-of-sentence match is found regardless of which paragraph is in?
Code: Select all
!6::
Clipboard := ""
SendInput ^+{Home}^c
ClipWait 1
SendInput {Right}
Top := Clipboard
Sleep, 100
; TopPos := RegExMatch(Top, "[\.!?]\s(?!.*[\.!?]\s)") ; Both versions of this statement work equally well
TopPos := RegExMatch(Top, ".*\K[\.!?]\s")
TopLength := StrLen(Top)
Offset := TopLength - TopPos
SendInput {Left %Offset%}
Return