Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.
I love PythonScript. It open so many doors. Thank you for the great work.
I have a question. I'm trying to style text within the console window. I'm trying to use the style functions, but I'm not familiar with Python and it's not working out for me.
Can somebody provide some examples on how to output text in different color and how to create clickable links.
Thank you in advance.
What you need is the console.editor property. This is only available from PythonScript 0.8, and there are a few things to remember when using it.
Firstly, the console window is set as readonly, so you need to set it as read/write before you output any text. Outputting text with console.write will automatically put the console back to read only (because it has to do exactly the same - ie. set read/write, output text, set readonly).
Styles you can obviously change as you like - if you want to use your own styles, then start from style 8 (probably ideally 32 or something, as the number of pre-defined styles may increase in future versions).
If you want to change the existing styles - see the manual for the style numbers - http://npppythonscript.sourceforge.net/docs/latest/console.html#Console.editor (obviously you don't need to set the read/write to set styles)
So, for example:
# define style 9 as blue - colour def is (red, green, blue)
# make the console read/write
# output the styled text.
# the 9 in brackets is a list of style numbers
# the style numbers are used from the list in order
# for each character. If there are more characters than
# style numbers, then the last style is repeated,
# hence just a  means that the 9 will be repeated
# for every character.
console.editor.addStyledText(Cell("Hello in blue\n", ))
As for hotspots - you can define them with console.editor.styleSetHotSpot, but you can't handle the "on click" event for the console (callbacks from the console are not supported), so it's a bit useless.
What are you trying to achieve with the hotspots - maybe there's something that can be added to the next version?
That's exactly what I was looking for. I couldn't figure out how to use addStyledText function.
I have bunch of perl scripts that do custom greps in source code in a unix environment. What I wanted to do is start the grep from notepad++ (which I already can) and then add hotspots to the files so that I can click on them and have notepad++ open that file on searched line.
You said hotspots are not supported, but how are they added when there's an error in a python script. Because it adds the text in red with hotspot to the file and line in error.
Hotspots are not "unsupported", just the callbacks - console.editor.callback() won't work (or may crash / kill your dog / etc :). When a hotspot is clicked in Scintilla, it sends a notification - at the moment these hotspot clicks from the console are handled internally by Python Script in order to open the file at the relevant line.
How about (for the next version) an event for console.callback(yourFunction, ), so if the user clicks on a style that the console doesn't know about, then your Python code can handle it? Would that help/suit?
Incidentally, you might want to look at the Analyse Plugin recently released - nowhere near as much fun, but might do exactly what you want :)
I think what you described about "console.callback" functions will be perfect.
Thanks a lot.
I just found out that if I just output a filename with a line number, it is made into a clickable link by default.
So if I do this:
console.write("C:\proj\myfile.txt:123: some text\n")
I will be able to click on it in the console window and it will open myfile.txt and set the caret to line 123.
This is what I needed.
Is there a way to also specify a range for the clickable text?
I've tried many combinations with different separators, but none seemed to work.
I'm looking for functionality similar to the FindInFiles built in NPP (you just click on the result and it highlights/selects the relevant section of the proper file).
Also... how can I retrieve the (base) filename of the Py-Script file being executed (__file__, __module__ and __name__ didn't work as I would expect to)?
In case you're wondering what I'm trying to achieve, here's the simple code I have so far:
SCRIPT_NAME = "RegExTest"
console.write("\nExecuting [" + SCRIPT_NAME + "] Python Script...\n")
pattern = notepad.prompt("Python-style Multiline RegEx:", "Python RegEx")
occurrences = 0
filename = notepad.getCurrentFilename()
for m in re.finditer(pattern, editor.getText(), re.M):
lineNum = editor.lineFromPosition(m.start()) + 1
startCol = editor.getColumn(m.start()) + 1
endCol = editor.getColumn(m.end()) + 1
console.write("%s:%d:%d-%d\n" % (filename, lineNum, startCol, endCol))
occurrences += 1
console.writeError("No matches found.\n") if occurrences == 0 else console.writeError("%d matches found.\n" % occurrences)
console.writeError("ERROR: No RegEx inserted.\n")