How to change syntax via hotkey ?
-
@PeterJones said
Do
notepad.getLanguageName()
andnotepad.getLanguageDesc()
still have the bugs mentioned in your 2019 workaround code?v1.5.4 was released a couple weeks ago (mid-April), with the fix to the
getLanguageDesc()
bug. I forgot to reply then.Thanks, @bruderstein !
-
I don’t want to diminish the work of @bruderstein,
but I think @chcg did the fix, thanks for that. -
You are right. Sorry @chcg – no slight intended.
-
Hi, thanks for this easy solution. Unfortunately I need to set up hotkey for Markdown language, which is the only language that doesn’t seem accessible this way. Markdown is in a kind of secondary block in the Language menu, like so:
Is there still an ID for Markdown that is missing from the list? -
@lászló-bene said in How to change syntax via hotkey ?:
Is there still an ID for Markdown that is missing from the list?
No. Markdown is a UDL (User Defined Language), and they are treated differently than the built-in lexers, since users can manipulate UDLs on-the-fly; so they don’t get constants for
notepad.setLangType(LANGTYPE.xxxx)
.Thus, as one of @Ekopalypse’s posts above pointed out (after the phrase “and the language script”), it requires different syntax (using
notepad.runMenuCommand(...)
) to switch the Language to a UDL. So you could just use that script, with putting inMarkdown (preinstalled)
as the NAME_OF_UDL.However, Ekopalypse’s script shown above had to work around a limitation in older PythonScript versions. If you have at least PythonScript 1.5.4 or 2.0.0, then you can use a simplified script like I show below. (If you have one of the PythonScript 3.x alpha releases, the ones below should still work, I think; I don’t use PythonScript 3.x yet, and I’m not a Python3 expert, so I forget whether my
.format()
syntax still works in Python3, but I think it does.)So, here are some example scripts with PythonScript 1.5.4/2.0.0:
- If you want to toggle between HTML and the
Markdown (preinstalled)
UDL:
from Npp import notepad, LANGTYPE NAME_OF_UDL = 'Markdown (preinstalled)' # <<- needs to be specified to match the UDL you want to select language = notepad.getLangType() desc = notepad.getLanguageDesc(language) # this needs PythonScript 1.5.4 or newer if desc == 'User Defined language file - {}'.format(NAME_OF_UDL): notepad.setLangType(LANGTYPE.HTML) else: notepad.runMenuCommand('Language', NAME_OF_UDL)
- If you want to toggle between two UDL, like
Markdown (preinstalled)
andMarkdown (preinstalled dark mode)
:
from Npp import notepad, LANGTYPE NAME_OF_UDL = 'Markdown (preinstalled)' # <<- needs to be specified to match the UDL you want to select NAME_OF_UDL2 = 'Markdown (preinstalled dark mode)' # <<- needs to be specified to match the UDL you want to select language = notepad.getLangType() desc = notepad.getLanguageDesc(language) # this needs PythonScript 1.5.4 or newer if desc == 'User Defined language file - {}'.format(NAME_OF_UDL): notepad.runMenuCommand('Language', NAME_OF_UDL2) else: notepad.runMenuCommand('Language', NAME_OF_UDL)
- If you want to just always set the current language to the Markdown UDL, regardless of the current language, it’s even more simple:
from Npp import notepad NAME_OF_UDL = 'Markdown (preinstalled)' # <<- needs to be specified to match the UDL you want to select notepad.runMenuCommand('Language', NAME_OF_UDL)
Just save one or more of those scripts, and assign keyboard shortcut(s) as desired, and you can either toggle between two languages, or just use that shortcut for always setting to the same UDL.
Good luck.
- If you want to toggle between HTML and the
-
@peterjones, thank you so much for your help! Toggling between HTML and MarkDown via one hotkey is more than I wished for, and it works just fine.
-
@PeterJones Works like a charm, 2minutes and configured, thanks