AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Comment out (or uncomment) the currently selected lines

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
Chris
Site Admin


Joined: 02 Mar 2004
Posts: 10463

PostPosted: Fri Oct 22, 2004 10:54 pm    Post subject: Comment out (or uncomment) the currently selected lines Reply with quote

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
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group