Package: gedit-latex-plugin / 3.8.0-2

0003-Support-gedit-3.14.patch Patch series | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
From: Pietro Battiston <me@pietrobattiston.it>
Date: Sat, 22 Nov 2014 16:55:08 +0100
Subject: Support gedit 3.14

Support for gedit > 3.8 was recently introduced in upstream git
(but still not released).
This patch introduces the minimum set of changes needed.
It basically merges the following upstream commits:
- c4e3a6596c68c5746b7f4af8a2bdfbb3f2741d9c
- 019725bea02151a833835a9aba8c82a55cea57f8
- c3a70ee7f0a304c74eac8c30f3b7cea4691b7b6f
- 7d4a482091db72bdda9e70e70d6802f8e3dda6f8
- 8e9970be499fe2345bdf1408bb84ff0428a07078
- 58497c76f44cc169129f23d5b405981b2a761179
- cbf93251c8ebeeb88b4e9dc9acd225a4f75001c5
and removes some irrelevant edits.
---
 data/ui/ui-toolbar-normal.builder |  2 +-
 latex/action.py                   |  8 +++-
 latex/appactivatable.py           | 78 ++++++++++++++++++++++++++++++++++++---
 latex/config.py                   |  7 +++-
 latex/latex/actions.py            |  8 ++++
 latex/tabdecorator.py             |  4 +-
 latex/tools/__init__.py           |  2 +-
 latex/windowactivatable.py        | 41 +++++++++++++-------
 po/POTFILES.in                    |  1 +
 9 files changed, 125 insertions(+), 26 deletions(-)

diff --git a/data/ui/ui-toolbar-normal.builder b/data/ui/ui-toolbar-normal.builder
index f5361dd..c2c5090 100644
--- a/data/ui/ui-toolbar-normal.builder
+++ b/data/ui/ui-toolbar-normal.builder
@@ -1,6 +1,6 @@
 <ui>
     <menubar name="MenuBar">
-        <menu name="FileMenu" action="File">
+        <menu name="FileMenu" action="FileDummyAction">
             <placeholder name="FileOps_1">
                 <menuitem action="LaTeXNewAction" />
             </placeholder>
diff --git a/latex/action.py b/latex/action.py
index 25113df..6fe3706 100644
--- a/latex/action.py
+++ b/latex/action.py
@@ -19,7 +19,7 @@
 # Street, Fifth Floor, Boston, MA  02110-1301, USA
 
 from uuid import uuid1
-from gi.repository import Gtk, GdkPixbuf
+from gi.repository import Gtk, GdkPixbuf, Gio
 
 from .file import File
 from .resources import Resources
@@ -60,6 +60,12 @@ class Action(object):
         self._handler = self._internal_action.connect("activate", lambda gtk_action, action: action.activate(window_context), self)
         action_group.add_action_with_accel(self._internal_action, self.accelerator)
 
+    # Hooks a Gio.SimpleAction to a given window.
+    def simplehook(self, window, window_context):
+        self._simple_internal_action = Gio.SimpleAction(name=self.__class__.__name__)
+        self._simplehandler = self._simple_internal_action.connect("activate", lambda action, param: self.activate(window_context))
+        window.add_action(self._simple_internal_action)
+
     @property
     def label(self):
         raise NotImplementedError
diff --git a/latex/appactivatable.py b/latex/appactivatable.py
index 57b4cd9..d6225d8 100644
--- a/latex/appactivatable.py
+++ b/latex/appactivatable.py
@@ -18,9 +18,12 @@
 import os.path
 import platform
 
-from gi.repository import GLib, Gedit, GObject
+from gi.repository import GLib, Gedit, GObject, Gio, Gtk
 from .resources import Resources
-from .preferences import Preferences
+
+from .config import MENUACTIONS
+
+from .preferences.tools import ToolPreferences
 
 class LaTeXAppActivatable(GObject.Object, Gedit.AppActivatable):
     __gtype_name__ = "GeditLaTeXAppActivatable"
@@ -44,9 +47,72 @@ class LaTeXAppActivatable(GObject.Object, Gedit.AppActivatable):
             sysdir = self.plugin_info.get_data_dir()
 
         Resources().set_dirs(userdir, sysdir)
