Re: [textadept] pointers for implementing abbreviation expansion

From: mitchell <>
Date: Fri, 8 Apr 2011 08:59:26 -0400 (Eastern Daylight Time)

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'

   function() _m.textadept.snippets.insert() end)

I haven't tested this, but in theory it should work.


> 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?
> "]=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)
> 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:
> "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
> To unsubscribe from this group, send email to
> For more options, visit this group at

Received on Fri 08 Apr 2011 - 08:59:26 EDT

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