def act(controller, bundle, options):
    '''
    Required action method
    
    Setting decode=True will decode instead of encoding
    '''
    
    context = cp.get_context(controller)
    decode = cp.get_option(options, 'decode', False)
    
    selection, range = cp.selection_and_range(context)
    
    # do nothing unless they selected something
    if range.length == 0:
        return
    
    if decode:
        try:
            text = base64.b64decode(selection)
            
        # base64decode raises a TypeError if the string doesn't have the right
        # padding, or if it's invalid base64... We'll just catch this error
        # and do nothing at all :)
        except TypeError:
            return
        
        # also, base64decode tends to return an empty string, which is also lame
        if range.length and not len(text):
            return
    
    else:
        text = base64.b64encode(selection)
    
    cp.insert_text(context, text, range)
def act(controller, bundle, options):
    context = cp.get_context(controller)
    line_ending = cp.get_line_ending(context)
    
    text, target_range = cp.selection_and_range(context)
    pb = NSPasteboard.generalPasteboard()
    
    old = pb.stringForType_(NSStringPboardType)
    
    if old:
        text = old + text
    
    pb.clearContents()
    pb.setString_forType_(text, NSStringPboardType)
def act(controller, bundle, options):
    context = cp.get_context(controller)
    line_ending = cp.get_line_ending(context)
    
    direction = cp.get_option(options, 'direction', 'right')
    
    line_text, line_range = cp.lines_and_range(context)
    selection, select_range = cp.selection_and_range(context)
    if select_range.length == 0:
        selection, select_range = cp.words_and_range(context)
    
    cp.say(context, 'word(s)', '||%s||' % selection)
    return
    
    if direction.lower() == 'left':
        prefix = line_text[:(select_range.location - line_range.location)]
        
        if not prefix.strip():
            cp.beep()
            return
        
        # we care about the original length of line after, not the balanced one we'll get in a second
        len_line_after = len(line_after)
        
        line_after, text = cp.balance_line_endings(line_after, text, line_ending)
        line_delta = len(line_after) - len_line_after
        
        select_start = select_range.location + len(line_after)
        select_end = min(select_start + select_range.length, len(context.string()))
        
        text = line_after + text
        
        select_range = cp.new_range(select_start, max(0,select_end - select_start))
        target_range = cp.new_range(target_range.location, max(0, target_range.length + len(line_after) - (len(line_after) - len_line_after)))
    else:
        line_before = cp.get_line_before(context, target_range)
        if line_before is None: return
        
        # we care about the original length of line before, not the balanced one we'll get in a second
        len_line_before = len(line_before)
        
        text, line_before = cp.balance_line_endings(text, line_before, line_ending)
        
        text = text + line_before
        select_range = cp.new_range(select_range.location - len_line_before, select_range.length)
        target_range = cp.new_range(target_range.location - len_line_before, target_range.length + len_line_before)
    
    cp.insert_text_and_select(context, text, target_range, select_range)
Exemple #4
0
def act(controller, bundle, options):
    context = cp.get_context(controller)
    line_ending = cp.get_line_ending(context)

    text, target_range = cp.selection_and_range(context)
    pb = NSPasteboard.generalPasteboard()

    # get the old clipboard contents
    old = pb.stringForType_(NSStringPboardType)

    # copy text to the clipboard
    pb.clearContents()
    pb.setString_forType_(text, NSStringPboardType)

    # replace the current selection with the clipboard contents
    cp.insert_text_and_select(context, old, target_range, cp.new_range(target_range.location, len(old)))
def act(controller, bundle, options):
    '''
    Required action method
    '''
    
    context = cp.get_context(controller)
    
    from_lang = cp.get_option(options, 'from', 'markdown').lower()
    to_lang = cp.get_option(options, 'to', 'html').lower()
    
    selection, range = cp.selection_and_range(context)
    
    # grab the whole document if they haven't selected anything...
    if range.length == 0:
        selection = context.string()
        range = cp.new_range(0, len(selection))
    
    # what are we coming from?
    if from_lang == 'markdown':
        html = markdown(selection)
    elif from_lang == 'textile':
        html = textile(selection)
    elif from_lang == 'rest':
        html = rest2html(selection)
    elif from_lang == 'html':
    	html = selection
    else:
    	return
    
    # what are we going to?
    if to_lang == 'markdown':
    	text = html2text(html)
    elif to_lang == 'textile':
    	text = html2textile(html)
    elif to_lang == 'rest':
        text = html2rest(html)
    elif to_lang == 'html':
    	text = html
    else:
    	return
    
    cp.insert_text(context, text, range)
def act(controller, bundle, options):
    '''
    Required action method
    '''
    
    context = cp.get_context(controller)
    selection, range = cp.selection_and_range(context)
    
    # do nothing unless they selected something
    if range.length == 0:
        return
    
    try:
        text = selection.encode('rot13')
    except LookupError:
        # for python 3... hopefully one of the two will do the trick.
        import rot13
        text = rot13.rot13(selection)
    
    cp.insert_text(context, text, range)
def act(controller, bundle, options):
    '''
    Required action method
    
    Setting decode=True will decode instead of encoding
    '''
    
    context = cp.get_context(controller)
    decode = cp.get_option(options, 'decode', False)
    
    selection, range = cp.selection_and_range(context)
    
    # do nothing unless they selected something
    if range.length == 0:
        return
    
    if decode:
        text = enc.htmlDecode(selection)
    else:
        text = enc.htmlEncode(selection)
    
    cp.insert_text(context, text, range)
def act(controller, bundle, options):
    '''
    Required action method
    
    Set desired capitalization with 'to_case' option
    '''
    
    context = cp.get_context(controller)
    to_case = cp.get_option(options, 'to_case', 'upper').lower()
    line_ending = cp.get_line_ending(context)
    
    text, range = cp.selection_and_range(context)
    
    # if nothing is selected, assume we're talking about the line.
    if range.length == 0:
        text, range = cp.lines_and_range(context)
        
        # we really only want most of this... lines_and_range returns a newline char at the end
        if text.endswith(line_ending):
            range = cp.new_range(range.location, range.length - len(line_ending))
            text = cp.get_selection(context, range)
    
    if to_case == 'upper':
        text = text.upper()
    elif to_case == 'lower':
        text = text.lower()
    elif to_case == 'title':
        text = line_ending.join([titlecase(x) for x in text.split(line_ending)])
    elif to_case == 'sentence':
        text = sentencecase(text)
    elif to_case == 'invert':
        text = text.swapcase()
    else:
        return
    
    # insert and select the resulting insertion
    cp.insert_text_and_select(context, text, range, cp.new_range(range.location, len(text)))