diff --git a/src/calibre/gui2/tweak_book/boss.py b/src/calibre/gui2/tweak_book/boss.py
index e108659e1c..f032a41d6c 100644
--- a/src/calibre/gui2/tweak_book/boss.py
+++ b/src/calibre/gui2/tweak_book/boss.py
@@ -443,6 +443,11 @@ class Boss(QObject):
if ed.has_marked_text:
self.gui.central.search_panel.set_where('selected-text')
+ def editor_action(self, action):
+ ed = self.gui.central.current_editor
+ if hasattr(ed, 'action_triggered'):
+ ed.action_triggered(action)
+
def show_find(self):
self.gui.central.show_find()
ed = self.gui.central.current_editor
diff --git a/src/calibre/gui2/tweak_book/editor/text.py b/src/calibre/gui2/tweak_book/editor/text.py
index 4eb8e0b378..cf19710fbb 100644
--- a/src/calibre/gui2/tweak_book/editor/text.py
+++ b/src/calibre/gui2/tweak_book/editor/text.py
@@ -161,6 +161,7 @@ class TextEdit(QPlainTextEdit):
clipboard.setMimeData(md, clipboard.Selection)
def load_text(self, text, syntax='html', process_template=False):
+ self.syntax = syntax
self.highlighter = {'html':HTMLHighlighter, 'css':CSSHighlighter, 'xml':XMLHighlighter}.get(syntax, SyntaxHighlighter)(self)
self.highlighter.apply_theme(self.theme)
self.highlighter.setDocument(self.document())
@@ -460,3 +461,49 @@ class TextEdit(QPlainTextEdit):
ev.ignore()
# }}}
+ def get_range_inside_tag(self):
+ c = self.textCursor()
+ left = min(c.anchor(), c.position())
+ right = max(c.anchor(), c.position())
+ # For speed we use QPlainTextEdit's toPlainText as we dont care about
+ # spaces in this context
+ raw = unicode(QPlainTextEdit.toPlainText(self))
+ # Make sure the left edge is not within a <>
+ gtpos = raw.find('>', left)
+ ltpos = raw.find('<', left)
+ if gtpos < ltpos:
+ left = gtpos + 1 if gtpos > -1 else left
+ right = max(left, right)
+ if right != left:
+ gtpos = raw.find('>', right)
+ ltpos = raw.find('<', right)
+ if ltpos > gtpos:
+ ltpos = raw.rfind('<', left, right+1)
+ right = max(ltpos, left)
+ return left, right
+
+ def format_text(self, formatting):
+ if self.syntax != 'html':
+ return
+ prefix, suffix = {
+ 'bold': ('', ''),
+ 'italic': ('', ''),
+ 'underline': ('', ''),
+ 'strikethrough': ('', ''),
+ 'superscript': ('', ''),
+ 'subscript': ('', ''),
+ }[formatting]
+ left, right = self.get_range_inside_tag()
+ c = self.textCursor()
+ c.setPosition(left)
+ c.setPosition(right, c.KeepAnchor)
+ prev_text = unicode(c.selectedText())
+ c.insertText(prefix + prev_text + suffix)
+ if prev_text:
+ right = c.position()
+ c.setPosition(left)
+ c.setPosition(right, c.KeepAnchor)
+ else:
+ c.setPosition(c.position() - len(suffix))
+ self.setTextCursor(c)
+
diff --git a/src/calibre/gui2/tweak_book/editor/widget.py b/src/calibre/gui2/tweak_book/editor/widget.py
index 5fa7905498..22335a42dd 100644
--- a/src/calibre/gui2/tweak_book/editor/widget.py
+++ b/src/calibre/gui2/tweak_book/editor/widget.py
@@ -14,6 +14,23 @@ from calibre.gui2 import error_dialog
from calibre.gui2.tweak_book import actions, current_container
from calibre.gui2.tweak_book.editor.text import TextEdit
+def register_text_editor_actions(reg):
+ ac = reg('format-text-bold', _('&Bold'), ('format_text', 'bold'), 'format-text-bold', 'Ctrl+B', _('Make the selected text bold'))
+ ac.setToolTip(_('