Community
    • Login

    How to change syntax via hotkey ?

    Scheduled Pinned Locked Moved General Discussion
    27 Posts 8 Posters 5.4k 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.
    • PeterJonesP
      PeterJones @PeterJones
      last edited by

      @PeterJones said

      Do notepad.getLanguageName() and notepad.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 !

      EkopalypseE 1 Reply Last reply Reply Quote 0
      • EkopalypseE
        Ekopalypse @PeterJones
        last edited by

        @PeterJones

        I don’t want to diminish the work of @bruderstein,
        but I think @chcg did the fix, thanks for that.

        1 Reply Last reply Reply Quote 1
        • PeterJonesP
          PeterJones
          last edited by

          @Ekopalypse ,

          You are right. Sorry @chcg – no slight intended.

          1 Reply Last reply Reply Quote 1
          • László BeneL
            László Bene @PeterJones
            last edited by

            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:
            Markdown from menu
            Is there still an ID for Markdown that is missing from the list?

            PeterJonesP 1 Reply Last reply Reply Quote 0
            • PeterJonesP
              PeterJones @László Bene
              last edited by PeterJones

              @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 in Markdown (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) and Markdown (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.

              László BeneL 1 Reply Last reply Reply Quote 5
              • László BeneL
                László Bene @PeterJones
                last edited by

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

                1 Reply Last reply Reply Quote 1
                • Szymon SzymonS
                  Szymon Szymon @PeterJones
                  last edited by

                  @PeterJones Works like a charm, 2minutes and configured, thanks

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