mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Make the Home key smarter when editing python
This commit is contained in:
parent
0745a05399
commit
5ca05e6698
@ -19,6 +19,13 @@ def get_text_before_cursor(editor):
|
|||||||
text = cursor.selectedText()
|
text = cursor.selectedText()
|
||||||
return cursor, text
|
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):
|
def expand_tabs(text):
|
||||||
return text.replace('\t', ' '*4)
|
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 expand_tabs(text[:len(text)-len(ntext)])
|
||||||
return ''
|
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):
|
class Smarts(NullSmarts):
|
||||||
|
|
||||||
override_tab_stop_width = 4
|
override_tab_stop_width = 4
|
||||||
@ -94,6 +115,16 @@ class Smarts(NullSmarts):
|
|||||||
editor.setTextCursor(cursor)
|
editor.setTextCursor(cursor)
|
||||||
return True
|
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__':
|
if __name__ == '__main__':
|
||||||
import os
|
import os
|
||||||
from calibre.gui2.tweak_book.editor.widget import launch_editor
|
from calibre.gui2.tweak_book.editor.widget import launch_editor
|
||||||
|
Loading…
x
Reference in New Issue
Block a user