예제 #1
0
파일: sortlines.py 프로젝트: editxt/editxt
def sortlines(editor, opts):
    if opts.sort_regex[0]:
        regex = re.compile(opts.sort_regex[0], flags=opts.sort_regex[0].flags)
        if opts.sort_regex[1]:
            groups = [int(g.strip()) for g in opts.sort_regex[1].split("\\") if g.strip()]
        else:
            groups = None
    else:
        regex = None
    def key(line):
        if opts.ignore_leading_whitespace:
            line = line.lstrip()
        if opts.ignore_case:
            line = line.lower()
        if regex is not None:
            match = regex.search(line)
            if match is None:
                line = (1,)
            elif not match.groups():
                line = (0, match.group(0))
            else:
                matched = match.groups("")
                if groups:
                    matched = dict(enumerate(matched))
                    matched = tuple(matched.get(g - 1, "") for g in groups)
                line = (0,) + matched
        return line
    text = editor.text
    if opts.selection:
        range = text.line_range(editor.selection)
    else:
        range = (0, len(text))
    output = "".join(sorted(iterlines(text, range), key=key, reverse=opts.reverse))
    editor.put(output, range, select=opts.selection)
예제 #2
0
파일: unique.py 프로젝트: editxt/editxt
def unique_lines(editor, args):
    """Remove duplicate lines"""
    text = editor.text
    if args.selection:
        range = text.line_range(editor.selection)
    else:
        range = (0, len(text))
    output = "".join(unique(iterlines(text, range)))
    editor.put(output, range, select=args.selection)
예제 #3
0
def unique_lines(editor, args):
    """Remove duplicate lines"""
    textview = editor.text_view
    text = textview.string()
    if args.selection:
        range = text.lineRangeForRange_(editor.selection)
    else:
        range = (0, len(text))
    output = "".join(unique(iterlines(text, range)))
    if textview.shouldChangeTextInRange_replacementString_(range, output):
        textview.textStorage().replaceCharactersInRange_withString_(range, output)
        textview.didChangeText()
        if args.selection:
            if len(output) != editor.selection[1]:
                range = (range[0], len(output))
            editor.selection = range
예제 #4
0
파일: sortlines.py 프로젝트: khairy/editxt
def sortlines(textview, opts):
    text = textview.string()
    if opts.sort_regex[0]:
        regex = re.compile(opts.sort_regex[0], flags=opts.sort_regex[0].flags)
        if opts.sort_regex[1]:
            groups = [
                int(g.strip()) for g in opts.sort_regex[1].split("\\")
                if g.strip()
            ]
        else:
            groups = None
    else:
        regex = None

    def key(line):
        if opts.ignore_leading_whitespace:
            line = line.lstrip()
        if opts.ignore_case:
            line = line.lower()
        if regex is not None:
            match = regex.search(line)
            if match is None:
                line = (1, )
            elif not match.groups():
                line = (0, match.group(0))
            else:
                matched = match.groups("")
                if groups:
                    matched = dict(enumerate(matched))
                    matched = tuple(matched.get(g - 1, "") for g in groups)
                line = (0, ) + matched
        return line

    if opts.selection:
        range = text.lineRangeForRange_(textview.selectedRange())
    else:
        range = (0, len(text))
    output = "".join(
        sorted(iterlines(text, range), key=key, reverse=opts.reverse))
    if textview.shouldChangeTextInRange_replacementString_(range, output):
        textview.textStorage().replaceCharactersInRange_withString_(
            range, output)
        textview.didChangeText()
        if opts.selection:
            textview.setSelectedRange_(range)
예제 #5
0
파일: sortlines.py 프로젝트: khairy/editxt
def sortlines(textview, opts):
    text = textview.string()
    if opts.sort_regex[0]:
        regex = re.compile(opts.sort_regex[0], flags=opts.sort_regex[0].flags)
        if opts.sort_regex[1]:
            groups = [int(g.strip()) for g in opts.sort_regex[1].split("\\") if g.strip()]
        else:
            groups = None
    else:
        regex = None
    def key(line):
        if opts.ignore_leading_whitespace:
            line = line.lstrip()
        if opts.ignore_case:
            line = line.lower()
        if regex is not None:
            match = regex.search(line)
            if match is None:
                line = (1,)
            elif not match.groups():
                line = (0, match.group(0))
            else:
                matched = match.groups("")
                if groups:
                    matched = dict(enumerate(matched))
                    matched = tuple(matched.get(g - 1, "") for g in groups)
                line = (0,) + matched
        return line
    if opts.selection:
        range = text.lineRangeForRange_(textview.selectedRange())
    else:
        range = (0, len(text))
    output = "".join(sorted(iterlines(text, range), key=key, reverse=opts.reverse))
    if textview.shouldChangeTextInRange_replacementString_(range, output):
        textview.textStorage().replaceCharactersInRange_withString_(range, output)
        textview.didChangeText()
        if opts.selection:
            textview.setSelectedRange_(range)
예제 #6
0
 def open_(self, sender):
     from editxt import app
     paths = iterlines(self.paths.textStorage().string())
     app.open_documents_with_paths([p.strip() for p in paths if p.strip()])
     self.window().orderOut_(self)
예제 #7
0
 def open_(self, sender):
     paths = iterlines(self.paths.textStorage().string())
     self.app.open_documents_with_paths(
         p.strip() for p in paths if p.strip())
     self.window().orderOut_(self)
예제 #8
0
 def open_(self, sender):
     from editxt import app
     paths = iterlines(self.paths.textStorage().string())
     app.open_documents_with_paths([p.strip() for p in paths if p.strip()])
     self.window().orderOut_(self)