// 'Toggle Comment' macro (WinEdt v6 and v7) v1.1 // $Id: Toggle Comment.edt 841 2012-04-14 13:55:03Z Geoffrey $ // ---------------------------------------------------------------------------- // Change Log: // 20120414 Updated to accommodate differences between WinEdt v6 and // v7 SetWrap parameter semantics // 20120414 Shifted UNIX mode to top of IfisMode fallthroughs to allow // other modes to override it if detected // 20120414 Removed restriction tying BAT to Application mode // 20120414 Added AHK (autohotkey script) // 20120414 Added note about improved SetTracking post Build 20120413 // -- no longer any need to call RefreshAll // ---------------------------------------------------------------------------- // // This macro "block comments/uncomments" lines containing the cursor or, // if more than one line is selected, the lines containing the highlighted // text. It inserts filetype-appropriate comment markers to the start of // lines in designated files (e.g., TEX, UNIX), does nothing in designated // other files (e.g., HTML), and inserts `// ' to the start of lines in // remaining file types. // // To install this macro, copy this file to %b\Macros\text and assign it // a shortcut of your choice (e.g., Ctrl+Q in keeping with many other code // editors). Then, with this file loaded in the editor, click the Load // Current Script button in the Options Interface. Note that due to the // way WinEdt does things, you might find that Load Current Script executes // this action. In which case, undo, exit without save or whatever. // // To run, place the cursor on the line, or highlight the lines (no need to // highlight the full first and last lines of the selection -- it's enough // just to highlight a bit), then press Ctrl+Q (or whatever keystroke // shortcut you established in your code above). Repeat to toggle. // // More detailed installation instructions for those that are new to this // sort of thing: // 1. copy this file to %b\Macros\text ('%b' is your WinEdt Ini Path // e.g., "%appdata%\WinEdt Team\WinEdt 7", or something like that) // 2. select Options -> Options Interface... from WinEdt's main menu // 3. click the Main Menu icon in the Menus and Interface... subtree in // the Options Interface panel on the left hand side of the editor // 4. insert this code (or something like it): // ITEM="Toggle_Comment" // CAPTION="&Toggle Comment" // IMAGE="CommentInsert" // MACRO="Exe('%b\Macros\text\Toggle Comment.edt');" // SHORTCUT="16465::Ctrl+Q" // REQ_DOCUMENT=1 // just before the code for "Insert_Comment" and "Remove_Comment" // in your version of MainMenu.ini // 5. click the little "Load Current Script" icon in the upper left corner // of the Options Interface panel to compile and load the script. // 6. NB, due to the way WinEdt does things, you will probably find that // the Toggle_Comment action was applied to whatever lines of text // happened to be selected when you invoked "Load Current Script". In // which case, just exit without save. // // Configure registers %!8 and %!9 if you need to customise this macro further. // // Variables and Registers Used // ---------------------------- // CONFIGURATION VARIABLES // // %!8 PROGRAMMER-DEFINED comment string, e.g., '%', '//', 'REM' or '#' // (write "%%" for "%") // %!9 PROGRAMMER-DEFINED number of trailing (separator) spaces inserted // between the comment string and the remainder of the line // ---------------------------------------------------------------------------- // WORKING VARIABLES // // 1 Mark/scope of start of text selection // 2 Mark/scope of end of text selection (current cursor location) // // %!0 WinEdt build number // %!1 Line number of top of selection; current line number during iteration // %!2 Line number of bottom of selection // %!3 Temp copy of %!1 in case %!1 and %!2 should be swapped depending on // relative order of mark 1 and 2 // %!4 Temp copy of %!2 in case %!1 and %!2 should be swapped depending on // relative order of mark 1 and 2 // %!5 Calculated length of comment string (see %!8, below) // ---------------------------------------------------------------------------- // NB, do not include trailing spaces when defining the comment string (%!8). // Specify the number of inserted trailing spaces in %!9 instead. // Explanation: Users often configure WinEdt to automatically trim spaces from // the end of lines. In which case, comment strings like "% " will be converted // to "%" whenever applied to blank lines. Since this won't match the comment // string, instead of toggling the comment, subsequent calls to this macro will // simply add a new comment string to the already commented line. // ---------------------------------------------------------------------------- // --------- Configuration Code (you might want to fool with this) ------------ // @BEGIN: EXCLUDED MODES (editor modes in which this macro does nothing) IfisMode('HTML;XML;PHP;HHC;NSIS;ANSI;Fortran','%!m',!'End;'); // @END: EXCLUDED MODES // @BEGIN: DEFAULT Programmer-defined constants LetReg(8,"//"); // The comment string LetRegNum(9,1); // Number of trailing spaces inserted after the comment string // @END: DEFAULT Programmer-defined constants // @BEGIN: UNIX (WinEdt auto-detects if EOLN is LF, rather than Windows CR+LF) IfisMode('UNIX','%!m',> !'LetReg(8,"#");> LetRegNum(9,1);'); // @END: UNIX // @BEGIN: TeX et al. IfisMode('TeX;BibTeX;MetaFont;MetaPost;BST;IST','%!m',> !'LetReg(8,"%%");> LetRegNum(9,1);'); // @END: TeX et al. // @BEGIN: BAT (cmd.exe script -- ensure BAT is a defined mode or submode in modes.ini) IfisMode('BAT','%!m',> !'LetReg(8,"REM");> LetRegNum(9,1);'); // @END: BAT // @BEGIN: AHK (autohotkey script -- ensure AHK is a defined mode or submode in modes.ini) IfisMode('AHK','%!m',> !'LetReg(8,";");> LetRegNum(9,1);'); // @END: AHK // ----- 'Toggle Comments' Macro Code (you might want to leave this alone) ---- SetTracking(0); BeginGroup; GetBuild(0); IfNum(%!0,20120000,">","SetWrap(-1,0);","SetWrap(0,0);"); MarkSelection(1,2); Goto(1);LetRegNum(1,%l);LetRegNum(3,%l); Goto(2);LetRegNum(2,%l);LetRegNum(4,%l); IfNum(%!3,%!4,">","LetRegNum(1,%!4);LetRegNum(2,%!3);Goto(1);","Goto(2);"); IfNum(%c,1,"=",'IfNum(%!1,%!2,"<>","LetRegNum(2,%!2-1);");'); GetLength(5,"%!8"); Loop(!|GotoCL(1,%!1);> Repeat(%!5,!"SelCharRight;");> IfStr("%S","%!8","=",> !`Delete;> Repeat(%!9,> !'SelCharRight;> IfStr("%S"," ","=","Delete;","CharLeft;");');`,> !`GotoCL(1,%!1);InsText('%!8');Repeat(%!9,'InsText(" ");');`);> LetRegNum(1,%!1+1);> IfNum(%!1,%!2,">","Break;");> |); RestoreSelection(1,2,0); RestoreWrap; EndGroup; SetTracking(1); // Note for version 6.0 and pre April 2012 (early) version 7.0 // ----------------------------------------------------------- // In situations where: // a) the first paragraph in the selected text is a multi-line // wrapped paragraph, and // b) the first line of this multi-line wrapped paragraph is // not visible in the window (i.e., it sits above the // presently visible paragraph text), // then WinEdt v6.0 and pre April 2012 v7.0 do not properly update paragraph // colouring. In this situation, the Toggle Comment command inserts the // start-of-comment marker (e.g., '%' in TeX) at the start of the line // (as it should) but, because the start of the line lies above the visible // portion of the paragraph, WinEdt only applies the "this is a comment" // colouring to the first line in the multi-line wrap display rather than // to the paragraph as a whole. // // Solution: upgrade to WinEdt Build: 20120413 (v7.0) or more recent. // // --------------------------------- The End ----------------------------------