Adding the Extension of PRG in the Tab Settings list
-
@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. :)
-
I see. It gives YOU grief if YOU can’t keep the indentation consistent. :-)
-
@alan-kilborn
I think I said that already. :) -
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.
-
@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-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:
And this is the .prg file showing that the changes didn’t take place:
-
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.
-
@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. :-(
-
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).
-
@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. -
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 aexample.prg
file, it properly used 3 spaces when I typed a tab in theexample.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 anothersetting 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.
-
@peterjones said in Adding the Extension of PRG in the Tab Settings list:
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.
Oops, I guess I didn’t mean to leave the print statement in there… But perhaps it was helpful here.
So the reason it “sets it up” everytime you switch tabs is that upon the tab change, Notepad++ itself will reset the tab settings…so the script code has to override that and set it up the way it wants it. (Just some background info).
BTW, the script isn’t “perfect”. If you rename a file that has a non .prg extension so that it has that extension, it will probably take a tab switch away and a return to get it to take effect. Also, once tab characters are “in” a file, this script won’t remove them, even if the configuration is set to use-spaces. These limitations could be worked around, with more code.
-
Yes, I get it, it’s not an indentation bug, I must have done something wrong. :-)
So I’ll have to mess with it to see what I did wrong. I will try creating a user startup.py and put it where ever it goes (reading the instructions on file locations I have here somewhere), and remove it from the machine startup.py. This is kind of the thing, I know I’ve screwed it up somehow and I don’t understand the indentation rules for Python…I’m am only learning and dabbling at the moment, and indentation was never a critical part of any programming I’ve ever done, so it’s new to me. I’ll get it. Thanks for the pointers and the tests.
-
-