Community
    • Login

    PythonScript get filenames in both views

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    4 Posts 2 Posters 521 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.
    • Michael VincentM
      Michael Vincent
      last edited by Michael Vincent

      PythonScript experts:

      Is there a way to get the name of the “active” file in both views? That is, say I have split into 2 Scintilla views - left and right - and there is a file in each. From an application and notepad object view, there is only 1 active file - the one with focus and I can get that with notepad.getCurrentFilename(). But the file in the other Scintilla view that is “viewable” but not “active” in terms of focus - how do I get it’s name?

      I see there is :

      • getopenfilenames
      • getopenfilenamesprimary
      • getopenfilenamessecond

      but they don’t seem to be implemented in PythonScript. Also, not sure how cycling through all opened files will be able to tell me which one is the “viewable” (but not “focus” “active”) in the secondary view.

      Cheers.

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

        @michael-vincent said in PythonScript get filenames in both views:

        Is there a way to get the name of the “active” file in both views?

        for (filename, bufferID, index, view) in notepad.getFiles():
            if index == notepad.getCurrentDocIndex(0) and view == 0:
                print('view0 file:', filename)
            elif index == notepad.getCurrentDocIndex(1) and view == 1:
                print('view1 file:', filename)
        
        1 Reply Last reply Reply Quote 4
        • Alan KilbornA
          Alan Kilborn
          last edited by

          The above question and code leads to making a nice function like this:

          def notepad_getCurrentFilename(view_to_query=None):
              if view_to_query == None:
                  return notepad.getCurrentFilename()
              for (filename, bufferID, index, view) in notepad.getFiles():
                  if view == view_to_query and index == notepad.getCurrentDocIndex(view_to_query):
                      return filename
              return ''
          

          and some code to exercise it:

          print('notepad_getCurrentFilename():', notepad_getCurrentFilename())
          print('notepad_getCurrentFilename(0):', notepad_getCurrentFilename(0))
          print('notepad_getCurrentFilename(1):', notepad_getCurrentFilename(1))
          current_view = notepad.getCurrentView()
          print('notepad_getCurrentFilename(current_view):', notepad_getCurrentFilename(current_view))
          other_view = 1 if current_view == 0 else 0
          print('notepad_getCurrentFilename(other_view):', notepad_getCurrentFilename(other_view))
          

          Some cautions/surprises:

          If only one view is open, it could be view1 (aka editor2 aka “secondary”) not view0 (aka editor1 aka “primary”)!

          Note: “aka” = also known as

          If a view is not open and you ask for the getCurrentDocIndex() on that view, it will return an “interesting” thing:

          • On 32-bit Notepad++, 4294967295L is returned. As an unsigned number, this is FFFFFFFF in hex, leading to a possible signed interpretation of -1 in 32-bit land.

          • On 64-bit Notepad++, 18446744073709551615 is returned. As an unsigned number, this is FFFFFFFFFFFFFFFF in hex, leading to a possible signed interpretation of -1 in 64-bit land.

          I’m not sure why PythonScript can’t just give us a -1 meaning that the view specified is not open.

          A possible workaround is to see if the return value is greater than 4294967294 and if so deduce that the view is not open:

          view = 1
          print('view{v} is{n} open'.format(v=view, n=' not' if notepad.getCurrentDocIndex(view) > 4294967294 else ''))
          

          Or one could run the for loop on notepad.getFiles() and remember which of the views is encountered:

          view_is_open = [ False, False ]
          for (filename, bufferID, index, view) in notepad.getFiles(): view_is_open[view] = True
          print('view_is_open:', view_is_open)
          

          We could turn that into a function:

          def notepad_isViewOpen(view_to_test):
              view_is_open = [ False, False ]
              for (filename, bufferID, index, view) in notepad.getFiles(): view_is_open[view] = True
              return True if view_is_open[view_to_test] else False
          

          and some code to exercise it:

          print('notepad_isViewOpen(0):', notepad_isViewOpen(0))
          print('notepad_isViewOpen(1):', notepad_isViewOpen(1))
          
          Alan KilbornA 1 Reply Last reply Reply Quote 2
          • Alan KilbornA
            Alan Kilborn @Alan Kilborn
            last edited by

            @Alan-Kilborn said:

            Regarding:

            If a view is not open and you ask for the getCurrentDocIndex() on that view, it will return an “interesting” thing

            See:

            https://github.com/bruderstein/PythonScript/issues/255

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