[gedit-latex] Add snippets manager



commit 7c73082ca07ece119d5494c80c667ca589ff9e80
Author: Ignacio Casal Quinteiro <icq gnome org>
Date:   Sun Sep 25 22:19:37 2011 +0200

    Add snippets manager

 latex/Makefile.am       |    2 ++
 latex/latex/views.py    |   32 ++++++++++++++------------------
 latex/resources.py      |   12 +-----------
 latex/singleton.py      |   30 ++++++++++++++++++++++++++++++
 latex/snippetmanager.py |   47 +++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 94 insertions(+), 29 deletions(-)
---
diff --git a/latex/Makefile.am b/latex/Makefile.am
index 043b1cc..41250bc 100644
--- a/latex/Makefile.am
+++ b/latex/Makefile.am
@@ -9,6 +9,8 @@ plugin_PYTHON = \
 	issues.py \
 	outline.py \
 	resources.py \
+	singleton.py \
+	snippetmanager.py \
 	util.py \
 	views.py
 
diff --git a/latex/latex/views.py b/latex/latex/views.py
index 2043e33..106715f 100644
--- a/latex/latex/views.py
+++ b/latex/latex/views.py
@@ -28,12 +28,17 @@ import logging
 import xml.etree.ElementTree as ElementTree
 
 from gi.repository import Gtk, GdkPixbuf
+from os import system
+from os.path import basename
 
 from ..preferences import Preferences
 from ..base import PanelView
+from ..base.file import File
 from ..resources import Resources
-from ..base.templates import Template
+from ..snippetmanager import SnippetManager
 from ..issues import Issue
+from ..outline import OutlineOffsetMap, BaseOutlineView
+from outline import OutlineNode
 from ..gldefs import _
 
 LOG = logging.getLogger(__name__)
@@ -53,12 +58,12 @@ class SymbolCollection(object):
 
 
     class Symbol(object):
-        def __init__(self, template, icon):
+        def __init__(self, snippet, icon):
             """
-            @param template: a Template instance
+            @param snippet: a snippet to insert
             @param icon: an icon filename
             """
-            self.template = template
+            self.snippet = snippet
             self.icon = icon
 
 
@@ -71,7 +76,7 @@ class SymbolCollection(object):
         for group_el in symbols_el.findall("group"):
             group = self.Group(group_el.get("label"))
             for symbol_el in group_el.findall("symbol"):
-                symbol = self.Symbol(Template(symbol_el.text.strip()), Resources().get_icon("%s" % symbol_el.get("icon")))
+                symbol = self.Symbol(symbol_el.text.strip(), Resources().get_icon("%s" % symbol_el.get("icon")))
                 group.symbols.append(symbol)
             self.groups.append(group)
 
@@ -113,11 +118,11 @@ class LaTeXSymbolMapView(PanelView):
             self._add_group(group)
 
     def _add_group(self, group):
-        model = Gtk.ListStore(GdkPixbuf.Pixbuf, str, object)        # icon, tooltip, Template
+        model = Gtk.ListStore(GdkPixbuf.Pixbuf, str)        # icon, snippet
 
         for symbol in group.symbols:
             try:
-                model.append([GdkPixbuf.Pixbuf.new_from_file(symbol.icon), str(symbol.template), symbol.template])
+                model.append([GdkPixbuf.Pixbuf.new_from_file(symbol.icon), symbol.snippet])
             except:
                 LOG.error("Could not add symbol group %s to model" % symbol, exc_info=True)
 
@@ -166,18 +171,12 @@ class LaTeXSymbolMapView(PanelView):
         @param icon_view: the Gtk.IconView
         @param path: the Gtk.TreePath to the item
         """
-        template = icon_view.get_model()[path][2]
-        self._context.active_editor.insert(template)
+        snippet = icon_view.get_model()[path][1]
+        SnippetManager().insert_at_cursor(self._context.active_editor, snippet)
 
     def _on_focus_out_event(self, icon_view, event):
         icon_view.unselect_all()
 
-from os import system
-
-from ..base.file import File
-from ..outline import OutlineOffsetMap, BaseOutlineView
-from outline import OutlineNode
-
 
 class LaTeXOutlineView(BaseOutlineView):
     """