-        ##FIXME: temporary set systemdir also for TEMPLATE_DIR, if exists
-        ## please find a better way to do so.
-        if os.path.exists(os.path.join(sysdir,"templates")):
-            Preferences().TEMPLATE_DIR = os.path.join(sysdir,"templates")
+        
+        #The following is needed to support gedit 3.12 new menu api.
+        #It adds menus and shortcuts here.
+        #Actions and toolbar construction are still done in windowactivatable.py.
+        
+        self._tool_preferences = ToolPreferences()
+        self._tool_preferences.connect("tools-changed", self._on_tools_changed)
+        
+        self.add_latex_menu()
+        self.add_latex_tools_menu()
+        self.init_tools()
+        
+    def add_latex_menu(self):
+        self.menu_ext = self.extend_menu("tools-section")
+        menu = Gio.MenuItem.new(_("LaTeX"))
+        container = Gio.Menu.new()
+        menu.set_submenu(container)
+        self.menu_ext.append_menu_item(menu)
+        
+        self._icon_factory = Gtk.IconFactory()
+        self._icon_factory.add_default()
+        
+        for clazz in MENUACTIONS:
+            action = clazz(icon_factory=self._icon_factory)
+            actionlink = "win." + clazz.__name__
+            container.append_item(Gio.MenuItem.new(_(action.label), actionlink))
+            if action.accelerator is not None:
+                self.app.add_accelerator(action.accelerator, actionlink, None)
+                
+    def add_latex_tools_menu(self):
+        menu = Gio.MenuItem.new(_("LaTeX Tools"))
+        container = Gio.Menu.new()
+        menu.set_submenu(container)
+        self.latex_tools_menu = container
+        self.menu_ext.append_menu_item(menu)
+    
+    def init_tools(self):
+      
+        #the following is copied from windowactivatable.py and modified as necessary
+        
+        i = 1                    # counting tool actions
+        accel_counter = 1        # counting tool actions without custom accel
+        for tool in self._tool_preferences.tools:
+            # hopefully unique action name
+            name = "Tool%sAction" % i
+
+            actionlink = "win." + name
+            self.latex_tools_menu.append_item(Gio.MenuItem.new(_(tool.label), actionlink))
+
+            accelerator = None
+            if tool.accelerator and len(tool.accelerator) > 0:
+                key,mods = Gtk.accelerator_parse(tool.accelerator)
+                if Gtk.accelerator_valid(key,mods):
+                    accelerator = tool.accelerator
+            if not accelerator:
+                accelerator = "<Ctrl><Alt>%s" % accel_counter
+                accel_counter += 1
+            self.app.add_accelerator(accelerator, actionlink, None)
+            i += 1
+            
+    def _on_tools_changed(self, tools):
+        self.latex_tools_menu.remove_all()
+        self.init_tools()
 
+    def do_deactivate(self):
+        del self.latex_tools_menu
+        del self.menu_ext
+        
 # ex:ts=4:et
diff --git a/latex/config.py b/latex/config.py
index 02cd041..001649e 100644
--- a/latex/config.py
+++ b/latex/config.py
@@ -34,7 +34,7 @@ from .latex.actions import LaTeXMenuAction, LaTeXNewAction, LaTeXChooseMasterAct
         LaTeXEquationAction, LaTeXUnEqnArrayAction, LaTeXEqnArrayAction, LaTeXUnderlineAction, LaTeXSmallCapitalsAction, \
         LaTeXRomanAction, LaTeXSansSerifAction, LaTeXTypewriterAction, LaTeXCloseEnvironmentAction, LaTeXBlackboardBoldAction, \
         LaTeXCaligraphyAction, LaTeXFrakturAction, LaTeXBuildImageAction, \
-        LaTeXBuildAction, LaTeXBuildMenuAction
+        LaTeXBuildAction, LaTeXBuildMenuAction, FileDummyAction, ToolsDummyAction
 
 from .bibtex.actions import BibTeXMenuAction, BibTeXNewEntryAction
 
@@ -50,7 +50,10 @@ ACTIONS = [LaTeXMenuAction, LaTeXNewAction, LaTeXChooseMasterAction,
         LaTeXRomanAction, LaTeXSansSerifAction, LaTeXTypewriterAction, LaTeXCloseEnvironmentAction, LaTeXBlackboardBoldAction,
         LaTeXCaligraphyAction, LaTeXFrakturAction, LaTeXBuildImageAction,
         LaTeXBuildAction, LaTeXBuildMenuAction,
-        BibTeXMenuAction, BibTeXNewEntryAction]
+        BibTeXMenuAction, BibTeXNewEntryAction,
+        FileDummyAction, ToolsDummyAction]
+
+MENUACTIONS = [LaTeXNewAction, LaTeXChooseMasterAction, LaTeXCloseEnvironmentAction, BibTeXNewEntryAction] 
 
 # views
 from .views import IssueView
