def createCursors(self, selectionCursor): text = selectionCursor.getText() cursLeftmost = selectionCursor.getStart() cursRightmost = selectionCursor.getEnd() try: if text.compareRegionStarts( selectionCursor.getEnd(), selectionCursor) >= 0: logger.debug("start of selection is on the right") # swap cursLeftmost, cursRightmost = cursRightmost, cursLeftmost except (RuntimeException, IllegalArgumentException): logger.warning("could not get range from selection") try: cursTmp = text.createTextCursorByRange(cursLeftmost) except (RuntimeException, IllegalArgumentException): raise exceptions.RangeError("Failed to go to text range.") cursTmp.gotoRange(cursRightmost, True) logger.debug("text = '%s'", cursTmp.getString()) # We use the viewcursor because text cursors cannot goRight into tables oVC = self.unoObjs.viewcursor oVC.gotoRange(cursLeftmost, False) #logger.debug(util.debug_tellNextChar(oVC)) self.cursLeft = oVC.getText().createTextCursorByRange(oVC) self.rangeRight = RangeCompare( text.createTextCursorByRange(cursRightmost), self.unoObjs.viewcursor)
def scopeSelection(self): """Search the currently selected text.""" logger.debug(util.funcName('begin')) self.ranger.resetRanges() oSel = self.unoObjs.controller.getSelection() if oSel is None: raise exceptions.RangeError("No text is selected.") #util.xray(oSel, self.unoObjs) #XXX: This check fails even though, according to xray, the object # *does* support it. #if not oSel.supportsService("com.sun.star.text.TextRange"): # raise exceptions.RangeError("No text is selected.") cursor = oSel.getText().createTextCursorByRange(oSel) if cursor.isCollapsed(): raise exceptions.RangeError("No text is selected.") self.ranger.addRange(oSel) logger.debug(util.funcName('end'))
def scopeSelection(self): """Search the currently selected text.""" logger.debug(util.funcName('begin')) self.ranger.resetRanges() oSels = self.unoObjs.controller.getSelection() if oSels is None: raise exceptions.RangeError("No text is selected.") if not oSels.supportsService("com.sun.star.text.TextRanges"): # When cells are selected rather than text, # the selection is a TextTableCursor and has no text ranges. raise exceptions.RangeError( "Please do not select individual table cells.") logger.debug("getCount() = %s", oSels.getCount()) if oSels.getCount() == 1: oSel = oSels.getByIndex(0) if oSel.supportsService("com.sun.star.text.TextRange"): cursor = oSel.getText().createTextCursorByRange(oSel) if cursor.isCollapsed(): raise exceptions.RangeError("No text is selected.") for oSel in iteruno.byIndex(oSels): if oSel.supportsService("com.sun.star.text.TextRange"): self.ranger.addRangesForCursor(oSel) logger.debug(util.funcName('end'))
def setTextRange(self, txtRange): """:param txtRange: type search.TxtRange""" oSel = txtRange.sel try: self.text = oSel.getText() self.rangeCursor = self.text.createTextCursorByRange(oSel) self.travelCursor = self.text.createTextCursorByRange( oSel.getStart()) self.placeCursor = self.text.createTextCursorByRange( oSel.getStart()) except (RuntimeException, IllegalArgumentException): logger.warning("Failed to go to text range.") raise exceptions.RangeError("Failed to go to text range.") logger.debug("String = <<%s>>", self.rangeCursor.getString())
def selectWord(self, stringBeforeWord, wordString): """Go to the word and select it.""" logger.debug( util.funcName('begin', args=(stringBeforeWord, wordString))) self.gotoLoc(stringBeforeWord) selectedString = "" while len(selectedString) < len(wordString): if not self.travelCursor.goRight(1, True): logger.warning("Could not go right.") raise exceptions.RangeError("Failed to go to text range.") try: selectedString = self.travelCursor.getString() logger.debug("'%s'", selectedString) except RuntimeException: logger.warning("Could not get string from selection.") raise exceptions.RangeError("Could not get selection string.") logger.debug("selectedString = '%s'", selectedString) try: # Select the word so the user can see it. self.unoObjs.viewcursor.gotoRange(self.travelCursor, False) except RuntimeException: logger.warning("Could not go to range.") raise exceptions.RangeError("Failed to go to text range.") return selectedString == wordString
def updateEx(self, refTextRough): """ This method gets called after a ref number to update has been selected in the document. The order of the next few steps is as follows: 1. Call gotoAfterEx() to move out of the table. 2. Insert the new example without the example number. 3. Call moveExNumber(). Steps 1 and 2 are done in insertEx(). """ logger.debug(util.funcName('begin')) if self.exType == EXTYPE_GRAMMAR: if not self.search.refInTable(): raise exceptions.RangeError( "Found a ref number, but it must be in an outer " "table in order to be updated.") self.insertEx(refTextRough, False, True) if self.exType == EXTYPE_GRAMMAR: if not self.settings.showCompDoc(): self.exUpdater.doNotMakeCompDoc() self.exUpdater.moveExNumber() self.exUpdater.moveExamplesToNewDoc() else: self.exUpdater.deleteOldPhonEx()
def outputExample(self, example, deleteRefNum, updatingEx): """Output the example to the Writer document.""" logger.debug(util.funcName('begin')) oVC = self.unoObjs.viewcursor # shorthand variable name ## Delete the selected reference number extraNewline = False # newline from ref number if deleteRefNum: self.unoObjs.dispatcher.executeDispatch(self.unoObjs.frame, ".uno:SwBackspace", "", 0, ()) if oVC.isAtEndOfLine(): extraNewline = True ## Start with default formatting at the beginning if oVC.TextTable or oVC.TextFrame: raise exceptions.RangeError("Cannot be inside a table or frame.") elif oVC.getText().getImplementationName() == "SwXHeadFootText": raise exceptions.RangeError("Cannot be in a header or footer.") try: self.textcursor = self.unoObjs.text.createTextCursorByRange( oVC.getStart()) except (RuntimeException, IllegalArgumentException): raise exceptions.RangeError("Cannot insert text here.") logger.debug("Created a text cursor.") propNames = [ 'ParaStyleName', 'CharStyleName', 'CharFontName', 'CharFontNameComplex', 'CharFontNameAsian', 'CharHeight', 'CharHeightComplex', 'CharHeightAsian' ] nextProps = dict() if self.textcursor.goRight(1, False): # Look ahead for next style and font for propName in propNames: nextProps[propName] = self.textcursor.getPropertyValue( propName) self.textcursor.goLeft(1, False) self.textcursor.setAllPropertiesToDefault() # works for fonts self.textcursor.setPropertyValue('ParaStyleName', 'Standard') self.textcursor.setPropertyToDefault('CharStyleName') ## Insert examples logger.debug("Writing example.") self.insertEx(example, updatingEx) self.unoObjs.text.insertControlCharacter(self.textcursor, PARAGRAPH_BREAK, 0) for propName in propNames: propVal = "" if propName in nextProps: propVal = nextProps[propName] if propVal: ## Set property to look-ahead value logger.debug("PropName '%s' = '%s'", propName, propVal) self.textcursor.setPropertyValue(propName, propVal) else: ## Set property to default value if propName == 'ParaStyleName': # Setting ParaStyleName to its default does not work. # So we use the underlying default style name "Standard". self.textcursor.setPropertyValue(propName, 'Standard') else: self.textcursor.setPropertyToDefault(propName) if extraNewline: # Delete the extra newline from the #ref number. self.unoObjs.dispatcher.executeDispatch(self.unoObjs.frame, ".uno:Delete", "", 0, ()) if updatingEx: # Go back to end of line before paragraph break, # in order to be ready to find the next ref number. oVC.goLeft(1, False) logger.debug("outputExamples FINISH")