@@ -284,9 +283,6 @@ class LaTeXOutlineView(BaseOutlineView):
         Preferences().set("outline-show-graphics", value)
 
 
-from os.path import basename
-
-
 class OutlineConverter(object):
     """
     This creates a Gtk.TreeStore object from a LaTeX outline model
diff --git a/latex/resources.py b/latex/resources.py
index e373cc8..a973268 100644
--- a/latex/resources.py
+++ b/latex/resources.py
@@ -26,20 +26,10 @@ resources
 import logging
 import os.path
 import errno
+from singleton import Singleton
 
 _log = logging.getLogger("resources")
 
-class Singleton(object):
-    _instance = None
-
-    def __new__(cls, *args, **kwargs):
-        if not cls._instance:
-            cls._instance = super(Singleton, cls).__new__(
-                     cls, *args, **kwargs)
-            cls._instance.__init_once__()
-
-        return cls._instance
-
 class Resources(Singleton):
     def __init_once__(self):
         pass
diff --git a/latex/singleton.py b/latex/singleton.py
new file mode 100644
index 0000000..2c32bf3
--- /dev/null
+++ b/latex/singleton.py
@@ -0,0 +1,30 @@
+# -*- coding: utf-8 -*-
+
+# This file is part of the Gedit LaTeX Plugin
+#
+# Copyright (C) 2011 Ignacio Casal Quinteiro
+#
+# This program is free software; you can redistribute it and/or modify it under
+# the terms of the GNU General Public Licence as published by the Free Software
+# Foundation; either version 2 of the Licence, or (at your option) any later
+# version.
+#
+# This program is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE.  See the GNU General Public Licence for more
+# details.
+#
+# You should have received a copy of the GNU General Public Licence along with
+# this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
+# Street, Fifth Floor, Boston, MA  02110-1301, USA
+
+class Singleton(object):
+    _instance = None
+
+    def __new__(cls, *args, **kwargs):
+        if not cls._instance:
+            cls._instance = super(Singleton, cls).__new__(
+                     cls, *args, **kwargs)
+            cls._instance.__init_once__()
+
+        return cls._instance
diff --git a/latex/snippetmanager.py b/latex/snippetmanager.py
new file mode 100644
index 0000000..d5e5730
--- /dev/null
+++ b/latex/snippetmanager.py
@@ -0,0 +1,47 @@
+# -*- coding: utf-8 -*-
+
+#  Copyright (C) 2011 - Ignacio Casal Quinteiro
+#
+#  This program is free software; you can redistribute it and/or modify
+#  it under the terms of the GNU General Public License as published by
+#  the Free Software Foundation; either version 2 of the License, or
+#  (at your option) any later version.
+#
+#  This program is distributed in the hope that it will be useful,
+#  but WITHOUT ANY WARRANTY; without even the implied warranty of
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#  GNU General Public License for more details.
+#
+#  You should have received a copy of the GNU General Public License
+#  along with this program; if not, write to the Free Software
+#  Foundation, Inc., 59 Temple Place, Suite 330,
+#  Boston, MA 02111-1307, USA.
+
+from gi.repository import Gtk, Gedit
+from singleton import Singleton
+import logging
+
+_log = logging.getLogger("snippet manager")
+
+
+class SnippetManager(Singleton):
+    def __init_once__(self):
+        pass
+
+    def insert(self, editor, iter, text):
+        view = editor.tab_decorator.tab.get_view()
+        window = view.get_toplevel()
+        bus = window.get_message_bus()
+
+        if bus.is_registered('/plugins/snippets', 'parse-and-activate'):
+            # FIXME: we miss the iter
+            bus.send('/plugins/snippets', 'parse-and-activate',
+                     trigger=text, view=view)
+
+    def insert_at_cursor(self, editor, text):
+        buf = editor.tab_decorator.tab.get_document()
+        insert = buf.get_insert()
+        iter = buf.get_iter_at_mark(insert)
+        self.insert(editor, iter, text)
+
+# vi:ex:ts=4:et:



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