Community
    • Login

    Using a user-defined language as default?

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    12 Posts 6 Posters 11.7k 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.
    • Claudia FrankC
      Claudia Frank @Travis Farris
      last edited by

      Hello @Travis-Farris,

      as you already said, this cannot be achieved by npp natively.
      My solution to the porblem would involve the python script plugin
      and this little script.

      def callback_BUFFERACTIVATED(args):
          if 'new' in notepad.getBufferFilename(args['bufferID']):
              notepad.runMenuCommand('Language','HERE_THE_NAME_OF_THE_UDL')
      
      notepad.clearCallbacks([NOTIFICATION.BUFFERACTIVATED ])
      notepad.callback(callback_BUFFERACTIVATED, [NOTIFICATION.BUFFERACTIVATED ])
      

      Current version has some disadvantages like, every time you activate a new… document it calls
      the runMenuCommand to set the udl, even if it was already set and if you chose a different lexer but keep
      the name of the document new… it will reset it once you reactivate it.

      So it’s up to you to

      a) decide if you wanna go this way and if so
      b) how you would handle the mentioned disadvantages. E.g. we could modify it like
      providing some property which indicates that this file has been already set or something else.

      Cheers
      Claudia

      1 Reply Last reply Reply Quote 0
      • Rich GoldmanR
        Rich Goldman
        last edited by Rich Goldman

        Hi there,

        This is a feature I’ve been hoping for for a very long time. The python script looks like an ideal workaround. I’m a bit unskilled with Python, but, would this work if I knew a specific pattern for the file I was opening? For example if I knew the path to the file had the string ‘/bank/*/src/[0-9]*’ (almost a regex but not quite).

        Thanks in advance!
        Rich G.

        Claudia FrankC 1 Reply Last reply Reply Quote 0
        • Claudia FrankC
          Claudia Frank @Rich Goldman
          last edited by

          Hello @Rich-Goldman,

          in general, yes but of course not with the provided example.
          I would use fnmatch to make the pattern comparison, so something like

          from fnmatch import fnmatch
          
          def callback_BUFFERACTIVATED(args):
              _file = notepad.getBufferFilename(args['bufferID'])
              if fnmatch(_file, '*\Notepad++\plugins\Con*\Python*\*\*.py'):
                  current_bufferID = str(args['bufferID'])
                  if editor.getProperty(current_bufferID) != '1':
                      editor.setProperty(current_bufferID, '1')
                      notepad.runMenuCommand('Language','ttp')
          
          notepad.clearCallbacks([NOTIFICATION.BUFFERACTIVATED ])
          notepad.callback(callback_BUFFERACTIVATED, [NOTIFICATION.BUFFERACTIVATED ])
          

          Example makes no sense but is provided to see that it can be done.
          fnmatch works as dir within cmd shell.
          get and setProperty are used to set lexer only once.
          Of course you need to modify

          if fnmatch(_file, '*\Notepad++\plugins\Con*\Python*\*\*.py'):
          

          and

          notepad.runMenuCommand('Language','ttp')
          

          In addition you can also use regex to match certain files.

          Cheers
          Claudia

          1 Reply Last reply Reply Quote 0
          • Adam KleyA
            Adam Kley
            last edited by

            Thanks Claudia Seems great. However… for an idiot like me, how and where do i put this code in order to make it work.
            I really want to make this function happend but i need an idiotproof tutorial how to set it up :))

            Idiot questions:
            Where do i copy paste this code to?
            How do i make this code run as autostart?

            Ofcourse? i need to modify the directory to?
            “language” and ttp should be changed to?

            XD

            Thats my level. Thanks would be great to get this done.
            For myself and for my friend at work who prepares fixtures for a huge ass fiber cutter that cut through 8mm steel np and he also asked me if i could help him with loading his language as autostart default.

            1 Reply Last reply Reply Quote 0
            • Claudia FrankC
              Claudia Frank
              last edited by

              Hello Adam,

              first you need to have python script plugin installed.
              In case that you haven’t installed python script plugin yet, I would propose to use the MSI package instead of using the plugin manager.

              Once installed, goto
              Plugins->Python script->Configration
              and change Initialisation from LAZY to ATSTARTUP
              Close.

              Plugins->Python script->New Script

              copy and paste the code

              def callback_BUFFERACTIVATED(args):
                  if 'new' in notepad.getBufferFilename(args['bufferID']):
                      notepad.runMenuCommand('Language','HERE_THE_NAME_OF_THE_UDL')
              
              notepad.clearCallbacks([NOTIFICATION.BUFFERACTIVATED ])
              notepad.callback(callback_BUFFERACTIVATED, [NOTIFICATION.BUFFERACTIVATED ])
              

              save it as startup.py

              Within script replace HERE_THE_NAME_OF_THE_UDL with the name of your UDL.

              Save and restart npp.

              What does the code?
              Everytime a new tab gets opened the function callback_BUFFERACTIVATED gets called.
              It first checks if the tab has a new in its name, if so it selects the proper lexer.

              That’s it - I hope so.

              Cheers
              Claudia

              1 Reply Last reply Reply Quote 2
              • Rich GoldmanR
                Rich Goldman
                last edited by

                Hi There,
                I finally got around to implementing the python script solution. The good news is - it works! The bad news is - it doesn’t seem to work if I open a file from the command line. For example: I have a cygwin script that launches notepad++ and passes a file or list of files on the command line, it basically looks like:

                C:/Program Files (x86)/notepad++/notepad++.exe D:/Users/bank/file.map

                When NPP opens - the file is shown with plain text formatting. If I open a new file that matches my pattern, it works properly, and when I switch back to the first file it gets formatted at that moment. But it seems the initial launch of a file from the command line isn’t sending the BUFFERACTIVATED notification, or the user-defined startup.py runs after the very first BUFFERACTIVATED notification is pushed out.

                I found this on 6.9 and upgraded to 7.1 and I see the same behavior there as well.

                How should I work around this? Is it a bug, or something I’m doing out of sequence?

                Thanks,
                Rich

                Scott SumnerS 1 Reply Last reply Reply Quote 0
                • Scott SumnerS
                  Scott Sumner @Rich Goldman
                  last edited by

                  @Rich-Goldman

                  When Notepad++ starts up, it has to load up and activate the plugins (e.g. Pythonscript) as well as load the files you specify on the command-line. I’m no expert on it, but I strongly suspect that your command line file(s) get loaded a lot earlier than when all the Pythonscript stuff is in place. Thus, it can’t catch that something special should happen.

                  Rich GoldmanR 1 Reply Last reply Reply Quote 0
                  • Rich GoldmanR
                    Rich Goldman @Scott Sumner
                    last edited by

                    @Scott-Sumner Thanks - that’s exactly what I was wondering. Sounds like I’m on the right track. The question then becomes, can I somehow trigger the BUFFERACTIVATED signal with a python script so that the auto-UDL piece above will trigger correctly?

                    Scott SumnerS 1 Reply Last reply Reply Quote 0
                    • Scott SumnerS
                      Scott Sumner @Rich Goldman
                      last edited by

                      @Rich-Goldman

                      I’m not very familiar with the UDL task you’re trying to accomplish; however, it seems like after your “BUFFERACTIVATED” stuff gets set up in your startup.py, you could trigger it with some code like this, which activates all the files currently in the editor tabs one by one:

                      files_tups_list = notepad.getFiles()
                      curr_file = notepad.getCurrentFilename()
                      for tup in files_tups_list:
                          file = tup[0]
                          if file != curr_file:
                              notepad.activateFile(file)
                      notepad.activateFile(curr_file)
                      
                      1 Reply Last reply Reply Quote 0
                      • Jim DaileyJ
                        Jim Dailey
                        last edited by

                        @Rich-Goldman

                        If I understand the function @Claudia-Frank wrote, even if you can somehow get the BUFFERACTIVATED notification from a command line invocation, you would still have to have ‘new’ as part of the file’s name.

                        If that is correct and problematic, maybe you could use a simpler Python function that simply sets the language of the current file and is triggered by some hotkey? It wouldn’t be automatic, but it doesn’t seem too painful (to me).

                        BTW, you can’t possibly be the Rich Goldman I worked with at TI in the late 80s, right? If so, you must be using an old, old picture for your avatar, or you’ve found the fountain of youth! :)

                        Claudia FrankC 1 Reply Last reply Reply Quote 0
                        • Claudia FrankC
                          Claudia Frank @Jim Dailey
                          last edited by

                          @Jim-Dailey
                          sorry for late answer - well it depends, first version of the script
                          looks for new files as user was asking for any new file feature.
                          The modified version for Rich Goldman uses fnmatch as he was asking
                          for a way to look pattern specific.

                          @Rich-Goldman
                          beside the solution Scott mentioned you might think of
                          checking only the current file if it matches your pattern and run
                          the MenuCommand to enable your UDL. All other files get set
                          once switched to them - as you already stated.

                          So something like

                          if fnmatch(notepad.getCurrentFilename(), 'YOUR_PATTERN'):
                               ... do your stuff ...
                          

                          before or after your callback definition.

                          Cheers
                          Claudia

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