Scite 词语替换

lua-users home
wiki

这是用于 SciTE 的一个简单的即时替换功能的代码;它类似于 Word 自动将“the”替换为“teh”的功能。无论你认为它的实用性如何(你可能同意 Phillipe 的观点,认为它会让人变得懒惰),它都展示了 SciTE Lua 扩展如何访问每个输入的单词。我们不会对每种文件类型使用相同的单词列表(“fun”在 Lua 和 Pascal 文件中扩展为“function”,而在文本文件中则不会);这是通过观察活动文件何时更改来实现的,无论是通过打开(使用 OnOpen)还是通过切换缓冲区(使用 OnSwitchFile

-- doing word substitutions on the fly!

local txt_words = {
 teh='the', wd='would',cd='could'   
}

local pascal_words = {
 fun='function',lfun='local function',
 proc='procedure',virt='virtual',ctor='constructor',
 dtor='destructor',prog='program',
 int='integer',dbl='double',str='string'
}

local words

function switch_substitution_table()
  local ext = props['FileExt']
  if ext == 'pas' or ext == 'lua' then 
    words = pascal_words  
  elseif ext == 'txt' then
    words = txt_words
  else
    words = nil
  end
end

local function word_substitute(word)
  return words and words[word] or word
end

local word_start,in_word,current_word
local find = string.find

function OnChar(s)
 if not in_word then
    if find(s,'%w') then 
      -- we have hit a word!
      word_start = editor.CurrentPos
      in_word = true
      current_word = s
    end
 else -- we're in a word
   -- and it's another word character, so collect
   if find(s,'%w') then   
      current_word = current_word..s
   else
    -- leaving a word; see if we have a substitution
      local word_end = editor.CurrentPos
      local subst = word_substitute(current_word)
      if subst ~= current_word then
         editor:SetSel(word_start-1,word_end-1)
         -- this is somewhat ad-hoc logic, but
         -- SciTE is handling space differently.
         local was_whitespace = find(s,'%s')
         if was_whitespace then
            subst = subst..s
         end
	 editor:ReplaceSel(subst)
         word_end = editor.CurrentPos
         if not was_whitespace then
            editor:GotoPos(word_end + 1)
         end
      end
      in_word = false
   end   
  end 
  -- don't interfere with usual processing!
  return false
end  

function OnOpen(f)
  switch_substitution_table()
end

function OnSwitchFile(f)
  switch_substitution_table()
end

SteveDonovan


最近更改 · 偏好设置
编辑 · 历史记录
最后编辑于 2006 年 10 月 22 日下午 11:46 GMT (差异)