One Button to comment out ANY type of code!

Usage tips, posted by users. No questions here please.

Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard

Post Reply
User avatar
CyberSlug
Posts: 120
Joined: Sat Oct 04, 2003 3:41 am

One Button to comment out ANY type of code!

Post by CyberSlug »

EDIT: Feb 14. Lots of changes. Docs will be updated soon....

(Edited Nov 5 so script runs under AutoIt 3.0.73)

Hello, I've written a program (which you assign to a user-defined tool) that comments out a block of code based on the file class. (You no longer have to define a macro for every programming language you use!)

The script needs to be compiled with AutoIt v3--a free download only 580 KB from http://www.hiddensoft.com/autoit3/

Requirements: Section 2 needs to be modified as needed.

I would appreciate any feedback if you decide to try it out! :-)

Code: Select all

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; AutoIt 3.0.73 script Designed for TextPad 4.7.1 ;
;  to comment out a SELECTED block of text        ;
;  Philip Gump - philipgump@yahoo.com             ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;FIRST, determine the class (file extension) of the active document.

$titles = WinGetText("TextPad - ","")
$tmp = StringLeft($titles, StringInStr($titles, Chr(10)))   ; file path

While StringInstr($tmp, ".") <> 0   ; path may contain many dots
  $tmp = StringTrimLeft($tmp, StringInstr($tmp, "."))
WEnd
If StringInstr($tmp, "*") <> 0  ; file was modified since last save
  $class = StringTrimRight($tmp, 3)
Else
  $class = StringTrimRight($tmp, 1) ; need to remove carriage return
EndIf
; We now have the file extension (stored in variable $class)!


;;SECOND, hard code the different comment types as needed
Select
     Case $class = "sim"
        $comment = "''"
     Case $class = "java" OR $class = "c"
        $comment = "//"
     Case $class = "perl"
        $comment = "#"
     Case $class = "bat" OR $class = "au3"
        $comment = ";"
     Case 1=1;Else
        $comment = "#NDEF#"
EndSelect


;;THIRD, send keystrokes to TextPad to do a Search and Replace...
AutoItSetOption("SendKeyDelay",0)
Send("^h")    ; Launch Replace dialog ("!s#r") is more compatible
WinWaitActive("Replace")
Send("{^}")   ; Find What == [beginning of line]
Send("{TAB}")
Send($comment & " ", 1)  ; Replace With == [comment char and a space]
Send("{TAB 3}")          ; move to the Reg Exp checkbox

; If the Reg Exp checkbox is not checked, then check it!
AutoItSetOption("MouseCoordMode",0)  ;needed due to bug in AutoIt?
AutoItSetOption("PixelCoordMode",0)  ;set coords relative to active window
If PixelGetColor(25,165) <> 0  ;looks for black checkmark
	Send("{SPACE}")
EndIf

Send("!s{TAB}{SPACE}")    ;move to Replace All button and click it
WinWaitActive("Replace")
Send("{TAB}{SPACE}")      ;close dialog window

; Comment should have been successfully inserted!!!  THE END
If you want to UNcomment, use the above script but replace the third section.

Code: Select all

;;THIRD, send keystrokes to TextPad to do a Search and Replace...

AutoItSetOption("SendKeyDelay",0)
Send("^h")                     ;Launch Replace dialog ("!s#r") is more compatible
WinWaitActive("Replace")
Send("^" & $comment & " ", 1)  ;Find What == [comment char and a space]
Send("{TAB}")
Send("{DEL}")                  ;Replace With nothing
Send("{TAB 3}")                ;move to the Reg Exp checkbox

; If the Reg Exp checkbox is not checked, then check it!
AutoItSetOption("MouseCoordMode",0)  ;needed due to bug in AutoIt?
AutoItSetOption("PixelCoordMode",0)  ;set coords relative to active window
If PixelGetColor(25,165) <> 0  ;looks for black checkmark
	Send("{SPACE}")
EndIf

Send("!s{TAB}{SPACE}")    ;move to Replace All button and click it
WinWaitActive("Replace")
Send("{TAB}{SPACE}")      ;close dialog window
Last edited by CyberSlug on Sun Feb 15, 2004 4:27 am, edited 2 times in total.
User avatar
CyberSlug
Posts: 120
Joined: Sat Oct 04, 2003 3:41 am

Addendum

Post by CyberSlug »

Actually, you don't need to compile the script.

You can create a user-defined tool that has:
COMMAND == "C:\Program Files\AutoIt3\AutoIt3.exe"
PARAMETER == "C:\MyFiles\MyCommentScript.au3"
(where the file names are changed as appropriate).

EDIT:
This method (of not compiling my code) allows you to easily tweak/maintain the script.
cyberlangur
Posts: 1
Joined: Wed Feb 11, 2004 9:49 pm

comment block

Post by cyberlangur »

hi,
this is probably a stupid problem, but
i cannot compile the comment.au3 script
using autoit.
i get the error message "on line 20
If must be followed by a Then statement"
any ideas would be most appreciated.
Aprajit
User avatar
CyberSlug
Posts: 120
Joined: Sat Oct 04, 2003 3:41 am

Post by CyberSlug »

Hello!

Previous versions of AutoIt did not require the Then keyword in If-statements. New versions do, so add it in.

You must fix two lines:

Code: Select all

If StringInstr($tmp, "*") <> 0 Then  ; file was modified since last save

