示例#1
0
def extract(self, event=None):
    r'''
    Create child node from the selected body text.

    1. If the selection starts with a section reference, the section
       name becomes the child's headline. All following lines become
       the child's body text. The section reference line remains in
       the original body text.

    2. If the selection looks like a definition line (for the Python,
       JavaScript, CoffeeScript or Clojure languages) the
       class/function/method name becomes the child's headline and all
       selected lines become the child's body text.
       
       You may add additional regex patterns for definition lines using
       @data extract-patterns nodes. Each line of the body text should a
       valid regex pattern. Lines starting with # are comment lines. Use \#
       for patterns starting with #.

    3. Otherwise, the first line becomes the child's headline, and all
       selected lines become the child's body text.
    '''
    c = self
    current = c.p  # Unchanging.
    u, undoType = c.undoer, 'Extract'
    head, lines, tail, oldSel, oldYview = c.getBodyLines()
    if not lines:
        return  # Nothing selected.
    # Remove leading whitespace.
    junk, ws = g.skip_leading_ws_with_indent(lines[0], 0, c.tab_width)
    lines = [g.removeLeadingWhitespace(s, ws, c.tab_width) for s in lines]
    h = lines[0].strip()
    ref_h = extractRef(c, h).strip()
    def_h = extractDef_find(c, lines)
    if ref_h:
        # h,b,middle = ref_h,lines[1:],lines[0]
        # 2012/02/27: Change suggested by vitalije ([email protected])
        h, b, middle = ref_h, lines[1:], ' ' * ws + lines[0]
    elif def_h:
        h, b, middle = def_h, lines, ''
    else:
        h, b, middle = lines[0].strip(), lines[1:], ''
    u.beforeChangeGroup(current, undoType)
    undoData = u.beforeInsertNode(current)
    p = createLastChildNode(c, current, h, ''.join(b))
    u.afterInsertNode(p, undoType, undoData)
    c.updateBodyPane(head,
                     middle,
                     tail,
                     undoType=undoType,
                     oldSel=None,
                     oldYview=oldYview)
    u.afterChangeGroup(current, undoType=undoType)
    p.parent().expand()
    c.redraw(p.parent())  # A bit more convenient than p.
    c.bodyWantsFocus()
示例#2
0
def extract(self, event=None):
    r'''
    Create child node from the selected body text.

    1. If the selection starts with a section reference, the section
       name becomes the child's headline. All following lines become
       the child's body text. The section reference line remains in
       the original body text.

    2. If the selection looks like a definition line (for the Python,
       JavaScript, CoffeeScript or Clojure languages) the
       class/function/method name becomes the child's headline and all
       selected lines become the child's body text.
       
       You may add additional regex patterns for definition lines using
       @data extract-patterns nodes. Each line of the body text should a
       valid regex pattern. Lines starting with # are comment lines. Use \#
       for patterns starting with #.

    3. Otherwise, the first line becomes the child's headline, and all
       selected lines become the child's body text.
    '''
    c = self
    current = c.p # Unchanging.
    u, undoType = c.undoer, 'Extract'
    head, lines, tail, oldSel, oldYview = c.getBodyLines()
    if not lines:
        return # Nothing selected.
    # Remove leading whitespace.
    junk, ws = g.skip_leading_ws_with_indent(lines[0], 0, c.tab_width)
    lines = [g.removeLeadingWhitespace(s, ws, c.tab_width) for s in lines]
    h = lines[0].strip()
    ref_h = extractRef(c, h).strip()
    def_h = extractDef_find(c, lines)
    if ref_h:
        # h,b,middle = ref_h,lines[1:],lines[0]
        # 2012/02/27: Change suggested by vitalije ([email protected])
        h, b, middle = ref_h, lines[1:], ' ' * ws + lines[0]
    elif def_h:
        h, b, middle = def_h, lines, ''
    else:
        h, b, middle = lines[0].strip(), lines[1:], ''
    u.beforeChangeGroup(current, undoType)
    undoData = u.beforeInsertNode(current)
    p = createLastChildNode(c, current, h, ''.join(b))
    u.afterInsertNode(p, undoType, undoData)
    c.updateBodyPane(head, middle, tail,
        undoType=undoType, oldSel=None, oldYview=oldYview)
    u.afterChangeGroup(current, undoType=undoType)
    p.parent().expand()
    c.redraw(p.parent()) # A bit more convenient than p.
    c.bodyWantsFocus()
