"text"
[text]
(text)
%text%
When text is not selected, the scripts insert the two symbols. For example:
""
[]
()
%%
With unselected text, the script moves the cursor between the two symbols.
The scripts are close to 100% reliable when text is not selected. But the scripts fail about 20% of the time when I act on selected text. The failure: the two symbols are inserted, but without the selected text.
I've toyed with logic tweaks and timing changes in an effort to increase reliability. But I haven't been able to discover a "magic bullet" that will cure this problem. Any ideas?
Code: Select all
; -------------------
; All of my hotkey-activated text wrapping macros are in this section.
!Q:: ; Alt + Q
Opener := Chr(34) ; Quotation marks
Closer := Chr(34)
Gosub, TextWrapMain
return
; -------------------
![:: ; Alt + [
Opener := Chr(91) ; [ ]
Closer := Chr(93)
Gosub, TextWrapMain
return
; -------------------
!9:: ; Alt + (
Opener := Chr(40) ; ( )
Closer := Chr(41)
Gosub, TextWrapMain
return
; etc.
; -------------------
; This subroutine is the "engine" for the scripts in the above section.
TextWrapMain:
ClipSaved := ClipboardAll ; Store the current clipboard
clipboard := ""
SendInput ^c
; Try increasing Clipwait delay to prevent clipboard from getting inserted rather than the selection!
; ClipWait, 0.2 ; Occasionally fails with 0.3, 0.4, 0.5, and 0.8 But maybe failure is inevitable?
ClipWait, 0.6
; Sleep 200 ; This pause was a desperate measure, but it does not seem to matter now!
if ErrorLevel ; ErrorLevel is TRUE implies the clipboard did NOT become populated, which means nothing was selected.
{
SendInput {Text}%Opener%%Closer% ; "{Text}" is needed to handle unescaped curly braces { and }
SendInput {Left}
}
Else
{
clipboard := Trim(clipboard, " `t`r`n") ; Trim spaces, tabs, CRs, and NLs
clipboard := Opener clipboard Closer
SendInput {Del}^v ; Sometimes the selection is not overwritten. Deleting it helps.
}
; Delay needed for reliability. Without, the original clipboard (or nothing) is inserted instead of the selection!
; Sleep 200
Sleep 800
clipboard := ClipSaved ; Restore the original clipboard.
ClipSaved := "" ; Free memory in case the original clipboard was very large.
Return