"ClipStack" by NickDMax

Rating: (0 votes)

Report this Script

Name: ClipStack
Uploaded By: NickDMax
Date Created: May 3, 2009 12:36 p.m.
Date Modified: May 3, 2009 12:36 p.m.
Description: A PN clipboard stack. When you copy using the ClipStack the selection is pushed on to the stack. When you paste using ClipStack you pop the top item off of the stack and paste into your document.
Num Views: 375
Num Downloads: 101

Contents

Download this script

###############################################################################
## 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()

Comments: