def act(context, default=None, prefix_selection=False, suffix_selection=False, undo_name=None, **syntaxes): ''' Required action method Inserts arbitrary text over all selections; specific text can be syntax-specific (same procedure as Wrap Selection In Link) If you set prefix_selection to true, the inserted text will precede any selected text; if suffix_selection is true it will follow any selected text; if both are true it will wrap the text ''' # Grab the ranges ranges = tea.get_ranges(context) # Set up our text recipe insertions = tea.new_recipe() for range in ranges: if prefix_selection or suffix_selection: # Get the selected text text = tea.get_selection(context, range) if prefix_selection: text = '$INSERT' + text if suffix_selection: text += '$INSERT' # If empty selection, only insert one if text == '$INSERT$INSERT': text = '$INSERT' else: text = '$INSERT' # Check for zone-specific insertion insert = tea.select_from_zones(context, range, default, **syntaxes) text = text.replace('$INSERT', insert) text = text.replace('$TOUCH', '') # Insert the text, or replace the selected text if range.length is 0: insertions.addInsertedString_forIndex_(text, range.location) else: insertions.addReplacementString_forRange_(text, range) # Set undo name and run the recipe if undo_name != None: insertions.setUndoActionName_(undo_name) reset_cursor = False if len(ranges) is 1 and ranges[0].length is 0: # Thanks to addInsertedString's wonkiness, we have to reset the cursor reset_cursor = True # Espresso beeps if I return True or False; hence this weirdness return_val = context.applyTextRecipe_(insertions) if reset_cursor: new_range = tea.new_range(ranges[0].location + len(text), 0) tea.set_selected_range(context, new_range) return return_val
def act(controller, bundle, options): ''' Required action method input dictates what should be trimmed: - None (default): falls back to alternate - selection: ignores lines if they exist, just trims selection - selected_lines: each line in the selection alternate dictates what to fall back on - None (default): will do nothing if input is blank - line: will trim the line the caret is on - all_lines: all lines in the document trim dictates what part of the text should be trimmed: - both (default) - start - end If respect_indent is True, indent characters (as defined in preferences) at the beginning of the line will be left untouched. ''' context = tea.get_context(controller) input = tea.get_option(options, 'input') alternate = tea.get_option(options, 'alternate') trim = tea.get_option(options, 'trim', 'both') respect_indent = tea.get_option(options, 'respect_indent', False) discard_empty = tea.get_option(options, 'discard_empty', False) # Since input is always a selection of some kind, check if we have one range = tea.get_range(context) if (range.length == 0) or input is None: if alternate.lower() == 'line': text, range = tea.get_line(context) text = tea.trim(context, text, False, trim, respect_indent, True, discard_empty) elif alternate.lower() == 'all_lines': range = tea.new_range(0, context.string().length()) text = tea.get_selection(context, range) text = tea.trim(context, text, True, trim, respect_indent, True, discard_empty) else: if input.lower() == 'selected_lines': parse_lines = True else: parse_lines = False text = tea.get_selection(context, range) text = tea.trim(context, text, parse_lines, trim, respect_indent, True, discard_empty) tea.insert_text(context, text, range) new_range = tea.new_range(range.location, len(text)) tea.set_selected_range(context, new_range)
def create_selection(self, start, end=None): """ Creates selection from <code>start</code> to <code>end</code> character indexes. If <code>end</code> is ommited, this method should place caret and <code>start</code> index @type start: int @type end: int @example zen_editor.create_selection(10, 40) # move caret to 15th character zen_editor.create_selection(15) """ if end is None: end = start new_range = tea.new_range(start, end - start) tea.set_selected_range(self._context, new_range)
def act(controller, bundle, options): # Grab the context context = tea.get_context(controller) image = find_image(context) if image: size = get_image_size(context, image['tag']) if size: new_tag = replace_or_append(image['tag'], 'width', size['width']) new_tag = replace_or_append(new_tag, 'height', size['height']) rng = tea.get_range(context) tea.insert_text(context, new_tag, tea.new_range(image['start'], image['end'] - image['start'])) tea.set_selected_range(context, rng) return True return False
def act(controller, bundle, options): context = tea.get_context(controller) direction = tea.get_option(options, 'direction', 'out') # Since input is always a selection of some kind, check if we have one rng = tea.get_range(context) cursor = rng.location + rng.length range_start, range_end = rng.location, rng.location + rng.length content = context.string() old_open_tag = html_matcher.last_match['opening_tag'] old_close_tag = html_matcher.last_match['closing_tag'] if direction.lower() == 'in' and old_open_tag and range_start != range_end: # user has previously selected tag and wants to move inward if not old_close_tag: # unary tag was selected, can't move inward return False elif old_open_tag.start == range_start: if content[old_open_tag.end] == '<': # test if the first inward tag matches the entire parent tag's content _start, _end = html_matcher.find(content, old_open_tag.end + 1) if _start == old_open_tag.end and _end == old_close_tag.start: start, end = html_matcher.match(content, old_open_tag.end + 1) else: start, end = old_open_tag.end, old_close_tag.start else: start, end = old_open_tag.end, old_close_tag.start else: new_cursor = content.find('<', old_open_tag.end, old_close_tag.start) search_pos = new_cursor != -1 and new_cursor + 1 or old_open_tag.end start, end = html_matcher.match(content, search_pos) else: start, end = html_matcher.match(content, cursor) if start is not None: new_range = tea.new_range(start, end - start) tea.set_selected_range(context, new_range) return True else: return False
def act(context, target=None, source=None, trim=False, discard_indent=False, search_string=None, regex=False): ''' Required action method target dictates what we're looking for: - text - if unspecified, simply selects the source source dictates how to gather the string to search for: - word (word under the caret) - line (line under the caret) - if unspecified, defaults to selection Setting trim=True will cause the source to be trimmed Setting discard_indent=True will cause leading whitespace to be trimmed (unnecessary unless trim=True) search_string will set the string to search for if target is text or zone - $SELECTED_TEXT will be replaced with the source text Setting regex=True will cause search_string to be evaluated as regex ''' range = tea.get_ranges(context)[0] if source == 'word': text, range = tea.get_word(context, range) elif source == 'line': text, range = tea.get_line(context, range) elif range.length > 0: text = tea.get_selection(context, range) # Make sure that we've got some text, even if it's an empty string if text is None: text = '' # Trim the source if trim: if discard_indent: trimmed = tea.trim(context, text, False) else: trimmed = tea.trim(context, text, False, 'end') start = text.find(trimmed) if start != -1: start = range.location + start length = len(trimmed) if source == 'line': # We don't want the linebreak if we're trimming length = length - 1 range = tea.new_range(start, length) text = trimmed if target is not None and text: if search_string is not None: search = search_string.replace('$SELECTED_TEXT', text) else: search = text # Find the start and end points of the substring start = end = None if regex: match = re.search(r'(' + search + r')', context.string()) if match: # Get the start and end points start, end = match.span(1) else: start = context.string().find(search) if start != -1: end = start + len(search) else: start = None # Construct the new target range if start is not None and end is not None: range = tea.new_range(start, end - start) # Set the new range tea.set_selected_range(context, range) return True
def act(controller, bundle, options): ''' Required action method target dictates what we're looking for: - text - if unspecified, simply selects the source source dictates how to gather the string to search for: - word (word under the caret) - line (line under the caret) - if unspecified, defaults to selection Setting trim=True will cause the source to be trimmed Setting discard_indent=True will cause leading whitespace to be trimmed (unnecessary unless trim=True) search_string will set the string to search for if target is text or zone - $SELECTED_TEXT will be replaced with the source text Setting regex=True will cause search_string to be evaluated as regex ''' context = tea.get_context(controller) target = tea.get_option(options, 'target') source = tea.get_option(options, 'source') trim = tea.get_option(options, 'trim', False) discard_indent = tea.get_option(options, 'discard_indent', False) search_string = tea.get_option(options, 'search_string') regex = tea.get_option(options, 'regex', False) range = tea.get_range(context) if source == 'word': text, range = tea.get_word(context, range) elif source == 'line': text, range = tea.get_line(context) elif range.length > 0: text = tea.get_selection(context, range) # Trim the source if trim: if discard_indent: trimmed = tea.trim(context, text, False, preserve_linebreaks=False) else: trimmed = tea.trim(context, text, False, 'end', preserve_linebreaks=False) start = text.find(trimmed) if start != -1: start = range.location + start length = len(trimmed) if source == 'line' and trimmed[-1:] in ['\r\n', '\r', '\n']: # We don't want the linebreak if we're trimming length = length - 1 range = tea.new_range(start, length) text = trimmed if target is not None and text: if search_string is not None: search = search_string.replace('$SELECTED_TEXT', text) else: search = text # Find the start and end points of the substring start = end = None if regex: match = re.search(r'(' + search + r')', context.string()) if match: # Get the start and end points start, end = match.span(1) else: start = context.string().find(search) if start != -1: end = start + len(search) else: start = None # Construct the new target range if start is not None and end is not None: range = tea.new_range(start, end - start) # Set the new range tea.set_selected_range(context, range)