Skip to content

Commit

Permalink
Edit book: A new action to toggle line wrapping mode in all code edit…
Browse files Browse the repository at this point in the history
…ors. Can be assigned via Preferences->Keyboard shortcuts->Global actions

or added to the toolbar via Preferences->Toolbars->Book wide actions
  • Loading branch information
kovidgoyal committed Jun 30, 2024
1 parent de55056 commit 7eb17c1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/calibre/gui2/tweak_book/boss.py
Original file line number Diff line number Diff line change
Expand Up @@ -1844,6 +1844,13 @@ def do_editor_paste(self):
if ed is not None:
ed.paste()

def toggle_line_wrapping_in_all_editors(self):
tprefs['editor_line_wrap'] ^= True
yes = tprefs['editor_line_wrap']
for ed in editors.values():
if getattr(ed, 'editor', None) and hasattr(ed.editor, 'apply_line_wrap_mode'):
ed.editor.apply_line_wrap_mode(yes)

def editor_data_changed(self, editor):
self.gui.preview.start_refresh_timer()
for name, ed in iteritems(editors):
Expand Down
5 changes: 4 additions & 1 deletion src/calibre/gui2/tweak_book/editor/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,13 @@ def is_modified(self, val):
def sizeHint(self):
return self.size_hint

def apply_line_wrap_mode(self, yes: bool = True) -> None:
self.setLineWrapMode(QPlainTextEdit.LineWrapMode.WidgetWidth if yes else QPlainTextEdit.LineWrapMode.NoWrap)

def apply_settings(self, prefs=None, dictionaries_changed=False): # {{{
prefs = prefs or tprefs
self.setAcceptDrops(prefs.get('editor_accepts_drops', True))
self.setLineWrapMode(QPlainTextEdit.LineWrapMode.WidgetWidth if prefs['editor_line_wrap'] else QPlainTextEdit.LineWrapMode.NoWrap)
self.apply_line_wrap_mode(prefs['editor_line_wrap'])
with suppress(Exception):
self.setCursorWidth(int(prefs.get('editor_cursor_width', 1)))
theme = get_theme(prefs['editor_theme'])
Expand Down
3 changes: 3 additions & 0 deletions src/calibre/gui2/tweak_book/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,9 @@ def treg(icon, text, target, sid, keys, description):
self.boss.import_book, 'import-book', (), _('Import an HTML or DOCX file as a new book'))
self.action_quick_edit = treg('modified.png', _('&Quick open a file to edit'), self.boss.quick_open, 'quick-open', ('Ctrl+T'), _(
'Quickly open a file from the book to edit it'))
self.action_editor_toggle_wrap = treg(
'format-justify-fill.png', _('Toggle code line &wrapping'), self.boss.toggle_line_wrapping_in_all_editors, 'editor-toggle-wrap', (), _(
'Toggle line wrapping in all code editor tabs'))

# Editor actions
group = _('Editor actions')
Expand Down

0 comments on commit 7eb17c1

Please sign in to comment.