[gedit-plugins] joinlines: move menu items to context menu



commit b073cd01ce77d9e010dff8619118f56bf22d9cb1
Author: Ignacio Casal Quinteiro <icq gnome org>
Date:   Sat Jan 11 15:37:02 2014 +0100

    joinlines: move menu items to context menu

 plugins/joinlines/joinlines.py |  286 ++++++++++++++++++++++------------------
 1 files changed, 157 insertions(+), 129 deletions(-)
---
diff --git a/plugins/joinlines/joinlines.py b/plugins/joinlines/joinlines.py
index 60d51c7..c41e5ec 100644
--- a/plugins/joinlines/joinlines.py
+++ b/plugins/joinlines/joinlines.py
@@ -52,10 +52,17 @@ class JoinLinesWindowActivatable(GObject.Object, Gedit.WindowActivatable):
         GObject.Object.__init__(self)
 
     def do_activate(self):
-        self._insert_menu()
+        action = Gio.SimpleAction(name="joinlines")
+        action.connect('activate', lambda a, p: self.join_lines())
+        self.window.add_action(action)
+
+        action = Gio.SimpleAction(name="splitlines")
+        action.connect('activate', lambda a, p: self.split_lines())
+        self.window.add_action(action)
 
     def do_deactivate(self):
-        self._remove_menu()
+        self.window.remove_action("joinlines")
+        self.window.remove_action("splitlines")
 
     def do_update_state(self):
         view = self.window.get_active_view()
@@ -64,141 +71,162 @@ class JoinLinesWindowActivatable(GObject.Object, Gedit.WindowActivatable):
         self.window.lookup_action("splitlines").set_enabled(view is not None and \
                                                             view.get_editable())
 
-    def _remove_menu(self):
-        self.window.remove_action("joinlines")
-        self.window.remove_action("splitlines")
-
-    def _insert_menu(self):
-        action = Gio.SimpleAction(name="joinlines")
-        action.connect('activate', lambda a, p: join_lines(self.window))
-        self.window.add_action(action)
-
-        action = Gio.SimpleAction(name="splitlines")
-        action.connect('activate', lambda a, p: split_lines(self.window))
-        self.window.add_action(action)
-
-        self.menu = self.extend_gear_menu("ext9")
-
-        item = Gio.MenuItem.new(_('_Split Lines'), "win.splitlines")
-        self.menu.prepend_menu_item(item)
-
-        item = Gio.MenuItem.new(_("_Join Lines"), "win.joinlines")
-        self.menu.prepend_menu_item(item)
-
-def join_lines(window):
-    doc = window.get_active_document()
-    if doc is None:
-        return
-
-    doc.begin_user_action()
-
-    # If there is a selection use it, otherwise join the
-    # next line
-    try:
-        start, end = doc.get_selection_bounds()
-    except ValueError:
-        start = doc.get_iter_at_mark(doc.get_insert())
-        end = start.copy()
-        end.forward_line()
-
-    end_mark = doc.create_mark(None, end)
-
-    if not start.ends_line():
-        start.forward_to_line_end()
-
-    # Include trailing spaces in the chunk to be removed
-    while start.backward_char() and start.get_char() in ('\t', ' '):
-        pass
-    start.forward_char()
-
-    while doc.get_iter_at_mark(end_mark).compare(start) == 1:
-        end = start.copy()
-        while end.get_char() in ('\r', '\n', ' ', '\t'):
-            end.forward_char()
-        doc.delete(start, end)
-
-        doc.insert(start, ' ')
-        start.forward_to_line_end()
-
-    doc.delete_mark(end_mark)
-    doc.end_user_action()
+    def join_lines(self):
+        view = self.window.get_active_view()
+        if view and hasattr(view, "join_lines_view_activatable"):
+            view.join_lines_view_activatable.join_lines()
 
-def split_lines(window):
-    view = window.get_active_view()
-    if view is None:
-        return
+    def split_lines(self):
+        view = self.window.get_active_view()
+        if view and hasattr(view, "join_lines_view_activatable"):
+            view.join_lines_view_activatable.split_lines()
 
-    doc = view.get_buffer()
+class JoinLinesViewActivatable(GObject.Object, Gedit.ViewActivatable):
 
-    width = view.get_right_margin_position()
-    tabwidth = view.get_tab_width()
+    view = GObject.property(type=Gedit.View)
 
-    doc.begin_user_action()
+    def __init__(self):
+        GObject.Object.__init__(self)
 
-    try:
-        # get selection bounds
-        start, end = doc.get_selection_bounds()
+    def do_activate(self):
+        self.view.join_lines_view_activatable = self
+        self.view.connect('populate-popup', self.populate_popup)
 
