Community
    • Login

    How to jump to a bookmark and show the bookmarked line at the top of the screen, not in the middle

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    bookmarkbookmarksnavigation
    30 Posts 8 Posters 12.4k 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.
    • Sebastian FreehS
      Sebastian Freeh @Terry R
      last edited by

      @Terry-R Interesting. I think I haven’t changed anything, at least I’m not aware of anything. I’m still farily new to Notepad++. I haven’t used PythonScript so far, thanks for pointing that out

      1 Reply Last reply Reply Quote 0
      • Sebastian FreehS
        Sebastian Freeh @Terry R
        last edited by

        @Terry-R Nice idea! Just tried it out and works pretty well actually. Could be helpful in some situations. I’ll probably go for the macro, though.

        1 Reply Last reply Reply Quote 2
        • Terry RT
          Terry R
          last edited by Terry R

          @Sebastian-Freeh said in How to jump to a bookmark and show the bookmarked line at the top of the screen, not in the middle:

          I think I haven’t changed anything

          My fault. I initially thought you’d changed the shortcuts I referred to in the image of “my” shortcuts. But actually you were just using a different shortcut. If you look in the shortcut mapper for the items I was referring to you should see the keys I used will likely be what you have also.

          Well at least you have 2 ideas that should work reasonably well. It’s a bit of a leap to get on the Python train. Considering you are already programming though, it might not be too much of a job to learn “yet” another language.

          Just as a bit of a background, NPP is built on top of a couple of other products out there, “Boost C++ libraries” and “Scintilla”. You can read more about these (links are under the FAQ section) elsewhere. Boost has the “regular expression” language (if you wish to call it that) that NPP uses, and Scintilla is the editor. Scintilla has lots of functions, more than NPP presents and it may be possible one of those could do EXACTLY what you want. PythonScript is one method of linking to those functions. There are lots of references made to these in other posts here, which you can search on.

          Terry

          Michael VincentM 1 Reply Last reply Reply Quote 5
          • Michael VincentM
            Michael Vincent @Terry R
            last edited by

            @Sebastian-Freeh
            @Terry-R said in How to jump to a bookmark and show the bookmarked line at the top of the screen, not in the middle:

            PythonScript is one method of linking to those functions. There are lots of references made to these in other posts here, which you can search on.

            One other such language is NppExec and the simplest form would be:

            SCI_SENDMSG SCI_GETCURRENTPOS
            SET LOCAL POS = $(MSG_RESULT)
            SET LOCAL MASK ~ 1<<24
            
            :LISTNEXT
            SCI_SENDMSG SCI_MARKERNEXT $(POS) $(MASK)
            SET LOCAL $(LINE) = $(MSG_RESULT)
            IF $(LINE)==-1 THEN
                SET LOCAL POS = 0
                GOTO LISTNEXT
            ENDIF
            IF $(LINE)==$(POS) THEN
                SET LOCAL POS ~ $(POS) + 1
                GOTO LISTNEXT
            ENDIF
            
            SCI_SENDMSG SCI_SETFIRSTVISIBLELINE $(LINE)
            SCI_SENDMSG SCI_GOTOLINE $(LINE)
            
            :END
            

            You’d associate that script with some shortcut key and basically it finds your current position and sets the marker to find as the Notepad++ bookmark (24, bit-shifted to form the proper mask) and then finds the next mark from your current position. If the return value is -1, there is no next mark so start at the top of the document and search again. If the next mark is the same line you’re on, then go to the next line and search again.

            Note you could make the script more robust by using SCI_MARKERPREVIOUS and some logic to search both up and / or down direction with wrap-around.

            The Scintilla docs for marker searching (again, 24 is the Notepad++ bookmark and you need to bit shift it to get the mask value they speak of).

            Cheers.

            1 Reply Last reply Reply Quote 5
            • dinkumoilD
              dinkumoil @Sebastian Freeh
              last edited by dinkumoil

              @Sebastian-Freeh

              Another NppExec variant without using loops:

              npp_console keep
              
              sci_sendmsg SCI_HOMEDISPLAY
              npp_sendmsg WM_COMMAND IDM_SEARCH_PREV_BOOKMARK
              
              sci_sendmsg SCI_GETCURRENTPOS
              sci_sendmsg SCI_LINEFROMPOSITION $(MSG_RESULT)
              sci_sendmsg SCI_SETFIRSTVISIBLELINE $(MSG_RESULT)
              
              Alan KilbornA 1 Reply Last reply Reply Quote 4
              • Sebastian FreehS
                Sebastian Freeh
                last edited by

                Thanks for all your suggestions so far!

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

                  @dinkumoil

                  npp_sendmsg WM_COMMAND IDM_SEARCH_PREV_BOOKMARK

                  I think your IDM_SEARCH_PREV_BOOKMARK should really be IDM_SEARCH_NEXT_BOOKMARK, i.e., just a typo?

                  Anyway, for another take on that specific solution, here’s a PythonScript equivalent:

                  from Npp import editor, notepad, MENUCOMMAND
                  editor.homeDisplay()
                  notepad.menuCommand(MENUCOMMAND.SEARCH_NEXT_BOOKMARK)
                  curr_pos = editor.getCurrentPos()
                  curr_line = editor.lineFromPosition(curr_pos)
                  editor.setFirstVisibleLine(curr_line)
                  
                  1 Reply Last reply Reply Quote 2
                  • Alan KilbornA
                    Alan Kilborn
                    last edited by

                    I’m curious, though, how one might adapt these solutions to put the bookmarked line at the very bottom of the user’s display window.

                    I didn’t see the obvious solution to this one, in the available Scintilla functions as there is no “SCI_SETLASTVISIBLELINE”.

                    I thought maybe SCI_GETLINEVISIBLE would help, but no, it returns “true” for lines that are not currently on-screen (as long as those lines aren’t part of a “fold”, or “hidden” lines).

                    Michael VincentM 1 Reply Last reply Reply Quote 0
                    • Michael VincentM
                      Michael Vincent @Alan Kilborn
                      last edited by

                      @Alan-Kilborn said in How to jump to a bookmark and show the bookmarked line at the top of the screen, not in the middle:

                      I’m curious, though, how one might adapt these solutions to put the bookmarked line at the very bottom of the user’s display window.

                      SCI_SENDMSG SCI_GETFIRSTVISIBLELINE
                      SET LOCAL FIRST = $(MSG_RESULT)
                      
                      SCI_SENDMSG SCI_LINESONSCREEN
                      SET LOCAL LINES = $(MSG_RESULT)
                      
                      SET LOCAL LINE ~ $(FIRST) + $(LINES)
                      SCI_SENDMSG SCI_GOTOLINE $(LINE)
                      

                      SCI_LINESONSCREEN

                      Cheers.

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

                        @Michael-Vincent said in How to jump to a bookmark and show the bookmarked line at the top of the screen, not in the middle:

                        SCI_LINESONSCREEN

                        In light of that info, thanks @Michael-Vincent , I’ll offer up this script, which can do either behavior, by a simple change (of one character) from 1 to 0 (your choice!) in the first line:

                        from Npp import editor, notepad, MENUCOMMAND
                        align_bookmark_to_bottom_of_window_not_top = True if 1 else False
                        editor.homeDisplay()
                        notepad.menuCommand(MENUCOMMAND.SEARCH_NEXT_BOOKMARK)
                        line_of_bookmark = editor.lineFromPosition(editor.getCurrentPos())
                        lines_on_screen = editor.linesOnScreen() if align_bookmark_to_bottom_of_window_not_top else 1
                        editor.setFirstVisibleLine(line_of_bookmark - lines_on_screen + 1)
                        
                        1 Reply Last reply Reply Quote 2
                        • Terry RT
                          Terry R
                          last edited by

                          @Alan-Kilborn said in How to jump to a bookmark and show the bookmarked line at the top of the screen, not in the middle:

                          I’ll offer up this script, which can do either behavior

                          @Michael-Vincent @dinkumoil also, actually the OP stated Shift-F2 which is previous bookmark. I see through all the iterations of solutions that seems to have changed to next bookmark.

                          Maybe someone might want to offer up a solution for a choice of forward or backwards and bookmarked line at top or at bottom. So in effect 4 options. How can this be achieved within the 1 solution. Is it possible to have NppExec do a quick question on screen before enacting the request. So asking whether next or previous and top or bottom.

                          I’m currently going through a course on Python (just started) as I was interested in some of the workings behind NPP (Scintilla functions) but maybe I need to reconsider and look at NppExec. Where does one learn NppExec?

                          Terry

                          Alan KilbornA 2 Replies Last reply Reply Quote 3
                          • Alan KilbornA
                            Alan Kilborn @Terry R
                            last edited by

                            @Terry-R said in How to jump to a bookmark and show the bookmarked line at the top of the screen, not in the middle:

                            actually the OP stated Shift-F2 which is previous bookmark

                            Ah, okay, so that is why @dinkumoil did what he did, and makes my typing of:

                            I think your IDM_SEARCH_PREV_BOOKMARK should really be IDM_SEARCH_NEXT_BOOKMARK, i.e., just a typo?

                            nicely invalid.
                            :-)

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

                              @Terry-R said in How to jump to a bookmark and show the bookmarked line at the top of the screen, not in the middle:

                              a solution for a choice of forward or backwards and bookmarked line at top or at bottom

                              So the problem here is that nobody would want to answer such questions each time they want to jump to a bookmark. :-)

                              So it becomes a “configuration” issue, that you’d answer once, perhaps the first time you run the script in a N++ session?

                              Or maybe there’s a second script to configure it, and the main script uses the configuration? To change the configuration mid-session, you’d run the config script again.

                              Lots of things are possible… :-)

                              Config with Pythonscript could be two message-boxes based questions:

                              Imgur

                              and

                              Imgur

                              1 Reply Last reply Reply Quote 4
                              • Terry RT
                                Terry R
                                last edited by

                                @Alan-Kilborn said in How to jump to a bookmark and show the bookmarked line at the top of the screen, not in the middle:

                                r maybe there’s a second script to configure it, and the main script uses the configuration?

                                Yes I was also thinking along those lines. Not knowing how that’s possible I could only assume each time the next/prev bookmark wanted the options would have to be presented. So I take it that within a “session” of NPP these settings can be saved across successive runs of the script?

                                I don’t suppose it’s possible to save these within one of NPP’s XML files so in fact the config is saved across consecutive runs of NPP? Or would one just create a new XML file with the settings that ONLY that script looks for?

                                Terry

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

                                  @Terry-R said in How to jump to a bookmark and show the bookmarked line at the top of the screen, not in the middle:

                                  these settings can be saved across successive runs of the script?

                                  A Python “global” variable can hold the value. Two different scripts can access. Or, the config script could write a disk file and the main script could read it (getting to the stuff below).

                                  I don’t suppose it’s possible to save these within one of NPP’s XML files so in fact the config is saved across consecutive runs of NPP?

                                  No.
                                  Or at least: “I wouldn’t do that”

                                  Or would one just create a new XML file with the settings that ONLY that script looks for?

                                  It’s so simple, I wouldn’t bother with XML.
                                  I’d go simpler, like the days when .ini files ruled the day. :-)

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

                                    @Terry-R said in How to jump to a bookmark and show the bookmarked line at the top of the screen, not in the middle:

                                    I’m currently going through a course on Python (just started) as I was interested in some of the workings behind NPP (Scintilla functions) but maybe I need to reconsider and look at NppExec. Where does one learn NppExec?

                                    NppExec is fine, but I would definitely stay the course with the Python!
                                    More of an investment, but a lot of payoff.

                                    1 Reply Last reply Reply Quote 2
                                    • Terry RT
                                      Terry R
                                      last edited by

                                      @Alan-Kilborn said in How to jump to a bookmark and show the bookmarked line at the top of the screen, not in the middle:

                                      I’d go simpler, like the days when .ini files ruled the day. :-)

                                      So one final question (possibly), is it possible to have 2 shortcuts (ex Shift-F2 and F2, as they ARE the prev and next bookmark options) BOTH mapped to a new script, and then that script can read what invoked it (so able to read which shortcut was used), then act accordingly?

                                      Just a thought as that would save the interaction window with user. The top or bottom option would be set as unlikely user would want to continually change that. Maybe the script would ask first time invoked each session which option top/bottom and write that away (to your .ini file).

                                      Terry

                                      Alan KilbornA astrosofistaA 2 Replies Last reply Reply Quote 3
                                      • Alan KilbornA
                                        Alan Kilborn @Terry R
                                        last edited by Alan Kilborn

                                        All good ideas and thinking, Terry!
                                        Your future scripts will be as good as your regexes. :-)

                                        @Terry-R said in How to jump to a bookmark and show the bookmarked line at the top of the screen, not in the middle:

                                        is it possible to have 2 shortcuts (ex Shift-F2 and F2, as they ARE the prev and next bookmark options) BOTH mapped to a new script, and then that script can read what invoked it (so able to read which shortcut was used), then act accordingly?

                                        PythonScripts don’t have any information about what keys invoked them, AFAIK.
                                        A single script can only be tied to a single shortcut.
                                        So, you’d have to have 2 scripts for what you describe.
                                        Of course, each could call a third script with a bunch of common stuff (the “meat”) in it, the first 2 scripts being very short and just passing in arguments to the third to tell it what to do, but that would be THREE when you wanted ONE!

                                        Jaguar AsadJ 1 Reply Last reply Reply Quote 3
                                        • astrosofistaA
                                          astrosofista @Terry R
                                          last edited by

                                          @Terry-R said in How to jump to a bookmark and show the bookmarked line at the top of the screen, not in the middle:

                                          So one final question (possibly), is it possible to have 2 shortcuts (ex Shift-F2 and F2, as they ARE the prev and next bookmark options) BOTH mapped to a new script, and then that script can read what invoked it (so able to read which shortcut was used), then act accordingly?

                                          Hi @Terry-R, @Alan-Kilborn, All:

                                          AutoHotkey is quite good for what you asked. This short script doesn’t actually deal with the internals of Notepad++, just plays with the user interface.

                                          #IfWinActive ahk_class Notepad++
                                          ~F2::
                                          ~+F2::
                                          Term := SubStr(A_ThisHotkey,2,1)
                                          if (Term = "+") {
                                          	SendInput, ^{Up 14}
                                          } else {
                                          	SendInput, ^{Down 14}
                                          }
                                          return
                                          #IfWinActive
                                          

                                          Press F2 to place the next bookmarked line at the top of the screen, press Shift+F2 to place the previous bookmarked line at the bottom of the screen. In my current set up 14 is the number of lines to send up or down, but you may need to adjust it to your needs.

                                          The tilde ~ lets the hotkey sequence get through. If you ever want to use the standard behaviour of the bookmark command —place the bookmarked line at the middle of the screen—, just select the Next or Previuos Bookmark commands from the Search menu.

                                          Finally, the #IfWinActive directive ensures that the hotkeys will not interfere with applications other than Notepad++, that is, they won’t work system wide.

                                          Have fun!

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

                                            @astrosofista said

                                            14

                                            A limitation of that approach (OP had previously said “it does happen that I resize the window on different screens”), but interesting.

                                            1 Reply Last reply Reply Quote 0
                                            • P CooperP P Cooper referenced this topic on
                                            • P CooperP P Cooper referenced this topic on
                                            • First post
                                              Last post
                                            The Community of users of the Notepad++ text editor.
                                            Powered by NodeBB | Contributors