Skip to content

Commit

Permalink
Edit book: Allow removing the currently highlighted tag (while keepin…
Browse files Browse the repository at this point in the history
…g its contents) by pressing Ctrl+>. You can also add a tool to do it via Preferences->Toolbars
  • Loading branch information
kovidgoyal committed Aug 8, 2018
1 parent 81f1d44 commit 668e208
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/calibre/gui2/tweak_book/editor/smarts/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,20 @@ def jump_to_enclosing_tag(self, editor, start=True):
editor.setTextCursor(c)
return True

def remove_tag(self, editor):
editor.highlighter.join()

def erase_tag(tag):
c = editor.textCursor()
c.setPosition(tag.start_block.position() + tag.start_offset)
c.setPosition(tag.end_block.position() + tag.end_offset + 1, c.KeepAnchor)
c.removeSelectedText()

if self.last_matched_closing_tag:
erase_tag(self.last_matched_closing_tag)
if self.last_matched_tag:
erase_tag(self.last_matched_tag)

def rename_block_tag(self, editor, new_name):
editor.highlighter.join()
c = editor.textCursor()
Expand Down
4 changes: 4 additions & 0 deletions src/calibre/gui2/tweak_book/editor/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,10 @@ def insert_tag(self, tag):
if hasattr(self.smarts, 'insert_tag'):
self.smarts.insert_tag(self, tag)

def remove_tag(self):
if hasattr(self.smarts, 'remove_tag'):
self.smarts.remove_tag(self)

def keyPressEvent(self, ev):
if ev.key() == Qt.Key_X and ev.modifiers() == Qt.AltModifier:
if self.replace_possible_unicode_sequence():
Expand Down
3 changes: 3 additions & 0 deletions src/calibre/gui2/tweak_book/editor/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ def reg(*args, **kw):
ac = reg('code.png', _('Insert &tag'), ('insert_tag',), 'insert-tag', ('Ctrl+<'), _('Insert tag'), syntaxes=('html', 'xml'))
ac.setToolTip(_('<h3>Insert tag</h3>Insert a tag, if some text is selected the tag will be inserted around the selected text'))

ac = reg('trash.png', _('Remove &tag'), ('remove_tag',), 'remove-tag', ('Ctrl+>'), _('Remove tag'), syntaxes=('html', 'xml'))
ac.setToolTip(_('<h3>Remove tag</h3>Remove the currently highlighted tag'))

editor_toolbar_actions['html']['fix-html-current'] = actions['fix-html-current']
for s in ('xml', 'html', 'css'):
editor_toolbar_actions[s]['pretty-current'] = actions['pretty-current']
Expand Down

0 comments on commit 668e208

Please sign in to comment.