Strange behavior of the sort function, with zero-length column mode selection
-
Hi, All,
I would like to get your feedback on this weird behavior. Could you confirm me that there is a problem ?
IMPORTANT : Although, I installed the
BetterMultiSelection
plugin, on my local N++v8.4.6
configuration, I disabled this plugin during the tests !Follow exactly all the subsequent steps :
- In a new tab, insert the text, below :
xyz -PQR AB-C abc ABCDEF abcdef ABC-DEF -pqr ab-c x-yz X-YZ ABC abc-def XYZ
Just note that these lines are randomly sorted.
-
Now, make a zero-length column-mode selection of all the lines, right after the four space chars ( indication
Sel: 14x0
in the status bar ) -
Type in, for instance, a double-quote
"
( to be sure that you’ve entered in the column-mode ! ) -
Run the option
Edit > Line Operations > Sort Lines Lexicographically Ascending
=> No modification occur !?
-
Hit the
Del
key to delete all the"
-
Run, again, the
Edit > Line Operations > Sort Lines Lexicographically Ascending
option
=> Still, no change occur !?
-
Hit the
Left Arrow
key to cancel the colum mode -
Again, make a zero-length column-mode selection of all the lines, right after the four space chars ( indication
Sel: 14x0
in the status bar ) -
Select, for the last time, the
Edit > Line Operations > Sort Lines Lexicographically Ascending
option
=> This time, as expected, the lines are correctly sorted (
Lexicographically Ascending
), as below :-PQR -pqr AB-C ABC ABC-DEF ABCDEF X-YZ XYZ ab-c abc abc-def abcdef x-yz xyz
Notes :
-
If, instead of the leading space chars, you insert a tabulation char, for all the lines and repeat the same steps, the problem persists :-(
-
If the
BetterMultiSelection
is enabled, the problem is identical, too !
Let’s try an other test :
- Insert the example test in a new tab
xyz -PQR AB-C abc ABCDEF abcdef ABC-DEF -pqr ab-c x-yz X-YZ ABC abc-def XYZ
-
Make a zero-length column-mode selection of all the lines, right after the four space chars ( indication
Sel: 14x0
in the status bar ) -
Type in, for instance, the sharp char
#
( to be sure that you’ve entered in the column-mode ! ) -
Run the
Edit > Line Operations > Sort Lines Lexicographically Ascending
option
=> As said above, no modification occur !?
-
Now, hit simultaneously the
Alt
andShift
keys and successively theRight Arrow
andLeft Arrow
keys -
Run, again, the
Edit > Line Operations > Sort Lines Lexicographically Ascending
option
=> This time, although the resulting column mode is STILL a zero-length selection, the behaviour of the sort is correct and gives, as epected :
-PQR -pqr AB-C ABC ABC-DEF ABCDEF X-YZ XYZ ab-c abc abc-def abcdef x-yz xyz
In summary, sort does not seem to occur when, simultaneously :
-
A column-mode selection is active
-
A char have been added or deleted
-
The column-mode selection is a zero-length one
On the contrary, the sort seems correct if :
A non-zero length column-mode selection is active
OR
A resulting zero-legnth column-mode selection is active
OR
No modification of the column-mode selection have been occurredBest Regards,
guy038
P.S. :
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)
-
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.
-
@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:
- Save this script, and optionally assign it a keyboard shortcut
- use the
one
…four
text above, do a column selection between the fourth space and the letters - type
"
then backspace to replicate Guy’s issue - Edit > Line Operations > Sort Lines Lexicographically Ascending – order should still be one, two, three, four, because it doesn’t sort because of multi-selection
- run this script
- 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()))
-
-
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) -
-
@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.
-
@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.
-
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.
-
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 oldXP
laptop. So, in the meanwhile, I use anUSB
key (E:
), containing some folders whose one is846_x64_RC3
which contains the last locale N++ versionAfter installing, as you suggested, the
PythonScript
plugin with thePlugins > Plugin Admin...
option, I selected thePlugins > PythonScript > New Script
option which created an emptyTest.py
file in the folderE:\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, theSave under...
windows proposes to save the future.py
file in the\E
folder instead of the normal locationE:\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
-
-
@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
-
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!
-
Hi All,
Could it be connected with the fact of putting a local N++ install on a removable drive ?
BR
guy038
-
Hi, @terry-r, @alan-kilborn, @peterjones and All
I’m really perplexed !!??
-
On my
USB
drive, I created a new folderE:\846_x64
-
Then, I moved the
npp.8.4.6.portable.x64.7z
archive, that I kept, from the folderE:\846_x64_RC3
to the new folderE:\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 thePlugin Admin
Bingo ! I’m pleased to tell you that, whatever the current tab, as soon as I select the
Plugins > PythonScript > New Script
option, theSave inder
window always opens inE:\846_x64\plugins\Config\PythonScript\Scripts
;-))- Finally, I added the
ComparePlus
andBetterMultiSelection
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 folderE:\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 ! -
-
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
-
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 folderLocal 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 thePlugins > PythonScript > New Script
option should open the right folder...\Plugins\Config\PythonScript\Scripts
;-))Remark : Note that holding the
Ctrl
key, while using thePlugins\Python Scripts\Scripts\"Script Name"
option, does not help in this matter :-((Best Regards,
guy038
-
-
@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.
-
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.
-
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
-
@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