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 replace_content(self, value, start=None, end=None, no_indent=False): """ Replace editor's content or it's part (from <code>start</code> to <code>end</code> index). If <code>value</code> contains <code>caret_placeholder</code>, the editor will put caret into this position. If you skip <code>start</code> and <code>end</code> arguments, the whole target's content will be replaced with <code>value</code>. If you pass <code>start</code> argument only, the <code>value</code> will be placed at <code>start</code> string index of current content. If you pass <code>start</code> and <code>end</code> arguments, the corresponding substring of current target's content will be replaced with <code>value</code> @param value: Content you want to paste @type value: str @param start: Start index of editor's content @type start: int @param end: End index of editor's content @type end: int """ if start is None: start = 0 if end is None: end = len(self.get_content()) rng = tea.new_range(start, end - start) value = self.add_placeholders(value) if not no_indent: value = zencoding.utils.pad_string(value, get_line_padding(self.get_current_line())) sel_start, sel_end, value = self.preprocess_text(value) if sel_start is not None: select_range = tea.new_range(sel_start + rng.location, sel_end - sel_start) tea.insert_text_and_select(self._context, value, rng, select_range) else: tea.insert_text(self._context, value, rng)
def act(controller, bundle, options): context = tea.get_context(controller) # Get the options alpha_numeric = tea.get_option(options, 'alpha_numeric', True) extra_characters = tea.get_option(options, 'extra_characters', '_-') bidirectional = tea.get_option(options, 'bidirectional', True) snippet = tea.get_option(options, 'snippet', '<$SELECTED_TEXT>$0</$WORD>') mode = tea.get_option(options, 'mode', '') # Fetch the word range = context.selectedRange() word, new_range = tea.get_word_or_selection(context, range, alpha_numeric, extra_characters, bidirectional) if word == '': # No word, so nothing further to do return False # If we're using $WORD, make sure the word is just a word if snippet.find('$WORD') >= 0: fullword = word word = tea.parse_word(word) if word is None: word = '' else: fullword = word # Process that sucker! if mode == 'zen' and fullword.find(' ') < 0: # Explicitly load zen settings zen_settings = settings_loader.load_settings() zen_core.update_settings(zen_settings) # Set up the config variables zen_core.newline = tea.get_line_ending(context) zen_settings['variables']['indentation'] = 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_core.insertion_point = place_ins_point # Determine doctype as best we can based on file extension doc_type = tea.get_zen_doctype(context) # Prepare the snippet snippet = zen_core.expand_abbreviation(fullword, doc_type, 'xhtml') elif mode == 'zen' and tea.is_selfclosing(word): # Self-closing, so construct the snippet from scratch snippet = '<' + fullword if fullword == word and not fullword in ['br', 'hr']: snippet += ' $0 />' else: snippet += ' />$0' # Indent the snippet snippet = tea.indent_snippet(context, snippet, new_range) snippet = tea.clean_line_endings(context, snippet) # Special replacement in case we're using $WORD snippet = snippet.replace('$WORD', word) snippet = snippet.replace('$SELECTED_TEXT', fullword) cursor_loc = snippet.find('$0') if cursor_loc != -1: select_range = tea.new_range(cursor_loc + new_range.location, 0) snippet = snippet.replace('$0', '') tea.insert_text_and_select(context, snippet, new_range, select_range) else: tea.insert_text(context, snippet, new_range)
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)