~ubuntu-branches/ubuntu/raring/gedit-plugins/raring-proposed

« back to all changes in this revision

Viewing changes to plugins/commander/commander/commands/completion.py

  • Committer: Bazaar Package Importer
  • Author(s): Michael Biebl
  • Date: 2011-07-22 20:45:45 UTC
  • mfrom: (1.1.26 upstream)
  • mto: (7.3.1 sid) (1.4.8)
  • mto: This revision was merged to the branch mainline in revision 44.
  • Revision ID: james.westby@ubuntu.com-20110722204545-15t6eui58z5tdb6l
Tags: upstream-3.0.5
ImportĀ upstreamĀ versionĀ 3.0.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
#
 
3
#  completion.py - commander
 
4
#
 
5
#  Copyright (C) 2010 - Jesse van den Kieboom
 
6
#
 
7
#  This program is free software; you can redistribute it and/or modify
 
8
#  it under the terms of the GNU General Public License as published by
 
9
#  the Free Software Foundation; either version 2 of the License, or
 
10
#  (at your option) any later version.
 
11
#
 
12
#  This program is distributed in the hope that it will be useful,
 
13
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
#  GNU General Public License for more details.
 
16
#
 
17
#  You should have received a copy of the GNU General Public License
 
18
#  along with this program; if not, write to the Free Software
 
19
#  Foundation, Inc., 59 Temple Place, Suite 330,
 
20
#  Boston, MA 02111-1307, USA.
 
21
 
1
22
import commander.commands as commands
2
23
import bisect
3
24
import sys
4
25
import os
5
26
import re
6
 
import gio
7
27
 
8
28
from xml.sax import saxutils
9
29
 
11
31
 
12
32
def _common_prefix_part(first, second):
13
33
        length = min(len(first), len(second))
14
 
        
 
34
 
15
35
        for i in range(0, length):
16
36
                if first[i] != second[i]:
17
37
                        return first[:i]
18
38
 
19
39
        return first[:length]
20
 
        
 
40
 
21
41
def common_prefix(args, sep=None):
22
42
        # A common prefix can be something like
23
43
        # first: some-thing
30
50
 
31
51
        if len(args) == 1:
32
52
                return str(args[0])
33
 
        
 
53
 
34
54
        first = str(args[0])
35
55
        second = str(args[1])
36
56
 
43
63
 
44
64
                for i in range(0, min(len(first), len(second))):
45
65
                        ret.append(_common_prefix_part(first[i], second[i]))
46
 
                
 
66
 
47
67
                ret = sep.join(ret)
48
 
                        
 
68
 
49
69
        del args[0]
50
70
        args[0] = ret
51
71
 
60
80
        del cmds[:]
61
81
 
62
82
        # Expand 'commands' to all the respective subcommands
63
 
        
 
83
 
64
84
        for cmd in old:
65
85
                for c in cmd.commands():
66
86
                        bisect.insort(cmds, c)
67
87
 
68
88
def _filter_command(cmd, subs):
69
89
        parts = cmd.name.split('-')
70
 
        
 
90
 
71
91
        if len(subs) > len(parts):
72
92
                return False
73
 
        
 
93
 
74
94
        for i in xrange(0, len(subs)):
75
95
                if not parts[i].startswith(subs[i]):
76
96
                        return False
77
 
        
 
97
 
78
98
        return True
79
99
 
80
100
def _filter_commands(cmds, subs):
81
101
        # See what parts of cmds still match the parts in subs
82
102
        idx = bisect.bisect_left(cmds, subs[0])
83
103
        ret = []
84
 
        
 
104
 
85
105
        while idx < len(cmds):
86
106
                if not cmds[idx].name.startswith(subs[0]):
87
107
                        break
88
 
                
 
108
 
89
109
                if _filter_command(cmds[idx], subs):
90
110
                        ret.append(cmds[idx])
91
 
                
 
111
 
92
112
                idx += 1
93
 
        
 
113
 
94
114
        return ret
95
115
 
96
116
def single_command(words, idx):
100
120
                return None
101
121
 
102
122
        ret[0] = filter(lambda x: x.method, ret[0])
103
 
        
 
123
 
104
124
        if not ret[0]:
105
125
                return None
106
 
        
 
126
 
107
127
        return ret[0][0]
108
128
 
109
129
def command(words, idx):
118
138
 
119
139
                if not cmds:
120
140
                        return None
121
 
                
122
 
                subs = i.split('-')     
 
141
 
 
142
                subs = i.split('-')
123
143
                cmds = _filter_commands(cmds, subs)
124
144
 
125
145
                if not cmds:
126
146
                        return None
127
 
        
 
147
 
128
148
        if not cmds:
129
149
                return None
130
 
        
 
150
 
131
151
        if len(parts) == 1:
132
152
                completed = common_prefix(cmds)
133
153
        else:
140
160
                format = '<span color="#799ec6">%s</span>'
141
161
        else:
142
162
                format = '%s'
143
 
        
 
163
 
144
164
        return format % (saxutils.escape(os.path.basename(path)),)
145
165
 
146
 
def _sort_nicely(l): 
 
166
def _sort_nicely(l):
147
167
        convert = lambda text: int(text) if text.isdigit() else text
148
168
        alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
149
169
 
152
172
def filename(words, idx, view):
153
173
        prefix = os.path.dirname(words[idx])
154
174
        partial = os.path.expanduser(words[idx])
155
 
        
 
175
 
156
176
        doc = view.get_buffer()
157
 
        
 
177
 
158
178
        if not doc.is_untitled():
159
 
                root = os.path.dirname(gio.File(doc.get_uri()).get_path())
 
179
                root = os.path.dirname(doc.get_location().get_path())
160
180
        else:
161
181
                root = os.path.expanduser('~/')
162
 
                
 
182
 
163
183
        if not os.path.isabs(partial):
164
184
                partial = os.path.join(root, partial)
165
 
        
 
185
 
166
186
        dirname = os.path.dirname(partial)
167
 
        
 
187
 
168
188
        try:
169
189
                files = os.listdir(dirname)
170
190
        except OSError:
171
191
                return None
172
 
        
 
192
 
173
193
        base = os.path.basename(partial)
174
194
        ret = []
175
195
        real = []
178
198
                if f.startswith(base) and (base or not f.startswith('.')):
179
199
                        real.append(os.path.join(dirname, f))
180
200
                        ret.append(os.path.join(prefix, f))
181
 
        
 
201
 
182
202
        _sort_nicely(real)
183
 
        
 
203
 
184
204
        if len(ret) == 1:
185
205
                if os.path.isdir(real[0]):
186
206
                        after = '/'
195
215
        def decorator(words, idx):
196
216
                rr = filter(lambda x: x.startswith(words[idx]), ret)
197
217
                return rr, common_prefix(rr)
198
 
        
 
218
 
199
219
        return decorator