How to jump to a bookmark and show the bookmarked line at the top of the screen, not in the middle
-
@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.
-
@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
-
@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.
-
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)
-
Thanks for all your suggestions so far!
-
npp_sendmsg WM_COMMAND IDM_SEARCH_PREV_BOOKMARK
I think your
IDM_SEARCH_PREV_BOOKMARK
should really beIDM_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)
-
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).
-
@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)
Cheers.
-
@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
to0
(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)
-
@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 tonext
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
-
@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.
:-) -
@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:
and
-
@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
-
@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. :-) -
@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. -
@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
-
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! -
@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, pressShift+F2
to place the previous bookmarked line at the bottom of the screen. In my current set up14
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 theNext
orPreviuos Bookmark
commands from theSearch
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!
-
@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.
-
-
-
@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!
I have followed all the instructions over there but all in vain. Considering myself dmbest