2 # This file is part of the LibreOffice project.
4 # This Source Code Form is subject to the terms of the Mozilla Public
5 # License, v. 2.0. If a copy of the MPL was not distributed with this
6 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 # This file incorporates work covered by the following license notice:
10 # Licensed to the Apache Software Foundation (ASF) under one or more
11 # contributor license agreements. See the NOTICE file distributed
12 # with this work for additional information regarding copyright
13 # ownership. The ASF licenses this file to you under the Apache
14 # License, Version 2.0 (the "License"); you may not use this file
15 # except in compliance with the License. You may obtain a copy of
16 # the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 def getNewString( theString
) :
21 if( not theString
or len(theString
) ==0) :
23 # should we tokenize on "."?
24 if theString
[0].isupper() and len(theString
)>=2 and theString
[1].isupper() :
25 # first two chars are UC => first UC, rest LC
26 newString
=theString
[0:1].upper() + theString
[1:].lower();
27 elif theString
[0].isupper():
28 # first char UC => all to LC
29 newString
=theString
.lower()
31 newString
=theString
.upper()
34 def capitalisePython( ):
35 """Change the case of a selection, or current word from upper case, to first char upper case, to all lower case to upper case..."""
38 # The context variable is of type XScriptContext and is available to
39 # all BeanShell scripts executed by the Script Framework
40 xModel
= XSCRIPTCONTEXT
.getDocument()
42 #the writer controller impl supports the css.view.XSelectionSupplier interface
43 xSelectionSupplier
= xModel
.getCurrentController()
45 #see section 7.5.1 of developers' guide
46 xIndexAccess
= xSelectionSupplier
.getSelection()
47 count
= xIndexAccess
.getCount();
48 if(count
>=1): #ie we have a selection
51 xTextRange
= xIndexAccess
.getByIndex(i
);
52 #print "string: " + xTextRange.getString();
53 theString
= xTextRange
.getString();
54 if len(theString
)==0 :
55 # sadly we can have a selection where nothing is selected
56 # in this case we get the XWordCursor and make a selection!
57 xText
= xTextRange
.getText();
58 xWordCursor
= xText
.createTextCursorByRange(xTextRange
);
59 if not xWordCursor
.isStartOfWord():
60 xWordCursor
.gotoStartOfWord(False);
61 xWordCursor
.gotoNextWord(True);
62 theString
= xWordCursor
.getString();
63 newString
= getNewString(theString
);
65 xWordCursor
.setString(newString
);
66 xSelectionSupplier
.select(xWordCursor
);
69 newString
= getNewString( theString
);
71 xTextRange
.setString(newString
);
72 xSelectionSupplier
.select(xTextRange
);
76 # lists the scripts, that shall be visible inside OOo. Can be omitted, if
77 # all functions shall be visible, however here getNewString shall be suppressed
78 g_exportedScripts
= capitalisePython
,
80 # vim: set shiftwidth=4 softtabstop=4 expandtab: