• Login
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.
  • N
    notdodgeball
    last edited by Mar 12, 2024, 12:31 PM

    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.

    A 1 Reply Last reply Mar 12, 2024, 1:40 PM Reply Quote 1
    • A
      Alan Kilborn @notdodgeball
      last edited by Alan Kilborn Mar 12, 2024, 1:40 PM Mar 12, 2024, 1:40 PM

      @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
      • N
        notdodgeball
        last edited by Mar 12, 2024, 8:11 PM

        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.

        A M 2 Replies Last reply Mar 12, 2024, 8:12 PM Reply Quote 0
        • A
          Alan Kilborn @notdodgeball
          last edited by Alan Kilborn Mar 12, 2024, 8:12 PM Mar 12, 2024, 8:12 PM

          @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
          • M
            Mark Olson @notdodgeball
            last edited by Mar 12, 2024, 8:33 PM

            @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
            • N
              notdodgeball
              last edited by Mar 12, 2024, 8:45 PM

              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…

              E 1 Reply Last reply Mar 15, 2024, 7:47 AM Reply Quote 0
              • N
                notdodgeball
                last edited by Mar 13, 2024, 7:25 PM

                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
                • E
                  Ekopalypse @notdodgeball
                  last edited by Mar 15, 2024, 7:47 AM

                  @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? ;-)

                  P 1 Reply Last reply Mar 15, 2024, 8:43 PM Reply Quote 4
                  • P
                    pbarney @Ekopalypse
                    last edited by Mar 15, 2024, 8:43 PM

                    @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.

                    C 1 Reply Last reply Mar 16, 2024, 4:13 PM Reply Quote 3
                    • C
                      chdikie hensley @pbarney
                      last edited by Mar 16, 2024, 4:13 PM

                      This post is deleted!
                      P 1 Reply Last reply Mar 16, 2024, 4:21 PM Reply Quote 0
                      • P
                        PeterJones @chdikie hensley
                        last edited by Mar 16, 2024, 4:21 PM

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