From 9229cdc4025fb38661cbd0179d0a56ddcb66df34 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 30 Nov 2014 08:52:35 +0530 Subject: [PATCH] Edit Book: Do not change the current selected text when right clicking on a mis-spelled word --- src/calibre/gui2/tweak_book/editor/text.py | 4 ++-- src/calibre/gui2/tweak_book/editor/widget.py | 10 ++++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/calibre/gui2/tweak_book/editor/text.py b/src/calibre/gui2/tweak_book/editor/text.py index d762d67140..fcd36083be 100644 --- a/src/calibre/gui2/tweak_book/editor/text.py +++ b/src/calibre/gui2/tweak_book/editor/text.py @@ -247,8 +247,8 @@ class TextEdit(PlainTextEdit): self.setTextCursor(c) self.ensureCursorVisible() - def simple_replace(self, text): - c = self.textCursor() + def simple_replace(self, text, cursor=None): + c = cursor or self.textCursor() c.insertText(unicodedata.normalize('NFC', text)) self.setTextCursor(c) diff --git a/src/calibre/gui2/tweak_book/editor/widget.py b/src/calibre/gui2/tweak_book/editor/widget.py index 6bdc726997..42f869a7ed 100644 --- a/src/calibre/gui2/tweak_book/editor/widget.py +++ b/src/calibre/gui2/tweak_book/editor/widget.py @@ -443,8 +443,9 @@ class Editor(QMainWindow): a = m.addAction c = self.editor.cursorForPosition(pos) origc = QTextCursor(c) + current_cursor = self.editor.textCursor() r = origr = self.editor.syntax_range_for_cursor(c) - if (r is None or not r.format.property(SPELL_PROPERTY)) and c.positionInBlock() > 0: + if (r is None or not r.format.property(SPELL_PROPERTY)) and c.positionInBlock() > 0 and not current_cursor.hasSelection(): c.setPosition(c.position() - 1) r = self.editor.syntax_range_for_cursor(c) @@ -460,11 +461,16 @@ class Editor(QMainWindow): fc = self.editor.textCursor() if fc.position() < c.position(): self.editor.find_spell_word([word], locale.langcode, center_on_cursor=False) + spell_cursor = self.editor.textCursor() + if current_cursor.hasSelection(): + # Restore the current cursor so that any selection is preserved + # for the change case actions + self.editor.setTextCursor(current_cursor) if found: suggestions = dictionaries.suggestions(word, locale)[:7] if suggestions: for suggestion in suggestions: - ac = m.addAction(suggestion, partial(self.editor.simple_replace, suggestion)) + ac = m.addAction(suggestion, partial(self.editor.simple_replace, suggestion, cursor=spell_cursor)) f = ac.font() f.setBold(True), ac.setFont(f) m.addSeparator()