示例#3
0
    def undent_by(self, s, undent_val):
        '''
        Remove leading whitespace equivalent to undent_val from each line.

        Strict languages: prepend the underindent escape for underindented lines.
        '''
        if self.is_rst:
            return s # Never unindent rst code.
        result = []
        for line in g.splitlines(s):
            lws_s = self.get_str_lws(line)
            lws = g.computeWidth(lws_s, self.tab_width)
            # Add underindentEscapeString only for strict languages.
            if self.strict and not line.isspace() and lws < undent_val:
                # End the underindent count with a period to
                # protect against lines that start with a digit!
                result.append("%s%s.%s" % (
                    self.escape, undent_val-lws, line.lstrip()))
            else:
                s = g.removeLeadingWhitespace(line, undent_val, self.tab_width)
                result.append(s)
        return ''.join(result)
示例#4
0
    def undent_by(self, s, undent_val):
        """
        Remove leading whitespace equivalent to undent_val from each line.

        Strict languages: prepend the underindent escape for underindented lines.
        """
        if self.is_rst:
            return s  # Never unindent rst code.
        result = []
        for line in g.splitlines(s):
            lws_s = self.get_str_lws(line)
            lws = g.computeWidth(lws_s, self.tab_width)
            # Add underindentEscapeString only for strict languages.
            if self.strict and not line.isspace() and lws < undent_val:
                # End the underindent count with a period to
                # protect against lines that start with a digit!
                result.append("%s%s.%s" % (
                    self.escape, undent_val - lws, line.lstrip()))
            else:
                s = g.removeLeadingWhitespace(line, undent_val, self.tab_width)
                result.append(s)
        return ''.join(result)
示例#5
0
def extract(self, event=None):
    #@+<< docstring for extract command >>
    #@+node:ekr.20201113130021.1: *3* << docstring for extract command >>
    r"""
    Create child node from the selected body text.

    1. If the selection starts with a section reference, the section
       name becomes the child's headline. All following lines become
       the child's body text. The section reference line remains in
       the original body text.

    2. If the selection looks like a definition line (for the Python,
       JavaScript, CoffeeScript or Clojure languages) the
       class/function/method name becomes the child's headline and all
       selected lines become the child's body text.

       You may add additional regex patterns for definition lines using
       @data extract-patterns nodes. Each line of the body text should a
       valid regex pattern. Lines starting with # are comment lines. Use \#
       for patterns starting with #.

    3. Otherwise, the first line becomes the child's headline, and all
       selected lines become the child's body text.
    """
    #@-<< docstring for extract command >>
    c, u, w = self, self.undoer, self.frame.body.wrapper
    undoType = 'Extract'
    # Set data.
    head, lines, tail, oldSel, oldYview = c.getBodyLines()
    if not lines:
        return  # Nothing selected.
    #
    # Remove leading whitespace.
    junk, ws = g.skip_leading_ws_with_indent(lines[0], 0, c.tab_width)
    lines = [g.removeLeadingWhitespace(s, ws, c.tab_width) for s in lines]
    h = lines[0].strip()
    ref_h = extractRef(c, h).strip()
    def_h = extractDef_find(c, lines)
    if ref_h:
        h, b, middle = ref_h, lines[1:], ' ' * ws + lines[0]  # By vitalije.
    elif def_h:
        h, b, middle = def_h, lines, ''
    else:
        h, b, middle = lines[0].strip(), lines[1:], ''
    #
    # Start the outer undo group.
    u.beforeChangeGroup(c.p, undoType)
    undoData = u.beforeInsertNode(c.p)
    p = createLastChildNode(c, c.p, h, ''.join(b))
    u.afterInsertNode(p, undoType, undoData)
    #
    # Start inner undo.
    if oldSel:
        i, j = oldSel
        w.setSelectionRange(i, j, insert=j)
    bunch = u.beforeChangeBody(c.p)  # Not p.
    #
    # Update the text and selection
    c.p.v.b = head + middle + tail  # Don't redraw.
    w.setAllText(head + middle + tail)
    i = len(head)
    j = max(i, len(head) + len(middle) - 1)
    w.setSelectionRange(i, j, insert=j)
    #
    # End the inner undo.
    u.afterChangeBody(c.p, undoType, bunch)
    #
    # Scroll as necessary.
    if oldYview:
        w.setYScrollPosition(oldYview)
    else:
        w.seeInsertPoint()
    #
    # Add the changes to the outer undo group.
    u.afterChangeGroup(c.p, undoType=undoType)
    p.parent().expand()
    c.redraw(p.parent())  # A bit more convenient than p.
    c.bodyWantsFocus()