############################################################################### ## ClipStack.py -- A clipboard stack allowing you to copy items into a stack ## and then paste them back in a FILO way. So the first item copied to the stack ## is the last item pasted from it. ## Copy item #1, Copy item #2, Copy item #3 ## Paste #3, #2, #1 ## ## The last item copied is placed onto the regular OS clipboard ## The last item pasted is ALSO placed into the regular OS clipboard ## So take care not to confuse this with the regular OS clipboard and ## The standard Copy/Paste operations. ## By: NickDMax import pn import scintilla from pypn.decorators import script class PNClipStack: """ Maintains a stack of text to paste """ def __init__(self): """ initialize inner data: stack -- the internal list used to store clipboard items """ self.stack = [] def push(self, text): self.stack.append(text) def pop(self): retValue = '' if len(self.stack) > 0: retValue = self.stack.pop() return retValue def clear(self): self.stack = [] def getSize(self): return len(self.stack) ClipStack = PNClipStack() @script('Stack Count', 'ClipStack') def clstkCount(): """ Prints out the size of the current ClipStack """ pn.AddOutput('ClipStack Size: ' + str(ClipStack.getSize()) + '\n') @script('Copy', 'ClipStack') def clstkCopy(): """ Adds the current selection to the ClipStack """ doc = pn.CurrentDoc() if doc is not None: #Lets try not to crash our script editor = scintilla.Scintilla(doc) start = editor.SelectionStart end = editor.SelectionEnd if (start == end): #nothing is selected lets try to grab the current word. start = editor.WordStartPosition(start, True) end = editor.WordEndPosition(end, True) text = None if (start != end): text = editor.GetTextRange(start, end) if text is not None: ClipStack.push(text) editor.CopyText(len(text), text) #clstkCount() @script('Cut', 'ClipStack') def clstkCut(): """ Adds the current selection to the ClipStack -- cuts it from document""" doc = pn.CurrentDoc() if doc is not None: #Lets try not to crash our script editor = scintilla.Scintilla(doc) start = editor.SelectionStart end = editor.SelectionEnd if (start == end): #nothing is selected lets try to grab the current word. start = editor.WordStartPosition(start, True) end = editor.WordEndPosition(end, True) text = None if (start != end): text = editor.GetTextRange(start, end) if text is not None: ClipStack.push(text) editor.SetSel(start, end) editor.Cut() #clstkCount() @script('Paste', 'ClipStack') def clstkPaste(): """ Pastes the top item from the ClipStack into the document """ doc = pn.CurrentDoc() if doc is not None: #Lets try not to crash our script editor = scintilla.Scintilla(doc) text = ClipStack.pop() editor.CopyText(len(text), text) editor.Paste() #clstkCount() @script('Clear', 'ClipStack') def clstkCear(): """ Clears the current ClipStack """ ClipStack.clear() #clstkCount()