Esempio n. 1
0
def act(controller, bundle, options):
    context = cp.get_context(controller)
    line_ending = cp.get_line_ending(context)
    
    direction = cp.get_option(options, 'direction', 'down')
    duplicate = cp.get_option(options, 'duplicate', False)
    
    text, target_range = cp.lines_and_range(context)
    select_range = cp.get_range(context)
    
    if duplicate:
        cloned = text
        
        if not cloned.endswith(line_ending):
            cloned += line_ending
        
        if direction.lower() == 'down':
            select_range = cp.new_range(select_range.location + len(cloned), select_range.length)
        
        text = cloned + text
    else:
        if direction.lower() == 'down':
            line_after = cp.get_line_after(context, target_range)
            if line_after is None: 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)
Esempio n. 2
0
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)
Esempio n. 3
0
def act(controller, bundle, options):
    context = cp.get_context(controller)
    line_ending = cp.get_line_ending(context)

    text, target_range = cp.lines_and_range(context)

    cp.insert_text_and_select(context, "", target_range, cp.new_range(target_range.location, 0))
Esempio n. 4
0
def act(controller, bundle, options):
    context = cp.get_context(controller)
    line_ending = cp.get_line_ending(context)

    direction = cp.get_option(options, 'direction', 'down')

    selection, current_range = cp.lines_and_range(context)

    if direction == 'down':
        target = select = cp.new_range(current_range.location + current_range.length, 0)
        if not selection.endswith(line_ending):
            select = cp.new_range(select.location + 1, 0)

    else:
        target = cp.new_range(max(current_range.location - 1, 0), 0)
        select = cp.new_range(current_range.location, 0)

    cp.insert_text_and_select(context, line_ending, target, select)
Esempio n. 5
0
def act(controller, bundle, options):
    context = cp.get_context(controller)
    line_ending = cp.get_line_ending(context)
    
    selection, current_range = cp.lines_and_range(context)
    insert_range = cp.new_range(current_range.location + current_range.length, 0)
    
    # copy text from the clipboard
    text = NSPasteboard.generalPasteboard().stringForType_(NSStringPboardType)
    
    if not text:
        return
    
    if not selection.endswith(line_ending):
        text = line_ending + text
    elif not text.endswith(line_ending):
        text = text + line_ending
    
    cp.insert_text_and_select(context, text, insert_range, cp.new_range(insert_range.location, len(text)))
Esempio n. 6
0
def act(controller, bundle, options):
    '''
    Required action method
    '''

    context = cp.get_context(controller)
    line_ending = cp.get_line_ending(context)
    lines, range = cp.lines_and_range(context)

    newlines = line_ending.join(balance_operators(lines.split(line_ending)))
    cp.insert_text_and_select(context, newlines, range, cp.new_range(range.location, len(newlines)))
Esempio n. 7
0
def act(controller, bundle, options):
    '''
    Required action method
    '''

    context = cp.get_context(controller)
    line_ending = cp.get_line_ending(context)

    select_range = range = cp.get_range(context)
    if range.length == 0:
        range = cp.new_range(0, len(context.string()))

    lines, range = cp.lines_and_range(context, range)

    newlines = line_ending.join([re.sub("^(.*?)\s*([;,]?)\s*$", '\\1\\2', x) for x in lines.split(line_ending)])

    if select_range.length == 0:
        prefix = line_ending.join([re.sub("^(.*?)\s*([;,]?)\s*$", '\\1\\2', x) for x in lines[0:select_range.location].split(line_ending)])
        cp.insert_text_and_select(context, newlines, range, cp.new_range(len(prefix), 0))
    else:
        cp.insert_text_and_select(context, newlines, range, cp.new_range(range.location, len(newlines)))
Esempio n. 8
0
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)))
Esempio n. 9
0
def act(controller, bundle, options):
    '''
    Required action method
    '''

    context = cp.get_context(controller)
    line_ending = cp.get_line_ending(context)
    lines, range = cp.lines_and_range(context)

    if lines.endswith(line_ending):
        lines = lines[:-len(line_ending)]
        range = cp.new_range(range.location, range.length - len(line_ending))

    try:
        newlines = line_ending.join(wrap_comment(
            lines.split(line_ending),
            context.tabWidth(),
            cp.get_option(options, 'width'),
            cp.get_option(options, 'min_width')
        ))
        cp.insert_text_and_select(context, newlines, range, cp.new_range(range.location, len(newlines)))
    except:
        cp.beep()
        raise
Esempio n. 10
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)))
Esempio n. 11
0
def act(controller, bundle, options):
    '''
    Required action method
    
    Supplying a lang option will override the automatic language guessing
    (which might not be such a bad thing...)
    '''
    
    context = cp.get_context(controller)

    lang = cp.get_option(options, 'lang', 'auto').lower()
    
    # get the file extension so we can guess the language.
    if lang == 'auto':
        path = context.path()
        if path is not None:
            pos = path.rfind('.')
            if pos != -1:
                lang = path[pos+1:]
    
    d = Docblock.get(lang)
    
    # get the current line
    text, target_range = cp.lines_and_range(context)
    
    # keep going until we find a non-empty line to document (up to X lines below the current line)
    tries_left = 3
    while tries_left and not text.strip():
        text, target_range = cp.get_line_after_and_range(context, target_range)
        
        if text is None:
            # we're at the end of the document?
            cp.beep()
            return
        
        tries_left -= 1
    
    insert_range = cp.new_range(target_range.location, 0)
    
    d.setLineEnding(cp.get_line_ending(context))
    docblock = d.doc(text)
    
    if docblock:
        cp.insert_text_with_insertion_point(context, docblock, insert_range)
    else:
        cp.beep()
Esempio n. 12
0
def act(controller, bundle, options):
    '''
    Required action method
    
    Supplying a lang option will override the automatic language guessing
    (which might not be such a bad thing...)
    '''

    context = cp.get_context(controller)

    lang = cp.get_option(options, 'lang', 'auto').lower()

    # get the file extension so we can guess the language.
    if lang == 'auto':
        path = context.path()
        if path is not None:
            pos = path.rfind('.')
            if pos != -1:
                lang = path[pos + 1:]

    d = Docblock.get(lang)

    # get the current line
    text, target_range = cp.lines_and_range(context)

    # keep going until we find a non-empty line to document (up to X lines below the current line)
    tries_left = 3
    while tries_left and not text.strip():
        text, target_range = cp.get_line_after_and_range(context, target_range)

        if text is None:
            # we're at the end of the document?
            cp.beep()
            return

        tries_left -= 1

    insert_range = cp.new_range(target_range.location, 0)

    d.setLineEnding(cp.get_line_ending(context))
    docblock = d.doc(text)

    if docblock:
        cp.insert_text_with_insertion_point(context, docblock, insert_range)
    else:
        cp.beep()
Esempio n. 13
0
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)
Esempio n. 14
0
def act(controller, bundle, options):
    '''
    Required action method
    
    if supplied, message will be written instead of Hello World
    
    Setting replace=True replace the current selection instead of inserting
    '''
    
    context = cp.get_context(controller)
    
    message = cp.get_option(options, 'message', 'Hello World')
    replace_selection = cp.get_option(options, 'replace', False)
    
    range = cp.get_range(context)
    
    if not replace_selection:
        range = cp.new_range(range.location, 0)
    
    cp.insert_text(context, message, range)