예제 #1
0
 def dynamicCompletion(self, event=None):
     '''
     dabbrev-completion
     Insert the common prefix of all dynamic abbrev's matching the present word.
     This corresponds to C-M-/ in Emacs.
     '''
     c, p, u = self.c, self.c.p, self.c.p.v.u
     w = self.editWidget(event)
     if not w: return
     s = w.getAllText()
     ins = ins1 = w.getInsertPoint()
     if 0 < ins < len(s) and not g.isWordChar(s[ins]): ins1 -= 1
     i, j = g.getWord(s, ins1)
     word = w.get(i, j)
     aList = self.getDynamicList(w, word)
     if not aList: return
     # Bug fix: remove s itself, otherwise we can not extend beyond it.
     if word in aList and len(aList) > 1:
         aList.remove(word)
     prefix = functools.reduce(g.longestCommonPrefix, aList)
     if prefix.strip():
         ypos = w.getYScrollPosition()
         b = c.undoer.beforeChangeNodeContents(p, oldYScroll=ypos)
         s = s[: i] + prefix + s[j:]
         w.setAllText(s)
         w.setInsertPoint(i + len(prefix))
         w.setYScrollPosition(ypos)
         c.undoer.afterChangeNodeContents(p,
             command='dabbrev-completion', bunch=b, dirtyVnodeList=[])
         c.recolor()
예제 #2
0
    def dynamicExpansion(self, event=None):
        '''
        dabbrev-expands (M-/ in Emacs).

        Inserts the longest common prefix of the word at the cursor. Displays
        all possible completions if the prefix is the same as the word.
        '''
        trace = False and not g.unitTesting
        c = self.c
        p = c.p
        w = self.editWidget(event)
        if not w:
            if trace: g.trace('no widget!')
            return
        s = w.getAllText()
        ins = ins1 = w.getInsertPoint()
        if 0 < ins < len(s) and not g.isWordChar(s[ins]): ins1 -= 1
        i, j = g.getWord(s, ins1)
        w.setInsertPoint(j)
            # This allows the cursor to be placed anywhere in the word.
        word = w.get(i, j)
        aList = self.getDynamicList(w, word)
        if not aList:
            if trace: g.trace('empty completion list')
            return
        if word in aList and len(aList) > 1:
            aList.remove(word)
        prefix = functools.reduce(g.longestCommonPrefix, aList)
        prefix = prefix.strip()
        if trace: g.trace(word, prefix, aList)
        self.dynamicExpandHelper(event, prefix, aList, w)
예제 #3
0
    def dynamicExpandHelper1(self, event):

        c, k = self.c, self.c.k
        p = c.p
        c.frame.log.deleteTab('Completion')
        k.clearState()
        k.resetLabel()
        if k.arg:
            w = self.w
            s = w.getAllText()
            ypos = w.getYScrollPosition()
            b = c.undoer.beforeChangeNodeContents(p, oldYScroll=ypos)
            ins = ins1 = w.getInsertPoint()
            if 0 < ins < len(s) and not g.isWordChar(s[ins]): ins1 -= 1
            i, j = g.getWord(s, ins1)
            # word = s[i: j]
            s = s[:i] + k.arg + s[j:]
            w.setAllText(s)
            w.setInsertPoint(i + len(k.arg))
            w.setYScrollPosition(ypos)
            c.undoer.afterChangeNodeContents(p,
                                             command='dabbrev-expand',
                                             bunch=b,
                                             dirtyVnodeList=[])
            c.recolor()
예제 #4
0
 def findNextWord(self, p):
     """Scan for the next word, leaving the result in the work widget"""
     trace = False and not g.unitTesting
     c = self.c; p = p.copy()
     while 1:
         s = self.workCtrl.getAllText()
         i = self.workCtrl.getInsertPoint()
         while i < len(s) and not g.isWordChar1(s[i]):
             i += 1
         # g.trace('p',p and p.h,'i',i,'len(s)',len(s))
         if i < len(s):
             # A non-empty word has been found.
             j = i
             while j < len(s) and (g.isWordChar(s[j]) or s[j] == "'"):
                 j += 1
             word = s[i: j]
             word = word.strip("'")
             # This trace verifies that all words have been checked.
             # g.trace(repr(word))
             for w in (self.workCtrl, c.frame.body.wrapper):
                 c.widgetWantsFocusNow(w)
                 w.setSelectionRange(i, j, insert=j)
             if trace: g.trace(i, j, word, p.h)
             return i, j, p, word
         else:
             # End of the body text.
             p.moveToThreadNext()
             if not p: break
             self.workCtrl.delete(0, 'end')
             self.workCtrl.insert(0, p.b)
             for w in (self.workCtrl, c.frame.body.wrapper):
                 c.widgetWantsFocusNow(w)
                 w.setSelectionRange(0, 0, insert=0)
             if trace: g.trace(0, 0, '-->', p.h)
     return None, None, None, None
