Release coccinelle-0.1
[bpt/coccinelle.git] / python / coccilib / coccigui / vimembed.py
1 # -*- coding: utf-8 -*-
2
3 # vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:
4 #Copyright (c) 2005-2006 The PIDA Project
5
6 #Permission is hereby granted, free of charge, to any person obtaining a copy
7 #of this software and associated documentation files (the "Software"), to deal
8 #in the Software without restriction, including without limitation the rights
9 #to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 #copies of the Software, and to permit persons to whom the Software is
11 #furnished to do so, subject to the following conditions:
12
13 #The above copyright notice and this permission notice shall be included in
14 #all copies or substantial portions of the Software.
15
16 #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 #IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 #FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 #AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 #LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 #OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 #SOFTWARE.
23
24
25 ''' A library to embed vim in a gtk socket '''
26
27
28 import gtk
29 import os
30 import time
31
32
33 import subprocess
34
35 class vim_embed(object):
36
37 HAS_CONTROL_BOX = False
38
39 HAS_TITLE = False
40
41 def init(self, command='gvim', args=[]):
42 self.__servername = self.__generate_servername()
43 self.pid = None
44 self.args = args
45 self.r_cb_plugged = None
46 self.r_cb_unplugged = None
47 self.__eb = None
48
49 def __pack(self):
50 socket = gtk.Socket()
51 eb = gtk.EventBox()
52 self.widget.pack_start(eb)
53 eb.add_events(gtk.gdk.KEY_PRESS_MASK)
54 eb.add(socket)
55 self.show_all()
56 self.__eb = eb
57 return socket.get_id()
58
59 def __generate_servername(self):
60 return 'PIDA_EMBEDDED_%s' % time.time()
61
62 def get_servername(self):
63 return self.__servername
64 servername = property(get_servername)
65
66 def should_remove(self):
67 self.service.remove_attempt()
68 return False
69
70 def run(self, command):
71 self.command = command
72 xid = self.__pack()
73 args = self.args[:] # a copy
74 args.extend(['--socketid', '%s' % xid])
75 if not xid:
76 return
77 if not self.pid:
78 popen = subprocess.Popen([self.command, '--servername',
79 self.servername, '--cmd',
80 'let PIDA_EMBEDDED=1'] + args,
81 close_fds=True)
82 self.pid = popen.pid
83 self.show_all()
84
85 def grab_input_focus(self):
86 self.__eb.child_focus(gtk.DIR_TAB_FORWARD)
87
88 class VimEmbedWidget(gtk.EventBox):
89
90 def __init__(self, command, script_path, args=[]):
91 gtk.EventBox.__init__(self)
92 self._servername = self._generate_servername()
93 self._command = command
94 self._init_script = script_path
95 self.pid = None
96 self.args = args
97 self.r_cb_plugged = None
98 self.r_cb_unplugged = None
99 self.__eb = None
100
101 def _create_ui(self):
102 socket = gtk.Socket()
103 self.add_events(gtk.gdk.KEY_PRESS_MASK)
104 self.add(socket)
105 self.show_all()
106 return socket.get_id()
107
108 def _generate_servername(self):
109 return 'PIDA_EMBEDDED_%s' % time.time()
110
111 def get_server_name(self):
112 return self._servername
113
114 def should_remove(self):
115 self.service.remove_attempt()
116 return False
117
118 def run(self):
119 xid = self._create_ui()
120 args = self.args[:] # a copy
121 args.extend(['--socketid', '%s' % xid])
122 if not xid:
123 return
124 if not self.pid:
125 try:
126 popen = subprocess.Popen(
127 [self._command,
128 '--servername', self.get_server_name(),
129 '--cmd', 'let PIDA_EMBEDDED=1',
130 '-c', 'so %s' % self._init_script
131 ] + args,
132 close_fds=True
133 )
134 self.pid = popen.pid
135 except OSError:
136 return False
137 self.show_all()
138 return True
139
140 def grab_input_focus(self):
141 self.child_focus(gtk.DIR_TAB_FORWARD)
142