Comment/uncomment a line Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
AndrewKun
Posts: 24
Joined: 03 Jun 2015, 12:03

Comment/uncomment a line

23 May 2017, 08:54

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...
Last edited by AndrewKun on 23 May 2017, 11:53, edited 1 time in total.
User avatar
mviens
Posts: 43
Joined: 08 Jan 2014, 19:04

Re: Comment/uncomment a line

23 May 2017, 11:45

Andrew,
Is something like that possible?
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.
Will this task be interesting enough for you to tackle?
Probably not.

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.
AndrewKun
Posts: 24
Joined: 03 Jun 2015, 12:03

Re: Comment/uncomment a line

23 May 2017, 11:51

mviens wrote:Andrew,
Is something like that possible?
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.
Will this task be interesting enough for you to tackle?
Probably not.

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.
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...
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Comment/uncomment a line

23 May 2017, 11:54

What you are asking for is relatively simple.

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
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
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
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Comment/uncomment a line  Topic is solved

23 May 2017, 19:04

Here is a more complicated version that works for multiple selected lines.

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
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
AndrewKun
Posts: 24
Joined: 03 Jun 2015, 12:03

Re: Comment/uncomment a line

24 May 2017, 03:39

FanaticGuru wrote:Here is a more complicated version that works for multiple selected lines.

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
FG
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.

I hope someone else will find this script useful.

Thank you once again!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Rohwedder, ruespe and 345 guests