Implement marking of regions in the text

This commit is contained in:
Kovid Goyal 2013-11-11 17:48:20 +05:30
parent 76d22f8f68
commit a9d96d8d77
6 changed files with 43 additions and 1 deletions

View File

@ -259,6 +259,12 @@ class Boss(QObject):
self.update_global_history_actions() self.update_global_history_actions()
# }}} # }}}
def mark_selected_text(self):
ed = self.gui.central.current_editor
if ed is not None:
ed.mark_selected_text()
self.gui.central.search_panel.set_where('selected-text')
def search(self, action, overrides=None): def search(self, action, overrides=None):
' Run a search/replace ' ' Run a search/replace '
sp = self.gui.central.search_panel sp = self.gui.central.search_panel

View File

@ -49,6 +49,8 @@ class TextEdit(QPlainTextEdit):
def __init__(self, parent=None): def __init__(self, parent=None):
QPlainTextEdit.__init__(self, parent) QPlainTextEdit.__init__(self, parent)
self.current_cursor_line = None
self.current_search_mark = None
self.highlighter = SyntaxHighlighter(self) self.highlighter = SyntaxHighlighter(self)
self.apply_settings() self.apply_settings()
self.setMouseTracking(True) self.setMouseTracking(True)
@ -107,6 +109,7 @@ class TextEdit(QPlainTextEdit):
w = self.fontMetrics() w = self.fontMetrics()
self.number_width = max(map(lambda x:w.width(str(x)), xrange(10))) self.number_width = max(map(lambda x:w.width(str(x)), xrange(10)))
self.size_hint = QSize(100 * w.averageCharWidth(), 50 * w.height()) self.size_hint = QSize(100 * w.averageCharWidth(), 50 * w.height())
self.highlight_color = theme_color(theme, 'HighlightRegion', 'bg')
# }}} # }}}
def load_text(self, text, syntax='html'): def load_text(self, text, syntax='html'):
@ -127,6 +130,24 @@ class TextEdit(QPlainTextEdit):
self.setTextCursor(c) self.setTextCursor(c)
self.ensureCursorVisible() self.ensureCursorVisible()
def update_extra_selections(self):
sel = []
if self.current_cursor_line is not None:
sel.append(self.current_cursor_line)
if self.current_search_mark is not None:
sel.append(self.current_search_mark)
self.setExtraSelections(sel)
def mark_selected_text(self):
sel = QTextEdit.ExtraSelection()
sel.format.setBackground(self.highlight_color)
sel.cursor = self.textCursor()
self.current_search_mark = sel
self.update_extra_selections()
c = self.textCursor()
c.clearSelection()
self.setTextCursor(c)
# Line numbers and cursor line {{{ # Line numbers and cursor line {{{
def highlight_cursor_line(self): def highlight_cursor_line(self):
sel = QTextEdit.ExtraSelection() sel = QTextEdit.ExtraSelection()
@ -134,7 +155,8 @@ class TextEdit(QPlainTextEdit):
sel.format.setProperty(QTextFormat.FullWidthSelection, True) sel.format.setProperty(QTextFormat.FullWidthSelection, True)
sel.cursor = self.textCursor() sel.cursor = self.textCursor()
sel.cursor.clearSelection() sel.cursor.clearSelection()
self.setExtraSelections([sel]) self.current_cursor_line = sel
self.update_extra_selections()
# Update the cursor line's line number in the line number area # Update the cursor line's line number in the line number area
try: try:
self.line_number_area.update(0, self.last_current_lnum[0], self.line_number_area.width(), self.last_current_lnum[1]) self.line_number_area.update(0, self.last_current_lnum[0], self.line_number_area.width(), self.last_current_lnum[1])

View File

@ -34,6 +34,7 @@ SOLARIZED = \
CursorLine bg={base02} CursorLine bg={base02}
CursorColumn bg={base02} CursorColumn bg={base02}
ColorColumn bg={base02} ColorColumn bg={base02}
HighlightRegion bg={base00}
MatchParen fg={red} bg={base01} bold MatchParen fg={red} bg={base01} bold
Pmenu fg={base0} bg={base02} Pmenu fg={base0} bg={base02}
PmenuSel fg={base01} bg={base2} PmenuSel fg={base01} bg={base2}
@ -68,6 +69,7 @@ THEMES = {
CursorLine bg={cursor_loc} CursorLine bg={cursor_loc}
CursorColumn bg={cursor_loc} CursorColumn bg={cursor_loc}
ColorColumn bg={cursor_loc} ColorColumn bg={cursor_loc}
HighlightRegion bg=323232
MatchParen fg=f6f3e8 bg=857b6f bold MatchParen fg=f6f3e8 bg=857b6f bold
Pmenu fg=f6f3e8 bg=444444 Pmenu fg=f6f3e8 bg=444444
PmenuSel fg=yellow bg={identifier} PmenuSel fg=yellow bg={identifier}
@ -106,6 +108,7 @@ THEMES = {
CursorLine bg={cursor_loc} CursorLine bg={cursor_loc}
CursorColumn bg={cursor_loc} CursorColumn bg={cursor_loc}
ColorColumn bg={cursor_loc} ColorColumn bg={cursor_loc}
HighlightRegion bg=E3F988
MatchParen fg=white bg=80a090 bold MatchParen fg=white bg=80a090 bold
Pmenu fg=white bg=808080 Pmenu fg=white bg=808080
PmenuSel fg=white bg=808080 PmenuSel fg=white bg=808080

View File

@ -67,6 +67,9 @@ class Editor(QMainWindow):
def redo(self): def redo(self):
self.editor.redo() self.editor.redo()
def mark_selected_text(self):
self.editor.mark_selected_text()
@dynamic_property @dynamic_property
def is_modified(self): def is_modified(self):
def fget(self): def fget(self):

View File

@ -219,6 +219,8 @@ class SearchWidget(QWidget):
def restore_state(self): def restore_state(self):
self.state = tprefs.get('find-widget-state', self.DEFAULT_STATE) self.state = tprefs.get('find-widget-state', self.DEFAULT_STATE)
if self.where == 'selected-text':
self.where = self.DEFAULT_STATE['where']
def save_state(self): def save_state(self):
tprefs.set('find-widget-state', self.state) tprefs.set('find-widget-state', self.state)
@ -261,3 +263,6 @@ class SearchPanel(QWidget):
ans['replace'] = self.widget.replace ans['replace'] = self.widget.replace
return ans return ans
def set_where(self, val):
self.widget.where = val

View File

@ -233,6 +233,7 @@ class Main(MainWindow):
'replace-all', keys=('Ctrl+A'), description=_('Replace all matches')) 'replace-all', keys=('Ctrl+A'), description=_('Replace all matches'))
self.action_count = sreg('count-matches', _('&Count all'), self.action_count = sreg('count-matches', _('&Count all'),
'count', keys=('Ctrl+N'), description=_('Count number of matches')) 'count', keys=('Ctrl+N'), description=_('Count number of matches'))
self.action_mark = reg(None, _('&Mark selected text'), self.boss.mark_selected_text, 'mark-selected-text', ('Ctrl+Shift+M',), _('Mark selected text'))
def create_menubar(self): def create_menubar(self):
b = self.menuBar() b = self.menuBar()
@ -281,6 +282,8 @@ class Main(MainWindow):
a(self.action_replace_all) a(self.action_replace_all)
e.addSeparator() e.addSeparator()
a(self.action_count) a(self.action_count)
e.addSeparator()
a(self.action_mark)
def create_toolbars(self): def create_toolbars(self):
def create(text, name): def create(text, name):