home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / SCRPTEXM.PAK / COMMENT.SPP < prev    next >
Encoding:
Text File  |  1997-05-06  |  2.5 KB  |  103 lines

  1. //--------------------------------------------------------------------------
  2. // Object Scripting
  3. // Copyright (c) 1996, 1997 by Borland International, All Rights Reserved
  4. //
  5. // COMMENT.SPP: Commenter. "Comments-out" the selected block, or
  6. //   uncomments the lines if they are already commented.
  7. //
  8. // USE: Select a block and run script.
  9. //--------------------------------------------------------------------------
  10. print typeid(module());
  11.  
  12. //
  13. // IDE imports.
  14. //
  15. import IDE;
  16. import editor;
  17.  
  18. comment()
  19. {
  20.   // Bail if no block.
  21.   //
  22.   if (!editor.TopView.IsValid) return;
  23.  
  24.   //
  25.   // Determine if the block is commented (i.e., "//" are first two
  26.   // non-whitespace characters of the first line.
  27.   //
  28.   declare curView = editor.TopView;
  29.   declare curBlk = curView.Block;
  30.   declare curPos = curView.Position;
  31.   curBlk.Save();
  32.   curPos.GotoLine(curBlk.StartingRow);
  33.   declare sTmp = new String(curPos.Read());
  34.   sTmp = sTmp.Trim(TRUE);  // Trim leading whitespace.
  35.   declare bComment = (sTmp.Index("//") == 1 ? TRUE : FALSE);
  36.   curBlk.Restore();
  37.  
  38.   // Call routine to comment or uncomment the block. Stash and restore cursor.
  39.   //
  40.   curPos.Save();
  41.   if (bComment) {
  42.     RemoveCommentChars();
  43.   }
  44.   else {
  45.     AddCommentChars();
  46.   }
  47.   curPos.Restore();
  48. }
  49.  
  50. //
  51. // Add comment characters to all the lines in the block.
  52. //
  53. AddCommentChars()
  54. {
  55.   declare curView = editor.TopView;
  56.   declare curBlk  = curView.Block;
  57.   declare curPos  = curView.Position;
  58.   declare lastRow = curBlk.EndingRow;
  59.  
  60.   // Walk each line and add comment characters.
  61.   //
  62.   for (declare i = curBlk.StartingRow; i <= lastRow; i++) {
  63.     curPos.Move(i, 1);
  64.     curPos.InsertText("//");
  65.   }
  66. }
  67.  
  68. //
  69. // Remove comment characters from all the lines in the block.
  70. //
  71. RemoveCommentChars()
  72. {
  73.   declare curView = editor.TopView;
  74.   declare curBlk  = curView.Block;
  75.   declare curPos  = curView.Position;
  76.   declare lastRow = curBlk.EndingRow;
  77.   declare sTmp    = new String();
  78.  
  79.   declare commentPosition;
  80.  
  81.   // Walk each line.
  82.   //
  83.   for (declare i = curBlk.StartingRow; i <= lastRow; i++) {
  84.     curPos.GotoLine(i);
  85.     sTmp.Text = curPos.Read();
  86.     commentPosition = sTmp.Index("//");
  87.  
  88.     // Be sure the entire line is a comment.
  89.     //
  90.     if (commentPosition) {
  91.       sTmp = sTmp.Trim(TRUE);
  92.       if (sTmp.Index("//") == 1) {
  93.  
  94.         // Delete comment characters.
  95.         //
  96.         curPos.Move(i, commentPosition);
  97.         curPos.Delete(2);
  98.       }
  99.     }
  100.   }
  101. }
  102.  
  103.