-        # measure indent until selection start
-        indent_iter = start.copy()
-        indent_iter.set_line_offset(0)
-        indent = ''
-        while indent_iter.get_offset() != start.get_offset():
-            if indent_iter.get_char() == '\t':
-                indent = indent + '\t'
-            else:
-                indent = indent + ' '
-            indent_iter.forward_char()
-    except ValueError:
-        # select from start to line end
-        start = doc.get_iter_at_mark(doc.get_insert())
-        start.set_line_offset(0)
-        end = start.copy()
-        if not end.ends_line():
-            end.forward_to_line_end()
-
-        # measure indent of line
-        indent_iter = start.copy()
-        indent = ''
-        while indent_iter.get_char() in (' ', '\t'):
-            indent = indent + indent_iter.get_char()
-            indent_iter.forward_char()
-
-    end_mark = doc.create_mark(None, end)
-
-    # ignore first word
-    previous_word_end = start.copy()
-    forward_to_word_start(previous_word_end)
-    forward_to_word_end(previous_word_end)
-
-    while 1:
-        current_word_start = previous_word_end.copy()
-        forward_to_word_start(current_word_start)
-
-        current_word_end = current_word_start.copy()
-        forward_to_word_end(current_word_end)
-
-        if ord(current_word_end.get_char()) and \
-           doc.get_iter_at_mark(end_mark).compare(current_word_end) >= 0:
-
-            word_length = current_word_end.get_offset() - \
-                          current_word_start.get_offset()
-
-            doc.delete(previous_word_end, current_word_start)
-
-            line_offset = get_line_offset(current_word_start, tabwidth) + word_length
-            if line_offset > width - 1:
-                doc.insert(current_word_start, '\n' + indent)
+    def do_deactivate(self):
+        delattr(self.view, "join_lines_view_activatable")
+
+    def populate_popup(self, view, popup):
+        if not isinstance(popup, Gtk.MenuShell):
+            return
+
+        item = Gtk.SeparatorMenuItem()
+        item.show()
+        popup.append(item)
+
+        item = Gtk.MenuItem.new_with_mnemonic(_("_Join Lines"))
+        item.set_sensitive(self.view.get_editable())
+        item.show()
+        item.connect('activate', lambda i: self.join_lines())
+        popup.append(item)
+
+        item = Gtk.MenuItem.new_with_mnemonic(_('_Split Lines'))
+        item.set_sensitive(self.view.get_editable())
+        item.show()
+        item.connect('activate', lambda i: self.split_lines())
+        popup.append(item)
+
+    def join_lines(self):
+        doc = self.view.get_buffer()
+        if doc is None:
+            return
+
+        doc.begin_user_action()
+
+        # If there is a selection use it, otherwise join the
+        # next line
+        try:
+            start, end = doc.get_selection_bounds()
+        except ValueError:
+            start = doc.get_iter_at_mark(doc.get_insert())
+            end = start.copy()
+            end.forward_line()
+
+        end_mark = doc.create_mark(None, end)
+
+        if not start.ends_line():
+            start.forward_to_line_end()
+
+        # Include trailing spaces in the chunk to be removed
+        while start.backward_char() and start.get_char() in ('\t', ' '):
+            pass
+        start.forward_char()
+
+        while doc.get_iter_at_mark(end_mark).compare(start) == 1:
+            end = start.copy()
+            while end.get_char() in ('\r', '\n', ' ', '\t'):
+                end.forward_char()
+            doc.delete(start, end)
+
+            doc.insert(start, ' ')
+            start.forward_to_line_end()
+
+        doc.delete_mark(end_mark)
+        doc.end_user_action()
+
+    def split_lines(self):
+        doc = self.view.get_buffer()
+        if doc is None:
+            return
+
+        width = self.view.get_right_margin_position()
+        tabwidth = self.view.get_tab_width()
+
+        doc.begin_user_action()
+
+        try:
+            # get selection bounds
+            start, end = doc.get_selection_bounds()
+
+            # measure indent until selection start
+            indent_iter = start.copy()
+            indent_iter.set_line_offset(0)
+            indent = ''
+            while indent_iter.get_offset() != start.get_offset():
+                if indent_iter.get_char() == '\t':
+                    indent = indent + '\t'
+                else:
+                    indent = indent + ' '
+                indent_iter.forward_char()
+        except ValueError:
+            # select from start to line end
+            start = doc.get_iter_at_mark(doc.get_insert())
+            start.set_line_offset(0)
+            end = start.copy()
+            if not end.ends_line():
+                end.forward_to_line_end()
+
+            # measure indent of line
+            indent_iter = start.copy()
+            indent = ''
+            while indent_iter.get_char() in (' ', '\t'):
+                indent = indent + indent_iter.get_char()
+                indent_iter.forward_char()
+
+        end_mark = doc.create_mark(None, end)
+
+        # ignore first word
+        previous_word_end = start.copy()
+        forward_to_word_start(previous_word_end)
+        forward_to_word_end(previous_word_end)
+
+        while 1:
+            current_word_start = previous_word_end.copy()
+            forward_to_word_start(current_word_start)
+
+            current_word_end = current_word_start.copy()
+            forward_to_word_end(current_word_end)
+
+            if ord(current_word_end.get_char()) and \
+               doc.get_iter_at_mark(end_mark).compare(current_word_end) >= 0:
+
+                word_length = current_word_end.get_offset() - \
+                              current_word_start.get_offset()
+
+                doc.delete(previous_word_end, current_word_start)
+
+                line_offset = get_line_offset(current_word_start, tabwidth) + word_length
+                if line_offset > width - 1:
+                    doc.insert(current_word_start, '\n' + indent)
+                else:
+                    doc.insert(current_word_start, ' ')
+
+                previous_word_end = current_word_start.copy()
+                previous_word_end.forward_chars(word_length)
             else:
-                doc.insert(current_word_start, ' ')
-
-            previous_word_end = current_word_start.copy()
-            previous_word_end.forward_chars(word_length)
-        else:
-            break
+                break
 
-    doc.delete_mark(end_mark)
-    doc.end_user_action()
+        doc.delete_mark(end_mark)
+        doc.end_user_action()
 
 def get_line_offset(text_iter, tabwidth):
     offset_iter = text_iter.copy()


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]