Community
    • Login

    Feature request - Left cursor movement to stop at left margin

    Scheduled Pinned Locked Moved Notepad++ & Plugin Development
    25 Posts 4 Posters 17.0k 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.
    • Scott SumnerS
      Scott Sumner @Rod T
      last edited by

      @Rod-T

      So it is easy enough to implement and test out my theory. Here’s a Pythonscript two-liner that implements the behavior:

      curr_pos = editor.getCurrentPos()
      if curr_pos != editor.positionFromLine(editor.lineFromPosition(curr_pos)): editor.charLeft()
      

      Testing it out, I found that for me at least it runs fine even when holding down its shortcut key to move from some large column number back to column 1 (and stop there). So it turns out I was worried for no reason. :)

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

        Hi, @scott-sumner

        Oh, incredible ! I’m not able to make your two-lines Python script running !! So, either, I’m wrong about the purpose of your script or I’m missing something very obvious :-((

        From the above posts, I simply thought that, thanks to your script, if you hit, repeatedly, the left arrow, the caret would go backwards and finally stops, while reaching the beginning of the current line, at column 1. Am I right about it ?

        Quite curious to know why it doesn’t work :-) May be, my Win XP configuration ?

        Cheers,

        guy038

        P.S. :

        Your other script, at the address, below, works fine ! So my Python installation should not be concerned !

        https://notepad-plus-plus.org/community/topic/13571/feature-or-plugin-request/10

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

          Hi, @scott-sumner

          Oh, I’m just idiot !! I didn’t realize that your script does not catch, of course, the Scintilla keyboard actions, related to the arrow keys !

          But, when hitting the shortcut of your script, it, rather, performs a Scintilla SCI_CHARLEFT function if the caret is not located, at column 1 and does no action, when caret reaches the column 1 of the current line !

          And I can confirm that, after more that more than 2,000 repeated shortcut hits, the popup dialog, about “running two Python scripts, simultaneously”, occurred less than five times. So, not a problem, indeed !

          Best regards,

          guy038

          Scott SumnerS 1 Reply Last reply Reply Quote 0
          • Rod TR
            Rod T
            last edited by

            Hi again Scott,

            I have installed Python and set the Python path.
            I verified that Python was working ok by running an example script that worked fine.

            When I run your script from above…

            curr_pos = editor.getCurrentPos()
            if curr_pos != editor.positionFromLine(editor.lineFromPosition(curr_pos)): editor.charLeft()

            I get the following error…

            Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:42:59) [MSC v.1500 32 bit (Intel)] on win32
            Type “copyright”, “credits” or “license()” for more information.

            ========== RESTART: D:\Documents\Arduino\Irridose\Python Script.txt ==========

            Traceback (most recent call last):
            File “D:\Documents\Arduino\Irridose\Python Script.txt”, line 1, in <module>
            curr_pos = editor.getCurrentPos()
            NameError: name ‘editor’ is not defined

            Any help or comments on this would be greatly appreciated.

            Scott SumnerS 1 Reply Last reply Reply Quote 0
            • Scott SumnerS
              Scott Sumner @Rod T
              last edited by

              @Rod-T

              Ummm…sorry for the confusion. This code runs under the “Pythonscript plugin for Notepad++”, not under a standalone Python. I guess some of the confusion comes because people often talk about “Python scripts” (note the space separating the words) which I personally dislike, because it demeans Python–one should speak of “Python programs” not simply “scripts”. Over the years I’ve come to respect Python as a full and capable language, not just a “script runner”.

              Anyway, what you need is to obtain the Pythonscript plugin and get it installed. Some (most?) people new to this use the version the Plugin Manager installs, but I’ve heard that is problematic; try to find a .msi install package for the PS plugin v1.0.8.0. If you can’t find it, let me (us) know and I’ll dig up a link to it for you.

              After that you will need to tie running of my 2-line script to a shortcut key or key-combo. Eventually you probably want to assign it to left-arrow, but for quick testing I just tied it to ctrl+shift+alt+leftarrow because I currently have that unassigned. You do this by first going to the “Configuration” choice in the Plugins -> Pythonscript menus, and then “Add” this script. This will allow the script to be mappable to a key (combo) in the Shortcut Mapper.

              This all is a bit involved to set up for the first time from scratch, but it does work. I guess it depends upon how much you want your “don’t allow caret to wrap to previous line” functionality. :)

              Rod TR 1 Reply Last reply Reply Quote 0
              • Scott SumnerS
                Scott Sumner @guy038
                last edited by

                @guy038

                Not an idiot…we all miss things now and again. It is easy to think of left-arrow functionality as being hardcoded into Notepad++/Scintilla (because who would ever change what it does?), however, it is simply just another reassignable key function in the Shortcut Mapper. Beauty, eh?

                But… in theory the Pythonscript could have captured the left arrow keypress (@Claudia-Frank has shown us great things here about intercepting keypresses via hooking the message processing loop…), but that would have been too much work, and overkill for this.

                Rod TR 1 Reply Last reply Reply Quote 0
                • Rod TR
                  Rod T @Scott Sumner
                  last edited by

                  @Scott-Sumner

                  As you suggested Scott I installed the “Pythonscript Plugin for Notepad++” saved the script, added a shortcut and mapped it to the left arrow key.

                  It took a while to get there, easy when you know how, but I now have a “Stop left cursor wrap” mapped to my left arrow and it works great.

                  I’m really liking Notepad++ it’s flexibility and support. Thanks for your help with this Scott.

                  1 Reply Last reply Reply Quote 1
                  • Rod TR
                    Rod T @Scott Sumner
                    last edited by

                    @Scott-Sumner

                    I have implemented the ‘Stop Left Wrap’ quite successfully apart from one issue which I’m hoping is quite easy to fix.

                    The ‘Stop Wrap’ works ok on lines that have text entered but if you are setup so that your work space includes ‘Virtual Spaces’ or text beyond end of line and the line contains no characters then the left key will not respond. I suspect that it is because the cur_pos value returned by editor.getCurrentPos() is returning zero when the actual cursor position is not zero.

                    I am currently trying to get familiar enough with python in order to fix this but any help would be appreciated?

                    Scott SumnerS 1 Reply Last reply Reply Quote 0
                    • Scott SumnerS
                      Scott Sumner @Rod T
                      last edited by

                      @Rod-T

                      Virtual spaces were not considered in the original solution because I rarely turn the caret-beyond-EOL mode on. I find odd (unexpected) caret movements occur occasionally when I leave it on, although I’d like to have it on 100% of the time. :(

                      editor.getCurrentPos() only returns 0 when the caret is at the start of document, not at the start of each line.

                      What is needed to fix the original script is a check for virtual space; adding a single line after the other two will do it:

                      if editor.getSelectionNAnchorVirtualSpace(0) > 0: editor.charLeft()
                      
                      Rod TR 1 Reply Last reply Reply Quote 1
                      • Rod TR
                        Rod T @Scott Sumner
                        last edited by

                        @Scott-Sumner

                        That’s great, a complete fix. Thanks Scott.
                        So far, no problems with caret-beyond-EOL mode. :)

                        For those that want to implement this Stop Left Wrap function
                        here is a summary of the install process discussed above.
                        All credits to Scott-Sumner.

                        Stop Left Cursor Wrap to previous line:

                        Install Python Script Plugin

                        • Plugins -> Plugin Manager -> Show Plugin Manager -> [Available]
                        • Tick ‘Python Script’ and [install]

                        Write ‘Stop Left Wrap’ Script

                        • Plugins -> Python Script -> New Script
                        • Add “Stop Left Wrap” File name for Script and [Save]
                        • This will open a new blank text file page
                        • Copy this Python Script code to the new file and [Save]

                        curr_pos = editor.getCurrentPos()
                        if curr_pos != editor.positionFromLine(editor.lineFromPosition(curr_pos)): editor.charLeft()
                        if editor.getSelectionNAnchorVirtualSpace(0) > 0: editor.charLeft()

                        Configure Shortcut

                        • Plugins -> Python Script -> Configuration
                        • Select ‘Stop Left Wrap’ and push [Add] to Menu Items and [OK]

                        Attach Shortcut to 'left’key

                        • Run -> Modify Shortcut/Delete Command -> [Plugin Commands]
                        • scroll down and click select ‘Stop Left Wrap’
                        • select [Modify] and scroll down to Select ‘left’ Cursor key (if Conflict, still say) [OK]
                        • [Close] Should be operating now, so no more wrap on left margin.
                        Scott SumnerS 1 Reply Last reply Reply Quote 0
                        • Scott SumnerS
                          Scott Sumner @Rod T
                          last edited by

                          @Rod-T

                          Nice summary of the setup. I would suggest not letting conflicts persist in the Shortcut Mapper as a “best practice”.

                          Rod TR 1 Reply Last reply Reply Quote 0
                          • Rod TR
                            Rod T @Scott Sumner
                            last edited by

                            @Scott-Sumner

                            Fair point. In this case I couldn’t find any way to attach the script to the left key without the ‘Conflict’ warning appearing.
                            So far it has been ok. I guess the [left] key is only going to be used to move left.

                            Scott SumnerS 2 Replies Last reply Reply Quote 0
                            • Scott SumnerS
                              Scott Sumner @Rod T
                              last edited by

                              @Rod-T

                              Well, when you have conflicts, Notepad++'s Shortcut Mapper will turn the conflicts red. It is just a matter of scrolling through the list until you see the other red one. Additionally, the box near the bottom (just above the buttons) of the Shortcut Mapper window will show the conflicts for the current item. I find a quick scroll through looking for the other red one is faster than reading the box and figuring out where to move to, although the text certainly gives you a push in the direction of the correct tab the conflict is on.

                              1 Reply Last reply Reply Quote 0
                              • Scott SumnerS
                                Scott Sumner @Rod T
                                last edited by

                                @Rod-T

                                And to be specific, you’ll find that the conflict you created is almost certainly going to be matched on Line 33 of the “Scintilla commands” tab.

                                1 Reply Last reply Reply Quote 0
                                • Rod TR
                                  Rod T
                                  last edited by

                                  Thanks for the comments Scott. I’ve followed that through and Line 33 of the “Scintilla commands” tab seems to just be activating a move left. I’m not sure how Scintilla responds to the conflict and I may have expected two move lefts but that is not happening so hopefully it is ok.

                                  Scott SumnerS 1 Reply Last reply Reply Quote 0
                                  • Scott SumnerS
                                    Scott Sumner @Rod T
                                    last edited by

                                    @Rod-T

                                    What it is actually doing (I think) is finding the definition for the Pythonscript tie to the shortcut key FIRST when it is trying to figure out what to do when you press that key. So, yes, Scintilla command 33 is a “move left” but the key point is that this move left is NEVER HAPPENING because the Pythonscript that runs your new functionality is being run INSTEAD (NOT in addition to!).

                                    Conflicts are bad because you can’t predict this “which-will-get-run” behavior, in general. However, it should always act the same way when a given conflict exists. So in our example here, the Pythonscript always runs and the Scintilla move-left never does.

                                    These are all general conclusions based upon what I’m taking from your experience. I don’t let conflicts persist in my setup, so I don’t have extensive time studying this and its effects.

                                    The best thing to do in your case is to change the shortcut key for Scintilla’s move-left to “None”. If you bring up the window that allows you to change the key combination for that entry, the dropdown box will have “None” as the first entry at the top. It would be nicer to not have to select None this way, but rather have a “Remove shortcut” button, but this is not the case.

                                    I hope this helps clarify…

                                    Scott SumnerS 1 Reply Last reply Reply Quote 0
                                    • Scott SumnerS
                                      Scott Sumner @Scott Sumner
                                      last edited by

                                      @Scott-Sumner

                                      The above is NOT based upon the most current Notepad++ as of this writing (7.3.3) simply because I like to lag a bit behind the bleeding edge (I’m using 7.2.2). 7.3.3 (and possibly slighter earlier) has a “Clear” button (which sounds useful!) but there is some indication that it is buggy; see https://notepad-plus-plus.org/community/topic/13687/bug-unable-to-clear-or-remove-shortcuts-for-scintilla-commands

                                      Rod TR 1 Reply Last reply Reply Quote 0
                                      • Rod TR
                                        Rod T @Scott Sumner
                                        last edited by

                                        @Scott-Sumner

                                        Thanks Scott, it makes total sense to avoid the conflict and remove the original [left] key Scintilla command 33 response. I have done that and added it to the Install summary.

                                        For those that want to implement this Stop Left Wrap function here is a summary of the install process discussed above.
                                        All credits to Scott-Sumner.

                                        Version 2 - added remove original [left] move response.
                                        Summary below applies to Notepad++ V3.3.3 (32 bit)

                                        Stop Left Cursor Wrap to previous line:

                                        Install Python Script Plugin

                                        • Plugins -> Plugin Manager -> Show Plugin Manager -> [Available]
                                        • Tick ‘Python Script’ and [install]

                                        Write ‘Stop Left Wrap’ Script

                                        • Plugins -> Python Script -> New Script
                                        • Add “Stop Left Wrap” File name for Script and [Save]
                                        • This will open a new blank text file page
                                        • Copy this Python Script code to the new file and [Save]

                                        curr_pos = editor.getCurrentPos()
                                        if curr_pos != editor.positionFromLine(editor.lineFromPosition(curr_pos)): editor.charLeft()
                                        if editor.getSelectionNAnchorVirtualSpace(0) > 0: editor.charLeft()

                                        Configure Shortcut

                                        • Plugins -> Python Script -> Configuration
                                        • Select ‘Stop Left Wrap’ and push [Add] to Menu Items and [OK]

                                        Attach Shortcut to ‘left’ key

                                        • Run -> Modify Shortcut/Delete Command -> [Plugin Commands]
                                        • scroll down and click select ‘Stop Left Wrap’
                                        • select [Modify] and scroll down to Select ‘left’ key
                                        • A Conflict warning will appear. Still say [OK]

                                        Remove Original ‘left’ key Response

                                        • Select [Scintilla Commands] tab
                                        • scroll down to Highlighted command 33 SCI_CHARLEFT and select
                                        • select [Modify] and scroll up to Select ‘none’
                                        • select [Apply] to remove original response
                                        • select [OK] conflict warning Highlight should now be gone
                                        • [Close] Should be operating now, so no more wrap on left margin.
                                        Rod TR 1 Reply Last reply Reply Quote 1
                                        • Rod TR
                                          Rod T @Rod T
                                          last edited by Rod T

                                          Minor correction to above.

                                          The Version of Notepad that the procedure applies to is V7.3.3 not V3.3.3

                                          1 Reply Last reply Reply Quote 0
                                          • Rod TR
                                            Rod T
                                            last edited by

                                            Another correction to the above setup.

                                            Symptom: When ‘Virtual Space’ mode is enabled the cursor can move left by two places not one as expected.

                                            The problem occurs only when ‘Virtual Space’ mode is enabled. ie the ability to move the cursor beyond the end of the line.
                                            This is now quite easy to enable with the Python Plugin installed as it is one of the example scripts which can be activated with an icon in the toolbar.

                                            When moving the cursor left within the ‘Virtual Space’ area of a line with existing characters, the cursor moves left by two positions not one as expected.

                                            This happens because in this situation both of the conditions in the script for activating a charLeft () are met so it moves twice.
                                            The fix is simple, make the second ‘if’ an ‘elif’ (else if ) so either one of the conditions can activate a charLeft () but not both.

                                            The last line of the script now becomes:
                                            elif editor.getSelectionNAnchorVirtualSpace(0) > 0: editor.charLeft()

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