Included as a companion to Swap Items https://sourceforge.net/p/npppythonscript/discussion/1199074/thread/85b4b1b4/#d3d2 this script will convert a comparison operator to it's inverse!
~~~~~~ :::python
''' PURPOSE One may need to invert the comparison operator:
: <= = : < < : >= <= : > = : <> == : != != : == <> : =
How to use: Set a KB short-cut for script, i.e. Ctrl-Shift-Z Make the appropriate Selection Press the KB short-cut. ''' import traceback
def pri(*args): # used for debug for i in args : console.write(str(i) + "\t") console.write("\n")
def InvertOperator(): e = editor lFrom = ['>', '>=', '<', '<=', '=', '==', '!=', '<>'] lTo = ['<=', '<', '>=', '>', '<>', '!=', '==', '=' ] try: # pCP = e.getCurrentPos() pSS = e.getSelectionStart() # NB 0-based pSE = e.getSelectionEnd() # NB after last char sSel = e.getTextRange(pSS, pSE) # pri("pSS:", pSS, "pSE:", pSE, "sSel:", sSel) if sSel in lFrom: n = lFrom.index(sSel) sRep = lTo[n] e.setTarget(pSS, pSE) e.replaceTarget(sRep) e.setSel(pSS, pSS + len(sRep)) except Exception, err: # sys.stderr.write('ERROR: %s\n' % str(err)) print('ERROR: %s\n' % str(err)) traceback.print_exc() #finally: # allways
editor.beginUndoAction() InvertOperator() editor.endUndoAction()
Log in to post a comment.
Included as a companion to Swap Items https://sourceforge.net/p/npppythonscript/discussion/1199074/thread/85b4b1b4/#d3d2
this script will convert a comparison operator to it's inverse!
~~~~~~
:::python
C:\Users\admin\AppData\Roaming\Notepad++\plugins\config\PythonScript\scripts\Invert_Comparison.py ; 2015-04-21 18:15:02 ; admin on ASUS-W7-BS ; 50 lines ; 1336 bytes
'''
PURPOSE
One may need to invert the comparison operator:
How to use:
Set a KB short-cut for script, i.e. Ctrl-Shift-Z
Make the appropriate Selection
Press the KB short-cut.
'''
import traceback
def pri(*args): # used for debug
for i in args : console.write(str(i) + "\t")
console.write("\n")
def InvertOperator():
e = editor
lFrom = ['>', '>=', '<', '<=', '=', '==', '!=', '<>']
lTo = ['<=', '<', '>=', '>', '<>', '!=', '==', '=' ]
try:
# pCP = e.getCurrentPos()
pSS = e.getSelectionStart() # NB 0-based
pSE = e.getSelectionEnd() # NB after last char
sSel = e.getTextRange(pSS, pSE)
# pri("pSS:", pSS, "pSE:", pSE, "sSel:", sSel)
if sSel in lFrom:
n = lFrom.index(sSel)
sRep = lTo[n]
e.setTarget(pSS, pSE)
e.replaceTarget(sRep)
e.setSel(pSS, pSS + len(sRep))
except Exception, err:
# sys.stderr.write('ERROR: %s\n' % str(err))
print('ERROR: %s\n' % str(err))
traceback.print_exc()
#finally: # allways
editor.beginUndoAction()
InvertOperator()
editor.endUndoAction()
Last edit: Kadner 2015-04-22