If PixelGetColor(25,165) <> 0 Then  ;looks for black checkmark
I actually have updated versions to post--I've been waiting for AutoIt's official release, but I'll try to release something sooner since there is at least some interest.

My revised scripts work a lot better, so stay posted....
User avatar
CyberSlug
Posts: 120
Joined: Sat Oct 04, 2003 3:41 am

Post by CyberSlug »

Updated versions!
I hope the Readme makes sense....
BarrySumpter
Posts: 4
Joined: Sat Feb 14, 2004 7:03 am

Well Done Gentlemen!

Post by BarrySumpter »

:D

Excellent work gentlemen.

I've been using TextPad for years.

I've just started to look as ASP and VBScript.

What a surprise to have color coding for html and ASP etc. already inplemented in TextPad.

Your block comment/uncomment solution to what I believe is a major oversight in textpad is brilliant.

Now, I can see the code, I can see how it works, but I'm having a really hard time trying to implement it.

For me, for now, I'm really not interested in getting lost in all the fine points of how this code works or how to get it to work.

I was hoping someone might have a step-by-step instruction set, for a complete novice, on how to implement this enhancement or perhaps a similar enhancement where I can just substitute this code.

Again, thanks for all the work and for offering this to TextPad users world.

Barry G. Sumpter
bsumpter@bigpond.net.au
BarrySumpter
Posts: 4
Joined: Sat Feb 14, 2004 7:03 am

' comment (single quote for commenting vbscript and ASP

Post by BarrySumpter »

I've added

Case StringInStr("asp", $type)
$macro = "' Comment"
; - single quote

to commentA.au3

and under

Configure | Preferences | Tools

Name: ' Comment - single quote

Command: H:\Program Files\AutoIt3\AutoIt3.exe
Parameters: "H:\Program Files\TextPad 4\AutoIT\commentA.au3" $Sel
Initial Folder: $FileDir

I then edit a .ASP file and execute the 'Comment

blinks but no updates are made.

Could it be the ' ? (single quote)

Is there a way to code the commentA.au3 parameters to accept another parameter like $char. Where we could change the parameters to:

"H:\Program Files\TextPad 4\AutoIT\commentA.au3" $Sel "//"
or
"H:\Program Files\TextPad 4\AutoIT\commentA.au3" $Sel "'" (single quote surrounded by double quotes)

Thanks,
baz
User avatar
CyberSlug
Posts: 120
Joined: Sat Oct 04, 2003 3:41 am

Post by CyberSlug »

Make sure that there is a macro named ' Comment on your macros menu.

Also, make sure that Case StringInStr("asp", $type) appears before Case Else.

Hope that helps
BarrySumpter
Posts: 4
Joined: Sat Feb 14, 2004 7:03 am

Post by BarrySumpter »

Hi all,

"....Also, make sure that Case StringInStr("asp", $type) appears before Case Else. ...."

Correct.



However,

"....Make sure that there is a macro named ' Comment on your macros menu. ...."

NOT so.

Only the // and ; comment and uncomment are available on the macros menu. Which I believe are the CommentToggle solution offered by cyberslug.

I believe the only way to have a macro available on the macro menu is to have the precompiled *.tpm files (personal macros) on the
c:\documents and settings\(currentUser)\Application Data\TextPad
folder.

I actually had to change the .tmp file names (offered by cyberslug on 12 feb 2004) to .tpm to get them to work on this directory. Or it could have been a combination of selecting the macros from the
Configure | Preference | Macros
dialog and/or restarting TextPad at the right time to make me think this name change is what made it work. Again, Which was my attempted implementation of the CommentToggle solution again offered by cyberslug.


Now back to the original problem of getting the commentA and uncommentA to work.

I used
Configure | Preference | Tools to implement commentA and uncommentA,
and can only access the ' Comment and ' UnComment thru the
Tools menu or thru the Tools Toolbar (View | Toolbars | Tools).

By the way anyone know how to change the buttons on the Tools Toolbar?
Where 1 thru 16 all look the same except for the number and the tooltip.

As I've spent more time on formatting these messages and asking for help, than implementation and debugging, its quite possible I've missed something in the setup procedures...

Any additional help would be sincerely appreciated.


Thanks again,

baz



P.S. Sorry CyberSlug I didn't realize it was you who replied to my call for help. Thanks heaps to CyberSlug.
User avatar
CyberSlug
Posts: 120
Joined: Sat Oct 04, 2003 3:41 am

Post by CyberSlug »

I'll update the docs :) It should help explain things better.

Stay tuned.
BarrySumpter
Posts: 4
Joined: Sat Feb 14, 2004 7:03 am

Post by BarrySumpter »

Any chance of you making help.doc instead of readme.txt ?

Adding Screen shots would be fantastic!

I know I'm pushing my luck here but...
Any chance of you building/compiling a singlequoteComment.tmp and singlequoteUncomment.tmp?

Or perhaps consider my comment:
-----
Is there a way to code the commentA.au3 parameters to accept another parameter like $char. Where we could change the parameters to:

"H:\Program Files\TextPad 4\AutoIT\commentA.au3" $Sel "//"
or
"H:\Program Files\TextPad 4\AutoIT\commentA.au3" $Sel "'" (single quote surrounded by double quotes)
-----

by using the commentToggle instead of the commnetA.au3 as suggested?




Thanks Champ,

baz
Post Reply