Change language to specific user defined language
-
I’ve been attempting to change the language to a specific user defined language for certain txt files by name. Currently all I can find is nppm_setcurrentlangtype which changes to a user defined language but not the correct language. It seems all user defined are type 15 L_USER. There is nppm_getnbuserlang however I don’t see what use this information gives me.
-
@Dustin-riddick said in Change language to specific user defined language:
I’ve been attempting to change the language to a specific user defined language for certain txt files by name.
How - are you developing a plugin, or are you using an existing scripting plugin (e.g., NppExec, PythonScript)? Both of the latter scripting plugins offer a “run Notepad menu command” command - which you could use to activate your UDL by calling it by name under the “Language” menu.
Example NppExec (I have an ‘NppExec’ UDL):
NPP_MENUCOMMAND Language\NppExec
Cheers.
-
@Michael-Vincent I’m developing in c++. I just downloaded the template and haven’t looked into any additional scripting engines. I hadn’t seen nppm_memucommand so I will attempt to use that
-
That won’t work - it is not a Notepad++ API; rather, made available in the existing scripting plugins. Hence why I asked. I think you’ll need a more “convoluted” method to do it via the Notepad++ API.
Note you can rather easily get the language type (again, NppExec example using standard N++ API calls):
NPP_SENDMSG NPPM_GETCURRENTLANGTYPE 0 @0 SET LANGTYPE = $(MSG_LPARAM) NPP_SENDMSG NPPM_GETLANGUAGENAME $(LANGTYPE) @"" SET LANGNAME = $(MSG_LPARAM) NPP_SENDMSG NPPM_GETLANGUAGEDESC $(LANGTYPE) @"" SET LANGDESC = $(MSG_LPARAM)
Which for my NppExec UDL, produces:
================ READY ================ \lang type = 15 name = udf - NppExec desc = User Defined language file - NppExec ================ READY ================
But I don’t see how to “set” the language once you know the UDL sub-type.
Cheers.
-
@Michael-Vincent said in Change language to specific user defined language:
it is not a Notepad++ API
NPP_MENUCOMMAND may be an NppExec nomenclature, but the API has a corresponding message that works with IDM values, not menu name.
NPPM_MENUCOMMAND, where the wParam is 0 and lParam is the IDM constant. I explained in this recent post how to find the menuCmdID in the source for builtin languages, or using NppUISpy for UDL (the macro I was showing there is using the NPPM_MENUCOMMAND message)
-
Hmm I was able to set the desired language on my computer with this method. The issue I have now is I have to iterate through the user languages set to a language and then check the name of the language to see if it’s the one I want. I can’t seem to get a specific menu option by name
-
I haven’t had occasion to try this, but I believe that to get the correct menu id you’d need to scan the Languages menu. (There’s no reason to think it would be the same for different users; maybe not even for the same user over time.) Use NPPM_GETMENUHANDLE with wParam = 1 to get the main menu handle. From there it should be possible to find the menu entry you need with GetMenuItemInfo. The wID member of the MENUITEMINFO structure should give you the command ID to send.
-
@Coises said in Change language to specific user defined language:
There’s no reason to think it would be the same for different users; maybe not even for the same user over time.
In regards to the UDL section of the Language menu, correct. (The builtin languages will of course have the same IDM across any installation.)
My original suggestion was more in the thought of showing how to find the right menuCmdId for a UDL in a single-user environment; I wasn’t thinking in terms of a generic plugin (though I should have been – sorry). So yes, as @Coises has said, you will have to scan through the entries in the Language menu to find the one that has the right name, and use the win32 data structure he mentioned in order to find the associated menuCmdId that goes with it.
-
@Dustin-riddick said in Change language to specific user defined language:
There is nppm_getnbuserlang however I don’t see what use this information gives me.
I think it does give you something helpful: the starting menu id for user-defined languages, and the number of user-defined languages. (I haven’t checked, but “starting number” implies they are consecutive.)
As described here, you can access menu items by id and Windows will traverse the menu structure. So once you know the first id and the number of ids (from NPPM_GETNBUSERLANG) and the handle of the main menu (from NPPM_GETMENUHANDLE), you can use GetMenuItemInfo to find the string associated with each until you either find the one you need or run out of entries.