Hi guys
I want to create a script that can comment/uncomment a line similar to how it's done in advanced notepads (Notepad++, Sublime Text, different IDEs e.t.c). If it's already been done, I can't find it using forum searches because "comment" is a very broad definition. I need this script because I often work in the text editors that doesn't support hotkeys (Different SQL database tools, web pages, e.t.c) and copy-pasting it to/from notepad will be a hassle. Of course being able to comment/uncomment whole blocks would be even greater, but I doubt it's easily achievable with AHK, so one line comment/uncomment will do for me.
So, how do I see it working? For example, when hotkey is pressed:
1. The script copy the whole current line to the clipboard (for example by sending 'Home > Shift+End > Ctrl+C' to the active window)
2. Script analyze the contents of the clipboard and determines the first character on the line.
3. If the first character is "#" (or another pre-defined comment symbol), the script deletes this character (for example simply by sending 'Home > Del' to the active window)
4. If it's any other character, the script inserts "#" at the start of the line ( Send Home > Send "#" )
Is something like that possible? Will this task be interesting enough for you to tackle? As usual, I'd be super-grateful for any hints or links to AHK functions that can achieve something similar.
UPD: This is not to ask for a ready-made solution, I just ask for a tip where to start reading, which method/function can be used for this...
Comment/uncomment a line Topic is solved
Comment/uncomment a line
Last edited by AndrewKun on 23 May 2017, 11:53, edited 1 time in total.
Re: Comment/uncomment a line
Andrew,
Why don't you start by building a script and when you need assistance, then post your script and people are far more willing to assist with specific questions than to write a script for you. "Teach a man to fish..." comes to mind here. While I could probably build this fairly quickly, I have little motivation to do so right now. If you truly want me to build this for you, contact me via a private message.
Yes, this can be done easily with AHK, including the "block" support you mention. I have done very similar things (and much more). It is not that challenging, so you should be able to read the AHK docs and come up with something.Is something like that possible?
Probably not.Will this task be interesting enough for you to tackle?
Why don't you start by building a script and when you need assistance, then post your script and people are far more willing to assist with specific questions than to write a script for you. "Teach a man to fish..." comes to mind here. While I could probably build this fairly quickly, I have little motivation to do so right now. If you truly want me to build this for you, contact me via a private message.
Mike V.
Re: Comment/uncomment a line
Thanks for the reply. Yes, I'm willing to get into this stuff myself, can you just give a couple of links where to start, i.e. which AHK methods/functions can be used for reading and manipulating text? I'm seeing a bunch of options( like Clipboard/ClipboardAll function), but the trick here is to pick the one that suits this best...mviens wrote:Andrew,
Yes, this can be done easily with AHK, including the "block" support you mention. I have done very similar things (and much more). It is not that challenging, so you should be able to read the AHK docs and come up with something.Is something like that possible?
Probably not.Will this task be interesting enough for you to tackle?
Why don't you start by building a script and when you need assistance, then post your script and people are far more willing to assist with specific questions than to write a script for you. "Teach a man to fish..." comes to mind here. While I could probably build this fairly quickly, I have little motivation to do so right now. If you truly want me to build this for you, contact me via a private message.
- FanaticGuru
- Posts: 1904
- Joined: 30 Sep 2013, 22:25
Re: Comment/uncomment a line
What you are asking for is relatively simple.
You could tinker with it to get the exact effect you want. Highlighting blocks of lines and toggling at the beginning of the line "#"s is also very doable.
FG
Code: Select all
F12::
Send {Home}+{End}^c
Sleep 50
if (SubStr(Clipboard,1,1) = "#")
Clipboard := SubStr(Clipboard,2)
else
Clipboard := "#" Clipboard
Sleep 50
Send ^v
Return
FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
- FanaticGuru
- Posts: 1904
- Joined: 30 Sep 2013, 22:25
Re: Comment/uncomment a line Topic is solved
Here is a more complicated version that works for multiple selected lines.
FG
Code: Select all
F12::
CRLF := "`r`n"
Clipboard =
Output =
Send ^c
Sleep 50
if !Clipboard
{
Send {Home}+{End}^c
ClipWait
}
Loop, Parse, Clipboard, `n, `r
{
RegExMatch(A_LoopField, "^(#?)(.*)$", Match)
if Match1
Output .= Match2 CRLF
else
Output .= "#" Match2 CRLF
}
if Match2
Clipboard := SubStr(Output, 1, 0-StrLen(CRLF))
else
Clipboard := SubStr(Output, 1, -1-StrLen(CRLF))
Sleep 50
Send ^v
return
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
Re: Comment/uncomment a line
Let me thank you sincerely, I mean it. This really works! To be honest I don't fully understand the regex, guess I should learn it first and foremost.FanaticGuru wrote:Here is a more complicated version that works for multiple selected lines.FGCode: Select all
F12:: CRLF := "`r`n" Clipboard = Output = Send ^c Sleep 50 if !Clipboard { Send {Home}+{End}^c ClipWait } Loop, Parse, Clipboard, `n, `r { RegExMatch(A_LoopField, "^(#?)(.*)$", Match) if Match1 Output .= Match2 CRLF else Output .= "#" Match2 CRLF } if Match2 Clipboard := SubStr(Output, 1, 0-StrLen(CRLF)) else Clipboard := SubStr(Output, 1, -1-StrLen(CRLF)) Sleep 50 Send ^v return
I hope someone else will find this script useful.
Thank you once again!
Who is online
Users browsing this forum: No registered users and 179 guests