Capitalise.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #
  2. # This file is part of the LibreOffice project.
  3. #
  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/.
  7. #
  8. # This file incorporates work covered by the following license notice:
  9. #
  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 .
  17. #
  18. def getNewString(theString):
  19. """helper function
  20. """
  21. if (not theString):
  22. return ""
  23. # should we tokenize on "."?
  24. if len(theString) >= 2 and theString[:2].isupper():
  25. # first two chars are UC => first UC, rest LC
  26. newString = theString[0].upper() + theString[1:].lower()
  27. elif theString[0].isupper():
  28. # first char UC => all to LC
  29. newString = theString.lower()
  30. else:
  31. # all to UC.
  32. newString = theString.upper()
  33. return newString
  34. def capitalisePython():
  35. """Change the case of the selected or current word(s).
  36. If at least the first two characters are "UPpercase, then it is changed
  37. to first char "Uppercase".
  38. If the first character is "Uppercase", then it is changed to
  39. all "lowercase".
  40. Otherwise, all are changed to "UPPERCASE".
  41. """
  42. # The context variable is of type XScriptContext and is available to
  43. # all BeanShell scripts executed by the Script Framework
  44. xModel = XSCRIPTCONTEXT.getDocument()
  45. # the writer controller impl supports the css.view.XSelectionSupplier
  46. # interface
  47. xSelectionSupplier = xModel.getCurrentController()
  48. # see section 7.5.1 of developers' guide
  49. xIndexAccess = xSelectionSupplier.getSelection()
  50. count = xIndexAccess.getCount()
  51. if(count >= 1): # ie we have a selection
  52. i = 0
  53. while i < count:
  54. xTextRange = xIndexAccess.getByIndex(i)
  55. theString = xTextRange.getString()
  56. # print("theString")
  57. if len(theString) == 0:
  58. # sadly we can have a selection where nothing is selected
  59. # in this case we get the XWordCursor and make a selection!
  60. xText = xTextRange.getText()
  61. xWordCursor = xText.createTextCursorByRange(xTextRange)
  62. if not xWordCursor.isStartOfWord():
  63. xWordCursor.gotoStartOfWord(False)
  64. xWordCursor.gotoNextWord(True)
  65. theString = xWordCursor.getString()
  66. newString = getNewString(theString)
  67. if newString:
  68. xWordCursor.setString(newString)
  69. xSelectionSupplier.select(xWordCursor)
  70. else:
  71. newString = getNewString(theString)
  72. if newString:
  73. xTextRange.setString(newString)
  74. xSelectionSupplier.select(xTextRange)
  75. i += 1
  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,
  79. # vim: set shiftwidth=4 softtabstop=4 expandtab: