AutoHotkey Community

It is currently May 27th, 2012, 7:31 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: October 22nd, 2004, 10:54 pm 
Offline

Joined: March 2nd, 2004, 3:36 pm
Posts: 10720
Here is a little script that some might find useful.
Code:
; INTRO
; This script allows a section of lines to be easily commented out or uncommented by
; automatically inserting a leading semicolon in front of each.  The hotkeys it uses
; seem somewhat standard, at least among Microsoft Visual products (maybe Borland and
; other visual source code editors use them too).
;
; USAGE
; When the right type of window is active (see the first few lines of the script):
; Pressing Ctrl-K followed by Ctrl-C will comment out the currently selected lines.
; Pressing Ctrl-K followed by Ctrl-U will uncomment the currently selected lines.
;
; NOTES
; It uses the clipboard because some editors (e.g. Metapad) aren't compatibible with
; "ControlGet Selected".  If your editor is something with a normal edit field, you
; could try using "ControlGet, selected" instead.
; Requires AutoHotkey v1.0.21 and Windows NT/2000/XP or later.

$^k::
SetTitleMatchMode, 2
IfWinNotActive, pad  ; Replace this with whatever filtering criteria you want.
{
   Send ^k
   return
}
Transform, CtrlC, chr, 3
Transform, CtrlU, chr, 21
Input, input, L1 M
if input = %CtrlC%
{
   ; Doesn't get the right text in Metapad, perhaps due to being RichEdit:
   ;ControlGet, selected, selected
   clipboard =
   Send ^c
   ClipWait, 1
   if ErrorLevel <> 0
      return
   NewClip =
   WaitingForFirstCharOfLine = y
   AutoTrim, off
   Loop, parse, clipboard
   {
      if WaitingForFirstCharOfLine = y
      {
         if A_LoopField not in %A_Space%,%A_Tab%
         {
            NewClip = %NewClip%;
            WaitingForFirstCharOfLine = n
         }
      }
      else if A_LoopField = `n
         WaitingForFirstCharOfLine = y
      NewClip = %NewClip%%A_LoopField%
   }
   clipboard = %NewClip%
   Send, ^v
}
else if input = %CtrlU%
{
   clipboard =
   Send ^c
   ClipWait, 1
   if ErrorLevel <> 0
      return
   NewClip =
   WaitingForFirstCharOfLine = y
   AutoTrim, off
   Loop, parse, clipboard
   {
      if WaitingForFirstCharOfLine = y
      {
         if A_LoopField not in %A_Space%,%A_Tab%
         {
            if A_LoopField <> `;
               NewClip = %NewClip%%A_LoopField%
            WaitingForFirstCharOfLine = n
         }
         else
            NewClip = %NewClip%%A_LoopField%
      }
      else
      {
         if A_LoopField = `n
            WaitingForFirstCharOfLine = y
         NewClip = %NewClip%%A_LoopField%
      }
   }
   clipboard = %NewClip%
   Send, ^v
}
return


Report this post
Top
 Profile  
Reply with quote  
PostPosted: June 17th, 2010, 6:18 pm 
Offline

Joined: August 12th, 2009, 1:09 pm
Posts: 5
Location: Norway
I was looking for a script that did exactly this, but while testing I noticed that Notepad++ already does this for you by pressing CTRL+Q. Also, it supports a lot of different languages (which will be based on the file extension, or your selection in the "Language" menu).

But without trying to implement this script, I would probably never have realized it - I should play around in the menus more often :oops:

(I realize that the original post is almost six years old, but this reply might be helpful for others.)


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 14 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group