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