Skip to content

Instantly share code, notes, and snippets.

@davebrny
Last active September 7, 2024 10:52
Show Gist options
  • Save davebrny/088c48d6678617876b34f53571e92ee6 to your computer and use it in GitHub Desktop.
Save davebrny/088c48d6678617876b34f53571e92ee6 to your computer and use it in GitHub Desktop.
πŸ“‡ (autohotkey) - wrap selected text in *symbols*
/*
[script info]
version = 2.5
description = wrap selected text in %symbols%
author = davebrny
source = https://gist.github.com/davebrny/088c48d6678617876b34f53571e92ee6
*/
sendMode input
return ; end of auto-execute
;---------------------------
!2::
::w2::
goTo, wrap_quote ; "text"
!'::
::w'::
goTo, wrap_apostrophe ; 'text'
!`::
::w``::
goTo, wrap_grave ; `text`
!9::
::w9::
goTo, wrap_parenthesis ; (text)
![::
::w[::
goTo, wrap_bracket ; [text]
!]::
::w]::
goTo, wrap_brace ; {text}
!,::
::w,::
goTo, wrap_angle ; <text>
!5::
::w5::
goTo, wrap_percent ; %text%
!.::
::w.::
goTo, wrap_2_angle ; >>text<<
!8::
::w8::
goTo, wrap_asterisk ; *text*
+!8::
::w*::
goTo, wrap_2_asterisk ; **text**
+!#::
::w~::
goTo, wrap_tilde ; ~~text~~
!-::
::w-::
goTo, wrap_hyphen ; -text-
+!-::
::w_::
goTo, wrap_underscore ; _text_
!k::
::wkbd::
goTo, wrap_kbd ; <kbd>text</kbd>
!del::
::wdel::
goTo, wrap_delete ; _text_ ---> text
;-----------------------
wrap_quote:
wrap_apostrophe:
wrap_grave:
wrap_parenthesis:
wrap_bracket:
wrap_brace:
wrap_angle:
wrap_percent:
wrap_2_angle:
wrap_asterisk:
wrap_2_asterisk:
wrap_tilde:
wrap_hyphen:
wrap_underscore:
wrap_kbd:
this_label := a_thisLabel
clipboard_text := get_clipboard()
for what, with in { "wrap_quote" : """" clipboard_text """"
, "wrap_apostrophe" : "'" clipboard_text "'"
, "wrap_grave" : "``" clipboard_text "``"
, "wrap_parenthesis" : "(" clipboard_text ")"
, "wrap_bracket" : "[" clipboard_text "]"
, "wrap_brace" : "{" clipboard_text "}"
, "wrap_angle" : "<" clipboard_text ">"
, "wrap_percent" : "%" clipboard_text "%"
, "wrap_2_angle" : ">>" clipboard_text "<<"
, "wrap_asterisk" : "*" clipboard_text "*"
, "wrap_2_asterisk" : "**" clipboard_text "**"
, "wrap_tilde" : "~~" clipboard_text "~~"
, "wrap_hyphen" : "-" clipboard_text "-"
, "wrap_underscore" : "_" clipboard_text "_"
, "wrap_kbd" : "<kbd>" clipboard_text "</kbd>" }
stringReplace, this_label, this_label, % what, % with, all
new_text := this_label
goSub, send_wrap
return
wrap_delete:
clipboard_text := get_clipboard()
loop, 2
{
stringLeft, left_character, clipboard_text, 1
stringRight, right_character, clipboard_text, 1
if regExMatch(left_character, "[\Q'%*-_""~``([{><\E]")
and if regExMatch(right_character, "[\Q'%*-_""~)``]}><\E]") ; if '%*-_"~`([{
{
stringTrimLeft, clipboard_text, clipboard_text, 1
stringTrimRight, clipboard_text, clipboard_text, 1
}
else break
}
new_text := clipboard_text
goSub, send_wrap
return
get_clipboard(){
global
if !inStr(a_thisHotkey, ":") ; if hotkey was used
{
revert_clipboard := clipboardAll
clipboard =
send ^{c}
clipWait, 0.3
if clipboard is space
clipboard =
}
return clipboard
}
send_wrap:
if !inStr(a_thisHotkey, ":") and if (clipboard = "") ; if hotkey was used
position := "{Left " round( strLen(new_text) / 2) "}" ; move cursor between symbols
else position := ""
clipboard := new_text
send % "^{v}" . position
sleep 150
clipboard := revert_clipboard
return
@davebrny
Copy link
Author

davebrny commented Oct 2, 2018

@jaydeepkarena youre welcome!

@Adrianrff
Copy link

This is fantastic, thank you!

I was wondering what should I modify in this script in order to trim leading and trailing spaces from the selected text, and then wrap the text around the desired symbols. I try to figure it out my self, but i'm still green in the AHK world.

Thanks in advance.

@Adrianrff
Copy link

I'm also noticing that, in some programs (such as memoQ) the key combination inserts the clipboard instead of wrapping the text. This is apparently random; sometimes it wraps the text and sometimes it pastes the clipboard.

@davebrny
Copy link
Author

davebrny commented Jun 17, 2019

sorry @adrianff i dont remember getting a notification about this comment. have you figured it out these problems since?
the trailing spaces can be fixed easily be using the trim() function:

string := " abc "
new_string := trim(string)
msgbox, >%string%< `n>%new_string%<

im not sure about the other problem. maybe the sleep could be could be changed to 100 or 200... it might be reverting the clipboard back to its previous state before the paste command has time to send it

@davebrny
Copy link
Author

@jaydeepkarena @adrianff
ive rewritten this script from scratch to make it easier to add and manage different types of wraps. the new script also uses a two stage hotkey so you dont have to worry about conflicts with other hotkeys as much:
https://gist.github.com/davebrny/cd98cb10d4c944b065fda67b707cfd0b

(i didnt add support for hotstrings into the new script though which is why im leaving this script here in case anyone still wants that feature)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment