Community
    • Login

    Problems identifying number of open files

    Scheduled Pinned Locked Moved Notepad++ & Plugin Development
    8 Posts 5 Posters 533 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.
    • Thomas KnoefelT
      Thomas Knoefel
      last edited by

      I’m facing a strange issue when focusing on files in both the main and secondary views, using NPPM_GETNBOPENFILES and NPPM_ACTIVATEDOC.

      When I use NPPM_GETNBOPENFILES to count open files, the results seem wired as there is oly one file open in N++ but it is showing me:

      • 0 (total number of files): 2 files
      • 1 (files opened in the main view): 1 file
      • 2 (files opened in the second view): 1 file

      When i focus with NPPM_ACTIVATEDOC to focus on these files, it always focuses on the same file, irrespective of specifying MAIN_VIEW or SECOND_VIEW. This results in focusing on the same file twice.

      I tried using the compare plugin to create a secondary window. Although the file count then seems correct, attempting to focus on a file in the secondary window still results in the main window being focused.
      ::SendMessage(nppData._nppHandle, NPPM_ACTIVATEDOC, SECOND_VIEW, i);

      I want to focus all documents, regardless of whether they are in the main or secondary view, without focusing on the same document twice.

      PeterJonesP 1 Reply Last reply Reply Quote 0
      • PeterJonesP
        PeterJones @Thomas Knoefel
        last edited by

        @Thomas-Knoefel ,

        If only one View is visible, there is (in my experience) still one “open” in the other View (the new 1 or equivalent dummy document).

        If you try to NPPM_ACTIVATEDOC a document in a View that’s not visible, Notepad++ will ignore the WPARAM and instead try to activate that index from the visible View instead of the View you specified in the WPARAM.

        I cannot find a NPPM message that tells whether each view is visible. The PythonScript source code defines notepad.isSingleView() and checks whether the “splitterContainer” is visible or not, to determine if two views are open or just one. If “splitterContainer” is not visible, there’s just one View visible, and NPPM_GETCURRENTSCINTILLA will tell you which of the Views is actually active (yes, it can be just the secondary View if you happened to close/move all the tabs from the primary View).

        So you can implement a similar function for checking for 1-vs-2-visible-views, then only iterate through a View if it’s visible. That way, you won’t try to activate a document from the invisible View.

        CoisesC 1 Reply Last reply Reply Quote 0
        • CoisesC
          Coises @PeterJones
          last edited by

          @PeterJones said in Problems identifying number of open files:

          I cannot find a NPPM message that tells whether each view is visible.

          bool visibleMain   = IsWindowVisible(nppData._scintillaMainHandle);
          bool visibleSecond = IsWindowVisible(nppData._scintillaSecondHandle);
          
          PeterJonesP 1 Reply Last reply Reply Quote 4
          • PeterJonesP
            PeterJones @Coises
            last edited by

            @Coises ,

            … and this is where you can tell I’ve never written a plugin. ;-)

            I’m pretty good at the messages themselves, because of my involvement in the User Manual, and from building up a whole Perl library of SendMessage calls to interface with N++ from the outside.

            But the inside stuff, including what data structures are available inside a plugin that don’t have a messaging interface, are outside my experience. :-)

            So yes, that’s much simpler than looking for the splitterContainer and then doing a message to figure out the active View.

            Thomas KnoefelT 1 Reply Last reply Reply Quote 3
            • Thomas KnoefelT
              Thomas Knoefel @PeterJones
              last edited by Thomas Knoefel

              The IsWindowVisible command significantly helped to narrow down the problem. With this command, I was able to focus only on the actually visible files.

              However, I’ve encountered another issue that I need to address. Previously, I had only one Scintilla handler set up for the main view only. I now realize that I need to check with NPPM_GETCURRENTSCINTILLA which view to use before performing each action.

              …the fix has been implemented! I’m really pleased that this issue could be resolved so easily!

              Thanks -> @Coises , @PeterJones

              Mark OlsonM 1 Reply Last reply Reply Quote 0
              • Mark OlsonM
                Mark Olson @Thomas Knoefel
                last edited by

                @Thomas-Knoefel said in Problems identifying number of open files:

                I now realize that I need to check with NPPM_GETCURRENTSCINTILLA which view to use before performing each action.

                Everybody says that, but it’s not quite right! It turns out that you can handle multi-instance mode just fine in the following way:

                • Have a globally accessible Scintilla handle that is initialized with NPPM_GETCURRENTSCINTILLA when your plugin starts up
                • whenever the plugin receives an NPPN_BUFFERACTIVATED notification, re-initialize your Scintilla handle with NPPM_GETCURRENTSCINTILLA.

                I do this in JsonTools, and it handles multi-instance fine.

                Granted, that’s a C# plugin, and your C++ plugin(s) may need to deal with weird pointer issues that I don’t have in my happy little garbage-collected world.

                Thomas KnoefelT 1 Reply Last reply Reply Quote 4
                • Thomas KnoefelT
                  Thomas Knoefel @Mark Olson
                  last edited by

                  @Mark-Olson

                  That’s a good idea. Stuff like this should be better managed in the background. I will have a look to see if it can be handled with a listener.

                  1 Reply Last reply Reply Quote 0
                  • Andi KiisselA
                    Andi Kiissel
                    last edited by

                    I needed the number of open tabs in NppExec script, and I found this way:

                    npp_sendmsg NPPM_GETNBOPENFILES 0 0 // = total, may be wrong +1
                    set local n ~ $(MSG_RESULT)
                    npp_sendmsg NPPM_GETCURRENTDOCINDEX 0 0 // MainView, returns -1 if view is closed
                    set local n ~ $(n) - ($(MSG_RESULT)<0)
                    npp_sendmsg NPPM_GETCURRENTDOCINDEX 0 1 // SubView, returns -1 if view is closed
                    set local n ~ $(n) - ($(MSG_RESULT)<0)
                    echo $(n) // = right number of open tabs
                    

                    If somebody knows simpler method, it’s very welcome.

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