• Login
Community
  • Login

language select on notepad launch

Scheduled Pinned Locked Moved Notepad++ & Plugin Development
9 Posts 4 Posters 874 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.
  • L
    lubysj
    last edited by Mar 13, 2020, 7:05 PM

    Hi

    I would like notepad automatically select user defined language based on the first line content.

    I have been trying to practice using command like this shown below but did not work. I think there is more to it than i thought.

    const char * lan = “xml”;
    ::SendMessage(nppData._nppHandle, SCI_SETLEXERLANGUAGE, 0, (LPARAM)lan);

    Thanks

    1 Reply Last reply Reply Quote 0
    • L
      lubysj
      last edited by Mar 16, 2020, 7:45 PM

      In the end after playing with the code I have understood that it is not possible to write such plugin. My biggest bet was to change language at a plugin initialization stage but the command (I have used a different command to the one listed above) gets ignored or overwritten.

      It is a bit pity that I cant do it as I work with old UNIX style files where the files do not include extensions and the extension definition is within a file. That is making to look at different editor despite I like notepad++.

      Thanks

      A 1 Reply Last reply Mar 16, 2020, 8:21 PM Reply Quote 0
      • A
        Alan Kilborn @lubysj
        last edited by Alan Kilborn Mar 16, 2020, 8:21 PM Mar 16, 2020, 8:21 PM

        @lubysj

        It seems like you might be able to do this with one of the scripting plugins.

        You could (write and) install some logic that would fire upon opening a file. The logic could look at the first line and then select a different “language” based upon what is found in the first line’s contents…

        1 Reply Last reply Reply Quote 0
        • P
          PeterJones
          last edited by Mar 17, 2020, 2:04 AM

          I tried playing with it some in PythonScript and my Perl library: it appears there may be a bug in the SCI_SETLEXERLANGUAGE string-based message, because it isn’t affecting the language chosen. There’s the SCI_SETLEXER int-based message which does work languages, but that cannot set a specific UDL.

          You could navigate the Languages menu, and look for the dynamically-allocated menuCmdID for your UDL.

          1 Reply Last reply Reply Quote 0
          • E
            Ekopalypse
            last edited by Mar 17, 2020, 12:22 PM

            In general I would say using a notepad message ensures more or less
            that the UI gets updated as well where as scintilla messages flying under the radar of npp.

            @lubysj
            in your case I would say act on notepads bufferactivated notification,
            get the first line of the document and send either a menucommand,
            in case of udls or use NPPM_SETCURRENTLANGTYPE in case of builtin languages.

            Example how something might look like:

            from Npp import notepad, NOTIFICATION, editor, LANGTYPE
            
            def callback_BUFFERACTIVATED(args):
                x = editor.getLine(0)
                if x.startswith('// c++'):
                    notepad.setLangType(LANGTYPE.CPP)
                elif x.startswith('# npp3'):
                    notepad.runMenuCommand('Language', 'NPP3')
                else:
                    notepad.setLangType(LANGTYPE.PYTHON)
                    
            notepad.callback(callback_BUFFERACTIVATED, [NOTIFICATION.BUFFERACTIVATED])
            
            L 1 Reply Last reply Mar 21, 2020, 9:53 PM Reply Quote 4
            • L
              lubysj
              last edited by lubysj Mar 17, 2020, 9:45 PM Mar 17, 2020, 9:44 PM

              Thanks to all. Buffer activated message did the trick. I do not work with Python, though always wanted to learn. For those who want code in c is below. It is quite good that it allows to catch other notepad messages, so plugin can play more part rather than being passive and reacting to user menu commands.

              xtern "C" __declspec(dllexport) void beNotified(SCNotification *notifyCode)
              {
              	switch (notifyCode->nmhdr.code) 
              	{
              		
              		case NPPN_BUFFERACTIVATED:
              		{
              			::SendMessage(nppData._nppHandle, NPPM_MENUCOMMAND, 0, IDM_LANG_XML);
              		}
              		break;
              		default:
              			return;
              	}
              }
              
              1 Reply Last reply Reply Quote 2
              • L
                lubysj @Ekopalypse
                last edited by Mar 21, 2020, 9:53 PM

                Going back to UDLs… I was able to execute normal menu commands including for changing build in languages. However, I wasn’t able to change to a user defined language.

                @Ekopalypse are you able to open phyton library to check how does runcommand translates into c call?

                E 1 Reply Last reply Mar 22, 2020, 12:24 PM Reply Quote 0
                • E
                  Ekopalypse @lubysj
                  last edited by Mar 22, 2020, 12:24 PM

                  @lubysj

                  Pythonscript plugin is on github and here the part you are interested in.

                  1 Reply Last reply Reply Quote 1
                  • L
                    lubysj
                    last edited by Mar 23, 2020, 10:58 PM

                    @Ekopalypse , thanks. I will need to dig a bit to get a around.

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