How to jump to previous active tab/file with PytonScript?
-
I know about Preferences -> MISC -> Document Switcher settings, but I wish to write my own script for quick jumping to previous active tab/file from MRU files list (without closing the current one!).
I found something connected with this in source code (Notepad_plus.cpp line 4148) “After closing a file choose the file to activate based on MRU list and not just last file in the list”, but I don’t know how to use this in PythonScript.
Please, can anybody help me with this? -
Do you mean the MRU list built into Windows or an MRU list created/maintained by the current session?
For example: You have the files A, B, C, D open. C is the current document, the previous one was A.
Switch to D.
The execution of the script would jump to C the first time and A the second time.Is this what you are looking for?
-
Here’s a rather simple demo script; not sure if you want any more advanced behavior than something like this…
I call this script
TabSwitchToPreviouslyUsed.py. When you run it, it will, somewhat obviously, switch to the tab that was active before the tab you are currently editing in – except for the first time you run it, when it will appear to do nothing (because at that point there was no “remembering” of where you were).# -*- coding: utf-8 -*- from __future__ import print_function #from Npp import * # note: only needed if this is going to be used via import in startup.py #------------------------------------------------------------------------------- class TSTPU(object): def __init__(self): self.visited_tab_list = [ notepad.getCurrentFilename() ] notepad.callback(self.bufferactivated_callback, [NOTIFICATION.BUFFERACTIVATED]) def bufferactivated_callback(self, args): self.visited_tab_list.append(notepad.getCurrentFilename()) def run(self): if len(self.visited_tab_list) >= 2: notepad.activateFile(self.visited_tab_list[-2]) #------------------------------------------------------------------------------- # to run via another file, e.g., startup.py, put these lines (uncommented) in that file: # import TabSwitchToPreviouslyUsed # tstpu = TabSwitchToPreviouslyUsed.TSTPU() if __name__ == '__main__': try: tstpu except NameError: tstpu = TSTPU() tstpu.run()Note that the script is rather simple-minded, as it doesn’t consider what happens if a tab that it might want to switch to no longer exists…
-
@alan-kilborn
Thank you!
It does exactly what I need.
But doesn’t the visited_tab_list grow too large?
I added a little, checked with print to console and seems to work:... def bufferactivated_callback(self, args): self.visited_tab_list.append(notepad.getCurrentFilename()) if len(self.visited_tab_list) >= 3: del self.visited_tab_list[-3] ...If tab is closed, it goes a little grazy, but soon starts to work correctly again.
-
@andi-kiissel said in How to jump to previous active tab/file with PytonScript?:
Because you said:
I wish to write my own script
I merely provided a theoretical demo, and left it to you to complete it. :-)
-
This “theoretical demo” is good enough for me :-)
It doesn’t work accurate between Main and Other View, but this doesn’t bother me.
At first I thought of something even more simpler, something like just one row notepad.activateFile(notepad_previous_active_file), but I didn’t know/find right names and inner logic. -
@andi-kiissel said:
It doesn’t work accurate between Main and Other View, but this doesn’t bother me
You could do:
self.visited_tab_list.append(str(notepad.getCurrentView) + notepad.getCurrentFilename())and then make changes to other parts of the script that would need to be made to accompany that.
I thought of something even more simpler, something like just one row notepad.activateFile(notepad_previous_active_file)
The problem with that is that the scripting doesn’t know by default what the previous file was, so there is no “simpler” one-liner possible.
-
@alan-kilborn said in How to jump to previous active tab/file with PytonScript?:
You could do:
self.visited_tab_list.append(str(notepad.getCurrentView) + notepad.getCurrentFilename())and then make changes to other parts of the script that would need to be made to accompany that.
Hmm… just by curiosity… I can’t find a PythonScript method to activate tab by
viewandfilename.
There isnotepad.activateIndex(view, index)but this is another story becauseindexdepends of tabs order.
But really, this goes too complicated for my needs, and skills. -
@andi-kiissel said in How to jump to previous active tab/file with PytonScript?:
notepad.activateIndex(view, index)
You need to put this together with
notepad.getFiles()as it is an iterable that returns all of the necessary information. See the help for that function and you’ll see what I mean.
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login