Community
    • Login

    How to Set Lexer Language after OpenFile

    Scheduled Pinned Locked Moved Notepad++ & Plugin Development
    10 Posts 4 Posters 824 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.
    • Paul BakerP
      Paul Baker
      last edited by Paul Baker

      Hi, How do I set the file language type after a file is opened in a new tab?

      With the snippet below, the OpenFile function works fine. A new file is opened in NPP. After the file is open, I want to set the language type.

      What I have (see below) is not working. The debugger shows that GetLexerLanguage returns “null”. When I use Set/GetLexer I get a value of 1 (note LangType.L_COBOL is 50). The documentation stated Get/SetLexerLanguage should be used over Get/SetLexer.

      What am I doing wrong? I can’t set the ReadOnly attribute either.

      Any HELP will be greatly appreciated.

      Envt:

      • Using C#
      • Kbg.Demo.Namespace (kbg template)
      • NotePad++ 8.6.1

      Paul

      
              static IScintillaGateway editor = new ScintillaGateway(PluginBase.GetCurrentScintilla());
              static INotepadPPGateway notepad = new NotepadPPGateway();
      ...
      ...
      
              notepad.OpenFile(path);
              editor.SetLexerLanguage("cobol");
              string lang = editor.GetLexerLanguage();  // returns "null"
      
              editor.SetLexer((int) LangType.L_COBOL); 
              int langType = editor.GetLexer();         // returns 1
      
              editor.SetReadOnly(true);
      
      
      Mark OlsonM 1 Reply Last reply Reply Quote 0
      • Mark OlsonM
        Mark Olson @Paul Baker
        last edited by Mark Olson

        @Paul-Baker
        Yeah, I understand why you’re confused; there are too many different ways to do this thing, most of them deprecated.

        I use INotepadPPGateway.SetCurrentLanguage(LangType language). I don’t think there’s another non-deprecated way to do it.

        You’ll have to look at the LangType enum in Msgs_h.cs to see if it has the language(s) you want.

        If you can’t find a language you want in the LangType enum, try using this updated version, based on Notepad_plus_msgs.h from the notepad-plus-plus main repo:

            public enum LangType
            {
                L_TEXT, L_PHP , L_C, L_CPP, L_CS, L_OBJC, L_JAVA, L_RC,
                L_HTML, L_XML, L_MAKEFILE, L_PASCAL, L_BATCH, L_INI, L_ASCII, L_USER,
                L_ASP, L_SQL, L_VB, L_JS, L_CSS, L_PERL, L_PYTHON, L_LUA, 
                L_TEX, L_FORTRAN, L_BASH, L_FLASH, L_NSIS, L_TCL, L_LISP, L_SCHEME,
                L_ASM, L_DIFF, L_PROPS, L_PS, L_RUBY, L_SMALLTALK, L_VHDL, L_KIX, L_AU3,
                L_CAML, L_ADA, L_VERILOG, L_MATLAB, L_HASKELL, L_INNO, L_SEARCHRESULT,
                L_CMAKE, L_YAML, L_COBOL, L_GUI4CLI, L_D, L_POWERSHELL, L_R, L_JSP,
                L_COFFEESCRIPT, L_JSON, L_JAVASCRIPT, L_FORTRAN_77, L_BAANC, L_SREC,
                L_IHEX, L_TEHEX, L_SWIFT,
                L_ASN1, L_AVS, L_BLITZBASIC, L_PUREBASIC, L_FREEBASIC, 
                L_CSOUND, L_ERLANG, L_ESCRIPT, L_FORTH, L_LATEX, 
                L_MMIXAL, L_NIM, L_NNCRONTAB, L_OSCRIPT, L_REBOL, 
                L_REGISTRY, L_RUST, L_SPICE, L_TXT2TAGS, L_VISUALPROLOG,
                L_TYPESCRIPT, L_JSON5, L_MSSQL, L_GDSCRIPT, L_HOLLYWOOD,
                // Don't use L_JS, use L_JAVASCRIPT instead
                // The end of enumated language type, so it should be always at the end
                L_EXTERNAL
            }
        
        Paul BakerP 1 Reply Last reply Reply Quote 1
        • Paul BakerP
          Paul Baker @Mark Olson
          last edited by Paul Baker

          @Mark-Olson said in How to Set Lexer Language after OpenFile:

          Yeah, I understand why you’re confused; there are too many different ways to do this thing, most of them deprecated.

          SOLVED

          Thanks again Mark. I saw the SetCurrentLanguage but I thought that was for English/Spanish/etc. I just tried using INotepadPPGateway.SetCurrentLanguage(LangType language) works perfect! TY

          Now, what about the editor.SetReadOnly(true). That’s not working, and recommendations there?

          Update ReadOnly Solved
          See below…

                      ...
                      ...
                      ...
                      if (!File.Exists(path)) { 
                          MessageBox.Show(path + " does not exist. Check proc or proc file name(s).");
                          return;
                      }
          
                      // Open the proc file in a new window.
                      // Set the file langauge type to COBOL.
                      // Set the read only flag (note this will not set the read only check mark
                      // on the tab.
          
                      notepad.OpenFile(path);
                      notepad.SetCurrentLanguage(LangType.L_COBOL);
          
                      // Use Menu Command feature to set ReadOnly.
                      // In this case it is Edit | Set Read Only
                      Win32.SendMessage(PluginBase.nppData._nppHandle, (uint)NppMsg.NPPM_MENUCOMMAND, 0, NppMenuCmd.IDM_EDIT_SETREADONLY);
          
                      //   editor.SetReadOnly(true);   // Does not work.
          
                      return;
          
          
          Mark OlsonM 1 Reply Last reply Reply Quote 0
          • Mark OlsonM
            Mark Olson @Paul Baker
            last edited by

            @Paul-Baker said in How to Set Lexer Language after OpenFile:

            Now, what about the editor.SetReadOnly(true). That’s not working, and recommendations there?

            I’ve never used that before; I can’t comment on it.

            If I can’t help you with something, you can also go looking in the source code of some of the other plugins based on this template (although I don’t know of any that set the editor to read-only). The most actively maintained plugins from the template with other maintainers are CsvLint and NavigateTo, and everything else hasn’t been maintained in around a year, meaning that they are likely to be using deprecated methods.

            Paul BakerP 1 Reply Last reply Reply Quote 0
            • Paul BakerP
              Paul Baker @Mark Olson
              last edited by

              @Mark-Olson said in How to Set Lexer Language after OpenFile:

              I’ve never used that before; I can’t comment on it.

              Mark, that for your continued help. Your comments are very helpful. I just noticed that the SetReadOnly function does work… It just does not check the ReadOnly check mark on the tab. I can live with that. :)

              I see the tab context menu has a option (Apply Color to Tab). I don’t see any NPP functions that would enable me to use that feature. Any ideas on that?

              Thanks again for you help!!!

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

                @Paul-Baker

                Regarding read-only not setting the tab icon… If you tell Scintilla directly to set the document to read-only (I haven’t looked, but presume this is what is going on), then the Notepad++ user interface has no idea what you’ve done, so it keeps the tab icon the way it is. I’m sure there’s a way (using menu command ids) to tell Notepad++ (instead of Scintilla) to make the current tab read-only; this way both the UI and Scintilla (which will get notified) stay in-sync.

                Regarding applying colors to tabs, same idea (use menu command ids).

                I saw the SetCurrentLanguage but I thought that was for English/Spanish/etc

                FYI: In Notepad++ speak, “language” refers to C/C++/C#/html/xml, etc. It refers to the UI presentation text (e.g. English/Spanish/French, etc.) as “localization”.

                Paul BakerP 1 Reply Last reply Reply Quote 2
                • Paul BakerP
                  Paul Baker @Alan Kilborn
                  last edited by

                  @Alan-Kilborn

                  @Alan-Kilborn said in How to Set Lexer Language after OpenFile:

                  Regarding read-only not setting the tab icon… If you tell Scintilla directly to set the document to read-only (I haven’t looked, but presume this is what is going on), then the Notepad++ user interface has no idea what you’ve done,

                  That make perfect sense and that may still be related to my latest ReadOnly problem. When I setReadOnly(true), the editor is read only. You can not type. However, if you switch tabs and then switch back, it’s no longer read only.

                  I’ll investigate more and start a new thread if needed.

                  @Alan-Kilborn said in How to Set Lexer Language after OpenFile:

                  FYI: In Notepad++ speak, “language” refers to C/C++/C#/html/xml, etc. It refers to the UI presentation text (e.g. English/Spanish/French, etc.) as “localization”.

                  Yes, while not apparent, I’m very familiar with localization. I guess the NPP program comments I looked at just weren’t clear. Anyway, again your comments are appreciated.

                  Paul BakerP Alan KilbornA rdipardoR 3 Replies Last reply Reply Quote 1
                  • Paul BakerP
                    Paul Baker @Paul Baker
                    last edited by

                    Read Only issue fixed…

                    Using the suggestion above I located the read only command Id and used it to set read only. Works perfect! Thanks everyone!

                    Win32.SendMessage(PluginBase.nppData._nppHandle, (uint)NppMsg.NPPM_MENUCOMMAND, 0, NppMenuCmd.IDM_EDIT_SETREADONLY);
                    
                    1 Reply Last reply Reply Quote 1
                    • Alan KilbornA
                      Alan Kilborn @Paul Baker
                      last edited by Alan Kilborn

                      @Paul-Baker said in How to Set Lexer Language after OpenFile:

                      When I setReadOnly(true), the editor is read only. You can not type

                      Yep, when you tell Scintilla to do that, typing into the doc has no effect.

                      if you switch tabs and then switch back, it’s no longer read only.

                      This is a problem with “subverting” Notepad++'s control. Notepad++ is the “master”: every time you switch tabs, one of the things it does is check what it thinks the read-only status is for that tab. If you’ve only told Scintilla that the tab should be read-only, Notepad++ thinks it should be read-write, and makes it so on a tab switch (for the tab becoming active).

                      1 Reply Last reply Reply Quote 1
                      • rdipardoR
                        rdipardo @Paul Baker
                        last edited by

                        @Paul-Baker said in How to Set Lexer Language after OpenFile:

                        if you switch tabs and then switch back . . .

                        The NPPN_READONLYCHANGED notification exists for use cases like that.

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