Community
    • Login

    Column mode smart selection shortcut

    Scheduled Pinned Locked Moved General Discussion
    11 Posts 7 Posters 2.2k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • notdodgeballN
      notdodgeball
      last edited by

      I’m trying to find a way to improve my usage of the Column mode option, thus I’m in the need of something like this:
      a

      Basically, move the carret up (or down) until it hits the “top” (or “bottom”) so I don’t have to press Alt+Shift that many times, or use the mouse, god forbid. A “smart” column mode selection if you will. It would be even more helpful if also worked like that:

      In other words, move upwards until either the line doesn’t go that far or the left or right character is different from those at the initial position. Or at least it’s how I think a script should handle it. Yes, a script, I think that’s the only way… Although it would be nice to have it as a standard feature, I’m sure other people would find it useful.

      So before I dabble in python or lua or something, if anyone could point me in the right direction, preferably a easier way or “it’s already been done, stupid” I’d appreciate.

      Alan KilbornA 1 Reply Last reply Reply Quote 1
      • Alan KilbornA
        Alan Kilborn @notdodgeball
        last edited by Alan Kilborn

        @notdodgeball said in Column mode smart selection shortcut:

        so I don’t have to press Alt+Shift that many times

        Well, there’s this that may help:

        dd2a3038-6b1d-49e2-bf82-e5c0f73338af-image.png

        User manual ref..

        1 Reply Last reply Reply Quote 3
        • notdodgeballN
          notdodgeball
          last edited by

          Ok, my first python script:

          if editor.getSelectionEmpty():
          
            currentLine = editor.lineFromPosition(editor.getCurrentPos())
            currentLineLength = editor.lineLength(currentLine)
            currentLineStart = editor.positionFromLine(currentLine)
            
            currentPos = editor.getCurrentPos()
            currentHorizontalPos = currentPos - currentLineStart
            currentChar = editor.getTextRange(currentPos, currentPos + 1)
          
            for x in range(1, 69):
              previousLineStart = editor.positionFromLine(currentLine-x)
              previousLinelength = editor.lineLength(currentLine-x)
              
              if editor.lineLength(currentLine-x) < currentHorizontalPos:
                break
              
              previousLineSamePosition = previousLineStart + currentHorizontalPos
              previousChar = editor.getTextRange(previousLineSamePosition, previousLineSamePosition + 1)
          
              if currentChar != previousChar:
                break
              else:
                editor.addSelection(previousLineSamePosition,previousLineSamePosition)
          

          There is better ways to do it, I assume, but it worked so far. Please tell me if there is something outstandingly bad.

          Alan KilbornA Mark OlsonM 2 Replies Last reply Reply Quote 0
          • Alan KilbornA
            Alan Kilborn @notdodgeball
            last edited by Alan Kilborn

            @notdodgeball said in Column mode smart selection shortcut:

            if there is something outstandingly bad

            Probably this is a leading candidate:

            69

            1 Reply Last reply Reply Quote 1
            • Mark OlsonM
              Mark Olson @notdodgeball
              last edited by

              @notdodgeball
              To expand on why @Alan-Kilborn said the number 69 is “oustandingly bad”:

              It’s (usually) bad practice to include “magic numbers” like 69 in your code with no explanation as to why you chose that number. Better to declare a global constant (preferably with an explanatory comment) like so, and then reference it later on in the code:

              # I'm using a proprietary file format where every file has 69 lines
              MY_MAGIC_NUMBER = 69
              ...
              ...
              for x in range(1, MY_MAGIC_NUMBER):
              

              That may reduce your confusion weeks later when you stumble across this script and wonder what the hell it’s supposed to do.

              1 Reply Last reply Reply Quote 0
              • notdodgeballN
                notdodgeball
                last edited by

                Yeah, I know, I thought putting it as 69 would make it clear but yes, you are right.

                Regardless, I was putting it into use and it’s actually worse than that. When the file has tabs, getCurrentPos() it does not line up as the position varies depending on the tabulation. How do circumvent it? I assume getColumn() might help looking at the docs. Hum…

                EkopalypseE 1 Reply Last reply Reply Quote 0
                • notdodgeballN
                  notdodgeball
                  last edited by

                  if editor.getSelectionEmpty():
                    
                    currentLine = editor.lineFromPosition(editor.getCurrentPos())
                    currentLineLength = editor.lineLength(currentLine)
                    currentPos = editor.getCurrentPos()
                    currentCol = editor.getColumn(currentPos)
                    currentLineStart = editor.positionFromLine(currentLine)
                    
                    currentHorizontalPos = currentPos - currentLineStart
                    currentRChar = editor.getTextRange(currentPos, currentPos + 1)
                    currentLChar = editor.getTextRange(currentPos, currentPos - 1)
                    
                    count = 0
                    while True:
                      
                      count += 1
                      previousLine = currentLine-count
                      previousLineStart = editor.positionFromLine(previousLine)
                      previousLineLength = editor.lineLength(previousLine)
                      previousPos = editor.findColumn(previousLine,currentCol)
                      previousCol = editor.getColumn(previousPos)
                      
                      previousRChar = editor.getTextRange(previousPos, previousPos + 1)
                      previousLChar = editor.getTextRange(previousPos, previousPos - 1)
                      
                      if currentRChar != previousRChar or currentLChar != previousLChar or previousCol != currentCol:
                        break
                      
                      editor.addSelection(previousPos,previousPos)
                  
                  
                  1 Reply Last reply Reply Quote 0
                  • EkopalypseE
                    Ekopalypse @notdodgeball
                    last edited by

                    @notdodgeball said in Column mode smart selection shortcut:

                    I thought putting it as 69 would make it clear but yes

                    Shouldn’t that be 42 instead? ;-)

                    pbarneyP 1 Reply Last reply Reply Quote 4
                    • pbarneyP
                      pbarney @Ekopalypse
                      last edited by

                      @Ekopalypse said in Column mode smart selection shortcut:

                      Shouldn’t that be 42 instead?

                      That just selects everything. And by “everything,” I mean everything: life, the universe, etc.

                      chdikie hensleyC 1 Reply Last reply Reply Quote 3
                      • chdikie hensleyC
                        chdikie hensley @pbarney
                        last edited by

                        This post is deleted!
                        PeterJonesP 1 Reply Last reply Reply Quote 0
                        • PeterJonesP
                          PeterJones @chdikie hensley
                          last edited by

                          This post is deleted!
                          1 Reply Last reply Reply Quote 0
                          • First post
                            Last post
                          The Community of users of the Notepad++ text editor.
                          Powered by NodeBB | Contributors