diff --git a/novelwriter/core/coretools.py b/novelwriter/core/coretools.py index f6558600..b8fc5ba1 100644 --- a/novelwriter/core/coretools.py +++ b/novelwriter/core/coretools.py @@ -382,8 +382,7 @@ class DocSearch: escaped += f"\\{c}" search = escaped if self._words: - search = search if search.startswith("\\b") else f"\\b{search}" - search = search if search.endswith("\\b") else f"{search}\\b" + search = f"(?:^|\\b){search}(?:$|\\b)" return search # END Class DocSearch diff --git a/novelwriter/gui/doceditor.py b/novelwriter/gui/doceditor.py index d695f041..fbf4f224 100644 --- a/novelwriter/gui/doceditor.py +++ b/novelwriter/gui/doceditor.py @@ -596,6 +596,15 @@ class GuiDocEditor(QPlainTextEdit): text = text.replace(nwUnicode.U_PSEP, "\n") # Paragraph separators return text + def getSelectedText(self) -> str: + """Get currently selected text.""" + if (cursor := self.textCursor()).hasSelection(): + text = cursor.selectedText() + text = text.replace(nwUnicode.U_LSEP, "\n") # Line separators + text = text.replace(nwUnicode.U_PSEP, "\n") # Paragraph separators + return text + return "" + def getCursorPosition(self) -> int: """Find the cursor position in the document. If the editor has a selection, return the position of the end of the selection. @@ -642,12 +651,13 @@ class GuiDocEditor(QPlainTextEdit): logger.debug("Cursor moved to line %d", line) return - def setCursorSelection(self, selStart: int, selLength: int) -> None: + def setCursorSelection(self, start: int, length: int) -> None: """Make a text selection.""" - cursor = self.textCursor() - cursor.setPosition(selStart, QTextCursor.MoveMode.MoveAnchor) - cursor.setPosition(selStart + selLength, QTextCursor.MoveMode.KeepAnchor) - self.setTextCursor(cursor) + if start >= 0 and length > 0: + cursor = self.textCursor() + cursor.setPosition(start, QTextCursor.MoveMode.MoveAnchor) + cursor.setPosition(start + length, QTextCursor.MoveMode.KeepAnchor) + self.setTextCursor(cursor) return ## @@ -1280,11 +1290,7 @@ class GuiDocEditor(QPlainTextEdit): def beginSearch(self) -> None: """Set the selected text as the search text.""" - cursor = self.textCursor() - if cursor.hasSelection(): - self.docSearch.setSearchText(cursor.selectedText()) - else: - self.docSearch.setSearchText(None) + self.docSearch.setSearchText(self.getSelectedText() or None) resS, _ = self.findAllOccurences() self.docSearch.setResultCount(None, len(resS)) return @@ -2226,7 +2232,7 @@ class BackgroundWordCounter(QRunnable): """ self._isRunning = True if self._forSelection: - text = self._docEditor.textCursor().selectedText() + text = self._docEditor.getSelectedText() else: text = self._docEditor.getText() diff --git a/novelwriter/gui/search.py b/novelwriter/gui/search.py index 0394c26e..769689ee 100644 --- a/novelwriter/gui/search.py +++ b/novelwriter/gui/search.py @@ -192,10 +192,13 @@ class GuiProjectSearch(QWidget): ) return - def beginSearch(self) -> None: + def beginSearch(self, text: str = "") -> None: """Focus the search box and select its text, if any.""" self.searchText.setFocus() self.searchText.selectAll() + if text: + self.searchText.setText(text.partition("\n")[0]) + self.searchText.selectAll() return def closeProjectTasks(self) -> None: diff --git a/novelwriter/guimain.py b/novelwriter/guimain.py index 3a336577..14f9a494 100644 --- a/novelwriter/guimain.py +++ b/novelwriter/guimain.py @@ -1168,7 +1168,9 @@ class GuiMain(QMainWindow): elif view == nwView.SEARCH: self.mainStack.setCurrentWidget(self.splitMain) self.projStack.setCurrentWidget(self.projSearch) - self.projSearch.beginSearch() + self.projSearch.beginSearch( + self.docEditor.getSelectedText() if self.docEditor.anyFocus() else "" + ) elif view == nwView.OUTLINE: self.mainStack.setCurrentWidget(self.outlineView) return