Hello everyone.
In editors that I have used before discovering Leo there were commands
for completing a word with similar word either before or after
insertion point. I could not find those commands in Leo so I have
written them. In case that anybody would like to use them here are
copies of those two nodes.
CUT FROM HERE
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet ekr_test?>
<leo_file>
<leo_header file_format="2"/>
<vnodes>
<v t="vitalije.20081125152732.3"><vh>@command complete-previous</vh>
<v t="vitalije.20081125152732.4"><vh>WordCompleter</vh>
<v t="vitalije.20081125152732.5" a="E"><vh>complete_word</vh>
<v t="vitalije.20081125152732.6"><vh><<backward search>></
vh>
<v t="vitalije.20081125154636.1"><vh><<counter check>></
vh></v>
<v t="vitalije.20081125152732.7"><vh><<clean up and
exit>></vh></v>
</v>
<v t="vitalije.20081125152732.8" a="E"><vh><<forward
search>></vh>
<v t="vitalije.20081125154636.1"></v>
<v t="vitalije.20081125152732.7"></v>
</v>
</v>
<v t="vitalije.20081125152732.9"><vh>acceptable_word</vh></v>
<v t="vitalije.20081125152732.10"><vh>undo_replacement</vh></v>
<v t="vitalije.20081125152732.11"><vh>redo_replacement</vh></v>
<v t="vitalije.20081125152732.12"><vh>exit</vh></v>
<v t="vitalije.20081125152732.13"><vh>run</vh></v>
<v t="vitalije.20081125152732.14"><vh>adjust</vh></v>
</v>
<v t="vitalije.20081125152732.15"><vh>getCurrentWord</vh></v>
</v>
</vnodes>
<tnodes>
<t tx="vitalije.20081125152732.3">wordsep = u'., -+\n\r[]{}<>=-
+*&[EMAIL PROTECTED]"\'?/\\|^()~`:;'
word_completer_key = 'back_word_completer'
@others
completer = g.app.config.get(c,word_completer_key,'WordCompleter')
if completer is None:
completer = WordCompleter(p,c,back=True)
g.app.config.set(c,word_completer_key,'WordCompleter',completer)
completer.run()
else:
completer.adjust(p, c.frame.body.getInsertPoint())
completer.complete_word()
</t>
<t tx="vitalije.20081125152732.4">class WordCompleter:
def __init__(self, p, c, back=True):
self.p = p.copy()
self.c = c
self.back = back
self.pos = -1
@others
</t>
<t tx="vitalije.20081125152732.5">def complete_word(self):
txt = self.chunk
word = self.word
try:
if self.back:
<<backward search>>
else:
<<forward search>>
except:
self.exit()
g.es_exception()</t>
<t tx="vitalije.20081125152732.6">p = self.search_pos
start = -1
counter = 0
while p:
<<counter check>>
i = txt.rfind(word, 0, start)
if i == -1:
p.moveToThreadBack()
if p:self.chunk = txt = p.bodyString()
start = -1
else:
if self.acceptable_word(i, txt, word):
self.chunk = txt[:i+len(word)-1]
return
start = i+len(word)-1
<<clean up and exit>></t>
<t tx="vitalije.20081125152732.7">if len(self.tried) > 1:
# there was some tries so we need to restore
self.c.setBodyString(self.p, self.before+self.word+self.after)
self.c.frame.body.setInsertPoint(self.pos)
return self.exit()</t>
<t tx="vitalije.20081125152732.8">p = self.search_pos
start = 0
counter = 0
while p:
<<counter check>>
i = txt.find(word, start)
if i == -1:
p.moveToThreadNext()
if p:self.chunk = txt = p.bodyString()
start = 0
else:
if self.acceptable_word(i, txt, word):
self.chunk = txt[i+1:]
return
start = i+1
<<clean up and exit>></t>
<t tx="vitalije.20081125152732.9">@ if found word for the first time
then try it
@c
def acceptable_word(self, i, txt, word):
if i == 0 or wordsep.find(txt[i-1]) != -1:
j = i+len(word)
while j < len(txt) and wordsep.find(txt[j]) < 0:
j += 1
nword = txt[i:j]
if nword not in self.tried:
self.tried[nword] = 1
u = self.c.undoer
bunch = u.createCommonBunch(p)
bunch.oldBody = p.bodyString()
bunch.insertPos = self.pos
# Set the type & helpers.
bunch.kind = 'node'
bunch.undoType = 'complete word'
bunch.undoHelper = self.undo_replacement
bunch.redoHelper = self.redo_replacement
bunch.newBody = newBody = self.before+nword+self.after
self.c.setBodyString(self.p, newBody)
self.c.frame.body.setInsertPoint(self.pos)
bunch.dirtyVnodeList = [p.v]
bunch.newChanged = u.c.isChanged()
bunch.newDirty = p.isDirty()
bunch.newMarked = p.isMarked()
u.pushBead(bunch)
return True
return False
</t>
<t tx="vitalije.20081125152732.10">def undo_replacement(self):
u = self.c.undoer; c=self.c; p=u.p
bunch = u.getBead(u.bead)
c.setBodyString(p, bunch.oldBody)
c.frame.body.setInsertPoint(bunch.insertPos)</t>
<t tx="vitalije.20081125152732.11">def redo_replacement(self):
c = self.c; u=c.undoer; bunch=u.getBead(u.bead+1)
c.setBodyString(bunch.p, bunch.newBody)
c.frame.body.setInsertPoint(bunch.insertPos)</t>
<t tx="vitalije.20081125152732.12">def exit(self):
# this was used during experiments
#g.app.config.set(c,word_completer_key,'WordCompleter',None)
#g.app.config.set(c,'next_word_completer','WordCompleter',None)
pass</t>
<t tx="vitalije.20081125152732.13">def run(self):
self.adjust(self.p, c.frame.body.getInsertPoint())
self.complete_word()
</t>
<t tx="vitalije.20081125152732.14">def adjust(self, p, pos):
if p != self.p or pos != self.pos:
self.p = p.copy()
self.pos = pos
bs = p.bodyString()
word = getCurrentWord(bs, pos)
self.word = word
self.before = bs[:pos-len(word)]
self.after = bs[pos:]
self.search_pos = p.copy()
if self.back:
self.chunk = self.before
else:
self.chunk = self.after
self.tried = {word:1}</t>
<t tx="vitalije.20081125152732.15">def getCurrentWord(s, pos):
i = pos-1
while i>0 and wordsep.find(s[i]) < 0:
i -= 1
return s[i+1:pos]</t>
<t tx="vitalije.20081125154636.1"># I had troubles with neverending
loop so this is just preventive
counter+=1
if counter > 10000:
g.es_trace("counter max")
break</t>
</tnodes>
</leo_file>
CUT TO HERE
and the other one
CUT FROM HERE
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet ekr_test?>
<leo_file>
<leo_header file_format="2"/>
<vnodes>
<v t="vitalije.20081125152732.16"><vh>@command complete-next</vh>
<v t="vitalije.20081125152732.17"><vh>WordCompleter</vh>
<v t="vitalije.20081125152732.18" a="E"><vh>complete_word</vh>
<v t="vitalije.20081125152732.19" a="E"><vh><<backward
search>></vh>
<v t="vitalije.20081125152732.20"><vh><<clean up and
exit>></vh></v>
</v>
<v t="vitalije.20081125152732.21" a="E"><vh><<forward
search>></vh>
<v t="vitalije.20081125152732.20"></v>
</v>
</v>
<v t="vitalije.20081125152732.22"><vh>acceptable_word</vh></v>
<v t="vitalije.20081125152732.23"><vh>undo_replacement</vh></v>
<v t="vitalije.20081125152732.24"><vh>redo_replacement</vh></v>
<v t="vitalije.20081125152732.25"><vh>exit</vh></v>
<v t="vitalije.20081125152732.26"><vh>run</vh></v>
<v t="vitalije.20081125152732.27"><vh>adjust</vh></v>
</v>
<v t="vitalije.20081125152732.28"><vh>getCurrentWord</vh></v>
</v>
</vnodes>
<tnodes>
<t tx="vitalije.20081125152732.16">wordsep = u'., -+\n\r[]{}<>=-
+*&[EMAIL PROTECTED]"\'?/\\|^()~`:;'
word_completer_key = 'next_word_completer'
@others
completer = g.app.config.get(c,word_completer_key,'WordCompleter')
if completer is None:
completer = WordCompleter(p, c, back=False)
g.app.config.set(c, word_completer_key, 'WordCompleter',
completer)
completer.run()
else:
completer.adjust(p, c.frame.body.getInsertPoint())
completer.complete_word()
</t>
<t tx="vitalije.20081125152732.17">class WordCompleter:
def __init__(self, p, c, back=True):
self.p = p.copy()
self.c = c
self.back = back
self.pos = -1
@others
</t>
<t tx="vitalije.20081125152732.18">def complete_word(self):
txt = self.chunk
word = self.word
try:
if self.back:
<<backward search>>
else:
<<forward search>>
except:
self.exit()
g.es_exception()</t>
<t tx="vitalije.20081125152732.19">p = self.search_pos
start = -1
counter = 0
while p:
counter+=1
if counter > 10000:
g.es_trace("counter max")
break
i = txt.rfind(word, 0, start)
if i == -1:
p.moveToThreadBack()
if p:self.chunk = txt = p.bodyString()
start = -1
else:
if self.acceptable_word(i, txt, word):
self.chunk = txt[:i+len(word)-1]
return
start = i+len(word)-1
<<clean up and exit>></t>
<t tx="vitalije.20081125152732.20">if len(self.tried) > 1:
# there was some tries so we need to restore
self.c.setBodyString(self.p, self.before+self.word+self.after)
self.c.frame.body.setInsertPoint(self.pos)
return self.exit()</t>
<t tx="vitalije.20081125152732.21">p = self.search_pos
start = 0
counter = 0
while p:
counter += 1
if counter > 10000:
g.es_trace("counter max")
break
i = txt.find(word, start)
if i == -1:
p.moveToThreadNext()
if p:self.chunk = txt = p.bodyString()
start = 0
else:
if self.acceptable_word(i, txt, word):
self.chunk = txt[i+1:]
return
start = i+1
<<clean up and exit>></t>
<t tx="vitalije.20081125152732.22">@ if found word for the first time
then try it
@c
def acceptable_word(self, i, txt, word):
if i == 0 or wordsep.find(txt[i-1]) != -1:
j = i+len(word)
while j < len(txt) and wordsep.find(txt[j]) < 0:
j += 1
nword = txt[i:j]
if nword not in self.tried:
self.tried[nword] = 1
u = self.c.undoer
bunch = u.createCommonBunch(p)
bunch.oldBody = p.bodyString()
bunch.insertPos = self.pos
# Set the type & helpers.
bunch.kind = 'node'
bunch.undoType = 'complete word'
bunch.undoHelper = self.undo_replacement
bunch.redoHelper = self.redo_replacement
bunch.newBody = newBody = self.before+nword+self.after
self.c.setBodyString(self.p, newBody)
self.c.frame.body.setInsertPoint(self.pos)
bunch.dirtyVnodeList = [p.v]
bunch.newChanged = u.c.isChanged()
bunch.newDirty = p.isDirty()
bunch.newMarked = p.isMarked()
u.pushBead(bunch)
return True
return False
</t>
<t tx="vitalije.20081125152732.23">def undo_replacement(self):
u = self.c.undoer; c=self.c; p=u.p
bunch = u.getBead(u.bead)
c.setBodyString(p, bunch.oldBody)
c.frame.body.setInsertPoint(bunch.insertPos)</t>
<t tx="vitalije.20081125152732.24">def redo_replacement(self):
c = self.c; u=c.undoer; bunch=u.getBead(u.bead+1)
c.setBodyString(bunch.p, bunch.newBody)
c.frame.body.setInsertPoint(bunch.insertPos)</t>
<t tx="vitalije.20081125152732.25">def exit(self):
#g.app.config.set(c,word_completer_key,'WordCompleter',None)
#g.app.config.set(c,'next_word_completer','WordCompleter',None)
pass</t>
<t tx="vitalije.20081125152732.26">def run(self):
self.adjust(self.p, c.frame.body.getInsertPoint())
self.complete_word()
</t>
<t tx="vitalije.20081125152732.27">def adjust(self, p, pos):
if p != self.p or pos != self.pos:
self.p = p.copy()
self.pos = pos
bs = p.bodyString()
word = getCurrentWord(bs, pos)
self.word = word
self.before = bs[:pos-len(word)]
self.after = bs[pos:]
self.search_pos = p.copy()
if self.back:
self.chunk = self.before
else:
self.chunk = self.after
self.tried = {word:1}</t>
<t tx="vitalije.20081125152732.28">def getCurrentWord(s, pos):
i = pos-1
while i>0 and wordsep.find(s[i]) < 0:
i -= 1
return s[i+1:pos]</t>
</tnodes>
</leo_file>
CUT TO HERE
I have placed those two nodes in myLeoSettings.leo in @commands node,
and also I have created two shortcuts
Alt-, = command-complete-previous
Alt-. = command-complete-next
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"leo-editor" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/leo-editor?hl=en
-~----------~----~----~----~------~----~------~--~---