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
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:
13
#The above copyright notice and this permission notice shall be included in
14
#all copies or substantial portions of the Software.
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
31
import pida.plugin as plugin
32
import pida.gtkextra as gtkextra
33
import pida.configuration.registry as registry
38
class Plugin(plugin.Plugin):
41
DICON = 'configure', 'Configure Pida'
43
def configure(self, reg):
44
self.registry = reg.add_group('vim', 'Options pertaining to the editor')
46
self.registry.add('foreground_jump',
49
'Determines whether Pida will foreground Vim on actions.')
50
self.registry.add('easy_mode',
53
'Determines whether Pida start Vim in Evim (easy mode).')
56
self.registry.add('shutdown_with_vim',
59
'Determines whether Pida will shutdown when Vim shuts down.')
61
self.registry.add('show_serverlist',
64
'Determines whether the server bar will be shown in embedded '
65
'mode. (embedded mode only)')
67
shgrp = reg.add_group('vim_shortcuts', 'Shortcuts called from vim.')
69
shgrp.add('shortcut_leader',
70
registry.RegistryItem,
72
'The value of the leasder key press')
74
shgrp.add('shortcut_debug',
75
registry.RegistryItem,
79
shgrp.add('shortcut_breakpoint_set',
80
registry.RegistryItem,
84
shgrp.add('shortcut_breakpoint_clear',
85
registry.RegistryItem,
89
shgrp.add('shortcut_execute',
90
registry.RegistryItem,
94
shgrp.add('shortcut_project_execute',
95
registry.RegistryItem,
99
shgrp.add('shortcut_doc_yanked',
100
registry.RegistryItem,
104
shgrp.add('shortcut_doc_cursor',
105
registry.RegistryItem,
109
shgrp.add('shortcut_pastebin_yanked',
110
registry.RegistryItem,
112
'The shortcut to paste the yanked text to a pastebin.')
114
shgrp.add('shortcut_switch_focus',
115
registry.RegistryItem,
117
'The shortcut to shift focust to the Pida controls')
119
def populate_widgets(self):
120
self.old_shortcuts = {'n':{}, 'v':{}}
122
self.embedname = None
123
#gobject.timeout_add(2000, self.cb_refresh)
124
self.currentserver = None
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
138
if self.registry.easy_mode.value():
140
vimcom = getattr(self.prop_main_registry.commands, vc).value()
141
if self.is_embedded():
143
self.message('Only one embedded Vim allowed.')
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)
149
os.system('%s --servername pida' % vimcom)
151
def refresh(self, serverlist):
152
if serverlist != self.oldservers:
153
self.oldservers = serverlist
154
actiter = self.entry.get_active_iter()
157
act = self.entry.get_model().get_value(actiter, 0)
158
self.entry.get_model().clear()
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)
167
self.entry.append_text(s.strip())
169
for row in self.entry.get_model():
171
self.entry.set_active_iter(row.iter)
173
self.entry.set_active(0)
174
if not self.currentserver:
176
if not self.is_embedded():
178
server = serverlist[0]
179
self.connectserver(server)
181
def connectserver(self, name):
182
# Actually does the connecting
184
self.currentserver = name
185
self.load_shortcuts()
186
self.cw.send_ex(self.currentserver, '%s' % VIMSCRIPT)
187
self.do_evt('serverchange', name)
189
# Being asked to connect to None is the same as having nothing
191
self.do_evt('disconnected', name)
193
def fetchserverlist(self):
194
"""Get the list of servers"""
195
# Call the method of the vim communication window.
196
self.cw.fetch_serverlist()
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()
208
self.old_shortcuts[mapc][self.currentserver].append(sc)
209
self.cw.send_ex(self.currentserver, NMAP_COM % (mapc, sc, command))
211
def generate_embedname(self):
212
self.embedname = 'PIDA_EMBED_%s' % time.time()
213
return self.embedname
215
def is_embedded(self):
216
return self.prop_main_registry.layout.embedded_mode.value()
218
def cb_plugged(self):
219
self.pida.embedslider.set_position(self.last_pane_position)
221
def cb_unplugged(self):
222
self.last_pane_position = self.pida.embedslider.get_position()
223
self.pida.embedslider.set_position(0)
226
def cb_connect(self, *a):
227
iter = self.entry.get_active_iter()
229
name = self.entry.get_model().get_value(iter, 0)
230
self.connectserver(name)
232
def cb_refresh(self, *args):
233
self.fetchserverlist()
236
def cb_run(self, *args):
239
def cb_alternative(self, *args):
240
self.do_action('showconfig')
242
def show_or_hide_serverlist(self):
243
if self.is_embedded():
244
if self.registry.show_serverlist.value():
250
if self.is_embedded():
252
#self.cw.quit(self.currentserver)
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.')
259
self.load_shortcuts()
260
self.show_or_hide_serverlist()
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():
268
def evt_serverlist(self, serverlist):
269
self.refresh(serverlist)
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():
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
283
def evt_serverchange(self, name):
284
self.currentserver = name
286
def evt_editorfocus(self):
287
self.cw.foreground(self.currentserver)
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)
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()
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)
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)
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)
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)
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)
329
def edit_preview(self, filename):
330
self.cw.preview_file(self.currentserver, filename)
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)
337
def edit_quitvim(self):
341
self.cw.quit(self.currentserver)
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,")')]
361
NMAP_COM = '%smap %s :call %s<lt>CR>'
363
UNMAP_COM = '%sun %s'
365
VIMSCRIPT = ''':silent function! Bufferlist()
367
let max = bufnr('$') + 1
371
let lis = lis.";".i.":".bufname(i)
377
:silent function! Yank_visual()
381
:silent function! Async_event(e)
382
let c = "call server2client('".expand('<client>')."', '".a:e."')"
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"