############################################################################### ## ClipStack2.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 ## ## Copy/Cut ## The selected item copied is placed onto the regular OS clipboard ## The last clipboard item (if text) is pushed onto the stack. ## ## Paste ## The current clipboard (if text) is pasted then the ## top item is poped into the clipboard. ## ## Warning: Since this uses the clipboard as the top item of the stack it ## is possible to loose items when mixing between this application and other ## windows since they do not push items onto the stack. ## For this reason there is a push command provided to force pushing the current ## contents of the clipboard onto the stack. So if you are going to copy from ## another program, you may first want to push the current clipboard onto the ## stack. ## ## By: NickDMax import pn import scintilla from pypn.decorators import script import win32clipboard as winclip import win32con class PNClipStack2: """ 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) ClipStack2 = PNClipStack2() @script('Stack Count', 'ClipStack2') def clstk2Count(): """ Prints out the size of the current ClipStack """ pn.AddOutput('ClipStack Size: ' + str(ClipStack2.getSize()) + '\n') @script('Push', 'ClipStack2') def clstk2Push(): """ Adds the current clipboard to the ClipStack """ doc = pn.CurrentDoc() if winclip.IsClipboardFormatAvailable(win32con.CF_TEXT): winclip.OpenClipboard() cbdata = winclip.GetClipboardData(win32con.CF_TEXT) winclip.CloseClipboard() if cbdata is not None: ClipStack2.push(cbdata) @script('Copy', 'ClipStack2') def clstk2Copy(): """ 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: clstk2Push() editor.CopyText(len(text), text) @script('Cut', 'ClipStack2') def clstk2Cut(): """ 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: clstk2Push() editor.Cut() @script('Paste', 'ClipStack2') def clstk2Paste(): """ 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) if winclip.IsClipboardFormatAvailable(win32con.CF_TEXT): editor.Paste() else: pn.AddOutput("Clipboard not text.\n") text = ClipStack2.pop() editor.CopyText(len(text), text) @script('Clear', 'ClipStack2') def clstk2Cear(): """ Clears the current ClipStack """ ClipStack2.clear() winclip.OpenClipboard() winclip.EmptyClipboard() winclip.CloseClipboard()