Re: [textadept] pointers for implementing abbreviation expansion

From: mitchell <c....at.caladbolg.net>
Date: Fri, 8 Apr 2011 09:03:14 -0400 (Eastern Daylight Time)

On Fri, 8 Apr 2011, mitchell wrote:

> Hi David,
>
> On Fri, 8 Apr 2011, David Tweed wrote:
>
>> Hi,
>>
>> I've been looking around for an simple text editor to try some UI
>> experiments with and I'm looking at textadept. In particular, I'm
>> looking at fully automatic abbreviation expansion (the standard idea
>> is that if you've stored an abbreviation "phy"->"phylogenetic", then
>> if you type p-h-y-<non-word character> the editor replaces the phy
>> with phylogenetic, leaving the "insertion point" after the <non-word
>> character> after it's finished). A websearch doesn't reveal any links,
>> so it looks like this hasn't been implemented already. I know lots of
>> languages, but Lua is one I've never done anything more than tweak
>> existing scripts with. So I'm taking the liberty of asking a question
>> of those familiar with these things:
>
> I would certainly take advantage of the snippets module that is already
> available. You can probably then hook autoexpansion into the 'char_added'
> event.
>
> events.connect('char_added',
> function() _m.textadept.snippets.insert() end)

I should add that in the interest of performance, maybe you only want to
limit autoexpansion to a certain set of characters:

events.connect('char_added', function(c)
   if c >= 65 and c <= 90 or c >= 97 and c <= 122 then
     _m.textadept.snippets.insert()
   end
end

The above only tries to autoexpand after [A-Za-z]

>
> I haven't tested this, but in theory it should work.
>
> mitchell
>
>>
>> 1. I've managed to cobble/cut-n-paste together something that is a
>> skeleton of what I want to do, but there's something I can't figure
>> out what to do. At the line marked HERE I want to set the selection to
>> the word preceding the punctuation just typed, but I can't see an
>> existing bit of code that does this to copy. What's the best way to do
>> this?
>>
>> EXPANSION_CHARS={["
>> "]=true,["."]=true,[","]=true,["\""]=true,["!"]=true,["?"]=true,["*"]=true,["_"]=true}
>> ABBR_CHARS={["A"]=true,["a"]=true,["S"]=true,["s"]=true,
>> ["D"]=true,["d"]=true,["F"]=true,["f"]=true,
>> ["G"]=true,["g"]=true,["H"]=true,["h"]=true,
>> ["J"]=true,["j"]=true,["K"]=true,["k"]=true,
>> ["L"]=true,["l"]=true,
>> ["'"]=true}
>> bigAbbrTable={["dflhd"]="drone",["gdd"]="bee"}
>>
>> --rudimentary abbrev expansion
>> events.connect('char_added', function(cc)
>> local c=string.char(cc)
>> print(c)
>> print(EXPANSION_CHARS[c])
>> if EXPANSION_CHARS[c] then
>> local buffer = buffer
>> --might have just finished a word, so get previous word ***** HERE
>> *******
>> buffer:word_left_extend()
>> local txt = buffer:get_sel_text()
>> print(txt)
>> --do nothing if it contains any non-abbreviation characters
>> for i = 1, #txt do
>> local t=txt:sub(i,i)
>> if ABBR_CHARS[t]==nil then
>> return -- previous contains out of abbreviation signifying
>> characters, so nothing to do
>> end
>> end
>> lowercaseWord=string.lower(txt)
>> --see if an abbreviation has been defined; if so replace
>> replacement=bigAbbrTable[lowercaseWord]
>> if replacement then
>> --massage replacement so case matches original
>> local newRepTable={}
>> for i = 1, #replacement do
>> local o=txt:sub(i,i)
>> local t = replacement:sub(i,i)
>> if o==string.upper(o) then -- if original letter uppercase,
>> convert replacement char to uppercase
>> t=string.upper(t)
>> end
>> table.insert(newRepTable,t)
>> end
>> buffer:replace_sel(table.concat(newRepTable))
>> end
>> end
>> end)
>>
>> (In addition, if anything should scream out in the above code as "not
>> the way to do that in Lua" I'd be very glad to know it.)
>>
>> Again, I know asking this rather than figuring it out myself the hard
>> way is a bit of a liberty, so many thanks for any help.
>>
>> --
>> cheers, dave tweed__________________________
>> computer vision reasearcher: david.tw....at.gmail.com
>> "while having code so boring anyone can maintain it, use Python." --
>> attempted insult seen on slashdot
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "textadept" group.
>> To post to this group, send email to textadept.at.googlegroups.com.
>> To unsubscribe from this group, send email to
>> textadept+unsubscribe.at.googlegroups.com.
>> For more options, visit this group at
>> http://groups.google.com/group/textadept?hl=en.
>>
>>
>
> mitchell
>
> --
> You received this message because you are subscribed to the Google Groups
> "textadept" group.
> To post to this group, send email to textadept.at.googlegroups.com.
> To unsubscribe from this group, send email to
> textadept+unsubscribe.at.googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/textadept?hl=en.
>
>

mitchell
Received on Fri 08 Apr 2011 - 09:03:14 EDT

This archive was generated by hypermail 2.2.0 : Thu 08 Mar 2012 - 12:05:10 EST