From 8ff5c3c61e52e2f91b83152e84ee10f437dd4a81 Mon Sep 17 00:00:00 2001 From: wurstsalat Date: Sat, 12 Sep 2020 13:21:56 +0200 Subject: [PATCH] [syntax_highlight] Add Paste as Code (Block) feature --- syntax_highlight/chat_syntax_highlighter.py | 41 ++++++++++++++++++++- syntax_highlight/syntax_highlight.py | 2 +- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/syntax_highlight/chat_syntax_highlighter.py b/syntax_highlight/chat_syntax_highlighter.py index a5ebb11..f32541a 100644 --- a/syntax_highlight/chat_syntax_highlighter.py +++ b/syntax_highlight/chat_syntax_highlighter.py @@ -5,6 +5,9 @@ from pygments.token import Comment from pygments.styles import get_style_by_name from gi.repository import Gtk +from gi.repository import Gdk + +from gajim.plugins.plugins_i18n import _ from syntax_highlight.gtkformatter import GTKFormatter from syntax_highlight.types import MatchType @@ -16,14 +19,48 @@ log = logging.getLogger('gajim.p.syntax_highlight') class ChatSyntaxHighlighter: - def __init__(self, plugin_config, highlighter_config, textview): - self.textview = textview + def __init__(self, plugin_config, highlighter_config, chat_control): + self.textview = chat_control.conv_textview self._plugin_config = plugin_config self._highlighter_config = highlighter_config + self._chat_control = chat_control + self._chat_control.msg_textview.connect( + 'populate-popup', self._on_msg_textview_populate_popup) def update_config(self, plugin_config): self._plugin_config = plugin_config + def _on_msg_textview_populate_popup(self, _textview, menu): + item = Gtk.MenuItem.new_with_mnemonic(_('_Paste as Code')) + menu.append(item) + id_ = item.connect('activate', self._paste_as_code) + self._chat_control.handlers[id_] = item + + item = Gtk.MenuItem.new_with_mnemonic(_('Paste as Code _Block')) + menu.append(item) + id_ = item.connect('activate', self._paste_as_code_block) + self._chat_control.handlers[id_] = item + + menu.show_all() + + @staticmethod + def _get_clipboard_text(): + clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD) + return clipboard.wait_for_text() + + def _insert_paste(self, text): + self._chat_control.msg_textview.remove_placeholder() + message_buffer = self._chat_control.msg_textview.get_buffer() + message_buffer.insert_at_cursor(text) + + def _paste_as_code(self, _item): + text = self._get_clipboard_text() + self._insert_paste(f'`{text}`') + + def _paste_as_code_block(self, _item): + text = self._get_clipboard_text() + self._insert_paste(f'```\n{text}\n```') + @staticmethod def _hide_code_markup(buf, start, end): tag = buf.get_tag_table().lookup('hide_code_markup') diff --git a/syntax_highlight/syntax_highlight.py b/syntax_highlight/syntax_highlight.py index 00190c8..8eb7060 100644 --- a/syntax_highlight/syntax_highlight.py +++ b/syntax_highlight/syntax_highlight.py @@ -68,7 +68,7 @@ class SyntaxHighlighterPlugin(GajimPlugin): def _connect_chat_control(self, chat_control): highlighter = ChatSyntaxHighlighter( - self.config, self.highlighter_config, chat_control.conv_textview) + self.config, self.highlighter_config, chat_control) self._highlighters[chat_control.control_id] = highlighter def _disconnect_chat_control(self, chat_control):