1*cdf0e10cSrcweir
2*cdf0e10cSrcweir# helper function
3*cdf0e10cSrcweirdef getNewString( theString ) :
4*cdf0e10cSrcweir    if( not theString or len(theString) ==0) :
5*cdf0e10cSrcweir        return ""
6*cdf0e10cSrcweir    # should we tokenize on "."?
7*cdf0e10cSrcweir    if theString[0].isupper() and len(theString)>=2 and theString[1].isupper() :
8*cdf0e10cSrcweir	# first two chars are UC => first UC, rest LC
9*cdf0e10cSrcweir        newString=theString[0:1].upper() + theString[1:].lower();
10*cdf0e10cSrcweir    elif theString[0].isupper():
11*cdf0e10cSrcweir	# first char UC => all to LC
12*cdf0e10cSrcweir        newString=theString.lower()
13*cdf0e10cSrcweir    else: # all to UC.
14*cdf0e10cSrcweir        newString=theString.upper()
15*cdf0e10cSrcweir    return newString;
16*cdf0e10cSrcweir
17*cdf0e10cSrcweirdef capitalisePython( ):
18*cdf0e10cSrcweir    """Change the case of a selection, or current word from upper case, to first char upper case, to all lower case to upper case..."""
19*cdf0e10cSrcweir    import string
20*cdf0e10cSrcweir
21*cdf0e10cSrcweir    # The context variable is of type XScriptContext and is available to
22*cdf0e10cSrcweir    # all BeanShell scripts executed by the Script Framework
23*cdf0e10cSrcweir    xModel = XSCRIPTCONTEXT.getDocument()
24*cdf0e10cSrcweir
25*cdf0e10cSrcweir    #the writer controller impl supports the css.view.XSelectionSupplier interface
26*cdf0e10cSrcweir    xSelectionSupplier = xModel.getCurrentController()
27*cdf0e10cSrcweir
28*cdf0e10cSrcweir    #see section 7.5.1 of developers' guide
29*cdf0e10cSrcweir    xIndexAccess = xSelectionSupplier.getSelection()
30*cdf0e10cSrcweir    count = xIndexAccess.getCount();
31*cdf0e10cSrcweir    if(count>=1):  #ie we have a selection
32*cdf0e10cSrcweir        i=0
33*cdf0e10cSrcweir	while i < count :
34*cdf0e10cSrcweir            xTextRange = xIndexAccess.getByIndex(i);
35*cdf0e10cSrcweir            #print "string: " + xTextRange.getString();
36*cdf0e10cSrcweir            theString = xTextRange.getString();
37*cdf0e10cSrcweir            if len(theString)==0 :
38*cdf0e10cSrcweir                # sadly we can have a selection where nothing is selected
39*cdf0e10cSrcweir                # in this case we get the XWordCursor and make a selection!
40*cdf0e10cSrcweir                xText = xTextRange.getText();
41*cdf0e10cSrcweir                xWordCursor = xText.createTextCursorByRange(xTextRange);
42*cdf0e10cSrcweir                if not xWordCursor.isStartOfWord():
43*cdf0e10cSrcweir                    xWordCursor.gotoStartOfWord(False);
44*cdf0e10cSrcweir                xWordCursor.gotoNextWord(True);
45*cdf0e10cSrcweir                theString = xWordCursor.getString();
46*cdf0e10cSrcweir                newString = getNewString(theString);
47*cdf0e10cSrcweir                if newString :
48*cdf0e10cSrcweir                    xWordCursor.setString(newString);
49*cdf0e10cSrcweir                    xSelectionSupplier.select(xWordCursor);
50*cdf0e10cSrcweir            else :
51*cdf0e10cSrcweir
52*cdf0e10cSrcweir                newString = getNewString( theString );
53*cdf0e10cSrcweir                if newString:
54*cdf0e10cSrcweir                    xTextRange.setString(newString);
55*cdf0e10cSrcweir                    xSelectionSupplier.select(xTextRange);
56*cdf0e10cSrcweir	    i+= 1
57*cdf0e10cSrcweir
58*cdf0e10cSrcweir
59*cdf0e10cSrcweir# lists the scripts, that shall be visible inside OOo. Can be omited, if
60*cdf0e10cSrcweir# all functions shall be visible, however here getNewString shall be surpressed
61*cdf0e10cSrcweirg_exportedScripts = capitalisePython,
62