I set myself a challenge. Teach myself Python and apply that knowledge to NPP.
I was somewhat successful but I'm having trouble with the .replace function
Here's some code with some debug messages in it.
The goal is to read an integer, subtract 1 and replace the next three occurrences of the integer with a value one less.
So far my code just replaces ALL occurrences not just the first three. Am I missing something in the implementation or is there a way to achieve what I'm hoping to achieve?
Hi Andrew,
because you wrote that you start learning python I thought I may give you some
friendly hints/ideas about using it together with pythonscript and npp.
The code you posted won't run because variable pos isn't initilized.
I assume it should be stpos and ndpos instead.
Maybe you wanna use String.Format function instead of old-style %s % text...
it is much more flexible.
getSelectionEnd is not needed and instead of adding 1250 to ndpos you could use
editor.getLength() to get the end of the document.
An alternative to notepad.messageBox could be console.write().
Of course makes only sense if you call console.show prior to write or you enable Show
Console via the menu
Happy coding
Claudia
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thanks Claudia. Yep found the pos issue although it only affected the notepad lines not that actual functionality I was after.
Below what I ended up refining the code to.
The issue was I'm replacing a selected number with and incremented or decremented value. The issue is there are lots of numbers in the block of text hence the nominal "1250" window.
I was able to get my replace quite precise except for the last one.
You'll note the last replace in this version of code actual has a start and end block to make sure the replace didn't replace the wrong value.
I do appreciate your pointers though. Thank you.
import re
text = editor.getSelText()
stpos = editor.getSelectionStart()
notepad.messageBox("Startpos: %i" % (stpos))
I set myself a challenge. Teach myself Python and apply that knowledge to NPP.
I was somewhat successful but I'm having trouble with the .replace function
Here's some code with some debug messages in it.
The goal is to read an integer, subtract 1 and replace the next three occurrences of the integer with a value one less.
So far my code just replaces ALL occurrences not just the first three. Am I missing something in the implementation or is there a way to achieve what I'm hoping to achieve?
text = editor.getSelText()
notepad.messageBox("Selected text: %s" % (text))
inum = int(text)
notepad.messageBox("Selected inum: %i" % (inum))
newnum = inum - 1
notepad.messageBox("Selected newnum: %i" % (newnum))
editor.replace(text, newnum,3)
Hi,
if you want to provide the maxCount you need to provide flags, startPosition and
endPosition parameters as well.
Either one of the following replace function is available
replace(old,new, flags, startPosition, endPosition, maxCount)
replace(old,new, flags, startPosition, endPosition)
replace(old,new, flags, startPosition)
replace(old,new, flags)
replace(old,new)
Cheers
Claudia
Thanks Claudia.
Here is my revised code. It works well enough.
import re
text = editor.getSelText()
stpos = editor.getSelectionStart()
notepad.messageBox("Startpos: %i" % (pos))
ndpos = editor.getSelectionEnd()
notepad.messageBox("Endpos: %i" % (pos))
notepad.messageBox("Selected text: %s" % (text))
inum = int(text)
notepad.messageBox("Selected inum: %i" % (inum))
newnum = inum - 1
notepad.messageBox("Selected newnum: %i" % (newnum))
editor.replace(text, newnum,re.IGNORECASE,stpos,ndpos+1250,4)
Hi Andrew,
because you wrote that you start learning python I thought I may give you some
friendly hints/ideas about using it together with pythonscript and npp.
The code you posted won't run because variable pos isn't initilized.
I assume it should be stpos and ndpos instead.
Maybe you wanna use String.Format function instead of old-style %s % text...
it is much more flexible.
getSelectionEnd is not needed and instead of adding 1250 to ndpos you could use
editor.getLength() to get the end of the document.
An alternative to notepad.messageBox could be console.write().
Of course makes only sense if you call console.show prior to write or you enable Show
Console via the menu
Happy coding
Claudia
Thanks Claudia. Yep found the pos issue although it only affected the notepad lines not that actual functionality I was after.
Below what I ended up refining the code to.
The issue was I'm replacing a selected number with and incremented or decremented value. The issue is there are lots of numbers in the block of text hence the nominal "1250" window.
I was able to get my replace quite precise except for the last one.
You'll note the last replace in this version of code actual has a start and end block to make sure the replace didn't replace the wrong value.
I do appreciate your pointers though. Thank you.
import re
text = editor.getSelText()
stpos = editor.getSelectionStart()
notepad.messageBox("Startpos: %i" % (stpos))
ndpos = editor.getSelectionEnd()
notepad.messageBox("Endpos: %i" % (ndpos))
notepad.messageBox("Selected text: %s" % (text))
inum = int(text)
notepad.messageBox("Selected inum: %i" % (inum))
newnum = inum + 1
notepad.messageBox("Selected newnum: %i" % (newnum))
text1 = text+". "
newtext1 = str(newnum)+". "
text2 = "h3>"+text+"."
newtext2 = "h3>"+str(newnum)+"."
text3 = ">"+text+""
newtext3 = ">"+str(newnum)+""
editor.replace(text1, newtext1,re.IGNORECASE,stpos,ndpos+1250,1)
editor.replace(text2, newtext2,re.IGNORECASE,stpos,ndpos+1250,1)
editor.replace(text3, newtext3,re.IGNORECASE,stpos,ndpos+1250,1)
editor.replace(text, newnum,re.IGNORECASE,stpos+1175,ndpos+1250,1)
Last edit: Andrew 2014-12-17