Community
    • Login

    Strange behavior of the sort function, with zero-length column mode selection

    Scheduled Pinned Locked Moved General Discussion
    18 Posts 4 Posters 1.1k 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.
    • Alan KilbornA
      Alan Kilborn
      last edited by Alan Kilborn

      @guy038

      If you make a column selection, then the Scintilla function SCI_SELECTIONISRECTANGLE will return 1 meaning true.

      However, if after making the column selection, you use it to modify text, a call to SCI_SELECTIONISRECTANGLE will then return 0 meaning false, even though you can plainly see with your eyes that your column selection is still reasonable and valid. Apparently the column selection is now considered “n” separate selections (multi-select), where “n” is the number of lines covered by the selection. And…subsequent actions that depend upon column mode will possibly act strangely, as you’ve discovered.

      You can try checking the Scintilla mode yourself by going to the PythonScript console window and typing editor.selectionIsRectangle() at the >>> prompt and pressing Enter, after you’ve set up your desired conditions in the editing window. This will show you what Scintilla thinks the current state is. Of course, you’d have to add PythonScript to your plugins for this.

      Note that from the PS console window, to get back to the editing window without altering your previously made selection, you might want to use the “two taps on the Windows key” trick, to experiment further. Clicking with the mouse into the editor window will destroy your previously set up selection.

      1 Reply Last reply Reply Quote 3
      • PeterJonesP
        PeterJones @guy038
        last edited by

        @guy038 ,

        Alan reminded me in chat about the conversation PythonScript Toggleable Script?, where I developed SelectionToRectangle.py, which converted a normal stream selection to a column (rectangle) selection.

        Using the concepts from that, I saw that after your manipulation (above) the selection converts from a rectangle selection to a multi-selection. If I just ran the SelectionToRectangle.py, the modes claim to be a rectangular selection again… but looking at the selection start/end, it’s a one-row rectangle, so not very columnar.

        However, before doing the conversion, the multi-selection still had the original extents for the beginning and end of selection. So using those values to create a new stream selection, and then using the SelectionToRectangle to convert to rectangular from that new stream, I think it works as desired:

            one
            two
            three
            four
        

        Instructions:

        1. Save this script, and optionally assign it a keyboard shortcut
        2. use the one…four text above, do a column selection between the fourth space and the letters
        3. type " then backspace to replicate Guy’s issue
        4. Edit > Line Operations > Sort Lines Lexicographically Ascending – order should still be one, two, three, four, because it doesn’t sort because of multi-selection
        5. run this script
        6. Edit > Line Operations > Sort Lines Lexicographically Ascending – order should now be four, one, three, two, because it went back to column/rectangle-selection
        # encoding=utf-8
        """
        Derived from SelectionToRectangle.py from 22890-simplest.py, in response to https://community.notepad-plus-plus.org/topic/23564,
        which had (accidentally) changed a Column Selection to a Multi-Selection instead. This converts a multi-selection to a column-selection
        """
        from Npp import notepad, editor, SELECTIONMODE, STATUSBARSECTION
        
        #console.clear()
        #console.write("PRE:  {}..{} R:{} M:{}\n".format(editor.getSelectionStart(), editor.getSelectionEnd(), editor.selectionIsRectangle(), editor.getMultipleSelection()))
        
        if not(editor.selectionIsRectangle()) and (editor.getMultipleSelection()):
            ss,se = editor.getSelectionStart(), editor.getSelectionEnd()
            #console.write("GET:  {}..{} R:{} M:{}\n".format(editor.getSelectionStart(), editor.getSelectionEnd(), editor.selectionIsRectangle(), editor.getMultipleSelection()))
            editor.setSelection(ss,se)
            #console.write("SET:  {}..{} R:{} M:{}\n".format(editor.getSelectionStart(), editor.getSelectionEnd(), editor.selectionIsRectangle(), editor.getMultipleSelection()))
            editor.setSelectionMode( SELECTIONMODE.RECTANGLE )
            #console.write("RECT: {}..{} R:{} M:{}\n".format(editor.getSelectionStart(), editor.getSelectionEnd(), editor.selectionIsRectangle(), editor.getMultipleSelection()))
        
        notepad.activateFile(notepad.getCurrentFilename()) # use the activateFile() command to refresh UI; otherwise, it doesn't _look_ like column/rectangle select)
        #console.write("POST: {}..{} R:{} M:{}\n".format(editor.getSelectionStart(), editor.getSelectionEnd(), editor.selectionIsRectangle(), editor.getMultipleSelection()))
        
        PeterJonesP 1 Reply Last reply Reply Quote 1
        • PeterJonesP PeterJones referenced this topic on
        • PeterJonesP
          PeterJones @PeterJones
          last edited by PeterJones

          Some more research and experimentation:

          The reason it changed mode out of Rectangle mode is because Scintilla has a selection mode called SC_SEL_THIN which indicates “the mode after a rectangular selection has been typed into and ensures that no characters are selected”. So what Guy found is not a bug: rather, Scintilla intentionally leaves pure Rectangle Selection Mode (SC_SEL_RECTANGLE) after you make edits; and the line operation sorting functions only work with stream or rectangle selections, not with “thin” selections or multi-selections.

          The script I showed above will get you back into full rectangle mode from the THIN mode, but not from any others.

          I have now updated my original SelectionToRectangle.py to work whether the active selection is starting from a normal (“stream”) selection, from an edited-column (“thin”) selection, or from a multi-selection: all three of those will be converted into a column-mode selection. And if it’s already a column-mode (“rectangle”), it will remain unchanged.

          SelectionToRectangle.py v2022.10.06

          # encoding=utf-8
          """
          Derived from 22890-simplest.py, in response to https://community.notepad-plus-plus.org/topic/22890/
          
          2022-Oct-06: modify based on https://community.notepad-plus-plus.org/topic/23564 to convert either stream selection (old behavior) or multi-selection (new behavior) to a single column selection
          
          This is the simplest paradigm for converting a stream (normal) selection to rectangular (column):
          1) Click or arrow to where you want to start selecting
          2) Shift+Click or Shift+arrow to get to the end of the selection (ie, do a normal selection)
          3) Run this script to convert the selection from STREAM to rectangle and refresh the screen automatically to see it
          
          You can also start with a Multi-Selection (multiple Ctrl+Clicks) and convert it, with the start of the column at the lowest selection-start, and the end of the selection at the highest selection-end
          """
          from Npp import notepad, editor, SELECTIONMODE, STATUSBARSECTION
          
          if editor.getSelectionMode()==SELECTIONMODE.THIN:    # this is the mode after making an edit in rectangle(column) mode
              ss,se = editor.getSelectionStart(), editor.getSelectionEnd()
              editor.setSelection(ss,se)
          
          if editor.getSelectionMode()==SELECTIONMODE.STREAM and editor.getSelections()>1:    # this is multi-selection mode
              ss = None
              se = None
          
              for si in range(0, editor.getSelections()):
                  s0 = editor.getSelectionNStart(si)
                  s1 = editor.getSelectionNEnd(si)
                  if ss is None or s0 < ss: ss = s0
                  if se is None or s1 > se: se = s1
          
              editor.setSelection(ss,se)
          
          if editor.getSelectionMode()!=SELECTIONMODE.RECTANGLE:
              editor.setSelectionMode( SELECTIONMODE.RECTANGLE )
              notepad.activateFile(notepad.getCurrentFilename()) # use the activateFile() command to refresh UI; otherwise, it doesn't _look_ like column/rectangle select)
          
          

          ----
          2024-Oct-16 Fix: instead of using <> for “not equal”, use != (see 2024-Oct-14 post below for reasons)

          Alan KilbornA 2 Replies Last reply Reply Quote 3
          • PeterJonesP PeterJones referenced this topic on
          • Alan KilbornA
            Alan Kilborn @PeterJones
            last edited by

            @PeterJones said in Strange behavior of the sort function, with zero-length column mode selection:

            So what Guy found is not a bug…the line operation sorting functions only work with stream or rectangle selections, not with “thin” selections.

            To me it sounds like Guy found a Notepad++ bug. When sorting, a zero-width selection – declared by code as “thin” or not, user doesn’t care about that distinction in this circumstance – over a number of lines is supposed to use as the sort key the data starting in the column of the selection and continuing to end-of-line (for each line). Note that this is a bit different from a sort using a non-zero width column selection, where the sort key is only the text shown as selected on each line.

            PeterJonesP 1 Reply Last reply Reply Quote 3
            • PeterJonesP
              PeterJones @Alan Kilborn
              last edited by

              @Alan-Kilborn said in Strange behavior of the sort function, with zero-length column mode selection:

              To me it sounds like Guy found a Notepad++ bug. …

              From that perspective, it does sound like it doesn’t meet user expectations.

              So yeah, Guy should go ahead and report it as a bug. If he does, it would be best to explain that SC_SEL_RECTANGLE sorts as expected, but SC_SEL_THIN does not sort when it is expected to do so.

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

                I’d think changing THIS from:

                if (_pEditView->execute(SCI_SELECTIONISRECTANGLE))
                

                to:

                if ((_pEditView->execute(SCI_SELECTIONISRECTANGLE)) || (_pEditView->execute(SCI_GETSELECTIONMODE) == SC_SEL_THIN))
                

                should fix it.

                1 Reply Last reply Reply Quote 3
                • guy038G
                  guy038
                  last edited by guy038

                  Hello, @alan-kilborn, @peterjones and All,

                  Many thanks for your feedback ! I now understand the problem and, certainly, the last suggestion of Alan seems the correct one !

                  I’ll create a new issue, very soon ! We’ll just have to live what this tiny problem !

                  Of course, we could, as well, have simply used the Stream Selection, right before the sort. In this case, the result is always correct !


                  Now, Peter and Alan, I’m upset by a behavior, a bit off-topic, relative to the PythonScript plugin. Let me explain :

                  On my new Windows 10 laptop, I still did not transfer all my personal data from my old XP laptop. So, in the meanwhile, I use an USB key ( E: ), containing some folders whose one is 846_x64_RC3 which contains the last locale N++ version

                  After installing, as you suggested, the PythonScript plugin with the Plugins > Plugin Admin... option, I selected the Plugins > PythonScript > New Script option which created an empty Test.py file in the folder E:\846_x64_RC3\plugins\Config\PythonScript\Scripts. Then, I pasted the contents of the @pterjones script, below, and re-saved it :

                  # encoding=utf-8
                  """
                  Derived from SelectionToRectangle.py from 22890-simplest.py, in response to https://community.notepad-plus-plus.org/topic/23564,
                  which had (accidentally) changed a Column Selection to a Multi-Selection instead. This converts a multi-selection to a column-selection
                  """
                  from Npp import notepad, editor, SELECTIONMODE, STATUSBARSECTION
                  
                  #console.clear()
                  #console.write("PRE:  {}..{} R:{} M:{}\n".format(editor.getSelectionStart(), editor.getSelectionEnd(), editor.selectionIsRectangle(), editor.getMultipleSelection()))
                  
                  if not(editor.selectionIsRectangle()) and (editor.getMultipleSelection()):
                      ss,se = editor.getSelectionStart(), editor.getSelectionEnd()
                      #console.write("GET:  {}..{} R:{} M:{}\n".format(editor.getSelectionStart(), editor.getSelectionEnd(), editor.selectionIsRectangle(), editor.getMultipleSelection()))
                      editor.setSelection(ss,se)
                      #console.write("SET:  {}..{} R:{} M:{}\n".format(editor.getSelectionStart(), editor.getSelectionEnd(), editor.selectionIsRectangle(), editor.getMultipleSelection()))
                      editor.setSelectionMode( SELECTIONMODE.RECTANGLE )
                      #console.write("RECT: {}..{} R:{} M:{}\n".format(editor.getSelectionStart(), editor.getSelectionEnd(), editor.selectionIsRectangle(), editor.getMultipleSelection()))
                  
                  notepad.activateFile(notepad.getCurrentFilename()) # use the activateFile() command to refresh UI; otherwise, it doesn't _look_ like column/rectangle select)
                  #console.write("POST: {}..{} R:{} M:{}\n".format(editor.getSelectionStart(), editor.getSelectionEnd(), editor.selectionIsRectangle(), editor.getMultipleSelection()))
                  

                  The INPUT text was in E:\xxx.txt file and, after following the @peterjones instructions, everything was OK !


                  However, I do not understand why IF I select again the Plugins > PythonScript > New Script option, this time, the Save under... windows proposes to save the future .py file in the \E folder instead of the normal location E:\846_x64_RC3\plugins\Config\PythonScript\Scripts ?!

                  Do you have some hints about it ? I surely miss something obvious !

                  Thanks, in advance !

                  Best Regards,

                  guy038

                  Notepad++ v8.4.6   (64-bit)
                  Build time : Sep 25 2022 - 19:51:39
                  Path : E:\846_x64_RC3\notepad++.exe
                  Command Line : 
                  Admin mode : OFF
                  Local Conf mode : ON
                  Cloud Config : OFF
                  OS Name : Windows 10 Pro (64-bit) 
                  OS Version : 21H2
                  OS Build : 19044.2075
                  Current ANSI codepage : 1252
                  Plugins : 
                      mimeTools (2.8)
                      NppConverter (4.4)
                      NppExport (0.4)
                      ComparePlus (1)
                      BetterMultiSelection (1.5)
                      PythonScript (2)
                  

                  The command line set gives :

                  ALLUSERSPROFILE=C:\ProgramData
                  APPDATA=C:\Users\Guy\AppData\Roaming
                  BRB=C:\Program Files\HP\Sure Click\bin
                  BRS=C:\Program Files\HP\Sure Click\servers
                  CommonProgramFiles=C:\Program Files\Common Files
                  CommonProgramFiles(x86)=C:\Program Files (x86)\Common Files
                  CommonProgramW6432=C:\Program Files\Common Files
                  COMPUTERNAME=GUYTOU
                  ComSpec=C:\windows\system32\cmd.exe
                  DriverData=C:\Windows\System32\Drivers\DriverData
                  FPS_BROWSER_APP_PROFILE_STRING=Internet Explorer
                  FPS_BROWSER_USER_PROFILE_STRING=Default
                  HOMEDRIVE=C:
                  HOMEPATH=\Users\Guy
                  LOCALAPPDATA=C:\Users\Guy\AppData\Local
                  LOGONSERVER=\\GUYTOU
                  NUMBER_OF_PROCESSORS=8
                  OneDrive=C:\Users\Guy\OneDrive
                  OnlineServices=Online Services
                  OS=Windows_NT
                  Path=: E:\846_x64_RC3\notepad++.exe
                  PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
                  platformcode=AN
                  PROCESSOR_ARCHITECTURE=AMD64
                  PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 140 Stepping 1, GenuineIntel
                  PROCESSOR_LEVEL=6
                  PROCESSOR_REVISION=8c01
                  ProgramData=C:\ProgramData
                  ProgramFiles=C:\Program Files
                  ProgramFiles(x86)=C:\Program Files (x86)
                  ProgramW6432=C:\Program Files
                  PROMPT=$P$G
                  PSModulePath=C:\Program Files\WindowsPowerShell\Modules;C:\windows\system32\WindowsPowerShell\v1.0\Modules
                  PUBLIC=C:\Users\Public
                  RegionCode=EMEA
                  SESSIONNAME=Console
                  SystemDrive=C:
                  SystemRoot=C:\windows
                  TEMP=C:\Users\Guy\AppData\Local\Temp
                  TMP=C:\Users\Guy\AppData\Local\Temp
                  USERDOMAIN=GUYTOU
                  USERDOMAIN_ROAMINGPROFILE=GUYTOU
                  USERNAME=Guy
                  USERPROFILE=C:\Users\Guy
                  windir=C:\windows
                  ZES_ENABLE_SYSMAN=1
                  
                  Terry RT Alan KilbornA 2 Replies Last reply Reply Quote 1
                  • guy038G guy038 referenced this topic on
                  • Terry RT
                    Terry R @guy038
                    last edited by Terry R

                    @guy038 said in Strange behavior of the sort function, with zero-length column mode selection:

                    However, I do not understand why IF I select again the Plugins > PythonScript > New Script option, this time, the Save under… windows proposes to save the future .py file in the \E folder instead of the normal location E:\846_x64_RC3\plugins\Config\PythonScript\Scripts ?!

                    After reading that I’m wondering if a variable has been set if the first use was in relation to the E:\ drive. Now any successive use of this will use the pre-set variable of E:\ as a starting point since it’s already set.

                    Possibly a test might be to complete a fresh start of NPP and do a new script and see what the Save Under proposes.

                    Terry

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

                      @guy038

                      I also have problems with the PythonScript > New Script command suggesting a folder that isn’t the one I want. It typically suggests a folder that is the scripts folder for another version of Notepad++ that I’ve recently used. I’ve no idea why this happens; I’ve posted about this before but never came to a resolution. So…I’m just doubly careful when I save a new script, such that it is going into the correct folder!

                      1 Reply Last reply Reply Quote 1
                      • guy038G
                        guy038
                        last edited by

                        Hi All,

                        Could it be connected with the fact of putting a local N++ install on a removable drive ?

                        BR

                        guy038

                        1 Reply Last reply Reply Quote 0
                        • guy038G
                          guy038
                          last edited by guy038

                          Hi, @terry-r, @alan-kilborn, @peterjones and All

                          I’m really perplexed !!??

                          • On my USB drive, I created a new folder E:\846_x64

                          • Then, I moved the npp.8.4.6.portable.x64.7z archive, that I kept, from the folder E:\846_x64_RC3 to the new folder E:\846_x64

                          • I opened this archive and extracted all the contents in the E:\846_x64 folder

                          • I defined my preferred parameters in Settings > Preferences...

                          • And I downloaded the PythonScript plugin via the Plugin Admin

                          Bingo ! I’m pleased to tell you that, whatever the current tab, as soon as I select the Plugins > PythonScript > New Script option, the Save inder window always opens in E:\846_x64\plugins\Config\PythonScript\Scripts ;-))

                          • Finally, I added the ComparePlus and BetterMultiSelection plugins and everything is still OK !

                          So, I don’t see why it’s OK on a local N++ install, in folder E:\846_x64 and KO on a SAME local N++ install, in folder E:\846_x64_RC3 ???

                          Anyway, my problem is solved !

                          Best Regards,

                          guy038

                          …A quarter of an hour later, even if I use my old local N++ version, within the E:\846_x64_RC3 folder, everything seems OK, again ??? Really confusing !

                          1 Reply Last reply Reply Quote 0
                          • guy038G
                            guy038
                            last edited by guy038

                            Hi all,

                            As promised, I created a new issue concerning this problem :

                            https://github.com/notepad-plus-plus/notepad-plus-plus/issues/12299#issue-1401062911

                            BR

                            guy038

                            1 Reply Last reply Reply Quote 1
                            • guy038G
                              guy038
                              last edited by guy038

                              Hello @alan-kilborn and All,

                              In the post above, of this discussion :

                              https://community.notepad-plus-plus.org/post/80361

                              You said that you met the problem, sometimes in the past and that you haven’t find a solution yet to the fact that running the Plugins > PythonScript > New Script does not open the folder Local N++ install\Plugins\Config\PythonScript\Scripts, in the case of a local N++ install !

                              I’ve already found out two ways to reproduce this wrong behavior and also how to get the right folder ! Could you, Alan, @peterjones or someone else confirm my hypotheses ?


                              A wrong folder destination, when using the Plugins > PythonScript > New Script option, may occur when :

                              • You’re trying, by mistake, to open an already opened file in your current N++ session which is not saved in the ...\Plugins\Config\PythonScript\Scripts folder

                              • You’re saving a new_x file, in a specific folder, different from the ...\Plugins\Config\PythonScript\Scripts folder


                              So, an easy solution is to re-open a Python script of your current session ! Afterwards, any use of the Plugins > PythonScript > New Script option should open the right folder ...\Plugins\Config\PythonScript\Scripts ;-))

                              Remark : Note that holding the Ctrl key, while using the Plugins\Python Scripts\Scripts\"Script Name" option, does not help in this matter :-((

                              Best Regards,

                              guy038

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

                                @guy038 said in Strange behavior of the sort function, with zero-length column mode selection:

                                two ways to reproduce this wrong behavior

                                As I never do those two actions, that isn’t a way I’m getting into the circumstance.

                                My problem with it mainly occurs when I am running a different instance of N++ to test a script in a “clean” setup. I tell it to create a New Script and it wants to save the file in the correct folder but in my daily use Notepad++.

                                I have several workarounds for this, but it is annoying behavior to say the least.

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

                                  The “Strange behavior of the sort function, with zero-length column mode selection” has been SOLVED by the developers and will be in the next release, presumably 8.4.8.

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

                                    @PeterJones :

                                    SelectionToRectangle.py v2022.10.06

                                    In Peter’s Oct 6, 2022 11:20am posting, the script code contained this line:

                                    if editor.getSelectionMode()<>SELECTIONMODE.RECTANGLE:

                                    Is Peter showing his BASIC roots here by using <>?

                                    Proper Python would be to use != (or so one would think… keep reading…).

                                    What caused me to notice this is that I exclusively use Python3 (and PythonScript v3.x) these days, and such a thing causes an error there, e.g.

                                    Python 3.12.3 (tags/v3.12.3:f6650f9, Apr  9 2024, 14:05:25) [MSC v.1938 64 bit (AMD64)]
                                    >Initialisation took 3078ms
                                    >Ready.
                                    >>> a = 3
                                    >>> a <> 4
                                      File "<console>", line 1
                                        a <> 4
                                          ^^
                                    SyntaxError: invalid syntax
                                    

                                    This made me dig up PythonScript 2 and try it there, where, indeed, it is fine:

                                    Python 2.7.18 (v2.7.18:8d21aa21f2, Apr 20 2020, 13:25:05) [MSC v.1500 64 bit (AMD64)]
                                    >Initialisation took 2703ms
                                    >Ready.
                                    >>> a = 3
                                    >>> a <> 4
                                    True
                                    

                                    Probably the moral to the story is to just not use <> with Python, whatever version. :-)

                                    Perhaps some further interesting reading: https://stackoverflow.com/questions/16749121

                                    PeterJonesP 1 Reply Last reply Reply Quote 2
                                    • PeterJonesP
                                      PeterJones @Alan Kilborn
                                      last edited by

                                      @Alan-Kilborn said in Strange behavior of the sort function, with zero-length column mode selection:

                                      Is Peter showing his BASIC roots here by using <>?

                                      100 PRINT "BASIC FOREVER"
                                      200 GOTO 100
                                      
                                      1 Reply Last reply Reply Quote 3
                                      • First post
                                        Last post
                                      The Community of users of the Notepad++ text editor.
                                      Powered by NodeBB | Contributors