Skip to content

Commit

Permalink
Edit Book: Add keyboard shortcuts to jumpt to opening and closing tag…
Browse files Browse the repository at this point in the history
…s (Ctrl+{ and Ctrl+})
  • Loading branch information
kovidgoyal committed Apr 16, 2017
1 parent 89eb3a5 commit d387cac
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
9 changes: 9 additions & 0 deletions manual/edit.rst
Expand Up @@ -837,3 +837,12 @@ text. The editor allows you to insert a snippet with only a few key strokes.
The snippets are very powerful, with many features, such as placeholders you
can jump between, automatic mirroring of repeated text and so on.
For more information, see :doc:`snippets`.


Matching tags
^^^^^^^^^^^^^^^^^^^^^^^^^^^

When editing HTML or XML files, the closest tag containing the current cursor
position is automatically highlighted. You can jump to either the opening tag
or the closing tag using the keyboard shortcuts, :kbd:`Ctrl+{` and
:kbd:`Ctrl+}`.
21 changes: 19 additions & 2 deletions src/calibre/gui2/tweak_book/editor/smarts/html.py
Expand Up @@ -311,7 +311,7 @@ def __init__(self, *args, **kwargs):
Smarts.self_closing_pat = re.compile(r'/\s*>')
Smarts.complete_attr_pat = re.compile(r'''([a-zA-Z0-9_-]+)\s*=\s*(?:'([^']*)|"([^"]*))$''')
NullSmarts.__init__(self, *args, **kwargs)
self.last_matched_tag = None
self.last_matched_tag = self.last_matched_closing_tag = None

def get_extra_selections(self, editor):
ans = []
Expand All @@ -332,13 +332,24 @@ def add_tag(tag):
c = editor.textCursor()
block, offset = c.block(), c.positionInBlock()
tag = self.last_matched_tag = find_closest_containing_tag(block, offset, max_tags=2000)
self.last_matched_closing_tag = None
if tag is not None:
add_tag(tag)
tag = find_closing_tag(tag, max_tags=4000)
tag = self.last_matched_closing_tag = find_closing_tag(tag, max_tags=4000)
if tag is not None:
add_tag(tag)
return ans

def jump_to_enclosing_tag(self, editor, start=True):
editor.highlighter.join()
tag = self.last_matched_tag if start else self.last_matched_closing_tag
if tag is None:
return False
c = editor.textCursor()
c.setPosition(tag.start_block.position() + tag.start_offset + (1 if start else 2))
editor.setTextCursor(c)
return True

def rename_block_tag(self, editor, new_name):
editor.highlighter.join()
c = editor.textCursor()
Expand Down Expand Up @@ -621,6 +632,12 @@ def handle_key_press(self, ev, editor):
if key == Qt.Key_Backspace and smart_backspace(editor, ev):
return True

if key in (Qt.Key_BraceLeft, Qt.Key_BraceRight):
mods = ev.modifiers()
if int(mods & Qt.ControlModifier):
if self.jump_to_enclosing_tag(editor, key == Qt.Key_BraceLeft):
return True

return False

def replace_possible_entity(self, editor):
Expand Down

0 comments on commit d387cac

Please sign in to comment.