2
2
import vdebug .log
3
3
import vim
4
4
import re
5
+ import os
5
6
import urllib
6
7
7
8
class Keymapper :
@@ -25,24 +26,37 @@ def close_key(self):
25
26
def map (self ):
26
27
if self .is_mapped :
27
28
return
29
+ self ._store_old_map ()
28
30
for func in self .keymaps :
29
- key = self .keymaps [func ]
30
31
if func not in self .exclude :
31
- vim .command ("redir @z | silent noremap %s%s | redir END" % (self .leader ,key ))
32
- self .__save_map_output ( vim .eval ("@z" ).strip () )
32
+ key = self .keymaps [func ]
33
33
map_cmd = "noremap %s%s :python debugger.%s()<cr>" % \
34
34
(self .leader ,key ,func )
35
35
vim .command (map_cmd )
36
36
self .is_mapped = True
37
37
38
- def __save_map_output (self ,output ):
39
- if output .startswith ('No mapping' ):
40
- return False
41
- else :
42
- vdebug .log .Log ("Storing existing key mapping, '%s' " % output ,\
43
- vdebug .log .Logger .DEBUG )
44
- self .existing .append (output )
45
- return True
38
+ def _store_old_map (self ):
39
+ vim .command ('let tempfile=tempname()' )
40
+ tempfile = vim .eval ("tempfile" )
41
+ vim .command ('mkexrc! %s' % (tempfile ))
42
+ regex = re .compile (r'^([nvxsoilc]|)(nore)?map!?' )
43
+ split_regex = re .compile (r'\s+' )
44
+ keys = set (v for (k ,v ) in self .keymaps .items () if k not in self .exclude )
45
+ special = set (["<buffer>" , "<silent>" , "<special>" , "<script>" , "<expr>" , "<unique>" ])
46
+ for line in open (tempfile , 'r' ):
47
+ if not regex .match (line ):
48
+ continue
49
+ parts = split_regex .split (line )[1 :]
50
+ for p in parts :
51
+ if p in special :
52
+ continue
53
+ elif p in keys :
54
+ vdebug .log .Log ("Storing existing key mapping, '%s' " % line ,
55
+ vdebug .log .Logger .DEBUG )
56
+ self .existing .append (line )
57
+ else :
58
+ break
59
+ os .remove (tempfile )
46
60
47
61
def unmap (self ):
48
62
if self .is_mapped :
@@ -55,18 +69,7 @@ def unmap(self):
55
69
for mapping in self .existing :
56
70
vdebug .log .Log ("Remapping key with '%s' " % mapping ,\
57
71
vdebug .log .Logger .DEBUG )
58
- regex = re .compile (r'\s+' )
59
- parts = regex .split (mapping )
60
- mapcmd = 'noremap'
61
- if len (parts )> 2 :
62
- modeRegex = re .compile (r'^[nvsxoilc!]$' )
63
- if modeRegex .match (parts [0 ]):
64
- mapping = ' ' .join (parts [1 :])
65
- if parts [0 ]== '!' :
66
- mapcmd = 'noremap!'
67
- else :
68
- mapcmd = '%snoremap' % parts [0 ]
69
- vim .command ("%s %s" % (mapcmd ,mapping ))
72
+ vim .command (mapping )
70
73
71
74
class FilePath :
72
75
is_win = False
0 commit comments