pointers for implementing abbreviation expansion

From: David Tweed <david.tw....at.gmail.com>
Date: Fri, 8 Apr 2011 02:10:39 +0100


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:

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


--rudimentary abbrev expansion
events.connect('char_added', function(cc)
  local c=string.char(cc)
  if EXPANSION_CHARS[c] then
    local buffer = buffer
    --might have just finished a word, so get previous word ***** HERE *******
    local txt = buffer:get_sel_text()
    --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
    --see if an abbreviation has been defined; if so replace
    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

(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
Received on Thu 07 Apr 2011 - 21:10:39 EDT

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