def act(controller, bundle, options): context = tea.get_context(controller) snippet = tea.get_option(options, 'snippet', '') maintain_selection = tea.get_option(options, 'maintain_selection', False) text, range = tea.selection_and_range(context) snippet = tea.indent_snippet(context, snippet, range) snippet = tea.clean_line_endings(context, snippet) # Set up target selection sel_loc = snippet.find('$SELECTED_TEXT') cursor_loc = snippet.find('$0') if maintain_selection: select_range = tea.new_range(sel_loc + range.location, range.length) elif cursor_loc != -1: select_range = tea.new_range(snippet.find('$0') + range.location, 0) else: select_range = None snippet = snippet.replace('$SELECTED_TEXT', text) snippet = snippet.replace('$0', '') if select_range is not None: tea.insert_text_and_select(context, snippet, range, select_range) else: tea.insert_text(context, snippet, range)
def act(controller, bundle, options): # Grab the context context = tea.get_context(controller) # Setup the options fallback = tea.get_option(options, 'fallback', '') snippet = tea.get_option(options, 'snippet', '$URL') # Get the clipboard contents, parse for a URL process = subprocess.Popen(['pbpaste'], stdout=subprocess.PIPE) clipboard, error = process.communicate(None) # Construct the default link url = format_hyperlink(clipboard, fallback) # Grab the selected text and range text, range = tea.selection_and_range(context) # Parse the snippet for $SELECTED_TEXT placeholder sel_loc = snippet.find('$SELECTED_TEXT') if sel_loc != -1: replace_text = True prefix = snippet[0:sel_loc] suffix = snippet[sel_loc+14:] else: replace_text = False prefix = snippet suffix = '' prefix = prefix.replace('$URL', url) suffix = suffix.replace('$URL', url) if replace_text: replacement = prefix + text + suffix else: replacement = prefix url_loc = replacement.find(url) if url_loc == -1: url_loc = len(replacement) url_len = 0 else: url_len = len(url) newrange = tea.new_range(url_loc + range.location, url_len) tea.insert_text_and_select(context, replacement, range, newrange)
def wrap(self, context, abbr, profile_name='xhtml'): # Set up the config variables zen_settings = settings_loader.load_settings() zen.update_settings(zen_settings) zen.newline = self.safe_str(tea.get_line_ending(context)) zen_settings['variables']['indentation'] = self.safe_str(tea.get_indentation_string(context)) # This allows us to use smart incrementing tab stops in zen snippets point_ix = [0] def place_ins_point(text): if not point_ix[0]: point_ix[0] += 1 return '$0' else: return '' zen.insertion_point = place_ins_point text, rng = tea.selection_and_range(context) if not text: # no selection, find matching tag content = context.string() start, end = html_matcher.match(content, rng.location) if start is None: # nothing to wrap return False def is_space(char): return char.isspace() or char in r'\n\r' # narrow down selection until first non-space character while start < end: if not is_space(content[start]): break start += 1 while end > start: end -= 1 if not is_space(content[end]): end += 1 break rng = tea.new_range(start, end - start) text = tea.get_selection(context, rng) # Fetch the doctype based on file extension doc_type = tea.get_zen_doctype(context) text = self.unindent(context, text) # Damn Python's encodings! Have to convert string to ascii before wrapping # and then back to utf-8 result = zen.wrap_with_abbreviation(self.safe_str(abbr), self.safe_str(text), doc_type, profile_name) result = unicode(result, 'utf-8') result = tea.indent_snippet(context, result, rng) result = tea.clean_line_endings(context, result) cursor_loc = result.find('$0') if cursor_loc != -1: select_range = tea.new_range(cursor_loc + rng.location, 0) result = result.replace('$0', '') tea.insert_text_and_select(context, result, rng, select_range) else: tea.insert_text(context, result, rng)