~ubuntu-branches/ubuntu/saucy/pida/saucy

« back to all changes in this revision

Viewing changes to src/plugins/vim/plugin.py

  • Committer: Bazaar Package Importer
  • Author(s): Barry deFreese
  • Date: 2006-08-01 13:08:56 UTC
  • mfrom: (0.1.2 etch) (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060801130856-v92ktopgdxc8rv7q
Tags: 0.3.1-2ubuntu1
* Re-sync with Debian
* Remove bashisms from debian/rules

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: utf-8 -*- 
2
 
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:
3
 
# $Id: plugin.py 427 2005-07-21 19:54:24Z aafshar $
4
 
#Copyright (c) 2005 Ali Afshar aafshar@gmail.com
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
 
# System imports
25
 
import os
26
 
import time
27
 
# GTK imports
28
 
import gtk
29
 
import gobject
30
 
# Pida imports
31
 
import pida.plugin as plugin
32
 
import pida.gtkextra as gtkextra
33
 
import pida.configuration.registry as registry
34
 
# local imports
35
 
import gdkvim
36
 
import vimembed
37
 
 
38
 
class Plugin(plugin.Plugin):
39
 
    NAME = 'Vim'
40
 
    RESIZABLE = False
41
 
    DICON = 'configure', 'Configure Pida'
42
 
 
43
 
    def configure(self, reg):
44
 
        self.registry = reg.add_group('vim', 'Options pertaining to the editor')
45
 
 
46
 
        self.registry.add('foreground_jump',
47
 
                  registry.Boolean,
48
 
                  1,
49
 
                  'Determines whether Pida will foreground Vim on actions.')
50
 
        self.registry.add('easy_mode',
51
 
                  registry.Boolean,
52
 
                  0,
53
 
                  'Determines whether Pida start Vim in Evim (easy mode).')
54
 
                  
55
 
                  
56
 
        self.registry.add('shutdown_with_vim',
57
 
                  registry.Boolean,
58
 
                  0,
59
 
                  'Determines whether Pida will shutdown when Vim shuts down.')
60
 
 
61
 
        self.registry.add('show_serverlist',
62
 
               registry.Boolean,
63
 
               False,
64
 
               'Determines whether the server bar will be shown in embedded '
65
 
               'mode. (embedded mode only)')
66
 
 
67
 
        shgrp = reg.add_group('vim_shortcuts', 'Shortcuts called from vim.')
68
 
 
69
 
        shgrp.add('shortcut_leader',
70
 
                  registry.RegistryItem,
71
 
                  ',',
72
 
                  'The value of the leasder key press')
73
 
 
74
 
        shgrp.add('shortcut_debug',
75
 
                  registry.RegistryItem,
76
 
                  'x',
77
 
                  'The shortcut to ')
78
 
 
79
 
        shgrp.add('shortcut_breakpoint_set',
80
 
                  registry.RegistryItem,
81
 
                  'b',
82
 
                  'The shortcut to ')
83
 
 
84
 
        shgrp.add('shortcut_breakpoint_clear',
85
 
                  registry.RegistryItem,
86
 
                  'B',
87
 
                  'The shortcut to ')
88
 
 
89
 
        shgrp.add('shortcut_execute',
90
 
                  registry.RegistryItem,
91
 
                  'x',
92
 
                  'The shortcut to ')
93
 
 
94
 
        shgrp.add('shortcut_project_execute',
95
 
                  registry.RegistryItem,
96
 
                  'p',
97
 
                  'The shortcut to ')
98
 
 
99
 
        shgrp.add('shortcut_doc_yanked',
100
 
                  registry.RegistryItem,
101
 
                  '/',
102
 
                  'The shortcut to ')
103
 
 
104
 
        shgrp.add('shortcut_doc_cursor',
105
 
                  registry.RegistryItem,
106
 
                  '?',
107
 
                  'The shortcut to ')
108
 
        
109
 
        shgrp.add('shortcut_pastebin_yanked',
110
 
                  registry.RegistryItem,
111
 
                  'm',
112
 
                  'The shortcut to paste the yanked text to a pastebin.')
113
 
 
114
 
        shgrp.add('shortcut_switch_focus',
115
 
            registry.RegistryItem,
116
 
            ',',
117
 
            'The shortcut to shift focust to the Pida controls')
118
 
 
119
 
    def populate_widgets(self):
120
 
        self.old_shortcuts = {'n':{}, 'v':{}}
121
 
        self.vim = None
122
 
        self.embedname = None
123
 
        #gobject.timeout_add(2000, self.cb_refresh)
124
 
        self.currentserver = None
125
 
        self.oldservers = []
126
 
        self.add_button('connect', self.cb_connect,
127
 
            'Connect to Vim session')
128
 
        self.add_button('editor', self.cb_run,
129
 
            'Execute a new Vim session')
130
 
        self.add_button('refresh', self.cb_refresh,
131
 
            'Refresh server list')
132
 
        self.entry = gtk.combo_box_new_text()
133
 
        self.add(self.entry, False)
134
 
        self.last_pane_position = 600
135
 
    
136
 
    def launch(self):
137
 
        vc = 'vim'
138
 
        if self.registry.easy_mode.value():
139
 
            vc = 'evim'
140
 
        vimcom = getattr(self.prop_main_registry.commands, vc).value()
141
 
        if self.is_embedded():
142
 
            if self.vim:
143
 
                self.message('Only one embedded Vim allowed.')
144
 
            else:
145
 
                name = self.generate_embedname()
146
 
                self.vim = vimembed.PidaVim(self.pida.embedwindow, vimcom, name)
147
 
                self.vim.connect(self.cb_plugged, self.cb_unplugged)
148
 
        else:
149
 
            os.system('%s --servername pida' % vimcom)
150
 
 
151
 
    def refresh(self, serverlist):
152
 
        if serverlist != self.oldservers:
153
 
            self.oldservers = serverlist
154
 
            actiter = self.entry.get_active_iter()
155
 
            act = None
156
 
            if actiter:
157
 
                act = self.entry.get_model().get_value(actiter, 0)
158
 
            self.entry.get_model().clear()
159
 
            for i in serverlist:
160
 
                s = i.strip()
161
 
                if self.is_embedded():
162
 
                    if s == self.embedname:
163
 
                        self.entry.append_text(s.strip())
164
 
                        if self.currentserver != s:
165
 
                            self.connectserver(s)
166
 
                else:
167
 
                    self.entry.append_text(s.strip())
168
 
            if act:
169
 
                for row in self.entry.get_model():
170
 
                    if row[0] == act:
171
 
                        self.entry.set_active_iter(row.iter)
172
 
            else:
173
 
                self.entry.set_active(0)
174
 
            if not self.currentserver:
175
 
                server = None
176
 
                if not self.is_embedded():
177
 
                    if serverlist:
178
 
                        server = serverlist[0]
179
 
                self.connectserver(server)
180
 
 
181
 
    def connectserver(self, name):
182
 
        # Actually does the connecting
183
 
        if name:
184
 
            self.currentserver = name
185
 
            self.load_shortcuts()
186
 
            self.cw.send_ex(self.currentserver, '%s' % VIMSCRIPT)
187
 
            self.do_evt('serverchange', name)
188
 
        else:
189
 
            # Being asked to connect to None is the same as having nothing
190
 
            # to connect to.
191
 
            self.do_evt('disconnected', name)
192
 
 
193
 
    def fetchserverlist(self):
194
 
        """Get the list of servers"""
195
 
        # Call the method of the vim communication window.
196
 
        self.cw.fetch_serverlist()
197
 
 
198
 
    def load_shortcuts(self):
199
 
        for mapc in ['n', 'v']:
200
 
            if self.old_shortcuts[mapc].setdefault(self.currentserver, []):
201
 
                for sc in self.old_shortcuts[mapc][self.currentserver]:
202
 
                    self.cw.send_ex(self.currentserver, UNMAP_COM % (mapc, sc))
203
 
            self.old_shortcuts[mapc][self.currentserver] = []
204
 
            l = self.prop_main_registry.vim_shortcuts.shortcut_leader.value()
205
 
            for name, command in SHORTCUTS:
206
 
                c = getattr(self.prop_main_registry.vim_shortcuts, name).value()
207
 
                sc = ''.join([l, c])
208
 
                self.old_shortcuts[mapc][self.currentserver].append(sc)
209
 
                self.cw.send_ex(self.currentserver, NMAP_COM % (mapc, sc, command))
210
 
 
211
 
    def generate_embedname(self):
212
 
        self.embedname = 'PIDA_EMBED_%s' % time.time()
213
 
        return self.embedname
214
 
 
215
 
    def is_embedded(self):
216
 
        return self.prop_main_registry.layout.embedded_mode.value()
217
 
 
218
 
    def cb_plugged(self):
219
 
        self.pida.embedslider.set_position(self.last_pane_position)
220
 
 
221
 
    def cb_unplugged(self):
222
 
        self.last_pane_position = self.pida.embedslider.get_position()
223
 
        self.pida.embedslider.set_position(0)
224
 
        self.vim = None
225
 
 
226
 
    def cb_connect(self, *a):
227
 
        iter = self.entry.get_active_iter()
228
 
        if iter:
229
 
            name = self.entry.get_model().get_value(iter, 0)
230
 
            self.connectserver(name)
231
 
   
232
 
    def cb_refresh(self, *args):
233
 
        self.fetchserverlist()
234
 
        return True
235
 
 
236
 
    def cb_run(self, *args):
237
 
        self.launch()
238
 
 
239
 
    def cb_alternative(self, *args):
240
 
        self.do_action('showconfig')
241
 
 
242
 
    def show_or_hide_serverlist(self):
243
 
        if self.is_embedded():
244
 
            if self.registry.show_serverlist.value():
245
 
                self.entry.show()
246
 
            else:
247
 
                self.entry.hide()
248
 
 
249
 
    def evt_die(self):
250
 
        if self.is_embedded():
251
 
            pass
252
 
            #self.cw.quit(self.currentserver)
253
 
 
254
 
    def evt_reset(self):
255
 
        if self.embedded_value != self.prop_main_registry.layout.embedded_mode.value():
256
 
            self.message('Embedded mode setting has changed.\n'
257
 
                         'You must restart Pida.')
258
 
            return
259
 
        self.load_shortcuts()
260
 
        self.show_or_hide_serverlist()
261
 
 
262
 
    def evt_started(self, *args):
263
 
        self.cw = gdkvim.VimWindow()
264
 
        self.embedded_value = self.prop_main_registry.layout.embedded_mode.value()
265
 
        if self.is_embedded():
266
 
            self.launch()
267
 
 
268
 
    def evt_serverlist(self, serverlist):
269
 
        self.refresh(serverlist)
270
 
 
271
 
    def evt_vimshutdown(self, *args):
272
 
        if self.is_embedded():
273
 
            # Check if users want shutdown with Vim
274
 
            if self.registry.shutdown_with_vim.value():
275
 
                # Quit pida
276
 
                self.do_action('quit')
277
 
                # The application never gets here
278
 
        del self.old_shortcuts['n'][self.currentserver]
279
 
        del self.old_shortcuts['v'][self.currentserver]
280
 
        self.currentserver = None
281
 
        self.cb_refresh()
282
 
 
283
 
    def evt_serverchange(self, name):
284
 
        self.currentserver = name
285
 
 
286
 
    def evt_editorfocus(self):
287
 
        self.cw.foreground(self.currentserver)
288
 
 
289
 
    def edit_closebuffer(self):
290
 
        """ Close the current buffer. """
291
 
        # Call the method of the vim communication window.
292
 
        self.cw.close_buffer(self.currentserver)
293
 
 
294
 
    def edit_gotoline(self, line):
295
 
        # Call the method of the vim communication window.
296
 
        self.cw.change_cursor(self.currentserver, 1, line)
297
 
        # Optionally foreground Vim.
298
 
        self.edit_foreground()
299
 
 
300
 
    def edit_getbufferlist(self):
301
 
        """ Get the buffer list. """
302
 
        # Call the method of the vim communication window.
303
 
        self.cw.get_bufferlist(self.currentserver)
304
 
 
305
 
    def edit_getcurrentbuffer(self):
306
 
        """ Ask Vim to return the current buffer number. """
307
 
        # Call the method of the vim communication window.
308
 
        self.cw.get_current_buffer(self.currentserver)
309
 
 
310
 
    def edit_changebuffer(self, number):
311
 
        """Change buffer in the active vim"""
312
 
        # Call the method of the vim communication window.
313
 
        self.cw.change_buffer(self.currentserver, number)
314
 
        # Optionally foreground Vim.
315
 
        self.cw.foreground(self.currentserver)
316
 
   
317
 
    def edit_foreground(self):
318
 
        """ Put vim into the foreground """
319
 
        # Check the configuration option.
320
 
        if self.registry.foreground_jump.value():
321
 
            # Call the method of the vim communication window.
322
 
            self.cw.foreground(self.currentserver)
323
 
 
324
 
    def edit_openfile(self, filename):
325
 
        """open a new file in the connected vim"""
326
 
        # Call the method of the vim communication window.
327
 
        self.cw.open_file(self.currentserver, filename)
328
 
 
329
 
    def edit_preview(self, filename):
330
 
        self.cw.preview_file(self.currentserver, filename)
331
 
 
332
 
    def edit_newterminal(self, command, args, **kw):
333
 
        """Open a new terminal, by issuing an event"""
334
 
        # Fire the newterm event, the terminal plugin will respond.
335
 
        self.evt('newterm', command, args, **kw)
336
 
 
337
 
    def edit_quitvim(self):
338
 
        """
339
 
        Quit Vim.
340
 
        """
341
 
        self.cw.quit(self.currentserver)
342
 
 
343
 
 
344
 
SHORTCUTS = [('shortcut_execute',
345
 
                'Async_event("bufferexecute,")'),
346
 
             ('shortcut_project_execute',
347
 
                'Async_event("projectexecute,")'),
348
 
             ('shortcut_breakpoint_set',
349
 
                'Async_event("breakpointset,".line("."))'),
350
 
             ('shortcut_breakpoint_clear',
351
 
                'Async_event("breakpointclear,".line("."))'),
352
 
             ('shortcut_doc_yanked',
353
 
                '''Async_event("doc,".expand('<cword>'))'''),
354
 
             ('shortcut_doc_cursor',
355
 
                'Async_event("doc,".@")'),
356
 
             ('shortcut_pastebin_yanked',
357
 
                '''Async_event("pastebin,".@")'''),
358
 
             ('shortcut_switch_focus',
359
 
                'Async_event("switchfocus,")')]
360
 
 
361
 
NMAP_COM = '%smap %s :call %s<lt>CR>'
362
 
 
363
 
UNMAP_COM = '%sun %s'
364
 
 
365
 
VIMSCRIPT = ''':silent function! Bufferlist()
366
 
let i = 1
367
 
    let max = bufnr('$') + 1
368
 
    let lis = ""
369
 
    while i < max
370
 
        if bufexists(i)
371
 
            let lis = lis.";".i.":".bufname(i)
372
 
        endif
373
 
        let i = i + 1
374
 
    endwhile
375
 
    return lis
376
 
endfunction
377
 
:silent function! Yank_visual()
378
 
    y
379
 
    return @"
380
 
endfunction
381
 
:silent function! Async_event(e)
382
 
    let c = "call server2client('".expand('<client>')."', '".a:e."')"
383
 
    try
384
 
        exec c
385
 
    catch /.*/
386
 
        echo c
387
 
    endtry
388
 
endfunction
389
 
:silent augroup pida
390
 
:silent au! pida
391
 
:silent au pida BufEnter * call Async_event("bufferchange,".bufnr('%').",".bufname('%'))
392
 
:silent au pida BufDelete * call Async_event("bufferunload,")
393
 
:silent au pida VimLeave * call Async_event("vimshutdown,")
394
 
:silent au pida FileType * call Async_event("filetype,".bufnr('%').",".expand('<amatch>'))
395
 
:echo "PIDA connected"
396
 
'''
397