I’ve written my own version, which includes a few smart options for the input. I’ll add the code at the end (it’s a lot of code). I’ll probably bundle it up and put it on BitBucket in a little while (I’m working on a few more plugins).
I’d be happy if you could try it out and tell me what you think right now, so I can get in some final fixes before putting it on BitBucket.
Summary of what this does
You can read the comments to find out how it works and see examples, but the idea is, it tries to do the “right” thing based on your input.
It supports comma separated lists of input.
It also supports numbers with any modifier you want.
With numbers, it supports automatically adding leading zeros, if it makes sense to do so.
It also supports hex output (input 0x1 and the output will be hex numbers), both “regular” hex output and 6-character hex output.
OK Here’s the code, let me know what you think. I bind the command to ctrl+i (i for insert), because I don’t use incremental find, I just use the normal find.
[code]
class insertText(sublimeplugin.TextCommand):
def init(self):
self.last_string = “1”
def run(self, view, args):
‘’’
Asks for text to be inserted in multiple selections.
Quick way to see all the features:
Select ten different lines, then try the following inputs and see what you get:
“green, blue, orange”
1
1 2
01
0x5
0x05
Full explanation of the input modes:
If you put in a comma separated list, it will copy each value in the list into each selection (it will keep repeating the list if necessary).
E.g., with 5 lines multi-selected, the input: "hello, this, is" will produce:
hello
this
is
hello
this
If you put in a number (and optionally a modifier to add/subtract, default is 1):
If it's a regular number, it will be added as expected.
If it has a 0 at the beginning, it will add leading zeros. For example, the input: "08" with 4 selections will give:
08 <----------- Note leading zero
09 <----------- Note leading zero
10
11
If you put in a number that starts with 0x, it will output hex numbers. If it starts with 0x0 (a leading zero), it will make it a full, 6 character hex number (and will wraparound to 0x000000 after 0xffffff).
'''
self.view = view
window = view.window()
window.showInputPanel('Enter value to insert:', self.last_string, self.onDone, None, None)
def interpretInput(self, input, num_selections):
‘’’
Returns a list of results, based on the input and the number of selections (i.e., the result list).
This is called in the case that the input is not a comma-seperated list.
‘’’
initial = input.split(" ")[0]
res = ] # The result array.
# Check if there is a modifier, if not use 1.
if len(input.split(" ")) > 1:
try:
modifier = int(input.split(" ")[1])
except:
raise RuntimeError, "Invalid modifier"
else:
modifier = 1
# The following code attempts to understand the input.
# It tries to match it against various patterns which we accept.
# If it fails, an exception is raised.
# If it's a single char (i.e. a letter)
if initial.isalpha() and len(initial) == 1:
curr = initial
for i in range(num_selections):
res.append(curr)
curr = chr(ord(curr) + modifier)
return res
# If it's only a number (not hex or binary, but might have a leading zero).
if initial.isdigit():
# Set some settings.
leading_zeros = False
if initial.startswith("0") and len(initial) > 1:
leading_zeros = True
# How many leading zeros to add?
if modifier > 0:
lastnum_len = len(str(int(initial) + modifier * num_selections))
else:
lastnum_len = len(str(int(initial)))
print lastnum_len
curr = int(initial)
if leading_zeros:
append_string = "%0" + str(lastnum_len) + "d"
else:
append_string = "%d"
for i in range(num_selections):
res.append(append_string % curr)
curr = curr + modifier
return res
# If it's a "full" hex number.
if initial.startswith("0x0") and len(initial) > 3:
hexnum = initial[3:]
intnum = int(hexnum, 16)
# Different code for full and non-full hex strings.
for i in range(num_selections):
# Since it's a full hex string, we'll make sure it's always 6 chars.
res.append("0x%06x" % (intnum % 0xffffff))
intnum = intnum + modifier
return res
# If it's a hex number (starts with an "0x").
if initial.startswith("0x") and len(initial) > 2:
hexnum = initial[2:]
intnum = int(hexnum, 16)
# Different code for full and non-full hex strings.
if len(hexnum) == 6:
for i in range(num_selections):
# Since it's a full hex string, we'll make sure it's always 6 chars.
res.append("0x%06x" % (intnum % 0xffffff))
intnum = intnum + modifier
else:
for i in range(num_selections):
res.append("0x%x" % intnum)
intnum = intnum + modifier
return res
# We couldn't recognize what it was, so raise an error.
raise RuntimeError, "Invalid input"
def onDone(self, input):
# Make a list of things to insert. We’ll fill this list up, then insert at the end.
self.last_string = input
selections = self.view.sel()
to_insert = ]
num_selections = len(selections)
# Input is either a comma seperated list, or a range.
if "," in input:
sp = [l.lstrip() for l in input.split(",")]
for i in range(num_selections):
to_insert.append(sp*)
else:
to_insert = self.interpretInput(input, num_selections)
for i, region in enumerate(selections):
self.view.replace(region, to_insert*)
[/code]**