diff --git a/nw/gui/doceditor.py b/nw/gui/doceditor.py index 44238fc5..ed85cb3a 100644 --- a/nw/gui/doceditor.py +++ b/nw/gui/doceditor.py @@ -81,8 +81,8 @@ class GuiDocEditor(QTextEdit): # Custom Shortcuts QShortcut(QKeySequence("Ctrl+."), self, context=Qt.WidgetShortcut, activated=self._openSpellContext) - # QShortcut(Qt.Key_Return | Qt.ControlModifier, self, context=Qt.WidgetShortcut, activated=self._insertHardBreak) - # QShortcut(Qt.Key_Enter | Qt.ControlModifier, self, context=Qt.WidgetShortcut, activated=self._insertHardBreak) + QShortcut(Qt.Key_Return | Qt.ControlModifier, self, context=Qt.WidgetShortcut, activated=self._followTag) + QShortcut(Qt.Key_Enter | Qt.ControlModifier, self, context=Qt.WidgetShortcut, activated=self._followTag) # Set Up Word Count Thread and Timer self.wcInterval = self.mainConf.wordCountTimer @@ -356,6 +356,33 @@ class GuiDocEditor(QTextEdit): # Internal Functions ## + def _followTag(self): + """Activated by Ctrl+Enter. Checks that we're in a block starting with '@'. We then find the + word under the cursor and check that it is after the ':'. If all this is fine, we have a tag + and can tell the document viewer to try and find and load the file where the tag is defined. + """ + + theCursor = self.textCursor() + theBlock = theCursor.block() + theText = theBlock.text() + + if len(theText) == 0: + return False + + if theText.startswith("@"): + + theCursor.select(QTextCursor.WordUnderCursor) + theWord = theCursor.selectedText() + cPos = theText.find(":") + wPos = theCursor.selectionStart() - theBlock.position() + if wPos <= cPos: + return False + + logger.verbose("Attempting to follow tag '%s'" % theWord) + self.theParent.docViewer.loadFromTag(theWord) + + return True + def _insertHardBreak(self): theCursor = self.textCursor() theCursor.beginEditBlock() diff --git a/nw/gui/docviewer.py b/nw/gui/docviewer.py index 76c57e67..39342036 100644 --- a/nw/gui/docviewer.py +++ b/nw/gui/docviewer.py @@ -115,6 +115,24 @@ class GuiDocViewer(QTextBrowser): return True + def loadFromTag(self, theTag): + + logger.debug("Loading document from tag '%s'" % theTag) + + if theTag in self.theParent.theIndex.tagIndex.keys(): + theTarget = self.theParent.theIndex.tagIndex[theTag] + else: + logger.debug("The tag was not found in the index") + return False + + if len(theTarget) != 3: + # Just to make sure the index is not messed up + return False + + self.loadText(theTarget[1]) + + return True + ## # Internal Functions ##