Community
    • Login

    Adding the Extension of PRG in the Tab Settings list

    Scheduled Pinned Locked Moved General Discussion
    24 Posts 4 Posters 3.9k 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.
    • Lycan ThropeL
      Lycan Thrope @FrankAndrew57
      last edited by Lycan Thrope

      @frankandrew57 said in Adding the Extension of PRG in the Tab Settings list:

      This file type PRG is used in Visual FoxPro/DBase as their program code files.
      I need to set the tab stops to the sire of three (3) and use spaces instead of the tab character but I see no place where I can add new extension types to the Tab Settings list.

      Aside from what Peter has said, there is a way you can change the default, so it affects every file you create from this point on, by changing the default and you do that by going to the Settings->Preferences->Languages where you’ll see this:

      Tab1.PNG

      You’ll notice I’ve already changed mine. To change yours click the checkbox for Replace by space, then click on the number next to Tab size: and you’ll see this screen:

      Tab2.PNG

      Change the tab size to the size you want and to apply it, you have to hit Enter. From then on all your tabs will be 3 spaces, but only for new documents. Any old ones will still have the old default of 4 and will be a tab character. I use dBASE Plus, so I’m a little familiar with your issues. I don’t think our languages are as similar now as they use to be, because we have a lot of differences in OOP implementation/syntax, though we have seen a number of VFP users/developers looking to move over to dBASE because of VFP development ending.

      Should you also look to change, I have developed a dBASE Plus UDL that some of our users wanted to see implemented in Notepad++, of which Peter and many here were very much instrumental in helping me get it done. I have a completed FunctionList, Autocomplete file and UDL for the language should you want to become a user of dBASE Plus, and we have a fairly active and helpful newsgroup at news.dbase.com should you want to join or pick up our UDL files.

      Lycan ThropeL 1 Reply Last reply Reply Quote 0
      • Lycan ThropeL
        Lycan Thrope @Lycan Thrope
        last edited by Lycan Thrope

        @lycan-thrope
        Sorry, that link doesn’t work the way I had hoped it would, and the forums won’t let me edit it, so…just use your newsgroup software using the news.dbase.com link to check out the newsgroup. Clicking this link will take you to the Web News group, which is clumsier than using a newsgroup reader program.

        Alan KilbornA 1 Reply Last reply Reply Quote 0
        • Alan KilbornA
          Alan Kilborn @Lycan Thrope
          last edited by

          @lycan-thrope

          Your method for changing the tab settings isn’t exactly what the OP is looking for. OP is looking for specifying a tab setting for a single type of file; your solution is changing the setting for all file types unknown to Notepad++'s tab settings box.

          Lycan ThropeL 1 Reply Last reply Reply Quote 0
          • Lycan ThropeL
            Lycan Thrope @Alan Kilborn
            last edited by

            @alan-kilborn ,
            Yes, I know, but short of him doing as Peter says, creating his own lexer/plugin, this will accomplish the same thing for him. If he uses NPP to program in Visual Fox Pro, as it seems he’s suggesting by his request, that he wants to do, then this will accomplish for him. Not many people program outside their normal development environment, where they have their defaults already set, but when they do, they tend to use the same kind of formatting through all the languages, with the exception of maybe Python, that requires specific formatting to function.

            I know in my C programs as well as my dBASE and other programming code files I use a 3 space indentation as I don’t like excessively spaced code when 1 less space per indentation will more likely keep the structure within an 80 character per line limit for printing on letter size if need be and I don’t have to scroll all across the editor to see the code on long runs, so I try to keep everything within that 80 character space and the smaller indents help. I actually had it down to 2 spaces, but then it got harder to tell the structure so I went back up to 3. :)

            PeterJonesP 1 Reply Last reply Reply Quote 0
            • PeterJonesP
              PeterJones @Lycan Thrope
              last edited by

              @FrankAndrew57 ,

              Instead of creating your own lexer/plugin (which I don’t think I actually suggested in this discussion, though I have suggested it at other times), the PythonScript plugin allows you to use editor.setTabWidth(tabWidth) to set the tab width… so you could do that in a callback which is activated every time the file is opened or gets focus (there are other examples in the forum, search for BUFFERACTIVATED , and you will probably find an example) – it would check the extension, and if found to be .PRG file, change the tab width.

              1 Reply Last reply Reply Quote 1
              • Lycan ThropeL
                Lycan Thrope @PeterJones
                last edited by

                @peterjones said in Adding the Extension of PRG in the Tab Settings list:

                The UDL system doesn’t have a way to define the tab/space settings, though that would be really nice.

                This is the part I thought you were hinting at it needing to be a lexer/plugin from, my mistake…and thanks for the Python reference, too. I’m still not as well versed in that as I’d like either so don’t often think of it. :( Thanks for the additional help. I still like setting the default though, since as I mentioned, that seems to be the way people do it, if they have a preference anyway. Python seems to be the only one that gives me grief about indentation, but…that’s the way IT works, so I don’t get a say in it. :)

                Alan KilbornA 1 Reply Last reply Reply Quote 0
                • Alan KilbornA
                  Alan Kilborn @Lycan Thrope
                  last edited by

                  @lycan-thrope said in Adding the Extension of PRG in the Tab Settings list:

                  Python seems to be the only one that gives me grief about indentation, but…that’s the way IT works, so I don’t get a say in it

                  How exactly does Python present a problem in this area?

                  Lycan ThropeL 1 Reply Last reply Reply Quote 0
                  • Alan KilbornA
                    Alan Kilborn
                    last edited by

                    As Peter mentions that scripting can be used to achieve the desired behavior, I’ll cite this semi-related script that could be used as a basis: https://community.notepad-plus-plus.org/post/54777

                    1 Reply Last reply Reply Quote 2
                    • PeterJonesP
                      PeterJones @FrankAndrew57
                      last edited by

                      @frankandrew57 ,

                      BTW: I was looking at something else, and found issue#4458 requested the ability to set tab settings for UDL back in 2018, but nothing has been done about it.

                      Using the workarounds presented here are your best choice for the short-to-medium term.

                      1 Reply Last reply Reply Quote 1
                      • Lycan ThropeL
                        Lycan Thrope @Alan Kilborn
                        last edited by

                        @alan-kilborn said in Adding the Extension of PRG in the Tab Settings list:

                        @lycan-thrope said in Adding the Extension of PRG in the Tab Settings list:

                        Python seems to be the only one that gives me grief about indentation, but…that’s the way IT works, so I don’t get a say in it

                        How exactly does Python present a problem in this area?

                        …
                        In Python, all the code that you type is arranged via correct whitespaces and therefore if at any instance you have a bad indentation, the overall code will not run and the interpreter will simply return an error function.
                        …
                        via: https://www.edureka.co/blog/indentation-error-in-python/

                        Not sure if PythonScript suffers this same issue, but I’ve had at least one interpreter that was adamant since I’ve been trying to figure out Python in the different tutorials. :)

                        Alan KilbornA 1 Reply Last reply Reply Quote 0
                        • Alan KilbornA
                          Alan Kilborn @Lycan Thrope
                          last edited by

                          @lycan-thrope

                          I see. It gives YOU grief if YOU can’t keep the indentation consistent. :-)

                          Lycan ThropeL 1 Reply Last reply Reply Quote 0
                          • Lycan ThropeL
                            Lycan Thrope @Alan Kilborn
                            last edited by

                            @alan-kilborn
                            I think I said that already. :)

                            1 Reply Last reply Reply Quote 0
                            • Alan KilbornA
                              Alan Kilborn
                              last edited by

                              FWIW, here’s a more direct script that handles the desired situation. It uses a dictionary for configuration; for more extension overrides, simply add a line into the dictionary. I call this script TabSettingsOverrideByExtension.py:

                              # -*- coding: utf-8 -*-
                              from __future__ import print_function
                              
                              from Npp import *
                              import os
                              
                              #-------------------------------------------------------------------------------
                              
                              class TSOBE(object):
                              
                                  def __init__(self):
                                      self.config_dict = {
                                          'prg' : { 'use_tab_chars' : False, 'spaces_per_tab' : 3 },
                                          'no_ext' : { 'use_tab_chars' : True, 'spaces_per_tab' : 8 },
                                      }
                                      notepad.callback(self.bufferactivated_callback, [NOTIFICATION.BUFFERACTIVATED])
                                      self.bufferactivated_callback(None)  # fake notification so that the current file gets set up when first run
                              
                                  def bufferactivated_callback(self, args):
                                      complete_pathname = notepad.getCurrentFilename()
                                      filename_with_ext = complete_pathname.rsplit(os.sep, 1)[-1]
                                      if '.' in filename_with_ext:
                                          ext = filename_with_ext.rsplit('.', 1)[-1].lower()
                                          if ext in self.config_dict:
                                              print('setting up for files with extension', ext)
                                              editor.setUseTabs(self.config_dict[ext]['use_tab_chars'])
                                              editor.setTabWidth(self.config_dict[ext]['spaces_per_tab'])
                                      elif notepad.getCurrentLang() == LANGTYPE.TXT:
                                          print('setting up for extensionless files')
                                          editor.setUseTabs(self.config_dict['no_ext']['use_tab_chars'])
                                          editor.setTabWidth(self.config_dict['no_ext']['spaces_per_tab'])
                              
                              #-------------------------------------------------------------------------------
                              
                              # to run via another file, e.g., startup.py:
                              #  import TabSettingsOverrideByExtension
                              #  tsobe = TabSettingsOverrideByExtension.TSOBE()
                              
                              if __name__ == '__main__':
                                  try:
                                      tsobe
                                  except NameError:
                                      tsobe = TSOBE()
                              

                              As indicated in the comments in the code, ideally this script would be run from user startup.py.

                              Lycan ThropeL Alan KilbornA 2 Replies Last reply Reply Quote 2
                              • Lycan ThropeL
                                Lycan Thrope @Alan Kilborn
                                last edited by

                                @alan-kilborn ,
                                For what it’s worth, I couldn’t get this to work, copy pasting the stuff into a new file, stored in the same folder as startup.py and copying and uncommenting the code you pointed had commented importing and the declaration, and the test code keeps erroring our right after the declaration. It doesn’t seem to get down to the main test.

                                Lycan ThropeL 1 Reply Last reply Reply Quote 0
                                • Lycan ThropeL
                                  Lycan Thrope @Lycan Thrope
                                  last edited by

                                  @lycan-thrope
                                  For clarification, here’s the files, in case I screwed something up (like that ever happens) :-)

                                  # The lines up to and including sys.stderr should always come first
                                  # Then any errors that occur later get reported to the console
                                  # If you'd prefer to report errors to a file, you can do that instead here.
                                  import sys
                                  from Npp import *
                                  
                                  console.write("START of machine startup.py\n\n")
                                  
                                  # Set the stderr to the normal console as early as possible, in case of early errors
                                  sys.stderr = console
                                  
                                  # Define a class for writing to the console in red
                                  class ConsoleError:
                                  	def __init__(self):
                                  		global console
                                  		self._console = console;
                                  
                                  	def write(self, text):
                                  		self._console.writeError(text);
                                  
                                  # Set the stderr to write errors in red
                                  sys.stderr = ConsoleError()
                                  
                                  # This imports the "normal" functions, including "help"
                                  import site
                                  
                                  sys.stdout = console
                                  
                                  # In order to set the stdout to the current active document, uncomment the following line
                                  # sys.stdout = editor
                                  # So print "hello world", will insert "hello world" at the current cursor position
                                  
                                  ########## added:
                                  # for e in (editor1,editor2):
                                      # e.callTipSetBack((204, 255, 102))
                                      # e.callTipSetFore((0,0,0))
                                      # e.callTipSetForeHlt((0, 102, 255))
                                  
                                  # to run via another file, e.g., startup.py:
                                  #  import TabSettingsOverrideByExtension
                                  #  tsobe = TabSettingsOverrideByExtension.TSOBE()
                                  
                                  import TabSettingsOverrideByExtension
                                  
                                  tsobe = TabSettingsOverrideByExtension.TSOBE()
                                  
                                  if __name__ == '__main__':
                                      try:
                                          tsobe
                                      except NameError:
                                  
                                  console.write("END of machine startup.py\n\n")
                                  console.hide()
                                  

                                  And your file I copied and created the file name that is referenced:

                                  # -*- coding: utf-8 -*-
                                  from __future__ import print_function
                                  
                                  from Npp import *
                                  import os
                                  
                                  #-------------------------------------------------------------------------------
                                  
                                  class TSOBE(object):
                                  
                                      def __init__(self):
                                          self.config_dict = {
                                              'prg' : { 'use_tab_chars' : False, 'spaces_per_tab' : 3 },
                                              'no_ext' : { 'use_tab_chars' : True, 'spaces_per_tab' : 8 },
                                          }
                                          notepad.callback(self.bufferactivated_callback, [NOTIFICATION.BUFFERACTIVATED])
                                          self.bufferactivated_callback(None)  # fake notification so that the current file gets set up when first run
                                  
                                      def bufferactivated_callback(self, args):
                                          complete_pathname = notepad.getCurrentFilename()
                                          filename_with_ext = complete_pathname.rsplit(os.sep, 1)[-1]
                                          if '.' in filename_with_ext:
                                              ext = filename_with_ext.rsplit('.', 1)[-1].lower()
                                              if ext in self.config_dict:
                                                  print('setting up for files with extension', ext)
                                                  editor.setUseTabs(self.config_dict[ext]['use_tab_chars'])
                                                  editor.setTabWidth(self.config_dict[ext]['spaces_per_tab'])
                                          elif notepad.getCurrentLang() == LANGTYPE.TXT:
                                              print('setting up for extensionless files')
                                              editor.setUseTabs(self.config_dict['no_ext']['use_tab_chars'])
                                              editor.setTabWidth(self.config_dict['no_ext']['spaces_per_tab'])
                                  
                                  #-------------------------------------------------------------------------------
                                  
                                  # to run via another file, e.g., startup.py:
                                  #  import TabSettingsOverrideByExtension
                                  #  tsobe = TabSettingsOverrideByExtension.TSOBE()
                                  
                                  # if __name__ == '__main__':
                                      # try:
                                          # tsobe
                                      # except NameError:
                                  

                                  This pic seems to be where the cursor stops in the TSOBE file:

                                  Consolestopintsobefile.PNG

                                  And this is the .prg file showing that the changes didn’t take place:

                                  prgfilenotshowingsettings.PNG

                                  Alan KilbornA 1 Reply Last reply Reply Quote 0
                                  • Alan KilbornA
                                    Alan Kilborn @Lycan Thrope
                                    last edited by

                                    @lycan-thrope

                                    Where it says:

                                    to run via another file, e.g., startup.py:

                                    You need to put the two lines below that, in uncommented form, into startup.py. That’s what “via another file” means. Perhaps this could’ve been made clearer somehow.

                                    Lycan ThropeL 1 Reply Last reply Reply Quote 0
                                    • Lycan ThropeL
                                      Lycan Thrope @Alan Kilborn
                                      last edited by

                                      @alan-kilborn ,
                                      That first black box is startup.py code, and the next one down was TSOBE code file.

                                      I did find out that when I changed them around I was bit again by that indentation bug, even in PythonScript, obviously it does it, too. For that error, I just eliminated the spaces and the comment symbol from those two lines…but it still doesn’t work as you suggested…even after I didn’t get any errors…which my startup script no longer stops the console from appearing even though the script closes it.

                                      So until I can not make stupid errors due to possible indentation errors in PythonScript, I’ll just set the default. It’s easier to implement, apparently. Here’s the error I got from the indentation issue.

                                      Traceback (most recent call last):
                                        File "C:\Program Files\Notepad++\plugins\PythonScript\scripts\startup.py", line 43, in <module>
                                          import TabSettingsOverrideByExtension
                                        File "C:\Program Files\Notepad++\plugins\PythonScript\scripts\TabSettingsOverrideByExtension.py", line 43
                                          
                                                               ^
                                      IndentationError: expected an indented block
                                      
                                      Python 2.7.18 (v2.7.18:8d21aa21f2, Apr 20 2020, 13:25:05) [MSC v.1500 64 bit (AMD64)]
                                      Initialisation took 421ms
                                      Ready.
                                      

                                      Thanks for the attempt, but I think I’ll wait until I’m more versed in the intricacies of Python before I venture into using it again, or commenting on it’s use. :-(

                                      Alan KilbornA 1 Reply Last reply Reply Quote 0
                                      • Alan KilbornA
                                        Alan Kilborn @Alan Kilborn
                                        last edited by

                                        So, one thing I said originally was:

                                        ideally this script would be run from user startup.py

                                        The operative word there being user.
                                        You edited the machine startup script – not advisable, not really proper – but in and of itself, this isn’t the source of your problems, I don’t think.

                                        But, forget all that “startup” stuff, for just taking it for a test drive…

                                        What I just tried:

                                        • Started up a clean N++ sandbox with PythonScript plugin
                                        • Created a new script giving it the name TabSettingsOverrideByExtension.py
                                        • Copied my “black box” code from above into the file
                                        • Saved the file
                                        • Ran the script from the menus

                                        It runs (quietly) with no output (thus no indentation problems).

                                        If I open a .prg file, it enforces my 3-space tab rule that’s defined in the code.

                                        Thus, I’m not certain where you went wrong. Maybe: It says something about line 43 but the file you show only has 42 lines; not sure what to think about that.

                                        Running the script not-from-startup is fine, it certainly will work that way. But note from the last 5 lines of the file the logic there – you only get to run it one time. Thus if you want to make changes to the script (such as modifying the configuration of additional file types), those won’t be picked up…until you restart Notepad++ and re-run. We don’t want the script to run more than once per Notepad++ session, because it installs a notification handler, and if the script were run multiple times, it would install multiple handlers (not good).

                                        PeterJonesP 1 Reply Last reply Reply Quote 2
                                        • Alan KilbornA
                                          Alan Kilborn @Lycan Thrope
                                          last edited by

                                          @lycan-thrope said in Adding the Extension of PRG in the Tab Settings list:

                                          I was bit again by that indentation bug, even in PythonScript, obviously it does it, too.

                                          I’m a bit disturbed by this.
                                          There is no indentation bug, just indentation rules to follow.
                                          And PythonScript is just a plugin version of an embedded Python interpreter, so any files created for use with it must also follow Python’s indentation rules.
                                          I wish I could help further, but I really don’t know how to.

                                          1 Reply Last reply Reply Quote 1
                                          • PeterJonesP
                                            PeterJones @Alan Kilborn
                                            last edited by

                                            @Lycan-Thrope ,

                                            There is no indentation problem with what @Alan-Kilborn originall posted. I just created a fresh Notepad++ with PythonScript, copy/pasted exactly what he had into TabSettingsOverrideByExtension.py, and ran that. When I opened a example.prg file, it properly used 3 spaces when I typed a tab in the example.prg file.

                                            My PythonScript console looks like:

                                            Python 2.7.18 (v2.7.18:8d21aa21f2, Apr 20 2020, 13:25:05) [MSC v.1500 64 bit (AMD64)]
                                            Initialisation took 15ms
                                            Ready.
                                            setting up for files with extension prg
                                            

                                            if I switch to a different tab, then back to example.prg, I see another setting up for files with extension prg in the console.

                                            Similarly, if I exit out and come back in, then open up example.prg without running the script first, it properly uses the default TAB character with a width of 4 characters.

                                            If I create a user startup.py with the exact contents:

                                            # -*- coding: utf-8 -*-
                                            from Npp import *
                                            import os
                                            
                                            import TabSettingsOverrideByExtension
                                            tsobe = TabSettingsOverrideByExtension.TSOBE()
                                            

                                            when I restart Notepad++, I get no errors in the PythonScript console, and if I open example.prg, it does 3-space when I hit the TAB key.

                                            So Alan’s example script works exactly as described. You do not need to edit anything after you paste in the exact contents from the black box and save. It just works right, with correct indentation.

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