Python Script plugin: script to tell the cursor to highlight the result and everything before it up to ">"
-
Hi,
Python Script plugin is standard Notepad++ plugin which allows the user to develop and run Python programs which can interact directly with their Notepad++ session.The editor.documentStart() script allows to Move the cursor to the start of the document.
what would be the script to tell the cursor to highlight the result and everything before it up to “>”
Exemple :
search for “585543”
highlight “585543” and everything before “585543” up to and including “>”1> toto|entry1|name entry 1
<5545530925043245043647831425433855454444254454443854954458554352443344530514554384755584535325242
2> toto|entry2|name entry 2
<5052111312475888808008337300450218742703373135855433133731130394883472454243543738810345331428373511418323890414683233322831303141448503314323173333332301183187278483374123574635428388480388131343333333328343545131430202150411403646343534333338333833523308138353313131145313331434117340114110404331621361303318123203538825
3>toto|code3|… -
@jean-luc-borla said in Python Script plugin: script to tell the cursor to highlight the result and everything before it up to ">":
what would be the script to tell the cursor to highlight the result and everything before it up to “>”
I’m not sure what you mean by that. “Everything before
585543
” seems to contractict “up to>
”. Unless you mean you want to select from the>
on one line to the585543
that comes next, something like this:
(I used the Mark > Mark All to set the color on the585543
, and then manually selected the text from the>
to the585543
to show what I think you want. That wouldn’t necessarily be the sequence used in the PythonScript equivalent, depending on your definition of “highlight”, as I request clarification below.)But I could also interpret that as if you didn’t mean “before” but actually meant “after”, in which case it would be highlighting more like:
If one of my screenshots matches the text you are expecting, please let us know which. If neither of my screenshots matches the text you are expecting, please do the manual selection for us, so that we can see which text you actually want highlighted.
Also, when you say “highlight”, do you mean “select the text”? Or do you mean “mark the text with the Search > Mark color”? Or do you mean “mark the text using **Search > Style One Token > Using #th Style”? Or do you mean, “apply some arbitrary text-styling color, like a language lexer does”? Because all three of those are ways to “highlight”, but they would each require different syntax in PythonScript.
But from what I do understand:
editor.documentStart()
is not a necessary part of the answer to this script; you don’t need to move the caret in order to highlight something (you could, but you don’t need to)editor.setSel(start,end)
is the easiest way to set the start and end locations for the active selection (assuming “highlight” means “select the text”)editor.setStyling(...)
could be part of a lexer-like solution. But Notepad++ changes styling based on the active lexer every time it refreshes the view, so you’d probably find it pretty annoyingeditor.setIndicatorCurrent(...); editor.setIndicatorValue(...); indicatorFillRange(...)
could be used to set the “indicator” rather than the “style”, which would have a similar result, from your perspective, but Notepad++ doesn’t mess with indicators as much as it does with styles, so there would be less conflict.editor.setSel(start,end); notepad.menuCommand(MENUCOMMAN.SEARCH_MARKALLEXT2)
would be the same as selecting the text then using Search > Style All Occurrences of Token > Using 2nd Style. Unfortunately, PythonScript 2.0.0.0 does not have the constants for Style One Token instead of Style All Occurrences of Token. Fortunately, you can figure out those constants from menuCmdId.h: Mark One Token using styles 1-5 would be values 43062 - 43066, sonotepad.menuCommand(43062)
would use the 1st Style for doing the marking of the active selection as the “Token”.editor.research()
would be an easy way to find the text you want, and then run a function to actually do the marking.
-
@jean-luc-borla said in Python Script plugin: script to tell the cursor to highlight the result and everything before it up to ">":
highlight “585543” and everything before “585543” up to and including “>”
-
Oh, right,
editor.findText
probably has an easier interface thaneditor.research()
… I tend to forget that there’s the non-callback version. -
@PeterJones said
"Unless you mean you want to select from the > on one line to the 585543 that comes next, something like this:
Yes it’s that
culd you provide the complete code to do that ? -
Thanks,
I have tried but there is an error
File “<console>”, line 1
z = editor.findText(FINDOPTION.REGEXP, 0, editor.getLength(), r’ (?s)>.*585543’) if Z is not None: editor.setSel(*z)
^
SyntaxError: invalid syntax -
I tried but there is an error:
File “<console>”, line 1
z = editor.findText(FINDOPTION.REGEXP, 0, editor.getLength(), r’ (?s)>.*585543’) if Z is not None: editor.setSel(*z)
^
SyntaxError: invalid syntax -
@jean-luc-borla said,
SyntaxError: invalid syntax
You put both commands on the same line. Python (and thus the PythonScript plugin) doesn’t work that way.
z = editor.findText(FINDOPTION.REGEXP, 0, editor.getLength(), r'(?s)>.*585543') if z is not None: editor.setSel(*z)
This works. Well, mostly. The regex is too greedy.
Change it to be less greedy:
z = editor.findText(FINDOPTION.REGEXP, 0, editor.getLength(), r'(?s)>.*?585543') if z is not None: editor.setSel(*z)
-
My response was going to be: “We can’t teach basic Python here”