Advertisement
anonymous1184

CapsLock Extravaganza

Feb 5th, 2021 (edited)
433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #NoEnv
  2. #SingleInstance, Force
  3.  
  4. SendMode, Input
  5. SetBatchLines, -1
  6. SetCapsLockState, AlwaysOff
  7. EnvGet, ProgramFilesx86, ProgramFiles(x86)
  8.  
  9. ; Menu
  10. Menu, caseChange, Add, &camelCase, caseC
  11. Menu, caseChange, Add, &lower case, caseL
  12. Menu, caseChange, Add, &Paragraph, caseP
  13. Menu, caseChange, Add, &Title Case, caseT
  14. Menu, caseChange, Add, &UPPER CASE, caseU
  15. Menu, caseChange, Add
  16. Menu, caseChange, Add, C&ancel, Cancel
  17.  
  18. ; #1: Stay off unless activated with Win+Caps
  19. #CapsLock::SetCapsLockState, % GetKeyState("CapsLock", "T") ? "AlwaysOff" : "On"
  20.  
  21. ; #2: ShareX commands
  22. Capslock & s::!F1
  23. Capslock & e::!F2
  24. Capslock & o::!F3
  25. Capslock & r::!F5
  26. Capslock & p::#+c
  27.  
  28. ; #3: Searching online
  29. CapsLock & g::Run, % "https://www.google.com/search?q=" clip(1)
  30. CapsLock & d::Run, % "https://www.google.com/search?q=define+" clip(1)
  31. CapsLock & t::Run, % "https://www.thesaurus.com/browse/" clip(1)
  32.  
  33. ; #4: Launching apps
  34. CapsLock & v::Run, % A_AppData "\..\Local\Vivaldi\Application\vivaldi.exe"
  35. CapsLock & n::Run, % A_AppData "\..\Local\Obsidian\Obsidian.exe"
  36. CapsLock & m::Run, % ProgramFilesx86 "\eM Client\MailClient.exe"
  37.  
  38. ; #5: Case Changer
  39. ^CapsLock::Menu, caseChange, Show
  40.  
  41. ; #6: Line editing
  42. Capslock & a::Send, {Home 2}+{End}
  43. Capslock & x::Send, {Home 2}+{End}^x
  44. Capslock & c::Send, {Home}{End}+!{5}
  45.  
  46. ; #7: Markdown
  47. Capslock & /::wrap("```````n", "`n```````n`n")
  48. Capslock & k::
  49.     Gui, New, +AlwaysOnTop -SysMenu, Add a link
  50.     Gui, Add, Edit, VmdTxt w300, Title of link (optional)
  51.     Gui, Add, Edit, VmdUri w300, % (Clipboard ~= "^(ftp|https?|mailto):" ? Clipboard : "Paste or type a link")
  52.     Gui, Add, Button, Default x-100 y-100 -Tabstop, OK
  53.     Gui, Show
  54. return
  55.  
  56. ; #8: Text wrap
  57. CapsLock & h::wrap("<kbd>", "</kbd>")
  58. CapsLock & z::wrap("~~~~")
  59. CapsLock & b::wrap("**")
  60. CapsLock & i::wrap("*")
  61. CapsLock & ,::wrap("<", ">")
  62. CapsLock & [::wrap("[[", "]]")
  63. CapsLock & ]::wrap("{{", "}}")
  64. CapsLock & 9::wrap("(", ")")
  65. CapsLock & `::wrap("``", "``")
  66. CapsLock & '::wrap(Chr(34))
  67. CapsLock & -::Send, % "{Home 2}-" A_Space
  68.  
  69. ; #9: Non-Caps Replacements and Hotstrings
  70. !-::Send,; Alt+Minus = Em dash
  71. +!-::Send,; Shift+Alt+Minus = En dash
  72.  
  73. ::|c::©
  74. ::(c)::©
  75.  
  76. ::|r::®
  77. ::(r)::®
  78.  
  79. ::|s::§
  80. ::(tm)::™
  81.  
  82. :c:|o::•
  83. ::|bull::•
  84. ::|bullet::•
  85.  
  86. ; Arrows
  87. :?*:-->::→
  88. :?*:<--::←
  89. :?*:<->::↔
  90. ::|^::↑
  91. ::|v::↓
  92.  
  93. clip(copy = 0, reset = -1)
  94. {
  95.     static backup := ""
  96.     if (copy)
  97.     {
  98.         backup := ClipboardAll
  99.         Clipboard := ""
  100.         if (copy = 1)
  101.         {
  102.             Send, ^c
  103.         }
  104.         else
  105.         {
  106.             Clipboard := copy
  107.         }
  108.         ClipWait, 1
  109.         if (ErrorLevel)
  110.         {
  111.             MsgBox, 0x10,, Error getting Clipboard data.
  112.             Exit
  113.         }
  114.         SetTimer, % A_ThisFunc, % reset
  115.         return Clipboard
  116.     }
  117.     else
  118.     {
  119.         Sleep, 500
  120.         Clipboard := backup
  121.     }
  122. }
  123.  
  124. wrap(before, after := "")
  125. {
  126.     txt := clip(1, "Off")
  127.     RegExMatch(txt, "\s+$", ews)
  128.     out := before RTrim(txt) (after ? after : before) ews
  129.     Clipboard := ""
  130.     Clipboard := out
  131.     ClipWait
  132.     Send, ^v
  133.     clip()
  134. }
  135.  
  136. formatLink(txt, uri)
  137. {
  138.     static chars := StrSplit("\*_{}[]<>()#+-.!|")
  139.     ; https://www.markdownguide.org/basic-syntax#characters-you-can-escape
  140.  
  141.     if (!RegExMatch(uri, "^(ftp|https?|mailto):"))
  142.     {
  143.         return
  144.     }
  145.  
  146.     start := InStr(uri, "/",,, 3)
  147.     , txt := txt = "Title of link (optional)" ? "" : txt
  148.     , uri := SubStr(uri, 1, start) encodeURI(decodeURI(SubStr(uri, start+1)))
  149.     ; https://www.markdownguide.org/basic-syntax#link-best-practices
  150.  
  151.     if (!txt)
  152.     {
  153.         return "<" uri ">"
  154.     }
  155.     for i,chr in chars
  156.     {
  157.         txt := StrReplace(txt, chr, "\" chr)
  158.     }
  159.     return "[" txt "](" uri ")"
  160. }
  161.  
  162. encodeURI(uri, encoding := "UTF-8")
  163. {
  164.     out := ""
  165.     VarSetCapacity(var, StrLen(uri) * 2, 0)
  166.     StrPut(uri, &var, encoding)
  167.     while (code := NumGet(var, A_Index - 1, "UChar"))
  168.     {
  169.         char := Chr(code)
  170.         out .= (char ~= "[!#$&-;=?-Z_a-z~]" ? char : Format("%{:02X}", code))
  171.     }
  172.     return out
  173. }
  174.  
  175. decodeURI(uri, encoding := "UTF-8")
  176. {
  177.     while RegExMatch(uri, "i)(%[\dA-F]{2})+", code)
  178.     {
  179.         VarSetCapacity(var, 2, 0)
  180.         loop, parse, % SubStr(code, 2), `%
  181.         {
  182.             NumPut("0x" A_LoopField, var, A_Index-1, "UChar")
  183.         }
  184.         uri := StrReplace(uri, code, StrGet(&var, encoding))
  185.     }
  186.     return uri
  187. }
  188.  
  189. ButtonOK:
  190.    Gui, Submit
  191.     if (link := formatLink(mdTxt, mdUri))
  192.     {
  193.         clip(link)
  194.         Send, ^v
  195.     }
  196. GuiEscape:
  197.     Gui, Destroy
  198. return
  199.  
  200. caseC:
  201. caseL:
  202. caseP:
  203. caseT:
  204. caseU:
  205.    txt := clip(1, "Off")
  206.     switch mode := SubStr(A_ThisLabel, 0)
  207.     {
  208.         case "C": txt := RegExReplace(txt, "(([A-Z]+)|(?i)((?<=[a-z])|[a-z])([a-z]*))[ _-]([a-z]|[A-Z]+)", "$L2$L3$4$T5")
  209.         case "P": txt := RegExReplace(txt, "(\w)([^?.:!]*)", "$U1$L2")
  210.         Default: txt := Format("{:" mode "}", txt)
  211.     }
  212.     Clipboard := txt
  213.     Send, ^v
  214.     clip()
  215. Cancel:
  216. return
  217.  
  218. ;
  219. ; Hotstring Helper
  220. ; https://www.autohotkey.com/docs/Hotstrings.htm#Helper
  221. ;
  222.  
  223. #h::  ; Win+H hotkey
  224. ; Get the text currently selected. The clipboard is used instead of
  225. ; "ControlGet Selected" because it works in a greater variety of editors
  226. ; (namely word processors).  Save the current clipboard contents to be
  227. ; restored later. Although this handles only plain text, it seems better
  228. ; than nothing:
  229. AutoTrim Off  ; Retain any leading and trailing whitespace on the clipboard.
  230. ClipboardOld := ClipboardAll
  231. Clipboard := ""  ; Must start off blank for detection to work.
  232. Send ^c
  233. ClipWait 1
  234. if ErrorLevel  ; ClipWait timed out.
  235.     return
  236. ; Replace CRLF and/or LF with `n for use in a "send-raw" hotstring:
  237. ; The same is done for any other characters that might otherwise
  238. ; be a problem in raw mode:
  239. StringReplace, Hotstring, Clipboard, ``, ````, All  ; Do this replacement first to avoid interfering with the others below.
  240. StringReplace, Hotstring, Hotstring, `r`n, ``r, All  ; Using `r works better than `n in MS Word, etc.
  241. StringReplace, Hotstring, Hotstring, `n, ``r, All
  242. StringReplace, Hotstring, Hotstring, %A_Tab%, ``t, All
  243. StringReplace, Hotstring, Hotstring, `;, ```;, All
  244. Clipboard := ClipboardOld  ; Restore previous contents of clipboard.
  245. ; This will move the input box's caret to a more friendly position:
  246. SetTimer, MoveCaret, 10
  247. ; Show the input box, providing the default hotstring:
  248. InputBox, Hotstring, New Hotstring, Type your abreviation at the indicated insertion point. You can also edit the replacement text if you wish.`n`nExample entry: :R:btw`::by the way,,,,,,,, :R:`::%Hotstring%
  249. if ErrorLevel  ; The user pressed Cancel.
  250.     return
  251. if InStr(Hotstring, ":R`:::")
  252. {
  253.     MsgBox You didn't provide an abbreviation. The hotstring has not been added.
  254.     return
  255. }
  256. ; Otherwise, add the hotstring and reload the script:
  257. FileAppend, `n%Hotstring%, %A_ScriptFullPath%  ; Put a `n at the beginning in case file lacks a blank line at its end.
  258. Reload
  259. Sleep 200 ; If successful, the reload will close this instance during the Sleep, so the line below will never be reached.
  260. MsgBox, 4,, The hotstring just added appears to be improperly formatted.  Would you like to open the script for editing? Note that the bad hotstring is at the bottom of the script.
  261. IfMsgBox, Yes, Edit
  262. return
  263.  
  264. MoveCaret:
  265. if not WinActive("New Hotstring")
  266.     return
  267. ; Otherwise, move the input box's insertion point to where the user will type the abbreviation.
  268. Send {Home}{Right 3}
  269. SetTimer, MoveCaret, Off
  270. return
  271.  
  272. ;
  273. ; Start of auto-generated content by Hotstring Helper
  274. ;
  275.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement