One Button to comment out ANY type of code! (Version 2)

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

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

Post Reply
drefty
Posts: 35
Joined: Mon Dec 22, 2003 3:53 pm

One Button to comment out ANY type of code! (Version 2)

Post by drefty »

Cyberslug already addressed this limitation in TextPad, but some people are unwilling or unable to install and learn AutoIt. This solution accomplishes the same thing, without requiring any third party application, it is pure Textpad.

The post is a bit long, so the short version is: use a macro in combination with "insert from file" to insert your comment characters. Then just edit the "inserted file" to change the comment characters based on document class.

PROBLEM:
TextPad has a nice easy way of specifying 'comment characters' for each
separate document class. Unfortunately, this feature applies only to Syntax
Highlighting.

There is no feature that lets you *insert* class-specific 'comment characters'
into a text while editing a file.

The Clip Library is inadequate because, for one thing, it doesn't allow you
to apply comments to multiple lines at once.

The Macro Editor is inadequate because you have to record and use a separate
macro for each different type of 'comment character' that you want to insert.
This is clumsy because the Macros Menu does not change the available macros
based on the document class of the currently open file.

WORKAROUND:
We can enhance the flexibility of macros by using the "insert from file"
feature of TextPad. This feature will give us a way to INSERT and DELETE comment
characters from the beginning of multiple lines.

HOW TO CREATE The INSERT MACRO:

Code: Select all

Macros->Record
Press {HOME}{HOME}      (do it twice to make sure we are at the beginning of line)
Edit->Insert->Files...  (we want to insert the contents of line prefix file)
Specify the File        (use a file that contains only the cmmt chars like "c:\MyCmmtChars.txt")
Press {HOME}{HOME}      (do it twice to make sure we are at the beginning of line)
Press {DOWN}
Macros->Stop Recording
Save the Macro          (IMPORTANT: make sure you select 'Repeat Thru Selection' when you save the macro)
HOW TO CREATE The DELETE MACRO:
To "uncomment" lines we need a macro to remove the characters that we already inserted.
This is a tricky operation since the comment characters can change based on the document
class.

One way to solve this problem is to impose a restriction on your comment characters.
For example, make sure that all of your comment characters satisfy the following
Regular Expression ...

Code: Select all

 ^ *[^ ]+ {0,1}
The preceeding Regular Expression tests for the beginning of the line, followed by
zero or more spaces, followed by 1 or more non spaces, followed by zero or one
spaces.

After you formulate your RegEx test, you can simply create a macro that deletes
everything inside the current selection that meets the test. This can be
accomplished using the TextPad Replace feature.

ANNOYANCES:

DESELECTED SELECTION: It would be better if the cmmt chars file could be used
directly with the Replace dialog. This is because the insert macro 'deselects'
the region after doing the insert.

UNINTELLIGENT DELETE: The delete macro is unintelligent because it blindly erases
the leading chars from all selected lines, even if the lines to not begin with
the expected comment char. It would be better if the delete happend if and ONLY if
the line begins with the expected comment char. For now, this is good enough, you just
have to be careful before deleting anything.
User avatar
Bob Hansen
Posts: 1517
Joined: Sun Mar 02, 2003 8:15 pm
Location: Salem, NH
Contact:

Post by Bob Hansen »

This is a great example of what types of items should be posted in TIPS forum. Thanks a lot drefty
Hope this was helpful.............good luck,
Bob
Post Reply