예제 #5
0
 def findNextWord(self, p):
     """Scan for the next word, leaving the result in the work widget"""
     trace = False and not g.unitTesting
     c = self.c; p = p.copy()
     while 1:
         s = self.workCtrl.getAllText()
         i = self.workCtrl.getInsertPoint()
         while i < len(s) and not g.isWordChar1(s[i]):
             i += 1
         # g.trace('p',p and p.h,'i',i,'len(s)',len(s))
         if i < len(s):
             # A non-empty word has been found.
             j = i
             while j < len(s) and (g.isWordChar(s[j]) or s[j] == "'"):
                 j += 1
             word = s[i: j]
             word = word.strip("'")
             # This trace verifies that all words have been checked.
             # g.trace(repr(word))
             for w in (self.workCtrl, c.frame.body.wrapper):
                 c.widgetWantsFocusNow(w)
                 w.setSelectionRange(i, j, insert=j)
             if trace: g.trace(i, j, word, p.h)
             return i, j, p, word
         else:
             # End of the body text.
             p.moveToThreadNext()
             if not p: break
             self.workCtrl.delete(0, 'end')
             self.workCtrl.insert(0, p.b)
             for w in (self.workCtrl, c.frame.body.wrapper):
                 c.widgetWantsFocusNow(w)
                 w.setSelectionRange(0, 0, insert=0)
             if trace: g.trace(0, 0, '-->', p.h)
     return None, None, None, None
예제 #6
0
 def dynamicExpandHelper(self, event, prefix=None, aList=None, w=None):
     '''State handler for dabbrev-expands command.'''
     trace = False and not g.unitTesting
     c, k = self.c, self.c.k
     p = c.p
     tag = 'dabbrev-expand'
     state = k.getState(tag)
     if state == 0:
         self.w = w
         prefix2 = 'dabbrev-expand: '
         c.frame.log.deleteTab('Completion')
         g.es('', '\n'.join(aList), tabName='Completion')
         # Protect only prefix2.
         # This is required for tab completion and backspace to work properly.
         k.setLabelBlue(prefix2, protect=True)
         k.setLabelBlue(prefix2 + prefix, protect=False)
         if trace: g.trace('len(aList)', len(aList))
         k.getArg(event,
                  tag,
                  1,
                  self.dynamicExpandHelper,
                  tabList=aList,
                  prefix=prefix)
     else:
         c.frame.log.deleteTab('Completion')
         k.clearState()
         k.resetLabel()
         if k.arg:
             w = self.w
             s = w.getAllText()
             ypos = w.getYScrollPosition()
             b = c.undoer.beforeChangeNodeContents(p, oldYScroll=ypos)
             ins = ins1 = w.getInsertPoint()
             if 0 < ins < len(s) and not g.isWordChar(s[ins]): ins1 -= 1
             i, j = g.getWord(s, ins1)
             word = s[i:j]
             s = s[:i] + k.arg + s[j:]
             if trace:
                 g.trace('ins', ins, 'k.arg', repr(k.arg), 'word', word)
             w.setAllText(s)
             w.setInsertPoint(i + len(k.arg))
             w.setYScrollPosition(ypos)
             c.undoer.afterChangeNodeContents(p,
                                              command=tag,
                                              bunch=b,
                                              dirtyVnodeList=[])
             c.recolor()
예제 #7
0
 def dynamicExpandHelper(self, event, prefix=None, aList=None, w=None):
     '''State handler for dabbrev-expands command.'''
     trace = False and not g.unitTesting
     c, k = self.c, self.c.k
     p = c.p
     tag = 'dabbrev-expand'
     state = k.getState(tag)
     if state == 0:
         self.w = w
         prefix2 = 'dabbrev-expand: '
         c.frame.log.deleteTab('Completion')
         g.es('', '\n'.join(aList), tabName='Completion')
         # Protect only prefix2.
         # This is required for tab completion and backspace to work properly.
         k.setLabelBlue(prefix2, protect=True)
         k.setLabelBlue(prefix2 + prefix, protect=False)
         if trace: g.trace('len(aList)', len(aList))
         k.getArg(event, tag, 1, self.dynamicExpandHelper, tabList=aList, prefix=prefix)
     else:
         c.frame.log.deleteTab('Completion')
         k.clearState()
         k.resetLabel()
         if k.arg:
             w = self.w
             s = w.getAllText()
             ypos = w.getYScrollPosition()
             b = c.undoer.beforeChangeNodeContents(p, oldYScroll=ypos)
             ins = ins1 = w.getInsertPoint()
             if 0 < ins < len(s) and not g.isWordChar(s[ins]): ins1 -= 1
             i, j = g.getWord(s, ins1)
             word = s[i: j]
             s = s[: i] + k.arg + s[j:]
             if trace: g.trace('ins', ins, 'k.arg', repr(k.arg), 'word', word)
             w.setAllText(s)
             w.setInsertPoint(i + len(k.arg))
             w.setYScrollPosition(ypos)
             c.undoer.afterChangeNodeContents(p,
                 command=tag, bunch=b, dirtyVnodeList=[])
             c.recolor()
예제 #8
0
 def dynamicExpandHelper1(self, event):
     trace = False and not g.unitTesting
     c, k = self.c, self.c.k
     p = c.p
     c.frame.log.deleteTab('Completion')
     k.clearState()
     k.resetLabel()
     if k.arg:
         w = self.w
         s = w.getAllText()
         ypos = w.getYScrollPosition()
         b = c.undoer.beforeChangeNodeContents(p, oldYScroll=ypos)
         ins = ins1 = w.getInsertPoint()
         if 0 < ins < len(s) and not g.isWordChar(s[ins]): ins1 -= 1
         i, j = g.getWord(s, ins1)
         word = s[i: j]
         s = s[: i] + k.arg + s[j:]
         if trace: g.trace('ins', ins, 'k.arg', repr(k.arg), 'word', word)
         w.setAllText(s)
         w.setInsertPoint(i + len(k.arg))
         w.setYScrollPosition(ypos)
         c.undoer.afterChangeNodeContents(p,
             command='dabbrev-expand', bunch=b, dirtyVnodeList=[])
         c.recolor()