diff --git a/latex/latex/actions.py b/latex/latex/actions.py
index 6094a3f..f404cf3 100644
--- a/latex/latex/actions.py
+++ b/latex/latex/actions.py
@@ -521,4 +521,12 @@ class LaTeXEqnArrayAction(LaTeXTemplateAction):
     $0
 \\end{align}"""
 
+class FileDummyAction(Action):
+    label = tooltip = stock_id = ''
+    accelerator = None
+
+class ToolsDummyAction(Action):
+    label = tooltip = stock_id = ''
+    accelerator = None
+
 # ex:ts=4:et:
diff --git a/latex/tabdecorator.py b/latex/tabdecorator.py
index 9d48dd4..425f387 100644
--- a/latex/tabdecorator.py
+++ b/latex/tabdecorator.py
@@ -78,7 +78,7 @@ class GeditTabDecorator(object):
     def tab(self):
         return self._tab
 
-    def _on_load(self, document, param):
+    def _on_load(self, document):
         """
         A file has been loaded
         """
@@ -86,7 +86,7 @@ class GeditTabDecorator(object):
 
         self._adjust_editor()
 
-    def _on_save(self, document, param):
+    def _on_save(self, document):
         """
         The file has been saved
         """
diff --git a/latex/tools/__init__.py b/latex/tools/__init__.py
index 389c449..360283d 100644
--- a/latex/tools/__init__.py
+++ b/latex/tools/__init__.py
@@ -127,7 +127,7 @@ class ToolAction(Action):
         else:
             LOG.error("tool activate: no active editor")
 
-    def run_tool(self, document, other, context, doc):
+    def run_tool(self, document, context, doc):
         tool_view = context.find_view(context.active_editor, "ToolView")
 
         self._runner.run(context.active_editor.file, self._tool, tool_view)
diff --git a/latex/windowactivatable.py b/latex/windowactivatable.py
index 17ef0c9..2f99306 100644
--- a/latex/windowactivatable.py
+++ b/latex/windowactivatable.py
@@ -24,6 +24,7 @@ This is searched by gedit for a class extending gedit.Plugin
 
 import logging
 import string
+from traceback import print_exc
 
 from gi.repository import Gedit, GObject, Gio, Gtk, PeasGtk
 
@@ -54,7 +55,7 @@ class LaTeXWindowActivatable(GObject.Object, Gedit.WindowActivatable, PeasGtk.Co
     # ui definition template for hooking tools in Gedit's ui.
     _tool_ui_template = string.Template("""<ui>
             <menubar name="MenuBar">
-                <menu name="ToolsMenu" action="Tools">
+                <menu name="ToolsMenu" action="ToolsDummyAction">
                     <placeholder name="ToolsOps_1">$items</placeholder>
                 </menu>
             </menubar>
@@ -89,9 +90,6 @@ class LaTeXWindowActivatable(GObject.Object, Gedit.WindowActivatable, PeasGtk.Co
         self._init_views()
         self._init_tab_decorators()
 
-        # FIXME: find another way to save a document
-        self._save_action = self._ui_manager.get_action("/MenuBar/FileMenu/FileSaveMenu")
-
         #
         # listen to tab signals
         #
@@ -161,7 +159,7 @@ class LaTeXWindowActivatable(GObject.Object, Gedit.WindowActivatable, PeasGtk.Co
         """
         Merge the plugin's UI definition with the one of Gedit and hook the actions
         """
-        self._ui_manager = self.window.get_ui_manager()
+        self._ui_manager = Gtk.UIManager()
         self._action_group = Gtk.ActionGroup("LaTeXWindowActivatableActions")
         self._icon_factory = Gtk.IconFactory()
         self._icon_factory.add_default()
@@ -175,6 +173,7 @@ class LaTeXWindowActivatable(GObject.Object, Gedit.WindowActivatable, PeasGtk.Co
         for clazz in ACTIONS:
             action = clazz(icon_factory=self._icon_factory)
             action.hook(self._action_group, self._window_context)
+            action.simplehook(self.window, self._window_context)
 
             self._action_objects[clazz.__name__] = action
 
@@ -184,8 +183,10 @@ class LaTeXWindowActivatable(GObject.Object, Gedit.WindowActivatable, PeasGtk.Co
                 else:
                     self._action_extensions[extension] = [clazz.__name__]
 
-        toolbar_mode = self._preferences.get("toolbar-mode")
-
+        #toolbar_mode = self._preferences.get("toolbar-mode")
+        # force normal mode
+        toolbar_mode = "normal"
+        
         # merge ui
         self._ui_manager.insert_action_group(self._action_group, -1)
         self._ui_id = self._ui_manager.add_ui_from_file(Resources().get_ui_file("ui-toolbar-%s.builder" % toolbar_mode))
@@ -195,9 +196,17 @@ class LaTeXWindowActivatable(GObject.Object, Gedit.WindowActivatable, PeasGtk.Co
             self._toolbar_name = "LaTeXToolbar"
             self._toolbar.set_style(Gtk.ToolbarStyle.BOTH_HORIZ)
             # FIXME: Adding a new toolbar to gedit is not really public API
-            self._main_box = self.window.get_children()[0]
-            self._main_box.pack_start(self._toolbar, False, True, 0)
-            self._main_box.reorder_child(self._toolbar, 2)
+            try:
+                # We assume the spot below exists
+                self._main_box = self.window.get_children()[0].get_children()[0].get_children()[0].get_children()[1]
+                self._main_box.pack_start(self._toolbar, False, True, 0)
+                self._main_box.reorder_child(self._toolbar, 1)
+            except (IndexError, AttributeError, TypeError) as e:
+                print_exc()
+                print()
+                print("Could not find place for the toolbar. Disabling toolbar.")
+                self_toolbar = None
+                self._toolbar_name = ""
         elif toolbar_mode == "combined":
             self._toolbar = None
             self._toolbar_name = "ToolBar"
@@ -258,6 +267,11 @@ class LaTeXWindowActivatable(GObject.Object, Gedit.WindowActivatable, PeasGtk.Co
             action = ToolAction(tool)
             gtk_action = Gtk.Action(name, action.label, action.tooltip, action.stock_id)
             self._action_handlers[gtk_action] = gtk_action.connect("activate", lambda gtk_action, action: action.activate(self._window_context), action)
+            
+            # create simple actions to be used by menu (created in appactivatable.py)
+            simpleaction = Gio.SimpleAction(name=name)
+            simpleaction.connect("activate", lambda _a, _b, action: action.activate(self._window_context), action)
+            self.window.add_action(simpleaction)
 
             accelerator = None
             if tool.accelerator and len(tool.accelerator) > 0:
@@ -287,7 +301,8 @@ class LaTeXWindowActivatable(GObject.Object, Gedit.WindowActivatable, PeasGtk.Co
 
         (used by ToolAction before tool run)
         """
-        self._save_action.activate()
+        tab = self._active_tab_decorator.tab
+        Gedit.commands_save_document(tab.get_toplevel(), tab.get_document())
 
     def show_toolbar(self):
         if self._toolbar:
@@ -449,7 +464,7 @@ class LaTeXWindowActivatable(GObject.Object, Gedit.WindowActivatable, PeasGtk.Co
             if view in self._side_views:
                 view.show()
             else:
-                self.window.get_side_panel().add_item(view, "after_side_view_id" + str(i), view.get_label(), view.get_icon())
+                self.window.get_side_panel().add_titled(view, "after_side_view_id" + str(i), view.get_label())
                 self._side_views.append(view)
                 i += 1
 
@@ -458,7 +473,7 @@ class LaTeXWindowActivatable(GObject.Object, Gedit.WindowActivatable, PeasGtk.Co
             if view in self._bottom_views:
                 view.show()
             else:
-                self.window.get_bottom_panel().add_item(view, "bottom_view_id" + str(i), view.get_label(), view.get_icon())
+                self.window.get_bottom_panel().add_titled(view, "bottom_view_id" + str(i), view.get_label())
                 self._bottom_views.append(view)
                 i += 1
 
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 1bfa2af..7b8c95b 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -19,3 +19,4 @@ latex/latex/views.py
 latex/outline.py
 latex/tools/views.py
 latex/views.py
+latex/appactivatable.py