I have just detected (and perhaps fixed) an old error that has been lurking (for a very long time?) in the `capitalise.py` example file in OpenOffice (4.1.16) and persists in LibreOffice (26.2).
The macro crashes if there is a multiple selection. I admit I didn't quite understand why.
On a day of extreme heat, I decided not to take the bike out...
In the code, we find:
Code: Select all
xModel = XSCRIPTCONTEXT.getDocument() # NOQA
xSelectionSupplier = xModel.getCurrentController()
xIndexAccess = xSelectionSupplier.getSelection()
count = xIndexAccess.getCount()
with no words selected or just one, `count` equals 1, but with two words selected, `count` equals 3!
And with N selections, `count` is N+1... naturally.
With 0 or one word, the selection is obtained via `xIndexAccess.getByIndex(0)` (a single element).
Otherwise, it is obtained via `xIndexAccess.getByIndex(i)` where 0 < i < count, with the first element being empty (a mystery?).
Next, we find:
Code: Select all
if count >= 1: # ie we have a selection
i = 0
while i < count:
xTextRange = xIndexAccess.getByIndex(i)
theString = xTextRange.getString()
if len(theString)==0 # This is where the ambiguity lies.
Indeed, following these instructions, if there is no selection, the macro selects the word under the cursor and applies the capitalization to it.
In the case of multiple selections, expanding the selection to the word at the cursor disrupts the rest of the macro.
Therefore, simply replace the ambiguous line with:
Code: Select all
if len(theString)==0 and count == 1: # no sélectionI am not quite sure whom to send this fix to; it has been outstanding for years and does not reflect well on Python programming.
I am also posting this on the French-language forum.
Have a great summer, everyone.
P.S.: As a bonus, here is a document to test the macro.