beforeRunningCommand nop bundleUUID B50944A0-714F-491C-9DE7-5C1E3937B8F2 command #!/usr/bin/python # -*- coding: utf-8 -*- import sys import re import os from subprocess import * # Set the environment for calling CocoaDialog. dialogpath = os.environ['TM_SUPPORT_PATH'] + '/bin/CocoaDialog.app/Contents/MacOS/' dialogenv = {'PATH': dialogpath} # Utility function for shortening text. def shorten(str, n): 'Truncate str to no more than n chars' return str if len(str) <= n else str[:n-1] + '…' # Get the selection. try: selection = os.environ['TM_SELECTED_TEXT'] except KeyError: selection = '' # Read in the buffer and collect all the reference links. text = sys.stdin.read() refs = re.findall(r'^\[([^^\]]+)\]: +(https?://(.+))$', text, re.MULTILINE) # Run the dialog asking for which link to use. choices = [ shorten(x[2], 100) for x in refs ] chosen = Popen(['CocoaDialog', 'standard-dropdown', '--title', 'Previous links', '--text', 'Choose an existing link', '--items'] + choices, stdout=PIPE, env =dialogenv).communicate()[0].split() # Insert the link. if int(chosen[0]) == 1: sys.stdout.write('[%s][%s]' % (selection, refs[int(chosen[1])][0])) else: sys.stdout.write(selection) fallbackInput none input document keyEquivalent ^~r name Choose reference link output replaceSelectedText scope text.html.markdown uuid 202A373B-314F-40DC-80E9-5AA5EF71C46A