Community
    • Login

    How to jump to previous active tab/file with PytonScript?

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    9 Posts 3 Posters 688 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.
    • Andi KiisselA
      Andi Kiissel
      last edited by

      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?

      EkopalypseE Alan KilbornA 3 Replies Last reply Reply Quote 0
      • EkopalypseE
        Ekopalypse @Andi Kiissel
        last edited by

        @andi-kiissel

        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?

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

          @andi-kiissel

          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…

          Andi KiisselA 1 Reply Last reply Reply Quote 1
          • Andi KiisselA
            Andi Kiissel @Alan Kilborn
            last edited by

            @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.

            1 Reply Last reply Reply Quote 1
            • Alan KilbornA
              Alan Kilborn @Andi Kiissel
              last edited by

              @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. :-)

              Andi KiisselA 1 Reply Last reply Reply Quote 2
              • Andi KiisselA
                Andi Kiissel @Alan Kilborn
                last edited by

                @alan-kilborn

                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.

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

                  @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.

                  Andi KiisselA 1 Reply Last reply Reply Quote 1
                  • Andi KiisselA
                    Andi Kiissel @Alan Kilborn
                    last edited by

                    @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 view and filename.
                    There is notepad.activateIndex(view, index) but this is another story because index depends of tabs order.
                    But really, this goes too complicated for my needs, and skills.

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

                      @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.

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