language select on notepad launch



  • 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



  • 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



  • @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…



  • 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.



  • 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])
    


  • 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;
    	}
    }
    


  • 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?



  • @lubysj

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



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


Log in to reply