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
):
25 # should we tokenize on "."?
26 if len(theString
) >= 2 and theString
[:2].isupper():
27 # first two chars are UC => first UC, rest LC
28 newString
= theString
[0].upper() + theString
[1:].lower()
30 elif theString
[0].isupper():
31 # first char UC => all to LC
32 newString
= theString
.lower()
36 newString
= theString
.upper()
41 def capitalisePython():
42 """Change the case of the selected or current word(s).
43 If at least the first two characters are "UPpercase, then it is changed
44 to first char "Uppercase".
45 If the first character is "Uppercase", then it is changed to
47 Otherwise, all are changed to "UPPERCASE".
49 # The context variable is of type XScriptContext and is available to
50 # all Python scripts executed by the Script Framework
51 # ask linters to therefore ignore it:
53 xModel
= XSCRIPTCONTEXT
.getDocument() # NOQA
54 # the writer controller impl supports the css.view.XSelectionSupplier
56 xSelectionSupplier
= xModel
.getCurrentController()
58 # see section 7.5.1 of developers' guide
59 xIndexAccess
= xSelectionSupplier
.getSelection()
60 count
= xIndexAccess
.getCount()
62 if count
>= 1: # ie we have a selection
66 xTextRange
= xIndexAccess
.getByIndex(i
)
67 theString
= xTextRange
.getString()
69 if len(theString
) == 0:
70 # sadly we can have a selection where nothing is selected
71 # in this case we get the XWordCursor and make a selection!
72 xText
= xTextRange
.getText()
73 xWordCursor
= xText
.createTextCursorByRange(xTextRange
)
75 if not xWordCursor
.isStartOfWord():
76 xWordCursor
.gotoStartOfWord(False)
78 xWordCursor
.gotoNextWord(True)
79 theString
= xWordCursor
.getString()
80 newString
= getNewString(theString
)
83 xWordCursor
.setString(newString
)
84 xSelectionSupplier
.select(xWordCursor
)
86 newString
= getNewString(theString
)
88 xTextRange
.setString(newString
)
89 xSelectionSupplier
.select(xTextRange
)
93 # lists the scripts, that shall be visible inside OOo. Can be omitted, if
94 # all functions shall be visible, however here getNewString shall be suppressed
95 g_exportedScripts
= (capitalisePython
,)
97 # vim: set shiftwidth=4 softtabstop=4 expandtab: