Attachment 'latextools-062006.py'
Download 1 # Wow! My first Gedit plugin and serious Python proggie!
2
3 import gedit
4 import gtk
5 import gnomevfs
6 import os
7 import os.path
8 import signal
9
10 import dbus
11 import dbus.service
12 if getattr(dbus, 'version', (0,0,0)) >= (0,41,0):
13 import dbus.glib
14
15
16 BUS_NAME = 'org.gedit.LatexPlugin'
17 OBJECT_PATH = '/org/gedit/LatexPluginObject'
18 DBUS_INCANTATION = 'dbus-send --type=method_call --dest=org.gedit.LatexPlugin /org/gedit/LatexPluginObject org.gedit.LatexPluginIFace.inverse string:%f int32:%l'
19
20
21 class LatexPluginObject(dbus.service.Object):
22 def __init__(self, latexplugin, bus_name, object_path=OBJECT_PATH):
23 dbus.service.Object.__init__(self, bus_name, object_path)
24 self.latexplugin = latexplugin
25
26 # This is for dbus inverse search
27
28 @dbus.service.method('org.gedit.LatexPluginIFace')
29 def inverse(self, fname, line):
30 print "Got inverse: " + fname + " " + str(line)
31 # we should check to see if the right file is loaded and active, and
32 # load it otherwise, but for now we don't. So, yes, this is just a quick hack!
33 self.latexplugin.window.get_active_document().goto_line(line-1) # tex counts from 1
34
35
36 # adding this makes things work---don't know why!
37
38 @dbus.service.method('org.gedit.LatexPluginIFace')
39 def dummy(self):
40 print "Everything seems to be OK..."
41
42
43 class latextools(gedit.Plugin):
44 def __init__(self):
45 gedit.Plugin.__init__(self)
46 self.clean_compile = False
47 self.fdir = ''
48 self.fbase = ''
49 self.window = None
50 self.action_group = None
51 self.merge_id = None
52 # now do dbus stuff
53 session_bus = dbus.SessionBus()
54 bus_name = dbus.service.BusName(BUS_NAME, bus=session_bus)
55 self.lpo = LatexPluginObject(self, bus_name)
56
57 def activate(self, window):
58 self.window = window # save window so we can access everything from the callbacks
59 latexActions = [('latexAction', None, _('LaTeX'), '<Alt><Shift>L', _('Compile LaTeX to DVI'), self.do_latex),
60 ('pdflatexAction', None, _('PDFLaTeX'), '<Alt><Shift>P', _('Compile LaTeX to PDF'), self.do_pdflatex),
61 ('bibtexAction', None, _('BibTeX'), '<Alt><Shift>B', _('Run BibTeX'), self.do_bibtex),
62 ('dviAction', None, _('View DVI'), '<Alt><Shift>D', _('View DVI file'), self.do_dvi),
63 ('forwardAction', None, _('DVI Find'), '<Alt><Shift>F', _('DVI forward search'), self.do_forward),
64 ('pdfAction', None, _('View PDF'), '<Alt><Shift>P', _('View PDF file'), self.do_pdf),
65 ('latexMenuAction', None, _('LaTeX Plugin'), None, _('Compile and Run LaTeX files'), None) ]
66
67 latexUI = """
68 <menubar name="MenuBar">
69 <menu name="ToolsMenu" action="Tools">
70 <placeholder name="ToolsOps_3">
71 <menu name="LaTeX Plugin" action="latexMenuAction">
72 <menuitem action="latexAction"/>
73 <menuitem action="pdflatexAction"/>
74 <menuitem action="bibtexAction"/>
75 <menuitem action="dviAction"/>
76 <menuitem action="forwardAction"/>
77 <menuitem action="pdfAction"/>
78 </menu>
79 </placeholder>
80 </menu>
81 </menubar>
82 """
83
84 manager = window.get_ui_manager()
85 self.merge_id = manager.new_merge_id()
86
87 self.action_group = gtk.ActionGroup("GeditLatexPluginActions")
88 self.action_group.set_translation_domain('gedit')
89
90 self.action_group.add_actions(latexActions)
91 manager.insert_action_group(self.action_group, -1)
92 self.merge_id = manager.add_ui_from_string(latexUI)
93 manager.ensure_update()
94
95 def deactivate(self, window):
96 manager = window.get_ui_manager()
97 manager.remove_ui(self.merge_id)
98 self.action_group = None
99
100 def update_ui(self, window):
101 # messagebox("update_ui called")
102 pass
103
104 # Actual work done here!
105
106 def do_latex(self, action):
107 self.clean_compile = False
108 res = self.run_command("latex", "tex", "-src", "-interaction=nonstopmode")
109 if type(res) is not tuple:
110 return # did not manage to get file names
111 (r, self.fdir, self.fbase) = res
112 if r != 0:
113 messagebox("Errors occurred. In the future, I will load the log file for you", "LaTeX plugin")
114 return
115 else:
116 self.clean_compile = True
117 self.run_xdvi("-unique -s 6 -bg white", self.fdir + os.sep + self.fbase+ ".dvi")
118
119 def do_pdflatex(self, action):
120 pass
121
122 def do_bibtex(self, action):
123 pass
124
125 def do_dvi(self, action):
126 if self.clean_compile:
127 self.run_xdvi('-unique -s 6 -bg white', self.fdir + os.sep + self.fbase+ '.dvi')
128
129
130 def do_forward(self, action):
131 if self.clean_compile:
132 d = self.window.get_active_document()
133 line = d.get_iter_at_mark(d.get_insert()).get_line() + 1 # tex counts from 1
134 self.run_xdvi('', '-sourceposition ' + str(line) + self.fdir + os.sep + self.fbase)
135
136 def do_pdf(self, action):
137 pass
138
139
140 # Utility functions
141
142 def run_command(self, cmd, extension, *args):
143 currdir = os.getcwd()
144 uri = self.window.get_active_document().get_uri()
145 if uri == None:
146 messagebox("No file name yet! Remember to save!", "LaTeX plugin")
147 return -998
148 scheme = gnomevfs.get_uri_scheme(uri)
149 if scheme != 'file':
150 messagebox("Remote files currently not supported", "LaTeX plugin")
151 return -999
152 f = gnomevfs.get_local_path_from_uri(uri)
153 (fdir, fname) = os.path.split(f)
154 os.chdir(fdir)
155 (fbase, fext) = os.path.splitext(fname)
156 # Note: (1) must repeat cmd as it's copied to argv[0]
157 # (2) does not work on Windows (use spawnl...)
158 newargs = list(args)
159 newargs.append(fbase+"."+extension)
160 newargs.insert(0,cmd)
161 r = os.spawnlp(os.P_WAIT, cmd, *tuple(newargs))
162 os.chdir(currdir)
163 return (r, fdir, fbase)
164
165 def run_xdvi(self, pre, post):
166 # invoke: xdvi <pre> <dbus stuff> <post>
167 # also we don't chdir
168 tcommand = ['xdvi']
169 if pre:
170 tcommand.extend(pre.split(" "))
171 tcommand.append('-editor')
172 tcommand.append(DBUS_INCANTATION)
173 if post:
174 tcommand.extend(post.split(" "))
175 print tcommand
176 r = os.spawnlp(os.P_WAIT, tcommand[0], *tcommand)
177 return r
178
179 # Utility function to put up a message box
180
181 def messagebox(text="Message box text", title="latexbox plugin"):
182 d = gtk.Dialog(title, flags=gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, buttons=(gtk.STOCK_OK, gtk.RESPONSE_OK))
183 l = gtk.Label(text)
184 d.vbox.pack_start(l,True,True,0)
185 l.show()
186 d.connect("response", lambda dlg,data : dlg.destroy())
187 d.run()
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.