def act(context, input=None, default=None, **syntaxes): ''' Required action method input dictates what fills the placeholder if there is no selection: - word - line default and syntaxes will replace $EDITOR_SELECTION with a URL escaped version of the selected text (or input, if no selected text) ''' text, range = tea.get_single_selection(context) if text is None: range = tea.get_single_range(context) if input == 'word': text, range = tea.get_word(context, range) elif input == 'line': text, range = tea.get_line(context, range) # If we still don't have text, there's nothing to work with if text is None: return False # URL escape the selected text text = urllib.quote_plus(text) url = tea.select_from_zones(context, range, default, **syntaxes) # Got the URL, let's run the URL # DEPRECATED: please use $EDITOR_SELECTION instead url = url.replace('$SELECTED_TEXT', text) url = url.replace('$EDITOR_SELECTION', text) NSWorkspace.sharedWorkspace().openURL_(NSURL.URLWithString_(url)) # Because this gets passed through to Obj-C, using int prevents beeping return True
def act(context, default=None, fallback_url="", undo_name=None, **syntaxes): """ Required action method A flexible link generator which uses the clipboard text (if there's a recognizable link there) and formats the snippet based on the active syntax of the context """ if default is None: return False # Get the text and range text, range = tea.get_single_selection(context, True) if text == None: return False # 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_url) # Get the snippet based on the root zone snippet = tea.select_from_zones(context, range, default, **syntaxes) snippet = tea.construct_snippet(text, snippet) snippet = snippet.replace("$URL", tea.sanitize_for_snippet(url)) return tea.insert_snippet(context, snippet)
def act(context, direction=None, remove_duplicates=False, undo_name=None): ''' Required action method This only allows a single selection (enforced through the utility functions) then sorts the lines, either ascending or descending. Theoretically we could allow discontiguous selections; might be useful? ''' # Check if there is a selection, otherwise take all lines ranges = tea.get_ranges(context) if len(ranges) == 1 and ranges[0].length == 0: range = tea.new_range(0, context.string().length()) text = tea.get_selection(context, range) else: text, range = tea.get_single_selection(context, True) if text == None: return False # Split the text into lines, not maintaining the linebreaks lines = text.splitlines(False) # Remove duplicates if set if remove_duplicates: if direction is None: seen = {} result = [] for x in lines: if x in seen: continue seen[x] = 1 result.append(x) lines = result else: lines = list(set(lines)) # Sort lines ascending or descending if direction == 'asc' or direction == 'desc': lines.sort() if direction == 'desc': lines.reverse() # If direction is random, shuffle lines if direction == 'random': random.shuffle(lines) # Join lines to one string linebreak = tea.get_line_ending(context) sortedText = unicode.join(linebreak, lines) # Add final linebreak if selected text has one if text.endswith(linebreak): sortedText += linebreak # Paste the text return tea.insert_text_over_range(context, sortedText, range, undo_name)
def act(context, first_snippet='', following_snippet='', final_append='', undo_name=None): ''' Required action method Wraps the selected text in a snippet Support for discontiguous selections will be implemented when recipes can support snippets; until then only first_snippet will be used ''' # TODO: change to a loop once snippets in recipes are supported # This function will handle the logic of when to use open vs. multi text, range = tea.get_single_selection(context) if text == None: text = '' # Indent the snippet snippet = tea.indent_snippet(context, first_snippet + final_append, range) snippet = tea.construct_snippet(text, snippet) return tea.insert_snippet_over_range(context, snippet, range, undo_name)
def act(context, default=None, undo_name=None, **syntaxes): ''' Required action method Inserts an arbitrary text snippet after the cursor with provisions for syntax-specific alternatives Accepts $EDITOR_SELECTION placeholder This method requires at least the snippet default to be defined in the XML ''' if default is None: return False # Get the cursor position text, range = tea.get_single_selection(context) # Check for root-zone specific override snippet = tea.select_from_zones(context, range, default, **syntaxes) # Construct the snippet snippet = tea.construct_snippet(text, snippet) # Insert that snippet! return tea.insert_snippet(context, snippet)
def act(context, first_snippet='', following_snippet='', final_append='', undo_name=None): ''' Required action method Wraps the selected text in a snippet Support for discontiguous selections will be implemented when recipes can support snippets; until then only first_snippet will be used ''' # TODO: change to a loop once snippets in recipes are supported # This function will handle the logic of when to use open vs. multi text, range = tea.get_single_selection(context) if text == None: text = '' # Only indent the snippet if there aren't multiple lines in the selected text if len(text.splitlines()) > 1: indent = False else: indent = True snippet = tea.construct_snippet(text, first_snippet + final_append) return tea.insert_snippet(context, snippet, indent)
def act(context, first_snippet='', following_snippet='', final_append='', undo_name=None): ''' Required action method This only allows a single selection (enforced through the utility functions) then parses over the lines and inserts a snippet Theoretically we could allow discontiguous selections; have to consider it if recipes get snippet capabilities ''' text, range = tea.get_single_selection(context, True) if text == None: return False # Split the text into lines, maintaining the linebreaks lines = text.splitlines(True) # Compile the regex for quicker action on lots of lines parser = re.compile(r'(\s*)(.*?)(\s*(\r?\n)|$)') # Indent the snippets first = tea.indent_snippet(context, first_snippet, range) following = tea.indent_snippet(context, following_snippet, range) # Loop over lines and construct the snippet snippet = '' # This is the number of snippets processed, not lines count = 1 for line in lines: content = parser.search(line) # Only wrap the line if there's some content if content.group(2) != '': if count == 1: segment = tea.construct_snippet(content.group(2), first) else: segment = tea.construct_snippet(content.group(2), following) snippet += content.group(1) + segment + content.group(3) count += 1 else: snippet += line snippet += final_append return tea.insert_snippet_over_range(context, snippet, range, undo_name)