• Login
Community
  • Login

Search in target is sometimes failing

Scheduled Pinned Locked Moved Notepad++ & Plugin Development
46 Posts 3 Posters 3.8k 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.
  • M
    Michael Vincent @Peter Goddard
    last edited by May 28, 2020, 8:44 PM

    @Peter-Goddard said in Search in target is sometimes failing:

    That could be it, but why would it be only working sometimes?

    Your search string is ANSI (const char *) but your file is Unicode?

    P 1 Reply Last reply May 28, 2020, 8:49 PM Reply Quote 2
    • E
      Ekopalypse @Peter Goddard
      last edited by May 28, 2020, 8:45 PM

      @Peter-Goddard

      As said earlier, npp does also use SearchInTarget therefore it could be
      that npp sets flags which aren’t useful in your case.
      For example, having a flag SCFIND_WHOLEWORD might conflict with
      your “xml version”.

      1 Reply Last reply Reply Quote 1
      • P
        Peter Goddard @Michael Vincent
        last edited by May 28, 2020, 8:49 PM

        @Michael-Vincent

        I will try and use SCI_TARGETFROMSELECTION. I am not sure what you mean by, “Your search string is ANSI (const char *) but your file is Unicode?”. I am very new to this all, so thanks for the help

        E 1 Reply Last reply May 28, 2020, 8:54 PM Reply Quote 0
        • E
          Ekopalypse @Peter Goddard
          last edited by Ekopalypse May 28, 2020, 8:55 PM May 28, 2020, 8:54 PM

          @Peter-Goddard said in Search in target is sometimes failing:

          I am not sure what you mean by, “Your search string is ANSI (const char *) but your file is Unicode?”

          this is actually a very advanced topic and MUST be understood by
          every programmer which handles text.
          I suggest reading this and you have to understand, that Windows API,
          internally, handles everything with UTF16 and that a char pointer is start of your nightmares. :-)

          P 1 Reply Last reply May 28, 2020, 8:58 PM Reply Quote 2
          • P
            Peter Goddard @Ekopalypse
            last edited by May 28, 2020, 8:58 PM

            @Ekopalypse
            Okay, thanks, as I said, I have limited experience with any of this stuff. I have only just started to program and I really appreciate you helping me out.

            E M 2 Replies Last reply May 28, 2020, 9:03 PM Reply Quote 0
            • E
              Ekopalypse @Peter Goddard
              last edited by May 28, 2020, 9:03 PM

              @Peter-Goddard

              Every big journey starts with a first step but you only reach your
              destination if you keep walking. ;-)

              1 Reply Last reply Reply Quote 0
              • P
                Peter Goddard @Michael Vincent
                last edited by May 28, 2020, 9:03 PM

                @Michael-Vincent

                After testing, SCI_TARGETFROMSELECTION, I think you may have been right. This seems to have fixed it

                E 1 Reply Last reply May 28, 2020, 9:13 PM Reply Quote 0
                • M
                  Michael Vincent @Peter Goddard
                  last edited by May 28, 2020, 9:09 PM

                  @Peter-Goddard said in Search in target is sometimes failing:

                  Okay, thanks, as I said, I have limited experience with any of this stuff. I have only just started to program and I really appreciate you helping me out.

                  I understand, I’m not a programmer but have muddled my way through Scintilla docs and other N++ plugins and been able to hack together some working code.

                  @Ekopalypse has it right about Unicode / ANSI. By default, all N++ plugins are compiled for Unicode as N++ no longer published an ANSI version. So the difference in your plugin when handling text in N++ documents (like searching for it) is to expect Unicode.

                  You’ll probably see character strings in N++ plugins initialized with:

                  TCHAR cString[MAX_PATH];
                  

                  since this can be ANSI or Unicode depending on compiler switches and that was needed back when there was an ANSI N++.

                  https://docs.microsoft.com/en-us/office/client-developer/outlook/mapi/tchar

                  Cheers.

                  1 Reply Last reply Reply Quote 3
                  • E
                    Ekopalypse @Peter Goddard
                    last edited by May 28, 2020, 9:13 PM

                    @Peter-Goddard

                    By the way, you defined your start and end being ints.
                    But SendMessage returns an LRESULT which is defined as LONG_PTR , so just be careful because a position might overflow it.

                    P 1 Reply Last reply May 29, 2020, 4:05 PM Reply Quote 3
                    • P
                      Peter Goddard @Ekopalypse
                      last edited by May 29, 2020, 4:05 PM

                      @Ekopalypse So how would I prevent it from overflowing?

                      E 1 Reply Last reply May 30, 2020, 11:55 AM Reply Quote 0
                      • E
                        Ekopalypse @Peter Goddard
                        last edited by Ekopalypse May 30, 2020, 11:55 AM May 30, 2020, 11:55 AM

                        @Peter-Goddard said in Search in target is sometimes failing:

                        So how would I prevent it from overflowing?

                        Not sure I understand this question because I guess you know the obvious answer,
                        which would be “use a datatype which is big enough to hold the values”, yourself.

                        P 1 Reply Last reply Jun 1, 2020, 1:20 PM Reply Quote 1
                        • P
                          Peter Goddard @Ekopalypse
                          last edited by Jun 1, 2020, 1:20 PM

                          @Ekopalypse As I said, I am very new at this so I thought I would just make sure I am understanding you correctly. I am assuming I use LONG_PTR instead of int, correct?

                          E 1 Reply Last reply Jun 1, 2020, 3:10 PM Reply Quote 0
                          • E
                            Ekopalypse @Peter Goddard
                            last edited by Jun 1, 2020, 3:10 PM

                            @Peter-Goddard

                            There are several possible data types that can be used, but without knowing your code and concept, it is hard to say what to use.
                            Personally, I would use INT_PTR, but if your result is ever converted to long, I would probably use LONG_PTR.

                            P 1 Reply Last reply Jun 1, 2020, 3:18 PM Reply Quote 0
                            • P
                              Peter Goddard @Ekopalypse
                              last edited by Peter Goddard Jun 1, 2020, 3:18 PM Jun 1, 2020, 3:18 PM

                              @Ekopalypse Would you mind looking at my code for one part of my plugin that is failing?

                              E 1 Reply Last reply Jun 1, 2020, 3:23 PM Reply Quote 0
                              • E
                                Ekopalypse @Peter Goddard
                                last edited by Jun 1, 2020, 3:23 PM

                                @Peter-Goddard

                                Sorry, but there is a small, but unfortunately insurmountable problem,
                                I cannot program in C++, I am a python junkie :-)

                                P 1 Reply Last reply Jun 1, 2020, 3:35 PM Reply Quote 0
                                • P
                                  Peter Goddard @Ekopalypse
                                  last edited by Jun 1, 2020, 3:35 PM

                                  @Ekopalypse No problem. Maybe you could just confirm that this is a memory issue for me? After restarting notepad my plugin seems to work and I can use it over and over again until it fails and then won’t work until I restart notepad. I’m pretty sure this means it’s a memory problem, and I don’t really know where to go from here

                                  E 1 Reply Last reply Jun 1, 2020, 3:51 PM Reply Quote 0
                                  • E
                                    Ekopalypse @Peter Goddard
                                    last edited by Ekopalypse Jun 1, 2020, 3:51 PM Jun 1, 2020, 3:51 PM

                                    @Peter-Goddard

                                    until it fails and then won’t work until I restart notepad

                                    yes, that could mean that you have an overflow.
                                    There are several ways to find out what is causing it.
                                    Old school, use print statements in your code and check certain parameters.
                                    More modern, use Visual Studios features like Analyzer and Debugger to see what is going on.
                                    I assume you are using Visual Studio for development,
                                    there is one setting I would suggest to activate, it is to treat every warning as an error.
                                    (Project->Properties->Configuration Properties->C/C+±>General->Treat warnings as errors->Yes)

                                    P 1 Reply Last reply Jun 1, 2020, 3:59 PM Reply Quote 1
                                    • P
                                      Peter Goddard @Ekopalypse
                                      last edited by Jun 1, 2020, 3:59 PM

                                      @Ekopalypse

                                      Thanks, I’ll try that.

                                      There is another thing I have noticed and I wonder if you could help me with it. The text file I am working on is around 40’000 lines long, and as I said before I am formatting small parts of that file and eventually it will start to mess up. What I have noticed though, is that when I copy and paste a part of the original text file to a separate notepad tab and try to format it, then once it’s formatted it, delete it and then repeat the process, the formatting process never messes up. Could it be that the length of the text file has something to do with how many times I can use my plugin on it?

                                      E 1 Reply Last reply Jun 1, 2020, 4:10 PM Reply Quote 0
                                      • E
                                        Ekopalypse @Peter Goddard
                                        last edited by Ekopalypse Jun 1, 2020, 4:12 PM Jun 1, 2020, 4:10 PM

                                        @Peter-Goddard

                                        yes, could be but to me it sounds more like an issue with the “new” content.
                                        For example, let’s say we have a text like this

                                        This is a sampl of a text which has two wrong words named sampl.
                                        

                                        Now let’s assume your plugin is used to auto correct wrong words.
                                        If your code logic would be

                                        1. find all instances of the wrong word
                                        2. Loop over the results and replace with the correct word

                                        then it would fail as you invalidate the position of the second sampl
                                        word by replacing the first one.
                                        In such a scenario replacing from bottom to top would avoid this.
                                        Of course, there are multiple other ways to solve this issue as well.

                                        P 1 Reply Last reply Jun 1, 2020, 4:19 PM Reply Quote 1
                                        • P
                                          Peter Goddard @Ekopalypse
                                          last edited by Jun 1, 2020, 4:19 PM

                                          @Ekopalypse

                                          I see what you mean, but I don’t think that applies to my situation as when my plugin fails, I can simply reopen notepad and continue to format some more content until it fails again. If this issue was happening to every instance of content all of the time then I could see why this makes sense.

                                          E 1 Reply Last reply Jun 1, 2020, 4:29 PM Reply Quote 1
                                          26 out of 46
                                          • First post
                                            26/46
                                            Last post
                                          The Community of users of the Notepad++ text editor.
                                          Powered by NodeBB | Contributors