Copying all bookmarked lines to active clipboard

Help with writing and running scripts

Copying all bookmarked lines to active clipboard

Postby traydaht » Thu Mar 24, 2011 7:45 am

Is there any way of copying all bookmarked lines?
traydaht
Newbie
 
Posts: 2
Joined: Thu Mar 24, 2011 7:37 am

Re: Copying all bookmarked lines to active clipboard

Postby Mofi » Thu Mar 24, 2011 10:36 am

Not with an internal command. There is a macro solution, see Copy to clipboard bookmarked lines?

Nowadays I would do that with a script which would have the advantage to not modify the file because the line number of first bookmark could be remembered and compared in the loop for exit condition.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4062
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Copying all bookmarked lines to active clipboard

Postby traydaht » Fri Mar 25, 2011 2:28 pm

Thanks Mofi

Sounds like an enhancement request to me.
traydaht
Newbie
 
Posts: 2
Joined: Thu Mar 24, 2011 7:37 am

Re: Copying all bookmarked lines to active clipboard

Postby Mofi » Sat Mar 26, 2011 1:55 pm

Why requesting and waiting for something which you can easily already have using existing macro or using a script added to the list of scripts?

I have developed now a script with a function to copy all bookmarked lines to active clipboard. Hard to deal with is first line of file bookmarked. Therefore the script is more complicated as I first thought to handle this situation also correct in any case.

Please note: The command selectLine() selects only the displayed line of a soft wrapped long line. Therefore the script as is does not copy the long line entirely if a wrapped line is bookmarked. For that task it would be necessary to select a bookmarked line with a regular expression search. But I think the users using this script do not work often with wrapped lines and simply turning off word-wrap before using this script and then enable it again should not be too difficult.

I expect that not many users really need that function. The menu View has already too many commands for a resolution of 1024x768 and menu Edit is also nearly full of commands. Therefore I think, such commands not needed by a really big number of UltraEdit users should not be added by IDM to UltraEdit. Such extensions can be solved by scripts or macros.

Code: Select all
function CopyBookmarkedLines () {
   // Remember active caret position.
   var nActLine = UltraEdit.activeDocument.currentLineNum;
   var nActCol = UltraEdit.activeDocument.currentColumnNum;
   if (typeof(UltraEdit.activeDocumentIdx) == "undefined") nActCol++;
   // Move caret to top of file.
   UltraEdit.activeDocument.top();
   // Move caret to next bookmark.
   UltraEdit.activeDocument.gotoBookmark(-1);
   // Get line number of first bookmark.
   var nFirstBookmark = UltraEdit.activeDocument.currentLineNum;
   // Is the caret still on first line of the file?
   if (nFirstBookmark > 1) {
      // No, then the file has bookmarked lines. But it is possible that
      // first line is also bookmarked and the caret is now already on
      // second bookmark. To check this, the Goto Previous Bookmark command
      // is executed next. If the line number is then 1, the first line
      // is bookmarked, otherwise the first Goto Next Bookmark command
      // moved the caret really to first bookmarked line in the file.
      UltraEdit.activeDocument.previousBookmark();
      if (UltraEdit.activeDocument.currentLineNum != 1) {
         UltraEdit.activeDocument.gotoLine(nFirstBookmark,1);
      } else nFirstBookmark = 1;
      // Copy the first bookmarked line to active clipboard.
      UltraEdit.activeDocument.selectLine();
      UltraEdit.activeDocument.copy();
      var nLineCount = 1;
      // Append all other bookmarked lines to active clipboard.
      while (1) {
         UltraEdit.activeDocument.gotoBookmark(-1);
         if (UltraEdit.activeDocument.currentLineNum == nFirstBookmark) {
            // All bookmarked lines copied. Restore initial position
            // of the caret and return the number of copied lines.
            UltraEdit.activeDocument.gotoLine(nActLine,nActCol);
            return nLineCount;
         }
         UltraEdit.activeDocument.selectLine();
         UltraEdit.activeDocument.copyAppend();
         nLineCount++;
      }
   }
   else {
      // Yes, either the first line is bookmarked and the first line is the
      // only bookmarked line, or the file does not contain any bookmark.
      // Bookmark on first line is very difficult to find out because it
      // is possible that the file contains only a string at top of the
      // file and no line termination at all (= no second line). The command
      // Next Bookmark moves caret to start of active line or does not move
      // the caret depending on configuration setting "Bookmark column with
      // line" if no bookmark is set. However, with moving the caret one
      // character to right if at start of first line ore to left and
      // additionally one line down (if there is a second line), remembering
      // the caret position, running Next Bookmark command again and check
      // the new caret position, it can be find out if the first line is
      // bookmarked or not, independent of the configuration setting and
      // if there is a second line at all or not. It is possible that one
      // bookmark is set on empty file, but then there is nothing to copy.
      if (UltraEdit.activeDocument.isColNum(1)) {
         UltraEdit.activeDocument.key("RIGHT ARROW");
      } else {
         UltraEdit.activeDocument.key("LEFT ARROW");
      }
      UltraEdit.activeDocument.key("DOWN ARROW");
      var nFilePos = UltraEdit.activeDocument.currentPos;
      UltraEdit.activeDocument.gotoBookmark(-1);
      if (UltraEdit.activeDocument.currentPos == nFilePos ||
          UltraEdit.activeDocument.currentLineNum == 2) {
         UltraEdit.activeDocument.gotoLine(nActLine,nActCol);
         return 0;
      }
      // Just the first line of the file is bookmarked. Copy that line
      // to the clipboard and move caret back to initial position.
      UltraEdit.activeDocument.selectLine();
      UltraEdit.activeDocument.copy();
      UltraEdit.activeDocument.gotoLine(nActLine,nActCol);
      return 1;
   }
}

if (UltraEdit.document.length > 0) {
   var nCopiedLines = CopyBookmarkedLines();
   if (!nCopiedLines) UltraEdit.messageBox("The active file does not contain any bookmarked line.","Copy Bookmarked Lines");
   else {
      var sLine = (nCopiedLines == 1) ? "line" : "lines";
      UltraEdit.messageBox(nCopiedLines.toString() + " bookmarked " + sLine + " copied to clipboard.","Copy Bookmarked Lines");
   }
}
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4062
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna


Return to Scripts

cron