diff --git a/src/calibre/gui2/tweak_book/editor/smarts/python.py b/src/calibre/gui2/tweak_book/editor/smarts/python.py index 6271a76203..0fe902f2a9 100644 --- a/src/calibre/gui2/tweak_book/editor/smarts/python.py +++ b/src/calibre/gui2/tweak_book/editor/smarts/python.py @@ -19,6 +19,13 @@ def get_text_before_cursor(editor): text = cursor.selectedText() return cursor, text +def is_cursor_on_wrapped_line(editor): + cursor = editor.textCursor() + cursor.movePosition(cursor.StartOfLine) + sol = cursor.position() + cursor.movePosition(cursor.StartOfBlock) + return sol != cursor.position() + def expand_tabs(text): return text.replace('\t', ' '*4) @@ -33,6 +40,20 @@ def get_leading_whitespace_on_line(editor, previous=False): return expand_tabs(text[:len(text)-len(ntext)]) return '' +def no_modifiers(ev, *args): + mods = ev.modifiers() + for mod_mask in args: + if int(mods & mod_mask): + return False + return True + +def test_modifiers(ev, *args): + mods = ev.modifiers() + for mod_mask in args: + if not int(mods & mod_mask): + return False + return True + class Smarts(NullSmarts): override_tab_stop_width = 4 @@ -94,6 +115,16 @@ class Smarts(NullSmarts): editor.setTextCursor(cursor) return True + elif key == Qt.Key_Home and no_modifiers(ev, Qt.ControlModifier) and not is_cursor_on_wrapped_line(editor): + cursor, text = get_text_before_cursor(editor) + mode = cursor.KeepAnchor if test_modifiers(ev, Qt.ShiftModifier) else cursor.MoveAnchor + cursor.movePosition(cursor.StartOfBlock, mode) + if text.strip(): + # Move to the start of text + cursor.movePosition(cursor.NextWord, mode) + editor.setTextCursor(cursor) + return True + if __name__ == '__main__': import os from calibre.gui2.tweak_book.editor.widget import launch_editor